tcm_qla2xxx.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  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_naa_name;
  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 allocate 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. target_sess_cmd_list_set_waiting(se_sess);
  396. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  397. return 1;
  398. }
  399. static void tcm_qla2xxx_close_session(struct se_session *se_sess)
  400. {
  401. struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
  402. struct scsi_qla_host *vha;
  403. unsigned long flags;
  404. BUG_ON(!sess);
  405. vha = sess->vha;
  406. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  407. qlt_unreg_sess(sess);
  408. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  409. }
  410. static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
  411. {
  412. return 0;
  413. }
  414. /*
  415. * The LIO target core uses DMA_TO_DEVICE to mean that data is going
  416. * to the target (eg handling a WRITE) and DMA_FROM_DEVICE to mean
  417. * that data is coming from the target (eg handling a READ). However,
  418. * this is just the opposite of what we have to tell the DMA mapping
  419. * layer -- eg when handling a READ, the HBA will have to DMA the data
  420. * out of memory so it can send it to the initiator, which means we
  421. * need to use DMA_TO_DEVICE when we map the data.
  422. */
  423. static enum dma_data_direction tcm_qla2xxx_mapping_dir(struct se_cmd *se_cmd)
  424. {
  425. if (se_cmd->se_cmd_flags & SCF_BIDI)
  426. return DMA_BIDIRECTIONAL;
  427. switch (se_cmd->data_direction) {
  428. case DMA_TO_DEVICE:
  429. return DMA_FROM_DEVICE;
  430. case DMA_FROM_DEVICE:
  431. return DMA_TO_DEVICE;
  432. case DMA_NONE:
  433. default:
  434. return DMA_NONE;
  435. }
  436. }
  437. static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
  438. {
  439. struct qla_tgt_cmd *cmd = container_of(se_cmd,
  440. struct qla_tgt_cmd, se_cmd);
  441. cmd->bufflen = se_cmd->data_length;
  442. cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
  443. cmd->sg_cnt = se_cmd->t_data_nents;
  444. cmd->sg = se_cmd->t_data_sg;
  445. /*
  446. * qla_target.c:qlt_rdy_to_xfer() will call pci_map_sg() to setup
  447. * the SGL mappings into PCIe memory for incoming FCP WRITE data.
  448. */
  449. return qlt_rdy_to_xfer(cmd);
  450. }
  451. static int tcm_qla2xxx_write_pending_status(struct se_cmd *se_cmd)
  452. {
  453. unsigned long flags;
  454. /*
  455. * Check for WRITE_PENDING status to determine if we need to wait for
  456. * CTIO aborts to be posted via hardware in tcm_qla2xxx_handle_data().
  457. */
  458. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  459. if (se_cmd->t_state == TRANSPORT_WRITE_PENDING ||
  460. se_cmd->t_state == TRANSPORT_COMPLETE_QF_WP) {
  461. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  462. wait_for_completion_timeout(&se_cmd->t_transport_stop_comp,
  463. 3000);
  464. return 0;
  465. }
  466. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  467. return 0;
  468. }
  469. static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
  470. {
  471. return;
  472. }
  473. static u32 tcm_qla2xxx_get_task_tag(struct se_cmd *se_cmd)
  474. {
  475. struct qla_tgt_cmd *cmd = container_of(se_cmd,
  476. struct qla_tgt_cmd, se_cmd);
  477. return cmd->tag;
  478. }
  479. static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
  480. {
  481. return 0;
  482. }
  483. /*
  484. * Called from process context in qla_target.c:qlt_do_work() code
  485. */
  486. static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
  487. unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
  488. int data_dir, int bidi)
  489. {
  490. struct se_cmd *se_cmd = &cmd->se_cmd;
  491. struct se_session *se_sess;
  492. struct qla_tgt_sess *sess;
  493. int flags = TARGET_SCF_ACK_KREF;
  494. if (bidi)
  495. flags |= TARGET_SCF_BIDI_OP;
  496. sess = cmd->sess;
  497. if (!sess) {
  498. pr_err("Unable to locate struct qla_tgt_sess from qla_tgt_cmd\n");
  499. return -EINVAL;
  500. }
  501. se_sess = sess->se_sess;
  502. if (!se_sess) {
  503. pr_err("Unable to locate active struct se_session\n");
  504. return -EINVAL;
  505. }
  506. return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
  507. cmd->unpacked_lun, data_length, fcp_task_attr,
  508. data_dir, flags);
  509. }
  510. static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
  511. {
  512. struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
  513. /*
  514. * Ensure that the complete FCP WRITE payload has been received.
  515. * Otherwise return an exception via CHECK_CONDITION status.
  516. */
  517. if (!cmd->write_data_transferred) {
  518. /*
  519. * Check if se_cmd has already been aborted via LUN_RESET, and
  520. * waiting upon completion in tcm_qla2xxx_write_pending_status()
  521. */
  522. if (cmd->se_cmd.transport_state & CMD_T_ABORTED) {
  523. complete(&cmd->se_cmd.t_transport_stop_comp);
  524. return;
  525. }
  526. transport_generic_request_failure(&cmd->se_cmd,
  527. TCM_CHECK_CONDITION_ABORT_CMD);
  528. return;
  529. }
  530. return target_execute_cmd(&cmd->se_cmd);
  531. }
  532. /*
  533. * Called from qla_target.c:qlt_do_ctio_completion()
  534. */
  535. static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
  536. {
  537. INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
  538. queue_work(tcm_qla2xxx_free_wq, &cmd->work);
  539. }
  540. /*
  541. * Called from qla_target.c:qlt_issue_task_mgmt()
  542. */
  543. static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, uint32_t lun,
  544. uint8_t tmr_func, uint32_t tag)
  545. {
  546. struct qla_tgt_sess *sess = mcmd->sess;
  547. struct se_cmd *se_cmd = &mcmd->se_cmd;
  548. return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
  549. tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF);
  550. }
  551. static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
  552. {
  553. struct qla_tgt_cmd *cmd = container_of(se_cmd,
  554. struct qla_tgt_cmd, se_cmd);
  555. cmd->bufflen = se_cmd->data_length;
  556. cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
  557. cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
  558. cmd->sg_cnt = se_cmd->t_data_nents;
  559. cmd->sg = se_cmd->t_data_sg;
  560. cmd->offset = 0;
  561. /*
  562. * Now queue completed DATA_IN the qla2xxx LLD and response ring
  563. */
  564. return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
  565. se_cmd->scsi_status);
  566. }
  567. static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
  568. {
  569. struct qla_tgt_cmd *cmd = container_of(se_cmd,
  570. struct qla_tgt_cmd, se_cmd);
  571. int xmit_type = QLA_TGT_XMIT_STATUS;
  572. cmd->bufflen = se_cmd->data_length;
  573. cmd->sg = NULL;
  574. cmd->sg_cnt = 0;
  575. cmd->offset = 0;
  576. cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
  577. cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
  578. if (se_cmd->data_direction == DMA_FROM_DEVICE) {
  579. /*
  580. * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
  581. * for qla_tgt_xmit_response LLD code
  582. */
  583. se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  584. se_cmd->residual_count = se_cmd->data_length;
  585. cmd->bufflen = 0;
  586. }
  587. /*
  588. * Now queue status response to qla2xxx LLD code and response ring
  589. */
  590. return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
  591. }
  592. static int tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
  593. {
  594. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  595. struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
  596. struct qla_tgt_mgmt_cmd, se_cmd);
  597. pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
  598. mcmd, se_tmr->function, se_tmr->response);
  599. /*
  600. * Do translation between TCM TM response codes and
  601. * QLA2xxx FC TM response codes.
  602. */
  603. switch (se_tmr->response) {
  604. case TMR_FUNCTION_COMPLETE:
  605. mcmd->fc_tm_rsp = FC_TM_SUCCESS;
  606. break;
  607. case TMR_TASK_DOES_NOT_EXIST:
  608. mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
  609. break;
  610. case TMR_FUNCTION_REJECTED:
  611. mcmd->fc_tm_rsp = FC_TM_REJECT;
  612. break;
  613. case TMR_LUN_DOES_NOT_EXIST:
  614. default:
  615. mcmd->fc_tm_rsp = FC_TM_FAILED;
  616. break;
  617. }
  618. /*
  619. * Queue the TM response to QLA2xxx LLD to build a
  620. * CTIO response packet.
  621. */
  622. qlt_xmit_tm_rsp(mcmd);
  623. return 0;
  624. }
  625. /* Local pointer to allocated TCM configfs fabric module */
  626. struct target_fabric_configfs *tcm_qla2xxx_fabric_configfs;
  627. struct target_fabric_configfs *tcm_qla2xxx_npiv_fabric_configfs;
  628. static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
  629. struct tcm_qla2xxx_nacl *, struct qla_tgt_sess *);
  630. /*
  631. * Expected to be called with struct qla_hw_data->hardware_lock held
  632. */
  633. static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct qla_tgt_sess *sess)
  634. {
  635. struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
  636. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  637. struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
  638. struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
  639. struct tcm_qla2xxx_lport, lport_wwn);
  640. struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
  641. struct tcm_qla2xxx_nacl, se_node_acl);
  642. void *node;
  643. pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
  644. node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
  645. WARN_ON(node && (node != se_nacl));
  646. pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
  647. se_nacl, nacl->nport_wwnn, nacl->nport_id);
  648. /*
  649. * Now clear the se_nacl and session pointers from our HW lport lookup
  650. * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
  651. *
  652. * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
  653. * target_wait_for_sess_cmds() before the session waits for outstanding
  654. * I/O to complete, to avoid a race between session shutdown execution
  655. * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
  656. */
  657. tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
  658. }
  659. static void tcm_qla2xxx_release_session(struct kref *kref)
  660. {
  661. struct se_session *se_sess = container_of(kref,
  662. struct se_session, sess_kref);
  663. qlt_unreg_sess(se_sess->fabric_sess_ptr);
  664. }
  665. static void tcm_qla2xxx_put_session(struct se_session *se_sess)
  666. {
  667. struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
  668. struct qla_hw_data *ha = sess->vha->hw;
  669. unsigned long flags;
  670. spin_lock_irqsave(&ha->hardware_lock, flags);
  671. kref_put(&se_sess->sess_kref, tcm_qla2xxx_release_session);
  672. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  673. }
  674. static void tcm_qla2xxx_put_sess(struct qla_tgt_sess *sess)
  675. {
  676. assert_spin_locked(&sess->vha->hw->hardware_lock);
  677. kref_put(&sess->se_sess->sess_kref, tcm_qla2xxx_release_session);
  678. }
  679. static void tcm_qla2xxx_shutdown_sess(struct qla_tgt_sess *sess)
  680. {
  681. assert_spin_locked(&sess->vha->hw->hardware_lock);
  682. target_sess_cmd_list_set_waiting(sess->se_sess);
  683. }
  684. static struct se_node_acl *tcm_qla2xxx_make_nodeacl(
  685. struct se_portal_group *se_tpg,
  686. struct config_group *group,
  687. const char *name)
  688. {
  689. struct se_node_acl *se_nacl, *se_nacl_new;
  690. struct tcm_qla2xxx_nacl *nacl;
  691. u64 wwnn;
  692. u32 qla2xxx_nexus_depth;
  693. if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
  694. return ERR_PTR(-EINVAL);
  695. se_nacl_new = tcm_qla2xxx_alloc_fabric_acl(se_tpg);
  696. if (!se_nacl_new)
  697. return ERR_PTR(-ENOMEM);
  698. /* #warning FIXME: Hardcoded qla2xxx_nexus depth in tcm_qla2xxx_make_nodeacl */
  699. qla2xxx_nexus_depth = 1;
  700. /*
  701. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  702. * when converting a NodeACL from demo mode -> explict
  703. */
  704. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  705. name, qla2xxx_nexus_depth);
  706. if (IS_ERR(se_nacl)) {
  707. tcm_qla2xxx_release_fabric_acl(se_tpg, se_nacl_new);
  708. return se_nacl;
  709. }
  710. /*
  711. * Locate our struct tcm_qla2xxx_nacl and set the FC Nport WWPN
  712. */
  713. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  714. nacl->nport_wwnn = wwnn;
  715. tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
  716. return se_nacl;
  717. }
  718. static void tcm_qla2xxx_drop_nodeacl(struct se_node_acl *se_acl)
  719. {
  720. struct se_portal_group *se_tpg = se_acl->se_tpg;
  721. struct tcm_qla2xxx_nacl *nacl = container_of(se_acl,
  722. struct tcm_qla2xxx_nacl, se_node_acl);
  723. core_tpg_del_initiator_node_acl(se_tpg, se_acl, 1);
  724. kfree(nacl);
  725. }
  726. /* Start items for tcm_qla2xxx_tpg_attrib_cit */
  727. #define DEF_QLA_TPG_ATTRIB(name) \
  728. \
  729. static ssize_t tcm_qla2xxx_tpg_attrib_show_##name( \
  730. struct se_portal_group *se_tpg, \
  731. char *page) \
  732. { \
  733. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
  734. struct tcm_qla2xxx_tpg, se_tpg); \
  735. \
  736. return sprintf(page, "%u\n", QLA_TPG_ATTRIB(tpg)->name); \
  737. } \
  738. \
  739. static ssize_t tcm_qla2xxx_tpg_attrib_store_##name( \
  740. struct se_portal_group *se_tpg, \
  741. const char *page, \
  742. size_t count) \
  743. { \
  744. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
  745. struct tcm_qla2xxx_tpg, se_tpg); \
  746. unsigned long val; \
  747. int ret; \
  748. \
  749. ret = kstrtoul(page, 0, &val); \
  750. if (ret < 0) { \
  751. pr_err("kstrtoul() failed with" \
  752. " ret: %d\n", ret); \
  753. return -EINVAL; \
  754. } \
  755. ret = tcm_qla2xxx_set_attrib_##name(tpg, val); \
  756. \
  757. return (!ret) ? count : -EINVAL; \
  758. }
  759. #define DEF_QLA_TPG_ATTR_BOOL(_name) \
  760. \
  761. static int tcm_qla2xxx_set_attrib_##_name( \
  762. struct tcm_qla2xxx_tpg *tpg, \
  763. unsigned long val) \
  764. { \
  765. struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib; \
  766. \
  767. if ((val != 0) && (val != 1)) { \
  768. pr_err("Illegal boolean value %lu\n", val); \
  769. return -EINVAL; \
  770. } \
  771. \
  772. a->_name = val; \
  773. return 0; \
  774. }
  775. #define QLA_TPG_ATTR(_name, _mode) \
  776. TF_TPG_ATTRIB_ATTR(tcm_qla2xxx, _name, _mode);
  777. /*
  778. * Define tcm_qla2xxx_tpg_attrib_s_generate_node_acls
  779. */
  780. DEF_QLA_TPG_ATTR_BOOL(generate_node_acls);
  781. DEF_QLA_TPG_ATTRIB(generate_node_acls);
  782. QLA_TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
  783. /*
  784. Define tcm_qla2xxx_attrib_s_cache_dynamic_acls
  785. */
  786. DEF_QLA_TPG_ATTR_BOOL(cache_dynamic_acls);
  787. DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
  788. QLA_TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
  789. /*
  790. * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_write_protect
  791. */
  792. DEF_QLA_TPG_ATTR_BOOL(demo_mode_write_protect);
  793. DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
  794. QLA_TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
  795. /*
  796. * Define tcm_qla2xxx_tpg_attrib_s_prod_mode_write_protect
  797. */
  798. DEF_QLA_TPG_ATTR_BOOL(prod_mode_write_protect);
  799. DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
  800. QLA_TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
  801. static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
  802. &tcm_qla2xxx_tpg_attrib_generate_node_acls.attr,
  803. &tcm_qla2xxx_tpg_attrib_cache_dynamic_acls.attr,
  804. &tcm_qla2xxx_tpg_attrib_demo_mode_write_protect.attr,
  805. &tcm_qla2xxx_tpg_attrib_prod_mode_write_protect.attr,
  806. NULL,
  807. };
  808. /* End items for tcm_qla2xxx_tpg_attrib_cit */
  809. static ssize_t tcm_qla2xxx_tpg_show_enable(
  810. struct se_portal_group *se_tpg,
  811. char *page)
  812. {
  813. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  814. struct tcm_qla2xxx_tpg, se_tpg);
  815. return snprintf(page, PAGE_SIZE, "%d\n",
  816. atomic_read(&tpg->lport_tpg_enabled));
  817. }
  818. static ssize_t tcm_qla2xxx_tpg_store_enable(
  819. struct se_portal_group *se_tpg,
  820. const char *page,
  821. size_t count)
  822. {
  823. struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
  824. struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
  825. struct tcm_qla2xxx_lport, lport_wwn);
  826. struct scsi_qla_host *vha = lport->qla_vha;
  827. struct qla_hw_data *ha = vha->hw;
  828. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  829. struct tcm_qla2xxx_tpg, se_tpg);
  830. unsigned long op;
  831. int rc;
  832. rc = kstrtoul(page, 0, &op);
  833. if (rc < 0) {
  834. pr_err("kstrtoul() returned %d\n", rc);
  835. return -EINVAL;
  836. }
  837. if ((op != 1) && (op != 0)) {
  838. pr_err("Illegal value for tpg_enable: %lu\n", op);
  839. return -EINVAL;
  840. }
  841. if (op) {
  842. atomic_set(&tpg->lport_tpg_enabled, 1);
  843. qlt_enable_vha(vha);
  844. } else {
  845. if (!ha->tgt.qla_tgt) {
  846. pr_err("truct qla_hw_data *ha->tgt.qla_tgt is NULL\n");
  847. return -ENODEV;
  848. }
  849. atomic_set(&tpg->lport_tpg_enabled, 0);
  850. qlt_stop_phase1(ha->tgt.qla_tgt);
  851. }
  852. return count;
  853. }
  854. TF_TPG_BASE_ATTR(tcm_qla2xxx, enable, S_IRUGO | S_IWUSR);
  855. static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
  856. &tcm_qla2xxx_tpg_enable.attr,
  857. NULL,
  858. };
  859. static struct se_portal_group *tcm_qla2xxx_make_tpg(
  860. struct se_wwn *wwn,
  861. struct config_group *group,
  862. const char *name)
  863. {
  864. struct tcm_qla2xxx_lport *lport = container_of(wwn,
  865. struct tcm_qla2xxx_lport, lport_wwn);
  866. struct tcm_qla2xxx_tpg *tpg;
  867. unsigned long tpgt;
  868. int ret;
  869. if (strstr(name, "tpgt_") != name)
  870. return ERR_PTR(-EINVAL);
  871. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
  872. return ERR_PTR(-EINVAL);
  873. if (!lport->qla_npiv_vp && (tpgt != 1)) {
  874. pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
  875. return ERR_PTR(-ENOSYS);
  876. }
  877. tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
  878. if (!tpg) {
  879. pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
  880. return ERR_PTR(-ENOMEM);
  881. }
  882. tpg->lport = lport;
  883. tpg->lport_tpgt = tpgt;
  884. /*
  885. * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
  886. * NodeACLs
  887. */
  888. QLA_TPG_ATTRIB(tpg)->generate_node_acls = 1;
  889. QLA_TPG_ATTRIB(tpg)->demo_mode_write_protect = 1;
  890. QLA_TPG_ATTRIB(tpg)->cache_dynamic_acls = 1;
  891. ret = core_tpg_register(&tcm_qla2xxx_fabric_configfs->tf_ops, wwn,
  892. &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
  893. if (ret < 0) {
  894. kfree(tpg);
  895. return NULL;
  896. }
  897. /*
  898. * Setup local TPG=1 pointer for non NPIV mode.
  899. */
  900. if (lport->qla_npiv_vp == NULL)
  901. lport->tpg_1 = tpg;
  902. return &tpg->se_tpg;
  903. }
  904. static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
  905. {
  906. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  907. struct tcm_qla2xxx_tpg, se_tpg);
  908. struct tcm_qla2xxx_lport *lport = tpg->lport;
  909. struct scsi_qla_host *vha = lport->qla_vha;
  910. struct qla_hw_data *ha = vha->hw;
  911. /*
  912. * Call into qla2x_target.c LLD logic to shutdown the active
  913. * FC Nexuses and disable target mode operation for this qla_hw_data
  914. */
  915. if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stop)
  916. qlt_stop_phase1(ha->tgt.qla_tgt);
  917. core_tpg_deregister(se_tpg);
  918. /*
  919. * Clear local TPG=1 pointer for non NPIV mode.
  920. */
  921. if (lport->qla_npiv_vp == NULL)
  922. lport->tpg_1 = NULL;
  923. kfree(tpg);
  924. }
  925. static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(
  926. struct se_wwn *wwn,
  927. struct config_group *group,
  928. const char *name)
  929. {
  930. struct tcm_qla2xxx_lport *lport = container_of(wwn,
  931. struct tcm_qla2xxx_lport, lport_wwn);
  932. struct tcm_qla2xxx_tpg *tpg;
  933. unsigned long tpgt;
  934. int ret;
  935. if (strstr(name, "tpgt_") != name)
  936. return ERR_PTR(-EINVAL);
  937. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
  938. return ERR_PTR(-EINVAL);
  939. tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
  940. if (!tpg) {
  941. pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
  942. return ERR_PTR(-ENOMEM);
  943. }
  944. tpg->lport = lport;
  945. tpg->lport_tpgt = tpgt;
  946. ret = core_tpg_register(&tcm_qla2xxx_npiv_fabric_configfs->tf_ops, wwn,
  947. &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
  948. if (ret < 0) {
  949. kfree(tpg);
  950. return NULL;
  951. }
  952. return &tpg->se_tpg;
  953. }
  954. /*
  955. * Expected to be called with struct qla_hw_data->hardware_lock held
  956. */
  957. static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_s_id(
  958. scsi_qla_host_t *vha,
  959. const uint8_t *s_id)
  960. {
  961. struct qla_hw_data *ha = vha->hw;
  962. struct tcm_qla2xxx_lport *lport;
  963. struct se_node_acl *se_nacl;
  964. struct tcm_qla2xxx_nacl *nacl;
  965. u32 key;
  966. lport = ha->tgt.target_lport_ptr;
  967. if (!lport) {
  968. pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
  969. dump_stack();
  970. return NULL;
  971. }
  972. key = (((unsigned long)s_id[0] << 16) |
  973. ((unsigned long)s_id[1] << 8) |
  974. (unsigned long)s_id[2]);
  975. pr_debug("find_sess_by_s_id: 0x%06x\n", key);
  976. se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
  977. if (!se_nacl) {
  978. pr_debug("Unable to locate s_id: 0x%06x\n", key);
  979. return NULL;
  980. }
  981. pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
  982. se_nacl, se_nacl->initiatorname);
  983. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  984. if (!nacl->qla_tgt_sess) {
  985. pr_err("Unable to locate struct qla_tgt_sess\n");
  986. return NULL;
  987. }
  988. return nacl->qla_tgt_sess;
  989. }
  990. /*
  991. * Expected to be called with struct qla_hw_data->hardware_lock held
  992. */
  993. static void tcm_qla2xxx_set_sess_by_s_id(
  994. struct tcm_qla2xxx_lport *lport,
  995. struct se_node_acl *new_se_nacl,
  996. struct tcm_qla2xxx_nacl *nacl,
  997. struct se_session *se_sess,
  998. struct qla_tgt_sess *qla_tgt_sess,
  999. uint8_t *s_id)
  1000. {
  1001. u32 key;
  1002. void *slot;
  1003. int rc;
  1004. key = (((unsigned long)s_id[0] << 16) |
  1005. ((unsigned long)s_id[1] << 8) |
  1006. (unsigned long)s_id[2]);
  1007. pr_debug("set_sess_by_s_id: %06x\n", key);
  1008. slot = btree_lookup32(&lport->lport_fcport_map, key);
  1009. if (!slot) {
  1010. if (new_se_nacl) {
  1011. pr_debug("Setting up new fc_port entry to new_se_nacl\n");
  1012. nacl->nport_id = key;
  1013. rc = btree_insert32(&lport->lport_fcport_map, key,
  1014. new_se_nacl, GFP_ATOMIC);
  1015. if (rc)
  1016. printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
  1017. (int)key);
  1018. } else {
  1019. pr_debug("Wiping nonexisting fc_port entry\n");
  1020. }
  1021. qla_tgt_sess->se_sess = se_sess;
  1022. nacl->qla_tgt_sess = qla_tgt_sess;
  1023. return;
  1024. }
  1025. if (nacl->qla_tgt_sess) {
  1026. if (new_se_nacl == NULL) {
  1027. pr_debug("Clearing existing nacl->qla_tgt_sess and fc_port entry\n");
  1028. btree_remove32(&lport->lport_fcport_map, key);
  1029. nacl->qla_tgt_sess = NULL;
  1030. return;
  1031. }
  1032. pr_debug("Replacing existing nacl->qla_tgt_sess and fc_port entry\n");
  1033. btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
  1034. qla_tgt_sess->se_sess = se_sess;
  1035. nacl->qla_tgt_sess = qla_tgt_sess;
  1036. return;
  1037. }
  1038. if (new_se_nacl == NULL) {
  1039. pr_debug("Clearing existing fc_port entry\n");
  1040. btree_remove32(&lport->lport_fcport_map, key);
  1041. return;
  1042. }
  1043. pr_debug("Replacing existing fc_port entry w/o active nacl->qla_tgt_sess\n");
  1044. btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
  1045. qla_tgt_sess->se_sess = se_sess;
  1046. nacl->qla_tgt_sess = qla_tgt_sess;
  1047. pr_debug("Setup nacl->qla_tgt_sess %p by s_id for se_nacl: %p, initiatorname: %s\n",
  1048. nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
  1049. }
  1050. /*
  1051. * Expected to be called with struct qla_hw_data->hardware_lock held
  1052. */
  1053. static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_loop_id(
  1054. scsi_qla_host_t *vha,
  1055. const uint16_t loop_id)
  1056. {
  1057. struct qla_hw_data *ha = vha->hw;
  1058. struct tcm_qla2xxx_lport *lport;
  1059. struct se_node_acl *se_nacl;
  1060. struct tcm_qla2xxx_nacl *nacl;
  1061. struct tcm_qla2xxx_fc_loopid *fc_loopid;
  1062. lport = ha->tgt.target_lport_ptr;
  1063. if (!lport) {
  1064. pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
  1065. dump_stack();
  1066. return NULL;
  1067. }
  1068. pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
  1069. fc_loopid = lport->lport_loopid_map + loop_id;
  1070. se_nacl = fc_loopid->se_nacl;
  1071. if (!se_nacl) {
  1072. pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
  1073. loop_id);
  1074. return NULL;
  1075. }
  1076. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  1077. if (!nacl->qla_tgt_sess) {
  1078. pr_err("Unable to locate struct qla_tgt_sess\n");
  1079. return NULL;
  1080. }
  1081. return nacl->qla_tgt_sess;
  1082. }
  1083. /*
  1084. * Expected to be called with struct qla_hw_data->hardware_lock held
  1085. */
  1086. static void tcm_qla2xxx_set_sess_by_loop_id(
  1087. struct tcm_qla2xxx_lport *lport,
  1088. struct se_node_acl *new_se_nacl,
  1089. struct tcm_qla2xxx_nacl *nacl,
  1090. struct se_session *se_sess,
  1091. struct qla_tgt_sess *qla_tgt_sess,
  1092. uint16_t loop_id)
  1093. {
  1094. struct se_node_acl *saved_nacl;
  1095. struct tcm_qla2xxx_fc_loopid *fc_loopid;
  1096. pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
  1097. fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
  1098. lport->lport_loopid_map)[loop_id];
  1099. saved_nacl = fc_loopid->se_nacl;
  1100. if (!saved_nacl) {
  1101. pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
  1102. fc_loopid->se_nacl = new_se_nacl;
  1103. if (qla_tgt_sess->se_sess != se_sess)
  1104. qla_tgt_sess->se_sess = se_sess;
  1105. if (nacl->qla_tgt_sess != qla_tgt_sess)
  1106. nacl->qla_tgt_sess = qla_tgt_sess;
  1107. return;
  1108. }
  1109. if (nacl->qla_tgt_sess) {
  1110. if (new_se_nacl == NULL) {
  1111. pr_debug("Clearing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
  1112. fc_loopid->se_nacl = NULL;
  1113. nacl->qla_tgt_sess = NULL;
  1114. return;
  1115. }
  1116. pr_debug("Replacing existing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
  1117. fc_loopid->se_nacl = new_se_nacl;
  1118. if (qla_tgt_sess->se_sess != se_sess)
  1119. qla_tgt_sess->se_sess = se_sess;
  1120. if (nacl->qla_tgt_sess != qla_tgt_sess)
  1121. nacl->qla_tgt_sess = qla_tgt_sess;
  1122. return;
  1123. }
  1124. if (new_se_nacl == NULL) {
  1125. pr_debug("Clearing fc_loopid->se_nacl\n");
  1126. fc_loopid->se_nacl = NULL;
  1127. return;
  1128. }
  1129. pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->qla_tgt_sess\n");
  1130. fc_loopid->se_nacl = new_se_nacl;
  1131. if (qla_tgt_sess->se_sess != se_sess)
  1132. qla_tgt_sess->se_sess = se_sess;
  1133. if (nacl->qla_tgt_sess != qla_tgt_sess)
  1134. nacl->qla_tgt_sess = qla_tgt_sess;
  1135. pr_debug("Setup nacl->qla_tgt_sess %p by loop_id for se_nacl: %p, initiatorname: %s\n",
  1136. nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
  1137. }
  1138. /*
  1139. * Should always be called with qla_hw_data->hardware_lock held.
  1140. */
  1141. static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
  1142. struct tcm_qla2xxx_nacl *nacl, struct qla_tgt_sess *sess)
  1143. {
  1144. struct se_session *se_sess = sess->se_sess;
  1145. unsigned char be_sid[3];
  1146. be_sid[0] = sess->s_id.b.domain;
  1147. be_sid[1] = sess->s_id.b.area;
  1148. be_sid[2] = sess->s_id.b.al_pa;
  1149. tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
  1150. sess, be_sid);
  1151. tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
  1152. sess, sess->loop_id);
  1153. }
  1154. static void tcm_qla2xxx_free_session(struct qla_tgt_sess *sess)
  1155. {
  1156. struct qla_tgt *tgt = sess->tgt;
  1157. struct qla_hw_data *ha = tgt->ha;
  1158. struct se_session *se_sess;
  1159. struct se_node_acl *se_nacl;
  1160. struct tcm_qla2xxx_lport *lport;
  1161. struct tcm_qla2xxx_nacl *nacl;
  1162. BUG_ON(in_interrupt());
  1163. se_sess = sess->se_sess;
  1164. if (!se_sess) {
  1165. pr_err("struct qla_tgt_sess->se_sess is NULL\n");
  1166. dump_stack();
  1167. return;
  1168. }
  1169. se_nacl = se_sess->se_node_acl;
  1170. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  1171. lport = ha->tgt.target_lport_ptr;
  1172. if (!lport) {
  1173. pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
  1174. dump_stack();
  1175. return;
  1176. }
  1177. target_wait_for_sess_cmds(se_sess);
  1178. transport_deregister_session_configfs(sess->se_sess);
  1179. transport_deregister_session(sess->se_sess);
  1180. }
  1181. /*
  1182. * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
  1183. * to locate struct se_node_acl
  1184. */
  1185. static int tcm_qla2xxx_check_initiator_node_acl(
  1186. scsi_qla_host_t *vha,
  1187. unsigned char *fc_wwpn,
  1188. void *qla_tgt_sess,
  1189. uint8_t *s_id,
  1190. uint16_t loop_id)
  1191. {
  1192. struct qla_hw_data *ha = vha->hw;
  1193. struct tcm_qla2xxx_lport *lport;
  1194. struct tcm_qla2xxx_tpg *tpg;
  1195. struct tcm_qla2xxx_nacl *nacl;
  1196. struct se_portal_group *se_tpg;
  1197. struct se_node_acl *se_nacl;
  1198. struct se_session *se_sess;
  1199. struct qla_tgt_sess *sess = qla_tgt_sess;
  1200. unsigned char port_name[36];
  1201. unsigned long flags;
  1202. lport = ha->tgt.target_lport_ptr;
  1203. if (!lport) {
  1204. pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
  1205. dump_stack();
  1206. return -EINVAL;
  1207. }
  1208. /*
  1209. * Locate the TPG=1 reference..
  1210. */
  1211. tpg = lport->tpg_1;
  1212. if (!tpg) {
  1213. pr_err("Unable to lcoate struct tcm_qla2xxx_lport->tpg_1\n");
  1214. return -EINVAL;
  1215. }
  1216. se_tpg = &tpg->se_tpg;
  1217. se_sess = transport_init_session();
  1218. if (IS_ERR(se_sess)) {
  1219. pr_err("Unable to initialize struct se_session\n");
  1220. return PTR_ERR(se_sess);
  1221. }
  1222. /*
  1223. * Format the FCP Initiator port_name into colon seperated values to
  1224. * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
  1225. */
  1226. memset(&port_name, 0, 36);
  1227. snprintf(port_name, 36, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
  1228. fc_wwpn[0], fc_wwpn[1], fc_wwpn[2], fc_wwpn[3], fc_wwpn[4],
  1229. fc_wwpn[5], fc_wwpn[6], fc_wwpn[7]);
  1230. /*
  1231. * Locate our struct se_node_acl either from an explict NodeACL created
  1232. * via ConfigFS, or via running in TPG demo mode.
  1233. */
  1234. se_sess->se_node_acl = core_tpg_check_initiator_node_acl(se_tpg,
  1235. port_name);
  1236. if (!se_sess->se_node_acl) {
  1237. transport_free_session(se_sess);
  1238. return -EINVAL;
  1239. }
  1240. se_nacl = se_sess->se_node_acl;
  1241. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  1242. /*
  1243. * And now setup the new se_nacl and session pointers into our HW lport
  1244. * mappings for fabric S_ID and LOOP_ID.
  1245. */
  1246. spin_lock_irqsave(&ha->hardware_lock, flags);
  1247. tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess,
  1248. qla_tgt_sess, s_id);
  1249. tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, se_sess,
  1250. qla_tgt_sess, loop_id);
  1251. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1252. /*
  1253. * Finally register the new FC Nexus with TCM
  1254. */
  1255. __transport_register_session(se_nacl->se_tpg, se_nacl, se_sess, sess);
  1256. return 0;
  1257. }
  1258. static void tcm_qla2xxx_update_sess(struct qla_tgt_sess *sess, port_id_t s_id,
  1259. uint16_t loop_id, bool conf_compl_supported)
  1260. {
  1261. struct qla_tgt *tgt = sess->tgt;
  1262. struct qla_hw_data *ha = tgt->ha;
  1263. struct tcm_qla2xxx_lport *lport = ha->tgt.target_lport_ptr;
  1264. struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
  1265. struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
  1266. struct tcm_qla2xxx_nacl, se_node_acl);
  1267. u32 key;
  1268. if (sess->loop_id != loop_id || sess->s_id.b24 != s_id.b24)
  1269. pr_info("Updating session %p from port %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
  1270. sess,
  1271. sess->port_name[0], sess->port_name[1],
  1272. sess->port_name[2], sess->port_name[3],
  1273. sess->port_name[4], sess->port_name[5],
  1274. sess->port_name[6], sess->port_name[7],
  1275. sess->loop_id, loop_id,
  1276. sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa,
  1277. s_id.b.domain, s_id.b.area, s_id.b.al_pa);
  1278. if (sess->loop_id != loop_id) {
  1279. /*
  1280. * Because we can shuffle loop IDs around and we
  1281. * update different sessions non-atomically, we might
  1282. * have overwritten this session's old loop ID
  1283. * already, and we might end up overwriting some other
  1284. * session that will be updated later. So we have to
  1285. * be extra careful and we can't warn about those things...
  1286. */
  1287. if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
  1288. lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
  1289. lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
  1290. sess->loop_id = loop_id;
  1291. }
  1292. if (sess->s_id.b24 != s_id.b24) {
  1293. key = (((u32) sess->s_id.b.domain << 16) |
  1294. ((u32) sess->s_id.b.area << 8) |
  1295. ((u32) sess->s_id.b.al_pa));
  1296. if (btree_lookup32(&lport->lport_fcport_map, key))
  1297. WARN(btree_remove32(&lport->lport_fcport_map, key) != se_nacl,
  1298. "Found wrong se_nacl when updating s_id %x:%x:%x\n",
  1299. sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
  1300. else
  1301. WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
  1302. sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
  1303. key = (((u32) s_id.b.domain << 16) |
  1304. ((u32) s_id.b.area << 8) |
  1305. ((u32) s_id.b.al_pa));
  1306. if (btree_lookup32(&lport->lport_fcport_map, key)) {
  1307. WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
  1308. s_id.b.domain, s_id.b.area, s_id.b.al_pa);
  1309. btree_update32(&lport->lport_fcport_map, key, se_nacl);
  1310. } else {
  1311. btree_insert32(&lport->lport_fcport_map, key, se_nacl, GFP_ATOMIC);
  1312. }
  1313. sess->s_id = s_id;
  1314. nacl->nport_id = key;
  1315. }
  1316. sess->conf_compl_supported = conf_compl_supported;
  1317. }
  1318. /*
  1319. * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
  1320. */
  1321. static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
  1322. .handle_cmd = tcm_qla2xxx_handle_cmd,
  1323. .handle_data = tcm_qla2xxx_handle_data,
  1324. .handle_tmr = tcm_qla2xxx_handle_tmr,
  1325. .free_cmd = tcm_qla2xxx_free_cmd,
  1326. .free_mcmd = tcm_qla2xxx_free_mcmd,
  1327. .free_session = tcm_qla2xxx_free_session,
  1328. .update_sess = tcm_qla2xxx_update_sess,
  1329. .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
  1330. .find_sess_by_s_id = tcm_qla2xxx_find_sess_by_s_id,
  1331. .find_sess_by_loop_id = tcm_qla2xxx_find_sess_by_loop_id,
  1332. .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
  1333. .put_sess = tcm_qla2xxx_put_sess,
  1334. .shutdown_sess = tcm_qla2xxx_shutdown_sess,
  1335. };
  1336. static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
  1337. {
  1338. int rc;
  1339. rc = btree_init32(&lport->lport_fcport_map);
  1340. if (rc) {
  1341. pr_err("Unable to initialize lport->lport_fcport_map btree\n");
  1342. return rc;
  1343. }
  1344. lport->lport_loopid_map = vmalloc(sizeof(struct tcm_qla2xxx_fc_loopid) *
  1345. 65536);
  1346. if (!lport->lport_loopid_map) {
  1347. pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
  1348. sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
  1349. btree_destroy32(&lport->lport_fcport_map);
  1350. return -ENOMEM;
  1351. }
  1352. memset(lport->lport_loopid_map, 0, sizeof(struct tcm_qla2xxx_fc_loopid)
  1353. * 65536);
  1354. pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
  1355. sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
  1356. return 0;
  1357. }
  1358. static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha)
  1359. {
  1360. struct qla_hw_data *ha = vha->hw;
  1361. struct tcm_qla2xxx_lport *lport;
  1362. /*
  1363. * Setup local pointer to vha, NPIV VP pointer (if present) and
  1364. * vha->tcm_lport pointer
  1365. */
  1366. lport = (struct tcm_qla2xxx_lport *)ha->tgt.target_lport_ptr;
  1367. lport->qla_vha = vha;
  1368. return 0;
  1369. }
  1370. static struct se_wwn *tcm_qla2xxx_make_lport(
  1371. struct target_fabric_configfs *tf,
  1372. struct config_group *group,
  1373. const char *name)
  1374. {
  1375. struct tcm_qla2xxx_lport *lport;
  1376. u64 wwpn;
  1377. int ret = -ENODEV;
  1378. if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
  1379. return ERR_PTR(-EINVAL);
  1380. lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
  1381. if (!lport) {
  1382. pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
  1383. return ERR_PTR(-ENOMEM);
  1384. }
  1385. lport->lport_wwpn = wwpn;
  1386. tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
  1387. wwpn);
  1388. sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
  1389. ret = tcm_qla2xxx_init_lport(lport);
  1390. if (ret != 0)
  1391. goto out;
  1392. ret = qlt_lport_register(&tcm_qla2xxx_template, wwpn,
  1393. tcm_qla2xxx_lport_register_cb, lport);
  1394. if (ret != 0)
  1395. goto out_lport;
  1396. return &lport->lport_wwn;
  1397. out_lport:
  1398. vfree(lport->lport_loopid_map);
  1399. btree_destroy32(&lport->lport_fcport_map);
  1400. out:
  1401. kfree(lport);
  1402. return ERR_PTR(ret);
  1403. }
  1404. static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
  1405. {
  1406. struct tcm_qla2xxx_lport *lport = container_of(wwn,
  1407. struct tcm_qla2xxx_lport, lport_wwn);
  1408. struct scsi_qla_host *vha = lport->qla_vha;
  1409. struct qla_hw_data *ha = vha->hw;
  1410. struct se_node_acl *node;
  1411. u32 key = 0;
  1412. /*
  1413. * Call into qla2x_target.c LLD logic to complete the
  1414. * shutdown of struct qla_tgt after the call to
  1415. * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
  1416. */
  1417. if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stopped)
  1418. qlt_stop_phase2(ha->tgt.qla_tgt);
  1419. qlt_lport_deregister(vha);
  1420. vfree(lport->lport_loopid_map);
  1421. btree_for_each_safe32(&lport->lport_fcport_map, key, node)
  1422. btree_remove32(&lport->lport_fcport_map, key);
  1423. btree_destroy32(&lport->lport_fcport_map);
  1424. kfree(lport);
  1425. }
  1426. static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
  1427. struct target_fabric_configfs *tf,
  1428. struct config_group *group,
  1429. const char *name)
  1430. {
  1431. struct tcm_qla2xxx_lport *lport;
  1432. u64 npiv_wwpn, npiv_wwnn;
  1433. int ret;
  1434. if (tcm_qla2xxx_npiv_parse_wwn(name, strlen(name)+1,
  1435. &npiv_wwpn, &npiv_wwnn) < 0)
  1436. return ERR_PTR(-EINVAL);
  1437. lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
  1438. if (!lport) {
  1439. pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
  1440. return ERR_PTR(-ENOMEM);
  1441. }
  1442. lport->lport_npiv_wwpn = npiv_wwpn;
  1443. lport->lport_npiv_wwnn = npiv_wwnn;
  1444. tcm_qla2xxx_npiv_format_wwn(&lport->lport_npiv_name[0],
  1445. TCM_QLA2XXX_NAMELEN, npiv_wwpn, npiv_wwnn);
  1446. sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
  1447. /* FIXME: tcm_qla2xxx_npiv_make_lport */
  1448. ret = -ENOSYS;
  1449. if (ret != 0)
  1450. goto out;
  1451. return &lport->lport_wwn;
  1452. out:
  1453. kfree(lport);
  1454. return ERR_PTR(ret);
  1455. }
  1456. static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
  1457. {
  1458. struct tcm_qla2xxx_lport *lport = container_of(wwn,
  1459. struct tcm_qla2xxx_lport, lport_wwn);
  1460. struct scsi_qla_host *vha = lport->qla_vha;
  1461. struct Scsi_Host *sh = vha->host;
  1462. /*
  1463. * Notify libfc that we want to release the lport->npiv_vport
  1464. */
  1465. fc_vport_terminate(lport->npiv_vport);
  1466. scsi_host_put(sh);
  1467. kfree(lport);
  1468. }
  1469. static ssize_t tcm_qla2xxx_wwn_show_attr_version(
  1470. struct target_fabric_configfs *tf,
  1471. char *page)
  1472. {
  1473. return sprintf(page,
  1474. "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on "
  1475. UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
  1476. utsname()->machine);
  1477. }
  1478. TF_WWN_ATTR_RO(tcm_qla2xxx, version);
  1479. static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
  1480. &tcm_qla2xxx_wwn_version.attr,
  1481. NULL,
  1482. };
  1483. static struct target_core_fabric_ops tcm_qla2xxx_ops = {
  1484. .get_fabric_name = tcm_qla2xxx_get_fabric_name,
  1485. .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
  1486. .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
  1487. .tpg_get_tag = tcm_qla2xxx_get_tag,
  1488. .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
  1489. .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
  1490. .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
  1491. .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
  1492. .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
  1493. .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
  1494. .tpg_check_demo_mode_write_protect =
  1495. tcm_qla2xxx_check_demo_write_protect,
  1496. .tpg_check_prod_mode_write_protect =
  1497. tcm_qla2xxx_check_prod_write_protect,
  1498. .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
  1499. .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
  1500. .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
  1501. .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
  1502. .check_stop_free = tcm_qla2xxx_check_stop_free,
  1503. .release_cmd = tcm_qla2xxx_release_cmd,
  1504. .put_session = tcm_qla2xxx_put_session,
  1505. .shutdown_session = tcm_qla2xxx_shutdown_session,
  1506. .close_session = tcm_qla2xxx_close_session,
  1507. .sess_get_index = tcm_qla2xxx_sess_get_index,
  1508. .sess_get_initiator_sid = NULL,
  1509. .write_pending = tcm_qla2xxx_write_pending,
  1510. .write_pending_status = tcm_qla2xxx_write_pending_status,
  1511. .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
  1512. .get_task_tag = tcm_qla2xxx_get_task_tag,
  1513. .get_cmd_state = tcm_qla2xxx_get_cmd_state,
  1514. .queue_data_in = tcm_qla2xxx_queue_data_in,
  1515. .queue_status = tcm_qla2xxx_queue_status,
  1516. .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
  1517. /*
  1518. * Setup function pointers for generic logic in
  1519. * target_core_fabric_configfs.c
  1520. */
  1521. .fabric_make_wwn = tcm_qla2xxx_make_lport,
  1522. .fabric_drop_wwn = tcm_qla2xxx_drop_lport,
  1523. .fabric_make_tpg = tcm_qla2xxx_make_tpg,
  1524. .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
  1525. .fabric_post_link = NULL,
  1526. .fabric_pre_unlink = NULL,
  1527. .fabric_make_np = NULL,
  1528. .fabric_drop_np = NULL,
  1529. .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
  1530. .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
  1531. };
  1532. static struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
  1533. .get_fabric_name = tcm_qla2xxx_npiv_get_fabric_name,
  1534. .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
  1535. .tpg_get_wwn = tcm_qla2xxx_npiv_get_fabric_wwn,
  1536. .tpg_get_tag = tcm_qla2xxx_get_tag,
  1537. .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
  1538. .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
  1539. .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
  1540. .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
  1541. .tpg_check_demo_mode = tcm_qla2xxx_check_false,
  1542. .tpg_check_demo_mode_cache = tcm_qla2xxx_check_true,
  1543. .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_true,
  1544. .tpg_check_prod_mode_write_protect = tcm_qla2xxx_check_false,
  1545. .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
  1546. .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
  1547. .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
  1548. .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
  1549. .release_cmd = tcm_qla2xxx_release_cmd,
  1550. .put_session = tcm_qla2xxx_put_session,
  1551. .shutdown_session = tcm_qla2xxx_shutdown_session,
  1552. .close_session = tcm_qla2xxx_close_session,
  1553. .sess_get_index = tcm_qla2xxx_sess_get_index,
  1554. .sess_get_initiator_sid = NULL,
  1555. .write_pending = tcm_qla2xxx_write_pending,
  1556. .write_pending_status = tcm_qla2xxx_write_pending_status,
  1557. .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
  1558. .get_task_tag = tcm_qla2xxx_get_task_tag,
  1559. .get_cmd_state = tcm_qla2xxx_get_cmd_state,
  1560. .queue_data_in = tcm_qla2xxx_queue_data_in,
  1561. .queue_status = tcm_qla2xxx_queue_status,
  1562. .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
  1563. /*
  1564. * Setup function pointers for generic logic in
  1565. * target_core_fabric_configfs.c
  1566. */
  1567. .fabric_make_wwn = tcm_qla2xxx_npiv_make_lport,
  1568. .fabric_drop_wwn = tcm_qla2xxx_npiv_drop_lport,
  1569. .fabric_make_tpg = tcm_qla2xxx_npiv_make_tpg,
  1570. .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
  1571. .fabric_post_link = NULL,
  1572. .fabric_pre_unlink = NULL,
  1573. .fabric_make_np = NULL,
  1574. .fabric_drop_np = NULL,
  1575. .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
  1576. .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
  1577. };
  1578. static int tcm_qla2xxx_register_configfs(void)
  1579. {
  1580. struct target_fabric_configfs *fabric, *npiv_fabric;
  1581. int ret;
  1582. pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on "
  1583. UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
  1584. utsname()->machine);
  1585. /*
  1586. * Register the top level struct config_item_type with TCM core
  1587. */
  1588. fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx");
  1589. if (IS_ERR(fabric)) {
  1590. pr_err("target_fabric_configfs_init() failed\n");
  1591. return PTR_ERR(fabric);
  1592. }
  1593. /*
  1594. * Setup fabric->tf_ops from our local tcm_qla2xxx_ops
  1595. */
  1596. fabric->tf_ops = tcm_qla2xxx_ops;
  1597. /*
  1598. * Setup default attribute lists for various fabric->tf_cit_tmpl
  1599. */
  1600. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
  1601. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_qla2xxx_tpg_attrs;
  1602. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs =
  1603. tcm_qla2xxx_tpg_attrib_attrs;
  1604. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1605. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1606. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  1607. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  1608. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  1609. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  1610. /*
  1611. * Register the fabric for use within TCM
  1612. */
  1613. ret = target_fabric_configfs_register(fabric);
  1614. if (ret < 0) {
  1615. pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
  1616. return ret;
  1617. }
  1618. /*
  1619. * Setup our local pointer to *fabric
  1620. */
  1621. tcm_qla2xxx_fabric_configfs = fabric;
  1622. pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_fabric_configfs\n");
  1623. /*
  1624. * Register the top level struct config_item_type for NPIV with TCM core
  1625. */
  1626. npiv_fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx_npiv");
  1627. if (IS_ERR(npiv_fabric)) {
  1628. pr_err("target_fabric_configfs_init() failed\n");
  1629. ret = PTR_ERR(npiv_fabric);
  1630. goto out_fabric;
  1631. }
  1632. /*
  1633. * Setup fabric->tf_ops from our local tcm_qla2xxx_npiv_ops
  1634. */
  1635. npiv_fabric->tf_ops = tcm_qla2xxx_npiv_ops;
  1636. /*
  1637. * Setup default attribute lists for various npiv_fabric->tf_cit_tmpl
  1638. */
  1639. TF_CIT_TMPL(npiv_fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
  1640. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_base_cit.ct_attrs = NULL;
  1641. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
  1642. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1643. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1644. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  1645. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  1646. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  1647. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  1648. /*
  1649. * Register the npiv_fabric for use within TCM
  1650. */
  1651. ret = target_fabric_configfs_register(npiv_fabric);
  1652. if (ret < 0) {
  1653. pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
  1654. goto out_fabric;
  1655. }
  1656. /*
  1657. * Setup our local pointer to *npiv_fabric
  1658. */
  1659. tcm_qla2xxx_npiv_fabric_configfs = npiv_fabric;
  1660. pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_npiv_fabric_configfs\n");
  1661. tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
  1662. WQ_MEM_RECLAIM, 0);
  1663. if (!tcm_qla2xxx_free_wq) {
  1664. ret = -ENOMEM;
  1665. goto out_fabric_npiv;
  1666. }
  1667. tcm_qla2xxx_cmd_wq = alloc_workqueue("tcm_qla2xxx_cmd", 0, 0);
  1668. if (!tcm_qla2xxx_cmd_wq) {
  1669. ret = -ENOMEM;
  1670. goto out_free_wq;
  1671. }
  1672. return 0;
  1673. out_free_wq:
  1674. destroy_workqueue(tcm_qla2xxx_free_wq);
  1675. out_fabric_npiv:
  1676. target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
  1677. out_fabric:
  1678. target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
  1679. return ret;
  1680. }
  1681. static void tcm_qla2xxx_deregister_configfs(void)
  1682. {
  1683. destroy_workqueue(tcm_qla2xxx_cmd_wq);
  1684. destroy_workqueue(tcm_qla2xxx_free_wq);
  1685. target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
  1686. tcm_qla2xxx_fabric_configfs = NULL;
  1687. pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_fabric_configfs\n");
  1688. target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
  1689. tcm_qla2xxx_npiv_fabric_configfs = NULL;
  1690. pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_npiv_fabric_configfs\n");
  1691. }
  1692. static int __init tcm_qla2xxx_init(void)
  1693. {
  1694. int ret;
  1695. ret = tcm_qla2xxx_register_configfs();
  1696. if (ret < 0)
  1697. return ret;
  1698. return 0;
  1699. }
  1700. static void __exit tcm_qla2xxx_exit(void)
  1701. {
  1702. tcm_qla2xxx_deregister_configfs();
  1703. }
  1704. MODULE_DESCRIPTION("TCM QLA2XXX series NPIV enabled fabric driver");
  1705. MODULE_LICENSE("GPL");
  1706. module_init(tcm_qla2xxx_init);
  1707. module_exit(tcm_qla2xxx_exit);