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