9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/string.h>
14 #include <linux/ctype.h>
15 #include <linux/usb/ch9.h>
19 #include <scsi/scsi.h>
26 #include <asm/unaligned.h>
39 static void usbg_cmd_release(
struct kref *);
41 static inline void usbg_cleanup_cmd(
struct usbg_cmd *
cmd)
43 kref_put(&cmd->
ref, usbg_cmd_release);
48 static int bot_enqueue_cmd_cbw(
struct f_uas *fu)
66 usbg_cleanup_cmd(cmd);
68 pr_err(
"ERR %s(%d)\n", __func__, __LINE__);
73 bot_enqueue_cmd_cbw(fu);
76 static void bot_enqueue_sense_code(
struct f_uas *fu,
struct usbg_cmd *cmd)
81 unsigned int csw_stat;
97 pr_err(
"%s(%d) ERR: %d\n", __func__, __LINE__, ret);
106 pr_err(
"ERR %s(%d)\n", __func__, __LINE__);
120 bot_enqueue_sense_code(fu, cmd);
123 static void bot_send_bad_status(
struct usbg_cmd *cmd)
153 bot_enqueue_sense_code(fu, cmd);
157 static int bot_send_status(
struct usbg_cmd *cmd,
bool moved_data)
170 bot_send_bad_status(cmd);
181 pr_err(
"%s(%d) ERR: %d\n", __func__, __LINE__, ret);
184 bot_send_bad_status(cmd);
193 static int bot_send_status_response(
struct usbg_cmd *cmd)
195 bool moved_data =
false;
199 return bot_send_status(cmd, moved_data);
208 pr_err(
"ERR %s(%d)\n", __func__, __LINE__);
210 bot_send_status(cmd,
true);
213 static int bot_send_read_response(
struct usbg_cmd *cmd)
222 bot_send_bad_status(cmd);
248 pr_err(
"%s(%d)\n", __func__, __LINE__);
255 static int bot_send_write_request(
struct usbg_cmd *cmd)
258 struct se_cmd *se_cmd = &cmd->
se_cmd;
286 ret = usbg_prepare_w_request(cmd, fu->
bot_req_out);
291 pr_err(
"%s(%d)\n", __func__, __LINE__);
299 static int bot_submit_command(
struct f_uas *,
void *,
unsigned int);
311 ret = bot_submit_command(fu, req->
buf, req->
actual);
313 pr_err(
"%s(%d): %d\n", __func__, __LINE__, ret);
316 static int bot_prepare_reqs(
struct f_uas *fu)
338 fu->
bot_status.req->complete = bot_status_complete;
345 fu->
cmd.req->complete = bot_cmd_complete;
346 fu->
cmd.req->buf = fu->
cmd.buf;
347 fu->
cmd.req->length = fu->
ep_out->maxpacket;
348 fu->
cmd.req->context = fu;
350 ret = bot_enqueue_cmd_cbw(fu);
360 usb_ep_free_request(fu->
ep_out, fu->
cmd.req);
369 pr_err(
"BOT: endpoint setup failed\n");
378 usb_ep_disable(fu->
ep_in);
379 usb_ep_disable(fu->
ep_out);
386 usb_ep_free_request(fu->
ep_out, fu->
cmd.req);
398 static void bot_set_alt(
struct f_uas *fu)
407 ret = usb_ep_enable(fu->
ep_in);
412 ret = usb_ep_enable(fu->
ep_out);
416 ret = bot_prepare_reqs(fu);
420 pr_info(
"Using the BOT protocol\n");
423 usb_ep_disable(fu->
ep_out);
425 usb_ep_disable(fu->
ep_in);
433 struct f_uas *fu = to_f_uas(f);
452 pr_err(
"No LUNs configured?\n");
464 ret_lun = cdev->
req->buf;
466 cdev->
req->length = 1;
472 bot_enqueue_cmd_cbw(fu);
481 static void uasp_cleanup_one_stream(
struct f_uas *fu,
struct uas_stream *stream)
496 static void uasp_free_cmdreq(
struct f_uas *fu)
498 usb_ep_free_request(fu->
ep_cmd, fu->
cmd.req);
504 static void uasp_cleanup_old_alt(
struct f_uas *fu)
511 usb_ep_disable(fu->
ep_in);
512 usb_ep_disable(fu->
ep_out);
514 usb_ep_disable(fu->
ep_cmd);
517 uasp_cleanup_one_stream(fu, &fu->
stream[i]);
518 uasp_free_cmdreq(fu);
523 static int uasp_prepare_r_request(
struct usbg_cmd *cmd)
525 struct se_cmd *se_cmd = &cmd->
se_cmd;
547 stream->
req_in->complete = uasp_status_data_cmpl;
555 static void uasp_prepare_status(
struct usbg_cmd *cmd)
557 struct se_cmd *se_cmd = &cmd->
se_cmd;
573 stream->
req_status->complete = uasp_status_data_cmpl;
586 switch (cmd->
state) {
588 ret = uasp_prepare_r_request(cmd);
593 pr_err(
"%s(%d) => %d\n", __func__, __LINE__, ret);
597 ret = usbg_prepare_w_request(cmd, stream->
req_out);
602 pr_err(
"%s(%d) => %d\n", __func__, __LINE__, ret);
606 uasp_prepare_status(cmd);
610 pr_err(
"%s(%d) => %d\n", __func__, __LINE__, ret);
614 usbg_cleanup_cmd(cmd);
624 usbg_cleanup_cmd(cmd);
627 static int uasp_send_status_response(
struct usbg_cmd *cmd)
634 stream->
req_status->complete = uasp_status_data_cmpl;
637 uasp_prepare_status(cmd);
641 static int uasp_send_read_response(
struct usbg_cmd *cmd)
653 ret = uasp_prepare_r_request(cmd);
658 pr_err(
"%s(%d) => %d\n", __func__, __LINE__, ret);
668 stream->
req_status->complete = uasp_status_data_cmpl;
673 stream->
req_status->length =
sizeof(
struct iu);
678 pr_err(
"%s(%d) => %d\n", __func__, __LINE__, ret);
684 static int uasp_send_write_request(
struct usbg_cmd *cmd)
687 struct se_cmd *se_cmd = &cmd->
se_cmd;
699 ret = usbg_prepare_w_request(cmd, stream->
req_out);
704 pr_err(
"%s(%d)\n", __func__, __LINE__);
711 stream->
req_status->complete = uasp_status_data_cmpl;
716 stream->
req_status->length =
sizeof(
struct iu);
721 pr_err(
"%s(%d)\n", __func__, __LINE__);
730 static int usbg_submit_command(
struct f_uas *,
void *,
unsigned int);
740 ret = usbg_submit_command(fu, req->
buf, req->
actual);
752 static int uasp_alloc_stream_res(
struct f_uas *fu,
struct uas_stream *stream)
777 static int uasp_alloc_cmd(
struct f_uas *fu)
787 fu->
cmd.req->complete = uasp_cmd_complete;
788 fu->
cmd.req->buf = fu->
cmd.buf;
789 fu->
cmd.req->length = fu->
ep_cmd->maxpacket;
790 fu->
cmd.req->context = fu;
794 usb_ep_free_request(fu->
ep_cmd, fu->
cmd.req);
799 static void uasp_setup_stream_res(
struct f_uas *fu,
int max_streams)
803 for (i = 0; i < max_streams; i++) {
806 s->
req_in->stream_id = i + 1;
812 static int uasp_prepare_reqs(
struct f_uas *fu)
823 for (i = 0; i < max_streams; i++) {
824 ret = uasp_alloc_stream_res(fu, &fu->
stream[i]);
829 ret = uasp_alloc_cmd(fu);
831 goto err_free_stream;
832 uasp_setup_stream_res(fu, max_streams);
836 goto err_free_stream;
841 uasp_free_cmdreq(fu);
846 uasp_cleanup_one_stream(fu, &fu->
stream[i - 1]);
850 pr_err(
"UASP: endpoint setup failed\n");
854 static void uasp_set_alt(
struct f_uas *fu)
866 ret = usb_ep_enable(fu->
ep_in);
871 ret = usb_ep_enable(fu->
ep_out);
876 ret = usb_ep_enable(fu->
ep_cmd);
884 ret = uasp_prepare_reqs(fu);
889 pr_info(
"Using the UAS protocol\n");
894 usb_ep_disable(fu->
ep_cmd);
896 usb_ep_disable(fu->
ep_out);
898 usb_ep_disable(fu->
ep_in);
903 static int get_cmd_dir(
const unsigned char *
cdb)
957 pr_warn(
"target: Unknown data direction for SCSI Opcode "
967 struct se_cmd *se_cmd = &cmd->
se_cmd;
970 pr_err(
"%s() state %d transfer failed\n", __func__, cmd->
state);
985 usbg_cleanup_cmd(cmd);
990 struct se_cmd *se_cmd = &cmd->
se_cmd;
1006 req->
complete = usbg_data_write_cmpl;
1012 static int usbg_send_status_response(
struct se_cmd *se_cmd)
1019 return bot_send_status_response(cmd);
1021 return uasp_send_status_response(cmd);
1024 static int usbg_send_write_request(
struct se_cmd *se_cmd)
1031 return bot_send_write_request(cmd);
1033 return uasp_send_write_request(cmd);
1036 static int usbg_send_read_response(
struct se_cmd *se_cmd)
1043 return bot_send_read_response(cmd);
1045 return uasp_send_read_response(cmd);
1051 struct se_cmd *se_cmd;
1059 dir = get_cmd_dir(cmd->
cmd_buf);
1078 usbg_cleanup_cmd(cmd);
1081 static int usbg_submit_command(
struct f_uas *fu,
1082 void *
cmdbuf,
unsigned int len)
1087 struct se_cmd *se_cmd;
1104 kref_init(&cmd->
ref);
1105 kref_get(&cmd->
ref);
1108 cmd_len = (cmd_iu->
len & ~0x3) + 16;
1116 if (cmd->
tag > UASP_SS_EP_COMP_NUM_STREAMS)
1128 pr_err(
"Missing nexus, ignoring command\n");
1164 static void bot_cmd_work(
struct work_struct *work)
1167 struct se_cmd *se_cmd;
1175 dir = get_cmd_dir(cmd->
cmd_buf);
1194 usbg_cleanup_cmd(cmd);
1197 static int bot_submit_command(
struct f_uas *fu,
1198 void *cmdbuf,
unsigned int len)
1203 struct se_cmd *se_cmd;
1209 pr_err(
"Wrong signature on CBW\n");
1213 pr_err(
"Wrong length for CBW\n");
1218 if (cmd_len < 1 || cmd_len > 16)
1228 kref_init(&cmd->
ref);
1229 kref_get(&cmd->
ref);
1239 pr_err(
"Missing nexus, ignoring command\n");
1272 static char *usbg_get_fabric_name(
void)
1274 return "usb_gadget";
1315 static u32 usbg_get_pr_transport_id(
1338 static u32 usbg_get_pr_transport_id_len(
1360 static char *usbg_parse_pr_out_transport_id(
1364 char **port_nexus_ptr)
1394 static void usbg_release_fabric_acl(
1408 static void usbg_cmd_release(
struct kref *ref)
1416 static void usbg_release_cmd(
struct se_cmd *se_cmd)
1425 static int usbg_shutdown_session(
struct se_session *se_sess)
1430 static void usbg_close_session(
struct se_session *se_sess)
1443 static int usbg_write_pending_status(
struct se_cmd *se_cmd)
1448 static void usbg_set_default_node_attrs(
struct se_node_acl *nacl)
1453 static u32 usbg_get_task_tag(
struct se_cmd *se_cmd)
1465 static int usbg_get_cmd_state(
struct se_cmd *se_cmd)
1470 static int usbg_queue_tm_rsp(
struct se_cmd *se_cmd)
1475 static const char *usbg_check_wwn(
const char *
name)
1480 n =
strstr(name,
"naa.");
1499 const char *wnn_name;
1501 wnn_name = usbg_check_wwn(name);
1504 se_nacl_new = usbg_alloc_fabric_acl(se_tpg);
1515 if (IS_ERR(se_nacl)) {
1516 usbg_release_fabric_acl(se_tpg, se_nacl_new);
1528 static void usbg_drop_nodeacl(
struct se_node_acl *se_acl)
1549 if (
strstr(name,
"tpgt_") != name)
1551 if (kstrtoul(name + 5, 0, &tpgt) || tpgt >
UINT_MAX)
1553 if (the_only_tpg_I_currently_have) {
1554 pr_err(
"Until the gadget framework can't handle multiple\n");
1555 pr_err(
"gadgets, you can't do this here.\n");
1556 return ERR_PTR(-
EBUSY);
1583 the_only_tpg_I_currently_have = tpg;
1595 the_only_tpg_I_currently_have =
NULL;
1598 static struct se_wwn *usbg_make_tport(
1604 const char *wnn_name;
1607 wnn_name = usbg_check_wwn(name);
1621 static void usbg_drop_tport(
struct se_wwn *wwn)
1631 static ssize_t usbg_wwn_show_attr_version(
1635 return sprintf(page,
"usb-gadget fabric module\n");
1640 &usbg_wwn_version.attr,
1644 static ssize_t tcm_usbg_tpg_show_enable(
1653 static int usbg_attach(
struct usbg_tpg *);
1654 static void usbg_detach(
struct usbg_tpg *);
1656 static ssize_t tcm_usbg_tpg_store_enable(
1665 ret = kstrtoul(page, 0, &op);
1677 ret = usbg_attach(tpg);
1689 static ssize_t tcm_usbg_tpg_show_nexus(
1704 tv_nexus->
tvn_se_sess->se_node_acl->initiatorname);
1710 static int tcm_usbg_make_nexus(
struct usbg_tpg *tpg,
char *name)
1719 pr_debug(
"tpg->tpg_nexus already exists\n");
1725 tv_nexus = kzalloc(
sizeof(*tv_nexus),
GFP_KERNEL);
1727 pr_err(
"Unable to allocate struct tcm_vhost_nexus\n");
1742 pr_debug(
"core_tpg_check_initiator_node_acl() failed"
1765 static int tcm_usbg_drop_nexus(
struct usbg_tpg *tpg)
1782 pr_err(
"Unable to remove Host I_T Nexus with"
1783 " active TPG port count: %d\n",
1788 pr_debug(
"Removing I_T Nexus to Initiator Port: %s\n",
1789 tv_nexus->
tvn_se_sess->se_node_acl->initiatorname);
1802 static ssize_t tcm_usbg_tpg_store_nexus(
1811 if (!
strncmp(page,
"NULL", 4)) {
1812 ret = tcm_usbg_drop_nexus(tpg);
1813 return (!ret) ? count :
ret;
1816 pr_err(
"Emulated NAA Sas Address: %s, exceeds"
1822 ptr =
strstr(i_port,
"naa.");
1824 pr_err(
"Missing 'naa.' prefix\n");
1828 if (i_port[
strlen(i_port) - 1] ==
'\n')
1829 i_port[
strlen(i_port) - 1] =
'\0';
1831 ret = tcm_usbg_make_nexus(tpg, &i_port[4]);
1839 &tcm_usbg_tpg_enable.attr,
1840 &tcm_usbg_tpg_nexus.attr,
1862 static int usbg_check_stop_free(
struct se_cmd *se_cmd)
1867 kref_put(&cmd->
ref, usbg_cmd_release);
1872 .get_fabric_name = usbg_get_fabric_name,
1873 .get_fabric_proto_ident = usbg_get_fabric_proto_ident,
1874 .tpg_get_wwn = usbg_get_fabric_wwn,
1875 .tpg_get_tag = usbg_get_tag,
1876 .tpg_get_default_depth = usbg_get_default_depth,
1877 .tpg_get_pr_transport_id = usbg_get_pr_transport_id,
1878 .tpg_get_pr_transport_id_len = usbg_get_pr_transport_id_len,
1879 .tpg_parse_pr_out_transport_id = usbg_parse_pr_out_transport_id,
1880 .tpg_check_demo_mode = usbg_check_true,
1881 .tpg_check_demo_mode_cache = usbg_check_false,
1882 .tpg_check_demo_mode_write_protect = usbg_check_false,
1883 .tpg_check_prod_mode_write_protect = usbg_check_false,
1884 .tpg_alloc_fabric_acl = usbg_alloc_fabric_acl,
1885 .tpg_release_fabric_acl = usbg_release_fabric_acl,
1886 .tpg_get_inst_index = usbg_tpg_get_inst_index,
1887 .release_cmd = usbg_release_cmd,
1888 .shutdown_session = usbg_shutdown_session,
1889 .close_session = usbg_close_session,
1890 .sess_get_index = usbg_sess_get_index,
1891 .sess_get_initiator_sid =
NULL,
1892 .write_pending = usbg_send_write_request,
1893 .write_pending_status = usbg_write_pending_status,
1894 .set_default_node_attributes = usbg_set_default_node_attrs,
1895 .get_task_tag = usbg_get_task_tag,
1896 .get_cmd_state = usbg_get_cmd_state,
1897 .queue_data_in = usbg_send_read_response,
1898 .queue_status = usbg_send_status_response,
1899 .queue_tm_rsp = usbg_queue_tm_rsp,
1900 .check_stop_free = usbg_check_stop_free,
1902 .fabric_make_wwn = usbg_make_tport,
1903 .fabric_drop_wwn = usbg_drop_tport,
1904 .fabric_make_tpg = usbg_make_tpg,
1905 .fabric_drop_tpg = usbg_drop_tpg,
1906 .fabric_post_link = usbg_port_link,
1907 .fabric_pre_unlink = usbg_port_unlink,
1908 .fabric_make_np =
NULL,
1909 .fabric_drop_np =
NULL,
1910 .fabric_make_nodeacl = usbg_make_nodeacl,
1911 .fabric_drop_nodeacl = usbg_drop_nodeacl,
1914 static int usbg_register_configfs(
void)
1920 if (IS_ERR(fabric)) {
1922 return PTR_ERR(fabric);
1925 fabric->
tf_ops = usbg_ops;
1926 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = usbg_wwn_attrs;
1927 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = usbg_base_attrs;
1938 " for usb-gadget\n");
1941 usbg_fabric_configfs = fabric;
1945 static void usbg_deregister_configfs(
void)
1947 if (!(usbg_fabric_configfs))
1951 usbg_fabric_configfs =
NULL;
1957 .bLength =
sizeof(bot_intf_desc),
1967 .bLength =
sizeof(uasp_intf_desc),
1992 .bLength =
sizeof(uasp_bi_pipe_desc),
2006 .bLength =
sizeof(uasp_bi_ep_comp_desc),
2014 .bLength =
sizeof(bot_bi_ep_comp_desc),
2035 .bLength =
sizeof(uasp_bo_pipe_desc),
2049 .bLength =
sizeof(uasp_bo_ep_comp_desc),
2055 .bLength =
sizeof(bot_bo_ep_comp_desc),
2075 .bLength =
sizeof(uasp_status_pipe_desc),
2089 .bLength =
sizeof(uasp_status_in_ep_comp_desc),
2110 .bLength =
sizeof(uasp_cmd_pipe_desc),
2124 .bLength =
sizeof(uasp_cmd_comp_desc),
2184 #define UAS_VENDOR_ID 0x0525
2185 #define UAS_PRODUCT_ID 0xa4a5
2188 .bLength =
sizeof(usbg_device_desc),
2194 .bNumConfigurations = 1,
2197 static struct usb_string usbg_us_strings[] = {
2209 .strings = usbg_us_strings,
2223 .label =
"Linux Target",
2224 .bConfigurationValue = 1,
2228 static void give_back_ep(
struct usb_ep **pep)
2230 struct usb_ep *ep = *pep;
2238 struct f_uas *fu = to_f_uas(f);
2251 &uasp_bi_ep_comp_desc);
2259 &uasp_bo_ep_comp_desc);
2266 &uasp_status_in_ep_comp_desc);
2273 &uasp_cmd_comp_desc);
2294 pr_err(
"Can't claim all required eps\n");
2296 give_back_ep(&fu->
ep_in);
2297 give_back_ep(&fu->
ep_out);
2299 give_back_ep(&fu->
ep_cmd);
2305 struct f_uas *fu = to_f_uas(f);
2316 static void usbg_delayed_set_alt(
struct work_struct *wq)
2321 int alt = work->
alt;
2328 uasp_cleanup_old_alt(fu);
2337 static int usbg_set_alt(
struct usb_function *f,
unsigned intf,
unsigned alt)
2339 struct f_uas *fu = to_f_uas(f);
2358 struct f_uas *fu = to_f_uas(f);
2361 uasp_cleanup_old_alt(fu);
2370 struct f_uas *fu = to_f_uas(f);
2375 return usbg_bot_setup(f, ctrl);
2386 fu->
function.name =
"Target Function";
2387 fu->
function.descriptors = uasp_fs_function_desc;
2388 fu->
function.hs_descriptors = uasp_hs_function_desc;
2389 fu->
function.ss_descriptors = uasp_ss_function_desc;
2392 fu->
function.set_alt = usbg_set_alt;
2394 fu->
function.disable = usbg_disable;
2436 .dev = &usbg_device_desc,
2437 .strings = usbg_strings,
2439 .bind = usb_target_bind,
2440 .unbind = guas_unbind,
2443 static int usbg_attach(
struct usbg_tpg *tpg)
2448 static void usbg_detach(
struct usbg_tpg *tpg)
2453 static int __init usb_target_gadget_init(
void)
2457 ret = usbg_register_configfs();
2462 static void __exit usb_target_gadget_exit(
void)
2464 usbg_deregister_configfs();