tcm_qla2xxx.c 53 KB

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