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