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 <asm/unaligned.h>
28 #define TPG_INSTANCES 1
31 struct usb_function_instance *func_inst;
35 static struct tpg_instance tpg_instances[TPG_INSTANCES];
37 static DEFINE_MUTEX(tpg_instances_lock);
39 static inline struct f_uas *to_f_uas(struct usb_function *f)
41 return container_of(f, struct f_uas, function);
44 static void usbg_cmd_release(struct kref *);
46 static inline void usbg_cleanup_cmd(struct usbg_cmd *cmd)
48 kref_put(&cmd->ref, usbg_cmd_release);
51 /* Start bot.c code */
53 static int bot_enqueue_cmd_cbw(struct f_uas *fu)
57 if (fu->flags & USBG_BOT_CMD_PEND)
60 ret = usb_ep_queue(fu->ep_out, fu->cmd.req, GFP_ATOMIC);
62 fu->flags |= USBG_BOT_CMD_PEND;
66 static void bot_status_complete(struct usb_ep *ep, struct usb_request *req)
68 struct usbg_cmd *cmd = req->context;
69 struct f_uas *fu = cmd->fu;
71 usbg_cleanup_cmd(cmd);
72 if (req->status < 0) {
73 pr_err("ERR %s(%d)\n", __func__, __LINE__);
77 /* CSW completed, wait for next CBW */
78 bot_enqueue_cmd_cbw(fu);
81 static void bot_enqueue_sense_code(struct f_uas *fu, struct usbg_cmd *cmd)
83 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
85 unsigned int csw_stat;
87 csw_stat = cmd->csw_code;
88 csw->Tag = cmd->bot_tag;
89 csw->Status = csw_stat;
90 fu->bot_status.req->context = cmd;
91 ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_ATOMIC);
93 pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
96 static void bot_err_compl(struct usb_ep *ep, struct usb_request *req)
98 struct usbg_cmd *cmd = req->context;
99 struct f_uas *fu = cmd->fu;
102 pr_err("ERR %s(%d)\n", __func__, __LINE__);
105 if (cmd->data_len > ep->maxpacket) {
106 req->length = ep->maxpacket;
107 cmd->data_len -= ep->maxpacket;
109 req->length = cmd->data_len;
113 usb_ep_queue(ep, req, GFP_ATOMIC);
116 bot_enqueue_sense_code(fu, cmd);
119 static void bot_send_bad_status(struct usbg_cmd *cmd)
121 struct f_uas *fu = cmd->fu;
122 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
123 struct usb_request *req;
126 csw->Residue = cpu_to_le32(cmd->data_len);
131 req = fu->bot_req_in;
134 req = fu->bot_req_out;
137 if (cmd->data_len > fu->ep_in->maxpacket) {
138 req->length = ep->maxpacket;
139 cmd->data_len -= ep->maxpacket;
141 req->length = cmd->data_len;
144 req->complete = bot_err_compl;
146 req->buf = fu->cmd.buf;
147 usb_ep_queue(ep, req, GFP_KERNEL);
149 bot_enqueue_sense_code(fu, cmd);
153 static int bot_send_status(struct usbg_cmd *cmd, bool moved_data)
155 struct f_uas *fu = cmd->fu;
156 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
159 if (cmd->se_cmd.scsi_status == SAM_STAT_GOOD) {
160 if (!moved_data && cmd->data_len) {
162 * the host wants to move data, we don't. Fill / empty
163 * the pipe and then send the csw with reside set.
165 cmd->csw_code = US_BULK_STAT_OK;
166 bot_send_bad_status(cmd);
170 csw->Tag = cmd->bot_tag;
171 csw->Residue = cpu_to_le32(0);
172 csw->Status = US_BULK_STAT_OK;
173 fu->bot_status.req->context = cmd;
175 ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_KERNEL);
177 pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
179 cmd->csw_code = US_BULK_STAT_FAIL;
180 bot_send_bad_status(cmd);
186 * Called after command (no data transfer) or after the write (to device)
187 * operation is completed
189 static int bot_send_status_response(struct usbg_cmd *cmd)
191 bool moved_data = false;
195 return bot_send_status(cmd, moved_data);
198 /* Read request completed, now we have to send the CSW */
199 static void bot_read_compl(struct usb_ep *ep, struct usb_request *req)
201 struct usbg_cmd *cmd = req->context;
204 pr_err("ERR %s(%d)\n", __func__, __LINE__);
206 bot_send_status(cmd, true);
209 static int bot_send_read_response(struct usbg_cmd *cmd)
211 struct f_uas *fu = cmd->fu;
212 struct se_cmd *se_cmd = &cmd->se_cmd;
213 struct usb_gadget *gadget = fuas_to_gadget(fu);
216 if (!cmd->data_len) {
217 cmd->csw_code = US_BULK_STAT_PHASE;
218 bot_send_bad_status(cmd);
222 if (!gadget->sg_supported) {
223 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
227 sg_copy_to_buffer(se_cmd->t_data_sg,
228 se_cmd->t_data_nents,
230 se_cmd->data_length);
232 fu->bot_req_in->buf = cmd->data_buf;
234 fu->bot_req_in->buf = NULL;
235 fu->bot_req_in->num_sgs = se_cmd->t_data_nents;
236 fu->bot_req_in->sg = se_cmd->t_data_sg;
239 fu->bot_req_in->complete = bot_read_compl;
240 fu->bot_req_in->length = se_cmd->data_length;
241 fu->bot_req_in->context = cmd;
242 ret = usb_ep_queue(fu->ep_in, fu->bot_req_in, GFP_ATOMIC);
244 pr_err("%s(%d)\n", __func__, __LINE__);
248 static void usbg_data_write_cmpl(struct usb_ep *, struct usb_request *);
249 static int usbg_prepare_w_request(struct usbg_cmd *, struct usb_request *);
251 static int bot_send_write_request(struct usbg_cmd *cmd)
253 struct f_uas *fu = cmd->fu;
254 struct se_cmd *se_cmd = &cmd->se_cmd;
255 struct usb_gadget *gadget = fuas_to_gadget(fu);
258 init_completion(&cmd->write_complete);
261 if (!cmd->data_len) {
262 cmd->csw_code = US_BULK_STAT_PHASE;
266 if (!gadget->sg_supported) {
267 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_KERNEL);
271 fu->bot_req_out->buf = cmd->data_buf;
273 fu->bot_req_out->buf = NULL;
274 fu->bot_req_out->num_sgs = se_cmd->t_data_nents;
275 fu->bot_req_out->sg = se_cmd->t_data_sg;
278 fu->bot_req_out->complete = usbg_data_write_cmpl;
279 fu->bot_req_out->length = se_cmd->data_length;
280 fu->bot_req_out->context = cmd;
282 ret = usbg_prepare_w_request(cmd, fu->bot_req_out);
285 ret = usb_ep_queue(fu->ep_out, fu->bot_req_out, GFP_KERNEL);
287 pr_err("%s(%d)\n", __func__, __LINE__);
289 wait_for_completion(&cmd->write_complete);
290 target_execute_cmd(se_cmd);
295 static int bot_submit_command(struct f_uas *, void *, unsigned int);
297 static void bot_cmd_complete(struct usb_ep *ep, struct usb_request *req)
299 struct f_uas *fu = req->context;
302 fu->flags &= ~USBG_BOT_CMD_PEND;
307 ret = bot_submit_command(fu, req->buf, req->actual);
309 pr_err("%s(%d): %d\n", __func__, __LINE__, ret);
312 static int bot_prepare_reqs(struct f_uas *fu)
316 fu->bot_req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
320 fu->bot_req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
321 if (!fu->bot_req_out)
324 fu->cmd.req = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
328 fu->bot_status.req = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
329 if (!fu->bot_status.req)
332 fu->bot_status.req->buf = &fu->bot_status.csw;
333 fu->bot_status.req->length = US_BULK_CS_WRAP_LEN;
334 fu->bot_status.req->complete = bot_status_complete;
335 fu->bot_status.csw.Signature = cpu_to_le32(US_BULK_CS_SIGN);
337 fu->cmd.buf = kmalloc(fu->ep_out->maxpacket, GFP_KERNEL);
341 fu->cmd.req->complete = bot_cmd_complete;
342 fu->cmd.req->buf = fu->cmd.buf;
343 fu->cmd.req->length = fu->ep_out->maxpacket;
344 fu->cmd.req->context = fu;
346 ret = bot_enqueue_cmd_cbw(fu);
354 usb_ep_free_request(fu->ep_in, fu->bot_status.req);
356 usb_ep_free_request(fu->ep_out, fu->cmd.req);
359 usb_ep_free_request(fu->ep_out, fu->bot_req_out);
360 fu->bot_req_out = NULL;
362 usb_ep_free_request(fu->ep_in, fu->bot_req_in);
363 fu->bot_req_in = NULL;
365 pr_err("BOT: endpoint setup failed\n");
369 static void bot_cleanup_old_alt(struct f_uas *fu)
371 if (!(fu->flags & USBG_ENABLED))
374 usb_ep_disable(fu->ep_in);
375 usb_ep_disable(fu->ep_out);
380 usb_ep_free_request(fu->ep_in, fu->bot_req_in);
381 usb_ep_free_request(fu->ep_out, fu->bot_req_out);
382 usb_ep_free_request(fu->ep_out, fu->cmd.req);
383 usb_ep_free_request(fu->ep_out, fu->bot_status.req);
387 fu->bot_req_in = NULL;
388 fu->bot_req_out = NULL;
390 fu->bot_status.req = NULL;
394 static void bot_set_alt(struct f_uas *fu)
396 struct usb_function *f = &fu->function;
397 struct usb_gadget *gadget = f->config->cdev->gadget;
400 fu->flags = USBG_IS_BOT;
402 config_ep_by_speed(gadget, f, fu->ep_in);
403 ret = usb_ep_enable(fu->ep_in);
407 config_ep_by_speed(gadget, f, fu->ep_out);
408 ret = usb_ep_enable(fu->ep_out);
412 ret = bot_prepare_reqs(fu);
415 fu->flags |= USBG_ENABLED;
416 pr_info("Using the BOT protocol\n");
419 usb_ep_disable(fu->ep_out);
421 usb_ep_disable(fu->ep_in);
423 fu->flags = USBG_IS_BOT;
426 static int usbg_bot_setup(struct usb_function *f,
427 const struct usb_ctrlrequest *ctrl)
429 struct f_uas *fu = to_f_uas(f);
430 struct usb_composite_dev *cdev = f->config->cdev;
431 u16 w_value = le16_to_cpu(ctrl->wValue);
432 u16 w_length = le16_to_cpu(ctrl->wLength);
436 switch (ctrl->bRequest) {
437 case US_BULK_GET_MAX_LUN:
438 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_CLASS |
439 USB_RECIP_INTERFACE))
446 luns = atomic_read(&fu->tpg->tpg_port_count);
448 pr_err("No LUNs configured?\n");
452 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
453 * accessed. The upper limit is 0xf
457 pr_info_once("Limiting the number of luns to 16\n");
460 ret_lun = cdev->req->buf;
462 cdev->req->length = 1;
463 return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
465 case US_BULK_RESET_REQUEST:
466 /* XXX maybe we should remove previous requests for IN + OUT */
467 bot_enqueue_cmd_cbw(fu);
473 /* Start uas.c code */
475 static void uasp_cleanup_one_stream(struct f_uas *fu, struct uas_stream *stream)
477 /* We have either all three allocated or none */
481 usb_ep_free_request(fu->ep_in, stream->req_in);
482 usb_ep_free_request(fu->ep_out, stream->req_out);
483 usb_ep_free_request(fu->ep_status, stream->req_status);
485 stream->req_in = NULL;
486 stream->req_out = NULL;
487 stream->req_status = NULL;
490 static void uasp_free_cmdreq(struct f_uas *fu)
492 usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
498 static void uasp_cleanup_old_alt(struct f_uas *fu)
502 if (!(fu->flags & USBG_ENABLED))
505 usb_ep_disable(fu->ep_in);
506 usb_ep_disable(fu->ep_out);
507 usb_ep_disable(fu->ep_status);
508 usb_ep_disable(fu->ep_cmd);
510 for (i = 0; i < UASP_SS_EP_COMP_NUM_STREAMS; i++)
511 uasp_cleanup_one_stream(fu, &fu->stream[i]);
512 uasp_free_cmdreq(fu);
515 static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req);
517 static int uasp_prepare_r_request(struct usbg_cmd *cmd)
519 struct se_cmd *se_cmd = &cmd->se_cmd;
520 struct f_uas *fu = cmd->fu;
521 struct usb_gadget *gadget = fuas_to_gadget(fu);
522 struct uas_stream *stream = cmd->stream;
524 if (!gadget->sg_supported) {
525 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
529 sg_copy_to_buffer(se_cmd->t_data_sg,
530 se_cmd->t_data_nents,
532 se_cmd->data_length);
534 stream->req_in->buf = cmd->data_buf;
536 stream->req_in->buf = NULL;
537 stream->req_in->num_sgs = se_cmd->t_data_nents;
538 stream->req_in->sg = se_cmd->t_data_sg;
541 stream->req_in->complete = uasp_status_data_cmpl;
542 stream->req_in->length = se_cmd->data_length;
543 stream->req_in->context = cmd;
545 cmd->state = UASP_SEND_STATUS;
549 static void uasp_prepare_status(struct usbg_cmd *cmd)
551 struct se_cmd *se_cmd = &cmd->se_cmd;
552 struct sense_iu *iu = &cmd->sense_iu;
553 struct uas_stream *stream = cmd->stream;
555 cmd->state = UASP_QUEUE_COMMAND;
556 iu->iu_id = IU_ID_STATUS;
557 iu->tag = cpu_to_be16(cmd->tag);
560 * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?);
562 iu->len = cpu_to_be16(se_cmd->scsi_sense_length);
563 iu->status = se_cmd->scsi_status;
564 stream->req_status->context = cmd;
565 stream->req_status->length = se_cmd->scsi_sense_length + 16;
566 stream->req_status->buf = iu;
567 stream->req_status->complete = uasp_status_data_cmpl;
570 static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req)
572 struct usbg_cmd *cmd = req->context;
573 struct uas_stream *stream = cmd->stream;
574 struct f_uas *fu = cmd->fu;
580 switch (cmd->state) {
582 ret = uasp_prepare_r_request(cmd);
585 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
587 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
590 case UASP_RECEIVE_DATA:
591 ret = usbg_prepare_w_request(cmd, stream->req_out);
594 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
596 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
599 case UASP_SEND_STATUS:
600 uasp_prepare_status(cmd);
601 ret = usb_ep_queue(fu->ep_status, stream->req_status,
604 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
607 case UASP_QUEUE_COMMAND:
608 usbg_cleanup_cmd(cmd);
609 usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
618 usbg_cleanup_cmd(cmd);
621 static int uasp_send_status_response(struct usbg_cmd *cmd)
623 struct f_uas *fu = cmd->fu;
624 struct uas_stream *stream = cmd->stream;
625 struct sense_iu *iu = &cmd->sense_iu;
627 iu->tag = cpu_to_be16(cmd->tag);
628 stream->req_status->complete = uasp_status_data_cmpl;
629 stream->req_status->context = cmd;
631 uasp_prepare_status(cmd);
632 return usb_ep_queue(fu->ep_status, stream->req_status, GFP_ATOMIC);
635 static int uasp_send_read_response(struct usbg_cmd *cmd)
637 struct f_uas *fu = cmd->fu;
638 struct uas_stream *stream = cmd->stream;
639 struct sense_iu *iu = &cmd->sense_iu;
644 iu->tag = cpu_to_be16(cmd->tag);
645 if (fu->flags & USBG_USE_STREAMS) {
647 ret = uasp_prepare_r_request(cmd);
650 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
652 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
653 kfree(cmd->data_buf);
654 cmd->data_buf = NULL;
659 iu->iu_id = IU_ID_READ_READY;
660 iu->tag = cpu_to_be16(cmd->tag);
662 stream->req_status->complete = uasp_status_data_cmpl;
663 stream->req_status->context = cmd;
665 cmd->state = UASP_SEND_DATA;
666 stream->req_status->buf = iu;
667 stream->req_status->length = sizeof(struct iu);
669 ret = usb_ep_queue(fu->ep_status, stream->req_status,
672 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
678 static int uasp_send_write_request(struct usbg_cmd *cmd)
680 struct f_uas *fu = cmd->fu;
681 struct se_cmd *se_cmd = &cmd->se_cmd;
682 struct uas_stream *stream = cmd->stream;
683 struct sense_iu *iu = &cmd->sense_iu;
686 init_completion(&cmd->write_complete);
689 iu->tag = cpu_to_be16(cmd->tag);
691 if (fu->flags & USBG_USE_STREAMS) {
693 ret = usbg_prepare_w_request(cmd, stream->req_out);
696 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
698 pr_err("%s(%d)\n", __func__, __LINE__);
702 iu->iu_id = IU_ID_WRITE_READY;
703 iu->tag = cpu_to_be16(cmd->tag);
705 stream->req_status->complete = uasp_status_data_cmpl;
706 stream->req_status->context = cmd;
708 cmd->state = UASP_RECEIVE_DATA;
709 stream->req_status->buf = iu;
710 stream->req_status->length = sizeof(struct iu);
712 ret = usb_ep_queue(fu->ep_status, stream->req_status,
715 pr_err("%s(%d)\n", __func__, __LINE__);
718 wait_for_completion(&cmd->write_complete);
719 target_execute_cmd(se_cmd);
724 static int usbg_submit_command(struct f_uas *, void *, unsigned int);
726 static void uasp_cmd_complete(struct usb_ep *ep, struct usb_request *req)
728 struct f_uas *fu = req->context;
734 ret = usbg_submit_command(fu, req->buf, req->actual);
736 * Once we tune for performance enqueue the command req here again so
737 * we can receive a second command while we processing this one. Pay
738 * attention to properly sync STAUS endpoint with DATA IN + OUT so you
743 usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
746 static int uasp_alloc_stream_res(struct f_uas *fu, struct uas_stream *stream)
748 stream->req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
752 stream->req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
753 if (!stream->req_out)
756 stream->req_status = usb_ep_alloc_request(fu->ep_status, GFP_KERNEL);
757 if (!stream->req_status)
762 usb_ep_free_request(fu->ep_status, stream->req_status);
763 stream->req_status = NULL;
765 usb_ep_free_request(fu->ep_out, stream->req_out);
766 stream->req_out = NULL;
771 static int uasp_alloc_cmd(struct f_uas *fu)
773 fu->cmd.req = usb_ep_alloc_request(fu->ep_cmd, GFP_KERNEL);
777 fu->cmd.buf = kmalloc(fu->ep_cmd->maxpacket, GFP_KERNEL);
781 fu->cmd.req->complete = uasp_cmd_complete;
782 fu->cmd.req->buf = fu->cmd.buf;
783 fu->cmd.req->length = fu->ep_cmd->maxpacket;
784 fu->cmd.req->context = fu;
788 usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
793 static void uasp_setup_stream_res(struct f_uas *fu, int max_streams)
797 for (i = 0; i < max_streams; i++) {
798 struct uas_stream *s = &fu->stream[i];
800 s->req_in->stream_id = i + 1;
801 s->req_out->stream_id = i + 1;
802 s->req_status->stream_id = i + 1;
806 static int uasp_prepare_reqs(struct f_uas *fu)
812 if (fu->flags & USBG_USE_STREAMS)
813 max_streams = UASP_SS_EP_COMP_NUM_STREAMS;
817 for (i = 0; i < max_streams; i++) {
818 ret = uasp_alloc_stream_res(fu, &fu->stream[i]);
823 ret = uasp_alloc_cmd(fu);
825 goto err_free_stream;
826 uasp_setup_stream_res(fu, max_streams);
828 ret = usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
830 goto err_free_stream;
835 uasp_free_cmdreq(fu);
840 uasp_cleanup_one_stream(fu, &fu->stream[i - 1]);
844 pr_err("UASP: endpoint setup failed\n");
848 static void uasp_set_alt(struct f_uas *fu)
850 struct usb_function *f = &fu->function;
851 struct usb_gadget *gadget = f->config->cdev->gadget;
854 fu->flags = USBG_IS_UAS;
856 if (gadget->speed == USB_SPEED_SUPER)
857 fu->flags |= USBG_USE_STREAMS;
859 config_ep_by_speed(gadget, f, fu->ep_in);
860 ret = usb_ep_enable(fu->ep_in);
864 config_ep_by_speed(gadget, f, fu->ep_out);
865 ret = usb_ep_enable(fu->ep_out);
869 config_ep_by_speed(gadget, f, fu->ep_cmd);
870 ret = usb_ep_enable(fu->ep_cmd);
873 config_ep_by_speed(gadget, f, fu->ep_status);
874 ret = usb_ep_enable(fu->ep_status);
878 ret = uasp_prepare_reqs(fu);
881 fu->flags |= USBG_ENABLED;
883 pr_info("Using the UAS protocol\n");
886 usb_ep_disable(fu->ep_status);
888 usb_ep_disable(fu->ep_cmd);
890 usb_ep_disable(fu->ep_out);
892 usb_ep_disable(fu->ep_in);
897 static int get_cmd_dir(const unsigned char *cdb)
909 case SERVICE_ACTION_IN_16:
911 case PERSISTENT_RESERVE_IN:
912 case SECURITY_PROTOCOL_IN:
913 case ACCESS_CONTROL_IN:
915 case READ_BLOCK_LIMITS:
919 case READ_FORMAT_CAPACITIES:
921 ret = DMA_FROM_DEVICE;
931 case WRITE_VERIFY_12:
932 case PERSISTENT_RESERVE_OUT:
933 case MAINTENANCE_OUT:
934 case SECURITY_PROTOCOL_OUT:
935 case ACCESS_CONTROL_OUT:
938 case ALLOW_MEDIUM_REMOVAL:
939 case TEST_UNIT_READY:
940 case SYNCHRONIZE_CACHE:
947 case WRITE_FILEMARKS:
951 #define CMD_DIR_MSG "target: Unknown data direction for SCSI Opcode 0x%02x\n"
952 pr_warn(CMD_DIR_MSG, cdb[0]);
959 static void usbg_data_write_cmpl(struct usb_ep *ep, struct usb_request *req)
961 struct usbg_cmd *cmd = req->context;
962 struct se_cmd *se_cmd = &cmd->se_cmd;
964 if (req->status < 0) {
965 pr_err("%s() state %d transfer failed\n", __func__, cmd->state);
969 if (req->num_sgs == 0) {
970 sg_copy_from_buffer(se_cmd->t_data_sg,
971 se_cmd->t_data_nents,
973 se_cmd->data_length);
976 complete(&cmd->write_complete);
980 usbg_cleanup_cmd(cmd);
983 static int usbg_prepare_w_request(struct usbg_cmd *cmd, struct usb_request *req)
985 struct se_cmd *se_cmd = &cmd->se_cmd;
986 struct f_uas *fu = cmd->fu;
987 struct usb_gadget *gadget = fuas_to_gadget(fu);
989 if (!gadget->sg_supported) {
990 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
994 req->buf = cmd->data_buf;
997 req->num_sgs = se_cmd->t_data_nents;
998 req->sg = se_cmd->t_data_sg;
1001 req->complete = usbg_data_write_cmpl;
1002 req->length = se_cmd->data_length;
1007 static int usbg_send_status_response(struct se_cmd *se_cmd)
1009 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1011 struct f_uas *fu = cmd->fu;
1013 if (fu->flags & USBG_IS_BOT)
1014 return bot_send_status_response(cmd);
1016 return uasp_send_status_response(cmd);
1019 static int usbg_send_write_request(struct se_cmd *se_cmd)
1021 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1023 struct f_uas *fu = cmd->fu;
1025 if (fu->flags & USBG_IS_BOT)
1026 return bot_send_write_request(cmd);
1028 return uasp_send_write_request(cmd);
1031 static int usbg_send_read_response(struct se_cmd *se_cmd)
1033 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1035 struct f_uas *fu = cmd->fu;
1037 if (fu->flags & USBG_IS_BOT)
1038 return bot_send_read_response(cmd);
1040 return uasp_send_read_response(cmd);
1043 static void usbg_cmd_work(struct work_struct *work)
1045 struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
1046 struct se_cmd *se_cmd;
1047 struct tcm_usbg_nexus *tv_nexus;
1048 struct usbg_tpg *tpg;
1051 se_cmd = &cmd->se_cmd;
1053 tv_nexus = tpg->tpg_nexus;
1054 dir = get_cmd_dir(cmd->cmd_buf);
1056 transport_init_se_cmd(se_cmd,
1057 tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
1058 tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
1059 cmd->prio_attr, cmd->sense_iu.sense);
1063 if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
1064 cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
1065 0, cmd->prio_attr, dir, TARGET_SCF_UNKNOWN_SIZE) < 0)
1071 transport_send_check_condition_and_sense(se_cmd,
1072 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1073 usbg_cleanup_cmd(cmd);
1076 static int usbg_submit_command(struct f_uas *fu,
1077 void *cmdbuf, unsigned int len)
1079 struct command_iu *cmd_iu = cmdbuf;
1080 struct usbg_cmd *cmd;
1081 struct usbg_tpg *tpg;
1082 struct tcm_usbg_nexus *tv_nexus;
1085 if (cmd_iu->iu_id != IU_ID_COMMAND) {
1086 pr_err("Unsupported type %d\n", cmd_iu->iu_id);
1090 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
1096 /* XXX until I figure out why I can't free in on complete */
1097 kref_init(&cmd->ref);
1098 kref_get(&cmd->ref);
1101 cmd_len = (cmd_iu->len & ~0x3) + 16;
1102 if (cmd_len > USBG_MAX_CMD)
1105 memcpy(cmd->cmd_buf, cmd_iu->cdb, cmd_len);
1107 cmd->tag = be16_to_cpup(&cmd_iu->tag);
1108 cmd->se_cmd.tag = cmd->tag;
1109 if (fu->flags & USBG_USE_STREAMS) {
1110 if (cmd->tag > UASP_SS_EP_COMP_NUM_STREAMS)
1113 cmd->stream = &fu->stream[0];
1115 cmd->stream = &fu->stream[cmd->tag - 1];
1117 cmd->stream = &fu->stream[0];
1120 tv_nexus = tpg->tpg_nexus;
1122 pr_err("Missing nexus, ignoring command\n");
1126 switch (cmd_iu->prio_attr & 0x7) {
1128 cmd->prio_attr = TCM_HEAD_TAG;
1130 case UAS_ORDERED_TAG:
1131 cmd->prio_attr = TCM_ORDERED_TAG;
1134 cmd->prio_attr = TCM_ACA_TAG;
1137 pr_debug_once("Unsupported prio_attr: %02x.\n",
1139 case UAS_SIMPLE_TAG:
1140 cmd->prio_attr = TCM_SIMPLE_TAG;
1144 cmd->unpacked_lun = scsilun_to_int(&cmd_iu->lun);
1146 INIT_WORK(&cmd->work, usbg_cmd_work);
1147 queue_work(tpg->workqueue, &cmd->work);
1155 static void bot_cmd_work(struct work_struct *work)
1157 struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
1158 struct se_cmd *se_cmd;
1159 struct tcm_usbg_nexus *tv_nexus;
1160 struct usbg_tpg *tpg;
1163 se_cmd = &cmd->se_cmd;
1165 tv_nexus = tpg->tpg_nexus;
1166 dir = get_cmd_dir(cmd->cmd_buf);
1168 transport_init_se_cmd(se_cmd,
1169 tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
1170 tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
1171 cmd->prio_attr, cmd->sense_iu.sense);
1175 if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
1176 cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
1177 cmd->data_len, cmd->prio_attr, dir, 0) < 0)
1183 transport_send_check_condition_and_sense(se_cmd,
1184 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1185 usbg_cleanup_cmd(cmd);
1188 static int bot_submit_command(struct f_uas *fu,
1189 void *cmdbuf, unsigned int len)
1191 struct bulk_cb_wrap *cbw = cmdbuf;
1192 struct usbg_cmd *cmd;
1193 struct usbg_tpg *tpg;
1194 struct tcm_usbg_nexus *tv_nexus;
1197 if (cbw->Signature != cpu_to_le32(US_BULK_CB_SIGN)) {
1198 pr_err("Wrong signature on CBW\n");
1202 pr_err("Wrong length for CBW\n");
1206 cmd_len = cbw->Length;
1207 if (cmd_len < 1 || cmd_len > 16)
1210 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
1216 /* XXX until I figure out why I can't free in on complete */
1217 kref_init(&cmd->ref);
1218 kref_get(&cmd->ref);
1222 memcpy(cmd->cmd_buf, cbw->CDB, cmd_len);
1224 cmd->bot_tag = cbw->Tag;
1226 tv_nexus = tpg->tpg_nexus;
1228 pr_err("Missing nexus, ignoring command\n");
1232 cmd->prio_attr = TCM_SIMPLE_TAG;
1233 cmd->unpacked_lun = cbw->Lun;
1234 cmd->is_read = cbw->Flags & US_BULK_FLAG_IN ? 1 : 0;
1235 cmd->data_len = le32_to_cpu(cbw->DataTransferLength);
1236 cmd->se_cmd.tag = le32_to_cpu(cmd->bot_tag);
1238 INIT_WORK(&cmd->work, bot_cmd_work);
1239 queue_work(tpg->workqueue, &cmd->work);
1247 /* Start fabric.c code */
1249 static int usbg_check_true(struct se_portal_group *se_tpg)
1254 static int usbg_check_false(struct se_portal_group *se_tpg)
1259 static char *usbg_get_fabric_name(void)
1261 return "usb_gadget";
1264 static char *usbg_get_fabric_wwn(struct se_portal_group *se_tpg)
1266 struct usbg_tpg *tpg = container_of(se_tpg,
1267 struct usbg_tpg, se_tpg);
1268 struct usbg_tport *tport = tpg->tport;
1270 return &tport->tport_name[0];
1273 static u16 usbg_get_tag(struct se_portal_group *se_tpg)
1275 struct usbg_tpg *tpg = container_of(se_tpg,
1276 struct usbg_tpg, se_tpg);
1277 return tpg->tport_tpgt;
1280 static u32 usbg_tpg_get_inst_index(struct se_portal_group *se_tpg)
1285 static void usbg_cmd_release(struct kref *ref)
1287 struct usbg_cmd *cmd = container_of(ref, struct usbg_cmd,
1290 transport_generic_free_cmd(&cmd->se_cmd, 0);
1293 static void usbg_release_cmd(struct se_cmd *se_cmd)
1295 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1297 kfree(cmd->data_buf);
1301 static int usbg_shutdown_session(struct se_session *se_sess)
1306 static void usbg_close_session(struct se_session *se_sess)
1310 static u32 usbg_sess_get_index(struct se_session *se_sess)
1316 * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be
1318 static int usbg_write_pending_status(struct se_cmd *se_cmd)
1323 static void usbg_set_default_node_attrs(struct se_node_acl *nacl)
1327 static int usbg_get_cmd_state(struct se_cmd *se_cmd)
1332 static void usbg_queue_tm_rsp(struct se_cmd *se_cmd)
1336 static void usbg_aborted_task(struct se_cmd *se_cmd)
1340 static const char *usbg_check_wwn(const char *name)
1345 n = strstr(name, "naa.");
1350 if (len == 0 || len > USBG_NAMELEN - 1)
1355 static int usbg_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
1357 if (!usbg_check_wwn(name))
1362 static struct se_portal_group *usbg_make_tpg(
1364 struct config_group *group,
1367 struct usbg_tport *tport = container_of(wwn, struct usbg_tport,
1369 struct usbg_tpg *tpg;
1372 struct f_tcm_opts *opts;
1375 if (strstr(name, "tpgt_") != name)
1376 return ERR_PTR(-EINVAL);
1377 if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX)
1378 return ERR_PTR(-EINVAL);
1380 mutex_lock(&tpg_instances_lock);
1381 for (i = 0; i < TPG_INSTANCES; ++i)
1382 if (tpg_instances[i].func_inst && !tpg_instances[i].tpg)
1384 if (i == TPG_INSTANCES)
1387 opts = container_of(tpg_instances[i].func_inst, struct f_tcm_opts,
1389 mutex_lock(&opts->dep_lock);
1393 if (opts->has_dep) {
1394 if (!try_module_get(opts->dependent))
1397 ret = configfs_depend_item_unlocked(
1399 &opts->func_inst.group.cg_item);
1404 tpg = kzalloc(sizeof(struct usbg_tpg), GFP_KERNEL);
1408 mutex_init(&tpg->tpg_mutex);
1409 atomic_set(&tpg->tpg_port_count, 0);
1410 tpg->workqueue = alloc_workqueue("tcm_usb_gadget", 0, 1);
1411 if (!tpg->workqueue)
1415 tpg->tport_tpgt = tpgt;
1418 * SPC doesn't assign a protocol identifier for USB-SCSI, so we
1419 * pretend to be SAS..
1421 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_SAS);
1423 goto free_workqueue;
1425 tpg_instances[i].tpg = tpg;
1426 tpg->fi = tpg_instances[i].func_inst;
1427 mutex_unlock(&opts->dep_lock);
1428 mutex_unlock(&tpg_instances_lock);
1429 return &tpg->se_tpg;
1432 destroy_workqueue(tpg->workqueue);
1437 module_put(opts->dependent);
1439 configfs_undepend_item_unlocked(&opts->func_inst.group.cg_item);
1441 mutex_unlock(&opts->dep_lock);
1443 mutex_unlock(&tpg_instances_lock);
1445 return ERR_PTR(ret);
1448 static int tcm_usbg_drop_nexus(struct usbg_tpg *);
1450 static void usbg_drop_tpg(struct se_portal_group *se_tpg)
1452 struct usbg_tpg *tpg = container_of(se_tpg,
1453 struct usbg_tpg, se_tpg);
1455 struct f_tcm_opts *opts;
1457 tcm_usbg_drop_nexus(tpg);
1458 core_tpg_deregister(se_tpg);
1459 destroy_workqueue(tpg->workqueue);
1461 mutex_lock(&tpg_instances_lock);
1462 for (i = 0; i < TPG_INSTANCES; ++i)
1463 if (tpg_instances[i].tpg == tpg)
1465 if (i < TPG_INSTANCES)
1466 tpg_instances[i].tpg = NULL;
1467 opts = container_of(tpg_instances[i].func_inst,
1468 struct f_tcm_opts, func_inst);
1469 mutex_lock(&opts->dep_lock);
1471 module_put(opts->dependent);
1473 configfs_undepend_item_unlocked(&opts->func_inst.group.cg_item);
1474 mutex_unlock(&opts->dep_lock);
1475 mutex_unlock(&tpg_instances_lock);
1480 static struct se_wwn *usbg_make_tport(
1481 struct target_fabric_configfs *tf,
1482 struct config_group *group,
1485 struct usbg_tport *tport;
1486 const char *wnn_name;
1489 wnn_name = usbg_check_wwn(name);
1491 return ERR_PTR(-EINVAL);
1493 tport = kzalloc(sizeof(struct usbg_tport), GFP_KERNEL);
1495 return ERR_PTR(-ENOMEM);
1497 tport->tport_wwpn = wwpn;
1498 snprintf(tport->tport_name, sizeof(tport->tport_name), "%s", wnn_name);
1499 return &tport->tport_wwn;
1502 static void usbg_drop_tport(struct se_wwn *wwn)
1504 struct usbg_tport *tport = container_of(wwn,
1505 struct usbg_tport, tport_wwn);
1510 * If somebody feels like dropping the version property, go ahead.
1512 static ssize_t usbg_wwn_version_show(struct config_item *item, char *page)
1514 return sprintf(page, "usb-gadget fabric module\n");
1517 CONFIGFS_ATTR_RO(usbg_wwn_, version);
1519 static struct configfs_attribute *usbg_wwn_attrs[] = {
1520 &usbg_wwn_attr_version,
1524 static ssize_t tcm_usbg_tpg_enable_show(struct config_item *item, char *page)
1526 struct se_portal_group *se_tpg = to_tpg(item);
1527 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1529 return snprintf(page, PAGE_SIZE, "%u\n", tpg->gadget_connect);
1532 static int usbg_attach(struct usbg_tpg *);
1533 static void usbg_detach(struct usbg_tpg *);
1535 static ssize_t tcm_usbg_tpg_enable_store(struct config_item *item,
1536 const char *page, size_t count)
1538 struct se_portal_group *se_tpg = to_tpg(item);
1539 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1543 ret = strtobool(page, &op);
1547 if ((op && tpg->gadget_connect) || (!op && !tpg->gadget_connect))
1551 ret = usbg_attach(tpg);
1557 tpg->gadget_connect = op;
1562 static ssize_t tcm_usbg_tpg_nexus_show(struct config_item *item, char *page)
1564 struct se_portal_group *se_tpg = to_tpg(item);
1565 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1566 struct tcm_usbg_nexus *tv_nexus;
1569 mutex_lock(&tpg->tpg_mutex);
1570 tv_nexus = tpg->tpg_nexus;
1575 ret = snprintf(page, PAGE_SIZE, "%s\n",
1576 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1578 mutex_unlock(&tpg->tpg_mutex);
1582 static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name)
1584 struct se_portal_group *se_tpg;
1585 struct tcm_usbg_nexus *tv_nexus;
1588 mutex_lock(&tpg->tpg_mutex);
1589 if (tpg->tpg_nexus) {
1591 pr_debug("tpg->tpg_nexus already exists\n");
1594 se_tpg = &tpg->se_tpg;
1597 tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
1600 tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL);
1601 if (IS_ERR(tv_nexus->tvn_se_sess))
1605 * Since we are running in 'demo mode' this call with generate a
1606 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1607 * the SCSI Initiator port name of the passed configfs group 'name'.
1609 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1611 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1612 #define MAKE_NEXUS_MSG "core_tpg_check_initiator_node_acl() failed for %s\n"
1613 pr_debug(MAKE_NEXUS_MSG, name);
1614 #undef MAKE_NEXUS_MSG
1618 * Now register the TCM vHost virtual I_T Nexus as active.
1620 transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1621 tv_nexus->tvn_se_sess, tv_nexus);
1622 tpg->tpg_nexus = tv_nexus;
1623 mutex_unlock(&tpg->tpg_mutex);
1627 transport_free_session(tv_nexus->tvn_se_sess);
1631 mutex_unlock(&tpg->tpg_mutex);
1635 static int tcm_usbg_drop_nexus(struct usbg_tpg *tpg)
1637 struct se_session *se_sess;
1638 struct tcm_usbg_nexus *tv_nexus;
1641 mutex_lock(&tpg->tpg_mutex);
1642 tv_nexus = tpg->tpg_nexus;
1646 se_sess = tv_nexus->tvn_se_sess;
1650 if (atomic_read(&tpg->tpg_port_count)) {
1652 #define MSG "Unable to remove Host I_T Nexus with active TPG port count: %d\n"
1653 pr_err(MSG, atomic_read(&tpg->tpg_port_count));
1658 pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
1659 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1661 * Release the SCSI I_T Nexus to the emulated vHost Target Port
1663 transport_deregister_session(tv_nexus->tvn_se_sess);
1664 tpg->tpg_nexus = NULL;
1669 mutex_unlock(&tpg->tpg_mutex);
1673 static ssize_t tcm_usbg_tpg_nexus_store(struct config_item *item,
1674 const char *page, size_t count)
1676 struct se_portal_group *se_tpg = to_tpg(item);
1677 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1678 unsigned char i_port[USBG_NAMELEN], *ptr;
1681 if (!strncmp(page, "NULL", 4)) {
1682 ret = tcm_usbg_drop_nexus(tpg);
1683 return (!ret) ? count : ret;
1685 if (strlen(page) >= USBG_NAMELEN) {
1687 #define NEXUS_STORE_MSG "Emulated NAA Sas Address: %s, exceeds max: %d\n"
1688 pr_err(NEXUS_STORE_MSG, page, USBG_NAMELEN);
1689 #undef NEXUS_STORE_MSG
1692 snprintf(i_port, USBG_NAMELEN, "%s", page);
1694 ptr = strstr(i_port, "naa.");
1696 pr_err("Missing 'naa.' prefix\n");
1700 if (i_port[strlen(i_port) - 1] == '\n')
1701 i_port[strlen(i_port) - 1] = '\0';
1703 ret = tcm_usbg_make_nexus(tpg, &i_port[0]);
1709 CONFIGFS_ATTR(tcm_usbg_tpg_, enable);
1710 CONFIGFS_ATTR(tcm_usbg_tpg_, nexus);
1712 static struct configfs_attribute *usbg_base_attrs[] = {
1713 &tcm_usbg_tpg_attr_enable,
1714 &tcm_usbg_tpg_attr_nexus,
1718 static int usbg_port_link(struct se_portal_group *se_tpg, struct se_lun *lun)
1720 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1722 atomic_inc(&tpg->tpg_port_count);
1723 smp_mb__after_atomic();
1727 static void usbg_port_unlink(struct se_portal_group *se_tpg,
1728 struct se_lun *se_lun)
1730 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1732 atomic_dec(&tpg->tpg_port_count);
1733 smp_mb__after_atomic();
1736 static int usbg_check_stop_free(struct se_cmd *se_cmd)
1738 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1741 kref_put(&cmd->ref, usbg_cmd_release);
1745 static const struct target_core_fabric_ops usbg_ops = {
1746 .module = THIS_MODULE,
1747 .name = "usb_gadget",
1748 .get_fabric_name = usbg_get_fabric_name,
1749 .tpg_get_wwn = usbg_get_fabric_wwn,
1750 .tpg_get_tag = usbg_get_tag,
1751 .tpg_check_demo_mode = usbg_check_true,
1752 .tpg_check_demo_mode_cache = usbg_check_false,
1753 .tpg_check_demo_mode_write_protect = usbg_check_false,
1754 .tpg_check_prod_mode_write_protect = usbg_check_false,
1755 .tpg_get_inst_index = usbg_tpg_get_inst_index,
1756 .release_cmd = usbg_release_cmd,
1757 .shutdown_session = usbg_shutdown_session,
1758 .close_session = usbg_close_session,
1759 .sess_get_index = usbg_sess_get_index,
1760 .sess_get_initiator_sid = NULL,
1761 .write_pending = usbg_send_write_request,
1762 .write_pending_status = usbg_write_pending_status,
1763 .set_default_node_attributes = usbg_set_default_node_attrs,
1764 .get_cmd_state = usbg_get_cmd_state,
1765 .queue_data_in = usbg_send_read_response,
1766 .queue_status = usbg_send_status_response,
1767 .queue_tm_rsp = usbg_queue_tm_rsp,
1768 .aborted_task = usbg_aborted_task,
1769 .check_stop_free = usbg_check_stop_free,
1771 .fabric_make_wwn = usbg_make_tport,
1772 .fabric_drop_wwn = usbg_drop_tport,
1773 .fabric_make_tpg = usbg_make_tpg,
1774 .fabric_drop_tpg = usbg_drop_tpg,
1775 .fabric_post_link = usbg_port_link,
1776 .fabric_pre_unlink = usbg_port_unlink,
1777 .fabric_init_nodeacl = usbg_init_nodeacl,
1779 .tfc_wwn_attrs = usbg_wwn_attrs,
1780 .tfc_tpg_base_attrs = usbg_base_attrs,
1783 /* Start gadget.c code */
1785 static struct usb_interface_descriptor bot_intf_desc = {
1786 .bLength = sizeof(bot_intf_desc),
1787 .bDescriptorType = USB_DT_INTERFACE,
1789 .bAlternateSetting = USB_G_ALT_INT_BBB,
1790 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1791 .bInterfaceSubClass = USB_SC_SCSI,
1792 .bInterfaceProtocol = USB_PR_BULK,
1795 static struct usb_interface_descriptor uasp_intf_desc = {
1796 .bLength = sizeof(uasp_intf_desc),
1797 .bDescriptorType = USB_DT_INTERFACE,
1799 .bAlternateSetting = USB_G_ALT_INT_UAS,
1800 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1801 .bInterfaceSubClass = USB_SC_SCSI,
1802 .bInterfaceProtocol = USB_PR_UAS,
1805 static struct usb_endpoint_descriptor uasp_bi_desc = {
1806 .bLength = USB_DT_ENDPOINT_SIZE,
1807 .bDescriptorType = USB_DT_ENDPOINT,
1808 .bEndpointAddress = USB_DIR_IN,
1809 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1810 .wMaxPacketSize = cpu_to_le16(512),
1813 static struct usb_endpoint_descriptor uasp_fs_bi_desc = {
1814 .bLength = USB_DT_ENDPOINT_SIZE,
1815 .bDescriptorType = USB_DT_ENDPOINT,
1816 .bEndpointAddress = USB_DIR_IN,
1817 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1820 static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc = {
1821 .bLength = sizeof(uasp_bi_pipe_desc),
1822 .bDescriptorType = USB_DT_PIPE_USAGE,
1823 .bPipeID = DATA_IN_PIPE_ID,
1826 static struct usb_endpoint_descriptor uasp_ss_bi_desc = {
1827 .bLength = USB_DT_ENDPOINT_SIZE,
1828 .bDescriptorType = USB_DT_ENDPOINT,
1829 .bEndpointAddress = USB_DIR_IN,
1830 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1831 .wMaxPacketSize = cpu_to_le16(1024),
1834 static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc = {
1835 .bLength = sizeof(uasp_bi_ep_comp_desc),
1836 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1838 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1839 .wBytesPerInterval = 0,
1842 static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc = {
1843 .bLength = sizeof(bot_bi_ep_comp_desc),
1844 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1848 static struct usb_endpoint_descriptor uasp_bo_desc = {
1849 .bLength = USB_DT_ENDPOINT_SIZE,
1850 .bDescriptorType = USB_DT_ENDPOINT,
1851 .bEndpointAddress = USB_DIR_OUT,
1852 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1853 .wMaxPacketSize = cpu_to_le16(512),
1856 static struct usb_endpoint_descriptor uasp_fs_bo_desc = {
1857 .bLength = USB_DT_ENDPOINT_SIZE,
1858 .bDescriptorType = USB_DT_ENDPOINT,
1859 .bEndpointAddress = USB_DIR_OUT,
1860 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1863 static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc = {
1864 .bLength = sizeof(uasp_bo_pipe_desc),
1865 .bDescriptorType = USB_DT_PIPE_USAGE,
1866 .bPipeID = DATA_OUT_PIPE_ID,
1869 static struct usb_endpoint_descriptor uasp_ss_bo_desc = {
1870 .bLength = USB_DT_ENDPOINT_SIZE,
1871 .bDescriptorType = USB_DT_ENDPOINT,
1872 .bEndpointAddress = USB_DIR_OUT,
1873 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1874 .wMaxPacketSize = cpu_to_le16(0x400),
1877 static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc = {
1878 .bLength = sizeof(uasp_bo_ep_comp_desc),
1879 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1880 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1883 static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc = {
1884 .bLength = sizeof(bot_bo_ep_comp_desc),
1885 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1888 static struct usb_endpoint_descriptor uasp_status_desc = {
1889 .bLength = USB_DT_ENDPOINT_SIZE,
1890 .bDescriptorType = USB_DT_ENDPOINT,
1891 .bEndpointAddress = USB_DIR_IN,
1892 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1893 .wMaxPacketSize = cpu_to_le16(512),
1896 static struct usb_endpoint_descriptor uasp_fs_status_desc = {
1897 .bLength = USB_DT_ENDPOINT_SIZE,
1898 .bDescriptorType = USB_DT_ENDPOINT,
1899 .bEndpointAddress = USB_DIR_IN,
1900 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1903 static struct usb_pipe_usage_descriptor uasp_status_pipe_desc = {
1904 .bLength = sizeof(uasp_status_pipe_desc),
1905 .bDescriptorType = USB_DT_PIPE_USAGE,
1906 .bPipeID = STATUS_PIPE_ID,
1909 static struct usb_endpoint_descriptor uasp_ss_status_desc = {
1910 .bLength = USB_DT_ENDPOINT_SIZE,
1911 .bDescriptorType = USB_DT_ENDPOINT,
1912 .bEndpointAddress = USB_DIR_IN,
1913 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1914 .wMaxPacketSize = cpu_to_le16(1024),
1917 static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc = {
1918 .bLength = sizeof(uasp_status_in_ep_comp_desc),
1919 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1920 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1923 static struct usb_endpoint_descriptor uasp_cmd_desc = {
1924 .bLength = USB_DT_ENDPOINT_SIZE,
1925 .bDescriptorType = USB_DT_ENDPOINT,
1926 .bEndpointAddress = USB_DIR_OUT,
1927 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1928 .wMaxPacketSize = cpu_to_le16(512),
1931 static struct usb_endpoint_descriptor uasp_fs_cmd_desc = {
1932 .bLength = USB_DT_ENDPOINT_SIZE,
1933 .bDescriptorType = USB_DT_ENDPOINT,
1934 .bEndpointAddress = USB_DIR_OUT,
1935 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1938 static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc = {
1939 .bLength = sizeof(uasp_cmd_pipe_desc),
1940 .bDescriptorType = USB_DT_PIPE_USAGE,
1941 .bPipeID = CMD_PIPE_ID,
1944 static struct usb_endpoint_descriptor uasp_ss_cmd_desc = {
1945 .bLength = USB_DT_ENDPOINT_SIZE,
1946 .bDescriptorType = USB_DT_ENDPOINT,
1947 .bEndpointAddress = USB_DIR_OUT,
1948 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1949 .wMaxPacketSize = cpu_to_le16(1024),
1952 static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc = {
1953 .bLength = sizeof(uasp_cmd_comp_desc),
1954 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1957 static struct usb_descriptor_header *uasp_fs_function_desc[] = {
1958 (struct usb_descriptor_header *) &bot_intf_desc,
1959 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
1960 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
1962 (struct usb_descriptor_header *) &uasp_intf_desc,
1963 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
1964 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
1965 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
1966 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
1967 (struct usb_descriptor_header *) &uasp_fs_status_desc,
1968 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
1969 (struct usb_descriptor_header *) &uasp_fs_cmd_desc,
1970 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
1974 static struct usb_descriptor_header *uasp_hs_function_desc[] = {
1975 (struct usb_descriptor_header *) &bot_intf_desc,
1976 (struct usb_descriptor_header *) &uasp_bi_desc,
1977 (struct usb_descriptor_header *) &uasp_bo_desc,
1979 (struct usb_descriptor_header *) &uasp_intf_desc,
1980 (struct usb_descriptor_header *) &uasp_bi_desc,
1981 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
1982 (struct usb_descriptor_header *) &uasp_bo_desc,
1983 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
1984 (struct usb_descriptor_header *) &uasp_status_desc,
1985 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
1986 (struct usb_descriptor_header *) &uasp_cmd_desc,
1987 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
1991 static struct usb_descriptor_header *uasp_ss_function_desc[] = {
1992 (struct usb_descriptor_header *) &bot_intf_desc,
1993 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
1994 (struct usb_descriptor_header *) &bot_bi_ep_comp_desc,
1995 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
1996 (struct usb_descriptor_header *) &bot_bo_ep_comp_desc,
1998 (struct usb_descriptor_header *) &uasp_intf_desc,
1999 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
2000 (struct usb_descriptor_header *) &uasp_bi_ep_comp_desc,
2001 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
2002 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
2003 (struct usb_descriptor_header *) &uasp_bo_ep_comp_desc,
2004 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
2005 (struct usb_descriptor_header *) &uasp_ss_status_desc,
2006 (struct usb_descriptor_header *) &uasp_status_in_ep_comp_desc,
2007 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
2008 (struct usb_descriptor_header *) &uasp_ss_cmd_desc,
2009 (struct usb_descriptor_header *) &uasp_cmd_comp_desc,
2010 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
2014 static struct usb_string tcm_us_strings[] = {
2015 [USB_G_STR_INT_UAS].s = "USB Attached SCSI",
2016 [USB_G_STR_INT_BBB].s = "Bulk Only Transport",
2020 static struct usb_gadget_strings tcm_stringtab = {
2022 .strings = tcm_us_strings,
2025 static struct usb_gadget_strings *tcm_strings[] = {
2030 static int tcm_bind(struct usb_configuration *c, struct usb_function *f)
2032 struct f_uas *fu = to_f_uas(f);
2033 struct usb_string *us;
2034 struct usb_gadget *gadget = c->cdev->gadget;
2036 struct f_tcm_opts *opts;
2040 opts = container_of(f->fi, struct f_tcm_opts, func_inst);
2042 mutex_lock(&opts->dep_lock);
2043 if (!opts->can_attach) {
2044 mutex_unlock(&opts->dep_lock);
2047 mutex_unlock(&opts->dep_lock);
2048 us = usb_gstrings_attach(c->cdev, tcm_strings,
2049 ARRAY_SIZE(tcm_us_strings));
2052 bot_intf_desc.iInterface = us[USB_G_STR_INT_BBB].id;
2053 uasp_intf_desc.iInterface = us[USB_G_STR_INT_UAS].id;
2055 iface = usb_interface_id(c, f);
2059 bot_intf_desc.bInterfaceNumber = iface;
2060 uasp_intf_desc.bInterfaceNumber = iface;
2062 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bi_desc,
2063 &uasp_bi_ep_comp_desc);
2069 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bo_desc,
2070 &uasp_bo_ep_comp_desc);
2075 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_status_desc,
2076 &uasp_status_in_ep_comp_desc);
2081 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_cmd_desc,
2082 &uasp_cmd_comp_desc);
2087 /* Assume endpoint addresses are the same for both speeds */
2088 uasp_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2089 uasp_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2090 uasp_status_desc.bEndpointAddress =
2091 uasp_ss_status_desc.bEndpointAddress;
2092 uasp_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2094 uasp_fs_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2095 uasp_fs_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2096 uasp_fs_status_desc.bEndpointAddress =
2097 uasp_ss_status_desc.bEndpointAddress;
2098 uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2100 ret = usb_assign_descriptors(f, uasp_fs_function_desc,
2101 uasp_hs_function_desc, uasp_ss_function_desc);
2107 pr_err("Can't claim all required eps\n");
2112 struct guas_setup_wq {
2113 struct work_struct work;
2118 static void tcm_delayed_set_alt(struct work_struct *wq)
2120 struct guas_setup_wq *work = container_of(wq, struct guas_setup_wq,
2122 struct f_uas *fu = work->fu;
2123 int alt = work->alt;
2127 if (fu->flags & USBG_IS_BOT)
2128 bot_cleanup_old_alt(fu);
2129 if (fu->flags & USBG_IS_UAS)
2130 uasp_cleanup_old_alt(fu);
2132 if (alt == USB_G_ALT_INT_BBB)
2134 else if (alt == USB_G_ALT_INT_UAS)
2136 usb_composite_setup_continue(fu->function.config->cdev);
2139 static int tcm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2141 struct f_uas *fu = to_f_uas(f);
2143 if ((alt == USB_G_ALT_INT_BBB) || (alt == USB_G_ALT_INT_UAS)) {
2144 struct guas_setup_wq *work;
2146 work = kmalloc(sizeof(*work), GFP_ATOMIC);
2149 INIT_WORK(&work->work, tcm_delayed_set_alt);
2152 schedule_work(&work->work);
2153 return USB_GADGET_DELAYED_STATUS;
2158 static void tcm_disable(struct usb_function *f)
2160 struct f_uas *fu = to_f_uas(f);
2162 if (fu->flags & USBG_IS_UAS)
2163 uasp_cleanup_old_alt(fu);
2164 else if (fu->flags & USBG_IS_BOT)
2165 bot_cleanup_old_alt(fu);
2169 static int tcm_setup(struct usb_function *f,
2170 const struct usb_ctrlrequest *ctrl)
2172 struct f_uas *fu = to_f_uas(f);
2174 if (!(fu->flags & USBG_IS_BOT))
2177 return usbg_bot_setup(f, ctrl);
2180 static inline struct f_tcm_opts *to_f_tcm_opts(struct config_item *item)
2182 return container_of(to_config_group(item), struct f_tcm_opts,
2186 static void tcm_attr_release(struct config_item *item)
2188 struct f_tcm_opts *opts = to_f_tcm_opts(item);
2190 usb_put_function_instance(&opts->func_inst);
2193 static struct configfs_item_operations tcm_item_ops = {
2194 .release = tcm_attr_release,
2197 static struct config_item_type tcm_func_type = {
2198 .ct_item_ops = &tcm_item_ops,
2199 .ct_owner = THIS_MODULE,
2202 static void tcm_free_inst(struct usb_function_instance *f)
2204 struct f_tcm_opts *opts;
2207 opts = container_of(f, struct f_tcm_opts, func_inst);
2209 mutex_lock(&tpg_instances_lock);
2210 for (i = 0; i < TPG_INSTANCES; ++i)
2211 if (tpg_instances[i].func_inst == f)
2213 if (i < TPG_INSTANCES)
2214 tpg_instances[i].func_inst = NULL;
2215 mutex_unlock(&tpg_instances_lock);
2220 static int tcm_register_callback(struct usb_function_instance *f)
2222 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2224 mutex_lock(&opts->dep_lock);
2225 opts->can_attach = true;
2226 mutex_unlock(&opts->dep_lock);
2231 static void tcm_unregister_callback(struct usb_function_instance *f)
2233 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2235 mutex_lock(&opts->dep_lock);
2236 unregister_gadget_item(opts->
2237 func_inst.group.cg_item.ci_parent->ci_parent);
2238 opts->can_attach = false;
2239 mutex_unlock(&opts->dep_lock);
2242 static int usbg_attach(struct usbg_tpg *tpg)
2244 struct usb_function_instance *f = tpg->fi;
2245 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2247 if (opts->tcm_register_callback)
2248 return opts->tcm_register_callback(f);
2253 static void usbg_detach(struct usbg_tpg *tpg)
2255 struct usb_function_instance *f = tpg->fi;
2256 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2258 if (opts->tcm_unregister_callback)
2259 opts->tcm_unregister_callback(f);
2262 static int tcm_set_name(struct usb_function_instance *f, const char *name)
2264 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2266 pr_debug("tcm: Activating %s\n", name);
2268 mutex_lock(&opts->dep_lock);
2270 mutex_unlock(&opts->dep_lock);
2275 static struct usb_function_instance *tcm_alloc_inst(void)
2277 struct f_tcm_opts *opts;
2281 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2283 return ERR_PTR(-ENOMEM);
2285 mutex_lock(&tpg_instances_lock);
2286 for (i = 0; i < TPG_INSTANCES; ++i)
2287 if (!tpg_instances[i].func_inst)
2290 if (i == TPG_INSTANCES) {
2291 mutex_unlock(&tpg_instances_lock);
2293 return ERR_PTR(-EBUSY);
2295 tpg_instances[i].func_inst = &opts->func_inst;
2296 mutex_unlock(&tpg_instances_lock);
2298 mutex_init(&opts->dep_lock);
2299 opts->func_inst.set_inst_name = tcm_set_name;
2300 opts->func_inst.free_func_inst = tcm_free_inst;
2301 opts->tcm_register_callback = tcm_register_callback;
2302 opts->tcm_unregister_callback = tcm_unregister_callback;
2304 config_group_init_type_name(&opts->func_inst.group, "",
2307 return &opts->func_inst;
2310 static void tcm_free(struct usb_function *f)
2312 struct f_uas *tcm = to_f_uas(f);
2317 static void tcm_unbind(struct usb_configuration *c, struct usb_function *f)
2319 usb_free_all_descriptors(f);
2322 static struct usb_function *tcm_alloc(struct usb_function_instance *fi)
2327 mutex_lock(&tpg_instances_lock);
2328 for (i = 0; i < TPG_INSTANCES; ++i)
2329 if (tpg_instances[i].func_inst == fi)
2331 if (i == TPG_INSTANCES) {
2332 mutex_unlock(&tpg_instances_lock);
2333 return ERR_PTR(-ENODEV);
2336 fu = kzalloc(sizeof(*fu), GFP_KERNEL);
2338 mutex_unlock(&tpg_instances_lock);
2339 return ERR_PTR(-ENOMEM);
2342 fu->function.name = "Target Function";
2343 fu->function.bind = tcm_bind;
2344 fu->function.unbind = tcm_unbind;
2345 fu->function.set_alt = tcm_set_alt;
2346 fu->function.setup = tcm_setup;
2347 fu->function.disable = tcm_disable;
2348 fu->function.free_func = tcm_free;
2349 fu->tpg = tpg_instances[i].tpg;
2350 mutex_unlock(&tpg_instances_lock);
2352 return &fu->function;
2355 DECLARE_USB_FUNCTION(tcm, tcm_alloc_inst, tcm_alloc);
2357 static int tcm_init(void)
2361 ret = usb_function_register(&tcmusb_func);
2365 ret = target_register_template(&usbg_ops);
2367 usb_function_unregister(&tcmusb_func);
2371 module_init(tcm_init);
2373 static void tcm_exit(void)
2375 target_unregister_template(&usbg_ops);
2376 usb_function_unregister(&tcmusb_func);
2378 module_exit(tcm_exit);
2380 MODULE_LICENSE("GPL");
2381 MODULE_AUTHOR("Sebastian Andrzej Siewior");