scsi.c 57 KB

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