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