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