scsi.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206
  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. int i, ret;
  607. for (i = 0; i < iter->nr_segs; i++) {
  608. void __user *base = iter->iov[i].iov_base + off;
  609. size_t len = iter->iov[i].iov_len - off;
  610. ret = vhost_scsi_map_to_sgl(cmd, base, len, sg, write);
  611. if (ret < 0) {
  612. for (i = 0; i < sg_count; i++) {
  613. struct page *page = sg_page(&sg[i]);
  614. if (page)
  615. put_page(page);
  616. }
  617. return ret;
  618. }
  619. sg += ret;
  620. off = 0;
  621. }
  622. return 0;
  623. }
  624. static int
  625. vhost_scsi_mapal(struct vhost_scsi_cmd *cmd,
  626. size_t prot_bytes, struct iov_iter *prot_iter,
  627. size_t data_bytes, struct iov_iter *data_iter)
  628. {
  629. int sgl_count, ret;
  630. bool write = (cmd->tvc_data_direction == DMA_FROM_DEVICE);
  631. if (prot_bytes) {
  632. sgl_count = vhost_scsi_calc_sgls(prot_iter, prot_bytes,
  633. VHOST_SCSI_PREALLOC_PROT_SGLS);
  634. if (sgl_count < 0)
  635. return sgl_count;
  636. sg_init_table(cmd->tvc_prot_sgl, sgl_count);
  637. cmd->tvc_prot_sgl_count = sgl_count;
  638. pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__,
  639. cmd->tvc_prot_sgl, cmd->tvc_prot_sgl_count);
  640. ret = vhost_scsi_iov_to_sgl(cmd, write, prot_iter,
  641. cmd->tvc_prot_sgl,
  642. cmd->tvc_prot_sgl_count);
  643. if (ret < 0) {
  644. cmd->tvc_prot_sgl_count = 0;
  645. return ret;
  646. }
  647. }
  648. sgl_count = vhost_scsi_calc_sgls(data_iter, data_bytes,
  649. VHOST_SCSI_PREALLOC_SGLS);
  650. if (sgl_count < 0)
  651. return sgl_count;
  652. sg_init_table(cmd->tvc_sgl, sgl_count);
  653. cmd->tvc_sgl_count = sgl_count;
  654. pr_debug("%s data_sg %p data_sgl_count %u\n", __func__,
  655. cmd->tvc_sgl, cmd->tvc_sgl_count);
  656. ret = vhost_scsi_iov_to_sgl(cmd, write, data_iter,
  657. cmd->tvc_sgl, cmd->tvc_sgl_count);
  658. if (ret < 0) {
  659. cmd->tvc_sgl_count = 0;
  660. return ret;
  661. }
  662. return 0;
  663. }
  664. static int vhost_scsi_to_tcm_attr(int attr)
  665. {
  666. switch (attr) {
  667. case VIRTIO_SCSI_S_SIMPLE:
  668. return TCM_SIMPLE_TAG;
  669. case VIRTIO_SCSI_S_ORDERED:
  670. return TCM_ORDERED_TAG;
  671. case VIRTIO_SCSI_S_HEAD:
  672. return TCM_HEAD_TAG;
  673. case VIRTIO_SCSI_S_ACA:
  674. return TCM_ACA_TAG;
  675. default:
  676. break;
  677. }
  678. return TCM_SIMPLE_TAG;
  679. }
  680. static void vhost_scsi_submission_work(struct work_struct *work)
  681. {
  682. struct vhost_scsi_cmd *cmd =
  683. container_of(work, struct vhost_scsi_cmd, work);
  684. struct vhost_scsi_nexus *tv_nexus;
  685. struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
  686. struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
  687. int rc;
  688. /* FIXME: BIDI operation */
  689. if (cmd->tvc_sgl_count) {
  690. sg_ptr = cmd->tvc_sgl;
  691. if (cmd->tvc_prot_sgl_count)
  692. sg_prot_ptr = cmd->tvc_prot_sgl;
  693. else
  694. se_cmd->prot_pto = true;
  695. } else {
  696. sg_ptr = NULL;
  697. }
  698. tv_nexus = cmd->tvc_nexus;
  699. se_cmd->tag = 0;
  700. rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
  701. cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
  702. cmd->tvc_lun, cmd->tvc_exp_data_len,
  703. vhost_scsi_to_tcm_attr(cmd->tvc_task_attr),
  704. cmd->tvc_data_direction, TARGET_SCF_ACK_KREF,
  705. sg_ptr, cmd->tvc_sgl_count, NULL, 0, sg_prot_ptr,
  706. cmd->tvc_prot_sgl_count);
  707. if (rc < 0) {
  708. transport_send_check_condition_and_sense(se_cmd,
  709. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  710. transport_generic_free_cmd(se_cmd, 0);
  711. }
  712. }
  713. static void
  714. vhost_scsi_send_bad_target(struct vhost_scsi *vs,
  715. struct vhost_virtqueue *vq,
  716. int head, unsigned out)
  717. {
  718. struct virtio_scsi_cmd_resp __user *resp;
  719. struct virtio_scsi_cmd_resp rsp;
  720. int ret;
  721. memset(&rsp, 0, sizeof(rsp));
  722. rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
  723. resp = vq->iov[out].iov_base;
  724. ret = __copy_to_user(resp, &rsp, sizeof(rsp));
  725. if (!ret)
  726. vhost_add_used_and_signal(&vs->dev, vq, head, 0);
  727. else
  728. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  729. }
  730. static void
  731. vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
  732. {
  733. struct vhost_scsi_tpg **vs_tpg, *tpg;
  734. struct virtio_scsi_cmd_req v_req;
  735. struct virtio_scsi_cmd_req_pi v_req_pi;
  736. struct vhost_scsi_cmd *cmd;
  737. struct iov_iter out_iter, in_iter, prot_iter, data_iter;
  738. u64 tag;
  739. u32 exp_data_len, data_direction;
  740. unsigned out, in;
  741. int head, ret, prot_bytes;
  742. size_t req_size, rsp_size = sizeof(struct virtio_scsi_cmd_resp);
  743. size_t out_size, in_size;
  744. u16 lun;
  745. u8 *target, *lunp, task_attr;
  746. bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI);
  747. void *req, *cdb;
  748. mutex_lock(&vq->mutex);
  749. /*
  750. * We can handle the vq only after the endpoint is setup by calling the
  751. * VHOST_SCSI_SET_ENDPOINT ioctl.
  752. */
  753. vs_tpg = vq->private_data;
  754. if (!vs_tpg)
  755. goto out;
  756. vhost_disable_notify(&vs->dev, vq);
  757. for (;;) {
  758. head = vhost_get_vq_desc(vq, vq->iov,
  759. ARRAY_SIZE(vq->iov), &out, &in,
  760. NULL, NULL);
  761. pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
  762. head, out, in);
  763. /* On error, stop handling until the next kick. */
  764. if (unlikely(head < 0))
  765. break;
  766. /* Nothing new? Wait for eventfd to tell us they refilled. */
  767. if (head == vq->num) {
  768. if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
  769. vhost_disable_notify(&vs->dev, vq);
  770. continue;
  771. }
  772. break;
  773. }
  774. /*
  775. * Check for a sane response buffer so we can report early
  776. * errors back to the guest.
  777. */
  778. if (unlikely(vq->iov[out].iov_len < rsp_size)) {
  779. vq_err(vq, "Expecting at least virtio_scsi_cmd_resp"
  780. " size, got %zu bytes\n", vq->iov[out].iov_len);
  781. break;
  782. }
  783. /*
  784. * Setup pointers and values based upon different virtio-scsi
  785. * request header if T10_PI is enabled in KVM guest.
  786. */
  787. if (t10_pi) {
  788. req = &v_req_pi;
  789. req_size = sizeof(v_req_pi);
  790. lunp = &v_req_pi.lun[0];
  791. target = &v_req_pi.lun[1];
  792. } else {
  793. req = &v_req;
  794. req_size = sizeof(v_req);
  795. lunp = &v_req.lun[0];
  796. target = &v_req.lun[1];
  797. }
  798. /*
  799. * FIXME: Not correct for BIDI operation
  800. */
  801. out_size = iov_length(vq->iov, out);
  802. in_size = iov_length(&vq->iov[out], in);
  803. /*
  804. * Copy over the virtio-scsi request header, which for a
  805. * ANY_LAYOUT enabled guest may span multiple iovecs, or a
  806. * single iovec may contain both the header + outgoing
  807. * WRITE payloads.
  808. *
  809. * copy_from_iter() will advance out_iter, so that it will
  810. * point at the start of the outgoing WRITE payload, if
  811. * DMA_TO_DEVICE is set.
  812. */
  813. iov_iter_init(&out_iter, WRITE, vq->iov, out, out_size);
  814. ret = copy_from_iter(req, req_size, &out_iter);
  815. if (unlikely(ret != req_size)) {
  816. vq_err(vq, "Faulted on copy_from_iter\n");
  817. vhost_scsi_send_bad_target(vs, vq, head, out);
  818. continue;
  819. }
  820. /* virtio-scsi spec requires byte 0 of the lun to be 1 */
  821. if (unlikely(*lunp != 1)) {
  822. vq_err(vq, "Illegal virtio-scsi lun: %u\n", *lunp);
  823. vhost_scsi_send_bad_target(vs, vq, head, out);
  824. continue;
  825. }
  826. tpg = ACCESS_ONCE(vs_tpg[*target]);
  827. if (unlikely(!tpg)) {
  828. /* Target does not exist, fail the request */
  829. vhost_scsi_send_bad_target(vs, vq, head, out);
  830. continue;
  831. }
  832. /*
  833. * Determine data_direction by calculating the total outgoing
  834. * iovec sizes + incoming iovec sizes vs. virtio-scsi request +
  835. * response headers respectively.
  836. *
  837. * For DMA_TO_DEVICE this is out_iter, which is already pointing
  838. * to the right place.
  839. *
  840. * For DMA_FROM_DEVICE, the iovec will be just past the end
  841. * of the virtio-scsi response header in either the same
  842. * or immediately following iovec.
  843. *
  844. * Any associated T10_PI bytes for the outgoing / incoming
  845. * payloads are included in calculation of exp_data_len here.
  846. */
  847. prot_bytes = 0;
  848. if (out_size > req_size) {
  849. data_direction = DMA_TO_DEVICE;
  850. exp_data_len = out_size - req_size;
  851. data_iter = out_iter;
  852. } else if (in_size > rsp_size) {
  853. data_direction = DMA_FROM_DEVICE;
  854. exp_data_len = in_size - rsp_size;
  855. iov_iter_init(&in_iter, READ, &vq->iov[out], in,
  856. rsp_size + exp_data_len);
  857. iov_iter_advance(&in_iter, rsp_size);
  858. data_iter = in_iter;
  859. } else {
  860. data_direction = DMA_NONE;
  861. exp_data_len = 0;
  862. }
  863. /*
  864. * If T10_PI header + payload is present, setup prot_iter values
  865. * and recalculate data_iter for vhost_scsi_mapal() mapping to
  866. * host scatterlists via get_user_pages_fast().
  867. */
  868. if (t10_pi) {
  869. if (v_req_pi.pi_bytesout) {
  870. if (data_direction != DMA_TO_DEVICE) {
  871. vq_err(vq, "Received non zero pi_bytesout,"
  872. " but wrong data_direction\n");
  873. vhost_scsi_send_bad_target(vs, vq, head, out);
  874. continue;
  875. }
  876. prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout);
  877. } else if (v_req_pi.pi_bytesin) {
  878. if (data_direction != DMA_FROM_DEVICE) {
  879. vq_err(vq, "Received non zero pi_bytesin,"
  880. " but wrong data_direction\n");
  881. vhost_scsi_send_bad_target(vs, vq, head, out);
  882. continue;
  883. }
  884. prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin);
  885. }
  886. /*
  887. * Set prot_iter to data_iter, and advance past any
  888. * preceeding prot_bytes that may be present.
  889. *
  890. * Also fix up the exp_data_len to reflect only the
  891. * actual data payload length.
  892. */
  893. if (prot_bytes) {
  894. exp_data_len -= prot_bytes;
  895. prot_iter = data_iter;
  896. iov_iter_advance(&data_iter, prot_bytes);
  897. }
  898. tag = vhost64_to_cpu(vq, v_req_pi.tag);
  899. task_attr = v_req_pi.task_attr;
  900. cdb = &v_req_pi.cdb[0];
  901. lun = ((v_req_pi.lun[2] << 8) | v_req_pi.lun[3]) & 0x3FFF;
  902. } else {
  903. tag = vhost64_to_cpu(vq, v_req.tag);
  904. task_attr = v_req.task_attr;
  905. cdb = &v_req.cdb[0];
  906. lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
  907. }
  908. /*
  909. * Check that the received CDB size does not exceeded our
  910. * hardcoded max for vhost-scsi, then get a pre-allocated
  911. * cmd descriptor for the new virtio-scsi tag.
  912. *
  913. * TODO what if cdb was too small for varlen cdb header?
  914. */
  915. if (unlikely(scsi_command_size(cdb) > VHOST_SCSI_MAX_CDB_SIZE)) {
  916. vq_err(vq, "Received SCSI CDB with command_size: %d that"
  917. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  918. scsi_command_size(cdb), VHOST_SCSI_MAX_CDB_SIZE);
  919. vhost_scsi_send_bad_target(vs, vq, head, out);
  920. continue;
  921. }
  922. cmd = vhost_scsi_get_tag(vq, tpg, cdb, tag, lun, task_attr,
  923. exp_data_len + prot_bytes,
  924. data_direction);
  925. if (IS_ERR(cmd)) {
  926. vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
  927. PTR_ERR(cmd));
  928. vhost_scsi_send_bad_target(vs, vq, head, out);
  929. continue;
  930. }
  931. cmd->tvc_vhost = vs;
  932. cmd->tvc_vq = vq;
  933. cmd->tvc_resp_iov = &vq->iov[out];
  934. cmd->tvc_in_iovs = in;
  935. pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
  936. cmd->tvc_cdb[0], cmd->tvc_lun);
  937. pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
  938. " %d\n", cmd, exp_data_len, prot_bytes, data_direction);
  939. if (data_direction != DMA_NONE) {
  940. ret = vhost_scsi_mapal(cmd,
  941. prot_bytes, &prot_iter,
  942. exp_data_len, &data_iter);
  943. if (unlikely(ret)) {
  944. vq_err(vq, "Failed to map iov to sgl\n");
  945. vhost_scsi_release_cmd(&cmd->tvc_se_cmd);
  946. vhost_scsi_send_bad_target(vs, vq, head, out);
  947. continue;
  948. }
  949. }
  950. /*
  951. * Save the descriptor from vhost_get_vq_desc() to be used to
  952. * complete the virtio-scsi request in TCM callback context via
  953. * vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
  954. */
  955. cmd->tvc_vq_desc = head;
  956. /*
  957. * Dispatch cmd descriptor for cmwq execution in process
  958. * context provided by vhost_scsi_workqueue. This also ensures
  959. * cmd is executed on the same kworker CPU as this vhost
  960. * thread to gain positive L2 cache locality effects.
  961. */
  962. INIT_WORK(&cmd->work, vhost_scsi_submission_work);
  963. queue_work(vhost_scsi_workqueue, &cmd->work);
  964. }
  965. out:
  966. mutex_unlock(&vq->mutex);
  967. }
  968. static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
  969. {
  970. pr_debug("%s: The handling func for control queue.\n", __func__);
  971. }
  972. static void
  973. vhost_scsi_send_evt(struct vhost_scsi *vs,
  974. struct vhost_scsi_tpg *tpg,
  975. struct se_lun *lun,
  976. u32 event,
  977. u32 reason)
  978. {
  979. struct vhost_scsi_evt *evt;
  980. evt = vhost_scsi_allocate_evt(vs, event, reason);
  981. if (!evt)
  982. return;
  983. if (tpg && lun) {
  984. /* TODO: share lun setup code with virtio-scsi.ko */
  985. /*
  986. * Note: evt->event is zeroed when we allocate it and
  987. * lun[4-7] need to be zero according to virtio-scsi spec.
  988. */
  989. evt->event.lun[0] = 0x01;
  990. evt->event.lun[1] = tpg->tport_tpgt;
  991. if (lun->unpacked_lun >= 256)
  992. evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
  993. evt->event.lun[3] = lun->unpacked_lun & 0xFF;
  994. }
  995. llist_add(&evt->list, &vs->vs_event_list);
  996. vhost_work_queue(&vs->dev, &vs->vs_event_work);
  997. }
  998. static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
  999. {
  1000. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1001. poll.work);
  1002. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  1003. mutex_lock(&vq->mutex);
  1004. if (!vq->private_data)
  1005. goto out;
  1006. if (vs->vs_events_missed)
  1007. vhost_scsi_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
  1008. out:
  1009. mutex_unlock(&vq->mutex);
  1010. }
  1011. static void vhost_scsi_handle_kick(struct vhost_work *work)
  1012. {
  1013. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1014. poll.work);
  1015. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  1016. vhost_scsi_handle_vq(vs, vq);
  1017. }
  1018. static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
  1019. {
  1020. vhost_poll_flush(&vs->vqs[index].vq.poll);
  1021. }
  1022. /* Callers must hold dev mutex */
  1023. static void vhost_scsi_flush(struct vhost_scsi *vs)
  1024. {
  1025. struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
  1026. int i;
  1027. /* Init new inflight and remember the old inflight */
  1028. vhost_scsi_init_inflight(vs, old_inflight);
  1029. /*
  1030. * The inflight->kref was initialized to 1. We decrement it here to
  1031. * indicate the start of the flush operation so that it will reach 0
  1032. * when all the reqs are finished.
  1033. */
  1034. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1035. kref_put(&old_inflight[i]->kref, vhost_scsi_done_inflight);
  1036. /* Flush both the vhost poll and vhost work */
  1037. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1038. vhost_scsi_flush_vq(vs, i);
  1039. vhost_work_flush(&vs->dev, &vs->vs_completion_work);
  1040. vhost_work_flush(&vs->dev, &vs->vs_event_work);
  1041. /* Wait for all reqs issued before the flush to be finished */
  1042. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1043. wait_for_completion(&old_inflight[i]->comp);
  1044. }
  1045. /*
  1046. * Called from vhost_scsi_ioctl() context to walk the list of available
  1047. * vhost_scsi_tpg with an active struct vhost_scsi_nexus
  1048. *
  1049. * The lock nesting rule is:
  1050. * vhost_scsi_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
  1051. */
  1052. static int
  1053. vhost_scsi_set_endpoint(struct vhost_scsi *vs,
  1054. struct vhost_scsi_target *t)
  1055. {
  1056. struct se_portal_group *se_tpg;
  1057. struct vhost_scsi_tport *tv_tport;
  1058. struct vhost_scsi_tpg *tpg;
  1059. struct vhost_scsi_tpg **vs_tpg;
  1060. struct vhost_virtqueue *vq;
  1061. int index, ret, i, len;
  1062. bool match = false;
  1063. mutex_lock(&vhost_scsi_mutex);
  1064. mutex_lock(&vs->dev.mutex);
  1065. /* Verify that ring has been setup correctly. */
  1066. for (index = 0; index < vs->dev.nvqs; ++index) {
  1067. /* Verify that ring has been setup correctly. */
  1068. if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
  1069. ret = -EFAULT;
  1070. goto out;
  1071. }
  1072. }
  1073. len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
  1074. vs_tpg = kzalloc(len, GFP_KERNEL);
  1075. if (!vs_tpg) {
  1076. ret = -ENOMEM;
  1077. goto out;
  1078. }
  1079. if (vs->vs_tpg)
  1080. memcpy(vs_tpg, vs->vs_tpg, len);
  1081. list_for_each_entry(tpg, &vhost_scsi_list, tv_tpg_list) {
  1082. mutex_lock(&tpg->tv_tpg_mutex);
  1083. if (!tpg->tpg_nexus) {
  1084. mutex_unlock(&tpg->tv_tpg_mutex);
  1085. continue;
  1086. }
  1087. if (tpg->tv_tpg_vhost_count != 0) {
  1088. mutex_unlock(&tpg->tv_tpg_mutex);
  1089. continue;
  1090. }
  1091. tv_tport = tpg->tport;
  1092. if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  1093. if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
  1094. kfree(vs_tpg);
  1095. mutex_unlock(&tpg->tv_tpg_mutex);
  1096. ret = -EEXIST;
  1097. goto out;
  1098. }
  1099. /*
  1100. * In order to ensure individual vhost-scsi configfs
  1101. * groups cannot be removed while in use by vhost ioctl,
  1102. * go ahead and take an explicit se_tpg->tpg_group.cg_item
  1103. * dependency now.
  1104. */
  1105. se_tpg = &tpg->se_tpg;
  1106. ret = target_depend_item(&se_tpg->tpg_group.cg_item);
  1107. if (ret) {
  1108. pr_warn("configfs_depend_item() failed: %d\n", ret);
  1109. kfree(vs_tpg);
  1110. mutex_unlock(&tpg->tv_tpg_mutex);
  1111. goto out;
  1112. }
  1113. tpg->tv_tpg_vhost_count++;
  1114. tpg->vhost_scsi = vs;
  1115. vs_tpg[tpg->tport_tpgt] = tpg;
  1116. smp_mb__after_atomic();
  1117. match = true;
  1118. }
  1119. mutex_unlock(&tpg->tv_tpg_mutex);
  1120. }
  1121. if (match) {
  1122. memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
  1123. sizeof(vs->vs_vhost_wwpn));
  1124. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1125. vq = &vs->vqs[i].vq;
  1126. mutex_lock(&vq->mutex);
  1127. vq->private_data = vs_tpg;
  1128. vhost_init_used(vq);
  1129. mutex_unlock(&vq->mutex);
  1130. }
  1131. ret = 0;
  1132. } else {
  1133. ret = -EEXIST;
  1134. }
  1135. /*
  1136. * Act as synchronize_rcu to make sure access to
  1137. * old vs->vs_tpg is finished.
  1138. */
  1139. vhost_scsi_flush(vs);
  1140. kfree(vs->vs_tpg);
  1141. vs->vs_tpg = vs_tpg;
  1142. out:
  1143. mutex_unlock(&vs->dev.mutex);
  1144. mutex_unlock(&vhost_scsi_mutex);
  1145. return ret;
  1146. }
  1147. static int
  1148. vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
  1149. struct vhost_scsi_target *t)
  1150. {
  1151. struct se_portal_group *se_tpg;
  1152. struct vhost_scsi_tport *tv_tport;
  1153. struct vhost_scsi_tpg *tpg;
  1154. struct vhost_virtqueue *vq;
  1155. bool match = false;
  1156. int index, ret, i;
  1157. u8 target;
  1158. mutex_lock(&vhost_scsi_mutex);
  1159. mutex_lock(&vs->dev.mutex);
  1160. /* Verify that ring has been setup correctly. */
  1161. for (index = 0; index < vs->dev.nvqs; ++index) {
  1162. if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
  1163. ret = -EFAULT;
  1164. goto err_dev;
  1165. }
  1166. }
  1167. if (!vs->vs_tpg) {
  1168. ret = 0;
  1169. goto err_dev;
  1170. }
  1171. for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
  1172. target = i;
  1173. tpg = vs->vs_tpg[target];
  1174. if (!tpg)
  1175. continue;
  1176. mutex_lock(&tpg->tv_tpg_mutex);
  1177. tv_tport = tpg->tport;
  1178. if (!tv_tport) {
  1179. ret = -ENODEV;
  1180. goto err_tpg;
  1181. }
  1182. if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  1183. pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
  1184. " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
  1185. tv_tport->tport_name, tpg->tport_tpgt,
  1186. t->vhost_wwpn, t->vhost_tpgt);
  1187. ret = -EINVAL;
  1188. goto err_tpg;
  1189. }
  1190. tpg->tv_tpg_vhost_count--;
  1191. tpg->vhost_scsi = NULL;
  1192. vs->vs_tpg[target] = NULL;
  1193. match = true;
  1194. mutex_unlock(&tpg->tv_tpg_mutex);
  1195. /*
  1196. * Release se_tpg->tpg_group.cg_item configfs dependency now
  1197. * to allow vhost-scsi WWPN se_tpg->tpg_group shutdown to occur.
  1198. */
  1199. se_tpg = &tpg->se_tpg;
  1200. target_undepend_item(&se_tpg->tpg_group.cg_item);
  1201. }
  1202. if (match) {
  1203. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1204. vq = &vs->vqs[i].vq;
  1205. mutex_lock(&vq->mutex);
  1206. vq->private_data = NULL;
  1207. mutex_unlock(&vq->mutex);
  1208. }
  1209. }
  1210. /*
  1211. * Act as synchronize_rcu to make sure access to
  1212. * old vs->vs_tpg is finished.
  1213. */
  1214. vhost_scsi_flush(vs);
  1215. kfree(vs->vs_tpg);
  1216. vs->vs_tpg = NULL;
  1217. WARN_ON(vs->vs_events_nr);
  1218. mutex_unlock(&vs->dev.mutex);
  1219. mutex_unlock(&vhost_scsi_mutex);
  1220. return 0;
  1221. err_tpg:
  1222. mutex_unlock(&tpg->tv_tpg_mutex);
  1223. err_dev:
  1224. mutex_unlock(&vs->dev.mutex);
  1225. mutex_unlock(&vhost_scsi_mutex);
  1226. return ret;
  1227. }
  1228. static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
  1229. {
  1230. struct vhost_virtqueue *vq;
  1231. int i;
  1232. if (features & ~VHOST_SCSI_FEATURES)
  1233. return -EOPNOTSUPP;
  1234. mutex_lock(&vs->dev.mutex);
  1235. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  1236. !vhost_log_access_ok(&vs->dev)) {
  1237. mutex_unlock(&vs->dev.mutex);
  1238. return -EFAULT;
  1239. }
  1240. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1241. vq = &vs->vqs[i].vq;
  1242. mutex_lock(&vq->mutex);
  1243. vq->acked_features = features;
  1244. mutex_unlock(&vq->mutex);
  1245. }
  1246. mutex_unlock(&vs->dev.mutex);
  1247. return 0;
  1248. }
  1249. static int vhost_scsi_open(struct inode *inode, struct file *f)
  1250. {
  1251. struct vhost_scsi *vs;
  1252. struct vhost_virtqueue **vqs;
  1253. int r = -ENOMEM, i;
  1254. vs = kzalloc(sizeof(*vs), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  1255. if (!vs) {
  1256. vs = vzalloc(sizeof(*vs));
  1257. if (!vs)
  1258. goto err_vs;
  1259. }
  1260. vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
  1261. if (!vqs)
  1262. goto err_vqs;
  1263. vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
  1264. vhost_work_init(&vs->vs_event_work, vhost_scsi_evt_work);
  1265. vs->vs_events_nr = 0;
  1266. vs->vs_events_missed = false;
  1267. vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
  1268. vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1269. vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
  1270. vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
  1271. for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
  1272. vqs[i] = &vs->vqs[i].vq;
  1273. vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
  1274. }
  1275. vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
  1276. vhost_scsi_init_inflight(vs, NULL);
  1277. f->private_data = vs;
  1278. return 0;
  1279. err_vqs:
  1280. kvfree(vs);
  1281. err_vs:
  1282. return r;
  1283. }
  1284. static int vhost_scsi_release(struct inode *inode, struct file *f)
  1285. {
  1286. struct vhost_scsi *vs = f->private_data;
  1287. struct vhost_scsi_target t;
  1288. mutex_lock(&vs->dev.mutex);
  1289. memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
  1290. mutex_unlock(&vs->dev.mutex);
  1291. vhost_scsi_clear_endpoint(vs, &t);
  1292. vhost_dev_stop(&vs->dev);
  1293. vhost_dev_cleanup(&vs->dev, false);
  1294. /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
  1295. vhost_scsi_flush(vs);
  1296. kfree(vs->dev.vqs);
  1297. kvfree(vs);
  1298. return 0;
  1299. }
  1300. static long
  1301. vhost_scsi_ioctl(struct file *f,
  1302. unsigned int ioctl,
  1303. unsigned long arg)
  1304. {
  1305. struct vhost_scsi *vs = f->private_data;
  1306. struct vhost_scsi_target backend;
  1307. void __user *argp = (void __user *)arg;
  1308. u64 __user *featurep = argp;
  1309. u32 __user *eventsp = argp;
  1310. u32 events_missed;
  1311. u64 features;
  1312. int r, abi_version = VHOST_SCSI_ABI_VERSION;
  1313. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1314. switch (ioctl) {
  1315. case VHOST_SCSI_SET_ENDPOINT:
  1316. if (copy_from_user(&backend, argp, sizeof backend))
  1317. return -EFAULT;
  1318. if (backend.reserved != 0)
  1319. return -EOPNOTSUPP;
  1320. return vhost_scsi_set_endpoint(vs, &backend);
  1321. case VHOST_SCSI_CLEAR_ENDPOINT:
  1322. if (copy_from_user(&backend, argp, sizeof backend))
  1323. return -EFAULT;
  1324. if (backend.reserved != 0)
  1325. return -EOPNOTSUPP;
  1326. return vhost_scsi_clear_endpoint(vs, &backend);
  1327. case VHOST_SCSI_GET_ABI_VERSION:
  1328. if (copy_to_user(argp, &abi_version, sizeof abi_version))
  1329. return -EFAULT;
  1330. return 0;
  1331. case VHOST_SCSI_SET_EVENTS_MISSED:
  1332. if (get_user(events_missed, eventsp))
  1333. return -EFAULT;
  1334. mutex_lock(&vq->mutex);
  1335. vs->vs_events_missed = events_missed;
  1336. mutex_unlock(&vq->mutex);
  1337. return 0;
  1338. case VHOST_SCSI_GET_EVENTS_MISSED:
  1339. mutex_lock(&vq->mutex);
  1340. events_missed = vs->vs_events_missed;
  1341. mutex_unlock(&vq->mutex);
  1342. if (put_user(events_missed, eventsp))
  1343. return -EFAULT;
  1344. return 0;
  1345. case VHOST_GET_FEATURES:
  1346. features = VHOST_SCSI_FEATURES;
  1347. if (copy_to_user(featurep, &features, sizeof features))
  1348. return -EFAULT;
  1349. return 0;
  1350. case VHOST_SET_FEATURES:
  1351. if (copy_from_user(&features, featurep, sizeof features))
  1352. return -EFAULT;
  1353. return vhost_scsi_set_features(vs, features);
  1354. default:
  1355. mutex_lock(&vs->dev.mutex);
  1356. r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
  1357. /* TODO: flush backend after dev ioctl. */
  1358. if (r == -ENOIOCTLCMD)
  1359. r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
  1360. mutex_unlock(&vs->dev.mutex);
  1361. return r;
  1362. }
  1363. }
  1364. #ifdef CONFIG_COMPAT
  1365. static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
  1366. unsigned long arg)
  1367. {
  1368. return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  1369. }
  1370. #endif
  1371. static const struct file_operations vhost_scsi_fops = {
  1372. .owner = THIS_MODULE,
  1373. .release = vhost_scsi_release,
  1374. .unlocked_ioctl = vhost_scsi_ioctl,
  1375. #ifdef CONFIG_COMPAT
  1376. .compat_ioctl = vhost_scsi_compat_ioctl,
  1377. #endif
  1378. .open = vhost_scsi_open,
  1379. .llseek = noop_llseek,
  1380. };
  1381. static struct miscdevice vhost_scsi_misc = {
  1382. MISC_DYNAMIC_MINOR,
  1383. "vhost-scsi",
  1384. &vhost_scsi_fops,
  1385. };
  1386. static int __init vhost_scsi_register(void)
  1387. {
  1388. return misc_register(&vhost_scsi_misc);
  1389. }
  1390. static void vhost_scsi_deregister(void)
  1391. {
  1392. misc_deregister(&vhost_scsi_misc);
  1393. }
  1394. static char *vhost_scsi_dump_proto_id(struct vhost_scsi_tport *tport)
  1395. {
  1396. switch (tport->tport_proto_id) {
  1397. case SCSI_PROTOCOL_SAS:
  1398. return "SAS";
  1399. case SCSI_PROTOCOL_FCP:
  1400. return "FCP";
  1401. case SCSI_PROTOCOL_ISCSI:
  1402. return "iSCSI";
  1403. default:
  1404. break;
  1405. }
  1406. return "Unknown";
  1407. }
  1408. static void
  1409. vhost_scsi_do_plug(struct vhost_scsi_tpg *tpg,
  1410. struct se_lun *lun, bool plug)
  1411. {
  1412. struct vhost_scsi *vs = tpg->vhost_scsi;
  1413. struct vhost_virtqueue *vq;
  1414. u32 reason;
  1415. if (!vs)
  1416. return;
  1417. mutex_lock(&vs->dev.mutex);
  1418. if (plug)
  1419. reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
  1420. else
  1421. reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
  1422. vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1423. mutex_lock(&vq->mutex);
  1424. if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG))
  1425. vhost_scsi_send_evt(vs, tpg, lun,
  1426. VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
  1427. mutex_unlock(&vq->mutex);
  1428. mutex_unlock(&vs->dev.mutex);
  1429. }
  1430. static void vhost_scsi_hotplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
  1431. {
  1432. vhost_scsi_do_plug(tpg, lun, true);
  1433. }
  1434. static void vhost_scsi_hotunplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
  1435. {
  1436. vhost_scsi_do_plug(tpg, lun, false);
  1437. }
  1438. static int vhost_scsi_port_link(struct se_portal_group *se_tpg,
  1439. struct se_lun *lun)
  1440. {
  1441. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1442. struct vhost_scsi_tpg, se_tpg);
  1443. mutex_lock(&vhost_scsi_mutex);
  1444. mutex_lock(&tpg->tv_tpg_mutex);
  1445. tpg->tv_tpg_port_count++;
  1446. mutex_unlock(&tpg->tv_tpg_mutex);
  1447. vhost_scsi_hotplug(tpg, lun);
  1448. mutex_unlock(&vhost_scsi_mutex);
  1449. return 0;
  1450. }
  1451. static void vhost_scsi_port_unlink(struct se_portal_group *se_tpg,
  1452. struct se_lun *lun)
  1453. {
  1454. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1455. struct vhost_scsi_tpg, se_tpg);
  1456. mutex_lock(&vhost_scsi_mutex);
  1457. mutex_lock(&tpg->tv_tpg_mutex);
  1458. tpg->tv_tpg_port_count--;
  1459. mutex_unlock(&tpg->tv_tpg_mutex);
  1460. vhost_scsi_hotunplug(tpg, lun);
  1461. mutex_unlock(&vhost_scsi_mutex);
  1462. }
  1463. static void vhost_scsi_free_cmd_map_res(struct vhost_scsi_nexus *nexus,
  1464. struct se_session *se_sess)
  1465. {
  1466. struct vhost_scsi_cmd *tv_cmd;
  1467. unsigned int i;
  1468. if (!se_sess->sess_cmd_map)
  1469. return;
  1470. for (i = 0; i < VHOST_SCSI_DEFAULT_TAGS; i++) {
  1471. tv_cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[i];
  1472. kfree(tv_cmd->tvc_sgl);
  1473. kfree(tv_cmd->tvc_prot_sgl);
  1474. kfree(tv_cmd->tvc_upages);
  1475. }
  1476. }
  1477. static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_store(
  1478. struct config_item *item, const char *page, size_t count)
  1479. {
  1480. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  1481. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1482. struct vhost_scsi_tpg, se_tpg);
  1483. unsigned long val;
  1484. int ret = kstrtoul(page, 0, &val);
  1485. if (ret) {
  1486. pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
  1487. return ret;
  1488. }
  1489. if (val != 0 && val != 1 && val != 3) {
  1490. pr_err("Invalid vhost_scsi fabric_prot_type: %lu\n", val);
  1491. return -EINVAL;
  1492. }
  1493. tpg->tv_fabric_prot_type = val;
  1494. return count;
  1495. }
  1496. static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_show(
  1497. struct config_item *item, char *page)
  1498. {
  1499. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  1500. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1501. struct vhost_scsi_tpg, se_tpg);
  1502. return sprintf(page, "%d\n", tpg->tv_fabric_prot_type);
  1503. }
  1504. CONFIGFS_ATTR(vhost_scsi_tpg_attrib_, fabric_prot_type);
  1505. static struct configfs_attribute *vhost_scsi_tpg_attrib_attrs[] = {
  1506. &vhost_scsi_tpg_attrib_attr_fabric_prot_type,
  1507. NULL,
  1508. };
  1509. static int vhost_scsi_make_nexus(struct vhost_scsi_tpg *tpg,
  1510. const char *name)
  1511. {
  1512. struct se_portal_group *se_tpg;
  1513. struct se_session *se_sess;
  1514. struct vhost_scsi_nexus *tv_nexus;
  1515. struct vhost_scsi_cmd *tv_cmd;
  1516. unsigned int i;
  1517. mutex_lock(&tpg->tv_tpg_mutex);
  1518. if (tpg->tpg_nexus) {
  1519. mutex_unlock(&tpg->tv_tpg_mutex);
  1520. pr_debug("tpg->tpg_nexus already exists\n");
  1521. return -EEXIST;
  1522. }
  1523. se_tpg = &tpg->se_tpg;
  1524. tv_nexus = kzalloc(sizeof(struct vhost_scsi_nexus), GFP_KERNEL);
  1525. if (!tv_nexus) {
  1526. mutex_unlock(&tpg->tv_tpg_mutex);
  1527. pr_err("Unable to allocate struct vhost_scsi_nexus\n");
  1528. return -ENOMEM;
  1529. }
  1530. /*
  1531. * Initialize the struct se_session pointer and setup tagpool
  1532. * for struct vhost_scsi_cmd descriptors
  1533. */
  1534. tv_nexus->tvn_se_sess = transport_init_session_tags(
  1535. VHOST_SCSI_DEFAULT_TAGS,
  1536. sizeof(struct vhost_scsi_cmd),
  1537. TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS);
  1538. if (IS_ERR(tv_nexus->tvn_se_sess)) {
  1539. mutex_unlock(&tpg->tv_tpg_mutex);
  1540. kfree(tv_nexus);
  1541. return -ENOMEM;
  1542. }
  1543. se_sess = tv_nexus->tvn_se_sess;
  1544. for (i = 0; i < VHOST_SCSI_DEFAULT_TAGS; i++) {
  1545. tv_cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[i];
  1546. tv_cmd->tvc_sgl = kzalloc(sizeof(struct scatterlist) *
  1547. VHOST_SCSI_PREALLOC_SGLS, GFP_KERNEL);
  1548. if (!tv_cmd->tvc_sgl) {
  1549. mutex_unlock(&tpg->tv_tpg_mutex);
  1550. pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
  1551. goto out;
  1552. }
  1553. tv_cmd->tvc_upages = kzalloc(sizeof(struct page *) *
  1554. VHOST_SCSI_PREALLOC_UPAGES, GFP_KERNEL);
  1555. if (!tv_cmd->tvc_upages) {
  1556. mutex_unlock(&tpg->tv_tpg_mutex);
  1557. pr_err("Unable to allocate tv_cmd->tvc_upages\n");
  1558. goto out;
  1559. }
  1560. tv_cmd->tvc_prot_sgl = kzalloc(sizeof(struct scatterlist) *
  1561. VHOST_SCSI_PREALLOC_PROT_SGLS, GFP_KERNEL);
  1562. if (!tv_cmd->tvc_prot_sgl) {
  1563. mutex_unlock(&tpg->tv_tpg_mutex);
  1564. pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
  1565. goto out;
  1566. }
  1567. }
  1568. /*
  1569. * Since we are running in 'demo mode' this call with generate a
  1570. * struct se_node_acl for the vhost_scsi struct se_portal_group with
  1571. * the SCSI Initiator port name of the passed configfs group 'name'.
  1572. */
  1573. tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  1574. se_tpg, (unsigned char *)name);
  1575. if (!tv_nexus->tvn_se_sess->se_node_acl) {
  1576. mutex_unlock(&tpg->tv_tpg_mutex);
  1577. pr_debug("core_tpg_check_initiator_node_acl() failed"
  1578. " for %s\n", name);
  1579. goto out;
  1580. }
  1581. /*
  1582. * Now register the TCM vhost virtual I_T Nexus as active.
  1583. */
  1584. transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
  1585. tv_nexus->tvn_se_sess, tv_nexus);
  1586. tpg->tpg_nexus = tv_nexus;
  1587. mutex_unlock(&tpg->tv_tpg_mutex);
  1588. return 0;
  1589. out:
  1590. vhost_scsi_free_cmd_map_res(tv_nexus, se_sess);
  1591. transport_free_session(se_sess);
  1592. kfree(tv_nexus);
  1593. return -ENOMEM;
  1594. }
  1595. static int vhost_scsi_drop_nexus(struct vhost_scsi_tpg *tpg)
  1596. {
  1597. struct se_session *se_sess;
  1598. struct vhost_scsi_nexus *tv_nexus;
  1599. mutex_lock(&tpg->tv_tpg_mutex);
  1600. tv_nexus = tpg->tpg_nexus;
  1601. if (!tv_nexus) {
  1602. mutex_unlock(&tpg->tv_tpg_mutex);
  1603. return -ENODEV;
  1604. }
  1605. se_sess = tv_nexus->tvn_se_sess;
  1606. if (!se_sess) {
  1607. mutex_unlock(&tpg->tv_tpg_mutex);
  1608. return -ENODEV;
  1609. }
  1610. if (tpg->tv_tpg_port_count != 0) {
  1611. mutex_unlock(&tpg->tv_tpg_mutex);
  1612. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1613. " active TPG port count: %d\n",
  1614. tpg->tv_tpg_port_count);
  1615. return -EBUSY;
  1616. }
  1617. if (tpg->tv_tpg_vhost_count != 0) {
  1618. mutex_unlock(&tpg->tv_tpg_mutex);
  1619. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1620. " active TPG vhost count: %d\n",
  1621. tpg->tv_tpg_vhost_count);
  1622. return -EBUSY;
  1623. }
  1624. pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
  1625. " %s Initiator Port: %s\n", vhost_scsi_dump_proto_id(tpg->tport),
  1626. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1627. vhost_scsi_free_cmd_map_res(tv_nexus, se_sess);
  1628. /*
  1629. * Release the SCSI I_T Nexus to the emulated vhost Target Port
  1630. */
  1631. transport_deregister_session(tv_nexus->tvn_se_sess);
  1632. tpg->tpg_nexus = NULL;
  1633. mutex_unlock(&tpg->tv_tpg_mutex);
  1634. kfree(tv_nexus);
  1635. return 0;
  1636. }
  1637. static ssize_t vhost_scsi_tpg_nexus_show(struct config_item *item, char *page)
  1638. {
  1639. struct se_portal_group *se_tpg = to_tpg(item);
  1640. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1641. struct vhost_scsi_tpg, se_tpg);
  1642. struct vhost_scsi_nexus *tv_nexus;
  1643. ssize_t ret;
  1644. mutex_lock(&tpg->tv_tpg_mutex);
  1645. tv_nexus = tpg->tpg_nexus;
  1646. if (!tv_nexus) {
  1647. mutex_unlock(&tpg->tv_tpg_mutex);
  1648. return -ENODEV;
  1649. }
  1650. ret = snprintf(page, PAGE_SIZE, "%s\n",
  1651. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1652. mutex_unlock(&tpg->tv_tpg_mutex);
  1653. return ret;
  1654. }
  1655. static ssize_t vhost_scsi_tpg_nexus_store(struct config_item *item,
  1656. const char *page, size_t count)
  1657. {
  1658. struct se_portal_group *se_tpg = to_tpg(item);
  1659. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1660. struct vhost_scsi_tpg, se_tpg);
  1661. struct vhost_scsi_tport *tport_wwn = tpg->tport;
  1662. unsigned char i_port[VHOST_SCSI_NAMELEN], *ptr, *port_ptr;
  1663. int ret;
  1664. /*
  1665. * Shutdown the active I_T nexus if 'NULL' is passed..
  1666. */
  1667. if (!strncmp(page, "NULL", 4)) {
  1668. ret = vhost_scsi_drop_nexus(tpg);
  1669. return (!ret) ? count : ret;
  1670. }
  1671. /*
  1672. * Otherwise make sure the passed virtual Initiator port WWN matches
  1673. * the fabric protocol_id set in vhost_scsi_make_tport(), and call
  1674. * vhost_scsi_make_nexus().
  1675. */
  1676. if (strlen(page) >= VHOST_SCSI_NAMELEN) {
  1677. pr_err("Emulated NAA Sas Address: %s, exceeds"
  1678. " max: %d\n", page, VHOST_SCSI_NAMELEN);
  1679. return -EINVAL;
  1680. }
  1681. snprintf(&i_port[0], VHOST_SCSI_NAMELEN, "%s", page);
  1682. ptr = strstr(i_port, "naa.");
  1683. if (ptr) {
  1684. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
  1685. pr_err("Passed SAS Initiator Port %s does not"
  1686. " match target port protoid: %s\n", i_port,
  1687. vhost_scsi_dump_proto_id(tport_wwn));
  1688. return -EINVAL;
  1689. }
  1690. port_ptr = &i_port[0];
  1691. goto check_newline;
  1692. }
  1693. ptr = strstr(i_port, "fc.");
  1694. if (ptr) {
  1695. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
  1696. pr_err("Passed FCP Initiator Port %s does not"
  1697. " match target port protoid: %s\n", i_port,
  1698. vhost_scsi_dump_proto_id(tport_wwn));
  1699. return -EINVAL;
  1700. }
  1701. port_ptr = &i_port[3]; /* Skip over "fc." */
  1702. goto check_newline;
  1703. }
  1704. ptr = strstr(i_port, "iqn.");
  1705. if (ptr) {
  1706. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
  1707. pr_err("Passed iSCSI Initiator Port %s does not"
  1708. " match target port protoid: %s\n", i_port,
  1709. vhost_scsi_dump_proto_id(tport_wwn));
  1710. return -EINVAL;
  1711. }
  1712. port_ptr = &i_port[0];
  1713. goto check_newline;
  1714. }
  1715. pr_err("Unable to locate prefix for emulated Initiator Port:"
  1716. " %s\n", i_port);
  1717. return -EINVAL;
  1718. /*
  1719. * Clear any trailing newline for the NAA WWN
  1720. */
  1721. check_newline:
  1722. if (i_port[strlen(i_port)-1] == '\n')
  1723. i_port[strlen(i_port)-1] = '\0';
  1724. ret = vhost_scsi_make_nexus(tpg, port_ptr);
  1725. if (ret < 0)
  1726. return ret;
  1727. return count;
  1728. }
  1729. CONFIGFS_ATTR(vhost_scsi_tpg_, nexus);
  1730. static struct configfs_attribute *vhost_scsi_tpg_attrs[] = {
  1731. &vhost_scsi_tpg_attr_nexus,
  1732. NULL,
  1733. };
  1734. static struct se_portal_group *
  1735. vhost_scsi_make_tpg(struct se_wwn *wwn,
  1736. struct config_group *group,
  1737. const char *name)
  1738. {
  1739. struct vhost_scsi_tport *tport = container_of(wwn,
  1740. struct vhost_scsi_tport, tport_wwn);
  1741. struct vhost_scsi_tpg *tpg;
  1742. u16 tpgt;
  1743. int ret;
  1744. if (strstr(name, "tpgt_") != name)
  1745. return ERR_PTR(-EINVAL);
  1746. if (kstrtou16(name + 5, 10, &tpgt) || tpgt >= VHOST_SCSI_MAX_TARGET)
  1747. return ERR_PTR(-EINVAL);
  1748. tpg = kzalloc(sizeof(struct vhost_scsi_tpg), GFP_KERNEL);
  1749. if (!tpg) {
  1750. pr_err("Unable to allocate struct vhost_scsi_tpg");
  1751. return ERR_PTR(-ENOMEM);
  1752. }
  1753. mutex_init(&tpg->tv_tpg_mutex);
  1754. INIT_LIST_HEAD(&tpg->tv_tpg_list);
  1755. tpg->tport = tport;
  1756. tpg->tport_tpgt = tpgt;
  1757. ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
  1758. if (ret < 0) {
  1759. kfree(tpg);
  1760. return NULL;
  1761. }
  1762. mutex_lock(&vhost_scsi_mutex);
  1763. list_add_tail(&tpg->tv_tpg_list, &vhost_scsi_list);
  1764. mutex_unlock(&vhost_scsi_mutex);
  1765. return &tpg->se_tpg;
  1766. }
  1767. static void vhost_scsi_drop_tpg(struct se_portal_group *se_tpg)
  1768. {
  1769. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1770. struct vhost_scsi_tpg, se_tpg);
  1771. mutex_lock(&vhost_scsi_mutex);
  1772. list_del(&tpg->tv_tpg_list);
  1773. mutex_unlock(&vhost_scsi_mutex);
  1774. /*
  1775. * Release the virtual I_T Nexus for this vhost TPG
  1776. */
  1777. vhost_scsi_drop_nexus(tpg);
  1778. /*
  1779. * Deregister the se_tpg from TCM..
  1780. */
  1781. core_tpg_deregister(se_tpg);
  1782. kfree(tpg);
  1783. }
  1784. static struct se_wwn *
  1785. vhost_scsi_make_tport(struct target_fabric_configfs *tf,
  1786. struct config_group *group,
  1787. const char *name)
  1788. {
  1789. struct vhost_scsi_tport *tport;
  1790. char *ptr;
  1791. u64 wwpn = 0;
  1792. int off = 0;
  1793. /* if (vhost_scsi_parse_wwn(name, &wwpn, 1) < 0)
  1794. return ERR_PTR(-EINVAL); */
  1795. tport = kzalloc(sizeof(struct vhost_scsi_tport), GFP_KERNEL);
  1796. if (!tport) {
  1797. pr_err("Unable to allocate struct vhost_scsi_tport");
  1798. return ERR_PTR(-ENOMEM);
  1799. }
  1800. tport->tport_wwpn = wwpn;
  1801. /*
  1802. * Determine the emulated Protocol Identifier and Target Port Name
  1803. * based on the incoming configfs directory name.
  1804. */
  1805. ptr = strstr(name, "naa.");
  1806. if (ptr) {
  1807. tport->tport_proto_id = SCSI_PROTOCOL_SAS;
  1808. goto check_len;
  1809. }
  1810. ptr = strstr(name, "fc.");
  1811. if (ptr) {
  1812. tport->tport_proto_id = SCSI_PROTOCOL_FCP;
  1813. off = 3; /* Skip over "fc." */
  1814. goto check_len;
  1815. }
  1816. ptr = strstr(name, "iqn.");
  1817. if (ptr) {
  1818. tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
  1819. goto check_len;
  1820. }
  1821. pr_err("Unable to locate prefix for emulated Target Port:"
  1822. " %s\n", name);
  1823. kfree(tport);
  1824. return ERR_PTR(-EINVAL);
  1825. check_len:
  1826. if (strlen(name) >= VHOST_SCSI_NAMELEN) {
  1827. pr_err("Emulated %s Address: %s, exceeds"
  1828. " max: %d\n", name, vhost_scsi_dump_proto_id(tport),
  1829. VHOST_SCSI_NAMELEN);
  1830. kfree(tport);
  1831. return ERR_PTR(-EINVAL);
  1832. }
  1833. snprintf(&tport->tport_name[0], VHOST_SCSI_NAMELEN, "%s", &name[off]);
  1834. pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
  1835. " %s Address: %s\n", vhost_scsi_dump_proto_id(tport), name);
  1836. return &tport->tport_wwn;
  1837. }
  1838. static void vhost_scsi_drop_tport(struct se_wwn *wwn)
  1839. {
  1840. struct vhost_scsi_tport *tport = container_of(wwn,
  1841. struct vhost_scsi_tport, tport_wwn);
  1842. pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
  1843. " %s Address: %s\n", vhost_scsi_dump_proto_id(tport),
  1844. tport->tport_name);
  1845. kfree(tport);
  1846. }
  1847. static ssize_t
  1848. vhost_scsi_wwn_version_show(struct config_item *item, char *page)
  1849. {
  1850. return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
  1851. "on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
  1852. utsname()->machine);
  1853. }
  1854. CONFIGFS_ATTR_RO(vhost_scsi_wwn_, version);
  1855. static struct configfs_attribute *vhost_scsi_wwn_attrs[] = {
  1856. &vhost_scsi_wwn_attr_version,
  1857. NULL,
  1858. };
  1859. static struct target_core_fabric_ops vhost_scsi_ops = {
  1860. .module = THIS_MODULE,
  1861. .name = "vhost",
  1862. .get_fabric_name = vhost_scsi_get_fabric_name,
  1863. .tpg_get_wwn = vhost_scsi_get_fabric_wwn,
  1864. .tpg_get_tag = vhost_scsi_get_tpgt,
  1865. .tpg_check_demo_mode = vhost_scsi_check_true,
  1866. .tpg_check_demo_mode_cache = vhost_scsi_check_true,
  1867. .tpg_check_demo_mode_write_protect = vhost_scsi_check_false,
  1868. .tpg_check_prod_mode_write_protect = vhost_scsi_check_false,
  1869. .tpg_check_prot_fabric_only = vhost_scsi_check_prot_fabric_only,
  1870. .tpg_get_inst_index = vhost_scsi_tpg_get_inst_index,
  1871. .release_cmd = vhost_scsi_release_cmd,
  1872. .check_stop_free = vhost_scsi_check_stop_free,
  1873. .shutdown_session = vhost_scsi_shutdown_session,
  1874. .close_session = vhost_scsi_close_session,
  1875. .sess_get_index = vhost_scsi_sess_get_index,
  1876. .sess_get_initiator_sid = NULL,
  1877. .write_pending = vhost_scsi_write_pending,
  1878. .write_pending_status = vhost_scsi_write_pending_status,
  1879. .set_default_node_attributes = vhost_scsi_set_default_node_attrs,
  1880. .get_cmd_state = vhost_scsi_get_cmd_state,
  1881. .queue_data_in = vhost_scsi_queue_data_in,
  1882. .queue_status = vhost_scsi_queue_status,
  1883. .queue_tm_rsp = vhost_scsi_queue_tm_rsp,
  1884. .aborted_task = vhost_scsi_aborted_task,
  1885. /*
  1886. * Setup callers for generic logic in target_core_fabric_configfs.c
  1887. */
  1888. .fabric_make_wwn = vhost_scsi_make_tport,
  1889. .fabric_drop_wwn = vhost_scsi_drop_tport,
  1890. .fabric_make_tpg = vhost_scsi_make_tpg,
  1891. .fabric_drop_tpg = vhost_scsi_drop_tpg,
  1892. .fabric_post_link = vhost_scsi_port_link,
  1893. .fabric_pre_unlink = vhost_scsi_port_unlink,
  1894. .tfc_wwn_attrs = vhost_scsi_wwn_attrs,
  1895. .tfc_tpg_base_attrs = vhost_scsi_tpg_attrs,
  1896. .tfc_tpg_attrib_attrs = vhost_scsi_tpg_attrib_attrs,
  1897. };
  1898. static int __init vhost_scsi_init(void)
  1899. {
  1900. int ret = -ENOMEM;
  1901. pr_debug("TCM_VHOST fabric module %s on %s/%s"
  1902. " on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
  1903. utsname()->machine);
  1904. /*
  1905. * Use our own dedicated workqueue for submitting I/O into
  1906. * target core to avoid contention within system_wq.
  1907. */
  1908. vhost_scsi_workqueue = alloc_workqueue("vhost_scsi", 0, 0);
  1909. if (!vhost_scsi_workqueue)
  1910. goto out;
  1911. ret = vhost_scsi_register();
  1912. if (ret < 0)
  1913. goto out_destroy_workqueue;
  1914. ret = target_register_template(&vhost_scsi_ops);
  1915. if (ret < 0)
  1916. goto out_vhost_scsi_deregister;
  1917. return 0;
  1918. out_vhost_scsi_deregister:
  1919. vhost_scsi_deregister();
  1920. out_destroy_workqueue:
  1921. destroy_workqueue(vhost_scsi_workqueue);
  1922. out:
  1923. return ret;
  1924. };
  1925. static void vhost_scsi_exit(void)
  1926. {
  1927. target_unregister_template(&vhost_scsi_ops);
  1928. vhost_scsi_deregister();
  1929. destroy_workqueue(vhost_scsi_workqueue);
  1930. };
  1931. MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
  1932. MODULE_ALIAS("tcm_vhost");
  1933. MODULE_LICENSE("GPL");
  1934. module_init(vhost_scsi_init);
  1935. module_exit(vhost_scsi_exit);