scsi.c 66 KB

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