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