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>
16 #include <linux/irqreturn.h>
22 #include <scsi/scsi.h>
23 #include <scsi/scsi_host.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_device.h>
26 #include <scsi/scsi_tcq.h>
27 #include <scsi/scsi_dbg.h>
28 #include <scsi/scsi_transport_spi.h>
32 #define DRV_MODULE_NAME "esp"
33 #define PFX DRV_MODULE_NAME ": "
34 #define DRV_VERSION "2.000"
35 #define DRV_MODULE_RELDATE "April 19, 2007"
37 /* SCSI bus reset settle time in seconds. */
38 static int esp_bus_reset_settle = 3;
41 #define ESP_DEBUG_INTR 0x00000001
42 #define ESP_DEBUG_SCSICMD 0x00000002
43 #define ESP_DEBUG_RESET 0x00000004
44 #define ESP_DEBUG_MSGIN 0x00000008
45 #define ESP_DEBUG_MSGOUT 0x00000010
46 #define ESP_DEBUG_CMDDONE 0x00000020
47 #define ESP_DEBUG_DISCONNECT 0x00000040
48 #define ESP_DEBUG_DATASTART 0x00000080
49 #define ESP_DEBUG_DATADONE 0x00000100
50 #define ESP_DEBUG_RECONNECT 0x00000200
51 #define ESP_DEBUG_AUTOSENSE 0x00000400
52 #define ESP_DEBUG_EVENT 0x00000800
53 #define ESP_DEBUG_COMMAND 0x00001000
55 #define esp_log_intr(f, a...) \
56 do { if (esp_debug & ESP_DEBUG_INTR) \
57 shost_printk(KERN_DEBUG, esp->host, f, ## a); \
60 #define esp_log_reset(f, a...) \
61 do { if (esp_debug & ESP_DEBUG_RESET) \
62 shost_printk(KERN_DEBUG, esp->host, f, ## a); \
65 #define esp_log_msgin(f, a...) \
66 do { if (esp_debug & ESP_DEBUG_MSGIN) \
67 shost_printk(KERN_DEBUG, esp->host, f, ## a); \
70 #define esp_log_msgout(f, a...) \
71 do { if (esp_debug & ESP_DEBUG_MSGOUT) \
72 shost_printk(KERN_DEBUG, esp->host, f, ## a); \
75 #define esp_log_cmddone(f, a...) \
76 do { if (esp_debug & ESP_DEBUG_CMDDONE) \
77 shost_printk(KERN_DEBUG, esp->host, f, ## a); \
80 #define esp_log_disconnect(f, a...) \
81 do { if (esp_debug & ESP_DEBUG_DISCONNECT) \
82 shost_printk(KERN_DEBUG, esp->host, f, ## a); \
85 #define esp_log_datastart(f, a...) \
86 do { if (esp_debug & ESP_DEBUG_DATASTART) \
87 shost_printk(KERN_DEBUG, esp->host, f, ## a); \
90 #define esp_log_datadone(f, a...) \
91 do { if (esp_debug & ESP_DEBUG_DATADONE) \
92 shost_printk(KERN_DEBUG, esp->host, f, ## a); \
95 #define esp_log_reconnect(f, a...) \
96 do { if (esp_debug & ESP_DEBUG_RECONNECT) \
97 shost_printk(KERN_DEBUG, esp->host, f, ## a); \
100 #define esp_log_autosense(f, a...) \
101 do { if (esp_debug & ESP_DEBUG_AUTOSENSE) \
102 shost_printk(KERN_DEBUG, esp->host, f, ## a); \
105 #define esp_log_event(f, a...) \
106 do { if (esp_debug & ESP_DEBUG_EVENT) \
107 shost_printk(KERN_DEBUG, esp->host, f, ## a); \
110 #define esp_log_command(f, a...) \
111 do { if (esp_debug & ESP_DEBUG_COMMAND) \
112 shost_printk(KERN_DEBUG, esp->host, f, ## a); \
115 #define esp_read8(REG) esp->ops->esp_read8(esp, REG)
116 #define esp_write8(VAL,REG) esp->ops->esp_write8(esp, VAL, REG)
118 static void esp_log_fill_regs(struct esp *esp,
119 struct esp_event_ent *p)
122 p->seqreg = esp->seqreg;
123 p->sreg2 = esp->sreg2;
125 p->select_state = esp->select_state;
126 p->event = esp->event;
129 void scsi_esp_cmd(struct esp *esp, u8 val)
131 struct esp_event_ent *p;
132 int idx = esp->esp_event_cur;
134 p = &esp->esp_event_log[idx];
135 p->type = ESP_EVENT_TYPE_CMD;
137 esp_log_fill_regs(esp, p);
139 esp->esp_event_cur = (idx + 1) & (ESP_EVENT_LOG_SZ - 1);
141 esp_log_command("cmd[%02x]\n", val);
142 esp_write8(val, ESP_CMD);
144 EXPORT_SYMBOL(scsi_esp_cmd);
146 static void esp_event(struct esp *esp, u8 val)
148 struct esp_event_ent *p;
149 int idx = esp->esp_event_cur;
151 p = &esp->esp_event_log[idx];
152 p->type = ESP_EVENT_TYPE_EVENT;
154 esp_log_fill_regs(esp, p);
156 esp->esp_event_cur = (idx + 1) & (ESP_EVENT_LOG_SZ - 1);
161 static void esp_dump_cmd_log(struct esp *esp)
163 int idx = esp->esp_event_cur;
166 shost_printk(KERN_INFO, esp->host, "Dumping command log\n");
168 struct esp_event_ent *p = &esp->esp_event_log[idx];
170 shost_printk(KERN_INFO, esp->host,
171 "ent[%d] %s val[%02x] sreg[%02x] seqreg[%02x] "
172 "sreg2[%02x] ireg[%02x] ss[%02x] event[%02x]\n",
174 p->type == ESP_EVENT_TYPE_CMD ? "CMD" : "EVENT",
175 p->val, p->sreg, p->seqreg,
176 p->sreg2, p->ireg, p->select_state, p->event);
178 idx = (idx + 1) & (ESP_EVENT_LOG_SZ - 1);
179 } while (idx != stop);
182 static void esp_flush_fifo(struct esp *esp)
184 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
185 if (esp->rev == ESP236) {
188 while (esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES) {
190 shost_printk(KERN_ALERT, esp->host,
191 "ESP_FF_BYTES will not clear!\n");
199 static void hme_read_fifo(struct esp *esp)
201 int fcnt = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
205 esp->fifo[idx++] = esp_read8(ESP_FDATA);
206 esp->fifo[idx++] = esp_read8(ESP_FDATA);
208 if (esp->sreg2 & ESP_STAT2_F1BYTE) {
209 esp_write8(0, ESP_FDATA);
210 esp->fifo[idx++] = esp_read8(ESP_FDATA);
211 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
216 static void esp_set_all_config3(struct esp *esp, u8 val)
220 for (i = 0; i < ESP_MAX_TARGET; i++)
221 esp->target[i].esp_config3 = val;
224 /* Reset the ESP chip, _not_ the SCSI bus. */
225 static void esp_reset_esp(struct esp *esp)
227 u8 family_code, version;
229 /* Now reset the ESP chip */
230 scsi_esp_cmd(esp, ESP_CMD_RC);
231 scsi_esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
232 if (esp->rev == FAST)
233 esp_write8(ESP_CONFIG2_FENAB, ESP_CFG2);
234 scsi_esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
236 /* This is the only point at which it is reliable to read
237 * the ID-code for a fast ESP chip variants.
239 esp->max_period = ((35 * esp->ccycle) / 1000);
240 if (esp->rev == FAST) {
241 version = esp_read8(ESP_UID);
242 family_code = (version & 0xf8) >> 3;
243 if (family_code == 0x02)
245 else if (family_code == 0x0a)
246 esp->rev = FASHME; /* Version is usually '5'. */
249 esp->min_period = ((4 * esp->ccycle) / 1000);
251 esp->min_period = ((5 * esp->ccycle) / 1000);
253 esp->max_period = (esp->max_period + 3)>>2;
254 esp->min_period = (esp->min_period + 3)>>2;
256 esp_write8(esp->config1, ESP_CFG1);
263 esp_write8(esp->config2, ESP_CFG2);
268 esp_write8(esp->config2, ESP_CFG2);
269 esp->prev_cfg3 = esp->target[0].esp_config3;
270 esp_write8(esp->prev_cfg3, ESP_CFG3);
274 esp->config2 |= (ESP_CONFIG2_HME32 | ESP_CONFIG2_HMEFENAB);
278 /* Fast 236 or HME */
279 esp_write8(esp->config2, ESP_CFG2);
280 if (esp->rev == FASHME) {
281 u8 cfg3 = esp->target[0].esp_config3;
283 cfg3 |= ESP_CONFIG3_FCLOCK | ESP_CONFIG3_OBPUSH;
284 if (esp->scsi_id >= 8)
285 cfg3 |= ESP_CONFIG3_IDBIT3;
286 esp_set_all_config3(esp, cfg3);
288 u32 cfg3 = esp->target[0].esp_config3;
290 cfg3 |= ESP_CONFIG3_FCLK;
291 esp_set_all_config3(esp, cfg3);
293 esp->prev_cfg3 = esp->target[0].esp_config3;
294 esp_write8(esp->prev_cfg3, ESP_CFG3);
295 if (esp->rev == FASHME) {
298 if (esp->flags & ESP_FLAG_DIFFERENTIAL)
307 esp_write8(esp->config2, ESP_CFG2);
308 esp_set_all_config3(esp,
309 (esp->target[0].esp_config3 |
310 ESP_CONFIG3_FCLOCK));
311 esp->prev_cfg3 = esp->target[0].esp_config3;
312 esp_write8(esp->prev_cfg3, ESP_CFG3);
320 /* Reload the configuration registers */
321 esp_write8(esp->cfact, ESP_CFACT);
324 esp_write8(esp->prev_stp, ESP_STP);
327 esp_write8(esp->prev_soff, ESP_SOFF);
329 esp_write8(esp->neg_defp, ESP_TIMEO);
331 /* Eat any bitrot in the chip */
332 esp_read8(ESP_INTRPT);
336 static void esp_map_dma(struct esp *esp, struct scsi_cmnd *cmd)
338 struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
339 struct scatterlist *sg = scsi_sglist(cmd);
340 int dir = cmd->sc_data_direction;
346 spriv->u.num_sg = esp->ops->map_sg(esp, sg, scsi_sg_count(cmd), dir);
347 spriv->cur_residue = sg_dma_len(sg);
351 for (i = 0; i < spriv->u.num_sg; i++)
352 total += sg_dma_len(&sg[i]);
353 spriv->tot_residue = total;
356 static dma_addr_t esp_cur_dma_addr(struct esp_cmd_entry *ent,
357 struct scsi_cmnd *cmd)
359 struct esp_cmd_priv *p = ESP_CMD_PRIV(cmd);
361 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
362 return ent->sense_dma +
363 (ent->sense_ptr - cmd->sense_buffer);
366 return sg_dma_address(p->cur_sg) +
367 (sg_dma_len(p->cur_sg) -
371 static unsigned int esp_cur_dma_len(struct esp_cmd_entry *ent,
372 struct scsi_cmnd *cmd)
374 struct esp_cmd_priv *p = ESP_CMD_PRIV(cmd);
376 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
377 return SCSI_SENSE_BUFFERSIZE -
378 (ent->sense_ptr - cmd->sense_buffer);
380 return p->cur_residue;
383 static void esp_advance_dma(struct esp *esp, struct esp_cmd_entry *ent,
384 struct scsi_cmnd *cmd, unsigned int len)
386 struct esp_cmd_priv *p = ESP_CMD_PRIV(cmd);
388 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
389 ent->sense_ptr += len;
393 p->cur_residue -= len;
394 p->tot_residue -= len;
395 if (p->cur_residue < 0 || p->tot_residue < 0) {
396 shost_printk(KERN_ERR, esp->host,
397 "Data transfer overflow.\n");
398 shost_printk(KERN_ERR, esp->host,
399 "cur_residue[%d] tot_residue[%d] len[%u]\n",
400 p->cur_residue, p->tot_residue, len);
404 if (!p->cur_residue && p->tot_residue) {
406 p->cur_residue = sg_dma_len(p->cur_sg);
410 static void esp_unmap_dma(struct esp *esp, struct scsi_cmnd *cmd)
412 struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
413 int dir = cmd->sc_data_direction;
418 esp->ops->unmap_sg(esp, scsi_sglist(cmd), spriv->u.num_sg, dir);
421 static void esp_save_pointers(struct esp *esp, struct esp_cmd_entry *ent)
423 struct scsi_cmnd *cmd = ent->cmd;
424 struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
426 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
427 ent->saved_sense_ptr = ent->sense_ptr;
430 ent->saved_cur_residue = spriv->cur_residue;
431 ent->saved_cur_sg = spriv->cur_sg;
432 ent->saved_tot_residue = spriv->tot_residue;
435 static void esp_restore_pointers(struct esp *esp, struct esp_cmd_entry *ent)
437 struct scsi_cmnd *cmd = ent->cmd;
438 struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
440 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
441 ent->sense_ptr = ent->saved_sense_ptr;
444 spriv->cur_residue = ent->saved_cur_residue;
445 spriv->cur_sg = ent->saved_cur_sg;
446 spriv->tot_residue = ent->saved_tot_residue;
449 static void esp_check_command_len(struct esp *esp, struct scsi_cmnd *cmd)
451 if (cmd->cmd_len == 6 ||
452 cmd->cmd_len == 10 ||
453 cmd->cmd_len == 12) {
454 esp->flags &= ~ESP_FLAG_DOING_SLOWCMD;
456 esp->flags |= ESP_FLAG_DOING_SLOWCMD;
460 static void esp_write_tgt_config3(struct esp *esp, int tgt)
462 if (esp->rev > ESP100A) {
463 u8 val = esp->target[tgt].esp_config3;
465 if (val != esp->prev_cfg3) {
466 esp->prev_cfg3 = val;
467 esp_write8(val, ESP_CFG3);
472 static void esp_write_tgt_sync(struct esp *esp, int tgt)
474 u8 off = esp->target[tgt].esp_offset;
475 u8 per = esp->target[tgt].esp_period;
477 if (off != esp->prev_soff) {
478 esp->prev_soff = off;
479 esp_write8(off, ESP_SOFF);
481 if (per != esp->prev_stp) {
483 esp_write8(per, ESP_STP);
487 static u32 esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len)
489 if (esp->rev == FASHME) {
490 /* Arbitrary segment boundaries, 24-bit counts. */
491 if (dma_len > (1U << 24))
492 dma_len = (1U << 24);
496 /* ESP chip limits other variants by 16-bits of transfer
497 * count. Actually on FAS100A and FAS236 we could get
498 * 24-bits of transfer count by enabling ESP_CONFIG2_FENAB
499 * in the ESP_CFG2 register but that causes other unwanted
500 * changes so we don't use it currently.
502 if (dma_len > (1U << 16))
503 dma_len = (1U << 16);
505 /* All of the DMA variants hooked up to these chips
506 * cannot handle crossing a 24-bit address boundary.
508 base = dma_addr & ((1U << 24) - 1U);
509 end = base + dma_len;
510 if (end > (1U << 24))
512 dma_len = end - base;
517 static int esp_need_to_nego_wide(struct esp_target_data *tp)
519 struct scsi_target *target = tp->starget;
521 return spi_width(target) != tp->nego_goal_width;
524 static int esp_need_to_nego_sync(struct esp_target_data *tp)
526 struct scsi_target *target = tp->starget;
528 /* When offset is zero, period is "don't care". */
529 if (!spi_offset(target) && !tp->nego_goal_offset)
532 if (spi_offset(target) == tp->nego_goal_offset &&
533 spi_period(target) == tp->nego_goal_period)
539 static int esp_alloc_lun_tag(struct esp_cmd_entry *ent,
540 struct esp_lun_data *lp)
542 if (!ent->orig_tag[0]) {
543 /* Non-tagged, slot already taken? */
544 if (lp->non_tagged_cmd)
548 /* We are being held by active tagged
554 /* Tagged commands completed, we can unplug
555 * the queue and run this untagged command.
558 } else if (lp->num_tagged) {
559 /* Plug the queue until num_tagged decreases
560 * to zero in esp_free_lun_tag.
566 lp->non_tagged_cmd = ent;
569 /* Tagged command, see if blocked by a
572 if (lp->non_tagged_cmd || lp->hold)
576 BUG_ON(lp->tagged_cmds[ent->orig_tag[1]]);
578 lp->tagged_cmds[ent->orig_tag[1]] = ent;
584 static void esp_free_lun_tag(struct esp_cmd_entry *ent,
585 struct esp_lun_data *lp)
587 if (ent->orig_tag[0]) {
588 BUG_ON(lp->tagged_cmds[ent->orig_tag[1]] != ent);
589 lp->tagged_cmds[ent->orig_tag[1]] = NULL;
592 BUG_ON(lp->non_tagged_cmd != ent);
593 lp->non_tagged_cmd = NULL;
597 /* When a contingent allegiance conditon is created, we force feed a
598 * REQUEST_SENSE command to the device to fetch the sense data. I
599 * tried many other schemes, relying on the scsi error handling layer
600 * to send out the REQUEST_SENSE automatically, but this was difficult
601 * to get right especially in the presence of applications like smartd
602 * which use SG_IO to send out their own REQUEST_SENSE commands.
604 static void esp_autosense(struct esp *esp, struct esp_cmd_entry *ent)
606 struct scsi_cmnd *cmd = ent->cmd;
607 struct scsi_device *dev = cmd->device;
615 if (!ent->sense_ptr) {
616 esp_log_autosense("Doing auto-sense for tgt[%d] lun[%d]\n",
619 ent->sense_ptr = cmd->sense_buffer;
620 ent->sense_dma = esp->ops->map_single(esp,
622 SCSI_SENSE_BUFFERSIZE,
625 ent->saved_sense_ptr = ent->sense_ptr;
627 esp->active_cmd = ent;
629 p = esp->command_block;
630 esp->msg_out_len = 0;
632 *p++ = IDENTIFY(0, lun);
633 *p++ = REQUEST_SENSE;
634 *p++ = ((dev->scsi_level <= SCSI_2) ?
638 *p++ = SCSI_SENSE_BUFFERSIZE;
641 esp->select_state = ESP_SELECT_BASIC;
644 if (esp->rev == FASHME)
645 val |= ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT;
646 esp_write8(val, ESP_BUSID);
648 esp_write_tgt_sync(esp, tgt);
649 esp_write_tgt_config3(esp, tgt);
651 val = (p - esp->command_block);
653 if (esp->rev == FASHME)
654 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
655 esp->ops->send_dma_cmd(esp, esp->command_block_dma,
656 val, 16, 0, ESP_CMD_DMA | ESP_CMD_SELA);
659 static struct esp_cmd_entry *find_and_prep_issuable_command(struct esp *esp)
661 struct esp_cmd_entry *ent;
663 list_for_each_entry(ent, &esp->queued_cmds, list) {
664 struct scsi_cmnd *cmd = ent->cmd;
665 struct scsi_device *dev = cmd->device;
666 struct esp_lun_data *lp = dev->hostdata;
668 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
674 if (!spi_populate_tag_msg(&ent->tag[0], cmd)) {
678 ent->orig_tag[0] = ent->tag[0];
679 ent->orig_tag[1] = ent->tag[1];
681 if (esp_alloc_lun_tag(ent, lp) < 0)
690 static void esp_maybe_execute_command(struct esp *esp)
692 struct esp_target_data *tp;
693 struct esp_lun_data *lp;
694 struct scsi_device *dev;
695 struct scsi_cmnd *cmd;
696 struct esp_cmd_entry *ent;
701 if (esp->active_cmd ||
702 (esp->flags & ESP_FLAG_RESETTING))
705 ent = find_and_prep_issuable_command(esp);
709 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
710 esp_autosense(esp, ent);
718 tp = &esp->target[tgt];
721 list_move(&ent->list, &esp->active_cmds);
723 esp->active_cmd = ent;
725 esp_map_dma(esp, cmd);
726 esp_save_pointers(esp, ent);
728 esp_check_command_len(esp, cmd);
730 p = esp->command_block;
732 esp->msg_out_len = 0;
733 if (tp->flags & ESP_TGT_CHECK_NEGO) {
734 /* Need to negotiate. If the target is broken
735 * go for synchronous transfers and non-wide.
737 if (tp->flags & ESP_TGT_BROKEN) {
738 tp->flags &= ~ESP_TGT_DISCONNECT;
739 tp->nego_goal_period = 0;
740 tp->nego_goal_offset = 0;
741 tp->nego_goal_width = 0;
742 tp->nego_goal_tags = 0;
745 /* If the settings are not changing, skip this. */
746 if (spi_width(tp->starget) == tp->nego_goal_width &&
747 spi_period(tp->starget) == tp->nego_goal_period &&
748 spi_offset(tp->starget) == tp->nego_goal_offset) {
749 tp->flags &= ~ESP_TGT_CHECK_NEGO;
753 if (esp->rev == FASHME && esp_need_to_nego_wide(tp)) {
755 spi_populate_width_msg(&esp->msg_out[0],
756 (tp->nego_goal_width ?
758 tp->flags |= ESP_TGT_NEGO_WIDE;
759 } else if (esp_need_to_nego_sync(tp)) {
761 spi_populate_sync_msg(&esp->msg_out[0],
762 tp->nego_goal_period,
763 tp->nego_goal_offset);
764 tp->flags |= ESP_TGT_NEGO_SYNC;
766 tp->flags &= ~ESP_TGT_CHECK_NEGO;
769 /* Process it like a slow command. */
770 if (tp->flags & (ESP_TGT_NEGO_WIDE | ESP_TGT_NEGO_SYNC))
771 esp->flags |= ESP_FLAG_DOING_SLOWCMD;
775 /* If we don't have a lun-data struct yet, we're probing
776 * so do not disconnect. Also, do not disconnect unless
777 * we have a tag on this command.
779 if (lp && (tp->flags & ESP_TGT_DISCONNECT) && ent->tag[0])
780 *p++ = IDENTIFY(1, lun);
782 *p++ = IDENTIFY(0, lun);
784 if (ent->tag[0] && esp->rev == ESP100) {
785 /* ESP100 lacks select w/atn3 command, use select
788 esp->flags |= ESP_FLAG_DOING_SLOWCMD;
791 if (!(esp->flags & ESP_FLAG_DOING_SLOWCMD)) {
792 start_cmd = ESP_CMD_DMA | ESP_CMD_SELA;
797 start_cmd = ESP_CMD_DMA | ESP_CMD_SA3;
800 for (i = 0; i < cmd->cmd_len; i++)
803 esp->select_state = ESP_SELECT_BASIC;
805 esp->cmd_bytes_left = cmd->cmd_len;
806 esp->cmd_bytes_ptr = &cmd->cmnd[0];
809 for (i = esp->msg_out_len - 1;
811 esp->msg_out[i + 2] = esp->msg_out[i];
812 esp->msg_out[0] = ent->tag[0];
813 esp->msg_out[1] = ent->tag[1];
814 esp->msg_out_len += 2;
817 start_cmd = ESP_CMD_DMA | ESP_CMD_SELAS;
818 esp->select_state = ESP_SELECT_MSGOUT;
821 if (esp->rev == FASHME)
822 val |= ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT;
823 esp_write8(val, ESP_BUSID);
825 esp_write_tgt_sync(esp, tgt);
826 esp_write_tgt_config3(esp, tgt);
828 val = (p - esp->command_block);
830 if (esp_debug & ESP_DEBUG_SCSICMD) {
831 printk("ESP: tgt[%d] lun[%d] scsi_cmd [ ", tgt, lun);
832 for (i = 0; i < cmd->cmd_len; i++)
833 printk("%02x ", cmd->cmnd[i]);
837 if (esp->rev == FASHME)
838 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
839 esp->ops->send_dma_cmd(esp, esp->command_block_dma,
840 val, 16, 0, start_cmd);
843 static struct esp_cmd_entry *esp_get_ent(struct esp *esp)
845 struct list_head *head = &esp->esp_cmd_pool;
846 struct esp_cmd_entry *ret;
848 if (list_empty(head)) {
849 ret = kzalloc(sizeof(struct esp_cmd_entry), GFP_ATOMIC);
851 ret = list_entry(head->next, struct esp_cmd_entry, list);
852 list_del(&ret->list);
853 memset(ret, 0, sizeof(*ret));
858 static void esp_put_ent(struct esp *esp, struct esp_cmd_entry *ent)
860 list_add(&ent->list, &esp->esp_cmd_pool);
863 static void esp_cmd_is_done(struct esp *esp, struct esp_cmd_entry *ent,
864 struct scsi_cmnd *cmd, unsigned int result)
866 struct scsi_device *dev = cmd->device;
870 esp->active_cmd = NULL;
871 esp_unmap_dma(esp, cmd);
872 esp_free_lun_tag(ent, dev->hostdata);
873 cmd->result = result;
876 complete(ent->eh_done);
880 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
881 esp->ops->unmap_single(esp, ent->sense_dma,
882 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
883 ent->sense_ptr = NULL;
885 /* Restore the message/status bytes to what we actually
886 * saw originally. Also, report that we are providing
889 cmd->result = ((DRIVER_SENSE << 24) |
891 (COMMAND_COMPLETE << 8) |
892 (SAM_STAT_CHECK_CONDITION << 0));
894 ent->flags &= ~ESP_CMD_FLAG_AUTOSENSE;
895 if (esp_debug & ESP_DEBUG_AUTOSENSE) {
898 printk("esp%d: tgt[%d] lun[%d] AUTO SENSE[ ",
899 esp->host->unique_id, tgt, lun);
900 for (i = 0; i < 18; i++)
901 printk("%02x ", cmd->sense_buffer[i]);
908 list_del(&ent->list);
909 esp_put_ent(esp, ent);
911 esp_maybe_execute_command(esp);
914 static unsigned int compose_result(unsigned int status, unsigned int message,
915 unsigned int driver_code)
917 return (status | (message << 8) | (driver_code << 16));
920 static void esp_event_queue_full(struct esp *esp, struct esp_cmd_entry *ent)
922 struct scsi_device *dev = ent->cmd->device;
923 struct esp_lun_data *lp = dev->hostdata;
925 scsi_track_queue_full(dev, lp->num_tagged - 1);
928 static int esp_queuecommand_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
930 struct scsi_device *dev = cmd->device;
931 struct esp *esp = shost_priv(dev->host);
932 struct esp_cmd_priv *spriv;
933 struct esp_cmd_entry *ent;
935 ent = esp_get_ent(esp);
937 return SCSI_MLQUEUE_HOST_BUSY;
941 cmd->scsi_done = done;
943 spriv = ESP_CMD_PRIV(cmd);
944 spriv->u.dma_addr = ~(dma_addr_t)0x0;
946 list_add_tail(&ent->list, &esp->queued_cmds);
948 esp_maybe_execute_command(esp);
953 static DEF_SCSI_QCMD(esp_queuecommand)
955 static int esp_check_gross_error(struct esp *esp)
957 if (esp->sreg & ESP_STAT_SPAM) {
958 /* Gross Error, could be one of:
959 * - top of fifo overwritten
960 * - top of command register overwritten
961 * - DMA programmed with wrong direction
962 * - improper phase change
964 shost_printk(KERN_ERR, esp->host,
965 "Gross error sreg[%02x]\n", esp->sreg);
966 /* XXX Reset the chip. XXX */
972 static int esp_check_spur_intr(struct esp *esp)
977 /* The interrupt pending bit of the status register cannot
978 * be trusted on these revisions.
980 esp->sreg &= ~ESP_STAT_INTR;
984 if (!(esp->sreg & ESP_STAT_INTR)) {
985 if (esp->ireg & ESP_INTR_SR)
988 /* If the DMA is indicating interrupt pending and the
989 * ESP is not, the only possibility is a DMA error.
991 if (!esp->ops->dma_error(esp)) {
992 shost_printk(KERN_ERR, esp->host,
993 "Spurious irq, sreg=%02x.\n",
998 shost_printk(KERN_ERR, esp->host, "DMA error\n");
1000 /* XXX Reset the chip. XXX */
1009 static void esp_schedule_reset(struct esp *esp)
1011 esp_log_reset("esp_schedule_reset() from %pf\n",
1012 __builtin_return_address(0));
1013 esp->flags |= ESP_FLAG_RESETTING;
1014 esp_event(esp, ESP_EVENT_RESET);
1017 /* In order to avoid having to add a special half-reconnected state
1018 * into the driver we just sit here and poll through the rest of
1019 * the reselection process to get the tag message bytes.
1021 static struct esp_cmd_entry *esp_reconnect_with_tag(struct esp *esp,
1022 struct esp_lun_data *lp)
1024 struct esp_cmd_entry *ent;
1027 if (!lp->num_tagged) {
1028 shost_printk(KERN_ERR, esp->host,
1029 "Reconnect w/num_tagged==0\n");
1033 esp_log_reconnect("reconnect tag, ");
1035 for (i = 0; i < ESP_QUICKIRQ_LIMIT; i++) {
1036 if (esp->ops->irq_pending(esp))
1039 if (i == ESP_QUICKIRQ_LIMIT) {
1040 shost_printk(KERN_ERR, esp->host,
1041 "Reconnect IRQ1 timeout\n");
1045 esp->sreg = esp_read8(ESP_STATUS);
1046 esp->ireg = esp_read8(ESP_INTRPT);
1048 esp_log_reconnect("IRQ(%d:%x:%x), ",
1049 i, esp->ireg, esp->sreg);
1051 if (esp->ireg & ESP_INTR_DC) {
1052 shost_printk(KERN_ERR, esp->host,
1053 "Reconnect, got disconnect.\n");
1057 if ((esp->sreg & ESP_STAT_PMASK) != ESP_MIP) {
1058 shost_printk(KERN_ERR, esp->host,
1059 "Reconnect, not MIP sreg[%02x].\n", esp->sreg);
1063 /* DMA in the tag bytes... */
1064 esp->command_block[0] = 0xff;
1065 esp->command_block[1] = 0xff;
1066 esp->ops->send_dma_cmd(esp, esp->command_block_dma,
1067 2, 2, 1, ESP_CMD_DMA | ESP_CMD_TI);
1069 /* ACK the message. */
1070 scsi_esp_cmd(esp, ESP_CMD_MOK);
1072 for (i = 0; i < ESP_RESELECT_TAG_LIMIT; i++) {
1073 if (esp->ops->irq_pending(esp)) {
1074 esp->sreg = esp_read8(ESP_STATUS);
1075 esp->ireg = esp_read8(ESP_INTRPT);
1076 if (esp->ireg & ESP_INTR_FDONE)
1081 if (i == ESP_RESELECT_TAG_LIMIT) {
1082 shost_printk(KERN_ERR, esp->host, "Reconnect IRQ2 timeout\n");
1085 esp->ops->dma_drain(esp);
1086 esp->ops->dma_invalidate(esp);
1088 esp_log_reconnect("IRQ2(%d:%x:%x) tag[%x:%x]\n",
1089 i, esp->ireg, esp->sreg,
1090 esp->command_block[0],
1091 esp->command_block[1]);
1093 if (esp->command_block[0] < SIMPLE_QUEUE_TAG ||
1094 esp->command_block[0] > ORDERED_QUEUE_TAG) {
1095 shost_printk(KERN_ERR, esp->host,
1096 "Reconnect, bad tag type %02x.\n",
1097 esp->command_block[0]);
1101 ent = lp->tagged_cmds[esp->command_block[1]];
1103 shost_printk(KERN_ERR, esp->host,
1104 "Reconnect, no entry for tag %02x.\n",
1105 esp->command_block[1]);
1112 static int esp_reconnect(struct esp *esp)
1114 struct esp_cmd_entry *ent;
1115 struct esp_target_data *tp;
1116 struct esp_lun_data *lp;
1117 struct scsi_device *dev;
1120 BUG_ON(esp->active_cmd);
1121 if (esp->rev == FASHME) {
1122 /* FASHME puts the target and lun numbers directly
1125 target = esp->fifo[0];
1126 lun = esp->fifo[1] & 0x7;
1128 u8 bits = esp_read8(ESP_FDATA);
1130 /* Older chips put the lun directly into the fifo, but
1131 * the target is given as a sample of the arbitration
1132 * lines on the bus at reselection time. So we should
1133 * see the ID of the ESP and the one reconnecting target
1134 * set in the bitmap.
1136 if (!(bits & esp->scsi_id_mask))
1138 bits &= ~esp->scsi_id_mask;
1139 if (!bits || (bits & (bits - 1)))
1142 target = ffs(bits) - 1;
1143 lun = (esp_read8(ESP_FDATA) & 0x7);
1145 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1146 if (esp->rev == ESP100) {
1147 u8 ireg = esp_read8(ESP_INTRPT);
1148 /* This chip has a bug during reselection that can
1149 * cause a spurious illegal-command interrupt, which
1150 * we simply ACK here. Another possibility is a bus
1151 * reset so we must check for that.
1153 if (ireg & ESP_INTR_SR)
1156 scsi_esp_cmd(esp, ESP_CMD_NULL);
1159 esp_write_tgt_sync(esp, target);
1160 esp_write_tgt_config3(esp, target);
1162 scsi_esp_cmd(esp, ESP_CMD_MOK);
1164 if (esp->rev == FASHME)
1165 esp_write8(target | ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT,
1168 tp = &esp->target[target];
1169 dev = __scsi_device_lookup_by_target(tp->starget, lun);
1171 shost_printk(KERN_ERR, esp->host,
1172 "Reconnect, no lp tgt[%u] lun[%u]\n",
1178 ent = lp->non_tagged_cmd;
1180 ent = esp_reconnect_with_tag(esp, lp);
1185 esp->active_cmd = ent;
1187 if (ent->flags & ESP_CMD_FLAG_ABORT) {
1188 esp->msg_out[0] = ABORT_TASK_SET;
1189 esp->msg_out_len = 1;
1190 scsi_esp_cmd(esp, ESP_CMD_SATN);
1193 esp_event(esp, ESP_EVENT_CHECK_PHASE);
1194 esp_restore_pointers(esp, ent);
1195 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1199 esp_schedule_reset(esp);
1203 static int esp_finish_select(struct esp *esp)
1205 struct esp_cmd_entry *ent;
1206 struct scsi_cmnd *cmd;
1207 u8 orig_select_state;
1209 orig_select_state = esp->select_state;
1211 /* No longer selecting. */
1212 esp->select_state = ESP_SELECT_NONE;
1214 esp->seqreg = esp_read8(ESP_SSTEP) & ESP_STEP_VBITS;
1215 ent = esp->active_cmd;
1218 if (esp->ops->dma_error(esp)) {
1219 /* If we see a DMA error during or as a result of selection,
1222 esp_schedule_reset(esp);
1223 esp_cmd_is_done(esp, ent, cmd, (DID_ERROR << 16));
1227 esp->ops->dma_invalidate(esp);
1229 if (esp->ireg == (ESP_INTR_RSEL | ESP_INTR_FDONE)) {
1230 struct esp_target_data *tp = &esp->target[cmd->device->id];
1232 /* Carefully back out of the selection attempt. Release
1233 * resources (such as DMA mapping & TAG) and reset state (such
1234 * as message out and command delivery variables).
1236 if (!(ent->flags & ESP_CMD_FLAG_AUTOSENSE)) {
1237 esp_unmap_dma(esp, cmd);
1238 esp_free_lun_tag(ent, cmd->device->hostdata);
1239 tp->flags &= ~(ESP_TGT_NEGO_SYNC | ESP_TGT_NEGO_WIDE);
1240 esp->flags &= ~ESP_FLAG_DOING_SLOWCMD;
1241 esp->cmd_bytes_ptr = NULL;
1242 esp->cmd_bytes_left = 0;
1244 esp->ops->unmap_single(esp, ent->sense_dma,
1245 SCSI_SENSE_BUFFERSIZE,
1247 ent->sense_ptr = NULL;
1250 /* Now that the state is unwound properly, put back onto
1251 * the issue queue. This command is no longer active.
1253 list_move(&ent->list, &esp->queued_cmds);
1254 esp->active_cmd = NULL;
1256 /* Return value ignored by caller, it directly invokes
1262 if (esp->ireg == ESP_INTR_DC) {
1263 struct scsi_device *dev = cmd->device;
1265 /* Disconnect. Make sure we re-negotiate sync and
1266 * wide parameters if this target starts responding
1267 * again in the future.
1269 esp->target[dev->id].flags |= ESP_TGT_CHECK_NEGO;
1271 scsi_esp_cmd(esp, ESP_CMD_ESEL);
1272 esp_cmd_is_done(esp, ent, cmd, (DID_BAD_TARGET << 16));
1276 if (esp->ireg == (ESP_INTR_FDONE | ESP_INTR_BSERV)) {
1277 /* Selection successful. On pre-FAST chips we have
1278 * to do a NOP and possibly clean out the FIFO.
1280 if (esp->rev <= ESP236) {
1281 int fcnt = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
1283 scsi_esp_cmd(esp, ESP_CMD_NULL);
1287 ((esp->sreg & ESP_STAT_PMASK) != ESP_DIP)))
1288 esp_flush_fifo(esp);
1291 /* If we are doing a slow command, negotiation, etc.
1292 * we'll do the right thing as we transition to the
1295 esp_event(esp, ESP_EVENT_CHECK_PHASE);
1299 shost_printk(KERN_INFO, esp->host,
1300 "Unexpected selection completion ireg[%x]\n", esp->ireg);
1301 esp_schedule_reset(esp);
1305 static int esp_data_bytes_sent(struct esp *esp, struct esp_cmd_entry *ent,
1306 struct scsi_cmnd *cmd)
1308 int fifo_cnt, ecount, bytes_sent, flush_fifo;
1310 fifo_cnt = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
1311 if (esp->prev_cfg3 & ESP_CONFIG3_EWIDE)
1315 if (!(esp->sreg & ESP_STAT_TCNT)) {
1316 ecount = ((unsigned int)esp_read8(ESP_TCLOW) |
1317 (((unsigned int)esp_read8(ESP_TCMED)) << 8));
1318 if (esp->rev == FASHME)
1319 ecount |= ((unsigned int)esp_read8(FAS_RLO)) << 16;
1322 bytes_sent = esp->data_dma_len;
1323 bytes_sent -= ecount;
1325 if (!(ent->flags & ESP_CMD_FLAG_WRITE))
1326 bytes_sent -= fifo_cnt;
1329 if (!esp->prev_soff) {
1330 /* Synchronous data transfer, always flush fifo. */
1333 if (esp->rev == ESP100) {
1336 /* ESP100 has a chip bug where in the synchronous data
1337 * phase it can mistake a final long REQ pulse from the
1338 * target as an extra data byte. Fun.
1340 * To detect this case we resample the status register
1341 * and fifo flags. If we're still in a data phase and
1342 * we see spurious chunks in the fifo, we return error
1343 * to the caller which should reset and set things up
1344 * such that we only try future transfers to this
1345 * target in synchronous mode.
1347 esp->sreg = esp_read8(ESP_STATUS);
1348 phase = esp->sreg & ESP_STAT_PMASK;
1349 fflags = esp_read8(ESP_FFLAGS);
1351 if ((phase == ESP_DOP &&
1352 (fflags & ESP_FF_ONOTZERO)) ||
1353 (phase == ESP_DIP &&
1354 (fflags & ESP_FF_FBYTES)))
1357 if (!(ent->flags & ESP_CMD_FLAG_WRITE))
1362 esp_flush_fifo(esp);
1367 static void esp_setsync(struct esp *esp, struct esp_target_data *tp,
1368 u8 scsi_period, u8 scsi_offset,
1369 u8 esp_stp, u8 esp_soff)
1371 spi_period(tp->starget) = scsi_period;
1372 spi_offset(tp->starget) = scsi_offset;
1373 spi_width(tp->starget) = (tp->flags & ESP_TGT_WIDE) ? 1 : 0;
1377 esp_soff |= esp->radelay;
1378 if (esp->rev >= FAS236) {
1379 u8 bit = ESP_CONFIG3_FSCSI;
1380 if (esp->rev >= FAS100A)
1381 bit = ESP_CONFIG3_FAST;
1383 if (scsi_period < 50) {
1384 if (esp->rev == FASHME)
1385 esp_soff &= ~esp->radelay;
1386 tp->esp_config3 |= bit;
1388 tp->esp_config3 &= ~bit;
1390 esp->prev_cfg3 = tp->esp_config3;
1391 esp_write8(esp->prev_cfg3, ESP_CFG3);
1395 tp->esp_period = esp->prev_stp = esp_stp;
1396 tp->esp_offset = esp->prev_soff = esp_soff;
1398 esp_write8(esp_soff, ESP_SOFF);
1399 esp_write8(esp_stp, ESP_STP);
1401 tp->flags &= ~(ESP_TGT_NEGO_SYNC | ESP_TGT_CHECK_NEGO);
1403 spi_display_xfer_agreement(tp->starget);
1406 static void esp_msgin_reject(struct esp *esp)
1408 struct esp_cmd_entry *ent = esp->active_cmd;
1409 struct scsi_cmnd *cmd = ent->cmd;
1410 struct esp_target_data *tp;
1413 tgt = cmd->device->id;
1414 tp = &esp->target[tgt];
1416 if (tp->flags & ESP_TGT_NEGO_WIDE) {
1417 tp->flags &= ~(ESP_TGT_NEGO_WIDE | ESP_TGT_WIDE);
1419 if (!esp_need_to_nego_sync(tp)) {
1420 tp->flags &= ~ESP_TGT_CHECK_NEGO;
1421 scsi_esp_cmd(esp, ESP_CMD_RATN);
1424 spi_populate_sync_msg(&esp->msg_out[0],
1425 tp->nego_goal_period,
1426 tp->nego_goal_offset);
1427 tp->flags |= ESP_TGT_NEGO_SYNC;
1428 scsi_esp_cmd(esp, ESP_CMD_SATN);
1433 if (tp->flags & ESP_TGT_NEGO_SYNC) {
1434 tp->flags &= ~(ESP_TGT_NEGO_SYNC | ESP_TGT_CHECK_NEGO);
1437 esp_setsync(esp, tp, 0, 0, 0, 0);
1438 scsi_esp_cmd(esp, ESP_CMD_RATN);
1442 esp->msg_out[0] = ABORT_TASK_SET;
1443 esp->msg_out_len = 1;
1444 scsi_esp_cmd(esp, ESP_CMD_SATN);
1447 static void esp_msgin_sdtr(struct esp *esp, struct esp_target_data *tp)
1449 u8 period = esp->msg_in[3];
1450 u8 offset = esp->msg_in[4];
1453 if (!(tp->flags & ESP_TGT_NEGO_SYNC))
1462 if (period > esp->max_period) {
1463 period = offset = 0;
1466 if (period < esp->min_period)
1469 one_clock = esp->ccycle / 1000;
1470 stp = DIV_ROUND_UP(period << 2, one_clock);
1471 if (stp && esp->rev >= FAS236) {
1479 esp_setsync(esp, tp, period, offset, stp, offset);
1483 esp->msg_out[0] = MESSAGE_REJECT;
1484 esp->msg_out_len = 1;
1485 scsi_esp_cmd(esp, ESP_CMD_SATN);
1489 tp->nego_goal_period = period;
1490 tp->nego_goal_offset = offset;
1492 spi_populate_sync_msg(&esp->msg_out[0],
1493 tp->nego_goal_period,
1494 tp->nego_goal_offset);
1495 scsi_esp_cmd(esp, ESP_CMD_SATN);
1498 static void esp_msgin_wdtr(struct esp *esp, struct esp_target_data *tp)
1500 int size = 8 << esp->msg_in[3];
1503 if (esp->rev != FASHME)
1506 if (size != 8 && size != 16)
1509 if (!(tp->flags & ESP_TGT_NEGO_WIDE))
1512 cfg3 = tp->esp_config3;
1514 tp->flags |= ESP_TGT_WIDE;
1515 cfg3 |= ESP_CONFIG3_EWIDE;
1517 tp->flags &= ~ESP_TGT_WIDE;
1518 cfg3 &= ~ESP_CONFIG3_EWIDE;
1520 tp->esp_config3 = cfg3;
1521 esp->prev_cfg3 = cfg3;
1522 esp_write8(cfg3, ESP_CFG3);
1524 tp->flags &= ~ESP_TGT_NEGO_WIDE;
1526 spi_period(tp->starget) = 0;
1527 spi_offset(tp->starget) = 0;
1528 if (!esp_need_to_nego_sync(tp)) {
1529 tp->flags &= ~ESP_TGT_CHECK_NEGO;
1530 scsi_esp_cmd(esp, ESP_CMD_RATN);
1533 spi_populate_sync_msg(&esp->msg_out[0],
1534 tp->nego_goal_period,
1535 tp->nego_goal_offset);
1536 tp->flags |= ESP_TGT_NEGO_SYNC;
1537 scsi_esp_cmd(esp, ESP_CMD_SATN);
1542 esp->msg_out[0] = MESSAGE_REJECT;
1543 esp->msg_out_len = 1;
1544 scsi_esp_cmd(esp, ESP_CMD_SATN);
1547 static void esp_msgin_extended(struct esp *esp)
1549 struct esp_cmd_entry *ent = esp->active_cmd;
1550 struct scsi_cmnd *cmd = ent->cmd;
1551 struct esp_target_data *tp;
1552 int tgt = cmd->device->id;
1554 tp = &esp->target[tgt];
1555 if (esp->msg_in[2] == EXTENDED_SDTR) {
1556 esp_msgin_sdtr(esp, tp);
1559 if (esp->msg_in[2] == EXTENDED_WDTR) {
1560 esp_msgin_wdtr(esp, tp);
1564 shost_printk(KERN_INFO, esp->host,
1565 "Unexpected extended msg type %x\n", esp->msg_in[2]);
1567 esp->msg_out[0] = ABORT_TASK_SET;
1568 esp->msg_out_len = 1;
1569 scsi_esp_cmd(esp, ESP_CMD_SATN);
1572 /* Analyze msgin bytes received from target so far. Return non-zero
1573 * if there are more bytes needed to complete the message.
1575 static int esp_msgin_process(struct esp *esp)
1577 u8 msg0 = esp->msg_in[0];
1578 int len = esp->msg_in_len;
1582 shost_printk(KERN_INFO, esp->host,
1583 "Unexpected msgin identify\n");
1588 case EXTENDED_MESSAGE:
1591 if (len < esp->msg_in[1] + 2)
1593 esp_msgin_extended(esp);
1596 case IGNORE_WIDE_RESIDUE: {
1597 struct esp_cmd_entry *ent;
1598 struct esp_cmd_priv *spriv;
1602 if (esp->msg_in[1] != 1)
1605 ent = esp->active_cmd;
1606 spriv = ESP_CMD_PRIV(ent->cmd);
1608 if (spriv->cur_residue == sg_dma_len(spriv->cur_sg)) {
1610 spriv->cur_residue = 1;
1612 spriv->cur_residue++;
1613 spriv->tot_residue++;
1618 case RESTORE_POINTERS:
1619 esp_restore_pointers(esp, esp->active_cmd);
1622 esp_save_pointers(esp, esp->active_cmd);
1625 case COMMAND_COMPLETE:
1627 struct esp_cmd_entry *ent = esp->active_cmd;
1629 ent->message = msg0;
1630 esp_event(esp, ESP_EVENT_FREE_BUS);
1631 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1634 case MESSAGE_REJECT:
1635 esp_msgin_reject(esp);
1640 esp->msg_out[0] = MESSAGE_REJECT;
1641 esp->msg_out_len = 1;
1642 scsi_esp_cmd(esp, ESP_CMD_SATN);
1647 static int esp_process_event(struct esp *esp)
1653 esp_log_event("process event %d phase %x\n",
1654 esp->event, esp->sreg & ESP_STAT_PMASK);
1655 switch (esp->event) {
1656 case ESP_EVENT_CHECK_PHASE:
1657 switch (esp->sreg & ESP_STAT_PMASK) {
1659 esp_event(esp, ESP_EVENT_DATA_OUT);
1662 esp_event(esp, ESP_EVENT_DATA_IN);
1665 esp_flush_fifo(esp);
1666 scsi_esp_cmd(esp, ESP_CMD_ICCSEQ);
1667 esp_event(esp, ESP_EVENT_STATUS);
1668 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1672 esp_event(esp, ESP_EVENT_MSGOUT);
1676 esp_event(esp, ESP_EVENT_MSGIN);
1680 esp_event(esp, ESP_EVENT_CMD_START);
1684 shost_printk(KERN_INFO, esp->host,
1685 "Unexpected phase, sreg=%02x\n",
1687 esp_schedule_reset(esp);
1693 case ESP_EVENT_DATA_IN:
1697 case ESP_EVENT_DATA_OUT: {
1698 struct esp_cmd_entry *ent = esp->active_cmd;
1699 struct scsi_cmnd *cmd = ent->cmd;
1700 dma_addr_t dma_addr = esp_cur_dma_addr(ent, cmd);
1701 unsigned int dma_len = esp_cur_dma_len(ent, cmd);
1703 if (esp->rev == ESP100)
1704 scsi_esp_cmd(esp, ESP_CMD_NULL);
1707 ent->flags |= ESP_CMD_FLAG_WRITE;
1709 ent->flags &= ~ESP_CMD_FLAG_WRITE;
1711 if (esp->ops->dma_length_limit)
1712 dma_len = esp->ops->dma_length_limit(esp, dma_addr,
1715 dma_len = esp_dma_length_limit(esp, dma_addr, dma_len);
1717 esp->data_dma_len = dma_len;
1720 shost_printk(KERN_ERR, esp->host,
1721 "DMA length is zero!\n");
1722 shost_printk(KERN_ERR, esp->host,
1723 "cur adr[%08llx] len[%08x]\n",
1724 (unsigned long long)esp_cur_dma_addr(ent, cmd),
1725 esp_cur_dma_len(ent, cmd));
1726 esp_schedule_reset(esp);
1730 esp_log_datastart("start data addr[%08llx] len[%u] write(%d)\n",
1731 (unsigned long long)dma_addr, dma_len, write);
1733 esp->ops->send_dma_cmd(esp, dma_addr, dma_len, dma_len,
1734 write, ESP_CMD_DMA | ESP_CMD_TI);
1735 esp_event(esp, ESP_EVENT_DATA_DONE);
1738 case ESP_EVENT_DATA_DONE: {
1739 struct esp_cmd_entry *ent = esp->active_cmd;
1740 struct scsi_cmnd *cmd = ent->cmd;
1743 if (esp->ops->dma_error(esp)) {
1744 shost_printk(KERN_INFO, esp->host,
1745 "data done, DMA error, resetting\n");
1746 esp_schedule_reset(esp);
1750 if (ent->flags & ESP_CMD_FLAG_WRITE) {
1751 /* XXX parity errors, etc. XXX */
1753 esp->ops->dma_drain(esp);
1755 esp->ops->dma_invalidate(esp);
1757 if (esp->ireg != ESP_INTR_BSERV) {
1758 /* We should always see exactly a bus-service
1759 * interrupt at the end of a successful transfer.
1761 shost_printk(KERN_INFO, esp->host,
1762 "data done, not BSERV, resetting\n");
1763 esp_schedule_reset(esp);
1767 bytes_sent = esp_data_bytes_sent(esp, ent, cmd);
1769 esp_log_datadone("data done flgs[%x] sent[%d]\n",
1770 ent->flags, bytes_sent);
1772 if (bytes_sent < 0) {
1773 /* XXX force sync mode for this target XXX */
1774 esp_schedule_reset(esp);
1778 esp_advance_dma(esp, ent, cmd, bytes_sent);
1779 esp_event(esp, ESP_EVENT_CHECK_PHASE);
1783 case ESP_EVENT_STATUS: {
1784 struct esp_cmd_entry *ent = esp->active_cmd;
1786 if (esp->ireg & ESP_INTR_FDONE) {
1787 ent->status = esp_read8(ESP_FDATA);
1788 ent->message = esp_read8(ESP_FDATA);
1789 scsi_esp_cmd(esp, ESP_CMD_MOK);
1790 } else if (esp->ireg == ESP_INTR_BSERV) {
1791 ent->status = esp_read8(ESP_FDATA);
1792 ent->message = 0xff;
1793 esp_event(esp, ESP_EVENT_MSGIN);
1797 if (ent->message != COMMAND_COMPLETE) {
1798 shost_printk(KERN_INFO, esp->host,
1799 "Unexpected message %x in status\n",
1801 esp_schedule_reset(esp);
1805 esp_event(esp, ESP_EVENT_FREE_BUS);
1806 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1809 case ESP_EVENT_FREE_BUS: {
1810 struct esp_cmd_entry *ent = esp->active_cmd;
1811 struct scsi_cmnd *cmd = ent->cmd;
1813 if (ent->message == COMMAND_COMPLETE ||
1814 ent->message == DISCONNECT)
1815 scsi_esp_cmd(esp, ESP_CMD_ESEL);
1817 if (ent->message == COMMAND_COMPLETE) {
1818 esp_log_cmddone("Command done status[%x] message[%x]\n",
1819 ent->status, ent->message);
1820 if (ent->status == SAM_STAT_TASK_SET_FULL)
1821 esp_event_queue_full(esp, ent);
1823 if (ent->status == SAM_STAT_CHECK_CONDITION &&
1824 !(ent->flags & ESP_CMD_FLAG_AUTOSENSE)) {
1825 ent->flags |= ESP_CMD_FLAG_AUTOSENSE;
1826 esp_autosense(esp, ent);
1828 esp_cmd_is_done(esp, ent, cmd,
1829 compose_result(ent->status,
1833 } else if (ent->message == DISCONNECT) {
1834 esp_log_disconnect("Disconnecting tgt[%d] tag[%x:%x]\n",
1836 ent->tag[0], ent->tag[1]);
1838 esp->active_cmd = NULL;
1839 esp_maybe_execute_command(esp);
1841 shost_printk(KERN_INFO, esp->host,
1842 "Unexpected message %x in freebus\n",
1844 esp_schedule_reset(esp);
1847 if (esp->active_cmd)
1848 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1851 case ESP_EVENT_MSGOUT: {
1852 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1854 if (esp_debug & ESP_DEBUG_MSGOUT) {
1856 printk("ESP: Sending message [ ");
1857 for (i = 0; i < esp->msg_out_len; i++)
1858 printk("%02x ", esp->msg_out[i]);
1862 if (esp->rev == FASHME) {
1865 /* Always use the fifo. */
1866 for (i = 0; i < esp->msg_out_len; i++) {
1867 esp_write8(esp->msg_out[i], ESP_FDATA);
1868 esp_write8(0, ESP_FDATA);
1870 scsi_esp_cmd(esp, ESP_CMD_TI);
1872 if (esp->msg_out_len == 1) {
1873 esp_write8(esp->msg_out[0], ESP_FDATA);
1874 scsi_esp_cmd(esp, ESP_CMD_TI);
1877 memcpy(esp->command_block,
1881 esp->ops->send_dma_cmd(esp,
1882 esp->command_block_dma,
1886 ESP_CMD_DMA|ESP_CMD_TI);
1889 esp_event(esp, ESP_EVENT_MSGOUT_DONE);
1892 case ESP_EVENT_MSGOUT_DONE:
1893 if (esp->rev == FASHME) {
1894 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1896 if (esp->msg_out_len > 1)
1897 esp->ops->dma_invalidate(esp);
1900 if (!(esp->ireg & ESP_INTR_DC)) {
1901 if (esp->rev != FASHME)
1902 scsi_esp_cmd(esp, ESP_CMD_NULL);
1904 esp_event(esp, ESP_EVENT_CHECK_PHASE);
1906 case ESP_EVENT_MSGIN:
1907 if (esp->ireg & ESP_INTR_BSERV) {
1908 if (esp->rev == FASHME) {
1909 if (!(esp_read8(ESP_STATUS2) &
1911 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1913 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1914 if (esp->rev == ESP100)
1915 scsi_esp_cmd(esp, ESP_CMD_NULL);
1917 scsi_esp_cmd(esp, ESP_CMD_TI);
1918 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1921 if (esp->ireg & ESP_INTR_FDONE) {
1924 if (esp->rev == FASHME)
1927 val = esp_read8(ESP_FDATA);
1928 esp->msg_in[esp->msg_in_len++] = val;
1930 esp_log_msgin("Got msgin byte %x\n", val);
1932 if (!esp_msgin_process(esp))
1933 esp->msg_in_len = 0;
1935 if (esp->rev == FASHME)
1936 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1938 scsi_esp_cmd(esp, ESP_CMD_MOK);
1940 if (esp->event != ESP_EVENT_FREE_BUS)
1941 esp_event(esp, ESP_EVENT_CHECK_PHASE);
1943 shost_printk(KERN_INFO, esp->host,
1944 "MSGIN neither BSERV not FDON, resetting");
1945 esp_schedule_reset(esp);
1949 case ESP_EVENT_CMD_START:
1950 memcpy(esp->command_block, esp->cmd_bytes_ptr,
1951 esp->cmd_bytes_left);
1952 if (esp->rev == FASHME)
1953 scsi_esp_cmd(esp, ESP_CMD_FLUSH);
1954 esp->ops->send_dma_cmd(esp, esp->command_block_dma,
1955 esp->cmd_bytes_left, 16, 0,
1956 ESP_CMD_DMA | ESP_CMD_TI);
1957 esp_event(esp, ESP_EVENT_CMD_DONE);
1958 esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
1960 case ESP_EVENT_CMD_DONE:
1961 esp->ops->dma_invalidate(esp);
1962 if (esp->ireg & ESP_INTR_BSERV) {
1963 esp_event(esp, ESP_EVENT_CHECK_PHASE);
1966 esp_schedule_reset(esp);
1970 case ESP_EVENT_RESET:
1971 scsi_esp_cmd(esp, ESP_CMD_RS);
1975 shost_printk(KERN_INFO, esp->host,
1976 "Unexpected event %x, resetting\n", esp->event);
1977 esp_schedule_reset(esp);
1984 static void esp_reset_cleanup_one(struct esp *esp, struct esp_cmd_entry *ent)
1986 struct scsi_cmnd *cmd = ent->cmd;
1988 esp_unmap_dma(esp, cmd);
1989 esp_free_lun_tag(ent, cmd->device->hostdata);
1990 cmd->result = DID_RESET << 16;
1992 if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
1993 esp->ops->unmap_single(esp, ent->sense_dma,
1994 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
1995 ent->sense_ptr = NULL;
1998 cmd->scsi_done(cmd);
1999 list_del(&ent->list);
2000 esp_put_ent(esp, ent);
2003 static void esp_clear_hold(struct scsi_device *dev, void *data)
2005 struct esp_lun_data *lp = dev->hostdata;
2007 BUG_ON(lp->num_tagged);
2011 static void esp_reset_cleanup(struct esp *esp)
2013 struct esp_cmd_entry *ent, *tmp;
2016 list_for_each_entry_safe(ent, tmp, &esp->queued_cmds, list) {
2017 struct scsi_cmnd *cmd = ent->cmd;
2019 list_del(&ent->list);
2020 cmd->result = DID_RESET << 16;
2021 cmd->scsi_done(cmd);
2022 esp_put_ent(esp, ent);
2025 list_for_each_entry_safe(ent, tmp, &esp->active_cmds, list) {
2026 if (ent == esp->active_cmd)
2027 esp->active_cmd = NULL;
2028 esp_reset_cleanup_one(esp, ent);
2031 BUG_ON(esp->active_cmd != NULL);
2033 /* Force renegotiation of sync/wide transfers. */
2034 for (i = 0; i < ESP_MAX_TARGET; i++) {
2035 struct esp_target_data *tp = &esp->target[i];
2039 tp->esp_config3 &= ~(ESP_CONFIG3_EWIDE |
2042 tp->flags &= ~ESP_TGT_WIDE;
2043 tp->flags |= ESP_TGT_CHECK_NEGO;
2046 __starget_for_each_device(tp->starget, NULL,
2049 esp->flags &= ~ESP_FLAG_RESETTING;
2052 /* Runs under host->lock */
2053 static void __esp_interrupt(struct esp *esp)
2055 int finish_reset, intr_done;
2059 * Once INTRPT is read STATUS and SSTEP are cleared.
2061 esp->sreg = esp_read8(ESP_STATUS);
2062 esp->seqreg = esp_read8(ESP_SSTEP);
2063 esp->ireg = esp_read8(ESP_INTRPT);
2065 if (esp->flags & ESP_FLAG_RESETTING) {
2068 if (esp_check_gross_error(esp))
2071 finish_reset = esp_check_spur_intr(esp);
2072 if (finish_reset < 0)
2076 if (esp->ireg & ESP_INTR_SR)
2080 esp_reset_cleanup(esp);
2081 if (esp->eh_reset) {
2082 complete(esp->eh_reset);
2083 esp->eh_reset = NULL;
2088 phase = (esp->sreg & ESP_STAT_PMASK);
2089 if (esp->rev == FASHME) {
2090 if (((phase != ESP_DIP && phase != ESP_DOP) &&
2091 esp->select_state == ESP_SELECT_NONE &&
2092 esp->event != ESP_EVENT_STATUS &&
2093 esp->event != ESP_EVENT_DATA_DONE) ||
2094 (esp->ireg & ESP_INTR_RSEL)) {
2095 esp->sreg2 = esp_read8(ESP_STATUS2);
2096 if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
2097 (esp->sreg2 & ESP_STAT2_F1BYTE))
2102 esp_log_intr("intr sreg[%02x] seqreg[%02x] "
2103 "sreg2[%02x] ireg[%02x]\n",
2104 esp->sreg, esp->seqreg, esp->sreg2, esp->ireg);
2108 if (esp->ireg & (ESP_INTR_S | ESP_INTR_SATN | ESP_INTR_IC)) {
2109 shost_printk(KERN_INFO, esp->host,
2110 "unexpected IREG %02x\n", esp->ireg);
2111 if (esp->ireg & ESP_INTR_IC)
2112 esp_dump_cmd_log(esp);
2114 esp_schedule_reset(esp);
2116 if (!(esp->ireg & ESP_INTR_RSEL)) {
2117 /* Some combination of FDONE, BSERV, DC. */
2118 if (esp->select_state != ESP_SELECT_NONE)
2119 intr_done = esp_finish_select(esp);
2120 } else if (esp->ireg & ESP_INTR_RSEL) {
2121 if (esp->active_cmd)
2122 (void) esp_finish_select(esp);
2123 intr_done = esp_reconnect(esp);
2127 intr_done = esp_process_event(esp);
2130 irqreturn_t scsi_esp_intr(int irq, void *dev_id)
2132 struct esp *esp = dev_id;
2133 unsigned long flags;
2136 spin_lock_irqsave(esp->host->host_lock, flags);
2138 if (esp->ops->irq_pending(esp)) {
2143 __esp_interrupt(esp);
2144 if (!(esp->flags & ESP_FLAG_QUICKIRQ_CHECK))
2146 esp->flags &= ~ESP_FLAG_QUICKIRQ_CHECK;
2148 for (i = 0; i < ESP_QUICKIRQ_LIMIT; i++) {
2149 if (esp->ops->irq_pending(esp))
2152 if (i == ESP_QUICKIRQ_LIMIT)
2156 spin_unlock_irqrestore(esp->host->host_lock, flags);
2160 EXPORT_SYMBOL(scsi_esp_intr);
2162 static void esp_get_revision(struct esp *esp)
2166 esp->config1 = (ESP_CONFIG1_PENABLE | (esp->scsi_id & 7));
2167 esp->config2 = (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY);
2168 esp_write8(esp->config2, ESP_CFG2);
2170 val = esp_read8(ESP_CFG2);
2171 val &= ~ESP_CONFIG2_MAGIC;
2172 if (val != (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY)) {
2173 /* If what we write to cfg2 does not come back, cfg2 is not
2174 * implemented, therefore this must be a plain esp100.
2179 esp_set_all_config3(esp, 5);
2181 esp_write8(esp->config2, ESP_CFG2);
2182 esp_write8(0, ESP_CFG3);
2183 esp_write8(esp->prev_cfg3, ESP_CFG3);
2185 val = esp_read8(ESP_CFG3);
2187 /* The cfg2 register is implemented, however
2188 * cfg3 is not, must be esp100a.
2192 esp_set_all_config3(esp, 0);
2194 esp_write8(esp->prev_cfg3, ESP_CFG3);
2196 /* All of cfg{1,2,3} implemented, must be one of
2197 * the fas variants, figure out which one.
2199 if (esp->cfact == 0 || esp->cfact > ESP_CCF_F5) {
2201 esp->sync_defp = SYNC_DEFP_FAST;
2206 esp_write8(esp->config2, ESP_CFG2);
2211 static void esp_init_swstate(struct esp *esp)
2215 INIT_LIST_HEAD(&esp->queued_cmds);
2216 INIT_LIST_HEAD(&esp->active_cmds);
2217 INIT_LIST_HEAD(&esp->esp_cmd_pool);
2219 /* Start with a clear state, domain validation (via ->slave_configure,
2220 * spi_dv_device()) will attempt to enable SYNC, WIDE, and tagged
2223 for (i = 0 ; i < ESP_MAX_TARGET; i++) {
2224 esp->target[i].flags = 0;
2225 esp->target[i].nego_goal_period = 0;
2226 esp->target[i].nego_goal_offset = 0;
2227 esp->target[i].nego_goal_width = 0;
2228 esp->target[i].nego_goal_tags = 0;
2232 /* This places the ESP into a known state at boot time. */
2233 static void esp_bootup_reset(struct esp *esp)
2238 esp->ops->reset_dma(esp);
2243 /* Reset the SCSI bus, but tell ESP not to generate an irq */
2244 val = esp_read8(ESP_CFG1);
2245 val |= ESP_CONFIG1_SRRDISAB;
2246 esp_write8(val, ESP_CFG1);
2248 scsi_esp_cmd(esp, ESP_CMD_RS);
2251 esp_write8(esp->config1, ESP_CFG1);
2253 /* Eat any bitrot in the chip and we are done... */
2254 esp_read8(ESP_INTRPT);
2257 static void esp_set_clock_params(struct esp *esp)
2262 /* This is getting messy but it has to be done correctly or else
2263 * you get weird behavior all over the place. We are trying to
2264 * basically figure out three pieces of information.
2266 * a) Clock Conversion Factor
2268 * This is a representation of the input crystal clock frequency
2269 * going into the ESP on this machine. Any operation whose timing
2270 * is longer than 400ns depends on this value being correct. For
2271 * example, you'll get blips for arbitration/selection during high
2272 * load or with multiple targets if this is not set correctly.
2274 * b) Selection Time-Out
2276 * The ESP isn't very bright and will arbitrate for the bus and try
2277 * to select a target forever if you let it. This value tells the
2278 * ESP when it has taken too long to negotiate and that it should
2279 * interrupt the CPU so we can see what happened. The value is
2280 * computed as follows (from NCR/Symbios chip docs).
2282 * (Time Out Period) * (Input Clock)
2283 * STO = ----------------------------------
2284 * (8192) * (Clock Conversion Factor)
2286 * We use a time out period of 250ms (ESP_BUS_TIMEOUT).
2288 * c) Imperical constants for synchronous offset and transfer period
2291 * This entails the smallest and largest sync period we could ever
2292 * handle on this ESP.
2296 ccf = ((fhz / 1000000) + 4) / 5;
2300 /* If we can't find anything reasonable, just assume 20MHZ.
2301 * This is the clock frequency of the older sun4c's where I've
2302 * been unable to find the clock-frequency PROM property. All
2303 * other machines provide useful values it seems.
2305 if (fhz <= 5000000 || ccf < 1 || ccf > 8) {
2310 esp->cfact = (ccf == 8 ? 0 : ccf);
2312 esp->ccycle = ESP_HZ_TO_CYCLE(fhz);
2313 esp->ctick = ESP_TICK(ccf, esp->ccycle);
2314 esp->neg_defp = ESP_NEG_DEFP(fhz, ccf);
2315 esp->sync_defp = SYNC_DEFP_SLOW;
2318 static const char *esp_chip_names[] = {
2328 static struct scsi_transport_template *esp_transport_template;
2330 int scsi_esp_register(struct esp *esp, struct device *dev)
2332 static int instance;
2336 esp->num_tags = ESP_DEFAULT_TAGS;
2337 else if (esp->num_tags >= ESP_MAX_TAG)
2338 esp->num_tags = ESP_MAX_TAG - 1;
2339 esp->host->transportt = esp_transport_template;
2340 esp->host->max_lun = ESP_MAX_LUN;
2341 esp->host->cmd_per_lun = 2;
2342 esp->host->unique_id = instance;
2344 esp_set_clock_params(esp);
2346 esp_get_revision(esp);
2348 esp_init_swstate(esp);
2350 esp_bootup_reset(esp);
2352 dev_printk(KERN_INFO, dev, "esp%u: regs[%1p:%1p] irq[%u]\n",
2353 esp->host->unique_id, esp->regs, esp->dma_regs,
2355 dev_printk(KERN_INFO, dev,
2356 "esp%u: is a %s, %u MHz (ccf=%u), SCSI ID %u\n",
2357 esp->host->unique_id, esp_chip_names[esp->rev],
2358 esp->cfreq / 1000000, esp->cfact, esp->scsi_id);
2360 /* Let the SCSI bus reset settle. */
2361 ssleep(esp_bus_reset_settle);
2363 err = scsi_add_host(esp->host, dev);
2369 scsi_scan_host(esp->host);
2373 EXPORT_SYMBOL(scsi_esp_register);
2375 void scsi_esp_unregister(struct esp *esp)
2377 scsi_remove_host(esp->host);
2379 EXPORT_SYMBOL(scsi_esp_unregister);
2381 static int esp_target_alloc(struct scsi_target *starget)
2383 struct esp *esp = shost_priv(dev_to_shost(&starget->dev));
2384 struct esp_target_data *tp = &esp->target[starget->id];
2386 tp->starget = starget;
2391 static void esp_target_destroy(struct scsi_target *starget)
2393 struct esp *esp = shost_priv(dev_to_shost(&starget->dev));
2394 struct esp_target_data *tp = &esp->target[starget->id];
2399 static int esp_slave_alloc(struct scsi_device *dev)
2401 struct esp *esp = shost_priv(dev->host);
2402 struct esp_target_data *tp = &esp->target[dev->id];
2403 struct esp_lun_data *lp;
2405 lp = kzalloc(sizeof(*lp), GFP_KERNEL);
2410 spi_min_period(tp->starget) = esp->min_period;
2411 spi_max_offset(tp->starget) = 15;
2413 if (esp->flags & ESP_FLAG_WIDE_CAPABLE)
2414 spi_max_width(tp->starget) = 1;
2416 spi_max_width(tp->starget) = 0;
2421 static int esp_slave_configure(struct scsi_device *dev)
2423 struct esp *esp = shost_priv(dev->host);
2424 struct esp_target_data *tp = &esp->target[dev->id];
2426 if (dev->tagged_supported)
2427 scsi_change_queue_depth(dev, esp->num_tags);
2429 tp->flags |= ESP_TGT_DISCONNECT;
2431 if (!spi_initial_dv(dev->sdev_target))
2437 static void esp_slave_destroy(struct scsi_device *dev)
2439 struct esp_lun_data *lp = dev->hostdata;
2442 dev->hostdata = NULL;
2445 static int esp_eh_abort_handler(struct scsi_cmnd *cmd)
2447 struct esp *esp = shost_priv(cmd->device->host);
2448 struct esp_cmd_entry *ent, *tmp;
2449 struct completion eh_done;
2450 unsigned long flags;
2452 /* XXX This helps a lot with debugging but might be a bit
2453 * XXX much for the final driver.
2455 spin_lock_irqsave(esp->host->host_lock, flags);
2456 shost_printk(KERN_ERR, esp->host, "Aborting command [%p:%02x]\n",
2458 ent = esp->active_cmd;
2460 shost_printk(KERN_ERR, esp->host,
2461 "Current command [%p:%02x]\n",
2462 ent->cmd, ent->cmd->cmnd[0]);
2463 list_for_each_entry(ent, &esp->queued_cmds, list) {
2464 shost_printk(KERN_ERR, esp->host, "Queued command [%p:%02x]\n",
2465 ent->cmd, ent->cmd->cmnd[0]);
2467 list_for_each_entry(ent, &esp->active_cmds, list) {
2468 shost_printk(KERN_ERR, esp->host, " Active command [%p:%02x]\n",
2469 ent->cmd, ent->cmd->cmnd[0]);
2471 esp_dump_cmd_log(esp);
2472 spin_unlock_irqrestore(esp->host->host_lock, flags);
2474 spin_lock_irqsave(esp->host->host_lock, flags);
2477 list_for_each_entry(tmp, &esp->queued_cmds, list) {
2478 if (tmp->cmd == cmd) {
2485 /* Easiest case, we didn't even issue the command
2486 * yet so it is trivial to abort.
2488 list_del(&ent->list);
2490 cmd->result = DID_ABORT << 16;
2491 cmd->scsi_done(cmd);
2493 esp_put_ent(esp, ent);
2498 init_completion(&eh_done);
2500 ent = esp->active_cmd;
2501 if (ent && ent->cmd == cmd) {
2502 /* Command is the currently active command on
2503 * the bus. If we already have an output message
2506 if (esp->msg_out_len)
2509 /* Send out an abort, encouraging the target to
2510 * go to MSGOUT phase by asserting ATN.
2512 esp->msg_out[0] = ABORT_TASK_SET;
2513 esp->msg_out_len = 1;
2514 ent->eh_done = &eh_done;
2516 scsi_esp_cmd(esp, ESP_CMD_SATN);
2518 /* The command is disconnected. This is not easy to
2519 * abort. For now we fail and let the scsi error
2520 * handling layer go try a scsi bus reset or host
2523 * What we could do is put together a scsi command
2524 * solely for the purpose of sending an abort message
2525 * to the target. Coming up with all the code to
2526 * cook up scsi commands, special case them everywhere,
2527 * etc. is for questionable gain and it would be better
2528 * if the generic scsi error handling layer could do at
2529 * least some of that for us.
2531 * Anyways this is an area for potential future improvement
2537 spin_unlock_irqrestore(esp->host->host_lock, flags);
2539 if (!wait_for_completion_timeout(&eh_done, 5 * HZ)) {
2540 spin_lock_irqsave(esp->host->host_lock, flags);
2541 ent->eh_done = NULL;
2542 spin_unlock_irqrestore(esp->host->host_lock, flags);
2550 spin_unlock_irqrestore(esp->host->host_lock, flags);
2554 /* XXX This might be a good location to set ESP_TGT_BROKEN
2555 * XXX since we know which target/lun in particular is
2556 * XXX causing trouble.
2558 spin_unlock_irqrestore(esp->host->host_lock, flags);
2562 static int esp_eh_bus_reset_handler(struct scsi_cmnd *cmd)
2564 struct esp *esp = shost_priv(cmd->device->host);
2565 struct completion eh_reset;
2566 unsigned long flags;
2568 init_completion(&eh_reset);
2570 spin_lock_irqsave(esp->host->host_lock, flags);
2572 esp->eh_reset = &eh_reset;
2574 /* XXX This is too simple... We should add lots of
2575 * XXX checks here so that if we find that the chip is
2576 * XXX very wedged we return failure immediately so
2577 * XXX that we can perform a full chip reset.
2579 esp->flags |= ESP_FLAG_RESETTING;
2580 scsi_esp_cmd(esp, ESP_CMD_RS);
2582 spin_unlock_irqrestore(esp->host->host_lock, flags);
2584 ssleep(esp_bus_reset_settle);
2586 if (!wait_for_completion_timeout(&eh_reset, 5 * HZ)) {
2587 spin_lock_irqsave(esp->host->host_lock, flags);
2588 esp->eh_reset = NULL;
2589 spin_unlock_irqrestore(esp->host->host_lock, flags);
2597 /* All bets are off, reset the entire device. */
2598 static int esp_eh_host_reset_handler(struct scsi_cmnd *cmd)
2600 struct esp *esp = shost_priv(cmd->device->host);
2601 unsigned long flags;
2603 spin_lock_irqsave(esp->host->host_lock, flags);
2604 esp_bootup_reset(esp);
2605 esp_reset_cleanup(esp);
2606 spin_unlock_irqrestore(esp->host->host_lock, flags);
2608 ssleep(esp_bus_reset_settle);
2613 static const char *esp_info(struct Scsi_Host *host)
2618 struct scsi_host_template scsi_esp_template = {
2619 .module = THIS_MODULE,
2622 .queuecommand = esp_queuecommand,
2623 .target_alloc = esp_target_alloc,
2624 .target_destroy = esp_target_destroy,
2625 .slave_alloc = esp_slave_alloc,
2626 .slave_configure = esp_slave_configure,
2627 .slave_destroy = esp_slave_destroy,
2628 .eh_abort_handler = esp_eh_abort_handler,
2629 .eh_bus_reset_handler = esp_eh_bus_reset_handler,
2630 .eh_host_reset_handler = esp_eh_host_reset_handler,
2633 .sg_tablesize = SG_ALL,
2634 .use_clustering = ENABLE_CLUSTERING,
2635 .max_sectors = 0xffff,
2636 .skip_settle_delay = 1,
2639 EXPORT_SYMBOL(scsi_esp_template);
2641 static void esp_get_signalling(struct Scsi_Host *host)
2643 struct esp *esp = shost_priv(host);
2644 enum spi_signal_type type;
2646 if (esp->flags & ESP_FLAG_DIFFERENTIAL)
2647 type = SPI_SIGNAL_HVD;
2649 type = SPI_SIGNAL_SE;
2651 spi_signalling(host) = type;
2654 static void esp_set_offset(struct scsi_target *target, int offset)
2656 struct Scsi_Host *host = dev_to_shost(target->dev.parent);
2657 struct esp *esp = shost_priv(host);
2658 struct esp_target_data *tp = &esp->target[target->id];
2660 if (esp->flags & ESP_FLAG_DISABLE_SYNC)
2661 tp->nego_goal_offset = 0;
2663 tp->nego_goal_offset = offset;
2664 tp->flags |= ESP_TGT_CHECK_NEGO;
2667 static void esp_set_period(struct scsi_target *target, int period)
2669 struct Scsi_Host *host = dev_to_shost(target->dev.parent);
2670 struct esp *esp = shost_priv(host);
2671 struct esp_target_data *tp = &esp->target[target->id];
2673 tp->nego_goal_period = period;
2674 tp->flags |= ESP_TGT_CHECK_NEGO;
2677 static void esp_set_width(struct scsi_target *target, int width)
2679 struct Scsi_Host *host = dev_to_shost(target->dev.parent);
2680 struct esp *esp = shost_priv(host);
2681 struct esp_target_data *tp = &esp->target[target->id];
2683 tp->nego_goal_width = (width ? 1 : 0);
2684 tp->flags |= ESP_TGT_CHECK_NEGO;
2687 static struct spi_function_template esp_transport_ops = {
2688 .set_offset = esp_set_offset,
2690 .set_period = esp_set_period,
2692 .set_width = esp_set_width,
2694 .get_signalling = esp_get_signalling,
2697 static int __init esp_init(void)
2699 BUILD_BUG_ON(sizeof(struct scsi_pointer) <
2700 sizeof(struct esp_cmd_priv));
2702 esp_transport_template = spi_attach_transport(&esp_transport_ops);
2703 if (!esp_transport_template)
2709 static void __exit esp_exit(void)
2711 spi_release_transport(esp_transport_template);
2714 MODULE_DESCRIPTION("ESP SCSI driver core");
2715 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
2716 MODULE_LICENSE("GPL");
2717 MODULE_VERSION(DRV_VERSION);
2719 module_param(esp_bus_reset_settle, int, 0);
2720 MODULE_PARM_DESC(esp_bus_reset_settle,
2721 "ESP scsi bus reset delay in seconds");
2723 module_param(esp_debug, int, 0);
2724 MODULE_PARM_DESC(esp_debug,
2725 "ESP bitmapped debugging message enable value:\n"
2726 " 0x00000001 Log interrupt events\n"
2727 " 0x00000002 Log scsi commands\n"
2728 " 0x00000004 Log resets\n"
2729 " 0x00000008 Log message in events\n"
2730 " 0x00000010 Log message out events\n"
2731 " 0x00000020 Log command completion\n"
2732 " 0x00000040 Log disconnects\n"
2733 " 0x00000080 Log data start\n"
2734 " 0x00000100 Log data done\n"
2735 " 0x00000200 Log reconnects\n"
2736 " 0x00000400 Log auto-sense data\n"
2739 module_init(esp_init);
2740 module_exit(esp_exit);