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