csio_lnode.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152
  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 = 0;
  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. case CSIO_LNE_DOWN_LINK:
  1040. csio_set_state(&ln->sm, csio_lns_uninit);
  1041. if (csio_is_phys_ln(ln)) {
  1042. /* Remove FCF entry */
  1043. list_del_init(&ln->fcfinfo->list);
  1044. }
  1045. break;
  1046. default:
  1047. csio_ln_dbg(ln,
  1048. "unexp ln event %d recv from did:x%x in "
  1049. "ln state[uninit].\n", evt, ln->nport_id);
  1050. CSIO_INC_STATS(ln, n_evt_unexp);
  1051. break;
  1052. } /* switch event */
  1053. }
  1054. /*
  1055. * csio_lns_ready - The request in ready state.
  1056. * @ln - FCOE lnode.
  1057. * @evt - Event to be processed.
  1058. *
  1059. * Process the given lnode event which is currently in "ready" state.
  1060. * Invoked with HW lock held.
  1061. * Return - none.
  1062. */
  1063. static void
  1064. csio_lns_ready(struct csio_lnode *ln, enum csio_ln_ev evt)
  1065. {
  1066. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1067. CSIO_INC_STATS(ln, n_evt_sm[evt]);
  1068. switch (evt) {
  1069. case CSIO_LNE_FAB_INIT_DONE:
  1070. csio_ln_dbg(ln,
  1071. "ignoring event %d recv from did x%x"
  1072. "in ln state[ready].\n", evt, ln->nport_id);
  1073. CSIO_INC_STATS(ln, n_evt_drop);
  1074. break;
  1075. case CSIO_LNE_LINK_DOWN:
  1076. csio_set_state(&ln->sm, csio_lns_offline);
  1077. csio_post_event_rns(ln, CSIO_RNFE_DOWN);
  1078. spin_unlock_irq(&hw->lock);
  1079. csio_lnode_async_event(ln, CSIO_LN_FC_LINKDOWN);
  1080. spin_lock_irq(&hw->lock);
  1081. if (csio_is_phys_ln(ln)) {
  1082. /* Remove FCF entry */
  1083. list_del_init(&ln->fcfinfo->list);
  1084. }
  1085. break;
  1086. case CSIO_LNE_DOWN_LINK:
  1087. csio_set_state(&ln->sm, csio_lns_offline);
  1088. csio_post_event_rns(ln, CSIO_RNFE_DOWN);
  1089. /* Host need to issue aborts in case if FW has not returned
  1090. * WRs with status "ABORTED"
  1091. */
  1092. spin_unlock_irq(&hw->lock);
  1093. csio_lnode_async_event(ln, CSIO_LN_FC_LINKDOWN);
  1094. spin_lock_irq(&hw->lock);
  1095. if (csio_is_phys_ln(ln)) {
  1096. /* Remove FCF entry */
  1097. list_del_init(&ln->fcfinfo->list);
  1098. }
  1099. break;
  1100. case CSIO_LNE_CLOSE:
  1101. csio_set_state(&ln->sm, csio_lns_uninit);
  1102. csio_post_event_rns(ln, CSIO_RNFE_CLOSE);
  1103. break;
  1104. case CSIO_LNE_LOGO:
  1105. csio_set_state(&ln->sm, csio_lns_offline);
  1106. csio_post_event_rns(ln, CSIO_RNFE_DOWN);
  1107. break;
  1108. default:
  1109. csio_ln_dbg(ln,
  1110. "unexp ln event %d recv from did:x%x in "
  1111. "ln state[uninit].\n", evt, ln->nport_id);
  1112. CSIO_INC_STATS(ln, n_evt_unexp);
  1113. CSIO_DB_ASSERT(0);
  1114. break;
  1115. } /* switch event */
  1116. }
  1117. /*
  1118. * csio_lns_offline - The request in offline state.
  1119. * @ln - FCOE lnode.
  1120. * @evt - Event to be processed.
  1121. *
  1122. * Process the given lnode event which is currently in "offline" state.
  1123. * Invoked with HW lock held.
  1124. * Return - none.
  1125. */
  1126. static void
  1127. csio_lns_offline(struct csio_lnode *ln, enum csio_ln_ev evt)
  1128. {
  1129. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1130. struct csio_lnode *rln = hw->rln;
  1131. int rv;
  1132. CSIO_INC_STATS(ln, n_evt_sm[evt]);
  1133. switch (evt) {
  1134. case CSIO_LNE_LINKUP:
  1135. csio_set_state(&ln->sm, csio_lns_online);
  1136. /* Read FCF only for physical lnode */
  1137. if (csio_is_phys_ln(ln)) {
  1138. rv = csio_ln_read_fcf_entry(ln,
  1139. csio_ln_read_fcf_cbfn);
  1140. if (rv != 0) {
  1141. /* TODO: Send HW RESET event */
  1142. CSIO_INC_STATS(ln, n_err);
  1143. break;
  1144. }
  1145. /* Add FCF record */
  1146. list_add_tail(&ln->fcfinfo->list, &rln->fcf_lsthead);
  1147. }
  1148. rv = csio_ln_vnp_read(ln, csio_ln_vnp_read_cbfn);
  1149. if (rv != 0) {
  1150. /* TODO: Send HW RESET event */
  1151. CSIO_INC_STATS(ln, n_err);
  1152. }
  1153. break;
  1154. case CSIO_LNE_LINK_DOWN:
  1155. case CSIO_LNE_DOWN_LINK:
  1156. case CSIO_LNE_LOGO:
  1157. csio_ln_dbg(ln,
  1158. "ignoring event %d recv from did x%x"
  1159. "in ln state[offline].\n", evt, ln->nport_id);
  1160. CSIO_INC_STATS(ln, n_evt_drop);
  1161. break;
  1162. case CSIO_LNE_CLOSE:
  1163. csio_set_state(&ln->sm, csio_lns_uninit);
  1164. csio_post_event_rns(ln, CSIO_RNFE_CLOSE);
  1165. break;
  1166. default:
  1167. csio_ln_dbg(ln,
  1168. "unexp ln event %d recv from did:x%x in "
  1169. "ln state[offline]\n", evt, ln->nport_id);
  1170. CSIO_INC_STATS(ln, n_evt_unexp);
  1171. CSIO_DB_ASSERT(0);
  1172. break;
  1173. } /* switch event */
  1174. }
  1175. /*****************************************************************************/
  1176. /* END: Lnode SM */
  1177. /*****************************************************************************/
  1178. static void
  1179. csio_free_fcfinfo(struct kref *kref)
  1180. {
  1181. struct csio_fcf_info *fcfinfo = container_of(kref,
  1182. struct csio_fcf_info, kref);
  1183. kfree(fcfinfo);
  1184. }
  1185. /* Helper routines for attributes */
  1186. /*
  1187. * csio_lnode_state_to_str - Get current state of FCOE lnode.
  1188. * @ln - lnode
  1189. * @str - state of lnode.
  1190. *
  1191. */
  1192. void
  1193. csio_lnode_state_to_str(struct csio_lnode *ln, int8_t *str)
  1194. {
  1195. if (csio_get_state(ln) == ((csio_sm_state_t)csio_lns_uninit)) {
  1196. strcpy(str, "UNINIT");
  1197. return;
  1198. }
  1199. if (csio_get_state(ln) == ((csio_sm_state_t)csio_lns_ready)) {
  1200. strcpy(str, "READY");
  1201. return;
  1202. }
  1203. if (csio_get_state(ln) == ((csio_sm_state_t)csio_lns_offline)) {
  1204. strcpy(str, "OFFLINE");
  1205. return;
  1206. }
  1207. strcpy(str, "UNKNOWN");
  1208. } /* csio_lnode_state_to_str */
  1209. int
  1210. csio_get_phy_port_stats(struct csio_hw *hw, uint8_t portid,
  1211. struct fw_fcoe_port_stats *port_stats)
  1212. {
  1213. struct csio_mb *mbp;
  1214. struct fw_fcoe_port_cmd_params portparams;
  1215. enum fw_retval retval;
  1216. int idx;
  1217. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1218. if (!mbp) {
  1219. csio_err(hw, "FCoE FCF PARAMS command out of memory!\n");
  1220. return -EINVAL;
  1221. }
  1222. portparams.portid = portid;
  1223. for (idx = 1; idx <= 3; idx++) {
  1224. portparams.idx = (idx-1)*6 + 1;
  1225. portparams.nstats = 6;
  1226. if (idx == 3)
  1227. portparams.nstats = 4;
  1228. csio_fcoe_read_portparams_init_mb(hw, mbp, CSIO_MB_DEFAULT_TMO,
  1229. &portparams, NULL);
  1230. if (csio_mb_issue(hw, mbp)) {
  1231. csio_err(hw, "Issue of FCoE port params failed!\n");
  1232. mempool_free(mbp, hw->mb_mempool);
  1233. return -EINVAL;
  1234. }
  1235. csio_mb_process_portparams_rsp(hw, mbp, &retval,
  1236. &portparams, port_stats);
  1237. }
  1238. mempool_free(mbp, hw->mb_mempool);
  1239. return 0;
  1240. }
  1241. /*
  1242. * csio_ln_mgmt_wr_handler -Mgmt Work Request handler.
  1243. * @wr - WR.
  1244. * @len - WR len.
  1245. * This handler is invoked when an outstanding mgmt WR is completed.
  1246. * Its invoked in the context of FW event worker thread for every
  1247. * mgmt event received.
  1248. * Return - none.
  1249. */
  1250. static void
  1251. csio_ln_mgmt_wr_handler(struct csio_hw *hw, void *wr, uint32_t len)
  1252. {
  1253. struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
  1254. struct csio_ioreq *io_req = NULL;
  1255. struct fw_fcoe_els_ct_wr *wr_cmd;
  1256. wr_cmd = (struct fw_fcoe_els_ct_wr *) wr;
  1257. if (len < sizeof(struct fw_fcoe_els_ct_wr)) {
  1258. csio_err(mgmtm->hw,
  1259. "Invalid ELS CT WR length recvd, len:%x\n", len);
  1260. mgmtm->stats.n_err++;
  1261. return;
  1262. }
  1263. io_req = (struct csio_ioreq *) ((uintptr_t) wr_cmd->cookie);
  1264. io_req->wr_status = csio_wr_status(wr_cmd);
  1265. /* lookup ioreq exists in our active Q */
  1266. spin_lock_irq(&hw->lock);
  1267. if (csio_mgmt_req_lookup(mgmtm, io_req) != 0) {
  1268. csio_err(mgmtm->hw,
  1269. "Error- Invalid IO handle recv in WR. handle: %p\n",
  1270. io_req);
  1271. mgmtm->stats.n_err++;
  1272. spin_unlock_irq(&hw->lock);
  1273. return;
  1274. }
  1275. mgmtm = csio_hw_to_mgmtm(hw);
  1276. /* Dequeue from active queue */
  1277. list_del_init(&io_req->sm.sm_list);
  1278. mgmtm->stats.n_active--;
  1279. spin_unlock_irq(&hw->lock);
  1280. /* io_req will be freed by completion handler */
  1281. if (io_req->io_cbfn)
  1282. io_req->io_cbfn(hw, io_req);
  1283. }
  1284. /**
  1285. * csio_fcoe_fwevt_handler - Event handler for Firmware FCoE events.
  1286. * @hw: HW module
  1287. * @cpl_op: CPL opcode
  1288. * @cmd: FW cmd/WR.
  1289. *
  1290. * Process received FCoE cmd/WR event from FW.
  1291. */
  1292. void
  1293. csio_fcoe_fwevt_handler(struct csio_hw *hw, __u8 cpl_op, __be64 *cmd)
  1294. {
  1295. struct csio_lnode *ln;
  1296. struct csio_rnode *rn;
  1297. uint8_t portid, opcode = *(uint8_t *)cmd;
  1298. struct fw_fcoe_link_cmd *lcmd;
  1299. struct fw_wr_hdr *wr;
  1300. struct fw_rdev_wr *rdev_wr;
  1301. enum fw_fcoe_link_status lstatus;
  1302. uint32_t fcfi, rdev_flowid, vnpi;
  1303. enum csio_ln_ev evt;
  1304. if (cpl_op == CPL_FW6_MSG && opcode == FW_FCOE_LINK_CMD) {
  1305. lcmd = (struct fw_fcoe_link_cmd *)cmd;
  1306. lstatus = lcmd->lstatus;
  1307. portid = FW_FCOE_LINK_CMD_PORTID_GET(
  1308. ntohl(lcmd->op_to_portid));
  1309. fcfi = FW_FCOE_LINK_CMD_FCFI_GET(ntohl(lcmd->sub_opcode_fcfi));
  1310. vnpi = FW_FCOE_LINK_CMD_VNPI_GET(ntohl(lcmd->vnpi_pkd));
  1311. if (lstatus == FCOE_LINKUP) {
  1312. /* HW lock here */
  1313. spin_lock_irq(&hw->lock);
  1314. csio_handle_link_up(hw, portid, fcfi, vnpi);
  1315. spin_unlock_irq(&hw->lock);
  1316. /* HW un lock here */
  1317. } else if (lstatus == FCOE_LINKDOWN) {
  1318. /* HW lock here */
  1319. spin_lock_irq(&hw->lock);
  1320. csio_handle_link_down(hw, portid, fcfi, vnpi);
  1321. spin_unlock_irq(&hw->lock);
  1322. /* HW un lock here */
  1323. } else {
  1324. csio_warn(hw, "Unexpected FCOE LINK status:0x%x\n",
  1325. lcmd->lstatus);
  1326. CSIO_INC_STATS(hw, n_cpl_unexp);
  1327. }
  1328. } else if (cpl_op == CPL_FW6_PLD) {
  1329. wr = (struct fw_wr_hdr *) (cmd + 4);
  1330. if (FW_WR_OP_G(be32_to_cpu(wr->hi))
  1331. == FW_RDEV_WR) {
  1332. rdev_wr = (struct fw_rdev_wr *) (cmd + 4);
  1333. rdev_flowid = FW_RDEV_WR_FLOWID_GET(
  1334. ntohl(rdev_wr->alloc_to_len16));
  1335. vnpi = FW_RDEV_WR_ASSOC_FLOWID_GET(
  1336. ntohl(rdev_wr->flags_to_assoc_flowid));
  1337. csio_dbg(hw,
  1338. "FW_RDEV_WR: flowid:x%x ev_cause:x%x "
  1339. "vnpi:0x%x\n", rdev_flowid,
  1340. rdev_wr->event_cause, vnpi);
  1341. if (rdev_wr->protocol != PROT_FCOE) {
  1342. csio_err(hw,
  1343. "FW_RDEV_WR: invalid proto:x%x "
  1344. "received with flowid:x%x\n",
  1345. rdev_wr->protocol,
  1346. rdev_flowid);
  1347. CSIO_INC_STATS(hw, n_evt_drop);
  1348. return;
  1349. }
  1350. /* HW lock here */
  1351. spin_lock_irq(&hw->lock);
  1352. ln = csio_ln_lookup_by_vnpi(hw, vnpi);
  1353. if (!ln) {
  1354. csio_err(hw,
  1355. "FW_DEV_WR: invalid vnpi:x%x received "
  1356. "with flowid:x%x\n", vnpi, rdev_flowid);
  1357. CSIO_INC_STATS(hw, n_evt_drop);
  1358. goto out_pld;
  1359. }
  1360. rn = csio_confirm_rnode(ln, rdev_flowid,
  1361. &rdev_wr->u.fcoe_rdev);
  1362. if (!rn) {
  1363. csio_ln_dbg(ln,
  1364. "Failed to confirm rnode "
  1365. "for flowid:x%x\n", rdev_flowid);
  1366. CSIO_INC_STATS(hw, n_evt_drop);
  1367. goto out_pld;
  1368. }
  1369. /* save previous event for debugging */
  1370. ln->prev_evt = ln->cur_evt;
  1371. ln->cur_evt = rdev_wr->event_cause;
  1372. CSIO_INC_STATS(ln, n_evt_fw[rdev_wr->event_cause]);
  1373. /* Translate all the fabric events to lnode SM events */
  1374. evt = CSIO_FWE_TO_LNE(rdev_wr->event_cause);
  1375. if (evt) {
  1376. csio_ln_dbg(ln,
  1377. "Posting event to lnode event:%d "
  1378. "cause:%d flowid:x%x\n", evt,
  1379. rdev_wr->event_cause, rdev_flowid);
  1380. csio_post_event(&ln->sm, evt);
  1381. }
  1382. /* Handover event to rn SM here. */
  1383. csio_rnode_fwevt_handler(rn, rdev_wr->event_cause);
  1384. out_pld:
  1385. spin_unlock_irq(&hw->lock);
  1386. return;
  1387. } else {
  1388. csio_warn(hw, "unexpected WR op(0x%x) recv\n",
  1389. FW_WR_OP_G(be32_to_cpu((wr->hi))));
  1390. CSIO_INC_STATS(hw, n_cpl_unexp);
  1391. }
  1392. } else if (cpl_op == CPL_FW6_MSG) {
  1393. wr = (struct fw_wr_hdr *) (cmd);
  1394. if (FW_WR_OP_G(be32_to_cpu(wr->hi)) == FW_FCOE_ELS_CT_WR) {
  1395. csio_ln_mgmt_wr_handler(hw, wr,
  1396. sizeof(struct fw_fcoe_els_ct_wr));
  1397. } else {
  1398. csio_warn(hw, "unexpected WR op(0x%x) recv\n",
  1399. FW_WR_OP_G(be32_to_cpu((wr->hi))));
  1400. CSIO_INC_STATS(hw, n_cpl_unexp);
  1401. }
  1402. } else {
  1403. csio_warn(hw, "unexpected CPL op(0x%x) recv\n", opcode);
  1404. CSIO_INC_STATS(hw, n_cpl_unexp);
  1405. }
  1406. }
  1407. /**
  1408. * csio_lnode_start - Kickstart lnode discovery.
  1409. * @ln: lnode
  1410. *
  1411. * This routine kickstarts the discovery by issuing an FCOE_LINK (up) command.
  1412. */
  1413. int
  1414. csio_lnode_start(struct csio_lnode *ln)
  1415. {
  1416. int rv = 0;
  1417. if (csio_is_phys_ln(ln) && !(ln->flags & CSIO_LNF_LINK_ENABLE)) {
  1418. rv = csio_fcoe_enable_link(ln, 1);
  1419. ln->flags |= CSIO_LNF_LINK_ENABLE;
  1420. }
  1421. return rv;
  1422. }
  1423. /**
  1424. * csio_lnode_stop - Stop the lnode.
  1425. * @ln: lnode
  1426. *
  1427. * This routine is invoked by HW module to stop lnode and its associated NPIV
  1428. * lnodes.
  1429. */
  1430. void
  1431. csio_lnode_stop(struct csio_lnode *ln)
  1432. {
  1433. csio_post_event_lns(ln, CSIO_LNE_DOWN_LINK);
  1434. if (csio_is_phys_ln(ln) && (ln->flags & CSIO_LNF_LINK_ENABLE)) {
  1435. csio_fcoe_enable_link(ln, 0);
  1436. ln->flags &= ~CSIO_LNF_LINK_ENABLE;
  1437. }
  1438. csio_ln_dbg(ln, "stopping ln :%p\n", ln);
  1439. }
  1440. /**
  1441. * csio_lnode_close - Close an lnode.
  1442. * @ln: lnode
  1443. *
  1444. * This routine is invoked by HW module to close an lnode and its
  1445. * associated NPIV lnodes. Lnode and its associated NPIV lnodes are
  1446. * set to uninitialized state.
  1447. */
  1448. void
  1449. csio_lnode_close(struct csio_lnode *ln)
  1450. {
  1451. csio_post_event_lns(ln, CSIO_LNE_CLOSE);
  1452. if (csio_is_phys_ln(ln))
  1453. ln->vnp_flowid = CSIO_INVALID_IDX;
  1454. csio_ln_dbg(ln, "closed ln :%p\n", ln);
  1455. }
  1456. /*
  1457. * csio_ln_prep_ecwr - Prepare ELS/CT WR.
  1458. * @io_req - IO request.
  1459. * @wr_len - WR len
  1460. * @immd_len - WR immediate data
  1461. * @sub_op - Sub opcode
  1462. * @sid - source portid.
  1463. * @did - destination portid
  1464. * @flow_id - flowid
  1465. * @fw_wr - ELS/CT WR to be prepared.
  1466. * Returns: 0 - on success
  1467. */
  1468. static int
  1469. csio_ln_prep_ecwr(struct csio_ioreq *io_req, uint32_t wr_len,
  1470. uint32_t immd_len, uint8_t sub_op, uint32_t sid,
  1471. uint32_t did, uint32_t flow_id, uint8_t *fw_wr)
  1472. {
  1473. struct fw_fcoe_els_ct_wr *wr;
  1474. __be32 port_id;
  1475. wr = (struct fw_fcoe_els_ct_wr *)fw_wr;
  1476. wr->op_immdlen = cpu_to_be32(FW_WR_OP_V(FW_FCOE_ELS_CT_WR) |
  1477. FW_FCOE_ELS_CT_WR_IMMDLEN(immd_len));
  1478. wr_len = DIV_ROUND_UP(wr_len, 16);
  1479. wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID_V(flow_id) |
  1480. FW_WR_LEN16_V(wr_len));
  1481. wr->els_ct_type = sub_op;
  1482. wr->ctl_pri = 0;
  1483. wr->cp_en_class = 0;
  1484. wr->cookie = io_req->fw_handle;
  1485. wr->iqid = cpu_to_be16(csio_q_physiqid(
  1486. io_req->lnode->hwp, io_req->iq_idx));
  1487. wr->fl_to_sp = FW_FCOE_ELS_CT_WR_SP(1);
  1488. wr->tmo_val = (uint8_t) io_req->tmo;
  1489. port_id = htonl(sid);
  1490. memcpy(wr->l_id, PORT_ID_PTR(port_id), 3);
  1491. port_id = htonl(did);
  1492. memcpy(wr->r_id, PORT_ID_PTR(port_id), 3);
  1493. /* Prepare RSP SGL */
  1494. wr->rsp_dmalen = cpu_to_be32(io_req->dma_buf.len);
  1495. wr->rsp_dmaaddr = cpu_to_be64(io_req->dma_buf.paddr);
  1496. return 0;
  1497. }
  1498. /*
  1499. * csio_ln_mgmt_submit_wr - Post elsct work request.
  1500. * @mgmtm - mgmtm
  1501. * @io_req - io request.
  1502. * @sub_op - ELS or CT request type
  1503. * @pld - Dma Payload buffer
  1504. * @pld_len - Payload len
  1505. * Prepares ELSCT Work request and sents it to FW.
  1506. * Returns: 0 - on success
  1507. */
  1508. static int
  1509. csio_ln_mgmt_submit_wr(struct csio_mgmtm *mgmtm, struct csio_ioreq *io_req,
  1510. uint8_t sub_op, struct csio_dma_buf *pld,
  1511. uint32_t pld_len)
  1512. {
  1513. struct csio_wr_pair wrp;
  1514. struct csio_lnode *ln = io_req->lnode;
  1515. struct csio_rnode *rn = io_req->rnode;
  1516. struct csio_hw *hw = mgmtm->hw;
  1517. uint8_t fw_wr[64];
  1518. struct ulptx_sgl dsgl;
  1519. uint32_t wr_size = 0;
  1520. uint8_t im_len = 0;
  1521. uint32_t wr_off = 0;
  1522. int ret = 0;
  1523. /* Calculate WR Size for this ELS REQ */
  1524. wr_size = sizeof(struct fw_fcoe_els_ct_wr);
  1525. /* Send as immediate data if pld < 256 */
  1526. if (pld_len < 256) {
  1527. wr_size += ALIGN(pld_len, 8);
  1528. im_len = (uint8_t)pld_len;
  1529. } else
  1530. wr_size += sizeof(struct ulptx_sgl);
  1531. /* Roundup WR size in units of 16 bytes */
  1532. wr_size = ALIGN(wr_size, 16);
  1533. /* Get WR to send ELS REQ */
  1534. ret = csio_wr_get(hw, mgmtm->eq_idx, wr_size, &wrp);
  1535. if (ret != 0) {
  1536. csio_err(hw, "Failed to get WR for ec_req %p ret:%d\n",
  1537. io_req, ret);
  1538. return ret;
  1539. }
  1540. /* Prepare Generic WR used by all ELS/CT cmd */
  1541. csio_ln_prep_ecwr(io_req, wr_size, im_len, sub_op,
  1542. ln->nport_id, rn->nport_id,
  1543. csio_rn_flowid(rn),
  1544. &fw_wr[0]);
  1545. /* Copy ELS/CT WR CMD */
  1546. csio_wr_copy_to_wrp(&fw_wr[0], &wrp, wr_off,
  1547. sizeof(struct fw_fcoe_els_ct_wr));
  1548. wr_off += sizeof(struct fw_fcoe_els_ct_wr);
  1549. /* Copy payload to Immediate section of WR */
  1550. if (im_len)
  1551. csio_wr_copy_to_wrp(pld->vaddr, &wrp, wr_off, im_len);
  1552. else {
  1553. /* Program DSGL to dma payload */
  1554. dsgl.cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
  1555. ULPTX_MORE_F | ULPTX_NSGE_V(1));
  1556. dsgl.len0 = cpu_to_be32(pld_len);
  1557. dsgl.addr0 = cpu_to_be64(pld->paddr);
  1558. csio_wr_copy_to_wrp(&dsgl, &wrp, ALIGN(wr_off, 8),
  1559. sizeof(struct ulptx_sgl));
  1560. }
  1561. /* Issue work request to xmit ELS/CT req to FW */
  1562. csio_wr_issue(mgmtm->hw, mgmtm->eq_idx, false);
  1563. return ret;
  1564. }
  1565. /*
  1566. * csio_ln_mgmt_submit_req - Submit FCOE Mgmt request.
  1567. * @io_req - IO Request
  1568. * @io_cbfn - Completion handler.
  1569. * @req_type - ELS or CT request type
  1570. * @pld - Dma Payload buffer
  1571. * @pld_len - Payload len
  1572. *
  1573. *
  1574. * This API used submit managment ELS/CT request.
  1575. * This called with hw lock held
  1576. * Returns: 0 - on success
  1577. * -ENOMEM - on error.
  1578. */
  1579. static int
  1580. csio_ln_mgmt_submit_req(struct csio_ioreq *io_req,
  1581. void (*io_cbfn) (struct csio_hw *, struct csio_ioreq *),
  1582. enum fcoe_cmn_type req_type, struct csio_dma_buf *pld,
  1583. uint32_t pld_len)
  1584. {
  1585. struct csio_hw *hw = csio_lnode_to_hw(io_req->lnode);
  1586. struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
  1587. int rv;
  1588. BUG_ON(pld_len > pld->len);
  1589. io_req->io_cbfn = io_cbfn; /* Upper layer callback handler */
  1590. io_req->fw_handle = (uintptr_t) (io_req);
  1591. io_req->eq_idx = mgmtm->eq_idx;
  1592. io_req->iq_idx = mgmtm->iq_idx;
  1593. rv = csio_ln_mgmt_submit_wr(mgmtm, io_req, req_type, pld, pld_len);
  1594. if (rv == 0) {
  1595. list_add_tail(&io_req->sm.sm_list, &mgmtm->active_q);
  1596. mgmtm->stats.n_active++;
  1597. }
  1598. return rv;
  1599. }
  1600. /*
  1601. * csio_ln_fdmi_init - FDMI Init entry point.
  1602. * @ln: lnode
  1603. */
  1604. static int
  1605. csio_ln_fdmi_init(struct csio_lnode *ln)
  1606. {
  1607. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1608. struct csio_dma_buf *dma_buf;
  1609. /* Allocate MGMT request required for FDMI */
  1610. ln->mgmt_req = kzalloc(sizeof(struct csio_ioreq), GFP_KERNEL);
  1611. if (!ln->mgmt_req) {
  1612. csio_ln_err(ln, "Failed to alloc ioreq for FDMI\n");
  1613. CSIO_INC_STATS(hw, n_err_nomem);
  1614. return -ENOMEM;
  1615. }
  1616. /* Allocate Dma buffers for FDMI response Payload */
  1617. dma_buf = &ln->mgmt_req->dma_buf;
  1618. dma_buf->len = 2048;
  1619. dma_buf->vaddr = dma_alloc_coherent(&hw->pdev->dev, dma_buf->len,
  1620. &dma_buf->paddr, GFP_KERNEL);
  1621. if (!dma_buf->vaddr) {
  1622. csio_err(hw, "Failed to alloc DMA buffer for FDMI!\n");
  1623. kfree(ln->mgmt_req);
  1624. ln->mgmt_req = NULL;
  1625. return -ENOMEM;
  1626. }
  1627. ln->flags |= CSIO_LNF_FDMI_ENABLE;
  1628. return 0;
  1629. }
  1630. /*
  1631. * csio_ln_fdmi_exit - FDMI exit entry point.
  1632. * @ln: lnode
  1633. */
  1634. static int
  1635. csio_ln_fdmi_exit(struct csio_lnode *ln)
  1636. {
  1637. struct csio_dma_buf *dma_buf;
  1638. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1639. if (!ln->mgmt_req)
  1640. return 0;
  1641. dma_buf = &ln->mgmt_req->dma_buf;
  1642. if (dma_buf->vaddr)
  1643. dma_free_coherent(&hw->pdev->dev, dma_buf->len, dma_buf->vaddr,
  1644. dma_buf->paddr);
  1645. kfree(ln->mgmt_req);
  1646. return 0;
  1647. }
  1648. int
  1649. csio_scan_done(struct csio_lnode *ln, unsigned long ticks,
  1650. unsigned long time, unsigned long max_scan_ticks,
  1651. unsigned long delta_scan_ticks)
  1652. {
  1653. int rv = 0;
  1654. if (time >= max_scan_ticks)
  1655. return 1;
  1656. if (!ln->tgt_scan_tick)
  1657. ln->tgt_scan_tick = ticks;
  1658. if (((ticks - ln->tgt_scan_tick) >= delta_scan_ticks)) {
  1659. if (!ln->last_scan_ntgts)
  1660. ln->last_scan_ntgts = ln->n_scsi_tgts;
  1661. else {
  1662. if (ln->last_scan_ntgts == ln->n_scsi_tgts)
  1663. return 1;
  1664. ln->last_scan_ntgts = ln->n_scsi_tgts;
  1665. }
  1666. ln->tgt_scan_tick = ticks;
  1667. }
  1668. return rv;
  1669. }
  1670. /*
  1671. * csio_notify_lnodes:
  1672. * @hw: HW module
  1673. * @note: Notification
  1674. *
  1675. * Called from the HW SM to fan out notifications to the
  1676. * Lnode SM. Since the HW SM is entered with lock held,
  1677. * there is no need to hold locks here.
  1678. *
  1679. */
  1680. void
  1681. csio_notify_lnodes(struct csio_hw *hw, enum csio_ln_notify note)
  1682. {
  1683. struct list_head *tmp;
  1684. struct csio_lnode *ln;
  1685. csio_dbg(hw, "Notifying all nodes of event %d\n", note);
  1686. /* Traverse children lnodes list and send evt */
  1687. list_for_each(tmp, &hw->sln_head) {
  1688. ln = (struct csio_lnode *) tmp;
  1689. switch (note) {
  1690. case CSIO_LN_NOTIFY_HWREADY:
  1691. csio_lnode_start(ln);
  1692. break;
  1693. case CSIO_LN_NOTIFY_HWRESET:
  1694. case CSIO_LN_NOTIFY_HWREMOVE:
  1695. csio_lnode_close(ln);
  1696. break;
  1697. case CSIO_LN_NOTIFY_HWSTOP:
  1698. csio_lnode_stop(ln);
  1699. break;
  1700. default:
  1701. break;
  1702. }
  1703. }
  1704. }
  1705. /*
  1706. * csio_disable_lnodes:
  1707. * @hw: HW module
  1708. * @portid:port id
  1709. * @disable: disable/enable flag.
  1710. * If disable=1, disables all lnode hosted on given physical port.
  1711. * otherwise enables all the lnodes on given phsysical port.
  1712. * This routine need to called with hw lock held.
  1713. */
  1714. void
  1715. csio_disable_lnodes(struct csio_hw *hw, uint8_t portid, bool disable)
  1716. {
  1717. struct list_head *tmp;
  1718. struct csio_lnode *ln;
  1719. csio_dbg(hw, "Notifying event to all nodes of port:%d\n", portid);
  1720. /* Traverse sibling lnodes list and send evt */
  1721. list_for_each(tmp, &hw->sln_head) {
  1722. ln = (struct csio_lnode *) tmp;
  1723. if (ln->portid != portid)
  1724. continue;
  1725. if (disable)
  1726. csio_lnode_stop(ln);
  1727. else
  1728. csio_lnode_start(ln);
  1729. }
  1730. }
  1731. /*
  1732. * csio_ln_init - Initialize an lnode.
  1733. * @ln: lnode
  1734. *
  1735. */
  1736. static int
  1737. csio_ln_init(struct csio_lnode *ln)
  1738. {
  1739. int rv = -EINVAL;
  1740. struct csio_lnode *pln;
  1741. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1742. csio_init_state(&ln->sm, csio_lns_uninit);
  1743. ln->vnp_flowid = CSIO_INVALID_IDX;
  1744. ln->fcf_flowid = CSIO_INVALID_IDX;
  1745. if (csio_is_root_ln(ln)) {
  1746. /* This is the lnode used during initialization */
  1747. ln->fcfinfo = kzalloc(sizeof(struct csio_fcf_info), GFP_KERNEL);
  1748. if (!ln->fcfinfo) {
  1749. csio_ln_err(ln, "Failed to alloc FCF record\n");
  1750. CSIO_INC_STATS(hw, n_err_nomem);
  1751. goto err;
  1752. }
  1753. INIT_LIST_HEAD(&ln->fcf_lsthead);
  1754. kref_init(&ln->fcfinfo->kref);
  1755. if (csio_fdmi_enable && csio_ln_fdmi_init(ln))
  1756. goto err;
  1757. } else { /* Either a non-root physical or a virtual lnode */
  1758. /*
  1759. * THe rest is common for non-root physical and NPIV lnodes.
  1760. * Just get references to all other modules
  1761. */
  1762. if (csio_is_npiv_ln(ln)) {
  1763. /* NPIV */
  1764. pln = csio_parent_lnode(ln);
  1765. kref_get(&pln->fcfinfo->kref);
  1766. ln->fcfinfo = pln->fcfinfo;
  1767. } else {
  1768. /* Another non-root physical lnode (FCF) */
  1769. ln->fcfinfo = kzalloc(sizeof(struct csio_fcf_info),
  1770. GFP_KERNEL);
  1771. if (!ln->fcfinfo) {
  1772. csio_ln_err(ln, "Failed to alloc FCF info\n");
  1773. CSIO_INC_STATS(hw, n_err_nomem);
  1774. goto err;
  1775. }
  1776. kref_init(&ln->fcfinfo->kref);
  1777. if (csio_fdmi_enable && csio_ln_fdmi_init(ln))
  1778. goto err;
  1779. }
  1780. } /* if (!csio_is_root_ln(ln)) */
  1781. return 0;
  1782. err:
  1783. return rv;
  1784. }
  1785. static void
  1786. csio_ln_exit(struct csio_lnode *ln)
  1787. {
  1788. struct csio_lnode *pln;
  1789. csio_cleanup_rns(ln);
  1790. if (csio_is_npiv_ln(ln)) {
  1791. pln = csio_parent_lnode(ln);
  1792. kref_put(&pln->fcfinfo->kref, csio_free_fcfinfo);
  1793. } else {
  1794. kref_put(&ln->fcfinfo->kref, csio_free_fcfinfo);
  1795. if (csio_fdmi_enable)
  1796. csio_ln_fdmi_exit(ln);
  1797. }
  1798. ln->fcfinfo = NULL;
  1799. }
  1800. /*
  1801. * csio_lnode_init - Initialize the members of an lnode.
  1802. * @ln: lnode
  1803. */
  1804. int
  1805. csio_lnode_init(struct csio_lnode *ln, struct csio_hw *hw,
  1806. struct csio_lnode *pln)
  1807. {
  1808. int rv = -EINVAL;
  1809. /* Link this lnode to hw */
  1810. csio_lnode_to_hw(ln) = hw;
  1811. /* Link child to parent if child lnode */
  1812. if (pln)
  1813. ln->pln = pln;
  1814. else
  1815. ln->pln = NULL;
  1816. /* Initialize scsi_tgt and timers to zero */
  1817. ln->n_scsi_tgts = 0;
  1818. ln->last_scan_ntgts = 0;
  1819. ln->tgt_scan_tick = 0;
  1820. /* Initialize rnode list */
  1821. INIT_LIST_HEAD(&ln->rnhead);
  1822. INIT_LIST_HEAD(&ln->cln_head);
  1823. /* Initialize log level for debug */
  1824. ln->params.log_level = hw->params.log_level;
  1825. if (csio_ln_init(ln))
  1826. goto err;
  1827. /* Add lnode to list of sibling or children lnodes */
  1828. spin_lock_irq(&hw->lock);
  1829. list_add_tail(&ln->sm.sm_list, pln ? &pln->cln_head : &hw->sln_head);
  1830. if (pln)
  1831. pln->num_vports++;
  1832. spin_unlock_irq(&hw->lock);
  1833. hw->num_lns++;
  1834. return 0;
  1835. err:
  1836. csio_lnode_to_hw(ln) = NULL;
  1837. return rv;
  1838. }
  1839. /**
  1840. * csio_lnode_exit - De-instantiate an lnode.
  1841. * @ln: lnode
  1842. *
  1843. */
  1844. void
  1845. csio_lnode_exit(struct csio_lnode *ln)
  1846. {
  1847. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1848. csio_ln_exit(ln);
  1849. /* Remove this lnode from hw->sln_head */
  1850. spin_lock_irq(&hw->lock);
  1851. list_del_init(&ln->sm.sm_list);
  1852. /* If it is children lnode, decrement the
  1853. * counter in its parent lnode
  1854. */
  1855. if (ln->pln)
  1856. ln->pln->num_vports--;
  1857. /* Update root lnode pointer */
  1858. if (list_empty(&hw->sln_head))
  1859. hw->rln = NULL;
  1860. else
  1861. hw->rln = (struct csio_lnode *)csio_list_next(&hw->sln_head);
  1862. spin_unlock_irq(&hw->lock);
  1863. csio_lnode_to_hw(ln) = NULL;
  1864. hw->num_lns--;
  1865. }