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