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