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