scsi.c 56 KB

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