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