blob: 5ef014ba6ae86917f34e6ebbd855845e3bf71732 [file] [log] [blame]
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001/*
2 * USB Attached SCSI
3 * Note that this is not the same as the USB Mass Storage driver
4 *
Hans de Goede13630742016-04-12 12:27:09 +02005 * Copyright Hans de Goede <hdegoede@redhat.com> for Red Hat, Inc. 2013 - 2016
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02006 * Copyright Matthew Wilcox for Intel Corp, 2010
7 * Copyright Sarah Sharp for Intel Corp, 2010
8 *
9 * Distributed under the terms of the GNU GPL, version two.
10 */
11
12#include <linux/blkdev.h>
13#include <linux/slab.h>
14#include <linux/types.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040015#include <linux/module.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020016#include <linux/usb.h>
Hans de Goede79b4c062013-10-25 17:04:33 +010017#include <linux/usb_usual.h>
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +010018#include <linux/usb/hcd.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020019#include <linux/usb/storage.h>
Sebastian Andrzej Siewior348748b2012-01-11 12:45:56 +010020#include <linux/usb/uas.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020021
22#include <scsi/scsi.h>
Hans de Goede4de7a3732013-10-22 16:10:44 +010023#include <scsi/scsi_eh.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020024#include <scsi/scsi_dbg.h>
25#include <scsi/scsi_cmnd.h>
26#include <scsi/scsi_device.h>
27#include <scsi/scsi_host.h>
28#include <scsi/scsi_tcq.h>
29
Hans de Goede82aa0382013-10-21 08:53:31 +010030#include "uas-detect.h"
Hans de Goede59307852014-09-15 16:04:12 +020031#include "scsiglue.h"
Hans de Goede82aa0382013-10-21 08:53:31 +010032
Hans de Goede5e61aed2014-09-13 12:26:32 +020033#define MAX_CMNDS 256
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020034
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020035struct uas_dev_info {
36 struct usb_interface *intf;
37 struct usb_device *udev;
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +020038 struct usb_anchor cmd_urbs;
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +020039 struct usb_anchor sense_urbs;
40 struct usb_anchor data_urbs;
Hans de Goede59307852014-09-15 16:04:12 +020041 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +020042 int qdepth, resetting;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020043 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
44 unsigned use_streams:1;
Hans de Goededa65c2b2013-11-11 11:51:42 +010045 unsigned shutdown:1;
Hans de Goede5e61aed2014-09-13 12:26:32 +020046 struct scsi_cmnd *cmnd[MAX_CMNDS];
Gerd Hoffmanne0648522012-09-25 10:47:08 +020047 spinlock_t lock;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +020048 struct work_struct work;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020049};
50
51enum {
Oliver Neukum76433192015-11-05 16:20:36 +010052 SUBMIT_STATUS_URB = BIT(1),
53 ALLOC_DATA_IN_URB = BIT(2),
54 SUBMIT_DATA_IN_URB = BIT(3),
55 ALLOC_DATA_OUT_URB = BIT(4),
56 SUBMIT_DATA_OUT_URB = BIT(5),
57 ALLOC_CMD_URB = BIT(6),
58 SUBMIT_CMD_URB = BIT(7),
59 COMMAND_INFLIGHT = BIT(8),
60 DATA_IN_URB_INFLIGHT = BIT(9),
61 DATA_OUT_URB_INFLIGHT = BIT(10),
62 COMMAND_ABORTED = BIT(11),
63 IS_IN_WORK_LIST = BIT(12),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020064};
65
66/* Overrides scsi_pointer */
67struct uas_cmd_info {
68 unsigned int state;
Hans de Goedee28e2f2f2014-10-12 12:19:40 +020069 unsigned int uas_tag;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020070 struct urb *cmd_urb;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020071 struct urb *data_in_urb;
72 struct urb *data_out_urb;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020073};
74
75/* I hate forward declarations, but I actually have a loop */
76static int uas_submit_urbs(struct scsi_cmnd *cmnd,
Oliver Neukumb36d8392015-11-03 16:43:17 +010077 struct uas_dev_info *devinfo);
Sarah Sharpea9da1c2011-12-02 11:55:44 -080078static void uas_do_work(struct work_struct *work);
Gerd Hoffmann4c456972012-11-30 11:54:44 +010079static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
Gerd Hoffmannd89bd832013-09-13 13:27:11 +020080static void uas_free_streams(struct uas_dev_info *devinfo);
Hans de Goede1ad7ed52014-09-13 12:26:49 +020081static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
82 int status);
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +010083
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020084static void uas_do_work(struct work_struct *work)
85{
Gerd Hoffmann1bf81982013-09-13 13:27:12 +020086 struct uas_dev_info *devinfo =
87 container_of(work, struct uas_dev_info, work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020088 struct uas_cmd_info *cmdinfo;
Hans de Goede43cd99c2014-09-13 12:26:38 +020089 struct scsi_cmnd *cmnd;
Gerd Hoffmanne0648522012-09-25 10:47:08 +020090 unsigned long flags;
Hans de Goede43cd99c2014-09-13 12:26:38 +020091 int i, err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020092
Gerd Hoffmann1bf81982013-09-13 13:27:12 +020093 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goedeb7b5d112014-09-13 12:26:30 +020094
95 if (devinfo->resetting)
96 goto out;
97
Hans de Goede43cd99c2014-09-13 12:26:38 +020098 for (i = 0; i < devinfo->qdepth; i++) {
99 if (!devinfo->cmnd[i])
100 continue;
101
102 cmnd = devinfo->cmnd[i];
103 cmdinfo = (void *)&cmnd->SCp;
Hans de Goede61c09ce2013-11-12 13:44:20 +0100104
105 if (!(cmdinfo->state & IS_IN_WORK_LIST))
106 continue;
107
Oliver Neukumb36d8392015-11-03 16:43:17 +0100108 err = uas_submit_urbs(cmnd, cmnd->device->hostdata);
Hans de Goede61c09ce2013-11-12 13:44:20 +0100109 if (!err)
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100110 cmdinfo->state &= ~IS_IN_WORK_LIST;
Hans de Goede61c09ce2013-11-12 13:44:20 +0100111 else
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200112 schedule_work(&devinfo->work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200113 }
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200114out:
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100115 spin_unlock_irqrestore(&devinfo->lock, flags);
116}
117
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200118static void uas_add_work(struct uas_cmd_info *cmdinfo)
119{
120 struct scsi_pointer *scp = (void *)cmdinfo;
121 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
122 struct uas_dev_info *devinfo = cmnd->device->hostdata;
123
Sanjeev Sharmaab945ef2014-08-12 12:10:21 +0530124 lockdep_assert_held(&devinfo->lock);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200125 cmdinfo->state |= IS_IN_WORK_LIST;
126 schedule_work(&devinfo->work);
127}
128
Hans de Goede15893492014-09-13 12:26:36 +0200129static void uas_zap_pending(struct uas_dev_info *devinfo, int result)
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200130{
131 struct uas_cmd_info *cmdinfo;
Hans de Goede43cd99c2014-09-13 12:26:38 +0200132 struct scsi_cmnd *cmnd;
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200133 unsigned long flags;
Hans de Goede43cd99c2014-09-13 12:26:38 +0200134 int i, err;
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200135
136 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goede43cd99c2014-09-13 12:26:38 +0200137 for (i = 0; i < devinfo->qdepth; i++) {
138 if (!devinfo->cmnd[i])
139 continue;
140
141 cmnd = devinfo->cmnd[i];
142 cmdinfo = (void *)&cmnd->SCp;
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200143 uas_log_cmd_state(cmnd, __func__, 0);
Hans de Goede9c15c572014-09-13 12:26:37 +0200144 /* Sense urbs were killed, clear COMMAND_INFLIGHT manually */
145 cmdinfo->state &= ~COMMAND_INFLIGHT;
Hans de Goede15893492014-09-13 12:26:36 +0200146 cmnd->result = result << 16;
Hans de Goede9c15c572014-09-13 12:26:37 +0200147 err = uas_try_complete(cmnd, __func__);
148 WARN_ON(err != 0);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200149 }
150 spin_unlock_irqrestore(&devinfo->lock, flags);
151}
152
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200153static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
154{
155 struct sense_iu *sense_iu = urb->transfer_buffer;
156 struct scsi_device *sdev = cmnd->device;
157
158 if (urb->actual_length > 16) {
159 unsigned len = be16_to_cpup(&sense_iu->len);
160 if (len + 16 != urb->actual_length) {
161 int newlen = min(len + 16, urb->actual_length) - 16;
162 if (newlen < 0)
163 newlen = 0;
164 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
165 "disagrees with IU sense data length %d, "
166 "using %d bytes of sense data\n", __func__,
167 urb->actual_length, len, newlen);
168 len = newlen;
169 }
170 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
171 }
172
173 cmnd->result = sense_iu->status;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200174}
175
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200176static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
177 int status)
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200178{
179 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200180 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200181
Hans de Goede60d9f672014-09-13 12:26:34 +0200182 scmd_printk(KERN_INFO, cmnd,
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200183 "%s %d uas-tag %d inflight:%s%s%s%s%s%s%s%s%s%s%s%s ",
184 prefix, status, cmdinfo->uas_tag,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200185 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
186 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
187 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
188 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
189 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
190 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
191 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
192 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
193 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
194 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100195 (ci->state & COMMAND_ABORTED) ? " abort" : "",
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100196 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
Hans de Goede6dcd8ec2014-09-13 12:26:45 +0200197 scsi_print_command(cmnd);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200198}
199
Hans de Goede4c5481e2014-09-13 12:26:41 +0200200static void uas_free_unsubmitted_urbs(struct scsi_cmnd *cmnd)
201{
202 struct uas_cmd_info *cmdinfo;
203
204 if (!cmnd)
205 return;
206
207 cmdinfo = (void *)&cmnd->SCp;
208
209 if (cmdinfo->state & SUBMIT_CMD_URB)
210 usb_free_urb(cmdinfo->cmd_urb);
211
212 /* data urbs may have never gotten their submit flag set */
213 if (!(cmdinfo->state & DATA_IN_URB_INFLIGHT))
214 usb_free_urb(cmdinfo->data_in_urb);
215 if (!(cmdinfo->state & DATA_OUT_URB_INFLIGHT))
216 usb_free_urb(cmdinfo->data_out_urb);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200217}
218
219static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
220{
221 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200222 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200223
Sanjeev Sharmaab945ef2014-08-12 12:10:21 +0530224 lockdep_assert_held(&devinfo->lock);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200225 if (cmdinfo->state & (COMMAND_INFLIGHT |
226 DATA_IN_URB_INFLIGHT |
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100227 DATA_OUT_URB_INFLIGHT |
Hans de Goede616f0e62014-09-13 12:26:40 +0200228 COMMAND_ABORTED))
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200229 return -EBUSY;
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200230 devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL;
Hans de Goede4c5481e2014-09-13 12:26:41 +0200231 uas_free_unsubmitted_urbs(cmnd);
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200232 cmnd->scsi_done(cmnd);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200233 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200234}
235
236static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200237 unsigned direction)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200238{
239 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
240 int err;
241
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200242 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
Oliver Neukumb36d8392015-11-03 16:43:17 +0100243 err = uas_submit_urbs(cmnd, cmnd->device->hostdata);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200244 if (err) {
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200245 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200246 }
247}
248
Oliver Neukumaa742682016-01-19 10:51:09 +0100249static bool uas_evaluate_response_iu(struct response_iu *riu, struct scsi_cmnd *cmnd)
250{
251 u8 response_code = riu->response_code;
252
253 switch (response_code) {
254 case RC_INCORRECT_LUN:
255 cmnd->result = DID_BAD_TARGET << 16;
256 break;
257 case RC_TMF_SUCCEEDED:
258 cmnd->result = DID_OK << 16;
259 break;
260 case RC_TMF_NOT_SUPPORTED:
261 cmnd->result = DID_TARGET_FAILURE << 16;
262 break;
263 default:
264 uas_log_cmd_state(cmnd, "response iu", response_code);
265 cmnd->result = DID_ERROR << 16;
266 break;
267 }
268
269 return response_code == RC_TMF_SUCCEEDED;
270}
271
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200272static void uas_stat_cmplt(struct urb *urb)
273{
274 struct iu *iu = urb->transfer_buffer;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100275 struct Scsi_Host *shost = urb->context;
Hans de Goede21fc05b2013-11-13 09:32:22 +0100276 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede60d9f672014-09-13 12:26:34 +0200277 struct urb *data_in_urb = NULL;
278 struct urb *data_out_urb = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200279 struct scsi_cmnd *cmnd;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200280 struct uas_cmd_info *cmdinfo;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200281 unsigned long flags;
Hans de Goede5e61aed2014-09-13 12:26:32 +0200282 unsigned int idx;
Oliver Neukumcca26be2015-08-24 11:10:43 +0200283 int status = urb->status;
Oliver Neukumaa742682016-01-19 10:51:09 +0100284 bool success;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200285
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200286 spin_lock_irqsave(&devinfo->lock, flags);
287
288 if (devinfo->resetting)
289 goto out;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200290
Oliver Neukumcca26be2015-08-24 11:10:43 +0200291 if (status) {
292 if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
293 dev_err(&urb->dev->dev, "stat urb: status %d\n", status);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200294 goto out;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200295 }
296
Hans de Goede5e61aed2014-09-13 12:26:32 +0200297 idx = be16_to_cpup(&iu->tag) - 1;
298 if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) {
299 dev_err(&urb->dev->dev,
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200300 "stat urb: no pending cmd for uas-tag %d\n", idx + 1);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200301 goto out;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200302 }
303
Hans de Goede5e61aed2014-09-13 12:26:32 +0200304 cmnd = devinfo->cmnd[idx];
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200305 cmdinfo = (void *)&cmnd->SCp;
Hans de Goeded89da032014-09-13 12:26:33 +0200306
307 if (!(cmdinfo->state & COMMAND_INFLIGHT)) {
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200308 uas_log_cmd_state(cmnd, "unexpected status cmplt", 0);
Hans de Goeded89da032014-09-13 12:26:33 +0200309 goto out;
310 }
311
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200312 switch (iu->iu_id) {
313 case IU_ID_STATUS:
Hans de Goede5ad22cf2014-09-13 12:26:47 +0200314 uas_sense(urb, cmnd);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200315 if (cmnd->result != 0) {
316 /* cancel data transfers on error */
Hans de Goede60d9f672014-09-13 12:26:34 +0200317 data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
318 data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200319 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200320 cmdinfo->state &= ~COMMAND_INFLIGHT;
321 uas_try_complete(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200322 break;
323 case IU_ID_READ_READY:
Hans de Goede8e453152013-11-15 10:04:11 +0100324 if (!cmdinfo->data_in_urb ||
325 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200326 uas_log_cmd_state(cmnd, "unexpected read rdy", 0);
Hans de Goede8e453152013-11-15 10:04:11 +0100327 break;
328 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200329 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
330 break;
331 case IU_ID_WRITE_READY:
Hans de Goede8e453152013-11-15 10:04:11 +0100332 if (!cmdinfo->data_out_urb ||
333 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200334 uas_log_cmd_state(cmnd, "unexpected write rdy", 0);
Hans de Goede8e453152013-11-15 10:04:11 +0100335 break;
336 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200337 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
338 break;
Hans de Goedefac1f482014-09-13 12:26:51 +0200339 case IU_ID_RESPONSE:
Hans de Goedefac1f482014-09-13 12:26:51 +0200340 cmdinfo->state &= ~COMMAND_INFLIGHT;
Oliver Neukumaa742682016-01-19 10:51:09 +0100341 success = uas_evaluate_response_iu((struct response_iu *)iu, cmnd);
342 if (!success) {
343 /* Error, cancel data transfers */
344 data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
345 data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
346 }
Hans de Goedefac1f482014-09-13 12:26:51 +0200347 uas_try_complete(cmnd, __func__);
348 break;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200349 default:
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200350 uas_log_cmd_state(cmnd, "bogus IU", iu->iu_id);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200351 }
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200352out:
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200353 usb_free_urb(urb);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200354 spin_unlock_irqrestore(&devinfo->lock, flags);
Hans de Goede60d9f672014-09-13 12:26:34 +0200355
356 /* Unlinking of data urbs must be done without holding the lock */
357 if (data_in_urb) {
358 usb_unlink_urb(data_in_urb);
359 usb_put_urb(data_in_urb);
360 }
361 if (data_out_urb) {
362 usb_unlink_urb(data_out_urb);
363 usb_put_urb(data_out_urb);
364 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200365}
366
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200367static void uas_data_cmplt(struct urb *urb)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200368{
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200369 struct scsi_cmnd *cmnd = urb->context;
370 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200371 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200372 struct scsi_data_buffer *sdb = NULL;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200373 unsigned long flags;
Oliver Neukumcca26be2015-08-24 11:10:43 +0200374 int status = urb->status;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200375
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200376 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200377
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200378 if (cmdinfo->data_in_urb == urb) {
379 sdb = scsi_in(cmnd);
380 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
Hans de Goede85fea822014-09-13 12:26:35 +0200381 cmdinfo->data_in_urb = NULL;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200382 } else if (cmdinfo->data_out_urb == urb) {
383 sdb = scsi_out(cmnd);
384 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
Hans de Goede85fea822014-09-13 12:26:35 +0200385 cmdinfo->data_out_urb = NULL;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200386 }
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200387 if (sdb == NULL) {
388 WARN_ON_ONCE(1);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200389 goto out;
390 }
391
392 if (devinfo->resetting)
393 goto out;
394
Hans de Goeded89da032014-09-13 12:26:33 +0200395 /* Data urbs should not complete before the cmd urb is submitted */
396 if (cmdinfo->state & SUBMIT_CMD_URB) {
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200397 uas_log_cmd_state(cmnd, "unexpected data cmplt", 0);
Hans de Goeded89da032014-09-13 12:26:33 +0200398 goto out;
399 }
400
Oliver Neukumcca26be2015-08-24 11:10:43 +0200401 if (status) {
402 if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
403 uas_log_cmd_state(cmnd, "data cmplt err", status);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200404 /* error: no data transfered */
405 sdb->resid = sdb->length;
406 } else {
407 sdb->resid = sdb->length - urb->actual_length;
408 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200409 uas_try_complete(cmnd, __func__);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200410out:
Hans de Goede85fea822014-09-13 12:26:35 +0200411 usb_free_urb(urb);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200412 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200413}
414
Hans de Goede876285c2013-11-07 08:52:42 +0100415static void uas_cmd_cmplt(struct urb *urb)
416{
Hans de Goedeb6823c52014-09-13 12:26:39 +0200417 if (urb->status)
418 dev_err(&urb->dev->dev, "cmd cmplt err %d\n", urb->status);
Hans de Goede876285c2013-11-07 08:52:42 +0100419
Hans de Goede876285c2013-11-07 08:52:42 +0100420 usb_free_urb(urb);
421}
422
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200423static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200424 struct scsi_cmnd *cmnd,
425 enum dma_data_direction dir)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200426{
427 struct usb_device *udev = devinfo->udev;
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200428 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200429 struct urb *urb = usb_alloc_urb(0, gfp);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200430 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
431 ? scsi_in(cmnd) : scsi_out(cmnd);
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200432 unsigned int pipe = (dir == DMA_FROM_DEVICE)
433 ? devinfo->data_in_pipe : devinfo->data_out_pipe;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200434
435 if (!urb)
436 goto out;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200437 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
438 uas_data_cmplt, cmnd);
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200439 if (devinfo->use_streams)
440 urb->stream_id = cmdinfo->uas_tag;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200441 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
442 urb->sg = sdb->table.sgl;
443 out:
444 return urb;
445}
446
447static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200448 struct scsi_cmnd *cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200449{
450 struct usb_device *udev = devinfo->udev;
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200451 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200452 struct urb *urb = usb_alloc_urb(0, gfp);
453 struct sense_iu *iu;
454
455 if (!urb)
456 goto out;
457
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500458 iu = kzalloc(sizeof(*iu), gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200459 if (!iu)
460 goto free;
461
462 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200463 uas_stat_cmplt, cmnd->device->host);
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200464 if (devinfo->use_streams)
465 urb->stream_id = cmdinfo->uas_tag;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200466 urb->transfer_flags |= URB_FREE_BUFFER;
467 out:
468 return urb;
469 free:
470 usb_free_urb(urb);
471 return NULL;
472}
473
474static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Hans de Goedea887cd32013-10-17 19:47:28 +0200475 struct scsi_cmnd *cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200476{
477 struct usb_device *udev = devinfo->udev;
478 struct scsi_device *sdev = cmnd->device;
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200479 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200480 struct urb *urb = usb_alloc_urb(0, gfp);
481 struct command_iu *iu;
482 int len;
483
484 if (!urb)
485 goto out;
486
487 len = cmnd->cmd_len - 16;
488 if (len < 0)
489 len = 0;
490 len = ALIGN(len, 4);
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500491 iu = kzalloc(sizeof(*iu) + len, gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200492 if (!iu)
493 goto free;
494
495 iu->iu_id = IU_ID_COMMAND;
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200496 iu->tag = cpu_to_be16(cmdinfo->uas_tag);
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100497 iu->prio_attr = UAS_SIMPLE_TAG;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200498 iu->len = len;
499 int_to_scsilun(sdev->lun, &iu->lun);
500 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
501
502 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
Hans de Goedeb6823c52014-09-13 12:26:39 +0200503 uas_cmd_cmplt, NULL);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200504 urb->transfer_flags |= URB_FREE_BUFFER;
505 out:
506 return urb;
507 free:
508 usb_free_urb(urb);
509 return NULL;
510}
511
512/*
513 * Why should I request the Status IU before sending the Command IU? Spec
514 * says to, but also says the device may receive them in any order. Seems
515 * daft to me.
516 */
517
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200518static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd, gfp_t gfp)
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200519{
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200520 struct uas_dev_info *devinfo = cmnd->device->hostdata;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200521 struct urb *urb;
Hans de Goede876285c2013-11-07 08:52:42 +0100522 int err;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200523
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200524 urb = uas_alloc_sense_urb(devinfo, gfp, cmnd);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200525 if (!urb)
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100526 return NULL;
Hans de Goeded5f808d2013-10-17 19:30:26 +0200527 usb_anchor_urb(urb, &devinfo->sense_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100528 err = usb_submit_urb(urb, gfp);
529 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200530 usb_unanchor_urb(urb);
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200531 uas_log_cmd_state(cmnd, "sense submit err", err);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200532 usb_free_urb(urb);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100533 return NULL;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200534 }
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100535 return urb;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200536}
537
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200538static int uas_submit_urbs(struct scsi_cmnd *cmnd,
Oliver Neukumb36d8392015-11-03 16:43:17 +0100539 struct uas_dev_info *devinfo)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200540{
541 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100542 struct urb *urb;
Hans de Goede876285c2013-11-07 08:52:42 +0100543 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200544
Sanjeev Sharmaab945ef2014-08-12 12:10:21 +0530545 lockdep_assert_held(&devinfo->lock);
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500546 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Oliver Neukumb36d8392015-11-03 16:43:17 +0100547 urb = uas_submit_sense_urb(cmnd, GFP_ATOMIC);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100548 if (!urb)
549 return SCSI_MLQUEUE_DEVICE_BUSY;
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500550 cmdinfo->state &= ~SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200551 }
552
553 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
Oliver Neukumb36d8392015-11-03 16:43:17 +0100554 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, GFP_ATOMIC,
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200555 cmnd, DMA_FROM_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200556 if (!cmdinfo->data_in_urb)
557 return SCSI_MLQUEUE_DEVICE_BUSY;
558 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
559 }
560
561 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200562 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
Oliver Neukumb36d8392015-11-03 16:43:17 +0100563 err = usb_submit_urb(cmdinfo->data_in_urb, GFP_ATOMIC);
Hans de Goede876285c2013-11-07 08:52:42 +0100564 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200565 usb_unanchor_urb(cmdinfo->data_in_urb);
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200566 uas_log_cmd_state(cmnd, "data in submit err", err);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200567 return SCSI_MLQUEUE_DEVICE_BUSY;
568 }
569 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200570 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200571 }
572
573 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
Oliver Neukumb36d8392015-11-03 16:43:17 +0100574 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, GFP_ATOMIC,
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200575 cmnd, DMA_TO_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200576 if (!cmdinfo->data_out_urb)
577 return SCSI_MLQUEUE_DEVICE_BUSY;
578 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
579 }
580
581 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200582 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
Oliver Neukumb36d8392015-11-03 16:43:17 +0100583 err = usb_submit_urb(cmdinfo->data_out_urb, GFP_ATOMIC);
Hans de Goede876285c2013-11-07 08:52:42 +0100584 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200585 usb_unanchor_urb(cmdinfo->data_out_urb);
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200586 uas_log_cmd_state(cmnd, "data out submit err", err);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200587 return SCSI_MLQUEUE_DEVICE_BUSY;
588 }
589 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200590 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200591 }
592
593 if (cmdinfo->state & ALLOC_CMD_URB) {
Oliver Neukumb36d8392015-11-03 16:43:17 +0100594 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, GFP_ATOMIC, cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200595 if (!cmdinfo->cmd_urb)
596 return SCSI_MLQUEUE_DEVICE_BUSY;
597 cmdinfo->state &= ~ALLOC_CMD_URB;
598 }
599
600 if (cmdinfo->state & SUBMIT_CMD_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200601 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
Oliver Neukumb36d8392015-11-03 16:43:17 +0100602 err = usb_submit_urb(cmdinfo->cmd_urb, GFP_ATOMIC);
Hans de Goede876285c2013-11-07 08:52:42 +0100603 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200604 usb_unanchor_urb(cmdinfo->cmd_urb);
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200605 uas_log_cmd_state(cmnd, "cmd submit err", err);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200606 return SCSI_MLQUEUE_DEVICE_BUSY;
607 }
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200608 cmdinfo->cmd_urb = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200609 cmdinfo->state &= ~SUBMIT_CMD_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200610 cmdinfo->state |= COMMAND_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200611 }
612
613 return 0;
614}
615
Jeff Garzikf2812332010-11-16 02:10:29 -0500616static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200617 void (*done)(struct scsi_cmnd *))
618{
619 struct scsi_device *sdev = cmnd->device;
620 struct uas_dev_info *devinfo = sdev->hostdata;
621 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200622 unsigned long flags;
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200623 int idx, err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200624
625 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
626
Hans de Goedef9dc0242014-09-13 12:26:42 +0200627 /* Re-check scsi_block_requests now that we've the host-lock */
628 if (cmnd->device->host->host_self_blocked)
629 return SCSI_MLQUEUE_DEVICE_BUSY;
630
Hans de Goede59307852014-09-15 16:04:12 +0200631 if ((devinfo->flags & US_FL_NO_ATA_1X) &&
632 (cmnd->cmnd[0] == ATA_12 || cmnd->cmnd[0] == ATA_16)) {
633 memcpy(cmnd->sense_buffer, usb_stor_sense_invalidCDB,
634 sizeof(usb_stor_sense_invalidCDB));
635 cmnd->result = SAM_STAT_CHECK_CONDITION;
636 cmnd->scsi_done(cmnd);
637 return 0;
638 }
639
Hans de Goedec6f63202013-11-13 09:24:15 +0100640 spin_lock_irqsave(&devinfo->lock, flags);
641
Gerd Hoffmannf8be6bf2012-11-30 11:54:45 +0100642 if (devinfo->resetting) {
643 cmnd->result = DID_ERROR << 16;
644 cmnd->scsi_done(cmnd);
Hans de Goedec6f63202013-11-13 09:24:15 +0100645 spin_unlock_irqrestore(&devinfo->lock, flags);
Gerd Hoffmannf8be6bf2012-11-30 11:54:45 +0100646 return 0;
647 }
648
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200649 /* Find a free uas-tag */
650 for (idx = 0; idx < devinfo->qdepth; idx++) {
651 if (!devinfo->cmnd[idx])
652 break;
653 }
654 if (idx == devinfo->qdepth) {
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200655 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200656 return SCSI_MLQUEUE_DEVICE_BUSY;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200657 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200658
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200659 cmnd->scsi_done = done;
660
Hans de Goede5e61aed2014-09-13 12:26:32 +0200661 memset(cmdinfo, 0, sizeof(*cmdinfo));
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200662 cmdinfo->uas_tag = idx + 1; /* uas-tag == usb-stream-id, so 1 based */
Hans de Goedee0620002014-09-13 12:26:31 +0200663 cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200664
665 switch (cmnd->sc_data_direction) {
666 case DMA_FROM_DEVICE:
667 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
668 break;
669 case DMA_BIDIRECTIONAL:
670 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
671 case DMA_TO_DEVICE:
672 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
673 case DMA_NONE:
674 break;
675 }
676
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200677 if (!devinfo->use_streams)
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200678 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200679
Oliver Neukumb36d8392015-11-03 16:43:17 +0100680 err = uas_submit_urbs(cmnd, devinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200681 if (err) {
682 /* If we did nothing, give up now */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500683 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200684 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200685 return SCSI_MLQUEUE_DEVICE_BUSY;
686 }
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200687 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200688 }
689
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200690 devinfo->cmnd[idx] = cmnd;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200691 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200692 return 0;
693}
694
Jeff Garzikf2812332010-11-16 02:10:29 -0500695static DEF_SCSI_QCMD(uas_queuecommand)
696
Hans de Goede616f0e62014-09-13 12:26:40 +0200697/*
698 * For now we do not support actually sending an abort to the device, so
699 * this eh always fails. Still we must define it to make sure that we've
700 * dropped all references to the cmnd in question once this function exits.
701 */
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200702static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
703{
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200704 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200705 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Hans de Goede616f0e62014-09-13 12:26:40 +0200706 struct urb *data_in_urb = NULL;
707 struct urb *data_out_urb = NULL;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200708 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200709
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200710 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goedec6f63202013-11-13 09:24:15 +0100711
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200712 uas_log_cmd_state(cmnd, __func__, 0);
Hans de Goede616f0e62014-09-13 12:26:40 +0200713
714 /* Ensure that try_complete does not call scsi_done */
715 cmdinfo->state |= COMMAND_ABORTED;
716
717 /* Drop all refs to this cmnd, kill data urbs to break their ref */
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200718 devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL;
Hans de Goede616f0e62014-09-13 12:26:40 +0200719 if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
720 data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
721 if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
722 data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
723
Hans de Goede4c5481e2014-09-13 12:26:41 +0200724 uas_free_unsubmitted_urbs(cmnd);
725
Hans de Goede616f0e62014-09-13 12:26:40 +0200726 spin_unlock_irqrestore(&devinfo->lock, flags);
727
728 if (data_in_urb) {
729 usb_kill_urb(data_in_urb);
730 usb_put_urb(data_in_urb);
731 }
732 if (data_out_urb) {
733 usb_kill_urb(data_out_urb);
734 usb_put_urb(data_out_urb);
Hans de Goedec6f63202013-11-13 09:24:15 +0100735 }
736
Hans de Goede616f0e62014-09-13 12:26:40 +0200737 return FAILED;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200738}
739
740static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
741{
742 struct scsi_device *sdev = cmnd->device;
743 struct uas_dev_info *devinfo = sdev->hostdata;
744 struct usb_device *udev = devinfo->udev;
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200745 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200746 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200747
Hans de Goedebe326f42013-09-22 16:27:02 +0200748 err = usb_lock_device_for_reset(udev, devinfo->intf);
749 if (err) {
750 shost_printk(KERN_ERR, sdev->host,
751 "%s FAILED to get lock err %d\n", __func__, err);
752 return FAILED;
753 }
754
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200755 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200756
757 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200758 devinfo->resetting = 1;
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200759 spin_unlock_irqrestore(&devinfo->lock, flags);
760
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200761 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200762 usb_kill_anchored_urbs(&devinfo->sense_urbs);
763 usb_kill_anchored_urbs(&devinfo->data_urbs);
Hans de Goede15893492014-09-13 12:26:36 +0200764 uas_zap_pending(devinfo, DID_RESET);
765
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200766 err = usb_reset_device(udev);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200767
768 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200769 devinfo->resetting = 0;
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200770 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200771
Hans de Goedebe326f42013-09-22 16:27:02 +0200772 usb_unlock_device(udev);
773
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200774 if (err) {
Hans de Goedece39fe62014-09-13 12:26:50 +0200775 shost_printk(KERN_INFO, sdev->host, "%s FAILED err %d\n",
776 __func__, err);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200777 return FAILED;
778 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200779
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200780 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
781 return SUCCESS;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200782}
783
Hans de Goede13630742016-04-12 12:27:09 +0200784static int uas_target_alloc(struct scsi_target *starget)
785{
786 struct uas_dev_info *devinfo = (struct uas_dev_info *)
787 dev_to_shost(starget->dev.parent)->hostdata;
788
789 if (devinfo->flags & US_FL_NO_REPORT_LUNS)
790 starget->no_report_luns = 1;
791
792 return 0;
793}
794
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200795static int uas_slave_alloc(struct scsi_device *sdev)
796{
Hans de Goedeee136af2015-04-21 11:20:31 +0200797 struct uas_dev_info *devinfo =
798 (struct uas_dev_info *)sdev->host->hostdata;
799
800 sdev->hostdata = devinfo;
Hans de Goede37599f92013-11-15 10:04:31 +0100801
Felipe Balbif0183a32016-04-18 13:09:11 +0300802 /*
803 * USB has unusual DMA-alignment requirements: Although the
Hans de Goede37599f92013-11-15 10:04:31 +0100804 * starting address of each scatter-gather element doesn't matter,
805 * the length of each element except the last must be divisible
806 * by the Bulk maxpacket value. There's currently no way to
807 * express this by block-layer constraints, so we'll cop out
808 * and simply require addresses to be aligned at 512-byte
809 * boundaries. This is okay since most block I/O involves
810 * hardware sectors that are multiples of 512 bytes in length,
811 * and since host controllers up through USB 2.0 have maxpacket
812 * values no larger than 512.
813 *
814 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
815 * values can be as large as 2048. To make that work properly
816 * will require changes to the block layer.
817 */
818 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
819
Hans de Goedeee136af2015-04-21 11:20:31 +0200820 if (devinfo->flags & US_FL_MAX_SECTORS_64)
821 blk_queue_max_hw_sectors(sdev->request_queue, 64);
822 else if (devinfo->flags & US_FL_MAX_SECTORS_240)
823 blk_queue_max_hw_sectors(sdev->request_queue, 240);
824
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200825 return 0;
826}
827
828static int uas_slave_configure(struct scsi_device *sdev)
829{
830 struct uas_dev_info *devinfo = sdev->hostdata;
Hans de Goede734016b2014-09-16 18:36:52 +0200831
832 if (devinfo->flags & US_FL_NO_REPORT_OPCODES)
833 sdev->no_report_opcodes = 1;
834
Dmitry Katsubo9fa62b12015-11-20 01:30:44 +0100835 /* A few buggy USB-ATA bridges don't understand FUA */
836 if (devinfo->flags & US_FL_BROKEN_FUA)
837 sdev->broken_fua = 1;
838
Hans de Goede593224e2016-05-31 09:18:03 +0200839 scsi_change_queue_depth(sdev, devinfo->qdepth - 2);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200840 return 0;
841}
842
843static struct scsi_host_template uas_host_template = {
844 .module = THIS_MODULE,
845 .name = "uas",
846 .queuecommand = uas_queuecommand,
Hans de Goede13630742016-04-12 12:27:09 +0200847 .target_alloc = uas_target_alloc,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200848 .slave_alloc = uas_slave_alloc,
849 .slave_configure = uas_slave_configure,
850 .eh_abort_handler = uas_eh_abort_handler,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200851 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200852 .this_id = -1,
853 .sg_tablesize = SG_NONE,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200854 .skip_settle_delay = 1,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200855};
856
Hans de Goede79b4c062013-10-25 17:04:33 +0100857#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
858 vendorName, productName, useProtocol, useTransport, \
859 initFunction, flags) \
860{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
861 .driver_info = (flags) }
862
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200863static struct usb_device_id uas_usb_ids[] = {
Hans de Goede79b4c062013-10-25 17:04:33 +0100864# include "unusual_uas.h"
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200865 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
866 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200867 { }
868};
869MODULE_DEVICE_TABLE(usb, uas_usb_ids);
870
Hans de Goede79b4c062013-10-25 17:04:33 +0100871#undef UNUSUAL_DEV
872
Hans de Goedee1be0672013-10-21 08:00:58 +0100873static int uas_switch_interface(struct usb_device *udev,
874 struct usb_interface *intf)
875{
876 int alt;
877
878 alt = uas_find_uas_alt_setting(intf);
879 if (alt < 0)
880 return alt;
881
882 return usb_set_interface(udev,
883 intf->altsetting[0].desc.bInterfaceNumber, alt);
884}
885
Hans de Goede58d51442013-10-29 10:23:26 +0100886static int uas_configure_endpoints(struct uas_dev_info *devinfo)
Hans de Goede34f11e52013-10-29 08:54:48 +0100887{
888 struct usb_host_endpoint *eps[4] = { };
889 struct usb_device *udev = devinfo->udev;
890 int r;
891
Hans de Goede34f11e52013-10-29 08:54:48 +0100892 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
Hans de Goede74d71ae2013-10-29 10:10:36 +0100893 if (r)
894 return r;
Hans de Goede34f11e52013-10-29 08:54:48 +0100895
Hans de Goede74d71ae2013-10-29 10:10:36 +0100896 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
897 usb_endpoint_num(&eps[0]->desc));
898 devinfo->status_pipe = usb_rcvbulkpipe(udev,
899 usb_endpoint_num(&eps[1]->desc));
900 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
901 usb_endpoint_num(&eps[2]->desc));
902 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
903 usb_endpoint_num(&eps[3]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200904
Hans de Goedee5e55812014-09-13 12:26:43 +0200905 if (udev->speed < USB_SPEED_SUPER) {
Hans de Goedee2875c32014-08-01 17:33:08 +0200906 devinfo->qdepth = 32;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200907 devinfo->use_streams = 0;
908 } else {
Hans de Goede58d51442013-10-29 10:23:26 +0100909 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
Hans de Goede5e61aed2014-09-13 12:26:32 +0200910 3, MAX_CMNDS, GFP_NOIO);
Hans de Goede58d51442013-10-29 10:23:26 +0100911 if (devinfo->qdepth < 0)
912 return devinfo->qdepth;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200913 devinfo->use_streams = 1;
914 }
Hans de Goede58d51442013-10-29 10:23:26 +0100915
916 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200917}
918
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100919static void uas_free_streams(struct uas_dev_info *devinfo)
920{
921 struct usb_device *udev = devinfo->udev;
922 struct usb_host_endpoint *eps[3];
923
924 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
925 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
926 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
Oliver Neukum94d72f02014-03-28 11:25:50 +0100927 usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100928}
929
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200930static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
931{
Hans de Goede6ce82132013-10-17 19:00:45 +0200932 int result = -ENOMEM;
933 struct Scsi_Host *shost = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200934 struct uas_dev_info *devinfo;
935 struct usb_device *udev = interface_to_usbdev(intf);
Hans de Goedea5011d42015-04-21 11:20:30 +0200936 unsigned long dev_flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200937
Hans de Goedea5011d42015-04-21 11:20:30 +0200938 if (!uas_use_uas_driver(intf, id, &dev_flags))
Hans de Goede79b4c062013-10-25 17:04:33 +0100939 return -ENODEV;
940
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500941 if (uas_switch_interface(udev, intf))
942 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200943
Hans de Goede21fc05b2013-11-13 09:32:22 +0100944 shost = scsi_host_alloc(&uas_host_template,
945 sizeof(struct uas_dev_info));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200946 if (!shost)
Hans de Goede6ce82132013-10-17 19:00:45 +0200947 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200948
949 shost->max_cmd_len = 16 + 252;
950 shost->max_id = 1;
Gerd Hoffmannbde027b2013-01-25 15:03:36 +0100951 shost->max_lun = 256;
952 shost->max_channel = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200953 shost->sg_tablesize = udev->bus->sg_tablesize;
954
Hans de Goede21fc05b2013-11-13 09:32:22 +0100955 devinfo = (struct uas_dev_info *)shost->hostdata;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200956 devinfo->intf = intf;
957 devinfo->udev = udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200958 devinfo->resetting = 0;
Hans de Goededa65c2b2013-11-11 11:51:42 +0100959 devinfo->shutdown = 0;
Hans de Goedea5011d42015-04-21 11:20:30 +0200960 devinfo->flags = dev_flags;
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200961 init_usb_anchor(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +0200962 init_usb_anchor(&devinfo->sense_urbs);
963 init_usb_anchor(&devinfo->data_urbs);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200964 spin_lock_init(&devinfo->lock);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200965 INIT_WORK(&devinfo->work, uas_do_work);
Hans de Goede58d51442013-10-29 10:23:26 +0100966
967 result = uas_configure_endpoints(devinfo);
968 if (result)
969 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200970
Hans de Goede198de512016-04-12 12:27:08 +0200971 /*
972 * 1 tag is reserved for untagged commands +
973 * 1 tag to avoid off by one errors in some bridge firmwares
974 */
975 shost->can_queue = devinfo->qdepth - 2;
976
Oliver Neukumc637f1f2014-03-28 11:29:25 +0100977 usb_set_intfdata(intf, shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100978 result = scsi_add_host(shost, &intf->dev);
979 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +0200980 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100981
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200982 scsi_scan_host(shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200983 return result;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100984
Hans de Goede6ce82132013-10-17 19:00:45 +0200985free_streams:
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100986 uas_free_streams(devinfo);
Oliver Neukumc637f1f2014-03-28 11:29:25 +0100987 usb_set_intfdata(intf, NULL);
Hans de Goede6ce82132013-10-17 19:00:45 +0200988set_alt0:
989 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200990 if (shost)
991 scsi_host_put(shost);
992 return result;
993}
994
Hans de Goedef9dc0242014-09-13 12:26:42 +0200995static int uas_cmnd_list_empty(struct uas_dev_info *devinfo)
996{
997 unsigned long flags;
998 int i, r = 1;
999
1000 spin_lock_irqsave(&devinfo->lock, flags);
1001
1002 for (i = 0; i < devinfo->qdepth; i++) {
1003 if (devinfo->cmnd[i]) {
1004 r = 0; /* Not empty */
1005 break;
1006 }
1007 }
1008
1009 spin_unlock_irqrestore(&devinfo->lock, flags);
1010
1011 return r;
1012}
1013
1014/*
1015 * Wait for any pending cmnds to complete, on usb-2 sense_urbs may temporarily
1016 * get empty while there still is more work to do due to sense-urbs completing
1017 * with a READ/WRITE_READY iu code, so keep waiting until the list gets empty.
1018 */
1019static int uas_wait_for_pending_cmnds(struct uas_dev_info *devinfo)
1020{
1021 unsigned long start_time;
1022 int r;
1023
1024 start_time = jiffies;
1025 do {
1026 flush_work(&devinfo->work);
1027
1028 r = usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000);
1029 if (r == 0)
1030 return -ETIME;
1031
1032 r = usb_wait_anchor_empty_timeout(&devinfo->data_urbs, 500);
1033 if (r == 0)
1034 return -ETIME;
1035
1036 if (time_after(jiffies, start_time + 5 * HZ))
1037 return -ETIME;
1038 } while (!uas_cmnd_list_empty(devinfo));
1039
1040 return 0;
1041}
1042
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001043static int uas_pre_reset(struct usb_interface *intf)
1044{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001045 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001046 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede4de7a3732013-10-22 16:10:44 +01001047 unsigned long flags;
1048
Hans de Goededa65c2b2013-11-11 11:51:42 +01001049 if (devinfo->shutdown)
1050 return 0;
1051
Hans de Goede4de7a3732013-10-22 16:10:44 +01001052 /* Block new requests */
1053 spin_lock_irqsave(shost->host_lock, flags);
1054 scsi_block_requests(shost);
1055 spin_unlock_irqrestore(shost->host_lock, flags);
1056
Hans de Goedef9dc0242014-09-13 12:26:42 +02001057 if (uas_wait_for_pending_cmnds(devinfo) != 0) {
Hans de Goede4de7a3732013-10-22 16:10:44 +01001058 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
Hans de Goedef9dc0242014-09-13 12:26:42 +02001059 scsi_unblock_requests(shost);
Hans de Goede4de7a3732013-10-22 16:10:44 +01001060 return 1;
1061 }
1062
1063 uas_free_streams(devinfo);
1064
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001065 return 0;
1066}
1067
1068static int uas_post_reset(struct usb_interface *intf)
1069{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001070 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001071 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede4de7a3732013-10-22 16:10:44 +01001072 unsigned long flags;
Hans de Goedece39fe62014-09-13 12:26:50 +02001073 int err;
Hans de Goede4de7a3732013-10-22 16:10:44 +01001074
Hans de Goededa65c2b2013-11-11 11:51:42 +01001075 if (devinfo->shutdown)
1076 return 0;
1077
Hans de Goedece39fe62014-09-13 12:26:50 +02001078 err = uas_configure_endpoints(devinfo);
1079 if (err) {
Hans de Goede58d51442013-10-29 10:23:26 +01001080 shost_printk(KERN_ERR, shost,
Hans de Goedece39fe62014-09-13 12:26:50 +02001081 "%s: alloc streams error %d after reset",
1082 __func__, err);
Hans de Goede58d51442013-10-29 10:23:26 +01001083 return 1;
1084 }
Hans de Goede4de7a3732013-10-22 16:10:44 +01001085
1086 spin_lock_irqsave(shost->host_lock, flags);
1087 scsi_report_bus_reset(shost, 0);
1088 spin_unlock_irqrestore(shost->host_lock, flags);
1089
1090 scsi_unblock_requests(shost);
1091
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001092 return 0;
1093}
1094
Hans de Goede0df1f662013-11-07 08:47:05 +01001095static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1096{
1097 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001098 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede0df1f662013-11-07 08:47:05 +01001099
Hans de Goedef9dc0242014-09-13 12:26:42 +02001100 if (uas_wait_for_pending_cmnds(devinfo) != 0) {
Hans de Goede0df1f662013-11-07 08:47:05 +01001101 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1102 return -ETIME;
1103 }
1104
1105 return 0;
1106}
1107
1108static int uas_resume(struct usb_interface *intf)
1109{
1110 return 0;
1111}
1112
1113static int uas_reset_resume(struct usb_interface *intf)
1114{
1115 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001116 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede0df1f662013-11-07 08:47:05 +01001117 unsigned long flags;
Hans de Goedece39fe62014-09-13 12:26:50 +02001118 int err;
Hans de Goede0df1f662013-11-07 08:47:05 +01001119
Hans de Goedece39fe62014-09-13 12:26:50 +02001120 err = uas_configure_endpoints(devinfo);
1121 if (err) {
Hans de Goede0df1f662013-11-07 08:47:05 +01001122 shost_printk(KERN_ERR, shost,
Hans de Goedece39fe62014-09-13 12:26:50 +02001123 "%s: alloc streams error %d after reset",
1124 __func__, err);
Hans de Goede0df1f662013-11-07 08:47:05 +01001125 return -EIO;
1126 }
1127
1128 spin_lock_irqsave(shost->host_lock, flags);
1129 scsi_report_bus_reset(shost, 0);
1130 spin_unlock_irqrestore(shost->host_lock, flags);
1131
1132 return 0;
1133}
1134
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001135static void uas_disconnect(struct usb_interface *intf)
1136{
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001137 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001138 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goedeb7b5d112014-09-13 12:26:30 +02001139 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001140
Hans de Goedeb7b5d112014-09-13 12:26:30 +02001141 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001142 devinfo->resetting = 1;
Hans de Goedeb7b5d112014-09-13 12:26:30 +02001143 spin_unlock_irqrestore(&devinfo->lock, flags);
1144
Gerd Hoffmann1bf81982013-09-13 13:27:12 +02001145 cancel_work_sync(&devinfo->work);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +02001146 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +02001147 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1148 usb_kill_anchored_urbs(&devinfo->data_urbs);
Hans de Goede15893492014-09-13 12:26:36 +02001149 uas_zap_pending(devinfo, DID_NO_CONNECT);
1150
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001151 scsi_remove_host(shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001152 uas_free_streams(devinfo);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001153 scsi_host_put(shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001154}
1155
Hans de Goededa65c2b2013-11-11 11:51:42 +01001156/*
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 */
1161static 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);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001166 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goededa65c2b2013-11-11 11:51:42 +01001167
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 Wilcox115bb1f2010-10-07 13:05:23 +02001177static 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 Goede0df1f662013-11-07 08:47:05 +01001183 .suspend = uas_suspend,
1184 .resume = uas_resume,
1185 .reset_resume = uas_reset_resume,
Hans de Goededa65c2b2013-11-11 11:51:42 +01001186 .drvwrap.driver.shutdown = uas_shutdown,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001187 .id_table = uas_usb_ids,
1188};
1189
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001190module_usb_driver(uas_driver);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001191
1192MODULE_LICENSE("GPL");
Hans de Goedef50a4962013-10-28 10:48:04 +00001193MODULE_AUTHOR(
1194 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");