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