tcm_vhost.c 42 KB

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