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