tcm_vhost.c 42 KB

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