Commit | Line | Data |
---|---|---|
c52661d6 SAS |
1 | /* Target based USB-Gadget |
2 | * | |
3 | * UAS protocol handling, target callbacks, configfs handling, | |
4 | * BBB (USB Mass Storage Class Bulk-Only (BBB) and Transport protocol handling. | |
5 | * | |
6 | * Author: Sebastian Andrzej Siewior <bigeasy at linutronix dot de> | |
7 | * License: GPLv2 as published by FSF. | |
8 | */ | |
9 | #include <linux/kernel.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/types.h> | |
12 | #include <linux/string.h> | |
13 | #include <linux/configfs.h> | |
14 | #include <linux/ctype.h> | |
15 | #include <linux/usb/ch9.h> | |
16 | #include <linux/usb/composite.h> | |
17 | #include <linux/usb/gadget.h> | |
18 | #include <linux/usb/storage.h> | |
19 | #include <scsi/scsi.h> | |
20 | #include <scsi/scsi_tcq.h> | |
21 | #include <target/target_core_base.h> | |
22 | #include <target/target_core_fabric.h> | |
23 | #include <target/target_core_fabric_configfs.h> | |
24 | #include <target/target_core_configfs.h> | |
25 | #include <target/configfs_macros.h> | |
26 | #include <asm/unaligned.h> | |
27 | ||
c52661d6 SAS |
28 | #include "tcm_usb_gadget.h" |
29 | ||
7d16e8d3 SAS |
30 | USB_GADGET_COMPOSITE_OPTIONS(); |
31 | ||
c52661d6 SAS |
32 | static struct target_fabric_configfs *usbg_fabric_configfs; |
33 | ||
34 | static inline struct f_uas *to_f_uas(struct usb_function *f) | |
35 | { | |
36 | return container_of(f, struct f_uas, function); | |
37 | } | |
38 | ||
39 | static void usbg_cmd_release(struct kref *); | |
40 | ||
41 | static inline void usbg_cleanup_cmd(struct usbg_cmd *cmd) | |
42 | { | |
43 | kref_put(&cmd->ref, usbg_cmd_release); | |
44 | } | |
45 | ||
46 | /* Start bot.c code */ | |
47 | ||
48 | static int bot_enqueue_cmd_cbw(struct f_uas *fu) | |
49 | { | |
50 | int ret; | |
51 | ||
52 | if (fu->flags & USBG_BOT_CMD_PEND) | |
53 | return 0; | |
54 | ||
55 | ret = usb_ep_queue(fu->ep_out, fu->cmd.req, GFP_ATOMIC); | |
56 | if (!ret) | |
57 | fu->flags |= USBG_BOT_CMD_PEND; | |
58 | return ret; | |
59 | } | |
60 | ||
61 | static void bot_status_complete(struct usb_ep *ep, struct usb_request *req) | |
62 | { | |
63 | struct usbg_cmd *cmd = req->context; | |
64 | struct f_uas *fu = cmd->fu; | |
65 | ||
66 | usbg_cleanup_cmd(cmd); | |
67 | if (req->status < 0) { | |
68 | pr_err("ERR %s(%d)\n", __func__, __LINE__); | |
69 | return; | |
70 | } | |
71 | ||
72 | /* CSW completed, wait for next CBW */ | |
73 | bot_enqueue_cmd_cbw(fu); | |
74 | } | |
75 | ||
76 | static void bot_enqueue_sense_code(struct f_uas *fu, struct usbg_cmd *cmd) | |
77 | { | |
78 | struct bulk_cs_wrap *csw = &fu->bot_status.csw; | |
79 | int ret; | |
80 | u8 *sense; | |
81 | unsigned int csw_stat; | |
82 | ||
83 | csw_stat = cmd->csw_code; | |
84 | ||
85 | /* | |
86 | * We can't send SENSE as a response. So we take ASC & ASCQ from our | |
87 | * sense buffer and queue it and hope the host sends a REQUEST_SENSE | |
88 | * command where it learns why we failed. | |
89 | */ | |
90 | sense = cmd->sense_iu.sense; | |
91 | ||
92 | csw->Tag = cmd->bot_tag; | |
93 | csw->Status = csw_stat; | |
94 | fu->bot_status.req->context = cmd; | |
95 | ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_ATOMIC); | |
96 | if (ret) | |
97 | pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret); | |
98 | } | |
99 | ||
100 | static void bot_err_compl(struct usb_ep *ep, struct usb_request *req) | |
101 | { | |
102 | struct usbg_cmd *cmd = req->context; | |
103 | struct f_uas *fu = cmd->fu; | |
104 | ||
105 | if (req->status < 0) | |
106 | pr_err("ERR %s(%d)\n", __func__, __LINE__); | |
107 | ||
108 | if (cmd->data_len) { | |
109 | if (cmd->data_len > ep->maxpacket) { | |
110 | req->length = ep->maxpacket; | |
111 | cmd->data_len -= ep->maxpacket; | |
112 | } else { | |
113 | req->length = cmd->data_len; | |
114 | cmd->data_len = 0; | |
115 | } | |
116 | ||
117 | usb_ep_queue(ep, req, GFP_ATOMIC); | |
118 | return ; | |
119 | } | |
120 | bot_enqueue_sense_code(fu, cmd); | |
121 | } | |
122 | ||
123 | static void bot_send_bad_status(struct usbg_cmd *cmd) | |
124 | { | |
125 | struct f_uas *fu = cmd->fu; | |
126 | struct bulk_cs_wrap *csw = &fu->bot_status.csw; | |
127 | struct usb_request *req; | |
128 | struct usb_ep *ep; | |
129 | ||
130 | csw->Residue = cpu_to_le32(cmd->data_len); | |
131 | ||
132 | if (cmd->data_len) { | |
133 | if (cmd->is_read) { | |
134 | ep = fu->ep_in; | |
135 | req = fu->bot_req_in; | |
136 | } else { | |
137 | ep = fu->ep_out; | |
138 | req = fu->bot_req_out; | |
139 | } | |
140 | ||
141 | if (cmd->data_len > fu->ep_in->maxpacket) { | |
142 | req->length = ep->maxpacket; | |
143 | cmd->data_len -= ep->maxpacket; | |
144 | } else { | |
145 | req->length = cmd->data_len; | |
146 | cmd->data_len = 0; | |
147 | } | |
148 | req->complete = bot_err_compl; | |
149 | req->context = cmd; | |
150 | req->buf = fu->cmd.buf; | |
151 | usb_ep_queue(ep, req, GFP_KERNEL); | |
152 | } else { | |
153 | bot_enqueue_sense_code(fu, cmd); | |
154 | } | |
155 | } | |
156 | ||
157 | static int bot_send_status(struct usbg_cmd *cmd, bool moved_data) | |
158 | { | |
159 | struct f_uas *fu = cmd->fu; | |
160 | struct bulk_cs_wrap *csw = &fu->bot_status.csw; | |
161 | int ret; | |
162 | ||
163 | if (cmd->se_cmd.scsi_status == SAM_STAT_GOOD) { | |
164 | if (!moved_data && cmd->data_len) { | |
165 | /* | |
166 | * the host wants to move data, we don't. Fill / empty | |
167 | * the pipe and then send the csw with reside set. | |
168 | */ | |
169 | cmd->csw_code = US_BULK_STAT_OK; | |
170 | bot_send_bad_status(cmd); | |
171 | return 0; | |
172 | } | |
173 | ||
174 | csw->Tag = cmd->bot_tag; | |
175 | csw->Residue = cpu_to_le32(0); | |
176 | csw->Status = US_BULK_STAT_OK; | |
177 | fu->bot_status.req->context = cmd; | |
178 | ||
179 | ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_KERNEL); | |
180 | if (ret) | |
181 | pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret); | |
182 | } else { | |
183 | cmd->csw_code = US_BULK_STAT_FAIL; | |
184 | bot_send_bad_status(cmd); | |
185 | } | |
186 | return 0; | |
187 | } | |
188 | ||
189 | /* | |
190 | * Called after command (no data transfer) or after the write (to device) | |
191 | * operation is completed | |
192 | */ | |
193 | static int bot_send_status_response(struct usbg_cmd *cmd) | |
194 | { | |
195 | bool moved_data = false; | |
196 | ||
197 | if (!cmd->is_read) | |
198 | moved_data = true; | |
199 | return bot_send_status(cmd, moved_data); | |
200 | } | |
201 | ||
202 | /* Read request completed, now we have to send the CSW */ | |
203 | static void bot_read_compl(struct usb_ep *ep, struct usb_request *req) | |
204 | { | |
205 | struct usbg_cmd *cmd = req->context; | |
206 | ||
207 | if (req->status < 0) | |
208 | pr_err("ERR %s(%d)\n", __func__, __LINE__); | |
209 | ||
210 | bot_send_status(cmd, true); | |
211 | } | |
212 | ||
213 | static int bot_send_read_response(struct usbg_cmd *cmd) | |
214 | { | |
215 | struct f_uas *fu = cmd->fu; | |
216 | struct se_cmd *se_cmd = &cmd->se_cmd; | |
217 | struct usb_gadget *gadget = fuas_to_gadget(fu); | |
218 | int ret; | |
219 | ||
220 | if (!cmd->data_len) { | |
221 | cmd->csw_code = US_BULK_STAT_PHASE; | |
222 | bot_send_bad_status(cmd); | |
223 | return 0; | |
224 | } | |
225 | ||
226 | if (!gadget->sg_supported) { | |
227 | cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC); | |
228 | if (!cmd->data_buf) | |
229 | return -ENOMEM; | |
230 | ||
231 | sg_copy_to_buffer(se_cmd->t_data_sg, | |
232 | se_cmd->t_data_nents, | |
233 | cmd->data_buf, | |
234 | se_cmd->data_length); | |
235 | ||
236 | fu->bot_req_in->buf = cmd->data_buf; | |
237 | } else { | |
238 | fu->bot_req_in->buf = NULL; | |
239 | fu->bot_req_in->num_sgs = se_cmd->t_data_nents; | |
240 | fu->bot_req_in->sg = se_cmd->t_data_sg; | |
241 | } | |
242 | ||
243 | fu->bot_req_in->complete = bot_read_compl; | |
244 | fu->bot_req_in->length = se_cmd->data_length; | |
245 | fu->bot_req_in->context = cmd; | |
246 | ret = usb_ep_queue(fu->ep_in, fu->bot_req_in, GFP_ATOMIC); | |
247 | if (ret) | |
248 | pr_err("%s(%d)\n", __func__, __LINE__); | |
249 | return 0; | |
250 | } | |
251 | ||
252 | static void usbg_data_write_cmpl(struct usb_ep *, struct usb_request *); | |
253 | static int usbg_prepare_w_request(struct usbg_cmd *, struct usb_request *); | |
254 | ||
255 | static int bot_send_write_request(struct usbg_cmd *cmd) | |
256 | { | |
257 | struct f_uas *fu = cmd->fu; | |
258 | struct se_cmd *se_cmd = &cmd->se_cmd; | |
259 | struct usb_gadget *gadget = fuas_to_gadget(fu); | |
260 | int ret; | |
261 | ||
262 | init_completion(&cmd->write_complete); | |
263 | cmd->fu = fu; | |
264 | ||
265 | if (!cmd->data_len) { | |
266 | cmd->csw_code = US_BULK_STAT_PHASE; | |
267 | return -EINVAL; | |
268 | } | |
269 | ||
270 | if (!gadget->sg_supported) { | |
271 | cmd->data_buf = kmalloc(se_cmd->data_length, GFP_KERNEL); | |
272 | if (!cmd->data_buf) | |
273 | return -ENOMEM; | |
274 | ||
275 | fu->bot_req_out->buf = cmd->data_buf; | |
276 | } else { | |
277 | fu->bot_req_out->buf = NULL; | |
278 | fu->bot_req_out->num_sgs = se_cmd->t_data_nents; | |
279 | fu->bot_req_out->sg = se_cmd->t_data_sg; | |
280 | } | |
281 | ||
282 | fu->bot_req_out->complete = usbg_data_write_cmpl; | |
283 | fu->bot_req_out->length = se_cmd->data_length; | |
284 | fu->bot_req_out->context = cmd; | |
285 | ||
286 | ret = usbg_prepare_w_request(cmd, fu->bot_req_out); | |
287 | if (ret) | |
288 | goto cleanup; | |
289 | ret = usb_ep_queue(fu->ep_out, fu->bot_req_out, GFP_KERNEL); | |
290 | if (ret) | |
291 | pr_err("%s(%d)\n", __func__, __LINE__); | |
292 | ||
293 | wait_for_completion(&cmd->write_complete); | |
70baf0ab | 294 | target_execute_cmd(se_cmd); |
c52661d6 SAS |
295 | cleanup: |
296 | return ret; | |
297 | } | |
298 | ||
299 | static int bot_submit_command(struct f_uas *, void *, unsigned int); | |
300 | ||
301 | static void bot_cmd_complete(struct usb_ep *ep, struct usb_request *req) | |
302 | { | |
303 | struct f_uas *fu = req->context; | |
304 | int ret; | |
305 | ||
306 | fu->flags &= ~USBG_BOT_CMD_PEND; | |
307 | ||
308 | if (req->status < 0) | |
309 | return; | |
310 | ||
311 | ret = bot_submit_command(fu, req->buf, req->actual); | |
312 | if (ret) | |
313 | pr_err("%s(%d): %d\n", __func__, __LINE__, ret); | |
314 | } | |
315 | ||
316 | static int bot_prepare_reqs(struct f_uas *fu) | |
317 | { | |
318 | int ret; | |
319 | ||
320 | fu->bot_req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL); | |
321 | if (!fu->bot_req_in) | |
322 | goto err; | |
323 | ||
324 | fu->bot_req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL); | |
325 | if (!fu->bot_req_out) | |
326 | goto err_out; | |
327 | ||
328 | fu->cmd.req = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL); | |
329 | if (!fu->cmd.req) | |
330 | goto err_cmd; | |
331 | ||
332 | fu->bot_status.req = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL); | |
333 | if (!fu->bot_status.req) | |
334 | goto err_sts; | |
335 | ||
336 | fu->bot_status.req->buf = &fu->bot_status.csw; | |
337 | fu->bot_status.req->length = US_BULK_CS_WRAP_LEN; | |
338 | fu->bot_status.req->complete = bot_status_complete; | |
339 | fu->bot_status.csw.Signature = cpu_to_le32(US_BULK_CS_SIGN); | |
340 | ||
341 | fu->cmd.buf = kmalloc(fu->ep_out->maxpacket, GFP_KERNEL); | |
342 | if (!fu->cmd.buf) | |
343 | goto err_buf; | |
344 | ||
345 | fu->cmd.req->complete = bot_cmd_complete; | |
346 | fu->cmd.req->buf = fu->cmd.buf; | |
347 | fu->cmd.req->length = fu->ep_out->maxpacket; | |
348 | fu->cmd.req->context = fu; | |
349 | ||
350 | ret = bot_enqueue_cmd_cbw(fu); | |
351 | if (ret) | |
352 | goto err_queue; | |
353 | return 0; | |
354 | err_queue: | |
355 | kfree(fu->cmd.buf); | |
356 | fu->cmd.buf = NULL; | |
357 | err_buf: | |
358 | usb_ep_free_request(fu->ep_in, fu->bot_status.req); | |
359 | err_sts: | |
360 | usb_ep_free_request(fu->ep_out, fu->cmd.req); | |
361 | fu->cmd.req = NULL; | |
362 | err_cmd: | |
363 | usb_ep_free_request(fu->ep_out, fu->bot_req_out); | |
364 | fu->bot_req_out = NULL; | |
365 | err_out: | |
366 | usb_ep_free_request(fu->ep_in, fu->bot_req_in); | |
367 | fu->bot_req_in = NULL; | |
368 | err: | |
369 | pr_err("BOT: endpoint setup failed\n"); | |
370 | return -ENOMEM; | |
371 | } | |
372 | ||
373 | void bot_cleanup_old_alt(struct f_uas *fu) | |
374 | { | |
375 | if (!(fu->flags & USBG_ENABLED)) | |
376 | return; | |
377 | ||
378 | usb_ep_disable(fu->ep_in); | |
379 | usb_ep_disable(fu->ep_out); | |
380 | ||
381 | if (!fu->bot_req_in) | |
382 | return; | |
383 | ||
384 | usb_ep_free_request(fu->ep_in, fu->bot_req_in); | |
385 | usb_ep_free_request(fu->ep_out, fu->bot_req_out); | |
386 | usb_ep_free_request(fu->ep_out, fu->cmd.req); | |
387 | usb_ep_free_request(fu->ep_out, fu->bot_status.req); | |
388 | ||
389 | kfree(fu->cmd.buf); | |
390 | ||
391 | fu->bot_req_in = NULL; | |
392 | fu->bot_req_out = NULL; | |
393 | fu->cmd.req = NULL; | |
394 | fu->bot_status.req = NULL; | |
395 | fu->cmd.buf = NULL; | |
396 | } | |
397 | ||
398 | static void bot_set_alt(struct f_uas *fu) | |
399 | { | |
400 | struct usb_function *f = &fu->function; | |
401 | struct usb_gadget *gadget = f->config->cdev->gadget; | |
402 | int ret; | |
403 | ||
404 | fu->flags = USBG_IS_BOT; | |
405 | ||
406 | config_ep_by_speed(gadget, f, fu->ep_in); | |
407 | ret = usb_ep_enable(fu->ep_in); | |
408 | if (ret) | |
409 | goto err_b_in; | |
410 | ||
411 | config_ep_by_speed(gadget, f, fu->ep_out); | |
412 | ret = usb_ep_enable(fu->ep_out); | |
413 | if (ret) | |
414 | goto err_b_out; | |
415 | ||
416 | ret = bot_prepare_reqs(fu); | |
417 | if (ret) | |
418 | goto err_wq; | |
419 | fu->flags |= USBG_ENABLED; | |
420 | pr_info("Using the BOT protocol\n"); | |
421 | return; | |
422 | err_wq: | |
423 | usb_ep_disable(fu->ep_out); | |
424 | err_b_out: | |
425 | usb_ep_disable(fu->ep_in); | |
426 | err_b_in: | |
427 | fu->flags = USBG_IS_BOT; | |
428 | } | |
429 | ||
430 | static int usbg_bot_setup(struct usb_function *f, | |
431 | const struct usb_ctrlrequest *ctrl) | |
432 | { | |
433 | struct f_uas *fu = to_f_uas(f); | |
434 | struct usb_composite_dev *cdev = f->config->cdev; | |
435 | u16 w_value = le16_to_cpu(ctrl->wValue); | |
436 | u16 w_length = le16_to_cpu(ctrl->wLength); | |
437 | int luns; | |
438 | u8 *ret_lun; | |
439 | ||
440 | switch (ctrl->bRequest) { | |
441 | case US_BULK_GET_MAX_LUN: | |
442 | if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_CLASS | | |
443 | USB_RECIP_INTERFACE)) | |
444 | return -ENOTSUPP; | |
445 | ||
446 | if (w_length < 1) | |
447 | return -EINVAL; | |
448 | if (w_value != 0) | |
449 | return -EINVAL; | |
450 | luns = atomic_read(&fu->tpg->tpg_port_count); | |
451 | if (!luns) { | |
452 | pr_err("No LUNs configured?\n"); | |
453 | return -EINVAL; | |
454 | } | |
455 | /* | |
456 | * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be | |
457 | * accessed. The upper limit is 0xf | |
458 | */ | |
459 | luns--; | |
460 | if (luns > 0xf) { | |
461 | pr_info_once("Limiting the number of luns to 16\n"); | |
462 | luns = 0xf; | |
463 | } | |
464 | ret_lun = cdev->req->buf; | |
465 | *ret_lun = luns; | |
466 | cdev->req->length = 1; | |
467 | return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC); | |
468 | break; | |
469 | ||
470 | case US_BULK_RESET_REQUEST: | |
471 | /* XXX maybe we should remove previous requests for IN + OUT */ | |
472 | bot_enqueue_cmd_cbw(fu); | |
473 | return 0; | |
474 | break; | |
475 | }; | |
476 | return -ENOTSUPP; | |
477 | } | |
478 | ||
479 | /* Start uas.c code */ | |
480 | ||
481 | static void uasp_cleanup_one_stream(struct f_uas *fu, struct uas_stream *stream) | |
482 | { | |
483 | /* We have either all three allocated or none */ | |
484 | if (!stream->req_in) | |
485 | return; | |
486 | ||
487 | usb_ep_free_request(fu->ep_in, stream->req_in); | |
488 | usb_ep_free_request(fu->ep_out, stream->req_out); | |
489 | usb_ep_free_request(fu->ep_status, stream->req_status); | |
490 | ||
491 | stream->req_in = NULL; | |
492 | stream->req_out = NULL; | |
493 | stream->req_status = NULL; | |
494 | } | |
495 | ||
496 | static void uasp_free_cmdreq(struct f_uas *fu) | |
497 | { | |
498 | usb_ep_free_request(fu->ep_cmd, fu->cmd.req); | |
499 | kfree(fu->cmd.buf); | |
500 | fu->cmd.req = NULL; | |
501 | fu->cmd.buf = NULL; | |
502 | } | |
503 | ||
504 | static void uasp_cleanup_old_alt(struct f_uas *fu) | |
505 | { | |
506 | int i; | |
507 | ||
508 | if (!(fu->flags & USBG_ENABLED)) | |
509 | return; | |
510 | ||
511 | usb_ep_disable(fu->ep_in); | |
512 | usb_ep_disable(fu->ep_out); | |
513 | usb_ep_disable(fu->ep_status); | |
514 | usb_ep_disable(fu->ep_cmd); | |
515 | ||
516 | for (i = 0; i < UASP_SS_EP_COMP_NUM_STREAMS; i++) | |
517 | uasp_cleanup_one_stream(fu, &fu->stream[i]); | |
518 | uasp_free_cmdreq(fu); | |
519 | } | |
520 | ||
521 | static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req); | |
522 | ||
523 | static int uasp_prepare_r_request(struct usbg_cmd *cmd) | |
524 | { | |
525 | struct se_cmd *se_cmd = &cmd->se_cmd; | |
526 | struct f_uas *fu = cmd->fu; | |
527 | struct usb_gadget *gadget = fuas_to_gadget(fu); | |
528 | struct uas_stream *stream = cmd->stream; | |
529 | ||
530 | if (!gadget->sg_supported) { | |
531 | cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC); | |
532 | if (!cmd->data_buf) | |
533 | return -ENOMEM; | |
534 | ||
535 | sg_copy_to_buffer(se_cmd->t_data_sg, | |
536 | se_cmd->t_data_nents, | |
537 | cmd->data_buf, | |
538 | se_cmd->data_length); | |
539 | ||
540 | stream->req_in->buf = cmd->data_buf; | |
541 | } else { | |
542 | stream->req_in->buf = NULL; | |
543 | stream->req_in->num_sgs = se_cmd->t_data_nents; | |
544 | stream->req_in->sg = se_cmd->t_data_sg; | |
545 | } | |
546 | ||
547 | stream->req_in->complete = uasp_status_data_cmpl; | |
548 | stream->req_in->length = se_cmd->data_length; | |
549 | stream->req_in->context = cmd; | |
550 | ||
551 | cmd->state = UASP_SEND_STATUS; | |
552 | return 0; | |
553 | } | |
554 | ||
555 | static void uasp_prepare_status(struct usbg_cmd *cmd) | |
556 | { | |
557 | struct se_cmd *se_cmd = &cmd->se_cmd; | |
558 | struct sense_iu *iu = &cmd->sense_iu; | |
559 | struct uas_stream *stream = cmd->stream; | |
560 | ||
561 | cmd->state = UASP_QUEUE_COMMAND; | |
562 | iu->iu_id = IU_ID_STATUS; | |
563 | iu->tag = cpu_to_be16(cmd->tag); | |
564 | ||
565 | /* | |
566 | * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?); | |
567 | */ | |
568 | iu->len = cpu_to_be16(se_cmd->scsi_sense_length); | |
569 | iu->status = se_cmd->scsi_status; | |
570 | stream->req_status->context = cmd; | |
571 | stream->req_status->length = se_cmd->scsi_sense_length + 16; | |
572 | stream->req_status->buf = iu; | |
573 | stream->req_status->complete = uasp_status_data_cmpl; | |
574 | } | |
575 | ||
576 | static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req) | |
577 | { | |
578 | struct usbg_cmd *cmd = req->context; | |
579 | struct uas_stream *stream = cmd->stream; | |
580 | struct f_uas *fu = cmd->fu; | |
581 | int ret; | |
582 | ||
583 | if (req->status < 0) | |
584 | goto cleanup; | |
585 | ||
586 | switch (cmd->state) { | |
587 | case UASP_SEND_DATA: | |
588 | ret = uasp_prepare_r_request(cmd); | |
589 | if (ret) | |
590 | goto cleanup; | |
591 | ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC); | |
592 | if (ret) | |
593 | pr_err("%s(%d) => %d\n", __func__, __LINE__, ret); | |
594 | break; | |
595 | ||
596 | case UASP_RECEIVE_DATA: | |
597 | ret = usbg_prepare_w_request(cmd, stream->req_out); | |
598 | if (ret) | |
599 | goto cleanup; | |
600 | ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC); | |
601 | if (ret) | |
602 | pr_err("%s(%d) => %d\n", __func__, __LINE__, ret); | |
603 | break; | |
604 | ||
605 | case UASP_SEND_STATUS: | |
606 | uasp_prepare_status(cmd); | |
607 | ret = usb_ep_queue(fu->ep_status, stream->req_status, | |
608 | GFP_ATOMIC); | |
609 | if (ret) | |
610 | pr_err("%s(%d) => %d\n", __func__, __LINE__, ret); | |
611 | break; | |
612 | ||
613 | case UASP_QUEUE_COMMAND: | |
614 | usbg_cleanup_cmd(cmd); | |
615 | usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC); | |
616 | break; | |
617 | ||
618 | default: | |
619 | BUG(); | |
620 | }; | |
621 | return; | |
622 | ||
623 | cleanup: | |
624 | usbg_cleanup_cmd(cmd); | |
625 | } | |
626 | ||
627 | static int uasp_send_status_response(struct usbg_cmd *cmd) | |
628 | { | |
629 | struct f_uas *fu = cmd->fu; | |
630 | struct uas_stream *stream = cmd->stream; | |
631 | struct sense_iu *iu = &cmd->sense_iu; | |
632 | ||
633 | iu->tag = cpu_to_be16(cmd->tag); | |
634 | stream->req_status->complete = uasp_status_data_cmpl; | |
635 | stream->req_status->context = cmd; | |
636 | cmd->fu = fu; | |
637 | uasp_prepare_status(cmd); | |
638 | return usb_ep_queue(fu->ep_status, stream->req_status, GFP_ATOMIC); | |
639 | } | |
640 | ||
641 | static int uasp_send_read_response(struct usbg_cmd *cmd) | |
642 | { | |
643 | struct f_uas *fu = cmd->fu; | |
644 | struct uas_stream *stream = cmd->stream; | |
645 | struct sense_iu *iu = &cmd->sense_iu; | |
646 | int ret; | |
647 | ||
648 | cmd->fu = fu; | |
649 | ||
650 | iu->tag = cpu_to_be16(cmd->tag); | |
651 | if (fu->flags & USBG_USE_STREAMS) { | |
652 | ||
653 | ret = uasp_prepare_r_request(cmd); | |
654 | if (ret) | |
655 | goto out; | |
656 | ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC); | |
657 | if (ret) { | |
658 | pr_err("%s(%d) => %d\n", __func__, __LINE__, ret); | |
659 | kfree(cmd->data_buf); | |
660 | cmd->data_buf = NULL; | |
661 | } | |
662 | ||
663 | } else { | |
664 | ||
665 | iu->iu_id = IU_ID_READ_READY; | |
666 | iu->tag = cpu_to_be16(cmd->tag); | |
667 | ||
668 | stream->req_status->complete = uasp_status_data_cmpl; | |
669 | stream->req_status->context = cmd; | |
670 | ||
671 | cmd->state = UASP_SEND_DATA; | |
672 | stream->req_status->buf = iu; | |
673 | stream->req_status->length = sizeof(struct iu); | |
674 | ||
675 | ret = usb_ep_queue(fu->ep_status, stream->req_status, | |
676 | GFP_ATOMIC); | |
677 | if (ret) | |
678 | pr_err("%s(%d) => %d\n", __func__, __LINE__, ret); | |
679 | } | |
680 | out: | |
681 | return ret; | |
682 | } | |
683 | ||
684 | static int uasp_send_write_request(struct usbg_cmd *cmd) | |
685 | { | |
686 | struct f_uas *fu = cmd->fu; | |
687 | struct se_cmd *se_cmd = &cmd->se_cmd; | |
688 | struct uas_stream *stream = cmd->stream; | |
689 | struct sense_iu *iu = &cmd->sense_iu; | |
690 | int ret; | |
691 | ||
692 | init_completion(&cmd->write_complete); | |
693 | cmd->fu = fu; | |
694 | ||
695 | iu->tag = cpu_to_be16(cmd->tag); | |
696 | ||
697 | if (fu->flags & USBG_USE_STREAMS) { | |
698 | ||
699 | ret = usbg_prepare_w_request(cmd, stream->req_out); | |
700 | if (ret) | |
701 | goto cleanup; | |
702 | ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC); | |
703 | if (ret) | |
704 | pr_err("%s(%d)\n", __func__, __LINE__); | |
705 | ||
706 | } else { | |
707 | ||
708 | iu->iu_id = IU_ID_WRITE_READY; | |
709 | iu->tag = cpu_to_be16(cmd->tag); | |
710 | ||
711 | stream->req_status->complete = uasp_status_data_cmpl; | |
712 | stream->req_status->context = cmd; | |
713 | ||
714 | cmd->state = UASP_RECEIVE_DATA; | |
715 | stream->req_status->buf = iu; | |
716 | stream->req_status->length = sizeof(struct iu); | |
717 | ||
718 | ret = usb_ep_queue(fu->ep_status, stream->req_status, | |
719 | GFP_ATOMIC); | |
720 | if (ret) | |
721 | pr_err("%s(%d)\n", __func__, __LINE__); | |
722 | } | |
723 | ||
724 | wait_for_completion(&cmd->write_complete); | |
70baf0ab | 725 | target_execute_cmd(se_cmd); |
c52661d6 SAS |
726 | cleanup: |
727 | return ret; | |
728 | } | |
729 | ||
730 | static int usbg_submit_command(struct f_uas *, void *, unsigned int); | |
731 | ||
732 | static void uasp_cmd_complete(struct usb_ep *ep, struct usb_request *req) | |
733 | { | |
734 | struct f_uas *fu = req->context; | |
735 | int ret; | |
736 | ||
737 | if (req->status < 0) | |
738 | return; | |
739 | ||
740 | ret = usbg_submit_command(fu, req->buf, req->actual); | |
741 | /* | |
742 | * Once we tune for performance enqueue the command req here again so | |
743 | * we can receive a second command while we processing this one. Pay | |
744 | * attention to properly sync STAUS endpoint with DATA IN + OUT so you | |
745 | * don't break HS. | |
746 | */ | |
747 | if (!ret) | |
748 | return; | |
749 | usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC); | |
750 | } | |
751 | ||
752 | static int uasp_alloc_stream_res(struct f_uas *fu, struct uas_stream *stream) | |
753 | { | |
754 | stream->req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL); | |
755 | if (!stream->req_in) | |
756 | goto out; | |
757 | ||
758 | stream->req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL); | |
759 | if (!stream->req_out) | |
760 | goto err_out; | |
761 | ||
762 | stream->req_status = usb_ep_alloc_request(fu->ep_status, GFP_KERNEL); | |
763 | if (!stream->req_status) | |
764 | goto err_sts; | |
765 | ||
766 | return 0; | |
767 | err_sts: | |
768 | usb_ep_free_request(fu->ep_status, stream->req_status); | |
769 | stream->req_status = NULL; | |
770 | err_out: | |
771 | usb_ep_free_request(fu->ep_out, stream->req_out); | |
772 | stream->req_out = NULL; | |
773 | out: | |
774 | return -ENOMEM; | |
775 | } | |
776 | ||
777 | static int uasp_alloc_cmd(struct f_uas *fu) | |
778 | { | |
779 | fu->cmd.req = usb_ep_alloc_request(fu->ep_cmd, GFP_KERNEL); | |
780 | if (!fu->cmd.req) | |
781 | goto err; | |
782 | ||
783 | fu->cmd.buf = kmalloc(fu->ep_cmd->maxpacket, GFP_KERNEL); | |
784 | if (!fu->cmd.buf) | |
785 | goto err_buf; | |
786 | ||
787 | fu->cmd.req->complete = uasp_cmd_complete; | |
788 | fu->cmd.req->buf = fu->cmd.buf; | |
789 | fu->cmd.req->length = fu->ep_cmd->maxpacket; | |
790 | fu->cmd.req->context = fu; | |
791 | return 0; | |
792 | ||
793 | err_buf: | |
794 | usb_ep_free_request(fu->ep_cmd, fu->cmd.req); | |
795 | err: | |
796 | return -ENOMEM; | |
797 | } | |
798 | ||
799 | static void uasp_setup_stream_res(struct f_uas *fu, int max_streams) | |
800 | { | |
801 | int i; | |
802 | ||
803 | for (i = 0; i < max_streams; i++) { | |
804 | struct uas_stream *s = &fu->stream[i]; | |
805 | ||
806 | s->req_in->stream_id = i + 1; | |
807 | s->req_out->stream_id = i + 1; | |
808 | s->req_status->stream_id = i + 1; | |
809 | } | |
810 | } | |
811 | ||
812 | static int uasp_prepare_reqs(struct f_uas *fu) | |
813 | { | |
814 | int ret; | |
815 | int i; | |
816 | int max_streams; | |
817 | ||
818 | if (fu->flags & USBG_USE_STREAMS) | |
819 | max_streams = UASP_SS_EP_COMP_NUM_STREAMS; | |
820 | else | |
821 | max_streams = 1; | |
822 | ||
823 | for (i = 0; i < max_streams; i++) { | |
824 | ret = uasp_alloc_stream_res(fu, &fu->stream[i]); | |
825 | if (ret) | |
826 | goto err_cleanup; | |
827 | } | |
828 | ||
829 | ret = uasp_alloc_cmd(fu); | |
830 | if (ret) | |
831 | goto err_free_stream; | |
832 | uasp_setup_stream_res(fu, max_streams); | |
833 | ||
834 | ret = usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC); | |
835 | if (ret) | |
836 | goto err_free_stream; | |
837 | ||
838 | return 0; | |
839 | ||
840 | err_free_stream: | |
841 | uasp_free_cmdreq(fu); | |
842 | ||
843 | err_cleanup: | |
844 | if (i) { | |
845 | do { | |
846 | uasp_cleanup_one_stream(fu, &fu->stream[i - 1]); | |
847 | i--; | |
848 | } while (i); | |
849 | } | |
850 | pr_err("UASP: endpoint setup failed\n"); | |
851 | return ret; | |
852 | } | |
853 | ||
854 | static void uasp_set_alt(struct f_uas *fu) | |
855 | { | |
856 | struct usb_function *f = &fu->function; | |
857 | struct usb_gadget *gadget = f->config->cdev->gadget; | |
858 | int ret; | |
859 | ||
860 | fu->flags = USBG_IS_UAS; | |
861 | ||
862 | if (gadget->speed == USB_SPEED_SUPER) | |
863 | fu->flags |= USBG_USE_STREAMS; | |
864 | ||
865 | config_ep_by_speed(gadget, f, fu->ep_in); | |
866 | ret = usb_ep_enable(fu->ep_in); | |
867 | if (ret) | |
868 | goto err_b_in; | |
869 | ||
870 | config_ep_by_speed(gadget, f, fu->ep_out); | |
871 | ret = usb_ep_enable(fu->ep_out); | |
872 | if (ret) | |
873 | goto err_b_out; | |
874 | ||
875 | config_ep_by_speed(gadget, f, fu->ep_cmd); | |
876 | ret = usb_ep_enable(fu->ep_cmd); | |
877 | if (ret) | |
878 | goto err_cmd; | |
879 | config_ep_by_speed(gadget, f, fu->ep_status); | |
880 | ret = usb_ep_enable(fu->ep_status); | |
881 | if (ret) | |
882 | goto err_status; | |
883 | ||
884 | ret = uasp_prepare_reqs(fu); | |
885 | if (ret) | |
886 | goto err_wq; | |
887 | fu->flags |= USBG_ENABLED; | |
888 | ||
889 | pr_info("Using the UAS protocol\n"); | |
890 | return; | |
891 | err_wq: | |
892 | usb_ep_disable(fu->ep_status); | |
893 | err_status: | |
894 | usb_ep_disable(fu->ep_cmd); | |
895 | err_cmd: | |
896 | usb_ep_disable(fu->ep_out); | |
897 | err_b_out: | |
898 | usb_ep_disable(fu->ep_in); | |
899 | err_b_in: | |
900 | fu->flags = 0; | |
901 | } | |
902 | ||
903 | static int get_cmd_dir(const unsigned char *cdb) | |
904 | { | |
905 | int ret; | |
906 | ||
907 | switch (cdb[0]) { | |
908 | case READ_6: | |
909 | case READ_10: | |
910 | case READ_12: | |
911 | case READ_16: | |
912 | case INQUIRY: | |
913 | case MODE_SENSE: | |
914 | case MODE_SENSE_10: | |
915 | case SERVICE_ACTION_IN: | |
916 | case MAINTENANCE_IN: | |
917 | case PERSISTENT_RESERVE_IN: | |
918 | case SECURITY_PROTOCOL_IN: | |
919 | case ACCESS_CONTROL_IN: | |
920 | case REPORT_LUNS: | |
921 | case READ_BLOCK_LIMITS: | |
922 | case READ_POSITION: | |
923 | case READ_CAPACITY: | |
924 | case READ_TOC: | |
925 | case READ_FORMAT_CAPACITIES: | |
926 | case REQUEST_SENSE: | |
927 | ret = DMA_FROM_DEVICE; | |
928 | break; | |
929 | ||
930 | case WRITE_6: | |
931 | case WRITE_10: | |
932 | case WRITE_12: | |
933 | case WRITE_16: | |
934 | case MODE_SELECT: | |
935 | case MODE_SELECT_10: | |
936 | case WRITE_VERIFY: | |
937 | case WRITE_VERIFY_12: | |
938 | case PERSISTENT_RESERVE_OUT: | |
939 | case MAINTENANCE_OUT: | |
940 | case SECURITY_PROTOCOL_OUT: | |
941 | case ACCESS_CONTROL_OUT: | |
942 | ret = DMA_TO_DEVICE; | |
943 | break; | |
944 | case ALLOW_MEDIUM_REMOVAL: | |
945 | case TEST_UNIT_READY: | |
946 | case SYNCHRONIZE_CACHE: | |
947 | case START_STOP: | |
948 | case ERASE: | |
949 | case REZERO_UNIT: | |
950 | case SEEK_10: | |
951 | case SPACE: | |
952 | case VERIFY: | |
953 | case WRITE_FILEMARKS: | |
954 | ret = DMA_NONE; | |
955 | break; | |
956 | default: | |
957 | pr_warn("target: Unknown data direction for SCSI Opcode " | |
958 | "0x%02x\n", cdb[0]); | |
959 | ret = -EINVAL; | |
960 | } | |
961 | return ret; | |
962 | } | |
963 | ||
964 | static void usbg_data_write_cmpl(struct usb_ep *ep, struct usb_request *req) | |
965 | { | |
966 | struct usbg_cmd *cmd = req->context; | |
967 | struct se_cmd *se_cmd = &cmd->se_cmd; | |
968 | ||
969 | if (req->status < 0) { | |
970 | pr_err("%s() state %d transfer failed\n", __func__, cmd->state); | |
971 | goto cleanup; | |
972 | } | |
973 | ||
974 | if (req->num_sgs == 0) { | |
975 | sg_copy_from_buffer(se_cmd->t_data_sg, | |
976 | se_cmd->t_data_nents, | |
977 | cmd->data_buf, | |
978 | se_cmd->data_length); | |
979 | } | |
980 | ||
981 | complete(&cmd->write_complete); | |
982 | return; | |
983 | ||
984 | cleanup: | |
985 | usbg_cleanup_cmd(cmd); | |
986 | } | |
987 | ||
988 | static int usbg_prepare_w_request(struct usbg_cmd *cmd, struct usb_request *req) | |
989 | { | |
990 | struct se_cmd *se_cmd = &cmd->se_cmd; | |
991 | struct f_uas *fu = cmd->fu; | |
992 | struct usb_gadget *gadget = fuas_to_gadget(fu); | |
993 | ||
994 | if (!gadget->sg_supported) { | |
995 | cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC); | |
996 | if (!cmd->data_buf) | |
997 | return -ENOMEM; | |
998 | ||
999 | req->buf = cmd->data_buf; | |
1000 | } else { | |
1001 | req->buf = NULL; | |
1002 | req->num_sgs = se_cmd->t_data_nents; | |
1003 | req->sg = se_cmd->t_data_sg; | |
1004 | } | |
1005 | ||
1006 | req->complete = usbg_data_write_cmpl; | |
1007 | req->length = se_cmd->data_length; | |
1008 | req->context = cmd; | |
1009 | return 0; | |
1010 | } | |
1011 | ||
1012 | static int usbg_send_status_response(struct se_cmd *se_cmd) | |
1013 | { | |
1014 | struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd, | |
1015 | se_cmd); | |
1016 | struct f_uas *fu = cmd->fu; | |
1017 | ||
1018 | if (fu->flags & USBG_IS_BOT) | |
1019 | return bot_send_status_response(cmd); | |
1020 | else | |
1021 | return uasp_send_status_response(cmd); | |
1022 | } | |
1023 | ||
1024 | static int usbg_send_write_request(struct se_cmd *se_cmd) | |
1025 | { | |
1026 | struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd, | |
1027 | se_cmd); | |
1028 | struct f_uas *fu = cmd->fu; | |
1029 | ||
1030 | if (fu->flags & USBG_IS_BOT) | |
1031 | return bot_send_write_request(cmd); | |
1032 | else | |
1033 | return uasp_send_write_request(cmd); | |
1034 | } | |
1035 | ||
1036 | static int usbg_send_read_response(struct se_cmd *se_cmd) | |
1037 | { | |
1038 | struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd, | |
1039 | se_cmd); | |
1040 | struct f_uas *fu = cmd->fu; | |
1041 | ||
1042 | if (fu->flags & USBG_IS_BOT) | |
1043 | return bot_send_read_response(cmd); | |
1044 | else | |
1045 | return uasp_send_read_response(cmd); | |
1046 | } | |
1047 | ||
1048 | static void usbg_cmd_work(struct work_struct *work) | |
1049 | { | |
1050 | struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work); | |
1051 | struct se_cmd *se_cmd; | |
1052 | struct tcm_usbg_nexus *tv_nexus; | |
1053 | struct usbg_tpg *tpg; | |
1054 | int dir; | |
1055 | ||
1056 | se_cmd = &cmd->se_cmd; | |
1057 | tpg = cmd->fu->tpg; | |
1058 | tv_nexus = tpg->tpg_nexus; | |
1059 | dir = get_cmd_dir(cmd->cmd_buf); | |
1060 | if (dir < 0) { | |
1061 | transport_init_se_cmd(se_cmd, | |
1062 | tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo, | |
1063 | tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE, | |
1064 | cmd->prio_attr, cmd->sense_iu.sense); | |
d6dfc868 | 1065 | goto out; |
c52661d6 SAS |
1066 | } |
1067 | ||
d6dfc868 | 1068 | if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess, |
c52661d6 | 1069 | cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun, |
d6dfc868 RD |
1070 | 0, cmd->prio_attr, dir, TARGET_SCF_UNKNOWN_SIZE) < 0) |
1071 | goto out; | |
1072 | ||
1073 | return; | |
1074 | ||
1075 | out: | |
1076 | transport_send_check_condition_and_sense(se_cmd, | |
1077 | TCM_UNSUPPORTED_SCSI_OPCODE, 1); | |
1078 | usbg_cleanup_cmd(cmd); | |
c52661d6 SAS |
1079 | } |
1080 | ||
1081 | static int usbg_submit_command(struct f_uas *fu, | |
1082 | void *cmdbuf, unsigned int len) | |
1083 | { | |
1084 | struct command_iu *cmd_iu = cmdbuf; | |
1085 | struct usbg_cmd *cmd; | |
1086 | struct usbg_tpg *tpg; | |
1087 | struct se_cmd *se_cmd; | |
1088 | struct tcm_usbg_nexus *tv_nexus; | |
1089 | u32 cmd_len; | |
1090 | int ret; | |
1091 | ||
1092 | if (cmd_iu->iu_id != IU_ID_COMMAND) { | |
1093 | pr_err("Unsupported type %d\n", cmd_iu->iu_id); | |
1094 | return -EINVAL; | |
1095 | } | |
1096 | ||
1097 | cmd = kzalloc(sizeof *cmd, GFP_ATOMIC); | |
1098 | if (!cmd) | |
1099 | return -ENOMEM; | |
1100 | ||
1101 | cmd->fu = fu; | |
1102 | ||
1103 | /* XXX until I figure out why I can't free in on complete */ | |
1104 | kref_init(&cmd->ref); | |
1105 | kref_get(&cmd->ref); | |
1106 | ||
1107 | tpg = fu->tpg; | |
1108 | cmd_len = (cmd_iu->len & ~0x3) + 16; | |
1109 | if (cmd_len > USBG_MAX_CMD) | |
1110 | goto err; | |
1111 | ||
1112 | memcpy(cmd->cmd_buf, cmd_iu->cdb, cmd_len); | |
1113 | ||
1114 | cmd->tag = be16_to_cpup(&cmd_iu->tag); | |
1115 | if (fu->flags & USBG_USE_STREAMS) { | |
1116 | if (cmd->tag > UASP_SS_EP_COMP_NUM_STREAMS) | |
1117 | goto err; | |
1118 | if (!cmd->tag) | |
1119 | cmd->stream = &fu->stream[0]; | |
1120 | else | |
1121 | cmd->stream = &fu->stream[cmd->tag - 1]; | |
1122 | } else { | |
1123 | cmd->stream = &fu->stream[0]; | |
1124 | } | |
1125 | ||
1126 | tv_nexus = tpg->tpg_nexus; | |
1127 | if (!tv_nexus) { | |
1128 | pr_err("Missing nexus, ignoring command\n"); | |
1129 | goto err; | |
1130 | } | |
1131 | ||
1132 | switch (cmd_iu->prio_attr & 0x7) { | |
1133 | case UAS_HEAD_TAG: | |
1134 | cmd->prio_attr = MSG_HEAD_TAG; | |
1135 | break; | |
1136 | case UAS_ORDERED_TAG: | |
1137 | cmd->prio_attr = MSG_ORDERED_TAG; | |
1138 | break; | |
1139 | case UAS_ACA: | |
1140 | cmd->prio_attr = MSG_ACA_TAG; | |
1141 | break; | |
1142 | default: | |
1143 | pr_debug_once("Unsupported prio_attr: %02x.\n", | |
1144 | cmd_iu->prio_attr); | |
1145 | case UAS_SIMPLE_TAG: | |
1146 | cmd->prio_attr = MSG_SIMPLE_TAG; | |
1147 | break; | |
1148 | } | |
1149 | ||
1150 | se_cmd = &cmd->se_cmd; | |
1151 | cmd->unpacked_lun = scsilun_to_int(&cmd_iu->lun); | |
1152 | ||
1153 | INIT_WORK(&cmd->work, usbg_cmd_work); | |
1154 | ret = queue_work(tpg->workqueue, &cmd->work); | |
1155 | if (ret < 0) | |
1156 | goto err; | |
1157 | ||
1158 | return 0; | |
1159 | err: | |
1160 | kfree(cmd); | |
1161 | return -EINVAL; | |
1162 | } | |
1163 | ||
1164 | static void bot_cmd_work(struct work_struct *work) | |
1165 | { | |
1166 | struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work); | |
1167 | struct se_cmd *se_cmd; | |
1168 | struct tcm_usbg_nexus *tv_nexus; | |
1169 | struct usbg_tpg *tpg; | |
1170 | int dir; | |
1171 | ||
1172 | se_cmd = &cmd->se_cmd; | |
1173 | tpg = cmd->fu->tpg; | |
1174 | tv_nexus = tpg->tpg_nexus; | |
1175 | dir = get_cmd_dir(cmd->cmd_buf); | |
1176 | if (dir < 0) { | |
1177 | transport_init_se_cmd(se_cmd, | |
1178 | tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo, | |
1179 | tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE, | |
1180 | cmd->prio_attr, cmd->sense_iu.sense); | |
d6dfc868 | 1181 | goto out; |
c52661d6 SAS |
1182 | } |
1183 | ||
d6dfc868 | 1184 | if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess, |
c52661d6 | 1185 | cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun, |
d6dfc868 RD |
1186 | cmd->data_len, cmd->prio_attr, dir, 0) < 0) |
1187 | goto out; | |
1188 | ||
1189 | return; | |
1190 | ||
1191 | out: | |
1192 | transport_send_check_condition_and_sense(se_cmd, | |
1193 | TCM_UNSUPPORTED_SCSI_OPCODE, 1); | |
1194 | usbg_cleanup_cmd(cmd); | |
c52661d6 SAS |
1195 | } |
1196 | ||
1197 | static int bot_submit_command(struct f_uas *fu, | |
1198 | void *cmdbuf, unsigned int len) | |
1199 | { | |
1200 | struct bulk_cb_wrap *cbw = cmdbuf; | |
1201 | struct usbg_cmd *cmd; | |
1202 | struct usbg_tpg *tpg; | |
1203 | struct se_cmd *se_cmd; | |
1204 | struct tcm_usbg_nexus *tv_nexus; | |
1205 | u32 cmd_len; | |
1206 | int ret; | |
1207 | ||
1208 | if (cbw->Signature != cpu_to_le32(US_BULK_CB_SIGN)) { | |
1209 | pr_err("Wrong signature on CBW\n"); | |
1210 | return -EINVAL; | |
1211 | } | |
1212 | if (len != 31) { | |
1213 | pr_err("Wrong length for CBW\n"); | |
1214 | return -EINVAL; | |
1215 | } | |
1216 | ||
1217 | cmd_len = cbw->Length; | |
1218 | if (cmd_len < 1 || cmd_len > 16) | |
1219 | return -EINVAL; | |
1220 | ||
1221 | cmd = kzalloc(sizeof *cmd, GFP_ATOMIC); | |
1222 | if (!cmd) | |
1223 | return -ENOMEM; | |
1224 | ||
1225 | cmd->fu = fu; | |
1226 | ||
1227 | /* XXX until I figure out why I can't free in on complete */ | |
1228 | kref_init(&cmd->ref); | |
1229 | kref_get(&cmd->ref); | |
1230 | ||
1231 | tpg = fu->tpg; | |
1232 | ||
1233 | memcpy(cmd->cmd_buf, cbw->CDB, cmd_len); | |
1234 | ||
1235 | cmd->bot_tag = cbw->Tag; | |
1236 | ||
1237 | tv_nexus = tpg->tpg_nexus; | |
1238 | if (!tv_nexus) { | |
1239 | pr_err("Missing nexus, ignoring command\n"); | |
1240 | goto err; | |
1241 | } | |
1242 | ||
1243 | cmd->prio_attr = MSG_SIMPLE_TAG; | |
1244 | se_cmd = &cmd->se_cmd; | |
1245 | cmd->unpacked_lun = cbw->Lun; | |
1246 | cmd->is_read = cbw->Flags & US_BULK_FLAG_IN ? 1 : 0; | |
1247 | cmd->data_len = le32_to_cpu(cbw->DataTransferLength); | |
1248 | ||
1249 | INIT_WORK(&cmd->work, bot_cmd_work); | |
1250 | ret = queue_work(tpg->workqueue, &cmd->work); | |
1251 | if (ret < 0) | |
1252 | goto err; | |
1253 | ||
1254 | return 0; | |
1255 | err: | |
1256 | kfree(cmd); | |
1257 | return -EINVAL; | |
1258 | } | |
1259 | ||
1260 | /* Start fabric.c code */ | |
1261 | ||
1262 | static int usbg_check_true(struct se_portal_group *se_tpg) | |
1263 | { | |
1264 | return 1; | |
1265 | } | |
1266 | ||
1267 | static int usbg_check_false(struct se_portal_group *se_tpg) | |
1268 | { | |
1269 | return 0; | |
1270 | } | |
1271 | ||
1272 | static char *usbg_get_fabric_name(void) | |
1273 | { | |
1274 | return "usb_gadget"; | |
1275 | } | |
1276 | ||
1277 | static u8 usbg_get_fabric_proto_ident(struct se_portal_group *se_tpg) | |
1278 | { | |
1279 | struct usbg_tpg *tpg = container_of(se_tpg, | |
1280 | struct usbg_tpg, se_tpg); | |
1281 | struct usbg_tport *tport = tpg->tport; | |
1282 | u8 proto_id; | |
1283 | ||
1284 | switch (tport->tport_proto_id) { | |
1285 | case SCSI_PROTOCOL_SAS: | |
1286 | default: | |
1287 | proto_id = sas_get_fabric_proto_ident(se_tpg); | |
1288 | break; | |
1289 | } | |
1290 | ||
1291 | return proto_id; | |
1292 | } | |
1293 | ||
1294 | static char *usbg_get_fabric_wwn(struct se_portal_group *se_tpg) | |
1295 | { | |
1296 | struct usbg_tpg *tpg = container_of(se_tpg, | |
1297 | struct usbg_tpg, se_tpg); | |
1298 | struct usbg_tport *tport = tpg->tport; | |
1299 | ||
1300 | return &tport->tport_name[0]; | |
1301 | } | |
1302 | ||
1303 | static u16 usbg_get_tag(struct se_portal_group *se_tpg) | |
1304 | { | |
1305 | struct usbg_tpg *tpg = container_of(se_tpg, | |
1306 | struct usbg_tpg, se_tpg); | |
1307 | return tpg->tport_tpgt; | |
1308 | } | |
1309 | ||
1310 | static u32 usbg_get_default_depth(struct se_portal_group *se_tpg) | |
1311 | { | |
1312 | return 1; | |
1313 | } | |
1314 | ||
1315 | static u32 usbg_get_pr_transport_id( | |
1316 | struct se_portal_group *se_tpg, | |
1317 | struct se_node_acl *se_nacl, | |
1318 | struct t10_pr_registration *pr_reg, | |
1319 | int *format_code, | |
1320 | unsigned char *buf) | |
1321 | { | |
1322 | struct usbg_tpg *tpg = container_of(se_tpg, | |
1323 | struct usbg_tpg, se_tpg); | |
1324 | struct usbg_tport *tport = tpg->tport; | |
1325 | int ret = 0; | |
1326 | ||
1327 | switch (tport->tport_proto_id) { | |
1328 | case SCSI_PROTOCOL_SAS: | |
1329 | default: | |
1330 | ret = sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, | |
1331 | format_code, buf); | |
1332 | break; | |
1333 | } | |
1334 | ||
1335 | return ret; | |
1336 | } | |
1337 | ||
1338 | static u32 usbg_get_pr_transport_id_len( | |
1339 | struct se_portal_group *se_tpg, | |
1340 | struct se_node_acl *se_nacl, | |
1341 | struct t10_pr_registration *pr_reg, | |
1342 | int *format_code) | |
1343 | { | |
1344 | struct usbg_tpg *tpg = container_of(se_tpg, | |
1345 | struct usbg_tpg, se_tpg); | |
1346 | struct usbg_tport *tport = tpg->tport; | |
1347 | int ret = 0; | |
1348 | ||
1349 | switch (tport->tport_proto_id) { | |
1350 | case SCSI_PROTOCOL_SAS: | |
1351 | default: | |
1352 | ret = sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, | |
1353 | format_code); | |
1354 | break; | |
1355 | } | |
1356 | ||
1357 | return ret; | |
1358 | } | |
1359 | ||
1360 | static char *usbg_parse_pr_out_transport_id( | |
1361 | struct se_portal_group *se_tpg, | |
1362 | const char *buf, | |
1363 | u32 *out_tid_len, | |
1364 | char **port_nexus_ptr) | |
1365 | { | |
1366 | struct usbg_tpg *tpg = container_of(se_tpg, | |
1367 | struct usbg_tpg, se_tpg); | |
1368 | struct usbg_tport *tport = tpg->tport; | |
1369 | char *tid = NULL; | |
1370 | ||
1371 | switch (tport->tport_proto_id) { | |
1372 | case SCSI_PROTOCOL_SAS: | |
1373 | default: | |
1374 | tid = sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, | |
1375 | port_nexus_ptr); | |
1376 | } | |
1377 | ||
1378 | return tid; | |
1379 | } | |
1380 | ||
1381 | static struct se_node_acl *usbg_alloc_fabric_acl(struct se_portal_group *se_tpg) | |
1382 | { | |
1383 | struct usbg_nacl *nacl; | |
1384 | ||
1385 | nacl = kzalloc(sizeof(struct usbg_nacl), GFP_KERNEL); | |
1386 | if (!nacl) { | |
984e833c | 1387 | printk(KERN_ERR "Unable to allocate struct usbg_nacl\n"); |
c52661d6 SAS |
1388 | return NULL; |
1389 | } | |
1390 | ||
1391 | return &nacl->se_node_acl; | |
1392 | } | |
1393 | ||
1394 | static void usbg_release_fabric_acl( | |
1395 | struct se_portal_group *se_tpg, | |
1396 | struct se_node_acl *se_nacl) | |
1397 | { | |
1398 | struct usbg_nacl *nacl = container_of(se_nacl, | |
1399 | struct usbg_nacl, se_node_acl); | |
1400 | kfree(nacl); | |
1401 | } | |
1402 | ||
1403 | static u32 usbg_tpg_get_inst_index(struct se_portal_group *se_tpg) | |
1404 | { | |
1405 | return 1; | |
1406 | } | |
1407 | ||
c52661d6 SAS |
1408 | static void usbg_cmd_release(struct kref *ref) |
1409 | { | |
1410 | struct usbg_cmd *cmd = container_of(ref, struct usbg_cmd, | |
1411 | ref); | |
1412 | ||
1413 | transport_generic_free_cmd(&cmd->se_cmd, 0); | |
1414 | } | |
1415 | ||
1416 | static void usbg_release_cmd(struct se_cmd *se_cmd) | |
1417 | { | |
1418 | struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd, | |
1419 | se_cmd); | |
1420 | kfree(cmd->data_buf); | |
1421 | kfree(cmd); | |
1422 | return; | |
1423 | } | |
1424 | ||
1425 | static int usbg_shutdown_session(struct se_session *se_sess) | |
1426 | { | |
1427 | return 0; | |
1428 | } | |
1429 | ||
1430 | static void usbg_close_session(struct se_session *se_sess) | |
1431 | { | |
1432 | return; | |
1433 | } | |
1434 | ||
1435 | static u32 usbg_sess_get_index(struct se_session *se_sess) | |
1436 | { | |
1437 | return 0; | |
1438 | } | |
1439 | ||
1440 | /* | |
1441 | * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be | |
1442 | */ | |
1443 | static int usbg_write_pending_status(struct se_cmd *se_cmd) | |
1444 | { | |
1445 | return 0; | |
1446 | } | |
1447 | ||
1448 | static void usbg_set_default_node_attrs(struct se_node_acl *nacl) | |
1449 | { | |
1450 | return; | |
1451 | } | |
1452 | ||
1453 | static u32 usbg_get_task_tag(struct se_cmd *se_cmd) | |
1454 | { | |
1455 | struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd, | |
1456 | se_cmd); | |
1457 | struct f_uas *fu = cmd->fu; | |
1458 | ||
1459 | if (fu->flags & USBG_IS_BOT) | |
1460 | return le32_to_cpu(cmd->bot_tag); | |
1461 | else | |
1462 | return cmd->tag; | |
1463 | } | |
1464 | ||
1465 | static int usbg_get_cmd_state(struct se_cmd *se_cmd) | |
1466 | { | |
1467 | return 0; | |
1468 | } | |
1469 | ||
1470 | static int usbg_queue_tm_rsp(struct se_cmd *se_cmd) | |
1471 | { | |
1472 | return 0; | |
1473 | } | |
1474 | ||
c52661d6 SAS |
1475 | static const char *usbg_check_wwn(const char *name) |
1476 | { | |
1477 | const char *n; | |
1478 | unsigned int len; | |
1479 | ||
1480 | n = strstr(name, "naa."); | |
1481 | if (!n) | |
1482 | return NULL; | |
1483 | n += 4; | |
1484 | len = strlen(n); | |
1485 | if (len == 0 || len > USBG_NAMELEN - 1) | |
1486 | return NULL; | |
1487 | return n; | |
1488 | } | |
1489 | ||
1490 | static struct se_node_acl *usbg_make_nodeacl( | |
1491 | struct se_portal_group *se_tpg, | |
1492 | struct config_group *group, | |
1493 | const char *name) | |
1494 | { | |
1495 | struct se_node_acl *se_nacl, *se_nacl_new; | |
1496 | struct usbg_nacl *nacl; | |
1497 | u64 wwpn = 0; | |
1498 | u32 nexus_depth; | |
1499 | const char *wnn_name; | |
1500 | ||
1501 | wnn_name = usbg_check_wwn(name); | |
1502 | if (!wnn_name) | |
1503 | return ERR_PTR(-EINVAL); | |
1504 | se_nacl_new = usbg_alloc_fabric_acl(se_tpg); | |
1505 | if (!(se_nacl_new)) | |
1506 | return ERR_PTR(-ENOMEM); | |
1507 | ||
1508 | nexus_depth = 1; | |
1509 | /* | |
1510 | * se_nacl_new may be released by core_tpg_add_initiator_node_acl() | |
1511 | * when converting a NodeACL from demo mode -> explict | |
1512 | */ | |
1513 | se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new, | |
1514 | name, nexus_depth); | |
1515 | if (IS_ERR(se_nacl)) { | |
1516 | usbg_release_fabric_acl(se_tpg, se_nacl_new); | |
1517 | return se_nacl; | |
1518 | } | |
1519 | /* | |
1520 | * Locate our struct usbg_nacl and set the FC Nport WWPN | |
1521 | */ | |
1522 | nacl = container_of(se_nacl, struct usbg_nacl, se_node_acl); | |
1523 | nacl->iport_wwpn = wwpn; | |
1524 | snprintf(nacl->iport_name, sizeof(nacl->iport_name), "%s", name); | |
1525 | return se_nacl; | |
1526 | } | |
1527 | ||
1528 | static void usbg_drop_nodeacl(struct se_node_acl *se_acl) | |
1529 | { | |
1530 | struct usbg_nacl *nacl = container_of(se_acl, | |
1531 | struct usbg_nacl, se_node_acl); | |
1532 | core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1); | |
1533 | kfree(nacl); | |
1534 | } | |
1535 | ||
1536 | struct usbg_tpg *the_only_tpg_I_currently_have; | |
1537 | ||
1538 | static struct se_portal_group *usbg_make_tpg( | |
1539 | struct se_wwn *wwn, | |
1540 | struct config_group *group, | |
1541 | const char *name) | |
1542 | { | |
1543 | struct usbg_tport *tport = container_of(wwn, struct usbg_tport, | |
1544 | tport_wwn); | |
1545 | struct usbg_tpg *tpg; | |
1546 | unsigned long tpgt; | |
1547 | int ret; | |
1548 | ||
1549 | if (strstr(name, "tpgt_") != name) | |
1550 | return ERR_PTR(-EINVAL); | |
1551 | if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX) | |
1552 | return ERR_PTR(-EINVAL); | |
1553 | if (the_only_tpg_I_currently_have) { | |
1554 | pr_err("Until the gadget framework can't handle multiple\n"); | |
1555 | pr_err("gadgets, you can't do this here.\n"); | |
1556 | return ERR_PTR(-EBUSY); | |
1557 | } | |
1558 | ||
1559 | tpg = kzalloc(sizeof(struct usbg_tpg), GFP_KERNEL); | |
1560 | if (!tpg) { | |
1561 | printk(KERN_ERR "Unable to allocate struct usbg_tpg"); | |
1562 | return ERR_PTR(-ENOMEM); | |
1563 | } | |
1564 | mutex_init(&tpg->tpg_mutex); | |
1565 | atomic_set(&tpg->tpg_port_count, 0); | |
1566 | tpg->workqueue = alloc_workqueue("tcm_usb_gadget", 0, 1); | |
1567 | if (!tpg->workqueue) { | |
1568 | kfree(tpg); | |
1569 | return NULL; | |
1570 | } | |
1571 | ||
1572 | tpg->tport = tport; | |
1573 | tpg->tport_tpgt = tpgt; | |
1574 | ||
1575 | ret = core_tpg_register(&usbg_fabric_configfs->tf_ops, wwn, | |
1576 | &tpg->se_tpg, tpg, | |
1577 | TRANSPORT_TPG_TYPE_NORMAL); | |
1578 | if (ret < 0) { | |
1579 | destroy_workqueue(tpg->workqueue); | |
1580 | kfree(tpg); | |
1581 | return NULL; | |
1582 | } | |
1583 | the_only_tpg_I_currently_have = tpg; | |
1584 | return &tpg->se_tpg; | |
1585 | } | |
1586 | ||
1587 | static void usbg_drop_tpg(struct se_portal_group *se_tpg) | |
1588 | { | |
1589 | struct usbg_tpg *tpg = container_of(se_tpg, | |
1590 | struct usbg_tpg, se_tpg); | |
1591 | ||
1592 | core_tpg_deregister(se_tpg); | |
1593 | destroy_workqueue(tpg->workqueue); | |
1594 | kfree(tpg); | |
1595 | the_only_tpg_I_currently_have = NULL; | |
1596 | } | |
1597 | ||
1598 | static struct se_wwn *usbg_make_tport( | |
1599 | struct target_fabric_configfs *tf, | |
1600 | struct config_group *group, | |
1601 | const char *name) | |
1602 | { | |
1603 | struct usbg_tport *tport; | |
1604 | const char *wnn_name; | |
1605 | u64 wwpn = 0; | |
1606 | ||
1607 | wnn_name = usbg_check_wwn(name); | |
1608 | if (!wnn_name) | |
1609 | return ERR_PTR(-EINVAL); | |
1610 | ||
1611 | tport = kzalloc(sizeof(struct usbg_tport), GFP_KERNEL); | |
1612 | if (!(tport)) { | |
1613 | printk(KERN_ERR "Unable to allocate struct usbg_tport"); | |
1614 | return ERR_PTR(-ENOMEM); | |
1615 | } | |
1616 | tport->tport_wwpn = wwpn; | |
1617 | snprintf(tport->tport_name, sizeof(tport->tport_name), wnn_name); | |
1618 | return &tport->tport_wwn; | |
1619 | } | |
1620 | ||
1621 | static void usbg_drop_tport(struct se_wwn *wwn) | |
1622 | { | |
1623 | struct usbg_tport *tport = container_of(wwn, | |
1624 | struct usbg_tport, tport_wwn); | |
1625 | kfree(tport); | |
1626 | } | |
1627 | ||
1628 | /* | |
1629 | * If somebody feels like dropping the version property, go ahead. | |
1630 | */ | |
1631 | static ssize_t usbg_wwn_show_attr_version( | |
1632 | struct target_fabric_configfs *tf, | |
1633 | char *page) | |
1634 | { | |
1635 | return sprintf(page, "usb-gadget fabric module\n"); | |
1636 | } | |
1637 | TF_WWN_ATTR_RO(usbg, version); | |
1638 | ||
1639 | static struct configfs_attribute *usbg_wwn_attrs[] = { | |
1640 | &usbg_wwn_version.attr, | |
1641 | NULL, | |
1642 | }; | |
1643 | ||
1644 | static ssize_t tcm_usbg_tpg_show_enable( | |
1645 | struct se_portal_group *se_tpg, | |
1646 | char *page) | |
1647 | { | |
1648 | struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg); | |
1649 | ||
1650 | return snprintf(page, PAGE_SIZE, "%u\n", tpg->gadget_connect); | |
1651 | } | |
1652 | ||
1653 | static int usbg_attach(struct usbg_tpg *); | |
1654 | static void usbg_detach(struct usbg_tpg *); | |
1655 | ||
1656 | static ssize_t tcm_usbg_tpg_store_enable( | |
1657 | struct se_portal_group *se_tpg, | |
1658 | const char *page, | |
1659 | size_t count) | |
1660 | { | |
1661 | struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg); | |
1662 | unsigned long op; | |
1663 | ssize_t ret; | |
1664 | ||
1665 | ret = kstrtoul(page, 0, &op); | |
1666 | if (ret < 0) | |
1667 | return -EINVAL; | |
1668 | if (op > 1) | |
1669 | return -EINVAL; | |
1670 | ||
1671 | if (op && tpg->gadget_connect) | |
1672 | goto out; | |
1673 | if (!op && !tpg->gadget_connect) | |
1674 | goto out; | |
1675 | ||
1676 | if (op) { | |
1677 | ret = usbg_attach(tpg); | |
1678 | if (ret) | |
1679 | goto out; | |
1680 | } else { | |
1681 | usbg_detach(tpg); | |
1682 | } | |
1683 | tpg->gadget_connect = op; | |
1684 | out: | |
1685 | return count; | |
1686 | } | |
1687 | TF_TPG_BASE_ATTR(tcm_usbg, enable, S_IRUGO | S_IWUSR); | |
1688 | ||
1689 | static ssize_t tcm_usbg_tpg_show_nexus( | |
1690 | struct se_portal_group *se_tpg, | |
1691 | char *page) | |
1692 | { | |
1693 | struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg); | |
1694 | struct tcm_usbg_nexus *tv_nexus; | |
1695 | ssize_t ret; | |
1696 | ||
1697 | mutex_lock(&tpg->tpg_mutex); | |
1698 | tv_nexus = tpg->tpg_nexus; | |
1699 | if (!tv_nexus) { | |
1700 | ret = -ENODEV; | |
1701 | goto out; | |
1702 | } | |
1703 | ret = snprintf(page, PAGE_SIZE, "%s\n", | |
1704 | tv_nexus->tvn_se_sess->se_node_acl->initiatorname); | |
1705 | out: | |
1706 | mutex_unlock(&tpg->tpg_mutex); | |
1707 | return ret; | |
1708 | } | |
1709 | ||
1710 | static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name) | |
1711 | { | |
1712 | struct se_portal_group *se_tpg; | |
1713 | struct tcm_usbg_nexus *tv_nexus; | |
1714 | int ret; | |
1715 | ||
1716 | mutex_lock(&tpg->tpg_mutex); | |
1717 | if (tpg->tpg_nexus) { | |
1718 | ret = -EEXIST; | |
1719 | pr_debug("tpg->tpg_nexus already exists\n"); | |
1720 | goto err_unlock; | |
1721 | } | |
1722 | se_tpg = &tpg->se_tpg; | |
1723 | ||
1724 | ret = -ENOMEM; | |
1725 | tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL); | |
1726 | if (!tv_nexus) { | |
1727 | pr_err("Unable to allocate struct tcm_vhost_nexus\n"); | |
1728 | goto err_unlock; | |
1729 | } | |
1730 | tv_nexus->tvn_se_sess = transport_init_session(); | |
1731 | if (IS_ERR(tv_nexus->tvn_se_sess)) | |
1732 | goto err_free; | |
1733 | ||
1734 | /* | |
1735 | * Since we are running in 'demo mode' this call with generate a | |
1736 | * struct se_node_acl for the tcm_vhost struct se_portal_group with | |
1737 | * the SCSI Initiator port name of the passed configfs group 'name'. | |
1738 | */ | |
1739 | tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl( | |
1740 | se_tpg, name); | |
1741 | if (!tv_nexus->tvn_se_sess->se_node_acl) { | |
1742 | pr_debug("core_tpg_check_initiator_node_acl() failed" | |
1743 | " for %s\n", name); | |
1744 | goto err_session; | |
1745 | } | |
1746 | /* | |
1747 | * Now register the TCM vHost virtual I_T Nexus as active with the | |
1748 | * call to __transport_register_session() | |
1749 | */ | |
1750 | __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl, | |
1751 | tv_nexus->tvn_se_sess, tv_nexus); | |
1752 | tpg->tpg_nexus = tv_nexus; | |
1753 | mutex_unlock(&tpg->tpg_mutex); | |
1754 | return 0; | |
1755 | ||
1756 | err_session: | |
1757 | transport_free_session(tv_nexus->tvn_se_sess); | |
1758 | err_free: | |
1759 | kfree(tv_nexus); | |
1760 | err_unlock: | |
1761 | mutex_unlock(&tpg->tpg_mutex); | |
1762 | return ret; | |
1763 | } | |
1764 | ||
1765 | static int tcm_usbg_drop_nexus(struct usbg_tpg *tpg) | |
1766 | { | |
1767 | struct se_session *se_sess; | |
1768 | struct tcm_usbg_nexus *tv_nexus; | |
1769 | int ret = -ENODEV; | |
1770 | ||
1771 | mutex_lock(&tpg->tpg_mutex); | |
1772 | tv_nexus = tpg->tpg_nexus; | |
1773 | if (!tv_nexus) | |
1774 | goto out; | |
1775 | ||
1776 | se_sess = tv_nexus->tvn_se_sess; | |
1777 | if (!se_sess) | |
1778 | goto out; | |
1779 | ||
1780 | if (atomic_read(&tpg->tpg_port_count)) { | |
1781 | ret = -EPERM; | |
1782 | pr_err("Unable to remove Host I_T Nexus with" | |
1783 | " active TPG port count: %d\n", | |
1784 | atomic_read(&tpg->tpg_port_count)); | |
1785 | goto out; | |
1786 | } | |
1787 | ||
1788 | pr_debug("Removing I_T Nexus to Initiator Port: %s\n", | |
1789 | tv_nexus->tvn_se_sess->se_node_acl->initiatorname); | |
1790 | /* | |
1791 | * Release the SCSI I_T Nexus to the emulated vHost Target Port | |
1792 | */ | |
1793 | transport_deregister_session(tv_nexus->tvn_se_sess); | |
1794 | tpg->tpg_nexus = NULL; | |
1795 | ||
1796 | kfree(tv_nexus); | |
5dbd6935 | 1797 | ret = 0; |
c52661d6 SAS |
1798 | out: |
1799 | mutex_unlock(&tpg->tpg_mutex); | |
5dbd6935 | 1800 | return ret; |
c52661d6 SAS |
1801 | } |
1802 | ||
1803 | static ssize_t tcm_usbg_tpg_store_nexus( | |
1804 | struct se_portal_group *se_tpg, | |
1805 | const char *page, | |
1806 | size_t count) | |
1807 | { | |
1808 | struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg); | |
1809 | unsigned char i_port[USBG_NAMELEN], *ptr; | |
1810 | int ret; | |
1811 | ||
1812 | if (!strncmp(page, "NULL", 4)) { | |
1813 | ret = tcm_usbg_drop_nexus(tpg); | |
1814 | return (!ret) ? count : ret; | |
1815 | } | |
232ebe34 | 1816 | if (strlen(page) >= USBG_NAMELEN) { |
c52661d6 SAS |
1817 | pr_err("Emulated NAA Sas Address: %s, exceeds" |
1818 | " max: %d\n", page, USBG_NAMELEN); | |
1819 | return -EINVAL; | |
1820 | } | |
1821 | snprintf(i_port, USBG_NAMELEN, "%s", page); | |
1822 | ||
1823 | ptr = strstr(i_port, "naa."); | |
1824 | if (!ptr) { | |
1825 | pr_err("Missing 'naa.' prefix\n"); | |
1826 | return -EINVAL; | |
1827 | } | |
1828 | ||
1829 | if (i_port[strlen(i_port) - 1] == '\n') | |
1830 | i_port[strlen(i_port) - 1] = '\0'; | |
1831 | ||
1832 | ret = tcm_usbg_make_nexus(tpg, &i_port[4]); | |
1833 | if (ret < 0) | |
1834 | return ret; | |
1835 | return count; | |
1836 | } | |
1837 | TF_TPG_BASE_ATTR(tcm_usbg, nexus, S_IRUGO | S_IWUSR); | |
1838 | ||
1839 | static struct configfs_attribute *usbg_base_attrs[] = { | |
1840 | &tcm_usbg_tpg_enable.attr, | |
1841 | &tcm_usbg_tpg_nexus.attr, | |
1842 | NULL, | |
1843 | }; | |
1844 | ||
1845 | static int usbg_port_link(struct se_portal_group *se_tpg, struct se_lun *lun) | |
1846 | { | |
1847 | struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg); | |
1848 | ||
1849 | atomic_inc(&tpg->tpg_port_count); | |
1850 | smp_mb__after_atomic_inc(); | |
1851 | return 0; | |
1852 | } | |
1853 | ||
1854 | static void usbg_port_unlink(struct se_portal_group *se_tpg, | |
1855 | struct se_lun *se_lun) | |
1856 | { | |
1857 | struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg); | |
1858 | ||
1859 | atomic_dec(&tpg->tpg_port_count); | |
1860 | smp_mb__after_atomic_dec(); | |
1861 | } | |
1862 | ||
1863 | static int usbg_check_stop_free(struct se_cmd *se_cmd) | |
1864 | { | |
1865 | struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd, | |
1866 | se_cmd); | |
1867 | ||
1868 | kref_put(&cmd->ref, usbg_cmd_release); | |
1869 | return 1; | |
1870 | } | |
1871 | ||
1872 | static struct target_core_fabric_ops usbg_ops = { | |
1873 | .get_fabric_name = usbg_get_fabric_name, | |
1874 | .get_fabric_proto_ident = usbg_get_fabric_proto_ident, | |
1875 | .tpg_get_wwn = usbg_get_fabric_wwn, | |
1876 | .tpg_get_tag = usbg_get_tag, | |
1877 | .tpg_get_default_depth = usbg_get_default_depth, | |
1878 | .tpg_get_pr_transport_id = usbg_get_pr_transport_id, | |
1879 | .tpg_get_pr_transport_id_len = usbg_get_pr_transport_id_len, | |
1880 | .tpg_parse_pr_out_transport_id = usbg_parse_pr_out_transport_id, | |
1881 | .tpg_check_demo_mode = usbg_check_true, | |
1882 | .tpg_check_demo_mode_cache = usbg_check_false, | |
1883 | .tpg_check_demo_mode_write_protect = usbg_check_false, | |
1884 | .tpg_check_prod_mode_write_protect = usbg_check_false, | |
1885 | .tpg_alloc_fabric_acl = usbg_alloc_fabric_acl, | |
1886 | .tpg_release_fabric_acl = usbg_release_fabric_acl, | |
1887 | .tpg_get_inst_index = usbg_tpg_get_inst_index, | |
c52661d6 SAS |
1888 | .release_cmd = usbg_release_cmd, |
1889 | .shutdown_session = usbg_shutdown_session, | |
1890 | .close_session = usbg_close_session, | |
1891 | .sess_get_index = usbg_sess_get_index, | |
1892 | .sess_get_initiator_sid = NULL, | |
1893 | .write_pending = usbg_send_write_request, | |
1894 | .write_pending_status = usbg_write_pending_status, | |
1895 | .set_default_node_attributes = usbg_set_default_node_attrs, | |
1896 | .get_task_tag = usbg_get_task_tag, | |
1897 | .get_cmd_state = usbg_get_cmd_state, | |
1898 | .queue_data_in = usbg_send_read_response, | |
1899 | .queue_status = usbg_send_status_response, | |
1900 | .queue_tm_rsp = usbg_queue_tm_rsp, | |
c52661d6 SAS |
1901 | .check_stop_free = usbg_check_stop_free, |
1902 | ||
1903 | .fabric_make_wwn = usbg_make_tport, | |
1904 | .fabric_drop_wwn = usbg_drop_tport, | |
1905 | .fabric_make_tpg = usbg_make_tpg, | |
1906 | .fabric_drop_tpg = usbg_drop_tpg, | |
1907 | .fabric_post_link = usbg_port_link, | |
1908 | .fabric_pre_unlink = usbg_port_unlink, | |
1909 | .fabric_make_np = NULL, | |
1910 | .fabric_drop_np = NULL, | |
1911 | .fabric_make_nodeacl = usbg_make_nodeacl, | |
1912 | .fabric_drop_nodeacl = usbg_drop_nodeacl, | |
1913 | }; | |
1914 | ||
1915 | static int usbg_register_configfs(void) | |
1916 | { | |
1917 | struct target_fabric_configfs *fabric; | |
1918 | int ret; | |
1919 | ||
1920 | fabric = target_fabric_configfs_init(THIS_MODULE, "usb_gadget"); | |
1921 | if (IS_ERR(fabric)) { | |
1922 | printk(KERN_ERR "target_fabric_configfs_init() failed\n"); | |
1923 | return PTR_ERR(fabric); | |
1924 | } | |
1925 | ||
1926 | fabric->tf_ops = usbg_ops; | |
1927 | TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = usbg_wwn_attrs; | |
1928 | TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = usbg_base_attrs; | |
1929 | TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL; | |
1930 | TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL; | |
1931 | TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL; | |
1932 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL; | |
1933 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL; | |
1934 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL; | |
1935 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL; | |
1936 | ret = target_fabric_configfs_register(fabric); | |
1937 | if (ret < 0) { | |
1938 | printk(KERN_ERR "target_fabric_configfs_register() failed" | |
1939 | " for usb-gadget\n"); | |
1940 | return ret; | |
1941 | } | |
1942 | usbg_fabric_configfs = fabric; | |
1943 | return 0; | |
1944 | }; | |
1945 | ||
1946 | static void usbg_deregister_configfs(void) | |
1947 | { | |
1948 | if (!(usbg_fabric_configfs)) | |
1949 | return; | |
1950 | ||
1951 | target_fabric_configfs_deregister(usbg_fabric_configfs); | |
1952 | usbg_fabric_configfs = NULL; | |
1953 | }; | |
1954 | ||
1955 | /* Start gadget.c code */ | |
1956 | ||
1957 | static struct usb_interface_descriptor bot_intf_desc = { | |
1958 | .bLength = sizeof(bot_intf_desc), | |
1959 | .bDescriptorType = USB_DT_INTERFACE, | |
c52661d6 SAS |
1960 | .bNumEndpoints = 2, |
1961 | .bAlternateSetting = USB_G_ALT_INT_BBB, | |
1962 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, | |
1963 | .bInterfaceSubClass = USB_SC_SCSI, | |
1964 | .bInterfaceProtocol = USB_PR_BULK, | |
c52661d6 SAS |
1965 | }; |
1966 | ||
1967 | static struct usb_interface_descriptor uasp_intf_desc = { | |
1968 | .bLength = sizeof(uasp_intf_desc), | |
1969 | .bDescriptorType = USB_DT_INTERFACE, | |
1970 | .bNumEndpoints = 4, | |
1971 | .bAlternateSetting = USB_G_ALT_INT_UAS, | |
1972 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, | |
1973 | .bInterfaceSubClass = USB_SC_SCSI, | |
1974 | .bInterfaceProtocol = USB_PR_UAS, | |
c52661d6 SAS |
1975 | }; |
1976 | ||
1977 | static struct usb_endpoint_descriptor uasp_bi_desc = { | |
1978 | .bLength = USB_DT_ENDPOINT_SIZE, | |
1979 | .bDescriptorType = USB_DT_ENDPOINT, | |
1980 | .bEndpointAddress = USB_DIR_IN, | |
1981 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
1982 | .wMaxPacketSize = cpu_to_le16(512), | |
1983 | }; | |
1984 | ||
1985 | static struct usb_endpoint_descriptor uasp_fs_bi_desc = { | |
1986 | .bLength = USB_DT_ENDPOINT_SIZE, | |
1987 | .bDescriptorType = USB_DT_ENDPOINT, | |
1988 | .bEndpointAddress = USB_DIR_IN, | |
1989 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
1990 | }; | |
1991 | ||
1992 | static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc = { | |
1993 | .bLength = sizeof(uasp_bi_pipe_desc), | |
1994 | .bDescriptorType = USB_DT_PIPE_USAGE, | |
1995 | .bPipeID = DATA_IN_PIPE_ID, | |
1996 | }; | |
1997 | ||
1998 | static struct usb_endpoint_descriptor uasp_ss_bi_desc = { | |
1999 | .bLength = USB_DT_ENDPOINT_SIZE, | |
2000 | .bDescriptorType = USB_DT_ENDPOINT, | |
2001 | .bEndpointAddress = USB_DIR_IN, | |
2002 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
2003 | .wMaxPacketSize = cpu_to_le16(1024), | |
2004 | }; | |
2005 | ||
2006 | static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc = { | |
2007 | .bLength = sizeof(uasp_bi_ep_comp_desc), | |
2008 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
2009 | .bMaxBurst = 0, | |
2010 | .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS, | |
2011 | .wBytesPerInterval = 0, | |
2012 | }; | |
2013 | ||
2014 | static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc = { | |
2015 | .bLength = sizeof(bot_bi_ep_comp_desc), | |
2016 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
2017 | .bMaxBurst = 0, | |
2018 | }; | |
2019 | ||
2020 | static struct usb_endpoint_descriptor uasp_bo_desc = { | |
2021 | .bLength = USB_DT_ENDPOINT_SIZE, | |
2022 | .bDescriptorType = USB_DT_ENDPOINT, | |
2023 | .bEndpointAddress = USB_DIR_OUT, | |
2024 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
2025 | .wMaxPacketSize = cpu_to_le16(512), | |
2026 | }; | |
2027 | ||
2028 | static struct usb_endpoint_descriptor uasp_fs_bo_desc = { | |
2029 | .bLength = USB_DT_ENDPOINT_SIZE, | |
2030 | .bDescriptorType = USB_DT_ENDPOINT, | |
2031 | .bEndpointAddress = USB_DIR_OUT, | |
2032 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
2033 | }; | |
2034 | ||
2035 | static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc = { | |
2036 | .bLength = sizeof(uasp_bo_pipe_desc), | |
2037 | .bDescriptorType = USB_DT_PIPE_USAGE, | |
2038 | .bPipeID = DATA_OUT_PIPE_ID, | |
2039 | }; | |
2040 | ||
2041 | static struct usb_endpoint_descriptor uasp_ss_bo_desc = { | |
2042 | .bLength = USB_DT_ENDPOINT_SIZE, | |
2043 | .bDescriptorType = USB_DT_ENDPOINT, | |
2044 | .bEndpointAddress = USB_DIR_OUT, | |
2045 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
2046 | .wMaxPacketSize = cpu_to_le16(0x400), | |
2047 | }; | |
2048 | ||
2049 | static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc = { | |
2050 | .bLength = sizeof(uasp_bo_ep_comp_desc), | |
2051 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
2052 | .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS, | |
2053 | }; | |
2054 | ||
2055 | static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc = { | |
2056 | .bLength = sizeof(bot_bo_ep_comp_desc), | |
2057 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
2058 | }; | |
2059 | ||
2060 | static struct usb_endpoint_descriptor uasp_status_desc = { | |
2061 | .bLength = USB_DT_ENDPOINT_SIZE, | |
2062 | .bDescriptorType = USB_DT_ENDPOINT, | |
2063 | .bEndpointAddress = USB_DIR_IN, | |
2064 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
2065 | .wMaxPacketSize = cpu_to_le16(512), | |
2066 | }; | |
2067 | ||
2068 | static struct usb_endpoint_descriptor uasp_fs_status_desc = { | |
2069 | .bLength = USB_DT_ENDPOINT_SIZE, | |
2070 | .bDescriptorType = USB_DT_ENDPOINT, | |
2071 | .bEndpointAddress = USB_DIR_IN, | |
2072 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
2073 | }; | |
2074 | ||
2075 | static struct usb_pipe_usage_descriptor uasp_status_pipe_desc = { | |
2076 | .bLength = sizeof(uasp_status_pipe_desc), | |
2077 | .bDescriptorType = USB_DT_PIPE_USAGE, | |
2078 | .bPipeID = STATUS_PIPE_ID, | |
2079 | }; | |
2080 | ||
2081 | static struct usb_endpoint_descriptor uasp_ss_status_desc = { | |
2082 | .bLength = USB_DT_ENDPOINT_SIZE, | |
2083 | .bDescriptorType = USB_DT_ENDPOINT, | |
2084 | .bEndpointAddress = USB_DIR_IN, | |
2085 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
2086 | .wMaxPacketSize = cpu_to_le16(1024), | |
2087 | }; | |
2088 | ||
2089 | static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc = { | |
2090 | .bLength = sizeof(uasp_status_in_ep_comp_desc), | |
2091 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
2092 | .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS, | |
2093 | }; | |
2094 | ||
2095 | static struct usb_endpoint_descriptor uasp_cmd_desc = { | |
2096 | .bLength = USB_DT_ENDPOINT_SIZE, | |
2097 | .bDescriptorType = USB_DT_ENDPOINT, | |
2098 | .bEndpointAddress = USB_DIR_OUT, | |
2099 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
2100 | .wMaxPacketSize = cpu_to_le16(512), | |
2101 | }; | |
2102 | ||
2103 | static struct usb_endpoint_descriptor uasp_fs_cmd_desc = { | |
2104 | .bLength = USB_DT_ENDPOINT_SIZE, | |
2105 | .bDescriptorType = USB_DT_ENDPOINT, | |
2106 | .bEndpointAddress = USB_DIR_OUT, | |
2107 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
2108 | }; | |
2109 | ||
2110 | static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc = { | |
2111 | .bLength = sizeof(uasp_cmd_pipe_desc), | |
2112 | .bDescriptorType = USB_DT_PIPE_USAGE, | |
2113 | .bPipeID = CMD_PIPE_ID, | |
2114 | }; | |
2115 | ||
2116 | static struct usb_endpoint_descriptor uasp_ss_cmd_desc = { | |
2117 | .bLength = USB_DT_ENDPOINT_SIZE, | |
2118 | .bDescriptorType = USB_DT_ENDPOINT, | |
2119 | .bEndpointAddress = USB_DIR_OUT, | |
2120 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
2121 | .wMaxPacketSize = cpu_to_le16(1024), | |
2122 | }; | |
2123 | ||
2124 | static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc = { | |
2125 | .bLength = sizeof(uasp_cmd_comp_desc), | |
2126 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
2127 | }; | |
2128 | ||
2129 | static struct usb_descriptor_header *uasp_fs_function_desc[] = { | |
2130 | (struct usb_descriptor_header *) &bot_intf_desc, | |
2131 | (struct usb_descriptor_header *) &uasp_fs_bi_desc, | |
2132 | (struct usb_descriptor_header *) &uasp_fs_bo_desc, | |
2133 | ||
2134 | (struct usb_descriptor_header *) &uasp_intf_desc, | |
2135 | (struct usb_descriptor_header *) &uasp_fs_bi_desc, | |
2136 | (struct usb_descriptor_header *) &uasp_bi_pipe_desc, | |
2137 | (struct usb_descriptor_header *) &uasp_fs_bo_desc, | |
2138 | (struct usb_descriptor_header *) &uasp_bo_pipe_desc, | |
2139 | (struct usb_descriptor_header *) &uasp_fs_status_desc, | |
2140 | (struct usb_descriptor_header *) &uasp_status_pipe_desc, | |
2141 | (struct usb_descriptor_header *) &uasp_fs_cmd_desc, | |
2142 | (struct usb_descriptor_header *) &uasp_cmd_pipe_desc, | |
fad8deb2 | 2143 | NULL, |
c52661d6 SAS |
2144 | }; |
2145 | ||
2146 | static struct usb_descriptor_header *uasp_hs_function_desc[] = { | |
2147 | (struct usb_descriptor_header *) &bot_intf_desc, | |
2148 | (struct usb_descriptor_header *) &uasp_bi_desc, | |
2149 | (struct usb_descriptor_header *) &uasp_bo_desc, | |
2150 | ||
2151 | (struct usb_descriptor_header *) &uasp_intf_desc, | |
2152 | (struct usb_descriptor_header *) &uasp_bi_desc, | |
2153 | (struct usb_descriptor_header *) &uasp_bi_pipe_desc, | |
2154 | (struct usb_descriptor_header *) &uasp_bo_desc, | |
2155 | (struct usb_descriptor_header *) &uasp_bo_pipe_desc, | |
2156 | (struct usb_descriptor_header *) &uasp_status_desc, | |
2157 | (struct usb_descriptor_header *) &uasp_status_pipe_desc, | |
2158 | (struct usb_descriptor_header *) &uasp_cmd_desc, | |
2159 | (struct usb_descriptor_header *) &uasp_cmd_pipe_desc, | |
2160 | NULL, | |
2161 | }; | |
2162 | ||
2163 | static struct usb_descriptor_header *uasp_ss_function_desc[] = { | |
2164 | (struct usb_descriptor_header *) &bot_intf_desc, | |
2165 | (struct usb_descriptor_header *) &uasp_ss_bi_desc, | |
2166 | (struct usb_descriptor_header *) &bot_bi_ep_comp_desc, | |
2167 | (struct usb_descriptor_header *) &uasp_ss_bo_desc, | |
2168 | (struct usb_descriptor_header *) &bot_bo_ep_comp_desc, | |
2169 | ||
2170 | (struct usb_descriptor_header *) &uasp_intf_desc, | |
2171 | (struct usb_descriptor_header *) &uasp_ss_bi_desc, | |
2172 | (struct usb_descriptor_header *) &uasp_bi_ep_comp_desc, | |
2173 | (struct usb_descriptor_header *) &uasp_bi_pipe_desc, | |
2174 | (struct usb_descriptor_header *) &uasp_ss_bo_desc, | |
2175 | (struct usb_descriptor_header *) &uasp_bo_ep_comp_desc, | |
2176 | (struct usb_descriptor_header *) &uasp_bo_pipe_desc, | |
2177 | (struct usb_descriptor_header *) &uasp_ss_status_desc, | |
2178 | (struct usb_descriptor_header *) &uasp_status_in_ep_comp_desc, | |
2179 | (struct usb_descriptor_header *) &uasp_status_pipe_desc, | |
2180 | (struct usb_descriptor_header *) &uasp_ss_cmd_desc, | |
2181 | (struct usb_descriptor_header *) &uasp_cmd_comp_desc, | |
2182 | (struct usb_descriptor_header *) &uasp_cmd_pipe_desc, | |
2183 | NULL, | |
2184 | }; | |
2185 | ||
2186 | #define UAS_VENDOR_ID 0x0525 /* NetChip */ | |
2187 | #define UAS_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */ | |
2188 | ||
2189 | static struct usb_device_descriptor usbg_device_desc = { | |
2190 | .bLength = sizeof(usbg_device_desc), | |
2191 | .bDescriptorType = USB_DT_DEVICE, | |
2192 | .bcdUSB = cpu_to_le16(0x0200), | |
2193 | .bDeviceClass = USB_CLASS_PER_INTERFACE, | |
2194 | .idVendor = cpu_to_le16(UAS_VENDOR_ID), | |
2195 | .idProduct = cpu_to_le16(UAS_PRODUCT_ID), | |
c52661d6 SAS |
2196 | .bNumConfigurations = 1, |
2197 | }; | |
2198 | ||
2199 | static struct usb_string usbg_us_strings[] = { | |
276e2e4f SAS |
2200 | [USB_GADGET_MANUFACTURER_IDX].s = "Target Manufactor", |
2201 | [USB_GADGET_PRODUCT_IDX].s = "Target Product", | |
2202 | [USB_GADGET_SERIAL_IDX].s = "000000000001", | |
18786da4 SAS |
2203 | [USB_G_STR_CONFIG].s = "default config", |
2204 | [USB_G_STR_INT_UAS].s = "USB Attached SCSI", | |
2205 | [USB_G_STR_INT_BBB].s = "Bulk Only Transport", | |
c52661d6 SAS |
2206 | { }, |
2207 | }; | |
2208 | ||
2209 | static struct usb_gadget_strings usbg_stringtab = { | |
2210 | .language = 0x0409, | |
2211 | .strings = usbg_us_strings, | |
2212 | }; | |
2213 | ||
2214 | static struct usb_gadget_strings *usbg_strings[] = { | |
2215 | &usbg_stringtab, | |
2216 | NULL, | |
2217 | }; | |
2218 | ||
2219 | static int guas_unbind(struct usb_composite_dev *cdev) | |
2220 | { | |
2221 | return 0; | |
2222 | } | |
2223 | ||
2224 | static struct usb_configuration usbg_config_driver = { | |
2225 | .label = "Linux Target", | |
2226 | .bConfigurationValue = 1, | |
c52661d6 SAS |
2227 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
2228 | }; | |
2229 | ||
2230 | static void give_back_ep(struct usb_ep **pep) | |
2231 | { | |
2232 | struct usb_ep *ep = *pep; | |
2233 | if (!ep) | |
2234 | return; | |
2235 | ep->driver_data = NULL; | |
2236 | } | |
2237 | ||
2238 | static int usbg_bind(struct usb_configuration *c, struct usb_function *f) | |
2239 | { | |
2240 | struct f_uas *fu = to_f_uas(f); | |
2241 | struct usb_gadget *gadget = c->cdev->gadget; | |
2242 | struct usb_ep *ep; | |
2243 | int iface; | |
10287bae | 2244 | int ret; |
c52661d6 SAS |
2245 | |
2246 | iface = usb_interface_id(c, f); | |
2247 | if (iface < 0) | |
2248 | return iface; | |
2249 | ||
2250 | bot_intf_desc.bInterfaceNumber = iface; | |
2251 | uasp_intf_desc.bInterfaceNumber = iface; | |
2252 | fu->iface = iface; | |
2253 | ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bi_desc, | |
2254 | &uasp_bi_ep_comp_desc); | |
2255 | if (!ep) | |
2256 | goto ep_fail; | |
2257 | ||
2258 | ep->driver_data = fu; | |
2259 | fu->ep_in = ep; | |
2260 | ||
2261 | ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bo_desc, | |
2262 | &uasp_bo_ep_comp_desc); | |
2263 | if (!ep) | |
2264 | goto ep_fail; | |
2265 | ep->driver_data = fu; | |
2266 | fu->ep_out = ep; | |
2267 | ||
2268 | ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_status_desc, | |
2269 | &uasp_status_in_ep_comp_desc); | |
2270 | if (!ep) | |
2271 | goto ep_fail; | |
2272 | ep->driver_data = fu; | |
2273 | fu->ep_status = ep; | |
2274 | ||
2275 | ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_cmd_desc, | |
2276 | &uasp_cmd_comp_desc); | |
2277 | if (!ep) | |
2278 | goto ep_fail; | |
2279 | ep->driver_data = fu; | |
2280 | fu->ep_cmd = ep; | |
2281 | ||
2282 | /* Assume endpoint addresses are the same for both speeds */ | |
2283 | uasp_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress; | |
2284 | uasp_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress; | |
2285 | uasp_status_desc.bEndpointAddress = | |
2286 | uasp_ss_status_desc.bEndpointAddress; | |
2287 | uasp_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress; | |
2288 | ||
2289 | uasp_fs_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress; | |
2290 | uasp_fs_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress; | |
2291 | uasp_fs_status_desc.bEndpointAddress = | |
2292 | uasp_ss_status_desc.bEndpointAddress; | |
2293 | uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress; | |
2294 | ||
10287bae SAS |
2295 | ret = usb_assign_descriptors(f, uasp_fs_function_desc, |
2296 | uasp_hs_function_desc, uasp_ss_function_desc); | |
2297 | if (ret) | |
2298 | goto ep_fail; | |
2299 | ||
c52661d6 SAS |
2300 | return 0; |
2301 | ep_fail: | |
2302 | pr_err("Can't claim all required eps\n"); | |
2303 | ||
2304 | give_back_ep(&fu->ep_in); | |
2305 | give_back_ep(&fu->ep_out); | |
2306 | give_back_ep(&fu->ep_status); | |
2307 | give_back_ep(&fu->ep_cmd); | |
2308 | return -ENOTSUPP; | |
2309 | } | |
2310 | ||
2311 | static void usbg_unbind(struct usb_configuration *c, struct usb_function *f) | |
2312 | { | |
2313 | struct f_uas *fu = to_f_uas(f); | |
2314 | ||
10287bae | 2315 | usb_free_all_descriptors(f); |
c52661d6 SAS |
2316 | kfree(fu); |
2317 | } | |
2318 | ||
2319 | struct guas_setup_wq { | |
2320 | struct work_struct work; | |
2321 | struct f_uas *fu; | |
2322 | unsigned int alt; | |
2323 | }; | |
2324 | ||
2325 | static void usbg_delayed_set_alt(struct work_struct *wq) | |
2326 | { | |
2327 | struct guas_setup_wq *work = container_of(wq, struct guas_setup_wq, | |
2328 | work); | |
2329 | struct f_uas *fu = work->fu; | |
2330 | int alt = work->alt; | |
2331 | ||
2332 | kfree(work); | |
2333 | ||
2334 | if (fu->flags & USBG_IS_BOT) | |
2335 | bot_cleanup_old_alt(fu); | |
2336 | if (fu->flags & USBG_IS_UAS) | |
2337 | uasp_cleanup_old_alt(fu); | |
2338 | ||
2339 | if (alt == USB_G_ALT_INT_BBB) | |
2340 | bot_set_alt(fu); | |
2341 | else if (alt == USB_G_ALT_INT_UAS) | |
2342 | uasp_set_alt(fu); | |
2343 | usb_composite_setup_continue(fu->function.config->cdev); | |
2344 | } | |
2345 | ||
2346 | static int usbg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | |
2347 | { | |
2348 | struct f_uas *fu = to_f_uas(f); | |
2349 | ||
2350 | if ((alt == USB_G_ALT_INT_BBB) || (alt == USB_G_ALT_INT_UAS)) { | |
2351 | struct guas_setup_wq *work; | |
2352 | ||
2353 | work = kmalloc(sizeof(*work), GFP_ATOMIC); | |
2354 | if (!work) | |
2355 | return -ENOMEM; | |
2356 | INIT_WORK(&work->work, usbg_delayed_set_alt); | |
2357 | work->fu = fu; | |
2358 | work->alt = alt; | |
2359 | schedule_work(&work->work); | |
2360 | return USB_GADGET_DELAYED_STATUS; | |
2361 | } | |
2362 | return -EOPNOTSUPP; | |
2363 | } | |
2364 | ||
2365 | static void usbg_disable(struct usb_function *f) | |
2366 | { | |
2367 | struct f_uas *fu = to_f_uas(f); | |
2368 | ||
2369 | if (fu->flags & USBG_IS_UAS) | |
2370 | uasp_cleanup_old_alt(fu); | |
2371 | else if (fu->flags & USBG_IS_BOT) | |
2372 | bot_cleanup_old_alt(fu); | |
2373 | fu->flags = 0; | |
2374 | } | |
2375 | ||
2376 | static int usbg_setup(struct usb_function *f, | |
2377 | const struct usb_ctrlrequest *ctrl) | |
2378 | { | |
2379 | struct f_uas *fu = to_f_uas(f); | |
2380 | ||
2381 | if (!(fu->flags & USBG_IS_BOT)) | |
2382 | return -EOPNOTSUPP; | |
2383 | ||
2384 | return usbg_bot_setup(f, ctrl); | |
2385 | } | |
2386 | ||
2387 | static int usbg_cfg_bind(struct usb_configuration *c) | |
2388 | { | |
2389 | struct f_uas *fu; | |
2390 | int ret; | |
2391 | ||
2392 | fu = kzalloc(sizeof(*fu), GFP_KERNEL); | |
2393 | if (!fu) | |
2394 | return -ENOMEM; | |
2395 | fu->function.name = "Target Function"; | |
c52661d6 SAS |
2396 | fu->function.bind = usbg_bind; |
2397 | fu->function.unbind = usbg_unbind; | |
2398 | fu->function.set_alt = usbg_set_alt; | |
2399 | fu->function.setup = usbg_setup; | |
2400 | fu->function.disable = usbg_disable; | |
2401 | fu->tpg = the_only_tpg_I_currently_have; | |
2402 | ||
18786da4 SAS |
2403 | bot_intf_desc.iInterface = usbg_us_strings[USB_G_STR_INT_BBB].id; |
2404 | uasp_intf_desc.iInterface = usbg_us_strings[USB_G_STR_INT_UAS].id; | |
2405 | ||
c52661d6 SAS |
2406 | ret = usb_add_function(c, &fu->function); |
2407 | if (ret) | |
2408 | goto err; | |
2409 | ||
2410 | return 0; | |
2411 | err: | |
2412 | kfree(fu); | |
2413 | return ret; | |
2414 | } | |
2415 | ||
2416 | static int usb_target_bind(struct usb_composite_dev *cdev) | |
2417 | { | |
2418 | int ret; | |
2419 | ||
18786da4 SAS |
2420 | ret = usb_string_ids_tab(cdev, usbg_us_strings); |
2421 | if (ret) | |
2422 | return ret; | |
2423 | ||
2424 | usbg_device_desc.iManufacturer = | |
276e2e4f SAS |
2425 | usbg_us_strings[USB_GADGET_MANUFACTURER_IDX].id; |
2426 | usbg_device_desc.iProduct = usbg_us_strings[USB_GADGET_PRODUCT_IDX].id; | |
2427 | usbg_device_desc.iSerialNumber = | |
2428 | usbg_us_strings[USB_GADGET_SERIAL_IDX].id; | |
18786da4 SAS |
2429 | usbg_config_driver.iConfiguration = |
2430 | usbg_us_strings[USB_G_STR_CONFIG].id; | |
2431 | ||
c52661d6 SAS |
2432 | ret = usb_add_config(cdev, &usbg_config_driver, |
2433 | usbg_cfg_bind); | |
7d16e8d3 SAS |
2434 | if (ret) |
2435 | return ret; | |
2436 | usb_composite_overwrite_options(cdev, &coverwrite); | |
c52661d6 SAS |
2437 | return 0; |
2438 | } | |
2439 | ||
c2ec75c2 | 2440 | static __refdata struct usb_composite_driver usbg_driver = { |
c52661d6 SAS |
2441 | .name = "g_target", |
2442 | .dev = &usbg_device_desc, | |
2443 | .strings = usbg_strings, | |
2444 | .max_speed = USB_SPEED_SUPER, | |
03e42bd5 | 2445 | .bind = usb_target_bind, |
c52661d6 SAS |
2446 | .unbind = guas_unbind, |
2447 | }; | |
2448 | ||
2449 | static int usbg_attach(struct usbg_tpg *tpg) | |
2450 | { | |
03e42bd5 | 2451 | return usb_composite_probe(&usbg_driver); |
c52661d6 SAS |
2452 | } |
2453 | ||
2454 | static void usbg_detach(struct usbg_tpg *tpg) | |
2455 | { | |
2456 | usb_composite_unregister(&usbg_driver); | |
2457 | } | |
2458 | ||
2459 | static int __init usb_target_gadget_init(void) | |
2460 | { | |
2461 | int ret; | |
2462 | ||
2463 | ret = usbg_register_configfs(); | |
2464 | return ret; | |
2465 | } | |
2466 | module_init(usb_target_gadget_init); | |
2467 | ||
2468 | static void __exit usb_target_gadget_exit(void) | |
2469 | { | |
2470 | usbg_deregister_configfs(); | |
2471 | } | |
2472 | module_exit(usb_target_gadget_exit); | |
2473 | ||
2474 | MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>"); | |
2475 | MODULE_DESCRIPTION("usb-gadget fabric"); | |
2476 | MODULE_LICENSE("GPL v2"); |