tcm_qla2xxx.c 54 KB

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