tcm_usb_gadget.c 54 KB

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