tcm_vhost.c 43 KB

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