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