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;
86 unsigned int csw_stat;
88 csw_stat = cmd->csw_code;
91 * We can't send SENSE as a response. So we take ASC & ASCQ from our
92 * sense buffer and queue it and hope the host sends a REQUEST_SENSE
93 * command where it learns why we failed.
95 sense = cmd->sense_iu.sense;
97 csw->Tag = cmd->bot_tag;
98 csw->Status = csw_stat;
99 fu->bot_status.req->context = cmd;
100 ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_ATOMIC);
102 pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
105 static void bot_err_compl(struct usb_ep *ep, struct usb_request *req)
107 struct usbg_cmd *cmd = req->context;
108 struct f_uas *fu = cmd->fu;
111 pr_err("ERR %s(%d)\n", __func__, __LINE__);
114 if (cmd->data_len > ep->maxpacket) {
115 req->length = ep->maxpacket;
116 cmd->data_len -= ep->maxpacket;
118 req->length = cmd->data_len;
122 usb_ep_queue(ep, req, GFP_ATOMIC);
125 bot_enqueue_sense_code(fu, cmd);
128 static void bot_send_bad_status(struct usbg_cmd *cmd)
130 struct f_uas *fu = cmd->fu;
131 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
132 struct usb_request *req;
135 csw->Residue = cpu_to_le32(cmd->data_len);
140 req = fu->bot_req_in;
143 req = fu->bot_req_out;
146 if (cmd->data_len > fu->ep_in->maxpacket) {
147 req->length = ep->maxpacket;
148 cmd->data_len -= ep->maxpacket;
150 req->length = cmd->data_len;
153 req->complete = bot_err_compl;
155 req->buf = fu->cmd.buf;
156 usb_ep_queue(ep, req, GFP_KERNEL);
158 bot_enqueue_sense_code(fu, cmd);
162 static int bot_send_status(struct usbg_cmd *cmd, bool moved_data)
164 struct f_uas *fu = cmd->fu;
165 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
168 if (cmd->se_cmd.scsi_status == SAM_STAT_GOOD) {
169 if (!moved_data && cmd->data_len) {
171 * the host wants to move data, we don't. Fill / empty
172 * the pipe and then send the csw with reside set.
174 cmd->csw_code = US_BULK_STAT_OK;
175 bot_send_bad_status(cmd);
179 csw->Tag = cmd->bot_tag;
180 csw->Residue = cpu_to_le32(0);
181 csw->Status = US_BULK_STAT_OK;
182 fu->bot_status.req->context = cmd;
184 ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_KERNEL);
186 pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
188 cmd->csw_code = US_BULK_STAT_FAIL;
189 bot_send_bad_status(cmd);
195 * Called after command (no data transfer) or after the write (to device)
196 * operation is completed
198 static int bot_send_status_response(struct usbg_cmd *cmd)
200 bool moved_data = false;
204 return bot_send_status(cmd, moved_data);
207 /* Read request completed, now we have to send the CSW */
208 static void bot_read_compl(struct usb_ep *ep, struct usb_request *req)
210 struct usbg_cmd *cmd = req->context;
213 pr_err("ERR %s(%d)\n", __func__, __LINE__);
215 bot_send_status(cmd, true);
218 static int bot_send_read_response(struct usbg_cmd *cmd)
220 struct f_uas *fu = cmd->fu;
221 struct se_cmd *se_cmd = &cmd->se_cmd;
222 struct usb_gadget *gadget = fuas_to_gadget(fu);
225 if (!cmd->data_len) {
226 cmd->csw_code = US_BULK_STAT_PHASE;
227 bot_send_bad_status(cmd);
231 if (!gadget->sg_supported) {
232 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
236 sg_copy_to_buffer(se_cmd->t_data_sg,
237 se_cmd->t_data_nents,
239 se_cmd->data_length);
241 fu->bot_req_in->buf = cmd->data_buf;
243 fu->bot_req_in->buf = NULL;
244 fu->bot_req_in->num_sgs = se_cmd->t_data_nents;
245 fu->bot_req_in->sg = se_cmd->t_data_sg;
248 fu->bot_req_in->complete = bot_read_compl;
249 fu->bot_req_in->length = se_cmd->data_length;
250 fu->bot_req_in->context = cmd;
251 ret = usb_ep_queue(fu->ep_in, fu->bot_req_in, GFP_ATOMIC);
253 pr_err("%s(%d)\n", __func__, __LINE__);
257 static void usbg_data_write_cmpl(struct usb_ep *, struct usb_request *);
258 static int usbg_prepare_w_request(struct usbg_cmd *, struct usb_request *);
260 static int bot_send_write_request(struct usbg_cmd *cmd)
262 struct f_uas *fu = cmd->fu;
263 struct se_cmd *se_cmd = &cmd->se_cmd;
264 struct usb_gadget *gadget = fuas_to_gadget(fu);
267 init_completion(&cmd->write_complete);
270 if (!cmd->data_len) {
271 cmd->csw_code = US_BULK_STAT_PHASE;
275 if (!gadget->sg_supported) {
276 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_KERNEL);
280 fu->bot_req_out->buf = cmd->data_buf;
282 fu->bot_req_out->buf = NULL;
283 fu->bot_req_out->num_sgs = se_cmd->t_data_nents;
284 fu->bot_req_out->sg = se_cmd->t_data_sg;
287 fu->bot_req_out->complete = usbg_data_write_cmpl;
288 fu->bot_req_out->length = se_cmd->data_length;
289 fu->bot_req_out->context = cmd;
291 ret = usbg_prepare_w_request(cmd, fu->bot_req_out);
294 ret = usb_ep_queue(fu->ep_out, fu->bot_req_out, GFP_KERNEL);
296 pr_err("%s(%d)\n", __func__, __LINE__);
298 wait_for_completion(&cmd->write_complete);
299 target_execute_cmd(se_cmd);
304 static int bot_submit_command(struct f_uas *, void *, unsigned int);
306 static void bot_cmd_complete(struct usb_ep *ep, struct usb_request *req)
308 struct f_uas *fu = req->context;
311 fu->flags &= ~USBG_BOT_CMD_PEND;
316 ret = bot_submit_command(fu, req->buf, req->actual);
318 pr_err("%s(%d): %d\n", __func__, __LINE__, ret);
321 static int bot_prepare_reqs(struct f_uas *fu)
325 fu->bot_req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
329 fu->bot_req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
330 if (!fu->bot_req_out)
333 fu->cmd.req = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
337 fu->bot_status.req = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
338 if (!fu->bot_status.req)
341 fu->bot_status.req->buf = &fu->bot_status.csw;
342 fu->bot_status.req->length = US_BULK_CS_WRAP_LEN;
343 fu->bot_status.req->complete = bot_status_complete;
344 fu->bot_status.csw.Signature = cpu_to_le32(US_BULK_CS_SIGN);
346 fu->cmd.buf = kmalloc(fu->ep_out->maxpacket, GFP_KERNEL);
350 fu->cmd.req->complete = bot_cmd_complete;
351 fu->cmd.req->buf = fu->cmd.buf;
352 fu->cmd.req->length = fu->ep_out->maxpacket;
353 fu->cmd.req->context = fu;
355 ret = bot_enqueue_cmd_cbw(fu);
363 usb_ep_free_request(fu->ep_in, fu->bot_status.req);
365 usb_ep_free_request(fu->ep_out, fu->cmd.req);
368 usb_ep_free_request(fu->ep_out, fu->bot_req_out);
369 fu->bot_req_out = NULL;
371 usb_ep_free_request(fu->ep_in, fu->bot_req_in);
372 fu->bot_req_in = NULL;
374 pr_err("BOT: endpoint setup failed\n");
378 static void bot_cleanup_old_alt(struct f_uas *fu)
380 if (!(fu->flags & USBG_ENABLED))
383 usb_ep_disable(fu->ep_in);
384 usb_ep_disable(fu->ep_out);
389 usb_ep_free_request(fu->ep_in, fu->bot_req_in);
390 usb_ep_free_request(fu->ep_out, fu->bot_req_out);
391 usb_ep_free_request(fu->ep_out, fu->cmd.req);
392 usb_ep_free_request(fu->ep_out, fu->bot_status.req);
396 fu->bot_req_in = NULL;
397 fu->bot_req_out = NULL;
399 fu->bot_status.req = NULL;
403 static void bot_set_alt(struct f_uas *fu)
405 struct usb_function *f = &fu->function;
406 struct usb_gadget *gadget = f->config->cdev->gadget;
409 fu->flags = USBG_IS_BOT;
411 config_ep_by_speed(gadget, f, fu->ep_in);
412 ret = usb_ep_enable(fu->ep_in);
416 config_ep_by_speed(gadget, f, fu->ep_out);
417 ret = usb_ep_enable(fu->ep_out);
421 ret = bot_prepare_reqs(fu);
424 fu->flags |= USBG_ENABLED;
425 pr_info("Using the BOT protocol\n");
428 usb_ep_disable(fu->ep_out);
430 usb_ep_disable(fu->ep_in);
432 fu->flags = USBG_IS_BOT;
435 static int usbg_bot_setup(struct usb_function *f,
436 const struct usb_ctrlrequest *ctrl)
438 struct f_uas *fu = to_f_uas(f);
439 struct usb_composite_dev *cdev = f->config->cdev;
440 u16 w_value = le16_to_cpu(ctrl->wValue);
441 u16 w_length = le16_to_cpu(ctrl->wLength);
445 switch (ctrl->bRequest) {
446 case US_BULK_GET_MAX_LUN:
447 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_CLASS |
448 USB_RECIP_INTERFACE))
455 luns = atomic_read(&fu->tpg->tpg_port_count);
457 pr_err("No LUNs configured?\n");
461 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
462 * accessed. The upper limit is 0xf
466 pr_info_once("Limiting the number of luns to 16\n");
469 ret_lun = cdev->req->buf;
471 cdev->req->length = 1;
472 return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
474 case US_BULK_RESET_REQUEST:
475 /* XXX maybe we should remove previous requests for IN + OUT */
476 bot_enqueue_cmd_cbw(fu);
482 /* Start uas.c code */
484 static void uasp_cleanup_one_stream(struct f_uas *fu, struct uas_stream *stream)
486 /* We have either all three allocated or none */
490 usb_ep_free_request(fu->ep_in, stream->req_in);
491 usb_ep_free_request(fu->ep_out, stream->req_out);
492 usb_ep_free_request(fu->ep_status, stream->req_status);
494 stream->req_in = NULL;
495 stream->req_out = NULL;
496 stream->req_status = NULL;
499 static void uasp_free_cmdreq(struct f_uas *fu)
501 usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
507 static void uasp_cleanup_old_alt(struct f_uas *fu)
511 if (!(fu->flags & USBG_ENABLED))
514 usb_ep_disable(fu->ep_in);
515 usb_ep_disable(fu->ep_out);
516 usb_ep_disable(fu->ep_status);
517 usb_ep_disable(fu->ep_cmd);
519 for (i = 0; i < UASP_SS_EP_COMP_NUM_STREAMS; i++)
520 uasp_cleanup_one_stream(fu, &fu->stream[i]);
521 uasp_free_cmdreq(fu);
524 static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req);
526 static int uasp_prepare_r_request(struct usbg_cmd *cmd)
528 struct se_cmd *se_cmd = &cmd->se_cmd;
529 struct f_uas *fu = cmd->fu;
530 struct usb_gadget *gadget = fuas_to_gadget(fu);
531 struct uas_stream *stream = cmd->stream;
533 if (!gadget->sg_supported) {
534 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
538 sg_copy_to_buffer(se_cmd->t_data_sg,
539 se_cmd->t_data_nents,
541 se_cmd->data_length);
543 stream->req_in->buf = cmd->data_buf;
545 stream->req_in->buf = NULL;
546 stream->req_in->num_sgs = se_cmd->t_data_nents;
547 stream->req_in->sg = se_cmd->t_data_sg;
550 stream->req_in->complete = uasp_status_data_cmpl;
551 stream->req_in->length = se_cmd->data_length;
552 stream->req_in->context = cmd;
554 cmd->state = UASP_SEND_STATUS;
558 static void uasp_prepare_status(struct usbg_cmd *cmd)
560 struct se_cmd *se_cmd = &cmd->se_cmd;
561 struct sense_iu *iu = &cmd->sense_iu;
562 struct uas_stream *stream = cmd->stream;
564 cmd->state = UASP_QUEUE_COMMAND;
565 iu->iu_id = IU_ID_STATUS;
566 iu->tag = cpu_to_be16(cmd->tag);
569 * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?);
571 iu->len = cpu_to_be16(se_cmd->scsi_sense_length);
572 iu->status = se_cmd->scsi_status;
573 stream->req_status->context = cmd;
574 stream->req_status->length = se_cmd->scsi_sense_length + 16;
575 stream->req_status->buf = iu;
576 stream->req_status->complete = uasp_status_data_cmpl;
579 static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req)
581 struct usbg_cmd *cmd = req->context;
582 struct uas_stream *stream = cmd->stream;
583 struct f_uas *fu = cmd->fu;
589 switch (cmd->state) {
591 ret = uasp_prepare_r_request(cmd);
594 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
596 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
599 case UASP_RECEIVE_DATA:
600 ret = usbg_prepare_w_request(cmd, stream->req_out);
603 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
605 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
608 case UASP_SEND_STATUS:
609 uasp_prepare_status(cmd);
610 ret = usb_ep_queue(fu->ep_status, stream->req_status,
613 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
616 case UASP_QUEUE_COMMAND:
617 usbg_cleanup_cmd(cmd);
618 usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
627 usbg_cleanup_cmd(cmd);
630 static int uasp_send_status_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;
636 iu->tag = cpu_to_be16(cmd->tag);
637 stream->req_status->complete = uasp_status_data_cmpl;
638 stream->req_status->context = cmd;
640 uasp_prepare_status(cmd);
641 return usb_ep_queue(fu->ep_status, stream->req_status, GFP_ATOMIC);
644 static int uasp_send_read_response(struct usbg_cmd *cmd)
646 struct f_uas *fu = cmd->fu;
647 struct uas_stream *stream = cmd->stream;
648 struct sense_iu *iu = &cmd->sense_iu;
653 iu->tag = cpu_to_be16(cmd->tag);
654 if (fu->flags & USBG_USE_STREAMS) {
656 ret = uasp_prepare_r_request(cmd);
659 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
661 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
662 kfree(cmd->data_buf);
663 cmd->data_buf = NULL;
668 iu->iu_id = IU_ID_READ_READY;
669 iu->tag = cpu_to_be16(cmd->tag);
671 stream->req_status->complete = uasp_status_data_cmpl;
672 stream->req_status->context = cmd;
674 cmd->state = UASP_SEND_DATA;
675 stream->req_status->buf = iu;
676 stream->req_status->length = sizeof(struct iu);
678 ret = usb_ep_queue(fu->ep_status, stream->req_status,
681 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
687 static int uasp_send_write_request(struct usbg_cmd *cmd)
689 struct f_uas *fu = cmd->fu;
690 struct se_cmd *se_cmd = &cmd->se_cmd;
691 struct uas_stream *stream = cmd->stream;
692 struct sense_iu *iu = &cmd->sense_iu;
695 init_completion(&cmd->write_complete);
698 iu->tag = cpu_to_be16(cmd->tag);
700 if (fu->flags & USBG_USE_STREAMS) {
702 ret = usbg_prepare_w_request(cmd, stream->req_out);
705 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
707 pr_err("%s(%d)\n", __func__, __LINE__);
711 iu->iu_id = IU_ID_WRITE_READY;
712 iu->tag = cpu_to_be16(cmd->tag);
714 stream->req_status->complete = uasp_status_data_cmpl;
715 stream->req_status->context = cmd;
717 cmd->state = UASP_RECEIVE_DATA;
718 stream->req_status->buf = iu;
719 stream->req_status->length = sizeof(struct iu);
721 ret = usb_ep_queue(fu->ep_status, stream->req_status,
724 pr_err("%s(%d)\n", __func__, __LINE__);
727 wait_for_completion(&cmd->write_complete);
728 target_execute_cmd(se_cmd);
733 static int usbg_submit_command(struct f_uas *, void *, unsigned int);
735 static void uasp_cmd_complete(struct usb_ep *ep, struct usb_request *req)
737 struct f_uas *fu = req->context;
743 ret = usbg_submit_command(fu, req->buf, req->actual);
745 * Once we tune for performance enqueue the command req here again so
746 * we can receive a second command while we processing this one. Pay
747 * attention to properly sync STAUS endpoint with DATA IN + OUT so you
752 usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
755 static int uasp_alloc_stream_res(struct f_uas *fu, struct uas_stream *stream)
757 stream->req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
761 stream->req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
762 if (!stream->req_out)
765 stream->req_status = usb_ep_alloc_request(fu->ep_status, GFP_KERNEL);
766 if (!stream->req_status)
771 usb_ep_free_request(fu->ep_status, stream->req_status);
772 stream->req_status = NULL;
774 usb_ep_free_request(fu->ep_out, stream->req_out);
775 stream->req_out = NULL;
780 static int uasp_alloc_cmd(struct f_uas *fu)
782 fu->cmd.req = usb_ep_alloc_request(fu->ep_cmd, GFP_KERNEL);
786 fu->cmd.buf = kmalloc(fu->ep_cmd->maxpacket, GFP_KERNEL);
790 fu->cmd.req->complete = uasp_cmd_complete;
791 fu->cmd.req->buf = fu->cmd.buf;
792 fu->cmd.req->length = fu->ep_cmd->maxpacket;
793 fu->cmd.req->context = fu;
797 usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
802 static void uasp_setup_stream_res(struct f_uas *fu, int max_streams)
806 for (i = 0; i < max_streams; i++) {
807 struct uas_stream *s = &fu->stream[i];
809 s->req_in->stream_id = i + 1;
810 s->req_out->stream_id = i + 1;
811 s->req_status->stream_id = i + 1;
815 static int uasp_prepare_reqs(struct f_uas *fu)
821 if (fu->flags & USBG_USE_STREAMS)
822 max_streams = UASP_SS_EP_COMP_NUM_STREAMS;
826 for (i = 0; i < max_streams; i++) {
827 ret = uasp_alloc_stream_res(fu, &fu->stream[i]);
832 ret = uasp_alloc_cmd(fu);
834 goto err_free_stream;
835 uasp_setup_stream_res(fu, max_streams);
837 ret = usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
839 goto err_free_stream;
844 uasp_free_cmdreq(fu);
849 uasp_cleanup_one_stream(fu, &fu->stream[i - 1]);
853 pr_err("UASP: endpoint setup failed\n");
857 static void uasp_set_alt(struct f_uas *fu)
859 struct usb_function *f = &fu->function;
860 struct usb_gadget *gadget = f->config->cdev->gadget;
863 fu->flags = USBG_IS_UAS;
865 if (gadget->speed == USB_SPEED_SUPER)
866 fu->flags |= USBG_USE_STREAMS;
868 config_ep_by_speed(gadget, f, fu->ep_in);
869 ret = usb_ep_enable(fu->ep_in);
873 config_ep_by_speed(gadget, f, fu->ep_out);
874 ret = usb_ep_enable(fu->ep_out);
878 config_ep_by_speed(gadget, f, fu->ep_cmd);
879 ret = usb_ep_enable(fu->ep_cmd);
882 config_ep_by_speed(gadget, f, fu->ep_status);
883 ret = usb_ep_enable(fu->ep_status);
887 ret = uasp_prepare_reqs(fu);
890 fu->flags |= USBG_ENABLED;
892 pr_info("Using the UAS protocol\n");
895 usb_ep_disable(fu->ep_status);
897 usb_ep_disable(fu->ep_cmd);
899 usb_ep_disable(fu->ep_out);
901 usb_ep_disable(fu->ep_in);
906 static int get_cmd_dir(const unsigned char *cdb)
918 case SERVICE_ACTION_IN_16:
920 case PERSISTENT_RESERVE_IN:
921 case SECURITY_PROTOCOL_IN:
922 case ACCESS_CONTROL_IN:
924 case READ_BLOCK_LIMITS:
928 case READ_FORMAT_CAPACITIES:
930 ret = DMA_FROM_DEVICE;
940 case WRITE_VERIFY_12:
941 case PERSISTENT_RESERVE_OUT:
942 case MAINTENANCE_OUT:
943 case SECURITY_PROTOCOL_OUT:
944 case ACCESS_CONTROL_OUT:
947 case ALLOW_MEDIUM_REMOVAL:
948 case TEST_UNIT_READY:
949 case SYNCHRONIZE_CACHE:
956 case WRITE_FILEMARKS:
960 #define CMD_DIR_MSG "target: Unknown data direction for SCSI Opcode 0x%02x\n"
961 pr_warn(CMD_DIR_MSG, cdb[0]);
968 static void usbg_data_write_cmpl(struct usb_ep *ep, struct usb_request *req)
970 struct usbg_cmd *cmd = req->context;
971 struct se_cmd *se_cmd = &cmd->se_cmd;
973 if (req->status < 0) {
974 pr_err("%s() state %d transfer failed\n", __func__, cmd->state);
978 if (req->num_sgs == 0) {
979 sg_copy_from_buffer(se_cmd->t_data_sg,
980 se_cmd->t_data_nents,
982 se_cmd->data_length);
985 complete(&cmd->write_complete);
989 usbg_cleanup_cmd(cmd);
992 static int usbg_prepare_w_request(struct usbg_cmd *cmd, struct usb_request *req)
994 struct se_cmd *se_cmd = &cmd->se_cmd;
995 struct f_uas *fu = cmd->fu;
996 struct usb_gadget *gadget = fuas_to_gadget(fu);
998 if (!gadget->sg_supported) {
999 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
1003 req->buf = cmd->data_buf;
1006 req->num_sgs = se_cmd->t_data_nents;
1007 req->sg = se_cmd->t_data_sg;
1010 req->complete = usbg_data_write_cmpl;
1011 req->length = se_cmd->data_length;
1016 static int usbg_send_status_response(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_status_response(cmd);
1025 return uasp_send_status_response(cmd);
1028 static int usbg_send_write_request(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_write_request(cmd);
1037 return uasp_send_write_request(cmd);
1040 static int usbg_send_read_response(struct se_cmd *se_cmd)
1042 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1044 struct f_uas *fu = cmd->fu;
1046 if (fu->flags & USBG_IS_BOT)
1047 return bot_send_read_response(cmd);
1049 return uasp_send_read_response(cmd);
1052 static void usbg_cmd_work(struct work_struct *work)
1054 struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
1055 struct se_cmd *se_cmd;
1056 struct tcm_usbg_nexus *tv_nexus;
1057 struct usbg_tpg *tpg;
1060 se_cmd = &cmd->se_cmd;
1062 tv_nexus = tpg->tpg_nexus;
1063 dir = get_cmd_dir(cmd->cmd_buf);
1065 transport_init_se_cmd(se_cmd,
1066 tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
1067 tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
1068 cmd->prio_attr, cmd->sense_iu.sense);
1072 if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
1073 cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
1074 0, cmd->prio_attr, dir, TARGET_SCF_UNKNOWN_SIZE) < 0)
1080 transport_send_check_condition_and_sense(se_cmd,
1081 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1082 usbg_cleanup_cmd(cmd);
1085 static int usbg_submit_command(struct f_uas *fu,
1086 void *cmdbuf, unsigned int len)
1088 struct command_iu *cmd_iu = cmdbuf;
1089 struct usbg_cmd *cmd;
1090 struct usbg_tpg *tpg;
1091 struct se_cmd *se_cmd;
1092 struct tcm_usbg_nexus *tv_nexus;
1095 if (cmd_iu->iu_id != IU_ID_COMMAND) {
1096 pr_err("Unsupported type %d\n", cmd_iu->iu_id);
1100 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
1106 /* XXX until I figure out why I can't free in on complete */
1107 kref_init(&cmd->ref);
1108 kref_get(&cmd->ref);
1111 cmd_len = (cmd_iu->len & ~0x3) + 16;
1112 if (cmd_len > USBG_MAX_CMD)
1115 memcpy(cmd->cmd_buf, cmd_iu->cdb, cmd_len);
1117 cmd->tag = be16_to_cpup(&cmd_iu->tag);
1118 cmd->se_cmd.tag = cmd->tag;
1119 if (fu->flags & USBG_USE_STREAMS) {
1120 if (cmd->tag > UASP_SS_EP_COMP_NUM_STREAMS)
1123 cmd->stream = &fu->stream[0];
1125 cmd->stream = &fu->stream[cmd->tag - 1];
1127 cmd->stream = &fu->stream[0];
1130 tv_nexus = tpg->tpg_nexus;
1132 pr_err("Missing nexus, ignoring command\n");
1136 switch (cmd_iu->prio_attr & 0x7) {
1138 cmd->prio_attr = TCM_HEAD_TAG;
1140 case UAS_ORDERED_TAG:
1141 cmd->prio_attr = TCM_ORDERED_TAG;
1144 cmd->prio_attr = TCM_ACA_TAG;
1147 pr_debug_once("Unsupported prio_attr: %02x.\n",
1149 case UAS_SIMPLE_TAG:
1150 cmd->prio_attr = TCM_SIMPLE_TAG;
1154 se_cmd = &cmd->se_cmd;
1155 cmd->unpacked_lun = scsilun_to_int(&cmd_iu->lun);
1157 INIT_WORK(&cmd->work, usbg_cmd_work);
1158 queue_work(tpg->workqueue, &cmd->work);
1166 static void bot_cmd_work(struct work_struct *work)
1168 struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
1169 struct se_cmd *se_cmd;
1170 struct tcm_usbg_nexus *tv_nexus;
1171 struct usbg_tpg *tpg;
1174 se_cmd = &cmd->se_cmd;
1176 tv_nexus = tpg->tpg_nexus;
1177 dir = get_cmd_dir(cmd->cmd_buf);
1179 transport_init_se_cmd(se_cmd,
1180 tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
1181 tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
1182 cmd->prio_attr, cmd->sense_iu.sense);
1186 if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
1187 cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
1188 cmd->data_len, cmd->prio_attr, dir, 0) < 0)
1194 transport_send_check_condition_and_sense(se_cmd,
1195 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1196 usbg_cleanup_cmd(cmd);
1199 static int bot_submit_command(struct f_uas *fu,
1200 void *cmdbuf, unsigned int len)
1202 struct bulk_cb_wrap *cbw = cmdbuf;
1203 struct usbg_cmd *cmd;
1204 struct usbg_tpg *tpg;
1205 struct se_cmd *se_cmd;
1206 struct tcm_usbg_nexus *tv_nexus;
1209 if (cbw->Signature != cpu_to_le32(US_BULK_CB_SIGN)) {
1210 pr_err("Wrong signature on CBW\n");
1214 pr_err("Wrong length for CBW\n");
1218 cmd_len = cbw->Length;
1219 if (cmd_len < 1 || cmd_len > 16)
1222 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
1228 /* XXX until I figure out why I can't free in on complete */
1229 kref_init(&cmd->ref);
1230 kref_get(&cmd->ref);
1234 memcpy(cmd->cmd_buf, cbw->CDB, cmd_len);
1236 cmd->bot_tag = cbw->Tag;
1238 tv_nexus = tpg->tpg_nexus;
1240 pr_err("Missing nexus, ignoring command\n");
1244 cmd->prio_attr = TCM_SIMPLE_TAG;
1245 se_cmd = &cmd->se_cmd;
1246 cmd->unpacked_lun = cbw->Lun;
1247 cmd->is_read = cbw->Flags & US_BULK_FLAG_IN ? 1 : 0;
1248 cmd->data_len = le32_to_cpu(cbw->DataTransferLength);
1249 cmd->se_cmd.tag = le32_to_cpu(cmd->bot_tag);
1251 INIT_WORK(&cmd->work, bot_cmd_work);
1252 queue_work(tpg->workqueue, &cmd->work);
1260 /* Start fabric.c code */
1262 static int usbg_check_true(struct se_portal_group *se_tpg)
1267 static int usbg_check_false(struct se_portal_group *se_tpg)
1272 static char *usbg_get_fabric_name(void)
1274 return "usb_gadget";
1277 static char *usbg_get_fabric_wwn(struct se_portal_group *se_tpg)
1279 struct usbg_tpg *tpg = container_of(se_tpg,
1280 struct usbg_tpg, se_tpg);
1281 struct usbg_tport *tport = tpg->tport;
1283 return &tport->tport_name[0];
1286 static u16 usbg_get_tag(struct se_portal_group *se_tpg)
1288 struct usbg_tpg *tpg = container_of(se_tpg,
1289 struct usbg_tpg, se_tpg);
1290 return tpg->tport_tpgt;
1293 static u32 usbg_tpg_get_inst_index(struct se_portal_group *se_tpg)
1298 static void usbg_cmd_release(struct kref *ref)
1300 struct usbg_cmd *cmd = container_of(ref, struct usbg_cmd,
1303 transport_generic_free_cmd(&cmd->se_cmd, 0);
1306 static void usbg_release_cmd(struct se_cmd *se_cmd)
1308 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1310 kfree(cmd->data_buf);
1314 static int usbg_shutdown_session(struct se_session *se_sess)
1319 static void usbg_close_session(struct se_session *se_sess)
1323 static u32 usbg_sess_get_index(struct se_session *se_sess)
1329 * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be
1331 static int usbg_write_pending_status(struct se_cmd *se_cmd)
1336 static void usbg_set_default_node_attrs(struct se_node_acl *nacl)
1340 static int usbg_get_cmd_state(struct se_cmd *se_cmd)
1345 static void usbg_queue_tm_rsp(struct se_cmd *se_cmd)
1349 static void usbg_aborted_task(struct se_cmd *se_cmd)
1353 static const char *usbg_check_wwn(const char *name)
1358 n = strstr(name, "naa.");
1363 if (len == 0 || len > USBG_NAMELEN - 1)
1368 static int usbg_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
1370 if (!usbg_check_wwn(name))
1375 static struct se_portal_group *usbg_make_tpg(
1377 struct config_group *group,
1380 struct usbg_tport *tport = container_of(wwn, struct usbg_tport,
1382 struct usbg_tpg *tpg;
1385 struct f_tcm_opts *opts;
1388 if (strstr(name, "tpgt_") != name)
1389 return ERR_PTR(-EINVAL);
1390 if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX)
1391 return ERR_PTR(-EINVAL);
1393 mutex_lock(&tpg_instances_lock);
1394 for (i = 0; i < TPG_INSTANCES; ++i)
1395 if (tpg_instances[i].func_inst && !tpg_instances[i].tpg)
1397 if (i == TPG_INSTANCES)
1400 opts = container_of(tpg_instances[i].func_inst, struct f_tcm_opts,
1402 mutex_lock(&opts->dep_lock);
1406 if (opts->has_dep) {
1407 if (!try_module_get(opts->dependent))
1410 ret = configfs_depend_item_unlocked(
1412 &opts->func_inst.group.cg_item);
1417 tpg = kzalloc(sizeof(struct usbg_tpg), GFP_KERNEL);
1421 mutex_init(&tpg->tpg_mutex);
1422 atomic_set(&tpg->tpg_port_count, 0);
1423 tpg->workqueue = alloc_workqueue("tcm_usb_gadget", 0, 1);
1424 if (!tpg->workqueue)
1428 tpg->tport_tpgt = tpgt;
1431 * SPC doesn't assign a protocol identifier for USB-SCSI, so we
1432 * pretend to be SAS..
1434 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_SAS);
1436 goto free_workqueue;
1438 tpg_instances[i].tpg = tpg;
1439 tpg->fi = tpg_instances[i].func_inst;
1440 mutex_unlock(&opts->dep_lock);
1441 mutex_unlock(&tpg_instances_lock);
1442 return &tpg->se_tpg;
1445 destroy_workqueue(tpg->workqueue);
1450 module_put(opts->dependent);
1452 configfs_undepend_item_unlocked(&opts->func_inst.group.cg_item);
1454 mutex_unlock(&opts->dep_lock);
1456 mutex_unlock(&tpg_instances_lock);
1458 return ERR_PTR(ret);
1461 static int tcm_usbg_drop_nexus(struct usbg_tpg *);
1463 static void usbg_drop_tpg(struct se_portal_group *se_tpg)
1465 struct usbg_tpg *tpg = container_of(se_tpg,
1466 struct usbg_tpg, se_tpg);
1468 struct f_tcm_opts *opts;
1470 tcm_usbg_drop_nexus(tpg);
1471 core_tpg_deregister(se_tpg);
1472 destroy_workqueue(tpg->workqueue);
1474 mutex_lock(&tpg_instances_lock);
1475 for (i = 0; i < TPG_INSTANCES; ++i)
1476 if (tpg_instances[i].tpg == tpg)
1478 if (i < TPG_INSTANCES)
1479 tpg_instances[i].tpg = NULL;
1480 opts = container_of(tpg_instances[i].func_inst,
1481 struct f_tcm_opts, func_inst);
1482 mutex_lock(&opts->dep_lock);
1484 module_put(opts->dependent);
1486 configfs_undepend_item_unlocked(&opts->func_inst.group.cg_item);
1487 mutex_unlock(&opts->dep_lock);
1488 mutex_unlock(&tpg_instances_lock);
1493 static struct se_wwn *usbg_make_tport(
1494 struct target_fabric_configfs *tf,
1495 struct config_group *group,
1498 struct usbg_tport *tport;
1499 const char *wnn_name;
1502 wnn_name = usbg_check_wwn(name);
1504 return ERR_PTR(-EINVAL);
1506 tport = kzalloc(sizeof(struct usbg_tport), GFP_KERNEL);
1508 return ERR_PTR(-ENOMEM);
1510 tport->tport_wwpn = wwpn;
1511 snprintf(tport->tport_name, sizeof(tport->tport_name), "%s", wnn_name);
1512 return &tport->tport_wwn;
1515 static void usbg_drop_tport(struct se_wwn *wwn)
1517 struct usbg_tport *tport = container_of(wwn,
1518 struct usbg_tport, tport_wwn);
1523 * If somebody feels like dropping the version property, go ahead.
1525 static ssize_t usbg_wwn_version_show(struct config_item *item, char *page)
1527 return sprintf(page, "usb-gadget fabric module\n");
1530 CONFIGFS_ATTR_RO(usbg_wwn_, version);
1532 static struct configfs_attribute *usbg_wwn_attrs[] = {
1533 &usbg_wwn_attr_version,
1537 static ssize_t tcm_usbg_tpg_enable_show(struct config_item *item, char *page)
1539 struct se_portal_group *se_tpg = to_tpg(item);
1540 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1542 return snprintf(page, PAGE_SIZE, "%u\n", tpg->gadget_connect);
1545 static int usbg_attach(struct usbg_tpg *);
1546 static void usbg_detach(struct usbg_tpg *);
1548 static ssize_t tcm_usbg_tpg_enable_store(struct config_item *item,
1549 const char *page, size_t count)
1551 struct se_portal_group *se_tpg = to_tpg(item);
1552 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1556 ret = strtobool(page, &op);
1560 if ((op && tpg->gadget_connect) || (!op && !tpg->gadget_connect))
1564 ret = usbg_attach(tpg);
1570 tpg->gadget_connect = op;
1575 static ssize_t tcm_usbg_tpg_nexus_show(struct config_item *item, char *page)
1577 struct se_portal_group *se_tpg = to_tpg(item);
1578 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1579 struct tcm_usbg_nexus *tv_nexus;
1582 mutex_lock(&tpg->tpg_mutex);
1583 tv_nexus = tpg->tpg_nexus;
1588 ret = snprintf(page, PAGE_SIZE, "%s\n",
1589 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1591 mutex_unlock(&tpg->tpg_mutex);
1595 static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name)
1597 struct se_portal_group *se_tpg;
1598 struct tcm_usbg_nexus *tv_nexus;
1601 mutex_lock(&tpg->tpg_mutex);
1602 if (tpg->tpg_nexus) {
1604 pr_debug("tpg->tpg_nexus already exists\n");
1607 se_tpg = &tpg->se_tpg;
1610 tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
1613 tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL);
1614 if (IS_ERR(tv_nexus->tvn_se_sess))
1618 * Since we are running in 'demo mode' this call with generate a
1619 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1620 * the SCSI Initiator port name of the passed configfs group 'name'.
1622 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1624 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1625 #define MAKE_NEXUS_MSG "core_tpg_check_initiator_node_acl() failed for %s\n"
1626 pr_debug(MAKE_NEXUS_MSG, name);
1627 #undef MAKE_NEXUS_MSG
1631 * Now register the TCM vHost virtual I_T Nexus as active.
1633 transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1634 tv_nexus->tvn_se_sess, tv_nexus);
1635 tpg->tpg_nexus = tv_nexus;
1636 mutex_unlock(&tpg->tpg_mutex);
1640 transport_free_session(tv_nexus->tvn_se_sess);
1644 mutex_unlock(&tpg->tpg_mutex);
1648 static int tcm_usbg_drop_nexus(struct usbg_tpg *tpg)
1650 struct se_session *se_sess;
1651 struct tcm_usbg_nexus *tv_nexus;
1654 mutex_lock(&tpg->tpg_mutex);
1655 tv_nexus = tpg->tpg_nexus;
1659 se_sess = tv_nexus->tvn_se_sess;
1663 if (atomic_read(&tpg->tpg_port_count)) {
1665 #define MSG "Unable to remove Host I_T Nexus with active TPG port count: %d\n"
1666 pr_err(MSG, atomic_read(&tpg->tpg_port_count));
1671 pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
1672 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1674 * Release the SCSI I_T Nexus to the emulated vHost Target Port
1676 transport_deregister_session(tv_nexus->tvn_se_sess);
1677 tpg->tpg_nexus = NULL;
1682 mutex_unlock(&tpg->tpg_mutex);
1686 static ssize_t tcm_usbg_tpg_nexus_store(struct config_item *item,
1687 const char *page, size_t count)
1689 struct se_portal_group *se_tpg = to_tpg(item);
1690 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1691 unsigned char i_port[USBG_NAMELEN], *ptr;
1694 if (!strncmp(page, "NULL", 4)) {
1695 ret = tcm_usbg_drop_nexus(tpg);
1696 return (!ret) ? count : ret;
1698 if (strlen(page) >= USBG_NAMELEN) {
1700 #define NEXUS_STORE_MSG "Emulated NAA Sas Address: %s, exceeds max: %d\n"
1701 pr_err(NEXUS_STORE_MSG, page, USBG_NAMELEN);
1702 #undef NEXUS_STORE_MSG
1705 snprintf(i_port, USBG_NAMELEN, "%s", page);
1707 ptr = strstr(i_port, "naa.");
1709 pr_err("Missing 'naa.' prefix\n");
1713 if (i_port[strlen(i_port) - 1] == '\n')
1714 i_port[strlen(i_port) - 1] = '\0';
1716 ret = tcm_usbg_make_nexus(tpg, &i_port[0]);
1722 CONFIGFS_ATTR(tcm_usbg_tpg_, enable);
1723 CONFIGFS_ATTR(tcm_usbg_tpg_, nexus);
1725 static struct configfs_attribute *usbg_base_attrs[] = {
1726 &tcm_usbg_tpg_attr_enable,
1727 &tcm_usbg_tpg_attr_nexus,
1731 static int usbg_port_link(struct se_portal_group *se_tpg, struct se_lun *lun)
1733 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1735 atomic_inc(&tpg->tpg_port_count);
1736 smp_mb__after_atomic();
1740 static void usbg_port_unlink(struct se_portal_group *se_tpg,
1741 struct se_lun *se_lun)
1743 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1745 atomic_dec(&tpg->tpg_port_count);
1746 smp_mb__after_atomic();
1749 static int usbg_check_stop_free(struct se_cmd *se_cmd)
1751 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1754 kref_put(&cmd->ref, usbg_cmd_release);
1758 static const struct target_core_fabric_ops usbg_ops = {
1759 .module = THIS_MODULE,
1760 .name = "usb_gadget",
1761 .get_fabric_name = usbg_get_fabric_name,
1762 .tpg_get_wwn = usbg_get_fabric_wwn,
1763 .tpg_get_tag = usbg_get_tag,
1764 .tpg_check_demo_mode = usbg_check_true,
1765 .tpg_check_demo_mode_cache = usbg_check_false,
1766 .tpg_check_demo_mode_write_protect = usbg_check_false,
1767 .tpg_check_prod_mode_write_protect = usbg_check_false,
1768 .tpg_get_inst_index = usbg_tpg_get_inst_index,
1769 .release_cmd = usbg_release_cmd,
1770 .shutdown_session = usbg_shutdown_session,
1771 .close_session = usbg_close_session,
1772 .sess_get_index = usbg_sess_get_index,
1773 .sess_get_initiator_sid = NULL,
1774 .write_pending = usbg_send_write_request,
1775 .write_pending_status = usbg_write_pending_status,
1776 .set_default_node_attributes = usbg_set_default_node_attrs,
1777 .get_cmd_state = usbg_get_cmd_state,
1778 .queue_data_in = usbg_send_read_response,
1779 .queue_status = usbg_send_status_response,
1780 .queue_tm_rsp = usbg_queue_tm_rsp,
1781 .aborted_task = usbg_aborted_task,
1782 .check_stop_free = usbg_check_stop_free,
1784 .fabric_make_wwn = usbg_make_tport,
1785 .fabric_drop_wwn = usbg_drop_tport,
1786 .fabric_make_tpg = usbg_make_tpg,
1787 .fabric_drop_tpg = usbg_drop_tpg,
1788 .fabric_post_link = usbg_port_link,
1789 .fabric_pre_unlink = usbg_port_unlink,
1790 .fabric_init_nodeacl = usbg_init_nodeacl,
1792 .tfc_wwn_attrs = usbg_wwn_attrs,
1793 .tfc_tpg_base_attrs = usbg_base_attrs,
1796 /* Start gadget.c code */
1798 static struct usb_interface_descriptor bot_intf_desc = {
1799 .bLength = sizeof(bot_intf_desc),
1800 .bDescriptorType = USB_DT_INTERFACE,
1802 .bAlternateSetting = USB_G_ALT_INT_BBB,
1803 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1804 .bInterfaceSubClass = USB_SC_SCSI,
1805 .bInterfaceProtocol = USB_PR_BULK,
1808 static struct usb_interface_descriptor uasp_intf_desc = {
1809 .bLength = sizeof(uasp_intf_desc),
1810 .bDescriptorType = USB_DT_INTERFACE,
1812 .bAlternateSetting = USB_G_ALT_INT_UAS,
1813 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1814 .bInterfaceSubClass = USB_SC_SCSI,
1815 .bInterfaceProtocol = USB_PR_UAS,
1818 static struct usb_endpoint_descriptor uasp_bi_desc = {
1819 .bLength = USB_DT_ENDPOINT_SIZE,
1820 .bDescriptorType = USB_DT_ENDPOINT,
1821 .bEndpointAddress = USB_DIR_IN,
1822 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1823 .wMaxPacketSize = cpu_to_le16(512),
1826 static struct usb_endpoint_descriptor uasp_fs_bi_desc = {
1827 .bLength = USB_DT_ENDPOINT_SIZE,
1828 .bDescriptorType = USB_DT_ENDPOINT,
1829 .bEndpointAddress = USB_DIR_IN,
1830 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1833 static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc = {
1834 .bLength = sizeof(uasp_bi_pipe_desc),
1835 .bDescriptorType = USB_DT_PIPE_USAGE,
1836 .bPipeID = DATA_IN_PIPE_ID,
1839 static struct usb_endpoint_descriptor uasp_ss_bi_desc = {
1840 .bLength = USB_DT_ENDPOINT_SIZE,
1841 .bDescriptorType = USB_DT_ENDPOINT,
1842 .bEndpointAddress = USB_DIR_IN,
1843 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1844 .wMaxPacketSize = cpu_to_le16(1024),
1847 static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc = {
1848 .bLength = sizeof(uasp_bi_ep_comp_desc),
1849 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1851 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1852 .wBytesPerInterval = 0,
1855 static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc = {
1856 .bLength = sizeof(bot_bi_ep_comp_desc),
1857 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1861 static struct usb_endpoint_descriptor uasp_bo_desc = {
1862 .bLength = USB_DT_ENDPOINT_SIZE,
1863 .bDescriptorType = USB_DT_ENDPOINT,
1864 .bEndpointAddress = USB_DIR_OUT,
1865 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1866 .wMaxPacketSize = cpu_to_le16(512),
1869 static struct usb_endpoint_descriptor uasp_fs_bo_desc = {
1870 .bLength = USB_DT_ENDPOINT_SIZE,
1871 .bDescriptorType = USB_DT_ENDPOINT,
1872 .bEndpointAddress = USB_DIR_OUT,
1873 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1876 static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc = {
1877 .bLength = sizeof(uasp_bo_pipe_desc),
1878 .bDescriptorType = USB_DT_PIPE_USAGE,
1879 .bPipeID = DATA_OUT_PIPE_ID,
1882 static struct usb_endpoint_descriptor uasp_ss_bo_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(0x400),
1890 static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc = {
1891 .bLength = sizeof(uasp_bo_ep_comp_desc),
1892 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1893 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1896 static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc = {
1897 .bLength = sizeof(bot_bo_ep_comp_desc),
1898 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1901 static struct usb_endpoint_descriptor uasp_status_desc = {
1902 .bLength = USB_DT_ENDPOINT_SIZE,
1903 .bDescriptorType = USB_DT_ENDPOINT,
1904 .bEndpointAddress = USB_DIR_IN,
1905 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1906 .wMaxPacketSize = cpu_to_le16(512),
1909 static struct usb_endpoint_descriptor uasp_fs_status_desc = {
1910 .bLength = USB_DT_ENDPOINT_SIZE,
1911 .bDescriptorType = USB_DT_ENDPOINT,
1912 .bEndpointAddress = USB_DIR_IN,
1913 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1916 static struct usb_pipe_usage_descriptor uasp_status_pipe_desc = {
1917 .bLength = sizeof(uasp_status_pipe_desc),
1918 .bDescriptorType = USB_DT_PIPE_USAGE,
1919 .bPipeID = STATUS_PIPE_ID,
1922 static struct usb_endpoint_descriptor uasp_ss_status_desc = {
1923 .bLength = USB_DT_ENDPOINT_SIZE,
1924 .bDescriptorType = USB_DT_ENDPOINT,
1925 .bEndpointAddress = USB_DIR_IN,
1926 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1927 .wMaxPacketSize = cpu_to_le16(1024),
1930 static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc = {
1931 .bLength = sizeof(uasp_status_in_ep_comp_desc),
1932 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1933 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1936 static struct usb_endpoint_descriptor uasp_cmd_desc = {
1937 .bLength = USB_DT_ENDPOINT_SIZE,
1938 .bDescriptorType = USB_DT_ENDPOINT,
1939 .bEndpointAddress = USB_DIR_OUT,
1940 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1941 .wMaxPacketSize = cpu_to_le16(512),
1944 static struct usb_endpoint_descriptor uasp_fs_cmd_desc = {
1945 .bLength = USB_DT_ENDPOINT_SIZE,
1946 .bDescriptorType = USB_DT_ENDPOINT,
1947 .bEndpointAddress = USB_DIR_OUT,
1948 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1951 static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc = {
1952 .bLength = sizeof(uasp_cmd_pipe_desc),
1953 .bDescriptorType = USB_DT_PIPE_USAGE,
1954 .bPipeID = CMD_PIPE_ID,
1957 static struct usb_endpoint_descriptor uasp_ss_cmd_desc = {
1958 .bLength = USB_DT_ENDPOINT_SIZE,
1959 .bDescriptorType = USB_DT_ENDPOINT,
1960 .bEndpointAddress = USB_DIR_OUT,
1961 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1962 .wMaxPacketSize = cpu_to_le16(1024),
1965 static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc = {
1966 .bLength = sizeof(uasp_cmd_comp_desc),
1967 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1970 static struct usb_descriptor_header *uasp_fs_function_desc[] = {
1971 (struct usb_descriptor_header *) &bot_intf_desc,
1972 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
1973 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
1975 (struct usb_descriptor_header *) &uasp_intf_desc,
1976 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
1977 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
1978 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
1979 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
1980 (struct usb_descriptor_header *) &uasp_fs_status_desc,
1981 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
1982 (struct usb_descriptor_header *) &uasp_fs_cmd_desc,
1983 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
1987 static struct usb_descriptor_header *uasp_hs_function_desc[] = {
1988 (struct usb_descriptor_header *) &bot_intf_desc,
1989 (struct usb_descriptor_header *) &uasp_bi_desc,
1990 (struct usb_descriptor_header *) &uasp_bo_desc,
1992 (struct usb_descriptor_header *) &uasp_intf_desc,
1993 (struct usb_descriptor_header *) &uasp_bi_desc,
1994 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
1995 (struct usb_descriptor_header *) &uasp_bo_desc,
1996 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
1997 (struct usb_descriptor_header *) &uasp_status_desc,
1998 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
1999 (struct usb_descriptor_header *) &uasp_cmd_desc,
2000 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
2004 static struct usb_descriptor_header *uasp_ss_function_desc[] = {
2005 (struct usb_descriptor_header *) &bot_intf_desc,
2006 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
2007 (struct usb_descriptor_header *) &bot_bi_ep_comp_desc,
2008 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
2009 (struct usb_descriptor_header *) &bot_bo_ep_comp_desc,
2011 (struct usb_descriptor_header *) &uasp_intf_desc,
2012 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
2013 (struct usb_descriptor_header *) &uasp_bi_ep_comp_desc,
2014 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
2015 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
2016 (struct usb_descriptor_header *) &uasp_bo_ep_comp_desc,
2017 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
2018 (struct usb_descriptor_header *) &uasp_ss_status_desc,
2019 (struct usb_descriptor_header *) &uasp_status_in_ep_comp_desc,
2020 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
2021 (struct usb_descriptor_header *) &uasp_ss_cmd_desc,
2022 (struct usb_descriptor_header *) &uasp_cmd_comp_desc,
2023 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
2027 static struct usb_string tcm_us_strings[] = {
2028 [USB_G_STR_INT_UAS].s = "USB Attached SCSI",
2029 [USB_G_STR_INT_BBB].s = "Bulk Only Transport",
2033 static struct usb_gadget_strings tcm_stringtab = {
2035 .strings = tcm_us_strings,
2038 static struct usb_gadget_strings *tcm_strings[] = {
2043 static int tcm_bind(struct usb_configuration *c, struct usb_function *f)
2045 struct f_uas *fu = to_f_uas(f);
2046 struct usb_string *us;
2047 struct usb_gadget *gadget = c->cdev->gadget;
2049 struct f_tcm_opts *opts;
2053 opts = container_of(f->fi, struct f_tcm_opts, func_inst);
2055 mutex_lock(&opts->dep_lock);
2056 if (!opts->can_attach) {
2057 mutex_unlock(&opts->dep_lock);
2060 mutex_unlock(&opts->dep_lock);
2061 us = usb_gstrings_attach(c->cdev, tcm_strings,
2062 ARRAY_SIZE(tcm_us_strings));
2065 bot_intf_desc.iInterface = us[USB_G_STR_INT_BBB].id;
2066 uasp_intf_desc.iInterface = us[USB_G_STR_INT_UAS].id;
2068 iface = usb_interface_id(c, f);
2072 bot_intf_desc.bInterfaceNumber = iface;
2073 uasp_intf_desc.bInterfaceNumber = iface;
2075 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bi_desc,
2076 &uasp_bi_ep_comp_desc);
2082 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bo_desc,
2083 &uasp_bo_ep_comp_desc);
2088 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_status_desc,
2089 &uasp_status_in_ep_comp_desc);
2094 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_cmd_desc,
2095 &uasp_cmd_comp_desc);
2100 /* Assume endpoint addresses are the same for both speeds */
2101 uasp_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2102 uasp_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2103 uasp_status_desc.bEndpointAddress =
2104 uasp_ss_status_desc.bEndpointAddress;
2105 uasp_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2107 uasp_fs_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2108 uasp_fs_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2109 uasp_fs_status_desc.bEndpointAddress =
2110 uasp_ss_status_desc.bEndpointAddress;
2111 uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2113 ret = usb_assign_descriptors(f, uasp_fs_function_desc,
2114 uasp_hs_function_desc, uasp_ss_function_desc);
2120 pr_err("Can't claim all required eps\n");
2125 struct guas_setup_wq {
2126 struct work_struct work;
2131 static void tcm_delayed_set_alt(struct work_struct *wq)
2133 struct guas_setup_wq *work = container_of(wq, struct guas_setup_wq,
2135 struct f_uas *fu = work->fu;
2136 int alt = work->alt;
2140 if (fu->flags & USBG_IS_BOT)
2141 bot_cleanup_old_alt(fu);
2142 if (fu->flags & USBG_IS_UAS)
2143 uasp_cleanup_old_alt(fu);
2145 if (alt == USB_G_ALT_INT_BBB)
2147 else if (alt == USB_G_ALT_INT_UAS)
2149 usb_composite_setup_continue(fu->function.config->cdev);
2152 static int tcm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2154 struct f_uas *fu = to_f_uas(f);
2156 if ((alt == USB_G_ALT_INT_BBB) || (alt == USB_G_ALT_INT_UAS)) {
2157 struct guas_setup_wq *work;
2159 work = kmalloc(sizeof(*work), GFP_ATOMIC);
2162 INIT_WORK(&work->work, tcm_delayed_set_alt);
2165 schedule_work(&work->work);
2166 return USB_GADGET_DELAYED_STATUS;
2171 static void tcm_disable(struct usb_function *f)
2173 struct f_uas *fu = to_f_uas(f);
2175 if (fu->flags & USBG_IS_UAS)
2176 uasp_cleanup_old_alt(fu);
2177 else if (fu->flags & USBG_IS_BOT)
2178 bot_cleanup_old_alt(fu);
2182 static int tcm_setup(struct usb_function *f,
2183 const struct usb_ctrlrequest *ctrl)
2185 struct f_uas *fu = to_f_uas(f);
2187 if (!(fu->flags & USBG_IS_BOT))
2190 return usbg_bot_setup(f, ctrl);
2193 static inline struct f_tcm_opts *to_f_tcm_opts(struct config_item *item)
2195 return container_of(to_config_group(item), struct f_tcm_opts,
2199 static void tcm_attr_release(struct config_item *item)
2201 struct f_tcm_opts *opts = to_f_tcm_opts(item);
2203 usb_put_function_instance(&opts->func_inst);
2206 static struct configfs_item_operations tcm_item_ops = {
2207 .release = tcm_attr_release,
2210 static struct config_item_type tcm_func_type = {
2211 .ct_item_ops = &tcm_item_ops,
2212 .ct_owner = THIS_MODULE,
2215 static void tcm_free_inst(struct usb_function_instance *f)
2217 struct f_tcm_opts *opts;
2220 opts = container_of(f, struct f_tcm_opts, func_inst);
2222 mutex_lock(&tpg_instances_lock);
2223 for (i = 0; i < TPG_INSTANCES; ++i)
2224 if (tpg_instances[i].func_inst == f)
2226 if (i < TPG_INSTANCES)
2227 tpg_instances[i].func_inst = NULL;
2228 mutex_unlock(&tpg_instances_lock);
2233 static int tcm_register_callback(struct usb_function_instance *f)
2235 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2237 mutex_lock(&opts->dep_lock);
2238 opts->can_attach = true;
2239 mutex_unlock(&opts->dep_lock);
2244 static void tcm_unregister_callback(struct usb_function_instance *f)
2246 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2248 mutex_lock(&opts->dep_lock);
2249 unregister_gadget_item(opts->
2250 func_inst.group.cg_item.ci_parent->ci_parent);
2251 opts->can_attach = false;
2252 mutex_unlock(&opts->dep_lock);
2255 static int usbg_attach(struct usbg_tpg *tpg)
2257 struct usb_function_instance *f = tpg->fi;
2258 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2260 if (opts->tcm_register_callback)
2261 return opts->tcm_register_callback(f);
2266 static void usbg_detach(struct usbg_tpg *tpg)
2268 struct usb_function_instance *f = tpg->fi;
2269 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2271 if (opts->tcm_unregister_callback)
2272 opts->tcm_unregister_callback(f);
2275 static int tcm_set_name(struct usb_function_instance *f, const char *name)
2277 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2279 pr_debug("tcm: Activating %s\n", name);
2281 mutex_lock(&opts->dep_lock);
2283 mutex_unlock(&opts->dep_lock);
2288 static struct usb_function_instance *tcm_alloc_inst(void)
2290 struct f_tcm_opts *opts;
2294 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2296 return ERR_PTR(-ENOMEM);
2298 mutex_lock(&tpg_instances_lock);
2299 for (i = 0; i < TPG_INSTANCES; ++i)
2300 if (!tpg_instances[i].func_inst)
2303 if (i == TPG_INSTANCES) {
2304 mutex_unlock(&tpg_instances_lock);
2306 return ERR_PTR(-EBUSY);
2308 tpg_instances[i].func_inst = &opts->func_inst;
2309 mutex_unlock(&tpg_instances_lock);
2311 mutex_init(&opts->dep_lock);
2312 opts->func_inst.set_inst_name = tcm_set_name;
2313 opts->func_inst.free_func_inst = tcm_free_inst;
2314 opts->tcm_register_callback = tcm_register_callback;
2315 opts->tcm_unregister_callback = tcm_unregister_callback;
2317 config_group_init_type_name(&opts->func_inst.group, "",
2320 return &opts->func_inst;
2323 static void tcm_free(struct usb_function *f)
2325 struct f_uas *tcm = to_f_uas(f);
2330 static void tcm_unbind(struct usb_configuration *c, struct usb_function *f)
2332 usb_free_all_descriptors(f);
2335 static struct usb_function *tcm_alloc(struct usb_function_instance *fi)
2338 struct f_tcm_opts *opts;
2341 mutex_lock(&tpg_instances_lock);
2342 for (i = 0; i < TPG_INSTANCES; ++i)
2343 if (tpg_instances[i].func_inst == fi)
2345 if (i == TPG_INSTANCES) {
2346 mutex_unlock(&tpg_instances_lock);
2347 return ERR_PTR(-ENODEV);
2350 opts = container_of(fi, struct f_tcm_opts, func_inst);
2352 fu = kzalloc(sizeof(*fu), GFP_KERNEL);
2354 mutex_unlock(&tpg_instances_lock);
2355 return ERR_PTR(-ENOMEM);
2358 fu->function.name = "Target Function";
2359 fu->function.bind = tcm_bind;
2360 fu->function.unbind = tcm_unbind;
2361 fu->function.set_alt = tcm_set_alt;
2362 fu->function.setup = tcm_setup;
2363 fu->function.disable = tcm_disable;
2364 fu->function.free_func = tcm_free;
2365 fu->tpg = tpg_instances[i].tpg;
2366 mutex_unlock(&tpg_instances_lock);
2368 return &fu->function;
2371 DECLARE_USB_FUNCTION(tcm, tcm_alloc_inst, tcm_alloc);
2373 static int tcm_init(void)
2377 ret = usb_function_register(&tcmusb_func);
2381 ret = target_register_template(&usbg_ops);
2383 usb_function_unregister(&tcmusb_func);
2387 module_init(tcm_init);
2389 static void tcm_exit(void)
2391 target_unregister_template(&usbg_ops);
2392 usb_function_unregister(&tcmusb_func);
2394 module_exit(tcm_exit);
2396 MODULE_LICENSE("GPL");
2397 MODULE_AUTHOR("Sebastian Andrzej Siewior");