scsi.c 63 KB

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