Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1 | /* |
| 2 | * USB Attached SCSI |
| 3 | * Note that this is not the same as the USB Mass Storage driver |
| 4 | * |
| 5 | * Copyright Matthew Wilcox for Intel Corp, 2010 |
| 6 | * Copyright Sarah Sharp for Intel Corp, 2010 |
| 7 | * |
| 8 | * Distributed under the terms of the GNU GPL, version two. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/blkdev.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/types.h> |
Paul Gortmaker | 6eb0de8 | 2011-07-03 16:09:31 -0400 | [diff] [blame] | 14 | #include <linux/module.h> |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 15 | #include <linux/usb.h> |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 16 | #include <linux/usb_usual.h> |
Sebastian Andrzej Siewior | c898add | 2012-01-11 12:42:32 +0100 | [diff] [blame] | 17 | #include <linux/usb/hcd.h> |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 18 | #include <linux/usb/storage.h> |
Sebastian Andrzej Siewior | 348748b | 2012-01-11 12:45:56 +0100 | [diff] [blame] | 19 | #include <linux/usb/uas.h> |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 20 | |
| 21 | #include <scsi/scsi.h> |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 22 | #include <scsi/scsi_eh.h> |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 23 | #include <scsi/scsi_dbg.h> |
| 24 | #include <scsi/scsi_cmnd.h> |
| 25 | #include <scsi/scsi_device.h> |
| 26 | #include <scsi/scsi_host.h> |
| 27 | #include <scsi/scsi_tcq.h> |
| 28 | |
Hans de Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 29 | #include "uas-detect.h" |
| 30 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 31 | /* |
| 32 | * The r00-r01c specs define this version of the SENSE IU data structure. |
| 33 | * It's still in use by several different firmware releases. |
| 34 | */ |
| 35 | struct sense_iu_old { |
| 36 | __u8 iu_id; |
| 37 | __u8 rsvd1; |
| 38 | __be16 tag; |
| 39 | __be16 len; |
| 40 | __u8 status; |
| 41 | __u8 service_response; |
| 42 | __u8 sense[SCSI_SENSE_BUFFERSIZE]; |
| 43 | }; |
| 44 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 45 | struct uas_dev_info { |
| 46 | struct usb_interface *intf; |
| 47 | struct usb_device *udev; |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 48 | struct usb_anchor cmd_urbs; |
Gerd Hoffmann | bdd000f | 2012-06-19 09:54:53 +0200 | [diff] [blame] | 49 | struct usb_anchor sense_urbs; |
| 50 | struct usb_anchor data_urbs; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 51 | int qdepth, resetting; |
Hans de Goede | e52e031 | 2013-10-23 14:27:09 +0100 | [diff] [blame] | 52 | struct response_iu response; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 53 | unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe; |
| 54 | unsigned use_streams:1; |
| 55 | unsigned uas_sense_old:1; |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 56 | unsigned running_task:1; |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 57 | unsigned shutdown:1; |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 58 | struct scsi_cmnd *cmnd; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 59 | spinlock_t lock; |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 60 | struct work_struct work; |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame] | 61 | struct list_head inflight_list; |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 62 | struct list_head dead_list; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | enum { |
Matthew Wilcox | 92a3f76 | 2010-12-15 15:44:04 -0500 | [diff] [blame] | 66 | SUBMIT_STATUS_URB = (1 << 1), |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 67 | ALLOC_DATA_IN_URB = (1 << 2), |
| 68 | SUBMIT_DATA_IN_URB = (1 << 3), |
| 69 | ALLOC_DATA_OUT_URB = (1 << 4), |
| 70 | SUBMIT_DATA_OUT_URB = (1 << 5), |
| 71 | ALLOC_CMD_URB = (1 << 6), |
| 72 | SUBMIT_CMD_URB = (1 << 7), |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 73 | COMMAND_INFLIGHT = (1 << 8), |
| 74 | DATA_IN_URB_INFLIGHT = (1 << 9), |
| 75 | DATA_OUT_URB_INFLIGHT = (1 << 10), |
| 76 | COMMAND_COMPLETED = (1 << 11), |
Gerd Hoffmann | ef018cc9 | 2012-09-25 10:47:06 +0200 | [diff] [blame] | 77 | COMMAND_ABORTED = (1 << 12), |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 78 | UNLINK_DATA_URBS = (1 << 13), |
Gerd Hoffmann | efefecf | 2012-11-30 11:54:42 +0100 | [diff] [blame] | 79 | IS_IN_WORK_LIST = (1 << 14), |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /* Overrides scsi_pointer */ |
| 83 | struct uas_cmd_info { |
| 84 | unsigned int state; |
| 85 | unsigned int stream; |
| 86 | struct urb *cmd_urb; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 87 | struct urb *data_in_urb; |
| 88 | struct urb *data_out_urb; |
Hans de Goede | 040d1a8 | 2013-11-12 14:02:12 +0100 | [diff] [blame^] | 89 | struct list_head list; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | /* I hate forward declarations, but I actually have a loop */ |
| 93 | static int uas_submit_urbs(struct scsi_cmnd *cmnd, |
| 94 | struct uas_dev_info *devinfo, gfp_t gfp); |
Sarah Sharp | ea9da1c | 2011-12-02 11:55:44 -0800 | [diff] [blame] | 95 | static void uas_do_work(struct work_struct *work); |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 96 | static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller); |
Gerd Hoffmann | d89bd83 | 2013-09-13 13:27:11 +0200 | [diff] [blame] | 97 | static void uas_free_streams(struct uas_dev_info *devinfo); |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 98 | static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 99 | |
Hans de Goede | 7e50e0b | 2013-10-17 19:19:04 +0200 | [diff] [blame] | 100 | /* Must be called with devinfo->lock held, will temporary unlock the lock */ |
Gerd Hoffmann | aa8f612 | 2012-11-30 11:54:40 +0100 | [diff] [blame] | 101 | static void uas_unlink_data_urbs(struct uas_dev_info *devinfo, |
Hans de Goede | 7e50e0b | 2013-10-17 19:19:04 +0200 | [diff] [blame] | 102 | struct uas_cmd_info *cmdinfo, |
| 103 | unsigned long *lock_flags) |
Gerd Hoffmann | aa8f612 | 2012-11-30 11:54:40 +0100 | [diff] [blame] | 104 | { |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 105 | /* |
| 106 | * The UNLINK_DATA_URBS flag makes sure uas_try_complete |
| 107 | * (called by urb completion) doesn't release cmdinfo |
| 108 | * underneath us. |
| 109 | */ |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 110 | cmdinfo->state |= UNLINK_DATA_URBS; |
Hans de Goede | 7e50e0b | 2013-10-17 19:19:04 +0200 | [diff] [blame] | 111 | spin_unlock_irqrestore(&devinfo->lock, *lock_flags); |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 112 | |
Gerd Hoffmann | aa8f612 | 2012-11-30 11:54:40 +0100 | [diff] [blame] | 113 | if (cmdinfo->data_in_urb) |
| 114 | usb_unlink_urb(cmdinfo->data_in_urb); |
| 115 | if (cmdinfo->data_out_urb) |
| 116 | usb_unlink_urb(cmdinfo->data_out_urb); |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 117 | |
Hans de Goede | 7e50e0b | 2013-10-17 19:19:04 +0200 | [diff] [blame] | 118 | spin_lock_irqsave(&devinfo->lock, *lock_flags); |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 119 | cmdinfo->state &= ~UNLINK_DATA_URBS; |
Gerd Hoffmann | aa8f612 | 2012-11-30 11:54:40 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 122 | static void uas_do_work(struct work_struct *work) |
| 123 | { |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 124 | struct uas_dev_info *devinfo = |
| 125 | container_of(work, struct uas_dev_info, work); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 126 | struct uas_cmd_info *cmdinfo; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 127 | unsigned long flags; |
Sarah Sharp | ea9da1c | 2011-12-02 11:55:44 -0800 | [diff] [blame] | 128 | int err; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 129 | |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 130 | spin_lock_irqsave(&devinfo->lock, flags); |
Hans de Goede | 040d1a8 | 2013-11-12 14:02:12 +0100 | [diff] [blame^] | 131 | list_for_each_entry(cmdinfo, &devinfo->inflight_list, list) { |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 132 | struct scsi_pointer *scp = (void *)cmdinfo; |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 133 | struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, |
| 134 | SCp); |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame] | 135 | |
| 136 | if (!(cmdinfo->state & IS_IN_WORK_LIST)) |
| 137 | continue; |
| 138 | |
Hans de Goede | e36e649 | 2013-11-07 08:35:55 +0100 | [diff] [blame] | 139 | err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO); |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame] | 140 | if (!err) |
Gerd Hoffmann | efefecf | 2012-11-30 11:54:42 +0100 | [diff] [blame] | 141 | cmdinfo->state &= ~IS_IN_WORK_LIST; |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame] | 142 | else |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 143 | schedule_work(&devinfo->work); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 144 | } |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 145 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Hans de Goede | da3033e | 2013-11-12 13:57:24 +0100 | [diff] [blame] | 148 | static void uas_mark_cmd_dead(struct uas_dev_info *devinfo, |
| 149 | struct uas_cmd_info *cmdinfo, const char *caller) |
| 150 | { |
| 151 | struct scsi_pointer *scp = (void *)cmdinfo; |
| 152 | struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp); |
| 153 | |
| 154 | uas_log_cmd_state(cmnd, caller); |
| 155 | WARN_ON_ONCE(!spin_is_locked(&devinfo->lock)); |
| 156 | WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED); |
| 157 | cmdinfo->state |= COMMAND_ABORTED; |
| 158 | cmdinfo->state &= ~IS_IN_WORK_LIST; |
Hans de Goede | 040d1a8 | 2013-11-12 14:02:12 +0100 | [diff] [blame^] | 159 | list_move_tail(&cmdinfo->list, &devinfo->dead_list); |
Hans de Goede | da3033e | 2013-11-12 13:57:24 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame] | 162 | static void uas_abort_inflight(struct uas_dev_info *devinfo) |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 163 | { |
| 164 | struct uas_cmd_info *cmdinfo; |
| 165 | struct uas_cmd_info *temp; |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 166 | unsigned long flags; |
| 167 | |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 168 | spin_lock_irqsave(&devinfo->lock, flags); |
Hans de Goede | 040d1a8 | 2013-11-12 14:02:12 +0100 | [diff] [blame^] | 169 | list_for_each_entry_safe(cmdinfo, temp, &devinfo->inflight_list, list) |
Hans de Goede | da3033e | 2013-11-12 13:57:24 +0100 | [diff] [blame] | 170 | uas_mark_cmd_dead(devinfo, cmdinfo, __func__); |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 171 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 172 | } |
| 173 | |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 174 | static void uas_add_work(struct uas_cmd_info *cmdinfo) |
| 175 | { |
| 176 | struct scsi_pointer *scp = (void *)cmdinfo; |
| 177 | struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp); |
| 178 | struct uas_dev_info *devinfo = cmnd->device->hostdata; |
| 179 | |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 180 | WARN_ON_ONCE(!spin_is_locked(&devinfo->lock)); |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 181 | cmdinfo->state |= IS_IN_WORK_LIST; |
| 182 | schedule_work(&devinfo->work); |
| 183 | } |
| 184 | |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 185 | static void uas_zap_dead(struct uas_dev_info *devinfo) |
| 186 | { |
| 187 | struct uas_cmd_info *cmdinfo; |
| 188 | struct uas_cmd_info *temp; |
| 189 | unsigned long flags; |
| 190 | |
| 191 | spin_lock_irqsave(&devinfo->lock, flags); |
Hans de Goede | 040d1a8 | 2013-11-12 14:02:12 +0100 | [diff] [blame^] | 192 | list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, list) { |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 193 | struct scsi_pointer *scp = (void *)cmdinfo; |
| 194 | struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, |
| 195 | SCp); |
| 196 | uas_log_cmd_state(cmnd, __func__); |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 197 | WARN_ON_ONCE(!(cmdinfo->state & COMMAND_ABORTED)); |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 198 | /* all urbs are killed, clear inflight bits */ |
| 199 | cmdinfo->state &= ~(COMMAND_INFLIGHT | |
| 200 | DATA_IN_URB_INFLIGHT | |
| 201 | DATA_OUT_URB_INFLIGHT); |
| 202 | uas_try_complete(cmnd, __func__); |
| 203 | } |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 204 | devinfo->running_task = 0; |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 205 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 206 | } |
| 207 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 208 | static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd) |
| 209 | { |
| 210 | struct sense_iu *sense_iu = urb->transfer_buffer; |
| 211 | struct scsi_device *sdev = cmnd->device; |
| 212 | |
| 213 | if (urb->actual_length > 16) { |
| 214 | unsigned len = be16_to_cpup(&sense_iu->len); |
| 215 | if (len + 16 != urb->actual_length) { |
| 216 | int newlen = min(len + 16, urb->actual_length) - 16; |
| 217 | if (newlen < 0) |
| 218 | newlen = 0; |
| 219 | sdev_printk(KERN_INFO, sdev, "%s: urb length %d " |
| 220 | "disagrees with IU sense data length %d, " |
| 221 | "using %d bytes of sense data\n", __func__, |
| 222 | urb->actual_length, len, newlen); |
| 223 | len = newlen; |
| 224 | } |
| 225 | memcpy(cmnd->sense_buffer, sense_iu->sense, len); |
| 226 | } |
| 227 | |
| 228 | cmnd->result = sense_iu->status; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd) |
| 232 | { |
| 233 | struct sense_iu_old *sense_iu = urb->transfer_buffer; |
| 234 | struct scsi_device *sdev = cmnd->device; |
| 235 | |
| 236 | if (urb->actual_length > 8) { |
| 237 | unsigned len = be16_to_cpup(&sense_iu->len) - 2; |
| 238 | if (len + 8 != urb->actual_length) { |
| 239 | int newlen = min(len + 8, urb->actual_length) - 8; |
| 240 | if (newlen < 0) |
| 241 | newlen = 0; |
| 242 | sdev_printk(KERN_INFO, sdev, "%s: urb length %d " |
| 243 | "disagrees with IU sense data length %d, " |
| 244 | "using %d bytes of sense data\n", __func__, |
| 245 | urb->actual_length, len, newlen); |
| 246 | len = newlen; |
| 247 | } |
| 248 | memcpy(cmnd->sense_buffer, sense_iu->sense, len); |
| 249 | } |
| 250 | |
| 251 | cmnd->result = sense_iu->status; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller) |
| 255 | { |
| 256 | struct uas_cmd_info *ci = (void *)&cmnd->SCp; |
| 257 | |
| 258 | scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:" |
Gerd Hoffmann | efefecf | 2012-11-30 11:54:42 +0100 | [diff] [blame] | 259 | "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 260 | caller, cmnd, cmnd->request->tag, |
| 261 | (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "", |
| 262 | (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "", |
| 263 | (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "", |
| 264 | (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "", |
| 265 | (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "", |
| 266 | (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "", |
| 267 | (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "", |
| 268 | (ci->state & COMMAND_INFLIGHT) ? " CMD" : "", |
| 269 | (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "", |
| 270 | (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "", |
Gerd Hoffmann | ef018cc9 | 2012-09-25 10:47:06 +0200 | [diff] [blame] | 271 | (ci->state & COMMAND_COMPLETED) ? " done" : "", |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 272 | (ci->state & COMMAND_ABORTED) ? " abort" : "", |
Gerd Hoffmann | efefecf | 2012-11-30 11:54:42 +0100 | [diff] [blame] | 273 | (ci->state & UNLINK_DATA_URBS) ? " unlink": "", |
| 274 | (ci->state & IS_IN_WORK_LIST) ? " work" : ""); |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller) |
| 278 | { |
| 279 | struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 280 | struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 281 | |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 282 | WARN_ON_ONCE(!spin_is_locked(&devinfo->lock)); |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 283 | if (cmdinfo->state & (COMMAND_INFLIGHT | |
| 284 | DATA_IN_URB_INFLIGHT | |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 285 | DATA_OUT_URB_INFLIGHT | |
| 286 | UNLINK_DATA_URBS)) |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 287 | return -EBUSY; |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 288 | WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED); |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 289 | cmdinfo->state |= COMMAND_COMPLETED; |
| 290 | usb_free_urb(cmdinfo->data_in_urb); |
| 291 | usb_free_urb(cmdinfo->data_out_urb); |
Gerd Hoffmann | 0871d7d | 2012-09-25 10:47:07 +0200 | [diff] [blame] | 292 | if (cmdinfo->state & COMMAND_ABORTED) { |
| 293 | scmd_printk(KERN_INFO, cmnd, "abort completed\n"); |
| 294 | cmnd->result = DID_ABORT << 16; |
Hans de Goede | 040d1a8 | 2013-11-12 14:02:12 +0100 | [diff] [blame^] | 295 | } |
| 296 | list_del(&cmdinfo->list); |
Gerd Hoffmann | c621a81 | 2012-06-19 09:54:48 +0200 | [diff] [blame] | 297 | cmnd->scsi_done(cmnd); |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 298 | return 0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd, |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 302 | unsigned direction) |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 303 | { |
| 304 | struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; |
| 305 | int err; |
| 306 | |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 307 | cmdinfo->state |= direction | SUBMIT_STATUS_URB; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 308 | err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC); |
| 309 | if (err) { |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 310 | uas_add_work(cmdinfo); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | static void uas_stat_cmplt(struct urb *urb) |
| 315 | { |
| 316 | struct iu *iu = urb->transfer_buffer; |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 317 | struct Scsi_Host *shost = urb->context; |
| 318 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 319 | struct scsi_cmnd *cmnd; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 320 | struct uas_cmd_info *cmdinfo; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 321 | unsigned long flags; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 322 | u16 tag; |
| 323 | |
| 324 | if (urb->status) { |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 325 | if (urb->status == -ENOENT) { |
| 326 | dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n", |
| 327 | urb->stream_id); |
| 328 | } else { |
| 329 | dev_err(&urb->dev->dev, "stat urb: status %d\n", |
| 330 | urb->status); |
| 331 | } |
Gerd Hoffmann | db32de1 | 2012-06-19 09:54:49 +0200 | [diff] [blame] | 332 | usb_free_urb(urb); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 333 | return; |
| 334 | } |
| 335 | |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 336 | if (devinfo->resetting) { |
| 337 | usb_free_urb(urb); |
| 338 | return; |
| 339 | } |
| 340 | |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 341 | spin_lock_irqsave(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 342 | tag = be16_to_cpup(&iu->tag) - 1; |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 343 | if (tag == 0) |
| 344 | cmnd = devinfo->cmnd; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 345 | else |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 346 | cmnd = scsi_host_find_tag(shost, tag - 1); |
Gerd Hoffmann | e0423de | 2012-09-26 10:29:03 +0200 | [diff] [blame] | 347 | |
Sarah Sharp | 96c1eb9 | 2011-12-02 11:55:48 -0800 | [diff] [blame] | 348 | if (!cmnd) { |
Gerd Hoffmann | e0423de | 2012-09-26 10:29:03 +0200 | [diff] [blame] | 349 | if (iu->iu_id == IU_ID_RESPONSE) { |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 350 | if (!devinfo->running_task) |
| 351 | dev_warn(&urb->dev->dev, |
| 352 | "stat urb: recv unexpected response iu\n"); |
Gerd Hoffmann | e0423de | 2012-09-26 10:29:03 +0200 | [diff] [blame] | 353 | /* store results for uas_eh_task_mgmt() */ |
| 354 | memcpy(&devinfo->response, iu, sizeof(devinfo->response)); |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 355 | } |
Gerd Hoffmann | e0423de | 2012-09-26 10:29:03 +0200 | [diff] [blame] | 356 | usb_free_urb(urb); |
| 357 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 358 | return; |
Sarah Sharp | 96c1eb9 | 2011-12-02 11:55:48 -0800 | [diff] [blame] | 359 | } |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 360 | |
Gerd Hoffmann | e0423de | 2012-09-26 10:29:03 +0200 | [diff] [blame] | 361 | cmdinfo = (void *)&cmnd->SCp; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 362 | switch (iu->iu_id) { |
| 363 | case IU_ID_STATUS: |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 364 | if (devinfo->cmnd == cmnd) |
| 365 | devinfo->cmnd = NULL; |
| 366 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 367 | if (urb->actual_length < 16) |
| 368 | devinfo->uas_sense_old = 1; |
| 369 | if (devinfo->uas_sense_old) |
| 370 | uas_sense_old(urb, cmnd); |
| 371 | else |
| 372 | uas_sense(urb, cmnd); |
Gerd Hoffmann | 8aac863 | 2012-06-19 09:54:52 +0200 | [diff] [blame] | 373 | if (cmnd->result != 0) { |
| 374 | /* cancel data transfers on error */ |
Hans de Goede | 7e50e0b | 2013-10-17 19:19:04 +0200 | [diff] [blame] | 375 | uas_unlink_data_urbs(devinfo, cmdinfo, &flags); |
Gerd Hoffmann | 8aac863 | 2012-06-19 09:54:52 +0200 | [diff] [blame] | 376 | } |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 377 | cmdinfo->state &= ~COMMAND_INFLIGHT; |
| 378 | uas_try_complete(cmnd, __func__); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 379 | break; |
| 380 | case IU_ID_READ_READY: |
| 381 | uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB); |
| 382 | break; |
| 383 | case IU_ID_WRITE_READY: |
| 384 | uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB); |
| 385 | break; |
| 386 | default: |
| 387 | scmd_printk(KERN_ERR, cmnd, |
| 388 | "Bogus IU (%d) received on status pipe\n", iu->iu_id); |
| 389 | } |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 390 | usb_free_urb(urb); |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 391 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Gerd Hoffmann | c621a81 | 2012-06-19 09:54:48 +0200 | [diff] [blame] | 394 | static void uas_data_cmplt(struct urb *urb) |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 395 | { |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 396 | struct scsi_cmnd *cmnd = urb->context; |
| 397 | struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 398 | struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 399 | struct scsi_data_buffer *sdb = NULL; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 400 | unsigned long flags; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 401 | |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 402 | spin_lock_irqsave(&devinfo->lock, flags); |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 403 | if (cmdinfo->data_in_urb == urb) { |
| 404 | sdb = scsi_in(cmnd); |
| 405 | cmdinfo->state &= ~DATA_IN_URB_INFLIGHT; |
| 406 | } else if (cmdinfo->data_out_urb == urb) { |
| 407 | sdb = scsi_out(cmnd); |
| 408 | cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT; |
| 409 | } |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 410 | if (sdb == NULL) { |
| 411 | WARN_ON_ONCE(1); |
| 412 | } else if (urb->status) { |
Gerd Hoffmann | 8aac863 | 2012-06-19 09:54:52 +0200 | [diff] [blame] | 413 | /* error: no data transfered */ |
| 414 | sdb->resid = sdb->length; |
| 415 | } else { |
| 416 | sdb->resid = sdb->length - urb->actual_length; |
| 417 | } |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 418 | uas_try_complete(cmnd, __func__); |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 419 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp, |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 423 | unsigned int pipe, u16 stream_id, |
| 424 | struct scsi_cmnd *cmnd, |
| 425 | enum dma_data_direction dir) |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 426 | { |
| 427 | struct usb_device *udev = devinfo->udev; |
| 428 | struct urb *urb = usb_alloc_urb(0, gfp); |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 429 | struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE) |
| 430 | ? scsi_in(cmnd) : scsi_out(cmnd); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 431 | |
| 432 | if (!urb) |
| 433 | goto out; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 434 | usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length, |
| 435 | uas_data_cmplt, cmnd); |
Hans de Goede | c6d4579 | 2013-11-12 10:53:57 +0100 | [diff] [blame] | 436 | urb->stream_id = stream_id; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 437 | urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0; |
| 438 | urb->sg = sdb->table.sgl; |
| 439 | out: |
| 440 | return urb; |
| 441 | } |
| 442 | |
| 443 | static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp, |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 444 | struct Scsi_Host *shost, u16 stream_id) |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 445 | { |
| 446 | struct usb_device *udev = devinfo->udev; |
| 447 | struct urb *urb = usb_alloc_urb(0, gfp); |
| 448 | struct sense_iu *iu; |
| 449 | |
| 450 | if (!urb) |
| 451 | goto out; |
| 452 | |
Matthew Wilcox | ac563cf | 2010-12-15 15:44:03 -0500 | [diff] [blame] | 453 | iu = kzalloc(sizeof(*iu), gfp); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 454 | if (!iu) |
| 455 | goto free; |
| 456 | |
| 457 | usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu), |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 458 | uas_stat_cmplt, shost); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 459 | urb->stream_id = stream_id; |
| 460 | urb->transfer_flags |= URB_FREE_BUFFER; |
| 461 | out: |
| 462 | return urb; |
| 463 | free: |
| 464 | usb_free_urb(urb); |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp, |
Hans de Goede | a887cd3 | 2013-10-17 19:47:28 +0200 | [diff] [blame] | 469 | struct scsi_cmnd *cmnd) |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 470 | { |
| 471 | struct usb_device *udev = devinfo->udev; |
| 472 | struct scsi_device *sdev = cmnd->device; |
| 473 | struct urb *urb = usb_alloc_urb(0, gfp); |
| 474 | struct command_iu *iu; |
| 475 | int len; |
| 476 | |
| 477 | if (!urb) |
| 478 | goto out; |
| 479 | |
| 480 | len = cmnd->cmd_len - 16; |
| 481 | if (len < 0) |
| 482 | len = 0; |
| 483 | len = ALIGN(len, 4); |
Matthew Wilcox | ac563cf | 2010-12-15 15:44:03 -0500 | [diff] [blame] | 484 | iu = kzalloc(sizeof(*iu) + len, gfp); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 485 | if (!iu) |
| 486 | goto free; |
| 487 | |
| 488 | iu->iu_id = IU_ID_COMMAND; |
Sarah Sharp | 9eb4454 | 2011-12-02 11:55:46 -0800 | [diff] [blame] | 489 | if (blk_rq_tagged(cmnd->request)) |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 490 | iu->tag = cpu_to_be16(cmnd->request->tag + 2); |
Sarah Sharp | 9eb4454 | 2011-12-02 11:55:46 -0800 | [diff] [blame] | 491 | else |
| 492 | iu->tag = cpu_to_be16(1); |
Christoph Hellwig | 02e031c | 2010-11-10 14:54:09 +0100 | [diff] [blame] | 493 | iu->prio_attr = UAS_SIMPLE_TAG; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 494 | iu->len = len; |
| 495 | int_to_scsilun(sdev->lun, &iu->lun); |
| 496 | memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len); |
| 497 | |
| 498 | usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len, |
| 499 | usb_free_urb, NULL); |
| 500 | urb->transfer_flags |= URB_FREE_BUFFER; |
| 501 | out: |
| 502 | return urb; |
| 503 | free: |
| 504 | usb_free_urb(urb); |
| 505 | return NULL; |
| 506 | } |
| 507 | |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 508 | static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp, |
| 509 | u8 function, u16 stream_id) |
| 510 | { |
| 511 | struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; |
| 512 | struct usb_device *udev = devinfo->udev; |
| 513 | struct urb *urb = usb_alloc_urb(0, gfp); |
| 514 | struct task_mgmt_iu *iu; |
| 515 | int err = -ENOMEM; |
| 516 | |
| 517 | if (!urb) |
| 518 | goto err; |
| 519 | |
| 520 | iu = kzalloc(sizeof(*iu), gfp); |
| 521 | if (!iu) |
| 522 | goto err; |
| 523 | |
| 524 | iu->iu_id = IU_ID_TASK_MGMT; |
| 525 | iu->tag = cpu_to_be16(stream_id); |
| 526 | int_to_scsilun(cmnd->device->lun, &iu->lun); |
| 527 | |
| 528 | iu->function = function; |
| 529 | switch (function) { |
| 530 | case TMF_ABORT_TASK: |
| 531 | if (blk_rq_tagged(cmnd->request)) |
| 532 | iu->task_tag = cpu_to_be16(cmnd->request->tag + 2); |
| 533 | else |
| 534 | iu->task_tag = cpu_to_be16(1); |
| 535 | break; |
| 536 | } |
| 537 | |
| 538 | usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu), |
| 539 | usb_free_urb, NULL); |
| 540 | urb->transfer_flags |= URB_FREE_BUFFER; |
| 541 | |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 542 | usb_anchor_urb(urb, &devinfo->cmd_urbs); |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 543 | err = usb_submit_urb(urb, gfp); |
| 544 | if (err) { |
| 545 | usb_unanchor_urb(urb); |
| 546 | goto err; |
| 547 | } |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 548 | |
| 549 | return 0; |
| 550 | |
| 551 | err: |
| 552 | usb_free_urb(urb); |
| 553 | return err; |
| 554 | } |
| 555 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 556 | /* |
| 557 | * Why should I request the Status IU before sending the Command IU? Spec |
| 558 | * says to, but also says the device may receive them in any order. Seems |
| 559 | * daft to me. |
| 560 | */ |
| 561 | |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 562 | static struct urb *uas_submit_sense_urb(struct Scsi_Host *shost, |
| 563 | gfp_t gfp, unsigned int stream) |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 564 | { |
| 565 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 566 | struct urb *urb; |
| 567 | |
| 568 | urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream); |
| 569 | if (!urb) |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 570 | return NULL; |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 571 | usb_anchor_urb(urb, &devinfo->sense_urbs); |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 572 | if (usb_submit_urb(urb, gfp)) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 573 | usb_unanchor_urb(urb); |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 574 | shost_printk(KERN_INFO, shost, |
| 575 | "sense urb submission failure\n"); |
| 576 | usb_free_urb(urb); |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 577 | return NULL; |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 578 | } |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 579 | return urb; |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 580 | } |
| 581 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 582 | static int uas_submit_urbs(struct scsi_cmnd *cmnd, |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 583 | struct uas_dev_info *devinfo, gfp_t gfp) |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 584 | { |
| 585 | struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 586 | struct urb *urb; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 587 | |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 588 | WARN_ON_ONCE(!spin_is_locked(&devinfo->lock)); |
Matthew Wilcox | 92a3f76 | 2010-12-15 15:44:04 -0500 | [diff] [blame] | 589 | if (cmdinfo->state & SUBMIT_STATUS_URB) { |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 590 | urb = uas_submit_sense_urb(cmnd->device->host, gfp, |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 591 | cmdinfo->stream); |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 592 | if (!urb) |
| 593 | return SCSI_MLQUEUE_DEVICE_BUSY; |
Matthew Wilcox | 92a3f76 | 2010-12-15 15:44:04 -0500 | [diff] [blame] | 594 | cmdinfo->state &= ~SUBMIT_STATUS_URB; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | if (cmdinfo->state & ALLOC_DATA_IN_URB) { |
| 598 | cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp, |
Gerd Hoffmann | c621a81 | 2012-06-19 09:54:48 +0200 | [diff] [blame] | 599 | devinfo->data_in_pipe, cmdinfo->stream, |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 600 | cmnd, DMA_FROM_DEVICE); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 601 | if (!cmdinfo->data_in_urb) |
| 602 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 603 | cmdinfo->state &= ~ALLOC_DATA_IN_URB; |
| 604 | } |
| 605 | |
| 606 | if (cmdinfo->state & SUBMIT_DATA_IN_URB) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 607 | usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 608 | if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 609 | usb_unanchor_urb(cmdinfo->data_in_urb); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 610 | scmd_printk(KERN_INFO, cmnd, |
| 611 | "data in urb submission failure\n"); |
| 612 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 613 | } |
| 614 | cmdinfo->state &= ~SUBMIT_DATA_IN_URB; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 615 | cmdinfo->state |= DATA_IN_URB_INFLIGHT; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | if (cmdinfo->state & ALLOC_DATA_OUT_URB) { |
| 619 | cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp, |
Gerd Hoffmann | c621a81 | 2012-06-19 09:54:48 +0200 | [diff] [blame] | 620 | devinfo->data_out_pipe, cmdinfo->stream, |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 621 | cmnd, DMA_TO_DEVICE); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 622 | if (!cmdinfo->data_out_urb) |
| 623 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 624 | cmdinfo->state &= ~ALLOC_DATA_OUT_URB; |
| 625 | } |
| 626 | |
| 627 | if (cmdinfo->state & SUBMIT_DATA_OUT_URB) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 628 | usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 629 | if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 630 | usb_unanchor_urb(cmdinfo->data_out_urb); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 631 | scmd_printk(KERN_INFO, cmnd, |
| 632 | "data out urb submission failure\n"); |
| 633 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 634 | } |
| 635 | cmdinfo->state &= ~SUBMIT_DATA_OUT_URB; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 636 | cmdinfo->state |= DATA_OUT_URB_INFLIGHT; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | if (cmdinfo->state & ALLOC_CMD_URB) { |
Hans de Goede | a887cd3 | 2013-10-17 19:47:28 +0200 | [diff] [blame] | 640 | cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 641 | if (!cmdinfo->cmd_urb) |
| 642 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 643 | cmdinfo->state &= ~ALLOC_CMD_URB; |
| 644 | } |
| 645 | |
| 646 | if (cmdinfo->state & SUBMIT_CMD_URB) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 647 | usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 648 | if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 649 | usb_unanchor_urb(cmdinfo->cmd_urb); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 650 | scmd_printk(KERN_INFO, cmnd, |
| 651 | "cmd urb submission failure\n"); |
| 652 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 653 | } |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 654 | cmdinfo->cmd_urb = NULL; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 655 | cmdinfo->state &= ~SUBMIT_CMD_URB; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 656 | cmdinfo->state |= COMMAND_INFLIGHT; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | |
Jeff Garzik | f281233 | 2010-11-16 02:10:29 -0500 | [diff] [blame] | 662 | static int uas_queuecommand_lck(struct scsi_cmnd *cmnd, |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 663 | void (*done)(struct scsi_cmnd *)) |
| 664 | { |
| 665 | struct scsi_device *sdev = cmnd->device; |
| 666 | struct uas_dev_info *devinfo = sdev->hostdata; |
| 667 | struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 668 | unsigned long flags; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 669 | int err; |
| 670 | |
| 671 | BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer)); |
| 672 | |
Gerd Hoffmann | f8be6bf | 2012-11-30 11:54:45 +0100 | [diff] [blame] | 673 | if (devinfo->resetting) { |
| 674 | cmnd->result = DID_ERROR << 16; |
| 675 | cmnd->scsi_done(cmnd); |
| 676 | return 0; |
| 677 | } |
| 678 | |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 679 | spin_lock_irqsave(&devinfo->lock, flags); |
| 680 | if (devinfo->cmnd) { |
| 681 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 682 | return SCSI_MLQUEUE_DEVICE_BUSY; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 683 | } |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 684 | |
| 685 | if (blk_rq_tagged(cmnd->request)) { |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 686 | cmdinfo->stream = cmnd->request->tag + 2; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 687 | } else { |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 688 | devinfo->cmnd = cmnd; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 689 | cmdinfo->stream = 1; |
| 690 | } |
| 691 | |
| 692 | cmnd->scsi_done = done; |
| 693 | |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 694 | cmdinfo->state = SUBMIT_STATUS_URB | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 695 | ALLOC_CMD_URB | SUBMIT_CMD_URB; |
| 696 | |
| 697 | switch (cmnd->sc_data_direction) { |
| 698 | case DMA_FROM_DEVICE: |
| 699 | cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB; |
| 700 | break; |
| 701 | case DMA_BIDIRECTIONAL: |
| 702 | cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB; |
| 703 | case DMA_TO_DEVICE: |
| 704 | cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB; |
| 705 | case DMA_NONE: |
| 706 | break; |
| 707 | } |
| 708 | |
| 709 | if (!devinfo->use_streams) { |
Gerd Hoffmann | db32de1 | 2012-06-19 09:54:49 +0200 | [diff] [blame] | 710 | cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 711 | cmdinfo->stream = 0; |
| 712 | } |
| 713 | |
| 714 | err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC); |
| 715 | if (err) { |
| 716 | /* If we did nothing, give up now */ |
Matthew Wilcox | 92a3f76 | 2010-12-15 15:44:04 -0500 | [diff] [blame] | 717 | if (cmdinfo->state & SUBMIT_STATUS_URB) { |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 718 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 719 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 720 | } |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 721 | uas_add_work(cmdinfo); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 722 | } |
| 723 | |
Hans de Goede | 040d1a8 | 2013-11-12 14:02:12 +0100 | [diff] [blame^] | 724 | list_add_tail(&cmdinfo->list, &devinfo->inflight_list); |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 725 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 726 | return 0; |
| 727 | } |
| 728 | |
Jeff Garzik | f281233 | 2010-11-16 02:10:29 -0500 | [diff] [blame] | 729 | static DEF_SCSI_QCMD(uas_queuecommand) |
| 730 | |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 731 | static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd, |
| 732 | const char *fname, u8 function) |
| 733 | { |
| 734 | struct Scsi_Host *shost = cmnd->device->host; |
| 735 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
Hans de Goede | d3f7c15 | 2013-10-23 17:46:17 +0100 | [diff] [blame] | 736 | u16 tag = devinfo->qdepth; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 737 | unsigned long flags; |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 738 | struct urb *sense_urb; |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 739 | int result = SUCCESS; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 740 | |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 741 | spin_lock_irqsave(&devinfo->lock, flags); |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 742 | |
| 743 | if (devinfo->running_task) { |
| 744 | shost_printk(KERN_INFO, shost, |
| 745 | "%s: %s: error already running a task\n", |
| 746 | __func__, fname); |
| 747 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 748 | return FAILED; |
| 749 | } |
| 750 | |
| 751 | devinfo->running_task = 1; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 752 | memset(&devinfo->response, 0, sizeof(devinfo->response)); |
Hans de Goede | f323abc | 2013-11-12 10:51:33 +0100 | [diff] [blame] | 753 | sense_urb = uas_submit_sense_urb(shost, GFP_NOIO, |
| 754 | devinfo->use_streams ? tag : 0); |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 755 | if (!sense_urb) { |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 756 | shost_printk(KERN_INFO, shost, |
| 757 | "%s: %s: submit sense urb failed\n", |
| 758 | __func__, fname); |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 759 | devinfo->running_task = 0; |
Gerd Hoffmann | 1994ff4 | 2012-09-26 10:28:58 +0200 | [diff] [blame] | 760 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 761 | return FAILED; |
| 762 | } |
Hans de Goede | e36e649 | 2013-11-07 08:35:55 +0100 | [diff] [blame] | 763 | if (uas_submit_task_urb(cmnd, GFP_NOIO, function, tag)) { |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 764 | shost_printk(KERN_INFO, shost, |
| 765 | "%s: %s: submit task mgmt urb failed\n", |
| 766 | __func__, fname); |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 767 | devinfo->running_task = 0; |
Gerd Hoffmann | 1994ff4 | 2012-09-26 10:28:58 +0200 | [diff] [blame] | 768 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 769 | usb_kill_urb(sense_urb); |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 770 | return FAILED; |
| 771 | } |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 772 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 773 | |
| 774 | if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) { |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 775 | /* |
| 776 | * Note we deliberately do not clear running_task here. If we |
| 777 | * allow new tasks to be submitted, there is no way to figure |
| 778 | * out if a received response_iu is for the failed task or for |
| 779 | * the new one. A bus-reset will eventually clear running_task. |
| 780 | */ |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 781 | shost_printk(KERN_INFO, shost, |
| 782 | "%s: %s timed out\n", __func__, fname); |
| 783 | return FAILED; |
| 784 | } |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 785 | |
| 786 | spin_lock_irqsave(&devinfo->lock, flags); |
| 787 | devinfo->running_task = 0; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 788 | if (be16_to_cpu(devinfo->response.tag) != tag) { |
| 789 | shost_printk(KERN_INFO, shost, |
| 790 | "%s: %s failed (wrong tag %d/%d)\n", __func__, |
| 791 | fname, be16_to_cpu(devinfo->response.tag), tag); |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 792 | result = FAILED; |
| 793 | } else if (devinfo->response.response_code != RC_TMF_COMPLETE) { |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 794 | shost_printk(KERN_INFO, shost, |
| 795 | "%s: %s failed (rc 0x%x)\n", __func__, |
| 796 | fname, devinfo->response.response_code); |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 797 | result = FAILED; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 798 | } |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 799 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 800 | |
| 801 | return result; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 802 | } |
| 803 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 804 | static int uas_eh_abort_handler(struct scsi_cmnd *cmnd) |
| 805 | { |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 806 | struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 807 | struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; |
| 808 | unsigned long flags; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 809 | int ret; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 810 | |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 811 | spin_lock_irqsave(&devinfo->lock, flags); |
Hans de Goede | da3033e | 2013-11-12 13:57:24 +0100 | [diff] [blame] | 812 | uas_mark_cmd_dead(devinfo, cmdinfo, __func__); |
Gerd Hoffmann | 5d39040 | 2012-11-30 11:54:43 +0100 | [diff] [blame] | 813 | if (cmdinfo->state & COMMAND_INFLIGHT) { |
| 814 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 815 | ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK); |
| 816 | } else { |
Hans de Goede | 7e50e0b | 2013-10-17 19:19:04 +0200 | [diff] [blame] | 817 | uas_unlink_data_urbs(devinfo, cmdinfo, &flags); |
Gerd Hoffmann | 5d39040 | 2012-11-30 11:54:43 +0100 | [diff] [blame] | 818 | uas_try_complete(cmnd, __func__); |
| 819 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 820 | ret = SUCCESS; |
| 821 | } |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 822 | return ret; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd) |
| 826 | { |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 827 | sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__); |
| 828 | return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET", |
| 829 | TMF_LOGICAL_UNIT_RESET); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd) |
| 833 | { |
| 834 | struct scsi_device *sdev = cmnd->device; |
| 835 | struct uas_dev_info *devinfo = sdev->hostdata; |
| 836 | struct usb_device *udev = devinfo->udev; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 837 | int err; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 838 | |
Hans de Goede | be326f4 | 2013-09-22 16:27:02 +0200 | [diff] [blame] | 839 | err = usb_lock_device_for_reset(udev, devinfo->intf); |
| 840 | if (err) { |
| 841 | shost_printk(KERN_ERR, sdev->host, |
| 842 | "%s FAILED to get lock err %d\n", __func__, err); |
| 843 | return FAILED; |
| 844 | } |
| 845 | |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 846 | shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__); |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 847 | devinfo->resetting = 1; |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame] | 848 | uas_abort_inflight(devinfo); |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 849 | usb_kill_anchored_urbs(&devinfo->cmd_urbs); |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 850 | usb_kill_anchored_urbs(&devinfo->sense_urbs); |
| 851 | usb_kill_anchored_urbs(&devinfo->data_urbs); |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 852 | uas_zap_dead(devinfo); |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 853 | err = usb_reset_device(udev); |
| 854 | devinfo->resetting = 0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 855 | |
Hans de Goede | be326f4 | 2013-09-22 16:27:02 +0200 | [diff] [blame] | 856 | usb_unlock_device(udev); |
| 857 | |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 858 | if (err) { |
| 859 | shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__); |
| 860 | return FAILED; |
| 861 | } |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 862 | |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 863 | shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__); |
| 864 | return SUCCESS; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | static int uas_slave_alloc(struct scsi_device *sdev) |
| 868 | { |
| 869 | sdev->hostdata = (void *)sdev->host->hostdata[0]; |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | static int uas_slave_configure(struct scsi_device *sdev) |
| 874 | { |
| 875 | struct uas_dev_info *devinfo = sdev->hostdata; |
| 876 | scsi_set_tag_type(sdev, MSG_ORDERED_TAG); |
Hans de Goede | d3f7c15 | 2013-10-23 17:46:17 +0100 | [diff] [blame] | 877 | scsi_activate_tcq(sdev, devinfo->qdepth - 2); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | static struct scsi_host_template uas_host_template = { |
| 882 | .module = THIS_MODULE, |
| 883 | .name = "uas", |
| 884 | .queuecommand = uas_queuecommand, |
| 885 | .slave_alloc = uas_slave_alloc, |
| 886 | .slave_configure = uas_slave_configure, |
| 887 | .eh_abort_handler = uas_eh_abort_handler, |
| 888 | .eh_device_reset_handler = uas_eh_device_reset_handler, |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 889 | .eh_bus_reset_handler = uas_eh_bus_reset_handler, |
| 890 | .can_queue = 65536, /* Is there a limit on the _host_ ? */ |
| 891 | .this_id = -1, |
| 892 | .sg_tablesize = SG_NONE, |
| 893 | .cmd_per_lun = 1, /* until we override it */ |
| 894 | .skip_settle_delay = 1, |
| 895 | .ordered_tag = 1, |
| 896 | }; |
| 897 | |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 898 | #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ |
| 899 | vendorName, productName, useProtocol, useTransport, \ |
| 900 | initFunction, flags) \ |
| 901 | { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \ |
| 902 | .driver_info = (flags) } |
| 903 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 904 | static struct usb_device_id uas_usb_ids[] = { |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 905 | # include "unusual_uas.h" |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 906 | { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) }, |
| 907 | { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) }, |
| 908 | /* 0xaa is a prototype device I happen to have access to */ |
| 909 | { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) }, |
| 910 | { } |
| 911 | }; |
| 912 | MODULE_DEVICE_TABLE(usb, uas_usb_ids); |
| 913 | |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 914 | #undef UNUSUAL_DEV |
| 915 | |
Hans de Goede | e1be067 | 2013-10-21 08:00:58 +0100 | [diff] [blame] | 916 | static int uas_switch_interface(struct usb_device *udev, |
| 917 | struct usb_interface *intf) |
| 918 | { |
| 919 | int alt; |
| 920 | |
| 921 | alt = uas_find_uas_alt_setting(intf); |
| 922 | if (alt < 0) |
| 923 | return alt; |
| 924 | |
| 925 | return usb_set_interface(udev, |
| 926 | intf->altsetting[0].desc.bInterfaceNumber, alt); |
| 927 | } |
| 928 | |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 929 | static int uas_configure_endpoints(struct uas_dev_info *devinfo) |
Hans de Goede | 34f11e5 | 2013-10-29 08:54:48 +0100 | [diff] [blame] | 930 | { |
| 931 | struct usb_host_endpoint *eps[4] = { }; |
| 932 | struct usb_device *udev = devinfo->udev; |
| 933 | int r; |
| 934 | |
| 935 | devinfo->uas_sense_old = 0; |
| 936 | devinfo->cmnd = NULL; |
| 937 | |
| 938 | r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps); |
Hans de Goede | 74d71ae | 2013-10-29 10:10:36 +0100 | [diff] [blame] | 939 | if (r) |
| 940 | return r; |
Hans de Goede | 34f11e5 | 2013-10-29 08:54:48 +0100 | [diff] [blame] | 941 | |
Hans de Goede | 74d71ae | 2013-10-29 10:10:36 +0100 | [diff] [blame] | 942 | devinfo->cmd_pipe = usb_sndbulkpipe(udev, |
| 943 | usb_endpoint_num(&eps[0]->desc)); |
| 944 | devinfo->status_pipe = usb_rcvbulkpipe(udev, |
| 945 | usb_endpoint_num(&eps[1]->desc)); |
| 946 | devinfo->data_in_pipe = usb_rcvbulkpipe(udev, |
| 947 | usb_endpoint_num(&eps[2]->desc)); |
| 948 | devinfo->data_out_pipe = usb_sndbulkpipe(udev, |
| 949 | usb_endpoint_num(&eps[3]->desc)); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 950 | |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 951 | if (udev->speed != USB_SPEED_SUPER) { |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 952 | devinfo->qdepth = 256; |
| 953 | devinfo->use_streams = 0; |
| 954 | } else { |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 955 | devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, |
| 956 | 3, 256, GFP_KERNEL); |
| 957 | if (devinfo->qdepth < 0) |
| 958 | return devinfo->qdepth; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 959 | devinfo->use_streams = 1; |
| 960 | } |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 961 | |
| 962 | return 0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 963 | } |
| 964 | |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 965 | static void uas_free_streams(struct uas_dev_info *devinfo) |
| 966 | { |
| 967 | struct usb_device *udev = devinfo->udev; |
| 968 | struct usb_host_endpoint *eps[3]; |
| 969 | |
| 970 | eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe); |
| 971 | eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe); |
| 972 | eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe); |
| 973 | usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL); |
| 974 | } |
| 975 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 976 | /* |
| 977 | * XXX: What I'd like to do here is register a SCSI host for each USB host in |
| 978 | * the system. Follow usb-storage's design of registering a SCSI host for |
| 979 | * each USB device for the moment. Can implement this by walking up the |
| 980 | * USB hierarchy until we find a USB host. |
| 981 | */ |
| 982 | static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 983 | { |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 984 | int result = -ENOMEM; |
| 985 | struct Scsi_Host *shost = NULL; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 986 | struct uas_dev_info *devinfo; |
| 987 | struct usb_device *udev = interface_to_usbdev(intf); |
| 988 | |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 989 | if (!uas_use_uas_driver(intf, id)) |
| 990 | return -ENODEV; |
| 991 | |
Matthew Wilcox | 89dc290 | 2010-12-15 15:44:05 -0500 | [diff] [blame] | 992 | if (uas_switch_interface(udev, intf)) |
| 993 | return -ENODEV; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 994 | |
| 995 | devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL); |
| 996 | if (!devinfo) |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 997 | goto set_alt0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 998 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 999 | shost = scsi_host_alloc(&uas_host_template, sizeof(void *)); |
| 1000 | if (!shost) |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 1001 | goto set_alt0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1002 | |
| 1003 | shost->max_cmd_len = 16 + 252; |
| 1004 | shost->max_id = 1; |
Gerd Hoffmann | bde027b | 2013-01-25 15:03:36 +0100 | [diff] [blame] | 1005 | shost->max_lun = 256; |
| 1006 | shost->max_channel = 0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1007 | shost->sg_tablesize = udev->bus->sg_tablesize; |
| 1008 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1009 | devinfo->intf = intf; |
| 1010 | devinfo->udev = udev; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 1011 | devinfo->resetting = 0; |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 1012 | devinfo->running_task = 0; |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1013 | devinfo->shutdown = 0; |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 1014 | init_usb_anchor(&devinfo->cmd_urbs); |
Gerd Hoffmann | bdd000f | 2012-06-19 09:54:53 +0200 | [diff] [blame] | 1015 | init_usb_anchor(&devinfo->sense_urbs); |
| 1016 | init_usb_anchor(&devinfo->data_urbs); |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 1017 | spin_lock_init(&devinfo->lock); |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 1018 | INIT_WORK(&devinfo->work, uas_do_work); |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame] | 1019 | INIT_LIST_HEAD(&devinfo->inflight_list); |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 1020 | INIT_LIST_HEAD(&devinfo->dead_list); |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 1021 | |
| 1022 | result = uas_configure_endpoints(devinfo); |
| 1023 | if (result) |
| 1024 | goto set_alt0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1025 | |
Hans de Goede | d3f7c15 | 2013-10-23 17:46:17 +0100 | [diff] [blame] | 1026 | result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2); |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 1027 | if (result) |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 1028 | goto free_streams; |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 1029 | |
| 1030 | result = scsi_add_host(shost, &intf->dev); |
| 1031 | if (result) |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 1032 | goto free_streams; |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 1033 | |
| 1034 | shost->hostdata[0] = (unsigned long)devinfo; |
| 1035 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1036 | scsi_scan_host(shost); |
| 1037 | usb_set_intfdata(intf, shost); |
| 1038 | return result; |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 1039 | |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 1040 | free_streams: |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 1041 | uas_free_streams(devinfo); |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 1042 | set_alt0: |
| 1043 | usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1044 | kfree(devinfo); |
| 1045 | if (shost) |
| 1046 | scsi_host_put(shost); |
| 1047 | return result; |
| 1048 | } |
| 1049 | |
| 1050 | static int uas_pre_reset(struct usb_interface *intf) |
| 1051 | { |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1052 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
| 1053 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 1054 | unsigned long flags; |
| 1055 | |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1056 | if (devinfo->shutdown) |
| 1057 | return 0; |
| 1058 | |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1059 | /* Block new requests */ |
| 1060 | spin_lock_irqsave(shost->host_lock, flags); |
| 1061 | scsi_block_requests(shost); |
| 1062 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1063 | |
| 1064 | /* Wait for any pending requests to complete */ |
| 1065 | flush_work(&devinfo->work); |
| 1066 | if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) { |
| 1067 | shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__); |
| 1068 | return 1; |
| 1069 | } |
| 1070 | |
| 1071 | uas_free_streams(devinfo); |
| 1072 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | static int uas_post_reset(struct usb_interface *intf) |
| 1077 | { |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1078 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
| 1079 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 1080 | unsigned long flags; |
| 1081 | |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1082 | if (devinfo->shutdown) |
| 1083 | return 0; |
| 1084 | |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 1085 | if (uas_configure_endpoints(devinfo) != 0) { |
| 1086 | shost_printk(KERN_ERR, shost, |
| 1087 | "%s: alloc streams error after reset", __func__); |
| 1088 | return 1; |
| 1089 | } |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1090 | |
| 1091 | spin_lock_irqsave(shost->host_lock, flags); |
| 1092 | scsi_report_bus_reset(shost, 0); |
| 1093 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1094 | |
| 1095 | scsi_unblock_requests(shost); |
| 1096 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
| 1099 | |
Hans de Goede | 0df1f66 | 2013-11-07 08:47:05 +0100 | [diff] [blame] | 1100 | static int uas_suspend(struct usb_interface *intf, pm_message_t message) |
| 1101 | { |
| 1102 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
| 1103 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 1104 | |
| 1105 | /* Wait for any pending requests to complete */ |
| 1106 | flush_work(&devinfo->work); |
| 1107 | if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) { |
| 1108 | shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__); |
| 1109 | return -ETIME; |
| 1110 | } |
| 1111 | |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | static int uas_resume(struct usb_interface *intf) |
| 1116 | { |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | static int uas_reset_resume(struct usb_interface *intf) |
| 1121 | { |
| 1122 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
| 1123 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 1124 | unsigned long flags; |
| 1125 | |
| 1126 | if (uas_configure_endpoints(devinfo) != 0) { |
| 1127 | shost_printk(KERN_ERR, shost, |
| 1128 | "%s: alloc streams error after reset", __func__); |
| 1129 | return -EIO; |
| 1130 | } |
| 1131 | |
| 1132 | spin_lock_irqsave(shost->host_lock, flags); |
| 1133 | scsi_report_bus_reset(shost, 0); |
| 1134 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1135 | |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1139 | static void uas_disconnect(struct usb_interface *intf) |
| 1140 | { |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1141 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
| 1142 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 1143 | |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 1144 | devinfo->resetting = 1; |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 1145 | cancel_work_sync(&devinfo->work); |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame] | 1146 | uas_abort_inflight(devinfo); |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 1147 | usb_kill_anchored_urbs(&devinfo->cmd_urbs); |
Gerd Hoffmann | bdd000f | 2012-06-19 09:54:53 +0200 | [diff] [blame] | 1148 | usb_kill_anchored_urbs(&devinfo->sense_urbs); |
| 1149 | usb_kill_anchored_urbs(&devinfo->data_urbs); |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 1150 | uas_zap_dead(devinfo); |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 1151 | scsi_remove_host(shost); |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 1152 | uas_free_streams(devinfo); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1153 | kfree(devinfo); |
| 1154 | } |
| 1155 | |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1156 | /* |
| 1157 | * Put the device back in usb-storage mode on shutdown, as some BIOS-es |
| 1158 | * hang on reboot when the device is still in uas mode. Note the reset is |
| 1159 | * necessary as some devices won't revert to usb-storage mode without it. |
| 1160 | */ |
| 1161 | static void uas_shutdown(struct device *dev) |
| 1162 | { |
| 1163 | struct usb_interface *intf = to_usb_interface(dev); |
| 1164 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1165 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
| 1166 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 1167 | |
| 1168 | if (system_state != SYSTEM_RESTART) |
| 1169 | return; |
| 1170 | |
| 1171 | devinfo->shutdown = 1; |
| 1172 | uas_free_streams(devinfo); |
| 1173 | usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0); |
| 1174 | usb_reset_device(udev); |
| 1175 | } |
| 1176 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1177 | static struct usb_driver uas_driver = { |
| 1178 | .name = "uas", |
| 1179 | .probe = uas_probe, |
| 1180 | .disconnect = uas_disconnect, |
| 1181 | .pre_reset = uas_pre_reset, |
| 1182 | .post_reset = uas_post_reset, |
Hans de Goede | 0df1f66 | 2013-11-07 08:47:05 +0100 | [diff] [blame] | 1183 | .suspend = uas_suspend, |
| 1184 | .resume = uas_resume, |
| 1185 | .reset_resume = uas_reset_resume, |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1186 | .drvwrap.driver.shutdown = uas_shutdown, |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1187 | .id_table = uas_usb_ids, |
| 1188 | }; |
| 1189 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 1190 | module_usb_driver(uas_driver); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1191 | |
| 1192 | MODULE_LICENSE("GPL"); |
| 1193 | MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp"); |