scsi.c 57 KB

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