f_tcm.c 56 KB

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