tcm_qla2xxx.c 54 KB

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