1 /* Target based USB-Gadget
3 * UAS protocol handling, target callbacks, configfs handling,
4 * BBB (USB Mass Storage Class Bulk-Only (BBB) and Transport protocol handling.
6 * Author: Sebastian Andrzej Siewior <bigeasy at linutronix dot de>
7 * License: GPLv2 as published by FSF.
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_tcq.h>
20 #include <target/target_core_base.h>
21 #include <target/target_core_fabric.h>
22 #include <target/target_core_fabric_configfs.h>
23 #include <target/target_core_configfs.h>
24 #include <target/configfs_macros.h>
25 #include <asm/unaligned.h>
27 #include "tcm_usb_gadget.h"
29 USB_GADGET_COMPOSITE_OPTIONS();
31 static const struct target_core_fabric_ops usbg_ops;
33 static inline struct f_uas *to_f_uas(struct usb_function *f)
35 return container_of(f, struct f_uas, function);
38 static void usbg_cmd_release(struct kref *);
40 static inline void usbg_cleanup_cmd(struct usbg_cmd *cmd)
42 kref_put(&cmd->ref, usbg_cmd_release);
45 /* Start bot.c code */
47 static int bot_enqueue_cmd_cbw(struct f_uas *fu)
51 if (fu->flags & USBG_BOT_CMD_PEND)
54 ret = usb_ep_queue(fu->ep_out, fu->cmd.req, GFP_ATOMIC);
56 fu->flags |= USBG_BOT_CMD_PEND;
60 static void bot_status_complete(struct usb_ep *ep, struct usb_request *req)
62 struct usbg_cmd *cmd = req->context;
63 struct f_uas *fu = cmd->fu;
65 usbg_cleanup_cmd(cmd);
66 if (req->status < 0) {
67 pr_err("ERR %s(%d)\n", __func__, __LINE__);
71 /* CSW completed, wait for next CBW */
72 bot_enqueue_cmd_cbw(fu);
75 static void bot_enqueue_sense_code(struct f_uas *fu, struct usbg_cmd *cmd)
77 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
80 unsigned int csw_stat;
82 csw_stat = cmd->csw_code;
85 * We can't send SENSE as a response. So we take ASC & ASCQ from our
86 * sense buffer and queue it and hope the host sends a REQUEST_SENSE
87 * command where it learns why we failed.
89 sense = cmd->sense_iu.sense;
91 csw->Tag = cmd->bot_tag;
92 csw->Status = csw_stat;
93 fu->bot_status.req->context = cmd;
94 ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_ATOMIC);
96 pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
99 static void bot_err_compl(struct usb_ep *ep, struct usb_request *req)
101 struct usbg_cmd *cmd = req->context;
102 struct f_uas *fu = cmd->fu;
105 pr_err("ERR %s(%d)\n", __func__, __LINE__);
108 if (cmd->data_len > ep->maxpacket) {
109 req->length = ep->maxpacket;
110 cmd->data_len -= ep->maxpacket;
112 req->length = cmd->data_len;
116 usb_ep_queue(ep, req, GFP_ATOMIC);
119 bot_enqueue_sense_code(fu, cmd);
122 static void bot_send_bad_status(struct usbg_cmd *cmd)
124 struct f_uas *fu = cmd->fu;
125 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
126 struct usb_request *req;
129 csw->Residue = cpu_to_le32(cmd->data_len);
134 req = fu->bot_req_in;
137 req = fu->bot_req_out;
140 if (cmd->data_len > fu->ep_in->maxpacket) {
141 req->length = ep->maxpacket;
142 cmd->data_len -= ep->maxpacket;
144 req->length = cmd->data_len;
147 req->complete = bot_err_compl;
149 req->buf = fu->cmd.buf;
150 usb_ep_queue(ep, req, GFP_KERNEL);
152 bot_enqueue_sense_code(fu, cmd);
156 static int bot_send_status(struct usbg_cmd *cmd, bool moved_data)
158 struct f_uas *fu = cmd->fu;
159 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
162 if (cmd->se_cmd.scsi_status == SAM_STAT_GOOD) {
163 if (!moved_data && cmd->data_len) {
165 * the host wants to move data, we don't. Fill / empty
166 * the pipe and then send the csw with reside set.
168 cmd->csw_code = US_BULK_STAT_OK;
169 bot_send_bad_status(cmd);
173 csw->Tag = cmd->bot_tag;
174 csw->Residue = cpu_to_le32(0);
175 csw->Status = US_BULK_STAT_OK;
176 fu->bot_status.req->context = cmd;
178 ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_KERNEL);
180 pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
182 cmd->csw_code = US_BULK_STAT_FAIL;
183 bot_send_bad_status(cmd);
189 * Called after command (no data transfer) or after the write (to device)
190 * operation is completed
192 static int bot_send_status_response(struct usbg_cmd *cmd)
194 bool moved_data = false;
198 return bot_send_status(cmd, moved_data);
201 /* Read request completed, now we have to send the CSW */
202 static void bot_read_compl(struct usb_ep *ep, struct usb_request *req)
204 struct usbg_cmd *cmd = req->context;
207 pr_err("ERR %s(%d)\n", __func__, __LINE__);
209 bot_send_status(cmd, true);
212 static int bot_send_read_response(struct usbg_cmd *cmd)
214 struct f_uas *fu = cmd->fu;
215 struct se_cmd *se_cmd = &cmd->se_cmd;
216 struct usb_gadget *gadget = fuas_to_gadget(fu);
219 if (!cmd->data_len) {
220 cmd->csw_code = US_BULK_STAT_PHASE;
221 bot_send_bad_status(cmd);
225 if (!gadget->sg_supported) {
226 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
230 sg_copy_to_buffer(se_cmd->t_data_sg,
231 se_cmd->t_data_nents,
233 se_cmd->data_length);
235 fu->bot_req_in->buf = cmd->data_buf;
237 fu->bot_req_in->buf = NULL;
238 fu->bot_req_in->num_sgs = se_cmd->t_data_nents;
239 fu->bot_req_in->sg = se_cmd->t_data_sg;
242 fu->bot_req_in->complete = bot_read_compl;
243 fu->bot_req_in->length = se_cmd->data_length;
244 fu->bot_req_in->context = cmd;
245 ret = usb_ep_queue(fu->ep_in, fu->bot_req_in, GFP_ATOMIC);
247 pr_err("%s(%d)\n", __func__, __LINE__);
251 static void usbg_data_write_cmpl(struct usb_ep *, struct usb_request *);
252 static int usbg_prepare_w_request(struct usbg_cmd *, struct usb_request *);
254 static int bot_send_write_request(struct usbg_cmd *cmd)
256 struct f_uas *fu = cmd->fu;
257 struct se_cmd *se_cmd = &cmd->se_cmd;
258 struct usb_gadget *gadget = fuas_to_gadget(fu);
261 init_completion(&cmd->write_complete);
264 if (!cmd->data_len) {
265 cmd->csw_code = US_BULK_STAT_PHASE;
269 if (!gadget->sg_supported) {
270 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_KERNEL);
274 fu->bot_req_out->buf = cmd->data_buf;
276 fu->bot_req_out->buf = NULL;
277 fu->bot_req_out->num_sgs = se_cmd->t_data_nents;
278 fu->bot_req_out->sg = se_cmd->t_data_sg;
281 fu->bot_req_out->complete = usbg_data_write_cmpl;
282 fu->bot_req_out->length = se_cmd->data_length;
283 fu->bot_req_out->context = cmd;
285 ret = usbg_prepare_w_request(cmd, fu->bot_req_out);
288 ret = usb_ep_queue(fu->ep_out, fu->bot_req_out, GFP_KERNEL);
290 pr_err("%s(%d)\n", __func__, __LINE__);
292 wait_for_completion(&cmd->write_complete);
293 target_execute_cmd(se_cmd);
298 static int bot_submit_command(struct f_uas *, void *, unsigned int);
300 static void bot_cmd_complete(struct usb_ep *ep, struct usb_request *req)
302 struct f_uas *fu = req->context;
305 fu->flags &= ~USBG_BOT_CMD_PEND;
310 ret = bot_submit_command(fu, req->buf, req->actual);
312 pr_err("%s(%d): %d\n", __func__, __LINE__, ret);
315 static int bot_prepare_reqs(struct f_uas *fu)
319 fu->bot_req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
323 fu->bot_req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
324 if (!fu->bot_req_out)
327 fu->cmd.req = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
331 fu->bot_status.req = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
332 if (!fu->bot_status.req)
335 fu->bot_status.req->buf = &fu->bot_status.csw;
336 fu->bot_status.req->length = US_BULK_CS_WRAP_LEN;
337 fu->bot_status.req->complete = bot_status_complete;
338 fu->bot_status.csw.Signature = cpu_to_le32(US_BULK_CS_SIGN);
340 fu->cmd.buf = kmalloc(fu->ep_out->maxpacket, GFP_KERNEL);
344 fu->cmd.req->complete = bot_cmd_complete;
345 fu->cmd.req->buf = fu->cmd.buf;
346 fu->cmd.req->length = fu->ep_out->maxpacket;
347 fu->cmd.req->context = fu;
349 ret = bot_enqueue_cmd_cbw(fu);
357 usb_ep_free_request(fu->ep_in, fu->bot_status.req);
359 usb_ep_free_request(fu->ep_out, fu->cmd.req);
362 usb_ep_free_request(fu->ep_out, fu->bot_req_out);
363 fu->bot_req_out = NULL;
365 usb_ep_free_request(fu->ep_in, fu->bot_req_in);
366 fu->bot_req_in = NULL;
368 pr_err("BOT: endpoint setup failed\n");
372 static void bot_cleanup_old_alt(struct f_uas *fu)
374 if (!(fu->flags & USBG_ENABLED))
377 usb_ep_disable(fu->ep_in);
378 usb_ep_disable(fu->ep_out);
383 usb_ep_free_request(fu->ep_in, fu->bot_req_in);
384 usb_ep_free_request(fu->ep_out, fu->bot_req_out);
385 usb_ep_free_request(fu->ep_out, fu->cmd.req);
386 usb_ep_free_request(fu->ep_out, fu->bot_status.req);
390 fu->bot_req_in = NULL;
391 fu->bot_req_out = NULL;
393 fu->bot_status.req = NULL;
397 static void bot_set_alt(struct f_uas *fu)
399 struct usb_function *f = &fu->function;
400 struct usb_gadget *gadget = f->config->cdev->gadget;
403 fu->flags = USBG_IS_BOT;
405 config_ep_by_speed(gadget, f, fu->ep_in);
406 ret = usb_ep_enable(fu->ep_in);
410 config_ep_by_speed(gadget, f, fu->ep_out);
411 ret = usb_ep_enable(fu->ep_out);
415 ret = bot_prepare_reqs(fu);
418 fu->flags |= USBG_ENABLED;
419 pr_info("Using the BOT protocol\n");
422 usb_ep_disable(fu->ep_out);
424 usb_ep_disable(fu->ep_in);
426 fu->flags = USBG_IS_BOT;
429 static int usbg_bot_setup(struct usb_function *f,
430 const struct usb_ctrlrequest *ctrl)
432 struct f_uas *fu = to_f_uas(f);
433 struct usb_composite_dev *cdev = f->config->cdev;
434 u16 w_value = le16_to_cpu(ctrl->wValue);
435 u16 w_length = le16_to_cpu(ctrl->wLength);
439 switch (ctrl->bRequest) {
440 case US_BULK_GET_MAX_LUN:
441 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_CLASS |
442 USB_RECIP_INTERFACE))
449 luns = atomic_read(&fu->tpg->tpg_port_count);
451 pr_err("No LUNs configured?\n");
455 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
456 * accessed. The upper limit is 0xf
460 pr_info_once("Limiting the number of luns to 16\n");
463 ret_lun = cdev->req->buf;
465 cdev->req->length = 1;
466 return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
469 case US_BULK_RESET_REQUEST:
470 /* XXX maybe we should remove previous requests for IN + OUT */
471 bot_enqueue_cmd_cbw(fu);
478 /* Start uas.c code */
480 static void uasp_cleanup_one_stream(struct f_uas *fu, struct uas_stream *stream)
482 /* We have either all three allocated or none */
486 usb_ep_free_request(fu->ep_in, stream->req_in);
487 usb_ep_free_request(fu->ep_out, stream->req_out);
488 usb_ep_free_request(fu->ep_status, stream->req_status);
490 stream->req_in = NULL;
491 stream->req_out = NULL;
492 stream->req_status = NULL;
495 static void uasp_free_cmdreq(struct f_uas *fu)
497 usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
503 static void uasp_cleanup_old_alt(struct f_uas *fu)
507 if (!(fu->flags & USBG_ENABLED))
510 usb_ep_disable(fu->ep_in);
511 usb_ep_disable(fu->ep_out);
512 usb_ep_disable(fu->ep_status);
513 usb_ep_disable(fu->ep_cmd);
515 for (i = 0; i < UASP_SS_EP_COMP_NUM_STREAMS; i++)
516 uasp_cleanup_one_stream(fu, &fu->stream[i]);
517 uasp_free_cmdreq(fu);
520 static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req);
522 static int uasp_prepare_r_request(struct usbg_cmd *cmd)
524 struct se_cmd *se_cmd = &cmd->se_cmd;
525 struct f_uas *fu = cmd->fu;
526 struct usb_gadget *gadget = fuas_to_gadget(fu);
527 struct uas_stream *stream = cmd->stream;
529 if (!gadget->sg_supported) {
530 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
534 sg_copy_to_buffer(se_cmd->t_data_sg,
535 se_cmd->t_data_nents,
537 se_cmd->data_length);
539 stream->req_in->buf = cmd->data_buf;
541 stream->req_in->buf = NULL;
542 stream->req_in->num_sgs = se_cmd->t_data_nents;
543 stream->req_in->sg = se_cmd->t_data_sg;
546 stream->req_in->complete = uasp_status_data_cmpl;
547 stream->req_in->length = se_cmd->data_length;
548 stream->req_in->context = cmd;
550 cmd->state = UASP_SEND_STATUS;
554 static void uasp_prepare_status(struct usbg_cmd *cmd)
556 struct se_cmd *se_cmd = &cmd->se_cmd;
557 struct sense_iu *iu = &cmd->sense_iu;
558 struct uas_stream *stream = cmd->stream;
560 cmd->state = UASP_QUEUE_COMMAND;
561 iu->iu_id = IU_ID_STATUS;
562 iu->tag = cpu_to_be16(cmd->tag);
565 * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?);
567 iu->len = cpu_to_be16(se_cmd->scsi_sense_length);
568 iu->status = se_cmd->scsi_status;
569 stream->req_status->context = cmd;
570 stream->req_status->length = se_cmd->scsi_sense_length + 16;
571 stream->req_status->buf = iu;
572 stream->req_status->complete = uasp_status_data_cmpl;
575 static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req)
577 struct usbg_cmd *cmd = req->context;
578 struct uas_stream *stream = cmd->stream;
579 struct f_uas *fu = cmd->fu;
585 switch (cmd->state) {
587 ret = uasp_prepare_r_request(cmd);
590 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
592 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
595 case UASP_RECEIVE_DATA:
596 ret = usbg_prepare_w_request(cmd, stream->req_out);
599 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
601 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
604 case UASP_SEND_STATUS:
605 uasp_prepare_status(cmd);
606 ret = usb_ep_queue(fu->ep_status, stream->req_status,
609 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
612 case UASP_QUEUE_COMMAND:
613 usbg_cleanup_cmd(cmd);
614 usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
623 usbg_cleanup_cmd(cmd);
626 static int uasp_send_status_response(struct usbg_cmd *cmd)
628 struct f_uas *fu = cmd->fu;
629 struct uas_stream *stream = cmd->stream;
630 struct sense_iu *iu = &cmd->sense_iu;
632 iu->tag = cpu_to_be16(cmd->tag);
633 stream->req_status->complete = uasp_status_data_cmpl;
634 stream->req_status->context = cmd;
636 uasp_prepare_status(cmd);
637 return usb_ep_queue(fu->ep_status, stream->req_status, GFP_ATOMIC);
640 static int uasp_send_read_response(struct usbg_cmd *cmd)
642 struct f_uas *fu = cmd->fu;
643 struct uas_stream *stream = cmd->stream;
644 struct sense_iu *iu = &cmd->sense_iu;
649 iu->tag = cpu_to_be16(cmd->tag);
650 if (fu->flags & USBG_USE_STREAMS) {
652 ret = uasp_prepare_r_request(cmd);
655 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
657 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
658 kfree(cmd->data_buf);
659 cmd->data_buf = NULL;
664 iu->iu_id = IU_ID_READ_READY;
665 iu->tag = cpu_to_be16(cmd->tag);
667 stream->req_status->complete = uasp_status_data_cmpl;
668 stream->req_status->context = cmd;
670 cmd->state = UASP_SEND_DATA;
671 stream->req_status->buf = iu;
672 stream->req_status->length = sizeof(struct iu);
674 ret = usb_ep_queue(fu->ep_status, stream->req_status,
677 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
683 static int uasp_send_write_request(struct usbg_cmd *cmd)
685 struct f_uas *fu = cmd->fu;
686 struct se_cmd *se_cmd = &cmd->se_cmd;
687 struct uas_stream *stream = cmd->stream;
688 struct sense_iu *iu = &cmd->sense_iu;
691 init_completion(&cmd->write_complete);
694 iu->tag = cpu_to_be16(cmd->tag);
696 if (fu->flags & USBG_USE_STREAMS) {
698 ret = usbg_prepare_w_request(cmd, stream->req_out);
701 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
703 pr_err("%s(%d)\n", __func__, __LINE__);
707 iu->iu_id = IU_ID_WRITE_READY;
708 iu->tag = cpu_to_be16(cmd->tag);
710 stream->req_status->complete = uasp_status_data_cmpl;
711 stream->req_status->context = cmd;
713 cmd->state = UASP_RECEIVE_DATA;
714 stream->req_status->buf = iu;
715 stream->req_status->length = sizeof(struct iu);
717 ret = usb_ep_queue(fu->ep_status, stream->req_status,
720 pr_err("%s(%d)\n", __func__, __LINE__);
723 wait_for_completion(&cmd->write_complete);
724 target_execute_cmd(se_cmd);
729 static int usbg_submit_command(struct f_uas *, void *, unsigned int);
731 static void uasp_cmd_complete(struct usb_ep *ep, struct usb_request *req)
733 struct f_uas *fu = req->context;
739 ret = usbg_submit_command(fu, req->buf, req->actual);
741 * Once we tune for performance enqueue the command req here again so
742 * we can receive a second command while we processing this one. Pay
743 * attention to properly sync STAUS endpoint with DATA IN + OUT so you
748 usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
751 static int uasp_alloc_stream_res(struct f_uas *fu, struct uas_stream *stream)
753 stream->req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
757 stream->req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
758 if (!stream->req_out)
761 stream->req_status = usb_ep_alloc_request(fu->ep_status, GFP_KERNEL);
762 if (!stream->req_status)
767 usb_ep_free_request(fu->ep_status, stream->req_status);
768 stream->req_status = NULL;
770 usb_ep_free_request(fu->ep_out, stream->req_out);
771 stream->req_out = NULL;
776 static int uasp_alloc_cmd(struct f_uas *fu)
778 fu->cmd.req = usb_ep_alloc_request(fu->ep_cmd, GFP_KERNEL);
782 fu->cmd.buf = kmalloc(fu->ep_cmd->maxpacket, GFP_KERNEL);
786 fu->cmd.req->complete = uasp_cmd_complete;
787 fu->cmd.req->buf = fu->cmd.buf;
788 fu->cmd.req->length = fu->ep_cmd->maxpacket;
789 fu->cmd.req->context = fu;
793 usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
798 static void uasp_setup_stream_res(struct f_uas *fu, int max_streams)
802 for (i = 0; i < max_streams; i++) {
803 struct uas_stream *s = &fu->stream[i];
805 s->req_in->stream_id = i + 1;
806 s->req_out->stream_id = i + 1;
807 s->req_status->stream_id = i + 1;
811 static int uasp_prepare_reqs(struct f_uas *fu)
817 if (fu->flags & USBG_USE_STREAMS)
818 max_streams = UASP_SS_EP_COMP_NUM_STREAMS;
822 for (i = 0; i < max_streams; i++) {
823 ret = uasp_alloc_stream_res(fu, &fu->stream[i]);
828 ret = uasp_alloc_cmd(fu);
830 goto err_free_stream;
831 uasp_setup_stream_res(fu, max_streams);
833 ret = usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
835 goto err_free_stream;
840 uasp_free_cmdreq(fu);
845 uasp_cleanup_one_stream(fu, &fu->stream[i - 1]);
849 pr_err("UASP: endpoint setup failed\n");
853 static void uasp_set_alt(struct f_uas *fu)
855 struct usb_function *f = &fu->function;
856 struct usb_gadget *gadget = f->config->cdev->gadget;
859 fu->flags = USBG_IS_UAS;
861 if (gadget->speed == USB_SPEED_SUPER)
862 fu->flags |= USBG_USE_STREAMS;
864 config_ep_by_speed(gadget, f, fu->ep_in);
865 ret = usb_ep_enable(fu->ep_in);
869 config_ep_by_speed(gadget, f, fu->ep_out);
870 ret = usb_ep_enable(fu->ep_out);
874 config_ep_by_speed(gadget, f, fu->ep_cmd);
875 ret = usb_ep_enable(fu->ep_cmd);
878 config_ep_by_speed(gadget, f, fu->ep_status);
879 ret = usb_ep_enable(fu->ep_status);
883 ret = uasp_prepare_reqs(fu);
886 fu->flags |= USBG_ENABLED;
888 pr_info("Using the UAS protocol\n");
891 usb_ep_disable(fu->ep_status);
893 usb_ep_disable(fu->ep_cmd);
895 usb_ep_disable(fu->ep_out);
897 usb_ep_disable(fu->ep_in);
902 static int get_cmd_dir(const unsigned char *cdb)
914 case SERVICE_ACTION_IN_16:
916 case PERSISTENT_RESERVE_IN:
917 case SECURITY_PROTOCOL_IN:
918 case ACCESS_CONTROL_IN:
920 case READ_BLOCK_LIMITS:
924 case READ_FORMAT_CAPACITIES:
926 ret = DMA_FROM_DEVICE;
936 case WRITE_VERIFY_12:
937 case PERSISTENT_RESERVE_OUT:
938 case MAINTENANCE_OUT:
939 case SECURITY_PROTOCOL_OUT:
940 case ACCESS_CONTROL_OUT:
943 case ALLOW_MEDIUM_REMOVAL:
944 case TEST_UNIT_READY:
945 case SYNCHRONIZE_CACHE:
952 case WRITE_FILEMARKS:
956 pr_warn("target: Unknown data direction for SCSI Opcode "
963 static void usbg_data_write_cmpl(struct usb_ep *ep, struct usb_request *req)
965 struct usbg_cmd *cmd = req->context;
966 struct se_cmd *se_cmd = &cmd->se_cmd;
968 if (req->status < 0) {
969 pr_err("%s() state %d transfer failed\n", __func__, cmd->state);
973 if (req->num_sgs == 0) {
974 sg_copy_from_buffer(se_cmd->t_data_sg,
975 se_cmd->t_data_nents,
977 se_cmd->data_length);
980 complete(&cmd->write_complete);
984 usbg_cleanup_cmd(cmd);
987 static int usbg_prepare_w_request(struct usbg_cmd *cmd, struct usb_request *req)
989 struct se_cmd *se_cmd = &cmd->se_cmd;
990 struct f_uas *fu = cmd->fu;
991 struct usb_gadget *gadget = fuas_to_gadget(fu);
993 if (!gadget->sg_supported) {
994 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
998 req->buf = cmd->data_buf;
1001 req->num_sgs = se_cmd->t_data_nents;
1002 req->sg = se_cmd->t_data_sg;
1005 req->complete = usbg_data_write_cmpl;
1006 req->length = se_cmd->data_length;
1011 static int usbg_send_status_response(struct se_cmd *se_cmd)
1013 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1015 struct f_uas *fu = cmd->fu;
1017 if (fu->flags & USBG_IS_BOT)
1018 return bot_send_status_response(cmd);
1020 return uasp_send_status_response(cmd);
1023 static int usbg_send_write_request(struct se_cmd *se_cmd)
1025 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1027 struct f_uas *fu = cmd->fu;
1029 if (fu->flags & USBG_IS_BOT)
1030 return bot_send_write_request(cmd);
1032 return uasp_send_write_request(cmd);
1035 static int usbg_send_read_response(struct se_cmd *se_cmd)
1037 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1039 struct f_uas *fu = cmd->fu;
1041 if (fu->flags & USBG_IS_BOT)
1042 return bot_send_read_response(cmd);
1044 return uasp_send_read_response(cmd);
1047 static void usbg_cmd_work(struct work_struct *work)
1049 struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
1050 struct se_cmd *se_cmd;
1051 struct tcm_usbg_nexus *tv_nexus;
1052 struct usbg_tpg *tpg;
1055 se_cmd = &cmd->se_cmd;
1057 tv_nexus = tpg->tpg_nexus;
1058 dir = get_cmd_dir(cmd->cmd_buf);
1060 transport_init_se_cmd(se_cmd,
1061 tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
1062 tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
1063 cmd->prio_attr, cmd->sense_iu.sense);
1067 if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
1068 cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
1069 0, cmd->prio_attr, dir, TARGET_SCF_UNKNOWN_SIZE) < 0)
1075 transport_send_check_condition_and_sense(se_cmd,
1076 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1077 usbg_cleanup_cmd(cmd);
1080 static int usbg_submit_command(struct f_uas *fu,
1081 void *cmdbuf, unsigned int len)
1083 struct command_iu *cmd_iu = cmdbuf;
1084 struct usbg_cmd *cmd;
1085 struct usbg_tpg *tpg;
1086 struct se_cmd *se_cmd;
1087 struct tcm_usbg_nexus *tv_nexus;
1091 if (cmd_iu->iu_id != IU_ID_COMMAND) {
1092 pr_err("Unsupported type %d\n", cmd_iu->iu_id);
1096 cmd = kzalloc(sizeof *cmd, GFP_ATOMIC);
1102 /* XXX until I figure out why I can't free in on complete */
1103 kref_init(&cmd->ref);
1104 kref_get(&cmd->ref);
1107 cmd_len = (cmd_iu->len & ~0x3) + 16;
1108 if (cmd_len > USBG_MAX_CMD)
1111 memcpy(cmd->cmd_buf, cmd_iu->cdb, cmd_len);
1113 cmd->tag = be16_to_cpup(&cmd_iu->tag);
1114 if (fu->flags & USBG_USE_STREAMS) {
1115 if (cmd->tag > UASP_SS_EP_COMP_NUM_STREAMS)
1118 cmd->stream = &fu->stream[0];
1120 cmd->stream = &fu->stream[cmd->tag - 1];
1122 cmd->stream = &fu->stream[0];
1125 tv_nexus = tpg->tpg_nexus;
1127 pr_err("Missing nexus, ignoring command\n");
1131 switch (cmd_iu->prio_attr & 0x7) {
1133 cmd->prio_attr = TCM_HEAD_TAG;
1135 case UAS_ORDERED_TAG:
1136 cmd->prio_attr = TCM_ORDERED_TAG;
1139 cmd->prio_attr = TCM_ACA_TAG;
1142 pr_debug_once("Unsupported prio_attr: %02x.\n",
1144 case UAS_SIMPLE_TAG:
1145 cmd->prio_attr = TCM_SIMPLE_TAG;
1149 se_cmd = &cmd->se_cmd;
1150 cmd->unpacked_lun = scsilun_to_int(&cmd_iu->lun);
1152 INIT_WORK(&cmd->work, usbg_cmd_work);
1153 ret = queue_work(tpg->workqueue, &cmd->work);
1163 static void bot_cmd_work(struct work_struct *work)
1165 struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
1166 struct se_cmd *se_cmd;
1167 struct tcm_usbg_nexus *tv_nexus;
1168 struct usbg_tpg *tpg;
1171 se_cmd = &cmd->se_cmd;
1173 tv_nexus = tpg->tpg_nexus;
1174 dir = get_cmd_dir(cmd->cmd_buf);
1176 transport_init_se_cmd(se_cmd,
1177 tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
1178 tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
1179 cmd->prio_attr, cmd->sense_iu.sense);
1183 if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
1184 cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
1185 cmd->data_len, cmd->prio_attr, dir, 0) < 0)
1191 transport_send_check_condition_and_sense(se_cmd,
1192 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1193 usbg_cleanup_cmd(cmd);
1196 static int bot_submit_command(struct f_uas *fu,
1197 void *cmdbuf, unsigned int len)
1199 struct bulk_cb_wrap *cbw = cmdbuf;
1200 struct usbg_cmd *cmd;
1201 struct usbg_tpg *tpg;
1202 struct se_cmd *se_cmd;
1203 struct tcm_usbg_nexus *tv_nexus;
1207 if (cbw->Signature != cpu_to_le32(US_BULK_CB_SIGN)) {
1208 pr_err("Wrong signature on CBW\n");
1212 pr_err("Wrong length for CBW\n");
1216 cmd_len = cbw->Length;
1217 if (cmd_len < 1 || cmd_len > 16)
1220 cmd = kzalloc(sizeof *cmd, GFP_ATOMIC);
1226 /* XXX until I figure out why I can't free in on complete */
1227 kref_init(&cmd->ref);
1228 kref_get(&cmd->ref);
1232 memcpy(cmd->cmd_buf, cbw->CDB, cmd_len);
1234 cmd->bot_tag = cbw->Tag;
1236 tv_nexus = tpg->tpg_nexus;
1238 pr_err("Missing nexus, ignoring command\n");
1242 cmd->prio_attr = TCM_SIMPLE_TAG;
1243 se_cmd = &cmd->se_cmd;
1244 cmd->unpacked_lun = cbw->Lun;
1245 cmd->is_read = cbw->Flags & US_BULK_FLAG_IN ? 1 : 0;
1246 cmd->data_len = le32_to_cpu(cbw->DataTransferLength);
1248 INIT_WORK(&cmd->work, bot_cmd_work);
1249 ret = queue_work(tpg->workqueue, &cmd->work);
1259 /* Start fabric.c code */
1261 static int usbg_check_true(struct se_portal_group *se_tpg)
1266 static int usbg_check_false(struct se_portal_group *se_tpg)
1271 static char *usbg_get_fabric_name(void)
1273 return "usb_gadget";
1276 static u8 usbg_get_fabric_proto_ident(struct se_portal_group *se_tpg)
1278 struct usbg_tpg *tpg = container_of(se_tpg,
1279 struct usbg_tpg, se_tpg);
1280 struct usbg_tport *tport = tpg->tport;
1283 switch (tport->tport_proto_id) {
1284 case SCSI_PROTOCOL_SAS:
1286 proto_id = sas_get_fabric_proto_ident(se_tpg);
1293 static char *usbg_get_fabric_wwn(struct se_portal_group *se_tpg)
1295 struct usbg_tpg *tpg = container_of(se_tpg,
1296 struct usbg_tpg, se_tpg);
1297 struct usbg_tport *tport = tpg->tport;
1299 return &tport->tport_name[0];
1302 static u16 usbg_get_tag(struct se_portal_group *se_tpg)
1304 struct usbg_tpg *tpg = container_of(se_tpg,
1305 struct usbg_tpg, se_tpg);
1306 return tpg->tport_tpgt;
1309 static u32 usbg_get_default_depth(struct se_portal_group *se_tpg)
1314 static u32 usbg_get_pr_transport_id(
1315 struct se_portal_group *se_tpg,
1316 struct se_node_acl *se_nacl,
1317 struct t10_pr_registration *pr_reg,
1321 struct usbg_tpg *tpg = container_of(se_tpg,
1322 struct usbg_tpg, se_tpg);
1323 struct usbg_tport *tport = tpg->tport;
1326 switch (tport->tport_proto_id) {
1327 case SCSI_PROTOCOL_SAS:
1329 ret = sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
1337 static u32 usbg_get_pr_transport_id_len(
1338 struct se_portal_group *se_tpg,
1339 struct se_node_acl *se_nacl,
1340 struct t10_pr_registration *pr_reg,
1343 struct usbg_tpg *tpg = container_of(se_tpg,
1344 struct usbg_tpg, se_tpg);
1345 struct usbg_tport *tport = tpg->tport;
1348 switch (tport->tport_proto_id) {
1349 case SCSI_PROTOCOL_SAS:
1351 ret = sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
1359 static char *usbg_parse_pr_out_transport_id(
1360 struct se_portal_group *se_tpg,
1363 char **port_nexus_ptr)
1365 struct usbg_tpg *tpg = container_of(se_tpg,
1366 struct usbg_tpg, se_tpg);
1367 struct usbg_tport *tport = tpg->tport;
1370 switch (tport->tport_proto_id) {
1371 case SCSI_PROTOCOL_SAS:
1373 tid = sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
1380 static struct se_node_acl *usbg_alloc_fabric_acl(struct se_portal_group *se_tpg)
1382 struct usbg_nacl *nacl;
1384 nacl = kzalloc(sizeof(struct usbg_nacl), GFP_KERNEL);
1388 return &nacl->se_node_acl;
1391 static void usbg_release_fabric_acl(
1392 struct se_portal_group *se_tpg,
1393 struct se_node_acl *se_nacl)
1395 struct usbg_nacl *nacl = container_of(se_nacl,
1396 struct usbg_nacl, se_node_acl);
1400 static u32 usbg_tpg_get_inst_index(struct se_portal_group *se_tpg)
1405 static void usbg_cmd_release(struct kref *ref)
1407 struct usbg_cmd *cmd = container_of(ref, struct usbg_cmd,
1410 transport_generic_free_cmd(&cmd->se_cmd, 0);
1413 static void usbg_release_cmd(struct se_cmd *se_cmd)
1415 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1417 kfree(cmd->data_buf);
1422 static int usbg_shutdown_session(struct se_session *se_sess)
1427 static void usbg_close_session(struct se_session *se_sess)
1432 static u32 usbg_sess_get_index(struct se_session *se_sess)
1438 * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be
1440 static int usbg_write_pending_status(struct se_cmd *se_cmd)
1445 static void usbg_set_default_node_attrs(struct se_node_acl *nacl)
1450 static u32 usbg_get_task_tag(struct se_cmd *se_cmd)
1452 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1454 struct f_uas *fu = cmd->fu;
1456 if (fu->flags & USBG_IS_BOT)
1457 return le32_to_cpu(cmd->bot_tag);
1462 static int usbg_get_cmd_state(struct se_cmd *se_cmd)
1467 static void usbg_queue_tm_rsp(struct se_cmd *se_cmd)
1471 static void usbg_aborted_task(struct se_cmd *se_cmd)
1476 static const char *usbg_check_wwn(const char *name)
1481 n = strstr(name, "naa.");
1486 if (len == 0 || len > USBG_NAMELEN - 1)
1491 static struct se_node_acl *usbg_make_nodeacl(
1492 struct se_portal_group *se_tpg,
1493 struct config_group *group,
1496 struct se_node_acl *se_nacl, *se_nacl_new;
1497 struct usbg_nacl *nacl;
1500 const char *wnn_name;
1502 wnn_name = usbg_check_wwn(name);
1504 return ERR_PTR(-EINVAL);
1505 se_nacl_new = usbg_alloc_fabric_acl(se_tpg);
1507 return ERR_PTR(-ENOMEM);
1511 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1512 * when converting a NodeACL from demo mode -> explict
1514 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1516 if (IS_ERR(se_nacl)) {
1517 usbg_release_fabric_acl(se_tpg, se_nacl_new);
1521 * Locate our struct usbg_nacl and set the FC Nport WWPN
1523 nacl = container_of(se_nacl, struct usbg_nacl, se_node_acl);
1524 nacl->iport_wwpn = wwpn;
1525 snprintf(nacl->iport_name, sizeof(nacl->iport_name), "%s", name);
1529 static void usbg_drop_nodeacl(struct se_node_acl *se_acl)
1531 struct usbg_nacl *nacl = container_of(se_acl,
1532 struct usbg_nacl, se_node_acl);
1533 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1537 struct usbg_tpg *the_only_tpg_I_currently_have;
1539 static struct se_portal_group *usbg_make_tpg(
1541 struct config_group *group,
1544 struct usbg_tport *tport = container_of(wwn, struct usbg_tport,
1546 struct usbg_tpg *tpg;
1550 if (strstr(name, "tpgt_") != name)
1551 return ERR_PTR(-EINVAL);
1552 if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX)
1553 return ERR_PTR(-EINVAL);
1554 if (the_only_tpg_I_currently_have) {
1555 pr_err("Until the gadget framework can't handle multiple\n");
1556 pr_err("gadgets, you can't do this here.\n");
1557 return ERR_PTR(-EBUSY);
1560 tpg = kzalloc(sizeof(struct usbg_tpg), GFP_KERNEL);
1562 return ERR_PTR(-ENOMEM);
1563 mutex_init(&tpg->tpg_mutex);
1564 atomic_set(&tpg->tpg_port_count, 0);
1565 tpg->workqueue = alloc_workqueue("tcm_usb_gadget", 0, 1);
1566 if (!tpg->workqueue) {
1572 tpg->tport_tpgt = tpgt;
1574 ret = core_tpg_register(&usbg_ops, wwn, &tpg->se_tpg, tpg,
1575 TRANSPORT_TPG_TYPE_NORMAL);
1577 destroy_workqueue(tpg->workqueue);
1581 the_only_tpg_I_currently_have = tpg;
1582 return &tpg->se_tpg;
1585 static void usbg_drop_tpg(struct se_portal_group *se_tpg)
1587 struct usbg_tpg *tpg = container_of(se_tpg,
1588 struct usbg_tpg, se_tpg);
1590 core_tpg_deregister(se_tpg);
1591 destroy_workqueue(tpg->workqueue);
1593 the_only_tpg_I_currently_have = NULL;
1596 static struct se_wwn *usbg_make_tport(
1597 struct target_fabric_configfs *tf,
1598 struct config_group *group,
1601 struct usbg_tport *tport;
1602 const char *wnn_name;
1605 wnn_name = usbg_check_wwn(name);
1607 return ERR_PTR(-EINVAL);
1609 tport = kzalloc(sizeof(struct usbg_tport), GFP_KERNEL);
1611 return ERR_PTR(-ENOMEM);
1612 tport->tport_wwpn = wwpn;
1613 snprintf(tport->tport_name, sizeof(tport->tport_name), "%s", wnn_name);
1614 return &tport->tport_wwn;
1617 static void usbg_drop_tport(struct se_wwn *wwn)
1619 struct usbg_tport *tport = container_of(wwn,
1620 struct usbg_tport, tport_wwn);
1625 * If somebody feels like dropping the version property, go ahead.
1627 static ssize_t usbg_wwn_show_attr_version(
1628 struct target_fabric_configfs *tf,
1631 return sprintf(page, "usb-gadget fabric module\n");
1633 TF_WWN_ATTR_RO(usbg, version);
1635 static struct configfs_attribute *usbg_wwn_attrs[] = {
1636 &usbg_wwn_version.attr,
1640 static ssize_t tcm_usbg_tpg_show_enable(
1641 struct se_portal_group *se_tpg,
1644 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1646 return snprintf(page, PAGE_SIZE, "%u\n", tpg->gadget_connect);
1649 static int usbg_attach(struct usbg_tpg *);
1650 static void usbg_detach(struct usbg_tpg *);
1652 static ssize_t tcm_usbg_tpg_store_enable(
1653 struct se_portal_group *se_tpg,
1657 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1661 ret = kstrtoul(page, 0, &op);
1667 if (op && tpg->gadget_connect)
1669 if (!op && !tpg->gadget_connect)
1673 ret = usbg_attach(tpg);
1679 tpg->gadget_connect = op;
1683 TF_TPG_BASE_ATTR(tcm_usbg, enable, S_IRUGO | S_IWUSR);
1685 static ssize_t tcm_usbg_tpg_show_nexus(
1686 struct se_portal_group *se_tpg,
1689 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1690 struct tcm_usbg_nexus *tv_nexus;
1693 mutex_lock(&tpg->tpg_mutex);
1694 tv_nexus = tpg->tpg_nexus;
1699 ret = snprintf(page, PAGE_SIZE, "%s\n",
1700 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1702 mutex_unlock(&tpg->tpg_mutex);
1706 static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name)
1708 struct se_portal_group *se_tpg;
1709 struct tcm_usbg_nexus *tv_nexus;
1712 mutex_lock(&tpg->tpg_mutex);
1713 if (tpg->tpg_nexus) {
1715 pr_debug("tpg->tpg_nexus already exists\n");
1718 se_tpg = &tpg->se_tpg;
1721 tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
1724 tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL);
1725 if (IS_ERR(tv_nexus->tvn_se_sess))
1729 * Since we are running in 'demo mode' this call with generate a
1730 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1731 * the SCSI Initiator port name of the passed configfs group 'name'.
1733 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1735 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1736 pr_debug("core_tpg_check_initiator_node_acl() failed"
1741 * Now register the TCM vHost virtual I_T Nexus as active.
1743 transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1744 tv_nexus->tvn_se_sess, tv_nexus);
1745 tpg->tpg_nexus = tv_nexus;
1746 mutex_unlock(&tpg->tpg_mutex);
1750 transport_free_session(tv_nexus->tvn_se_sess);
1754 mutex_unlock(&tpg->tpg_mutex);
1758 static int tcm_usbg_drop_nexus(struct usbg_tpg *tpg)
1760 struct se_session *se_sess;
1761 struct tcm_usbg_nexus *tv_nexus;
1764 mutex_lock(&tpg->tpg_mutex);
1765 tv_nexus = tpg->tpg_nexus;
1769 se_sess = tv_nexus->tvn_se_sess;
1773 if (atomic_read(&tpg->tpg_port_count)) {
1775 pr_err("Unable to remove Host I_T Nexus with"
1776 " active TPG port count: %d\n",
1777 atomic_read(&tpg->tpg_port_count));
1781 pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
1782 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1784 * Release the SCSI I_T Nexus to the emulated vHost Target Port
1786 transport_deregister_session(tv_nexus->tvn_se_sess);
1787 tpg->tpg_nexus = NULL;
1792 mutex_unlock(&tpg->tpg_mutex);
1796 static ssize_t tcm_usbg_tpg_store_nexus(
1797 struct se_portal_group *se_tpg,
1801 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1802 unsigned char i_port[USBG_NAMELEN], *ptr;
1805 if (!strncmp(page, "NULL", 4)) {
1806 ret = tcm_usbg_drop_nexus(tpg);
1807 return (!ret) ? count : ret;
1809 if (strlen(page) >= USBG_NAMELEN) {
1810 pr_err("Emulated NAA Sas Address: %s, exceeds"
1811 " max: %d\n", page, USBG_NAMELEN);
1814 snprintf(i_port, USBG_NAMELEN, "%s", page);
1816 ptr = strstr(i_port, "naa.");
1818 pr_err("Missing 'naa.' prefix\n");
1822 if (i_port[strlen(i_port) - 1] == '\n')
1823 i_port[strlen(i_port) - 1] = '\0';
1825 ret = tcm_usbg_make_nexus(tpg, &i_port[4]);
1830 TF_TPG_BASE_ATTR(tcm_usbg, nexus, S_IRUGO | S_IWUSR);
1832 static struct configfs_attribute *usbg_base_attrs[] = {
1833 &tcm_usbg_tpg_enable.attr,
1834 &tcm_usbg_tpg_nexus.attr,
1838 static int usbg_port_link(struct se_portal_group *se_tpg, struct se_lun *lun)
1840 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1842 atomic_inc(&tpg->tpg_port_count);
1843 smp_mb__after_atomic();
1847 static void usbg_port_unlink(struct se_portal_group *se_tpg,
1848 struct se_lun *se_lun)
1850 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1852 atomic_dec(&tpg->tpg_port_count);
1853 smp_mb__after_atomic();
1856 static int usbg_check_stop_free(struct se_cmd *se_cmd)
1858 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1861 kref_put(&cmd->ref, usbg_cmd_release);
1865 static const struct target_core_fabric_ops usbg_ops = {
1866 .module = THIS_MODULE,
1867 .name = "usb_gadget",
1868 .get_fabric_name = usbg_get_fabric_name,
1869 .get_fabric_proto_ident = usbg_get_fabric_proto_ident,
1870 .tpg_get_wwn = usbg_get_fabric_wwn,
1871 .tpg_get_tag = usbg_get_tag,
1872 .tpg_get_default_depth = usbg_get_default_depth,
1873 .tpg_get_pr_transport_id = usbg_get_pr_transport_id,
1874 .tpg_get_pr_transport_id_len = usbg_get_pr_transport_id_len,
1875 .tpg_parse_pr_out_transport_id = usbg_parse_pr_out_transport_id,
1876 .tpg_check_demo_mode = usbg_check_true,
1877 .tpg_check_demo_mode_cache = usbg_check_false,
1878 .tpg_check_demo_mode_write_protect = usbg_check_false,
1879 .tpg_check_prod_mode_write_protect = usbg_check_false,
1880 .tpg_alloc_fabric_acl = usbg_alloc_fabric_acl,
1881 .tpg_release_fabric_acl = usbg_release_fabric_acl,
1882 .tpg_get_inst_index = usbg_tpg_get_inst_index,
1883 .release_cmd = usbg_release_cmd,
1884 .shutdown_session = usbg_shutdown_session,
1885 .close_session = usbg_close_session,
1886 .sess_get_index = usbg_sess_get_index,
1887 .sess_get_initiator_sid = NULL,
1888 .write_pending = usbg_send_write_request,
1889 .write_pending_status = usbg_write_pending_status,
1890 .set_default_node_attributes = usbg_set_default_node_attrs,
1891 .get_task_tag = usbg_get_task_tag,
1892 .get_cmd_state = usbg_get_cmd_state,
1893 .queue_data_in = usbg_send_read_response,
1894 .queue_status = usbg_send_status_response,
1895 .queue_tm_rsp = usbg_queue_tm_rsp,
1896 .aborted_task = usbg_aborted_task,
1897 .check_stop_free = usbg_check_stop_free,
1899 .fabric_make_wwn = usbg_make_tport,
1900 .fabric_drop_wwn = usbg_drop_tport,
1901 .fabric_make_tpg = usbg_make_tpg,
1902 .fabric_drop_tpg = usbg_drop_tpg,
1903 .fabric_post_link = usbg_port_link,
1904 .fabric_pre_unlink = usbg_port_unlink,
1905 .fabric_make_np = NULL,
1906 .fabric_drop_np = NULL,
1907 .fabric_make_nodeacl = usbg_make_nodeacl,
1908 .fabric_drop_nodeacl = usbg_drop_nodeacl,
1910 .tfc_wwn_attrs = usbg_wwn_attrs,
1911 .tfc_tpg_base_attrs = usbg_base_attrs,
1914 /* Start gadget.c code */
1916 static struct usb_interface_descriptor bot_intf_desc = {
1917 .bLength = sizeof(bot_intf_desc),
1918 .bDescriptorType = USB_DT_INTERFACE,
1920 .bAlternateSetting = USB_G_ALT_INT_BBB,
1921 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1922 .bInterfaceSubClass = USB_SC_SCSI,
1923 .bInterfaceProtocol = USB_PR_BULK,
1926 static struct usb_interface_descriptor uasp_intf_desc = {
1927 .bLength = sizeof(uasp_intf_desc),
1928 .bDescriptorType = USB_DT_INTERFACE,
1930 .bAlternateSetting = USB_G_ALT_INT_UAS,
1931 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1932 .bInterfaceSubClass = USB_SC_SCSI,
1933 .bInterfaceProtocol = USB_PR_UAS,
1936 static struct usb_endpoint_descriptor uasp_bi_desc = {
1937 .bLength = USB_DT_ENDPOINT_SIZE,
1938 .bDescriptorType = USB_DT_ENDPOINT,
1939 .bEndpointAddress = USB_DIR_IN,
1940 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1941 .wMaxPacketSize = cpu_to_le16(512),
1944 static struct usb_endpoint_descriptor uasp_fs_bi_desc = {
1945 .bLength = USB_DT_ENDPOINT_SIZE,
1946 .bDescriptorType = USB_DT_ENDPOINT,
1947 .bEndpointAddress = USB_DIR_IN,
1948 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1951 static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc = {
1952 .bLength = sizeof(uasp_bi_pipe_desc),
1953 .bDescriptorType = USB_DT_PIPE_USAGE,
1954 .bPipeID = DATA_IN_PIPE_ID,
1957 static struct usb_endpoint_descriptor uasp_ss_bi_desc = {
1958 .bLength = USB_DT_ENDPOINT_SIZE,
1959 .bDescriptorType = USB_DT_ENDPOINT,
1960 .bEndpointAddress = USB_DIR_IN,
1961 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1962 .wMaxPacketSize = cpu_to_le16(1024),
1965 static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc = {
1966 .bLength = sizeof(uasp_bi_ep_comp_desc),
1967 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1969 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1970 .wBytesPerInterval = 0,
1973 static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc = {
1974 .bLength = sizeof(bot_bi_ep_comp_desc),
1975 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1979 static struct usb_endpoint_descriptor uasp_bo_desc = {
1980 .bLength = USB_DT_ENDPOINT_SIZE,
1981 .bDescriptorType = USB_DT_ENDPOINT,
1982 .bEndpointAddress = USB_DIR_OUT,
1983 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1984 .wMaxPacketSize = cpu_to_le16(512),
1987 static struct usb_endpoint_descriptor uasp_fs_bo_desc = {
1988 .bLength = USB_DT_ENDPOINT_SIZE,
1989 .bDescriptorType = USB_DT_ENDPOINT,
1990 .bEndpointAddress = USB_DIR_OUT,
1991 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1994 static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc = {
1995 .bLength = sizeof(uasp_bo_pipe_desc),
1996 .bDescriptorType = USB_DT_PIPE_USAGE,
1997 .bPipeID = DATA_OUT_PIPE_ID,
2000 static struct usb_endpoint_descriptor uasp_ss_bo_desc = {
2001 .bLength = USB_DT_ENDPOINT_SIZE,
2002 .bDescriptorType = USB_DT_ENDPOINT,
2003 .bEndpointAddress = USB_DIR_OUT,
2004 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2005 .wMaxPacketSize = cpu_to_le16(0x400),
2008 static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc = {
2009 .bLength = sizeof(uasp_bo_ep_comp_desc),
2010 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
2011 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
2014 static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc = {
2015 .bLength = sizeof(bot_bo_ep_comp_desc),
2016 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
2019 static struct usb_endpoint_descriptor uasp_status_desc = {
2020 .bLength = USB_DT_ENDPOINT_SIZE,
2021 .bDescriptorType = USB_DT_ENDPOINT,
2022 .bEndpointAddress = USB_DIR_IN,
2023 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2024 .wMaxPacketSize = cpu_to_le16(512),
2027 static struct usb_endpoint_descriptor uasp_fs_status_desc = {
2028 .bLength = USB_DT_ENDPOINT_SIZE,
2029 .bDescriptorType = USB_DT_ENDPOINT,
2030 .bEndpointAddress = USB_DIR_IN,
2031 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2034 static struct usb_pipe_usage_descriptor uasp_status_pipe_desc = {
2035 .bLength = sizeof(uasp_status_pipe_desc),
2036 .bDescriptorType = USB_DT_PIPE_USAGE,
2037 .bPipeID = STATUS_PIPE_ID,
2040 static struct usb_endpoint_descriptor uasp_ss_status_desc = {
2041 .bLength = USB_DT_ENDPOINT_SIZE,
2042 .bDescriptorType = USB_DT_ENDPOINT,
2043 .bEndpointAddress = USB_DIR_IN,
2044 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2045 .wMaxPacketSize = cpu_to_le16(1024),
2048 static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc = {
2049 .bLength = sizeof(uasp_status_in_ep_comp_desc),
2050 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
2051 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
2054 static struct usb_endpoint_descriptor uasp_cmd_desc = {
2055 .bLength = USB_DT_ENDPOINT_SIZE,
2056 .bDescriptorType = USB_DT_ENDPOINT,
2057 .bEndpointAddress = USB_DIR_OUT,
2058 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2059 .wMaxPacketSize = cpu_to_le16(512),
2062 static struct usb_endpoint_descriptor uasp_fs_cmd_desc = {
2063 .bLength = USB_DT_ENDPOINT_SIZE,
2064 .bDescriptorType = USB_DT_ENDPOINT,
2065 .bEndpointAddress = USB_DIR_OUT,
2066 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2069 static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc = {
2070 .bLength = sizeof(uasp_cmd_pipe_desc),
2071 .bDescriptorType = USB_DT_PIPE_USAGE,
2072 .bPipeID = CMD_PIPE_ID,
2075 static struct usb_endpoint_descriptor uasp_ss_cmd_desc = {
2076 .bLength = USB_DT_ENDPOINT_SIZE,
2077 .bDescriptorType = USB_DT_ENDPOINT,
2078 .bEndpointAddress = USB_DIR_OUT,
2079 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2080 .wMaxPacketSize = cpu_to_le16(1024),
2083 static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc = {
2084 .bLength = sizeof(uasp_cmd_comp_desc),
2085 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
2088 static struct usb_descriptor_header *uasp_fs_function_desc[] = {
2089 (struct usb_descriptor_header *) &bot_intf_desc,
2090 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
2091 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
2093 (struct usb_descriptor_header *) &uasp_intf_desc,
2094 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
2095 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
2096 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
2097 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
2098 (struct usb_descriptor_header *) &uasp_fs_status_desc,
2099 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
2100 (struct usb_descriptor_header *) &uasp_fs_cmd_desc,
2101 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
2105 static struct usb_descriptor_header *uasp_hs_function_desc[] = {
2106 (struct usb_descriptor_header *) &bot_intf_desc,
2107 (struct usb_descriptor_header *) &uasp_bi_desc,
2108 (struct usb_descriptor_header *) &uasp_bo_desc,
2110 (struct usb_descriptor_header *) &uasp_intf_desc,
2111 (struct usb_descriptor_header *) &uasp_bi_desc,
2112 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
2113 (struct usb_descriptor_header *) &uasp_bo_desc,
2114 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
2115 (struct usb_descriptor_header *) &uasp_status_desc,
2116 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
2117 (struct usb_descriptor_header *) &uasp_cmd_desc,
2118 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
2122 static struct usb_descriptor_header *uasp_ss_function_desc[] = {
2123 (struct usb_descriptor_header *) &bot_intf_desc,
2124 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
2125 (struct usb_descriptor_header *) &bot_bi_ep_comp_desc,
2126 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
2127 (struct usb_descriptor_header *) &bot_bo_ep_comp_desc,
2129 (struct usb_descriptor_header *) &uasp_intf_desc,
2130 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
2131 (struct usb_descriptor_header *) &uasp_bi_ep_comp_desc,
2132 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
2133 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
2134 (struct usb_descriptor_header *) &uasp_bo_ep_comp_desc,
2135 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
2136 (struct usb_descriptor_header *) &uasp_ss_status_desc,
2137 (struct usb_descriptor_header *) &uasp_status_in_ep_comp_desc,
2138 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
2139 (struct usb_descriptor_header *) &uasp_ss_cmd_desc,
2140 (struct usb_descriptor_header *) &uasp_cmd_comp_desc,
2141 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
2145 #define UAS_VENDOR_ID 0x0525 /* NetChip */
2146 #define UAS_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
2148 static struct usb_device_descriptor usbg_device_desc = {
2149 .bLength = sizeof(usbg_device_desc),
2150 .bDescriptorType = USB_DT_DEVICE,
2151 .bcdUSB = cpu_to_le16(0x0200),
2152 .bDeviceClass = USB_CLASS_PER_INTERFACE,
2153 .idVendor = cpu_to_le16(UAS_VENDOR_ID),
2154 .idProduct = cpu_to_le16(UAS_PRODUCT_ID),
2155 .bNumConfigurations = 1,
2158 static struct usb_string usbg_us_strings[] = {
2159 [USB_GADGET_MANUFACTURER_IDX].s = "Target Manufactor",
2160 [USB_GADGET_PRODUCT_IDX].s = "Target Product",
2161 [USB_GADGET_SERIAL_IDX].s = "000000000001",
2162 [USB_G_STR_CONFIG].s = "default config",
2163 [USB_G_STR_INT_UAS].s = "USB Attached SCSI",
2164 [USB_G_STR_INT_BBB].s = "Bulk Only Transport",
2168 static struct usb_gadget_strings usbg_stringtab = {
2170 .strings = usbg_us_strings,
2173 static struct usb_gadget_strings *usbg_strings[] = {
2178 static int guas_unbind(struct usb_composite_dev *cdev)
2183 static struct usb_configuration usbg_config_driver = {
2184 .label = "Linux Target",
2185 .bConfigurationValue = 1,
2186 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
2189 static void give_back_ep(struct usb_ep **pep)
2191 struct usb_ep *ep = *pep;
2194 ep->driver_data = NULL;
2197 static int usbg_bind(struct usb_configuration *c, struct usb_function *f)
2199 struct f_uas *fu = to_f_uas(f);
2200 struct usb_gadget *gadget = c->cdev->gadget;
2205 iface = usb_interface_id(c, f);
2209 bot_intf_desc.bInterfaceNumber = iface;
2210 uasp_intf_desc.bInterfaceNumber = iface;
2212 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bi_desc,
2213 &uasp_bi_ep_comp_desc);
2217 ep->driver_data = fu;
2220 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bo_desc,
2221 &uasp_bo_ep_comp_desc);
2224 ep->driver_data = fu;
2227 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_status_desc,
2228 &uasp_status_in_ep_comp_desc);
2231 ep->driver_data = fu;
2234 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_cmd_desc,
2235 &uasp_cmd_comp_desc);
2238 ep->driver_data = fu;
2241 /* Assume endpoint addresses are the same for both speeds */
2242 uasp_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2243 uasp_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2244 uasp_status_desc.bEndpointAddress =
2245 uasp_ss_status_desc.bEndpointAddress;
2246 uasp_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2248 uasp_fs_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2249 uasp_fs_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2250 uasp_fs_status_desc.bEndpointAddress =
2251 uasp_ss_status_desc.bEndpointAddress;
2252 uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2254 ret = usb_assign_descriptors(f, uasp_fs_function_desc,
2255 uasp_hs_function_desc, uasp_ss_function_desc);
2261 pr_err("Can't claim all required eps\n");
2263 give_back_ep(&fu->ep_in);
2264 give_back_ep(&fu->ep_out);
2265 give_back_ep(&fu->ep_status);
2266 give_back_ep(&fu->ep_cmd);
2270 static void usbg_unbind(struct usb_configuration *c, struct usb_function *f)
2272 struct f_uas *fu = to_f_uas(f);
2274 usb_free_all_descriptors(f);
2278 struct guas_setup_wq {
2279 struct work_struct work;
2284 static void usbg_delayed_set_alt(struct work_struct *wq)
2286 struct guas_setup_wq *work = container_of(wq, struct guas_setup_wq,
2288 struct f_uas *fu = work->fu;
2289 int alt = work->alt;
2293 if (fu->flags & USBG_IS_BOT)
2294 bot_cleanup_old_alt(fu);
2295 if (fu->flags & USBG_IS_UAS)
2296 uasp_cleanup_old_alt(fu);
2298 if (alt == USB_G_ALT_INT_BBB)
2300 else if (alt == USB_G_ALT_INT_UAS)
2302 usb_composite_setup_continue(fu->function.config->cdev);
2305 static int usbg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2307 struct f_uas *fu = to_f_uas(f);
2309 if ((alt == USB_G_ALT_INT_BBB) || (alt == USB_G_ALT_INT_UAS)) {
2310 struct guas_setup_wq *work;
2312 work = kmalloc(sizeof(*work), GFP_ATOMIC);
2315 INIT_WORK(&work->work, usbg_delayed_set_alt);
2318 schedule_work(&work->work);
2319 return USB_GADGET_DELAYED_STATUS;
2324 static void usbg_disable(struct usb_function *f)
2326 struct f_uas *fu = to_f_uas(f);
2328 if (fu->flags & USBG_IS_UAS)
2329 uasp_cleanup_old_alt(fu);
2330 else if (fu->flags & USBG_IS_BOT)
2331 bot_cleanup_old_alt(fu);
2335 static int usbg_setup(struct usb_function *f,
2336 const struct usb_ctrlrequest *ctrl)
2338 struct f_uas *fu = to_f_uas(f);
2340 if (!(fu->flags & USBG_IS_BOT))
2343 return usbg_bot_setup(f, ctrl);
2346 static int usbg_cfg_bind(struct usb_configuration *c)
2351 fu = kzalloc(sizeof(*fu), GFP_KERNEL);
2354 fu->function.name = "Target Function";
2355 fu->function.bind = usbg_bind;
2356 fu->function.unbind = usbg_unbind;
2357 fu->function.set_alt = usbg_set_alt;
2358 fu->function.setup = usbg_setup;
2359 fu->function.disable = usbg_disable;
2360 fu->tpg = the_only_tpg_I_currently_have;
2362 bot_intf_desc.iInterface = usbg_us_strings[USB_G_STR_INT_BBB].id;
2363 uasp_intf_desc.iInterface = usbg_us_strings[USB_G_STR_INT_UAS].id;
2365 ret = usb_add_function(c, &fu->function);
2375 static int usb_target_bind(struct usb_composite_dev *cdev)
2379 ret = usb_string_ids_tab(cdev, usbg_us_strings);
2383 usbg_device_desc.iManufacturer =
2384 usbg_us_strings[USB_GADGET_MANUFACTURER_IDX].id;
2385 usbg_device_desc.iProduct = usbg_us_strings[USB_GADGET_PRODUCT_IDX].id;
2386 usbg_device_desc.iSerialNumber =
2387 usbg_us_strings[USB_GADGET_SERIAL_IDX].id;
2388 usbg_config_driver.iConfiguration =
2389 usbg_us_strings[USB_G_STR_CONFIG].id;
2391 ret = usb_add_config(cdev, &usbg_config_driver,
2395 usb_composite_overwrite_options(cdev, &coverwrite);
2399 static struct usb_composite_driver usbg_driver = {
2401 .dev = &usbg_device_desc,
2402 .strings = usbg_strings,
2403 .max_speed = USB_SPEED_SUPER,
2404 .bind = usb_target_bind,
2405 .unbind = guas_unbind,
2408 static int usbg_attach(struct usbg_tpg *tpg)
2410 return usb_composite_probe(&usbg_driver);
2413 static void usbg_detach(struct usbg_tpg *tpg)
2415 usb_composite_unregister(&usbg_driver);
2418 static int __init usb_target_gadget_init(void)
2420 return target_register_template(&usbg_ops);
2422 module_init(usb_target_gadget_init);
2424 static void __exit usb_target_gadget_exit(void)
2426 target_unregister_template(&usbg_ops);
2428 module_exit(usb_target_gadget_exit);
2430 MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");
2431 MODULE_DESCRIPTION("usb-gadget fabric");
2432 MODULE_LICENSE("GPL v2");