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