tcm_qla2xxx.c 54 KB

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