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