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