tcm_vhost.c 42 KB

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