scsi.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  1. /*******************************************************************************
  2. * Vhost kernel TCM fabric driver for virtio SCSI initiators
  3. *
  4. * (C) Copyright 2010-2013 Datera, Inc.
  5. * (C) Copyright 2010-2012 IBM Corp.
  6. *
  7. * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
  8. *
  9. * Authors: Nicholas A. Bellinger <nab@daterainc.com>
  10. * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. ****************************************************************************/
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <generated/utsrelease.h>
  26. #include <linux/utsname.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/kthread.h>
  30. #include <linux/types.h>
  31. #include <linux/string.h>
  32. #include <linux/configfs.h>
  33. #include <linux/ctype.h>
  34. #include <linux/compat.h>
  35. #include <linux/eventfd.h>
  36. #include <linux/fs.h>
  37. #include <linux/vmalloc.h>
  38. #include <linux/miscdevice.h>
  39. #include <asm/unaligned.h>
  40. #include <scsi/scsi_common.h>
  41. #include <scsi/scsi_proto.h>
  42. #include <target/target_core_base.h>
  43. #include <target/target_core_fabric.h>
  44. #include <linux/vhost.h>
  45. #include <linux/virtio_scsi.h>
  46. #include <linux/llist.h>
  47. #include <linux/bitmap.h>
  48. #include <linux/percpu_ida.h>
  49. #include "vhost.h"
  50. #define VHOST_SCSI_VERSION "v0.1"
  51. #define VHOST_SCSI_NAMELEN 256
  52. #define VHOST_SCSI_MAX_CDB_SIZE 32
  53. #define VHOST_SCSI_DEFAULT_TAGS 256
  54. #define VHOST_SCSI_PREALLOC_SGLS 2048
  55. #define VHOST_SCSI_PREALLOC_UPAGES 2048
  56. #define VHOST_SCSI_PREALLOC_PROT_SGLS 512
  57. struct vhost_scsi_inflight {
  58. /* Wait for the flush operation to finish */
  59. struct completion comp;
  60. /* Refcount for the inflight reqs */
  61. struct kref kref;
  62. };
  63. struct vhost_scsi_cmd {
  64. /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
  65. int tvc_vq_desc;
  66. /* virtio-scsi initiator task attribute */
  67. int tvc_task_attr;
  68. /* virtio-scsi response incoming iovecs */
  69. int tvc_in_iovs;
  70. /* virtio-scsi initiator data direction */
  71. enum dma_data_direction tvc_data_direction;
  72. /* Expected data transfer length from virtio-scsi header */
  73. u32 tvc_exp_data_len;
  74. /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
  75. u64 tvc_tag;
  76. /* The number of scatterlists associated with this cmd */
  77. u32 tvc_sgl_count;
  78. u32 tvc_prot_sgl_count;
  79. /* Saved unpacked SCSI LUN for vhost_scsi_submission_work() */
  80. u32 tvc_lun;
  81. /* Pointer to the SGL formatted memory from virtio-scsi */
  82. struct scatterlist *tvc_sgl;
  83. struct scatterlist *tvc_prot_sgl;
  84. struct page **tvc_upages;
  85. /* Pointer to response header iovec */
  86. struct iovec *tvc_resp_iov;
  87. /* Pointer to vhost_scsi for our device */
  88. struct vhost_scsi *tvc_vhost;
  89. /* Pointer to vhost_virtqueue for the cmd */
  90. struct vhost_virtqueue *tvc_vq;
  91. /* Pointer to vhost nexus memory */
  92. struct vhost_scsi_nexus *tvc_nexus;
  93. /* The TCM I/O descriptor that is accessed via container_of() */
  94. struct se_cmd tvc_se_cmd;
  95. /* work item used for cmwq dispatch to vhost_scsi_submission_work() */
  96. struct work_struct work;
  97. /* Copy of the incoming SCSI command descriptor block (CDB) */
  98. unsigned char tvc_cdb[VHOST_SCSI_MAX_CDB_SIZE];
  99. /* Sense buffer that will be mapped into outgoing status */
  100. unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
  101. /* Completed commands list, serviced from vhost worker thread */
  102. struct llist_node tvc_completion_list;
  103. /* Used to track inflight cmd */
  104. struct vhost_scsi_inflight *inflight;
  105. };
  106. struct vhost_scsi_nexus {
  107. /* Pointer to TCM session for I_T Nexus */
  108. struct se_session *tvn_se_sess;
  109. };
  110. struct vhost_scsi_tpg {
  111. /* Vhost port target portal group tag for TCM */
  112. u16 tport_tpgt;
  113. /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
  114. int tv_tpg_port_count;
  115. /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
  116. int tv_tpg_vhost_count;
  117. /* Used for enabling T10-PI with legacy devices */
  118. int tv_fabric_prot_type;
  119. /* list for vhost_scsi_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 vhost_scsi_nexus *tpg_nexus;
  125. /* Pointer back to vhost_scsi_tport */
  126. struct vhost_scsi_tport *tport;
  127. /* Returned by vhost_scsi_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 vhost_scsi_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[VHOST_SCSI_NAMELEN];
  139. /* Returned by vhost_scsi_make_tport() */
  140. struct se_wwn tport_wwn;
  141. };
  142. struct vhost_scsi_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. /* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
  154. enum {
  155. VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG) |
  156. (1ULL << VIRTIO_SCSI_F_T10_PI)
  157. };
  158. #define VHOST_SCSI_MAX_TARGET 256
  159. #define VHOST_SCSI_MAX_VQ 128
  160. #define VHOST_SCSI_MAX_EVENT 128
  161. struct vhost_scsi_virtqueue {
  162. struct vhost_virtqueue vq;
  163. /*
  164. * Reference counting for inflight reqs, used for flush operation. At
  165. * each time, one reference tracks new commands submitted, while we
  166. * wait for another one to reach 0.
  167. */
  168. struct vhost_scsi_inflight inflights[2];
  169. /*
  170. * Indicate current inflight in use, protected by vq->mutex.
  171. * Writers must also take dev mutex and flush under it.
  172. */
  173. int inflight_idx;
  174. };
  175. struct vhost_scsi {
  176. /* Protected by vhost_scsi->dev.mutex */
  177. struct vhost_scsi_tpg **vs_tpg;
  178. char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
  179. struct vhost_dev dev;
  180. struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
  181. struct vhost_work vs_completion_work; /* cmd completion work item */
  182. struct llist_head vs_completion_list; /* cmd completion queue */
  183. struct vhost_work vs_event_work; /* evt injection work item */
  184. struct llist_head vs_event_list; /* evt injection queue */
  185. bool vs_events_missed; /* any missed events, protected by vq->mutex */
  186. int vs_events_nr; /* num of pending events, protected by vq->mutex */
  187. };
  188. static struct workqueue_struct *vhost_scsi_workqueue;
  189. /* Global spinlock to protect vhost_scsi TPG list for vhost IOCTL access */
  190. static DEFINE_MUTEX(vhost_scsi_mutex);
  191. static LIST_HEAD(vhost_scsi_list);
  192. static int iov_num_pages(void __user *iov_base, size_t iov_len)
  193. {
  194. return (PAGE_ALIGN((unsigned long)iov_base + iov_len) -
  195. ((unsigned long)iov_base & PAGE_MASK)) >> PAGE_SHIFT;
  196. }
  197. static void vhost_scsi_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 vhost_scsi_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. vhost_scsi_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 vhost_scsi_put_inflight(struct vhost_scsi_inflight *inflight)
  235. {
  236. kref_put(&inflight->kref, vhost_scsi_done_inflight);
  237. }
  238. static int vhost_scsi_check_true(struct se_portal_group *se_tpg)
  239. {
  240. return 1;
  241. }
  242. static int vhost_scsi_check_false(struct se_portal_group *se_tpg)
  243. {
  244. return 0;
  245. }
  246. static char *vhost_scsi_get_fabric_name(void)
  247. {
  248. return "vhost";
  249. }
  250. static char *vhost_scsi_get_fabric_wwn(struct se_portal_group *se_tpg)
  251. {
  252. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  253. struct vhost_scsi_tpg, se_tpg);
  254. struct vhost_scsi_tport *tport = tpg->tport;
  255. return &tport->tport_name[0];
  256. }
  257. static u16 vhost_scsi_get_tpgt(struct se_portal_group *se_tpg)
  258. {
  259. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  260. struct vhost_scsi_tpg, se_tpg);
  261. return tpg->tport_tpgt;
  262. }
  263. static int vhost_scsi_check_prot_fabric_only(struct se_portal_group *se_tpg)
  264. {
  265. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  266. struct vhost_scsi_tpg, se_tpg);
  267. return tpg->tv_fabric_prot_type;
  268. }
  269. static u32 vhost_scsi_tpg_get_inst_index(struct se_portal_group *se_tpg)
  270. {
  271. return 1;
  272. }
  273. static void vhost_scsi_release_cmd(struct se_cmd *se_cmd)
  274. {
  275. struct vhost_scsi_cmd *tv_cmd = container_of(se_cmd,
  276. struct vhost_scsi_cmd, tvc_se_cmd);
  277. struct se_session *se_sess = tv_cmd->tvc_nexus->tvn_se_sess;
  278. int i;
  279. if (tv_cmd->tvc_sgl_count) {
  280. for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
  281. put_page(sg_page(&tv_cmd->tvc_sgl[i]));
  282. }
  283. if (tv_cmd->tvc_prot_sgl_count) {
  284. for (i = 0; i < tv_cmd->tvc_prot_sgl_count; i++)
  285. put_page(sg_page(&tv_cmd->tvc_prot_sgl[i]));
  286. }
  287. vhost_scsi_put_inflight(tv_cmd->inflight);
  288. percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
  289. }
  290. static int vhost_scsi_shutdown_session(struct se_session *se_sess)
  291. {
  292. return 0;
  293. }
  294. static void vhost_scsi_close_session(struct se_session *se_sess)
  295. {
  296. return;
  297. }
  298. static u32 vhost_scsi_sess_get_index(struct se_session *se_sess)
  299. {
  300. return 0;
  301. }
  302. static int vhost_scsi_write_pending(struct se_cmd *se_cmd)
  303. {
  304. /* Go ahead and process the write immediately */
  305. target_execute_cmd(se_cmd);
  306. return 0;
  307. }
  308. static int vhost_scsi_write_pending_status(struct se_cmd *se_cmd)
  309. {
  310. return 0;
  311. }
  312. static void vhost_scsi_set_default_node_attrs(struct se_node_acl *nacl)
  313. {
  314. return;
  315. }
  316. static int vhost_scsi_get_cmd_state(struct se_cmd *se_cmd)
  317. {
  318. return 0;
  319. }
  320. static void vhost_scsi_complete_cmd(struct vhost_scsi_cmd *cmd)
  321. {
  322. struct vhost_scsi *vs = cmd->tvc_vhost;
  323. llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
  324. vhost_work_queue(&vs->dev, &vs->vs_completion_work);
  325. }
  326. static int vhost_scsi_queue_data_in(struct se_cmd *se_cmd)
  327. {
  328. struct vhost_scsi_cmd *cmd = container_of(se_cmd,
  329. struct vhost_scsi_cmd, tvc_se_cmd);
  330. vhost_scsi_complete_cmd(cmd);
  331. return 0;
  332. }
  333. static int vhost_scsi_queue_status(struct se_cmd *se_cmd)
  334. {
  335. struct vhost_scsi_cmd *cmd = container_of(se_cmd,
  336. struct vhost_scsi_cmd, tvc_se_cmd);
  337. vhost_scsi_complete_cmd(cmd);
  338. return 0;
  339. }
  340. static void vhost_scsi_queue_tm_rsp(struct se_cmd *se_cmd)
  341. {
  342. return;
  343. }
  344. static void vhost_scsi_aborted_task(struct se_cmd *se_cmd)
  345. {
  346. return;
  347. }
  348. static void vhost_scsi_free_evt(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
  349. {
  350. vs->vs_events_nr--;
  351. kfree(evt);
  352. }
  353. static struct vhost_scsi_evt *
  354. vhost_scsi_allocate_evt(struct vhost_scsi *vs,
  355. u32 event, u32 reason)
  356. {
  357. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  358. struct vhost_scsi_evt *evt;
  359. if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
  360. vs->vs_events_missed = true;
  361. return NULL;
  362. }
  363. evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  364. if (!evt) {
  365. vq_err(vq, "Failed to allocate vhost_scsi_evt\n");
  366. vs->vs_events_missed = true;
  367. return NULL;
  368. }
  369. evt->event.event = cpu_to_vhost32(vq, event);
  370. evt->event.reason = cpu_to_vhost32(vq, reason);
  371. vs->vs_events_nr++;
  372. return evt;
  373. }
  374. static void vhost_scsi_free_cmd(struct vhost_scsi_cmd *cmd)
  375. {
  376. struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
  377. /* TODO locking against target/backend threads? */
  378. transport_generic_free_cmd(se_cmd, 0);
  379. }
  380. static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
  381. {
  382. return target_put_sess_cmd(se_cmd);
  383. }
  384. static void
  385. vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
  386. {
  387. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  388. struct virtio_scsi_event *event = &evt->event;
  389. struct virtio_scsi_event __user *eventp;
  390. unsigned out, in;
  391. int head, ret;
  392. if (!vq->private_data) {
  393. vs->vs_events_missed = true;
  394. return;
  395. }
  396. again:
  397. vhost_disable_notify(&vs->dev, vq);
  398. head = vhost_get_vq_desc(vq, vq->iov,
  399. ARRAY_SIZE(vq->iov), &out, &in,
  400. NULL, NULL);
  401. if (head < 0) {
  402. vs->vs_events_missed = true;
  403. return;
  404. }
  405. if (head == vq->num) {
  406. if (vhost_enable_notify(&vs->dev, vq))
  407. goto again;
  408. vs->vs_events_missed = true;
  409. return;
  410. }
  411. if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
  412. vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
  413. vq->iov[out].iov_len);
  414. vs->vs_events_missed = true;
  415. return;
  416. }
  417. if (vs->vs_events_missed) {
  418. event->event |= cpu_to_vhost32(vq, VIRTIO_SCSI_T_EVENTS_MISSED);
  419. vs->vs_events_missed = false;
  420. }
  421. eventp = vq->iov[out].iov_base;
  422. ret = __copy_to_user(eventp, event, sizeof(*event));
  423. if (!ret)
  424. vhost_add_used_and_signal(&vs->dev, vq, head, 0);
  425. else
  426. vq_err(vq, "Faulted on vhost_scsi_send_event\n");
  427. }
  428. static void vhost_scsi_evt_work(struct vhost_work *work)
  429. {
  430. struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
  431. vs_event_work);
  432. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  433. struct vhost_scsi_evt *evt;
  434. struct llist_node *llnode;
  435. mutex_lock(&vq->mutex);
  436. llnode = llist_del_all(&vs->vs_event_list);
  437. while (llnode) {
  438. evt = llist_entry(llnode, struct vhost_scsi_evt, list);
  439. llnode = llist_next(llnode);
  440. vhost_scsi_do_evt_work(vs, evt);
  441. vhost_scsi_free_evt(vs, evt);
  442. }
  443. mutex_unlock(&vq->mutex);
  444. }
  445. /* Fill in status and signal that we are done processing this command
  446. *
  447. * This is scheduled in the vhost work queue so we are called with the owner
  448. * process mm and can access the vring.
  449. */
  450. static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
  451. {
  452. struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
  453. vs_completion_work);
  454. DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
  455. struct virtio_scsi_cmd_resp v_rsp;
  456. struct vhost_scsi_cmd *cmd;
  457. struct llist_node *llnode;
  458. struct se_cmd *se_cmd;
  459. struct iov_iter iov_iter;
  460. int ret, vq;
  461. bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
  462. llnode = llist_del_all(&vs->vs_completion_list);
  463. while (llnode) {
  464. cmd = llist_entry(llnode, struct vhost_scsi_cmd,
  465. tvc_completion_list);
  466. llnode = llist_next(llnode);
  467. se_cmd = &cmd->tvc_se_cmd;
  468. pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
  469. cmd, se_cmd->residual_count, se_cmd->scsi_status);
  470. memset(&v_rsp, 0, sizeof(v_rsp));
  471. v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq, se_cmd->residual_count);
  472. /* TODO is status_qualifier field needed? */
  473. v_rsp.status = se_cmd->scsi_status;
  474. v_rsp.sense_len = cpu_to_vhost32(cmd->tvc_vq,
  475. se_cmd->scsi_sense_length);
  476. memcpy(v_rsp.sense, cmd->tvc_sense_buf,
  477. se_cmd->scsi_sense_length);
  478. iov_iter_init(&iov_iter, READ, cmd->tvc_resp_iov,
  479. cmd->tvc_in_iovs, sizeof(v_rsp));
  480. ret = copy_to_iter(&v_rsp, sizeof(v_rsp), &iov_iter);
  481. if (likely(ret == sizeof(v_rsp))) {
  482. struct vhost_scsi_virtqueue *q;
  483. vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
  484. q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
  485. vq = q - vs->vqs;
  486. __set_bit(vq, signal);
  487. } else
  488. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  489. vhost_scsi_free_cmd(cmd);
  490. }
  491. vq = -1;
  492. while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
  493. < VHOST_SCSI_MAX_VQ)
  494. vhost_signal(&vs->dev, &vs->vqs[vq].vq);
  495. }
  496. static struct vhost_scsi_cmd *
  497. vhost_scsi_get_tag(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg,
  498. unsigned char *cdb, u64 scsi_tag, u16 lun, u8 task_attr,
  499. u32 exp_data_len, int data_direction)
  500. {
  501. struct vhost_scsi_cmd *cmd;
  502. struct vhost_scsi_nexus *tv_nexus;
  503. struct se_session *se_sess;
  504. struct scatterlist *sg, *prot_sg;
  505. struct page **pages;
  506. int tag;
  507. tv_nexus = tpg->tpg_nexus;
  508. if (!tv_nexus) {
  509. pr_err("Unable to locate active struct vhost_scsi_nexus\n");
  510. return ERR_PTR(-EIO);
  511. }
  512. se_sess = tv_nexus->tvn_se_sess;
  513. tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
  514. if (tag < 0) {
  515. pr_err("Unable to obtain tag for vhost_scsi_cmd\n");
  516. return ERR_PTR(-ENOMEM);
  517. }
  518. cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[tag];
  519. sg = cmd->tvc_sgl;
  520. prot_sg = cmd->tvc_prot_sgl;
  521. pages = cmd->tvc_upages;
  522. memset(cmd, 0, sizeof(struct vhost_scsi_cmd));
  523. cmd->tvc_sgl = sg;
  524. cmd->tvc_prot_sgl = prot_sg;
  525. cmd->tvc_upages = pages;
  526. cmd->tvc_se_cmd.map_tag = tag;
  527. cmd->tvc_tag = scsi_tag;
  528. cmd->tvc_lun = lun;
  529. cmd->tvc_task_attr = task_attr;
  530. cmd->tvc_exp_data_len = exp_data_len;
  531. cmd->tvc_data_direction = data_direction;
  532. cmd->tvc_nexus = tv_nexus;
  533. cmd->inflight = vhost_scsi_get_inflight(vq);
  534. memcpy(cmd->tvc_cdb, cdb, VHOST_SCSI_MAX_CDB_SIZE);
  535. return cmd;
  536. }
  537. /*
  538. * Map a user memory range into a scatterlist
  539. *
  540. * Returns the number of scatterlist entries used or -errno on error.
  541. */
  542. static int
  543. vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd,
  544. void __user *ptr,
  545. size_t len,
  546. struct scatterlist *sgl,
  547. bool write)
  548. {
  549. unsigned int npages = 0, offset, nbytes;
  550. unsigned int pages_nr = iov_num_pages(ptr, len);
  551. struct scatterlist *sg = sgl;
  552. struct page **pages = cmd->tvc_upages;
  553. int ret, i;
  554. if (pages_nr > VHOST_SCSI_PREALLOC_UPAGES) {
  555. pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
  556. " preallocated VHOST_SCSI_PREALLOC_UPAGES: %u\n",
  557. pages_nr, VHOST_SCSI_PREALLOC_UPAGES);
  558. return -ENOBUFS;
  559. }
  560. ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
  561. /* No pages were pinned */
  562. if (ret < 0)
  563. goto out;
  564. /* Less pages pinned than wanted */
  565. if (ret != pages_nr) {
  566. for (i = 0; i < ret; i++)
  567. put_page(pages[i]);
  568. ret = -EFAULT;
  569. goto out;
  570. }
  571. while (len > 0) {
  572. offset = (uintptr_t)ptr & ~PAGE_MASK;
  573. nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
  574. sg_set_page(sg, pages[npages], nbytes, offset);
  575. ptr += nbytes;
  576. len -= nbytes;
  577. sg++;
  578. npages++;
  579. }
  580. out:
  581. return ret;
  582. }
  583. static int
  584. vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls)
  585. {
  586. int sgl_count = 0;
  587. if (!iter || !iter->iov) {
  588. pr_err("%s: iter->iov is NULL, but expected bytes: %zu"
  589. " present\n", __func__, bytes);
  590. return -EINVAL;
  591. }
  592. sgl_count = iov_iter_npages(iter, 0xffff);
  593. if (sgl_count > max_sgls) {
  594. pr_err("%s: requested sgl_count: %d exceeds pre-allocated"
  595. " max_sgls: %d\n", __func__, sgl_count, max_sgls);
  596. return -EINVAL;
  597. }
  598. return sgl_count;
  599. }
  600. static int
  601. vhost_scsi_iov_to_sgl(struct vhost_scsi_cmd *cmd, bool write,
  602. struct iov_iter *iter,
  603. struct scatterlist *sg, int sg_count)
  604. {
  605. size_t off = iter->iov_offset;
  606. int i, ret;
  607. for (i = 0; i < iter->nr_segs; i++) {
  608. void __user *base = iter->iov[i].iov_base + off;
  609. size_t len = iter->iov[i].iov_len - off;
  610. ret = vhost_scsi_map_to_sgl(cmd, base, len, sg, write);
  611. if (ret < 0) {
  612. for (i = 0; i < sg_count; i++) {
  613. struct page *page = sg_page(&sg[i]);
  614. if (page)
  615. put_page(page);
  616. }
  617. return ret;
  618. }
  619. sg += ret;
  620. off = 0;
  621. }
  622. return 0;
  623. }
  624. static int
  625. vhost_scsi_mapal(struct vhost_scsi_cmd *cmd,
  626. size_t prot_bytes, struct iov_iter *prot_iter,
  627. size_t data_bytes, struct iov_iter *data_iter)
  628. {
  629. int sgl_count, ret;
  630. bool write = (cmd->tvc_data_direction == DMA_FROM_DEVICE);
  631. if (prot_bytes) {
  632. sgl_count = vhost_scsi_calc_sgls(prot_iter, prot_bytes,
  633. VHOST_SCSI_PREALLOC_PROT_SGLS);
  634. if (sgl_count < 0)
  635. return sgl_count;
  636. sg_init_table(cmd->tvc_prot_sgl, sgl_count);
  637. cmd->tvc_prot_sgl_count = sgl_count;
  638. pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__,
  639. cmd->tvc_prot_sgl, cmd->tvc_prot_sgl_count);
  640. ret = vhost_scsi_iov_to_sgl(cmd, write, prot_iter,
  641. cmd->tvc_prot_sgl,
  642. cmd->tvc_prot_sgl_count);
  643. if (ret < 0) {
  644. cmd->tvc_prot_sgl_count = 0;
  645. return ret;
  646. }
  647. }
  648. sgl_count = vhost_scsi_calc_sgls(data_iter, data_bytes,
  649. VHOST_SCSI_PREALLOC_SGLS);
  650. if (sgl_count < 0)
  651. return sgl_count;
  652. sg_init_table(cmd->tvc_sgl, sgl_count);
  653. cmd->tvc_sgl_count = sgl_count;
  654. pr_debug("%s data_sg %p data_sgl_count %u\n", __func__,
  655. cmd->tvc_sgl, cmd->tvc_sgl_count);
  656. ret = vhost_scsi_iov_to_sgl(cmd, write, data_iter,
  657. cmd->tvc_sgl, cmd->tvc_sgl_count);
  658. if (ret < 0) {
  659. cmd->tvc_sgl_count = 0;
  660. return ret;
  661. }
  662. return 0;
  663. }
  664. static int vhost_scsi_to_tcm_attr(int attr)
  665. {
  666. switch (attr) {
  667. case VIRTIO_SCSI_S_SIMPLE:
  668. return TCM_SIMPLE_TAG;
  669. case VIRTIO_SCSI_S_ORDERED:
  670. return TCM_ORDERED_TAG;
  671. case VIRTIO_SCSI_S_HEAD:
  672. return TCM_HEAD_TAG;
  673. case VIRTIO_SCSI_S_ACA:
  674. return TCM_ACA_TAG;
  675. default:
  676. break;
  677. }
  678. return TCM_SIMPLE_TAG;
  679. }
  680. static void vhost_scsi_submission_work(struct work_struct *work)
  681. {
  682. struct vhost_scsi_cmd *cmd =
  683. container_of(work, struct vhost_scsi_cmd, work);
  684. struct vhost_scsi_nexus *tv_nexus;
  685. struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
  686. struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
  687. int rc;
  688. /* FIXME: BIDI operation */
  689. if (cmd->tvc_sgl_count) {
  690. sg_ptr = cmd->tvc_sgl;
  691. if (cmd->tvc_prot_sgl_count)
  692. sg_prot_ptr = cmd->tvc_prot_sgl;
  693. else
  694. se_cmd->prot_pto = true;
  695. } else {
  696. sg_ptr = NULL;
  697. }
  698. tv_nexus = cmd->tvc_nexus;
  699. se_cmd->tag = 0;
  700. rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
  701. cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
  702. cmd->tvc_lun, cmd->tvc_exp_data_len,
  703. vhost_scsi_to_tcm_attr(cmd->tvc_task_attr),
  704. cmd->tvc_data_direction, TARGET_SCF_ACK_KREF,
  705. sg_ptr, cmd->tvc_sgl_count, NULL, 0, sg_prot_ptr,
  706. cmd->tvc_prot_sgl_count);
  707. if (rc < 0) {
  708. transport_send_check_condition_and_sense(se_cmd,
  709. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  710. transport_generic_free_cmd(se_cmd, 0);
  711. }
  712. }
  713. static void
  714. vhost_scsi_send_bad_target(struct vhost_scsi *vs,
  715. struct vhost_virtqueue *vq,
  716. int head, unsigned out)
  717. {
  718. struct virtio_scsi_cmd_resp __user *resp;
  719. struct virtio_scsi_cmd_resp rsp;
  720. int ret;
  721. memset(&rsp, 0, sizeof(rsp));
  722. rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
  723. resp = vq->iov[out].iov_base;
  724. ret = __copy_to_user(resp, &rsp, sizeof(rsp));
  725. if (!ret)
  726. vhost_add_used_and_signal(&vs->dev, vq, head, 0);
  727. else
  728. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  729. }
  730. static void
  731. vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
  732. {
  733. struct vhost_scsi_tpg **vs_tpg, *tpg;
  734. struct virtio_scsi_cmd_req v_req;
  735. struct virtio_scsi_cmd_req_pi v_req_pi;
  736. struct vhost_scsi_cmd *cmd;
  737. struct iov_iter out_iter, in_iter, prot_iter, data_iter;
  738. u64 tag;
  739. u32 exp_data_len, data_direction;
  740. unsigned out, in;
  741. int head, ret, prot_bytes;
  742. size_t req_size, rsp_size = sizeof(struct virtio_scsi_cmd_resp);
  743. size_t out_size, in_size;
  744. u16 lun;
  745. u8 *target, *lunp, task_attr;
  746. bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI);
  747. void *req, *cdb;
  748. mutex_lock(&vq->mutex);
  749. /*
  750. * We can handle the vq only after the endpoint is setup by calling the
  751. * VHOST_SCSI_SET_ENDPOINT ioctl.
  752. */
  753. vs_tpg = vq->private_data;
  754. if (!vs_tpg)
  755. goto out;
  756. vhost_disable_notify(&vs->dev, vq);
  757. for (;;) {
  758. head = vhost_get_vq_desc(vq, vq->iov,
  759. ARRAY_SIZE(vq->iov), &out, &in,
  760. NULL, NULL);
  761. pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
  762. head, out, in);
  763. /* On error, stop handling until the next kick. */
  764. if (unlikely(head < 0))
  765. break;
  766. /* Nothing new? Wait for eventfd to tell us they refilled. */
  767. if (head == vq->num) {
  768. if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
  769. vhost_disable_notify(&vs->dev, vq);
  770. continue;
  771. }
  772. break;
  773. }
  774. /*
  775. * Check for a sane response buffer so we can report early
  776. * errors back to the guest.
  777. */
  778. if (unlikely(vq->iov[out].iov_len < rsp_size)) {
  779. vq_err(vq, "Expecting at least virtio_scsi_cmd_resp"
  780. " size, got %zu bytes\n", vq->iov[out].iov_len);
  781. break;
  782. }
  783. /*
  784. * Setup pointers and values based upon different virtio-scsi
  785. * request header if T10_PI is enabled in KVM guest.
  786. */
  787. if (t10_pi) {
  788. req = &v_req_pi;
  789. req_size = sizeof(v_req_pi);
  790. lunp = &v_req_pi.lun[0];
  791. target = &v_req_pi.lun[1];
  792. } else {
  793. req = &v_req;
  794. req_size = sizeof(v_req);
  795. lunp = &v_req.lun[0];
  796. target = &v_req.lun[1];
  797. }
  798. /*
  799. * FIXME: Not correct for BIDI operation
  800. */
  801. out_size = iov_length(vq->iov, out);
  802. in_size = iov_length(&vq->iov[out], in);
  803. /*
  804. * Copy over the virtio-scsi request header, which for a
  805. * ANY_LAYOUT enabled guest may span multiple iovecs, or a
  806. * single iovec may contain both the header + outgoing
  807. * WRITE payloads.
  808. *
  809. * copy_from_iter() will advance out_iter, so that it will
  810. * point at the start of the outgoing WRITE payload, if
  811. * DMA_TO_DEVICE is set.
  812. */
  813. iov_iter_init(&out_iter, WRITE, vq->iov, out, out_size);
  814. ret = copy_from_iter(req, req_size, &out_iter);
  815. if (unlikely(ret != req_size)) {
  816. vq_err(vq, "Faulted on copy_from_iter\n");
  817. vhost_scsi_send_bad_target(vs, vq, head, out);
  818. continue;
  819. }
  820. /* virtio-scsi spec requires byte 0 of the lun to be 1 */
  821. if (unlikely(*lunp != 1)) {
  822. vq_err(vq, "Illegal virtio-scsi lun: %u\n", *lunp);
  823. vhost_scsi_send_bad_target(vs, vq, head, out);
  824. continue;
  825. }
  826. tpg = ACCESS_ONCE(vs_tpg[*target]);
  827. if (unlikely(!tpg)) {
  828. /* Target does not exist, fail the request */
  829. vhost_scsi_send_bad_target(vs, vq, head, out);
  830. continue;
  831. }
  832. /*
  833. * Determine data_direction by calculating the total outgoing
  834. * iovec sizes + incoming iovec sizes vs. virtio-scsi request +
  835. * response headers respectively.
  836. *
  837. * For DMA_TO_DEVICE this is out_iter, which is already pointing
  838. * to the right place.
  839. *
  840. * For DMA_FROM_DEVICE, the iovec will be just past the end
  841. * of the virtio-scsi response header in either the same
  842. * or immediately following iovec.
  843. *
  844. * Any associated T10_PI bytes for the outgoing / incoming
  845. * payloads are included in calculation of exp_data_len here.
  846. */
  847. prot_bytes = 0;
  848. if (out_size > req_size) {
  849. data_direction = DMA_TO_DEVICE;
  850. exp_data_len = out_size - req_size;
  851. data_iter = out_iter;
  852. } else if (in_size > rsp_size) {
  853. data_direction = DMA_FROM_DEVICE;
  854. exp_data_len = in_size - rsp_size;
  855. iov_iter_init(&in_iter, READ, &vq->iov[out], in,
  856. rsp_size + exp_data_len);
  857. iov_iter_advance(&in_iter, rsp_size);
  858. data_iter = in_iter;
  859. } else {
  860. data_direction = DMA_NONE;
  861. exp_data_len = 0;
  862. }
  863. /*
  864. * If T10_PI header + payload is present, setup prot_iter values
  865. * and recalculate data_iter for vhost_scsi_mapal() mapping to
  866. * host scatterlists via get_user_pages_fast().
  867. */
  868. if (t10_pi) {
  869. if (v_req_pi.pi_bytesout) {
  870. if (data_direction != DMA_TO_DEVICE) {
  871. vq_err(vq, "Received non zero pi_bytesout,"
  872. " but wrong data_direction\n");
  873. vhost_scsi_send_bad_target(vs, vq, head, out);
  874. continue;
  875. }
  876. prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout);
  877. } else if (v_req_pi.pi_bytesin) {
  878. if (data_direction != DMA_FROM_DEVICE) {
  879. vq_err(vq, "Received non zero pi_bytesin,"
  880. " but wrong data_direction\n");
  881. vhost_scsi_send_bad_target(vs, vq, head, out);
  882. continue;
  883. }
  884. prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin);
  885. }
  886. /*
  887. * Set prot_iter to data_iter, and advance past any
  888. * preceeding prot_bytes that may be present.
  889. *
  890. * Also fix up the exp_data_len to reflect only the
  891. * actual data payload length.
  892. */
  893. if (prot_bytes) {
  894. exp_data_len -= prot_bytes;
  895. prot_iter = data_iter;
  896. iov_iter_advance(&data_iter, prot_bytes);
  897. }
  898. tag = vhost64_to_cpu(vq, v_req_pi.tag);
  899. task_attr = v_req_pi.task_attr;
  900. cdb = &v_req_pi.cdb[0];
  901. lun = ((v_req_pi.lun[2] << 8) | v_req_pi.lun[3]) & 0x3FFF;
  902. } else {
  903. tag = vhost64_to_cpu(vq, v_req.tag);
  904. task_attr = v_req.task_attr;
  905. cdb = &v_req.cdb[0];
  906. lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
  907. }
  908. /*
  909. * Check that the received CDB size does not exceeded our
  910. * hardcoded max for vhost-scsi, then get a pre-allocated
  911. * cmd descriptor for the new virtio-scsi tag.
  912. *
  913. * TODO what if cdb was too small for varlen cdb header?
  914. */
  915. if (unlikely(scsi_command_size(cdb) > VHOST_SCSI_MAX_CDB_SIZE)) {
  916. vq_err(vq, "Received SCSI CDB with command_size: %d that"
  917. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  918. scsi_command_size(cdb), VHOST_SCSI_MAX_CDB_SIZE);
  919. vhost_scsi_send_bad_target(vs, vq, head, out);
  920. continue;
  921. }
  922. cmd = vhost_scsi_get_tag(vq, tpg, cdb, tag, lun, task_attr,
  923. exp_data_len + prot_bytes,
  924. data_direction);
  925. if (IS_ERR(cmd)) {
  926. vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
  927. PTR_ERR(cmd));
  928. vhost_scsi_send_bad_target(vs, vq, head, out);
  929. continue;
  930. }
  931. cmd->tvc_vhost = vs;
  932. cmd->tvc_vq = vq;
  933. cmd->tvc_resp_iov = &vq->iov[out];
  934. cmd->tvc_in_iovs = in;
  935. pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
  936. cmd->tvc_cdb[0], cmd->tvc_lun);
  937. pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
  938. " %d\n", cmd, exp_data_len, prot_bytes, data_direction);
  939. if (data_direction != DMA_NONE) {
  940. ret = vhost_scsi_mapal(cmd,
  941. prot_bytes, &prot_iter,
  942. exp_data_len, &data_iter);
  943. if (unlikely(ret)) {
  944. vq_err(vq, "Failed to map iov to sgl\n");
  945. vhost_scsi_release_cmd(&cmd->tvc_se_cmd);
  946. vhost_scsi_send_bad_target(vs, vq, head, out);
  947. continue;
  948. }
  949. }
  950. /*
  951. * Save the descriptor from vhost_get_vq_desc() to be used to
  952. * complete the virtio-scsi request in TCM callback context via
  953. * vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
  954. */
  955. cmd->tvc_vq_desc = head;
  956. /*
  957. * Dispatch cmd descriptor for cmwq execution in process
  958. * context provided by vhost_scsi_workqueue. This also ensures
  959. * cmd is executed on the same kworker CPU as this vhost
  960. * thread to gain positive L2 cache locality effects.
  961. */
  962. INIT_WORK(&cmd->work, vhost_scsi_submission_work);
  963. queue_work(vhost_scsi_workqueue, &cmd->work);
  964. }
  965. out:
  966. mutex_unlock(&vq->mutex);
  967. }
  968. static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
  969. {
  970. pr_debug("%s: The handling func for control queue.\n", __func__);
  971. }
  972. static void
  973. vhost_scsi_send_evt(struct vhost_scsi *vs,
  974. struct vhost_scsi_tpg *tpg,
  975. struct se_lun *lun,
  976. u32 event,
  977. u32 reason)
  978. {
  979. struct vhost_scsi_evt *evt;
  980. evt = vhost_scsi_allocate_evt(vs, event, reason);
  981. if (!evt)
  982. return;
  983. if (tpg && lun) {
  984. /* TODO: share lun setup code with virtio-scsi.ko */
  985. /*
  986. * Note: evt->event is zeroed when we allocate it and
  987. * lun[4-7] need to be zero according to virtio-scsi spec.
  988. */
  989. evt->event.lun[0] = 0x01;
  990. evt->event.lun[1] = tpg->tport_tpgt;
  991. if (lun->unpacked_lun >= 256)
  992. evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
  993. evt->event.lun[3] = lun->unpacked_lun & 0xFF;
  994. }
  995. llist_add(&evt->list, &vs->vs_event_list);
  996. vhost_work_queue(&vs->dev, &vs->vs_event_work);
  997. }
  998. static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
  999. {
  1000. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1001. poll.work);
  1002. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  1003. mutex_lock(&vq->mutex);
  1004. if (!vq->private_data)
  1005. goto out;
  1006. if (vs->vs_events_missed)
  1007. vhost_scsi_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
  1008. out:
  1009. mutex_unlock(&vq->mutex);
  1010. }
  1011. static void vhost_scsi_handle_kick(struct vhost_work *work)
  1012. {
  1013. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1014. poll.work);
  1015. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  1016. vhost_scsi_handle_vq(vs, vq);
  1017. }
  1018. static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
  1019. {
  1020. vhost_poll_flush(&vs->vqs[index].vq.poll);
  1021. }
  1022. /* Callers must hold dev mutex */
  1023. static void vhost_scsi_flush(struct vhost_scsi *vs)
  1024. {
  1025. struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
  1026. int i;
  1027. /* Init new inflight and remember the old inflight */
  1028. vhost_scsi_init_inflight(vs, old_inflight);
  1029. /*
  1030. * The inflight->kref was initialized to 1. We decrement it here to
  1031. * indicate the start of the flush operation so that it will reach 0
  1032. * when all the reqs are finished.
  1033. */
  1034. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1035. kref_put(&old_inflight[i]->kref, vhost_scsi_done_inflight);
  1036. /* Flush both the vhost poll and vhost work */
  1037. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1038. vhost_scsi_flush_vq(vs, i);
  1039. vhost_work_flush(&vs->dev, &vs->vs_completion_work);
  1040. vhost_work_flush(&vs->dev, &vs->vs_event_work);
  1041. /* Wait for all reqs issued before the flush to be finished */
  1042. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1043. wait_for_completion(&old_inflight[i]->comp);
  1044. }
  1045. /*
  1046. * Called from vhost_scsi_ioctl() context to walk the list of available
  1047. * vhost_scsi_tpg with an active struct vhost_scsi_nexus
  1048. *
  1049. * The lock nesting rule is:
  1050. * vhost_scsi_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
  1051. */
  1052. static int
  1053. vhost_scsi_set_endpoint(struct vhost_scsi *vs,
  1054. struct vhost_scsi_target *t)
  1055. {
  1056. struct se_portal_group *se_tpg;
  1057. struct vhost_scsi_tport *tv_tport;
  1058. struct vhost_scsi_tpg *tpg;
  1059. struct vhost_scsi_tpg **vs_tpg;
  1060. struct vhost_virtqueue *vq;
  1061. int index, ret, i, len;
  1062. bool match = false;
  1063. mutex_lock(&vhost_scsi_mutex);
  1064. mutex_lock(&vs->dev.mutex);
  1065. /* Verify that ring has been setup correctly. */
  1066. for (index = 0; index < vs->dev.nvqs; ++index) {
  1067. /* Verify that ring has been setup correctly. */
  1068. if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
  1069. ret = -EFAULT;
  1070. goto out;
  1071. }
  1072. }
  1073. len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
  1074. vs_tpg = kzalloc(len, GFP_KERNEL);
  1075. if (!vs_tpg) {
  1076. ret = -ENOMEM;
  1077. goto out;
  1078. }
  1079. if (vs->vs_tpg)
  1080. memcpy(vs_tpg, vs->vs_tpg, len);
  1081. list_for_each_entry(tpg, &vhost_scsi_list, tv_tpg_list) {
  1082. mutex_lock(&tpg->tv_tpg_mutex);
  1083. if (!tpg->tpg_nexus) {
  1084. mutex_unlock(&tpg->tv_tpg_mutex);
  1085. continue;
  1086. }
  1087. if (tpg->tv_tpg_vhost_count != 0) {
  1088. mutex_unlock(&tpg->tv_tpg_mutex);
  1089. continue;
  1090. }
  1091. tv_tport = tpg->tport;
  1092. if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  1093. if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
  1094. kfree(vs_tpg);
  1095. mutex_unlock(&tpg->tv_tpg_mutex);
  1096. ret = -EEXIST;
  1097. goto out;
  1098. }
  1099. /*
  1100. * In order to ensure individual vhost-scsi configfs
  1101. * groups cannot be removed while in use by vhost ioctl,
  1102. * go ahead and take an explicit se_tpg->tpg_group.cg_item
  1103. * dependency now.
  1104. */
  1105. se_tpg = &tpg->se_tpg;
  1106. ret = target_depend_item(&se_tpg->tpg_group.cg_item);
  1107. if (ret) {
  1108. pr_warn("configfs_depend_item() failed: %d\n", ret);
  1109. kfree(vs_tpg);
  1110. mutex_unlock(&tpg->tv_tpg_mutex);
  1111. goto out;
  1112. }
  1113. tpg->tv_tpg_vhost_count++;
  1114. tpg->vhost_scsi = vs;
  1115. vs_tpg[tpg->tport_tpgt] = tpg;
  1116. smp_mb__after_atomic();
  1117. match = true;
  1118. }
  1119. mutex_unlock(&tpg->tv_tpg_mutex);
  1120. }
  1121. if (match) {
  1122. memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
  1123. sizeof(vs->vs_vhost_wwpn));
  1124. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1125. vq = &vs->vqs[i].vq;
  1126. mutex_lock(&vq->mutex);
  1127. vq->private_data = vs_tpg;
  1128. vhost_vq_init_access(vq);
  1129. mutex_unlock(&vq->mutex);
  1130. }
  1131. ret = 0;
  1132. } else {
  1133. ret = -EEXIST;
  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 = vs_tpg;
  1142. out:
  1143. mutex_unlock(&vs->dev.mutex);
  1144. mutex_unlock(&vhost_scsi_mutex);
  1145. return ret;
  1146. }
  1147. static int
  1148. vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
  1149. struct vhost_scsi_target *t)
  1150. {
  1151. struct se_portal_group *se_tpg;
  1152. struct vhost_scsi_tport *tv_tport;
  1153. struct vhost_scsi_tpg *tpg;
  1154. struct vhost_virtqueue *vq;
  1155. bool match = false;
  1156. int index, ret, i;
  1157. u8 target;
  1158. mutex_lock(&vhost_scsi_mutex);
  1159. mutex_lock(&vs->dev.mutex);
  1160. /* Verify that ring has been setup correctly. */
  1161. for (index = 0; index < vs->dev.nvqs; ++index) {
  1162. if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
  1163. ret = -EFAULT;
  1164. goto err_dev;
  1165. }
  1166. }
  1167. if (!vs->vs_tpg) {
  1168. ret = 0;
  1169. goto err_dev;
  1170. }
  1171. for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
  1172. target = i;
  1173. tpg = vs->vs_tpg[target];
  1174. if (!tpg)
  1175. continue;
  1176. mutex_lock(&tpg->tv_tpg_mutex);
  1177. tv_tport = tpg->tport;
  1178. if (!tv_tport) {
  1179. ret = -ENODEV;
  1180. goto err_tpg;
  1181. }
  1182. if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  1183. pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
  1184. " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
  1185. tv_tport->tport_name, tpg->tport_tpgt,
  1186. t->vhost_wwpn, t->vhost_tpgt);
  1187. ret = -EINVAL;
  1188. goto err_tpg;
  1189. }
  1190. tpg->tv_tpg_vhost_count--;
  1191. tpg->vhost_scsi = NULL;
  1192. vs->vs_tpg[target] = NULL;
  1193. match = true;
  1194. mutex_unlock(&tpg->tv_tpg_mutex);
  1195. /*
  1196. * Release se_tpg->tpg_group.cg_item configfs dependency now
  1197. * to allow vhost-scsi WWPN se_tpg->tpg_group shutdown to occur.
  1198. */
  1199. se_tpg = &tpg->se_tpg;
  1200. target_undepend_item(&se_tpg->tpg_group.cg_item);
  1201. }
  1202. if (match) {
  1203. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1204. vq = &vs->vqs[i].vq;
  1205. mutex_lock(&vq->mutex);
  1206. vq->private_data = NULL;
  1207. mutex_unlock(&vq->mutex);
  1208. }
  1209. }
  1210. /*
  1211. * Act as synchronize_rcu to make sure access to
  1212. * old vs->vs_tpg is finished.
  1213. */
  1214. vhost_scsi_flush(vs);
  1215. kfree(vs->vs_tpg);
  1216. vs->vs_tpg = NULL;
  1217. WARN_ON(vs->vs_events_nr);
  1218. mutex_unlock(&vs->dev.mutex);
  1219. mutex_unlock(&vhost_scsi_mutex);
  1220. return 0;
  1221. err_tpg:
  1222. mutex_unlock(&tpg->tv_tpg_mutex);
  1223. err_dev:
  1224. mutex_unlock(&vs->dev.mutex);
  1225. mutex_unlock(&vhost_scsi_mutex);
  1226. return ret;
  1227. }
  1228. static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
  1229. {
  1230. struct vhost_virtqueue *vq;
  1231. int i;
  1232. if (features & ~VHOST_SCSI_FEATURES)
  1233. return -EOPNOTSUPP;
  1234. mutex_lock(&vs->dev.mutex);
  1235. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  1236. !vhost_log_access_ok(&vs->dev)) {
  1237. mutex_unlock(&vs->dev.mutex);
  1238. return -EFAULT;
  1239. }
  1240. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1241. vq = &vs->vqs[i].vq;
  1242. mutex_lock(&vq->mutex);
  1243. vq->acked_features = features;
  1244. mutex_unlock(&vq->mutex);
  1245. }
  1246. mutex_unlock(&vs->dev.mutex);
  1247. return 0;
  1248. }
  1249. static int vhost_scsi_open(struct inode *inode, struct file *f)
  1250. {
  1251. struct vhost_scsi *vs;
  1252. struct vhost_virtqueue **vqs;
  1253. int r = -ENOMEM, i;
  1254. vs = kzalloc(sizeof(*vs), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  1255. if (!vs) {
  1256. vs = vzalloc(sizeof(*vs));
  1257. if (!vs)
  1258. goto err_vs;
  1259. }
  1260. vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
  1261. if (!vqs)
  1262. goto err_vqs;
  1263. vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
  1264. vhost_work_init(&vs->vs_event_work, vhost_scsi_evt_work);
  1265. vs->vs_events_nr = 0;
  1266. vs->vs_events_missed = false;
  1267. vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
  1268. vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1269. vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
  1270. vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
  1271. for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
  1272. vqs[i] = &vs->vqs[i].vq;
  1273. vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
  1274. }
  1275. vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
  1276. vhost_scsi_init_inflight(vs, NULL);
  1277. f->private_data = vs;
  1278. return 0;
  1279. err_vqs:
  1280. kvfree(vs);
  1281. err_vs:
  1282. return r;
  1283. }
  1284. static int vhost_scsi_release(struct inode *inode, struct file *f)
  1285. {
  1286. struct vhost_scsi *vs = f->private_data;
  1287. struct vhost_scsi_target t;
  1288. mutex_lock(&vs->dev.mutex);
  1289. memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
  1290. mutex_unlock(&vs->dev.mutex);
  1291. vhost_scsi_clear_endpoint(vs, &t);
  1292. vhost_dev_stop(&vs->dev);
  1293. vhost_dev_cleanup(&vs->dev, false);
  1294. /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
  1295. vhost_scsi_flush(vs);
  1296. kfree(vs->dev.vqs);
  1297. kvfree(vs);
  1298. return 0;
  1299. }
  1300. static long
  1301. vhost_scsi_ioctl(struct file *f,
  1302. unsigned int ioctl,
  1303. unsigned long arg)
  1304. {
  1305. struct vhost_scsi *vs = f->private_data;
  1306. struct vhost_scsi_target backend;
  1307. void __user *argp = (void __user *)arg;
  1308. u64 __user *featurep = argp;
  1309. u32 __user *eventsp = argp;
  1310. u32 events_missed;
  1311. u64 features;
  1312. int r, abi_version = VHOST_SCSI_ABI_VERSION;
  1313. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1314. switch (ioctl) {
  1315. case VHOST_SCSI_SET_ENDPOINT:
  1316. if (copy_from_user(&backend, argp, sizeof backend))
  1317. return -EFAULT;
  1318. if (backend.reserved != 0)
  1319. return -EOPNOTSUPP;
  1320. return vhost_scsi_set_endpoint(vs, &backend);
  1321. case VHOST_SCSI_CLEAR_ENDPOINT:
  1322. if (copy_from_user(&backend, argp, sizeof backend))
  1323. return -EFAULT;
  1324. if (backend.reserved != 0)
  1325. return -EOPNOTSUPP;
  1326. return vhost_scsi_clear_endpoint(vs, &backend);
  1327. case VHOST_SCSI_GET_ABI_VERSION:
  1328. if (copy_to_user(argp, &abi_version, sizeof abi_version))
  1329. return -EFAULT;
  1330. return 0;
  1331. case VHOST_SCSI_SET_EVENTS_MISSED:
  1332. if (get_user(events_missed, eventsp))
  1333. return -EFAULT;
  1334. mutex_lock(&vq->mutex);
  1335. vs->vs_events_missed = events_missed;
  1336. mutex_unlock(&vq->mutex);
  1337. return 0;
  1338. case VHOST_SCSI_GET_EVENTS_MISSED:
  1339. mutex_lock(&vq->mutex);
  1340. events_missed = vs->vs_events_missed;
  1341. mutex_unlock(&vq->mutex);
  1342. if (put_user(events_missed, eventsp))
  1343. return -EFAULT;
  1344. return 0;
  1345. case VHOST_GET_FEATURES:
  1346. features = VHOST_SCSI_FEATURES;
  1347. if (copy_to_user(featurep, &features, sizeof features))
  1348. return -EFAULT;
  1349. return 0;
  1350. case VHOST_SET_FEATURES:
  1351. if (copy_from_user(&features, featurep, sizeof features))
  1352. return -EFAULT;
  1353. return vhost_scsi_set_features(vs, features);
  1354. default:
  1355. mutex_lock(&vs->dev.mutex);
  1356. r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
  1357. /* TODO: flush backend after dev ioctl. */
  1358. if (r == -ENOIOCTLCMD)
  1359. r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
  1360. mutex_unlock(&vs->dev.mutex);
  1361. return r;
  1362. }
  1363. }
  1364. #ifdef CONFIG_COMPAT
  1365. static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
  1366. unsigned long arg)
  1367. {
  1368. return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  1369. }
  1370. #endif
  1371. static const struct file_operations vhost_scsi_fops = {
  1372. .owner = THIS_MODULE,
  1373. .release = vhost_scsi_release,
  1374. .unlocked_ioctl = vhost_scsi_ioctl,
  1375. #ifdef CONFIG_COMPAT
  1376. .compat_ioctl = vhost_scsi_compat_ioctl,
  1377. #endif
  1378. .open = vhost_scsi_open,
  1379. .llseek = noop_llseek,
  1380. };
  1381. static struct miscdevice vhost_scsi_misc = {
  1382. MISC_DYNAMIC_MINOR,
  1383. "vhost-scsi",
  1384. &vhost_scsi_fops,
  1385. };
  1386. static int __init vhost_scsi_register(void)
  1387. {
  1388. return misc_register(&vhost_scsi_misc);
  1389. }
  1390. static void vhost_scsi_deregister(void)
  1391. {
  1392. misc_deregister(&vhost_scsi_misc);
  1393. }
  1394. static char *vhost_scsi_dump_proto_id(struct vhost_scsi_tport *tport)
  1395. {
  1396. switch (tport->tport_proto_id) {
  1397. case SCSI_PROTOCOL_SAS:
  1398. return "SAS";
  1399. case SCSI_PROTOCOL_FCP:
  1400. return "FCP";
  1401. case SCSI_PROTOCOL_ISCSI:
  1402. return "iSCSI";
  1403. default:
  1404. break;
  1405. }
  1406. return "Unknown";
  1407. }
  1408. static void
  1409. vhost_scsi_do_plug(struct vhost_scsi_tpg *tpg,
  1410. struct se_lun *lun, bool plug)
  1411. {
  1412. struct vhost_scsi *vs = tpg->vhost_scsi;
  1413. struct vhost_virtqueue *vq;
  1414. u32 reason;
  1415. if (!vs)
  1416. return;
  1417. mutex_lock(&vs->dev.mutex);
  1418. if (plug)
  1419. reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
  1420. else
  1421. reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
  1422. vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1423. mutex_lock(&vq->mutex);
  1424. if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG))
  1425. vhost_scsi_send_evt(vs, tpg, lun,
  1426. VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
  1427. mutex_unlock(&vq->mutex);
  1428. mutex_unlock(&vs->dev.mutex);
  1429. }
  1430. static void vhost_scsi_hotplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
  1431. {
  1432. vhost_scsi_do_plug(tpg, lun, true);
  1433. }
  1434. static void vhost_scsi_hotunplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
  1435. {
  1436. vhost_scsi_do_plug(tpg, lun, false);
  1437. }
  1438. static int vhost_scsi_port_link(struct se_portal_group *se_tpg,
  1439. struct se_lun *lun)
  1440. {
  1441. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1442. struct vhost_scsi_tpg, se_tpg);
  1443. mutex_lock(&vhost_scsi_mutex);
  1444. mutex_lock(&tpg->tv_tpg_mutex);
  1445. tpg->tv_tpg_port_count++;
  1446. mutex_unlock(&tpg->tv_tpg_mutex);
  1447. vhost_scsi_hotplug(tpg, lun);
  1448. mutex_unlock(&vhost_scsi_mutex);
  1449. return 0;
  1450. }
  1451. static void vhost_scsi_port_unlink(struct se_portal_group *se_tpg,
  1452. struct se_lun *lun)
  1453. {
  1454. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1455. struct vhost_scsi_tpg, se_tpg);
  1456. mutex_lock(&vhost_scsi_mutex);
  1457. mutex_lock(&tpg->tv_tpg_mutex);
  1458. tpg->tv_tpg_port_count--;
  1459. mutex_unlock(&tpg->tv_tpg_mutex);
  1460. vhost_scsi_hotunplug(tpg, lun);
  1461. mutex_unlock(&vhost_scsi_mutex);
  1462. }
  1463. static void vhost_scsi_free_cmd_map_res(struct se_session *se_sess)
  1464. {
  1465. struct vhost_scsi_cmd *tv_cmd;
  1466. unsigned int i;
  1467. if (!se_sess->sess_cmd_map)
  1468. return;
  1469. for (i = 0; i < VHOST_SCSI_DEFAULT_TAGS; i++) {
  1470. tv_cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[i];
  1471. kfree(tv_cmd->tvc_sgl);
  1472. kfree(tv_cmd->tvc_prot_sgl);
  1473. kfree(tv_cmd->tvc_upages);
  1474. }
  1475. }
  1476. static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_store(
  1477. struct config_item *item, const char *page, size_t count)
  1478. {
  1479. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  1480. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1481. struct vhost_scsi_tpg, se_tpg);
  1482. unsigned long val;
  1483. int ret = kstrtoul(page, 0, &val);
  1484. if (ret) {
  1485. pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
  1486. return ret;
  1487. }
  1488. if (val != 0 && val != 1 && val != 3) {
  1489. pr_err("Invalid vhost_scsi fabric_prot_type: %lu\n", val);
  1490. return -EINVAL;
  1491. }
  1492. tpg->tv_fabric_prot_type = val;
  1493. return count;
  1494. }
  1495. static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_show(
  1496. struct config_item *item, char *page)
  1497. {
  1498. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  1499. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1500. struct vhost_scsi_tpg, se_tpg);
  1501. return sprintf(page, "%d\n", tpg->tv_fabric_prot_type);
  1502. }
  1503. CONFIGFS_ATTR(vhost_scsi_tpg_attrib_, fabric_prot_type);
  1504. static struct configfs_attribute *vhost_scsi_tpg_attrib_attrs[] = {
  1505. &vhost_scsi_tpg_attrib_attr_fabric_prot_type,
  1506. NULL,
  1507. };
  1508. static int vhost_scsi_nexus_cb(struct se_portal_group *se_tpg,
  1509. struct se_session *se_sess, void *p)
  1510. {
  1511. struct vhost_scsi_cmd *tv_cmd;
  1512. unsigned int i;
  1513. for (i = 0; i < VHOST_SCSI_DEFAULT_TAGS; i++) {
  1514. tv_cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[i];
  1515. tv_cmd->tvc_sgl = kzalloc(sizeof(struct scatterlist) *
  1516. VHOST_SCSI_PREALLOC_SGLS, GFP_KERNEL);
  1517. if (!tv_cmd->tvc_sgl) {
  1518. pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
  1519. goto out;
  1520. }
  1521. tv_cmd->tvc_upages = kzalloc(sizeof(struct page *) *
  1522. VHOST_SCSI_PREALLOC_UPAGES, GFP_KERNEL);
  1523. if (!tv_cmd->tvc_upages) {
  1524. pr_err("Unable to allocate tv_cmd->tvc_upages\n");
  1525. goto out;
  1526. }
  1527. tv_cmd->tvc_prot_sgl = kzalloc(sizeof(struct scatterlist) *
  1528. VHOST_SCSI_PREALLOC_PROT_SGLS, GFP_KERNEL);
  1529. if (!tv_cmd->tvc_prot_sgl) {
  1530. pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
  1531. goto out;
  1532. }
  1533. }
  1534. return 0;
  1535. out:
  1536. vhost_scsi_free_cmd_map_res(se_sess);
  1537. return -ENOMEM;
  1538. }
  1539. static int vhost_scsi_make_nexus(struct vhost_scsi_tpg *tpg,
  1540. const char *name)
  1541. {
  1542. struct se_portal_group *se_tpg;
  1543. struct vhost_scsi_nexus *tv_nexus;
  1544. mutex_lock(&tpg->tv_tpg_mutex);
  1545. if (tpg->tpg_nexus) {
  1546. mutex_unlock(&tpg->tv_tpg_mutex);
  1547. pr_debug("tpg->tpg_nexus already exists\n");
  1548. return -EEXIST;
  1549. }
  1550. se_tpg = &tpg->se_tpg;
  1551. tv_nexus = kzalloc(sizeof(struct vhost_scsi_nexus), GFP_KERNEL);
  1552. if (!tv_nexus) {
  1553. mutex_unlock(&tpg->tv_tpg_mutex);
  1554. pr_err("Unable to allocate struct vhost_scsi_nexus\n");
  1555. return -ENOMEM;
  1556. }
  1557. /*
  1558. * Since we are running in 'demo mode' this call with generate a
  1559. * struct se_node_acl for the vhost_scsi struct se_portal_group with
  1560. * the SCSI Initiator port name of the passed configfs group 'name'.
  1561. */
  1562. tv_nexus->tvn_se_sess = target_alloc_session(&tpg->se_tpg,
  1563. VHOST_SCSI_DEFAULT_TAGS,
  1564. sizeof(struct vhost_scsi_cmd),
  1565. TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS,
  1566. (unsigned char *)name, tv_nexus,
  1567. vhost_scsi_nexus_cb);
  1568. if (IS_ERR(tv_nexus->tvn_se_sess)) {
  1569. mutex_unlock(&tpg->tv_tpg_mutex);
  1570. kfree(tv_nexus);
  1571. return -ENOMEM;
  1572. }
  1573. tpg->tpg_nexus = tv_nexus;
  1574. mutex_unlock(&tpg->tv_tpg_mutex);
  1575. return 0;
  1576. }
  1577. static int vhost_scsi_drop_nexus(struct vhost_scsi_tpg *tpg)
  1578. {
  1579. struct se_session *se_sess;
  1580. struct vhost_scsi_nexus *tv_nexus;
  1581. mutex_lock(&tpg->tv_tpg_mutex);
  1582. tv_nexus = tpg->tpg_nexus;
  1583. if (!tv_nexus) {
  1584. mutex_unlock(&tpg->tv_tpg_mutex);
  1585. return -ENODEV;
  1586. }
  1587. se_sess = tv_nexus->tvn_se_sess;
  1588. if (!se_sess) {
  1589. mutex_unlock(&tpg->tv_tpg_mutex);
  1590. return -ENODEV;
  1591. }
  1592. if (tpg->tv_tpg_port_count != 0) {
  1593. mutex_unlock(&tpg->tv_tpg_mutex);
  1594. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1595. " active TPG port count: %d\n",
  1596. tpg->tv_tpg_port_count);
  1597. return -EBUSY;
  1598. }
  1599. if (tpg->tv_tpg_vhost_count != 0) {
  1600. mutex_unlock(&tpg->tv_tpg_mutex);
  1601. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1602. " active TPG vhost count: %d\n",
  1603. tpg->tv_tpg_vhost_count);
  1604. return -EBUSY;
  1605. }
  1606. pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
  1607. " %s Initiator Port: %s\n", vhost_scsi_dump_proto_id(tpg->tport),
  1608. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1609. vhost_scsi_free_cmd_map_res(se_sess);
  1610. /*
  1611. * Release the SCSI I_T Nexus to the emulated vhost Target Port
  1612. */
  1613. transport_deregister_session(tv_nexus->tvn_se_sess);
  1614. tpg->tpg_nexus = NULL;
  1615. mutex_unlock(&tpg->tv_tpg_mutex);
  1616. kfree(tv_nexus);
  1617. return 0;
  1618. }
  1619. static ssize_t vhost_scsi_tpg_nexus_show(struct config_item *item, char *page)
  1620. {
  1621. struct se_portal_group *se_tpg = to_tpg(item);
  1622. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1623. struct vhost_scsi_tpg, se_tpg);
  1624. struct vhost_scsi_nexus *tv_nexus;
  1625. ssize_t ret;
  1626. mutex_lock(&tpg->tv_tpg_mutex);
  1627. tv_nexus = tpg->tpg_nexus;
  1628. if (!tv_nexus) {
  1629. mutex_unlock(&tpg->tv_tpg_mutex);
  1630. return -ENODEV;
  1631. }
  1632. ret = snprintf(page, PAGE_SIZE, "%s\n",
  1633. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1634. mutex_unlock(&tpg->tv_tpg_mutex);
  1635. return ret;
  1636. }
  1637. static ssize_t vhost_scsi_tpg_nexus_store(struct config_item *item,
  1638. const char *page, size_t count)
  1639. {
  1640. struct se_portal_group *se_tpg = to_tpg(item);
  1641. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1642. struct vhost_scsi_tpg, se_tpg);
  1643. struct vhost_scsi_tport *tport_wwn = tpg->tport;
  1644. unsigned char i_port[VHOST_SCSI_NAMELEN], *ptr, *port_ptr;
  1645. int ret;
  1646. /*
  1647. * Shutdown the active I_T nexus if 'NULL' is passed..
  1648. */
  1649. if (!strncmp(page, "NULL", 4)) {
  1650. ret = vhost_scsi_drop_nexus(tpg);
  1651. return (!ret) ? count : ret;
  1652. }
  1653. /*
  1654. * Otherwise make sure the passed virtual Initiator port WWN matches
  1655. * the fabric protocol_id set in vhost_scsi_make_tport(), and call
  1656. * vhost_scsi_make_nexus().
  1657. */
  1658. if (strlen(page) >= VHOST_SCSI_NAMELEN) {
  1659. pr_err("Emulated NAA Sas Address: %s, exceeds"
  1660. " max: %d\n", page, VHOST_SCSI_NAMELEN);
  1661. return -EINVAL;
  1662. }
  1663. snprintf(&i_port[0], VHOST_SCSI_NAMELEN, "%s", page);
  1664. ptr = strstr(i_port, "naa.");
  1665. if (ptr) {
  1666. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
  1667. pr_err("Passed SAS Initiator Port %s does not"
  1668. " match target port protoid: %s\n", i_port,
  1669. vhost_scsi_dump_proto_id(tport_wwn));
  1670. return -EINVAL;
  1671. }
  1672. port_ptr = &i_port[0];
  1673. goto check_newline;
  1674. }
  1675. ptr = strstr(i_port, "fc.");
  1676. if (ptr) {
  1677. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
  1678. pr_err("Passed FCP Initiator Port %s does not"
  1679. " match target port protoid: %s\n", i_port,
  1680. vhost_scsi_dump_proto_id(tport_wwn));
  1681. return -EINVAL;
  1682. }
  1683. port_ptr = &i_port[3]; /* Skip over "fc." */
  1684. goto check_newline;
  1685. }
  1686. ptr = strstr(i_port, "iqn.");
  1687. if (ptr) {
  1688. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
  1689. pr_err("Passed iSCSI Initiator Port %s does not"
  1690. " match target port protoid: %s\n", i_port,
  1691. vhost_scsi_dump_proto_id(tport_wwn));
  1692. return -EINVAL;
  1693. }
  1694. port_ptr = &i_port[0];
  1695. goto check_newline;
  1696. }
  1697. pr_err("Unable to locate prefix for emulated Initiator Port:"
  1698. " %s\n", i_port);
  1699. return -EINVAL;
  1700. /*
  1701. * Clear any trailing newline for the NAA WWN
  1702. */
  1703. check_newline:
  1704. if (i_port[strlen(i_port)-1] == '\n')
  1705. i_port[strlen(i_port)-1] = '\0';
  1706. ret = vhost_scsi_make_nexus(tpg, port_ptr);
  1707. if (ret < 0)
  1708. return ret;
  1709. return count;
  1710. }
  1711. CONFIGFS_ATTR(vhost_scsi_tpg_, nexus);
  1712. static struct configfs_attribute *vhost_scsi_tpg_attrs[] = {
  1713. &vhost_scsi_tpg_attr_nexus,
  1714. NULL,
  1715. };
  1716. static struct se_portal_group *
  1717. vhost_scsi_make_tpg(struct se_wwn *wwn,
  1718. struct config_group *group,
  1719. const char *name)
  1720. {
  1721. struct vhost_scsi_tport *tport = container_of(wwn,
  1722. struct vhost_scsi_tport, tport_wwn);
  1723. struct vhost_scsi_tpg *tpg;
  1724. u16 tpgt;
  1725. int ret;
  1726. if (strstr(name, "tpgt_") != name)
  1727. return ERR_PTR(-EINVAL);
  1728. if (kstrtou16(name + 5, 10, &tpgt) || tpgt >= VHOST_SCSI_MAX_TARGET)
  1729. return ERR_PTR(-EINVAL);
  1730. tpg = kzalloc(sizeof(struct vhost_scsi_tpg), GFP_KERNEL);
  1731. if (!tpg) {
  1732. pr_err("Unable to allocate struct vhost_scsi_tpg");
  1733. return ERR_PTR(-ENOMEM);
  1734. }
  1735. mutex_init(&tpg->tv_tpg_mutex);
  1736. INIT_LIST_HEAD(&tpg->tv_tpg_list);
  1737. tpg->tport = tport;
  1738. tpg->tport_tpgt = tpgt;
  1739. ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
  1740. if (ret < 0) {
  1741. kfree(tpg);
  1742. return NULL;
  1743. }
  1744. mutex_lock(&vhost_scsi_mutex);
  1745. list_add_tail(&tpg->tv_tpg_list, &vhost_scsi_list);
  1746. mutex_unlock(&vhost_scsi_mutex);
  1747. return &tpg->se_tpg;
  1748. }
  1749. static void vhost_scsi_drop_tpg(struct se_portal_group *se_tpg)
  1750. {
  1751. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1752. struct vhost_scsi_tpg, se_tpg);
  1753. mutex_lock(&vhost_scsi_mutex);
  1754. list_del(&tpg->tv_tpg_list);
  1755. mutex_unlock(&vhost_scsi_mutex);
  1756. /*
  1757. * Release the virtual I_T Nexus for this vhost TPG
  1758. */
  1759. vhost_scsi_drop_nexus(tpg);
  1760. /*
  1761. * Deregister the se_tpg from TCM..
  1762. */
  1763. core_tpg_deregister(se_tpg);
  1764. kfree(tpg);
  1765. }
  1766. static struct se_wwn *
  1767. vhost_scsi_make_tport(struct target_fabric_configfs *tf,
  1768. struct config_group *group,
  1769. const char *name)
  1770. {
  1771. struct vhost_scsi_tport *tport;
  1772. char *ptr;
  1773. u64 wwpn = 0;
  1774. int off = 0;
  1775. /* if (vhost_scsi_parse_wwn(name, &wwpn, 1) < 0)
  1776. return ERR_PTR(-EINVAL); */
  1777. tport = kzalloc(sizeof(struct vhost_scsi_tport), GFP_KERNEL);
  1778. if (!tport) {
  1779. pr_err("Unable to allocate struct vhost_scsi_tport");
  1780. return ERR_PTR(-ENOMEM);
  1781. }
  1782. tport->tport_wwpn = wwpn;
  1783. /*
  1784. * Determine the emulated Protocol Identifier and Target Port Name
  1785. * based on the incoming configfs directory name.
  1786. */
  1787. ptr = strstr(name, "naa.");
  1788. if (ptr) {
  1789. tport->tport_proto_id = SCSI_PROTOCOL_SAS;
  1790. goto check_len;
  1791. }
  1792. ptr = strstr(name, "fc.");
  1793. if (ptr) {
  1794. tport->tport_proto_id = SCSI_PROTOCOL_FCP;
  1795. off = 3; /* Skip over "fc." */
  1796. goto check_len;
  1797. }
  1798. ptr = strstr(name, "iqn.");
  1799. if (ptr) {
  1800. tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
  1801. goto check_len;
  1802. }
  1803. pr_err("Unable to locate prefix for emulated Target Port:"
  1804. " %s\n", name);
  1805. kfree(tport);
  1806. return ERR_PTR(-EINVAL);
  1807. check_len:
  1808. if (strlen(name) >= VHOST_SCSI_NAMELEN) {
  1809. pr_err("Emulated %s Address: %s, exceeds"
  1810. " max: %d\n", name, vhost_scsi_dump_proto_id(tport),
  1811. VHOST_SCSI_NAMELEN);
  1812. kfree(tport);
  1813. return ERR_PTR(-EINVAL);
  1814. }
  1815. snprintf(&tport->tport_name[0], VHOST_SCSI_NAMELEN, "%s", &name[off]);
  1816. pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
  1817. " %s Address: %s\n", vhost_scsi_dump_proto_id(tport), name);
  1818. return &tport->tport_wwn;
  1819. }
  1820. static void vhost_scsi_drop_tport(struct se_wwn *wwn)
  1821. {
  1822. struct vhost_scsi_tport *tport = container_of(wwn,
  1823. struct vhost_scsi_tport, tport_wwn);
  1824. pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
  1825. " %s Address: %s\n", vhost_scsi_dump_proto_id(tport),
  1826. tport->tport_name);
  1827. kfree(tport);
  1828. }
  1829. static ssize_t
  1830. vhost_scsi_wwn_version_show(struct config_item *item, char *page)
  1831. {
  1832. return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
  1833. "on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
  1834. utsname()->machine);
  1835. }
  1836. CONFIGFS_ATTR_RO(vhost_scsi_wwn_, version);
  1837. static struct configfs_attribute *vhost_scsi_wwn_attrs[] = {
  1838. &vhost_scsi_wwn_attr_version,
  1839. NULL,
  1840. };
  1841. static struct target_core_fabric_ops vhost_scsi_ops = {
  1842. .module = THIS_MODULE,
  1843. .name = "vhost",
  1844. .get_fabric_name = vhost_scsi_get_fabric_name,
  1845. .tpg_get_wwn = vhost_scsi_get_fabric_wwn,
  1846. .tpg_get_tag = vhost_scsi_get_tpgt,
  1847. .tpg_check_demo_mode = vhost_scsi_check_true,
  1848. .tpg_check_demo_mode_cache = vhost_scsi_check_true,
  1849. .tpg_check_demo_mode_write_protect = vhost_scsi_check_false,
  1850. .tpg_check_prod_mode_write_protect = vhost_scsi_check_false,
  1851. .tpg_check_prot_fabric_only = vhost_scsi_check_prot_fabric_only,
  1852. .tpg_get_inst_index = vhost_scsi_tpg_get_inst_index,
  1853. .release_cmd = vhost_scsi_release_cmd,
  1854. .check_stop_free = vhost_scsi_check_stop_free,
  1855. .shutdown_session = vhost_scsi_shutdown_session,
  1856. .close_session = vhost_scsi_close_session,
  1857. .sess_get_index = vhost_scsi_sess_get_index,
  1858. .sess_get_initiator_sid = NULL,
  1859. .write_pending = vhost_scsi_write_pending,
  1860. .write_pending_status = vhost_scsi_write_pending_status,
  1861. .set_default_node_attributes = vhost_scsi_set_default_node_attrs,
  1862. .get_cmd_state = vhost_scsi_get_cmd_state,
  1863. .queue_data_in = vhost_scsi_queue_data_in,
  1864. .queue_status = vhost_scsi_queue_status,
  1865. .queue_tm_rsp = vhost_scsi_queue_tm_rsp,
  1866. .aborted_task = vhost_scsi_aborted_task,
  1867. /*
  1868. * Setup callers for generic logic in target_core_fabric_configfs.c
  1869. */
  1870. .fabric_make_wwn = vhost_scsi_make_tport,
  1871. .fabric_drop_wwn = vhost_scsi_drop_tport,
  1872. .fabric_make_tpg = vhost_scsi_make_tpg,
  1873. .fabric_drop_tpg = vhost_scsi_drop_tpg,
  1874. .fabric_post_link = vhost_scsi_port_link,
  1875. .fabric_pre_unlink = vhost_scsi_port_unlink,
  1876. .tfc_wwn_attrs = vhost_scsi_wwn_attrs,
  1877. .tfc_tpg_base_attrs = vhost_scsi_tpg_attrs,
  1878. .tfc_tpg_attrib_attrs = vhost_scsi_tpg_attrib_attrs,
  1879. };
  1880. static int __init vhost_scsi_init(void)
  1881. {
  1882. int ret = -ENOMEM;
  1883. pr_debug("TCM_VHOST fabric module %s on %s/%s"
  1884. " on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
  1885. utsname()->machine);
  1886. /*
  1887. * Use our own dedicated workqueue for submitting I/O into
  1888. * target core to avoid contention within system_wq.
  1889. */
  1890. vhost_scsi_workqueue = alloc_workqueue("vhost_scsi", 0, 0);
  1891. if (!vhost_scsi_workqueue)
  1892. goto out;
  1893. ret = vhost_scsi_register();
  1894. if (ret < 0)
  1895. goto out_destroy_workqueue;
  1896. ret = target_register_template(&vhost_scsi_ops);
  1897. if (ret < 0)
  1898. goto out_vhost_scsi_deregister;
  1899. return 0;
  1900. out_vhost_scsi_deregister:
  1901. vhost_scsi_deregister();
  1902. out_destroy_workqueue:
  1903. destroy_workqueue(vhost_scsi_workqueue);
  1904. out:
  1905. return ret;
  1906. };
  1907. static void vhost_scsi_exit(void)
  1908. {
  1909. target_unregister_template(&vhost_scsi_ops);
  1910. vhost_scsi_deregister();
  1911. destroy_workqueue(vhost_scsi_workqueue);
  1912. };
  1913. MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
  1914. MODULE_ALIAS("tcm_vhost");
  1915. MODULE_LICENSE("GPL");
  1916. module_init(vhost_scsi_init);
  1917. module_exit(vhost_scsi_exit);