tcm_qla2xxx.c 55 KB

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