scsi.c 56 KB

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