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