scsi.c 61 KB

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