tcm_vhost.c 42 KB

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