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
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/blkdev.h>
21 #include <linux/proc_fs.h>
22 #include <linux/stat.h>
23 #include <linux/init.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
27 #include <asm/byteorder.h>
29 #include "qlogicpti.h"
33 #include <asm/system.h>
34 #include <asm/ptrace.h>
35 #include <asm/pgtable.h>
36 #include <asm/oplib.h>
40 #include <linux/module.h>
42 #define MAX_TARGETS 16
43 #define MAX_LUNS 8 /* 32 for 1.31 F/W */
45 #define DEFAULT_LOOP_COUNT 10000
47 #include "qlogicpti_asm.c"
49 static struct qlogicpti *qptichain = NULL;
50 static spinlock_t qptichain_lock = SPIN_LOCK_UNLOCKED;
51 static int qptis_running = 0;
53 #define PACKB(a, b) (((a)<<4)|(b))
55 static const u_char mbox_param[] = {
56 PACKB(1, 1), /* MBOX_NO_OP */
57 PACKB(5, 5), /* MBOX_LOAD_RAM */
58 PACKB(2, 0), /* MBOX_EXEC_FIRMWARE */
59 PACKB(5, 5), /* MBOX_DUMP_RAM */
60 PACKB(3, 3), /* MBOX_WRITE_RAM_WORD */
61 PACKB(2, 3), /* MBOX_READ_RAM_WORD */
62 PACKB(6, 6), /* MBOX_MAILBOX_REG_TEST */
63 PACKB(2, 3), /* MBOX_VERIFY_CHECKSUM */
64 PACKB(1, 3), /* MBOX_ABOUT_FIRMWARE */
65 PACKB(0, 0), /* 0x0009 */
66 PACKB(0, 0), /* 0x000a */
67 PACKB(0, 0), /* 0x000b */
68 PACKB(0, 0), /* 0x000c */
69 PACKB(0, 0), /* 0x000d */
70 PACKB(1, 2), /* MBOX_CHECK_FIRMWARE */
71 PACKB(0, 0), /* 0x000f */
72 PACKB(5, 5), /* MBOX_INIT_REQ_QUEUE */
73 PACKB(6, 6), /* MBOX_INIT_RES_QUEUE */
74 PACKB(4, 4), /* MBOX_EXECUTE_IOCB */
75 PACKB(2, 2), /* MBOX_WAKE_UP */
76 PACKB(1, 6), /* MBOX_STOP_FIRMWARE */
77 PACKB(4, 4), /* MBOX_ABORT */
78 PACKB(2, 2), /* MBOX_ABORT_DEVICE */
79 PACKB(3, 3), /* MBOX_ABORT_TARGET */
80 PACKB(2, 2), /* MBOX_BUS_RESET */
81 PACKB(2, 3), /* MBOX_STOP_QUEUE */
82 PACKB(2, 3), /* MBOX_START_QUEUE */
83 PACKB(2, 3), /* MBOX_SINGLE_STEP_QUEUE */
84 PACKB(2, 3), /* MBOX_ABORT_QUEUE */
85 PACKB(2, 4), /* MBOX_GET_DEV_QUEUE_STATUS */
86 PACKB(0, 0), /* 0x001e */
87 PACKB(1, 3), /* MBOX_GET_FIRMWARE_STATUS */
88 PACKB(1, 2), /* MBOX_GET_INIT_SCSI_ID */
89 PACKB(1, 2), /* MBOX_GET_SELECT_TIMEOUT */
90 PACKB(1, 3), /* MBOX_GET_RETRY_COUNT */
91 PACKB(1, 2), /* MBOX_GET_TAG_AGE_LIMIT */
92 PACKB(1, 2), /* MBOX_GET_CLOCK_RATE */
93 PACKB(1, 2), /* MBOX_GET_ACT_NEG_STATE */
94 PACKB(1, 2), /* MBOX_GET_ASYNC_DATA_SETUP_TIME */
95 PACKB(1, 3), /* MBOX_GET_SBUS_PARAMS */
96 PACKB(2, 4), /* MBOX_GET_TARGET_PARAMS */
97 PACKB(2, 4), /* MBOX_GET_DEV_QUEUE_PARAMS */
98 PACKB(0, 0), /* 0x002a */
99 PACKB(0, 0), /* 0x002b */
100 PACKB(0, 0), /* 0x002c */
101 PACKB(0, 0), /* 0x002d */
102 PACKB(0, 0), /* 0x002e */
103 PACKB(0, 0), /* 0x002f */
104 PACKB(2, 2), /* MBOX_SET_INIT_SCSI_ID */
105 PACKB(2, 2), /* MBOX_SET_SELECT_TIMEOUT */
106 PACKB(3, 3), /* MBOX_SET_RETRY_COUNT */
107 PACKB(2, 2), /* MBOX_SET_TAG_AGE_LIMIT */
108 PACKB(2, 2), /* MBOX_SET_CLOCK_RATE */
109 PACKB(2, 2), /* MBOX_SET_ACTIVE_NEG_STATE */
110 PACKB(2, 2), /* MBOX_SET_ASYNC_DATA_SETUP_TIME */
111 PACKB(3, 3), /* MBOX_SET_SBUS_CONTROL_PARAMS */
112 PACKB(4, 4), /* MBOX_SET_TARGET_PARAMS */
113 PACKB(4, 4), /* MBOX_SET_DEV_QUEUE_PARAMS */
114 PACKB(0, 0), /* 0x003a */
115 PACKB(0, 0), /* 0x003b */
116 PACKB(0, 0), /* 0x003c */
117 PACKB(0, 0), /* 0x003d */
118 PACKB(0, 0), /* 0x003e */
119 PACKB(0, 0), /* 0x003f */
120 PACKB(0, 0), /* 0x0040 */
121 PACKB(0, 0), /* 0x0041 */
122 PACKB(0, 0) /* 0x0042 */
125 #define MAX_MBOX_COMMAND (sizeof(mbox_param)/sizeof(u_short))
127 /* queue length's _must_ be power of two: */
128 #define QUEUE_DEPTH(in, out, ql) ((in - out) & (ql))
129 #define REQ_QUEUE_DEPTH(in, out) QUEUE_DEPTH(in, out, \
130 QLOGICPTI_REQ_QUEUE_LEN)
131 #define RES_QUEUE_DEPTH(in, out) QUEUE_DEPTH(in, out, RES_QUEUE_LEN)
133 static inline void qlogicpti_enable_irqs(struct qlogicpti *qpti)
135 sbus_writew(SBUS_CTRL_ERIRQ | SBUS_CTRL_GENAB,
136 qpti->qregs + SBUS_CTRL);
139 static inline void qlogicpti_disable_irqs(struct qlogicpti *qpti)
141 sbus_writew(0, qpti->qregs + SBUS_CTRL);
144 static inline void set_sbus_cfg1(struct qlogicpti *qpti)
147 u8 bursts = qpti->bursts;
149 #if 0 /* It appears that at least PTI cards do not support
150 * 64-byte bursts and that setting the B64 bit actually
151 * is a nop and the chip ends up using the smallest burst
154 if (sbus_can_burst64(qpti->sdev) && (bursts & DMA_BURST64)) {
155 val = (SBUS_CFG1_BENAB | SBUS_CFG1_B64);
158 if (bursts & DMA_BURST32) {
159 val = (SBUS_CFG1_BENAB | SBUS_CFG1_B32);
160 } else if (bursts & DMA_BURST16) {
161 val = (SBUS_CFG1_BENAB | SBUS_CFG1_B16);
162 } else if (bursts & DMA_BURST8) {
163 val = (SBUS_CFG1_BENAB | SBUS_CFG1_B8);
165 val = 0; /* No sbus bursts for you... */
167 sbus_writew(val, qpti->qregs + SBUS_CFG1);
170 static int qlogicpti_mbox_command(struct qlogicpti *qpti, u_short param[], int force)
175 if (mbox_param[param[0]] == 0)
178 /* Set SBUS semaphore. */
179 tmp = sbus_readw(qpti->qregs + SBUS_SEMAPHORE);
180 tmp |= SBUS_SEMAPHORE_LCK;
181 sbus_writew(tmp, qpti->qregs + SBUS_SEMAPHORE);
183 /* Wait for host IRQ bit to clear. */
184 loop_count = DEFAULT_LOOP_COUNT;
185 while (--loop_count && (sbus_readw(qpti->qregs + HCCTRL) & HCCTRL_HIRQ)) {
190 printk(KERN_EMERG "qlogicpti: mbox_command loop timeout #1\n");
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);
202 /* Clear RISC interrupt. */
203 tmp = sbus_readw(qpti->qregs + HCCTRL);
205 sbus_writew(tmp, qpti->qregs + HCCTRL);
207 /* Clear SBUS semaphore. */
208 sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
210 /* Set HOST interrupt. */
211 tmp = sbus_readw(qpti->qregs + HCCTRL);
213 sbus_writew(tmp, qpti->qregs + HCCTRL);
215 /* Wait for HOST interrupt clears. */
216 loop_count = DEFAULT_LOOP_COUNT;
217 while (--loop_count &&
218 (sbus_readw(qpti->qregs + HCCTRL) & HCCTRL_CRIRQ))
221 printk(KERN_EMERG "qlogicpti: mbox_command[%04x] loop timeout #2\n",
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)) {
230 /* Workaround for some buggy chips. */
231 if (sbus_readw(qpti->qregs + MBOX0) & 0x4000)
235 printk(KERN_EMERG "qlogicpti: mbox_command[%04x] loop timeout #3\n",
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))
243 printk(KERN_EMERG "qlogicpti: mbox_command[%04x] loop timeout #4\n",
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);
256 /* Clear RISC interrupt. */
257 tmp = sbus_readw(qpti->qregs + HCCTRL);
259 sbus_writew(tmp, qpti->qregs + HCCTRL);
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);
270 static inline void qlogicpti_set_hostdev_defaults(struct qlogicpti *qpti)
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;
287 for(i = 0; i < MAX_TARGETS; i++) {
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.
295 qpti->dev_param[i].device_flags = 0xcd;
296 qpti->dev_param[i].execution_throttle = 16;
298 qpti->dev_param[i].synchronous_period = 12;
299 qpti->dev_param[i].synchronous_offset = 8;
301 qpti->dev_param[i].synchronous_period = 25;
302 qpti->dev_param[i].synchronous_offset = 12;
304 qpti->dev_param[i].device_enable = 1;
306 /* this is very important to set! */
307 qpti->sbits = 1 << qpti->scsi_id;
310 static int qlogicpti_reset_hardware(struct Scsi_Host *host)
312 struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
314 unsigned short risc_code_addr;
318 risc_code_addr = 0x1000; /* all load addresses are at 0x1000 */
320 spin_lock_irqsave(host->host_lock, flags);
322 sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
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);
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);
335 loop_count = DEFAULT_LOOP_COUNT;
336 while (--loop_count && ((sbus_readw(qpti->qregs + MBOX0) & 0xff) == 0x04))
339 printk(KERN_EMERG "qlogicpti: reset_hardware loop timeout\n");
341 sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
343 qlogicpti_enable_irqs(qpti);
345 if (sbus_readw(qpti->qregs + RISC_PSR) & RISC_PSR_ULTRA) {
347 sbus_writew((RISC_MTREG_P0ULTRA | RISC_MTREG_P1ULTRA),
348 qpti->qregs + RISC_MTREG);
351 sbus_writew((RISC_MTREG_P0DFLT | RISC_MTREG_P1DFLT),
352 qpti->qregs + RISC_MTREG);
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);
359 /* Release the RISC processor. */
360 sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
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",
368 spin_unlock_irqrestore(host->host_lock, flags);
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",
379 spin_unlock_irqrestore(host->host_lock, flags);
383 /* Initialize state of the queues, both hw and sw. */
384 qpti->req_in_ptr = qpti->res_out_ptr = 0;
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",
394 spin_unlock_irqrestore(host->host_lock, flags);
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",
406 spin_unlock_irqrestore(host->host_lock, flags);
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);
415 param[0] = MBOX_SET_TAG_AGE_LIMIT;
416 param[1] = qpti->host_param.tag_aging;
417 qlogicpti_mbox_command(qpti, param, 0);
419 for (i = 0; i < MAX_TARGETS; i++) {
420 param[0] = MBOX_GET_DEV_QUEUE_PARAMS;
422 qlogicpti_mbox_command(qpti, param, 0);
425 param[0] = MBOX_GET_FIRMWARE_STATUS;
426 qlogicpti_mbox_command(qpti, param, 0);
428 param[0] = MBOX_SET_SELECT_TIMEOUT;
429 param[1] = qpti->host_param.selection_timeout;
430 qlogicpti_mbox_command(qpti, param, 0);
432 for (i = 0; i < MAX_TARGETS; i++) {
433 param[0] = MBOX_SET_TARGET_PARAMS;
435 param[2] = (qpti->dev_param[i].device_flags << 8);
437 * Since we're now loading 1.31 f/w, force narrow/async.
440 param[3] = 0; /* no offset, we do not have sync mode yet */
441 qlogicpti_mbox_command(qpti, param, 0);
445 * Always (sigh) do an initial bus reset (kicks f/w).
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;
452 spin_unlock_irqrestore(host->host_lock, flags);
456 #define PTI_RESET_LIMIT 400
458 static int __init qlogicpti_load_firmware(struct qlogicpti *qpti)
460 struct Scsi_Host *host = qpti->qhost;
461 unsigned short csum = 0;
462 unsigned short param[6];
463 unsigned short *risc_code, risc_code_addr, risc_code_length;
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 spin_lock_irqsave(host->host_lock, flags);
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];
479 spin_unlock_irqrestore(host->host_lock, flags);
480 printk(KERN_EMERG "qlogicpti%d: Aieee, firmware checksum failed!",
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))
491 spin_unlock_irqrestore(host->host_lock, flags);
492 printk(KERN_EMERG "qlogicpti%d: Cannot reset the ISP.", qpti->qpti_id);
496 sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
499 sbus_writew((SBUS_CTRL_GENAB | SBUS_CTRL_ERIRQ), qpti->qregs + SBUS_CTRL);
501 sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
503 if (sbus_readw(qpti->qregs + RISC_PSR) & RISC_PSR_ULTRA) {
505 sbus_writew((RISC_MTREG_P0ULTRA | RISC_MTREG_P1ULTRA),
506 qpti->qregs + RISC_MTREG);
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;
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",
531 spin_unlock_irqrestore(host->host_lock, flags);
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",
544 spin_unlock_irqrestore(host->host_lock, flags);
549 /* Reset the ISP again. */
550 sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
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",
564 spin_unlock_irqrestore(host->host_lock, flags);
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",
578 spin_unlock_irqrestore(host->host_lock, flags);
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",
594 spin_unlock_irqrestore(host->host_lock, flags);
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;
602 param[2] = (unsigned short) qpti->scsi_id;
603 qlogicpti_mbox_command(qpti, param, 1);
605 param[0] = MBOX_WRITE_RAM_WORD;
607 param[2] = (unsigned short) 3;
608 qlogicpti_mbox_command(qpti, param, 1);
611 spin_unlock_irqrestore(host->host_lock, flags);
615 static int qlogicpti_verify_tmon(struct qlogicpti *qpti)
617 int curstat = sbus_readb(qpti->sreg);
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) {
626 if (curstat & SREG_FUSE) {
628 printk("qlogicpti%d: Fuse is open!\n", qpti->qpti_id);
630 if (curstat & SREG_TPOWER) {
632 printk("qlogicpti%d: termpwr failure\n", qpti->qpti_id);
634 if (qpti->differential &&
635 (curstat & SREG_DSENSE) != SREG_DSENSE) {
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;
646 static irqreturn_t 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;
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;
670 struct qlogicpti *qlink = qptichain;
671 while(qlink->next != qpti)
673 qlink->next = qpti->next;
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,
687 printk("PTI: Qlogic/ISP registers are unmappable\n");
691 qpti->sreg = sbus_ioremap(&sdev->resource[0], (16 * 4096),
692 sizeof(unsigned char),
693 "PTI Qlogic/ISP statreg");
695 printk("PTI: Qlogic/ISP status register is unmappable\n");
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
714 if (request_irq(qpti->irq, qpti_intr,
715 SA_SHIRQ, "Qlogic/PTI", qpti))
718 printk("qpti%d: IRQ %s ", qpti->qpti_id, __irq_itoa(qpti->irq));
723 printk("qpti%d: Cannot acquire irq line\n", qpti->qpti_id);
727 static void __init qpti_get_scsi_id(struct qlogicpti *qpti)
729 qpti->scsi_id = prom_getintdefault(qpti->prom_node,
732 if (qpti->scsi_id == -1)
733 qpti->scsi_id = prom_getintdefault(qpti->prom_node,
736 if (qpti->scsi_id == -1)
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;
751 bursts = prom_getintdefault(qpti->prom_node, "burst-sizes", 0xff);
752 bmask = prom_getintdefault(sdev->bus->prom_node,
753 "burst-sizes", 0xff);
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)
768 /* Check for what the clock input to this card is.
771 cfreq = prom_getintdefault(qpti->prom_node,"clock-frequency",40000000);
772 qpti->clock = (cfreq + 500000)/1000000;
773 if (qpti->clock == 0) /* bullshit */
777 /* The request and response queues must each be aligned
778 * on a page boundary.
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),
788 if (qpti->res_cpu == NULL ||
789 qpti->res_dvma == 0) {
790 printk("QPTI: Cannot map response queue.\n");
794 qpti->req_cpu = sbus_alloc_consistent(sdev,
795 QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
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");
804 memset(qpti->res_cpu, 0, QSIZE(RES_QUEUE_LEN));
805 memset(qpti->req_cpu, 0, QSIZE(QLOGICPTI_REQ_QUEUE_LEN));
809 /* Detect all PTI Qlogic ISP's in the machine. */
810 static int __init qlogicpti_detect(struct 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"))
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);
838 /* Yep, register and allocate software state. */
839 qpti_host = scsi_register(tpnt, sizeof(struct qlogicpti));
841 printk("QPTI: Cannot register PTI Qlogic ISP SCSI host");
844 qpti = (struct qlogicpti *) qpti_host->hostdata;
846 /* We are wide capable, 16 targets. */
847 qpti_host->max_id = MAX_TARGETS;
849 /* Setup back pointers and misc. state. */
850 qpti->qhost = qpti_host;
852 qpti->qpti_id = nqptis++;
853 qpti->prom_node = sdev->prom_node;
854 prom_getstring(qpti->prom_node, "name",
856 sizeof(qpti->prom_name));
858 /* This is not correct, actually. There's a switch
859 * on the PTI cards that put them into "emulation"
860 * mode- i.e., report themselves as QLGC,isp
861 * instead of PTI,ptisp. The only real substantive
862 * difference between non-pti and pti cards is
863 * the tmon register. Which is possibly even
864 * there for Qlogic cards, but non-functional.
866 qpti->is_pti = (strcmp (qpti->prom_name, "QLGC,isp") != 0);
868 qpti_chain_add(qpti);
869 if (qpti_map_regs(qpti) < 0)
872 if (qpti_register_irq(qpti) < 0)
873 goto fail_unmap_regs;
875 qpti_get_scsi_id(qpti);
876 qpti_get_bursts(qpti);
877 qpti_get_clock(qpti);
879 /* Clear out scsi_cmnd array. */
880 memset(qpti->cmd_slots, 0, sizeof(qpti->cmd_slots));
882 if (qpti_map_queues(qpti) < 0)
885 /* Load the firmware. */
886 if (qlogicpti_load_firmware(qpti))
887 goto fail_unmap_queues;
889 /* Check the PTI status reg. */
890 if (qlogicpti_verify_tmon(qpti))
891 goto fail_unmap_queues;
894 /* Reset the ISP and init res/req queues. */
895 if (qlogicpti_reset_hardware(qpti_host))
896 goto fail_unmap_queues;
898 printk("(Firmware v%d.%d.%d)", qpti->fware_majrev,
899 qpti->fware_minrev, qpti->fware_micrev);
903 prom_getstring (qpti->prom_node,
904 "isp-fcode", buffer, 60);
906 printk("(Firmware %s)", buffer);
907 if (prom_getbool(qpti->prom_node, "differential"))
908 qpti->differential = 1;
911 printk (" [%s Wide, using %s interface]\n",
912 (qpti->ultra ? "Ultra" : "Fast"),
913 (qpti->differential ? "differential" : "single ended"));
919 #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
920 sbus_free_consistent(qpti->sdev,
921 QSIZE(RES_QUEUE_LEN),
922 qpti->res_cpu, qpti->res_dvma);
923 sbus_free_consistent(qpti->sdev,
924 QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
925 qpti->req_cpu, qpti->req_dvma);
928 free_irq(qpti->irq, qpti);
931 sbus_iounmap(qpti->qregs,
932 qpti->sdev->reg_addrs[0].reg_size);
934 sbus_iounmap(qpti->sreg, sizeof(unsigned char));
936 qpti_chain_del(qpti);
937 scsi_unregister(qpti->qhost);
941 printk("QPTI: Total of %d PTI Qlogic/ISP hosts found, %d actually in use.\n",
942 nqptis, nqptis_in_use);
943 qptis_running = nqptis_in_use;
947 static int qlogicpti_release(struct Scsi_Host *host)
949 struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
951 /* Remove visibility from IRQ handlers. */
952 qpti_chain_del(qpti);
954 /* Shut up the card. */
955 sbus_writew(0, qpti->qregs + SBUS_CTRL);
957 /* Free IRQ handler and unmap Qlogic,ISP and PTI status regs. */
958 free_irq(qpti->irq, qpti);
960 #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
961 sbus_free_consistent(qpti->sdev,
962 QSIZE(RES_QUEUE_LEN),
963 qpti->res_cpu, qpti->res_dvma);
964 sbus_free_consistent(qpti->sdev,
965 QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
966 qpti->req_cpu, qpti->req_dvma);
969 sbus_iounmap(qpti->qregs, qpti->sdev->reg_addrs[0].reg_size);
971 sbus_iounmap(qpti->sreg, sizeof(unsigned char));
976 const char *qlogicpti_info(struct Scsi_Host *host)
979 struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
981 sprintf(buf, "PTI Qlogic,ISP SBUS SCSI irq %s regs at %lx",
982 __irq_itoa(qpti->qhost->irq), qpti->qregs);
986 /* I am a certified frobtronicist. */
987 static inline void marker_frob(struct Command_Entry *cmd)
989 struct Marker_Entry *marker = (struct Marker_Entry *) cmd;
991 memset(marker, 0, sizeof(struct Marker_Entry));
992 marker->hdr.entry_cnt = 1;
993 marker->hdr.entry_type = ENTRY_MARKER;
994 marker->modifier = SYNC_ALL;
998 static inline void cmd_frob(struct Command_Entry *cmd, struct scsi_cmnd *Cmnd,
999 struct qlogicpti *qpti)
1001 memset(cmd, 0, sizeof(struct Command_Entry));
1002 cmd->hdr.entry_cnt = 1;
1003 cmd->hdr.entry_type = ENTRY_COMMAND;
1004 cmd->target_id = Cmnd->device->id;
1005 cmd->target_lun = Cmnd->device->lun;
1006 cmd->cdb_length = Cmnd->cmd_len;
1007 cmd->control_flags = 0;
1008 if (Cmnd->device->tagged_supported) {
1009 if (qpti->cmd_count[Cmnd->device->id] == 0)
1010 qpti->tag_ages[Cmnd->device->id] = jiffies;
1011 if ((jiffies - qpti->tag_ages[Cmnd->device->id]) > (5*HZ)) {
1012 cmd->control_flags = CFLAG_ORDERED_TAG;
1013 qpti->tag_ages[Cmnd->device->id] = jiffies;
1015 cmd->control_flags = CFLAG_SIMPLE_TAG;
1017 if ((Cmnd->cmnd[0] == WRITE_6) ||
1018 (Cmnd->cmnd[0] == WRITE_10) ||
1019 (Cmnd->cmnd[0] == WRITE_12))
1020 cmd->control_flags |= CFLAG_WRITE;
1022 cmd->control_flags |= CFLAG_READ;
1024 memcpy(cmd->cdb, Cmnd->cmnd, Cmnd->cmd_len);
1027 /* Do it to it baby. */
1028 static inline int load_cmd(struct scsi_cmnd *Cmnd, struct Command_Entry *cmd,
1029 struct qlogicpti *qpti, u_int in_ptr, u_int out_ptr)
1032 struct scatterlist *sg;
1038 sg = (struct scatterlist *) Cmnd->buffer;
1039 sg_count = sbus_map_sg(qpti->sdev, sg, Cmnd->use_sg, Cmnd->sc_data_direction);
1042 cmd->segment_cnt = sg_count;
1044 /* Fill in first four sg entries: */
1048 for (i = 0; i < n; i++, sg++) {
1049 ds[i].d_base = sg_dma_address(sg);
1050 ds[i].d_count = sg_dma_len(sg);
1053 while (sg_count > 0) {
1054 struct Continuation_Entry *cont;
1056 ++cmd->hdr.entry_cnt;
1057 cont = (struct Continuation_Entry *) &qpti->req_cpu[in_ptr];
1058 in_ptr = NEXT_REQ_PTR(in_ptr);
1059 if (in_ptr == out_ptr)
1062 cont->hdr.entry_type = ENTRY_CONTINUATION;
1063 cont->hdr.entry_cnt = 0;
1064 cont->hdr.sys_def_1 = 0;
1065 cont->hdr.flags = 0;
1071 for (i = 0; i < n; i++, sg++) {
1072 ds[i].d_base = sg_dma_address(sg);
1073 ds[i].d_count = sg_dma_len(sg);
1077 } else if (Cmnd->request_bufflen) {
1078 Cmnd->SCp.ptr = (char *)(unsigned long)
1079 sbus_map_single(qpti->sdev,
1080 Cmnd->request_buffer,
1081 Cmnd->request_bufflen,
1082 Cmnd->sc_data_direction);
1084 cmd->dataseg[0].d_base = (u32) ((unsigned long)Cmnd->SCp.ptr);
1085 cmd->dataseg[0].d_count = Cmnd->request_bufflen;
1086 cmd->segment_cnt = 1;
1088 cmd->dataseg[0].d_base = 0;
1089 cmd->dataseg[0].d_count = 0;
1090 cmd->segment_cnt = 1; /* Shouldn't this be 0? */
1093 /* Committed, record Scsi_Cmd so we can find it later. */
1094 cmd->handle = in_ptr;
1095 qpti->cmd_slots[in_ptr] = Cmnd;
1097 qpti->cmd_count[Cmnd->device->id]++;
1098 sbus_writew(in_ptr, qpti->qregs + MBOX4);
1099 qpti->req_in_ptr = in_ptr;
1104 static inline void update_can_queue(struct Scsi_Host *host, u_int in_ptr, u_int out_ptr)
1106 /* Temporary workaround until bug is found and fixed (one bug has been found
1107 already, but fixing it makes things even worse) -jj */
1108 int num_free = QLOGICPTI_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr) - 64;
1109 host->can_queue = host->host_busy + num_free;
1110 host->sg_tablesize = QLOGICPTI_MAX_SG(num_free);
1114 * Until we scan the entire bus with inquiries, go throught this fella...
1116 static void ourdone(struct scsi_cmnd *Cmnd)
1118 struct qlogicpti *qpti = (struct qlogicpti *) Cmnd->device->host->hostdata;
1119 int tgt = Cmnd->device->id;
1120 void (*done) (struct scsi_cmnd *);
1122 /* This grot added by DaveM, blame him for ugliness.
1123 * The issue is that in the 2.3.x driver we use the
1124 * host_scribble portion of the scsi command as a
1125 * completion linked list at interrupt service time,
1126 * so we have to store the done function pointer elsewhere.
1128 done = (void (*)(struct scsi_cmnd *))
1129 (((unsigned long) Cmnd->SCp.Message)
1131 | ((unsigned long) Cmnd->SCp.Status << 32UL)
1135 if ((qpti->sbits & (1 << tgt)) == 0) {
1136 int ok = host_byte(Cmnd->result) == DID_OK;
1137 if (Cmnd->cmnd[0] == 0x12 && ok) {
1140 if (Cmnd->use_sg != 0)
1143 iqd = ((unsigned char *)Cmnd->buffer);
1145 /* tags handled in midlayer */
1146 /* enable sync mode? */
1147 if (iqd[7] & 0x10) {
1148 qpti->dev_param[tgt].device_flags |= 0x10;
1150 qpti->dev_param[tgt].synchronous_offset = 0;
1151 qpti->dev_param[tgt].synchronous_period = 0;
1153 /* are we wide capable? */
1154 if (iqd[7] & 0x20) {
1155 qpti->dev_param[tgt].device_flags |= 0x20;
1157 qpti->sbits |= (1 << tgt);
1159 qpti->sbits |= (1 << tgt);
1165 static int qlogicpti_queuecommand(struct scsi_cmnd *Cmnd, void (*done)(struct scsi_cmnd *));
1167 static int qlogicpti_queuecommand_slow(struct scsi_cmnd *Cmnd,
1168 void (*done)(struct scsi_cmnd *))
1170 struct qlogicpti *qpti = (struct qlogicpti *) Cmnd->device->host->hostdata;
1173 * done checking this host adapter?
1174 * If not, then rewrite the command
1175 * to finish through ourdone so we
1176 * can peek at Inquiry data results.
1178 if (qpti->sbits && qpti->sbits != 0xffff) {
1179 /* See above about in ourdone this ugliness... */
1180 Cmnd->SCp.Message = ((unsigned long)done) & 0xffffffff;
1181 #ifdef CONFIG_SPARC64
1182 Cmnd->SCp.Status = ((unsigned long)done >> 32UL) & 0xffffffff;
1184 return qlogicpti_queuecommand(Cmnd, ourdone);
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) {
1193 for(i = 0; i < MAX_TARGETS; i++) {
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;
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,
1213 /* check to see if we're done with all adapters... */
1214 for (qpti = qptichain; qpti != NULL; qpti = qpti->next) {
1221 * if we hit the end of the chain w/o finding adapters still
1222 * capability-configuring, then we're done with all adapters
1226 Cmnd->device->host->hostt->queuecommand = qlogicpti_queuecommand;
1228 return qlogicpti_queuecommand(Cmnd, done);
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).
1237 * "This code must fly." -davem
1239 static int qlogicpti_queuecommand(struct scsi_cmnd *Cmnd, void (*done)(struct scsi_cmnd *))
1241 struct Scsi_Host *host = Cmnd->device->host;
1242 struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
1243 struct Command_Entry *cmd;
1247 Cmnd->scsi_done = done;
1249 in_ptr = qpti->req_in_ptr;
1250 cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
1251 out_ptr = sbus_readw(qpti->qregs + MBOX4);
1252 in_ptr = NEXT_REQ_PTR(in_ptr);
1253 if (in_ptr == out_ptr)
1256 if (qpti->send_marker) {
1258 qpti->send_marker = 0;
1259 if (NEXT_REQ_PTR(in_ptr) == out_ptr) {
1260 sbus_writew(in_ptr, qpti->qregs + MBOX4);
1261 qpti->req_in_ptr = in_ptr;
1264 cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
1265 in_ptr = NEXT_REQ_PTR(in_ptr);
1267 cmd_frob(cmd, Cmnd, qpti);
1268 if ((in_ptr = load_cmd(Cmnd, cmd, qpti, in_ptr, out_ptr)) == -1)
1271 update_can_queue(host, in_ptr, out_ptr);
1276 printk(KERN_EMERG "qlogicpti%d: request queue overflow\n",
1279 /* Unfortunately, unless you use the new EH code, which
1280 * we don't, the midlayer will ignore the return value,
1281 * which is insane. We pick up the pieces like this.
1283 Cmnd->result = DID_BUS_BUSY;
1288 static int qlogicpti_return_status(struct Status_Entry *sts, int id)
1290 int host_status = DID_ERROR;
1292 switch (sts->completion_status) {
1294 host_status = DID_OK;
1297 if (!(sts->state_flags & SF_GOT_BUS))
1298 host_status = DID_NO_CONNECT;
1299 else if (!(sts->state_flags & SF_GOT_TARGET))
1300 host_status = DID_BAD_TARGET;
1301 else if (!(sts->state_flags & SF_SENT_CDB))
1302 host_status = DID_ERROR;
1303 else if (!(sts->state_flags & SF_TRANSFERRED_DATA))
1304 host_status = DID_ERROR;
1305 else if (!(sts->state_flags & SF_GOT_STATUS))
1306 host_status = DID_ERROR;
1307 else if (!(sts->state_flags & SF_GOT_SENSE))
1308 host_status = DID_ERROR;
1311 case CS_TRANSPORT_ERROR:
1312 host_status = DID_ERROR;
1314 case CS_RESET_OCCURRED:
1316 host_status = DID_RESET;
1319 host_status = DID_ABORT;
1322 host_status = DID_TIME_OUT;
1324 case CS_DATA_OVERRUN:
1325 case CS_COMMAND_OVERRUN:
1326 case CS_STATUS_OVERRUN:
1327 case CS_BAD_MESSAGE:
1328 case CS_NO_MESSAGE_OUT:
1329 case CS_EXT_ID_FAILED:
1330 case CS_IDE_MSG_FAILED:
1331 case CS_ABORT_MSG_FAILED:
1332 case CS_NOP_MSG_FAILED:
1333 case CS_PARITY_ERROR_MSG_FAILED:
1334 case CS_DEVICE_RESET_MSG_FAILED:
1335 case CS_ID_MSG_FAILED:
1336 case CS_UNEXP_BUS_FREE:
1337 host_status = DID_ERROR;
1339 case CS_DATA_UNDERRUN:
1340 host_status = DID_OK;
1343 printk(KERN_EMERG "qpti%d: unknown completion status 0x%04x\n",
1344 id, sts->completion_status);
1345 host_status = DID_ERROR;
1349 return (sts->scsi_status & STATUS_MASK) | (host_status << 16);
1352 static struct scsi_cmnd *qlogicpti_intr_handler(struct qlogicpti *qpti)
1354 struct scsi_cmnd *Cmnd, *done_queue = NULL;
1355 struct Status_Entry *sts;
1356 u_int in_ptr, out_ptr;
1358 if (!(sbus_readw(qpti->qregs + SBUS_STAT) & SBUS_STAT_RINT))
1361 in_ptr = sbus_readw(qpti->qregs + MBOX5);
1362 sbus_writew(HCCTRL_CRIRQ, qpti->qregs + HCCTRL);
1363 if (sbus_readw(qpti->qregs + SBUS_SEMAPHORE) & SBUS_SEMAPHORE_LCK) {
1364 switch (sbus_readw(qpti->qregs + MBOX0)) {
1365 case ASYNC_SCSI_BUS_RESET:
1366 case EXECUTION_TIMEOUT_RESET:
1367 qpti->send_marker = 1;
1369 case INVALID_COMMAND:
1370 case HOST_INTERFACE_ERROR:
1372 case COMMAND_PARAM_ERROR:
1375 sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
1378 /* This looks like a network driver! */
1379 out_ptr = qpti->res_out_ptr;
1380 while (out_ptr != in_ptr) {
1383 sts = (struct Status_Entry *) &qpti->res_cpu[out_ptr];
1384 out_ptr = NEXT_RES_PTR(out_ptr);
1386 /* We store an index in the handle, not the pointer in
1387 * some form. This avoids problems due to the fact
1388 * that the handle provided is only 32-bits. -DaveM
1390 cmd_slot = sts->handle;
1391 Cmnd = qpti->cmd_slots[cmd_slot];
1392 qpti->cmd_slots[cmd_slot] = NULL;
1394 if (sts->completion_status == CS_RESET_OCCURRED ||
1395 sts->completion_status == CS_ABORTED ||
1396 (sts->status_flags & STF_BUS_RESET))
1397 qpti->send_marker = 1;
1399 if (sts->state_flags & SF_GOT_SENSE)
1400 memcpy(Cmnd->sense_buffer, sts->req_sense_data,
1401 sizeof(Cmnd->sense_buffer));
1403 if (sts->hdr.entry_type == ENTRY_STATUS)
1405 qlogicpti_return_status(sts, qpti->qpti_id);
1407 Cmnd->result = DID_ERROR << 16;
1410 sbus_unmap_sg(qpti->sdev,
1411 (struct scatterlist *)Cmnd->buffer,
1413 Cmnd->sc_data_direction);
1415 sbus_unmap_single(qpti->sdev,
1416 (__u32)((unsigned long)Cmnd->SCp.ptr),
1417 Cmnd->request_bufflen,
1418 Cmnd->sc_data_direction);
1420 qpti->cmd_count[Cmnd->device->id]--;
1421 sbus_writew(out_ptr, qpti->qregs + MBOX5);
1422 Cmnd->host_scribble = (unsigned char *) done_queue;
1425 qpti->res_out_ptr = out_ptr;
1430 static irqreturn_t qpti_intr(int irq, void *dev_id, struct pt_regs *regs)
1432 struct qlogicpti *qpti = dev_id;
1433 unsigned long flags;
1434 struct scsi_cmnd *dq;
1436 spin_lock_irqsave(qpti->qhost->host_lock, flags);
1437 dq = qlogicpti_intr_handler(qpti);
1441 struct scsi_cmnd *next;
1443 next = (struct scsi_cmnd *) dq->host_scribble;
1446 } while (dq != NULL);
1448 spin_unlock_irqrestore(qpti->qhost->host_lock, flags);
1453 static int qlogicpti_abort(struct scsi_cmnd *Cmnd)
1456 struct Scsi_Host *host = Cmnd->device->host;
1457 struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
1458 int return_status = SUCCESS;
1462 printk(KERN_WARNING "qlogicpti : Aborting cmd for tgt[%d] lun[%d]\n",
1463 (int)Cmnd->device->id, (int)Cmnd->device->lun);
1465 qlogicpti_disable_irqs(qpti);
1467 /* Find the 32-bit cookie we gave to the firmware for
1470 for (i = 0; i < QLOGICPTI_REQ_QUEUE_LEN + 1; i++)
1471 if (qpti->cmd_slots[i] == Cmnd)
1475 param[0] = MBOX_ABORT;
1476 param[1] = (((u_short) Cmnd->device->id) << 8) | Cmnd->device->lun;
1477 param[2] = cmd_cookie >> 16;
1478 param[3] = cmd_cookie & 0xffff;
1479 if (qlogicpti_mbox_command(qpti, param, 0) ||
1480 (param[0] != MBOX_COMMAND_COMPLETE)) {
1481 printk(KERN_EMERG "qlogicpti : scsi abort failure: %x\n", param[0]);
1482 return_status = FAILED;
1485 qlogicpti_enable_irqs(qpti);
1487 return return_status;
1490 static int qlogicpti_reset(struct scsi_cmnd *Cmnd)
1493 struct Scsi_Host *host = Cmnd->device->host;
1494 struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
1495 int return_status = SUCCESS;
1497 printk(KERN_WARNING "qlogicpti : Resetting SCSI bus!\n");
1499 qlogicpti_disable_irqs(qpti);
1501 param[0] = MBOX_BUS_RESET;
1502 param[1] = qpti->host_param.bus_reset_delay;
1503 if (qlogicpti_mbox_command(qpti, param, 0) ||
1504 (param[0] != MBOX_COMMAND_COMPLETE)) {
1505 printk(KERN_EMERG "qlogicisp : scsi bus reset failure: %x\n", param[0]);
1506 return_status = FAILED;
1509 qlogicpti_enable_irqs(qpti);
1511 return return_status;
1514 static struct scsi_host_template driver_template = {
1515 .detect = qlogicpti_detect,
1516 .release = qlogicpti_release,
1517 .info = qlogicpti_info,
1518 .queuecommand = qlogicpti_queuecommand_slow,
1519 .eh_abort_handler = qlogicpti_abort,
1520 .eh_bus_reset_handler = qlogicpti_reset,
1521 .can_queue = QLOGICPTI_REQ_QUEUE_LEN,
1523 .sg_tablesize = QLOGICPTI_MAX_SG(QLOGICPTI_REQ_QUEUE_LEN),
1525 .use_clustering = ENABLE_CLUSTERING,
1529 #include "scsi_module.c"
1531 MODULE_LICENSE("GPL");