1 /* esp_scsi.c: ESP SCSI driver.
3 * Copyright (C) 2007 David S. Miller (davem@davemloft.net)
6 #include <linux/kernel.h>
7 #include <linux/types.h>
8 #include <linux/slab.h>
9 #include <linux/delay.h>
10 #include <linux/list.h>
11 #include <linux/completion.h>
12 #include <linux/kallsyms.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
21 #include <scsi/scsi.h>
22 #include <scsi/scsi_host.h>
23 #include <scsi/scsi_cmnd.h>
24 #include <scsi/scsi_device.h>
25 #include <scsi/scsi_tcq.h>
26 #include <scsi/scsi_dbg.h>
27 #include <scsi/scsi_transport_spi.h>
31 #define DRV_MODULE_NAME "esp"
32 #define PFX DRV_MODULE_NAME ": "
33 #define DRV_VERSION "2.000"
34 #define DRV_MODULE_RELDATE "April 19, 2007"
36 /* SCSI bus reset settle time in seconds. */
37 static int esp_bus_reset_settle = 3;
40 #define ESP_DEBUG_INTR 0x00000001
41 #define ESP_DEBUG_SCSICMD 0x00000002
42 #define ESP_DEBUG_RESET 0x00000004
43 #define ESP_DEBUG_MSGIN 0x00000008
44 #define ESP_DEBUG_MSGOUT 0x00000010
45 #define ESP_DEBUG_CMDDONE 0x00000020
46 #define ESP_DEBUG_DISCONNECT 0x00000040
47 #define ESP_DEBUG_DATASTART 0x00000080
48 #define ESP_DEBUG_DATADONE 0x00000100
49 #define ESP_DEBUG_RECONNECT 0x00000200
50 #define ESP_DEBUG_AUTOSENSE 0x00000400
52 #define esp_log_intr(f, a...) \
53 do { if (esp_debug & ESP_DEBUG_INTR) \
57 #define esp_log_reset(f, a...) \
58 do { if (esp_debug & ESP_DEBUG_RESET) \
62 #define esp_log_msgin(f, a...) \
63 do { if (esp_debug & ESP_DEBUG_MSGIN) \
67 #define esp_log_msgout(f, a...) \
68 do { if (esp_debug & ESP_DEBUG_MSGOUT) \
72 #define esp_log_cmddone(f, a...) \
73 do { if (esp_debug & ESP_DEBUG_CMDDONE) \
77 #define esp_log_disconnect(f, a...) \
78 do { if (esp_debug & ESP_DEBUG_DISCONNECT) \
82 #define esp_log_datastart(f, a...) \
83 do { if (esp_debug & ESP_DEBUG_DATASTART) \
87 #define esp_log_datadone(f, a...) \
88 do { if (esp_debug & ESP_DEBUG_DATADONE) \
92 #define esp_log_reconnect(f, a...) \
93 do { if (esp_debug & ESP_DEBUG_RECONNECT) \
97 #define esp_log_autosense(f, a...) \
98 do { if (esp_debug & ESP_DEBUG_AUTOSENSE) \
102 #define esp_read8(REG) esp->ops->esp_read8(esp, REG)
103 #define esp_write8(VAL,REG) esp->ops->esp_write8(esp, VAL, REG)
105 static void esp_log_fill_regs(struct esp *esp,
106 struct esp_event_ent *p)
109 p->seqreg = esp->seqreg;
110 p->sreg2 = esp->sreg2;
112 p->select_state = esp->select_state;
113 p->event = esp->event;
116 void scsi_esp_cmd(struct esp *esp, u8 val)
118 struct esp_event_ent *p;
119 int idx = esp->esp_event_cur;
121 p = &esp->esp_event_log[idx];
122 p->type = ESP_EVENT_TYPE_CMD;
124 esp_log_fill_regs(esp, p);
126 esp->esp_event_cur = (idx + 1) & (ESP_EVENT_LOG_SZ - 1);
128 esp_write8(val, ESP_CMD);
130 EXPORT_SYMBOL(scsi_esp_cmd);
132 static void esp_event(struct esp *esp, u8 val)
134 struct esp_event_ent *p;
135 int idx = esp->esp_event_cur;
137 p = &esp->esp_event_log[idx];
138 p->type = ESP_EVENT_TYPE_EVENT;
140 esp_log_fill_regs(esp, p);
142 esp->esp_event_cur = (idx + 1) & (ESP_EVENT_LOG_SZ - 1);
147 static void esp_dump_cmd_log(struct esp *esp)
149 int idx = esp->esp_event_cur;
152 printk(KERN_INFO PFX "esp%d: Dumping command log\n",
153 esp->host->unique_id);
155 struct esp_event_ent *p = &esp->esp_event_log[idx];
157 printk(KERN_INFO PFX "esp%d: ent[%d] %s ",
158 esp->host->unique_id, idx,
159 p->type == ESP_EVENT_TYPE_CMD ? "CMD" : "EVENT");
161 printk("val[%02x] sreg[%02x] seqreg[%02x] "
162 "sreg2[%02x] ireg[%02x] ss[%02x] event[%02x]\n",
163 p->val, p->sreg, p->seqreg,
164 p->sreg2, p->ireg, p->select_state, p->event);
166 idx = (idx + 1) & (ESP_EVENT_LOG_SZ - 1);
167 } while (idx != stop);
170 static void esp_flush_fifo(struct esp *esp)
172 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
173 if (esp->rev == ESP236) {
176 while (esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES) {
178 printk(KERN_ALERT PFX "esp%d: ESP_FF_BYTES "
180 esp->host->unique_id);
188 static void hme_read_fifo(struct esp *esp)
190 int fcnt = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
194 esp->fifo[idx++] = esp_read8(ESP_FDATA);
195 esp->fifo[idx++] = esp_read8(ESP_FDATA);
197 if (esp->sreg2 & ESP_STAT2_F1BYTE) {
198 esp_write8(0, ESP_FDATA);
199 esp->fifo[idx++] = esp_read8(ESP_FDATA);
200 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
205 static void esp_set_all_config3(struct esp *esp, u8 val)
209 for (i = 0; i < ESP_MAX_TARGET; i++)
210 esp->target[i].esp_config3 = val;
213 /* Reset the ESP chip, _not_ the SCSI bus. */
214 static void esp_reset_esp(struct esp *esp)
216 u8 family_code, version;
218 /* Now reset the ESP chip */
219 scsi_esp_cmd(esp, ESP_CMD_RC);
220 scsi_esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
221 scsi_esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
223 /* Reload the configuration registers */
224 esp_write8(esp->cfact, ESP_CFACT);
227 esp_write8(esp->prev_stp, ESP_STP);
230 esp_write8(esp->prev_soff, ESP_SOFF);
232 esp_write8(esp->neg_defp, ESP_TIMEO);
234 /* This is the only point at which it is reliable to read
235 * the ID-code for a fast ESP chip variants.
237 esp->max_period = ((35 * esp->ccycle) / 1000);
238 if (esp->rev == FAST) {
239 version = esp_read8(ESP_UID);
240 family_code = (version & 0xf8) >> 3;
241 if (family_code == 0x02)
243 else if (family_code == 0x0a)
244 esp->rev = FASHME; /* Version is usually '5'. */
247 esp->min_period = ((4 * esp->ccycle) / 1000);
249 esp->min_period = ((5 * esp->ccycle) / 1000);
251 esp->max_period = (esp->max_period + 3)>>2;
252 esp->min_period = (esp->min_period + 3)>>2;
254 esp_write8(esp->config1, ESP_CFG1);
261 esp_write8(esp->config2, ESP_CFG2);
266 esp_write8(esp->config2, ESP_CFG2);
267 esp->prev_cfg3 = esp->target[0].esp_config3;
268 esp_write8(esp->prev_cfg3, ESP_CFG3);
272 esp->config2 |= (ESP_CONFIG2_HME32 | ESP_CONFIG2_HMEFENAB);
276 /* Fast 236 or HME */
277 esp_write8(esp->config2, ESP_CFG2);
278 if (esp->rev == FASHME) {
279 u8 cfg3 = esp->target[0].esp_config3;
281 cfg3 |= ESP_CONFIG3_FCLOCK | ESP_CONFIG3_OBPUSH;
282 if (esp->scsi_id >= 8)
283 cfg3 |= ESP_CONFIG3_IDBIT3;
284 esp_set_all_config3(esp, cfg3);
286 u32 cfg3 = esp->target[0].esp_config3;
288 cfg3 |= ESP_CONFIG3_FCLK;
289 esp_set_all_config3(esp, cfg3);
291 esp->prev_cfg3 = esp->target[0].esp_config3;
292 esp_write8(esp->prev_cfg3, ESP_CFG3);
293 if (esp->rev == FASHME) {
296 if (esp->flags & ESP_FLAG_DIFFERENTIAL)
305 esp_write8(esp->config2, ESP_CFG2);
306 esp_set_all_config3(esp,
307 (esp->target[0].esp_config3 |
308 ESP_CONFIG3_FCLOCK));
309 esp->prev_cfg3 = esp->target[0].esp_config3;
310 esp_write8(esp->prev_cfg3, ESP_CFG3);
318 /* Eat any bitrot in the chip */
319 esp_read8(ESP_INTRPT);
323 static void esp_map_dma(struct esp *esp, struct scsi_cmnd *cmd)
325 struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
326 struct scatterlist *sg = cmd->request_buffer;
327 int dir = cmd->sc_data_direction;
333 BUG_ON(cmd->use_sg == 0);
335 spriv->u.num_sg = esp->ops->map_sg(esp, sg,
337 spriv->cur_residue = sg_dma_len(sg);
341 for (i = 0; i < spriv->u.num_sg; i++)
342 total += sg_dma_len(&sg[i]);
343 spriv->tot_residue = total;
346 static dma_addr_t esp_cur_dma_addr(struct esp_cmd_entry *ent,
347 struct scsi_cmnd *cmd)
349 struct esp_cmd_priv *p = ESP_CMD_PRIV(cmd);
351 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
352 return ent->sense_dma +
353 (ent->sense_ptr - cmd->sense_buffer);
356 return sg_dma_address(p->cur_sg) +
357 (sg_dma_len(p->cur_sg) -
361 static unsigned int esp_cur_dma_len(struct esp_cmd_entry *ent,
362 struct scsi_cmnd *cmd)
364 struct esp_cmd_priv *p = ESP_CMD_PRIV(cmd);
366 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
367 return SCSI_SENSE_BUFFERSIZE -
368 (ent->sense_ptr - cmd->sense_buffer);
370 return p->cur_residue;
373 static void esp_advance_dma(struct esp *esp, struct esp_cmd_entry *ent,
374 struct scsi_cmnd *cmd, unsigned int len)
376 struct esp_cmd_priv *p = ESP_CMD_PRIV(cmd);
378 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
379 ent->sense_ptr += len;
383 p->cur_residue -= len;
384 p->tot_residue -= len;
385 if (p->cur_residue < 0 || p->tot_residue < 0) {
386 printk(KERN_ERR PFX "esp%d: Data transfer overflow.\n",
387 esp->host->unique_id);
388 printk(KERN_ERR PFX "esp%d: cur_residue[%d] tot_residue[%d] "
390 esp->host->unique_id,
391 p->cur_residue, p->tot_residue, len);
395 if (!p->cur_residue && p->tot_residue) {
397 p->cur_residue = sg_dma_len(p->cur_sg);
401 static void esp_unmap_dma(struct esp *esp, struct scsi_cmnd *cmd)
403 struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
404 int dir = cmd->sc_data_direction;
409 esp->ops->unmap_sg(esp, cmd->request_buffer,
410 spriv->u.num_sg, dir);
413 static void esp_save_pointers(struct esp *esp, struct esp_cmd_entry *ent)
415 struct scsi_cmnd *cmd = ent->cmd;
416 struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
418 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
419 ent->saved_sense_ptr = ent->sense_ptr;
422 ent->saved_cur_residue = spriv->cur_residue;
423 ent->saved_cur_sg = spriv->cur_sg;
424 ent->saved_tot_residue = spriv->tot_residue;
427 static void esp_restore_pointers(struct esp *esp, struct esp_cmd_entry *ent)
429 struct scsi_cmnd *cmd = ent->cmd;
430 struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
432 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
433 ent->sense_ptr = ent->saved_sense_ptr;
436 spriv->cur_residue = ent->saved_cur_residue;
437 spriv->cur_sg = ent->saved_cur_sg;
438 spriv->tot_residue = ent->saved_tot_residue;
441 static void esp_check_command_len(struct esp *esp, struct scsi_cmnd *cmd)
443 if (cmd->cmd_len == 6 ||
444 cmd->cmd_len == 10 ||
445 cmd->cmd_len == 12) {
446 esp->flags &= ~ESP_FLAG_DOING_SLOWCMD;
448 esp->flags |= ESP_FLAG_DOING_SLOWCMD;
452 static void esp_write_tgt_config3(struct esp *esp, int tgt)
454 if (esp->rev > ESP100A) {
455 u8 val = esp->target[tgt].esp_config3;
457 if (val != esp->prev_cfg3) {
458 esp->prev_cfg3 = val;
459 esp_write8(val, ESP_CFG3);
464 static void esp_write_tgt_sync(struct esp *esp, int tgt)
466 u8 off = esp->target[tgt].esp_offset;
467 u8 per = esp->target[tgt].esp_period;
469 if (off != esp->prev_soff) {
470 esp->prev_soff = off;
471 esp_write8(off, ESP_SOFF);
473 if (per != esp->prev_stp) {
475 esp_write8(per, ESP_STP);
479 static u32 esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len)
481 if (esp->rev == FASHME) {
482 /* Arbitrary segment boundaries, 24-bit counts. */
483 if (dma_len > (1U << 24))
484 dma_len = (1U << 24);
488 /* ESP chip limits other variants by 16-bits of transfer
489 * count. Actually on FAS100A and FAS236 we could get
490 * 24-bits of transfer count by enabling ESP_CONFIG2_FENAB
491 * in the ESP_CFG2 register but that causes other unwanted
492 * changes so we don't use it currently.
494 if (dma_len > (1U << 16))
495 dma_len = (1U << 16);
497 /* All of the DMA variants hooked up to these chips
498 * cannot handle crossing a 24-bit address boundary.
500 base = dma_addr & ((1U << 24) - 1U);
501 end = base + dma_len;
502 if (end > (1U << 24))
504 dma_len = end - base;
509 static int esp_need_to_nego_wide(struct esp_target_data *tp)
511 struct scsi_target *target = tp->starget;
513 return spi_width(target) != tp->nego_goal_width;
516 static int esp_need_to_nego_sync(struct esp_target_data *tp)
518 struct scsi_target *target = tp->starget;
520 /* When offset is zero, period is "don't care". */
521 if (!spi_offset(target) && !tp->nego_goal_offset)
524 if (spi_offset(target) == tp->nego_goal_offset &&
525 spi_period(target) == tp->nego_goal_period)
531 static int esp_alloc_lun_tag(struct esp_cmd_entry *ent,
532 struct esp_lun_data *lp)
535 /* Non-tagged, slot already taken? */
536 if (lp->non_tagged_cmd)
540 /* We are being held by active tagged
546 /* Tagged commands completed, we can unplug
547 * the queue and run this untagged command.
550 } else if (lp->num_tagged) {
551 /* Plug the queue until num_tagged decreases
552 * to zero in esp_free_lun_tag.
558 lp->non_tagged_cmd = ent;
561 /* Tagged command, see if blocked by a
564 if (lp->non_tagged_cmd || lp->hold)
568 BUG_ON(lp->tagged_cmds[ent->tag[1]]);
570 lp->tagged_cmds[ent->tag[1]] = ent;
576 static void esp_free_lun_tag(struct esp_cmd_entry *ent,
577 struct esp_lun_data *lp)
580 BUG_ON(lp->tagged_cmds[ent->tag[1]] != ent);
581 lp->tagged_cmds[ent->tag[1]] = NULL;
584 BUG_ON(lp->non_tagged_cmd != ent);
585 lp->non_tagged_cmd = NULL;
589 /* When a contingent allegiance conditon is created, we force feed a
590 * REQUEST_SENSE command to the device to fetch the sense data. I
591 * tried many other schemes, relying on the scsi error handling layer
592 * to send out the REQUEST_SENSE automatically, but this was difficult
593 * to get right especially in the presence of applications like smartd
594 * which use SG_IO to send out their own REQUEST_SENSE commands.
596 static void esp_autosense(struct esp *esp, struct esp_cmd_entry *ent)
598 struct scsi_cmnd *cmd = ent->cmd;
599 struct scsi_device *dev = cmd->device;
607 if (!ent->sense_ptr) {
608 esp_log_autosense("esp%d: Doing auto-sense for "
610 esp->host->unique_id, tgt, lun);
612 ent->sense_ptr = cmd->sense_buffer;
613 ent->sense_dma = esp->ops->map_single(esp,
615 SCSI_SENSE_BUFFERSIZE,
618 ent->saved_sense_ptr = ent->sense_ptr;
620 esp->active_cmd = ent;
622 p = esp->command_block;
623 esp->msg_out_len = 0;
625 *p++ = IDENTIFY(0, lun);
626 *p++ = REQUEST_SENSE;
627 *p++ = ((dev->scsi_level <= SCSI_2) ?
631 *p++ = SCSI_SENSE_BUFFERSIZE;
634 esp->select_state = ESP_SELECT_BASIC;
637 if (esp->rev == FASHME)
638 val |= ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT;
639 esp_write8(val, ESP_BUSID);
641 esp_write_tgt_sync(esp, tgt);
642 esp_write_tgt_config3(esp, tgt);
644 val = (p - esp->command_block);
646 if (esp->rev == FASHME)
647 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
648 esp->ops->send_dma_cmd(esp, esp->command_block_dma,
649 val, 16, 0, ESP_CMD_DMA | ESP_CMD_SELA);
652 static struct esp_cmd_entry *find_and_prep_issuable_command(struct esp *esp)
654 struct esp_cmd_entry *ent;
656 list_for_each_entry(ent, &esp->queued_cmds, list) {
657 struct scsi_cmnd *cmd = ent->cmd;
658 struct scsi_device *dev = cmd->device;
659 struct esp_lun_data *lp = dev->hostdata;
661 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
667 if (!scsi_populate_tag_msg(cmd, &ent->tag[0])) {
672 if (esp_alloc_lun_tag(ent, lp) < 0)
681 static void esp_maybe_execute_command(struct esp *esp)
683 struct esp_target_data *tp;
684 struct esp_lun_data *lp;
685 struct scsi_device *dev;
686 struct scsi_cmnd *cmd;
687 struct esp_cmd_entry *ent;
692 if (esp->active_cmd ||
693 (esp->flags & ESP_FLAG_RESETTING))
696 ent = find_and_prep_issuable_command(esp);
700 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
701 esp_autosense(esp, ent);
709 tp = &esp->target[tgt];
712 list_del(&ent->list);
713 list_add(&ent->list, &esp->active_cmds);
715 esp->active_cmd = ent;
717 esp_map_dma(esp, cmd);
718 esp_save_pointers(esp, ent);
720 esp_check_command_len(esp, cmd);
722 p = esp->command_block;
724 esp->msg_out_len = 0;
725 if (tp->flags & ESP_TGT_CHECK_NEGO) {
726 /* Need to negotiate. If the target is broken
727 * go for synchronous transfers and non-wide.
729 if (tp->flags & ESP_TGT_BROKEN) {
730 tp->flags &= ~ESP_TGT_DISCONNECT;
731 tp->nego_goal_period = 0;
732 tp->nego_goal_offset = 0;
733 tp->nego_goal_width = 0;
734 tp->nego_goal_tags = 0;
737 /* If the settings are not changing, skip this. */
738 if (spi_width(tp->starget) == tp->nego_goal_width &&
739 spi_period(tp->starget) == tp->nego_goal_period &&
740 spi_offset(tp->starget) == tp->nego_goal_offset) {
741 tp->flags &= ~ESP_TGT_CHECK_NEGO;
745 if (esp->rev == FASHME && esp_need_to_nego_wide(tp)) {
747 spi_populate_width_msg(&esp->msg_out[0],
748 (tp->nego_goal_width ?
750 tp->flags |= ESP_TGT_NEGO_WIDE;
751 } else if (esp_need_to_nego_sync(tp)) {
753 spi_populate_sync_msg(&esp->msg_out[0],
754 tp->nego_goal_period,
755 tp->nego_goal_offset);
756 tp->flags |= ESP_TGT_NEGO_SYNC;
758 tp->flags &= ~ESP_TGT_CHECK_NEGO;
761 /* Process it like a slow command. */
762 if (tp->flags & (ESP_TGT_NEGO_WIDE | ESP_TGT_NEGO_SYNC))
763 esp->flags |= ESP_FLAG_DOING_SLOWCMD;
767 /* If we don't have a lun-data struct yet, we're probing
768 * so do not disconnect. Also, do not disconnect unless
769 * we have a tag on this command.
771 if (lp && (tp->flags & ESP_TGT_DISCONNECT) && ent->tag[0])
772 *p++ = IDENTIFY(1, lun);
774 *p++ = IDENTIFY(0, lun);
776 if (ent->tag[0] && esp->rev == ESP100) {
777 /* ESP100 lacks select w/atn3 command, use select
780 esp->flags |= ESP_FLAG_DOING_SLOWCMD;
783 if (!(esp->flags & ESP_FLAG_DOING_SLOWCMD)) {
784 start_cmd = ESP_CMD_DMA | ESP_CMD_SELA;
789 start_cmd = ESP_CMD_DMA | ESP_CMD_SA3;
792 for (i = 0; i < cmd->cmd_len; i++)
795 esp->select_state = ESP_SELECT_BASIC;
797 esp->cmd_bytes_left = cmd->cmd_len;
798 esp->cmd_bytes_ptr = &cmd->cmnd[0];
801 for (i = esp->msg_out_len - 1;
803 esp->msg_out[i + 2] = esp->msg_out[i];
804 esp->msg_out[0] = ent->tag[0];
805 esp->msg_out[1] = ent->tag[1];
806 esp->msg_out_len += 2;
809 start_cmd = ESP_CMD_DMA | ESP_CMD_SELAS;
810 esp->select_state = ESP_SELECT_MSGOUT;
813 if (esp->rev == FASHME)
814 val |= ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT;
815 esp_write8(val, ESP_BUSID);
817 esp_write_tgt_sync(esp, tgt);
818 esp_write_tgt_config3(esp, tgt);
820 val = (p - esp->command_block);
822 if (esp_debug & ESP_DEBUG_SCSICMD) {
823 printk("ESP: tgt[%d] lun[%d] scsi_cmd [ ", tgt, lun);
824 for (i = 0; i < cmd->cmd_len; i++)
825 printk("%02x ", cmd->cmnd[i]);
829 if (esp->rev == FASHME)
830 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
831 esp->ops->send_dma_cmd(esp, esp->command_block_dma,
832 val, 16, 0, start_cmd);
835 static struct esp_cmd_entry *esp_get_ent(struct esp *esp)
837 struct list_head *head = &esp->esp_cmd_pool;
838 struct esp_cmd_entry *ret;
840 if (list_empty(head)) {
841 ret = kzalloc(sizeof(struct esp_cmd_entry), GFP_ATOMIC);
843 ret = list_entry(head->next, struct esp_cmd_entry, list);
844 list_del(&ret->list);
845 memset(ret, 0, sizeof(*ret));
850 static void esp_put_ent(struct esp *esp, struct esp_cmd_entry *ent)
852 list_add(&ent->list, &esp->esp_cmd_pool);
855 static void esp_cmd_is_done(struct esp *esp, struct esp_cmd_entry *ent,
856 struct scsi_cmnd *cmd, unsigned int result)
858 struct scsi_device *dev = cmd->device;
862 esp->active_cmd = NULL;
863 esp_unmap_dma(esp, cmd);
864 esp_free_lun_tag(ent, dev->hostdata);
865 cmd->result = result;
868 complete(ent->eh_done);
872 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
873 esp->ops->unmap_single(esp, ent->sense_dma,
874 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
875 ent->sense_ptr = NULL;
877 /* Restore the message/status bytes to what we actually
878 * saw originally. Also, report that we are providing
881 cmd->result = ((DRIVER_SENSE << 24) |
883 (COMMAND_COMPLETE << 8) |
884 (SAM_STAT_CHECK_CONDITION << 0));
886 ent->flags &= ~ESP_CMD_FLAG_AUTOSENSE;
887 if (esp_debug & ESP_DEBUG_AUTOSENSE) {
890 printk("esp%d: tgt[%d] lun[%d] AUTO SENSE[ ",
891 esp->host->unique_id, tgt, lun);
892 for (i = 0; i < 18; i++)
893 printk("%02x ", cmd->sense_buffer[i]);
900 list_del(&ent->list);
901 esp_put_ent(esp, ent);
903 esp_maybe_execute_command(esp);
906 static unsigned int compose_result(unsigned int status, unsigned int message,
907 unsigned int driver_code)
909 return (status | (message << 8) | (driver_code << 16));
912 static void esp_event_queue_full(struct esp *esp, struct esp_cmd_entry *ent)
914 struct scsi_device *dev = ent->cmd->device;
915 struct esp_lun_data *lp = dev->hostdata;
917 scsi_track_queue_full(dev, lp->num_tagged - 1);
920 static int esp_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
922 struct scsi_device *dev = cmd->device;
923 struct esp *esp = host_to_esp(dev->host);
924 struct esp_cmd_priv *spriv;
925 struct esp_cmd_entry *ent;
927 ent = esp_get_ent(esp);
929 return SCSI_MLQUEUE_HOST_BUSY;
933 cmd->scsi_done = done;
935 spriv = ESP_CMD_PRIV(cmd);
936 spriv->u.dma_addr = ~(dma_addr_t)0x0;
938 list_add_tail(&ent->list, &esp->queued_cmds);
940 esp_maybe_execute_command(esp);
945 static int esp_check_gross_error(struct esp *esp)
947 if (esp->sreg & ESP_STAT_SPAM) {
948 /* Gross Error, could be one of:
949 * - top of fifo overwritten
950 * - top of command register overwritten
951 * - DMA programmed with wrong direction
952 * - improper phase change
954 printk(KERN_ERR PFX "esp%d: Gross error sreg[%02x]\n",
955 esp->host->unique_id, esp->sreg);
956 /* XXX Reset the chip. XXX */
962 static int esp_check_spur_intr(struct esp *esp)
967 /* The interrupt pending bit of the status register cannot
968 * be trusted on these revisions.
970 esp->sreg &= ~ESP_STAT_INTR;
974 if (!(esp->sreg & ESP_STAT_INTR)) {
975 esp->ireg = esp_read8(ESP_INTRPT);
976 if (esp->ireg & ESP_INTR_SR)
979 /* If the DMA is indicating interrupt pending and the
980 * ESP is not, the only possibility is a DMA error.
982 if (!esp->ops->dma_error(esp)) {
983 printk(KERN_ERR PFX "esp%d: Spurious irq, "
985 esp->host->unique_id, esp->sreg);
989 printk(KERN_ERR PFX "esp%d: DMA error\n",
990 esp->host->unique_id);
992 /* XXX Reset the chip. XXX */
1001 static void esp_schedule_reset(struct esp *esp)
1003 esp_log_reset("ESP: esp_schedule_reset() from %p\n",
1004 __builtin_return_address(0));
1005 esp->flags |= ESP_FLAG_RESETTING;
1006 esp_event(esp, ESP_EVENT_RESET);
1009 /* In order to avoid having to add a special half-reconnected state
1010 * into the driver we just sit here and poll through the rest of
1011 * the reselection process to get the tag message bytes.
1013 static struct esp_cmd_entry *esp_reconnect_with_tag(struct esp *esp,
1014 struct esp_lun_data *lp)
1016 struct esp_cmd_entry *ent;
1019 if (!lp->num_tagged) {
1020 printk(KERN_ERR PFX "esp%d: Reconnect w/num_tagged==0\n",
1021 esp->host->unique_id);
1025 esp_log_reconnect("ESP: reconnect tag, ");
1027 for (i = 0; i < ESP_QUICKIRQ_LIMIT; i++) {
1028 if (esp->ops->irq_pending(esp))
1031 if (i == ESP_QUICKIRQ_LIMIT) {
1032 printk(KERN_ERR PFX "esp%d: Reconnect IRQ1 timeout\n",
1033 esp->host->unique_id);
1037 esp->sreg = esp_read8(ESP_STATUS);
1038 esp->ireg = esp_read8(ESP_INTRPT);
1040 esp_log_reconnect("IRQ(%d:%x:%x), ",
1041 i, esp->ireg, esp->sreg);
1043 if (esp->ireg & ESP_INTR_DC) {
1044 printk(KERN_ERR PFX "esp%d: Reconnect, got disconnect.\n",
1045 esp->host->unique_id);
1049 if ((esp->sreg & ESP_STAT_PMASK) != ESP_MIP) {
1050 printk(KERN_ERR PFX "esp%d: Reconnect, not MIP sreg[%02x].\n",
1051 esp->host->unique_id, esp->sreg);
1055 /* DMA in the tag bytes... */
1056 esp->command_block[0] = 0xff;
1057 esp->command_block[1] = 0xff;
1058 esp->ops->send_dma_cmd(esp, esp->command_block_dma,
1059 2, 2, 1, ESP_CMD_DMA | ESP_CMD_TI);
1061 /* ACK the msssage. */
1062 scsi_esp_cmd(esp, ESP_CMD_MOK);
1064 for (i = 0; i < ESP_RESELECT_TAG_LIMIT; i++) {
1065 if (esp->ops->irq_pending(esp)) {
1066 esp->sreg = esp_read8(ESP_STATUS);
1067 esp->ireg = esp_read8(ESP_INTRPT);
1068 if (esp->ireg & ESP_INTR_FDONE)
1073 if (i == ESP_RESELECT_TAG_LIMIT) {
1074 printk(KERN_ERR PFX "esp%d: Reconnect IRQ2 timeout\n",
1075 esp->host->unique_id);
1078 esp->ops->dma_drain(esp);
1079 esp->ops->dma_invalidate(esp);
1081 esp_log_reconnect("IRQ2(%d:%x:%x) tag[%x:%x]\n",
1082 i, esp->ireg, esp->sreg,
1083 esp->command_block[0],
1084 esp->command_block[1]);
1086 if (esp->command_block[0] < SIMPLE_QUEUE_TAG ||
1087 esp->command_block[0] > ORDERED_QUEUE_TAG) {
1088 printk(KERN_ERR PFX "esp%d: Reconnect, bad tag "
1090 esp->host->unique_id, esp->command_block[0]);
1094 ent = lp->tagged_cmds[esp->command_block[1]];
1096 printk(KERN_ERR PFX "esp%d: Reconnect, no entry for "
1098 esp->host->unique_id, esp->command_block[1]);
1105 static int esp_reconnect(struct esp *esp)
1107 struct esp_cmd_entry *ent;
1108 struct esp_target_data *tp;
1109 struct esp_lun_data *lp;
1110 struct scsi_device *dev;
1113 BUG_ON(esp->active_cmd);
1114 if (esp->rev == FASHME) {
1115 /* FASHME puts the target and lun numbers directly
1118 target = esp->fifo[0];
1119 lun = esp->fifo[1] & 0x7;
1121 u8 bits = esp_read8(ESP_FDATA);
1123 /* Older chips put the lun directly into the fifo, but
1124 * the target is given as a sample of the arbitration
1125 * lines on the bus at reselection time. So we should
1126 * see the ID of the ESP and the one reconnecting target
1127 * set in the bitmap.
1129 if (!(bits & esp->scsi_id_mask))
1131 bits &= ~esp->scsi_id_mask;
1132 if (!bits || (bits & (bits - 1)))
1135 target = ffs(bits) - 1;
1136 lun = (esp_read8(ESP_FDATA) & 0x7);
1138 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1139 if (esp->rev == ESP100) {
1140 u8 ireg = esp_read8(ESP_INTRPT);
1141 /* This chip has a bug during reselection that can
1142 * cause a spurious illegal-command interrupt, which
1143 * we simply ACK here. Another possibility is a bus
1144 * reset so we must check for that.
1146 if (ireg & ESP_INTR_SR)
1149 scsi_esp_cmd(esp, ESP_CMD_NULL);
1152 esp_write_tgt_sync(esp, target);
1153 esp_write_tgt_config3(esp, target);
1155 scsi_esp_cmd(esp, ESP_CMD_MOK);
1157 if (esp->rev == FASHME)
1158 esp_write8(target | ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT,
1161 tp = &esp->target[target];
1162 dev = __scsi_device_lookup_by_target(tp->starget, lun);
1164 printk(KERN_ERR PFX "esp%d: Reconnect, no lp "
1165 "tgt[%u] lun[%u]\n",
1166 esp->host->unique_id, target, lun);
1171 ent = lp->non_tagged_cmd;
1173 ent = esp_reconnect_with_tag(esp, lp);
1178 esp->active_cmd = ent;
1180 if (ent->flags & ESP_CMD_FLAG_ABORT) {
1181 esp->msg_out[0] = ABORT_TASK_SET;
1182 esp->msg_out_len = 1;
1183 scsi_esp_cmd(esp, ESP_CMD_SATN);
1186 esp_event(esp, ESP_EVENT_CHECK_PHASE);
1187 esp_restore_pointers(esp, ent);
1188 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1192 esp_schedule_reset(esp);
1196 static int esp_finish_select(struct esp *esp)
1198 struct esp_cmd_entry *ent;
1199 struct scsi_cmnd *cmd;
1200 u8 orig_select_state;
1202 orig_select_state = esp->select_state;
1204 /* No longer selecting. */
1205 esp->select_state = ESP_SELECT_NONE;
1207 esp->seqreg = esp_read8(ESP_SSTEP) & ESP_STEP_VBITS;
1208 ent = esp->active_cmd;
1211 if (esp->ops->dma_error(esp)) {
1212 /* If we see a DMA error during or as a result of selection,
1215 esp_schedule_reset(esp);
1216 esp_cmd_is_done(esp, ent, cmd, (DID_ERROR << 16));
1220 esp->ops->dma_invalidate(esp);
1222 if (esp->ireg == (ESP_INTR_RSEL | ESP_INTR_FDONE)) {
1223 struct esp_target_data *tp = &esp->target[cmd->device->id];
1225 /* Carefully back out of the selection attempt. Release
1226 * resources (such as DMA mapping & TAG) and reset state (such
1227 * as message out and command delivery variables).
1229 if (!(ent->flags & ESP_CMD_FLAG_AUTOSENSE)) {
1230 esp_unmap_dma(esp, cmd);
1231 esp_free_lun_tag(ent, cmd->device->hostdata);
1232 tp->flags &= ~(ESP_TGT_NEGO_SYNC | ESP_TGT_NEGO_WIDE);
1233 esp->flags &= ~ESP_FLAG_DOING_SLOWCMD;
1234 esp->cmd_bytes_ptr = NULL;
1235 esp->cmd_bytes_left = 0;
1237 esp->ops->unmap_single(esp, ent->sense_dma,
1238 SCSI_SENSE_BUFFERSIZE,
1240 ent->sense_ptr = NULL;
1243 /* Now that the state is unwound properly, put back onto
1244 * the issue queue. This command is no longer active.
1246 list_del(&ent->list);
1247 list_add(&ent->list, &esp->queued_cmds);
1248 esp->active_cmd = NULL;
1250 /* Return value ignored by caller, it directly invokes
1256 if (esp->ireg == ESP_INTR_DC) {
1257 struct scsi_device *dev = cmd->device;
1259 /* Disconnect. Make sure we re-negotiate sync and
1260 * wide parameters if this target starts responding
1261 * again in the future.
1263 esp->target[dev->id].flags |= ESP_TGT_CHECK_NEGO;
1265 scsi_esp_cmd(esp, ESP_CMD_ESEL);
1266 esp_cmd_is_done(esp, ent, cmd, (DID_BAD_TARGET << 16));
1270 if (esp->ireg == (ESP_INTR_FDONE | ESP_INTR_BSERV)) {
1271 /* Selection successful. On pre-FAST chips we have
1272 * to do a NOP and possibly clean out the FIFO.
1274 if (esp->rev <= ESP236) {
1275 int fcnt = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
1277 scsi_esp_cmd(esp, ESP_CMD_NULL);
1281 ((esp->sreg & ESP_STAT_PMASK) != ESP_DIP)))
1282 esp_flush_fifo(esp);
1285 /* If we are doing a slow command, negotiation, etc.
1286 * we'll do the right thing as we transition to the
1289 esp_event(esp, ESP_EVENT_CHECK_PHASE);
1293 printk("ESP: Unexpected selection completion ireg[%x].\n",
1295 esp_schedule_reset(esp);
1299 static int esp_data_bytes_sent(struct esp *esp, struct esp_cmd_entry *ent,
1300 struct scsi_cmnd *cmd)
1302 int fifo_cnt, ecount, bytes_sent, flush_fifo;
1304 fifo_cnt = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
1305 if (esp->prev_cfg3 & ESP_CONFIG3_EWIDE)
1309 if (!(esp->sreg & ESP_STAT_TCNT)) {
1310 ecount = ((unsigned int)esp_read8(ESP_TCLOW) |
1311 (((unsigned int)esp_read8(ESP_TCMED)) << 8));
1312 if (esp->rev == FASHME)
1313 ecount |= ((unsigned int)esp_read8(FAS_RLO)) << 16;
1316 bytes_sent = esp->data_dma_len;
1317 bytes_sent -= ecount;
1319 if (!(ent->flags & ESP_CMD_FLAG_WRITE))
1320 bytes_sent -= fifo_cnt;
1323 if (!esp->prev_soff) {
1324 /* Synchronous data transfer, always flush fifo. */
1327 if (esp->rev == ESP100) {
1330 /* ESP100 has a chip bug where in the synchronous data
1331 * phase it can mistake a final long REQ pulse from the
1332 * target as an extra data byte. Fun.
1334 * To detect this case we resample the status register
1335 * and fifo flags. If we're still in a data phase and
1336 * we see spurious chunks in the fifo, we return error
1337 * to the caller which should reset and set things up
1338 * such that we only try future transfers to this
1339 * target in synchronous mode.
1341 esp->sreg = esp_read8(ESP_STATUS);
1342 phase = esp->sreg & ESP_STAT_PMASK;
1343 fflags = esp_read8(ESP_FFLAGS);
1345 if ((phase == ESP_DOP &&
1346 (fflags & ESP_FF_ONOTZERO)) ||
1347 (phase == ESP_DIP &&
1348 (fflags & ESP_FF_FBYTES)))
1351 if (!(ent->flags & ESP_CMD_FLAG_WRITE))
1356 esp_flush_fifo(esp);
1361 static void esp_setsync(struct esp *esp, struct esp_target_data *tp,
1362 u8 scsi_period, u8 scsi_offset,
1363 u8 esp_stp, u8 esp_soff)
1365 spi_period(tp->starget) = scsi_period;
1366 spi_offset(tp->starget) = scsi_offset;
1367 spi_width(tp->starget) = (tp->flags & ESP_TGT_WIDE) ? 1 : 0;
1371 esp_soff |= esp->radelay;
1372 if (esp->rev >= FAS236) {
1373 u8 bit = ESP_CONFIG3_FSCSI;
1374 if (esp->rev >= FAS100A)
1375 bit = ESP_CONFIG3_FAST;
1377 if (scsi_period < 50) {
1378 if (esp->rev == FASHME)
1379 esp_soff &= ~esp->radelay;
1380 tp->esp_config3 |= bit;
1382 tp->esp_config3 &= ~bit;
1384 esp->prev_cfg3 = tp->esp_config3;
1385 esp_write8(esp->prev_cfg3, ESP_CFG3);
1389 tp->esp_period = esp->prev_stp = esp_stp;
1390 tp->esp_offset = esp->prev_soff = esp_soff;
1392 esp_write8(esp_soff, ESP_SOFF);
1393 esp_write8(esp_stp, ESP_STP);
1395 tp->flags &= ~(ESP_TGT_NEGO_SYNC | ESP_TGT_CHECK_NEGO);
1397 spi_display_xfer_agreement(tp->starget);
1400 static void esp_msgin_reject(struct esp *esp)
1402 struct esp_cmd_entry *ent = esp->active_cmd;
1403 struct scsi_cmnd *cmd = ent->cmd;
1404 struct esp_target_data *tp;
1407 tgt = cmd->device->id;
1408 tp = &esp->target[tgt];
1410 if (tp->flags & ESP_TGT_NEGO_WIDE) {
1411 tp->flags &= ~(ESP_TGT_NEGO_WIDE | ESP_TGT_WIDE);
1413 if (!esp_need_to_nego_sync(tp)) {
1414 tp->flags &= ~ESP_TGT_CHECK_NEGO;
1415 scsi_esp_cmd(esp, ESP_CMD_RATN);
1418 spi_populate_sync_msg(&esp->msg_out[0],
1419 tp->nego_goal_period,
1420 tp->nego_goal_offset);
1421 tp->flags |= ESP_TGT_NEGO_SYNC;
1422 scsi_esp_cmd(esp, ESP_CMD_SATN);
1427 if (tp->flags & ESP_TGT_NEGO_SYNC) {
1428 tp->flags &= ~(ESP_TGT_NEGO_SYNC | ESP_TGT_CHECK_NEGO);
1431 esp_setsync(esp, tp, 0, 0, 0, 0);
1432 scsi_esp_cmd(esp, ESP_CMD_RATN);
1436 esp->msg_out[0] = ABORT_TASK_SET;
1437 esp->msg_out_len = 1;
1438 scsi_esp_cmd(esp, ESP_CMD_SATN);
1441 static void esp_msgin_sdtr(struct esp *esp, struct esp_target_data *tp)
1443 u8 period = esp->msg_in[3];
1444 u8 offset = esp->msg_in[4];
1447 if (!(tp->flags & ESP_TGT_NEGO_SYNC))
1454 int rounded_up, one_clock;
1456 if (period > esp->max_period) {
1457 period = offset = 0;
1460 if (period < esp->min_period)
1463 one_clock = esp->ccycle / 1000;
1464 rounded_up = (period << 2);
1465 rounded_up = (rounded_up + one_clock - 1) / one_clock;
1467 if (stp && esp->rev >= FAS236) {
1475 esp_setsync(esp, tp, period, offset, stp, offset);
1479 esp->msg_out[0] = MESSAGE_REJECT;
1480 esp->msg_out_len = 1;
1481 scsi_esp_cmd(esp, ESP_CMD_SATN);
1485 tp->nego_goal_period = period;
1486 tp->nego_goal_offset = offset;
1488 spi_populate_sync_msg(&esp->msg_out[0],
1489 tp->nego_goal_period,
1490 tp->nego_goal_offset);
1491 scsi_esp_cmd(esp, ESP_CMD_SATN);
1494 static void esp_msgin_wdtr(struct esp *esp, struct esp_target_data *tp)
1496 int size = 8 << esp->msg_in[3];
1499 if (esp->rev != FASHME)
1502 if (size != 8 && size != 16)
1505 if (!(tp->flags & ESP_TGT_NEGO_WIDE))
1508 cfg3 = tp->esp_config3;
1510 tp->flags |= ESP_TGT_WIDE;
1511 cfg3 |= ESP_CONFIG3_EWIDE;
1513 tp->flags &= ~ESP_TGT_WIDE;
1514 cfg3 &= ~ESP_CONFIG3_EWIDE;
1516 tp->esp_config3 = cfg3;
1517 esp->prev_cfg3 = cfg3;
1518 esp_write8(cfg3, ESP_CFG3);
1520 tp->flags &= ~ESP_TGT_NEGO_WIDE;
1522 spi_period(tp->starget) = 0;
1523 spi_offset(tp->starget) = 0;
1524 if (!esp_need_to_nego_sync(tp)) {
1525 tp->flags &= ~ESP_TGT_CHECK_NEGO;
1526 scsi_esp_cmd(esp, ESP_CMD_RATN);
1529 spi_populate_sync_msg(&esp->msg_out[0],
1530 tp->nego_goal_period,
1531 tp->nego_goal_offset);
1532 tp->flags |= ESP_TGT_NEGO_SYNC;
1533 scsi_esp_cmd(esp, ESP_CMD_SATN);
1538 esp->msg_out[0] = MESSAGE_REJECT;
1539 esp->msg_out_len = 1;
1540 scsi_esp_cmd(esp, ESP_CMD_SATN);
1543 static void esp_msgin_extended(struct esp *esp)
1545 struct esp_cmd_entry *ent = esp->active_cmd;
1546 struct scsi_cmnd *cmd = ent->cmd;
1547 struct esp_target_data *tp;
1548 int tgt = cmd->device->id;
1550 tp = &esp->target[tgt];
1551 if (esp->msg_in[2] == EXTENDED_SDTR) {
1552 esp_msgin_sdtr(esp, tp);
1555 if (esp->msg_in[2] == EXTENDED_WDTR) {
1556 esp_msgin_wdtr(esp, tp);
1560 printk("ESP: Unexpected extended msg type %x\n",
1563 esp->msg_out[0] = ABORT_TASK_SET;
1564 esp->msg_out_len = 1;
1565 scsi_esp_cmd(esp, ESP_CMD_SATN);
1568 /* Analyze msgin bytes received from target so far. Return non-zero
1569 * if there are more bytes needed to complete the message.
1571 static int esp_msgin_process(struct esp *esp)
1573 u8 msg0 = esp->msg_in[0];
1574 int len = esp->msg_in_len;
1578 printk("ESP: Unexpected msgin identify\n");
1583 case EXTENDED_MESSAGE:
1586 if (len < esp->msg_in[1] + 2)
1588 esp_msgin_extended(esp);
1591 case IGNORE_WIDE_RESIDUE: {
1592 struct esp_cmd_entry *ent;
1593 struct esp_cmd_priv *spriv;
1597 if (esp->msg_in[1] != 1)
1600 ent = esp->active_cmd;
1601 spriv = ESP_CMD_PRIV(ent->cmd);
1603 if (spriv->cur_residue == sg_dma_len(spriv->cur_sg)) {
1605 spriv->cur_residue = 1;
1607 spriv->cur_residue++;
1608 spriv->tot_residue++;
1613 case RESTORE_POINTERS:
1614 esp_restore_pointers(esp, esp->active_cmd);
1617 esp_save_pointers(esp, esp->active_cmd);
1620 case COMMAND_COMPLETE:
1622 struct esp_cmd_entry *ent = esp->active_cmd;
1624 ent->message = msg0;
1625 esp_event(esp, ESP_EVENT_FREE_BUS);
1626 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1629 case MESSAGE_REJECT:
1630 esp_msgin_reject(esp);
1635 esp->msg_out[0] = MESSAGE_REJECT;
1636 esp->msg_out_len = 1;
1637 scsi_esp_cmd(esp, ESP_CMD_SATN);
1642 static int esp_process_event(struct esp *esp)
1648 switch (esp->event) {
1649 case ESP_EVENT_CHECK_PHASE:
1650 switch (esp->sreg & ESP_STAT_PMASK) {
1652 esp_event(esp, ESP_EVENT_DATA_OUT);
1655 esp_event(esp, ESP_EVENT_DATA_IN);
1658 esp_flush_fifo(esp);
1659 scsi_esp_cmd(esp, ESP_CMD_ICCSEQ);
1660 esp_event(esp, ESP_EVENT_STATUS);
1661 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1665 esp_event(esp, ESP_EVENT_MSGOUT);
1669 esp_event(esp, ESP_EVENT_MSGIN);
1673 esp_event(esp, ESP_EVENT_CMD_START);
1677 printk("ESP: Unexpected phase, sreg=%02x\n",
1679 esp_schedule_reset(esp);
1685 case ESP_EVENT_DATA_IN:
1689 case ESP_EVENT_DATA_OUT: {
1690 struct esp_cmd_entry *ent = esp->active_cmd;
1691 struct scsi_cmnd *cmd = ent->cmd;
1692 dma_addr_t dma_addr = esp_cur_dma_addr(ent, cmd);
1693 unsigned int dma_len = esp_cur_dma_len(ent, cmd);
1695 if (esp->rev == ESP100)
1696 scsi_esp_cmd(esp, ESP_CMD_NULL);
1699 ent->flags |= ESP_CMD_FLAG_WRITE;
1701 ent->flags &= ~ESP_CMD_FLAG_WRITE;
1703 dma_len = esp_dma_length_limit(esp, dma_addr, dma_len);
1704 esp->data_dma_len = dma_len;
1707 printk(KERN_ERR PFX "esp%d: DMA length is zero!\n",
1708 esp->host->unique_id);
1709 printk(KERN_ERR PFX "esp%d: cur adr[%08x] len[%08x]\n",
1710 esp->host->unique_id,
1711 esp_cur_dma_addr(ent, cmd),
1712 esp_cur_dma_len(ent, cmd));
1713 esp_schedule_reset(esp);
1717 esp_log_datastart("ESP: start data addr[%08x] len[%u] "
1719 dma_addr, dma_len, write);
1721 esp->ops->send_dma_cmd(esp, dma_addr, dma_len, dma_len,
1722 write, ESP_CMD_DMA | ESP_CMD_TI);
1723 esp_event(esp, ESP_EVENT_DATA_DONE);
1726 case ESP_EVENT_DATA_DONE: {
1727 struct esp_cmd_entry *ent = esp->active_cmd;
1728 struct scsi_cmnd *cmd = ent->cmd;
1731 if (esp->ops->dma_error(esp)) {
1732 printk("ESP: data done, DMA error, resetting\n");
1733 esp_schedule_reset(esp);
1737 if (ent->flags & ESP_CMD_FLAG_WRITE) {
1738 /* XXX parity errors, etc. XXX */
1740 esp->ops->dma_drain(esp);
1742 esp->ops->dma_invalidate(esp);
1744 if (esp->ireg != ESP_INTR_BSERV) {
1745 /* We should always see exactly a bus-service
1746 * interrupt at the end of a successful transfer.
1748 printk("ESP: data done, not BSERV, resetting\n");
1749 esp_schedule_reset(esp);
1753 bytes_sent = esp_data_bytes_sent(esp, ent, cmd);
1755 esp_log_datadone("ESP: data done flgs[%x] sent[%d]\n",
1756 ent->flags, bytes_sent);
1758 if (bytes_sent < 0) {
1759 /* XXX force sync mode for this target XXX */
1760 esp_schedule_reset(esp);
1764 esp_advance_dma(esp, ent, cmd, bytes_sent);
1765 esp_event(esp, ESP_EVENT_CHECK_PHASE);
1770 case ESP_EVENT_STATUS: {
1771 struct esp_cmd_entry *ent = esp->active_cmd;
1773 if (esp->ireg & ESP_INTR_FDONE) {
1774 ent->status = esp_read8(ESP_FDATA);
1775 ent->message = esp_read8(ESP_FDATA);
1776 scsi_esp_cmd(esp, ESP_CMD_MOK);
1777 } else if (esp->ireg == ESP_INTR_BSERV) {
1778 ent->status = esp_read8(ESP_FDATA);
1779 ent->message = 0xff;
1780 esp_event(esp, ESP_EVENT_MSGIN);
1784 if (ent->message != COMMAND_COMPLETE) {
1785 printk("ESP: Unexpected message %x in status\n",
1787 esp_schedule_reset(esp);
1791 esp_event(esp, ESP_EVENT_FREE_BUS);
1792 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1795 case ESP_EVENT_FREE_BUS: {
1796 struct esp_cmd_entry *ent = esp->active_cmd;
1797 struct scsi_cmnd *cmd = ent->cmd;
1799 if (ent->message == COMMAND_COMPLETE ||
1800 ent->message == DISCONNECT)
1801 scsi_esp_cmd(esp, ESP_CMD_ESEL);
1803 if (ent->message == COMMAND_COMPLETE) {
1804 esp_log_cmddone("ESP: Command done status[%x] "
1806 ent->status, ent->message);
1807 if (ent->status == SAM_STAT_TASK_SET_FULL)
1808 esp_event_queue_full(esp, ent);
1810 if (ent->status == SAM_STAT_CHECK_CONDITION &&
1811 !(ent->flags & ESP_CMD_FLAG_AUTOSENSE)) {
1812 ent->flags |= ESP_CMD_FLAG_AUTOSENSE;
1813 esp_autosense(esp, ent);
1815 esp_cmd_is_done(esp, ent, cmd,
1816 compose_result(ent->status,
1820 } else if (ent->message == DISCONNECT) {
1821 esp_log_disconnect("ESP: Disconnecting tgt[%d] "
1824 ent->tag[0], ent->tag[1]);
1826 esp->active_cmd = NULL;
1827 esp_maybe_execute_command(esp);
1829 printk("ESP: Unexpected message %x in freebus\n",
1831 esp_schedule_reset(esp);
1834 if (esp->active_cmd)
1835 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1838 case ESP_EVENT_MSGOUT: {
1839 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1841 if (esp_debug & ESP_DEBUG_MSGOUT) {
1843 printk("ESP: Sending message [ ");
1844 for (i = 0; i < esp->msg_out_len; i++)
1845 printk("%02x ", esp->msg_out[i]);
1849 if (esp->rev == FASHME) {
1852 /* Always use the fifo. */
1853 for (i = 0; i < esp->msg_out_len; i++) {
1854 esp_write8(esp->msg_out[i], ESP_FDATA);
1855 esp_write8(0, ESP_FDATA);
1857 scsi_esp_cmd(esp, ESP_CMD_TI);
1859 if (esp->msg_out_len == 1) {
1860 esp_write8(esp->msg_out[0], ESP_FDATA);
1861 scsi_esp_cmd(esp, ESP_CMD_TI);
1864 memcpy(esp->command_block,
1868 esp->ops->send_dma_cmd(esp,
1869 esp->command_block_dma,
1873 ESP_CMD_DMA|ESP_CMD_TI);
1876 esp_event(esp, ESP_EVENT_MSGOUT_DONE);
1879 case ESP_EVENT_MSGOUT_DONE:
1880 if (esp->rev == FASHME) {
1881 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1883 if (esp->msg_out_len > 1)
1884 esp->ops->dma_invalidate(esp);
1887 if (!(esp->ireg & ESP_INTR_DC)) {
1888 if (esp->rev != FASHME)
1889 scsi_esp_cmd(esp, ESP_CMD_NULL);
1891 esp_event(esp, ESP_EVENT_CHECK_PHASE);
1893 case ESP_EVENT_MSGIN:
1894 if (esp->ireg & ESP_INTR_BSERV) {
1895 if (esp->rev == FASHME) {
1896 if (!(esp_read8(ESP_STATUS2) &
1898 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1900 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1901 if (esp->rev == ESP100)
1902 scsi_esp_cmd(esp, ESP_CMD_NULL);
1904 scsi_esp_cmd(esp, ESP_CMD_TI);
1905 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1908 if (esp->ireg & ESP_INTR_FDONE) {
1911 if (esp->rev == FASHME)
1914 val = esp_read8(ESP_FDATA);
1915 esp->msg_in[esp->msg_in_len++] = val;
1917 esp_log_msgin("ESP: Got msgin byte %x\n", val);
1919 if (!esp_msgin_process(esp))
1920 esp->msg_in_len = 0;
1922 if (esp->rev == FASHME)
1923 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1925 scsi_esp_cmd(esp, ESP_CMD_MOK);
1927 if (esp->event != ESP_EVENT_FREE_BUS)
1928 esp_event(esp, ESP_EVENT_CHECK_PHASE);
1930 printk("ESP: MSGIN neither BSERV not FDON, resetting");
1931 esp_schedule_reset(esp);
1935 case ESP_EVENT_CMD_START:
1936 memcpy(esp->command_block, esp->cmd_bytes_ptr,
1937 esp->cmd_bytes_left);
1938 if (esp->rev == FASHME)
1939 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1940 esp->ops->send_dma_cmd(esp, esp->command_block_dma,
1941 esp->cmd_bytes_left, 16, 0,
1942 ESP_CMD_DMA | ESP_CMD_TI);
1943 esp_event(esp, ESP_EVENT_CMD_DONE);
1944 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1946 case ESP_EVENT_CMD_DONE:
1947 esp->ops->dma_invalidate(esp);
1948 if (esp->ireg & ESP_INTR_BSERV) {
1949 esp_event(esp, ESP_EVENT_CHECK_PHASE);
1952 esp_schedule_reset(esp);
1956 case ESP_EVENT_RESET:
1957 scsi_esp_cmd(esp, ESP_CMD_RS);
1961 printk("ESP: Unexpected event %x, resetting\n",
1963 esp_schedule_reset(esp);
1970 static void esp_reset_cleanup_one(struct esp *esp, struct esp_cmd_entry *ent)
1972 struct scsi_cmnd *cmd = ent->cmd;
1974 esp_unmap_dma(esp, cmd);
1975 esp_free_lun_tag(ent, cmd->device->hostdata);
1976 cmd->result = DID_RESET << 16;
1978 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
1979 esp->ops->unmap_single(esp, ent->sense_dma,
1980 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
1981 ent->sense_ptr = NULL;
1984 cmd->scsi_done(cmd);
1985 list_del(&ent->list);
1986 esp_put_ent(esp, ent);
1989 static void esp_clear_hold(struct scsi_device *dev, void *data)
1991 struct esp_lun_data *lp = dev->hostdata;
1993 BUG_ON(lp->num_tagged);
1997 static void esp_reset_cleanup(struct esp *esp)
1999 struct esp_cmd_entry *ent, *tmp;
2002 list_for_each_entry_safe(ent, tmp, &esp->queued_cmds, list) {
2003 struct scsi_cmnd *cmd = ent->cmd;
2005 list_del(&ent->list);
2006 cmd->result = DID_RESET << 16;
2007 cmd->scsi_done(cmd);
2008 esp_put_ent(esp, ent);
2011 list_for_each_entry_safe(ent, tmp, &esp->active_cmds, list) {
2012 if (ent == esp->active_cmd)
2013 esp->active_cmd = NULL;
2014 esp_reset_cleanup_one(esp, ent);
2017 BUG_ON(esp->active_cmd != NULL);
2019 /* Force renegotiation of sync/wide transfers. */
2020 for (i = 0; i < ESP_MAX_TARGET; i++) {
2021 struct esp_target_data *tp = &esp->target[i];
2025 tp->esp_config3 &= ~(ESP_CONFIG3_EWIDE |
2028 tp->flags &= ~ESP_TGT_WIDE;
2029 tp->flags |= ESP_TGT_CHECK_NEGO;
2032 starget_for_each_device(tp->starget, NULL,
2037 /* Runs under host->lock */
2038 static void __esp_interrupt(struct esp *esp)
2040 int finish_reset, intr_done;
2043 esp->sreg = esp_read8(ESP_STATUS);
2045 if (esp->flags & ESP_FLAG_RESETTING) {
2048 if (esp_check_gross_error(esp))
2051 finish_reset = esp_check_spur_intr(esp);
2052 if (finish_reset < 0)
2056 esp->ireg = esp_read8(ESP_INTRPT);
2058 if (esp->ireg & ESP_INTR_SR)
2062 esp_reset_cleanup(esp);
2063 if (esp->eh_reset) {
2064 complete(esp->eh_reset);
2065 esp->eh_reset = NULL;
2070 phase = (esp->sreg & ESP_STAT_PMASK);
2071 if (esp->rev == FASHME) {
2072 if (((phase != ESP_DIP && phase != ESP_DOP) &&
2073 esp->select_state == ESP_SELECT_NONE &&
2074 esp->event != ESP_EVENT_STATUS &&
2075 esp->event != ESP_EVENT_DATA_DONE) ||
2076 (esp->ireg & ESP_INTR_RSEL)) {
2077 esp->sreg2 = esp_read8(ESP_STATUS2);
2078 if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
2079 (esp->sreg2 & ESP_STAT2_F1BYTE))
2084 esp_log_intr("ESP: intr sreg[%02x] seqreg[%02x] "
2085 "sreg2[%02x] ireg[%02x]\n",
2086 esp->sreg, esp->seqreg, esp->sreg2, esp->ireg);
2090 if (esp->ireg & (ESP_INTR_S | ESP_INTR_SATN | ESP_INTR_IC)) {
2091 printk("ESP: unexpected IREG %02x\n", esp->ireg);
2092 if (esp->ireg & ESP_INTR_IC)
2093 esp_dump_cmd_log(esp);
2095 esp_schedule_reset(esp);
2097 if (!(esp->ireg & ESP_INTR_RSEL)) {
2098 /* Some combination of FDONE, BSERV, DC. */
2099 if (esp->select_state != ESP_SELECT_NONE)
2100 intr_done = esp_finish_select(esp);
2101 } else if (esp->ireg & ESP_INTR_RSEL) {
2102 if (esp->active_cmd)
2103 (void) esp_finish_select(esp);
2104 intr_done = esp_reconnect(esp);
2108 intr_done = esp_process_event(esp);
2111 irqreturn_t scsi_esp_intr(int irq, void *dev_id)
2113 struct esp *esp = dev_id;
2114 unsigned long flags;
2117 spin_lock_irqsave(esp->host->host_lock, flags);
2119 if (esp->ops->irq_pending(esp)) {
2124 __esp_interrupt(esp);
2125 if (!(esp->flags & ESP_FLAG_QUICKIRQ_CHECK))
2127 esp->flags &= ~ESP_FLAG_QUICKIRQ_CHECK;
2129 for (i = 0; i < ESP_QUICKIRQ_LIMIT; i++) {
2130 if (esp->ops->irq_pending(esp))
2133 if (i == ESP_QUICKIRQ_LIMIT)
2137 spin_unlock_irqrestore(esp->host->host_lock, flags);
2141 EXPORT_SYMBOL(scsi_esp_intr);
2143 static void __devinit esp_get_revision(struct esp *esp)
2147 esp->config1 = (ESP_CONFIG1_PENABLE | (esp->scsi_id & 7));
2148 esp->config2 = (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY);
2149 esp_write8(esp->config2, ESP_CFG2);
2151 val = esp_read8(ESP_CFG2);
2152 val &= ~ESP_CONFIG2_MAGIC;
2153 if (val != (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY)) {
2154 /* If what we write to cfg2 does not come back, cfg2 is not
2155 * implemented, therefore this must be a plain esp100.
2160 esp_set_all_config3(esp, 5);
2162 esp_write8(esp->config2, ESP_CFG2);
2163 esp_write8(0, ESP_CFG3);
2164 esp_write8(esp->prev_cfg3, ESP_CFG3);
2166 val = esp_read8(ESP_CFG3);
2168 /* The cfg2 register is implemented, however
2169 * cfg3 is not, must be esp100a.
2173 esp_set_all_config3(esp, 0);
2175 esp_write8(esp->prev_cfg3, ESP_CFG3);
2177 /* All of cfg{1,2,3} implemented, must be one of
2178 * the fas variants, figure out which one.
2180 if (esp->cfact == 0 || esp->cfact > ESP_CCF_F5) {
2182 esp->sync_defp = SYNC_DEFP_FAST;
2187 esp_write8(esp->config2, ESP_CFG2);
2192 static void __devinit esp_init_swstate(struct esp *esp)
2196 INIT_LIST_HEAD(&esp->queued_cmds);
2197 INIT_LIST_HEAD(&esp->active_cmds);
2198 INIT_LIST_HEAD(&esp->esp_cmd_pool);
2200 /* Start with a clear state, domain validation (via ->slave_configure,
2201 * spi_dv_device()) will attempt to enable SYNC, WIDE, and tagged
2204 for (i = 0 ; i < ESP_MAX_TARGET; i++) {
2205 esp->target[i].flags = 0;
2206 esp->target[i].nego_goal_period = 0;
2207 esp->target[i].nego_goal_offset = 0;
2208 esp->target[i].nego_goal_width = 0;
2209 esp->target[i].nego_goal_tags = 0;
2213 /* This places the ESP into a known state at boot time. */
2214 static void __devinit esp_bootup_reset(struct esp *esp)
2219 esp->ops->reset_dma(esp);
2224 /* Reset the SCSI bus, but tell ESP not to generate an irq */
2225 val = esp_read8(ESP_CFG1);
2226 val |= ESP_CONFIG1_SRRDISAB;
2227 esp_write8(val, ESP_CFG1);
2229 scsi_esp_cmd(esp, ESP_CMD_RS);
2232 esp_write8(esp->config1, ESP_CFG1);
2234 /* Eat any bitrot in the chip and we are done... */
2235 esp_read8(ESP_INTRPT);
2238 static void __devinit esp_set_clock_params(struct esp *esp)
2243 /* This is getting messy but it has to be done correctly or else
2244 * you get weird behavior all over the place. We are trying to
2245 * basically figure out three pieces of information.
2247 * a) Clock Conversion Factor
2249 * This is a representation of the input crystal clock frequency
2250 * going into the ESP on this machine. Any operation whose timing
2251 * is longer than 400ns depends on this value being correct. For
2252 * example, you'll get blips for arbitration/selection during high
2253 * load or with multiple targets if this is not set correctly.
2255 * b) Selection Time-Out
2257 * The ESP isn't very bright and will arbitrate for the bus and try
2258 * to select a target forever if you let it. This value tells the
2259 * ESP when it has taken too long to negotiate and that it should
2260 * interrupt the CPU so we can see what happened. The value is
2261 * computed as follows (from NCR/Symbios chip docs).
2263 * (Time Out Period) * (Input Clock)
2264 * STO = ----------------------------------
2265 * (8192) * (Clock Conversion Factor)
2267 * We use a time out period of 250ms (ESP_BUS_TIMEOUT).
2269 * c) Imperical constants for synchronous offset and transfer period
2272 * This entails the smallest and largest sync period we could ever
2273 * handle on this ESP.
2277 ccf = ((fmhz / 1000000) + 4) / 5;
2281 /* If we can't find anything reasonable, just assume 20MHZ.
2282 * This is the clock frequency of the older sun4c's where I've
2283 * been unable to find the clock-frequency PROM property. All
2284 * other machines provide useful values it seems.
2286 if (fmhz <= 5000000 || ccf < 1 || ccf > 8) {
2291 esp->cfact = (ccf == 8 ? 0 : ccf);
2293 esp->ccycle = ESP_MHZ_TO_CYCLE(fmhz);
2294 esp->ctick = ESP_TICK(ccf, esp->ccycle);
2295 esp->neg_defp = ESP_NEG_DEFP(fmhz, ccf);
2296 esp->sync_defp = SYNC_DEFP_SLOW;
2299 static const char *esp_chip_names[] = {
2309 static struct scsi_transport_template *esp_transport_template;
2311 int __devinit scsi_esp_register(struct esp *esp, struct device *dev)
2313 static int instance;
2316 esp->host->transportt = esp_transport_template;
2317 esp->host->max_lun = ESP_MAX_LUN;
2318 esp->host->cmd_per_lun = 2;
2320 esp_set_clock_params(esp);
2322 esp_get_revision(esp);
2324 esp_init_swstate(esp);
2326 esp_bootup_reset(esp);
2328 printk(KERN_INFO PFX "esp%u, regs[%1p:%1p] irq[%u]\n",
2329 esp->host->unique_id, esp->regs, esp->dma_regs,
2331 printk(KERN_INFO PFX "esp%u is a %s, %u MHz (ccf=%u), SCSI ID %u\n",
2332 esp->host->unique_id, esp_chip_names[esp->rev],
2333 esp->cfreq / 1000000, esp->cfact, esp->scsi_id);
2335 /* Let the SCSI bus reset settle. */
2336 ssleep(esp_bus_reset_settle);
2338 err = scsi_add_host(esp->host, dev);
2342 esp->host->unique_id = instance++;
2344 scsi_scan_host(esp->host);
2348 EXPORT_SYMBOL(scsi_esp_register);
2350 void __devexit scsi_esp_unregister(struct esp *esp)
2352 scsi_remove_host(esp->host);
2354 EXPORT_SYMBOL(scsi_esp_unregister);
2356 static int esp_slave_alloc(struct scsi_device *dev)
2358 struct esp *esp = host_to_esp(dev->host);
2359 struct esp_target_data *tp = &esp->target[dev->id];
2360 struct esp_lun_data *lp;
2362 lp = kzalloc(sizeof(*lp), GFP_KERNEL);
2367 tp->starget = dev->sdev_target;
2369 spi_min_period(tp->starget) = esp->min_period;
2370 spi_max_offset(tp->starget) = 15;
2372 if (esp->flags & ESP_FLAG_WIDE_CAPABLE)
2373 spi_max_width(tp->starget) = 1;
2375 spi_max_width(tp->starget) = 0;
2380 static int esp_slave_configure(struct scsi_device *dev)
2382 struct esp *esp = host_to_esp(dev->host);
2383 struct esp_target_data *tp = &esp->target[dev->id];
2384 int goal_tags, queue_depth;
2388 if (dev->tagged_supported) {
2389 /* XXX make this configurable somehow XXX */
2390 goal_tags = ESP_DEFAULT_TAGS;
2392 if (goal_tags > ESP_MAX_TAG)
2393 goal_tags = ESP_MAX_TAG;
2396 queue_depth = goal_tags;
2397 if (queue_depth < dev->host->cmd_per_lun)
2398 queue_depth = dev->host->cmd_per_lun;
2401 scsi_set_tag_type(dev, MSG_ORDERED_TAG);
2402 scsi_activate_tcq(dev, queue_depth);
2404 scsi_deactivate_tcq(dev, queue_depth);
2406 tp->flags |= ESP_TGT_DISCONNECT;
2408 if (!spi_initial_dv(dev->sdev_target))
2414 static void esp_slave_destroy(struct scsi_device *dev)
2416 struct esp_lun_data *lp = dev->hostdata;
2419 dev->hostdata = NULL;
2422 static int esp_eh_abort_handler(struct scsi_cmnd *cmd)
2424 struct esp *esp = host_to_esp(cmd->device->host);
2425 struct esp_cmd_entry *ent, *tmp;
2426 struct completion eh_done;
2427 unsigned long flags;
2429 /* XXX This helps a lot with debugging but might be a bit
2430 * XXX much for the final driver.
2432 spin_lock_irqsave(esp->host->host_lock, flags);
2433 printk(KERN_ERR PFX "esp%d: Aborting command [%p:%02x]\n",
2434 esp->host->unique_id, cmd, cmd->cmnd[0]);
2435 ent = esp->active_cmd;
2437 printk(KERN_ERR PFX "esp%d: Current command [%p:%02x]\n",
2438 esp->host->unique_id, ent->cmd, ent->cmd->cmnd[0]);
2439 list_for_each_entry(ent, &esp->queued_cmds, list) {
2440 printk(KERN_ERR PFX "esp%d: Queued command [%p:%02x]\n",
2441 esp->host->unique_id, ent->cmd, ent->cmd->cmnd[0]);
2443 list_for_each_entry(ent, &esp->active_cmds, list) {
2444 printk(KERN_ERR PFX "esp%d: Active command [%p:%02x]\n",
2445 esp->host->unique_id, ent->cmd, ent->cmd->cmnd[0]);
2447 esp_dump_cmd_log(esp);
2448 spin_unlock_irqrestore(esp->host->host_lock, flags);
2450 spin_lock_irqsave(esp->host->host_lock, flags);
2453 list_for_each_entry(tmp, &esp->queued_cmds, list) {
2454 if (tmp->cmd == cmd) {
2461 /* Easiest case, we didn't even issue the command
2462 * yet so it is trivial to abort.
2464 list_del(&ent->list);
2466 cmd->result = DID_ABORT << 16;
2467 cmd->scsi_done(cmd);
2469 esp_put_ent(esp, ent);
2474 init_completion(&eh_done);
2476 ent = esp->active_cmd;
2477 if (ent && ent->cmd == cmd) {
2478 /* Command is the currently active command on
2479 * the bus. If we already have an output message
2482 if (esp->msg_out_len)
2485 /* Send out an abort, encouraging the target to
2486 * go to MSGOUT phase by asserting ATN.
2488 esp->msg_out[0] = ABORT_TASK_SET;
2489 esp->msg_out_len = 1;
2490 ent->eh_done = &eh_done;
2492 scsi_esp_cmd(esp, ESP_CMD_SATN);
2494 /* The command is disconnected. This is not easy to
2495 * abort. For now we fail and let the scsi error
2496 * handling layer go try a scsi bus reset or host
2499 * What we could do is put together a scsi command
2500 * solely for the purpose of sending an abort message
2501 * to the target. Coming up with all the code to
2502 * cook up scsi commands, special case them everywhere,
2503 * etc. is for questionable gain and it would be better
2504 * if the generic scsi error handling layer could do at
2505 * least some of that for us.
2507 * Anyways this is an area for potential future improvement
2513 spin_unlock_irqrestore(esp->host->host_lock, flags);
2515 if (!wait_for_completion_timeout(&eh_done, 5 * HZ)) {
2516 spin_lock_irqsave(esp->host->host_lock, flags);
2517 ent->eh_done = NULL;
2518 spin_unlock_irqrestore(esp->host->host_lock, flags);
2526 spin_unlock_irqrestore(esp->host->host_lock, flags);
2530 /* XXX This might be a good location to set ESP_TGT_BROKEN
2531 * XXX since we know which target/lun in particular is
2532 * XXX causing trouble.
2534 spin_unlock_irqrestore(esp->host->host_lock, flags);
2538 static int esp_eh_bus_reset_handler(struct scsi_cmnd *cmd)
2540 struct esp *esp = host_to_esp(cmd->device->host);
2541 struct completion eh_reset;
2542 unsigned long flags;
2544 init_completion(&eh_reset);
2546 spin_lock_irqsave(esp->host->host_lock, flags);
2548 esp->eh_reset = &eh_reset;
2550 /* XXX This is too simple... We should add lots of
2551 * XXX checks here so that if we find that the chip is
2552 * XXX very wedged we return failure immediately so
2553 * XXX that we can perform a full chip reset.
2555 esp->flags |= ESP_FLAG_RESETTING;
2556 scsi_esp_cmd(esp, ESP_CMD_RS);
2558 spin_unlock_irqrestore(esp->host->host_lock, flags);
2560 ssleep(esp_bus_reset_settle);
2562 if (!wait_for_completion_timeout(&eh_reset, 5 * HZ)) {
2563 spin_lock_irqsave(esp->host->host_lock, flags);
2564 esp->eh_reset = NULL;
2565 spin_unlock_irqrestore(esp->host->host_lock, flags);
2573 /* All bets are off, reset the entire device. */
2574 static int esp_eh_host_reset_handler(struct scsi_cmnd *cmd)
2576 struct esp *esp = host_to_esp(cmd->device->host);
2577 unsigned long flags;
2579 spin_lock_irqsave(esp->host->host_lock, flags);
2580 esp_bootup_reset(esp);
2581 esp_reset_cleanup(esp);
2582 spin_unlock_irqrestore(esp->host->host_lock, flags);
2584 ssleep(esp_bus_reset_settle);
2589 static const char *esp_info(struct Scsi_Host *host)
2594 struct scsi_host_template scsi_esp_template = {
2595 .module = THIS_MODULE,
2598 .queuecommand = esp_queuecommand,
2599 .slave_alloc = esp_slave_alloc,
2600 .slave_configure = esp_slave_configure,
2601 .slave_destroy = esp_slave_destroy,
2602 .eh_abort_handler = esp_eh_abort_handler,
2603 .eh_bus_reset_handler = esp_eh_bus_reset_handler,
2604 .eh_host_reset_handler = esp_eh_host_reset_handler,
2607 .sg_tablesize = SG_ALL,
2608 .use_clustering = ENABLE_CLUSTERING,
2609 .max_sectors = 0xffff,
2610 .skip_settle_delay = 1,
2612 EXPORT_SYMBOL(scsi_esp_template);
2614 static void esp_get_signalling(struct Scsi_Host *host)
2616 struct esp *esp = host_to_esp(host);
2617 enum spi_signal_type type;
2619 if (esp->flags & ESP_FLAG_DIFFERENTIAL)
2620 type = SPI_SIGNAL_HVD;
2622 type = SPI_SIGNAL_SE;
2624 spi_signalling(host) = type;
2627 static void esp_set_offset(struct scsi_target *target, int offset)
2629 struct Scsi_Host *host = dev_to_shost(target->dev.parent);
2630 struct esp *esp = host_to_esp(host);
2631 struct esp_target_data *tp = &esp->target[target->id];
2633 tp->nego_goal_offset = offset;
2634 tp->flags |= ESP_TGT_CHECK_NEGO;
2637 static void esp_set_period(struct scsi_target *target, int period)
2639 struct Scsi_Host *host = dev_to_shost(target->dev.parent);
2640 struct esp *esp = host_to_esp(host);
2641 struct esp_target_data *tp = &esp->target[target->id];
2643 tp->nego_goal_period = period;
2644 tp->flags |= ESP_TGT_CHECK_NEGO;
2647 static void esp_set_width(struct scsi_target *target, int width)
2649 struct Scsi_Host *host = dev_to_shost(target->dev.parent);
2650 struct esp *esp = host_to_esp(host);
2651 struct esp_target_data *tp = &esp->target[target->id];
2653 tp->nego_goal_width = (width ? 1 : 0);
2654 tp->flags |= ESP_TGT_CHECK_NEGO;
2657 static struct spi_function_template esp_transport_ops = {
2658 .set_offset = esp_set_offset,
2660 .set_period = esp_set_period,
2662 .set_width = esp_set_width,
2664 .get_signalling = esp_get_signalling,
2667 static int __init esp_init(void)
2669 BUILD_BUG_ON(sizeof(struct scsi_pointer) <
2670 sizeof(struct esp_cmd_priv));
2672 esp_transport_template = spi_attach_transport(&esp_transport_ops);
2673 if (!esp_transport_template)
2679 static void __exit esp_exit(void)
2681 spi_release_transport(esp_transport_template);
2684 MODULE_DESCRIPTION("ESP SCSI driver core");
2685 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
2686 MODULE_LICENSE("GPL");
2687 MODULE_VERSION(DRV_VERSION);
2689 module_param(esp_bus_reset_settle, int, 0);
2690 MODULE_PARM_DESC(esp_bus_reset_settle,
2691 "ESP scsi bus reset delay in seconds");
2693 module_param(esp_debug, int, 0);
2694 MODULE_PARM_DESC(esp_debug,
2695 "ESP bitmapped debugging message enable value:\n"
2696 " 0x00000001 Log interrupt events\n"
2697 " 0x00000002 Log scsi commands\n"
2698 " 0x00000004 Log resets\n"
2699 " 0x00000008 Log message in events\n"
2700 " 0x00000010 Log message out events\n"
2701 " 0x00000020 Log command completion\n"
2702 " 0x00000040 Log disconnects\n"
2703 " 0x00000080 Log data start\n"
2704 " 0x00000100 Log data done\n"
2705 " 0x00000200 Log reconnects\n"
2706 " 0x00000400 Log auto-sense data\n"
2709 module_init(esp_init);
2710 module_exit(esp_exit);