scsi.c 56 KB

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