1/*
2 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
3 * All rights reserved
4 * www.brocade.com
5 *
6 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License (GPL) Version 2 as
10 * published by the Free Software Foundation
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * General Public License for more details.
16 */
17
18/*
19 *  bfa_fcs.c BFA FCS main
20 */
21
22#include "bfad_drv.h"
23#include "bfad_im.h"
24#include "bfa_fcs.h"
25#include "bfa_fcbuild.h"
26
27BFA_TRC_FILE(FCS, FCS);
28
29/*
30 * FCS sub-modules
31 */
32struct bfa_fcs_mod_s {
33	void		(*attach) (struct bfa_fcs_s *fcs);
34	void		(*modinit) (struct bfa_fcs_s *fcs);
35	void		(*modexit) (struct bfa_fcs_s *fcs);
36};
37
38#define BFA_FCS_MODULE(_mod) { _mod ## _modinit, _mod ## _modexit }
39
40static struct bfa_fcs_mod_s fcs_modules[] = {
41	{ bfa_fcs_port_attach, NULL, NULL },
42	{ bfa_fcs_uf_attach, NULL, NULL },
43	{ bfa_fcs_fabric_attach, bfa_fcs_fabric_modinit,
44	  bfa_fcs_fabric_modexit },
45};
46
47/*
48 *  fcs_api BFA FCS API
49 */
50
51static void
52bfa_fcs_exit_comp(void *fcs_cbarg)
53{
54	struct bfa_fcs_s      *fcs = fcs_cbarg;
55	struct bfad_s         *bfad = fcs->bfad;
56
57	complete(&bfad->comp);
58}
59
60
61
62/*
63 *  fcs_api BFA FCS API
64 */
65
66/*
67 * fcs attach -- called once to initialize data structures at driver attach time
68 */
69void
70bfa_fcs_attach(struct bfa_fcs_s *fcs, struct bfa_s *bfa, struct bfad_s *bfad,
71	       bfa_boolean_t min_cfg)
72{
73	int		i;
74	struct bfa_fcs_mod_s  *mod;
75
76	fcs->bfa = bfa;
77	fcs->bfad = bfad;
78	fcs->min_cfg = min_cfg;
79	fcs->num_rport_logins = 0;
80
81	bfa->fcs = BFA_TRUE;
82	fcbuild_init();
83
84	for (i = 0; i < ARRAY_SIZE(fcs_modules); i++) {
85		mod = &fcs_modules[i];
86		if (mod->attach)
87			mod->attach(fcs);
88	}
89}
90
91/*
92 * fcs initialization, called once after bfa initialization is complete
93 */
94void
95bfa_fcs_init(struct bfa_fcs_s *fcs)
96{
97	int	i;
98	struct bfa_fcs_mod_s  *mod;
99
100	for (i = 0; i < ARRAY_SIZE(fcs_modules); i++) {
101		mod = &fcs_modules[i];
102		if (mod->modinit)
103			mod->modinit(fcs);
104	}
105}
106
107/*
108 * FCS update cfg - reset the pwwn/nwwn of fabric base logical port
109 * with values learned during bfa_init firmware GETATTR REQ.
110 */
111void
112bfa_fcs_update_cfg(struct bfa_fcs_s *fcs)
113{
114	struct bfa_fcs_fabric_s *fabric = &fcs->fabric;
115	struct bfa_lport_cfg_s *port_cfg = &fabric->bport.port_cfg;
116	struct bfa_ioc_s *ioc = &fabric->fcs->bfa->ioc;
117
118	port_cfg->nwwn = ioc->attr->nwwn;
119	port_cfg->pwwn = ioc->attr->pwwn;
120}
121
122/*
123 * Stop FCS operations.
124 */
125void
126bfa_fcs_stop(struct bfa_fcs_s *fcs)
127{
128	bfa_wc_init(&fcs->wc, bfa_fcs_exit_comp, fcs);
129	bfa_wc_up(&fcs->wc);
130	bfa_fcs_fabric_modstop(fcs);
131	bfa_wc_wait(&fcs->wc);
132}
133
134/*
135 * fcs pbc vport initialization
136 */
137void
138bfa_fcs_pbc_vport_init(struct bfa_fcs_s *fcs)
139{
140	int i, npbc_vports;
141	struct bfi_pbc_vport_s pbc_vports[BFI_PBC_MAX_VPORTS];
142
143	/* Initialize pbc vports */
144	if (!fcs->min_cfg) {
145		npbc_vports =
146			bfa_iocfc_get_pbc_vports(fcs->bfa, pbc_vports);
147		for (i = 0; i < npbc_vports; i++)
148			bfa_fcb_pbc_vport_create(fcs->bfa->bfad, pbc_vports[i]);
149	}
150}
151
152/*
153 *	brief
154 *		FCS driver details initialization.
155 *
156 *	param[in]		fcs		FCS instance
157 *	param[in]		driver_info	Driver Details
158 *
159 *	return None
160 */
161void
162bfa_fcs_driver_info_init(struct bfa_fcs_s *fcs,
163			struct bfa_fcs_driver_info_s *driver_info)
164{
165
166	fcs->driver_info = *driver_info;
167
168	bfa_fcs_fabric_psymb_init(&fcs->fabric);
169	bfa_fcs_fabric_nsymb_init(&fcs->fabric);
170}
171
172/*
173 *	brief
174 *		FCS instance cleanup and exit.
175 *
176 *	param[in]		fcs			FCS instance
177 *	return None
178 */
179void
180bfa_fcs_exit(struct bfa_fcs_s *fcs)
181{
182	struct bfa_fcs_mod_s  *mod;
183	int		nmods, i;
184
185	bfa_wc_init(&fcs->wc, bfa_fcs_exit_comp, fcs);
186
187	nmods = ARRAY_SIZE(fcs_modules);
188
189	for (i = 0; i < nmods; i++) {
190
191		mod = &fcs_modules[i];
192		if (mod->modexit) {
193			bfa_wc_up(&fcs->wc);
194			mod->modexit(fcs);
195		}
196	}
197
198	bfa_wc_wait(&fcs->wc);
199}
200
201
202/*
203 * Fabric module implementation.
204 */
205
206#define BFA_FCS_FABRIC_RETRY_DELAY	(2000)	/* Milliseconds */
207#define BFA_FCS_FABRIC_CLEANUP_DELAY	(10000)	/* Milliseconds */
208
209#define bfa_fcs_fabric_set_opertype(__fabric) do {			\
210	if (bfa_fcport_get_topology((__fabric)->fcs->bfa)		\
211				== BFA_PORT_TOPOLOGY_P2P) {		\
212		if (fabric->fab_type == BFA_FCS_FABRIC_SWITCHED)	\
213			(__fabric)->oper_type = BFA_PORT_TYPE_NPORT;	\
214		else							\
215			(__fabric)->oper_type = BFA_PORT_TYPE_P2P;	\
216	} else								\
217		(__fabric)->oper_type = BFA_PORT_TYPE_NLPORT;		\
218} while (0)
219
220/*
221 * forward declarations
222 */
223static void bfa_fcs_fabric_init(struct bfa_fcs_fabric_s *fabric);
224static void bfa_fcs_fabric_login(struct bfa_fcs_fabric_s *fabric);
225static void bfa_fcs_fabric_notify_online(struct bfa_fcs_fabric_s *fabric);
226static void bfa_fcs_fabric_notify_offline(struct bfa_fcs_fabric_s *fabric);
227static void bfa_fcs_fabric_delay(void *cbarg);
228static void bfa_fcs_fabric_delete(struct bfa_fcs_fabric_s *fabric);
229static void bfa_fcs_fabric_delete_comp(void *cbarg);
230static void bfa_fcs_fabric_stop(struct bfa_fcs_fabric_s *fabric);
231static void bfa_fcs_fabric_stop_comp(void *cbarg);
232static void bfa_fcs_fabric_process_uf(struct bfa_fcs_fabric_s *fabric,
233				      struct fchs_s *fchs, u16 len);
234static void bfa_fcs_fabric_process_flogi(struct bfa_fcs_fabric_s *fabric,
235					 struct fchs_s *fchs, u16 len);
236static void bfa_fcs_fabric_send_flogi_acc(struct bfa_fcs_fabric_s *fabric);
237static void bfa_fcs_fabric_flogiacc_comp(void *fcsarg,
238					 struct bfa_fcxp_s *fcxp, void *cbarg,
239					 bfa_status_t status,
240					 u32 rsp_len,
241					 u32 resid_len,
242					 struct fchs_s *rspfchs);
243
244static void	bfa_fcs_fabric_sm_uninit(struct bfa_fcs_fabric_s *fabric,
245					 enum bfa_fcs_fabric_event event);
246static void	bfa_fcs_fabric_sm_created(struct bfa_fcs_fabric_s *fabric,
247					  enum bfa_fcs_fabric_event event);
248static void	bfa_fcs_fabric_sm_linkdown(struct bfa_fcs_fabric_s *fabric,
249					   enum bfa_fcs_fabric_event event);
250static void	bfa_fcs_fabric_sm_flogi(struct bfa_fcs_fabric_s *fabric,
251					enum bfa_fcs_fabric_event event);
252static void	bfa_fcs_fabric_sm_flogi_retry(struct bfa_fcs_fabric_s *fabric,
253					      enum bfa_fcs_fabric_event event);
254static void	bfa_fcs_fabric_sm_auth(struct bfa_fcs_fabric_s *fabric,
255				       enum bfa_fcs_fabric_event event);
256static void	bfa_fcs_fabric_sm_nofabric(struct bfa_fcs_fabric_s *fabric,
257					   enum bfa_fcs_fabric_event event);
258static void	bfa_fcs_fabric_sm_evfp(struct bfa_fcs_fabric_s *fabric,
259				       enum bfa_fcs_fabric_event event);
260static void	bfa_fcs_fabric_sm_evfp_done(struct bfa_fcs_fabric_s *fabric,
261					    enum bfa_fcs_fabric_event event);
262static void	bfa_fcs_fabric_sm_isolated(struct bfa_fcs_fabric_s *fabric,
263					   enum bfa_fcs_fabric_event event);
264static void	bfa_fcs_fabric_sm_deleting(struct bfa_fcs_fabric_s *fabric,
265					   enum bfa_fcs_fabric_event event);
266static void	bfa_fcs_fabric_sm_stopping(struct bfa_fcs_fabric_s *fabric,
267					   enum bfa_fcs_fabric_event event);
268static void	bfa_fcs_fabric_sm_cleanup(struct bfa_fcs_fabric_s *fabric,
269					  enum bfa_fcs_fabric_event event);
270/*
271 *   Beginning state before fabric creation.
272 */
273static void
274bfa_fcs_fabric_sm_uninit(struct bfa_fcs_fabric_s *fabric,
275			 enum bfa_fcs_fabric_event event)
276{
277	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
278	bfa_trc(fabric->fcs, event);
279
280	switch (event) {
281	case BFA_FCS_FABRIC_SM_CREATE:
282		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_created);
283		bfa_fcs_fabric_init(fabric);
284		bfa_fcs_lport_init(&fabric->bport, &fabric->bport.port_cfg);
285		break;
286
287	case BFA_FCS_FABRIC_SM_LINK_UP:
288	case BFA_FCS_FABRIC_SM_LINK_DOWN:
289		break;
290
291	default:
292		bfa_sm_fault(fabric->fcs, event);
293	}
294}
295
296/*
297 *   Beginning state before fabric creation.
298 */
299static void
300bfa_fcs_fabric_sm_created(struct bfa_fcs_fabric_s *fabric,
301			  enum bfa_fcs_fabric_event event)
302{
303	struct bfa_s	*bfa = fabric->fcs->bfa;
304
305	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
306	bfa_trc(fabric->fcs, event);
307
308	switch (event) {
309	case BFA_FCS_FABRIC_SM_START:
310		if (!bfa_fcport_is_linkup(fabric->fcs->bfa)) {
311			bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
312			break;
313		}
314		if (bfa_fcport_get_topology(bfa) ==
315				BFA_PORT_TOPOLOGY_LOOP) {
316			fabric->fab_type = BFA_FCS_FABRIC_LOOP;
317			fabric->bport.pid = bfa_fcport_get_myalpa(bfa);
318			fabric->bport.pid = bfa_hton3b(fabric->bport.pid);
319			bfa_sm_set_state(fabric,
320					bfa_fcs_fabric_sm_online);
321			bfa_fcs_fabric_set_opertype(fabric);
322			bfa_fcs_lport_online(&fabric->bport);
323		} else {
324			bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_flogi);
325			bfa_fcs_fabric_login(fabric);
326		}
327		break;
328
329	case BFA_FCS_FABRIC_SM_LINK_UP:
330	case BFA_FCS_FABRIC_SM_LINK_DOWN:
331		break;
332
333	case BFA_FCS_FABRIC_SM_DELETE:
334		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
335		bfa_fcs_fabric_delete(fabric);
336		break;
337
338	default:
339		bfa_sm_fault(fabric->fcs, event);
340	}
341}
342
343/*
344 *   Link is down, awaiting LINK UP event from port. This is also the
345 *   first state at fabric creation.
346 */
347static void
348bfa_fcs_fabric_sm_linkdown(struct bfa_fcs_fabric_s *fabric,
349			   enum bfa_fcs_fabric_event event)
350{
351	struct bfa_s	*bfa = fabric->fcs->bfa;
352
353	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
354	bfa_trc(fabric->fcs, event);
355
356	switch (event) {
357	case BFA_FCS_FABRIC_SM_LINK_UP:
358		if (bfa_fcport_get_topology(bfa) != BFA_PORT_TOPOLOGY_LOOP) {
359			bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_flogi);
360			bfa_fcs_fabric_login(fabric);
361			break;
362		}
363		fabric->fab_type = BFA_FCS_FABRIC_LOOP;
364		fabric->bport.pid = bfa_fcport_get_myalpa(bfa);
365		fabric->bport.pid = bfa_hton3b(fabric->bport.pid);
366		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_online);
367		bfa_fcs_fabric_set_opertype(fabric);
368		bfa_fcs_lport_online(&fabric->bport);
369		break;
370
371	case BFA_FCS_FABRIC_SM_RETRY_OP:
372	case BFA_FCS_FABRIC_SM_LOOPBACK:
373		break;
374
375	case BFA_FCS_FABRIC_SM_DELETE:
376		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
377		bfa_fcs_fabric_delete(fabric);
378		break;
379
380	case BFA_FCS_FABRIC_SM_STOP:
381		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_cleanup);
382		bfa_fcs_fabric_stop(fabric);
383		break;
384
385	default:
386		bfa_sm_fault(fabric->fcs, event);
387	}
388}
389
390/*
391 *   FLOGI is in progress, awaiting FLOGI reply.
392 */
393static void
394bfa_fcs_fabric_sm_flogi(struct bfa_fcs_fabric_s *fabric,
395			enum bfa_fcs_fabric_event event)
396{
397	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
398	bfa_trc(fabric->fcs, event);
399
400	switch (event) {
401	case BFA_FCS_FABRIC_SM_CONT_OP:
402
403		bfa_fcport_set_tx_bbcredit(fabric->fcs->bfa,
404					   fabric->bb_credit);
405		fabric->fab_type = BFA_FCS_FABRIC_SWITCHED;
406
407		if (fabric->auth_reqd && fabric->is_auth) {
408			bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_auth);
409			bfa_trc(fabric->fcs, event);
410		} else {
411			bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_online);
412			bfa_fcs_fabric_notify_online(fabric);
413		}
414		break;
415
416	case BFA_FCS_FABRIC_SM_RETRY_OP:
417		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_flogi_retry);
418		bfa_timer_start(fabric->fcs->bfa, &fabric->delay_timer,
419				bfa_fcs_fabric_delay, fabric,
420				BFA_FCS_FABRIC_RETRY_DELAY);
421		break;
422
423	case BFA_FCS_FABRIC_SM_LOOPBACK:
424		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_loopback);
425		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
426		bfa_fcs_fabric_set_opertype(fabric);
427		break;
428
429	case BFA_FCS_FABRIC_SM_NO_FABRIC:
430		fabric->fab_type = BFA_FCS_FABRIC_N2N;
431		bfa_fcport_set_tx_bbcredit(fabric->fcs->bfa,
432					   fabric->bb_credit);
433		bfa_fcs_fabric_notify_online(fabric);
434		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_nofabric);
435		break;
436
437	case BFA_FCS_FABRIC_SM_LINK_DOWN:
438		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
439		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
440		break;
441
442	case BFA_FCS_FABRIC_SM_DELETE:
443		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
444		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
445		bfa_fcs_fabric_delete(fabric);
446		break;
447
448	default:
449		bfa_sm_fault(fabric->fcs, event);
450	}
451}
452
453
454static void
455bfa_fcs_fabric_sm_flogi_retry(struct bfa_fcs_fabric_s *fabric,
456			      enum bfa_fcs_fabric_event event)
457{
458	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
459	bfa_trc(fabric->fcs, event);
460
461	switch (event) {
462	case BFA_FCS_FABRIC_SM_DELAYED:
463		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_flogi);
464		bfa_fcs_fabric_login(fabric);
465		break;
466
467	case BFA_FCS_FABRIC_SM_LINK_DOWN:
468		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
469		bfa_timer_stop(&fabric->delay_timer);
470		break;
471
472	case BFA_FCS_FABRIC_SM_DELETE:
473		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
474		bfa_timer_stop(&fabric->delay_timer);
475		bfa_fcs_fabric_delete(fabric);
476		break;
477
478	default:
479		bfa_sm_fault(fabric->fcs, event);
480	}
481}
482
483/*
484 *   Authentication is in progress, awaiting authentication results.
485 */
486static void
487bfa_fcs_fabric_sm_auth(struct bfa_fcs_fabric_s *fabric,
488		       enum bfa_fcs_fabric_event event)
489{
490	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
491	bfa_trc(fabric->fcs, event);
492
493	switch (event) {
494	case BFA_FCS_FABRIC_SM_AUTH_FAILED:
495		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_auth_failed);
496		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
497		break;
498
499	case BFA_FCS_FABRIC_SM_AUTH_SUCCESS:
500		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_online);
501		bfa_fcs_fabric_notify_online(fabric);
502		break;
503
504	case BFA_FCS_FABRIC_SM_PERF_EVFP:
505		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_evfp);
506		break;
507
508	case BFA_FCS_FABRIC_SM_LINK_DOWN:
509		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
510		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
511		break;
512
513	case BFA_FCS_FABRIC_SM_DELETE:
514		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
515		bfa_fcs_fabric_delete(fabric);
516		break;
517
518	default:
519		bfa_sm_fault(fabric->fcs, event);
520	}
521}
522
523/*
524 *   Authentication failed
525 */
526void
527bfa_fcs_fabric_sm_auth_failed(struct bfa_fcs_fabric_s *fabric,
528			      enum bfa_fcs_fabric_event event)
529{
530	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
531	bfa_trc(fabric->fcs, event);
532
533	switch (event) {
534	case BFA_FCS_FABRIC_SM_LINK_DOWN:
535		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
536		bfa_fcs_fabric_notify_offline(fabric);
537		break;
538
539	case BFA_FCS_FABRIC_SM_DELETE:
540		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
541		bfa_fcs_fabric_delete(fabric);
542		break;
543
544	default:
545		bfa_sm_fault(fabric->fcs, event);
546	}
547}
548
549/*
550 *   Port is in loopback mode.
551 */
552void
553bfa_fcs_fabric_sm_loopback(struct bfa_fcs_fabric_s *fabric,
554			   enum bfa_fcs_fabric_event event)
555{
556	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
557	bfa_trc(fabric->fcs, event);
558
559	switch (event) {
560	case BFA_FCS_FABRIC_SM_LINK_DOWN:
561		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
562		bfa_fcs_fabric_notify_offline(fabric);
563		break;
564
565	case BFA_FCS_FABRIC_SM_DELETE:
566		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
567		bfa_fcs_fabric_delete(fabric);
568		break;
569
570	default:
571		bfa_sm_fault(fabric->fcs, event);
572	}
573}
574
575/*
576 *   There is no attached fabric - private loop or NPort-to-NPort topology.
577 */
578static void
579bfa_fcs_fabric_sm_nofabric(struct bfa_fcs_fabric_s *fabric,
580			   enum bfa_fcs_fabric_event event)
581{
582	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
583	bfa_trc(fabric->fcs, event);
584
585	switch (event) {
586	case BFA_FCS_FABRIC_SM_LINK_DOWN:
587		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
588		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
589		bfa_fcs_fabric_notify_offline(fabric);
590		break;
591
592	case BFA_FCS_FABRIC_SM_DELETE:
593		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
594		bfa_fcs_fabric_delete(fabric);
595		break;
596
597	case BFA_FCS_FABRIC_SM_NO_FABRIC:
598		bfa_trc(fabric->fcs, fabric->bb_credit);
599		bfa_fcport_set_tx_bbcredit(fabric->fcs->bfa,
600					   fabric->bb_credit);
601		break;
602
603	case BFA_FCS_FABRIC_SM_RETRY_OP:
604		break;
605
606	default:
607		bfa_sm_fault(fabric->fcs, event);
608	}
609}
610
611/*
612 *   Fabric is online - normal operating state.
613 */
614void
615bfa_fcs_fabric_sm_online(struct bfa_fcs_fabric_s *fabric,
616			 enum bfa_fcs_fabric_event event)
617{
618	struct bfa_s	*bfa = fabric->fcs->bfa;
619
620	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
621	bfa_trc(fabric->fcs, event);
622
623	switch (event) {
624	case BFA_FCS_FABRIC_SM_LINK_DOWN:
625		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
626		if (bfa_fcport_get_topology(bfa) == BFA_PORT_TOPOLOGY_LOOP) {
627			bfa_fcs_lport_offline(&fabric->bport);
628		} else {
629			bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
630			bfa_fcs_fabric_notify_offline(fabric);
631		}
632		break;
633
634	case BFA_FCS_FABRIC_SM_DELETE:
635		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
636		bfa_fcs_fabric_delete(fabric);
637		break;
638
639	case BFA_FCS_FABRIC_SM_STOP:
640		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_stopping);
641		bfa_fcs_fabric_stop(fabric);
642		break;
643
644	case BFA_FCS_FABRIC_SM_AUTH_FAILED:
645		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_auth_failed);
646		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
647		break;
648
649	case BFA_FCS_FABRIC_SM_AUTH_SUCCESS:
650		break;
651
652	default:
653		bfa_sm_fault(fabric->fcs, event);
654	}
655}
656
657/*
658 *   Exchanging virtual fabric parameters.
659 */
660static void
661bfa_fcs_fabric_sm_evfp(struct bfa_fcs_fabric_s *fabric,
662		       enum bfa_fcs_fabric_event event)
663{
664	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
665	bfa_trc(fabric->fcs, event);
666
667	switch (event) {
668	case BFA_FCS_FABRIC_SM_CONT_OP:
669		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_evfp_done);
670		break;
671
672	case BFA_FCS_FABRIC_SM_ISOLATE:
673		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_isolated);
674		break;
675
676	default:
677		bfa_sm_fault(fabric->fcs, event);
678	}
679}
680
681/*
682 *   EVFP exchange complete and VFT tagging is enabled.
683 */
684static void
685bfa_fcs_fabric_sm_evfp_done(struct bfa_fcs_fabric_s *fabric,
686			    enum bfa_fcs_fabric_event event)
687{
688	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
689	bfa_trc(fabric->fcs, event);
690}
691
692/*
693 *   Port is isolated after EVFP exchange due to VF_ID mismatch (N and F).
694 */
695static void
696bfa_fcs_fabric_sm_isolated(struct bfa_fcs_fabric_s *fabric,
697			   enum bfa_fcs_fabric_event event)
698{
699	struct bfad_s *bfad = (struct bfad_s *)fabric->fcs->bfad;
700	char	pwwn_ptr[BFA_STRING_32];
701
702	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
703	bfa_trc(fabric->fcs, event);
704	wwn2str(pwwn_ptr, fabric->bport.port_cfg.pwwn);
705
706	BFA_LOG(KERN_INFO, bfad, bfa_log_level,
707		"Port is isolated due to VF_ID mismatch. "
708		"PWWN: %s Port VF_ID: %04x switch port VF_ID: %04x.",
709		pwwn_ptr, fabric->fcs->port_vfid,
710		fabric->event_arg.swp_vfid);
711}
712
713/*
714 *   Fabric is being deleted, awaiting vport delete completions.
715 */
716static void
717bfa_fcs_fabric_sm_deleting(struct bfa_fcs_fabric_s *fabric,
718			   enum bfa_fcs_fabric_event event)
719{
720	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
721	bfa_trc(fabric->fcs, event);
722
723	switch (event) {
724	case BFA_FCS_FABRIC_SM_DELCOMP:
725		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_uninit);
726		bfa_wc_down(&fabric->fcs->wc);
727		break;
728
729	case BFA_FCS_FABRIC_SM_LINK_UP:
730		break;
731
732	case BFA_FCS_FABRIC_SM_LINK_DOWN:
733		bfa_fcs_fabric_notify_offline(fabric);
734		break;
735
736	default:
737		bfa_sm_fault(fabric->fcs, event);
738	}
739}
740
741/*
742 * Fabric is being stopped, awaiting vport stop completions.
743 */
744static void
745bfa_fcs_fabric_sm_stopping(struct bfa_fcs_fabric_s *fabric,
746			   enum bfa_fcs_fabric_event event)
747{
748	struct bfa_s	*bfa = fabric->fcs->bfa;
749
750	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
751	bfa_trc(fabric->fcs, event);
752
753	switch (event) {
754	case BFA_FCS_FABRIC_SM_STOPCOMP:
755		if (bfa_fcport_get_topology(bfa) == BFA_PORT_TOPOLOGY_LOOP) {
756			bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_created);
757		} else {
758			bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_cleanup);
759			bfa_sm_send_event(fabric->lps, BFA_LPS_SM_LOGOUT);
760		}
761		break;
762
763	case BFA_FCS_FABRIC_SM_LINK_UP:
764		break;
765
766	case BFA_FCS_FABRIC_SM_LINK_DOWN:
767		if (bfa_fcport_get_topology(bfa) == BFA_PORT_TOPOLOGY_LOOP)
768			bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_created);
769		else
770			bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_cleanup);
771		break;
772
773	default:
774		bfa_sm_fault(fabric->fcs, event);
775	}
776}
777
778/*
779 * Fabric is being stopped, cleanup without FLOGO
780 */
781static void
782bfa_fcs_fabric_sm_cleanup(struct bfa_fcs_fabric_s *fabric,
783			  enum bfa_fcs_fabric_event event)
784{
785	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
786	bfa_trc(fabric->fcs, event);
787
788	switch (event) {
789	case BFA_FCS_FABRIC_SM_STOPCOMP:
790	case BFA_FCS_FABRIC_SM_LOGOCOMP:
791		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_created);
792		bfa_wc_down(&(fabric->fcs)->wc);
793		break;
794
795	case BFA_FCS_FABRIC_SM_LINK_DOWN:
796		/*
797		 * Ignore - can get this event if we get notified about IOC down
798		 * before the fabric completion callbk is done.
799		 */
800		break;
801
802	default:
803		bfa_sm_fault(fabric->fcs, event);
804	}
805}
806
807/*
808 *  fcs_fabric_private fabric private functions
809 */
810
811static void
812bfa_fcs_fabric_init(struct bfa_fcs_fabric_s *fabric)
813{
814	struct bfa_lport_cfg_s *port_cfg = &fabric->bport.port_cfg;
815
816	port_cfg->roles = BFA_LPORT_ROLE_FCP_IM;
817	port_cfg->nwwn = fabric->fcs->bfa->ioc.attr->nwwn;
818	port_cfg->pwwn = fabric->fcs->bfa->ioc.attr->pwwn;
819}
820
821/*
822 * Port Symbolic Name Creation for base port.
823 */
824void
825bfa_fcs_fabric_psymb_init(struct bfa_fcs_fabric_s *fabric)
826{
827	struct bfa_lport_cfg_s *port_cfg = &fabric->bport.port_cfg;
828	char model[BFA_ADAPTER_MODEL_NAME_LEN] = {0};
829	struct bfa_fcs_driver_info_s *driver_info = &fabric->fcs->driver_info;
830
831	bfa_ioc_get_adapter_model(&fabric->fcs->bfa->ioc, model);
832
833	/* Model name/number */
834	strncpy((char *)&port_cfg->sym_name, model,
835		BFA_FCS_PORT_SYMBNAME_MODEL_SZ);
836	strncat((char *)&port_cfg->sym_name, BFA_FCS_PORT_SYMBNAME_SEPARATOR,
837		sizeof(BFA_FCS_PORT_SYMBNAME_SEPARATOR));
838
839	/* Driver Version */
840	strncat((char *)&port_cfg->sym_name, (char *)driver_info->version,
841		BFA_FCS_PORT_SYMBNAME_VERSION_SZ);
842	strncat((char *)&port_cfg->sym_name, BFA_FCS_PORT_SYMBNAME_SEPARATOR,
843		sizeof(BFA_FCS_PORT_SYMBNAME_SEPARATOR));
844
845	/* Host machine name */
846	strncat((char *)&port_cfg->sym_name,
847		(char *)driver_info->host_machine_name,
848		BFA_FCS_PORT_SYMBNAME_MACHINENAME_SZ);
849	strncat((char *)&port_cfg->sym_name, BFA_FCS_PORT_SYMBNAME_SEPARATOR,
850		sizeof(BFA_FCS_PORT_SYMBNAME_SEPARATOR));
851
852	/*
853	 * Host OS Info :
854	 * If OS Patch Info is not there, do not truncate any bytes from the
855	 * OS name string and instead copy the entire OS info string (64 bytes).
856	 */
857	if (driver_info->host_os_patch[0] == '\0') {
858		strncat((char *)&port_cfg->sym_name,
859			(char *)driver_info->host_os_name,
860			BFA_FCS_OS_STR_LEN);
861		strncat((char *)&port_cfg->sym_name,
862			BFA_FCS_PORT_SYMBNAME_SEPARATOR,
863			sizeof(BFA_FCS_PORT_SYMBNAME_SEPARATOR));
864	} else {
865		strncat((char *)&port_cfg->sym_name,
866			(char *)driver_info->host_os_name,
867			BFA_FCS_PORT_SYMBNAME_OSINFO_SZ);
868		strncat((char *)&port_cfg->sym_name,
869			BFA_FCS_PORT_SYMBNAME_SEPARATOR,
870			sizeof(BFA_FCS_PORT_SYMBNAME_SEPARATOR));
871
872		/* Append host OS Patch Info */
873		strncat((char *)&port_cfg->sym_name,
874			(char *)driver_info->host_os_patch,
875			BFA_FCS_PORT_SYMBNAME_OSPATCH_SZ);
876	}
877
878	/* null terminate */
879	port_cfg->sym_name.symname[BFA_SYMNAME_MAXLEN - 1] = 0;
880}
881
882/*
883 * Node Symbolic Name Creation for base port and all vports
884 */
885void
886bfa_fcs_fabric_nsymb_init(struct bfa_fcs_fabric_s *fabric)
887{
888	struct bfa_lport_cfg_s *port_cfg = &fabric->bport.port_cfg;
889	char model[BFA_ADAPTER_MODEL_NAME_LEN] = {0};
890	struct bfa_fcs_driver_info_s *driver_info = &fabric->fcs->driver_info;
891
892	bfa_ioc_get_adapter_model(&fabric->fcs->bfa->ioc, model);
893
894	/* Model name/number */
895	strncpy((char *)&port_cfg->node_sym_name, model,
896		BFA_FCS_PORT_SYMBNAME_MODEL_SZ);
897	strncat((char *)&port_cfg->node_sym_name,
898			BFA_FCS_PORT_SYMBNAME_SEPARATOR,
899			sizeof(BFA_FCS_PORT_SYMBNAME_SEPARATOR));
900
901	/* Driver Version */
902	strncat((char *)&port_cfg->node_sym_name, (char *)driver_info->version,
903		BFA_FCS_PORT_SYMBNAME_VERSION_SZ);
904	strncat((char *)&port_cfg->node_sym_name,
905			BFA_FCS_PORT_SYMBNAME_SEPARATOR,
906			sizeof(BFA_FCS_PORT_SYMBNAME_SEPARATOR));
907
908	/* Host machine name */
909	strncat((char *)&port_cfg->node_sym_name,
910		(char *)driver_info->host_machine_name,
911		BFA_FCS_PORT_SYMBNAME_MACHINENAME_SZ);
912	strncat((char *)&port_cfg->node_sym_name,
913			BFA_FCS_PORT_SYMBNAME_SEPARATOR,
914			sizeof(BFA_FCS_PORT_SYMBNAME_SEPARATOR));
915
916	/* null terminate */
917	port_cfg->node_sym_name.symname[BFA_SYMNAME_MAXLEN - 1] = 0;
918}
919
920/*
921 * bfa lps login completion callback
922 */
923void
924bfa_cb_lps_flogi_comp(void *bfad, void *uarg, bfa_status_t status)
925{
926	struct bfa_fcs_fabric_s *fabric = uarg;
927
928	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
929	bfa_trc(fabric->fcs, status);
930
931	switch (status) {
932	case BFA_STATUS_OK:
933		fabric->stats.flogi_accepts++;
934		break;
935
936	case BFA_STATUS_INVALID_MAC:
937		/* Only for CNA */
938		fabric->stats.flogi_acc_err++;
939		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_RETRY_OP);
940
941		return;
942
943	case BFA_STATUS_EPROTOCOL:
944		switch (fabric->lps->ext_status) {
945		case BFA_EPROTO_BAD_ACCEPT:
946			fabric->stats.flogi_acc_err++;
947			break;
948
949		case BFA_EPROTO_UNKNOWN_RSP:
950			fabric->stats.flogi_unknown_rsp++;
951			break;
952
953		default:
954			break;
955		}
956		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_RETRY_OP);
957
958		return;
959
960	case BFA_STATUS_FABRIC_RJT:
961		fabric->stats.flogi_rejects++;
962		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_RETRY_OP);
963		return;
964
965	default:
966		fabric->stats.flogi_rsp_err++;
967		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_RETRY_OP);
968		return;
969	}
970
971	fabric->bb_credit = fabric->lps->pr_bbcred;
972	bfa_trc(fabric->fcs, fabric->bb_credit);
973
974	if (!(fabric->lps->brcd_switch))
975		fabric->fabric_name =  fabric->lps->pr_nwwn;
976
977	/*
978	 * Check port type. It should be 1 = F-port.
979	 */
980	if (fabric->lps->fport) {
981		fabric->bport.pid = fabric->lps->lp_pid;
982		fabric->is_npiv = fabric->lps->npiv_en;
983		fabric->is_auth = fabric->lps->auth_req;
984		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_CONT_OP);
985	} else {
986		/*
987		 * Nport-2-Nport direct attached
988		 */
989		fabric->bport.port_topo.pn2n.rem_port_wwn =
990			fabric->lps->pr_pwwn;
991		fabric->fab_type = BFA_FCS_FABRIC_N2N;
992		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_NO_FABRIC);
993	}
994
995	bfa_trc(fabric->fcs, fabric->bport.pid);
996	bfa_trc(fabric->fcs, fabric->is_npiv);
997	bfa_trc(fabric->fcs, fabric->is_auth);
998}
999/*
1000 *		Allocate and send FLOGI.
1001 */
1002static void
1003bfa_fcs_fabric_login(struct bfa_fcs_fabric_s *fabric)
1004{
1005	struct bfa_s		*bfa = fabric->fcs->bfa;
1006	struct bfa_lport_cfg_s	*pcfg = &fabric->bport.port_cfg;
1007	u8			alpa = 0;
1008
1009
1010	bfa_lps_flogi(fabric->lps, fabric, alpa, bfa_fcport_get_maxfrsize(bfa),
1011		      pcfg->pwwn, pcfg->nwwn, fabric->auth_reqd);
1012
1013	fabric->stats.flogi_sent++;
1014}
1015
1016static void
1017bfa_fcs_fabric_notify_online(struct bfa_fcs_fabric_s *fabric)
1018{
1019	struct bfa_fcs_vport_s *vport;
1020	struct list_head	      *qe, *qen;
1021
1022	bfa_trc(fabric->fcs, fabric->fabric_name);
1023
1024	bfa_fcs_fabric_set_opertype(fabric);
1025	fabric->stats.fabric_onlines++;
1026
1027	/*
1028	 * notify online event to base and then virtual ports
1029	 */
1030	bfa_fcs_lport_online(&fabric->bport);
1031
1032	list_for_each_safe(qe, qen, &fabric->vport_q) {
1033		vport = (struct bfa_fcs_vport_s *) qe;
1034		bfa_fcs_vport_online(vport);
1035	}
1036}
1037
1038static void
1039bfa_fcs_fabric_notify_offline(struct bfa_fcs_fabric_s *fabric)
1040{
1041	struct bfa_fcs_vport_s *vport;
1042	struct list_head	      *qe, *qen;
1043
1044	bfa_trc(fabric->fcs, fabric->fabric_name);
1045	fabric->stats.fabric_offlines++;
1046
1047	/*
1048	 * notify offline event first to vports and then base port.
1049	 */
1050	list_for_each_safe(qe, qen, &fabric->vport_q) {
1051		vport = (struct bfa_fcs_vport_s *) qe;
1052		bfa_fcs_vport_offline(vport);
1053	}
1054
1055	bfa_fcs_lport_offline(&fabric->bport);
1056
1057	fabric->fabric_name = 0;
1058	fabric->fabric_ip_addr[0] = 0;
1059}
1060
1061static void
1062bfa_fcs_fabric_delay(void *cbarg)
1063{
1064	struct bfa_fcs_fabric_s *fabric = cbarg;
1065
1066	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_DELAYED);
1067}
1068
1069/*
1070 * Stop all vports and wait for vport stop completions.
1071 */
1072static void
1073bfa_fcs_fabric_stop(struct bfa_fcs_fabric_s *fabric)
1074{
1075	struct bfa_fcs_vport_s *vport;
1076	struct list_head	*qe, *qen;
1077
1078	bfa_wc_init(&fabric->stop_wc, bfa_fcs_fabric_stop_comp, fabric);
1079
1080	list_for_each_safe(qe, qen, &fabric->vport_q) {
1081		vport = (struct bfa_fcs_vport_s *) qe;
1082		bfa_wc_up(&fabric->stop_wc);
1083		bfa_fcs_vport_fcs_stop(vport);
1084	}
1085
1086	bfa_wc_up(&fabric->stop_wc);
1087	bfa_fcs_lport_stop(&fabric->bport);
1088	bfa_wc_wait(&fabric->stop_wc);
1089}
1090
1091/*
1092 * Delete all vports and wait for vport delete completions.
1093 */
1094static void
1095bfa_fcs_fabric_delete(struct bfa_fcs_fabric_s *fabric)
1096{
1097	struct bfa_fcs_vport_s *vport;
1098	struct list_head	      *qe, *qen;
1099
1100	list_for_each_safe(qe, qen, &fabric->vport_q) {
1101		vport = (struct bfa_fcs_vport_s *) qe;
1102		bfa_fcs_vport_fcs_delete(vport);
1103	}
1104
1105	bfa_fcs_lport_delete(&fabric->bport);
1106	bfa_wc_wait(&fabric->wc);
1107}
1108
1109static void
1110bfa_fcs_fabric_delete_comp(void *cbarg)
1111{
1112	struct bfa_fcs_fabric_s *fabric = cbarg;
1113
1114	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_DELCOMP);
1115}
1116
1117static void
1118bfa_fcs_fabric_stop_comp(void *cbarg)
1119{
1120	struct bfa_fcs_fabric_s *fabric = cbarg;
1121
1122	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_STOPCOMP);
1123}
1124
1125/*
1126 *  fcs_fabric_public fabric public functions
1127 */
1128
1129/*
1130 * Attach time initialization.
1131 */
1132void
1133bfa_fcs_fabric_attach(struct bfa_fcs_s *fcs)
1134{
1135	struct bfa_fcs_fabric_s *fabric;
1136
1137	fabric = &fcs->fabric;
1138	memset(fabric, 0, sizeof(struct bfa_fcs_fabric_s));
1139
1140	/*
1141	 * Initialize base fabric.
1142	 */
1143	fabric->fcs = fcs;
1144	INIT_LIST_HEAD(&fabric->vport_q);
1145	INIT_LIST_HEAD(&fabric->vf_q);
1146	fabric->lps = bfa_lps_alloc(fcs->bfa);
1147	WARN_ON(!fabric->lps);
1148
1149	/*
1150	 * Initialize fabric delete completion handler. Fabric deletion is
1151	 * complete when the last vport delete is complete.
1152	 */
1153	bfa_wc_init(&fabric->wc, bfa_fcs_fabric_delete_comp, fabric);
1154	bfa_wc_up(&fabric->wc); /* For the base port */
1155
1156	bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_uninit);
1157	bfa_fcs_lport_attach(&fabric->bport, fabric->fcs, FC_VF_ID_NULL, NULL);
1158}
1159
1160void
1161bfa_fcs_fabric_modinit(struct bfa_fcs_s *fcs)
1162{
1163	bfa_sm_send_event(&fcs->fabric, BFA_FCS_FABRIC_SM_CREATE);
1164	bfa_trc(fcs, 0);
1165}
1166
1167/*
1168 *   Module cleanup
1169 */
1170void
1171bfa_fcs_fabric_modexit(struct bfa_fcs_s *fcs)
1172{
1173	struct bfa_fcs_fabric_s *fabric;
1174
1175	bfa_trc(fcs, 0);
1176
1177	/*
1178	 * Cleanup base fabric.
1179	 */
1180	fabric = &fcs->fabric;
1181	bfa_lps_delete(fabric->lps);
1182	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_DELETE);
1183}
1184
1185/*
1186 * Fabric module stop -- stop FCS actions
1187 */
1188void
1189bfa_fcs_fabric_modstop(struct bfa_fcs_s *fcs)
1190{
1191	struct bfa_fcs_fabric_s *fabric;
1192
1193	bfa_trc(fcs, 0);
1194	fabric = &fcs->fabric;
1195	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_STOP);
1196}
1197
1198/*
1199 * Fabric module start -- kick starts FCS actions
1200 */
1201void
1202bfa_fcs_fabric_modstart(struct bfa_fcs_s *fcs)
1203{
1204	struct bfa_fcs_fabric_s *fabric;
1205
1206	bfa_trc(fcs, 0);
1207	fabric = &fcs->fabric;
1208	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_START);
1209}
1210
1211
1212/*
1213 *   Link up notification from BFA physical port module.
1214 */
1215void
1216bfa_fcs_fabric_link_up(struct bfa_fcs_fabric_s *fabric)
1217{
1218	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
1219	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_LINK_UP);
1220}
1221
1222/*
1223 *   Link down notification from BFA physical port module.
1224 */
1225void
1226bfa_fcs_fabric_link_down(struct bfa_fcs_fabric_s *fabric)
1227{
1228	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
1229	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_LINK_DOWN);
1230}
1231
1232/*
1233 *   A child vport is being created in the fabric.
1234 *
1235 *   Call from vport module at vport creation. A list of base port and vports
1236 *   belonging to a fabric is maintained to propagate link events.
1237 *
1238 *   param[in] fabric - Fabric instance. This can be a base fabric or vf.
1239 *   param[in] vport  - Vport being created.
1240 *
1241 *   @return None (always succeeds)
1242 */
1243void
1244bfa_fcs_fabric_addvport(struct bfa_fcs_fabric_s *fabric,
1245			struct bfa_fcs_vport_s *vport)
1246{
1247	/*
1248	 * - add vport to fabric's vport_q
1249	 */
1250	bfa_trc(fabric->fcs, fabric->vf_id);
1251
1252	list_add_tail(&vport->qe, &fabric->vport_q);
1253	fabric->num_vports++;
1254	bfa_wc_up(&fabric->wc);
1255}
1256
1257/*
1258 *   A child vport is being deleted from fabric.
1259 *
1260 *   Vport is being deleted.
1261 */
1262void
1263bfa_fcs_fabric_delvport(struct bfa_fcs_fabric_s *fabric,
1264			struct bfa_fcs_vport_s *vport)
1265{
1266	list_del(&vport->qe);
1267	fabric->num_vports--;
1268	bfa_wc_down(&fabric->wc);
1269}
1270
1271
1272/*
1273 * Lookup for a vport within a fabric given its pwwn
1274 */
1275struct bfa_fcs_vport_s *
1276bfa_fcs_fabric_vport_lookup(struct bfa_fcs_fabric_s *fabric, wwn_t pwwn)
1277{
1278	struct bfa_fcs_vport_s *vport;
1279	struct list_head	      *qe;
1280
1281	list_for_each(qe, &fabric->vport_q) {
1282		vport = (struct bfa_fcs_vport_s *) qe;
1283		if (bfa_fcs_lport_get_pwwn(&vport->lport) == pwwn)
1284			return vport;
1285	}
1286
1287	return NULL;
1288}
1289
1290
1291/*
1292 *  Get OUI of the attached switch.
1293 *
1294 *  Note : Use of this function should be avoided as much as possible.
1295 *         This function should be used only if there is any requirement
1296*          to check for FOS version below 6.3.
1297 *         To check if the attached fabric is a brocade fabric, use
1298 *         bfa_lps_is_brcd_fabric() which works for FOS versions 6.3
1299 *         or above only.
1300 */
1301
1302u16
1303bfa_fcs_fabric_get_switch_oui(struct bfa_fcs_fabric_s *fabric)
1304{
1305	wwn_t fab_nwwn;
1306	u8 *tmp;
1307	u16 oui;
1308
1309	fab_nwwn = fabric->lps->pr_nwwn;
1310
1311	tmp = (u8 *)&fab_nwwn;
1312	oui = (tmp[3] << 8) | tmp[4];
1313
1314	return oui;
1315}
1316/*
1317 *		Unsolicited frame receive handling.
1318 */
1319void
1320bfa_fcs_fabric_uf_recv(struct bfa_fcs_fabric_s *fabric, struct fchs_s *fchs,
1321		       u16 len)
1322{
1323	u32	pid = fchs->d_id;
1324	struct bfa_fcs_vport_s *vport;
1325	struct list_head	      *qe;
1326	struct fc_els_cmd_s *els_cmd = (struct fc_els_cmd_s *) (fchs + 1);
1327	struct fc_logi_s *flogi = (struct fc_logi_s *) els_cmd;
1328
1329	bfa_trc(fabric->fcs, len);
1330	bfa_trc(fabric->fcs, pid);
1331
1332	/*
1333	 * Look for our own FLOGI frames being looped back. This means an
1334	 * external loopback cable is in place. Our own FLOGI frames are
1335	 * sometimes looped back when switch port gets temporarily bypassed.
1336	 */
1337	if ((pid == bfa_ntoh3b(FC_FABRIC_PORT)) &&
1338	    (els_cmd->els_code == FC_ELS_FLOGI) &&
1339	    (flogi->port_name == bfa_fcs_lport_get_pwwn(&fabric->bport))) {
1340		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_LOOPBACK);
1341		return;
1342	}
1343
1344	/*
1345	 * FLOGI/EVFP exchanges should be consumed by base fabric.
1346	 */
1347	if (fchs->d_id == bfa_hton3b(FC_FABRIC_PORT)) {
1348		bfa_trc(fabric->fcs, pid);
1349		bfa_fcs_fabric_process_uf(fabric, fchs, len);
1350		return;
1351	}
1352
1353	if (fabric->bport.pid == pid) {
1354		/*
1355		 * All authentication frames should be routed to auth
1356		 */
1357		bfa_trc(fabric->fcs, els_cmd->els_code);
1358		if (els_cmd->els_code == FC_ELS_AUTH) {
1359			bfa_trc(fabric->fcs, els_cmd->els_code);
1360			return;
1361		}
1362
1363		bfa_trc(fabric->fcs, *(u8 *) ((u8 *) fchs));
1364		bfa_fcs_lport_uf_recv(&fabric->bport, fchs, len);
1365		return;
1366	}
1367
1368	/*
1369	 * look for a matching local port ID
1370	 */
1371	list_for_each(qe, &fabric->vport_q) {
1372		vport = (struct bfa_fcs_vport_s *) qe;
1373		if (vport->lport.pid == pid) {
1374			bfa_fcs_lport_uf_recv(&vport->lport, fchs, len);
1375			return;
1376		}
1377	}
1378
1379	if (!bfa_fcs_fabric_is_switched(fabric))
1380		bfa_fcs_lport_uf_recv(&fabric->bport, fchs, len);
1381
1382	bfa_trc(fabric->fcs, fchs->type);
1383}
1384
1385/*
1386 *		Unsolicited frames to be processed by fabric.
1387 */
1388static void
1389bfa_fcs_fabric_process_uf(struct bfa_fcs_fabric_s *fabric, struct fchs_s *fchs,
1390			  u16 len)
1391{
1392	struct fc_els_cmd_s *els_cmd = (struct fc_els_cmd_s *) (fchs + 1);
1393
1394	bfa_trc(fabric->fcs, els_cmd->els_code);
1395
1396	switch (els_cmd->els_code) {
1397	case FC_ELS_FLOGI:
1398		bfa_fcs_fabric_process_flogi(fabric, fchs, len);
1399		break;
1400
1401	default:
1402		/*
1403		 * need to generate a LS_RJT
1404		 */
1405		break;
1406	}
1407}
1408
1409/*
1410 *	Process	incoming FLOGI
1411 */
1412static void
1413bfa_fcs_fabric_process_flogi(struct bfa_fcs_fabric_s *fabric,
1414			struct fchs_s *fchs, u16 len)
1415{
1416	struct fc_logi_s *flogi = (struct fc_logi_s *) (fchs + 1);
1417	struct bfa_fcs_lport_s *bport = &fabric->bport;
1418
1419	bfa_trc(fabric->fcs, fchs->s_id);
1420
1421	fabric->stats.flogi_rcvd++;
1422	/*
1423	 * Check port type. It should be 0 = n-port.
1424	 */
1425	if (flogi->csp.port_type) {
1426		/*
1427		 * @todo: may need to send a LS_RJT
1428		 */
1429		bfa_trc(fabric->fcs, flogi->port_name);
1430		fabric->stats.flogi_rejected++;
1431		return;
1432	}
1433
1434	fabric->bb_credit = be16_to_cpu(flogi->csp.bbcred);
1435	bport->port_topo.pn2n.rem_port_wwn = flogi->port_name;
1436	bport->port_topo.pn2n.reply_oxid = fchs->ox_id;
1437
1438	/*
1439	 * Send a Flogi Acc
1440	 */
1441	bfa_fcs_fabric_send_flogi_acc(fabric);
1442	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_NO_FABRIC);
1443}
1444
1445static void
1446bfa_fcs_fabric_send_flogi_acc(struct bfa_fcs_fabric_s *fabric)
1447{
1448	struct bfa_lport_cfg_s *pcfg = &fabric->bport.port_cfg;
1449	struct bfa_fcs_lport_n2n_s *n2n_port = &fabric->bport.port_topo.pn2n;
1450	struct bfa_s	  *bfa = fabric->fcs->bfa;
1451	struct bfa_fcxp_s *fcxp;
1452	u16	reqlen;
1453	struct fchs_s	fchs;
1454
1455	fcxp = bfa_fcs_fcxp_alloc(fabric->fcs, BFA_FALSE);
1456	/*
1457	 * Do not expect this failure -- expect remote node to retry
1458	 */
1459	if (!fcxp)
1460		return;
1461
1462	reqlen = fc_flogi_acc_build(&fchs, bfa_fcxp_get_reqbuf(fcxp),
1463				    bfa_hton3b(FC_FABRIC_PORT),
1464				    n2n_port->reply_oxid, pcfg->pwwn,
1465				    pcfg->nwwn,
1466				    bfa_fcport_get_maxfrsize(bfa),
1467				    bfa_fcport_get_rx_bbcredit(bfa), 0);
1468
1469	bfa_fcxp_send(fcxp, NULL, fabric->vf_id, fabric->lps->bfa_tag,
1470		      BFA_FALSE, FC_CLASS_3,
1471		      reqlen, &fchs, bfa_fcs_fabric_flogiacc_comp, fabric,
1472		      FC_MAX_PDUSZ, 0);
1473}
1474
1475/*
1476 *   Flogi Acc completion callback.
1477 */
1478static void
1479bfa_fcs_fabric_flogiacc_comp(void *fcsarg, struct bfa_fcxp_s *fcxp, void *cbarg,
1480			     bfa_status_t status, u32 rsp_len,
1481			     u32 resid_len, struct fchs_s *rspfchs)
1482{
1483	struct bfa_fcs_fabric_s *fabric = cbarg;
1484
1485	bfa_trc(fabric->fcs, status);
1486}
1487
1488
1489/*
1490 * Send AEN notification
1491 */
1492static void
1493bfa_fcs_fabric_aen_post(struct bfa_fcs_lport_s *port,
1494			enum bfa_port_aen_event event)
1495{
1496	struct bfad_s *bfad = (struct bfad_s *)port->fabric->fcs->bfad;
1497	struct bfa_aen_entry_s  *aen_entry;
1498
1499	bfad_get_aen_entry(bfad, aen_entry);
1500	if (!aen_entry)
1501		return;
1502
1503	aen_entry->aen_data.port.pwwn = bfa_fcs_lport_get_pwwn(port);
1504	aen_entry->aen_data.port.fwwn = bfa_fcs_lport_get_fabric_name(port);
1505
1506	/* Send the AEN notification */
1507	bfad_im_post_vendor_event(aen_entry, bfad, ++port->fcs->fcs_aen_seq,
1508				  BFA_AEN_CAT_PORT, event);
1509}
1510
1511/*
1512 *
1513 * @param[in] fabric - fabric
1514 * @param[in] wwn_t - new fabric name
1515 *
1516 * @return - none
1517 */
1518void
1519bfa_fcs_fabric_set_fabric_name(struct bfa_fcs_fabric_s *fabric,
1520			       wwn_t fabric_name)
1521{
1522	struct bfad_s *bfad = (struct bfad_s *)fabric->fcs->bfad;
1523	char	pwwn_ptr[BFA_STRING_32];
1524	char	fwwn_ptr[BFA_STRING_32];
1525
1526	bfa_trc(fabric->fcs, fabric_name);
1527
1528	if (fabric->fabric_name == 0) {
1529		/*
1530		 * With BRCD switches, we don't get Fabric Name in FLOGI.
1531		 * Don't generate a fabric name change event in this case.
1532		 */
1533		fabric->fabric_name = fabric_name;
1534	} else {
1535		fabric->fabric_name = fabric_name;
1536		wwn2str(pwwn_ptr, bfa_fcs_lport_get_pwwn(&fabric->bport));
1537		wwn2str(fwwn_ptr,
1538			bfa_fcs_lport_get_fabric_name(&fabric->bport));
1539		BFA_LOG(KERN_WARNING, bfad, bfa_log_level,
1540			"Base port WWN = %s Fabric WWN = %s\n",
1541			pwwn_ptr, fwwn_ptr);
1542		bfa_fcs_fabric_aen_post(&fabric->bport,
1543				BFA_PORT_AEN_FABRIC_NAME_CHANGE);
1544	}
1545}
1546
1547void
1548bfa_cb_lps_flogo_comp(void *bfad, void *uarg)
1549{
1550	struct bfa_fcs_fabric_s *fabric = uarg;
1551	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_LOGOCOMP);
1552}
1553
1554/*
1555 *	Returns FCS vf structure for a given vf_id.
1556 *
1557 *	param[in]	vf_id - VF_ID
1558 *
1559 *	return
1560 *	If lookup succeeds, retuns fcs vf object, otherwise returns NULL
1561 */
1562bfa_fcs_vf_t   *
1563bfa_fcs_vf_lookup(struct bfa_fcs_s *fcs, u16 vf_id)
1564{
1565	bfa_trc(fcs, vf_id);
1566	if (vf_id == FC_VF_ID_NULL)
1567		return &fcs->fabric;
1568
1569	return NULL;
1570}
1571
1572/*
1573 *	Return the list of local logical ports present in the given VF.
1574 *
1575 *	@param[in]	vf	vf for which logical ports are returned
1576 *	@param[out]	lpwwn	returned logical port wwn list
1577 *	@param[in,out]	nlports in:size of lpwwn list;
1578 *				out:total elements present,
1579 *				actual elements returned is limited by the size
1580 */
1581void
1582bfa_fcs_vf_get_ports(bfa_fcs_vf_t *vf, wwn_t lpwwn[], int *nlports)
1583{
1584	struct list_head *qe;
1585	struct bfa_fcs_vport_s *vport;
1586	int	i = 0;
1587	struct bfa_fcs_s	*fcs;
1588
1589	if (vf == NULL || lpwwn == NULL || *nlports == 0)
1590		return;
1591
1592	fcs = vf->fcs;
1593
1594	bfa_trc(fcs, vf->vf_id);
1595	bfa_trc(fcs, (uint32_t) *nlports);
1596
1597	lpwwn[i++] = vf->bport.port_cfg.pwwn;
1598
1599	list_for_each(qe, &vf->vport_q) {
1600		if (i >= *nlports)
1601			break;
1602
1603		vport = (struct bfa_fcs_vport_s *) qe;
1604		lpwwn[i++] = vport->lport.port_cfg.pwwn;
1605	}
1606
1607	bfa_trc(fcs, i);
1608	*nlports = i;
1609}
1610
1611/*
1612 * BFA FCS PPORT ( physical port)
1613 */
1614static void
1615bfa_fcs_port_event_handler(void *cbarg, enum bfa_port_linkstate event)
1616{
1617	struct bfa_fcs_s      *fcs = cbarg;
1618
1619	bfa_trc(fcs, event);
1620
1621	switch (event) {
1622	case BFA_PORT_LINKUP:
1623		bfa_fcs_fabric_link_up(&fcs->fabric);
1624		break;
1625
1626	case BFA_PORT_LINKDOWN:
1627		bfa_fcs_fabric_link_down(&fcs->fabric);
1628		break;
1629
1630	default:
1631		WARN_ON(1);
1632	}
1633}
1634
1635void
1636bfa_fcs_port_attach(struct bfa_fcs_s *fcs)
1637{
1638	bfa_fcport_event_register(fcs->bfa, bfa_fcs_port_event_handler, fcs);
1639}
1640
1641/*
1642 * BFA FCS UF ( Unsolicited Frames)
1643 */
1644
1645/*
1646 *		BFA callback for unsolicited frame receive handler.
1647 *
1648 * @param[in]		cbarg		callback arg for receive handler
1649 * @param[in]		uf		unsolicited frame descriptor
1650 *
1651 * @return None
1652 */
1653static void
1654bfa_fcs_uf_recv(void *cbarg, struct bfa_uf_s *uf)
1655{
1656	struct bfa_fcs_s	*fcs = (struct bfa_fcs_s *) cbarg;
1657	struct fchs_s	*fchs = bfa_uf_get_frmbuf(uf);
1658	u16	len = bfa_uf_get_frmlen(uf);
1659	struct fc_vft_s *vft;
1660	struct bfa_fcs_fabric_s *fabric;
1661
1662	/*
1663	 * check for VFT header
1664	 */
1665	if (fchs->routing == FC_RTG_EXT_HDR &&
1666	    fchs->cat_info == FC_CAT_VFT_HDR) {
1667		bfa_stats(fcs, uf.tagged);
1668		vft = bfa_uf_get_frmbuf(uf);
1669		if (fcs->port_vfid == vft->vf_id)
1670			fabric = &fcs->fabric;
1671		else
1672			fabric = bfa_fcs_vf_lookup(fcs, (u16) vft->vf_id);
1673
1674		/*
1675		 * drop frame if vfid is unknown
1676		 */
1677		if (!fabric) {
1678			WARN_ON(1);
1679			bfa_stats(fcs, uf.vfid_unknown);
1680			bfa_uf_free(uf);
1681			return;
1682		}
1683
1684		/*
1685		 * skip vft header
1686		 */
1687		fchs = (struct fchs_s *) (vft + 1);
1688		len -= sizeof(struct fc_vft_s);
1689
1690		bfa_trc(fcs, vft->vf_id);
1691	} else {
1692		bfa_stats(fcs, uf.untagged);
1693		fabric = &fcs->fabric;
1694	}
1695
1696	bfa_trc(fcs, ((u32 *) fchs)[0]);
1697	bfa_trc(fcs, ((u32 *) fchs)[1]);
1698	bfa_trc(fcs, ((u32 *) fchs)[2]);
1699	bfa_trc(fcs, ((u32 *) fchs)[3]);
1700	bfa_trc(fcs, ((u32 *) fchs)[4]);
1701	bfa_trc(fcs, ((u32 *) fchs)[5]);
1702	bfa_trc(fcs, len);
1703
1704	bfa_fcs_fabric_uf_recv(fabric, fchs, len);
1705	bfa_uf_free(uf);
1706}
1707
1708void
1709bfa_fcs_uf_attach(struct bfa_fcs_s *fcs)
1710{
1711	bfa_uf_recv_register(fcs->bfa, bfa_fcs_uf_recv, fcs);
1712}
1713