scsi.c 57 KB

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