tcm_vhost.c 44 KB

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