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