scsi.c 56 KB

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