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/miscdevice.h>
  38. #include <asm/unaligned.h>
  39. #include <scsi/scsi.h>
  40. #include <target/target_core_base.h>
  41. #include <target/target_core_fabric.h>
  42. #include <target/target_core_fabric_configfs.h>
  43. #include <target/target_core_configfs.h>
  44. #include <target/configfs_macros.h>
  45. #include <linux/vhost.h>
  46. #include <linux/virtio_scsi.h>
  47. #include <linux/llist.h>
  48. #include <linux/bitmap.h>
  49. #include <linux/percpu_ida.h>
  50. #include "vhost.h"
  51. #define VHOST_SCSI_VERSION "v0.1"
  52. #define VHOST_SCSI_NAMELEN 256
  53. #define VHOST_SCSI_MAX_CDB_SIZE 32
  54. #define VHOST_SCSI_DEFAULT_TAGS 256
  55. #define VHOST_SCSI_PREALLOC_SGLS 2048
  56. #define VHOST_SCSI_PREALLOC_UPAGES 2048
  57. #define VHOST_SCSI_PREALLOC_PROT_SGLS 512
  58. struct vhost_scsi_inflight {
  59. /* Wait for the flush operation to finish */
  60. struct completion comp;
  61. /* Refcount for the inflight reqs */
  62. struct kref kref;
  63. };
  64. struct vhost_scsi_cmd {
  65. /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
  66. int tvc_vq_desc;
  67. /* virtio-scsi initiator task attribute */
  68. int tvc_task_attr;
  69. /* virtio-scsi response incoming iovecs */
  70. int tvc_in_iovs;
  71. /* virtio-scsi initiator data direction */
  72. enum dma_data_direction tvc_data_direction;
  73. /* Expected data transfer length from virtio-scsi header */
  74. u32 tvc_exp_data_len;
  75. /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
  76. u64 tvc_tag;
  77. /* The number of scatterlists associated with this cmd */
  78. u32 tvc_sgl_count;
  79. u32 tvc_prot_sgl_count;
  80. /* Saved unpacked SCSI LUN for vhost_scsi_submission_work() */
  81. u32 tvc_lun;
  82. /* Pointer to the SGL formatted memory from virtio-scsi */
  83. struct scatterlist *tvc_sgl;
  84. struct scatterlist *tvc_prot_sgl;
  85. struct page **tvc_upages;
  86. /* Pointer to response header iovec */
  87. struct iovec *tvc_resp_iov;
  88. /* Pointer to vhost_scsi for our device */
  89. struct vhost_scsi *tvc_vhost;
  90. /* Pointer to vhost_virtqueue for the cmd */
  91. struct vhost_virtqueue *tvc_vq;
  92. /* Pointer to vhost nexus memory */
  93. struct vhost_scsi_nexus *tvc_nexus;
  94. /* The TCM I/O descriptor that is accessed via container_of() */
  95. struct se_cmd tvc_se_cmd;
  96. /* work item used for cmwq dispatch to vhost_scsi_submission_work() */
  97. struct work_struct work;
  98. /* Copy of the incoming SCSI command descriptor block (CDB) */
  99. unsigned char tvc_cdb[VHOST_SCSI_MAX_CDB_SIZE];
  100. /* Sense buffer that will be mapped into outgoing status */
  101. unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
  102. /* Completed commands list, serviced from vhost worker thread */
  103. struct llist_node tvc_completion_list;
  104. /* Used to track inflight cmd */
  105. struct vhost_scsi_inflight *inflight;
  106. };
  107. struct vhost_scsi_nexus {
  108. /* Pointer to TCM session for I_T Nexus */
  109. struct se_session *tvn_se_sess;
  110. };
  111. struct vhost_scsi_nacl {
  112. /* Binary World Wide unique Port Name for Vhost Initiator port */
  113. u64 iport_wwpn;
  114. /* ASCII formatted WWPN for Sas Initiator port */
  115. char iport_name[VHOST_SCSI_NAMELEN];
  116. /* Returned by vhost_scsi_make_nodeacl() */
  117. struct se_node_acl se_node_acl;
  118. };
  119. struct vhost_scsi_tpg {
  120. /* Vhost port target portal group tag for TCM */
  121. u16 tport_tpgt;
  122. /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
  123. int tv_tpg_port_count;
  124. /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
  125. int tv_tpg_vhost_count;
  126. /* Used for enabling T10-PI with legacy devices */
  127. int tv_fabric_prot_type;
  128. /* list for vhost_scsi_list */
  129. struct list_head tv_tpg_list;
  130. /* Used to protect access for tpg_nexus */
  131. struct mutex tv_tpg_mutex;
  132. /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
  133. struct vhost_scsi_nexus *tpg_nexus;
  134. /* Pointer back to vhost_scsi_tport */
  135. struct vhost_scsi_tport *tport;
  136. /* Returned by vhost_scsi_make_tpg() */
  137. struct se_portal_group se_tpg;
  138. /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
  139. struct vhost_scsi *vhost_scsi;
  140. };
  141. struct vhost_scsi_tport {
  142. /* SCSI protocol the tport is providing */
  143. u8 tport_proto_id;
  144. /* Binary World Wide unique Port Name for Vhost Target port */
  145. u64 tport_wwpn;
  146. /* ASCII formatted WWPN for Vhost Target port */
  147. char tport_name[VHOST_SCSI_NAMELEN];
  148. /* Returned by vhost_scsi_make_tport() */
  149. struct se_wwn tport_wwn;
  150. };
  151. struct vhost_scsi_evt {
  152. /* event to be sent to guest */
  153. struct virtio_scsi_event event;
  154. /* event list, serviced from vhost worker thread */
  155. struct llist_node list;
  156. };
  157. enum {
  158. VHOST_SCSI_VQ_CTL = 0,
  159. VHOST_SCSI_VQ_EVT = 1,
  160. VHOST_SCSI_VQ_IO = 2,
  161. };
  162. /* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
  163. enum {
  164. VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG) |
  165. (1ULL << VIRTIO_SCSI_F_T10_PI) |
  166. (1ULL << VIRTIO_F_ANY_LAYOUT) |
  167. (1ULL << VIRTIO_F_VERSION_1)
  168. };
  169. #define VHOST_SCSI_MAX_TARGET 256
  170. #define VHOST_SCSI_MAX_VQ 128
  171. #define VHOST_SCSI_MAX_EVENT 128
  172. struct vhost_scsi_virtqueue {
  173. struct vhost_virtqueue vq;
  174. /*
  175. * Reference counting for inflight reqs, used for flush operation. At
  176. * each time, one reference tracks new commands submitted, while we
  177. * wait for another one to reach 0.
  178. */
  179. struct vhost_scsi_inflight inflights[2];
  180. /*
  181. * Indicate current inflight in use, protected by vq->mutex.
  182. * Writers must also take dev mutex and flush under it.
  183. */
  184. int inflight_idx;
  185. };
  186. struct vhost_scsi {
  187. /* Protected by vhost_scsi->dev.mutex */
  188. struct vhost_scsi_tpg **vs_tpg;
  189. char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
  190. struct vhost_dev dev;
  191. struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
  192. struct vhost_work vs_completion_work; /* cmd completion work item */
  193. struct llist_head vs_completion_list; /* cmd completion queue */
  194. struct vhost_work vs_event_work; /* evt injection work item */
  195. struct llist_head vs_event_list; /* evt injection queue */
  196. bool vs_events_missed; /* any missed events, protected by vq->mutex */
  197. int vs_events_nr; /* num of pending events, protected by vq->mutex */
  198. };
  199. static struct target_core_fabric_ops vhost_scsi_ops;
  200. static struct workqueue_struct *vhost_scsi_workqueue;
  201. /* Global spinlock to protect vhost_scsi TPG list for vhost IOCTL access */
  202. static DEFINE_MUTEX(vhost_scsi_mutex);
  203. static LIST_HEAD(vhost_scsi_list);
  204. static int iov_num_pages(void __user *iov_base, size_t iov_len)
  205. {
  206. return (PAGE_ALIGN((unsigned long)iov_base + iov_len) -
  207. ((unsigned long)iov_base & PAGE_MASK)) >> PAGE_SHIFT;
  208. }
  209. static void vhost_scsi_done_inflight(struct kref *kref)
  210. {
  211. struct vhost_scsi_inflight *inflight;
  212. inflight = container_of(kref, struct vhost_scsi_inflight, kref);
  213. complete(&inflight->comp);
  214. }
  215. static void vhost_scsi_init_inflight(struct vhost_scsi *vs,
  216. struct vhost_scsi_inflight *old_inflight[])
  217. {
  218. struct vhost_scsi_inflight *new_inflight;
  219. struct vhost_virtqueue *vq;
  220. int idx, i;
  221. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  222. vq = &vs->vqs[i].vq;
  223. mutex_lock(&vq->mutex);
  224. /* store old infight */
  225. idx = vs->vqs[i].inflight_idx;
  226. if (old_inflight)
  227. old_inflight[i] = &vs->vqs[i].inflights[idx];
  228. /* setup new infight */
  229. vs->vqs[i].inflight_idx = idx ^ 1;
  230. new_inflight = &vs->vqs[i].inflights[idx ^ 1];
  231. kref_init(&new_inflight->kref);
  232. init_completion(&new_inflight->comp);
  233. mutex_unlock(&vq->mutex);
  234. }
  235. }
  236. static struct vhost_scsi_inflight *
  237. vhost_scsi_get_inflight(struct vhost_virtqueue *vq)
  238. {
  239. struct vhost_scsi_inflight *inflight;
  240. struct vhost_scsi_virtqueue *svq;
  241. svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
  242. inflight = &svq->inflights[svq->inflight_idx];
  243. kref_get(&inflight->kref);
  244. return inflight;
  245. }
  246. static void vhost_scsi_put_inflight(struct vhost_scsi_inflight *inflight)
  247. {
  248. kref_put(&inflight->kref, vhost_scsi_done_inflight);
  249. }
  250. static int vhost_scsi_check_true(struct se_portal_group *se_tpg)
  251. {
  252. return 1;
  253. }
  254. static int vhost_scsi_check_false(struct se_portal_group *se_tpg)
  255. {
  256. return 0;
  257. }
  258. static char *vhost_scsi_get_fabric_name(void)
  259. {
  260. return "vhost";
  261. }
  262. static u8 vhost_scsi_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  263. {
  264. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  265. struct vhost_scsi_tpg, se_tpg);
  266. struct vhost_scsi_tport *tport = tpg->tport;
  267. switch (tport->tport_proto_id) {
  268. case SCSI_PROTOCOL_SAS:
  269. return sas_get_fabric_proto_ident(se_tpg);
  270. case SCSI_PROTOCOL_FCP:
  271. return fc_get_fabric_proto_ident(se_tpg);
  272. case SCSI_PROTOCOL_ISCSI:
  273. return iscsi_get_fabric_proto_ident(se_tpg);
  274. default:
  275. pr_err("Unknown tport_proto_id: 0x%02x, using"
  276. " SAS emulation\n", tport->tport_proto_id);
  277. break;
  278. }
  279. return sas_get_fabric_proto_ident(se_tpg);
  280. }
  281. static char *vhost_scsi_get_fabric_wwn(struct se_portal_group *se_tpg)
  282. {
  283. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  284. struct vhost_scsi_tpg, se_tpg);
  285. struct vhost_scsi_tport *tport = tpg->tport;
  286. return &tport->tport_name[0];
  287. }
  288. static u16 vhost_scsi_get_tpgt(struct se_portal_group *se_tpg)
  289. {
  290. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  291. struct vhost_scsi_tpg, se_tpg);
  292. return tpg->tport_tpgt;
  293. }
  294. static u32 vhost_scsi_get_default_depth(struct se_portal_group *se_tpg)
  295. {
  296. return 1;
  297. }
  298. static u32
  299. vhost_scsi_get_pr_transport_id(struct se_portal_group *se_tpg,
  300. struct se_node_acl *se_nacl,
  301. struct t10_pr_registration *pr_reg,
  302. int *format_code,
  303. unsigned char *buf)
  304. {
  305. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  306. struct vhost_scsi_tpg, se_tpg);
  307. struct vhost_scsi_tport *tport = tpg->tport;
  308. switch (tport->tport_proto_id) {
  309. case SCSI_PROTOCOL_SAS:
  310. return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  311. format_code, buf);
  312. case SCSI_PROTOCOL_FCP:
  313. return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  314. format_code, buf);
  315. case SCSI_PROTOCOL_ISCSI:
  316. return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  317. format_code, buf);
  318. default:
  319. pr_err("Unknown tport_proto_id: 0x%02x, using"
  320. " SAS emulation\n", tport->tport_proto_id);
  321. break;
  322. }
  323. return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  324. format_code, buf);
  325. }
  326. static u32
  327. vhost_scsi_get_pr_transport_id_len(struct se_portal_group *se_tpg,
  328. struct se_node_acl *se_nacl,
  329. struct t10_pr_registration *pr_reg,
  330. int *format_code)
  331. {
  332. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  333. struct vhost_scsi_tpg, se_tpg);
  334. struct vhost_scsi_tport *tport = tpg->tport;
  335. switch (tport->tport_proto_id) {
  336. case SCSI_PROTOCOL_SAS:
  337. return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  338. format_code);
  339. case SCSI_PROTOCOL_FCP:
  340. return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  341. format_code);
  342. case SCSI_PROTOCOL_ISCSI:
  343. return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  344. format_code);
  345. default:
  346. pr_err("Unknown tport_proto_id: 0x%02x, using"
  347. " SAS emulation\n", tport->tport_proto_id);
  348. break;
  349. }
  350. return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  351. format_code);
  352. }
  353. static char *
  354. vhost_scsi_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
  355. const char *buf,
  356. u32 *out_tid_len,
  357. char **port_nexus_ptr)
  358. {
  359. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  360. struct vhost_scsi_tpg, se_tpg);
  361. struct vhost_scsi_tport *tport = tpg->tport;
  362. switch (tport->tport_proto_id) {
  363. case SCSI_PROTOCOL_SAS:
  364. return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  365. port_nexus_ptr);
  366. case SCSI_PROTOCOL_FCP:
  367. return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  368. port_nexus_ptr);
  369. case SCSI_PROTOCOL_ISCSI:
  370. return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  371. port_nexus_ptr);
  372. default:
  373. pr_err("Unknown tport_proto_id: 0x%02x, using"
  374. " SAS emulation\n", tport->tport_proto_id);
  375. break;
  376. }
  377. return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  378. port_nexus_ptr);
  379. }
  380. static int vhost_scsi_check_prot_fabric_only(struct se_portal_group *se_tpg)
  381. {
  382. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  383. struct vhost_scsi_tpg, se_tpg);
  384. return tpg->tv_fabric_prot_type;
  385. }
  386. static struct se_node_acl *
  387. vhost_scsi_alloc_fabric_acl(struct se_portal_group *se_tpg)
  388. {
  389. struct vhost_scsi_nacl *nacl;
  390. nacl = kzalloc(sizeof(struct vhost_scsi_nacl), GFP_KERNEL);
  391. if (!nacl) {
  392. pr_err("Unable to allocate struct vhost_scsi_nacl\n");
  393. return NULL;
  394. }
  395. return &nacl->se_node_acl;
  396. }
  397. static void
  398. vhost_scsi_release_fabric_acl(struct se_portal_group *se_tpg,
  399. struct se_node_acl *se_nacl)
  400. {
  401. struct vhost_scsi_nacl *nacl = container_of(se_nacl,
  402. struct vhost_scsi_nacl, se_node_acl);
  403. kfree(nacl);
  404. }
  405. static u32 vhost_scsi_tpg_get_inst_index(struct se_portal_group *se_tpg)
  406. {
  407. return 1;
  408. }
  409. static void vhost_scsi_release_cmd(struct se_cmd *se_cmd)
  410. {
  411. struct vhost_scsi_cmd *tv_cmd = container_of(se_cmd,
  412. struct vhost_scsi_cmd, tvc_se_cmd);
  413. struct se_session *se_sess = tv_cmd->tvc_nexus->tvn_se_sess;
  414. int i;
  415. if (tv_cmd->tvc_sgl_count) {
  416. for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
  417. put_page(sg_page(&tv_cmd->tvc_sgl[i]));
  418. }
  419. if (tv_cmd->tvc_prot_sgl_count) {
  420. for (i = 0; i < tv_cmd->tvc_prot_sgl_count; i++)
  421. put_page(sg_page(&tv_cmd->tvc_prot_sgl[i]));
  422. }
  423. vhost_scsi_put_inflight(tv_cmd->inflight);
  424. percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
  425. }
  426. static int vhost_scsi_shutdown_session(struct se_session *se_sess)
  427. {
  428. return 0;
  429. }
  430. static void vhost_scsi_close_session(struct se_session *se_sess)
  431. {
  432. return;
  433. }
  434. static u32 vhost_scsi_sess_get_index(struct se_session *se_sess)
  435. {
  436. return 0;
  437. }
  438. static int vhost_scsi_write_pending(struct se_cmd *se_cmd)
  439. {
  440. /* Go ahead and process the write immediately */
  441. target_execute_cmd(se_cmd);
  442. return 0;
  443. }
  444. static int vhost_scsi_write_pending_status(struct se_cmd *se_cmd)
  445. {
  446. return 0;
  447. }
  448. static void vhost_scsi_set_default_node_attrs(struct se_node_acl *nacl)
  449. {
  450. return;
  451. }
  452. static u32 vhost_scsi_get_task_tag(struct se_cmd *se_cmd)
  453. {
  454. return 0;
  455. }
  456. static int vhost_scsi_get_cmd_state(struct se_cmd *se_cmd)
  457. {
  458. return 0;
  459. }
  460. static void vhost_scsi_complete_cmd(struct vhost_scsi_cmd *cmd)
  461. {
  462. struct vhost_scsi *vs = cmd->tvc_vhost;
  463. llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
  464. vhost_work_queue(&vs->dev, &vs->vs_completion_work);
  465. }
  466. static int vhost_scsi_queue_data_in(struct se_cmd *se_cmd)
  467. {
  468. struct vhost_scsi_cmd *cmd = container_of(se_cmd,
  469. struct vhost_scsi_cmd, tvc_se_cmd);
  470. vhost_scsi_complete_cmd(cmd);
  471. return 0;
  472. }
  473. static int vhost_scsi_queue_status(struct se_cmd *se_cmd)
  474. {
  475. struct vhost_scsi_cmd *cmd = container_of(se_cmd,
  476. struct vhost_scsi_cmd, tvc_se_cmd);
  477. vhost_scsi_complete_cmd(cmd);
  478. return 0;
  479. }
  480. static void vhost_scsi_queue_tm_rsp(struct se_cmd *se_cmd)
  481. {
  482. return;
  483. }
  484. static void vhost_scsi_aborted_task(struct se_cmd *se_cmd)
  485. {
  486. return;
  487. }
  488. static void vhost_scsi_free_evt(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
  489. {
  490. vs->vs_events_nr--;
  491. kfree(evt);
  492. }
  493. static struct vhost_scsi_evt *
  494. vhost_scsi_allocate_evt(struct vhost_scsi *vs,
  495. u32 event, u32 reason)
  496. {
  497. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  498. struct vhost_scsi_evt *evt;
  499. if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
  500. vs->vs_events_missed = true;
  501. return NULL;
  502. }
  503. evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  504. if (!evt) {
  505. vq_err(vq, "Failed to allocate vhost_scsi_evt\n");
  506. vs->vs_events_missed = true;
  507. return NULL;
  508. }
  509. evt->event.event = cpu_to_vhost32(vq, event);
  510. evt->event.reason = cpu_to_vhost32(vq, reason);
  511. vs->vs_events_nr++;
  512. return evt;
  513. }
  514. static void vhost_scsi_free_cmd(struct vhost_scsi_cmd *cmd)
  515. {
  516. struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
  517. /* TODO locking against target/backend threads? */
  518. transport_generic_free_cmd(se_cmd, 0);
  519. }
  520. static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
  521. {
  522. return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
  523. }
  524. static void
  525. vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
  526. {
  527. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  528. struct virtio_scsi_event *event = &evt->event;
  529. struct virtio_scsi_event __user *eventp;
  530. unsigned out, in;
  531. int head, ret;
  532. if (!vq->private_data) {
  533. vs->vs_events_missed = true;
  534. return;
  535. }
  536. again:
  537. vhost_disable_notify(&vs->dev, vq);
  538. head = vhost_get_vq_desc(vq, vq->iov,
  539. ARRAY_SIZE(vq->iov), &out, &in,
  540. NULL, NULL);
  541. if (head < 0) {
  542. vs->vs_events_missed = true;
  543. return;
  544. }
  545. if (head == vq->num) {
  546. if (vhost_enable_notify(&vs->dev, vq))
  547. goto again;
  548. vs->vs_events_missed = true;
  549. return;
  550. }
  551. if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
  552. vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
  553. vq->iov[out].iov_len);
  554. vs->vs_events_missed = true;
  555. return;
  556. }
  557. if (vs->vs_events_missed) {
  558. event->event |= cpu_to_vhost32(vq, VIRTIO_SCSI_T_EVENTS_MISSED);
  559. vs->vs_events_missed = false;
  560. }
  561. eventp = vq->iov[out].iov_base;
  562. ret = __copy_to_user(eventp, event, sizeof(*event));
  563. if (!ret)
  564. vhost_add_used_and_signal(&vs->dev, vq, head, 0);
  565. else
  566. vq_err(vq, "Faulted on vhost_scsi_send_event\n");
  567. }
  568. static void vhost_scsi_evt_work(struct vhost_work *work)
  569. {
  570. struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
  571. vs_event_work);
  572. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  573. struct vhost_scsi_evt *evt;
  574. struct llist_node *llnode;
  575. mutex_lock(&vq->mutex);
  576. llnode = llist_del_all(&vs->vs_event_list);
  577. while (llnode) {
  578. evt = llist_entry(llnode, struct vhost_scsi_evt, list);
  579. llnode = llist_next(llnode);
  580. vhost_scsi_do_evt_work(vs, evt);
  581. vhost_scsi_free_evt(vs, evt);
  582. }
  583. mutex_unlock(&vq->mutex);
  584. }
  585. /* Fill in status and signal that we are done processing this command
  586. *
  587. * This is scheduled in the vhost work queue so we are called with the owner
  588. * process mm and can access the vring.
  589. */
  590. static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
  591. {
  592. struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
  593. vs_completion_work);
  594. DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
  595. struct virtio_scsi_cmd_resp v_rsp;
  596. struct vhost_scsi_cmd *cmd;
  597. struct llist_node *llnode;
  598. struct se_cmd *se_cmd;
  599. struct iov_iter iov_iter;
  600. int ret, vq;
  601. bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
  602. llnode = llist_del_all(&vs->vs_completion_list);
  603. while (llnode) {
  604. cmd = llist_entry(llnode, struct vhost_scsi_cmd,
  605. tvc_completion_list);
  606. llnode = llist_next(llnode);
  607. se_cmd = &cmd->tvc_se_cmd;
  608. pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
  609. cmd, se_cmd->residual_count, se_cmd->scsi_status);
  610. memset(&v_rsp, 0, sizeof(v_rsp));
  611. v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq, se_cmd->residual_count);
  612. /* TODO is status_qualifier field needed? */
  613. v_rsp.status = se_cmd->scsi_status;
  614. v_rsp.sense_len = cpu_to_vhost32(cmd->tvc_vq,
  615. se_cmd->scsi_sense_length);
  616. memcpy(v_rsp.sense, cmd->tvc_sense_buf,
  617. se_cmd->scsi_sense_length);
  618. iov_iter_init(&iov_iter, READ, cmd->tvc_resp_iov,
  619. cmd->tvc_in_iovs, sizeof(v_rsp));
  620. ret = copy_to_iter(&v_rsp, sizeof(v_rsp), &iov_iter);
  621. if (likely(ret == sizeof(v_rsp))) {
  622. struct vhost_scsi_virtqueue *q;
  623. vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
  624. q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
  625. vq = q - vs->vqs;
  626. __set_bit(vq, signal);
  627. } else
  628. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  629. vhost_scsi_free_cmd(cmd);
  630. }
  631. vq = -1;
  632. while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
  633. < VHOST_SCSI_MAX_VQ)
  634. vhost_signal(&vs->dev, &vs->vqs[vq].vq);
  635. }
  636. static struct vhost_scsi_cmd *
  637. vhost_scsi_get_tag(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg,
  638. unsigned char *cdb, u64 scsi_tag, u16 lun, u8 task_attr,
  639. u32 exp_data_len, int data_direction)
  640. {
  641. struct vhost_scsi_cmd *cmd;
  642. struct vhost_scsi_nexus *tv_nexus;
  643. struct se_session *se_sess;
  644. struct scatterlist *sg, *prot_sg;
  645. struct page **pages;
  646. int tag;
  647. tv_nexus = tpg->tpg_nexus;
  648. if (!tv_nexus) {
  649. pr_err("Unable to locate active struct vhost_scsi_nexus\n");
  650. return ERR_PTR(-EIO);
  651. }
  652. se_sess = tv_nexus->tvn_se_sess;
  653. tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
  654. if (tag < 0) {
  655. pr_err("Unable to obtain tag for vhost_scsi_cmd\n");
  656. return ERR_PTR(-ENOMEM);
  657. }
  658. cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[tag];
  659. sg = cmd->tvc_sgl;
  660. prot_sg = cmd->tvc_prot_sgl;
  661. pages = cmd->tvc_upages;
  662. memset(cmd, 0, sizeof(struct vhost_scsi_cmd));
  663. cmd->tvc_sgl = sg;
  664. cmd->tvc_prot_sgl = prot_sg;
  665. cmd->tvc_upages = pages;
  666. cmd->tvc_se_cmd.map_tag = tag;
  667. cmd->tvc_tag = scsi_tag;
  668. cmd->tvc_lun = lun;
  669. cmd->tvc_task_attr = task_attr;
  670. cmd->tvc_exp_data_len = exp_data_len;
  671. cmd->tvc_data_direction = data_direction;
  672. cmd->tvc_nexus = tv_nexus;
  673. cmd->inflight = vhost_scsi_get_inflight(vq);
  674. memcpy(cmd->tvc_cdb, cdb, VHOST_SCSI_MAX_CDB_SIZE);
  675. return cmd;
  676. }
  677. /*
  678. * Map a user memory range into a scatterlist
  679. *
  680. * Returns the number of scatterlist entries used or -errno on error.
  681. */
  682. static int
  683. vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd,
  684. void __user *ptr,
  685. size_t len,
  686. struct scatterlist *sgl,
  687. bool write)
  688. {
  689. unsigned int npages = 0, offset, nbytes;
  690. unsigned int pages_nr = iov_num_pages(ptr, len);
  691. struct scatterlist *sg = sgl;
  692. struct page **pages = cmd->tvc_upages;
  693. int ret, i;
  694. if (pages_nr > VHOST_SCSI_PREALLOC_UPAGES) {
  695. pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
  696. " preallocated VHOST_SCSI_PREALLOC_UPAGES: %u\n",
  697. pages_nr, VHOST_SCSI_PREALLOC_UPAGES);
  698. return -ENOBUFS;
  699. }
  700. ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
  701. /* No pages were pinned */
  702. if (ret < 0)
  703. goto out;
  704. /* Less pages pinned than wanted */
  705. if (ret != pages_nr) {
  706. for (i = 0; i < ret; i++)
  707. put_page(pages[i]);
  708. ret = -EFAULT;
  709. goto out;
  710. }
  711. while (len > 0) {
  712. offset = (uintptr_t)ptr & ~PAGE_MASK;
  713. nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
  714. sg_set_page(sg, pages[npages], nbytes, offset);
  715. ptr += nbytes;
  716. len -= nbytes;
  717. sg++;
  718. npages++;
  719. }
  720. out:
  721. return ret;
  722. }
  723. static int
  724. vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls)
  725. {
  726. int sgl_count = 0;
  727. if (!iter || !iter->iov) {
  728. pr_err("%s: iter->iov is NULL, but expected bytes: %zu"
  729. " present\n", __func__, bytes);
  730. return -EINVAL;
  731. }
  732. sgl_count = iov_iter_npages(iter, 0xffff);
  733. if (sgl_count > max_sgls) {
  734. pr_err("%s: requested sgl_count: %d exceeds pre-allocated"
  735. " max_sgls: %d\n", __func__, sgl_count, max_sgls);
  736. return -EINVAL;
  737. }
  738. return sgl_count;
  739. }
  740. static int
  741. vhost_scsi_iov_to_sgl(struct vhost_scsi_cmd *cmd, bool write,
  742. struct iov_iter *iter,
  743. struct scatterlist *sg, int sg_count)
  744. {
  745. size_t off = iter->iov_offset;
  746. int i, ret;
  747. for (i = 0; i < iter->nr_segs; i++) {
  748. void __user *base = iter->iov[i].iov_base + off;
  749. size_t len = iter->iov[i].iov_len - off;
  750. ret = vhost_scsi_map_to_sgl(cmd, base, len, sg, write);
  751. if (ret < 0) {
  752. for (i = 0; i < sg_count; i++) {
  753. struct page *page = sg_page(&sg[i]);
  754. if (page)
  755. put_page(page);
  756. }
  757. return ret;
  758. }
  759. sg += ret;
  760. off = 0;
  761. }
  762. return 0;
  763. }
  764. static int
  765. vhost_scsi_mapal(struct vhost_scsi_cmd *cmd,
  766. size_t prot_bytes, struct iov_iter *prot_iter,
  767. size_t data_bytes, struct iov_iter *data_iter)
  768. {
  769. int sgl_count, ret;
  770. bool write = (cmd->tvc_data_direction == DMA_FROM_DEVICE);
  771. if (prot_bytes) {
  772. sgl_count = vhost_scsi_calc_sgls(prot_iter, prot_bytes,
  773. VHOST_SCSI_PREALLOC_PROT_SGLS);
  774. if (sgl_count < 0)
  775. return sgl_count;
  776. sg_init_table(cmd->tvc_prot_sgl, sgl_count);
  777. cmd->tvc_prot_sgl_count = sgl_count;
  778. pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__,
  779. cmd->tvc_prot_sgl, cmd->tvc_prot_sgl_count);
  780. ret = vhost_scsi_iov_to_sgl(cmd, write, prot_iter,
  781. cmd->tvc_prot_sgl,
  782. cmd->tvc_prot_sgl_count);
  783. if (ret < 0) {
  784. cmd->tvc_prot_sgl_count = 0;
  785. return ret;
  786. }
  787. }
  788. sgl_count = vhost_scsi_calc_sgls(data_iter, data_bytes,
  789. VHOST_SCSI_PREALLOC_SGLS);
  790. if (sgl_count < 0)
  791. return sgl_count;
  792. sg_init_table(cmd->tvc_sgl, sgl_count);
  793. cmd->tvc_sgl_count = sgl_count;
  794. pr_debug("%s data_sg %p data_sgl_count %u\n", __func__,
  795. cmd->tvc_sgl, cmd->tvc_sgl_count);
  796. ret = vhost_scsi_iov_to_sgl(cmd, write, data_iter,
  797. cmd->tvc_sgl, cmd->tvc_sgl_count);
  798. if (ret < 0) {
  799. cmd->tvc_sgl_count = 0;
  800. return ret;
  801. }
  802. return 0;
  803. }
  804. static int vhost_scsi_to_tcm_attr(int attr)
  805. {
  806. switch (attr) {
  807. case VIRTIO_SCSI_S_SIMPLE:
  808. return TCM_SIMPLE_TAG;
  809. case VIRTIO_SCSI_S_ORDERED:
  810. return TCM_ORDERED_TAG;
  811. case VIRTIO_SCSI_S_HEAD:
  812. return TCM_HEAD_TAG;
  813. case VIRTIO_SCSI_S_ACA:
  814. return TCM_ACA_TAG;
  815. default:
  816. break;
  817. }
  818. return TCM_SIMPLE_TAG;
  819. }
  820. static void vhost_scsi_submission_work(struct work_struct *work)
  821. {
  822. struct vhost_scsi_cmd *cmd =
  823. container_of(work, struct vhost_scsi_cmd, work);
  824. struct vhost_scsi_nexus *tv_nexus;
  825. struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
  826. struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
  827. int rc;
  828. /* FIXME: BIDI operation */
  829. if (cmd->tvc_sgl_count) {
  830. sg_ptr = cmd->tvc_sgl;
  831. if (cmd->tvc_prot_sgl_count)
  832. sg_prot_ptr = cmd->tvc_prot_sgl;
  833. else
  834. se_cmd->prot_pto = true;
  835. } else {
  836. sg_ptr = NULL;
  837. }
  838. tv_nexus = cmd->tvc_nexus;
  839. rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
  840. cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
  841. cmd->tvc_lun, cmd->tvc_exp_data_len,
  842. vhost_scsi_to_tcm_attr(cmd->tvc_task_attr),
  843. cmd->tvc_data_direction, TARGET_SCF_ACK_KREF,
  844. sg_ptr, cmd->tvc_sgl_count, NULL, 0, sg_prot_ptr,
  845. cmd->tvc_prot_sgl_count);
  846. if (rc < 0) {
  847. transport_send_check_condition_and_sense(se_cmd,
  848. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  849. transport_generic_free_cmd(se_cmd, 0);
  850. }
  851. }
  852. static void
  853. vhost_scsi_send_bad_target(struct vhost_scsi *vs,
  854. struct vhost_virtqueue *vq,
  855. int head, unsigned out)
  856. {
  857. struct virtio_scsi_cmd_resp __user *resp;
  858. struct virtio_scsi_cmd_resp rsp;
  859. int ret;
  860. memset(&rsp, 0, sizeof(rsp));
  861. rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
  862. resp = vq->iov[out].iov_base;
  863. ret = __copy_to_user(resp, &rsp, sizeof(rsp));
  864. if (!ret)
  865. vhost_add_used_and_signal(&vs->dev, vq, head, 0);
  866. else
  867. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  868. }
  869. static void
  870. vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
  871. {
  872. struct vhost_scsi_tpg **vs_tpg, *tpg;
  873. struct virtio_scsi_cmd_req v_req;
  874. struct virtio_scsi_cmd_req_pi v_req_pi;
  875. struct vhost_scsi_cmd *cmd;
  876. struct iov_iter out_iter, in_iter, prot_iter, data_iter;
  877. u64 tag;
  878. u32 exp_data_len, data_direction;
  879. unsigned out, in;
  880. int head, ret, prot_bytes;
  881. size_t req_size, rsp_size = sizeof(struct virtio_scsi_cmd_resp);
  882. size_t out_size, in_size;
  883. u16 lun;
  884. u8 *target, *lunp, task_attr;
  885. bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI);
  886. void *req, *cdb;
  887. mutex_lock(&vq->mutex);
  888. /*
  889. * We can handle the vq only after the endpoint is setup by calling the
  890. * VHOST_SCSI_SET_ENDPOINT ioctl.
  891. */
  892. vs_tpg = vq->private_data;
  893. if (!vs_tpg)
  894. goto out;
  895. vhost_disable_notify(&vs->dev, vq);
  896. for (;;) {
  897. head = vhost_get_vq_desc(vq, vq->iov,
  898. ARRAY_SIZE(vq->iov), &out, &in,
  899. NULL, NULL);
  900. pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
  901. head, out, in);
  902. /* On error, stop handling until the next kick. */
  903. if (unlikely(head < 0))
  904. break;
  905. /* Nothing new? Wait for eventfd to tell us they refilled. */
  906. if (head == vq->num) {
  907. if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
  908. vhost_disable_notify(&vs->dev, vq);
  909. continue;
  910. }
  911. break;
  912. }
  913. /*
  914. * Check for a sane response buffer so we can report early
  915. * errors back to the guest.
  916. */
  917. if (unlikely(vq->iov[out].iov_len < rsp_size)) {
  918. vq_err(vq, "Expecting at least virtio_scsi_cmd_resp"
  919. " size, got %zu bytes\n", vq->iov[out].iov_len);
  920. break;
  921. }
  922. /*
  923. * Setup pointers and values based upon different virtio-scsi
  924. * request header if T10_PI is enabled in KVM guest.
  925. */
  926. if (t10_pi) {
  927. req = &v_req_pi;
  928. req_size = sizeof(v_req_pi);
  929. lunp = &v_req_pi.lun[0];
  930. target = &v_req_pi.lun[1];
  931. } else {
  932. req = &v_req;
  933. req_size = sizeof(v_req);
  934. lunp = &v_req.lun[0];
  935. target = &v_req.lun[1];
  936. }
  937. /*
  938. * FIXME: Not correct for BIDI operation
  939. */
  940. out_size = iov_length(vq->iov, out);
  941. in_size = iov_length(&vq->iov[out], in);
  942. /*
  943. * Copy over the virtio-scsi request header, which for a
  944. * ANY_LAYOUT enabled guest may span multiple iovecs, or a
  945. * single iovec may contain both the header + outgoing
  946. * WRITE payloads.
  947. *
  948. * copy_from_iter() will advance out_iter, so that it will
  949. * point at the start of the outgoing WRITE payload, if
  950. * DMA_TO_DEVICE is set.
  951. */
  952. iov_iter_init(&out_iter, WRITE, vq->iov, out, out_size);
  953. ret = copy_from_iter(req, req_size, &out_iter);
  954. if (unlikely(ret != req_size)) {
  955. vq_err(vq, "Faulted on copy_from_iter\n");
  956. vhost_scsi_send_bad_target(vs, vq, head, out);
  957. continue;
  958. }
  959. /* virtio-scsi spec requires byte 0 of the lun to be 1 */
  960. if (unlikely(*lunp != 1)) {
  961. vq_err(vq, "Illegal virtio-scsi lun: %u\n", *lunp);
  962. vhost_scsi_send_bad_target(vs, vq, head, out);
  963. continue;
  964. }
  965. tpg = ACCESS_ONCE(vs_tpg[*target]);
  966. if (unlikely(!tpg)) {
  967. /* Target does not exist, fail the request */
  968. vhost_scsi_send_bad_target(vs, vq, head, out);
  969. continue;
  970. }
  971. /*
  972. * Determine data_direction by calculating the total outgoing
  973. * iovec sizes + incoming iovec sizes vs. virtio-scsi request +
  974. * response headers respectively.
  975. *
  976. * For DMA_TO_DEVICE this is out_iter, which is already pointing
  977. * to the right place.
  978. *
  979. * For DMA_FROM_DEVICE, the iovec will be just past the end
  980. * of the virtio-scsi response header in either the same
  981. * or immediately following iovec.
  982. *
  983. * Any associated T10_PI bytes for the outgoing / incoming
  984. * payloads are included in calculation of exp_data_len here.
  985. */
  986. prot_bytes = 0;
  987. if (out_size > req_size) {
  988. data_direction = DMA_TO_DEVICE;
  989. exp_data_len = out_size - req_size;
  990. data_iter = out_iter;
  991. } else if (in_size > rsp_size) {
  992. data_direction = DMA_FROM_DEVICE;
  993. exp_data_len = in_size - rsp_size;
  994. iov_iter_init(&in_iter, READ, &vq->iov[out], in,
  995. rsp_size + exp_data_len);
  996. iov_iter_advance(&in_iter, rsp_size);
  997. data_iter = in_iter;
  998. } else {
  999. data_direction = DMA_NONE;
  1000. exp_data_len = 0;
  1001. }
  1002. /*
  1003. * If T10_PI header + payload is present, setup prot_iter values
  1004. * and recalculate data_iter for vhost_scsi_mapal() mapping to
  1005. * host scatterlists via get_user_pages_fast().
  1006. */
  1007. if (t10_pi) {
  1008. if (v_req_pi.pi_bytesout) {
  1009. if (data_direction != DMA_TO_DEVICE) {
  1010. vq_err(vq, "Received non zero pi_bytesout,"
  1011. " but wrong data_direction\n");
  1012. vhost_scsi_send_bad_target(vs, vq, head, out);
  1013. continue;
  1014. }
  1015. prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout);
  1016. } else if (v_req_pi.pi_bytesin) {
  1017. if (data_direction != DMA_FROM_DEVICE) {
  1018. vq_err(vq, "Received non zero pi_bytesin,"
  1019. " but wrong data_direction\n");
  1020. vhost_scsi_send_bad_target(vs, vq, head, out);
  1021. continue;
  1022. }
  1023. prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin);
  1024. }
  1025. /*
  1026. * Set prot_iter to data_iter, and advance past any
  1027. * preceeding prot_bytes that may be present.
  1028. *
  1029. * Also fix up the exp_data_len to reflect only the
  1030. * actual data payload length.
  1031. */
  1032. if (prot_bytes) {
  1033. exp_data_len -= prot_bytes;
  1034. prot_iter = data_iter;
  1035. iov_iter_advance(&data_iter, prot_bytes);
  1036. }
  1037. tag = vhost64_to_cpu(vq, v_req_pi.tag);
  1038. task_attr = v_req_pi.task_attr;
  1039. cdb = &v_req_pi.cdb[0];
  1040. lun = ((v_req_pi.lun[2] << 8) | v_req_pi.lun[3]) & 0x3FFF;
  1041. } else {
  1042. tag = vhost64_to_cpu(vq, v_req.tag);
  1043. task_attr = v_req.task_attr;
  1044. cdb = &v_req.cdb[0];
  1045. lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
  1046. }
  1047. /*
  1048. * Check that the received CDB size does not exceeded our
  1049. * hardcoded max for vhost-scsi, then get a pre-allocated
  1050. * cmd descriptor for the new virtio-scsi tag.
  1051. *
  1052. * TODO what if cdb was too small for varlen cdb header?
  1053. */
  1054. if (unlikely(scsi_command_size(cdb) > VHOST_SCSI_MAX_CDB_SIZE)) {
  1055. vq_err(vq, "Received SCSI CDB with command_size: %d that"
  1056. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1057. scsi_command_size(cdb), VHOST_SCSI_MAX_CDB_SIZE);
  1058. vhost_scsi_send_bad_target(vs, vq, head, out);
  1059. continue;
  1060. }
  1061. cmd = vhost_scsi_get_tag(vq, tpg, cdb, tag, lun, task_attr,
  1062. exp_data_len + prot_bytes,
  1063. data_direction);
  1064. if (IS_ERR(cmd)) {
  1065. vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
  1066. PTR_ERR(cmd));
  1067. vhost_scsi_send_bad_target(vs, vq, head, out);
  1068. continue;
  1069. }
  1070. cmd->tvc_vhost = vs;
  1071. cmd->tvc_vq = vq;
  1072. cmd->tvc_resp_iov = &vq->iov[out];
  1073. cmd->tvc_in_iovs = in;
  1074. pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
  1075. cmd->tvc_cdb[0], cmd->tvc_lun);
  1076. pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
  1077. " %d\n", cmd, exp_data_len, prot_bytes, data_direction);
  1078. if (data_direction != DMA_NONE) {
  1079. ret = vhost_scsi_mapal(cmd,
  1080. prot_bytes, &prot_iter,
  1081. exp_data_len, &data_iter);
  1082. if (unlikely(ret)) {
  1083. vq_err(vq, "Failed to map iov to sgl\n");
  1084. vhost_scsi_release_cmd(&cmd->tvc_se_cmd);
  1085. vhost_scsi_send_bad_target(vs, vq, head, out);
  1086. continue;
  1087. }
  1088. }
  1089. /*
  1090. * Save the descriptor from vhost_get_vq_desc() to be used to
  1091. * complete the virtio-scsi request in TCM callback context via
  1092. * vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
  1093. */
  1094. cmd->tvc_vq_desc = head;
  1095. /*
  1096. * Dispatch cmd descriptor for cmwq execution in process
  1097. * context provided by vhost_scsi_workqueue. This also ensures
  1098. * cmd is executed on the same kworker CPU as this vhost
  1099. * thread to gain positive L2 cache locality effects.
  1100. */
  1101. INIT_WORK(&cmd->work, vhost_scsi_submission_work);
  1102. queue_work(vhost_scsi_workqueue, &cmd->work);
  1103. }
  1104. out:
  1105. mutex_unlock(&vq->mutex);
  1106. }
  1107. static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
  1108. {
  1109. pr_debug("%s: The handling func for control queue.\n", __func__);
  1110. }
  1111. static void
  1112. vhost_scsi_send_evt(struct vhost_scsi *vs,
  1113. struct vhost_scsi_tpg *tpg,
  1114. struct se_lun *lun,
  1115. u32 event,
  1116. u32 reason)
  1117. {
  1118. struct vhost_scsi_evt *evt;
  1119. evt = vhost_scsi_allocate_evt(vs, event, reason);
  1120. if (!evt)
  1121. return;
  1122. if (tpg && lun) {
  1123. /* TODO: share lun setup code with virtio-scsi.ko */
  1124. /*
  1125. * Note: evt->event is zeroed when we allocate it and
  1126. * lun[4-7] need to be zero according to virtio-scsi spec.
  1127. */
  1128. evt->event.lun[0] = 0x01;
  1129. evt->event.lun[1] = tpg->tport_tpgt;
  1130. if (lun->unpacked_lun >= 256)
  1131. evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
  1132. evt->event.lun[3] = lun->unpacked_lun & 0xFF;
  1133. }
  1134. llist_add(&evt->list, &vs->vs_event_list);
  1135. vhost_work_queue(&vs->dev, &vs->vs_event_work);
  1136. }
  1137. static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
  1138. {
  1139. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1140. poll.work);
  1141. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  1142. mutex_lock(&vq->mutex);
  1143. if (!vq->private_data)
  1144. goto out;
  1145. if (vs->vs_events_missed)
  1146. vhost_scsi_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
  1147. out:
  1148. mutex_unlock(&vq->mutex);
  1149. }
  1150. static void vhost_scsi_handle_kick(struct vhost_work *work)
  1151. {
  1152. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1153. poll.work);
  1154. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  1155. vhost_scsi_handle_vq(vs, vq);
  1156. }
  1157. static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
  1158. {
  1159. vhost_poll_flush(&vs->vqs[index].vq.poll);
  1160. }
  1161. /* Callers must hold dev mutex */
  1162. static void vhost_scsi_flush(struct vhost_scsi *vs)
  1163. {
  1164. struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
  1165. int i;
  1166. /* Init new inflight and remember the old inflight */
  1167. vhost_scsi_init_inflight(vs, old_inflight);
  1168. /*
  1169. * The inflight->kref was initialized to 1. We decrement it here to
  1170. * indicate the start of the flush operation so that it will reach 0
  1171. * when all the reqs are finished.
  1172. */
  1173. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1174. kref_put(&old_inflight[i]->kref, vhost_scsi_done_inflight);
  1175. /* Flush both the vhost poll and vhost work */
  1176. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1177. vhost_scsi_flush_vq(vs, i);
  1178. vhost_work_flush(&vs->dev, &vs->vs_completion_work);
  1179. vhost_work_flush(&vs->dev, &vs->vs_event_work);
  1180. /* Wait for all reqs issued before the flush to be finished */
  1181. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1182. wait_for_completion(&old_inflight[i]->comp);
  1183. }
  1184. /*
  1185. * Called from vhost_scsi_ioctl() context to walk the list of available
  1186. * vhost_scsi_tpg with an active struct vhost_scsi_nexus
  1187. *
  1188. * The lock nesting rule is:
  1189. * vhost_scsi_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
  1190. */
  1191. static int
  1192. vhost_scsi_set_endpoint(struct vhost_scsi *vs,
  1193. struct vhost_scsi_target *t)
  1194. {
  1195. struct se_portal_group *se_tpg;
  1196. struct vhost_scsi_tport *tv_tport;
  1197. struct vhost_scsi_tpg *tpg;
  1198. struct vhost_scsi_tpg **vs_tpg;
  1199. struct vhost_virtqueue *vq;
  1200. int index, ret, i, len;
  1201. bool match = false;
  1202. mutex_lock(&vhost_scsi_mutex);
  1203. mutex_lock(&vs->dev.mutex);
  1204. /* Verify that ring has been setup correctly. */
  1205. for (index = 0; index < vs->dev.nvqs; ++index) {
  1206. /* Verify that ring has been setup correctly. */
  1207. if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
  1208. ret = -EFAULT;
  1209. goto out;
  1210. }
  1211. }
  1212. len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
  1213. vs_tpg = kzalloc(len, GFP_KERNEL);
  1214. if (!vs_tpg) {
  1215. ret = -ENOMEM;
  1216. goto out;
  1217. }
  1218. if (vs->vs_tpg)
  1219. memcpy(vs_tpg, vs->vs_tpg, len);
  1220. list_for_each_entry(tpg, &vhost_scsi_list, tv_tpg_list) {
  1221. mutex_lock(&tpg->tv_tpg_mutex);
  1222. if (!tpg->tpg_nexus) {
  1223. mutex_unlock(&tpg->tv_tpg_mutex);
  1224. continue;
  1225. }
  1226. if (tpg->tv_tpg_vhost_count != 0) {
  1227. mutex_unlock(&tpg->tv_tpg_mutex);
  1228. continue;
  1229. }
  1230. tv_tport = tpg->tport;
  1231. if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  1232. if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
  1233. kfree(vs_tpg);
  1234. mutex_unlock(&tpg->tv_tpg_mutex);
  1235. ret = -EEXIST;
  1236. goto out;
  1237. }
  1238. /*
  1239. * In order to ensure individual vhost-scsi configfs
  1240. * groups cannot be removed while in use by vhost ioctl,
  1241. * go ahead and take an explicit se_tpg->tpg_group.cg_item
  1242. * dependency now.
  1243. */
  1244. se_tpg = &tpg->se_tpg;
  1245. ret = configfs_depend_item(se_tpg->se_tpg_tfo->tf_subsys,
  1246. &se_tpg->tpg_group.cg_item);
  1247. if (ret) {
  1248. pr_warn("configfs_depend_item() failed: %d\n", ret);
  1249. kfree(vs_tpg);
  1250. mutex_unlock(&tpg->tv_tpg_mutex);
  1251. goto out;
  1252. }
  1253. tpg->tv_tpg_vhost_count++;
  1254. tpg->vhost_scsi = vs;
  1255. vs_tpg[tpg->tport_tpgt] = tpg;
  1256. smp_mb__after_atomic();
  1257. match = true;
  1258. }
  1259. mutex_unlock(&tpg->tv_tpg_mutex);
  1260. }
  1261. if (match) {
  1262. memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
  1263. sizeof(vs->vs_vhost_wwpn));
  1264. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1265. vq = &vs->vqs[i].vq;
  1266. mutex_lock(&vq->mutex);
  1267. vq->private_data = vs_tpg;
  1268. vhost_init_used(vq);
  1269. mutex_unlock(&vq->mutex);
  1270. }
  1271. ret = 0;
  1272. } else {
  1273. ret = -EEXIST;
  1274. }
  1275. /*
  1276. * Act as synchronize_rcu to make sure access to
  1277. * old vs->vs_tpg is finished.
  1278. */
  1279. vhost_scsi_flush(vs);
  1280. kfree(vs->vs_tpg);
  1281. vs->vs_tpg = vs_tpg;
  1282. out:
  1283. mutex_unlock(&vs->dev.mutex);
  1284. mutex_unlock(&vhost_scsi_mutex);
  1285. return ret;
  1286. }
  1287. static int
  1288. vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
  1289. struct vhost_scsi_target *t)
  1290. {
  1291. struct se_portal_group *se_tpg;
  1292. struct vhost_scsi_tport *tv_tport;
  1293. struct vhost_scsi_tpg *tpg;
  1294. struct vhost_virtqueue *vq;
  1295. bool match = false;
  1296. int index, ret, i;
  1297. u8 target;
  1298. mutex_lock(&vhost_scsi_mutex);
  1299. mutex_lock(&vs->dev.mutex);
  1300. /* Verify that ring has been setup correctly. */
  1301. for (index = 0; index < vs->dev.nvqs; ++index) {
  1302. if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
  1303. ret = -EFAULT;
  1304. goto err_dev;
  1305. }
  1306. }
  1307. if (!vs->vs_tpg) {
  1308. ret = 0;
  1309. goto err_dev;
  1310. }
  1311. for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
  1312. target = i;
  1313. tpg = vs->vs_tpg[target];
  1314. if (!tpg)
  1315. continue;
  1316. mutex_lock(&tpg->tv_tpg_mutex);
  1317. tv_tport = tpg->tport;
  1318. if (!tv_tport) {
  1319. ret = -ENODEV;
  1320. goto err_tpg;
  1321. }
  1322. if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  1323. pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
  1324. " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
  1325. tv_tport->tport_name, tpg->tport_tpgt,
  1326. t->vhost_wwpn, t->vhost_tpgt);
  1327. ret = -EINVAL;
  1328. goto err_tpg;
  1329. }
  1330. tpg->tv_tpg_vhost_count--;
  1331. tpg->vhost_scsi = NULL;
  1332. vs->vs_tpg[target] = NULL;
  1333. match = true;
  1334. mutex_unlock(&tpg->tv_tpg_mutex);
  1335. /*
  1336. * Release se_tpg->tpg_group.cg_item configfs dependency now
  1337. * to allow vhost-scsi WWPN se_tpg->tpg_group shutdown to occur.
  1338. */
  1339. se_tpg = &tpg->se_tpg;
  1340. configfs_undepend_item(se_tpg->se_tpg_tfo->tf_subsys,
  1341. &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);