scsi.c 65 KB

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