tcm_qla2xxx.c 55 KB

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