scsi.c 56 KB

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