- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / scsi / qlogicpti.c
blobd471dddd999416221779cd175439abfd6e679feb
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/malloc.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/vaddrs.h>
39 #include <asm/io.h>
40 #include <asm/irq.h>
42 #include <linux/module.h>
44 #define MAX_TARGETS 16
45 #define MAX_LUNS 8 /* 32 for 1.31 F/W */
47 #define DEFAULT_LOOP_COUNT 10000
49 #include "qlogicpti_asm.c"
51 static struct qlogicpti *qptichain = NULL;
52 static spinlock_t qptichain_lock = SPIN_LOCK_UNLOCKED;
53 static int qptis_running = 0;
55 #define PACKB(a, b) (((a)<<4)|(b))
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 */
127 #define MAX_MBOX_COMMAND (sizeof(mbox_param)/sizeof(u_short))
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)
135 static inline void qlogicpti_enable_irqs(struct qlogicpti *qpti)
137 sbus_writew(SBUS_CTRL_ERIRQ | SBUS_CTRL_GENAB,
138 qpti->qregs + SBUS_CTRL);
141 static inline void qlogicpti_disable_irqs(struct qlogicpti *qpti)
143 sbus_writew(0, qpti->qregs + SBUS_CTRL);
146 static inline void set_sbus_cfg1(struct qlogicpti *qpti)
148 u16 val;
149 u8 bursts = qpti->bursts;
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
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... */
169 sbus_writew(val, qpti->qregs + SBUS_CFG1);
172 static int qlogicpti_mbox_command(struct qlogicpti *qpti, u_short param[], int force)
174 int loop_count;
175 u16 tmp;
177 if (mbox_param[param[0]] == 0)
178 return 1;
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);
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");
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);
204 tmp |= HCCTRL_CRIRQ;
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);
212 tmp |= HCCTRL_SHIRQ;
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))
219 udelay(20);
220 if (!loop_count)
221 printk(KERN_EMERG "qlogicpti: mbox_command[%04x] loop timeout #2\n",
222 param[0]);
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);
230 /* Workaround for some buggy chips. */
231 if (sbus_readw(qpti->qregs + MBOX0) & 0x4000)
232 break;
234 if (!loop_count)
235 printk(KERN_EMERG "qlogicpti: mbox_command[%04x] loop timeout #3\n",
236 param[0]);
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]);
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);
258 tmp |= HCCTRL_CRIRQ;
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);
266 /* We're done. */
267 return 0;
270 static inline void qlogicpti_set_hostdev_defaults(struct qlogicpti *qpti)
272 int i;
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;
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;
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;
313 u_short param[6];
314 unsigned short risc_code_addr;
315 int loop_count, i;
316 unsigned long flags;
318 risc_code_addr = 0x1000; /* all load addresses are at 0x1000 */
320 save_flags(flags); cli();
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);
328 udelay(400);
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))
337 udelay(20);
338 if (!loop_count)
339 printk(KERN_EMERG "qlogicpti: reset_hardware loop timeout\n");
341 sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
342 set_sbus_cfg1(qpti);
343 qlogicpti_enable_irqs(qpti);
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);
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",
367 qpti->qpti_id);
368 restore_flags(flags);
369 return 1;
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;
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",
393 qpti->qpti_id);
394 restore_flags(flags);
395 return 1;
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;
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;
421 param[1] = (i << 8);
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;
434 param[1] = (i << 8);
435 param[2] = (qpti->dev_param[i].device_flags << 8);
437 * Since we're now loading 1.31 f/w, force narrow/async.
439 param[2] |= 0xc0;
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 restore_flags(flags);
453 return 0;
456 #define PTI_RESET_LIMIT 400
458 static int __init qlogicpti_load_firmware(struct qlogicpti *qpti)
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;
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;
470 save_flags(flags); cli();
472 /* Verify the checksum twice, one before loading it, and once
473 * afterwards via the mailbox commands.
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;
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;
495 sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
496 mdelay(1);
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);
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);
512 sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
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);
522 /* This shouldn't be necessary- we've reset things so we should be
523 running from the ROM now.. */
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;
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;
548 /* Reset the ISP again. */
549 sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
550 mdelay(1);
552 qlogicpti_enable_irqs(qpti);
553 sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
554 sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
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;
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);
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;
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];
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;
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);
604 param[0] = MBOX_WRITE_RAM_WORD;
605 param[1] = 0xff00;
606 param[2] = (unsigned short) 3;
607 qlogicpti_mbox_command(qpti, param, 1);
610 restore_flags(flags);
611 return 0;
614 static int qlogicpti_verify_tmon(struct qlogicpti *qpti)
616 int curstat = sbus_readb(qpti->sreg);
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);
629 if (curstat & SREG_TPOWER) {
630 error++;
631 printk("qlogicpti%d: termpwr failure\n", qpti->qpti_id);
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);
639 qpti->swsreg = curstat;
640 return error;
642 return 0;
645 static void qpti_intr(int irq, void *dev_id, struct pt_regs *regs);
647 static void __init qpti_chain_add(struct qlogicpti *qpti)
649 spin_lock_irq(&qptichain_lock);
650 if (qptichain != NULL) {
651 struct qlogicpti *qlink = qptichain;
653 while(qlink->next)
654 qlink = qlink->next;
655 qlink->next = qpti;
656 } else {
657 qptichain = qpti;
659 qpti->next = NULL;
660 spin_unlock_irq(&qptichain_lock);
663 static void __init qpti_chain_del(struct qlogicpti *qpti)
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;
674 qpti->next = NULL;
675 spin_unlock_irq(&qptichain_lock);
678 static int __init qpti_map_regs(struct qlogicpti *qpti)
680 struct sbus_dev *sdev = qpti->sdev;
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;
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;
698 return 0;
701 static int __init qpti_register_irq(struct qlogicpti *qpti)
703 struct sbus_dev *sdev = qpti->sdev;
705 qpti->qhost->irq = qpti->irq = sdev->irqs[0];
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.
713 if (request_irq(qpti->irq, qpti_intr,
714 SA_SHIRQ, "Qlogic/PTI", qpti))
715 goto fail;
717 printk("qpti%d: IRQ %s ", qpti->qpti_id, __irq_itoa(qpti->irq));
719 return 0;
721 fail:
722 printk("qpti%d: Cannot acquire irq line\n", qpti->qpti_id);
723 return -1;
726 static void __init qpti_get_scsi_id(struct qlogicpti *qpti)
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;
741 printk("SCSI ID %d ", qpti->scsi_id);
744 static void qpti_get_bursts(struct qlogicpti *qpti)
746 struct sbus_dev *sdev = qpti->sdev;
747 u8 bursts, bmask;
749 bursts = prom_getintdefault(qpti->prom_node, "burst-sizes", 0xff);
750 bmask = prom_getintdefault(sdev->bus->prom_node,
751 "burst-sizes", 0xff);
752 if (bmask != 0xff)
753 bursts &= bmask;
754 if (bursts == 0xff ||
755 (bursts & DMA_BURST16) == 0 ||
756 (bursts & DMA_BURST32) == 0)
757 bursts = (DMA_BURST32 - 1);
759 qpti->bursts = bursts;
762 static void qpti_get_clock(struct qlogicpti *qpti)
764 unsigned int cfreq;
766 /* Check for what the clock input to this card is.
767 * Default to 40Mhz.
769 cfreq = prom_getintdefault(qpti->prom_node,"clock-frequency",40000000);
770 qpti->clock = (cfreq + 500000)/1000000;
771 if (qpti->clock == 0) /* bullshit */
772 qpti->clock = 40;
775 /* The request and response queues must each be aligned
776 * on a page boundry.
778 static int __init qpti_map_queues(struct qlogicpti *qpti)
780 struct sbus_dev *sdev = qpti->sdev;
782 #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
783 qpti->res_cpu = sbus_alloc_consistent(sdev,
784 QSIZE(RES_QUEUE_LEN),
785 &qpti->res_dvma);
786 if (qpti->res_cpu == NULL ||
787 qpti->res_dvma == 0) {
788 printk("QPTI: Cannot map response queue.\n");
789 return -1;
792 qpti->req_cpu = sbus_alloc_consistent(sdev,
793 QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
794 &qpti->req_dvma);
795 if (qpti->req_cpu == NULL ||
796 qpti->req_dvma == 0) {
797 sbus_free_consistent(sdev, QSIZE(RES_QUEUE_LEN),
798 qpti->res_cpu, qpti->res_dvma);
799 printk("QPTI: Cannot map request queue.\n");
800 return -1;
802 memset(qpti->res_cpu, 0, QSIZE(RES_QUEUE_LEN));
803 memset(qpti->req_cpu, 0, QSIZE(QLOGICPTI_REQ_QUEUE_LEN));
804 return 0;
807 /* Detect all PTI Qlogic ISP's in the machine. */
808 int __init qlogicpti_detect(Scsi_Host_Template *tpnt)
810 struct qlogicpti *qpti;
811 struct Scsi_Host *qpti_host;
812 struct sbus_bus *sbus;
813 struct sbus_dev *sdev;
814 int nqptis = 0, nqptis_in_use = 0;
816 tpnt->proc_name = "qlogicpti";
817 for_each_sbus(sbus) {
818 for_each_sbusdev(sdev, sbus) {
819 /* Is this a red snapper? */
820 if (strcmp(sdev->prom_name, "ptisp") &&
821 strcmp(sdev->prom_name, "PTI,ptisp") &&
822 strcmp(sdev->prom_name, "QLGC,isp"))
823 continue;
825 /* Sometimes Antares cards come up not completely
826 * setup, and we get a report of a zero IRQ.
827 * Skip over them in such cases so we survive.
829 if (sdev->irqs[0] == 0) {
830 printk("qpti%d: Adapter reports no interrupt, "
831 "skipping over this card.", nqptis);
832 continue;
835 /* Yep, register and allocate software state. */
836 qpti_host = scsi_register(tpnt, sizeof(struct qlogicpti));
837 if (!qpti_host) {
838 printk("QPTI: Cannot register PTI Qlogic ISP SCSI host");
839 continue;
841 qpti = (struct qlogicpti *) qpti_host->hostdata;
843 spin_lock_init(&qpti->lock);
845 /* We are wide capable, 16 targets. */
846 qpti_host->max_id = MAX_TARGETS;
848 /* Setup back pointers and misc. state. */
849 qpti->qhost = qpti_host;
850 qpti->sdev = sdev;
851 qpti->qpti_id = nqptis++;
852 qpti->prom_node = sdev->prom_node;
853 prom_getstring(qpti->prom_node, "name",
854 qpti->prom_name,
855 sizeof(qpti->prom_name));
857 /* This is not correct, actually. There's a switch
858 * on the PTI cards that put them into "emulation"
859 * mode- i.e., report themselves as QLGC,isp
860 * instead of PTI,ptisp. The only real substantive
861 * difference between non-pti and pti cards is
862 * the tmon register. Which is possibly even
863 * there for Qlogic cards, but non-functional.
865 qpti->is_pti = (strcmp (qpti->prom_name, "QLGC,isp") != 0);
867 qpti_chain_add(qpti);
868 if (qpti_map_regs(qpti) < 0)
869 goto fail_unlink;
871 if (qpti_register_irq(qpti) < 0)
872 goto fail_unmap_regs;
874 qpti_get_scsi_id(qpti);
875 qpti_get_bursts(qpti);
876 qpti_get_clock(qpti);
878 /* Clear out Scsi_Cmnd array. */
879 memset(qpti->cmd_slots, 0, sizeof(qpti->cmd_slots));
881 if (qpti_map_queues(qpti) < 0)
882 goto fail_free_irq;
884 /* Load the firmware. */
885 if (qlogicpti_load_firmware(qpti))
886 goto fail_unmap_queues;
887 if (qpti->is_pti) {
888 /* Check the PTI status reg. */
889 if (qlogicpti_verify_tmon(qpti))
890 goto fail_unmap_queues;
893 /* Reset the ISP and init res/req queues. */
894 if (qlogicpti_reset_hardware(qpti_host))
895 goto fail_unmap_queues;
897 printk("(Firmware v%d.%d.%d)", qpti->fware_majrev,
898 qpti->fware_minrev, qpti->fware_micrev);
900 char buffer[60];
902 prom_getstring (qpti->prom_node,
903 "isp-fcode", buffer, 60);
904 if (buffer[0])
905 printk("(Firmware %s)", buffer);
906 if (prom_getbool(qpti->prom_node, "differential"))
907 qpti->differential = 1;
910 printk (" [%s Wide, using %s interface]\n",
911 (qpti->ultra ? "Ultra" : "Fast"),
912 (qpti->differential ? "differential" : "single ended"));
914 nqptis_in_use++;
915 continue;
917 fail_unmap_queues:
918 #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
919 sbus_free_consistent(qpti->sdev,
920 QSIZE(RES_QUEUE_LEN),
921 qpti->res_cpu, qpti->res_dvma);
922 sbus_free_consistent(qpti->sdev,
923 QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
924 qpti->req_cpu, qpti->req_dvma);
925 #undef QSIZE
926 fail_free_irq:
927 free_irq(qpti->irq, qpti);
929 fail_unmap_regs:
930 sbus_iounmap(qpti->qregs,
931 qpti->sdev->reg_addrs[0].reg_size);
932 if (qpti->is_pti)
933 sbus_iounmap(qpti->sreg, sizeof(unsigned char));
934 fail_unlink:
935 qpti_chain_del(qpti);
936 scsi_unregister(qpti->qhost);
939 if (nqptis)
940 printk("QPTI: Total of %d PTI Qlogic/ISP hosts found, %d actually in use.\n",
941 nqptis, nqptis_in_use);
942 qptis_running = nqptis_in_use;
943 return nqptis;
946 int qlogicpti_release(struct Scsi_Host *host)
948 struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
950 /* Remove visibility from IRQ handlers. */
951 qpti_chain_del(qpti);
953 /* Shut up the card. */
954 sbus_writew(0, qpti->qregs + SBUS_CTRL);
956 /* Free IRQ handler and unmap Qlogic,ISP and PTI status regs. */
957 free_irq(qpti->irq, qpti);
959 #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
960 sbus_free_consistent(qpti->sdev,
961 QSIZE(RES_QUEUE_LEN),
962 qpti->res_cpu, qpti->res_dvma);
963 sbus_free_consistent(qpti->sdev,
964 QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
965 qpti->req_cpu, qpti->req_dvma);
966 #undef QSIZE
968 sbus_iounmap(qpti->qregs, qpti->sdev->reg_addrs[0].reg_size);
969 if (qpti->is_pti)
970 sbus_iounmap(qpti->sreg, sizeof(unsigned char));
972 return 0;
975 const char *qlogicpti_info(struct Scsi_Host *host)
977 static char buf[80];
978 struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
980 sprintf(buf, "PTI Qlogic,ISP SBUS SCSI irq %s regs at %lx",
981 __irq_itoa(qpti->qhost->irq), qpti->qregs);
982 return buf;
985 /* I am a certified frobtronicist. */
986 static inline void marker_frob(struct Command_Entry *cmd)
988 struct Marker_Entry *marker = (struct Marker_Entry *) cmd;
990 memset(marker, 0, sizeof(struct Marker_Entry));
991 marker->hdr.entry_cnt = 1;
992 marker->hdr.entry_type = ENTRY_MARKER;
993 marker->modifier = SYNC_ALL;
994 marker->rsvd = 0;
997 static inline void cmd_frob(struct Command_Entry *cmd, Scsi_Cmnd *Cmnd,
998 struct qlogicpti *qpti)
1000 memset(cmd, 0, sizeof(struct Command_Entry));
1001 cmd->hdr.entry_cnt = 1;
1002 cmd->hdr.entry_type = ENTRY_COMMAND;
1003 cmd->target_id = Cmnd->target;
1004 cmd->target_lun = Cmnd->lun;
1005 cmd->cdb_length = Cmnd->cmd_len;
1006 cmd->control_flags = 0;
1007 if (Cmnd->device->tagged_supported) {
1008 if (qpti->cmd_count[Cmnd->target] == 0)
1009 qpti->tag_ages[Cmnd->target] = jiffies;
1010 if ((jiffies - qpti->tag_ages[Cmnd->target]) > (5*HZ)) {
1011 cmd->control_flags = CFLAG_ORDERED_TAG;
1012 qpti->tag_ages[Cmnd->target] = jiffies;
1013 } else
1014 cmd->control_flags = CFLAG_SIMPLE_TAG;
1016 if ((Cmnd->cmnd[0] == WRITE_6) ||
1017 (Cmnd->cmnd[0] == WRITE_10) ||
1018 (Cmnd->cmnd[0] == WRITE_12))
1019 cmd->control_flags |= CFLAG_WRITE;
1020 else
1021 cmd->control_flags |= CFLAG_READ;
1022 cmd->time_out = 30;
1023 memcpy(cmd->cdb, Cmnd->cmnd, Cmnd->cmd_len);
1026 /* Do it to it baby. */
1027 static inline int load_cmd(Scsi_Cmnd *Cmnd, struct Command_Entry *cmd,
1028 struct qlogicpti *qpti, u_int in_ptr, u_int out_ptr)
1030 struct dataseg *ds;
1031 struct scatterlist *sg;
1032 int i, n;
1034 if (Cmnd->use_sg) {
1035 int sg_count;
1037 sg = (struct scatterlist *) Cmnd->buffer;
1038 sg_count = sbus_map_sg(qpti->sdev, sg, Cmnd->use_sg, scsi_to_sbus_dma_dir(Cmnd->sc_data_direction));
1040 ds = cmd->dataseg;
1041 cmd->segment_cnt = sg_count;
1043 /* Fill in first four sg entries: */
1044 n = sg_count;
1045 if (n > 4)
1046 n = 4;
1047 for (i = 0; i < n; i++, sg++) {
1048 ds[i].d_base = sg_dma_address(sg);
1049 ds[i].d_count = sg_dma_len(sg);
1051 sg_count -= 4;
1052 while (sg_count > 0) {
1053 struct Continuation_Entry *cont;
1055 ++cmd->hdr.entry_cnt;
1056 cont = (struct Continuation_Entry *) &qpti->req_cpu[in_ptr];
1057 in_ptr = NEXT_REQ_PTR(in_ptr);
1058 if (in_ptr == out_ptr)
1059 return -1;
1061 cont->hdr.entry_type = ENTRY_CONTINUATION;
1062 cont->hdr.entry_cnt = 0;
1063 cont->hdr.sys_def_1 = 0;
1064 cont->hdr.flags = 0;
1065 cont->reserved = 0;
1066 ds = cont->dataseg;
1067 n = sg_count;
1068 if (n > 7)
1069 n = 7;
1070 for (i = 0; i < n; i++, sg++) {
1071 ds[i].d_base = sg_dma_address(sg);
1072 ds[i].d_count = sg_dma_len(sg);
1074 sg_count -= n;
1076 } else if (Cmnd->request_bufflen) {
1077 Cmnd->SCp.ptr = (char *)(unsigned long)
1078 sbus_map_single(qpti->sdev,
1079 Cmnd->request_buffer,
1080 Cmnd->request_bufflen,
1081 scsi_to_sbus_dma_dir(Cmnd->sc_data_direction));
1083 cmd->dataseg[0].d_base = (u32) ((unsigned long)Cmnd->SCp.ptr);
1084 cmd->dataseg[0].d_count = Cmnd->request_bufflen;
1085 cmd->segment_cnt = 1;
1086 } else {
1087 cmd->dataseg[0].d_base = 0;
1088 cmd->dataseg[0].d_count = 0;
1089 cmd->segment_cnt = 1; /* Shouldn't this be 0? */
1092 /* Committed, record Scsi_Cmd so we can find it later. */
1093 cmd->handle = in_ptr;
1094 qpti->cmd_slots[in_ptr] = Cmnd;
1096 qpti->cmd_count[Cmnd->target]++;
1097 sbus_writew(in_ptr, qpti->qregs + MBOX4);
1098 qpti->req_in_ptr = in_ptr;
1100 return in_ptr;
1103 static inline void update_can_queue(struct Scsi_Host *host, u_int in_ptr, u_int out_ptr)
1105 /* Temporary workaround until bug is found and fixed (one bug has been found
1106 already, but fixing it makes things even worse) -jj */
1107 int num_free = QLOGICPTI_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr) - 64;
1108 host->can_queue = host->host_busy + num_free;
1109 host->sg_tablesize = QLOGICPTI_MAX_SG(num_free);
1113 * Until we scan the entire bus with inquiries, go throught this fella...
1115 static void ourdone(Scsi_Cmnd *Cmnd)
1117 struct qlogicpti *qpti = (struct qlogicpti *) Cmnd->host->hostdata;
1118 int tgt = Cmnd->target;
1119 void (*done) (Scsi_Cmnd *);
1121 /* This grot added by DaveM, blame him for ugliness.
1122 * The issue is that in the 2.3.x driver we use the
1123 * host_scribble portion of the scsi command as a
1124 * completion linked list at interrupt service time,
1125 * so we have to store the done function pointer elsewhere.
1127 done = (void (*)(Scsi_Cmnd *))
1128 (((unsigned long) Cmnd->SCp.Message)
1129 #ifdef __sparc_v9__
1130 | ((unsigned long) Cmnd->SCp.Status << 32UL)
1131 #endif
1134 if ((qpti->sbits & (1 << tgt)) == 0) {
1135 int ok = host_byte(Cmnd->result) == DID_OK;
1136 if (Cmnd->cmnd[0] == 0x12 && ok) {
1137 unsigned char *iqd;
1138 if (Cmnd->use_sg == 0) {
1139 iqd = ((unsigned char *)Cmnd->buffer);
1140 } else {
1141 iqd = ((struct scatterlist *) Cmnd->request_buffer)->address;
1143 /* tags handled in midlayer */
1144 /* enable sync mode? */
1145 if (iqd[7] & 0x10) {
1146 qpti->dev_param[tgt].device_flags |= 0x10;
1147 } else {
1148 qpti->dev_param[tgt].synchronous_offset = 0;
1149 qpti->dev_param[tgt].synchronous_period = 0;
1151 /* are we wide capable? */
1152 if (iqd[7] & 0x20) {
1153 qpti->dev_param[tgt].device_flags |= 0x20;
1155 qpti->sbits |= (1 << tgt);
1156 } else if (!ok) {
1157 qpti->sbits |= (1 << tgt);
1160 done(Cmnd);
1163 int qlogicpti_queuecommand_slow(Scsi_Cmnd *Cmnd, void (*done)(Scsi_Cmnd *))
1165 unsigned long flags;
1166 struct qlogicpti *qpti = (struct qlogicpti *) Cmnd->host->hostdata;
1169 * done checking this host adapter?
1170 * If not, then rewrite the command
1171 * to finish through ourdone so we
1172 * can peek at Inquiry data results.
1174 if (qpti->sbits && qpti->sbits != 0xffff) {
1175 /* See above about in ourdone this ugliness... */
1176 Cmnd->SCp.Message = ((unsigned long)done) & 0xffffffff;
1177 #ifdef __sparc_v9__
1178 Cmnd->SCp.Status = ((unsigned long)done >> 32UL) & 0xffffffff;
1179 #endif
1180 return qlogicpti_queuecommand(Cmnd, ourdone);
1182 save_flags(flags); cli();
1185 * We've peeked at all targets for this bus- time
1186 * to set parameters for devices for real now.
1188 if (qpti->sbits == 0xffff) {
1189 int i;
1190 for(i = 0; i < MAX_TARGETS; i++) {
1191 u_short param[6];
1192 param[0] = MBOX_SET_TARGET_PARAMS;
1193 param[1] = (i << 8);
1194 param[2] = (qpti->dev_param[i].device_flags << 8);
1195 if (qpti->dev_param[i].device_flags & 0x10) {
1196 param[3] = (qpti->dev_param[i].synchronous_offset << 8) |
1197 qpti->dev_param[i].synchronous_period;
1198 } else {
1199 param[3] = 0;
1201 (void) qlogicpti_mbox_command(qpti, param, 0);
1204 * set to zero so any traverse through ourdone
1205 * doesn't start the whole process again,
1207 qpti->sbits = 0;
1210 /* check to see if we're done with all adapters... */
1211 for (qpti = qptichain; qpti != NULL; qpti = qpti->next) {
1212 if (qpti->sbits) {
1213 break;
1218 * if we hit the end of the chain w/o finding adapters still
1219 * capability-configuring, then we're done with all adapters
1220 * and can rock on..
1222 if (qpti == NULL)
1223 Cmnd->host->hostt->queuecommand = qlogicpti_queuecommand;
1225 restore_flags(flags);
1226 return qlogicpti_queuecommand(Cmnd, done);
1230 * The middle SCSI layer ensures that queuecommand never gets invoked
1231 * concurrently with itself or the interrupt handler (though the
1232 * interrupt handler may call this routine as part of
1233 * request-completion handling).
1235 * "This code must fly." -davem
1237 int qlogicpti_queuecommand(Scsi_Cmnd *Cmnd, void (*done)(Scsi_Cmnd *))
1239 struct Scsi_Host *host = Cmnd->host;
1240 struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
1241 struct Command_Entry *cmd;
1242 unsigned long flags;
1243 u_int out_ptr;
1244 int in_ptr;
1246 Cmnd->scsi_done = done;
1248 spin_lock_irqsave(&qpti->lock, flags);
1250 in_ptr = qpti->req_in_ptr;
1251 cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
1252 out_ptr = sbus_readw(qpti->qregs + MBOX4);
1253 in_ptr = NEXT_REQ_PTR(in_ptr);
1254 if (in_ptr == out_ptr)
1255 goto toss_command;
1257 if (qpti->send_marker) {
1258 marker_frob(cmd);
1259 qpti->send_marker = 0;
1260 if (NEXT_REQ_PTR(in_ptr) == out_ptr) {
1261 sbus_writew(in_ptr, qpti->qregs + MBOX4);
1262 qpti->req_in_ptr = in_ptr;
1263 goto toss_command;
1265 cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
1266 in_ptr = NEXT_REQ_PTR(in_ptr);
1268 cmd_frob(cmd, Cmnd, qpti);
1269 if ((in_ptr = load_cmd(Cmnd, cmd, qpti, in_ptr, out_ptr)) == -1)
1270 goto toss_command;
1272 update_can_queue(host, in_ptr, out_ptr);
1274 spin_unlock_irqrestore(&qpti->lock, flags);
1275 return 0;
1277 toss_command:
1278 printk(KERN_EMERG "qlogicpti%d: request queue overflow\n",
1279 qpti->qpti_id);
1281 spin_unlock_irqrestore(&qpti->lock, flags);
1283 /* Unfortunately, unless you use the new EH code, which
1284 * we don't, the midlayer will ignore the return value,
1285 * which is insane. We pick up the pieces like this.
1287 Cmnd->result = DID_BUS_BUSY;
1288 done(Cmnd);
1289 return 1;
1292 static int qlogicpti_return_status(struct Status_Entry *sts, int id)
1294 int host_status = DID_ERROR;
1296 switch (sts->completion_status) {
1297 case CS_COMPLETE:
1298 host_status = DID_OK;
1299 break;
1300 case CS_INCOMPLETE:
1301 if (!(sts->state_flags & SF_GOT_BUS))
1302 host_status = DID_NO_CONNECT;
1303 else if (!(sts->state_flags & SF_GOT_TARGET))
1304 host_status = DID_BAD_TARGET;
1305 else if (!(sts->state_flags & SF_SENT_CDB))
1306 host_status = DID_ERROR;
1307 else if (!(sts->state_flags & SF_TRANSFERRED_DATA))
1308 host_status = DID_ERROR;
1309 else if (!(sts->state_flags & SF_GOT_STATUS))
1310 host_status = DID_ERROR;
1311 else if (!(sts->state_flags & SF_GOT_SENSE))
1312 host_status = DID_ERROR;
1313 break;
1314 case CS_DMA_ERROR:
1315 case CS_TRANSPORT_ERROR:
1316 host_status = DID_ERROR;
1317 break;
1318 case CS_RESET_OCCURRED:
1319 case CS_BUS_RESET:
1320 host_status = DID_RESET;
1321 break;
1322 case CS_ABORTED:
1323 host_status = DID_ABORT;
1324 break;
1325 case CS_TIMEOUT:
1326 host_status = DID_TIME_OUT;
1327 break;
1328 case CS_DATA_OVERRUN:
1329 case CS_COMMAND_OVERRUN:
1330 case CS_STATUS_OVERRUN:
1331 case CS_BAD_MESSAGE:
1332 case CS_NO_MESSAGE_OUT:
1333 case CS_EXT_ID_FAILED:
1334 case CS_IDE_MSG_FAILED:
1335 case CS_ABORT_MSG_FAILED:
1336 case CS_NOP_MSG_FAILED:
1337 case CS_PARITY_ERROR_MSG_FAILED:
1338 case CS_DEVICE_RESET_MSG_FAILED:
1339 case CS_ID_MSG_FAILED:
1340 case CS_UNEXP_BUS_FREE:
1341 host_status = DID_ERROR;
1342 break;
1343 case CS_DATA_UNDERRUN:
1344 host_status = DID_OK;
1345 break;
1346 default:
1347 printk(KERN_EMERG "qpti%d: unknown completion status 0x%04x\n",
1348 id, sts->completion_status);
1349 host_status = DID_ERROR;
1350 break;
1353 return (sts->scsi_status & STATUS_MASK) | (host_status << 16);
1356 static Scsi_Cmnd *qlogicpti_intr_handler(struct qlogicpti *qpti)
1358 Scsi_Cmnd *Cmnd, *done_queue = NULL;
1359 struct Status_Entry *sts;
1360 u_int in_ptr, out_ptr;
1362 if (!(sbus_readw(qpti->qregs + SBUS_STAT) & SBUS_STAT_RINT))
1363 return NULL;
1365 in_ptr = sbus_readw(qpti->qregs + MBOX5);
1366 sbus_writew(HCCTRL_CRIRQ, qpti->qregs + HCCTRL);
1367 if (sbus_readw(qpti->qregs + SBUS_SEMAPHORE) & SBUS_SEMAPHORE_LCK) {
1368 switch (sbus_readw(qpti->qregs + MBOX0)) {
1369 case ASYNC_SCSI_BUS_RESET:
1370 case EXECUTION_TIMEOUT_RESET:
1371 qpti->send_marker = 1;
1372 break;
1373 case INVALID_COMMAND:
1374 case HOST_INTERFACE_ERROR:
1375 case COMMAND_ERROR:
1376 case COMMAND_PARAM_ERROR:
1377 break;
1379 sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
1382 /* This looks like a network driver! */
1383 out_ptr = qpti->res_out_ptr;
1384 while (out_ptr != in_ptr) {
1385 u_int cmd_slot;
1387 sts = (struct Status_Entry *) &qpti->res_cpu[out_ptr];
1388 out_ptr = NEXT_RES_PTR(out_ptr);
1390 /* We store an index in the handle, not the pointer in
1391 * some form. This avoids problems due to the fact
1392 * that the handle provided is only 32-bits. -DaveM
1394 cmd_slot = sts->handle;
1395 Cmnd = qpti->cmd_slots[cmd_slot];
1396 qpti->cmd_slots[cmd_slot] = NULL;
1398 if (sts->completion_status == CS_RESET_OCCURRED ||
1399 sts->completion_status == CS_ABORTED ||
1400 (sts->status_flags & STF_BUS_RESET))
1401 qpti->send_marker = 1;
1403 if (sts->state_flags & SF_GOT_SENSE)
1404 memcpy(Cmnd->sense_buffer, sts->req_sense_data,
1405 sizeof(Cmnd->sense_buffer));
1407 if (sts->hdr.entry_type == ENTRY_STATUS)
1408 Cmnd->result =
1409 qlogicpti_return_status(sts, qpti->qpti_id);
1410 else
1411 Cmnd->result = DID_ERROR << 16;
1413 if (Cmnd->use_sg) {
1414 sbus_unmap_sg(qpti->sdev,
1415 (struct scatterlist *)Cmnd->buffer,
1416 Cmnd->use_sg,
1417 scsi_to_sbus_dma_dir(Cmnd->sc_data_direction));
1418 } else {
1419 sbus_unmap_single(qpti->sdev,
1420 (__u32)((unsigned long)Cmnd->SCp.ptr),
1421 Cmnd->request_bufflen,
1422 scsi_to_sbus_dma_dir(Cmnd->sc_data_direction));
1424 qpti->cmd_count[Cmnd->target]--;
1425 sbus_writew(out_ptr, qpti->qregs + MBOX5);
1426 Cmnd->host_scribble = (unsigned char *) done_queue;
1427 done_queue = Cmnd;
1429 qpti->res_out_ptr = out_ptr;
1431 return done_queue;
1434 static void qpti_intr(int irq, void *dev_id, struct pt_regs *regs)
1436 struct qlogicpti *qpti = dev_id;
1437 unsigned long flags;
1438 Scsi_Cmnd *dq;
1440 spin_lock_irqsave(&qpti->lock, flags);
1441 dq = qlogicpti_intr_handler(qpti);
1442 spin_unlock(&qpti->lock);
1444 if (dq != NULL) {
1445 spin_lock(&io_request_lock);
1446 do {
1447 Scsi_Cmnd *next;
1449 next = (Scsi_Cmnd *) dq->host_scribble;
1450 dq->scsi_done(dq);
1451 dq = next;
1452 } while (dq != NULL);
1453 spin_unlock(&io_request_lock);
1455 __restore_flags(flags);
1458 int qlogicpti_abort(Scsi_Cmnd *Cmnd)
1460 u_short param[6];
1461 struct Scsi_Host *host = Cmnd->host;
1462 struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
1463 int return_status = SCSI_ABORT_SUCCESS;
1464 unsigned long flags;
1465 u32 cmd_cookie;
1466 int i;
1468 printk(KERN_WARNING "qlogicpti : Aborting cmd for tgt[%d] lun[%d]\n",
1469 (int)Cmnd->target, (int)Cmnd->lun);
1471 spin_lock_irqsave(&qpti->lock, flags);
1473 qlogicpti_disable_irqs(qpti);
1475 /* Find the 32-bit cookie we gave to the firmware for
1476 * this command.
1478 for (i = 0; i < QLOGICPTI_REQ_QUEUE_LEN + 1; i++)
1479 if (qpti->cmd_slots[i] == Cmnd)
1480 break;
1481 cmd_cookie = i;
1483 param[0] = MBOX_ABORT;
1484 param[1] = (((u_short) Cmnd->target) << 8) | Cmnd->lun;
1485 param[2] = cmd_cookie >> 16;
1486 param[3] = cmd_cookie & 0xffff;
1487 if (qlogicpti_mbox_command(qpti, param, 0) ||
1488 (param[0] != MBOX_COMMAND_COMPLETE)) {
1489 printk(KERN_EMERG "qlogicpti : scsi abort failure: %x\n", param[0]);
1490 return_status = SCSI_ABORT_ERROR;
1493 qlogicpti_enable_irqs(qpti);
1495 spin_unlock_irqrestore(&qpti->lock, flags);
1497 return return_status;
1500 int qlogicpti_reset(Scsi_Cmnd *Cmnd, unsigned int reset_flags)
1502 u_short param[6];
1503 struct Scsi_Host *host = Cmnd->host;
1504 struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
1505 int return_status = SCSI_RESET_SUCCESS;
1506 unsigned long flags;
1508 printk(KERN_WARNING "qlogicpti : Resetting SCSI bus!\n");
1510 spin_lock_irqsave(&qpti->lock, flags);
1512 qlogicpti_disable_irqs(qpti);
1514 param[0] = MBOX_BUS_RESET;
1515 param[1] = qpti->host_param.bus_reset_delay;
1516 if (qlogicpti_mbox_command(qpti, param, 0) ||
1517 (param[0] != MBOX_COMMAND_COMPLETE)) {
1518 printk(KERN_EMERG "qlogicisp : scsi bus reset failure: %x\n", param[0]);
1519 return_status = SCSI_RESET_ERROR;
1522 qlogicpti_enable_irqs(qpti);
1524 spin_unlock_irqrestore(&qpti->lock, flags);
1526 return return_status;
1529 static Scsi_Host_Template driver_template = QLOGICPTI;
1531 #include "scsi_module.c"
1533 EXPORT_NO_SYMBOLS;