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