tcm_vhost.c 44 KB

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