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