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