tcm_qla2xxx.c 53 KB

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