29522c4f5113431398302108583f74dd474280cc
[linux-2.6.git] / drivers / scsi / qlogicpti.c
1 /* qlogicpti.c: Performance Technologies QlogicISP sbus card driver.
2  *
3  * Copyright (C) 1996 David S. Miller (davem@caipfs.rutgers.edu)
4  *
5  * A lot of this driver was directly stolen from Erik H. Moe's PCI
6  * Qlogic ISP driver.  Mucho kudos to him for this code.
7  *
8  * An even bigger kudos to John Grana at Performance Technologies
9  * for providing me with the hardware to write this driver, you rule
10  * John you really do.
11  *
12  * May, 2, 1997: Added support for QLGC,isp --jj
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/delay.h>
17 #include <linux/types.h>
18 #include <linux/string.h>
19 #include <linux/slab.h>
20 #include <linux/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>
26
27 #include <asm/byteorder.h>
28
29 #include "qlogicpti.h"
30
31 #include <asm/sbus.h>
32 #include <asm/dma.h>
33 #include <asm/system.h>
34 #include <asm/ptrace.h>
35 #include <asm/pgtable.h>
36 #include <asm/oplib.h>
37 #include <asm/io.h>
38 #include <asm/irq.h>
39
40 #include <linux/module.h>
41
42 #define MAX_TARGETS     16
43 #define MAX_LUNS        8       /* 32 for 1.31 F/W */
44
45 #define DEFAULT_LOOP_COUNT      10000
46
47 #include "qlogicpti_asm.c"
48
49 static struct qlogicpti *qptichain = NULL;
50 static spinlock_t qptichain_lock = SPIN_LOCK_UNLOCKED;
51 static int qptis_running = 0;
52
53 #define PACKB(a, b)                     (((a)<<4)|(b))
54
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 */
123 };
124
125 #define MAX_MBOX_COMMAND        (sizeof(mbox_param)/sizeof(u_short))
126
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)
132
133 static inline void qlogicpti_enable_irqs(struct qlogicpti *qpti)
134 {
135         sbus_writew(SBUS_CTRL_ERIRQ | SBUS_CTRL_GENAB,
136                     qpti->qregs + SBUS_CTRL);
137 }
138
139 static inline void qlogicpti_disable_irqs(struct qlogicpti *qpti)
140 {
141         sbus_writew(0, qpti->qregs + SBUS_CTRL);
142 }
143
144 static inline void set_sbus_cfg1(struct qlogicpti *qpti)
145 {
146         u16 val;
147         u8 bursts = qpti->bursts;
148
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
152          * size. -DaveM
153          */
154         if (sbus_can_burst64(qpti->sdev) && (bursts & DMA_BURST64)) {
155                 val = (SBUS_CFG1_BENAB | SBUS_CFG1_B64);
156         } else
157 #endif
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);
164         } else {
165                 val = 0; /* No sbus bursts for you... */
166         }
167         sbus_writew(val, qpti->qregs + SBUS_CFG1);
168 }
169
170 static int qlogicpti_mbox_command(struct qlogicpti *qpti, u_short param[], int force)
171 {
172         int loop_count;
173         u16 tmp;
174
175         if (mbox_param[param[0]] == 0)
176                 return 1;
177
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);
182
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)) {
186                 barrier();
187                 cpu_relax();
188         }
189         if (!loop_count)
190                 printk(KERN_EMERG "qlogicpti: mbox_command loop timeout #1\n");
191
192         /* Write mailbox command registers. */
193         switch (mbox_param[param[0]] >> 4) {
194         case 6: sbus_writew(param[5], qpti->qregs + MBOX5);
195         case 5: sbus_writew(param[4], qpti->qregs + MBOX4);
196         case 4: sbus_writew(param[3], qpti->qregs + MBOX3);
197         case 3: sbus_writew(param[2], qpti->qregs + MBOX2);
198         case 2: sbus_writew(param[1], qpti->qregs + MBOX1);
199         case 1: sbus_writew(param[0], qpti->qregs + MBOX0);
200         }
201
202         /* Clear RISC interrupt. */
203         tmp = sbus_readw(qpti->qregs + HCCTRL);
204         tmp |= HCCTRL_CRIRQ;
205         sbus_writew(tmp, qpti->qregs + HCCTRL);
206
207         /* Clear SBUS semaphore. */
208         sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
209
210         /* Set HOST interrupt. */
211         tmp = sbus_readw(qpti->qregs + HCCTRL);
212         tmp |= HCCTRL_SHIRQ;
213         sbus_writew(tmp, qpti->qregs + HCCTRL);
214
215         /* Wait for HOST interrupt clears. */
216         loop_count = DEFAULT_LOOP_COUNT;
217         while (--loop_count &&
218                (sbus_readw(qpti->qregs + HCCTRL) & HCCTRL_CRIRQ))
219                 udelay(20);
220         if (!loop_count)
221                 printk(KERN_EMERG "qlogicpti: mbox_command[%04x] loop timeout #2\n",
222                        param[0]);
223
224         /* Wait for SBUS semaphore to get set. */
225         loop_count = DEFAULT_LOOP_COUNT;
226         while (--loop_count &&
227                !(sbus_readw(qpti->qregs + SBUS_SEMAPHORE) & SBUS_SEMAPHORE_LCK)) {
228                 udelay(20);
229
230                 /* Workaround for some buggy chips. */
231                 if (sbus_readw(qpti->qregs + MBOX0) & 0x4000)
232                         break;
233         }
234         if (!loop_count)
235                 printk(KERN_EMERG "qlogicpti: mbox_command[%04x] loop timeout #3\n",
236                        param[0]);
237
238         /* Wait for MBOX busy condition to go away. */
239         loop_count = DEFAULT_LOOP_COUNT;
240         while (--loop_count && (sbus_readw(qpti->qregs + MBOX0) == 0x04))
241                 udelay(20);
242         if (!loop_count)
243                 printk(KERN_EMERG "qlogicpti: mbox_command[%04x] loop timeout #4\n",
244                        param[0]);
245
246         /* Read back output parameters. */
247         switch (mbox_param[param[0]] & 0xf) {
248         case 6: param[5] = sbus_readw(qpti->qregs + MBOX5);
249         case 5: param[4] = sbus_readw(qpti->qregs + MBOX4);
250         case 4: param[3] = sbus_readw(qpti->qregs + MBOX3);
251         case 3: param[2] = sbus_readw(qpti->qregs + MBOX2);
252         case 2: param[1] = sbus_readw(qpti->qregs + MBOX1);
253         case 1: param[0] = sbus_readw(qpti->qregs + MBOX0);
254         }
255
256         /* Clear RISC interrupt. */
257         tmp = sbus_readw(qpti->qregs + HCCTRL);
258         tmp |= HCCTRL_CRIRQ;
259         sbus_writew(tmp, qpti->qregs + HCCTRL);
260
261         /* Release SBUS semaphore. */
262         tmp = sbus_readw(qpti->qregs + SBUS_SEMAPHORE);
263         tmp &= ~(SBUS_SEMAPHORE_LCK);
264         sbus_writew(tmp, qpti->qregs + SBUS_SEMAPHORE);
265
266         /* We're done. */
267         return 0;
268 }
269
270 static inline void qlogicpti_set_hostdev_defaults(struct qlogicpti *qpti)
271 {
272         int i;
273
274         qpti->host_param.initiator_scsi_id = qpti->scsi_id;
275         qpti->host_param.bus_reset_delay = 3;
276         qpti->host_param.retry_count = 0;
277         qpti->host_param.retry_delay = 5;
278         qpti->host_param.async_data_setup_time = 3;
279         qpti->host_param.req_ack_active_negation = 1;
280         qpti->host_param.data_line_active_negation = 1;
281         qpti->host_param.data_dma_burst_enable = 1;
282         qpti->host_param.command_dma_burst_enable = 1;
283         qpti->host_param.tag_aging = 8;
284         qpti->host_param.selection_timeout = 250;
285         qpti->host_param.max_queue_depth = 256;
286
287         for(i = 0; i < MAX_TARGETS; i++) {
288                 /*
289                  * disconnect, parity, arq, reneg on reset, and, oddly enough
290                  * tags...the midlayer's notion of tagged support has to match
291                  * our device settings, and since we base whether we enable a
292                  * tag on a  per-cmnd basis upon what the midlayer sez, we
293                  * actually enable the capability here.
294                  */
295                 qpti->dev_param[i].device_flags = 0xcd;
296                 qpti->dev_param[i].execution_throttle = 16;
297                 if (qpti->ultra) {
298                         qpti->dev_param[i].synchronous_period = 12;
299                         qpti->dev_param[i].synchronous_offset = 8;
300                 } else {
301                         qpti->dev_param[i].synchronous_period = 25;
302                         qpti->dev_param[i].synchronous_offset = 12;
303                 }
304                 qpti->dev_param[i].device_enable = 1;
305         }
306         /* this is very important to set! */
307         qpti->sbits = 1 << qpti->scsi_id;
308 }
309
310 static int qlogicpti_reset_hardware(struct Scsi_Host *host)
311 {
312         struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
313         u_short param[6];
314         unsigned short risc_code_addr;
315         int loop_count, i;
316         unsigned long flags;
317
318         risc_code_addr = 0x1000;        /* all load addresses are at 0x1000 */
319
320         spin_lock_irqsave(host->host_lock, flags);
321
322         sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
323
324         /* Only reset the scsi bus if it is not free. */
325         if (sbus_readw(qpti->qregs + CPU_PCTRL) & CPU_PCTRL_BSY) {
326                 sbus_writew(CPU_ORIDE_RMOD, qpti->qregs + CPU_ORIDE);
327                 sbus_writew(CPU_CMD_BRESET, qpti->qregs + CPU_CMD);
328                 udelay(400);
329         }
330
331         sbus_writew(SBUS_CTRL_RESET, qpti->qregs + SBUS_CTRL);
332         sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + CMD_DMA_CTRL);
333         sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + DATA_DMA_CTRL);
334
335         loop_count = DEFAULT_LOOP_COUNT;
336         while (--loop_count && ((sbus_readw(qpti->qregs + MBOX0) & 0xff) == 0x04))
337                 udelay(20);
338         if (!loop_count)
339                 printk(KERN_EMERG "qlogicpti: reset_hardware loop timeout\n");
340
341         sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
342         set_sbus_cfg1(qpti);
343         qlogicpti_enable_irqs(qpti);
344
345         if (sbus_readw(qpti->qregs + RISC_PSR) & RISC_PSR_ULTRA) {
346                 qpti->ultra = 1;
347                 sbus_writew((RISC_MTREG_P0ULTRA | RISC_MTREG_P1ULTRA),
348                             qpti->qregs + RISC_MTREG);
349         } else {
350                 qpti->ultra = 0;
351                 sbus_writew((RISC_MTREG_P0DFLT | RISC_MTREG_P1DFLT),
352                             qpti->qregs + RISC_MTREG);
353         }
354
355         /* reset adapter and per-device default values. */
356         /* do it after finding out whether we're ultra mode capable */
357         qlogicpti_set_hostdev_defaults(qpti);
358
359         /* Release the RISC processor. */
360         sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
361
362         /* Get RISC to start executing the firmware code. */
363         param[0] = MBOX_EXEC_FIRMWARE;
364         param[1] = risc_code_addr;
365         if (qlogicpti_mbox_command(qpti, param, 1)) {
366                 printk(KERN_EMERG "qlogicpti%d: Cannot execute ISP firmware.\n",
367                        qpti->qpti_id);
368                 spin_unlock_irqrestore(host->host_lock, flags);
369                 return 1;
370         }
371
372         /* Set initiator scsi ID. */
373         param[0] = MBOX_SET_INIT_SCSI_ID;
374         param[1] = qpti->host_param.initiator_scsi_id;
375         if (qlogicpti_mbox_command(qpti, param, 1) ||
376            (param[0] != MBOX_COMMAND_COMPLETE)) {
377                 printk(KERN_EMERG "qlogicpti%d: Cannot set initiator SCSI ID.\n",
378                        qpti->qpti_id);
379                 spin_unlock_irqrestore(host->host_lock, flags);
380                 return 1;
381         }
382
383         /* Initialize state of the queues, both hw and sw. */
384         qpti->req_in_ptr = qpti->res_out_ptr = 0;
385
386         param[0] = MBOX_INIT_RES_QUEUE;
387         param[1] = RES_QUEUE_LEN + 1;
388         param[2] = (u_short) (qpti->res_dvma >> 16);
389         param[3] = (u_short) (qpti->res_dvma & 0xffff);
390         param[4] = param[5] = 0;
391         if (qlogicpti_mbox_command(qpti, param, 1)) {
392                 printk(KERN_EMERG "qlogicpti%d: Cannot init response queue.\n",
393                        qpti->qpti_id);
394                 spin_unlock_irqrestore(host->host_lock, flags);
395                 return 1;
396         }
397
398         param[0] = MBOX_INIT_REQ_QUEUE;
399         param[1] = QLOGICPTI_REQ_QUEUE_LEN + 1;
400         param[2] = (u_short) (qpti->req_dvma >> 16);
401         param[3] = (u_short) (qpti->req_dvma & 0xffff);
402         param[4] = param[5] = 0;
403         if (qlogicpti_mbox_command(qpti, param, 1)) {
404                 printk(KERN_EMERG "qlogicpti%d: Cannot init request queue.\n",
405                        qpti->qpti_id);
406                 spin_unlock_irqrestore(host->host_lock, flags);
407                 return 1;
408         }
409
410         param[0] = MBOX_SET_RETRY_COUNT;
411         param[1] = qpti->host_param.retry_count;
412         param[2] = qpti->host_param.retry_delay;
413         qlogicpti_mbox_command(qpti, param, 0);
414
415         param[0] = MBOX_SET_TAG_AGE_LIMIT;
416         param[1] = qpti->host_param.tag_aging;
417         qlogicpti_mbox_command(qpti, param, 0);
418
419         for (i = 0; i < MAX_TARGETS; i++) {
420                 param[0] = MBOX_GET_DEV_QUEUE_PARAMS;
421                 param[1] = (i << 8);
422                 qlogicpti_mbox_command(qpti, param, 0);
423         }
424
425         param[0] = MBOX_GET_FIRMWARE_STATUS;
426         qlogicpti_mbox_command(qpti, param, 0);
427
428         param[0] = MBOX_SET_SELECT_TIMEOUT;
429         param[1] = qpti->host_param.selection_timeout;
430         qlogicpti_mbox_command(qpti, param, 0);
431
432         for (i = 0; i < MAX_TARGETS; i++) {
433                 param[0] = MBOX_SET_TARGET_PARAMS;
434                 param[1] = (i << 8);
435                 param[2] = (qpti->dev_param[i].device_flags << 8);
436                 /*
437                  * Since we're now loading 1.31 f/w, force narrow/async.
438                  */
439                 param[2] |= 0xc0;
440                 param[3] = 0;   /* no offset, we do not have sync mode yet */
441                 qlogicpti_mbox_command(qpti, param, 0);
442         }
443
444         /*
445          * Always (sigh) do an initial bus reset (kicks f/w).
446          */
447         param[0] = MBOX_BUS_RESET;
448         param[1] = qpti->host_param.bus_reset_delay;
449         qlogicpti_mbox_command(qpti, param, 0);
450         qpti->send_marker = 1;
451
452         spin_unlock_irqrestore(host->host_lock, flags);
453         return 0;
454 }
455
456 #define PTI_RESET_LIMIT 400
457
458 static int __init qlogicpti_load_firmware(struct qlogicpti *qpti)
459 {
460         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;
464         unsigned long flags;
465         int i, timeout;
466
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;
470
471         spin_lock_irqsave(host->host_lock, flags);
472
473         /* Verify the checksum twice, one before loading it, and once
474          * afterwards via the mailbox commands.
475          */
476         for (i = 0; i < risc_code_length; i++)
477                 csum += risc_code[i];
478         if (csum) {
479                 spin_unlock_irqrestore(host->host_lock, flags);
480                 printk(KERN_EMERG "qlogicpti%d: Aieee, firmware checksum failed!",
481                        qpti->qpti_id);
482                 return 1;
483         }               
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                 spin_unlock_irqrestore(host->host_lock, flags);
492                 printk(KERN_EMERG "qlogicpti%d: Cannot reset the ISP.", qpti->qpti_id);
493                 return 1;
494         }
495
496         sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
497         mdelay(1);
498
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);
502
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);
511         }
512
513         sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
514
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);
522
523         /* This shouldn't be necessary- we've reset things so we should be
524            running from the ROM now.. */
525
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                 spin_unlock_irqrestore(host->host_lock, flags);
532                 return 1;
533         }               
534
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                         spin_unlock_irqrestore(host->host_lock, flags);
545                         return 1;
546                 }
547         }
548
549         /* Reset the ISP again. */
550         sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
551         mdelay(1);
552
553         qlogicpti_enable_irqs(qpti);
554         sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
555         sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
556
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                 spin_unlock_irqrestore(host->host_lock, flags);
565                 return 1;
566         }
567
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);
572
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                 spin_unlock_irqrestore(host->host_lock, flags);
579                 return 1;
580         }
581
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];
586
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                 spin_unlock_irqrestore(host->host_lock, flags);
595                 return 1;
596         }
597
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);
604
605                 param[0] = MBOX_WRITE_RAM_WORD;
606                 param[1] = 0xff00;
607                 param[2] = (unsigned short) 3;
608                 qlogicpti_mbox_command(qpti, param, 1);
609         }
610
611         spin_unlock_irqrestore(host->host_lock, flags);
612         return 0;
613 }
614
615 static int qlogicpti_verify_tmon(struct qlogicpti *qpti)
616 {
617         int curstat = sbus_readb(qpti->sreg);
618
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);
629                 }
630                 if (curstat & SREG_TPOWER) {
631                         error++;
632                         printk("qlogicpti%d: termpwr failure\n", qpti->qpti_id);
633                 }
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);
639                 }
640                 qpti->swsreg = curstat;
641                 return error;
642         }
643         return 0;
644 }
645
646 static irqreturn_t qpti_intr(int irq, void *dev_id, struct pt_regs *regs);
647
648 static void __init qpti_chain_add(struct qlogicpti *qpti)
649 {
650         spin_lock_irq(&qptichain_lock);
651         if (qptichain != NULL) {
652                 struct qlogicpti *qlink = qptichain;
653
654                 while(qlink->next)
655                         qlink = qlink->next;
656                 qlink->next = qpti;
657         } else {
658                 qptichain = qpti;
659         }
660         qpti->next = NULL;
661         spin_unlock_irq(&qptichain_lock);
662 }
663
664 static void __init qpti_chain_del(struct qlogicpti *qpti)
665 {
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;
674         }
675         qpti->next = NULL;
676         spin_unlock_irq(&qptichain_lock);
677 }
678
679 static int __init qpti_map_regs(struct qlogicpti *qpti)
680 {
681         struct sbus_dev *sdev = qpti->sdev;
682
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;
689         }
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;
697                 }
698         }
699         return 0;
700 }
701
702 static int __init qpti_register_irq(struct qlogicpti *qpti)
703 {
704         struct sbus_dev *sdev = qpti->sdev;
705
706         qpti->qhost->irq = qpti->irq = sdev->irqs[0];
707
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.
713          */
714         if (request_irq(qpti->irq, qpti_intr,
715                         SA_SHIRQ, "Qlogic/PTI", qpti))
716                 goto fail;
717
718         printk("qpti%d: IRQ %s ", qpti->qpti_id, __irq_itoa(qpti->irq));
719
720         return 0;
721
722 fail:
723         printk("qpti%d: Cannot acquire irq line\n", qpti->qpti_id);
724         return -1;
725 }
726
727 static void __init qpti_get_scsi_id(struct qlogicpti *qpti)
728 {
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;
742
743         printk("SCSI ID %d ", qpti->scsi_id);
744 }
745
746 static void qpti_get_bursts(struct qlogicpti *qpti)
747 {
748         struct sbus_dev *sdev = qpti->sdev;
749         u8 bursts, bmask;
750
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);
760
761         qpti->bursts = bursts;
762 }
763
764 static void qpti_get_clock(struct qlogicpti *qpti)
765 {
766         unsigned int cfreq;
767
768         /* Check for what the clock input to this card is.
769          * Default to 40Mhz.
770          */
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;
775 }
776
777 /* The request and response queues must each be aligned
778  * on a page boundary.
779  */
780 static int __init qpti_map_queues(struct qlogicpti *qpti)
781 {
782         struct sbus_dev *sdev = qpti->sdev;
783
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;
792         }
793
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;
803         }
804         memset(qpti->res_cpu, 0, QSIZE(RES_QUEUE_LEN));
805         memset(qpti->req_cpu, 0, QSIZE(QLOGICPTI_REQ_QUEUE_LEN));
806         return 0;
807 }
808
809 /* Detect all PTI Qlogic ISP's in the machine. */
810 static int __init qlogicpti_detect(struct scsi_host_template *tpnt)
811 {
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;
817
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;
827
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.
831                          */
832                         if (sdev->irqs[0] == 0) {
833                                 printk("qpti%d: Adapter reports no interrupt, "
834                                        "skipping over this card.", nqptis);
835                                 continue;
836                         }
837
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;
843                         }
844                         qpti = (struct qlogicpti *) qpti_host->hostdata;
845
846                         /* We are wide capable, 16 targets. */
847                         qpti_host->max_id = MAX_TARGETS;
848
849                         /* Setup back pointers and misc. state. */
850                         qpti->qhost = qpti_host;
851                         qpti->sdev = sdev;
852                         qpti->qpti_id = nqptis++;
853                         qpti->prom_node = sdev->prom_node;
854                         prom_getstring(qpti->prom_node, "name",
855                                        qpti->prom_name,
856                                        sizeof(qpti->prom_name));
857
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.
865                          */
866                         qpti->is_pti = (strcmp (qpti->prom_name, "QLGC,isp") != 0);
867
868                         qpti_chain_add(qpti);
869                         if (qpti_map_regs(qpti) < 0)
870                                 goto fail_unlink;
871
872                         if (qpti_register_irq(qpti) < 0)
873                                 goto fail_unmap_regs;
874
875                         qpti_get_scsi_id(qpti);
876                         qpti_get_bursts(qpti);
877                         qpti_get_clock(qpti);
878
879                         /* Clear out scsi_cmnd array. */
880                         memset(qpti->cmd_slots, 0, sizeof(qpti->cmd_slots));
881
882                         if (qpti_map_queues(qpti) < 0)
883                                 goto fail_free_irq;
884
885                         /* Load the firmware. */
886                         if (qlogicpti_load_firmware(qpti))
887                                 goto fail_unmap_queues;
888                         if (qpti->is_pti) {
889                                 /* Check the PTI status reg. */
890                                 if (qlogicpti_verify_tmon(qpti))
891                                         goto fail_unmap_queues;
892                         }
893
894                         /* Reset the ISP and init res/req queues. */
895                         if (qlogicpti_reset_hardware(qpti_host))
896                                 goto fail_unmap_queues;
897
898                         printk("(Firmware v%d.%d.%d)", qpti->fware_majrev,
899                             qpti->fware_minrev, qpti->fware_micrev);
900                         {
901                                 char buffer[60];
902                                 
903                                 prom_getstring (qpti->prom_node,
904                                                 "isp-fcode", buffer, 60);
905                                 if (buffer[0])
906                                         printk("(Firmware %s)", buffer);
907                                 if (prom_getbool(qpti->prom_node, "differential"))
908                                         qpti->differential = 1;
909                         }
910                         
911                         printk (" [%s Wide, using %s interface]\n",
912                                (qpti->ultra ? "Ultra" : "Fast"),
913                                (qpti->differential ? "differential" : "single ended"));
914
915                         nqptis_in_use++;
916                         continue;
917
918                 fail_unmap_queues:
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);
926 #undef QSIZE
927                 fail_free_irq:
928                         free_irq(qpti->irq, qpti);
929
930                 fail_unmap_regs:
931                         sbus_iounmap(qpti->qregs,
932                                      qpti->sdev->reg_addrs[0].reg_size);
933                         if (qpti->is_pti)
934                                 sbus_iounmap(qpti->sreg, sizeof(unsigned char));
935                 fail_unlink:
936                         qpti_chain_del(qpti);
937                         scsi_unregister(qpti->qhost);
938                 }
939         }
940         if (nqptis)
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;
944         return nqptis;
945 }
946
947 static int qlogicpti_release(struct Scsi_Host *host)
948 {
949         struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
950
951         /* Remove visibility from IRQ handlers. */
952         qpti_chain_del(qpti);
953
954         /* Shut up the card. */
955         sbus_writew(0, qpti->qregs + SBUS_CTRL);
956
957         /* Free IRQ handler and unmap Qlogic,ISP and PTI status regs. */
958         free_irq(qpti->irq, qpti);
959
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);
967 #undef QSIZE
968
969         sbus_iounmap(qpti->qregs, qpti->sdev->reg_addrs[0].reg_size);
970         if (qpti->is_pti)
971                 sbus_iounmap(qpti->sreg, sizeof(unsigned char));
972
973         return 0;
974 }
975
976 const char *qlogicpti_info(struct Scsi_Host *host)
977 {
978         static char buf[80];
979         struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
980
981         sprintf(buf, "PTI Qlogic,ISP SBUS SCSI irq %s regs at %lx",
982                 __irq_itoa(qpti->qhost->irq), qpti->qregs);
983         return buf;
984 }
985
986 /* I am a certified frobtronicist. */
987 static inline void marker_frob(struct Command_Entry *cmd)
988 {
989         struct Marker_Entry *marker = (struct Marker_Entry *) cmd;
990
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;
995         marker->rsvd = 0;
996 }
997
998 static inline void cmd_frob(struct Command_Entry *cmd, struct scsi_cmnd *Cmnd,
999                             struct qlogicpti *qpti)
1000 {
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;
1014                 } else
1015                         cmd->control_flags = CFLAG_SIMPLE_TAG;
1016         }
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;
1021         else
1022                 cmd->control_flags |= CFLAG_READ;
1023         cmd->time_out = 30;
1024         memcpy(cmd->cdb, Cmnd->cmnd, Cmnd->cmd_len);
1025 }
1026
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)
1030 {
1031         struct dataseg *ds;
1032         struct scatterlist *sg;
1033         int i, n;
1034
1035         if (Cmnd->use_sg) {
1036                 int sg_count;
1037
1038                 sg = (struct scatterlist *) Cmnd->buffer;
1039                 sg_count = sbus_map_sg(qpti->sdev, sg, Cmnd->use_sg, Cmnd->sc_data_direction);
1040
1041                 ds = cmd->dataseg;
1042                 cmd->segment_cnt = sg_count;
1043
1044                 /* Fill in first four sg entries: */
1045                 n = sg_count;
1046                 if (n > 4)
1047                         n = 4;
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);
1051                 }
1052                 sg_count -= 4;
1053                 while (sg_count > 0) {
1054                         struct Continuation_Entry *cont;
1055
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)
1060                                 return -1;
1061
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;
1066                         cont->reserved = 0;
1067                         ds = cont->dataseg;
1068                         n = sg_count;
1069                         if (n > 7)
1070                                 n = 7;
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);
1074                         }
1075                         sg_count -= n;
1076                 }
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);
1083
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;
1087         } else {
1088                 cmd->dataseg[0].d_base = 0;
1089                 cmd->dataseg[0].d_count = 0;
1090                 cmd->segment_cnt = 1; /* Shouldn't this be 0? */
1091         }
1092
1093         /* Committed, record Scsi_Cmd so we can find it later. */
1094         cmd->handle = in_ptr;
1095         qpti->cmd_slots[in_ptr] = Cmnd;
1096
1097         qpti->cmd_count[Cmnd->device->id]++;
1098         sbus_writew(in_ptr, qpti->qregs + MBOX4);
1099         qpti->req_in_ptr = in_ptr;
1100
1101         return in_ptr;
1102 }
1103
1104 static inline void update_can_queue(struct Scsi_Host *host, u_int in_ptr, u_int out_ptr)
1105 {
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);
1111 }
1112
1113 /*
1114  * Until we scan the entire bus with inquiries, go throught this fella...
1115  */
1116 static void ourdone(struct scsi_cmnd *Cmnd)
1117 {
1118         struct qlogicpti *qpti = (struct qlogicpti *) Cmnd->device->host->hostdata;
1119         int tgt = Cmnd->device->id;
1120         void (*done) (struct scsi_cmnd *);
1121
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.
1127          */
1128         done = (void (*)(struct scsi_cmnd *))
1129                 (((unsigned long) Cmnd->SCp.Message)
1130 #ifdef __sparc_v9__
1131                  | ((unsigned long) Cmnd->SCp.Status << 32UL)
1132 #endif
1133                  );
1134
1135         if ((qpti->sbits & (1 << tgt)) == 0) {
1136                 int ok = host_byte(Cmnd->result) == DID_OK;
1137                 if (Cmnd->cmnd[0] == 0x12 && ok) {
1138                         unsigned char *iqd;
1139
1140                         if (Cmnd->use_sg != 0)
1141                                 BUG();
1142
1143                         iqd = ((unsigned char *)Cmnd->buffer);
1144
1145                         /* tags handled in midlayer */
1146                         /* enable sync mode? */
1147                         if (iqd[7] & 0x10) {
1148                                 qpti->dev_param[tgt].device_flags |= 0x10;
1149                         } else {
1150                                 qpti->dev_param[tgt].synchronous_offset = 0;
1151                                 qpti->dev_param[tgt].synchronous_period = 0;
1152                         }
1153                         /* are we wide capable? */
1154                         if (iqd[7] & 0x20) {
1155                                 qpti->dev_param[tgt].device_flags |= 0x20;
1156                         }
1157                         qpti->sbits |= (1 << tgt);
1158                 } else if (!ok) {
1159                         qpti->sbits |= (1 << tgt);
1160                 }
1161         }
1162         done(Cmnd);
1163 }
1164
1165 static int qlogicpti_queuecommand(struct scsi_cmnd *Cmnd, void (*done)(struct scsi_cmnd *));
1166
1167 static int qlogicpti_queuecommand_slow(struct scsi_cmnd *Cmnd,
1168                                        void (*done)(struct scsi_cmnd *))
1169 {
1170         struct qlogicpti *qpti = (struct qlogicpti *) Cmnd->device->host->hostdata;
1171
1172         /*
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.
1177          */
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;
1183 #endif
1184                 return qlogicpti_queuecommand(Cmnd, ourdone);
1185         }
1186
1187         /*
1188          * We've peeked at all targets for this bus- time
1189          * to set parameters for devices for real now.
1190          */
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;
1203                         }
1204                         (void) qlogicpti_mbox_command(qpti, param, 0);
1205                 }
1206                 /*
1207                  * set to zero so any traverse through ourdone
1208                  * doesn't start the whole process again,
1209                  */
1210                 qpti->sbits = 0;
1211         }
1212
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;
1217                 }
1218         }
1219
1220         /*
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..
1224          */
1225         if (qpti == NULL)
1226                 Cmnd->device->host->hostt->queuecommand = qlogicpti_queuecommand;
1227
1228         return qlogicpti_queuecommand(Cmnd, done);
1229 }
1230
1231 /*
1232  * The middle SCSI layer ensures that queuecommand never gets invoked
1233  * concurrently with itself or the interrupt handler (though the
1234  * interrupt handler may call this routine as part of
1235  * request-completion handling).
1236  *
1237  * "This code must fly." -davem
1238  */
1239 static int qlogicpti_queuecommand(struct scsi_cmnd *Cmnd, void (*done)(struct scsi_cmnd *))
1240 {
1241         struct Scsi_Host *host = Cmnd->device->host;
1242         struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
1243         struct Command_Entry *cmd;
1244         u_int out_ptr;
1245         int in_ptr;
1246
1247         Cmnd->scsi_done = done;
1248
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)
1254                 goto toss_command;
1255
1256         if (qpti->send_marker) {
1257                 marker_frob(cmd);
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;
1262                         goto toss_command;
1263                 }
1264                 cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
1265                 in_ptr = NEXT_REQ_PTR(in_ptr);
1266         }
1267         cmd_frob(cmd, Cmnd, qpti);
1268         if ((in_ptr = load_cmd(Cmnd, cmd, qpti, in_ptr, out_ptr)) == -1)
1269                 goto toss_command;
1270
1271         update_can_queue(host, in_ptr, out_ptr);
1272
1273         return 0;
1274
1275 toss_command:
1276         printk(KERN_EMERG "qlogicpti%d: request queue overflow\n",
1277                qpti->qpti_id);
1278
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.
1282          */
1283         Cmnd->result = DID_BUS_BUSY;
1284         done(Cmnd);
1285         return 1;
1286 }
1287
1288 static int qlogicpti_return_status(struct Status_Entry *sts, int id)
1289 {
1290         int host_status = DID_ERROR;
1291
1292         switch (sts->completion_status) {
1293               case CS_COMPLETE:
1294                 host_status = DID_OK;
1295                 break;
1296               case CS_INCOMPLETE:
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;
1309                 break;
1310               case CS_DMA_ERROR:
1311               case CS_TRANSPORT_ERROR:
1312                 host_status = DID_ERROR;
1313                 break;
1314               case CS_RESET_OCCURRED:
1315               case CS_BUS_RESET:
1316                 host_status = DID_RESET;
1317                 break;
1318               case CS_ABORTED:
1319                 host_status = DID_ABORT;
1320                 break;
1321               case CS_TIMEOUT:
1322                 host_status = DID_TIME_OUT;
1323                 break;
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;
1338                 break;
1339               case CS_DATA_UNDERRUN:
1340                 host_status = DID_OK;
1341                 break;
1342               default:
1343                 printk(KERN_EMERG "qpti%d: unknown completion status 0x%04x\n",
1344                        id, sts->completion_status);
1345                 host_status = DID_ERROR;
1346                 break;
1347         }
1348
1349         return (sts->scsi_status & STATUS_MASK) | (host_status << 16);
1350 }
1351
1352 static struct scsi_cmnd *qlogicpti_intr_handler(struct qlogicpti *qpti)
1353 {
1354         struct scsi_cmnd *Cmnd, *done_queue = NULL;
1355         struct Status_Entry *sts;
1356         u_int in_ptr, out_ptr;
1357
1358         if (!(sbus_readw(qpti->qregs + SBUS_STAT) & SBUS_STAT_RINT))
1359                 return NULL;
1360                 
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;
1368                         break;
1369                 case INVALID_COMMAND:
1370                 case HOST_INTERFACE_ERROR:
1371                 case COMMAND_ERROR:
1372                 case COMMAND_PARAM_ERROR:
1373                         break;
1374                 };
1375                 sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
1376         }
1377
1378         /* This looks like a network driver! */
1379         out_ptr = qpti->res_out_ptr;
1380         while (out_ptr != in_ptr) {
1381                 u_int cmd_slot;
1382
1383                 sts = (struct Status_Entry *) &qpti->res_cpu[out_ptr];
1384                 out_ptr = NEXT_RES_PTR(out_ptr);
1385
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
1389                  */
1390                 cmd_slot = sts->handle;
1391                 Cmnd = qpti->cmd_slots[cmd_slot];
1392                 qpti->cmd_slots[cmd_slot] = NULL;
1393
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;
1398
1399                 if (sts->state_flags & SF_GOT_SENSE)
1400                         memcpy(Cmnd->sense_buffer, sts->req_sense_data,
1401                                sizeof(Cmnd->sense_buffer));
1402
1403                 if (sts->hdr.entry_type == ENTRY_STATUS)
1404                         Cmnd->result =
1405                             qlogicpti_return_status(sts, qpti->qpti_id);
1406                 else
1407                         Cmnd->result = DID_ERROR << 16;
1408
1409                 if (Cmnd->use_sg) {
1410                         sbus_unmap_sg(qpti->sdev,
1411                                       (struct scatterlist *)Cmnd->buffer,
1412                                       Cmnd->use_sg,
1413                                       Cmnd->sc_data_direction);
1414                 } else {
1415                         sbus_unmap_single(qpti->sdev,
1416                                           (__u32)((unsigned long)Cmnd->SCp.ptr),
1417                                           Cmnd->request_bufflen,
1418                                           Cmnd->sc_data_direction);
1419                 }
1420                 qpti->cmd_count[Cmnd->device->id]--;
1421                 sbus_writew(out_ptr, qpti->qregs + MBOX5);
1422                 Cmnd->host_scribble = (unsigned char *) done_queue;
1423                 done_queue = Cmnd;
1424         }
1425         qpti->res_out_ptr = out_ptr;
1426
1427         return done_queue;
1428 }
1429
1430 static irqreturn_t qpti_intr(int irq, void *dev_id, struct pt_regs *regs)
1431 {
1432         struct qlogicpti *qpti = dev_id;
1433         unsigned long flags;
1434         struct scsi_cmnd *dq;
1435
1436         spin_lock_irqsave(qpti->qhost->host_lock, flags);
1437         dq = qlogicpti_intr_handler(qpti);
1438
1439         if (dq != NULL) {
1440                 do {
1441                         struct scsi_cmnd *next;
1442
1443                         next = (struct scsi_cmnd *) dq->host_scribble;
1444                         dq->scsi_done(dq);
1445                         dq = next;
1446                 } while (dq != NULL);
1447         }
1448         spin_unlock_irqrestore(qpti->qhost->host_lock, flags);
1449
1450         return IRQ_HANDLED;
1451 }
1452
1453 static int qlogicpti_abort(struct scsi_cmnd *Cmnd)
1454 {
1455         u_short param[6];
1456         struct Scsi_Host *host = Cmnd->device->host;
1457         struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
1458         int return_status = SUCCESS;
1459         u32 cmd_cookie;
1460         int i;
1461
1462         printk(KERN_WARNING "qlogicpti : Aborting cmd for tgt[%d] lun[%d]\n",
1463                (int)Cmnd->device->id, (int)Cmnd->device->lun);
1464
1465         qlogicpti_disable_irqs(qpti);
1466
1467         /* Find the 32-bit cookie we gave to the firmware for
1468          * this command.
1469          */
1470         for (i = 0; i < QLOGICPTI_REQ_QUEUE_LEN + 1; i++)
1471                 if (qpti->cmd_slots[i] == Cmnd)
1472                         break;
1473         cmd_cookie = i;
1474
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;
1483         }
1484
1485         qlogicpti_enable_irqs(qpti);
1486
1487         return return_status;
1488 }
1489
1490 static int qlogicpti_reset(struct scsi_cmnd *Cmnd)
1491 {
1492         u_short param[6];
1493         struct Scsi_Host *host = Cmnd->device->host;
1494         struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
1495         int return_status = SUCCESS;
1496
1497         printk(KERN_WARNING "qlogicpti : Resetting SCSI bus!\n");
1498
1499         qlogicpti_disable_irqs(qpti);
1500
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;
1507         }
1508
1509         qlogicpti_enable_irqs(qpti);
1510
1511         return return_status;
1512 }
1513
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,
1522         .this_id                = 7,
1523         .sg_tablesize           = QLOGICPTI_MAX_SG(QLOGICPTI_REQ_QUEUE_LEN),
1524         .cmd_per_lun            = 1,
1525         .use_clustering         = ENABLE_CLUSTERING,
1526 };
1527
1528
1529 #include "scsi_module.c"
1530
1531 MODULE_LICENSE("GPL");
1532