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