blob: 8dd200f920200a0f84de9f2f7ba93e9519ce2408 [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
Alexander Kappner187941e2018-05-18 21:50:15 -0700839 /* UAS also needs to support FL_ALWAYS_SYNC */
840 if (devinfo->flags & US_FL_ALWAYS_SYNC) {
841 sdev->skip_ms_page_3f = 1;
842 sdev->skip_ms_page_8 = 1;
843 sdev->wce_default_on = 1;
844 }
Hans de Goede593224e2016-05-31 09:18:03 +0200845 scsi_change_queue_depth(sdev, devinfo->qdepth - 2);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200846 return 0;
847}
848
849static struct scsi_host_template uas_host_template = {
850 .module = THIS_MODULE,
851 .name = "uas",
852 .queuecommand = uas_queuecommand,
Hans de Goede13630742016-04-12 12:27:09 +0200853 .target_alloc = uas_target_alloc,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200854 .slave_alloc = uas_slave_alloc,
855 .slave_configure = uas_slave_configure,
856 .eh_abort_handler = uas_eh_abort_handler,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200857 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200858 .this_id = -1,
859 .sg_tablesize = SG_NONE,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200860 .skip_settle_delay = 1,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200861};
862
Hans de Goede79b4c062013-10-25 17:04:33 +0100863#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
864 vendorName, productName, useProtocol, useTransport, \
865 initFunction, flags) \
866{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
867 .driver_info = (flags) }
868
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200869static struct usb_device_id uas_usb_ids[] = {
Hans de Goede79b4c062013-10-25 17:04:33 +0100870# include "unusual_uas.h"
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200871 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
872 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200873 { }
874};
875MODULE_DEVICE_TABLE(usb, uas_usb_ids);
876
Hans de Goede79b4c062013-10-25 17:04:33 +0100877#undef UNUSUAL_DEV
878
Hans de Goedee1be0672013-10-21 08:00:58 +0100879static int uas_switch_interface(struct usb_device *udev,
880 struct usb_interface *intf)
881{
Alan Sternd77606e2017-09-22 11:56:49 -0400882 struct usb_host_interface *alt;
Hans de Goedee1be0672013-10-21 08:00:58 +0100883
884 alt = uas_find_uas_alt_setting(intf);
Alan Sternd77606e2017-09-22 11:56:49 -0400885 if (!alt)
886 return -ENODEV;
Hans de Goedee1be0672013-10-21 08:00:58 +0100887
Alan Sternd77606e2017-09-22 11:56:49 -0400888 return usb_set_interface(udev, alt->desc.bInterfaceNumber,
889 alt->desc.bAlternateSetting);
Hans de Goedee1be0672013-10-21 08:00:58 +0100890}
891
Hans de Goede58d51442013-10-29 10:23:26 +0100892static int uas_configure_endpoints(struct uas_dev_info *devinfo)
Hans de Goede34f11e52013-10-29 08:54:48 +0100893{
894 struct usb_host_endpoint *eps[4] = { };
895 struct usb_device *udev = devinfo->udev;
896 int r;
897
Hans de Goede34f11e52013-10-29 08:54:48 +0100898 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
Hans de Goede74d71ae2013-10-29 10:10:36 +0100899 if (r)
900 return r;
Hans de Goede34f11e52013-10-29 08:54:48 +0100901
Hans de Goede74d71ae2013-10-29 10:10:36 +0100902 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
903 usb_endpoint_num(&eps[0]->desc));
904 devinfo->status_pipe = usb_rcvbulkpipe(udev,
905 usb_endpoint_num(&eps[1]->desc));
906 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
907 usb_endpoint_num(&eps[2]->desc));
908 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
909 usb_endpoint_num(&eps[3]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200910
Hans de Goedee5e55812014-09-13 12:26:43 +0200911 if (udev->speed < USB_SPEED_SUPER) {
Hans de Goedee2875c32014-08-01 17:33:08 +0200912 devinfo->qdepth = 32;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200913 devinfo->use_streams = 0;
914 } else {
Hans de Goede58d51442013-10-29 10:23:26 +0100915 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
Hans de Goede5e61aed2014-09-13 12:26:32 +0200916 3, MAX_CMNDS, GFP_NOIO);
Hans de Goede58d51442013-10-29 10:23:26 +0100917 if (devinfo->qdepth < 0)
918 return devinfo->qdepth;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200919 devinfo->use_streams = 1;
920 }
Hans de Goede58d51442013-10-29 10:23:26 +0100921
922 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200923}
924
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100925static void uas_free_streams(struct uas_dev_info *devinfo)
926{
927 struct usb_device *udev = devinfo->udev;
928 struct usb_host_endpoint *eps[3];
929
930 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
931 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
932 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
Oliver Neukum94d72f02014-03-28 11:25:50 +0100933 usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100934}
935
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200936static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
937{
Hans de Goede6ce82132013-10-17 19:00:45 +0200938 int result = -ENOMEM;
939 struct Scsi_Host *shost = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200940 struct uas_dev_info *devinfo;
941 struct usb_device *udev = interface_to_usbdev(intf);
Hans de Goedea5011d42015-04-21 11:20:30 +0200942 unsigned long dev_flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200943
Hans de Goedea5011d42015-04-21 11:20:30 +0200944 if (!uas_use_uas_driver(intf, id, &dev_flags))
Hans de Goede79b4c062013-10-25 17:04:33 +0100945 return -ENODEV;
946
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500947 if (uas_switch_interface(udev, intf))
948 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200949
Hans de Goede21fc05b2013-11-13 09:32:22 +0100950 shost = scsi_host_alloc(&uas_host_template,
951 sizeof(struct uas_dev_info));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200952 if (!shost)
Hans de Goede6ce82132013-10-17 19:00:45 +0200953 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200954
955 shost->max_cmd_len = 16 + 252;
956 shost->max_id = 1;
Gerd Hoffmannbde027b2013-01-25 15:03:36 +0100957 shost->max_lun = 256;
958 shost->max_channel = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200959 shost->sg_tablesize = udev->bus->sg_tablesize;
960
Hans de Goede21fc05b2013-11-13 09:32:22 +0100961 devinfo = (struct uas_dev_info *)shost->hostdata;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200962 devinfo->intf = intf;
963 devinfo->udev = udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200964 devinfo->resetting = 0;
Hans de Goededa65c2b2013-11-11 11:51:42 +0100965 devinfo->shutdown = 0;
Hans de Goedea5011d42015-04-21 11:20:30 +0200966 devinfo->flags = dev_flags;
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200967 init_usb_anchor(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +0200968 init_usb_anchor(&devinfo->sense_urbs);
969 init_usb_anchor(&devinfo->data_urbs);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200970 spin_lock_init(&devinfo->lock);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200971 INIT_WORK(&devinfo->work, uas_do_work);
Hans de Goede58d51442013-10-29 10:23:26 +0100972
973 result = uas_configure_endpoints(devinfo);
974 if (result)
975 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200976
Hans de Goede198de512016-04-12 12:27:08 +0200977 /*
978 * 1 tag is reserved for untagged commands +
979 * 1 tag to avoid off by one errors in some bridge firmwares
980 */
981 shost->can_queue = devinfo->qdepth - 2;
982
Oliver Neukumc637f1f2014-03-28 11:29:25 +0100983 usb_set_intfdata(intf, shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100984 result = scsi_add_host(shost, &intf->dev);
985 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +0200986 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100987
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200988 scsi_scan_host(shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200989 return result;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100990
Hans de Goede6ce82132013-10-17 19:00:45 +0200991free_streams:
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100992 uas_free_streams(devinfo);
Oliver Neukumc637f1f2014-03-28 11:29:25 +0100993 usb_set_intfdata(intf, NULL);
Hans de Goede6ce82132013-10-17 19:00:45 +0200994set_alt0:
995 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200996 if (shost)
997 scsi_host_put(shost);
998 return result;
999}
1000
Hans de Goedef9dc0242014-09-13 12:26:42 +02001001static int uas_cmnd_list_empty(struct uas_dev_info *devinfo)
1002{
1003 unsigned long flags;
1004 int i, r = 1;
1005
1006 spin_lock_irqsave(&devinfo->lock, flags);
1007
1008 for (i = 0; i < devinfo->qdepth; i++) {
1009 if (devinfo->cmnd[i]) {
1010 r = 0; /* Not empty */
1011 break;
1012 }
1013 }
1014
1015 spin_unlock_irqrestore(&devinfo->lock, flags);
1016
1017 return r;
1018}
1019
1020/*
1021 * Wait for any pending cmnds to complete, on usb-2 sense_urbs may temporarily
1022 * get empty while there still is more work to do due to sense-urbs completing
1023 * with a READ/WRITE_READY iu code, so keep waiting until the list gets empty.
1024 */
1025static int uas_wait_for_pending_cmnds(struct uas_dev_info *devinfo)
1026{
1027 unsigned long start_time;
1028 int r;
1029
1030 start_time = jiffies;
1031 do {
1032 flush_work(&devinfo->work);
1033
1034 r = usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000);
1035 if (r == 0)
1036 return -ETIME;
1037
1038 r = usb_wait_anchor_empty_timeout(&devinfo->data_urbs, 500);
1039 if (r == 0)
1040 return -ETIME;
1041
1042 if (time_after(jiffies, start_time + 5 * HZ))
1043 return -ETIME;
1044 } while (!uas_cmnd_list_empty(devinfo));
1045
1046 return 0;
1047}
1048
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001049static int uas_pre_reset(struct usb_interface *intf)
1050{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001051 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001052 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede4de7a3732013-10-22 16:10:44 +01001053 unsigned long flags;
1054
Hans de Goededa65c2b2013-11-11 11:51:42 +01001055 if (devinfo->shutdown)
1056 return 0;
1057
Hans de Goede4de7a3732013-10-22 16:10:44 +01001058 /* Block new requests */
1059 spin_lock_irqsave(shost->host_lock, flags);
1060 scsi_block_requests(shost);
1061 spin_unlock_irqrestore(shost->host_lock, flags);
1062
Hans de Goedef9dc0242014-09-13 12:26:42 +02001063 if (uas_wait_for_pending_cmnds(devinfo) != 0) {
Hans de Goede4de7a3732013-10-22 16:10:44 +01001064 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
Hans de Goedef9dc0242014-09-13 12:26:42 +02001065 scsi_unblock_requests(shost);
Hans de Goede4de7a3732013-10-22 16:10:44 +01001066 return 1;
1067 }
1068
1069 uas_free_streams(devinfo);
1070
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001071 return 0;
1072}
1073
1074static int uas_post_reset(struct usb_interface *intf)
1075{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001076 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001077 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede4de7a3732013-10-22 16:10:44 +01001078 unsigned long flags;
Hans de Goedece39fe62014-09-13 12:26:50 +02001079 int err;
Hans de Goede4de7a3732013-10-22 16:10:44 +01001080
Hans de Goededa65c2b2013-11-11 11:51:42 +01001081 if (devinfo->shutdown)
1082 return 0;
1083
Hans de Goedece39fe62014-09-13 12:26:50 +02001084 err = uas_configure_endpoints(devinfo);
Oliver Neukum438fd542018-03-06 15:04:24 +01001085 if (err && err != -ENODEV)
Hans de Goede58d51442013-10-29 10:23:26 +01001086 shost_printk(KERN_ERR, shost,
Hans de Goedece39fe62014-09-13 12:26:50 +02001087 "%s: alloc streams error %d after reset",
1088 __func__, err);
Hans de Goede4de7a3732013-10-22 16:10:44 +01001089
Oliver Neukum92e64a12018-01-11 13:10:16 +01001090 /* we must unblock the host in every case lest we deadlock */
Hans de Goede4de7a3732013-10-22 16:10:44 +01001091 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
Oliver Neukum92e64a12018-01-11 13:10:16 +01001097 return err ? 1 : 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001098}
1099
Hans de Goede0df1f662013-11-07 08:47:05 +01001100static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1101{
1102 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001103 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede0df1f662013-11-07 08:47:05 +01001104
Hans de Goedef9dc0242014-09-13 12:26:42 +02001105 if (uas_wait_for_pending_cmnds(devinfo) != 0) {
Hans de Goede0df1f662013-11-07 08:47:05 +01001106 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1107 return -ETIME;
1108 }
1109
1110 return 0;
1111}
1112
1113static int uas_resume(struct usb_interface *intf)
1114{
1115 return 0;
1116}
1117
1118static int uas_reset_resume(struct usb_interface *intf)
1119{
1120 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001121 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede0df1f662013-11-07 08:47:05 +01001122 unsigned long flags;
Hans de Goedece39fe62014-09-13 12:26:50 +02001123 int err;
Hans de Goede0df1f662013-11-07 08:47:05 +01001124
Hans de Goedece39fe62014-09-13 12:26:50 +02001125 err = uas_configure_endpoints(devinfo);
1126 if (err) {
Hans de Goede0df1f662013-11-07 08:47:05 +01001127 shost_printk(KERN_ERR, shost,
Hans de Goedece39fe62014-09-13 12:26:50 +02001128 "%s: alloc streams error %d after reset",
1129 __func__, err);
Hans de Goede0df1f662013-11-07 08:47:05 +01001130 return -EIO;
1131 }
1132
1133 spin_lock_irqsave(shost->host_lock, flags);
1134 scsi_report_bus_reset(shost, 0);
1135 spin_unlock_irqrestore(shost->host_lock, flags);
1136
1137 return 0;
1138}
1139
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001140static void uas_disconnect(struct usb_interface *intf)
1141{
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001142 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001143 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goedeb7b5d112014-09-13 12:26:30 +02001144 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001145
Hans de Goedeb7b5d112014-09-13 12:26:30 +02001146 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001147 devinfo->resetting = 1;
Hans de Goedeb7b5d112014-09-13 12:26:30 +02001148 spin_unlock_irqrestore(&devinfo->lock, flags);
1149
Gerd Hoffmann1bf81982013-09-13 13:27:12 +02001150 cancel_work_sync(&devinfo->work);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +02001151 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +02001152 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1153 usb_kill_anchored_urbs(&devinfo->data_urbs);
Hans de Goede15893492014-09-13 12:26:36 +02001154 uas_zap_pending(devinfo, DID_NO_CONNECT);
1155
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001156 scsi_remove_host(shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001157 uas_free_streams(devinfo);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001158 scsi_host_put(shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001159}
1160
Hans de Goededa65c2b2013-11-11 11:51:42 +01001161/*
1162 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1163 * hang on reboot when the device is still in uas mode. Note the reset is
1164 * necessary as some devices won't revert to usb-storage mode without it.
1165 */
1166static void uas_shutdown(struct device *dev)
1167{
1168 struct usb_interface *intf = to_usb_interface(dev);
1169 struct usb_device *udev = interface_to_usbdev(intf);
1170 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001171 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goededa65c2b2013-11-11 11:51:42 +01001172
1173 if (system_state != SYSTEM_RESTART)
1174 return;
1175
1176 devinfo->shutdown = 1;
1177 uas_free_streams(devinfo);
1178 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1179 usb_reset_device(udev);
1180}
1181
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001182static struct usb_driver uas_driver = {
1183 .name = "uas",
1184 .probe = uas_probe,
1185 .disconnect = uas_disconnect,
1186 .pre_reset = uas_pre_reset,
1187 .post_reset = uas_post_reset,
Hans de Goede0df1f662013-11-07 08:47:05 +01001188 .suspend = uas_suspend,
1189 .resume = uas_resume,
1190 .reset_resume = uas_reset_resume,
Hans de Goededa65c2b2013-11-11 11:51:42 +01001191 .drvwrap.driver.shutdown = uas_shutdown,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001192 .id_table = uas_usb_ids,
1193};
1194
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001195module_usb_driver(uas_driver);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001196
1197MODULE_LICENSE("GPL");
Hans de Goedef50a4962013-10-28 10:48:04 +00001198MODULE_AUTHOR(
1199 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");