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