scsi.c 65 KB

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