scsi.c 61 KB

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