6 #include <linux/kernel.h>
7 #include <linux/types.h>
8 #include <linux/slab.h>
10 #include <linux/list.h>
13 #include <linux/module.h>
22 #include <scsi/scsi.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"
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
53 #define esp_log_intr(f, a...) \
54 do { if (esp_debug & ESP_DEBUG_INTR) \
58 #define esp_log_reset(f, a...) \
59 do { if (esp_debug & ESP_DEBUG_RESET) \
63 #define esp_log_msgin(f, a...) \
64 do { if (esp_debug & ESP_DEBUG_MSGIN) \
68 #define esp_log_msgout(f, a...) \
69 do { if (esp_debug & ESP_DEBUG_MSGOUT) \
73 #define esp_log_cmddone(f, a...) \
74 do { if (esp_debug & ESP_DEBUG_CMDDONE) \
78 #define esp_log_disconnect(f, a...) \
79 do { if (esp_debug & ESP_DEBUG_DISCONNECT) \
83 #define esp_log_datastart(f, a...) \
84 do { if (esp_debug & ESP_DEBUG_DATASTART) \
88 #define esp_log_datadone(f, a...) \
89 do { if (esp_debug & ESP_DEBUG_DATADONE) \
93 #define esp_log_reconnect(f, a...) \
94 do { if (esp_debug & ESP_DEBUG_RECONNECT) \
98 #define esp_log_autosense(f, a...) \
99 do { if (esp_debug & ESP_DEBUG_AUTOSENSE) \
103 #define esp_read8(REG) esp->ops->esp_read8(esp, REG)
104 #define esp_write8(VAL,REG) esp->ops->esp_write8(esp, VAL, REG)
106 static void esp_log_fill_regs(
struct esp *
esp,
125 esp_log_fill_regs(esp, p);
141 esp_log_fill_regs(esp, p);
148 static void esp_dump_cmd_log(
struct esp *esp)
154 esp->
host->unique_id);
159 esp->
host->unique_id, idx,
162 printk(
"val[%02x] sreg[%02x] seqreg[%02x] "
163 "sreg2[%02x] ireg[%02x] ss[%02x] event[%02x]\n",
168 }
while (idx != stop);
171 static void esp_flush_fifo(
struct esp *esp)
181 esp->
host->unique_id);
189 static void hme_read_fifo(
struct esp *esp)
206 static void esp_set_all_config3(
struct esp *esp,
u8 val)
211 esp->
target[i].esp_config3 = val;
215 static void esp_reset_esp(
struct esp *esp)
232 family_code = (version & 0xf8) >> 3;
233 if (family_code == 0x02)
235 else if (family_code == 0x0a)
271 u8 cfg3 = esp->
target[0].esp_config3;
276 esp_set_all_config3(esp, cfg3);
281 esp_set_all_config3(esp, cfg3);
298 esp_set_all_config3(esp,
299 (esp->
target[0].esp_config3 |
326 static void esp_map_dma(
struct esp *esp,
struct scsi_cmnd *
cmd)
336 spriv->
u.
num_sg = esp->
ops->map_sg(esp, sg, scsi_sg_count(cmd), dir);
341 for (i = 0; i < spriv->
u.
num_sg; i++)
361 static unsigned int esp_cur_dma_len(
struct esp_cmd_entry *ent,
373 static void esp_advance_dma(
struct esp *esp,
struct esp_cmd_entry *ent,
387 esp->
host->unique_id);
390 esp->
host->unique_id,
401 static void esp_unmap_dma(
struct esp *esp,
struct scsi_cmnd *cmd)
409 esp->
ops->unmap_sg(esp, scsi_sglist(cmd), spriv->
u.
num_sg, dir);
412 static void esp_save_pointers(
struct esp *esp,
struct esp_cmd_entry *ent)
426 static void esp_restore_pointers(
struct esp *esp,
struct esp_cmd_entry *ent)
440 static void esp_check_command_len(
struct esp *esp,
struct scsi_cmnd *cmd)
451 static void esp_write_tgt_config3(
struct esp *esp,
int tgt)
454 u8 val = esp->
target[tgt].esp_config3;
463 static void esp_write_tgt_sync(
struct esp *esp,
int tgt)
465 u8 off = esp->
target[tgt].esp_offset;
482 if (dma_len > (1
U << 24))
483 dma_len = (1
U << 24);
493 if (dma_len > (1
U << 16))
494 dma_len = (1
U << 16);
499 base = dma_addr & ((1
U << 24) - 1
U);
500 end = base + dma_len;
501 if (end > (1
U << 24))
503 dma_len = end - base;
595 static void esp_autosense(
struct esp *esp,
struct esp_cmd_entry *ent)
609 esp->
host->unique_id, tgt, lun);
640 esp_write_tgt_sync(esp, tgt);
641 esp_write_tgt_config3(esp, tgt);
651 static struct esp_cmd_entry *find_and_prep_issuable_command(
struct esp *esp)
666 if (!scsi_populate_tag_msg(cmd, &ent->
tag[0])) {
671 if (esp_alloc_lun_tag(ent, lp) < 0)
680 static void esp_maybe_execute_command(
struct esp *esp)
695 ent = find_and_prep_issuable_command(esp);
700 esp_autosense(esp, ent);
715 esp_map_dma(esp, cmd);
716 esp_save_pointers(esp, ent);
718 esp_check_command_len(esp, cmd);
743 if (esp->
rev ==
FASHME && esp_need_to_nego_wide(tp)) {
749 }
else if (esp_need_to_nego_sync(tp)) {
790 for (i = 0; i < cmd->
cmd_len; i++)
815 esp_write_tgt_sync(esp, tgt);
816 esp_write_tgt_config3(esp, tgt);
821 printk(
"ESP: tgt[%d] lun[%d] scsi_cmd [ ", tgt, lun);
822 for (i = 0; i < cmd->
cmd_len; i++)
830 val, 16, 0, start_cmd);
838 if (list_empty(head)) {
843 memset(ret, 0,
sizeof(*ret));
848 static void esp_put_ent(
struct esp *esp,
struct esp_cmd_entry *ent)
853 static void esp_cmd_is_done(
struct esp *esp,
struct esp_cmd_entry *ent,
861 esp_unmap_dma(esp, cmd);
862 esp_free_lun_tag(ent, dev->
hostdata);
888 printk(
"esp%d: tgt[%d] lun[%d] AUTO SENSE[ ",
889 esp->
host->unique_id, tgt, lun);
890 for (i = 0; i < 18; i++)
899 esp_put_ent(esp, ent);
901 esp_maybe_execute_command(esp);
904 static unsigned int compose_result(
unsigned int status,
unsigned int message,
905 unsigned int driver_code)
907 return (status | (message << 8) | (driver_code << 16));
910 static void esp_event_queue_full(
struct esp *esp,
struct esp_cmd_entry *ent)
921 struct esp *esp = shost_priv(dev->
host);
925 ent = esp_get_ent(esp);
938 esp_maybe_execute_command(esp);
945 static
int esp_check_gross_error(
struct esp *esp)
955 esp->host->unique_id, esp->sreg);
962 static int esp_check_spur_intr(
struct esp *esp)
982 if (!esp->
ops->dma_error(esp)) {
990 esp->
host->unique_id);
1001 static void esp_schedule_reset(
struct esp *esp)
1004 __builtin_return_address(0));
1013 static struct esp_cmd_entry *esp_reconnect_with_tag(
struct esp *esp,
1021 esp->
host->unique_id);
1028 if (esp->
ops->irq_pending(esp))
1031 if (i == ESP_QUICKIRQ_LIMIT) {
1033 esp->
host->unique_id);
1045 esp->
host->unique_id);
1065 if (esp->
ops->irq_pending(esp)) {
1073 if (i == ESP_RESELECT_TAG_LIMIT) {
1075 esp->
host->unique_id);
1078 esp->
ops->dma_drain(esp);
1079 esp->
ops->dma_invalidate(esp);
1105 static int esp_reconnect(
struct esp *esp)
1118 target = esp->
fifo[0];
1119 lun = esp->
fifo[1] & 0x7;
1132 if (!bits || (bits & (bits - 1)))
1135 target =
ffs(bits) - 1;
1152 esp_write_tgt_sync(esp, target);
1153 esp_write_tgt_config3(esp, target);
1165 "tgt[%u] lun[%u]\n",
1166 esp->
host->unique_id, target, lun);
1173 ent = esp_reconnect_with_tag(esp, lp);
1187 esp_restore_pointers(esp, ent);
1192 esp_schedule_reset(esp);
1196 static int esp_finish_select(
struct esp *esp)
1200 u8 orig_select_state;
1211 if (esp->
ops->dma_error(esp)) {
1215 esp_schedule_reset(esp);
1216 esp_cmd_is_done(esp, ent, cmd, (
DID_ERROR << 16));
1220 esp->
ops->dma_invalidate(esp);
1230 esp_unmap_dma(esp, cmd);
1231 esp_free_lun_tag(ent, cmd->
device->hostdata);
1281 esp_flush_fifo(esp);
1292 printk(
"ESP: Unexpected selection completion ireg[%x].\n",
1294 esp_schedule_reset(esp);
1298 static int esp_data_bytes_sent(
struct esp *esp,
struct esp_cmd_entry *ent,
1301 int fifo_cnt, ecount, bytes_sent, flush_fifo;
1316 bytes_sent -= ecount;
1319 bytes_sent -= fifo_cnt;
1355 esp_flush_fifo(esp);
1361 u8 scsi_period,
u8 scsi_offset,
1362 u8 esp_stp,
u8 esp_soff)
1376 if (scsi_period < 50) {
1399 static void esp_msgin_reject(
struct esp *esp)
1412 if (!esp_need_to_nego_sync(tp)) {
1430 esp_setsync(esp, tp, 0, 0, 0, 0);
1440 static void esp_msgin_sdtr(
struct esp *esp,
struct esp_target_data *tp)
1456 period = offset = 0;
1459 if (period < esp->min_period)
1462 one_clock = esp->
ccycle / 1000;
1472 esp_setsync(esp, tp, period, offset, stp, offset);
1491 static void esp_msgin_wdtr(
struct esp *esp,
struct esp_target_data *tp)
1499 if (size != 8 && size != 16)
1521 if (!esp_need_to_nego_sync(tp)) {
1540 static void esp_msgin_extended(
struct esp *esp)
1545 int tgt = cmd->
device->id;
1549 esp_msgin_sdtr(esp, tp);
1553 esp_msgin_wdtr(esp, tp);
1557 printk(
"ESP: Unexpected extended msg type %x\n",
1568 static int esp_msgin_process(
struct esp *esp)
1575 printk(
"ESP: Unexpected msgin identify\n");
1583 if (len < esp->
msg_in[1] + 2)
1585 esp_msgin_extended(esp);
1627 esp_msgin_reject(esp);
1639 static int esp_process_event(
struct esp *esp)
1645 switch (esp->
event) {
1655 esp_flush_fifo(esp);
1674 printk(
"ESP: Unexpected phase, sreg=%02x\n",
1676 esp_schedule_reset(esp);
1689 dma_addr_t dma_addr = esp_cur_dma_addr(ent, cmd);
1690 unsigned int dma_len = esp_cur_dma_len(ent, cmd);
1700 if (esp->
ops->dma_length_limit)
1701 dma_len = esp->
ops->dma_length_limit(esp, dma_addr,
1704 dma_len = esp_dma_length_limit(esp, dma_addr, dma_len);
1710 esp->
host->unique_id);
1712 esp->
host->unique_id,
1713 (
unsigned long long)esp_cur_dma_addr(ent, cmd),
1714 esp_cur_dma_len(ent, cmd));
1715 esp_schedule_reset(esp);
1721 (
unsigned long long)dma_addr, dma_len, write);
1723 esp->
ops->send_dma_cmd(esp, dma_addr, dma_len, dma_len,
1733 if (esp->
ops->dma_error(esp)) {
1734 printk(
"ESP: data done, DMA error, resetting\n");
1735 esp_schedule_reset(esp);
1742 esp->
ops->dma_drain(esp);
1744 esp->
ops->dma_invalidate(esp);
1750 printk(
"ESP: data done, not BSERV, resetting\n");
1751 esp_schedule_reset(esp);
1755 bytes_sent = esp_data_bytes_sent(esp, ent, cmd);
1758 ent->
flags, bytes_sent);
1760 if (bytes_sent < 0) {
1762 esp_schedule_reset(esp);
1766 esp_advance_dma(esp, ent, cmd, bytes_sent);
1786 printk(
"ESP: Unexpected message %x in status\n",
1788 esp_schedule_reset(esp);
1809 esp_event_queue_full(esp, ent);
1814 esp_autosense(esp, ent);
1816 esp_cmd_is_done(esp, ent, cmd,
1817 compose_result(ent->
status,
1825 ent->
tag[0], ent->
tag[1]);
1828 esp_maybe_execute_command(esp);
1830 printk(
"ESP: Unexpected message %x in freebus\n",
1832 esp_schedule_reset(esp);
1844 printk(
"ESP: Sending message [ ");
1869 esp->
ops->send_dma_cmd(esp,
1885 esp->
ops->dma_invalidate(esp);
1920 if (!esp_msgin_process(esp))
1931 printk(
"ESP: MSGIN neither BSERV not FDON, resetting");
1932 esp_schedule_reset(esp);
1948 esp->
ops->dma_invalidate(esp);
1953 esp_schedule_reset(esp);
1962 printk(
"ESP: Unexpected event %x, resetting\n",
1964 esp_schedule_reset(esp);
1971 static void esp_reset_cleanup_one(
struct esp *esp,
struct esp_cmd_entry *ent)
1975 esp_unmap_dma(esp, cmd);
1976 esp_free_lun_tag(ent, cmd->
device->hostdata);
1987 esp_put_ent(esp, ent);
1998 static void esp_reset_cleanup(
struct esp *esp)
2009 esp_put_ent(esp, ent);
2015 esp_reset_cleanup_one(esp, ent);
2040 static void __esp_interrupt(
struct esp *esp)
2042 int finish_reset, intr_done;
2050 if (esp_check_gross_error(esp))
2053 finish_reset = esp_check_spur_intr(esp);
2054 if (finish_reset < 0)
2064 esp_reset_cleanup(esp);
2087 "sreg2[%02x] ireg[%02x]\n",
2093 printk(
"ESP: unexpected IREG %02x\n", esp->
ireg);
2095 esp_dump_cmd_log(esp);
2097 esp_schedule_reset(esp);
2102 intr_done = esp_finish_select(esp);
2105 (
void) esp_finish_select(esp);
2106 intr_done = esp_reconnect(esp);
2110 intr_done = esp_process_event(esp);
2115 struct esp *esp =
dev_id;
2116 unsigned long flags;
2121 if (esp->
ops->irq_pending(esp)) {
2126 __esp_interrupt(esp);
2132 if (esp->
ops->irq_pending(esp))
2135 if (i == ESP_QUICKIRQ_LIMIT)
2139 spin_unlock_irqrestore(esp->
host->host_lock, flags);
2145 static void esp_get_revision(
struct esp *esp)
2162 esp_set_all_config3(esp, 5);
2175 esp_set_all_config3(esp, 0);
2194 static void esp_init_swstate(
struct esp *esp)
2208 esp->
target[
i].nego_goal_period = 0;
2209 esp->
target[
i].nego_goal_offset = 0;
2210 esp->
target[
i].nego_goal_width = 0;
2211 esp->
target[
i].nego_goal_tags = 0;
2216 static void esp_bootup_reset(
struct esp *esp)
2221 esp->
ops->reset_dma(esp);
2240 static void esp_set_clock_params(
struct esp *esp)
2279 ccf = ((fhz / 1000000) + 4) / 5;
2288 if (fhz <= 5000000 || ccf < 1 || ccf > 8) {
2293 esp->
cfact = (ccf == 8 ? 0 : ccf);
2301 static const char *esp_chip_names[] = {
2315 static int instance;
2318 esp->
host->transportt = esp_transport_template;
2320 esp->
host->cmd_per_lun = 2;
2321 esp->
host->unique_id = instance;
2323 esp_set_clock_params(esp);
2325 esp_get_revision(esp);
2327 esp_init_swstate(esp);
2329 esp_bootup_reset(esp);
2335 esp->
host->unique_id, esp_chip_names[esp->
rev],
2339 ssleep(esp_bus_reset_settle);
2341 err = scsi_add_host(esp->
host, dev);
2359 static int esp_target_alloc(
struct scsi_target *starget)
2361 struct esp *esp = shost_priv(dev_to_shost(&starget->
dev));
2369 static void esp_target_destroy(
struct scsi_target *starget)
2371 struct esp *esp = shost_priv(dev_to_shost(&starget->
dev));
2377 static int esp_slave_alloc(
struct scsi_device *dev)
2379 struct esp *esp = shost_priv(dev->
host);
2399 static int esp_slave_configure(
struct scsi_device *dev)
2401 struct esp *esp = shost_priv(dev->
host);
2415 queue_depth = goal_tags;
2416 if (queue_depth < dev->
host->cmd_per_lun)
2417 queue_depth = dev->
host->cmd_per_lun;
2421 scsi_activate_tcq(dev, queue_depth);
2423 scsi_deactivate_tcq(dev, queue_depth);
2433 static void esp_slave_destroy(
struct scsi_device *dev)
2441 static int esp_eh_abort_handler(
struct scsi_cmnd *cmd)
2443 struct esp *esp = shost_priv(cmd->
device->host);
2446 unsigned long flags;
2453 esp->
host->unique_id, cmd, cmd->
cmnd[0]);
2457 esp->
host->unique_id, ent->
cmd, ent->
cmd->cmnd[0]);
2460 esp->
host->unique_id, ent->
cmd, ent->
cmd->cmnd[0]);
2464 esp->
host->unique_id, ent->
cmd, ent->
cmd->cmnd[0]);
2466 esp_dump_cmd_log(esp);
2467 spin_unlock_irqrestore(esp->
host->host_lock, flags);
2473 if (tmp->
cmd == cmd) {
2488 esp_put_ent(esp, ent);
2493 init_completion(&eh_done);
2496 if (ent && ent->
cmd == cmd) {
2532 spin_unlock_irqrestore(esp->
host->host_lock, flags);
2537 spin_unlock_irqrestore(esp->
host->host_lock, flags);
2545 spin_unlock_irqrestore(esp->
host->host_lock, flags);
2553 spin_unlock_irqrestore(esp->
host->host_lock, flags);
2557 static int esp_eh_bus_reset_handler(
struct scsi_cmnd *cmd)
2559 struct esp *esp = shost_priv(cmd->
device->host);
2561 unsigned long flags;
2563 init_completion(&eh_reset);
2577 spin_unlock_irqrestore(esp->
host->host_lock, flags);
2579 ssleep(esp_bus_reset_settle);
2584 spin_unlock_irqrestore(esp->
host->host_lock, flags);
2593 static int esp_eh_host_reset_handler(
struct scsi_cmnd *cmd)
2595 struct esp *esp = shost_priv(cmd->
device->host);
2596 unsigned long flags;
2599 esp_bootup_reset(esp);
2600 esp_reset_cleanup(esp);
2601 spin_unlock_irqrestore(esp->
host->host_lock, flags);
2603 ssleep(esp_bus_reset_settle);
2617 .queuecommand = esp_queuecommand,
2618 .target_alloc = esp_target_alloc,
2619 .target_destroy = esp_target_destroy,
2620 .slave_alloc = esp_slave_alloc,
2621 .slave_configure = esp_slave_configure,
2622 .slave_destroy = esp_slave_destroy,
2623 .eh_abort_handler = esp_eh_abort_handler,
2624 .eh_bus_reset_handler = esp_eh_bus_reset_handler,
2625 .eh_host_reset_handler = esp_eh_host_reset_handler,
2630 .max_sectors = 0xffff,
2631 .skip_settle_delay = 1,
2637 struct esp *esp = shost_priv(host);
2648 static void esp_set_offset(
struct scsi_target *target,
int offset)
2650 struct Scsi_Host *host = dev_to_shost(target->
dev.parent);
2651 struct esp *esp = shost_priv(host);
2661 static void esp_set_period(
struct scsi_target *target,
int period)
2663 struct Scsi_Host *host = dev_to_shost(target->
dev.parent);
2664 struct esp *esp = shost_priv(host);
2673 struct Scsi_Host *host = dev_to_shost(target->
dev.parent);
2674 struct esp *esp = shost_priv(host);
2682 .set_offset = esp_set_offset,
2684 .set_period = esp_set_period,
2686 .set_width = esp_set_width,
2688 .get_signalling = esp_get_signalling,
2691 static int __init esp_init(
void)
2697 if (!esp_transport_template)
2703 static void __exit esp_exit(
void)
2715 "ESP scsi bus reset delay in seconds");
2719 "ESP bitmapped debugging message enable value:\n"
2720 " 0x00000001 Log interrupt events\n"
2721 " 0x00000002 Log scsi commands\n"
2722 " 0x00000004 Log resets\n"
2723 " 0x00000008 Log message in events\n"
2724 " 0x00000010 Log message out events\n"
2725 " 0x00000020 Log command completion\n"
2726 " 0x00000040 Log disconnects\n"
2727 " 0x00000080 Log data start\n"
2728 " 0x00000100 Log data done\n"
2729 " 0x00000200 Log reconnects\n"
2730 " 0x00000400 Log auto-sense data\n"