csio_lnode.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156
  1. /*
  2. * This file is part of the Chelsio FCoE driver for Linux.
  3. *
  4. * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/delay.h>
  36. #include <linux/slab.h>
  37. #include <linux/utsname.h>
  38. #include <scsi/scsi_device.h>
  39. #include <scsi/scsi_transport_fc.h>
  40. #include <asm/unaligned.h>
  41. #include <scsi/fc/fc_els.h>
  42. #include <scsi/fc/fc_fs.h>
  43. #include <scsi/fc/fc_gs.h>
  44. #include <scsi/fc/fc_ms.h>
  45. #include "csio_hw.h"
  46. #include "csio_mb.h"
  47. #include "csio_lnode.h"
  48. #include "csio_rnode.h"
  49. int csio_fcoe_rnodes = 1024;
  50. int csio_fdmi_enable = 1;
  51. #define PORT_ID_PTR(_x) ((uint8_t *)(&_x) + 1)
  52. /* Lnode SM declarations */
  53. static void csio_lns_uninit(struct csio_lnode *, enum csio_ln_ev);
  54. static void csio_lns_online(struct csio_lnode *, enum csio_ln_ev);
  55. static void csio_lns_ready(struct csio_lnode *, enum csio_ln_ev);
  56. static void csio_lns_offline(struct csio_lnode *, enum csio_ln_ev);
  57. static int csio_ln_mgmt_submit_req(struct csio_ioreq *,
  58. void (*io_cbfn) (struct csio_hw *, struct csio_ioreq *),
  59. enum fcoe_cmn_type, struct csio_dma_buf *, uint32_t);
  60. /* LN event mapping */
  61. static enum csio_ln_ev fwevt_to_lnevt[] = {
  62. CSIO_LNE_NONE, /* None */
  63. CSIO_LNE_NONE, /* PLOGI_ACC_RCVD */
  64. CSIO_LNE_NONE, /* PLOGI_RJT_RCVD */
  65. CSIO_LNE_NONE, /* PLOGI_RCVD */
  66. CSIO_LNE_NONE, /* PLOGO_RCVD */
  67. CSIO_LNE_NONE, /* PRLI_ACC_RCVD */
  68. CSIO_LNE_NONE, /* PRLI_RJT_RCVD */
  69. CSIO_LNE_NONE, /* PRLI_RCVD */
  70. CSIO_LNE_NONE, /* PRLO_RCVD */
  71. CSIO_LNE_NONE, /* NPORT_ID_CHGD */
  72. CSIO_LNE_LOGO, /* FLOGO_RCVD */
  73. CSIO_LNE_LOGO, /* CLR_VIRT_LNK_RCVD */
  74. CSIO_LNE_FAB_INIT_DONE,/* FLOGI_ACC_RCVD */
  75. CSIO_LNE_NONE, /* FLOGI_RJT_RCVD */
  76. CSIO_LNE_FAB_INIT_DONE,/* FDISC_ACC_RCVD */
  77. CSIO_LNE_NONE, /* FDISC_RJT_RCVD */
  78. CSIO_LNE_NONE, /* FLOGI_TMO_MAX_RETRY */
  79. CSIO_LNE_NONE, /* IMPL_LOGO_ADISC_ACC */
  80. CSIO_LNE_NONE, /* IMPL_LOGO_ADISC_RJT */
  81. CSIO_LNE_NONE, /* IMPL_LOGO_ADISC_CNFLT */
  82. CSIO_LNE_NONE, /* PRLI_TMO */
  83. CSIO_LNE_NONE, /* ADISC_TMO */
  84. CSIO_LNE_NONE, /* RSCN_DEV_LOST */
  85. CSIO_LNE_NONE, /* SCR_ACC_RCVD */
  86. CSIO_LNE_NONE, /* ADISC_RJT_RCVD */
  87. CSIO_LNE_NONE, /* LOGO_SNT */
  88. CSIO_LNE_NONE, /* PROTO_ERR_IMPL_LOGO */
  89. };
  90. #define CSIO_FWE_TO_LNE(_evt) ((_evt > PROTO_ERR_IMPL_LOGO) ? \
  91. CSIO_LNE_NONE : \
  92. fwevt_to_lnevt[_evt])
  93. #define csio_ct_rsp(cp) (((struct fc_ct_hdr *)cp)->ct_cmd)
  94. #define csio_ct_reason(cp) (((struct fc_ct_hdr *)cp)->ct_reason)
  95. #define csio_ct_expl(cp) (((struct fc_ct_hdr *)cp)->ct_explan)
  96. #define csio_ct_get_pld(cp) ((void *)(((uint8_t *)cp) + FC_CT_HDR_LEN))
  97. /*
  98. * csio_ln_match_by_portid - lookup lnode using given portid.
  99. * @hw: HW module
  100. * @portid: port-id.
  101. *
  102. * If found, returns lnode matching given portid otherwise returns NULL.
  103. */
  104. static struct csio_lnode *
  105. csio_ln_lookup_by_portid(struct csio_hw *hw, uint8_t portid)
  106. {
  107. struct csio_lnode *ln;
  108. struct list_head *tmp;
  109. /* Match siblings lnode with portid */
  110. list_for_each(tmp, &hw->sln_head) {
  111. ln = (struct csio_lnode *) tmp;
  112. if (ln->portid == portid)
  113. return ln;
  114. }
  115. return NULL;
  116. }
  117. /*
  118. * csio_ln_lookup_by_vnpi - Lookup lnode using given vnp id.
  119. * @hw - HW module
  120. * @vnpi - vnp index.
  121. * Returns - If found, returns lnode matching given vnp id
  122. * otherwise returns NULL.
  123. */
  124. static struct csio_lnode *
  125. csio_ln_lookup_by_vnpi(struct csio_hw *hw, uint32_t vnp_id)
  126. {
  127. struct list_head *tmp1, *tmp2;
  128. struct csio_lnode *sln = NULL, *cln = NULL;
  129. if (list_empty(&hw->sln_head)) {
  130. CSIO_INC_STATS(hw, n_lnlkup_miss);
  131. return NULL;
  132. }
  133. /* Traverse sibling lnodes */
  134. list_for_each(tmp1, &hw->sln_head) {
  135. sln = (struct csio_lnode *) tmp1;
  136. /* Match sibling lnode */
  137. if (sln->vnp_flowid == vnp_id)
  138. return sln;
  139. if (list_empty(&sln->cln_head))
  140. continue;
  141. /* Traverse children lnodes */
  142. list_for_each(tmp2, &sln->cln_head) {
  143. cln = (struct csio_lnode *) tmp2;
  144. if (cln->vnp_flowid == vnp_id)
  145. return cln;
  146. }
  147. }
  148. CSIO_INC_STATS(hw, n_lnlkup_miss);
  149. return NULL;
  150. }
  151. /**
  152. * csio_lnode_lookup_by_wwpn - Lookup lnode using given wwpn.
  153. * @hw: HW module.
  154. * @wwpn: WWPN.
  155. *
  156. * If found, returns lnode matching given wwpn, returns NULL otherwise.
  157. */
  158. struct csio_lnode *
  159. csio_lnode_lookup_by_wwpn(struct csio_hw *hw, uint8_t *wwpn)
  160. {
  161. struct list_head *tmp1, *tmp2;
  162. struct csio_lnode *sln = NULL, *cln = NULL;
  163. if (list_empty(&hw->sln_head)) {
  164. CSIO_INC_STATS(hw, n_lnlkup_miss);
  165. return NULL;
  166. }
  167. /* Traverse sibling lnodes */
  168. list_for_each(tmp1, &hw->sln_head) {
  169. sln = (struct csio_lnode *) tmp1;
  170. /* Match sibling lnode */
  171. if (!memcmp(csio_ln_wwpn(sln), wwpn, 8))
  172. return sln;
  173. if (list_empty(&sln->cln_head))
  174. continue;
  175. /* Traverse children lnodes */
  176. list_for_each(tmp2, &sln->cln_head) {
  177. cln = (struct csio_lnode *) tmp2;
  178. if (!memcmp(csio_ln_wwpn(cln), wwpn, 8))
  179. return cln;
  180. }
  181. }
  182. return NULL;
  183. }
  184. /* FDMI */
  185. static void
  186. csio_fill_ct_iu(void *buf, uint8_t type, uint8_t sub_type, uint16_t op)
  187. {
  188. struct fc_ct_hdr *cmd = (struct fc_ct_hdr *)buf;
  189. cmd->ct_rev = FC_CT_REV;
  190. cmd->ct_fs_type = type;
  191. cmd->ct_fs_subtype = sub_type;
  192. cmd->ct_cmd = htons(op);
  193. }
  194. static int
  195. csio_hostname(uint8_t *buf, size_t buf_len)
  196. {
  197. if (snprintf(buf, buf_len, "%s", init_utsname()->nodename) > 0)
  198. return 0;
  199. return -1;
  200. }
  201. static int
  202. csio_osname(uint8_t *buf, size_t buf_len)
  203. {
  204. if (snprintf(buf, buf_len, "%s %s %s",
  205. init_utsname()->sysname,
  206. init_utsname()->release,
  207. init_utsname()->version) > 0)
  208. return 0;
  209. return -1;
  210. }
  211. static inline void
  212. csio_append_attrib(uint8_t **ptr, uint16_t type, void *val, size_t val_len)
  213. {
  214. uint16_t len;
  215. struct fc_fdmi_attr_entry *ae = (struct fc_fdmi_attr_entry *)*ptr;
  216. if (WARN_ON(val_len > U16_MAX))
  217. return;
  218. len = val_len;
  219. ae->type = htons(type);
  220. len += 4; /* includes attribute type and length */
  221. len = (len + 3) & ~3; /* should be multiple of 4 bytes */
  222. ae->len = htons(len);
  223. memcpy(ae->value, val, val_len);
  224. if (len > val_len)
  225. memset(ae->value + val_len, 0, len - val_len);
  226. *ptr += len;
  227. }
  228. /*
  229. * csio_ln_fdmi_done - FDMI registeration completion
  230. * @hw: HW context
  231. * @fdmi_req: fdmi request
  232. */
  233. static void
  234. csio_ln_fdmi_done(struct csio_hw *hw, struct csio_ioreq *fdmi_req)
  235. {
  236. void *cmd;
  237. struct csio_lnode *ln = fdmi_req->lnode;
  238. if (fdmi_req->wr_status != FW_SUCCESS) {
  239. csio_ln_dbg(ln, "WR error:%x in processing fdmi rpa cmd\n",
  240. fdmi_req->wr_status);
  241. CSIO_INC_STATS(ln, n_fdmi_err);
  242. }
  243. cmd = fdmi_req->dma_buf.vaddr;
  244. if (ntohs(csio_ct_rsp(cmd)) != FC_FS_ACC) {
  245. csio_ln_dbg(ln, "fdmi rpa cmd rejected reason %x expl %x\n",
  246. csio_ct_reason(cmd), csio_ct_expl(cmd));
  247. }
  248. }
  249. /*
  250. * csio_ln_fdmi_rhba_cbfn - RHBA completion
  251. * @hw: HW context
  252. * @fdmi_req: fdmi request
  253. */
  254. static void
  255. csio_ln_fdmi_rhba_cbfn(struct csio_hw *hw, struct csio_ioreq *fdmi_req)
  256. {
  257. void *cmd;
  258. uint8_t *pld;
  259. uint32_t len = 0;
  260. __be32 val;
  261. __be16 mfs;
  262. uint32_t numattrs = 0;
  263. struct csio_lnode *ln = fdmi_req->lnode;
  264. struct fs_fdmi_attrs *attrib_blk;
  265. struct fc_fdmi_port_name *port_name;
  266. uint8_t buf[64];
  267. uint8_t *fc4_type;
  268. unsigned long flags;
  269. if (fdmi_req->wr_status != FW_SUCCESS) {
  270. csio_ln_dbg(ln, "WR error:%x in processing fdmi rhba cmd\n",
  271. fdmi_req->wr_status);
  272. CSIO_INC_STATS(ln, n_fdmi_err);
  273. }
  274. cmd = fdmi_req->dma_buf.vaddr;
  275. if (ntohs(csio_ct_rsp(cmd)) != FC_FS_ACC) {
  276. csio_ln_dbg(ln, "fdmi rhba cmd rejected reason %x expl %x\n",
  277. csio_ct_reason(cmd), csio_ct_expl(cmd));
  278. }
  279. if (!csio_is_rnode_ready(fdmi_req->rnode)) {
  280. CSIO_INC_STATS(ln, n_fdmi_err);
  281. return;
  282. }
  283. /* Prepare CT hdr for RPA cmd */
  284. memset(cmd, 0, FC_CT_HDR_LEN);
  285. csio_fill_ct_iu(cmd, FC_FST_MGMT, FC_FDMI_SUBTYPE, FC_FDMI_RPA);
  286. /* Prepare RPA payload */
  287. pld = (uint8_t *)csio_ct_get_pld(cmd);
  288. port_name = (struct fc_fdmi_port_name *)pld;
  289. memcpy(&port_name->portname, csio_ln_wwpn(ln), 8);
  290. pld += sizeof(*port_name);
  291. /* Start appending Port attributes */
  292. attrib_blk = (struct fs_fdmi_attrs *)pld;
  293. attrib_blk->numattrs = 0;
  294. len += sizeof(attrib_blk->numattrs);
  295. pld += sizeof(attrib_blk->numattrs);
  296. fc4_type = &buf[0];
  297. memset(fc4_type, 0, FC_FDMI_PORT_ATTR_FC4TYPES_LEN);
  298. fc4_type[2] = 1;
  299. fc4_type[7] = 1;
  300. csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_FC4TYPES,
  301. fc4_type, FC_FDMI_PORT_ATTR_FC4TYPES_LEN);
  302. numattrs++;
  303. val = htonl(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
  304. csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_SUPPORTEDSPEED,
  305. &val,
  306. FC_FDMI_PORT_ATTR_SUPPORTEDSPEED_LEN);
  307. numattrs++;
  308. if (hw->pport[ln->portid].link_speed == FW_PORT_CAP_SPEED_1G)
  309. val = htonl(FC_PORTSPEED_1GBIT);
  310. else if (hw->pport[ln->portid].link_speed == FW_PORT_CAP_SPEED_10G)
  311. val = htonl(FC_PORTSPEED_10GBIT);
  312. else if (hw->pport[ln->portid].link_speed == FW_PORT_CAP32_SPEED_25G)
  313. val = htonl(FC_PORTSPEED_25GBIT);
  314. else if (hw->pport[ln->portid].link_speed == FW_PORT_CAP32_SPEED_40G)
  315. val = htonl(FC_PORTSPEED_40GBIT);
  316. else if (hw->pport[ln->portid].link_speed == FW_PORT_CAP32_SPEED_50G)
  317. val = htonl(FC_PORTSPEED_50GBIT);
  318. else if (hw->pport[ln->portid].link_speed == FW_PORT_CAP32_SPEED_100G)
  319. val = htonl(FC_PORTSPEED_100GBIT);
  320. else
  321. val = htonl(CSIO_HBA_PORTSPEED_UNKNOWN);
  322. csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_CURRENTPORTSPEED,
  323. &val, FC_FDMI_PORT_ATTR_CURRENTPORTSPEED_LEN);
  324. numattrs++;
  325. mfs = ln->ln_sparm.csp.sp_bb_data;
  326. csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_MAXFRAMESIZE,
  327. &mfs, sizeof(mfs));
  328. numattrs++;
  329. strcpy(buf, "csiostor");
  330. csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_OSDEVICENAME, buf,
  331. strlen(buf));
  332. numattrs++;
  333. if (!csio_hostname(buf, sizeof(buf))) {
  334. csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_HOSTNAME,
  335. buf, strlen(buf));
  336. numattrs++;
  337. }
  338. attrib_blk->numattrs = htonl(numattrs);
  339. len = (uint32_t)(pld - (uint8_t *)cmd);
  340. /* Submit FDMI RPA request */
  341. spin_lock_irqsave(&hw->lock, flags);
  342. if (csio_ln_mgmt_submit_req(fdmi_req, csio_ln_fdmi_done,
  343. FCOE_CT, &fdmi_req->dma_buf, len)) {
  344. CSIO_INC_STATS(ln, n_fdmi_err);
  345. csio_ln_dbg(ln, "Failed to issue fdmi rpa req\n");
  346. }
  347. spin_unlock_irqrestore(&hw->lock, flags);
  348. }
  349. /*
  350. * csio_ln_fdmi_dprt_cbfn - DPRT completion
  351. * @hw: HW context
  352. * @fdmi_req: fdmi request
  353. */
  354. static void
  355. csio_ln_fdmi_dprt_cbfn(struct csio_hw *hw, struct csio_ioreq *fdmi_req)
  356. {
  357. void *cmd;
  358. uint8_t *pld;
  359. uint32_t len = 0;
  360. uint32_t numattrs = 0;
  361. __be32 maxpayload = htonl(65536);
  362. struct fc_fdmi_hba_identifier *hbaid;
  363. struct csio_lnode *ln = fdmi_req->lnode;
  364. struct fc_fdmi_rpl *reg_pl;
  365. struct fs_fdmi_attrs *attrib_blk;
  366. uint8_t buf[64];
  367. unsigned long flags;
  368. if (fdmi_req->wr_status != FW_SUCCESS) {
  369. csio_ln_dbg(ln, "WR error:%x in processing fdmi dprt cmd\n",
  370. fdmi_req->wr_status);
  371. CSIO_INC_STATS(ln, n_fdmi_err);
  372. }
  373. if (!csio_is_rnode_ready(fdmi_req->rnode)) {
  374. CSIO_INC_STATS(ln, n_fdmi_err);
  375. return;
  376. }
  377. cmd = fdmi_req->dma_buf.vaddr;
  378. if (ntohs(csio_ct_rsp(cmd)) != FC_FS_ACC) {
  379. csio_ln_dbg(ln, "fdmi dprt cmd rejected reason %x expl %x\n",
  380. csio_ct_reason(cmd), csio_ct_expl(cmd));
  381. }
  382. /* Prepare CT hdr for RHBA cmd */
  383. memset(cmd, 0, FC_CT_HDR_LEN);
  384. csio_fill_ct_iu(cmd, FC_FST_MGMT, FC_FDMI_SUBTYPE, FC_FDMI_RHBA);
  385. len = FC_CT_HDR_LEN;
  386. /* Prepare RHBA payload */
  387. pld = (uint8_t *)csio_ct_get_pld(cmd);
  388. hbaid = (struct fc_fdmi_hba_identifier *)pld;
  389. memcpy(&hbaid->id, csio_ln_wwpn(ln), 8); /* HBA identifer */
  390. pld += sizeof(*hbaid);
  391. /* Register one port per hba */
  392. reg_pl = (struct fc_fdmi_rpl *)pld;
  393. reg_pl->numport = htonl(1);
  394. memcpy(&reg_pl->port[0].portname, csio_ln_wwpn(ln), 8);
  395. pld += sizeof(*reg_pl);
  396. /* Start appending HBA attributes hba */
  397. attrib_blk = (struct fs_fdmi_attrs *)pld;
  398. attrib_blk->numattrs = 0;
  399. len += sizeof(attrib_blk->numattrs);
  400. pld += sizeof(attrib_blk->numattrs);
  401. csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_NODENAME, csio_ln_wwnn(ln),
  402. FC_FDMI_HBA_ATTR_NODENAME_LEN);
  403. numattrs++;
  404. memset(buf, 0, sizeof(buf));
  405. strcpy(buf, "Chelsio Communications");
  406. csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MANUFACTURER, buf,
  407. strlen(buf));
  408. numattrs++;
  409. csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_SERIALNUMBER,
  410. hw->vpd.sn, sizeof(hw->vpd.sn));
  411. numattrs++;
  412. csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MODEL, hw->vpd.id,
  413. sizeof(hw->vpd.id));
  414. numattrs++;
  415. csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MODELDESCRIPTION,
  416. hw->model_desc, strlen(hw->model_desc));
  417. numattrs++;
  418. csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_HARDWAREVERSION,
  419. hw->hw_ver, sizeof(hw->hw_ver));
  420. numattrs++;
  421. csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_FIRMWAREVERSION,
  422. hw->fwrev_str, strlen(hw->fwrev_str));
  423. numattrs++;
  424. if (!csio_osname(buf, sizeof(buf))) {
  425. csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_OSNAMEVERSION,
  426. buf, strlen(buf));
  427. numattrs++;
  428. }
  429. csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MAXCTPAYLOAD,
  430. &maxpayload, FC_FDMI_HBA_ATTR_MAXCTPAYLOAD_LEN);
  431. len = (uint32_t)(pld - (uint8_t *)cmd);
  432. numattrs++;
  433. attrib_blk->numattrs = htonl(numattrs);
  434. /* Submit FDMI RHBA request */
  435. spin_lock_irqsave(&hw->lock, flags);
  436. if (csio_ln_mgmt_submit_req(fdmi_req, csio_ln_fdmi_rhba_cbfn,
  437. FCOE_CT, &fdmi_req->dma_buf, len)) {
  438. CSIO_INC_STATS(ln, n_fdmi_err);
  439. csio_ln_dbg(ln, "Failed to issue fdmi rhba req\n");
  440. }
  441. spin_unlock_irqrestore(&hw->lock, flags);
  442. }
  443. /*
  444. * csio_ln_fdmi_dhba_cbfn - DHBA completion
  445. * @hw: HW context
  446. * @fdmi_req: fdmi request
  447. */
  448. static void
  449. csio_ln_fdmi_dhba_cbfn(struct csio_hw *hw, struct csio_ioreq *fdmi_req)
  450. {
  451. struct csio_lnode *ln = fdmi_req->lnode;
  452. void *cmd;
  453. struct fc_fdmi_port_name *port_name;
  454. uint32_t len;
  455. unsigned long flags;
  456. if (fdmi_req->wr_status != FW_SUCCESS) {
  457. csio_ln_dbg(ln, "WR error:%x in processing fdmi dhba cmd\n",
  458. fdmi_req->wr_status);
  459. CSIO_INC_STATS(ln, n_fdmi_err);
  460. }
  461. if (!csio_is_rnode_ready(fdmi_req->rnode)) {
  462. CSIO_INC_STATS(ln, n_fdmi_err);
  463. return;
  464. }
  465. cmd = fdmi_req->dma_buf.vaddr;
  466. if (ntohs(csio_ct_rsp(cmd)) != FC_FS_ACC) {
  467. csio_ln_dbg(ln, "fdmi dhba cmd rejected reason %x expl %x\n",
  468. csio_ct_reason(cmd), csio_ct_expl(cmd));
  469. }
  470. /* Send FDMI cmd to de-register any Port attributes if registered
  471. * before
  472. */
  473. /* Prepare FDMI DPRT cmd */
  474. memset(cmd, 0, FC_CT_HDR_LEN);
  475. csio_fill_ct_iu(cmd, FC_FST_MGMT, FC_FDMI_SUBTYPE, FC_FDMI_DPRT);
  476. len = FC_CT_HDR_LEN;
  477. port_name = (struct fc_fdmi_port_name *)csio_ct_get_pld(cmd);
  478. memcpy(&port_name->portname, csio_ln_wwpn(ln), 8);
  479. len += sizeof(*port_name);
  480. /* Submit FDMI request */
  481. spin_lock_irqsave(&hw->lock, flags);
  482. if (csio_ln_mgmt_submit_req(fdmi_req, csio_ln_fdmi_dprt_cbfn,
  483. FCOE_CT, &fdmi_req->dma_buf, len)) {
  484. CSIO_INC_STATS(ln, n_fdmi_err);
  485. csio_ln_dbg(ln, "Failed to issue fdmi dprt req\n");
  486. }
  487. spin_unlock_irqrestore(&hw->lock, flags);
  488. }
  489. /**
  490. * csio_ln_fdmi_start - Start an FDMI request.
  491. * @ln: lnode
  492. * @context: session context
  493. *
  494. * Issued with lock held.
  495. */
  496. int
  497. csio_ln_fdmi_start(struct csio_lnode *ln, void *context)
  498. {
  499. struct csio_ioreq *fdmi_req;
  500. struct csio_rnode *fdmi_rn = (struct csio_rnode *)context;
  501. void *cmd;
  502. struct fc_fdmi_hba_identifier *hbaid;
  503. uint32_t len;
  504. if (!(ln->flags & CSIO_LNF_FDMI_ENABLE))
  505. return -EPROTONOSUPPORT;
  506. if (!csio_is_rnode_ready(fdmi_rn))
  507. CSIO_INC_STATS(ln, n_fdmi_err);
  508. /* Send FDMI cmd to de-register any HBA attributes if registered
  509. * before
  510. */
  511. fdmi_req = ln->mgmt_req;
  512. fdmi_req->lnode = ln;
  513. fdmi_req->rnode = fdmi_rn;
  514. /* Prepare FDMI DHBA cmd */
  515. cmd = fdmi_req->dma_buf.vaddr;
  516. memset(cmd, 0, FC_CT_HDR_LEN);
  517. csio_fill_ct_iu(cmd, FC_FST_MGMT, FC_FDMI_SUBTYPE, FC_FDMI_DHBA);
  518. len = FC_CT_HDR_LEN;
  519. hbaid = (struct fc_fdmi_hba_identifier *)csio_ct_get_pld(cmd);
  520. memcpy(&hbaid->id, csio_ln_wwpn(ln), 8);
  521. len += sizeof(*hbaid);
  522. /* Submit FDMI request */
  523. if (csio_ln_mgmt_submit_req(fdmi_req, csio_ln_fdmi_dhba_cbfn,
  524. FCOE_CT, &fdmi_req->dma_buf, len)) {
  525. CSIO_INC_STATS(ln, n_fdmi_err);
  526. csio_ln_dbg(ln, "Failed to issue fdmi dhba req\n");
  527. }
  528. return 0;
  529. }
  530. /*
  531. * csio_ln_vnp_read_cbfn - vnp read completion handler.
  532. * @hw: HW lnode
  533. * @cbfn: Completion handler.
  534. *
  535. * Reads vnp response and updates ln parameters.
  536. */
  537. static void
  538. csio_ln_vnp_read_cbfn(struct csio_hw *hw, struct csio_mb *mbp)
  539. {
  540. struct csio_lnode *ln = ((struct csio_lnode *)mbp->priv);
  541. struct fw_fcoe_vnp_cmd *rsp = (struct fw_fcoe_vnp_cmd *)(mbp->mb);
  542. struct fc_els_csp *csp;
  543. struct fc_els_cssp *clsp;
  544. enum fw_retval retval;
  545. __be32 nport_id;
  546. retval = FW_CMD_RETVAL_G(ntohl(rsp->alloc_to_len16));
  547. if (retval != FW_SUCCESS) {
  548. csio_err(hw, "FCOE VNP read cmd returned error:0x%x\n", retval);
  549. mempool_free(mbp, hw->mb_mempool);
  550. return;
  551. }
  552. spin_lock_irq(&hw->lock);
  553. memcpy(ln->mac, rsp->vnport_mac, sizeof(ln->mac));
  554. memcpy(&nport_id, &rsp->vnport_mac[3], sizeof(uint8_t)*3);
  555. ln->nport_id = ntohl(nport_id);
  556. ln->nport_id = ln->nport_id >> 8;
  557. /* Update WWNs */
  558. /*
  559. * This may look like a duplication of what csio_fcoe_enable_link()
  560. * does, but is absolutely necessary if the vnpi changes between
  561. * a FCOE LINK UP and FCOE LINK DOWN.
  562. */
  563. memcpy(csio_ln_wwnn(ln), rsp->vnport_wwnn, 8);
  564. memcpy(csio_ln_wwpn(ln), rsp->vnport_wwpn, 8);
  565. /* Copy common sparam */
  566. csp = (struct fc_els_csp *)rsp->cmn_srv_parms;
  567. ln->ln_sparm.csp.sp_hi_ver = csp->sp_hi_ver;
  568. ln->ln_sparm.csp.sp_lo_ver = csp->sp_lo_ver;
  569. ln->ln_sparm.csp.sp_bb_cred = csp->sp_bb_cred;
  570. ln->ln_sparm.csp.sp_features = csp->sp_features;
  571. ln->ln_sparm.csp.sp_bb_data = csp->sp_bb_data;
  572. ln->ln_sparm.csp.sp_r_a_tov = csp->sp_r_a_tov;
  573. ln->ln_sparm.csp.sp_e_d_tov = csp->sp_e_d_tov;
  574. /* Copy word 0 & word 1 of class sparam */
  575. clsp = (struct fc_els_cssp *)rsp->clsp_word_0_1;
  576. ln->ln_sparm.clsp[2].cp_class = clsp->cp_class;
  577. ln->ln_sparm.clsp[2].cp_init = clsp->cp_init;
  578. ln->ln_sparm.clsp[2].cp_recip = clsp->cp_recip;
  579. ln->ln_sparm.clsp[2].cp_rdfs = clsp->cp_rdfs;
  580. spin_unlock_irq(&hw->lock);
  581. mempool_free(mbp, hw->mb_mempool);
  582. /* Send an event to update local attribs */
  583. csio_lnode_async_event(ln, CSIO_LN_FC_ATTRIB_UPDATE);
  584. }
  585. /*
  586. * csio_ln_vnp_read - Read vnp params.
  587. * @ln: lnode
  588. * @cbfn: Completion handler.
  589. *
  590. * Issued with lock held.
  591. */
  592. static int
  593. csio_ln_vnp_read(struct csio_lnode *ln,
  594. void (*cbfn) (struct csio_hw *, struct csio_mb *))
  595. {
  596. struct csio_hw *hw = ln->hwp;
  597. struct csio_mb *mbp;
  598. /* Allocate Mbox request */
  599. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  600. if (!mbp) {
  601. CSIO_INC_STATS(hw, n_err_nomem);
  602. return -ENOMEM;
  603. }
  604. /* Prepare VNP Command */
  605. csio_fcoe_vnp_read_init_mb(ln, mbp,
  606. CSIO_MB_DEFAULT_TMO,
  607. ln->fcf_flowid,
  608. ln->vnp_flowid,
  609. cbfn);
  610. /* Issue MBOX cmd */
  611. if (csio_mb_issue(hw, mbp)) {
  612. csio_err(hw, "Failed to issue mbox FCoE VNP command\n");
  613. mempool_free(mbp, hw->mb_mempool);
  614. return -EINVAL;
  615. }
  616. return 0;
  617. }
  618. /*
  619. * csio_fcoe_enable_link - Enable fcoe link.
  620. * @ln: lnode
  621. * @enable: enable/disable
  622. * Issued with lock held.
  623. * Issues mbox cmd to bring up FCOE link on port associated with given ln.
  624. */
  625. static int
  626. csio_fcoe_enable_link(struct csio_lnode *ln, bool enable)
  627. {
  628. struct csio_hw *hw = ln->hwp;
  629. struct csio_mb *mbp;
  630. enum fw_retval retval;
  631. uint8_t portid;
  632. uint8_t sub_op;
  633. struct fw_fcoe_link_cmd *lcmd;
  634. int i;
  635. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  636. if (!mbp) {
  637. CSIO_INC_STATS(hw, n_err_nomem);
  638. return -ENOMEM;
  639. }
  640. portid = ln->portid;
  641. sub_op = enable ? FCOE_LINK_UP : FCOE_LINK_DOWN;
  642. csio_dbg(hw, "bringing FCOE LINK %s on Port:%d\n",
  643. sub_op ? "UP" : "DOWN", portid);
  644. csio_write_fcoe_link_cond_init_mb(ln, mbp, CSIO_MB_DEFAULT_TMO,
  645. portid, sub_op, 0, 0, 0, NULL);
  646. if (csio_mb_issue(hw, mbp)) {
  647. csio_err(hw, "failed to issue FCOE LINK cmd on port[%d]\n",
  648. portid);
  649. mempool_free(mbp, hw->mb_mempool);
  650. return -EINVAL;
  651. }
  652. retval = csio_mb_fw_retval(mbp);
  653. if (retval != FW_SUCCESS) {
  654. csio_err(hw,
  655. "FCOE LINK %s cmd on port[%d] failed with "
  656. "ret:x%x\n", sub_op ? "UP" : "DOWN", portid, retval);
  657. mempool_free(mbp, hw->mb_mempool);
  658. return -EINVAL;
  659. }
  660. if (!enable)
  661. goto out;
  662. lcmd = (struct fw_fcoe_link_cmd *)mbp->mb;
  663. memcpy(csio_ln_wwnn(ln), lcmd->vnport_wwnn, 8);
  664. memcpy(csio_ln_wwpn(ln), lcmd->vnport_wwpn, 8);
  665. for (i = 0; i < CSIO_MAX_PPORTS; i++)
  666. if (hw->pport[i].portid == portid)
  667. memcpy(hw->pport[i].mac, lcmd->phy_mac, 6);
  668. out:
  669. mempool_free(mbp, hw->mb_mempool);
  670. return 0;
  671. }
  672. /*
  673. * csio_ln_read_fcf_cbfn - Read fcf parameters
  674. * @ln: lnode
  675. *
  676. * read fcf response and Update ln fcf information.
  677. */
  678. static void
  679. csio_ln_read_fcf_cbfn(struct csio_hw *hw, struct csio_mb *mbp)
  680. {
  681. struct csio_lnode *ln = (struct csio_lnode *)mbp->priv;
  682. struct csio_fcf_info *fcf_info;
  683. struct fw_fcoe_fcf_cmd *rsp =
  684. (struct fw_fcoe_fcf_cmd *)(mbp->mb);
  685. enum fw_retval retval;
  686. retval = FW_CMD_RETVAL_G(ntohl(rsp->retval_len16));
  687. if (retval != FW_SUCCESS) {
  688. csio_ln_err(ln, "FCOE FCF cmd failed with ret x%x\n",
  689. retval);
  690. mempool_free(mbp, hw->mb_mempool);
  691. return;
  692. }
  693. spin_lock_irq(&hw->lock);
  694. fcf_info = ln->fcfinfo;
  695. fcf_info->priority = FW_FCOE_FCF_CMD_PRIORITY_GET(
  696. ntohs(rsp->priority_pkd));
  697. fcf_info->vf_id = ntohs(rsp->vf_id);
  698. fcf_info->vlan_id = rsp->vlan_id;
  699. fcf_info->max_fcoe_size = ntohs(rsp->max_fcoe_size);
  700. fcf_info->fka_adv = be32_to_cpu(rsp->fka_adv);
  701. fcf_info->fcfi = FW_FCOE_FCF_CMD_FCFI_GET(ntohl(rsp->op_to_fcfi));
  702. fcf_info->fpma = FW_FCOE_FCF_CMD_FPMA_GET(rsp->fpma_to_portid);
  703. fcf_info->spma = FW_FCOE_FCF_CMD_SPMA_GET(rsp->fpma_to_portid);
  704. fcf_info->login = FW_FCOE_FCF_CMD_LOGIN_GET(rsp->fpma_to_portid);
  705. fcf_info->portid = FW_FCOE_FCF_CMD_PORTID_GET(rsp->fpma_to_portid);
  706. memcpy(fcf_info->fc_map, rsp->fc_map, sizeof(fcf_info->fc_map));
  707. memcpy(fcf_info->mac, rsp->mac, sizeof(fcf_info->mac));
  708. memcpy(fcf_info->name_id, rsp->name_id, sizeof(fcf_info->name_id));
  709. memcpy(fcf_info->fabric, rsp->fabric, sizeof(fcf_info->fabric));
  710. memcpy(fcf_info->spma_mac, rsp->spma_mac, sizeof(fcf_info->spma_mac));
  711. spin_unlock_irq(&hw->lock);
  712. mempool_free(mbp, hw->mb_mempool);
  713. }
  714. /*
  715. * csio_ln_read_fcf_entry - Read fcf entry.
  716. * @ln: lnode
  717. * @cbfn: Completion handler.
  718. *
  719. * Issued with lock held.
  720. */
  721. static int
  722. csio_ln_read_fcf_entry(struct csio_lnode *ln,
  723. void (*cbfn) (struct csio_hw *, struct csio_mb *))
  724. {
  725. struct csio_hw *hw = ln->hwp;
  726. struct csio_mb *mbp;
  727. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  728. if (!mbp) {
  729. CSIO_INC_STATS(hw, n_err_nomem);
  730. return -ENOMEM;
  731. }
  732. /* Get FCoE FCF information */
  733. csio_fcoe_read_fcf_init_mb(ln, mbp, CSIO_MB_DEFAULT_TMO,
  734. ln->portid, ln->fcf_flowid, cbfn);
  735. if (csio_mb_issue(hw, mbp)) {
  736. csio_err(hw, "failed to issue FCOE FCF cmd\n");
  737. mempool_free(mbp, hw->mb_mempool);
  738. return -EINVAL;
  739. }
  740. return 0;
  741. }
  742. /*
  743. * csio_handle_link_up - Logical Linkup event.
  744. * @hw - HW module.
  745. * @portid - Physical port number
  746. * @fcfi - FCF index.
  747. * @vnpi - VNP index.
  748. * Returns - none.
  749. *
  750. * This event is received from FW, when virtual link is established between
  751. * Physical port[ENode] and FCF. If its new vnpi, then local node object is
  752. * created on this FCF and set to [ONLINE] state.
  753. * Lnode waits for FW_RDEV_CMD event to be received indicating that
  754. * Fabric login is completed and lnode moves to [READY] state.
  755. *
  756. * This called with hw lock held
  757. */
  758. static void
  759. csio_handle_link_up(struct csio_hw *hw, uint8_t portid, uint32_t fcfi,
  760. uint32_t vnpi)
  761. {
  762. struct csio_lnode *ln = NULL;
  763. /* Lookup lnode based on vnpi */
  764. ln = csio_ln_lookup_by_vnpi(hw, vnpi);
  765. if (!ln) {
  766. /* Pick lnode based on portid */
  767. ln = csio_ln_lookup_by_portid(hw, portid);
  768. if (!ln) {
  769. csio_err(hw, "failed to lookup fcoe lnode on port:%d\n",
  770. portid);
  771. CSIO_DB_ASSERT(0);
  772. return;
  773. }
  774. /* Check if lnode has valid vnp flowid */
  775. if (ln->vnp_flowid != CSIO_INVALID_IDX) {
  776. /* New VN-Port */
  777. spin_unlock_irq(&hw->lock);
  778. csio_lnode_alloc(hw);
  779. spin_lock_irq(&hw->lock);
  780. if (!ln) {
  781. csio_err(hw,
  782. "failed to allocate fcoe lnode"
  783. "for port:%d vnpi:x%x\n",
  784. portid, vnpi);
  785. CSIO_DB_ASSERT(0);
  786. return;
  787. }
  788. ln->portid = portid;
  789. }
  790. ln->vnp_flowid = vnpi;
  791. ln->dev_num &= ~0xFFFF;
  792. ln->dev_num |= vnpi;
  793. }
  794. /*Initialize fcfi */
  795. ln->fcf_flowid = fcfi;
  796. csio_info(hw, "Port:%d - FCOE LINK UP\n", portid);
  797. CSIO_INC_STATS(ln, n_link_up);
  798. /* Send LINKUP event to SM */
  799. csio_post_event(&ln->sm, CSIO_LNE_LINKUP);
  800. }
  801. /*
  802. * csio_post_event_rns
  803. * @ln - FCOE lnode
  804. * @evt - Given rnode event
  805. * Returns - none
  806. *
  807. * Posts given rnode event to all FCOE rnodes connected with given Lnode.
  808. * This routine is invoked when lnode receives LINK_DOWN/DOWN_LINK/CLOSE
  809. * event.
  810. *
  811. * This called with hw lock held
  812. */
  813. static void
  814. csio_post_event_rns(struct csio_lnode *ln, enum csio_rn_ev evt)
  815. {
  816. struct csio_rnode *rnhead = (struct csio_rnode *) &ln->rnhead;
  817. struct list_head *tmp, *next;
  818. struct csio_rnode *rn;
  819. list_for_each_safe(tmp, next, &rnhead->sm.sm_list) {
  820. rn = (struct csio_rnode *) tmp;
  821. csio_post_event(&rn->sm, evt);
  822. }
  823. }
  824. /*
  825. * csio_cleanup_rns
  826. * @ln - FCOE lnode
  827. * Returns - none
  828. *
  829. * Frees all FCOE rnodes connected with given Lnode.
  830. *
  831. * This called with hw lock held
  832. */
  833. static void
  834. csio_cleanup_rns(struct csio_lnode *ln)
  835. {
  836. struct csio_rnode *rnhead = (struct csio_rnode *) &ln->rnhead;
  837. struct list_head *tmp, *next_rn;
  838. struct csio_rnode *rn;
  839. list_for_each_safe(tmp, next_rn, &rnhead->sm.sm_list) {
  840. rn = (struct csio_rnode *) tmp;
  841. csio_put_rnode(ln, rn);
  842. }
  843. }
  844. /*
  845. * csio_post_event_lns
  846. * @ln - FCOE lnode
  847. * @evt - Given lnode event
  848. * Returns - none
  849. *
  850. * Posts given lnode event to all FCOE lnodes connected with given Lnode.
  851. * This routine is invoked when lnode receives LINK_DOWN/DOWN_LINK/CLOSE
  852. * event.
  853. *
  854. * This called with hw lock held
  855. */
  856. static void
  857. csio_post_event_lns(struct csio_lnode *ln, enum csio_ln_ev evt)
  858. {
  859. struct list_head *tmp;
  860. struct csio_lnode *cln, *sln;
  861. /* If NPIV lnode, send evt only to that and return */
  862. if (csio_is_npiv_ln(ln)) {
  863. csio_post_event(&ln->sm, evt);
  864. return;
  865. }
  866. sln = ln;
  867. /* Traverse children lnodes list and send evt */
  868. list_for_each(tmp, &sln->cln_head) {
  869. cln = (struct csio_lnode *) tmp;
  870. csio_post_event(&cln->sm, evt);
  871. }
  872. /* Send evt to parent lnode */
  873. csio_post_event(&ln->sm, evt);
  874. }
  875. /*
  876. * csio_ln_down - Lcoal nport is down
  877. * @ln - FCOE Lnode
  878. * Returns - none
  879. *
  880. * Sends LINK_DOWN events to Lnode and its associated NPIVs lnodes.
  881. *
  882. * This called with hw lock held
  883. */
  884. static void
  885. csio_ln_down(struct csio_lnode *ln)
  886. {
  887. csio_post_event_lns(ln, CSIO_LNE_LINK_DOWN);
  888. }
  889. /*
  890. * csio_handle_link_down - Logical Linkdown event.
  891. * @hw - HW module.
  892. * @portid - Physical port number
  893. * @fcfi - FCF index.
  894. * @vnpi - VNP index.
  895. * Returns - none
  896. *
  897. * This event is received from FW, when virtual link goes down between
  898. * Physical port[ENode] and FCF. Lnode and its associated NPIVs lnode hosted on
  899. * this vnpi[VN-Port] will be de-instantiated.
  900. *
  901. * This called with hw lock held
  902. */
  903. static void
  904. csio_handle_link_down(struct csio_hw *hw, uint8_t portid, uint32_t fcfi,
  905. uint32_t vnpi)
  906. {
  907. struct csio_fcf_info *fp;
  908. struct csio_lnode *ln;
  909. /* Lookup lnode based on vnpi */
  910. ln = csio_ln_lookup_by_vnpi(hw, vnpi);
  911. if (ln) {
  912. fp = ln->fcfinfo;
  913. CSIO_INC_STATS(ln, n_link_down);
  914. /*Warn if linkdown received if lnode is not in ready state */
  915. if (!csio_is_lnode_ready(ln)) {
  916. csio_ln_warn(ln,
  917. "warn: FCOE link is already in offline "
  918. "Ignoring Fcoe linkdown event on portid %d\n",
  919. portid);
  920. CSIO_INC_STATS(ln, n_evt_drop);
  921. return;
  922. }
  923. /* Verify portid */
  924. if (fp->portid != portid) {
  925. csio_ln_warn(ln,
  926. "warn: FCOE linkdown recv with "
  927. "invalid port %d\n", portid);
  928. CSIO_INC_STATS(ln, n_evt_drop);
  929. return;
  930. }
  931. /* verify fcfi */
  932. if (ln->fcf_flowid != fcfi) {
  933. csio_ln_warn(ln,
  934. "warn: FCOE linkdown recv with "
  935. "invalid fcfi x%x\n", fcfi);
  936. CSIO_INC_STATS(ln, n_evt_drop);
  937. return;
  938. }
  939. csio_info(hw, "Port:%d - FCOE LINK DOWN\n", portid);
  940. /* Send LINK_DOWN event to lnode s/m */
  941. csio_ln_down(ln);
  942. return;
  943. } else {
  944. csio_warn(hw,
  945. "warn: FCOE linkdown recv with invalid vnpi x%x\n",
  946. vnpi);
  947. CSIO_INC_STATS(hw, n_evt_drop);
  948. }
  949. }
  950. /*
  951. * csio_is_lnode_ready - Checks FCOE lnode is in ready state.
  952. * @ln: Lnode module
  953. *
  954. * Returns True if FCOE lnode is in ready state.
  955. */
  956. int
  957. csio_is_lnode_ready(struct csio_lnode *ln)
  958. {
  959. return (csio_get_state(ln) == ((csio_sm_state_t)csio_lns_ready));
  960. }
  961. /*****************************************************************************/
  962. /* START: Lnode SM */
  963. /*****************************************************************************/
  964. /*
  965. * csio_lns_uninit - The request in uninit state.
  966. * @ln - FCOE lnode.
  967. * @evt - Event to be processed.
  968. *
  969. * Process the given lnode event which is currently in "uninit" state.
  970. * Invoked with HW lock held.
  971. * Return - none.
  972. */
  973. static void
  974. csio_lns_uninit(struct csio_lnode *ln, enum csio_ln_ev evt)
  975. {
  976. struct csio_hw *hw = csio_lnode_to_hw(ln);
  977. struct csio_lnode *rln = hw->rln;
  978. int rv;
  979. CSIO_INC_STATS(ln, n_evt_sm[evt]);
  980. switch (evt) {
  981. case CSIO_LNE_LINKUP:
  982. csio_set_state(&ln->sm, csio_lns_online);
  983. /* Read FCF only for physical lnode */
  984. if (csio_is_phys_ln(ln)) {
  985. rv = csio_ln_read_fcf_entry(ln,
  986. csio_ln_read_fcf_cbfn);
  987. if (rv != 0) {
  988. /* TODO: Send HW RESET event */
  989. CSIO_INC_STATS(ln, n_err);
  990. break;
  991. }
  992. /* Add FCF record */
  993. list_add_tail(&ln->fcfinfo->list, &rln->fcf_lsthead);
  994. }
  995. rv = csio_ln_vnp_read(ln, csio_ln_vnp_read_cbfn);
  996. if (rv != 0) {
  997. /* TODO: Send HW RESET event */
  998. CSIO_INC_STATS(ln, n_err);
  999. }
  1000. break;
  1001. case CSIO_LNE_DOWN_LINK:
  1002. break;
  1003. default:
  1004. csio_ln_dbg(ln,
  1005. "unexp ln event %d recv from did:x%x in "
  1006. "ln state[uninit].\n", evt, ln->nport_id);
  1007. CSIO_INC_STATS(ln, n_evt_unexp);
  1008. break;
  1009. } /* switch event */
  1010. }
  1011. /*
  1012. * csio_lns_online - The request in online state.
  1013. * @ln - FCOE lnode.
  1014. * @evt - Event to be processed.
  1015. *
  1016. * Process the given lnode event which is currently in "online" state.
  1017. * Invoked with HW lock held.
  1018. * Return - none.
  1019. */
  1020. static void
  1021. csio_lns_online(struct csio_lnode *ln, enum csio_ln_ev evt)
  1022. {
  1023. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1024. CSIO_INC_STATS(ln, n_evt_sm[evt]);
  1025. switch (evt) {
  1026. case CSIO_LNE_LINKUP:
  1027. csio_ln_warn(ln,
  1028. "warn: FCOE link is up already "
  1029. "Ignoring linkup on port:%d\n", ln->portid);
  1030. CSIO_INC_STATS(ln, n_evt_drop);
  1031. break;
  1032. case CSIO_LNE_FAB_INIT_DONE:
  1033. csio_set_state(&ln->sm, csio_lns_ready);
  1034. spin_unlock_irq(&hw->lock);
  1035. csio_lnode_async_event(ln, CSIO_LN_FC_LINKUP);
  1036. spin_lock_irq(&hw->lock);
  1037. break;
  1038. case CSIO_LNE_LINK_DOWN:
  1039. /* Fall through */
  1040. case CSIO_LNE_DOWN_LINK:
  1041. csio_set_state(&ln->sm, csio_lns_uninit);
  1042. if (csio_is_phys_ln(ln)) {
  1043. /* Remove FCF entry */
  1044. list_del_init(&ln->fcfinfo->list);
  1045. }
  1046. break;
  1047. default:
  1048. csio_ln_dbg(ln,
  1049. "unexp ln event %d recv from did:x%x in "
  1050. "ln state[uninit].\n", evt, ln->nport_id);
  1051. CSIO_INC_STATS(ln, n_evt_unexp);
  1052. break;
  1053. } /* switch event */
  1054. }
  1055. /*
  1056. * csio_lns_ready - The request in ready state.
  1057. * @ln - FCOE lnode.
  1058. * @evt - Event to be processed.
  1059. *
  1060. * Process the given lnode event which is currently in "ready" state.
  1061. * Invoked with HW lock held.
  1062. * Return - none.
  1063. */
  1064. static void
  1065. csio_lns_ready(struct csio_lnode *ln, enum csio_ln_ev evt)
  1066. {
  1067. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1068. CSIO_INC_STATS(ln, n_evt_sm[evt]);
  1069. switch (evt) {
  1070. case CSIO_LNE_FAB_INIT_DONE:
  1071. csio_ln_dbg(ln,
  1072. "ignoring event %d recv from did x%x"
  1073. "in ln state[ready].\n", evt, ln->nport_id);
  1074. CSIO_INC_STATS(ln, n_evt_drop);
  1075. break;
  1076. case CSIO_LNE_LINK_DOWN:
  1077. csio_set_state(&ln->sm, csio_lns_offline);
  1078. csio_post_event_rns(ln, CSIO_RNFE_DOWN);
  1079. spin_unlock_irq(&hw->lock);
  1080. csio_lnode_async_event(ln, CSIO_LN_FC_LINKDOWN);
  1081. spin_lock_irq(&hw->lock);
  1082. if (csio_is_phys_ln(ln)) {
  1083. /* Remove FCF entry */
  1084. list_del_init(&ln->fcfinfo->list);
  1085. }
  1086. break;
  1087. case CSIO_LNE_DOWN_LINK:
  1088. csio_set_state(&ln->sm, csio_lns_offline);
  1089. csio_post_event_rns(ln, CSIO_RNFE_DOWN);
  1090. /* Host need to issue aborts in case if FW has not returned
  1091. * WRs with status "ABORTED"
  1092. */
  1093. spin_unlock_irq(&hw->lock);
  1094. csio_lnode_async_event(ln, CSIO_LN_FC_LINKDOWN);
  1095. spin_lock_irq(&hw->lock);
  1096. if (csio_is_phys_ln(ln)) {
  1097. /* Remove FCF entry */
  1098. list_del_init(&ln->fcfinfo->list);
  1099. }
  1100. break;
  1101. case CSIO_LNE_CLOSE:
  1102. csio_set_state(&ln->sm, csio_lns_uninit);
  1103. csio_post_event_rns(ln, CSIO_RNFE_CLOSE);
  1104. break;
  1105. case CSIO_LNE_LOGO:
  1106. csio_set_state(&ln->sm, csio_lns_offline);
  1107. csio_post_event_rns(ln, CSIO_RNFE_DOWN);
  1108. break;
  1109. default:
  1110. csio_ln_dbg(ln,
  1111. "unexp ln event %d recv from did:x%x in "
  1112. "ln state[uninit].\n", evt, ln->nport_id);
  1113. CSIO_INC_STATS(ln, n_evt_unexp);
  1114. CSIO_DB_ASSERT(0);
  1115. break;
  1116. } /* switch event */
  1117. }
  1118. /*
  1119. * csio_lns_offline - The request in offline state.
  1120. * @ln - FCOE lnode.
  1121. * @evt - Event to be processed.
  1122. *
  1123. * Process the given lnode event which is currently in "offline" state.
  1124. * Invoked with HW lock held.
  1125. * Return - none.
  1126. */
  1127. static void
  1128. csio_lns_offline(struct csio_lnode *ln, enum csio_ln_ev evt)
  1129. {
  1130. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1131. struct csio_lnode *rln = hw->rln;
  1132. int rv;
  1133. CSIO_INC_STATS(ln, n_evt_sm[evt]);
  1134. switch (evt) {
  1135. case CSIO_LNE_LINKUP:
  1136. csio_set_state(&ln->sm, csio_lns_online);
  1137. /* Read FCF only for physical lnode */
  1138. if (csio_is_phys_ln(ln)) {
  1139. rv = csio_ln_read_fcf_entry(ln,
  1140. csio_ln_read_fcf_cbfn);
  1141. if (rv != 0) {
  1142. /* TODO: Send HW RESET event */
  1143. CSIO_INC_STATS(ln, n_err);
  1144. break;
  1145. }
  1146. /* Add FCF record */
  1147. list_add_tail(&ln->fcfinfo->list, &rln->fcf_lsthead);
  1148. }
  1149. rv = csio_ln_vnp_read(ln, csio_ln_vnp_read_cbfn);
  1150. if (rv != 0) {
  1151. /* TODO: Send HW RESET event */
  1152. CSIO_INC_STATS(ln, n_err);
  1153. }
  1154. break;
  1155. case CSIO_LNE_LINK_DOWN:
  1156. case CSIO_LNE_DOWN_LINK:
  1157. case CSIO_LNE_LOGO:
  1158. csio_ln_dbg(ln,
  1159. "ignoring event %d recv from did x%x"
  1160. "in ln state[offline].\n", evt, ln->nport_id);
  1161. CSIO_INC_STATS(ln, n_evt_drop);
  1162. break;
  1163. case CSIO_LNE_CLOSE:
  1164. csio_set_state(&ln->sm, csio_lns_uninit);
  1165. csio_post_event_rns(ln, CSIO_RNFE_CLOSE);
  1166. break;
  1167. default:
  1168. csio_ln_dbg(ln,
  1169. "unexp ln event %d recv from did:x%x in "
  1170. "ln state[offline]\n", evt, ln->nport_id);
  1171. CSIO_INC_STATS(ln, n_evt_unexp);
  1172. CSIO_DB_ASSERT(0);
  1173. break;
  1174. } /* switch event */
  1175. }
  1176. /*****************************************************************************/
  1177. /* END: Lnode SM */
  1178. /*****************************************************************************/
  1179. static void
  1180. csio_free_fcfinfo(struct kref *kref)
  1181. {
  1182. struct csio_fcf_info *fcfinfo = container_of(kref,
  1183. struct csio_fcf_info, kref);
  1184. kfree(fcfinfo);
  1185. }
  1186. /* Helper routines for attributes */
  1187. /*
  1188. * csio_lnode_state_to_str - Get current state of FCOE lnode.
  1189. * @ln - lnode
  1190. * @str - state of lnode.
  1191. *
  1192. */
  1193. void
  1194. csio_lnode_state_to_str(struct csio_lnode *ln, int8_t *str)
  1195. {
  1196. if (csio_get_state(ln) == ((csio_sm_state_t)csio_lns_uninit)) {
  1197. strcpy(str, "UNINIT");
  1198. return;
  1199. }
  1200. if (csio_get_state(ln) == ((csio_sm_state_t)csio_lns_ready)) {
  1201. strcpy(str, "READY");
  1202. return;
  1203. }
  1204. if (csio_get_state(ln) == ((csio_sm_state_t)csio_lns_offline)) {
  1205. strcpy(str, "OFFLINE");
  1206. return;
  1207. }
  1208. strcpy(str, "UNKNOWN");
  1209. } /* csio_lnode_state_to_str */
  1210. int
  1211. csio_get_phy_port_stats(struct csio_hw *hw, uint8_t portid,
  1212. struct fw_fcoe_port_stats *port_stats)
  1213. {
  1214. struct csio_mb *mbp;
  1215. struct fw_fcoe_port_cmd_params portparams;
  1216. enum fw_retval retval;
  1217. int idx;
  1218. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1219. if (!mbp) {
  1220. csio_err(hw, "FCoE FCF PARAMS command out of memory!\n");
  1221. return -EINVAL;
  1222. }
  1223. portparams.portid = portid;
  1224. for (idx = 1; idx <= 3; idx++) {
  1225. portparams.idx = (idx-1)*6 + 1;
  1226. portparams.nstats = 6;
  1227. if (idx == 3)
  1228. portparams.nstats = 4;
  1229. csio_fcoe_read_portparams_init_mb(hw, mbp, CSIO_MB_DEFAULT_TMO,
  1230. &portparams, NULL);
  1231. if (csio_mb_issue(hw, mbp)) {
  1232. csio_err(hw, "Issue of FCoE port params failed!\n");
  1233. mempool_free(mbp, hw->mb_mempool);
  1234. return -EINVAL;
  1235. }
  1236. csio_mb_process_portparams_rsp(hw, mbp, &retval,
  1237. &portparams, port_stats);
  1238. }
  1239. mempool_free(mbp, hw->mb_mempool);
  1240. return 0;
  1241. }
  1242. /*
  1243. * csio_ln_mgmt_wr_handler -Mgmt Work Request handler.
  1244. * @wr - WR.
  1245. * @len - WR len.
  1246. * This handler is invoked when an outstanding mgmt WR is completed.
  1247. * Its invoked in the context of FW event worker thread for every
  1248. * mgmt event received.
  1249. * Return - none.
  1250. */
  1251. static void
  1252. csio_ln_mgmt_wr_handler(struct csio_hw *hw, void *wr, uint32_t len)
  1253. {
  1254. struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
  1255. struct csio_ioreq *io_req = NULL;
  1256. struct fw_fcoe_els_ct_wr *wr_cmd;
  1257. wr_cmd = (struct fw_fcoe_els_ct_wr *) wr;
  1258. if (len < sizeof(struct fw_fcoe_els_ct_wr)) {
  1259. csio_err(mgmtm->hw,
  1260. "Invalid ELS CT WR length recvd, len:%x\n", len);
  1261. mgmtm->stats.n_err++;
  1262. return;
  1263. }
  1264. io_req = (struct csio_ioreq *) ((uintptr_t) wr_cmd->cookie);
  1265. io_req->wr_status = csio_wr_status(wr_cmd);
  1266. /* lookup ioreq exists in our active Q */
  1267. spin_lock_irq(&hw->lock);
  1268. if (csio_mgmt_req_lookup(mgmtm, io_req) != 0) {
  1269. csio_err(mgmtm->hw,
  1270. "Error- Invalid IO handle recv in WR. handle: %p\n",
  1271. io_req);
  1272. mgmtm->stats.n_err++;
  1273. spin_unlock_irq(&hw->lock);
  1274. return;
  1275. }
  1276. mgmtm = csio_hw_to_mgmtm(hw);
  1277. /* Dequeue from active queue */
  1278. list_del_init(&io_req->sm.sm_list);
  1279. mgmtm->stats.n_active--;
  1280. spin_unlock_irq(&hw->lock);
  1281. /* io_req will be freed by completion handler */
  1282. if (io_req->io_cbfn)
  1283. io_req->io_cbfn(hw, io_req);
  1284. }
  1285. /**
  1286. * csio_fcoe_fwevt_handler - Event handler for Firmware FCoE events.
  1287. * @hw: HW module
  1288. * @cpl_op: CPL opcode
  1289. * @cmd: FW cmd/WR.
  1290. *
  1291. * Process received FCoE cmd/WR event from FW.
  1292. */
  1293. void
  1294. csio_fcoe_fwevt_handler(struct csio_hw *hw, __u8 cpl_op, __be64 *cmd)
  1295. {
  1296. struct csio_lnode *ln;
  1297. struct csio_rnode *rn;
  1298. uint8_t portid, opcode = *(uint8_t *)cmd;
  1299. struct fw_fcoe_link_cmd *lcmd;
  1300. struct fw_wr_hdr *wr;
  1301. struct fw_rdev_wr *rdev_wr;
  1302. enum fw_fcoe_link_status lstatus;
  1303. uint32_t fcfi, rdev_flowid, vnpi;
  1304. enum csio_ln_ev evt;
  1305. if (cpl_op == CPL_FW6_MSG && opcode == FW_FCOE_LINK_CMD) {
  1306. lcmd = (struct fw_fcoe_link_cmd *)cmd;
  1307. lstatus = lcmd->lstatus;
  1308. portid = FW_FCOE_LINK_CMD_PORTID_GET(
  1309. ntohl(lcmd->op_to_portid));
  1310. fcfi = FW_FCOE_LINK_CMD_FCFI_GET(ntohl(lcmd->sub_opcode_fcfi));
  1311. vnpi = FW_FCOE_LINK_CMD_VNPI_GET(ntohl(lcmd->vnpi_pkd));
  1312. if (lstatus == FCOE_LINKUP) {
  1313. /* HW lock here */
  1314. spin_lock_irq(&hw->lock);
  1315. csio_handle_link_up(hw, portid, fcfi, vnpi);
  1316. spin_unlock_irq(&hw->lock);
  1317. /* HW un lock here */
  1318. } else if (lstatus == FCOE_LINKDOWN) {
  1319. /* HW lock here */
  1320. spin_lock_irq(&hw->lock);
  1321. csio_handle_link_down(hw, portid, fcfi, vnpi);
  1322. spin_unlock_irq(&hw->lock);
  1323. /* HW un lock here */
  1324. } else {
  1325. csio_warn(hw, "Unexpected FCOE LINK status:0x%x\n",
  1326. lcmd->lstatus);
  1327. CSIO_INC_STATS(hw, n_cpl_unexp);
  1328. }
  1329. } else if (cpl_op == CPL_FW6_PLD) {
  1330. wr = (struct fw_wr_hdr *) (cmd + 4);
  1331. if (FW_WR_OP_G(be32_to_cpu(wr->hi))
  1332. == FW_RDEV_WR) {
  1333. rdev_wr = (struct fw_rdev_wr *) (cmd + 4);
  1334. rdev_flowid = FW_RDEV_WR_FLOWID_GET(
  1335. ntohl(rdev_wr->alloc_to_len16));
  1336. vnpi = FW_RDEV_WR_ASSOC_FLOWID_GET(
  1337. ntohl(rdev_wr->flags_to_assoc_flowid));
  1338. csio_dbg(hw,
  1339. "FW_RDEV_WR: flowid:x%x ev_cause:x%x "
  1340. "vnpi:0x%x\n", rdev_flowid,
  1341. rdev_wr->event_cause, vnpi);
  1342. if (rdev_wr->protocol != PROT_FCOE) {
  1343. csio_err(hw,
  1344. "FW_RDEV_WR: invalid proto:x%x "
  1345. "received with flowid:x%x\n",
  1346. rdev_wr->protocol,
  1347. rdev_flowid);
  1348. CSIO_INC_STATS(hw, n_evt_drop);
  1349. return;
  1350. }
  1351. /* HW lock here */
  1352. spin_lock_irq(&hw->lock);
  1353. ln = csio_ln_lookup_by_vnpi(hw, vnpi);
  1354. if (!ln) {
  1355. csio_err(hw,
  1356. "FW_DEV_WR: invalid vnpi:x%x received "
  1357. "with flowid:x%x\n", vnpi, rdev_flowid);
  1358. CSIO_INC_STATS(hw, n_evt_drop);
  1359. goto out_pld;
  1360. }
  1361. rn = csio_confirm_rnode(ln, rdev_flowid,
  1362. &rdev_wr->u.fcoe_rdev);
  1363. if (!rn) {
  1364. csio_ln_dbg(ln,
  1365. "Failed to confirm rnode "
  1366. "for flowid:x%x\n", rdev_flowid);
  1367. CSIO_INC_STATS(hw, n_evt_drop);
  1368. goto out_pld;
  1369. }
  1370. /* save previous event for debugging */
  1371. ln->prev_evt = ln->cur_evt;
  1372. ln->cur_evt = rdev_wr->event_cause;
  1373. CSIO_INC_STATS(ln, n_evt_fw[rdev_wr->event_cause]);
  1374. /* Translate all the fabric events to lnode SM events */
  1375. evt = CSIO_FWE_TO_LNE(rdev_wr->event_cause);
  1376. if (evt) {
  1377. csio_ln_dbg(ln,
  1378. "Posting event to lnode event:%d "
  1379. "cause:%d flowid:x%x\n", evt,
  1380. rdev_wr->event_cause, rdev_flowid);
  1381. csio_post_event(&ln->sm, evt);
  1382. }
  1383. /* Handover event to rn SM here. */
  1384. csio_rnode_fwevt_handler(rn, rdev_wr->event_cause);
  1385. out_pld:
  1386. spin_unlock_irq(&hw->lock);
  1387. return;
  1388. } else {
  1389. csio_warn(hw, "unexpected WR op(0x%x) recv\n",
  1390. FW_WR_OP_G(be32_to_cpu((wr->hi))));
  1391. CSIO_INC_STATS(hw, n_cpl_unexp);
  1392. }
  1393. } else if (cpl_op == CPL_FW6_MSG) {
  1394. wr = (struct fw_wr_hdr *) (cmd);
  1395. if (FW_WR_OP_G(be32_to_cpu(wr->hi)) == FW_FCOE_ELS_CT_WR) {
  1396. csio_ln_mgmt_wr_handler(hw, wr,
  1397. sizeof(struct fw_fcoe_els_ct_wr));
  1398. } else {
  1399. csio_warn(hw, "unexpected WR op(0x%x) recv\n",
  1400. FW_WR_OP_G(be32_to_cpu((wr->hi))));
  1401. CSIO_INC_STATS(hw, n_cpl_unexp);
  1402. }
  1403. } else {
  1404. csio_warn(hw, "unexpected CPL op(0x%x) recv\n", opcode);
  1405. CSIO_INC_STATS(hw, n_cpl_unexp);
  1406. }
  1407. }
  1408. /**
  1409. * csio_lnode_start - Kickstart lnode discovery.
  1410. * @ln: lnode
  1411. *
  1412. * This routine kickstarts the discovery by issuing an FCOE_LINK (up) command.
  1413. */
  1414. int
  1415. csio_lnode_start(struct csio_lnode *ln)
  1416. {
  1417. int rv = 0;
  1418. if (csio_is_phys_ln(ln) && !(ln->flags & CSIO_LNF_LINK_ENABLE)) {
  1419. rv = csio_fcoe_enable_link(ln, 1);
  1420. ln->flags |= CSIO_LNF_LINK_ENABLE;
  1421. }
  1422. return rv;
  1423. }
  1424. /**
  1425. * csio_lnode_stop - Stop the lnode.
  1426. * @ln: lnode
  1427. *
  1428. * This routine is invoked by HW module to stop lnode and its associated NPIV
  1429. * lnodes.
  1430. */
  1431. void
  1432. csio_lnode_stop(struct csio_lnode *ln)
  1433. {
  1434. csio_post_event_lns(ln, CSIO_LNE_DOWN_LINK);
  1435. if (csio_is_phys_ln(ln) && (ln->flags & CSIO_LNF_LINK_ENABLE)) {
  1436. csio_fcoe_enable_link(ln, 0);
  1437. ln->flags &= ~CSIO_LNF_LINK_ENABLE;
  1438. }
  1439. csio_ln_dbg(ln, "stopping ln :%p\n", ln);
  1440. }
  1441. /**
  1442. * csio_lnode_close - Close an lnode.
  1443. * @ln: lnode
  1444. *
  1445. * This routine is invoked by HW module to close an lnode and its
  1446. * associated NPIV lnodes. Lnode and its associated NPIV lnodes are
  1447. * set to uninitialized state.
  1448. */
  1449. void
  1450. csio_lnode_close(struct csio_lnode *ln)
  1451. {
  1452. csio_post_event_lns(ln, CSIO_LNE_CLOSE);
  1453. if (csio_is_phys_ln(ln))
  1454. ln->vnp_flowid = CSIO_INVALID_IDX;
  1455. csio_ln_dbg(ln, "closed ln :%p\n", ln);
  1456. }
  1457. /*
  1458. * csio_ln_prep_ecwr - Prepare ELS/CT WR.
  1459. * @io_req - IO request.
  1460. * @wr_len - WR len
  1461. * @immd_len - WR immediate data
  1462. * @sub_op - Sub opcode
  1463. * @sid - source portid.
  1464. * @did - destination portid
  1465. * @flow_id - flowid
  1466. * @fw_wr - ELS/CT WR to be prepared.
  1467. * Returns: 0 - on success
  1468. */
  1469. static int
  1470. csio_ln_prep_ecwr(struct csio_ioreq *io_req, uint32_t wr_len,
  1471. uint32_t immd_len, uint8_t sub_op, uint32_t sid,
  1472. uint32_t did, uint32_t flow_id, uint8_t *fw_wr)
  1473. {
  1474. struct fw_fcoe_els_ct_wr *wr;
  1475. __be32 port_id;
  1476. wr = (struct fw_fcoe_els_ct_wr *)fw_wr;
  1477. wr->op_immdlen = cpu_to_be32(FW_WR_OP_V(FW_FCOE_ELS_CT_WR) |
  1478. FW_FCOE_ELS_CT_WR_IMMDLEN(immd_len));
  1479. wr_len = DIV_ROUND_UP(wr_len, 16);
  1480. wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID_V(flow_id) |
  1481. FW_WR_LEN16_V(wr_len));
  1482. wr->els_ct_type = sub_op;
  1483. wr->ctl_pri = 0;
  1484. wr->cp_en_class = 0;
  1485. wr->cookie = io_req->fw_handle;
  1486. wr->iqid = cpu_to_be16(csio_q_physiqid(
  1487. io_req->lnode->hwp, io_req->iq_idx));
  1488. wr->fl_to_sp = FW_FCOE_ELS_CT_WR_SP(1);
  1489. wr->tmo_val = (uint8_t) io_req->tmo;
  1490. port_id = htonl(sid);
  1491. memcpy(wr->l_id, PORT_ID_PTR(port_id), 3);
  1492. port_id = htonl(did);
  1493. memcpy(wr->r_id, PORT_ID_PTR(port_id), 3);
  1494. /* Prepare RSP SGL */
  1495. wr->rsp_dmalen = cpu_to_be32(io_req->dma_buf.len);
  1496. wr->rsp_dmaaddr = cpu_to_be64(io_req->dma_buf.paddr);
  1497. return 0;
  1498. }
  1499. /*
  1500. * csio_ln_mgmt_submit_wr - Post elsct work request.
  1501. * @mgmtm - mgmtm
  1502. * @io_req - io request.
  1503. * @sub_op - ELS or CT request type
  1504. * @pld - Dma Payload buffer
  1505. * @pld_len - Payload len
  1506. * Prepares ELSCT Work request and sents it to FW.
  1507. * Returns: 0 - on success
  1508. */
  1509. static int
  1510. csio_ln_mgmt_submit_wr(struct csio_mgmtm *mgmtm, struct csio_ioreq *io_req,
  1511. uint8_t sub_op, struct csio_dma_buf *pld,
  1512. uint32_t pld_len)
  1513. {
  1514. struct csio_wr_pair wrp;
  1515. struct csio_lnode *ln = io_req->lnode;
  1516. struct csio_rnode *rn = io_req->rnode;
  1517. struct csio_hw *hw = mgmtm->hw;
  1518. uint8_t fw_wr[64];
  1519. struct ulptx_sgl dsgl;
  1520. uint32_t wr_size = 0;
  1521. uint8_t im_len = 0;
  1522. uint32_t wr_off = 0;
  1523. int ret = 0;
  1524. /* Calculate WR Size for this ELS REQ */
  1525. wr_size = sizeof(struct fw_fcoe_els_ct_wr);
  1526. /* Send as immediate data if pld < 256 */
  1527. if (pld_len < 256) {
  1528. wr_size += ALIGN(pld_len, 8);
  1529. im_len = (uint8_t)pld_len;
  1530. } else
  1531. wr_size += sizeof(struct ulptx_sgl);
  1532. /* Roundup WR size in units of 16 bytes */
  1533. wr_size = ALIGN(wr_size, 16);
  1534. /* Get WR to send ELS REQ */
  1535. ret = csio_wr_get(hw, mgmtm->eq_idx, wr_size, &wrp);
  1536. if (ret != 0) {
  1537. csio_err(hw, "Failed to get WR for ec_req %p ret:%d\n",
  1538. io_req, ret);
  1539. return ret;
  1540. }
  1541. /* Prepare Generic WR used by all ELS/CT cmd */
  1542. csio_ln_prep_ecwr(io_req, wr_size, im_len, sub_op,
  1543. ln->nport_id, rn->nport_id,
  1544. csio_rn_flowid(rn),
  1545. &fw_wr[0]);
  1546. /* Copy ELS/CT WR CMD */
  1547. csio_wr_copy_to_wrp(&fw_wr[0], &wrp, wr_off,
  1548. sizeof(struct fw_fcoe_els_ct_wr));
  1549. wr_off += sizeof(struct fw_fcoe_els_ct_wr);
  1550. /* Copy payload to Immediate section of WR */
  1551. if (im_len)
  1552. csio_wr_copy_to_wrp(pld->vaddr, &wrp, wr_off, im_len);
  1553. else {
  1554. /* Program DSGL to dma payload */
  1555. dsgl.cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
  1556. ULPTX_MORE_F | ULPTX_NSGE_V(1));
  1557. dsgl.len0 = cpu_to_be32(pld_len);
  1558. dsgl.addr0 = cpu_to_be64(pld->paddr);
  1559. csio_wr_copy_to_wrp(&dsgl, &wrp, ALIGN(wr_off, 8),
  1560. sizeof(struct ulptx_sgl));
  1561. }
  1562. /* Issue work request to xmit ELS/CT req to FW */
  1563. csio_wr_issue(mgmtm->hw, mgmtm->eq_idx, false);
  1564. return ret;
  1565. }
  1566. /*
  1567. * csio_ln_mgmt_submit_req - Submit FCOE Mgmt request.
  1568. * @io_req - IO Request
  1569. * @io_cbfn - Completion handler.
  1570. * @req_type - ELS or CT request type
  1571. * @pld - Dma Payload buffer
  1572. * @pld_len - Payload len
  1573. *
  1574. *
  1575. * This API used submit managment ELS/CT request.
  1576. * This called with hw lock held
  1577. * Returns: 0 - on success
  1578. * -ENOMEM - on error.
  1579. */
  1580. static int
  1581. csio_ln_mgmt_submit_req(struct csio_ioreq *io_req,
  1582. void (*io_cbfn) (struct csio_hw *, struct csio_ioreq *),
  1583. enum fcoe_cmn_type req_type, struct csio_dma_buf *pld,
  1584. uint32_t pld_len)
  1585. {
  1586. struct csio_hw *hw = csio_lnode_to_hw(io_req->lnode);
  1587. struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
  1588. int rv;
  1589. BUG_ON(pld_len > pld->len);
  1590. io_req->io_cbfn = io_cbfn; /* Upper layer callback handler */
  1591. io_req->fw_handle = (uintptr_t) (io_req);
  1592. io_req->eq_idx = mgmtm->eq_idx;
  1593. io_req->iq_idx = mgmtm->iq_idx;
  1594. rv = csio_ln_mgmt_submit_wr(mgmtm, io_req, req_type, pld, pld_len);
  1595. if (rv == 0) {
  1596. list_add_tail(&io_req->sm.sm_list, &mgmtm->active_q);
  1597. mgmtm->stats.n_active++;
  1598. }
  1599. return rv;
  1600. }
  1601. /*
  1602. * csio_ln_fdmi_init - FDMI Init entry point.
  1603. * @ln: lnode
  1604. */
  1605. static int
  1606. csio_ln_fdmi_init(struct csio_lnode *ln)
  1607. {
  1608. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1609. struct csio_dma_buf *dma_buf;
  1610. /* Allocate MGMT request required for FDMI */
  1611. ln->mgmt_req = kzalloc(sizeof(struct csio_ioreq), GFP_KERNEL);
  1612. if (!ln->mgmt_req) {
  1613. csio_ln_err(ln, "Failed to alloc ioreq for FDMI\n");
  1614. CSIO_INC_STATS(hw, n_err_nomem);
  1615. return -ENOMEM;
  1616. }
  1617. /* Allocate Dma buffers for FDMI response Payload */
  1618. dma_buf = &ln->mgmt_req->dma_buf;
  1619. dma_buf->len = 2048;
  1620. dma_buf->vaddr = pci_alloc_consistent(hw->pdev, dma_buf->len,
  1621. &dma_buf->paddr);
  1622. if (!dma_buf->vaddr) {
  1623. csio_err(hw, "Failed to alloc DMA buffer for FDMI!\n");
  1624. kfree(ln->mgmt_req);
  1625. ln->mgmt_req = NULL;
  1626. return -ENOMEM;
  1627. }
  1628. ln->flags |= CSIO_LNF_FDMI_ENABLE;
  1629. return 0;
  1630. }
  1631. /*
  1632. * csio_ln_fdmi_exit - FDMI exit entry point.
  1633. * @ln: lnode
  1634. */
  1635. static int
  1636. csio_ln_fdmi_exit(struct csio_lnode *ln)
  1637. {
  1638. struct csio_dma_buf *dma_buf;
  1639. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1640. if (!ln->mgmt_req)
  1641. return 0;
  1642. dma_buf = &ln->mgmt_req->dma_buf;
  1643. if (dma_buf->vaddr)
  1644. pci_free_consistent(hw->pdev, dma_buf->len, dma_buf->vaddr,
  1645. dma_buf->paddr);
  1646. kfree(ln->mgmt_req);
  1647. return 0;
  1648. }
  1649. int
  1650. csio_scan_done(struct csio_lnode *ln, unsigned long ticks,
  1651. unsigned long time, unsigned long max_scan_ticks,
  1652. unsigned long delta_scan_ticks)
  1653. {
  1654. int rv = 0;
  1655. if (time >= max_scan_ticks)
  1656. return 1;
  1657. if (!ln->tgt_scan_tick)
  1658. ln->tgt_scan_tick = ticks;
  1659. if (((ticks - ln->tgt_scan_tick) >= delta_scan_ticks)) {
  1660. if (!ln->last_scan_ntgts)
  1661. ln->last_scan_ntgts = ln->n_scsi_tgts;
  1662. else {
  1663. if (ln->last_scan_ntgts == ln->n_scsi_tgts)
  1664. return 1;
  1665. ln->last_scan_ntgts = ln->n_scsi_tgts;
  1666. }
  1667. ln->tgt_scan_tick = ticks;
  1668. }
  1669. return rv;
  1670. }
  1671. /*
  1672. * csio_notify_lnodes:
  1673. * @hw: HW module
  1674. * @note: Notification
  1675. *
  1676. * Called from the HW SM to fan out notifications to the
  1677. * Lnode SM. Since the HW SM is entered with lock held,
  1678. * there is no need to hold locks here.
  1679. *
  1680. */
  1681. void
  1682. csio_notify_lnodes(struct csio_hw *hw, enum csio_ln_notify note)
  1683. {
  1684. struct list_head *tmp;
  1685. struct csio_lnode *ln;
  1686. csio_dbg(hw, "Notifying all nodes of event %d\n", note);
  1687. /* Traverse children lnodes list and send evt */
  1688. list_for_each(tmp, &hw->sln_head) {
  1689. ln = (struct csio_lnode *) tmp;
  1690. switch (note) {
  1691. case CSIO_LN_NOTIFY_HWREADY:
  1692. csio_lnode_start(ln);
  1693. break;
  1694. case CSIO_LN_NOTIFY_HWRESET:
  1695. case CSIO_LN_NOTIFY_HWREMOVE:
  1696. csio_lnode_close(ln);
  1697. break;
  1698. case CSIO_LN_NOTIFY_HWSTOP:
  1699. csio_lnode_stop(ln);
  1700. break;
  1701. default:
  1702. break;
  1703. }
  1704. }
  1705. }
  1706. /*
  1707. * csio_disable_lnodes:
  1708. * @hw: HW module
  1709. * @portid:port id
  1710. * @disable: disable/enable flag.
  1711. * If disable=1, disables all lnode hosted on given physical port.
  1712. * otherwise enables all the lnodes on given phsysical port.
  1713. * This routine need to called with hw lock held.
  1714. */
  1715. void
  1716. csio_disable_lnodes(struct csio_hw *hw, uint8_t portid, bool disable)
  1717. {
  1718. struct list_head *tmp;
  1719. struct csio_lnode *ln;
  1720. csio_dbg(hw, "Notifying event to all nodes of port:%d\n", portid);
  1721. /* Traverse sibling lnodes list and send evt */
  1722. list_for_each(tmp, &hw->sln_head) {
  1723. ln = (struct csio_lnode *) tmp;
  1724. if (ln->portid != portid)
  1725. continue;
  1726. if (disable)
  1727. csio_lnode_stop(ln);
  1728. else
  1729. csio_lnode_start(ln);
  1730. }
  1731. }
  1732. /*
  1733. * csio_ln_init - Initialize an lnode.
  1734. * @ln: lnode
  1735. *
  1736. */
  1737. static int
  1738. csio_ln_init(struct csio_lnode *ln)
  1739. {
  1740. int rv = -EINVAL;
  1741. struct csio_lnode *rln, *pln;
  1742. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1743. csio_init_state(&ln->sm, csio_lns_uninit);
  1744. ln->vnp_flowid = CSIO_INVALID_IDX;
  1745. ln->fcf_flowid = CSIO_INVALID_IDX;
  1746. if (csio_is_root_ln(ln)) {
  1747. /* This is the lnode used during initialization */
  1748. ln->fcfinfo = kzalloc(sizeof(struct csio_fcf_info), GFP_KERNEL);
  1749. if (!ln->fcfinfo) {
  1750. csio_ln_err(ln, "Failed to alloc FCF record\n");
  1751. CSIO_INC_STATS(hw, n_err_nomem);
  1752. goto err;
  1753. }
  1754. INIT_LIST_HEAD(&ln->fcf_lsthead);
  1755. kref_init(&ln->fcfinfo->kref);
  1756. if (csio_fdmi_enable && csio_ln_fdmi_init(ln))
  1757. goto err;
  1758. } else { /* Either a non-root physical or a virtual lnode */
  1759. /*
  1760. * THe rest is common for non-root physical and NPIV lnodes.
  1761. * Just get references to all other modules
  1762. */
  1763. rln = csio_root_lnode(ln);
  1764. if (csio_is_npiv_ln(ln)) {
  1765. /* NPIV */
  1766. pln = csio_parent_lnode(ln);
  1767. kref_get(&pln->fcfinfo->kref);
  1768. ln->fcfinfo = pln->fcfinfo;
  1769. } else {
  1770. /* Another non-root physical lnode (FCF) */
  1771. ln->fcfinfo = kzalloc(sizeof(struct csio_fcf_info),
  1772. GFP_KERNEL);
  1773. if (!ln->fcfinfo) {
  1774. csio_ln_err(ln, "Failed to alloc FCF info\n");
  1775. CSIO_INC_STATS(hw, n_err_nomem);
  1776. goto err;
  1777. }
  1778. kref_init(&ln->fcfinfo->kref);
  1779. if (csio_fdmi_enable && csio_ln_fdmi_init(ln))
  1780. goto err;
  1781. }
  1782. } /* if (!csio_is_root_ln(ln)) */
  1783. return 0;
  1784. err:
  1785. return rv;
  1786. }
  1787. static void
  1788. csio_ln_exit(struct csio_lnode *ln)
  1789. {
  1790. struct csio_lnode *pln;
  1791. csio_cleanup_rns(ln);
  1792. if (csio_is_npiv_ln(ln)) {
  1793. pln = csio_parent_lnode(ln);
  1794. kref_put(&pln->fcfinfo->kref, csio_free_fcfinfo);
  1795. } else {
  1796. kref_put(&ln->fcfinfo->kref, csio_free_fcfinfo);
  1797. if (csio_fdmi_enable)
  1798. csio_ln_fdmi_exit(ln);
  1799. }
  1800. ln->fcfinfo = NULL;
  1801. }
  1802. /**
  1803. * csio_lnode_init - Initialize the members of an lnode.
  1804. * @ln: lnode
  1805. *
  1806. */
  1807. int
  1808. csio_lnode_init(struct csio_lnode *ln, struct csio_hw *hw,
  1809. struct csio_lnode *pln)
  1810. {
  1811. int rv = -EINVAL;
  1812. /* Link this lnode to hw */
  1813. csio_lnode_to_hw(ln) = hw;
  1814. /* Link child to parent if child lnode */
  1815. if (pln)
  1816. ln->pln = pln;
  1817. else
  1818. ln->pln = NULL;
  1819. /* Initialize scsi_tgt and timers to zero */
  1820. ln->n_scsi_tgts = 0;
  1821. ln->last_scan_ntgts = 0;
  1822. ln->tgt_scan_tick = 0;
  1823. /* Initialize rnode list */
  1824. INIT_LIST_HEAD(&ln->rnhead);
  1825. INIT_LIST_HEAD(&ln->cln_head);
  1826. /* Initialize log level for debug */
  1827. ln->params.log_level = hw->params.log_level;
  1828. if (csio_ln_init(ln))
  1829. goto err;
  1830. /* Add lnode to list of sibling or children lnodes */
  1831. spin_lock_irq(&hw->lock);
  1832. list_add_tail(&ln->sm.sm_list, pln ? &pln->cln_head : &hw->sln_head);
  1833. if (pln)
  1834. pln->num_vports++;
  1835. spin_unlock_irq(&hw->lock);
  1836. hw->num_lns++;
  1837. return 0;
  1838. err:
  1839. csio_lnode_to_hw(ln) = NULL;
  1840. return rv;
  1841. }
  1842. /**
  1843. * csio_lnode_exit - De-instantiate an lnode.
  1844. * @ln: lnode
  1845. *
  1846. */
  1847. void
  1848. csio_lnode_exit(struct csio_lnode *ln)
  1849. {
  1850. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1851. csio_ln_exit(ln);
  1852. /* Remove this lnode from hw->sln_head */
  1853. spin_lock_irq(&hw->lock);
  1854. list_del_init(&ln->sm.sm_list);
  1855. /* If it is children lnode, decrement the
  1856. * counter in its parent lnode
  1857. */
  1858. if (ln->pln)
  1859. ln->pln->num_vports--;
  1860. /* Update root lnode pointer */
  1861. if (list_empty(&hw->sln_head))
  1862. hw->rln = NULL;
  1863. else
  1864. hw->rln = (struct csio_lnode *)csio_list_next(&hw->sln_head);
  1865. spin_unlock_irq(&hw->lock);
  1866. csio_lnode_to_hw(ln) = NULL;
  1867. hw->num_lns--;
  1868. }