tcm_vhost.c 42 KB

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