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