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