2 * linux/drivers/acorn/scsi/fas216.c
4 * Copyright (C) 1997-2003 Russell King
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Based on information in qlogicfas.c by Tom Zerucha, Michael Griffith, and
11 * other sources, including:
12 * the AMD Am53CF94 data sheet
13 * the AMD Am53C94 data sheet
15 * This is a generic driver. To use it, have a look at cumana_2.c. You
16 * should define your own structure that overlays FAS216_Info, eg:
17 * struct my_host_data {
19 * ... my host specific data ...
23 * 30-08-1997 RMK Created
24 * 14-09-1997 RMK Started disconnect support
25 * 08-02-1998 RMK Corrected real DMA support
26 * 15-02-1998 RMK Started sync xfer support
27 * 06-04-1998 RMK Tightened conditions for printing incomplete
29 * 02-05-1998 RMK Added extra checks in fas216_reset
30 * 24-05-1998 RMK Fixed synchronous transfers with period >= 200ns
31 * 27-06-1998 RMK Changed asm/delay.h to linux/delay.h
32 * 26-08-1998 RMK Improved message support wrt MESSAGE_REJECT
33 * 02-04-2000 RMK Converted to use the new error handling, and
34 * automatically request sense data upon check
35 * condition status from targets.
37 #include <linux/module.h>
38 #include <linux/blk.h>
39 #include <linux/kernel.h>
40 #include <linux/string.h>
41 #include <linux/ioport.h>
42 #include <linux/sched.h>
43 #include <linux/proc_fs.h>
44 #include <linux/delay.h>
45 #include <linux/bitops.h>
46 #include <linux/init.h>
47 #include <linux/interrupt.h>
52 #include <asm/ecard.h>
54 #include "../../scsi/scsi.h"
55 #include "../../scsi/hosts.h"
59 /* NOTE: SCSI2 Synchronous transfers *require* DMA according to
60 * the data sheet. This restriction is crazy, especially when
61 * you only want to send 16 bytes! What were the guys who
62 * designed this chip on at that time? Did they read the SCSI2
63 * spec at all? The following sections are taken from the SCSI2
64 * standard (s2r10) concerning this:
66 * > IMPLEMENTORS NOTES:
67 * > (1) Re-negotiation at every selection is not recommended, since a
68 * > significant performance impact is likely.
70 * > The implied synchronous agreement shall remain in effect until a BUS DEVICE
71 * > RESET message is received, until a hard reset condition occurs, or until one
72 * > of the two SCSI devices elects to modify the agreement. The default data
73 * > transfer mode is asynchronous data transfer mode. The default data transfer
74 * > mode is entered at power on, after a BUS DEVICE RESET message, or after a hard
77 * In total, this means that once you have elected to use synchronous
78 * transfers, you must always use DMA.
80 * I was thinking that this was a good chip until I found this restriction ;(
88 #undef CHECK_STRUCTURE
91 #define ABORT_TAG 0x0d
94 #define LOG_CONNECT (1 << 0)
95 #define LOG_BUSSERVICE (1 << 1)
96 #define LOG_FUNCTIONDONE (1 << 2)
97 #define LOG_MESSAGES (1 << 3)
98 #define LOG_BUFFER (1 << 4)
99 #define LOG_ERROR (1 << 8)
101 static int level_mask = LOG_ERROR;
103 MODULE_PARM(level_mask, "i");
105 static inline unsigned char fas216_readb(FAS216_Info *info, unsigned int reg)
107 unsigned int off = reg << info->scsi.io_shift;
109 return inb(info->scsi.io_port + off);
112 static inline void fas216_writeb(FAS216_Info *info, unsigned int reg, unsigned int val)
114 unsigned int off = reg << info->scsi.io_shift;
116 outb(val, info->scsi.io_port + off);
119 static void fas216_dumpstate(FAS216_Info *info)
121 unsigned char is, stat, inst;
123 is = fas216_readb(info, REG_IS);
124 stat = fas216_readb(info, REG_STAT);
125 inst = fas216_readb(info, REG_INST);
127 printk("FAS216: CTCL=%02X CTCM=%02X CMD=%02X STAT=%02X"
128 " INST=%02X IS=%02X CFIS=%02X",
129 fas216_readb(info, REG_CTCL),
130 fas216_readb(info, REG_CTCM),
131 fas216_readb(info, REG_CMD), stat, inst, is,
132 fas216_readb(info, REG_CFIS));
133 printk(" CNTL1=%02X CNTL2=%02X CNTL3=%02X CTCH=%02X\n",
134 fas216_readb(info, REG_CNTL1),
135 fas216_readb(info, REG_CNTL2),
136 fas216_readb(info, REG_CNTL3),
137 fas216_readb(info, REG_CTCH));
140 static void print_SCp(Scsi_Pointer *SCp, const char *prefix, const char *suffix)
142 printk("%sptr %p this_residual 0x%x buffer %p buffers_residual 0x%x%s",
143 prefix, SCp->ptr, SCp->this_residual, SCp->buffer,
144 SCp->buffers_residual, suffix);
147 static void fas216_dumpinfo(FAS216_Info *info)
155 printk("FAS216_Info=\n");
156 printk(" { magic_start=%lX host=%p SCpnt=%p origSCpnt=%p\n",
157 info->magic_start, info->host, info->SCpnt,
159 printk(" scsi={ io_port=%X io_shift=%X irq=%X cfg={ %X %X %X %X }\n",
160 info->scsi.io_port, info->scsi.io_shift, info->scsi.irq,
161 info->scsi.cfg[0], info->scsi.cfg[1], info->scsi.cfg[2],
163 printk(" type=%p phase=%X\n",
164 info->scsi.type, info->scsi.phase);
165 print_SCp(&info->scsi.SCp, " SCp={ ", " }\n");
166 printk(" msgs async_stp=%X disconnectable=%d aborting=%d }\n",
167 info->scsi.async_stp,
168 info->scsi.disconnectable, info->scsi.aborting);
169 printk(" stats={ queues=%X removes=%X fins=%X reads=%X writes=%X miscs=%X\n"
170 " disconnects=%X aborts=%X bus_resets=%X host_resets=%X}\n",
171 info->stats.queues, info->stats.removes, info->stats.fins,
172 info->stats.reads, info->stats.writes, info->stats.miscs,
173 info->stats.disconnects, info->stats.aborts, info->stats.bus_resets,
174 info->stats.host_resets);
175 printk(" ifcfg={ clockrate=%X select_timeout=%X asyncperiod=%X sync_max_depth=%X }\n",
176 info->ifcfg.clockrate, info->ifcfg.select_timeout,
177 info->ifcfg.asyncperiod, info->ifcfg.sync_max_depth);
178 for (i = 0; i < 8; i++) {
179 printk(" busyluns[%d]=%02x dev[%d]={ disconnect_ok=%d stp=%X sof=%X sync_state=%X }\n",
180 i, info->busyluns[i], i,
181 info->device[i].disconnect_ok, info->device[i].stp,
182 info->device[i].sof, info->device[i].sync_state);
184 printk(" dma={ transfer_type=%X setup=%p pseudo=%p stop=%p }\n",
185 info->dma.transfer_type, info->dma.setup,
186 info->dma.pseudo, info->dma.stop);
187 printk(" internal_done=%X magic_end=%lX }\n",
188 info->internal_done, info->magic_end);
191 #ifdef CHECK_STRUCTURE
192 static void __fas216_checkmagic(FAS216_Info *info, const char *func)
195 if (info->magic_start != MAGIC) {
196 printk(KERN_CRIT "FAS216 Error: magic at start corrupted\n");
199 if (info->magic_end != MAGIC) {
200 printk(KERN_CRIT "FAS216 Error: magic at end corrupted\n");
204 fas216_dumpinfo(info);
205 panic("scsi memory space corrupted in %s", func);
208 #define fas216_checkmagic(info) __fas216_checkmagic((info), __FUNCTION__)
210 #define fas216_checkmagic(info)
213 static const char *fas216_bus_phase(int stat)
215 static const char *phases[] = {
216 "DATA OUT", "DATA IN",
218 "MISC OUT", "MISC IN",
219 "MESG OUT", "MESG IN"
222 return phases[stat & STAT_BUSMASK];
225 static const char *fas216_drv_phase(FAS216_Info *info)
227 static const char *phases[] = {
228 [PHASE_IDLE] = "idle",
229 [PHASE_SELECTION] = "selection",
230 [PHASE_COMMAND] = "command",
231 [PHASE_DATAOUT] = "data out",
232 [PHASE_DATAIN] = "data in",
233 [PHASE_MSGIN] = "message in",
234 [PHASE_MSGIN_DISCONNECT]= "disconnect",
235 [PHASE_MSGOUT_EXPECT] = "expect message out",
236 [PHASE_MSGOUT] = "message out",
237 [PHASE_STATUS] = "status",
238 [PHASE_DONE] = "done",
241 if (info->scsi.phase < ARRAY_SIZE(phases) &&
242 phases[info->scsi.phase])
243 return phases[info->scsi.phase];
247 static char fas216_target(FAS216_Info *info)
250 return '0' + info->SCpnt->target;
256 fas216_do_log(FAS216_Info *info, char target, char *fmt, va_list ap)
258 static char buf[1024];
260 vsnprintf(buf, sizeof(buf), fmt, ap);
261 printk("scsi%d.%c: %s", info->host->host_no, target, buf);
265 fas216_log_command(FAS216_Info *info, int level, Scsi_Cmnd *SCpnt, char *fmt, ...)
269 if (level != 0 && !(level & level_mask))
273 fas216_do_log(info, '0' + SCpnt->target, fmt, args);
277 print_command(SCpnt->cmnd);
281 fas216_log_target(FAS216_Info *info, int level, int target, char *fmt, ...)
285 if (level != 0 && !(level & level_mask))
294 fas216_do_log(info, target, fmt, args);
300 static void fas216_log(FAS216_Info *info, int level, char *fmt, ...)
304 if (level != 0 && !(level & level_mask))
308 fas216_do_log(info, fas216_target(info), fmt, args);
316 static struct { int stat, ssr, isr, ph; } ph_list[PH_SIZE];
319 static void add_debug_list(int stat, int ssr, int isr, int ph)
321 ph_list[ph_ptr].stat = stat;
322 ph_list[ph_ptr].ssr = ssr;
323 ph_list[ph_ptr].isr = isr;
324 ph_list[ph_ptr].ph = ph;
326 ph_ptr = (ph_ptr + 1) & (PH_SIZE-1);
329 static struct { int command; void *from; } cmd_list[8];
332 static void fas216_cmd(FAS216_Info *info, unsigned int command)
334 cmd_list[cmd_ptr].command = command;
335 cmd_list[cmd_ptr].from = __builtin_return_address(0);
337 cmd_ptr = (cmd_ptr + 1) & 7;
339 fas216_writeb(info, REG_CMD, command);
342 static void print_debug_list(void)
348 printk(KERN_ERR "SCSI IRQ trail\n");
350 printk(" %02x:%02x:%02x:%1x",
351 ph_list[i].stat, ph_list[i].ssr,
352 ph_list[i].isr, ph_list[i].ph);
353 i = (i + 1) & (PH_SIZE - 1);
354 if (((i ^ ph_ptr) & 7) == 0)
356 } while (i != ph_ptr);
357 if ((i ^ ph_ptr) & 7)
361 printk(KERN_ERR "FAS216 commands: ");
363 printk("%02x:%p ", cmd_list[i].command, cmd_list[i].from);
365 } while (i != cmd_ptr);
369 static void fas216_done(FAS216_Info *info, unsigned int result);
372 * fas216_get_last_msg - retrive last message from the list
373 * @info: interface to search
374 * @pos: current fifo position
376 * Retrieve a last message from the list, using position in fifo.
378 static inline unsigned short
379 fas216_get_last_msg(FAS216_Info *info, int pos)
381 unsigned short packed_msg = NOP;
385 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
386 if (pos >= msg->fifo)
391 if (msg->msg[0] == EXTENDED_MESSAGE)
392 packed_msg = EXTENDED_MESSAGE | msg->msg[2] << 8;
394 packed_msg = msg->msg[0];
397 fas216_log(info, LOG_MESSAGES,
398 "Message: %04x found at position %02x\n", packed_msg, pos);
404 * fas216_syncperiod - calculate STP register value
405 * @info: state structure for interface connected to device
406 * @ns: period in ns (between subsequent bytes)
408 * Calculate value to be loaded into the STP register for a given period
409 * in ns. Returns a value suitable for REG_STP.
411 static int fas216_syncperiod(FAS216_Info *info, int ns)
413 int value = (info->ifcfg.clockrate * ns) / 1000;
415 fas216_checkmagic(info);
426 * fas216_set_sync - setup FAS216 chip for specified transfer period.
427 * @info: state structure for interface connected to device
430 * Correctly setup FAS216 chip for specified transfer period.
431 * Notes : we need to switch the chip out of FASTSCSI mode if we have
432 * a transfer period >= 200ns - otherwise the chip will violate
435 static void fas216_set_sync(FAS216_Info *info, int target)
439 fas216_writeb(info, REG_SOF, info->device[target].sof);
440 fas216_writeb(info, REG_STP, info->device[target].stp);
442 cntl3 = info->scsi.cfg[2];
443 if (info->device[target].period >= (200 / 4))
444 cntl3 = cntl3 & ~CNTL3_FASTSCSI;
446 fas216_writeb(info, REG_CNTL3, cntl3);
449 /* Synchronous transfer support
451 * Note: The SCSI II r10 spec says (5.6.12):
453 * (2) Due to historical problems with early host adapters that could
454 * not accept an SDTR message, some targets may not initiate synchronous
455 * negotiation after a power cycle as required by this standard. Host
456 * adapters that support synchronous mode may avoid the ensuing failure
457 * modes when the target is independently power cycled by initiating a
458 * synchronous negotiation on each REQUEST SENSE and INQUIRY command.
459 * This approach increases the SCSI bus overhead and is not recommended
460 * for new implementations. The correct method is to respond to an
461 * SDTR message with a MESSAGE REJECT message if the either the
462 * initiator or target devices does not support synchronous transfers
463 * or does not want to negotiate for synchronous transfers at the time.
464 * Using the correct method assures compatibility with wide data
465 * transfers and future enhancements.
467 * We will always initiate a synchronous transfer negotiation request on
468 * every INQUIRY or REQUEST SENSE message, unless the target itself has
469 * at some point performed a synchronous transfer negotiation request, or
470 * we have synchronous transfers disabled for this device.
474 * fas216_handlesync - Handle a synchronous transfer message
475 * @info: state structure for interface
476 * @msg: message from target
478 * Handle a synchronous transfer message from the target
480 static void fas216_handlesync(FAS216_Info *info, char *msg)
482 struct fas216_device *dev = &info->device[info->SCpnt->target];
483 enum { sync, async, none, reject } res = none;
488 /* Synchronous transfer request failed.
491 * SCSI devices that are capable of synchronous
492 * data transfers shall not respond to an SDTR
493 * message with a MESSAGE REJECT message.
495 * Hence, if we get this condition, we disable
496 * negotiation for this device.
498 if (dev->sync_state == neg_inprogress) {
499 dev->sync_state = neg_invalid;
504 case EXTENDED_MESSAGE:
505 switch (dev->sync_state) {
506 /* We don't accept synchronous transfer requests.
507 * Respond with a MESSAGE_REJECT to prevent a
508 * synchronous transfer agreement from being reached.
514 /* We were not negotiating a synchronous transfer,
515 * but the device sent us a negotiation request.
516 * Honour the request by sending back a SDTR
517 * message containing our capability, limited by
518 * the targets capability.
521 fas216_cmd(info, CMD_SETATN);
522 if (msg[4] > info->ifcfg.sync_max_depth)
523 msg[4] = info->ifcfg.sync_max_depth;
524 if (msg[3] < 1000 / info->ifcfg.clockrate)
525 msg[3] = 1000 / info->ifcfg.clockrate;
527 msgqueue_flush(&info->scsi.msgs);
528 msgqueue_addmsg(&info->scsi.msgs, 5,
529 EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
531 info->scsi.phase = PHASE_MSGOUT_EXPECT;
533 /* This is wrong. The agreement is not in effect
534 * until this message is accepted by the device
536 dev->sync_state = neg_targcomplete;
540 /* We initiated the synchronous transfer negotiation,
541 * and have successfully received a response from the
542 * target. The synchronous transfer agreement has been
543 * reached. Note: if the values returned are out of our
544 * bounds, we must reject the message.
548 if (msg[4] <= info->ifcfg.sync_max_depth &&
549 msg[3] >= 1000 / info->ifcfg.clockrate) {
550 dev->sync_state = neg_complete;
562 dev->period = msg[3];
564 dev->stp = fas216_syncperiod(info, msg[3] * 4);
565 fas216_set_sync(info, info->SCpnt->target);
569 fas216_cmd(info, CMD_SETATN);
570 msgqueue_flush(&info->scsi.msgs);
571 msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
572 info->scsi.phase = PHASE_MSGOUT_EXPECT;
575 dev->period = info->ifcfg.asyncperiod / 4;
577 dev->stp = info->scsi.async_stp;
578 fas216_set_sync(info, info->SCpnt->target);
587 * fas216_updateptrs - update data pointers after transfer suspended/paused
588 * @info: interface's local pointer to update
589 * @bytes_transferred: number of bytes transferred
591 * Update data pointers after transfer suspended/paused
593 static void fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
595 Scsi_Pointer *SCp = &info->scsi.SCp;
597 fas216_checkmagic(info);
599 BUG_ON(bytes_transferred < 0);
601 info->SCpnt->request_bufflen -= bytes_transferred;
603 while (bytes_transferred != 0) {
604 if (SCp->this_residual > bytes_transferred)
607 * We have used up this buffer. Move on to the
610 bytes_transferred -= SCp->this_residual;
611 if (!next_SCp(SCp) && bytes_transferred) {
612 printk(KERN_WARNING "scsi%d.%c: out of buffers\n",
613 info->host->host_no, '0' + info->SCpnt->target);
618 SCp->this_residual -= bytes_transferred;
619 if (SCp->this_residual)
620 SCp->ptr += bytes_transferred;
626 * fas216_pio - transfer data off of/on to card using programmed IO
627 * @info: interface to transfer data to/from
628 * @direction: direction to transfer data (DMA_OUT/DMA_IN)
630 * Transfer data off of/on to card using programmed IO.
631 * Notes: this is incredibly slow.
633 static void fas216_pio(FAS216_Info *info, fasdmadir_t direction)
635 Scsi_Pointer *SCp = &info->scsi.SCp;
637 fas216_checkmagic(info);
639 if (direction == DMA_OUT)
640 fas216_writeb(info, REG_FF, get_next_SCp_byte(SCp));
642 put_next_SCp_byte(SCp, fas216_readb(info, REG_FF));
644 if (SCp->this_residual == 0)
648 static void fas216_set_stc(FAS216_Info *info, unsigned int length)
650 fas216_writeb(info, REG_STCL, length);
651 fas216_writeb(info, REG_STCM, length >> 8);
652 fas216_writeb(info, REG_STCH, length >> 16);
655 static unsigned int fas216_get_ctc(FAS216_Info *info)
657 return fas216_readb(info, REG_CTCL) +
658 (fas216_readb(info, REG_CTCM) << 8) +
659 (fas216_readb(info, REG_CTCH) << 16);
663 * fas216_cleanuptransfer - clean up after a transfer has completed.
664 * @info: interface to clean up
666 * Update the data pointers according to the number of bytes transferred
669 static void fas216_cleanuptransfer(FAS216_Info *info)
671 unsigned long total, residual, fifo;
672 fasdmatype_t dmatype = info->dma.transfer_type;
674 info->dma.transfer_type = fasdma_none;
677 * PIO transfers do not need to be cleaned up.
679 if (dmatype == fasdma_pio || dmatype == fasdma_none)
682 if (dmatype == fasdma_real_all)
683 total = info->SCpnt->request_bufflen;
685 total = info->scsi.SCp.this_residual;
687 residual = fas216_get_ctc(info);
689 fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
691 fas216_log(info, LOG_BUFFER, "cleaning up from previous "
692 "transfer: length 0x%06x, residual 0x%x, fifo %d",
693 total, residual, fifo);
696 * If we were performing Data-Out, the transfer counter
697 * counts down each time a byte is transferred by the
698 * host to the FIFO. This means we must include the
699 * bytes left in the FIFO from the transfer counter.
701 if (info->scsi.phase == PHASE_DATAOUT)
704 fas216_updateptrs(info, total - residual);
708 * fas216_transfer - Perform a DMA/PIO transfer off of/on to card
709 * @info: interface from which device disconnected from
711 * Start a DMA/PIO transfer off of/on to card
713 static void fas216_transfer(FAS216_Info *info)
715 fasdmadir_t direction;
716 fasdmatype_t dmatype;
718 fas216_log(info, LOG_BUFFER,
719 "starttransfer: buffer %p length 0x%06x reqlen 0x%06x",
720 info->scsi.SCp.ptr, info->scsi.SCp.this_residual,
721 info->SCpnt->request_bufflen);
723 if (!info->scsi.SCp.ptr) {
724 fas216_log(info, LOG_ERROR, "null buffer passed to "
725 "fas216_starttransfer");
726 print_SCp(&info->scsi.SCp, "SCp: ", "\n");
727 print_SCp(&info->SCpnt->SCp, "Cmnd SCp: ", "\n");
732 * If we have a synchronous transfer agreement in effect, we must
733 * use DMA mode. If we are using asynchronous transfers, we may
734 * use DMA mode or PIO mode.
736 if (info->device[info->SCpnt->target].sof)
737 dmatype = fasdma_real_all;
739 dmatype = fasdma_pio;
741 if (info->scsi.phase == PHASE_DATAOUT)
747 dmatype = info->dma.setup(info->host, &info->scsi.SCp,
749 info->dma.transfer_type = dmatype;
751 if (dmatype == fasdma_real_all)
752 fas216_set_stc(info, info->SCpnt->request_bufflen);
754 fas216_set_stc(info, info->scsi.SCp.this_residual);
758 fas216_log(info, LOG_BUFFER, "PIO transfer");
759 fas216_writeb(info, REG_SOF, 0);
760 fas216_writeb(info, REG_STP, info->scsi.async_stp);
761 fas216_cmd(info, CMD_TRANSFERINFO);
762 fas216_pio(info, direction);
766 fas216_log(info, LOG_BUFFER, "pseudo transfer");
767 fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
768 info->dma.pseudo(info->host, &info->scsi.SCp,
769 direction, info->SCpnt->transfersize);
772 case fasdma_real_block:
773 fas216_log(info, LOG_BUFFER, "block dma transfer");
774 fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
777 case fasdma_real_all:
778 fas216_log(info, LOG_BUFFER, "total dma transfer");
779 fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
783 fas216_log(info, LOG_BUFFER | LOG_ERROR,
784 "invalid FAS216 DMA type");
790 * fas216_stoptransfer - Stop a DMA transfer onto / off of the card
791 * @info: interface from which device disconnected from
793 * Called when we switch away from DATA IN or DATA OUT phases.
795 static void fas216_stoptransfer(FAS216_Info *info)
797 fas216_checkmagic(info);
799 if (info->dma.transfer_type == fasdma_real_all ||
800 info->dma.transfer_type == fasdma_real_block)
801 info->dma.stop(info->host, &info->scsi.SCp);
803 fas216_cleanuptransfer(info);
805 if (info->scsi.phase == PHASE_DATAIN) {
809 * If we were performing Data-In, then the FIFO counter
810 * contains the number of bytes not transferred via DMA
811 * from the on-board FIFO. Read them manually.
813 fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
814 while (fifo && info->scsi.SCp.ptr) {
815 *info->scsi.SCp.ptr = fas216_readb(info, REG_FF);
816 fas216_updateptrs(info, 1);
821 * After a Data-Out phase, there may be unsent
822 * bytes left in the FIFO. Flush them out.
824 fas216_cmd(info, CMD_FLUSHFIFO);
828 static void fas216_aborttransfer(FAS216_Info *info)
830 fas216_checkmagic(info);
832 if (info->dma.transfer_type == fasdma_real_all ||
833 info->dma.transfer_type == fasdma_real_block)
834 info->dma.stop(info->host, &info->scsi.SCp);
836 info->dma.transfer_type = fasdma_none;
837 fas216_cmd(info, CMD_FLUSHFIFO);
840 static void fas216_kick(FAS216_Info *info);
843 * fas216_disconnected_intr - handle device disconnection
844 * @info: interface from which device disconnected from
846 * Handle device disconnection
848 static void fas216_disconnect_intr(FAS216_Info *info)
852 fas216_checkmagic(info);
854 fas216_log(info, LOG_CONNECT, "disconnect phase=%02x",
857 msgqueue_flush(&info->scsi.msgs);
859 switch (info->scsi.phase) {
860 case PHASE_SELECTION: /* while selecting - no target */
862 fas216_done(info, DID_NO_CONNECT);
865 case PHASE_MSGIN_DISCONNECT: /* message in - disconnecting */
866 info->scsi.disconnectable = 1;
867 info->scsi.phase = PHASE_IDLE;
868 info->stats.disconnects += 1;
869 spin_lock_irqsave(&info->host_lock, flags);
870 if (info->scsi.phase == PHASE_IDLE)
872 spin_unlock_irqrestore(&info->host_lock, flags);
875 case PHASE_DONE: /* at end of command - complete */
876 fas216_done(info, DID_OK);
879 case PHASE_MSGOUT: /* message out - possible ABORT message */
880 if (fas216_get_last_msg(info, info->scsi.msgin_fifo) == ABORT) {
881 info->scsi.aborting = 0;
882 fas216_done(info, DID_ABORT);
887 printk(KERN_ERR "scsi%d.%c: unexpected disconnect in phase %s\n",
888 info->host->host_no, fas216_target(info), fas216_drv_phase(info));
890 fas216_stoptransfer(info);
891 fas216_done(info, DID_ERROR);
897 * fas216_reselected_intr - start reconnection of a device
898 * @info: interface which was reselected
900 * Start reconnection of a device
903 fas216_reselected_intr(FAS216_Info *info)
905 unsigned int cfis, i;
906 unsigned char msg[4];
907 unsigned char target, lun, tag;
909 fas216_checkmagic(info);
911 if (info->scsi.phase == PHASE_SELECTION ||
912 info->scsi.phase == PHASE_SELSTEPS) {
913 fas216_log(info, LOG_ERROR, "wrong phase in reselected_intr\n");
917 cfis = fas216_readb(info, REG_CFIS);
919 fas216_log(info, LOG_CONNECT, "reconnect phase=%02x cfis=%02x",
920 info->scsi.phase, cfis);
924 if (cfis < 2 || cfis > 4) {
925 printk(KERN_ERR "scsi%d.H: incorrect number of bytes after reselect\n",
926 info->host->host_no);
930 for (i = 0; i < cfis; i++)
931 msg[i] = fas216_readb(info, REG_FF);
933 if (!(msg[0] & (1 << info->host->this_id)) ||
935 goto initiator_error;
937 target = msg[0] & ~(1 << info->host->this_id);
938 target = ffs(target) - 1;
943 if (msg[2] != SIMPLE_QUEUE_TAG)
944 goto initiator_error;
949 /* set up for synchronous transfers */
950 fas216_writeb(info, REG_SDID, target);
951 fas216_set_sync(info, target);
952 msgqueue_flush(&info->scsi.msgs);
954 fas216_log(info, LOG_CONNECT, "Reconnected: target %1x lun %1x tag %02x",
957 if (info->scsi.disconnectable && info->SCpnt) {
958 info->scsi.disconnectable = 0;
959 if (info->SCpnt->target == target &&
960 info->SCpnt->lun == lun &&
961 info->SCpnt->tag == tag) {
962 fas216_log(info, LOG_CONNECT, "reconnected previously connected command");
964 queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
965 fas216_log(info, LOG_CONNECT, "had to move command to disconnected queue");
970 info->SCpnt = queue_remove_tgtluntag(&info->queues.disconnected,
972 fas216_log(info, LOG_CONNECT, "had to get command");
977 * Restore data pointer from SAVED data pointer
979 info->scsi.SCp = info->SCpnt->SCp;
981 fas216_log(info, LOG_CONNECT, "data pointers: [%p, %X]",
982 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
983 info->scsi.phase = PHASE_MSGIN;
986 * Our command structure not found - abort the
987 * command on the target. Since we have no
988 * record of this command, we can't send
989 * an INITIATOR DETECTED ERROR message.
991 fas216_cmd(info, CMD_SETATN);
994 msgqueue_addmsg(&info->scsi.msgs, 2, ABORT_TAG, tag);
996 msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
997 info->scsi.phase = PHASE_MSGOUT_EXPECT;
998 info->scsi.aborting = 1;
1001 fas216_cmd(info, CMD_MSGACCEPTED);
1005 printk(KERN_ERR "scsi%d.H: error during reselection: bytes",
1006 info->host->host_no);
1007 for (i = 0; i < cfis; i++)
1008 printk(" %02x", msg[i]);
1011 fas216_cmd(info, CMD_SETATN);
1012 msgqueue_flush(&info->scsi.msgs);
1013 msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1014 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1015 fas216_cmd(info, CMD_MSGACCEPTED);
1018 static void fas216_parse_message(FAS216_Info *info, unsigned char *message, int msglen)
1022 switch (message[0]) {
1023 case COMMAND_COMPLETE:
1027 printk(KERN_ERR "scsi%d.%c: command complete with no "
1028 "status in MESSAGE_IN?\n",
1029 info->host->host_no, fas216_target(info));
1037 * Save current data pointer to SAVED data pointer
1038 * SCSI II standard says that we must not acknowledge
1039 * this until we have really saved pointers.
1040 * NOTE: we DO NOT save the command nor status pointers
1041 * as required by the SCSI II standard. These always
1042 * point to the start of their respective areas.
1044 info->SCpnt->SCp = info->scsi.SCp;
1045 info->SCpnt->SCp.sent_command = 0;
1046 fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
1047 "save data pointers: [%p, %X]",
1048 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1051 case RESTORE_POINTERS:
1056 * Restore current data pointer from SAVED data pointer
1058 info->scsi.SCp = info->SCpnt->SCp;
1059 fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
1060 "restore data pointers: [%p, 0x%x]",
1061 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1068 info->scsi.phase = PHASE_MSGIN_DISCONNECT;
1071 case MESSAGE_REJECT:
1075 switch (fas216_get_last_msg(info, info->scsi.msgin_fifo)) {
1076 case EXTENDED_MESSAGE | EXTENDED_SDTR << 8:
1077 fas216_handlesync(info, message);
1081 fas216_log(info, 0, "reject, last message 0x%04x",
1082 fas216_get_last_msg(info, info->scsi.msgin_fifo));
1089 case EXTENDED_MESSAGE:
1093 switch (message[2]) {
1094 case EXTENDED_SDTR: /* Sync transfer negotiation request/reply */
1095 fas216_handlesync(info, message);
1109 fas216_log(info, 0, "unrecognised message, rejecting");
1110 printk("scsi%d.%c: message was", info->host->host_no, fas216_target(info));
1111 for (i = 0; i < msglen; i++)
1112 printk("%s%02X", i & 31 ? " " : "\n ", message[i]);
1116 * Something strange seems to be happening here -
1117 * I can't use SETATN since the chip gives me an
1118 * invalid command interrupt when I do. Weird.
1120 fas216_cmd(info, CMD_NOP);
1121 fas216_dumpstate(info);
1122 fas216_cmd(info, CMD_SETATN);
1123 msgqueue_flush(&info->scsi.msgs);
1124 msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
1125 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1126 fas216_dumpstate(info);
1129 static int fas216_wait_cmd(FAS216_Info *info, int cmd)
1134 fas216_cmd(info, cmd);
1136 for (tout = 1000; tout; tout -= 1) {
1137 stat = fas216_readb(info, REG_STAT);
1138 if (stat & (STAT_INT|STAT_PARITYERROR))
1146 static int fas216_get_msg_byte(FAS216_Info *info)
1148 unsigned int stat = fas216_wait_cmd(info, CMD_MSGACCEPTED);
1150 if ((stat & STAT_INT) == 0)
1153 if ((stat & STAT_BUSMASK) != STAT_MESGIN)
1154 goto unexpected_phase_change;
1156 fas216_readb(info, REG_INST);
1158 stat = fas216_wait_cmd(info, CMD_TRANSFERINFO);
1160 if ((stat & STAT_INT) == 0)
1163 if (stat & STAT_PARITYERROR)
1166 if ((stat & STAT_BUSMASK) != STAT_MESGIN)
1167 goto unexpected_phase_change;
1169 fas216_readb(info, REG_INST);
1171 return fas216_readb(info, REG_FF);
1174 fas216_log(info, LOG_ERROR, "timed out waiting for message byte");
1177 unexpected_phase_change:
1178 fas216_log(info, LOG_ERROR, "unexpected phase change: status = %02x", stat);
1182 fas216_log(info, LOG_ERROR, "parity error during message in phase");
1187 * fas216_message - handle a function done interrupt from FAS216 chip
1188 * @info: interface which caused function done interrupt
1190 * Handle a function done interrupt from FAS216 chip
1192 static void fas216_message(FAS216_Info *info)
1194 unsigned char *message = info->scsi.message;
1195 unsigned int msglen = 1;
1198 fas216_checkmagic(info);
1200 message[0] = fas216_readb(info, REG_FF);
1202 if (message[0] == EXTENDED_MESSAGE) {
1203 msgbyte = fas216_get_msg_byte(info);
1206 message[1] = msgbyte;
1208 for (msglen = 2; msglen < message[1] + 2; msglen++) {
1209 msgbyte = fas216_get_msg_byte(info);
1212 message[msglen] = msgbyte;
1222 #ifdef DEBUG_MESSAGES
1226 printk("scsi%d.%c: message in: ",
1227 info->host->host_no, fas216_target(info));
1228 for (i = 0; i < msglen; i++)
1229 printk("%02X ", message[i]);
1234 fas216_parse_message(info, message, msglen);
1235 fas216_cmd(info, CMD_MSGACCEPTED);
1239 fas216_cmd(info, CMD_SETATN);
1240 msgqueue_flush(&info->scsi.msgs);
1241 msgqueue_addmsg(&info->scsi.msgs, 1, MSG_PARITY_ERROR);
1242 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1243 fas216_cmd(info, CMD_MSGACCEPTED);
1248 * fas216_send_command - send command after all message bytes have been sent
1249 * @info: interface which caused bus service
1251 * Send a command to a target after all message bytes have been sent
1253 static void fas216_send_command(FAS216_Info *info)
1257 fas216_checkmagic(info);
1259 fas216_cmd(info, CMD_NOP|CMD_WITHDMA);
1260 fas216_cmd(info, CMD_FLUSHFIFO);
1263 for (i = info->scsi.SCp.sent_command; i < info->SCpnt->cmd_len; i++)
1264 fas216_writeb(info, REG_FF, info->SCpnt->cmnd[i]);
1266 fas216_cmd(info, CMD_TRANSFERINFO);
1268 info->scsi.phase = PHASE_COMMAND;
1272 * fas216_send_messageout - handle bus service to send a message
1273 * @info: interface which caused bus service
1275 * Handle bus service to send a message.
1276 * Note: We do not allow the device to change the data direction!
1278 static void fas216_send_messageout(FAS216_Info *info, int start)
1280 unsigned int tot_msglen = msgqueue_msglength(&info->scsi.msgs);
1282 fas216_checkmagic(info);
1284 fas216_cmd(info, CMD_FLUSHFIFO);
1287 struct message *msg;
1290 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1293 for (i = start; i < msg->length; i++)
1294 fas216_writeb(info, REG_FF, msg->msg[i]);
1296 msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
1300 fas216_writeb(info, REG_FF, NOP);
1302 fas216_cmd(info, CMD_TRANSFERINFO);
1304 info->scsi.phase = PHASE_MSGOUT;
1308 * fas216_busservice_intr - handle bus service interrupt from FAS216 chip
1309 * @info: interface which caused bus service interrupt
1310 * @stat: Status register contents
1311 * @is: SCSI Status register contents
1313 * Handle a bus service interrupt from FAS216 chip
1315 static void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
1317 fas216_checkmagic(info);
1319 fas216_log(info, LOG_BUSSERVICE,
1320 "bus service: stat=%02x is=%02x phase=%02x",
1321 stat, is, info->scsi.phase);
1323 switch (info->scsi.phase) {
1324 case PHASE_SELECTION:
1325 if ((is & IS_BITS) != IS_MSGBYTESENT)
1329 case PHASE_SELSTEPS:
1330 switch (is & IS_BITS) {
1332 case IS_MSGBYTESENT:
1337 if ((stat & STAT_BUSMASK) == STAT_MESGIN)
1349 fas216_cmd(info, CMD_NOP);
1351 #define STATE(st,ph) ((ph) << 3 | (st))
1352 /* This table describes the legal SCSI state transitions,
1353 * as described by the SCSI II spec.
1355 switch (STATE(stat & STAT_BUSMASK, info->scsi.phase)) {
1356 case STATE(STAT_DATAIN, PHASE_SELSTEPS):/* Sel w/ steps -> Data In */
1357 case STATE(STAT_DATAIN, PHASE_MSGOUT): /* Message Out -> Data In */
1358 case STATE(STAT_DATAIN, PHASE_COMMAND): /* Command -> Data In */
1359 case STATE(STAT_DATAIN, PHASE_MSGIN): /* Message In -> Data In */
1360 info->scsi.phase = PHASE_DATAIN;
1361 fas216_transfer(info);
1364 case STATE(STAT_DATAIN, PHASE_DATAIN): /* Data In -> Data In */
1365 case STATE(STAT_DATAOUT, PHASE_DATAOUT):/* Data Out -> Data Out */
1366 fas216_cleanuptransfer(info);
1367 fas216_transfer(info);
1370 case STATE(STAT_DATAOUT, PHASE_SELSTEPS):/* Sel w/ steps-> Data Out */
1371 case STATE(STAT_DATAOUT, PHASE_MSGOUT): /* Message Out -> Data Out */
1372 case STATE(STAT_DATAOUT, PHASE_COMMAND):/* Command -> Data Out */
1373 case STATE(STAT_DATAOUT, PHASE_MSGIN): /* Message In -> Data Out */
1374 fas216_cmd(info, CMD_FLUSHFIFO);
1375 info->scsi.phase = PHASE_DATAOUT;
1376 fas216_transfer(info);
1379 case STATE(STAT_STATUS, PHASE_DATAOUT): /* Data Out -> Status */
1380 case STATE(STAT_STATUS, PHASE_DATAIN): /* Data In -> Status */
1381 fas216_stoptransfer(info);
1382 case STATE(STAT_STATUS, PHASE_SELSTEPS):/* Sel w/ steps -> Status */
1383 case STATE(STAT_STATUS, PHASE_MSGOUT): /* Message Out -> Status */
1384 case STATE(STAT_STATUS, PHASE_COMMAND): /* Command -> Status */
1385 case STATE(STAT_STATUS, PHASE_MSGIN): /* Message In -> Status */
1386 fas216_cmd(info, CMD_INITCMDCOMPLETE);
1387 info->scsi.phase = PHASE_STATUS;
1390 case STATE(STAT_MESGIN, PHASE_DATAOUT): /* Data Out -> Message In */
1391 case STATE(STAT_MESGIN, PHASE_DATAIN): /* Data In -> Message In */
1392 fas216_stoptransfer(info);
1393 case STATE(STAT_MESGIN, PHASE_COMMAND): /* Command -> Message In */
1394 case STATE(STAT_MESGIN, PHASE_SELSTEPS):/* Sel w/ steps -> Message In */
1395 case STATE(STAT_MESGIN, PHASE_MSGOUT): /* Message Out -> Message In */
1396 info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
1397 fas216_cmd(info, CMD_FLUSHFIFO);
1398 fas216_cmd(info, CMD_TRANSFERINFO);
1399 info->scsi.phase = PHASE_MSGIN;
1402 case STATE(STAT_MESGIN, PHASE_MSGIN):
1403 info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
1404 fas216_cmd(info, CMD_TRANSFERINFO);
1407 case STATE(STAT_COMMAND, PHASE_MSGOUT): /* Message Out -> Command */
1408 case STATE(STAT_COMMAND, PHASE_MSGIN): /* Message In -> Command */
1409 fas216_send_command(info);
1410 info->scsi.phase = PHASE_COMMAND;
1415 * Selection -> Message Out
1417 case STATE(STAT_MESGOUT, PHASE_SELECTION):
1418 fas216_send_messageout(info, 1);
1422 * Message Out -> Message Out
1424 case STATE(STAT_MESGOUT, PHASE_SELSTEPS):
1425 case STATE(STAT_MESGOUT, PHASE_MSGOUT):
1427 * If we get another message out phase, this usually
1428 * means some parity error occurred. Resend complete
1429 * set of messages. If we have more than one byte to
1430 * send, we need to assert ATN again.
1432 if (info->device[info->SCpnt->target].parity_check) {
1434 * We were testing... good, the device
1435 * supports parity checking.
1437 info->device[info->SCpnt->target].parity_check = 0;
1438 info->device[info->SCpnt->target].parity_enabled = 1;
1439 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
1442 if (msgqueue_msglength(&info->scsi.msgs) > 1)
1443 fas216_cmd(info, CMD_SETATN);
1447 * Any -> Message Out
1449 case STATE(STAT_MESGOUT, PHASE_MSGOUT_EXPECT):
1450 fas216_send_messageout(info, 0);
1453 /* Error recovery rules.
1454 * These either attempt to abort or retry the operation.
1455 * TODO: we need more of these
1457 case STATE(STAT_COMMAND, PHASE_COMMAND):/* Command -> Command */
1458 /* error - we've sent out all the command bytes
1460 * NOTE: we need SAVE DATA POINTERS/RESTORE DATA POINTERS
1461 * to include the command bytes sent for this to work
1464 printk(KERN_ERR "scsi%d.%c: "
1465 "target trying to receive more command bytes\n",
1466 info->host->host_no, fas216_target(info));
1467 fas216_cmd(info, CMD_SETATN);
1468 fas216_set_stc(info, 15);
1469 fas216_cmd(info, CMD_PADBYTES | CMD_WITHDMA);
1470 msgqueue_flush(&info->scsi.msgs);
1471 msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1472 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1476 if (info->scsi.phase == PHASE_MSGIN_DISCONNECT) {
1477 printk(KERN_ERR "scsi%d.%c: disconnect message received, but bus service %s?\n",
1478 info->host->host_no, fas216_target(info),
1479 fas216_bus_phase(stat));
1480 msgqueue_flush(&info->scsi.msgs);
1481 fas216_cmd(info, CMD_SETATN);
1482 msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1483 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1484 info->scsi.aborting = 1;
1485 fas216_cmd(info, CMD_TRANSFERINFO);
1488 printk(KERN_ERR "scsi%d.%c: bus phase %s after %s?\n",
1489 info->host->host_no, fas216_target(info),
1490 fas216_bus_phase(stat),
1491 fas216_drv_phase(info));
1496 fas216_log(info, 0, "bus service at step %d?", is & IS_BITS);
1497 fas216_dumpstate(info);
1500 fas216_done(info, DID_ERROR);
1504 * fas216_funcdone_intr - handle a function done interrupt from FAS216 chip
1505 * @info: interface which caused function done interrupt
1506 * @stat: Status register contents
1507 * @is: SCSI Status register contents
1509 * Handle a function done interrupt from FAS216 chip
1511 static void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
1513 unsigned int fifo_len = fas216_readb(info, REG_CFIS) & CFIS_CF;
1515 fas216_checkmagic(info);
1517 fas216_log(info, LOG_FUNCTIONDONE,
1518 "function done: stat=%02x is=%02x phase=%02x",
1519 stat, is, info->scsi.phase);
1521 switch (info->scsi.phase) {
1522 case PHASE_STATUS: /* status phase - read status and msg */
1523 if (fifo_len != 2) {
1524 fas216_log(info, 0, "odd number of bytes in FIFO: %d", fifo_len);
1527 * Read status then message byte.
1529 info->scsi.SCp.Status = fas216_readb(info, REG_FF);
1530 info->scsi.SCp.Message = fas216_readb(info, REG_FF);
1531 info->scsi.phase = PHASE_DONE;
1532 fas216_cmd(info, CMD_MSGACCEPTED);
1536 case PHASE_SELECTION:
1537 case PHASE_SELSTEPS:
1540 case PHASE_MSGIN: /* message in phase */
1541 if ((stat & STAT_BUSMASK) == STAT_MESGIN) {
1542 info->scsi.msgin_fifo = fifo_len;
1543 fas216_message(info);
1548 fas216_log(info, 0, "internal phase %s for function done?"
1549 " What do I do with this?",
1550 fas216_drv_phase(info));
1554 static void fas216_bus_reset(FAS216_Info *info)
1559 msgqueue_flush(&info->scsi.msgs);
1561 sync_state = neg_invalid;
1564 if (info->ifcfg.capabilities & (FASCAP_DMA|FASCAP_PSEUDODMA))
1565 sync_state = neg_wait;
1568 info->scsi.phase = PHASE_IDLE;
1569 info->SCpnt = NULL; /* bug! */
1570 memset(&info->scsi.SCp, 0, sizeof(info->scsi.SCp));
1572 for (i = 0; i < 8; i++) {
1573 info->device[i].disconnect_ok = info->ifcfg.disconnect_ok;
1574 info->device[i].sync_state = sync_state;
1575 info->device[i].period = info->ifcfg.asyncperiod / 4;
1576 info->device[i].stp = info->scsi.async_stp;
1577 info->device[i].sof = 0;
1578 info->device[i].wide_xfer = 0;
1581 info->rst_bus_status = 1;
1582 wake_up(&info->eh_wait);
1586 * fas216_intr - handle interrupts to progress a command
1587 * @info: interface to service
1589 * Handle interrupts from the interface to progress a command
1591 void fas216_intr(FAS216_Info *info)
1593 unsigned char inst, is, stat;
1595 fas216_checkmagic(info);
1597 stat = fas216_readb(info, REG_STAT);
1598 is = fas216_readb(info, REG_IS);
1599 inst = fas216_readb(info, REG_INST);
1601 add_debug_list(stat, is, inst, info->scsi.phase);
1603 if (stat & STAT_INT) {
1604 if (inst & INST_BUSRESET) {
1605 fas216_log(info, 0, "bus reset detected");
1606 fas216_bus_reset(info);
1607 scsi_report_bus_reset(info->host, 0);
1608 } else if (inst & INST_ILLEGALCMD) {
1609 fas216_log(info, LOG_ERROR, "illegal command given\n");
1610 fas216_dumpstate(info);
1612 } else if (inst & INST_DISCONNECT)
1613 fas216_disconnect_intr(info);
1614 else if (inst & INST_RESELECTED) /* reselected */
1615 fas216_reselected_intr(info);
1616 else if (inst & INST_BUSSERVICE) /* bus service request */
1617 fas216_busservice_intr(info, stat, is);
1618 else if (inst & INST_FUNCDONE) /* function done */
1619 fas216_funcdone_intr(info, stat, is);
1621 fas216_log(info, 0, "unknown interrupt received:"
1622 " phase %s inst %02X is %02X stat %02X",
1623 fas216_drv_phase(info), inst, is, stat);
1627 static void __fas216_start_command(FAS216_Info *info, Scsi_Cmnd *SCpnt)
1631 /* following what the ESP driver says */
1632 fas216_set_stc(info, 0);
1633 fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
1636 fas216_cmd(info, CMD_FLUSHFIFO);
1638 /* load bus-id and timeout */
1639 fas216_writeb(info, REG_SDID, BUSID(SCpnt->target));
1640 fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
1642 /* synchronous transfers */
1643 fas216_set_sync(info, SCpnt->target);
1645 tot_msglen = msgqueue_msglength(&info->scsi.msgs);
1647 #ifdef DEBUG_MESSAGES
1649 struct message *msg;
1652 printk("scsi%d.%c: message out: ",
1653 info->host->host_no, '0' + SCpnt->target);
1654 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1656 for (i = 0; i < msg->length; i++)
1657 printk("%02x ", msg->msg[i]);
1664 if (tot_msglen == 1 || tot_msglen == 3) {
1666 * We have an easy message length to send...
1668 struct message *msg;
1671 info->scsi.phase = PHASE_SELSTEPS;
1673 /* load message bytes */
1674 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1675 for (i = 0; i < msg->length; i++)
1676 fas216_writeb(info, REG_FF, msg->msg[i]);
1677 msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
1681 for (i = 0; i < SCpnt->cmd_len; i++)
1682 fas216_writeb(info, REG_FF, SCpnt->cmnd[i]);
1684 if (tot_msglen == 1)
1685 fas216_cmd(info, CMD_SELECTATN);
1687 fas216_cmd(info, CMD_SELECTATN3);
1690 * We have an unusual number of message bytes to send.
1691 * Load first byte into fifo, and issue SELECT with ATN and
1694 struct message *msg = msgqueue_getmsg(&info->scsi.msgs, 0);
1696 fas216_writeb(info, REG_FF, msg->msg[0]);
1699 fas216_cmd(info, CMD_SELECTATNSTOP);
1704 * Decide whether we need to perform a parity test on this device.
1705 * Can also be used to force parity error conditions during initial
1706 * information transfer phase (message out) for test purposes.
1708 static int parity_test(FAS216_Info *info, int target)
1712 info->device[target].parity_check = 0;
1716 return info->device[target].parity_check;
1719 static void fas216_start_command(FAS216_Info *info, Scsi_Cmnd *SCpnt)
1726 info->scsi.phase = PHASE_SELECTION;
1727 info->scsi.SCp = SCpnt->SCp;
1728 info->SCpnt = SCpnt;
1729 info->dma.transfer_type = fasdma_none;
1731 if (parity_test(info, SCpnt->target))
1732 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_PTE);
1734 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
1737 * Don't allow request sense commands to disconnect.
1739 disconnect_ok = SCpnt->cmnd[0] != REQUEST_SENSE &&
1740 info->device[SCpnt->target].disconnect_ok;
1743 * build outgoing message bytes
1745 msgqueue_flush(&info->scsi.msgs);
1746 msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(disconnect_ok, SCpnt->lun));
1749 * add tag message if required
1752 msgqueue_addmsg(&info->scsi.msgs, 2, SIMPLE_QUEUE_TAG, SCpnt->tag);
1756 if ((info->device[SCpnt->target].sync_state == neg_wait ||
1757 info->device[SCpnt->target].sync_state == neg_complete) &&
1758 (SCpnt->cmnd[0] == REQUEST_SENSE ||
1759 SCpnt->cmnd[0] == INQUIRY)) {
1760 info->device[SCpnt->target].sync_state = neg_inprogress;
1761 msgqueue_addmsg(&info->scsi.msgs, 5,
1762 EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
1763 1000 / info->ifcfg.clockrate,
1764 info->ifcfg.sync_max_depth);
1770 __fas216_start_command(info, SCpnt);
1773 static void fas216_allocate_tag(FAS216_Info *info, Scsi_Cmnd *SCpnt)
1777 * tagged queuing - allocate a new tag to this command
1779 if (SCpnt->device->tagged_queue && SCpnt->cmnd[0] != REQUEST_SENSE &&
1780 SCpnt->cmnd[0] != INQUIRY) {
1781 SCpnt->device->current_tag += 1;
1782 if (SCpnt->device->current_tag == 0)
1783 SCpnt->device->current_tag = 1;
1784 SCpnt->tag = SCpnt->device->current_tag;
1787 set_bit(SCpnt->target * 8 + SCpnt->lun, info->busyluns);
1789 info->stats.removes += 1;
1790 switch (SCpnt->cmnd[0]) {
1794 info->stats.writes += 1;
1799 info->stats.reads += 1;
1802 info->stats.miscs += 1;
1807 static void fas216_do_bus_device_reset(FAS216_Info *info, Scsi_Cmnd *SCpnt)
1809 struct message *msg;
1814 info->scsi.phase = PHASE_SELECTION;
1815 info->scsi.SCp = SCpnt->SCp;
1816 info->SCpnt = SCpnt;
1817 info->dma.transfer_type = fasdma_none;
1819 fas216_log(info, LOG_ERROR, "sending bus device reset");
1821 msgqueue_flush(&info->scsi.msgs);
1822 msgqueue_addmsg(&info->scsi.msgs, 1, BUS_DEVICE_RESET);
1824 /* following what the ESP driver says */
1825 fas216_set_stc(info, 0);
1826 fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
1829 fas216_cmd(info, CMD_FLUSHFIFO);
1831 /* load bus-id and timeout */
1832 fas216_writeb(info, REG_SDID, BUSID(SCpnt->target));
1833 fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
1835 /* synchronous transfers */
1836 fas216_set_sync(info, SCpnt->target);
1838 msg = msgqueue_getmsg(&info->scsi.msgs, 0);
1840 fas216_writeb(info, REG_FF, BUS_DEVICE_RESET);
1843 fas216_cmd(info, CMD_SELECTATNSTOP);
1847 * fas216_kick - kick a command to the interface
1848 * @info: our host interface to kick
1850 * Kick a command to the interface, interface should be idle.
1851 * Notes: Interrupts are always disabled!
1853 static void fas216_kick(FAS216_Info *info)
1855 Scsi_Cmnd *SCpnt = NULL;
1856 #define TYPE_OTHER 0
1857 #define TYPE_RESET 1
1858 #define TYPE_QUEUE 2
1859 int where_from = TYPE_OTHER;
1861 fas216_checkmagic(info);
1864 * Obtain the next command to process.
1867 if (info->rstSCpnt) {
1868 SCpnt = info->rstSCpnt;
1869 /* don't remove it */
1870 where_from = TYPE_RESET;
1874 if (info->reqSCpnt) {
1875 SCpnt = info->reqSCpnt;
1876 info->reqSCpnt = NULL;
1880 if (info->origSCpnt) {
1881 SCpnt = info->origSCpnt;
1882 info->origSCpnt = NULL;
1886 /* retrieve next command */
1888 SCpnt = queue_remove_exclude(&info->queues.issue,
1890 where_from = TYPE_QUEUE;
1897 * no command pending, so enable reselection.
1899 fas216_cmd(info, CMD_ENABLESEL);
1904 * We're going to start a command, so disable reselection
1906 fas216_cmd(info, CMD_DISABLESEL);
1908 if (info->scsi.disconnectable && info->SCpnt) {
1909 fas216_log(info, LOG_CONNECT,
1910 "moved command for %d to disconnected queue",
1911 info->SCpnt->target);
1912 queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
1913 info->scsi.disconnectable = 0;
1917 fas216_log_command(info, LOG_CONNECT | LOG_MESSAGES, SCpnt, "starting ");
1919 switch (where_from) {
1921 fas216_allocate_tag(info, SCpnt);
1923 fas216_start_command(info, SCpnt);
1926 fas216_do_bus_device_reset(info, SCpnt);
1930 fas216_log(info, LOG_CONNECT, "select: data pointers [%p, %X]",
1931 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1934 * should now get either DISCONNECT or
1935 * (FUNCTION DONE with BUS SERVICE) interrupt
1940 * Clean up from issuing a BUS DEVICE RESET message to a device.
1943 fas216_devicereset_done(FAS216_Info *info, Scsi_Cmnd *SCpnt, unsigned int result)
1945 fas216_log(info, LOG_ERROR, "fas216 device reset complete");
1947 info->rstSCpnt = NULL;
1948 info->rst_dev_status = 1;
1949 wake_up(&info->eh_wait);
1953 * fas216_rq_sns_done - Finish processing automatic request sense command
1954 * @info: interface that completed
1955 * @SCpnt: command that completed
1956 * @result: driver byte of result
1958 * Finish processing automatic request sense command
1961 fas216_rq_sns_done(FAS216_Info *info, Scsi_Cmnd *SCpnt, unsigned int result)
1963 fas216_log_target(info, LOG_CONNECT, SCpnt->target,
1964 "request sense complete, result=0x%04x%02x%02x",
1965 result, SCpnt->SCp.Message, SCpnt->SCp.Status);
1967 if (result != DID_OK || SCpnt->SCp.Status != GOOD)
1969 * Something went wrong. Make sure that we don't
1970 * have valid data in the sense buffer that could
1971 * confuse the higher levels.
1973 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
1974 //printk("scsi%d.%c: sense buffer: ", info->host->host_no, '0' + SCpnt->target);
1975 //{ int i; for (i = 0; i < 32; i++) printk("%02x ", SCpnt->sense_buffer[i]); printk("\n"); }
1977 * Note that we don't set SCpnt->result, since that should
1978 * reflect the status of the command that we were asked by
1979 * the upper layers to process. This would have been set
1980 * correctly by fas216_std_done.
1982 SCpnt->scsi_done(SCpnt);
1986 * fas216_std_done - finish processing of standard command
1987 * @info: interface that completed
1988 * @SCpnt: command that completed
1989 * @result: driver byte of result
1991 * Finish processing of standard command
1994 fas216_std_done(FAS216_Info *info, Scsi_Cmnd *SCpnt, unsigned int result)
1996 info->stats.fins += 1;
1998 SCpnt->result = result << 16 | info->scsi.SCp.Message << 8 |
1999 info->scsi.SCp.Status;
2001 fas216_log_command(info, LOG_CONNECT, SCpnt,
2002 "command complete, result=0x%08x ", SCpnt->result);
2005 * If the driver detected an error, we're all done.
2007 if (host_byte(SCpnt->result) != DID_OK ||
2008 msg_byte(SCpnt->result) != COMMAND_COMPLETE)
2012 * If the command returned CHECK_CONDITION or COMMAND_TERMINATED
2013 * status, request the sense information.
2015 if (status_byte(SCpnt->result) == CHECK_CONDITION ||
2016 status_byte(SCpnt->result) == COMMAND_TERMINATED)
2020 * If the command did not complete with GOOD status,
2021 * we are all done here.
2023 if (status_byte(SCpnt->result) != GOOD)
2027 * We have successfully completed a command. Make sure that
2028 * we do not have any buffers left to transfer. The world
2029 * is not perfect, and we seem to occasionally hit this.
2030 * It can be indicative of a buggy driver, target or the upper
2031 * levels of the SCSI code.
2033 if (info->scsi.SCp.ptr) {
2034 switch (SCpnt->cmnd[0]) {
2041 printk(KERN_ERR "scsi%d.%c: incomplete data transfer "
2042 "detected: res=%08X ptr=%p len=%X CDB: ",
2043 info->host->host_no, '0' + SCpnt->target,
2044 SCpnt->result, info->scsi.SCp.ptr,
2045 info->scsi.SCp.this_residual);
2046 print_command(SCpnt->cmnd);
2047 SCpnt->result &= ~(255 << 16);
2048 SCpnt->result |= DID_BAD_TARGET << 16;
2054 if (SCpnt->scsi_done) {
2055 SCpnt->scsi_done(SCpnt);
2059 panic("scsi%d.H: null scsi_done function in fas216_done",
2060 info->host->host_no);
2064 if (SCpnt->cmnd[0] == REQUEST_SENSE)
2067 fas216_log_target(info, LOG_CONNECT, SCpnt->target,
2068 "requesting sense");
2069 memset(SCpnt->cmnd, 0, sizeof (SCpnt->cmnd));
2070 SCpnt->cmnd[0] = REQUEST_SENSE;
2071 SCpnt->cmnd[1] = SCpnt->lun << 5;
2072 SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
2073 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
2074 SCpnt->SCp.buffer = NULL;
2075 SCpnt->SCp.buffers_residual = 0;
2076 SCpnt->SCp.ptr = (char *)SCpnt->sense_buffer;
2077 SCpnt->SCp.this_residual = sizeof(SCpnt->sense_buffer);
2078 SCpnt->SCp.Message = 0;
2079 SCpnt->SCp.Status = 0;
2080 SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
2081 SCpnt->sc_data_direction = SCSI_DATA_READ;
2084 SCpnt->host_scribble = (void *)fas216_rq_sns_done;
2087 * Place this command into the high priority "request
2088 * sense" slot. This will be the very next command
2089 * executed, unless a target connects to us.
2092 printk(KERN_WARNING "scsi%d.%c: loosing request command\n",
2093 info->host->host_no, '0' + SCpnt->target);
2094 info->reqSCpnt = SCpnt;
2098 * fas216_done - complete processing for current command
2099 * @info: interface that completed
2100 * @result: driver byte of result
2102 * Complete processing for current command
2104 static void fas216_done(FAS216_Info *info, unsigned int result)
2106 void (*fn)(FAS216_Info *, Scsi_Cmnd *, unsigned int);
2108 unsigned long flags;
2110 fas216_checkmagic(info);
2115 SCpnt = info->SCpnt;
2117 info->scsi.phase = PHASE_IDLE;
2119 if (info->scsi.aborting) {
2120 fas216_log(info, 0, "uncaught abort - returning DID_ABORT");
2122 info->scsi.aborting = 0;
2126 * Sanity check the completion - if we have zero bytes left
2127 * to transfer, we should not have a valid pointer.
2129 if (info->scsi.SCp.ptr && info->scsi.SCp.this_residual == 0) {
2130 printk("scsi%d.%c: zero bytes left to transfer, but "
2131 "buffer pointer still valid: ptr=%p len=%08x CDB: ",
2132 info->host->host_no, '0' + SCpnt->target,
2133 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
2134 info->scsi.SCp.ptr = NULL;
2135 print_command(SCpnt->cmnd);
2139 * Clear down this command as completed. If we need to request
2140 * the sense information, fas216_kick will re-assert the busy
2143 info->device[SCpnt->target].parity_check = 0;
2144 clear_bit(SCpnt->target * 8 + SCpnt->lun, info->busyluns);
2146 fn = (void (*)(FAS216_Info *, Scsi_Cmnd *, unsigned int))SCpnt->host_scribble;
2147 fn(info, SCpnt, result);
2149 if (info->scsi.irq != NO_IRQ) {
2150 spin_lock_irqsave(&info->host_lock, flags);
2151 if (info->scsi.phase == PHASE_IDLE)
2153 spin_unlock_irqrestore(&info->host_lock, flags);
2158 panic("scsi%d.H: null command in fas216_done",
2159 info->host->host_no);
2163 * fas216_queue_command - queue a command for adapter to process.
2164 * @SCpnt: Command to queue
2165 * @done: done function to call once command is complete
2167 * Queue a command for adapter to process.
2168 * Returns: 0 on success, else error.
2169 * Notes: io_request_lock is held, interrupts are disabled.
2171 int fas216_queue_command(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2173 FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
2176 fas216_checkmagic(info);
2178 fas216_log_command(info, LOG_CONNECT, SCpnt,
2179 "received command (%p) ", SCpnt);
2181 SCpnt->scsi_done = done;
2182 SCpnt->host_scribble = (void *)fas216_std_done;
2187 info->stats.queues += 1;
2190 spin_lock(&info->host_lock);
2193 * Add command into execute queue and let it complete under
2194 * whatever scheme we're using.
2196 result = !queue_add_cmd_ordered(&info->queues.issue, SCpnt);
2199 * If we successfully added the command,
2200 * kick the interface to get it moving.
2202 if (result == 0 && info->scsi.phase == PHASE_IDLE)
2204 spin_unlock(&info->host_lock);
2206 fas216_log_target(info, LOG_CONNECT, -1, "queue %s",
2207 result ? "failure" : "success");
2213 * fas216_internal_done - trigger restart of a waiting thread in fas216_command
2214 * @SCpnt: Command to wake
2216 * Trigger restart of a waiting thread in fas216_command
2218 static void fas216_internal_done(Scsi_Cmnd *SCpnt)
2220 FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
2222 fas216_checkmagic(info);
2224 info->internal_done = 1;
2228 * fas216_command - queue a command for adapter to process.
2229 * @SCpnt: Command to queue
2231 * Queue a command for adapter to process.
2232 * Returns: scsi result code.
2233 * Notes: io_request_lock is held, interrupts are disabled.
2235 int fas216_command(Scsi_Cmnd *SCpnt)
2237 FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
2239 fas216_checkmagic(info);
2242 * We should only be using this if we don't have an interrupt.
2243 * Provide some "incentive" to use the queueing code.
2245 BUG_ON(info->scsi.irq != NO_IRQ);
2247 info->internal_done = 0;
2248 fas216_queue_command(SCpnt, fas216_internal_done);
2251 * This wastes time, since we can't return until the command is
2252 * complete. We can't sleep either since we may get re-entered!
2253 * However, we must re-enable interrupts, or else we'll be
2256 spin_unlock_irq(&io_request_lock);
2258 while (!info->internal_done) {
2260 * If we don't have an IRQ, then we must poll the card for
2261 * it's interrupt, and use that to call this driver's
2262 * interrupt routine. That way, we keep the command
2263 * progressing. Maybe we can add some inteligence here
2264 * and go to sleep if we know that the device is going
2265 * to be some time (eg, disconnected).
2267 if (fas216_readb(info, REG_STAT) & STAT_INT) {
2268 spin_lock_irq(&io_request_lock);
2270 spin_unlock_irq(&io_request_lock);
2274 spin_lock_irq(&io_request_lock);
2276 return SCpnt->result;
2280 * Error handler timeout function. Indicate that we timed out,
2281 * and wake up any error handler process so it can continue.
2283 static void fas216_eh_timer(unsigned long data)
2285 FAS216_Info *info = (FAS216_Info *)data;
2287 fas216_log(info, LOG_ERROR, "error handling timed out\n");
2289 del_timer(&info->eh_timer);
2291 if (info->rst_bus_status == 0)
2292 info->rst_bus_status = -1;
2293 if (info->rst_dev_status == 0)
2294 info->rst_dev_status = -1;
2296 wake_up(&info->eh_wait);
2300 res_failed, /* not found */
2301 res_success, /* command on issue queue */
2302 res_hw_abort /* command on disconnected dev */
2306 * fas216_do_abort - decide how to abort a command
2307 * @SCpnt: command to abort
2309 * Decide how to abort a command.
2310 * Returns: abort status
2312 static enum res_find fas216_find_command(FAS216_Info *info, Scsi_Cmnd *SCpnt)
2314 enum res_find res = res_failed;
2316 if (queue_remove_cmd(&info->queues.issue, SCpnt)) {
2318 * The command was on the issue queue, and has not been
2319 * issued yet. We can remove the command from the queue,
2320 * and acknowledge the abort. Neither the device nor the
2321 * interface know about the command.
2323 printk("on issue queue ");
2326 } else if (queue_remove_cmd(&info->queues.disconnected, SCpnt)) {
2328 * The command was on the disconnected queue. We must
2329 * reconnect with the device if possible, and send it
2332 printk("on disconnected queue ");
2335 } else if (info->SCpnt == SCpnt) {
2336 printk("executing ");
2338 switch (info->scsi.phase) {
2340 * If the interface is idle, and the command is 'disconnectable',
2341 * then it is the same as on the disconnected queue.
2344 if (info->scsi.disconnectable) {
2345 info->scsi.disconnectable = 0;
2354 } else if (info->origSCpnt == SCpnt) {
2356 * The command will be executed next, but a command
2357 * is currently using the interface. This is similar to
2358 * being on the issue queue, except the busylun bit has
2361 info->origSCpnt = NULL;
2362 clear_bit(SCpnt->target * 8 + SCpnt->lun, info->busyluns);
2363 printk("waiting for execution ");
2372 * fas216_eh_abort - abort this command
2373 * @SCpnt: command to abort
2375 * Abort this command.
2376 * Returns: FAILED if unable to abort
2377 * Notes: io_request_lock is taken, and irqs are disabled
2379 int fas216_eh_abort(Scsi_Cmnd *SCpnt)
2381 FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
2382 int result = FAILED;
2384 fas216_checkmagic(info);
2386 info->stats.aborts += 1;
2388 printk(KERN_WARNING "scsi%d: abort command ", info->host->host_no);
2389 print_command(SCpnt->data_cmnd);
2392 fas216_dumpstate(info);
2394 printk(KERN_WARNING "scsi%d: abort %p ", info->host->host_no, SCpnt);
2396 switch (fas216_find_command(info, SCpnt)) {
2398 * We found the command, and cleared it out. Either
2399 * the command is still known to be executing on the
2400 * target, or the busylun bit is not set.
2403 printk("success\n");
2408 * We need to reconnect to the target and send it an
2409 * ABORT or ABORT_TAG message. We can only do this
2410 * if the bus is free.
2416 * We are unable to abort the command for some reason.
2428 * fas216_eh_device_reset - Reset the device associated with this command
2429 * @SCpnt: command specifing device to reset
2431 * Reset the device associated with this command.
2432 * Returns: FAILED if unable to reset.
2433 * Notes: We won't be re-entered, so we'll only have one device
2434 * reset on the go at one time.
2436 int fas216_eh_device_reset(Scsi_Cmnd *SCpnt)
2438 FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
2439 unsigned long flags;
2440 int i, res = FAILED, target = SCpnt->target;
2442 fas216_log(info, LOG_ERROR, "device reset for target %d", target);
2444 spin_lock_irqsave(&info->host_lock, flags);
2448 * If we are currently connected to a device, and
2449 * it is the device we want to reset, there is
2450 * nothing we can do here. Chances are it is stuck,
2451 * and we need a bus reset.
2453 if (info->SCpnt && !info->scsi.disconnectable &&
2454 info->SCpnt->target == SCpnt->target)
2458 * We're going to be resetting this device. Remove
2459 * all pending commands from the driver. By doing
2460 * so, we guarantee that we won't touch the command
2461 * structures except to process the reset request.
2463 queue_remove_all_target(&info->queues.issue, target);
2464 queue_remove_all_target(&info->queues.disconnected, target);
2465 if (info->origSCpnt && info->origSCpnt->target == target)
2466 info->origSCpnt = NULL;
2467 if (info->reqSCpnt && info->reqSCpnt->target == target)
2468 info->reqSCpnt = NULL;
2469 for (i = 0; i < 8; i++)
2470 clear_bit(target * 8 + i, info->busyluns);
2473 * Hijack this SCSI command structure to send
2474 * a bus device reset message to this device.
2476 SCpnt->host_scribble = (void *)fas216_devicereset_done;
2478 info->rst_dev_status = 0;
2479 info->rstSCpnt = SCpnt;
2481 if (info->scsi.phase == PHASE_IDLE)
2484 mod_timer(&info->eh_timer, 30 * HZ);
2485 spin_unlock_irqrestore(&info->host_lock, flags);
2488 * Wait up to 30 seconds for the reset to complete.
2490 wait_event(info->eh_wait, info->rst_dev_status);
2492 del_timer_sync(&info->eh_timer);
2493 spin_lock_irqsave(&info->host_lock, flags);
2494 info->rstSCpnt = NULL;
2496 if (info->rst_dev_status == 1)
2500 SCpnt->host_scribble = NULL;
2501 spin_unlock_irqrestore(&info->host_lock, flags);
2503 fas216_log(info, LOG_ERROR, "device reset complete: %s\n",
2504 res == SUCCESS ? "success" : "failed");
2510 * fas216_eh_bus_reset - Reset the bus associated with the command
2511 * @SCpnt: command specifing bus to reset
2513 * Reset the bus associated with the command.
2514 * Returns: FAILED if unable to reset.
2515 * Notes: Further commands are blocked.
2517 int fas216_eh_bus_reset(Scsi_Cmnd *SCpnt)
2519 FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
2520 unsigned long flags;
2523 fas216_checkmagic(info);
2524 fas216_log(info, LOG_ERROR, "resetting bus");
2526 info->stats.bus_resets += 1;
2528 spin_lock_irqsave(&info->host_lock, flags);
2531 * Stop all activity on this interface.
2533 fas216_aborttransfer(info);
2534 fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
2537 * Clear any pending interrupts.
2539 while (fas216_readb(info, REG_STAT) & STAT_INT)
2540 fas216_readb(info, REG_INST);
2542 info->rst_bus_status = 0;
2545 * For each attached hard-reset device, clear out
2546 * all command structures. Leave the running
2549 for (SDpnt = info->host->host_queue; SDpnt; SDpnt = SDpnt->next) {
2552 if (SDpnt->soft_reset)
2555 queue_remove_all_target(&info->queues.issue, SDpnt->id);
2556 queue_remove_all_target(&info->queues.disconnected, SDpnt->id);
2557 if (info->origSCpnt && info->origSCpnt->target == SDpnt->id)
2558 info->origSCpnt = NULL;
2559 if (info->reqSCpnt && info->reqSCpnt->target == SDpnt->id)
2560 info->reqSCpnt = NULL;
2563 for (i = 0; i < 8; i++)
2564 clear_bit(SDpnt->id * 8 + i, info->busyluns);
2567 info->scsi.phase = PHASE_IDLE;
2570 * Reset the SCSI bus. Device cleanup happens in
2571 * the interrupt handler.
2573 fas216_cmd(info, CMD_RESETSCSI);
2575 mod_timer(&info->eh_timer, jiffies + HZ);
2576 spin_unlock_irqrestore(&info->host_lock, flags);
2579 * Wait one second for the interrupt.
2581 wait_event(info->eh_wait, info->rst_bus_status);
2582 del_timer_sync(&info->eh_timer);
2584 fas216_log(info, LOG_ERROR, "bus reset complete: %s\n",
2585 info->rst_bus_status == 1 ? "success" : "failed");
2587 return info->rst_bus_status == 1 ? SUCCESS : FAILED;
2591 * fas216_init_chip - Initialise FAS216 state after reset
2592 * @info: state structure for interface
2594 * Initialise FAS216 state after reset
2596 static void fas216_init_chip(FAS216_Info *info)
2598 unsigned int clock = ((info->ifcfg.clockrate - 1) / 5 + 1) & 7;
2599 fas216_writeb(info, REG_CLKF, clock);
2600 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2601 fas216_writeb(info, REG_CNTL2, info->scsi.cfg[1]);
2602 fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
2603 fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
2604 fas216_writeb(info, REG_SOF, 0);
2605 fas216_writeb(info, REG_STP, info->scsi.async_stp);
2606 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2610 * fas216_eh_host_reset - Reset the host associated with this command
2611 * @SCpnt: command specifing host to reset
2613 * Reset the host associated with this command.
2614 * Returns: FAILED if unable to reset.
2615 * Notes: io_request_lock is taken, and irqs are disabled
2617 int fas216_eh_host_reset(Scsi_Cmnd *SCpnt)
2619 FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
2621 fas216_checkmagic(info);
2623 printk("scsi%d.%c: %s: resetting host\n",
2624 info->host->host_no, '0' + SCpnt->target, __FUNCTION__);
2627 * Reset the SCSI chip.
2629 fas216_cmd(info, CMD_RESETCHIP);
2633 * We need to release the io_request_lock and enable
2634 * IRQs if we sleep, but we must relock and disable
2635 * IRQs after the sleep.
2637 spin_unlock_irq(&io_request_lock);
2638 scsi_sleep(50 * HZ/100);
2639 spin_lock_irq(&io_request_lock);
2642 * Release the SCSI reset.
2644 fas216_cmd(info, CMD_NOP);
2646 fas216_init_chip(info);
2651 #define TYPE_UNKNOWN 0
2652 #define TYPE_NCR53C90 1
2653 #define TYPE_NCR53C90A 2
2654 #define TYPE_NCR53C9x 3
2655 #define TYPE_Am53CF94 4
2656 #define TYPE_EmFAS216 5
2657 #define TYPE_QLFAS216 6
2659 static char *chip_types[] = {
2669 static int fas216_detect_type(FAS216_Info *info)
2676 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2678 fas216_writeb(info, REG_CMD, CMD_NOP);
2681 * Check to see if control reg 2 is present.
2683 fas216_writeb(info, REG_CNTL3, 0);
2684 fas216_writeb(info, REG_CNTL2, CNTL2_S2FE);
2687 * If we are unable to read back control reg 2
2688 * correctly, it is not present, and we have a
2691 if ((fas216_readb(info, REG_CNTL2) & (~0xe0)) != CNTL2_S2FE)
2692 return TYPE_NCR53C90;
2695 * Now, check control register 3
2697 fas216_writeb(info, REG_CNTL2, 0);
2698 fas216_writeb(info, REG_CNTL3, 0);
2699 fas216_writeb(info, REG_CNTL3, 5);
2702 * If we are unable to read the register back
2703 * correctly, we have a NCR53C90A
2705 if (fas216_readb(info, REG_CNTL3) != 5)
2706 return TYPE_NCR53C90A;
2709 * Now read the ID from the chip.
2711 fas216_writeb(info, REG_CNTL3, 0);
2713 fas216_writeb(info, REG_CNTL3, CNTL3_ADIDCHK);
2714 fas216_writeb(info, REG_CNTL3, 0);
2716 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2718 fas216_writeb(info, REG_CMD, CMD_WITHDMA | CMD_NOP);
2720 fas216_writeb(info, REG_CNTL2, CNTL2_ENF);
2721 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2723 fas216_writeb(info, REG_CMD, CMD_NOP);
2725 rev = fas216_readb(info, REG_ID);
2732 return TYPE_Am53CF94;
2738 return TYPE_EmFAS216;
2740 return TYPE_QLFAS216;
2747 printk("family %x rev %x\n", family, rev);
2748 return TYPE_NCR53C9x;
2752 * fas216_reset_state - Initialise driver internal state
2753 * @info: state to initialise
2755 * Initialise driver internal state
2757 static void fas216_reset_state(FAS216_Info *info)
2761 fas216_checkmagic(info);
2763 fas216_bus_reset(info);
2766 * Clear out all stale info in our state structure
2768 memset(info->busyluns, 0, sizeof(info->busyluns));
2769 info->scsi.disconnectable = 0;
2770 info->scsi.aborting = 0;
2772 for (i = 0; i < 8; i++) {
2773 info->device[i].parity_enabled = 0;
2774 info->device[i].parity_check = 1;
2778 * Drain all commands on disconnected queue
2780 while (queue_remove(&info->queues.disconnected) != NULL);
2783 * Remove executing commands.
2786 info->reqSCpnt = NULL;
2787 info->rstSCpnt = NULL;
2788 info->origSCpnt = NULL;
2792 * fas216_init - initialise FAS/NCR/AMD SCSI structures.
2793 * @host: a driver-specific filled-out structure
2795 * Initialise FAS/NCR/AMD SCSI structures.
2796 * Returns: 0 on success
2798 int fas216_init(struct Scsi_Host *host)
2800 FAS216_Info *info = (FAS216_Info *)host->hostdata;
2802 info->magic_start = MAGIC;
2803 info->magic_end = MAGIC;
2805 info->scsi.cfg[0] = host->this_id | CNTL1_PERE;
2806 info->scsi.cfg[1] = CNTL2_ENF | CNTL2_S2FE;
2807 info->scsi.cfg[2] = info->ifcfg.cntl3 |
2808 CNTL3_ADIDCHK | CNTL3_QTAG | CNTL3_G2CB | CNTL3_LBTM;
2809 info->scsi.async_stp = fas216_syncperiod(info, info->ifcfg.asyncperiod);
2811 info->rst_dev_status = -1;
2812 info->rst_bus_status = -1;
2813 init_waitqueue_head(&info->eh_wait);
2814 init_timer(&info->eh_timer);
2815 info->eh_timer.data = (unsigned long)info;
2816 info->eh_timer.function = fas216_eh_timer;
2818 spin_lock_init(&info->host_lock);
2820 memset(&info->stats, 0, sizeof(info->stats));
2822 msgqueue_initialise(&info->scsi.msgs);
2824 if (!queue_initialise(&info->queues.issue))
2827 if (!queue_initialise(&info->queues.disconnected)) {
2828 queue_free(&info->queues.issue);
2836 * fas216_add - initialise FAS/NCR/AMD SCSI ic.
2837 * @host: a driver-specific filled-out structure
2839 * Initialise FAS/NCR/AMD SCSI ic.
2840 * Returns: 0 on success
2842 int fas216_add(struct Scsi_Host *host)
2844 FAS216_Info *info = (FAS216_Info *)host->hostdata;
2847 if (info->ifcfg.clockrate <= 10 || info->ifcfg.clockrate > 40) {
2848 printk(KERN_CRIT "fas216: invalid clock rate %u MHz\n",
2849 info->ifcfg.clockrate);
2853 fas216_reset_state(info);
2854 type = fas216_detect_type(info);
2855 info->scsi.type = chip_types[type];
2860 * Initialise the chip correctly.
2862 fas216_init_chip(info);
2865 * Reset the SCSI bus. We don't want to see
2866 * the resulting reset interrupt, so mask it
2869 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_DISR);
2870 fas216_writeb(info, REG_CMD, CMD_RESETSCSI);
2873 * scsi standard says wait 250ms
2875 spin_unlock_irq(&io_request_lock);
2876 scsi_sleep(100*HZ/100);
2877 spin_lock_irq(&io_request_lock);
2879 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2880 fas216_readb(info, REG_INST);
2882 fas216_checkmagic(info);
2887 void fas216_remove(struct Scsi_Host *host)
2889 FAS216_Info *info = (FAS216_Info *)host->hostdata;
2891 fas216_checkmagic(info);
2893 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2897 * fas216_release - release all resources for FAS/NCR/AMD SCSI ic.
2898 * @host: a driver-specific filled-out structure
2900 * release all resources and put everything to bed for FAS/NCR/AMD SCSI ic.
2902 void fas216_release(struct Scsi_Host *host)
2904 FAS216_Info *info = (FAS216_Info *)host->hostdata;
2906 queue_free(&info->queues.disconnected);
2907 queue_free(&info->queues.issue);
2910 int fas216_print_host(FAS216_Info *info, char *buffer)
2912 return sprintf(buffer,
2915 " Address: 0x%08lx\n"
2918 info->scsi.type, info->host->io_port,
2919 info->host->irq, info->host->dma_channel);
2922 int fas216_print_stats(FAS216_Info *info, char *buffer)
2926 p += sprintf(p, "\n"
2927 "Command Statistics:\n"
2934 " Disconnects: %u\n"
2936 " Bus resets : %u\n"
2937 " Host resets: %u\n",
2938 info->stats.queues, info->stats.removes,
2939 info->stats.fins, info->stats.reads,
2940 info->stats.writes, info->stats.miscs,
2941 info->stats.disconnects, info->stats.aborts,
2942 info->stats.bus_resets, info->stats.host_resets);
2947 int fas216_print_device(FAS216_Info *info, Scsi_Device *scd, char *buffer)
2949 struct fas216_device *dev = &info->device[scd->id];
2953 proc_print_scsidevice(scd, buffer, &len, 0);
2956 p += sprintf(p, " Extensions: ");
2958 if (scd->tagged_supported)
2959 p += sprintf(p, "TAG %sabled [%d] ",
2960 scd->tagged_queue ? "en" : "dis",
2963 p += sprintf(p, "%s\n", dev->parity_enabled ? "parity" : "");
2965 p += sprintf(p, " Transfers : %d-bit ",
2966 8 << dev->wide_xfer);
2969 p += sprintf(p, "sync offset %d, %d ns\n",
2970 dev->sof, dev->period * 4);
2972 p += sprintf(p, "async\n");
2977 EXPORT_SYMBOL(fas216_init);
2978 EXPORT_SYMBOL(fas216_add);
2979 EXPORT_SYMBOL(fas216_queue_command);
2980 EXPORT_SYMBOL(fas216_command);
2981 EXPORT_SYMBOL(fas216_intr);
2982 EXPORT_SYMBOL(fas216_remove);
2983 EXPORT_SYMBOL(fas216_release);
2984 EXPORT_SYMBOL(fas216_eh_abort);
2985 EXPORT_SYMBOL(fas216_eh_device_reset);
2986 EXPORT_SYMBOL(fas216_eh_bus_reset);
2987 EXPORT_SYMBOL(fas216_eh_host_reset);
2988 EXPORT_SYMBOL(fas216_print_host);
2989 EXPORT_SYMBOL(fas216_print_stats);
2990 EXPORT_SYMBOL(fas216_print_device);
2992 MODULE_AUTHOR("Russell King");
2993 MODULE_DESCRIPTION("Generic FAS216/NCR53C9x driver core");
2994 MODULE_LICENSE("GPL");