scsi.c 57 KB

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