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