scsi.c 62 KB

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