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