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