scsi.c 75 KB

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