scsi.c 58 KB

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