scsi.c 56 KB

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