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