tcm_qla2xxx.c 54 KB

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