1
2
3
4
5
6
7
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>
23
24#include "tcm.h"
25#include "u_tcm.h"
26#include "configfs.h"
27
28#define TPG_INSTANCES 1
29
30struct tpg_instance {
31 struct usb_function_instance *func_inst;
32 struct usbg_tpg *tpg;
33};
34
35static struct tpg_instance tpg_instances[TPG_INSTANCES];
36
37static DEFINE_MUTEX(tpg_instances_lock);
38
39static inline struct f_uas *to_f_uas(struct usb_function *f)
40{
41 return container_of(f, struct f_uas, function);
42}
43
44
45
46static struct usbg_cdb *acquire_cmd_request(struct f_uas *fu);
47static void release_cmd_request(struct f_uas *fu, struct usb_request *req);
48static int bot_enqueue_cmd_cbw(struct f_uas *fu)
49{
50 int ret;
51 struct usbg_cdb *cmd = acquire_cmd_request(fu);
52
53 if (fu->flags & USBG_BOT_CMD_PEND)
54 return 0;
55
56 ret = usb_ep_queue(fu->ep_out, cmd->req, GFP_ATOMIC);
57 if (!ret)
58 fu->flags |= USBG_BOT_CMD_PEND;
59 return ret;
60}
61
62static void bot_status_complete(struct usb_ep *ep, struct usb_request *req)
63{
64 struct usbg_cmd *cmd = req->context;
65 struct f_uas *fu = cmd->fu;
66
67 release_cmd_request(fu, req);
68 transport_generic_free_cmd(&cmd->se_cmd, 0);
69 if (req->status < 0) {
70 pr_err("ERR %s(%d)\n", __func__, __LINE__);
71 return;
72 }
73
74
75 bot_enqueue_cmd_cbw(fu);
76}
77
78static void bot_enqueue_sense_code(struct f_uas *fu, struct usbg_cmd *cmd)
79{
80 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
81 int ret;
82 unsigned int csw_stat;
83
84 csw_stat = cmd->csw_code;
85 csw->Tag = cmd->bot_tag;
86 csw->Status = csw_stat;
87 fu->bot_status.req->context = cmd;
88 ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_ATOMIC);
89 if (ret)
90 pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
91}
92
93static void bot_err_compl(struct usb_ep *ep, struct usb_request *req)
94{
95 struct usbg_cmd *cmd = req->context;
96 struct f_uas *fu = cmd->fu;
97
98 if (req->status < 0)
99 pr_err("ERR %s(%d)\n", __func__, __LINE__);
100
101 if (cmd->data_len) {
102 if (cmd->data_len > ep->maxpacket) {
103 req->length = ep->maxpacket;
104 cmd->data_len -= ep->maxpacket;
105 } else {
106 req->length = cmd->data_len;
107 cmd->data_len = 0;
108 }
109
110 usb_ep_queue(ep, req, GFP_ATOMIC);
111 return;
112 }
113 bot_enqueue_sense_code(fu, cmd);
114}
115
116static void bot_send_bad_status(struct usbg_cmd *cmd)
117{
118 struct f_uas *fu = cmd->fu;
119 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
120 struct usb_request *req;
121 struct usb_ep *ep;
122
123 csw->Residue = cpu_to_le32(cmd->data_len);
124
125 if (cmd->data_len) {
126 if (cmd->is_read) {
127 ep = fu->ep_in;
128 req = fu->bot_req_in;
129 } else {
130 ep = fu->ep_out;
131 req = fu->bot_req_out;
132 }
133
134 if (cmd->data_len > fu->ep_in->maxpacket) {
135 req->length = ep->maxpacket;
136 cmd->data_len -= ep->maxpacket;
137 } else {
138 req->length = cmd->data_len;
139 cmd->data_len = 0;
140 }
141 req->complete = bot_err_compl;
142 req->context = cmd;
143 req->buf = fu->cmd[0]->buf;
144 usb_ep_queue(ep, req, GFP_KERNEL);
145 } else {
146 bot_enqueue_sense_code(fu, cmd);
147 }
148}
149
150static int bot_send_status(struct usbg_cmd *cmd, bool moved_data)
151{
152 struct f_uas *fu = cmd->fu;
153 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
154 int ret;
155
156 if (cmd->se_cmd.scsi_status == SAM_STAT_GOOD) {
157 if (!moved_data && cmd->data_len) {
158
159
160
161
162 cmd->csw_code = US_BULK_STAT_OK;
163 bot_send_bad_status(cmd);
164 return 0;
165 }
166
167 csw->Tag = cmd->bot_tag;
168 csw->Residue = cpu_to_le32(0);
169 csw->Status = US_BULK_STAT_OK;
170 fu->bot_status.req->context = cmd;
171
172 ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_KERNEL);
173 if (ret)
174 pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
175 } else {
176 cmd->csw_code = US_BULK_STAT_FAIL;
177 bot_send_bad_status(cmd);
178 }
179 return 0;
180}
181
182
183
184
185
186static int bot_send_status_response(struct usbg_cmd *cmd)
187{
188 bool moved_data = false;
189
190 if (!cmd->is_read)
191 moved_data = true;
192 return bot_send_status(cmd, moved_data);
193}
194
195
196static void bot_read_compl(struct usb_ep *ep, struct usb_request *req)
197{
198 struct usbg_cmd *cmd = req->context;
199
200 if (req->status < 0)
201 pr_err("ERR %s(%d)\n", __func__, __LINE__);
202
203 bot_send_status(cmd, true);
204}
205
206static int bot_send_read_response(struct usbg_cmd *cmd)
207{
208 struct f_uas *fu = cmd->fu;
209 struct se_cmd *se_cmd = &cmd->se_cmd;
210 struct usb_gadget *gadget = fuas_to_gadget(fu);
211 int ret;
212
213 if (!cmd->data_len) {
214 cmd->csw_code = US_BULK_STAT_PHASE;
215 bot_send_bad_status(cmd);
216 return 0;
217 }
218
219 if (!gadget->sg_supported) {
220 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
221 if (!cmd->data_buf)
222 return -ENOMEM;
223
224 sg_copy_to_buffer(se_cmd->t_data_sg,
225 se_cmd->t_data_nents,
226 cmd->data_buf,
227 se_cmd->data_length);
228
229 fu->bot_req_in->buf = cmd->data_buf;
230 } else {
231 fu->bot_req_in->buf = NULL;
232 fu->bot_req_in->num_sgs = se_cmd->t_data_nents;
233 fu->bot_req_in->sg = se_cmd->t_data_sg;
234 }
235
236 fu->bot_req_in->complete = bot_read_compl;
237 fu->bot_req_in->length = se_cmd->data_length;
238 fu->bot_req_in->context = cmd;
239 ret = usb_ep_queue(fu->ep_in, fu->bot_req_in, GFP_ATOMIC);
240 if (ret)
241 pr_err("%s(%d)\n", __func__, __LINE__);
242 return 0;
243}
244
245static void usbg_data_write_cmpl(struct usb_ep *, struct usb_request *);
246static int usbg_prepare_w_request(struct usbg_cmd *, struct usb_request *);
247
248static int bot_send_write_request(struct usbg_cmd *cmd)
249{
250 struct f_uas *fu = cmd->fu;
251 struct se_cmd *se_cmd = &cmd->se_cmd;
252 int ret;
253
254 init_completion(&cmd->write_complete);
255 cmd->fu = fu;
256
257 if (!cmd->data_len) {
258 cmd->csw_code = US_BULK_STAT_PHASE;
259 return -EINVAL;
260 }
261
262 ret = usbg_prepare_w_request(cmd, fu->bot_req_out);
263 if (ret)
264 goto cleanup;
265 ret = usb_ep_queue(fu->ep_out, fu->bot_req_out, GFP_KERNEL);
266 if (ret)
267 pr_err("%s(%d)\n", __func__, __LINE__);
268
269 wait_for_completion(&cmd->write_complete);
270 target_execute_cmd(se_cmd);
271cleanup:
272 return ret;
273}
274
275static int bot_submit_command(struct f_uas *, void *, unsigned int);
276
277static void bot_cmd_complete(struct usb_ep *ep, struct usb_request *req)
278{
279 struct f_uas *fu = req->context;
280 int ret;
281
282 fu->flags &= ~USBG_BOT_CMD_PEND;
283
284 if (req->status < 0)
285 return;
286
287 release_cmd_request(fu, req);
288 ret = bot_submit_command(fu, req->buf, req->actual);
289 if (ret)
290 pr_err("%s(%d): %d\n", __func__, __LINE__, ret);
291}
292
293static struct usbg_cdb *acquire_cmd_request(struct f_uas *fu)
294{
295 int i;
296
297 for (i = 0; i < fu->ncmd; i++) {
298 if (!fu->cmd[i]->claimed) {
299 fu->cmd[i]->claimed = true;
300 return fu->cmd[i];
301 }
302 }
303 return NULL;
304}
305
306static void release_cmd_request(struct f_uas *fu, struct usb_request *req)
307{
308 int i;
309
310 for (i = 0; i < fu->ncmd; i++) {
311 if (fu->cmd[i]->req == req)
312 fu->cmd[i]->claimed = false;
313 }
314}
315
316static void free_cmd_resource(struct f_uas *fu, struct usb_ep *ep)
317{
318 int i;
319
320 for (i = 0; i < fu->ncmd; i++) {
321 if (fu->cmd[i]->req)
322 usb_ep_free_request(ep, fu->cmd[i]->req);
323
324 kfree(fu->cmd[i]->buf);
325 fu->cmd[i]->buf = NULL;
326
327 kfree(fu->cmd[i]);
328 fu->cmd[i] = NULL;
329 }
330}
331
332static int alloc_cmd_resource(struct f_uas *fu, int num, struct usb_ep *ep,
333 void (*complete)(struct usb_ep *ep,
334 struct usb_request *req))
335{
336 int i;
337
338 fu->ncmd = num;
339 for (i = 0; i < fu->ncmd; i++) {
340 fu->cmd[i] = kcalloc(fu->ncmd, sizeof(struct usbg_cdb),
341 GFP_KERNEL);
342 if (!fu->cmd)
343 goto err_cmd;
344
345 fu->cmd[i]->req = usb_ep_alloc_request(ep, GFP_KERNEL);
346 if (!fu->cmd[i]->req)
347 goto err_cmd;
348
349 fu->cmd[i]->buf = kmalloc(fu->ep_out->maxpacket, GFP_KERNEL);
350 if (!fu->cmd[i]->buf)
351 goto err_cmd;
352
353 fu->cmd[i]->req->complete = complete;
354 fu->cmd[i]->req->buf = fu->cmd[i]->buf;
355 fu->cmd[i]->req->length = fu->ep_out->maxpacket;
356 fu->cmd[i]->req->context = fu;
357 }
358
359 return 0;
360err_cmd:
361 free_cmd_resource(fu, ep);
362 return -ENOMEM;
363}
364
365static int bot_prepare_reqs(struct f_uas *fu)
366{
367 int ret;
368
369 fu->bot_req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
370 if (!fu->bot_req_in)
371 goto err;
372
373 fu->bot_req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
374 if (!fu->bot_req_out)
375 goto err_out;
376
377 fu->bot_status.req = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
378 if (!fu->bot_status.req)
379 goto err_sts;
380
381 fu->bot_status.req->buf = &fu->bot_status.csw;
382 fu->bot_status.req->length = US_BULK_CS_WRAP_LEN;
383 fu->bot_status.req->complete = bot_status_complete;
384 fu->bot_status.csw.Signature = cpu_to_le32(US_BULK_CS_SIGN);
385
386 ret = alloc_cmd_resource(fu, BOT_MAX_COMMANDS, fu->ep_out,
387 bot_cmd_complete);
388 if (ret)
389 goto err_cmd;
390
391 ret = bot_enqueue_cmd_cbw(fu);
392 if (ret)
393 goto err_queue;
394 return 0;
395err_queue:
396 free_cmd_resource(fu, fu->ep_out);
397err_cmd:
398 usb_ep_free_request(fu->ep_in, fu->bot_status.req);
399err_sts:
400 usb_ep_free_request(fu->ep_out, fu->bot_req_out);
401 fu->bot_req_out = NULL;
402err_out:
403 usb_ep_free_request(fu->ep_in, fu->bot_req_in);
404 fu->bot_req_in = NULL;
405err:
406 pr_err("BOT: endpoint setup failed\n");
407 return -ENOMEM;
408}
409
410static void bot_cleanup_old_alt(struct f_uas *fu)
411{
412 if (!(fu->flags & USBG_ENABLED))
413 return;
414
415 usb_ep_disable(fu->ep_in);
416 usb_ep_disable(fu->ep_out);
417
418 if (!fu->bot_req_in)
419 return;
420
421 usb_ep_free_request(fu->ep_in, fu->bot_req_in);
422 usb_ep_free_request(fu->ep_out, fu->bot_req_out);
423 usb_ep_free_request(fu->ep_out, fu->bot_status.req);
424
425 free_cmd_resource(fu, fu->ep_out);
426
427 fu->bot_req_in = NULL;
428 fu->bot_req_out = NULL;
429 fu->bot_status.req = NULL;
430}
431
432static void bot_set_alt(struct f_uas *fu)
433{
434 struct usb_function *f = &fu->function;
435 struct usb_gadget *gadget = f->config->cdev->gadget;
436 int ret;
437
438 fu->flags = USBG_IS_BOT;
439
440 config_ep_by_speed(gadget, f, fu->ep_in);
441 ret = usb_ep_enable(fu->ep_in);
442 if (ret)
443 goto err_b_in;
444
445 config_ep_by_speed(gadget, f, fu->ep_out);
446 ret = usb_ep_enable(fu->ep_out);
447 if (ret)
448 goto err_b_out;
449
450 ret = bot_prepare_reqs(fu);
451 if (ret)
452 goto err_wq;
453 fu->flags |= USBG_ENABLED;
454 pr_info("Using the BOT protocol\n");
455 return;
456err_wq:
457 usb_ep_disable(fu->ep_out);
458err_b_out:
459 usb_ep_disable(fu->ep_in);
460err_b_in:
461 fu->flags = USBG_IS_BOT;
462}
463
464static int usbg_bot_setup(struct usb_function *f,
465 const struct usb_ctrlrequest *ctrl)
466{
467 struct f_uas *fu = to_f_uas(f);
468 struct usb_composite_dev *cdev = f->config->cdev;
469 u16 w_value = le16_to_cpu(ctrl->wValue);
470 u16 w_length = le16_to_cpu(ctrl->wLength);
471 int luns;
472 u8 *ret_lun;
473
474 switch (ctrl->bRequest) {
475 case US_BULK_GET_MAX_LUN:
476 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_CLASS |
477 USB_RECIP_INTERFACE))
478 return -ENOTSUPP;
479
480 if (w_length < 1)
481 return -EINVAL;
482 if (w_value != 0)
483 return -EINVAL;
484 luns = atomic_read(&fu->tpg->tpg_port_count);
485 if (!luns) {
486 pr_err("No LUNs configured?\n");
487 return -EINVAL;
488 }
489
490
491
492
493 luns--;
494 if (luns > 0xf) {
495 pr_info_once("Limiting the number of luns to 16\n");
496 luns = 0xf;
497 }
498 ret_lun = cdev->req->buf;
499 *ret_lun = luns;
500 cdev->req->length = 1;
501 return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
502
503 case US_BULK_RESET_REQUEST:
504
505 bot_enqueue_cmd_cbw(fu);
506 return 0;
507 }
508 return -ENOTSUPP;
509}
510
511
512
513static void uasp_cleanup_one_stream(struct f_uas *fu, struct uas_stream *stream)
514{
515
516 if (!stream->req_in)
517 return;
518
519 usb_ep_free_request(fu->ep_in, stream->req_in);
520 usb_ep_free_request(fu->ep_out, stream->req_out);
521 usb_ep_free_request(fu->ep_status, stream->req_status);
522
523 stream->req_in = NULL;
524 stream->req_out = NULL;
525 stream->req_status = NULL;
526}
527
528static void uasp_cleanup_old_alt(struct f_uas *fu)
529{
530 int i;
531
532 if (!(fu->flags & USBG_ENABLED))
533 return;
534
535 usb_ep_disable(fu->ep_in);
536 usb_ep_disable(fu->ep_out);
537 usb_ep_disable(fu->ep_status);
538 usb_ep_disable(fu->ep_cmd);
539
540 for (i = 0; i < UASP_SS_EP_COMP_NUM_STREAMS; i++)
541 uasp_cleanup_one_stream(fu, &fu->stream[i]);
542 free_cmd_resource(fu, fu->ep_cmd);
543}
544
545static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req);
546
547static int uasp_prepare_r_request(struct usbg_cmd *cmd)
548{
549 struct se_cmd *se_cmd = &cmd->se_cmd;
550 struct f_uas *fu = cmd->fu;
551 struct usb_gadget *gadget = fuas_to_gadget(fu);
552 struct uas_stream *stream = cmd->stream;
553
554 if (!gadget->sg_supported) {
555 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
556 if (!cmd->data_buf)
557 return -ENOMEM;
558
559 sg_copy_to_buffer(se_cmd->t_data_sg,
560 se_cmd->t_data_nents,
561 cmd->data_buf,
562 se_cmd->data_length);
563
564 stream->req_in->buf = cmd->data_buf;
565 } else {
566 stream->req_in->buf = NULL;
567 stream->req_in->num_sgs = se_cmd->t_data_nents;
568 stream->req_in->sg = se_cmd->t_data_sg;
569 }
570
571 stream->req_in->complete = uasp_status_data_cmpl;
572 stream->req_in->length = se_cmd->data_length;
573 stream->req_in->context = cmd;
574
575 cmd->state = UASP_SEND_STATUS;
576 return 0;
577}
578
579static void uasp_prepare_status(struct usbg_cmd *cmd)
580{
581 struct se_cmd *se_cmd = &cmd->se_cmd;
582 struct sense_iu *iu = &cmd->sense_iu;
583 struct uas_stream *stream = cmd->stream;
584
585 cmd->state = UASP_QUEUE_COMMAND;
586 iu->iu_id = IU_ID_STATUS;
587 iu->tag = cpu_to_be16(cmd->tag);
588
589
590
591
592 iu->len = cpu_to_be16(se_cmd->scsi_sense_length);
593 iu->status = se_cmd->scsi_status;
594 stream->req_status->context = cmd;
595 stream->req_status->length = se_cmd->scsi_sense_length + 16;
596 stream->req_status->buf = iu;
597 stream->req_status->complete = uasp_status_data_cmpl;
598}
599
600static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req)
601{
602 struct usbg_cmd *cmd = req->context;
603 struct uas_stream *stream = cmd->stream;
604 struct f_uas *fu = cmd->fu;
605 struct usbg_cdb *cmd_cdb;
606 int ret;
607
608 if (req->status < 0)
609 goto cleanup;
610
611 switch (cmd->state) {
612 case UASP_SEND_DATA:
613 ret = uasp_prepare_r_request(cmd);
614 if (ret)
615 goto cleanup;
616 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
617 if (ret)
618 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
619 break;
620
621 case UASP_RECEIVE_DATA:
622 ret = usbg_prepare_w_request(cmd, stream->req_out);
623 if (ret)
624 goto cleanup;
625 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
626 if (ret)
627 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
628 break;
629
630 case UASP_SEND_STATUS:
631 uasp_prepare_status(cmd);
632 ret = usb_ep_queue(fu->ep_status, stream->req_status,
633 GFP_ATOMIC);
634 if (ret)
635 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
636 break;
637
638 case UASP_QUEUE_COMMAND:
639 transport_generic_free_cmd(&cmd->se_cmd, 0);
640 cmd_cdb = acquire_cmd_request(fu);
641 usb_ep_queue(fu->ep_cmd, cmd_cdb->req, GFP_ATOMIC);
642 break;
643
644 default:
645 BUG();
646 }
647 return;
648
649cleanup:
650 transport_generic_free_cmd(&cmd->se_cmd, 0);
651}
652
653static int uasp_send_status_response(struct usbg_cmd *cmd)
654{
655 struct f_uas *fu = cmd->fu;
656 struct uas_stream *stream = cmd->stream;
657 struct sense_iu *iu = &cmd->sense_iu;
658
659 iu->tag = cpu_to_be16(cmd->tag);
660 stream->req_status->complete = uasp_status_data_cmpl;
661 stream->req_status->context = cmd;
662 cmd->fu = fu;
663 uasp_prepare_status(cmd);
664 return usb_ep_queue(fu->ep_status, stream->req_status, GFP_ATOMIC);
665}
666
667static int uasp_send_read_response(struct usbg_cmd *cmd)
668{
669 struct f_uas *fu = cmd->fu;
670 struct uas_stream *stream = cmd->stream;
671 struct sense_iu *iu = &cmd->sense_iu;
672 int ret;
673
674 cmd->fu = fu;
675
676 iu->tag = cpu_to_be16(cmd->tag);
677 if (fu->flags & USBG_USE_STREAMS) {
678
679 ret = uasp_prepare_r_request(cmd);
680 if (ret)
681 goto out;
682 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
683 if (ret) {
684 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
685 kfree(cmd->data_buf);
686 cmd->data_buf = NULL;
687 }
688
689 } else {
690
691 iu->iu_id = IU_ID_READ_READY;
692 iu->tag = cpu_to_be16(cmd->tag);
693
694 stream->req_status->complete = uasp_status_data_cmpl;
695 stream->req_status->context = cmd;
696
697 cmd->state = UASP_SEND_DATA;
698 stream->req_status->buf = iu;
699 stream->req_status->length = sizeof(struct iu);
700
701 ret = usb_ep_queue(fu->ep_status, stream->req_status,
702 GFP_ATOMIC);
703 if (ret)
704 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
705 }
706out:
707 return ret;
708}
709
710static int uasp_send_write_request(struct usbg_cmd *cmd)
711{
712 struct f_uas *fu = cmd->fu;
713 struct se_cmd *se_cmd = &cmd->se_cmd;
714 struct uas_stream *stream = cmd->stream;
715 struct sense_iu *iu = &cmd->sense_iu;
716 int ret;
717
718 init_completion(&cmd->write_complete);
719 cmd->fu = fu;
720
721 iu->tag = cpu_to_be16(cmd->tag);
722
723 if (fu->flags & USBG_USE_STREAMS) {
724
725 ret = usbg_prepare_w_request(cmd, stream->req_out);
726 if (ret)
727 goto cleanup;
728 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
729 if (ret)
730 pr_err("%s(%d)\n", __func__, __LINE__);
731
732 } else {
733
734 iu->iu_id = IU_ID_WRITE_READY;
735 iu->tag = cpu_to_be16(cmd->tag);
736
737 stream->req_status->complete = uasp_status_data_cmpl;
738 stream->req_status->context = cmd;
739
740 cmd->state = UASP_RECEIVE_DATA;
741 stream->req_status->buf = iu;
742 stream->req_status->length = sizeof(struct iu);
743
744 ret = usb_ep_queue(fu->ep_status, stream->req_status,
745 GFP_ATOMIC);
746 if (ret)
747 pr_err("%s(%d)\n", __func__, __LINE__);
748 }
749
750 wait_for_completion(&cmd->write_complete);
751 target_execute_cmd(se_cmd);
752cleanup:
753 return ret;
754}
755
756static int usbg_submit_command(struct f_uas *, void *, unsigned int);
757
758static void uasp_cmd_complete(struct usb_ep *ep, struct usb_request *req)
759{
760 struct f_uas *fu = req->context;
761 struct usbg_cdb *cmd;
762 int ret;
763
764 if (req->status < 0)
765 return;
766
767 release_cmd_request(fu, req);
768 ret = usbg_submit_command(fu, req->buf, req->actual);
769
770
771
772
773
774
775 if (!ret)
776 return;
777 cmd = acquire_cmd_request(fu);
778 usb_ep_queue(fu->ep_cmd, cmd->req, GFP_ATOMIC);
779}
780
781static int uasp_alloc_stream_res(struct f_uas *fu, struct uas_stream *stream)
782{
783 stream->req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
784 if (!stream->req_in)
785 goto out;
786
787 stream->req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
788 if (!stream->req_out)
789 goto err_out;
790
791 stream->req_status = usb_ep_alloc_request(fu->ep_status, GFP_KERNEL);
792 if (!stream->req_status)
793 goto err_sts;
794
795 return 0;
796err_sts:
797 usb_ep_free_request(fu->ep_status, stream->req_status);
798 stream->req_status = NULL;
799err_out:
800 usb_ep_free_request(fu->ep_out, stream->req_out);
801 stream->req_out = NULL;
802out:
803 return -ENOMEM;
804}
805
806static void uasp_setup_stream_res(struct f_uas *fu, int max_streams)
807{
808 int i;
809
810 for (i = 0; i < max_streams; i++) {
811 struct uas_stream *s = &fu->stream[i];
812
813 s->req_in->stream_id = i + 1;
814 s->req_out->stream_id = i + 1;
815 s->req_status->stream_id = i + 1;
816 }
817}
818
819static int uasp_prepare_reqs(struct f_uas *fu)
820{
821 int ret;
822 int i;
823 int max_streams, max_commands;
824
825 if (fu->flags & USBG_USE_STREAMS) {
826 max_commands = UASP_MAX_COMMANDS;
827 max_streams = UASP_SS_EP_COMP_NUM_STREAMS;
828 } else {
829 max_commands = 1;
830 max_streams = 1;
831 }
832
833 for (i = 0; i < max_streams; i++) {
834 ret = uasp_alloc_stream_res(fu, &fu->stream[i]);
835 if (ret)
836 goto err_cleanup;
837 }
838
839 ret = alloc_cmd_resource(fu, max_commands, fu->ep_cmd,
840 uasp_cmd_complete);
841 if (ret)
842 goto err_free_stream;
843 uasp_setup_stream_res(fu, max_streams);
844
845
846 for (i = 0; i < fu->ncmd; i++) {
847 struct usbg_cdb *cmd = acquire_cmd_request(fu);
848
849 ret = usb_ep_queue(fu->ep_cmd, cmd->req, GFP_ATOMIC);
850 if (ret)
851 goto err_free_stream;
852 }
853
854 return 0;
855
856err_free_stream:
857 free_cmd_resource(fu, fu->ep_cmd);
858
859err_cleanup:
860 if (i) {
861 do {
862 uasp_cleanup_one_stream(fu, &fu->stream[i - 1]);
863 i--;
864 } while (i);
865 }
866 pr_err("UASP: endpoint setup failed\n");
867 return ret;
868}
869
870#define SS_BOT_INTERFACE_DESC_NO 5
871static void uasp_set_alt(struct f_uas *fu)
872{
873 struct usb_function *f = &fu->function;
874 struct usb_gadget *gadget = f->config->cdev->gadget;
875 struct usb_descriptor_header **ss_uasp_backup = f->ss_descriptors;
876 int ret;
877
878 fu->flags = USBG_IS_UAS;
879
880 if (gadget->speed == USB_SPEED_SUPER) {
881 fu->flags |= USBG_USE_STREAMS;
882
883
884
885
886
887
888
889
890 f->ss_descriptors += SS_BOT_INTERFACE_DESC_NO;
891 }
892
893 config_ep_by_speed(gadget, f, fu->ep_in);
894 ret = usb_ep_enable(fu->ep_in);
895 if (ret)
896 goto err_b_in;
897
898 config_ep_by_speed(gadget, f, fu->ep_out);
899 ret = usb_ep_enable(fu->ep_out);
900 if (ret)
901 goto err_b_out;
902
903 config_ep_by_speed(gadget, f, fu->ep_cmd);
904 ret = usb_ep_enable(fu->ep_cmd);
905 if (ret)
906 goto err_cmd;
907 config_ep_by_speed(gadget, f, fu->ep_status);
908 ret = usb_ep_enable(fu->ep_status);
909 if (ret)
910 goto err_status;
911
912 ret = uasp_prepare_reqs(fu);
913 if (ret)
914 goto err_wq;
915 fu->flags |= USBG_ENABLED;
916
917
918 if (gadget->speed == USB_SPEED_SUPER)
919 f->ss_descriptors = ss_uasp_backup;
920
921 pr_info("Using the UAS protocol\n");
922 return;
923err_wq:
924 usb_ep_disable(fu->ep_status);
925err_status:
926 usb_ep_disable(fu->ep_cmd);
927err_cmd:
928 usb_ep_disable(fu->ep_out);
929err_b_out:
930 usb_ep_disable(fu->ep_in);
931err_b_in:
932
933 if (gadget->speed == USB_SPEED_SUPER)
934 f->ss_descriptors = ss_uasp_backup;
935 fu->flags = 0;
936}
937
938static int get_cmd_dir(const unsigned char *cdb)
939{
940 int ret;
941
942 switch (cdb[0]) {
943 case READ_6:
944 case READ_10:
945 case READ_12:
946 case READ_16:
947 case INQUIRY:
948 case MODE_SENSE:
949 case MODE_SENSE_10:
950 case SERVICE_ACTION_IN_16:
951 case MAINTENANCE_IN:
952 case PERSISTENT_RESERVE_IN:
953 case SECURITY_PROTOCOL_IN:
954 case ACCESS_CONTROL_IN:
955 case REPORT_LUNS:
956 case READ_BLOCK_LIMITS:
957 case READ_POSITION:
958 case READ_CAPACITY:
959 case READ_TOC:
960 case READ_FORMAT_CAPACITIES:
961 case REQUEST_SENSE:
962 ret = DMA_FROM_DEVICE;
963 break;
964
965 case WRITE_6:
966 case WRITE_10:
967 case WRITE_12:
968 case WRITE_16:
969 case MODE_SELECT:
970 case MODE_SELECT_10:
971 case WRITE_VERIFY:
972 case WRITE_VERIFY_12:
973 case PERSISTENT_RESERVE_OUT:
974 case MAINTENANCE_OUT:
975 case SECURITY_PROTOCOL_OUT:
976 case ACCESS_CONTROL_OUT:
977 ret = DMA_TO_DEVICE;
978 break;
979 case ALLOW_MEDIUM_REMOVAL:
980 case TEST_UNIT_READY:
981 case SYNCHRONIZE_CACHE:
982 case START_STOP:
983 case ERASE:
984 case REZERO_UNIT:
985 case SEEK_10:
986 case SPACE:
987 case VERIFY:
988 case WRITE_FILEMARKS:
989 ret = DMA_NONE;
990 break;
991 default:
992#define CMD_DIR_MSG "target: Unknown data direction for SCSI Opcode 0x%02x\n"
993 pr_warn(CMD_DIR_MSG, cdb[0]);
994#undef CMD_DIR_MSG
995 ret = -EINVAL;
996 }
997 return ret;
998}
999
1000static void recover_w_length_with_maxpacket(struct usbg_cmd *cmd,
1001 struct usb_request *req)
1002{
1003 struct se_cmd *se_cmd = &cmd->se_cmd;
1004 struct f_uas *fu = cmd->fu;
1005 struct usb_gadget *gadget = fuas_to_gadget(fu);
1006 int rem;
1007
1008 rem = se_cmd->data_length % fu->ep_out->maxpacket;
1009 if (rem) {
1010
1011 cmd->data_len -= fu->ep_out->maxpacket - rem;
1012
1013 if (gadget->sg_supported) {
1014 struct scatterlist *s = sg_last(se_cmd->t_data_sg,
1015 se_cmd->t_data_nents);
1016
1017 s->length -= fu->ep_out->maxpacket - rem;
1018 }
1019 }
1020}
1021
1022static void adjust_w_length_with_maxpacket(struct usbg_cmd *cmd,
1023 struct usb_request *req)
1024{
1025 struct se_cmd *se_cmd = &cmd->se_cmd;
1026 struct f_uas *fu = cmd->fu;
1027 struct usb_gadget *gadget = fuas_to_gadget(fu);
1028 int rem;
1029
1030 cmd->data_len = se_cmd->data_length;
1031 rem = cmd->data_len % fu->ep_out->maxpacket;
1032 if (rem) {
1033
1034
1035
1036 cmd->data_len += fu->ep_out->maxpacket - rem;
1037
1038 if (gadget->sg_supported) {
1039
1040
1041
1042 struct scatterlist *s = sg_last(se_cmd->t_data_sg,
1043 se_cmd->t_data_nents);
1044
1045 s->length += fu->ep_out->maxpacket - rem;
1046 }
1047 }
1048}
1049
1050static void usbg_data_write_cmpl(struct usb_ep *ep, struct usb_request *req)
1051{
1052 struct usbg_cmd *cmd = req->context;
1053 struct se_cmd *se_cmd = &cmd->se_cmd;
1054
1055 if (req->status < 0) {
1056 pr_err("%s() state %d transfer failed\n", __func__, cmd->state);
1057 goto cleanup;
1058 }
1059
1060 recover_w_length_with_maxpacket(cmd, req);
1061
1062 if (req->num_sgs == 0) {
1063 sg_copy_from_buffer(se_cmd->t_data_sg,
1064 se_cmd->t_data_nents,
1065 cmd->data_buf,
1066 se_cmd->data_length);
1067 }
1068
1069 complete(&cmd->write_complete);
1070 return;
1071
1072cleanup:
1073 transport_generic_free_cmd(&cmd->se_cmd, 0);
1074}
1075
1076static int usbg_prepare_w_request(struct usbg_cmd *cmd, struct usb_request *req)
1077{
1078 struct se_cmd *se_cmd = &cmd->se_cmd;
1079 struct f_uas *fu = cmd->fu;
1080 struct usb_gadget *gadget = fuas_to_gadget(fu);
1081
1082 adjust_w_length_with_maxpacket(cmd, req);
1083
1084 if (!gadget->sg_supported) {
1085 cmd->data_buf = kmalloc(cmd->data_len, GFP_ATOMIC);
1086 if (!cmd->data_buf)
1087 return -ENOMEM;
1088
1089 req->buf = cmd->data_buf;
1090 } else {
1091 req->buf = NULL;
1092 req->num_sgs = se_cmd->t_data_nents;
1093 req->sg = se_cmd->t_data_sg;
1094 }
1095
1096 req->complete = usbg_data_write_cmpl;
1097 req->length = cmd->data_len;
1098 req->context = cmd;
1099 return 0;
1100}
1101
1102static int usbg_send_status_response(struct se_cmd *se_cmd)
1103{
1104 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1105 se_cmd);
1106 struct f_uas *fu = cmd->fu;
1107
1108 if (fu->flags & USBG_IS_BOT)
1109 return bot_send_status_response(cmd);
1110 else
1111 return uasp_send_status_response(cmd);
1112}
1113
1114static int usbg_send_write_request(struct se_cmd *se_cmd)
1115{
1116 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1117 se_cmd);
1118 struct f_uas *fu = cmd->fu;
1119
1120 if (fu->flags & USBG_IS_BOT)
1121 return bot_send_write_request(cmd);
1122 else
1123 return uasp_send_write_request(cmd);
1124}
1125
1126static int usbg_send_read_response(struct se_cmd *se_cmd)
1127{
1128 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1129 se_cmd);
1130 struct f_uas *fu = cmd->fu;
1131
1132 if (fu->flags & USBG_IS_BOT)
1133 return bot_send_read_response(cmd);
1134 else
1135 return uasp_send_read_response(cmd);
1136}
1137
1138static void usbg_cmd_work(struct work_struct *work)
1139{
1140 struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
1141 struct se_cmd *se_cmd;
1142 struct tcm_usbg_nexus *tv_nexus;
1143 struct usbg_tpg *tpg;
1144 int dir, flags = (TARGET_SCF_UNKNOWN_SIZE | TARGET_SCF_ACK_KREF);
1145
1146 se_cmd = &cmd->se_cmd;
1147 tpg = cmd->fu->tpg;
1148 tv_nexus = tpg->tpg_nexus;
1149 dir = get_cmd_dir(cmd->cmd_buf);
1150 if (dir < 0) {
1151 transport_init_se_cmd(se_cmd,
1152 tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
1153 tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
1154 cmd->prio_attr, cmd->sense_iu.sense);
1155 goto out;
1156 }
1157
1158 if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess, cmd->cmd_buf,
1159 cmd->sense_iu.sense, cmd->unpacked_lun, 0,
1160 cmd->prio_attr, dir, flags) < 0)
1161 goto out;
1162
1163 return;
1164
1165out:
1166 transport_send_check_condition_and_sense(se_cmd,
1167 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1168 transport_generic_free_cmd(&cmd->se_cmd, 0);
1169}
1170
1171static struct usbg_cmd *usbg_get_cmd(struct f_uas *fu,
1172 struct tcm_usbg_nexus *tv_nexus, u32 scsi_tag)
1173{
1174 struct se_session *se_sess = tv_nexus->tvn_se_sess;
1175 struct usbg_cmd *cmd;
1176 int tag;
1177
1178 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, GFP_ATOMIC);
1179 if (tag < 0)
1180 return ERR_PTR(-ENOMEM);
1181
1182 cmd = &((struct usbg_cmd *)se_sess->sess_cmd_map)[tag];
1183 memset(cmd, 0, sizeof(*cmd));
1184 cmd->se_cmd.map_tag = tag;
1185 cmd->se_cmd.tag = cmd->tag = scsi_tag;
1186 cmd->fu = fu;
1187
1188 return cmd;
1189}
1190
1191static void usbg_release_cmd(struct se_cmd *);
1192
1193static int usbg_submit_command(struct f_uas *fu,
1194 void *cmdbuf, unsigned int len)
1195{
1196 struct command_iu *cmd_iu = cmdbuf;
1197 struct usbg_cmd *cmd;
1198 struct usbg_tpg *tpg = fu->tpg;
1199 struct tcm_usbg_nexus *tv_nexus = tpg->tpg_nexus;
1200 u32 cmd_len;
1201 u16 scsi_tag;
1202
1203 if (cmd_iu->iu_id != IU_ID_COMMAND) {
1204 pr_err("Unsupported type %d\n", cmd_iu->iu_id);
1205 return -EINVAL;
1206 }
1207
1208 tv_nexus = tpg->tpg_nexus;
1209 if (!tv_nexus) {
1210 pr_err("Missing nexus, ignoring command\n");
1211 return -EINVAL;
1212 }
1213
1214 cmd_len = (cmd_iu->len & ~0x3) + 16;
1215 if (cmd_len > USBG_MAX_CMD)
1216 return -EINVAL;
1217
1218 scsi_tag = be16_to_cpup(&cmd_iu->tag);
1219 cmd = usbg_get_cmd(fu, tv_nexus, scsi_tag);
1220 if (IS_ERR(cmd)) {
1221 pr_err("usbg_get_cmd failed\n");
1222 return -ENOMEM;
1223 }
1224 memcpy(cmd->cmd_buf, cmd_iu->cdb, cmd_len);
1225
1226 if (fu->flags & USBG_USE_STREAMS) {
1227 if (cmd->tag > UASP_SS_EP_COMP_NUM_STREAMS)
1228 goto err;
1229 if (!cmd->tag)
1230 cmd->stream = &fu->stream[0];
1231 else
1232 cmd->stream = &fu->stream[cmd->tag - 1];
1233 } else {
1234 cmd->stream = &fu->stream[0];
1235 }
1236
1237 switch (cmd_iu->prio_attr & 0x7) {
1238 case UAS_HEAD_TAG:
1239 cmd->prio_attr = TCM_HEAD_TAG;
1240 break;
1241 case UAS_ORDERED_TAG:
1242 cmd->prio_attr = TCM_ORDERED_TAG;
1243 break;
1244 case UAS_ACA:
1245 cmd->prio_attr = TCM_ACA_TAG;
1246 break;
1247 default:
1248 pr_debug_once("Unsupported prio_attr: %02x.\n",
1249 cmd_iu->prio_attr);
1250 case UAS_SIMPLE_TAG:
1251 cmd->prio_attr = TCM_SIMPLE_TAG;
1252 break;
1253 }
1254
1255 cmd->unpacked_lun = scsilun_to_int(&cmd_iu->lun);
1256
1257 INIT_WORK(&cmd->work, usbg_cmd_work);
1258 queue_work(tpg->workqueue, &cmd->work);
1259
1260 return 0;
1261err:
1262 usbg_release_cmd(&cmd->se_cmd);
1263 return -EINVAL;
1264}
1265
1266static void bot_cmd_work(struct work_struct *work)
1267{
1268 struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
1269 struct se_cmd *se_cmd;
1270 struct tcm_usbg_nexus *tv_nexus;
1271 struct usbg_tpg *tpg;
1272 int dir;
1273
1274 se_cmd = &cmd->se_cmd;
1275 tpg = cmd->fu->tpg;
1276 tv_nexus = tpg->tpg_nexus;
1277 dir = get_cmd_dir(cmd->cmd_buf);
1278 if (dir < 0) {
1279 transport_init_se_cmd(se_cmd,
1280 tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
1281 tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
1282 cmd->prio_attr, cmd->sense_iu.sense);
1283 goto out;
1284 }
1285
1286 if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
1287 cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
1288 cmd->data_len, cmd->prio_attr, dir,
1289 TARGET_SCF_ACK_KREF) < 0)
1290 goto out;
1291
1292 return;
1293
1294out:
1295 transport_send_check_condition_and_sense(se_cmd,
1296 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1297 transport_generic_free_cmd(&cmd->se_cmd, 0);
1298}
1299
1300static int bot_submit_command(struct f_uas *fu,
1301 void *cmdbuf, unsigned int len)
1302{
1303 struct bulk_cb_wrap *cbw = cmdbuf;
1304 struct usbg_cmd *cmd;
1305 struct usbg_tpg *tpg = fu->tpg;
1306 struct tcm_usbg_nexus *tv_nexus;
1307 u32 cmd_len;
1308
1309 if (cbw->Signature != cpu_to_le32(US_BULK_CB_SIGN)) {
1310 pr_err("Wrong signature on CBW\n");
1311 return -EINVAL;
1312 }
1313 if (len != 31) {
1314 pr_err("Wrong length for CBW\n");
1315 return -EINVAL;
1316 }
1317
1318 cmd_len = cbw->Length;
1319 if (cmd_len < 1 || cmd_len > 16)
1320 return -EINVAL;
1321
1322 tv_nexus = tpg->tpg_nexus;
1323 if (!tv_nexus) {
1324 pr_err("Missing nexus, ignoring command\n");
1325 return -ENODEV;
1326 }
1327
1328 cmd = usbg_get_cmd(fu, tv_nexus, cbw->Tag);
1329 if (IS_ERR(cmd)) {
1330 pr_err("usbg_get_cmd failed\n");
1331 return -ENOMEM;
1332 }
1333 memcpy(cmd->cmd_buf, cbw->CDB, cmd_len);
1334
1335 cmd->bot_tag = cbw->Tag;
1336 cmd->prio_attr = TCM_SIMPLE_TAG;
1337 cmd->unpacked_lun = cbw->Lun;
1338 cmd->is_read = cbw->Flags & US_BULK_FLAG_IN ? 1 : 0;
1339 cmd->data_len = le32_to_cpu(cbw->DataTransferLength);
1340 cmd->se_cmd.tag = le32_to_cpu(cmd->bot_tag);
1341
1342 INIT_WORK(&cmd->work, bot_cmd_work);
1343 queue_work(tpg->workqueue, &cmd->work);
1344
1345 return 0;
1346}
1347
1348
1349
1350static int usbg_check_true(struct se_portal_group *se_tpg)
1351{
1352 return 1;
1353}
1354
1355static int usbg_check_false(struct se_portal_group *se_tpg)
1356{
1357 return 0;
1358}
1359
1360static char *usbg_get_fabric_name(void)
1361{
1362 return "usb_gadget";
1363}
1364
1365static char *usbg_get_fabric_wwn(struct se_portal_group *se_tpg)
1366{
1367 struct usbg_tpg *tpg = container_of(se_tpg,
1368 struct usbg_tpg, se_tpg);
1369 struct usbg_tport *tport = tpg->tport;
1370
1371 return &tport->tport_name[0];
1372}
1373
1374static u16 usbg_get_tag(struct se_portal_group *se_tpg)
1375{
1376 struct usbg_tpg *tpg = container_of(se_tpg,
1377 struct usbg_tpg, se_tpg);
1378 return tpg->tport_tpgt;
1379}
1380
1381static u32 usbg_tpg_get_inst_index(struct se_portal_group *se_tpg)
1382{
1383 return 1;
1384}
1385
1386static void usbg_release_cmd(struct se_cmd *se_cmd)
1387{
1388 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1389 se_cmd);
1390 struct se_session *se_sess = se_cmd->se_sess;
1391
1392 kfree(cmd->data_buf);
1393 percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
1394}
1395
1396static u32 usbg_sess_get_index(struct se_session *se_sess)
1397{
1398 return 0;
1399}
1400
1401
1402
1403
1404static int usbg_write_pending_status(struct se_cmd *se_cmd)
1405{
1406 return 0;
1407}
1408
1409static void usbg_set_default_node_attrs(struct se_node_acl *nacl)
1410{
1411}
1412
1413static int usbg_get_cmd_state(struct se_cmd *se_cmd)
1414{
1415 return 0;
1416}
1417
1418static void usbg_queue_tm_rsp(struct se_cmd *se_cmd)
1419{
1420}
1421
1422static void usbg_aborted_task(struct se_cmd *se_cmd)
1423{
1424}
1425
1426static const char *usbg_check_wwn(const char *name)
1427{
1428 const char *n;
1429 unsigned int len;
1430
1431 n = strstr(name, "naa.");
1432 if (!n)
1433 return NULL;
1434 n += 4;
1435 len = strlen(n);
1436 if (len == 0 || len > USBG_NAMELEN - 1)
1437 return NULL;
1438 return n;
1439}
1440
1441static int usbg_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
1442{
1443 if (!usbg_check_wwn(name))
1444 return -EINVAL;
1445 return 0;
1446}
1447
1448static struct se_portal_group *usbg_make_tpg(
1449 struct se_wwn *wwn,
1450 struct config_group *group,
1451 const char *name)
1452{
1453 struct usbg_tport *tport = container_of(wwn, struct usbg_tport,
1454 tport_wwn);
1455 struct usbg_tpg *tpg;
1456 unsigned long tpgt;
1457 int ret;
1458 struct f_tcm_opts *opts;
1459 unsigned i;
1460
1461 if (strstr(name, "tpgt_") != name)
1462 return ERR_PTR(-EINVAL);
1463 if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX)
1464 return ERR_PTR(-EINVAL);
1465 ret = -ENODEV;
1466 mutex_lock(&tpg_instances_lock);
1467 for (i = 0; i < TPG_INSTANCES; ++i)
1468 if (tpg_instances[i].func_inst && !tpg_instances[i].tpg)
1469 break;
1470 if (i == TPG_INSTANCES)
1471 goto unlock_inst;
1472
1473 opts = container_of(tpg_instances[i].func_inst, struct f_tcm_opts,
1474 func_inst);
1475 mutex_lock(&opts->dep_lock);
1476 if (!opts->ready)
1477 goto unlock_dep;
1478
1479 if (opts->has_dep) {
1480 if (!try_module_get(opts->dependent))
1481 goto unlock_dep;
1482 } else {
1483 ret = configfs_depend_item_unlocked(
1484 group->cg_subsys,
1485 &opts->func_inst.group.cg_item);
1486 if (ret)
1487 goto unlock_dep;
1488 }
1489
1490 tpg = kzalloc(sizeof(struct usbg_tpg), GFP_KERNEL);
1491 ret = -ENOMEM;
1492 if (!tpg)
1493 goto unref_dep;
1494 mutex_init(&tpg->tpg_mutex);
1495 atomic_set(&tpg->tpg_port_count, 0);
1496 tpg->workqueue = alloc_workqueue("tcm_usb_gadget", 0, 1);
1497 if (!tpg->workqueue)
1498 goto free_tpg;
1499
1500 tpg->tport = tport;
1501 tpg->tport_tpgt = tpgt;
1502
1503
1504
1505
1506
1507 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_SAS);
1508 if (ret < 0)
1509 goto free_workqueue;
1510
1511 tpg_instances[i].tpg = tpg;
1512 tpg->fi = tpg_instances[i].func_inst;
1513 mutex_unlock(&opts->dep_lock);
1514 mutex_unlock(&tpg_instances_lock);
1515 return &tpg->se_tpg;
1516
1517free_workqueue:
1518 destroy_workqueue(tpg->workqueue);
1519free_tpg:
1520 kfree(tpg);
1521unref_dep:
1522 if (opts->has_dep)
1523 module_put(opts->dependent);
1524 else
1525 configfs_undepend_item_unlocked(&opts->func_inst.group.cg_item);
1526unlock_dep:
1527 mutex_unlock(&opts->dep_lock);
1528unlock_inst:
1529 mutex_unlock(&tpg_instances_lock);
1530
1531 return ERR_PTR(ret);
1532}
1533
1534static int tcm_usbg_drop_nexus(struct usbg_tpg *);
1535
1536static void usbg_drop_tpg(struct se_portal_group *se_tpg)
1537{
1538 struct usbg_tpg *tpg = container_of(se_tpg,
1539 struct usbg_tpg, se_tpg);
1540 unsigned i;
1541 struct f_tcm_opts *opts;
1542
1543 tcm_usbg_drop_nexus(tpg);
1544 core_tpg_deregister(se_tpg);
1545 destroy_workqueue(tpg->workqueue);
1546
1547 mutex_lock(&tpg_instances_lock);
1548 for (i = 0; i < TPG_INSTANCES; ++i)
1549 if (tpg_instances[i].tpg == tpg)
1550 break;
1551 if (i < TPG_INSTANCES) {
1552 tpg_instances[i].tpg = NULL;
1553 opts = container_of(tpg_instances[i].func_inst,
1554 struct f_tcm_opts, func_inst);
1555 mutex_lock(&opts->dep_lock);
1556 if (opts->has_dep)
1557 module_put(opts->dependent);
1558 else
1559 configfs_undepend_item_unlocked(
1560 &opts->func_inst.group.cg_item);
1561 mutex_unlock(&opts->dep_lock);
1562 }
1563 mutex_unlock(&tpg_instances_lock);
1564
1565 kfree(tpg);
1566}
1567
1568static struct se_wwn *usbg_make_tport(
1569 struct target_fabric_configfs *tf,
1570 struct config_group *group,
1571 const char *name)
1572{
1573 struct usbg_tport *tport;
1574 const char *wnn_name;
1575 u64 wwpn = 0;
1576
1577 wnn_name = usbg_check_wwn(name);
1578 if (!wnn_name)
1579 return ERR_PTR(-EINVAL);
1580
1581 tport = kzalloc(sizeof(struct usbg_tport), GFP_KERNEL);
1582 if (!(tport))
1583 return ERR_PTR(-ENOMEM);
1584
1585 tport->tport_wwpn = wwpn;
1586 snprintf(tport->tport_name, sizeof(tport->tport_name), "%s", wnn_name);
1587 return &tport->tport_wwn;
1588}
1589
1590static void usbg_drop_tport(struct se_wwn *wwn)
1591{
1592 struct usbg_tport *tport = container_of(wwn,
1593 struct usbg_tport, tport_wwn);
1594 kfree(tport);
1595}
1596
1597
1598
1599
1600static ssize_t usbg_wwn_version_show(struct config_item *item, char *page)
1601{
1602 return sprintf(page, "usb-gadget fabric module\n");
1603}
1604
1605CONFIGFS_ATTR_RO(usbg_wwn_, version);
1606
1607static struct configfs_attribute *usbg_wwn_attrs[] = {
1608 &usbg_wwn_attr_version,
1609 NULL,
1610};
1611
1612static ssize_t tcm_usbg_tpg_enable_show(struct config_item *item, char *page)
1613{
1614 struct se_portal_group *se_tpg = to_tpg(item);
1615 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1616
1617 return snprintf(page, PAGE_SIZE, "%u\n", tpg->gadget_connect);
1618}
1619
1620static int usbg_attach(struct usbg_tpg *);
1621static void usbg_detach(struct usbg_tpg *);
1622
1623static ssize_t tcm_usbg_tpg_enable_store(struct config_item *item,
1624 const char *page, size_t count)
1625{
1626 struct se_portal_group *se_tpg = to_tpg(item);
1627 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1628 bool op;
1629 ssize_t ret;
1630
1631 ret = strtobool(page, &op);
1632 if (ret)
1633 return ret;
1634
1635 if ((op && tpg->gadget_connect) || (!op && !tpg->gadget_connect))
1636 return -EINVAL;
1637
1638 if (op)
1639 ret = usbg_attach(tpg);
1640 else
1641 usbg_detach(tpg);
1642 if (ret)
1643 return ret;
1644
1645 tpg->gadget_connect = op;
1646
1647 return count;
1648}
1649
1650static ssize_t tcm_usbg_tpg_nexus_show(struct config_item *item, char *page)
1651{
1652 struct se_portal_group *se_tpg = to_tpg(item);
1653 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1654 struct tcm_usbg_nexus *tv_nexus;
1655 ssize_t ret;
1656
1657 mutex_lock(&tpg->tpg_mutex);
1658 tv_nexus = tpg->tpg_nexus;
1659 if (!tv_nexus) {
1660 ret = -ENODEV;
1661 goto out;
1662 }
1663 ret = snprintf(page, PAGE_SIZE, "%s\n",
1664 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1665out:
1666 mutex_unlock(&tpg->tpg_mutex);
1667 return ret;
1668}
1669
1670static int usbg_alloc_sess_cb(struct se_portal_group *se_tpg,
1671 struct se_session *se_sess, void *p)
1672{
1673 struct usbg_tpg *tpg = container_of(se_tpg,
1674 struct usbg_tpg, se_tpg);
1675
1676 tpg->tpg_nexus = p;
1677 return 0;
1678}
1679
1680static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name)
1681{
1682 struct tcm_usbg_nexus *tv_nexus;
1683 int ret = 0;
1684
1685 mutex_lock(&tpg->tpg_mutex);
1686 if (tpg->tpg_nexus) {
1687 ret = -EEXIST;
1688 pr_debug("tpg->tpg_nexus already exists\n");
1689 goto out_unlock;
1690 }
1691
1692 tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
1693 if (!tv_nexus) {
1694 ret = -ENOMEM;
1695 goto out_unlock;
1696 }
1697
1698 tv_nexus->tvn_se_sess = target_alloc_session(&tpg->se_tpg,
1699 USB_G_DEFAULT_SESSION_TAGS,
1700 sizeof(struct usbg_cmd),
1701 TARGET_PROT_NORMAL, name,
1702 tv_nexus, usbg_alloc_sess_cb);
1703 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1704#define MAKE_NEXUS_MSG "core_tpg_check_initiator_node_acl() failed for %s\n"
1705 pr_debug(MAKE_NEXUS_MSG, name);
1706#undef MAKE_NEXUS_MSG
1707 ret = PTR_ERR(tv_nexus->tvn_se_sess);
1708 kfree(tv_nexus);
1709 }
1710
1711out_unlock:
1712 mutex_unlock(&tpg->tpg_mutex);
1713 return ret;
1714}
1715
1716static int tcm_usbg_drop_nexus(struct usbg_tpg *tpg)
1717{
1718 struct se_session *se_sess;
1719 struct tcm_usbg_nexus *tv_nexus;
1720 int ret = -ENODEV;
1721
1722 mutex_lock(&tpg->tpg_mutex);
1723 tv_nexus = tpg->tpg_nexus;
1724 if (!tv_nexus)
1725 goto out;
1726
1727 se_sess = tv_nexus->tvn_se_sess;
1728 if (!se_sess)
1729 goto out;
1730
1731 if (atomic_read(&tpg->tpg_port_count)) {
1732 ret = -EPERM;
1733#define MSG "Unable to remove Host I_T Nexus with active TPG port count: %d\n"
1734 pr_err(MSG, atomic_read(&tpg->tpg_port_count));
1735#undef MSG
1736 goto out;
1737 }
1738
1739 pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
1740 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1741
1742
1743
1744 transport_deregister_session(tv_nexus->tvn_se_sess);
1745 tpg->tpg_nexus = NULL;
1746
1747 kfree(tv_nexus);
1748 ret = 0;
1749out:
1750 mutex_unlock(&tpg->tpg_mutex);
1751 return ret;
1752}
1753
1754static ssize_t tcm_usbg_tpg_nexus_store(struct config_item *item,
1755 const char *page, size_t count)
1756{
1757 struct se_portal_group *se_tpg = to_tpg(item);
1758 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1759 unsigned char i_port[USBG_NAMELEN], *ptr;
1760 int ret;
1761
1762 if (!strncmp(page, "NULL", 4)) {
1763 ret = tcm_usbg_drop_nexus(tpg);
1764 return (!ret) ? count : ret;
1765 }
1766 if (strlen(page) >= USBG_NAMELEN) {
1767
1768#define NEXUS_STORE_MSG "Emulated NAA Sas Address: %s, exceeds max: %d\n"
1769 pr_err(NEXUS_STORE_MSG, page, USBG_NAMELEN);
1770#undef NEXUS_STORE_MSG
1771 return -EINVAL;
1772 }
1773 snprintf(i_port, USBG_NAMELEN, "%s", page);
1774
1775 ptr = strstr(i_port, "naa.");
1776 if (!ptr) {
1777 pr_err("Missing 'naa.' prefix\n");
1778 return -EINVAL;
1779 }
1780
1781 if (i_port[strlen(i_port) - 1] == '\n')
1782 i_port[strlen(i_port) - 1] = '\0';
1783
1784 ret = tcm_usbg_make_nexus(tpg, &i_port[0]);
1785 if (ret < 0)
1786 return ret;
1787 return count;
1788}
1789
1790CONFIGFS_ATTR(tcm_usbg_tpg_, enable);
1791CONFIGFS_ATTR(tcm_usbg_tpg_, nexus);
1792
1793static struct configfs_attribute tcm_usbg_tpg_attr_maxburst;
1794static struct configfs_attribute *usbg_base_attrs[] = {
1795 &tcm_usbg_tpg_attr_enable,
1796 &tcm_usbg_tpg_attr_nexus,
1797 &tcm_usbg_tpg_attr_maxburst,
1798 NULL,
1799};
1800
1801static int usbg_port_link(struct se_portal_group *se_tpg, struct se_lun *lun)
1802{
1803 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1804
1805 atomic_inc(&tpg->tpg_port_count);
1806 smp_mb__after_atomic();
1807 return 0;
1808}
1809
1810static void usbg_port_unlink(struct se_portal_group *se_tpg,
1811 struct se_lun *se_lun)
1812{
1813 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1814
1815 atomic_dec(&tpg->tpg_port_count);
1816 smp_mb__after_atomic();
1817}
1818
1819static int usbg_check_stop_free(struct se_cmd *se_cmd)
1820{
1821 return target_put_sess_cmd(se_cmd);
1822}
1823
1824static const struct target_core_fabric_ops usbg_ops = {
1825 .module = THIS_MODULE,
1826 .name = "usb_gadget",
1827 .get_fabric_name = usbg_get_fabric_name,
1828 .tpg_get_wwn = usbg_get_fabric_wwn,
1829 .tpg_get_tag = usbg_get_tag,
1830 .tpg_check_demo_mode = usbg_check_true,
1831 .tpg_check_demo_mode_cache = usbg_check_false,
1832 .tpg_check_demo_mode_write_protect = usbg_check_false,
1833 .tpg_check_prod_mode_write_protect = usbg_check_false,
1834 .tpg_get_inst_index = usbg_tpg_get_inst_index,
1835 .release_cmd = usbg_release_cmd,
1836 .sess_get_index = usbg_sess_get_index,
1837 .sess_get_initiator_sid = NULL,
1838 .write_pending = usbg_send_write_request,
1839 .write_pending_status = usbg_write_pending_status,
1840 .set_default_node_attributes = usbg_set_default_node_attrs,
1841 .get_cmd_state = usbg_get_cmd_state,
1842 .queue_data_in = usbg_send_read_response,
1843 .queue_status = usbg_send_status_response,
1844 .queue_tm_rsp = usbg_queue_tm_rsp,
1845 .aborted_task = usbg_aborted_task,
1846 .check_stop_free = usbg_check_stop_free,
1847
1848 .fabric_make_wwn = usbg_make_tport,
1849 .fabric_drop_wwn = usbg_drop_tport,
1850 .fabric_make_tpg = usbg_make_tpg,
1851 .fabric_drop_tpg = usbg_drop_tpg,
1852 .fabric_post_link = usbg_port_link,
1853 .fabric_pre_unlink = usbg_port_unlink,
1854 .fabric_init_nodeacl = usbg_init_nodeacl,
1855
1856 .tfc_wwn_attrs = usbg_wwn_attrs,
1857 .tfc_tpg_base_attrs = usbg_base_attrs,
1858};
1859
1860
1861
1862static struct usb_interface_descriptor bot_intf_desc = {
1863 .bLength = sizeof(bot_intf_desc),
1864 .bDescriptorType = USB_DT_INTERFACE,
1865 .bNumEndpoints = 2,
1866 .bAlternateSetting = USB_G_ALT_INT_BBB,
1867 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1868 .bInterfaceSubClass = USB_SC_SCSI,
1869 .bInterfaceProtocol = USB_PR_BULK,
1870};
1871
1872static struct usb_interface_descriptor uasp_intf_desc = {
1873 .bLength = sizeof(uasp_intf_desc),
1874 .bDescriptorType = USB_DT_INTERFACE,
1875 .bNumEndpoints = 4,
1876 .bAlternateSetting = USB_G_ALT_INT_UAS,
1877 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1878 .bInterfaceSubClass = USB_SC_SCSI,
1879 .bInterfaceProtocol = USB_PR_UAS,
1880};
1881
1882static struct usb_endpoint_descriptor uasp_bi_desc = {
1883 .bLength = USB_DT_ENDPOINT_SIZE,
1884 .bDescriptorType = USB_DT_ENDPOINT,
1885 .bEndpointAddress = USB_DIR_IN,
1886 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1887 .wMaxPacketSize = cpu_to_le16(512),
1888};
1889
1890static struct usb_endpoint_descriptor uasp_fs_bi_desc = {
1891 .bLength = USB_DT_ENDPOINT_SIZE,
1892 .bDescriptorType = USB_DT_ENDPOINT,
1893 .bEndpointAddress = USB_DIR_IN,
1894 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1895};
1896
1897static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc = {
1898 .bLength = sizeof(uasp_bi_pipe_desc),
1899 .bDescriptorType = USB_DT_PIPE_USAGE,
1900 .bPipeID = DATA_IN_PIPE_ID,
1901};
1902
1903static struct usb_endpoint_descriptor uasp_ss_bi_desc = {
1904 .bLength = USB_DT_ENDPOINT_SIZE,
1905 .bDescriptorType = USB_DT_ENDPOINT,
1906 .bEndpointAddress = USB_DIR_IN,
1907 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1908 .wMaxPacketSize = cpu_to_le16(1024),
1909};
1910
1911static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc = {
1912 .bLength = sizeof(uasp_bi_ep_comp_desc),
1913 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1914 .bMaxBurst = 0,
1915 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1916 .wBytesPerInterval = 0,
1917};
1918
1919static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc = {
1920 .bLength = sizeof(bot_bi_ep_comp_desc),
1921 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1922 .bMaxBurst = 0,
1923};
1924
1925static struct usb_endpoint_descriptor uasp_bo_desc = {
1926 .bLength = USB_DT_ENDPOINT_SIZE,
1927 .bDescriptorType = USB_DT_ENDPOINT,
1928 .bEndpointAddress = USB_DIR_OUT,
1929 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1930 .wMaxPacketSize = cpu_to_le16(512),
1931};
1932
1933static struct usb_endpoint_descriptor uasp_fs_bo_desc = {
1934 .bLength = USB_DT_ENDPOINT_SIZE,
1935 .bDescriptorType = USB_DT_ENDPOINT,
1936 .bEndpointAddress = USB_DIR_OUT,
1937 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1938};
1939
1940static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc = {
1941 .bLength = sizeof(uasp_bo_pipe_desc),
1942 .bDescriptorType = USB_DT_PIPE_USAGE,
1943 .bPipeID = DATA_OUT_PIPE_ID,
1944};
1945
1946static struct usb_endpoint_descriptor uasp_ss_bo_desc = {
1947 .bLength = USB_DT_ENDPOINT_SIZE,
1948 .bDescriptorType = USB_DT_ENDPOINT,
1949 .bEndpointAddress = USB_DIR_OUT,
1950 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1951 .wMaxPacketSize = cpu_to_le16(0x400),
1952};
1953
1954static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc = {
1955 .bLength = sizeof(uasp_bo_ep_comp_desc),
1956 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1957 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1958};
1959
1960static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc = {
1961 .bLength = sizeof(bot_bo_ep_comp_desc),
1962 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1963};
1964
1965static struct usb_endpoint_descriptor uasp_status_desc = {
1966 .bLength = USB_DT_ENDPOINT_SIZE,
1967 .bDescriptorType = USB_DT_ENDPOINT,
1968 .bEndpointAddress = USB_DIR_IN,
1969 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1970 .wMaxPacketSize = cpu_to_le16(512),
1971};
1972
1973static struct usb_endpoint_descriptor uasp_fs_status_desc = {
1974 .bLength = USB_DT_ENDPOINT_SIZE,
1975 .bDescriptorType = USB_DT_ENDPOINT,
1976 .bEndpointAddress = USB_DIR_IN,
1977 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1978};
1979
1980static struct usb_pipe_usage_descriptor uasp_status_pipe_desc = {
1981 .bLength = sizeof(uasp_status_pipe_desc),
1982 .bDescriptorType = USB_DT_PIPE_USAGE,
1983 .bPipeID = STATUS_PIPE_ID,
1984};
1985
1986static struct usb_endpoint_descriptor uasp_ss_status_desc = {
1987 .bLength = USB_DT_ENDPOINT_SIZE,
1988 .bDescriptorType = USB_DT_ENDPOINT,
1989 .bEndpointAddress = USB_DIR_IN,
1990 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1991 .wMaxPacketSize = cpu_to_le16(1024),
1992};
1993
1994static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc = {
1995 .bLength = sizeof(uasp_status_in_ep_comp_desc),
1996 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1997 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1998};
1999
2000static struct usb_endpoint_descriptor uasp_cmd_desc = {
2001 .bLength = USB_DT_ENDPOINT_SIZE,
2002 .bDescriptorType = USB_DT_ENDPOINT,
2003 .bEndpointAddress = USB_DIR_OUT,
2004 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2005 .wMaxPacketSize = cpu_to_le16(512),
2006};
2007
2008static struct usb_endpoint_descriptor uasp_fs_cmd_desc = {
2009 .bLength = USB_DT_ENDPOINT_SIZE,
2010 .bDescriptorType = USB_DT_ENDPOINT,
2011 .bEndpointAddress = USB_DIR_OUT,
2012 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2013};
2014
2015static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc = {
2016 .bLength = sizeof(uasp_cmd_pipe_desc),
2017 .bDescriptorType = USB_DT_PIPE_USAGE,
2018 .bPipeID = CMD_PIPE_ID,
2019};
2020
2021static struct usb_endpoint_descriptor uasp_ss_cmd_desc = {
2022 .bLength = USB_DT_ENDPOINT_SIZE,
2023 .bDescriptorType = USB_DT_ENDPOINT,
2024 .bEndpointAddress = USB_DIR_OUT,
2025 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2026 .wMaxPacketSize = cpu_to_le16(1024),
2027};
2028
2029static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc = {
2030 .bLength = sizeof(uasp_cmd_comp_desc),
2031 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
2032};
2033
2034static struct usb_descriptor_header *uasp_fs_function_desc[] = {
2035 (struct usb_descriptor_header *) &bot_intf_desc,
2036 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
2037 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
2038
2039 (struct usb_descriptor_header *) &uasp_intf_desc,
2040 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
2041 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
2042 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
2043 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
2044 (struct usb_descriptor_header *) &uasp_fs_status_desc,
2045 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
2046 (struct usb_descriptor_header *) &uasp_fs_cmd_desc,
2047 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
2048 NULL,
2049};
2050
2051static struct usb_descriptor_header *uasp_hs_function_desc[] = {
2052 (struct usb_descriptor_header *) &bot_intf_desc,
2053 (struct usb_descriptor_header *) &uasp_bi_desc,
2054 (struct usb_descriptor_header *) &uasp_bo_desc,
2055
2056 (struct usb_descriptor_header *) &uasp_intf_desc,
2057 (struct usb_descriptor_header *) &uasp_bi_desc,
2058 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
2059 (struct usb_descriptor_header *) &uasp_bo_desc,
2060 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
2061 (struct usb_descriptor_header *) &uasp_status_desc,
2062 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
2063 (struct usb_descriptor_header *) &uasp_cmd_desc,
2064 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
2065 NULL,
2066};
2067
2068static struct usb_descriptor_header *uasp_ss_function_desc[] = {
2069 (struct usb_descriptor_header *) &bot_intf_desc,
2070 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
2071 (struct usb_descriptor_header *) &bot_bi_ep_comp_desc,
2072 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
2073 (struct usb_descriptor_header *) &bot_bo_ep_comp_desc,
2074
2075 (struct usb_descriptor_header *) &uasp_intf_desc,
2076 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
2077 (struct usb_descriptor_header *) &uasp_bi_ep_comp_desc,
2078 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
2079 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
2080 (struct usb_descriptor_header *) &uasp_bo_ep_comp_desc,
2081 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
2082 (struct usb_descriptor_header *) &uasp_ss_status_desc,
2083 (struct usb_descriptor_header *) &uasp_status_in_ep_comp_desc,
2084 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
2085 (struct usb_descriptor_header *) &uasp_ss_cmd_desc,
2086 (struct usb_descriptor_header *) &uasp_cmd_comp_desc,
2087 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
2088 NULL,
2089};
2090
2091static struct usb_string tcm_us_strings[] = {
2092 [USB_G_STR_INT_UAS].s = "USB Attached SCSI",
2093 [USB_G_STR_INT_BBB].s = "Bulk Only Transport",
2094 { },
2095};
2096
2097static struct usb_gadget_strings tcm_stringtab = {
2098 .language = 0x0409,
2099 .strings = tcm_us_strings,
2100};
2101
2102static struct usb_gadget_strings *tcm_strings[] = {
2103 &tcm_stringtab,
2104 NULL,
2105};
2106
2107static ssize_t tcm_usbg_tpg_maxburst_show(struct config_item *item, char *page)
2108{
2109 return snprintf(page, PAGE_SIZE, "%u\n", uasp_cmd_comp_desc.bMaxBurst);
2110}
2111
2112static ssize_t tcm_usbg_tpg_maxburst_store(struct config_item *item,
2113 const char *page, size_t count)
2114{
2115 int value;
2116 int ret;
2117
2118 ret = kstrtouint(page, 10, &value);
2119 if (ret)
2120 return ret;
2121
2122 uasp_bi_ep_comp_desc.bMaxBurst = value;
2123 uasp_bo_ep_comp_desc.bMaxBurst = value;
2124 uasp_status_in_ep_comp_desc.bMaxBurst = value;
2125 uasp_cmd_comp_desc.bMaxBurst = value;
2126 bot_bi_ep_comp_desc.bMaxBurst = value;
2127 bot_bo_ep_comp_desc.bMaxBurst = value;
2128
2129 return count;
2130}
2131CONFIGFS_ATTR(tcm_usbg_tpg_, maxburst);
2132
2133static int tcm_bind(struct usb_configuration *c, struct usb_function *f)
2134{
2135 struct f_uas *fu = to_f_uas(f);
2136 struct usb_string *us;
2137 struct usb_gadget *gadget = c->cdev->gadget;
2138 struct usb_ep *ep;
2139 struct f_tcm_opts *opts;
2140 int iface;
2141 int ret;
2142
2143 opts = container_of(f->fi, struct f_tcm_opts, func_inst);
2144
2145 mutex_lock(&opts->dep_lock);
2146 if (!opts->can_attach) {
2147 mutex_unlock(&opts->dep_lock);
2148 return -ENODEV;
2149 }
2150 mutex_unlock(&opts->dep_lock);
2151 us = usb_gstrings_attach(c->cdev, tcm_strings,
2152 ARRAY_SIZE(tcm_us_strings));
2153 if (IS_ERR(us))
2154 return PTR_ERR(us);
2155 bot_intf_desc.iInterface = us[USB_G_STR_INT_BBB].id;
2156 uasp_intf_desc.iInterface = us[USB_G_STR_INT_UAS].id;
2157
2158 iface = usb_interface_id(c, f);
2159 if (iface < 0)
2160 return iface;
2161
2162 bot_intf_desc.bInterfaceNumber = iface;
2163 uasp_intf_desc.bInterfaceNumber = iface;
2164 fu->iface = iface;
2165 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bi_desc,
2166 &uasp_bi_ep_comp_desc);
2167 if (!ep)
2168 goto ep_fail;
2169
2170 fu->ep_in = ep;
2171
2172 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bo_desc,
2173 &uasp_bo_ep_comp_desc);
2174 if (!ep)
2175 goto ep_fail;
2176 fu->ep_out = ep;
2177
2178 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_status_desc,
2179 &uasp_status_in_ep_comp_desc);
2180 if (!ep)
2181 goto ep_fail;
2182 fu->ep_status = ep;
2183
2184 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_cmd_desc,
2185 &uasp_cmd_comp_desc);
2186 if (!ep)
2187 goto ep_fail;
2188 fu->ep_cmd = ep;
2189
2190
2191 uasp_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2192 uasp_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2193 uasp_status_desc.bEndpointAddress =
2194 uasp_ss_status_desc.bEndpointAddress;
2195 uasp_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2196
2197 uasp_fs_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2198 uasp_fs_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2199 uasp_fs_status_desc.bEndpointAddress =
2200 uasp_ss_status_desc.bEndpointAddress;
2201 uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2202
2203 ret = usb_assign_descriptors(f, uasp_fs_function_desc,
2204 uasp_hs_function_desc, uasp_ss_function_desc, NULL);
2205 if (ret)
2206 goto ep_fail;
2207
2208 return 0;
2209ep_fail:
2210 pr_err("Can't claim all required eps\n");
2211
2212 return -ENOTSUPP;
2213}
2214
2215struct guas_setup_wq {
2216 struct work_struct work;
2217 struct f_uas *fu;
2218 unsigned int alt;
2219};
2220
2221static void tcm_delayed_set_alt(struct work_struct *wq)
2222{
2223 struct guas_setup_wq *work = container_of(wq, struct guas_setup_wq,
2224 work);
2225 struct f_uas *fu = work->fu;
2226 int alt = work->alt;
2227
2228 kfree(work);
2229
2230 if (fu->flags & USBG_IS_BOT)
2231 bot_cleanup_old_alt(fu);
2232 if (fu->flags & USBG_IS_UAS)
2233 uasp_cleanup_old_alt(fu);
2234
2235 if (alt == USB_G_ALT_INT_BBB)
2236 bot_set_alt(fu);
2237 else if (alt == USB_G_ALT_INT_UAS)
2238 uasp_set_alt(fu);
2239 usb_composite_setup_continue(fu->function.config->cdev);
2240}
2241
2242static int tcm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2243{
2244 struct f_uas *fu = to_f_uas(f);
2245
2246 if ((alt == USB_G_ALT_INT_BBB) || (alt == USB_G_ALT_INT_UAS)) {
2247 struct guas_setup_wq *work;
2248
2249 work = kmalloc(sizeof(*work), GFP_ATOMIC);
2250 if (!work)
2251 return -ENOMEM;
2252 INIT_WORK(&work->work, tcm_delayed_set_alt);
2253 work->fu = fu;
2254 work->alt = alt;
2255 schedule_work(&work->work);
2256 return USB_GADGET_DELAYED_STATUS;
2257 }
2258 return -EOPNOTSUPP;
2259}
2260
2261static void tcm_disable(struct usb_function *f)
2262{
2263 struct f_uas *fu = to_f_uas(f);
2264
2265 if (fu->flags & USBG_IS_UAS)
2266 uasp_cleanup_old_alt(fu);
2267 else if (fu->flags & USBG_IS_BOT)
2268 bot_cleanup_old_alt(fu);
2269 fu->flags = 0;
2270}
2271
2272static int tcm_setup(struct usb_function *f,
2273 const struct usb_ctrlrequest *ctrl)
2274{
2275 struct f_uas *fu = to_f_uas(f);
2276
2277 if (!(fu->flags & USBG_IS_BOT))
2278 return -EOPNOTSUPP;
2279
2280 return usbg_bot_setup(f, ctrl);
2281}
2282
2283static inline struct f_tcm_opts *to_f_tcm_opts(struct config_item *item)
2284{
2285 return container_of(to_config_group(item), struct f_tcm_opts,
2286 func_inst.group);
2287}
2288
2289static void tcm_attr_release(struct config_item *item)
2290{
2291 struct f_tcm_opts *opts = to_f_tcm_opts(item);
2292
2293 usb_put_function_instance(&opts->func_inst);
2294}
2295
2296static struct configfs_item_operations tcm_item_ops = {
2297 .release = tcm_attr_release,
2298};
2299
2300static struct config_item_type tcm_func_type = {
2301 .ct_item_ops = &tcm_item_ops,
2302 .ct_owner = THIS_MODULE,
2303};
2304
2305static void tcm_free_inst(struct usb_function_instance *f)
2306{
2307 struct f_tcm_opts *opts;
2308 unsigned i;
2309
2310 opts = container_of(f, struct f_tcm_opts, func_inst);
2311
2312 mutex_lock(&tpg_instances_lock);
2313 for (i = 0; i < TPG_INSTANCES; ++i)
2314 if (tpg_instances[i].func_inst == f)
2315 break;
2316 if (i < TPG_INSTANCES)
2317 tpg_instances[i].func_inst = NULL;
2318 mutex_unlock(&tpg_instances_lock);
2319
2320 kfree(opts);
2321}
2322
2323static int tcm_register_callback(struct usb_function_instance *f)
2324{
2325 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2326
2327 mutex_lock(&opts->dep_lock);
2328 opts->can_attach = true;
2329 mutex_unlock(&opts->dep_lock);
2330
2331 return 0;
2332}
2333
2334static void tcm_unregister_callback(struct usb_function_instance *f)
2335{
2336 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2337
2338 mutex_lock(&opts->dep_lock);
2339 unregister_gadget_item(opts->
2340 func_inst.group.cg_item.ci_parent->ci_parent);
2341 opts->can_attach = false;
2342 mutex_unlock(&opts->dep_lock);
2343}
2344
2345static int usbg_attach(struct usbg_tpg *tpg)
2346{
2347 struct usb_function_instance *f = tpg->fi;
2348 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2349
2350 if (opts->tcm_register_callback)
2351 return opts->tcm_register_callback(f);
2352
2353 return 0;
2354}
2355
2356static void usbg_detach(struct usbg_tpg *tpg)
2357{
2358 struct usb_function_instance *f = tpg->fi;
2359 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2360
2361 if (opts->tcm_unregister_callback)
2362 opts->tcm_unregister_callback(f);
2363}
2364
2365static int tcm_set_name(struct usb_function_instance *f, const char *name)
2366{
2367 struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst);
2368
2369 pr_debug("tcm: Activating %s\n", name);
2370
2371 mutex_lock(&opts->dep_lock);
2372 opts->ready = true;
2373 mutex_unlock(&opts->dep_lock);
2374
2375 return 0;
2376}
2377
2378static struct usb_function_instance *tcm_alloc_inst(void)
2379{
2380 struct f_tcm_opts *opts;
2381 int i;
2382
2383
2384 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2385 if (!opts)
2386 return ERR_PTR(-ENOMEM);
2387
2388 mutex_lock(&tpg_instances_lock);
2389 for (i = 0; i < TPG_INSTANCES; ++i)
2390 if (!tpg_instances[i].func_inst)
2391 break;
2392
2393 if (i == TPG_INSTANCES) {
2394 mutex_unlock(&tpg_instances_lock);
2395 kfree(opts);
2396 return ERR_PTR(-EBUSY);
2397 }
2398 tpg_instances[i].func_inst = &opts->func_inst;
2399 mutex_unlock(&tpg_instances_lock);
2400
2401 mutex_init(&opts->dep_lock);
2402 opts->func_inst.set_inst_name = tcm_set_name;
2403 opts->func_inst.free_func_inst = tcm_free_inst;
2404 opts->tcm_register_callback = tcm_register_callback;
2405 opts->tcm_unregister_callback = tcm_unregister_callback;
2406
2407 config_group_init_type_name(&opts->func_inst.group, "",
2408 &tcm_func_type);
2409
2410 return &opts->func_inst;
2411}
2412
2413static void tcm_free(struct usb_function *f)
2414{
2415 struct f_uas *tcm = to_f_uas(f);
2416
2417 kfree(tcm);
2418}
2419
2420static void tcm_unbind(struct usb_configuration *c, struct usb_function *f)
2421{
2422 usb_free_all_descriptors(f);
2423}
2424
2425static struct usb_function *tcm_alloc(struct usb_function_instance *fi)
2426{
2427 struct f_uas *fu;
2428 unsigned i;
2429
2430 mutex_lock(&tpg_instances_lock);
2431 for (i = 0; i < TPG_INSTANCES; ++i)
2432 if (tpg_instances[i].func_inst == fi)
2433 break;
2434 if (i == TPG_INSTANCES) {
2435 mutex_unlock(&tpg_instances_lock);
2436 return ERR_PTR(-ENODEV);
2437 }
2438
2439 fu = kzalloc(sizeof(*fu), GFP_KERNEL);
2440 if (!fu) {
2441 mutex_unlock(&tpg_instances_lock);
2442 return ERR_PTR(-ENOMEM);
2443 }
2444
2445 fu->function.name = "Target Function";
2446 fu->function.bind = tcm_bind;
2447 fu->function.unbind = tcm_unbind;
2448 fu->function.set_alt = tcm_set_alt;
2449 fu->function.setup = tcm_setup;
2450 fu->function.disable = tcm_disable;
2451 fu->function.free_func = tcm_free;
2452 fu->tpg = tpg_instances[i].tpg;
2453 mutex_unlock(&tpg_instances_lock);
2454
2455 return &fu->function;
2456}
2457
2458DECLARE_USB_FUNCTION(tcm, tcm_alloc_inst, tcm_alloc);
2459
2460static int tcm_init(void)
2461{
2462 int ret;
2463
2464 ret = usb_function_register(&tcmusb_func);
2465 if (ret)
2466 return ret;
2467
2468 ret = target_register_template(&usbg_ops);
2469 if (ret)
2470 usb_function_unregister(&tcmusb_func);
2471
2472 return ret;
2473}
2474module_init(tcm_init);
2475
2476static void tcm_exit(void)
2477{
2478 target_unregister_template(&usbg_ops);
2479 usb_function_unregister(&tcmusb_func);
2480}
2481module_exit(tcm_exit);
2482
2483MODULE_LICENSE("GPL");
2484MODULE_AUTHOR("Sebastian Andrzej Siewior");
2485