scsi.c 64 KB

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