tcm_vhost.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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, struct iovec *iov, int write)
  370. {
  371. unsigned int npages = 0, pages_nr, offset, nbytes;
  372. struct scatterlist *sg = sgl;
  373. void __user *ptr = iov->iov_base;
  374. size_t len = iov->iov_len;
  375. struct page **pages;
  376. int ret, i;
  377. pages_nr = iov_num_pages(iov);
  378. if (pages_nr > sgl_count)
  379. return -ENOBUFS;
  380. pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
  381. if (!pages)
  382. return -ENOMEM;
  383. ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
  384. /* No pages were pinned */
  385. if (ret < 0)
  386. goto out;
  387. /* Less pages pinned than wanted */
  388. if (ret != pages_nr) {
  389. for (i = 0; i < ret; i++)
  390. put_page(pages[i]);
  391. ret = -EFAULT;
  392. goto out;
  393. }
  394. while (len > 0) {
  395. offset = (uintptr_t)ptr & ~PAGE_MASK;
  396. nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
  397. sg_set_page(sg, pages[npages], nbytes, offset);
  398. ptr += nbytes;
  399. len -= nbytes;
  400. sg++;
  401. npages++;
  402. }
  403. out:
  404. kfree(pages);
  405. return ret;
  406. }
  407. static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
  408. struct iovec *iov, unsigned int niov, int write)
  409. {
  410. int ret;
  411. unsigned int i;
  412. u32 sgl_count;
  413. struct scatterlist *sg;
  414. /*
  415. * Find out how long sglist needs to be
  416. */
  417. sgl_count = 0;
  418. for (i = 0; i < niov; i++)
  419. sgl_count += iov_num_pages(&iov[i]);
  420. /* TODO overflow checking */
  421. sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
  422. if (!sg)
  423. return -ENOMEM;
  424. pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
  425. sg, sgl_count, !sg);
  426. sg_init_table(sg, sgl_count);
  427. tv_cmd->tvc_sgl = sg;
  428. tv_cmd->tvc_sgl_count = sgl_count;
  429. pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
  430. for (i = 0; i < niov; i++) {
  431. ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
  432. if (ret < 0) {
  433. for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
  434. put_page(sg_page(&tv_cmd->tvc_sgl[i]));
  435. kfree(tv_cmd->tvc_sgl);
  436. tv_cmd->tvc_sgl = NULL;
  437. tv_cmd->tvc_sgl_count = 0;
  438. return ret;
  439. }
  440. sg += ret;
  441. sgl_count -= ret;
  442. }
  443. return 0;
  444. }
  445. static void tcm_vhost_submission_work(struct work_struct *work)
  446. {
  447. struct tcm_vhost_cmd *tv_cmd =
  448. container_of(work, struct tcm_vhost_cmd, work);
  449. struct tcm_vhost_nexus *tv_nexus;
  450. struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
  451. struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
  452. int rc, sg_no_bidi = 0;
  453. if (tv_cmd->tvc_sgl_count) {
  454. sg_ptr = tv_cmd->tvc_sgl;
  455. /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
  456. #if 0
  457. if (se_cmd->se_cmd_flags & SCF_BIDI) {
  458. sg_bidi_ptr = NULL;
  459. sg_no_bidi = 0;
  460. }
  461. #endif
  462. } else {
  463. sg_ptr = NULL;
  464. }
  465. tv_nexus = tv_cmd->tvc_nexus;
  466. rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
  467. tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
  468. tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
  469. tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
  470. 0, sg_ptr, tv_cmd->tvc_sgl_count,
  471. sg_bidi_ptr, sg_no_bidi);
  472. if (rc < 0) {
  473. transport_send_check_condition_and_sense(se_cmd,
  474. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  475. transport_generic_free_cmd(se_cmd, 0);
  476. }
  477. }
  478. static void vhost_scsi_handle_vq(struct vhost_scsi *vs)
  479. {
  480. struct vhost_virtqueue *vq = &vs->vqs[2];
  481. struct virtio_scsi_cmd_req v_req;
  482. struct tcm_vhost_tpg *tv_tpg;
  483. struct tcm_vhost_cmd *tv_cmd;
  484. u32 exp_data_len, data_first, data_num, data_direction;
  485. unsigned out, in, i;
  486. int head, ret;
  487. /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
  488. tv_tpg = vs->vs_tpg;
  489. if (unlikely(!tv_tpg))
  490. return;
  491. mutex_lock(&vq->mutex);
  492. vhost_disable_notify(&vs->dev, vq);
  493. for (;;) {
  494. head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
  495. ARRAY_SIZE(vq->iov), &out, &in,
  496. NULL, NULL);
  497. pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
  498. head, out, in);
  499. /* On error, stop handling until the next kick. */
  500. if (unlikely(head < 0))
  501. break;
  502. /* Nothing new? Wait for eventfd to tell us they refilled. */
  503. if (head == vq->num) {
  504. if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
  505. vhost_disable_notify(&vs->dev, vq);
  506. continue;
  507. }
  508. break;
  509. }
  510. /* FIXME: BIDI operation */
  511. if (out == 1 && in == 1) {
  512. data_direction = DMA_NONE;
  513. data_first = 0;
  514. data_num = 0;
  515. } else if (out == 1 && in > 1) {
  516. data_direction = DMA_FROM_DEVICE;
  517. data_first = out + 1;
  518. data_num = in - 1;
  519. } else if (out > 1 && in == 1) {
  520. data_direction = DMA_TO_DEVICE;
  521. data_first = 1;
  522. data_num = out - 1;
  523. } else {
  524. vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
  525. out, in);
  526. break;
  527. }
  528. /*
  529. * Check for a sane resp buffer so we can report errors to
  530. * the guest.
  531. */
  532. if (unlikely(vq->iov[out].iov_len !=
  533. sizeof(struct virtio_scsi_cmd_resp))) {
  534. vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
  535. " bytes\n", vq->iov[out].iov_len);
  536. break;
  537. }
  538. if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
  539. vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
  540. " bytes\n", vq->iov[0].iov_len);
  541. break;
  542. }
  543. pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
  544. " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
  545. ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
  546. sizeof(v_req));
  547. if (unlikely(ret)) {
  548. vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
  549. break;
  550. }
  551. exp_data_len = 0;
  552. for (i = 0; i < data_num; i++)
  553. exp_data_len += vq->iov[data_first + i].iov_len;
  554. tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
  555. exp_data_len, data_direction);
  556. if (IS_ERR(tv_cmd)) {
  557. vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
  558. PTR_ERR(tv_cmd));
  559. break;
  560. }
  561. pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
  562. ": %d\n", tv_cmd, exp_data_len, data_direction);
  563. tv_cmd->tvc_vhost = vs;
  564. if (unlikely(vq->iov[out].iov_len !=
  565. sizeof(struct virtio_scsi_cmd_resp))) {
  566. vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
  567. " bytes, out: %d, in: %d\n",
  568. vq->iov[out].iov_len, out, in);
  569. break;
  570. }
  571. tv_cmd->tvc_resp = vq->iov[out].iov_base;
  572. /*
  573. * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
  574. * that will be used by tcm_vhost_new_cmd_map() and down into
  575. * target_setup_cmd_from_cdb()
  576. */
  577. memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
  578. /*
  579. * Check that the recieved CDB size does not exceeded our
  580. * hardcoded max for tcm_vhost
  581. */
  582. /* TODO what if cdb was too small for varlen cdb header? */
  583. if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
  584. TCM_VHOST_MAX_CDB_SIZE)) {
  585. vq_err(vq, "Received SCSI CDB with command_size: %d that"
  586. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  587. scsi_command_size(tv_cmd->tvc_cdb),
  588. TCM_VHOST_MAX_CDB_SIZE);
  589. break; /* TODO */
  590. }
  591. tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
  592. pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
  593. tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
  594. if (data_direction != DMA_NONE) {
  595. ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
  596. &vq->iov[data_first], data_num,
  597. data_direction == DMA_TO_DEVICE);
  598. if (unlikely(ret)) {
  599. vq_err(vq, "Failed to map iov to sgl\n");
  600. break; /* TODO */
  601. }
  602. }
  603. /*
  604. * Save the descriptor from vhost_get_vq_desc() to be used to
  605. * complete the virtio-scsi request in TCM callback context via
  606. * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
  607. */
  608. tv_cmd->tvc_vq_desc = head;
  609. /*
  610. * Dispatch tv_cmd descriptor for cmwq execution in process
  611. * context provided by tcm_vhost_workqueue. This also ensures
  612. * tv_cmd is executed on the same kworker CPU as this vhost
  613. * thread to gain positive L2 cache locality effects..
  614. */
  615. INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
  616. queue_work(tcm_vhost_workqueue, &tv_cmd->work);
  617. }
  618. mutex_unlock(&vq->mutex);
  619. }
  620. static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
  621. {
  622. pr_debug("%s: The handling func for control queue.\n", __func__);
  623. }
  624. static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
  625. {
  626. pr_debug("%s: The handling func for event queue.\n", __func__);
  627. }
  628. static void vhost_scsi_handle_kick(struct vhost_work *work)
  629. {
  630. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  631. poll.work);
  632. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  633. vhost_scsi_handle_vq(vs);
  634. }
  635. /*
  636. * Called from vhost_scsi_ioctl() context to walk the list of available
  637. * tcm_vhost_tpg with an active struct tcm_vhost_nexus
  638. */
  639. static int vhost_scsi_set_endpoint(
  640. struct vhost_scsi *vs,
  641. struct vhost_scsi_target *t)
  642. {
  643. struct tcm_vhost_tport *tv_tport;
  644. struct tcm_vhost_tpg *tv_tpg;
  645. int index;
  646. mutex_lock(&vs->dev.mutex);
  647. /* Verify that ring has been setup correctly. */
  648. for (index = 0; index < vs->dev.nvqs; ++index) {
  649. /* Verify that ring has been setup correctly. */
  650. if (!vhost_vq_access_ok(&vs->vqs[index])) {
  651. mutex_unlock(&vs->dev.mutex);
  652. return -EFAULT;
  653. }
  654. }
  655. mutex_unlock(&vs->dev.mutex);
  656. mutex_lock(&tcm_vhost_mutex);
  657. list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
  658. mutex_lock(&tv_tpg->tv_tpg_mutex);
  659. if (!tv_tpg->tpg_nexus) {
  660. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  661. continue;
  662. }
  663. if (tv_tpg->tv_tpg_vhost_count != 0) {
  664. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  665. continue;
  666. }
  667. tv_tport = tv_tpg->tport;
  668. if (!strcmp(tv_tport->tport_name, t->vhost_wwpn) &&
  669. (tv_tpg->tport_tpgt == t->vhost_tpgt)) {
  670. tv_tpg->tv_tpg_vhost_count++;
  671. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  672. mutex_unlock(&tcm_vhost_mutex);
  673. mutex_lock(&vs->dev.mutex);
  674. if (vs->vs_tpg) {
  675. mutex_unlock(&vs->dev.mutex);
  676. mutex_lock(&tv_tpg->tv_tpg_mutex);
  677. tv_tpg->tv_tpg_vhost_count--;
  678. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  679. return -EEXIST;
  680. }
  681. vs->vs_tpg = tv_tpg;
  682. smp_mb__after_atomic_inc();
  683. mutex_unlock(&vs->dev.mutex);
  684. return 0;
  685. }
  686. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  687. }
  688. mutex_unlock(&tcm_vhost_mutex);
  689. return -EINVAL;
  690. }
  691. static int vhost_scsi_clear_endpoint(
  692. struct vhost_scsi *vs,
  693. struct vhost_scsi_target *t)
  694. {
  695. struct tcm_vhost_tport *tv_tport;
  696. struct tcm_vhost_tpg *tv_tpg;
  697. int index, ret;
  698. mutex_lock(&vs->dev.mutex);
  699. /* Verify that ring has been setup correctly. */
  700. for (index = 0; index < vs->dev.nvqs; ++index) {
  701. if (!vhost_vq_access_ok(&vs->vqs[index])) {
  702. ret = -EFAULT;
  703. goto err;
  704. }
  705. }
  706. if (!vs->vs_tpg) {
  707. ret = -ENODEV;
  708. goto err;
  709. }
  710. tv_tpg = vs->vs_tpg;
  711. tv_tport = tv_tpg->tport;
  712. if (strcmp(tv_tport->tport_name, t->vhost_wwpn) ||
  713. (tv_tpg->tport_tpgt != t->vhost_tpgt)) {
  714. pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
  715. " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
  716. tv_tport->tport_name, tv_tpg->tport_tpgt,
  717. t->vhost_wwpn, t->vhost_tpgt);
  718. ret = -EINVAL;
  719. goto err;
  720. }
  721. tv_tpg->tv_tpg_vhost_count--;
  722. vs->vs_tpg = NULL;
  723. mutex_unlock(&vs->dev.mutex);
  724. return 0;
  725. err:
  726. mutex_unlock(&vs->dev.mutex);
  727. return ret;
  728. }
  729. static int vhost_scsi_open(struct inode *inode, struct file *f)
  730. {
  731. struct vhost_scsi *s;
  732. int r;
  733. s = kzalloc(sizeof(*s), GFP_KERNEL);
  734. if (!s)
  735. return -ENOMEM;
  736. vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
  737. s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick;
  738. s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick;
  739. s->vqs[VHOST_SCSI_VQ_IO].handle_kick = vhost_scsi_handle_kick;
  740. r = vhost_dev_init(&s->dev, s->vqs, 3);
  741. if (r < 0) {
  742. kfree(s);
  743. return r;
  744. }
  745. f->private_data = s;
  746. return 0;
  747. }
  748. static int vhost_scsi_release(struct inode *inode, struct file *f)
  749. {
  750. struct vhost_scsi *s = f->private_data;
  751. if (s->vs_tpg && s->vs_tpg->tport) {
  752. struct vhost_scsi_target backend;
  753. memcpy(backend.vhost_wwpn, s->vs_tpg->tport->tport_name,
  754. sizeof(backend.vhost_wwpn));
  755. backend.vhost_tpgt = s->vs_tpg->tport_tpgt;
  756. vhost_scsi_clear_endpoint(s, &backend);
  757. }
  758. vhost_dev_stop(&s->dev);
  759. vhost_dev_cleanup(&s->dev, false);
  760. kfree(s);
  761. return 0;
  762. }
  763. static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
  764. {
  765. vhost_poll_flush(&vs->dev.vqs[index].poll);
  766. }
  767. static void vhost_scsi_flush(struct vhost_scsi *vs)
  768. {
  769. vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_CTL);
  770. vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_EVT);
  771. vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_IO);
  772. }
  773. static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
  774. {
  775. if (features & ~VHOST_FEATURES)
  776. return -EOPNOTSUPP;
  777. mutex_lock(&vs->dev.mutex);
  778. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  779. !vhost_log_access_ok(&vs->dev)) {
  780. mutex_unlock(&vs->dev.mutex);
  781. return -EFAULT;
  782. }
  783. vs->dev.acked_features = features;
  784. smp_wmb();
  785. vhost_scsi_flush(vs);
  786. mutex_unlock(&vs->dev.mutex);
  787. return 0;
  788. }
  789. static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
  790. unsigned long arg)
  791. {
  792. struct vhost_scsi *vs = f->private_data;
  793. struct vhost_scsi_target backend;
  794. void __user *argp = (void __user *)arg;
  795. u64 __user *featurep = argp;
  796. u64 features;
  797. int r, abi_version = VHOST_SCSI_ABI_VERSION;
  798. switch (ioctl) {
  799. case VHOST_SCSI_SET_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_set_endpoint(vs, &backend);
  805. case VHOST_SCSI_CLEAR_ENDPOINT:
  806. if (copy_from_user(&backend, argp, sizeof backend))
  807. return -EFAULT;
  808. if (backend.reserved != 0)
  809. return -EOPNOTSUPP;
  810. return vhost_scsi_clear_endpoint(vs, &backend);
  811. case VHOST_SCSI_GET_ABI_VERSION:
  812. if (copy_to_user(argp, &abi_version, sizeof abi_version))
  813. return -EFAULT;
  814. return 0;
  815. case VHOST_GET_FEATURES:
  816. features = VHOST_FEATURES;
  817. if (copy_to_user(featurep, &features, sizeof features))
  818. return -EFAULT;
  819. return 0;
  820. case VHOST_SET_FEATURES:
  821. if (copy_from_user(&features, featurep, sizeof features))
  822. return -EFAULT;
  823. return vhost_scsi_set_features(vs, features);
  824. default:
  825. mutex_lock(&vs->dev.mutex);
  826. r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
  827. /* TODO: flush backend after dev ioctl. */
  828. if (r == -ENOIOCTLCMD)
  829. r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
  830. mutex_unlock(&vs->dev.mutex);
  831. return r;
  832. }
  833. }
  834. #ifdef CONFIG_COMPAT
  835. static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
  836. unsigned long arg)
  837. {
  838. return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  839. }
  840. #endif
  841. static const struct file_operations vhost_scsi_fops = {
  842. .owner = THIS_MODULE,
  843. .release = vhost_scsi_release,
  844. .unlocked_ioctl = vhost_scsi_ioctl,
  845. #ifdef CONFIG_COMPAT
  846. .compat_ioctl = vhost_scsi_compat_ioctl,
  847. #endif
  848. .open = vhost_scsi_open,
  849. .llseek = noop_llseek,
  850. };
  851. static struct miscdevice vhost_scsi_misc = {
  852. MISC_DYNAMIC_MINOR,
  853. "vhost-scsi",
  854. &vhost_scsi_fops,
  855. };
  856. static int __init vhost_scsi_register(void)
  857. {
  858. return misc_register(&vhost_scsi_misc);
  859. }
  860. static int vhost_scsi_deregister(void)
  861. {
  862. return misc_deregister(&vhost_scsi_misc);
  863. }
  864. static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
  865. {
  866. switch (tport->tport_proto_id) {
  867. case SCSI_PROTOCOL_SAS:
  868. return "SAS";
  869. case SCSI_PROTOCOL_FCP:
  870. return "FCP";
  871. case SCSI_PROTOCOL_ISCSI:
  872. return "iSCSI";
  873. default:
  874. break;
  875. }
  876. return "Unknown";
  877. }
  878. static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
  879. struct se_lun *lun)
  880. {
  881. struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
  882. struct tcm_vhost_tpg, se_tpg);
  883. mutex_lock(&tv_tpg->tv_tpg_mutex);
  884. tv_tpg->tv_tpg_port_count++;
  885. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  886. return 0;
  887. }
  888. static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
  889. struct se_lun *se_lun)
  890. {
  891. struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
  892. struct tcm_vhost_tpg, se_tpg);
  893. mutex_lock(&tv_tpg->tv_tpg_mutex);
  894. tv_tpg->tv_tpg_port_count--;
  895. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  896. }
  897. static struct se_node_acl *tcm_vhost_make_nodeacl(
  898. struct se_portal_group *se_tpg,
  899. struct config_group *group,
  900. const char *name)
  901. {
  902. struct se_node_acl *se_nacl, *se_nacl_new;
  903. struct tcm_vhost_nacl *nacl;
  904. u64 wwpn = 0;
  905. u32 nexus_depth;
  906. /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
  907. return ERR_PTR(-EINVAL); */
  908. se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
  909. if (!se_nacl_new)
  910. return ERR_PTR(-ENOMEM);
  911. nexus_depth = 1;
  912. /*
  913. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  914. * when converting a NodeACL from demo mode -> explict
  915. */
  916. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  917. name, nexus_depth);
  918. if (IS_ERR(se_nacl)) {
  919. tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
  920. return se_nacl;
  921. }
  922. /*
  923. * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
  924. */
  925. nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
  926. nacl->iport_wwpn = wwpn;
  927. return se_nacl;
  928. }
  929. static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
  930. {
  931. struct tcm_vhost_nacl *nacl = container_of(se_acl,
  932. struct tcm_vhost_nacl, se_node_acl);
  933. core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
  934. kfree(nacl);
  935. }
  936. static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
  937. const char *name)
  938. {
  939. struct se_portal_group *se_tpg;
  940. struct tcm_vhost_nexus *tv_nexus;
  941. mutex_lock(&tv_tpg->tv_tpg_mutex);
  942. if (tv_tpg->tpg_nexus) {
  943. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  944. pr_debug("tv_tpg->tpg_nexus already exists\n");
  945. return -EEXIST;
  946. }
  947. se_tpg = &tv_tpg->se_tpg;
  948. tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
  949. if (!tv_nexus) {
  950. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  951. pr_err("Unable to allocate struct tcm_vhost_nexus\n");
  952. return -ENOMEM;
  953. }
  954. /*
  955. * Initialize the struct se_session pointer
  956. */
  957. tv_nexus->tvn_se_sess = transport_init_session();
  958. if (IS_ERR(tv_nexus->tvn_se_sess)) {
  959. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  960. kfree(tv_nexus);
  961. return -ENOMEM;
  962. }
  963. /*
  964. * Since we are running in 'demo mode' this call with generate a
  965. * struct se_node_acl for the tcm_vhost struct se_portal_group with
  966. * the SCSI Initiator port name of the passed configfs group 'name'.
  967. */
  968. tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  969. se_tpg, (unsigned char *)name);
  970. if (!tv_nexus->tvn_se_sess->se_node_acl) {
  971. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  972. pr_debug("core_tpg_check_initiator_node_acl() failed"
  973. " for %s\n", name);
  974. transport_free_session(tv_nexus->tvn_se_sess);
  975. kfree(tv_nexus);
  976. return -ENOMEM;
  977. }
  978. /*
  979. * Now register the TCM vhost virtual I_T Nexus as active with the
  980. * call to __transport_register_session()
  981. */
  982. __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
  983. tv_nexus->tvn_se_sess, tv_nexus);
  984. tv_tpg->tpg_nexus = tv_nexus;
  985. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  986. return 0;
  987. }
  988. static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
  989. {
  990. struct se_session *se_sess;
  991. struct tcm_vhost_nexus *tv_nexus;
  992. mutex_lock(&tpg->tv_tpg_mutex);
  993. tv_nexus = tpg->tpg_nexus;
  994. if (!tv_nexus) {
  995. mutex_unlock(&tpg->tv_tpg_mutex);
  996. return -ENODEV;
  997. }
  998. se_sess = tv_nexus->tvn_se_sess;
  999. if (!se_sess) {
  1000. mutex_unlock(&tpg->tv_tpg_mutex);
  1001. return -ENODEV;
  1002. }
  1003. if (tpg->tv_tpg_port_count != 0) {
  1004. mutex_unlock(&tpg->tv_tpg_mutex);
  1005. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1006. " active TPG port count: %d\n",
  1007. tpg->tv_tpg_port_count);
  1008. return -EBUSY;
  1009. }
  1010. if (tpg->tv_tpg_vhost_count != 0) {
  1011. mutex_unlock(&tpg->tv_tpg_mutex);
  1012. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1013. " active TPG vhost count: %d\n",
  1014. tpg->tv_tpg_vhost_count);
  1015. return -EBUSY;
  1016. }
  1017. pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
  1018. " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
  1019. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1020. /*
  1021. * Release the SCSI I_T Nexus to the emulated vhost Target Port
  1022. */
  1023. transport_deregister_session(tv_nexus->tvn_se_sess);
  1024. tpg->tpg_nexus = NULL;
  1025. mutex_unlock(&tpg->tv_tpg_mutex);
  1026. kfree(tv_nexus);
  1027. return 0;
  1028. }
  1029. static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
  1030. char *page)
  1031. {
  1032. struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
  1033. struct tcm_vhost_tpg, se_tpg);
  1034. struct tcm_vhost_nexus *tv_nexus;
  1035. ssize_t ret;
  1036. mutex_lock(&tv_tpg->tv_tpg_mutex);
  1037. tv_nexus = tv_tpg->tpg_nexus;
  1038. if (!tv_nexus) {
  1039. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  1040. return -ENODEV;
  1041. }
  1042. ret = snprintf(page, PAGE_SIZE, "%s\n",
  1043. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1044. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  1045. return ret;
  1046. }
  1047. static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
  1048. const char *page,
  1049. size_t count)
  1050. {
  1051. struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
  1052. struct tcm_vhost_tpg, se_tpg);
  1053. struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
  1054. unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
  1055. int ret;
  1056. /*
  1057. * Shutdown the active I_T nexus if 'NULL' is passed..
  1058. */
  1059. if (!strncmp(page, "NULL", 4)) {
  1060. ret = tcm_vhost_drop_nexus(tv_tpg);
  1061. return (!ret) ? count : ret;
  1062. }
  1063. /*
  1064. * Otherwise make sure the passed virtual Initiator port WWN matches
  1065. * the fabric protocol_id set in tcm_vhost_make_tport(), and call
  1066. * tcm_vhost_make_nexus().
  1067. */
  1068. if (strlen(page) >= TCM_VHOST_NAMELEN) {
  1069. pr_err("Emulated NAA Sas Address: %s, exceeds"
  1070. " max: %d\n", page, TCM_VHOST_NAMELEN);
  1071. return -EINVAL;
  1072. }
  1073. snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
  1074. ptr = strstr(i_port, "naa.");
  1075. if (ptr) {
  1076. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
  1077. pr_err("Passed SAS Initiator Port %s does not"
  1078. " match target port protoid: %s\n", i_port,
  1079. tcm_vhost_dump_proto_id(tport_wwn));
  1080. return -EINVAL;
  1081. }
  1082. port_ptr = &i_port[0];
  1083. goto check_newline;
  1084. }
  1085. ptr = strstr(i_port, "fc.");
  1086. if (ptr) {
  1087. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
  1088. pr_err("Passed FCP Initiator Port %s does not"
  1089. " match target port protoid: %s\n", i_port,
  1090. tcm_vhost_dump_proto_id(tport_wwn));
  1091. return -EINVAL;
  1092. }
  1093. port_ptr = &i_port[3]; /* Skip over "fc." */
  1094. goto check_newline;
  1095. }
  1096. ptr = strstr(i_port, "iqn.");
  1097. if (ptr) {
  1098. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
  1099. pr_err("Passed iSCSI Initiator Port %s does not"
  1100. " match target port protoid: %s\n", i_port,
  1101. tcm_vhost_dump_proto_id(tport_wwn));
  1102. return -EINVAL;
  1103. }
  1104. port_ptr = &i_port[0];
  1105. goto check_newline;
  1106. }
  1107. pr_err("Unable to locate prefix for emulated Initiator Port:"
  1108. " %s\n", i_port);
  1109. return -EINVAL;
  1110. /*
  1111. * Clear any trailing newline for the NAA WWN
  1112. */
  1113. check_newline:
  1114. if (i_port[strlen(i_port)-1] == '\n')
  1115. i_port[strlen(i_port)-1] = '\0';
  1116. ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
  1117. if (ret < 0)
  1118. return ret;
  1119. return count;
  1120. }
  1121. TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
  1122. static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
  1123. &tcm_vhost_tpg_nexus.attr,
  1124. NULL,
  1125. };
  1126. static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
  1127. struct config_group *group,
  1128. const char *name)
  1129. {
  1130. struct tcm_vhost_tport *tport = container_of(wwn,
  1131. struct tcm_vhost_tport, tport_wwn);
  1132. struct tcm_vhost_tpg *tpg;
  1133. unsigned long tpgt;
  1134. int ret;
  1135. if (strstr(name, "tpgt_") != name)
  1136. return ERR_PTR(-EINVAL);
  1137. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
  1138. return ERR_PTR(-EINVAL);
  1139. tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
  1140. if (!tpg) {
  1141. pr_err("Unable to allocate struct tcm_vhost_tpg");
  1142. return ERR_PTR(-ENOMEM);
  1143. }
  1144. mutex_init(&tpg->tv_tpg_mutex);
  1145. INIT_LIST_HEAD(&tpg->tv_tpg_list);
  1146. tpg->tport = tport;
  1147. tpg->tport_tpgt = tpgt;
  1148. ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
  1149. &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
  1150. if (ret < 0) {
  1151. kfree(tpg);
  1152. return NULL;
  1153. }
  1154. mutex_lock(&tcm_vhost_mutex);
  1155. list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
  1156. mutex_unlock(&tcm_vhost_mutex);
  1157. return &tpg->se_tpg;
  1158. }
  1159. static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
  1160. {
  1161. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  1162. struct tcm_vhost_tpg, se_tpg);
  1163. mutex_lock(&tcm_vhost_mutex);
  1164. list_del(&tpg->tv_tpg_list);
  1165. mutex_unlock(&tcm_vhost_mutex);
  1166. /*
  1167. * Release the virtual I_T Nexus for this vhost TPG
  1168. */
  1169. tcm_vhost_drop_nexus(tpg);
  1170. /*
  1171. * Deregister the se_tpg from TCM..
  1172. */
  1173. core_tpg_deregister(se_tpg);
  1174. kfree(tpg);
  1175. }
  1176. static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
  1177. struct config_group *group,
  1178. const char *name)
  1179. {
  1180. struct tcm_vhost_tport *tport;
  1181. char *ptr;
  1182. u64 wwpn = 0;
  1183. int off = 0;
  1184. /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
  1185. return ERR_PTR(-EINVAL); */
  1186. tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
  1187. if (!tport) {
  1188. pr_err("Unable to allocate struct tcm_vhost_tport");
  1189. return ERR_PTR(-ENOMEM);
  1190. }
  1191. tport->tport_wwpn = wwpn;
  1192. /*
  1193. * Determine the emulated Protocol Identifier and Target Port Name
  1194. * based on the incoming configfs directory name.
  1195. */
  1196. ptr = strstr(name, "naa.");
  1197. if (ptr) {
  1198. tport->tport_proto_id = SCSI_PROTOCOL_SAS;
  1199. goto check_len;
  1200. }
  1201. ptr = strstr(name, "fc.");
  1202. if (ptr) {
  1203. tport->tport_proto_id = SCSI_PROTOCOL_FCP;
  1204. off = 3; /* Skip over "fc." */
  1205. goto check_len;
  1206. }
  1207. ptr = strstr(name, "iqn.");
  1208. if (ptr) {
  1209. tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
  1210. goto check_len;
  1211. }
  1212. pr_err("Unable to locate prefix for emulated Target Port:"
  1213. " %s\n", name);
  1214. kfree(tport);
  1215. return ERR_PTR(-EINVAL);
  1216. check_len:
  1217. if (strlen(name) >= TCM_VHOST_NAMELEN) {
  1218. pr_err("Emulated %s Address: %s, exceeds"
  1219. " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
  1220. TCM_VHOST_NAMELEN);
  1221. kfree(tport);
  1222. return ERR_PTR(-EINVAL);
  1223. }
  1224. snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
  1225. pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
  1226. " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
  1227. return &tport->tport_wwn;
  1228. }
  1229. static void tcm_vhost_drop_tport(struct se_wwn *wwn)
  1230. {
  1231. struct tcm_vhost_tport *tport = container_of(wwn,
  1232. struct tcm_vhost_tport, tport_wwn);
  1233. pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
  1234. " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
  1235. tport->tport_name);
  1236. kfree(tport);
  1237. }
  1238. static ssize_t tcm_vhost_wwn_show_attr_version(
  1239. struct target_fabric_configfs *tf,
  1240. char *page)
  1241. {
  1242. return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
  1243. "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
  1244. utsname()->machine);
  1245. }
  1246. TF_WWN_ATTR_RO(tcm_vhost, version);
  1247. static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
  1248. &tcm_vhost_wwn_version.attr,
  1249. NULL,
  1250. };
  1251. static struct target_core_fabric_ops tcm_vhost_ops = {
  1252. .get_fabric_name = tcm_vhost_get_fabric_name,
  1253. .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
  1254. .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
  1255. .tpg_get_tag = tcm_vhost_get_tag,
  1256. .tpg_get_default_depth = tcm_vhost_get_default_depth,
  1257. .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
  1258. .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
  1259. .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
  1260. .tpg_check_demo_mode = tcm_vhost_check_true,
  1261. .tpg_check_demo_mode_cache = tcm_vhost_check_true,
  1262. .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
  1263. .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
  1264. .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
  1265. .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
  1266. .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
  1267. .release_cmd = tcm_vhost_release_cmd,
  1268. .shutdown_session = tcm_vhost_shutdown_session,
  1269. .close_session = tcm_vhost_close_session,
  1270. .sess_get_index = tcm_vhost_sess_get_index,
  1271. .sess_get_initiator_sid = NULL,
  1272. .write_pending = tcm_vhost_write_pending,
  1273. .write_pending_status = tcm_vhost_write_pending_status,
  1274. .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
  1275. .get_task_tag = tcm_vhost_get_task_tag,
  1276. .get_cmd_state = tcm_vhost_get_cmd_state,
  1277. .queue_data_in = tcm_vhost_queue_data_in,
  1278. .queue_status = tcm_vhost_queue_status,
  1279. .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
  1280. /*
  1281. * Setup callers for generic logic in target_core_fabric_configfs.c
  1282. */
  1283. .fabric_make_wwn = tcm_vhost_make_tport,
  1284. .fabric_drop_wwn = tcm_vhost_drop_tport,
  1285. .fabric_make_tpg = tcm_vhost_make_tpg,
  1286. .fabric_drop_tpg = tcm_vhost_drop_tpg,
  1287. .fabric_post_link = tcm_vhost_port_link,
  1288. .fabric_pre_unlink = tcm_vhost_port_unlink,
  1289. .fabric_make_np = NULL,
  1290. .fabric_drop_np = NULL,
  1291. .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
  1292. .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
  1293. };
  1294. static int tcm_vhost_register_configfs(void)
  1295. {
  1296. struct target_fabric_configfs *fabric;
  1297. int ret;
  1298. pr_debug("TCM_VHOST fabric module %s on %s/%s"
  1299. " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
  1300. utsname()->machine);
  1301. /*
  1302. * Register the top level struct config_item_type with TCM core
  1303. */
  1304. fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
  1305. if (IS_ERR(fabric)) {
  1306. pr_err("target_fabric_configfs_init() failed\n");
  1307. return PTR_ERR(fabric);
  1308. }
  1309. /*
  1310. * Setup fabric->tf_ops from our local tcm_vhost_ops
  1311. */
  1312. fabric->tf_ops = tcm_vhost_ops;
  1313. /*
  1314. * Setup default attribute lists for various fabric->tf_cit_tmpl
  1315. */
  1316. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
  1317. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
  1318. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
  1319. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1320. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1321. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  1322. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  1323. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  1324. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  1325. /*
  1326. * Register the fabric for use within TCM
  1327. */
  1328. ret = target_fabric_configfs_register(fabric);
  1329. if (ret < 0) {
  1330. pr_err("target_fabric_configfs_register() failed"
  1331. " for TCM_VHOST\n");
  1332. return ret;
  1333. }
  1334. /*
  1335. * Setup our local pointer to *fabric
  1336. */
  1337. tcm_vhost_fabric_configfs = fabric;
  1338. pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
  1339. return 0;
  1340. };
  1341. static void tcm_vhost_deregister_configfs(void)
  1342. {
  1343. if (!tcm_vhost_fabric_configfs)
  1344. return;
  1345. target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
  1346. tcm_vhost_fabric_configfs = NULL;
  1347. pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
  1348. };
  1349. static int __init tcm_vhost_init(void)
  1350. {
  1351. int ret = -ENOMEM;
  1352. /*
  1353. * Use our own dedicated workqueue for submitting I/O into
  1354. * target core to avoid contention within system_wq.
  1355. */
  1356. tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
  1357. if (!tcm_vhost_workqueue)
  1358. goto out;
  1359. ret = vhost_scsi_register();
  1360. if (ret < 0)
  1361. goto out_destroy_workqueue;
  1362. ret = tcm_vhost_register_configfs();
  1363. if (ret < 0)
  1364. goto out_vhost_scsi_deregister;
  1365. return 0;
  1366. out_vhost_scsi_deregister:
  1367. vhost_scsi_deregister();
  1368. out_destroy_workqueue:
  1369. destroy_workqueue(tcm_vhost_workqueue);
  1370. out:
  1371. return ret;
  1372. };
  1373. static void tcm_vhost_exit(void)
  1374. {
  1375. tcm_vhost_deregister_configfs();
  1376. vhost_scsi_deregister();
  1377. destroy_workqueue(tcm_vhost_workqueue);
  1378. };
  1379. MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
  1380. MODULE_LICENSE("GPL");
  1381. module_init(tcm_vhost_init);
  1382. module_exit(tcm_vhost_exit);