tcm_vhost.c 42 KB

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