tcm_vhost.c 41 KB

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