scsi.c 62 KB

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