tcm_vhost.c 42 KB

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