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