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