tcm_vhost.c 41 KB

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