tcm_qla2xxx.c 52 KB

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