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