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