File: /usr/src/linux/drivers/scsi/qlogicpti.c

1     /* qlogicpti.c: Performance Technologies QlogicISP sbus card driver.
2      *
3      * Copyright (C) 1996 David S. Miller (davem@caipfs.rutgers.edu)
4      *
5      * A lot of this driver was directly stolen from Erik H. Moe's PCI
6      * Qlogic ISP driver.  Mucho kudos to him for this code.
7      *
8      * An even bigger kudos to John Grana at Performance Technologies
9      * for providing me with the hardware to write this driver, you rule
10      * John you really do.
11      *
12      * May, 2, 1997: Added support for QLGC,isp --jj
13      */
14     
15     #include <linux/kernel.h>
16     #include <linux/delay.h>
17     #include <linux/types.h>
18     #include <linux/string.h>
19     #include <linux/slab.h>
20     #include <linux/blk.h>
21     #include <linux/proc_fs.h>
22     #include <linux/stat.h>
23     #include <linux/init.h>
24     #include <linux/spinlock.h>
25     
26     #include <asm/byteorder.h>
27     
28     #include "scsi.h"
29     #include "hosts.h"
30     #include "qlogicpti.h"
31     
32     #include <asm/sbus.h>
33     #include <asm/dma.h>
34     #include <asm/system.h>
35     #include <asm/ptrace.h>
36     #include <asm/pgtable.h>
37     #include <asm/oplib.h>
38     #include <asm/vaddrs.h>
39     #include <asm/io.h>
40     #include <asm/irq.h>
41     
42     #include <linux/module.h>
43     
44     #define MAX_TARGETS	16
45     #define MAX_LUNS	8	/* 32 for 1.31 F/W */
46     
47     #define DEFAULT_LOOP_COUNT	10000
48     
49     #include "qlogicpti_asm.c"
50     
51     static struct qlogicpti *qptichain = NULL;
52     static spinlock_t qptichain_lock = SPIN_LOCK_UNLOCKED;
53     static int qptis_running = 0;
54     
55     #define PACKB(a, b)			(((a)<<4)|(b))
56     
57     const u_char mbox_param[] = {
58     	PACKB(1, 1),	/* MBOX_NO_OP */
59     	PACKB(5, 5),	/* MBOX_LOAD_RAM */
60     	PACKB(2, 0),	/* MBOX_EXEC_FIRMWARE */
61     	PACKB(5, 5),	/* MBOX_DUMP_RAM */
62     	PACKB(3, 3),	/* MBOX_WRITE_RAM_WORD */
63     	PACKB(2, 3),	/* MBOX_READ_RAM_WORD */
64     	PACKB(6, 6),	/* MBOX_MAILBOX_REG_TEST */
65     	PACKB(2, 3),	/* MBOX_VERIFY_CHECKSUM	*/
66     	PACKB(1, 3),	/* MBOX_ABOUT_FIRMWARE */
67     	PACKB(0, 0),	/* 0x0009 */
68     	PACKB(0, 0),	/* 0x000a */
69     	PACKB(0, 0),	/* 0x000b */
70     	PACKB(0, 0),	/* 0x000c */
71     	PACKB(0, 0),	/* 0x000d */
72     	PACKB(1, 2),	/* MBOX_CHECK_FIRMWARE */
73     	PACKB(0, 0),	/* 0x000f */
74     	PACKB(5, 5),	/* MBOX_INIT_REQ_QUEUE */
75     	PACKB(6, 6),	/* MBOX_INIT_RES_QUEUE */
76     	PACKB(4, 4),	/* MBOX_EXECUTE_IOCB */
77     	PACKB(2, 2),	/* MBOX_WAKE_UP	*/
78     	PACKB(1, 6),	/* MBOX_STOP_FIRMWARE */
79     	PACKB(4, 4),	/* MBOX_ABORT */
80     	PACKB(2, 2),	/* MBOX_ABORT_DEVICE */
81     	PACKB(3, 3),	/* MBOX_ABORT_TARGET */
82     	PACKB(2, 2),	/* MBOX_BUS_RESET */
83     	PACKB(2, 3),	/* MBOX_STOP_QUEUE */
84     	PACKB(2, 3),	/* MBOX_START_QUEUE */
85     	PACKB(2, 3),	/* MBOX_SINGLE_STEP_QUEUE */
86     	PACKB(2, 3),	/* MBOX_ABORT_QUEUE */
87     	PACKB(2, 4),	/* MBOX_GET_DEV_QUEUE_STATUS */
88     	PACKB(0, 0),	/* 0x001e */
89     	PACKB(1, 3),	/* MBOX_GET_FIRMWARE_STATUS */
90     	PACKB(1, 2),	/* MBOX_GET_INIT_SCSI_ID */
91     	PACKB(1, 2),	/* MBOX_GET_SELECT_TIMEOUT */
92     	PACKB(1, 3),	/* MBOX_GET_RETRY_COUNT	*/
93     	PACKB(1, 2),	/* MBOX_GET_TAG_AGE_LIMIT */
94     	PACKB(1, 2),	/* MBOX_GET_CLOCK_RATE */
95     	PACKB(1, 2),	/* MBOX_GET_ACT_NEG_STATE */
96     	PACKB(1, 2),	/* MBOX_GET_ASYNC_DATA_SETUP_TIME */
97     	PACKB(1, 3),	/* MBOX_GET_SBUS_PARAMS */
98     	PACKB(2, 4),	/* MBOX_GET_TARGET_PARAMS */
99     	PACKB(2, 4),	/* MBOX_GET_DEV_QUEUE_PARAMS */
100     	PACKB(0, 0),	/* 0x002a */
101     	PACKB(0, 0),	/* 0x002b */
102     	PACKB(0, 0),	/* 0x002c */
103     	PACKB(0, 0),	/* 0x002d */
104     	PACKB(0, 0),	/* 0x002e */
105     	PACKB(0, 0),	/* 0x002f */
106     	PACKB(2, 2),	/* MBOX_SET_INIT_SCSI_ID */
107     	PACKB(2, 2),	/* MBOX_SET_SELECT_TIMEOUT */
108     	PACKB(3, 3),	/* MBOX_SET_RETRY_COUNT	*/
109     	PACKB(2, 2),	/* MBOX_SET_TAG_AGE_LIMIT */
110     	PACKB(2, 2),	/* MBOX_SET_CLOCK_RATE */
111     	PACKB(2, 2),	/* MBOX_SET_ACTIVE_NEG_STATE */
112     	PACKB(2, 2),	/* MBOX_SET_ASYNC_DATA_SETUP_TIME */
113     	PACKB(3, 3),	/* MBOX_SET_SBUS_CONTROL_PARAMS */
114     	PACKB(4, 4),	/* MBOX_SET_TARGET_PARAMS */
115     	PACKB(4, 4),	/* MBOX_SET_DEV_QUEUE_PARAMS */
116     	PACKB(0, 0),	/* 0x003a */
117     	PACKB(0, 0),	/* 0x003b */
118     	PACKB(0, 0),	/* 0x003c */
119     	PACKB(0, 0),	/* 0x003d */
120     	PACKB(0, 0),	/* 0x003e */
121     	PACKB(0, 0),	/* 0x003f */
122     	PACKB(0, 0),	/* 0x0040 */
123     	PACKB(0, 0),	/* 0x0041 */
124     	PACKB(0, 0)	/* 0x0042 */
125     };
126     
127     #define MAX_MBOX_COMMAND	(sizeof(mbox_param)/sizeof(u_short))
128     
129     /* queue length's _must_ be power of two: */
130     #define QUEUE_DEPTH(in, out, ql)	((in - out) & (ql))
131     #define REQ_QUEUE_DEPTH(in, out)	QUEUE_DEPTH(in, out, 		     \
132     						    QLOGICPTI_REQ_QUEUE_LEN)
133     #define RES_QUEUE_DEPTH(in, out)	QUEUE_DEPTH(in, out, RES_QUEUE_LEN)
134     
135     static inline void qlogicpti_enable_irqs(struct qlogicpti *qpti)
136     {
137     	sbus_writew(SBUS_CTRL_ERIRQ | SBUS_CTRL_GENAB,
138     		    qpti->qregs + SBUS_CTRL);
139     }
140     
141     static inline void qlogicpti_disable_irqs(struct qlogicpti *qpti)
142     {
143     	sbus_writew(0, qpti->qregs + SBUS_CTRL);
144     }
145     
146     static inline void set_sbus_cfg1(struct qlogicpti *qpti)
147     {
148     	u16 val;
149     	u8 bursts = qpti->bursts;
150     
151     #if 0	/* It appears that at least PTI cards do not support
152     	 * 64-byte bursts and that setting the B64 bit actually
153     	 * is a nop and the chip ends up using the smallest burst
154     	 * size. -DaveM
155     	 */
156     	if (sbus_can_burst64(qpti->sdev) && (bursts & DMA_BURST64)) {
157     		val = (SBUS_CFG1_BENAB | SBUS_CFG1_B64);
158     	} else
159     #endif
160     	if (bursts & DMA_BURST32) {
161     		val = (SBUS_CFG1_BENAB | SBUS_CFG1_B32);
162     	} else if (bursts & DMA_BURST16) {
163     		val = (SBUS_CFG1_BENAB | SBUS_CFG1_B16);
164     	} else if (bursts & DMA_BURST8) {
165     		val = (SBUS_CFG1_BENAB | SBUS_CFG1_B8);
166     	} else {
167     		val = 0; /* No sbus bursts for you... */
168     	}
169     	sbus_writew(val, qpti->qregs + SBUS_CFG1);
170     }
171     
172     static int qlogicpti_mbox_command(struct qlogicpti *qpti, u_short param[], int force)
173     {
174     	int loop_count;
175     	u16 tmp;
176     
177     	if (mbox_param[param[0]] == 0)
178     		return 1;
179     
180     	/* Set SBUS semaphore. */
181     	tmp = sbus_readw(qpti->qregs + SBUS_SEMAPHORE);
182     	tmp |= SBUS_SEMAPHORE_LCK;
183     	sbus_writew(tmp, qpti->qregs + SBUS_SEMAPHORE);
184     
185     	/* Wait for host IRQ bit to clear. */
186     	loop_count = DEFAULT_LOOP_COUNT;
187     	while (--loop_count && (sbus_readw(qpti->qregs + HCCTRL) & HCCTRL_HIRQ))
188     		barrier();
189     	if (!loop_count)
190     		printk(KERN_EMERG "qlogicpti: mbox_command loop timeout #1\n");
191     
192     	/* Write mailbox command registers. */
193     	switch (mbox_param[param[0]] >> 4) {
194     	case 6: sbus_writew(param[5], qpti->qregs + MBOX5);
195     	case 5: sbus_writew(param[4], qpti->qregs + MBOX4);
196     	case 4: sbus_writew(param[3], qpti->qregs + MBOX3);
197     	case 3: sbus_writew(param[2], qpti->qregs + MBOX2);
198     	case 2: sbus_writew(param[1], qpti->qregs + MBOX1);
199     	case 1: sbus_writew(param[0], qpti->qregs + MBOX0);
200     	}
201     
202     	/* Clear RISC interrupt. */
203     	tmp = sbus_readw(qpti->qregs + HCCTRL);
204     	tmp |= HCCTRL_CRIRQ;
205     	sbus_writew(tmp, qpti->qregs + HCCTRL);
206     
207     	/* Clear SBUS semaphore. */
208     	sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
209     
210     	/* Set HOST interrupt. */
211     	tmp = sbus_readw(qpti->qregs + HCCTRL);
212     	tmp |= HCCTRL_SHIRQ;
213     	sbus_writew(tmp, qpti->qregs + HCCTRL);
214     
215     	/* Wait for HOST interrupt clears. */
216     	loop_count = DEFAULT_LOOP_COUNT;
217     	while (--loop_count &&
218     	       (sbus_readw(qpti->qregs + HCCTRL) & HCCTRL_CRIRQ))
219     		udelay(20);
220     	if (!loop_count)
221     		printk(KERN_EMERG "qlogicpti: mbox_command[%04x] loop timeout #2\n",
222     		       param[0]);
223     
224     	/* Wait for SBUS semaphore to get set. */
225     	loop_count = DEFAULT_LOOP_COUNT;
226     	while (--loop_count &&
227     	       !(sbus_readw(qpti->qregs + SBUS_SEMAPHORE) & SBUS_SEMAPHORE_LCK)) {
228     		udelay(20);
229     
230     		/* Workaround for some buggy chips. */
231     		if (sbus_readw(qpti->qregs + MBOX0) & 0x4000)
232     			break;
233     	}
234     	if (!loop_count)
235     		printk(KERN_EMERG "qlogicpti: mbox_command[%04x] loop timeout #3\n",
236     		       param[0]);
237     
238     	/* Wait for MBOX busy condition to go away. */
239     	loop_count = DEFAULT_LOOP_COUNT;
240     	while (--loop_count && (sbus_readw(qpti->qregs + MBOX0) == 0x04))
241     		udelay(20);
242     	if (!loop_count)
243     		printk(KERN_EMERG "qlogicpti: mbox_command[%04x] loop timeout #4\n",
244     		       param[0]);
245     
246     	/* Read back output parameters. */
247     	switch (mbox_param[param[0]] & 0xf) {
248     	case 6: param[5] = sbus_readw(qpti->qregs + MBOX5);
249     	case 5: param[4] = sbus_readw(qpti->qregs + MBOX4);
250     	case 4: param[3] = sbus_readw(qpti->qregs + MBOX3);
251     	case 3: param[2] = sbus_readw(qpti->qregs + MBOX2);
252     	case 2: param[1] = sbus_readw(qpti->qregs + MBOX1);
253     	case 1: param[0] = sbus_readw(qpti->qregs + MBOX0);
254     	}
255     
256     	/* Clear RISC interrupt. */
257     	tmp = sbus_readw(qpti->qregs + HCCTRL);
258     	tmp |= HCCTRL_CRIRQ;
259     	sbus_writew(tmp, qpti->qregs + HCCTRL);
260     
261     	/* Release SBUS semaphore. */
262     	tmp = sbus_readw(qpti->qregs + SBUS_SEMAPHORE);
263     	tmp &= ~(SBUS_SEMAPHORE_LCK);
264     	sbus_writew(tmp, qpti->qregs + SBUS_SEMAPHORE);
265     
266     	/* We're done. */
267     	return 0;
268     }
269     
270     static inline void qlogicpti_set_hostdev_defaults(struct qlogicpti *qpti)
271     {
272     	int i;
273     
274     	qpti->host_param.initiator_scsi_id = qpti->scsi_id;
275     	qpti->host_param.bus_reset_delay = 3;
276     	qpti->host_param.retry_count = 0;
277     	qpti->host_param.retry_delay = 5;
278     	qpti->host_param.async_data_setup_time = 3;
279     	qpti->host_param.req_ack_active_negation = 1;
280     	qpti->host_param.data_line_active_negation = 1;
281     	qpti->host_param.data_dma_burst_enable = 1;
282     	qpti->host_param.command_dma_burst_enable = 1;
283     	qpti->host_param.tag_aging = 8;
284     	qpti->host_param.selection_timeout = 250;
285     	qpti->host_param.max_queue_depth = 256;
286     
287     	for(i = 0; i < MAX_TARGETS; i++) {
288     		/*
289     		 * disconnect, parity, arq, reneg on reset, and, oddly enough
290     		 * tags...the midlayer's notion of tagged support has to match
291     		 * our device settings, and since we base whether we enable a
292     		 * tag on a  per-cmnd basis upon what the midlayer sez, we
293     		 * actually enable the capability here.
294     		 */
295     		qpti->dev_param[i].device_flags = 0xcd;
296     		qpti->dev_param[i].execution_throttle = 16;
297     		if (qpti->ultra) {
298     			qpti->dev_param[i].synchronous_period = 12;
299     			qpti->dev_param[i].synchronous_offset = 8;
300     		} else {
301     			qpti->dev_param[i].synchronous_period = 25;
302     			qpti->dev_param[i].synchronous_offset = 12;
303     		}
304     		qpti->dev_param[i].device_enable = 1;
305     	}
306     	/* this is very important to set! */
307     	qpti->sbits = 1 << qpti->scsi_id;
308     }
309     
310     static int qlogicpti_reset_hardware(struct Scsi_Host *host)
311     {
312     	struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
313     	u_short param[6];
314     	unsigned short risc_code_addr;
315     	int loop_count, i;
316     	unsigned long flags;
317     
318     	risc_code_addr = 0x1000;	/* all load addresses are at 0x1000 */
319     
320     	save_flags(flags); cli();
321     
322     	sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
323     
324     	/* Only reset the scsi bus if it is not free. */
325     	if (sbus_readw(qpti->qregs + CPU_PCTRL) & CPU_PCTRL_BSY) {
326     		sbus_writew(CPU_ORIDE_RMOD, qpti->qregs + CPU_ORIDE);
327     		sbus_writew(CPU_CMD_BRESET, qpti->qregs + CPU_CMD);
328     		udelay(400);
329     	}
330     
331     	sbus_writew(SBUS_CTRL_RESET, qpti->qregs + SBUS_CTRL);
332     	sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + CMD_DMA_CTRL);
333     	sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + DATA_DMA_CTRL);
334     
335     	loop_count = DEFAULT_LOOP_COUNT;
336     	while (--loop_count && ((sbus_readw(qpti->qregs + MBOX0) & 0xff) == 0x04))
337     		udelay(20);
338     	if (!loop_count)
339     		printk(KERN_EMERG "qlogicpti: reset_hardware loop timeout\n");
340     
341     	sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
342     	set_sbus_cfg1(qpti);
343     	qlogicpti_enable_irqs(qpti);
344     
345     	if (sbus_readw(qpti->qregs + RISC_PSR) & RISC_PSR_ULTRA) {
346     		qpti->ultra = 1;
347     		sbus_writew((RISC_MTREG_P0ULTRA | RISC_MTREG_P1ULTRA),
348     			    qpti->qregs + RISC_MTREG);
349     	} else {
350     		qpti->ultra = 0;
351     		sbus_writew((RISC_MTREG_P0DFLT | RISC_MTREG_P1DFLT),
352     			    qpti->qregs + RISC_MTREG);
353     	}
354     
355     	/* reset adapter and per-device default values. */
356     	/* do it after finding out whether we're ultra mode capable */
357     	qlogicpti_set_hostdev_defaults(qpti);
358     
359     	/* Release the RISC processor. */
360     	sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
361     
362     	/* Get RISC to start executing the firmware code. */
363     	param[0] = MBOX_EXEC_FIRMWARE;
364     	param[1] = risc_code_addr;
365     	if (qlogicpti_mbox_command(qpti, param, 1)) {
366     		printk(KERN_EMERG "qlogicpti%d: Cannot execute ISP firmware.\n",
367     		       qpti->qpti_id);
368     		restore_flags(flags);
369     		return 1;
370     	}
371     
372     	/* Set initiator scsi ID. */
373     	param[0] = MBOX_SET_INIT_SCSI_ID;
374     	param[1] = qpti->host_param.initiator_scsi_id;
375     	if (qlogicpti_mbox_command(qpti, param, 1) ||
376     	   (param[0] != MBOX_COMMAND_COMPLETE)) {
377     		printk(KERN_EMERG "qlogicpti%d: Cannot set initiator SCSI ID.\n",
378     		       qpti->qpti_id);
379     		restore_flags(flags);
380     		return 1;
381     	}
382     
383     	/* Initialize state of the queues, both hw and sw. */
384     	qpti->req_in_ptr = qpti->res_out_ptr = 0;
385     
386     	param[0] = MBOX_INIT_RES_QUEUE;
387     	param[1] = RES_QUEUE_LEN + 1;
388     	param[2] = (u_short) (qpti->res_dvma >> 16);
389     	param[3] = (u_short) (qpti->res_dvma & 0xffff);
390     	param[4] = param[5] = 0;
391     	if (qlogicpti_mbox_command(qpti, param, 1)) {
392     		printk(KERN_EMERG "qlogicpti%d: Cannot init response queue.\n",
393     		       qpti->qpti_id);
394     		restore_flags(flags);
395     		return 1;
396     	}
397     
398     	param[0] = MBOX_INIT_REQ_QUEUE;
399     	param[1] = QLOGICPTI_REQ_QUEUE_LEN + 1;
400     	param[2] = (u_short) (qpti->req_dvma >> 16);
401     	param[3] = (u_short) (qpti->req_dvma & 0xffff);
402     	param[4] = param[5] = 0;
403     	if (qlogicpti_mbox_command(qpti, param, 1)) {
404     		printk(KERN_EMERG "qlogicpti%d: Cannot init request queue.\n",
405     		       qpti->qpti_id);
406     		restore_flags(flags);
407     		return 1;
408     	}
409     
410     	param[0] = MBOX_SET_RETRY_COUNT;
411     	param[1] = qpti->host_param.retry_count;
412     	param[2] = qpti->host_param.retry_delay;
413     	qlogicpti_mbox_command(qpti, param, 0);
414     
415     	param[0] = MBOX_SET_TAG_AGE_LIMIT;
416     	param[1] = qpti->host_param.tag_aging;
417     	qlogicpti_mbox_command(qpti, param, 0);
418     
419     	for (i = 0; i < MAX_TARGETS; i++) {
420     		param[0] = MBOX_GET_DEV_QUEUE_PARAMS;
421     		param[1] = (i << 8);
422     		qlogicpti_mbox_command(qpti, param, 0);
423     	}
424     
425     	param[0] = MBOX_GET_FIRMWARE_STATUS;
426     	qlogicpti_mbox_command(qpti, param, 0);
427     
428     	param[0] = MBOX_SET_SELECT_TIMEOUT;
429     	param[1] = qpti->host_param.selection_timeout;
430     	qlogicpti_mbox_command(qpti, param, 0);
431     
432     	for (i = 0; i < MAX_TARGETS; i++) {
433     		param[0] = MBOX_SET_TARGET_PARAMS;
434     		param[1] = (i << 8);
435     		param[2] = (qpti->dev_param[i].device_flags << 8);
436     		/*
437     		 * Since we're now loading 1.31 f/w, force narrow/async.
438     		 */
439     		param[2] |= 0xc0;
440     		param[3] = 0;	/* no offset, we do not have sync mode yet */
441     		qlogicpti_mbox_command(qpti, param, 0);
442     	}
443     
444     	/*
445     	 * Always (sigh) do an initial bus reset (kicks f/w).
446     	 */
447     	param[0] = MBOX_BUS_RESET;
448     	param[1] = qpti->host_param.bus_reset_delay;
449     	qlogicpti_mbox_command(qpti, param, 0);
450     	qpti->send_marker = 1;
451     
452     	restore_flags(flags);
453     	return 0;
454     }
455     
456     #define PTI_RESET_LIMIT 400
457     
458     static int __init qlogicpti_load_firmware(struct qlogicpti *qpti)
459     {
460     	unsigned short csum = 0;
461     	unsigned short param[6];
462     	unsigned short *risc_code, risc_code_addr, risc_code_length;
463     	unsigned long flags;
464     	int i, timeout;
465     
466     	risc_code = &sbus_risc_code01[0];
467     	risc_code_addr = 0x1000;	/* all f/w modules load at 0x1000 */
468     	risc_code_length = sbus_risc_code_length01;
469     
470     	save_flags(flags); cli();
471     
472     	/* Verify the checksum twice, one before loading it, and once
473     	 * afterwards via the mailbox commands.
474     	 */
475     	for (i = 0; i < risc_code_length; i++)
476     		csum += risc_code[i];
477     	if (csum) {
478     		restore_flags(flags);
479     		printk(KERN_EMERG "qlogicpti%d: Aieee, firmware checksum failed!",
480     		       qpti->qpti_id);
481     		return 1;
482     	}		
483     	sbus_writew(SBUS_CTRL_RESET, qpti->qregs + SBUS_CTRL);
484     	sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + CMD_DMA_CTRL);
485     	sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + DATA_DMA_CTRL);
486     	timeout = PTI_RESET_LIMIT;
487     	while (--timeout && (sbus_readw(qpti->qregs + SBUS_CTRL) & SBUS_CTRL_RESET))
488     		udelay(20);
489     	if (!timeout) {
490     		restore_flags(flags);
491     		printk(KERN_EMERG "qlogicpti%d: Cannot reset the ISP.", qpti->qpti_id);
492     		return 1;
493     	}
494     
495     	sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
496     	mdelay(1);
497     
498     	sbus_writew((SBUS_CTRL_GENAB | SBUS_CTRL_ERIRQ), qpti->qregs + SBUS_CTRL);
499     	set_sbus_cfg1(qpti);
500     	sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
501     
502     	if (sbus_readw(qpti->qregs + RISC_PSR) & RISC_PSR_ULTRA) {
503     		qpti->ultra = 1;
504     		sbus_writew((RISC_MTREG_P0ULTRA | RISC_MTREG_P1ULTRA),
505     			    qpti->qregs + RISC_MTREG);
506     	} else {
507     		qpti->ultra = 0;
508     		sbus_writew((RISC_MTREG_P0DFLT | RISC_MTREG_P1DFLT),
509     			    qpti->qregs + RISC_MTREG);
510     	}
511     
512     	sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
513     
514     	/* Pin lines are only stable while RISC is paused. */
515     	sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
516     	if (sbus_readw(qpti->qregs + CPU_PDIFF) & CPU_PDIFF_MODE)
517     		qpti->differential = 1;
518     	else
519     		qpti->differential = 0;
520     	sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
521     
522     	/* This shouldn't be necessary- we've reset things so we should be
523     	   running from the ROM now.. */
524     
525     	param[0] = MBOX_STOP_FIRMWARE;
526     	param[1] = param[2] = param[3] = param[4] = param[5] = 0;
527     	if (qlogicpti_mbox_command(qpti, param, 1)) {
528     		printk(KERN_EMERG "qlogicpti%d: Cannot stop firmware for reload.\n",
529     		       qpti->qpti_id);
530     		restore_flags(flags);
531     		return 1;
532     	}		
533     
534     	/* Load it up.. */
535     	for (i = 0; i < risc_code_length; i++) {
536     		param[0] = MBOX_WRITE_RAM_WORD;
537     		param[1] = risc_code_addr + i;
538     		param[2] = risc_code[i];
539     		if (qlogicpti_mbox_command(qpti, param, 1) ||
540     		    param[0] != MBOX_COMMAND_COMPLETE) {
541     			printk("qlogicpti%d: Firmware dload failed, I'm bolixed!\n",
542     			       qpti->qpti_id);
543     			restore_flags(flags);
544     			return 1;
545     		}
546     	}
547     
548     	/* Reset the ISP again. */
549     	sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
550     	mdelay(1);
551     
552     	qlogicpti_enable_irqs(qpti);
553     	sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
554     	sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
555     
556     	/* Ask ISP to verify the checksum of the new code. */
557     	param[0] = MBOX_VERIFY_CHECKSUM;
558     	param[1] = risc_code_addr;
559     	if (qlogicpti_mbox_command(qpti, param, 1) ||
560     	    (param[0] != MBOX_COMMAND_COMPLETE)) {
561     		printk(KERN_EMERG "qlogicpti%d: New firmware csum failure!\n",
562     		       qpti->qpti_id);
563     		restore_flags(flags);
564     		return 1;
565     	}
566     
567     	/* Start using newly downloaded firmware. */
568     	param[0] = MBOX_EXEC_FIRMWARE;
569     	param[1] = risc_code_addr;
570     	qlogicpti_mbox_command(qpti, param, 1);
571     
572     	param[0] = MBOX_ABOUT_FIRMWARE;
573     	if (qlogicpti_mbox_command(qpti, param, 1) ||
574     	    (param[0] != MBOX_COMMAND_COMPLETE)) {
575     		printk(KERN_EMERG "qlogicpti%d: AboutFirmware cmd fails.\n",
576     		       qpti->qpti_id);
577     		restore_flags(flags);
578     		return 1;
579     	}
580     
581     	/* Snag the major and minor revisions from the result. */
582     	qpti->fware_majrev = param[1];
583     	qpti->fware_minrev = param[2];
584     	qpti->fware_micrev = param[3];
585     
586     	/* Set the clock rate */
587     	param[0] = MBOX_SET_CLOCK_RATE;
588     	param[1] = qpti->clock;
589     	if (qlogicpti_mbox_command(qpti, param, 1) ||
590     	    (param[0] != MBOX_COMMAND_COMPLETE)) {
591     		printk(KERN_EMERG "qlogicpti%d: could not set clock rate.\n",
592     		       qpti->qpti_id);
593     		restore_flags(flags);
594     		return 1;
595     	}
596     
597     	if (qpti->is_pti != 0) {
598     		/* Load scsi initiator ID and interrupt level into sbus static ram. */
599     		param[0] = MBOX_WRITE_RAM_WORD;
600     		param[1] = 0xff80;
601     		param[2] = (unsigned short) qpti->scsi_id;
602     		qlogicpti_mbox_command(qpti, param, 1);
603     
604     		param[0] = MBOX_WRITE_RAM_WORD;
605     		param[1] = 0xff00;
606     		param[2] = (unsigned short) 3;
607     		qlogicpti_mbox_command(qpti, param, 1);
608     	}
609     
610     	restore_flags(flags);
611     	return 0;
612     }
613     
614     static int qlogicpti_verify_tmon(struct qlogicpti *qpti)
615     {
616     	int curstat = sbus_readb(qpti->sreg);
617     
618     	curstat &= 0xf0;
619     	if (!(curstat & SREG_FUSE) && (qpti->swsreg & SREG_FUSE))
620     		printk("qlogicpti%d: Fuse returned to normal state.\n", qpti->qpti_id);
621     	if (!(curstat & SREG_TPOWER) && (qpti->swsreg & SREG_TPOWER))
622     		printk("qlogicpti%d: termpwr back to normal state.\n", qpti->qpti_id);
623     	if (curstat != qpti->swsreg) {
624     		int error = 0;
625     		if (curstat & SREG_FUSE) {
626     			error++;
627     			printk("qlogicpti%d: Fuse is open!\n", qpti->qpti_id);
628     		}
629     		if (curstat & SREG_TPOWER) {
630     			error++;
631     			printk("qlogicpti%d: termpwr failure\n", qpti->qpti_id);
632     		}
633     		if (qpti->differential &&
634     		    (curstat & SREG_DSENSE) != SREG_DSENSE) {
635     			error++;
636     			printk("qlogicpti%d: You have a single ended device on a "
637     			       "differential bus!  Please fix!\n", qpti->qpti_id);
638     		}
639     		qpti->swsreg = curstat;
640     		return error;
641     	}
642     	return 0;
643     }
644     
645     static void qpti_intr(int irq, void *dev_id, struct pt_regs *regs);
646     
647     static void __init qpti_chain_add(struct qlogicpti *qpti)
648     {
649     	spin_lock_irq(&qptichain_lock);
650     	if (qptichain != NULL) {
651     		struct qlogicpti *qlink = qptichain;
652     
653     		while(qlink->next)
654     			qlink = qlink->next;
655     		qlink->next = qpti;
656     	} else {
657     		qptichain = qpti;
658     	}
659     	qpti->next = NULL;
660     	spin_unlock_irq(&qptichain_lock);
661     }
662     
663     static void __init qpti_chain_del(struct qlogicpti *qpti)
664     {
665     	spin_lock_irq(&qptichain_lock);
666     	if (qptichain == qpti) {
667     		qptichain = qpti->next;
668     	} else {
669     		struct qlogicpti *qlink = qptichain;
670     		while(qlink->next != qpti)
671     			qlink = qlink->next;
672     		qlink->next = qpti->next;
673     	}
674     	qpti->next = NULL;
675     	spin_unlock_irq(&qptichain_lock);
676     }
677     
678     static int __init qpti_map_regs(struct qlogicpti *qpti)
679     {
680     	struct sbus_dev *sdev = qpti->sdev;
681     
682     	qpti->qregs = sbus_ioremap(&sdev->resource[0], 0,
683     				   sdev->reg_addrs[0].reg_size,
684     				   "PTI Qlogic/ISP");
685     	if (!qpti->qregs) {
686     		printk("PTI: Qlogic/ISP registers are unmappable\n");
687     		return -1;
688     	}
689     	if (qpti->is_pti) {
690     		qpti->sreg = sbus_ioremap(&sdev->resource[0], (16 * 4096),
691     					  sizeof(unsigned char),
692     					  "PTI Qlogic/ISP statreg");
693     		if (!qpti->sreg) {
694     			printk("PTI: Qlogic/ISP status register is unmappable\n");
695     			return -1;
696     		}
697     	}
698     	return 0;
699     }
700     
701     static int __init qpti_register_irq(struct qlogicpti *qpti)
702     {
703     	struct sbus_dev *sdev = qpti->sdev;
704     
705     	qpti->qhost->irq = qpti->irq = sdev->irqs[0];
706     
707     	/* We used to try various overly-clever things to
708     	 * reduce the interrupt processing overhead on
709     	 * sun4c/sun4m when multiple PTI's shared the
710     	 * same IRQ.  It was too complex and messy to
711     	 * sanely maintain.
712     	 */
713     	if (request_irq(qpti->irq, qpti_intr,
714     			SA_SHIRQ, "Qlogic/PTI", qpti))
715     		goto fail;
716     
717     	printk("qpti%d: IRQ %s ", qpti->qpti_id, __irq_itoa(qpti->irq));
718     
719     	return 0;
720     
721     fail:
722     	printk("qpti%d: Cannot acquire irq line\n", qpti->qpti_id);
723     	return -1;
724     }
725     
726     static void __init qpti_get_scsi_id(struct qlogicpti *qpti)
727     {
728     	qpti->scsi_id = prom_getintdefault(qpti->prom_node,
729     					   "initiator-id",
730     					   -1);
731     	if (qpti->scsi_id == -1)
732     		qpti->scsi_id = prom_getintdefault(qpti->prom_node,
733     						   "scsi-initiator-id",
734     						   -1);
735     	if (qpti->scsi_id == -1)
736     		qpti->scsi_id =
737     			prom_getintdefault(qpti->sdev->bus->prom_node,
738     					   "scsi-initiator-id", 7);
739     	qpti->qhost->this_id = qpti->scsi_id;
740     	qpti->qhost->max_sectors = 64;
741     
742     	printk("SCSI ID %d ", qpti->scsi_id);
743     }
744     
745     static void qpti_get_bursts(struct qlogicpti *qpti)
746     {
747     	struct sbus_dev *sdev = qpti->sdev;
748     	u8 bursts, bmask;
749     
750     	bursts = prom_getintdefault(qpti->prom_node, "burst-sizes", 0xff);
751     	bmask = prom_getintdefault(sdev->bus->prom_node,
752     				   "burst-sizes", 0xff);
753     	if (bmask != 0xff)
754     		bursts &= bmask;
755     	if (bursts == 0xff ||
756     	    (bursts & DMA_BURST16) == 0 ||
757     	    (bursts & DMA_BURST32) == 0)
758     		bursts = (DMA_BURST32 - 1);
759     
760     	qpti->bursts = bursts;
761     }
762     
763     static void qpti_get_clock(struct qlogicpti *qpti)
764     {
765     	unsigned int cfreq;
766     
767     	/* Check for what the clock input to this card is.
768     	 * Default to 40Mhz.
769     	 */
770     	cfreq = prom_getintdefault(qpti->prom_node,"clock-frequency",40000000);
771     	qpti->clock = (cfreq + 500000)/1000000;
772     	if (qpti->clock == 0) /* bullshit */
773     		qpti->clock = 40;
774     }
775     
776     /* The request and response queues must each be aligned
777      * on a page boundry.
778      */
779     static int __init qpti_map_queues(struct qlogicpti *qpti)
780     {
781     	struct sbus_dev *sdev = qpti->sdev;
782     
783     #define QSIZE(entries)	(((entries) + 1) * QUEUE_ENTRY_LEN)
784     	qpti->res_cpu = sbus_alloc_consistent(sdev,
785     					      QSIZE(RES_QUEUE_LEN),
786     					      &qpti->res_dvma);
787     	if (qpti->res_cpu == NULL ||
788     	    qpti->res_dvma == 0) {
789     		printk("QPTI: Cannot map response queue.\n");
790     		return -1;
791     	}
792     
793     	qpti->req_cpu = sbus_alloc_consistent(sdev,
794     					      QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
795     					      &qpti->req_dvma);
796     	if (qpti->req_cpu == NULL ||
797     	    qpti->req_dvma == 0) {
798     		sbus_free_consistent(sdev, QSIZE(RES_QUEUE_LEN),
799     				     qpti->res_cpu, qpti->res_dvma);
800     		printk("QPTI: Cannot map request queue.\n");
801     		return -1;
802     	}
803     	memset(qpti->res_cpu, 0, QSIZE(RES_QUEUE_LEN));
804     	memset(qpti->req_cpu, 0, QSIZE(QLOGICPTI_REQ_QUEUE_LEN));
805     	return 0;
806     }
807     
808     /* Detect all PTI Qlogic ISP's in the machine. */
809     int __init qlogicpti_detect(Scsi_Host_Template *tpnt)
810     {
811     	struct qlogicpti *qpti;
812     	struct Scsi_Host *qpti_host;
813     	struct sbus_bus *sbus;
814     	struct sbus_dev *sdev;
815     	int nqptis = 0, nqptis_in_use = 0;
816     
817     	tpnt->proc_name = "qlogicpti";
818     	for_each_sbus(sbus) {
819     		for_each_sbusdev(sdev, sbus) {
820     			/* Is this a red snapper? */
821     			if (strcmp(sdev->prom_name, "ptisp") &&
822     			    strcmp(sdev->prom_name, "PTI,ptisp") &&
823     			    strcmp(sdev->prom_name, "QLGC,isp") &&
824     			    strcmp(sdev->prom_name, "SUNW,isp"))
825     				continue;
826     
827     			/* Sometimes Antares cards come up not completely
828     			 * setup, and we get a report of a zero IRQ.
829     			 * Skip over them in such cases so we survive.
830     			 */
831     			if (sdev->irqs[0] == 0) {
832     				printk("qpti%d: Adapter reports no interrupt, "
833     				       "skipping over this card.", nqptis);
834     				continue;
835     			}
836     
837     			/* Yep, register and allocate software state. */
838     			qpti_host = scsi_register(tpnt, sizeof(struct qlogicpti));
839     			if (!qpti_host) {
840     				printk("QPTI: Cannot register PTI Qlogic ISP SCSI host");
841     				continue;
842     			}
843     			qpti = (struct qlogicpti *) qpti_host->hostdata;
844     
845     			spin_lock_init(&qpti->lock);
846     
847     			/* We are wide capable, 16 targets. */
848     			qpti_host->max_id = MAX_TARGETS;
849     
850     			/* Setup back pointers and misc. state. */
851     			qpti->qhost = qpti_host;
852     			qpti->sdev = sdev;
853     			qpti->qpti_id = nqptis++;
854     			qpti->prom_node = sdev->prom_node;
855     			prom_getstring(qpti->prom_node, "name",
856     				       qpti->prom_name,
857     				       sizeof(qpti->prom_name));
858     
859     			/* This is not correct, actually. There's a switch
860     			 * on the PTI cards that put them into "emulation"
861     			 * mode- i.e., report themselves as QLGC,isp
862     			 * instead of PTI,ptisp. The only real substantive
863     			 * difference between non-pti and pti cards is
864     			 * the tmon register. Which is possibly even
865     			 * there for Qlogic cards, but non-functional.
866     			 */
867     			qpti->is_pti = (strcmp (qpti->prom_name, "QLGC,isp") != 0);
868     
869     			qpti_chain_add(qpti);
870     			if (qpti_map_regs(qpti) < 0)
871     				goto fail_unlink;
872     
873     			if (qpti_register_irq(qpti) < 0)
874     				goto fail_unmap_regs;
875     
876     			qpti_get_scsi_id(qpti);
877     			qpti_get_bursts(qpti);
878     			qpti_get_clock(qpti);
879     
880     			/* Clear out Scsi_Cmnd array. */
881     			memset(qpti->cmd_slots, 0, sizeof(qpti->cmd_slots));
882     
883     			if (qpti_map_queues(qpti) < 0)
884     				goto fail_free_irq;
885     
886     			/* Load the firmware. */
887     			if (qlogicpti_load_firmware(qpti))
888     				goto fail_unmap_queues;
889     			if (qpti->is_pti) {
890     				/* Check the PTI status reg. */
891     				if (qlogicpti_verify_tmon(qpti))
892     					goto fail_unmap_queues;
893     			}
894     
895     			/* Reset the ISP and init res/req queues. */
896     			if (qlogicpti_reset_hardware(qpti_host))
897     				goto fail_unmap_queues;
898     
899     			printk("(Firmware v%d.%d.%d)", qpti->fware_majrev,
900     			    qpti->fware_minrev, qpti->fware_micrev);
901     			{
902     				char buffer[60];
903     				
904     				prom_getstring (qpti->prom_node,
905     						"isp-fcode", buffer, 60);
906     				if (buffer[0])
907     					printk("(Firmware %s)", buffer);
908     				if (prom_getbool(qpti->prom_node, "differential"))
909     					qpti->differential = 1;
910     			}
911     			
912     			printk (" [%s Wide, using %s interface]\n",
913     			       (qpti->ultra ? "Ultra" : "Fast"),
914     			       (qpti->differential ? "differential" : "single ended"));
915     
916     			nqptis_in_use++;
917     			continue;
918     
919     		fail_unmap_queues:
920     #define QSIZE(entries)	(((entries) + 1) * QUEUE_ENTRY_LEN)
921     			sbus_free_consistent(qpti->sdev,
922     					     QSIZE(RES_QUEUE_LEN),
923     					     qpti->res_cpu, qpti->res_dvma);
924     			sbus_free_consistent(qpti->sdev,
925     					     QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
926     					     qpti->req_cpu, qpti->req_dvma);
927     #undef QSIZE
928     		fail_free_irq:
929     			free_irq(qpti->irq, qpti);
930     
931     		fail_unmap_regs:
932     			sbus_iounmap(qpti->qregs,
933     				     qpti->sdev->reg_addrs[0].reg_size);
934     			if (qpti->is_pti)
935     				sbus_iounmap(qpti->sreg, sizeof(unsigned char));
936     		fail_unlink:
937     			qpti_chain_del(qpti);
938     			scsi_unregister(qpti->qhost);
939     		}
940     	}
941     	if (nqptis)
942     		printk("QPTI: Total of %d PTI Qlogic/ISP hosts found, %d actually in use.\n",
943     		       nqptis, nqptis_in_use);
944     	qptis_running = nqptis_in_use;
945     	return nqptis;
946     }
947     
948     int qlogicpti_release(struct Scsi_Host *host)
949     {
950     	struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
951     
952     	/* Remove visibility from IRQ handlers. */
953     	qpti_chain_del(qpti);
954     
955     	/* Shut up the card. */
956     	sbus_writew(0, qpti->qregs + SBUS_CTRL);
957     
958     	/* Free IRQ handler and unmap Qlogic,ISP and PTI status regs. */
959     	free_irq(qpti->irq, qpti);
960     
961     #define QSIZE(entries)	(((entries) + 1) * QUEUE_ENTRY_LEN)
962     	sbus_free_consistent(qpti->sdev,
963     			     QSIZE(RES_QUEUE_LEN),
964     			     qpti->res_cpu, qpti->res_dvma);
965     	sbus_free_consistent(qpti->sdev,
966     			     QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
967     			     qpti->req_cpu, qpti->req_dvma);
968     #undef QSIZE
969     
970     	sbus_iounmap(qpti->qregs, qpti->sdev->reg_addrs[0].reg_size);
971     	if (qpti->is_pti)
972     		sbus_iounmap(qpti->sreg, sizeof(unsigned char));
973     
974     	return 0;
975     }
976     
977     const char *qlogicpti_info(struct Scsi_Host *host)
978     {
979     	static char buf[80];
980     	struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
981     
982     	sprintf(buf, "PTI Qlogic,ISP SBUS SCSI irq %s regs at %lx",
983     		__irq_itoa(qpti->qhost->irq), qpti->qregs);
984     	return buf;
985     }
986     
987     /* I am a certified frobtronicist. */
988     static inline void marker_frob(struct Command_Entry *cmd)
989     {
990     	struct Marker_Entry *marker = (struct Marker_Entry *) cmd;
991     
992     	memset(marker, 0, sizeof(struct Marker_Entry));
993     	marker->hdr.entry_cnt = 1;
994     	marker->hdr.entry_type = ENTRY_MARKER;
995     	marker->modifier = SYNC_ALL;
996     	marker->rsvd = 0;
997     }
998     
999     static inline void cmd_frob(struct Command_Entry *cmd, Scsi_Cmnd *Cmnd,
1000     			    struct qlogicpti *qpti)
1001     {
1002     	memset(cmd, 0, sizeof(struct Command_Entry));
1003     	cmd->hdr.entry_cnt = 1;
1004     	cmd->hdr.entry_type = ENTRY_COMMAND;
1005     	cmd->target_id = Cmnd->target;
1006     	cmd->target_lun = Cmnd->lun;
1007     	cmd->cdb_length = Cmnd->cmd_len;
1008     	cmd->control_flags = 0;
1009     	if (Cmnd->device->tagged_supported) {
1010     		if (qpti->cmd_count[Cmnd->target] == 0)
1011     			qpti->tag_ages[Cmnd->target] = jiffies;
1012     		if ((jiffies - qpti->tag_ages[Cmnd->target]) > (5*HZ)) {
1013     			cmd->control_flags = CFLAG_ORDERED_TAG;
1014     			qpti->tag_ages[Cmnd->target] = jiffies;
1015     		} else
1016     			cmd->control_flags = CFLAG_SIMPLE_TAG;
1017     	}
1018     	if ((Cmnd->cmnd[0] == WRITE_6) ||
1019     	    (Cmnd->cmnd[0] == WRITE_10) ||
1020     	    (Cmnd->cmnd[0] == WRITE_12))
1021     		cmd->control_flags |= CFLAG_WRITE;
1022     	else
1023     		cmd->control_flags |= CFLAG_READ;
1024     	cmd->time_out = 30;
1025     	memcpy(cmd->cdb, Cmnd->cmnd, Cmnd->cmd_len);
1026     }
1027     
1028     /* Do it to it baby. */
1029     static inline int load_cmd(Scsi_Cmnd *Cmnd, struct Command_Entry *cmd,
1030     			   struct qlogicpti *qpti, u_int in_ptr, u_int out_ptr)
1031     {
1032     	struct dataseg *ds;
1033     	struct scatterlist *sg;
1034     	int i, n;
1035     
1036     	if (Cmnd->use_sg) {
1037     		int sg_count;
1038     
1039     		sg = (struct scatterlist *) Cmnd->buffer;
1040     		sg_count = sbus_map_sg(qpti->sdev, sg, Cmnd->use_sg, scsi_to_sbus_dma_dir(Cmnd->sc_data_direction));
1041     
1042     		ds = cmd->dataseg;
1043     		cmd->segment_cnt = sg_count;
1044     
1045     		/* Fill in first four sg entries: */
1046     		n = sg_count;
1047     		if (n > 4)
1048     			n = 4;
1049     		for (i = 0; i < n; i++, sg++) {
1050     			ds[i].d_base = sg_dma_address(sg);
1051     			ds[i].d_count = sg_dma_len(sg);
1052     		}
1053     		sg_count -= 4;
1054     		while (sg_count > 0) {
1055     			struct Continuation_Entry *cont;
1056     
1057     			++cmd->hdr.entry_cnt;
1058     			cont = (struct Continuation_Entry *) &qpti->req_cpu[in_ptr];
1059     			in_ptr = NEXT_REQ_PTR(in_ptr);
1060     			if (in_ptr == out_ptr)
1061     				return -1;
1062     
1063     			cont->hdr.entry_type = ENTRY_CONTINUATION;
1064     			cont->hdr.entry_cnt = 0;
1065     			cont->hdr.sys_def_1 = 0;
1066     			cont->hdr.flags = 0;
1067     			cont->reserved = 0;
1068     			ds = cont->dataseg;
1069     			n = sg_count;
1070     			if (n > 7)
1071     				n = 7;
1072     			for (i = 0; i < n; i++, sg++) {
1073     				ds[i].d_base = sg_dma_address(sg);
1074     				ds[i].d_count = sg_dma_len(sg);
1075     			}
1076     			sg_count -= n;
1077     		}
1078     	} else if (Cmnd->request_bufflen) {
1079     		Cmnd->SCp.ptr = (char *)(unsigned long)
1080     			sbus_map_single(qpti->sdev,
1081     					Cmnd->request_buffer,
1082     					Cmnd->request_bufflen,
1083     					scsi_to_sbus_dma_dir(Cmnd->sc_data_direction));
1084     
1085     		cmd->dataseg[0].d_base = (u32) ((unsigned long)Cmnd->SCp.ptr);
1086     		cmd->dataseg[0].d_count = Cmnd->request_bufflen;
1087     		cmd->segment_cnt = 1;
1088     	} else {
1089     		cmd->dataseg[0].d_base = 0;
1090     		cmd->dataseg[0].d_count = 0;
1091     		cmd->segment_cnt = 1; /* Shouldn't this be 0? */
1092     	}
1093     
1094     	/* Committed, record Scsi_Cmd so we can find it later. */
1095     	cmd->handle = in_ptr;
1096     	qpti->cmd_slots[in_ptr] = Cmnd;
1097     
1098     	qpti->cmd_count[Cmnd->target]++;
1099     	sbus_writew(in_ptr, qpti->qregs + MBOX4);
1100     	qpti->req_in_ptr = in_ptr;
1101     
1102     	return in_ptr;
1103     }
1104     
1105     static inline void update_can_queue(struct Scsi_Host *host, u_int in_ptr, u_int out_ptr)
1106     {
1107     	/* Temporary workaround until bug is found and fixed (one bug has been found
1108     	   already, but fixing it makes things even worse) -jj */
1109     	int num_free = QLOGICPTI_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr) - 64;
1110     	host->can_queue = host->host_busy + num_free;
1111     	host->sg_tablesize = QLOGICPTI_MAX_SG(num_free);
1112     }
1113     
1114     /*
1115      * Until we scan the entire bus with inquiries, go throught this fella...
1116      */
1117     static void ourdone(Scsi_Cmnd *Cmnd)
1118     {
1119     	struct qlogicpti *qpti = (struct qlogicpti *) Cmnd->host->hostdata;
1120     	int tgt = Cmnd->target;
1121     	void (*done) (Scsi_Cmnd *);
1122     
1123     	/* This grot added by DaveM, blame him for ugliness.
1124     	 * The issue is that in the 2.3.x driver we use the
1125     	 * host_scribble portion of the scsi command as a
1126     	 * completion linked list at interrupt service time,
1127     	 * so we have to store the done function pointer elsewhere.
1128     	 */
1129     	done = (void (*)(Scsi_Cmnd *))
1130     		(((unsigned long) Cmnd->SCp.Message)
1131     #ifdef __sparc_v9__
1132     		 | ((unsigned long) Cmnd->SCp.Status << 32UL)
1133     #endif
1134     		 );
1135     
1136     	if ((qpti->sbits & (1 << tgt)) == 0) {
1137     		int ok = host_byte(Cmnd->result) == DID_OK;
1138     		if (Cmnd->cmnd[0] == 0x12 && ok) {
1139     			unsigned char *iqd;
1140     			if (Cmnd->use_sg == 0) {
1141     				iqd = ((unsigned char *)Cmnd->buffer);
1142     			} else {
1143     				iqd = ((struct scatterlist *) Cmnd->request_buffer)->address;
1144     			}
1145     			/* tags handled in midlayer */
1146     			/* enable sync mode? */
1147     			if (iqd[7] & 0x10) {
1148     				qpti->dev_param[tgt].device_flags |= 0x10;
1149     			} else {
1150     				qpti->dev_param[tgt].synchronous_offset = 0;
1151     				qpti->dev_param[tgt].synchronous_period = 0;
1152     			}
1153     			/* are we wide capable? */
1154     			if (iqd[7] & 0x20) {
1155     				qpti->dev_param[tgt].device_flags |= 0x20;
1156     			}
1157     			qpti->sbits |= (1 << tgt);
1158     		} else if (!ok) {
1159     			qpti->sbits |= (1 << tgt);
1160     		}
1161     	}
1162     	done(Cmnd);
1163     }
1164     
1165     int qlogicpti_queuecommand_slow(Scsi_Cmnd *Cmnd, void (*done)(Scsi_Cmnd *))
1166     {
1167     	unsigned long flags;
1168     	struct qlogicpti *qpti = (struct qlogicpti *) Cmnd->host->hostdata;
1169     
1170     	/*
1171     	 * done checking this host adapter?
1172     	 * If not, then rewrite the command
1173     	 * to finish through ourdone so we
1174     	 * can peek at Inquiry data results.
1175     	 */
1176     	if (qpti->sbits && qpti->sbits != 0xffff) {
1177     		/* See above about in ourdone this ugliness... */
1178     		Cmnd->SCp.Message = ((unsigned long)done) & 0xffffffff;
1179     #ifdef __sparc_v9__
1180     		Cmnd->SCp.Status = ((unsigned long)done >> 32UL) & 0xffffffff;
1181     #endif
1182     		return qlogicpti_queuecommand(Cmnd, ourdone);
1183     	}
1184     	save_flags(flags); cli();
1185     
1186     	/*
1187     	 * We've peeked at all targets for this bus- time
1188     	 * to set parameters for devices for real now.
1189     	 */
1190     	if (qpti->sbits == 0xffff) {
1191     		int i;
1192     		for(i = 0; i < MAX_TARGETS; i++) {
1193     			u_short param[6];
1194     			param[0] = MBOX_SET_TARGET_PARAMS;
1195     			param[1] = (i << 8);
1196     			param[2] = (qpti->dev_param[i].device_flags << 8);
1197     			if (qpti->dev_param[i].device_flags & 0x10) {
1198     				param[3] = (qpti->dev_param[i].synchronous_offset << 8) |
1199     					qpti->dev_param[i].synchronous_period;
1200     			} else {
1201     				param[3] = 0;
1202     			}
1203     			(void) qlogicpti_mbox_command(qpti, param, 0);
1204     		}
1205     		/*
1206     		 * set to zero so any traverse through ourdone
1207     		 * doesn't start the whole process again,
1208     		 */
1209     		qpti->sbits = 0;
1210     	}
1211     
1212     	/* check to see if we're done with all adapters... */
1213     	for (qpti = qptichain; qpti != NULL; qpti = qpti->next) {
1214     		if (qpti->sbits) {
1215     			break;
1216     		}
1217     	}
1218     
1219     	/*
1220     	 * if we hit the end of the chain w/o finding adapters still
1221     	 * capability-configuring, then we're done with all adapters
1222     	 * and can rock on..
1223     	 */
1224     	if (qpti == NULL)
1225     		Cmnd->host->hostt->queuecommand = qlogicpti_queuecommand;
1226     
1227     	restore_flags(flags);
1228     	return qlogicpti_queuecommand(Cmnd, done);
1229     }
1230     
1231     /*
1232      * The middle SCSI layer ensures that queuecommand never gets invoked
1233      * concurrently with itself or the interrupt handler (though the
1234      * interrupt handler may call this routine as part of
1235      * request-completion handling).
1236      *
1237      * "This code must fly." -davem
1238      */
1239     int qlogicpti_queuecommand(Scsi_Cmnd *Cmnd, void (*done)(Scsi_Cmnd *))
1240     {
1241     	struct Scsi_Host *host = Cmnd->host;
1242     	struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
1243     	struct Command_Entry *cmd;
1244     	unsigned long flags;
1245     	u_int out_ptr;
1246     	int in_ptr;
1247     
1248     	Cmnd->scsi_done = done;
1249     
1250     	spin_lock_irqsave(&qpti->lock, flags);
1251     
1252     	in_ptr = qpti->req_in_ptr;
1253     	cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
1254     	out_ptr = sbus_readw(qpti->qregs + MBOX4);
1255     	in_ptr = NEXT_REQ_PTR(in_ptr);
1256     	if (in_ptr == out_ptr)
1257     		goto toss_command;
1258     
1259     	if (qpti->send_marker) {
1260     		marker_frob(cmd);
1261     		qpti->send_marker = 0;
1262     		if (NEXT_REQ_PTR(in_ptr) == out_ptr) {
1263     			sbus_writew(in_ptr, qpti->qregs + MBOX4);
1264     			qpti->req_in_ptr = in_ptr;
1265     			goto toss_command;
1266     		}
1267     		cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
1268     		in_ptr = NEXT_REQ_PTR(in_ptr);
1269     	}
1270     	cmd_frob(cmd, Cmnd, qpti);
1271     	if ((in_ptr = load_cmd(Cmnd, cmd, qpti, in_ptr, out_ptr)) == -1)
1272     		goto toss_command;
1273     
1274     	update_can_queue(host, in_ptr, out_ptr);
1275     
1276     	spin_unlock_irqrestore(&qpti->lock, flags);
1277     	return 0;
1278     
1279     toss_command:
1280     	printk(KERN_EMERG "qlogicpti%d: request queue overflow\n",
1281     	       qpti->qpti_id);
1282     
1283     	spin_unlock_irqrestore(&qpti->lock, flags);
1284     
1285     	/* Unfortunately, unless you use the new EH code, which
1286     	 * we don't, the midlayer will ignore the return value,
1287     	 * which is insane.  We pick up the pieces like this.
1288     	 */
1289     	Cmnd->result = DID_BUS_BUSY;
1290     	done(Cmnd);
1291     	return 1;
1292     }
1293     
1294     static int qlogicpti_return_status(struct Status_Entry *sts, int id)
1295     {
1296     	int host_status = DID_ERROR;
1297     
1298     	switch (sts->completion_status) {
1299     	      case CS_COMPLETE:
1300     		host_status = DID_OK;
1301     		break;
1302     	      case CS_INCOMPLETE:
1303     		if (!(sts->state_flags & SF_GOT_BUS))
1304     			host_status = DID_NO_CONNECT;
1305     		else if (!(sts->state_flags & SF_GOT_TARGET))
1306     			host_status = DID_BAD_TARGET;
1307     		else if (!(sts->state_flags & SF_SENT_CDB))
1308     			host_status = DID_ERROR;
1309     		else if (!(sts->state_flags & SF_TRANSFERRED_DATA))
1310     			host_status = DID_ERROR;
1311     		else if (!(sts->state_flags & SF_GOT_STATUS))
1312     			host_status = DID_ERROR;
1313     		else if (!(sts->state_flags & SF_GOT_SENSE))
1314     			host_status = DID_ERROR;
1315     		break;
1316     	      case CS_DMA_ERROR:
1317     	      case CS_TRANSPORT_ERROR:
1318     		host_status = DID_ERROR;
1319     		break;
1320     	      case CS_RESET_OCCURRED:
1321     	      case CS_BUS_RESET:
1322     		host_status = DID_RESET;
1323     		break;
1324     	      case CS_ABORTED:
1325     		host_status = DID_ABORT;
1326     		break;
1327     	      case CS_TIMEOUT:
1328     		host_status = DID_TIME_OUT;
1329     		break;
1330     	      case CS_DATA_OVERRUN:
1331     	      case CS_COMMAND_OVERRUN:
1332     	      case CS_STATUS_OVERRUN:
1333     	      case CS_BAD_MESSAGE:
1334     	      case CS_NO_MESSAGE_OUT:
1335     	      case CS_EXT_ID_FAILED:
1336     	      case CS_IDE_MSG_FAILED:
1337     	      case CS_ABORT_MSG_FAILED:
1338     	      case CS_NOP_MSG_FAILED:
1339     	      case CS_PARITY_ERROR_MSG_FAILED:
1340     	      case CS_DEVICE_RESET_MSG_FAILED:
1341     	      case CS_ID_MSG_FAILED:
1342     	      case CS_UNEXP_BUS_FREE:
1343     		host_status = DID_ERROR;
1344     		break;
1345     	      case CS_DATA_UNDERRUN:
1346     		host_status = DID_OK;
1347     		break;
1348     	      default:
1349     		printk(KERN_EMERG "qpti%d: unknown completion status 0x%04x\n",
1350     		       id, sts->completion_status);
1351     		host_status = DID_ERROR;
1352     		break;
1353     	}
1354     
1355     	return (sts->scsi_status & STATUS_MASK) | (host_status << 16);
1356     }
1357     
1358     static Scsi_Cmnd *qlogicpti_intr_handler(struct qlogicpti *qpti)
1359     {
1360     	Scsi_Cmnd *Cmnd, *done_queue = NULL;
1361     	struct Status_Entry *sts;
1362     	u_int in_ptr, out_ptr;
1363     
1364     	if (!(sbus_readw(qpti->qregs + SBUS_STAT) & SBUS_STAT_RINT))
1365     		return NULL;
1366     		
1367     	in_ptr = sbus_readw(qpti->qregs + MBOX5);
1368     	sbus_writew(HCCTRL_CRIRQ, qpti->qregs + HCCTRL);
1369     	if (sbus_readw(qpti->qregs + SBUS_SEMAPHORE) & SBUS_SEMAPHORE_LCK) {
1370     		switch (sbus_readw(qpti->qregs + MBOX0)) {
1371     		case ASYNC_SCSI_BUS_RESET:
1372     		case EXECUTION_TIMEOUT_RESET:
1373     			qpti->send_marker = 1;
1374     			break;
1375     		case INVALID_COMMAND:
1376     		case HOST_INTERFACE_ERROR:
1377     		case COMMAND_ERROR:
1378     		case COMMAND_PARAM_ERROR:
1379     			break;
1380     		};
1381     		sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
1382     	}
1383     
1384     	/* This looks like a network driver! */
1385     	out_ptr = qpti->res_out_ptr;
1386     	while (out_ptr != in_ptr) {
1387     		u_int cmd_slot;
1388     
1389     		sts = (struct Status_Entry *) &qpti->res_cpu[out_ptr];
1390     		out_ptr = NEXT_RES_PTR(out_ptr);
1391     
1392     		/* We store an index in the handle, not the pointer in
1393     		 * some form.  This avoids problems due to the fact
1394     		 * that the handle provided is only 32-bits. -DaveM
1395     		 */
1396     		cmd_slot = sts->handle;
1397     		Cmnd = qpti->cmd_slots[cmd_slot];
1398     		qpti->cmd_slots[cmd_slot] = NULL;
1399     
1400     		if (sts->completion_status == CS_RESET_OCCURRED ||
1401     		    sts->completion_status == CS_ABORTED ||
1402     		    (sts->status_flags & STF_BUS_RESET))
1403     			qpti->send_marker = 1;
1404     
1405     		if (sts->state_flags & SF_GOT_SENSE)
1406     			memcpy(Cmnd->sense_buffer, sts->req_sense_data,
1407     			       sizeof(Cmnd->sense_buffer));
1408     
1409     		if (sts->hdr.entry_type == ENTRY_STATUS)
1410     			Cmnd->result =
1411     			    qlogicpti_return_status(sts, qpti->qpti_id);
1412     		else
1413     			Cmnd->result = DID_ERROR << 16;
1414     
1415     		if (Cmnd->use_sg) {
1416     			sbus_unmap_sg(qpti->sdev,
1417     				      (struct scatterlist *)Cmnd->buffer,
1418     				      Cmnd->use_sg,
1419     				      scsi_to_sbus_dma_dir(Cmnd->sc_data_direction));
1420     		} else {
1421     			sbus_unmap_single(qpti->sdev,
1422     					  (__u32)((unsigned long)Cmnd->SCp.ptr),
1423     					  Cmnd->request_bufflen,
1424     					  scsi_to_sbus_dma_dir(Cmnd->sc_data_direction));
1425     		}
1426     		qpti->cmd_count[Cmnd->target]--;
1427     		sbus_writew(out_ptr, qpti->qregs + MBOX5);
1428     		Cmnd->host_scribble = (unsigned char *) done_queue;
1429     		done_queue = Cmnd;
1430     	}
1431     	qpti->res_out_ptr = out_ptr;
1432     
1433     	return done_queue;
1434     }
1435     
1436     static void qpti_intr(int irq, void *dev_id, struct pt_regs *regs)
1437     {
1438     	struct qlogicpti *qpti = dev_id;
1439     	unsigned long flags;
1440     	Scsi_Cmnd *dq;
1441     
1442     	spin_lock_irqsave(&qpti->lock, flags);
1443     	dq = qlogicpti_intr_handler(qpti);
1444     	spin_unlock(&qpti->lock);
1445     
1446     	if (dq != NULL) {
1447     		spin_lock(&io_request_lock);
1448     		do {
1449     			Scsi_Cmnd *next;
1450     
1451     			next = (Scsi_Cmnd *) dq->host_scribble;
1452     			dq->scsi_done(dq);
1453     			dq = next;
1454     		} while (dq != NULL);
1455     		spin_unlock(&io_request_lock);
1456     	}
1457     	__restore_flags(flags);
1458     }
1459     
1460     int qlogicpti_abort(Scsi_Cmnd *Cmnd)
1461     {
1462     	u_short param[6];
1463     	struct Scsi_Host *host = Cmnd->host;
1464     	struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
1465     	int return_status = SCSI_ABORT_SUCCESS;
1466     	unsigned long flags;
1467     	u32 cmd_cookie;
1468     	int i;
1469     
1470     	printk(KERN_WARNING "qlogicpti : Aborting cmd for tgt[%d] lun[%d]\n",
1471     	       (int)Cmnd->target, (int)Cmnd->lun);
1472     
1473     	spin_lock_irqsave(&qpti->lock, flags);
1474     
1475     	qlogicpti_disable_irqs(qpti);
1476     
1477     	/* Find the 32-bit cookie we gave to the firmware for
1478     	 * this command.
1479     	 */
1480     	for (i = 0; i < QLOGICPTI_REQ_QUEUE_LEN + 1; i++)
1481     		if (qpti->cmd_slots[i] == Cmnd)
1482     			break;
1483     	cmd_cookie = i;
1484     
1485     	param[0] = MBOX_ABORT;
1486     	param[1] = (((u_short) Cmnd->target) << 8) | Cmnd->lun;
1487     	param[2] = cmd_cookie >> 16;
1488     	param[3] = cmd_cookie & 0xffff;
1489     	if (qlogicpti_mbox_command(qpti, param, 0) ||
1490     	    (param[0] != MBOX_COMMAND_COMPLETE)) {
1491     		printk(KERN_EMERG "qlogicpti : scsi abort failure: %x\n", param[0]);
1492     		return_status = SCSI_ABORT_ERROR;
1493     	}
1494     
1495     	qlogicpti_enable_irqs(qpti);
1496     
1497     	spin_unlock_irqrestore(&qpti->lock, flags);
1498     
1499     	return return_status;
1500     }
1501     
1502     int qlogicpti_reset(Scsi_Cmnd *Cmnd, unsigned int reset_flags)
1503     {
1504     	u_short param[6];
1505     	struct Scsi_Host *host = Cmnd->host;
1506     	struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
1507     	int return_status = SCSI_RESET_SUCCESS;
1508     	unsigned long flags;
1509     
1510     	printk(KERN_WARNING "qlogicpti : Resetting SCSI bus!\n");
1511     
1512     	spin_lock_irqsave(&qpti->lock, flags);
1513     
1514     	qlogicpti_disable_irqs(qpti);
1515     
1516     	param[0] = MBOX_BUS_RESET;
1517     	param[1] = qpti->host_param.bus_reset_delay;
1518     	if (qlogicpti_mbox_command(qpti, param, 0) ||
1519     	   (param[0] != MBOX_COMMAND_COMPLETE)) {
1520     		printk(KERN_EMERG "qlogicisp : scsi bus reset failure: %x\n", param[0]);
1521     		return_status = SCSI_RESET_ERROR;
1522     	}
1523     
1524     	qlogicpti_enable_irqs(qpti);
1525     
1526     	spin_unlock_irqrestore(&qpti->lock, flags);
1527     
1528     	return return_status;
1529     }
1530     
1531     static Scsi_Host_Template driver_template = QLOGICPTI;
1532     
1533     #include "scsi_module.c"
1534     
1535     EXPORT_NO_SYMBOLS;
1536