treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / usb / gadget / function / f_tcm.c
blob36504931b2d1be401ea9101aa0c04b53d86a5818
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>
8 */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/string.h>
13 #include <linux/configfs.h>
14 #include <linux/ctype.h>
15 #include <linux/usb/ch9.h>
16 #include <linux/usb/composite.h>
17 #include <linux/usb/gadget.h>
18 #include <linux/usb/storage.h>
19 #include <scsi/scsi_tcq.h>
20 #include <target/target_core_base.h>
21 #include <target/target_core_fabric.h>
22 #include <asm/unaligned.h>
24 #include "tcm.h"
25 #include "u_tcm.h"
26 #include "configfs.h"
28 #define TPG_INSTANCES 1
30 struct tpg_instance {
31 struct usb_function_instance *func_inst;
32 struct usbg_tpg *tpg;
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)
48 int ret;
50 if (fu->flags & USBG_BOT_CMD_PEND)
51 return 0;
53 ret = usb_ep_queue(fu->ep_out, fu->cmd.req, GFP_ATOMIC);
54 if (!ret)
55 fu->flags |= USBG_BOT_CMD_PEND;
56 return ret;
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__);
67 return;
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;
77 int ret;
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);
85 if (ret)
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;
94 if (req->status < 0)
95 pr_err("ERR %s(%d)\n", __func__, __LINE__);
97 if (cmd->data_len) {
98 if (cmd->data_len > ep->maxpacket) {
99 req->length = ep->maxpacket;
100 cmd->data_len -= ep->maxpacket;
101 } else {
102 req->length = cmd->data_len;
103 cmd->data_len = 0;
106 usb_ep_queue(ep, req, GFP_ATOMIC);
107 return;
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;
117 struct usb_ep *ep;
119 csw->Residue = cpu_to_le32(cmd->data_len);
121 if (cmd->data_len) {
122 if (cmd->is_read) {
123 ep = fu->ep_in;
124 req = fu->bot_req_in;
125 } else {
126 ep = fu->ep_out;
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;
133 } else {
134 req->length = cmd->data_len;
135 cmd->data_len = 0;
137 req->complete = bot_err_compl;
138 req->context = cmd;
139 req->buf = fu->cmd.buf;
140 usb_ep_queue(ep, req, GFP_KERNEL);
141 } else {
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;
150 int ret;
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);
160 return 0;
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);
169 if (ret)
170 pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
171 } else {
172 cmd->csw_code = US_BULK_STAT_FAIL;
173 bot_send_bad_status(cmd);
175 return 0;
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;
186 if (!cmd->is_read)
187 moved_data = true;
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;
196 if (req->status < 0)
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);
207 int ret;
209 if (!cmd->data_len) {
210 cmd->csw_code = US_BULK_STAT_PHASE;
211 bot_send_bad_status(cmd);
212 return 0;
215 if (!gadget->sg_supported) {
216 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
217 if (!cmd->data_buf)
218 return -ENOMEM;
220 sg_copy_to_buffer(se_cmd->t_data_sg,
221 se_cmd->t_data_nents,
222 cmd->data_buf,
223 se_cmd->data_length);
225 fu->bot_req_in->buf = cmd->data_buf;
226 } else {
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);
236 if (ret)
237 pr_err("%s(%d)\n", __func__, __LINE__);
238 return 0;
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);
249 int ret;
251 init_completion(&cmd->write_complete);
252 cmd->fu = fu;
254 if (!cmd->data_len) {
255 cmd->csw_code = US_BULK_STAT_PHASE;
256 return -EINVAL;
259 if (!gadget->sg_supported) {
260 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_KERNEL);
261 if (!cmd->data_buf)
262 return -ENOMEM;
264 fu->bot_req_out->buf = cmd->data_buf;
265 } else {
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);
276 if (ret)
277 goto cleanup;
278 ret = usb_ep_queue(fu->ep_out, fu->bot_req_out, GFP_KERNEL);
279 if (ret)
280 pr_err("%s(%d)\n", __func__, __LINE__);
282 wait_for_completion(&cmd->write_complete);
283 target_execute_cmd(se_cmd);
284 cleanup:
285 return ret;
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;
293 int ret;
295 fu->flags &= ~USBG_BOT_CMD_PEND;
297 if (req->status < 0)
298 return;
300 ret = bot_submit_command(fu, req->buf, req->actual);
301 if (ret)
302 pr_err("%s(%d): %d\n", __func__, __LINE__, ret);
305 static int bot_prepare_reqs(struct f_uas *fu)
307 int ret;
309 fu->bot_req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
310 if (!fu->bot_req_in)
311 goto err;
313 fu->bot_req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
314 if (!fu->bot_req_out)
315 goto err_out;
317 fu->cmd.req = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
318 if (!fu->cmd.req)
319 goto err_cmd;
321 fu->bot_status.req = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
322 if (!fu->bot_status.req)
323 goto err_sts;
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);
331 if (!fu->cmd.buf)
332 goto err_buf;
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);
340 if (ret)
341 goto err_queue;
342 return 0;
343 err_queue:
344 kfree(fu->cmd.buf);
345 fu->cmd.buf = NULL;
346 err_buf:
347 usb_ep_free_request(fu->ep_in, fu->bot_status.req);
348 err_sts:
349 usb_ep_free_request(fu->ep_out, fu->cmd.req);
350 fu->cmd.req = NULL;
351 err_cmd:
352 usb_ep_free_request(fu->ep_out, fu->bot_req_out);
353 fu->bot_req_out = NULL;
354 err_out:
355 usb_ep_free_request(fu->ep_in, fu->bot_req_in);
356 fu->bot_req_in = NULL;
357 err:
358 pr_err("BOT: endpoint setup failed\n");
359 return -ENOMEM;
362 static void bot_cleanup_old_alt(struct f_uas *fu)
364 if (!(fu->flags & USBG_ENABLED))
365 return;
367 usb_ep_disable(fu->ep_in);
368 usb_ep_disable(fu->ep_out);
370 if (!fu->bot_req_in)
371 return;
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);
378 kfree(fu->cmd.buf);
380 fu->bot_req_in = NULL;
381 fu->bot_req_out = NULL;
382 fu->cmd.req = NULL;
383 fu->bot_status.req = NULL;
384 fu->cmd.buf = 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;
391 int ret;
393 fu->flags = USBG_IS_BOT;
395 config_ep_by_speed(gadget, f, fu->ep_in);
396 ret = usb_ep_enable(fu->ep_in);
397 if (ret)
398 goto err_b_in;
400 config_ep_by_speed(gadget, f, fu->ep_out);
401 ret = usb_ep_enable(fu->ep_out);
402 if (ret)
403 goto err_b_out;
405 ret = bot_prepare_reqs(fu);
406 if (ret)
407 goto err_wq;
408 fu->flags |= USBG_ENABLED;
409 pr_info("Using the BOT protocol\n");
410 return;
411 err_wq:
412 usb_ep_disable(fu->ep_out);
413 err_b_out:
414 usb_ep_disable(fu->ep_in);
415 err_b_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);
426 int luns;
427 u8 *ret_lun;
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))
433 return -ENOTSUPP;
435 if (w_length < 1)
436 return -EINVAL;
437 if (w_value != 0)
438 return -EINVAL;
439 luns = atomic_read(&fu->tpg->tpg_port_count);
440 if (!luns) {
441 pr_err("No LUNs configured?\n");
442 return -EINVAL;
445 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
446 * accessed. The upper limit is 0xf
448 luns--;
449 if (luns > 0xf) {
450 pr_info_once("Limiting the number of luns to 16\n");
451 luns = 0xf;
453 ret_lun = cdev->req->buf;
454 *ret_lun = luns;
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);
461 return 0;
463 return -ENOTSUPP;
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 */
471 if (!stream->req_in)
472 return;
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);
486 kfree(fu->cmd.buf);
487 fu->cmd.req = NULL;
488 fu->cmd.buf = NULL;
491 static void uasp_cleanup_old_alt(struct f_uas *fu)
493 int i;
495 if (!(fu->flags & USBG_ENABLED))
496 return;
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);
519 if (!cmd->data_buf)
520 return -ENOMEM;
522 sg_copy_to_buffer(se_cmd->t_data_sg,
523 se_cmd->t_data_nents,
524 cmd->data_buf,
525 se_cmd->data_length);
527 stream->req_in->buf = cmd->data_buf;
528 } else {
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->complete = uasp_status_data_cmpl;
535 stream->req_in->length = se_cmd->data_length;
536 stream->req_in->context = cmd;
538 cmd->state = UASP_SEND_STATUS;
539 return 0;
542 static void uasp_prepare_status(struct usbg_cmd *cmd)
544 struct se_cmd *se_cmd = &cmd->se_cmd;
545 struct sense_iu *iu = &cmd->sense_iu;
546 struct uas_stream *stream = cmd->stream;
548 cmd->state = UASP_QUEUE_COMMAND;
549 iu->iu_id = IU_ID_STATUS;
550 iu->tag = cpu_to_be16(cmd->tag);
553 * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?);
555 iu->len = cpu_to_be16(se_cmd->scsi_sense_length);
556 iu->status = se_cmd->scsi_status;
557 stream->req_status->context = cmd;
558 stream->req_status->length = se_cmd->scsi_sense_length + 16;
559 stream->req_status->buf = iu;
560 stream->req_status->complete = uasp_status_data_cmpl;
563 static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req)
565 struct usbg_cmd *cmd = req->context;
566 struct uas_stream *stream = cmd->stream;
567 struct f_uas *fu = cmd->fu;
568 int ret;
570 if (req->status < 0)
571 goto cleanup;
573 switch (cmd->state) {
574 case UASP_SEND_DATA:
575 ret = uasp_prepare_r_request(cmd);
576 if (ret)
577 goto cleanup;
578 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
579 if (ret)
580 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
581 break;
583 case UASP_RECEIVE_DATA:
584 ret = usbg_prepare_w_request(cmd, stream->req_out);
585 if (ret)
586 goto cleanup;
587 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
588 if (ret)
589 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
590 break;
592 case UASP_SEND_STATUS:
593 uasp_prepare_status(cmd);
594 ret = usb_ep_queue(fu->ep_status, stream->req_status,
595 GFP_ATOMIC);
596 if (ret)
597 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
598 break;
600 case UASP_QUEUE_COMMAND:
601 transport_generic_free_cmd(&cmd->se_cmd, 0);
602 usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
603 break;
605 default:
606 BUG();
608 return;
610 cleanup:
611 transport_generic_free_cmd(&cmd->se_cmd, 0);
614 static int uasp_send_status_response(struct usbg_cmd *cmd)
616 struct f_uas *fu = cmd->fu;
617 struct uas_stream *stream = cmd->stream;
618 struct sense_iu *iu = &cmd->sense_iu;
620 iu->tag = cpu_to_be16(cmd->tag);
621 stream->req_status->complete = uasp_status_data_cmpl;
622 stream->req_status->context = cmd;
623 cmd->fu = fu;
624 uasp_prepare_status(cmd);
625 return usb_ep_queue(fu->ep_status, stream->req_status, GFP_ATOMIC);
628 static int uasp_send_read_response(struct usbg_cmd *cmd)
630 struct f_uas *fu = cmd->fu;
631 struct uas_stream *stream = cmd->stream;
632 struct sense_iu *iu = &cmd->sense_iu;
633 int ret;
635 cmd->fu = fu;
637 iu->tag = cpu_to_be16(cmd->tag);
638 if (fu->flags & USBG_USE_STREAMS) {
640 ret = uasp_prepare_r_request(cmd);
641 if (ret)
642 goto out;
643 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
644 if (ret) {
645 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
646 kfree(cmd->data_buf);
647 cmd->data_buf = NULL;
650 } else {
652 iu->iu_id = IU_ID_READ_READY;
653 iu->tag = cpu_to_be16(cmd->tag);
655 stream->req_status->complete = uasp_status_data_cmpl;
656 stream->req_status->context = cmd;
658 cmd->state = UASP_SEND_DATA;
659 stream->req_status->buf = iu;
660 stream->req_status->length = sizeof(struct iu);
662 ret = usb_ep_queue(fu->ep_status, stream->req_status,
663 GFP_ATOMIC);
664 if (ret)
665 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
667 out:
668 return ret;
671 static int uasp_send_write_request(struct usbg_cmd *cmd)
673 struct f_uas *fu = cmd->fu;
674 struct se_cmd *se_cmd = &cmd->se_cmd;
675 struct uas_stream *stream = cmd->stream;
676 struct sense_iu *iu = &cmd->sense_iu;
677 int ret;
679 init_completion(&cmd->write_complete);
680 cmd->fu = fu;
682 iu->tag = cpu_to_be16(cmd->tag);
684 if (fu->flags & USBG_USE_STREAMS) {
686 ret = usbg_prepare_w_request(cmd, stream->req_out);
687 if (ret)
688 goto cleanup;
689 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
690 if (ret)
691 pr_err("%s(%d)\n", __func__, __LINE__);
693 } else {
695 iu->iu_id = IU_ID_WRITE_READY;
696 iu->tag = cpu_to_be16(cmd->tag);
698 stream->req_status->complete = uasp_status_data_cmpl;
699 stream->req_status->context = cmd;
701 cmd->state = UASP_RECEIVE_DATA;
702 stream->req_status->buf = iu;
703 stream->req_status->length = sizeof(struct iu);
705 ret = usb_ep_queue(fu->ep_status, stream->req_status,
706 GFP_ATOMIC);
707 if (ret)
708 pr_err("%s(%d)\n", __func__, __LINE__);
711 wait_for_completion(&cmd->write_complete);
712 target_execute_cmd(se_cmd);
713 cleanup:
714 return ret;
717 static int usbg_submit_command(struct f_uas *, void *, unsigned int);
719 static void uasp_cmd_complete(struct usb_ep *ep, struct usb_request *req)
721 struct f_uas *fu = req->context;
722 int ret;
724 if (req->status < 0)
725 return;
727 ret = usbg_submit_command(fu, req->buf, req->actual);
729 * Once we tune for performance enqueue the command req here again so
730 * we can receive a second command while we processing this one. Pay
731 * attention to properly sync STAUS endpoint with DATA IN + OUT so you
732 * don't break HS.
734 if (!ret)
735 return;
736 usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
739 static int uasp_alloc_stream_res(struct f_uas *fu, struct uas_stream *stream)
741 stream->req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
742 if (!stream->req_in)
743 goto out;
745 stream->req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
746 if (!stream->req_out)
747 goto err_out;
749 stream->req_status = usb_ep_alloc_request(fu->ep_status, GFP_KERNEL);
750 if (!stream->req_status)
751 goto err_sts;
753 return 0;
754 err_sts:
755 usb_ep_free_request(fu->ep_status, stream->req_status);
756 stream->req_status = NULL;
757 err_out:
758 usb_ep_free_request(fu->ep_out, stream->req_out);
759 stream->req_out = NULL;
760 out:
761 return -ENOMEM;
764 static int uasp_alloc_cmd(struct f_uas *fu)
766 fu->cmd.req = usb_ep_alloc_request(fu->ep_cmd, GFP_KERNEL);
767 if (!fu->cmd.req)
768 goto err;
770 fu->cmd.buf = kmalloc(fu->ep_cmd->maxpacket, GFP_KERNEL);
771 if (!fu->cmd.buf)
772 goto err_buf;
774 fu->cmd.req->complete = uasp_cmd_complete;
775 fu->cmd.req->buf = fu->cmd.buf;
776 fu->cmd.req->length = fu->ep_cmd->maxpacket;
777 fu->cmd.req->context = fu;
778 return 0;
780 err_buf:
781 usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
782 err:
783 return -ENOMEM;
786 static void uasp_setup_stream_res(struct f_uas *fu, int max_streams)
788 int i;
790 for (i = 0; i < max_streams; i++) {
791 struct uas_stream *s = &fu->stream[i];
793 s->req_in->stream_id = i + 1;
794 s->req_out->stream_id = i + 1;
795 s->req_status->stream_id = i + 1;
799 static int uasp_prepare_reqs(struct f_uas *fu)
801 int ret;
802 int i;
803 int max_streams;
805 if (fu->flags & USBG_USE_STREAMS)
806 max_streams = UASP_SS_EP_COMP_NUM_STREAMS;
807 else
808 max_streams = 1;
810 for (i = 0; i < max_streams; i++) {
811 ret = uasp_alloc_stream_res(fu, &fu->stream[i]);
812 if (ret)
813 goto err_cleanup;
816 ret = uasp_alloc_cmd(fu);
817 if (ret)
818 goto err_free_stream;
819 uasp_setup_stream_res(fu, max_streams);
821 ret = usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
822 if (ret)
823 goto err_free_stream;
825 return 0;
827 err_free_stream:
828 uasp_free_cmdreq(fu);
830 err_cleanup:
831 if (i) {
832 do {
833 uasp_cleanup_one_stream(fu, &fu->stream[i - 1]);
834 i--;
835 } while (i);
837 pr_err("UASP: endpoint setup failed\n");
838 return ret;
841 static void uasp_set_alt(struct f_uas *fu)
843 struct usb_function *f = &fu->function;
844 struct usb_gadget *gadget = f->config->cdev->gadget;
845 int ret;
847 fu->flags = USBG_IS_UAS;
849 if (gadget->speed >= USB_SPEED_SUPER)
850 fu->flags |= USBG_USE_STREAMS;
852 config_ep_by_speed(gadget, f, fu->ep_in);
853 ret = usb_ep_enable(fu->ep_in);
854 if (ret)
855 goto err_b_in;
857 config_ep_by_speed(gadget, f, fu->ep_out);
858 ret = usb_ep_enable(fu->ep_out);
859 if (ret)
860 goto err_b_out;
862 config_ep_by_speed(gadget, f, fu->ep_cmd);
863 ret = usb_ep_enable(fu->ep_cmd);
864 if (ret)
865 goto err_cmd;
866 config_ep_by_speed(gadget, f, fu->ep_status);
867 ret = usb_ep_enable(fu->ep_status);
868 if (ret)
869 goto err_status;
871 ret = uasp_prepare_reqs(fu);
872 if (ret)
873 goto err_wq;
874 fu->flags |= USBG_ENABLED;
876 pr_info("Using the UAS protocol\n");
877 return;
878 err_wq:
879 usb_ep_disable(fu->ep_status);
880 err_status:
881 usb_ep_disable(fu->ep_cmd);
882 err_cmd:
883 usb_ep_disable(fu->ep_out);
884 err_b_out:
885 usb_ep_disable(fu->ep_in);
886 err_b_in:
887 fu->flags = 0;
890 static int get_cmd_dir(const unsigned char *cdb)
892 int ret;
894 switch (cdb[0]) {
895 case READ_6:
896 case READ_10:
897 case READ_12:
898 case READ_16:
899 case INQUIRY:
900 case MODE_SENSE:
901 case MODE_SENSE_10:
902 case SERVICE_ACTION_IN_16:
903 case MAINTENANCE_IN:
904 case PERSISTENT_RESERVE_IN:
905 case SECURITY_PROTOCOL_IN:
906 case ACCESS_CONTROL_IN:
907 case REPORT_LUNS:
908 case READ_BLOCK_LIMITS:
909 case READ_POSITION:
910 case READ_CAPACITY:
911 case READ_TOC:
912 case READ_FORMAT_CAPACITIES:
913 case REQUEST_SENSE:
914 ret = DMA_FROM_DEVICE;
915 break;
917 case WRITE_6:
918 case WRITE_10:
919 case WRITE_12:
920 case WRITE_16:
921 case MODE_SELECT:
922 case MODE_SELECT_10:
923 case WRITE_VERIFY:
924 case WRITE_VERIFY_12:
925 case PERSISTENT_RESERVE_OUT:
926 case MAINTENANCE_OUT:
927 case SECURITY_PROTOCOL_OUT:
928 case ACCESS_CONTROL_OUT:
929 ret = DMA_TO_DEVICE;
930 break;
931 case ALLOW_MEDIUM_REMOVAL:
932 case TEST_UNIT_READY:
933 case SYNCHRONIZE_CACHE:
934 case START_STOP:
935 case ERASE:
936 case REZERO_UNIT:
937 case SEEK_10:
938 case SPACE:
939 case VERIFY:
940 case WRITE_FILEMARKS:
941 ret = DMA_NONE;
942 break;
943 default:
944 #define CMD_DIR_MSG "target: Unknown data direction for SCSI Opcode 0x%02x\n"
945 pr_warn(CMD_DIR_MSG, cdb[0]);
946 #undef CMD_DIR_MSG
947 ret = -EINVAL;
949 return ret;
952 static void usbg_data_write_cmpl(struct usb_ep *ep, struct usb_request *req)
954 struct usbg_cmd *cmd = req->context;
955 struct se_cmd *se_cmd = &cmd->se_cmd;
957 if (req->status < 0) {
958 pr_err("%s() state %d transfer failed\n", __func__, cmd->state);
959 goto cleanup;
962 if (req->num_sgs == 0) {
963 sg_copy_from_buffer(se_cmd->t_data_sg,
964 se_cmd->t_data_nents,
965 cmd->data_buf,
966 se_cmd->data_length);
969 complete(&cmd->write_complete);
970 return;
972 cleanup:
973 transport_generic_free_cmd(&cmd->se_cmd, 0);
976 static int usbg_prepare_w_request(struct usbg_cmd *cmd, struct usb_request *req)
978 struct se_cmd *se_cmd = &cmd->se_cmd;
979 struct f_uas *fu = cmd->fu;
980 struct usb_gadget *gadget = fuas_to_gadget(fu);
982 if (!gadget->sg_supported) {
983 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
984 if (!cmd->data_buf)
985 return -ENOMEM;
987 req->buf = cmd->data_buf;
988 } else {
989 req->buf = NULL;
990 req->num_sgs = se_cmd->t_data_nents;
991 req->sg = se_cmd->t_data_sg;
994 req->complete = usbg_data_write_cmpl;
995 req->length = se_cmd->data_length;
996 req->context = cmd;
997 return 0;
1000 static int usbg_send_status_response(struct se_cmd *se_cmd)
1002 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1003 se_cmd);
1004 struct f_uas *fu = cmd->fu;
1006 if (fu->flags & USBG_IS_BOT)
1007 return bot_send_status_response(cmd);
1008 else
1009 return uasp_send_status_response(cmd);
1012 static int usbg_send_write_request(struct se_cmd *se_cmd)
1014 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1015 se_cmd);
1016 struct f_uas *fu = cmd->fu;
1018 if (fu->flags & USBG_IS_BOT)
1019 return bot_send_write_request(cmd);
1020 else
1021 return uasp_send_write_request(cmd);
1024 static int usbg_send_read_response(struct se_cmd *se_cmd)
1026 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1027 se_cmd);
1028 struct f_uas *fu = cmd->fu;
1030 if (fu->flags & USBG_IS_BOT)
1031 return bot_send_read_response(cmd);
1032 else
1033 return uasp_send_read_response(cmd);
1036 static void usbg_cmd_work(struct work_struct *work)
1038 struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
1039 struct se_cmd *se_cmd;
1040 struct tcm_usbg_nexus *tv_nexus;
1041 struct usbg_tpg *tpg;
1042 int dir, flags = (TARGET_SCF_UNKNOWN_SIZE | TARGET_SCF_ACK_KREF);
1044 se_cmd = &cmd->se_cmd;
1045 tpg = cmd->fu->tpg;
1046 tv_nexus = tpg->tpg_nexus;
1047 dir = get_cmd_dir(cmd->cmd_buf);
1048 if (dir < 0) {
1049 transport_init_se_cmd(se_cmd,
1050 tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
1051 tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
1052 cmd->prio_attr, cmd->sense_iu.sense);
1053 goto out;
1056 if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess, cmd->cmd_buf,
1057 cmd->sense_iu.sense, cmd->unpacked_lun, 0,
1058 cmd->prio_attr, dir, flags) < 0)
1059 goto out;
1061 return;
1063 out:
1064 transport_send_check_condition_and_sense(se_cmd,
1065 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1066 transport_generic_free_cmd(&cmd->se_cmd, 0);
1069 static struct usbg_cmd *usbg_get_cmd(struct f_uas *fu,
1070 struct tcm_usbg_nexus *tv_nexus, u32 scsi_tag)
1072 struct se_session *se_sess = tv_nexus->tvn_se_sess;
1073 struct usbg_cmd *cmd;
1074 int tag, cpu;
1076 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
1077 if (tag < 0)
1078 return ERR_PTR(-ENOMEM);
1080 cmd = &((struct usbg_cmd *)se_sess->sess_cmd_map)[tag];
1081 memset(cmd, 0, sizeof(*cmd));
1082 cmd->se_cmd.map_tag = tag;
1083 cmd->se_cmd.map_cpu = cpu;
1084 cmd->se_cmd.tag = cmd->tag = scsi_tag;
1085 cmd->fu = fu;
1087 return cmd;
1090 static void usbg_release_cmd(struct se_cmd *);
1092 static int usbg_submit_command(struct f_uas *fu,
1093 void *cmdbuf, unsigned int len)
1095 struct command_iu *cmd_iu = cmdbuf;
1096 struct usbg_cmd *cmd;
1097 struct usbg_tpg *tpg = fu->tpg;
1098 struct tcm_usbg_nexus *tv_nexus;
1099 u32 cmd_len;
1100 u16 scsi_tag;
1102 if (cmd_iu->iu_id != IU_ID_COMMAND) {
1103 pr_err("Unsupported type %d\n", cmd_iu->iu_id);
1104 return -EINVAL;
1107 tv_nexus = tpg->tpg_nexus;
1108 if (!tv_nexus) {
1109 pr_err("Missing nexus, ignoring command\n");
1110 return -EINVAL;
1113 cmd_len = (cmd_iu->len & ~0x3) + 16;
1114 if (cmd_len > USBG_MAX_CMD)
1115 return -EINVAL;
1117 scsi_tag = be16_to_cpup(&cmd_iu->tag);
1118 cmd = usbg_get_cmd(fu, tv_nexus, scsi_tag);
1119 if (IS_ERR(cmd)) {
1120 pr_err("usbg_get_cmd failed\n");
1121 return -ENOMEM;
1123 memcpy(cmd->cmd_buf, cmd_iu->cdb, cmd_len);
1125 if (fu->flags & USBG_USE_STREAMS) {
1126 if (cmd->tag > UASP_SS_EP_COMP_NUM_STREAMS)
1127 goto err;
1128 if (!cmd->tag)
1129 cmd->stream = &fu->stream[0];
1130 else
1131 cmd->stream = &fu->stream[cmd->tag - 1];
1132 } else {
1133 cmd->stream = &fu->stream[0];
1136 switch (cmd_iu->prio_attr & 0x7) {
1137 case UAS_HEAD_TAG:
1138 cmd->prio_attr = TCM_HEAD_TAG;
1139 break;
1140 case UAS_ORDERED_TAG:
1141 cmd->prio_attr = TCM_ORDERED_TAG;
1142 break;
1143 case UAS_ACA:
1144 cmd->prio_attr = TCM_ACA_TAG;
1145 break;
1146 default:
1147 pr_debug_once("Unsupported prio_attr: %02x.\n",
1148 cmd_iu->prio_attr);
1149 /* fall through */
1150 case UAS_SIMPLE_TAG:
1151 cmd->prio_attr = TCM_SIMPLE_TAG;
1152 break;
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);
1160 return 0;
1161 err:
1162 usbg_release_cmd(&cmd->se_cmd);
1163 return -EINVAL;
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;
1172 int dir;
1174 se_cmd = &cmd->se_cmd;
1175 tpg = cmd->fu->tpg;
1176 tv_nexus = tpg->tpg_nexus;
1177 dir = get_cmd_dir(cmd->cmd_buf);
1178 if (dir < 0) {
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);
1183 goto out;
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)
1189 goto out;
1191 return;
1193 out:
1194 transport_send_check_condition_and_sense(se_cmd,
1195 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1196 transport_generic_free_cmd(&cmd->se_cmd, 0);
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 = fu->tpg;
1205 struct tcm_usbg_nexus *tv_nexus;
1206 u32 cmd_len;
1208 if (cbw->Signature != cpu_to_le32(US_BULK_CB_SIGN)) {
1209 pr_err("Wrong signature on CBW\n");
1210 return -EINVAL;
1212 if (len != 31) {
1213 pr_err("Wrong length for CBW\n");
1214 return -EINVAL;
1217 cmd_len = cbw->Length;
1218 if (cmd_len < 1 || cmd_len > 16)
1219 return -EINVAL;
1221 tv_nexus = tpg->tpg_nexus;
1222 if (!tv_nexus) {
1223 pr_err("Missing nexus, ignoring command\n");
1224 return -ENODEV;
1227 cmd = usbg_get_cmd(fu, tv_nexus, cbw->Tag);
1228 if (IS_ERR(cmd)) {
1229 pr_err("usbg_get_cmd failed\n");
1230 return -ENOMEM;
1232 memcpy(cmd->cmd_buf, cbw->CDB, cmd_len);
1234 cmd->bot_tag = cbw->Tag;
1235 cmd->prio_attr = TCM_SIMPLE_TAG;
1236 cmd->unpacked_lun = cbw->Lun;
1237 cmd->is_read = cbw->Flags & US_BULK_FLAG_IN ? 1 : 0;
1238 cmd->data_len = le32_to_cpu(cbw->DataTransferLength);
1239 cmd->se_cmd.tag = le32_to_cpu(cmd->bot_tag);
1241 INIT_WORK(&cmd->work, bot_cmd_work);
1242 queue_work(tpg->workqueue, &cmd->work);
1244 return 0;
1247 /* Start fabric.c code */
1249 static int usbg_check_true(struct se_portal_group *se_tpg)
1251 return 1;
1254 static int usbg_check_false(struct se_portal_group *se_tpg)
1256 return 0;
1259 static char *usbg_get_fabric_wwn(struct se_portal_group *se_tpg)
1261 struct usbg_tpg *tpg = container_of(se_tpg,
1262 struct usbg_tpg, se_tpg);
1263 struct usbg_tport *tport = tpg->tport;
1265 return &tport->tport_name[0];
1268 static u16 usbg_get_tag(struct se_portal_group *se_tpg)
1270 struct usbg_tpg *tpg = container_of(se_tpg,
1271 struct usbg_tpg, se_tpg);
1272 return tpg->tport_tpgt;
1275 static u32 usbg_tpg_get_inst_index(struct se_portal_group *se_tpg)
1277 return 1;
1280 static void usbg_release_cmd(struct se_cmd *se_cmd)
1282 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1283 se_cmd);
1284 struct se_session *se_sess = se_cmd->se_sess;
1286 kfree(cmd->data_buf);
1287 target_free_tag(se_sess, se_cmd);
1290 static u32 usbg_sess_get_index(struct se_session *se_sess)
1292 return 0;
1295 static void usbg_set_default_node_attrs(struct se_node_acl *nacl)
1299 static int usbg_get_cmd_state(struct se_cmd *se_cmd)
1301 return 0;
1304 static void usbg_queue_tm_rsp(struct se_cmd *se_cmd)
1308 static void usbg_aborted_task(struct se_cmd *se_cmd)
1312 static const char *usbg_check_wwn(const char *name)
1314 const char *n;
1315 unsigned int len;
1317 n = strstr(name, "naa.");
1318 if (!n)
1319 return NULL;
1320 n += 4;
1321 len = strlen(n);
1322 if (len == 0 || len > USBG_NAMELEN - 1)
1323 return NULL;
1324 return n;
1327 static int usbg_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
1329 if (!usbg_check_wwn(name))
1330 return -EINVAL;
1331 return 0;
1334 static struct se_portal_group *usbg_make_tpg(struct se_wwn *wwn,
1335 const char *name)
1337 struct usbg_tport *tport = container_of(wwn, struct usbg_tport,
1338 tport_wwn);
1339 struct usbg_tpg *tpg;
1340 unsigned long tpgt;
1341 int ret;
1342 struct f_tcm_opts *opts;
1343 unsigned i;
1345 if (strstr(name, "tpgt_") != name)
1346 return ERR_PTR(-EINVAL);
1347 if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX)
1348 return ERR_PTR(-EINVAL);
1349 ret = -ENODEV;
1350 mutex_lock(&tpg_instances_lock);
1351 for (i = 0; i < TPG_INSTANCES; ++i)
1352 if (tpg_instances[i].func_inst && !tpg_instances[i].tpg)
1353 break;
1354 if (i == TPG_INSTANCES)
1355 goto unlock_inst;
1357 opts = container_of(tpg_instances[i].func_inst, struct f_tcm_opts,
1358 func_inst);
1359 mutex_lock(&opts->dep_lock);
1360 if (!opts->ready)
1361 goto unlock_dep;
1363 if (opts->has_dep) {
1364 if (!try_module_get(opts->dependent))
1365 goto unlock_dep;
1366 } else {
1367 ret = configfs_depend_item_unlocked(
1368 wwn->wwn_group.cg_subsys,
1369 &opts->func_inst.group.cg_item);
1370 if (ret)
1371 goto unlock_dep;
1374 tpg = kzalloc(sizeof(struct usbg_tpg), GFP_KERNEL);
1375 ret = -ENOMEM;
1376 if (!tpg)
1377 goto unref_dep;
1378 mutex_init(&tpg->tpg_mutex);
1379 atomic_set(&tpg->tpg_port_count, 0);
1380 tpg->workqueue = alloc_workqueue("tcm_usb_gadget", 0, 1);
1381 if (!tpg->workqueue)
1382 goto free_tpg;
1384 tpg->tport = tport;
1385 tpg->tport_tpgt = tpgt;
1388 * SPC doesn't assign a protocol identifier for USB-SCSI, so we
1389 * pretend to be SAS..
1391 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_SAS);
1392 if (ret < 0)
1393 goto free_workqueue;
1395 tpg_instances[i].tpg = tpg;
1396 tpg->fi = tpg_instances[i].func_inst;
1397 mutex_unlock(&opts->dep_lock);
1398 mutex_unlock(&tpg_instances_lock);
1399 return &tpg->se_tpg;
1401 free_workqueue:
1402 destroy_workqueue(tpg->workqueue);
1403 free_tpg:
1404 kfree(tpg);
1405 unref_dep:
1406 if (opts->has_dep)
1407 module_put(opts->dependent);
1408 else
1409 configfs_undepend_item_unlocked(&opts->func_inst.group.cg_item);
1410 unlock_dep:
1411 mutex_unlock(&opts->dep_lock);
1412 unlock_inst:
1413 mutex_unlock(&tpg_instances_lock);
1415 return ERR_PTR(ret);
1418 static int tcm_usbg_drop_nexus(struct usbg_tpg *);
1420 static void usbg_drop_tpg(struct se_portal_group *se_tpg)
1422 struct usbg_tpg *tpg = container_of(se_tpg,
1423 struct usbg_tpg, se_tpg);
1424 unsigned i;
1425 struct f_tcm_opts *opts;
1427 tcm_usbg_drop_nexus(tpg);
1428 core_tpg_deregister(se_tpg);
1429 destroy_workqueue(tpg->workqueue);
1431 mutex_lock(&tpg_instances_lock);
1432 for (i = 0; i < TPG_INSTANCES; ++i)
1433 if (tpg_instances[i].tpg == tpg)
1434 break;
1435 if (i < TPG_INSTANCES) {
1436 tpg_instances[i].tpg = NULL;
1437 opts = container_of(tpg_instances[i].func_inst,
1438 struct f_tcm_opts, func_inst);
1439 mutex_lock(&opts->dep_lock);
1440 if (opts->has_dep)
1441 module_put(opts->dependent);
1442 else
1443 configfs_undepend_item_unlocked(
1444 &opts->func_inst.group.cg_item);
1445 mutex_unlock(&opts->dep_lock);
1447 mutex_unlock(&tpg_instances_lock);
1449 kfree(tpg);
1452 static struct se_wwn *usbg_make_tport(
1453 struct target_fabric_configfs *tf,
1454 struct config_group *group,
1455 const char *name)
1457 struct usbg_tport *tport;
1458 const char *wnn_name;
1459 u64 wwpn = 0;
1461 wnn_name = usbg_check_wwn(name);
1462 if (!wnn_name)
1463 return ERR_PTR(-EINVAL);
1465 tport = kzalloc(sizeof(struct usbg_tport), GFP_KERNEL);
1466 if (!(tport))
1467 return ERR_PTR(-ENOMEM);
1469 tport->tport_wwpn = wwpn;
1470 snprintf(tport->tport_name, sizeof(tport->tport_name), "%s", wnn_name);
1471 return &tport->tport_wwn;
1474 static void usbg_drop_tport(struct se_wwn *wwn)
1476 struct usbg_tport *tport = container_of(wwn,
1477 struct usbg_tport, tport_wwn);
1478 kfree(tport);
1482 * If somebody feels like dropping the version property, go ahead.
1484 static ssize_t usbg_wwn_version_show(struct config_item *item, char *page)
1486 return sprintf(page, "usb-gadget fabric module\n");
1489 CONFIGFS_ATTR_RO(usbg_wwn_, version);
1491 static struct configfs_attribute *usbg_wwn_attrs[] = {
1492 &usbg_wwn_attr_version,
1493 NULL,
1496 static ssize_t tcm_usbg_tpg_enable_show(struct config_item *item, char *page)
1498 struct se_portal_group *se_tpg = to_tpg(item);
1499 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1501 return snprintf(page, PAGE_SIZE, "%u\n", tpg->gadget_connect);
1504 static int usbg_attach(struct usbg_tpg *);
1505 static void usbg_detach(struct usbg_tpg *);
1507 static ssize_t tcm_usbg_tpg_enable_store(struct config_item *item,
1508 const char *page, size_t count)
1510 struct se_portal_group *se_tpg = to_tpg(item);
1511 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1512 bool op;
1513 ssize_t ret;
1515 ret = strtobool(page, &op);
1516 if (ret)
1517 return ret;
1519 if ((op && tpg->gadget_connect) || (!op && !tpg->gadget_connect))
1520 return -EINVAL;
1522 if (op)
1523 ret = usbg_attach(tpg);
1524 else
1525 usbg_detach(tpg);
1526 if (ret)
1527 return ret;
1529 tpg->gadget_connect = op;
1531 return count;
1534 static ssize_t tcm_usbg_tpg_nexus_show(struct config_item *item, char *page)
1536 struct se_portal_group *se_tpg = to_tpg(item);
1537 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1538 struct tcm_usbg_nexus *tv_nexus;
1539 ssize_t ret;
1541 mutex_lock(&tpg->tpg_mutex);
1542 tv_nexus = tpg->tpg_nexus;
1543 if (!tv_nexus) {
1544 ret = -ENODEV;
1545 goto out;
1547 ret = snprintf(page, PAGE_SIZE, "%s\n",
1548 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1549 out:
1550 mutex_unlock(&tpg->tpg_mutex);
1551 return ret;
1554 static int usbg_alloc_sess_cb(struct se_portal_group *se_tpg,
1555 struct se_session *se_sess, void *p)
1557 struct usbg_tpg *tpg = container_of(se_tpg,
1558 struct usbg_tpg, se_tpg);
1560 tpg->tpg_nexus = p;
1561 return 0;
1564 static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name)
1566 struct tcm_usbg_nexus *tv_nexus;
1567 int ret = 0;
1569 mutex_lock(&tpg->tpg_mutex);
1570 if (tpg->tpg_nexus) {
1571 ret = -EEXIST;
1572 pr_debug("tpg->tpg_nexus already exists\n");
1573 goto out_unlock;
1576 tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
1577 if (!tv_nexus) {
1578 ret = -ENOMEM;
1579 goto out_unlock;
1582 tv_nexus->tvn_se_sess = target_setup_session(&tpg->se_tpg,
1583 USB_G_DEFAULT_SESSION_TAGS,
1584 sizeof(struct usbg_cmd),
1585 TARGET_PROT_NORMAL, name,
1586 tv_nexus, usbg_alloc_sess_cb);
1587 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1588 #define MAKE_NEXUS_MSG "core_tpg_check_initiator_node_acl() failed for %s\n"
1589 pr_debug(MAKE_NEXUS_MSG, name);
1590 #undef MAKE_NEXUS_MSG
1591 ret = PTR_ERR(tv_nexus->tvn_se_sess);
1592 kfree(tv_nexus);
1595 out_unlock:
1596 mutex_unlock(&tpg->tpg_mutex);
1597 return ret;
1600 static int tcm_usbg_drop_nexus(struct usbg_tpg *tpg)
1602 struct se_session *se_sess;
1603 struct tcm_usbg_nexus *tv_nexus;
1604 int ret = -ENODEV;
1606 mutex_lock(&tpg->tpg_mutex);
1607 tv_nexus = tpg->tpg_nexus;
1608 if (!tv_nexus)
1609 goto out;
1611 se_sess = tv_nexus->tvn_se_sess;
1612 if (!se_sess)
1613 goto out;
1615 if (atomic_read(&tpg->tpg_port_count)) {
1616 ret = -EPERM;
1617 #define MSG "Unable to remove Host I_T Nexus with active TPG port count: %d\n"
1618 pr_err(MSG, atomic_read(&tpg->tpg_port_count));
1619 #undef MSG
1620 goto out;
1623 pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
1624 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1626 * Release the SCSI I_T Nexus to the emulated vHost Target Port
1628 target_remove_session(se_sess);
1629 tpg->tpg_nexus = NULL;
1631 kfree(tv_nexus);
1632 ret = 0;
1633 out:
1634 mutex_unlock(&tpg->tpg_mutex);
1635 return ret;
1638 static ssize_t tcm_usbg_tpg_nexus_store(struct config_item *item,
1639 const char *page, size_t count)
1641 struct se_portal_group *se_tpg = to_tpg(item);
1642 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1643 unsigned char i_port[USBG_NAMELEN], *ptr;
1644 int ret;
1646 if (!strncmp(page, "NULL", 4)) {
1647 ret = tcm_usbg_drop_nexus(tpg);
1648 return (!ret) ? count : ret;
1650 if (strlen(page) >= USBG_NAMELEN) {
1652 #define NEXUS_STORE_MSG "Emulated NAA Sas Address: %s, exceeds max: %d\n"
1653 pr_err(NEXUS_STORE_MSG, page, USBG_NAMELEN);
1654 #undef NEXUS_STORE_MSG
1655 return -EINVAL;
1657 snprintf(i_port, USBG_NAMELEN, "%s", page);
1659 ptr = strstr(i_port, "naa.");
1660 if (!ptr) {
1661 pr_err("Missing 'naa.' prefix\n");
1662 return -EINVAL;
1665 if (i_port[strlen(i_port) - 1] == '\n')
1666 i_port[strlen(i_port) - 1] = '\0';
1668 ret = tcm_usbg_make_nexus(tpg, &i_port[0]);
1669 if (ret < 0)
1670 return ret;
1671 return count;
1674 CONFIGFS_ATTR(tcm_usbg_tpg_, enable);
1675 CONFIGFS_ATTR(tcm_usbg_tpg_, nexus);
1677 static struct configfs_attribute *usbg_base_attrs[] = {
1678 &tcm_usbg_tpg_attr_enable,
1679 &tcm_usbg_tpg_attr_nexus,
1680 NULL,
1683 static int usbg_port_link(struct se_portal_group *se_tpg, struct se_lun *lun)
1685 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1687 atomic_inc(&tpg->tpg_port_count);
1688 smp_mb__after_atomic();
1689 return 0;
1692 static void usbg_port_unlink(struct se_portal_group *se_tpg,
1693 struct se_lun *se_lun)
1695 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1697 atomic_dec(&tpg->tpg_port_count);
1698 smp_mb__after_atomic();
1701 static int usbg_check_stop_free(struct se_cmd *se_cmd)
1703 return target_put_sess_cmd(se_cmd);
1706 static const struct target_core_fabric_ops usbg_ops = {
1707 .module = THIS_MODULE,
1708 .fabric_name = "usb_gadget",
1709 .tpg_get_wwn = usbg_get_fabric_wwn,
1710 .tpg_get_tag = usbg_get_tag,
1711 .tpg_check_demo_mode = usbg_check_true,
1712 .tpg_check_demo_mode_cache = usbg_check_false,
1713 .tpg_check_demo_mode_write_protect = usbg_check_false,
1714 .tpg_check_prod_mode_write_protect = usbg_check_false,
1715 .tpg_get_inst_index = usbg_tpg_get_inst_index,
1716 .release_cmd = usbg_release_cmd,
1717 .sess_get_index = usbg_sess_get_index,
1718 .sess_get_initiator_sid = NULL,
1719 .write_pending = usbg_send_write_request,
1720 .set_default_node_attributes = usbg_set_default_node_attrs,
1721 .get_cmd_state = usbg_get_cmd_state,
1722 .queue_data_in = usbg_send_read_response,
1723 .queue_status = usbg_send_status_response,
1724 .queue_tm_rsp = usbg_queue_tm_rsp,
1725 .aborted_task = usbg_aborted_task,
1726 .check_stop_free = usbg_check_stop_free,
1728 .fabric_make_wwn = usbg_make_tport,
1729 .fabric_drop_wwn = usbg_drop_tport,
1730 .fabric_make_tpg = usbg_make_tpg,
1731 .fabric_drop_tpg = usbg_drop_tpg,
1732 .fabric_post_link = usbg_port_link,
1733 .fabric_pre_unlink = usbg_port_unlink,
1734 .fabric_init_nodeacl = usbg_init_nodeacl,
1736 .tfc_wwn_attrs = usbg_wwn_attrs,
1737 .tfc_tpg_base_attrs = usbg_base_attrs,
1740 /* Start gadget.c code */
1742 static struct usb_interface_descriptor bot_intf_desc = {
1743 .bLength = sizeof(bot_intf_desc),
1744 .bDescriptorType = USB_DT_INTERFACE,
1745 .bNumEndpoints = 2,
1746 .bAlternateSetting = USB_G_ALT_INT_BBB,
1747 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1748 .bInterfaceSubClass = USB_SC_SCSI,
1749 .bInterfaceProtocol = USB_PR_BULK,
1752 static struct usb_interface_descriptor uasp_intf_desc = {
1753 .bLength = sizeof(uasp_intf_desc),
1754 .bDescriptorType = USB_DT_INTERFACE,
1755 .bNumEndpoints = 4,
1756 .bAlternateSetting = USB_G_ALT_INT_UAS,
1757 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1758 .bInterfaceSubClass = USB_SC_SCSI,
1759 .bInterfaceProtocol = USB_PR_UAS,
1762 static struct usb_endpoint_descriptor uasp_bi_desc = {
1763 .bLength = USB_DT_ENDPOINT_SIZE,
1764 .bDescriptorType = USB_DT_ENDPOINT,
1765 .bEndpointAddress = USB_DIR_IN,
1766 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1767 .wMaxPacketSize = cpu_to_le16(512),
1770 static struct usb_endpoint_descriptor uasp_fs_bi_desc = {
1771 .bLength = USB_DT_ENDPOINT_SIZE,
1772 .bDescriptorType = USB_DT_ENDPOINT,
1773 .bEndpointAddress = USB_DIR_IN,
1774 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1777 static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc = {
1778 .bLength = sizeof(uasp_bi_pipe_desc),
1779 .bDescriptorType = USB_DT_PIPE_USAGE,
1780 .bPipeID = DATA_IN_PIPE_ID,
1783 static struct usb_endpoint_descriptor uasp_ss_bi_desc = {
1784 .bLength = USB_DT_ENDPOINT_SIZE,
1785 .bDescriptorType = USB_DT_ENDPOINT,
1786 .bEndpointAddress = USB_DIR_IN,
1787 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1788 .wMaxPacketSize = cpu_to_le16(1024),
1791 static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc = {
1792 .bLength = sizeof(uasp_bi_ep_comp_desc),
1793 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1794 .bMaxBurst = 0,
1795 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1796 .wBytesPerInterval = 0,
1799 static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc = {
1800 .bLength = sizeof(bot_bi_ep_comp_desc),
1801 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1802 .bMaxBurst = 0,
1805 static struct usb_endpoint_descriptor uasp_bo_desc = {
1806 .bLength = USB_DT_ENDPOINT_SIZE,
1807 .bDescriptorType = USB_DT_ENDPOINT,
1808 .bEndpointAddress = USB_DIR_OUT,
1809 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1810 .wMaxPacketSize = cpu_to_le16(512),
1813 static struct usb_endpoint_descriptor uasp_fs_bo_desc = {
1814 .bLength = USB_DT_ENDPOINT_SIZE,
1815 .bDescriptorType = USB_DT_ENDPOINT,
1816 .bEndpointAddress = USB_DIR_OUT,
1817 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1820 static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc = {
1821 .bLength = sizeof(uasp_bo_pipe_desc),
1822 .bDescriptorType = USB_DT_PIPE_USAGE,
1823 .bPipeID = DATA_OUT_PIPE_ID,
1826 static struct usb_endpoint_descriptor uasp_ss_bo_desc = {
1827 .bLength = USB_DT_ENDPOINT_SIZE,
1828 .bDescriptorType = USB_DT_ENDPOINT,
1829 .bEndpointAddress = USB_DIR_OUT,
1830 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1831 .wMaxPacketSize = cpu_to_le16(0x400),
1834 static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc = {
1835 .bLength = sizeof(uasp_bo_ep_comp_desc),
1836 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1837 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1840 static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc = {
1841 .bLength = sizeof(bot_bo_ep_comp_desc),
1842 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1845 static struct usb_endpoint_descriptor uasp_status_desc = {
1846 .bLength = USB_DT_ENDPOINT_SIZE,
1847 .bDescriptorType = USB_DT_ENDPOINT,
1848 .bEndpointAddress = USB_DIR_IN,
1849 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1850 .wMaxPacketSize = cpu_to_le16(512),
1853 static struct usb_endpoint_descriptor uasp_fs_status_desc = {
1854 .bLength = USB_DT_ENDPOINT_SIZE,
1855 .bDescriptorType = USB_DT_ENDPOINT,
1856 .bEndpointAddress = USB_DIR_IN,
1857 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1860 static struct usb_pipe_usage_descriptor uasp_status_pipe_desc = {
1861 .bLength = sizeof(uasp_status_pipe_desc),
1862 .bDescriptorType = USB_DT_PIPE_USAGE,
1863 .bPipeID = STATUS_PIPE_ID,
1866 static struct usb_endpoint_descriptor uasp_ss_status_desc = {
1867 .bLength = USB_DT_ENDPOINT_SIZE,
1868 .bDescriptorType = USB_DT_ENDPOINT,
1869 .bEndpointAddress = USB_DIR_IN,
1870 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1871 .wMaxPacketSize = cpu_to_le16(1024),
1874 static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc = {
1875 .bLength = sizeof(uasp_status_in_ep_comp_desc),
1876 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1877 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1880 static struct usb_endpoint_descriptor uasp_cmd_desc = {
1881 .bLength = USB_DT_ENDPOINT_SIZE,
1882 .bDescriptorType = USB_DT_ENDPOINT,
1883 .bEndpointAddress = USB_DIR_OUT,
1884 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1885 .wMaxPacketSize = cpu_to_le16(512),
1888 static struct usb_endpoint_descriptor uasp_fs_cmd_desc = {
1889 .bLength = USB_DT_ENDPOINT_SIZE,
1890 .bDescriptorType = USB_DT_ENDPOINT,
1891 .bEndpointAddress = USB_DIR_OUT,
1892 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1895 static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc = {
1896 .bLength = sizeof(uasp_cmd_pipe_desc),
1897 .bDescriptorType = USB_DT_PIPE_USAGE,
1898 .bPipeID = CMD_PIPE_ID,
1901 static struct usb_endpoint_descriptor uasp_ss_cmd_desc = {
1902 .bLength = USB_DT_ENDPOINT_SIZE,
1903 .bDescriptorType = USB_DT_ENDPOINT,
1904 .bEndpointAddress = USB_DIR_OUT,
1905 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1906 .wMaxPacketSize = cpu_to_le16(1024),
1909 static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc = {
1910 .bLength = sizeof(uasp_cmd_comp_desc),
1911 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1914 static struct usb_descriptor_header *uasp_fs_function_desc[] = {
1915 (struct usb_descriptor_header *) &bot_intf_desc,
1916 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
1917 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
1919 (struct usb_descriptor_header *) &uasp_intf_desc,
1920 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
1921 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
1922 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
1923 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
1924 (struct usb_descriptor_header *) &uasp_fs_status_desc,
1925 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
1926 (struct usb_descriptor_header *) &uasp_fs_cmd_desc,
1927 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
1928 NULL,
1931 static struct usb_descriptor_header *uasp_hs_function_desc[] = {
1932 (struct usb_descriptor_header *) &bot_intf_desc,
1933 (struct usb_descriptor_header *) &uasp_bi_desc,
1934 (struct usb_descriptor_header *) &uasp_bo_desc,
1936 (struct usb_descriptor_header *) &uasp_intf_desc,
1937 (struct usb_descriptor_header *) &uasp_bi_desc,
1938 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
1939 (struct usb_descriptor_header *) &uasp_bo_desc,
1940 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
1941 (struct usb_descriptor_header *) &uasp_status_desc,
1942 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
1943 (struct usb_descriptor_header *) &uasp_cmd_desc,
1944 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
1945 NULL,
1948 static struct usb_descriptor_header *uasp_ss_function_desc[] = {
1949 (struct usb_descriptor_header *) &bot_intf_desc,
1950 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
1951 (struct usb_descriptor_header *) &bot_bi_ep_comp_desc,
1952 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
1953 (struct usb_descriptor_header *) &bot_bo_ep_comp_desc,
1955 (struct usb_descriptor_header *) &uasp_intf_desc,
1956 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
1957 (struct usb_descriptor_header *) &uasp_bi_ep_comp_desc,
1958 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
1959 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
1960 (struct usb_descriptor_header *) &uasp_bo_ep_comp_desc,
1961 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
1962 (struct usb_descriptor_header *) &uasp_ss_status_desc,
1963 (struct usb_descriptor_header *) &uasp_status_in_ep_comp_desc,
1964 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
1965 (struct usb_descriptor_header *) &uasp_ss_cmd_desc,
1966 (struct usb_descriptor_header *) &uasp_cmd_comp_desc,
1967 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
1968 NULL,
1971 static struct usb_string tcm_us_strings[] = {
1972 [USB_G_STR_INT_UAS].s = "USB Attached SCSI",
1973 [USB_G_STR_INT_BBB].s = "Bulk Only Transport",
1974 { },
1977 static struct usb_gadget_strings tcm_stringtab = {
1978 .language = 0x0409,
1979 .strings = tcm_us_strings,
1982 static struct usb_gadget_strings *tcm_strings[] = {
1983 &tcm_stringtab,
1984 NULL,
1987 static int tcm_bind(struct usb_configuration *c, struct usb_function *f)
1989 struct f_uas *fu = to_f_uas(f);
1990 struct usb_string *us;
1991 struct usb_gadget *gadget = c->cdev->gadget;
1992 struct usb_ep *ep;
1993 struct f_tcm_opts *opts;
1994 int iface;
1995 int ret;
1997 opts = container_of(f->fi, struct f_tcm_opts, func_inst);
1999 mutex_lock(&opts->dep_lock);
2000 if (!opts->can_attach) {
2001 mutex_unlock(&opts->dep_lock);
2002 return -ENODEV;
2004 mutex_unlock(&opts->dep_lock);
2005 us = usb_gstrings_attach(c->cdev, tcm_strings,
2006 ARRAY_SIZE(tcm_us_strings));
2007 if (IS_ERR(us))
2008 return PTR_ERR(us);
2009 bot_intf_desc.iInterface = us[USB_G_STR_INT_BBB].id;
2010 uasp_intf_desc.iInterface = us[USB_G_STR_INT_UAS].id;
2012 iface = usb_interface_id(c, f);
2013 if (iface < 0)
2014 return iface;
2016 bot_intf_desc.bInterfaceNumber = iface;
2017 uasp_intf_desc.bInterfaceNumber = iface;
2018 fu->iface = iface;
2019 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bi_desc,
2020 &uasp_bi_ep_comp_desc);
2021 if (!ep)
2022 goto ep_fail;
2024 fu->ep_in = ep;
2026 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bo_desc,
2027 &uasp_bo_ep_comp_desc);
2028 if (!ep)
2029 goto ep_fail;
2030 fu->ep_out = ep;
2032 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_status_desc,
2033 &uasp_status_in_ep_comp_desc);
2034 if (!ep)
2035 goto ep_fail;
2036 fu->ep_status = ep;
2038 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_cmd_desc,
2039 &uasp_cmd_comp_desc);
2040 if (!ep)
2041 goto ep_fail;
2042 fu->ep_cmd = ep;
2044 /* Assume endpoint addresses are the same for both speeds */
2045 uasp_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2046 uasp_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2047 uasp_status_desc.bEndpointAddress =
2048 uasp_ss_status_desc.bEndpointAddress;
2049 uasp_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2051 uasp_fs_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2052 uasp_fs_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2053 uasp_fs_status_desc.bEndpointAddress =
2054 uasp_ss_status_desc.bEndpointAddress;
2055 uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2057 ret = usb_assign_descriptors(f, uasp_fs_function_desc,
2058 uasp_hs_function_desc, uasp_ss_function_desc, NULL);
2059 if (ret)
2060 goto ep_fail;
2062 return 0;
2063 ep_fail:
2064 pr_err("Can't claim all required eps\n");
2066 return -ENOTSUPP;
2069 struct guas_setup_wq {
2070 struct work_struct work;
2071 struct f_uas *fu;
2072 unsigned int alt;
2075 static void tcm_delayed_set_alt(struct work_struct *wq)
2077 struct guas_setup_wq *work = container_of(wq, struct guas_setup_wq,
2078 work);
2079 struct f_uas *fu = work->fu;
2080 int alt = work->alt;
2082 kfree(work);
2084 if (fu->flags & USBG_IS_BOT)
2085 bot_cleanup_old_alt(fu);
2086 if (fu->flags & USBG_IS_UAS)
2087 uasp_cleanup_old_alt(fu);
2089 if (alt == USB_G_ALT_INT_BBB)
2090 bot_set_alt(fu);
2091 else if (alt == USB_G_ALT_INT_UAS)
2092 uasp_set_alt(fu);
2093 usb_composite_setup_continue(fu->function.config->cdev);
2096 static int tcm_get_alt(struct usb_function *f, unsigned intf)
2098 if (intf == bot_intf_desc.bInterfaceNumber)
2099 return USB_G_ALT_INT_BBB;
2100 if (intf == uasp_intf_desc.bInterfaceNumber)
2101 return USB_G_ALT_INT_UAS;
2103 return -EOPNOTSUPP;
2106 static int tcm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2108 struct f_uas *fu = to_f_uas(f);
2110 if ((alt == USB_G_ALT_INT_BBB) || (alt == USB_G_ALT_INT_UAS)) {
2111 struct guas_setup_wq *work;
2113 work = kmalloc(sizeof(*work), GFP_ATOMIC);
2114 if (!work)
2115 return -ENOMEM;
2116 INIT_WORK(&work->work, tcm_delayed_set_alt);
2117 work->fu = fu;
2118 work->alt = alt;
2119 schedule_work(&work->work);
2120 return USB_GADGET_DELAYED_STATUS;
2122 return -EOPNOTSUPP;
2125 static void tcm_disable(struct usb_function *f)
2127 struct f_uas *fu = to_f_uas(f);
2129 if (fu->flags & USBG_IS_UAS)
2130 uasp_cleanup_old_alt(fu);
2131 else if (fu->flags & USBG_IS_BOT)
2132 bot_cleanup_old_alt(fu);
2133 fu->flags = 0;
2136 static int tcm_setup(struct usb_function *f,
2137 const struct usb_ctrlrequest *ctrl)
2139 struct f_uas *fu = to_f_uas(f);
2141 if (!(fu->flags & USBG_IS_BOT))
2142 return -EOPNOTSUPP;
2144 return usbg_bot_setup(f, ctrl);
2147 static inline struct f_tcm_opts *to_f_tcm_opts(struct config_item *item)
2149 return container_of(to_config_group(item), struct f_tcm_opts,
2150 func_inst.group);
2153 static void tcm_attr_release(struct config_item *item)
2155 struct f_tcm_opts *opts = to_f_tcm_opts(item);
2157 usb_put_function_instance(&opts->func_inst);
2160 static struct configfs_item_operations tcm_item_ops = {
2161 .release = tcm_attr_release,
2164 static const struct config_item_type tcm_func_type = {
2165 .ct_item_ops = &tcm_item_ops,
2166 .ct_owner = THIS_MODULE,
2169 static void tcm_free_inst(struct usb_function_instance *f)
2171 struct f_tcm_opts *opts;
2172 unsigned i;
2174 opts = container_of(f, struct f_tcm_opts, func_inst);
2176 mutex_lock(&tpg_instances_lock);
2177 for (i = 0; i < TPG_INSTANCES; ++i)
2178 if (tpg_instances[i].func_inst == f)
2179 break;
2180 if (i < TPG_INSTANCES)
2181 tpg_instances[i].func_inst = NULL;
2182 mutex_unlock(&tpg_instances_lock);
2184 kfree(opts);
2187 static int tcm_register_callback(struct usb_function_instance *f)
2189 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2191 mutex_lock(&opts->dep_lock);
2192 opts->can_attach = true;
2193 mutex_unlock(&opts->dep_lock);
2195 return 0;
2198 static void tcm_unregister_callback(struct usb_function_instance *f)
2200 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2202 mutex_lock(&opts->dep_lock);
2203 unregister_gadget_item(opts->
2204 func_inst.group.cg_item.ci_parent->ci_parent);
2205 opts->can_attach = false;
2206 mutex_unlock(&opts->dep_lock);
2209 static int usbg_attach(struct usbg_tpg *tpg)
2211 struct usb_function_instance *f = tpg->fi;
2212 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2214 if (opts->tcm_register_callback)
2215 return opts->tcm_register_callback(f);
2217 return 0;
2220 static void usbg_detach(struct usbg_tpg *tpg)
2222 struct usb_function_instance *f = tpg->fi;
2223 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2225 if (opts->tcm_unregister_callback)
2226 opts->tcm_unregister_callback(f);
2229 static int tcm_set_name(struct usb_function_instance *f, const char *name)
2231 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2233 pr_debug("tcm: Activating %s\n", name);
2235 mutex_lock(&opts->dep_lock);
2236 opts->ready = true;
2237 mutex_unlock(&opts->dep_lock);
2239 return 0;
2242 static struct usb_function_instance *tcm_alloc_inst(void)
2244 struct f_tcm_opts *opts;
2245 int i;
2248 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2249 if (!opts)
2250 return ERR_PTR(-ENOMEM);
2252 mutex_lock(&tpg_instances_lock);
2253 for (i = 0; i < TPG_INSTANCES; ++i)
2254 if (!tpg_instances[i].func_inst)
2255 break;
2257 if (i == TPG_INSTANCES) {
2258 mutex_unlock(&tpg_instances_lock);
2259 kfree(opts);
2260 return ERR_PTR(-EBUSY);
2262 tpg_instances[i].func_inst = &opts->func_inst;
2263 mutex_unlock(&tpg_instances_lock);
2265 mutex_init(&opts->dep_lock);
2266 opts->func_inst.set_inst_name = tcm_set_name;
2267 opts->func_inst.free_func_inst = tcm_free_inst;
2268 opts->tcm_register_callback = tcm_register_callback;
2269 opts->tcm_unregister_callback = tcm_unregister_callback;
2271 config_group_init_type_name(&opts->func_inst.group, "",
2272 &tcm_func_type);
2274 return &opts->func_inst;
2277 static void tcm_free(struct usb_function *f)
2279 struct f_uas *tcm = to_f_uas(f);
2281 kfree(tcm);
2284 static void tcm_unbind(struct usb_configuration *c, struct usb_function *f)
2286 usb_free_all_descriptors(f);
2289 static struct usb_function *tcm_alloc(struct usb_function_instance *fi)
2291 struct f_uas *fu;
2292 unsigned i;
2294 mutex_lock(&tpg_instances_lock);
2295 for (i = 0; i < TPG_INSTANCES; ++i)
2296 if (tpg_instances[i].func_inst == fi)
2297 break;
2298 if (i == TPG_INSTANCES) {
2299 mutex_unlock(&tpg_instances_lock);
2300 return ERR_PTR(-ENODEV);
2303 fu = kzalloc(sizeof(*fu), GFP_KERNEL);
2304 if (!fu) {
2305 mutex_unlock(&tpg_instances_lock);
2306 return ERR_PTR(-ENOMEM);
2309 fu->function.name = "Target Function";
2310 fu->function.bind = tcm_bind;
2311 fu->function.unbind = tcm_unbind;
2312 fu->function.set_alt = tcm_set_alt;
2313 fu->function.get_alt = tcm_get_alt;
2314 fu->function.setup = tcm_setup;
2315 fu->function.disable = tcm_disable;
2316 fu->function.free_func = tcm_free;
2317 fu->tpg = tpg_instances[i].tpg;
2318 mutex_unlock(&tpg_instances_lock);
2320 return &fu->function;
2323 DECLARE_USB_FUNCTION(tcm, tcm_alloc_inst, tcm_alloc);
2325 static int tcm_init(void)
2327 int ret;
2329 ret = usb_function_register(&tcmusb_func);
2330 if (ret)
2331 return ret;
2333 ret = target_register_template(&usbg_ops);
2334 if (ret)
2335 usb_function_unregister(&tcmusb_func);
2337 return ret;
2339 module_init(tcm_init);
2341 static void tcm_exit(void)
2343 target_unregister_template(&usbg_ops);
2344 usb_function_unregister(&tcmusb_func);
2346 module_exit(tcm_exit);
2348 MODULE_LICENSE("GPL");
2349 MODULE_AUTHOR("Sebastian Andrzej Siewior");