scsi.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407
  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 void vhost_scsi_submission_work(struct work_struct *work)
  798. {
  799. struct vhost_scsi_cmd *cmd =
  800. container_of(work, struct vhost_scsi_cmd, work);
  801. struct vhost_scsi_nexus *tv_nexus;
  802. struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
  803. struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
  804. int rc;
  805. /* FIXME: BIDI operation */
  806. if (cmd->tvc_sgl_count) {
  807. sg_ptr = cmd->tvc_sgl;
  808. if (cmd->tvc_prot_sgl_count)
  809. sg_prot_ptr = cmd->tvc_prot_sgl;
  810. else
  811. se_cmd->prot_pto = true;
  812. } else {
  813. sg_ptr = NULL;
  814. }
  815. tv_nexus = cmd->tvc_nexus;
  816. rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
  817. cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
  818. cmd->tvc_lun, cmd->tvc_exp_data_len,
  819. cmd->tvc_task_attr, cmd->tvc_data_direction,
  820. TARGET_SCF_ACK_KREF, sg_ptr, cmd->tvc_sgl_count,
  821. NULL, 0, sg_prot_ptr, cmd->tvc_prot_sgl_count);
  822. if (rc < 0) {
  823. transport_send_check_condition_and_sense(se_cmd,
  824. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  825. transport_generic_free_cmd(se_cmd, 0);
  826. }
  827. }
  828. static void
  829. vhost_scsi_send_bad_target(struct vhost_scsi *vs,
  830. struct vhost_virtqueue *vq,
  831. int head, unsigned out)
  832. {
  833. struct virtio_scsi_cmd_resp __user *resp;
  834. struct virtio_scsi_cmd_resp rsp;
  835. int ret;
  836. memset(&rsp, 0, sizeof(rsp));
  837. rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
  838. resp = vq->iov[out].iov_base;
  839. ret = __copy_to_user(resp, &rsp, sizeof(rsp));
  840. if (!ret)
  841. vhost_add_used_and_signal(&vs->dev, vq, head, 0);
  842. else
  843. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  844. }
  845. static void
  846. vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
  847. {
  848. struct vhost_scsi_tpg **vs_tpg, *tpg;
  849. struct virtio_scsi_cmd_req v_req;
  850. struct virtio_scsi_cmd_req_pi v_req_pi;
  851. struct vhost_scsi_cmd *cmd;
  852. struct iov_iter out_iter, in_iter, prot_iter, data_iter;
  853. u64 tag;
  854. u32 exp_data_len, data_direction;
  855. unsigned out, in;
  856. int head, ret, prot_bytes;
  857. size_t req_size, rsp_size = sizeof(struct virtio_scsi_cmd_resp);
  858. size_t out_size, in_size;
  859. u16 lun;
  860. u8 *target, *lunp, task_attr;
  861. bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI);
  862. void *req, *cdb;
  863. mutex_lock(&vq->mutex);
  864. /*
  865. * We can handle the vq only after the endpoint is setup by calling the
  866. * VHOST_SCSI_SET_ENDPOINT ioctl.
  867. */
  868. vs_tpg = vq->private_data;
  869. if (!vs_tpg)
  870. goto out;
  871. vhost_disable_notify(&vs->dev, vq);
  872. for (;;) {
  873. head = vhost_get_vq_desc(vq, vq->iov,
  874. ARRAY_SIZE(vq->iov), &out, &in,
  875. NULL, NULL);
  876. pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
  877. head, out, in);
  878. /* On error, stop handling until the next kick. */
  879. if (unlikely(head < 0))
  880. break;
  881. /* Nothing new? Wait for eventfd to tell us they refilled. */
  882. if (head == vq->num) {
  883. if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
  884. vhost_disable_notify(&vs->dev, vq);
  885. continue;
  886. }
  887. break;
  888. }
  889. /*
  890. * Check for a sane response buffer so we can report early
  891. * errors back to the guest.
  892. */
  893. if (unlikely(vq->iov[out].iov_len < rsp_size)) {
  894. vq_err(vq, "Expecting at least virtio_scsi_cmd_resp"
  895. " size, got %zu bytes\n", vq->iov[out].iov_len);
  896. break;
  897. }
  898. /*
  899. * Setup pointers and values based upon different virtio-scsi
  900. * request header if T10_PI is enabled in KVM guest.
  901. */
  902. if (t10_pi) {
  903. req = &v_req_pi;
  904. req_size = sizeof(v_req_pi);
  905. lunp = &v_req_pi.lun[0];
  906. target = &v_req_pi.lun[1];
  907. } else {
  908. req = &v_req;
  909. req_size = sizeof(v_req);
  910. lunp = &v_req.lun[0];
  911. target = &v_req.lun[1];
  912. }
  913. /*
  914. * FIXME: Not correct for BIDI operation
  915. */
  916. out_size = iov_length(vq->iov, out);
  917. in_size = iov_length(&vq->iov[out], in);
  918. /*
  919. * Copy over the virtio-scsi request header, which for a
  920. * ANY_LAYOUT enabled guest may span multiple iovecs, or a
  921. * single iovec may contain both the header + outgoing
  922. * WRITE payloads.
  923. *
  924. * copy_from_iter() will advance out_iter, so that it will
  925. * point at the start of the outgoing WRITE payload, if
  926. * DMA_TO_DEVICE is set.
  927. */
  928. iov_iter_init(&out_iter, WRITE, vq->iov, out, out_size);
  929. ret = copy_from_iter(req, req_size, &out_iter);
  930. if (unlikely(ret != req_size)) {
  931. vq_err(vq, "Faulted on copy_from_iter\n");
  932. vhost_scsi_send_bad_target(vs, vq, head, out);
  933. continue;
  934. }
  935. /* virtio-scsi spec requires byte 0 of the lun to be 1 */
  936. if (unlikely(*lunp != 1)) {
  937. vq_err(vq, "Illegal virtio-scsi lun: %u\n", *lunp);
  938. vhost_scsi_send_bad_target(vs, vq, head, out);
  939. continue;
  940. }
  941. tpg = ACCESS_ONCE(vs_tpg[*target]);
  942. if (unlikely(!tpg)) {
  943. /* Target does not exist, fail the request */
  944. vhost_scsi_send_bad_target(vs, vq, head, out);
  945. continue;
  946. }
  947. /*
  948. * Determine data_direction by calculating the total outgoing
  949. * iovec sizes + incoming iovec sizes vs. virtio-scsi request +
  950. * response headers respectively.
  951. *
  952. * For DMA_TO_DEVICE this is out_iter, which is already pointing
  953. * to the right place.
  954. *
  955. * For DMA_FROM_DEVICE, the iovec will be just past the end
  956. * of the virtio-scsi response header in either the same
  957. * or immediately following iovec.
  958. *
  959. * Any associated T10_PI bytes for the outgoing / incoming
  960. * payloads are included in calculation of exp_data_len here.
  961. */
  962. prot_bytes = 0;
  963. if (out_size > req_size) {
  964. data_direction = DMA_TO_DEVICE;
  965. exp_data_len = out_size - req_size;
  966. data_iter = out_iter;
  967. } else if (in_size > rsp_size) {
  968. data_direction = DMA_FROM_DEVICE;
  969. exp_data_len = in_size - rsp_size;
  970. iov_iter_init(&in_iter, READ, &vq->iov[out], in,
  971. rsp_size + exp_data_len);
  972. iov_iter_advance(&in_iter, rsp_size);
  973. data_iter = in_iter;
  974. } else {
  975. data_direction = DMA_NONE;
  976. exp_data_len = 0;
  977. }
  978. /*
  979. * If T10_PI header + payload is present, setup prot_iter values
  980. * and recalculate data_iter for vhost_scsi_mapal() mapping to
  981. * host scatterlists via get_user_pages_fast().
  982. */
  983. if (t10_pi) {
  984. if (v_req_pi.pi_bytesout) {
  985. if (data_direction != DMA_TO_DEVICE) {
  986. vq_err(vq, "Received non zero pi_bytesout,"
  987. " but wrong data_direction\n");
  988. vhost_scsi_send_bad_target(vs, vq, head, out);
  989. continue;
  990. }
  991. prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout);
  992. } else if (v_req_pi.pi_bytesin) {
  993. if (data_direction != DMA_FROM_DEVICE) {
  994. vq_err(vq, "Received non zero pi_bytesin,"
  995. " but wrong data_direction\n");
  996. vhost_scsi_send_bad_target(vs, vq, head, out);
  997. continue;
  998. }
  999. prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin);
  1000. }
  1001. /*
  1002. * Set prot_iter to data_iter, and advance past any
  1003. * preceeding prot_bytes that may be present.
  1004. *
  1005. * Also fix up the exp_data_len to reflect only the
  1006. * actual data payload length.
  1007. */
  1008. if (prot_bytes) {
  1009. exp_data_len -= prot_bytes;
  1010. prot_iter = data_iter;
  1011. iov_iter_advance(&data_iter, prot_bytes);
  1012. }
  1013. tag = vhost64_to_cpu(vq, v_req_pi.tag);
  1014. task_attr = v_req_pi.task_attr;
  1015. cdb = &v_req_pi.cdb[0];
  1016. lun = ((v_req_pi.lun[2] << 8) | v_req_pi.lun[3]) & 0x3FFF;
  1017. } else {
  1018. tag = vhost64_to_cpu(vq, v_req.tag);
  1019. task_attr = v_req.task_attr;
  1020. cdb = &v_req.cdb[0];
  1021. lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
  1022. }
  1023. /*
  1024. * Check that the received CDB size does not exceeded our
  1025. * hardcoded max for vhost-scsi, then get a pre-allocated
  1026. * cmd descriptor for the new virtio-scsi tag.
  1027. *
  1028. * TODO what if cdb was too small for varlen cdb header?
  1029. */
  1030. if (unlikely(scsi_command_size(cdb) > VHOST_SCSI_MAX_CDB_SIZE)) {
  1031. vq_err(vq, "Received SCSI CDB with command_size: %d that"
  1032. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1033. scsi_command_size(cdb), VHOST_SCSI_MAX_CDB_SIZE);
  1034. vhost_scsi_send_bad_target(vs, vq, head, out);
  1035. continue;
  1036. }
  1037. cmd = vhost_scsi_get_tag(vq, tpg, cdb, tag, lun, task_attr,
  1038. exp_data_len + prot_bytes,
  1039. data_direction);
  1040. if (IS_ERR(cmd)) {
  1041. vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
  1042. PTR_ERR(cmd));
  1043. vhost_scsi_send_bad_target(vs, vq, head, out);
  1044. continue;
  1045. }
  1046. cmd->tvc_vhost = vs;
  1047. cmd->tvc_vq = vq;
  1048. cmd->tvc_resp_iov = &vq->iov[out];
  1049. cmd->tvc_in_iovs = in;
  1050. pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
  1051. cmd->tvc_cdb[0], cmd->tvc_lun);
  1052. pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
  1053. " %d\n", cmd, exp_data_len, prot_bytes, data_direction);
  1054. if (data_direction != DMA_NONE) {
  1055. ret = vhost_scsi_mapal(cmd,
  1056. prot_bytes, &prot_iter,
  1057. exp_data_len, &data_iter);
  1058. if (unlikely(ret)) {
  1059. vq_err(vq, "Failed to map iov to sgl\n");
  1060. vhost_scsi_release_cmd(&cmd->tvc_se_cmd);
  1061. vhost_scsi_send_bad_target(vs, vq, head, out);
  1062. continue;
  1063. }
  1064. }
  1065. /*
  1066. * Save the descriptor from vhost_get_vq_desc() to be used to
  1067. * complete the virtio-scsi request in TCM callback context via
  1068. * vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
  1069. */
  1070. cmd->tvc_vq_desc = head;
  1071. /*
  1072. * Dispatch cmd descriptor for cmwq execution in process
  1073. * context provided by vhost_scsi_workqueue. This also ensures
  1074. * cmd is executed on the same kworker CPU as this vhost
  1075. * thread to gain positive L2 cache locality effects.
  1076. */
  1077. INIT_WORK(&cmd->work, vhost_scsi_submission_work);
  1078. queue_work(vhost_scsi_workqueue, &cmd->work);
  1079. }
  1080. out:
  1081. mutex_unlock(&vq->mutex);
  1082. }
  1083. static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
  1084. {
  1085. pr_debug("%s: The handling func for control queue.\n", __func__);
  1086. }
  1087. static void
  1088. vhost_scsi_send_evt(struct vhost_scsi *vs,
  1089. struct vhost_scsi_tpg *tpg,
  1090. struct se_lun *lun,
  1091. u32 event,
  1092. u32 reason)
  1093. {
  1094. struct vhost_scsi_evt *evt;
  1095. evt = vhost_scsi_allocate_evt(vs, event, reason);
  1096. if (!evt)
  1097. return;
  1098. if (tpg && lun) {
  1099. /* TODO: share lun setup code with virtio-scsi.ko */
  1100. /*
  1101. * Note: evt->event is zeroed when we allocate it and
  1102. * lun[4-7] need to be zero according to virtio-scsi spec.
  1103. */
  1104. evt->event.lun[0] = 0x01;
  1105. evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
  1106. if (lun->unpacked_lun >= 256)
  1107. evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
  1108. evt->event.lun[3] = lun->unpacked_lun & 0xFF;
  1109. }
  1110. llist_add(&evt->list, &vs->vs_event_list);
  1111. vhost_work_queue(&vs->dev, &vs->vs_event_work);
  1112. }
  1113. static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
  1114. {
  1115. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1116. poll.work);
  1117. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  1118. mutex_lock(&vq->mutex);
  1119. if (!vq->private_data)
  1120. goto out;
  1121. if (vs->vs_events_missed)
  1122. vhost_scsi_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
  1123. out:
  1124. mutex_unlock(&vq->mutex);
  1125. }
  1126. static void vhost_scsi_handle_kick(struct vhost_work *work)
  1127. {
  1128. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1129. poll.work);
  1130. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  1131. vhost_scsi_handle_vq(vs, vq);
  1132. }
  1133. static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
  1134. {
  1135. vhost_poll_flush(&vs->vqs[index].vq.poll);
  1136. }
  1137. /* Callers must hold dev mutex */
  1138. static void vhost_scsi_flush(struct vhost_scsi *vs)
  1139. {
  1140. struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
  1141. int i;
  1142. /* Init new inflight and remember the old inflight */
  1143. vhost_scsi_init_inflight(vs, old_inflight);
  1144. /*
  1145. * The inflight->kref was initialized to 1. We decrement it here to
  1146. * indicate the start of the flush operation so that it will reach 0
  1147. * when all the reqs are finished.
  1148. */
  1149. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1150. kref_put(&old_inflight[i]->kref, vhost_scsi_done_inflight);
  1151. /* Flush both the vhost poll and vhost work */
  1152. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1153. vhost_scsi_flush_vq(vs, i);
  1154. vhost_work_flush(&vs->dev, &vs->vs_completion_work);
  1155. vhost_work_flush(&vs->dev, &vs->vs_event_work);
  1156. /* Wait for all reqs issued before the flush to be finished */
  1157. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  1158. wait_for_completion(&old_inflight[i]->comp);
  1159. }
  1160. /*
  1161. * Called from vhost_scsi_ioctl() context to walk the list of available
  1162. * vhost_scsi_tpg with an active struct vhost_scsi_nexus
  1163. *
  1164. * The lock nesting rule is:
  1165. * vhost_scsi_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
  1166. */
  1167. static int
  1168. vhost_scsi_set_endpoint(struct vhost_scsi *vs,
  1169. struct vhost_scsi_target *t)
  1170. {
  1171. struct se_portal_group *se_tpg;
  1172. struct vhost_scsi_tport *tv_tport;
  1173. struct vhost_scsi_tpg *tpg;
  1174. struct vhost_scsi_tpg **vs_tpg;
  1175. struct vhost_virtqueue *vq;
  1176. int index, ret, i, len;
  1177. bool match = false;
  1178. mutex_lock(&vhost_scsi_mutex);
  1179. mutex_lock(&vs->dev.mutex);
  1180. /* Verify that ring has been setup correctly. */
  1181. for (index = 0; index < vs->dev.nvqs; ++index) {
  1182. /* Verify that ring has been setup correctly. */
  1183. if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
  1184. ret = -EFAULT;
  1185. goto out;
  1186. }
  1187. }
  1188. len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
  1189. vs_tpg = kzalloc(len, GFP_KERNEL);
  1190. if (!vs_tpg) {
  1191. ret = -ENOMEM;
  1192. goto out;
  1193. }
  1194. if (vs->vs_tpg)
  1195. memcpy(vs_tpg, vs->vs_tpg, len);
  1196. list_for_each_entry(tpg, &vhost_scsi_list, tv_tpg_list) {
  1197. mutex_lock(&tpg->tv_tpg_mutex);
  1198. if (!tpg->tpg_nexus) {
  1199. mutex_unlock(&tpg->tv_tpg_mutex);
  1200. continue;
  1201. }
  1202. if (tpg->tv_tpg_vhost_count != 0) {
  1203. mutex_unlock(&tpg->tv_tpg_mutex);
  1204. continue;
  1205. }
  1206. tv_tport = tpg->tport;
  1207. if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  1208. if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
  1209. kfree(vs_tpg);
  1210. mutex_unlock(&tpg->tv_tpg_mutex);
  1211. ret = -EEXIST;
  1212. goto out;
  1213. }
  1214. /*
  1215. * In order to ensure individual vhost-scsi configfs
  1216. * groups cannot be removed while in use by vhost ioctl,
  1217. * go ahead and take an explicit se_tpg->tpg_group.cg_item
  1218. * dependency now.
  1219. */
  1220. se_tpg = &tpg->se_tpg;
  1221. ret = configfs_depend_item(se_tpg->se_tpg_tfo->tf_subsys,
  1222. &se_tpg->tpg_group.cg_item);
  1223. if (ret) {
  1224. pr_warn("configfs_depend_item() failed: %d\n", ret);
  1225. kfree(vs_tpg);
  1226. mutex_unlock(&tpg->tv_tpg_mutex);
  1227. goto out;
  1228. }
  1229. tpg->tv_tpg_vhost_count++;
  1230. tpg->vhost_scsi = vs;
  1231. vs_tpg[tpg->tport_tpgt] = tpg;
  1232. smp_mb__after_atomic();
  1233. match = true;
  1234. }
  1235. mutex_unlock(&tpg->tv_tpg_mutex);
  1236. }
  1237. if (match) {
  1238. memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
  1239. sizeof(vs->vs_vhost_wwpn));
  1240. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1241. vq = &vs->vqs[i].vq;
  1242. mutex_lock(&vq->mutex);
  1243. vq->private_data = vs_tpg;
  1244. vhost_init_used(vq);
  1245. mutex_unlock(&vq->mutex);
  1246. }
  1247. ret = 0;
  1248. } else {
  1249. ret = -EEXIST;
  1250. }
  1251. /*
  1252. * Act as synchronize_rcu to make sure access to
  1253. * old vs->vs_tpg is finished.
  1254. */
  1255. vhost_scsi_flush(vs);
  1256. kfree(vs->vs_tpg);
  1257. vs->vs_tpg = vs_tpg;
  1258. out:
  1259. mutex_unlock(&vs->dev.mutex);
  1260. mutex_unlock(&vhost_scsi_mutex);
  1261. return ret;
  1262. }
  1263. static int
  1264. vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
  1265. struct vhost_scsi_target *t)
  1266. {
  1267. struct se_portal_group *se_tpg;
  1268. struct vhost_scsi_tport *tv_tport;
  1269. struct vhost_scsi_tpg *tpg;
  1270. struct vhost_virtqueue *vq;
  1271. bool match = false;
  1272. int index, ret, i;
  1273. u8 target;
  1274. mutex_lock(&vhost_scsi_mutex);
  1275. mutex_lock(&vs->dev.mutex);
  1276. /* Verify that ring has been setup correctly. */
  1277. for (index = 0; index < vs->dev.nvqs; ++index) {
  1278. if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
  1279. ret = -EFAULT;
  1280. goto err_dev;
  1281. }
  1282. }
  1283. if (!vs->vs_tpg) {
  1284. ret = 0;
  1285. goto err_dev;
  1286. }
  1287. for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
  1288. target = i;
  1289. tpg = vs->vs_tpg[target];
  1290. if (!tpg)
  1291. continue;
  1292. mutex_lock(&tpg->tv_tpg_mutex);
  1293. tv_tport = tpg->tport;
  1294. if (!tv_tport) {
  1295. ret = -ENODEV;
  1296. goto err_tpg;
  1297. }
  1298. if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  1299. pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
  1300. " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
  1301. tv_tport->tport_name, tpg->tport_tpgt,
  1302. t->vhost_wwpn, t->vhost_tpgt);
  1303. ret = -EINVAL;
  1304. goto err_tpg;
  1305. }
  1306. tpg->tv_tpg_vhost_count--;
  1307. tpg->vhost_scsi = NULL;
  1308. vs->vs_tpg[target] = NULL;
  1309. match = true;
  1310. mutex_unlock(&tpg->tv_tpg_mutex);
  1311. /*
  1312. * Release se_tpg->tpg_group.cg_item configfs dependency now
  1313. * to allow vhost-scsi WWPN se_tpg->tpg_group shutdown to occur.
  1314. */
  1315. se_tpg = &tpg->se_tpg;
  1316. configfs_undepend_item(se_tpg->se_tpg_tfo->tf_subsys,
  1317. &se_tpg->tpg_group.cg_item);
  1318. }
  1319. if (match) {
  1320. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1321. vq = &vs->vqs[i].vq;
  1322. mutex_lock(&vq->mutex);
  1323. vq->private_data = NULL;
  1324. mutex_unlock(&vq->mutex);
  1325. }
  1326. }
  1327. /*
  1328. * Act as synchronize_rcu to make sure access to
  1329. * old vs->vs_tpg is finished.
  1330. */
  1331. vhost_scsi_flush(vs);
  1332. kfree(vs->vs_tpg);
  1333. vs->vs_tpg = NULL;
  1334. WARN_ON(vs->vs_events_nr);
  1335. mutex_unlock(&vs->dev.mutex);
  1336. mutex_unlock(&vhost_scsi_mutex);
  1337. return 0;
  1338. err_tpg:
  1339. mutex_unlock(&tpg->tv_tpg_mutex);
  1340. err_dev:
  1341. mutex_unlock(&vs->dev.mutex);
  1342. mutex_unlock(&vhost_scsi_mutex);
  1343. return ret;
  1344. }
  1345. static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
  1346. {
  1347. struct vhost_virtqueue *vq;
  1348. int i;
  1349. if (features & ~VHOST_SCSI_FEATURES)
  1350. return -EOPNOTSUPP;
  1351. mutex_lock(&vs->dev.mutex);
  1352. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  1353. !vhost_log_access_ok(&vs->dev)) {
  1354. mutex_unlock(&vs->dev.mutex);
  1355. return -EFAULT;
  1356. }
  1357. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  1358. vq = &vs->vqs[i].vq;
  1359. mutex_lock(&vq->mutex);
  1360. vq->acked_features = features;
  1361. mutex_unlock(&vq->mutex);
  1362. }
  1363. mutex_unlock(&vs->dev.mutex);
  1364. return 0;
  1365. }
  1366. static int vhost_scsi_open(struct inode *inode, struct file *f)
  1367. {
  1368. struct vhost_scsi *vs;
  1369. struct vhost_virtqueue **vqs;
  1370. int r = -ENOMEM, i;
  1371. vs = kzalloc(sizeof(*vs), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  1372. if (!vs) {
  1373. vs = vzalloc(sizeof(*vs));
  1374. if (!vs)
  1375. goto err_vs;
  1376. }
  1377. vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
  1378. if (!vqs)
  1379. goto err_vqs;
  1380. vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
  1381. vhost_work_init(&vs->vs_event_work, vhost_scsi_evt_work);
  1382. vs->vs_events_nr = 0;
  1383. vs->vs_events_missed = false;
  1384. vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
  1385. vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1386. vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
  1387. vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
  1388. for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
  1389. vqs[i] = &vs->vqs[i].vq;
  1390. vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
  1391. }
  1392. vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
  1393. vhost_scsi_init_inflight(vs, NULL);
  1394. f->private_data = vs;
  1395. return 0;
  1396. err_vqs:
  1397. kvfree(vs);
  1398. err_vs:
  1399. return r;
  1400. }
  1401. static int vhost_scsi_release(struct inode *inode, struct file *f)
  1402. {
  1403. struct vhost_scsi *vs = f->private_data;
  1404. struct vhost_scsi_target t;
  1405. mutex_lock(&vs->dev.mutex);
  1406. memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
  1407. mutex_unlock(&vs->dev.mutex);
  1408. vhost_scsi_clear_endpoint(vs, &t);
  1409. vhost_dev_stop(&vs->dev);
  1410. vhost_dev_cleanup(&vs->dev, false);
  1411. /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
  1412. vhost_scsi_flush(vs);
  1413. kfree(vs->dev.vqs);
  1414. kvfree(vs);
  1415. return 0;
  1416. }
  1417. static long
  1418. vhost_scsi_ioctl(struct file *f,
  1419. unsigned int ioctl,
  1420. unsigned long arg)
  1421. {
  1422. struct vhost_scsi *vs = f->private_data;
  1423. struct vhost_scsi_target backend;
  1424. void __user *argp = (void __user *)arg;
  1425. u64 __user *featurep = argp;
  1426. u32 __user *eventsp = argp;
  1427. u32 events_missed;
  1428. u64 features;
  1429. int r, abi_version = VHOST_SCSI_ABI_VERSION;
  1430. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1431. switch (ioctl) {
  1432. case VHOST_SCSI_SET_ENDPOINT:
  1433. if (copy_from_user(&backend, argp, sizeof backend))
  1434. return -EFAULT;
  1435. if (backend.reserved != 0)
  1436. return -EOPNOTSUPP;
  1437. return vhost_scsi_set_endpoint(vs, &backend);
  1438. case VHOST_SCSI_CLEAR_ENDPOINT:
  1439. if (copy_from_user(&backend, argp, sizeof backend))
  1440. return -EFAULT;
  1441. if (backend.reserved != 0)
  1442. return -EOPNOTSUPP;
  1443. return vhost_scsi_clear_endpoint(vs, &backend);
  1444. case VHOST_SCSI_GET_ABI_VERSION:
  1445. if (copy_to_user(argp, &abi_version, sizeof abi_version))
  1446. return -EFAULT;
  1447. return 0;
  1448. case VHOST_SCSI_SET_EVENTS_MISSED:
  1449. if (get_user(events_missed, eventsp))
  1450. return -EFAULT;
  1451. mutex_lock(&vq->mutex);
  1452. vs->vs_events_missed = events_missed;
  1453. mutex_unlock(&vq->mutex);
  1454. return 0;
  1455. case VHOST_SCSI_GET_EVENTS_MISSED:
  1456. mutex_lock(&vq->mutex);
  1457. events_missed = vs->vs_events_missed;
  1458. mutex_unlock(&vq->mutex);
  1459. if (put_user(events_missed, eventsp))
  1460. return -EFAULT;
  1461. return 0;
  1462. case VHOST_GET_FEATURES:
  1463. features = VHOST_SCSI_FEATURES;
  1464. if (copy_to_user(featurep, &features, sizeof features))
  1465. return -EFAULT;
  1466. return 0;
  1467. case VHOST_SET_FEATURES:
  1468. if (copy_from_user(&features, featurep, sizeof features))
  1469. return -EFAULT;
  1470. return vhost_scsi_set_features(vs, features);
  1471. default:
  1472. mutex_lock(&vs->dev.mutex);
  1473. r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
  1474. /* TODO: flush backend after dev ioctl. */
  1475. if (r == -ENOIOCTLCMD)
  1476. r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
  1477. mutex_unlock(&vs->dev.mutex);
  1478. return r;
  1479. }
  1480. }
  1481. #ifdef CONFIG_COMPAT
  1482. static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
  1483. unsigned long arg)
  1484. {
  1485. return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  1486. }
  1487. #endif
  1488. static const struct file_operations vhost_scsi_fops = {
  1489. .owner = THIS_MODULE,
  1490. .release = vhost_scsi_release,
  1491. .unlocked_ioctl = vhost_scsi_ioctl,
  1492. #ifdef CONFIG_COMPAT
  1493. .compat_ioctl = vhost_scsi_compat_ioctl,
  1494. #endif
  1495. .open = vhost_scsi_open,
  1496. .llseek = noop_llseek,
  1497. };
  1498. static struct miscdevice vhost_scsi_misc = {
  1499. MISC_DYNAMIC_MINOR,
  1500. "vhost-scsi",
  1501. &vhost_scsi_fops,
  1502. };
  1503. static int __init vhost_scsi_register(void)
  1504. {
  1505. return misc_register(&vhost_scsi_misc);
  1506. }
  1507. static int vhost_scsi_deregister(void)
  1508. {
  1509. return misc_deregister(&vhost_scsi_misc);
  1510. }
  1511. static char *vhost_scsi_dump_proto_id(struct vhost_scsi_tport *tport)
  1512. {
  1513. switch (tport->tport_proto_id) {
  1514. case SCSI_PROTOCOL_SAS:
  1515. return "SAS";
  1516. case SCSI_PROTOCOL_FCP:
  1517. return "FCP";
  1518. case SCSI_PROTOCOL_ISCSI:
  1519. return "iSCSI";
  1520. default:
  1521. break;
  1522. }
  1523. return "Unknown";
  1524. }
  1525. static void
  1526. vhost_scsi_do_plug(struct vhost_scsi_tpg *tpg,
  1527. struct se_lun *lun, bool plug)
  1528. {
  1529. struct vhost_scsi *vs = tpg->vhost_scsi;
  1530. struct vhost_virtqueue *vq;
  1531. u32 reason;
  1532. if (!vs)
  1533. return;
  1534. mutex_lock(&vs->dev.mutex);
  1535. if (plug)
  1536. reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
  1537. else
  1538. reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
  1539. vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
  1540. mutex_lock(&vq->mutex);
  1541. if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG))
  1542. vhost_scsi_send_evt(vs, tpg, lun,
  1543. VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
  1544. mutex_unlock(&vq->mutex);
  1545. mutex_unlock(&vs->dev.mutex);
  1546. }
  1547. static void vhost_scsi_hotplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
  1548. {
  1549. vhost_scsi_do_plug(tpg, lun, true);
  1550. }
  1551. static void vhost_scsi_hotunplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
  1552. {
  1553. vhost_scsi_do_plug(tpg, lun, false);
  1554. }
  1555. static int vhost_scsi_port_link(struct se_portal_group *se_tpg,
  1556. struct se_lun *lun)
  1557. {
  1558. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1559. struct vhost_scsi_tpg, se_tpg);
  1560. mutex_lock(&vhost_scsi_mutex);
  1561. mutex_lock(&tpg->tv_tpg_mutex);
  1562. tpg->tv_tpg_port_count++;
  1563. mutex_unlock(&tpg->tv_tpg_mutex);
  1564. vhost_scsi_hotplug(tpg, lun);
  1565. mutex_unlock(&vhost_scsi_mutex);
  1566. return 0;
  1567. }
  1568. static void vhost_scsi_port_unlink(struct se_portal_group *se_tpg,
  1569. struct se_lun *lun)
  1570. {
  1571. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1572. struct vhost_scsi_tpg, se_tpg);
  1573. mutex_lock(&vhost_scsi_mutex);
  1574. mutex_lock(&tpg->tv_tpg_mutex);
  1575. tpg->tv_tpg_port_count--;
  1576. mutex_unlock(&tpg->tv_tpg_mutex);
  1577. vhost_scsi_hotunplug(tpg, lun);
  1578. mutex_unlock(&vhost_scsi_mutex);
  1579. }
  1580. static struct se_node_acl *
  1581. vhost_scsi_make_nodeacl(struct se_portal_group *se_tpg,
  1582. struct config_group *group,
  1583. const char *name)
  1584. {
  1585. struct se_node_acl *se_nacl, *se_nacl_new;
  1586. struct vhost_scsi_nacl *nacl;
  1587. u64 wwpn = 0;
  1588. u32 nexus_depth;
  1589. /* vhost_scsi_parse_wwn(name, &wwpn, 1) < 0)
  1590. return ERR_PTR(-EINVAL); */
  1591. se_nacl_new = vhost_scsi_alloc_fabric_acl(se_tpg);
  1592. if (!se_nacl_new)
  1593. return ERR_PTR(-ENOMEM);
  1594. nexus_depth = 1;
  1595. /*
  1596. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  1597. * when converting a NodeACL from demo mode -> explict
  1598. */
  1599. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  1600. name, nexus_depth);
  1601. if (IS_ERR(se_nacl)) {
  1602. vhost_scsi_release_fabric_acl(se_tpg, se_nacl_new);
  1603. return se_nacl;
  1604. }
  1605. /*
  1606. * Locate our struct vhost_scsi_nacl and set the FC Nport WWPN
  1607. */
  1608. nacl = container_of(se_nacl, struct vhost_scsi_nacl, se_node_acl);
  1609. nacl->iport_wwpn = wwpn;
  1610. return se_nacl;
  1611. }
  1612. static void vhost_scsi_drop_nodeacl(struct se_node_acl *se_acl)
  1613. {
  1614. struct vhost_scsi_nacl *nacl = container_of(se_acl,
  1615. struct vhost_scsi_nacl, se_node_acl);
  1616. core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
  1617. kfree(nacl);
  1618. }
  1619. static void vhost_scsi_free_cmd_map_res(struct vhost_scsi_nexus *nexus,
  1620. struct se_session *se_sess)
  1621. {
  1622. struct vhost_scsi_cmd *tv_cmd;
  1623. unsigned int i;
  1624. if (!se_sess->sess_cmd_map)
  1625. return;
  1626. for (i = 0; i < VHOST_SCSI_DEFAULT_TAGS; i++) {
  1627. tv_cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[i];
  1628. kfree(tv_cmd->tvc_sgl);
  1629. kfree(tv_cmd->tvc_prot_sgl);
  1630. kfree(tv_cmd->tvc_upages);
  1631. }
  1632. }
  1633. static int vhost_scsi_make_nexus(struct vhost_scsi_tpg *tpg,
  1634. const char *name)
  1635. {
  1636. struct se_portal_group *se_tpg;
  1637. struct se_session *se_sess;
  1638. struct vhost_scsi_nexus *tv_nexus;
  1639. struct vhost_scsi_cmd *tv_cmd;
  1640. unsigned int i;
  1641. mutex_lock(&tpg->tv_tpg_mutex);
  1642. if (tpg->tpg_nexus) {
  1643. mutex_unlock(&tpg->tv_tpg_mutex);
  1644. pr_debug("tpg->tpg_nexus already exists\n");
  1645. return -EEXIST;
  1646. }
  1647. se_tpg = &tpg->se_tpg;
  1648. tv_nexus = kzalloc(sizeof(struct vhost_scsi_nexus), GFP_KERNEL);
  1649. if (!tv_nexus) {
  1650. mutex_unlock(&tpg->tv_tpg_mutex);
  1651. pr_err("Unable to allocate struct vhost_scsi_nexus\n");
  1652. return -ENOMEM;
  1653. }
  1654. /*
  1655. * Initialize the struct se_session pointer and setup tagpool
  1656. * for struct vhost_scsi_cmd descriptors
  1657. */
  1658. tv_nexus->tvn_se_sess = transport_init_session_tags(
  1659. VHOST_SCSI_DEFAULT_TAGS,
  1660. sizeof(struct vhost_scsi_cmd),
  1661. TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS);
  1662. if (IS_ERR(tv_nexus->tvn_se_sess)) {
  1663. mutex_unlock(&tpg->tv_tpg_mutex);
  1664. kfree(tv_nexus);
  1665. return -ENOMEM;
  1666. }
  1667. se_sess = tv_nexus->tvn_se_sess;
  1668. for (i = 0; i < VHOST_SCSI_DEFAULT_TAGS; i++) {
  1669. tv_cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[i];
  1670. tv_cmd->tvc_sgl = kzalloc(sizeof(struct scatterlist) *
  1671. VHOST_SCSI_PREALLOC_SGLS, GFP_KERNEL);
  1672. if (!tv_cmd->tvc_sgl) {
  1673. mutex_unlock(&tpg->tv_tpg_mutex);
  1674. pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
  1675. goto out;
  1676. }
  1677. tv_cmd->tvc_upages = kzalloc(sizeof(struct page *) *
  1678. VHOST_SCSI_PREALLOC_UPAGES, GFP_KERNEL);
  1679. if (!tv_cmd->tvc_upages) {
  1680. mutex_unlock(&tpg->tv_tpg_mutex);
  1681. pr_err("Unable to allocate tv_cmd->tvc_upages\n");
  1682. goto out;
  1683. }
  1684. tv_cmd->tvc_prot_sgl = kzalloc(sizeof(struct scatterlist) *
  1685. VHOST_SCSI_PREALLOC_PROT_SGLS, GFP_KERNEL);
  1686. if (!tv_cmd->tvc_prot_sgl) {
  1687. mutex_unlock(&tpg->tv_tpg_mutex);
  1688. pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
  1689. goto out;
  1690. }
  1691. }
  1692. /*
  1693. * Since we are running in 'demo mode' this call with generate a
  1694. * struct se_node_acl for the vhost_scsi struct se_portal_group with
  1695. * the SCSI Initiator port name of the passed configfs group 'name'.
  1696. */
  1697. tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  1698. se_tpg, (unsigned char *)name);
  1699. if (!tv_nexus->tvn_se_sess->se_node_acl) {
  1700. mutex_unlock(&tpg->tv_tpg_mutex);
  1701. pr_debug("core_tpg_check_initiator_node_acl() failed"
  1702. " for %s\n", name);
  1703. goto out;
  1704. }
  1705. /*
  1706. * Now register the TCM vhost virtual I_T Nexus as active with the
  1707. * call to __transport_register_session()
  1708. */
  1709. __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
  1710. tv_nexus->tvn_se_sess, tv_nexus);
  1711. tpg->tpg_nexus = tv_nexus;
  1712. mutex_unlock(&tpg->tv_tpg_mutex);
  1713. return 0;
  1714. out:
  1715. vhost_scsi_free_cmd_map_res(tv_nexus, se_sess);
  1716. transport_free_session(se_sess);
  1717. kfree(tv_nexus);
  1718. return -ENOMEM;
  1719. }
  1720. static int vhost_scsi_drop_nexus(struct vhost_scsi_tpg *tpg)
  1721. {
  1722. struct se_session *se_sess;
  1723. struct vhost_scsi_nexus *tv_nexus;
  1724. mutex_lock(&tpg->tv_tpg_mutex);
  1725. tv_nexus = tpg->tpg_nexus;
  1726. if (!tv_nexus) {
  1727. mutex_unlock(&tpg->tv_tpg_mutex);
  1728. return -ENODEV;
  1729. }
  1730. se_sess = tv_nexus->tvn_se_sess;
  1731. if (!se_sess) {
  1732. mutex_unlock(&tpg->tv_tpg_mutex);
  1733. return -ENODEV;
  1734. }
  1735. if (tpg->tv_tpg_port_count != 0) {
  1736. mutex_unlock(&tpg->tv_tpg_mutex);
  1737. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1738. " active TPG port count: %d\n",
  1739. tpg->tv_tpg_port_count);
  1740. return -EBUSY;
  1741. }
  1742. if (tpg->tv_tpg_vhost_count != 0) {
  1743. mutex_unlock(&tpg->tv_tpg_mutex);
  1744. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1745. " active TPG vhost count: %d\n",
  1746. tpg->tv_tpg_vhost_count);
  1747. return -EBUSY;
  1748. }
  1749. pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
  1750. " %s Initiator Port: %s\n", vhost_scsi_dump_proto_id(tpg->tport),
  1751. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1752. vhost_scsi_free_cmd_map_res(tv_nexus, se_sess);
  1753. /*
  1754. * Release the SCSI I_T Nexus to the emulated vhost Target Port
  1755. */
  1756. transport_deregister_session(tv_nexus->tvn_se_sess);
  1757. tpg->tpg_nexus = NULL;
  1758. mutex_unlock(&tpg->tv_tpg_mutex);
  1759. kfree(tv_nexus);
  1760. return 0;
  1761. }
  1762. static ssize_t vhost_scsi_tpg_show_nexus(struct se_portal_group *se_tpg,
  1763. char *page)
  1764. {
  1765. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1766. struct vhost_scsi_tpg, se_tpg);
  1767. struct vhost_scsi_nexus *tv_nexus;
  1768. ssize_t ret;
  1769. mutex_lock(&tpg->tv_tpg_mutex);
  1770. tv_nexus = tpg->tpg_nexus;
  1771. if (!tv_nexus) {
  1772. mutex_unlock(&tpg->tv_tpg_mutex);
  1773. return -ENODEV;
  1774. }
  1775. ret = snprintf(page, PAGE_SIZE, "%s\n",
  1776. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1777. mutex_unlock(&tpg->tv_tpg_mutex);
  1778. return ret;
  1779. }
  1780. static ssize_t vhost_scsi_tpg_store_nexus(struct se_portal_group *se_tpg,
  1781. const char *page,
  1782. size_t count)
  1783. {
  1784. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1785. struct vhost_scsi_tpg, se_tpg);
  1786. struct vhost_scsi_tport *tport_wwn = tpg->tport;
  1787. unsigned char i_port[VHOST_SCSI_NAMELEN], *ptr, *port_ptr;
  1788. int ret;
  1789. /*
  1790. * Shutdown the active I_T nexus if 'NULL' is passed..
  1791. */
  1792. if (!strncmp(page, "NULL", 4)) {
  1793. ret = vhost_scsi_drop_nexus(tpg);
  1794. return (!ret) ? count : ret;
  1795. }
  1796. /*
  1797. * Otherwise make sure the passed virtual Initiator port WWN matches
  1798. * the fabric protocol_id set in vhost_scsi_make_tport(), and call
  1799. * vhost_scsi_make_nexus().
  1800. */
  1801. if (strlen(page) >= VHOST_SCSI_NAMELEN) {
  1802. pr_err("Emulated NAA Sas Address: %s, exceeds"
  1803. " max: %d\n", page, VHOST_SCSI_NAMELEN);
  1804. return -EINVAL;
  1805. }
  1806. snprintf(&i_port[0], VHOST_SCSI_NAMELEN, "%s", page);
  1807. ptr = strstr(i_port, "naa.");
  1808. if (ptr) {
  1809. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
  1810. pr_err("Passed SAS Initiator Port %s does not"
  1811. " match target port protoid: %s\n", i_port,
  1812. vhost_scsi_dump_proto_id(tport_wwn));
  1813. return -EINVAL;
  1814. }
  1815. port_ptr = &i_port[0];
  1816. goto check_newline;
  1817. }
  1818. ptr = strstr(i_port, "fc.");
  1819. if (ptr) {
  1820. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
  1821. pr_err("Passed FCP Initiator Port %s does not"
  1822. " match target port protoid: %s\n", i_port,
  1823. vhost_scsi_dump_proto_id(tport_wwn));
  1824. return -EINVAL;
  1825. }
  1826. port_ptr = &i_port[3]; /* Skip over "fc." */
  1827. goto check_newline;
  1828. }
  1829. ptr = strstr(i_port, "iqn.");
  1830. if (ptr) {
  1831. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
  1832. pr_err("Passed iSCSI Initiator Port %s does not"
  1833. " match target port protoid: %s\n", i_port,
  1834. vhost_scsi_dump_proto_id(tport_wwn));
  1835. return -EINVAL;
  1836. }
  1837. port_ptr = &i_port[0];
  1838. goto check_newline;
  1839. }
  1840. pr_err("Unable to locate prefix for emulated Initiator Port:"
  1841. " %s\n", i_port);
  1842. return -EINVAL;
  1843. /*
  1844. * Clear any trailing newline for the NAA WWN
  1845. */
  1846. check_newline:
  1847. if (i_port[strlen(i_port)-1] == '\n')
  1848. i_port[strlen(i_port)-1] = '\0';
  1849. ret = vhost_scsi_make_nexus(tpg, port_ptr);
  1850. if (ret < 0)
  1851. return ret;
  1852. return count;
  1853. }
  1854. TF_TPG_BASE_ATTR(vhost_scsi, nexus, S_IRUGO | S_IWUSR);
  1855. static struct configfs_attribute *vhost_scsi_tpg_attrs[] = {
  1856. &vhost_scsi_tpg_nexus.attr,
  1857. NULL,
  1858. };
  1859. static struct se_portal_group *
  1860. vhost_scsi_make_tpg(struct se_wwn *wwn,
  1861. struct config_group *group,
  1862. const char *name)
  1863. {
  1864. struct vhost_scsi_tport *tport = container_of(wwn,
  1865. struct vhost_scsi_tport, tport_wwn);
  1866. struct vhost_scsi_tpg *tpg;
  1867. unsigned long tpgt;
  1868. int ret;
  1869. if (strstr(name, "tpgt_") != name)
  1870. return ERR_PTR(-EINVAL);
  1871. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
  1872. return ERR_PTR(-EINVAL);
  1873. tpg = kzalloc(sizeof(struct vhost_scsi_tpg), GFP_KERNEL);
  1874. if (!tpg) {
  1875. pr_err("Unable to allocate struct vhost_scsi_tpg");
  1876. return ERR_PTR(-ENOMEM);
  1877. }
  1878. mutex_init(&tpg->tv_tpg_mutex);
  1879. INIT_LIST_HEAD(&tpg->tv_tpg_list);
  1880. tpg->tport = tport;
  1881. tpg->tport_tpgt = tpgt;
  1882. ret = core_tpg_register(&vhost_scsi_fabric_configfs->tf_ops, wwn,
  1883. &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
  1884. if (ret < 0) {
  1885. kfree(tpg);
  1886. return NULL;
  1887. }
  1888. mutex_lock(&vhost_scsi_mutex);
  1889. list_add_tail(&tpg->tv_tpg_list, &vhost_scsi_list);
  1890. mutex_unlock(&vhost_scsi_mutex);
  1891. return &tpg->se_tpg;
  1892. }
  1893. static void vhost_scsi_drop_tpg(struct se_portal_group *se_tpg)
  1894. {
  1895. struct vhost_scsi_tpg *tpg = container_of(se_tpg,
  1896. struct vhost_scsi_tpg, se_tpg);
  1897. mutex_lock(&vhost_scsi_mutex);
  1898. list_del(&tpg->tv_tpg_list);
  1899. mutex_unlock(&vhost_scsi_mutex);
  1900. /*
  1901. * Release the virtual I_T Nexus for this vhost TPG
  1902. */
  1903. vhost_scsi_drop_nexus(tpg);
  1904. /*
  1905. * Deregister the se_tpg from TCM..
  1906. */
  1907. core_tpg_deregister(se_tpg);
  1908. kfree(tpg);
  1909. }
  1910. static struct se_wwn *
  1911. vhost_scsi_make_tport(struct target_fabric_configfs *tf,
  1912. struct config_group *group,
  1913. const char *name)
  1914. {
  1915. struct vhost_scsi_tport *tport;
  1916. char *ptr;
  1917. u64 wwpn = 0;
  1918. int off = 0;
  1919. /* if (vhost_scsi_parse_wwn(name, &wwpn, 1) < 0)
  1920. return ERR_PTR(-EINVAL); */
  1921. tport = kzalloc(sizeof(struct vhost_scsi_tport), GFP_KERNEL);
  1922. if (!tport) {
  1923. pr_err("Unable to allocate struct vhost_scsi_tport");
  1924. return ERR_PTR(-ENOMEM);
  1925. }
  1926. tport->tport_wwpn = wwpn;
  1927. /*
  1928. * Determine the emulated Protocol Identifier and Target Port Name
  1929. * based on the incoming configfs directory name.
  1930. */
  1931. ptr = strstr(name, "naa.");
  1932. if (ptr) {
  1933. tport->tport_proto_id = SCSI_PROTOCOL_SAS;
  1934. goto check_len;
  1935. }
  1936. ptr = strstr(name, "fc.");
  1937. if (ptr) {
  1938. tport->tport_proto_id = SCSI_PROTOCOL_FCP;
  1939. off = 3; /* Skip over "fc." */
  1940. goto check_len;
  1941. }
  1942. ptr = strstr(name, "iqn.");
  1943. if (ptr) {
  1944. tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
  1945. goto check_len;
  1946. }
  1947. pr_err("Unable to locate prefix for emulated Target Port:"
  1948. " %s\n", name);
  1949. kfree(tport);
  1950. return ERR_PTR(-EINVAL);
  1951. check_len:
  1952. if (strlen(name) >= VHOST_SCSI_NAMELEN) {
  1953. pr_err("Emulated %s Address: %s, exceeds"
  1954. " max: %d\n", name, vhost_scsi_dump_proto_id(tport),
  1955. VHOST_SCSI_NAMELEN);
  1956. kfree(tport);
  1957. return ERR_PTR(-EINVAL);
  1958. }
  1959. snprintf(&tport->tport_name[0], VHOST_SCSI_NAMELEN, "%s", &name[off]);
  1960. pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
  1961. " %s Address: %s\n", vhost_scsi_dump_proto_id(tport), name);
  1962. return &tport->tport_wwn;
  1963. }
  1964. static void vhost_scsi_drop_tport(struct se_wwn *wwn)
  1965. {
  1966. struct vhost_scsi_tport *tport = container_of(wwn,
  1967. struct vhost_scsi_tport, tport_wwn);
  1968. pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
  1969. " %s Address: %s\n", vhost_scsi_dump_proto_id(tport),
  1970. tport->tport_name);
  1971. kfree(tport);
  1972. }
  1973. static ssize_t
  1974. vhost_scsi_wwn_show_attr_version(struct target_fabric_configfs *tf,
  1975. char *page)
  1976. {
  1977. return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
  1978. "on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
  1979. utsname()->machine);
  1980. }
  1981. TF_WWN_ATTR_RO(vhost_scsi, version);
  1982. static struct configfs_attribute *vhost_scsi_wwn_attrs[] = {
  1983. &vhost_scsi_wwn_version.attr,
  1984. NULL,
  1985. };
  1986. static struct target_core_fabric_ops vhost_scsi_ops = {
  1987. .get_fabric_name = vhost_scsi_get_fabric_name,
  1988. .get_fabric_proto_ident = vhost_scsi_get_fabric_proto_ident,
  1989. .tpg_get_wwn = vhost_scsi_get_fabric_wwn,
  1990. .tpg_get_tag = vhost_scsi_get_tpgt,
  1991. .tpg_get_default_depth = vhost_scsi_get_default_depth,
  1992. .tpg_get_pr_transport_id = vhost_scsi_get_pr_transport_id,
  1993. .tpg_get_pr_transport_id_len = vhost_scsi_get_pr_transport_id_len,
  1994. .tpg_parse_pr_out_transport_id = vhost_scsi_parse_pr_out_transport_id,
  1995. .tpg_check_demo_mode = vhost_scsi_check_true,
  1996. .tpg_check_demo_mode_cache = vhost_scsi_check_true,
  1997. .tpg_check_demo_mode_write_protect = vhost_scsi_check_false,
  1998. .tpg_check_prod_mode_write_protect = vhost_scsi_check_false,
  1999. .tpg_alloc_fabric_acl = vhost_scsi_alloc_fabric_acl,
  2000. .tpg_release_fabric_acl = vhost_scsi_release_fabric_acl,
  2001. .tpg_get_inst_index = vhost_scsi_tpg_get_inst_index,
  2002. .release_cmd = vhost_scsi_release_cmd,
  2003. .check_stop_free = vhost_scsi_check_stop_free,
  2004. .shutdown_session = vhost_scsi_shutdown_session,
  2005. .close_session = vhost_scsi_close_session,
  2006. .sess_get_index = vhost_scsi_sess_get_index,
  2007. .sess_get_initiator_sid = NULL,
  2008. .write_pending = vhost_scsi_write_pending,
  2009. .write_pending_status = vhost_scsi_write_pending_status,
  2010. .set_default_node_attributes = vhost_scsi_set_default_node_attrs,
  2011. .get_task_tag = vhost_scsi_get_task_tag,
  2012. .get_cmd_state = vhost_scsi_get_cmd_state,
  2013. .queue_data_in = vhost_scsi_queue_data_in,
  2014. .queue_status = vhost_scsi_queue_status,
  2015. .queue_tm_rsp = vhost_scsi_queue_tm_rsp,
  2016. .aborted_task = vhost_scsi_aborted_task,
  2017. /*
  2018. * Setup callers for generic logic in target_core_fabric_configfs.c
  2019. */
  2020. .fabric_make_wwn = vhost_scsi_make_tport,
  2021. .fabric_drop_wwn = vhost_scsi_drop_tport,
  2022. .fabric_make_tpg = vhost_scsi_make_tpg,
  2023. .fabric_drop_tpg = vhost_scsi_drop_tpg,
  2024. .fabric_post_link = vhost_scsi_port_link,
  2025. .fabric_pre_unlink = vhost_scsi_port_unlink,
  2026. .fabric_make_np = NULL,
  2027. .fabric_drop_np = NULL,
  2028. .fabric_make_nodeacl = vhost_scsi_make_nodeacl,
  2029. .fabric_drop_nodeacl = vhost_scsi_drop_nodeacl,
  2030. };
  2031. static int vhost_scsi_register_configfs(void)
  2032. {
  2033. struct target_fabric_configfs *fabric;
  2034. int ret;
  2035. pr_debug("vhost-scsi fabric module %s on %s/%s"
  2036. " on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
  2037. utsname()->machine);
  2038. /*
  2039. * Register the top level struct config_item_type with TCM core
  2040. */
  2041. fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
  2042. if (IS_ERR(fabric)) {
  2043. pr_err("target_fabric_configfs_init() failed\n");
  2044. return PTR_ERR(fabric);
  2045. }
  2046. /*
  2047. * Setup fabric->tf_ops from our local vhost_scsi_ops
  2048. */
  2049. fabric->tf_ops = vhost_scsi_ops;
  2050. /*
  2051. * Setup default attribute lists for various fabric->tf_cit_tmpl
  2052. */
  2053. fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = vhost_scsi_wwn_attrs;
  2054. fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = vhost_scsi_tpg_attrs;
  2055. fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;
  2056. fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
  2057. fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
  2058. fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  2059. fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  2060. fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  2061. fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  2062. /*
  2063. * Register the fabric for use within TCM
  2064. */
  2065. ret = target_fabric_configfs_register(fabric);
  2066. if (ret < 0) {
  2067. pr_err("target_fabric_configfs_register() failed"
  2068. " for TCM_VHOST\n");
  2069. return ret;
  2070. }
  2071. /*
  2072. * Setup our local pointer to *fabric
  2073. */
  2074. vhost_scsi_fabric_configfs = fabric;
  2075. pr_debug("TCM_VHOST[0] - Set fabric -> vhost_scsi_fabric_configfs\n");
  2076. return 0;
  2077. };
  2078. static void vhost_scsi_deregister_configfs(void)
  2079. {
  2080. if (!vhost_scsi_fabric_configfs)
  2081. return;
  2082. target_fabric_configfs_deregister(vhost_scsi_fabric_configfs);
  2083. vhost_scsi_fabric_configfs = NULL;
  2084. pr_debug("TCM_VHOST[0] - Cleared vhost_scsi_fabric_configfs\n");
  2085. };
  2086. static int __init vhost_scsi_init(void)
  2087. {
  2088. int ret = -ENOMEM;
  2089. /*
  2090. * Use our own dedicated workqueue for submitting I/O into
  2091. * target core to avoid contention within system_wq.
  2092. */
  2093. vhost_scsi_workqueue = alloc_workqueue("vhost_scsi", 0, 0);
  2094. if (!vhost_scsi_workqueue)
  2095. goto out;
  2096. ret = vhost_scsi_register();
  2097. if (ret < 0)
  2098. goto out_destroy_workqueue;
  2099. ret = vhost_scsi_register_configfs();
  2100. if (ret < 0)
  2101. goto out_vhost_scsi_deregister;
  2102. return 0;
  2103. out_vhost_scsi_deregister:
  2104. vhost_scsi_deregister();
  2105. out_destroy_workqueue:
  2106. destroy_workqueue(vhost_scsi_workqueue);
  2107. out:
  2108. return ret;
  2109. };
  2110. static void vhost_scsi_exit(void)
  2111. {
  2112. vhost_scsi_deregister_configfs();
  2113. vhost_scsi_deregister();
  2114. destroy_workqueue(vhost_scsi_workqueue);
  2115. };
  2116. MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
  2117. MODULE_ALIAS("tcm_vhost");
  2118. MODULE_LICENSE("GPL");
  2119. module_init(vhost_scsi_init);
  2120. module_exit(vhost_scsi_exit);