scsi.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409
  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 int vhost_scsi_to_tcm_attr(int attr)
  786. {
  787. switch (attr) {
  788. case VIRTIO_SCSI_S_SIMPLE:
  789. return MSG_SIMPLE_TAG;
  790. case VIRTIO_SCSI_S_ORDERED:
  791. return MSG_ORDERED_TAG;
  792. case VIRTIO_SCSI_S_HEAD:
  793. return MSG_HEAD_TAG;
  794. case VIRTIO_SCSI_S_ACA:
  795. return MSG_ACA_TAG;
  796. default:
  797. break;
  798. }
  799. return MSG_SIMPLE_TAG;
  800. }
  801. static void tcm_vhost_submission_work(struct work_struct *work)
  802. {
  803. struct tcm_vhost_cmd *cmd =
  804. container_of(work, struct tcm_vhost_cmd, work);
  805. struct tcm_vhost_nexus *tv_nexus;
  806. struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
  807. struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
  808. int rc;
  809. /* FIXME: BIDI operation */
  810. if (cmd->tvc_sgl_count) {
  811. sg_ptr = cmd->tvc_sgl;
  812. if (cmd->tvc_prot_sgl_count)
  813. sg_prot_ptr = cmd->tvc_prot_sgl;
  814. else
  815. se_cmd->prot_pto = true;
  816. } else {
  817. sg_ptr = NULL;
  818. }
  819. tv_nexus = cmd->tvc_nexus;
  820. rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
  821. cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
  822. cmd->tvc_lun, cmd->tvc_exp_data_len,
  823. vhost_scsi_to_tcm_attr(cmd->tvc_task_attr),
  824. cmd->tvc_data_direction, TARGET_SCF_ACK_KREF,
  825. sg_ptr, cmd->tvc_sgl_count, NULL, 0,
  826. sg_prot_ptr, cmd->tvc_prot_sgl_count);
  827. if (rc < 0) {
  828. transport_send_check_condition_and_sense(se_cmd,
  829. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  830. transport_generic_free_cmd(se_cmd, 0);
  831. }
  832. }
  833. static void
  834. vhost_scsi_send_bad_target(struct vhost_scsi *vs,
  835. struct vhost_virtqueue *vq,
  836. int head, unsigned out)
  837. {
  838. struct virtio_scsi_cmd_resp __user *resp;
  839. struct virtio_scsi_cmd_resp rsp;
  840. int ret;
  841. memset(&rsp, 0, sizeof(rsp));
  842. rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
  843. resp = vq->iov[out].iov_base;
  844. ret = __copy_to_user(resp, &rsp, sizeof(rsp));
  845. if (!ret)
  846. vhost_add_used_and_signal(&vs->dev, vq, head, 0);
  847. else
  848. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  849. }
  850. static void
  851. vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
  852. {
  853. struct tcm_vhost_tpg **vs_tpg;
  854. struct virtio_scsi_cmd_req v_req;
  855. struct virtio_scsi_cmd_req_pi v_req_pi;
  856. struct tcm_vhost_tpg *tpg;
  857. struct tcm_vhost_cmd *cmd;
  858. u64 tag;
  859. u32 exp_data_len, data_first, data_num, data_direction, prot_first;
  860. unsigned out, in, i;
  861. int head, ret, data_niov, prot_niov, prot_bytes;
  862. size_t req_size;
  863. u16 lun;
  864. u8 *target, *lunp, task_attr;
  865. bool hdr_pi;
  866. void *req, *cdb;
  867. mutex_lock(&vq->mutex);
  868. /*
  869. * We can handle the vq only after the endpoint is setup by calling the
  870. * VHOST_SCSI_SET_ENDPOINT ioctl.
  871. */
  872. vs_tpg = vq->private_data;
  873. if (!vs_tpg)
  874. goto out;
  875. vhost_disable_notify(&vs->dev, vq);
  876. for (;;) {
  877. head = vhost_get_vq_desc(vq, vq->iov,
  878. ARRAY_SIZE(vq->iov), &out, &in,
  879. NULL, NULL);
  880. pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
  881. head, out, in);
  882. /* On error, stop handling until the next kick. */
  883. if (unlikely(head < 0))
  884. break;
  885. /* Nothing new? Wait for eventfd to tell us they refilled. */
  886. if (head == vq->num) {
  887. if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
  888. vhost_disable_notify(&vs->dev, vq);
  889. continue;
  890. }
  891. break;
  892. }
  893. /* FIXME: BIDI operation */
  894. if (out == 1 && in == 1) {
  895. data_direction = DMA_NONE;
  896. data_first = 0;
  897. data_num = 0;
  898. } else if (out == 1 && in > 1) {
  899. data_direction = DMA_FROM_DEVICE;
  900. data_first = out + 1;
  901. data_num = in - 1;
  902. } else if (out > 1 && in == 1) {
  903. data_direction = DMA_TO_DEVICE;
  904. data_first = 1;
  905. data_num = out - 1;
  906. } else {
  907. vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
  908. out, in);
  909. break;
  910. }
  911. /*
  912. * Check for a sane resp buffer so we can report errors to
  913. * the guest.
  914. */
  915. if (unlikely(vq->iov[out].iov_len !=
  916. sizeof(struct virtio_scsi_cmd_resp))) {
  917. vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
  918. " bytes\n", vq->iov[out].iov_len);
  919. break;
  920. }
  921. if (vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI)) {
  922. req = &v_req_pi;
  923. lunp = &v_req_pi.lun[0];
  924. target = &v_req_pi.lun[1];
  925. req_size = sizeof(v_req_pi);
  926. hdr_pi = true;
  927. } else {
  928. req = &v_req;
  929. lunp = &v_req.lun[0];
  930. target = &v_req.lun[1];
  931. req_size = sizeof(v_req);
  932. hdr_pi = false;
  933. }
  934. if (unlikely(vq->iov[0].iov_len < req_size)) {
  935. pr_err("Expecting virtio-scsi header: %zu, got %zu\n",
  936. req_size, vq->iov[0].iov_len);
  937. break;
  938. }
  939. ret = memcpy_fromiovecend(req, &vq->iov[0], 0, req_size);
  940. if (unlikely(ret)) {
  941. vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
  942. break;
  943. }
  944. /* virtio-scsi spec requires byte 0 of the lun to be 1 */
  945. if (unlikely(*lunp != 1)) {
  946. vhost_scsi_send_bad_target(vs, vq, head, out);
  947. continue;
  948. }
  949. tpg = ACCESS_ONCE(vs_tpg[*target]);
  950. /* Target does not exist, fail the request */
  951. if (unlikely(!tpg)) {
  952. vhost_scsi_send_bad_target(vs, vq, head, out);
  953. continue;
  954. }
  955. data_niov = data_num;
  956. prot_niov = prot_first = prot_bytes = 0;
  957. /*
  958. * Determine if any protection information iovecs are preceeding
  959. * the actual data payload, and adjust data_first + data_niov
  960. * values accordingly for vhost_scsi_map_iov_to_sgl() below.
  961. *
  962. * Also extract virtio_scsi header bits for vhost_scsi_get_tag()
  963. */
  964. if (hdr_pi) {
  965. if (v_req_pi.pi_bytesout) {
  966. if (data_direction != DMA_TO_DEVICE) {
  967. vq_err(vq, "Received non zero do_pi_niov"
  968. ", but wrong data_direction\n");
  969. goto err_cmd;
  970. }
  971. prot_bytes = v_req_pi.pi_bytesout;
  972. } else if (v_req_pi.pi_bytesin) {
  973. if (data_direction != DMA_FROM_DEVICE) {
  974. vq_err(vq, "Received non zero di_pi_niov"
  975. ", but wrong data_direction\n");
  976. goto err_cmd;
  977. }
  978. prot_bytes = v_req_pi.pi_bytesin;
  979. }
  980. if (prot_bytes) {
  981. int tmp = 0;
  982. for (i = 0; i < data_num; i++) {
  983. tmp += vq->iov[data_first + i].iov_len;
  984. prot_niov++;
  985. if (tmp >= prot_bytes)
  986. break;
  987. }
  988. prot_first = data_first;
  989. data_first += prot_niov;
  990. data_niov = data_num - prot_niov;
  991. }
  992. tag = v_req_pi.tag;
  993. task_attr = v_req_pi.task_attr;
  994. cdb = &v_req_pi.cdb[0];
  995. lun = ((v_req_pi.lun[2] << 8) | v_req_pi.lun[3]) & 0x3FFF;
  996. } else {
  997. tag = v_req.tag;
  998. task_attr = v_req.task_attr;
  999. cdb = &v_req.cdb[0];
  1000. lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
  1001. }
  1002. exp_data_len = 0;
  1003. for (i = 0; i < data_niov; i++)
  1004. exp_data_len += vq->iov[data_first + i].iov_len;
  1005. /*
  1006. * Check that the recieved CDB size does not exceeded our
  1007. * hardcoded max for vhost-scsi
  1008. *
  1009. * TODO what if cdb was too small for varlen cdb header?
  1010. */
  1011. if (unlikely(scsi_command_size(cdb) > TCM_VHOST_MAX_CDB_SIZE)) {
  1012. vq_err(vq, "Received SCSI CDB with command_size: %d that"
  1013. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1014. scsi_command_size(cdb), TCM_VHOST_MAX_CDB_SIZE);
  1015. goto err_cmd;
  1016. }
  1017. cmd = vhost_scsi_get_tag(vq, tpg, cdb, tag, lun, task_attr,
  1018. exp_data_len + prot_bytes,
  1019. data_direction);
  1020. if (IS_ERR(cmd)) {
  1021. vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
  1022. PTR_ERR(cmd));
  1023. goto err_cmd;
  1024. }
  1025. pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
  1026. ": %d\n", cmd, exp_data_len, data_direction);
  1027. cmd->tvc_vhost = vs;
  1028. cmd->tvc_vq = vq;
  1029. cmd->tvc_resp = vq->iov[out].iov_base;
  1030. pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
  1031. cmd->tvc_cdb[0], cmd->tvc_lun);
  1032. if (prot_niov) {
  1033. ret = vhost_scsi_map_iov_to_prot(cmd,
  1034. &vq->iov[prot_first], prot_niov,
  1035. data_direction == DMA_FROM_DEVICE);
  1036. if (unlikely(ret)) {
  1037. vq_err(vq, "Failed to map iov to"
  1038. " prot_sgl\n");
  1039. goto err_free;
  1040. }
  1041. }
  1042. if (data_direction != DMA_NONE) {
  1043. ret = vhost_scsi_map_iov_to_sgl(cmd,
  1044. &vq->iov[data_first], data_niov,
  1045. data_direction == DMA_FROM_DEVICE);
  1046. if (unlikely(ret)) {
  1047. vq_err(vq, "Failed to map iov to sgl\n");
  1048. goto err_free;
  1049. }
  1050. }
  1051. /*
  1052. * Save the descriptor from vhost_get_vq_desc() to be used to
  1053. * complete the virtio-scsi request in TCM callback context via
  1054. * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
  1055. */
  1056. cmd->tvc_vq_desc = head;
  1057. /*
  1058. * Dispatch tv_cmd descriptor for cmwq execution in process
  1059. * context provided by tcm_vhost_workqueue. This also ensures
  1060. * tv_cmd is executed on the same kworker CPU as this vhost
  1061. * thread to gain positive L2 cache locality effects..
  1062. */
  1063. INIT_WORK(&cmd->work, tcm_vhost_submission_work);
  1064. queue_work(tcm_vhost_workqueue, &cmd->work);
  1065. }
  1066. mutex_unlock(&vq->mutex);
  1067. return;
  1068. err_free:
  1069. vhost_scsi_free_cmd(cmd);
  1070. err_cmd:
  1071. vhost_scsi_send_bad_target(vs, vq, head, out);
  1072. out:
  1073. mutex_unlock(&vq->mutex);
  1074. }
  1075. static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
  1076. {
  1077. pr_debug("%s: The handling func for control queue.\n", __func__);
  1078. }
  1079. static void
  1080. tcm_vhost_send_evt(struct vhost_scsi *vs,
  1081. struct tcm_vhost_tpg *tpg,
  1082. struct se_lun *lun,
  1083. u32 event,
  1084. u32 reason)
  1085. {
  1086. struct tcm_vhost_evt *evt;
  1087. evt = tcm_vhost_allocate_evt(vs, event, reason);
  1088. if (!evt)
  1089. return;
  1090. if (tpg && lun) {
  1091. /* TODO: share lun setup code with virtio-scsi.ko */
  1092. /*
  1093. * Note: evt->event is zeroed when we allocate it and
  1094. * lun[4-7] need to be zero according to virtio-scsi spec.
  1095. */
  1096. evt->event.lun[0] = 0x01;
  1097. evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
  1098. if (lun->unpacked_lun >= 256)
  1099. evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
  1100. evt->event.lun[3] = lun->unpacked_lun & 0xFF;
  1101. }
  1102. llist_add(&evt->list, &vs->vs_event_list);
  1103. vhost_work_queue(&vs->dev, &vs->vs_event_work);
  1104. }
  1105. static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
  1106. {
  1107. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1108. poll.work);
  1109. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  1110. mutex_lock(&vq->mutex);
  1111. if (!vq->private_data)
  1112. goto out;
  1113. if (vs->vs_events_missed)
  1114. tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
  1115. out:
  1116. mutex_unlock(&vq->mutex);
  1117. }
  1118. static void vhost_scsi_handle_kick(struct vhost_work *work)
  1119. {
  1120. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1121. poll.work);
  1122. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  1123. vhost_scsi_handle_vq(vs, vq);
  1124. }
  1125. static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
  1126. {
  1127. vhost_poll_flush(&vs->vqs[index].vq.poll);
  1128. }
  1129. /* Callers must hold dev mutex */
  1130. static void vhost_scsi_flush(struct vhost_scsi *vs)
  1131. {
  1132. struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
  1133. int i;
  1134. /* Init new inflight and remember the old inflight */
  1135. tcm_vhost_init_inflight(vs, old_inflight);
  1136. /*
  1137. * The inflight->kref was initialized to 1. We decrement it here to
  1138. * indicate the start of the flush operation so that it will reach 0
  1139. * when all the reqs are finished.
  1140. */
  1141. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1142. kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight);
  1143. /* Flush both the vhost poll and vhost work */
  1144. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1145. vhost_scsi_flush_vq(vs, i);
  1146. vhost_work_flush(&vs->dev, &vs->vs_completion_work);
  1147. vhost_work_flush(&vs->dev, &vs->vs_event_work);
  1148. /* Wait for all reqs issued before the flush to be finished */
  1149. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1150. wait_for_completion(&old_inflight[i]->comp);
  1151. }
  1152. /*
  1153. * Called from vhost_scsi_ioctl() context to walk the list of available
  1154. * tcm_vhost_tpg with an active struct tcm_vhost_nexus
  1155. *
  1156. * The lock nesting rule is:
  1157. * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
  1158. */
  1159. static int
  1160. vhost_scsi_set_endpoint(struct vhost_scsi *vs,
  1161. struct vhost_scsi_target *t)
  1162. {
  1163. struct se_portal_group *se_tpg;
  1164. struct tcm_vhost_tport *tv_tport;
  1165. struct tcm_vhost_tpg *tpg;
  1166. struct tcm_vhost_tpg **vs_tpg;
  1167. struct vhost_virtqueue *vq;
  1168. int index, ret, i, len;
  1169. bool match = false;
  1170. mutex_lock(&tcm_vhost_mutex);
  1171. mutex_lock(&vs->dev.mutex);
  1172. /* Verify that ring has been setup correctly. */
  1173. for (index = 0; index < vs->dev.nvqs; ++index) {
  1174. /* Verify that ring has been setup correctly. */
  1175. if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
  1176. ret = -EFAULT;
  1177. goto out;
  1178. }
  1179. }
  1180. len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
  1181. vs_tpg = kzalloc(len, GFP_KERNEL);
  1182. if (!vs_tpg) {
  1183. ret = -ENOMEM;
  1184. goto out;
  1185. }
  1186. if (vs->vs_tpg)
  1187. memcpy(vs_tpg, vs->vs_tpg, len);
  1188. list_for_each_entry(tpg, &tcm_vhost_list, tv_tpg_list) {
  1189. mutex_lock(&tpg->tv_tpg_mutex);
  1190. if (!tpg->tpg_nexus) {
  1191. mutex_unlock(&tpg->tv_tpg_mutex);
  1192. continue;
  1193. }
  1194. if (tpg->tv_tpg_vhost_count != 0) {
  1195. mutex_unlock(&tpg->tv_tpg_mutex);
  1196. continue;
  1197. }
  1198. tv_tport = tpg->tport;
  1199. if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  1200. if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
  1201. kfree(vs_tpg);
  1202. mutex_unlock(&tpg->tv_tpg_mutex);
  1203. ret = -EEXIST;
  1204. goto out;
  1205. }
  1206. /*
  1207. * In order to ensure individual vhost-scsi configfs
  1208. * groups cannot be removed while in use by vhost ioctl,
  1209. * go ahead and take an explicit se_tpg->tpg_group.cg_item
  1210. * dependency now.
  1211. */
  1212. se_tpg = &tpg->se_tpg;
  1213. ret = configfs_depend_item(se_tpg->se_tpg_tfo->tf_subsys,
  1214. &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(&tcm_vhost_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 tcm_vhost_tport *tv_tport;
  1261. struct tcm_vhost_tpg *tpg;
  1262. struct vhost_virtqueue *vq;
  1263. bool match = false;
  1264. int index, ret, i;
  1265. u8 target;
  1266. mutex_lock(&tcm_vhost_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. configfs_undepend_item(se_tpg->se_tpg_tfo->tf_subsys,
  1309. &se_tpg->tpg_group.cg_item);
  1310. }
  1311. if (match) {
  1312. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1313. vq = &vs->vqs[i].vq;
  1314. mutex_lock(&vq->mutex);
  1315. vq->private_data = NULL;
  1316. mutex_unlock(&vq->mutex);
  1317. }
  1318. }
  1319. /*
  1320. * Act as synchronize_rcu to make sure access to
  1321. * old vs->vs_tpg is finished.
  1322. */
  1323. vhost_scsi_flush(vs);
  1324. kfree(vs->vs_tpg);
  1325. vs->vs_tpg = NULL;
  1326. WARN_ON(vs->vs_events_nr);
  1327. mutex_unlock(&vs->dev.mutex);
  1328. mutex_unlock(&tcm_vhost_mutex);
  1329. return 0;
  1330. err_tpg:
  1331. mutex_unlock(&tpg->tv_tpg_mutex);
  1332. err_dev:
  1333. mutex_unlock(&vs->dev.mutex);
  1334. mutex_unlock(&tcm_vhost_mutex);
  1335. return ret;
  1336. }
  1337. static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
  1338. {
  1339. struct vhost_virtqueue *vq;
  1340. int i;
  1341. if (features & ~VHOST_SCSI_FEATURES)
  1342. return -EOPNOTSUPP;
  1343. mutex_lock(&vs->dev.mutex);
  1344. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  1345. !vhost_log_access_ok(&vs->dev)) {
  1346. mutex_unlock(&vs->dev.mutex);
  1347. return -EFAULT;
  1348. }
  1349. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1350. vq = &vs->vqs[i].vq;
  1351. mutex_lock(&vq->mutex);
  1352. vq->acked_features = features;
  1353. mutex_unlock(&vq->mutex);
  1354. }
  1355. mutex_unlock(&vs->dev.mutex);
  1356. return 0;
  1357. }
  1358. static int vhost_scsi_open(struct inode *inode, struct file *f)
  1359. {
  1360. struct vhost_scsi *vs;
  1361. struct vhost_virtqueue **vqs;
  1362. int r = -ENOMEM, i;
  1363. vs = kzalloc(sizeof(*vs), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  1364. if (!vs) {
  1365. vs = vzalloc(sizeof(*vs));
  1366. if (!vs)
  1367. goto err_vs;
  1368. }
  1369. vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
  1370. if (!vqs)
  1371. goto err_vqs;
  1372. vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
  1373. vhost_work_init(&vs->vs_event_work, tcm_vhost_evt_work);
  1374. vs->vs_events_nr = 0;
  1375. vs->vs_events_missed = false;
  1376. vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
  1377. vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1378. vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
  1379. vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
  1380. for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
  1381. vqs[i] = &vs->vqs[i].vq;
  1382. vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
  1383. }
  1384. vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
  1385. tcm_vhost_init_inflight(vs, NULL);
  1386. f->private_data = vs;
  1387. return 0;
  1388. err_vqs:
  1389. kvfree(vs);
  1390. err_vs:
  1391. return r;
  1392. }
  1393. static int vhost_scsi_release(struct inode *inode, struct file *f)
  1394. {
  1395. struct vhost_scsi *vs = f->private_data;
  1396. struct vhost_scsi_target t;
  1397. mutex_lock(&vs->dev.mutex);
  1398. memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
  1399. mutex_unlock(&vs->dev.mutex);
  1400. vhost_scsi_clear_endpoint(vs, &t);
  1401. vhost_dev_stop(&vs->dev);
  1402. vhost_dev_cleanup(&vs->dev, false);
  1403. /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
  1404. vhost_scsi_flush(vs);
  1405. kfree(vs->dev.vqs);
  1406. kvfree(vs);
  1407. return 0;
  1408. }
  1409. static long
  1410. vhost_scsi_ioctl(struct file *f,
  1411. unsigned int ioctl,
  1412. unsigned long arg)
  1413. {
  1414. struct vhost_scsi *vs = f->private_data;
  1415. struct vhost_scsi_target backend;
  1416. void __user *argp = (void __user *)arg;
  1417. u64 __user *featurep = argp;
  1418. u32 __user *eventsp = argp;
  1419. u32 events_missed;
  1420. u64 features;
  1421. int r, abi_version = VHOST_SCSI_ABI_VERSION;
  1422. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1423. switch (ioctl) {
  1424. case VHOST_SCSI_SET_ENDPOINT:
  1425. if (copy_from_user(&backend, argp, sizeof backend))
  1426. return -EFAULT;
  1427. if (backend.reserved != 0)
  1428. return -EOPNOTSUPP;
  1429. return vhost_scsi_set_endpoint(vs, &backend);
  1430. case VHOST_SCSI_CLEAR_ENDPOINT:
  1431. if (copy_from_user(&backend, argp, sizeof backend))
  1432. return -EFAULT;
  1433. if (backend.reserved != 0)
  1434. return -EOPNOTSUPP;
  1435. return vhost_scsi_clear_endpoint(vs, &backend);
  1436. case VHOST_SCSI_GET_ABI_VERSION:
  1437. if (copy_to_user(argp, &abi_version, sizeof abi_version))
  1438. return -EFAULT;
  1439. return 0;
  1440. case VHOST_SCSI_SET_EVENTS_MISSED:
  1441. if (get_user(events_missed, eventsp))
  1442. return -EFAULT;
  1443. mutex_lock(&vq->mutex);
  1444. vs->vs_events_missed = events_missed;
  1445. mutex_unlock(&vq->mutex);
  1446. return 0;
  1447. case VHOST_SCSI_GET_EVENTS_MISSED:
  1448. mutex_lock(&vq->mutex);
  1449. events_missed = vs->vs_events_missed;
  1450. mutex_unlock(&vq->mutex);
  1451. if (put_user(events_missed, eventsp))
  1452. return -EFAULT;
  1453. return 0;
  1454. case VHOST_GET_FEATURES:
  1455. features = VHOST_SCSI_FEATURES;
  1456. if (copy_to_user(featurep, &features, sizeof features))
  1457. return -EFAULT;
  1458. return 0;
  1459. case VHOST_SET_FEATURES:
  1460. if (copy_from_user(&features, featurep, sizeof features))
  1461. return -EFAULT;
  1462. return vhost_scsi_set_features(vs, features);
  1463. default:
  1464. mutex_lock(&vs->dev.mutex);
  1465. r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
  1466. /* TODO: flush backend after dev ioctl. */
  1467. if (r == -ENOIOCTLCMD)
  1468. r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
  1469. mutex_unlock(&vs->dev.mutex);
  1470. return r;
  1471. }
  1472. }
  1473. #ifdef CONFIG_COMPAT
  1474. static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
  1475. unsigned long arg)
  1476. {
  1477. return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  1478. }
  1479. #endif
  1480. static const struct file_operations vhost_scsi_fops = {
  1481. .owner = THIS_MODULE,
  1482. .release = vhost_scsi_release,
  1483. .unlocked_ioctl = vhost_scsi_ioctl,
  1484. #ifdef CONFIG_COMPAT
  1485. .compat_ioctl = vhost_scsi_compat_ioctl,
  1486. #endif
  1487. .open = vhost_scsi_open,
  1488. .llseek = noop_llseek,
  1489. };
  1490. static struct miscdevice vhost_scsi_misc = {
  1491. MISC_DYNAMIC_MINOR,
  1492. "vhost-scsi",
  1493. &vhost_scsi_fops,
  1494. };
  1495. static int __init vhost_scsi_register(void)
  1496. {
  1497. return misc_register(&vhost_scsi_misc);
  1498. }
  1499. static int vhost_scsi_deregister(void)
  1500. {
  1501. return misc_deregister(&vhost_scsi_misc);
  1502. }
  1503. static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
  1504. {
  1505. switch (tport->tport_proto_id) {
  1506. case SCSI_PROTOCOL_SAS:
  1507. return "SAS";
  1508. case SCSI_PROTOCOL_FCP:
  1509. return "FCP";
  1510. case SCSI_PROTOCOL_ISCSI:
  1511. return "iSCSI";
  1512. default:
  1513. break;
  1514. }
  1515. return "Unknown";
  1516. }
  1517. static void
  1518. tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
  1519. struct se_lun *lun, bool plug)
  1520. {
  1521. struct vhost_scsi *vs = tpg->vhost_scsi;
  1522. struct vhost_virtqueue *vq;
  1523. u32 reason;
  1524. if (!vs)
  1525. return;
  1526. mutex_lock(&vs->dev.mutex);
  1527. if (plug)
  1528. reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
  1529. else
  1530. reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
  1531. vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1532. mutex_lock(&vq->mutex);
  1533. if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG))
  1534. tcm_vhost_send_evt(vs, tpg, lun,
  1535. VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
  1536. mutex_unlock(&vq->mutex);
  1537. mutex_unlock(&vs->dev.mutex);
  1538. }
  1539. static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
  1540. {
  1541. tcm_vhost_do_plug(tpg, lun, true);
  1542. }
  1543. static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
  1544. {
  1545. tcm_vhost_do_plug(tpg, lun, false);
  1546. }
  1547. static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
  1548. struct se_lun *lun)
  1549. {
  1550. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  1551. struct tcm_vhost_tpg, se_tpg);
  1552. mutex_lock(&tcm_vhost_mutex);
  1553. mutex_lock(&tpg->tv_tpg_mutex);
  1554. tpg->tv_tpg_port_count++;
  1555. mutex_unlock(&tpg->tv_tpg_mutex);
  1556. tcm_vhost_hotplug(tpg, lun);
  1557. mutex_unlock(&tcm_vhost_mutex);
  1558. return 0;
  1559. }
  1560. static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
  1561. struct se_lun *lun)
  1562. {
  1563. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  1564. struct tcm_vhost_tpg, se_tpg);
  1565. mutex_lock(&tcm_vhost_mutex);
  1566. mutex_lock(&tpg->tv_tpg_mutex);
  1567. tpg->tv_tpg_port_count--;
  1568. mutex_unlock(&tpg->tv_tpg_mutex);
  1569. tcm_vhost_hotunplug(tpg, lun);
  1570. mutex_unlock(&tcm_vhost_mutex);
  1571. }
  1572. static struct se_node_acl *
  1573. tcm_vhost_make_nodeacl(struct se_portal_group *se_tpg,
  1574. struct config_group *group,
  1575. const char *name)
  1576. {
  1577. struct se_node_acl *se_nacl, *se_nacl_new;
  1578. struct tcm_vhost_nacl *nacl;
  1579. u64 wwpn = 0;
  1580. u32 nexus_depth;
  1581. /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
  1582. return ERR_PTR(-EINVAL); */
  1583. se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
  1584. if (!se_nacl_new)
  1585. return ERR_PTR(-ENOMEM);
  1586. nexus_depth = 1;
  1587. /*
  1588. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  1589. * when converting a NodeACL from demo mode -> explict
  1590. */
  1591. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  1592. name, nexus_depth);
  1593. if (IS_ERR(se_nacl)) {
  1594. tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
  1595. return se_nacl;
  1596. }
  1597. /*
  1598. * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
  1599. */
  1600. nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
  1601. nacl->iport_wwpn = wwpn;
  1602. return se_nacl;
  1603. }
  1604. static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
  1605. {
  1606. struct tcm_vhost_nacl *nacl = container_of(se_acl,
  1607. struct tcm_vhost_nacl, se_node_acl);
  1608. core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
  1609. kfree(nacl);
  1610. }
  1611. static void tcm_vhost_free_cmd_map_res(struct tcm_vhost_nexus *nexus,
  1612. struct se_session *se_sess)
  1613. {
  1614. struct tcm_vhost_cmd *tv_cmd;
  1615. unsigned int i;
  1616. if (!se_sess->sess_cmd_map)
  1617. return;
  1618. for (i = 0; i < TCM_VHOST_DEFAULT_TAGS; i++) {
  1619. tv_cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[i];
  1620. kfree(tv_cmd->tvc_sgl);
  1621. kfree(tv_cmd->tvc_prot_sgl);
  1622. kfree(tv_cmd->tvc_upages);
  1623. }
  1624. }
  1625. static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tpg,
  1626. const char *name)
  1627. {
  1628. struct se_portal_group *se_tpg;
  1629. struct se_session *se_sess;
  1630. struct tcm_vhost_nexus *tv_nexus;
  1631. struct tcm_vhost_cmd *tv_cmd;
  1632. unsigned int i;
  1633. mutex_lock(&tpg->tv_tpg_mutex);
  1634. if (tpg->tpg_nexus) {
  1635. mutex_unlock(&tpg->tv_tpg_mutex);
  1636. pr_debug("tpg->tpg_nexus already exists\n");
  1637. return -EEXIST;
  1638. }
  1639. se_tpg = &tpg->se_tpg;
  1640. tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
  1641. if (!tv_nexus) {
  1642. mutex_unlock(&tpg->tv_tpg_mutex);
  1643. pr_err("Unable to allocate struct tcm_vhost_nexus\n");
  1644. return -ENOMEM;
  1645. }
  1646. /*
  1647. * Initialize the struct se_session pointer and setup tagpool
  1648. * for struct tcm_vhost_cmd descriptors
  1649. */
  1650. tv_nexus->tvn_se_sess = transport_init_session_tags(
  1651. TCM_VHOST_DEFAULT_TAGS,
  1652. sizeof(struct tcm_vhost_cmd),
  1653. TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS);
  1654. if (IS_ERR(tv_nexus->tvn_se_sess)) {
  1655. mutex_unlock(&tpg->tv_tpg_mutex);
  1656. kfree(tv_nexus);
  1657. return -ENOMEM;
  1658. }
  1659. se_sess = tv_nexus->tvn_se_sess;
  1660. for (i = 0; i < TCM_VHOST_DEFAULT_TAGS; i++) {
  1661. tv_cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[i];
  1662. tv_cmd->tvc_sgl = kzalloc(sizeof(struct scatterlist) *
  1663. TCM_VHOST_PREALLOC_SGLS, GFP_KERNEL);
  1664. if (!tv_cmd->tvc_sgl) {
  1665. mutex_unlock(&tpg->tv_tpg_mutex);
  1666. pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
  1667. goto out;
  1668. }
  1669. tv_cmd->tvc_upages = kzalloc(sizeof(struct page *) *
  1670. TCM_VHOST_PREALLOC_UPAGES, GFP_KERNEL);
  1671. if (!tv_cmd->tvc_upages) {
  1672. mutex_unlock(&tpg->tv_tpg_mutex);
  1673. pr_err("Unable to allocate tv_cmd->tvc_upages\n");
  1674. goto out;
  1675. }
  1676. tv_cmd->tvc_prot_sgl = kzalloc(sizeof(struct scatterlist) *
  1677. TCM_VHOST_PREALLOC_PROT_SGLS, GFP_KERNEL);
  1678. if (!tv_cmd->tvc_prot_sgl) {
  1679. mutex_unlock(&tpg->tv_tpg_mutex);
  1680. pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
  1681. goto out;
  1682. }
  1683. }
  1684. /*
  1685. * Since we are running in 'demo mode' this call with generate a
  1686. * struct se_node_acl for the tcm_vhost struct se_portal_group with
  1687. * the SCSI Initiator port name of the passed configfs group 'name'.
  1688. */
  1689. tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  1690. se_tpg, (unsigned char *)name);
  1691. if (!tv_nexus->tvn_se_sess->se_node_acl) {
  1692. mutex_unlock(&tpg->tv_tpg_mutex);
  1693. pr_debug("core_tpg_check_initiator_node_acl() failed"
  1694. " for %s\n", name);
  1695. goto out;
  1696. }
  1697. /*
  1698. * Now register the TCM vhost virtual I_T Nexus as active with the
  1699. * call to __transport_register_session()
  1700. */
  1701. __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
  1702. tv_nexus->tvn_se_sess, tv_nexus);
  1703. tpg->tpg_nexus = tv_nexus;
  1704. mutex_unlock(&tpg->tv_tpg_mutex);
  1705. return 0;
  1706. out:
  1707. tcm_vhost_free_cmd_map_res(tv_nexus, se_sess);
  1708. transport_free_session(se_sess);
  1709. kfree(tv_nexus);
  1710. return -ENOMEM;
  1711. }
  1712. static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
  1713. {
  1714. struct se_session *se_sess;
  1715. struct tcm_vhost_nexus *tv_nexus;
  1716. mutex_lock(&tpg->tv_tpg_mutex);
  1717. tv_nexus = tpg->tpg_nexus;
  1718. if (!tv_nexus) {
  1719. mutex_unlock(&tpg->tv_tpg_mutex);
  1720. return -ENODEV;
  1721. }
  1722. se_sess = tv_nexus->tvn_se_sess;
  1723. if (!se_sess) {
  1724. mutex_unlock(&tpg->tv_tpg_mutex);
  1725. return -ENODEV;
  1726. }
  1727. if (tpg->tv_tpg_port_count != 0) {
  1728. mutex_unlock(&tpg->tv_tpg_mutex);
  1729. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1730. " active TPG port count: %d\n",
  1731. tpg->tv_tpg_port_count);
  1732. return -EBUSY;
  1733. }
  1734. if (tpg->tv_tpg_vhost_count != 0) {
  1735. mutex_unlock(&tpg->tv_tpg_mutex);
  1736. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1737. " active TPG vhost count: %d\n",
  1738. tpg->tv_tpg_vhost_count);
  1739. return -EBUSY;
  1740. }
  1741. pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
  1742. " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
  1743. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1744. tcm_vhost_free_cmd_map_res(tv_nexus, se_sess);
  1745. /*
  1746. * Release the SCSI I_T Nexus to the emulated vhost Target Port
  1747. */
  1748. transport_deregister_session(tv_nexus->tvn_se_sess);
  1749. tpg->tpg_nexus = NULL;
  1750. mutex_unlock(&tpg->tv_tpg_mutex);
  1751. kfree(tv_nexus);
  1752. return 0;
  1753. }
  1754. static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
  1755. char *page)
  1756. {
  1757. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  1758. struct tcm_vhost_tpg, se_tpg);
  1759. struct tcm_vhost_nexus *tv_nexus;
  1760. ssize_t ret;
  1761. mutex_lock(&tpg->tv_tpg_mutex);
  1762. tv_nexus = tpg->tpg_nexus;
  1763. if (!tv_nexus) {
  1764. mutex_unlock(&tpg->tv_tpg_mutex);
  1765. return -ENODEV;
  1766. }
  1767. ret = snprintf(page, PAGE_SIZE, "%s\n",
  1768. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1769. mutex_unlock(&tpg->tv_tpg_mutex);
  1770. return ret;
  1771. }
  1772. static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
  1773. const char *page,
  1774. size_t count)
  1775. {
  1776. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  1777. struct tcm_vhost_tpg, se_tpg);
  1778. struct tcm_vhost_tport *tport_wwn = tpg->tport;
  1779. unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
  1780. int ret;
  1781. /*
  1782. * Shutdown the active I_T nexus if 'NULL' is passed..
  1783. */
  1784. if (!strncmp(page, "NULL", 4)) {
  1785. ret = tcm_vhost_drop_nexus(tpg);
  1786. return (!ret) ? count : ret;
  1787. }
  1788. /*
  1789. * Otherwise make sure the passed virtual Initiator port WWN matches
  1790. * the fabric protocol_id set in tcm_vhost_make_tport(), and call
  1791. * tcm_vhost_make_nexus().
  1792. */
  1793. if (strlen(page) >= TCM_VHOST_NAMELEN) {
  1794. pr_err("Emulated NAA Sas Address: %s, exceeds"
  1795. " max: %d\n", page, TCM_VHOST_NAMELEN);
  1796. return -EINVAL;
  1797. }
  1798. snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
  1799. ptr = strstr(i_port, "naa.");
  1800. if (ptr) {
  1801. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
  1802. pr_err("Passed SAS Initiator Port %s does not"
  1803. " match target port protoid: %s\n", i_port,
  1804. tcm_vhost_dump_proto_id(tport_wwn));
  1805. return -EINVAL;
  1806. }
  1807. port_ptr = &i_port[0];
  1808. goto check_newline;
  1809. }
  1810. ptr = strstr(i_port, "fc.");
  1811. if (ptr) {
  1812. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
  1813. pr_err("Passed FCP Initiator Port %s does not"
  1814. " match target port protoid: %s\n", i_port,
  1815. tcm_vhost_dump_proto_id(tport_wwn));
  1816. return -EINVAL;
  1817. }
  1818. port_ptr = &i_port[3]; /* Skip over "fc." */
  1819. goto check_newline;
  1820. }
  1821. ptr = strstr(i_port, "iqn.");
  1822. if (ptr) {
  1823. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
  1824. pr_err("Passed iSCSI Initiator Port %s does not"
  1825. " match target port protoid: %s\n", i_port,
  1826. tcm_vhost_dump_proto_id(tport_wwn));
  1827. return -EINVAL;
  1828. }
  1829. port_ptr = &i_port[0];
  1830. goto check_newline;
  1831. }
  1832. pr_err("Unable to locate prefix for emulated Initiator Port:"
  1833. " %s\n", i_port);
  1834. return -EINVAL;
  1835. /*
  1836. * Clear any trailing newline for the NAA WWN
  1837. */
  1838. check_newline:
  1839. if (i_port[strlen(i_port)-1] == '\n')
  1840. i_port[strlen(i_port)-1] = '\0';
  1841. ret = tcm_vhost_make_nexus(tpg, port_ptr);
  1842. if (ret < 0)
  1843. return ret;
  1844. return count;
  1845. }
  1846. TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
  1847. static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
  1848. &tcm_vhost_tpg_nexus.attr,
  1849. NULL,
  1850. };
  1851. static struct se_portal_group *
  1852. tcm_vhost_make_tpg(struct se_wwn *wwn,
  1853. struct config_group *group,
  1854. const char *name)
  1855. {
  1856. struct tcm_vhost_tport *tport = container_of(wwn,
  1857. struct tcm_vhost_tport, tport_wwn);
  1858. struct tcm_vhost_tpg *tpg;
  1859. unsigned long tpgt;
  1860. int ret;
  1861. if (strstr(name, "tpgt_") != name)
  1862. return ERR_PTR(-EINVAL);
  1863. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
  1864. return ERR_PTR(-EINVAL);
  1865. tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
  1866. if (!tpg) {
  1867. pr_err("Unable to allocate struct tcm_vhost_tpg");
  1868. return ERR_PTR(-ENOMEM);
  1869. }
  1870. mutex_init(&tpg->tv_tpg_mutex);
  1871. INIT_LIST_HEAD(&tpg->tv_tpg_list);
  1872. tpg->tport = tport;
  1873. tpg->tport_tpgt = tpgt;
  1874. ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
  1875. &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
  1876. if (ret < 0) {
  1877. kfree(tpg);
  1878. return NULL;
  1879. }
  1880. mutex_lock(&tcm_vhost_mutex);
  1881. list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
  1882. mutex_unlock(&tcm_vhost_mutex);
  1883. return &tpg->se_tpg;
  1884. }
  1885. static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
  1886. {
  1887. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  1888. struct tcm_vhost_tpg, se_tpg);
  1889. mutex_lock(&tcm_vhost_mutex);
  1890. list_del(&tpg->tv_tpg_list);
  1891. mutex_unlock(&tcm_vhost_mutex);
  1892. /*
  1893. * Release the virtual I_T Nexus for this vhost TPG
  1894. */
  1895. tcm_vhost_drop_nexus(tpg);
  1896. /*
  1897. * Deregister the se_tpg from TCM..
  1898. */
  1899. core_tpg_deregister(se_tpg);
  1900. kfree(tpg);
  1901. }
  1902. static struct se_wwn *
  1903. tcm_vhost_make_tport(struct target_fabric_configfs *tf,
  1904. struct config_group *group,
  1905. const char *name)
  1906. {
  1907. struct tcm_vhost_tport *tport;
  1908. char *ptr;
  1909. u64 wwpn = 0;
  1910. int off = 0;
  1911. /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
  1912. return ERR_PTR(-EINVAL); */
  1913. tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
  1914. if (!tport) {
  1915. pr_err("Unable to allocate struct tcm_vhost_tport");
  1916. return ERR_PTR(-ENOMEM);
  1917. }
  1918. tport->tport_wwpn = wwpn;
  1919. /*
  1920. * Determine the emulated Protocol Identifier and Target Port Name
  1921. * based on the incoming configfs directory name.
  1922. */
  1923. ptr = strstr(name, "naa.");
  1924. if (ptr) {
  1925. tport->tport_proto_id = SCSI_PROTOCOL_SAS;
  1926. goto check_len;
  1927. }
  1928. ptr = strstr(name, "fc.");
  1929. if (ptr) {
  1930. tport->tport_proto_id = SCSI_PROTOCOL_FCP;
  1931. off = 3; /* Skip over "fc." */
  1932. goto check_len;
  1933. }
  1934. ptr = strstr(name, "iqn.");
  1935. if (ptr) {
  1936. tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
  1937. goto check_len;
  1938. }
  1939. pr_err("Unable to locate prefix for emulated Target Port:"
  1940. " %s\n", name);
  1941. kfree(tport);
  1942. return ERR_PTR(-EINVAL);
  1943. check_len:
  1944. if (strlen(name) >= TCM_VHOST_NAMELEN) {
  1945. pr_err("Emulated %s Address: %s, exceeds"
  1946. " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
  1947. TCM_VHOST_NAMELEN);
  1948. kfree(tport);
  1949. return ERR_PTR(-EINVAL);
  1950. }
  1951. snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
  1952. pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
  1953. " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
  1954. return &tport->tport_wwn;
  1955. }
  1956. static void tcm_vhost_drop_tport(struct se_wwn *wwn)
  1957. {
  1958. struct tcm_vhost_tport *tport = container_of(wwn,
  1959. struct tcm_vhost_tport, tport_wwn);
  1960. pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
  1961. " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
  1962. tport->tport_name);
  1963. kfree(tport);
  1964. }
  1965. static ssize_t
  1966. tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs *tf,
  1967. char *page)
  1968. {
  1969. return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
  1970. "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
  1971. utsname()->machine);
  1972. }
  1973. TF_WWN_ATTR_RO(tcm_vhost, version);
  1974. static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
  1975. &tcm_vhost_wwn_version.attr,
  1976. NULL,
  1977. };
  1978. static struct target_core_fabric_ops tcm_vhost_ops = {
  1979. .get_fabric_name = tcm_vhost_get_fabric_name,
  1980. .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
  1981. .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
  1982. .tpg_get_tag = tcm_vhost_get_tag,
  1983. .tpg_get_default_depth = tcm_vhost_get_default_depth,
  1984. .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
  1985. .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
  1986. .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
  1987. .tpg_check_demo_mode = tcm_vhost_check_true,
  1988. .tpg_check_demo_mode_cache = tcm_vhost_check_true,
  1989. .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
  1990. .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
  1991. .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
  1992. .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
  1993. .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
  1994. .release_cmd = tcm_vhost_release_cmd,
  1995. .check_stop_free = vhost_scsi_check_stop_free,
  1996. .shutdown_session = tcm_vhost_shutdown_session,
  1997. .close_session = tcm_vhost_close_session,
  1998. .sess_get_index = tcm_vhost_sess_get_index,
  1999. .sess_get_initiator_sid = NULL,
  2000. .write_pending = tcm_vhost_write_pending,
  2001. .write_pending_status = tcm_vhost_write_pending_status,
  2002. .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
  2003. .get_task_tag = tcm_vhost_get_task_tag,
  2004. .get_cmd_state = tcm_vhost_get_cmd_state,
  2005. .queue_data_in = tcm_vhost_queue_data_in,
  2006. .queue_status = tcm_vhost_queue_status,
  2007. .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
  2008. .aborted_task = tcm_vhost_aborted_task,
  2009. /*
  2010. * Setup callers for generic logic in target_core_fabric_configfs.c
  2011. */
  2012. .fabric_make_wwn = tcm_vhost_make_tport,
  2013. .fabric_drop_wwn = tcm_vhost_drop_tport,
  2014. .fabric_make_tpg = tcm_vhost_make_tpg,
  2015. .fabric_drop_tpg = tcm_vhost_drop_tpg,
  2016. .fabric_post_link = tcm_vhost_port_link,
  2017. .fabric_pre_unlink = tcm_vhost_port_unlink,
  2018. .fabric_make_np = NULL,
  2019. .fabric_drop_np = NULL,
  2020. .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
  2021. .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
  2022. };
  2023. static int tcm_vhost_register_configfs(void)
  2024. {
  2025. struct target_fabric_configfs *fabric;
  2026. int ret;
  2027. pr_debug("TCM_VHOST fabric module %s on %s/%s"
  2028. " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
  2029. utsname()->machine);
  2030. /*
  2031. * Register the top level struct config_item_type with TCM core
  2032. */
  2033. fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
  2034. if (IS_ERR(fabric)) {
  2035. pr_err("target_fabric_configfs_init() failed\n");
  2036. return PTR_ERR(fabric);
  2037. }
  2038. /*
  2039. * Setup fabric->tf_ops from our local tcm_vhost_ops
  2040. */
  2041. fabric->tf_ops = tcm_vhost_ops;
  2042. /*
  2043. * Setup default attribute lists for various fabric->tf_cit_tmpl
  2044. */
  2045. fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
  2046. fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
  2047. fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;
  2048. fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
  2049. fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
  2050. fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  2051. fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  2052. fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  2053. fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  2054. /*
  2055. * Register the fabric for use within TCM
  2056. */
  2057. ret = target_fabric_configfs_register(fabric);
  2058. if (ret < 0) {
  2059. pr_err("target_fabric_configfs_register() failed"
  2060. " for TCM_VHOST\n");
  2061. return ret;
  2062. }
  2063. /*
  2064. * Setup our local pointer to *fabric
  2065. */
  2066. tcm_vhost_fabric_configfs = fabric;
  2067. pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
  2068. return 0;
  2069. };
  2070. static void tcm_vhost_deregister_configfs(void)
  2071. {
  2072. if (!tcm_vhost_fabric_configfs)
  2073. return;
  2074. target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
  2075. tcm_vhost_fabric_configfs = NULL;
  2076. pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
  2077. };
  2078. static int __init tcm_vhost_init(void)
  2079. {
  2080. int ret = -ENOMEM;
  2081. /*
  2082. * Use our own dedicated workqueue for submitting I/O into
  2083. * target core to avoid contention within system_wq.
  2084. */
  2085. tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
  2086. if (!tcm_vhost_workqueue)
  2087. goto out;
  2088. ret = vhost_scsi_register();
  2089. if (ret < 0)
  2090. goto out_destroy_workqueue;
  2091. ret = tcm_vhost_register_configfs();
  2092. if (ret < 0)
  2093. goto out_vhost_scsi_deregister;
  2094. return 0;
  2095. out_vhost_scsi_deregister:
  2096. vhost_scsi_deregister();
  2097. out_destroy_workqueue:
  2098. destroy_workqueue(tcm_vhost_workqueue);
  2099. out:
  2100. return ret;
  2101. };
  2102. static void tcm_vhost_exit(void)
  2103. {
  2104. tcm_vhost_deregister_configfs();
  2105. vhost_scsi_deregister();
  2106. destroy_workqueue(tcm_vhost_workqueue);
  2107. };
  2108. MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
  2109. MODULE_ALIAS("tcm_vhost");
  2110. MODULE_LICENSE("GPL");
  2111. module_init(tcm_vhost_init);
  2112. module_exit(tcm_vhost_exit);