tcm_vhost.c 43 KB

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