Linux 2.4.0-test7pre1
[davej-history.git] / drivers / scsi / ibmmca.c
blobaa4cca39018e5f58cfb0e59812d81390f9b257f7
1 /*
2 * Low Level Driver for the IBM Microchannel SCSI Subsystem
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 README.ibmmca for a detailed description of this driver,
8 * the commandline arguments and the history of its development.
9 * See the WWW-page: http://www.uni-mainz.de/~langm000/linux.html for latest
10 * updates, info and ADF-files for adapters supported by this driver.
13 /******************* HEADER FILE INCLUDES ************************************/
14 #ifndef LINUX_VERSION_CODE
15 #include <linux/version.h>
16 #endif
18 /* choose adaption for Kernellevel */
19 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,65)
20 #define OLDKERN
21 #else
22 #undef OLDKERN
23 #endif
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/ctype.h>
28 #include <linux/string.h>
29 #include <linux/ioport.h>
30 #include <linux/delay.h>
31 #include <linux/sched.h>
32 #include <linux/blk.h>
33 #include <linux/proc_fs.h>
34 #include <linux/stat.h>
35 #include <linux/mca.h>
36 #include <asm/system.h>
37 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,17)
38 #include <linux/spinlock.h>
39 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
40 #include <asm/spinlock.h>
41 #endif
42 #include <asm/io.h>
43 #include <linux/init.h>
44 #include "sd.h"
45 #include "scsi.h"
46 #include "hosts.h"
47 #include "ibmmca.h"
49 #include <linux/config.h> /* for CONFIG_SCSI_IBMMCA etc. */
51 /******************* LOCAL DEFINES *******************************************/
53 /* milliseconds of delay for timing out reset. */
54 #ifndef mdelay
55 #define mdelay(a) udelay((a) * 1000)
56 #endif
58 /*--------------------------------------------------------------------*/
60 /* current version of this driver-source: */
61 #define IBMMCA_SCSI_DRIVER_VERSION "3.2"
63 /*--------------------------------------------------------------------*/
65 /* driver configuration */
66 #define IM_MAX_HOSTS 8 /* maximum number of host adapters */
67 #define IM_RESET_DELAY 60 /* seconds allowed for a reset */
69 /* driver debugging - #undef all for normal operation */
71 /* if defined: count interrupts and ignore this special one: */
72 #undef IM_DEBUG_TIMEOUT 50
73 #define TIMEOUT_PUN 0
74 #define TIMEOUT_LUN 0
75 /* verbose interrupt: */
76 #undef IM_DEBUG_INT
77 /* verbose queuecommand: */
78 #undef IM_DEBUG_CMD
79 /* verbose queucommand for specific SCSI-device type: */
80 #undef IM_DEBUG_CMD_SPEC_DEV
81 /* verbose device probing */
82 #define IM_DEBUG_PROBE
84 /* device type that shall be displayed on syslog (only during debugging): */
85 #define IM_DEBUG_CMD_DEVICE TYPE_TAPE
87 /* relative addresses of hardware registers on a subsystem */
88 #define IM_CMD_REG(hi) (hosts[(hi)]->io_port) /*Command Interface, (4 bytes long) */
89 #define IM_ATTN_REG(hi) (hosts[(hi)]->io_port+4) /*Attention (1 byte) */
90 #define IM_CTR_REG(hi) (hosts[(hi)]->io_port+5) /*Basic Control (1 byte) */
91 #define IM_INTR_REG(hi) (hosts[(hi)]->io_port+6) /*Interrupt Status (1 byte, r/o) */
92 #define IM_STAT_REG(hi) (hosts[(hi)]->io_port+7) /*Basic Status (1 byte, read only) */
94 /* basic I/O-port of first adapter */
95 #define IM_IO_PORT 0x3540
96 /* maximum number of hosts that can be found */
97 #define IM_N_IO_PORT 8
99 /*requests going into the upper nibble of the Attention register */
100 /*note: the lower nibble specifies the device(0-14), or subsystem(15) */
101 #define IM_IMM_CMD 0x10 /*immediate command */
102 #define IM_SCB 0x30 /*Subsystem Control Block command */
103 #define IM_LONG_SCB 0x40 /*long Subsystem Control Block command */
104 #define IM_EOI 0xe0 /*end-of-interrupt request */
106 /*values for bits 7,1,0 of Basic Control reg. (bits 6-2 reserved) */
107 #define IM_HW_RESET 0x80 /*hardware reset */
108 #define IM_ENABLE_DMA 0x02 /*enable subsystem's busmaster DMA */
109 #define IM_ENABLE_INTR 0x01 /*enable interrupts to the system */
111 /*to interpret the upper nibble of Interrupt Status register */
112 /*note: the lower nibble specifies the device(0-14), or subsystem(15) */
113 #define IM_SCB_CMD_COMPLETED 0x10
114 #define IM_SCB_CMD_COMPLETED_WITH_RETRIES 0x50
115 #define IM_LOOP_SCATTER_BUFFER_FULL 0x60
116 #define IM_ADAPTER_HW_FAILURE 0x70
117 #define IM_IMMEDIATE_CMD_COMPLETED 0xa0
118 #define IM_CMD_COMPLETED_WITH_FAILURE 0xc0
119 #define IM_CMD_ERROR 0xe0
120 #define IM_SOFTWARE_SEQUENCING_ERROR 0xf0
122 /*to interpret bits 3-0 of Basic Status register (bits 7-4 reserved) */
123 #define IM_CMD_REG_FULL 0x08
124 #define IM_CMD_REG_EMPTY 0x04
125 #define IM_INTR_REQUEST 0x02
126 #define IM_BUSY 0x01
128 /*immediate commands (word written into low 2 bytes of command reg) */
129 #define IM_RESET_IMM_CMD 0x0400
130 #define IM_FEATURE_CTR_IMM_CMD 0x040c
131 #define IM_DMA_PACING_IMM_CMD 0x040d
132 #define IM_ASSIGN_IMM_CMD 0x040e
133 #define IM_ABORT_IMM_CMD 0x040f
134 #define IM_FORMAT_PREP_IMM_CMD 0x0417
136 /*SCB (Subsystem Control Block) structure */
137 struct im_scb
139 unsigned short command; /*command word (read, etc.) */
140 unsigned short enable; /*enable word, modifies cmd */
141 union
143 unsigned long log_blk_adr; /*block address on SCSI device */
144 unsigned char scsi_cmd_length; /*6,10,12, for other scsi cmd */
147 unsigned long sys_buf_adr; /*physical system memory adr */
148 unsigned long sys_buf_length; /*size of sys mem buffer */
149 unsigned long tsb_adr; /*Termination Status Block adr */
150 unsigned long scb_chain_adr; /*optional SCB chain address */
151 union
153 struct
155 unsigned short count; /*block count, on SCSI device */
156 unsigned short length; /*block length, on SCSI device */
158 blk;
159 unsigned char scsi_command[12]; /*other scsi command */
164 /*structure scatter-gather element (for list of system memory areas) */
165 struct im_sge
167 void *address;
168 unsigned long byte_length;
171 /*structure returned by a get_pos_info command: */
172 struct im_pos_info
174 unsigned short pos_id; /* adapter id */
175 unsigned char pos_3a; /* pos 3 (if pos 6 = 0) */
176 unsigned char pos_2; /* pos 2 */
177 unsigned char int_level; /* interrupt level IRQ 11 or 14 */
178 unsigned char pos_4a; /* pos 4 (if pos 6 = 0) */
179 unsigned short connector_size; /* MCA connector size: 16 or 32 Bit */
180 unsigned char num_luns; /* number of supported luns per device */
181 unsigned char num_puns; /* number of supported puns */
182 unsigned char pacing_factor; /* pacing factor */
183 unsigned char num_ldns; /* number of ldns available */
184 unsigned char eoi_off; /* time EOI and interrupt inactive */
185 unsigned char max_busy; /* time between reset and busy on */
186 unsigned short cache_stat; /* ldn cachestat. Bit=1 = not cached */
187 unsigned short retry_stat; /* retry status of ldns. Bit=1=disabled */
188 unsigned char pos_4b; /* pos 4 (if pos 6 = 1) */
189 unsigned char pos_3b; /* pos 3 (if pos 6 = 1) */
190 unsigned char pos_6; /* pos 6 */
191 unsigned char pos_5; /* pos 5 */
192 unsigned short max_overlap; /* maximum overlapping requests */
193 unsigned short num_bus; /* number of SCSI-busses */
196 /*values for SCB command word */
197 #define IM_NO_SYNCHRONOUS 0x0040 /*flag for any command */
198 #define IM_NO_DISCONNECT 0x0080 /*flag for any command */
199 #define IM_READ_DATA_CMD 0x1c01
200 #define IM_WRITE_DATA_CMD 0x1c02
201 #define IM_READ_VERIFY_CMD 0x1c03
202 #define IM_WRITE_VERIFY_CMD 0x1c04
203 #define IM_REQUEST_SENSE_CMD 0x1c08
204 #define IM_READ_CAPACITY_CMD 0x1c09
205 #define IM_DEVICE_INQUIRY_CMD 0x1c0b
206 #define IM_READ_LOGICAL_CMD 0x1c2a
207 #define IM_OTHER_SCSI_CMD_CMD 0x241f
209 /* unused, but supported, SCB commands */
210 #define IM_GET_COMMAND_COMPLETE_STATUS_CMD 0x1c07 /* command status */
211 #define IM_GET_POS_INFO_CMD 0x1c0a /* returns neat stuff */
212 #define IM_READ_PREFETCH_CMD 0x1c31 /* caching controller only */
213 #define IM_FOMAT_UNIT_CMD 0x1c16 /* format unit */
214 #define IM_REASSIGN_BLOCK_CMD 0x1c18 /* in case of error */
216 /*values to set bits in the enable word of SCB */
217 #define IM_READ_CONTROL 0x8000
218 #define IM_REPORT_TSB_ONLY_ON_ERROR 0x4000
219 #define IM_RETRY_ENABLE 0x2000
220 #define IM_POINTER_TO_LIST 0x1000
221 #define IM_SUPRESS_EXCEPTION_SHORT 0x0400
222 #define IM_BYPASS_BUFFER 0x0200
223 #define IM_CHAIN_ON_NO_ERROR 0x0001
225 /*TSB (Termination Status Block) structure */
226 struct im_tsb
228 unsigned short end_status;
229 unsigned short reserved1;
230 unsigned long residual_byte_count;
231 unsigned long sg_list_element_adr;
232 unsigned short status_length;
233 unsigned char dev_status;
234 unsigned char cmd_status;
235 unsigned char dev_error;
236 unsigned char cmd_error;
237 unsigned short reserved2;
238 unsigned short reserved3;
239 unsigned short low_of_last_scb_adr;
240 unsigned short high_of_last_scb_adr;
243 /*subsystem uses interrupt request level 14 */
244 #define IM_IRQ 14
245 /*SCSI-2 F/W may evade to interrupt 11 */
246 #define IM_IRQ_FW 11
248 /*--------------------------------------------------------------------*/
250 The model 95 doesn't have a standard activity light. Instead it
251 has a row of alphanumerial LEDs on the front. We use the last one
252 as the activity indicator if we think we're on a model 95. I suspect
253 the model id check will be either too narrow or too general, and some
254 machines won't have an activity indicator. Oh well...
256 The regular PS/2 disk led is turned on/off by bits 6,7 of system
257 control port.
260 /* LED display-port (actually, last LED on display) */
261 #define MOD95_LED_PORT 0x108
262 /* system-control-register of PS/2s with diskindicator */
263 #define PS2_SYS_CTR 0x92
264 /* activity displaying methods */
265 #define LED_DISP 1
266 #define LED_ADISP 2
267 #define LED_ACTIVITY 4
269 #define CMD_FAIL 255
271 /* The SCSI-ID(!) of the accessed SCSI-device is shown on PS/2-95 machines' LED
272 displays. ldn is no longer displayed here, because the ldn mapping is now
273 done dynamically and the ldn <-> pun,lun maps can be looked-up at boottime
274 or during uptime in /proc/scsi/ibmmca/<host_no> in case of trouble,
275 interest, debugging or just for having fun. The left number gives the
276 host-adapter number and the right shows the accessed SCSI-ID. */
278 /* display_mode is set by the ibmmcascsi= command line arg */
279 static int display_mode = 0;
280 /* set default adapter timeout */
281 static unsigned int adapter_timeout = 45;
282 /* for probing on feature-command: */
283 static unsigned int global_command_error_excuse = 0;
284 /* global setting by command line for adapter_speed */
285 static int global_adapter_speed = 0; /* full speed by default */
287 /* Panel / LED on, do it right for F/W addressin, too. adisplay will
288 * just ignore ids>7, as the panel has only 7 digits available */
289 #define PS2_DISK_LED_ON(ad,id) {\
290 if (display_mode & LED_DISP) { \
291 if (id>9) \
292 outw((ad+48)|((id+55)<<8), MOD95_LED_PORT ); \
293 else \
294 outw((ad+48)|((id+48)<<8), MOD95_LED_PORT ); } \
295 else if (display_mode & LED_ADISP) { \
296 if (id<7) outb((char)(id+48),MOD95_LED_PORT+1+id); \
297 outb((char)(ad+48), MOD95_LED_PORT); } \
298 if ((display_mode & LED_ACTIVITY)||(!display_mode)) \
299 outb(inb(PS2_SYS_CTR) | 0xc0, PS2_SYS_CTR); \
301 /* Panel / LED off */
302 /* bug fixed, Dec 15, 1997, where | was replaced by & here */
303 #define PS2_DISK_LED_OFF() {\
304 if (display_mode & LED_DISP) \
305 outw(0x2020, MOD95_LED_PORT ); \
306 else if (display_mode & LED_ADISP) { \
307 outl(0x20202020,MOD95_LED_PORT); \
308 outl(0x20202020,MOD95_LED_PORT+4); } \
309 if ((display_mode & LED_ACTIVITY)||(!display_mode)) \
310 outb(inb(PS2_SYS_CTR) & 0x3f, PS2_SYS_CTR); \
313 /*--------------------------------------------------------------------*/
315 /*list of supported subsystems */
316 struct subsys_list_struct
318 unsigned short mca_id;
319 char *description;
322 /* types of different supported hardware that goes to hostdata special */
323 #define IBM_SCSI2_FW 0
324 #define IBM_7568_WCACHE 1
325 #define IBM_EXP_UNIT 2
326 #define IBM_SCSI_WCACHE 3
327 #define IBM_SCSI 4
329 /* other special flags for hostdata structure */
330 #define FORCED_DETECTION 100
331 #define INTEGRATED_SCSI 101
333 /* List of possible IBM-SCSI-adapters */
334 struct subsys_list_struct subsys_list[] =
336 {0x8efc, "IBM SCSI-2 F/W Adapter"}, /* special = 0 */
337 {0x8efd, "IBM 7568 Industrial Computer SCSI Adapter w/Cache"}, /* special = 1 */
338 {0x8ef8, "IBM Expansion Unit SCSI Controller"},/* special = 2 */
339 {0x8eff, "IBM SCSI Adapter w/Cache"}, /* special = 3 */
340 {0x8efe, "IBM SCSI Adapter"}, /* special = 4 */
343 /*for /proc filesystem, only valid in older kernel releases */
344 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,27)
345 struct proc_dir_entry proc_scsi_ibmmca =
347 PROC_SCSI_IBMMCA, 6, "ibmmca",
348 S_IFDIR | S_IRUGO | S_IXUGO, 2,
349 0, 0, 0, NULL, NULL, NULL, NULL,
350 NULL, NULL, NULL
352 #endif
354 /* Max number of logical devices (can be up from 0 to 14). 15 is the address
355 of the adapter itself. */
356 #define MAX_LOG_DEV 15
358 /*local data for a logical device */
359 struct logical_device
361 struct im_scb scb; /* SCSI-subsystem-control-block structure */
362 struct im_tsb tsb; /* SCSI command complete status block structure */
363 struct im_sge sge[16]; /* scatter gather list structure */
364 unsigned char buf[256]; /* SCSI command return data buffer */
365 Scsi_Cmnd *cmd; /* SCSI-command that is currently in progress */
366 int device_type; /* type of the SCSI-device. See include/scsi/scsi.h
367 for interpretation of the possible values */
368 int block_length;/* blocksize of a particular logical SCSI-device */
369 int cache_flag; /* 1 if this is uncached, 0 if cache is present for ldn */
370 int retry_flag; /* 1 if adapter retry is disabled, 0 if enabled */
373 /* statistics of the driver during operations (for proc_info) */
374 struct Driver_Statistics
376 /* SCSI statistics on the adapter */
377 int ldn_access[MAX_LOG_DEV+1]; /* total accesses on a ldn */
378 int ldn_read_access[MAX_LOG_DEV+1]; /* total read-access on a ldn */
379 int ldn_write_access[MAX_LOG_DEV+1]; /* total write-access on a ldn */
380 int ldn_inquiry_access[MAX_LOG_DEV+1]; /* total inquiries on a ldn */
381 int ldn_modeselect_access[MAX_LOG_DEV+1]; /* total mode selects on ldn */
382 int scbs; /* short SCBs queued */
383 int long_scbs; /* long SCBs queued */
384 int total_accesses; /* total accesses on all ldns */
385 int total_interrupts; /* total interrupts (should be
386 same as total_accesses) */
387 int total_errors; /* command completed with error */
388 /* dynamical assignment statistics */
389 int total_scsi_devices; /* number of physical pun,lun */
390 int dyn_flag; /* flag showing dynamical mode */
391 int dynamical_assignments; /* number of remappings of ldns */
392 int ldn_assignments[MAX_LOG_DEV+1]; /* number of remappings of each
393 ldn */
396 /* data structure for each host adapter */
397 struct ibmmca_hostdata
399 /* array of logical devices: */
400 struct logical_device _ld[MAX_LOG_DEV+1];
401 /* array to convert (pun, lun) into logical device number: */
402 unsigned char _get_ldn[16][8];
403 /*array that contains the information about the physical SCSI-devices
404 attached to this host adapter: */
405 unsigned char _get_scsi[16][8];
406 /* used only when checking logical devices: */
407 int _local_checking_phase_flag;
408 /* report received interrupt: */
409 int _got_interrupt;
410 /* report termination-status of SCSI-command: */
411 int _stat_result;
412 /* reset status (used only when doing reset): */
413 int _reset_status;
414 /* code of the last SCSI command (needed for panic info): */
415 int _last_scsi_command[MAX_LOG_DEV+1];
416 /* identifier of the last SCSI-command type */
417 int _last_scsi_type[MAX_LOG_DEV+1];
418 /* last blockcount */
419 int _last_scsi_blockcount[MAX_LOG_DEV+1];
420 /* last locgical block address */
421 unsigned long _last_scsi_logical_block[MAX_LOG_DEV+1];
422 /* Counter that points on the next reassignable ldn for dynamical
423 remapping. The default value is 7, that is the first reassignable
424 number in the list at boottime: */
425 int _next_ldn;
426 /* Statistics-structure for this IBM-SCSI-host: */
427 struct Driver_Statistics _IBM_DS;
428 /* This hostadapters pos-registers pos2 until pos6 */
429 unsigned _pos2, _pos3, _pos4, _pos5, _pos6;
430 /* assign a special variable, that contains dedicated info about the
431 adaptertype */
432 int _special;
433 /* connector size on the MCA bus */
434 int _connector_size;
435 /* synchronous SCSI transfer rate bitpattern */
436 int _adapter_speed;
439 /* macros to access host data structure */
440 #define subsystem_pun(hi) (hosts[(hi)]->this_id)
441 #define subsystem_maxid(hi) (hosts[(hi)]->max_id)
442 #define ld(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_ld)
443 #define get_ldn(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_get_ldn)
444 #define get_scsi(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_get_scsi)
445 #define local_checking_phase_flag(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_local_checking_phase_flag)
446 #define got_interrupt(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_got_interrupt)
447 #define stat_result(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_stat_result)
448 #define reset_status(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_reset_status)
449 #define last_scsi_command(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_last_scsi_command)
450 #define last_scsi_type(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_last_scsi_type)
451 #define last_scsi_blockcount(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_last_scsi_blockcount)
452 #define last_scsi_logical_block(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_last_scsi_logical_block)
453 #define last_scsi_type(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_last_scsi_type)
454 #define next_ldn(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_next_ldn)
455 #define IBM_DS(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_IBM_DS)
456 #define special(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_special)
457 #define subsystem_connector_size(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_connector_size)
458 #define adapter_speed(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_adapter_speed)
459 #define pos2(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_pos2)
460 #define pos3(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_pos3)
461 #define pos4(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_pos4)
462 #define pos5(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_pos5)
463 #define pos6(hi) (((struct ibmmca_hostdata *) hosts[(hi)]->hostdata)->_pos6)
465 /* Define a arbitrary number as subsystem-marker-type. This number is, as
466 described in the ANSI-SCSI-standard, not occupied by other device-types. */
467 #define TYPE_IBM_SCSI_ADAPTER 0x2F
469 /* Define 0xFF for no device type, because this type is not defined within
470 the ANSI-SCSI-standard, therefore, it can be used and should not cause any
471 harm. */
472 #define TYPE_NO_DEVICE 0xFF
474 /* define medium-changer. If this is not defined previously, e.g. Linux
475 2.0.x, define this type here. */
476 #ifndef TYPE_MEDIUM_CHANGER
477 #define TYPE_MEDIUM_CHANGER 0x08
478 #endif
480 /* define possible operations for the immediate_assign command */
481 #define SET_LDN 0
482 #define REMOVE_LDN 1
484 /* ldn which is used to probe the SCSI devices */
485 #define PROBE_LDN 0
487 /* reset status flag contents */
488 #define IM_RESET_NOT_IN_PROGRESS 0
489 #define IM_RESET_IN_PROGRESS 1
490 #define IM_RESET_FINISHED_OK 2
491 #define IM_RESET_FINISHED_FAIL 3
492 #define IM_RESET_NOT_IN_PROGRESS_NO_INT 4
493 #define IM_RESET_FINISHED_OK_NO_INT 5
495 /* define undefined SCSI-command */
496 #define NO_SCSI 0xffff
498 /*-----------------------------------------------------------------------*/
500 /* if this is nonzero, ibmmcascsi option has been passed to the kernel */
501 static int io_port[IM_MAX_HOSTS] = { 0, 0, 0, 0, 0, 0, 0, 0 };
502 static int scsi_id[IM_MAX_HOSTS] = { 7, 7, 7, 7, 7, 7, 7, 7 };
504 /* fill module-parameters only, when this define is present.
505 (that is kernel version 2.1.x) */
506 #if defined(MODULE)
507 static char *boot_options = NULL;
508 #include <linux/module.h>
509 MODULE_PARM(boot_options, "s");
510 MODULE_PARM(io_port, "1-" __MODULE_STRING(IM_MAX_HOSTS) "i");
511 MODULE_PARM(scsi_id, "1-" __MODULE_STRING(IM_MAX_HOSTS) "i");
512 MODULE_PARM(display, "1i");
513 MODULE_PARM(adisplay, "1i");
514 MODULE_PARM(bypass, "1i");
515 MODULE_PARM(normal, "1i");
516 MODULE_PARM(ansi, "1i");
517 #endif
519 /*counter of concurrent disk read/writes, to turn on/off disk led */
520 static int disk_rw_in_progress = 0;
522 /* spinlock handling to avoid command clash while in operation */
523 #ifndef OLDKERN
524 spinlock_t info_lock = SPIN_LOCK_UNLOCKED;
525 spinlock_t proc_lock = SPIN_LOCK_UNLOCKED;
526 spinlock_t abort_lock = SPIN_LOCK_UNLOCKED;
527 spinlock_t reset_lock = SPIN_LOCK_UNLOCKED;
528 spinlock_t issue_lock = SPIN_LOCK_UNLOCKED;
529 spinlock_t intr_lock = SPIN_LOCK_UNLOCKED;
530 #endif
532 /* host information */
533 static int found = 0;
534 static struct Scsi_Host *hosts[IM_MAX_HOSTS+1] = { NULL, NULL, NULL, NULL,
535 NULL, NULL, NULL, NULL,
536 NULL };
537 static unsigned int pos[8]; /* whole pos register-line for diagnosis */
538 /* Taking into account the additions, made by ZP Gu.
539 * This selects now the preset value from the configfile and
540 * offers the 'normal' commandline option to be accepted */
541 #ifdef CONFIG_IBMMCA_SCSI_ORDER_STANDARD
542 static char ibm_ansi_order = 1;
543 #else
544 static char ibm_ansi_order = 0;
545 #endif
547 /*-----------------------------------------------------------------------*/
549 /******************* FUNCTIONS IN FORWARD DECLARATION ************************/
551 static void interrupt_handler (int, void *, struct pt_regs *);
552 #ifndef OLDKERN
553 static void do_interrupt_handler (int, void *, struct pt_regs *);
554 #endif
555 static void issue_cmd (int, unsigned long, unsigned char);
556 static void internal_done (Scsi_Cmnd * cmd);
557 static void check_devices (int, int);
558 static int immediate_assign(int, unsigned int, unsigned int, unsigned int,
559 unsigned int);
560 static int immediate_feature(int, unsigned int, unsigned int);
561 #ifdef CONFIG_IBMMCA_SCSI_DEV_RESET
562 static int immediate_reset(int, unsigned int);
563 #endif
564 static int device_inquiry(int, int);
565 static int read_capacity(int, int);
566 static int get_pos_info(int);
567 static char *ti_p(int);
568 static char *ti_l(int);
569 static char *ibmrate(unsigned int, int);
570 static int probe_display(int);
571 static int probe_bus_mode(int);
572 static int device_exists (int, int, int *, int *);
573 static struct Scsi_Host *ibmmca_register(Scsi_Host_Template *,
574 int, int, int, char *);
575 /* local functions needed for proc_info */
576 static int ldn_access_load(int, int);
577 static int ldn_access_total_read_write(int);
579 static int bypass_controller = 0; /* bypass integrated SCSI-cmd set flag */
581 /*--------------------------------------------------------------------*/
583 /******************* LOCAL FUNCTIONS IMPLEMENTATION *************************/
585 #ifndef OLDKERN
586 /* newer Kernels need the spinlock interrupt handler */
587 static void do_interrupt_handler (int irq, void *dev_id, struct pt_regs *regs)
589 unsigned long flags;
591 spin_lock_irqsave(&io_request_lock, flags);
592 interrupt_handler(irq, dev_id, regs);
593 spin_unlock_irqrestore(&io_request_lock, flags);
594 return;
596 #endif
598 static void interrupt_handler (int irq, void *dev_id, struct pt_regs *regs)
600 int host_index;
601 unsigned int intr_reg;
602 unsigned int cmd_result;
603 unsigned int ldn;
604 unsigned long flags;
605 Scsi_Cmnd *cmd;
606 int lastSCSI;
608 host_index = 0; /* make sure, host_index is 0 */
610 /* search for one adapter-response on shared interrupt */
611 while (hosts[host_index]
612 && !(inb(IM_STAT_REG(host_index)) & IM_INTR_REQUEST))
613 host_index++;
615 /* return if some other device on this IRQ caused the interrupt */
616 if (!hosts[host_index]) return;
618 /* the reset-function already did all the job, even ints got
619 renabled on the subsystem, so just return */
620 if ((reset_status(host_index) == IM_RESET_NOT_IN_PROGRESS_NO_INT)||
621 (reset_status(host_index) == IM_RESET_FINISHED_OK_NO_INT))
623 reset_status(host_index) = IM_RESET_NOT_IN_PROGRESS;
624 return;
627 /*must wait for attention reg not busy, then send EOI to subsystem */
628 while (1)
630 #ifdef OLDKERN
631 save_flags(flags);
632 cli();
633 #else
634 spin_lock_irqsave(&intr_lock, flags);
635 #endif
636 /* if (!(inb (IM_STAT_REG(host_index)) & IM_BUSY)) */
637 if ((inb(IM_STAT_REG(host_index)) & 0xf) == (IM_CMD_REG_EMPTY | IM_INTR_REQUEST))
638 break;
639 #ifdef OLDKERN
640 restore_flags(flags);
641 #else
642 spin_unlock_irqrestore(&intr_lock, flags);
643 #endif
645 /*get command result and logical device */
646 intr_reg = (unsigned char)(inb (IM_INTR_REG(host_index)));
647 cmd_result = intr_reg & 0xf0;
648 ldn = intr_reg & 0x0f;
650 /* get the last_scsi_command here */
651 lastSCSI = last_scsi_command(host_index)[ldn];
653 /*these should never happen (hw fails, or a local programming bug) */
654 if (!global_command_error_excuse)
656 switch (cmd_result)
657 { /* Prevent from Ooopsing on error to show the real reason */
658 case IM_ADAPTER_HW_FAILURE:
659 case IM_SOFTWARE_SEQUENCING_ERROR:
660 case IM_CMD_ERROR:
661 printk("\nIBM MCA SCSI: Fatal Subsystem ERROR!\n");
662 printk(" Last cmd=0x%x, ena=%x, len=",lastSCSI,
663 ld(host_index)[ldn].scb.enable);
664 if (ld(host_index)[ldn].cmd)
665 printk("%ld/%ld",(long)(ld(host_index)[ldn].cmd->request_bufflen),
666 (long)(ld(host_index)[ldn].scb.sys_buf_length));
667 else
668 printk("none");
669 printk(", ");
670 if (ld(host_index)[ldn].cmd)
671 printk("Blocksize=%d",ld(host_index)[ldn].scb.u2.blk.length);
672 else
673 printk("Blocksize=none");
674 printk(", host=0x%x, ldn=0x%x\n",host_index, ldn);
675 if (ld(host_index)[ldn].cmd)
677 printk("Blockcount=%d/%d\n",last_scsi_blockcount(host_index)[ldn],
678 ld(host_index)[ldn].scb.u2.blk.count);
679 printk("Logical block=%lx/%lx\n",last_scsi_logical_block(host_index)[ldn],
680 ld(host_index)[ldn].scb.u1.log_blk_adr);
682 printk("Reason given: %s\n",
683 (cmd_result==IM_ADAPTER_HW_FAILURE) ? "HARDWARE FAILURE" :
684 (cmd_result==IM_SOFTWARE_SEQUENCING_ERROR) ? "SOFTWARE SEQUENCING ERROR" :
685 (cmd_result==IM_CMD_ERROR) ? "COMMAND ERROR" : "UNKNOWN");
686 /* if errors appear, enter this section to give detailed info */
687 printk("IBM MCA SCSI: Subsystem Error-Status follows:\n");
688 printk(" Command Type................: %x\n",
689 last_scsi_type(host_index)[ldn]);
690 printk(" Attention Register..........: %x\n",
691 inb (IM_ATTN_REG(host_index)));
692 printk(" Basic Control Register......: %x\n",
693 inb (IM_CTR_REG(host_index)));
694 printk(" Interrupt Status Register...: %x\n",
695 intr_reg);
696 printk(" Basic Status Register.......: %x\n",
697 inb (IM_STAT_REG(host_index)));
698 if ((last_scsi_type(host_index)[ldn]==IM_SCB)||
699 (last_scsi_type(host_index)[ldn]==IM_LONG_SCB))
701 printk(" SCB-Command.................: %x\n",
702 ld(host_index)[ldn].scb.command);
703 printk(" SCB-Enable..................: %x\n",
704 ld(host_index)[ldn].scb.enable);
705 printk(" SCB-logical block address...: %lx\n",
706 ld(host_index)[ldn].scb.u1.log_blk_adr);
707 printk(" SCB-system buffer address...: %lx\n",
708 ld(host_index)[ldn].scb.sys_buf_adr);
709 printk(" SCB-system buffer length....: %lx\n",
710 ld(host_index)[ldn].scb.sys_buf_length);
711 printk(" SCB-tsb address.............: %lx\n",
712 ld(host_index)[ldn].scb.tsb_adr);
713 printk(" SCB-Chain address...........: %lx\n",
714 ld(host_index)[ldn].scb.scb_chain_adr);
715 printk(" SCB-block count.............: %x\n",
716 ld(host_index)[ldn].scb.u2.blk.count);
717 printk(" SCB-block length............: %x\n",
718 ld(host_index)[ldn].scb.u2.blk.length);
720 printk(" Send this report to the maintainer.\n");
721 panic("IBM MCA SCSI: Fatal errormessage from the subsystem (0x%X,0x%X)!\n",
722 lastSCSI,cmd_result);
723 break;
726 else
727 { /* The command error handling is made silent, but we tell the
728 * calling function, that there is a reported error from the
729 * adapter. */
730 switch (cmd_result)
732 case IM_ADAPTER_HW_FAILURE:
733 case IM_SOFTWARE_SEQUENCING_ERROR:
734 case IM_CMD_ERROR:
735 global_command_error_excuse = CMD_FAIL;
736 break;
737 default:
738 global_command_error_excuse = 0;
739 break;
743 /* if no panic appeared, increase the interrupt-counter */
744 IBM_DS(host_index).total_interrupts++;
746 /*only for local checking phase */
747 if (local_checking_phase_flag(host_index))
749 stat_result(host_index) = cmd_result;
750 got_interrupt(host_index) = 1;
751 reset_status(host_index) = IM_RESET_FINISHED_OK;
752 last_scsi_command(host_index)[ldn] = NO_SCSI;
754 outb (IM_EOI | ldn, IM_ATTN_REG(host_index));
755 #ifdef OLDKERN
756 restore_flags(flags);
757 #else
758 spin_unlock_irqrestore(&intr_lock, flags);
759 #endif
760 return;
762 /*handling of commands coming from upper level of scsi driver */
763 else
765 if (last_scsi_type(host_index)[ldn] == IM_IMM_CMD)
767 /*verify ldn, and may handle rare reset immediate command */
768 if ((reset_status(host_index) == IM_RESET_IN_PROGRESS)&&
769 (last_scsi_command(host_index)[ldn] == IM_RESET_IMM_CMD))
771 if (cmd_result == IM_CMD_COMPLETED_WITH_FAILURE)
773 disk_rw_in_progress = 0;
774 PS2_DISK_LED_OFF ();
775 reset_status(host_index) = IM_RESET_FINISHED_FAIL;
777 else
779 /*reset disk led counter, turn off disk led */
780 disk_rw_in_progress = 0;
781 PS2_DISK_LED_OFF ();
782 reset_status(host_index) = IM_RESET_FINISHED_OK;
784 stat_result(host_index) = cmd_result;
785 last_scsi_command(host_index)[ldn] = NO_SCSI;
786 last_scsi_type(host_index)[ldn] = 0;
787 outb (IM_EOI | ldn, IM_ATTN_REG(host_index));
788 #ifdef OLDKERN
789 restore_flags(flags);
790 #else
791 spin_unlock_irqrestore(&intr_lock, flags);
792 #endif
793 return;
795 else if (last_scsi_command(host_index)[ldn] == IM_ABORT_IMM_CMD)
796 { /* react on SCSI abort command */
797 #ifdef IM_DEBUG_PROBE
798 printk("IBM MCA SCSI: Interrupt from SCSI-abort.\n");
799 #endif
800 disk_rw_in_progress = 0;
801 PS2_DISK_LED_OFF();
802 cmd = ld(host_index)[ldn].cmd;
803 ld(host_index)[ldn].cmd = NULL;
804 if (cmd_result == IM_CMD_COMPLETED_WITH_FAILURE)
805 cmd->result = DID_NO_CONNECT << 16;
806 else
807 cmd->result = DID_ABORT << 16;
808 stat_result(host_index) = cmd_result;
809 last_scsi_command(host_index)[ldn] = NO_SCSI;
810 last_scsi_type(host_index)[ldn] = 0;
811 outb (IM_EOI | ldn, IM_ATTN_REG(host_index));
812 #ifdef OLDKERN
813 restore_flags(flags);
814 #else
815 spin_unlock_irqrestore(&intr_lock, flags);
816 #endif
817 if (cmd->scsi_done)
818 (cmd->scsi_done)(cmd); /* should be the internal_done */
819 return;
821 else
823 disk_rw_in_progress = 0;
824 PS2_DISK_LED_OFF ();
825 reset_status(host_index) = IM_RESET_FINISHED_OK;
826 stat_result(host_index) = cmd_result;
827 last_scsi_command(host_index)[ldn] = NO_SCSI;
829 outb (IM_EOI | ldn, IM_ATTN_REG(host_index));
830 #ifdef OLDKERN
831 restore_flags(flags);
832 #else
833 spin_unlock_irqrestore(&intr_lock, flags);
834 #endif
835 return;
838 last_scsi_command(host_index)[ldn] = NO_SCSI;
839 last_scsi_type(host_index)[ldn] = 0;
840 cmd = ld(host_index)[ldn].cmd;
841 ld(host_index)[ldn].cmd = NULL;
842 #ifdef IM_DEBUG_TIMEOUT
843 if (cmd)
845 if ((cmd->target == TIMEOUT_PUN)&&
846 (cmd->lun == TIMEOUT_LUN))
848 printk("IBM MCA SCSI: Ignoring interrupt from pun=%x, lun=%x.\n",
849 cmd->target, cmd->lun);
850 outb (IM_EOI | ldn, IM_ATTN_REG(host_index));
851 #ifdef OLDKERN
852 restore_flags(flags);
853 #else
854 spin_unlock_irqrestore(&intr_lock, flags);
855 #endif
856 return;
859 #endif
860 /*if no command structure, just return, else clear cmd */
861 if (!cmd)
863 outb (IM_EOI | ldn, IM_ATTN_REG(host_index));
864 #ifdef OLDKERN
865 restore_flags(flags);
866 #else
867 spin_unlock_irqrestore(&intr_lock, flags);
868 #endif
869 return;
872 #ifdef IM_DEBUG_INT
873 printk("cmd=%02x ireg=%02x ds=%02x cs=%02x de=%02x ce=%02x\n",
874 cmd->cmnd[0], intr_reg,
875 ld(host_index)[ldn].tsb.dev_status,
876 ld(host_index)[ldn].tsb.cmd_status,
877 ld(host_index)[ldn].tsb.dev_error,
878 ld(host_index)[ldn].tsb.cmd_error);
879 #endif
881 /*if this is end of media read/write, may turn off PS/2 disk led */
882 if ((ld(host_index)[ldn].device_type!=TYPE_NO_LUN)&&
883 (ld(host_index)[ldn].device_type!=TYPE_NO_DEVICE))
884 { /* only access this, if there was a valid device addressed */
885 switch (cmd->cmnd[0])
887 case READ_6:
888 case WRITE_6:
889 case READ_10:
890 case WRITE_10:
891 case READ_12:
892 case WRITE_12:
893 if (--disk_rw_in_progress == 0)
894 PS2_DISK_LED_OFF ();
898 /* IBM describes the status-mask to be 0x1e, but this is not conform
899 * with SCSI-defintion, I suppose, the reason for it is that IBM
900 * adapters do not support CMD_TERMINATED, TASK_SET_FULL and
901 * ACA_ACTIVE as returning statusbyte information. (ML) */
902 if (cmd_result == IM_CMD_COMPLETED_WITH_FAILURE)
904 cmd->result = (unsigned char)(ld(host_index)[ldn].tsb.dev_status & 0x1e);
905 IBM_DS(host_index).total_errors++;
907 else
908 cmd->result = 0;
909 /* write device status into cmd->result, and call done function */
910 if (lastSCSI == NO_SCSI) /* unexpected interrupt :-( */
911 cmd->result |= DID_BAD_INTR << 16;
912 else /* things went right :-) */
913 cmd->result |= DID_OK << 16;
914 outb (IM_EOI | ldn, IM_ATTN_REG(host_index));
915 #ifdef OLDKERN
916 restore_flags(flags);
917 #else
918 spin_unlock_irqrestore(&intr_lock, flags);
919 #endif
920 /* This is for Kernel 2.2.x. Something weired happens here.
921 * Between the command got queued and the interrupt is released,
922 * the flags sometimes contain different values, which must
923 * be a strange thing. E.g. it appears when cold-booting with a
924 * tape drive at id0. */
925 cmd->flags &= 0x3f;
926 if (cmd->scsi_done)
927 (cmd->scsi_done)(cmd);
929 if (lastSCSI == NO_SCSI)
930 printk("IBM MCA SCSI: WARNING - Interrupt from non-pending SCSI-command!\n");
931 return;
934 /*--------------------------------------------------------------------*/
936 static void issue_cmd (int host_index, unsigned long cmd_reg,
937 unsigned char attn_reg)
939 static unsigned long flags;
940 /* must wait for attention reg not busy */
941 while (1)
943 #ifdef OLDKERN
944 save_flags(flags);
945 cli();
946 #else
947 spin_lock_irqsave(&issue_lock, flags);
948 #endif
949 if (!(inb(IM_STAT_REG(host_index)) & IM_BUSY))
950 break;
951 #ifdef OLDKERN
952 restore_flags(flags);
953 #else
954 spin_unlock_irqrestore(&issue_lock, flags);
955 #endif
957 /*write registers and enable system interrupts */
958 outl (cmd_reg, IM_CMD_REG(host_index));
959 outb (attn_reg, IM_ATTN_REG(host_index));
960 #ifdef OLDKERN
961 restore_flags(flags);
962 #else
963 spin_unlock_irqrestore(&issue_lock, flags);
964 #endif
965 return;
968 /*--------------------------------------------------------------------*/
970 static void internal_done (Scsi_Cmnd * cmd)
972 cmd->SCp.Status++;
973 return;
976 /*--------------------------------------------------------------------*/
978 /* SCSI-SCB-command for device_inquiry */
979 static int device_inquiry(int host_index, int ldn)
981 int retries;
982 Scsi_Cmnd cmd;
983 struct im_scb *scb;
984 struct im_tsb *tsb;
985 unsigned char *buf;
987 scb = &(ld(host_index)[ldn].scb);
988 tsb = &(ld(host_index)[ldn].tsb);
989 buf = (unsigned char *)(&(ld(host_index)[ldn].buf));
990 ld(host_index)[ldn].tsb.dev_status = 0; /* prepare stusblock */
992 if (bypass_controller)
993 { /* fill the commonly known field for device-inquiry SCSI cmnd */
994 cmd.cmd_len = 6;
995 memset (&(cmd.cmnd), 0x0, sizeof(char) * cmd.cmd_len);
996 cmd.cmnd[0] = INQUIRY; /* device inquiry */
997 cmd.cmnd[4] = 0xff; /* return buffer size = 255 */
999 for (retries = 0; retries < 3; retries++)
1001 if (bypass_controller)
1002 { /* bypass the hardware integrated command set */
1003 scb->command = IM_OTHER_SCSI_CMD_CMD | IM_NO_DISCONNECT;
1004 scb->enable = IM_REPORT_TSB_ONLY_ON_ERROR | IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT | IM_BYPASS_BUFFER;
1005 scb->u1.scsi_cmd_length = cmd.cmd_len;
1006 memcpy (scb->u2.scsi_command, &(cmd.cmnd), cmd.cmd_len);
1007 last_scsi_command(host_index)[ldn] = INQUIRY;
1008 last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
1010 else
1012 /*fill scb with inquiry command */
1013 scb->command = IM_DEVICE_INQUIRY_CMD | IM_NO_DISCONNECT;
1014 scb->enable = IM_REPORT_TSB_ONLY_ON_ERROR | IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT;
1015 last_scsi_command(host_index)[ldn] = IM_DEVICE_INQUIRY_CMD;
1016 last_scsi_type(host_index)[ldn] = IM_SCB;
1018 scb->sys_buf_adr = virt_to_bus(buf);
1019 scb->sys_buf_length = 0xff; /* maximum bufferlength gives max info */
1020 scb->tsb_adr = virt_to_bus(tsb);
1022 /*issue scb to passed ldn, and busy wait for interrupt */
1023 got_interrupt(host_index) = 0;
1024 if ((scb->command & IM_OTHER_SCSI_CMD_CMD) == IM_OTHER_SCSI_CMD_CMD)
1025 issue_cmd (host_index, virt_to_bus(scb), IM_LONG_SCB | ldn);
1026 else
1027 issue_cmd (host_index, virt_to_bus(scb), IM_SCB | ldn);
1028 while (!got_interrupt(host_index))
1029 barrier ();
1031 /*if command succesful, break */
1032 if ((stat_result(host_index) == IM_SCB_CMD_COMPLETED)||
1033 (stat_result(host_index) == IM_SCB_CMD_COMPLETED_WITH_RETRIES))
1034 return 1;
1037 /*if all three retries failed, return "no device at this ldn" */
1038 if (retries >= 3)
1039 return 0;
1040 else
1041 return 1;
1044 static int read_capacity(int host_index, int ldn)
1046 int retries;
1047 Scsi_Cmnd cmd;
1048 struct im_scb *scb;
1049 struct im_tsb *tsb;
1050 unsigned char *buf;
1052 scb = &(ld(host_index)[ldn].scb);
1053 tsb = &(ld(host_index)[ldn].tsb);
1054 buf = (unsigned char *)(&(ld(host_index)[ldn].buf));
1055 ld(host_index)[ldn].tsb.dev_status = 0;
1057 if (bypass_controller)
1058 { /* read capacity in commonly known default SCSI-format */
1059 cmd.cmd_len = 10;
1060 memset (&(cmd.cmnd), 0x0, sizeof(char) * cmd.cmd_len);
1061 cmd.cmnd[0] = READ_CAPACITY; /* read capacity */
1063 for (retries = 0; retries < 3; retries++)
1065 /*fill scb with read capacity command */
1066 if (bypass_controller)
1067 { /* bypass the SCSI-command */
1068 scb->command = IM_OTHER_SCSI_CMD_CMD;
1069 scb->enable = IM_REPORT_TSB_ONLY_ON_ERROR | IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT | IM_BYPASS_BUFFER;
1070 scb->u1.scsi_cmd_length = cmd.cmd_len;
1071 memcpy (scb->u2.scsi_command, &(cmd.cmnd), cmd.cmd_len);
1072 last_scsi_command(host_index)[ldn] = READ_CAPACITY;
1073 last_scsi_type(host_index)[ldn] = IM_SCB;
1075 else
1077 scb->command = IM_READ_CAPACITY_CMD;
1078 scb->enable = IM_REPORT_TSB_ONLY_ON_ERROR | IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT;
1079 last_scsi_command(host_index)[ldn] = IM_READ_CAPACITY_CMD;
1080 last_scsi_type(host_index)[ldn] = IM_SCB;
1082 scb->sys_buf_adr = virt_to_bus(buf);
1083 scb->sys_buf_length = 8;
1084 scb->tsb_adr = virt_to_bus(tsb);
1086 /*issue scb to passed ldn, and busy wait for interrupt */
1087 got_interrupt(host_index) = 0;
1088 if ((scb->command & IM_OTHER_SCSI_CMD_CMD) == IM_OTHER_SCSI_CMD_CMD)
1089 issue_cmd (host_index, virt_to_bus(scb), IM_LONG_SCB | ldn);
1090 else
1091 issue_cmd (host_index, virt_to_bus(scb), IM_SCB | ldn);
1092 while (!got_interrupt(host_index))
1093 barrier ();
1095 /*if got capacity, get block length and return one device found */
1096 if ((stat_result(host_index) == IM_SCB_CMD_COMPLETED)||
1097 (stat_result(host_index) == IM_SCB_CMD_COMPLETED_WITH_RETRIES))
1098 return 1;
1100 /*if all three retries failed, return "no device at this ldn" */
1101 if (retries >= 3)
1102 return 0;
1103 else
1104 return 1;
1107 static int get_pos_info(int host_index)
1109 int retries;
1110 struct im_scb *scb;
1111 struct im_tsb *tsb;
1112 unsigned char *buf;
1114 scb = &(ld(host_index)[MAX_LOG_DEV].scb);
1115 tsb = &(ld(host_index)[MAX_LOG_DEV].tsb);
1116 buf = (unsigned char *)(&(ld(host_index)[MAX_LOG_DEV].buf));
1117 ld(host_index)[MAX_LOG_DEV].tsb.dev_status = 0;
1119 for (retries = 0; retries < 3; retries++)
1121 /*fill scb with get_pos_info command */
1122 scb->command = IM_GET_POS_INFO_CMD;
1123 scb->enable = IM_READ_CONTROL | IM_REPORT_TSB_ONLY_ON_ERROR | IM_RETRY_ENABLE | IM_BYPASS_BUFFER | IM_SUPRESS_EXCEPTION_SHORT;
1124 last_scsi_command(host_index)[MAX_LOG_DEV] = IM_GET_POS_INFO_CMD;
1125 last_scsi_type(host_index)[MAX_LOG_DEV] = IM_SCB;
1127 scb->sys_buf_adr = virt_to_bus(buf);
1128 if (special(host_index)==IBM_SCSI2_FW)
1129 scb->sys_buf_length = 256; /* get all info from F/W adapter */
1130 else
1131 scb->sys_buf_length = 18; /* get exactly 18 bytes for other SCSI */
1133 scb->tsb_adr = virt_to_bus(tsb);
1135 /*issue scb to ldn=15, and busy wait for interrupt */
1136 got_interrupt(host_index) = 0;
1137 issue_cmd (host_index, virt_to_bus(scb), IM_SCB | MAX_LOG_DEV);
1138 while (!got_interrupt(host_index))
1139 barrier ();
1141 /*if got POS-stuff, get block length and return one device found */
1142 if ((stat_result(host_index) == IM_SCB_CMD_COMPLETED)||
1143 (stat_result(host_index) == IM_SCB_CMD_COMPLETED_WITH_RETRIES))
1144 return 1;
1146 /* if all three retries failed, return "no device at this ldn" */
1147 if (retries >= 3)
1148 return 0;
1149 else
1150 return 1;
1153 /* SCSI-immediate-command for assign. This functions maps/unmaps specific
1154 ldn-numbers on SCSI (PUN,LUN). It is needed for presetting of the
1155 subsystem and for dynamical remapping od ldns. */
1156 static int immediate_assign(int host_index, unsigned int pun,
1157 unsigned int lun, unsigned int ldn,
1158 unsigned int operation)
1160 int retries;
1161 unsigned long imm_command;
1163 for (retries=0; retries<3; retries ++)
1165 /* select mutation level of the SCSI-adapter */
1166 switch (special(host_index))
1168 case IBM_SCSI2_FW:
1169 imm_command = (unsigned long)(IM_ASSIGN_IMM_CMD);
1170 imm_command |= (unsigned long)((lun & 7) << 24);
1171 imm_command |= (unsigned long)((operation & 1) << 23);
1172 imm_command |= (unsigned long)((pun & 7)<< 20)|((pun & 8)<< 24);
1173 imm_command |= (unsigned long)((ldn & 15) << 16);
1174 break;
1175 default:
1176 imm_command = inl(IM_CMD_REG(host_index));
1177 imm_command &= (unsigned long)(0xF8000000); /* keep reserved bits */
1178 imm_command |= (unsigned long)(IM_ASSIGN_IMM_CMD);
1179 imm_command |= (unsigned long)((lun & 7) << 24);
1180 imm_command |= (unsigned long)((operation & 1) << 23);
1181 imm_command |= (unsigned long)((pun & 7) << 20);
1182 imm_command |= (unsigned long)((ldn & 15) << 16);
1183 break;
1185 last_scsi_command(host_index)[MAX_LOG_DEV] = IM_ASSIGN_IMM_CMD;
1186 last_scsi_type(host_index)[MAX_LOG_DEV] = IM_IMM_CMD;
1187 got_interrupt(host_index) = 0;
1188 issue_cmd (host_index, (unsigned long)(imm_command), IM_IMM_CMD | MAX_LOG_DEV);
1189 while (!got_interrupt(host_index))
1190 barrier ();
1192 /*if command succesful, break */
1193 if (stat_result(host_index) == IM_IMMEDIATE_CMD_COMPLETED)
1194 return 1;
1196 if (retries >= 3)
1197 return 0;
1198 else
1199 return 1;
1202 static int immediate_feature(int host_index, unsigned int speed,
1203 unsigned int timeout)
1205 int retries;
1206 unsigned long imm_command;
1208 for (retries=0; retries<3; retries ++)
1210 /* select mutation level of the SCSI-adapter */
1211 switch (special(host_index))
1213 default:
1214 imm_command = IM_FEATURE_CTR_IMM_CMD;
1215 imm_command |= (unsigned long)((speed & 0x7) << 29);
1216 imm_command |= (unsigned long)((timeout & 0x1fff) << 16);
1217 break;
1219 last_scsi_command(host_index)[MAX_LOG_DEV] = IM_FEATURE_CTR_IMM_CMD;
1220 last_scsi_type(host_index)[MAX_LOG_DEV] = IM_IMM_CMD;
1221 got_interrupt(host_index) = 0;
1222 /* we need to run into command errors in order to probe for the
1223 * right speed! */
1224 global_command_error_excuse = 1;
1225 issue_cmd (host_index, (unsigned long)(imm_command), IM_IMM_CMD | MAX_LOG_DEV);
1226 while (!got_interrupt(host_index))
1227 barrier ();
1228 if (global_command_error_excuse == CMD_FAIL)
1230 global_command_error_excuse = 0;
1231 return 2;
1233 else
1234 global_command_error_excuse = 0;
1236 /*if command succesful, break */
1237 if (stat_result(host_index) == IM_IMMEDIATE_CMD_COMPLETED)
1238 return 1;
1241 if (retries >= 3)
1242 return 0;
1243 else
1244 return 1;
1247 #ifdef CONFIG_IBMMCA_SCSI_DEV_RESET
1248 static int immediate_reset(int host_index, unsigned int ldn)
1250 int retries;
1251 int ticks;
1252 unsigned long imm_command;
1254 for (retries=0; retries<3; retries ++)
1256 imm_command = inl(IM_CMD_REG(host_index));
1257 imm_command &= (unsigned long)(0xFFFF0000); /* keep reserved bits */
1258 imm_command |= (unsigned long)(IM_RESET_IMM_CMD);
1259 last_scsi_command(host_index)[ldn] = IM_RESET_IMM_CMD;
1260 last_scsi_type(host_index)[ldn] = IM_IMM_CMD;
1262 got_interrupt(host_index) = 0;
1263 reset_status(host_index) = IM_RESET_IN_PROGRESS;
1264 issue_cmd (host_index, (unsigned long)(imm_command), IM_IMM_CMD | ldn);
1265 ticks = IM_RESET_DELAY*HZ;
1266 while (reset_status(host_index) == IM_RESET_IN_PROGRESS && --ticks)
1268 mdelay(1+999/HZ);
1269 barrier();
1271 /* if reset did not complete, just claim */
1272 if (!ticks)
1274 printk("IBM MCA SCSI: reset did not complete within %d seconds.\n",
1275 IM_RESET_DELAY);
1276 reset_status(host_index) = IM_RESET_FINISHED_OK;
1277 /* did not work, finish */
1278 return 1;
1280 /*if command succesful, break */
1281 if (stat_result(host_index) == IM_IMMEDIATE_CMD_COMPLETED)
1282 return 1;
1285 if (retries >= 3)
1286 return 0;
1287 else
1288 return 1;
1290 #endif
1292 /* type-interpreter for physical device numbers */
1293 static char *ti_p(int value)
1295 switch (value)
1297 case TYPE_IBM_SCSI_ADAPTER: return("A"); break;
1298 case TYPE_DISK: return("D"); break;
1299 case TYPE_TAPE: return("T"); break;
1300 case TYPE_PROCESSOR: return("P"); break;
1301 case TYPE_WORM: return("W"); break;
1302 case TYPE_ROM: return("R"); break;
1303 case TYPE_SCANNER: return("S"); break;
1304 case TYPE_MOD: return("M"); break;
1305 case TYPE_MEDIUM_CHANGER: return("C"); break;
1306 case TYPE_NO_LUN: return("+"); break; /* show NO_LUN */
1307 case TYPE_NO_DEVICE:
1308 default: return("-"); break;
1310 return("-");
1313 /* interpreter for logical device numbers (ldn) */
1314 static char *ti_l(int value)
1316 const char hex[16] = "0123456789abcdef";
1317 static char answer[2];
1319 answer[1] = (char)(0x0);
1320 if (value<=MAX_LOG_DEV)
1321 answer[0] = hex[value];
1322 else
1323 answer[0] = '-';
1325 return (char *)&answer;
1328 /* transfers bitpattern of the feature command to values in MHz */
1329 static char *ibmrate(unsigned int speed, int adaptertype)
1331 int i;
1332 i=adaptertype;
1333 switch (speed)
1335 case 0: if (i) return "5.00"; else return "10.00"; break;
1336 case 1: if (i) return "4.00"; else return "8.00"; break;
1337 case 2: if (i) return "3.33"; else return "6.66"; break;
1338 case 3: if (i) return "2.86"; else return "5.00"; break;
1339 case 4: if (i) return "2.50"; else return "4.00"; break;
1340 case 5: if (i) return "2.22"; else return "3.10"; break;
1341 case 6: if (i) return "2.00"; else return "2.50"; break;
1342 case 7: if (i) return "1.82"; else return "2.00"; break;
1344 return "---";
1347 static int probe_display(int what)
1349 static int rotator = 0;
1350 const char rotor[] = "|/-\\";
1352 if (!(display_mode & LED_DISP))
1353 return 0;
1354 if (!what)
1356 outl(0x20202020,MOD95_LED_PORT);
1357 outl(0x20202020,MOD95_LED_PORT+4);
1359 else
1361 outb('S',MOD95_LED_PORT+7);
1362 outb('C',MOD95_LED_PORT+6);
1363 outb('S',MOD95_LED_PORT+5);
1364 outb('I',MOD95_LED_PORT+4);
1365 outb('i',MOD95_LED_PORT+3);
1366 outb('n',MOD95_LED_PORT+2);
1367 outb('i',MOD95_LED_PORT+1);
1368 outb((char)(rotor[rotator]),MOD95_LED_PORT);
1369 rotator++;
1370 if (rotator>3)
1371 rotator=0;
1373 return 0;
1376 static int probe_bus_mode(int host_index)
1378 struct im_pos_info *info;
1379 int num_bus = 0;
1380 int ldn;
1382 info = (struct im_pos_info *)(&(ld(host_index)[MAX_LOG_DEV].buf));
1384 if (get_pos_info(host_index))
1386 if (info->connector_size & 0xf000)
1387 subsystem_connector_size(host_index)=16;
1388 else
1389 subsystem_connector_size(host_index)=32;
1390 num_bus |= (info->pos_4b & 8) >> 3;
1391 for (ldn=0; ldn<=MAX_LOG_DEV; ldn++)
1393 if ((special(host_index)==IBM_SCSI_WCACHE)||
1394 (special(host_index)==IBM_7568_WCACHE))
1396 if (!((info->cache_stat >> ldn) & 1))
1397 ld(host_index)[ldn].cache_flag = 0;
1399 if (!((info->retry_stat >> ldn) & 1))
1400 ld(host_index)[ldn].retry_flag = 0;
1402 #ifdef IM_DEBUG_PROBE
1403 printk("IBM MCA SCSI: SCSI-Cache bits: ");
1404 for (ldn=0; ldn<=MAX_LOG_DEV; ldn++)
1406 printk("%d",ld(host_index)[ldn].cache_flag);
1408 printk("\nIBM MCA SCSI: SCSI-Retry bits: ");
1409 for (ldn=0; ldn<=MAX_LOG_DEV; ldn++)
1411 printk("%d",ld(host_index)[ldn].retry_flag);
1413 printk("\n");
1414 #endif
1416 return num_bus;
1420 The following routine probes the SCSI-devices in four steps:
1421 1. The current ldn -> pun,lun mapping is removed on the SCSI-adapter.
1422 2. ldn 0 is used to go through all possible combinations of pun,lun and
1423 a device_inquiry is done to fiddle out whether there is a device
1424 responding or not. This physical map is stored in get_scsi[][].
1425 3. The 15 available ldns (0-14) are mapped to existing pun,lun.
1426 If there are more devices than ldns, it stops at 14 for the boot
1427 time. Dynamical remapping will be done in ibmmca_queuecommand.
1428 4. If there are less than 15 valid pun,lun, the remaining ldns are
1429 mapped to NON-existing pun,lun to satisfy the adapter. Information
1430 about pun,lun -> ldn is stored as before in get_ldn[][].
1431 This method leads to the result, that the SCSI-pun,lun shown to Linux
1432 mid-level- and higher-level-drivers is exactly corresponding to the
1433 physical reality on the SCSI-bus. Therefore, it is possible that users
1434 of older releases of this driver have to rewrite their fstab-file, because
1435 the /dev/sdXXX could have changed due to the right pun,lun report, now.
1436 The assignment of ALL ldns avoids dynamical remapping by the adapter
1437 itself.
1439 static void check_devices (int host_index, int adaptertype)
1441 int id, lun, ldn, ticks;
1442 int count_devices; /* local counter for connected device */
1443 int max_pun;
1444 int num_bus;
1445 int speedrun; /* local adapter_speed check variable */
1447 /* assign default values to certain variables */
1448 ticks = 0;
1449 count_devices = 0;
1450 IBM_DS(host_index).dyn_flag = 0; /* normally no need for dynamical ldn management */
1451 IBM_DS(host_index).total_errors = 0; /* set errorcounter to 0 */
1452 next_ldn(host_index) = 7; /* next ldn to be assigned is 7, because 0-6 is 'hardwired'*/
1454 /* initialize the very important driver-informational arrays/structs */
1455 memset (ld(host_index), 0,
1456 sizeof(ld(host_index)));
1457 for (ldn=0; ldn<=MAX_LOG_DEV; ldn++)
1459 last_scsi_command(host_index)[ldn] = NO_SCSI; /* emptify last SCSI-command storage */
1460 last_scsi_type(host_index)[ldn] = 0;
1461 ld(host_index)[ldn].cache_flag = 1;
1462 ld(host_index)[ldn].retry_flag = 1;
1464 memset (get_ldn(host_index), TYPE_NO_DEVICE,
1465 sizeof(get_ldn(host_index))); /* this is essential ! */
1466 memset (get_scsi(host_index), TYPE_NO_DEVICE,
1467 sizeof(get_scsi(host_index))); /* this is essential ! */
1469 for (lun=0; lun<8; lun++) /* mark the adapter at its pun on all luns*/
1471 get_scsi(host_index)[subsystem_pun(host_index)][lun] = TYPE_IBM_SCSI_ADAPTER;
1472 get_ldn(host_index)[subsystem_pun(host_index)][lun] = MAX_LOG_DEV; /* make sure, the subsystem
1473 ldn is active for all
1474 luns. */
1477 probe_display(0); /* Supercool display usage during SCSI-probing. */
1478 /* This makes sense, when booting without any */
1479 /* monitor connected on model XX95. */
1481 /* STEP 1: */
1482 adapter_speed(host_index) = global_adapter_speed;
1483 speedrun = adapter_speed(host_index);
1484 while (immediate_feature(host_index,speedrun,adapter_timeout)==2)
1486 probe_display(1);
1487 if (speedrun==7)
1488 panic("IBM MCA SCSI: Cannot set Synchronous-Transfer-Rate!\n");
1489 speedrun++;
1490 if (speedrun>7)
1491 speedrun=7;
1493 adapter_speed(host_index) = speedrun;
1494 /* Get detailed information about the current adapter, necessary for
1495 * device operations: */
1496 num_bus=probe_bus_mode(host_index);
1498 /* num_bus contains only valid data for the F/W adapter! */
1499 if (adaptertype==IBM_SCSI2_FW) /* F/W SCSI adapter: */
1501 /* F/W adapter PUN-space extension evaluation: */
1502 if (num_bus)
1504 printk("IBM MCA SCSI: Seperate bus mode (wide-addressing enabled)\n");
1505 subsystem_maxid(host_index) = 16;
1507 else
1509 printk("IBM MCA SCSI: Combined bus mode (wide-addressing disabled)\n");
1510 subsystem_maxid(host_index) = 8;
1512 printk("IBM MCA SCSI: Sync.-Rate (F/W: 20, Int.: 10, Ext.: %s) MBytes/s\n",
1513 ibmrate(speedrun,adaptertype));
1515 else /* all other IBM SCSI adapters: */
1516 printk("IBM MCA SCSI: Synchronous-SCSI-Transfer-Rate: %s MBytes/s\n",
1517 ibmrate(speedrun,adaptertype));
1519 /* assign correct PUN device space */
1520 max_pun = subsystem_maxid(host_index);
1522 #ifdef IM_DEBUG_PROBE
1523 printk("IBM MCA SCSI: Current SCSI-host index: %d\n",host_index);
1524 #endif
1525 printk("IBM MCA SCSI: Removing default logical SCSI-device mapping.");
1526 for (ldn=0; ldn<MAX_LOG_DEV; ldn++)
1528 probe_display(1);
1529 #ifdef IM_DEBUG_PROBE
1530 printk(".");
1531 #endif
1532 immediate_assign(host_index,0,0,ldn,REMOVE_LDN); /* remove ldn (wherever)*/
1535 lun = 0; /* default lun is 0 */
1537 /* STEP 2: */
1538 printk("\nIBM MCA SCSI: Probing SCSI-devices");
1539 #ifndef IM_DEBUG_PROBE
1540 printk(" (this can take up to 2 minutes)");
1541 #endif
1542 printk(".");
1544 for (id=0; id<max_pun ; id++)
1545 #ifdef CONFIG_SCSI_MULTI_LUN
1546 for (lun=0; lun<8; lun++)
1547 #endif
1549 probe_display(1);
1550 #ifdef IM_DEBUG_PROBE
1551 printk(".");
1552 #endif
1553 if (id != subsystem_pun(host_index))
1554 { /* if pun is not the adapter: */
1555 /*set ldn=0 to pun,lun*/
1556 immediate_assign(host_index,id,lun,PROBE_LDN,SET_LDN);
1557 if (device_inquiry(host_index, PROBE_LDN)) /* probe device */
1559 get_scsi(host_index)[id][lun]=
1560 (unsigned char)(ld(host_index)[PROBE_LDN].buf[0]);
1561 /* entry, even for NO_LUN */
1562 if (ld(host_index)[PROBE_LDN].buf[0] != TYPE_NO_LUN)
1563 count_devices++; /* a existing device is found */
1565 /* remove ldn */
1566 immediate_assign(host_index,id,lun,PROBE_LDN,REMOVE_LDN);
1570 /* STEP 3: */
1571 printk("\nIBM MCA SCSI: Mapping SCSI-devices.");
1573 ldn = 0;
1574 lun = 0;
1576 #ifdef CONFIG_SCSI_MULTI_LUN
1577 for (lun=0; lun<8 && ldn<MAX_LOG_DEV; lun++)
1578 #endif
1579 for (id=0; id<max_pun && ldn<MAX_LOG_DEV; id++)
1581 probe_display(1);
1582 #ifdef IM_DEBUG_PROBE
1583 printk(".");
1584 #endif
1585 if (id != subsystem_pun(host_index))
1587 if (get_scsi(host_index)[id][lun] != TYPE_NO_LUN &&
1588 get_scsi(host_index)[id][lun] != TYPE_NO_DEVICE)
1590 /* Only map if accepted type. Always enter for
1591 lun == 0 to get no gaps into ldn-mapping for ldn<7. */
1592 immediate_assign(host_index,id,lun,ldn,SET_LDN);
1593 get_ldn(host_index)[id][lun]=ldn; /* map ldn */
1594 if (device_exists (host_index, ldn,
1595 &ld(host_index)[ldn].block_length,
1596 &ld(host_index)[ldn].device_type))
1598 #ifdef CONFIG_IBMMCA_SCSI_DEV_RESET
1599 printk("resetting device at ldn=%x ... ",ldn);
1600 immediate_reset(host_index,ldn);
1601 #endif
1602 ldn++;
1604 else
1606 /* device vanished, probably because we don't know how to
1607 * handle it or because it has problems */
1608 if (lun > 0)
1610 /* remove mapping */
1611 get_ldn(host_index)[id][lun]=TYPE_NO_DEVICE;
1612 immediate_assign(host_index,0,0,ldn,REMOVE_LDN);
1614 else ldn++;
1617 else if (lun == 0)
1619 /* map lun == 0, even if no device exists */
1620 immediate_assign(host_index,id,lun,ldn,SET_LDN);
1621 get_ldn(host_index)[id][lun]=ldn; /* map ldn */
1622 ldn++;
1627 /* STEP 4: */
1629 /* map remaining ldns to non-existing devices */
1630 for (lun=1; lun<8 && ldn<MAX_LOG_DEV; lun++)
1631 for (id=0; id<max_pun && ldn<MAX_LOG_DEV; id++)
1633 if (get_scsi(host_index)[id][lun] == TYPE_NO_LUN ||
1634 get_scsi(host_index)[id][lun] == TYPE_NO_DEVICE)
1636 probe_display(1);
1637 /* Map remaining ldns only to NON-existing pun,lun
1638 combinations to make sure an inquiry will fail.
1639 For MULTI_LUN, it is needed to avoid adapter autonome
1640 SCSI-remapping. */
1641 immediate_assign(host_index,id,lun,ldn,SET_LDN);
1642 get_ldn(host_index)[id][lun]=ldn;
1643 ldn++;
1647 printk("\n");
1648 if (ibm_ansi_order)
1649 printk("IBM MCA SCSI: Device order: IBM/ANSI (pun=7 is first).\n");
1650 else
1651 printk("IBM MCA SCSI: Device order: New Industry Standard (pun=0 is first).\n");
1653 #ifdef IM_DEBUG_PROBE
1654 /* Show the physical and logical mapping during boot. */
1655 printk("IBM MCA SCSI: Determined SCSI-device-mapping:\n");
1656 printk(" Physical SCSI-Device Map Logical SCSI-Device Map\n");
1657 printk("ID\\LUN 0 1 2 3 4 5 6 7 ID\\LUN 0 1 2 3 4 5 6 7\n");
1658 for (id=0; id<max_pun; id++)
1660 printk("%2d ",id);
1661 for (lun=0; lun<8; lun++)
1662 printk("%2s ",ti_p(get_scsi(host_index)[id][lun]));
1663 printk(" %2d ",id);
1664 for (lun=0; lun<8; lun++)
1665 printk("%2s ",ti_l(get_ldn(host_index)[id][lun]));
1666 printk("\n");
1668 #endif
1670 /* assign total number of found SCSI-devices to the statistics struct */
1671 IBM_DS(host_index).total_scsi_devices = count_devices;
1673 /* decide for output in /proc-filesystem, if the configuration of
1674 SCSI-devices makes dynamical reassignment of devices necessary */
1675 if (count_devices>=MAX_LOG_DEV)
1676 IBM_DS(host_index).dyn_flag = 1; /* dynamical assignment is necessary */
1677 else
1678 IBM_DS(host_index).dyn_flag = 0; /* dynamical assignment is not necessary */
1680 /* If no SCSI-devices are assigned, return 1 in order to cause message. */
1681 if (ldn == 0)
1682 printk("IBM MCA SCSI: Warning: No SCSI-devices found/assigned!\n");
1684 /* reset the counters for statistics on the current adapter */
1685 IBM_DS(host_index).scbs = 0;
1686 IBM_DS(host_index).long_scbs = 0;
1687 IBM_DS(host_index).total_accesses = 0;
1688 IBM_DS(host_index).total_interrupts = 0;
1689 IBM_DS(host_index).dynamical_assignments = 0;
1690 memset (IBM_DS(host_index).ldn_access, 0x0,
1691 sizeof (IBM_DS(host_index).ldn_access));
1692 memset (IBM_DS(host_index).ldn_read_access, 0x0,
1693 sizeof (IBM_DS(host_index).ldn_read_access));
1694 memset (IBM_DS(host_index).ldn_write_access, 0x0,
1695 sizeof (IBM_DS(host_index).ldn_write_access));
1696 memset (IBM_DS(host_index).ldn_inquiry_access, 0x0,
1697 sizeof (IBM_DS(host_index).ldn_inquiry_access));
1698 memset (IBM_DS(host_index).ldn_modeselect_access, 0x0,
1699 sizeof (IBM_DS(host_index).ldn_modeselect_access));
1700 memset (IBM_DS(host_index).ldn_assignments, 0x0,
1701 sizeof (IBM_DS(host_index).ldn_assignments));
1702 probe_display(0);
1703 return;
1706 /*--------------------------------------------------------------------*/
1708 static int device_exists (int host_index, int ldn, int *block_length,
1709 int *device_type)
1711 unsigned char *buf;
1713 /* if no valid device found, return immediately with 0 */
1714 if (!(device_inquiry(host_index, ldn)))
1715 return 0;
1717 buf = (unsigned char *)(&(ld(host_index)[ldn].buf));
1719 /*if device is CD_ROM, assume block size 2048 and return */
1720 if (*buf == TYPE_ROM)
1722 *device_type = TYPE_ROM;
1723 *block_length = 2048; /* (standard blocksize for yellow-/red-book) */
1724 return 1;
1727 if (*buf == TYPE_WORM) /* CD-burner, WORM, Linux handles this as CD-ROM
1728 therefore, the block_length is also 2048. */
1730 *device_type = TYPE_WORM;
1731 *block_length = 2048;
1732 return 1;
1735 /* if device is disk, use "read capacity" to find its block size */
1736 if (*buf == TYPE_DISK)
1738 *device_type = TYPE_DISK;
1739 if (read_capacity( host_index, ldn))
1741 *block_length = *(buf+7) + (*(buf+6) << 8) +
1742 (*(buf+5) << 16) + (*(buf+4) << 24);
1743 return 1;
1745 else
1746 return 0;
1749 /* if this is a magneto-optical drive, treat it like a harddisk */
1750 if (*buf == TYPE_MOD)
1752 *device_type = TYPE_MOD;
1753 if (read_capacity( host_index, ldn))
1755 *block_length = *(buf+7) + (*(buf+6) << 8) +
1756 (*(buf+5) << 16) + (*(buf+4) << 24);
1757 return 1;
1759 else
1760 return 0;
1763 if (*buf == TYPE_TAPE) /* TAPE-device found */
1765 *device_type = TYPE_TAPE;
1766 *block_length = 0; /* not in use (setting by mt and mtst in op.) */
1767 return 1;
1770 if (*buf == TYPE_PROCESSOR) /* HP-Scanners, diverse SCSI-processing units*/
1772 *device_type = TYPE_PROCESSOR;
1773 *block_length = 0; /* they set their stuff on drivers */
1774 return 1;
1777 if (*buf == TYPE_SCANNER) /* other SCSI-scanners */
1779 *device_type = TYPE_SCANNER;
1780 *block_length = 0; /* they set their stuff on drivers */
1781 return 1;
1784 if (*buf == TYPE_MEDIUM_CHANGER) /* Medium-Changer */
1786 *device_type = TYPE_MEDIUM_CHANGER;
1787 *block_length = 0; /* One never knows, what to expect on a medium
1788 changer device. */
1789 return 1;
1792 /* Up to now, no SCSI-devices that are known up to kernel 2.1.31 are
1793 ignored! MO-drives are now supported and treated as harddisk. */
1794 return 0;
1797 /*--------------------------------------------------------------------*/
1799 #ifdef CONFIG_SCSI_IBMMCA
1801 void internal_ibmmca_scsi_setup (char *str, int *ints)
1803 int i, j, io_base, id_base;
1804 char *token;
1806 io_base = 0;
1807 id_base = 0;
1809 if (str)
1811 token = strtok(str,",");
1812 j = 0;
1813 while (token)
1815 if (!strcmp(token,"activity"))
1816 display_mode |= LED_ACTIVITY;
1817 if (!strcmp(token,"display"))
1818 display_mode |= LED_DISP;
1819 if (!strcmp(token,"adisplay"))
1820 display_mode |= LED_ADISP;
1821 if (!strcmp(token,"bypass"))
1822 bypass_controller = 1;
1823 if (!strcmp(token,"normal"))
1824 ibm_ansi_order = 0;
1825 if (!strcmp(token,"ansi"))
1826 ibm_ansi_order = 1;
1827 if (!strcmp(token,"fast"))
1828 global_adapter_speed = 0;
1829 if (!strcmp(token,"medium"))
1830 global_adapter_speed = 4;
1831 if (!strcmp(token,"slow"))
1832 global_adapter_speed = 7;
1833 if ( (*token == '-') || (isdigit(*token)) )
1835 if (!(j%2) && (io_base < IM_MAX_HOSTS))
1836 io_port[io_base++] = simple_strtoul(token,NULL,0);
1837 if ((j%2) && (id_base < IM_MAX_HOSTS))
1838 scsi_id[id_base++] = simple_strtoul(token,NULL,0);
1839 j++;
1841 token = strtok(NULL,",");
1844 else if (ints)
1846 for (i = 0; i < IM_MAX_HOSTS && 2*i+2 < ints[0]; i++)
1848 io_port[i] = ints[2*i+2];
1849 scsi_id[i] = ints[2*i+2];
1852 return;
1855 #endif
1857 /*--------------------------------------------------------------------*/
1859 static int ibmmca_getinfo (char *buf, int slot, void *dev)
1861 struct Scsi_Host *shpnt;
1862 int len, speciale,connectore;
1863 unsigned int pos2, pos3;
1864 static unsigned long flags;
1866 #ifdef OLDKERN
1867 save_flags(flags);
1868 cli();
1869 #else
1870 spin_lock_irqsave(&info_lock, flags);
1871 #endif
1873 shpnt = dev; /* assign host-structure to local pointer */
1874 len = 0; /* set filled text-buffer index to 0 */
1875 /* get the _special contents of the hostdata structure */
1876 speciale = ((struct ibmmca_hostdata *)shpnt->hostdata)->_special;
1877 connectore = ((struct ibmmca_hostdata *)shpnt->hostdata)->_connector_size;
1878 pos2 = ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos2;
1879 pos3 = ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos3;
1881 if (speciale == FORCED_DETECTION) /* forced detection */
1883 len += sprintf (buf + len, "Adapter category: forced detected\n");
1884 len += sprintf(buf + len, "***************************************\n");
1885 len += sprintf(buf + len, "*** Forced detected SCSI Adapter ***\n");
1886 len += sprintf(buf + len, "*** No chip-information available ***\n");
1887 len += sprintf(buf + len, "***************************************\n");
1889 else if (speciale == INTEGRATED_SCSI)
1890 { /* if the integrated subsystem has been found automatically: */
1891 len += sprintf (buf + len, "Adapter category: integrated\n");
1892 len += sprintf (buf + len, "Chip revision level: %d\n",
1893 ((pos2 & 0xf0) >> 4));
1894 len += sprintf (buf + len, "Chip status: %s\n",
1895 (pos2 & 1) ? "enabled" : "disabled");
1896 len += sprintf (buf + len, "8 kByte NVRAM status: %s\n",
1897 (pos2 & 2) ? "locked" : "accessible");
1899 else if ((speciale>=0)&&
1900 (speciale<(sizeof(subsys_list)/sizeof(struct subsys_list_struct))))
1901 { /* if the subsystem is a slot adapter */
1902 len += sprintf (buf + len, "Adapter category: slot-card\n");
1903 len += sprintf (buf + len, "ROM Segment Address: ");
1904 if ((pos2 & 0xf0) == 0xf0)
1905 len += sprintf (buf + len, "off\n");
1906 else
1907 len += sprintf (buf + len, "0x%x\n",
1908 ((pos2 & 0xf0) << 13) + 0xc0000);
1909 len += sprintf (buf + len, "Chip status: %s\n",
1910 (pos2 & 1) ? "enabled" : "disabled");
1911 len += sprintf (buf + len, "Adapter I/O Offset: 0x%x\n",
1912 ((pos2 & 0x0e) << 2));
1914 else
1916 len += sprintf (buf + len, "Adapter category: unknown\n");
1918 /* common subsystem information to write to the slotn file */
1919 len += sprintf (buf + len, "Subsystem PUN: %d\n", shpnt->this_id);
1920 len += sprintf (buf + len, "I/O base address range: 0x%x-0x%x\n",
1921 (unsigned int)(shpnt->io_port),
1922 (unsigned int)(shpnt->io_port+7));
1923 len += sprintf (buf + len, "MCA-slot size: %d bits",connectore);
1924 /* Now make sure, the bufferlength is devidable by 4 to avoid
1925 * paging problems of the buffer. */
1926 while ( len % sizeof( int ) != ( sizeof ( int ) - 1 ) )
1928 len += sprintf (buf + len, " ");
1930 len += sprintf (buf + len, "\n");
1932 #ifdef OLDKERN
1933 restore_flags(flags);
1934 #else
1935 spin_unlock_irqrestore(&info_lock, flags);
1936 #endif
1937 return len;
1940 int ibmmca_detect (Scsi_Host_Template * scsi_template)
1942 struct Scsi_Host *shpnt;
1943 int port, id, i, j, list_size, slot;
1944 int devices_on_irq_11 = 0;
1945 int devices_on_irq_14 = 0;
1946 int IRQ14_registered = 0;
1947 int IRQ11_registered = 0;
1949 found = 0; /* make absolutely sure, that found is set to 0 */
1951 /* First of all, print the version number of the driver. This is
1952 * important to allow better user bugreports in case of already
1953 * having problems with the MCA_bus probing. */
1954 printk("IBM MCA SCSI: Version %s\n",IBMMCA_SCSI_DRIVER_VERSION);
1955 /* if this is not MCA machine, return "nothing found" */
1956 if (!MCA_bus)
1958 printk("IBM MCA SCSI: No Microchannel-bus present --> Aborting.\n");
1959 printk(" This machine does not have any IBM MCA-bus\n");
1960 printk(" or the MCA-Kernel-support is not enabled!\n");
1961 return 0;
1964 #ifdef MODULE
1965 /* If the driver is run as module, read from conf.modules or cmd-line */
1966 if (boot_options)
1967 option_setup(boot_options);
1968 #endif
1970 /* get interrupt request level */
1971 #ifdef OLDKERN
1972 if (request_irq (IM_IRQ, interrupt_handler, SA_SHIRQ, "ibmmcascsi",
1973 hosts))
1974 #else
1975 if (request_irq (IM_IRQ, do_interrupt_handler, SA_SHIRQ, "ibmmcascsi",
1976 hosts))
1977 #endif
1979 printk("IBM MCA SCSI: Unable to get shared IRQ %d.\n", IM_IRQ);
1980 return 0;
1982 else
1983 IRQ14_registered++;
1985 /* if ibmmcascsi setup option was passed to kernel, return "found" */
1986 for (i = 0; i < IM_MAX_HOSTS; i++)
1987 if (io_port[i] > 0 && scsi_id[i] >= 0 && scsi_id[i] < 8)
1989 printk("IBM MCA SCSI: forced detected SCSI Adapter, io=0x%x, scsi id=%d.\n",
1990 io_port[i], scsi_id[i]);
1991 if ((shpnt = ibmmca_register(scsi_template, io_port[i], scsi_id[i],
1992 FORCED_DETECTION,
1993 "forced detected SCSI Adapter")))
1995 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos2 = 0;
1996 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos3 = 0;
1997 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos4 = 0;
1998 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos5 = 0;
1999 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos6 = 0;
2000 ((struct ibmmca_hostdata *)shpnt->hostdata)->_special =
2001 FORCED_DETECTION;
2002 mca_set_adapter_name(MCA_INTEGSCSI, "forced detected SCSI Adapter");
2003 mca_set_adapter_procfn(MCA_INTEGSCSI, (MCA_ProcFn) ibmmca_getinfo,
2004 shpnt);
2005 mca_mark_as_used(MCA_INTEGSCSI);
2006 devices_on_irq_14++;
2009 if (found) return found;
2011 /* The POS2-register of all PS/2 model SCSI-subsystems has the following
2012 * interpretation of bits:
2013 * Bit 7 - 4 : Chip Revision ID (Release)
2014 * Bit 3 - 2 : Reserved
2015 * Bit 1 : 8k NVRAM Disabled
2016 * Bit 0 : Chip Enable (EN-Signal)
2017 * The POS3-register is interpreted as follows:
2018 * Bit 7 - 5 : SCSI ID
2019 * Bit 4 : Reserved = 0
2020 * Bit 3 - 0 : Reserved = 0
2021 * (taken from "IBM, PS/2 Hardware Interface Technical Reference, Common
2022 * Interfaces (1991)").
2023 * In short words, this means, that IBM PS/2 machines only support
2024 * 1 single subsystem by default. The slot-adapters must have another
2025 * configuration on pos2. Here, one has to assume the following
2026 * things for POS2-register:
2027 * Bit 7 - 4 : Chip Revision ID (Release)
2028 * Bit 3 - 1 : port offset factor
2029 * Bit 0 : Chip Enable (EN-Signal)
2030 * As I found a patch here, setting the IO-registers to 0x3540 forced,
2031 * as there was a 0x05 in POS2 on a model 56, I assume, that the
2032 * port 0x3540 must be fix for integrated SCSI-controllers.
2033 * Ok, this discovery leads to the following implementation: (M.Lang) */
2035 /* first look for the IBM SCSI integrated subsystem on the motherboard */
2036 for (j=0;j<8;j++) /* read the pos-information */
2037 pos[j] = mca_read_stored_pos(MCA_INTEGSCSI,j);
2038 /* pos2 = pos3 = 0xff if there is no integrated SCSI-subsystem present */
2039 /* if (( pos[2] != 0xff) || (pos[3] != 0xff )) */
2040 /* Previous if-arguments do fail! Therefore, we use now the following to
2041 * make sure, we see a real integrated onboard SCSI-interface: */
2042 if ((!pos[0] && !pos[1] && pos[2]>0 && pos[3]>0 && !pos[4] && !pos[5] && !pos[6] && !pos[7]) ||
2043 (pos[0]==0xff && pos[1]==0xff && pos[2]<0xff && pos[3]<0xff && pos[4]==0xff && pos[5]==0xff && pos[6]==0xff && pos[7]==0xff))
2045 if ((pos[2] & 1) == 1) /* is the subsystem chip enabled ? */
2046 port = IM_IO_PORT;
2047 else
2048 { /* if disabled, no IRQs will be generated, as the chip won't
2049 * listen to the incomming commands and will do really nothing,
2050 * except for listening to the pos-register settings. If this
2051 * happens, I need to hugely think about it, as one has to
2052 * write something to the MCA-Bus pos register in order to
2053 * enable the chip. Normally, IBM-SCSI won't pass the POST,
2054 * when the chip is disabled (see IBM tech. ref.). */
2055 port = IM_IO_PORT; /* anyway, set the portnumber and warn */
2056 printk("IBM MCA SCSI: WARNING - Your SCSI-subsystem is disabled!\n");
2057 printk(" SCSI-operations may not work.\n");
2059 id = (pos[3] & 0xe0) >> 5; /* this is correct and represents the PUN */
2061 /* give detailed information on the subsystem. This helps me
2062 * additionally during debugging and analyzing bug-reports. */
2063 printk("IBM MCA SCSI: IBM Integrated SCSI Controller found, io=0x%x, scsi id=%d,\n",
2064 port, id);
2065 printk(" chip rev.=%d, 8K NVRAM=%s, subsystem=%s\n",
2066 ((pos[2] & 0xf0) >> 4), (pos[2] & 2) ? "locked" : "accessible",
2067 (pos[2] & 1) ? "enabled." : "disabled.");
2069 /* register the found integrated SCSI-subsystem */
2070 if ((shpnt = ibmmca_register(scsi_template, port, id,
2071 INTEGRATED_SCSI,
2072 "IBM Integrated SCSI Controller")))
2074 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos2 = pos[2];
2075 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos3 = pos[3];
2076 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos4 = pos[4];
2077 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos5 = pos[5];
2078 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos6 = pos[6];
2079 ((struct ibmmca_hostdata *)shpnt->hostdata)->_special =
2080 INTEGRATED_SCSI;
2081 mca_set_adapter_name(MCA_INTEGSCSI, "IBM Integrated SCSI Controller");
2082 mca_set_adapter_procfn(MCA_INTEGSCSI, (MCA_ProcFn) ibmmca_getinfo,
2083 shpnt);
2084 mca_mark_as_used(MCA_INTEGSCSI);
2085 devices_on_irq_14++;
2089 /* now look for other adapters in MCA slots, */
2090 /* determine the number of known IBM-SCSI-subsystem types */
2091 /* see the pos[2] dependence to get the adapter port-offset. */
2092 list_size = sizeof(subsys_list) / sizeof(struct subsys_list_struct);
2093 for (i = 0; i < list_size; i++)
2094 { /* scan each slot for a fitting adapter id */
2095 slot = 0; /* start at slot 0 */
2096 while ((slot = mca_find_adapter(subsys_list[i].mca_id, slot))
2097 != MCA_NOTFOUND)
2098 { /* scan through all slots */
2099 for (j=0;j<8;j++) /* read the pos-information */
2100 pos[j] = mca_read_stored_pos(slot, j);
2101 if ((pos[2] & 1) == 1) /* is the subsystem chip enabled ? */
2102 { /* (explanations see above) */
2103 port = IM_IO_PORT + ((pos[2] & 0x0e) << 2);
2105 else
2106 { /* anyway, set the portnumber and warn */
2107 port = IM_IO_PORT + ((pos[2] & 0x0e) << 2);
2108 printk("IBM MCA SCSI: WARNING - Your SCSI-subsystem is disabled!\n");
2109 printk(" SCSI-operations may not work.\n");
2111 if ((i==IBM_SCSI2_FW)&&(pos[6]!=0))
2113 printk("IBM MCA SCSI: ERROR - Wrong POS(6)-register setting!\n");
2114 printk(" Impossible to determine adapter PUN!\n");
2115 printk(" Guessing adapter PUN = 7.\n");
2116 id = 7;
2118 else
2120 id = (pos[3] & 0xe0) >> 5; /* get subsystem PUN */
2121 if (i==IBM_SCSI2_FW)
2123 id |= (pos[3] & 0x10) >> 1; /* get subsystem PUN high-bit
2124 * for F/W adapters */
2127 if ((i==IBM_SCSI2_FW)&&(pos[4] & 0x01)&&(pos[6]==0))
2128 { /* IRQ11 is used by SCSI-2 F/W Adapter/A */
2129 printk("IBM MCA SCSI: SCSI-2 F/W adapter needs IRQ 11.\n");
2130 /* get interrupt request level */
2131 #ifdef OLDKERN
2132 if (request_irq (IM_IRQ_FW, interrupt_handler, SA_SHIRQ,
2133 "ibmmcascsi", hosts))
2134 #else
2135 if (request_irq (IM_IRQ_FW, do_interrupt_handler, SA_SHIRQ,
2136 "ibmmcascsi", hosts))
2137 #endif
2139 printk("IBM MCA SCSI: Unable to get shared IRQ %d.\n",
2140 IM_IRQ_FW);
2142 else
2143 IRQ11_registered++;
2145 printk("IBM MCA SCSI: %s found in slot %d, io=0x%x, scsi id=%d,\n",
2146 subsys_list[i].description, slot + 1, port, id);
2147 if ((pos[2] & 0xf0) == 0xf0)
2148 printk(" ROM Addr.=off,");
2149 else
2150 printk(" ROM Addr.=0x%x,",
2151 ((pos[2] & 0xf0) << 13) + 0xc0000);
2152 printk(" port-offset=0x%x, subsystem=%s\n",
2153 ((pos[2] & 0x0e) << 2),
2154 (pos[2] & 1) ? "enabled." : "disabled.");
2156 /* register the hostadapter */
2157 if ((shpnt = ibmmca_register(scsi_template, port, id, i,
2158 subsys_list[i].description)))
2160 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos2 = pos[2];
2161 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos3 = pos[3];
2162 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos4 = pos[4];
2163 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos5 = pos[5];
2164 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos6 = pos[6];
2165 ((struct ibmmca_hostdata *)shpnt->hostdata)->_special = i;
2167 mca_set_adapter_name (slot, subsys_list[i].description);
2168 mca_set_adapter_procfn (slot, (MCA_ProcFn) ibmmca_getinfo,
2169 shpnt);
2170 mca_mark_as_used(slot);
2171 if ((i==IBM_SCSI2_FW)&&(pos[4] & 0x01)&&(pos[6]==0))
2172 devices_on_irq_11++;
2173 else
2174 devices_on_irq_14++;
2176 slot++; /* advance to next slot */
2177 } /* advance to next adapter id in the list of IBM-SCSI-subsystems*/
2181 /* now look for SCSI-adapters, by bugs mapped to the integrated SCSI
2182 * area. E.g. a W/Cache in MCA-slot 9 ???? Arrrrgh!! */
2183 list_size = sizeof(subsys_list) / sizeof(struct subsys_list_struct);
2184 for (i = 0; i < list_size; i++)
2185 { /* scan each slot for a fitting adapter id */
2186 slot = mca_find_adapter(subsys_list[i].mca_id, MCA_INTEGSCSI);
2187 if (slot != MCA_NOTFOUND)
2188 { /* scan through all slots */
2189 for (j=0;j<8;j++) /* read the pos-information */
2190 pos[j] = mca_read_stored_pos(slot, j);
2191 if ((pos[2] & 1) == 1) /* is the subsystem chip enabled ? */
2192 { /* (explanations see above) */
2193 port = IM_IO_PORT + ((pos[2] & 0x0e) << 2);
2195 else
2196 { /* anyway, set the portnumber and warn */
2197 port = IM_IO_PORT + ((pos[2] & 0x0e) << 2);
2198 printk("IBM MCA SCSI: WARNING - Your SCSI-subsystem is disabled!\n");
2199 printk(" SCSI-operations may not work.\n");
2201 if ((i==IBM_SCSI2_FW)&&(pos[6]!=0))
2203 printk("IBM MCA SCSI: ERROR - Wrong POS(6)-register setting!\n");
2204 printk(" Impossible to determine adapter PUN!\n");
2205 printk(" Guessing adapter PUN = 7.\n");
2206 id = 7;
2208 else
2210 id = (pos[3] & 0xe0) >> 5; /* get subsystem PUN */
2211 if (i==IBM_SCSI2_FW)
2213 id |= (pos[3] & 0x10) >> 1; /* get subsystem PUN high-bit
2214 * for F/W adapters */
2217 if ((i==IBM_SCSI2_FW)&&(pos[4] & 0x01)&&(pos[6]==0))
2218 { /* IRQ11 is used by SCSI-2 F/W Adapter/A */
2219 printk("IBM MCA SCSI: SCSI-2 F/W adapter needs IRQ 11.\n");
2220 /* get interrupt request level */
2221 #ifdef OLDKERN
2222 if (request_irq (IM_IRQ_FW, interrupt_handler, SA_SHIRQ,
2223 "ibmmcascsi", hosts))
2224 #else
2225 if (request_irq (IM_IRQ_FW, do_interrupt_handler, SA_SHIRQ,
2226 "ibmmcascsi", hosts))
2227 #endif
2229 printk("IBM MCA SCSI: Unable to get shared IRQ %d.\n",
2230 IM_IRQ_FW);
2232 else
2233 IRQ11_registered++;
2235 printk("IBM MCA SCSI: %s found in slot %d, io=0x%x, scsi id=%d,\n",
2236 subsys_list[i].description, slot + 1, port, id);
2237 if ((pos[2] & 0xf0) == 0xf0)
2238 printk(" ROM Addr.=off,");
2239 else
2240 printk(" ROM Addr.=0x%x,",
2241 ((pos[2] & 0xf0) << 13) + 0xc0000);
2242 printk(" port-offset=0x%x, subsystem=%s\n",
2243 ((pos[2] & 0x0e) << 2),
2244 (pos[2] & 1) ? "enabled." : "disabled.");
2246 /* register the hostadapter */
2247 if ((shpnt = ibmmca_register(scsi_template, port, id, i,
2248 subsys_list[i].description)))
2250 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos2 = pos[2];
2251 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos3 = pos[3];
2252 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos4 = pos[4];
2253 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos5 = pos[5];
2254 ((struct ibmmca_hostdata *)shpnt->hostdata)->_pos6 = pos[6];
2255 ((struct ibmmca_hostdata *)shpnt->hostdata)->_special = i;
2257 mca_set_adapter_name (slot, subsys_list[i].description);
2258 mca_set_adapter_procfn (slot, (MCA_ProcFn) ibmmca_getinfo,
2259 shpnt);
2260 mca_mark_as_used(slot);
2261 if ((i==IBM_SCSI2_FW)&&(pos[4] & 0x01)&&(pos[6]==0))
2262 devices_on_irq_11++;
2263 else
2264 devices_on_irq_14++;
2266 slot++; /* advance to next slot */
2267 } /* advance to next adapter id in the list of IBM-SCSI-subsystems*/
2270 if ( IRQ11_registered && !devices_on_irq_11 )
2271 free_irq(IM_IRQ_FW, hosts); /* no devices on IRQ 11 */
2272 if ( IRQ14_registered && !devices_on_irq_14 )
2273 free_irq(IM_IRQ, hosts); /* no devices on IRQ 14 */
2274 if ( !devices_on_irq_11 && !devices_on_irq_14 )
2275 printk("IBM MCA SCSI: No IBM SCSI-subsystem adapter attached.\n");
2276 return found; /* return the number of found SCSI hosts. Should be 1 or 0. */
2279 static struct Scsi_Host *
2280 ibmmca_register(Scsi_Host_Template * scsi_template, int port, int id,
2281 int adaptertype, char *hostname)
2283 struct Scsi_Host *shpnt;
2284 int i, j;
2285 unsigned int ctrl;
2287 /* check I/O region */
2288 if (check_region(port, IM_N_IO_PORT))
2290 printk("IBM MCA SCSI: Unable to get I/O region 0x%x-0x%x (%d ports).\n",
2291 port, port + IM_N_IO_PORT - 1, IM_N_IO_PORT);
2292 return NULL;
2295 /* register host */
2296 shpnt = scsi_register(scsi_template, sizeof(struct ibmmca_hostdata));
2297 if (!shpnt)
2299 printk("IBM MCA SCSI: Unable to register host.\n");
2300 return NULL;
2303 /* request I/O region */
2304 request_region(port, IM_N_IO_PORT, hostname);
2306 hosts[found] = shpnt; /* add new found hostadapter to the list */
2307 special(found) = adaptertype; /* important assignment or else crash! */
2308 subsystem_connector_size(found) = 0; /* preset slot-size */
2309 shpnt->irq = IM_IRQ; /* assign necessary stuff for the adapter */
2310 shpnt->io_port = port;
2311 shpnt->n_io_port = IM_N_IO_PORT;
2312 shpnt->this_id = id;
2313 shpnt->max_id = 8; /* 8 PUNs are default */
2314 /* now, the SCSI-subsystem is connected to Linux */
2316 ctrl = (unsigned int)(inb(IM_CTR_REG(found))); /* get control-register status */
2317 #ifdef IM_DEBUG_PROBE
2318 printk("IBM MCA SCSI: Control Register contents: %x, status: %x\n",
2319 ctrl,inb(IM_STAT_REG(found)));
2320 printk("IBM MCA SCSI: This adapters' POS-registers: ");
2321 for (i=0;i<8;i++)
2322 printk("%x ",pos[i]);
2323 printk("\n");
2324 if (bypass_controller)
2325 printk("IBM MCA SCSI: Subsystem SCSI-commands get bypassed.\n");
2326 #endif
2328 reset_status(found) = IM_RESET_NOT_IN_PROGRESS;
2330 for (i = 0; i < 16; i++) /* reset the tables */
2331 for (j = 0; j < 8; j++)
2332 get_ldn(found)[i][j] = MAX_LOG_DEV;
2334 /* check which logical devices exist */
2335 /* after this line, local interrupting is possible: */
2336 local_checking_phase_flag(found) = 1;
2337 check_devices(found,adaptertype); /* call by value, using the global variable hosts*/
2338 local_checking_phase_flag(found) = 0;
2340 found++; /* now increase index to be prepared for next found subsystem */
2341 /* an ibm mca subsystem has been detected */
2342 return shpnt;
2345 /*--------------------------------------------------------------------*/
2347 int ibmmca_command (Scsi_Cmnd * cmd)
2349 ibmmca_queuecommand (cmd, internal_done);
2350 cmd->SCp.Status = 0;
2351 while (!cmd->SCp.Status)
2352 barrier ();
2353 return cmd->result;
2356 /*--------------------------------------------------------------------*/
2358 int ibmmca_release(struct Scsi_Host *shpnt)
2360 release_region(shpnt->io_port, shpnt->n_io_port);
2361 if (!(--found))
2362 free_irq(shpnt->irq, hosts);
2363 return 0;
2366 /*--------------------------------------------------------------------*/
2368 /* The following routine is the SCSI command queue. The old edition is
2369 now improved by dynamical reassignment of ldn numbers that are
2370 currently not assigned. The mechanism works in a way, that first
2371 the physical structure is checked. If at a certain pun,lun a device
2372 should be present, the routine proceeds to the ldn check from
2373 get_ldn. An answer of 0xff would show-up, that the aimed device is
2374 currently not assigned any ldn. At this point, the dynamical
2375 remapping algorithm is called. It works in a way, that it goes in
2376 cyclic order through the ldns from 7 to 14. If a ldn is assigned,
2377 it takes 8 dynamical reassignment calls, until a device looses its
2378 ldn again. With this method it is assured, that while doing
2379 intense I/O between up to eight devices, no dynamical remapping is
2380 done there. ldns 0 through 6(!) are left untouched, which means, that
2381 puns 0 through 7(!) on lun=0 are always accessible without remapping.
2382 These ldns are statically assigned by this driver. The subsystem always
2383 occupies at least one pun, therefore 7 ldns (at lun=0) for other devices
2384 are sufficient. (The adapter uses always ldn=15, at whatever pun it is.) */
2385 int ibmmca_queuecommand (Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
2387 unsigned int ldn;
2388 unsigned int scsi_cmd;
2389 struct im_scb *scb;
2390 struct Scsi_Host *shpnt;
2391 int current_ldn;
2392 int id,lun;
2393 int target;
2394 int host_index;
2395 int max_pun;
2396 int i;
2397 struct scatterlist *sl;
2399 shpnt = cmd->host;
2401 /* search for the right hostadapter */
2402 for (host_index = 0; hosts[host_index] && hosts[host_index]->host_no != shpnt->host_no; host_index++);
2404 if (!hosts[host_index])
2405 { /* invalid hostadapter descriptor address */
2406 cmd->result = DID_NO_CONNECT << 16;
2407 if (done)
2408 done (cmd);
2409 return 0;
2412 max_pun = subsystem_maxid(host_index);
2414 if (ibm_ansi_order)
2416 target = max_pun - 1 - cmd->target;
2417 if ((target <= subsystem_pun(host_index))&&(cmd->target <= subsystem_pun(host_index)))
2418 target--;
2419 else if ((target >= subsystem_pun(host_index))&&(cmd->target >= subsystem_pun(host_index)))
2420 target++;
2422 else
2423 target = cmd->target;
2425 /*if (target,lun) is NO LUN or not existing at all, return error */
2426 if ((get_scsi(host_index)[target][cmd->lun] == TYPE_NO_LUN)||
2427 (get_scsi(host_index)[target][cmd->lun] == TYPE_NO_DEVICE))
2429 cmd->result = DID_NO_CONNECT << 16;
2430 if (done)
2431 done (cmd);
2432 return 0;
2435 /*if (target,lun) unassigned, do further checks... */
2436 ldn = get_ldn(host_index)[target][cmd->lun];
2437 if (ldn >= MAX_LOG_DEV) /* on invalid ldn do special stuff */
2439 if (ldn > MAX_LOG_DEV) /* dynamical remapping if ldn unassigned */
2441 current_ldn = next_ldn(host_index); /* stop-value for one circle */
2442 while (ld(host_index)[next_ldn(host_index)].cmd) /* search for a occupied, but not in */
2443 { /* command-processing ldn. */
2444 next_ldn(host_index)++;
2445 if (next_ldn(host_index)>=MAX_LOG_DEV)
2446 next_ldn(host_index) = 7;
2447 if (current_ldn == next_ldn(host_index)) /* One circle done ? */
2448 { /* no non-processing ldn found */
2449 printk("IBM MCA SCSI: Cannot assign SCSI-device dynamically!\n");
2450 printk(" On ldn 7-14 SCSI-commands everywhere in progress.\n");
2451 printk(" Reporting DID_NO_CONNECT for device (%d,%d).\n",
2452 target, cmd->lun);
2453 cmd->result = DID_NO_CONNECT << 16;/* return no connect*/
2454 if (done)
2455 done (cmd);
2456 return 0;
2460 /* unmap non-processing ldn */
2461 for (id=0; id<max_pun; id ++)
2462 for (lun=0; lun<8; lun++)
2464 if (get_ldn(host_index)[id][lun] == next_ldn(host_index))
2466 get_ldn(host_index)[id][lun] = TYPE_NO_DEVICE;
2467 /* unmap entry */
2470 /* set reduced interrupt_handler-mode for checking */
2471 local_checking_phase_flag(host_index) = 1;
2472 /* unassign found ldn (pun,lun does not matter for remove) */
2473 immediate_assign(host_index,0,0,next_ldn(host_index),REMOVE_LDN);
2474 /* assign found ldn to aimed pun,lun */
2475 immediate_assign(host_index,target,cmd->lun,next_ldn(host_index),SET_LDN);
2476 /* map found ldn to pun,lun */
2477 get_ldn(host_index)[target][cmd->lun] = next_ldn(host_index);
2478 /* change ldn to the right value, that is now next_ldn */
2479 ldn = next_ldn(host_index);
2480 /* get device information for ld[ldn] */
2481 if (device_exists (host_index, ldn,
2482 &ld(host_index)[ldn].block_length,
2483 &ld(host_index)[ldn].device_type))
2485 ld(host_index)[ldn].cmd = 0; /* To prevent panic set 0, because
2486 devices that were not assigned,
2487 should have nothing in progress. */
2489 /* increase assignment counters for statistics in /proc */
2490 IBM_DS(host_index).dynamical_assignments++;
2491 IBM_DS(host_index).ldn_assignments[ldn]++;
2493 else
2494 /* panic here, because a device, found at boottime has
2495 vanished */
2496 panic("IBM MCA SCSI: ldn=0x%x, SCSI-device on (%d,%d) vanished!\n",
2497 ldn, target, cmd->lun);
2499 /* set back to normal interrupt_handling */
2500 local_checking_phase_flag(host_index) = 0;
2502 /* Information on syslog terminal */
2503 printk("IBM MCA SCSI: ldn=0x%x dynamically reassigned to (%d,%d).\n",
2504 ldn, target, cmd->lun);
2506 /* increase next_ldn for next dynamical assignment */
2507 next_ldn(host_index)++;
2508 if (next_ldn(host_index)>=MAX_LOG_DEV)
2509 next_ldn(host_index) = 7;
2511 else
2512 { /* wall against Linux accesses to the subsystem adapter */
2513 cmd->result = DID_BAD_TARGET << 16;
2514 if (done)
2515 done (cmd);
2516 return 0;
2520 /*verify there is no command already in progress for this log dev */
2521 if (ld(host_index)[ldn].cmd)
2522 panic ("IBM MCA SCSI: cmd already in progress for this ldn.\n");
2524 /*save done in cmd, and save cmd for the interrupt handler */
2525 cmd->scsi_done = done;
2526 ld(host_index)[ldn].cmd = cmd;
2528 /*fill scb information independent of the scsi command */
2529 scb = &(ld(host_index)[ldn].scb);
2530 ld(host_index)[ldn].tsb.dev_status = 0;
2531 scb->enable = IM_REPORT_TSB_ONLY_ON_ERROR | IM_RETRY_ENABLE;
2532 scb->tsb_adr = virt_to_bus(&(ld(host_index)[ldn].tsb));
2533 scsi_cmd = cmd->cmnd[0];
2535 if (cmd->use_sg)
2537 i = cmd->use_sg;
2538 sl = (struct scatterlist *)(cmd->request_buffer);
2539 if (i > 16)
2540 panic ("IBM MCA SCSI: scatter-gather list too long.\n");
2541 while (--i >= 0)
2543 ld(host_index)[ldn].sge[i].address = (void *)(virt_to_bus(sl[i].address));
2544 ld(host_index)[ldn].sge[i].byte_length = sl[i].length;
2546 scb->enable |= IM_POINTER_TO_LIST;
2547 scb->sys_buf_adr = virt_to_bus(&(ld(host_index)[ldn].sge[0]));
2548 scb->sys_buf_length = cmd->use_sg * sizeof (struct im_sge);
2550 else
2552 scb->sys_buf_adr = virt_to_bus(cmd->request_buffer);
2553 /* recent Linux midlevel SCSI places 1024 byte for inquiry
2554 * command. Far too much for old PS/2 hardware. */
2555 switch (scsi_cmd)
2556 { /* avoid command errors by setting bufferlengths to
2557 * ANSI-standard. */
2558 case INQUIRY:
2559 case REQUEST_SENSE:
2560 case MODE_SENSE:
2561 case MODE_SELECT:
2562 scb->sys_buf_length = 255;
2563 break;
2564 case TEST_UNIT_READY:
2565 scb->sys_buf_length = 0;
2566 break;
2567 default:
2568 scb->sys_buf_length = cmd->request_bufflen;
2569 break;
2572 /*fill scb information dependent on scsi command */
2574 #ifdef IM_DEBUG_CMD
2575 printk("issue scsi cmd=%02x to ldn=%d\n", scsi_cmd, ldn);
2576 #endif
2578 /* for specific device-type debugging: */
2579 #ifdef IM_DEBUG_CMD_SPEC_DEV
2580 if (ld(host_index)[ldn].device_type==IM_DEBUG_CMD_DEVICE)
2581 printk("(SCSI-device-type=0x%x) issue scsi cmd=%02x to ldn=%d\n",
2582 ld(host_index)[ldn].device_type, scsi_cmd, ldn);
2583 #endif
2585 /* for possible panics store current command */
2586 last_scsi_command(host_index)[ldn] = scsi_cmd;
2587 last_scsi_type(host_index)[ldn] = IM_SCB;
2588 /* update statistical info */
2589 IBM_DS(host_index).total_accesses++;
2590 IBM_DS(host_index).ldn_access[ldn]++;
2592 switch (scsi_cmd)
2594 case READ_6:
2595 case WRITE_6:
2596 case READ_10:
2597 case WRITE_10:
2598 case READ_12:
2599 case WRITE_12:
2600 /* Distinguish between disk and other devices. Only disks (that are the
2601 most frequently accessed devices) should be supported by the
2602 IBM-SCSI-Subsystem commands. */
2603 switch (ld(host_index)[ldn].device_type)
2605 case TYPE_DISK: /* for harddisks enter here ... */
2606 case TYPE_MOD: /* ... try it also for MO-drives (send flames as */
2607 /* you like, if this won't work.) */
2608 if (scsi_cmd == READ_6 || scsi_cmd == READ_10 ||
2609 scsi_cmd == READ_12)
2610 { /* read command preparations */
2611 scb->enable |= IM_READ_CONTROL;
2612 IBM_DS(host_index).ldn_read_access[ldn]++; /* increase READ-access on ldn stat. */
2613 if (bypass_controller)
2615 scb->command = IM_OTHER_SCSI_CMD_CMD;
2616 scb->enable |= IM_BYPASS_BUFFER;
2617 scb->u1.scsi_cmd_length = cmd->cmd_len;
2618 memcpy(scb->u2.scsi_command,cmd->cmnd,cmd->cmd_len);
2619 last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
2621 else
2622 scb->command = IM_READ_DATA_CMD | IM_NO_DISCONNECT;
2624 else
2625 { /* write command preparations */
2626 IBM_DS(host_index).ldn_write_access[ldn]++; /* increase write-count on ldn stat.*/
2627 if (bypass_controller)
2629 scb->command = IM_OTHER_SCSI_CMD_CMD;
2630 scb->enable |= IM_BYPASS_BUFFER;
2631 scb->u1.scsi_cmd_length = cmd->cmd_len;
2632 memcpy(scb->u2.scsi_command,cmd->cmnd,cmd->cmd_len);
2633 last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
2635 else
2636 scb->command = IM_WRITE_DATA_CMD | IM_NO_DISCONNECT;
2639 if (!bypass_controller)
2641 if (scsi_cmd == READ_6 || scsi_cmd == WRITE_6)
2643 scb->u1.log_blk_adr = (((unsigned) cmd->cmnd[3]) << 0) |
2644 (((unsigned) cmd->cmnd[2]) << 8) |
2645 ((((unsigned) cmd->cmnd[1]) & 0x1f) << 16);
2646 scb->u2.blk.count = (unsigned) cmd->cmnd[4];
2648 else
2650 scb->u1.log_blk_adr = (((unsigned) cmd->cmnd[5]) << 0) |
2651 (((unsigned) cmd->cmnd[4]) << 8) |
2652 (((unsigned) cmd->cmnd[3]) << 16) |
2653 (((unsigned) cmd->cmnd[2]) << 24);
2654 scb->u2.blk.count = (((unsigned) cmd->cmnd[8]) << 0) |
2655 (((unsigned) cmd->cmnd[7]) << 8);
2657 last_scsi_logical_block(host_index)[ldn] = scb->u1.log_blk_adr;
2658 last_scsi_blockcount(host_index)[ldn] = scb->u2.blk.count;
2659 scb->u2.blk.length = ld(host_index)[ldn].block_length;
2661 if (++disk_rw_in_progress == 1)
2662 PS2_DISK_LED_ON (shpnt->host_no, target);
2663 break;
2665 /* for other devices, enter here. Other types are not known by
2666 Linux! TYPE_NO_LUN is forbidden as valid device. */
2667 case TYPE_ROM:
2668 case TYPE_TAPE:
2669 case TYPE_PROCESSOR:
2670 case TYPE_WORM:
2671 case TYPE_SCANNER:
2672 case TYPE_MEDIUM_CHANGER:
2673 /* If there is a sequential-device, IBM recommends to use
2674 IM_OTHER_SCSI_CMD_CMD instead of subsystem READ/WRITE.
2675 Good/modern CD-ROM-drives are capable of
2676 reading sequential AND random-access. This leads to the problem,
2677 that random-accesses are covered by the subsystem, but
2678 sequentials are not, as like for tape-drives. Therefore, it is
2679 the easiest way to use IM_OTHER_SCSI_CMD_CMD for all read-ops
2680 on CD-ROM-drives in order not to run into timing problems and
2681 to have a stable state. In addition, data-access on CD-ROMs
2682 works faster like that. Strange, but obvious. */
2684 scb->command = IM_OTHER_SCSI_CMD_CMD;
2685 if (scsi_cmd == READ_6 || scsi_cmd == READ_10 ||
2686 scsi_cmd == READ_12) /* enable READ */
2687 scb->enable |= IM_READ_CONTROL;
2688 scb->enable |= IM_BYPASS_BUFFER;
2689 scb->u1.scsi_cmd_length = cmd->cmd_len;
2690 memcpy (scb->u2.scsi_command, cmd->cmnd, cmd->cmd_len);
2691 last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
2693 /* Read/write on this non-disk devices is also displayworthy,
2694 so flash-up the LED/display. */
2695 if (++disk_rw_in_progress == 1)
2696 PS2_DISK_LED_ON (shpnt->host_no, target);
2697 break;
2699 break;
2700 case INQUIRY:
2701 IBM_DS(host_index).ldn_inquiry_access[ldn]++;
2702 if (bypass_controller)
2704 scb->command = IM_OTHER_SCSI_CMD_CMD;
2705 scb->enable |= IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT | IM_BYPASS_BUFFER;
2706 scb->u1.log_blk_adr = 0;
2707 scb->u1.scsi_cmd_length = cmd->cmd_len;
2708 memcpy (scb->u2.scsi_command, cmd->cmnd, cmd->cmd_len);
2709 last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
2711 else
2713 scb->command = IM_DEVICE_INQUIRY_CMD;
2714 scb->enable |= IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT;
2715 scb->u1.log_blk_adr = 0;
2717 break;
2718 case TEST_UNIT_READY:
2719 scb->command = IM_OTHER_SCSI_CMD_CMD;
2720 scb->enable |= IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT | IM_BYPASS_BUFFER;
2721 scb->u1.log_blk_adr = 0;
2722 scb->u1.scsi_cmd_length = 6;
2723 memcpy (scb->u2.scsi_command, cmd->cmnd, 6);
2724 last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
2725 break;
2726 case READ_CAPACITY:
2727 /* the length of system memory buffer must be exactly 8 bytes */
2728 if (scb->sys_buf_length > 8)
2729 scb->sys_buf_length = 8;
2730 if (bypass_controller)
2732 scb->command = IM_OTHER_SCSI_CMD_CMD;
2733 scb->enable |= IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT | IM_BYPASS_BUFFER;
2734 scb->u1.scsi_cmd_length = cmd->cmd_len;
2735 memcpy (scb->u2.scsi_command, cmd->cmnd, cmd->cmd_len);
2736 last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
2738 else
2740 scb->command = IM_READ_CAPACITY_CMD;
2741 scb->enable |= IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT;
2743 break;
2745 /* Commands that need read-only-mode (system <- device): */
2746 case REQUEST_SENSE:
2747 if (bypass_controller)
2749 scb->command = IM_OTHER_SCSI_CMD_CMD;
2750 scb->enable |= IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT | IM_BYPASS_BUFFER;
2751 scb->u1.scsi_cmd_length = cmd->cmd_len;
2752 memcpy (scb->u2.scsi_command, cmd->cmnd, cmd->cmd_len);
2753 last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
2755 else
2757 scb->command = IM_REQUEST_SENSE_CMD;
2758 scb->enable |= IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT;
2760 break;
2762 /* Commands that need write-only-mode (system -> device): */
2763 case MODE_SELECT:
2764 case MODE_SELECT_10:
2765 IBM_DS(host_index).ldn_modeselect_access[ldn]++;
2766 scb->command = IM_OTHER_SCSI_CMD_CMD;
2767 scb->enable |= IM_SUPRESS_EXCEPTION_SHORT | IM_BYPASS_BUFFER; /*Select needs WRITE-enabled*/
2768 scb->u1.scsi_cmd_length = cmd->cmd_len;
2769 memcpy (scb->u2.scsi_command, cmd->cmnd, cmd->cmd_len);
2770 last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
2771 break;
2773 /* For other commands, read-only is useful. Most other commands are
2774 running without an input-data-block. */
2775 default:
2776 scb->command = IM_OTHER_SCSI_CMD_CMD;
2777 scb->enable |= IM_READ_CONTROL | IM_SUPRESS_EXCEPTION_SHORT | IM_BYPASS_BUFFER;
2778 scb->u1.scsi_cmd_length = cmd->cmd_len;
2779 memcpy (scb->u2.scsi_command, cmd->cmnd, cmd->cmd_len);
2780 last_scsi_type(host_index)[ldn] = IM_LONG_SCB;
2781 break;
2783 /*issue scb command, and return */
2784 if (last_scsi_type(host_index)[ldn] == IM_LONG_SCB)
2786 issue_cmd (host_index, virt_to_bus(scb), IM_LONG_SCB | ldn);
2787 IBM_DS(host_index).long_scbs++;
2789 else
2791 issue_cmd (host_index, virt_to_bus(scb), IM_SCB | ldn);
2792 IBM_DS(host_index).scbs++;
2794 return 0;
2797 /*--------------------------------------------------------------------*/
2799 int ibmmca_abort (Scsi_Cmnd * cmd)
2801 /* Abort does not work, as the adapter never generates an interrupt on
2802 * whatever situation is simulated, even when really pending commands
2803 * are running on the adapters' hardware ! */
2805 struct Scsi_Host *shpnt;
2806 unsigned int ldn;
2807 void (*saved_done) (Scsi_Cmnd *);
2808 int target;
2809 int host_index;
2810 int max_pun;
2811 static unsigned long flags;
2812 unsigned long imm_command;
2814 /* return SCSI_ABORT_SNOOZE ; */
2816 #ifdef OLDKERN
2817 save_flags(flags);
2818 cli();
2819 #else
2820 spin_lock_irqsave(&abort_lock, flags);
2821 #endif
2822 shpnt = cmd->host;
2824 /* search for the right hostadapter */
2825 for (host_index = 0; hosts[host_index] && hosts[host_index]->host_no != shpnt->host_no; host_index++);
2827 if (!hosts[host_index])
2828 { /* invalid hostadapter descriptor address */
2829 cmd->result = DID_NO_CONNECT << 16;
2830 if (cmd->scsi_done)
2831 (cmd->scsi_done) (cmd);
2832 return SCSI_ABORT_SNOOZE;
2835 max_pun = subsystem_maxid(host_index);
2837 if (ibm_ansi_order)
2839 target = max_pun - 1 - cmd->target;
2840 if ((target <= subsystem_pun(host_index))&&(cmd->target <= subsystem_pun(host_index)))
2841 target--;
2842 else if ((target >= subsystem_pun(host_index))&&(cmd->target >= subsystem_pun(host_index)))
2843 target++;
2845 else
2846 target = cmd->target;
2848 /*get logical device number, and disable system interrupts */
2849 printk ("IBM MCA SCSI: Sending abort to device pun=%d, lun=%d.\n",
2850 target, cmd->lun);
2851 ldn = get_ldn(host_index)[target][cmd->lun];
2853 /*if cmd for this ldn has already finished, no need to abort */
2854 if (!ld(host_index)[ldn].cmd)
2856 #ifdef OLDKERN
2857 restore_flags(flags);
2858 #else
2859 spin_unlock_irqrestore(&abort_lock, flags);
2860 #endif
2861 return SCSI_ABORT_NOT_RUNNING;
2864 /* Clear ld.cmd, save done function, install internal done,
2865 * send abort immediate command (this enables sys. interrupts),
2866 * and wait until the interrupt arrives.
2868 saved_done = cmd->scsi_done;
2869 cmd->scsi_done = internal_done;
2870 cmd->SCp.Status = 0;
2871 last_scsi_command(host_index)[ldn] = IM_ABORT_IMM_CMD;
2872 last_scsi_type(host_index)[ldn] = IM_IMM_CMD;
2873 imm_command = inl(IM_CMD_REG(host_index));
2874 imm_command &= (unsigned long)(0xffff0000); /* mask reserved stuff */
2875 imm_command |= (unsigned long)(IM_ABORT_IMM_CMD);
2876 /* must wait for attention reg not busy */
2877 while (1)
2879 if (!(inb (IM_STAT_REG(host_index)) & IM_BUSY))
2880 break;
2881 #ifdef OLDKERN
2882 restore_flags (flags);
2883 #else
2884 spin_unlock_irqrestore(&abort_lock, flags);
2885 #endif
2886 #ifdef OLDKERN
2887 save_flags(flags);
2888 cli();
2889 #else
2890 spin_lock_irqsave(&abort_lock, flags);
2891 #endif
2893 /*write registers and enable system interrupts */
2894 outl (imm_command, IM_CMD_REG(host_index));
2895 outb (IM_IMM_CMD | ldn, IM_ATTN_REG(host_index));
2896 #ifdef OLDKERN
2897 restore_flags (flags);
2898 #else
2899 spin_unlock_irqrestore(&abort_lock, flags);
2900 #endif
2902 #ifdef IM_DEBUG_PROBE
2903 printk("IBM MCA SCSI: Abort submitted, waiting for adapter response...\n");
2904 #endif
2905 while (!cmd->SCp.Status)
2906 barrier ();
2907 cmd->scsi_done = saved_done;
2908 /*if abort went well, call saved done, then return success or error */
2909 if (cmd->result == (DID_ABORT << 16))
2911 cmd->result |= DID_ABORT << 16;
2912 if (cmd->scsi_done)
2913 (cmd->scsi_done) (cmd);
2914 ld(host_index)[ldn].cmd = NULL;
2915 #ifdef IM_DEBUG_PROBE
2916 printk("IBM MCA SCSI: Abort finished with success.\n");
2917 #endif
2918 return SCSI_ABORT_SUCCESS;
2920 else
2922 cmd->result |= DID_NO_CONNECT << 16;
2923 if (cmd->scsi_done)
2924 (cmd->scsi_done) (cmd);
2925 ld(host_index)[ldn].cmd = NULL;
2926 #ifdef IM_DEBUG_PROBE
2927 printk("IBM MCA SCSI: Abort failed.\n");
2928 #endif
2929 return SCSI_ABORT_ERROR;
2933 /*--------------------------------------------------------------------*/
2935 int ibmmca_reset (Scsi_Cmnd * cmd, unsigned int reset_flags)
2937 struct Scsi_Host *shpnt;
2938 Scsi_Cmnd *cmd_aid;
2939 int ticks,i;
2940 int host_index;
2941 static unsigned long flags;
2942 unsigned long imm_command;
2944 if (cmd == NULL)
2946 printk("IBM MCA SCSI: Reset called with NULL-command!\n");
2947 return(SCSI_RESET_SNOOZE);
2949 #ifdef OLDKERN
2950 save_flags(flags);
2951 cli();
2952 #else
2953 spin_lock_irqsave(&reset_lock, flags);
2954 #endif
2955 ticks = IM_RESET_DELAY*HZ;
2956 shpnt = cmd->host;
2957 /* search for the right hostadapter */
2958 for (host_index = 0; hosts[host_index] && hosts[host_index]->host_no != shpnt->host_no; host_index++);
2960 if (!hosts[host_index])
2961 { /* invalid hostadapter descriptor address */
2962 if (!local_checking_phase_flag(host_index))
2964 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,132)
2965 if (flags & SCSI_RESET_SYNCHRONOUS)
2967 #ifdef OLDKERN
2968 restore_flags(flags);
2969 #else
2970 spin_unlock_irqrestore(&reset_lock, flags);
2971 #endif
2972 cmd->result = DID_NO_CONNECT << 16;
2973 if (cmd->scsi_done)
2974 (cmd->scsi_done) (cmd);
2976 #endif
2978 return SCSI_ABORT_SNOOZE;
2981 if (local_checking_phase_flag(host_index))
2983 printk("IBM MCA SCSI: unable to reset while checking devices.\n");
2984 #ifdef OLDKERN
2985 restore_flags(flags);
2986 #else
2987 spin_unlock_irqrestore(&reset_lock, flags);
2988 #endif
2989 return SCSI_RESET_SNOOZE;
2992 /* issue reset immediate command to subsystem, and wait for interrupt */
2993 printk("IBM MCA SCSI: resetting all devices.\n");
2994 reset_status(host_index) = IM_RESET_IN_PROGRESS;
2995 last_scsi_command(host_index)[0xf] = IM_RESET_IMM_CMD;
2996 last_scsi_type(host_index)[0xf] = IM_IMM_CMD;
2997 imm_command = inl(IM_CMD_REG(host_index));
2998 imm_command &= (unsigned long)(0xffff0000); /* mask reserved stuff */
2999 imm_command |= (unsigned long)(IM_RESET_IMM_CMD);
3000 /* must wait for attention reg not busy */
3001 while (1)
3003 if (!(inb (IM_STAT_REG(host_index)) & IM_BUSY))
3004 break;
3005 #ifdef OLDKERN
3006 restore_flags(flags);
3007 #else
3008 spin_unlock_irqrestore(&reset_lock, flags);
3009 #endif
3010 #ifdef OLDKERN
3011 save_flags(flags);
3012 cli();
3013 #else
3014 spin_lock_irqsave(&reset_lock, flags);
3015 #endif
3017 /*write registers and enable system interrupts */
3018 outl (imm_command, IM_CMD_REG(host_index));
3019 outb (IM_IMM_CMD | 0xf, IM_ATTN_REG(host_index));
3020 /* wait for interrupt finished or intr_stat register to be set, as the
3021 * interrupt will not be executed, while we are in here! */
3022 while (reset_status(host_index) == IM_RESET_IN_PROGRESS && --ticks
3023 && ((inb(IM_INTR_REG(host_index)) & 0x8f)!=0x8f)) {
3024 mdelay(1+999/HZ);
3025 barrier();
3027 /* if reset did not complete, just return an error*/
3028 if (!ticks) {
3029 printk("IBM MCA SCSI: reset did not complete within %d seconds.\n",
3030 IM_RESET_DELAY);
3031 reset_status(host_index) = IM_RESET_FINISHED_FAIL;
3032 #ifdef OLDKERN
3033 restore_flags(flags);
3034 #else
3035 spin_unlock_irqrestore(&reset_lock, flags);
3036 #endif
3037 return SCSI_RESET_ERROR;
3040 if ((inb(IM_INTR_REG(host_index)) & 0x8f)==0x8f)
3041 { /* analysis done by this routine and not by the intr-routine */
3042 if (inb(IM_INTR_REG(host_index))==0xaf)
3043 reset_status(host_index) = IM_RESET_FINISHED_OK_NO_INT;
3044 else if (inb(IM_INTR_REG(host_index))==0xcf)
3045 reset_status(host_index) = IM_RESET_FINISHED_FAIL;
3046 else /* failed, 4get it */
3047 reset_status(host_index) = IM_RESET_NOT_IN_PROGRESS_NO_INT;
3048 outb (IM_EOI | 0xf, IM_ATTN_REG(host_index));
3051 /* if reset failed, just return an error */
3052 if (reset_status(host_index) == IM_RESET_FINISHED_FAIL) {
3053 printk("IBM MCA SCSI: reset failed.\n");
3054 #ifdef OLDKERN
3055 restore_flags(flags);
3056 #else
3057 spin_unlock_irqrestore(&reset_lock, flags);
3058 #endif
3059 return SCSI_RESET_ERROR;
3062 /* so reset finished ok - call outstanding done's, and return success */
3063 printk ("IBM MCA SCSI: Reset successfully completed.\n");
3064 #ifdef OLDKERN
3065 restore_flags(flags);
3066 #else
3067 spin_unlock_irqrestore(&reset_lock, flags);
3068 #endif
3069 for (i = 0; i < MAX_LOG_DEV; i++)
3071 cmd_aid = ld(host_index)[i].cmd;
3072 if (cmd_aid && cmd_aid->scsi_done)
3074 ld(host_index)[i].cmd = NULL;
3075 cmd_aid->result = DID_RESET << 16;
3076 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,132)
3077 if (flags & SCSI_RESET_SYNCHRONOUS)
3079 cmd_aid->result = DID_BUS_BUSY << 16;
3080 if (cmd_aid->scsi_done)
3081 (cmd_aid->scsi_done) (cmd_aid);
3083 #endif
3086 if (flags & SCSI_RESET_SUGGEST_HOST_RESET)
3087 return (SCSI_RESET_SUCCESS | SCSI_RESET_HOST_RESET);
3088 else if (flags & SCSI_RESET_SUGGEST_BUS_RESET)
3089 return (SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET);
3090 else
3091 return SCSI_RESET_SUCCESS;
3094 /*--------------------------------------------------------------------*/
3096 int ibmmca_biosparam (Disk * disk, kdev_t dev, int *info)
3098 info[0] = 64;
3099 info[1] = 32;
3100 info[2] = disk->capacity / (info[0] * info[1]);
3101 if (info[2] >= 1024)
3103 info[0] = 128;
3104 info[1] = 63;
3105 info[2] = disk->capacity / (info[0] * info[1]);
3106 if (info[2] >= 1024)
3108 info[0] = 255;
3109 info[1] = 63;
3110 info[2] = disk->capacity / (info[0] * info[1]);
3111 if (info[2] >= 1024)
3112 info[2] = 1023;
3115 return 0;
3118 /* calculate percentage of total accesses on a ldn */
3119 static int ldn_access_load(int host_index, int ldn)
3121 if (IBM_DS(host_index).total_accesses == 0) return (0);
3122 if (IBM_DS(host_index).ldn_access[ldn] == 0) return (0);
3123 return (IBM_DS(host_index).ldn_access[ldn] * 100) / IBM_DS(host_index).total_accesses;
3126 /* calculate total amount of r/w-accesses */
3127 static int ldn_access_total_read_write(int host_index)
3129 int a;
3130 int i;
3132 a = 0;
3133 for (i=0; i<=MAX_LOG_DEV; i++)
3134 a+=IBM_DS(host_index).ldn_read_access[i]+IBM_DS(host_index).ldn_write_access[i];
3135 return(a);
3138 static int ldn_access_total_inquiry(int host_index)
3140 int a;
3141 int i;
3143 a = 0;
3144 for (i=0; i<=MAX_LOG_DEV; i++)
3145 a+=IBM_DS(host_index).ldn_inquiry_access[i];
3146 return(a);
3149 static int ldn_access_total_modeselect(int host_index)
3151 int a;
3152 int i;
3154 a = 0;
3155 for (i=0; i<=MAX_LOG_DEV; i++)
3156 a+=IBM_DS(host_index).ldn_modeselect_access[i];
3157 return(a);
3160 /* routine to display info in the proc-fs-structure (a deluxe feature) */
3161 int ibmmca_proc_info (char *buffer, char **start, off_t offset, int length,
3162 int hostno, int inout)
3164 int len=0;
3165 int i,id,lun,host_index;
3166 struct Scsi_Host *shpnt;
3167 unsigned long flags;
3168 int max_pun;
3170 #ifdef OLDKERN
3171 save_flags(flags);
3172 cli();
3173 #else
3174 spin_lock_irqsave(&proc_lock, flags);
3175 #endif
3177 for (i = 0; hosts[i] && hosts[i]->host_no != hostno; i++);
3178 shpnt = hosts[i];
3179 host_index = i;
3180 if (!shpnt) {
3181 len += sprintf(buffer+len, "\nIBM MCA SCSI: Can't find adapter for host number %d\n", hostno);
3182 return len;
3184 max_pun = subsystem_maxid(host_index);
3186 len += sprintf(buffer+len, "\n IBM-SCSI-Subsystem-Linux-Driver, Version %s\n\n\n",
3187 IBMMCA_SCSI_DRIVER_VERSION);
3188 len += sprintf(buffer+len, " SCSI Access-Statistics:\n");
3189 len += sprintf(buffer+len, " Device Scanning Order....: %s\n",
3190 (ibm_ansi_order) ? "IBM/ANSI" : "New Industry Standard");
3191 #ifdef CONFIG_SCSI_MULTI_LUN
3192 len += sprintf(buffer+len, " Multiple LUN probing.....: Yes\n");
3193 #else
3194 len += sprintf(buffer+len, " Multiple LUN probing.....: No\n");
3195 #endif
3196 len += sprintf(buffer+len, " This Hostnumber..........: %d\n",
3197 hostno);
3198 len += sprintf(buffer+len, " Base I/O-Port............: 0x%x\n",
3199 (unsigned int)(IM_CMD_REG(host_index)));
3200 len += sprintf(buffer+len, " (Shared) IRQ.............: %d\n",
3201 IM_IRQ);
3202 len += sprintf(buffer+len, " SCSI-command set used....: %s\n",
3203 (bypass_controller) ? "software" : "hardware integrated");
3204 len += sprintf(buffer+len, " Total Interrupts.........: %d\n",
3205 IBM_DS(host_index).total_interrupts);
3206 len += sprintf(buffer+len, " Total SCSI Accesses......: %d\n",
3207 IBM_DS(host_index).total_accesses);
3208 len += sprintf(buffer+len, " Total short SCBs.........: %d\n",
3209 IBM_DS(host_index).scbs);
3210 len += sprintf(buffer+len, " Total long SCBs..........: %d\n",
3211 IBM_DS(host_index).long_scbs);
3212 len += sprintf(buffer+len, " Total SCSI READ/WRITE..: %d\n",
3213 ldn_access_total_read_write(host_index));
3214 len += sprintf(buffer+len, " Total SCSI Inquiries...: %d\n",
3215 ldn_access_total_inquiry(host_index));
3216 len += sprintf(buffer+len, " Total SCSI Modeselects.: %d\n",
3217 ldn_access_total_modeselect(host_index));
3218 len += sprintf(buffer+len, " Total SCSI other cmds..: %d\n",
3219 IBM_DS(host_index).total_accesses - ldn_access_total_read_write(host_index)
3220 - ldn_access_total_modeselect(host_index)
3221 - ldn_access_total_inquiry(host_index));
3222 len += sprintf(buffer+len, " Total SCSI command fails.: %d\n\n",
3223 IBM_DS(host_index).total_errors);
3224 len += sprintf(buffer+len, " Logical-Device-Number (LDN) Access-Statistics:\n");
3225 len += sprintf(buffer+len, " LDN | Accesses [%%] | READ | WRITE | ASSIGNMENTS\n");
3226 len += sprintf(buffer+len, " -----|--------------|-----------|-----------|--------------\n");
3227 for (i=0; i<=MAX_LOG_DEV; i++)
3228 len += sprintf(buffer+len, " %2X | %3d | %8d | %8d | %8d\n",
3229 i, ldn_access_load(host_index, i), IBM_DS(host_index).ldn_read_access[i],
3230 IBM_DS(host_index).ldn_write_access[i], IBM_DS(host_index).ldn_assignments[i]);
3231 len += sprintf(buffer+len, " -----------------------------------------------------------\n\n");
3233 len += sprintf(buffer+len, " Dynamical-LDN-Assignment-Statistics:\n");
3234 len += sprintf(buffer+len, " Number of physical SCSI-devices..: %d (+ Adapter)\n",
3235 IBM_DS(host_index).total_scsi_devices);
3236 len += sprintf(buffer+len, " Dynamical Assignment necessary...: %s\n",
3237 IBM_DS(host_index).dyn_flag ? "Yes" : "No ");
3238 len += sprintf(buffer+len, " Next LDN to be assigned..........: 0x%x\n",
3239 next_ldn(host_index));
3240 len += sprintf(buffer+len, " Dynamical assignments done yet...: %d\n",
3241 IBM_DS(host_index).dynamical_assignments);
3243 len += sprintf(buffer+len, "\n Current SCSI-Device-Mapping:\n");
3244 len += sprintf(buffer+len, " Physical SCSI-Device Map Logical SCSI-Device Map\n");
3245 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");
3246 for (id=0; id<max_pun; id++)
3248 len += sprintf(buffer+len, " %2d ",id);
3249 for (lun=0; lun<8; lun++)
3250 len += sprintf(buffer+len,"%2s ",ti_p(get_scsi(host_index)[id][lun]));
3251 len += sprintf(buffer+len, " %2d ",id);
3252 for (lun=0; lun<8; lun++)
3253 len += sprintf(buffer+len,"%2s ",ti_l(get_ldn(host_index)[id][lun]));
3254 len += sprintf(buffer+len,"\n");
3257 len += sprintf(buffer+len, "(A = IBM-Subsystem, D = Harddisk, T = Tapedrive, P = Processor, W = WORM,\n");
3258 len += sprintf(buffer+len, " R = CD-ROM, S = Scanner, M = MO-Drive, C = Medium-Changer, + = unprovided LUN,\n");
3259 len += sprintf(buffer+len, " - = nothing found, nothing assigned or unprobed LUN)\n\n");
3261 *start = buffer + offset;
3262 len -= offset;
3263 if (len > length)
3264 len = length;
3265 #ifdef OLDKERN
3266 restore_flags(flags);
3267 #else
3268 spin_unlock_irqrestore(&proc_lock, flags);
3269 #endif
3270 return len;
3273 void ibmmca_scsi_setup (char *str, int *ints)
3275 internal_ibmmca_scsi_setup (str, ints);
3278 static int option_setup(char *str)
3280 int ints[IM_MAX_HOSTS];
3281 char *cur = str;
3282 int i = 1;
3284 while (cur && isdigit(*cur) && i <= IM_MAX_HOSTS) {
3285 ints[i++] = simple_strtoul(cur, NULL, 0);
3286 if ((cur = strchr(cur,',')) != NULL) cur++;
3288 ints[0] = i - 1;
3289 internal_ibmmca_scsi_setup(cur, ints);
3290 return 0;
3293 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,18)
3294 __setup("ibmmcascsi=", option_setup);
3295 #endif
3297 #ifdef MODULE
3298 /* Eventually this will go into an include file, but this will be later */
3299 Scsi_Host_Template driver_template = IBMMCA;
3301 #include "scsi_module.c"
3302 #endif
3304 /*--------------------------------------------------------------------*/