1 // SPDX-License-Identifier: GPL-2.0
2 /* Target based USB-Gadget
4 * UAS protocol handling, target callbacks, configfs handling,
5 * BBB (USB Mass Storage Class Bulk-Only (BBB) and Transport protocol handling.
7 * Author: Sebastian Andrzej Siewior <bigeasy at linutronix dot de>
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 /* Start bot.c code */
46 static int bot_enqueue_cmd_cbw(struct f_uas *fu)
50 if (fu->flags & USBG_BOT_CMD_PEND)
53 ret = usb_ep_queue(fu->ep_out, fu->cmd.req, GFP_ATOMIC);
55 fu->flags |= USBG_BOT_CMD_PEND;
59 static void bot_status_complete(struct usb_ep *ep, struct usb_request *req)
61 struct usbg_cmd *cmd = req->context;
62 struct f_uas *fu = cmd->fu;
64 transport_generic_free_cmd(&cmd->se_cmd, 0);
65 if (req->status < 0) {
66 pr_err("ERR %s(%d)\n", __func__, __LINE__);
70 /* CSW completed, wait for next CBW */
71 bot_enqueue_cmd_cbw(fu);
74 static void bot_enqueue_sense_code(struct f_uas *fu, struct usbg_cmd *cmd)
76 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
78 unsigned int csw_stat;
80 csw_stat = cmd->csw_code;
81 csw->Tag = cmd->bot_tag;
82 csw->Status = csw_stat;
83 fu->bot_status.req->context = cmd;
84 ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_ATOMIC);
86 pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
89 static void bot_err_compl(struct usb_ep *ep, struct usb_request *req)
91 struct usbg_cmd *cmd = req->context;
92 struct f_uas *fu = cmd->fu;
95 pr_err("ERR %s(%d)\n", __func__, __LINE__);
98 if (cmd->data_len > ep->maxpacket) {
99 req->length = ep->maxpacket;
100 cmd->data_len -= ep->maxpacket;
102 req->length = cmd->data_len;
106 usb_ep_queue(ep, req, GFP_ATOMIC);
109 bot_enqueue_sense_code(fu, cmd);
112 static void bot_send_bad_status(struct usbg_cmd *cmd)
114 struct f_uas *fu = cmd->fu;
115 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
116 struct usb_request *req;
119 csw->Residue = cpu_to_le32(cmd->data_len);
124 req = fu->bot_req_in;
127 req = fu->bot_req_out;
130 if (cmd->data_len > fu->ep_in->maxpacket) {
131 req->length = ep->maxpacket;
132 cmd->data_len -= ep->maxpacket;
134 req->length = cmd->data_len;
137 req->complete = bot_err_compl;
139 req->buf = fu->cmd.buf;
140 usb_ep_queue(ep, req, GFP_KERNEL);
142 bot_enqueue_sense_code(fu, cmd);
146 static int bot_send_status(struct usbg_cmd *cmd, bool moved_data)
148 struct f_uas *fu = cmd->fu;
149 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
152 if (cmd->se_cmd.scsi_status == SAM_STAT_GOOD) {
153 if (!moved_data && cmd->data_len) {
155 * the host wants to move data, we don't. Fill / empty
156 * the pipe and then send the csw with reside set.
158 cmd->csw_code = US_BULK_STAT_OK;
159 bot_send_bad_status(cmd);
163 csw->Tag = cmd->bot_tag;
164 csw->Residue = cpu_to_le32(0);
165 csw->Status = US_BULK_STAT_OK;
166 fu->bot_status.req->context = cmd;
168 ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_KERNEL);
170 pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
172 cmd->csw_code = US_BULK_STAT_FAIL;
173 bot_send_bad_status(cmd);
179 * Called after command (no data transfer) or after the write (to device)
180 * operation is completed
182 static int bot_send_status_response(struct usbg_cmd *cmd)
184 bool moved_data = false;
188 return bot_send_status(cmd, moved_data);
191 /* Read request completed, now we have to send the CSW */
192 static void bot_read_compl(struct usb_ep *ep, struct usb_request *req)
194 struct usbg_cmd *cmd = req->context;
197 pr_err("ERR %s(%d)\n", __func__, __LINE__);
199 bot_send_status(cmd, true);
202 static int bot_send_read_response(struct usbg_cmd *cmd)
204 struct f_uas *fu = cmd->fu;
205 struct se_cmd *se_cmd = &cmd->se_cmd;
206 struct usb_gadget *gadget = fuas_to_gadget(fu);
209 if (!cmd->data_len) {
210 cmd->csw_code = US_BULK_STAT_PHASE;
211 bot_send_bad_status(cmd);
215 if (!gadget->sg_supported) {
216 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
220 sg_copy_to_buffer(se_cmd->t_data_sg,
221 se_cmd->t_data_nents,
223 se_cmd->data_length);
225 fu->bot_req_in->buf = cmd->data_buf;
227 fu->bot_req_in->buf = NULL;
228 fu->bot_req_in->num_sgs = se_cmd->t_data_nents;
229 fu->bot_req_in->sg = se_cmd->t_data_sg;
232 fu->bot_req_in->complete = bot_read_compl;
233 fu->bot_req_in->length = se_cmd->data_length;
234 fu->bot_req_in->context = cmd;
235 ret = usb_ep_queue(fu->ep_in, fu->bot_req_in, GFP_ATOMIC);
237 pr_err("%s(%d)\n", __func__, __LINE__);
241 static void usbg_data_write_cmpl(struct usb_ep *, struct usb_request *);
242 static int usbg_prepare_w_request(struct usbg_cmd *, struct usb_request *);
244 static int bot_send_write_request(struct usbg_cmd *cmd)
246 struct f_uas *fu = cmd->fu;
247 struct se_cmd *se_cmd = &cmd->se_cmd;
248 struct usb_gadget *gadget = fuas_to_gadget(fu);
251 init_completion(&cmd->write_complete);
254 if (!cmd->data_len) {
255 cmd->csw_code = US_BULK_STAT_PHASE;
259 if (!gadget->sg_supported) {
260 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_KERNEL);
264 fu->bot_req_out->buf = cmd->data_buf;
266 fu->bot_req_out->buf = NULL;
267 fu->bot_req_out->num_sgs = se_cmd->t_data_nents;
268 fu->bot_req_out->sg = se_cmd->t_data_sg;
271 fu->bot_req_out->complete = usbg_data_write_cmpl;
272 fu->bot_req_out->length = se_cmd->data_length;
273 fu->bot_req_out->context = cmd;
275 ret = usbg_prepare_w_request(cmd, fu->bot_req_out);
278 ret = usb_ep_queue(fu->ep_out, fu->bot_req_out, GFP_KERNEL);
280 pr_err("%s(%d)\n", __func__, __LINE__);
282 wait_for_completion(&cmd->write_complete);
283 target_execute_cmd(se_cmd);
288 static int bot_submit_command(struct f_uas *, void *, unsigned int);
290 static void bot_cmd_complete(struct usb_ep *ep, struct usb_request *req)
292 struct f_uas *fu = req->context;
295 fu->flags &= ~USBG_BOT_CMD_PEND;
300 ret = bot_submit_command(fu, req->buf, req->actual);
302 pr_err("%s(%d): %d\n", __func__, __LINE__, ret);
305 static int bot_prepare_reqs(struct f_uas *fu)
309 fu->bot_req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
313 fu->bot_req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
314 if (!fu->bot_req_out)
317 fu->cmd.req = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
321 fu->bot_status.req = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
322 if (!fu->bot_status.req)
325 fu->bot_status.req->buf = &fu->bot_status.csw;
326 fu->bot_status.req->length = US_BULK_CS_WRAP_LEN;
327 fu->bot_status.req->complete = bot_status_complete;
328 fu->bot_status.csw.Signature = cpu_to_le32(US_BULK_CS_SIGN);
330 fu->cmd.buf = kmalloc(fu->ep_out->maxpacket, GFP_KERNEL);
334 fu->cmd.req->complete = bot_cmd_complete;
335 fu->cmd.req->buf = fu->cmd.buf;
336 fu->cmd.req->length = fu->ep_out->maxpacket;
337 fu->cmd.req->context = fu;
339 ret = bot_enqueue_cmd_cbw(fu);
347 usb_ep_free_request(fu->ep_in, fu->bot_status.req);
349 usb_ep_free_request(fu->ep_out, fu->cmd.req);
352 usb_ep_free_request(fu->ep_out, fu->bot_req_out);
353 fu->bot_req_out = NULL;
355 usb_ep_free_request(fu->ep_in, fu->bot_req_in);
356 fu->bot_req_in = NULL;
358 pr_err("BOT: endpoint setup failed\n");
362 static void bot_cleanup_old_alt(struct f_uas *fu)
364 if (!(fu->flags & USBG_ENABLED))
367 usb_ep_disable(fu->ep_in);
368 usb_ep_disable(fu->ep_out);
373 usb_ep_free_request(fu->ep_in, fu->bot_req_in);
374 usb_ep_free_request(fu->ep_out, fu->bot_req_out);
375 usb_ep_free_request(fu->ep_out, fu->cmd.req);
376 usb_ep_free_request(fu->ep_in, fu->bot_status.req);
380 fu->bot_req_in = NULL;
381 fu->bot_req_out = NULL;
383 fu->bot_status.req = NULL;
387 static void bot_set_alt(struct f_uas *fu)
389 struct usb_function *f = &fu->function;
390 struct usb_gadget *gadget = f->config->cdev->gadget;
393 fu->flags = USBG_IS_BOT;
395 config_ep_by_speed_and_alt(gadget, f, fu->ep_in, USB_G_ALT_INT_BBB);
396 ret = usb_ep_enable(fu->ep_in);
400 config_ep_by_speed_and_alt(gadget, f, fu->ep_out, USB_G_ALT_INT_BBB);
401 ret = usb_ep_enable(fu->ep_out);
405 ret = bot_prepare_reqs(fu);
408 fu->flags |= USBG_ENABLED;
409 pr_info("Using the BOT protocol\n");
412 usb_ep_disable(fu->ep_out);
414 usb_ep_disable(fu->ep_in);
416 fu->flags = USBG_IS_BOT;
419 static int usbg_bot_setup(struct usb_function *f,
420 const struct usb_ctrlrequest *ctrl)
422 struct f_uas *fu = to_f_uas(f);
423 struct usb_composite_dev *cdev = f->config->cdev;
424 u16 w_value = le16_to_cpu(ctrl->wValue);
425 u16 w_length = le16_to_cpu(ctrl->wLength);
429 switch (ctrl->bRequest) {
430 case US_BULK_GET_MAX_LUN:
431 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_CLASS |
432 USB_RECIP_INTERFACE))
439 luns = atomic_read(&fu->tpg->tpg_port_count);
441 pr_err("No LUNs configured?\n");
445 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
446 * accessed. The upper limit is 0xf
450 pr_info_once("Limiting the number of luns to 16\n");
453 ret_lun = cdev->req->buf;
455 cdev->req->length = 1;
456 return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
458 case US_BULK_RESET_REQUEST:
459 /* XXX maybe we should remove previous requests for IN + OUT */
460 bot_enqueue_cmd_cbw(fu);
466 /* Start uas.c code */
468 static void uasp_cleanup_one_stream(struct f_uas *fu, struct uas_stream *stream)
470 /* We have either all three allocated or none */
474 usb_ep_free_request(fu->ep_in, stream->req_in);
475 usb_ep_free_request(fu->ep_out, stream->req_out);
476 usb_ep_free_request(fu->ep_status, stream->req_status);
478 stream->req_in = NULL;
479 stream->req_out = NULL;
480 stream->req_status = NULL;
483 static void uasp_free_cmdreq(struct f_uas *fu)
485 usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
491 static void uasp_cleanup_old_alt(struct f_uas *fu)
495 if (!(fu->flags & USBG_ENABLED))
498 usb_ep_disable(fu->ep_in);
499 usb_ep_disable(fu->ep_out);
500 usb_ep_disable(fu->ep_status);
501 usb_ep_disable(fu->ep_cmd);
503 for (i = 0; i < UASP_SS_EP_COMP_NUM_STREAMS; i++)
504 uasp_cleanup_one_stream(fu, &fu->stream[i]);
505 uasp_free_cmdreq(fu);
508 static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req);
510 static int uasp_prepare_r_request(struct usbg_cmd *cmd)
512 struct se_cmd *se_cmd = &cmd->se_cmd;
513 struct f_uas *fu = cmd->fu;
514 struct usb_gadget *gadget = fuas_to_gadget(fu);
515 struct uas_stream *stream = cmd->stream;
517 if (!gadget->sg_supported) {
518 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
522 sg_copy_to_buffer(se_cmd->t_data_sg,
523 se_cmd->t_data_nents,
525 se_cmd->data_length);
527 stream->req_in->buf = cmd->data_buf;
529 stream->req_in->buf = NULL;
530 stream->req_in->num_sgs = se_cmd->t_data_nents;
531 stream->req_in->sg = se_cmd->t_data_sg;
534 stream->req_in->is_last = 1;
535 stream->req_in->complete = uasp_status_data_cmpl;
536 stream->req_in->length = se_cmd->data_length;
537 stream->req_in->context = cmd;
539 cmd->state = UASP_SEND_STATUS;
543 static void uasp_prepare_status(struct usbg_cmd *cmd)
545 struct se_cmd *se_cmd = &cmd->se_cmd;
546 struct sense_iu *iu = &cmd->sense_iu;
547 struct uas_stream *stream = cmd->stream;
549 cmd->state = UASP_QUEUE_COMMAND;
550 iu->iu_id = IU_ID_STATUS;
551 iu->tag = cpu_to_be16(cmd->tag);
554 * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?);
556 iu->len = cpu_to_be16(se_cmd->scsi_sense_length);
557 iu->status = se_cmd->scsi_status;
558 stream->req_status->is_last = 1;
559 stream->req_status->context = cmd;
560 stream->req_status->length = se_cmd->scsi_sense_length + 16;
561 stream->req_status->buf = iu;
562 stream->req_status->complete = uasp_status_data_cmpl;
565 static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req)
567 struct usbg_cmd *cmd = req->context;
568 struct uas_stream *stream = cmd->stream;
569 struct f_uas *fu = cmd->fu;
575 switch (cmd->state) {
577 ret = uasp_prepare_r_request(cmd);
580 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
582 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
585 case UASP_RECEIVE_DATA:
586 ret = usbg_prepare_w_request(cmd, stream->req_out);
589 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
591 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
594 case UASP_SEND_STATUS:
595 uasp_prepare_status(cmd);
596 ret = usb_ep_queue(fu->ep_status, stream->req_status,
599 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
602 case UASP_QUEUE_COMMAND:
603 transport_generic_free_cmd(&cmd->se_cmd, 0);
604 usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
613 transport_generic_free_cmd(&cmd->se_cmd, 0);
616 static int uasp_send_status_response(struct usbg_cmd *cmd)
618 struct f_uas *fu = cmd->fu;
619 struct uas_stream *stream = cmd->stream;
620 struct sense_iu *iu = &cmd->sense_iu;
622 iu->tag = cpu_to_be16(cmd->tag);
623 stream->req_status->complete = uasp_status_data_cmpl;
624 stream->req_status->context = cmd;
626 uasp_prepare_status(cmd);
627 return usb_ep_queue(fu->ep_status, stream->req_status, GFP_ATOMIC);
630 static int uasp_send_read_response(struct usbg_cmd *cmd)
632 struct f_uas *fu = cmd->fu;
633 struct uas_stream *stream = cmd->stream;
634 struct sense_iu *iu = &cmd->sense_iu;
639 iu->tag = cpu_to_be16(cmd->tag);
640 if (fu->flags & USBG_USE_STREAMS) {
642 ret = uasp_prepare_r_request(cmd);
645 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
647 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
648 kfree(cmd->data_buf);
649 cmd->data_buf = NULL;
654 iu->iu_id = IU_ID_READ_READY;
655 iu->tag = cpu_to_be16(cmd->tag);
657 stream->req_status->complete = uasp_status_data_cmpl;
658 stream->req_status->context = cmd;
660 cmd->state = UASP_SEND_DATA;
661 stream->req_status->buf = iu;
662 stream->req_status->length = sizeof(struct iu);
664 ret = usb_ep_queue(fu->ep_status, stream->req_status,
667 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
673 static int uasp_send_write_request(struct usbg_cmd *cmd)
675 struct f_uas *fu = cmd->fu;
676 struct se_cmd *se_cmd = &cmd->se_cmd;
677 struct uas_stream *stream = cmd->stream;
678 struct sense_iu *iu = &cmd->sense_iu;
681 init_completion(&cmd->write_complete);
684 iu->tag = cpu_to_be16(cmd->tag);
686 if (fu->flags & USBG_USE_STREAMS) {
688 ret = usbg_prepare_w_request(cmd, stream->req_out);
691 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
693 pr_err("%s(%d)\n", __func__, __LINE__);
697 iu->iu_id = IU_ID_WRITE_READY;
698 iu->tag = cpu_to_be16(cmd->tag);
700 stream->req_status->complete = uasp_status_data_cmpl;
701 stream->req_status->context = cmd;
703 cmd->state = UASP_RECEIVE_DATA;
704 stream->req_status->buf = iu;
705 stream->req_status->length = sizeof(struct iu);
707 ret = usb_ep_queue(fu->ep_status, stream->req_status,
710 pr_err("%s(%d)\n", __func__, __LINE__);
713 wait_for_completion(&cmd->write_complete);
714 target_execute_cmd(se_cmd);
719 static int usbg_submit_command(struct f_uas *, void *, unsigned int);
721 static void uasp_cmd_complete(struct usb_ep *ep, struct usb_request *req)
723 struct f_uas *fu = req->context;
729 ret = usbg_submit_command(fu, req->buf, req->actual);
731 * Once we tune for performance enqueue the command req here again so
732 * we can receive a second command while we processing this one. Pay
733 * attention to properly sync STAUS endpoint with DATA IN + OUT so you
738 usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
741 static int uasp_alloc_stream_res(struct f_uas *fu, struct uas_stream *stream)
743 stream->req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
747 stream->req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
748 if (!stream->req_out)
751 stream->req_status = usb_ep_alloc_request(fu->ep_status, GFP_KERNEL);
752 if (!stream->req_status)
758 usb_ep_free_request(fu->ep_out, stream->req_out);
759 stream->req_out = NULL;
761 usb_ep_free_request(fu->ep_in, stream->req_in);
762 stream->req_in = NULL;
767 static int uasp_alloc_cmd(struct f_uas *fu)
769 fu->cmd.req = usb_ep_alloc_request(fu->ep_cmd, GFP_KERNEL);
773 fu->cmd.buf = kmalloc(fu->ep_cmd->maxpacket, GFP_KERNEL);
777 fu->cmd.req->complete = uasp_cmd_complete;
778 fu->cmd.req->buf = fu->cmd.buf;
779 fu->cmd.req->length = fu->ep_cmd->maxpacket;
780 fu->cmd.req->context = fu;
784 usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
789 static void uasp_setup_stream_res(struct f_uas *fu, int max_streams)
793 for (i = 0; i < max_streams; i++) {
794 struct uas_stream *s = &fu->stream[i];
796 s->req_in->stream_id = i + 1;
797 s->req_out->stream_id = i + 1;
798 s->req_status->stream_id = i + 1;
802 static int uasp_prepare_reqs(struct f_uas *fu)
808 if (fu->flags & USBG_USE_STREAMS)
809 max_streams = UASP_SS_EP_COMP_NUM_STREAMS;
813 for (i = 0; i < max_streams; i++) {
814 ret = uasp_alloc_stream_res(fu, &fu->stream[i]);
819 ret = uasp_alloc_cmd(fu);
821 goto err_free_stream;
822 uasp_setup_stream_res(fu, max_streams);
824 ret = usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
826 goto err_free_stream;
831 uasp_free_cmdreq(fu);
836 uasp_cleanup_one_stream(fu, &fu->stream[i - 1]);
840 pr_err("UASP: endpoint setup failed\n");
844 static void uasp_set_alt(struct f_uas *fu)
846 struct usb_function *f = &fu->function;
847 struct usb_gadget *gadget = f->config->cdev->gadget;
850 fu->flags = USBG_IS_UAS;
852 if (gadget->speed >= USB_SPEED_SUPER)
853 fu->flags |= USBG_USE_STREAMS;
855 config_ep_by_speed_and_alt(gadget, f, fu->ep_in, USB_G_ALT_INT_UAS);
856 ret = usb_ep_enable(fu->ep_in);
860 config_ep_by_speed_and_alt(gadget, f, fu->ep_out, USB_G_ALT_INT_UAS);
861 ret = usb_ep_enable(fu->ep_out);
865 config_ep_by_speed_and_alt(gadget, f, fu->ep_cmd, USB_G_ALT_INT_UAS);
866 ret = usb_ep_enable(fu->ep_cmd);
869 config_ep_by_speed_and_alt(gadget, f, fu->ep_status, USB_G_ALT_INT_UAS);
870 ret = usb_ep_enable(fu->ep_status);
874 ret = uasp_prepare_reqs(fu);
877 fu->flags |= USBG_ENABLED;
879 pr_info("Using the UAS protocol\n");
882 usb_ep_disable(fu->ep_status);
884 usb_ep_disable(fu->ep_cmd);
886 usb_ep_disable(fu->ep_out);
888 usb_ep_disable(fu->ep_in);
893 static int get_cmd_dir(const unsigned char *cdb)
905 case SERVICE_ACTION_IN_16:
907 case PERSISTENT_RESERVE_IN:
908 case SECURITY_PROTOCOL_IN:
909 case ACCESS_CONTROL_IN:
911 case READ_BLOCK_LIMITS:
915 case READ_FORMAT_CAPACITIES:
917 ret = DMA_FROM_DEVICE;
927 case WRITE_VERIFY_12:
928 case PERSISTENT_RESERVE_OUT:
929 case MAINTENANCE_OUT:
930 case SECURITY_PROTOCOL_OUT:
931 case ACCESS_CONTROL_OUT:
934 case ALLOW_MEDIUM_REMOVAL:
935 case TEST_UNIT_READY:
936 case SYNCHRONIZE_CACHE:
943 case WRITE_FILEMARKS:
947 #define CMD_DIR_MSG "target: Unknown data direction for SCSI Opcode 0x%02x\n"
948 pr_warn(CMD_DIR_MSG, cdb[0]);
955 static void usbg_data_write_cmpl(struct usb_ep *ep, struct usb_request *req)
957 struct usbg_cmd *cmd = req->context;
958 struct se_cmd *se_cmd = &cmd->se_cmd;
960 if (req->status < 0) {
961 pr_err("%s() state %d transfer failed\n", __func__, cmd->state);
965 if (req->num_sgs == 0) {
966 sg_copy_from_buffer(se_cmd->t_data_sg,
967 se_cmd->t_data_nents,
969 se_cmd->data_length);
972 complete(&cmd->write_complete);
976 transport_generic_free_cmd(&cmd->se_cmd, 0);
979 static int usbg_prepare_w_request(struct usbg_cmd *cmd, struct usb_request *req)
981 struct se_cmd *se_cmd = &cmd->se_cmd;
982 struct f_uas *fu = cmd->fu;
983 struct usb_gadget *gadget = fuas_to_gadget(fu);
985 if (!gadget->sg_supported) {
986 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
990 req->buf = cmd->data_buf;
993 req->num_sgs = se_cmd->t_data_nents;
994 req->sg = se_cmd->t_data_sg;
998 req->complete = usbg_data_write_cmpl;
999 req->length = se_cmd->data_length;
1004 static int usbg_send_status_response(struct se_cmd *se_cmd)
1006 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1008 struct f_uas *fu = cmd->fu;
1010 if (fu->flags & USBG_IS_BOT)
1011 return bot_send_status_response(cmd);
1013 return uasp_send_status_response(cmd);
1016 static int usbg_send_write_request(struct se_cmd *se_cmd)
1018 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1020 struct f_uas *fu = cmd->fu;
1022 if (fu->flags & USBG_IS_BOT)
1023 return bot_send_write_request(cmd);
1025 return uasp_send_write_request(cmd);
1028 static int usbg_send_read_response(struct se_cmd *se_cmd)
1030 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1032 struct f_uas *fu = cmd->fu;
1034 if (fu->flags & USBG_IS_BOT)
1035 return bot_send_read_response(cmd);
1037 return uasp_send_read_response(cmd);
1040 static void usbg_cmd_work(struct work_struct *work)
1042 struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
1043 struct se_cmd *se_cmd;
1044 struct tcm_usbg_nexus *tv_nexus;
1045 struct usbg_tpg *tpg;
1046 int dir, flags = (TARGET_SCF_UNKNOWN_SIZE | TARGET_SCF_ACK_KREF);
1048 se_cmd = &cmd->se_cmd;
1050 tv_nexus = tpg->tpg_nexus;
1051 dir = get_cmd_dir(cmd->cmd_buf);
1053 __target_init_cmd(se_cmd,
1054 tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
1055 tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
1056 cmd->prio_attr, cmd->sense_iu.sense,
1061 target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess, cmd->cmd_buf,
1062 cmd->sense_iu.sense, cmd->unpacked_lun, 0,
1063 cmd->prio_attr, dir, flags);
1067 transport_send_check_condition_and_sense(se_cmd,
1068 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1069 transport_generic_free_cmd(&cmd->se_cmd, 0);
1072 static struct usbg_cmd *usbg_get_cmd(struct f_uas *fu,
1073 struct tcm_usbg_nexus *tv_nexus, u32 scsi_tag)
1075 struct se_session *se_sess = tv_nexus->tvn_se_sess;
1076 struct usbg_cmd *cmd;
1079 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
1081 return ERR_PTR(-ENOMEM);
1083 cmd = &((struct usbg_cmd *)se_sess->sess_cmd_map)[tag];
1084 memset(cmd, 0, sizeof(*cmd));
1085 cmd->se_cmd.map_tag = tag;
1086 cmd->se_cmd.map_cpu = cpu;
1087 cmd->se_cmd.tag = cmd->tag = scsi_tag;
1093 static void usbg_release_cmd(struct se_cmd *);
1095 static int usbg_submit_command(struct f_uas *fu,
1096 void *cmdbuf, unsigned int len)
1098 struct command_iu *cmd_iu = cmdbuf;
1099 struct usbg_cmd *cmd;
1100 struct usbg_tpg *tpg = fu->tpg;
1101 struct tcm_usbg_nexus *tv_nexus;
1105 if (cmd_iu->iu_id != IU_ID_COMMAND) {
1106 pr_err("Unsupported type %d\n", cmd_iu->iu_id);
1110 tv_nexus = tpg->tpg_nexus;
1112 pr_err("Missing nexus, ignoring command\n");
1116 cmd_len = (cmd_iu->len & ~0x3) + 16;
1117 if (cmd_len > USBG_MAX_CMD)
1120 scsi_tag = be16_to_cpup(&cmd_iu->tag);
1121 cmd = usbg_get_cmd(fu, tv_nexus, scsi_tag);
1123 pr_err("usbg_get_cmd failed\n");
1126 memcpy(cmd->cmd_buf, cmd_iu->cdb, cmd_len);
1128 if (fu->flags & USBG_USE_STREAMS) {
1129 if (cmd->tag > UASP_SS_EP_COMP_NUM_STREAMS)
1132 cmd->stream = &fu->stream[0];
1134 cmd->stream = &fu->stream[cmd->tag - 1];
1136 cmd->stream = &fu->stream[0];
1139 switch (cmd_iu->prio_attr & 0x7) {
1141 cmd->prio_attr = TCM_HEAD_TAG;
1143 case UAS_ORDERED_TAG:
1144 cmd->prio_attr = TCM_ORDERED_TAG;
1147 cmd->prio_attr = TCM_ACA_TAG;
1150 pr_debug_once("Unsupported prio_attr: %02x.\n",
1153 case UAS_SIMPLE_TAG:
1154 cmd->prio_attr = TCM_SIMPLE_TAG;
1158 cmd->unpacked_lun = scsilun_to_int(&cmd_iu->lun);
1160 INIT_WORK(&cmd->work, usbg_cmd_work);
1161 queue_work(tpg->workqueue, &cmd->work);
1165 usbg_release_cmd(&cmd->se_cmd);
1169 static void bot_cmd_work(struct work_struct *work)
1171 struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
1172 struct se_cmd *se_cmd;
1173 struct tcm_usbg_nexus *tv_nexus;
1174 struct usbg_tpg *tpg;
1177 se_cmd = &cmd->se_cmd;
1179 tv_nexus = tpg->tpg_nexus;
1180 dir = get_cmd_dir(cmd->cmd_buf);
1182 __target_init_cmd(se_cmd,
1183 tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
1184 tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
1185 cmd->prio_attr, cmd->sense_iu.sense,
1190 target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
1191 cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
1192 cmd->data_len, cmd->prio_attr, dir, 0);
1196 transport_send_check_condition_and_sense(se_cmd,
1197 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1198 transport_generic_free_cmd(&cmd->se_cmd, 0);
1201 static int bot_submit_command(struct f_uas *fu,
1202 void *cmdbuf, unsigned int len)
1204 struct bulk_cb_wrap *cbw = cmdbuf;
1205 struct usbg_cmd *cmd;
1206 struct usbg_tpg *tpg = fu->tpg;
1207 struct tcm_usbg_nexus *tv_nexus;
1210 if (cbw->Signature != cpu_to_le32(US_BULK_CB_SIGN)) {
1211 pr_err("Wrong signature on CBW\n");
1215 pr_err("Wrong length for CBW\n");
1219 cmd_len = cbw->Length;
1220 if (cmd_len < 1 || cmd_len > 16)
1223 tv_nexus = tpg->tpg_nexus;
1225 pr_err("Missing nexus, ignoring command\n");
1229 cmd = usbg_get_cmd(fu, tv_nexus, cbw->Tag);
1231 pr_err("usbg_get_cmd failed\n");
1234 memcpy(cmd->cmd_buf, cbw->CDB, cmd_len);
1236 cmd->bot_tag = cbw->Tag;
1237 cmd->prio_attr = TCM_SIMPLE_TAG;
1238 cmd->unpacked_lun = cbw->Lun;
1239 cmd->is_read = cbw->Flags & US_BULK_FLAG_IN ? 1 : 0;
1240 cmd->data_len = le32_to_cpu(cbw->DataTransferLength);
1241 cmd->se_cmd.tag = le32_to_cpu(cmd->bot_tag);
1243 INIT_WORK(&cmd->work, bot_cmd_work);
1244 queue_work(tpg->workqueue, &cmd->work);
1249 /* Start fabric.c code */
1251 static int usbg_check_true(struct se_portal_group *se_tpg)
1256 static int usbg_check_false(struct se_portal_group *se_tpg)
1261 static char *usbg_get_fabric_wwn(struct se_portal_group *se_tpg)
1263 struct usbg_tpg *tpg = container_of(se_tpg,
1264 struct usbg_tpg, se_tpg);
1265 struct usbg_tport *tport = tpg->tport;
1267 return &tport->tport_name[0];
1270 static u16 usbg_get_tag(struct se_portal_group *se_tpg)
1272 struct usbg_tpg *tpg = container_of(se_tpg,
1273 struct usbg_tpg, se_tpg);
1274 return tpg->tport_tpgt;
1277 static u32 usbg_tpg_get_inst_index(struct se_portal_group *se_tpg)
1282 static void usbg_release_cmd(struct se_cmd *se_cmd)
1284 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1286 struct se_session *se_sess = se_cmd->se_sess;
1288 kfree(cmd->data_buf);
1289 target_free_tag(se_sess, se_cmd);
1292 static u32 usbg_sess_get_index(struct se_session *se_sess)
1297 static void usbg_set_default_node_attrs(struct se_node_acl *nacl)
1301 static int usbg_get_cmd_state(struct se_cmd *se_cmd)
1306 static void usbg_queue_tm_rsp(struct se_cmd *se_cmd)
1310 static void usbg_aborted_task(struct se_cmd *se_cmd)
1314 static const char *usbg_check_wwn(const char *name)
1319 n = strstr(name, "naa.");
1324 if (len == 0 || len > USBG_NAMELEN - 1)
1329 static int usbg_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
1331 if (!usbg_check_wwn(name))
1336 static struct se_portal_group *usbg_make_tpg(struct se_wwn *wwn,
1339 struct usbg_tport *tport = container_of(wwn, struct usbg_tport,
1341 struct usbg_tpg *tpg;
1344 struct f_tcm_opts *opts;
1347 if (strstr(name, "tpgt_") != name)
1348 return ERR_PTR(-EINVAL);
1349 if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX)
1350 return ERR_PTR(-EINVAL);
1352 mutex_lock(&tpg_instances_lock);
1353 for (i = 0; i < TPG_INSTANCES; ++i)
1354 if (tpg_instances[i].func_inst && !tpg_instances[i].tpg)
1356 if (i == TPG_INSTANCES)
1359 opts = container_of(tpg_instances[i].func_inst, struct f_tcm_opts,
1361 mutex_lock(&opts->dep_lock);
1365 if (opts->has_dep) {
1366 if (!try_module_get(opts->dependent))
1369 ret = configfs_depend_item_unlocked(
1370 wwn->wwn_group.cg_subsys,
1371 &opts->func_inst.group.cg_item);
1376 tpg = kzalloc(sizeof(struct usbg_tpg), GFP_KERNEL);
1380 mutex_init(&tpg->tpg_mutex);
1381 atomic_set(&tpg->tpg_port_count, 0);
1382 tpg->workqueue = alloc_workqueue("tcm_usb_gadget", 0, 1);
1383 if (!tpg->workqueue)
1387 tpg->tport_tpgt = tpgt;
1390 * SPC doesn't assign a protocol identifier for USB-SCSI, so we
1391 * pretend to be SAS..
1393 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_SAS);
1395 goto free_workqueue;
1397 tpg_instances[i].tpg = tpg;
1398 tpg->fi = tpg_instances[i].func_inst;
1399 mutex_unlock(&opts->dep_lock);
1400 mutex_unlock(&tpg_instances_lock);
1401 return &tpg->se_tpg;
1404 destroy_workqueue(tpg->workqueue);
1409 module_put(opts->dependent);
1411 configfs_undepend_item_unlocked(&opts->func_inst.group.cg_item);
1413 mutex_unlock(&opts->dep_lock);
1415 mutex_unlock(&tpg_instances_lock);
1417 return ERR_PTR(ret);
1420 static int tcm_usbg_drop_nexus(struct usbg_tpg *);
1422 static void usbg_drop_tpg(struct se_portal_group *se_tpg)
1424 struct usbg_tpg *tpg = container_of(se_tpg,
1425 struct usbg_tpg, se_tpg);
1427 struct f_tcm_opts *opts;
1429 tcm_usbg_drop_nexus(tpg);
1430 core_tpg_deregister(se_tpg);
1431 destroy_workqueue(tpg->workqueue);
1433 mutex_lock(&tpg_instances_lock);
1434 for (i = 0; i < TPG_INSTANCES; ++i)
1435 if (tpg_instances[i].tpg == tpg)
1437 if (i < TPG_INSTANCES) {
1438 tpg_instances[i].tpg = NULL;
1439 opts = container_of(tpg_instances[i].func_inst,
1440 struct f_tcm_opts, func_inst);
1441 mutex_lock(&opts->dep_lock);
1443 module_put(opts->dependent);
1445 configfs_undepend_item_unlocked(
1446 &opts->func_inst.group.cg_item);
1447 mutex_unlock(&opts->dep_lock);
1449 mutex_unlock(&tpg_instances_lock);
1454 static struct se_wwn *usbg_make_tport(
1455 struct target_fabric_configfs *tf,
1456 struct config_group *group,
1459 struct usbg_tport *tport;
1460 const char *wnn_name;
1463 wnn_name = usbg_check_wwn(name);
1465 return ERR_PTR(-EINVAL);
1467 tport = kzalloc(sizeof(struct usbg_tport), GFP_KERNEL);
1469 return ERR_PTR(-ENOMEM);
1471 tport->tport_wwpn = wwpn;
1472 snprintf(tport->tport_name, sizeof(tport->tport_name), "%s", wnn_name);
1473 return &tport->tport_wwn;
1476 static void usbg_drop_tport(struct se_wwn *wwn)
1478 struct usbg_tport *tport = container_of(wwn,
1479 struct usbg_tport, tport_wwn);
1484 * If somebody feels like dropping the version property, go ahead.
1486 static ssize_t usbg_wwn_version_show(struct config_item *item, char *page)
1488 return sprintf(page, "usb-gadget fabric module\n");
1491 CONFIGFS_ATTR_RO(usbg_wwn_, version);
1493 static struct configfs_attribute *usbg_wwn_attrs[] = {
1494 &usbg_wwn_attr_version,
1498 static ssize_t tcm_usbg_tpg_enable_show(struct config_item *item, char *page)
1500 struct se_portal_group *se_tpg = to_tpg(item);
1501 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1503 return snprintf(page, PAGE_SIZE, "%u\n", tpg->gadget_connect);
1506 static int usbg_attach(struct usbg_tpg *);
1507 static void usbg_detach(struct usbg_tpg *);
1509 static ssize_t tcm_usbg_tpg_enable_store(struct config_item *item,
1510 const char *page, size_t count)
1512 struct se_portal_group *se_tpg = to_tpg(item);
1513 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1517 ret = strtobool(page, &op);
1521 if ((op && tpg->gadget_connect) || (!op && !tpg->gadget_connect))
1525 ret = usbg_attach(tpg);
1531 tpg->gadget_connect = op;
1536 static ssize_t tcm_usbg_tpg_nexus_show(struct config_item *item, char *page)
1538 struct se_portal_group *se_tpg = to_tpg(item);
1539 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1540 struct tcm_usbg_nexus *tv_nexus;
1543 mutex_lock(&tpg->tpg_mutex);
1544 tv_nexus = tpg->tpg_nexus;
1549 ret = snprintf(page, PAGE_SIZE, "%s\n",
1550 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1552 mutex_unlock(&tpg->tpg_mutex);
1556 static int usbg_alloc_sess_cb(struct se_portal_group *se_tpg,
1557 struct se_session *se_sess, void *p)
1559 struct usbg_tpg *tpg = container_of(se_tpg,
1560 struct usbg_tpg, se_tpg);
1566 static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name)
1568 struct tcm_usbg_nexus *tv_nexus;
1571 mutex_lock(&tpg->tpg_mutex);
1572 if (tpg->tpg_nexus) {
1574 pr_debug("tpg->tpg_nexus already exists\n");
1578 tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
1584 tv_nexus->tvn_se_sess = target_setup_session(&tpg->se_tpg,
1585 USB_G_DEFAULT_SESSION_TAGS,
1586 sizeof(struct usbg_cmd),
1587 TARGET_PROT_NORMAL, name,
1588 tv_nexus, usbg_alloc_sess_cb);
1589 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1590 #define MAKE_NEXUS_MSG "core_tpg_check_initiator_node_acl() failed for %s\n"
1591 pr_debug(MAKE_NEXUS_MSG, name);
1592 #undef MAKE_NEXUS_MSG
1593 ret = PTR_ERR(tv_nexus->tvn_se_sess);
1598 mutex_unlock(&tpg->tpg_mutex);
1602 static int tcm_usbg_drop_nexus(struct usbg_tpg *tpg)
1604 struct se_session *se_sess;
1605 struct tcm_usbg_nexus *tv_nexus;
1608 mutex_lock(&tpg->tpg_mutex);
1609 tv_nexus = tpg->tpg_nexus;
1613 se_sess = tv_nexus->tvn_se_sess;
1617 if (atomic_read(&tpg->tpg_port_count)) {
1619 #define MSG "Unable to remove Host I_T Nexus with active TPG port count: %d\n"
1620 pr_err(MSG, atomic_read(&tpg->tpg_port_count));
1625 pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
1626 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1628 * Release the SCSI I_T Nexus to the emulated vHost Target Port
1630 target_remove_session(se_sess);
1631 tpg->tpg_nexus = NULL;
1636 mutex_unlock(&tpg->tpg_mutex);
1640 static ssize_t tcm_usbg_tpg_nexus_store(struct config_item *item,
1641 const char *page, size_t count)
1643 struct se_portal_group *se_tpg = to_tpg(item);
1644 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1645 unsigned char i_port[USBG_NAMELEN], *ptr;
1648 if (!strncmp(page, "NULL", 4)) {
1649 ret = tcm_usbg_drop_nexus(tpg);
1650 return (!ret) ? count : ret;
1652 if (strlen(page) >= USBG_NAMELEN) {
1654 #define NEXUS_STORE_MSG "Emulated NAA Sas Address: %s, exceeds max: %d\n"
1655 pr_err(NEXUS_STORE_MSG, page, USBG_NAMELEN);
1656 #undef NEXUS_STORE_MSG
1659 snprintf(i_port, USBG_NAMELEN, "%s", page);
1661 ptr = strstr(i_port, "naa.");
1663 pr_err("Missing 'naa.' prefix\n");
1667 if (i_port[strlen(i_port) - 1] == '\n')
1668 i_port[strlen(i_port) - 1] = '\0';
1670 ret = tcm_usbg_make_nexus(tpg, &i_port[0]);
1676 CONFIGFS_ATTR(tcm_usbg_tpg_, enable);
1677 CONFIGFS_ATTR(tcm_usbg_tpg_, nexus);
1679 static struct configfs_attribute *usbg_base_attrs[] = {
1680 &tcm_usbg_tpg_attr_enable,
1681 &tcm_usbg_tpg_attr_nexus,
1685 static int usbg_port_link(struct se_portal_group *se_tpg, struct se_lun *lun)
1687 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1689 atomic_inc(&tpg->tpg_port_count);
1690 smp_mb__after_atomic();
1694 static void usbg_port_unlink(struct se_portal_group *se_tpg,
1695 struct se_lun *se_lun)
1697 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1699 atomic_dec(&tpg->tpg_port_count);
1700 smp_mb__after_atomic();
1703 static int usbg_check_stop_free(struct se_cmd *se_cmd)
1705 return target_put_sess_cmd(se_cmd);
1708 static const struct target_core_fabric_ops usbg_ops = {
1709 .module = THIS_MODULE,
1710 .fabric_name = "usb_gadget",
1711 .tpg_get_wwn = usbg_get_fabric_wwn,
1712 .tpg_get_tag = usbg_get_tag,
1713 .tpg_check_demo_mode = usbg_check_true,
1714 .tpg_check_demo_mode_cache = usbg_check_false,
1715 .tpg_check_demo_mode_write_protect = usbg_check_false,
1716 .tpg_check_prod_mode_write_protect = usbg_check_false,
1717 .tpg_get_inst_index = usbg_tpg_get_inst_index,
1718 .release_cmd = usbg_release_cmd,
1719 .sess_get_index = usbg_sess_get_index,
1720 .sess_get_initiator_sid = NULL,
1721 .write_pending = usbg_send_write_request,
1722 .set_default_node_attributes = usbg_set_default_node_attrs,
1723 .get_cmd_state = usbg_get_cmd_state,
1724 .queue_data_in = usbg_send_read_response,
1725 .queue_status = usbg_send_status_response,
1726 .queue_tm_rsp = usbg_queue_tm_rsp,
1727 .aborted_task = usbg_aborted_task,
1728 .check_stop_free = usbg_check_stop_free,
1730 .fabric_make_wwn = usbg_make_tport,
1731 .fabric_drop_wwn = usbg_drop_tport,
1732 .fabric_make_tpg = usbg_make_tpg,
1733 .fabric_drop_tpg = usbg_drop_tpg,
1734 .fabric_post_link = usbg_port_link,
1735 .fabric_pre_unlink = usbg_port_unlink,
1736 .fabric_init_nodeacl = usbg_init_nodeacl,
1738 .tfc_wwn_attrs = usbg_wwn_attrs,
1739 .tfc_tpg_base_attrs = usbg_base_attrs,
1742 /* Start gadget.c code */
1744 static struct usb_interface_descriptor bot_intf_desc = {
1745 .bLength = sizeof(bot_intf_desc),
1746 .bDescriptorType = USB_DT_INTERFACE,
1748 .bAlternateSetting = USB_G_ALT_INT_BBB,
1749 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1750 .bInterfaceSubClass = USB_SC_SCSI,
1751 .bInterfaceProtocol = USB_PR_BULK,
1754 static struct usb_interface_descriptor uasp_intf_desc = {
1755 .bLength = sizeof(uasp_intf_desc),
1756 .bDescriptorType = USB_DT_INTERFACE,
1758 .bAlternateSetting = USB_G_ALT_INT_UAS,
1759 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1760 .bInterfaceSubClass = USB_SC_SCSI,
1761 .bInterfaceProtocol = USB_PR_UAS,
1764 static struct usb_endpoint_descriptor uasp_bi_desc = {
1765 .bLength = USB_DT_ENDPOINT_SIZE,
1766 .bDescriptorType = USB_DT_ENDPOINT,
1767 .bEndpointAddress = USB_DIR_IN,
1768 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1769 .wMaxPacketSize = cpu_to_le16(512),
1772 static struct usb_endpoint_descriptor uasp_fs_bi_desc = {
1773 .bLength = USB_DT_ENDPOINT_SIZE,
1774 .bDescriptorType = USB_DT_ENDPOINT,
1775 .bEndpointAddress = USB_DIR_IN,
1776 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1779 static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc = {
1780 .bLength = sizeof(uasp_bi_pipe_desc),
1781 .bDescriptorType = USB_DT_PIPE_USAGE,
1782 .bPipeID = DATA_IN_PIPE_ID,
1785 static struct usb_endpoint_descriptor uasp_ss_bi_desc = {
1786 .bLength = USB_DT_ENDPOINT_SIZE,
1787 .bDescriptorType = USB_DT_ENDPOINT,
1788 .bEndpointAddress = USB_DIR_IN,
1789 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1790 .wMaxPacketSize = cpu_to_le16(1024),
1793 static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc = {
1794 .bLength = sizeof(uasp_bi_ep_comp_desc),
1795 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1797 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1798 .wBytesPerInterval = 0,
1801 static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc = {
1802 .bLength = sizeof(bot_bi_ep_comp_desc),
1803 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1807 static struct usb_endpoint_descriptor uasp_bo_desc = {
1808 .bLength = USB_DT_ENDPOINT_SIZE,
1809 .bDescriptorType = USB_DT_ENDPOINT,
1810 .bEndpointAddress = USB_DIR_OUT,
1811 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1812 .wMaxPacketSize = cpu_to_le16(512),
1815 static struct usb_endpoint_descriptor uasp_fs_bo_desc = {
1816 .bLength = USB_DT_ENDPOINT_SIZE,
1817 .bDescriptorType = USB_DT_ENDPOINT,
1818 .bEndpointAddress = USB_DIR_OUT,
1819 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1822 static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc = {
1823 .bLength = sizeof(uasp_bo_pipe_desc),
1824 .bDescriptorType = USB_DT_PIPE_USAGE,
1825 .bPipeID = DATA_OUT_PIPE_ID,
1828 static struct usb_endpoint_descriptor uasp_ss_bo_desc = {
1829 .bLength = USB_DT_ENDPOINT_SIZE,
1830 .bDescriptorType = USB_DT_ENDPOINT,
1831 .bEndpointAddress = USB_DIR_OUT,
1832 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1833 .wMaxPacketSize = cpu_to_le16(0x400),
1836 static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc = {
1837 .bLength = sizeof(uasp_bo_ep_comp_desc),
1838 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1839 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1842 static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc = {
1843 .bLength = sizeof(bot_bo_ep_comp_desc),
1844 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1847 static struct usb_endpoint_descriptor uasp_status_desc = {
1848 .bLength = USB_DT_ENDPOINT_SIZE,
1849 .bDescriptorType = USB_DT_ENDPOINT,
1850 .bEndpointAddress = USB_DIR_IN,
1851 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1852 .wMaxPacketSize = cpu_to_le16(512),
1855 static struct usb_endpoint_descriptor uasp_fs_status_desc = {
1856 .bLength = USB_DT_ENDPOINT_SIZE,
1857 .bDescriptorType = USB_DT_ENDPOINT,
1858 .bEndpointAddress = USB_DIR_IN,
1859 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1862 static struct usb_pipe_usage_descriptor uasp_status_pipe_desc = {
1863 .bLength = sizeof(uasp_status_pipe_desc),
1864 .bDescriptorType = USB_DT_PIPE_USAGE,
1865 .bPipeID = STATUS_PIPE_ID,
1868 static struct usb_endpoint_descriptor uasp_ss_status_desc = {
1869 .bLength = USB_DT_ENDPOINT_SIZE,
1870 .bDescriptorType = USB_DT_ENDPOINT,
1871 .bEndpointAddress = USB_DIR_IN,
1872 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1873 .wMaxPacketSize = cpu_to_le16(1024),
1876 static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc = {
1877 .bLength = sizeof(uasp_status_in_ep_comp_desc),
1878 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1879 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1882 static struct usb_endpoint_descriptor uasp_cmd_desc = {
1883 .bLength = USB_DT_ENDPOINT_SIZE,
1884 .bDescriptorType = USB_DT_ENDPOINT,
1885 .bEndpointAddress = USB_DIR_OUT,
1886 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1887 .wMaxPacketSize = cpu_to_le16(512),
1890 static struct usb_endpoint_descriptor uasp_fs_cmd_desc = {
1891 .bLength = USB_DT_ENDPOINT_SIZE,
1892 .bDescriptorType = USB_DT_ENDPOINT,
1893 .bEndpointAddress = USB_DIR_OUT,
1894 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1897 static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc = {
1898 .bLength = sizeof(uasp_cmd_pipe_desc),
1899 .bDescriptorType = USB_DT_PIPE_USAGE,
1900 .bPipeID = CMD_PIPE_ID,
1903 static struct usb_endpoint_descriptor uasp_ss_cmd_desc = {
1904 .bLength = USB_DT_ENDPOINT_SIZE,
1905 .bDescriptorType = USB_DT_ENDPOINT,
1906 .bEndpointAddress = USB_DIR_OUT,
1907 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1908 .wMaxPacketSize = cpu_to_le16(1024),
1911 static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc = {
1912 .bLength = sizeof(uasp_cmd_comp_desc),
1913 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1916 static struct usb_descriptor_header *uasp_fs_function_desc[] = {
1917 (struct usb_descriptor_header *) &bot_intf_desc,
1918 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
1919 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
1921 (struct usb_descriptor_header *) &uasp_intf_desc,
1922 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
1923 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
1924 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
1925 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
1926 (struct usb_descriptor_header *) &uasp_fs_status_desc,
1927 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
1928 (struct usb_descriptor_header *) &uasp_fs_cmd_desc,
1929 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
1933 static struct usb_descriptor_header *uasp_hs_function_desc[] = {
1934 (struct usb_descriptor_header *) &bot_intf_desc,
1935 (struct usb_descriptor_header *) &uasp_bi_desc,
1936 (struct usb_descriptor_header *) &uasp_bo_desc,
1938 (struct usb_descriptor_header *) &uasp_intf_desc,
1939 (struct usb_descriptor_header *) &uasp_bi_desc,
1940 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
1941 (struct usb_descriptor_header *) &uasp_bo_desc,
1942 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
1943 (struct usb_descriptor_header *) &uasp_status_desc,
1944 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
1945 (struct usb_descriptor_header *) &uasp_cmd_desc,
1946 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
1950 static struct usb_descriptor_header *uasp_ss_function_desc[] = {
1951 (struct usb_descriptor_header *) &bot_intf_desc,
1952 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
1953 (struct usb_descriptor_header *) &bot_bi_ep_comp_desc,
1954 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
1955 (struct usb_descriptor_header *) &bot_bo_ep_comp_desc,
1957 (struct usb_descriptor_header *) &uasp_intf_desc,
1958 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
1959 (struct usb_descriptor_header *) &uasp_bi_ep_comp_desc,
1960 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
1961 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
1962 (struct usb_descriptor_header *) &uasp_bo_ep_comp_desc,
1963 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
1964 (struct usb_descriptor_header *) &uasp_ss_status_desc,
1965 (struct usb_descriptor_header *) &uasp_status_in_ep_comp_desc,
1966 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
1967 (struct usb_descriptor_header *) &uasp_ss_cmd_desc,
1968 (struct usb_descriptor_header *) &uasp_cmd_comp_desc,
1969 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
1973 static struct usb_string tcm_us_strings[] = {
1974 [USB_G_STR_INT_UAS].s = "USB Attached SCSI",
1975 [USB_G_STR_INT_BBB].s = "Bulk Only Transport",
1979 static struct usb_gadget_strings tcm_stringtab = {
1981 .strings = tcm_us_strings,
1984 static struct usb_gadget_strings *tcm_strings[] = {
1989 static int tcm_bind(struct usb_configuration *c, struct usb_function *f)
1991 struct f_uas *fu = to_f_uas(f);
1992 struct usb_string *us;
1993 struct usb_gadget *gadget = c->cdev->gadget;
1995 struct f_tcm_opts *opts;
1999 opts = container_of(f->fi, struct f_tcm_opts, func_inst);
2001 mutex_lock(&opts->dep_lock);
2002 if (!opts->can_attach) {
2003 mutex_unlock(&opts->dep_lock);
2006 mutex_unlock(&opts->dep_lock);
2007 us = usb_gstrings_attach(c->cdev, tcm_strings,
2008 ARRAY_SIZE(tcm_us_strings));
2011 bot_intf_desc.iInterface = us[USB_G_STR_INT_BBB].id;
2012 uasp_intf_desc.iInterface = us[USB_G_STR_INT_UAS].id;
2014 iface = usb_interface_id(c, f);
2018 bot_intf_desc.bInterfaceNumber = iface;
2019 uasp_intf_desc.bInterfaceNumber = iface;
2021 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bi_desc,
2022 &uasp_bi_ep_comp_desc);
2028 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bo_desc,
2029 &uasp_bo_ep_comp_desc);
2034 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_status_desc,
2035 &uasp_status_in_ep_comp_desc);
2040 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_cmd_desc,
2041 &uasp_cmd_comp_desc);
2046 /* Assume endpoint addresses are the same for both speeds */
2047 uasp_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2048 uasp_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2049 uasp_status_desc.bEndpointAddress =
2050 uasp_ss_status_desc.bEndpointAddress;
2051 uasp_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2053 uasp_fs_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2054 uasp_fs_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2055 uasp_fs_status_desc.bEndpointAddress =
2056 uasp_ss_status_desc.bEndpointAddress;
2057 uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2059 ret = usb_assign_descriptors(f, uasp_fs_function_desc,
2060 uasp_hs_function_desc, uasp_ss_function_desc,
2061 uasp_ss_function_desc);
2067 pr_err("Can't claim all required eps\n");
2072 struct guas_setup_wq {
2073 struct work_struct work;
2078 static void tcm_delayed_set_alt(struct work_struct *wq)
2080 struct guas_setup_wq *work = container_of(wq, struct guas_setup_wq,
2082 struct f_uas *fu = work->fu;
2083 int alt = work->alt;
2087 if (fu->flags & USBG_IS_BOT)
2088 bot_cleanup_old_alt(fu);
2089 if (fu->flags & USBG_IS_UAS)
2090 uasp_cleanup_old_alt(fu);
2092 if (alt == USB_G_ALT_INT_BBB)
2094 else if (alt == USB_G_ALT_INT_UAS)
2096 usb_composite_setup_continue(fu->function.config->cdev);
2099 static int tcm_get_alt(struct usb_function *f, unsigned intf)
2101 if (intf == bot_intf_desc.bInterfaceNumber)
2102 return USB_G_ALT_INT_BBB;
2103 if (intf == uasp_intf_desc.bInterfaceNumber)
2104 return USB_G_ALT_INT_UAS;
2109 static int tcm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2111 struct f_uas *fu = to_f_uas(f);
2113 if ((alt == USB_G_ALT_INT_BBB) || (alt == USB_G_ALT_INT_UAS)) {
2114 struct guas_setup_wq *work;
2116 work = kmalloc(sizeof(*work), GFP_ATOMIC);
2119 INIT_WORK(&work->work, tcm_delayed_set_alt);
2122 schedule_work(&work->work);
2123 return USB_GADGET_DELAYED_STATUS;
2128 static void tcm_disable(struct usb_function *f)
2130 struct f_uas *fu = to_f_uas(f);
2132 if (fu->flags & USBG_IS_UAS)
2133 uasp_cleanup_old_alt(fu);
2134 else if (fu->flags & USBG_IS_BOT)
2135 bot_cleanup_old_alt(fu);
2139 static int tcm_setup(struct usb_function *f,
2140 const struct usb_ctrlrequest *ctrl)
2142 struct f_uas *fu = to_f_uas(f);
2144 if (!(fu->flags & USBG_IS_BOT))
2147 return usbg_bot_setup(f, ctrl);
2150 static inline struct f_tcm_opts *to_f_tcm_opts(struct config_item *item)
2152 return container_of(to_config_group(item), struct f_tcm_opts,
2156 static void tcm_attr_release(struct config_item *item)
2158 struct f_tcm_opts *opts = to_f_tcm_opts(item);
2160 usb_put_function_instance(&opts->func_inst);
2163 static struct configfs_item_operations tcm_item_ops = {
2164 .release = tcm_attr_release,
2167 static const struct config_item_type tcm_func_type = {
2168 .ct_item_ops = &tcm_item_ops,
2169 .ct_owner = THIS_MODULE,
2172 static void tcm_free_inst(struct usb_function_instance *f)
2174 struct f_tcm_opts *opts;
2177 opts = container_of(f, struct f_tcm_opts, func_inst);
2179 mutex_lock(&tpg_instances_lock);
2180 for (i = 0; i < TPG_INSTANCES; ++i)
2181 if (tpg_instances[i].func_inst == f)
2183 if (i < TPG_INSTANCES)
2184 tpg_instances[i].func_inst = NULL;
2185 mutex_unlock(&tpg_instances_lock);
2190 static int tcm_register_callback(struct usb_function_instance *f)
2192 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2194 mutex_lock(&opts->dep_lock);
2195 opts->can_attach = true;
2196 mutex_unlock(&opts->dep_lock);
2201 static void tcm_unregister_callback(struct usb_function_instance *f)
2203 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2205 mutex_lock(&opts->dep_lock);
2206 unregister_gadget_item(opts->
2207 func_inst.group.cg_item.ci_parent->ci_parent);
2208 opts->can_attach = false;
2209 mutex_unlock(&opts->dep_lock);
2212 static int usbg_attach(struct usbg_tpg *tpg)
2214 struct usb_function_instance *f = tpg->fi;
2215 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2217 if (opts->tcm_register_callback)
2218 return opts->tcm_register_callback(f);
2223 static void usbg_detach(struct usbg_tpg *tpg)
2225 struct usb_function_instance *f = tpg->fi;
2226 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2228 if (opts->tcm_unregister_callback)
2229 opts->tcm_unregister_callback(f);
2232 static int tcm_set_name(struct usb_function_instance *f, const char *name)
2234 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2236 pr_debug("tcm: Activating %s\n", name);
2238 mutex_lock(&opts->dep_lock);
2240 mutex_unlock(&opts->dep_lock);
2245 static struct usb_function_instance *tcm_alloc_inst(void)
2247 struct f_tcm_opts *opts;
2251 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2253 return ERR_PTR(-ENOMEM);
2255 mutex_lock(&tpg_instances_lock);
2256 for (i = 0; i < TPG_INSTANCES; ++i)
2257 if (!tpg_instances[i].func_inst)
2260 if (i == TPG_INSTANCES) {
2261 mutex_unlock(&tpg_instances_lock);
2263 return ERR_PTR(-EBUSY);
2265 tpg_instances[i].func_inst = &opts->func_inst;
2266 mutex_unlock(&tpg_instances_lock);
2268 mutex_init(&opts->dep_lock);
2269 opts->func_inst.set_inst_name = tcm_set_name;
2270 opts->func_inst.free_func_inst = tcm_free_inst;
2271 opts->tcm_register_callback = tcm_register_callback;
2272 opts->tcm_unregister_callback = tcm_unregister_callback;
2274 config_group_init_type_name(&opts->func_inst.group, "",
2277 return &opts->func_inst;
2280 static void tcm_free(struct usb_function *f)
2282 struct f_uas *tcm = to_f_uas(f);
2287 static void tcm_unbind(struct usb_configuration *c, struct usb_function *f)
2289 usb_free_all_descriptors(f);
2292 static struct usb_function *tcm_alloc(struct usb_function_instance *fi)
2297 mutex_lock(&tpg_instances_lock);
2298 for (i = 0; i < TPG_INSTANCES; ++i)
2299 if (tpg_instances[i].func_inst == fi)
2301 if (i == TPG_INSTANCES) {
2302 mutex_unlock(&tpg_instances_lock);
2303 return ERR_PTR(-ENODEV);
2306 fu = kzalloc(sizeof(*fu), GFP_KERNEL);
2308 mutex_unlock(&tpg_instances_lock);
2309 return ERR_PTR(-ENOMEM);
2312 fu->function.name = "Target Function";
2313 fu->function.bind = tcm_bind;
2314 fu->function.unbind = tcm_unbind;
2315 fu->function.set_alt = tcm_set_alt;
2316 fu->function.get_alt = tcm_get_alt;
2317 fu->function.setup = tcm_setup;
2318 fu->function.disable = tcm_disable;
2319 fu->function.free_func = tcm_free;
2320 fu->tpg = tpg_instances[i].tpg;
2321 mutex_unlock(&tpg_instances_lock);
2323 return &fu->function;
2326 DECLARE_USB_FUNCTION(tcm, tcm_alloc_inst, tcm_alloc);
2328 static int tcm_init(void)
2332 ret = usb_function_register(&tcmusb_func);
2336 ret = target_register_template(&usbg_ops);
2338 usb_function_unregister(&tcmusb_func);
2342 module_init(tcm_init);
2344 static void tcm_exit(void)
2346 target_unregister_template(&usbg_ops);
2347 usb_function_unregister(&tcmusb_func);
2349 module_exit(tcm_exit);
2351 MODULE_LICENSE("GPL");
2352 MODULE_AUTHOR("Sebastian Andrzej Siewior");