scsi.c 56 KB

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