scsi.c 64 KB

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