tcm_vhost.c 42 KB

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