tcm_vhost.c 42 KB

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