scsi.c 58 KB

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