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