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