tcm_vhost.c 43 KB

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