tcm_vhost.c 53 KB

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