scsi.c 57 KB

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