Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
0004  * Copyright (c) 2014- QLogic Corporation.
0005  * All rights reserved
0006  * www.qlogic.com
0007  *
0008  * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
0009  */
0010 
0011 /*
0012  *  bfa_fcs.c BFA FCS main
0013  */
0014 
0015 #include "bfad_drv.h"
0016 #include "bfad_im.h"
0017 #include "bfa_fcs.h"
0018 #include "bfa_fcbuild.h"
0019 
0020 BFA_TRC_FILE(FCS, FCS);
0021 
0022 /*
0023  *  fcs_api BFA FCS API
0024  */
0025 
0026 static void
0027 bfa_fcs_exit_comp(void *fcs_cbarg)
0028 {
0029     struct bfa_fcs_s      *fcs = fcs_cbarg;
0030     struct bfad_s         *bfad = fcs->bfad;
0031 
0032     complete(&bfad->comp);
0033 }
0034 
0035 /*
0036  * fcs initialization, called once after bfa initialization is complete
0037  */
0038 void
0039 bfa_fcs_init(struct bfa_fcs_s *fcs)
0040 {
0041     bfa_sm_send_event(&fcs->fabric, BFA_FCS_FABRIC_SM_CREATE);
0042     bfa_trc(fcs, 0);
0043 }
0044 
0045 /*
0046  *  fcs_api BFA FCS API
0047  */
0048 
0049 /*
0050  * FCS update cfg - reset the pwwn/nwwn of fabric base logical port
0051  * with values learned during bfa_init firmware GETATTR REQ.
0052  */
0053 void
0054 bfa_fcs_update_cfg(struct bfa_fcs_s *fcs)
0055 {
0056     struct bfa_fcs_fabric_s *fabric = &fcs->fabric;
0057     struct bfa_lport_cfg_s *port_cfg = &fabric->bport.port_cfg;
0058     struct bfa_ioc_s *ioc = &fabric->fcs->bfa->ioc;
0059 
0060     port_cfg->nwwn = ioc->attr->nwwn;
0061     port_cfg->pwwn = ioc->attr->pwwn;
0062 }
0063 
0064 /*
0065  * Stop FCS operations.
0066  */
0067 void
0068 bfa_fcs_stop(struct bfa_fcs_s *fcs)
0069 {
0070     bfa_wc_init(&fcs->wc, bfa_fcs_exit_comp, fcs);
0071     bfa_wc_up(&fcs->wc);
0072     bfa_fcs_fabric_modstop(fcs);
0073     bfa_wc_wait(&fcs->wc);
0074 }
0075 
0076 /*
0077  * fcs pbc vport initialization
0078  */
0079 void
0080 bfa_fcs_pbc_vport_init(struct bfa_fcs_s *fcs)
0081 {
0082     int i, npbc_vports;
0083     struct bfi_pbc_vport_s pbc_vports[BFI_PBC_MAX_VPORTS];
0084 
0085     /* Initialize pbc vports */
0086     if (!fcs->min_cfg) {
0087         npbc_vports =
0088             bfa_iocfc_get_pbc_vports(fcs->bfa, pbc_vports);
0089         for (i = 0; i < npbc_vports; i++)
0090             bfa_fcb_pbc_vport_create(fcs->bfa->bfad, pbc_vports[i]);
0091     }
0092 }
0093 
0094 /*
0095  *  brief
0096  *      FCS driver details initialization.
0097  *
0098  *  param[in]       fcs     FCS instance
0099  *  param[in]       driver_info Driver Details
0100  *
0101  *  return None
0102  */
0103 void
0104 bfa_fcs_driver_info_init(struct bfa_fcs_s *fcs,
0105             struct bfa_fcs_driver_info_s *driver_info)
0106 {
0107 
0108     fcs->driver_info = *driver_info;
0109 
0110     bfa_fcs_fabric_psymb_init(&fcs->fabric);
0111     bfa_fcs_fabric_nsymb_init(&fcs->fabric);
0112 }
0113 
0114 /*
0115  *  brief
0116  *      FCS instance cleanup and exit.
0117  *
0118  *  param[in]       fcs         FCS instance
0119  *  return None
0120  */
0121 void
0122 bfa_fcs_exit(struct bfa_fcs_s *fcs)
0123 {
0124     bfa_wc_init(&fcs->wc, bfa_fcs_exit_comp, fcs);
0125     bfa_wc_up(&fcs->wc);
0126     bfa_trc(fcs, 0);
0127     bfa_lps_delete(fcs->fabric.lps);
0128     bfa_sm_send_event(&fcs->fabric, BFA_FCS_FABRIC_SM_DELETE);
0129     bfa_wc_wait(&fcs->wc);
0130 }
0131 
0132 /*
0133  * Fabric module implementation.
0134  */
0135 
0136 #define BFA_FCS_FABRIC_RETRY_DELAY  (2000)  /* Milliseconds */
0137 #define BFA_FCS_FABRIC_CLEANUP_DELAY    (10000) /* Milliseconds */
0138 
0139 #define bfa_fcs_fabric_set_opertype(__fabric) do {          \
0140     if (bfa_fcport_get_topology((__fabric)->fcs->bfa)       \
0141                 == BFA_PORT_TOPOLOGY_P2P) {     \
0142         if (fabric->fab_type == BFA_FCS_FABRIC_SWITCHED)    \
0143             (__fabric)->oper_type = BFA_PORT_TYPE_NPORT;    \
0144         else                            \
0145             (__fabric)->oper_type = BFA_PORT_TYPE_P2P;  \
0146     } else                              \
0147         (__fabric)->oper_type = BFA_PORT_TYPE_NLPORT;       \
0148 } while (0)
0149 
0150 /*
0151  * forward declarations
0152  */
0153 static void bfa_fcs_fabric_init(struct bfa_fcs_fabric_s *fabric);
0154 static void bfa_fcs_fabric_login(struct bfa_fcs_fabric_s *fabric);
0155 static void bfa_fcs_fabric_notify_online(struct bfa_fcs_fabric_s *fabric);
0156 static void bfa_fcs_fabric_notify_offline(struct bfa_fcs_fabric_s *fabric);
0157 static void bfa_fcs_fabric_delay(void *cbarg);
0158 static void bfa_fcs_fabric_delete(struct bfa_fcs_fabric_s *fabric);
0159 static void bfa_fcs_fabric_delete_comp(void *cbarg);
0160 static void bfa_fcs_fabric_stop(struct bfa_fcs_fabric_s *fabric);
0161 static void bfa_fcs_fabric_stop_comp(void *cbarg);
0162 static void bfa_fcs_fabric_process_uf(struct bfa_fcs_fabric_s *fabric,
0163                       struct fchs_s *fchs, u16 len);
0164 static void bfa_fcs_fabric_process_flogi(struct bfa_fcs_fabric_s *fabric,
0165                      struct fchs_s *fchs, u16 len);
0166 static void bfa_fcs_fabric_send_flogi_acc(struct bfa_fcs_fabric_s *fabric);
0167 static void bfa_fcs_fabric_flogiacc_comp(void *fcsarg,
0168                      struct bfa_fcxp_s *fcxp, void *cbarg,
0169                      bfa_status_t status,
0170                      u32 rsp_len,
0171                      u32 resid_len,
0172                      struct fchs_s *rspfchs);
0173 
0174 static void bfa_fcs_fabric_sm_uninit(struct bfa_fcs_fabric_s *fabric,
0175                      enum bfa_fcs_fabric_event event);
0176 static void bfa_fcs_fabric_sm_created(struct bfa_fcs_fabric_s *fabric,
0177                       enum bfa_fcs_fabric_event event);
0178 static void bfa_fcs_fabric_sm_linkdown(struct bfa_fcs_fabric_s *fabric,
0179                        enum bfa_fcs_fabric_event event);
0180 static void bfa_fcs_fabric_sm_flogi(struct bfa_fcs_fabric_s *fabric,
0181                     enum bfa_fcs_fabric_event event);
0182 static void bfa_fcs_fabric_sm_flogi_retry(struct bfa_fcs_fabric_s *fabric,
0183                           enum bfa_fcs_fabric_event event);
0184 static void bfa_fcs_fabric_sm_auth(struct bfa_fcs_fabric_s *fabric,
0185                        enum bfa_fcs_fabric_event event);
0186 static void bfa_fcs_fabric_sm_nofabric(struct bfa_fcs_fabric_s *fabric,
0187                        enum bfa_fcs_fabric_event event);
0188 static void bfa_fcs_fabric_sm_evfp(struct bfa_fcs_fabric_s *fabric,
0189                        enum bfa_fcs_fabric_event event);
0190 static void bfa_fcs_fabric_sm_evfp_done(struct bfa_fcs_fabric_s *fabric,
0191                         enum bfa_fcs_fabric_event event);
0192 static void bfa_fcs_fabric_sm_isolated(struct bfa_fcs_fabric_s *fabric,
0193                        enum bfa_fcs_fabric_event event);
0194 static void bfa_fcs_fabric_sm_deleting(struct bfa_fcs_fabric_s *fabric,
0195                        enum bfa_fcs_fabric_event event);
0196 static void bfa_fcs_fabric_sm_stopping(struct bfa_fcs_fabric_s *fabric,
0197                        enum bfa_fcs_fabric_event event);
0198 static void bfa_fcs_fabric_sm_cleanup(struct bfa_fcs_fabric_s *fabric,
0199                       enum bfa_fcs_fabric_event event);
0200 /*
0201  *   Beginning state before fabric creation.
0202  */
0203 static void
0204 bfa_fcs_fabric_sm_uninit(struct bfa_fcs_fabric_s *fabric,
0205              enum bfa_fcs_fabric_event event)
0206 {
0207     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0208     bfa_trc(fabric->fcs, event);
0209 
0210     switch (event) {
0211     case BFA_FCS_FABRIC_SM_CREATE:
0212         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_created);
0213         bfa_fcs_fabric_init(fabric);
0214         bfa_fcs_lport_init(&fabric->bport, &fabric->bport.port_cfg);
0215         break;
0216 
0217     case BFA_FCS_FABRIC_SM_LINK_UP:
0218     case BFA_FCS_FABRIC_SM_LINK_DOWN:
0219         break;
0220 
0221     default:
0222         bfa_sm_fault(fabric->fcs, event);
0223     }
0224 }
0225 
0226 /*
0227  *   Beginning state before fabric creation.
0228  */
0229 static void
0230 bfa_fcs_fabric_sm_created(struct bfa_fcs_fabric_s *fabric,
0231               enum bfa_fcs_fabric_event event)
0232 {
0233     struct bfa_s    *bfa = fabric->fcs->bfa;
0234 
0235     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0236     bfa_trc(fabric->fcs, event);
0237 
0238     switch (event) {
0239     case BFA_FCS_FABRIC_SM_START:
0240         if (!bfa_fcport_is_linkup(fabric->fcs->bfa)) {
0241             bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
0242             break;
0243         }
0244         if (bfa_fcport_get_topology(bfa) ==
0245                 BFA_PORT_TOPOLOGY_LOOP) {
0246             fabric->fab_type = BFA_FCS_FABRIC_LOOP;
0247             fabric->bport.pid = bfa_fcport_get_myalpa(bfa);
0248             fabric->bport.pid = bfa_hton3b(fabric->bport.pid);
0249             bfa_sm_set_state(fabric,
0250                     bfa_fcs_fabric_sm_online);
0251             bfa_fcs_fabric_set_opertype(fabric);
0252             bfa_fcs_lport_online(&fabric->bport);
0253         } else {
0254             bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_flogi);
0255             bfa_fcs_fabric_login(fabric);
0256         }
0257         break;
0258 
0259     case BFA_FCS_FABRIC_SM_LINK_UP:
0260     case BFA_FCS_FABRIC_SM_LINK_DOWN:
0261         break;
0262 
0263     case BFA_FCS_FABRIC_SM_DELETE:
0264         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
0265         bfa_fcs_fabric_delete(fabric);
0266         break;
0267 
0268     default:
0269         bfa_sm_fault(fabric->fcs, event);
0270     }
0271 }
0272 
0273 /*
0274  *   Link is down, awaiting LINK UP event from port. This is also the
0275  *   first state at fabric creation.
0276  */
0277 static void
0278 bfa_fcs_fabric_sm_linkdown(struct bfa_fcs_fabric_s *fabric,
0279                enum bfa_fcs_fabric_event event)
0280 {
0281     struct bfa_s    *bfa = fabric->fcs->bfa;
0282 
0283     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0284     bfa_trc(fabric->fcs, event);
0285 
0286     switch (event) {
0287     case BFA_FCS_FABRIC_SM_LINK_UP:
0288         if (bfa_fcport_get_topology(bfa) != BFA_PORT_TOPOLOGY_LOOP) {
0289             bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_flogi);
0290             bfa_fcs_fabric_login(fabric);
0291             break;
0292         }
0293         fabric->fab_type = BFA_FCS_FABRIC_LOOP;
0294         fabric->bport.pid = bfa_fcport_get_myalpa(bfa);
0295         fabric->bport.pid = bfa_hton3b(fabric->bport.pid);
0296         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_online);
0297         bfa_fcs_fabric_set_opertype(fabric);
0298         bfa_fcs_lport_online(&fabric->bport);
0299         break;
0300 
0301     case BFA_FCS_FABRIC_SM_RETRY_OP:
0302     case BFA_FCS_FABRIC_SM_LOOPBACK:
0303         break;
0304 
0305     case BFA_FCS_FABRIC_SM_DELETE:
0306         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
0307         bfa_fcs_fabric_delete(fabric);
0308         break;
0309 
0310     case BFA_FCS_FABRIC_SM_STOP:
0311         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_cleanup);
0312         bfa_fcs_fabric_stop(fabric);
0313         break;
0314 
0315     default:
0316         bfa_sm_fault(fabric->fcs, event);
0317     }
0318 }
0319 
0320 /*
0321  *   FLOGI is in progress, awaiting FLOGI reply.
0322  */
0323 static void
0324 bfa_fcs_fabric_sm_flogi(struct bfa_fcs_fabric_s *fabric,
0325             enum bfa_fcs_fabric_event event)
0326 {
0327     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0328     bfa_trc(fabric->fcs, event);
0329 
0330     switch (event) {
0331     case BFA_FCS_FABRIC_SM_CONT_OP:
0332 
0333         bfa_fcport_set_tx_bbcredit(fabric->fcs->bfa,
0334                        fabric->bb_credit);
0335         fabric->fab_type = BFA_FCS_FABRIC_SWITCHED;
0336 
0337         if (fabric->auth_reqd && fabric->is_auth) {
0338             bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_auth);
0339             bfa_trc(fabric->fcs, event);
0340         } else {
0341             bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_online);
0342             bfa_fcs_fabric_notify_online(fabric);
0343         }
0344         break;
0345 
0346     case BFA_FCS_FABRIC_SM_RETRY_OP:
0347         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_flogi_retry);
0348         bfa_timer_start(fabric->fcs->bfa, &fabric->delay_timer,
0349                 bfa_fcs_fabric_delay, fabric,
0350                 BFA_FCS_FABRIC_RETRY_DELAY);
0351         break;
0352 
0353     case BFA_FCS_FABRIC_SM_LOOPBACK:
0354         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_loopback);
0355         bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
0356         bfa_fcs_fabric_set_opertype(fabric);
0357         break;
0358 
0359     case BFA_FCS_FABRIC_SM_NO_FABRIC:
0360         fabric->fab_type = BFA_FCS_FABRIC_N2N;
0361         bfa_fcport_set_tx_bbcredit(fabric->fcs->bfa,
0362                        fabric->bb_credit);
0363         bfa_fcs_fabric_notify_online(fabric);
0364         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_nofabric);
0365         break;
0366 
0367     case BFA_FCS_FABRIC_SM_LINK_DOWN:
0368         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
0369         bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
0370         break;
0371 
0372     case BFA_FCS_FABRIC_SM_DELETE:
0373         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
0374         bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
0375         bfa_fcs_fabric_delete(fabric);
0376         break;
0377 
0378     default:
0379         bfa_sm_fault(fabric->fcs, event);
0380     }
0381 }
0382 
0383 
0384 static void
0385 bfa_fcs_fabric_sm_flogi_retry(struct bfa_fcs_fabric_s *fabric,
0386                   enum bfa_fcs_fabric_event event)
0387 {
0388     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0389     bfa_trc(fabric->fcs, event);
0390 
0391     switch (event) {
0392     case BFA_FCS_FABRIC_SM_DELAYED:
0393         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_flogi);
0394         bfa_fcs_fabric_login(fabric);
0395         break;
0396 
0397     case BFA_FCS_FABRIC_SM_LINK_DOWN:
0398         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
0399         bfa_timer_stop(&fabric->delay_timer);
0400         break;
0401 
0402     case BFA_FCS_FABRIC_SM_DELETE:
0403         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
0404         bfa_timer_stop(&fabric->delay_timer);
0405         bfa_fcs_fabric_delete(fabric);
0406         break;
0407 
0408     default:
0409         bfa_sm_fault(fabric->fcs, event);
0410     }
0411 }
0412 
0413 /*
0414  *   Authentication is in progress, awaiting authentication results.
0415  */
0416 static void
0417 bfa_fcs_fabric_sm_auth(struct bfa_fcs_fabric_s *fabric,
0418                enum bfa_fcs_fabric_event event)
0419 {
0420     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0421     bfa_trc(fabric->fcs, event);
0422 
0423     switch (event) {
0424     case BFA_FCS_FABRIC_SM_AUTH_FAILED:
0425         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_auth_failed);
0426         bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
0427         break;
0428 
0429     case BFA_FCS_FABRIC_SM_AUTH_SUCCESS:
0430         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_online);
0431         bfa_fcs_fabric_notify_online(fabric);
0432         break;
0433 
0434     case BFA_FCS_FABRIC_SM_PERF_EVFP:
0435         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_evfp);
0436         break;
0437 
0438     case BFA_FCS_FABRIC_SM_LINK_DOWN:
0439         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
0440         bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
0441         break;
0442 
0443     case BFA_FCS_FABRIC_SM_DELETE:
0444         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
0445         bfa_fcs_fabric_delete(fabric);
0446         break;
0447 
0448     default:
0449         bfa_sm_fault(fabric->fcs, event);
0450     }
0451 }
0452 
0453 /*
0454  *   Authentication failed
0455  */
0456 void
0457 bfa_fcs_fabric_sm_auth_failed(struct bfa_fcs_fabric_s *fabric,
0458                   enum bfa_fcs_fabric_event event)
0459 {
0460     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0461     bfa_trc(fabric->fcs, event);
0462 
0463     switch (event) {
0464     case BFA_FCS_FABRIC_SM_LINK_DOWN:
0465         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
0466         bfa_fcs_fabric_notify_offline(fabric);
0467         break;
0468 
0469     case BFA_FCS_FABRIC_SM_DELETE:
0470         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
0471         bfa_fcs_fabric_delete(fabric);
0472         break;
0473 
0474     default:
0475         bfa_sm_fault(fabric->fcs, event);
0476     }
0477 }
0478 
0479 /*
0480  *   Port is in loopback mode.
0481  */
0482 void
0483 bfa_fcs_fabric_sm_loopback(struct bfa_fcs_fabric_s *fabric,
0484                enum bfa_fcs_fabric_event event)
0485 {
0486     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0487     bfa_trc(fabric->fcs, event);
0488 
0489     switch (event) {
0490     case BFA_FCS_FABRIC_SM_LINK_DOWN:
0491         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
0492         bfa_fcs_fabric_notify_offline(fabric);
0493         break;
0494 
0495     case BFA_FCS_FABRIC_SM_DELETE:
0496         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
0497         bfa_fcs_fabric_delete(fabric);
0498         break;
0499 
0500     default:
0501         bfa_sm_fault(fabric->fcs, event);
0502     }
0503 }
0504 
0505 /*
0506  *   There is no attached fabric - private loop or NPort-to-NPort topology.
0507  */
0508 static void
0509 bfa_fcs_fabric_sm_nofabric(struct bfa_fcs_fabric_s *fabric,
0510                enum bfa_fcs_fabric_event event)
0511 {
0512     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0513     bfa_trc(fabric->fcs, event);
0514 
0515     switch (event) {
0516     case BFA_FCS_FABRIC_SM_LINK_DOWN:
0517         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
0518         bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
0519         bfa_fcs_fabric_notify_offline(fabric);
0520         break;
0521 
0522     case BFA_FCS_FABRIC_SM_DELETE:
0523         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
0524         bfa_fcs_fabric_delete(fabric);
0525         break;
0526 
0527     case BFA_FCS_FABRIC_SM_NO_FABRIC:
0528         bfa_trc(fabric->fcs, fabric->bb_credit);
0529         bfa_fcport_set_tx_bbcredit(fabric->fcs->bfa,
0530                        fabric->bb_credit);
0531         break;
0532 
0533     case BFA_FCS_FABRIC_SM_RETRY_OP:
0534         break;
0535 
0536     default:
0537         bfa_sm_fault(fabric->fcs, event);
0538     }
0539 }
0540 
0541 /*
0542  *   Fabric is online - normal operating state.
0543  */
0544 void
0545 bfa_fcs_fabric_sm_online(struct bfa_fcs_fabric_s *fabric,
0546              enum bfa_fcs_fabric_event event)
0547 {
0548     struct bfa_s    *bfa = fabric->fcs->bfa;
0549 
0550     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0551     bfa_trc(fabric->fcs, event);
0552 
0553     switch (event) {
0554     case BFA_FCS_FABRIC_SM_LINK_DOWN:
0555         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
0556         if (bfa_fcport_get_topology(bfa) == BFA_PORT_TOPOLOGY_LOOP) {
0557             bfa_fcs_lport_offline(&fabric->bport);
0558         } else {
0559             bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
0560             bfa_fcs_fabric_notify_offline(fabric);
0561         }
0562         break;
0563 
0564     case BFA_FCS_FABRIC_SM_DELETE:
0565         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
0566         bfa_fcs_fabric_delete(fabric);
0567         break;
0568 
0569     case BFA_FCS_FABRIC_SM_STOP:
0570         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_stopping);
0571         bfa_fcs_fabric_stop(fabric);
0572         break;
0573 
0574     case BFA_FCS_FABRIC_SM_AUTH_FAILED:
0575         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_auth_failed);
0576         bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
0577         break;
0578 
0579     case BFA_FCS_FABRIC_SM_AUTH_SUCCESS:
0580         break;
0581 
0582     default:
0583         bfa_sm_fault(fabric->fcs, event);
0584     }
0585 }
0586 
0587 /*
0588  *   Exchanging virtual fabric parameters.
0589  */
0590 static void
0591 bfa_fcs_fabric_sm_evfp(struct bfa_fcs_fabric_s *fabric,
0592                enum bfa_fcs_fabric_event event)
0593 {
0594     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0595     bfa_trc(fabric->fcs, event);
0596 
0597     switch (event) {
0598     case BFA_FCS_FABRIC_SM_CONT_OP:
0599         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_evfp_done);
0600         break;
0601 
0602     case BFA_FCS_FABRIC_SM_ISOLATE:
0603         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_isolated);
0604         break;
0605 
0606     default:
0607         bfa_sm_fault(fabric->fcs, event);
0608     }
0609 }
0610 
0611 /*
0612  *   EVFP exchange complete and VFT tagging is enabled.
0613  */
0614 static void
0615 bfa_fcs_fabric_sm_evfp_done(struct bfa_fcs_fabric_s *fabric,
0616                 enum bfa_fcs_fabric_event event)
0617 {
0618     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0619     bfa_trc(fabric->fcs, event);
0620 }
0621 
0622 /*
0623  *   Port is isolated after EVFP exchange due to VF_ID mismatch (N and F).
0624  */
0625 static void
0626 bfa_fcs_fabric_sm_isolated(struct bfa_fcs_fabric_s *fabric,
0627                enum bfa_fcs_fabric_event event)
0628 {
0629     struct bfad_s *bfad = (struct bfad_s *)fabric->fcs->bfad;
0630     char    pwwn_ptr[BFA_STRING_32];
0631 
0632     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0633     bfa_trc(fabric->fcs, event);
0634     wwn2str(pwwn_ptr, fabric->bport.port_cfg.pwwn);
0635 
0636     BFA_LOG(KERN_INFO, bfad, bfa_log_level,
0637         "Port is isolated due to VF_ID mismatch. "
0638         "PWWN: %s Port VF_ID: %04x switch port VF_ID: %04x.",
0639         pwwn_ptr, fabric->fcs->port_vfid,
0640         fabric->event_arg.swp_vfid);
0641 }
0642 
0643 /*
0644  *   Fabric is being deleted, awaiting vport delete completions.
0645  */
0646 static void
0647 bfa_fcs_fabric_sm_deleting(struct bfa_fcs_fabric_s *fabric,
0648                enum bfa_fcs_fabric_event event)
0649 {
0650     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0651     bfa_trc(fabric->fcs, event);
0652 
0653     switch (event) {
0654     case BFA_FCS_FABRIC_SM_DELCOMP:
0655         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_uninit);
0656         bfa_wc_down(&fabric->fcs->wc);
0657         break;
0658 
0659     case BFA_FCS_FABRIC_SM_LINK_UP:
0660         break;
0661 
0662     case BFA_FCS_FABRIC_SM_LINK_DOWN:
0663         bfa_fcs_fabric_notify_offline(fabric);
0664         break;
0665 
0666     default:
0667         bfa_sm_fault(fabric->fcs, event);
0668     }
0669 }
0670 
0671 /*
0672  * Fabric is being stopped, awaiting vport stop completions.
0673  */
0674 static void
0675 bfa_fcs_fabric_sm_stopping(struct bfa_fcs_fabric_s *fabric,
0676                enum bfa_fcs_fabric_event event)
0677 {
0678     struct bfa_s    *bfa = fabric->fcs->bfa;
0679 
0680     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0681     bfa_trc(fabric->fcs, event);
0682 
0683     switch (event) {
0684     case BFA_FCS_FABRIC_SM_STOPCOMP:
0685         if (bfa_fcport_get_topology(bfa) == BFA_PORT_TOPOLOGY_LOOP) {
0686             bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_created);
0687         } else {
0688             bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_cleanup);
0689             bfa_sm_send_event(fabric->lps, BFA_LPS_SM_LOGOUT);
0690         }
0691         break;
0692 
0693     case BFA_FCS_FABRIC_SM_LINK_UP:
0694         break;
0695 
0696     case BFA_FCS_FABRIC_SM_LINK_DOWN:
0697         if (bfa_fcport_get_topology(bfa) == BFA_PORT_TOPOLOGY_LOOP)
0698             bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_created);
0699         else
0700             bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_cleanup);
0701         break;
0702 
0703     default:
0704         bfa_sm_fault(fabric->fcs, event);
0705     }
0706 }
0707 
0708 /*
0709  * Fabric is being stopped, cleanup without FLOGO
0710  */
0711 static void
0712 bfa_fcs_fabric_sm_cleanup(struct bfa_fcs_fabric_s *fabric,
0713               enum bfa_fcs_fabric_event event)
0714 {
0715     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0716     bfa_trc(fabric->fcs, event);
0717 
0718     switch (event) {
0719     case BFA_FCS_FABRIC_SM_STOPCOMP:
0720     case BFA_FCS_FABRIC_SM_LOGOCOMP:
0721         bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_created);
0722         bfa_wc_down(&(fabric->fcs)->wc);
0723         break;
0724 
0725     case BFA_FCS_FABRIC_SM_LINK_DOWN:
0726         /*
0727          * Ignore - can get this event if we get notified about IOC down
0728          * before the fabric completion callbk is done.
0729          */
0730         break;
0731 
0732     default:
0733         bfa_sm_fault(fabric->fcs, event);
0734     }
0735 }
0736 
0737 /*
0738  *  fcs_fabric_private fabric private functions
0739  */
0740 
0741 static void
0742 bfa_fcs_fabric_init(struct bfa_fcs_fabric_s *fabric)
0743 {
0744     struct bfa_lport_cfg_s *port_cfg = &fabric->bport.port_cfg;
0745 
0746     port_cfg->roles = BFA_LPORT_ROLE_FCP_IM;
0747     port_cfg->nwwn = fabric->fcs->bfa->ioc.attr->nwwn;
0748     port_cfg->pwwn = fabric->fcs->bfa->ioc.attr->pwwn;
0749 }
0750 
0751 /*
0752  * Port Symbolic Name Creation for base port.
0753  */
0754 void
0755 bfa_fcs_fabric_psymb_init(struct bfa_fcs_fabric_s *fabric)
0756 {
0757     struct bfa_lport_cfg_s *port_cfg = &fabric->bport.port_cfg;
0758     char model[BFA_ADAPTER_MODEL_NAME_LEN] = {0};
0759     struct bfa_fcs_driver_info_s *driver_info = &fabric->fcs->driver_info;
0760 
0761     bfa_ioc_get_adapter_model(&fabric->fcs->bfa->ioc, model);
0762 
0763     /* Model name/number */
0764     strlcpy(port_cfg->sym_name.symname, model,
0765         BFA_SYMNAME_MAXLEN);
0766     strlcat(port_cfg->sym_name.symname, BFA_FCS_PORT_SYMBNAME_SEPARATOR,
0767         BFA_SYMNAME_MAXLEN);
0768 
0769     /* Driver Version */
0770     strlcat(port_cfg->sym_name.symname, driver_info->version,
0771         BFA_SYMNAME_MAXLEN);
0772     strlcat(port_cfg->sym_name.symname, BFA_FCS_PORT_SYMBNAME_SEPARATOR,
0773         BFA_SYMNAME_MAXLEN);
0774 
0775     /* Host machine name */
0776     strlcat(port_cfg->sym_name.symname,
0777         driver_info->host_machine_name,
0778         BFA_SYMNAME_MAXLEN);
0779     strlcat(port_cfg->sym_name.symname, BFA_FCS_PORT_SYMBNAME_SEPARATOR,
0780         BFA_SYMNAME_MAXLEN);
0781 
0782     /*
0783      * Host OS Info :
0784      * If OS Patch Info is not there, do not truncate any bytes from the
0785      * OS name string and instead copy the entire OS info string (64 bytes).
0786      */
0787     if (driver_info->host_os_patch[0] == '\0') {
0788         strlcat(port_cfg->sym_name.symname,
0789             driver_info->host_os_name,
0790             BFA_SYMNAME_MAXLEN);
0791         strlcat(port_cfg->sym_name.symname,
0792             BFA_FCS_PORT_SYMBNAME_SEPARATOR,
0793             BFA_SYMNAME_MAXLEN);
0794     } else {
0795         strlcat(port_cfg->sym_name.symname,
0796             driver_info->host_os_name,
0797             BFA_SYMNAME_MAXLEN);
0798         strlcat(port_cfg->sym_name.symname,
0799             BFA_FCS_PORT_SYMBNAME_SEPARATOR,
0800             BFA_SYMNAME_MAXLEN);
0801 
0802         /* Append host OS Patch Info */
0803         strlcat(port_cfg->sym_name.symname,
0804             driver_info->host_os_patch,
0805             BFA_SYMNAME_MAXLEN);
0806     }
0807 
0808     /* null terminate */
0809     port_cfg->sym_name.symname[BFA_SYMNAME_MAXLEN - 1] = 0;
0810 }
0811 
0812 /*
0813  * Node Symbolic Name Creation for base port and all vports
0814  */
0815 void
0816 bfa_fcs_fabric_nsymb_init(struct bfa_fcs_fabric_s *fabric)
0817 {
0818     struct bfa_lport_cfg_s *port_cfg = &fabric->bport.port_cfg;
0819     char model[BFA_ADAPTER_MODEL_NAME_LEN] = {0};
0820     struct bfa_fcs_driver_info_s *driver_info = &fabric->fcs->driver_info;
0821 
0822     bfa_ioc_get_adapter_model(&fabric->fcs->bfa->ioc, model);
0823 
0824     /* Model name/number */
0825     strlcpy(port_cfg->node_sym_name.symname, model,
0826         BFA_SYMNAME_MAXLEN);
0827     strlcat(port_cfg->node_sym_name.symname,
0828             BFA_FCS_PORT_SYMBNAME_SEPARATOR,
0829             BFA_SYMNAME_MAXLEN);
0830 
0831     /* Driver Version */
0832     strlcat(port_cfg->node_sym_name.symname, (char *)driver_info->version,
0833         BFA_SYMNAME_MAXLEN);
0834     strlcat(port_cfg->node_sym_name.symname,
0835             BFA_FCS_PORT_SYMBNAME_SEPARATOR,
0836             BFA_SYMNAME_MAXLEN);
0837 
0838     /* Host machine name */
0839     strlcat(port_cfg->node_sym_name.symname,
0840         driver_info->host_machine_name,
0841         BFA_SYMNAME_MAXLEN);
0842     strlcat(port_cfg->node_sym_name.symname,
0843             BFA_FCS_PORT_SYMBNAME_SEPARATOR,
0844             BFA_SYMNAME_MAXLEN);
0845 
0846     /* null terminate */
0847     port_cfg->node_sym_name.symname[BFA_SYMNAME_MAXLEN - 1] = 0;
0848 }
0849 
0850 /*
0851  * bfa lps login completion callback
0852  */
0853 void
0854 bfa_cb_lps_flogi_comp(void *bfad, void *uarg, bfa_status_t status)
0855 {
0856     struct bfa_fcs_fabric_s *fabric = uarg;
0857 
0858     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
0859     bfa_trc(fabric->fcs, status);
0860 
0861     switch (status) {
0862     case BFA_STATUS_OK:
0863         fabric->stats.flogi_accepts++;
0864         break;
0865 
0866     case BFA_STATUS_INVALID_MAC:
0867         /* Only for CNA */
0868         fabric->stats.flogi_acc_err++;
0869         bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_RETRY_OP);
0870 
0871         return;
0872 
0873     case BFA_STATUS_EPROTOCOL:
0874         switch (fabric->lps->ext_status) {
0875         case BFA_EPROTO_BAD_ACCEPT:
0876             fabric->stats.flogi_acc_err++;
0877             break;
0878 
0879         case BFA_EPROTO_UNKNOWN_RSP:
0880             fabric->stats.flogi_unknown_rsp++;
0881             break;
0882 
0883         default:
0884             break;
0885         }
0886         bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_RETRY_OP);
0887 
0888         return;
0889 
0890     case BFA_STATUS_FABRIC_RJT:
0891         fabric->stats.flogi_rejects++;
0892         bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_RETRY_OP);
0893         return;
0894 
0895     default:
0896         fabric->stats.flogi_rsp_err++;
0897         bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_RETRY_OP);
0898         return;
0899     }
0900 
0901     fabric->bb_credit = fabric->lps->pr_bbcred;
0902     bfa_trc(fabric->fcs, fabric->bb_credit);
0903 
0904     if (!(fabric->lps->brcd_switch))
0905         fabric->fabric_name =  fabric->lps->pr_nwwn;
0906 
0907     /*
0908      * Check port type. It should be 1 = F-port.
0909      */
0910     if (fabric->lps->fport) {
0911         fabric->bport.pid = fabric->lps->lp_pid;
0912         fabric->is_npiv = fabric->lps->npiv_en;
0913         fabric->is_auth = fabric->lps->auth_req;
0914         bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_CONT_OP);
0915     } else {
0916         /*
0917          * Nport-2-Nport direct attached
0918          */
0919         fabric->bport.port_topo.pn2n.rem_port_wwn =
0920             fabric->lps->pr_pwwn;
0921         fabric->fab_type = BFA_FCS_FABRIC_N2N;
0922         bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_NO_FABRIC);
0923     }
0924 
0925     bfa_trc(fabric->fcs, fabric->bport.pid);
0926     bfa_trc(fabric->fcs, fabric->is_npiv);
0927     bfa_trc(fabric->fcs, fabric->is_auth);
0928 }
0929 /*
0930  *      Allocate and send FLOGI.
0931  */
0932 static void
0933 bfa_fcs_fabric_login(struct bfa_fcs_fabric_s *fabric)
0934 {
0935     struct bfa_s        *bfa = fabric->fcs->bfa;
0936     struct bfa_lport_cfg_s  *pcfg = &fabric->bport.port_cfg;
0937     u8          alpa = 0;
0938 
0939 
0940     bfa_lps_flogi(fabric->lps, fabric, alpa, bfa_fcport_get_maxfrsize(bfa),
0941               pcfg->pwwn, pcfg->nwwn, fabric->auth_reqd);
0942 
0943     fabric->stats.flogi_sent++;
0944 }
0945 
0946 static void
0947 bfa_fcs_fabric_notify_online(struct bfa_fcs_fabric_s *fabric)
0948 {
0949     struct bfa_fcs_vport_s *vport;
0950     struct list_head          *qe, *qen;
0951 
0952     bfa_trc(fabric->fcs, fabric->fabric_name);
0953 
0954     bfa_fcs_fabric_set_opertype(fabric);
0955     fabric->stats.fabric_onlines++;
0956 
0957     /*
0958      * notify online event to base and then virtual ports
0959      */
0960     bfa_fcs_lport_online(&fabric->bport);
0961 
0962     list_for_each_safe(qe, qen, &fabric->vport_q) {
0963         vport = (struct bfa_fcs_vport_s *) qe;
0964         bfa_fcs_vport_online(vport);
0965     }
0966 }
0967 
0968 static void
0969 bfa_fcs_fabric_notify_offline(struct bfa_fcs_fabric_s *fabric)
0970 {
0971     struct bfa_fcs_vport_s *vport;
0972     struct list_head          *qe, *qen;
0973 
0974     bfa_trc(fabric->fcs, fabric->fabric_name);
0975     fabric->stats.fabric_offlines++;
0976 
0977     /*
0978      * notify offline event first to vports and then base port.
0979      */
0980     list_for_each_safe(qe, qen, &fabric->vport_q) {
0981         vport = (struct bfa_fcs_vport_s *) qe;
0982         bfa_fcs_vport_offline(vport);
0983     }
0984 
0985     bfa_fcs_lport_offline(&fabric->bport);
0986 
0987     fabric->fabric_name = 0;
0988     fabric->fabric_ip_addr[0] = 0;
0989 }
0990 
0991 static void
0992 bfa_fcs_fabric_delay(void *cbarg)
0993 {
0994     struct bfa_fcs_fabric_s *fabric = cbarg;
0995 
0996     bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_DELAYED);
0997 }
0998 
0999 /*
1000  * Stop all vports and wait for vport stop completions.
1001  */
1002 static void
1003 bfa_fcs_fabric_stop(struct bfa_fcs_fabric_s *fabric)
1004 {
1005     struct bfa_fcs_vport_s *vport;
1006     struct list_head    *qe, *qen;
1007 
1008     bfa_wc_init(&fabric->stop_wc, bfa_fcs_fabric_stop_comp, fabric);
1009 
1010     list_for_each_safe(qe, qen, &fabric->vport_q) {
1011         vport = (struct bfa_fcs_vport_s *) qe;
1012         bfa_wc_up(&fabric->stop_wc);
1013         bfa_fcs_vport_fcs_stop(vport);
1014     }
1015 
1016     bfa_wc_up(&fabric->stop_wc);
1017     bfa_fcs_lport_stop(&fabric->bport);
1018     bfa_wc_wait(&fabric->stop_wc);
1019 }
1020 
1021 /*
1022  * Delete all vports and wait for vport delete completions.
1023  */
1024 static void
1025 bfa_fcs_fabric_delete(struct bfa_fcs_fabric_s *fabric)
1026 {
1027     struct bfa_fcs_vport_s *vport;
1028     struct list_head          *qe, *qen;
1029 
1030     list_for_each_safe(qe, qen, &fabric->vport_q) {
1031         vport = (struct bfa_fcs_vport_s *) qe;
1032         bfa_fcs_vport_fcs_delete(vport);
1033     }
1034 
1035     bfa_fcs_lport_delete(&fabric->bport);
1036     bfa_wc_wait(&fabric->wc);
1037 }
1038 
1039 static void
1040 bfa_fcs_fabric_delete_comp(void *cbarg)
1041 {
1042     struct bfa_fcs_fabric_s *fabric = cbarg;
1043 
1044     bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_DELCOMP);
1045 }
1046 
1047 static void
1048 bfa_fcs_fabric_stop_comp(void *cbarg)
1049 {
1050     struct bfa_fcs_fabric_s *fabric = cbarg;
1051 
1052     bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_STOPCOMP);
1053 }
1054 
1055 /*
1056  *  fcs_fabric_public fabric public functions
1057  */
1058 
1059 /*
1060  * Fabric module stop -- stop FCS actions
1061  */
1062 void
1063 bfa_fcs_fabric_modstop(struct bfa_fcs_s *fcs)
1064 {
1065     struct bfa_fcs_fabric_s *fabric;
1066 
1067     bfa_trc(fcs, 0);
1068     fabric = &fcs->fabric;
1069     bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_STOP);
1070 }
1071 
1072 /*
1073  * Fabric module start -- kick starts FCS actions
1074  */
1075 void
1076 bfa_fcs_fabric_modstart(struct bfa_fcs_s *fcs)
1077 {
1078     struct bfa_fcs_fabric_s *fabric;
1079 
1080     bfa_trc(fcs, 0);
1081     fabric = &fcs->fabric;
1082     bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_START);
1083 }
1084 
1085 
1086 /*
1087  *   Link up notification from BFA physical port module.
1088  */
1089 void
1090 bfa_fcs_fabric_link_up(struct bfa_fcs_fabric_s *fabric)
1091 {
1092     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
1093     bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_LINK_UP);
1094 }
1095 
1096 /*
1097  *   Link down notification from BFA physical port module.
1098  */
1099 void
1100 bfa_fcs_fabric_link_down(struct bfa_fcs_fabric_s *fabric)
1101 {
1102     bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
1103     bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_LINK_DOWN);
1104 }
1105 
1106 /*
1107  *   A child vport is being created in the fabric.
1108  *
1109  *   Call from vport module at vport creation. A list of base port and vports
1110  *   belonging to a fabric is maintained to propagate link events.
1111  *
1112  *   param[in] fabric - Fabric instance. This can be a base fabric or vf.
1113  *   param[in] vport  - Vport being created.
1114  *
1115  *   @return None (always succeeds)
1116  */
1117 void
1118 bfa_fcs_fabric_addvport(struct bfa_fcs_fabric_s *fabric,
1119             struct bfa_fcs_vport_s *vport)
1120 {
1121     /*
1122      * - add vport to fabric's vport_q
1123      */
1124     bfa_trc(fabric->fcs, fabric->vf_id);
1125 
1126     list_add_tail(&vport->qe, &fabric->vport_q);
1127     fabric->num_vports++;
1128     bfa_wc_up(&fabric->wc);
1129 }
1130 
1131 /*
1132  *   A child vport is being deleted from fabric.
1133  *
1134  *   Vport is being deleted.
1135  */
1136 void
1137 bfa_fcs_fabric_delvport(struct bfa_fcs_fabric_s *fabric,
1138             struct bfa_fcs_vport_s *vport)
1139 {
1140     list_del(&vport->qe);
1141     fabric->num_vports--;
1142     bfa_wc_down(&fabric->wc);
1143 }
1144 
1145 
1146 /*
1147  * Lookup for a vport within a fabric given its pwwn
1148  */
1149 struct bfa_fcs_vport_s *
1150 bfa_fcs_fabric_vport_lookup(struct bfa_fcs_fabric_s *fabric, wwn_t pwwn)
1151 {
1152     struct bfa_fcs_vport_s *vport;
1153     struct list_head          *qe;
1154 
1155     list_for_each(qe, &fabric->vport_q) {
1156         vport = (struct bfa_fcs_vport_s *) qe;
1157         if (bfa_fcs_lport_get_pwwn(&vport->lport) == pwwn)
1158             return vport;
1159     }
1160 
1161     return NULL;
1162 }
1163 
1164 
1165 /*
1166  *  Get OUI of the attached switch.
1167  *
1168  *  Note : Use of this function should be avoided as much as possible.
1169  *         This function should be used only if there is any requirement
1170 *          to check for FOS version below 6.3.
1171  *         To check if the attached fabric is a brocade fabric, use
1172  *         bfa_lps_is_brcd_fabric() which works for FOS versions 6.3
1173  *         or above only.
1174  */
1175 
1176 u16
1177 bfa_fcs_fabric_get_switch_oui(struct bfa_fcs_fabric_s *fabric)
1178 {
1179     wwn_t fab_nwwn;
1180     u8 *tmp;
1181     u16 oui;
1182 
1183     fab_nwwn = fabric->lps->pr_nwwn;
1184 
1185     tmp = (u8 *)&fab_nwwn;
1186     oui = (tmp[3] << 8) | tmp[4];
1187 
1188     return oui;
1189 }
1190 /*
1191  *      Unsolicited frame receive handling.
1192  */
1193 void
1194 bfa_fcs_fabric_uf_recv(struct bfa_fcs_fabric_s *fabric, struct fchs_s *fchs,
1195                u16 len)
1196 {
1197     u32 pid = fchs->d_id;
1198     struct bfa_fcs_vport_s *vport;
1199     struct list_head          *qe;
1200     struct fc_els_cmd_s *els_cmd = (struct fc_els_cmd_s *) (fchs + 1);
1201     struct fc_logi_s *flogi = (struct fc_logi_s *) els_cmd;
1202 
1203     bfa_trc(fabric->fcs, len);
1204     bfa_trc(fabric->fcs, pid);
1205 
1206     /*
1207      * Look for our own FLOGI frames being looped back. This means an
1208      * external loopback cable is in place. Our own FLOGI frames are
1209      * sometimes looped back when switch port gets temporarily bypassed.
1210      */
1211     if ((pid == bfa_ntoh3b(FC_FABRIC_PORT)) &&
1212         (els_cmd->els_code == FC_ELS_FLOGI) &&
1213         (flogi->port_name == bfa_fcs_lport_get_pwwn(&fabric->bport))) {
1214         bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_LOOPBACK);
1215         return;
1216     }
1217 
1218     /*
1219      * FLOGI/EVFP exchanges should be consumed by base fabric.
1220      */
1221     if (fchs->d_id == bfa_hton3b(FC_FABRIC_PORT)) {
1222         bfa_trc(fabric->fcs, pid);
1223         bfa_fcs_fabric_process_uf(fabric, fchs, len);
1224         return;
1225     }
1226 
1227     if (fabric->bport.pid == pid) {
1228         /*
1229          * All authentication frames should be routed to auth
1230          */
1231         bfa_trc(fabric->fcs, els_cmd->els_code);
1232         if (els_cmd->els_code == FC_ELS_AUTH) {
1233             bfa_trc(fabric->fcs, els_cmd->els_code);
1234             return;
1235         }
1236 
1237         bfa_trc(fabric->fcs, *(u8 *) ((u8 *) fchs));
1238         bfa_fcs_lport_uf_recv(&fabric->bport, fchs, len);
1239         return;
1240     }
1241 
1242     /*
1243      * look for a matching local port ID
1244      */
1245     list_for_each(qe, &fabric->vport_q) {
1246         vport = (struct bfa_fcs_vport_s *) qe;
1247         if (vport->lport.pid == pid) {
1248             bfa_fcs_lport_uf_recv(&vport->lport, fchs, len);
1249             return;
1250         }
1251     }
1252 
1253     if (!bfa_fcs_fabric_is_switched(fabric))
1254         bfa_fcs_lport_uf_recv(&fabric->bport, fchs, len);
1255 
1256     bfa_trc(fabric->fcs, fchs->type);
1257 }
1258 
1259 /*
1260  *      Unsolicited frames to be processed by fabric.
1261  */
1262 static void
1263 bfa_fcs_fabric_process_uf(struct bfa_fcs_fabric_s *fabric, struct fchs_s *fchs,
1264               u16 len)
1265 {
1266     struct fc_els_cmd_s *els_cmd = (struct fc_els_cmd_s *) (fchs + 1);
1267 
1268     bfa_trc(fabric->fcs, els_cmd->els_code);
1269 
1270     switch (els_cmd->els_code) {
1271     case FC_ELS_FLOGI:
1272         bfa_fcs_fabric_process_flogi(fabric, fchs, len);
1273         break;
1274 
1275     default:
1276         /*
1277          * need to generate a LS_RJT
1278          */
1279         break;
1280     }
1281 }
1282 
1283 /*
1284  *  Process incoming FLOGI
1285  */
1286 static void
1287 bfa_fcs_fabric_process_flogi(struct bfa_fcs_fabric_s *fabric,
1288             struct fchs_s *fchs, u16 len)
1289 {
1290     struct fc_logi_s *flogi = (struct fc_logi_s *) (fchs + 1);
1291     struct bfa_fcs_lport_s *bport = &fabric->bport;
1292 
1293     bfa_trc(fabric->fcs, fchs->s_id);
1294 
1295     fabric->stats.flogi_rcvd++;
1296     /*
1297      * Check port type. It should be 0 = n-port.
1298      */
1299     if (flogi->csp.port_type) {
1300         /*
1301          * @todo: may need to send a LS_RJT
1302          */
1303         bfa_trc(fabric->fcs, flogi->port_name);
1304         fabric->stats.flogi_rejected++;
1305         return;
1306     }
1307 
1308     fabric->bb_credit = be16_to_cpu(flogi->csp.bbcred);
1309     bport->port_topo.pn2n.rem_port_wwn = flogi->port_name;
1310     bport->port_topo.pn2n.reply_oxid = fchs->ox_id;
1311 
1312     /*
1313      * Send a Flogi Acc
1314      */
1315     bfa_fcs_fabric_send_flogi_acc(fabric);
1316     bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_NO_FABRIC);
1317 }
1318 
1319 static void
1320 bfa_fcs_fabric_send_flogi_acc(struct bfa_fcs_fabric_s *fabric)
1321 {
1322     struct bfa_lport_cfg_s *pcfg = &fabric->bport.port_cfg;
1323     struct bfa_fcs_lport_n2n_s *n2n_port = &fabric->bport.port_topo.pn2n;
1324     struct bfa_s      *bfa = fabric->fcs->bfa;
1325     struct bfa_fcxp_s *fcxp;
1326     u16 reqlen;
1327     struct fchs_s   fchs;
1328 
1329     fcxp = bfa_fcs_fcxp_alloc(fabric->fcs, BFA_FALSE);
1330     /*
1331      * Do not expect this failure -- expect remote node to retry
1332      */
1333     if (!fcxp)
1334         return;
1335 
1336     reqlen = fc_flogi_acc_build(&fchs, bfa_fcxp_get_reqbuf(fcxp),
1337                     bfa_hton3b(FC_FABRIC_PORT),
1338                     n2n_port->reply_oxid, pcfg->pwwn,
1339                     pcfg->nwwn,
1340                     bfa_fcport_get_maxfrsize(bfa),
1341                     bfa_fcport_get_rx_bbcredit(bfa), 0);
1342 
1343     bfa_fcxp_send(fcxp, NULL, fabric->vf_id, fabric->lps->bfa_tag,
1344               BFA_FALSE, FC_CLASS_3,
1345               reqlen, &fchs, bfa_fcs_fabric_flogiacc_comp, fabric,
1346               FC_MAX_PDUSZ, 0);
1347 }
1348 
1349 /*
1350  *   Flogi Acc completion callback.
1351  */
1352 static void
1353 bfa_fcs_fabric_flogiacc_comp(void *fcsarg, struct bfa_fcxp_s *fcxp, void *cbarg,
1354                  bfa_status_t status, u32 rsp_len,
1355                  u32 resid_len, struct fchs_s *rspfchs)
1356 {
1357     struct bfa_fcs_fabric_s *fabric = cbarg;
1358 
1359     bfa_trc(fabric->fcs, status);
1360 }
1361 
1362 
1363 /*
1364  * Send AEN notification
1365  */
1366 static void
1367 bfa_fcs_fabric_aen_post(struct bfa_fcs_lport_s *port,
1368             enum bfa_port_aen_event event)
1369 {
1370     struct bfad_s *bfad = (struct bfad_s *)port->fabric->fcs->bfad;
1371     struct bfa_aen_entry_s  *aen_entry;
1372 
1373     bfad_get_aen_entry(bfad, aen_entry);
1374     if (!aen_entry)
1375         return;
1376 
1377     aen_entry->aen_data.port.pwwn = bfa_fcs_lport_get_pwwn(port);
1378     aen_entry->aen_data.port.fwwn = bfa_fcs_lport_get_fabric_name(port);
1379 
1380     /* Send the AEN notification */
1381     bfad_im_post_vendor_event(aen_entry, bfad, ++port->fcs->fcs_aen_seq,
1382                   BFA_AEN_CAT_PORT, event);
1383 }
1384 
1385 /*
1386  *
1387  * @param[in] fabric - fabric
1388  * @param[in] wwn_t - new fabric name
1389  *
1390  * @return - none
1391  */
1392 void
1393 bfa_fcs_fabric_set_fabric_name(struct bfa_fcs_fabric_s *fabric,
1394                    wwn_t fabric_name)
1395 {
1396     struct bfad_s *bfad = (struct bfad_s *)fabric->fcs->bfad;
1397     char    pwwn_ptr[BFA_STRING_32];
1398     char    fwwn_ptr[BFA_STRING_32];
1399 
1400     bfa_trc(fabric->fcs, fabric_name);
1401 
1402     if (fabric->fabric_name == 0) {
1403         /*
1404          * With BRCD switches, we don't get Fabric Name in FLOGI.
1405          * Don't generate a fabric name change event in this case.
1406          */
1407         fabric->fabric_name = fabric_name;
1408     } else {
1409         fabric->fabric_name = fabric_name;
1410         wwn2str(pwwn_ptr, bfa_fcs_lport_get_pwwn(&fabric->bport));
1411         wwn2str(fwwn_ptr,
1412             bfa_fcs_lport_get_fabric_name(&fabric->bport));
1413         BFA_LOG(KERN_WARNING, bfad, bfa_log_level,
1414             "Base port WWN = %s Fabric WWN = %s\n",
1415             pwwn_ptr, fwwn_ptr);
1416         bfa_fcs_fabric_aen_post(&fabric->bport,
1417                 BFA_PORT_AEN_FABRIC_NAME_CHANGE);
1418     }
1419 }
1420 
1421 void
1422 bfa_cb_lps_flogo_comp(void *bfad, void *uarg)
1423 {
1424     struct bfa_fcs_fabric_s *fabric = uarg;
1425     bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_LOGOCOMP);
1426 }
1427 
1428 /*
1429  *  Returns FCS vf structure for a given vf_id.
1430  *
1431  *  param[in]   vf_id - VF_ID
1432  *
1433  *  return
1434  *  If lookup succeeds, retuns fcs vf object, otherwise returns NULL
1435  */
1436 bfa_fcs_vf_t   *
1437 bfa_fcs_vf_lookup(struct bfa_fcs_s *fcs, u16 vf_id)
1438 {
1439     bfa_trc(fcs, vf_id);
1440     if (vf_id == FC_VF_ID_NULL)
1441         return &fcs->fabric;
1442 
1443     return NULL;
1444 }
1445 
1446 /*
1447  *  Return the list of local logical ports present in the given VF.
1448  *
1449  *  @param[in]  vf  vf for which logical ports are returned
1450  *  @param[out] lpwwn   returned logical port wwn list
1451  *  @param[in,out]  nlports in:size of lpwwn list;
1452  *              out:total elements present,
1453  *              actual elements returned is limited by the size
1454  */
1455 void
1456 bfa_fcs_vf_get_ports(bfa_fcs_vf_t *vf, wwn_t lpwwn[], int *nlports)
1457 {
1458     struct list_head *qe;
1459     struct bfa_fcs_vport_s *vport;
1460     int i = 0;
1461     struct bfa_fcs_s    *fcs;
1462 
1463     if (vf == NULL || lpwwn == NULL || *nlports == 0)
1464         return;
1465 
1466     fcs = vf->fcs;
1467 
1468     bfa_trc(fcs, vf->vf_id);
1469     bfa_trc(fcs, (uint32_t) *nlports);
1470 
1471     lpwwn[i++] = vf->bport.port_cfg.pwwn;
1472 
1473     list_for_each(qe, &vf->vport_q) {
1474         if (i >= *nlports)
1475             break;
1476 
1477         vport = (struct bfa_fcs_vport_s *) qe;
1478         lpwwn[i++] = vport->lport.port_cfg.pwwn;
1479     }
1480 
1481     bfa_trc(fcs, i);
1482     *nlports = i;
1483 }
1484 
1485 /*
1486  * BFA FCS PPORT ( physical port)
1487  */
1488 static void
1489 bfa_fcs_port_event_handler(void *cbarg, enum bfa_port_linkstate event)
1490 {
1491     struct bfa_fcs_s      *fcs = cbarg;
1492 
1493     bfa_trc(fcs, event);
1494 
1495     switch (event) {
1496     case BFA_PORT_LINKUP:
1497         bfa_fcs_fabric_link_up(&fcs->fabric);
1498         break;
1499 
1500     case BFA_PORT_LINKDOWN:
1501         bfa_fcs_fabric_link_down(&fcs->fabric);
1502         break;
1503 
1504     default:
1505         WARN_ON(1);
1506     }
1507 }
1508 
1509 /*
1510  * BFA FCS UF ( Unsolicited Frames)
1511  */
1512 
1513 /*
1514  *      BFA callback for unsolicited frame receive handler.
1515  *
1516  * @param[in]       cbarg       callback arg for receive handler
1517  * @param[in]       uf      unsolicited frame descriptor
1518  *
1519  * @return None
1520  */
1521 static void
1522 bfa_fcs_uf_recv(void *cbarg, struct bfa_uf_s *uf)
1523 {
1524     struct bfa_fcs_s    *fcs = (struct bfa_fcs_s *) cbarg;
1525     struct fchs_s   *fchs = bfa_uf_get_frmbuf(uf);
1526     u16 len = bfa_uf_get_frmlen(uf);
1527     struct fc_vft_s *vft;
1528     struct bfa_fcs_fabric_s *fabric;
1529 
1530     /*
1531      * check for VFT header
1532      */
1533     if (fchs->routing == FC_RTG_EXT_HDR &&
1534         fchs->cat_info == FC_CAT_VFT_HDR) {
1535         bfa_stats(fcs, uf.tagged);
1536         vft = bfa_uf_get_frmbuf(uf);
1537         if (fcs->port_vfid == vft->vf_id)
1538             fabric = &fcs->fabric;
1539         else
1540             fabric = bfa_fcs_vf_lookup(fcs, (u16) vft->vf_id);
1541 
1542         /*
1543          * drop frame if vfid is unknown
1544          */
1545         if (!fabric) {
1546             WARN_ON(1);
1547             bfa_stats(fcs, uf.vfid_unknown);
1548             bfa_uf_free(uf);
1549             return;
1550         }
1551 
1552         /*
1553          * skip vft header
1554          */
1555         fchs = (struct fchs_s *) (vft + 1);
1556         len -= sizeof(struct fc_vft_s);
1557 
1558         bfa_trc(fcs, vft->vf_id);
1559     } else {
1560         bfa_stats(fcs, uf.untagged);
1561         fabric = &fcs->fabric;
1562     }
1563 
1564     bfa_trc(fcs, ((u32 *) fchs)[0]);
1565     bfa_trc(fcs, ((u32 *) fchs)[1]);
1566     bfa_trc(fcs, ((u32 *) fchs)[2]);
1567     bfa_trc(fcs, ((u32 *) fchs)[3]);
1568     bfa_trc(fcs, ((u32 *) fchs)[4]);
1569     bfa_trc(fcs, ((u32 *) fchs)[5]);
1570     bfa_trc(fcs, len);
1571 
1572     bfa_fcs_fabric_uf_recv(fabric, fchs, len);
1573     bfa_uf_free(uf);
1574 }
1575 
1576 /*
1577  * fcs attach -- called once to initialize data structures at driver attach time
1578  */
1579 void
1580 bfa_fcs_attach(struct bfa_fcs_s *fcs, struct bfa_s *bfa, struct bfad_s *bfad,
1581            bfa_boolean_t min_cfg)
1582 {
1583     struct bfa_fcs_fabric_s *fabric = &fcs->fabric;
1584 
1585     fcs->bfa = bfa;
1586     fcs->bfad = bfad;
1587     fcs->min_cfg = min_cfg;
1588     fcs->num_rport_logins = 0;
1589 
1590     bfa->fcs = BFA_TRUE;
1591     fcbuild_init();
1592 
1593     bfa_fcport_event_register(fcs->bfa, bfa_fcs_port_event_handler, fcs);
1594     bfa_uf_recv_register(fcs->bfa, bfa_fcs_uf_recv, fcs);
1595 
1596     memset(fabric, 0, sizeof(struct bfa_fcs_fabric_s));
1597 
1598     /*
1599      * Initialize base fabric.
1600      */
1601     fabric->fcs = fcs;
1602     INIT_LIST_HEAD(&fabric->vport_q);
1603     INIT_LIST_HEAD(&fabric->vf_q);
1604     fabric->lps = bfa_lps_alloc(fcs->bfa);
1605     WARN_ON(!fabric->lps);
1606 
1607     /*
1608      * Initialize fabric delete completion handler. Fabric deletion is
1609      * complete when the last vport delete is complete.
1610      */
1611     bfa_wc_init(&fabric->wc, bfa_fcs_fabric_delete_comp, fabric);
1612     bfa_wc_up(&fabric->wc); /* For the base port */
1613 
1614     bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_uninit);
1615     bfa_fcs_lport_attach(&fabric->bport, fabric->fcs, FC_VF_ID_NULL, NULL);
1616 }