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