tcm_qla2xxx.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  1. /*******************************************************************************
  2. * This file contains tcm implementation using v4 configfs fabric infrastructure
  3. * for QLogic target mode HBAs
  4. *
  5. * ?? Copyright 2010-2011 RisingTide Systems LLC.
  6. *
  7. * Licensed to the Linux Foundation under the General Public License (GPL)
  8. * version 2.
  9. *
  10. * Author: Nicholas A. Bellinger <nab@risingtidesystems.com>
  11. *
  12. * tcm_qla2xxx_parse_wwn() and tcm_qla2xxx_format_wwn() contains code from
  13. * the TCM_FC / Open-FCoE.org fabric module.
  14. *
  15. * Copyright (c) 2010 Cisco Systems, Inc
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. ****************************************************************************/
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <generated/utsrelease.h>
  30. #include <linux/utsname.h>
  31. #include <linux/init.h>
  32. #include <linux/list.h>
  33. #include <linux/slab.h>
  34. #include <linux/kthread.h>
  35. #include <linux/types.h>
  36. #include <linux/string.h>
  37. #include <linux/configfs.h>
  38. #include <linux/ctype.h>
  39. #include <asm/unaligned.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_host.h>
  42. #include <scsi/scsi_device.h>
  43. #include <scsi/scsi_cmnd.h>
  44. #include <target/target_core_base.h>
  45. #include <target/target_core_fabric.h>
  46. #include <target/target_core_fabric_configfs.h>
  47. #include <target/target_core_configfs.h>
  48. #include <target/configfs_macros.h>
  49. #include "qla_def.h"
  50. #include "qla_target.h"
  51. #include "tcm_qla2xxx.h"
  52. struct workqueue_struct *tcm_qla2xxx_free_wq;
  53. struct workqueue_struct *tcm_qla2xxx_cmd_wq;
  54. static int tcm_qla2xxx_check_true(struct se_portal_group *se_tpg)
  55. {
  56. return 1;
  57. }
  58. static int tcm_qla2xxx_check_false(struct se_portal_group *se_tpg)
  59. {
  60. return 0;
  61. }
  62. /*
  63. * Parse WWN.
  64. * If strict, we require lower-case hex and colon separators to be sure
  65. * the name is the same as what would be generated by ft_format_wwn()
  66. * so the name and wwn are mapped one-to-one.
  67. */
  68. static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
  69. {
  70. const char *cp;
  71. char c;
  72. u32 nibble;
  73. u32 byte = 0;
  74. u32 pos = 0;
  75. u32 err;
  76. *wwn = 0;
  77. for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
  78. c = *cp;
  79. if (c == '\n' && cp[1] == '\0')
  80. continue;
  81. if (strict && pos++ == 2 && byte++ < 7) {
  82. pos = 0;
  83. if (c == ':')
  84. continue;
  85. err = 1;
  86. goto fail;
  87. }
  88. if (c == '\0') {
  89. err = 2;
  90. if (strict && byte != 8)
  91. goto fail;
  92. return cp - name;
  93. }
  94. err = 3;
  95. if (isdigit(c))
  96. nibble = c - '0';
  97. else if (isxdigit(c) && (islower(c) || !strict))
  98. nibble = tolower(c) - 'a' + 10;
  99. else
  100. goto fail;
  101. *wwn = (*wwn << 4) | nibble;
  102. }
  103. err = 4;
  104. fail:
  105. pr_debug("err %u len %zu pos %u byte %u\n",
  106. err, cp - name, pos, byte);
  107. return -1;
  108. }
  109. static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
  110. {
  111. u8 b[8];
  112. put_unaligned_be64(wwn, b);
  113. return snprintf(buf, len,
  114. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
  115. b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
  116. }
  117. static char *tcm_qla2xxx_get_fabric_name(void)
  118. {
  119. return "qla2xxx";
  120. }
  121. /*
  122. * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
  123. */
  124. static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
  125. {
  126. unsigned int i, j;
  127. u8 wwn[8];
  128. memset(wwn, 0, sizeof(wwn));
  129. /* Validate and store the new name */
  130. for (i = 0, j = 0; i < 16; i++) {
  131. int value;
  132. value = hex_to_bin(*ns++);
  133. if (value >= 0)
  134. j = (j << 4) | value;
  135. else
  136. return -EINVAL;
  137. if (i % 2) {
  138. wwn[i/2] = j & 0xff;
  139. j = 0;
  140. }
  141. }
  142. *nm = wwn_to_u64(wwn);
  143. return 0;
  144. }
  145. /*
  146. * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
  147. * store_fc_host_vport_create()
  148. */
  149. static int tcm_qla2xxx_npiv_parse_wwn(
  150. const char *name,
  151. size_t count,
  152. u64 *wwpn,
  153. u64 *wwnn)
  154. {
  155. unsigned int cnt = count;
  156. int rc;
  157. *wwpn = 0;
  158. *wwnn = 0;
  159. /* count may include a LF at end of string */
  160. if (name[cnt-1] == '\n')
  161. cnt--;
  162. /* validate we have enough characters for WWPN */
  163. if ((cnt != (16+1+16)) || (name[16] != ':'))
  164. return -EINVAL;
  165. rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
  166. if (rc != 0)
  167. return rc;
  168. rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
  169. if (rc != 0)
  170. return rc;
  171. return 0;
  172. }
  173. static ssize_t tcm_qla2xxx_npiv_format_wwn(char *buf, size_t len,
  174. u64 wwpn, u64 wwnn)
  175. {
  176. u8 b[8], b2[8];
  177. put_unaligned_be64(wwpn, b);
  178. put_unaligned_be64(wwnn, b2);
  179. return snprintf(buf, len,
  180. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x,"
  181. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
  182. b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
  183. b2[0], b2[1], b2[2], b2[3], b2[4], b2[5], b2[6], b2[7]);
  184. }
  185. static char *tcm_qla2xxx_npiv_get_fabric_name(void)
  186. {
  187. return "qla2xxx_npiv";
  188. }
  189. static u8 tcm_qla2xxx_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  190. {
  191. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  192. struct tcm_qla2xxx_tpg, se_tpg);
  193. struct tcm_qla2xxx_lport *lport = tpg->lport;
  194. u8 proto_id;
  195. switch (lport->lport_proto_id) {
  196. case SCSI_PROTOCOL_FCP:
  197. default:
  198. proto_id = fc_get_fabric_proto_ident(se_tpg);
  199. break;
  200. }
  201. return proto_id;
  202. }
  203. static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
  204. {
  205. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  206. struct tcm_qla2xxx_tpg, se_tpg);
  207. struct tcm_qla2xxx_lport *lport = tpg->lport;
  208. return &lport->lport_name[0];
  209. }
  210. static char *tcm_qla2xxx_npiv_get_fabric_wwn(struct se_portal_group *se_tpg)
  211. {
  212. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  213. struct tcm_qla2xxx_tpg, se_tpg);
  214. struct tcm_qla2xxx_lport *lport = tpg->lport;
  215. return &lport->lport_npiv_name[0];
  216. }
  217. static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
  218. {
  219. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  220. struct tcm_qla2xxx_tpg, se_tpg);
  221. return tpg->lport_tpgt;
  222. }
  223. static u32 tcm_qla2xxx_get_default_depth(struct se_portal_group *se_tpg)
  224. {
  225. return 1;
  226. }
  227. static u32 tcm_qla2xxx_get_pr_transport_id(
  228. struct se_portal_group *se_tpg,
  229. struct se_node_acl *se_nacl,
  230. struct t10_pr_registration *pr_reg,
  231. int *format_code,
  232. unsigned char *buf)
  233. {
  234. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  235. struct tcm_qla2xxx_tpg, se_tpg);
  236. struct tcm_qla2xxx_lport *lport = tpg->lport;
  237. int ret = 0;
  238. switch (lport->lport_proto_id) {
  239. case SCSI_PROTOCOL_FCP:
  240. default:
  241. ret = fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  242. format_code, buf);
  243. break;
  244. }
  245. return ret;
  246. }
  247. static u32 tcm_qla2xxx_get_pr_transport_id_len(
  248. struct se_portal_group *se_tpg,
  249. struct se_node_acl *se_nacl,
  250. struct t10_pr_registration *pr_reg,
  251. int *format_code)
  252. {
  253. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  254. struct tcm_qla2xxx_tpg, se_tpg);
  255. struct tcm_qla2xxx_lport *lport = tpg->lport;
  256. int ret = 0;
  257. switch (lport->lport_proto_id) {
  258. case SCSI_PROTOCOL_FCP:
  259. default:
  260. ret = fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  261. format_code);
  262. break;
  263. }
  264. return ret;
  265. }
  266. static char *tcm_qla2xxx_parse_pr_out_transport_id(
  267. struct se_portal_group *se_tpg,
  268. const char *buf,
  269. u32 *out_tid_len,
  270. char **port_nexus_ptr)
  271. {
  272. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  273. struct tcm_qla2xxx_tpg, se_tpg);
  274. struct tcm_qla2xxx_lport *lport = tpg->lport;
  275. char *tid = NULL;
  276. switch (lport->lport_proto_id) {
  277. case SCSI_PROTOCOL_FCP:
  278. default:
  279. tid = fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  280. port_nexus_ptr);
  281. break;
  282. }
  283. return tid;
  284. }
  285. static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
  286. {
  287. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  288. struct tcm_qla2xxx_tpg, se_tpg);
  289. return QLA_TPG_ATTRIB(tpg)->generate_node_acls;
  290. }
  291. static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
  292. {
  293. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  294. struct tcm_qla2xxx_tpg, se_tpg);
  295. return QLA_TPG_ATTRIB(tpg)->cache_dynamic_acls;
  296. }
  297. static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
  298. {
  299. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  300. struct tcm_qla2xxx_tpg, se_tpg);
  301. return QLA_TPG_ATTRIB(tpg)->demo_mode_write_protect;
  302. }
  303. static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
  304. {
  305. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  306. struct tcm_qla2xxx_tpg, se_tpg);
  307. return QLA_TPG_ATTRIB(tpg)->prod_mode_write_protect;
  308. }
  309. static struct se_node_acl *tcm_qla2xxx_alloc_fabric_acl(
  310. struct se_portal_group *se_tpg)
  311. {
  312. struct tcm_qla2xxx_nacl *nacl;
  313. nacl = kzalloc(sizeof(struct tcm_qla2xxx_nacl), GFP_KERNEL);
  314. if (!nacl) {
  315. pr_err("Unable to alocate struct tcm_qla2xxx_nacl\n");
  316. return NULL;
  317. }
  318. return &nacl->se_node_acl;
  319. }
  320. static void tcm_qla2xxx_release_fabric_acl(
  321. struct se_portal_group *se_tpg,
  322. struct se_node_acl *se_nacl)
  323. {
  324. struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
  325. struct tcm_qla2xxx_nacl, se_node_acl);
  326. kfree(nacl);
  327. }
  328. static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
  329. {
  330. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  331. struct tcm_qla2xxx_tpg, se_tpg);
  332. return tpg->lport_tpgt;
  333. }
  334. static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
  335. {
  336. struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
  337. struct qla_tgt_mgmt_cmd, free_work);
  338. transport_generic_free_cmd(&mcmd->se_cmd, 0);
  339. }
  340. /*
  341. * Called from qla_target_template->free_mcmd(), and will call
  342. * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
  343. * release callback. qla_hw_data->hardware_lock is expected to be held
  344. */
  345. static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
  346. {
  347. INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
  348. queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
  349. }
  350. static void tcm_qla2xxx_complete_free(struct work_struct *work)
  351. {
  352. struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
  353. transport_generic_free_cmd(&cmd->se_cmd, 0);
  354. }
  355. /*
  356. * Called from qla_target_template->free_cmd(), and will call
  357. * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
  358. * release callback. qla_hw_data->hardware_lock is expected to be held
  359. */
  360. static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
  361. {
  362. INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
  363. queue_work(tcm_qla2xxx_free_wq, &cmd->work);
  364. }
  365. /*
  366. * Called from struct target_core_fabric_ops->check_stop_free() context
  367. */
  368. static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
  369. {
  370. return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
  371. }
  372. /* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
  373. * fabric descriptor @se_cmd command to release
  374. */
  375. static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
  376. {
  377. struct qla_tgt_cmd *cmd;
  378. if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
  379. struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
  380. struct qla_tgt_mgmt_cmd, se_cmd);
  381. qlt_free_mcmd(mcmd);
  382. return;
  383. }
  384. cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
  385. qlt_free_cmd(cmd);
  386. }
  387. static int tcm_qla2xxx_shutdown_session(struct se_session *se_sess)
  388. {
  389. struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
  390. struct scsi_qla_host *vha;
  391. unsigned long flags;
  392. BUG_ON(!sess);
  393. vha = sess->vha;
  394. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  395. sess->tearing_down = 1;
  396. target_splice_sess_cmd_list(se_sess);
  397. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  398. return 1;
  399. }
  400. static void tcm_qla2xxx_close_session(struct se_session *se_sess)
  401. {
  402. struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
  403. struct scsi_qla_host *vha;
  404. unsigned long flags;
  405. BUG_ON(!sess);
  406. vha = sess->vha;
  407. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  408. qlt_unreg_sess(sess);
  409. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  410. }
  411. static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
  412. {
  413. return 0;
  414. }
  415. /*
  416. * The LIO target core uses DMA_TO_DEVICE to mean that data is going
  417. * to the target (eg handling a WRITE) and DMA_FROM_DEVICE to mean
  418. * that data is coming from the target (eg handling a READ). However,
  419. * this is just the opposite of what we have to tell the DMA mapping
  420. * layer -- eg when handling a READ, the HBA will have to DMA the data
  421. * out of memory so it can send it to the initiator, which means we
  422. * need to use DMA_TO_DEVICE when we map the data.
  423. */
  424. static enum dma_data_direction tcm_qla2xxx_mapping_dir(struct se_cmd *se_cmd)
  425. {
  426. if (se_cmd->se_cmd_flags & SCF_BIDI)
  427. return DMA_BIDIRECTIONAL;
  428. switch (se_cmd->data_direction) {
  429. case DMA_TO_DEVICE:
  430. return DMA_FROM_DEVICE;
  431. case DMA_FROM_DEVICE:
  432. return DMA_TO_DEVICE;
  433. case DMA_NONE:
  434. default:
  435. return DMA_NONE;
  436. }
  437. }
  438. static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
  439. {
  440. struct qla_tgt_cmd *cmd = container_of(se_cmd,
  441. struct qla_tgt_cmd, se_cmd);
  442. cmd->bufflen = se_cmd->data_length;
  443. cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
  444. cmd->sg_cnt = se_cmd->t_data_nents;
  445. cmd->sg = se_cmd->t_data_sg;
  446. /*
  447. * qla_target.c:qlt_rdy_to_xfer() will call pci_map_sg() to setup
  448. * the SGL mappings into PCIe memory for incoming FCP WRITE data.
  449. */
  450. return qlt_rdy_to_xfer(cmd);
  451. }
  452. static int tcm_qla2xxx_write_pending_status(struct se_cmd *se_cmd)
  453. {
  454. unsigned long flags;
  455. /*
  456. * Check for WRITE_PENDING status to determine if we need to wait for
  457. * CTIO aborts to be posted via hardware in tcm_qla2xxx_handle_data().
  458. */
  459. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  460. if (se_cmd->t_state == TRANSPORT_WRITE_PENDING ||
  461. se_cmd->t_state == TRANSPORT_COMPLETE_QF_WP) {
  462. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  463. wait_for_completion_timeout(&se_cmd->t_transport_stop_comp,
  464. 3000);
  465. return 0;
  466. }
  467. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  468. return 0;
  469. }
  470. static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
  471. {
  472. return;
  473. }
  474. static u32 tcm_qla2xxx_get_task_tag(struct se_cmd *se_cmd)
  475. {
  476. struct qla_tgt_cmd *cmd = container_of(se_cmd,
  477. struct qla_tgt_cmd, se_cmd);
  478. return cmd->tag;
  479. }
  480. static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
  481. {
  482. return 0;
  483. }
  484. /*
  485. * Called from process context in qla_target.c:qlt_do_work() code
  486. */
  487. static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
  488. unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
  489. int data_dir, int bidi)
  490. {
  491. struct se_cmd *se_cmd = &cmd->se_cmd;
  492. struct se_session *se_sess;
  493. struct qla_tgt_sess *sess;
  494. int flags = TARGET_SCF_ACK_KREF;
  495. if (bidi)
  496. flags |= TARGET_SCF_BIDI_OP;
  497. sess = cmd->sess;
  498. if (!sess) {
  499. pr_err("Unable to locate struct qla_tgt_sess from qla_tgt_cmd\n");
  500. return -EINVAL;
  501. }
  502. se_sess = sess->se_sess;
  503. if (!se_sess) {
  504. pr_err("Unable to locate active struct se_session\n");
  505. return -EINVAL;
  506. }
  507. target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
  508. cmd->unpacked_lun, data_length, fcp_task_attr,
  509. data_dir, flags);
  510. return 0;
  511. }
  512. static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
  513. {
  514. struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
  515. /*
  516. * Ensure that the complete FCP WRITE payload has been received.
  517. * Otherwise return an exception via CHECK_CONDITION status.
  518. */
  519. if (!cmd->write_data_transferred) {
  520. /*
  521. * Check if se_cmd has already been aborted via LUN_RESET, and
  522. * waiting upon completion in tcm_qla2xxx_write_pending_status()
  523. */
  524. if (cmd->se_cmd.transport_state & CMD_T_ABORTED) {
  525. complete(&cmd->se_cmd.t_transport_stop_comp);
  526. return;
  527. }
  528. cmd->se_cmd.scsi_sense_reason = TCM_CHECK_CONDITION_ABORT_CMD;
  529. transport_generic_request_failure(&cmd->se_cmd);
  530. return;
  531. }
  532. return target_execute_cmd(&cmd->se_cmd);
  533. }
  534. /*
  535. * Called from qla_target.c:qlt_do_ctio_completion()
  536. */
  537. static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
  538. {
  539. INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
  540. queue_work(tcm_qla2xxx_free_wq, &cmd->work);
  541. }
  542. /*
  543. * Called from qla_target.c:qlt_issue_task_mgmt()
  544. */
  545. static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, uint32_t lun,
  546. uint8_t tmr_func, uint32_t tag)
  547. {
  548. struct qla_tgt_sess *sess = mcmd->sess;
  549. struct se_cmd *se_cmd = &mcmd->se_cmd;
  550. return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
  551. tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF);
  552. }
  553. static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
  554. {
  555. struct qla_tgt_cmd *cmd = container_of(se_cmd,
  556. struct qla_tgt_cmd, se_cmd);
  557. cmd->bufflen = se_cmd->data_length;
  558. cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
  559. cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
  560. cmd->sg_cnt = se_cmd->t_data_nents;
  561. cmd->sg = se_cmd->t_data_sg;
  562. cmd->offset = 0;
  563. /*
  564. * Now queue completed DATA_IN the qla2xxx LLD and response ring
  565. */
  566. return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
  567. se_cmd->scsi_status);
  568. }
  569. static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
  570. {
  571. struct qla_tgt_cmd *cmd = container_of(se_cmd,
  572. struct qla_tgt_cmd, se_cmd);
  573. int xmit_type = QLA_TGT_XMIT_STATUS;
  574. cmd->bufflen = se_cmd->data_length;
  575. cmd->sg = NULL;
  576. cmd->sg_cnt = 0;
  577. cmd->offset = 0;
  578. cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
  579. cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
  580. if (se_cmd->data_direction == DMA_FROM_DEVICE) {
  581. /*
  582. * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
  583. * for qla_tgt_xmit_response LLD code
  584. */
  585. se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  586. se_cmd->residual_count = se_cmd->data_length;
  587. cmd->bufflen = 0;
  588. }
  589. /*
  590. * Now queue status response to qla2xxx LLD code and response ring
  591. */
  592. return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
  593. }
  594. static int tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
  595. {
  596. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  597. struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
  598. struct qla_tgt_mgmt_cmd, se_cmd);
  599. pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
  600. mcmd, se_tmr->function, se_tmr->response);
  601. /*
  602. * Do translation between TCM TM response codes and
  603. * QLA2xxx FC TM response codes.
  604. */
  605. switch (se_tmr->response) {
  606. case TMR_FUNCTION_COMPLETE:
  607. mcmd->fc_tm_rsp = FC_TM_SUCCESS;
  608. break;
  609. case TMR_TASK_DOES_NOT_EXIST:
  610. mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
  611. break;
  612. case TMR_FUNCTION_REJECTED:
  613. mcmd->fc_tm_rsp = FC_TM_REJECT;
  614. break;
  615. case TMR_LUN_DOES_NOT_EXIST:
  616. default:
  617. mcmd->fc_tm_rsp = FC_TM_FAILED;
  618. break;
  619. }
  620. /*
  621. * Queue the TM response to QLA2xxx LLD to build a
  622. * CTIO response packet.
  623. */
  624. qlt_xmit_tm_rsp(mcmd);
  625. return 0;
  626. }
  627. static u16 tcm_qla2xxx_get_fabric_sense_len(void)
  628. {
  629. return 0;
  630. }
  631. static u16 tcm_qla2xxx_set_fabric_sense_len(struct se_cmd *se_cmd,
  632. u32 sense_length)
  633. {
  634. return 0;
  635. }
  636. /* Local pointer to allocated TCM configfs fabric module */
  637. struct target_fabric_configfs *tcm_qla2xxx_fabric_configfs;
  638. struct target_fabric_configfs *tcm_qla2xxx_npiv_fabric_configfs;
  639. static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
  640. struct tcm_qla2xxx_nacl *, struct qla_tgt_sess *);
  641. /*
  642. * Expected to be called with struct qla_hw_data->hardware_lock held
  643. */
  644. static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct qla_tgt_sess *sess)
  645. {
  646. struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
  647. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  648. struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
  649. struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
  650. struct tcm_qla2xxx_lport, lport_wwn);
  651. struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
  652. struct tcm_qla2xxx_nacl, se_node_acl);
  653. void *node;
  654. pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
  655. node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
  656. WARN_ON(node && (node != se_nacl));
  657. pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
  658. se_nacl, nacl->nport_wwnn, nacl->nport_id);
  659. /*
  660. * Now clear the se_nacl and session pointers from our HW lport lookup
  661. * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
  662. *
  663. * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
  664. * target_wait_for_sess_cmds() before the session waits for outstanding
  665. * I/O to complete, to avoid a race between session shutdown execution
  666. * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
  667. */
  668. tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
  669. }
  670. static void tcm_qla2xxx_release_session(struct kref *kref)
  671. {
  672. struct se_session *se_sess = container_of(kref,
  673. struct se_session, sess_kref);
  674. qlt_unreg_sess(se_sess->fabric_sess_ptr);
  675. }
  676. static void tcm_qla2xxx_put_session(struct se_session *se_sess)
  677. {
  678. struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
  679. struct qla_hw_data *ha = sess->vha->hw;
  680. unsigned long flags;
  681. spin_lock_irqsave(&ha->hardware_lock, flags);
  682. kref_put(&se_sess->sess_kref, tcm_qla2xxx_release_session);
  683. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  684. }
  685. static void tcm_qla2xxx_put_sess(struct qla_tgt_sess *sess)
  686. {
  687. tcm_qla2xxx_put_session(sess->se_sess);
  688. }
  689. static void tcm_qla2xxx_shutdown_sess(struct qla_tgt_sess *sess)
  690. {
  691. tcm_qla2xxx_shutdown_session(sess->se_sess);
  692. }
  693. static struct se_node_acl *tcm_qla2xxx_make_nodeacl(
  694. struct se_portal_group *se_tpg,
  695. struct config_group *group,
  696. const char *name)
  697. {
  698. struct se_node_acl *se_nacl, *se_nacl_new;
  699. struct tcm_qla2xxx_nacl *nacl;
  700. u64 wwnn;
  701. u32 qla2xxx_nexus_depth;
  702. if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
  703. return ERR_PTR(-EINVAL);
  704. se_nacl_new = tcm_qla2xxx_alloc_fabric_acl(se_tpg);
  705. if (!se_nacl_new)
  706. return ERR_PTR(-ENOMEM);
  707. /* #warning FIXME: Hardcoded qla2xxx_nexus depth in tcm_qla2xxx_make_nodeacl */
  708. qla2xxx_nexus_depth = 1;
  709. /*
  710. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  711. * when converting a NodeACL from demo mode -> explict
  712. */
  713. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  714. name, qla2xxx_nexus_depth);
  715. if (IS_ERR(se_nacl)) {
  716. tcm_qla2xxx_release_fabric_acl(se_tpg, se_nacl_new);
  717. return se_nacl;
  718. }
  719. /*
  720. * Locate our struct tcm_qla2xxx_nacl and set the FC Nport WWPN
  721. */
  722. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  723. nacl->nport_wwnn = wwnn;
  724. tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
  725. return se_nacl;
  726. }
  727. static void tcm_qla2xxx_drop_nodeacl(struct se_node_acl *se_acl)
  728. {
  729. struct se_portal_group *se_tpg = se_acl->se_tpg;
  730. struct tcm_qla2xxx_nacl *nacl = container_of(se_acl,
  731. struct tcm_qla2xxx_nacl, se_node_acl);
  732. core_tpg_del_initiator_node_acl(se_tpg, se_acl, 1);
  733. kfree(nacl);
  734. }
  735. /* Start items for tcm_qla2xxx_tpg_attrib_cit */
  736. #define DEF_QLA_TPG_ATTRIB(name) \
  737. \
  738. static ssize_t tcm_qla2xxx_tpg_attrib_show_##name( \
  739. struct se_portal_group *se_tpg, \
  740. char *page) \
  741. { \
  742. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
  743. struct tcm_qla2xxx_tpg, se_tpg); \
  744. \
  745. return sprintf(page, "%u\n", QLA_TPG_ATTRIB(tpg)->name); \
  746. } \
  747. \
  748. static ssize_t tcm_qla2xxx_tpg_attrib_store_##name( \
  749. struct se_portal_group *se_tpg, \
  750. const char *page, \
  751. size_t count) \
  752. { \
  753. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
  754. struct tcm_qla2xxx_tpg, se_tpg); \
  755. unsigned long val; \
  756. int ret; \
  757. \
  758. ret = kstrtoul(page, 0, &val); \
  759. if (ret < 0) { \
  760. pr_err("kstrtoul() failed with" \
  761. " ret: %d\n", ret); \
  762. return -EINVAL; \
  763. } \
  764. ret = tcm_qla2xxx_set_attrib_##name(tpg, val); \
  765. \
  766. return (!ret) ? count : -EINVAL; \
  767. }
  768. #define DEF_QLA_TPG_ATTR_BOOL(_name) \
  769. \
  770. static int tcm_qla2xxx_set_attrib_##_name( \
  771. struct tcm_qla2xxx_tpg *tpg, \
  772. unsigned long val) \
  773. { \
  774. struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib; \
  775. \
  776. if ((val != 0) && (val != 1)) { \
  777. pr_err("Illegal boolean value %lu\n", val); \
  778. return -EINVAL; \
  779. } \
  780. \
  781. a->_name = val; \
  782. return 0; \
  783. }
  784. #define QLA_TPG_ATTR(_name, _mode) \
  785. TF_TPG_ATTRIB_ATTR(tcm_qla2xxx, _name, _mode);
  786. /*
  787. * Define tcm_qla2xxx_tpg_attrib_s_generate_node_acls
  788. */
  789. DEF_QLA_TPG_ATTR_BOOL(generate_node_acls);
  790. DEF_QLA_TPG_ATTRIB(generate_node_acls);
  791. QLA_TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
  792. /*
  793. Define tcm_qla2xxx_attrib_s_cache_dynamic_acls
  794. */
  795. DEF_QLA_TPG_ATTR_BOOL(cache_dynamic_acls);
  796. DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
  797. QLA_TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
  798. /*
  799. * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_write_protect
  800. */
  801. DEF_QLA_TPG_ATTR_BOOL(demo_mode_write_protect);
  802. DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
  803. QLA_TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
  804. /*
  805. * Define tcm_qla2xxx_tpg_attrib_s_prod_mode_write_protect
  806. */
  807. DEF_QLA_TPG_ATTR_BOOL(prod_mode_write_protect);
  808. DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
  809. QLA_TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
  810. static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
  811. &tcm_qla2xxx_tpg_attrib_generate_node_acls.attr,
  812. &tcm_qla2xxx_tpg_attrib_cache_dynamic_acls.attr,
  813. &tcm_qla2xxx_tpg_attrib_demo_mode_write_protect.attr,
  814. &tcm_qla2xxx_tpg_attrib_prod_mode_write_protect.attr,
  815. NULL,
  816. };
  817. /* End items for tcm_qla2xxx_tpg_attrib_cit */
  818. static ssize_t tcm_qla2xxx_tpg_show_enable(
  819. struct se_portal_group *se_tpg,
  820. char *page)
  821. {
  822. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  823. struct tcm_qla2xxx_tpg, se_tpg);
  824. return snprintf(page, PAGE_SIZE, "%d\n",
  825. atomic_read(&tpg->lport_tpg_enabled));
  826. }
  827. static ssize_t tcm_qla2xxx_tpg_store_enable(
  828. struct se_portal_group *se_tpg,
  829. const char *page,
  830. size_t count)
  831. {
  832. struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
  833. struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
  834. struct tcm_qla2xxx_lport, lport_wwn);
  835. struct scsi_qla_host *vha = lport->qla_vha;
  836. struct qla_hw_data *ha = vha->hw;
  837. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  838. struct tcm_qla2xxx_tpg, se_tpg);
  839. unsigned long op;
  840. int rc;
  841. rc = kstrtoul(page, 0, &op);
  842. if (rc < 0) {
  843. pr_err("kstrtoul() returned %d\n", rc);
  844. return -EINVAL;
  845. }
  846. if ((op != 1) && (op != 0)) {
  847. pr_err("Illegal value for tpg_enable: %lu\n", op);
  848. return -EINVAL;
  849. }
  850. if (op) {
  851. atomic_set(&tpg->lport_tpg_enabled, 1);
  852. qlt_enable_vha(vha);
  853. } else {
  854. if (!ha->tgt.qla_tgt) {
  855. pr_err("truct qla_hw_data *ha->tgt.qla_tgt is NULL\n");
  856. return -ENODEV;
  857. }
  858. atomic_set(&tpg->lport_tpg_enabled, 0);
  859. qlt_stop_phase1(ha->tgt.qla_tgt);
  860. }
  861. return count;
  862. }
  863. TF_TPG_BASE_ATTR(tcm_qla2xxx, enable, S_IRUGO | S_IWUSR);
  864. static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
  865. &tcm_qla2xxx_tpg_enable.attr,
  866. NULL,
  867. };
  868. static struct se_portal_group *tcm_qla2xxx_make_tpg(
  869. struct se_wwn *wwn,
  870. struct config_group *group,
  871. const char *name)
  872. {
  873. struct tcm_qla2xxx_lport *lport = container_of(wwn,
  874. struct tcm_qla2xxx_lport, lport_wwn);
  875. struct tcm_qla2xxx_tpg *tpg;
  876. unsigned long tpgt;
  877. int ret;
  878. if (strstr(name, "tpgt_") != name)
  879. return ERR_PTR(-EINVAL);
  880. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
  881. return ERR_PTR(-EINVAL);
  882. if (!lport->qla_npiv_vp && (tpgt != 1)) {
  883. pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
  884. return ERR_PTR(-ENOSYS);
  885. }
  886. tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
  887. if (!tpg) {
  888. pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
  889. return ERR_PTR(-ENOMEM);
  890. }
  891. tpg->lport = lport;
  892. tpg->lport_tpgt = tpgt;
  893. /*
  894. * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
  895. * NodeACLs
  896. */
  897. QLA_TPG_ATTRIB(tpg)->generate_node_acls = 1;
  898. QLA_TPG_ATTRIB(tpg)->demo_mode_write_protect = 1;
  899. QLA_TPG_ATTRIB(tpg)->cache_dynamic_acls = 1;
  900. ret = core_tpg_register(&tcm_qla2xxx_fabric_configfs->tf_ops, wwn,
  901. &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
  902. if (ret < 0) {
  903. kfree(tpg);
  904. return NULL;
  905. }
  906. /*
  907. * Setup local TPG=1 pointer for non NPIV mode.
  908. */
  909. if (lport->qla_npiv_vp == NULL)
  910. lport->tpg_1 = tpg;
  911. return &tpg->se_tpg;
  912. }
  913. static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
  914. {
  915. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  916. struct tcm_qla2xxx_tpg, se_tpg);
  917. struct tcm_qla2xxx_lport *lport = tpg->lport;
  918. struct scsi_qla_host *vha = lport->qla_vha;
  919. struct qla_hw_data *ha = vha->hw;
  920. /*
  921. * Call into qla2x_target.c LLD logic to shutdown the active
  922. * FC Nexuses and disable target mode operation for this qla_hw_data
  923. */
  924. if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stop)
  925. qlt_stop_phase1(ha->tgt.qla_tgt);
  926. core_tpg_deregister(se_tpg);
  927. /*
  928. * Clear local TPG=1 pointer for non NPIV mode.
  929. */
  930. if (lport->qla_npiv_vp == NULL)
  931. lport->tpg_1 = NULL;
  932. kfree(tpg);
  933. }
  934. static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(
  935. struct se_wwn *wwn,
  936. struct config_group *group,
  937. const char *name)
  938. {
  939. struct tcm_qla2xxx_lport *lport = container_of(wwn,
  940. struct tcm_qla2xxx_lport, lport_wwn);
  941. struct tcm_qla2xxx_tpg *tpg;
  942. unsigned long tpgt;
  943. int ret;
  944. if (strstr(name, "tpgt_") != name)
  945. return ERR_PTR(-EINVAL);
  946. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
  947. return ERR_PTR(-EINVAL);
  948. tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
  949. if (!tpg) {
  950. pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
  951. return ERR_PTR(-ENOMEM);
  952. }
  953. tpg->lport = lport;
  954. tpg->lport_tpgt = tpgt;
  955. ret = core_tpg_register(&tcm_qla2xxx_npiv_fabric_configfs->tf_ops, wwn,
  956. &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
  957. if (ret < 0) {
  958. kfree(tpg);
  959. return NULL;
  960. }
  961. return &tpg->se_tpg;
  962. }
  963. /*
  964. * Expected to be called with struct qla_hw_data->hardware_lock held
  965. */
  966. static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_s_id(
  967. scsi_qla_host_t *vha,
  968. const uint8_t *s_id)
  969. {
  970. struct qla_hw_data *ha = vha->hw;
  971. struct tcm_qla2xxx_lport *lport;
  972. struct se_node_acl *se_nacl;
  973. struct tcm_qla2xxx_nacl *nacl;
  974. u32 key;
  975. lport = ha->tgt.target_lport_ptr;
  976. if (!lport) {
  977. pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
  978. dump_stack();
  979. return NULL;
  980. }
  981. key = (((unsigned long)s_id[0] << 16) |
  982. ((unsigned long)s_id[1] << 8) |
  983. (unsigned long)s_id[2]);
  984. pr_debug("find_sess_by_s_id: 0x%06x\n", key);
  985. se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
  986. if (!se_nacl) {
  987. pr_debug("Unable to locate s_id: 0x%06x\n", key);
  988. return NULL;
  989. }
  990. pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
  991. se_nacl, se_nacl->initiatorname);
  992. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  993. if (!nacl->qla_tgt_sess) {
  994. pr_err("Unable to locate struct qla_tgt_sess\n");
  995. return NULL;
  996. }
  997. return nacl->qla_tgt_sess;
  998. }
  999. /*
  1000. * Expected to be called with struct qla_hw_data->hardware_lock held
  1001. */
  1002. static void tcm_qla2xxx_set_sess_by_s_id(
  1003. struct tcm_qla2xxx_lport *lport,
  1004. struct se_node_acl *new_se_nacl,
  1005. struct tcm_qla2xxx_nacl *nacl,
  1006. struct se_session *se_sess,
  1007. struct qla_tgt_sess *qla_tgt_sess,
  1008. uint8_t *s_id)
  1009. {
  1010. u32 key;
  1011. void *slot;
  1012. int rc;
  1013. key = (((unsigned long)s_id[0] << 16) |
  1014. ((unsigned long)s_id[1] << 8) |
  1015. (unsigned long)s_id[2]);
  1016. pr_debug("set_sess_by_s_id: %06x\n", key);
  1017. slot = btree_lookup32(&lport->lport_fcport_map, key);
  1018. if (!slot) {
  1019. if (new_se_nacl) {
  1020. pr_debug("Setting up new fc_port entry to new_se_nacl\n");
  1021. nacl->nport_id = key;
  1022. rc = btree_insert32(&lport->lport_fcport_map, key,
  1023. new_se_nacl, GFP_ATOMIC);
  1024. if (rc)
  1025. printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
  1026. (int)key);
  1027. } else {
  1028. pr_debug("Wiping nonexisting fc_port entry\n");
  1029. }
  1030. qla_tgt_sess->se_sess = se_sess;
  1031. nacl->qla_tgt_sess = qla_tgt_sess;
  1032. return;
  1033. }
  1034. if (nacl->qla_tgt_sess) {
  1035. if (new_se_nacl == NULL) {
  1036. pr_debug("Clearing existing nacl->qla_tgt_sess and fc_port entry\n");
  1037. btree_remove32(&lport->lport_fcport_map, key);
  1038. nacl->qla_tgt_sess = NULL;
  1039. return;
  1040. }
  1041. pr_debug("Replacing existing nacl->qla_tgt_sess and fc_port entry\n");
  1042. btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
  1043. qla_tgt_sess->se_sess = se_sess;
  1044. nacl->qla_tgt_sess = qla_tgt_sess;
  1045. return;
  1046. }
  1047. if (new_se_nacl == NULL) {
  1048. pr_debug("Clearing existing fc_port entry\n");
  1049. btree_remove32(&lport->lport_fcport_map, key);
  1050. return;
  1051. }
  1052. pr_debug("Replacing existing fc_port entry w/o active nacl->qla_tgt_sess\n");
  1053. btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
  1054. qla_tgt_sess->se_sess = se_sess;
  1055. nacl->qla_tgt_sess = qla_tgt_sess;
  1056. pr_debug("Setup nacl->qla_tgt_sess %p by s_id for se_nacl: %p, initiatorname: %s\n",
  1057. nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
  1058. }
  1059. /*
  1060. * Expected to be called with struct qla_hw_data->hardware_lock held
  1061. */
  1062. static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_loop_id(
  1063. scsi_qla_host_t *vha,
  1064. const uint16_t loop_id)
  1065. {
  1066. struct qla_hw_data *ha = vha->hw;
  1067. struct tcm_qla2xxx_lport *lport;
  1068. struct se_node_acl *se_nacl;
  1069. struct tcm_qla2xxx_nacl *nacl;
  1070. struct tcm_qla2xxx_fc_loopid *fc_loopid;
  1071. lport = ha->tgt.target_lport_ptr;
  1072. if (!lport) {
  1073. pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
  1074. dump_stack();
  1075. return NULL;
  1076. }
  1077. pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
  1078. fc_loopid = lport->lport_loopid_map + loop_id;
  1079. se_nacl = fc_loopid->se_nacl;
  1080. if (!se_nacl) {
  1081. pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
  1082. loop_id);
  1083. return NULL;
  1084. }
  1085. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  1086. if (!nacl->qla_tgt_sess) {
  1087. pr_err("Unable to locate struct qla_tgt_sess\n");
  1088. return NULL;
  1089. }
  1090. return nacl->qla_tgt_sess;
  1091. }
  1092. /*
  1093. * Expected to be called with struct qla_hw_data->hardware_lock held
  1094. */
  1095. static void tcm_qla2xxx_set_sess_by_loop_id(
  1096. struct tcm_qla2xxx_lport *lport,
  1097. struct se_node_acl *new_se_nacl,
  1098. struct tcm_qla2xxx_nacl *nacl,
  1099. struct se_session *se_sess,
  1100. struct qla_tgt_sess *qla_tgt_sess,
  1101. uint16_t loop_id)
  1102. {
  1103. struct se_node_acl *saved_nacl;
  1104. struct tcm_qla2xxx_fc_loopid *fc_loopid;
  1105. pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
  1106. fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
  1107. lport->lport_loopid_map)[loop_id];
  1108. saved_nacl = fc_loopid->se_nacl;
  1109. if (!saved_nacl) {
  1110. pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
  1111. fc_loopid->se_nacl = new_se_nacl;
  1112. if (qla_tgt_sess->se_sess != se_sess)
  1113. qla_tgt_sess->se_sess = se_sess;
  1114. if (nacl->qla_tgt_sess != qla_tgt_sess)
  1115. nacl->qla_tgt_sess = qla_tgt_sess;
  1116. return;
  1117. }
  1118. if (nacl->qla_tgt_sess) {
  1119. if (new_se_nacl == NULL) {
  1120. pr_debug("Clearing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
  1121. fc_loopid->se_nacl = NULL;
  1122. nacl->qla_tgt_sess = NULL;
  1123. return;
  1124. }
  1125. pr_debug("Replacing existing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
  1126. fc_loopid->se_nacl = new_se_nacl;
  1127. if (qla_tgt_sess->se_sess != se_sess)
  1128. qla_tgt_sess->se_sess = se_sess;
  1129. if (nacl->qla_tgt_sess != qla_tgt_sess)
  1130. nacl->qla_tgt_sess = qla_tgt_sess;
  1131. return;
  1132. }
  1133. if (new_se_nacl == NULL) {
  1134. pr_debug("Clearing fc_loopid->se_nacl\n");
  1135. fc_loopid->se_nacl = NULL;
  1136. return;
  1137. }
  1138. pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->qla_tgt_sess\n");
  1139. fc_loopid->se_nacl = new_se_nacl;
  1140. if (qla_tgt_sess->se_sess != se_sess)
  1141. qla_tgt_sess->se_sess = se_sess;
  1142. if (nacl->qla_tgt_sess != qla_tgt_sess)
  1143. nacl->qla_tgt_sess = qla_tgt_sess;
  1144. pr_debug("Setup nacl->qla_tgt_sess %p by loop_id for se_nacl: %p, initiatorname: %s\n",
  1145. nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
  1146. }
  1147. /*
  1148. * Should always be called with qla_hw_data->hardware_lock held.
  1149. */
  1150. static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
  1151. struct tcm_qla2xxx_nacl *nacl, struct qla_tgt_sess *sess)
  1152. {
  1153. struct se_session *se_sess = sess->se_sess;
  1154. unsigned char be_sid[3];
  1155. be_sid[0] = sess->s_id.b.domain;
  1156. be_sid[1] = sess->s_id.b.area;
  1157. be_sid[2] = sess->s_id.b.al_pa;
  1158. tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
  1159. sess, be_sid);
  1160. tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
  1161. sess, sess->loop_id);
  1162. }
  1163. static void tcm_qla2xxx_free_session(struct qla_tgt_sess *sess)
  1164. {
  1165. struct qla_tgt *tgt = sess->tgt;
  1166. struct qla_hw_data *ha = tgt->ha;
  1167. struct se_session *se_sess;
  1168. struct se_node_acl *se_nacl;
  1169. struct tcm_qla2xxx_lport *lport;
  1170. struct tcm_qla2xxx_nacl *nacl;
  1171. BUG_ON(in_interrupt());
  1172. se_sess = sess->se_sess;
  1173. if (!se_sess) {
  1174. pr_err("struct qla_tgt_sess->se_sess is NULL\n");
  1175. dump_stack();
  1176. return;
  1177. }
  1178. se_nacl = se_sess->se_node_acl;
  1179. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  1180. lport = ha->tgt.target_lport_ptr;
  1181. if (!lport) {
  1182. pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
  1183. dump_stack();
  1184. return;
  1185. }
  1186. target_wait_for_sess_cmds(se_sess, 0);
  1187. transport_deregister_session_configfs(sess->se_sess);
  1188. transport_deregister_session(sess->se_sess);
  1189. }
  1190. /*
  1191. * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
  1192. * to locate struct se_node_acl
  1193. */
  1194. static int tcm_qla2xxx_check_initiator_node_acl(
  1195. scsi_qla_host_t *vha,
  1196. unsigned char *fc_wwpn,
  1197. void *qla_tgt_sess,
  1198. uint8_t *s_id,
  1199. uint16_t loop_id)
  1200. {
  1201. struct qla_hw_data *ha = vha->hw;
  1202. struct tcm_qla2xxx_lport *lport;
  1203. struct tcm_qla2xxx_tpg *tpg;
  1204. struct tcm_qla2xxx_nacl *nacl;
  1205. struct se_portal_group *se_tpg;
  1206. struct se_node_acl *se_nacl;
  1207. struct se_session *se_sess;
  1208. struct qla_tgt_sess *sess = qla_tgt_sess;
  1209. unsigned char port_name[36];
  1210. unsigned long flags;
  1211. lport = ha->tgt.target_lport_ptr;
  1212. if (!lport) {
  1213. pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
  1214. dump_stack();
  1215. return -EINVAL;
  1216. }
  1217. /*
  1218. * Locate the TPG=1 reference..
  1219. */
  1220. tpg = lport->tpg_1;
  1221. if (!tpg) {
  1222. pr_err("Unable to lcoate struct tcm_qla2xxx_lport->tpg_1\n");
  1223. return -EINVAL;
  1224. }
  1225. se_tpg = &tpg->se_tpg;
  1226. se_sess = transport_init_session();
  1227. if (IS_ERR(se_sess)) {
  1228. pr_err("Unable to initialize struct se_session\n");
  1229. return PTR_ERR(se_sess);
  1230. }
  1231. /*
  1232. * Format the FCP Initiator port_name into colon seperated values to
  1233. * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
  1234. */
  1235. memset(&port_name, 0, 36);
  1236. snprintf(port_name, 36, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
  1237. fc_wwpn[0], fc_wwpn[1], fc_wwpn[2], fc_wwpn[3], fc_wwpn[4],
  1238. fc_wwpn[5], fc_wwpn[6], fc_wwpn[7]);
  1239. /*
  1240. * Locate our struct se_node_acl either from an explict NodeACL created
  1241. * via ConfigFS, or via running in TPG demo mode.
  1242. */
  1243. se_sess->se_node_acl = core_tpg_check_initiator_node_acl(se_tpg,
  1244. port_name);
  1245. if (!se_sess->se_node_acl) {
  1246. transport_free_session(se_sess);
  1247. return -EINVAL;
  1248. }
  1249. se_nacl = se_sess->se_node_acl;
  1250. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  1251. /*
  1252. * And now setup the new se_nacl and session pointers into our HW lport
  1253. * mappings for fabric S_ID and LOOP_ID.
  1254. */
  1255. spin_lock_irqsave(&ha->hardware_lock, flags);
  1256. tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess,
  1257. qla_tgt_sess, s_id);
  1258. tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, se_sess,
  1259. qla_tgt_sess, loop_id);
  1260. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1261. /*
  1262. * Finally register the new FC Nexus with TCM
  1263. */
  1264. __transport_register_session(se_nacl->se_tpg, se_nacl, se_sess, sess);
  1265. return 0;
  1266. }
  1267. /*
  1268. * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
  1269. */
  1270. static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
  1271. .handle_cmd = tcm_qla2xxx_handle_cmd,
  1272. .handle_data = tcm_qla2xxx_handle_data,
  1273. .handle_tmr = tcm_qla2xxx_handle_tmr,
  1274. .free_cmd = tcm_qla2xxx_free_cmd,
  1275. .free_mcmd = tcm_qla2xxx_free_mcmd,
  1276. .free_session = tcm_qla2xxx_free_session,
  1277. .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
  1278. .find_sess_by_s_id = tcm_qla2xxx_find_sess_by_s_id,
  1279. .find_sess_by_loop_id = tcm_qla2xxx_find_sess_by_loop_id,
  1280. .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
  1281. .put_sess = tcm_qla2xxx_put_sess,
  1282. .shutdown_sess = tcm_qla2xxx_shutdown_sess,
  1283. };
  1284. static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
  1285. {
  1286. int rc;
  1287. rc = btree_init32(&lport->lport_fcport_map);
  1288. if (rc) {
  1289. pr_err("Unable to initialize lport->lport_fcport_map btree\n");
  1290. return rc;
  1291. }
  1292. lport->lport_loopid_map = vmalloc(sizeof(struct tcm_qla2xxx_fc_loopid) *
  1293. 65536);
  1294. if (!lport->lport_loopid_map) {
  1295. pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
  1296. sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
  1297. btree_destroy32(&lport->lport_fcport_map);
  1298. return -ENOMEM;
  1299. }
  1300. memset(lport->lport_loopid_map, 0, sizeof(struct tcm_qla2xxx_fc_loopid)
  1301. * 65536);
  1302. pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
  1303. sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
  1304. return 0;
  1305. }
  1306. static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha)
  1307. {
  1308. struct qla_hw_data *ha = vha->hw;
  1309. struct tcm_qla2xxx_lport *lport;
  1310. /*
  1311. * Setup local pointer to vha, NPIV VP pointer (if present) and
  1312. * vha->tcm_lport pointer
  1313. */
  1314. lport = (struct tcm_qla2xxx_lport *)ha->tgt.target_lport_ptr;
  1315. lport->qla_vha = vha;
  1316. return 0;
  1317. }
  1318. static struct se_wwn *tcm_qla2xxx_make_lport(
  1319. struct target_fabric_configfs *tf,
  1320. struct config_group *group,
  1321. const char *name)
  1322. {
  1323. struct tcm_qla2xxx_lport *lport;
  1324. u64 wwpn;
  1325. int ret = -ENODEV;
  1326. if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
  1327. return ERR_PTR(-EINVAL);
  1328. lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
  1329. if (!lport) {
  1330. pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
  1331. return ERR_PTR(-ENOMEM);
  1332. }
  1333. lport->lport_wwpn = wwpn;
  1334. tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
  1335. wwpn);
  1336. ret = tcm_qla2xxx_init_lport(lport);
  1337. if (ret != 0)
  1338. goto out;
  1339. ret = qlt_lport_register(&tcm_qla2xxx_template, wwpn,
  1340. tcm_qla2xxx_lport_register_cb, lport);
  1341. if (ret != 0)
  1342. goto out_lport;
  1343. return &lport->lport_wwn;
  1344. out_lport:
  1345. vfree(lport->lport_loopid_map);
  1346. btree_destroy32(&lport->lport_fcport_map);
  1347. out:
  1348. kfree(lport);
  1349. return ERR_PTR(ret);
  1350. }
  1351. static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
  1352. {
  1353. struct tcm_qla2xxx_lport *lport = container_of(wwn,
  1354. struct tcm_qla2xxx_lport, lport_wwn);
  1355. struct scsi_qla_host *vha = lport->qla_vha;
  1356. struct qla_hw_data *ha = vha->hw;
  1357. struct se_node_acl *node;
  1358. u32 key = 0;
  1359. /*
  1360. * Call into qla2x_target.c LLD logic to complete the
  1361. * shutdown of struct qla_tgt after the call to
  1362. * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
  1363. */
  1364. if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stopped)
  1365. qlt_stop_phase2(ha->tgt.qla_tgt);
  1366. qlt_lport_deregister(vha);
  1367. vfree(lport->lport_loopid_map);
  1368. btree_for_each_safe32(&lport->lport_fcport_map, key, node)
  1369. btree_remove32(&lport->lport_fcport_map, key);
  1370. btree_destroy32(&lport->lport_fcport_map);
  1371. kfree(lport);
  1372. }
  1373. static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
  1374. struct target_fabric_configfs *tf,
  1375. struct config_group *group,
  1376. const char *name)
  1377. {
  1378. struct tcm_qla2xxx_lport *lport;
  1379. u64 npiv_wwpn, npiv_wwnn;
  1380. int ret;
  1381. if (tcm_qla2xxx_npiv_parse_wwn(name, strlen(name)+1,
  1382. &npiv_wwpn, &npiv_wwnn) < 0)
  1383. return ERR_PTR(-EINVAL);
  1384. lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
  1385. if (!lport) {
  1386. pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
  1387. return ERR_PTR(-ENOMEM);
  1388. }
  1389. lport->lport_npiv_wwpn = npiv_wwpn;
  1390. lport->lport_npiv_wwnn = npiv_wwnn;
  1391. tcm_qla2xxx_npiv_format_wwn(&lport->lport_npiv_name[0],
  1392. TCM_QLA2XXX_NAMELEN, npiv_wwpn, npiv_wwnn);
  1393. /* FIXME: tcm_qla2xxx_npiv_make_lport */
  1394. ret = -ENOSYS;
  1395. if (ret != 0)
  1396. goto out;
  1397. return &lport->lport_wwn;
  1398. out:
  1399. kfree(lport);
  1400. return ERR_PTR(ret);
  1401. }
  1402. static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
  1403. {
  1404. struct tcm_qla2xxx_lport *lport = container_of(wwn,
  1405. struct tcm_qla2xxx_lport, lport_wwn);
  1406. struct scsi_qla_host *vha = lport->qla_vha;
  1407. struct Scsi_Host *sh = vha->host;
  1408. /*
  1409. * Notify libfc that we want to release the lport->npiv_vport
  1410. */
  1411. fc_vport_terminate(lport->npiv_vport);
  1412. scsi_host_put(sh);
  1413. kfree(lport);
  1414. }
  1415. static ssize_t tcm_qla2xxx_wwn_show_attr_version(
  1416. struct target_fabric_configfs *tf,
  1417. char *page)
  1418. {
  1419. return sprintf(page,
  1420. "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on "
  1421. UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
  1422. utsname()->machine);
  1423. }
  1424. TF_WWN_ATTR_RO(tcm_qla2xxx, version);
  1425. static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
  1426. &tcm_qla2xxx_wwn_version.attr,
  1427. NULL,
  1428. };
  1429. static struct target_core_fabric_ops tcm_qla2xxx_ops = {
  1430. .get_fabric_name = tcm_qla2xxx_get_fabric_name,
  1431. .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
  1432. .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
  1433. .tpg_get_tag = tcm_qla2xxx_get_tag,
  1434. .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
  1435. .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
  1436. .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
  1437. .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
  1438. .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
  1439. .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
  1440. .tpg_check_demo_mode_write_protect =
  1441. tcm_qla2xxx_check_demo_write_protect,
  1442. .tpg_check_prod_mode_write_protect =
  1443. tcm_qla2xxx_check_prod_write_protect,
  1444. .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
  1445. .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
  1446. .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
  1447. .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
  1448. .check_stop_free = tcm_qla2xxx_check_stop_free,
  1449. .release_cmd = tcm_qla2xxx_release_cmd,
  1450. .put_session = tcm_qla2xxx_put_session,
  1451. .shutdown_session = tcm_qla2xxx_shutdown_session,
  1452. .close_session = tcm_qla2xxx_close_session,
  1453. .sess_get_index = tcm_qla2xxx_sess_get_index,
  1454. .sess_get_initiator_sid = NULL,
  1455. .write_pending = tcm_qla2xxx_write_pending,
  1456. .write_pending_status = tcm_qla2xxx_write_pending_status,
  1457. .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
  1458. .get_task_tag = tcm_qla2xxx_get_task_tag,
  1459. .get_cmd_state = tcm_qla2xxx_get_cmd_state,
  1460. .queue_data_in = tcm_qla2xxx_queue_data_in,
  1461. .queue_status = tcm_qla2xxx_queue_status,
  1462. .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
  1463. .get_fabric_sense_len = tcm_qla2xxx_get_fabric_sense_len,
  1464. .set_fabric_sense_len = tcm_qla2xxx_set_fabric_sense_len,
  1465. /*
  1466. * Setup function pointers for generic logic in
  1467. * target_core_fabric_configfs.c
  1468. */
  1469. .fabric_make_wwn = tcm_qla2xxx_make_lport,
  1470. .fabric_drop_wwn = tcm_qla2xxx_drop_lport,
  1471. .fabric_make_tpg = tcm_qla2xxx_make_tpg,
  1472. .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
  1473. .fabric_post_link = NULL,
  1474. .fabric_pre_unlink = NULL,
  1475. .fabric_make_np = NULL,
  1476. .fabric_drop_np = NULL,
  1477. .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
  1478. .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
  1479. };
  1480. static struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
  1481. .get_fabric_name = tcm_qla2xxx_npiv_get_fabric_name,
  1482. .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
  1483. .tpg_get_wwn = tcm_qla2xxx_npiv_get_fabric_wwn,
  1484. .tpg_get_tag = tcm_qla2xxx_get_tag,
  1485. .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
  1486. .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
  1487. .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
  1488. .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
  1489. .tpg_check_demo_mode = tcm_qla2xxx_check_false,
  1490. .tpg_check_demo_mode_cache = tcm_qla2xxx_check_true,
  1491. .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_true,
  1492. .tpg_check_prod_mode_write_protect = tcm_qla2xxx_check_false,
  1493. .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
  1494. .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
  1495. .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
  1496. .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
  1497. .release_cmd = tcm_qla2xxx_release_cmd,
  1498. .put_session = tcm_qla2xxx_put_session,
  1499. .shutdown_session = tcm_qla2xxx_shutdown_session,
  1500. .close_session = tcm_qla2xxx_close_session,
  1501. .sess_get_index = tcm_qla2xxx_sess_get_index,
  1502. .sess_get_initiator_sid = NULL,
  1503. .write_pending = tcm_qla2xxx_write_pending,
  1504. .write_pending_status = tcm_qla2xxx_write_pending_status,
  1505. .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
  1506. .get_task_tag = tcm_qla2xxx_get_task_tag,
  1507. .get_cmd_state = tcm_qla2xxx_get_cmd_state,
  1508. .queue_data_in = tcm_qla2xxx_queue_data_in,
  1509. .queue_status = tcm_qla2xxx_queue_status,
  1510. .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
  1511. .get_fabric_sense_len = tcm_qla2xxx_get_fabric_sense_len,
  1512. .set_fabric_sense_len = tcm_qla2xxx_set_fabric_sense_len,
  1513. /*
  1514. * Setup function pointers for generic logic in
  1515. * target_core_fabric_configfs.c
  1516. */
  1517. .fabric_make_wwn = tcm_qla2xxx_npiv_make_lport,
  1518. .fabric_drop_wwn = tcm_qla2xxx_npiv_drop_lport,
  1519. .fabric_make_tpg = tcm_qla2xxx_npiv_make_tpg,
  1520. .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
  1521. .fabric_post_link = NULL,
  1522. .fabric_pre_unlink = NULL,
  1523. .fabric_make_np = NULL,
  1524. .fabric_drop_np = NULL,
  1525. .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
  1526. .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
  1527. };
  1528. static int tcm_qla2xxx_register_configfs(void)
  1529. {
  1530. struct target_fabric_configfs *fabric, *npiv_fabric;
  1531. int ret;
  1532. pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on "
  1533. UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
  1534. utsname()->machine);
  1535. /*
  1536. * Register the top level struct config_item_type with TCM core
  1537. */
  1538. fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx");
  1539. if (IS_ERR(fabric)) {
  1540. pr_err("target_fabric_configfs_init() failed\n");
  1541. return PTR_ERR(fabric);
  1542. }
  1543. /*
  1544. * Setup fabric->tf_ops from our local tcm_qla2xxx_ops
  1545. */
  1546. fabric->tf_ops = tcm_qla2xxx_ops;
  1547. /*
  1548. * Setup default attribute lists for various fabric->tf_cit_tmpl
  1549. */
  1550. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
  1551. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_qla2xxx_tpg_attrs;
  1552. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs =
  1553. tcm_qla2xxx_tpg_attrib_attrs;
  1554. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1555. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1556. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  1557. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  1558. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  1559. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  1560. /*
  1561. * Register the fabric for use within TCM
  1562. */
  1563. ret = target_fabric_configfs_register(fabric);
  1564. if (ret < 0) {
  1565. pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
  1566. return ret;
  1567. }
  1568. /*
  1569. * Setup our local pointer to *fabric
  1570. */
  1571. tcm_qla2xxx_fabric_configfs = fabric;
  1572. pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_fabric_configfs\n");
  1573. /*
  1574. * Register the top level struct config_item_type for NPIV with TCM core
  1575. */
  1576. npiv_fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx_npiv");
  1577. if (IS_ERR(npiv_fabric)) {
  1578. pr_err("target_fabric_configfs_init() failed\n");
  1579. ret = PTR_ERR(npiv_fabric);
  1580. goto out_fabric;
  1581. }
  1582. /*
  1583. * Setup fabric->tf_ops from our local tcm_qla2xxx_npiv_ops
  1584. */
  1585. npiv_fabric->tf_ops = tcm_qla2xxx_npiv_ops;
  1586. /*
  1587. * Setup default attribute lists for various npiv_fabric->tf_cit_tmpl
  1588. */
  1589. TF_CIT_TMPL(npiv_fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
  1590. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_base_cit.ct_attrs = NULL;
  1591. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
  1592. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1593. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1594. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  1595. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  1596. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  1597. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  1598. /*
  1599. * Register the npiv_fabric for use within TCM
  1600. */
  1601. ret = target_fabric_configfs_register(npiv_fabric);
  1602. if (ret < 0) {
  1603. pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
  1604. goto out_fabric;
  1605. }
  1606. /*
  1607. * Setup our local pointer to *npiv_fabric
  1608. */
  1609. tcm_qla2xxx_npiv_fabric_configfs = npiv_fabric;
  1610. pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_npiv_fabric_configfs\n");
  1611. tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
  1612. WQ_MEM_RECLAIM, 0);
  1613. if (!tcm_qla2xxx_free_wq) {
  1614. ret = -ENOMEM;
  1615. goto out_fabric_npiv;
  1616. }
  1617. tcm_qla2xxx_cmd_wq = alloc_workqueue("tcm_qla2xxx_cmd", 0, 0);
  1618. if (!tcm_qla2xxx_cmd_wq) {
  1619. ret = -ENOMEM;
  1620. goto out_free_wq;
  1621. }
  1622. return 0;
  1623. out_free_wq:
  1624. destroy_workqueue(tcm_qla2xxx_free_wq);
  1625. out_fabric_npiv:
  1626. target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
  1627. out_fabric:
  1628. target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
  1629. return ret;
  1630. }
  1631. static void tcm_qla2xxx_deregister_configfs(void)
  1632. {
  1633. destroy_workqueue(tcm_qla2xxx_cmd_wq);
  1634. destroy_workqueue(tcm_qla2xxx_free_wq);
  1635. target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
  1636. tcm_qla2xxx_fabric_configfs = NULL;
  1637. pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_fabric_configfs\n");
  1638. target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
  1639. tcm_qla2xxx_npiv_fabric_configfs = NULL;
  1640. pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_npiv_fabric_configfs\n");
  1641. }
  1642. static int __init tcm_qla2xxx_init(void)
  1643. {
  1644. int ret;
  1645. ret = tcm_qla2xxx_register_configfs();
  1646. if (ret < 0)
  1647. return ret;
  1648. return 0;
  1649. }
  1650. static void __exit tcm_qla2xxx_exit(void)
  1651. {
  1652. tcm_qla2xxx_deregister_configfs();
  1653. }
  1654. MODULE_DESCRIPTION("TCM QLA2XXX series NPIV enabled fabric driver");
  1655. MODULE_LICENSE("GPL");
  1656. module_init(tcm_qla2xxx_init);
  1657. module_exit(tcm_qla2xxx_exit);