scsi.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*******************************************************************************
  3. * Vhost kernel TCM fabric driver for virtio SCSI initiators
  4. *
  5. * (C) Copyright 2010-2013 Datera, Inc.
  6. * (C) Copyright 2010-2012 IBM Corp.
  7. *
  8. * Authors: Nicholas A. Bellinger <nab@daterainc.com>
  9. * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
  10. ****************************************************************************/
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <generated/utsrelease.h>
  14. #include <linux/utsname.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/kthread.h>
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/configfs.h>
  21. #include <linux/ctype.h>
  22. #include <linux/compat.h>
  23. #include <linux/eventfd.h>
  24. #include <linux/fs.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/blk_types.h>
  28. #include <linux/bio.h>
  29. #include <linux/unaligned.h>
  30. #include <scsi/scsi_common.h>
  31. #include <scsi/scsi_proto.h>
  32. #include <target/target_core_base.h>
  33. #include <target/target_core_fabric.h>
  34. #include <linux/vhost.h>
  35. #include <linux/virtio_scsi.h>
  36. #include <linux/llist.h>
  37. #include <linux/bitmap.h>
  38. #include "vhost.h"
  39. #define VHOST_SCSI_VERSION "v0.1"
  40. #define VHOST_SCSI_NAMELEN 256
  41. #define VHOST_SCSI_MAX_CDB_SIZE 32
  42. #define VHOST_SCSI_PREALLOC_SGLS 2048
  43. #define VHOST_SCSI_PREALLOC_UPAGES 2048
  44. #define VHOST_SCSI_PREALLOC_PROT_SGLS 2048
  45. /* Max number of requests before requeueing the job.
  46. * Using this limit prevents one virtqueue from starving others with
  47. * request.
  48. */
  49. #define VHOST_SCSI_WEIGHT 256
  50. struct vhost_scsi_inflight {
  51. /* Wait for the flush operation to finish */
  52. struct completion comp;
  53. /* Refcount for the inflight reqs */
  54. struct kref kref;
  55. };
  56. struct vhost_scsi_cmd {
  57. /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
  58. int tvc_vq_desc;
  59. /* virtio-scsi initiator task attribute */
  60. int tvc_task_attr;
  61. /* virtio-scsi response incoming iovecs */
  62. int tvc_in_iovs;
  63. /* virtio-scsi initiator data direction */
  64. enum dma_data_direction tvc_data_direction;
  65. /* Expected data transfer length from virtio-scsi header */
  66. u32 tvc_exp_data_len;
  67. /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
  68. u64 tvc_tag;
  69. /* The number of scatterlists associated with this cmd */
  70. u32 tvc_sgl_count;
  71. u32 tvc_prot_sgl_count;
  72. /* Saved unpacked SCSI LUN for vhost_scsi_target_queue_cmd() */
  73. u32 tvc_lun;
  74. u32 copied_iov:1;
  75. const void *saved_iter_addr;
  76. struct iov_iter saved_iter;
  77. /* Pointer to the SGL formatted memory from virtio-scsi */
  78. struct scatterlist *tvc_sgl;
  79. struct scatterlist *tvc_prot_sgl;
  80. struct page **tvc_upages;
  81. /* Pointer to response header iovec */
  82. struct iovec *tvc_resp_iov;
  83. /* Pointer to vhost_scsi for our device */
  84. struct vhost_scsi *tvc_vhost;
  85. /* Pointer to vhost_virtqueue for the cmd */
  86. struct vhost_virtqueue *tvc_vq;
  87. /* Pointer to vhost nexus memory */
  88. struct vhost_scsi_nexus *tvc_nexus;
  89. /* The TCM I/O descriptor that is accessed via container_of() */
  90. struct se_cmd tvc_se_cmd;
  91. /* Copy of the incoming SCSI command descriptor block (CDB) */
  92. unsigned char tvc_cdb[VHOST_SCSI_MAX_CDB_SIZE];
  93. /* Sense buffer that will be mapped into outgoing status */
  94. unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
  95. /* Completed commands list, serviced from vhost worker thread */
  96. struct llist_node tvc_completion_list;
  97. /* Used to track inflight cmd */
  98. struct vhost_scsi_inflight *inflight;
  99. };
  100. struct vhost_scsi_nexus {
  101. /* Pointer to TCM session for I_T Nexus */
  102. struct se_session *tvn_se_sess;
  103. };
  104. struct vhost_scsi_tpg {
  105. /* Vhost port target portal group tag for TCM */
  106. u16 tport_tpgt;
  107. /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
  108. int tv_tpg_port_count;
  109. /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
  110. int tv_tpg_vhost_count;
  111. /* Used for enabling T10-PI with legacy devices */
  112. int tv_fabric_prot_type;
  113. /* list for vhost_scsi_list */
  114. struct list_head tv_tpg_list;
  115. /* Used to protect access for tpg_nexus */
  116. struct mutex tv_tpg_mutex;
  117. /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
  118. struct vhost_scsi_nexus *tpg_nexus;
  119. /* Pointer back to vhost_scsi_tport */
  120. struct vhost_scsi_tport *tport;
  121. /* Returned by vhost_scsi_make_tpg() */
  122. struct se_portal_group se_tpg;
  123. /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
  124. struct vhost_scsi *vhost_scsi;
  125. };
  126. struct vhost_scsi_tport {
  127. /* SCSI protocol the tport is providing */
  128. u8 tport_proto_id;
  129. /* Binary World Wide unique Port Name for Vhost Target port */
  130. u64 tport_wwpn;
  131. /* ASCII formatted WWPN for Vhost Target port */
  132. char tport_name[VHOST_SCSI_NAMELEN];
  133. /* Returned by vhost_scsi_make_tport() */
  134. struct se_wwn tport_wwn;
  135. };
  136. struct vhost_scsi_evt {
  137. /* event to be sent to guest */
  138. struct virtio_scsi_event event;
  139. /* event list, serviced from vhost worker thread */
  140. struct llist_node list;
  141. };
  142. enum {
  143. VHOST_SCSI_VQ_CTL = 0,
  144. VHOST_SCSI_VQ_EVT = 1,
  145. VHOST_SCSI_VQ_IO = 2,
  146. };
  147. /* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
  148. enum {
  149. VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG) |
  150. (1ULL << VIRTIO_SCSI_F_T10_PI)
  151. };
  152. #define VHOST_SCSI_MAX_TARGET 256
  153. #define VHOST_SCSI_MAX_IO_VQ 1024
  154. #define VHOST_SCSI_MAX_EVENT 128
  155. static unsigned vhost_scsi_max_io_vqs = 128;
  156. module_param_named(max_io_vqs, vhost_scsi_max_io_vqs, uint, 0644);
  157. MODULE_PARM_DESC(max_io_vqs, "Set the max number of IO virtqueues a vhost scsi device can support. The default is 128. The max is 1024.");
  158. struct vhost_scsi_virtqueue {
  159. struct vhost_virtqueue vq;
  160. struct vhost_scsi *vs;
  161. /*
  162. * Reference counting for inflight reqs, used for flush operation. At
  163. * each time, one reference tracks new commands submitted, while we
  164. * wait for another one to reach 0.
  165. */
  166. struct vhost_scsi_inflight inflights[2];
  167. /*
  168. * Indicate current inflight in use, protected by vq->mutex.
  169. * Writers must also take dev mutex and flush under it.
  170. */
  171. int inflight_idx;
  172. struct vhost_scsi_cmd *scsi_cmds;
  173. struct sbitmap scsi_tags;
  174. int max_cmds;
  175. struct vhost_work completion_work;
  176. struct llist_head completion_list;
  177. };
  178. struct vhost_scsi {
  179. /* Protected by vhost_scsi->dev.mutex */
  180. struct vhost_scsi_tpg **vs_tpg;
  181. char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
  182. struct vhost_dev dev;
  183. struct vhost_scsi_virtqueue *vqs;
  184. struct vhost_scsi_inflight **old_inflight;
  185. struct vhost_work vs_event_work; /* evt injection work item */
  186. struct llist_head vs_event_list; /* evt injection queue */
  187. bool vs_events_missed; /* any missed events, protected by vq->mutex */
  188. int vs_events_nr; /* num of pending events, protected by vq->mutex */
  189. };
  190. struct vhost_scsi_tmf {
  191. struct vhost_work vwork;
  192. struct work_struct flush_work;
  193. struct vhost_scsi *vhost;
  194. struct vhost_scsi_virtqueue *svq;
  195. struct se_cmd se_cmd;
  196. u8 scsi_resp;
  197. struct vhost_scsi_inflight *inflight;
  198. struct iovec resp_iov;
  199. int in_iovs;
  200. int vq_desc;
  201. };
  202. /*
  203. * Context for processing request and control queue operations.
  204. */
  205. struct vhost_scsi_ctx {
  206. int head;
  207. unsigned int out, in;
  208. size_t req_size, rsp_size;
  209. size_t out_size, in_size;
  210. u8 *target, *lunp;
  211. void *req;
  212. struct iov_iter out_iter;
  213. };
  214. /*
  215. * Global mutex to protect vhost_scsi TPG list for vhost IOCTLs and LIO
  216. * configfs management operations.
  217. */
  218. static DEFINE_MUTEX(vhost_scsi_mutex);
  219. static LIST_HEAD(vhost_scsi_list);
  220. static void vhost_scsi_done_inflight(struct kref *kref)
  221. {
  222. struct vhost_scsi_inflight *inflight;
  223. inflight = container_of(kref, struct vhost_scsi_inflight, kref);
  224. complete(&inflight->comp);
  225. }
  226. static void vhost_scsi_init_inflight(struct vhost_scsi *vs,
  227. struct vhost_scsi_inflight *old_inflight[])
  228. {
  229. struct vhost_scsi_inflight *new_inflight;
  230. struct vhost_virtqueue *vq;
  231. int idx, i;
  232. for (i = 0; i < vs->dev.nvqs; i++) {
  233. vq = &vs->vqs[i].vq;
  234. mutex_lock(&vq->mutex);
  235. /* store old infight */
  236. idx = vs->vqs[i].inflight_idx;
  237. if (old_inflight)
  238. old_inflight[i] = &vs->vqs[i].inflights[idx];
  239. /* setup new infight */
  240. vs->vqs[i].inflight_idx = idx ^ 1;
  241. new_inflight = &vs->vqs[i].inflights[idx ^ 1];
  242. kref_init(&new_inflight->kref);
  243. init_completion(&new_inflight->comp);
  244. mutex_unlock(&vq->mutex);
  245. }
  246. }
  247. static struct vhost_scsi_inflight *
  248. vhost_scsi_get_inflight(struct vhost_virtqueue *vq)
  249. {
  250. struct vhost_scsi_inflight *inflight;
  251. struct vhost_scsi_virtqueue *svq;
  252. svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
  253. inflight = &svq->inflights[svq->inflight_idx];
  254. kref_get(&inflight->kref);
  255. return inflight;
  256. }
  257. static void vhost_scsi_put_inflight(struct vhost_scsi_inflight *inflight)
  258. {
  259. kref_put(&inflight->kref, vhost_scsi_done_inflight);
  260. }
  261. static int vhost_scsi_check_true(struct se_portal_group *se_tpg)
  262. {
  263. return 1;
  264. }
  265. static char *vhost_scsi_get_fabric_wwn(struct se_portal_group *se_tpg)
  266. {
  267. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  268. struct vhost_scsi_tpg, se_tpg);
  269. struct vhost_scsi_tport *tport = tpg->tport;
  270. return &tport->tport_name[0];
  271. }
  272. static u16 vhost_scsi_get_tpgt(struct se_portal_group *se_tpg)
  273. {
  274. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  275. struct vhost_scsi_tpg, se_tpg);
  276. return tpg->tport_tpgt;
  277. }
  278. static int vhost_scsi_check_prot_fabric_only(struct se_portal_group *se_tpg)
  279. {
  280. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  281. struct vhost_scsi_tpg, se_tpg);
  282. return tpg->tv_fabric_prot_type;
  283. }
  284. static void vhost_scsi_release_cmd_res(struct se_cmd *se_cmd)
  285. {
  286. struct vhost_scsi_cmd *tv_cmd = container_of(se_cmd,
  287. struct vhost_scsi_cmd, tvc_se_cmd);
  288. struct vhost_scsi_virtqueue *svq = container_of(tv_cmd->tvc_vq,
  289. struct vhost_scsi_virtqueue, vq);
  290. struct vhost_scsi_inflight *inflight = tv_cmd->inflight;
  291. int i;
  292. if (tv_cmd->tvc_sgl_count) {
  293. for (i = 0; i < tv_cmd->tvc_sgl_count; i++) {
  294. if (tv_cmd->copied_iov)
  295. __free_page(sg_page(&tv_cmd->tvc_sgl[i]));
  296. else
  297. put_page(sg_page(&tv_cmd->tvc_sgl[i]));
  298. }
  299. kfree(tv_cmd->saved_iter_addr);
  300. }
  301. if (tv_cmd->tvc_prot_sgl_count) {
  302. for (i = 0; i < tv_cmd->tvc_prot_sgl_count; i++)
  303. put_page(sg_page(&tv_cmd->tvc_prot_sgl[i]));
  304. }
  305. sbitmap_clear_bit(&svq->scsi_tags, se_cmd->map_tag);
  306. vhost_scsi_put_inflight(inflight);
  307. }
  308. static void vhost_scsi_release_tmf_res(struct vhost_scsi_tmf *tmf)
  309. {
  310. struct vhost_scsi_inflight *inflight = tmf->inflight;
  311. kfree(tmf);
  312. vhost_scsi_put_inflight(inflight);
  313. }
  314. static void vhost_scsi_drop_cmds(struct vhost_scsi_virtqueue *svq)
  315. {
  316. struct vhost_scsi_cmd *cmd, *t;
  317. struct llist_node *llnode;
  318. llnode = llist_del_all(&svq->completion_list);
  319. llist_for_each_entry_safe(cmd, t, llnode, tvc_completion_list)
  320. vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd);
  321. }
  322. static void vhost_scsi_release_cmd(struct se_cmd *se_cmd)
  323. {
  324. if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
  325. struct vhost_scsi_tmf *tmf = container_of(se_cmd,
  326. struct vhost_scsi_tmf, se_cmd);
  327. schedule_work(&tmf->flush_work);
  328. } else {
  329. struct vhost_scsi_cmd *cmd = container_of(se_cmd,
  330. struct vhost_scsi_cmd, tvc_se_cmd);
  331. struct vhost_scsi_virtqueue *svq = container_of(cmd->tvc_vq,
  332. struct vhost_scsi_virtqueue, vq);
  333. llist_add(&cmd->tvc_completion_list, &svq->completion_list);
  334. if (!vhost_vq_work_queue(&svq->vq, &svq->completion_work))
  335. vhost_scsi_drop_cmds(svq);
  336. }
  337. }
  338. static int vhost_scsi_write_pending(struct se_cmd *se_cmd)
  339. {
  340. /* Go ahead and process the write immediately */
  341. target_execute_cmd(se_cmd);
  342. return 0;
  343. }
  344. static int vhost_scsi_queue_data_in(struct se_cmd *se_cmd)
  345. {
  346. transport_generic_free_cmd(se_cmd, 0);
  347. return 0;
  348. }
  349. static int vhost_scsi_queue_status(struct se_cmd *se_cmd)
  350. {
  351. transport_generic_free_cmd(se_cmd, 0);
  352. return 0;
  353. }
  354. static void vhost_scsi_queue_tm_rsp(struct se_cmd *se_cmd)
  355. {
  356. struct vhost_scsi_tmf *tmf = container_of(se_cmd, struct vhost_scsi_tmf,
  357. se_cmd);
  358. tmf->scsi_resp = se_cmd->se_tmr_req->response;
  359. transport_generic_free_cmd(&tmf->se_cmd, 0);
  360. }
  361. static void vhost_scsi_aborted_task(struct se_cmd *se_cmd)
  362. {
  363. return;
  364. }
  365. static void vhost_scsi_free_evt(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
  366. {
  367. vs->vs_events_nr--;
  368. kfree(evt);
  369. }
  370. static struct vhost_scsi_evt *
  371. vhost_scsi_allocate_evt(struct vhost_scsi *vs,
  372. u32 event, u32 reason)
  373. {
  374. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  375. struct vhost_scsi_evt *evt;
  376. if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
  377. vs->vs_events_missed = true;
  378. return NULL;
  379. }
  380. evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  381. if (!evt) {
  382. vq_err(vq, "Failed to allocate vhost_scsi_evt\n");
  383. vs->vs_events_missed = true;
  384. return NULL;
  385. }
  386. evt->event.event = cpu_to_vhost32(vq, event);
  387. evt->event.reason = cpu_to_vhost32(vq, reason);
  388. vs->vs_events_nr++;
  389. return evt;
  390. }
  391. static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
  392. {
  393. return target_put_sess_cmd(se_cmd);
  394. }
  395. static void
  396. vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
  397. {
  398. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  399. struct virtio_scsi_event *event = &evt->event;
  400. struct virtio_scsi_event __user *eventp;
  401. unsigned out, in;
  402. int head, ret;
  403. if (!vhost_vq_get_backend(vq)) {
  404. vs->vs_events_missed = true;
  405. return;
  406. }
  407. again:
  408. vhost_disable_notify(&vs->dev, vq);
  409. head = vhost_get_vq_desc(vq, vq->iov,
  410. ARRAY_SIZE(vq->iov), &out, &in,
  411. NULL, NULL);
  412. if (head < 0) {
  413. vs->vs_events_missed = true;
  414. return;
  415. }
  416. if (head == vq->num) {
  417. if (vhost_enable_notify(&vs->dev, vq))
  418. goto again;
  419. vs->vs_events_missed = true;
  420. return;
  421. }
  422. if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
  423. vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
  424. vq->iov[out].iov_len);
  425. vs->vs_events_missed = true;
  426. return;
  427. }
  428. if (vs->vs_events_missed) {
  429. event->event |= cpu_to_vhost32(vq, VIRTIO_SCSI_T_EVENTS_MISSED);
  430. vs->vs_events_missed = false;
  431. }
  432. eventp = vq->iov[out].iov_base;
  433. ret = __copy_to_user(eventp, event, sizeof(*event));
  434. if (!ret)
  435. vhost_add_used_and_signal(&vs->dev, vq, head, 0);
  436. else
  437. vq_err(vq, "Faulted on vhost_scsi_send_event\n");
  438. }
  439. static void vhost_scsi_complete_events(struct vhost_scsi *vs, bool drop)
  440. {
  441. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  442. struct vhost_scsi_evt *evt, *t;
  443. struct llist_node *llnode;
  444. mutex_lock(&vq->mutex);
  445. llnode = llist_del_all(&vs->vs_event_list);
  446. llist_for_each_entry_safe(evt, t, llnode, list) {
  447. if (!drop)
  448. vhost_scsi_do_evt_work(vs, evt);
  449. vhost_scsi_free_evt(vs, evt);
  450. }
  451. mutex_unlock(&vq->mutex);
  452. }
  453. static void vhost_scsi_evt_work(struct vhost_work *work)
  454. {
  455. struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
  456. vs_event_work);
  457. vhost_scsi_complete_events(vs, false);
  458. }
  459. static int vhost_scsi_copy_sgl_to_iov(struct vhost_scsi_cmd *cmd)
  460. {
  461. struct iov_iter *iter = &cmd->saved_iter;
  462. struct scatterlist *sg = cmd->tvc_sgl;
  463. struct page *page;
  464. size_t len;
  465. int i;
  466. for (i = 0; i < cmd->tvc_sgl_count; i++) {
  467. page = sg_page(&sg[i]);
  468. len = sg[i].length;
  469. if (copy_page_to_iter(page, 0, len, iter) != len) {
  470. pr_err("Could not copy data while handling misaligned cmd. Error %zu\n",
  471. len);
  472. return -1;
  473. }
  474. }
  475. return 0;
  476. }
  477. /* Fill in status and signal that we are done processing this command
  478. *
  479. * This is scheduled in the vhost work queue so we are called with the owner
  480. * process mm and can access the vring.
  481. */
  482. static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
  483. {
  484. struct vhost_scsi_virtqueue *svq = container_of(work,
  485. struct vhost_scsi_virtqueue, completion_work);
  486. struct virtio_scsi_cmd_resp v_rsp;
  487. struct vhost_scsi_cmd *cmd, *t;
  488. struct llist_node *llnode;
  489. struct se_cmd *se_cmd;
  490. struct iov_iter iov_iter;
  491. bool signal = false;
  492. int ret;
  493. llnode = llist_del_all(&svq->completion_list);
  494. mutex_lock(&svq->vq.mutex);
  495. llist_for_each_entry_safe(cmd, t, llnode, tvc_completion_list) {
  496. se_cmd = &cmd->tvc_se_cmd;
  497. pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
  498. cmd, se_cmd->residual_count, se_cmd->scsi_status);
  499. memset(&v_rsp, 0, sizeof(v_rsp));
  500. if (cmd->saved_iter_addr && vhost_scsi_copy_sgl_to_iov(cmd)) {
  501. v_rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
  502. } else {
  503. v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq,
  504. se_cmd->residual_count);
  505. /* TODO is status_qualifier field needed? */
  506. v_rsp.status = se_cmd->scsi_status;
  507. v_rsp.sense_len = cpu_to_vhost32(cmd->tvc_vq,
  508. se_cmd->scsi_sense_length);
  509. memcpy(v_rsp.sense, cmd->tvc_sense_buf,
  510. se_cmd->scsi_sense_length);
  511. }
  512. iov_iter_init(&iov_iter, ITER_DEST, cmd->tvc_resp_iov,
  513. cmd->tvc_in_iovs, sizeof(v_rsp));
  514. ret = copy_to_iter(&v_rsp, sizeof(v_rsp), &iov_iter);
  515. if (likely(ret == sizeof(v_rsp))) {
  516. signal = true;
  517. vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
  518. } else
  519. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  520. vhost_scsi_release_cmd_res(se_cmd);
  521. }
  522. mutex_unlock(&svq->vq.mutex);
  523. if (signal)
  524. vhost_signal(&svq->vs->dev, &svq->vq);
  525. }
  526. static struct vhost_scsi_cmd *
  527. vhost_scsi_get_cmd(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg,
  528. unsigned char *cdb, u64 scsi_tag, u16 lun, u8 task_attr,
  529. u32 exp_data_len, int data_direction)
  530. {
  531. struct vhost_scsi_virtqueue *svq = container_of(vq,
  532. struct vhost_scsi_virtqueue, vq);
  533. struct vhost_scsi_cmd *cmd;
  534. struct vhost_scsi_nexus *tv_nexus;
  535. struct scatterlist *sg, *prot_sg;
  536. struct iovec *tvc_resp_iov;
  537. struct page **pages;
  538. int tag;
  539. tv_nexus = tpg->tpg_nexus;
  540. if (!tv_nexus) {
  541. pr_err("Unable to locate active struct vhost_scsi_nexus\n");
  542. return ERR_PTR(-EIO);
  543. }
  544. tag = sbitmap_get(&svq->scsi_tags);
  545. if (tag < 0) {
  546. pr_warn_once("Guest sent too many cmds. Returning TASK_SET_FULL.\n");
  547. return ERR_PTR(-ENOMEM);
  548. }
  549. cmd = &svq->scsi_cmds[tag];
  550. sg = cmd->tvc_sgl;
  551. prot_sg = cmd->tvc_prot_sgl;
  552. pages = cmd->tvc_upages;
  553. tvc_resp_iov = cmd->tvc_resp_iov;
  554. memset(cmd, 0, sizeof(*cmd));
  555. cmd->tvc_sgl = sg;
  556. cmd->tvc_prot_sgl = prot_sg;
  557. cmd->tvc_upages = pages;
  558. cmd->tvc_se_cmd.map_tag = tag;
  559. cmd->tvc_tag = scsi_tag;
  560. cmd->tvc_lun = lun;
  561. cmd->tvc_task_attr = task_attr;
  562. cmd->tvc_exp_data_len = exp_data_len;
  563. cmd->tvc_data_direction = data_direction;
  564. cmd->tvc_nexus = tv_nexus;
  565. cmd->inflight = vhost_scsi_get_inflight(vq);
  566. cmd->tvc_resp_iov = tvc_resp_iov;
  567. memcpy(cmd->tvc_cdb, cdb, VHOST_SCSI_MAX_CDB_SIZE);
  568. return cmd;
  569. }
  570. /*
  571. * Map a user memory range into a scatterlist
  572. *
  573. * Returns the number of scatterlist entries used or -errno on error.
  574. */
  575. static int
  576. vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd,
  577. struct iov_iter *iter,
  578. struct scatterlist *sgl,
  579. bool is_prot)
  580. {
  581. struct page **pages = cmd->tvc_upages;
  582. struct scatterlist *sg = sgl;
  583. ssize_t bytes, mapped_bytes;
  584. size_t offset, mapped_offset;
  585. unsigned int npages = 0;
  586. bytes = iov_iter_get_pages2(iter, pages, LONG_MAX,
  587. VHOST_SCSI_PREALLOC_UPAGES, &offset);
  588. /* No pages were pinned */
  589. if (bytes <= 0)
  590. return bytes < 0 ? bytes : -EFAULT;
  591. mapped_bytes = bytes;
  592. mapped_offset = offset;
  593. while (bytes) {
  594. unsigned n = min_t(unsigned, PAGE_SIZE - offset, bytes);
  595. /*
  596. * The block layer requires bios/requests to be a multiple of
  597. * 512 bytes, but Windows can send us vecs that are misaligned.
  598. * This can result in bios and later requests with misaligned
  599. * sizes if we have to break up a cmd/scatterlist into multiple
  600. * bios.
  601. *
  602. * We currently only break up a command into multiple bios if
  603. * we hit the vec/seg limit, so check if our sgl_count is
  604. * greater than the max and if a vec in the cmd has a
  605. * misaligned offset/size.
  606. */
  607. if (!is_prot &&
  608. (offset & (SECTOR_SIZE - 1) || n & (SECTOR_SIZE - 1)) &&
  609. cmd->tvc_sgl_count > BIO_MAX_VECS) {
  610. WARN_ONCE(true,
  611. "vhost-scsi detected misaligned IO. Performance may be degraded.");
  612. goto revert_iter_get_pages;
  613. }
  614. sg_set_page(sg++, pages[npages++], n, offset);
  615. bytes -= n;
  616. offset = 0;
  617. }
  618. return npages;
  619. revert_iter_get_pages:
  620. iov_iter_revert(iter, mapped_bytes);
  621. npages = 0;
  622. while (mapped_bytes) {
  623. unsigned int n = min_t(unsigned int, PAGE_SIZE - mapped_offset,
  624. mapped_bytes);
  625. put_page(pages[npages++]);
  626. mapped_bytes -= n;
  627. mapped_offset = 0;
  628. }
  629. return -EINVAL;
  630. }
  631. static int
  632. vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls)
  633. {
  634. int sgl_count = 0;
  635. if (!iter || !iter_iov(iter)) {
  636. pr_err("%s: iter->iov is NULL, but expected bytes: %zu"
  637. " present\n", __func__, bytes);
  638. return -EINVAL;
  639. }
  640. sgl_count = iov_iter_npages(iter, 0xffff);
  641. if (sgl_count > max_sgls) {
  642. pr_err("%s: requested sgl_count: %d exceeds pre-allocated"
  643. " max_sgls: %d\n", __func__, sgl_count, max_sgls);
  644. return -EINVAL;
  645. }
  646. return sgl_count;
  647. }
  648. static int
  649. vhost_scsi_copy_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
  650. struct scatterlist *sg, int sg_count)
  651. {
  652. size_t len = iov_iter_count(iter);
  653. unsigned int nbytes = 0;
  654. struct page *page;
  655. int i, ret;
  656. if (cmd->tvc_data_direction == DMA_FROM_DEVICE) {
  657. cmd->saved_iter_addr = dup_iter(&cmd->saved_iter, iter,
  658. GFP_KERNEL);
  659. if (!cmd->saved_iter_addr)
  660. return -ENOMEM;
  661. }
  662. for (i = 0; i < sg_count; i++) {
  663. page = alloc_page(GFP_KERNEL);
  664. if (!page) {
  665. i--;
  666. ret = -ENOMEM;
  667. goto err;
  668. }
  669. nbytes = min_t(unsigned int, PAGE_SIZE, len);
  670. sg_set_page(&sg[i], page, nbytes, 0);
  671. if (cmd->tvc_data_direction == DMA_TO_DEVICE &&
  672. copy_page_from_iter(page, 0, nbytes, iter) != nbytes) {
  673. ret = -EFAULT;
  674. goto err;
  675. }
  676. len -= nbytes;
  677. }
  678. cmd->copied_iov = 1;
  679. return 0;
  680. err:
  681. pr_err("Could not read %u bytes while handling misaligned cmd\n",
  682. nbytes);
  683. for (; i >= 0; i--)
  684. __free_page(sg_page(&sg[i]));
  685. kfree(cmd->saved_iter_addr);
  686. return ret;
  687. }
  688. static int
  689. vhost_scsi_map_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
  690. struct scatterlist *sg, int sg_count, bool is_prot)
  691. {
  692. struct scatterlist *p = sg;
  693. size_t revert_bytes;
  694. int ret;
  695. while (iov_iter_count(iter)) {
  696. ret = vhost_scsi_map_to_sgl(cmd, iter, sg, is_prot);
  697. if (ret < 0) {
  698. revert_bytes = 0;
  699. while (p < sg) {
  700. struct page *page = sg_page(p);
  701. if (page) {
  702. put_page(page);
  703. revert_bytes += p->length;
  704. }
  705. p++;
  706. }
  707. iov_iter_revert(iter, revert_bytes);
  708. return ret;
  709. }
  710. sg += ret;
  711. }
  712. return 0;
  713. }
  714. static int
  715. vhost_scsi_mapal(struct vhost_scsi_cmd *cmd,
  716. size_t prot_bytes, struct iov_iter *prot_iter,
  717. size_t data_bytes, struct iov_iter *data_iter)
  718. {
  719. int sgl_count, ret;
  720. if (prot_bytes) {
  721. sgl_count = vhost_scsi_calc_sgls(prot_iter, prot_bytes,
  722. VHOST_SCSI_PREALLOC_PROT_SGLS);
  723. if (sgl_count < 0)
  724. return sgl_count;
  725. sg_init_table(cmd->tvc_prot_sgl, sgl_count);
  726. cmd->tvc_prot_sgl_count = sgl_count;
  727. pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__,
  728. cmd->tvc_prot_sgl, cmd->tvc_prot_sgl_count);
  729. ret = vhost_scsi_map_iov_to_sgl(cmd, prot_iter,
  730. cmd->tvc_prot_sgl,
  731. cmd->tvc_prot_sgl_count, true);
  732. if (ret < 0) {
  733. cmd->tvc_prot_sgl_count = 0;
  734. return ret;
  735. }
  736. }
  737. sgl_count = vhost_scsi_calc_sgls(data_iter, data_bytes,
  738. VHOST_SCSI_PREALLOC_SGLS);
  739. if (sgl_count < 0)
  740. return sgl_count;
  741. sg_init_table(cmd->tvc_sgl, sgl_count);
  742. cmd->tvc_sgl_count = sgl_count;
  743. pr_debug("%s data_sg %p data_sgl_count %u\n", __func__,
  744. cmd->tvc_sgl, cmd->tvc_sgl_count);
  745. ret = vhost_scsi_map_iov_to_sgl(cmd, data_iter, cmd->tvc_sgl,
  746. cmd->tvc_sgl_count, false);
  747. if (ret == -EINVAL) {
  748. sg_init_table(cmd->tvc_sgl, cmd->tvc_sgl_count);
  749. ret = vhost_scsi_copy_iov_to_sgl(cmd, data_iter, cmd->tvc_sgl,
  750. cmd->tvc_sgl_count);
  751. }
  752. if (ret < 0) {
  753. cmd->tvc_sgl_count = 0;
  754. return ret;
  755. }
  756. return 0;
  757. }
  758. static int vhost_scsi_to_tcm_attr(int attr)
  759. {
  760. switch (attr) {
  761. case VIRTIO_SCSI_S_SIMPLE:
  762. return TCM_SIMPLE_TAG;
  763. case VIRTIO_SCSI_S_ORDERED:
  764. return TCM_ORDERED_TAG;
  765. case VIRTIO_SCSI_S_HEAD:
  766. return TCM_HEAD_TAG;
  767. case VIRTIO_SCSI_S_ACA:
  768. return TCM_ACA_TAG;
  769. default:
  770. break;
  771. }
  772. return TCM_SIMPLE_TAG;
  773. }
  774. static void vhost_scsi_target_queue_cmd(struct vhost_scsi_cmd *cmd)
  775. {
  776. struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
  777. struct vhost_scsi_nexus *tv_nexus;
  778. struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
  779. /* FIXME: BIDI operation */
  780. if (cmd->tvc_sgl_count) {
  781. sg_ptr = cmd->tvc_sgl;
  782. if (cmd->tvc_prot_sgl_count)
  783. sg_prot_ptr = cmd->tvc_prot_sgl;
  784. else
  785. se_cmd->prot_pto = true;
  786. } else {
  787. sg_ptr = NULL;
  788. }
  789. tv_nexus = cmd->tvc_nexus;
  790. se_cmd->tag = 0;
  791. target_init_cmd(se_cmd, tv_nexus->tvn_se_sess, &cmd->tvc_sense_buf[0],
  792. cmd->tvc_lun, cmd->tvc_exp_data_len,
  793. vhost_scsi_to_tcm_attr(cmd->tvc_task_attr),
  794. cmd->tvc_data_direction, TARGET_SCF_ACK_KREF);
  795. if (target_submit_prep(se_cmd, cmd->tvc_cdb, sg_ptr,
  796. cmd->tvc_sgl_count, NULL, 0, sg_prot_ptr,
  797. cmd->tvc_prot_sgl_count, GFP_KERNEL))
  798. return;
  799. target_submit(se_cmd);
  800. }
  801. static void
  802. vhost_scsi_send_status(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
  803. struct vhost_scsi_ctx *vc, u8 status)
  804. {
  805. struct virtio_scsi_cmd_resp rsp;
  806. struct iov_iter iov_iter;
  807. int ret;
  808. memset(&rsp, 0, sizeof(rsp));
  809. rsp.status = status;
  810. iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in,
  811. sizeof(rsp));
  812. ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter);
  813. if (likely(ret == sizeof(rsp)))
  814. vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
  815. else
  816. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  817. }
  818. #define TYPE_IO_CMD 0
  819. #define TYPE_CTRL_TMF 1
  820. #define TYPE_CTRL_AN 2
  821. static void
  822. vhost_scsi_send_bad_target(struct vhost_scsi *vs,
  823. struct vhost_virtqueue *vq,
  824. struct vhost_scsi_ctx *vc, int type)
  825. {
  826. union {
  827. struct virtio_scsi_cmd_resp cmd;
  828. struct virtio_scsi_ctrl_tmf_resp tmf;
  829. struct virtio_scsi_ctrl_an_resp an;
  830. } rsp;
  831. struct iov_iter iov_iter;
  832. size_t rsp_size;
  833. int ret;
  834. memset(&rsp, 0, sizeof(rsp));
  835. if (type == TYPE_IO_CMD) {
  836. rsp_size = sizeof(struct virtio_scsi_cmd_resp);
  837. rsp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
  838. } else if (type == TYPE_CTRL_TMF) {
  839. rsp_size = sizeof(struct virtio_scsi_ctrl_tmf_resp);
  840. rsp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
  841. } else {
  842. rsp_size = sizeof(struct virtio_scsi_ctrl_an_resp);
  843. rsp.an.response = VIRTIO_SCSI_S_BAD_TARGET;
  844. }
  845. iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in,
  846. rsp_size);
  847. ret = copy_to_iter(&rsp, rsp_size, &iov_iter);
  848. if (likely(ret == rsp_size))
  849. vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
  850. else
  851. pr_err("Faulted on virtio scsi type=%d\n", type);
  852. }
  853. static int
  854. vhost_scsi_get_desc(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
  855. struct vhost_scsi_ctx *vc)
  856. {
  857. int ret = -ENXIO;
  858. vc->head = vhost_get_vq_desc(vq, vq->iov,
  859. ARRAY_SIZE(vq->iov), &vc->out, &vc->in,
  860. NULL, NULL);
  861. pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
  862. vc->head, vc->out, vc->in);
  863. /* On error, stop handling until the next kick. */
  864. if (unlikely(vc->head < 0))
  865. goto done;
  866. /* Nothing new? Wait for eventfd to tell us they refilled. */
  867. if (vc->head == vq->num) {
  868. if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
  869. vhost_disable_notify(&vs->dev, vq);
  870. ret = -EAGAIN;
  871. }
  872. goto done;
  873. }
  874. /*
  875. * Get the size of request and response buffers.
  876. * FIXME: Not correct for BIDI operation
  877. */
  878. vc->out_size = iov_length(vq->iov, vc->out);
  879. vc->in_size = iov_length(&vq->iov[vc->out], vc->in);
  880. /*
  881. * Copy over the virtio-scsi request header, which for a
  882. * ANY_LAYOUT enabled guest may span multiple iovecs, or a
  883. * single iovec may contain both the header + outgoing
  884. * WRITE payloads.
  885. *
  886. * copy_from_iter() will advance out_iter, so that it will
  887. * point at the start of the outgoing WRITE payload, if
  888. * DMA_TO_DEVICE is set.
  889. */
  890. iov_iter_init(&vc->out_iter, ITER_SOURCE, vq->iov, vc->out, vc->out_size);
  891. ret = 0;
  892. done:
  893. return ret;
  894. }
  895. static int
  896. vhost_scsi_chk_size(struct vhost_virtqueue *vq, struct vhost_scsi_ctx *vc)
  897. {
  898. if (unlikely(vc->in_size < vc->rsp_size)) {
  899. vq_err(vq,
  900. "Response buf too small, need min %zu bytes got %zu",
  901. vc->rsp_size, vc->in_size);
  902. return -EINVAL;
  903. } else if (unlikely(vc->out_size < vc->req_size)) {
  904. vq_err(vq,
  905. "Request buf too small, need min %zu bytes got %zu",
  906. vc->req_size, vc->out_size);
  907. return -EIO;
  908. }
  909. return 0;
  910. }
  911. static int
  912. vhost_scsi_get_req(struct vhost_virtqueue *vq, struct vhost_scsi_ctx *vc,
  913. struct vhost_scsi_tpg **tpgp)
  914. {
  915. int ret = -EIO;
  916. if (unlikely(!copy_from_iter_full(vc->req, vc->req_size,
  917. &vc->out_iter))) {
  918. vq_err(vq, "Faulted on copy_from_iter_full\n");
  919. } else if (unlikely(*vc->lunp != 1)) {
  920. /* virtio-scsi spec requires byte 0 of the lun to be 1 */
  921. vq_err(vq, "Illegal virtio-scsi lun: %u\n", *vc->lunp);
  922. } else {
  923. struct vhost_scsi_tpg **vs_tpg, *tpg = NULL;
  924. if (vc->target) {
  925. /* validated at handler entry */
  926. vs_tpg = vhost_vq_get_backend(vq);
  927. tpg = READ_ONCE(vs_tpg[*vc->target]);
  928. if (unlikely(!tpg))
  929. goto out;
  930. }
  931. if (tpgp)
  932. *tpgp = tpg;
  933. ret = 0;
  934. }
  935. out:
  936. return ret;
  937. }
  938. static u16 vhost_buf_to_lun(u8 *lun_buf)
  939. {
  940. return ((lun_buf[2] << 8) | lun_buf[3]) & 0x3FFF;
  941. }
  942. static void
  943. vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
  944. {
  945. struct vhost_scsi_tpg **vs_tpg, *tpg;
  946. struct virtio_scsi_cmd_req v_req;
  947. struct virtio_scsi_cmd_req_pi v_req_pi;
  948. struct vhost_scsi_ctx vc;
  949. struct vhost_scsi_cmd *cmd;
  950. struct iov_iter in_iter, prot_iter, data_iter;
  951. u64 tag;
  952. u32 exp_data_len, data_direction;
  953. int ret, prot_bytes, i, c = 0;
  954. u16 lun;
  955. u8 task_attr;
  956. bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI);
  957. void *cdb;
  958. mutex_lock(&vq->mutex);
  959. /*
  960. * We can handle the vq only after the endpoint is setup by calling the
  961. * VHOST_SCSI_SET_ENDPOINT ioctl.
  962. */
  963. vs_tpg = vhost_vq_get_backend(vq);
  964. if (!vs_tpg)
  965. goto out;
  966. memset(&vc, 0, sizeof(vc));
  967. vc.rsp_size = sizeof(struct virtio_scsi_cmd_resp);
  968. vhost_disable_notify(&vs->dev, vq);
  969. do {
  970. ret = vhost_scsi_get_desc(vs, vq, &vc);
  971. if (ret)
  972. goto err;
  973. /*
  974. * Setup pointers and values based upon different virtio-scsi
  975. * request header if T10_PI is enabled in KVM guest.
  976. */
  977. if (t10_pi) {
  978. vc.req = &v_req_pi;
  979. vc.req_size = sizeof(v_req_pi);
  980. vc.lunp = &v_req_pi.lun[0];
  981. vc.target = &v_req_pi.lun[1];
  982. } else {
  983. vc.req = &v_req;
  984. vc.req_size = sizeof(v_req);
  985. vc.lunp = &v_req.lun[0];
  986. vc.target = &v_req.lun[1];
  987. }
  988. /*
  989. * Validate the size of request and response buffers.
  990. * Check for a sane response buffer so we can report
  991. * early errors back to the guest.
  992. */
  993. ret = vhost_scsi_chk_size(vq, &vc);
  994. if (ret)
  995. goto err;
  996. ret = vhost_scsi_get_req(vq, &vc, &tpg);
  997. if (ret)
  998. goto err;
  999. ret = -EIO; /* bad target on any error from here on */
  1000. /*
  1001. * Determine data_direction by calculating the total outgoing
  1002. * iovec sizes + incoming iovec sizes vs. virtio-scsi request +
  1003. * response headers respectively.
  1004. *
  1005. * For DMA_TO_DEVICE this is out_iter, which is already pointing
  1006. * to the right place.
  1007. *
  1008. * For DMA_FROM_DEVICE, the iovec will be just past the end
  1009. * of the virtio-scsi response header in either the same
  1010. * or immediately following iovec.
  1011. *
  1012. * Any associated T10_PI bytes for the outgoing / incoming
  1013. * payloads are included in calculation of exp_data_len here.
  1014. */
  1015. prot_bytes = 0;
  1016. if (vc.out_size > vc.req_size) {
  1017. data_direction = DMA_TO_DEVICE;
  1018. exp_data_len = vc.out_size - vc.req_size;
  1019. data_iter = vc.out_iter;
  1020. } else if (vc.in_size > vc.rsp_size) {
  1021. data_direction = DMA_FROM_DEVICE;
  1022. exp_data_len = vc.in_size - vc.rsp_size;
  1023. iov_iter_init(&in_iter, ITER_DEST, &vq->iov[vc.out], vc.in,
  1024. vc.rsp_size + exp_data_len);
  1025. iov_iter_advance(&in_iter, vc.rsp_size);
  1026. data_iter = in_iter;
  1027. } else {
  1028. data_direction = DMA_NONE;
  1029. exp_data_len = 0;
  1030. }
  1031. /*
  1032. * If T10_PI header + payload is present, setup prot_iter values
  1033. * and recalculate data_iter for vhost_scsi_mapal() mapping to
  1034. * host scatterlists via get_user_pages_fast().
  1035. */
  1036. if (t10_pi) {
  1037. if (v_req_pi.pi_bytesout) {
  1038. if (data_direction != DMA_TO_DEVICE) {
  1039. vq_err(vq, "Received non zero pi_bytesout,"
  1040. " but wrong data_direction\n");
  1041. goto err;
  1042. }
  1043. prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout);
  1044. } else if (v_req_pi.pi_bytesin) {
  1045. if (data_direction != DMA_FROM_DEVICE) {
  1046. vq_err(vq, "Received non zero pi_bytesin,"
  1047. " but wrong data_direction\n");
  1048. goto err;
  1049. }
  1050. prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin);
  1051. }
  1052. /*
  1053. * Set prot_iter to data_iter and truncate it to
  1054. * prot_bytes, and advance data_iter past any
  1055. * preceding prot_bytes that may be present.
  1056. *
  1057. * Also fix up the exp_data_len to reflect only the
  1058. * actual data payload length.
  1059. */
  1060. if (prot_bytes) {
  1061. exp_data_len -= prot_bytes;
  1062. prot_iter = data_iter;
  1063. iov_iter_truncate(&prot_iter, prot_bytes);
  1064. iov_iter_advance(&data_iter, prot_bytes);
  1065. }
  1066. tag = vhost64_to_cpu(vq, v_req_pi.tag);
  1067. task_attr = v_req_pi.task_attr;
  1068. cdb = &v_req_pi.cdb[0];
  1069. lun = vhost_buf_to_lun(v_req_pi.lun);
  1070. } else {
  1071. tag = vhost64_to_cpu(vq, v_req.tag);
  1072. task_attr = v_req.task_attr;
  1073. cdb = &v_req.cdb[0];
  1074. lun = vhost_buf_to_lun(v_req.lun);
  1075. }
  1076. /*
  1077. * Check that the received CDB size does not exceeded our
  1078. * hardcoded max for vhost-scsi, then get a pre-allocated
  1079. * cmd descriptor for the new virtio-scsi tag.
  1080. *
  1081. * TODO what if cdb was too small for varlen cdb header?
  1082. */
  1083. if (unlikely(scsi_command_size(cdb) > VHOST_SCSI_MAX_CDB_SIZE)) {
  1084. vq_err(vq, "Received SCSI CDB with command_size: %d that"
  1085. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1086. scsi_command_size(cdb), VHOST_SCSI_MAX_CDB_SIZE);
  1087. goto err;
  1088. }
  1089. cmd = vhost_scsi_get_cmd(vq, tpg, cdb, tag, lun, task_attr,
  1090. exp_data_len + prot_bytes,
  1091. data_direction);
  1092. if (IS_ERR(cmd)) {
  1093. ret = PTR_ERR(cmd);
  1094. vq_err(vq, "vhost_scsi_get_tag failed %dd\n", ret);
  1095. goto err;
  1096. }
  1097. cmd->tvc_vhost = vs;
  1098. cmd->tvc_vq = vq;
  1099. for (i = 0; i < vc.in ; i++)
  1100. cmd->tvc_resp_iov[i] = vq->iov[vc.out + i];
  1101. cmd->tvc_in_iovs = vc.in;
  1102. pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
  1103. cmd->tvc_cdb[0], cmd->tvc_lun);
  1104. pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
  1105. " %d\n", cmd, exp_data_len, prot_bytes, data_direction);
  1106. if (data_direction != DMA_NONE) {
  1107. ret = vhost_scsi_mapal(cmd, prot_bytes, &prot_iter,
  1108. exp_data_len, &data_iter);
  1109. if (unlikely(ret)) {
  1110. vq_err(vq, "Failed to map iov to sgl\n");
  1111. vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd);
  1112. goto err;
  1113. }
  1114. }
  1115. /*
  1116. * Save the descriptor from vhost_get_vq_desc() to be used to
  1117. * complete the virtio-scsi request in TCM callback context via
  1118. * vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
  1119. */
  1120. cmd->tvc_vq_desc = vc.head;
  1121. vhost_scsi_target_queue_cmd(cmd);
  1122. ret = 0;
  1123. err:
  1124. /*
  1125. * ENXIO: No more requests, or read error, wait for next kick
  1126. * EINVAL: Invalid response buffer, drop the request
  1127. * EIO: Respond with bad target
  1128. * EAGAIN: Pending request
  1129. * ENOMEM: Could not allocate resources for request
  1130. */
  1131. if (ret == -ENXIO)
  1132. break;
  1133. else if (ret == -EIO)
  1134. vhost_scsi_send_bad_target(vs, vq, &vc, TYPE_IO_CMD);
  1135. else if (ret == -ENOMEM)
  1136. vhost_scsi_send_status(vs, vq, &vc,
  1137. SAM_STAT_TASK_SET_FULL);
  1138. } while (likely(!vhost_exceeds_weight(vq, ++c, 0)));
  1139. out:
  1140. mutex_unlock(&vq->mutex);
  1141. }
  1142. static void
  1143. vhost_scsi_send_tmf_resp(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
  1144. int in_iovs, int vq_desc, struct iovec *resp_iov,
  1145. int tmf_resp_code)
  1146. {
  1147. struct virtio_scsi_ctrl_tmf_resp rsp;
  1148. struct iov_iter iov_iter;
  1149. int ret;
  1150. pr_debug("%s\n", __func__);
  1151. memset(&rsp, 0, sizeof(rsp));
  1152. rsp.response = tmf_resp_code;
  1153. iov_iter_init(&iov_iter, ITER_DEST, resp_iov, in_iovs, sizeof(rsp));
  1154. ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter);
  1155. if (likely(ret == sizeof(rsp)))
  1156. vhost_add_used_and_signal(&vs->dev, vq, vq_desc, 0);
  1157. else
  1158. pr_err("Faulted on virtio_scsi_ctrl_tmf_resp\n");
  1159. }
  1160. static void vhost_scsi_tmf_resp_work(struct vhost_work *work)
  1161. {
  1162. struct vhost_scsi_tmf *tmf = container_of(work, struct vhost_scsi_tmf,
  1163. vwork);
  1164. int resp_code;
  1165. if (tmf->scsi_resp == TMR_FUNCTION_COMPLETE)
  1166. resp_code = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
  1167. else
  1168. resp_code = VIRTIO_SCSI_S_FUNCTION_REJECTED;
  1169. mutex_lock(&tmf->svq->vq.mutex);
  1170. vhost_scsi_send_tmf_resp(tmf->vhost, &tmf->svq->vq, tmf->in_iovs,
  1171. tmf->vq_desc, &tmf->resp_iov, resp_code);
  1172. mutex_unlock(&tmf->svq->vq.mutex);
  1173. vhost_scsi_release_tmf_res(tmf);
  1174. }
  1175. static void vhost_scsi_tmf_flush_work(struct work_struct *work)
  1176. {
  1177. struct vhost_scsi_tmf *tmf = container_of(work, struct vhost_scsi_tmf,
  1178. flush_work);
  1179. struct vhost_virtqueue *vq = &tmf->svq->vq;
  1180. /*
  1181. * Make sure we have sent responses for other commands before we
  1182. * send our response.
  1183. */
  1184. vhost_dev_flush(vq->dev);
  1185. if (!vhost_vq_work_queue(vq, &tmf->vwork))
  1186. vhost_scsi_release_tmf_res(tmf);
  1187. }
  1188. static void
  1189. vhost_scsi_handle_tmf(struct vhost_scsi *vs, struct vhost_scsi_tpg *tpg,
  1190. struct vhost_virtqueue *vq,
  1191. struct virtio_scsi_ctrl_tmf_req *vtmf,
  1192. struct vhost_scsi_ctx *vc)
  1193. {
  1194. struct vhost_scsi_virtqueue *svq = container_of(vq,
  1195. struct vhost_scsi_virtqueue, vq);
  1196. struct vhost_scsi_tmf *tmf;
  1197. if (vhost32_to_cpu(vq, vtmf->subtype) !=
  1198. VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET)
  1199. goto send_reject;
  1200. if (!tpg->tpg_nexus || !tpg->tpg_nexus->tvn_se_sess) {
  1201. pr_err("Unable to locate active struct vhost_scsi_nexus for LUN RESET.\n");
  1202. goto send_reject;
  1203. }
  1204. tmf = kzalloc(sizeof(*tmf), GFP_KERNEL);
  1205. if (!tmf)
  1206. goto send_reject;
  1207. INIT_WORK(&tmf->flush_work, vhost_scsi_tmf_flush_work);
  1208. vhost_work_init(&tmf->vwork, vhost_scsi_tmf_resp_work);
  1209. tmf->vhost = vs;
  1210. tmf->svq = svq;
  1211. tmf->resp_iov = vq->iov[vc->out];
  1212. tmf->vq_desc = vc->head;
  1213. tmf->in_iovs = vc->in;
  1214. tmf->inflight = vhost_scsi_get_inflight(vq);
  1215. if (target_submit_tmr(&tmf->se_cmd, tpg->tpg_nexus->tvn_se_sess, NULL,
  1216. vhost_buf_to_lun(vtmf->lun), NULL,
  1217. TMR_LUN_RESET, GFP_KERNEL, 0,
  1218. TARGET_SCF_ACK_KREF) < 0) {
  1219. vhost_scsi_release_tmf_res(tmf);
  1220. goto send_reject;
  1221. }
  1222. return;
  1223. send_reject:
  1224. vhost_scsi_send_tmf_resp(vs, vq, vc->in, vc->head, &vq->iov[vc->out],
  1225. VIRTIO_SCSI_S_FUNCTION_REJECTED);
  1226. }
  1227. static void
  1228. vhost_scsi_send_an_resp(struct vhost_scsi *vs,
  1229. struct vhost_virtqueue *vq,
  1230. struct vhost_scsi_ctx *vc)
  1231. {
  1232. struct virtio_scsi_ctrl_an_resp rsp;
  1233. struct iov_iter iov_iter;
  1234. int ret;
  1235. pr_debug("%s\n", __func__);
  1236. memset(&rsp, 0, sizeof(rsp)); /* event_actual = 0 */
  1237. rsp.response = VIRTIO_SCSI_S_OK;
  1238. iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in, sizeof(rsp));
  1239. ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter);
  1240. if (likely(ret == sizeof(rsp)))
  1241. vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
  1242. else
  1243. pr_err("Faulted on virtio_scsi_ctrl_an_resp\n");
  1244. }
  1245. static void
  1246. vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
  1247. {
  1248. struct vhost_scsi_tpg *tpg;
  1249. union {
  1250. __virtio32 type;
  1251. struct virtio_scsi_ctrl_an_req an;
  1252. struct virtio_scsi_ctrl_tmf_req tmf;
  1253. } v_req;
  1254. struct vhost_scsi_ctx vc;
  1255. size_t typ_size;
  1256. int ret, c = 0;
  1257. mutex_lock(&vq->mutex);
  1258. /*
  1259. * We can handle the vq only after the endpoint is setup by calling the
  1260. * VHOST_SCSI_SET_ENDPOINT ioctl.
  1261. */
  1262. if (!vhost_vq_get_backend(vq))
  1263. goto out;
  1264. memset(&vc, 0, sizeof(vc));
  1265. vhost_disable_notify(&vs->dev, vq);
  1266. do {
  1267. ret = vhost_scsi_get_desc(vs, vq, &vc);
  1268. if (ret)
  1269. goto err;
  1270. /*
  1271. * Get the request type first in order to setup
  1272. * other parameters dependent on the type.
  1273. */
  1274. vc.req = &v_req.type;
  1275. typ_size = sizeof(v_req.type);
  1276. if (unlikely(!copy_from_iter_full(vc.req, typ_size,
  1277. &vc.out_iter))) {
  1278. vq_err(vq, "Faulted on copy_from_iter tmf type\n");
  1279. /*
  1280. * The size of the response buffer depends on the
  1281. * request type and must be validated against it.
  1282. * Since the request type is not known, don't send
  1283. * a response.
  1284. */
  1285. continue;
  1286. }
  1287. switch (vhost32_to_cpu(vq, v_req.type)) {
  1288. case VIRTIO_SCSI_T_TMF:
  1289. vc.req = &v_req.tmf;
  1290. vc.req_size = sizeof(struct virtio_scsi_ctrl_tmf_req);
  1291. vc.rsp_size = sizeof(struct virtio_scsi_ctrl_tmf_resp);
  1292. vc.lunp = &v_req.tmf.lun[0];
  1293. vc.target = &v_req.tmf.lun[1];
  1294. break;
  1295. case VIRTIO_SCSI_T_AN_QUERY:
  1296. case VIRTIO_SCSI_T_AN_SUBSCRIBE:
  1297. vc.req = &v_req.an;
  1298. vc.req_size = sizeof(struct virtio_scsi_ctrl_an_req);
  1299. vc.rsp_size = sizeof(struct virtio_scsi_ctrl_an_resp);
  1300. vc.lunp = &v_req.an.lun[0];
  1301. vc.target = NULL;
  1302. break;
  1303. default:
  1304. vq_err(vq, "Unknown control request %d", v_req.type);
  1305. continue;
  1306. }
  1307. /*
  1308. * Validate the size of request and response buffers.
  1309. * Check for a sane response buffer so we can report
  1310. * early errors back to the guest.
  1311. */
  1312. ret = vhost_scsi_chk_size(vq, &vc);
  1313. if (ret)
  1314. goto err;
  1315. /*
  1316. * Get the rest of the request now that its size is known.
  1317. */
  1318. vc.req += typ_size;
  1319. vc.req_size -= typ_size;
  1320. ret = vhost_scsi_get_req(vq, &vc, &tpg);
  1321. if (ret)
  1322. goto err;
  1323. if (v_req.type == VIRTIO_SCSI_T_TMF)
  1324. vhost_scsi_handle_tmf(vs, tpg, vq, &v_req.tmf, &vc);
  1325. else
  1326. vhost_scsi_send_an_resp(vs, vq, &vc);
  1327. err:
  1328. /*
  1329. * ENXIO: No more requests, or read error, wait for next kick
  1330. * EINVAL: Invalid response buffer, drop the request
  1331. * EIO: Respond with bad target
  1332. * EAGAIN: Pending request
  1333. */
  1334. if (ret == -ENXIO)
  1335. break;
  1336. else if (ret == -EIO)
  1337. vhost_scsi_send_bad_target(vs, vq, &vc,
  1338. v_req.type == VIRTIO_SCSI_T_TMF ?
  1339. TYPE_CTRL_TMF :
  1340. TYPE_CTRL_AN);
  1341. } while (likely(!vhost_exceeds_weight(vq, ++c, 0)));
  1342. out:
  1343. mutex_unlock(&vq->mutex);
  1344. }
  1345. static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
  1346. {
  1347. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1348. poll.work);
  1349. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  1350. pr_debug("%s: The handling func for control queue.\n", __func__);
  1351. vhost_scsi_ctl_handle_vq(vs, vq);
  1352. }
  1353. static void
  1354. vhost_scsi_send_evt(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
  1355. struct vhost_scsi_tpg *tpg, struct se_lun *lun,
  1356. u32 event, u32 reason)
  1357. {
  1358. struct vhost_scsi_evt *evt;
  1359. evt = vhost_scsi_allocate_evt(vs, event, reason);
  1360. if (!evt)
  1361. return;
  1362. if (tpg && lun) {
  1363. /* TODO: share lun setup code with virtio-scsi.ko */
  1364. /*
  1365. * Note: evt->event is zeroed when we allocate it and
  1366. * lun[4-7] need to be zero according to virtio-scsi spec.
  1367. */
  1368. evt->event.lun[0] = 0x01;
  1369. evt->event.lun[1] = tpg->tport_tpgt;
  1370. if (lun->unpacked_lun >= 256)
  1371. evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
  1372. evt->event.lun[3] = lun->unpacked_lun & 0xFF;
  1373. }
  1374. llist_add(&evt->list, &vs->vs_event_list);
  1375. if (!vhost_vq_work_queue(vq, &vs->vs_event_work))
  1376. vhost_scsi_complete_events(vs, true);
  1377. }
  1378. static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
  1379. {
  1380. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1381. poll.work);
  1382. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  1383. mutex_lock(&vq->mutex);
  1384. if (!vhost_vq_get_backend(vq))
  1385. goto out;
  1386. if (vs->vs_events_missed)
  1387. vhost_scsi_send_evt(vs, vq, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT,
  1388. 0);
  1389. out:
  1390. mutex_unlock(&vq->mutex);
  1391. }
  1392. static void vhost_scsi_handle_kick(struct vhost_work *work)
  1393. {
  1394. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1395. poll.work);
  1396. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  1397. vhost_scsi_handle_vq(vs, vq);
  1398. }
  1399. /* Callers must hold dev mutex */
  1400. static void vhost_scsi_flush(struct vhost_scsi *vs)
  1401. {
  1402. int i;
  1403. /* Init new inflight and remember the old inflight */
  1404. vhost_scsi_init_inflight(vs, vs->old_inflight);
  1405. /*
  1406. * The inflight->kref was initialized to 1. We decrement it here to
  1407. * indicate the start of the flush operation so that it will reach 0
  1408. * when all the reqs are finished.
  1409. */
  1410. for (i = 0; i < vs->dev.nvqs; i++)
  1411. kref_put(&vs->old_inflight[i]->kref, vhost_scsi_done_inflight);
  1412. /* Flush both the vhost poll and vhost work */
  1413. vhost_dev_flush(&vs->dev);
  1414. /* Wait for all reqs issued before the flush to be finished */
  1415. for (i = 0; i < vs->dev.nvqs; i++)
  1416. wait_for_completion(&vs->old_inflight[i]->comp);
  1417. }
  1418. static void vhost_scsi_destroy_vq_cmds(struct vhost_virtqueue *vq)
  1419. {
  1420. struct vhost_scsi_virtqueue *svq = container_of(vq,
  1421. struct vhost_scsi_virtqueue, vq);
  1422. struct vhost_scsi_cmd *tv_cmd;
  1423. unsigned int i;
  1424. if (!svq->scsi_cmds)
  1425. return;
  1426. for (i = 0; i < svq->max_cmds; i++) {
  1427. tv_cmd = &svq->scsi_cmds[i];
  1428. kfree(tv_cmd->tvc_sgl);
  1429. kfree(tv_cmd->tvc_prot_sgl);
  1430. kfree(tv_cmd->tvc_upages);
  1431. kfree(tv_cmd->tvc_resp_iov);
  1432. }
  1433. sbitmap_free(&svq->scsi_tags);
  1434. kfree(svq->scsi_cmds);
  1435. svq->scsi_cmds = NULL;
  1436. }
  1437. static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
  1438. {
  1439. struct vhost_scsi_virtqueue *svq = container_of(vq,
  1440. struct vhost_scsi_virtqueue, vq);
  1441. struct vhost_scsi_cmd *tv_cmd;
  1442. unsigned int i;
  1443. if (svq->scsi_cmds)
  1444. return 0;
  1445. if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL,
  1446. NUMA_NO_NODE, false, true))
  1447. return -ENOMEM;
  1448. svq->max_cmds = max_cmds;
  1449. svq->scsi_cmds = kcalloc(max_cmds, sizeof(*tv_cmd), GFP_KERNEL);
  1450. if (!svq->scsi_cmds) {
  1451. sbitmap_free(&svq->scsi_tags);
  1452. return -ENOMEM;
  1453. }
  1454. for (i = 0; i < max_cmds; i++) {
  1455. tv_cmd = &svq->scsi_cmds[i];
  1456. tv_cmd->tvc_sgl = kcalloc(VHOST_SCSI_PREALLOC_SGLS,
  1457. sizeof(struct scatterlist),
  1458. GFP_KERNEL);
  1459. if (!tv_cmd->tvc_sgl) {
  1460. pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
  1461. goto out;
  1462. }
  1463. tv_cmd->tvc_upages = kcalloc(VHOST_SCSI_PREALLOC_UPAGES,
  1464. sizeof(struct page *),
  1465. GFP_KERNEL);
  1466. if (!tv_cmd->tvc_upages) {
  1467. pr_err("Unable to allocate tv_cmd->tvc_upages\n");
  1468. goto out;
  1469. }
  1470. tv_cmd->tvc_resp_iov = kcalloc(UIO_MAXIOV,
  1471. sizeof(struct iovec),
  1472. GFP_KERNEL);
  1473. if (!tv_cmd->tvc_resp_iov) {
  1474. pr_err("Unable to allocate tv_cmd->tvc_resp_iov\n");
  1475. goto out;
  1476. }
  1477. tv_cmd->tvc_prot_sgl = kcalloc(VHOST_SCSI_PREALLOC_PROT_SGLS,
  1478. sizeof(struct scatterlist),
  1479. GFP_KERNEL);
  1480. if (!tv_cmd->tvc_prot_sgl) {
  1481. pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
  1482. goto out;
  1483. }
  1484. }
  1485. return 0;
  1486. out:
  1487. vhost_scsi_destroy_vq_cmds(vq);
  1488. return -ENOMEM;
  1489. }
  1490. /*
  1491. * Called from vhost_scsi_ioctl() context to walk the list of available
  1492. * vhost_scsi_tpg with an active struct vhost_scsi_nexus
  1493. *
  1494. * The lock nesting rule is:
  1495. * vs->dev.mutex -> vhost_scsi_mutex -> tpg->tv_tpg_mutex -> vq->mutex
  1496. */
  1497. static int
  1498. vhost_scsi_set_endpoint(struct vhost_scsi *vs,
  1499. struct vhost_scsi_target *t)
  1500. {
  1501. struct se_portal_group *se_tpg;
  1502. struct vhost_scsi_tport *tv_tport;
  1503. struct vhost_scsi_tpg *tpg;
  1504. struct vhost_scsi_tpg **vs_tpg;
  1505. struct vhost_virtqueue *vq;
  1506. int index, ret, i, len;
  1507. bool match = false;
  1508. mutex_lock(&vs->dev.mutex);
  1509. /* Verify that ring has been setup correctly. */
  1510. for (index = 0; index < vs->dev.nvqs; ++index) {
  1511. /* Verify that ring has been setup correctly. */
  1512. if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
  1513. ret = -EFAULT;
  1514. goto out;
  1515. }
  1516. }
  1517. if (vs->vs_tpg) {
  1518. pr_err("vhost-scsi endpoint already set for %s.\n",
  1519. vs->vs_vhost_wwpn);
  1520. ret = -EEXIST;
  1521. goto out;
  1522. }
  1523. len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
  1524. vs_tpg = kzalloc(len, GFP_KERNEL);
  1525. if (!vs_tpg) {
  1526. ret = -ENOMEM;
  1527. goto out;
  1528. }
  1529. mutex_lock(&vhost_scsi_mutex);
  1530. list_for_each_entry(tpg, &vhost_scsi_list, tv_tpg_list) {
  1531. mutex_lock(&tpg->tv_tpg_mutex);
  1532. if (!tpg->tpg_nexus) {
  1533. mutex_unlock(&tpg->tv_tpg_mutex);
  1534. continue;
  1535. }
  1536. if (tpg->tv_tpg_vhost_count != 0) {
  1537. mutex_unlock(&tpg->tv_tpg_mutex);
  1538. continue;
  1539. }
  1540. tv_tport = tpg->tport;
  1541. if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  1542. /*
  1543. * In order to ensure individual vhost-scsi configfs
  1544. * groups cannot be removed while in use by vhost ioctl,
  1545. * go ahead and take an explicit se_tpg->tpg_group.cg_item
  1546. * dependency now.
  1547. */
  1548. se_tpg = &tpg->se_tpg;
  1549. ret = target_depend_item(&se_tpg->tpg_group.cg_item);
  1550. if (ret) {
  1551. pr_warn("target_depend_item() failed: %d\n", ret);
  1552. mutex_unlock(&tpg->tv_tpg_mutex);
  1553. mutex_unlock(&vhost_scsi_mutex);
  1554. goto undepend;
  1555. }
  1556. tpg->tv_tpg_vhost_count++;
  1557. tpg->vhost_scsi = vs;
  1558. vs_tpg[tpg->tport_tpgt] = tpg;
  1559. match = true;
  1560. }
  1561. mutex_unlock(&tpg->tv_tpg_mutex);
  1562. }
  1563. mutex_unlock(&vhost_scsi_mutex);
  1564. if (match) {
  1565. memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
  1566. sizeof(vs->vs_vhost_wwpn));
  1567. for (i = VHOST_SCSI_VQ_IO; i < vs->dev.nvqs; i++) {
  1568. vq = &vs->vqs[i].vq;
  1569. if (!vhost_vq_is_setup(vq))
  1570. continue;
  1571. ret = vhost_scsi_setup_vq_cmds(vq, vq->num);
  1572. if (ret)
  1573. goto destroy_vq_cmds;
  1574. }
  1575. for (i = 0; i < vs->dev.nvqs; i++) {
  1576. vq = &vs->vqs[i].vq;
  1577. mutex_lock(&vq->mutex);
  1578. vhost_vq_set_backend(vq, vs_tpg);
  1579. vhost_vq_init_access(vq);
  1580. mutex_unlock(&vq->mutex);
  1581. }
  1582. ret = 0;
  1583. } else {
  1584. ret = -ENODEV;
  1585. goto free_tpg;
  1586. }
  1587. /*
  1588. * Act as synchronize_rcu to make sure requests after this point
  1589. * see a fully setup device.
  1590. */
  1591. vhost_scsi_flush(vs);
  1592. vs->vs_tpg = vs_tpg;
  1593. goto out;
  1594. destroy_vq_cmds:
  1595. for (i--; i >= VHOST_SCSI_VQ_IO; i--) {
  1596. if (!vhost_vq_get_backend(&vs->vqs[i].vq))
  1597. vhost_scsi_destroy_vq_cmds(&vs->vqs[i].vq);
  1598. }
  1599. undepend:
  1600. for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
  1601. tpg = vs_tpg[i];
  1602. if (tpg) {
  1603. mutex_lock(&tpg->tv_tpg_mutex);
  1604. tpg->vhost_scsi = NULL;
  1605. tpg->tv_tpg_vhost_count--;
  1606. mutex_unlock(&tpg->tv_tpg_mutex);
  1607. target_undepend_item(&tpg->se_tpg.tpg_group.cg_item);
  1608. }
  1609. }
  1610. free_tpg:
  1611. kfree(vs_tpg);
  1612. out:
  1613. mutex_unlock(&vs->dev.mutex);
  1614. return ret;
  1615. }
  1616. static int
  1617. vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
  1618. struct vhost_scsi_target *t)
  1619. {
  1620. struct se_portal_group *se_tpg;
  1621. struct vhost_scsi_tport *tv_tport;
  1622. struct vhost_scsi_tpg *tpg;
  1623. struct vhost_virtqueue *vq;
  1624. bool match = false;
  1625. int index, ret, i;
  1626. u8 target;
  1627. mutex_lock(&vs->dev.mutex);
  1628. /* Verify that ring has been setup correctly. */
  1629. for (index = 0; index < vs->dev.nvqs; ++index) {
  1630. if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
  1631. ret = -EFAULT;
  1632. goto err_dev;
  1633. }
  1634. }
  1635. if (!vs->vs_tpg) {
  1636. ret = 0;
  1637. goto err_dev;
  1638. }
  1639. for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
  1640. target = i;
  1641. tpg = vs->vs_tpg[target];
  1642. if (!tpg)
  1643. continue;
  1644. tv_tport = tpg->tport;
  1645. if (!tv_tport) {
  1646. ret = -ENODEV;
  1647. goto err_dev;
  1648. }
  1649. if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  1650. pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
  1651. " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
  1652. tv_tport->tport_name, tpg->tport_tpgt,
  1653. t->vhost_wwpn, t->vhost_tpgt);
  1654. ret = -EINVAL;
  1655. goto err_dev;
  1656. }
  1657. match = true;
  1658. }
  1659. if (!match)
  1660. goto free_vs_tpg;
  1661. /* Prevent new cmds from starting and accessing the tpgs/sessions */
  1662. for (i = 0; i < vs->dev.nvqs; i++) {
  1663. vq = &vs->vqs[i].vq;
  1664. mutex_lock(&vq->mutex);
  1665. vhost_vq_set_backend(vq, NULL);
  1666. mutex_unlock(&vq->mutex);
  1667. }
  1668. /* Make sure cmds are not running before tearing them down. */
  1669. vhost_scsi_flush(vs);
  1670. for (i = 0; i < vs->dev.nvqs; i++) {
  1671. vq = &vs->vqs[i].vq;
  1672. vhost_scsi_destroy_vq_cmds(vq);
  1673. }
  1674. /*
  1675. * We can now release our hold on the tpg and sessions and userspace
  1676. * can free them after this point.
  1677. */
  1678. for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
  1679. target = i;
  1680. tpg = vs->vs_tpg[target];
  1681. if (!tpg)
  1682. continue;
  1683. mutex_lock(&tpg->tv_tpg_mutex);
  1684. tpg->tv_tpg_vhost_count--;
  1685. tpg->vhost_scsi = NULL;
  1686. vs->vs_tpg[target] = NULL;
  1687. mutex_unlock(&tpg->tv_tpg_mutex);
  1688. se_tpg = &tpg->se_tpg;
  1689. target_undepend_item(&se_tpg->tpg_group.cg_item);
  1690. }
  1691. free_vs_tpg:
  1692. /*
  1693. * Act as synchronize_rcu to make sure access to
  1694. * old vs->vs_tpg is finished.
  1695. */
  1696. vhost_scsi_flush(vs);
  1697. kfree(vs->vs_tpg);
  1698. vs->vs_tpg = NULL;
  1699. memset(vs->vs_vhost_wwpn, 0, sizeof(vs->vs_vhost_wwpn));
  1700. WARN_ON(vs->vs_events_nr);
  1701. mutex_unlock(&vs->dev.mutex);
  1702. return 0;
  1703. err_dev:
  1704. mutex_unlock(&vs->dev.mutex);
  1705. return ret;
  1706. }
  1707. static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
  1708. {
  1709. struct vhost_virtqueue *vq;
  1710. int i;
  1711. if (features & ~VHOST_SCSI_FEATURES)
  1712. return -EOPNOTSUPP;
  1713. mutex_lock(&vs->dev.mutex);
  1714. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  1715. !vhost_log_access_ok(&vs->dev)) {
  1716. mutex_unlock(&vs->dev.mutex);
  1717. return -EFAULT;
  1718. }
  1719. for (i = 0; i < vs->dev.nvqs; i++) {
  1720. vq = &vs->vqs[i].vq;
  1721. mutex_lock(&vq->mutex);
  1722. vq->acked_features = features;
  1723. mutex_unlock(&vq->mutex);
  1724. }
  1725. mutex_unlock(&vs->dev.mutex);
  1726. return 0;
  1727. }
  1728. static int vhost_scsi_open(struct inode *inode, struct file *f)
  1729. {
  1730. struct vhost_scsi_virtqueue *svq;
  1731. struct vhost_scsi *vs;
  1732. struct vhost_virtqueue **vqs;
  1733. int r = -ENOMEM, i, nvqs = vhost_scsi_max_io_vqs;
  1734. vs = kvzalloc(sizeof(*vs), GFP_KERNEL);
  1735. if (!vs)
  1736. goto err_vs;
  1737. if (nvqs > VHOST_SCSI_MAX_IO_VQ) {
  1738. pr_err("Invalid max_io_vqs of %d. Using %d.\n", nvqs,
  1739. VHOST_SCSI_MAX_IO_VQ);
  1740. nvqs = VHOST_SCSI_MAX_IO_VQ;
  1741. } else if (nvqs == 0) {
  1742. pr_err("Invalid max_io_vqs of %d. Using 1.\n", nvqs);
  1743. nvqs = 1;
  1744. }
  1745. nvqs += VHOST_SCSI_VQ_IO;
  1746. vs->old_inflight = kmalloc_array(nvqs, sizeof(*vs->old_inflight),
  1747. GFP_KERNEL | __GFP_ZERO);
  1748. if (!vs->old_inflight)
  1749. goto err_inflight;
  1750. vs->vqs = kmalloc_array(nvqs, sizeof(*vs->vqs),
  1751. GFP_KERNEL | __GFP_ZERO);
  1752. if (!vs->vqs)
  1753. goto err_vqs;
  1754. vqs = kmalloc_array(nvqs, sizeof(*vqs), GFP_KERNEL);
  1755. if (!vqs)
  1756. goto err_local_vqs;
  1757. vhost_work_init(&vs->vs_event_work, vhost_scsi_evt_work);
  1758. vs->vs_events_nr = 0;
  1759. vs->vs_events_missed = false;
  1760. vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
  1761. vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1762. vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
  1763. vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
  1764. for (i = VHOST_SCSI_VQ_IO; i < nvqs; i++) {
  1765. svq = &vs->vqs[i];
  1766. vqs[i] = &svq->vq;
  1767. svq->vs = vs;
  1768. init_llist_head(&svq->completion_list);
  1769. vhost_work_init(&svq->completion_work,
  1770. vhost_scsi_complete_cmd_work);
  1771. svq->vq.handle_kick = vhost_scsi_handle_kick;
  1772. }
  1773. vhost_dev_init(&vs->dev, vqs, nvqs, UIO_MAXIOV,
  1774. VHOST_SCSI_WEIGHT, 0, true, NULL);
  1775. vhost_scsi_init_inflight(vs, NULL);
  1776. f->private_data = vs;
  1777. return 0;
  1778. err_local_vqs:
  1779. kfree(vs->vqs);
  1780. err_vqs:
  1781. kfree(vs->old_inflight);
  1782. err_inflight:
  1783. kvfree(vs);
  1784. err_vs:
  1785. return r;
  1786. }
  1787. static int vhost_scsi_release(struct inode *inode, struct file *f)
  1788. {
  1789. struct vhost_scsi *vs = f->private_data;
  1790. struct vhost_scsi_target t;
  1791. mutex_lock(&vs->dev.mutex);
  1792. memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
  1793. mutex_unlock(&vs->dev.mutex);
  1794. vhost_scsi_clear_endpoint(vs, &t);
  1795. vhost_dev_stop(&vs->dev);
  1796. vhost_dev_cleanup(&vs->dev);
  1797. kfree(vs->dev.vqs);
  1798. kfree(vs->vqs);
  1799. kfree(vs->old_inflight);
  1800. kvfree(vs);
  1801. return 0;
  1802. }
  1803. static long
  1804. vhost_scsi_ioctl(struct file *f,
  1805. unsigned int ioctl,
  1806. unsigned long arg)
  1807. {
  1808. struct vhost_scsi *vs = f->private_data;
  1809. struct vhost_scsi_target backend;
  1810. void __user *argp = (void __user *)arg;
  1811. u64 __user *featurep = argp;
  1812. u32 __user *eventsp = argp;
  1813. u32 events_missed;
  1814. u64 features;
  1815. int r, abi_version = VHOST_SCSI_ABI_VERSION;
  1816. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1817. switch (ioctl) {
  1818. case VHOST_SCSI_SET_ENDPOINT:
  1819. if (copy_from_user(&backend, argp, sizeof backend))
  1820. return -EFAULT;
  1821. if (backend.reserved != 0)
  1822. return -EOPNOTSUPP;
  1823. return vhost_scsi_set_endpoint(vs, &backend);
  1824. case VHOST_SCSI_CLEAR_ENDPOINT:
  1825. if (copy_from_user(&backend, argp, sizeof backend))
  1826. return -EFAULT;
  1827. if (backend.reserved != 0)
  1828. return -EOPNOTSUPP;
  1829. return vhost_scsi_clear_endpoint(vs, &backend);
  1830. case VHOST_SCSI_GET_ABI_VERSION:
  1831. if (copy_to_user(argp, &abi_version, sizeof abi_version))
  1832. return -EFAULT;
  1833. return 0;
  1834. case VHOST_SCSI_SET_EVENTS_MISSED:
  1835. if (get_user(events_missed, eventsp))
  1836. return -EFAULT;
  1837. mutex_lock(&vq->mutex);
  1838. vs->vs_events_missed = events_missed;
  1839. mutex_unlock(&vq->mutex);
  1840. return 0;
  1841. case VHOST_SCSI_GET_EVENTS_MISSED:
  1842. mutex_lock(&vq->mutex);
  1843. events_missed = vs->vs_events_missed;
  1844. mutex_unlock(&vq->mutex);
  1845. if (put_user(events_missed, eventsp))
  1846. return -EFAULT;
  1847. return 0;
  1848. case VHOST_GET_FEATURES:
  1849. features = VHOST_SCSI_FEATURES;
  1850. if (copy_to_user(featurep, &features, sizeof features))
  1851. return -EFAULT;
  1852. return 0;
  1853. case VHOST_SET_FEATURES:
  1854. if (copy_from_user(&features, featurep, sizeof features))
  1855. return -EFAULT;
  1856. return vhost_scsi_set_features(vs, features);
  1857. case VHOST_NEW_WORKER:
  1858. case VHOST_FREE_WORKER:
  1859. case VHOST_ATTACH_VRING_WORKER:
  1860. case VHOST_GET_VRING_WORKER:
  1861. mutex_lock(&vs->dev.mutex);
  1862. r = vhost_worker_ioctl(&vs->dev, ioctl, argp);
  1863. mutex_unlock(&vs->dev.mutex);
  1864. return r;
  1865. default:
  1866. mutex_lock(&vs->dev.mutex);
  1867. r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
  1868. /* TODO: flush backend after dev ioctl. */
  1869. if (r == -ENOIOCTLCMD)
  1870. r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
  1871. mutex_unlock(&vs->dev.mutex);
  1872. return r;
  1873. }
  1874. }
  1875. static const struct file_operations vhost_scsi_fops = {
  1876. .owner = THIS_MODULE,
  1877. .release = vhost_scsi_release,
  1878. .unlocked_ioctl = vhost_scsi_ioctl,
  1879. .compat_ioctl = compat_ptr_ioctl,
  1880. .open = vhost_scsi_open,
  1881. .llseek = noop_llseek,
  1882. };
  1883. static struct miscdevice vhost_scsi_misc = {
  1884. MISC_DYNAMIC_MINOR,
  1885. "vhost-scsi",
  1886. &vhost_scsi_fops,
  1887. };
  1888. static int __init vhost_scsi_register(void)
  1889. {
  1890. return misc_register(&vhost_scsi_misc);
  1891. }
  1892. static void vhost_scsi_deregister(void)
  1893. {
  1894. misc_deregister(&vhost_scsi_misc);
  1895. }
  1896. static char *vhost_scsi_dump_proto_id(struct vhost_scsi_tport *tport)
  1897. {
  1898. switch (tport->tport_proto_id) {
  1899. case SCSI_PROTOCOL_SAS:
  1900. return "SAS";
  1901. case SCSI_PROTOCOL_FCP:
  1902. return "FCP";
  1903. case SCSI_PROTOCOL_ISCSI:
  1904. return "iSCSI";
  1905. default:
  1906. break;
  1907. }
  1908. return "Unknown";
  1909. }
  1910. static void
  1911. vhost_scsi_do_plug(struct vhost_scsi_tpg *tpg,
  1912. struct se_lun *lun, bool plug)
  1913. {
  1914. struct vhost_scsi *vs = tpg->vhost_scsi;
  1915. struct vhost_virtqueue *vq;
  1916. u32 reason;
  1917. if (!vs)
  1918. return;
  1919. if (plug)
  1920. reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
  1921. else
  1922. reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
  1923. vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1924. mutex_lock(&vq->mutex);
  1925. /*
  1926. * We can't queue events if the backend has been cleared, because
  1927. * we could end up queueing an event after the flush.
  1928. */
  1929. if (!vhost_vq_get_backend(vq))
  1930. goto unlock;
  1931. if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG))
  1932. vhost_scsi_send_evt(vs, vq, tpg, lun,
  1933. VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
  1934. unlock:
  1935. mutex_unlock(&vq->mutex);
  1936. }
  1937. static void vhost_scsi_hotplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
  1938. {
  1939. vhost_scsi_do_plug(tpg, lun, true);
  1940. }
  1941. static void vhost_scsi_hotunplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
  1942. {
  1943. vhost_scsi_do_plug(tpg, lun, false);
  1944. }
  1945. static int vhost_scsi_port_link(struct se_portal_group *se_tpg,
  1946. struct se_lun *lun)
  1947. {
  1948. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1949. struct vhost_scsi_tpg, se_tpg);
  1950. mutex_lock(&tpg->tv_tpg_mutex);
  1951. tpg->tv_tpg_port_count++;
  1952. vhost_scsi_hotplug(tpg, lun);
  1953. mutex_unlock(&tpg->tv_tpg_mutex);
  1954. return 0;
  1955. }
  1956. static void vhost_scsi_port_unlink(struct se_portal_group *se_tpg,
  1957. struct se_lun *lun)
  1958. {
  1959. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1960. struct vhost_scsi_tpg, se_tpg);
  1961. mutex_lock(&tpg->tv_tpg_mutex);
  1962. tpg->tv_tpg_port_count--;
  1963. vhost_scsi_hotunplug(tpg, lun);
  1964. mutex_unlock(&tpg->tv_tpg_mutex);
  1965. }
  1966. static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_store(
  1967. struct config_item *item, const char *page, size_t count)
  1968. {
  1969. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  1970. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1971. struct vhost_scsi_tpg, se_tpg);
  1972. unsigned long val;
  1973. int ret = kstrtoul(page, 0, &val);
  1974. if (ret) {
  1975. pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
  1976. return ret;
  1977. }
  1978. if (val != 0 && val != 1 && val != 3) {
  1979. pr_err("Invalid vhost_scsi fabric_prot_type: %lu\n", val);
  1980. return -EINVAL;
  1981. }
  1982. tpg->tv_fabric_prot_type = val;
  1983. return count;
  1984. }
  1985. static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_show(
  1986. struct config_item *item, char *page)
  1987. {
  1988. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  1989. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1990. struct vhost_scsi_tpg, se_tpg);
  1991. return sysfs_emit(page, "%d\n", tpg->tv_fabric_prot_type);
  1992. }
  1993. CONFIGFS_ATTR(vhost_scsi_tpg_attrib_, fabric_prot_type);
  1994. static struct configfs_attribute *vhost_scsi_tpg_attrib_attrs[] = {
  1995. &vhost_scsi_tpg_attrib_attr_fabric_prot_type,
  1996. NULL,
  1997. };
  1998. static int vhost_scsi_make_nexus(struct vhost_scsi_tpg *tpg,
  1999. const char *name)
  2000. {
  2001. struct vhost_scsi_nexus *tv_nexus;
  2002. mutex_lock(&tpg->tv_tpg_mutex);
  2003. if (tpg->tpg_nexus) {
  2004. mutex_unlock(&tpg->tv_tpg_mutex);
  2005. pr_debug("tpg->tpg_nexus already exists\n");
  2006. return -EEXIST;
  2007. }
  2008. tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
  2009. if (!tv_nexus) {
  2010. mutex_unlock(&tpg->tv_tpg_mutex);
  2011. pr_err("Unable to allocate struct vhost_scsi_nexus\n");
  2012. return -ENOMEM;
  2013. }
  2014. /*
  2015. * Since we are running in 'demo mode' this call with generate a
  2016. * struct se_node_acl for the vhost_scsi struct se_portal_group with
  2017. * the SCSI Initiator port name of the passed configfs group 'name'.
  2018. */
  2019. tv_nexus->tvn_se_sess = target_setup_session(&tpg->se_tpg, 0, 0,
  2020. TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS,
  2021. (unsigned char *)name, tv_nexus, NULL);
  2022. if (IS_ERR(tv_nexus->tvn_se_sess)) {
  2023. mutex_unlock(&tpg->tv_tpg_mutex);
  2024. kfree(tv_nexus);
  2025. return -ENOMEM;
  2026. }
  2027. tpg->tpg_nexus = tv_nexus;
  2028. mutex_unlock(&tpg->tv_tpg_mutex);
  2029. return 0;
  2030. }
  2031. static int vhost_scsi_drop_nexus(struct vhost_scsi_tpg *tpg)
  2032. {
  2033. struct se_session *se_sess;
  2034. struct vhost_scsi_nexus *tv_nexus;
  2035. mutex_lock(&tpg->tv_tpg_mutex);
  2036. tv_nexus = tpg->tpg_nexus;
  2037. if (!tv_nexus) {
  2038. mutex_unlock(&tpg->tv_tpg_mutex);
  2039. return -ENODEV;
  2040. }
  2041. se_sess = tv_nexus->tvn_se_sess;
  2042. if (!se_sess) {
  2043. mutex_unlock(&tpg->tv_tpg_mutex);
  2044. return -ENODEV;
  2045. }
  2046. if (tpg->tv_tpg_port_count != 0) {
  2047. mutex_unlock(&tpg->tv_tpg_mutex);
  2048. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  2049. " active TPG port count: %d\n",
  2050. tpg->tv_tpg_port_count);
  2051. return -EBUSY;
  2052. }
  2053. if (tpg->tv_tpg_vhost_count != 0) {
  2054. mutex_unlock(&tpg->tv_tpg_mutex);
  2055. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  2056. " active TPG vhost count: %d\n",
  2057. tpg->tv_tpg_vhost_count);
  2058. return -EBUSY;
  2059. }
  2060. pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
  2061. " %s Initiator Port: %s\n", vhost_scsi_dump_proto_id(tpg->tport),
  2062. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  2063. /*
  2064. * Release the SCSI I_T Nexus to the emulated vhost Target Port
  2065. */
  2066. target_remove_session(se_sess);
  2067. tpg->tpg_nexus = NULL;
  2068. mutex_unlock(&tpg->tv_tpg_mutex);
  2069. kfree(tv_nexus);
  2070. return 0;
  2071. }
  2072. static ssize_t vhost_scsi_tpg_nexus_show(struct config_item *item, char *page)
  2073. {
  2074. struct se_portal_group *se_tpg = to_tpg(item);
  2075. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  2076. struct vhost_scsi_tpg, se_tpg);
  2077. struct vhost_scsi_nexus *tv_nexus;
  2078. ssize_t ret;
  2079. mutex_lock(&tpg->tv_tpg_mutex);
  2080. tv_nexus = tpg->tpg_nexus;
  2081. if (!tv_nexus) {
  2082. mutex_unlock(&tpg->tv_tpg_mutex);
  2083. return -ENODEV;
  2084. }
  2085. ret = sysfs_emit(page, "%s\n",
  2086. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  2087. mutex_unlock(&tpg->tv_tpg_mutex);
  2088. return ret;
  2089. }
  2090. static ssize_t vhost_scsi_tpg_nexus_store(struct config_item *item,
  2091. const char *page, size_t count)
  2092. {
  2093. struct se_portal_group *se_tpg = to_tpg(item);
  2094. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  2095. struct vhost_scsi_tpg, se_tpg);
  2096. struct vhost_scsi_tport *tport_wwn = tpg->tport;
  2097. unsigned char i_port[VHOST_SCSI_NAMELEN], *ptr, *port_ptr;
  2098. int ret;
  2099. /*
  2100. * Shutdown the active I_T nexus if 'NULL' is passed..
  2101. */
  2102. if (!strncmp(page, "NULL", 4)) {
  2103. ret = vhost_scsi_drop_nexus(tpg);
  2104. return (!ret) ? count : ret;
  2105. }
  2106. /*
  2107. * Otherwise make sure the passed virtual Initiator port WWN matches
  2108. * the fabric protocol_id set in vhost_scsi_make_tport(), and call
  2109. * vhost_scsi_make_nexus().
  2110. */
  2111. if (strlen(page) >= VHOST_SCSI_NAMELEN) {
  2112. pr_err("Emulated NAA Sas Address: %s, exceeds"
  2113. " max: %d\n", page, VHOST_SCSI_NAMELEN);
  2114. return -EINVAL;
  2115. }
  2116. snprintf(&i_port[0], VHOST_SCSI_NAMELEN, "%s", page);
  2117. ptr = strstr(i_port, "naa.");
  2118. if (ptr) {
  2119. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
  2120. pr_err("Passed SAS Initiator Port %s does not"
  2121. " match target port protoid: %s\n", i_port,
  2122. vhost_scsi_dump_proto_id(tport_wwn));
  2123. return -EINVAL;
  2124. }
  2125. port_ptr = &i_port[0];
  2126. goto check_newline;
  2127. }
  2128. ptr = strstr(i_port, "fc.");
  2129. if (ptr) {
  2130. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
  2131. pr_err("Passed FCP Initiator Port %s does not"
  2132. " match target port protoid: %s\n", i_port,
  2133. vhost_scsi_dump_proto_id(tport_wwn));
  2134. return -EINVAL;
  2135. }
  2136. port_ptr = &i_port[3]; /* Skip over "fc." */
  2137. goto check_newline;
  2138. }
  2139. ptr = strstr(i_port, "iqn.");
  2140. if (ptr) {
  2141. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
  2142. pr_err("Passed iSCSI Initiator Port %s does not"
  2143. " match target port protoid: %s\n", i_port,
  2144. vhost_scsi_dump_proto_id(tport_wwn));
  2145. return -EINVAL;
  2146. }
  2147. port_ptr = &i_port[0];
  2148. goto check_newline;
  2149. }
  2150. pr_err("Unable to locate prefix for emulated Initiator Port:"
  2151. " %s\n", i_port);
  2152. return -EINVAL;
  2153. /*
  2154. * Clear any trailing newline for the NAA WWN
  2155. */
  2156. check_newline:
  2157. if (i_port[strlen(i_port)-1] == '\n')
  2158. i_port[strlen(i_port)-1] = '\0';
  2159. ret = vhost_scsi_make_nexus(tpg, port_ptr);
  2160. if (ret < 0)
  2161. return ret;
  2162. return count;
  2163. }
  2164. CONFIGFS_ATTR(vhost_scsi_tpg_, nexus);
  2165. static struct configfs_attribute *vhost_scsi_tpg_attrs[] = {
  2166. &vhost_scsi_tpg_attr_nexus,
  2167. NULL,
  2168. };
  2169. static struct se_portal_group *
  2170. vhost_scsi_make_tpg(struct se_wwn *wwn, const char *name)
  2171. {
  2172. struct vhost_scsi_tport *tport = container_of(wwn,
  2173. struct vhost_scsi_tport, tport_wwn);
  2174. struct vhost_scsi_tpg *tpg;
  2175. u16 tpgt;
  2176. int ret;
  2177. if (strstr(name, "tpgt_") != name)
  2178. return ERR_PTR(-EINVAL);
  2179. if (kstrtou16(name + 5, 10, &tpgt) || tpgt >= VHOST_SCSI_MAX_TARGET)
  2180. return ERR_PTR(-EINVAL);
  2181. tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
  2182. if (!tpg) {
  2183. pr_err("Unable to allocate struct vhost_scsi_tpg");
  2184. return ERR_PTR(-ENOMEM);
  2185. }
  2186. mutex_init(&tpg->tv_tpg_mutex);
  2187. INIT_LIST_HEAD(&tpg->tv_tpg_list);
  2188. tpg->tport = tport;
  2189. tpg->tport_tpgt = tpgt;
  2190. ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
  2191. if (ret < 0) {
  2192. kfree(tpg);
  2193. return NULL;
  2194. }
  2195. mutex_lock(&vhost_scsi_mutex);
  2196. list_add_tail(&tpg->tv_tpg_list, &vhost_scsi_list);
  2197. mutex_unlock(&vhost_scsi_mutex);
  2198. return &tpg->se_tpg;
  2199. }
  2200. static void vhost_scsi_drop_tpg(struct se_portal_group *se_tpg)
  2201. {
  2202. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  2203. struct vhost_scsi_tpg, se_tpg);
  2204. mutex_lock(&vhost_scsi_mutex);
  2205. list_del(&tpg->tv_tpg_list);
  2206. mutex_unlock(&vhost_scsi_mutex);
  2207. /*
  2208. * Release the virtual I_T Nexus for this vhost TPG
  2209. */
  2210. vhost_scsi_drop_nexus(tpg);
  2211. /*
  2212. * Deregister the se_tpg from TCM..
  2213. */
  2214. core_tpg_deregister(se_tpg);
  2215. kfree(tpg);
  2216. }
  2217. static struct se_wwn *
  2218. vhost_scsi_make_tport(struct target_fabric_configfs *tf,
  2219. struct config_group *group,
  2220. const char *name)
  2221. {
  2222. struct vhost_scsi_tport *tport;
  2223. char *ptr;
  2224. u64 wwpn = 0;
  2225. int off = 0;
  2226. /* if (vhost_scsi_parse_wwn(name, &wwpn, 1) < 0)
  2227. return ERR_PTR(-EINVAL); */
  2228. tport = kzalloc(sizeof(*tport), GFP_KERNEL);
  2229. if (!tport) {
  2230. pr_err("Unable to allocate struct vhost_scsi_tport");
  2231. return ERR_PTR(-ENOMEM);
  2232. }
  2233. tport->tport_wwpn = wwpn;
  2234. /*
  2235. * Determine the emulated Protocol Identifier and Target Port Name
  2236. * based on the incoming configfs directory name.
  2237. */
  2238. ptr = strstr(name, "naa.");
  2239. if (ptr) {
  2240. tport->tport_proto_id = SCSI_PROTOCOL_SAS;
  2241. goto check_len;
  2242. }
  2243. ptr = strstr(name, "fc.");
  2244. if (ptr) {
  2245. tport->tport_proto_id = SCSI_PROTOCOL_FCP;
  2246. off = 3; /* Skip over "fc." */
  2247. goto check_len;
  2248. }
  2249. ptr = strstr(name, "iqn.");
  2250. if (ptr) {
  2251. tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
  2252. goto check_len;
  2253. }
  2254. pr_err("Unable to locate prefix for emulated Target Port:"
  2255. " %s\n", name);
  2256. kfree(tport);
  2257. return ERR_PTR(-EINVAL);
  2258. check_len:
  2259. if (strlen(name) >= VHOST_SCSI_NAMELEN) {
  2260. pr_err("Emulated %s Address: %s, exceeds"
  2261. " max: %d\n", name, vhost_scsi_dump_proto_id(tport),
  2262. VHOST_SCSI_NAMELEN);
  2263. kfree(tport);
  2264. return ERR_PTR(-EINVAL);
  2265. }
  2266. snprintf(&tport->tport_name[0], VHOST_SCSI_NAMELEN, "%s", &name[off]);
  2267. pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
  2268. " %s Address: %s\n", vhost_scsi_dump_proto_id(tport), name);
  2269. return &tport->tport_wwn;
  2270. }
  2271. static void vhost_scsi_drop_tport(struct se_wwn *wwn)
  2272. {
  2273. struct vhost_scsi_tport *tport = container_of(wwn,
  2274. struct vhost_scsi_tport, tport_wwn);
  2275. pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
  2276. " %s Address: %s\n", vhost_scsi_dump_proto_id(tport),
  2277. tport->tport_name);
  2278. kfree(tport);
  2279. }
  2280. static ssize_t
  2281. vhost_scsi_wwn_version_show(struct config_item *item, char *page)
  2282. {
  2283. return sysfs_emit(page, "TCM_VHOST fabric module %s on %s/%s"
  2284. "on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
  2285. utsname()->machine);
  2286. }
  2287. CONFIGFS_ATTR_RO(vhost_scsi_wwn_, version);
  2288. static struct configfs_attribute *vhost_scsi_wwn_attrs[] = {
  2289. &vhost_scsi_wwn_attr_version,
  2290. NULL,
  2291. };
  2292. static const struct target_core_fabric_ops vhost_scsi_ops = {
  2293. .module = THIS_MODULE,
  2294. .fabric_name = "vhost",
  2295. .max_data_sg_nents = VHOST_SCSI_PREALLOC_SGLS,
  2296. .tpg_get_wwn = vhost_scsi_get_fabric_wwn,
  2297. .tpg_get_tag = vhost_scsi_get_tpgt,
  2298. .tpg_check_demo_mode = vhost_scsi_check_true,
  2299. .tpg_check_demo_mode_cache = vhost_scsi_check_true,
  2300. .tpg_check_prot_fabric_only = vhost_scsi_check_prot_fabric_only,
  2301. .release_cmd = vhost_scsi_release_cmd,
  2302. .check_stop_free = vhost_scsi_check_stop_free,
  2303. .sess_get_initiator_sid = NULL,
  2304. .write_pending = vhost_scsi_write_pending,
  2305. .queue_data_in = vhost_scsi_queue_data_in,
  2306. .queue_status = vhost_scsi_queue_status,
  2307. .queue_tm_rsp = vhost_scsi_queue_tm_rsp,
  2308. .aborted_task = vhost_scsi_aborted_task,
  2309. /*
  2310. * Setup callers for generic logic in target_core_fabric_configfs.c
  2311. */
  2312. .fabric_make_wwn = vhost_scsi_make_tport,
  2313. .fabric_drop_wwn = vhost_scsi_drop_tport,
  2314. .fabric_make_tpg = vhost_scsi_make_tpg,
  2315. .fabric_drop_tpg = vhost_scsi_drop_tpg,
  2316. .fabric_post_link = vhost_scsi_port_link,
  2317. .fabric_pre_unlink = vhost_scsi_port_unlink,
  2318. .tfc_wwn_attrs = vhost_scsi_wwn_attrs,
  2319. .tfc_tpg_base_attrs = vhost_scsi_tpg_attrs,
  2320. .tfc_tpg_attrib_attrs = vhost_scsi_tpg_attrib_attrs,
  2321. .default_submit_type = TARGET_QUEUE_SUBMIT,
  2322. .direct_submit_supp = 1,
  2323. };
  2324. static int __init vhost_scsi_init(void)
  2325. {
  2326. int ret = -ENOMEM;
  2327. pr_debug("TCM_VHOST fabric module %s on %s/%s"
  2328. " on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
  2329. utsname()->machine);
  2330. ret = vhost_scsi_register();
  2331. if (ret < 0)
  2332. goto out;
  2333. ret = target_register_template(&vhost_scsi_ops);
  2334. if (ret < 0)
  2335. goto out_vhost_scsi_deregister;
  2336. return 0;
  2337. out_vhost_scsi_deregister:
  2338. vhost_scsi_deregister();
  2339. out:
  2340. return ret;
  2341. };
  2342. static void vhost_scsi_exit(void)
  2343. {
  2344. target_unregister_template(&vhost_scsi_ops);
  2345. vhost_scsi_deregister();
  2346. };
  2347. MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
  2348. MODULE_ALIAS("tcm_vhost");
  2349. MODULE_LICENSE("GPL");
  2350. module_init(vhost_scsi_init);
  2351. module_exit(vhost_scsi_exit);