tcm_qla2xxx.c 52 KB

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