Merge master.kernel.org:/pub/scm/linux/kernel/git/kyle/parisc-2.6
[sfrench/cifs-2.6.git] / drivers / scsi / ibmmca.c
1 /*
2  Low Level Linux Driver for the IBM Microchannel SCSI Subsystem for
3  Linux Kernel >= 2.4.0.
4  Copyright (c) 1995 Strom Systems, Inc. under the terms of the GNU
5  General Public License. Written by Martin Kolinek, December 1995.
6  Further development by: Chris Beauregard, Klaus Kudielka, Michael Lang
7  See the file Documentation/scsi/ibmmca.txt for a detailed description
8  of this driver, the commandline arguments and the history of its
9  development.
10  See the WWW-page: http://www.uni-mainz.de/~langm000/linux.html for latest
11  updates, info and ADF-files for adapters supported by this driver.
12
13  Alan Cox <alan@redhat.com>
14  Updated for Linux 2.5.45 to use the new error handler, cleaned up the
15  lock macros and did a few unavoidable locking tweaks, plus one locking
16  fix in the irq and completion path.
17  
18  */
19
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/types.h>
24 #include <linux/ctype.h>
25 #include <linux/string.h>
26 #include <linux/interrupt.h>
27 #include <linux/ioport.h>
28 #include <linux/delay.h>
29 #include <linux/blkdev.h>
30 #include <linux/proc_fs.h>
31 #include <linux/stat.h>
32 #include <linux/mca.h>
33 #include <linux/spinlock.h>
34 #include <linux/init.h>
35 #include <linux/mca-legacy.h>
36
37 #include <asm/system.h>
38 #include <asm/io.h>
39
40 #include "scsi.h"
41 #include <scsi/scsi_host.h>
42 #include "ibmmca.h"
43
44 /* current version of this driver-source: */
45 #define IBMMCA_SCSI_DRIVER_VERSION "4.0b-ac"
46
47 /* driver configuration */
48 #define IM_MAX_HOSTS     8      /* maximum number of host adapters */
49 #define IM_RESET_DELAY  60      /* seconds allowed for a reset */
50
51 /* driver debugging - #undef all for normal operation */
52 /* if defined: count interrupts and ignore this special one: */
53 #undef  IM_DEBUG_TIMEOUT        //50
54 #define TIMEOUT_PUN     0
55 #define TIMEOUT_LUN     0
56 /* verbose interrupt: */
57 #undef IM_DEBUG_INT
58 /* verbose queuecommand: */
59 #undef IM_DEBUG_CMD
60 /* verbose queucommand for specific SCSI-device type: */
61 #undef IM_DEBUG_CMD_SPEC_DEV
62 /* verbose device probing */
63 #undef IM_DEBUG_PROBE
64
65 /* device type that shall be displayed on syslog (only during debugging): */
66 #define IM_DEBUG_CMD_DEVICE     TYPE_TAPE
67
68 /* relative addresses of hardware registers on a subsystem */
69 #define IM_CMD_REG(hi)  (hosts[(hi)]->io_port)  /*Command Interface, (4 bytes long) */
70 #define IM_ATTN_REG(hi) (hosts[(hi)]->io_port+4)        /*Attention (1 byte) */
71 #define IM_CTR_REG(hi)  (hosts[(hi)]->io_port+5)        /*Basic Control (1 byte) */
72 #define IM_INTR_REG(hi) (hosts[(hi)]->io_port+6)        /*Interrupt Status (1 byte, r/o) */
73 #define IM_STAT_REG(hi) (hosts[(hi)]->io_port+7)        /*Basic Status (1 byte, read only) */
74
75 /* basic I/O-port of first adapter */
76 #define IM_IO_PORT      0x3540
77 /* maximum number of hosts that can be found */
78 #define IM_N_IO_PORT    8
79
80 /*requests going into the upper nibble of the Attention register */
81 /*note: the lower nibble specifies the device(0-14), or subsystem(15) */
82 #define IM_IMM_CMD      0x10    /*immediate command */
83 #define IM_SCB          0x30    /*Subsystem Control Block command */
84 #define IM_LONG_SCB     0x40    /*long Subsystem Control Block command */
85 #define IM_EOI          0xe0    /*end-of-interrupt request */
86
87 /*values for bits 7,1,0 of Basic Control reg. (bits 6-2 reserved) */
88 #define IM_HW_RESET     0x80    /*hardware reset */
89 #define IM_ENABLE_DMA   0x02    /*enable subsystem's busmaster DMA */
90 #define IM_ENABLE_INTR  0x01    /*enable interrupts to the system */
91
92 /*to interpret the upper nibble of Interrupt Status register */
93 /*note: the lower nibble specifies the device(0-14), or subsystem(15) */
94 #define IM_SCB_CMD_COMPLETED                    0x10
95 #define IM_SCB_CMD_COMPLETED_WITH_RETRIES       0x50
96 #define IM_LOOP_SCATTER_BUFFER_FULL             0x60
97 #define IM_ADAPTER_HW_FAILURE                   0x70
98 #define IM_IMMEDIATE_CMD_COMPLETED              0xa0
99 #define IM_CMD_COMPLETED_WITH_FAILURE           0xc0
100 #define IM_CMD_ERROR                            0xe0
101 #define IM_SOFTWARE_SEQUENCING_ERROR            0xf0
102
103 /*to interpret bits 3-0 of Basic Status register (bits 7-4 reserved) */
104 #define IM_CMD_REG_FULL         0x08
105 #define IM_CMD_REG_EMPTY        0x04
106 #define IM_INTR_REQUEST         0x02
107 #define IM_BUSY                 0x01
108
109 /*immediate commands (word written into low 2 bytes of command reg) */
110 #define IM_RESET_IMM_CMD        0x0400
111 #define IM_FEATURE_CTR_IMM_CMD  0x040c
112 #define IM_DMA_PACING_IMM_CMD   0x040d
113 #define IM_ASSIGN_IMM_CMD       0x040e
114 #define IM_ABORT_IMM_CMD        0x040f
115 #define IM_FORMAT_PREP_IMM_CMD  0x0417
116
117 /*SCB (Subsystem Control Block) structure */
118 struct im_scb {
119         unsigned short command; /*command word (read, etc.) */
120         unsigned short enable;  /*enable word, modifies cmd */
121         union {
122                 unsigned long log_blk_adr;      /*block address on SCSI device */
123                 unsigned char scsi_cmd_length;  /*6,10,12, for other scsi cmd */
124         } u1;
125         unsigned long sys_buf_adr;      /*physical system memory adr */
126         unsigned long sys_buf_length;   /*size of sys mem buffer */
127         unsigned long tsb_adr;  /*Termination Status Block adr */
128         unsigned long scb_chain_adr;    /*optional SCB chain address */
129         union {
130                 struct {
131                         unsigned short count;   /*block count, on SCSI device */
132                         unsigned short length;  /*block length, on SCSI device */
133                 } blk;
134                 unsigned char scsi_command[12]; /*other scsi command */
135         } u2;
136 };
137
138 /*structure scatter-gather element (for list of system memory areas) */
139 struct im_sge {
140         void *address;
141         unsigned long byte_length;
142 };
143
144 /*structure returned by a get_pos_info command: */
145 struct im_pos_info {
146         unsigned short pos_id;  /* adapter id */
147         unsigned char pos_3a;   /* pos 3 (if pos 6 = 0) */
148         unsigned char pos_2;    /* pos 2 */
149         unsigned char int_level;        /* interrupt level IRQ 11 or 14 */
150         unsigned char pos_4a;   /* pos 4 (if pos 6 = 0) */
151         unsigned short connector_size;  /* MCA connector size: 16 or 32 Bit */
152         unsigned char num_luns; /* number of supported luns per device */
153         unsigned char num_puns; /* number of supported puns */
154         unsigned char pacing_factor;    /* pacing factor */
155         unsigned char num_ldns; /* number of ldns available */
156         unsigned char eoi_off;  /* time EOI and interrupt inactive */
157         unsigned char max_busy; /* time between reset and busy on */
158         unsigned short cache_stat;      /* ldn cachestat. Bit=1 = not cached */
159         unsigned short retry_stat;      /* retry status of ldns. Bit=1=disabled */
160         unsigned char pos_4b;   /* pos 4 (if pos 6 = 1) */
161         unsigned char pos_3b;   /* pos 3 (if pos 6 = 1) */
162         unsigned char pos_6;    /* pos 6 */
163         unsigned char pos_5;    /* pos 5 */
164         unsigned short max_overlap;     /* maximum overlapping requests */
165         unsigned short num_bus; /* number of SCSI-busses */
166 };
167
168 /*values for SCB command word */
169 #define IM_NO_SYNCHRONOUS      0x0040   /*flag for any command */
170 #define IM_NO_DISCONNECT       0x0080   /*flag for any command */
171 #define IM_READ_DATA_CMD       0x1c01
172 #define IM_WRITE_DATA_CMD      0x1c02
173 #define IM_READ_VERIFY_CMD     0x1c03
174 #define IM_WRITE_VERIFY_CMD    0x1c04
175 #define IM_REQUEST_SENSE_CMD   0x1c08
176 #define IM_READ_CAPACITY_CMD   0x1c09
177 #define IM_DEVICE_INQUIRY_CMD  0x1c0b
178 #define IM_READ_LOGICAL_CMD    0x1c2a
179 #define IM_OTHER_SCSI_CMD_CMD  0x241f
180
181 /* unused, but supported, SCB commands */
182 #define IM_GET_COMMAND_COMPLETE_STATUS_CMD   0x1c07     /* command status */
183 #define IM_GET_POS_INFO_CMD                  0x1c0a     /* returns neat stuff */
184 #define IM_READ_PREFETCH_CMD                 0x1c31     /* caching controller only */
185 #define IM_FOMAT_UNIT_CMD                    0x1c16     /* format unit */
186 #define IM_REASSIGN_BLOCK_CMD                0x1c18     /* in case of error */
187
188 /*values to set bits in the enable word of SCB */
189 #define IM_READ_CONTROL              0x8000
190 #define IM_REPORT_TSB_ONLY_ON_ERROR  0x4000
191 #define IM_RETRY_ENABLE              0x2000
192 #define IM_POINTER_TO_LIST           0x1000
193 #define IM_SUPRESS_EXCEPTION_SHORT   0x0400
194 #define IM_BYPASS_BUFFER             0x0200
195 #define IM_CHAIN_ON_NO_ERROR         0x0001
196
197 /*TSB (Termination Status Block) structure */
198 struct im_tsb {
199         unsigned short end_status;
200         unsigned short reserved1;
201         unsigned long residual_byte_count;
202         unsigned long sg_list_element_adr;
203         unsigned short status_length;
204         unsigned char dev_status;
205         unsigned char cmd_status;
206         unsigned char dev_error;
207         unsigned char cmd_error;
208         unsigned short reserved2;
209         unsigned short reserved3;
210         unsigned short low_of_last_scb_adr;
211         unsigned short high_of_last_scb_adr;
212 };
213
214 /*subsystem uses interrupt request level 14 */
215 #define IM_IRQ     14
216 /*SCSI-2 F/W may evade to interrupt 11 */
217 #define IM_IRQ_FW  11
218
219 /* Model 95 has an additional alphanumeric display, which can be used
220    to display SCSI-activities. 8595 models do not have any disk led, which
221    makes this feature quite useful.
222    The regular PS/2 disk led is turned on/off by bits 6,7 of system
223    control port. */
224
225 /* LED display-port (actually, last LED on display) */
226 #define MOD95_LED_PORT     0x108
227 /* system-control-register of PS/2s with diskindicator */
228 #define PS2_SYS_CTR        0x92
229 /* activity displaying methods */
230 #define LED_DISP           1
231 #define LED_ADISP          2
232 #define LED_ACTIVITY       4
233 /* failed intr */
234 #define CMD_FAIL           255
235
236 /* The SCSI-ID(!) of the accessed SCSI-device is shown on PS/2-95 machines' LED
237    displays. ldn is no longer displayed here, because the ldn mapping is now 
238    done dynamically and the ldn <-> pun,lun maps can be looked-up at boottime 
239    or during uptime in /proc/scsi/ibmmca/<host_no> in case of trouble, 
240    interest, debugging or just for having fun. The left number gives the
241    host-adapter number and the right shows the accessed SCSI-ID. */
242
243 /* display_mode is set by the ibmmcascsi= command line arg */
244 static int display_mode = 0;
245 /* set default adapter timeout */
246 static unsigned int adapter_timeout = 45;
247 /* for probing on feature-command: */
248 static unsigned int global_command_error_excuse = 0;
249 /* global setting by command line for adapter_speed */
250 static int global_adapter_speed = 0;    /* full speed by default */
251
252 /* Panel / LED on, do it right for F/W addressin, too. adisplay will
253  * just ignore ids>7, as the panel has only 7 digits available */
254 #define PS2_DISK_LED_ON(ad,id) { if (display_mode & LED_DISP) { if (id>9) \
255     outw((ad+48)|((id+55)<<8), MOD95_LED_PORT ); else \
256     outw((ad+48)|((id+48)<<8), MOD95_LED_PORT ); } else \
257     if (display_mode & LED_ADISP) { if (id<7) outb((char)(id+48),MOD95_LED_PORT+1+id); \
258     outb((char)(ad+48), MOD95_LED_PORT); } \
259     if ((display_mode & LED_ACTIVITY)||(!display_mode)) \
260     outb(inb(PS2_SYS_CTR) | 0xc0, PS2_SYS_CTR); }
261
262 /* Panel / LED off */
263 /* bug fixed, Dec 15, 1997, where | was replaced by & here */
264 #define PS2_DISK_LED_OFF() { if (display_mode & LED_DISP) \
265     outw(0x2020, MOD95_LED_PORT ); else if (display_mode & LED_ADISP) { \
266     outl(0x20202020,MOD95_LED_PORT); outl(0x20202020,MOD95_LED_PORT+4); } \
267     if ((display_mode & LED_ACTIVITY)||(!display_mode)) \
268     outb(inb(PS2_SYS_CTR) & 0x3f, PS2_SYS_CTR); }
269
270 /*list of supported subsystems */
271 struct subsys_list_struct {
272         unsigned short mca_id;
273         char *description;
274 };
275
276 /* types of different supported hardware that goes to hostdata special */
277 #define IBM_SCSI2_FW     0
278 #define IBM_7568_WCACHE  1
279 #define IBM_EXP_UNIT     2
280 #define IBM_SCSI_WCACHE  3
281 #define IBM_SCSI         4
282
283 /* other special flags for hostdata structure */
284 #define FORCED_DETECTION         100
285 #define INTEGRATED_SCSI          101
286
287 /* List of possible IBM-SCSI-adapters */
288 static struct subsys_list_struct subsys_list[] = {
289         {0x8efc, "IBM SCSI-2 F/W Adapter"},     /* special = 0 */
290         {0x8efd, "IBM 7568 Industrial Computer SCSI Adapter w/Cache"},  /* special = 1 */
291         {0x8ef8, "IBM Expansion Unit SCSI Controller"}, /* special = 2 */
292         {0x8eff, "IBM SCSI Adapter w/Cache"},   /* special = 3 */
293         {0x8efe, "IBM SCSI Adapter"},   /* special = 4 */
294 };
295
296 /* Max number of logical devices (can be up from 0 to 14).  15 is the address
297 of the adapter itself. */
298 #define MAX_LOG_DEV  15
299
300 /*local data for a logical device */
301 struct logical_device {
302         struct im_scb scb;      /* SCSI-subsystem-control-block structure */
303         struct im_tsb tsb;      /* SCSI command complete status block structure */
304         struct im_sge sge[16];  /* scatter gather list structure */
305         unsigned char buf[256]; /* SCSI command return data buffer */
306         Scsi_Cmnd *cmd;         /* SCSI-command that is currently in progress */
307         int device_type;        /* type of the SCSI-device. See include/scsi/scsi.h
308                                    for interpretation of the possible values */
309         int block_length;       /* blocksize of a particular logical SCSI-device */
310         int cache_flag;         /* 1 if this is uncached, 0 if cache is present for ldn */
311         int retry_flag;         /* 1 if adapter retry is disabled, 0 if enabled */
312 };
313
314 /* statistics of the driver during operations (for proc_info) */
315 struct Driver_Statistics {
316         /* SCSI statistics on the adapter */
317         int ldn_access[MAX_LOG_DEV + 1];        /* total accesses on a ldn */
318         int ldn_read_access[MAX_LOG_DEV + 1];   /* total read-access on a ldn */
319         int ldn_write_access[MAX_LOG_DEV + 1];  /* total write-access on a ldn */
320         int ldn_inquiry_access[MAX_LOG_DEV + 1];        /* total inquiries on a ldn */
321         int ldn_modeselect_access[MAX_LOG_DEV + 1];     /* total mode selects on ldn */
322         int scbs;               /* short SCBs queued */
323         int long_scbs;          /* long SCBs queued */
324         int total_accesses;     /* total accesses on all ldns */
325         int total_interrupts;   /* total interrupts (should be
326                                    same as total_accesses) */
327         int total_errors;       /* command completed with error */
328         /* dynamical assignment statistics */
329         int total_scsi_devices; /* number of physical pun,lun */
330         int dyn_flag;           /* flag showing dynamical mode */
331         int dynamical_assignments;      /* number of remappings of ldns */
332         int ldn_assignments[MAX_LOG_DEV + 1];   /* number of remappings of each
333                                                    ldn */
334 };
335
336 /* data structure for each host adapter */
337 struct ibmmca_hostdata {
338         /* array of logical devices: */
339         struct logical_device _ld[MAX_LOG_DEV + 1];
340         /* array to convert (pun, lun) into logical device number: */
341         unsigned char _get_ldn[16][8];
342         /*array that contains the information about the physical SCSI-devices
343            attached to this host adapter: */
344         unsigned char _get_scsi[16][8];
345         /* used only when checking logical devices: */
346         int _local_checking_phase_flag;
347         /* report received interrupt: */
348         int _got_interrupt;
349         /* report termination-status of SCSI-command: */
350         int _stat_result;
351         /* reset status (used only when doing reset): */
352         int _reset_status;
353         /* code of the last SCSI command (needed for panic info): */
354         int _last_scsi_command[MAX_LOG_DEV + 1];
355         /* identifier of the last SCSI-command type */
356         int _last_scsi_type[MAX_LOG_DEV + 1];
357         /* last blockcount */
358         int _last_scsi_blockcount[MAX_LOG_DEV + 1];
359         /* last locgical block address */
360         unsigned long _last_scsi_logical_block[MAX_LOG_DEV + 1];
361         /* Counter that points on the next reassignable ldn for dynamical
362            remapping. The default value is 7, that is the first reassignable
363            number in the list at boottime: */
364         int _next_ldn;
365         /* Statistics-structure for this IBM-SCSI-host: */
366         struct Driver_Statistics _IBM_DS;
367         /* This hostadapters pos-registers pos2 until pos6 */
368         unsigned int _pos[8];
369         /* assign a special variable, that contains dedicated info about the
370            adaptertype */
371         int _special;
372         /* connector size on the MCA bus */
373         int _connector_size;
374         /* synchronous SCSI transfer rate bitpattern */
375         int _adapter_speed;
376 };
377
378 /* macros to access host data structure */
379 #define subsystem_pun(hi) (hosts[(hi)]->this_id)
380 #define subsystem_maxid(hi) (hosts[(hi)]->max_id)
381 #define ld(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_ld)
382 #define get_ldn(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_get_ldn)
383 #define get_scsi(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_get_scsi)
384 #define local_checking_phase_flag(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_local_checking_phase_flag)
385 #define got_interrupt(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_got_interrupt)
386 #define stat_result(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_stat_result)
387 #define reset_status(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_reset_status)
388 #define last_scsi_command(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_last_scsi_command)
389 #define last_scsi_type(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_last_scsi_type)
390 #define last_scsi_blockcount(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_last_scsi_blockcount)
391 #define last_scsi_logical_block(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_last_scsi_logical_block)
392 #define last_scsi_type(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_last_scsi_type)
393 #define next_ldn(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_next_ldn)
394 #define IBM_DS(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_IBM_DS)
395 #define special(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_special)
396 #define subsystem_connector_size(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_connector_size)
397 #define adapter_speed(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_adapter_speed)
398 #define pos2(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_pos[2])
399 #define pos3(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_pos[3])
400 #define pos4(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_pos[4])
401 #define pos5(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_pos[5])
402 #define pos6(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_pos[6])
403
404 /* Define a arbitrary number as subsystem-marker-type. This number is, as
405    described in the ANSI-SCSI-standard, not occupied by other device-types. */
406 #define TYPE_IBM_SCSI_ADAPTER   0x2F
407
408 /* Define 0xFF for no device type, because this type is not defined within
409    the ANSI-SCSI-standard, therefore, it can be used and should not cause any
410    harm. */
411 #define TYPE_NO_DEVICE          0xFF
412
413 /* define medium-changer. If this is not defined previously, e.g. Linux
414    2.0.x, define this type here. */
415 #ifndef TYPE_MEDIUM_CHANGER
416 #define TYPE_MEDIUM_CHANGER     0x08
417 #endif
418
419 /* define possible operations for the immediate_assign command */
420 #define SET_LDN        0
421 #define REMOVE_LDN     1
422
423 /* ldn which is used to probe the SCSI devices */
424 #define PROBE_LDN      0
425
426 /* reset status flag contents */
427 #define IM_RESET_NOT_IN_PROGRESS         0
428 #define IM_RESET_IN_PROGRESS             1
429 #define IM_RESET_FINISHED_OK             2
430 #define IM_RESET_FINISHED_FAIL           3
431 #define IM_RESET_NOT_IN_PROGRESS_NO_INT  4
432 #define IM_RESET_FINISHED_OK_NO_INT      5
433
434 /* define undefined SCSI-command */
435 #define NO_SCSI                  0xffff
436
437 /*-----------------------------------------------------------------------*/
438
439 /* if this is nonzero, ibmmcascsi option has been passed to the kernel */
440 static int io_port[IM_MAX_HOSTS] = { 0, 0, 0, 0, 0, 0, 0, 0 };
441 static int scsi_id[IM_MAX_HOSTS] = { 7, 7, 7, 7, 7, 7, 7, 7 };
442
443 /* fill module-parameters only, when this define is present.
444    (that is kernel version 2.1.x) */
445 #if defined(MODULE)
446 static char *boot_options = NULL;
447 module_param(boot_options, charp, 0);
448 module_param_array(io_port, int, NULL, 0);
449 module_param_array(scsi_id, int, NULL, 0);
450
451 #if 0 /* FIXME: No longer exist? --RR */
452 MODULE_PARM(display, "1i");
453 MODULE_PARM(adisplay, "1i");
454 MODULE_PARM(normal, "1i");
455 MODULE_PARM(ansi, "1i");
456 #endif
457
458 MODULE_LICENSE("GPL");
459 #endif
460 /*counter of concurrent disk read/writes, to turn on/off disk led */
461 static int disk_rw_in_progress = 0;
462
463 /* host information */
464 static int found = 0;
465 static struct Scsi_Host *hosts[IM_MAX_HOSTS + 1] = {
466         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
467 };
468 static unsigned int pos[8];     /* whole pos register-line for diagnosis */
469 /* Taking into account the additions, made by ZP Gu.
470  * This selects now the preset value from the configfile and
471  * offers the 'normal' commandline option to be accepted */
472 #ifdef CONFIG_IBMMCA_SCSI_ORDER_STANDARD
473 static char ibm_ansi_order = 1;
474 #else
475 static char ibm_ansi_order = 0;
476 #endif
477
478 static void issue_cmd(int, unsigned long, unsigned char);
479 static void internal_done(Scsi_Cmnd * cmd);
480 static void check_devices(int, int);
481 static int immediate_assign(int, unsigned int, unsigned int, unsigned int, unsigned int);
482 static int immediate_feature(int, unsigned int, unsigned int);
483 #ifdef CONFIG_IBMMCA_SCSI_DEV_RESET
484 static int immediate_reset(int, unsigned int);
485 #endif
486 static int device_inquiry(int, int);
487 static int read_capacity(int, int);
488 static int get_pos_info(int);
489 static char *ti_p(int);
490 static char *ti_l(int);
491 static char *ibmrate(unsigned int, int);
492 static int probe_display(int);
493 static int probe_bus_mode(int);
494 static int device_exists(int, int, int *, int *);
495 static struct Scsi_Host *ibmmca_register(struct scsi_host_template *, int, int, int, char *);
496 static int option_setup(char *);
497 /* local functions needed for proc_info */
498 static int ldn_access_load(int, int);
499 static int ldn_access_total_read_write(int);
500
501 static irqreturn_t interrupt_handler(int irq, void *dev_id,
502                                         struct pt_regs *regs)
503 {
504         int host_index, ihost_index;
505         unsigned int intr_reg;
506         unsigned int cmd_result;
507         unsigned int ldn;
508         Scsi_Cmnd *cmd;
509         int lastSCSI;
510         struct Scsi_Host *dev = dev_id;
511
512         spin_lock(dev->host_lock);
513             /* search for one adapter-response on shared interrupt */
514             for (host_index = 0; hosts[host_index] && !(inb(IM_STAT_REG(host_index)) & IM_INTR_REQUEST); host_index++);
515         /* return if some other device on this IRQ caused the interrupt */
516         if (!hosts[host_index]) {
517                 spin_unlock(dev->host_lock);
518                 return IRQ_NONE;
519         }
520
521         /* the reset-function already did all the job, even ints got
522            renabled on the subsystem, so just return */
523         if ((reset_status(host_index) == IM_RESET_NOT_IN_PROGRESS_NO_INT) || (reset_status(host_index) == IM_RESET_FINISHED_OK_NO_INT)) {
524                 reset_status(host_index) = IM_RESET_NOT_IN_PROGRESS;
525                 spin_unlock(dev->host_lock);
526                 return IRQ_HANDLED;
527         }
528
529         /*must wait for attention reg not busy, then send EOI to subsystem */
530         while (1) {
531                 if (!(inb(IM_STAT_REG(host_index)) & IM_BUSY))
532                         break;
533                 cpu_relax();
534         }
535         ihost_index = host_index;
536         /*get command result and logical device */
537         intr_reg = (unsigned char) (inb(IM_INTR_REG(ihost_index)));
538         cmd_result = intr_reg & 0xf0;
539         ldn = intr_reg & 0x0f;
540         /* get the last_scsi_command here */
541         lastSCSI = last_scsi_command(ihost_index)[ldn];
542         outb(IM_EOI | ldn, IM_ATTN_REG(ihost_index));
543         
544         /*these should never happen (hw fails, or a local programming bug) */
545         if (!global_command_error_excuse) {
546                 switch (cmd_result) {
547                         /* Prevent from Ooopsing on error to show the real reason */
548                 case IM_ADAPTER_HW_FAILURE:
549                 case IM_SOFTWARE_SEQUENCING_ERROR:
550                 case IM_CMD_ERROR:
551                         printk(KERN_ERR "IBM MCA SCSI: Fatal Subsystem ERROR!\n");
552                         printk(KERN_ERR "              Last cmd=0x%x, ena=%x, len=", lastSCSI, ld(ihost_index)[ldn].scb.enable);
553                         if (ld(ihost_index)[ldn].cmd)
554                                 printk("%ld/%ld,", (long) (ld(ihost_index)[ldn].cmd->request_bufflen), (long) (ld(ihost_index)[ldn].scb.sys_buf_length));
555                         else
556                                 printk("none,");
557                         if (ld(ihost_index)[ldn].cmd)
558                                 printk("Blocksize=%d", ld(ihost_index)[ldn].scb.u2.blk.length);
559                         else
560                                 printk("Blocksize=none");
561                         printk(", host=0x%x, ldn=0x%x\n", ihost_index, ldn);
562                         if (ld(ihost_index)[ldn].cmd) {
563                                 printk(KERN_ERR "Blockcount=%d/%d\n", last_scsi_blockcount(ihost_index)[ldn], ld(ihost_index)[ldn].scb.u2.blk.count);
564                                 printk(KERN_ERR "Logical block=%lx/%lx\n", last_scsi_logical_block(ihost_index)[ldn], ld(ihost_index)[ldn].scb.u1.log_blk_adr);
565                         }
566                         printk(KERN_ERR "Reason given: %s\n", (cmd_result == IM_ADAPTER_HW_FAILURE) ? "HARDWARE FAILURE" : (cmd_result == IM_SOFTWARE_SEQUENCING_ERROR) ? "SOFTWARE SEQUENCING ERROR" : (cmd_result == IM_CMD_ERROR) ? "COMMAND ERROR" : "UNKNOWN");
567                         /* if errors appear, enter this section to give detailed info */
568                         printk(KERN_ERR "IBM MCA SCSI: Subsystem Error-Status follows:\n");
569                         printk(KERN_ERR "              Command Type................: %x\n", last_scsi_type(ihost_index)[ldn]);
570                         printk(KERN_ERR "              Attention Register..........: %x\n", inb(IM_ATTN_REG(ihost_index)));
571                         printk(KERN_ERR "              Basic Control Register......: %x\n", inb(IM_CTR_REG(ihost_index)));
572                         printk(KERN_ERR "              Interrupt Status Register...: %x\n", intr_reg);
573                         printk(KERN_ERR "              Basic Status Register.......: %x\n", inb(IM_STAT_REG(ihost_index)));
574                         if ((last_scsi_type(ihost_index)[ldn] == IM_SCB) || (last_scsi_type(ihost_index)[ldn] == IM_LONG_SCB)) {
575                                 printk(KERN_ERR "              SCB-Command.................: %x\n", ld(ihost_index)[ldn].scb.command);
576                                 printk(KERN_ERR "              SCB-Enable..................: %x\n", ld(ihost_index)[ldn].scb.enable);
577                                 printk(KERN_ERR "              SCB-logical block address...: %lx\n", ld(ihost_index)[ldn].scb.u1.log_blk_adr);
578                                 printk(KERN_ERR "              SCB-system buffer address...: %lx\n", ld(ihost_index)[ldn].scb.sys_buf_adr);
579                                 printk(KERN_ERR "              SCB-system buffer length....: %lx\n", ld(ihost_index)[ldn].scb.sys_buf_length);
580                                 printk(KERN_ERR "              SCB-tsb address.............: %lx\n", ld(ihost_index)[ldn].scb.tsb_adr);
581                                 printk(KERN_ERR "              SCB-Chain address...........: %lx\n", ld(ihost_index)[ldn].scb.scb_chain_adr);
582                                 printk(KERN_ERR "              SCB-block count.............: %x\n", ld(ihost_index)[ldn].scb.u2.blk.count);
583                                 printk(KERN_ERR "              SCB-block length............: %x\n", ld(ihost_index)[ldn].scb.u2.blk.length);
584                         }
585                         printk(KERN_ERR "              Send this report to the maintainer.\n");
586                         panic("IBM MCA SCSI: Fatal error message from the subsystem (0x%X,0x%X)!\n", lastSCSI, cmd_result);
587                         break;
588                 }
589         } else {
590                 /* The command error handling is made silent, but we tell the
591                  * calling function, that there is a reported error from the
592                  * adapter. */
593                 switch (cmd_result) {
594                 case IM_ADAPTER_HW_FAILURE:
595                 case IM_SOFTWARE_SEQUENCING_ERROR:
596                 case IM_CMD_ERROR:
597                         global_command_error_excuse = CMD_FAIL;
598                         break;
599                 default:
600                         global_command_error_excuse = 0;
601                         break;
602                 }
603         }
604         /* if no panic appeared, increase the interrupt-counter */
605         IBM_DS(ihost_index).total_interrupts++;
606         /*only for local checking phase */
607         if (local_checking_phase_flag(ihost_index)) {
608                 stat_result(ihost_index) = cmd_result;
609                 got_interrupt(ihost_index) = 1;
610                 reset_status(ihost_index) = IM_RESET_FINISHED_OK;
611                 last_scsi_command(ihost_index)[ldn] = NO_SCSI;
612                 spin_unlock(dev->host_lock);
613                 return IRQ_HANDLED;
614         }
615         /* handling of commands coming from upper level of scsi driver */
616         if (last_scsi_type(ihost_index)[ldn] == IM_IMM_CMD) {
617                 /* verify ldn, and may handle rare reset immediate command */
618                 if ((reset_status(ihost_index) == IM_RESET_IN_PROGRESS) && (last_scsi_command(ihost_index)[ldn] == IM_RESET_IMM_CMD)) {
619                         if (cmd_result == IM_CMD_COMPLETED_WITH_FAILURE) {
620                                 disk_rw_in_progress = 0;
621                                 PS2_DISK_LED_OFF();
622                                 reset_status(ihost_index) = IM_RESET_FINISHED_FAIL;
623                         } else {
624                                 /*reset disk led counter, turn off disk led */
625                                 disk_rw_in_progress = 0;
626                                 PS2_DISK_LED_OFF();
627                                 reset_status(ihost_index) = IM_RESET_FINISHED_OK;
628                         }
629                         stat_result(ihost_index) = cmd_result;
630                         last_scsi_command(ihost_index)[ldn] = NO_SCSI;
631                         last_scsi_type(ihost_index)[ldn] = 0;
632                         spin_unlock(dev->host_lock);
633                         return IRQ_HANDLED;
634                 } else if (last_scsi_command(ihost_index)[ldn] == IM_ABORT_IMM_CMD) {
635                         /* react on SCSI abort command */
636 #ifdef IM_DEBUG_PROBE
637                         printk("IBM MCA SCSI: Interrupt from SCSI-abort.\n");
638 #endif
639                         disk_rw_in_progress = 0;
640                         PS2_DISK_LED_OFF();
641                         cmd = ld(ihost_index)[ldn].cmd;
642                         ld(ihost_index)[ldn].cmd = NULL;
643                         if (cmd_result == IM_CMD_COMPLETED_WITH_FAILURE)
644                                 cmd->result = DID_NO_CONNECT << 16;
645                         else
646                                 cmd->result = DID_ABORT << 16;
647                         stat_result(ihost_index) = cmd_result;
648                         last_scsi_command(ihost_index)[ldn] = NO_SCSI;
649                         last_scsi_type(ihost_index)[ldn] = 0;
650                         if (cmd->scsi_done)
651                                 (cmd->scsi_done) (cmd); /* should be the internal_done */
652                         spin_unlock(dev->host_lock);
653                         return IRQ_HANDLED;
654                 } else {
655                         disk_rw_in_progress = 0;
656                         PS2_DISK_LED_OFF();
657                         reset_status(ihost_index) = IM_RESET_FINISHED_OK;
658                         stat_result(ihost_index) = cmd_result;
659                         last_scsi_command(ihost_index)[ldn] = NO_SCSI;
660                         spin_unlock(dev->host_lock);
661                         return IRQ_HANDLED;
662                 }
663         }
664         last_scsi_command(ihost_index)[ldn] = NO_SCSI;
665         last_scsi_type(ihost_index)[ldn] = 0;
666         cmd = ld(ihost_index)[ldn].cmd;
667         ld(ihost_index)[ldn].cmd = NULL;
668 #ifdef IM_DEBUG_TIMEOUT
669         if (cmd) {
670                 if ((cmd->target == TIMEOUT_PUN) && (cmd->device->lun == TIMEOUT_LUN)) {
671                         printk("IBM MCA SCSI: Ignoring interrupt from pun=%x, lun=%x.\n", cmd->target, cmd->device->lun);
672                         return IRQ_HANDLED;
673                 }
674         }
675 #endif
676         /*if no command structure, just return, else clear cmd */
677         if (!cmd)
678         {
679                 spin_unlock(dev->host_lock);
680                 return IRQ_HANDLED;
681         }
682
683 #ifdef IM_DEBUG_INT
684         printk("cmd=%02x ireg=%02x ds=%02x cs=%02x de=%02x ce=%02x\n", cmd->cmnd[0], intr_reg, ld(ihost_index)[ldn].tsb.dev_status, ld(ihost_index)[ldn].tsb.cmd_status, ld(ihost_index)[ldn].tsb.dev_error, ld(ihost_index)[ldn].tsb.cmd_error);
685 #endif
686         /*if this is end of media read/write, may turn off PS/2 disk led */
687         if ((ld(ihost_index)[ldn].device_type != TYPE_NO_LUN) && (ld(ihost_index)[ldn].device_type != TYPE_NO_DEVICE)) {
688                 /* only access this, if there was a valid device addressed */
689                 if (--disk_rw_in_progress == 0)
690                         PS2_DISK_LED_OFF();
691         }
692
693         /* IBM describes the status-mask to be 0x1e, but this is not conform
694          * with SCSI-definition, I suppose, the reason for it is that IBM
695          * adapters do not support CMD_TERMINATED, TASK_SET_FULL and
696          * ACA_ACTIVE as returning statusbyte information. (ML) */
697         if (cmd_result == IM_CMD_COMPLETED_WITH_FAILURE) {
698                 cmd->result = (unsigned char) (ld(ihost_index)[ldn].tsb.dev_status & 0x1e);
699                 IBM_DS(ihost_index).total_errors++;
700         } else
701                 cmd->result = 0;
702         /* write device status into cmd->result, and call done function */
703         if (lastSCSI == NO_SCSI) {      /* unexpected interrupt :-( */
704                 cmd->result |= DID_BAD_INTR << 16;
705                 printk("IBM MCA SCSI: WARNING - Interrupt from non-pending SCSI-command!\n");
706         } else                  /* things went right :-) */
707                 cmd->result |= DID_OK << 16;
708         if (cmd->scsi_done)
709                 (cmd->scsi_done) (cmd);
710         spin_unlock(dev->host_lock);
711         return IRQ_HANDLED;
712 }
713
714 static void issue_cmd(int host_index, unsigned long cmd_reg, unsigned char attn_reg)
715 {
716         unsigned long flags;
717         /* must wait for attention reg not busy */
718         while (1) {
719                 spin_lock_irqsave(hosts[host_index]->host_lock, flags);
720                 if (!(inb(IM_STAT_REG(host_index)) & IM_BUSY))
721                         break;
722                 spin_unlock_irqrestore(hosts[host_index]->host_lock, flags);
723         }
724         /* write registers and enable system interrupts */
725         outl(cmd_reg, IM_CMD_REG(host_index));
726         outb(attn_reg, IM_ATTN_REG(host_index));
727         spin_unlock_irqrestore(hosts[host_index]->host_lock, flags);
728 }
729
730 static void internal_done(Scsi_Cmnd * cmd)
731 {
732         cmd->SCp.Status++;
733         return;
734 }
735
736 /* SCSI-SCB-command for device_inquiry */
737 static int device_inquiry(int host_index, int ldn)
738 {
739         int retr;
740         struct im_scb *scb;
741         struct im_tsb *tsb;
742         unsigned char *buf;
743
744         scb = &(ld(host_index)[ldn].scb);
745         tsb = &(ld(host_index)[ldn].tsb);
746         buf = (unsigned char *) (&(ld(host_index)[ldn].buf));
747         ld(host_index)[ldn].tsb.dev_status = 0; /* prepare statusblock */
748         for (retr = 0; retr < 3; retr++) {
749                 /* fill scb with inquiry command */
750                 scb->command = IM_DEVICE_INQUIRY_CMD | IM_NO_DISCONNECT;
751                 scb->enable = IM_REPORT_TSB_ONLY_ON_ERROR | IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT | IM_RETRY_ENABLE | IM_BYPASS_BUFFER;
752                 last_scsi_command(host_index)[ldn] = IM_DEVICE_INQUIRY_CMD;
753                 last_scsi_type(host_index)[ldn] = IM_SCB;
754                 scb->sys_buf_adr = isa_virt_to_bus(buf);
755                 scb->sys_buf_length = 255;      /* maximum bufferlength gives max info */
756                 scb->tsb_adr = isa_virt_to_bus(tsb);
757                 /* issue scb to passed ldn, and busy wait for interrupt */
758                 got_interrupt(host_index) = 0;
759                 issue_cmd(host_index, isa_virt_to_bus(scb), IM_SCB | ldn);
760                 while (!got_interrupt(host_index))
761                         barrier();
762
763                 /*if command successful, break */
764                 if ((stat_result(host_index) == IM_SCB_CMD_COMPLETED) || (stat_result(host_index) == IM_SCB_CMD_COMPLETED_WITH_RETRIES))
765                         return 1;
766         }
767         /*if all three retries failed, return "no device at this ldn" */
768         if (retr >= 3)
769                 return 0;
770         else
771                 return 1;
772 }
773
774 static int read_capacity(int host_index, int ldn)
775 {
776         int retr;
777         struct im_scb *scb;
778         struct im_tsb *tsb;
779         unsigned char *buf;
780
781         scb = &(ld(host_index)[ldn].scb);
782         tsb = &(ld(host_index)[ldn].tsb);
783         buf = (unsigned char *) (&(ld(host_index)[ldn].buf));
784         ld(host_index)[ldn].tsb.dev_status = 0;
785         for (retr = 0; retr < 3; retr++) {
786                 /*fill scb with read capacity command */
787                 scb->command = IM_READ_CAPACITY_CMD;
788                 scb->enable = IM_REPORT_TSB_ONLY_ON_ERROR | IM_READ_CONTROL | IM_RETRY_ENABLE | IM_BYPASS_BUFFER;
789                 last_scsi_command(host_index)[ldn] = IM_READ_CAPACITY_CMD;
790                 last_scsi_type(host_index)[ldn] = IM_SCB;
791                 scb->sys_buf_adr = isa_virt_to_bus(buf);
792                 scb->sys_buf_length = 8;
793                 scb->tsb_adr = isa_virt_to_bus(tsb);
794                 /*issue scb to passed ldn, and busy wait for interrupt */
795                 got_interrupt(host_index) = 0;
796                 issue_cmd(host_index, isa_virt_to_bus(scb), IM_SCB | ldn);
797                 while (!got_interrupt(host_index))
798                         barrier();
799
800                 /*if got capacity, get block length and return one device found */
801                 if ((stat_result(host_index) == IM_SCB_CMD_COMPLETED) || (stat_result(host_index) == IM_SCB_CMD_COMPLETED_WITH_RETRIES))
802                         return 1;
803         }
804         /*if all three retries failed, return "no device at this ldn" */
805         if (retr >= 3)
806                 return 0;
807         else
808                 return 1;
809 }
810
811 static int get_pos_info(int host_index)
812 {
813         int retr;
814         struct im_scb *scb;
815         struct im_tsb *tsb;
816         unsigned char *buf;
817
818         scb = &(ld(host_index)[MAX_LOG_DEV].scb);
819         tsb = &(ld(host_index)[MAX_LOG_DEV].tsb);
820         buf = (unsigned char *) (&(ld(host_index)[MAX_LOG_DEV].buf));
821         ld(host_index)[MAX_LOG_DEV].tsb.dev_status = 0;
822         for (retr = 0; retr < 3; retr++) {
823                 /*fill scb with get_pos_info command */
824                 scb->command = IM_GET_POS_INFO_CMD;
825                 scb->enable = IM_READ_CONTROL | IM_REPORT_TSB_ONLY_ON_ERROR | IM_RETRY_ENABLE | IM_BYPASS_BUFFER;
826                 last_scsi_command(host_index)[MAX_LOG_DEV] = IM_GET_POS_INFO_CMD;
827                 last_scsi_type(host_index)[MAX_LOG_DEV] = IM_SCB;
828                 scb->sys_buf_adr = isa_virt_to_bus(buf);
829                 if (special(host_index) == IBM_SCSI2_FW)
830                         scb->sys_buf_length = 256;      /* get all info from F/W adapter */
831                 else
832                         scb->sys_buf_length = 18;       /* get exactly 18 bytes for other SCSI */
833                 scb->tsb_adr = isa_virt_to_bus(tsb);
834                 /*issue scb to ldn=15, and busy wait for interrupt */
835                 got_interrupt(host_index) = 0;
836                 issue_cmd(host_index, isa_virt_to_bus(scb), IM_SCB | MAX_LOG_DEV);
837                 
838                 /* FIXME: timeout */
839                 while (!got_interrupt(host_index))
840                         barrier();
841
842                 /*if got POS-stuff, get block length and return one device found */
843                 if ((stat_result(host_index) == IM_SCB_CMD_COMPLETED) || (stat_result(host_index) == IM_SCB_CMD_COMPLETED_WITH_RETRIES))
844                         return 1;
845         }
846         /* if all three retries failed, return "no device at this ldn" */
847         if (retr >= 3)
848                 return 0;
849         else
850                 return 1;
851 }
852
853 /* SCSI-immediate-command for assign. This functions maps/unmaps specific
854  ldn-numbers on SCSI (PUN,LUN). It is needed for presetting of the
855  subsystem and for dynamical remapping od ldns. */
856 static int immediate_assign(int host_index, unsigned int pun, unsigned int lun, unsigned int ldn, unsigned int operation)
857 {
858         int retr;
859         unsigned long imm_cmd;
860
861         for (retr = 0; retr < 3; retr++) {
862                 /* select mutation level of the SCSI-adapter */
863                 switch (special(host_index)) {
864                 case IBM_SCSI2_FW:
865                         imm_cmd = (unsigned long) (IM_ASSIGN_IMM_CMD);
866                         imm_cmd |= (unsigned long) ((lun & 7) << 24);
867                         imm_cmd |= (unsigned long) ((operation & 1) << 23);
868                         imm_cmd |= (unsigned long) ((pun & 7) << 20) | ((pun & 8) << 24);
869                         imm_cmd |= (unsigned long) ((ldn & 15) << 16);
870                         break;
871                 default:
872                         imm_cmd = inl(IM_CMD_REG(host_index));
873                         imm_cmd &= (unsigned long) (0xF8000000);        /* keep reserved bits */
874                         imm_cmd |= (unsigned long) (IM_ASSIGN_IMM_CMD);
875                         imm_cmd |= (unsigned long) ((lun & 7) << 24);
876                         imm_cmd |= (unsigned long) ((operation & 1) << 23);
877                         imm_cmd |= (unsigned long) ((pun & 7) << 20);
878                         imm_cmd |= (unsigned long) ((ldn & 15) << 16);
879                         break;
880                 }
881                 last_scsi_command(host_index)[MAX_LOG_DEV] = IM_ASSIGN_IMM_CMD;
882                 last_scsi_type(host_index)[MAX_LOG_DEV] = IM_IMM_CMD;
883                 got_interrupt(host_index) = 0;
884                 issue_cmd(host_index, (unsigned long) (imm_cmd), IM_IMM_CMD | MAX_LOG_DEV);
885                 while (!got_interrupt(host_index))
886                         barrier();
887
888                 /*if command successful, break */
889                 if (stat_result(host_index) == IM_IMMEDIATE_CMD_COMPLETED)
890                         return 1;
891         }
892         if (retr >= 3)
893                 return 0;
894         else
895                 return 1;
896 }
897
898 static int immediate_feature(int host_index, unsigned int speed, unsigned int timeout)
899 {
900         int retr;
901         unsigned long imm_cmd;
902
903         for (retr = 0; retr < 3; retr++) {
904                 /* select mutation level of the SCSI-adapter */
905                 imm_cmd = IM_FEATURE_CTR_IMM_CMD;
906                 imm_cmd |= (unsigned long) ((speed & 0x7) << 29);
907                 imm_cmd |= (unsigned long) ((timeout & 0x1fff) << 16);
908                 last_scsi_command(host_index)[MAX_LOG_DEV] = IM_FEATURE_CTR_IMM_CMD;
909                 last_scsi_type(host_index)[MAX_LOG_DEV] = IM_IMM_CMD;
910                 got_interrupt(host_index) = 0;
911                 /* we need to run into command errors in order to probe for the
912                  * right speed! */
913                 global_command_error_excuse = 1;
914                 issue_cmd(host_index, (unsigned long) (imm_cmd), IM_IMM_CMD | MAX_LOG_DEV);
915                 
916                 /* FIXME: timeout */
917                 while (!got_interrupt(host_index))
918                         barrier();
919                 if (global_command_error_excuse == CMD_FAIL) {
920                         global_command_error_excuse = 0;
921                         return 2;
922                 } else
923                         global_command_error_excuse = 0;
924                 /*if command successful, break */
925                 if (stat_result(host_index) == IM_IMMEDIATE_CMD_COMPLETED)
926                         return 1;
927         }
928         if (retr >= 3)
929                 return 0;
930         else
931                 return 1;
932 }
933
934 #ifdef CONFIG_IBMMCA_SCSI_DEV_RESET
935 static int immediate_reset(int host_index, unsigned int ldn)
936 {
937         int retries;
938         int ticks;
939         unsigned long imm_command;
940
941         for (retries = 0; retries < 3; retries++) {
942                 imm_command = inl(IM_CMD_REG(host_index));
943                 imm_command &= (unsigned long) (0xFFFF0000);    /* keep reserved bits */
944                 imm_command |= (unsigned long) (IM_RESET_IMM_CMD);
945                 last_scsi_command(host_index)[ldn] = IM_RESET_IMM_CMD;
946                 last_scsi_type(host_index)[ldn] = IM_IMM_CMD;
947                 got_interrupt(host_index) = 0;
948                 reset_status(host_index) = IM_RESET_IN_PROGRESS;
949                 issue_cmd(host_index, (unsigned long) (imm_command), IM_IMM_CMD | ldn);
950                 ticks = IM_RESET_DELAY * HZ;
951                 while (reset_status(host_index) == IM_RESET_IN_PROGRESS && --ticks) {
952                         udelay((1 + 999 / HZ) * 1000);
953                         barrier();
954                 }
955                 /* if reset did not complete, just complain */
956                 if (!ticks) {
957                         printk(KERN_ERR "IBM MCA SCSI: reset did not complete within %d seconds.\n", IM_RESET_DELAY);
958                         reset_status(host_index) = IM_RESET_FINISHED_OK;
959                         /* did not work, finish */
960                         return 1;
961                 }
962                 /*if command successful, break */
963                 if (stat_result(host_index) == IM_IMMEDIATE_CMD_COMPLETED)
964                         return 1;
965         }
966         if (retries >= 3)
967                 return 0;
968         else
969                 return 1;
970 }
971 #endif
972
973 /* type-interpreter for physical device numbers */
974 static char *ti_p(int dev)
975 {
976         switch (dev) {
977         case TYPE_IBM_SCSI_ADAPTER:
978                 return ("A");
979         case TYPE_DISK:
980                 return ("D");
981         case TYPE_TAPE:
982                 return ("T");
983         case TYPE_PROCESSOR:
984                 return ("P");
985         case TYPE_WORM:
986                 return ("W");
987         case TYPE_ROM:
988                 return ("R");
989         case TYPE_SCANNER:
990                 return ("S");
991         case TYPE_MOD:
992                 return ("M");
993         case TYPE_MEDIUM_CHANGER:
994                 return ("C");
995         case TYPE_NO_LUN:
996                 return ("+");   /* show NO_LUN */
997         }
998         return ("-");           /* TYPE_NO_DEVICE and others */
999 }
1000
1001 /* interpreter for logical device numbers (ldn) */
1002 static char *ti_l(int val)
1003 {
1004         const char hex[16] = "0123456789abcdef";
1005         static char answer[2];
1006
1007         answer[1] = (char) (0x0);
1008         if (val <= MAX_LOG_DEV)
1009                 answer[0] = hex[val];
1010         else
1011                 answer[0] = '-';
1012         return (char *) &answer;
1013 }
1014
1015 /* transfers bitpattern of the feature command to values in MHz */
1016 static char *ibmrate(unsigned int speed, int i)
1017 {
1018         switch (speed) {
1019         case 0:
1020                 return i ? "5.00" : "10.00";
1021         case 1:
1022                 return i ? "4.00" : "8.00";
1023         case 2:
1024                 return i ? "3.33" : "6.66";
1025         case 3:
1026                 return i ? "2.86" : "5.00";
1027         case 4:
1028                 return i ? "2.50" : "4.00";
1029         case 5:
1030                 return i ? "2.22" : "3.10";
1031         case 6:
1032                 return i ? "2.00" : "2.50";
1033         case 7:
1034                 return i ? "1.82" : "2.00";
1035         }
1036         return "---";
1037 }
1038
1039 static int probe_display(int what)
1040 {
1041         static int rotator = 0;
1042         const char rotor[] = "|/-\\";
1043
1044         if (!(display_mode & LED_DISP))
1045                 return 0;
1046         if (!what) {
1047                 outl(0x20202020, MOD95_LED_PORT);
1048                 outl(0x20202020, MOD95_LED_PORT + 4);
1049         } else {
1050                 outb('S', MOD95_LED_PORT + 7);
1051                 outb('C', MOD95_LED_PORT + 6);
1052                 outb('S', MOD95_LED_PORT + 5);
1053                 outb('I', MOD95_LED_PORT + 4);
1054                 outb('i', MOD95_LED_PORT + 3);
1055                 outb('n', MOD95_LED_PORT + 2);
1056                 outb('i', MOD95_LED_PORT + 1);
1057                 outb((char) (rotor[rotator]), MOD95_LED_PORT);
1058                 rotator++;
1059                 if (rotator > 3)
1060                         rotator = 0;
1061         }
1062         return 0;
1063 }
1064
1065 static int probe_bus_mode(int host_index)
1066 {
1067         struct im_pos_info *info;
1068         int num_bus = 0;
1069         int ldn;
1070
1071         info = (struct im_pos_info *) (&(ld(host_index)[MAX_LOG_DEV].buf));
1072         if (get_pos_info(host_index)) {
1073                 if (info->connector_size & 0xf000)
1074                         subsystem_connector_size(host_index) = 16;
1075                 else
1076                         subsystem_connector_size(host_index) = 32;
1077                 num_bus |= (info->pos_4b & 8) >> 3;
1078                 for (ldn = 0; ldn <= MAX_LOG_DEV; ldn++) {
1079                         if ((special(host_index) == IBM_SCSI_WCACHE) || (special(host_index) == IBM_7568_WCACHE)) {
1080                                 if (!((info->cache_stat >> ldn) & 1))
1081                                         ld(host_index)[ldn].cache_flag = 0;
1082                         }
1083                         if (!((info->retry_stat >> ldn) & 1))
1084                                 ld(host_index)[ldn].retry_flag = 0;
1085                 }
1086 #ifdef IM_DEBUG_PROBE
1087                 printk("IBM MCA SCSI: SCSI-Cache bits: ");
1088                 for (ldn = 0; ldn <= MAX_LOG_DEV; ldn++) {
1089                         printk("%d", ld(host_index)[ldn].cache_flag);
1090                 }
1091                 printk("\nIBM MCA SCSI: SCSI-Retry bits: ");
1092                 for (ldn = 0; ldn <= MAX_LOG_DEV; ldn++) {
1093                         printk("%d", ld(host_index)[ldn].retry_flag);
1094                 }
1095                 printk("\n");
1096 #endif
1097         }
1098         return num_bus;
1099 }
1100
1101 /* probing scsi devices */
1102 static void check_devices(int host_index, int adaptertype)
1103 {
1104         int id, lun, ldn, ticks;
1105         int count_devices;      /* local counter for connected device */
1106         int max_pun;
1107         int num_bus;
1108         int speedrun;           /* local adapter_speed check variable */
1109
1110         /* assign default values to certain variables */
1111         ticks = 0;
1112         count_devices = 0;
1113         IBM_DS(host_index).dyn_flag = 0;        /* normally no need for dynamical ldn management */
1114         IBM_DS(host_index).total_errors = 0;    /* set errorcounter to 0 */
1115         next_ldn(host_index) = 7;       /* next ldn to be assigned is 7, because 0-6 is 'hardwired' */
1116
1117         /* initialize the very important driver-informational arrays/structs */
1118         memset(ld(host_index), 0, sizeof(ld(host_index)));
1119         for (ldn = 0; ldn <= MAX_LOG_DEV; ldn++) {
1120                 last_scsi_command(host_index)[ldn] = NO_SCSI;   /* emptify last SCSI-command storage */
1121                 last_scsi_type(host_index)[ldn] = 0;
1122                 ld(host_index)[ldn].cache_flag = 1;
1123                 ld(host_index)[ldn].retry_flag = 1;
1124         }
1125         memset(get_ldn(host_index), TYPE_NO_DEVICE, sizeof(get_ldn(host_index)));       /* this is essential ! */
1126         memset(get_scsi(host_index), TYPE_NO_DEVICE, sizeof(get_scsi(host_index)));     /* this is essential ! */
1127         for (lun = 0; lun < 8; lun++) {
1128                 /* mark the adapter at its pun on all luns */
1129                 get_scsi(host_index)[subsystem_pun(host_index)][lun] = TYPE_IBM_SCSI_ADAPTER;
1130                 get_ldn(host_index)[subsystem_pun(host_index)][lun] = MAX_LOG_DEV;      /* make sure, the subsystem
1131                                                                                            ldn is active for all
1132                                                                                            luns. */
1133         }
1134         probe_display(0);       /* Supercool display usage during SCSI-probing. */
1135         /* This makes sense, when booting without any */
1136         /* monitor connected on model XX95. */
1137
1138         /* STEP 1: */
1139         adapter_speed(host_index) = global_adapter_speed;
1140         speedrun = adapter_speed(host_index);
1141         while (immediate_feature(host_index, speedrun, adapter_timeout) == 2) {
1142                 probe_display(1);
1143                 if (speedrun == 7)
1144                         panic("IBM MCA SCSI: Cannot set Synchronous-Transfer-Rate!\n");
1145                 speedrun++;
1146                 if (speedrun > 7)
1147                         speedrun = 7;
1148         }
1149         adapter_speed(host_index) = speedrun;
1150         /* Get detailed information about the current adapter, necessary for
1151          * device operations: */
1152         num_bus = probe_bus_mode(host_index);
1153
1154         /* num_bus contains only valid data for the F/W adapter! */
1155         if (adaptertype == IBM_SCSI2_FW) {      /* F/W SCSI adapter: */
1156                 /* F/W adapter PUN-space extension evaluation: */
1157                 if (num_bus) {
1158                         printk(KERN_INFO "IBM MCA SCSI: Separate bus mode (wide-addressing enabled)\n");
1159                         subsystem_maxid(host_index) = 16;
1160                 } else {
1161                         printk(KERN_INFO "IBM MCA SCSI: Combined bus mode (wide-addressing disabled)\n");
1162                         subsystem_maxid(host_index) = 8;
1163                 }
1164                 printk(KERN_INFO "IBM MCA SCSI: Sync.-Rate (F/W: 20, Int.: 10, Ext.: %s) MBytes/s\n", ibmrate(speedrun, adaptertype));
1165         } else                  /* all other IBM SCSI adapters: */
1166                 printk(KERN_INFO "IBM MCA SCSI: Synchronous-SCSI-Transfer-Rate: %s MBytes/s\n", ibmrate(speedrun, adaptertype));
1167
1168         /* assign correct PUN device space */
1169         max_pun = subsystem_maxid(host_index);
1170
1171 #ifdef IM_DEBUG_PROBE
1172         printk("IBM MCA SCSI: Current SCSI-host index: %d\n", host_index);
1173         printk("IBM MCA SCSI: Removing default logical SCSI-device mapping.");
1174 #else
1175         printk(KERN_INFO "IBM MCA SCSI: Dev. Order: %s, Mapping (takes <2min): ", (ibm_ansi_order) ? "ANSI" : "New");
1176 #endif
1177         for (ldn = 0; ldn < MAX_LOG_DEV; ldn++) {
1178                 probe_display(1);
1179 #ifdef IM_DEBUG_PROBE
1180                 printk(".");
1181 #endif
1182                 immediate_assign(host_index, 0, 0, ldn, REMOVE_LDN);    /* remove ldn (wherever) */
1183         }
1184         lun = 0;                /* default lun is 0 */
1185 #ifndef IM_DEBUG_PROBE
1186         printk("cleared,");
1187 #endif
1188         /* STEP 2: */
1189 #ifdef IM_DEBUG_PROBE
1190         printk("\nIBM MCA SCSI: Scanning SCSI-devices.");
1191 #endif
1192         for (id = 0; id < max_pun; id++)
1193 #ifdef CONFIG_SCSI_MULTI_LUN
1194                 for (lun = 0; lun < 8; lun++)
1195 #endif
1196                 {
1197                         probe_display(1);
1198 #ifdef IM_DEBUG_PROBE
1199                         printk(".");
1200 #endif
1201                         if (id != subsystem_pun(host_index)) {
1202                                 /* if pun is not the adapter: */
1203                                 /* set ldn=0 to pun,lun */
1204                                 immediate_assign(host_index, id, lun, PROBE_LDN, SET_LDN);
1205                                 if (device_inquiry(host_index, PROBE_LDN)) {    /* probe device */
1206                                         get_scsi(host_index)[id][lun] = (unsigned char) (ld(host_index)[PROBE_LDN].buf[0]);
1207                                         /* entry, even for NO_LUN */
1208                                         if (ld(host_index)[PROBE_LDN].buf[0] != TYPE_NO_LUN)
1209                                                 count_devices++;        /* a existing device is found */
1210                                 }
1211                                 /* remove ldn */
1212                                 immediate_assign(host_index, id, lun, PROBE_LDN, REMOVE_LDN);
1213                         }
1214                 }
1215 #ifndef IM_DEBUG_PROBE
1216         printk("scanned,");
1217 #endif
1218         /* STEP 3: */
1219 #ifdef IM_DEBUG_PROBE
1220         printk("\nIBM MCA SCSI: Mapping SCSI-devices.");
1221 #endif
1222         ldn = 0;
1223         lun = 0;
1224 #ifdef CONFIG_SCSI_MULTI_LUN
1225         for (lun = 0; lun < 8 && ldn < MAX_LOG_DEV; lun++)
1226 #endif
1227                 for (id = 0; id < max_pun && ldn < MAX_LOG_DEV; id++) {
1228                         probe_display(1);
1229 #ifdef IM_DEBUG_PROBE
1230                         printk(".");
1231 #endif
1232                         if (id != subsystem_pun(host_index)) {
1233                                 if (get_scsi(host_index)[id][lun] != TYPE_NO_LUN && get_scsi(host_index)[id][lun] != TYPE_NO_DEVICE) {
1234                                         /* Only map if accepted type. Always enter for
1235                                            lun == 0 to get no gaps into ldn-mapping for ldn<7. */
1236                                         immediate_assign(host_index, id, lun, ldn, SET_LDN);
1237                                         get_ldn(host_index)[id][lun] = ldn;     /* map ldn */
1238                                         if (device_exists(host_index, ldn, &ld(host_index)[ldn].block_length, &ld(host_index)[ldn].device_type)) {
1239 #ifdef CONFIG_IBMMCA_SCSI_DEV_RESET
1240                                                 printk("resetting device at ldn=%x ... ", ldn);
1241                                                 immediate_reset(host_index, ldn);
1242 #endif
1243                                                 ldn++;
1244                                         } else {
1245                                                 /* device vanished, probably because we don't know how to
1246                                                  * handle it or because it has problems */
1247                                                 if (lun > 0) {
1248                                                         /* remove mapping */
1249                                                         get_ldn(host_index)[id][lun] = TYPE_NO_DEVICE;
1250                                                         immediate_assign(host_index, 0, 0, ldn, REMOVE_LDN);
1251                                                 } else
1252                                                         ldn++;
1253                                         }
1254                                 } else if (lun == 0) {
1255                                         /* map lun == 0, even if no device exists */
1256                                         immediate_assign(host_index, id, lun, ldn, SET_LDN);
1257                                         get_ldn(host_index)[id][lun] = ldn;     /* map ldn */
1258                                         ldn++;
1259                                 }
1260                         }
1261                 }
1262         /* STEP 4: */
1263
1264         /* map remaining ldns to non-existing devices */
1265         for (lun = 1; lun < 8 && ldn < MAX_LOG_DEV; lun++)
1266                 for (id = 0; id < max_pun && ldn < MAX_LOG_DEV; id++) {
1267                         if (get_scsi(host_index)[id][lun] == TYPE_NO_LUN || get_scsi(host_index)[id][lun] == TYPE_NO_DEVICE) {
1268                                 probe_display(1);
1269                                 /* Map remaining ldns only to NON-existing pun,lun
1270                                    combinations to make sure an inquiry will fail.
1271                                    For MULTI_LUN, it is needed to avoid adapter autonome
1272                                    SCSI-remapping. */
1273                                 immediate_assign(host_index, id, lun, ldn, SET_LDN);
1274                                 get_ldn(host_index)[id][lun] = ldn;
1275                                 ldn++;
1276                         }
1277                 }
1278 #ifndef IM_DEBUG_PROBE
1279         printk("mapped.");
1280 #endif
1281         printk("\n");
1282 #ifdef IM_DEBUG_PROBE
1283         if (ibm_ansi_order)
1284                 printk("IBM MCA SCSI: Device order: IBM/ANSI (pun=7 is first).\n");
1285         else
1286                 printk("IBM MCA SCSI: Device order: New Industry Standard (pun=0 is first).\n");
1287 #endif
1288
1289 #ifdef IM_DEBUG_PROBE
1290         /* Show the physical and logical mapping during boot. */
1291         printk("IBM MCA SCSI: Determined SCSI-device-mapping:\n");
1292         printk("    Physical SCSI-Device Map               Logical SCSI-Device Map\n");
1293         printk("ID\\LUN  0  1  2  3  4  5  6  7       ID\\LUN  0  1  2  3  4  5  6  7\n");
1294         for (id = 0; id < max_pun; id++) {
1295                 printk("%2d     ", id);
1296                 for (lun = 0; lun < 8; lun++)
1297                         printk("%2s ", ti_p(get_scsi(host_index)[id][lun]));
1298                 printk("      %2d     ", id);
1299                 for (lun = 0; lun < 8; lun++)
1300                         printk("%2s ", ti_l(get_ldn(host_index)[id][lun]));
1301                 printk("\n");
1302         }
1303 #endif
1304
1305         /* assign total number of found SCSI-devices to the statistics struct */
1306         IBM_DS(host_index).total_scsi_devices = count_devices;
1307
1308         /* decide for output in /proc-filesystem, if the configuration of
1309            SCSI-devices makes dynamical reassignment of devices necessary */
1310         if (count_devices >= MAX_LOG_DEV)
1311                 IBM_DS(host_index).dyn_flag = 1;        /* dynamical assignment is necessary */
1312         else
1313                 IBM_DS(host_index).dyn_flag = 0;        /* dynamical assignment is not necessary */
1314
1315         /* If no SCSI-devices are assigned, return 1 in order to cause message. */
1316         if (ldn == 0)
1317                 printk("IBM MCA SCSI: Warning: No SCSI-devices found/assigned!\n");
1318
1319         /* reset the counters for statistics on the current adapter */
1320         IBM_DS(host_index).scbs = 0;
1321         IBM_DS(host_index).long_scbs = 0;
1322         IBM_DS(host_index).total_accesses = 0;
1323         IBM_DS(host_index).total_interrupts = 0;
1324         IBM_DS(host_index).dynamical_assignments = 0;
1325         memset(IBM_DS(host_index).ldn_access, 0x0, sizeof(IBM_DS(host_index).ldn_access));
1326         memset(IBM_DS(host_index).ldn_read_access, 0x0, sizeof(IBM_DS(host_index).ldn_read_access));
1327         memset(IBM_DS(host_index).ldn_write_access, 0x0, sizeof(IBM_DS(host_index).ldn_write_access));
1328         memset(IBM_DS(host_index).ldn_inquiry_access, 0x0, sizeof(IBM_DS(host_index).ldn_inquiry_access));
1329         memset(IBM_DS(host_index).ldn_modeselect_access, 0x0, sizeof(IBM_DS(host_index).ldn_modeselect_access));
1330         memset(IBM_DS(host_index).ldn_assignments, 0x0, sizeof(IBM_DS(host_index).ldn_assignments));
1331         probe_display(0);
1332         return;
1333 }
1334
1335 static int device_exists(int host_index, int ldn, int *block_length, int *device_type)
1336 {
1337         unsigned char *buf;
1338         /* if no valid device found, return immediately with 0 */
1339         if (!(device_inquiry(host_index, ldn)))
1340                 return 0;
1341         buf = (unsigned char *) (&(ld(host_index)[ldn].buf));
1342         if (*buf == TYPE_ROM) {
1343                 *device_type = TYPE_ROM;
1344                 *block_length = 2048;   /* (standard blocksize for yellow-/red-book) */
1345                 return 1;
1346         }
1347         if (*buf == TYPE_WORM) {
1348                 *device_type = TYPE_WORM;
1349                 *block_length = 2048;
1350                 return 1;
1351         }
1352         if (*buf == TYPE_DISK) {
1353                 *device_type = TYPE_DISK;
1354                 if (read_capacity(host_index, ldn)) {
1355                         *block_length = *(buf + 7) + (*(buf + 6) << 8) + (*(buf + 5) << 16) + (*(buf + 4) << 24);
1356                         return 1;
1357                 } else
1358                         return 0;
1359         }
1360         if (*buf == TYPE_MOD) {
1361                 *device_type = TYPE_MOD;
1362                 if (read_capacity(host_index, ldn)) {
1363                         *block_length = *(buf + 7) + (*(buf + 6) << 8) + (*(buf + 5) << 16) + (*(buf + 4) << 24);
1364                         return 1;
1365                 } else
1366                         return 0;
1367         }
1368         if (*buf == TYPE_TAPE) {
1369                 *device_type = TYPE_TAPE;
1370                 *block_length = 0;      /* not in use (setting by mt and mtst in op.) */
1371                 return 1;
1372         }
1373         if (*buf == TYPE_PROCESSOR) {
1374                 *device_type = TYPE_PROCESSOR;
1375                 *block_length = 0;      /* they set their stuff on drivers */
1376                 return 1;
1377         }
1378         if (*buf == TYPE_SCANNER) {
1379                 *device_type = TYPE_SCANNER;
1380                 *block_length = 0;      /* they set their stuff on drivers */
1381                 return 1;
1382         }
1383         if (*buf == TYPE_MEDIUM_CHANGER) {
1384                 *device_type = TYPE_MEDIUM_CHANGER;
1385                 *block_length = 0;      /* One never knows, what to expect on a medium
1386                                            changer device. */
1387                 return 1;
1388         }
1389         return 0;
1390 }
1391
1392 static void internal_ibmmca_scsi_setup(char *str, int *ints)
1393 {
1394         int i, j, io_base, id_base;
1395         char *token;
1396
1397         io_base = 0;
1398         id_base = 0;
1399         if (str) {
1400                 j = 0;
1401                 while ((token = strsep(&str, ",")) != NULL) {
1402                         if (!strcmp(token, "activity"))
1403                                 display_mode |= LED_ACTIVITY;
1404                         if (!strcmp(token, "display"))
1405                                 display_mode |= LED_DISP;
1406                         if (!strcmp(token, "adisplay"))
1407                                 display_mode |= LED_ADISP;
1408                         if (!strcmp(token, "normal"))
1409                                 ibm_ansi_order = 0;
1410                         if (!strcmp(token, "ansi"))
1411                                 ibm_ansi_order = 1;
1412                         if (!strcmp(token, "fast"))
1413                                 global_adapter_speed = 0;
1414                         if (!strcmp(token, "medium"))
1415                                 global_adapter_speed = 4;
1416                         if (!strcmp(token, "slow"))
1417                                 global_adapter_speed = 7;
1418                         if ((*token == '-') || (isdigit(*token))) {
1419                                 if (!(j % 2) && (io_base < IM_MAX_HOSTS))
1420                                         io_port[io_base++] = simple_strtoul(token, NULL, 0);
1421                                 if ((j % 2) && (id_base < IM_MAX_HOSTS))
1422                                         scsi_id[id_base++] = simple_strtoul(token, NULL, 0);
1423                                 j++;
1424                         }
1425                 }
1426         } else if (ints) {
1427                 for (i = 0; i < IM_MAX_HOSTS && 2 * i + 2 < ints[0]; i++) {
1428                         io_port[i] = ints[2 * i + 2];
1429                         scsi_id[i] = ints[2 * i + 2];
1430                 }
1431         }
1432         return;
1433 }
1434
1435 static int ibmmca_getinfo(char *buf, int slot, void *dev_id)
1436 {
1437         struct Scsi_Host *shpnt;
1438         int len, speciale, connectore, k;
1439         unsigned int pos[8];
1440         unsigned long flags;
1441         struct Scsi_Host *dev = dev_id;
1442
1443         spin_lock_irqsave(dev->host_lock, flags);
1444
1445         shpnt = dev;            /* assign host-structure to local pointer */
1446         len = 0;                /* set filled text-buffer index to 0 */
1447         /* get the _special contents of the hostdata structure */
1448         speciale = ((struct ibmmca_hostdata *) shpnt->hostdata)->_special;
1449         connectore = ((struct ibmmca_hostdata *) shpnt->hostdata)->_connector_size;
1450         for (k = 2; k < 4; k++)
1451                 pos[k] = ((struct ibmmca_hostdata *) shpnt->hostdata)->_pos[k];
1452         if (speciale == FORCED_DETECTION) {     /* forced detection */
1453                 len += sprintf(buf + len,
1454                                "Adapter category: forced detected\n" "***************************************\n" "***  Forced detected SCSI Adapter   ***\n" "***  No chip-information available  ***\n" "***************************************\n");
1455         } else if (speciale == INTEGRATED_SCSI) {
1456                 /* if the integrated subsystem has been found automatically: */
1457                 len += sprintf(buf + len,
1458                                "Adapter category: integrated\n" "Chip revision level: %d\n" "Chip status: %s\n" "8 kByte NVRAM status: %s\n", ((pos[2] & 0xf0) >> 4), (pos[2] & 1) ? "enabled" : "disabled", (pos[2] & 2) ? "locked" : "accessible");
1459         } else if ((speciale >= 0) && (speciale < ARRAY_SIZE(subsys_list))) {
1460                 /* if the subsystem is a slot adapter */
1461                 len += sprintf(buf + len, "Adapter category: slot-card\n" "ROM Segment Address: ");
1462                 if ((pos[2] & 0xf0) == 0xf0)
1463                         len += sprintf(buf + len, "off\n");
1464                 else
1465                         len += sprintf(buf + len, "0x%x\n", ((pos[2] & 0xf0) << 13) + 0xc0000);
1466                 len += sprintf(buf + len, "Chip status: %s\n", (pos[2] & 1) ? "enabled" : "disabled");
1467                 len += sprintf(buf + len, "Adapter I/O Offset: 0x%x\n", ((pos[2] & 0x0e) << 2));
1468         } else {
1469                 len += sprintf(buf + len, "Adapter category: unknown\n");
1470         }
1471         /* common subsystem information to write to the slotn file */
1472         len += sprintf(buf + len, "Subsystem PUN: %d\n", shpnt->this_id);
1473         len += sprintf(buf + len, "I/O base address range: 0x%x-0x%x\n", (unsigned int) (shpnt->io_port), (unsigned int) (shpnt->io_port + 7));
1474         len += sprintf(buf + len, "MCA-slot size: %d bits", connectore);
1475         /* Now make sure, the bufferlength is devidable by 4 to avoid
1476          * paging problems of the buffer. */
1477         while (len % sizeof(int) != (sizeof(int) - 1))
1478                 len += sprintf(buf + len, " ");
1479         len += sprintf(buf + len, "\n");
1480
1481         spin_unlock_irqrestore(shpnt->host_lock, flags);
1482
1483         return len;
1484 }
1485
1486 int ibmmca_detect(struct scsi_host_template * scsi_template)
1487 {
1488         struct Scsi_Host *shpnt;
1489         int port, id, i, j, k, slot;
1490         int devices_on_irq_11 = 0;
1491         int devices_on_irq_14 = 0;
1492         int IRQ14_registered = 0;
1493         int IRQ11_registered = 0;
1494
1495         found = 0;              /* make absolutely sure, that found is set to 0 */
1496
1497         /* First of all, print the version number of the driver. This is
1498          * important to allow better user bugreports in case of already
1499          * having problems with the MCA_bus probing. */
1500         printk(KERN_INFO "IBM MCA SCSI: Version %s\n", IBMMCA_SCSI_DRIVER_VERSION);
1501         /* if this is not MCA machine, return "nothing found" */
1502         if (!MCA_bus) {
1503                 printk(KERN_INFO "IBM MCA SCSI:  No Microchannel-bus present --> Aborting.\n" "              This machine does not have any IBM MCA-bus\n" "                 or the MCA-Kernel-support is not enabled!\n");
1504                 return 0;
1505         }
1506
1507 #ifdef MODULE
1508         /* If the driver is run as module, read from conf.modules or cmd-line */
1509         if (boot_options)
1510                 option_setup(boot_options);
1511 #endif
1512
1513         /* get interrupt request level */
1514         if (request_irq(IM_IRQ, interrupt_handler, SA_SHIRQ, "ibmmcascsi", hosts)) {
1515                 printk(KERN_ERR "IBM MCA SCSI: Unable to get shared IRQ %d.\n", IM_IRQ);
1516                 return 0;
1517         } else
1518                 IRQ14_registered++;
1519
1520         /* if ibmmcascsi setup option was passed to kernel, return "found" */
1521         for (i = 0; i < IM_MAX_HOSTS; i++)
1522                 if (io_port[i] > 0 && scsi_id[i] >= 0 && scsi_id[i] < 8) {
1523                         printk("IBM MCA SCSI: forced detected SCSI Adapter, io=0x%x, scsi id=%d.\n", io_port[i], scsi_id[i]);
1524                         if ((shpnt = ibmmca_register(scsi_template, io_port[i], scsi_id[i], FORCED_DETECTION, "forced detected SCSI Adapter"))) {
1525                                 for (k = 2; k < 7; k++)
1526                                         ((struct ibmmca_hostdata *) shpnt->hostdata)->_pos[k] = 0;
1527                                 ((struct ibmmca_hostdata *) shpnt->hostdata)->_special = FORCED_DETECTION;
1528                                 mca_set_adapter_name(MCA_INTEGSCSI, "forced detected SCSI Adapter");
1529                                 mca_set_adapter_procfn(MCA_INTEGSCSI, (MCA_ProcFn) ibmmca_getinfo, shpnt);
1530                                 mca_mark_as_used(MCA_INTEGSCSI);
1531                                 devices_on_irq_14++;
1532                         }
1533                 }
1534         if (found)
1535                 return found;
1536
1537         /* The POS2-register of all PS/2 model SCSI-subsystems has the following
1538          * interpretation of bits:
1539          *                             Bit 7 - 4 : Chip Revision ID (Release)
1540          *                             Bit 3 - 2 : Reserved
1541          *                             Bit 1     : 8k NVRAM Disabled
1542          *                             Bit 0     : Chip Enable (EN-Signal)
1543          * The POS3-register is interpreted as follows:
1544          *                             Bit 7 - 5 : SCSI ID
1545          *                             Bit 4     : Reserved = 0
1546          *                             Bit 3 - 0 : Reserved = 0
1547          * (taken from "IBM, PS/2 Hardware Interface Technical Reference, Common
1548          * Interfaces (1991)").
1549          * In short words, this means, that IBM PS/2 machines only support
1550          * 1 single subsystem by default. The slot-adapters must have another
1551          * configuration on pos2. Here, one has to assume the following
1552          * things for POS2-register:
1553          *                             Bit 7 - 4 : Chip Revision ID (Release)
1554          *                             Bit 3 - 1 : port offset factor
1555          *                             Bit 0     : Chip Enable (EN-Signal)
1556          * As I found a patch here, setting the IO-registers to 0x3540 forced,
1557          * as there was a 0x05 in POS2 on a model 56, I assume, that the
1558          * port 0x3540 must be fix for integrated SCSI-controllers.
1559          * Ok, this discovery leads to the following implementation: (M.Lang) */
1560
1561         /* first look for the IBM SCSI integrated subsystem on the motherboard */
1562         for (j = 0; j < 8; j++) /* read the pos-information */
1563                 pos[j] = mca_read_stored_pos(MCA_INTEGSCSI, j);
1564         /* pos2 = pos3 = 0xff if there is no integrated SCSI-subsystem present, but
1565          * if we ignore the settings of all surrounding pos registers, it is not
1566          * completely sufficient to only check pos2 and pos3. */
1567         /* Therefore, now the following if statement is used to
1568          * make sure, we see a real integrated onboard SCSI-interface and no
1569          * internal system information, which gets mapped to some pos registers
1570          * on models 95xx. */
1571         if ((!pos[0] && !pos[1] && pos[2] > 0 && pos[3] > 0 && !pos[4] && !pos[5] && !pos[6] && !pos[7]) || (pos[0] == 0xff && pos[1] == 0xff && pos[2] < 0xff && pos[3] < 0xff && pos[4] == 0xff && pos[5] == 0xff && pos[6] == 0xff && pos[7] == 0xff)) {
1572                 if ((pos[2] & 1) == 1)  /* is the subsystem chip enabled ? */
1573                         port = IM_IO_PORT;
1574                 else {          /* if disabled, no IRQs will be generated, as the chip won't
1575                                  * listen to the incoming commands and will do really nothing,
1576                                  * except for listening to the pos-register settings. If this
1577                                  * happens, I need to hugely think about it, as one has to
1578                                  * write something to the MCA-Bus pos register in order to
1579                                  * enable the chip. Normally, IBM-SCSI won't pass the POST,
1580                                  * when the chip is disabled (see IBM tech. ref.). */
1581                         port = IM_IO_PORT;      /* anyway, set the portnumber and warn */
1582                         printk("IBM MCA SCSI: WARNING - Your SCSI-subsystem is disabled!\n" "              SCSI-operations may not work.\n");
1583                 }
1584                 id = (pos[3] & 0xe0) >> 5;      /* this is correct and represents the PUN */
1585                 /* give detailed information on the subsystem. This helps me
1586                  * additionally during debugging and analyzing bug-reports. */
1587                 printk(KERN_INFO "IBM MCA SCSI: IBM Integrated SCSI Controller ffound, io=0x%x, scsi id=%d,\n", port, id);
1588                 printk(KERN_INFO "              chip rev.=%d, 8K NVRAM=%s, subsystem=%s\n", ((pos[2] & 0xf0) >> 4), (pos[2] & 2) ? "locked" : "accessible", (pos[2] & 1) ? "enabled." : "disabled.");
1589
1590                 /* register the found integrated SCSI-subsystem */
1591                 if ((shpnt = ibmmca_register(scsi_template, port, id, INTEGRATED_SCSI, "IBM Integrated SCSI Controller"))) 
1592                 {
1593                         for (k = 2; k < 7; k++)
1594                                 ((struct ibmmca_hostdata *) shpnt->hostdata)->_pos[k] = pos[k];
1595                         ((struct ibmmca_hostdata *) shpnt->hostdata)->_special = INTEGRATED_SCSI;
1596                         mca_set_adapter_name(MCA_INTEGSCSI, "IBM Integrated SCSI Controller");
1597                         mca_set_adapter_procfn(MCA_INTEGSCSI, (MCA_ProcFn) ibmmca_getinfo, shpnt);
1598                         mca_mark_as_used(MCA_INTEGSCSI);
1599                         devices_on_irq_14++;
1600                 }
1601         }
1602
1603         /* now look for other adapters in MCA slots, */
1604         /* determine the number of known IBM-SCSI-subsystem types */
1605         /* see the pos[2] dependence to get the adapter port-offset. */
1606         for (i = 0; i < ARRAY_SIZE(subsys_list); i++) {
1607                 /* scan each slot for a fitting adapter id */
1608                 slot = 0;       /* start at slot 0 */
1609                 while ((slot = mca_find_adapter(subsys_list[i].mca_id, slot))
1610                        != MCA_NOTFOUND) {       /* scan through all slots */
1611                         for (j = 0; j < 8; j++) /* read the pos-information */
1612                                 pos[j] = mca_read_stored_pos(slot, j);
1613                         if ((pos[2] & 1) == 1)
1614                                 /* is the subsystem chip enabled ? */
1615                                 /* (explanations see above) */
1616                                 port = IM_IO_PORT + ((pos[2] & 0x0e) << 2);
1617                         else {
1618                                 /* anyway, set the portnumber and warn */
1619                                 port = IM_IO_PORT + ((pos[2] & 0x0e) << 2);
1620                                 printk(KERN_WARNING "IBM MCA SCSI: WARNING - Your SCSI-subsystem is disabled!\n");
1621                                 printk(KERN_WARNING "              SCSI-operations may not work.\n");
1622                         }
1623                         if ((i == IBM_SCSI2_FW) && (pos[6] != 0)) {
1624                                 printk(KERN_ERR "IBM MCA SCSI: ERROR - Wrong POS(6)-register setting!\n");
1625                                 printk(KERN_ERR "              Impossible to determine adapter PUN!\n");
1626                                 printk(KERN_ERR "              Guessing adapter PUN = 7.\n");
1627                                 id = 7;
1628                         } else {
1629                                 id = (pos[3] & 0xe0) >> 5;      /* get subsystem PUN */
1630                                 if (i == IBM_SCSI2_FW) {
1631                                         id |= (pos[3] & 0x10) >> 1;     /* get subsystem PUN high-bit
1632                                                                          * for F/W adapters */
1633                                 }
1634                         }
1635                         if ((i == IBM_SCSI2_FW) && (pos[4] & 0x01) && (pos[6] == 0)) {
1636                                 /* IRQ11 is used by SCSI-2 F/W Adapter/A */
1637                                 printk(KERN_DEBUG "IBM MCA SCSI: SCSI-2 F/W adapter needs IRQ 11.\n");
1638                                 /* get interrupt request level */
1639                                 if (request_irq(IM_IRQ_FW, interrupt_handler, SA_SHIRQ, "ibmmcascsi", hosts)) {
1640                                         printk(KERN_ERR "IBM MCA SCSI: Unable to get shared IRQ %d.\n", IM_IRQ_FW);
1641                                 } else
1642                                         IRQ11_registered++;
1643                         }
1644                         printk(KERN_INFO "IBM MCA SCSI: %s found in slot %d, io=0x%x, scsi id=%d,\n", subsys_list[i].description, slot + 1, port, id);
1645                         if ((pos[2] & 0xf0) == 0xf0)
1646                                 printk(KERN_DEBUG"              ROM Addr.=off,");
1647                         else
1648                                 printk(KERN_DEBUG "              ROM Addr.=0x%x,", ((pos[2] & 0xf0) << 13) + 0xc0000);
1649                         printk(KERN_DEBUG " port-offset=0x%x, subsystem=%s\n", ((pos[2] & 0x0e) << 2), (pos[2] & 1) ? "enabled." : "disabled.");
1650
1651                         /* register the hostadapter */
1652                         if ((shpnt = ibmmca_register(scsi_template, port, id, i, subsys_list[i].description))) {
1653                                 for (k = 2; k < 8; k++)
1654                                         ((struct ibmmca_hostdata *) shpnt->hostdata)->_pos[k] = pos[k];
1655                                 ((struct ibmmca_hostdata *) shpnt->hostdata)->_special = i;
1656                                 mca_set_adapter_name(slot, subsys_list[i].description);
1657                                 mca_set_adapter_procfn(slot, (MCA_ProcFn) ibmmca_getinfo, shpnt);
1658                                 mca_mark_as_used(slot);
1659                                 if ((i == IBM_SCSI2_FW) && (pos[4] & 0x01) && (pos[6] == 0))
1660                                         devices_on_irq_11++;
1661                                 else
1662                                         devices_on_irq_14++;
1663                         }
1664                         slot++; /* advance to next slot */
1665                 }               /* advance to next adapter id in the list of IBM-SCSI-subsystems */
1666         }
1667
1668         /* now check for SCSI-adapters, mapped to the integrated SCSI
1669          * area. E.g. a W/Cache in MCA-slot 9(!). Do the check correct here,
1670          * as this is a known effect on some models 95xx. */
1671         for (i = 0; i < ARRAY_SIZE(subsys_list); i++) {
1672                 /* scan each slot for a fitting adapter id */
1673                 slot = mca_find_adapter(subsys_list[i].mca_id, MCA_INTEGSCSI);
1674                 if (slot != MCA_NOTFOUND) {     /* scan through all slots */
1675                         for (j = 0; j < 8; j++) /* read the pos-information */
1676                                 pos[j] = mca_read_stored_pos(slot, j);
1677                         if ((pos[2] & 1) == 1) {        /* is the subsystem chip enabled ? */
1678                                 /* (explanations see above) */
1679                                 port = IM_IO_PORT + ((pos[2] & 0x0e) << 2);
1680                         } else {        /* anyway, set the portnumber and warn */
1681                                 port = IM_IO_PORT + ((pos[2] & 0x0e) << 2);
1682                                 printk(KERN_WARNING "IBM MCA SCSI: WARNING - Your SCSI-subsystem is disabled!\n");
1683                                 printk(KERN_WARNING "              SCSI-operations may not work.\n");
1684                         }
1685                         if ((i == IBM_SCSI2_FW) && (pos[6] != 0)) {
1686                                 printk(KERN_ERR "IBM MCA SCSI: ERROR - Wrong POS(6)-register setting!\n");
1687                                 printk(KERN_ERR  "              Impossible to determine adapter PUN!\n");
1688                                 printk(KERN_ERR "              Guessing adapter PUN = 7.\n");
1689                                 id = 7;
1690                         } else {
1691                                 id = (pos[3] & 0xe0) >> 5;      /* get subsystem PUN */
1692                                 if (i == IBM_SCSI2_FW)
1693                                         id |= (pos[3] & 0x10) >> 1;     /* get subsystem PUN high-bit
1694                                                                          * for F/W adapters */
1695                         }
1696                         if ((i == IBM_SCSI2_FW) && (pos[4] & 0x01) && (pos[6] == 0)) {
1697                                 /* IRQ11 is used by SCSI-2 F/W Adapter/A */
1698                                 printk(KERN_DEBUG  "IBM MCA SCSI: SCSI-2 F/W adapter needs IRQ 11.\n");
1699                                 /* get interrupt request level */
1700                                 if (request_irq(IM_IRQ_FW, interrupt_handler, SA_SHIRQ, "ibmmcascsi", hosts))
1701                                         printk(KERN_ERR "IBM MCA SCSI: Unable to get shared IRQ %d.\n", IM_IRQ_FW);
1702                                 else
1703                                         IRQ11_registered++;
1704                         }
1705                         printk(KERN_INFO "IBM MCA SCSI: %s found in slot %d, io=0x%x, scsi id=%d,\n", subsys_list[i].description, slot + 1, port, id);
1706                         if ((pos[2] & 0xf0) == 0xf0)
1707                                 printk(KERN_DEBUG "              ROM Addr.=off,");
1708                         else
1709                                 printk(KERN_DEBUG "              ROM Addr.=0x%x,", ((pos[2] & 0xf0) << 13) + 0xc0000);
1710                         printk(KERN_DEBUG " port-offset=0x%x, subsystem=%s\n", ((pos[2] & 0x0e) << 2), (pos[2] & 1) ? "enabled." : "disabled.");
1711
1712                         /* register the hostadapter */
1713                         if ((shpnt = ibmmca_register(scsi_template, port, id, i, subsys_list[i].description))) {
1714                                 for (k = 2; k < 7; k++)
1715                                         ((struct ibmmca_hostdata *) shpnt->hostdata)->_pos[k] = pos[k];
1716                                 ((struct ibmmca_hostdata *) shpnt->hostdata)->_special = i;
1717                                 mca_set_adapter_name(slot, subsys_list[i].description);
1718                                 mca_set_adapter_procfn(slot, (MCA_ProcFn) ibmmca_getinfo, shpnt);
1719                                 mca_mark_as_used(slot);
1720                                 if ((i == IBM_SCSI2_FW) && (pos[4] & 0x01) && (pos[6] == 0))
1721                                         devices_on_irq_11++;
1722                                 else
1723                                         devices_on_irq_14++;
1724                         }
1725                         slot++; /* advance to next slot */
1726                 }               /* advance to next adapter id in the list of IBM-SCSI-subsystems */
1727         }
1728         if (IRQ11_registered && !devices_on_irq_11)
1729                 free_irq(IM_IRQ_FW, hosts);     /* no devices on IRQ 11 */
1730         if (IRQ14_registered && !devices_on_irq_14)
1731                 free_irq(IM_IRQ, hosts);        /* no devices on IRQ 14 */
1732         if (!devices_on_irq_11 && !devices_on_irq_14)
1733                 printk(KERN_WARNING "IBM MCA SCSI: No IBM SCSI-subsystem adapter attached.\n");
1734         return found;           /* return the number of found SCSI hosts. Should be 1 or 0. */
1735 }
1736
1737 static struct Scsi_Host *ibmmca_register(struct scsi_host_template * scsi_template, int port, int id, int adaptertype, char *hostname)
1738 {
1739         struct Scsi_Host *shpnt;
1740         int i, j;
1741         unsigned int ctrl;
1742
1743         /* check I/O region */
1744         if (!request_region(port, IM_N_IO_PORT, hostname)) {
1745                 printk(KERN_ERR "IBM MCA SCSI: Unable to get I/O region 0x%x-0x%x (%d ports).\n", port, port + IM_N_IO_PORT - 1, IM_N_IO_PORT);
1746                 return NULL;
1747         }
1748
1749         /* register host */
1750         shpnt = scsi_register(scsi_template, sizeof(struct ibmmca_hostdata));
1751         if (!shpnt) {
1752                 printk(KERN_ERR "IBM MCA SCSI: Unable to register host.\n");
1753                 release_region(port, IM_N_IO_PORT);
1754                 return NULL;
1755         }
1756
1757         /* request I/O region */
1758         hosts[found] = shpnt;   /* add new found hostadapter to the list */
1759         special(found) = adaptertype;   /* important assignment or else crash! */
1760         subsystem_connector_size(found) = 0;    /* preset slot-size */
1761         shpnt->irq = IM_IRQ;    /* assign necessary stuff for the adapter */
1762         shpnt->io_port = port;
1763         shpnt->n_io_port = IM_N_IO_PORT;
1764         shpnt->this_id = id;
1765         shpnt->max_id = 8;      /* 8 PUNs are default */
1766         /* now, the SCSI-subsystem is connected to Linux */
1767
1768         ctrl = (unsigned int) (inb(IM_CTR_REG(found))); /* get control-register status */
1769 #ifdef IM_DEBUG_PROBE
1770         printk("IBM MCA SCSI: Control Register contents: %x, status: %x\n", ctrl, inb(IM_STAT_REG(found)));
1771         printk("IBM MCA SCSI: This adapters' POS-registers: ");
1772         for (i = 0; i < 8; i++)
1773                 printk("%x ", pos[i]);
1774         printk("\n");
1775 #endif
1776         reset_status(found) = IM_RESET_NOT_IN_PROGRESS;
1777
1778         for (i = 0; i < 16; i++)        /* reset the tables */
1779                 for (j = 0; j < 8; j++)
1780                         get_ldn(found)[i][j] = MAX_LOG_DEV;
1781
1782         /* check which logical devices exist */
1783         /* after this line, local interrupting is possible: */
1784         local_checking_phase_flag(found) = 1;
1785         check_devices(found, adaptertype);      /* call by value, using the global variable hosts */
1786         local_checking_phase_flag(found) = 0;
1787         found++;                /* now increase index to be prepared for next found subsystem */
1788         /* an ibm mca subsystem has been detected */
1789         return shpnt;
1790 }
1791
1792 static int ibmmca_release(struct Scsi_Host *shpnt)
1793 {
1794         release_region(shpnt->io_port, shpnt->n_io_port);
1795         if (!(--found))
1796                 free_irq(shpnt->irq, hosts);
1797         return 0;
1798 }
1799
1800 /* The following routine is the SCSI command queue for the midlevel driver */
1801 static int ibmmca_queuecommand(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
1802 {
1803         unsigned int ldn;
1804         unsigned int scsi_cmd;
1805         struct im_scb *scb;
1806         struct Scsi_Host *shpnt;
1807         int current_ldn;
1808         int id, lun;
1809         int target;
1810         int host_index;
1811         int max_pun;
1812         int i;
1813         struct scatterlist *sl;
1814
1815         shpnt = cmd->device->host;
1816         /* search for the right hostadapter */
1817         for (host_index = 0; hosts[host_index] && hosts[host_index]->host_no != shpnt->host_no; host_index++);
1818
1819         if (!hosts[host_index]) {       /* invalid hostadapter descriptor address */
1820                 cmd->result = DID_NO_CONNECT << 16;
1821                 if (done)
1822                         done(cmd);
1823                 return 0;
1824         }
1825         max_pun = subsystem_maxid(host_index);
1826         if (ibm_ansi_order) {
1827                 target = max_pun - 1 - cmd->device->id;
1828                 if ((target <= subsystem_pun(host_index)) && (cmd->device->id <= subsystem_pun(host_index)))
1829                         target--;
1830                 else if ((target >= subsystem_pun(host_index)) && (cmd->device->id >= subsystem_pun(host_index)))
1831                         target++;
1832         } else
1833                 target = cmd->device->id;
1834
1835         /* if (target,lun) is NO LUN or not existing at all, return error */
1836         if ((get_scsi(host_index)[target][cmd->device->lun] == TYPE_NO_LUN) || (get_scsi(host_index)[target][cmd->device->lun] == TYPE_NO_DEVICE)) {
1837                 cmd->result = DID_NO_CONNECT << 16;
1838                 if (done)
1839                         done(cmd);
1840                 return 0;
1841         }
1842
1843         /*if (target,lun) unassigned, do further checks... */
1844         ldn = get_ldn(host_index)[target][cmd->device->lun];
1845         if (ldn >= MAX_LOG_DEV) {       /* on invalid ldn do special stuff */
1846                 if (ldn > MAX_LOG_DEV) {        /* dynamical remapping if ldn unassigned */
1847                         current_ldn = next_ldn(host_index);     /* stop-value for one circle */
1848                         while (ld(host_index)[next_ldn(host_index)].cmd) {      /* search for a occupied, but not in */
1849                                 /* command-processing ldn. */
1850                                 next_ldn(host_index)++;
1851                                 if (next_ldn(host_index) >= MAX_LOG_DEV)
1852                                         next_ldn(host_index) = 7;
1853                                 if (current_ldn == next_ldn(host_index)) {      /* One circle done ? */
1854                                         /* no non-processing ldn found */
1855                                         scmd_printk(KERN_WARNING, cmd,
1856         "IBM MCA SCSI: Cannot assign SCSI-device dynamically!\n"
1857         "              On ldn 7-14 SCSI-commands everywhere in progress.\n"
1858         "              Reporting DID_NO_CONNECT for device.\n");
1859                                         cmd->result = DID_NO_CONNECT << 16;     /* return no connect */
1860                                         if (done)
1861                                                 done(cmd);
1862                                         return 0;
1863                                 }
1864                         }
1865
1866                         /* unmap non-processing ldn */
1867                         for (id = 0; id < max_pun; id++)
1868                                 for (lun = 0; lun < 8; lun++) {
1869                                         if (get_ldn(host_index)[id][lun] == next_ldn(host_index)) {
1870                                                 get_ldn(host_index)[id][lun] = TYPE_NO_DEVICE;
1871                                                 get_scsi(host_index)[id][lun] = TYPE_NO_DEVICE;
1872                                                 /* unmap entry */
1873                                         }
1874                                 }
1875                         /* set reduced interrupt_handler-mode for checking */
1876                         local_checking_phase_flag(host_index) = 1;
1877                         /* map found ldn to pun,lun */
1878                         get_ldn(host_index)[target][cmd->device->lun] = next_ldn(host_index);
1879                         /* change ldn to the right value, that is now next_ldn */
1880                         ldn = next_ldn(host_index);
1881                         /* unassign all ldns (pun,lun,ldn does not matter for remove) */
1882                         immediate_assign(host_index, 0, 0, 0, REMOVE_LDN);
1883                         /* set only LDN for remapped device */
1884                         immediate_assign(host_index, target, cmd->device->lun, ldn, SET_LDN);
1885                         /* get device information for ld[ldn] */
1886                         if (device_exists(host_index, ldn, &ld(host_index)[ldn].block_length, &ld(host_index)[ldn].device_type)) {
1887                                 ld(host_index)[ldn].cmd = NULL; /* To prevent panic set 0, because
1888                                                                    devices that were not assigned,
1889                                                                    should have nothing in progress. */
1890                                 get_scsi(host_index)[target][cmd->device->lun] = ld(host_index)[ldn].device_type;
1891                                 /* increase assignment counters for statistics in /proc */
1892                                 IBM_DS(host_index).dynamical_assignments++;
1893                                 IBM_DS(host_index).ldn_assignments[ldn]++;
1894                         } else
1895                                 /* panic here, because a device, found at boottime has
1896                                    vanished */
1897                                 panic("IBM MCA SCSI: ldn=0x%x, SCSI-device on (%d,%d) vanished!\n", ldn, target, cmd->device->lun);
1898                         /* unassign again all ldns (pun,lun,ldn does not matter for remove) */
1899                         immediate_assign(host_index, 0, 0, 0, REMOVE_LDN);
1900                         /* remap all ldns, as written in the pun/lun table */
1901                         lun = 0;
1902 #ifdef CONFIG_SCSI_MULTI_LUN
1903                         for (lun = 0; lun < 8; lun++)
1904 #endif
1905                                 for (id = 0; id < max_pun; id++) {
1906                                         if (get_ldn(host_index)[id][lun] <= MAX_LOG_DEV)
1907                                                 immediate_assign(host_index, id, lun, get_ldn(host_index)[id][lun], SET_LDN);
1908                                 }
1909                         /* set back to normal interrupt_handling */
1910                         local_checking_phase_flag(host_index) = 0;
1911 #ifdef IM_DEBUG_PROBE
1912                         /* Information on syslog terminal */
1913                         printk("IBM MCA SCSI: ldn=0x%x dynamically reassigned to (%d,%d).\n", ldn, target, cmd->device->lun);
1914 #endif
1915                         /* increase next_ldn for next dynamical assignment */
1916                         next_ldn(host_index)++;
1917                         if (next_ldn(host_index) >= MAX_LOG_DEV)
1918                                 next_ldn(host_index) = 7;
1919                 } else {        /* wall against Linux accesses to the subsystem adapter */
1920                         cmd->result = DID_BAD_TARGET << 16;
1921                         if (done)
1922                                 done(cmd);
1923                         return 0;
1924                 }
1925         }
1926
1927         /*verify there is no command already in progress for this log dev */
1928         if (ld(host_index)[ldn].cmd)
1929                 panic("IBM MCA SCSI: cmd already in progress for this ldn.\n");
1930
1931         /*save done in cmd, and save cmd for the interrupt handler */
1932         cmd->scsi_done = done;
1933         ld(host_index)[ldn].cmd = cmd;
1934
1935         /*fill scb information independent of the scsi command */
1936         scb = &(ld(host_index)[ldn].scb);
1937         ld(host_index)[ldn].tsb.dev_status = 0;
1938         scb->enable = IM_REPORT_TSB_ONLY_ON_ERROR | IM_RETRY_ENABLE;
1939         scb->tsb_adr = isa_virt_to_bus(&(ld(host_index)[ldn].tsb));
1940         scsi_cmd = cmd->cmnd[0];
1941
1942         if (cmd->use_sg) {
1943                 i = cmd->use_sg;
1944                 sl = (struct scatterlist *) (cmd->request_buffer);
1945                 if (i > 16)
1946                         panic("IBM MCA SCSI: scatter-gather list too long.\n");
1947                 while (--i >= 0) {
1948                         ld(host_index)[ldn].sge[i].address = (void *) (isa_page_to_bus(sl[i].page) + sl[i].offset);
1949                         ld(host_index)[ldn].sge[i].byte_length = sl[i].length;
1950                 }
1951                 scb->enable |= IM_POINTER_TO_LIST;
1952                 scb->sys_buf_adr = isa_virt_to_bus(&(ld(host_index)[ldn].sge[0]));
1953                 scb->sys_buf_length = cmd->use_sg * sizeof(struct im_sge);
1954         } else {
1955                 scb->sys_buf_adr = isa_virt_to_bus(cmd->request_buffer);
1956                 /* recent Linux midlevel SCSI places 1024 byte for inquiry
1957                  * command. Far too much for old PS/2 hardware. */
1958                 switch (scsi_cmd) {
1959                         /* avoid command errors by setting bufferlengths to
1960                          * ANSI-standard. Beware of forcing it to 255,
1961                          * this could SEGV the kernel!!! */
1962                 case INQUIRY:
1963                 case REQUEST_SENSE:
1964                 case MODE_SENSE:
1965                 case MODE_SELECT:
1966                         if (cmd->request_bufflen > 255)
1967                                 scb->sys_buf_length = 255;
1968                         else
1969                                 scb->sys_buf_length = cmd->request_bufflen;
1970                         break;
1971                 case TEST_UNIT_READY:
1972                         scb->sys_buf_length = 0;
1973                         break;
1974                 default:
1975                         scb->sys_buf_length = cmd->request_bufflen;
1976                         break;
1977                 }
1978         }
1979         /*fill scb information dependent on scsi command */
1980
1981 #ifdef IM_DEBUG_CMD
1982         printk("issue scsi cmd=%02x to ldn=%d\n", scsi_cmd, ldn);
1983 #endif
1984
1985         /* for specific device-type debugging: */
1986 #ifdef IM_DEBUG_CMD_SPEC_DEV
1987         if (ld(host_index)[ldn].device_type == IM_DEBUG_CMD_DEVICE)
1988                 printk("(SCSI-device-type=0x%x) issue scsi cmd=%02x to ldn=%d\n", ld(host_index)[ldn].device_type, scsi_cmd, ldn);
1989 #endif
1990
1991         /* for possible panics store current command */
1992         last_scsi_command(host_index)[ldn] = scsi_cmd;
1993         last_scsi_type(host_index)[ldn] = IM_SCB;
1994         /* update statistical info */
1995         IBM_DS(host_index).total_accesses++;
1996         IBM_DS(host_index).ldn_access[ldn]++;
1997
1998         switch (scsi_cmd) {
1999         case READ_6:
2000         case WRITE_6:
2001         case READ_10:
2002         case WRITE_10:
2003         case READ_12:
2004         case WRITE_12:
2005                 /* Distinguish between disk and other devices. Only disks (that are the
2006                    most frequently accessed devices) should be supported by the
2007                    IBM-SCSI-Subsystem commands. */
2008                 switch (ld(host_index)[ldn].device_type) {
2009                 case TYPE_DISK: /* for harddisks enter here ... */
2010                 case TYPE_MOD:  /* ... try it also for MO-drives (send flames as */
2011                         /*     you like, if this won't work.) */
2012                         if (scsi_cmd == READ_6 || scsi_cmd == READ_10 || scsi_cmd == READ_12) {
2013                                 /* read command preparations */
2014                                 scb->enable |= IM_READ_CONTROL;
2015                                 IBM_DS(host_index).ldn_read_access[ldn]++;      /* increase READ-access on ldn stat. */
2016                                 scb->command = IM_READ_DATA_CMD | IM_NO_DISCONNECT;
2017                         } else {        /* write command preparations */
2018                                 IBM_DS(host_index).ldn_write_access[ldn]++;     /* increase write-count on ldn stat. */
2019                                 scb->command = IM_WRITE_DATA_CMD | IM_NO_DISCONNECT;
2020                         }
2021                         if (scsi_cmd == READ_6 || scsi_cmd == WRITE_6) {
2022                                 scb->u1.log_blk_adr = (((unsigned) cmd->cmnd[3]) << 0) | (((unsigned) cmd->cmnd[2]) << 8) | ((((unsigned) cmd->cmnd[1]) & 0x1f) << 16);
2023                                 scb->u2.blk.count = (unsigned) cmd->cmnd[4];
2024                         } else {
2025                                 scb->u1.log_blk_adr = (((unsigned) cmd->cmnd[5]) << 0) | (((unsigned) cmd->cmnd[4]) << 8) | (((unsigned) cmd->cmnd[3]) << 16) | (((unsigned) cmd->cmnd[2]) << 24);
2026                                 scb->u2.blk.count = (((unsigned) cmd->cmnd[8]) << 0) | (((unsigned) cmd->cmnd[7]) << 8);
2027                         }
2028                         last_scsi_logical_block(host_index)[ldn] = scb->u1.log_blk_adr;
2029                         last_scsi_blockcount(host_index)[ldn] = scb->u2.blk.count;
2030                         scb->u2.blk.length = ld(host_index)[ldn].block_length;
2031                         break;
2032                         /* for other devices, enter here. Other types are not known by
2033                            Linux! TYPE_NO_LUN is forbidden as valid device. */
2034                 case TYPE_ROM:
2035                 case TYPE_TAPE:
2036                 case TYPE_PROCESSOR:
2037                 case TYPE_WORM:
2038                 case TYPE_SCANNER:
2039                 case TYPE_MEDIUM_CHANGER:
2040                         /* If there is a sequential-device, IBM recommends to use
2041                            IM_OTHER_SCSI_CMD_CMD instead of subsystem READ/WRITE.
2042                            This includes CD-ROM devices, too, due to the partial sequential
2043                            read capabilities. */
2044                         scb->command = IM_OTHER_SCSI_CMD_CMD;
2045                         if (scsi_cmd == READ_6 || scsi_cmd == READ_10 || scsi_cmd == READ_12)
2046                                 /* enable READ */
2047                                 scb->enable |= IM_READ_CONTROL;
2048                         scb->enable |= IM_BYPASS_BUFFER;
2049                         scb->u1.scsi_cmd_length = cmd->cmd_len;
2050                         memcpy(scb->u2.scsi_command, cmd->cmnd, cmd->cmd_len);
2051                         last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
2052                         /* Read/write on this non-disk devices is also displayworthy,
2053                            so flash-up the LED/display. */
2054                         break;
2055                 }
2056                 break;
2057         case INQUIRY:
2058                 IBM_DS(host_index).ldn_inquiry_access[ldn]++;
2059                 scb->command = IM_DEVICE_INQUIRY_CMD;
2060                 scb->enable |= IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT | IM_BYPASS_BUFFER;
2061                 scb->u1.log_blk_adr = 0;
2062                 break;
2063         case TEST_UNIT_READY:
2064                 scb->command = IM_OTHER_SCSI_CMD_CMD;
2065                 scb->enable |= IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT | IM_BYPASS_BUFFER;
2066                 scb->u1.log_blk_adr = 0;
2067                 scb->u1.scsi_cmd_length = 6;
2068                 memcpy(scb->u2.scsi_command, cmd->cmnd, 6);
2069                 last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
2070                 break;
2071         case READ_CAPACITY:
2072                 /* the length of system memory buffer must be exactly 8 bytes */
2073                 scb->command = IM_READ_CAPACITY_CMD;
2074                 scb->enable |= IM_READ_CONTROL | IM_BYPASS_BUFFER;
2075                 if (scb->sys_buf_length > 8)
2076                         scb->sys_buf_length = 8;
2077                 break;
2078                 /* Commands that need read-only-mode (system <- device): */
2079         case REQUEST_SENSE:
2080                 scb->command = IM_REQUEST_SENSE_CMD;
2081                 scb->enable |= IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT | IM_BYPASS_BUFFER;
2082                 break;
2083                 /* Commands that need write-only-mode (system -> device): */
2084         case MODE_SELECT:
2085         case MODE_SELECT_10:
2086                 IBM_DS(host_index).ldn_modeselect_access[ldn]++;
2087                 scb->command = IM_OTHER_SCSI_CMD_CMD;
2088                 scb->enable |= IM_SUPRESS_EXCEPTION_SHORT | IM_BYPASS_BUFFER;   /*Select needs WRITE-enabled */
2089                 scb->u1.scsi_cmd_length = cmd->cmd_len;
2090                 memcpy(scb->u2.scsi_command, cmd->cmnd, cmd->cmd_len);
2091                 last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
2092                 break;
2093                 /* For other commands, read-only is useful. Most other commands are
2094                    running without an input-data-block. */
2095         default:
2096                 scb->command = IM_OTHER_SCSI_CMD_CMD;
2097                 scb->enable |= IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT | IM_BYPASS_BUFFER;
2098                 scb->u1.scsi_cmd_length = cmd->cmd_len;
2099                 memcpy(scb->u2.scsi_command, cmd->cmnd, cmd->cmd_len);
2100                 last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
2101                 break;
2102         }
2103         /*issue scb command, and return */
2104         if (++disk_rw_in_progress == 1)
2105                 PS2_DISK_LED_ON(shpnt->host_no, target);
2106
2107         if (last_scsi_type(host_index)[ldn] == IM_LONG_SCB) {
2108                 issue_cmd(host_index, isa_virt_to_bus(scb), IM_LONG_SCB | ldn);
2109                 IBM_DS(host_index).long_scbs++;
2110         } else {
2111                 issue_cmd(host_index, isa_virt_to_bus(scb), IM_SCB | ldn);
2112                 IBM_DS(host_index).scbs++;
2113         }
2114         return 0;
2115 }
2116
2117 static int __ibmmca_abort(Scsi_Cmnd * cmd)
2118 {
2119         /* Abort does not work, as the adapter never generates an interrupt on
2120          * whatever situation is simulated, even when really pending commands
2121          * are running on the adapters' hardware ! */
2122
2123         struct Scsi_Host *shpnt;
2124         unsigned int ldn;
2125         void (*saved_done) (Scsi_Cmnd *);
2126         int target;
2127         int host_index;
2128         int max_pun;
2129         unsigned long imm_command;
2130
2131 #ifdef IM_DEBUG_PROBE
2132         printk("IBM MCA SCSI: Abort subroutine called...\n");
2133 #endif
2134
2135         shpnt = cmd->device->host;
2136         /* search for the right hostadapter */
2137         for (host_index = 0; hosts[host_index] && hosts[host_index]->host_no != shpnt->host_no; host_index++);
2138
2139         if (!hosts[host_index]) {       /* invalid hostadapter descriptor address */
2140                 cmd->result = DID_NO_CONNECT << 16;
2141                 if (cmd->scsi_done)
2142                         (cmd->scsi_done) (cmd);
2143                 shpnt = cmd->device->host;
2144 #ifdef IM_DEBUG_PROBE
2145                 printk(KERN_DEBUG "IBM MCA SCSI: Abort adapter selection failed!\n");
2146 #endif
2147                 return SUCCESS;
2148         }
2149         max_pun = subsystem_maxid(host_index);
2150         if (ibm_ansi_order) {
2151                 target = max_pun - 1 - cmd->device->id;
2152                 if ((target <= subsystem_pun(host_index)) && (cmd->device->id <= subsystem_pun(host_index)))
2153                         target--;
2154                 else if ((target >= subsystem_pun(host_index)) && (cmd->device->id >= subsystem_pun(host_index)))
2155                         target++;
2156         } else
2157                 target = cmd->device->id;
2158
2159         /* get logical device number, and disable system interrupts */
2160         printk(KERN_WARNING "IBM MCA SCSI: Sending abort to device pun=%d, lun=%d.\n", target, cmd->device->lun);
2161         ldn = get_ldn(host_index)[target][cmd->device->lun];
2162
2163         /*if cmd for this ldn has already finished, no need to abort */
2164         if (!ld(host_index)[ldn].cmd) {
2165                     return SUCCESS;
2166         }
2167
2168         /* Clear ld.cmd, save done function, install internal done,
2169          * send abort immediate command (this enables sys. interrupts),
2170          * and wait until the interrupt arrives.
2171          */
2172         saved_done = cmd->scsi_done;
2173         cmd->scsi_done = internal_done;
2174         cmd->SCp.Status = 0;
2175         last_scsi_command(host_index)[ldn] = IM_ABORT_IMM_CMD;
2176         last_scsi_type(host_index)[ldn] = IM_IMM_CMD;
2177         imm_command = inl(IM_CMD_REG(host_index));
2178         imm_command &= (unsigned long) (0xffff0000);    /* mask reserved stuff */
2179         imm_command |= (unsigned long) (IM_ABORT_IMM_CMD);
2180         /* must wait for attention reg not busy */
2181         /* FIXME - timeout, politeness */
2182         while (1) {
2183                 if (!(inb(IM_STAT_REG(host_index)) & IM_BUSY))
2184                         break;
2185         }
2186         /* write registers and enable system interrupts */
2187         outl(imm_command, IM_CMD_REG(host_index));
2188         outb(IM_IMM_CMD | ldn, IM_ATTN_REG(host_index));
2189 #ifdef IM_DEBUG_PROBE
2190         printk("IBM MCA SCSI: Abort queued to adapter...\n");
2191 #endif
2192         spin_unlock_irq(shpnt->host_lock);
2193         while (!cmd->SCp.Status)
2194                 yield();
2195         spin_lock_irq(shpnt->host_lock);
2196         cmd->scsi_done = saved_done;
2197 #ifdef IM_DEBUG_PROBE
2198         printk("IBM MCA SCSI: Abort returned with adapter response...\n");
2199 #endif
2200
2201         /*if abort went well, call saved done, then return success or error */
2202         if (cmd->result == (DID_ABORT << 16)) 
2203         {
2204                 cmd->result |= DID_ABORT << 16;
2205                 if (cmd->scsi_done)
2206                         (cmd->scsi_done) (cmd);
2207                 ld(host_index)[ldn].cmd = NULL;
2208 #ifdef IM_DEBUG_PROBE
2209                 printk("IBM MCA SCSI: Abort finished with success.\n");
2210 #endif
2211                 return SUCCESS;
2212         } else {
2213                 cmd->result |= DID_NO_CONNECT << 16;
2214                 if (cmd->scsi_done)
2215                         (cmd->scsi_done) (cmd);
2216                 ld(host_index)[ldn].cmd = NULL;
2217 #ifdef IM_DEBUG_PROBE
2218                 printk("IBM MCA SCSI: Abort failed.\n");
2219 #endif
2220                 return FAILED;
2221         }
2222 }
2223
2224 static int ibmmca_abort(Scsi_Cmnd * cmd)
2225 {
2226         struct Scsi_Host *shpnt = cmd->device->host;
2227         int rc;
2228
2229         spin_lock_irq(shpnt->host_lock);
2230         rc = __ibmmca_abort(cmd);
2231         spin_unlock_irq(shpnt->host_lock);
2232
2233         return rc;
2234 }
2235
2236 static int __ibmmca_host_reset(Scsi_Cmnd * cmd)
2237 {
2238         struct Scsi_Host *shpnt;
2239         Scsi_Cmnd *cmd_aid;
2240         int ticks, i;
2241         int host_index;
2242         unsigned long imm_command;
2243
2244         BUG_ON(cmd == NULL);
2245
2246         ticks = IM_RESET_DELAY * HZ;
2247         shpnt = cmd->device->host;
2248         /* search for the right hostadapter */
2249         for (host_index = 0; hosts[host_index] && hosts[host_index]->host_no != shpnt->host_no; host_index++);
2250
2251         if (!hosts[host_index]) /* invalid hostadapter descriptor address */
2252                 return FAILED;
2253
2254         if (local_checking_phase_flag(host_index)) {
2255                 printk(KERN_WARNING "IBM MCA SCSI: unable to reset while checking devices.\n");
2256                 return FAILED;
2257         }
2258
2259         /* issue reset immediate command to subsystem, and wait for interrupt */
2260         printk("IBM MCA SCSI: resetting all devices.\n");
2261         reset_status(host_index) = IM_RESET_IN_PROGRESS;
2262         last_scsi_command(host_index)[0xf] = IM_RESET_IMM_CMD;
2263         last_scsi_type(host_index)[0xf] = IM_IMM_CMD;
2264         imm_command = inl(IM_CMD_REG(host_index));
2265         imm_command &= (unsigned long) (0xffff0000);    /* mask reserved stuff */
2266         imm_command |= (unsigned long) (IM_RESET_IMM_CMD);
2267         /* must wait for attention reg not busy */
2268         while (1) {
2269                 if (!(inb(IM_STAT_REG(host_index)) & IM_BUSY))
2270                         break;
2271                 spin_unlock_irq(shpnt->host_lock);
2272                 yield();
2273                 spin_lock_irq(shpnt->host_lock);
2274         }
2275         /*write registers and enable system interrupts */
2276         outl(imm_command, IM_CMD_REG(host_index));
2277         outb(IM_IMM_CMD | 0xf, IM_ATTN_REG(host_index));
2278         /* wait for interrupt finished or intr_stat register to be set, as the
2279          * interrupt will not be executed, while we are in here! */
2280          
2281         /* FIXME: This is really really icky we so want a sleeping version of this ! */
2282         while (reset_status(host_index) == IM_RESET_IN_PROGRESS && --ticks && ((inb(IM_INTR_REG(host_index)) & 0x8f) != 0x8f)) {
2283                 udelay((1 + 999 / HZ) * 1000);
2284                 barrier();
2285         }
2286         /* if reset did not complete, just return an error */
2287         if (!ticks) {
2288                 printk(KERN_ERR "IBM MCA SCSI: reset did not complete within %d seconds.\n", IM_RESET_DELAY);
2289                 reset_status(host_index) = IM_RESET_FINISHED_FAIL;
2290                 return FAILED;
2291         }
2292
2293         if ((inb(IM_INTR_REG(host_index)) & 0x8f) == 0x8f) {
2294                 /* analysis done by this routine and not by the intr-routine */
2295                 if (inb(IM_INTR_REG(host_index)) == 0xaf)
2296                         reset_status(host_index) = IM_RESET_FINISHED_OK_NO_INT;
2297                 else if (inb(IM_INTR_REG(host_index)) == 0xcf)
2298                         reset_status(host_index) = IM_RESET_FINISHED_FAIL;
2299                 else            /* failed, 4get it */
2300                         reset_status(host_index) = IM_RESET_NOT_IN_PROGRESS_NO_INT;
2301                 outb(IM_EOI | 0xf, IM_ATTN_REG(host_index));
2302         }
2303
2304         /* if reset failed, just return an error */
2305         if (reset_status(host_index) == IM_RESET_FINISHED_FAIL) {
2306                 printk(KERN_ERR "IBM MCA SCSI: reset failed.\n");
2307                 return FAILED;
2308         }
2309
2310         /* so reset finished ok - call outstanding done's, and return success */
2311         printk(KERN_INFO "IBM MCA SCSI: Reset successfully completed.\n");
2312         for (i = 0; i < MAX_LOG_DEV; i++) {
2313                 cmd_aid = ld(host_index)[i].cmd;
2314                 if (cmd_aid && cmd_aid->scsi_done) {
2315                         ld(host_index)[i].cmd = NULL;
2316                         cmd_aid->result = DID_RESET << 16;
2317                 }
2318         }
2319         return SUCCESS;
2320 }
2321
2322 static int ibmmca_host_reset(Scsi_Cmnd * cmd)
2323 {
2324         struct Scsi_Host *shpnt = cmd->device->host;
2325         int rc;
2326
2327         spin_lock_irq(shpnt->host_lock);
2328         rc = __ibmmca_host_reset(cmd);
2329         spin_unlock_irq(shpnt->host_lock);
2330
2331         return rc;
2332 }
2333
2334 static int ibmmca_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int *info)
2335 {
2336         int size = capacity;
2337         info[0] = 64;
2338         info[1] = 32;
2339         info[2] = size / (info[0] * info[1]);
2340         if (info[2] >= 1024) {
2341                 info[0] = 128;
2342                 info[1] = 63;
2343                 info[2] = size / (info[0] * info[1]);
2344                 if (info[2] >= 1024) {
2345                         info[0] = 255;
2346                         info[1] = 63;
2347                         info[2] = size / (info[0] * info[1]);
2348                         if (info[2] >= 1024)
2349                                 info[2] = 1023;
2350                 }
2351         }
2352         return 0;
2353 }
2354
2355 /* calculate percentage of total accesses on a ldn */
2356 static int ldn_access_load(int host_index, int ldn)
2357 {
2358         if (IBM_DS(host_index).total_accesses == 0)
2359                 return (0);
2360         if (IBM_DS(host_index).ldn_access[ldn] == 0)
2361                 return (0);
2362         return (IBM_DS(host_index).ldn_access[ldn] * 100) / IBM_DS(host_index).total_accesses;
2363 }
2364
2365 /* calculate total amount of r/w-accesses */
2366 static int ldn_access_total_read_write(int host_index)
2367 {
2368         int a;
2369         int i;
2370
2371         a = 0;
2372         for (i = 0; i <= MAX_LOG_DEV; i++)
2373                 a += IBM_DS(host_index).ldn_read_access[i] + IBM_DS(host_index).ldn_write_access[i];
2374         return (a);
2375 }
2376
2377 static int ldn_access_total_inquiry(int host_index)
2378 {
2379         int a;
2380         int i;
2381
2382         a = 0;
2383         for (i = 0; i <= MAX_LOG_DEV; i++)
2384                 a += IBM_DS(host_index).ldn_inquiry_access[i];
2385         return (a);
2386 }
2387
2388 static int ldn_access_total_modeselect(int host_index)
2389 {
2390         int a;
2391         int i;
2392
2393         a = 0;
2394         for (i = 0; i <= MAX_LOG_DEV; i++)
2395                 a += IBM_DS(host_index).ldn_modeselect_access[i];
2396         return (a);
2397 }
2398
2399 /* routine to display info in the proc-fs-structure (a deluxe feature) */
2400 static int ibmmca_proc_info(struct Scsi_Host *shpnt, char *buffer, char **start, off_t offset, int length, int inout)
2401 {
2402         int len = 0;
2403         int i, id, lun, host_index;
2404         unsigned long flags;
2405         int max_pun;
2406
2407         for (i = 0; hosts[i] && hosts[i] != shpnt; i++);
2408         
2409         spin_lock_irqsave(hosts[i]->host_lock, flags);  /* Check it */
2410         host_index = i;
2411         if (!shpnt) {
2412                 len += sprintf(buffer + len, "\nIBM MCA SCSI: Can't find adapter");
2413                 return len;
2414         }
2415         max_pun = subsystem_maxid(host_index);
2416
2417         len += sprintf(buffer + len, "\n             IBM-SCSI-Subsystem-Linux-Driver, Version %s\n\n\n", IBMMCA_SCSI_DRIVER_VERSION);
2418         len += sprintf(buffer + len, " SCSI Access-Statistics:\n");
2419         len += sprintf(buffer + len, "               Device Scanning Order....: %s\n", (ibm_ansi_order) ? "IBM/ANSI" : "New Industry Standard");
2420 #ifdef CONFIG_SCSI_MULTI_LUN
2421         len += sprintf(buffer + len, "               Multiple LUN probing.....: Yes\n");
2422 #else
2423         len += sprintf(buffer + len, "               Multiple LUN probing.....: No\n");
2424 #endif
2425         len += sprintf(buffer + len, "               This Hostnumber..........: %d\n", shpnt->host_no);
2426         len += sprintf(buffer + len, "               Base I/O-Port............: 0x%x\n", (unsigned int) (IM_CMD_REG(host_index)));
2427         len += sprintf(buffer + len, "               (Shared) IRQ.............: %d\n", IM_IRQ);
2428         len += sprintf(buffer + len, "               Total Interrupts.........: %d\n", IBM_DS(host_index).total_interrupts);
2429         len += sprintf(buffer + len, "               Total SCSI Accesses......: %d\n", IBM_DS(host_index).total_accesses);
2430         len += sprintf(buffer + len, "               Total short SCBs.........: %d\n", IBM_DS(host_index).scbs);
2431         len += sprintf(buffer + len, "               Total long SCBs..........: %d\n", IBM_DS(host_index).long_scbs);
2432         len += sprintf(buffer + len, "                 Total SCSI READ/WRITE..: %d\n", ldn_access_total_read_write(host_index));
2433         len += sprintf(buffer + len, "                 Total SCSI Inquiries...: %d\n", ldn_access_total_inquiry(host_index));
2434         len += sprintf(buffer + len, "                 Total SCSI Modeselects.: %d\n", ldn_access_total_modeselect(host_index));
2435         len += sprintf(buffer + len, "                 Total SCSI other cmds..: %d\n", IBM_DS(host_index).total_accesses - ldn_access_total_read_write(host_index)
2436                        - ldn_access_total_modeselect(host_index)
2437                        - ldn_access_total_inquiry(host_index));
2438         len += sprintf(buffer + len, "               Total SCSI command fails.: %d\n\n", IBM_DS(host_index).total_errors);
2439         len += sprintf(buffer + len, " Logical-Device-Number (LDN) Access-Statistics:\n");
2440         len += sprintf(buffer + len, "         LDN | Accesses [%%] |   READ    |   WRITE   | ASSIGNMENTS\n");
2441         len += sprintf(buffer + len, "        -----|--------------|-----------|-----------|--------------\n");
2442         for (i = 0; i <= MAX_LOG_DEV; i++)
2443                 len += sprintf(buffer + len, "         %2X  |    %3d       |  %8d |  %8d | %8d\n", i, ldn_access_load(host_index, i), IBM_DS(host_index).ldn_read_access[i], IBM_DS(host_index).ldn_write_access[i], IBM_DS(host_index).ldn_assignments[i]);
2444         len += sprintf(buffer + len, "        -----------------------------------------------------------\n\n");
2445         len += sprintf(buffer + len, " Dynamical-LDN-Assignment-Statistics:\n");
2446         len += sprintf(buffer + len, "               Number of physical SCSI-devices..: %d (+ Adapter)\n", IBM_DS(host_index).total_scsi_devices);
2447         len += sprintf(buffer + len, "               Dynamical Assignment necessary...: %s\n", IBM_DS(host_index).dyn_flag ? "Yes" : "No ");
2448         len += sprintf(buffer + len, "               Next LDN to be assigned..........: 0x%x\n", next_ldn(host_index));
2449         len += sprintf(buffer + len, "               Dynamical assignments done yet...: %d\n", IBM_DS(host_index).dynamical_assignments);
2450         len += sprintf(buffer + len, "\n Current SCSI-Device-Mapping:\n");
2451         len += sprintf(buffer + len, "        Physical SCSI-Device Map               Logical SCSI-Device Map\n");
2452         len += sprintf(buffer + len, "    ID\\LUN  0  1  2  3  4  5  6  7       ID\\LUN  0  1  2  3  4  5  6  7\n");
2453         for (id = 0; id < max_pun; id++) {
2454                 len += sprintf(buffer + len, "    %2d     ", id);
2455                 for (lun = 0; lun < 8; lun++)
2456                         len += sprintf(buffer + len, "%2s ", ti_p(get_scsi(host_index)[id][lun]));
2457                 len += sprintf(buffer + len, "      %2d     ", id);
2458                 for (lun = 0; lun < 8; lun++)
2459                         len += sprintf(buffer + len, "%2s ", ti_l(get_ldn(host_index)[id][lun]));
2460                 len += sprintf(buffer + len, "\n");
2461         }
2462
2463         len += sprintf(buffer + len, "(A = IBM-Subsystem, D = Harddisk, T = Tapedrive, P = Processor, W = WORM,\n");
2464         len += sprintf(buffer + len, " R = CD-ROM, S = Scanner, M = MO-Drive, C = Medium-Changer, + = unprovided LUN,\n");
2465         len += sprintf(buffer + len, " - = nothing found, nothing assigned or unprobed LUN)\n\n");
2466
2467         *start = buffer + offset;
2468         len -= offset;
2469         if (len > length)
2470                 len = length;
2471         spin_unlock_irqrestore(shpnt->host_lock, flags);
2472         return len;
2473 }
2474
2475 static int option_setup(char *str)
2476 {
2477         int ints[IM_MAX_HOSTS];
2478         char *cur = str;
2479         int i = 1;
2480
2481         while (cur && isdigit(*cur) && i <= IM_MAX_HOSTS) {
2482                 ints[i++] = simple_strtoul(cur, NULL, 0);
2483                 if ((cur = strchr(cur, ',')) != NULL)
2484                         cur++;
2485         }
2486         ints[0] = i - 1;
2487         internal_ibmmca_scsi_setup(cur, ints);
2488         return 1;
2489 }
2490
2491 __setup("ibmmcascsi=", option_setup);
2492
2493 static struct scsi_host_template driver_template = {
2494           .proc_name      = "ibmmca",
2495           .proc_info      = ibmmca_proc_info,
2496           .name           = "IBM SCSI-Subsystem",
2497           .detect         = ibmmca_detect,
2498           .release        = ibmmca_release,
2499           .queuecommand   = ibmmca_queuecommand,
2500           .eh_abort_handler = ibmmca_abort,
2501           .eh_host_reset_handler = ibmmca_host_reset,
2502           .bios_param     = ibmmca_biosparam,
2503           .can_queue      = 16,
2504           .this_id        = 7,
2505           .sg_tablesize   = 16,
2506           .cmd_per_lun    = 1,
2507           .use_clustering = ENABLE_CLUSTERING,
2508 };
2509 #include "scsi_module.c"