f_tcm.c 54 KB

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