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