tcm_vhost.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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_net.h> /* TODO vhost.h currently depends on this */
  48. #include <linux/virtio_scsi.h>
  49. #include <linux/llist.h>
  50. #include "vhost.c"
  51. #include "vhost.h"
  52. #include "tcm_vhost.h"
  53. enum {
  54. VHOST_SCSI_VQ_CTL = 0,
  55. VHOST_SCSI_VQ_EVT = 1,
  56. VHOST_SCSI_VQ_IO = 2,
  57. };
  58. struct vhost_scsi {
  59. struct tcm_vhost_tpg *vs_tpg; /* Protected by vhost_scsi->dev.mutex */
  60. struct vhost_dev dev;
  61. struct vhost_virtqueue vqs[3];
  62. struct vhost_work vs_completion_work; /* cmd completion work item */
  63. struct llist_head vs_completion_list; /* cmd completion queue */
  64. };
  65. /* Local pointer to allocated TCM configfs fabric module */
  66. static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
  67. static struct workqueue_struct *tcm_vhost_workqueue;
  68. /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
  69. static DEFINE_MUTEX(tcm_vhost_mutex);
  70. static LIST_HEAD(tcm_vhost_list);
  71. static int iov_num_pages(struct iovec *iov)
  72. {
  73. return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
  74. ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
  75. }
  76. static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
  77. {
  78. return 1;
  79. }
  80. static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
  81. {
  82. return 0;
  83. }
  84. static char *tcm_vhost_get_fabric_name(void)
  85. {
  86. return "vhost";
  87. }
  88. static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  89. {
  90. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  91. struct tcm_vhost_tpg, se_tpg);
  92. struct tcm_vhost_tport *tport = tpg->tport;
  93. switch (tport->tport_proto_id) {
  94. case SCSI_PROTOCOL_SAS:
  95. return sas_get_fabric_proto_ident(se_tpg);
  96. case SCSI_PROTOCOL_FCP:
  97. return fc_get_fabric_proto_ident(se_tpg);
  98. case SCSI_PROTOCOL_ISCSI:
  99. return iscsi_get_fabric_proto_ident(se_tpg);
  100. default:
  101. pr_err("Unknown tport_proto_id: 0x%02x, using"
  102. " SAS emulation\n", tport->tport_proto_id);
  103. break;
  104. }
  105. return sas_get_fabric_proto_ident(se_tpg);
  106. }
  107. static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
  108. {
  109. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  110. struct tcm_vhost_tpg, se_tpg);
  111. struct tcm_vhost_tport *tport = tpg->tport;
  112. return &tport->tport_name[0];
  113. }
  114. static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
  115. {
  116. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  117. struct tcm_vhost_tpg, se_tpg);
  118. return tpg->tport_tpgt;
  119. }
  120. static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
  121. {
  122. return 1;
  123. }
  124. static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
  125. struct se_node_acl *se_nacl,
  126. struct t10_pr_registration *pr_reg,
  127. int *format_code,
  128. unsigned char *buf)
  129. {
  130. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  131. struct tcm_vhost_tpg, se_tpg);
  132. struct tcm_vhost_tport *tport = tpg->tport;
  133. switch (tport->tport_proto_id) {
  134. case SCSI_PROTOCOL_SAS:
  135. return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  136. format_code, buf);
  137. case SCSI_PROTOCOL_FCP:
  138. return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  139. format_code, buf);
  140. case SCSI_PROTOCOL_ISCSI:
  141. return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  142. format_code, buf);
  143. default:
  144. pr_err("Unknown tport_proto_id: 0x%02x, using"
  145. " SAS emulation\n", tport->tport_proto_id);
  146. break;
  147. }
  148. return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  149. format_code, buf);
  150. }
  151. static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
  152. struct se_node_acl *se_nacl,
  153. struct t10_pr_registration *pr_reg,
  154. int *format_code)
  155. {
  156. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  157. struct tcm_vhost_tpg, se_tpg);
  158. struct tcm_vhost_tport *tport = tpg->tport;
  159. switch (tport->tport_proto_id) {
  160. case SCSI_PROTOCOL_SAS:
  161. return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  162. format_code);
  163. case SCSI_PROTOCOL_FCP:
  164. return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  165. format_code);
  166. case SCSI_PROTOCOL_ISCSI:
  167. return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  168. format_code);
  169. default:
  170. pr_err("Unknown tport_proto_id: 0x%02x, using"
  171. " SAS emulation\n", tport->tport_proto_id);
  172. break;
  173. }
  174. return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  175. format_code);
  176. }
  177. static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
  178. const char *buf,
  179. u32 *out_tid_len,
  180. char **port_nexus_ptr)
  181. {
  182. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  183. struct tcm_vhost_tpg, se_tpg);
  184. struct tcm_vhost_tport *tport = tpg->tport;
  185. switch (tport->tport_proto_id) {
  186. case SCSI_PROTOCOL_SAS:
  187. return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  188. port_nexus_ptr);
  189. case SCSI_PROTOCOL_FCP:
  190. return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  191. port_nexus_ptr);
  192. case SCSI_PROTOCOL_ISCSI:
  193. return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  194. port_nexus_ptr);
  195. default:
  196. pr_err("Unknown tport_proto_id: 0x%02x, using"
  197. " SAS emulation\n", tport->tport_proto_id);
  198. break;
  199. }
  200. return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  201. port_nexus_ptr);
  202. }
  203. static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
  204. struct se_portal_group *se_tpg)
  205. {
  206. struct tcm_vhost_nacl *nacl;
  207. nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
  208. if (!nacl) {
  209. pr_err("Unable to allocate struct tcm_vhost_nacl\n");
  210. return NULL;
  211. }
  212. return &nacl->se_node_acl;
  213. }
  214. static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
  215. struct se_node_acl *se_nacl)
  216. {
  217. struct tcm_vhost_nacl *nacl = container_of(se_nacl,
  218. struct tcm_vhost_nacl, se_node_acl);
  219. kfree(nacl);
  220. }
  221. static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
  222. {
  223. return 1;
  224. }
  225. static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
  226. {
  227. return;
  228. }
  229. static int tcm_vhost_shutdown_session(struct se_session *se_sess)
  230. {
  231. return 0;
  232. }
  233. static void tcm_vhost_close_session(struct se_session *se_sess)
  234. {
  235. return;
  236. }
  237. static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
  238. {
  239. return 0;
  240. }
  241. static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
  242. {
  243. /* Go ahead and process the write immediately */
  244. target_execute_cmd(se_cmd);
  245. return 0;
  246. }
  247. static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
  248. {
  249. return 0;
  250. }
  251. static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
  252. {
  253. return;
  254. }
  255. static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
  256. {
  257. return 0;
  258. }
  259. static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
  260. {
  261. return 0;
  262. }
  263. static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
  264. {
  265. struct vhost_scsi *vs = tv_cmd->tvc_vhost;
  266. llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
  267. vhost_work_queue(&vs->dev, &vs->vs_completion_work);
  268. }
  269. static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
  270. {
  271. struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
  272. struct tcm_vhost_cmd, tvc_se_cmd);
  273. vhost_scsi_complete_cmd(tv_cmd);
  274. return 0;
  275. }
  276. static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
  277. {
  278. struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
  279. struct tcm_vhost_cmd, tvc_se_cmd);
  280. vhost_scsi_complete_cmd(tv_cmd);
  281. return 0;
  282. }
  283. static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
  284. {
  285. return 0;
  286. }
  287. static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
  288. {
  289. struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
  290. /* TODO locking against target/backend threads? */
  291. transport_generic_free_cmd(se_cmd, 1);
  292. if (tv_cmd->tvc_sgl_count) {
  293. u32 i;
  294. for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
  295. put_page(sg_page(&tv_cmd->tvc_sgl[i]));
  296. kfree(tv_cmd->tvc_sgl);
  297. }
  298. kfree(tv_cmd);
  299. }
  300. /* Fill in status and signal that we are done processing this command
  301. *
  302. * This is scheduled in the vhost work queue so we are called with the owner
  303. * process mm and can access the vring.
  304. */
  305. static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
  306. {
  307. struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
  308. vs_completion_work);
  309. struct virtio_scsi_cmd_resp v_rsp;
  310. struct tcm_vhost_cmd *tv_cmd;
  311. struct llist_node *llnode;
  312. struct se_cmd *se_cmd;
  313. int ret;
  314. llnode = llist_del_all(&vs->vs_completion_list);
  315. while (llnode) {
  316. tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd,
  317. tvc_completion_list);
  318. llnode = llist_next(llnode);
  319. se_cmd = &tv_cmd->tvc_se_cmd;
  320. pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
  321. tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
  322. memset(&v_rsp, 0, sizeof(v_rsp));
  323. v_rsp.resid = se_cmd->residual_count;
  324. /* TODO is status_qualifier field needed? */
  325. v_rsp.status = se_cmd->scsi_status;
  326. v_rsp.sense_len = se_cmd->scsi_sense_length;
  327. memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
  328. v_rsp.sense_len);
  329. ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
  330. if (likely(ret == 0))
  331. vhost_add_used(&vs->vqs[2], tv_cmd->tvc_vq_desc, 0);
  332. else
  333. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  334. vhost_scsi_free_cmd(tv_cmd);
  335. }
  336. vhost_signal(&vs->dev, &vs->vqs[2]);
  337. }
  338. static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
  339. struct tcm_vhost_tpg *tv_tpg,
  340. struct virtio_scsi_cmd_req *v_req,
  341. u32 exp_data_len,
  342. int data_direction)
  343. {
  344. struct tcm_vhost_cmd *tv_cmd;
  345. struct tcm_vhost_nexus *tv_nexus;
  346. tv_nexus = tv_tpg->tpg_nexus;
  347. if (!tv_nexus) {
  348. pr_err("Unable to locate active struct tcm_vhost_nexus\n");
  349. return ERR_PTR(-EIO);
  350. }
  351. tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
  352. if (!tv_cmd) {
  353. pr_err("Unable to allocate struct tcm_vhost_cmd\n");
  354. return ERR_PTR(-ENOMEM);
  355. }
  356. tv_cmd->tvc_tag = v_req->tag;
  357. tv_cmd->tvc_task_attr = v_req->task_attr;
  358. tv_cmd->tvc_exp_data_len = exp_data_len;
  359. tv_cmd->tvc_data_direction = data_direction;
  360. tv_cmd->tvc_nexus = tv_nexus;
  361. return tv_cmd;
  362. }
  363. /*
  364. * Map a user memory range into a scatterlist
  365. *
  366. * Returns the number of scatterlist entries used or -errno on error.
  367. */
  368. static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
  369. unsigned int sgl_count, void __user *ptr, size_t len, int write)
  370. {
  371. struct scatterlist *sg = sgl;
  372. unsigned int npages = 0;
  373. int ret;
  374. while (len > 0) {
  375. struct page *page;
  376. unsigned int offset = (uintptr_t)ptr & ~PAGE_MASK;
  377. unsigned int nbytes = min_t(unsigned int,
  378. PAGE_SIZE - offset, len);
  379. if (npages == sgl_count) {
  380. ret = -ENOBUFS;
  381. goto err;
  382. }
  383. ret = get_user_pages_fast((unsigned long)ptr, 1, write, &page);
  384. BUG_ON(ret == 0); /* we should either get our page or fail */
  385. if (ret < 0)
  386. goto err;
  387. sg_set_page(sg, page, nbytes, offset);
  388. ptr += nbytes;
  389. len -= nbytes;
  390. sg++;
  391. npages++;
  392. }
  393. return npages;
  394. err:
  395. /* Put pages that we hold */
  396. for (sg = sgl; sg != &sgl[npages]; sg++)
  397. put_page(sg_page(sg));
  398. return ret;
  399. }
  400. static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
  401. struct iovec *iov, unsigned int niov, int write)
  402. {
  403. int ret;
  404. unsigned int i;
  405. u32 sgl_count;
  406. struct scatterlist *sg;
  407. /*
  408. * Find out how long sglist needs to be
  409. */
  410. sgl_count = 0;
  411. for (i = 0; i < niov; i++)
  412. sgl_count += iov_num_pages(&iov[i]);
  413. /* TODO overflow checking */
  414. sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
  415. if (!sg)
  416. return -ENOMEM;
  417. pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
  418. sg, sgl_count, !sg);
  419. sg_init_table(sg, sgl_count);
  420. tv_cmd->tvc_sgl = sg;
  421. tv_cmd->tvc_sgl_count = sgl_count;
  422. pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
  423. for (i = 0; i < niov; i++) {
  424. ret = vhost_scsi_map_to_sgl(sg, sgl_count, iov[i].iov_base,
  425. iov[i].iov_len, write);
  426. if (ret < 0) {
  427. for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
  428. put_page(sg_page(&tv_cmd->tvc_sgl[i]));
  429. kfree(tv_cmd->tvc_sgl);
  430. tv_cmd->tvc_sgl = NULL;
  431. tv_cmd->tvc_sgl_count = 0;
  432. return ret;
  433. }
  434. sg += ret;
  435. sgl_count -= ret;
  436. }
  437. return 0;
  438. }
  439. static void tcm_vhost_submission_work(struct work_struct *work)
  440. {
  441. struct tcm_vhost_cmd *tv_cmd =
  442. container_of(work, struct tcm_vhost_cmd, work);
  443. struct tcm_vhost_nexus *tv_nexus;
  444. struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
  445. struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
  446. int rc, sg_no_bidi = 0;
  447. if (tv_cmd->tvc_sgl_count) {
  448. sg_ptr = tv_cmd->tvc_sgl;
  449. /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
  450. #if 0
  451. if (se_cmd->se_cmd_flags & SCF_BIDI) {
  452. sg_bidi_ptr = NULL;
  453. sg_no_bidi = 0;
  454. }
  455. #endif
  456. } else {
  457. sg_ptr = NULL;
  458. }
  459. tv_nexus = tv_cmd->tvc_nexus;
  460. rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
  461. tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
  462. tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
  463. tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
  464. 0, sg_ptr, tv_cmd->tvc_sgl_count,
  465. sg_bidi_ptr, sg_no_bidi);
  466. if (rc < 0) {
  467. transport_send_check_condition_and_sense(se_cmd,
  468. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  469. transport_generic_free_cmd(se_cmd, 0);
  470. }
  471. }
  472. static void vhost_scsi_handle_vq(struct vhost_scsi *vs)
  473. {
  474. struct vhost_virtqueue *vq = &vs->vqs[2];
  475. struct virtio_scsi_cmd_req v_req;
  476. struct tcm_vhost_tpg *tv_tpg;
  477. struct tcm_vhost_cmd *tv_cmd;
  478. u32 exp_data_len, data_first, data_num, data_direction;
  479. unsigned out, in, i;
  480. int head, ret;
  481. /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
  482. tv_tpg = vs->vs_tpg;
  483. if (unlikely(!tv_tpg))
  484. return;
  485. mutex_lock(&vq->mutex);
  486. vhost_disable_notify(&vs->dev, vq);
  487. for (;;) {
  488. head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
  489. ARRAY_SIZE(vq->iov), &out, &in,
  490. NULL, NULL);
  491. pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
  492. head, out, in);
  493. /* On error, stop handling until the next kick. */
  494. if (unlikely(head < 0))
  495. break;
  496. /* Nothing new? Wait for eventfd to tell us they refilled. */
  497. if (head == vq->num) {
  498. if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
  499. vhost_disable_notify(&vs->dev, vq);
  500. continue;
  501. }
  502. break;
  503. }
  504. /* FIXME: BIDI operation */
  505. if (out == 1 && in == 1) {
  506. data_direction = DMA_NONE;
  507. data_first = 0;
  508. data_num = 0;
  509. } else if (out == 1 && in > 1) {
  510. data_direction = DMA_FROM_DEVICE;
  511. data_first = out + 1;
  512. data_num = in - 1;
  513. } else if (out > 1 && in == 1) {
  514. data_direction = DMA_TO_DEVICE;
  515. data_first = 1;
  516. data_num = out - 1;
  517. } else {
  518. vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
  519. out, in);
  520. break;
  521. }
  522. /*
  523. * Check for a sane resp buffer so we can report errors to
  524. * the guest.
  525. */
  526. if (unlikely(vq->iov[out].iov_len !=
  527. sizeof(struct virtio_scsi_cmd_resp))) {
  528. vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
  529. " bytes\n", vq->iov[out].iov_len);
  530. break;
  531. }
  532. if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
  533. vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
  534. " bytes\n", vq->iov[0].iov_len);
  535. break;
  536. }
  537. pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
  538. " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
  539. ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
  540. sizeof(v_req));
  541. if (unlikely(ret)) {
  542. vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
  543. break;
  544. }
  545. exp_data_len = 0;
  546. for (i = 0; i < data_num; i++)
  547. exp_data_len += vq->iov[data_first + i].iov_len;
  548. tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
  549. exp_data_len, data_direction);
  550. if (IS_ERR(tv_cmd)) {
  551. vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
  552. PTR_ERR(tv_cmd));
  553. break;
  554. }
  555. pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
  556. ": %d\n", tv_cmd, exp_data_len, data_direction);
  557. tv_cmd->tvc_vhost = vs;
  558. if (unlikely(vq->iov[out].iov_len !=
  559. sizeof(struct virtio_scsi_cmd_resp))) {
  560. vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
  561. " bytes, out: %d, in: %d\n",
  562. vq->iov[out].iov_len, out, in);
  563. break;
  564. }
  565. tv_cmd->tvc_resp = vq->iov[out].iov_base;
  566. /*
  567. * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
  568. * that will be used by tcm_vhost_new_cmd_map() and down into
  569. * target_setup_cmd_from_cdb()
  570. */
  571. memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
  572. /*
  573. * Check that the recieved CDB size does not exceeded our
  574. * hardcoded max for tcm_vhost
  575. */
  576. /* TODO what if cdb was too small for varlen cdb header? */
  577. if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
  578. TCM_VHOST_MAX_CDB_SIZE)) {
  579. vq_err(vq, "Received SCSI CDB with command_size: %d that"
  580. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  581. scsi_command_size(tv_cmd->tvc_cdb),
  582. TCM_VHOST_MAX_CDB_SIZE);
  583. break; /* TODO */
  584. }
  585. tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
  586. pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
  587. tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
  588. if (data_direction != DMA_NONE) {
  589. ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
  590. &vq->iov[data_first], data_num,
  591. data_direction == DMA_TO_DEVICE);
  592. if (unlikely(ret)) {
  593. vq_err(vq, "Failed to map iov to sgl\n");
  594. break; /* TODO */
  595. }
  596. }
  597. /*
  598. * Save the descriptor from vhost_get_vq_desc() to be used to
  599. * complete the virtio-scsi request in TCM callback context via
  600. * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
  601. */
  602. tv_cmd->tvc_vq_desc = head;
  603. /*
  604. * Dispatch tv_cmd descriptor for cmwq execution in process
  605. * context provided by tcm_vhost_workqueue. This also ensures
  606. * tv_cmd is executed on the same kworker CPU as this vhost
  607. * thread to gain positive L2 cache locality effects..
  608. */
  609. INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
  610. queue_work(tcm_vhost_workqueue, &tv_cmd->work);
  611. }
  612. mutex_unlock(&vq->mutex);
  613. }
  614. static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
  615. {
  616. pr_debug("%s: The handling func for control queue.\n", __func__);
  617. }
  618. static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
  619. {
  620. pr_debug("%s: The handling func for event queue.\n", __func__);
  621. }
  622. static void vhost_scsi_handle_kick(struct vhost_work *work)
  623. {
  624. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  625. poll.work);
  626. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  627. vhost_scsi_handle_vq(vs);
  628. }
  629. /*
  630. * Called from vhost_scsi_ioctl() context to walk the list of available
  631. * tcm_vhost_tpg with an active struct tcm_vhost_nexus
  632. */
  633. static int vhost_scsi_set_endpoint(
  634. struct vhost_scsi *vs,
  635. struct vhost_scsi_target *t)
  636. {
  637. struct tcm_vhost_tport *tv_tport;
  638. struct tcm_vhost_tpg *tv_tpg;
  639. int index;
  640. mutex_lock(&vs->dev.mutex);
  641. /* Verify that ring has been setup correctly. */
  642. for (index = 0; index < vs->dev.nvqs; ++index) {
  643. /* Verify that ring has been setup correctly. */
  644. if (!vhost_vq_access_ok(&vs->vqs[index])) {
  645. mutex_unlock(&vs->dev.mutex);
  646. return -EFAULT;
  647. }
  648. }
  649. mutex_unlock(&vs->dev.mutex);
  650. mutex_lock(&tcm_vhost_mutex);
  651. list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
  652. mutex_lock(&tv_tpg->tv_tpg_mutex);
  653. if (!tv_tpg->tpg_nexus) {
  654. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  655. continue;
  656. }
  657. if (tv_tpg->tv_tpg_vhost_count != 0) {
  658. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  659. continue;
  660. }
  661. tv_tport = tv_tpg->tport;
  662. if (!strcmp(tv_tport->tport_name, t->vhost_wwpn) &&
  663. (tv_tpg->tport_tpgt == t->vhost_tpgt)) {
  664. tv_tpg->tv_tpg_vhost_count++;
  665. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  666. mutex_unlock(&tcm_vhost_mutex);
  667. mutex_lock(&vs->dev.mutex);
  668. if (vs->vs_tpg) {
  669. mutex_unlock(&vs->dev.mutex);
  670. mutex_lock(&tv_tpg->tv_tpg_mutex);
  671. tv_tpg->tv_tpg_vhost_count--;
  672. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  673. return -EEXIST;
  674. }
  675. vs->vs_tpg = tv_tpg;
  676. smp_mb__after_atomic_inc();
  677. mutex_unlock(&vs->dev.mutex);
  678. return 0;
  679. }
  680. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  681. }
  682. mutex_unlock(&tcm_vhost_mutex);
  683. return -EINVAL;
  684. }
  685. static int vhost_scsi_clear_endpoint(
  686. struct vhost_scsi *vs,
  687. struct vhost_scsi_target *t)
  688. {
  689. struct tcm_vhost_tport *tv_tport;
  690. struct tcm_vhost_tpg *tv_tpg;
  691. int index, ret;
  692. mutex_lock(&vs->dev.mutex);
  693. /* Verify that ring has been setup correctly. */
  694. for (index = 0; index < vs->dev.nvqs; ++index) {
  695. if (!vhost_vq_access_ok(&vs->vqs[index])) {
  696. ret = -EFAULT;
  697. goto err;
  698. }
  699. }
  700. if (!vs->vs_tpg) {
  701. ret = -ENODEV;
  702. goto err;
  703. }
  704. tv_tpg = vs->vs_tpg;
  705. tv_tport = tv_tpg->tport;
  706. if (strcmp(tv_tport->tport_name, t->vhost_wwpn) ||
  707. (tv_tpg->tport_tpgt != t->vhost_tpgt)) {
  708. pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
  709. " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
  710. tv_tport->tport_name, tv_tpg->tport_tpgt,
  711. t->vhost_wwpn, t->vhost_tpgt);
  712. ret = -EINVAL;
  713. goto err;
  714. }
  715. tv_tpg->tv_tpg_vhost_count--;
  716. vs->vs_tpg = NULL;
  717. mutex_unlock(&vs->dev.mutex);
  718. return 0;
  719. err:
  720. mutex_unlock(&vs->dev.mutex);
  721. return ret;
  722. }
  723. static int vhost_scsi_open(struct inode *inode, struct file *f)
  724. {
  725. struct vhost_scsi *s;
  726. int r;
  727. s = kzalloc(sizeof(*s), GFP_KERNEL);
  728. if (!s)
  729. return -ENOMEM;
  730. vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
  731. s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick;
  732. s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick;
  733. s->vqs[VHOST_SCSI_VQ_IO].handle_kick = vhost_scsi_handle_kick;
  734. r = vhost_dev_init(&s->dev, s->vqs, 3);
  735. if (r < 0) {
  736. kfree(s);
  737. return r;
  738. }
  739. f->private_data = s;
  740. return 0;
  741. }
  742. static int vhost_scsi_release(struct inode *inode, struct file *f)
  743. {
  744. struct vhost_scsi *s = f->private_data;
  745. if (s->vs_tpg && s->vs_tpg->tport) {
  746. struct vhost_scsi_target backend;
  747. memcpy(backend.vhost_wwpn, s->vs_tpg->tport->tport_name,
  748. sizeof(backend.vhost_wwpn));
  749. backend.vhost_tpgt = s->vs_tpg->tport_tpgt;
  750. vhost_scsi_clear_endpoint(s, &backend);
  751. }
  752. vhost_dev_stop(&s->dev);
  753. vhost_dev_cleanup(&s->dev, false);
  754. kfree(s);
  755. return 0;
  756. }
  757. static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
  758. {
  759. vhost_poll_flush(&vs->dev.vqs[index].poll);
  760. }
  761. static void vhost_scsi_flush(struct vhost_scsi *vs)
  762. {
  763. vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_CTL);
  764. vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_EVT);
  765. vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_IO);
  766. }
  767. static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
  768. {
  769. if (features & ~VHOST_FEATURES)
  770. return -EOPNOTSUPP;
  771. mutex_lock(&vs->dev.mutex);
  772. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  773. !vhost_log_access_ok(&vs->dev)) {
  774. mutex_unlock(&vs->dev.mutex);
  775. return -EFAULT;
  776. }
  777. vs->dev.acked_features = features;
  778. smp_wmb();
  779. vhost_scsi_flush(vs);
  780. mutex_unlock(&vs->dev.mutex);
  781. return 0;
  782. }
  783. static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
  784. unsigned long arg)
  785. {
  786. struct vhost_scsi *vs = f->private_data;
  787. struct vhost_scsi_target backend;
  788. void __user *argp = (void __user *)arg;
  789. u64 __user *featurep = argp;
  790. u64 features;
  791. int r, abi_version = VHOST_SCSI_ABI_VERSION;
  792. switch (ioctl) {
  793. case VHOST_SCSI_SET_ENDPOINT:
  794. if (copy_from_user(&backend, argp, sizeof backend))
  795. return -EFAULT;
  796. if (backend.reserved != 0)
  797. return -EOPNOTSUPP;
  798. return vhost_scsi_set_endpoint(vs, &backend);
  799. case VHOST_SCSI_CLEAR_ENDPOINT:
  800. if (copy_from_user(&backend, argp, sizeof backend))
  801. return -EFAULT;
  802. if (backend.reserved != 0)
  803. return -EOPNOTSUPP;
  804. return vhost_scsi_clear_endpoint(vs, &backend);
  805. case VHOST_SCSI_GET_ABI_VERSION:
  806. if (copy_to_user(argp, &abi_version, sizeof abi_version))
  807. return -EFAULT;
  808. return 0;
  809. case VHOST_GET_FEATURES:
  810. features = VHOST_FEATURES;
  811. if (copy_to_user(featurep, &features, sizeof features))
  812. return -EFAULT;
  813. return 0;
  814. case VHOST_SET_FEATURES:
  815. if (copy_from_user(&features, featurep, sizeof features))
  816. return -EFAULT;
  817. return vhost_scsi_set_features(vs, features);
  818. default:
  819. mutex_lock(&vs->dev.mutex);
  820. r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
  821. /* TODO: flush backend after dev ioctl. */
  822. if (r == -ENOIOCTLCMD)
  823. r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
  824. mutex_unlock(&vs->dev.mutex);
  825. return r;
  826. }
  827. }
  828. #ifdef CONFIG_COMPAT
  829. static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
  830. unsigned long arg)
  831. {
  832. return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  833. }
  834. #endif
  835. static const struct file_operations vhost_scsi_fops = {
  836. .owner = THIS_MODULE,
  837. .release = vhost_scsi_release,
  838. .unlocked_ioctl = vhost_scsi_ioctl,
  839. #ifdef CONFIG_COMPAT
  840. .compat_ioctl = vhost_scsi_compat_ioctl,
  841. #endif
  842. .open = vhost_scsi_open,
  843. .llseek = noop_llseek,
  844. };
  845. static struct miscdevice vhost_scsi_misc = {
  846. MISC_DYNAMIC_MINOR,
  847. "vhost-scsi",
  848. &vhost_scsi_fops,
  849. };
  850. static int __init vhost_scsi_register(void)
  851. {
  852. return misc_register(&vhost_scsi_misc);
  853. }
  854. static int vhost_scsi_deregister(void)
  855. {
  856. return misc_deregister(&vhost_scsi_misc);
  857. }
  858. static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
  859. {
  860. switch (tport->tport_proto_id) {
  861. case SCSI_PROTOCOL_SAS:
  862. return "SAS";
  863. case SCSI_PROTOCOL_FCP:
  864. return "FCP";
  865. case SCSI_PROTOCOL_ISCSI:
  866. return "iSCSI";
  867. default:
  868. break;
  869. }
  870. return "Unknown";
  871. }
  872. static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
  873. struct se_lun *lun)
  874. {
  875. struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
  876. struct tcm_vhost_tpg, se_tpg);
  877. mutex_lock(&tv_tpg->tv_tpg_mutex);
  878. tv_tpg->tv_tpg_port_count++;
  879. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  880. return 0;
  881. }
  882. static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
  883. struct se_lun *se_lun)
  884. {
  885. struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
  886. struct tcm_vhost_tpg, se_tpg);
  887. mutex_lock(&tv_tpg->tv_tpg_mutex);
  888. tv_tpg->tv_tpg_port_count--;
  889. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  890. }
  891. static struct se_node_acl *tcm_vhost_make_nodeacl(
  892. struct se_portal_group *se_tpg,
  893. struct config_group *group,
  894. const char *name)
  895. {
  896. struct se_node_acl *se_nacl, *se_nacl_new;
  897. struct tcm_vhost_nacl *nacl;
  898. u64 wwpn = 0;
  899. u32 nexus_depth;
  900. /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
  901. return ERR_PTR(-EINVAL); */
  902. se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
  903. if (!se_nacl_new)
  904. return ERR_PTR(-ENOMEM);
  905. nexus_depth = 1;
  906. /*
  907. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  908. * when converting a NodeACL from demo mode -> explict
  909. */
  910. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  911. name, nexus_depth);
  912. if (IS_ERR(se_nacl)) {
  913. tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
  914. return se_nacl;
  915. }
  916. /*
  917. * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
  918. */
  919. nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
  920. nacl->iport_wwpn = wwpn;
  921. return se_nacl;
  922. }
  923. static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
  924. {
  925. struct tcm_vhost_nacl *nacl = container_of(se_acl,
  926. struct tcm_vhost_nacl, se_node_acl);
  927. core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
  928. kfree(nacl);
  929. }
  930. static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
  931. const char *name)
  932. {
  933. struct se_portal_group *se_tpg;
  934. struct tcm_vhost_nexus *tv_nexus;
  935. mutex_lock(&tv_tpg->tv_tpg_mutex);
  936. if (tv_tpg->tpg_nexus) {
  937. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  938. pr_debug("tv_tpg->tpg_nexus already exists\n");
  939. return -EEXIST;
  940. }
  941. se_tpg = &tv_tpg->se_tpg;
  942. tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
  943. if (!tv_nexus) {
  944. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  945. pr_err("Unable to allocate struct tcm_vhost_nexus\n");
  946. return -ENOMEM;
  947. }
  948. /*
  949. * Initialize the struct se_session pointer
  950. */
  951. tv_nexus->tvn_se_sess = transport_init_session();
  952. if (IS_ERR(tv_nexus->tvn_se_sess)) {
  953. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  954. kfree(tv_nexus);
  955. return -ENOMEM;
  956. }
  957. /*
  958. * Since we are running in 'demo mode' this call with generate a
  959. * struct se_node_acl for the tcm_vhost struct se_portal_group with
  960. * the SCSI Initiator port name of the passed configfs group 'name'.
  961. */
  962. tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  963. se_tpg, (unsigned char *)name);
  964. if (!tv_nexus->tvn_se_sess->se_node_acl) {
  965. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  966. pr_debug("core_tpg_check_initiator_node_acl() failed"
  967. " for %s\n", name);
  968. transport_free_session(tv_nexus->tvn_se_sess);
  969. kfree(tv_nexus);
  970. return -ENOMEM;
  971. }
  972. /*
  973. * Now register the TCM vhost virtual I_T Nexus as active with the
  974. * call to __transport_register_session()
  975. */
  976. __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
  977. tv_nexus->tvn_se_sess, tv_nexus);
  978. tv_tpg->tpg_nexus = tv_nexus;
  979. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  980. return 0;
  981. }
  982. static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
  983. {
  984. struct se_session *se_sess;
  985. struct tcm_vhost_nexus *tv_nexus;
  986. mutex_lock(&tpg->tv_tpg_mutex);
  987. tv_nexus = tpg->tpg_nexus;
  988. if (!tv_nexus) {
  989. mutex_unlock(&tpg->tv_tpg_mutex);
  990. return -ENODEV;
  991. }
  992. se_sess = tv_nexus->tvn_se_sess;
  993. if (!se_sess) {
  994. mutex_unlock(&tpg->tv_tpg_mutex);
  995. return -ENODEV;
  996. }
  997. if (tpg->tv_tpg_port_count != 0) {
  998. mutex_unlock(&tpg->tv_tpg_mutex);
  999. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1000. " active TPG port count: %d\n",
  1001. tpg->tv_tpg_port_count);
  1002. return -EBUSY;
  1003. }
  1004. if (tpg->tv_tpg_vhost_count != 0) {
  1005. mutex_unlock(&tpg->tv_tpg_mutex);
  1006. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1007. " active TPG vhost count: %d\n",
  1008. tpg->tv_tpg_vhost_count);
  1009. return -EBUSY;
  1010. }
  1011. pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
  1012. " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
  1013. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1014. /*
  1015. * Release the SCSI I_T Nexus to the emulated vhost Target Port
  1016. */
  1017. transport_deregister_session(tv_nexus->tvn_se_sess);
  1018. tpg->tpg_nexus = NULL;
  1019. mutex_unlock(&tpg->tv_tpg_mutex);
  1020. kfree(tv_nexus);
  1021. return 0;
  1022. }
  1023. static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
  1024. char *page)
  1025. {
  1026. struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
  1027. struct tcm_vhost_tpg, se_tpg);
  1028. struct tcm_vhost_nexus *tv_nexus;
  1029. ssize_t ret;
  1030. mutex_lock(&tv_tpg->tv_tpg_mutex);
  1031. tv_nexus = tv_tpg->tpg_nexus;
  1032. if (!tv_nexus) {
  1033. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  1034. return -ENODEV;
  1035. }
  1036. ret = snprintf(page, PAGE_SIZE, "%s\n",
  1037. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1038. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  1039. return ret;
  1040. }
  1041. static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
  1042. const char *page,
  1043. size_t count)
  1044. {
  1045. struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
  1046. struct tcm_vhost_tpg, se_tpg);
  1047. struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
  1048. unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
  1049. int ret;
  1050. /*
  1051. * Shutdown the active I_T nexus if 'NULL' is passed..
  1052. */
  1053. if (!strncmp(page, "NULL", 4)) {
  1054. ret = tcm_vhost_drop_nexus(tv_tpg);
  1055. return (!ret) ? count : ret;
  1056. }
  1057. /*
  1058. * Otherwise make sure the passed virtual Initiator port WWN matches
  1059. * the fabric protocol_id set in tcm_vhost_make_tport(), and call
  1060. * tcm_vhost_make_nexus().
  1061. */
  1062. if (strlen(page) >= TCM_VHOST_NAMELEN) {
  1063. pr_err("Emulated NAA Sas Address: %s, exceeds"
  1064. " max: %d\n", page, TCM_VHOST_NAMELEN);
  1065. return -EINVAL;
  1066. }
  1067. snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
  1068. ptr = strstr(i_port, "naa.");
  1069. if (ptr) {
  1070. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
  1071. pr_err("Passed SAS Initiator Port %s does not"
  1072. " match target port protoid: %s\n", i_port,
  1073. tcm_vhost_dump_proto_id(tport_wwn));
  1074. return -EINVAL;
  1075. }
  1076. port_ptr = &i_port[0];
  1077. goto check_newline;
  1078. }
  1079. ptr = strstr(i_port, "fc.");
  1080. if (ptr) {
  1081. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
  1082. pr_err("Passed FCP Initiator Port %s does not"
  1083. " match target port protoid: %s\n", i_port,
  1084. tcm_vhost_dump_proto_id(tport_wwn));
  1085. return -EINVAL;
  1086. }
  1087. port_ptr = &i_port[3]; /* Skip over "fc." */
  1088. goto check_newline;
  1089. }
  1090. ptr = strstr(i_port, "iqn.");
  1091. if (ptr) {
  1092. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
  1093. pr_err("Passed iSCSI Initiator Port %s does not"
  1094. " match target port protoid: %s\n", i_port,
  1095. tcm_vhost_dump_proto_id(tport_wwn));
  1096. return -EINVAL;
  1097. }
  1098. port_ptr = &i_port[0];
  1099. goto check_newline;
  1100. }
  1101. pr_err("Unable to locate prefix for emulated Initiator Port:"
  1102. " %s\n", i_port);
  1103. return -EINVAL;
  1104. /*
  1105. * Clear any trailing newline for the NAA WWN
  1106. */
  1107. check_newline:
  1108. if (i_port[strlen(i_port)-1] == '\n')
  1109. i_port[strlen(i_port)-1] = '\0';
  1110. ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
  1111. if (ret < 0)
  1112. return ret;
  1113. return count;
  1114. }
  1115. TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
  1116. static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
  1117. &tcm_vhost_tpg_nexus.attr,
  1118. NULL,
  1119. };
  1120. static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
  1121. struct config_group *group,
  1122. const char *name)
  1123. {
  1124. struct tcm_vhost_tport *tport = container_of(wwn,
  1125. struct tcm_vhost_tport, tport_wwn);
  1126. struct tcm_vhost_tpg *tpg;
  1127. unsigned long tpgt;
  1128. int ret;
  1129. if (strstr(name, "tpgt_") != name)
  1130. return ERR_PTR(-EINVAL);
  1131. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
  1132. return ERR_PTR(-EINVAL);
  1133. tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
  1134. if (!tpg) {
  1135. pr_err("Unable to allocate struct tcm_vhost_tpg");
  1136. return ERR_PTR(-ENOMEM);
  1137. }
  1138. mutex_init(&tpg->tv_tpg_mutex);
  1139. INIT_LIST_HEAD(&tpg->tv_tpg_list);
  1140. tpg->tport = tport;
  1141. tpg->tport_tpgt = tpgt;
  1142. ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
  1143. &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
  1144. if (ret < 0) {
  1145. kfree(tpg);
  1146. return NULL;
  1147. }
  1148. mutex_lock(&tcm_vhost_mutex);
  1149. list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
  1150. mutex_unlock(&tcm_vhost_mutex);
  1151. return &tpg->se_tpg;
  1152. }
  1153. static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
  1154. {
  1155. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  1156. struct tcm_vhost_tpg, se_tpg);
  1157. mutex_lock(&tcm_vhost_mutex);
  1158. list_del(&tpg->tv_tpg_list);
  1159. mutex_unlock(&tcm_vhost_mutex);
  1160. /*
  1161. * Release the virtual I_T Nexus for this vhost TPG
  1162. */
  1163. tcm_vhost_drop_nexus(tpg);
  1164. /*
  1165. * Deregister the se_tpg from TCM..
  1166. */
  1167. core_tpg_deregister(se_tpg);
  1168. kfree(tpg);
  1169. }
  1170. static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
  1171. struct config_group *group,
  1172. const char *name)
  1173. {
  1174. struct tcm_vhost_tport *tport;
  1175. char *ptr;
  1176. u64 wwpn = 0;
  1177. int off = 0;
  1178. /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
  1179. return ERR_PTR(-EINVAL); */
  1180. tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
  1181. if (!tport) {
  1182. pr_err("Unable to allocate struct tcm_vhost_tport");
  1183. return ERR_PTR(-ENOMEM);
  1184. }
  1185. tport->tport_wwpn = wwpn;
  1186. /*
  1187. * Determine the emulated Protocol Identifier and Target Port Name
  1188. * based on the incoming configfs directory name.
  1189. */
  1190. ptr = strstr(name, "naa.");
  1191. if (ptr) {
  1192. tport->tport_proto_id = SCSI_PROTOCOL_SAS;
  1193. goto check_len;
  1194. }
  1195. ptr = strstr(name, "fc.");
  1196. if (ptr) {
  1197. tport->tport_proto_id = SCSI_PROTOCOL_FCP;
  1198. off = 3; /* Skip over "fc." */
  1199. goto check_len;
  1200. }
  1201. ptr = strstr(name, "iqn.");
  1202. if (ptr) {
  1203. tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
  1204. goto check_len;
  1205. }
  1206. pr_err("Unable to locate prefix for emulated Target Port:"
  1207. " %s\n", name);
  1208. kfree(tport);
  1209. return ERR_PTR(-EINVAL);
  1210. check_len:
  1211. if (strlen(name) >= TCM_VHOST_NAMELEN) {
  1212. pr_err("Emulated %s Address: %s, exceeds"
  1213. " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
  1214. TCM_VHOST_NAMELEN);
  1215. kfree(tport);
  1216. return ERR_PTR(-EINVAL);
  1217. }
  1218. snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
  1219. pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
  1220. " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
  1221. return &tport->tport_wwn;
  1222. }
  1223. static void tcm_vhost_drop_tport(struct se_wwn *wwn)
  1224. {
  1225. struct tcm_vhost_tport *tport = container_of(wwn,
  1226. struct tcm_vhost_tport, tport_wwn);
  1227. pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
  1228. " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
  1229. tport->tport_name);
  1230. kfree(tport);
  1231. }
  1232. static ssize_t tcm_vhost_wwn_show_attr_version(
  1233. struct target_fabric_configfs *tf,
  1234. char *page)
  1235. {
  1236. return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
  1237. "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
  1238. utsname()->machine);
  1239. }
  1240. TF_WWN_ATTR_RO(tcm_vhost, version);
  1241. static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
  1242. &tcm_vhost_wwn_version.attr,
  1243. NULL,
  1244. };
  1245. static struct target_core_fabric_ops tcm_vhost_ops = {
  1246. .get_fabric_name = tcm_vhost_get_fabric_name,
  1247. .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
  1248. .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
  1249. .tpg_get_tag = tcm_vhost_get_tag,
  1250. .tpg_get_default_depth = tcm_vhost_get_default_depth,
  1251. .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
  1252. .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
  1253. .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
  1254. .tpg_check_demo_mode = tcm_vhost_check_true,
  1255. .tpg_check_demo_mode_cache = tcm_vhost_check_true,
  1256. .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
  1257. .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
  1258. .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
  1259. .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
  1260. .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
  1261. .release_cmd = tcm_vhost_release_cmd,
  1262. .shutdown_session = tcm_vhost_shutdown_session,
  1263. .close_session = tcm_vhost_close_session,
  1264. .sess_get_index = tcm_vhost_sess_get_index,
  1265. .sess_get_initiator_sid = NULL,
  1266. .write_pending = tcm_vhost_write_pending,
  1267. .write_pending_status = tcm_vhost_write_pending_status,
  1268. .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
  1269. .get_task_tag = tcm_vhost_get_task_tag,
  1270. .get_cmd_state = tcm_vhost_get_cmd_state,
  1271. .queue_data_in = tcm_vhost_queue_data_in,
  1272. .queue_status = tcm_vhost_queue_status,
  1273. .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
  1274. /*
  1275. * Setup callers for generic logic in target_core_fabric_configfs.c
  1276. */
  1277. .fabric_make_wwn = tcm_vhost_make_tport,
  1278. .fabric_drop_wwn = tcm_vhost_drop_tport,
  1279. .fabric_make_tpg = tcm_vhost_make_tpg,
  1280. .fabric_drop_tpg = tcm_vhost_drop_tpg,
  1281. .fabric_post_link = tcm_vhost_port_link,
  1282. .fabric_pre_unlink = tcm_vhost_port_unlink,
  1283. .fabric_make_np = NULL,
  1284. .fabric_drop_np = NULL,
  1285. .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
  1286. .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
  1287. };
  1288. static int tcm_vhost_register_configfs(void)
  1289. {
  1290. struct target_fabric_configfs *fabric;
  1291. int ret;
  1292. pr_debug("TCM_VHOST fabric module %s on %s/%s"
  1293. " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
  1294. utsname()->machine);
  1295. /*
  1296. * Register the top level struct config_item_type with TCM core
  1297. */
  1298. fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
  1299. if (IS_ERR(fabric)) {
  1300. pr_err("target_fabric_configfs_init() failed\n");
  1301. return PTR_ERR(fabric);
  1302. }
  1303. /*
  1304. * Setup fabric->tf_ops from our local tcm_vhost_ops
  1305. */
  1306. fabric->tf_ops = tcm_vhost_ops;
  1307. /*
  1308. * Setup default attribute lists for various fabric->tf_cit_tmpl
  1309. */
  1310. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
  1311. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
  1312. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
  1313. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1314. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1315. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  1316. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  1317. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  1318. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  1319. /*
  1320. * Register the fabric for use within TCM
  1321. */
  1322. ret = target_fabric_configfs_register(fabric);
  1323. if (ret < 0) {
  1324. pr_err("target_fabric_configfs_register() failed"
  1325. " for TCM_VHOST\n");
  1326. return ret;
  1327. }
  1328. /*
  1329. * Setup our local pointer to *fabric
  1330. */
  1331. tcm_vhost_fabric_configfs = fabric;
  1332. pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
  1333. return 0;
  1334. };
  1335. static void tcm_vhost_deregister_configfs(void)
  1336. {
  1337. if (!tcm_vhost_fabric_configfs)
  1338. return;
  1339. target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
  1340. tcm_vhost_fabric_configfs = NULL;
  1341. pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
  1342. };
  1343. static int __init tcm_vhost_init(void)
  1344. {
  1345. int ret = -ENOMEM;
  1346. /*
  1347. * Use our own dedicated workqueue for submitting I/O into
  1348. * target core to avoid contention within system_wq.
  1349. */
  1350. tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
  1351. if (!tcm_vhost_workqueue)
  1352. goto out;
  1353. ret = vhost_scsi_register();
  1354. if (ret < 0)
  1355. goto out_destroy_workqueue;
  1356. ret = tcm_vhost_register_configfs();
  1357. if (ret < 0)
  1358. goto out_vhost_scsi_deregister;
  1359. return 0;
  1360. out_vhost_scsi_deregister:
  1361. vhost_scsi_deregister();
  1362. out_destroy_workqueue:
  1363. destroy_workqueue(tcm_vhost_workqueue);
  1364. out:
  1365. return ret;
  1366. };
  1367. static void tcm_vhost_exit(void)
  1368. {
  1369. tcm_vhost_deregister_configfs();
  1370. vhost_scsi_deregister();
  1371. destroy_workqueue(tcm_vhost_workqueue);
  1372. };
  1373. MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
  1374. MODULE_LICENSE("GPL");
  1375. module_init(tcm_vhost_init);
  1376. module_exit(tcm_vhost_exit);