scsi.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  1. /*******************************************************************************
  2. * Vhost kernel TCM fabric driver for virtio SCSI initiators
  3. *
  4. * (C) Copyright 2010-2012 RisingTide Systems LLC.
  5. * (C) Copyright 2010-2012 IBM Corp.
  6. *
  7. * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
  8. *
  9. * Authors: Nicholas A. Bellinger <nab@risingtidesystems.com>
  10. * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. ****************************************************************************/
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <generated/utsrelease.h>
  26. #include <linux/utsname.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/kthread.h>
  30. #include <linux/types.h>
  31. #include <linux/string.h>
  32. #include <linux/configfs.h>
  33. #include <linux/ctype.h>
  34. #include <linux/compat.h>
  35. #include <linux/eventfd.h>
  36. #include <linux/fs.h>
  37. #include <linux/miscdevice.h>
  38. #include <asm/unaligned.h>
  39. #include <scsi/scsi.h>
  40. #include <scsi/scsi_tcq.h>
  41. #include <target/target_core_base.h>
  42. #include <target/target_core_fabric.h>
  43. #include <target/target_core_fabric_configfs.h>
  44. #include <target/target_core_configfs.h>
  45. #include <target/configfs_macros.h>
  46. #include <linux/vhost.h>
  47. #include <linux/virtio_scsi.h>
  48. #include <linux/llist.h>
  49. #include <linux/bitmap.h>
  50. #include <linux/percpu_ida.h>
  51. #include "vhost.h"
  52. #define TCM_VHOST_VERSION "v0.1"
  53. #define TCM_VHOST_NAMELEN 256
  54. #define TCM_VHOST_MAX_CDB_SIZE 32
  55. #define TCM_VHOST_DEFAULT_TAGS 256
  56. #define TCM_VHOST_PREALLOC_SGLS 2048
  57. #define TCM_VHOST_PREALLOC_PAGES 2048
  58. struct vhost_scsi_inflight {
  59. /* Wait for the flush operation to finish */
  60. struct completion comp;
  61. /* Refcount for the inflight reqs */
  62. struct kref kref;
  63. };
  64. struct tcm_vhost_cmd {
  65. /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
  66. int tvc_vq_desc;
  67. /* virtio-scsi initiator task attribute */
  68. int tvc_task_attr;
  69. /* virtio-scsi initiator data direction */
  70. enum dma_data_direction tvc_data_direction;
  71. /* Expected data transfer length from virtio-scsi header */
  72. u32 tvc_exp_data_len;
  73. /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
  74. u64 tvc_tag;
  75. /* The number of scatterlists associated with this cmd */
  76. u32 tvc_sgl_count;
  77. /* Saved unpacked SCSI LUN for tcm_vhost_submission_work() */
  78. u32 tvc_lun;
  79. /* Pointer to the SGL formatted memory from virtio-scsi */
  80. struct scatterlist *tvc_sgl;
  81. struct page **tvc_upages;
  82. /* Pointer to response */
  83. struct virtio_scsi_cmd_resp __user *tvc_resp;
  84. /* Pointer to vhost_scsi for our device */
  85. struct vhost_scsi *tvc_vhost;
  86. /* Pointer to vhost_virtqueue for the cmd */
  87. struct vhost_virtqueue *tvc_vq;
  88. /* Pointer to vhost nexus memory */
  89. struct tcm_vhost_nexus *tvc_nexus;
  90. /* The TCM I/O descriptor that is accessed via container_of() */
  91. struct se_cmd tvc_se_cmd;
  92. /* work item used for cmwq dispatch to tcm_vhost_submission_work() */
  93. struct work_struct work;
  94. /* Copy of the incoming SCSI command descriptor block (CDB) */
  95. unsigned char tvc_cdb[TCM_VHOST_MAX_CDB_SIZE];
  96. /* Sense buffer that will be mapped into outgoing status */
  97. unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
  98. /* Completed commands list, serviced from vhost worker thread */
  99. struct llist_node tvc_completion_list;
  100. /* Used to track inflight cmd */
  101. struct vhost_scsi_inflight *inflight;
  102. };
  103. struct tcm_vhost_nexus {
  104. /* Pointer to TCM session for I_T Nexus */
  105. struct se_session *tvn_se_sess;
  106. };
  107. struct tcm_vhost_nacl {
  108. /* Binary World Wide unique Port Name for Vhost Initiator port */
  109. u64 iport_wwpn;
  110. /* ASCII formatted WWPN for Sas Initiator port */
  111. char iport_name[TCM_VHOST_NAMELEN];
  112. /* Returned by tcm_vhost_make_nodeacl() */
  113. struct se_node_acl se_node_acl;
  114. };
  115. struct tcm_vhost_tpg {
  116. /* Vhost port target portal group tag for TCM */
  117. u16 tport_tpgt;
  118. /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
  119. int tv_tpg_port_count;
  120. /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
  121. int tv_tpg_vhost_count;
  122. /* list for tcm_vhost_list */
  123. struct list_head tv_tpg_list;
  124. /* Used to protect access for tpg_nexus */
  125. struct mutex tv_tpg_mutex;
  126. /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
  127. struct tcm_vhost_nexus *tpg_nexus;
  128. /* Pointer back to tcm_vhost_tport */
  129. struct tcm_vhost_tport *tport;
  130. /* Returned by tcm_vhost_make_tpg() */
  131. struct se_portal_group se_tpg;
  132. /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
  133. struct vhost_scsi *vhost_scsi;
  134. };
  135. struct tcm_vhost_tport {
  136. /* SCSI protocol the tport is providing */
  137. u8 tport_proto_id;
  138. /* Binary World Wide unique Port Name for Vhost Target port */
  139. u64 tport_wwpn;
  140. /* ASCII formatted WWPN for Vhost Target port */
  141. char tport_name[TCM_VHOST_NAMELEN];
  142. /* Returned by tcm_vhost_make_tport() */
  143. struct se_wwn tport_wwn;
  144. };
  145. struct tcm_vhost_evt {
  146. /* event to be sent to guest */
  147. struct virtio_scsi_event event;
  148. /* event list, serviced from vhost worker thread */
  149. struct llist_node list;
  150. };
  151. enum {
  152. VHOST_SCSI_VQ_CTL = 0,
  153. VHOST_SCSI_VQ_EVT = 1,
  154. VHOST_SCSI_VQ_IO = 2,
  155. };
  156. enum {
  157. VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG)
  158. };
  159. #define VHOST_SCSI_MAX_TARGET 256
  160. #define VHOST_SCSI_MAX_VQ 128
  161. #define VHOST_SCSI_MAX_EVENT 128
  162. struct vhost_scsi_virtqueue {
  163. struct vhost_virtqueue vq;
  164. /*
  165. * Reference counting for inflight reqs, used for flush operation. At
  166. * each time, one reference tracks new commands submitted, while we
  167. * wait for another one to reach 0.
  168. */
  169. struct vhost_scsi_inflight inflights[2];
  170. /*
  171. * Indicate current inflight in use, protected by vq->mutex.
  172. * Writers must also take dev mutex and flush under it.
  173. */
  174. int inflight_idx;
  175. };
  176. struct vhost_scsi {
  177. /* Protected by vhost_scsi->dev.mutex */
  178. struct tcm_vhost_tpg **vs_tpg;
  179. char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
  180. struct vhost_dev dev;
  181. struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
  182. struct vhost_work vs_completion_work; /* cmd completion work item */
  183. struct llist_head vs_completion_list; /* cmd completion queue */
  184. struct vhost_work vs_event_work; /* evt injection work item */
  185. struct llist_head vs_event_list; /* evt injection queue */
  186. bool vs_events_missed; /* any missed events, protected by vq->mutex */
  187. int vs_events_nr; /* num of pending events, protected by vq->mutex */
  188. };
  189. /* Local pointer to allocated TCM configfs fabric module */
  190. static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
  191. static struct workqueue_struct *tcm_vhost_workqueue;
  192. /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
  193. static DEFINE_MUTEX(tcm_vhost_mutex);
  194. static LIST_HEAD(tcm_vhost_list);
  195. static int iov_num_pages(struct iovec *iov)
  196. {
  197. return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
  198. ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
  199. }
  200. static void tcm_vhost_done_inflight(struct kref *kref)
  201. {
  202. struct vhost_scsi_inflight *inflight;
  203. inflight = container_of(kref, struct vhost_scsi_inflight, kref);
  204. complete(&inflight->comp);
  205. }
  206. static void tcm_vhost_init_inflight(struct vhost_scsi *vs,
  207. struct vhost_scsi_inflight *old_inflight[])
  208. {
  209. struct vhost_scsi_inflight *new_inflight;
  210. struct vhost_virtqueue *vq;
  211. int idx, i;
  212. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  213. vq = &vs->vqs[i].vq;
  214. mutex_lock(&vq->mutex);
  215. /* store old infight */
  216. idx = vs->vqs[i].inflight_idx;
  217. if (old_inflight)
  218. old_inflight[i] = &vs->vqs[i].inflights[idx];
  219. /* setup new infight */
  220. vs->vqs[i].inflight_idx = idx ^ 1;
  221. new_inflight = &vs->vqs[i].inflights[idx ^ 1];
  222. kref_init(&new_inflight->kref);
  223. init_completion(&new_inflight->comp);
  224. mutex_unlock(&vq->mutex);
  225. }
  226. }
  227. static struct vhost_scsi_inflight *
  228. tcm_vhost_get_inflight(struct vhost_virtqueue *vq)
  229. {
  230. struct vhost_scsi_inflight *inflight;
  231. struct vhost_scsi_virtqueue *svq;
  232. svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
  233. inflight = &svq->inflights[svq->inflight_idx];
  234. kref_get(&inflight->kref);
  235. return inflight;
  236. }
  237. static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight)
  238. {
  239. kref_put(&inflight->kref, tcm_vhost_done_inflight);
  240. }
  241. static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
  242. {
  243. return 1;
  244. }
  245. static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
  246. {
  247. return 0;
  248. }
  249. static char *tcm_vhost_get_fabric_name(void)
  250. {
  251. return "vhost";
  252. }
  253. static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  254. {
  255. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  256. struct tcm_vhost_tpg, se_tpg);
  257. struct tcm_vhost_tport *tport = tpg->tport;
  258. switch (tport->tport_proto_id) {
  259. case SCSI_PROTOCOL_SAS:
  260. return sas_get_fabric_proto_ident(se_tpg);
  261. case SCSI_PROTOCOL_FCP:
  262. return fc_get_fabric_proto_ident(se_tpg);
  263. case SCSI_PROTOCOL_ISCSI:
  264. return iscsi_get_fabric_proto_ident(se_tpg);
  265. default:
  266. pr_err("Unknown tport_proto_id: 0x%02x, using"
  267. " SAS emulation\n", tport->tport_proto_id);
  268. break;
  269. }
  270. return sas_get_fabric_proto_ident(se_tpg);
  271. }
  272. static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
  273. {
  274. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  275. struct tcm_vhost_tpg, se_tpg);
  276. struct tcm_vhost_tport *tport = tpg->tport;
  277. return &tport->tport_name[0];
  278. }
  279. static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
  280. {
  281. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  282. struct tcm_vhost_tpg, se_tpg);
  283. return tpg->tport_tpgt;
  284. }
  285. static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
  286. {
  287. return 1;
  288. }
  289. static u32
  290. tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
  291. struct se_node_acl *se_nacl,
  292. struct t10_pr_registration *pr_reg,
  293. int *format_code,
  294. unsigned char *buf)
  295. {
  296. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  297. struct tcm_vhost_tpg, se_tpg);
  298. struct tcm_vhost_tport *tport = tpg->tport;
  299. switch (tport->tport_proto_id) {
  300. case SCSI_PROTOCOL_SAS:
  301. return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  302. format_code, buf);
  303. case SCSI_PROTOCOL_FCP:
  304. return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  305. format_code, buf);
  306. case SCSI_PROTOCOL_ISCSI:
  307. return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  308. format_code, buf);
  309. default:
  310. pr_err("Unknown tport_proto_id: 0x%02x, using"
  311. " SAS emulation\n", tport->tport_proto_id);
  312. break;
  313. }
  314. return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  315. format_code, buf);
  316. }
  317. static u32
  318. tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
  319. struct se_node_acl *se_nacl,
  320. struct t10_pr_registration *pr_reg,
  321. int *format_code)
  322. {
  323. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  324. struct tcm_vhost_tpg, se_tpg);
  325. struct tcm_vhost_tport *tport = tpg->tport;
  326. switch (tport->tport_proto_id) {
  327. case SCSI_PROTOCOL_SAS:
  328. return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  329. format_code);
  330. case SCSI_PROTOCOL_FCP:
  331. return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  332. format_code);
  333. case SCSI_PROTOCOL_ISCSI:
  334. return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  335. format_code);
  336. default:
  337. pr_err("Unknown tport_proto_id: 0x%02x, using"
  338. " SAS emulation\n", tport->tport_proto_id);
  339. break;
  340. }
  341. return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  342. format_code);
  343. }
  344. static char *
  345. tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
  346. const char *buf,
  347. u32 *out_tid_len,
  348. char **port_nexus_ptr)
  349. {
  350. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  351. struct tcm_vhost_tpg, se_tpg);
  352. struct tcm_vhost_tport *tport = tpg->tport;
  353. switch (tport->tport_proto_id) {
  354. case SCSI_PROTOCOL_SAS:
  355. return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  356. port_nexus_ptr);
  357. case SCSI_PROTOCOL_FCP:
  358. return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  359. port_nexus_ptr);
  360. case SCSI_PROTOCOL_ISCSI:
  361. return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  362. port_nexus_ptr);
  363. default:
  364. pr_err("Unknown tport_proto_id: 0x%02x, using"
  365. " SAS emulation\n", tport->tport_proto_id);
  366. break;
  367. }
  368. return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  369. port_nexus_ptr);
  370. }
  371. static struct se_node_acl *
  372. tcm_vhost_alloc_fabric_acl(struct se_portal_group *se_tpg)
  373. {
  374. struct tcm_vhost_nacl *nacl;
  375. nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
  376. if (!nacl) {
  377. pr_err("Unable to allocate struct tcm_vhost_nacl\n");
  378. return NULL;
  379. }
  380. return &nacl->se_node_acl;
  381. }
  382. static void
  383. tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
  384. struct se_node_acl *se_nacl)
  385. {
  386. struct tcm_vhost_nacl *nacl = container_of(se_nacl,
  387. struct tcm_vhost_nacl, se_node_acl);
  388. kfree(nacl);
  389. }
  390. static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
  391. {
  392. return 1;
  393. }
  394. static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
  395. {
  396. struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
  397. struct tcm_vhost_cmd, tvc_se_cmd);
  398. struct se_session *se_sess = se_cmd->se_sess;
  399. if (tv_cmd->tvc_sgl_count) {
  400. u32 i;
  401. for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
  402. put_page(sg_page(&tv_cmd->tvc_sgl[i]));
  403. }
  404. tcm_vhost_put_inflight(tv_cmd->inflight);
  405. percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
  406. }
  407. static int tcm_vhost_shutdown_session(struct se_session *se_sess)
  408. {
  409. return 0;
  410. }
  411. static void tcm_vhost_close_session(struct se_session *se_sess)
  412. {
  413. return;
  414. }
  415. static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
  416. {
  417. return 0;
  418. }
  419. static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
  420. {
  421. /* Go ahead and process the write immediately */
  422. target_execute_cmd(se_cmd);
  423. return 0;
  424. }
  425. static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
  426. {
  427. return 0;
  428. }
  429. static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
  430. {
  431. return;
  432. }
  433. static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
  434. {
  435. return 0;
  436. }
  437. static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
  438. {
  439. return 0;
  440. }
  441. static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *cmd)
  442. {
  443. struct vhost_scsi *vs = cmd->tvc_vhost;
  444. llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
  445. vhost_work_queue(&vs->dev, &vs->vs_completion_work);
  446. }
  447. static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
  448. {
  449. struct tcm_vhost_cmd *cmd = container_of(se_cmd,
  450. struct tcm_vhost_cmd, tvc_se_cmd);
  451. vhost_scsi_complete_cmd(cmd);
  452. return 0;
  453. }
  454. static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
  455. {
  456. struct tcm_vhost_cmd *cmd = container_of(se_cmd,
  457. struct tcm_vhost_cmd, tvc_se_cmd);
  458. vhost_scsi_complete_cmd(cmd);
  459. return 0;
  460. }
  461. static void tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
  462. {
  463. return;
  464. }
  465. static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
  466. {
  467. vs->vs_events_nr--;
  468. kfree(evt);
  469. }
  470. static struct tcm_vhost_evt *
  471. tcm_vhost_allocate_evt(struct vhost_scsi *vs,
  472. u32 event, u32 reason)
  473. {
  474. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  475. struct tcm_vhost_evt *evt;
  476. if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
  477. vs->vs_events_missed = true;
  478. return NULL;
  479. }
  480. evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  481. if (!evt) {
  482. vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
  483. vs->vs_events_missed = true;
  484. return NULL;
  485. }
  486. evt->event.event = event;
  487. evt->event.reason = reason;
  488. vs->vs_events_nr++;
  489. return evt;
  490. }
  491. static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *cmd)
  492. {
  493. struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
  494. /* TODO locking against target/backend threads? */
  495. transport_generic_free_cmd(se_cmd, 0);
  496. }
  497. static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
  498. {
  499. return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
  500. }
  501. static void
  502. tcm_vhost_do_evt_work(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
  503. {
  504. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  505. struct virtio_scsi_event *event = &evt->event;
  506. struct virtio_scsi_event __user *eventp;
  507. unsigned out, in;
  508. int head, ret;
  509. if (!vq->private_data) {
  510. vs->vs_events_missed = true;
  511. return;
  512. }
  513. again:
  514. vhost_disable_notify(&vs->dev, vq);
  515. head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
  516. ARRAY_SIZE(vq->iov), &out, &in,
  517. NULL, NULL);
  518. if (head < 0) {
  519. vs->vs_events_missed = true;
  520. return;
  521. }
  522. if (head == vq->num) {
  523. if (vhost_enable_notify(&vs->dev, vq))
  524. goto again;
  525. vs->vs_events_missed = true;
  526. return;
  527. }
  528. if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
  529. vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
  530. vq->iov[out].iov_len);
  531. vs->vs_events_missed = true;
  532. return;
  533. }
  534. if (vs->vs_events_missed) {
  535. event->event |= VIRTIO_SCSI_T_EVENTS_MISSED;
  536. vs->vs_events_missed = false;
  537. }
  538. eventp = vq->iov[out].iov_base;
  539. ret = __copy_to_user(eventp, event, sizeof(*event));
  540. if (!ret)
  541. vhost_add_used_and_signal(&vs->dev, vq, head, 0);
  542. else
  543. vq_err(vq, "Faulted on tcm_vhost_send_event\n");
  544. }
  545. static void tcm_vhost_evt_work(struct vhost_work *work)
  546. {
  547. struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
  548. vs_event_work);
  549. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  550. struct tcm_vhost_evt *evt;
  551. struct llist_node *llnode;
  552. mutex_lock(&vq->mutex);
  553. llnode = llist_del_all(&vs->vs_event_list);
  554. while (llnode) {
  555. evt = llist_entry(llnode, struct tcm_vhost_evt, list);
  556. llnode = llist_next(llnode);
  557. tcm_vhost_do_evt_work(vs, evt);
  558. tcm_vhost_free_evt(vs, evt);
  559. }
  560. mutex_unlock(&vq->mutex);
  561. }
  562. /* Fill in status and signal that we are done processing this command
  563. *
  564. * This is scheduled in the vhost work queue so we are called with the owner
  565. * process mm and can access the vring.
  566. */
  567. static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
  568. {
  569. struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
  570. vs_completion_work);
  571. DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
  572. struct virtio_scsi_cmd_resp v_rsp;
  573. struct tcm_vhost_cmd *cmd;
  574. struct llist_node *llnode;
  575. struct se_cmd *se_cmd;
  576. int ret, vq;
  577. bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
  578. llnode = llist_del_all(&vs->vs_completion_list);
  579. while (llnode) {
  580. cmd = llist_entry(llnode, struct tcm_vhost_cmd,
  581. tvc_completion_list);
  582. llnode = llist_next(llnode);
  583. se_cmd = &cmd->tvc_se_cmd;
  584. pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
  585. cmd, se_cmd->residual_count, se_cmd->scsi_status);
  586. memset(&v_rsp, 0, sizeof(v_rsp));
  587. v_rsp.resid = se_cmd->residual_count;
  588. /* TODO is status_qualifier field needed? */
  589. v_rsp.status = se_cmd->scsi_status;
  590. v_rsp.sense_len = se_cmd->scsi_sense_length;
  591. memcpy(v_rsp.sense, cmd->tvc_sense_buf,
  592. v_rsp.sense_len);
  593. ret = copy_to_user(cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
  594. if (likely(ret == 0)) {
  595. struct vhost_scsi_virtqueue *q;
  596. vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
  597. q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
  598. vq = q - vs->vqs;
  599. __set_bit(vq, signal);
  600. } else
  601. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  602. vhost_scsi_free_cmd(cmd);
  603. }
  604. vq = -1;
  605. while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
  606. < VHOST_SCSI_MAX_VQ)
  607. vhost_signal(&vs->dev, &vs->vqs[vq].vq);
  608. }
  609. static struct tcm_vhost_cmd *
  610. vhost_scsi_get_tag(struct vhost_virtqueue *vq,
  611. struct tcm_vhost_tpg *tpg,
  612. struct virtio_scsi_cmd_req *v_req,
  613. u32 exp_data_len,
  614. int data_direction)
  615. {
  616. struct tcm_vhost_cmd *cmd;
  617. struct tcm_vhost_nexus *tv_nexus;
  618. struct se_session *se_sess;
  619. struct scatterlist *sg;
  620. struct page **pages;
  621. int tag;
  622. tv_nexus = tpg->tpg_nexus;
  623. if (!tv_nexus) {
  624. pr_err("Unable to locate active struct tcm_vhost_nexus\n");
  625. return ERR_PTR(-EIO);
  626. }
  627. se_sess = tv_nexus->tvn_se_sess;
  628. tag = percpu_ida_alloc(&se_sess->sess_tag_pool, GFP_KERNEL);
  629. cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[tag];
  630. sg = cmd->tvc_sgl;
  631. pages = cmd->tvc_upages;
  632. memset(cmd, 0, sizeof(struct tcm_vhost_cmd));
  633. cmd->tvc_sgl = sg;
  634. cmd->tvc_upages = pages;
  635. cmd->tvc_se_cmd.map_tag = tag;
  636. cmd->tvc_tag = v_req->tag;
  637. cmd->tvc_task_attr = v_req->task_attr;
  638. cmd->tvc_exp_data_len = exp_data_len;
  639. cmd->tvc_data_direction = data_direction;
  640. cmd->tvc_nexus = tv_nexus;
  641. cmd->inflight = tcm_vhost_get_inflight(vq);
  642. return cmd;
  643. }
  644. /*
  645. * Map a user memory range into a scatterlist
  646. *
  647. * Returns the number of scatterlist entries used or -errno on error.
  648. */
  649. static int
  650. vhost_scsi_map_to_sgl(struct tcm_vhost_cmd *tv_cmd,
  651. struct scatterlist *sgl,
  652. unsigned int sgl_count,
  653. struct iovec *iov,
  654. int write)
  655. {
  656. unsigned int npages = 0, pages_nr, offset, nbytes;
  657. struct scatterlist *sg = sgl;
  658. void __user *ptr = iov->iov_base;
  659. size_t len = iov->iov_len;
  660. struct page **pages;
  661. int ret, i;
  662. if (sgl_count > TCM_VHOST_PREALLOC_SGLS) {
  663. pr_err("vhost_scsi_map_to_sgl() psgl_count: %u greater than"
  664. " preallocated TCM_VHOST_PREALLOC_SGLS: %u\n",
  665. sgl_count, TCM_VHOST_PREALLOC_SGLS);
  666. return -ENOBUFS;
  667. }
  668. pages_nr = iov_num_pages(iov);
  669. if (pages_nr > sgl_count)
  670. return -ENOBUFS;
  671. if (pages_nr > TCM_VHOST_PREALLOC_PAGES) {
  672. pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
  673. " preallocated TCM_VHOST_PREALLOC_PAGES: %u\n",
  674. pages_nr, TCM_VHOST_PREALLOC_PAGES);
  675. return -ENOBUFS;
  676. }
  677. pages = tv_cmd->tvc_upages;
  678. ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
  679. /* No pages were pinned */
  680. if (ret < 0)
  681. goto out;
  682. /* Less pages pinned than wanted */
  683. if (ret != pages_nr) {
  684. for (i = 0; i < ret; i++)
  685. put_page(pages[i]);
  686. ret = -EFAULT;
  687. goto out;
  688. }
  689. while (len > 0) {
  690. offset = (uintptr_t)ptr & ~PAGE_MASK;
  691. nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
  692. sg_set_page(sg, pages[npages], nbytes, offset);
  693. ptr += nbytes;
  694. len -= nbytes;
  695. sg++;
  696. npages++;
  697. }
  698. out:
  699. return ret;
  700. }
  701. static int
  702. vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *cmd,
  703. struct iovec *iov,
  704. unsigned int niov,
  705. int write)
  706. {
  707. int ret;
  708. unsigned int i;
  709. u32 sgl_count;
  710. struct scatterlist *sg;
  711. /*
  712. * Find out how long sglist needs to be
  713. */
  714. sgl_count = 0;
  715. for (i = 0; i < niov; i++)
  716. sgl_count += iov_num_pages(&iov[i]);
  717. /* TODO overflow checking */
  718. sg = cmd->tvc_sgl;
  719. pr_debug("%s sg %p sgl_count %u\n", __func__, sg, sgl_count);
  720. sg_init_table(sg, sgl_count);
  721. cmd->tvc_sgl_count = sgl_count;
  722. pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
  723. for (i = 0; i < niov; i++) {
  724. ret = vhost_scsi_map_to_sgl(cmd, sg, sgl_count, &iov[i],
  725. write);
  726. if (ret < 0) {
  727. for (i = 0; i < cmd->tvc_sgl_count; i++)
  728. put_page(sg_page(&cmd->tvc_sgl[i]));
  729. cmd->tvc_sgl_count = 0;
  730. return ret;
  731. }
  732. sg += ret;
  733. sgl_count -= ret;
  734. }
  735. return 0;
  736. }
  737. static void tcm_vhost_submission_work(struct work_struct *work)
  738. {
  739. struct tcm_vhost_cmd *cmd =
  740. container_of(work, struct tcm_vhost_cmd, work);
  741. struct tcm_vhost_nexus *tv_nexus;
  742. struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
  743. struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
  744. int rc, sg_no_bidi = 0;
  745. if (cmd->tvc_sgl_count) {
  746. sg_ptr = cmd->tvc_sgl;
  747. /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
  748. #if 0
  749. if (se_cmd->se_cmd_flags & SCF_BIDI) {
  750. sg_bidi_ptr = NULL;
  751. sg_no_bidi = 0;
  752. }
  753. #endif
  754. } else {
  755. sg_ptr = NULL;
  756. }
  757. tv_nexus = cmd->tvc_nexus;
  758. rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
  759. cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
  760. cmd->tvc_lun, cmd->tvc_exp_data_len,
  761. cmd->tvc_task_attr, cmd->tvc_data_direction,
  762. TARGET_SCF_ACK_KREF, sg_ptr, cmd->tvc_sgl_count,
  763. sg_bidi_ptr, sg_no_bidi);
  764. if (rc < 0) {
  765. transport_send_check_condition_and_sense(se_cmd,
  766. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  767. transport_generic_free_cmd(se_cmd, 0);
  768. }
  769. }
  770. static void
  771. vhost_scsi_send_bad_target(struct vhost_scsi *vs,
  772. struct vhost_virtqueue *vq,
  773. int head, unsigned out)
  774. {
  775. struct virtio_scsi_cmd_resp __user *resp;
  776. struct virtio_scsi_cmd_resp rsp;
  777. int ret;
  778. memset(&rsp, 0, sizeof(rsp));
  779. rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
  780. resp = vq->iov[out].iov_base;
  781. ret = __copy_to_user(resp, &rsp, sizeof(rsp));
  782. if (!ret)
  783. vhost_add_used_and_signal(&vs->dev, vq, head, 0);
  784. else
  785. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  786. }
  787. static void
  788. vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
  789. {
  790. struct tcm_vhost_tpg **vs_tpg;
  791. struct virtio_scsi_cmd_req v_req;
  792. struct tcm_vhost_tpg *tpg;
  793. struct tcm_vhost_cmd *cmd;
  794. u32 exp_data_len, data_first, data_num, data_direction;
  795. unsigned out, in, i;
  796. int head, ret;
  797. u8 target;
  798. mutex_lock(&vq->mutex);
  799. /*
  800. * We can handle the vq only after the endpoint is setup by calling the
  801. * VHOST_SCSI_SET_ENDPOINT ioctl.
  802. */
  803. vs_tpg = vq->private_data;
  804. if (!vs_tpg)
  805. goto out;
  806. vhost_disable_notify(&vs->dev, vq);
  807. for (;;) {
  808. head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
  809. ARRAY_SIZE(vq->iov), &out, &in,
  810. NULL, NULL);
  811. pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
  812. head, out, in);
  813. /* On error, stop handling until the next kick. */
  814. if (unlikely(head < 0))
  815. break;
  816. /* Nothing new? Wait for eventfd to tell us they refilled. */
  817. if (head == vq->num) {
  818. if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
  819. vhost_disable_notify(&vs->dev, vq);
  820. continue;
  821. }
  822. break;
  823. }
  824. /* FIXME: BIDI operation */
  825. if (out == 1 && in == 1) {
  826. data_direction = DMA_NONE;
  827. data_first = 0;
  828. data_num = 0;
  829. } else if (out == 1 && in > 1) {
  830. data_direction = DMA_FROM_DEVICE;
  831. data_first = out + 1;
  832. data_num = in - 1;
  833. } else if (out > 1 && in == 1) {
  834. data_direction = DMA_TO_DEVICE;
  835. data_first = 1;
  836. data_num = out - 1;
  837. } else {
  838. vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
  839. out, in);
  840. break;
  841. }
  842. /*
  843. * Check for a sane resp buffer so we can report errors to
  844. * the guest.
  845. */
  846. if (unlikely(vq->iov[out].iov_len !=
  847. sizeof(struct virtio_scsi_cmd_resp))) {
  848. vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
  849. " bytes\n", vq->iov[out].iov_len);
  850. break;
  851. }
  852. if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
  853. vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
  854. " bytes\n", vq->iov[0].iov_len);
  855. break;
  856. }
  857. pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
  858. " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
  859. ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
  860. sizeof(v_req));
  861. if (unlikely(ret)) {
  862. vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
  863. break;
  864. }
  865. /* Extract the tpgt */
  866. target = v_req.lun[1];
  867. tpg = ACCESS_ONCE(vs_tpg[target]);
  868. /* Target does not exist, fail the request */
  869. if (unlikely(!tpg)) {
  870. vhost_scsi_send_bad_target(vs, vq, head, out);
  871. continue;
  872. }
  873. exp_data_len = 0;
  874. for (i = 0; i < data_num; i++)
  875. exp_data_len += vq->iov[data_first + i].iov_len;
  876. cmd = vhost_scsi_get_tag(vq, tpg, &v_req,
  877. exp_data_len, data_direction);
  878. if (IS_ERR(cmd)) {
  879. vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
  880. PTR_ERR(cmd));
  881. goto err_cmd;
  882. }
  883. pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
  884. ": %d\n", cmd, exp_data_len, data_direction);
  885. cmd->tvc_vhost = vs;
  886. cmd->tvc_vq = vq;
  887. cmd->tvc_resp = vq->iov[out].iov_base;
  888. /*
  889. * Copy in the recieved CDB descriptor into cmd->tvc_cdb
  890. * that will be used by tcm_vhost_new_cmd_map() and down into
  891. * target_setup_cmd_from_cdb()
  892. */
  893. memcpy(cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
  894. /*
  895. * Check that the recieved CDB size does not exceeded our
  896. * hardcoded max for tcm_vhost
  897. */
  898. /* TODO what if cdb was too small for varlen cdb header? */
  899. if (unlikely(scsi_command_size(cmd->tvc_cdb) >
  900. TCM_VHOST_MAX_CDB_SIZE)) {
  901. vq_err(vq, "Received SCSI CDB with command_size: %d that"
  902. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  903. scsi_command_size(cmd->tvc_cdb),
  904. TCM_VHOST_MAX_CDB_SIZE);
  905. goto err_free;
  906. }
  907. cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
  908. pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
  909. cmd->tvc_cdb[0], cmd->tvc_lun);
  910. if (data_direction != DMA_NONE) {
  911. ret = vhost_scsi_map_iov_to_sgl(cmd,
  912. &vq->iov[data_first], data_num,
  913. data_direction == DMA_TO_DEVICE);
  914. if (unlikely(ret)) {
  915. vq_err(vq, "Failed to map iov to sgl\n");
  916. goto err_free;
  917. }
  918. }
  919. /*
  920. * Save the descriptor from vhost_get_vq_desc() to be used to
  921. * complete the virtio-scsi request in TCM callback context via
  922. * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
  923. */
  924. cmd->tvc_vq_desc = head;
  925. /*
  926. * Dispatch tv_cmd descriptor for cmwq execution in process
  927. * context provided by tcm_vhost_workqueue. This also ensures
  928. * tv_cmd is executed on the same kworker CPU as this vhost
  929. * thread to gain positive L2 cache locality effects..
  930. */
  931. INIT_WORK(&cmd->work, tcm_vhost_submission_work);
  932. queue_work(tcm_vhost_workqueue, &cmd->work);
  933. }
  934. mutex_unlock(&vq->mutex);
  935. return;
  936. err_free:
  937. vhost_scsi_free_cmd(cmd);
  938. err_cmd:
  939. vhost_scsi_send_bad_target(vs, vq, head, out);
  940. out:
  941. mutex_unlock(&vq->mutex);
  942. }
  943. static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
  944. {
  945. pr_debug("%s: The handling func for control queue.\n", __func__);
  946. }
  947. static void
  948. tcm_vhost_send_evt(struct vhost_scsi *vs,
  949. struct tcm_vhost_tpg *tpg,
  950. struct se_lun *lun,
  951. u32 event,
  952. u32 reason)
  953. {
  954. struct tcm_vhost_evt *evt;
  955. evt = tcm_vhost_allocate_evt(vs, event, reason);
  956. if (!evt)
  957. return;
  958. if (tpg && lun) {
  959. /* TODO: share lun setup code with virtio-scsi.ko */
  960. /*
  961. * Note: evt->event is zeroed when we allocate it and
  962. * lun[4-7] need to be zero according to virtio-scsi spec.
  963. */
  964. evt->event.lun[0] = 0x01;
  965. evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
  966. if (lun->unpacked_lun >= 256)
  967. evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
  968. evt->event.lun[3] = lun->unpacked_lun & 0xFF;
  969. }
  970. llist_add(&evt->list, &vs->vs_event_list);
  971. vhost_work_queue(&vs->dev, &vs->vs_event_work);
  972. }
  973. static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
  974. {
  975. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  976. poll.work);
  977. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  978. mutex_lock(&vq->mutex);
  979. if (!vq->private_data)
  980. goto out;
  981. if (vs->vs_events_missed)
  982. tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
  983. out:
  984. mutex_unlock(&vq->mutex);
  985. }
  986. static void vhost_scsi_handle_kick(struct vhost_work *work)
  987. {
  988. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  989. poll.work);
  990. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  991. vhost_scsi_handle_vq(vs, vq);
  992. }
  993. static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
  994. {
  995. vhost_poll_flush(&vs->vqs[index].vq.poll);
  996. }
  997. /* Callers must hold dev mutex */
  998. static void vhost_scsi_flush(struct vhost_scsi *vs)
  999. {
  1000. struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
  1001. int i;
  1002. /* Init new inflight and remember the old inflight */
  1003. tcm_vhost_init_inflight(vs, old_inflight);
  1004. /*
  1005. * The inflight->kref was initialized to 1. We decrement it here to
  1006. * indicate the start of the flush operation so that it will reach 0
  1007. * when all the reqs are finished.
  1008. */
  1009. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1010. kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight);
  1011. /* Flush both the vhost poll and vhost work */
  1012. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1013. vhost_scsi_flush_vq(vs, i);
  1014. vhost_work_flush(&vs->dev, &vs->vs_completion_work);
  1015. vhost_work_flush(&vs->dev, &vs->vs_event_work);
  1016. /* Wait for all reqs issued before the flush to be finished */
  1017. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1018. wait_for_completion(&old_inflight[i]->comp);
  1019. }
  1020. /*
  1021. * Called from vhost_scsi_ioctl() context to walk the list of available
  1022. * tcm_vhost_tpg with an active struct tcm_vhost_nexus
  1023. *
  1024. * The lock nesting rule is:
  1025. * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
  1026. */
  1027. static int
  1028. vhost_scsi_set_endpoint(struct vhost_scsi *vs,
  1029. struct vhost_scsi_target *t)
  1030. {
  1031. struct tcm_vhost_tport *tv_tport;
  1032. struct tcm_vhost_tpg *tpg;
  1033. struct tcm_vhost_tpg **vs_tpg;
  1034. struct vhost_virtqueue *vq;
  1035. int index, ret, i, len;
  1036. bool match = false;
  1037. mutex_lock(&tcm_vhost_mutex);
  1038. mutex_lock(&vs->dev.mutex);
  1039. /* Verify that ring has been setup correctly. */
  1040. for (index = 0; index < vs->dev.nvqs; ++index) {
  1041. /* Verify that ring has been setup correctly. */
  1042. if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
  1043. ret = -EFAULT;
  1044. goto out;
  1045. }
  1046. }
  1047. len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
  1048. vs_tpg = kzalloc(len, GFP_KERNEL);
  1049. if (!vs_tpg) {
  1050. ret = -ENOMEM;
  1051. goto out;
  1052. }
  1053. if (vs->vs_tpg)
  1054. memcpy(vs_tpg, vs->vs_tpg, len);
  1055. list_for_each_entry(tpg, &tcm_vhost_list, tv_tpg_list) {
  1056. mutex_lock(&tpg->tv_tpg_mutex);
  1057. if (!tpg->tpg_nexus) {
  1058. mutex_unlock(&tpg->tv_tpg_mutex);
  1059. continue;
  1060. }
  1061. if (tpg->tv_tpg_vhost_count != 0) {
  1062. mutex_unlock(&tpg->tv_tpg_mutex);
  1063. continue;
  1064. }
  1065. tv_tport = tpg->tport;
  1066. if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  1067. if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
  1068. kfree(vs_tpg);
  1069. mutex_unlock(&tpg->tv_tpg_mutex);
  1070. ret = -EEXIST;
  1071. goto out;
  1072. }
  1073. tpg->tv_tpg_vhost_count++;
  1074. tpg->vhost_scsi = vs;
  1075. vs_tpg[tpg->tport_tpgt] = tpg;
  1076. smp_mb__after_atomic_inc();
  1077. match = true;
  1078. }
  1079. mutex_unlock(&tpg->tv_tpg_mutex);
  1080. }
  1081. if (match) {
  1082. memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
  1083. sizeof(vs->vs_vhost_wwpn));
  1084. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1085. vq = &vs->vqs[i].vq;
  1086. mutex_lock(&vq->mutex);
  1087. vq->private_data = vs_tpg;
  1088. vhost_init_used(vq);
  1089. mutex_unlock(&vq->mutex);
  1090. }
  1091. ret = 0;
  1092. } else {
  1093. ret = -EEXIST;
  1094. }
  1095. /*
  1096. * Act as synchronize_rcu to make sure access to
  1097. * old vs->vs_tpg is finished.
  1098. */
  1099. vhost_scsi_flush(vs);
  1100. kfree(vs->vs_tpg);
  1101. vs->vs_tpg = vs_tpg;
  1102. out:
  1103. mutex_unlock(&vs->dev.mutex);
  1104. mutex_unlock(&tcm_vhost_mutex);
  1105. return ret;
  1106. }
  1107. static int
  1108. vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
  1109. struct vhost_scsi_target *t)
  1110. {
  1111. struct tcm_vhost_tport *tv_tport;
  1112. struct tcm_vhost_tpg *tpg;
  1113. struct vhost_virtqueue *vq;
  1114. bool match = false;
  1115. int index, ret, i;
  1116. u8 target;
  1117. mutex_lock(&tcm_vhost_mutex);
  1118. mutex_lock(&vs->dev.mutex);
  1119. /* Verify that ring has been setup correctly. */
  1120. for (index = 0; index < vs->dev.nvqs; ++index) {
  1121. if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
  1122. ret = -EFAULT;
  1123. goto err_dev;
  1124. }
  1125. }
  1126. if (!vs->vs_tpg) {
  1127. ret = 0;
  1128. goto err_dev;
  1129. }
  1130. for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
  1131. target = i;
  1132. tpg = vs->vs_tpg[target];
  1133. if (!tpg)
  1134. continue;
  1135. mutex_lock(&tpg->tv_tpg_mutex);
  1136. tv_tport = tpg->tport;
  1137. if (!tv_tport) {
  1138. ret = -ENODEV;
  1139. goto err_tpg;
  1140. }
  1141. if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  1142. pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
  1143. " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
  1144. tv_tport->tport_name, tpg->tport_tpgt,
  1145. t->vhost_wwpn, t->vhost_tpgt);
  1146. ret = -EINVAL;
  1147. goto err_tpg;
  1148. }
  1149. tpg->tv_tpg_vhost_count--;
  1150. tpg->vhost_scsi = NULL;
  1151. vs->vs_tpg[target] = NULL;
  1152. match = true;
  1153. mutex_unlock(&tpg->tv_tpg_mutex);
  1154. }
  1155. if (match) {
  1156. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1157. vq = &vs->vqs[i].vq;
  1158. mutex_lock(&vq->mutex);
  1159. vq->private_data = NULL;
  1160. mutex_unlock(&vq->mutex);
  1161. }
  1162. }
  1163. /*
  1164. * Act as synchronize_rcu to make sure access to
  1165. * old vs->vs_tpg is finished.
  1166. */
  1167. vhost_scsi_flush(vs);
  1168. kfree(vs->vs_tpg);
  1169. vs->vs_tpg = NULL;
  1170. WARN_ON(vs->vs_events_nr);
  1171. mutex_unlock(&vs->dev.mutex);
  1172. mutex_unlock(&tcm_vhost_mutex);
  1173. return 0;
  1174. err_tpg:
  1175. mutex_unlock(&tpg->tv_tpg_mutex);
  1176. err_dev:
  1177. mutex_unlock(&vs->dev.mutex);
  1178. mutex_unlock(&tcm_vhost_mutex);
  1179. return ret;
  1180. }
  1181. static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
  1182. {
  1183. if (features & ~VHOST_SCSI_FEATURES)
  1184. return -EOPNOTSUPP;
  1185. mutex_lock(&vs->dev.mutex);
  1186. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  1187. !vhost_log_access_ok(&vs->dev)) {
  1188. mutex_unlock(&vs->dev.mutex);
  1189. return -EFAULT;
  1190. }
  1191. vs->dev.acked_features = features;
  1192. smp_wmb();
  1193. vhost_scsi_flush(vs);
  1194. mutex_unlock(&vs->dev.mutex);
  1195. return 0;
  1196. }
  1197. static int vhost_scsi_open(struct inode *inode, struct file *f)
  1198. {
  1199. struct vhost_scsi *vs;
  1200. struct vhost_virtqueue **vqs;
  1201. int r, i;
  1202. vs = kzalloc(sizeof(*vs), GFP_KERNEL);
  1203. if (!vs)
  1204. return -ENOMEM;
  1205. vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
  1206. if (!vqs) {
  1207. kfree(vs);
  1208. return -ENOMEM;
  1209. }
  1210. vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
  1211. vhost_work_init(&vs->vs_event_work, tcm_vhost_evt_work);
  1212. vs->vs_events_nr = 0;
  1213. vs->vs_events_missed = false;
  1214. vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
  1215. vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1216. vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
  1217. vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
  1218. for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
  1219. vqs[i] = &vs->vqs[i].vq;
  1220. vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
  1221. }
  1222. r = vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
  1223. tcm_vhost_init_inflight(vs, NULL);
  1224. if (r < 0) {
  1225. kfree(vqs);
  1226. kfree(vs);
  1227. return r;
  1228. }
  1229. f->private_data = vs;
  1230. return 0;
  1231. }
  1232. static int vhost_scsi_release(struct inode *inode, struct file *f)
  1233. {
  1234. struct vhost_scsi *vs = f->private_data;
  1235. struct vhost_scsi_target t;
  1236. mutex_lock(&vs->dev.mutex);
  1237. memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
  1238. mutex_unlock(&vs->dev.mutex);
  1239. vhost_scsi_clear_endpoint(vs, &t);
  1240. vhost_dev_stop(&vs->dev);
  1241. vhost_dev_cleanup(&vs->dev, false);
  1242. /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
  1243. vhost_scsi_flush(vs);
  1244. kfree(vs->dev.vqs);
  1245. kfree(vs);
  1246. return 0;
  1247. }
  1248. static long
  1249. vhost_scsi_ioctl(struct file *f,
  1250. unsigned int ioctl,
  1251. unsigned long arg)
  1252. {
  1253. struct vhost_scsi *vs = f->private_data;
  1254. struct vhost_scsi_target backend;
  1255. void __user *argp = (void __user *)arg;
  1256. u64 __user *featurep = argp;
  1257. u32 __user *eventsp = argp;
  1258. u32 events_missed;
  1259. u64 features;
  1260. int r, abi_version = VHOST_SCSI_ABI_VERSION;
  1261. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1262. switch (ioctl) {
  1263. case VHOST_SCSI_SET_ENDPOINT:
  1264. if (copy_from_user(&backend, argp, sizeof backend))
  1265. return -EFAULT;
  1266. if (backend.reserved != 0)
  1267. return -EOPNOTSUPP;
  1268. return vhost_scsi_set_endpoint(vs, &backend);
  1269. case VHOST_SCSI_CLEAR_ENDPOINT:
  1270. if (copy_from_user(&backend, argp, sizeof backend))
  1271. return -EFAULT;
  1272. if (backend.reserved != 0)
  1273. return -EOPNOTSUPP;
  1274. return vhost_scsi_clear_endpoint(vs, &backend);
  1275. case VHOST_SCSI_GET_ABI_VERSION:
  1276. if (copy_to_user(argp, &abi_version, sizeof abi_version))
  1277. return -EFAULT;
  1278. return 0;
  1279. case VHOST_SCSI_SET_EVENTS_MISSED:
  1280. if (get_user(events_missed, eventsp))
  1281. return -EFAULT;
  1282. mutex_lock(&vq->mutex);
  1283. vs->vs_events_missed = events_missed;
  1284. mutex_unlock(&vq->mutex);
  1285. return 0;
  1286. case VHOST_SCSI_GET_EVENTS_MISSED:
  1287. mutex_lock(&vq->mutex);
  1288. events_missed = vs->vs_events_missed;
  1289. mutex_unlock(&vq->mutex);
  1290. if (put_user(events_missed, eventsp))
  1291. return -EFAULT;
  1292. return 0;
  1293. case VHOST_GET_FEATURES:
  1294. features = VHOST_SCSI_FEATURES;
  1295. if (copy_to_user(featurep, &features, sizeof features))
  1296. return -EFAULT;
  1297. return 0;
  1298. case VHOST_SET_FEATURES:
  1299. if (copy_from_user(&features, featurep, sizeof features))
  1300. return -EFAULT;
  1301. return vhost_scsi_set_features(vs, features);
  1302. default:
  1303. mutex_lock(&vs->dev.mutex);
  1304. r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
  1305. /* TODO: flush backend after dev ioctl. */
  1306. if (r == -ENOIOCTLCMD)
  1307. r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
  1308. mutex_unlock(&vs->dev.mutex);
  1309. return r;
  1310. }
  1311. }
  1312. #ifdef CONFIG_COMPAT
  1313. static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
  1314. unsigned long arg)
  1315. {
  1316. return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  1317. }
  1318. #endif
  1319. static const struct file_operations vhost_scsi_fops = {
  1320. .owner = THIS_MODULE,
  1321. .release = vhost_scsi_release,
  1322. .unlocked_ioctl = vhost_scsi_ioctl,
  1323. #ifdef CONFIG_COMPAT
  1324. .compat_ioctl = vhost_scsi_compat_ioctl,
  1325. #endif
  1326. .open = vhost_scsi_open,
  1327. .llseek = noop_llseek,
  1328. };
  1329. static struct miscdevice vhost_scsi_misc = {
  1330. MISC_DYNAMIC_MINOR,
  1331. "vhost-scsi",
  1332. &vhost_scsi_fops,
  1333. };
  1334. static int __init vhost_scsi_register(void)
  1335. {
  1336. return misc_register(&vhost_scsi_misc);
  1337. }
  1338. static int vhost_scsi_deregister(void)
  1339. {
  1340. return misc_deregister(&vhost_scsi_misc);
  1341. }
  1342. static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
  1343. {
  1344. switch (tport->tport_proto_id) {
  1345. case SCSI_PROTOCOL_SAS:
  1346. return "SAS";
  1347. case SCSI_PROTOCOL_FCP:
  1348. return "FCP";
  1349. case SCSI_PROTOCOL_ISCSI:
  1350. return "iSCSI";
  1351. default:
  1352. break;
  1353. }
  1354. return "Unknown";
  1355. }
  1356. static void
  1357. tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
  1358. struct se_lun *lun, bool plug)
  1359. {
  1360. struct vhost_scsi *vs = tpg->vhost_scsi;
  1361. struct vhost_virtqueue *vq;
  1362. u32 reason;
  1363. if (!vs)
  1364. return;
  1365. mutex_lock(&vs->dev.mutex);
  1366. if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) {
  1367. mutex_unlock(&vs->dev.mutex);
  1368. return;
  1369. }
  1370. if (plug)
  1371. reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
  1372. else
  1373. reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
  1374. vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1375. mutex_lock(&vq->mutex);
  1376. tcm_vhost_send_evt(vs, tpg, lun,
  1377. VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
  1378. mutex_unlock(&vq->mutex);
  1379. mutex_unlock(&vs->dev.mutex);
  1380. }
  1381. static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
  1382. {
  1383. tcm_vhost_do_plug(tpg, lun, true);
  1384. }
  1385. static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
  1386. {
  1387. tcm_vhost_do_plug(tpg, lun, false);
  1388. }
  1389. static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
  1390. struct se_lun *lun)
  1391. {
  1392. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  1393. struct tcm_vhost_tpg, se_tpg);
  1394. mutex_lock(&tcm_vhost_mutex);
  1395. mutex_lock(&tpg->tv_tpg_mutex);
  1396. tpg->tv_tpg_port_count++;
  1397. mutex_unlock(&tpg->tv_tpg_mutex);
  1398. tcm_vhost_hotplug(tpg, lun);
  1399. mutex_unlock(&tcm_vhost_mutex);
  1400. return 0;
  1401. }
  1402. static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
  1403. struct se_lun *lun)
  1404. {
  1405. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  1406. struct tcm_vhost_tpg, se_tpg);
  1407. mutex_lock(&tcm_vhost_mutex);
  1408. mutex_lock(&tpg->tv_tpg_mutex);
  1409. tpg->tv_tpg_port_count--;
  1410. mutex_unlock(&tpg->tv_tpg_mutex);
  1411. tcm_vhost_hotunplug(tpg, lun);
  1412. mutex_unlock(&tcm_vhost_mutex);
  1413. }
  1414. static struct se_node_acl *
  1415. tcm_vhost_make_nodeacl(struct se_portal_group *se_tpg,
  1416. struct config_group *group,
  1417. const char *name)
  1418. {
  1419. struct se_node_acl *se_nacl, *se_nacl_new;
  1420. struct tcm_vhost_nacl *nacl;
  1421. u64 wwpn = 0;
  1422. u32 nexus_depth;
  1423. /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
  1424. return ERR_PTR(-EINVAL); */
  1425. se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
  1426. if (!se_nacl_new)
  1427. return ERR_PTR(-ENOMEM);
  1428. nexus_depth = 1;
  1429. /*
  1430. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  1431. * when converting a NodeACL from demo mode -> explict
  1432. */
  1433. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  1434. name, nexus_depth);
  1435. if (IS_ERR(se_nacl)) {
  1436. tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
  1437. return se_nacl;
  1438. }
  1439. /*
  1440. * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
  1441. */
  1442. nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
  1443. nacl->iport_wwpn = wwpn;
  1444. return se_nacl;
  1445. }
  1446. static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
  1447. {
  1448. struct tcm_vhost_nacl *nacl = container_of(se_acl,
  1449. struct tcm_vhost_nacl, se_node_acl);
  1450. core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
  1451. kfree(nacl);
  1452. }
  1453. static void tcm_vhost_free_cmd_map_res(struct tcm_vhost_nexus *nexus,
  1454. struct se_session *se_sess)
  1455. {
  1456. struct tcm_vhost_cmd *tv_cmd;
  1457. unsigned int i;
  1458. if (!se_sess->sess_cmd_map)
  1459. return;
  1460. for (i = 0; i < TCM_VHOST_DEFAULT_TAGS; i++) {
  1461. tv_cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[i];
  1462. kfree(tv_cmd->tvc_sgl);
  1463. kfree(tv_cmd->tvc_upages);
  1464. }
  1465. }
  1466. static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tpg,
  1467. const char *name)
  1468. {
  1469. struct se_portal_group *se_tpg;
  1470. struct se_session *se_sess;
  1471. struct tcm_vhost_nexus *tv_nexus;
  1472. struct tcm_vhost_cmd *tv_cmd;
  1473. unsigned int i;
  1474. mutex_lock(&tpg->tv_tpg_mutex);
  1475. if (tpg->tpg_nexus) {
  1476. mutex_unlock(&tpg->tv_tpg_mutex);
  1477. pr_debug("tpg->tpg_nexus already exists\n");
  1478. return -EEXIST;
  1479. }
  1480. se_tpg = &tpg->se_tpg;
  1481. tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
  1482. if (!tv_nexus) {
  1483. mutex_unlock(&tpg->tv_tpg_mutex);
  1484. pr_err("Unable to allocate struct tcm_vhost_nexus\n");
  1485. return -ENOMEM;
  1486. }
  1487. /*
  1488. * Initialize the struct se_session pointer and setup tagpool
  1489. * for struct tcm_vhost_cmd descriptors
  1490. */
  1491. tv_nexus->tvn_se_sess = transport_init_session_tags(
  1492. TCM_VHOST_DEFAULT_TAGS,
  1493. sizeof(struct tcm_vhost_cmd));
  1494. if (IS_ERR(tv_nexus->tvn_se_sess)) {
  1495. mutex_unlock(&tpg->tv_tpg_mutex);
  1496. kfree(tv_nexus);
  1497. return -ENOMEM;
  1498. }
  1499. se_sess = tv_nexus->tvn_se_sess;
  1500. for (i = 0; i < TCM_VHOST_DEFAULT_TAGS; i++) {
  1501. tv_cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[i];
  1502. tv_cmd->tvc_sgl = kzalloc(sizeof(struct scatterlist) *
  1503. TCM_VHOST_PREALLOC_SGLS, GFP_KERNEL);
  1504. if (!tv_cmd->tvc_sgl) {
  1505. mutex_unlock(&tpg->tv_tpg_mutex);
  1506. pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
  1507. goto out;
  1508. }
  1509. tv_cmd->tvc_upages = kzalloc(sizeof(struct page *) *
  1510. TCM_VHOST_PREALLOC_PAGES, GFP_KERNEL);
  1511. if (!tv_cmd->tvc_upages) {
  1512. mutex_unlock(&tpg->tv_tpg_mutex);
  1513. pr_err("Unable to allocate tv_cmd->tvc_upages\n");
  1514. goto out;
  1515. }
  1516. }
  1517. /*
  1518. * Since we are running in 'demo mode' this call with generate a
  1519. * struct se_node_acl for the tcm_vhost struct se_portal_group with
  1520. * the SCSI Initiator port name of the passed configfs group 'name'.
  1521. */
  1522. tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  1523. se_tpg, (unsigned char *)name);
  1524. if (!tv_nexus->tvn_se_sess->se_node_acl) {
  1525. mutex_unlock(&tpg->tv_tpg_mutex);
  1526. pr_debug("core_tpg_check_initiator_node_acl() failed"
  1527. " for %s\n", name);
  1528. goto out;
  1529. }
  1530. /*
  1531. * Now register the TCM vhost virtual I_T Nexus as active with the
  1532. * call to __transport_register_session()
  1533. */
  1534. __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
  1535. tv_nexus->tvn_se_sess, tv_nexus);
  1536. tpg->tpg_nexus = tv_nexus;
  1537. mutex_unlock(&tpg->tv_tpg_mutex);
  1538. return 0;
  1539. out:
  1540. tcm_vhost_free_cmd_map_res(tv_nexus, se_sess);
  1541. transport_free_session(se_sess);
  1542. kfree(tv_nexus);
  1543. return -ENOMEM;
  1544. }
  1545. static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
  1546. {
  1547. struct se_session *se_sess;
  1548. struct tcm_vhost_nexus *tv_nexus;
  1549. mutex_lock(&tpg->tv_tpg_mutex);
  1550. tv_nexus = tpg->tpg_nexus;
  1551. if (!tv_nexus) {
  1552. mutex_unlock(&tpg->tv_tpg_mutex);
  1553. return -ENODEV;
  1554. }
  1555. se_sess = tv_nexus->tvn_se_sess;
  1556. if (!se_sess) {
  1557. mutex_unlock(&tpg->tv_tpg_mutex);
  1558. return -ENODEV;
  1559. }
  1560. if (tpg->tv_tpg_port_count != 0) {
  1561. mutex_unlock(&tpg->tv_tpg_mutex);
  1562. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1563. " active TPG port count: %d\n",
  1564. tpg->tv_tpg_port_count);
  1565. return -EBUSY;
  1566. }
  1567. if (tpg->tv_tpg_vhost_count != 0) {
  1568. mutex_unlock(&tpg->tv_tpg_mutex);
  1569. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1570. " active TPG vhost count: %d\n",
  1571. tpg->tv_tpg_vhost_count);
  1572. return -EBUSY;
  1573. }
  1574. pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
  1575. " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
  1576. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1577. tcm_vhost_free_cmd_map_res(tv_nexus, se_sess);
  1578. /*
  1579. * Release the SCSI I_T Nexus to the emulated vhost Target Port
  1580. */
  1581. transport_deregister_session(tv_nexus->tvn_se_sess);
  1582. tpg->tpg_nexus = NULL;
  1583. mutex_unlock(&tpg->tv_tpg_mutex);
  1584. kfree(tv_nexus);
  1585. return 0;
  1586. }
  1587. static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
  1588. char *page)
  1589. {
  1590. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  1591. struct tcm_vhost_tpg, se_tpg);
  1592. struct tcm_vhost_nexus *tv_nexus;
  1593. ssize_t ret;
  1594. mutex_lock(&tpg->tv_tpg_mutex);
  1595. tv_nexus = tpg->tpg_nexus;
  1596. if (!tv_nexus) {
  1597. mutex_unlock(&tpg->tv_tpg_mutex);
  1598. return -ENODEV;
  1599. }
  1600. ret = snprintf(page, PAGE_SIZE, "%s\n",
  1601. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1602. mutex_unlock(&tpg->tv_tpg_mutex);
  1603. return ret;
  1604. }
  1605. static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
  1606. const char *page,
  1607. size_t count)
  1608. {
  1609. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  1610. struct tcm_vhost_tpg, se_tpg);
  1611. struct tcm_vhost_tport *tport_wwn = tpg->tport;
  1612. unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
  1613. int ret;
  1614. /*
  1615. * Shutdown the active I_T nexus if 'NULL' is passed..
  1616. */
  1617. if (!strncmp(page, "NULL", 4)) {
  1618. ret = tcm_vhost_drop_nexus(tpg);
  1619. return (!ret) ? count : ret;
  1620. }
  1621. /*
  1622. * Otherwise make sure the passed virtual Initiator port WWN matches
  1623. * the fabric protocol_id set in tcm_vhost_make_tport(), and call
  1624. * tcm_vhost_make_nexus().
  1625. */
  1626. if (strlen(page) >= TCM_VHOST_NAMELEN) {
  1627. pr_err("Emulated NAA Sas Address: %s, exceeds"
  1628. " max: %d\n", page, TCM_VHOST_NAMELEN);
  1629. return -EINVAL;
  1630. }
  1631. snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
  1632. ptr = strstr(i_port, "naa.");
  1633. if (ptr) {
  1634. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
  1635. pr_err("Passed SAS Initiator Port %s does not"
  1636. " match target port protoid: %s\n", i_port,
  1637. tcm_vhost_dump_proto_id(tport_wwn));
  1638. return -EINVAL;
  1639. }
  1640. port_ptr = &i_port[0];
  1641. goto check_newline;
  1642. }
  1643. ptr = strstr(i_port, "fc.");
  1644. if (ptr) {
  1645. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
  1646. pr_err("Passed FCP Initiator Port %s does not"
  1647. " match target port protoid: %s\n", i_port,
  1648. tcm_vhost_dump_proto_id(tport_wwn));
  1649. return -EINVAL;
  1650. }
  1651. port_ptr = &i_port[3]; /* Skip over "fc." */
  1652. goto check_newline;
  1653. }
  1654. ptr = strstr(i_port, "iqn.");
  1655. if (ptr) {
  1656. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
  1657. pr_err("Passed iSCSI Initiator Port %s does not"
  1658. " match target port protoid: %s\n", i_port,
  1659. tcm_vhost_dump_proto_id(tport_wwn));
  1660. return -EINVAL;
  1661. }
  1662. port_ptr = &i_port[0];
  1663. goto check_newline;
  1664. }
  1665. pr_err("Unable to locate prefix for emulated Initiator Port:"
  1666. " %s\n", i_port);
  1667. return -EINVAL;
  1668. /*
  1669. * Clear any trailing newline for the NAA WWN
  1670. */
  1671. check_newline:
  1672. if (i_port[strlen(i_port)-1] == '\n')
  1673. i_port[strlen(i_port)-1] = '\0';
  1674. ret = tcm_vhost_make_nexus(tpg, port_ptr);
  1675. if (ret < 0)
  1676. return ret;
  1677. return count;
  1678. }
  1679. TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
  1680. static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
  1681. &tcm_vhost_tpg_nexus.attr,
  1682. NULL,
  1683. };
  1684. static struct se_portal_group *
  1685. tcm_vhost_make_tpg(struct se_wwn *wwn,
  1686. struct config_group *group,
  1687. const char *name)
  1688. {
  1689. struct tcm_vhost_tport *tport = container_of(wwn,
  1690. struct tcm_vhost_tport, tport_wwn);
  1691. struct tcm_vhost_tpg *tpg;
  1692. unsigned long tpgt;
  1693. int ret;
  1694. if (strstr(name, "tpgt_") != name)
  1695. return ERR_PTR(-EINVAL);
  1696. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
  1697. return ERR_PTR(-EINVAL);
  1698. tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
  1699. if (!tpg) {
  1700. pr_err("Unable to allocate struct tcm_vhost_tpg");
  1701. return ERR_PTR(-ENOMEM);
  1702. }
  1703. mutex_init(&tpg->tv_tpg_mutex);
  1704. INIT_LIST_HEAD(&tpg->tv_tpg_list);
  1705. tpg->tport = tport;
  1706. tpg->tport_tpgt = tpgt;
  1707. ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
  1708. &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
  1709. if (ret < 0) {
  1710. kfree(tpg);
  1711. return NULL;
  1712. }
  1713. mutex_lock(&tcm_vhost_mutex);
  1714. list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
  1715. mutex_unlock(&tcm_vhost_mutex);
  1716. return &tpg->se_tpg;
  1717. }
  1718. static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
  1719. {
  1720. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  1721. struct tcm_vhost_tpg, se_tpg);
  1722. mutex_lock(&tcm_vhost_mutex);
  1723. list_del(&tpg->tv_tpg_list);
  1724. mutex_unlock(&tcm_vhost_mutex);
  1725. /*
  1726. * Release the virtual I_T Nexus for this vhost TPG
  1727. */
  1728. tcm_vhost_drop_nexus(tpg);
  1729. /*
  1730. * Deregister the se_tpg from TCM..
  1731. */
  1732. core_tpg_deregister(se_tpg);
  1733. kfree(tpg);
  1734. }
  1735. static struct se_wwn *
  1736. tcm_vhost_make_tport(struct target_fabric_configfs *tf,
  1737. struct config_group *group,
  1738. const char *name)
  1739. {
  1740. struct tcm_vhost_tport *tport;
  1741. char *ptr;
  1742. u64 wwpn = 0;
  1743. int off = 0;
  1744. /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
  1745. return ERR_PTR(-EINVAL); */
  1746. tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
  1747. if (!tport) {
  1748. pr_err("Unable to allocate struct tcm_vhost_tport");
  1749. return ERR_PTR(-ENOMEM);
  1750. }
  1751. tport->tport_wwpn = wwpn;
  1752. /*
  1753. * Determine the emulated Protocol Identifier and Target Port Name
  1754. * based on the incoming configfs directory name.
  1755. */
  1756. ptr = strstr(name, "naa.");
  1757. if (ptr) {
  1758. tport->tport_proto_id = SCSI_PROTOCOL_SAS;
  1759. goto check_len;
  1760. }
  1761. ptr = strstr(name, "fc.");
  1762. if (ptr) {
  1763. tport->tport_proto_id = SCSI_PROTOCOL_FCP;
  1764. off = 3; /* Skip over "fc." */
  1765. goto check_len;
  1766. }
  1767. ptr = strstr(name, "iqn.");
  1768. if (ptr) {
  1769. tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
  1770. goto check_len;
  1771. }
  1772. pr_err("Unable to locate prefix for emulated Target Port:"
  1773. " %s\n", name);
  1774. kfree(tport);
  1775. return ERR_PTR(-EINVAL);
  1776. check_len:
  1777. if (strlen(name) >= TCM_VHOST_NAMELEN) {
  1778. pr_err("Emulated %s Address: %s, exceeds"
  1779. " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
  1780. TCM_VHOST_NAMELEN);
  1781. kfree(tport);
  1782. return ERR_PTR(-EINVAL);
  1783. }
  1784. snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
  1785. pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
  1786. " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
  1787. return &tport->tport_wwn;
  1788. }
  1789. static void tcm_vhost_drop_tport(struct se_wwn *wwn)
  1790. {
  1791. struct tcm_vhost_tport *tport = container_of(wwn,
  1792. struct tcm_vhost_tport, tport_wwn);
  1793. pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
  1794. " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
  1795. tport->tport_name);
  1796. kfree(tport);
  1797. }
  1798. static ssize_t
  1799. tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs *tf,
  1800. char *page)
  1801. {
  1802. return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
  1803. "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
  1804. utsname()->machine);
  1805. }
  1806. TF_WWN_ATTR_RO(tcm_vhost, version);
  1807. static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
  1808. &tcm_vhost_wwn_version.attr,
  1809. NULL,
  1810. };
  1811. static struct target_core_fabric_ops tcm_vhost_ops = {
  1812. .get_fabric_name = tcm_vhost_get_fabric_name,
  1813. .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
  1814. .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
  1815. .tpg_get_tag = tcm_vhost_get_tag,
  1816. .tpg_get_default_depth = tcm_vhost_get_default_depth,
  1817. .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
  1818. .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
  1819. .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
  1820. .tpg_check_demo_mode = tcm_vhost_check_true,
  1821. .tpg_check_demo_mode_cache = tcm_vhost_check_true,
  1822. .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
  1823. .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
  1824. .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
  1825. .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
  1826. .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
  1827. .release_cmd = tcm_vhost_release_cmd,
  1828. .check_stop_free = vhost_scsi_check_stop_free,
  1829. .shutdown_session = tcm_vhost_shutdown_session,
  1830. .close_session = tcm_vhost_close_session,
  1831. .sess_get_index = tcm_vhost_sess_get_index,
  1832. .sess_get_initiator_sid = NULL,
  1833. .write_pending = tcm_vhost_write_pending,
  1834. .write_pending_status = tcm_vhost_write_pending_status,
  1835. .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
  1836. .get_task_tag = tcm_vhost_get_task_tag,
  1837. .get_cmd_state = tcm_vhost_get_cmd_state,
  1838. .queue_data_in = tcm_vhost_queue_data_in,
  1839. .queue_status = tcm_vhost_queue_status,
  1840. .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
  1841. /*
  1842. * Setup callers for generic logic in target_core_fabric_configfs.c
  1843. */
  1844. .fabric_make_wwn = tcm_vhost_make_tport,
  1845. .fabric_drop_wwn = tcm_vhost_drop_tport,
  1846. .fabric_make_tpg = tcm_vhost_make_tpg,
  1847. .fabric_drop_tpg = tcm_vhost_drop_tpg,
  1848. .fabric_post_link = tcm_vhost_port_link,
  1849. .fabric_pre_unlink = tcm_vhost_port_unlink,
  1850. .fabric_make_np = NULL,
  1851. .fabric_drop_np = NULL,
  1852. .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
  1853. .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
  1854. };
  1855. static int tcm_vhost_register_configfs(void)
  1856. {
  1857. struct target_fabric_configfs *fabric;
  1858. int ret;
  1859. pr_debug("TCM_VHOST fabric module %s on %s/%s"
  1860. " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
  1861. utsname()->machine);
  1862. /*
  1863. * Register the top level struct config_item_type with TCM core
  1864. */
  1865. fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
  1866. if (IS_ERR(fabric)) {
  1867. pr_err("target_fabric_configfs_init() failed\n");
  1868. return PTR_ERR(fabric);
  1869. }
  1870. /*
  1871. * Setup fabric->tf_ops from our local tcm_vhost_ops
  1872. */
  1873. fabric->tf_ops = tcm_vhost_ops;
  1874. /*
  1875. * Setup default attribute lists for various fabric->tf_cit_tmpl
  1876. */
  1877. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
  1878. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
  1879. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
  1880. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1881. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1882. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  1883. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  1884. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  1885. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  1886. /*
  1887. * Register the fabric for use within TCM
  1888. */
  1889. ret = target_fabric_configfs_register(fabric);
  1890. if (ret < 0) {
  1891. pr_err("target_fabric_configfs_register() failed"
  1892. " for TCM_VHOST\n");
  1893. return ret;
  1894. }
  1895. /*
  1896. * Setup our local pointer to *fabric
  1897. */
  1898. tcm_vhost_fabric_configfs = fabric;
  1899. pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
  1900. return 0;
  1901. };
  1902. static void tcm_vhost_deregister_configfs(void)
  1903. {
  1904. if (!tcm_vhost_fabric_configfs)
  1905. return;
  1906. target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
  1907. tcm_vhost_fabric_configfs = NULL;
  1908. pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
  1909. };
  1910. static int __init tcm_vhost_init(void)
  1911. {
  1912. int ret = -ENOMEM;
  1913. /*
  1914. * Use our own dedicated workqueue for submitting I/O into
  1915. * target core to avoid contention within system_wq.
  1916. */
  1917. tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
  1918. if (!tcm_vhost_workqueue)
  1919. goto out;
  1920. ret = vhost_scsi_register();
  1921. if (ret < 0)
  1922. goto out_destroy_workqueue;
  1923. ret = tcm_vhost_register_configfs();
  1924. if (ret < 0)
  1925. goto out_vhost_scsi_deregister;
  1926. return 0;
  1927. out_vhost_scsi_deregister:
  1928. vhost_scsi_deregister();
  1929. out_destroy_workqueue:
  1930. destroy_workqueue(tcm_vhost_workqueue);
  1931. out:
  1932. return ret;
  1933. };
  1934. static void tcm_vhost_exit(void)
  1935. {
  1936. tcm_vhost_deregister_configfs();
  1937. vhost_scsi_deregister();
  1938. destroy_workqueue(tcm_vhost_workqueue);
  1939. };
  1940. MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
  1941. MODULE_ALIAS("tcm_vhost");
  1942. MODULE_LICENSE("GPL");
  1943. module_init(tcm_vhost_init);
  1944. module_exit(tcm_vhost_exit);