scsi.c 62 KB

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