tcm_qla2xxx.c 54 KB

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