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