scsi.c 56 KB

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