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