tcm_vhost.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  1. /*******************************************************************************
  2. * Vhost kernel TCM fabric driver for virtio SCSI initiators
  3. *
  4. * (C) Copyright 2010-2012 RisingTide Systems LLC.
  5. * (C) Copyright 2010-2012 IBM Corp.
  6. *
  7. * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
  8. *
  9. * Authors: Nicholas A. Bellinger <nab@risingtidesystems.com>
  10. * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. ****************************************************************************/
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <generated/utsrelease.h>
  26. #include <linux/utsname.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/kthread.h>
  30. #include <linux/types.h>
  31. #include <linux/string.h>
  32. #include <linux/configfs.h>
  33. #include <linux/ctype.h>
  34. #include <linux/compat.h>
  35. #include <linux/eventfd.h>
  36. #include <linux/fs.h>
  37. #include <linux/miscdevice.h>
  38. #include <asm/unaligned.h>
  39. #include <scsi/scsi.h>
  40. #include <scsi/scsi_tcq.h>
  41. #include <target/target_core_base.h>
  42. #include <target/target_core_fabric.h>
  43. #include <target/target_core_fabric_configfs.h>
  44. #include <target/target_core_configfs.h>
  45. #include <target/configfs_macros.h>
  46. #include <linux/vhost.h>
  47. #include <linux/virtio_net.h> /* TODO vhost.h currently depends on this */
  48. #include <linux/virtio_scsi.h>
  49. #include <linux/llist.h>
  50. #include <linux/bitmap.h>
  51. #include "vhost.c"
  52. #include "vhost.h"
  53. #include "tcm_vhost.h"
  54. enum {
  55. VHOST_SCSI_VQ_CTL = 0,
  56. VHOST_SCSI_VQ_EVT = 1,
  57. VHOST_SCSI_VQ_IO = 2,
  58. };
  59. /*
  60. * VIRTIO_RING_F_EVENT_IDX seems broken. Not sure the bug is in
  61. * kernel but disabling it helps.
  62. * TODO: debug and remove the workaround.
  63. */
  64. enum {
  65. VHOST_SCSI_FEATURES = VHOST_FEATURES & (~VIRTIO_RING_F_EVENT_IDX)
  66. };
  67. #define VHOST_SCSI_MAX_TARGET 256
  68. #define VHOST_SCSI_MAX_VQ 128
  69. #define VHOST_SCSI_MAX_EVENT 128
  70. struct vhost_scsi {
  71. /* Protected by vhost_scsi->dev.mutex */
  72. struct tcm_vhost_tpg **vs_tpg;
  73. char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
  74. struct vhost_dev dev;
  75. struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
  76. struct vhost_work vs_completion_work; /* cmd completion work item */
  77. struct llist_head vs_completion_list; /* cmd completion queue */
  78. struct vhost_work vs_event_work; /* evt injection work item */
  79. struct llist_head vs_event_list; /* evt injection queue */
  80. bool vs_events_missed; /* any missed events, protected by vq->mutex */
  81. int vs_events_nr; /* num of pending events, protected by vq->mutex */
  82. };
  83. /* Local pointer to allocated TCM configfs fabric module */
  84. static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
  85. static struct workqueue_struct *tcm_vhost_workqueue;
  86. /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
  87. static DEFINE_MUTEX(tcm_vhost_mutex);
  88. static LIST_HEAD(tcm_vhost_list);
  89. static int iov_num_pages(struct iovec *iov)
  90. {
  91. return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
  92. ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
  93. }
  94. static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
  95. {
  96. return 1;
  97. }
  98. static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
  99. {
  100. return 0;
  101. }
  102. static char *tcm_vhost_get_fabric_name(void)
  103. {
  104. return "vhost";
  105. }
  106. static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  107. {
  108. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  109. struct tcm_vhost_tpg, se_tpg);
  110. struct tcm_vhost_tport *tport = tpg->tport;
  111. switch (tport->tport_proto_id) {
  112. case SCSI_PROTOCOL_SAS:
  113. return sas_get_fabric_proto_ident(se_tpg);
  114. case SCSI_PROTOCOL_FCP:
  115. return fc_get_fabric_proto_ident(se_tpg);
  116. case SCSI_PROTOCOL_ISCSI:
  117. return iscsi_get_fabric_proto_ident(se_tpg);
  118. default:
  119. pr_err("Unknown tport_proto_id: 0x%02x, using"
  120. " SAS emulation\n", tport->tport_proto_id);
  121. break;
  122. }
  123. return sas_get_fabric_proto_ident(se_tpg);
  124. }
  125. static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
  126. {
  127. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  128. struct tcm_vhost_tpg, se_tpg);
  129. struct tcm_vhost_tport *tport = tpg->tport;
  130. return &tport->tport_name[0];
  131. }
  132. static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
  133. {
  134. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  135. struct tcm_vhost_tpg, se_tpg);
  136. return tpg->tport_tpgt;
  137. }
  138. static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
  139. {
  140. return 1;
  141. }
  142. static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
  143. struct se_node_acl *se_nacl,
  144. struct t10_pr_registration *pr_reg,
  145. int *format_code,
  146. unsigned char *buf)
  147. {
  148. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  149. struct tcm_vhost_tpg, se_tpg);
  150. struct tcm_vhost_tport *tport = tpg->tport;
  151. switch (tport->tport_proto_id) {
  152. case SCSI_PROTOCOL_SAS:
  153. return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  154. format_code, buf);
  155. case SCSI_PROTOCOL_FCP:
  156. return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  157. format_code, buf);
  158. case SCSI_PROTOCOL_ISCSI:
  159. return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  160. format_code, buf);
  161. default:
  162. pr_err("Unknown tport_proto_id: 0x%02x, using"
  163. " SAS emulation\n", tport->tport_proto_id);
  164. break;
  165. }
  166. return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  167. format_code, buf);
  168. }
  169. static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
  170. struct se_node_acl *se_nacl,
  171. struct t10_pr_registration *pr_reg,
  172. int *format_code)
  173. {
  174. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  175. struct tcm_vhost_tpg, se_tpg);
  176. struct tcm_vhost_tport *tport = tpg->tport;
  177. switch (tport->tport_proto_id) {
  178. case SCSI_PROTOCOL_SAS:
  179. return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  180. format_code);
  181. case SCSI_PROTOCOL_FCP:
  182. return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  183. format_code);
  184. case SCSI_PROTOCOL_ISCSI:
  185. return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  186. format_code);
  187. default:
  188. pr_err("Unknown tport_proto_id: 0x%02x, using"
  189. " SAS emulation\n", tport->tport_proto_id);
  190. break;
  191. }
  192. return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  193. format_code);
  194. }
  195. static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
  196. const char *buf,
  197. u32 *out_tid_len,
  198. char **port_nexus_ptr)
  199. {
  200. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  201. struct tcm_vhost_tpg, se_tpg);
  202. struct tcm_vhost_tport *tport = tpg->tport;
  203. switch (tport->tport_proto_id) {
  204. case SCSI_PROTOCOL_SAS:
  205. return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  206. port_nexus_ptr);
  207. case SCSI_PROTOCOL_FCP:
  208. return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  209. port_nexus_ptr);
  210. case SCSI_PROTOCOL_ISCSI:
  211. return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  212. port_nexus_ptr);
  213. default:
  214. pr_err("Unknown tport_proto_id: 0x%02x, using"
  215. " SAS emulation\n", tport->tport_proto_id);
  216. break;
  217. }
  218. return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  219. port_nexus_ptr);
  220. }
  221. static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
  222. struct se_portal_group *se_tpg)
  223. {
  224. struct tcm_vhost_nacl *nacl;
  225. nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
  226. if (!nacl) {
  227. pr_err("Unable to allocate struct tcm_vhost_nacl\n");
  228. return NULL;
  229. }
  230. return &nacl->se_node_acl;
  231. }
  232. static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
  233. struct se_node_acl *se_nacl)
  234. {
  235. struct tcm_vhost_nacl *nacl = container_of(se_nacl,
  236. struct tcm_vhost_nacl, se_node_acl);
  237. kfree(nacl);
  238. }
  239. static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
  240. {
  241. return 1;
  242. }
  243. static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
  244. {
  245. return;
  246. }
  247. static int tcm_vhost_shutdown_session(struct se_session *se_sess)
  248. {
  249. return 0;
  250. }
  251. static void tcm_vhost_close_session(struct se_session *se_sess)
  252. {
  253. return;
  254. }
  255. static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
  256. {
  257. return 0;
  258. }
  259. static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
  260. {
  261. /* Go ahead and process the write immediately */
  262. target_execute_cmd(se_cmd);
  263. return 0;
  264. }
  265. static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
  266. {
  267. return 0;
  268. }
  269. static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
  270. {
  271. return;
  272. }
  273. static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
  274. {
  275. return 0;
  276. }
  277. static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
  278. {
  279. return 0;
  280. }
  281. static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
  282. {
  283. struct vhost_scsi *vs = tv_cmd->tvc_vhost;
  284. llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
  285. vhost_work_queue(&vs->dev, &vs->vs_completion_work);
  286. }
  287. static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
  288. {
  289. struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
  290. struct tcm_vhost_cmd, tvc_se_cmd);
  291. vhost_scsi_complete_cmd(tv_cmd);
  292. return 0;
  293. }
  294. static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
  295. {
  296. struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
  297. struct tcm_vhost_cmd, tvc_se_cmd);
  298. vhost_scsi_complete_cmd(tv_cmd);
  299. return 0;
  300. }
  301. static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
  302. {
  303. return 0;
  304. }
  305. static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
  306. {
  307. vs->vs_events_nr--;
  308. kfree(evt);
  309. }
  310. static struct tcm_vhost_evt *tcm_vhost_allocate_evt(struct vhost_scsi *vs,
  311. u32 event, u32 reason)
  312. {
  313. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
  314. struct tcm_vhost_evt *evt;
  315. if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
  316. vs->vs_events_missed = true;
  317. return NULL;
  318. }
  319. evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  320. if (!evt) {
  321. vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
  322. vs->vs_events_missed = true;
  323. return NULL;
  324. }
  325. evt->event.event = event;
  326. evt->event.reason = reason;
  327. vs->vs_events_nr++;
  328. return evt;
  329. }
  330. static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
  331. {
  332. struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
  333. /* TODO locking against target/backend threads? */
  334. transport_generic_free_cmd(se_cmd, 1);
  335. if (tv_cmd->tvc_sgl_count) {
  336. u32 i;
  337. for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
  338. put_page(sg_page(&tv_cmd->tvc_sgl[i]));
  339. kfree(tv_cmd->tvc_sgl);
  340. }
  341. kfree(tv_cmd);
  342. }
  343. static void tcm_vhost_do_evt_work(struct vhost_scsi *vs,
  344. struct tcm_vhost_evt *evt)
  345. {
  346. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
  347. struct virtio_scsi_event *event = &evt->event;
  348. struct virtio_scsi_event __user *eventp;
  349. unsigned out, in;
  350. int head, ret;
  351. if (!vq->private_data) {
  352. vs->vs_events_missed = true;
  353. return;
  354. }
  355. again:
  356. vhost_disable_notify(&vs->dev, vq);
  357. head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
  358. ARRAY_SIZE(vq->iov), &out, &in,
  359. NULL, NULL);
  360. if (head < 0) {
  361. vs->vs_events_missed = true;
  362. return;
  363. }
  364. if (head == vq->num) {
  365. if (vhost_enable_notify(&vs->dev, vq))
  366. goto again;
  367. vs->vs_events_missed = true;
  368. return;
  369. }
  370. if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
  371. vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
  372. vq->iov[out].iov_len);
  373. vs->vs_events_missed = true;
  374. return;
  375. }
  376. if (vs->vs_events_missed) {
  377. event->event |= VIRTIO_SCSI_T_EVENTS_MISSED;
  378. vs->vs_events_missed = false;
  379. }
  380. eventp = vq->iov[out].iov_base;
  381. ret = __copy_to_user(eventp, event, sizeof(*event));
  382. if (!ret)
  383. vhost_add_used_and_signal(&vs->dev, vq, head, 0);
  384. else
  385. vq_err(vq, "Faulted on tcm_vhost_send_event\n");
  386. }
  387. static void tcm_vhost_evt_work(struct vhost_work *work)
  388. {
  389. struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
  390. vs_event_work);
  391. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
  392. struct tcm_vhost_evt *evt;
  393. struct llist_node *llnode;
  394. mutex_lock(&vq->mutex);
  395. llnode = llist_del_all(&vs->vs_event_list);
  396. while (llnode) {
  397. evt = llist_entry(llnode, struct tcm_vhost_evt, list);
  398. llnode = llist_next(llnode);
  399. tcm_vhost_do_evt_work(vs, evt);
  400. tcm_vhost_free_evt(vs, evt);
  401. }
  402. mutex_unlock(&vq->mutex);
  403. }
  404. /* Fill in status and signal that we are done processing this command
  405. *
  406. * This is scheduled in the vhost work queue so we are called with the owner
  407. * process mm and can access the vring.
  408. */
  409. static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
  410. {
  411. struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
  412. vs_completion_work);
  413. DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
  414. struct virtio_scsi_cmd_resp v_rsp;
  415. struct tcm_vhost_cmd *tv_cmd;
  416. struct llist_node *llnode;
  417. struct se_cmd *se_cmd;
  418. int ret, vq;
  419. bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
  420. llnode = llist_del_all(&vs->vs_completion_list);
  421. while (llnode) {
  422. tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd,
  423. tvc_completion_list);
  424. llnode = llist_next(llnode);
  425. se_cmd = &tv_cmd->tvc_se_cmd;
  426. pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
  427. tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
  428. memset(&v_rsp, 0, sizeof(v_rsp));
  429. v_rsp.resid = se_cmd->residual_count;
  430. /* TODO is status_qualifier field needed? */
  431. v_rsp.status = se_cmd->scsi_status;
  432. v_rsp.sense_len = se_cmd->scsi_sense_length;
  433. memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
  434. v_rsp.sense_len);
  435. ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
  436. if (likely(ret == 0)) {
  437. vhost_add_used(tv_cmd->tvc_vq, tv_cmd->tvc_vq_desc, 0);
  438. vq = tv_cmd->tvc_vq - vs->vqs;
  439. __set_bit(vq, signal);
  440. } else
  441. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  442. vhost_scsi_free_cmd(tv_cmd);
  443. }
  444. vq = -1;
  445. while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
  446. < VHOST_SCSI_MAX_VQ)
  447. vhost_signal(&vs->dev, &vs->vqs[vq]);
  448. }
  449. static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
  450. struct tcm_vhost_tpg *tv_tpg,
  451. struct virtio_scsi_cmd_req *v_req,
  452. u32 exp_data_len,
  453. int data_direction)
  454. {
  455. struct tcm_vhost_cmd *tv_cmd;
  456. struct tcm_vhost_nexus *tv_nexus;
  457. tv_nexus = tv_tpg->tpg_nexus;
  458. if (!tv_nexus) {
  459. pr_err("Unable to locate active struct tcm_vhost_nexus\n");
  460. return ERR_PTR(-EIO);
  461. }
  462. tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
  463. if (!tv_cmd) {
  464. pr_err("Unable to allocate struct tcm_vhost_cmd\n");
  465. return ERR_PTR(-ENOMEM);
  466. }
  467. tv_cmd->tvc_tag = v_req->tag;
  468. tv_cmd->tvc_task_attr = v_req->task_attr;
  469. tv_cmd->tvc_exp_data_len = exp_data_len;
  470. tv_cmd->tvc_data_direction = data_direction;
  471. tv_cmd->tvc_nexus = tv_nexus;
  472. return tv_cmd;
  473. }
  474. /*
  475. * Map a user memory range into a scatterlist
  476. *
  477. * Returns the number of scatterlist entries used or -errno on error.
  478. */
  479. static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
  480. unsigned int sgl_count, struct iovec *iov, int write)
  481. {
  482. unsigned int npages = 0, pages_nr, offset, nbytes;
  483. struct scatterlist *sg = sgl;
  484. void __user *ptr = iov->iov_base;
  485. size_t len = iov->iov_len;
  486. struct page **pages;
  487. int ret, i;
  488. pages_nr = iov_num_pages(iov);
  489. if (pages_nr > sgl_count)
  490. return -ENOBUFS;
  491. pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
  492. if (!pages)
  493. return -ENOMEM;
  494. ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
  495. /* No pages were pinned */
  496. if (ret < 0)
  497. goto out;
  498. /* Less pages pinned than wanted */
  499. if (ret != pages_nr) {
  500. for (i = 0; i < ret; i++)
  501. put_page(pages[i]);
  502. ret = -EFAULT;
  503. goto out;
  504. }
  505. while (len > 0) {
  506. offset = (uintptr_t)ptr & ~PAGE_MASK;
  507. nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
  508. sg_set_page(sg, pages[npages], nbytes, offset);
  509. ptr += nbytes;
  510. len -= nbytes;
  511. sg++;
  512. npages++;
  513. }
  514. out:
  515. kfree(pages);
  516. return ret;
  517. }
  518. static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
  519. struct iovec *iov, unsigned int niov, int write)
  520. {
  521. int ret;
  522. unsigned int i;
  523. u32 sgl_count;
  524. struct scatterlist *sg;
  525. /*
  526. * Find out how long sglist needs to be
  527. */
  528. sgl_count = 0;
  529. for (i = 0; i < niov; i++)
  530. sgl_count += iov_num_pages(&iov[i]);
  531. /* TODO overflow checking */
  532. sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
  533. if (!sg)
  534. return -ENOMEM;
  535. pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
  536. sg, sgl_count, !sg);
  537. sg_init_table(sg, sgl_count);
  538. tv_cmd->tvc_sgl = sg;
  539. tv_cmd->tvc_sgl_count = sgl_count;
  540. pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
  541. for (i = 0; i < niov; i++) {
  542. ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
  543. if (ret < 0) {
  544. for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
  545. put_page(sg_page(&tv_cmd->tvc_sgl[i]));
  546. kfree(tv_cmd->tvc_sgl);
  547. tv_cmd->tvc_sgl = NULL;
  548. tv_cmd->tvc_sgl_count = 0;
  549. return ret;
  550. }
  551. sg += ret;
  552. sgl_count -= ret;
  553. }
  554. return 0;
  555. }
  556. static void tcm_vhost_submission_work(struct work_struct *work)
  557. {
  558. struct tcm_vhost_cmd *tv_cmd =
  559. container_of(work, struct tcm_vhost_cmd, work);
  560. struct tcm_vhost_nexus *tv_nexus;
  561. struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
  562. struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
  563. int rc, sg_no_bidi = 0;
  564. if (tv_cmd->tvc_sgl_count) {
  565. sg_ptr = tv_cmd->tvc_sgl;
  566. /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
  567. #if 0
  568. if (se_cmd->se_cmd_flags & SCF_BIDI) {
  569. sg_bidi_ptr = NULL;
  570. sg_no_bidi = 0;
  571. }
  572. #endif
  573. } else {
  574. sg_ptr = NULL;
  575. }
  576. tv_nexus = tv_cmd->tvc_nexus;
  577. rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
  578. tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
  579. tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
  580. tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
  581. 0, sg_ptr, tv_cmd->tvc_sgl_count,
  582. sg_bidi_ptr, sg_no_bidi);
  583. if (rc < 0) {
  584. transport_send_check_condition_and_sense(se_cmd,
  585. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  586. transport_generic_free_cmd(se_cmd, 0);
  587. }
  588. }
  589. static void vhost_scsi_send_bad_target(struct vhost_scsi *vs,
  590. struct vhost_virtqueue *vq, int head, unsigned out)
  591. {
  592. struct virtio_scsi_cmd_resp __user *resp;
  593. struct virtio_scsi_cmd_resp rsp;
  594. int ret;
  595. memset(&rsp, 0, sizeof(rsp));
  596. rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
  597. resp = vq->iov[out].iov_base;
  598. ret = __copy_to_user(resp, &rsp, sizeof(rsp));
  599. if (!ret)
  600. vhost_add_used_and_signal(&vs->dev, vq, head, 0);
  601. else
  602. pr_err("Faulted on virtio_scsi_cmd_resp\n");
  603. }
  604. static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
  605. struct vhost_virtqueue *vq)
  606. {
  607. struct tcm_vhost_tpg **vs_tpg;
  608. struct virtio_scsi_cmd_req v_req;
  609. struct tcm_vhost_tpg *tv_tpg;
  610. struct tcm_vhost_cmd *tv_cmd;
  611. u32 exp_data_len, data_first, data_num, data_direction;
  612. unsigned out, in, i;
  613. int head, ret;
  614. u8 target;
  615. /*
  616. * We can handle the vq only after the endpoint is setup by calling the
  617. * VHOST_SCSI_SET_ENDPOINT ioctl.
  618. *
  619. * TODO: Check that we are running from vhost_worker which acts
  620. * as read-side critical section for vhost kind of RCU.
  621. * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
  622. */
  623. vs_tpg = rcu_dereference_check(vq->private_data, 1);
  624. if (!vs_tpg)
  625. return;
  626. mutex_lock(&vq->mutex);
  627. vhost_disable_notify(&vs->dev, vq);
  628. for (;;) {
  629. head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
  630. ARRAY_SIZE(vq->iov), &out, &in,
  631. NULL, NULL);
  632. pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
  633. head, out, in);
  634. /* On error, stop handling until the next kick. */
  635. if (unlikely(head < 0))
  636. break;
  637. /* Nothing new? Wait for eventfd to tell us they refilled. */
  638. if (head == vq->num) {
  639. if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
  640. vhost_disable_notify(&vs->dev, vq);
  641. continue;
  642. }
  643. break;
  644. }
  645. /* FIXME: BIDI operation */
  646. if (out == 1 && in == 1) {
  647. data_direction = DMA_NONE;
  648. data_first = 0;
  649. data_num = 0;
  650. } else if (out == 1 && in > 1) {
  651. data_direction = DMA_FROM_DEVICE;
  652. data_first = out + 1;
  653. data_num = in - 1;
  654. } else if (out > 1 && in == 1) {
  655. data_direction = DMA_TO_DEVICE;
  656. data_first = 1;
  657. data_num = out - 1;
  658. } else {
  659. vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
  660. out, in);
  661. break;
  662. }
  663. /*
  664. * Check for a sane resp buffer so we can report errors to
  665. * the guest.
  666. */
  667. if (unlikely(vq->iov[out].iov_len !=
  668. sizeof(struct virtio_scsi_cmd_resp))) {
  669. vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
  670. " bytes\n", vq->iov[out].iov_len);
  671. break;
  672. }
  673. if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
  674. vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
  675. " bytes\n", vq->iov[0].iov_len);
  676. break;
  677. }
  678. pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
  679. " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
  680. ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
  681. sizeof(v_req));
  682. if (unlikely(ret)) {
  683. vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
  684. break;
  685. }
  686. /* Extract the tpgt */
  687. target = v_req.lun[1];
  688. tv_tpg = ACCESS_ONCE(vs_tpg[target]);
  689. /* Target does not exist, fail the request */
  690. if (unlikely(!tv_tpg)) {
  691. vhost_scsi_send_bad_target(vs, vq, head, out);
  692. continue;
  693. }
  694. exp_data_len = 0;
  695. for (i = 0; i < data_num; i++)
  696. exp_data_len += vq->iov[data_first + i].iov_len;
  697. tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
  698. exp_data_len, data_direction);
  699. if (IS_ERR(tv_cmd)) {
  700. vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
  701. PTR_ERR(tv_cmd));
  702. goto err_cmd;
  703. }
  704. pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
  705. ": %d\n", tv_cmd, exp_data_len, data_direction);
  706. tv_cmd->tvc_vhost = vs;
  707. tv_cmd->tvc_vq = vq;
  708. tv_cmd->tvc_resp = vq->iov[out].iov_base;
  709. /*
  710. * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
  711. * that will be used by tcm_vhost_new_cmd_map() and down into
  712. * target_setup_cmd_from_cdb()
  713. */
  714. memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
  715. /*
  716. * Check that the recieved CDB size does not exceeded our
  717. * hardcoded max for tcm_vhost
  718. */
  719. /* TODO what if cdb was too small for varlen cdb header? */
  720. if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
  721. TCM_VHOST_MAX_CDB_SIZE)) {
  722. vq_err(vq, "Received SCSI CDB with command_size: %d that"
  723. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  724. scsi_command_size(tv_cmd->tvc_cdb),
  725. TCM_VHOST_MAX_CDB_SIZE);
  726. goto err_free;
  727. }
  728. tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
  729. pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
  730. tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
  731. if (data_direction != DMA_NONE) {
  732. ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
  733. &vq->iov[data_first], data_num,
  734. data_direction == DMA_TO_DEVICE);
  735. if (unlikely(ret)) {
  736. vq_err(vq, "Failed to map iov to sgl\n");
  737. goto err_free;
  738. }
  739. }
  740. /*
  741. * Save the descriptor from vhost_get_vq_desc() to be used to
  742. * complete the virtio-scsi request in TCM callback context via
  743. * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
  744. */
  745. tv_cmd->tvc_vq_desc = head;
  746. /*
  747. * Dispatch tv_cmd descriptor for cmwq execution in process
  748. * context provided by tcm_vhost_workqueue. This also ensures
  749. * tv_cmd is executed on the same kworker CPU as this vhost
  750. * thread to gain positive L2 cache locality effects..
  751. */
  752. INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
  753. queue_work(tcm_vhost_workqueue, &tv_cmd->work);
  754. }
  755. mutex_unlock(&vq->mutex);
  756. return;
  757. err_free:
  758. vhost_scsi_free_cmd(tv_cmd);
  759. err_cmd:
  760. vhost_scsi_send_bad_target(vs, vq, head, out);
  761. mutex_unlock(&vq->mutex);
  762. }
  763. static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
  764. {
  765. pr_debug("%s: The handling func for control queue.\n", __func__);
  766. }
  767. static void tcm_vhost_send_evt(struct vhost_scsi *vs, struct tcm_vhost_tpg *tpg,
  768. struct se_lun *lun, u32 event, u32 reason)
  769. {
  770. struct tcm_vhost_evt *evt;
  771. evt = tcm_vhost_allocate_evt(vs, event, reason);
  772. if (!evt)
  773. return;
  774. if (tpg && lun) {
  775. /* TODO: share lun setup code with virtio-scsi.ko */
  776. /*
  777. * Note: evt->event is zeroed when we allocate it and
  778. * lun[4-7] need to be zero according to virtio-scsi spec.
  779. */
  780. evt->event.lun[0] = 0x01;
  781. evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
  782. if (lun->unpacked_lun >= 256)
  783. evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
  784. evt->event.lun[3] = lun->unpacked_lun & 0xFF;
  785. }
  786. llist_add(&evt->list, &vs->vs_event_list);
  787. vhost_work_queue(&vs->dev, &vs->vs_event_work);
  788. }
  789. static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
  790. {
  791. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  792. poll.work);
  793. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  794. mutex_lock(&vq->mutex);
  795. if (!vq->private_data)
  796. goto out;
  797. if (vs->vs_events_missed)
  798. tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
  799. out:
  800. mutex_unlock(&vq->mutex);
  801. }
  802. static void vhost_scsi_handle_kick(struct vhost_work *work)
  803. {
  804. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  805. poll.work);
  806. struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
  807. vhost_scsi_handle_vq(vs, vq);
  808. }
  809. static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
  810. {
  811. vhost_poll_flush(&vs->dev.vqs[index].poll);
  812. }
  813. static void vhost_scsi_flush(struct vhost_scsi *vs)
  814. {
  815. int i;
  816. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
  817. vhost_scsi_flush_vq(vs, i);
  818. vhost_work_flush(&vs->dev, &vs->vs_completion_work);
  819. vhost_work_flush(&vs->dev, &vs->vs_event_work);
  820. }
  821. /*
  822. * Called from vhost_scsi_ioctl() context to walk the list of available
  823. * tcm_vhost_tpg with an active struct tcm_vhost_nexus
  824. *
  825. * The lock nesting rule is:
  826. * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
  827. */
  828. static int vhost_scsi_set_endpoint(
  829. struct vhost_scsi *vs,
  830. struct vhost_scsi_target *t)
  831. {
  832. struct tcm_vhost_tport *tv_tport;
  833. struct tcm_vhost_tpg *tv_tpg;
  834. struct tcm_vhost_tpg **vs_tpg;
  835. struct vhost_virtqueue *vq;
  836. int index, ret, i, len;
  837. bool match = false;
  838. mutex_lock(&tcm_vhost_mutex);
  839. mutex_lock(&vs->dev.mutex);
  840. /* Verify that ring has been setup correctly. */
  841. for (index = 0; index < vs->dev.nvqs; ++index) {
  842. /* Verify that ring has been setup correctly. */
  843. if (!vhost_vq_access_ok(&vs->vqs[index])) {
  844. ret = -EFAULT;
  845. goto out;
  846. }
  847. }
  848. len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
  849. vs_tpg = kzalloc(len, GFP_KERNEL);
  850. if (!vs_tpg) {
  851. ret = -ENOMEM;
  852. goto out;
  853. }
  854. if (vs->vs_tpg)
  855. memcpy(vs_tpg, vs->vs_tpg, len);
  856. list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
  857. mutex_lock(&tv_tpg->tv_tpg_mutex);
  858. if (!tv_tpg->tpg_nexus) {
  859. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  860. continue;
  861. }
  862. if (tv_tpg->tv_tpg_vhost_count != 0) {
  863. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  864. continue;
  865. }
  866. tv_tport = tv_tpg->tport;
  867. if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  868. if (vs->vs_tpg && vs->vs_tpg[tv_tpg->tport_tpgt]) {
  869. kfree(vs_tpg);
  870. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  871. ret = -EEXIST;
  872. goto out;
  873. }
  874. tv_tpg->tv_tpg_vhost_count++;
  875. tv_tpg->vhost_scsi = vs;
  876. vs_tpg[tv_tpg->tport_tpgt] = tv_tpg;
  877. smp_mb__after_atomic_inc();
  878. match = true;
  879. }
  880. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  881. }
  882. if (match) {
  883. memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
  884. sizeof(vs->vs_vhost_wwpn));
  885. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  886. vq = &vs->vqs[i];
  887. /* Flushing the vhost_work acts as synchronize_rcu */
  888. mutex_lock(&vq->mutex);
  889. rcu_assign_pointer(vq->private_data, vs_tpg);
  890. vhost_init_used(vq);
  891. mutex_unlock(&vq->mutex);
  892. }
  893. ret = 0;
  894. } else {
  895. ret = -EEXIST;
  896. }
  897. /*
  898. * Act as synchronize_rcu to make sure access to
  899. * old vs->vs_tpg is finished.
  900. */
  901. vhost_scsi_flush(vs);
  902. kfree(vs->vs_tpg);
  903. vs->vs_tpg = vs_tpg;
  904. out:
  905. mutex_unlock(&vs->dev.mutex);
  906. mutex_unlock(&tcm_vhost_mutex);
  907. return ret;
  908. }
  909. static int vhost_scsi_clear_endpoint(
  910. struct vhost_scsi *vs,
  911. struct vhost_scsi_target *t)
  912. {
  913. struct tcm_vhost_tport *tv_tport;
  914. struct tcm_vhost_tpg *tv_tpg;
  915. struct vhost_virtqueue *vq;
  916. bool match = false;
  917. int index, ret, i;
  918. u8 target;
  919. mutex_lock(&tcm_vhost_mutex);
  920. mutex_lock(&vs->dev.mutex);
  921. /* Verify that ring has been setup correctly. */
  922. for (index = 0; index < vs->dev.nvqs; ++index) {
  923. if (!vhost_vq_access_ok(&vs->vqs[index])) {
  924. ret = -EFAULT;
  925. goto err_dev;
  926. }
  927. }
  928. if (!vs->vs_tpg) {
  929. ret = 0;
  930. goto err_dev;
  931. }
  932. for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
  933. target = i;
  934. tv_tpg = vs->vs_tpg[target];
  935. if (!tv_tpg)
  936. continue;
  937. mutex_lock(&tv_tpg->tv_tpg_mutex);
  938. tv_tport = tv_tpg->tport;
  939. if (!tv_tport) {
  940. ret = -ENODEV;
  941. goto err_tpg;
  942. }
  943. if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
  944. pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
  945. " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
  946. tv_tport->tport_name, tv_tpg->tport_tpgt,
  947. t->vhost_wwpn, t->vhost_tpgt);
  948. ret = -EINVAL;
  949. goto err_tpg;
  950. }
  951. tv_tpg->tv_tpg_vhost_count--;
  952. tv_tpg->vhost_scsi = NULL;
  953. vs->vs_tpg[target] = NULL;
  954. match = true;
  955. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  956. }
  957. if (match) {
  958. for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
  959. vq = &vs->vqs[i];
  960. /* Flushing the vhost_work acts as synchronize_rcu */
  961. mutex_lock(&vq->mutex);
  962. rcu_assign_pointer(vq->private_data, NULL);
  963. mutex_unlock(&vq->mutex);
  964. }
  965. }
  966. /*
  967. * Act as synchronize_rcu to make sure access to
  968. * old vs->vs_tpg is finished.
  969. */
  970. vhost_scsi_flush(vs);
  971. kfree(vs->vs_tpg);
  972. vs->vs_tpg = NULL;
  973. WARN_ON(vs->vs_events_nr);
  974. mutex_unlock(&vs->dev.mutex);
  975. mutex_unlock(&tcm_vhost_mutex);
  976. return 0;
  977. err_tpg:
  978. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  979. err_dev:
  980. mutex_unlock(&vs->dev.mutex);
  981. mutex_unlock(&tcm_vhost_mutex);
  982. return ret;
  983. }
  984. static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
  985. {
  986. if (features & ~VHOST_SCSI_FEATURES)
  987. return -EOPNOTSUPP;
  988. mutex_lock(&vs->dev.mutex);
  989. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  990. !vhost_log_access_ok(&vs->dev)) {
  991. mutex_unlock(&vs->dev.mutex);
  992. return -EFAULT;
  993. }
  994. vs->dev.acked_features = features;
  995. smp_wmb();
  996. vhost_scsi_flush(vs);
  997. mutex_unlock(&vs->dev.mutex);
  998. return 0;
  999. }
  1000. static int vhost_scsi_open(struct inode *inode, struct file *f)
  1001. {
  1002. struct vhost_scsi *s;
  1003. int r, i;
  1004. s = kzalloc(sizeof(*s), GFP_KERNEL);
  1005. if (!s)
  1006. return -ENOMEM;
  1007. vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
  1008. vhost_work_init(&s->vs_event_work, tcm_vhost_evt_work);
  1009. s->vs_events_nr = 0;
  1010. s->vs_events_missed = false;
  1011. s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick;
  1012. s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick;
  1013. for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++)
  1014. s->vqs[i].handle_kick = vhost_scsi_handle_kick;
  1015. r = vhost_dev_init(&s->dev, s->vqs, VHOST_SCSI_MAX_VQ);
  1016. if (r < 0) {
  1017. kfree(s);
  1018. return r;
  1019. }
  1020. f->private_data = s;
  1021. return 0;
  1022. }
  1023. static int vhost_scsi_release(struct inode *inode, struct file *f)
  1024. {
  1025. struct vhost_scsi *s = f->private_data;
  1026. struct vhost_scsi_target t;
  1027. mutex_lock(&s->dev.mutex);
  1028. memcpy(t.vhost_wwpn, s->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
  1029. mutex_unlock(&s->dev.mutex);
  1030. vhost_scsi_clear_endpoint(s, &t);
  1031. vhost_dev_stop(&s->dev);
  1032. vhost_dev_cleanup(&s->dev, false);
  1033. /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
  1034. vhost_scsi_flush(s);
  1035. kfree(s);
  1036. return 0;
  1037. }
  1038. static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
  1039. unsigned long arg)
  1040. {
  1041. struct vhost_scsi *vs = f->private_data;
  1042. struct vhost_scsi_target backend;
  1043. void __user *argp = (void __user *)arg;
  1044. u64 __user *featurep = argp;
  1045. u32 __user *eventsp = argp;
  1046. u32 events_missed;
  1047. u64 features;
  1048. int r, abi_version = VHOST_SCSI_ABI_VERSION;
  1049. struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
  1050. switch (ioctl) {
  1051. case VHOST_SCSI_SET_ENDPOINT:
  1052. if (copy_from_user(&backend, argp, sizeof backend))
  1053. return -EFAULT;
  1054. if (backend.reserved != 0)
  1055. return -EOPNOTSUPP;
  1056. return vhost_scsi_set_endpoint(vs, &backend);
  1057. case VHOST_SCSI_CLEAR_ENDPOINT:
  1058. if (copy_from_user(&backend, argp, sizeof backend))
  1059. return -EFAULT;
  1060. if (backend.reserved != 0)
  1061. return -EOPNOTSUPP;
  1062. return vhost_scsi_clear_endpoint(vs, &backend);
  1063. case VHOST_SCSI_GET_ABI_VERSION:
  1064. if (copy_to_user(argp, &abi_version, sizeof abi_version))
  1065. return -EFAULT;
  1066. return 0;
  1067. case VHOST_SCSI_SET_EVENTS_MISSED:
  1068. if (get_user(events_missed, eventsp))
  1069. return -EFAULT;
  1070. mutex_lock(&vq->mutex);
  1071. vs->vs_events_missed = events_missed;
  1072. mutex_unlock(&vq->mutex);
  1073. return 0;
  1074. case VHOST_SCSI_GET_EVENTS_MISSED:
  1075. mutex_lock(&vq->mutex);
  1076. events_missed = vs->vs_events_missed;
  1077. mutex_unlock(&vq->mutex);
  1078. if (put_user(events_missed, eventsp))
  1079. return -EFAULT;
  1080. return 0;
  1081. case VHOST_GET_FEATURES:
  1082. features = VHOST_SCSI_FEATURES;
  1083. if (copy_to_user(featurep, &features, sizeof features))
  1084. return -EFAULT;
  1085. return 0;
  1086. case VHOST_SET_FEATURES:
  1087. if (copy_from_user(&features, featurep, sizeof features))
  1088. return -EFAULT;
  1089. return vhost_scsi_set_features(vs, features);
  1090. default:
  1091. mutex_lock(&vs->dev.mutex);
  1092. r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
  1093. /* TODO: flush backend after dev ioctl. */
  1094. if (r == -ENOIOCTLCMD)
  1095. r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
  1096. mutex_unlock(&vs->dev.mutex);
  1097. return r;
  1098. }
  1099. }
  1100. #ifdef CONFIG_COMPAT
  1101. static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
  1102. unsigned long arg)
  1103. {
  1104. return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  1105. }
  1106. #endif
  1107. static const struct file_operations vhost_scsi_fops = {
  1108. .owner = THIS_MODULE,
  1109. .release = vhost_scsi_release,
  1110. .unlocked_ioctl = vhost_scsi_ioctl,
  1111. #ifdef CONFIG_COMPAT
  1112. .compat_ioctl = vhost_scsi_compat_ioctl,
  1113. #endif
  1114. .open = vhost_scsi_open,
  1115. .llseek = noop_llseek,
  1116. };
  1117. static struct miscdevice vhost_scsi_misc = {
  1118. MISC_DYNAMIC_MINOR,
  1119. "vhost-scsi",
  1120. &vhost_scsi_fops,
  1121. };
  1122. static int __init vhost_scsi_register(void)
  1123. {
  1124. return misc_register(&vhost_scsi_misc);
  1125. }
  1126. static int vhost_scsi_deregister(void)
  1127. {
  1128. return misc_deregister(&vhost_scsi_misc);
  1129. }
  1130. static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
  1131. {
  1132. switch (tport->tport_proto_id) {
  1133. case SCSI_PROTOCOL_SAS:
  1134. return "SAS";
  1135. case SCSI_PROTOCOL_FCP:
  1136. return "FCP";
  1137. case SCSI_PROTOCOL_ISCSI:
  1138. return "iSCSI";
  1139. default:
  1140. break;
  1141. }
  1142. return "Unknown";
  1143. }
  1144. static void tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
  1145. struct se_lun *lun, bool plug)
  1146. {
  1147. struct vhost_scsi *vs = tpg->vhost_scsi;
  1148. struct vhost_virtqueue *vq;
  1149. u32 reason;
  1150. if (!vs)
  1151. return;
  1152. mutex_lock(&vs->dev.mutex);
  1153. if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) {
  1154. mutex_unlock(&vs->dev.mutex);
  1155. return;
  1156. }
  1157. if (plug)
  1158. reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
  1159. else
  1160. reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
  1161. vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
  1162. mutex_lock(&vq->mutex);
  1163. tcm_vhost_send_evt(vs, tpg, lun,
  1164. VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
  1165. mutex_unlock(&vq->mutex);
  1166. mutex_unlock(&vs->dev.mutex);
  1167. }
  1168. static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
  1169. {
  1170. tcm_vhost_do_plug(tpg, lun, true);
  1171. }
  1172. static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
  1173. {
  1174. tcm_vhost_do_plug(tpg, lun, false);
  1175. }
  1176. static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
  1177. struct se_lun *lun)
  1178. {
  1179. struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
  1180. struct tcm_vhost_tpg, se_tpg);
  1181. mutex_lock(&tcm_vhost_mutex);
  1182. mutex_lock(&tv_tpg->tv_tpg_mutex);
  1183. tv_tpg->tv_tpg_port_count++;
  1184. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  1185. tcm_vhost_hotplug(tv_tpg, lun);
  1186. mutex_unlock(&tcm_vhost_mutex);
  1187. return 0;
  1188. }
  1189. static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
  1190. struct se_lun *lun)
  1191. {
  1192. struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
  1193. struct tcm_vhost_tpg, se_tpg);
  1194. mutex_lock(&tcm_vhost_mutex);
  1195. mutex_lock(&tv_tpg->tv_tpg_mutex);
  1196. tv_tpg->tv_tpg_port_count--;
  1197. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  1198. tcm_vhost_hotunplug(tv_tpg, lun);
  1199. mutex_unlock(&tcm_vhost_mutex);
  1200. }
  1201. static struct se_node_acl *tcm_vhost_make_nodeacl(
  1202. struct se_portal_group *se_tpg,
  1203. struct config_group *group,
  1204. const char *name)
  1205. {
  1206. struct se_node_acl *se_nacl, *se_nacl_new;
  1207. struct tcm_vhost_nacl *nacl;
  1208. u64 wwpn = 0;
  1209. u32 nexus_depth;
  1210. /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
  1211. return ERR_PTR(-EINVAL); */
  1212. se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
  1213. if (!se_nacl_new)
  1214. return ERR_PTR(-ENOMEM);
  1215. nexus_depth = 1;
  1216. /*
  1217. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  1218. * when converting a NodeACL from demo mode -> explict
  1219. */
  1220. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  1221. name, nexus_depth);
  1222. if (IS_ERR(se_nacl)) {
  1223. tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
  1224. return se_nacl;
  1225. }
  1226. /*
  1227. * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
  1228. */
  1229. nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
  1230. nacl->iport_wwpn = wwpn;
  1231. return se_nacl;
  1232. }
  1233. static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
  1234. {
  1235. struct tcm_vhost_nacl *nacl = container_of(se_acl,
  1236. struct tcm_vhost_nacl, se_node_acl);
  1237. core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
  1238. kfree(nacl);
  1239. }
  1240. static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
  1241. const char *name)
  1242. {
  1243. struct se_portal_group *se_tpg;
  1244. struct tcm_vhost_nexus *tv_nexus;
  1245. mutex_lock(&tv_tpg->tv_tpg_mutex);
  1246. if (tv_tpg->tpg_nexus) {
  1247. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  1248. pr_debug("tv_tpg->tpg_nexus already exists\n");
  1249. return -EEXIST;
  1250. }
  1251. se_tpg = &tv_tpg->se_tpg;
  1252. tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
  1253. if (!tv_nexus) {
  1254. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  1255. pr_err("Unable to allocate struct tcm_vhost_nexus\n");
  1256. return -ENOMEM;
  1257. }
  1258. /*
  1259. * Initialize the struct se_session pointer
  1260. */
  1261. tv_nexus->tvn_se_sess = transport_init_session();
  1262. if (IS_ERR(tv_nexus->tvn_se_sess)) {
  1263. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  1264. kfree(tv_nexus);
  1265. return -ENOMEM;
  1266. }
  1267. /*
  1268. * Since we are running in 'demo mode' this call with generate a
  1269. * struct se_node_acl for the tcm_vhost struct se_portal_group with
  1270. * the SCSI Initiator port name of the passed configfs group 'name'.
  1271. */
  1272. tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  1273. se_tpg, (unsigned char *)name);
  1274. if (!tv_nexus->tvn_se_sess->se_node_acl) {
  1275. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  1276. pr_debug("core_tpg_check_initiator_node_acl() failed"
  1277. " for %s\n", name);
  1278. transport_free_session(tv_nexus->tvn_se_sess);
  1279. kfree(tv_nexus);
  1280. return -ENOMEM;
  1281. }
  1282. /*
  1283. * Now register the TCM vhost virtual I_T Nexus as active with the
  1284. * call to __transport_register_session()
  1285. */
  1286. __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
  1287. tv_nexus->tvn_se_sess, tv_nexus);
  1288. tv_tpg->tpg_nexus = tv_nexus;
  1289. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  1290. return 0;
  1291. }
  1292. static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
  1293. {
  1294. struct se_session *se_sess;
  1295. struct tcm_vhost_nexus *tv_nexus;
  1296. mutex_lock(&tpg->tv_tpg_mutex);
  1297. tv_nexus = tpg->tpg_nexus;
  1298. if (!tv_nexus) {
  1299. mutex_unlock(&tpg->tv_tpg_mutex);
  1300. return -ENODEV;
  1301. }
  1302. se_sess = tv_nexus->tvn_se_sess;
  1303. if (!se_sess) {
  1304. mutex_unlock(&tpg->tv_tpg_mutex);
  1305. return -ENODEV;
  1306. }
  1307. if (tpg->tv_tpg_port_count != 0) {
  1308. mutex_unlock(&tpg->tv_tpg_mutex);
  1309. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1310. " active TPG port count: %d\n",
  1311. tpg->tv_tpg_port_count);
  1312. return -EBUSY;
  1313. }
  1314. if (tpg->tv_tpg_vhost_count != 0) {
  1315. mutex_unlock(&tpg->tv_tpg_mutex);
  1316. pr_err("Unable to remove TCM_vhost I_T Nexus with"
  1317. " active TPG vhost count: %d\n",
  1318. tpg->tv_tpg_vhost_count);
  1319. return -EBUSY;
  1320. }
  1321. pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
  1322. " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
  1323. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1324. /*
  1325. * Release the SCSI I_T Nexus to the emulated vhost Target Port
  1326. */
  1327. transport_deregister_session(tv_nexus->tvn_se_sess);
  1328. tpg->tpg_nexus = NULL;
  1329. mutex_unlock(&tpg->tv_tpg_mutex);
  1330. kfree(tv_nexus);
  1331. return 0;
  1332. }
  1333. static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
  1334. char *page)
  1335. {
  1336. struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
  1337. struct tcm_vhost_tpg, se_tpg);
  1338. struct tcm_vhost_nexus *tv_nexus;
  1339. ssize_t ret;
  1340. mutex_lock(&tv_tpg->tv_tpg_mutex);
  1341. tv_nexus = tv_tpg->tpg_nexus;
  1342. if (!tv_nexus) {
  1343. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  1344. return -ENODEV;
  1345. }
  1346. ret = snprintf(page, PAGE_SIZE, "%s\n",
  1347. tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
  1348. mutex_unlock(&tv_tpg->tv_tpg_mutex);
  1349. return ret;
  1350. }
  1351. static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
  1352. const char *page,
  1353. size_t count)
  1354. {
  1355. struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
  1356. struct tcm_vhost_tpg, se_tpg);
  1357. struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
  1358. unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
  1359. int ret;
  1360. /*
  1361. * Shutdown the active I_T nexus if 'NULL' is passed..
  1362. */
  1363. if (!strncmp(page, "NULL", 4)) {
  1364. ret = tcm_vhost_drop_nexus(tv_tpg);
  1365. return (!ret) ? count : ret;
  1366. }
  1367. /*
  1368. * Otherwise make sure the passed virtual Initiator port WWN matches
  1369. * the fabric protocol_id set in tcm_vhost_make_tport(), and call
  1370. * tcm_vhost_make_nexus().
  1371. */
  1372. if (strlen(page) >= TCM_VHOST_NAMELEN) {
  1373. pr_err("Emulated NAA Sas Address: %s, exceeds"
  1374. " max: %d\n", page, TCM_VHOST_NAMELEN);
  1375. return -EINVAL;
  1376. }
  1377. snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
  1378. ptr = strstr(i_port, "naa.");
  1379. if (ptr) {
  1380. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
  1381. pr_err("Passed SAS Initiator Port %s does not"
  1382. " match target port protoid: %s\n", i_port,
  1383. tcm_vhost_dump_proto_id(tport_wwn));
  1384. return -EINVAL;
  1385. }
  1386. port_ptr = &i_port[0];
  1387. goto check_newline;
  1388. }
  1389. ptr = strstr(i_port, "fc.");
  1390. if (ptr) {
  1391. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
  1392. pr_err("Passed FCP Initiator Port %s does not"
  1393. " match target port protoid: %s\n", i_port,
  1394. tcm_vhost_dump_proto_id(tport_wwn));
  1395. return -EINVAL;
  1396. }
  1397. port_ptr = &i_port[3]; /* Skip over "fc." */
  1398. goto check_newline;
  1399. }
  1400. ptr = strstr(i_port, "iqn.");
  1401. if (ptr) {
  1402. if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
  1403. pr_err("Passed iSCSI Initiator Port %s does not"
  1404. " match target port protoid: %s\n", i_port,
  1405. tcm_vhost_dump_proto_id(tport_wwn));
  1406. return -EINVAL;
  1407. }
  1408. port_ptr = &i_port[0];
  1409. goto check_newline;
  1410. }
  1411. pr_err("Unable to locate prefix for emulated Initiator Port:"
  1412. " %s\n", i_port);
  1413. return -EINVAL;
  1414. /*
  1415. * Clear any trailing newline for the NAA WWN
  1416. */
  1417. check_newline:
  1418. if (i_port[strlen(i_port)-1] == '\n')
  1419. i_port[strlen(i_port)-1] = '\0';
  1420. ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
  1421. if (ret < 0)
  1422. return ret;
  1423. return count;
  1424. }
  1425. TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
  1426. static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
  1427. &tcm_vhost_tpg_nexus.attr,
  1428. NULL,
  1429. };
  1430. static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
  1431. struct config_group *group,
  1432. const char *name)
  1433. {
  1434. struct tcm_vhost_tport *tport = container_of(wwn,
  1435. struct tcm_vhost_tport, tport_wwn);
  1436. struct tcm_vhost_tpg *tpg;
  1437. unsigned long tpgt;
  1438. int ret;
  1439. if (strstr(name, "tpgt_") != name)
  1440. return ERR_PTR(-EINVAL);
  1441. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
  1442. return ERR_PTR(-EINVAL);
  1443. tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
  1444. if (!tpg) {
  1445. pr_err("Unable to allocate struct tcm_vhost_tpg");
  1446. return ERR_PTR(-ENOMEM);
  1447. }
  1448. mutex_init(&tpg->tv_tpg_mutex);
  1449. INIT_LIST_HEAD(&tpg->tv_tpg_list);
  1450. tpg->tport = tport;
  1451. tpg->tport_tpgt = tpgt;
  1452. ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
  1453. &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
  1454. if (ret < 0) {
  1455. kfree(tpg);
  1456. return NULL;
  1457. }
  1458. mutex_lock(&tcm_vhost_mutex);
  1459. list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
  1460. mutex_unlock(&tcm_vhost_mutex);
  1461. return &tpg->se_tpg;
  1462. }
  1463. static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
  1464. {
  1465. struct tcm_vhost_tpg *tpg = container_of(se_tpg,
  1466. struct tcm_vhost_tpg, se_tpg);
  1467. mutex_lock(&tcm_vhost_mutex);
  1468. list_del(&tpg->tv_tpg_list);
  1469. mutex_unlock(&tcm_vhost_mutex);
  1470. /*
  1471. * Release the virtual I_T Nexus for this vhost TPG
  1472. */
  1473. tcm_vhost_drop_nexus(tpg);
  1474. /*
  1475. * Deregister the se_tpg from TCM..
  1476. */
  1477. core_tpg_deregister(se_tpg);
  1478. kfree(tpg);
  1479. }
  1480. static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
  1481. struct config_group *group,
  1482. const char *name)
  1483. {
  1484. struct tcm_vhost_tport *tport;
  1485. char *ptr;
  1486. u64 wwpn = 0;
  1487. int off = 0;
  1488. /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
  1489. return ERR_PTR(-EINVAL); */
  1490. tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
  1491. if (!tport) {
  1492. pr_err("Unable to allocate struct tcm_vhost_tport");
  1493. return ERR_PTR(-ENOMEM);
  1494. }
  1495. tport->tport_wwpn = wwpn;
  1496. /*
  1497. * Determine the emulated Protocol Identifier and Target Port Name
  1498. * based on the incoming configfs directory name.
  1499. */
  1500. ptr = strstr(name, "naa.");
  1501. if (ptr) {
  1502. tport->tport_proto_id = SCSI_PROTOCOL_SAS;
  1503. goto check_len;
  1504. }
  1505. ptr = strstr(name, "fc.");
  1506. if (ptr) {
  1507. tport->tport_proto_id = SCSI_PROTOCOL_FCP;
  1508. off = 3; /* Skip over "fc." */
  1509. goto check_len;
  1510. }
  1511. ptr = strstr(name, "iqn.");
  1512. if (ptr) {
  1513. tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
  1514. goto check_len;
  1515. }
  1516. pr_err("Unable to locate prefix for emulated Target Port:"
  1517. " %s\n", name);
  1518. kfree(tport);
  1519. return ERR_PTR(-EINVAL);
  1520. check_len:
  1521. if (strlen(name) >= TCM_VHOST_NAMELEN) {
  1522. pr_err("Emulated %s Address: %s, exceeds"
  1523. " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
  1524. TCM_VHOST_NAMELEN);
  1525. kfree(tport);
  1526. return ERR_PTR(-EINVAL);
  1527. }
  1528. snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
  1529. pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
  1530. " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
  1531. return &tport->tport_wwn;
  1532. }
  1533. static void tcm_vhost_drop_tport(struct se_wwn *wwn)
  1534. {
  1535. struct tcm_vhost_tport *tport = container_of(wwn,
  1536. struct tcm_vhost_tport, tport_wwn);
  1537. pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
  1538. " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
  1539. tport->tport_name);
  1540. kfree(tport);
  1541. }
  1542. static ssize_t tcm_vhost_wwn_show_attr_version(
  1543. struct target_fabric_configfs *tf,
  1544. char *page)
  1545. {
  1546. return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
  1547. "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
  1548. utsname()->machine);
  1549. }
  1550. TF_WWN_ATTR_RO(tcm_vhost, version);
  1551. static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
  1552. &tcm_vhost_wwn_version.attr,
  1553. NULL,
  1554. };
  1555. static struct target_core_fabric_ops tcm_vhost_ops = {
  1556. .get_fabric_name = tcm_vhost_get_fabric_name,
  1557. .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
  1558. .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
  1559. .tpg_get_tag = tcm_vhost_get_tag,
  1560. .tpg_get_default_depth = tcm_vhost_get_default_depth,
  1561. .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
  1562. .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
  1563. .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
  1564. .tpg_check_demo_mode = tcm_vhost_check_true,
  1565. .tpg_check_demo_mode_cache = tcm_vhost_check_true,
  1566. .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
  1567. .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
  1568. .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
  1569. .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
  1570. .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
  1571. .release_cmd = tcm_vhost_release_cmd,
  1572. .shutdown_session = tcm_vhost_shutdown_session,
  1573. .close_session = tcm_vhost_close_session,
  1574. .sess_get_index = tcm_vhost_sess_get_index,
  1575. .sess_get_initiator_sid = NULL,
  1576. .write_pending = tcm_vhost_write_pending,
  1577. .write_pending_status = tcm_vhost_write_pending_status,
  1578. .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
  1579. .get_task_tag = tcm_vhost_get_task_tag,
  1580. .get_cmd_state = tcm_vhost_get_cmd_state,
  1581. .queue_data_in = tcm_vhost_queue_data_in,
  1582. .queue_status = tcm_vhost_queue_status,
  1583. .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
  1584. /*
  1585. * Setup callers for generic logic in target_core_fabric_configfs.c
  1586. */
  1587. .fabric_make_wwn = tcm_vhost_make_tport,
  1588. .fabric_drop_wwn = tcm_vhost_drop_tport,
  1589. .fabric_make_tpg = tcm_vhost_make_tpg,
  1590. .fabric_drop_tpg = tcm_vhost_drop_tpg,
  1591. .fabric_post_link = tcm_vhost_port_link,
  1592. .fabric_pre_unlink = tcm_vhost_port_unlink,
  1593. .fabric_make_np = NULL,
  1594. .fabric_drop_np = NULL,
  1595. .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
  1596. .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
  1597. };
  1598. static int tcm_vhost_register_configfs(void)
  1599. {
  1600. struct target_fabric_configfs *fabric;
  1601. int ret;
  1602. pr_debug("TCM_VHOST fabric module %s on %s/%s"
  1603. " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
  1604. utsname()->machine);
  1605. /*
  1606. * Register the top level struct config_item_type with TCM core
  1607. */
  1608. fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
  1609. if (IS_ERR(fabric)) {
  1610. pr_err("target_fabric_configfs_init() failed\n");
  1611. return PTR_ERR(fabric);
  1612. }
  1613. /*
  1614. * Setup fabric->tf_ops from our local tcm_vhost_ops
  1615. */
  1616. fabric->tf_ops = tcm_vhost_ops;
  1617. /*
  1618. * Setup default attribute lists for various fabric->tf_cit_tmpl
  1619. */
  1620. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
  1621. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
  1622. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
  1623. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1624. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1625. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  1626. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  1627. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  1628. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  1629. /*
  1630. * Register the fabric for use within TCM
  1631. */
  1632. ret = target_fabric_configfs_register(fabric);
  1633. if (ret < 0) {
  1634. pr_err("target_fabric_configfs_register() failed"
  1635. " for TCM_VHOST\n");
  1636. return ret;
  1637. }
  1638. /*
  1639. * Setup our local pointer to *fabric
  1640. */
  1641. tcm_vhost_fabric_configfs = fabric;
  1642. pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
  1643. return 0;
  1644. };
  1645. static void tcm_vhost_deregister_configfs(void)
  1646. {
  1647. if (!tcm_vhost_fabric_configfs)
  1648. return;
  1649. target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
  1650. tcm_vhost_fabric_configfs = NULL;
  1651. pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
  1652. };
  1653. static int __init tcm_vhost_init(void)
  1654. {
  1655. int ret = -ENOMEM;
  1656. /*
  1657. * Use our own dedicated workqueue for submitting I/O into
  1658. * target core to avoid contention within system_wq.
  1659. */
  1660. tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
  1661. if (!tcm_vhost_workqueue)
  1662. goto out;
  1663. ret = vhost_scsi_register();
  1664. if (ret < 0)
  1665. goto out_destroy_workqueue;
  1666. ret = tcm_vhost_register_configfs();
  1667. if (ret < 0)
  1668. goto out_vhost_scsi_deregister;
  1669. return 0;
  1670. out_vhost_scsi_deregister:
  1671. vhost_scsi_deregister();
  1672. out_destroy_workqueue:
  1673. destroy_workqueue(tcm_vhost_workqueue);
  1674. out:
  1675. return ret;
  1676. };
  1677. static void tcm_vhost_exit(void)
  1678. {
  1679. tcm_vhost_deregister_configfs();
  1680. vhost_scsi_deregister();
  1681. destroy_workqueue(tcm_vhost_workqueue);
  1682. };
  1683. MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
  1684. MODULE_LICENSE("GPL");
  1685. module_init(tcm_vhost_init);
  1686. module_exit(tcm_vhost_exit);