tcm_vhost.c 43 KB

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