Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * NCR 5380 generic driver routines. These should make it *trivial* |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 4 | * to implement 5380 SCSI drivers under Linux with a non-trantor |
| 5 | * architecture. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 7 | * Note that these routines also work with NR53c400 family chips. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * Copyright 1993, Drew Eckhardt |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 10 | * Visionary Computing |
| 11 | * (Unix and Linux consulting and custom programming) |
| 12 | * drew@colorado.edu |
| 13 | * +1 (303) 666-5836 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 15 | * For more information, please consult |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * |
| 17 | * NCR 5380 Family |
| 18 | * SCSI Protocol Controller |
| 19 | * Databook |
| 20 | * |
| 21 | * NCR Microelectronics |
| 22 | * 1635 Aeroplaza Drive |
| 23 | * Colorado Springs, CO 80916 |
| 24 | * 1+ (719) 578-3400 |
| 25 | * 1+ (800) 334-5454 |
| 26 | */ |
| 27 | |
| 28 | /* |
Finn Thain | c16df32 | 2016-01-03 16:06:08 +1100 | [diff] [blame] | 29 | * With contributions from Ray Van Tassle, Ingmar Baumgart, |
| 30 | * Ronald van Cuijlenborg, Alan Cox and others. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | */ |
| 32 | |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 33 | /* Ported to Atari by Roman Hodek and others. */ |
| 34 | |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 35 | /* Adapted for the Sun 3 by Sam Creasey. */ |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | * Design |
| 39 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 40 | * This is a generic 5380 driver. To use it on a different platform, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | * one simply writes appropriate system specific macros (ie, data |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 42 | * transfer - some PC's will use the I/O bus, 68K's must use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | * memory mapped) and drops this file in their 'C' wrapper. |
| 44 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 45 | * As far as command queueing, two queues are maintained for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | * each 5380 in the system - commands that haven't been issued yet, |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 47 | * and commands that are currently executing. This means that an |
| 48 | * unlimited number of commands may be queued, letting |
| 49 | * more commands propagate from the higher driver levels giving higher |
| 50 | * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported, |
| 51 | * allowing multiple commands to propagate all the way to a SCSI-II device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | * while a command is already executing. |
| 53 | * |
| 54 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 55 | * Issues specific to the NCR5380 : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 57 | * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead |
| 58 | * piece of hardware that requires you to sit in a loop polling for |
| 59 | * the REQ signal as long as you are connected. Some devices are |
| 60 | * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect |
Finn Thain | 686f399 | 2016-01-03 16:05:26 +1100 | [diff] [blame] | 61 | * while doing long seek operations. [...] These |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | * broken devices are the exception rather than the rule and I'd rather |
| 63 | * spend my time optimizing for the normal case. |
| 64 | * |
| 65 | * Architecture : |
| 66 | * |
| 67 | * At the heart of the design is a coroutine, NCR5380_main, |
| 68 | * which is started from a workqueue for each NCR5380 host in the |
| 69 | * system. It attempts to establish I_T_L or I_T_L_Q nexuses by |
| 70 | * removing the commands from the issue queue and calling |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 71 | * NCR5380_select() if a nexus is not established. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * |
| 73 | * Once a nexus is established, the NCR5380_information_transfer() |
| 74 | * phase goes through the various phases as instructed by the target. |
| 75 | * if the target goes into MSG IN and sends a DISCONNECT message, |
| 76 | * the command structure is placed into the per instance disconnected |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 77 | * queue, and NCR5380_main tries to find more work. If the target is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | * idle for too long, the system will try to sleep. |
| 79 | * |
| 80 | * If a command has disconnected, eventually an interrupt will trigger, |
| 81 | * calling NCR5380_intr() which will in turn call NCR5380_reselect |
| 82 | * to reestablish a nexus. This will run main if necessary. |
| 83 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 84 | * On command termination, the done function will be called as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | * appropriate. |
| 86 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 87 | * SCSI pointers are maintained in the SCp field of SCSI command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * structures, being initialized after the command is connected |
| 89 | * in NCR5380_select, and set as appropriate in NCR5380_information_transfer. |
| 90 | * Note that in violation of the standard, an implicit SAVE POINTERS operation |
| 91 | * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS. |
| 92 | */ |
| 93 | |
| 94 | /* |
| 95 | * Using this file : |
| 96 | * This file a skeleton Linux SCSI driver for the NCR 5380 series |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 97 | * of chips. To use it, you write an architecture specific functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | * and macros and include this file in your driver. |
| 99 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | * These macros MUST be defined : |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 101 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | * NCR5380_read(register) - read from the specified register |
| 103 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 104 | * NCR5380_write(register, value) - write to the specific register |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 106 | * NCR5380_implementation_fields - additional fields needed for this |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 107 | * specific implementation of the NCR5380 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | * |
| 109 | * Either real DMA *or* pseudo DMA may be implemented |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | * |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 111 | * NCR5380_dma_xfer_len - determine size of DMA/PDMA transfer |
| 112 | * NCR5380_dma_send_setup - execute DMA/PDMA from memory to 5380 |
| 113 | * NCR5380_dma_recv_setup - execute DMA/PDMA from 5380 to memory |
| 114 | * NCR5380_dma_residual - residual byte count |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | * The generic driver is initialized by calling NCR5380_init(instance), |
Ondrej Zary | 906e4a3c | 2016-12-05 01:07:20 -0500 | [diff] [blame] | 117 | * after setting the appropriate host specific fields and ID. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | */ |
| 119 | |
Finn Thain | e5d55d1 | 2016-03-23 21:10:16 +1100 | [diff] [blame] | 120 | #ifndef NCR5380_io_delay |
| 121 | #define NCR5380_io_delay(x) |
| 122 | #endif |
| 123 | |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 124 | #ifndef NCR5380_acquire_dma_irq |
| 125 | #define NCR5380_acquire_dma_irq(x) (1) |
| 126 | #endif |
| 127 | |
| 128 | #ifndef NCR5380_release_dma_irq |
| 129 | #define NCR5380_release_dma_irq(x) |
| 130 | #endif |
| 131 | |
Finn Thain | 54d8fe4 | 2016-01-03 16:05:06 +1100 | [diff] [blame] | 132 | static int do_abort(struct Scsi_Host *); |
| 133 | static void do_reset(struct Scsi_Host *); |
Finn Thain | 6b0e87a | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 134 | static void bus_reset_cleanup(struct Scsi_Host *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Finn Thain | c16df32 | 2016-01-03 16:06:08 +1100 | [diff] [blame] | 136 | /** |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 137 | * initialize_SCp - init the scsi pointer field |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 138 | * @cmd: command block to set up |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | * |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 140 | * Set up the internal fields in the SCSI command. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | */ |
| 142 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 143 | static inline void initialize_SCp(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 145 | /* |
| 146 | * Initialize the Scsi Pointer field so that all of the commands in the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | * various queues are valid. |
| 148 | */ |
| 149 | |
Boaz Harrosh | 9e0fe44 | 2007-11-05 11:23:35 +0200 | [diff] [blame] | 150 | if (scsi_bufflen(cmd)) { |
| 151 | cmd->SCp.buffer = scsi_sglist(cmd); |
| 152 | cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1; |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 153 | cmd->SCp.ptr = sg_virt(cmd->SCp.buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | cmd->SCp.this_residual = cmd->SCp.buffer->length; |
| 155 | } else { |
| 156 | cmd->SCp.buffer = NULL; |
| 157 | cmd->SCp.buffers_residual = 0; |
Boaz Harrosh | 9e0fe44 | 2007-11-05 11:23:35 +0200 | [diff] [blame] | 158 | cmd->SCp.ptr = NULL; |
| 159 | cmd->SCp.this_residual = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 161 | |
| 162 | cmd->SCp.Status = 0; |
| 163 | cmd->SCp.Message = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 167 | * NCR5380_poll_politely2 - wait for two chip register values |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 168 | * @hostdata: host private data |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 169 | * @reg1: 5380 register to poll |
| 170 | * @bit1: Bitmask to check |
| 171 | * @val1: Expected value |
| 172 | * @reg2: Second 5380 register to poll |
| 173 | * @bit2: Second bitmask to check |
| 174 | * @val2: Second expected value |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 175 | * @wait: Time-out in jiffies |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | * |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 177 | * Polls the chip in a reasonably efficient manner waiting for an |
| 178 | * event to occur. After a short quick poll we begin to yield the CPU |
| 179 | * (if possible). In irq contexts the time-out is arbitrarily limited. |
| 180 | * Callers may hold locks as long as they are held in irq mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | * |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 182 | * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 185 | static int NCR5380_poll_politely2(struct NCR5380_hostdata *hostdata, |
Finn Thain | 61e1ce5 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 186 | unsigned int reg1, u8 bit1, u8 val1, |
| 187 | unsigned int reg2, u8 bit2, u8 val2, |
| 188 | unsigned long wait) |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 189 | { |
Finn Thain | d4408dd | 2016-10-10 00:46:52 -0400 | [diff] [blame] | 190 | unsigned long n = hostdata->poll_loops; |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 191 | unsigned long deadline = jiffies + wait; |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 192 | |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 193 | do { |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 194 | if ((NCR5380_read(reg1) & bit1) == val1) |
| 195 | return 0; |
| 196 | if ((NCR5380_read(reg2) & bit2) == val2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return 0; |
| 198 | cpu_relax(); |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 199 | } while (n--); |
| 200 | |
| 201 | if (irqs_disabled() || in_interrupt()) |
| 202 | return -ETIMEDOUT; |
| 203 | |
| 204 | /* Repeatedly sleep for 1 ms until deadline */ |
| 205 | while (time_is_after_jiffies(deadline)) { |
| 206 | schedule_timeout_uninterruptible(1); |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 207 | if ((NCR5380_read(reg1) & bit1) == val1) |
| 208 | return 0; |
| 209 | if ((NCR5380_read(reg2) & bit2) == val2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | return -ETIMEDOUT; |
| 214 | } |
| 215 | |
viro@ZenIV.linux.org.uk | 185a7a1 | 2005-09-07 23:18:24 +0100 | [diff] [blame] | 216 | #if NDEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | static struct { |
| 218 | unsigned char mask; |
| 219 | const char *name; |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 220 | } signals[] = { |
| 221 | {SR_DBP, "PARITY"}, |
| 222 | {SR_RST, "RST"}, |
| 223 | {SR_BSY, "BSY"}, |
| 224 | {SR_REQ, "REQ"}, |
| 225 | {SR_MSG, "MSG"}, |
| 226 | {SR_CD, "CD"}, |
| 227 | {SR_IO, "IO"}, |
| 228 | {SR_SEL, "SEL"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | {0, NULL} |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 230 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | basrs[] = { |
Finn Thain | 12866b9 | 2016-03-23 21:10:25 +1100 | [diff] [blame] | 232 | {BASR_END_DMA_TRANSFER, "END OF DMA"}, |
| 233 | {BASR_DRQ, "DRQ"}, |
| 234 | {BASR_PARITY_ERROR, "PARITY ERROR"}, |
| 235 | {BASR_IRQ, "IRQ"}, |
| 236 | {BASR_PHASE_MATCH, "PHASE MATCH"}, |
| 237 | {BASR_BUSY_ERROR, "BUSY ERROR"}, |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 238 | {BASR_ATN, "ATN"}, |
| 239 | {BASR_ACK, "ACK"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | {0, NULL} |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 241 | }, |
| 242 | icrs[] = { |
| 243 | {ICR_ASSERT_RST, "ASSERT RST"}, |
Finn Thain | 12866b9 | 2016-03-23 21:10:25 +1100 | [diff] [blame] | 244 | {ICR_ARBITRATION_PROGRESS, "ARB. IN PROGRESS"}, |
| 245 | {ICR_ARBITRATION_LOST, "LOST ARB."}, |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 246 | {ICR_ASSERT_ACK, "ASSERT ACK"}, |
| 247 | {ICR_ASSERT_BSY, "ASSERT BSY"}, |
| 248 | {ICR_ASSERT_SEL, "ASSERT SEL"}, |
| 249 | {ICR_ASSERT_ATN, "ASSERT ATN"}, |
| 250 | {ICR_ASSERT_DATA, "ASSERT DATA"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | {0, NULL} |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 252 | }, |
| 253 | mrs[] = { |
Finn Thain | 12866b9 | 2016-03-23 21:10:25 +1100 | [diff] [blame] | 254 | {MR_BLOCK_DMA_MODE, "BLOCK DMA MODE"}, |
| 255 | {MR_TARGET, "TARGET"}, |
| 256 | {MR_ENABLE_PAR_CHECK, "PARITY CHECK"}, |
| 257 | {MR_ENABLE_PAR_INTR, "PARITY INTR"}, |
| 258 | {MR_ENABLE_EOP_INTR, "EOP INTR"}, |
| 259 | {MR_MONITOR_BSY, "MONITOR BSY"}, |
| 260 | {MR_DMA_MODE, "DMA MODE"}, |
| 261 | {MR_ARBITRATE, "ARBITRATE"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | {0, NULL} |
| 263 | }; |
| 264 | |
| 265 | /** |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 266 | * NCR5380_print - print scsi bus signals |
| 267 | * @instance: adapter state to dump |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | * |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 269 | * Print the SCSI bus signals for debugging purposes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | */ |
| 271 | |
| 272 | static void NCR5380_print(struct Scsi_Host *instance) |
| 273 | { |
Finn Thain | 61e1ce5 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 274 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | unsigned char status, data, basr, mr, icr, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | data = NCR5380_read(CURRENT_SCSI_DATA_REG); |
| 278 | status = NCR5380_read(STATUS_REG); |
| 279 | mr = NCR5380_read(MODE_REG); |
| 280 | icr = NCR5380_read(INITIATOR_COMMAND_REG); |
| 281 | basr = NCR5380_read(BUS_AND_STATUS_REG); |
| 282 | |
Finn Thain | 12866b9 | 2016-03-23 21:10:25 +1100 | [diff] [blame] | 283 | printk(KERN_DEBUG "SR = 0x%02x : ", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | for (i = 0; signals[i].mask; ++i) |
| 285 | if (status & signals[i].mask) |
Finn Thain | 12866b9 | 2016-03-23 21:10:25 +1100 | [diff] [blame] | 286 | printk(KERN_CONT "%s, ", signals[i].name); |
| 287 | printk(KERN_CONT "\nBASR = 0x%02x : ", basr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | for (i = 0; basrs[i].mask; ++i) |
| 289 | if (basr & basrs[i].mask) |
Finn Thain | 12866b9 | 2016-03-23 21:10:25 +1100 | [diff] [blame] | 290 | printk(KERN_CONT "%s, ", basrs[i].name); |
| 291 | printk(KERN_CONT "\nICR = 0x%02x : ", icr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | for (i = 0; icrs[i].mask; ++i) |
| 293 | if (icr & icrs[i].mask) |
Finn Thain | 12866b9 | 2016-03-23 21:10:25 +1100 | [diff] [blame] | 294 | printk(KERN_CONT "%s, ", icrs[i].name); |
| 295 | printk(KERN_CONT "\nMR = 0x%02x : ", mr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | for (i = 0; mrs[i].mask; ++i) |
| 297 | if (mr & mrs[i].mask) |
Finn Thain | 12866b9 | 2016-03-23 21:10:25 +1100 | [diff] [blame] | 298 | printk(KERN_CONT "%s, ", mrs[i].name); |
| 299 | printk(KERN_CONT "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 302 | static struct { |
| 303 | unsigned char value; |
| 304 | const char *name; |
| 305 | } phases[] = { |
| 306 | {PHASE_DATAOUT, "DATAOUT"}, |
| 307 | {PHASE_DATAIN, "DATAIN"}, |
| 308 | {PHASE_CMDOUT, "CMDOUT"}, |
| 309 | {PHASE_STATIN, "STATIN"}, |
| 310 | {PHASE_MSGOUT, "MSGOUT"}, |
| 311 | {PHASE_MSGIN, "MSGIN"}, |
| 312 | {PHASE_UNKNOWN, "UNKNOWN"} |
| 313 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Finn Thain | c16df32 | 2016-01-03 16:06:08 +1100 | [diff] [blame] | 315 | /** |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 316 | * NCR5380_print_phase - show SCSI phase |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 317 | * @instance: adapter to dump |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | * |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 319 | * Print the current SCSI phase for debugging purposes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | */ |
| 321 | |
| 322 | static void NCR5380_print_phase(struct Scsi_Host *instance) |
| 323 | { |
Finn Thain | 61e1ce5 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 324 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | unsigned char status; |
| 326 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
| 328 | status = NCR5380_read(STATUS_REG); |
| 329 | if (!(status & SR_REQ)) |
Finn Thain | 6a6ff4a | 2016-01-03 16:06:04 +1100 | [diff] [blame] | 330 | shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | else { |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 332 | for (i = 0; (phases[i].value != PHASE_UNKNOWN) && |
| 333 | (phases[i].value != (status & PHASE_MASK)); ++i) |
| 334 | ; |
Finn Thain | 6a6ff4a | 2016-01-03 16:06:04 +1100 | [diff] [blame] | 335 | shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | #endif |
| 339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | /** |
Finn Thain | 0902846 | 2017-01-15 18:50:57 -0500 | [diff] [blame] | 341 | * NCR5380_info - report driver and host information |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 342 | * @instance: relevant scsi host instance |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | * |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 344 | * For use as the host template info() handler. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | */ |
| 346 | |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 347 | static const char *NCR5380_info(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 349 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 350 | |
| 351 | return hostdata->info; |
| 352 | } |
| 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | /** |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 355 | * NCR5380_init - initialise an NCR5380 |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 356 | * @instance: adapter to configure |
| 357 | * @flags: control flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | * |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 359 | * Initializes *instance and corresponding 5380 chip, |
| 360 | * with flags OR'd into the initial flags value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | * |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 362 | * Notes : I assume that the host, hostno, and id bits have been |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 363 | * set correctly. I don't care about the irq and other fields. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | * |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 365 | * Returns 0 for success |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | */ |
| 367 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 368 | static int NCR5380_init(struct Scsi_Host *instance, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
Finn Thain | e8a6014 | 2016-01-03 16:05:54 +1100 | [diff] [blame] | 370 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Finn Thain | b6488f9 | 2016-01-03 16:05:08 +1100 | [diff] [blame] | 371 | int i; |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 372 | unsigned long deadline; |
Finn Thain | d4408dd | 2016-10-10 00:46:52 -0400 | [diff] [blame] | 373 | unsigned long accesses_per_ms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Finn Thain | ae5e33a | 2016-03-23 21:10:23 +1100 | [diff] [blame] | 375 | instance->max_lun = 7; |
| 376 | |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 377 | hostdata->host = instance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | hostdata->id_mask = 1 << instance->this_id; |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 379 | hostdata->id_higher_mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | for (i = hostdata->id_mask; i <= 0x80; i <<= 1) |
| 381 | if (i > hostdata->id_mask) |
| 382 | hostdata->id_higher_mask |= i; |
| 383 | for (i = 0; i < 8; ++i) |
| 384 | hostdata->busy[i] = 0; |
Finn Thain | e4dec68 | 2016-03-23 21:10:12 +1100 | [diff] [blame] | 385 | hostdata->dma_len = 0; |
| 386 | |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 387 | spin_lock_init(&hostdata->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | hostdata->connected = NULL; |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 389 | hostdata->sensing = NULL; |
| 390 | INIT_LIST_HEAD(&hostdata->autosense); |
Finn Thain | 32b26a1 | 2016-01-03 16:05:58 +1100 | [diff] [blame] | 391 | INIT_LIST_HEAD(&hostdata->unissued); |
| 392 | INIT_LIST_HEAD(&hostdata->disconnected); |
| 393 | |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 394 | hostdata->flags = flags; |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 395 | |
Finn Thain | 8d8601a | 2016-01-03 16:05:37 +1100 | [diff] [blame] | 396 | INIT_WORK(&hostdata->main_task, NCR5380_main); |
Finn Thain | 0ad0eff | 2016-01-03 16:05:21 +1100 | [diff] [blame] | 397 | hostdata->work_q = alloc_workqueue("ncr5380_%d", |
| 398 | WQ_UNBOUND | WQ_MEM_RECLAIM, |
| 399 | 1, instance->host_no); |
| 400 | if (!hostdata->work_q) |
| 401 | return -ENOMEM; |
| 402 | |
Finn Thain | 0902846 | 2017-01-15 18:50:57 -0500 | [diff] [blame] | 403 | snprintf(hostdata->info, sizeof(hostdata->info), |
| 404 | "%s, irq %d, io_port 0x%lx, base 0x%lx, can_queue %d, cmd_per_lun %d, sg_tablesize %d, this_id %d, flags { %s%s%s}", |
| 405 | instance->hostt->name, instance->irq, hostdata->io_port, |
| 406 | hostdata->base, instance->can_queue, instance->cmd_per_lun, |
| 407 | instance->sg_tablesize, instance->this_id, |
| 408 | hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "", |
| 409 | hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "", |
| 410 | hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : ""); |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 413 | NCR5380_write(MODE_REG, MR_BASE); |
| 414 | NCR5380_write(TARGET_COMMAND_REG, 0); |
| 415 | NCR5380_write(SELECT_ENABLE_REG, 0); |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 416 | |
| 417 | /* Calibrate register polling loop */ |
| 418 | i = 0; |
| 419 | deadline = jiffies + 1; |
| 420 | do { |
| 421 | cpu_relax(); |
| 422 | } while (time_is_after_jiffies(deadline)); |
| 423 | deadline += msecs_to_jiffies(256); |
| 424 | do { |
| 425 | NCR5380_read(STATUS_REG); |
| 426 | ++i; |
| 427 | cpu_relax(); |
| 428 | } while (time_is_after_jiffies(deadline)); |
Finn Thain | d4408dd | 2016-10-10 00:46:52 -0400 | [diff] [blame] | 429 | accesses_per_ms = i / 256; |
| 430 | hostdata->poll_loops = NCR5380_REG_POLL_TIME * accesses_per_ms / 2; |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 431 | |
Finn Thain | b6488f9 | 2016-01-03 16:05:08 +1100 | [diff] [blame] | 432 | return 0; |
| 433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Finn Thain | b6488f9 | 2016-01-03 16:05:08 +1100 | [diff] [blame] | 435 | /** |
| 436 | * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems. |
| 437 | * @instance: adapter to check |
| 438 | * |
| 439 | * If the system crashed, it may have crashed with a connected target and |
| 440 | * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the |
| 441 | * currently established nexus, which we know nothing about. Failing that |
| 442 | * do a bus reset. |
| 443 | * |
| 444 | * Note that a bus reset will cause the chip to assert IRQ. |
| 445 | * |
| 446 | * Returns 0 if successful, otherwise -ENXIO. |
| 447 | */ |
| 448 | |
| 449 | static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance) |
| 450 | { |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 451 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Finn Thain | b6488f9 | 2016-01-03 16:05:08 +1100 | [diff] [blame] | 452 | int pass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
| 454 | for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) { |
| 455 | switch (pass) { |
| 456 | case 1: |
| 457 | case 3: |
| 458 | case 5: |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 459 | shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n"); |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 460 | NCR5380_poll_politely(hostdata, |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 461 | STATUS_REG, SR_BSY, 0, 5 * HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | break; |
| 463 | case 2: |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 464 | shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | do_abort(instance); |
| 466 | break; |
| 467 | case 4: |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 468 | shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | do_reset(instance); |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 470 | /* Wait after a reset; the SCSI standard calls for |
| 471 | * 250ms, we wait 500ms to be on the safe side. |
| 472 | * But some Toshiba CD-ROMs need ten times that. |
| 473 | */ |
| 474 | if (hostdata->flags & FLAG_TOSHIBA_DELAY) |
| 475 | msleep(2500); |
| 476 | else |
| 477 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | break; |
| 479 | case 6: |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 480 | shost_printk(KERN_ERR, instance, "bus locked solid\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | return -ENXIO; |
| 482 | } |
| 483 | } |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | /** |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 488 | * NCR5380_exit - remove an NCR5380 |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 489 | * @instance: adapter to remove |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 490 | * |
| 491 | * Assumes that no more work can be queued (e.g. by NCR5380_intr). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | */ |
| 493 | |
Randy Dunlap | a43cf0f | 2008-01-22 21:39:33 -0800 | [diff] [blame] | 494 | static void NCR5380_exit(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
Finn Thain | e8a6014 | 2016-01-03 16:05:54 +1100 | [diff] [blame] | 496 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Finn Thain | 8d8601a | 2016-01-03 16:05:37 +1100 | [diff] [blame] | 498 | cancel_work_sync(&hostdata->main_task); |
Finn Thain | 0ad0eff | 2016-01-03 16:05:21 +1100 | [diff] [blame] | 499 | destroy_workqueue(hostdata->work_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /** |
Finn Thain | 677e019 | 2016-01-03 16:05:59 +1100 | [diff] [blame] | 503 | * complete_cmd - finish processing a command and return it to the SCSI ML |
| 504 | * @instance: the host instance |
| 505 | * @cmd: command to complete |
| 506 | */ |
| 507 | |
| 508 | static void complete_cmd(struct Scsi_Host *instance, |
| 509 | struct scsi_cmnd *cmd) |
| 510 | { |
| 511 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 512 | |
| 513 | dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd); |
| 514 | |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 515 | if (hostdata->sensing == cmd) { |
| 516 | /* Autosense processing ends here */ |
Finn Thain | 0703565 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 517 | if (status_byte(cmd->result) != GOOD) { |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 518 | scsi_eh_restore_cmnd(cmd, &hostdata->ses); |
Finn Thain | 0703565 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 519 | } else { |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 520 | scsi_eh_restore_cmnd(cmd, &hostdata->ses); |
Finn Thain | 0703565 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 521 | set_driver_byte(cmd, DRIVER_SENSE); |
| 522 | } |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 523 | hostdata->sensing = NULL; |
| 524 | } |
| 525 | |
Finn Thain | 677e019 | 2016-01-03 16:05:59 +1100 | [diff] [blame] | 526 | cmd->scsi_done(cmd); |
| 527 | } |
| 528 | |
| 529 | /** |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 530 | * NCR5380_queue_command - queue a command |
| 531 | * @instance: the relevant SCSI adapter |
| 532 | * @cmd: SCSI command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | * |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 534 | * cmd is added to the per-instance issue queue, with minor |
| 535 | * twiddling done to the host specific fields of cmd. If the |
| 536 | * main coroutine is not running, it is restarted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | */ |
| 538 | |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 539 | static int NCR5380_queue_command(struct Scsi_Host *instance, |
| 540 | struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | { |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 542 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Finn Thain | 32b26a1 | 2016-01-03 16:05:58 +1100 | [diff] [blame] | 543 | struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd); |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 544 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
| 546 | #if (NDEBUG & NDEBUG_NO_WRITE) |
| 547 | switch (cmd->cmnd[0]) { |
| 548 | case WRITE_6: |
| 549 | case WRITE_10: |
Finn Thain | dbb6b35 | 2016-01-03 16:05:53 +1100 | [diff] [blame] | 550 | shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | cmd->result = (DID_ERROR << 16); |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 552 | cmd->scsi_done(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return 0; |
| 554 | } |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 555 | #endif /* (NDEBUG & NDEBUG_NO_WRITE) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | cmd->result = 0; |
| 558 | |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 559 | if (!NCR5380_acquire_dma_irq(instance)) |
| 560 | return SCSI_MLQUEUE_HOST_BUSY; |
| 561 | |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 562 | spin_lock_irqsave(&hostdata->lock, flags); |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 563 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 564 | /* |
| 565 | * Insert the cmd into the issue queue. Note that REQUEST SENSE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | * commands are added to the head of the queue since any command will |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 567 | * clear the contingent allegiance condition that exists and the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | * sense data is only guaranteed to be valid while the condition exists. |
| 569 | */ |
| 570 | |
Finn Thain | 32b26a1 | 2016-01-03 16:05:58 +1100 | [diff] [blame] | 571 | if (cmd->cmnd[0] == REQUEST_SENSE) |
| 572 | list_add(&ncmd->list, &hostdata->unissued); |
| 573 | else |
| 574 | list_add_tail(&ncmd->list, &hostdata->unissued); |
| 575 | |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 576 | spin_unlock_irqrestore(&hostdata->lock, flags); |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 577 | |
Finn Thain | dbb6b35 | 2016-01-03 16:05:53 +1100 | [diff] [blame] | 578 | dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n", |
| 579 | cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | /* Kick off command processing */ |
Finn Thain | 8d8601a | 2016-01-03 16:05:37 +1100 | [diff] [blame] | 582 | queue_work(hostdata->work_q, &hostdata->main_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | return 0; |
| 584 | } |
| 585 | |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 586 | static inline void maybe_release_dma_irq(struct Scsi_Host *instance) |
| 587 | { |
| 588 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 589 | |
| 590 | /* Caller does the locking needed to set & test these data atomically */ |
| 591 | if (list_empty(&hostdata->disconnected) && |
| 592 | list_empty(&hostdata->unissued) && |
| 593 | list_empty(&hostdata->autosense) && |
| 594 | !hostdata->connected && |
Finn Thain | 4ab2a78 | 2017-01-15 18:50:57 -0500 | [diff] [blame] | 595 | !hostdata->selecting) { |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 596 | NCR5380_release_dma_irq(instance); |
Finn Thain | 4ab2a78 | 2017-01-15 18:50:57 -0500 | [diff] [blame] | 597 | } |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 598 | } |
| 599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | /** |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 601 | * dequeue_next_cmd - dequeue a command for processing |
| 602 | * @instance: the scsi host instance |
| 603 | * |
| 604 | * Priority is given to commands on the autosense queue. These commands |
| 605 | * need autosense because of a CHECK CONDITION result. |
| 606 | * |
| 607 | * Returns a command pointer if a command is found for a target that is |
| 608 | * not already busy. Otherwise returns NULL. |
| 609 | */ |
| 610 | |
| 611 | static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance) |
| 612 | { |
| 613 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 614 | struct NCR5380_cmd *ncmd; |
| 615 | struct scsi_cmnd *cmd; |
| 616 | |
Finn Thain | 8d5dbec3 | 2016-02-23 10:07:09 +1100 | [diff] [blame] | 617 | if (hostdata->sensing || list_empty(&hostdata->autosense)) { |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 618 | list_for_each_entry(ncmd, &hostdata->unissued, list) { |
| 619 | cmd = NCR5380_to_scmd(ncmd); |
| 620 | dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n", |
| 621 | cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun); |
| 622 | |
| 623 | if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) { |
| 624 | list_del(&ncmd->list); |
| 625 | dsprintk(NDEBUG_QUEUES, instance, |
| 626 | "dequeue: removed %p from issue queue\n", cmd); |
| 627 | return cmd; |
| 628 | } |
| 629 | } |
| 630 | } else { |
| 631 | /* Autosense processing begins here */ |
| 632 | ncmd = list_first_entry(&hostdata->autosense, |
| 633 | struct NCR5380_cmd, list); |
| 634 | list_del(&ncmd->list); |
| 635 | cmd = NCR5380_to_scmd(ncmd); |
| 636 | dsprintk(NDEBUG_QUEUES, instance, |
| 637 | "dequeue: removed %p from autosense queue\n", cmd); |
| 638 | scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0); |
| 639 | hostdata->sensing = cmd; |
| 640 | return cmd; |
| 641 | } |
| 642 | return NULL; |
| 643 | } |
| 644 | |
| 645 | static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd) |
| 646 | { |
| 647 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 648 | struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd); |
| 649 | |
Finn Thain | 8d5dbec3 | 2016-02-23 10:07:09 +1100 | [diff] [blame] | 650 | if (hostdata->sensing == cmd) { |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 651 | scsi_eh_restore_cmnd(cmd, &hostdata->ses); |
| 652 | list_add(&ncmd->list, &hostdata->autosense); |
| 653 | hostdata->sensing = NULL; |
| 654 | } else |
| 655 | list_add(&ncmd->list, &hostdata->unissued); |
| 656 | } |
| 657 | |
| 658 | /** |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 659 | * NCR5380_main - NCR state machines |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | * |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 661 | * NCR5380_main is a coroutine that runs as long as more work can |
| 662 | * be done on the NCR5380 host adapters in a system. Both |
| 663 | * NCR5380_queue_command() and NCR5380_intr() will try to start it |
| 664 | * in case it is not running. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | */ |
| 666 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 667 | static void NCR5380_main(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 669 | struct NCR5380_hostdata *hostdata = |
Finn Thain | 8d8601a | 2016-01-03 16:05:37 +1100 | [diff] [blame] | 670 | container_of(work, struct NCR5380_hostdata, main_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | struct Scsi_Host *instance = hostdata->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | int done; |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | done = 1; |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 676 | |
Finn Thain | 0a4e361 | 2016-01-03 16:06:07 +1100 | [diff] [blame] | 677 | spin_lock_irq(&hostdata->lock); |
Finn Thain | ccf6efd | 2016-02-23 10:07:08 +1100 | [diff] [blame] | 678 | while (!hostdata->connected && !hostdata->selecting) { |
| 679 | struct scsi_cmnd *cmd = dequeue_next_cmd(instance); |
| 680 | |
| 681 | if (!cmd) |
| 682 | break; |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 683 | |
| 684 | dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd); |
| 685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | /* |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 687 | * Attempt to establish an I_T_L nexus here. |
| 688 | * On success, instance->hostdata->connected is set. |
| 689 | * On failure, we must add the command back to the |
| 690 | * issue queue so we can keep trying. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | */ |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 692 | /* |
| 693 | * REQUEST SENSE commands are issued without tagged |
| 694 | * queueing, even on SCSI-II devices because the |
| 695 | * contingent allegiance condition exists for the |
| 696 | * entire unit. |
| 697 | */ |
Finn Thain | 32b26a1 | 2016-01-03 16:05:58 +1100 | [diff] [blame] | 698 | |
Finn Thain | ccf6efd | 2016-02-23 10:07:08 +1100 | [diff] [blame] | 699 | if (!NCR5380_select(instance, cmd)) { |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 700 | dsprintk(NDEBUG_MAIN, instance, "main: select complete\n"); |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 701 | maybe_release_dma_irq(instance); |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 702 | } else { |
| 703 | dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance, |
| 704 | "main: select failed, returning %p to queue\n", cmd); |
| 705 | requeue_cmd(instance, cmd); |
| 706 | } |
| 707 | } |
Finn Thain | e4dec68 | 2016-03-23 21:10:12 +1100 | [diff] [blame] | 708 | if (hostdata->connected && !hostdata->dma_len) { |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 709 | dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | NCR5380_information_transfer(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | done = 0; |
Finn Thain | 1d3db59 | 2016-01-03 16:05:24 +1100 | [diff] [blame] | 712 | } |
Finn Thain | 0a4e361 | 2016-01-03 16:06:07 +1100 | [diff] [blame] | 713 | spin_unlock_irq(&hostdata->lock); |
| 714 | if (!done) |
| 715 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | } while (!done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 719 | /* |
| 720 | * NCR5380_dma_complete - finish DMA transfer |
| 721 | * @instance: the scsi host instance |
| 722 | * |
| 723 | * Called by the interrupt handler when DMA finishes or a phase |
| 724 | * mismatch occurs (which would end the DMA transfer). |
| 725 | */ |
| 726 | |
| 727 | static void NCR5380_dma_complete(struct Scsi_Host *instance) |
| 728 | { |
| 729 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 730 | int transferred; |
| 731 | unsigned char **data; |
| 732 | int *count; |
| 733 | int saved_data = 0, overrun = 0; |
| 734 | unsigned char p; |
| 735 | |
| 736 | if (hostdata->read_overruns) { |
| 737 | p = hostdata->connected->SCp.phase; |
| 738 | if (p & SR_IO) { |
| 739 | udelay(10); |
| 740 | if ((NCR5380_read(BUS_AND_STATUS_REG) & |
| 741 | (BASR_PHASE_MATCH | BASR_ACK)) == |
| 742 | (BASR_PHASE_MATCH | BASR_ACK)) { |
| 743 | saved_data = NCR5380_read(INPUT_DATA_REG); |
| 744 | overrun = 1; |
| 745 | dsprintk(NDEBUG_DMA, instance, "read overrun handled\n"); |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 750 | #ifdef CONFIG_SUN3 |
| 751 | if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) { |
| 752 | pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n", |
| 753 | instance->host_no); |
| 754 | BUG(); |
| 755 | } |
| 756 | |
| 757 | if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == |
| 758 | (BASR_PHASE_MATCH | BASR_ACK)) { |
| 759 | pr_err("scsi%d: BASR %02x\n", instance->host_no, |
| 760 | NCR5380_read(BUS_AND_STATUS_REG)); |
| 761 | pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n", |
| 762 | instance->host_no); |
| 763 | BUG(); |
| 764 | } |
| 765 | #endif |
| 766 | |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 767 | NCR5380_write(MODE_REG, MR_BASE); |
| 768 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 769 | NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 770 | |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 771 | transferred = hostdata->dma_len - NCR5380_dma_residual(hostdata); |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 772 | hostdata->dma_len = 0; |
| 773 | |
| 774 | data = (unsigned char **)&hostdata->connected->SCp.ptr; |
| 775 | count = &hostdata->connected->SCp.this_residual; |
| 776 | *data += transferred; |
| 777 | *count -= transferred; |
| 778 | |
| 779 | if (hostdata->read_overruns) { |
| 780 | int cnt, toPIO; |
| 781 | |
| 782 | if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) { |
| 783 | cnt = toPIO = hostdata->read_overruns; |
| 784 | if (overrun) { |
| 785 | dsprintk(NDEBUG_DMA, instance, |
| 786 | "Got an input overrun, using saved byte\n"); |
| 787 | *(*data)++ = saved_data; |
| 788 | (*count)--; |
| 789 | cnt--; |
| 790 | toPIO--; |
| 791 | } |
| 792 | if (toPIO > 0) { |
| 793 | dsprintk(NDEBUG_DMA, instance, |
| 794 | "Doing %d byte PIO to 0x%p\n", cnt, *data); |
| 795 | NCR5380_transfer_pio(instance, &p, &cnt, data); |
| 796 | *count -= toPIO - cnt; |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | /** |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 803 | * NCR5380_intr - generic NCR5380 irq handler |
| 804 | * @irq: interrupt number |
| 805 | * @dev_id: device info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | * |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 807 | * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses |
| 808 | * from the disconnected queue, and restarting NCR5380_main() |
| 809 | * as required. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | * |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 811 | * The chip can assert IRQ in any of six different conditions. The IRQ flag |
| 812 | * is then cleared by reading the Reset Parity/Interrupt Register (RPIR). |
| 813 | * Three of these six conditions are latched in the Bus and Status Register: |
| 814 | * - End of DMA (cleared by ending DMA Mode) |
| 815 | * - Parity error (cleared by reading RPIR) |
| 816 | * - Loss of BSY (cleared by reading RPIR) |
| 817 | * Two conditions have flag bits that are not latched: |
| 818 | * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode) |
| 819 | * - Bus reset (non-maskable) |
| 820 | * The remaining condition has no flag bit at all: |
| 821 | * - Selection/reselection |
| 822 | * |
| 823 | * Hence, establishing the cause(s) of any interrupt is partly guesswork. |
| 824 | * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor |
| 825 | * claimed that "the design of the [DP8490] interrupt logic ensures |
| 826 | * interrupts will not be lost (they can be on the DP5380)." |
| 827 | * The L5380/53C80 datasheet from LOGIC Devices has more details. |
| 828 | * |
| 829 | * Checking for bus reset by reading RST is futile because of interrupt |
| 830 | * latency, but a bus reset will reset chip logic. Checking for parity error |
| 831 | * is unnecessary because that interrupt is never enabled. A Loss of BSY |
| 832 | * condition will clear DMA Mode. We can tell when this occurs because the |
| 833 | * the Busy Monitor interrupt is enabled together with DMA Mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | */ |
| 835 | |
Finn Thain | a46865d | 2016-03-23 21:10:27 +1100 | [diff] [blame] | 836 | static irqreturn_t __maybe_unused NCR5380_intr(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | { |
Jeff Garzik | baa9aac | 2007-12-13 16:14:14 -0800 | [diff] [blame] | 838 | struct Scsi_Host *instance = dev_id; |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 839 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 840 | int handled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | unsigned char basr; |
| 842 | unsigned long flags; |
| 843 | |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 844 | spin_lock_irqsave(&hostdata->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 846 | basr = NCR5380_read(BUS_AND_STATUS_REG); |
| 847 | if (basr & BASR_IRQ) { |
| 848 | unsigned char mr = NCR5380_read(MODE_REG); |
| 849 | unsigned char sr = NCR5380_read(STATUS_REG); |
| 850 | |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 851 | dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n", |
| 852 | irq, basr, sr, mr); |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 853 | |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 854 | if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) { |
| 855 | /* Probably End of DMA, Phase Mismatch or Loss of BSY. |
| 856 | * We ack IRQ after clearing Mode Register. Workarounds |
| 857 | * for End of DMA errata need to happen in DMA Mode. |
| 858 | */ |
| 859 | |
| 860 | dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n"); |
| 861 | |
| 862 | if (hostdata->connected) { |
| 863 | NCR5380_dma_complete(instance); |
| 864 | queue_work(hostdata->work_q, &hostdata->main_task); |
| 865 | } else { |
| 866 | NCR5380_write(MODE_REG, MR_BASE); |
| 867 | NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 868 | } |
| 869 | } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) && |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 870 | (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) { |
| 871 | /* Probably reselected */ |
| 872 | NCR5380_write(SELECT_ENABLE_REG, 0); |
| 873 | NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 874 | |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 875 | dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n"); |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 876 | |
| 877 | if (!hostdata->connected) { |
| 878 | NCR5380_reselect(instance); |
| 879 | queue_work(hostdata->work_q, &hostdata->main_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | } |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 881 | if (!hostdata->connected) |
| 882 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 883 | } else { |
| 884 | /* Probably Bus Reset */ |
| 885 | NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 886 | |
Finn Thain | 6b0e87a | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 887 | if (sr & SR_RST) { |
| 888 | /* Certainly Bus Reset */ |
| 889 | shost_printk(KERN_WARNING, instance, |
| 890 | "bus reset interrupt\n"); |
| 891 | bus_reset_cleanup(instance); |
| 892 | } else { |
| 893 | dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n"); |
| 894 | } |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 895 | #ifdef SUN3_SCSI_VME |
| 896 | dregs->csr |= CSR_DMA_ENABLE; |
| 897 | #endif |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 898 | } |
| 899 | handled = 1; |
| 900 | } else { |
Finn Thain | 9af9fec | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 901 | dsprintk(NDEBUG_INTR, instance, "interrupt without IRQ bit\n"); |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 902 | #ifdef SUN3_SCSI_VME |
| 903 | dregs->csr |= CSR_DMA_ENABLE; |
| 904 | #endif |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 905 | } |
| 906 | |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 907 | spin_unlock_irqrestore(&hostdata->lock, flags); |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 908 | |
| 909 | return IRQ_RETVAL(handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 912 | /** |
| 913 | * NCR5380_select - attempt arbitration and selection for a given command |
| 914 | * @instance: the Scsi_Host instance |
| 915 | * @cmd: the scsi_cmnd to execute |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | * |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 917 | * This routine establishes an I_T_L nexus for a SCSI command. This involves |
| 918 | * ARBITRATION, SELECTION and MESSAGE OUT phases and an IDENTIFY message. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | * |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 920 | * Returns true if the operation should be retried. |
| 921 | * Returns false if it should not be retried. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 923 | * Side effects : |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 924 | * If bus busy, arbitration failed, etc, NCR5380_select() will exit |
| 925 | * with registers as they should have been on entry - ie |
| 926 | * SELECT_ENABLE will be set appropriately, the NCR5380 |
| 927 | * will cease to drive any SCSI bus signals. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | * |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 929 | * If successful : the I_T_L nexus will be established, and |
| 930 | * hostdata->connected will be set to cmd. |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 931 | * SELECT interrupt will be disabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | * |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 933 | * If failed (no target) : cmd->scsi_done() will be called, and the |
| 934 | * cmd->result host byte set to DID_BAD_TARGET. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | */ |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 936 | |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 937 | static bool NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd) |
Finn Thain | 4ab2a78 | 2017-01-15 18:50:57 -0500 | [diff] [blame] | 938 | __releases(&hostdata->lock) __acquires(&hostdata->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | { |
Finn Thain | e8a6014 | 2016-01-03 16:05:54 +1100 | [diff] [blame] | 940 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | unsigned char tmp[3], phase; |
| 942 | unsigned char *data; |
| 943 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | int err; |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 945 | bool ret = true; |
Finn Thain | 7c8ed78 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 946 | bool can_disconnect = instance->irq != NO_IRQ && |
| 947 | cmd->cmnd[0] != REQUEST_SENSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | NCR5380_dprint(NDEBUG_ARBITRATION, instance); |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 950 | dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n", |
| 951 | instance->this_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 953 | /* |
| 954 | * Arbitration and selection phases are slow and involve dropping the |
| 955 | * lock, so we have to watch out for EH. An exception handler may |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 956 | * change 'selecting' to NULL. This function will then return false |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 957 | * so that the caller will forget about 'cmd'. (During information |
| 958 | * transfer phases, EH may change 'connected' to NULL.) |
| 959 | */ |
| 960 | hostdata->selecting = cmd; |
| 961 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 962 | /* |
| 963 | * Set the phase bits to 0, otherwise the NCR5380 won't drive the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | * data bus during SELECTION. |
| 965 | */ |
| 966 | |
| 967 | NCR5380_write(TARGET_COMMAND_REG, 0); |
| 968 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 969 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | * Start arbitration. |
| 971 | */ |
| 972 | |
| 973 | NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask); |
| 974 | NCR5380_write(MODE_REG, MR_ARBITRATE); |
| 975 | |
Finn Thain | 55500d9 | 2016-01-03 16:05:35 +1100 | [diff] [blame] | 976 | /* The chip now waits for BUS FREE phase. Then after the 800 ns |
| 977 | * Bus Free Delay, arbitration will begin. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | */ |
| 979 | |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 980 | spin_unlock_irq(&hostdata->lock); |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 981 | err = NCR5380_poll_politely2(hostdata, MODE_REG, MR_ARBITRATE, 0, |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 982 | INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS, |
| 983 | ICR_ARBITRATION_PROGRESS, HZ); |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 984 | spin_lock_irq(&hostdata->lock); |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 985 | if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) { |
| 986 | /* Reselection interrupt */ |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 987 | goto out; |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 988 | } |
Finn Thain | ccf6efd | 2016-02-23 10:07:08 +1100 | [diff] [blame] | 989 | if (!hostdata->selecting) { |
| 990 | /* Command was aborted */ |
| 991 | NCR5380_write(MODE_REG, MR_BASE); |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 992 | return false; |
Finn Thain | ccf6efd | 2016-02-23 10:07:08 +1100 | [diff] [blame] | 993 | } |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 994 | if (err < 0) { |
| 995 | NCR5380_write(MODE_REG, MR_BASE); |
| 996 | shost_printk(KERN_ERR, instance, |
| 997 | "select: arbitration timeout\n"); |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 998 | goto out; |
Finn Thain | 55500d9 | 2016-01-03 16:05:35 +1100 | [diff] [blame] | 999 | } |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1000 | spin_unlock_irq(&hostdata->lock); |
Finn Thain | 55500d9 | 2016-01-03 16:05:35 +1100 | [diff] [blame] | 1001 | |
| 1002 | /* The SCSI-2 arbitration delay is 2.4 us */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | udelay(3); |
| 1004 | |
| 1005 | /* Check for lost arbitration */ |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1006 | if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) || |
| 1007 | (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) || |
| 1008 | (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | NCR5380_write(MODE_REG, MR_BASE); |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 1010 | dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n"); |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1011 | spin_lock_irq(&hostdata->lock); |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 1012 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | } |
Finn Thain | cf13b08 | 2016-01-03 16:05:18 +1100 | [diff] [blame] | 1014 | |
| 1015 | /* After/during arbitration, BSY should be asserted. |
| 1016 | * IBM DPES-31080 Version S31Q works now |
| 1017 | * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) |
| 1018 | */ |
| 1019 | NCR5380_write(INITIATOR_COMMAND_REG, |
| 1020 | ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1022 | /* |
| 1023 | * Again, bus clear + bus settle time is 1.2us, however, this is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | * a minimum so we'll udelay ceil(1.2) |
| 1025 | */ |
| 1026 | |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 1027 | if (hostdata->flags & FLAG_TOSHIBA_DELAY) |
| 1028 | udelay(15); |
| 1029 | else |
| 1030 | udelay(2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1032 | spin_lock_irq(&hostdata->lock); |
| 1033 | |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 1034 | /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */ |
| 1035 | if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 1036 | goto out; |
| 1037 | |
| 1038 | if (!hostdata->selecting) { |
| 1039 | NCR5380_write(MODE_REG, MR_BASE); |
| 1040 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1041 | return false; |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 1042 | } |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 1043 | |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 1044 | dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1046 | /* |
| 1047 | * Now that we have won arbitration, start Selection process, asserting |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | * the host and target ID's on the SCSI bus. |
| 1049 | */ |
| 1050 | |
Finn Thain | 3d07d22 | 2016-01-03 16:06:13 +1100 | [diff] [blame] | 1051 | NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1053 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | * Raise ATN while SEL is true before BSY goes false from arbitration, |
| 1055 | * since this is the only way to guarantee that we'll get a MESSAGE OUT |
| 1056 | * phase immediately after selection. |
| 1057 | */ |
| 1058 | |
Finn Thain | 3d07d22 | 2016-01-03 16:06:13 +1100 | [diff] [blame] | 1059 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY | |
| 1060 | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | NCR5380_write(MODE_REG, MR_BASE); |
| 1062 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1063 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | * Reselect interrupts must be turned off prior to the dropping of BSY, |
| 1065 | * otherwise we will trigger an interrupt. |
| 1066 | */ |
| 1067 | NCR5380_write(SELECT_ENABLE_REG, 0); |
| 1068 | |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1069 | spin_unlock_irq(&hostdata->lock); |
| 1070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | /* |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1072 | * The initiator shall then wait at least two deskew delays and release |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | * the BSY signal. |
| 1074 | */ |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1075 | udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
| 1077 | /* Reset BSY */ |
Finn Thain | 3d07d22 | 2016-01-03 16:06:13 +1100 | [diff] [blame] | 1078 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | |
| 1079 | ICR_ASSERT_ATN | ICR_ASSERT_SEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1081 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | * Something weird happens when we cease to drive BSY - looks |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1083 | * like the board/chip is letting us do another read before the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | * appropriate propagation delay has expired, and we're confusing |
| 1085 | * a BSY signal from ourselves as the target's response to SELECTION. |
| 1086 | * |
| 1087 | * A small delay (the 'C++' frontend breaks the pipeline with an |
| 1088 | * unnecessary jump, making it work on my 386-33/Trantor T128, the |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1089 | * tighter 'C' code breaks and requires this) solves the problem - |
| 1090 | * the 1 us delay is arbitrary, and only used because this delay will |
| 1091 | * be the same on other platforms and since it works here, it should |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | * work there. |
| 1093 | * |
| 1094 | * wingel suggests that this could be due to failing to wait |
| 1095 | * one deskew delay. |
| 1096 | */ |
| 1097 | |
| 1098 | udelay(1); |
| 1099 | |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 1100 | dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1102 | /* |
| 1103 | * The SCSI specification calls for a 250 ms timeout for the actual |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | * selection. |
| 1105 | */ |
| 1106 | |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1107 | err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_BSY, SR_BSY, |
Finn Thain | ae753a3 | 2016-01-03 16:05:23 +1100 | [diff] [blame] | 1108 | msecs_to_jiffies(250)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) { |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1111 | spin_lock_irq(&hostdata->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1113 | NCR5380_reselect(instance); |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1114 | if (!hostdata->connected) |
| 1115 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
Finn Thain | 6a6ff4a | 2016-01-03 16:06:04 +1100 | [diff] [blame] | 1116 | shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n"); |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 1117 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | } |
Finn Thain | ae753a3 | 2016-01-03 16:05:23 +1100 | [diff] [blame] | 1119 | |
| 1120 | if (err < 0) { |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1121 | spin_lock_irq(&hostdata->lock); |
Finn Thain | ae753a3 | 2016-01-03 16:05:23 +1100 | [diff] [blame] | 1122 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Finn Thain | ae753a3 | 2016-01-03 16:05:23 +1100 | [diff] [blame] | 1123 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
Finn Thain | 6a16283 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1124 | |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 1125 | /* Can't touch cmd if it has been reclaimed by the scsi ML */ |
Finn Thain | 6a16283 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1126 | if (!hostdata->selecting) |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1127 | return false; |
Finn Thain | 6a16283 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1128 | |
| 1129 | cmd->result = DID_BAD_TARGET << 16; |
| 1130 | complete_cmd(instance, cmd); |
| 1131 | dsprintk(NDEBUG_SELECTION, instance, |
| 1132 | "target did not respond within 250ms\n"); |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1133 | ret = false; |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 1134 | goto out; |
Finn Thain | ae753a3 | 2016-01-03 16:05:23 +1100 | [diff] [blame] | 1135 | } |
| 1136 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1137 | /* |
| 1138 | * No less than two deskew delays after the initiator detects the |
| 1139 | * BSY signal is true, it shall release the SEL signal and may |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | * change the DATA BUS. -wingel |
| 1141 | */ |
| 1142 | |
| 1143 | udelay(1); |
| 1144 | |
| 1145 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
| 1146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | /* |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1148 | * Since we followed the SCSI spec, and raised ATN while SEL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | * was true but before BSY was false during selection, the information |
| 1150 | * transfer phase should be a MESSAGE OUT phase so that we can send the |
| 1151 | * IDENTIFY message. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | */ |
| 1153 | |
| 1154 | /* Wait for start of REQ/ACK handshake */ |
| 1155 | |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1156 | err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ); |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1157 | spin_lock_irq(&hostdata->lock); |
Finn Thain | 1cc160e | 2016-01-03 16:05:32 +1100 | [diff] [blame] | 1158 | if (err < 0) { |
Finn Thain | 55500d9 | 2016-01-03 16:05:35 +1100 | [diff] [blame] | 1159 | shost_printk(KERN_ERR, instance, "select: REQ timeout\n"); |
| 1160 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 1162 | goto out; |
| 1163 | } |
| 1164 | if (!hostdata->selecting) { |
| 1165 | do_abort(instance); |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1166 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 1169 | dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n", |
| 1170 | scmd_id(cmd)); |
Finn Thain | 7c8ed78 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1171 | tmp[0] = IDENTIFY(can_disconnect, cmd->device->lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | |
| 1173 | len = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | data = tmp; |
| 1175 | phase = PHASE_MSGOUT; |
| 1176 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
Finn Thain | b15e791 | 2017-01-15 18:50:57 -0500 | [diff] [blame] | 1177 | if (len) { |
| 1178 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1179 | cmd->result = DID_ERROR << 16; |
| 1180 | complete_cmd(instance, cmd); |
| 1181 | dsprintk(NDEBUG_SELECTION, instance, "IDENTIFY message transfer failed\n"); |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1182 | ret = false; |
Finn Thain | b15e791 | 2017-01-15 18:50:57 -0500 | [diff] [blame] | 1183 | goto out; |
| 1184 | } |
| 1185 | |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 1186 | dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n"); |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | hostdata->connected = cmd; |
Finn Thain | 3d07d22 | 2016-01-03 16:06:13 +1100 | [diff] [blame] | 1189 | hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 1191 | #ifdef SUN3_SCSI_VME |
| 1192 | dregs->csr |= CSR_INTR; |
| 1193 | #endif |
| 1194 | |
Boaz Harrosh | 28424d3 | 2007-09-10 22:37:45 +0300 | [diff] [blame] | 1195 | initialize_SCp(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1197 | ret = false; |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 1198 | |
| 1199 | out: |
| 1200 | if (!hostdata->selecting) |
Finn Thain | 96edebd | 2018-10-24 18:45:33 +1100 | [diff] [blame] | 1201 | return false; |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 1202 | hostdata->selecting = NULL; |
Finn Thain | dad8261 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1203 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1206 | /* |
| 1207 | * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance, |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1208 | * unsigned char *phase, int *count, unsigned char **data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | * |
| 1210 | * Purpose : transfers data in given phase using polled I/O |
| 1211 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1212 | * Inputs : instance - instance of driver, *phase - pointer to |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1213 | * what phase is expected, *count - pointer to number of |
| 1214 | * bytes to transfer, **data - pointer to data pointer. |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1215 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | * Returns : -1 when different phase is entered without transferring |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1217 | * maximum number of bytes, 0 if all bytes are transferred or exit |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1218 | * is in same phase. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | * |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1220 | * Also, *phase, *count, *data are modified in place. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | * |
| 1222 | * XXX Note : handling for bus free may be useful. |
| 1223 | */ |
| 1224 | |
| 1225 | /* |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1226 | * Note : this code is not as quick as it could be, however it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | * IS 100% reliable, and for the actual data transfer where speed |
| 1228 | * counts, we will always do a pseudo DMA or DMA transfer. |
| 1229 | */ |
| 1230 | |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1231 | static int NCR5380_transfer_pio(struct Scsi_Host *instance, |
| 1232 | unsigned char *phase, int *count, |
| 1233 | unsigned char **data) |
| 1234 | { |
Finn Thain | 61e1ce5 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1235 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | unsigned char p = *phase, tmp; |
| 1237 | int c = *count; |
| 1238 | unsigned char *d = *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1240 | /* |
| 1241 | * The NCR5380 chip will only drive the SCSI bus when the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | * phase specified in the appropriate bits of the TARGET COMMAND |
| 1243 | * REGISTER match the STATUS REGISTER |
| 1244 | */ |
| 1245 | |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1246 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | do { |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1249 | /* |
| 1250 | * Wait for assertion of REQ, after which the phase bits will be |
| 1251 | * valid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | */ |
| 1253 | |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1254 | if (NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 1257 | dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
| 1259 | /* Check for phase mismatch */ |
Finn Thain | 686f399 | 2016-01-03 16:05:26 +1100 | [diff] [blame] | 1260 | if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) { |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 1261 | dsprintk(NDEBUG_PIO, instance, "phase mismatch\n"); |
| 1262 | NCR5380_dprint_phase(NDEBUG_PIO, instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | break; |
| 1264 | } |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | /* Do actual transfer from SCSI bus to / from memory */ |
| 1267 | if (!(p & SR_IO)) |
| 1268 | NCR5380_write(OUTPUT_DATA_REG, *d); |
| 1269 | else |
| 1270 | *d = NCR5380_read(CURRENT_SCSI_DATA_REG); |
| 1271 | |
| 1272 | ++d; |
| 1273 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1274 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | * The SCSI standard suggests that in MSGOUT phase, the initiator |
| 1276 | * should drop ATN on the last byte of the message phase |
| 1277 | * after REQ has been asserted for the handshake but before |
| 1278 | * the initiator raises ACK. |
| 1279 | */ |
| 1280 | |
| 1281 | if (!(p & SR_IO)) { |
| 1282 | if (!((p & SR_MSG) && c > 1)) { |
| 1283 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA); |
| 1284 | NCR5380_dprint(NDEBUG_PIO, instance); |
Finn Thain | 3d07d22 | 2016-01-03 16:06:13 +1100 | [diff] [blame] | 1285 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | |
| 1286 | ICR_ASSERT_DATA | ICR_ASSERT_ACK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | } else { |
Finn Thain | 3d07d22 | 2016-01-03 16:06:13 +1100 | [diff] [blame] | 1288 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | |
| 1289 | ICR_ASSERT_DATA | ICR_ASSERT_ATN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | NCR5380_dprint(NDEBUG_PIO, instance); |
Finn Thain | 3d07d22 | 2016-01-03 16:06:13 +1100 | [diff] [blame] | 1291 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | |
| 1292 | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | } |
| 1294 | } else { |
| 1295 | NCR5380_dprint(NDEBUG_PIO, instance); |
| 1296 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK); |
| 1297 | } |
| 1298 | |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1299 | if (NCR5380_poll_politely(hostdata, |
Finn Thain | a2edc4a | 2016-01-03 16:05:27 +1100 | [diff] [blame] | 1300 | STATUS_REG, SR_REQ, 0, 5 * HZ) < 0) |
| 1301 | break; |
| 1302 | |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 1303 | dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
| 1305 | /* |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1306 | * We have several special cases to consider during REQ/ACK handshaking : |
| 1307 | * 1. We were in MSGOUT phase, and we are on the last byte of the |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1308 | * message. ATN must be dropped as ACK is dropped. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1310 | * 2. We are in a MSGIN phase, and we are on the last byte of the |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1311 | * message. We must exit with ACK asserted, so that the calling |
| 1312 | * code may raise ATN before dropping ACK to reject the message. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | * |
| 1314 | * 3. ACK and ATN are clear and the target may proceed as normal. |
| 1315 | */ |
| 1316 | if (!(p == PHASE_MSGIN && c == 1)) { |
| 1317 | if (p == PHASE_MSGOUT && c > 1) |
| 1318 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
| 1319 | else |
| 1320 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1321 | } |
| 1322 | } while (--c); |
| 1323 | |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 1324 | dsprintk(NDEBUG_PIO, instance, "residual %d\n", c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
| 1326 | *count = c; |
| 1327 | *data = d; |
| 1328 | tmp = NCR5380_read(STATUS_REG); |
Finn Thain | a2edc4a | 2016-01-03 16:05:27 +1100 | [diff] [blame] | 1329 | /* The phase read from the bus is valid if either REQ is (already) |
| 1330 | * asserted or if ACK hasn't been released yet. The latter applies if |
| 1331 | * we're in MSG IN, DATA IN or STATUS and all bytes have been received. |
| 1332 | */ |
| 1333 | if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | *phase = tmp & PHASE_MASK; |
| 1335 | else |
| 1336 | *phase = PHASE_UNKNOWN; |
| 1337 | |
| 1338 | if (!c || (*phase == p)) |
| 1339 | return 0; |
| 1340 | else |
| 1341 | return -1; |
| 1342 | } |
| 1343 | |
| 1344 | /** |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 1345 | * do_reset - issue a reset command |
| 1346 | * @instance: adapter to reset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | * |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 1348 | * Issue a reset sequence to the NCR5380 and try and get the bus |
| 1349 | * back into sane shape. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | * |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 1351 | * This clears the reset interrupt flag because there may be no handler for |
| 1352 | * it. When the driver is initialized, the NCR5380_intr() handler has not yet |
| 1353 | * been installed. And when in EH we may have released the ST DMA interrupt. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | */ |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1355 | |
Finn Thain | 54d8fe4 | 2016-01-03 16:05:06 +1100 | [diff] [blame] | 1356 | static void do_reset(struct Scsi_Host *instance) |
| 1357 | { |
Finn Thain | 61e1ce5 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1358 | struct NCR5380_hostdata __maybe_unused *hostdata = shost_priv(instance); |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 1359 | unsigned long flags; |
| 1360 | |
| 1361 | local_irq_save(flags); |
| 1362 | NCR5380_write(TARGET_COMMAND_REG, |
| 1363 | PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST); |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 1365 | udelay(50); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 1367 | (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 1368 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
Finn Thain | 80d3eb6 | 2016-01-03 16:05:34 +1100 | [diff] [blame] | 1371 | /** |
| 1372 | * do_abort - abort the currently established nexus by going to |
| 1373 | * MESSAGE OUT phase and sending an ABORT message. |
| 1374 | * @instance: relevant scsi host instance |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | * |
Finn Thain | 80d3eb6 | 2016-01-03 16:05:34 +1100 | [diff] [blame] | 1376 | * Returns 0 on success, -1 on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | */ |
| 1378 | |
Finn Thain | 54d8fe4 | 2016-01-03 16:05:06 +1100 | [diff] [blame] | 1379 | static int do_abort(struct Scsi_Host *instance) |
| 1380 | { |
Finn Thain | 61e1ce5 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1381 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | unsigned char *msgptr, phase, tmp; |
| 1383 | int len; |
| 1384 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | |
| 1386 | /* Request message out phase */ |
| 1387 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
| 1388 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1389 | /* |
| 1390 | * Wait for the target to indicate a valid phase by asserting |
| 1391 | * REQ. Once this happens, we'll have either a MSGOUT phase |
| 1392 | * and can immediately send the ABORT message, or we'll have some |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | * other phase and will have to source/sink data. |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1394 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | * We really don't care what value was on the bus or what value |
| 1396 | * the target sees, so we just handshake. |
| 1397 | */ |
| 1398 | |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1399 | rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ); |
Finn Thain | 1cc160e | 2016-01-03 16:05:32 +1100 | [diff] [blame] | 1400 | if (rc < 0) |
Finn Thain | 80d3eb6 | 2016-01-03 16:05:34 +1100 | [diff] [blame] | 1401 | goto timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
Finn Thain | f35d347 | 2016-01-03 16:05:33 +1100 | [diff] [blame] | 1403 | tmp = NCR5380_read(STATUS_REG) & PHASE_MASK; |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp)); |
| 1406 | |
Finn Thain | f35d347 | 2016-01-03 16:05:33 +1100 | [diff] [blame] | 1407 | if (tmp != PHASE_MSGOUT) { |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1408 | NCR5380_write(INITIATOR_COMMAND_REG, |
| 1409 | ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK); |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1410 | rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, 0, 3 * HZ); |
Finn Thain | 1cc160e | 2016-01-03 16:05:32 +1100 | [diff] [blame] | 1411 | if (rc < 0) |
Finn Thain | 80d3eb6 | 2016-01-03 16:05:34 +1100 | [diff] [blame] | 1412 | goto timeout; |
| 1413 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | } |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | tmp = ABORT; |
| 1417 | msgptr = &tmp; |
| 1418 | len = 1; |
| 1419 | phase = PHASE_MSGOUT; |
Finn Thain | 54d8fe4 | 2016-01-03 16:05:06 +1100 | [diff] [blame] | 1420 | NCR5380_transfer_pio(instance, &phase, &len, &msgptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
| 1422 | /* |
| 1423 | * If we got here, and the command completed successfully, |
| 1424 | * we're about to go into bus free state. |
| 1425 | */ |
| 1426 | |
| 1427 | return len ? -1 : 0; |
Finn Thain | 80d3eb6 | 2016-01-03 16:05:34 +1100 | [diff] [blame] | 1428 | |
| 1429 | timeout: |
| 1430 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1431 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1434 | /* |
| 1435 | * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance, |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1436 | * unsigned char *phase, int *count, unsigned char **data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | * |
| 1438 | * Purpose : transfers data in given phase using either real |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1439 | * or pseudo DMA. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1441 | * Inputs : instance - instance of driver, *phase - pointer to |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1442 | * what phase is expected, *count - pointer to number of |
| 1443 | * bytes to transfer, **data - pointer to data pointer. |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1444 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | * Returns : -1 when different phase is entered without transferring |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1446 | * maximum number of bytes, 0 if all bytes or transferred or exit |
| 1447 | * is in same phase. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | * |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1449 | * Also, *phase, *count, *data are modified in place. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | */ |
| 1451 | |
| 1452 | |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1453 | static int NCR5380_transfer_dma(struct Scsi_Host *instance, |
| 1454 | unsigned char *phase, int *count, |
| 1455 | unsigned char **data) |
| 1456 | { |
| 1457 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Finn Thain | f0ea73a | 2016-03-23 21:10:26 +1100 | [diff] [blame] | 1458 | int c = *count; |
| 1459 | unsigned char p = *phase; |
| 1460 | unsigned char *d = *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | unsigned char tmp; |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 1462 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) { |
| 1465 | *phase = tmp; |
| 1466 | return -1; |
| 1467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 1469 | hostdata->connected->SCp.phase = p; |
| 1470 | |
| 1471 | if (p & SR_IO) { |
| 1472 | if (hostdata->read_overruns) |
| 1473 | c -= hostdata->read_overruns; |
| 1474 | else if (hostdata->flags & FLAG_DMA_FIXUP) |
| 1475 | --c; |
| 1476 | } |
| 1477 | |
| 1478 | dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n", |
| 1479 | (p & SR_IO) ? "receive" : "send", c, d); |
| 1480 | |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 1481 | #ifdef CONFIG_SUN3 |
| 1482 | /* send start chain */ |
| 1483 | sun3scsi_dma_start(c, *data); |
| 1484 | #endif |
| 1485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p)); |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 1487 | NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY | |
| 1488 | MR_ENABLE_EOP_INTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 1490 | if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) { |
| 1491 | /* On the Medusa, it is a must to initialize the DMA before |
| 1492 | * starting the NCR. This is also the cleaner way for the TT. |
| 1493 | */ |
| 1494 | if (p & SR_IO) |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1495 | result = NCR5380_dma_recv_setup(hostdata, d, c); |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 1496 | else |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1497 | result = NCR5380_dma_send_setup(hostdata, d, c); |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 1498 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1500 | /* |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1501 | * On the PAS16 at least I/O recovery delays are not needed here. |
| 1502 | * Everyone else seems to want them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | */ |
| 1504 | |
| 1505 | if (p & SR_IO) { |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 1506 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Finn Thain | e5d55d1 | 2016-03-23 21:10:16 +1100 | [diff] [blame] | 1507 | NCR5380_io_delay(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0); |
| 1509 | } else { |
Finn Thain | e5d55d1 | 2016-03-23 21:10:16 +1100 | [diff] [blame] | 1510 | NCR5380_io_delay(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA); |
Finn Thain | e5d55d1 | 2016-03-23 21:10:16 +1100 | [diff] [blame] | 1512 | NCR5380_io_delay(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | NCR5380_write(START_DMA_SEND_REG, 0); |
Finn Thain | e5d55d1 | 2016-03-23 21:10:16 +1100 | [diff] [blame] | 1514 | NCR5380_io_delay(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 1517 | #ifdef CONFIG_SUN3 |
| 1518 | #ifdef SUN3_SCSI_VME |
| 1519 | dregs->csr |= CSR_DMA_ENABLE; |
| 1520 | #endif |
| 1521 | sun3_dma_active = 1; |
| 1522 | #endif |
| 1523 | |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 1524 | if (hostdata->flags & FLAG_LATE_DMA_SETUP) { |
| 1525 | /* On the Falcon, the DMA setup must be done after the last |
| 1526 | * NCR access, else the DMA setup gets trashed! |
| 1527 | */ |
| 1528 | if (p & SR_IO) |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1529 | result = NCR5380_dma_recv_setup(hostdata, d, c); |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 1530 | else |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1531 | result = NCR5380_dma_send_setup(hostdata, d, c); |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */ |
| 1535 | if (result < 0) |
| 1536 | return result; |
| 1537 | |
| 1538 | /* For real DMA, result is the byte count. DMA interrupt is expected. */ |
| 1539 | if (result > 0) { |
| 1540 | hostdata->dma_len = result; |
| 1541 | return 0; |
| 1542 | } |
| 1543 | |
| 1544 | /* The result is zero iff pseudo DMA send/receive was completed. */ |
| 1545 | hostdata->dma_len = c; |
| 1546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | /* |
Finn Thain | e4dec68 | 2016-03-23 21:10:12 +1100 | [diff] [blame] | 1548 | * A note regarding the DMA errata workarounds for early NMOS silicon. |
Finn Thain | c16df32 | 2016-01-03 16:06:08 +1100 | [diff] [blame] | 1549 | * |
| 1550 | * For DMA sends, we want to wait until the last byte has been |
| 1551 | * transferred out over the bus before we turn off DMA mode. Alas, there |
| 1552 | * seems to be no terribly good way of doing this on a 5380 under all |
| 1553 | * conditions. For non-scatter-gather operations, we can wait until REQ |
| 1554 | * and ACK both go false, or until a phase mismatch occurs. Gather-sends |
| 1555 | * are nastier, since the device will be expecting more data than we |
| 1556 | * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we |
| 1557 | * could test Last Byte Sent to assure transfer (I imagine this is precisely |
| 1558 | * why this signal was added to the newer chips) but on the older 538[01] |
| 1559 | * this signal does not exist. The workaround for this lack is a watchdog; |
| 1560 | * we bail out of the wait-loop after a modest amount of wait-time if |
| 1561 | * the usual exit conditions are not met. Not a terribly clean or |
| 1562 | * correct solution :-% |
| 1563 | * |
| 1564 | * DMA receive is equally tricky due to a nasty characteristic of the NCR5380. |
| 1565 | * If the chip is in DMA receive mode, it will respond to a target's |
| 1566 | * REQ by latching the SCSI data into the INPUT DATA register and asserting |
| 1567 | * ACK, even if it has _already_ been notified by the DMA controller that |
| 1568 | * the current DMA transfer has completed! If the NCR5380 is then taken |
| 1569 | * out of DMA mode, this already-acknowledged byte is lost. This is |
| 1570 | * not a problem for "one DMA transfer per READ command", because |
| 1571 | * the situation will never arise... either all of the data is DMA'ed |
| 1572 | * properly, or the target switches to MESSAGE IN phase to signal a |
| 1573 | * disconnection (either operation bringing the DMA to a clean halt). |
| 1574 | * However, in order to handle scatter-receive, we must work around the |
Finn Thain | e4dec68 | 2016-03-23 21:10:12 +1100 | [diff] [blame] | 1575 | * problem. The chosen fix is to DMA fewer bytes, then check for the |
Finn Thain | c16df32 | 2016-01-03 16:06:08 +1100 | [diff] [blame] | 1576 | * condition before taking the NCR5380 out of DMA mode. One or two extra |
| 1577 | * bytes are transferred via PIO as necessary to fill out the original |
| 1578 | * request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | */ |
| 1580 | |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 1581 | if (hostdata->flags & FLAG_DMA_FIXUP) { |
| 1582 | if (p & SR_IO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | /* |
Finn Thain | e4dec68 | 2016-03-23 21:10:12 +1100 | [diff] [blame] | 1584 | * The workaround was to transfer fewer bytes than we |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1585 | * intended to with the pseudo-DMA read function, wait for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | * the chip to latch the last byte, read it, and then disable |
| 1587 | * pseudo-DMA mode. |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1588 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | * After REQ is asserted, the NCR5380 asserts DRQ and ACK. |
| 1590 | * REQ is deasserted when ACK is asserted, and not reasserted |
| 1591 | * until ACK goes false. Since the NCR5380 won't lower ACK |
| 1592 | * until DACK is asserted, which won't happen unless we twiddle |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1593 | * the DMA port or we take the NCR5380 out of DMA mode, we |
| 1594 | * can guarantee that we won't handshake another extra |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | * byte. |
| 1596 | */ |
| 1597 | |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1598 | if (NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 1599 | BASR_DRQ, BASR_DRQ, HZ) < 0) { |
Finn Thain | 438af51 | 2016-03-23 21:10:18 +1100 | [diff] [blame] | 1600 | result = -1; |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 1601 | shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | } |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1603 | if (NCR5380_poll_politely(hostdata, STATUS_REG, |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 1604 | SR_REQ, 0, HZ) < 0) { |
Finn Thain | 438af51 | 2016-03-23 21:10:18 +1100 | [diff] [blame] | 1605 | result = -1; |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 1606 | shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n"); |
| 1607 | } |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 1608 | d[*count - 1] = NCR5380_read(INPUT_DATA_REG); |
| 1609 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | /* |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1611 | * Wait for the last byte to be sent. If REQ is being asserted for |
| 1612 | * the byte we're interested, we'll ACK it and it will go false. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | */ |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1614 | if (NCR5380_poll_politely2(hostdata, |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 1615 | BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ, |
| 1616 | BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) { |
Finn Thain | 438af51 | 2016-03-23 21:10:18 +1100 | [diff] [blame] | 1617 | result = -1; |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 1618 | shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | } |
| 1620 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | } |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 1622 | |
| 1623 | NCR5380_dma_complete(instance); |
Finn Thain | 438af51 | 2016-03-23 21:10:18 +1100 | [diff] [blame] | 1624 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | |
| 1627 | /* |
| 1628 | * Function : NCR5380_information_transfer (struct Scsi_Host *instance) |
| 1629 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1630 | * Purpose : run through the various SCSI phases and do as the target |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1631 | * directs us to. Operates on the currently connected command, |
| 1632 | * instance->connected. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | * |
| 1634 | * Inputs : instance, instance for which we are doing commands |
| 1635 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1636 | * Side effects : SCSI things happen, the disconnected queue will be |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1637 | * modified if a command disconnects, *instance->connected will |
| 1638 | * change. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1640 | * XXX Note : we need to watch for bus free or a reset condition here |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 1641 | * to recover from an unexpected bus free condition. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | */ |
| 1643 | |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1644 | static void NCR5380_information_transfer(struct Scsi_Host *instance) |
Finn Thain | 4ab2a78 | 2017-01-15 18:50:57 -0500 | [diff] [blame] | 1645 | __releases(&hostdata->lock) __acquires(&hostdata->lock) |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1646 | { |
Finn Thain | e8a6014 | 2016-01-03 16:05:54 +1100 | [diff] [blame] | 1647 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | unsigned char msgout = NOP; |
| 1649 | int sink = 0; |
| 1650 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | int transfersize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | unsigned char *data; |
| 1653 | unsigned char phase, tmp, extended_msg[10], old_phase = 0xff; |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1654 | struct scsi_cmnd *cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 1656 | #ifdef SUN3_SCSI_VME |
| 1657 | dregs->csr |= CSR_INTR; |
| 1658 | #endif |
| 1659 | |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1660 | while ((cmd = hostdata->connected)) { |
Finn Thain | 32b26a1 | 2016-01-03 16:05:58 +1100 | [diff] [blame] | 1661 | struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd); |
| 1662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | tmp = NCR5380_read(STATUS_REG); |
| 1664 | /* We only have a valid SCSI phase when REQ is asserted */ |
| 1665 | if (tmp & SR_REQ) { |
| 1666 | phase = (tmp & PHASE_MASK); |
| 1667 | if (phase != old_phase) { |
| 1668 | old_phase = phase; |
| 1669 | NCR5380_dprint_phase(NDEBUG_INFORMATION, instance); |
| 1670 | } |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 1671 | #ifdef CONFIG_SUN3 |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1672 | if (phase == PHASE_CMDOUT && |
| 1673 | sun3_dma_setup_done != cmd) { |
| 1674 | int count; |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 1675 | |
| 1676 | if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) { |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1677 | ++cmd->SCp.buffer; |
| 1678 | --cmd->SCp.buffers_residual; |
| 1679 | cmd->SCp.this_residual = cmd->SCp.buffer->length; |
| 1680 | cmd->SCp.ptr = sg_virt(cmd->SCp.buffer); |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 1681 | } |
| 1682 | |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1683 | count = sun3scsi_dma_xfer_len(hostdata, cmd); |
| 1684 | |
| 1685 | if (count > 0) { |
| 1686 | if (rq_data_dir(cmd->request)) |
| 1687 | sun3scsi_dma_send_setup(hostdata, |
| 1688 | cmd->SCp.ptr, count); |
| 1689 | else |
| 1690 | sun3scsi_dma_recv_setup(hostdata, |
| 1691 | cmd->SCp.ptr, count); |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 1692 | sun3_dma_setup_done = cmd; |
| 1693 | } |
| 1694 | #ifdef SUN3_SCSI_VME |
| 1695 | dregs->csr |= CSR_INTR; |
| 1696 | #endif |
| 1697 | } |
| 1698 | #endif /* CONFIG_SUN3 */ |
| 1699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | if (sink && (phase != PHASE_MSGOUT)) { |
| 1701 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp)); |
| 1702 | |
Finn Thain | 3d07d22 | 2016-01-03 16:06:13 +1100 | [diff] [blame] | 1703 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | |
| 1704 | ICR_ASSERT_ACK); |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1705 | while (NCR5380_read(STATUS_REG) & SR_REQ) |
| 1706 | ; |
Finn Thain | 3d07d22 | 2016-01-03 16:06:13 +1100 | [diff] [blame] | 1707 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | |
| 1708 | ICR_ASSERT_ATN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | sink = 0; |
| 1710 | continue; |
| 1711 | } |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | switch (phase) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | case PHASE_DATAOUT: |
| 1715 | #if (NDEBUG & NDEBUG_NO_DATAOUT) |
Finn Thain | 6a6ff4a | 2016-01-03 16:06:04 +1100 | [diff] [blame] | 1716 | shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | sink = 1; |
| 1718 | do_abort(instance); |
| 1719 | cmd->result = DID_ERROR << 16; |
Finn Thain | 677e019 | 2016-01-03 16:05:59 +1100 | [diff] [blame] | 1720 | complete_cmd(instance, cmd); |
Finn Thain | dc18396 | 2016-02-23 10:07:07 +1100 | [diff] [blame] | 1721 | hostdata->connected = NULL; |
Finn Thain | 45ddc1b | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1722 | hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | return; |
| 1724 | #endif |
Finn Thain | bf1a0c6f | 2016-01-03 16:05:47 +1100 | [diff] [blame] | 1725 | case PHASE_DATAIN: |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1726 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | * If there is no room left in the current buffer in the |
| 1728 | * scatter-gather list, move onto the next one. |
| 1729 | */ |
| 1730 | |
| 1731 | if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) { |
| 1732 | ++cmd->SCp.buffer; |
| 1733 | --cmd->SCp.buffers_residual; |
| 1734 | cmd->SCp.this_residual = cmd->SCp.buffer->length; |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 1735 | cmd->SCp.ptr = sg_virt(cmd->SCp.buffer); |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 1736 | dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n", |
| 1737 | cmd->SCp.this_residual, |
| 1738 | cmd->SCp.buffers_residual); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | } |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | /* |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1742 | * The preferred transfer method is going to be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | * PSEUDO-DMA for systems that are strictly PIO, |
| 1744 | * since we can let the hardware do the handshaking. |
| 1745 | * |
| 1746 | * For this to work, we need to know the transfersize |
| 1747 | * ahead of time, since the pseudo-DMA code will sit |
| 1748 | * in an unconditional loop. |
| 1749 | */ |
| 1750 | |
Finn Thain | ff3d457 | 2016-01-03 16:05:25 +1100 | [diff] [blame] | 1751 | transfersize = 0; |
Finn Thain | 7e9ec8d | 2016-03-23 21:10:11 +1100 | [diff] [blame] | 1752 | if (!cmd->device->borken) |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1753 | transfersize = NCR5380_dma_xfer_len(hostdata, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
Finn Thain | 438af51 | 2016-03-23 21:10:18 +1100 | [diff] [blame] | 1755 | if (transfersize > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | len = transfersize; |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1757 | if (NCR5380_transfer_dma(instance, &phase, |
| 1758 | &len, (unsigned char **)&cmd->SCp.ptr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | /* |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1760 | * If the watchdog timer fires, all future |
| 1761 | * accesses to this device will use the |
| 1762 | * polled-IO. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | */ |
Jeff Garzik | 017560f | 2005-10-24 18:04:36 -0400 | [diff] [blame] | 1764 | scmd_printk(KERN_INFO, cmd, |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1765 | "switching to slow handshake\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | cmd->device->borken = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | sink = 1; |
| 1768 | do_abort(instance); |
| 1769 | cmd->result = DID_ERROR << 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | /* XXX - need to source or sink data here, as appropriate */ |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 1771 | } |
Finn Thain | f825e40 | 2016-03-23 21:10:15 +1100 | [diff] [blame] | 1772 | } else { |
Finn Thain | 08348b1 | 2016-08-31 14:44:56 +1000 | [diff] [blame] | 1773 | /* Transfer a small chunk so that the |
| 1774 | * irq mode lock is not held too long. |
Finn Thain | 1678847 | 2016-02-23 10:07:05 +1100 | [diff] [blame] | 1775 | */ |
Finn Thain | 08348b1 | 2016-08-31 14:44:56 +1000 | [diff] [blame] | 1776 | transfersize = min(cmd->SCp.this_residual, |
| 1777 | NCR5380_PIO_CHUNK_SIZE); |
Finn Thain | 1678847 | 2016-02-23 10:07:05 +1100 | [diff] [blame] | 1778 | len = transfersize; |
| 1779 | NCR5380_transfer_pio(instance, &phase, &len, |
Finn Thain | 3d07d22 | 2016-01-03 16:06:13 +1100 | [diff] [blame] | 1780 | (unsigned char **)&cmd->SCp.ptr); |
Finn Thain | 1678847 | 2016-02-23 10:07:05 +1100 | [diff] [blame] | 1781 | cmd->SCp.this_residual -= transfersize - len; |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1782 | } |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 1783 | #ifdef CONFIG_SUN3 |
| 1784 | if (sun3_dma_setup_done == cmd) |
| 1785 | sun3_dma_setup_done = NULL; |
| 1786 | #endif |
Finn Thain | 1678847 | 2016-02-23 10:07:05 +1100 | [diff] [blame] | 1787 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | case PHASE_MSGIN: |
| 1789 | len = 1; |
| 1790 | data = &tmp; |
| 1791 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
| 1792 | cmd->SCp.Message = tmp; |
| 1793 | |
| 1794 | switch (tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | case ABORT: |
| 1796 | case COMMAND_COMPLETE: |
| 1797 | /* Accept message by clearing ACK */ |
| 1798 | sink = 1; |
| 1799 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Finn Thain | 0d3d9a4 | 2016-01-03 16:05:55 +1100 | [diff] [blame] | 1800 | dsprintk(NDEBUG_QUEUES, instance, |
| 1801 | "COMMAND COMPLETE %p target %d lun %llu\n", |
| 1802 | cmd, scmd_id(cmd), cmd->device->lun); |
| 1803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | hostdata->connected = NULL; |
Finn Thain | 45ddc1b | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1805 | hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 1807 | cmd->result &= ~0xffff; |
| 1808 | cmd->result |= cmd->SCp.Status; |
| 1809 | cmd->result |= cmd->SCp.Message << 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 1811 | if (cmd->cmnd[0] == REQUEST_SENSE) |
Finn Thain | 677e019 | 2016-01-03 16:05:59 +1100 | [diff] [blame] | 1812 | complete_cmd(instance, cmd); |
Finn Thain | f27db8e | 2016-01-03 16:06:00 +1100 | [diff] [blame] | 1813 | else { |
| 1814 | if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION || |
| 1815 | cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) { |
| 1816 | dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n", |
| 1817 | cmd); |
| 1818 | list_add_tail(&ncmd->list, |
| 1819 | &hostdata->autosense); |
| 1820 | } else |
| 1821 | complete_cmd(instance, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1824 | /* |
| 1825 | * Restore phase bits to 0 so an interrupted selection, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | * arbitration can resume. |
| 1827 | */ |
| 1828 | NCR5380_write(TARGET_COMMAND_REG, 0); |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 1829 | |
| 1830 | /* Enable reselect interrupts */ |
| 1831 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 1832 | |
| 1833 | maybe_release_dma_irq(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | return; |
| 1835 | case MESSAGE_REJECT: |
| 1836 | /* Accept message by clearing ACK */ |
| 1837 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1838 | switch (hostdata->last_message) { |
| 1839 | case HEAD_OF_QUEUE_TAG: |
| 1840 | case ORDERED_QUEUE_TAG: |
| 1841 | case SIMPLE_QUEUE_TAG: |
| 1842 | cmd->device->simple_tags = 0; |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 1843 | hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | break; |
| 1845 | default: |
| 1846 | break; |
| 1847 | } |
Finn Thain | 340b961 | 2016-01-03 16:05:31 +1100 | [diff] [blame] | 1848 | break; |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1849 | case DISCONNECT: |
| 1850 | /* Accept message by clearing ACK */ |
| 1851 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1852 | hostdata->connected = NULL; |
| 1853 | list_add(&ncmd->list, &hostdata->disconnected); |
| 1854 | dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES, |
| 1855 | instance, "connected command %p for target %d lun %llu moved to disconnected queue\n", |
| 1856 | cmd, scmd_id(cmd), cmd->device->lun); |
Finn Thain | 0d3d9a4 | 2016-01-03 16:05:55 +1100 | [diff] [blame] | 1857 | |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1858 | /* |
| 1859 | * Restore phase bits to 0 so an interrupted selection, |
| 1860 | * arbitration can resume. |
| 1861 | */ |
| 1862 | NCR5380_write(TARGET_COMMAND_REG, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1864 | /* Enable reselect interrupts */ |
| 1865 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 1866 | #ifdef SUN3_SCSI_VME |
| 1867 | dregs->csr |= CSR_DMA_ENABLE; |
| 1868 | #endif |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1869 | return; |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1870 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | * The SCSI data pointer is *IMPLICITLY* saved on a disconnect |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1872 | * operation, in violation of the SCSI spec so we can safely |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | * ignore SAVE/RESTORE pointers calls. |
| 1874 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1875 | * Unfortunately, some disks violate the SCSI spec and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | * don't issue the required SAVE_POINTERS message before |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1877 | * disconnecting, and we have to break spec to remain |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | * compatible. |
| 1879 | */ |
| 1880 | case SAVE_POINTERS: |
| 1881 | case RESTORE_POINTERS: |
| 1882 | /* Accept message by clearing ACK */ |
| 1883 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1884 | break; |
| 1885 | case EXTENDED_MESSAGE: |
Finn Thain | c16df32 | 2016-01-03 16:06:08 +1100 | [diff] [blame] | 1886 | /* |
| 1887 | * Start the message buffer with the EXTENDED_MESSAGE |
| 1888 | * byte, since spi_print_msg() wants the whole thing. |
| 1889 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | extended_msg[0] = EXTENDED_MESSAGE; |
| 1891 | /* Accept first byte by clearing ACK */ |
| 1892 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1893 | |
| 1894 | spin_unlock_irq(&hostdata->lock); |
| 1895 | |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 1896 | dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | |
| 1898 | len = 2; |
| 1899 | data = extended_msg + 1; |
| 1900 | phase = PHASE_MSGIN; |
| 1901 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 1902 | dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n", |
| 1903 | (int)extended_msg[1], |
| 1904 | (int)extended_msg[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | |
Finn Thain | e0783ed | 2016-01-03 16:05:45 +1100 | [diff] [blame] | 1906 | if (!len && extended_msg[1] > 0 && |
| 1907 | extended_msg[1] <= sizeof(extended_msg) - 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | /* Accept third byte by clearing ACK */ |
| 1909 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1910 | len = extended_msg[1] - 1; |
| 1911 | data = extended_msg + 3; |
| 1912 | phase = PHASE_MSGIN; |
| 1913 | |
| 1914 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 1915 | dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n", |
| 1916 | len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
| 1918 | switch (extended_msg[2]) { |
| 1919 | case EXTENDED_SDTR: |
| 1920 | case EXTENDED_WDTR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | tmp = 0; |
| 1922 | } |
| 1923 | } else if (len) { |
Finn Thain | 6a6ff4a | 2016-01-03 16:06:04 +1100 | [diff] [blame] | 1924 | shost_printk(KERN_ERR, instance, "error receiving extended message\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | tmp = 0; |
| 1926 | } else { |
Finn Thain | 6a6ff4a | 2016-01-03 16:06:04 +1100 | [diff] [blame] | 1927 | shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n", |
| 1928 | extended_msg[2], extended_msg[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | tmp = 0; |
| 1930 | } |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1931 | |
| 1932 | spin_lock_irq(&hostdata->lock); |
| 1933 | if (!hostdata->connected) |
| 1934 | return; |
| 1935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | /* Fall through to reject message */ |
| 1937 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1938 | /* |
| 1939 | * If we get something weird that we aren't expecting, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | * reject it. |
| 1941 | */ |
| 1942 | default: |
Finn Thain | 39bef87 | 2017-10-26 16:51:50 +1100 | [diff] [blame] | 1943 | if (tmp == EXTENDED_MESSAGE) |
Jeff Garzik | 017560f | 2005-10-24 18:04:36 -0400 | [diff] [blame] | 1944 | scmd_printk(KERN_INFO, cmd, |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1945 | "rejecting unknown extended message code %02x, length %d\n", |
Finn Thain | 39bef87 | 2017-10-26 16:51:50 +1100 | [diff] [blame] | 1946 | extended_msg[2], extended_msg[1]); |
| 1947 | else if (tmp) |
| 1948 | scmd_printk(KERN_INFO, cmd, |
| 1949 | "rejecting unknown message code %02x\n", |
| 1950 | tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | |
| 1952 | msgout = MESSAGE_REJECT; |
| 1953 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
| 1954 | break; |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1955 | } /* switch (tmp) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | break; |
| 1957 | case PHASE_MSGOUT: |
| 1958 | len = 1; |
| 1959 | data = &msgout; |
| 1960 | hostdata->last_message = msgout; |
| 1961 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
| 1962 | if (msgout == ABORT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | hostdata->connected = NULL; |
Finn Thain | 45ddc1b | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 1964 | hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | cmd->result = DID_ERROR << 16; |
Finn Thain | 677e019 | 2016-01-03 16:05:59 +1100 | [diff] [blame] | 1966 | complete_cmd(instance, cmd); |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 1967 | maybe_release_dma_irq(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 1969 | return; |
| 1970 | } |
| 1971 | msgout = NOP; |
| 1972 | break; |
| 1973 | case PHASE_CMDOUT: |
| 1974 | len = cmd->cmd_len; |
| 1975 | data = cmd->cmnd; |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 1976 | /* |
| 1977 | * XXX for performance reasons, on machines with a |
| 1978 | * PSEUDO-DMA architecture we should probably |
| 1979 | * use the dma transfer function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | */ |
| 1981 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | break; |
| 1983 | case PHASE_STATIN: |
| 1984 | len = 1; |
| 1985 | data = &tmp; |
| 1986 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
| 1987 | cmd->SCp.Status = tmp; |
| 1988 | break; |
| 1989 | default: |
Finn Thain | 6a6ff4a | 2016-01-03 16:06:04 +1100 | [diff] [blame] | 1990 | shost_printk(KERN_ERR, instance, "unknown phase\n"); |
Finn Thain | 4dde8f7 | 2014-03-18 11:42:17 +1100 | [diff] [blame] | 1991 | NCR5380_dprint(NDEBUG_ANY, instance); |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 1992 | } /* switch(phase) */ |
Finn Thain | 686f399 | 2016-01-03 16:05:26 +1100 | [diff] [blame] | 1993 | } else { |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1994 | spin_unlock_irq(&hostdata->lock); |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 1995 | NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ); |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1996 | spin_lock_irq(&hostdata->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | } |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 1998 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | /* |
| 2002 | * Function : void NCR5380_reselect (struct Scsi_Host *instance) |
| 2003 | * |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 2004 | * Purpose : does reselection, initializing the instance->connected |
Finn Thain | 594d4ba | 2016-01-03 16:06:10 +1100 | [diff] [blame] | 2005 | * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q |
| 2006 | * nexus has been reestablished, |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 2007 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | * Inputs : instance - this instance of the NCR5380. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | */ |
| 2010 | |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 2011 | static void NCR5380_reselect(struct Scsi_Host *instance) |
| 2012 | { |
Finn Thain | e8a6014 | 2016-01-03 16:05:54 +1100 | [diff] [blame] | 2013 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | unsigned char target_mask; |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 2015 | unsigned char lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | unsigned char msg[3]; |
Finn Thain | 32b26a1 | 2016-01-03 16:05:58 +1100 | [diff] [blame] | 2017 | struct NCR5380_cmd *ncmd; |
| 2018 | struct scsi_cmnd *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | |
| 2020 | /* |
| 2021 | * Disable arbitration, etc. since the host adapter obviously |
| 2022 | * lost, and tell an interrupted NCR5380_select() to restart. |
| 2023 | */ |
| 2024 | |
| 2025 | NCR5380_write(MODE_REG, MR_BASE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | |
| 2027 | target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask); |
Finn Thain | 7ef55f6 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 2028 | if (!target_mask || target_mask & (target_mask - 1)) { |
| 2029 | shost_printk(KERN_WARNING, instance, |
| 2030 | "reselect: bad target_mask 0x%02x\n", target_mask); |
| 2031 | return; |
| 2032 | } |
Finn Thain | b746545 | 2016-01-03 16:06:05 +1100 | [diff] [blame] | 2033 | |
Finn Thain | aff0cf9 | 2016-01-03 16:06:09 +1100 | [diff] [blame] | 2034 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | * At this point, we have detected that our SCSI ID is on the bus, |
| 2036 | * SEL is true and BSY was false for at least one bus settle delay |
| 2037 | * (400 ns). |
| 2038 | * |
| 2039 | * We must assert BSY ourselves, until the target drops the SEL |
| 2040 | * signal. |
| 2041 | */ |
| 2042 | |
| 2043 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY); |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 2044 | if (NCR5380_poll_politely(hostdata, |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2045 | STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) { |
Finn Thain | 0826721 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 2046 | shost_printk(KERN_ERR, instance, "reselect: !SEL timeout\n"); |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2047 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2048 | return; |
| 2049 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2051 | |
| 2052 | /* |
| 2053 | * Wait for target to go into MSGIN. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | */ |
| 2055 | |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 2056 | if (NCR5380_poll_politely(hostdata, |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2057 | STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) { |
Finn Thain | ca694af | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 2058 | if ((NCR5380_read(STATUS_REG) & (SR_BSY | SR_SEL)) == 0) |
| 2059 | /* BUS FREE phase */ |
| 2060 | return; |
Finn Thain | 0826721 | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 2061 | shost_printk(KERN_ERR, instance, "reselect: REQ timeout\n"); |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2062 | do_abort(instance); |
| 2063 | return; |
| 2064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 2066 | #ifdef CONFIG_SUN3 |
| 2067 | /* acknowledge toggle to MSGIN */ |
| 2068 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 2070 | /* peek at the byte without really hitting the bus */ |
| 2071 | msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG); |
| 2072 | #else |
| 2073 | { |
| 2074 | int len = 1; |
| 2075 | unsigned char *data = msg; |
| 2076 | unsigned char phase = PHASE_MSGIN; |
| 2077 | |
| 2078 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
| 2079 | |
| 2080 | if (len) { |
| 2081 | do_abort(instance); |
| 2082 | return; |
| 2083 | } |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2084 | } |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 2085 | #endif /* CONFIG_SUN3 */ |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | if (!(msg[0] & 0x80)) { |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2088 | shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got "); |
Matthew Wilcox | 1abfd37 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 2089 | spi_print_msg(msg); |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2090 | printk("\n"); |
| 2091 | do_abort(instance); |
| 2092 | return; |
| 2093 | } |
| 2094 | lun = msg[0] & 0x07; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2096 | /* |
| 2097 | * We need to add code for SCSI-II to track which devices have |
| 2098 | * I_T_L_Q nexuses established, and which have simple I_T_L |
| 2099 | * nexuses so we can chose to do additional data transfer. |
| 2100 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2102 | /* |
| 2103 | * Find the command corresponding to the I_T_L or I_T_L_Q nexus we |
| 2104 | * just reestablished, and remove it from the disconnected queue. |
| 2105 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | |
Finn Thain | 32b26a1 | 2016-01-03 16:05:58 +1100 | [diff] [blame] | 2107 | tmp = NULL; |
| 2108 | list_for_each_entry(ncmd, &hostdata->disconnected, list) { |
| 2109 | struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd); |
| 2110 | |
| 2111 | if (target_mask == (1 << scmd_id(cmd)) && |
| 2112 | lun == (u8)cmd->device->lun) { |
| 2113 | list_del(&ncmd->list); |
| 2114 | tmp = cmd; |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2115 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | } |
| 2117 | } |
Finn Thain | 0d3d9a4 | 2016-01-03 16:05:55 +1100 | [diff] [blame] | 2118 | |
| 2119 | if (tmp) { |
| 2120 | dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance, |
| 2121 | "reselect: removed %p from disconnected queue\n", tmp); |
| 2122 | } else { |
Finn Thain | 45ddc1b | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 2123 | int target = ffs(target_mask) - 1; |
| 2124 | |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2125 | shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n", |
| 2126 | target_mask, lun); |
| 2127 | /* |
Finn Thain | 0d2cf86 | 2016-01-03 16:06:11 +1100 | [diff] [blame] | 2128 | * Since we have an established nexus that we can't do anything |
| 2129 | * with, we must abort it. |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2130 | */ |
Finn Thain | 45ddc1b | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 2131 | if (do_abort(instance) == 0) |
| 2132 | hostdata->busy[target] &= ~(1 << lun); |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2133 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | } |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2135 | |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 2136 | #ifdef CONFIG_SUN3 |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 2137 | if (sun3_dma_setup_done != tmp) { |
| 2138 | int count; |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 2139 | |
| 2140 | if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) { |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 2141 | ++tmp->SCp.buffer; |
| 2142 | --tmp->SCp.buffers_residual; |
| 2143 | tmp->SCp.this_residual = tmp->SCp.buffer->length; |
| 2144 | tmp->SCp.ptr = sg_virt(tmp->SCp.buffer); |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 2145 | } |
| 2146 | |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 2147 | count = sun3scsi_dma_xfer_len(hostdata, tmp); |
| 2148 | |
| 2149 | if (count > 0) { |
| 2150 | if (rq_data_dir(tmp->request)) |
| 2151 | sun3scsi_dma_send_setup(hostdata, |
| 2152 | tmp->SCp.ptr, count); |
| 2153 | else |
| 2154 | sun3scsi_dma_recv_setup(hostdata, |
| 2155 | tmp->SCp.ptr, count); |
Finn Thain | e9db319 | 2016-03-23 21:10:21 +1100 | [diff] [blame] | 2156 | sun3_dma_setup_done = tmp; |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK); |
| 2161 | #endif /* CONFIG_SUN3 */ |
| 2162 | |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2163 | /* Accept message by clearing ACK */ |
| 2164 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2165 | |
| 2166 | hostdata->connected = tmp; |
Finn Thain | c4ec6f9 | 2016-03-23 21:10:22 +1100 | [diff] [blame] | 2167 | dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n", |
| 2168 | scmd_id(tmp), tmp->device->lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2171 | /** |
| 2172 | * list_find_cmd - test for presence of a command in a linked list |
| 2173 | * @haystack: list of commands |
| 2174 | * @needle: command to search for |
| 2175 | */ |
| 2176 | |
| 2177 | static bool list_find_cmd(struct list_head *haystack, |
| 2178 | struct scsi_cmnd *needle) |
| 2179 | { |
| 2180 | struct NCR5380_cmd *ncmd; |
| 2181 | |
| 2182 | list_for_each_entry(ncmd, haystack, list) |
| 2183 | if (NCR5380_to_scmd(ncmd) == needle) |
| 2184 | return true; |
| 2185 | return false; |
| 2186 | } |
| 2187 | |
| 2188 | /** |
| 2189 | * list_remove_cmd - remove a command from linked list |
| 2190 | * @haystack: list of commands |
| 2191 | * @needle: command to remove |
| 2192 | */ |
| 2193 | |
| 2194 | static bool list_del_cmd(struct list_head *haystack, |
| 2195 | struct scsi_cmnd *needle) |
| 2196 | { |
| 2197 | if (list_find_cmd(haystack, needle)) { |
| 2198 | struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle); |
| 2199 | |
| 2200 | list_del(&ncmd->list); |
| 2201 | return true; |
| 2202 | } |
| 2203 | return false; |
| 2204 | } |
| 2205 | |
| 2206 | /** |
| 2207 | * NCR5380_abort - scsi host eh_abort_handler() method |
| 2208 | * @cmd: the command to be aborted |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | * |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2210 | * Try to abort a given command by removing it from queues and/or sending |
| 2211 | * the target an abort message. This may not succeed in causing a target |
| 2212 | * to abort the command. Nonetheless, the low-level driver must forget about |
| 2213 | * the command because the mid-layer reclaims it and it may be re-issued. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | * |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2215 | * The normal path taken by a command is as follows. For EH we trace this |
| 2216 | * same path to locate and abort the command. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | * |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2218 | * unissued -> selecting -> [unissued -> selecting ->]... connected -> |
| 2219 | * [disconnected -> connected ->]... |
| 2220 | * [autosense -> connected ->] done |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | * |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2222 | * If cmd was not found at all then presumably it has already been completed, |
| 2223 | * in which case return SUCCESS to try to avoid further EH measures. |
Finn Thain | dc18396 | 2016-02-23 10:07:07 +1100 | [diff] [blame] | 2224 | * |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2225 | * If the command has not completed yet, we must not fail to find it. |
Finn Thain | dc18396 | 2016-02-23 10:07:07 +1100 | [diff] [blame] | 2226 | * We have no option but to forget the aborted command (even if it still |
| 2227 | * lacks sense data). The mid-layer may re-issue a command that is in error |
| 2228 | * recovery (see scsi_send_eh_cmnd), but the logic and data structures in |
| 2229 | * this driver are such that a command can appear on one queue only. |
Finn Thain | 71a0059 | 2016-02-23 10:07:06 +1100 | [diff] [blame] | 2230 | * |
| 2231 | * The lock protects driver data structures, but EH handlers also use it |
| 2232 | * to serialize their own execution and prevent their own re-entry. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | */ |
| 2234 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2235 | static int NCR5380_abort(struct scsi_cmnd *cmd) |
| 2236 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | struct Scsi_Host *instance = cmd->device->host; |
Finn Thain | e8a6014 | 2016-01-03 16:05:54 +1100 | [diff] [blame] | 2238 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 2239 | unsigned long flags; |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2240 | int result = SUCCESS; |
Hannes Reinecke | 1fa6b5f | 2014-10-24 14:26:58 +0200 | [diff] [blame] | 2241 | |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 2242 | spin_lock_irqsave(&hostdata->lock, flags); |
| 2243 | |
Finn Thain | 32b26a1 | 2016-01-03 16:05:58 +1100 | [diff] [blame] | 2244 | #if (NDEBUG & NDEBUG_ANY) |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2245 | scmd_printk(KERN_INFO, cmd, __func__); |
Finn Thain | 32b26a1 | 2016-01-03 16:05:58 +1100 | [diff] [blame] | 2246 | #endif |
Finn Thain | e5c3fdd | 2016-01-03 16:05:52 +1100 | [diff] [blame] | 2247 | NCR5380_dprint(NDEBUG_ANY, instance); |
| 2248 | NCR5380_dprint_phase(NDEBUG_ANY, instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2250 | if (list_del_cmd(&hostdata->unissued, cmd)) { |
| 2251 | dsprintk(NDEBUG_ABORT, instance, |
| 2252 | "abort: removed %p from issue queue\n", cmd); |
| 2253 | cmd->result = DID_ABORT << 16; |
| 2254 | cmd->scsi_done(cmd); /* No tag or busy flag to worry about */ |
Finn Thain | dc18396 | 2016-02-23 10:07:07 +1100 | [diff] [blame] | 2255 | goto out; |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2256 | } |
| 2257 | |
Finn Thain | 707d62b | 2016-01-03 16:06:02 +1100 | [diff] [blame] | 2258 | if (hostdata->selecting == cmd) { |
| 2259 | dsprintk(NDEBUG_ABORT, instance, |
| 2260 | "abort: cmd %p == selecting\n", cmd); |
| 2261 | hostdata->selecting = NULL; |
| 2262 | cmd->result = DID_ABORT << 16; |
| 2263 | complete_cmd(instance, cmd); |
| 2264 | goto out; |
| 2265 | } |
| 2266 | |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2267 | if (list_del_cmd(&hostdata->disconnected, cmd)) { |
| 2268 | dsprintk(NDEBUG_ABORT, instance, |
| 2269 | "abort: removed %p from disconnected list\n", cmd); |
Finn Thain | 71a0059 | 2016-02-23 10:07:06 +1100 | [diff] [blame] | 2270 | /* Can't call NCR5380_select() and send ABORT because that |
| 2271 | * means releasing the lock. Need a bus reset. |
| 2272 | */ |
Finn Thain | dc18396 | 2016-02-23 10:07:07 +1100 | [diff] [blame] | 2273 | set_host_byte(cmd, DID_ERROR); |
| 2274 | complete_cmd(instance, cmd); |
Finn Thain | 71a0059 | 2016-02-23 10:07:06 +1100 | [diff] [blame] | 2275 | result = FAILED; |
| 2276 | goto out; |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | if (hostdata->connected == cmd) { |
| 2280 | dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd); |
| 2281 | hostdata->connected = NULL; |
Finn Thain | dc18396 | 2016-02-23 10:07:07 +1100 | [diff] [blame] | 2282 | hostdata->dma_len = 0; |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2283 | if (do_abort(instance)) { |
| 2284 | set_host_byte(cmd, DID_ERROR); |
| 2285 | complete_cmd(instance, cmd); |
| 2286 | result = FAILED; |
| 2287 | goto out; |
| 2288 | } |
| 2289 | set_host_byte(cmd, DID_ABORT); |
Finn Thain | dc18396 | 2016-02-23 10:07:07 +1100 | [diff] [blame] | 2290 | complete_cmd(instance, cmd); |
| 2291 | goto out; |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2292 | } |
| 2293 | |
Finn Thain | dc18396 | 2016-02-23 10:07:07 +1100 | [diff] [blame] | 2294 | if (list_del_cmd(&hostdata->autosense, cmd)) { |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2295 | dsprintk(NDEBUG_ABORT, instance, |
Finn Thain | dc18396 | 2016-02-23 10:07:07 +1100 | [diff] [blame] | 2296 | "abort: removed %p from sense queue\n", cmd); |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2297 | complete_cmd(instance, cmd); |
| 2298 | } |
| 2299 | |
| 2300 | out: |
| 2301 | if (result == FAILED) |
| 2302 | dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd); |
Finn Thain | 45ddc1b | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 2303 | else { |
| 2304 | hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun); |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2305 | dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd); |
Finn Thain | 45ddc1b | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 2306 | } |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2307 | |
| 2308 | queue_work(hostdata->work_q, &hostdata->main_task); |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 2309 | maybe_release_dma_irq(instance); |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 2310 | spin_unlock_irqrestore(&hostdata->lock, flags); |
Finn Thain | 32b26a1 | 2016-01-03 16:05:58 +1100 | [diff] [blame] | 2311 | |
Finn Thain | 8b00c3d | 2016-01-03 16:06:01 +1100 | [diff] [blame] | 2312 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | |
Finn Thain | 6b0e87a | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 2316 | static void bus_reset_cleanup(struct Scsi_Host *instance) |
Jeff Garzik | 68b3aa7 | 2005-05-28 07:56:31 -0400 | [diff] [blame] | 2317 | { |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 2318 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Finn Thain | 62717f5 | 2016-01-03 16:06:03 +1100 | [diff] [blame] | 2319 | int i; |
Finn Thain | 62717f5 | 2016-01-03 16:06:03 +1100 | [diff] [blame] | 2320 | struct NCR5380_cmd *ncmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | |
Finn Thain | 62717f5 | 2016-01-03 16:06:03 +1100 | [diff] [blame] | 2322 | /* reset NCR registers */ |
| 2323 | NCR5380_write(MODE_REG, MR_BASE); |
| 2324 | NCR5380_write(TARGET_COMMAND_REG, 0); |
| 2325 | NCR5380_write(SELECT_ENABLE_REG, 0); |
| 2326 | |
| 2327 | /* After the reset, there are no more connected or disconnected commands |
| 2328 | * and no busy units; so clear the low-level status here to avoid |
| 2329 | * conflicts when the mid-level code tries to wake up the affected |
| 2330 | * commands! |
| 2331 | */ |
| 2332 | |
Finn Thain | 1884c28 | 2016-02-23 10:07:04 +1100 | [diff] [blame] | 2333 | if (hostdata->selecting) { |
| 2334 | hostdata->selecting->result = DID_RESET << 16; |
| 2335 | complete_cmd(instance, hostdata->selecting); |
| 2336 | hostdata->selecting = NULL; |
| 2337 | } |
Finn Thain | 62717f5 | 2016-01-03 16:06:03 +1100 | [diff] [blame] | 2338 | |
| 2339 | list_for_each_entry(ncmd, &hostdata->disconnected, list) { |
| 2340 | struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd); |
| 2341 | |
| 2342 | set_host_byte(cmd, DID_RESET); |
Finn Thain | 216fad9 | 2016-03-23 21:10:32 +1100 | [diff] [blame] | 2343 | complete_cmd(instance, cmd); |
Finn Thain | 62717f5 | 2016-01-03 16:06:03 +1100 | [diff] [blame] | 2344 | } |
Finn Thain | 1884c28 | 2016-02-23 10:07:04 +1100 | [diff] [blame] | 2345 | INIT_LIST_HEAD(&hostdata->disconnected); |
Finn Thain | 62717f5 | 2016-01-03 16:06:03 +1100 | [diff] [blame] | 2346 | |
| 2347 | list_for_each_entry(ncmd, &hostdata->autosense, list) { |
| 2348 | struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd); |
| 2349 | |
Finn Thain | 62717f5 | 2016-01-03 16:06:03 +1100 | [diff] [blame] | 2350 | cmd->scsi_done(cmd); |
| 2351 | } |
Finn Thain | 1884c28 | 2016-02-23 10:07:04 +1100 | [diff] [blame] | 2352 | INIT_LIST_HEAD(&hostdata->autosense); |
Finn Thain | 62717f5 | 2016-01-03 16:06:03 +1100 | [diff] [blame] | 2353 | |
| 2354 | if (hostdata->connected) { |
| 2355 | set_host_byte(hostdata->connected, DID_RESET); |
| 2356 | complete_cmd(instance, hostdata->connected); |
| 2357 | hostdata->connected = NULL; |
| 2358 | } |
| 2359 | |
Finn Thain | 62717f5 | 2016-01-03 16:06:03 +1100 | [diff] [blame] | 2360 | for (i = 0; i < 8; ++i) |
| 2361 | hostdata->busy[i] = 0; |
Finn Thain | 62717f5 | 2016-01-03 16:06:03 +1100 | [diff] [blame] | 2362 | hostdata->dma_len = 0; |
Finn Thain | 62717f5 | 2016-01-03 16:06:03 +1100 | [diff] [blame] | 2363 | |
| 2364 | queue_work(hostdata->work_q, &hostdata->main_task); |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 2365 | maybe_release_dma_irq(instance); |
Finn Thain | 6b0e87a | 2018-09-27 11:17:11 +1000 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | /** |
| 2369 | * NCR5380_host_reset - reset the SCSI host |
| 2370 | * @cmd: SCSI command undergoing EH |
| 2371 | * |
| 2372 | * Returns SUCCESS |
| 2373 | */ |
| 2374 | |
| 2375 | static int NCR5380_host_reset(struct scsi_cmnd *cmd) |
| 2376 | { |
| 2377 | struct Scsi_Host *instance = cmd->device->host; |
| 2378 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 2379 | unsigned long flags; |
| 2380 | struct NCR5380_cmd *ncmd; |
| 2381 | |
| 2382 | spin_lock_irqsave(&hostdata->lock, flags); |
| 2383 | |
| 2384 | #if (NDEBUG & NDEBUG_ANY) |
| 2385 | shost_printk(KERN_INFO, instance, __func__); |
| 2386 | #endif |
| 2387 | NCR5380_dprint(NDEBUG_ANY, instance); |
| 2388 | NCR5380_dprint_phase(NDEBUG_ANY, instance); |
| 2389 | |
| 2390 | list_for_each_entry(ncmd, &hostdata->unissued, list) { |
| 2391 | struct scsi_cmnd *scmd = NCR5380_to_scmd(ncmd); |
| 2392 | |
| 2393 | scmd->result = DID_RESET << 16; |
| 2394 | scmd->scsi_done(scmd); |
| 2395 | } |
| 2396 | INIT_LIST_HEAD(&hostdata->unissued); |
| 2397 | |
| 2398 | do_reset(instance); |
| 2399 | bus_reset_cleanup(instance); |
| 2400 | |
Finn Thain | 11d2f63 | 2016-01-03 16:05:51 +1100 | [diff] [blame] | 2401 | spin_unlock_irqrestore(&hostdata->lock, flags); |
Jeff Garzik | 68b3aa7 | 2005-05-28 07:56:31 -0400 | [diff] [blame] | 2402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | return SUCCESS; |
| 2404 | } |