scsi.c 56 KB

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