1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/acorn/scsi/fas216.c
5 * Copyright (C) 1997-2003 Russell King
7 * Based on information in qlogicfas.c by Tom Zerucha, Michael Griffith, and
8 * other sources, including:
9 * the AMD Am53CF94 data sheet
10 * the AMD Am53C94 data sheet
12 * This is a generic driver. To use it, have a look at cumana_2.c. You
13 * should define your own structure that overlays FAS216_Info, eg:
14 * struct my_host_data {
16 * ... my host specific data ...
20 * 30-08-1997 RMK Created
21 * 14-09-1997 RMK Started disconnect support
22 * 08-02-1998 RMK Corrected real DMA support
23 * 15-02-1998 RMK Started sync xfer support
24 * 06-04-1998 RMK Tightened conditions for printing incomplete
26 * 02-05-1998 RMK Added extra checks in fas216_reset
27 * 24-05-1998 RMK Fixed synchronous transfers with period >= 200ns
28 * 27-06-1998 RMK Changed asm/delay.h to linux/delay.h
29 * 26-08-1998 RMK Improved message support wrt MESSAGE_REJECT
30 * 02-04-2000 RMK Converted to use the new error handling, and
31 * automatically request sense data upon check
32 * condition status from targets.
34 #include <linux/module.h>
35 #include <linux/blkdev.h>
36 #include <linux/kernel.h>
37 #include <linux/string.h>
38 #include <linux/ioport.h>
39 #include <linux/proc_fs.h>
40 #include <linux/delay.h>
41 #include <linux/bitops.h>
42 #include <linux/init.h>
43 #include <linux/interrupt.h>
48 #include <asm/ecard.h>
51 #include <scsi/scsi_dbg.h>
52 #include <scsi/scsi_host.h>
56 /* NOTE: SCSI2 Synchronous transfers *require* DMA according to
57 * the data sheet. This restriction is crazy, especially when
58 * you only want to send 16 bytes! What were the guys who
59 * designed this chip on at that time? Did they read the SCSI2
60 * spec at all? The following sections are taken from the SCSI2
61 * standard (s2r10) concerning this:
63 * > IMPLEMENTORS NOTES:
64 * > (1) Re-negotiation at every selection is not recommended, since a
65 * > significant performance impact is likely.
67 * > The implied synchronous agreement shall remain in effect until a BUS DEVICE
68 * > RESET message is received, until a hard reset condition occurs, or until one
69 * > of the two SCSI devices elects to modify the agreement. The default data
70 * > transfer mode is asynchronous data transfer mode. The default data transfer
71 * > mode is entered at power on, after a BUS DEVICE RESET message, or after a hard
74 * In total, this means that once you have elected to use synchronous
75 * transfers, you must always use DMA.
77 * I was thinking that this was a good chip until I found this restriction ;(
85 #undef CHECK_STRUCTURE
87 #define LOG_CONNECT (1 << 0)
88 #define LOG_BUSSERVICE (1 << 1)
89 #define LOG_FUNCTIONDONE (1 << 2)
90 #define LOG_MESSAGES (1 << 3)
91 #define LOG_BUFFER (1 << 4)
92 #define LOG_ERROR (1 << 8)
94 static int level_mask = LOG_ERROR;
96 module_param(level_mask, int, 0644);
99 static int __init fas216_log_setup(char *str)
105 while ((s = strsep(&str, ",")) != NULL) {
108 if (strcmp(s, "all") == 0)
112 if (strncmp(s, "bus", 3) == 0)
113 level_mask |= LOG_BUSSERVICE;
114 if (strncmp(s, "buf", 3) == 0)
115 level_mask |= LOG_BUFFER;
118 level_mask |= LOG_CONNECT;
121 level_mask |= LOG_ERROR;
124 level_mask |= LOG_MESSAGES;
127 if (strcmp(s, "none") == 0)
131 level_mask |= LOG_FUNCTIONDONE;
138 __setup("fas216_logging=", fas216_log_setup);
141 static inline unsigned char fas216_readb(FAS216_Info *info, unsigned int reg)
143 unsigned int off = reg << info->scsi.io_shift;
144 return readb(info->scsi.io_base + off);
147 static inline void fas216_writeb(FAS216_Info *info, unsigned int reg, unsigned int val)
149 unsigned int off = reg << info->scsi.io_shift;
150 writeb(val, info->scsi.io_base + off);
153 static void fas216_dumpstate(FAS216_Info *info)
155 unsigned char is, stat, inst;
157 is = fas216_readb(info, REG_IS);
158 stat = fas216_readb(info, REG_STAT);
159 inst = fas216_readb(info, REG_INST);
161 printk("FAS216: CTCL=%02X CTCM=%02X CMD=%02X STAT=%02X"
162 " INST=%02X IS=%02X CFIS=%02X",
163 fas216_readb(info, REG_CTCL),
164 fas216_readb(info, REG_CTCM),
165 fas216_readb(info, REG_CMD), stat, inst, is,
166 fas216_readb(info, REG_CFIS));
167 printk(" CNTL1=%02X CNTL2=%02X CNTL3=%02X CTCH=%02X\n",
168 fas216_readb(info, REG_CNTL1),
169 fas216_readb(info, REG_CNTL2),
170 fas216_readb(info, REG_CNTL3),
171 fas216_readb(info, REG_CTCH));
174 static void print_SCp(struct scsi_pointer *SCp, const char *prefix, const char *suffix)
176 printk("%sptr %p this_residual 0x%x buffer %p buffers_residual 0x%x%s",
177 prefix, SCp->ptr, SCp->this_residual, SCp->buffer,
178 SCp->buffers_residual, suffix);
181 #ifdef CHECK_STRUCTURE
182 static void fas216_dumpinfo(FAS216_Info *info)
190 printk("FAS216_Info=\n");
191 printk(" { magic_start=%lX host=%p SCpnt=%p origSCpnt=%p\n",
192 info->magic_start, info->host, info->SCpnt,
194 printk(" scsi={ io_shift=%X irq=%X cfg={ %X %X %X %X }\n",
195 info->scsi.io_shift, info->scsi.irq,
196 info->scsi.cfg[0], info->scsi.cfg[1], info->scsi.cfg[2],
198 printk(" type=%p phase=%X\n",
199 info->scsi.type, info->scsi.phase);
200 print_SCp(&info->scsi.SCp, " SCp={ ", " }\n");
201 printk(" msgs async_stp=%X disconnectable=%d aborting=%d }\n",
202 info->scsi.async_stp,
203 info->scsi.disconnectable, info->scsi.aborting);
204 printk(" stats={ queues=%X removes=%X fins=%X reads=%X writes=%X miscs=%X\n"
205 " disconnects=%X aborts=%X bus_resets=%X host_resets=%X}\n",
206 info->stats.queues, info->stats.removes, info->stats.fins,
207 info->stats.reads, info->stats.writes, info->stats.miscs,
208 info->stats.disconnects, info->stats.aborts, info->stats.bus_resets,
209 info->stats.host_resets);
210 printk(" ifcfg={ clockrate=%X select_timeout=%X asyncperiod=%X sync_max_depth=%X }\n",
211 info->ifcfg.clockrate, info->ifcfg.select_timeout,
212 info->ifcfg.asyncperiod, info->ifcfg.sync_max_depth);
213 for (i = 0; i < 8; i++) {
214 printk(" busyluns[%d]=%08lx dev[%d]={ disconnect_ok=%d stp=%X sof=%X sync_state=%X }\n",
215 i, info->busyluns[i], i,
216 info->device[i].disconnect_ok, info->device[i].stp,
217 info->device[i].sof, info->device[i].sync_state);
219 printk(" dma={ transfer_type=%X setup=%p pseudo=%p stop=%p }\n",
220 info->dma.transfer_type, info->dma.setup,
221 info->dma.pseudo, info->dma.stop);
222 printk(" internal_done=%X magic_end=%lX }\n",
223 info->internal_done, info->magic_end);
226 static void __fas216_checkmagic(FAS216_Info *info, const char *func)
229 if (info->magic_start != MAGIC) {
230 printk(KERN_CRIT "FAS216 Error: magic at start corrupted\n");
233 if (info->magic_end != MAGIC) {
234 printk(KERN_CRIT "FAS216 Error: magic at end corrupted\n");
238 fas216_dumpinfo(info);
239 panic("scsi memory space corrupted in %s", func);
242 #define fas216_checkmagic(info) __fas216_checkmagic((info), __func__)
244 #define fas216_checkmagic(info)
247 static const char *fas216_bus_phase(int stat)
249 static const char *phases[] = {
250 "DATA OUT", "DATA IN",
252 "MISC OUT", "MISC IN",
253 "MESG OUT", "MESG IN"
256 return phases[stat & STAT_BUSMASK];
259 static const char *fas216_drv_phase(FAS216_Info *info)
261 static const char *phases[] = {
262 [PHASE_IDLE] = "idle",
263 [PHASE_SELECTION] = "selection",
264 [PHASE_COMMAND] = "command",
265 [PHASE_DATAOUT] = "data out",
266 [PHASE_DATAIN] = "data in",
267 [PHASE_MSGIN] = "message in",
268 [PHASE_MSGIN_DISCONNECT]= "disconnect",
269 [PHASE_MSGOUT_EXPECT] = "expect message out",
270 [PHASE_MSGOUT] = "message out",
271 [PHASE_STATUS] = "status",
272 [PHASE_DONE] = "done",
275 if (info->scsi.phase < ARRAY_SIZE(phases) &&
276 phases[info->scsi.phase])
277 return phases[info->scsi.phase];
281 static char fas216_target(FAS216_Info *info)
284 return '0' + info->SCpnt->device->id;
290 fas216_do_log(FAS216_Info *info, char target, char *fmt, va_list ap)
292 static char buf[1024];
294 vsnprintf(buf, sizeof(buf), fmt, ap);
295 printk("scsi%d.%c: %s", info->host->host_no, target, buf);
298 static void fas216_log_command(FAS216_Info *info, int level,
299 struct scsi_cmnd *SCpnt, char *fmt, ...)
303 if (level != 0 && !(level & level_mask))
307 fas216_do_log(info, '0' + SCpnt->device->id, fmt, args);
310 scsi_print_command(SCpnt);
314 fas216_log_target(FAS216_Info *info, int level, int target, char *fmt, ...)
318 if (level != 0 && !(level & level_mask))
327 fas216_do_log(info, target, fmt, args);
333 static void fas216_log(FAS216_Info *info, int level, char *fmt, ...)
337 if (level != 0 && !(level & level_mask))
341 fas216_do_log(info, fas216_target(info), fmt, args);
349 static struct { int stat, ssr, isr, ph; } ph_list[PH_SIZE];
352 static void add_debug_list(int stat, int ssr, int isr, int ph)
354 ph_list[ph_ptr].stat = stat;
355 ph_list[ph_ptr].ssr = ssr;
356 ph_list[ph_ptr].isr = isr;
357 ph_list[ph_ptr].ph = ph;
359 ph_ptr = (ph_ptr + 1) & (PH_SIZE-1);
362 static struct { int command; void *from; } cmd_list[8];
365 static void fas216_cmd(FAS216_Info *info, unsigned int command)
367 cmd_list[cmd_ptr].command = command;
368 cmd_list[cmd_ptr].from = __builtin_return_address(0);
370 cmd_ptr = (cmd_ptr + 1) & 7;
372 fas216_writeb(info, REG_CMD, command);
375 static void print_debug_list(void)
381 printk(KERN_ERR "SCSI IRQ trail\n");
383 printk(" %02x:%02x:%02x:%1x",
384 ph_list[i].stat, ph_list[i].ssr,
385 ph_list[i].isr, ph_list[i].ph);
386 i = (i + 1) & (PH_SIZE - 1);
387 if (((i ^ ph_ptr) & 7) == 0)
389 } while (i != ph_ptr);
390 if ((i ^ ph_ptr) & 7)
394 printk(KERN_ERR "FAS216 commands: ");
396 printk("%02x:%p ", cmd_list[i].command, cmd_list[i].from);
398 } while (i != cmd_ptr);
402 static void fas216_done(FAS216_Info *info, unsigned int result);
405 * fas216_get_last_msg - retrive last message from the list
406 * @info: interface to search
407 * @pos: current fifo position
409 * Retrieve a last message from the list, using position in fifo.
411 static inline unsigned short
412 fas216_get_last_msg(FAS216_Info *info, int pos)
414 unsigned short packed_msg = NOP;
418 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
419 if (pos >= msg->fifo)
424 if (msg->msg[0] == EXTENDED_MESSAGE)
425 packed_msg = EXTENDED_MESSAGE | msg->msg[2] << 8;
427 packed_msg = msg->msg[0];
430 fas216_log(info, LOG_MESSAGES,
431 "Message: %04x found at position %02x\n", packed_msg, pos);
437 * fas216_syncperiod - calculate STP register value
438 * @info: state structure for interface connected to device
439 * @ns: period in ns (between subsequent bytes)
441 * Calculate value to be loaded into the STP register for a given period
442 * in ns. Returns a value suitable for REG_STP.
444 static int fas216_syncperiod(FAS216_Info *info, int ns)
446 int value = (info->ifcfg.clockrate * ns) / 1000;
448 fas216_checkmagic(info);
459 * fas216_set_sync - setup FAS216 chip for specified transfer period.
460 * @info: state structure for interface connected to device
463 * Correctly setup FAS216 chip for specified transfer period.
464 * Notes : we need to switch the chip out of FASTSCSI mode if we have
465 * a transfer period >= 200ns - otherwise the chip will violate
468 static void fas216_set_sync(FAS216_Info *info, int target)
472 fas216_writeb(info, REG_SOF, info->device[target].sof);
473 fas216_writeb(info, REG_STP, info->device[target].stp);
475 cntl3 = info->scsi.cfg[2];
476 if (info->device[target].period >= (200 / 4))
477 cntl3 = cntl3 & ~CNTL3_FASTSCSI;
479 fas216_writeb(info, REG_CNTL3, cntl3);
482 /* Synchronous transfer support
484 * Note: The SCSI II r10 spec says (5.6.12):
486 * (2) Due to historical problems with early host adapters that could
487 * not accept an SDTR message, some targets may not initiate synchronous
488 * negotiation after a power cycle as required by this standard. Host
489 * adapters that support synchronous mode may avoid the ensuing failure
490 * modes when the target is independently power cycled by initiating a
491 * synchronous negotiation on each REQUEST SENSE and INQUIRY command.
492 * This approach increases the SCSI bus overhead and is not recommended
493 * for new implementations. The correct method is to respond to an
494 * SDTR message with a MESSAGE REJECT message if the either the
495 * initiator or target devices does not support synchronous transfers
496 * or does not want to negotiate for synchronous transfers at the time.
497 * Using the correct method assures compatibility with wide data
498 * transfers and future enhancements.
500 * We will always initiate a synchronous transfer negotiation request on
501 * every INQUIRY or REQUEST SENSE message, unless the target itself has
502 * at some point performed a synchronous transfer negotiation request, or
503 * we have synchronous transfers disabled for this device.
507 * fas216_handlesync - Handle a synchronous transfer message
508 * @info: state structure for interface
509 * @msg: message from target
511 * Handle a synchronous transfer message from the target
513 static void fas216_handlesync(FAS216_Info *info, char *msg)
515 struct fas216_device *dev = &info->device[info->SCpnt->device->id];
516 enum { sync, async, none, reject } res = none;
521 /* Synchronous transfer request failed.
524 * SCSI devices that are capable of synchronous
525 * data transfers shall not respond to an SDTR
526 * message with a MESSAGE REJECT message.
528 * Hence, if we get this condition, we disable
529 * negotiation for this device.
531 if (dev->sync_state == neg_inprogress) {
532 dev->sync_state = neg_invalid;
537 case EXTENDED_MESSAGE:
538 switch (dev->sync_state) {
539 /* We don't accept synchronous transfer requests.
540 * Respond with a MESSAGE_REJECT to prevent a
541 * synchronous transfer agreement from being reached.
547 /* We were not negotiating a synchronous transfer,
548 * but the device sent us a negotiation request.
549 * Honour the request by sending back a SDTR
550 * message containing our capability, limited by
551 * the targets capability.
554 fas216_cmd(info, CMD_SETATN);
555 if (msg[4] > info->ifcfg.sync_max_depth)
556 msg[4] = info->ifcfg.sync_max_depth;
557 if (msg[3] < 1000 / info->ifcfg.clockrate)
558 msg[3] = 1000 / info->ifcfg.clockrate;
560 msgqueue_flush(&info->scsi.msgs);
561 msgqueue_addmsg(&info->scsi.msgs, 5,
562 EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
564 info->scsi.phase = PHASE_MSGOUT_EXPECT;
566 /* This is wrong. The agreement is not in effect
567 * until this message is accepted by the device
569 dev->sync_state = neg_targcomplete;
573 /* We initiated the synchronous transfer negotiation,
574 * and have successfully received a response from the
575 * target. The synchronous transfer agreement has been
576 * reached. Note: if the values returned are out of our
577 * bounds, we must reject the message.
581 if (msg[4] <= info->ifcfg.sync_max_depth &&
582 msg[3] >= 1000 / info->ifcfg.clockrate) {
583 dev->sync_state = neg_complete;
595 dev->period = msg[3];
597 dev->stp = fas216_syncperiod(info, msg[3] * 4);
598 fas216_set_sync(info, info->SCpnt->device->id);
602 fas216_cmd(info, CMD_SETATN);
603 msgqueue_flush(&info->scsi.msgs);
604 msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
605 info->scsi.phase = PHASE_MSGOUT_EXPECT;
609 dev->period = info->ifcfg.asyncperiod / 4;
611 dev->stp = info->scsi.async_stp;
612 fas216_set_sync(info, info->SCpnt->device->id);
621 * fas216_updateptrs - update data pointers after transfer suspended/paused
622 * @info: interface's local pointer to update
623 * @bytes_transferred: number of bytes transferred
625 * Update data pointers after transfer suspended/paused
627 static void fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
629 struct scsi_pointer *SCp = &info->scsi.SCp;
631 fas216_checkmagic(info);
633 BUG_ON(bytes_transferred < 0);
635 SCp->phase -= bytes_transferred;
637 while (bytes_transferred != 0) {
638 if (SCp->this_residual > bytes_transferred)
641 * We have used up this buffer. Move on to the
644 bytes_transferred -= SCp->this_residual;
645 if (!next_SCp(SCp) && bytes_transferred) {
646 printk(KERN_WARNING "scsi%d.%c: out of buffers\n",
647 info->host->host_no, '0' + info->SCpnt->device->id);
652 SCp->this_residual -= bytes_transferred;
653 if (SCp->this_residual)
654 SCp->ptr += bytes_transferred;
660 * fas216_pio - transfer data off of/on to card using programmed IO
661 * @info: interface to transfer data to/from
662 * @direction: direction to transfer data (DMA_OUT/DMA_IN)
664 * Transfer data off of/on to card using programmed IO.
665 * Notes: this is incredibly slow.
667 static void fas216_pio(FAS216_Info *info, fasdmadir_t direction)
669 struct scsi_pointer *SCp = &info->scsi.SCp;
671 fas216_checkmagic(info);
673 if (direction == DMA_OUT)
674 fas216_writeb(info, REG_FF, get_next_SCp_byte(SCp));
676 put_next_SCp_byte(SCp, fas216_readb(info, REG_FF));
678 if (SCp->this_residual == 0)
682 static void fas216_set_stc(FAS216_Info *info, unsigned int length)
684 fas216_writeb(info, REG_STCL, length);
685 fas216_writeb(info, REG_STCM, length >> 8);
686 fas216_writeb(info, REG_STCH, length >> 16);
689 static unsigned int fas216_get_ctc(FAS216_Info *info)
691 return fas216_readb(info, REG_CTCL) +
692 (fas216_readb(info, REG_CTCM) << 8) +
693 (fas216_readb(info, REG_CTCH) << 16);
697 * fas216_cleanuptransfer - clean up after a transfer has completed.
698 * @info: interface to clean up
700 * Update the data pointers according to the number of bytes transferred
703 static void fas216_cleanuptransfer(FAS216_Info *info)
705 unsigned long total, residual, fifo;
706 fasdmatype_t dmatype = info->dma.transfer_type;
708 info->dma.transfer_type = fasdma_none;
711 * PIO transfers do not need to be cleaned up.
713 if (dmatype == fasdma_pio || dmatype == fasdma_none)
716 if (dmatype == fasdma_real_all)
717 total = info->scsi.SCp.phase;
719 total = info->scsi.SCp.this_residual;
721 residual = fas216_get_ctc(info);
723 fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
725 fas216_log(info, LOG_BUFFER, "cleaning up from previous "
726 "transfer: length 0x%06x, residual 0x%x, fifo %d",
727 total, residual, fifo);
730 * If we were performing Data-Out, the transfer counter
731 * counts down each time a byte is transferred by the
732 * host to the FIFO. This means we must include the
733 * bytes left in the FIFO from the transfer counter.
735 if (info->scsi.phase == PHASE_DATAOUT)
738 fas216_updateptrs(info, total - residual);
742 * fas216_transfer - Perform a DMA/PIO transfer off of/on to card
743 * @info: interface from which device disconnected from
745 * Start a DMA/PIO transfer off of/on to card
747 static void fas216_transfer(FAS216_Info *info)
749 fasdmadir_t direction;
750 fasdmatype_t dmatype;
752 fas216_log(info, LOG_BUFFER,
753 "starttransfer: buffer %p length 0x%06x reqlen 0x%06x",
754 info->scsi.SCp.ptr, info->scsi.SCp.this_residual,
755 info->scsi.SCp.phase);
757 if (!info->scsi.SCp.ptr) {
758 fas216_log(info, LOG_ERROR, "null buffer passed to "
759 "fas216_starttransfer");
760 print_SCp(&info->scsi.SCp, "SCp: ", "\n");
761 print_SCp(&info->SCpnt->SCp, "Cmnd SCp: ", "\n");
766 * If we have a synchronous transfer agreement in effect, we must
767 * use DMA mode. If we are using asynchronous transfers, we may
768 * use DMA mode or PIO mode.
770 if (info->device[info->SCpnt->device->id].sof)
771 dmatype = fasdma_real_all;
773 dmatype = fasdma_pio;
775 if (info->scsi.phase == PHASE_DATAOUT)
781 dmatype = info->dma.setup(info->host, &info->scsi.SCp,
783 info->dma.transfer_type = dmatype;
785 if (dmatype == fasdma_real_all)
786 fas216_set_stc(info, info->scsi.SCp.phase);
788 fas216_set_stc(info, info->scsi.SCp.this_residual);
792 fas216_log(info, LOG_BUFFER, "PIO transfer");
793 fas216_writeb(info, REG_SOF, 0);
794 fas216_writeb(info, REG_STP, info->scsi.async_stp);
795 fas216_cmd(info, CMD_TRANSFERINFO);
796 fas216_pio(info, direction);
800 fas216_log(info, LOG_BUFFER, "pseudo transfer");
801 fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
802 info->dma.pseudo(info->host, &info->scsi.SCp,
803 direction, info->SCpnt->transfersize);
806 case fasdma_real_block:
807 fas216_log(info, LOG_BUFFER, "block dma transfer");
808 fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
811 case fasdma_real_all:
812 fas216_log(info, LOG_BUFFER, "total dma transfer");
813 fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
817 fas216_log(info, LOG_BUFFER | LOG_ERROR,
818 "invalid FAS216 DMA type");
824 * fas216_stoptransfer - Stop a DMA transfer onto / off of the card
825 * @info: interface from which device disconnected from
827 * Called when we switch away from DATA IN or DATA OUT phases.
829 static void fas216_stoptransfer(FAS216_Info *info)
831 fas216_checkmagic(info);
833 if (info->dma.transfer_type == fasdma_real_all ||
834 info->dma.transfer_type == fasdma_real_block)
835 info->dma.stop(info->host, &info->scsi.SCp);
837 fas216_cleanuptransfer(info);
839 if (info->scsi.phase == PHASE_DATAIN) {
843 * If we were performing Data-In, then the FIFO counter
844 * contains the number of bytes not transferred via DMA
845 * from the on-board FIFO. Read them manually.
847 fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
848 while (fifo && info->scsi.SCp.ptr) {
849 *info->scsi.SCp.ptr = fas216_readb(info, REG_FF);
850 fas216_updateptrs(info, 1);
855 * After a Data-Out phase, there may be unsent
856 * bytes left in the FIFO. Flush them out.
858 fas216_cmd(info, CMD_FLUSHFIFO);
862 static void fas216_aborttransfer(FAS216_Info *info)
864 fas216_checkmagic(info);
866 if (info->dma.transfer_type == fasdma_real_all ||
867 info->dma.transfer_type == fasdma_real_block)
868 info->dma.stop(info->host, &info->scsi.SCp);
870 info->dma.transfer_type = fasdma_none;
871 fas216_cmd(info, CMD_FLUSHFIFO);
874 static void fas216_kick(FAS216_Info *info);
877 * fas216_disconnected_intr - handle device disconnection
878 * @info: interface from which device disconnected from
880 * Handle device disconnection
882 static void fas216_disconnect_intr(FAS216_Info *info)
886 fas216_checkmagic(info);
888 fas216_log(info, LOG_CONNECT, "disconnect phase=%02x",
891 msgqueue_flush(&info->scsi.msgs);
893 switch (info->scsi.phase) {
894 case PHASE_SELECTION: /* while selecting - no target */
896 fas216_done(info, DID_NO_CONNECT);
899 case PHASE_MSGIN_DISCONNECT: /* message in - disconnecting */
900 info->scsi.disconnectable = 1;
901 info->scsi.phase = PHASE_IDLE;
902 info->stats.disconnects += 1;
903 spin_lock_irqsave(&info->host_lock, flags);
904 if (info->scsi.phase == PHASE_IDLE)
906 spin_unlock_irqrestore(&info->host_lock, flags);
909 case PHASE_DONE: /* at end of command - complete */
910 fas216_done(info, DID_OK);
913 case PHASE_MSGOUT: /* message out - possible ABORT message */
914 if (fas216_get_last_msg(info, info->scsi.msgin_fifo) == ABORT) {
915 info->scsi.aborting = 0;
916 fas216_done(info, DID_ABORT);
922 printk(KERN_ERR "scsi%d.%c: unexpected disconnect in phase %s\n",
923 info->host->host_no, fas216_target(info), fas216_drv_phase(info));
925 fas216_stoptransfer(info);
926 fas216_done(info, DID_ERROR);
932 * fas216_reselected_intr - start reconnection of a device
933 * @info: interface which was reselected
935 * Start reconnection of a device
938 fas216_reselected_intr(FAS216_Info *info)
940 unsigned int cfis, i;
941 unsigned char msg[4];
942 unsigned char target, lun, tag;
944 fas216_checkmagic(info);
946 WARN_ON(info->scsi.phase == PHASE_SELECTION ||
947 info->scsi.phase == PHASE_SELSTEPS);
949 cfis = fas216_readb(info, REG_CFIS);
951 fas216_log(info, LOG_CONNECT, "reconnect phase=%02x cfis=%02x",
952 info->scsi.phase, cfis);
956 if (cfis < 2 || cfis > 4) {
957 printk(KERN_ERR "scsi%d.H: incorrect number of bytes after reselect\n",
958 info->host->host_no);
962 for (i = 0; i < cfis; i++)
963 msg[i] = fas216_readb(info, REG_FF);
965 if (!(msg[0] & (1 << info->host->this_id)) ||
967 goto initiator_error;
969 target = msg[0] & ~(1 << info->host->this_id);
970 target = ffs(target) - 1;
975 if (msg[2] != SIMPLE_QUEUE_TAG)
976 goto initiator_error;
981 /* set up for synchronous transfers */
982 fas216_writeb(info, REG_SDID, target);
983 fas216_set_sync(info, target);
984 msgqueue_flush(&info->scsi.msgs);
986 fas216_log(info, LOG_CONNECT, "Reconnected: target %1x lun %1x tag %02x",
989 if (info->scsi.disconnectable && info->SCpnt) {
990 info->scsi.disconnectable = 0;
991 if (info->SCpnt->device->id == target &&
992 info->SCpnt->device->lun == lun &&
993 info->SCpnt->tag == tag) {
994 fas216_log(info, LOG_CONNECT, "reconnected previously executing command");
996 queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
997 fas216_log(info, LOG_CONNECT, "had to move command to disconnected queue");
1002 info->SCpnt = queue_remove_tgtluntag(&info->queues.disconnected,
1004 fas216_log(info, LOG_CONNECT, "had to get command");
1009 * Restore data pointer from SAVED data pointer
1011 info->scsi.SCp = info->SCpnt->SCp;
1013 fas216_log(info, LOG_CONNECT, "data pointers: [%p, %X]",
1014 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1015 info->scsi.phase = PHASE_MSGIN;
1018 * Our command structure not found - abort the
1019 * command on the target. Since we have no
1020 * record of this command, we can't send
1021 * an INITIATOR DETECTED ERROR message.
1023 fas216_cmd(info, CMD_SETATN);
1027 msgqueue_addmsg(&info->scsi.msgs, 2, ABORT_TAG, tag);
1030 msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
1031 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1032 info->scsi.aborting = 1;
1035 fas216_cmd(info, CMD_MSGACCEPTED);
1039 printk(KERN_ERR "scsi%d.H: error during reselection: bytes",
1040 info->host->host_no);
1041 for (i = 0; i < cfis; i++)
1042 printk(" %02x", msg[i]);
1045 fas216_cmd(info, CMD_SETATN);
1046 msgqueue_flush(&info->scsi.msgs);
1047 msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1048 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1049 fas216_cmd(info, CMD_MSGACCEPTED);
1052 static void fas216_parse_message(FAS216_Info *info, unsigned char *message, int msglen)
1056 switch (message[0]) {
1057 case COMMAND_COMPLETE:
1061 printk(KERN_ERR "scsi%d.%c: command complete with no "
1062 "status in MESSAGE_IN?\n",
1063 info->host->host_no, fas216_target(info));
1071 * Save current data pointer to SAVED data pointer
1072 * SCSI II standard says that we must not acknowledge
1073 * this until we have really saved pointers.
1074 * NOTE: we DO NOT save the command nor status pointers
1075 * as required by the SCSI II standard. These always
1076 * point to the start of their respective areas.
1078 info->SCpnt->SCp = info->scsi.SCp;
1079 info->SCpnt->SCp.sent_command = 0;
1080 fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
1081 "save data pointers: [%p, %X]",
1082 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1085 case RESTORE_POINTERS:
1090 * Restore current data pointer from SAVED data pointer
1092 info->scsi.SCp = info->SCpnt->SCp;
1093 fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
1094 "restore data pointers: [%p, 0x%x]",
1095 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1102 info->scsi.phase = PHASE_MSGIN_DISCONNECT;
1105 case MESSAGE_REJECT:
1109 switch (fas216_get_last_msg(info, info->scsi.msgin_fifo)) {
1110 case EXTENDED_MESSAGE | EXTENDED_SDTR << 8:
1111 fas216_handlesync(info, message);
1115 fas216_log(info, 0, "reject, last message 0x%04x",
1116 fas216_get_last_msg(info, info->scsi.msgin_fifo));
1123 case EXTENDED_MESSAGE:
1127 switch (message[2]) {
1128 case EXTENDED_SDTR: /* Sync transfer negotiation request/reply */
1129 fas216_handlesync(info, message);
1143 fas216_log(info, 0, "unrecognised message, rejecting");
1144 printk("scsi%d.%c: message was", info->host->host_no, fas216_target(info));
1145 for (i = 0; i < msglen; i++)
1146 printk("%s%02X", i & 31 ? " " : "\n ", message[i]);
1150 * Something strange seems to be happening here -
1151 * I can't use SETATN since the chip gives me an
1152 * invalid command interrupt when I do. Weird.
1154 fas216_cmd(info, CMD_NOP);
1155 fas216_dumpstate(info);
1156 fas216_cmd(info, CMD_SETATN);
1157 msgqueue_flush(&info->scsi.msgs);
1158 msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
1159 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1160 fas216_dumpstate(info);
1163 static int fas216_wait_cmd(FAS216_Info *info, int cmd)
1168 fas216_cmd(info, cmd);
1170 for (tout = 1000; tout; tout -= 1) {
1171 stat = fas216_readb(info, REG_STAT);
1172 if (stat & (STAT_INT|STAT_PARITYERROR))
1180 static int fas216_get_msg_byte(FAS216_Info *info)
1182 unsigned int stat = fas216_wait_cmd(info, CMD_MSGACCEPTED);
1184 if ((stat & STAT_INT) == 0)
1187 if ((stat & STAT_BUSMASK) != STAT_MESGIN)
1188 goto unexpected_phase_change;
1190 fas216_readb(info, REG_INST);
1192 stat = fas216_wait_cmd(info, CMD_TRANSFERINFO);
1194 if ((stat & STAT_INT) == 0)
1197 if (stat & STAT_PARITYERROR)
1200 if ((stat & STAT_BUSMASK) != STAT_MESGIN)
1201 goto unexpected_phase_change;
1203 fas216_readb(info, REG_INST);
1205 return fas216_readb(info, REG_FF);
1208 fas216_log(info, LOG_ERROR, "timed out waiting for message byte");
1211 unexpected_phase_change:
1212 fas216_log(info, LOG_ERROR, "unexpected phase change: status = %02x", stat);
1216 fas216_log(info, LOG_ERROR, "parity error during message in phase");
1221 * fas216_message - handle a function done interrupt from FAS216 chip
1222 * @info: interface which caused function done interrupt
1224 * Handle a function done interrupt from FAS216 chip
1226 static void fas216_message(FAS216_Info *info)
1228 unsigned char *message = info->scsi.message;
1229 unsigned int msglen = 1;
1232 fas216_checkmagic(info);
1234 message[0] = fas216_readb(info, REG_FF);
1236 if (message[0] == EXTENDED_MESSAGE) {
1237 msgbyte = fas216_get_msg_byte(info);
1240 message[1] = msgbyte;
1242 for (msglen = 2; msglen < message[1] + 2; msglen++) {
1243 msgbyte = fas216_get_msg_byte(info);
1246 message[msglen] = msgbyte;
1256 #ifdef DEBUG_MESSAGES
1260 printk("scsi%d.%c: message in: ",
1261 info->host->host_no, fas216_target(info));
1262 for (i = 0; i < msglen; i++)
1263 printk("%02X ", message[i]);
1268 fas216_parse_message(info, message, msglen);
1269 fas216_cmd(info, CMD_MSGACCEPTED);
1273 fas216_cmd(info, CMD_SETATN);
1274 msgqueue_flush(&info->scsi.msgs);
1275 msgqueue_addmsg(&info->scsi.msgs, 1, MSG_PARITY_ERROR);
1276 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1277 fas216_cmd(info, CMD_MSGACCEPTED);
1282 * fas216_send_command - send command after all message bytes have been sent
1283 * @info: interface which caused bus service
1285 * Send a command to a target after all message bytes have been sent
1287 static void fas216_send_command(FAS216_Info *info)
1291 fas216_checkmagic(info);
1293 fas216_cmd(info, CMD_NOP|CMD_WITHDMA);
1294 fas216_cmd(info, CMD_FLUSHFIFO);
1297 for (i = info->scsi.SCp.sent_command; i < info->SCpnt->cmd_len; i++)
1298 fas216_writeb(info, REG_FF, info->SCpnt->cmnd[i]);
1300 fas216_cmd(info, CMD_TRANSFERINFO);
1302 info->scsi.phase = PHASE_COMMAND;
1306 * fas216_send_messageout - handle bus service to send a message
1307 * @info: interface which caused bus service
1309 * Handle bus service to send a message.
1310 * Note: We do not allow the device to change the data direction!
1312 static void fas216_send_messageout(FAS216_Info *info, int start)
1314 unsigned int tot_msglen = msgqueue_msglength(&info->scsi.msgs);
1316 fas216_checkmagic(info);
1318 fas216_cmd(info, CMD_FLUSHFIFO);
1321 struct message *msg;
1324 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1327 for (i = start; i < msg->length; i++)
1328 fas216_writeb(info, REG_FF, msg->msg[i]);
1330 msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
1334 fas216_writeb(info, REG_FF, NOP);
1336 fas216_cmd(info, CMD_TRANSFERINFO);
1338 info->scsi.phase = PHASE_MSGOUT;
1342 * fas216_busservice_intr - handle bus service interrupt from FAS216 chip
1343 * @info: interface which caused bus service interrupt
1344 * @stat: Status register contents
1345 * @is: SCSI Status register contents
1347 * Handle a bus service interrupt from FAS216 chip
1349 static void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
1351 fas216_checkmagic(info);
1353 fas216_log(info, LOG_BUSSERVICE,
1354 "bus service: stat=%02x is=%02x phase=%02x",
1355 stat, is, info->scsi.phase);
1357 switch (info->scsi.phase) {
1358 case PHASE_SELECTION:
1359 if ((is & IS_BITS) != IS_MSGBYTESENT)
1363 case PHASE_SELSTEPS:
1364 switch (is & IS_BITS) {
1366 case IS_MSGBYTESENT:
1371 if ((stat & STAT_BUSMASK) == STAT_MESGIN)
1384 fas216_cmd(info, CMD_NOP);
1386 #define STATE(st,ph) ((ph) << 3 | (st))
1387 /* This table describes the legal SCSI state transitions,
1388 * as described by the SCSI II spec.
1390 switch (STATE(stat & STAT_BUSMASK, info->scsi.phase)) {
1391 case STATE(STAT_DATAIN, PHASE_SELSTEPS):/* Sel w/ steps -> Data In */
1392 case STATE(STAT_DATAIN, PHASE_MSGOUT): /* Message Out -> Data In */
1393 case STATE(STAT_DATAIN, PHASE_COMMAND): /* Command -> Data In */
1394 case STATE(STAT_DATAIN, PHASE_MSGIN): /* Message In -> Data In */
1395 info->scsi.phase = PHASE_DATAIN;
1396 fas216_transfer(info);
1399 case STATE(STAT_DATAIN, PHASE_DATAIN): /* Data In -> Data In */
1400 case STATE(STAT_DATAOUT, PHASE_DATAOUT):/* Data Out -> Data Out */
1401 fas216_cleanuptransfer(info);
1402 fas216_transfer(info);
1405 case STATE(STAT_DATAOUT, PHASE_SELSTEPS):/* Sel w/ steps-> Data Out */
1406 case STATE(STAT_DATAOUT, PHASE_MSGOUT): /* Message Out -> Data Out */
1407 case STATE(STAT_DATAOUT, PHASE_COMMAND):/* Command -> Data Out */
1408 case STATE(STAT_DATAOUT, PHASE_MSGIN): /* Message In -> Data Out */
1409 fas216_cmd(info, CMD_FLUSHFIFO);
1410 info->scsi.phase = PHASE_DATAOUT;
1411 fas216_transfer(info);
1414 case STATE(STAT_STATUS, PHASE_DATAOUT): /* Data Out -> Status */
1415 case STATE(STAT_STATUS, PHASE_DATAIN): /* Data In -> Status */
1416 fas216_stoptransfer(info);
1419 case STATE(STAT_STATUS, PHASE_SELSTEPS):/* Sel w/ steps -> Status */
1420 case STATE(STAT_STATUS, PHASE_MSGOUT): /* Message Out -> Status */
1421 case STATE(STAT_STATUS, PHASE_COMMAND): /* Command -> Status */
1422 case STATE(STAT_STATUS, PHASE_MSGIN): /* Message In -> Status */
1423 fas216_cmd(info, CMD_INITCMDCOMPLETE);
1424 info->scsi.phase = PHASE_STATUS;
1427 case STATE(STAT_MESGIN, PHASE_DATAOUT): /* Data Out -> Message In */
1428 case STATE(STAT_MESGIN, PHASE_DATAIN): /* Data In -> Message In */
1429 fas216_stoptransfer(info);
1432 case STATE(STAT_MESGIN, PHASE_COMMAND): /* Command -> Message In */
1433 case STATE(STAT_MESGIN, PHASE_SELSTEPS):/* Sel w/ steps -> Message In */
1434 case STATE(STAT_MESGIN, PHASE_MSGOUT): /* Message Out -> Message In */
1435 info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
1436 fas216_cmd(info, CMD_FLUSHFIFO);
1437 fas216_cmd(info, CMD_TRANSFERINFO);
1438 info->scsi.phase = PHASE_MSGIN;
1441 case STATE(STAT_MESGIN, PHASE_MSGIN):
1442 info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
1443 fas216_cmd(info, CMD_TRANSFERINFO);
1446 case STATE(STAT_COMMAND, PHASE_MSGOUT): /* Message Out -> Command */
1447 case STATE(STAT_COMMAND, PHASE_MSGIN): /* Message In -> Command */
1448 fas216_send_command(info);
1449 info->scsi.phase = PHASE_COMMAND;
1454 * Selection -> Message Out
1456 case STATE(STAT_MESGOUT, PHASE_SELECTION):
1457 fas216_send_messageout(info, 1);
1461 * Message Out -> Message Out
1463 case STATE(STAT_MESGOUT, PHASE_SELSTEPS):
1464 case STATE(STAT_MESGOUT, PHASE_MSGOUT):
1466 * If we get another message out phase, this usually
1467 * means some parity error occurred. Resend complete
1468 * set of messages. If we have more than one byte to
1469 * send, we need to assert ATN again.
1471 if (info->device[info->SCpnt->device->id].parity_check) {
1473 * We were testing... good, the device
1474 * supports parity checking.
1476 info->device[info->SCpnt->device->id].parity_check = 0;
1477 info->device[info->SCpnt->device->id].parity_enabled = 1;
1478 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
1481 if (msgqueue_msglength(&info->scsi.msgs) > 1)
1482 fas216_cmd(info, CMD_SETATN);
1486 * Any -> Message Out
1488 case STATE(STAT_MESGOUT, PHASE_MSGOUT_EXPECT):
1489 fas216_send_messageout(info, 0);
1492 /* Error recovery rules.
1493 * These either attempt to abort or retry the operation.
1494 * TODO: we need more of these
1496 case STATE(STAT_COMMAND, PHASE_COMMAND):/* Command -> Command */
1497 /* error - we've sent out all the command bytes
1499 * NOTE: we need SAVE DATA POINTERS/RESTORE DATA POINTERS
1500 * to include the command bytes sent for this to work
1503 printk(KERN_ERR "scsi%d.%c: "
1504 "target trying to receive more command bytes\n",
1505 info->host->host_no, fas216_target(info));
1506 fas216_cmd(info, CMD_SETATN);
1507 fas216_set_stc(info, 15);
1508 fas216_cmd(info, CMD_PADBYTES | CMD_WITHDMA);
1509 msgqueue_flush(&info->scsi.msgs);
1510 msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1511 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1515 if (info->scsi.phase == PHASE_MSGIN_DISCONNECT) {
1516 printk(KERN_ERR "scsi%d.%c: disconnect message received, but bus service %s?\n",
1517 info->host->host_no, fas216_target(info),
1518 fas216_bus_phase(stat));
1519 msgqueue_flush(&info->scsi.msgs);
1520 fas216_cmd(info, CMD_SETATN);
1521 msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1522 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1523 info->scsi.aborting = 1;
1524 fas216_cmd(info, CMD_TRANSFERINFO);
1527 printk(KERN_ERR "scsi%d.%c: bus phase %s after %s?\n",
1528 info->host->host_no, fas216_target(info),
1529 fas216_bus_phase(stat),
1530 fas216_drv_phase(info));
1535 fas216_log(info, 0, "bus service at step %d?", is & IS_BITS);
1536 fas216_dumpstate(info);
1539 fas216_done(info, DID_ERROR);
1543 * fas216_funcdone_intr - handle a function done interrupt from FAS216 chip
1544 * @info: interface which caused function done interrupt
1545 * @stat: Status register contents
1546 * @is: SCSI Status register contents
1548 * Handle a function done interrupt from FAS216 chip
1550 static void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
1552 unsigned int fifo_len = fas216_readb(info, REG_CFIS) & CFIS_CF;
1554 fas216_checkmagic(info);
1556 fas216_log(info, LOG_FUNCTIONDONE,
1557 "function done: stat=%02x is=%02x phase=%02x",
1558 stat, is, info->scsi.phase);
1560 switch (info->scsi.phase) {
1561 case PHASE_STATUS: /* status phase - read status and msg */
1562 if (fifo_len != 2) {
1563 fas216_log(info, 0, "odd number of bytes in FIFO: %d", fifo_len);
1566 * Read status then message byte.
1568 info->scsi.SCp.Status = fas216_readb(info, REG_FF);
1569 info->scsi.SCp.Message = fas216_readb(info, REG_FF);
1570 info->scsi.phase = PHASE_DONE;
1571 fas216_cmd(info, CMD_MSGACCEPTED);
1575 case PHASE_SELECTION:
1576 case PHASE_SELSTEPS:
1579 case PHASE_MSGIN: /* message in phase */
1580 if ((stat & STAT_BUSMASK) == STAT_MESGIN) {
1581 info->scsi.msgin_fifo = fifo_len;
1582 fas216_message(info);
1588 fas216_log(info, 0, "internal phase %s for function done?"
1589 " What do I do with this?",
1590 fas216_target(info), fas216_drv_phase(info));
1594 static void fas216_bus_reset(FAS216_Info *info)
1599 msgqueue_flush(&info->scsi.msgs);
1601 sync_state = neg_invalid;
1604 if (info->ifcfg.capabilities & (FASCAP_DMA|FASCAP_PSEUDODMA))
1605 sync_state = neg_wait;
1608 info->scsi.phase = PHASE_IDLE;
1609 info->SCpnt = NULL; /* bug! */
1610 memset(&info->scsi.SCp, 0, sizeof(info->scsi.SCp));
1612 for (i = 0; i < 8; i++) {
1613 info->device[i].disconnect_ok = info->ifcfg.disconnect_ok;
1614 info->device[i].sync_state = sync_state;
1615 info->device[i].period = info->ifcfg.asyncperiod / 4;
1616 info->device[i].stp = info->scsi.async_stp;
1617 info->device[i].sof = 0;
1618 info->device[i].wide_xfer = 0;
1621 info->rst_bus_status = 1;
1622 wake_up(&info->eh_wait);
1626 * fas216_intr - handle interrupts to progress a command
1627 * @info: interface to service
1629 * Handle interrupts from the interface to progress a command
1631 irqreturn_t fas216_intr(FAS216_Info *info)
1633 unsigned char inst, is, stat;
1634 int handled = IRQ_NONE;
1636 fas216_checkmagic(info);
1638 stat = fas216_readb(info, REG_STAT);
1639 is = fas216_readb(info, REG_IS);
1640 inst = fas216_readb(info, REG_INST);
1642 add_debug_list(stat, is, inst, info->scsi.phase);
1644 if (stat & STAT_INT) {
1645 if (inst & INST_BUSRESET) {
1646 fas216_log(info, 0, "bus reset detected");
1647 fas216_bus_reset(info);
1648 scsi_report_bus_reset(info->host, 0);
1649 } else if (inst & INST_ILLEGALCMD) {
1650 fas216_log(info, LOG_ERROR, "illegal command given\n");
1651 fas216_dumpstate(info);
1653 } else if (inst & INST_DISCONNECT)
1654 fas216_disconnect_intr(info);
1655 else if (inst & INST_RESELECTED) /* reselected */
1656 fas216_reselected_intr(info);
1657 else if (inst & INST_BUSSERVICE) /* bus service request */
1658 fas216_busservice_intr(info, stat, is);
1659 else if (inst & INST_FUNCDONE) /* function done */
1660 fas216_funcdone_intr(info, stat, is);
1662 fas216_log(info, 0, "unknown interrupt received:"
1663 " phase %s inst %02X is %02X stat %02X",
1664 fas216_drv_phase(info), inst, is, stat);
1665 handled = IRQ_HANDLED;
1670 static void __fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
1674 /* following what the ESP driver says */
1675 fas216_set_stc(info, 0);
1676 fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
1679 fas216_cmd(info, CMD_FLUSHFIFO);
1681 /* load bus-id and timeout */
1682 fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
1683 fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
1685 /* synchronous transfers */
1686 fas216_set_sync(info, SCpnt->device->id);
1688 tot_msglen = msgqueue_msglength(&info->scsi.msgs);
1690 #ifdef DEBUG_MESSAGES
1692 struct message *msg;
1695 printk("scsi%d.%c: message out: ",
1696 info->host->host_no, '0' + SCpnt->device->id);
1697 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1699 for (i = 0; i < msg->length; i++)
1700 printk("%02x ", msg->msg[i]);
1707 if (tot_msglen == 1 || tot_msglen == 3) {
1709 * We have an easy message length to send...
1711 struct message *msg;
1714 info->scsi.phase = PHASE_SELSTEPS;
1716 /* load message bytes */
1717 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1718 for (i = 0; i < msg->length; i++)
1719 fas216_writeb(info, REG_FF, msg->msg[i]);
1720 msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
1724 for (i = 0; i < SCpnt->cmd_len; i++)
1725 fas216_writeb(info, REG_FF, SCpnt->cmnd[i]);
1727 if (tot_msglen == 1)
1728 fas216_cmd(info, CMD_SELECTATN);
1730 fas216_cmd(info, CMD_SELECTATN3);
1733 * We have an unusual number of message bytes to send.
1734 * Load first byte into fifo, and issue SELECT with ATN and
1737 struct message *msg = msgqueue_getmsg(&info->scsi.msgs, 0);
1739 fas216_writeb(info, REG_FF, msg->msg[0]);
1742 fas216_cmd(info, CMD_SELECTATNSTOP);
1747 * Decide whether we need to perform a parity test on this device.
1748 * Can also be used to force parity error conditions during initial
1749 * information transfer phase (message out) for test purposes.
1751 static int parity_test(FAS216_Info *info, int target)
1755 info->device[target].parity_check = 0;
1759 return info->device[target].parity_check;
1762 static void fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
1769 info->scsi.phase = PHASE_SELECTION;
1770 info->scsi.SCp = SCpnt->SCp;
1771 info->SCpnt = SCpnt;
1772 info->dma.transfer_type = fasdma_none;
1774 if (parity_test(info, SCpnt->device->id))
1775 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_PTE);
1777 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
1780 * Don't allow request sense commands to disconnect.
1782 disconnect_ok = SCpnt->cmnd[0] != REQUEST_SENSE &&
1783 info->device[SCpnt->device->id].disconnect_ok;
1786 * build outgoing message bytes
1788 msgqueue_flush(&info->scsi.msgs);
1789 msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(disconnect_ok, SCpnt->device->lun));
1792 * add tag message if required
1795 msgqueue_addmsg(&info->scsi.msgs, 2, SIMPLE_QUEUE_TAG, SCpnt->tag);
1799 if ((info->device[SCpnt->device->id].sync_state == neg_wait ||
1800 info->device[SCpnt->device->id].sync_state == neg_complete) &&
1801 (SCpnt->cmnd[0] == REQUEST_SENSE ||
1802 SCpnt->cmnd[0] == INQUIRY)) {
1803 info->device[SCpnt->device->id].sync_state = neg_inprogress;
1804 msgqueue_addmsg(&info->scsi.msgs, 5,
1805 EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
1806 1000 / info->ifcfg.clockrate,
1807 info->ifcfg.sync_max_depth);
1813 __fas216_start_command(info, SCpnt);
1816 static void fas216_allocate_tag(FAS216_Info *info, struct scsi_cmnd *SCpnt)
1820 * tagged queuing - allocate a new tag to this command
1822 if (SCpnt->device->simple_tags && SCpnt->cmnd[0] != REQUEST_SENSE &&
1823 SCpnt->cmnd[0] != INQUIRY) {
1824 SCpnt->device->current_tag += 1;
1825 if (SCpnt->device->current_tag == 0)
1826 SCpnt->device->current_tag = 1;
1827 SCpnt->tag = SCpnt->device->current_tag;
1830 set_bit(SCpnt->device->id * 8 +
1831 (u8)(SCpnt->device->lun & 0x7), info->busyluns);
1833 info->stats.removes += 1;
1834 switch (SCpnt->cmnd[0]) {
1838 info->stats.writes += 1;
1843 info->stats.reads += 1;
1846 info->stats.miscs += 1;
1851 static void fas216_do_bus_device_reset(FAS216_Info *info,
1852 struct scsi_cmnd *SCpnt)
1854 struct message *msg;
1859 info->scsi.phase = PHASE_SELECTION;
1860 info->scsi.SCp = SCpnt->SCp;
1861 info->SCpnt = SCpnt;
1862 info->dma.transfer_type = fasdma_none;
1864 fas216_log(info, LOG_ERROR, "sending bus device reset");
1866 msgqueue_flush(&info->scsi.msgs);
1867 msgqueue_addmsg(&info->scsi.msgs, 1, BUS_DEVICE_RESET);
1869 /* following what the ESP driver says */
1870 fas216_set_stc(info, 0);
1871 fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
1874 fas216_cmd(info, CMD_FLUSHFIFO);
1876 /* load bus-id and timeout */
1877 fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
1878 fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
1880 /* synchronous transfers */
1881 fas216_set_sync(info, SCpnt->device->id);
1883 msg = msgqueue_getmsg(&info->scsi.msgs, 0);
1885 fas216_writeb(info, REG_FF, BUS_DEVICE_RESET);
1888 fas216_cmd(info, CMD_SELECTATNSTOP);
1892 * fas216_kick - kick a command to the interface
1893 * @info: our host interface to kick
1895 * Kick a command to the interface, interface should be idle.
1896 * Notes: Interrupts are always disabled!
1898 static void fas216_kick(FAS216_Info *info)
1900 struct scsi_cmnd *SCpnt = NULL;
1901 #define TYPE_OTHER 0
1902 #define TYPE_RESET 1
1903 #define TYPE_QUEUE 2
1904 int where_from = TYPE_OTHER;
1906 fas216_checkmagic(info);
1909 * Obtain the next command to process.
1912 if (info->rstSCpnt) {
1913 SCpnt = info->rstSCpnt;
1914 /* don't remove it */
1915 where_from = TYPE_RESET;
1919 if (info->reqSCpnt) {
1920 SCpnt = info->reqSCpnt;
1921 info->reqSCpnt = NULL;
1925 if (info->origSCpnt) {
1926 SCpnt = info->origSCpnt;
1927 info->origSCpnt = NULL;
1931 /* retrieve next command */
1933 SCpnt = queue_remove_exclude(&info->queues.issue,
1935 where_from = TYPE_QUEUE;
1942 * no command pending, so enable reselection.
1944 fas216_cmd(info, CMD_ENABLESEL);
1949 * We're going to start a command, so disable reselection
1951 fas216_cmd(info, CMD_DISABLESEL);
1953 if (info->scsi.disconnectable && info->SCpnt) {
1954 fas216_log(info, LOG_CONNECT,
1955 "moved command for %d to disconnected queue",
1956 info->SCpnt->device->id);
1957 queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
1958 info->scsi.disconnectable = 0;
1962 fas216_log_command(info, LOG_CONNECT | LOG_MESSAGES, SCpnt,
1965 switch (where_from) {
1967 fas216_allocate_tag(info, SCpnt);
1970 fas216_start_command(info, SCpnt);
1973 fas216_do_bus_device_reset(info, SCpnt);
1977 fas216_log(info, LOG_CONNECT, "select: data pointers [%p, %X]",
1978 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1981 * should now get either DISCONNECT or
1982 * (FUNCTION DONE with BUS SERVICE) interrupt
1987 * Clean up from issuing a BUS DEVICE RESET message to a device.
1989 static void fas216_devicereset_done(FAS216_Info *info, struct scsi_cmnd *SCpnt,
1990 unsigned int result)
1992 fas216_log(info, LOG_ERROR, "fas216 device reset complete");
1994 info->rstSCpnt = NULL;
1995 info->rst_dev_status = 1;
1996 wake_up(&info->eh_wait);
2000 * fas216_rq_sns_done - Finish processing automatic request sense command
2001 * @info: interface that completed
2002 * @SCpnt: command that completed
2003 * @result: driver byte of result
2005 * Finish processing automatic request sense command
2007 static void fas216_rq_sns_done(FAS216_Info *info, struct scsi_cmnd *SCpnt,
2008 unsigned int result)
2010 fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
2011 "request sense complete, result=0x%04x%02x%02x",
2012 result, SCpnt->SCp.Message, SCpnt->SCp.Status);
2014 if (result != DID_OK || SCpnt->SCp.Status != SAM_STAT_GOOD)
2016 * Something went wrong. Make sure that we don't
2017 * have valid data in the sense buffer that could
2018 * confuse the higher levels.
2020 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2021 //printk("scsi%d.%c: sense buffer: ", info->host->host_no, '0' + SCpnt->device->id);
2022 //{ int i; for (i = 0; i < 32; i++) printk("%02x ", SCpnt->sense_buffer[i]); printk("\n"); }
2024 * Note that we don't set SCpnt->result, since that should
2025 * reflect the status of the command that we were asked by
2026 * the upper layers to process. This would have been set
2027 * correctly by fas216_std_done.
2029 scsi_eh_restore_cmnd(SCpnt, &info->ses);
2030 SCpnt->scsi_done(SCpnt);
2034 * fas216_std_done - finish processing of standard command
2035 * @info: interface that completed
2036 * @SCpnt: command that completed
2037 * @result: driver byte of result
2039 * Finish processing of standard command
2042 fas216_std_done(FAS216_Info *info, struct scsi_cmnd *SCpnt, unsigned int result)
2044 info->stats.fins += 1;
2046 set_host_byte(SCpnt, result);
2047 if (result == DID_OK)
2048 scsi_msg_to_host_byte(SCpnt, info->scsi.SCp.Message);
2049 set_status_byte(SCpnt, info->scsi.SCp.Status);
2051 fas216_log_command(info, LOG_CONNECT, SCpnt,
2052 "command complete, result=0x%08x", SCpnt->result);
2055 * If the driver detected an error, we're all done.
2057 if (get_host_byte(SCpnt) != DID_OK)
2061 * If the command returned CHECK_CONDITION or COMMAND_TERMINATED
2062 * status, request the sense information.
2064 if (get_status_byte(SCpnt) == SAM_STAT_CHECK_CONDITION ||
2065 get_status_byte(SCpnt) == SAM_STAT_COMMAND_TERMINATED)
2069 * If the command did not complete with GOOD status,
2070 * we are all done here.
2072 if (get_status_byte(SCpnt) != SAM_STAT_GOOD)
2076 * We have successfully completed a command. Make sure that
2077 * we do not have any buffers left to transfer. The world
2078 * is not perfect, and we seem to occasionally hit this.
2079 * It can be indicative of a buggy driver, target or the upper
2080 * levels of the SCSI code.
2082 if (info->scsi.SCp.ptr) {
2083 switch (SCpnt->cmnd[0]) {
2090 scmd_printk(KERN_ERR, SCpnt,
2091 "incomplete data transfer detected: res=%08X ptr=%p len=%X\n",
2092 SCpnt->result, info->scsi.SCp.ptr,
2093 info->scsi.SCp.this_residual);
2094 scsi_print_command(SCpnt);
2095 set_host_byte(SCpnt, DID_ERROR);
2101 if (SCpnt->scsi_done) {
2102 SCpnt->scsi_done(SCpnt);
2106 panic("scsi%d.H: null scsi_done function in fas216_done",
2107 info->host->host_no);
2111 if (SCpnt->cmnd[0] == REQUEST_SENSE)
2114 scsi_eh_prep_cmnd(SCpnt, &info->ses, NULL, 0, ~0);
2115 fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
2116 "requesting sense");
2118 SCpnt->SCp.Message = 0;
2119 SCpnt->SCp.Status = 0;
2121 SCpnt->host_scribble = (void *)fas216_rq_sns_done;
2124 * Place this command into the high priority "request
2125 * sense" slot. This will be the very next command
2126 * executed, unless a target connects to us.
2129 printk(KERN_WARNING "scsi%d.%c: losing request command\n",
2130 info->host->host_no, '0' + SCpnt->device->id);
2131 info->reqSCpnt = SCpnt;
2135 * fas216_done - complete processing for current command
2136 * @info: interface that completed
2137 * @result: driver byte of result
2139 * Complete processing for current command
2141 static void fas216_done(FAS216_Info *info, unsigned int result)
2143 void (*fn)(FAS216_Info *, struct scsi_cmnd *, unsigned int);
2144 struct scsi_cmnd *SCpnt;
2145 unsigned long flags;
2147 fas216_checkmagic(info);
2152 SCpnt = info->SCpnt;
2154 info->scsi.phase = PHASE_IDLE;
2156 if (info->scsi.aborting) {
2157 fas216_log(info, 0, "uncaught abort - returning DID_ABORT");
2159 info->scsi.aborting = 0;
2163 * Sanity check the completion - if we have zero bytes left
2164 * to transfer, we should not have a valid pointer.
2166 if (info->scsi.SCp.ptr && info->scsi.SCp.this_residual == 0) {
2167 scmd_printk(KERN_INFO, SCpnt,
2168 "zero bytes left to transfer, but buffer pointer still valid: ptr=%p len=%08x\n",
2169 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
2170 info->scsi.SCp.ptr = NULL;
2171 scsi_print_command(SCpnt);
2175 * Clear down this command as completed. If we need to request
2176 * the sense information, fas216_kick will re-assert the busy
2179 info->device[SCpnt->device->id].parity_check = 0;
2180 clear_bit(SCpnt->device->id * 8 +
2181 (u8)(SCpnt->device->lun & 0x7), info->busyluns);
2183 fn = (void (*)(FAS216_Info *, struct scsi_cmnd *, unsigned int))SCpnt->host_scribble;
2184 fn(info, SCpnt, result);
2186 if (info->scsi.irq) {
2187 spin_lock_irqsave(&info->host_lock, flags);
2188 if (info->scsi.phase == PHASE_IDLE)
2190 spin_unlock_irqrestore(&info->host_lock, flags);
2195 panic("scsi%d.H: null command in fas216_done",
2196 info->host->host_no);
2200 * fas216_queue_command - queue a command for adapter to process.
2201 * @SCpnt: Command to queue
2202 * @done: done function to call once command is complete
2204 * Queue a command for adapter to process.
2205 * Returns: 0 on success, else error.
2206 * Notes: io_request_lock is held, interrupts are disabled.
2208 static int fas216_queue_command_lck(struct scsi_cmnd *SCpnt,
2209 void (*done)(struct scsi_cmnd *))
2211 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2214 fas216_checkmagic(info);
2216 fas216_log_command(info, LOG_CONNECT, SCpnt,
2217 "received command (%p)", SCpnt);
2219 SCpnt->scsi_done = done;
2220 SCpnt->host_scribble = (void *)fas216_std_done;
2225 info->stats.queues += 1;
2228 spin_lock(&info->host_lock);
2231 * Add command into execute queue and let it complete under
2232 * whatever scheme we're using.
2234 result = !queue_add_cmd_ordered(&info->queues.issue, SCpnt);
2237 * If we successfully added the command,
2238 * kick the interface to get it moving.
2240 if (result == 0 && info->scsi.phase == PHASE_IDLE)
2242 spin_unlock(&info->host_lock);
2244 fas216_log_target(info, LOG_CONNECT, -1, "queue %s",
2245 result ? "failure" : "success");
2250 DEF_SCSI_QCMD(fas216_queue_command)
2253 * fas216_internal_done - trigger restart of a waiting thread in fas216_noqueue_command
2254 * @SCpnt: Command to wake
2256 * Trigger restart of a waiting thread in fas216_command
2258 static void fas216_internal_done(struct scsi_cmnd *SCpnt)
2260 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2262 fas216_checkmagic(info);
2264 info->internal_done = 1;
2268 * fas216_noqueue_command - process a command for the adapter.
2269 * @SCpnt: Command to queue
2271 * Queue a command for adapter to process.
2272 * Returns: scsi result code.
2273 * Notes: io_request_lock is held, interrupts are disabled.
2275 static int fas216_noqueue_command_lck(struct scsi_cmnd *SCpnt,
2276 void (*done)(struct scsi_cmnd *))
2278 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2280 fas216_checkmagic(info);
2283 * We should only be using this if we don't have an interrupt.
2284 * Provide some "incentive" to use the queueing code.
2286 BUG_ON(info->scsi.irq);
2288 info->internal_done = 0;
2289 fas216_queue_command_lck(SCpnt, fas216_internal_done);
2292 * This wastes time, since we can't return until the command is
2293 * complete. We can't sleep either since we may get re-entered!
2294 * However, we must re-enable interrupts, or else we'll be
2297 spin_unlock_irq(info->host->host_lock);
2299 while (!info->internal_done) {
2301 * If we don't have an IRQ, then we must poll the card for
2302 * it's interrupt, and use that to call this driver's
2303 * interrupt routine. That way, we keep the command
2304 * progressing. Maybe we can add some intelligence here
2305 * and go to sleep if we know that the device is going
2306 * to be some time (eg, disconnected).
2308 if (fas216_readb(info, REG_STAT) & STAT_INT) {
2309 spin_lock_irq(info->host->host_lock);
2311 spin_unlock_irq(info->host->host_lock);
2315 spin_lock_irq(info->host->host_lock);
2322 DEF_SCSI_QCMD(fas216_noqueue_command)
2325 * Error handler timeout function. Indicate that we timed out,
2326 * and wake up any error handler process so it can continue.
2328 static void fas216_eh_timer(struct timer_list *t)
2330 FAS216_Info *info = from_timer(info, t, eh_timer);
2332 fas216_log(info, LOG_ERROR, "error handling timed out\n");
2334 del_timer(&info->eh_timer);
2336 if (info->rst_bus_status == 0)
2337 info->rst_bus_status = -1;
2338 if (info->rst_dev_status == 0)
2339 info->rst_dev_status = -1;
2341 wake_up(&info->eh_wait);
2345 res_failed, /* not found */
2346 res_success, /* command on issue queue */
2347 res_hw_abort /* command on disconnected dev */
2351 * fas216_do_abort - decide how to abort a command
2352 * @SCpnt: command to abort
2354 * Decide how to abort a command.
2355 * Returns: abort status
2357 static enum res_find fas216_find_command(FAS216_Info *info,
2358 struct scsi_cmnd *SCpnt)
2360 enum res_find res = res_failed;
2362 if (queue_remove_cmd(&info->queues.issue, SCpnt)) {
2364 * The command was on the issue queue, and has not been
2365 * issued yet. We can remove the command from the queue,
2366 * and acknowledge the abort. Neither the device nor the
2367 * interface know about the command.
2369 printk("on issue queue ");
2372 } else if (queue_remove_cmd(&info->queues.disconnected, SCpnt)) {
2374 * The command was on the disconnected queue. We must
2375 * reconnect with the device if possible, and send it
2378 printk("on disconnected queue ");
2381 } else if (info->SCpnt == SCpnt) {
2382 printk("executing ");
2384 switch (info->scsi.phase) {
2386 * If the interface is idle, and the command is 'disconnectable',
2387 * then it is the same as on the disconnected queue.
2390 if (info->scsi.disconnectable) {
2391 info->scsi.disconnectable = 0;
2400 } else if (info->origSCpnt == SCpnt) {
2402 * The command will be executed next, but a command
2403 * is currently using the interface. This is similar to
2404 * being on the issue queue, except the busylun bit has
2407 info->origSCpnt = NULL;
2408 clear_bit(SCpnt->device->id * 8 +
2409 (u8)(SCpnt->device->lun & 0x7), info->busyluns);
2410 printk("waiting for execution ");
2419 * fas216_eh_abort - abort this command
2420 * @SCpnt: command to abort
2422 * Abort this command.
2423 * Returns: FAILED if unable to abort
2424 * Notes: io_request_lock is taken, and irqs are disabled
2426 int fas216_eh_abort(struct scsi_cmnd *SCpnt)
2428 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2429 int result = FAILED;
2431 fas216_checkmagic(info);
2433 info->stats.aborts += 1;
2435 scmd_printk(KERN_WARNING, SCpnt, "abort command\n");
2438 fas216_dumpstate(info);
2440 switch (fas216_find_command(info, SCpnt)) {
2442 * We found the command, and cleared it out. Either
2443 * the command is still known to be executing on the
2444 * target, or the busylun bit is not set.
2447 scmd_printk(KERN_WARNING, SCpnt, "abort %p success\n", SCpnt);
2452 * We need to reconnect to the target and send it an
2453 * ABORT or ABORT_TAG message. We can only do this
2454 * if the bus is free.
2459 * We are unable to abort the command for some reason.
2463 scmd_printk(KERN_WARNING, SCpnt, "abort %p failed\n", SCpnt);
2471 * fas216_eh_device_reset - Reset the device associated with this command
2472 * @SCpnt: command specifing device to reset
2474 * Reset the device associated with this command.
2475 * Returns: FAILED if unable to reset.
2476 * Notes: We won't be re-entered, so we'll only have one device
2477 * reset on the go at one time.
2479 int fas216_eh_device_reset(struct scsi_cmnd *SCpnt)
2481 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2482 unsigned long flags;
2483 int i, res = FAILED, target = SCpnt->device->id;
2485 fas216_log(info, LOG_ERROR, "device reset for target %d", target);
2487 spin_lock_irqsave(&info->host_lock, flags);
2491 * If we are currently connected to a device, and
2492 * it is the device we want to reset, there is
2493 * nothing we can do here. Chances are it is stuck,
2494 * and we need a bus reset.
2496 if (info->SCpnt && !info->scsi.disconnectable &&
2497 info->SCpnt->device->id == SCpnt->device->id)
2501 * We're going to be resetting this device. Remove
2502 * all pending commands from the driver. By doing
2503 * so, we guarantee that we won't touch the command
2504 * structures except to process the reset request.
2506 queue_remove_all_target(&info->queues.issue, target);
2507 queue_remove_all_target(&info->queues.disconnected, target);
2508 if (info->origSCpnt && info->origSCpnt->device->id == target)
2509 info->origSCpnt = NULL;
2510 if (info->reqSCpnt && info->reqSCpnt->device->id == target)
2511 info->reqSCpnt = NULL;
2512 for (i = 0; i < 8; i++)
2513 clear_bit(target * 8 + i, info->busyluns);
2516 * Hijack this SCSI command structure to send
2517 * a bus device reset message to this device.
2519 SCpnt->host_scribble = (void *)fas216_devicereset_done;
2521 info->rst_dev_status = 0;
2522 info->rstSCpnt = SCpnt;
2524 if (info->scsi.phase == PHASE_IDLE)
2527 mod_timer(&info->eh_timer, jiffies + 30 * HZ);
2528 spin_unlock_irqrestore(&info->host_lock, flags);
2531 * Wait up to 30 seconds for the reset to complete.
2533 wait_event(info->eh_wait, info->rst_dev_status);
2535 del_timer_sync(&info->eh_timer);
2536 spin_lock_irqsave(&info->host_lock, flags);
2537 info->rstSCpnt = NULL;
2539 if (info->rst_dev_status == 1)
2543 SCpnt->host_scribble = NULL;
2544 spin_unlock_irqrestore(&info->host_lock, flags);
2546 fas216_log(info, LOG_ERROR, "device reset complete: %s\n",
2547 res == SUCCESS ? "success" : "failed");
2553 * fas216_eh_bus_reset - Reset the bus associated with the command
2554 * @SCpnt: command specifing bus to reset
2556 * Reset the bus associated with the command.
2557 * Returns: FAILED if unable to reset.
2558 * Notes: Further commands are blocked.
2560 int fas216_eh_bus_reset(struct scsi_cmnd *SCpnt)
2562 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2563 unsigned long flags;
2564 struct scsi_device *SDpnt;
2566 fas216_checkmagic(info);
2567 fas216_log(info, LOG_ERROR, "resetting bus");
2569 info->stats.bus_resets += 1;
2571 spin_lock_irqsave(&info->host_lock, flags);
2574 * Stop all activity on this interface.
2576 fas216_aborttransfer(info);
2577 fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
2580 * Clear any pending interrupts.
2582 while (fas216_readb(info, REG_STAT) & STAT_INT)
2583 fas216_readb(info, REG_INST);
2585 info->rst_bus_status = 0;
2588 * For each attached hard-reset device, clear out
2589 * all command structures. Leave the running
2592 shost_for_each_device(SDpnt, info->host) {
2595 if (SDpnt->soft_reset)
2598 queue_remove_all_target(&info->queues.issue, SDpnt->id);
2599 queue_remove_all_target(&info->queues.disconnected, SDpnt->id);
2600 if (info->origSCpnt && info->origSCpnt->device->id == SDpnt->id)
2601 info->origSCpnt = NULL;
2602 if (info->reqSCpnt && info->reqSCpnt->device->id == SDpnt->id)
2603 info->reqSCpnt = NULL;
2606 for (i = 0; i < 8; i++)
2607 clear_bit(SDpnt->id * 8 + i, info->busyluns);
2610 info->scsi.phase = PHASE_IDLE;
2613 * Reset the SCSI bus. Device cleanup happens in
2614 * the interrupt handler.
2616 fas216_cmd(info, CMD_RESETSCSI);
2618 mod_timer(&info->eh_timer, jiffies + HZ);
2619 spin_unlock_irqrestore(&info->host_lock, flags);
2622 * Wait one second for the interrupt.
2624 wait_event(info->eh_wait, info->rst_bus_status);
2625 del_timer_sync(&info->eh_timer);
2627 fas216_log(info, LOG_ERROR, "bus reset complete: %s\n",
2628 info->rst_bus_status == 1 ? "success" : "failed");
2630 return info->rst_bus_status == 1 ? SUCCESS : FAILED;
2634 * fas216_init_chip - Initialise FAS216 state after reset
2635 * @info: state structure for interface
2637 * Initialise FAS216 state after reset
2639 static void fas216_init_chip(FAS216_Info *info)
2641 unsigned int clock = ((info->ifcfg.clockrate - 1) / 5 + 1) & 7;
2642 fas216_writeb(info, REG_CLKF, clock);
2643 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2644 fas216_writeb(info, REG_CNTL2, info->scsi.cfg[1]);
2645 fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
2646 fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
2647 fas216_writeb(info, REG_SOF, 0);
2648 fas216_writeb(info, REG_STP, info->scsi.async_stp);
2649 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2653 * fas216_eh_host_reset - Reset the host associated with this command
2654 * @SCpnt: command specifing host to reset
2656 * Reset the host associated with this command.
2657 * Returns: FAILED if unable to reset.
2658 * Notes: io_request_lock is taken, and irqs are disabled
2660 int fas216_eh_host_reset(struct scsi_cmnd *SCpnt)
2662 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2664 spin_lock_irq(info->host->host_lock);
2666 fas216_checkmagic(info);
2668 fas216_log(info, LOG_ERROR, "resetting host");
2671 * Reset the SCSI chip.
2673 fas216_cmd(info, CMD_RESETCHIP);
2677 * We need to release the host_lock and enable
2678 * IRQs if we sleep, but we must relock and disable
2679 * IRQs after the sleep.
2681 spin_unlock_irq(info->host->host_lock);
2682 msleep(50 * 1000/100);
2683 spin_lock_irq(info->host->host_lock);
2686 * Release the SCSI reset.
2688 fas216_cmd(info, CMD_NOP);
2690 fas216_init_chip(info);
2692 spin_unlock_irq(info->host->host_lock);
2696 #define TYPE_UNKNOWN 0
2697 #define TYPE_NCR53C90 1
2698 #define TYPE_NCR53C90A 2
2699 #define TYPE_NCR53C9x 3
2700 #define TYPE_Am53CF94 4
2701 #define TYPE_EmFAS216 5
2702 #define TYPE_QLFAS216 6
2704 static char *chip_types[] = {
2714 static int fas216_detect_type(FAS216_Info *info)
2721 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2723 fas216_writeb(info, REG_CMD, CMD_NOP);
2726 * Check to see if control reg 2 is present.
2728 fas216_writeb(info, REG_CNTL3, 0);
2729 fas216_writeb(info, REG_CNTL2, CNTL2_S2FE);
2732 * If we are unable to read back control reg 2
2733 * correctly, it is not present, and we have a
2736 if ((fas216_readb(info, REG_CNTL2) & (~0xe0)) != CNTL2_S2FE)
2737 return TYPE_NCR53C90;
2740 * Now, check control register 3
2742 fas216_writeb(info, REG_CNTL2, 0);
2743 fas216_writeb(info, REG_CNTL3, 0);
2744 fas216_writeb(info, REG_CNTL3, 5);
2747 * If we are unable to read the register back
2748 * correctly, we have a NCR53C90A
2750 if (fas216_readb(info, REG_CNTL3) != 5)
2751 return TYPE_NCR53C90A;
2754 * Now read the ID from the chip.
2756 fas216_writeb(info, REG_CNTL3, 0);
2758 fas216_writeb(info, REG_CNTL3, CNTL3_ADIDCHK);
2759 fas216_writeb(info, REG_CNTL3, 0);
2761 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2763 fas216_writeb(info, REG_CMD, CMD_WITHDMA | CMD_NOP);
2765 fas216_writeb(info, REG_CNTL2, CNTL2_ENF);
2766 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2768 fas216_writeb(info, REG_CMD, CMD_NOP);
2770 rev = fas216_readb(info, REG_ID);
2777 return TYPE_Am53CF94;
2783 return TYPE_EmFAS216;
2785 return TYPE_QLFAS216;
2792 printk("family %x rev %x\n", family, rev);
2793 return TYPE_NCR53C9x;
2797 * fas216_reset_state - Initialise driver internal state
2798 * @info: state to initialise
2800 * Initialise driver internal state
2802 static void fas216_reset_state(FAS216_Info *info)
2806 fas216_checkmagic(info);
2808 fas216_bus_reset(info);
2811 * Clear out all stale info in our state structure
2813 memset(info->busyluns, 0, sizeof(info->busyluns));
2814 info->scsi.disconnectable = 0;
2815 info->scsi.aborting = 0;
2817 for (i = 0; i < 8; i++) {
2818 info->device[i].parity_enabled = 0;
2819 info->device[i].parity_check = 1;
2823 * Drain all commands on disconnected queue
2825 while (queue_remove(&info->queues.disconnected) != NULL);
2828 * Remove executing commands.
2831 info->reqSCpnt = NULL;
2832 info->rstSCpnt = NULL;
2833 info->origSCpnt = NULL;
2837 * fas216_init - initialise FAS/NCR/AMD SCSI structures.
2838 * @host: a driver-specific filled-out structure
2840 * Initialise FAS/NCR/AMD SCSI structures.
2841 * Returns: 0 on success
2843 int fas216_init(struct Scsi_Host *host)
2845 FAS216_Info *info = (FAS216_Info *)host->hostdata;
2847 info->magic_start = MAGIC;
2848 info->magic_end = MAGIC;
2850 info->scsi.cfg[0] = host->this_id | CNTL1_PERE;
2851 info->scsi.cfg[1] = CNTL2_ENF | CNTL2_S2FE;
2852 info->scsi.cfg[2] = info->ifcfg.cntl3 |
2853 CNTL3_ADIDCHK | CNTL3_QTAG | CNTL3_G2CB | CNTL3_LBTM;
2854 info->scsi.async_stp = fas216_syncperiod(info, info->ifcfg.asyncperiod);
2856 info->rst_dev_status = -1;
2857 info->rst_bus_status = -1;
2858 init_waitqueue_head(&info->eh_wait);
2859 timer_setup(&info->eh_timer, fas216_eh_timer, 0);
2861 spin_lock_init(&info->host_lock);
2863 memset(&info->stats, 0, sizeof(info->stats));
2865 msgqueue_initialise(&info->scsi.msgs);
2867 if (!queue_initialise(&info->queues.issue))
2870 if (!queue_initialise(&info->queues.disconnected)) {
2871 queue_free(&info->queues.issue);
2879 * fas216_add - initialise FAS/NCR/AMD SCSI ic.
2880 * @host: a driver-specific filled-out structure
2881 * @dev: parent device
2883 * Initialise FAS/NCR/AMD SCSI ic.
2884 * Returns: 0 on success
2886 int fas216_add(struct Scsi_Host *host, struct device *dev)
2888 FAS216_Info *info = (FAS216_Info *)host->hostdata;
2891 if (info->ifcfg.clockrate <= 10 || info->ifcfg.clockrate > 40) {
2892 printk(KERN_CRIT "fas216: invalid clock rate %u MHz\n",
2893 info->ifcfg.clockrate);
2897 fas216_reset_state(info);
2898 type = fas216_detect_type(info);
2899 info->scsi.type = chip_types[type];
2904 * Initialise the chip correctly.
2906 fas216_init_chip(info);
2909 * Reset the SCSI bus. We don't want to see
2910 * the resulting reset interrupt, so mask it
2913 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_DISR);
2914 fas216_writeb(info, REG_CMD, CMD_RESETSCSI);
2917 * scsi standard says wait 250ms
2919 spin_unlock_irq(info->host->host_lock);
2920 msleep(100*1000/100);
2921 spin_lock_irq(info->host->host_lock);
2923 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2924 fas216_readb(info, REG_INST);
2926 fas216_checkmagic(info);
2928 ret = scsi_add_host(host, dev);
2930 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2932 scsi_scan_host(host);
2937 void fas216_remove(struct Scsi_Host *host)
2939 FAS216_Info *info = (FAS216_Info *)host->hostdata;
2941 fas216_checkmagic(info);
2942 scsi_remove_host(host);
2944 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2945 scsi_host_put(host);
2949 * fas216_release - release all resources for FAS/NCR/AMD SCSI ic.
2950 * @host: a driver-specific filled-out structure
2952 * release all resources and put everything to bed for FAS/NCR/AMD SCSI ic.
2954 void fas216_release(struct Scsi_Host *host)
2956 FAS216_Info *info = (FAS216_Info *)host->hostdata;
2958 queue_free(&info->queues.disconnected);
2959 queue_free(&info->queues.issue);
2962 void fas216_print_host(FAS216_Info *info, struct seq_file *m)
2970 info->scsi.type, info->scsi.io_base,
2971 info->scsi.irq, info->scsi.dma);
2974 void fas216_print_stats(FAS216_Info *info, struct seq_file *m)
2977 "Command Statistics:\n"
2984 " Disconnects: %u\n"
2986 " Bus resets : %u\n"
2987 " Host resets: %u\n",
2988 info->stats.queues, info->stats.removes,
2989 info->stats.fins, info->stats.reads,
2990 info->stats.writes, info->stats.miscs,
2991 info->stats.disconnects, info->stats.aborts,
2992 info->stats.bus_resets, info->stats.host_resets);
2995 void fas216_print_devices(FAS216_Info *info, struct seq_file *m)
2997 struct fas216_device *dev;
2998 struct scsi_device *scd;
3000 seq_puts(m, "Device/Lun TaggedQ Parity Sync\n");
3002 shost_for_each_device(scd, info->host) {
3003 dev = &info->device[scd->id];
3004 seq_printf(m, " %d/%llu ", scd->id, scd->lun);
3005 if (scd->tagged_supported)
3006 seq_printf(m, "%3sabled(%3d) ",
3007 scd->simple_tags ? "en" : "dis",
3010 seq_puts(m, "unsupported ");
3012 seq_printf(m, "%3sabled ", dev->parity_enabled ? "en" : "dis");
3015 seq_printf(m, "offset %d, %d ns\n",
3016 dev->sof, dev->period * 4);
3018 seq_puts(m, "async\n");
3022 EXPORT_SYMBOL(fas216_init);
3023 EXPORT_SYMBOL(fas216_add);
3024 EXPORT_SYMBOL(fas216_queue_command);
3025 EXPORT_SYMBOL(fas216_noqueue_command);
3026 EXPORT_SYMBOL(fas216_intr);
3027 EXPORT_SYMBOL(fas216_remove);
3028 EXPORT_SYMBOL(fas216_release);
3029 EXPORT_SYMBOL(fas216_eh_abort);
3030 EXPORT_SYMBOL(fas216_eh_device_reset);
3031 EXPORT_SYMBOL(fas216_eh_bus_reset);
3032 EXPORT_SYMBOL(fas216_eh_host_reset);
3033 EXPORT_SYMBOL(fas216_print_host);
3034 EXPORT_SYMBOL(fas216_print_stats);
3035 EXPORT_SYMBOL(fas216_print_devices);
3037 MODULE_AUTHOR("Russell King");
3038 MODULE_DESCRIPTION("Generic FAS216/NCR53C9x driver core");
3039 MODULE_LICENSE("GPL");