scsi.c 58 KB

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