Linux 3.4.102
[linux/fpc-iii.git] / drivers / net / ethernet / brocade / bna / bfa_ioc.c
blob77977d735dd7167dc8fe1f2690c87912ce1ecf0e
1 /*
2 * Linux network driver for Brocade Converged Network Adapter.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License (GPL) Version 2 as
6 * published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
14 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
15 * All rights reserved
16 * www.brocade.com
19 #include "bfa_ioc.h"
20 #include "bfi_reg.h"
21 #include "bfa_defs.h"
23 /**
24 * IOC local definitions
27 /**
28 * Asic specific macros : see bfa_hw_cb.c and bfa_hw_ct.c for details.
31 #define bfa_ioc_firmware_lock(__ioc) \
32 ((__ioc)->ioc_hwif->ioc_firmware_lock(__ioc))
33 #define bfa_ioc_firmware_unlock(__ioc) \
34 ((__ioc)->ioc_hwif->ioc_firmware_unlock(__ioc))
35 #define bfa_ioc_reg_init(__ioc) ((__ioc)->ioc_hwif->ioc_reg_init(__ioc))
36 #define bfa_ioc_map_port(__ioc) ((__ioc)->ioc_hwif->ioc_map_port(__ioc))
37 #define bfa_ioc_notify_fail(__ioc) \
38 ((__ioc)->ioc_hwif->ioc_notify_fail(__ioc))
39 #define bfa_ioc_sync_start(__ioc) \
40 ((__ioc)->ioc_hwif->ioc_sync_start(__ioc))
41 #define bfa_ioc_sync_join(__ioc) \
42 ((__ioc)->ioc_hwif->ioc_sync_join(__ioc))
43 #define bfa_ioc_sync_leave(__ioc) \
44 ((__ioc)->ioc_hwif->ioc_sync_leave(__ioc))
45 #define bfa_ioc_sync_ack(__ioc) \
46 ((__ioc)->ioc_hwif->ioc_sync_ack(__ioc))
47 #define bfa_ioc_sync_complete(__ioc) \
48 ((__ioc)->ioc_hwif->ioc_sync_complete(__ioc))
50 #define bfa_ioc_mbox_cmd_pending(__ioc) \
51 (!list_empty(&((__ioc)->mbox_mod.cmd_q)) || \
52 readl((__ioc)->ioc_regs.hfn_mbox_cmd))
54 static bool bfa_nw_auto_recover = true;
57 * forward declarations
59 static void bfa_ioc_hw_sem_init(struct bfa_ioc *ioc);
60 static void bfa_ioc_hw_sem_get(struct bfa_ioc *ioc);
61 static void bfa_ioc_hw_sem_get_cancel(struct bfa_ioc *ioc);
62 static void bfa_ioc_hwinit(struct bfa_ioc *ioc, bool force);
63 static void bfa_ioc_poll_fwinit(struct bfa_ioc *ioc);
64 static void bfa_ioc_send_enable(struct bfa_ioc *ioc);
65 static void bfa_ioc_send_disable(struct bfa_ioc *ioc);
66 static void bfa_ioc_send_getattr(struct bfa_ioc *ioc);
67 static void bfa_ioc_hb_monitor(struct bfa_ioc *ioc);
68 static void bfa_ioc_hb_stop(struct bfa_ioc *ioc);
69 static void bfa_ioc_reset(struct bfa_ioc *ioc, bool force);
70 static void bfa_ioc_mbox_poll(struct bfa_ioc *ioc);
71 static void bfa_ioc_mbox_flush(struct bfa_ioc *ioc);
72 static void bfa_ioc_recover(struct bfa_ioc *ioc);
73 static void bfa_ioc_check_attr_wwns(struct bfa_ioc *ioc);
74 static void bfa_ioc_event_notify(struct bfa_ioc *, enum bfa_ioc_event);
75 static void bfa_ioc_disable_comp(struct bfa_ioc *ioc);
76 static void bfa_ioc_lpu_stop(struct bfa_ioc *ioc);
77 static void bfa_nw_ioc_debug_save_ftrc(struct bfa_ioc *ioc);
78 static void bfa_ioc_fail_notify(struct bfa_ioc *ioc);
79 static void bfa_ioc_pf_enabled(struct bfa_ioc *ioc);
80 static void bfa_ioc_pf_disabled(struct bfa_ioc *ioc);
81 static void bfa_ioc_pf_failed(struct bfa_ioc *ioc);
82 static void bfa_ioc_pf_hwfailed(struct bfa_ioc *ioc);
83 static void bfa_ioc_pf_fwmismatch(struct bfa_ioc *ioc);
84 static void bfa_ioc_boot(struct bfa_ioc *ioc, u32 boot_type,
85 u32 boot_param);
86 static u32 bfa_ioc_smem_pgnum(struct bfa_ioc *ioc, u32 fmaddr);
87 static void bfa_ioc_get_adapter_serial_num(struct bfa_ioc *ioc,
88 char *serial_num);
89 static void bfa_ioc_get_adapter_fw_ver(struct bfa_ioc *ioc,
90 char *fw_ver);
91 static void bfa_ioc_get_pci_chip_rev(struct bfa_ioc *ioc,
92 char *chip_rev);
93 static void bfa_ioc_get_adapter_optrom_ver(struct bfa_ioc *ioc,
94 char *optrom_ver);
95 static void bfa_ioc_get_adapter_manufacturer(struct bfa_ioc *ioc,
96 char *manufacturer);
97 static void bfa_ioc_get_adapter_model(struct bfa_ioc *ioc, char *model);
98 static u64 bfa_ioc_get_pwwn(struct bfa_ioc *ioc);
101 * IOC state machine definitions/declarations
103 enum ioc_event {
104 IOC_E_RESET = 1, /*!< IOC reset request */
105 IOC_E_ENABLE = 2, /*!< IOC enable request */
106 IOC_E_DISABLE = 3, /*!< IOC disable request */
107 IOC_E_DETACH = 4, /*!< driver detach cleanup */
108 IOC_E_ENABLED = 5, /*!< f/w enabled */
109 IOC_E_FWRSP_GETATTR = 6, /*!< IOC get attribute response */
110 IOC_E_DISABLED = 7, /*!< f/w disabled */
111 IOC_E_PFFAILED = 8, /*!< failure notice by iocpf sm */
112 IOC_E_HBFAIL = 9, /*!< heartbeat failure */
113 IOC_E_HWERROR = 10, /*!< hardware error interrupt */
114 IOC_E_TIMEOUT = 11, /*!< timeout */
115 IOC_E_HWFAILED = 12, /*!< PCI mapping failure notice */
118 bfa_fsm_state_decl(bfa_ioc, uninit, struct bfa_ioc, enum ioc_event);
119 bfa_fsm_state_decl(bfa_ioc, reset, struct bfa_ioc, enum ioc_event);
120 bfa_fsm_state_decl(bfa_ioc, enabling, struct bfa_ioc, enum ioc_event);
121 bfa_fsm_state_decl(bfa_ioc, getattr, struct bfa_ioc, enum ioc_event);
122 bfa_fsm_state_decl(bfa_ioc, op, struct bfa_ioc, enum ioc_event);
123 bfa_fsm_state_decl(bfa_ioc, fail_retry, struct bfa_ioc, enum ioc_event);
124 bfa_fsm_state_decl(bfa_ioc, fail, struct bfa_ioc, enum ioc_event);
125 bfa_fsm_state_decl(bfa_ioc, disabling, struct bfa_ioc, enum ioc_event);
126 bfa_fsm_state_decl(bfa_ioc, disabled, struct bfa_ioc, enum ioc_event);
127 bfa_fsm_state_decl(bfa_ioc, hwfail, struct bfa_ioc, enum ioc_event);
129 static struct bfa_sm_table ioc_sm_table[] = {
130 {BFA_SM(bfa_ioc_sm_uninit), BFA_IOC_UNINIT},
131 {BFA_SM(bfa_ioc_sm_reset), BFA_IOC_RESET},
132 {BFA_SM(bfa_ioc_sm_enabling), BFA_IOC_ENABLING},
133 {BFA_SM(bfa_ioc_sm_getattr), BFA_IOC_GETATTR},
134 {BFA_SM(bfa_ioc_sm_op), BFA_IOC_OPERATIONAL},
135 {BFA_SM(bfa_ioc_sm_fail_retry), BFA_IOC_INITFAIL},
136 {BFA_SM(bfa_ioc_sm_fail), BFA_IOC_FAIL},
137 {BFA_SM(bfa_ioc_sm_disabling), BFA_IOC_DISABLING},
138 {BFA_SM(bfa_ioc_sm_disabled), BFA_IOC_DISABLED},
139 {BFA_SM(bfa_ioc_sm_hwfail), BFA_IOC_HWFAIL},
143 * Forward declareations for iocpf state machine
145 static void bfa_iocpf_enable(struct bfa_ioc *ioc);
146 static void bfa_iocpf_disable(struct bfa_ioc *ioc);
147 static void bfa_iocpf_fail(struct bfa_ioc *ioc);
148 static void bfa_iocpf_initfail(struct bfa_ioc *ioc);
149 static void bfa_iocpf_getattrfail(struct bfa_ioc *ioc);
150 static void bfa_iocpf_stop(struct bfa_ioc *ioc);
153 * IOCPF state machine events
155 enum iocpf_event {
156 IOCPF_E_ENABLE = 1, /*!< IOCPF enable request */
157 IOCPF_E_DISABLE = 2, /*!< IOCPF disable request */
158 IOCPF_E_STOP = 3, /*!< stop on driver detach */
159 IOCPF_E_FWREADY = 4, /*!< f/w initialization done */
160 IOCPF_E_FWRSP_ENABLE = 5, /*!< enable f/w response */
161 IOCPF_E_FWRSP_DISABLE = 6, /*!< disable f/w response */
162 IOCPF_E_FAIL = 7, /*!< failure notice by ioc sm */
163 IOCPF_E_INITFAIL = 8, /*!< init fail notice by ioc sm */
164 IOCPF_E_GETATTRFAIL = 9, /*!< init fail notice by ioc sm */
165 IOCPF_E_SEMLOCKED = 10, /*!< h/w semaphore is locked */
166 IOCPF_E_TIMEOUT = 11, /*!< f/w response timeout */
167 IOCPF_E_SEM_ERROR = 12, /*!< h/w sem mapping error */
171 * IOCPF states
173 enum bfa_iocpf_state {
174 BFA_IOCPF_RESET = 1, /*!< IOC is in reset state */
175 BFA_IOCPF_SEMWAIT = 2, /*!< Waiting for IOC h/w semaphore */
176 BFA_IOCPF_HWINIT = 3, /*!< IOC h/w is being initialized */
177 BFA_IOCPF_READY = 4, /*!< IOCPF is initialized */
178 BFA_IOCPF_INITFAIL = 5, /*!< IOCPF failed */
179 BFA_IOCPF_FAIL = 6, /*!< IOCPF failed */
180 BFA_IOCPF_DISABLING = 7, /*!< IOCPF is being disabled */
181 BFA_IOCPF_DISABLED = 8, /*!< IOCPF is disabled */
182 BFA_IOCPF_FWMISMATCH = 9, /*!< IOC f/w different from drivers */
185 bfa_fsm_state_decl(bfa_iocpf, reset, struct bfa_iocpf, enum iocpf_event);
186 bfa_fsm_state_decl(bfa_iocpf, fwcheck, struct bfa_iocpf, enum iocpf_event);
187 bfa_fsm_state_decl(bfa_iocpf, mismatch, struct bfa_iocpf, enum iocpf_event);
188 bfa_fsm_state_decl(bfa_iocpf, semwait, struct bfa_iocpf, enum iocpf_event);
189 bfa_fsm_state_decl(bfa_iocpf, hwinit, struct bfa_iocpf, enum iocpf_event);
190 bfa_fsm_state_decl(bfa_iocpf, enabling, struct bfa_iocpf, enum iocpf_event);
191 bfa_fsm_state_decl(bfa_iocpf, ready, struct bfa_iocpf, enum iocpf_event);
192 bfa_fsm_state_decl(bfa_iocpf, initfail_sync, struct bfa_iocpf,
193 enum iocpf_event);
194 bfa_fsm_state_decl(bfa_iocpf, initfail, struct bfa_iocpf, enum iocpf_event);
195 bfa_fsm_state_decl(bfa_iocpf, fail_sync, struct bfa_iocpf, enum iocpf_event);
196 bfa_fsm_state_decl(bfa_iocpf, fail, struct bfa_iocpf, enum iocpf_event);
197 bfa_fsm_state_decl(bfa_iocpf, disabling, struct bfa_iocpf, enum iocpf_event);
198 bfa_fsm_state_decl(bfa_iocpf, disabling_sync, struct bfa_iocpf,
199 enum iocpf_event);
200 bfa_fsm_state_decl(bfa_iocpf, disabled, struct bfa_iocpf, enum iocpf_event);
202 static struct bfa_sm_table iocpf_sm_table[] = {
203 {BFA_SM(bfa_iocpf_sm_reset), BFA_IOCPF_RESET},
204 {BFA_SM(bfa_iocpf_sm_fwcheck), BFA_IOCPF_FWMISMATCH},
205 {BFA_SM(bfa_iocpf_sm_mismatch), BFA_IOCPF_FWMISMATCH},
206 {BFA_SM(bfa_iocpf_sm_semwait), BFA_IOCPF_SEMWAIT},
207 {BFA_SM(bfa_iocpf_sm_hwinit), BFA_IOCPF_HWINIT},
208 {BFA_SM(bfa_iocpf_sm_enabling), BFA_IOCPF_HWINIT},
209 {BFA_SM(bfa_iocpf_sm_ready), BFA_IOCPF_READY},
210 {BFA_SM(bfa_iocpf_sm_initfail_sync), BFA_IOCPF_INITFAIL},
211 {BFA_SM(bfa_iocpf_sm_initfail), BFA_IOCPF_INITFAIL},
212 {BFA_SM(bfa_iocpf_sm_fail_sync), BFA_IOCPF_FAIL},
213 {BFA_SM(bfa_iocpf_sm_fail), BFA_IOCPF_FAIL},
214 {BFA_SM(bfa_iocpf_sm_disabling), BFA_IOCPF_DISABLING},
215 {BFA_SM(bfa_iocpf_sm_disabling_sync), BFA_IOCPF_DISABLING},
216 {BFA_SM(bfa_iocpf_sm_disabled), BFA_IOCPF_DISABLED},
220 * IOC State Machine
224 * Beginning state. IOC uninit state.
226 static void
227 bfa_ioc_sm_uninit_entry(struct bfa_ioc *ioc)
232 * IOC is in uninit state.
234 static void
235 bfa_ioc_sm_uninit(struct bfa_ioc *ioc, enum ioc_event event)
237 switch (event) {
238 case IOC_E_RESET:
239 bfa_fsm_set_state(ioc, bfa_ioc_sm_reset);
240 break;
242 default:
243 bfa_sm_fault(event);
248 * Reset entry actions -- initialize state machine
250 static void
251 bfa_ioc_sm_reset_entry(struct bfa_ioc *ioc)
253 bfa_fsm_set_state(&ioc->iocpf, bfa_iocpf_sm_reset);
257 * IOC is in reset state.
259 static void
260 bfa_ioc_sm_reset(struct bfa_ioc *ioc, enum ioc_event event)
262 switch (event) {
263 case IOC_E_ENABLE:
264 bfa_fsm_set_state(ioc, bfa_ioc_sm_enabling);
265 break;
267 case IOC_E_DISABLE:
268 bfa_ioc_disable_comp(ioc);
269 break;
271 case IOC_E_DETACH:
272 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
273 break;
275 default:
276 bfa_sm_fault(event);
280 static void
281 bfa_ioc_sm_enabling_entry(struct bfa_ioc *ioc)
283 bfa_iocpf_enable(ioc);
287 * Host IOC function is being enabled, awaiting response from firmware.
288 * Semaphore is acquired.
290 static void
291 bfa_ioc_sm_enabling(struct bfa_ioc *ioc, enum ioc_event event)
293 switch (event) {
294 case IOC_E_ENABLED:
295 bfa_fsm_set_state(ioc, bfa_ioc_sm_getattr);
296 break;
298 case IOC_E_PFFAILED:
299 /* !!! fall through !!! */
300 case IOC_E_HWERROR:
301 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
302 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
303 if (event != IOC_E_PFFAILED)
304 bfa_iocpf_initfail(ioc);
305 break;
307 case IOC_E_HWFAILED:
308 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
309 bfa_fsm_set_state(ioc, bfa_ioc_sm_hwfail);
310 break;
312 case IOC_E_DISABLE:
313 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
314 break;
316 case IOC_E_DETACH:
317 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
318 bfa_iocpf_stop(ioc);
319 break;
321 case IOC_E_ENABLE:
322 break;
324 default:
325 bfa_sm_fault(event);
330 * Semaphore should be acquired for version check.
332 static void
333 bfa_ioc_sm_getattr_entry(struct bfa_ioc *ioc)
335 mod_timer(&ioc->ioc_timer, jiffies +
336 msecs_to_jiffies(BFA_IOC_TOV));
337 bfa_ioc_send_getattr(ioc);
341 * IOC configuration in progress. Timer is active.
343 static void
344 bfa_ioc_sm_getattr(struct bfa_ioc *ioc, enum ioc_event event)
346 switch (event) {
347 case IOC_E_FWRSP_GETATTR:
348 del_timer(&ioc->ioc_timer);
349 bfa_ioc_check_attr_wwns(ioc);
350 bfa_ioc_hb_monitor(ioc);
351 bfa_fsm_set_state(ioc, bfa_ioc_sm_op);
352 break;
354 case IOC_E_PFFAILED:
355 case IOC_E_HWERROR:
356 del_timer(&ioc->ioc_timer);
357 /* fall through */
358 case IOC_E_TIMEOUT:
359 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
360 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
361 if (event != IOC_E_PFFAILED)
362 bfa_iocpf_getattrfail(ioc);
363 break;
365 case IOC_E_DISABLE:
366 del_timer(&ioc->ioc_timer);
367 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
368 break;
370 case IOC_E_ENABLE:
371 break;
373 default:
374 bfa_sm_fault(event);
378 static void
379 bfa_ioc_sm_op_entry(struct bfa_ioc *ioc)
381 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_OK);
382 bfa_ioc_event_notify(ioc, BFA_IOC_E_ENABLED);
385 static void
386 bfa_ioc_sm_op(struct bfa_ioc *ioc, enum ioc_event event)
388 switch (event) {
389 case IOC_E_ENABLE:
390 break;
392 case IOC_E_DISABLE:
393 bfa_ioc_hb_stop(ioc);
394 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
395 break;
397 case IOC_E_PFFAILED:
398 case IOC_E_HWERROR:
399 bfa_ioc_hb_stop(ioc);
400 /* !!! fall through !!! */
401 case IOC_E_HBFAIL:
402 if (ioc->iocpf.auto_recover)
403 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail_retry);
404 else
405 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
407 bfa_ioc_fail_notify(ioc);
409 if (event != IOC_E_PFFAILED)
410 bfa_iocpf_fail(ioc);
411 break;
413 default:
414 bfa_sm_fault(event);
418 static void
419 bfa_ioc_sm_disabling_entry(struct bfa_ioc *ioc)
421 bfa_iocpf_disable(ioc);
425 * IOC is being disabled
427 static void
428 bfa_ioc_sm_disabling(struct bfa_ioc *ioc, enum ioc_event event)
430 switch (event) {
431 case IOC_E_DISABLED:
432 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
433 break;
435 case IOC_E_HWERROR:
437 * No state change. Will move to disabled state
438 * after iocpf sm completes failure processing and
439 * moves to disabled state.
441 bfa_iocpf_fail(ioc);
442 break;
444 case IOC_E_HWFAILED:
445 bfa_fsm_set_state(ioc, bfa_ioc_sm_hwfail);
446 bfa_ioc_disable_comp(ioc);
447 break;
449 default:
450 bfa_sm_fault(event);
455 * IOC disable completion entry.
457 static void
458 bfa_ioc_sm_disabled_entry(struct bfa_ioc *ioc)
460 bfa_ioc_disable_comp(ioc);
463 static void
464 bfa_ioc_sm_disabled(struct bfa_ioc *ioc, enum ioc_event event)
466 switch (event) {
467 case IOC_E_ENABLE:
468 bfa_fsm_set_state(ioc, bfa_ioc_sm_enabling);
469 break;
471 case IOC_E_DISABLE:
472 ioc->cbfn->disable_cbfn(ioc->bfa);
473 break;
475 case IOC_E_DETACH:
476 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
477 bfa_iocpf_stop(ioc);
478 break;
480 default:
481 bfa_sm_fault(event);
485 static void
486 bfa_ioc_sm_fail_retry_entry(struct bfa_ioc *ioc)
491 * Hardware initialization retry.
493 static void
494 bfa_ioc_sm_fail_retry(struct bfa_ioc *ioc, enum ioc_event event)
496 switch (event) {
497 case IOC_E_ENABLED:
498 bfa_fsm_set_state(ioc, bfa_ioc_sm_getattr);
499 break;
501 case IOC_E_PFFAILED:
502 case IOC_E_HWERROR:
504 * Initialization retry failed.
506 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
507 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
508 if (event != IOC_E_PFFAILED)
509 bfa_iocpf_initfail(ioc);
510 break;
512 case IOC_E_HWFAILED:
513 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
514 bfa_fsm_set_state(ioc, bfa_ioc_sm_hwfail);
515 break;
517 case IOC_E_ENABLE:
518 break;
520 case IOC_E_DISABLE:
521 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
522 break;
524 case IOC_E_DETACH:
525 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
526 bfa_iocpf_stop(ioc);
527 break;
529 default:
530 bfa_sm_fault(event);
534 static void
535 bfa_ioc_sm_fail_entry(struct bfa_ioc *ioc)
540 * IOC failure.
542 static void
543 bfa_ioc_sm_fail(struct bfa_ioc *ioc, enum ioc_event event)
545 switch (event) {
546 case IOC_E_ENABLE:
547 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
548 break;
550 case IOC_E_DISABLE:
551 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
552 break;
554 case IOC_E_DETACH:
555 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
556 bfa_iocpf_stop(ioc);
557 break;
559 case IOC_E_HWERROR:
560 /* HB failure notification, ignore. */
561 break;
563 default:
564 bfa_sm_fault(event);
568 static void
569 bfa_ioc_sm_hwfail_entry(struct bfa_ioc *ioc)
574 * IOC failure.
576 static void
577 bfa_ioc_sm_hwfail(struct bfa_ioc *ioc, enum ioc_event event)
579 switch (event) {
581 case IOC_E_ENABLE:
582 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
583 break;
585 case IOC_E_DISABLE:
586 ioc->cbfn->disable_cbfn(ioc->bfa);
587 break;
589 case IOC_E_DETACH:
590 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
591 break;
593 default:
594 bfa_sm_fault(event);
599 * IOCPF State Machine
603 * Reset entry actions -- initialize state machine
605 static void
606 bfa_iocpf_sm_reset_entry(struct bfa_iocpf *iocpf)
608 iocpf->fw_mismatch_notified = false;
609 iocpf->auto_recover = bfa_nw_auto_recover;
613 * Beginning state. IOC is in reset state.
615 static void
616 bfa_iocpf_sm_reset(struct bfa_iocpf *iocpf, enum iocpf_event event)
618 switch (event) {
619 case IOCPF_E_ENABLE:
620 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fwcheck);
621 break;
623 case IOCPF_E_STOP:
624 break;
626 default:
627 bfa_sm_fault(event);
632 * Semaphore should be acquired for version check.
634 static void
635 bfa_iocpf_sm_fwcheck_entry(struct bfa_iocpf *iocpf)
637 bfa_ioc_hw_sem_init(iocpf->ioc);
638 bfa_ioc_hw_sem_get(iocpf->ioc);
642 * Awaiting h/w semaphore to continue with version check.
644 static void
645 bfa_iocpf_sm_fwcheck(struct bfa_iocpf *iocpf, enum iocpf_event event)
647 struct bfa_ioc *ioc = iocpf->ioc;
649 switch (event) {
650 case IOCPF_E_SEMLOCKED:
651 if (bfa_ioc_firmware_lock(ioc)) {
652 if (bfa_ioc_sync_start(ioc)) {
653 bfa_ioc_sync_join(ioc);
654 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_hwinit);
655 } else {
656 bfa_ioc_firmware_unlock(ioc);
657 bfa_nw_ioc_hw_sem_release(ioc);
658 mod_timer(&ioc->sem_timer, jiffies +
659 msecs_to_jiffies(BFA_IOC_HWSEM_TOV));
661 } else {
662 bfa_nw_ioc_hw_sem_release(ioc);
663 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_mismatch);
665 break;
667 case IOCPF_E_SEM_ERROR:
668 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
669 bfa_ioc_pf_hwfailed(ioc);
670 break;
672 case IOCPF_E_DISABLE:
673 bfa_ioc_hw_sem_get_cancel(ioc);
674 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
675 bfa_ioc_pf_disabled(ioc);
676 break;
678 case IOCPF_E_STOP:
679 bfa_ioc_hw_sem_get_cancel(ioc);
680 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
681 break;
683 default:
684 bfa_sm_fault(event);
689 * Notify enable completion callback
691 static void
692 bfa_iocpf_sm_mismatch_entry(struct bfa_iocpf *iocpf)
694 /* Call only the first time sm enters fwmismatch state. */
695 if (!iocpf->fw_mismatch_notified)
696 bfa_ioc_pf_fwmismatch(iocpf->ioc);
698 iocpf->fw_mismatch_notified = true;
699 mod_timer(&(iocpf->ioc)->iocpf_timer, jiffies +
700 msecs_to_jiffies(BFA_IOC_TOV));
704 * Awaiting firmware version match.
706 static void
707 bfa_iocpf_sm_mismatch(struct bfa_iocpf *iocpf, enum iocpf_event event)
709 struct bfa_ioc *ioc = iocpf->ioc;
711 switch (event) {
712 case IOCPF_E_TIMEOUT:
713 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fwcheck);
714 break;
716 case IOCPF_E_DISABLE:
717 del_timer(&ioc->iocpf_timer);
718 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
719 bfa_ioc_pf_disabled(ioc);
720 break;
722 case IOCPF_E_STOP:
723 del_timer(&ioc->iocpf_timer);
724 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
725 break;
727 default:
728 bfa_sm_fault(event);
733 * Request for semaphore.
735 static void
736 bfa_iocpf_sm_semwait_entry(struct bfa_iocpf *iocpf)
738 bfa_ioc_hw_sem_get(iocpf->ioc);
742 * Awaiting semaphore for h/w initialzation.
744 static void
745 bfa_iocpf_sm_semwait(struct bfa_iocpf *iocpf, enum iocpf_event event)
747 struct bfa_ioc *ioc = iocpf->ioc;
749 switch (event) {
750 case IOCPF_E_SEMLOCKED:
751 if (bfa_ioc_sync_complete(ioc)) {
752 bfa_ioc_sync_join(ioc);
753 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_hwinit);
754 } else {
755 bfa_nw_ioc_hw_sem_release(ioc);
756 mod_timer(&ioc->sem_timer, jiffies +
757 msecs_to_jiffies(BFA_IOC_HWSEM_TOV));
759 break;
761 case IOCPF_E_SEM_ERROR:
762 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
763 bfa_ioc_pf_hwfailed(ioc);
764 break;
766 case IOCPF_E_DISABLE:
767 bfa_ioc_hw_sem_get_cancel(ioc);
768 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
769 break;
771 default:
772 bfa_sm_fault(event);
776 static void
777 bfa_iocpf_sm_hwinit_entry(struct bfa_iocpf *iocpf)
779 iocpf->poll_time = 0;
780 bfa_ioc_reset(iocpf->ioc, false);
784 * Hardware is being initialized. Interrupts are enabled.
785 * Holding hardware semaphore lock.
787 static void
788 bfa_iocpf_sm_hwinit(struct bfa_iocpf *iocpf, enum iocpf_event event)
790 struct bfa_ioc *ioc = iocpf->ioc;
792 switch (event) {
793 case IOCPF_E_FWREADY:
794 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_enabling);
795 break;
797 case IOCPF_E_TIMEOUT:
798 bfa_nw_ioc_hw_sem_release(ioc);
799 bfa_ioc_pf_failed(ioc);
800 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_initfail_sync);
801 break;
803 case IOCPF_E_DISABLE:
804 del_timer(&ioc->iocpf_timer);
805 bfa_ioc_sync_leave(ioc);
806 bfa_nw_ioc_hw_sem_release(ioc);
807 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabled);
808 break;
810 default:
811 bfa_sm_fault(event);
815 static void
816 bfa_iocpf_sm_enabling_entry(struct bfa_iocpf *iocpf)
818 mod_timer(&(iocpf->ioc)->iocpf_timer, jiffies +
819 msecs_to_jiffies(BFA_IOC_TOV));
821 * Enable Interrupts before sending fw IOC ENABLE cmd.
823 iocpf->ioc->cbfn->reset_cbfn(iocpf->ioc->bfa);
824 bfa_ioc_send_enable(iocpf->ioc);
828 * Host IOC function is being enabled, awaiting response from firmware.
829 * Semaphore is acquired.
831 static void
832 bfa_iocpf_sm_enabling(struct bfa_iocpf *iocpf, enum iocpf_event event)
834 struct bfa_ioc *ioc = iocpf->ioc;
836 switch (event) {
837 case IOCPF_E_FWRSP_ENABLE:
838 del_timer(&ioc->iocpf_timer);
839 bfa_nw_ioc_hw_sem_release(ioc);
840 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_ready);
841 break;
843 case IOCPF_E_INITFAIL:
844 del_timer(&ioc->iocpf_timer);
846 * !!! fall through !!!
848 case IOCPF_E_TIMEOUT:
849 bfa_nw_ioc_hw_sem_release(ioc);
850 if (event == IOCPF_E_TIMEOUT)
851 bfa_ioc_pf_failed(ioc);
852 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_initfail_sync);
853 break;
855 case IOCPF_E_DISABLE:
856 del_timer(&ioc->iocpf_timer);
857 bfa_nw_ioc_hw_sem_release(ioc);
858 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling);
859 break;
861 default:
862 bfa_sm_fault(event);
866 static void
867 bfa_iocpf_sm_ready_entry(struct bfa_iocpf *iocpf)
869 bfa_ioc_pf_enabled(iocpf->ioc);
872 static void
873 bfa_iocpf_sm_ready(struct bfa_iocpf *iocpf, enum iocpf_event event)
875 switch (event) {
876 case IOCPF_E_DISABLE:
877 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling);
878 break;
880 case IOCPF_E_GETATTRFAIL:
881 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_initfail_sync);
882 break;
884 case IOCPF_E_FAIL:
885 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail_sync);
886 break;
888 default:
889 bfa_sm_fault(event);
893 static void
894 bfa_iocpf_sm_disabling_entry(struct bfa_iocpf *iocpf)
896 mod_timer(&(iocpf->ioc)->iocpf_timer, jiffies +
897 msecs_to_jiffies(BFA_IOC_TOV));
898 bfa_ioc_send_disable(iocpf->ioc);
902 * IOC is being disabled
904 static void
905 bfa_iocpf_sm_disabling(struct bfa_iocpf *iocpf, enum iocpf_event event)
907 struct bfa_ioc *ioc = iocpf->ioc;
909 switch (event) {
910 case IOCPF_E_FWRSP_DISABLE:
911 del_timer(&ioc->iocpf_timer);
912 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
913 break;
915 case IOCPF_E_FAIL:
916 del_timer(&ioc->iocpf_timer);
918 * !!! fall through !!!
921 case IOCPF_E_TIMEOUT:
922 writel(BFI_IOC_FAIL, ioc->ioc_regs.ioc_fwstate);
923 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
924 break;
926 case IOCPF_E_FWRSP_ENABLE:
927 break;
929 default:
930 bfa_sm_fault(event);
934 static void
935 bfa_iocpf_sm_disabling_sync_entry(struct bfa_iocpf *iocpf)
937 bfa_ioc_hw_sem_get(iocpf->ioc);
941 * IOC hb ack request is being removed.
943 static void
944 bfa_iocpf_sm_disabling_sync(struct bfa_iocpf *iocpf, enum iocpf_event event)
946 struct bfa_ioc *ioc = iocpf->ioc;
948 switch (event) {
949 case IOCPF_E_SEMLOCKED:
950 bfa_ioc_sync_leave(ioc);
951 bfa_nw_ioc_hw_sem_release(ioc);
952 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabled);
953 break;
955 case IOCPF_E_SEM_ERROR:
956 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
957 bfa_ioc_pf_hwfailed(ioc);
958 break;
960 case IOCPF_E_FAIL:
961 break;
963 default:
964 bfa_sm_fault(event);
969 * IOC disable completion entry.
971 static void
972 bfa_iocpf_sm_disabled_entry(struct bfa_iocpf *iocpf)
974 bfa_ioc_mbox_flush(iocpf->ioc);
975 bfa_ioc_pf_disabled(iocpf->ioc);
978 static void
979 bfa_iocpf_sm_disabled(struct bfa_iocpf *iocpf, enum iocpf_event event)
981 struct bfa_ioc *ioc = iocpf->ioc;
983 switch (event) {
984 case IOCPF_E_ENABLE:
985 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_semwait);
986 break;
988 case IOCPF_E_STOP:
989 bfa_ioc_firmware_unlock(ioc);
990 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
991 break;
993 default:
994 bfa_sm_fault(event);
998 static void
999 bfa_iocpf_sm_initfail_sync_entry(struct bfa_iocpf *iocpf)
1001 bfa_nw_ioc_debug_save_ftrc(iocpf->ioc);
1002 bfa_ioc_hw_sem_get(iocpf->ioc);
1006 * Hardware initialization failed.
1008 static void
1009 bfa_iocpf_sm_initfail_sync(struct bfa_iocpf *iocpf, enum iocpf_event event)
1011 struct bfa_ioc *ioc = iocpf->ioc;
1013 switch (event) {
1014 case IOCPF_E_SEMLOCKED:
1015 bfa_ioc_notify_fail(ioc);
1016 bfa_ioc_sync_leave(ioc);
1017 writel(BFI_IOC_FAIL, ioc->ioc_regs.ioc_fwstate);
1018 bfa_nw_ioc_hw_sem_release(ioc);
1019 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_initfail);
1020 break;
1022 case IOCPF_E_SEM_ERROR:
1023 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
1024 bfa_ioc_pf_hwfailed(ioc);
1025 break;
1027 case IOCPF_E_DISABLE:
1028 bfa_ioc_hw_sem_get_cancel(ioc);
1029 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
1030 break;
1032 case IOCPF_E_STOP:
1033 bfa_ioc_hw_sem_get_cancel(ioc);
1034 bfa_ioc_firmware_unlock(ioc);
1035 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
1036 break;
1038 case IOCPF_E_FAIL:
1039 break;
1041 default:
1042 bfa_sm_fault(event);
1046 static void
1047 bfa_iocpf_sm_initfail_entry(struct bfa_iocpf *iocpf)
1052 * Hardware initialization failed.
1054 static void
1055 bfa_iocpf_sm_initfail(struct bfa_iocpf *iocpf, enum iocpf_event event)
1057 struct bfa_ioc *ioc = iocpf->ioc;
1059 switch (event) {
1060 case IOCPF_E_DISABLE:
1061 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabled);
1062 break;
1064 case IOCPF_E_STOP:
1065 bfa_ioc_firmware_unlock(ioc);
1066 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
1067 break;
1069 default:
1070 bfa_sm_fault(event);
1074 static void
1075 bfa_iocpf_sm_fail_sync_entry(struct bfa_iocpf *iocpf)
1078 * Mark IOC as failed in hardware and stop firmware.
1080 bfa_ioc_lpu_stop(iocpf->ioc);
1083 * Flush any queued up mailbox requests.
1085 bfa_ioc_mbox_flush(iocpf->ioc);
1086 bfa_ioc_hw_sem_get(iocpf->ioc);
1090 * IOC is in failed state.
1092 static void
1093 bfa_iocpf_sm_fail_sync(struct bfa_iocpf *iocpf, enum iocpf_event event)
1095 struct bfa_ioc *ioc = iocpf->ioc;
1097 switch (event) {
1098 case IOCPF_E_SEMLOCKED:
1099 bfa_ioc_sync_ack(ioc);
1100 bfa_ioc_notify_fail(ioc);
1101 if (!iocpf->auto_recover) {
1102 bfa_ioc_sync_leave(ioc);
1103 writel(BFI_IOC_FAIL, ioc->ioc_regs.ioc_fwstate);
1104 bfa_nw_ioc_hw_sem_release(ioc);
1105 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
1106 } else {
1107 if (bfa_ioc_sync_complete(ioc))
1108 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_hwinit);
1109 else {
1110 bfa_nw_ioc_hw_sem_release(ioc);
1111 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_semwait);
1114 break;
1116 case IOCPF_E_SEM_ERROR:
1117 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
1118 bfa_ioc_pf_hwfailed(ioc);
1119 break;
1121 case IOCPF_E_DISABLE:
1122 bfa_ioc_hw_sem_get_cancel(ioc);
1123 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
1124 break;
1126 case IOCPF_E_FAIL:
1127 break;
1129 default:
1130 bfa_sm_fault(event);
1134 static void
1135 bfa_iocpf_sm_fail_entry(struct bfa_iocpf *iocpf)
1140 * @brief
1141 * IOC is in failed state.
1143 static void
1144 bfa_iocpf_sm_fail(struct bfa_iocpf *iocpf, enum iocpf_event event)
1146 switch (event) {
1147 case IOCPF_E_DISABLE:
1148 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabled);
1149 break;
1151 default:
1152 bfa_sm_fault(event);
1157 * BFA IOC private functions
1161 * Notify common modules registered for notification.
1163 static void
1164 bfa_ioc_event_notify(struct bfa_ioc *ioc, enum bfa_ioc_event event)
1166 struct bfa_ioc_notify *notify;
1167 struct list_head *qe;
1169 list_for_each(qe, &ioc->notify_q) {
1170 notify = (struct bfa_ioc_notify *)qe;
1171 notify->cbfn(notify->cbarg, event);
1175 static void
1176 bfa_ioc_disable_comp(struct bfa_ioc *ioc)
1178 ioc->cbfn->disable_cbfn(ioc->bfa);
1179 bfa_ioc_event_notify(ioc, BFA_IOC_E_DISABLED);
1182 bool
1183 bfa_nw_ioc_sem_get(void __iomem *sem_reg)
1185 u32 r32;
1186 int cnt = 0;
1187 #define BFA_SEM_SPINCNT 3000
1189 r32 = readl(sem_reg);
1191 while ((r32 & 1) && (cnt < BFA_SEM_SPINCNT)) {
1192 cnt++;
1193 udelay(2);
1194 r32 = readl(sem_reg);
1197 if (!(r32 & 1))
1198 return true;
1200 return false;
1203 void
1204 bfa_nw_ioc_sem_release(void __iomem *sem_reg)
1206 readl(sem_reg);
1207 writel(1, sem_reg);
1210 static void
1211 bfa_ioc_hw_sem_init(struct bfa_ioc *ioc)
1213 struct bfi_ioc_image_hdr fwhdr;
1214 u32 fwstate = readl(ioc->ioc_regs.ioc_fwstate);
1216 if (fwstate == BFI_IOC_UNINIT)
1217 return;
1219 bfa_nw_ioc_fwver_get(ioc, &fwhdr);
1221 if (swab32(fwhdr.exec) == BFI_FWBOOT_TYPE_NORMAL)
1222 return;
1224 writel(BFI_IOC_UNINIT, ioc->ioc_regs.ioc_fwstate);
1227 * Try to lock and then unlock the semaphore.
1229 readl(ioc->ioc_regs.ioc_sem_reg);
1230 writel(1, ioc->ioc_regs.ioc_sem_reg);
1233 static void
1234 bfa_ioc_hw_sem_get(struct bfa_ioc *ioc)
1236 u32 r32;
1239 * First read to the semaphore register will return 0, subsequent reads
1240 * will return 1. Semaphore is released by writing 1 to the register
1242 r32 = readl(ioc->ioc_regs.ioc_sem_reg);
1243 if (r32 == ~0) {
1244 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_SEM_ERROR);
1245 return;
1247 if (!(r32 & 1)) {
1248 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_SEMLOCKED);
1249 return;
1252 mod_timer(&ioc->sem_timer, jiffies +
1253 msecs_to_jiffies(BFA_IOC_HWSEM_TOV));
1256 void
1257 bfa_nw_ioc_hw_sem_release(struct bfa_ioc *ioc)
1259 writel(1, ioc->ioc_regs.ioc_sem_reg);
1262 static void
1263 bfa_ioc_hw_sem_get_cancel(struct bfa_ioc *ioc)
1265 del_timer(&ioc->sem_timer);
1269 * @brief
1270 * Initialize LPU local memory (aka secondary memory / SRAM)
1272 static void
1273 bfa_ioc_lmem_init(struct bfa_ioc *ioc)
1275 u32 pss_ctl;
1276 int i;
1277 #define PSS_LMEM_INIT_TIME 10000
1279 pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1280 pss_ctl &= ~__PSS_LMEM_RESET;
1281 pss_ctl |= __PSS_LMEM_INIT_EN;
1284 * i2c workaround 12.5khz clock
1286 pss_ctl |= __PSS_I2C_CLK_DIV(3UL);
1287 writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1290 * wait for memory initialization to be complete
1292 i = 0;
1293 do {
1294 pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1295 i++;
1296 } while (!(pss_ctl & __PSS_LMEM_INIT_DONE) && (i < PSS_LMEM_INIT_TIME));
1299 * If memory initialization is not successful, IOC timeout will catch
1300 * such failures.
1302 BUG_ON(!(pss_ctl & __PSS_LMEM_INIT_DONE));
1304 pss_ctl &= ~(__PSS_LMEM_INIT_DONE | __PSS_LMEM_INIT_EN);
1305 writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1308 static void
1309 bfa_ioc_lpu_start(struct bfa_ioc *ioc)
1311 u32 pss_ctl;
1314 * Take processor out of reset.
1316 pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1317 pss_ctl &= ~__PSS_LPU0_RESET;
1319 writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1322 static void
1323 bfa_ioc_lpu_stop(struct bfa_ioc *ioc)
1325 u32 pss_ctl;
1328 * Put processors in reset.
1330 pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1331 pss_ctl |= (__PSS_LPU0_RESET | __PSS_LPU1_RESET);
1333 writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1337 * Get driver and firmware versions.
1339 void
1340 bfa_nw_ioc_fwver_get(struct bfa_ioc *ioc, struct bfi_ioc_image_hdr *fwhdr)
1342 u32 pgnum;
1343 u32 loff = 0;
1344 int i;
1345 u32 *fwsig = (u32 *) fwhdr;
1347 pgnum = bfa_ioc_smem_pgnum(ioc, loff);
1348 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
1350 for (i = 0; i < (sizeof(struct bfi_ioc_image_hdr) / sizeof(u32));
1351 i++) {
1352 fwsig[i] =
1353 swab32(readl((loff) + (ioc->ioc_regs.smem_page_start)));
1354 loff += sizeof(u32);
1359 * Returns TRUE if same.
1361 bool
1362 bfa_nw_ioc_fwver_cmp(struct bfa_ioc *ioc, struct bfi_ioc_image_hdr *fwhdr)
1364 struct bfi_ioc_image_hdr *drv_fwhdr;
1365 int i;
1367 drv_fwhdr = (struct bfi_ioc_image_hdr *)
1368 bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc), 0);
1370 for (i = 0; i < BFI_IOC_MD5SUM_SZ; i++) {
1371 if (fwhdr->md5sum[i] != drv_fwhdr->md5sum[i])
1372 return false;
1375 return true;
1379 * Return true if current running version is valid. Firmware signature and
1380 * execution context (driver/bios) must match.
1382 static bool
1383 bfa_ioc_fwver_valid(struct bfa_ioc *ioc, u32 boot_env)
1385 struct bfi_ioc_image_hdr fwhdr, *drv_fwhdr;
1387 bfa_nw_ioc_fwver_get(ioc, &fwhdr);
1388 drv_fwhdr = (struct bfi_ioc_image_hdr *)
1389 bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc), 0);
1391 if (fwhdr.signature != drv_fwhdr->signature)
1392 return false;
1394 if (swab32(fwhdr.bootenv) != boot_env)
1395 return false;
1397 return bfa_nw_ioc_fwver_cmp(ioc, &fwhdr);
1401 * Conditionally flush any pending message from firmware at start.
1403 static void
1404 bfa_ioc_msgflush(struct bfa_ioc *ioc)
1406 u32 r32;
1408 r32 = readl(ioc->ioc_regs.lpu_mbox_cmd);
1409 if (r32)
1410 writel(1, ioc->ioc_regs.lpu_mbox_cmd);
1414 * @img ioc_init_logic.jpg
1416 static void
1417 bfa_ioc_hwinit(struct bfa_ioc *ioc, bool force)
1419 enum bfi_ioc_state ioc_fwstate;
1420 bool fwvalid;
1421 u32 boot_env;
1423 ioc_fwstate = readl(ioc->ioc_regs.ioc_fwstate);
1425 if (force)
1426 ioc_fwstate = BFI_IOC_UNINIT;
1428 boot_env = BFI_FWBOOT_ENV_OS;
1431 * check if firmware is valid
1433 fwvalid = (ioc_fwstate == BFI_IOC_UNINIT) ?
1434 false : bfa_ioc_fwver_valid(ioc, boot_env);
1436 if (!fwvalid) {
1437 bfa_ioc_boot(ioc, BFI_FWBOOT_TYPE_NORMAL, boot_env);
1438 bfa_ioc_poll_fwinit(ioc);
1439 return;
1443 * If hardware initialization is in progress (initialized by other IOC),
1444 * just wait for an initialization completion interrupt.
1446 if (ioc_fwstate == BFI_IOC_INITING) {
1447 bfa_ioc_poll_fwinit(ioc);
1448 return;
1452 * If IOC function is disabled and firmware version is same,
1453 * just re-enable IOC.
1455 if (ioc_fwstate == BFI_IOC_DISABLED || ioc_fwstate == BFI_IOC_OP) {
1457 * When using MSI-X any pending firmware ready event should
1458 * be flushed. Otherwise MSI-X interrupts are not delivered.
1460 bfa_ioc_msgflush(ioc);
1461 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_FWREADY);
1462 return;
1466 * Initialize the h/w for any other states.
1468 bfa_ioc_boot(ioc, BFI_FWBOOT_TYPE_NORMAL, boot_env);
1469 bfa_ioc_poll_fwinit(ioc);
1472 void
1473 bfa_nw_ioc_timeout(void *ioc_arg)
1475 struct bfa_ioc *ioc = (struct bfa_ioc *) ioc_arg;
1477 bfa_fsm_send_event(ioc, IOC_E_TIMEOUT);
1480 static void
1481 bfa_ioc_mbox_send(struct bfa_ioc *ioc, void *ioc_msg, int len)
1483 u32 *msgp = (u32 *) ioc_msg;
1484 u32 i;
1486 BUG_ON(!(len <= BFI_IOC_MSGLEN_MAX));
1489 * first write msg to mailbox registers
1491 for (i = 0; i < len / sizeof(u32); i++)
1492 writel(cpu_to_le32(msgp[i]),
1493 ioc->ioc_regs.hfn_mbox + i * sizeof(u32));
1495 for (; i < BFI_IOC_MSGLEN_MAX / sizeof(u32); i++)
1496 writel(0, ioc->ioc_regs.hfn_mbox + i * sizeof(u32));
1499 * write 1 to mailbox CMD to trigger LPU event
1501 writel(1, ioc->ioc_regs.hfn_mbox_cmd);
1502 (void) readl(ioc->ioc_regs.hfn_mbox_cmd);
1505 static void
1506 bfa_ioc_send_enable(struct bfa_ioc *ioc)
1508 struct bfi_ioc_ctrl_req enable_req;
1509 struct timeval tv;
1511 bfi_h2i_set(enable_req.mh, BFI_MC_IOC, BFI_IOC_H2I_ENABLE_REQ,
1512 bfa_ioc_portid(ioc));
1513 enable_req.clscode = htons(ioc->clscode);
1514 do_gettimeofday(&tv);
1515 enable_req.tv_sec = ntohl(tv.tv_sec);
1516 bfa_ioc_mbox_send(ioc, &enable_req, sizeof(struct bfi_ioc_ctrl_req));
1519 static void
1520 bfa_ioc_send_disable(struct bfa_ioc *ioc)
1522 struct bfi_ioc_ctrl_req disable_req;
1524 bfi_h2i_set(disable_req.mh, BFI_MC_IOC, BFI_IOC_H2I_DISABLE_REQ,
1525 bfa_ioc_portid(ioc));
1526 bfa_ioc_mbox_send(ioc, &disable_req, sizeof(struct bfi_ioc_ctrl_req));
1529 static void
1530 bfa_ioc_send_getattr(struct bfa_ioc *ioc)
1532 struct bfi_ioc_getattr_req attr_req;
1534 bfi_h2i_set(attr_req.mh, BFI_MC_IOC, BFI_IOC_H2I_GETATTR_REQ,
1535 bfa_ioc_portid(ioc));
1536 bfa_dma_be_addr_set(attr_req.attr_addr, ioc->attr_dma.pa);
1537 bfa_ioc_mbox_send(ioc, &attr_req, sizeof(attr_req));
1540 void
1541 bfa_nw_ioc_hb_check(void *cbarg)
1543 struct bfa_ioc *ioc = cbarg;
1544 u32 hb_count;
1546 hb_count = readl(ioc->ioc_regs.heartbeat);
1547 if (ioc->hb_count == hb_count) {
1548 bfa_ioc_recover(ioc);
1549 return;
1550 } else {
1551 ioc->hb_count = hb_count;
1554 bfa_ioc_mbox_poll(ioc);
1555 mod_timer(&ioc->hb_timer, jiffies +
1556 msecs_to_jiffies(BFA_IOC_HB_TOV));
1559 static void
1560 bfa_ioc_hb_monitor(struct bfa_ioc *ioc)
1562 ioc->hb_count = readl(ioc->ioc_regs.heartbeat);
1563 mod_timer(&ioc->hb_timer, jiffies +
1564 msecs_to_jiffies(BFA_IOC_HB_TOV));
1567 static void
1568 bfa_ioc_hb_stop(struct bfa_ioc *ioc)
1570 del_timer(&ioc->hb_timer);
1574 * @brief
1575 * Initiate a full firmware download.
1577 static void
1578 bfa_ioc_download_fw(struct bfa_ioc *ioc, u32 boot_type,
1579 u32 boot_env)
1581 u32 *fwimg;
1582 u32 pgnum;
1583 u32 loff = 0;
1584 u32 chunkno = 0;
1585 u32 i;
1586 u32 asicmode;
1589 * Initialize LMEM first before code download
1591 bfa_ioc_lmem_init(ioc);
1593 fwimg = bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc), chunkno);
1595 pgnum = bfa_ioc_smem_pgnum(ioc, loff);
1597 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
1599 for (i = 0; i < bfa_cb_image_get_size(bfa_ioc_asic_gen(ioc)); i++) {
1600 if (BFA_IOC_FLASH_CHUNK_NO(i) != chunkno) {
1601 chunkno = BFA_IOC_FLASH_CHUNK_NO(i);
1602 fwimg = bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc),
1603 BFA_IOC_FLASH_CHUNK_ADDR(chunkno));
1607 * write smem
1609 writel((swab32(fwimg[BFA_IOC_FLASH_OFFSET_IN_CHUNK(i)])),
1610 ((ioc->ioc_regs.smem_page_start) + (loff)));
1612 loff += sizeof(u32);
1615 * handle page offset wrap around
1617 loff = PSS_SMEM_PGOFF(loff);
1618 if (loff == 0) {
1619 pgnum++;
1620 writel(pgnum,
1621 ioc->ioc_regs.host_page_num_fn);
1625 writel(bfa_ioc_smem_pgnum(ioc, 0),
1626 ioc->ioc_regs.host_page_num_fn);
1629 * Set boot type, env and device mode at the end.
1631 asicmode = BFI_FWBOOT_DEVMODE(ioc->asic_gen, ioc->asic_mode,
1632 ioc->port0_mode, ioc->port1_mode);
1633 writel(asicmode, ((ioc->ioc_regs.smem_page_start)
1634 + BFI_FWBOOT_DEVMODE_OFF));
1635 writel(boot_type, ((ioc->ioc_regs.smem_page_start)
1636 + (BFI_FWBOOT_TYPE_OFF)));
1637 writel(boot_env, ((ioc->ioc_regs.smem_page_start)
1638 + (BFI_FWBOOT_ENV_OFF)));
1641 static void
1642 bfa_ioc_reset(struct bfa_ioc *ioc, bool force)
1644 bfa_ioc_hwinit(ioc, force);
1648 * BFA ioc enable reply by firmware
1650 static void
1651 bfa_ioc_enable_reply(struct bfa_ioc *ioc, enum bfa_mode port_mode,
1652 u8 cap_bm)
1654 struct bfa_iocpf *iocpf = &ioc->iocpf;
1656 ioc->port_mode = ioc->port_mode_cfg = port_mode;
1657 ioc->ad_cap_bm = cap_bm;
1658 bfa_fsm_send_event(iocpf, IOCPF_E_FWRSP_ENABLE);
1662 * @brief
1663 * Update BFA configuration from firmware configuration.
1665 static void
1666 bfa_ioc_getattr_reply(struct bfa_ioc *ioc)
1668 struct bfi_ioc_attr *attr = ioc->attr;
1670 attr->adapter_prop = ntohl(attr->adapter_prop);
1671 attr->card_type = ntohl(attr->card_type);
1672 attr->maxfrsize = ntohs(attr->maxfrsize);
1674 bfa_fsm_send_event(ioc, IOC_E_FWRSP_GETATTR);
1678 * Attach time initialization of mbox logic.
1680 static void
1681 bfa_ioc_mbox_attach(struct bfa_ioc *ioc)
1683 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
1684 int mc;
1686 INIT_LIST_HEAD(&mod->cmd_q);
1687 for (mc = 0; mc < BFI_MC_MAX; mc++) {
1688 mod->mbhdlr[mc].cbfn = NULL;
1689 mod->mbhdlr[mc].cbarg = ioc->bfa;
1694 * Mbox poll timer -- restarts any pending mailbox requests.
1696 static void
1697 bfa_ioc_mbox_poll(struct bfa_ioc *ioc)
1699 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
1700 struct bfa_mbox_cmd *cmd;
1701 bfa_mbox_cmd_cbfn_t cbfn;
1702 void *cbarg;
1703 u32 stat;
1706 * If no command pending, do nothing
1708 if (list_empty(&mod->cmd_q))
1709 return;
1712 * If previous command is not yet fetched by firmware, do nothing
1714 stat = readl(ioc->ioc_regs.hfn_mbox_cmd);
1715 if (stat)
1716 return;
1719 * Enqueue command to firmware.
1721 bfa_q_deq(&mod->cmd_q, &cmd);
1722 bfa_ioc_mbox_send(ioc, cmd->msg, sizeof(cmd->msg));
1725 * Give a callback to the client, indicating that the command is sent
1727 if (cmd->cbfn) {
1728 cbfn = cmd->cbfn;
1729 cbarg = cmd->cbarg;
1730 cmd->cbfn = NULL;
1731 cbfn(cbarg);
1736 * Cleanup any pending requests.
1738 static void
1739 bfa_ioc_mbox_flush(struct bfa_ioc *ioc)
1741 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
1742 struct bfa_mbox_cmd *cmd;
1744 while (!list_empty(&mod->cmd_q))
1745 bfa_q_deq(&mod->cmd_q, &cmd);
1749 * Read data from SMEM to host through PCI memmap
1751 * @param[in] ioc memory for IOC
1752 * @param[in] tbuf app memory to store data from smem
1753 * @param[in] soff smem offset
1754 * @param[in] sz size of smem in bytes
1756 static int
1757 bfa_nw_ioc_smem_read(struct bfa_ioc *ioc, void *tbuf, u32 soff, u32 sz)
1759 u32 pgnum, loff, r32;
1760 int i, len;
1761 u32 *buf = tbuf;
1763 pgnum = PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, soff);
1764 loff = PSS_SMEM_PGOFF(soff);
1767 * Hold semaphore to serialize pll init and fwtrc.
1769 if (bfa_nw_ioc_sem_get(ioc->ioc_regs.ioc_init_sem_reg) == 0)
1770 return 1;
1772 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
1774 len = sz/sizeof(u32);
1775 for (i = 0; i < len; i++) {
1776 r32 = swab32(readl((loff) + (ioc->ioc_regs.smem_page_start)));
1777 buf[i] = be32_to_cpu(r32);
1778 loff += sizeof(u32);
1781 * handle page offset wrap around
1783 loff = PSS_SMEM_PGOFF(loff);
1784 if (loff == 0) {
1785 pgnum++;
1786 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
1790 writel(PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, 0),
1791 ioc->ioc_regs.host_page_num_fn);
1794 * release semaphore
1796 readl(ioc->ioc_regs.ioc_init_sem_reg);
1797 writel(1, ioc->ioc_regs.ioc_init_sem_reg);
1798 return 0;
1802 * Retrieve saved firmware trace from a prior IOC failure.
1805 bfa_nw_ioc_debug_fwtrc(struct bfa_ioc *ioc, void *trcdata, int *trclen)
1807 u32 loff = BFI_IOC_TRC_OFF + BNA_DBG_FWTRC_LEN * ioc->port_id;
1808 int tlen, status = 0;
1810 tlen = *trclen;
1811 if (tlen > BNA_DBG_FWTRC_LEN)
1812 tlen = BNA_DBG_FWTRC_LEN;
1814 status = bfa_nw_ioc_smem_read(ioc, trcdata, loff, tlen);
1815 *trclen = tlen;
1816 return status;
1820 * Save firmware trace if configured.
1822 static void
1823 bfa_nw_ioc_debug_save_ftrc(struct bfa_ioc *ioc)
1825 int tlen;
1827 if (ioc->dbg_fwsave_once) {
1828 ioc->dbg_fwsave_once = 0;
1829 if (ioc->dbg_fwsave_len) {
1830 tlen = ioc->dbg_fwsave_len;
1831 bfa_nw_ioc_debug_fwtrc(ioc, ioc->dbg_fwsave, &tlen);
1837 * Retrieve saved firmware trace from a prior IOC failure.
1840 bfa_nw_ioc_debug_fwsave(struct bfa_ioc *ioc, void *trcdata, int *trclen)
1842 int tlen;
1844 if (ioc->dbg_fwsave_len == 0)
1845 return BFA_STATUS_ENOFSAVE;
1847 tlen = *trclen;
1848 if (tlen > ioc->dbg_fwsave_len)
1849 tlen = ioc->dbg_fwsave_len;
1851 memcpy(trcdata, ioc->dbg_fwsave, tlen);
1852 *trclen = tlen;
1853 return BFA_STATUS_OK;
1856 static void
1857 bfa_ioc_fail_notify(struct bfa_ioc *ioc)
1860 * Notify driver and common modules registered for notification.
1862 ioc->cbfn->hbfail_cbfn(ioc->bfa);
1863 bfa_ioc_event_notify(ioc, BFA_IOC_E_FAILED);
1864 bfa_nw_ioc_debug_save_ftrc(ioc);
1868 * IOCPF to IOC interface
1870 static void
1871 bfa_ioc_pf_enabled(struct bfa_ioc *ioc)
1873 bfa_fsm_send_event(ioc, IOC_E_ENABLED);
1876 static void
1877 bfa_ioc_pf_disabled(struct bfa_ioc *ioc)
1879 bfa_fsm_send_event(ioc, IOC_E_DISABLED);
1882 static void
1883 bfa_ioc_pf_failed(struct bfa_ioc *ioc)
1885 bfa_fsm_send_event(ioc, IOC_E_PFFAILED);
1888 static void
1889 bfa_ioc_pf_hwfailed(struct bfa_ioc *ioc)
1891 bfa_fsm_send_event(ioc, IOC_E_HWFAILED);
1894 static void
1895 bfa_ioc_pf_fwmismatch(struct bfa_ioc *ioc)
1898 * Provide enable completion callback and AEN notification.
1900 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
1904 * IOC public
1906 static enum bfa_status
1907 bfa_ioc_pll_init(struct bfa_ioc *ioc)
1910 * Hold semaphore so that nobody can access the chip during init.
1912 bfa_nw_ioc_sem_get(ioc->ioc_regs.ioc_init_sem_reg);
1914 bfa_ioc_pll_init_asic(ioc);
1916 ioc->pllinit = true;
1918 * release semaphore.
1920 bfa_nw_ioc_sem_release(ioc->ioc_regs.ioc_init_sem_reg);
1922 return BFA_STATUS_OK;
1926 * Interface used by diag module to do firmware boot with memory test
1927 * as the entry vector.
1929 static void
1930 bfa_ioc_boot(struct bfa_ioc *ioc, enum bfi_fwboot_type boot_type,
1931 u32 boot_env)
1933 bfa_ioc_stats(ioc, ioc_boots);
1935 if (bfa_ioc_pll_init(ioc) != BFA_STATUS_OK)
1936 return;
1939 * Initialize IOC state of all functions on a chip reset.
1941 if (boot_type == BFI_FWBOOT_TYPE_MEMTEST) {
1942 writel(BFI_IOC_MEMTEST, ioc->ioc_regs.ioc_fwstate);
1943 writel(BFI_IOC_MEMTEST, ioc->ioc_regs.alt_ioc_fwstate);
1944 } else {
1945 writel(BFI_IOC_INITING, ioc->ioc_regs.ioc_fwstate);
1946 writel(BFI_IOC_INITING, ioc->ioc_regs.alt_ioc_fwstate);
1949 bfa_ioc_msgflush(ioc);
1950 bfa_ioc_download_fw(ioc, boot_type, boot_env);
1951 bfa_ioc_lpu_start(ioc);
1955 * Enable/disable IOC failure auto recovery.
1957 void
1958 bfa_nw_ioc_auto_recover(bool auto_recover)
1960 bfa_nw_auto_recover = auto_recover;
1963 static bool
1964 bfa_ioc_msgget(struct bfa_ioc *ioc, void *mbmsg)
1966 u32 *msgp = mbmsg;
1967 u32 r32;
1968 int i;
1970 r32 = readl(ioc->ioc_regs.lpu_mbox_cmd);
1971 if ((r32 & 1) == 0)
1972 return false;
1975 * read the MBOX msg
1977 for (i = 0; i < (sizeof(union bfi_ioc_i2h_msg_u) / sizeof(u32));
1978 i++) {
1979 r32 = readl(ioc->ioc_regs.lpu_mbox +
1980 i * sizeof(u32));
1981 msgp[i] = htonl(r32);
1985 * turn off mailbox interrupt by clearing mailbox status
1987 writel(1, ioc->ioc_regs.lpu_mbox_cmd);
1988 readl(ioc->ioc_regs.lpu_mbox_cmd);
1990 return true;
1993 static void
1994 bfa_ioc_isr(struct bfa_ioc *ioc, struct bfi_mbmsg *m)
1996 union bfi_ioc_i2h_msg_u *msg;
1997 struct bfa_iocpf *iocpf = &ioc->iocpf;
1999 msg = (union bfi_ioc_i2h_msg_u *) m;
2001 bfa_ioc_stats(ioc, ioc_isrs);
2003 switch (msg->mh.msg_id) {
2004 case BFI_IOC_I2H_HBEAT:
2005 break;
2007 case BFI_IOC_I2H_ENABLE_REPLY:
2008 bfa_ioc_enable_reply(ioc,
2009 (enum bfa_mode)msg->fw_event.port_mode,
2010 msg->fw_event.cap_bm);
2011 break;
2013 case BFI_IOC_I2H_DISABLE_REPLY:
2014 bfa_fsm_send_event(iocpf, IOCPF_E_FWRSP_DISABLE);
2015 break;
2017 case BFI_IOC_I2H_GETATTR_REPLY:
2018 bfa_ioc_getattr_reply(ioc);
2019 break;
2021 default:
2022 BUG_ON(1);
2027 * IOC attach time initialization and setup.
2029 * @param[in] ioc memory for IOC
2030 * @param[in] bfa driver instance structure
2032 void
2033 bfa_nw_ioc_attach(struct bfa_ioc *ioc, void *bfa, struct bfa_ioc_cbfn *cbfn)
2035 ioc->bfa = bfa;
2036 ioc->cbfn = cbfn;
2037 ioc->fcmode = false;
2038 ioc->pllinit = false;
2039 ioc->dbg_fwsave_once = true;
2040 ioc->iocpf.ioc = ioc;
2042 bfa_ioc_mbox_attach(ioc);
2043 INIT_LIST_HEAD(&ioc->notify_q);
2045 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
2046 bfa_fsm_send_event(ioc, IOC_E_RESET);
2050 * Driver detach time IOC cleanup.
2052 void
2053 bfa_nw_ioc_detach(struct bfa_ioc *ioc)
2055 bfa_fsm_send_event(ioc, IOC_E_DETACH);
2057 /* Done with detach, empty the notify_q. */
2058 INIT_LIST_HEAD(&ioc->notify_q);
2062 * Setup IOC PCI properties.
2064 * @param[in] pcidev PCI device information for this IOC
2066 void
2067 bfa_nw_ioc_pci_init(struct bfa_ioc *ioc, struct bfa_pcidev *pcidev,
2068 enum bfi_pcifn_class clscode)
2070 ioc->clscode = clscode;
2071 ioc->pcidev = *pcidev;
2074 * Initialize IOC and device personality
2076 ioc->port0_mode = ioc->port1_mode = BFI_PORT_MODE_FC;
2077 ioc->asic_mode = BFI_ASIC_MODE_FC;
2079 switch (pcidev->device_id) {
2080 case PCI_DEVICE_ID_BROCADE_CT:
2081 ioc->asic_gen = BFI_ASIC_GEN_CT;
2082 ioc->port0_mode = ioc->port1_mode = BFI_PORT_MODE_ETH;
2083 ioc->asic_mode = BFI_ASIC_MODE_ETH;
2084 ioc->port_mode = ioc->port_mode_cfg = BFA_MODE_CNA;
2085 ioc->ad_cap_bm = BFA_CM_CNA;
2086 break;
2088 case BFA_PCI_DEVICE_ID_CT2:
2089 ioc->asic_gen = BFI_ASIC_GEN_CT2;
2090 if (clscode == BFI_PCIFN_CLASS_FC &&
2091 pcidev->ssid == BFA_PCI_CT2_SSID_FC) {
2092 ioc->asic_mode = BFI_ASIC_MODE_FC16;
2093 ioc->fcmode = true;
2094 ioc->port_mode = ioc->port_mode_cfg = BFA_MODE_HBA;
2095 ioc->ad_cap_bm = BFA_CM_HBA;
2096 } else {
2097 ioc->port0_mode = ioc->port1_mode = BFI_PORT_MODE_ETH;
2098 ioc->asic_mode = BFI_ASIC_MODE_ETH;
2099 if (pcidev->ssid == BFA_PCI_CT2_SSID_FCoE) {
2100 ioc->port_mode =
2101 ioc->port_mode_cfg = BFA_MODE_CNA;
2102 ioc->ad_cap_bm = BFA_CM_CNA;
2103 } else {
2104 ioc->port_mode =
2105 ioc->port_mode_cfg = BFA_MODE_NIC;
2106 ioc->ad_cap_bm = BFA_CM_NIC;
2109 break;
2111 default:
2112 BUG_ON(1);
2116 * Set asic specific interfaces.
2118 if (ioc->asic_gen == BFI_ASIC_GEN_CT)
2119 bfa_nw_ioc_set_ct_hwif(ioc);
2120 else {
2121 WARN_ON(ioc->asic_gen != BFI_ASIC_GEN_CT2);
2122 bfa_nw_ioc_set_ct2_hwif(ioc);
2123 bfa_nw_ioc_ct2_poweron(ioc);
2126 bfa_ioc_map_port(ioc);
2127 bfa_ioc_reg_init(ioc);
2131 * Initialize IOC dma memory
2133 * @param[in] dm_kva kernel virtual address of IOC dma memory
2134 * @param[in] dm_pa physical address of IOC dma memory
2136 void
2137 bfa_nw_ioc_mem_claim(struct bfa_ioc *ioc, u8 *dm_kva, u64 dm_pa)
2140 * dma memory for firmware attribute
2142 ioc->attr_dma.kva = dm_kva;
2143 ioc->attr_dma.pa = dm_pa;
2144 ioc->attr = (struct bfi_ioc_attr *) dm_kva;
2148 * Return size of dma memory required.
2151 bfa_nw_ioc_meminfo(void)
2153 return roundup(sizeof(struct bfi_ioc_attr), BFA_DMA_ALIGN_SZ);
2156 void
2157 bfa_nw_ioc_enable(struct bfa_ioc *ioc)
2159 bfa_ioc_stats(ioc, ioc_enables);
2160 ioc->dbg_fwsave_once = true;
2162 bfa_fsm_send_event(ioc, IOC_E_ENABLE);
2165 void
2166 bfa_nw_ioc_disable(struct bfa_ioc *ioc)
2168 bfa_ioc_stats(ioc, ioc_disables);
2169 bfa_fsm_send_event(ioc, IOC_E_DISABLE);
2173 * Initialize memory for saving firmware trace.
2175 void
2176 bfa_nw_ioc_debug_memclaim(struct bfa_ioc *ioc, void *dbg_fwsave)
2178 ioc->dbg_fwsave = dbg_fwsave;
2179 ioc->dbg_fwsave_len = ioc->iocpf.auto_recover ? BNA_DBG_FWTRC_LEN : 0;
2182 static u32
2183 bfa_ioc_smem_pgnum(struct bfa_ioc *ioc, u32 fmaddr)
2185 return PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, fmaddr);
2189 * Register mailbox message handler function, to be called by common modules
2191 void
2192 bfa_nw_ioc_mbox_regisr(struct bfa_ioc *ioc, enum bfi_mclass mc,
2193 bfa_ioc_mbox_mcfunc_t cbfn, void *cbarg)
2195 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2197 mod->mbhdlr[mc].cbfn = cbfn;
2198 mod->mbhdlr[mc].cbarg = cbarg;
2202 * Queue a mailbox command request to firmware. Waits if mailbox is busy.
2203 * Responsibility of caller to serialize
2205 * @param[in] ioc IOC instance
2206 * @param[i] cmd Mailbox command
2208 bool
2209 bfa_nw_ioc_mbox_queue(struct bfa_ioc *ioc, struct bfa_mbox_cmd *cmd,
2210 bfa_mbox_cmd_cbfn_t cbfn, void *cbarg)
2212 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2213 u32 stat;
2215 cmd->cbfn = cbfn;
2216 cmd->cbarg = cbarg;
2219 * If a previous command is pending, queue new command
2221 if (!list_empty(&mod->cmd_q)) {
2222 list_add_tail(&cmd->qe, &mod->cmd_q);
2223 return true;
2227 * If mailbox is busy, queue command for poll timer
2229 stat = readl(ioc->ioc_regs.hfn_mbox_cmd);
2230 if (stat) {
2231 list_add_tail(&cmd->qe, &mod->cmd_q);
2232 return true;
2236 * mailbox is free -- queue command to firmware
2238 bfa_ioc_mbox_send(ioc, cmd->msg, sizeof(cmd->msg));
2240 return false;
2244 * Handle mailbox interrupts
2246 void
2247 bfa_nw_ioc_mbox_isr(struct bfa_ioc *ioc)
2249 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2250 struct bfi_mbmsg m;
2251 int mc;
2253 if (bfa_ioc_msgget(ioc, &m)) {
2255 * Treat IOC message class as special.
2257 mc = m.mh.msg_class;
2258 if (mc == BFI_MC_IOC) {
2259 bfa_ioc_isr(ioc, &m);
2260 return;
2263 if ((mc >= BFI_MC_MAX) || (mod->mbhdlr[mc].cbfn == NULL))
2264 return;
2266 mod->mbhdlr[mc].cbfn(mod->mbhdlr[mc].cbarg, &m);
2269 bfa_ioc_lpu_read_stat(ioc);
2272 * Try to send pending mailbox commands
2274 bfa_ioc_mbox_poll(ioc);
2277 void
2278 bfa_nw_ioc_error_isr(struct bfa_ioc *ioc)
2280 bfa_ioc_stats(ioc, ioc_hbfails);
2281 bfa_ioc_stats_hb_count(ioc, ioc->hb_count);
2282 bfa_fsm_send_event(ioc, IOC_E_HWERROR);
2286 * return true if IOC is disabled
2288 bool
2289 bfa_nw_ioc_is_disabled(struct bfa_ioc *ioc)
2291 return bfa_fsm_cmp_state(ioc, bfa_ioc_sm_disabling) ||
2292 bfa_fsm_cmp_state(ioc, bfa_ioc_sm_disabled);
2296 * return true if IOC is operational
2298 bool
2299 bfa_nw_ioc_is_operational(struct bfa_ioc *ioc)
2301 return bfa_fsm_cmp_state(ioc, bfa_ioc_sm_op);
2305 * Add to IOC heartbeat failure notification queue. To be used by common
2306 * modules such as cee, port, diag.
2308 void
2309 bfa_nw_ioc_notify_register(struct bfa_ioc *ioc,
2310 struct bfa_ioc_notify *notify)
2312 list_add_tail(&notify->qe, &ioc->notify_q);
2315 #define BFA_MFG_NAME "Brocade"
2316 static void
2317 bfa_ioc_get_adapter_attr(struct bfa_ioc *ioc,
2318 struct bfa_adapter_attr *ad_attr)
2320 struct bfi_ioc_attr *ioc_attr;
2322 ioc_attr = ioc->attr;
2324 bfa_ioc_get_adapter_serial_num(ioc, ad_attr->serial_num);
2325 bfa_ioc_get_adapter_fw_ver(ioc, ad_attr->fw_ver);
2326 bfa_ioc_get_adapter_optrom_ver(ioc, ad_attr->optrom_ver);
2327 bfa_ioc_get_adapter_manufacturer(ioc, ad_attr->manufacturer);
2328 memcpy(&ad_attr->vpd, &ioc_attr->vpd,
2329 sizeof(struct bfa_mfg_vpd));
2331 ad_attr->nports = bfa_ioc_get_nports(ioc);
2332 ad_attr->max_speed = bfa_ioc_speed_sup(ioc);
2334 bfa_ioc_get_adapter_model(ioc, ad_attr->model);
2335 /* For now, model descr uses same model string */
2336 bfa_ioc_get_adapter_model(ioc, ad_attr->model_descr);
2338 ad_attr->card_type = ioc_attr->card_type;
2339 ad_attr->is_mezz = bfa_mfg_is_mezz(ioc_attr->card_type);
2341 if (BFI_ADAPTER_IS_SPECIAL(ioc_attr->adapter_prop))
2342 ad_attr->prototype = 1;
2343 else
2344 ad_attr->prototype = 0;
2346 ad_attr->pwwn = bfa_ioc_get_pwwn(ioc);
2347 ad_attr->mac = bfa_nw_ioc_get_mac(ioc);
2349 ad_attr->pcie_gen = ioc_attr->pcie_gen;
2350 ad_attr->pcie_lanes = ioc_attr->pcie_lanes;
2351 ad_attr->pcie_lanes_orig = ioc_attr->pcie_lanes_orig;
2352 ad_attr->asic_rev = ioc_attr->asic_rev;
2354 bfa_ioc_get_pci_chip_rev(ioc, ad_attr->hw_ver);
2357 static enum bfa_ioc_type
2358 bfa_ioc_get_type(struct bfa_ioc *ioc)
2360 if (ioc->clscode == BFI_PCIFN_CLASS_ETH)
2361 return BFA_IOC_TYPE_LL;
2363 BUG_ON(!(ioc->clscode == BFI_PCIFN_CLASS_FC));
2365 return (ioc->attr->port_mode == BFI_PORT_MODE_FC)
2366 ? BFA_IOC_TYPE_FC : BFA_IOC_TYPE_FCoE;
2369 static void
2370 bfa_ioc_get_adapter_serial_num(struct bfa_ioc *ioc, char *serial_num)
2372 memset(serial_num, 0, BFA_ADAPTER_SERIAL_NUM_LEN);
2373 memcpy(serial_num,
2374 (void *)ioc->attr->brcd_serialnum,
2375 BFA_ADAPTER_SERIAL_NUM_LEN);
2378 static void
2379 bfa_ioc_get_adapter_fw_ver(struct bfa_ioc *ioc, char *fw_ver)
2381 memset(fw_ver, 0, BFA_VERSION_LEN);
2382 memcpy(fw_ver, ioc->attr->fw_version, BFA_VERSION_LEN);
2385 static void
2386 bfa_ioc_get_pci_chip_rev(struct bfa_ioc *ioc, char *chip_rev)
2388 BUG_ON(!(chip_rev));
2390 memset(chip_rev, 0, BFA_IOC_CHIP_REV_LEN);
2392 chip_rev[0] = 'R';
2393 chip_rev[1] = 'e';
2394 chip_rev[2] = 'v';
2395 chip_rev[3] = '-';
2396 chip_rev[4] = ioc->attr->asic_rev;
2397 chip_rev[5] = '\0';
2400 static void
2401 bfa_ioc_get_adapter_optrom_ver(struct bfa_ioc *ioc, char *optrom_ver)
2403 memset(optrom_ver, 0, BFA_VERSION_LEN);
2404 memcpy(optrom_ver, ioc->attr->optrom_version,
2405 BFA_VERSION_LEN);
2408 static void
2409 bfa_ioc_get_adapter_manufacturer(struct bfa_ioc *ioc, char *manufacturer)
2411 memset(manufacturer, 0, BFA_ADAPTER_MFG_NAME_LEN);
2412 memcpy(manufacturer, BFA_MFG_NAME, BFA_ADAPTER_MFG_NAME_LEN);
2415 static void
2416 bfa_ioc_get_adapter_model(struct bfa_ioc *ioc, char *model)
2418 struct bfi_ioc_attr *ioc_attr;
2420 BUG_ON(!(model));
2421 memset(model, 0, BFA_ADAPTER_MODEL_NAME_LEN);
2423 ioc_attr = ioc->attr;
2425 snprintf(model, BFA_ADAPTER_MODEL_NAME_LEN, "%s-%u",
2426 BFA_MFG_NAME, ioc_attr->card_type);
2429 static enum bfa_ioc_state
2430 bfa_ioc_get_state(struct bfa_ioc *ioc)
2432 enum bfa_iocpf_state iocpf_st;
2433 enum bfa_ioc_state ioc_st = bfa_sm_to_state(ioc_sm_table, ioc->fsm);
2435 if (ioc_st == BFA_IOC_ENABLING ||
2436 ioc_st == BFA_IOC_FAIL || ioc_st == BFA_IOC_INITFAIL) {
2438 iocpf_st = bfa_sm_to_state(iocpf_sm_table, ioc->iocpf.fsm);
2440 switch (iocpf_st) {
2441 case BFA_IOCPF_SEMWAIT:
2442 ioc_st = BFA_IOC_SEMWAIT;
2443 break;
2445 case BFA_IOCPF_HWINIT:
2446 ioc_st = BFA_IOC_HWINIT;
2447 break;
2449 case BFA_IOCPF_FWMISMATCH:
2450 ioc_st = BFA_IOC_FWMISMATCH;
2451 break;
2453 case BFA_IOCPF_FAIL:
2454 ioc_st = BFA_IOC_FAIL;
2455 break;
2457 case BFA_IOCPF_INITFAIL:
2458 ioc_st = BFA_IOC_INITFAIL;
2459 break;
2461 default:
2462 break;
2465 return ioc_st;
2468 void
2469 bfa_nw_ioc_get_attr(struct bfa_ioc *ioc, struct bfa_ioc_attr *ioc_attr)
2471 memset((void *)ioc_attr, 0, sizeof(struct bfa_ioc_attr));
2473 ioc_attr->state = bfa_ioc_get_state(ioc);
2474 ioc_attr->port_id = ioc->port_id;
2475 ioc_attr->port_mode = ioc->port_mode;
2477 ioc_attr->port_mode_cfg = ioc->port_mode_cfg;
2478 ioc_attr->cap_bm = ioc->ad_cap_bm;
2480 ioc_attr->ioc_type = bfa_ioc_get_type(ioc);
2482 bfa_ioc_get_adapter_attr(ioc, &ioc_attr->adapter_attr);
2484 ioc_attr->pci_attr.device_id = ioc->pcidev.device_id;
2485 ioc_attr->pci_attr.pcifn = ioc->pcidev.pci_func;
2486 bfa_ioc_get_pci_chip_rev(ioc, ioc_attr->pci_attr.chip_rev);
2490 * WWN public
2492 static u64
2493 bfa_ioc_get_pwwn(struct bfa_ioc *ioc)
2495 return ioc->attr->pwwn;
2498 mac_t
2499 bfa_nw_ioc_get_mac(struct bfa_ioc *ioc)
2501 return ioc->attr->mac;
2505 * Firmware failure detected. Start recovery actions.
2507 static void
2508 bfa_ioc_recover(struct bfa_ioc *ioc)
2510 pr_crit("Heart Beat of IOC has failed\n");
2511 bfa_ioc_stats(ioc, ioc_hbfails);
2512 bfa_ioc_stats_hb_count(ioc, ioc->hb_count);
2513 bfa_fsm_send_event(ioc, IOC_E_HBFAIL);
2516 static void
2517 bfa_ioc_check_attr_wwns(struct bfa_ioc *ioc)
2519 if (bfa_ioc_get_type(ioc) == BFA_IOC_TYPE_LL)
2520 return;
2524 * @dg hal_iocpf_pvt BFA IOC PF private functions
2525 * @{
2528 static void
2529 bfa_iocpf_enable(struct bfa_ioc *ioc)
2531 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_ENABLE);
2534 static void
2535 bfa_iocpf_disable(struct bfa_ioc *ioc)
2537 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_DISABLE);
2540 static void
2541 bfa_iocpf_fail(struct bfa_ioc *ioc)
2543 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_FAIL);
2546 static void
2547 bfa_iocpf_initfail(struct bfa_ioc *ioc)
2549 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_INITFAIL);
2552 static void
2553 bfa_iocpf_getattrfail(struct bfa_ioc *ioc)
2555 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_GETATTRFAIL);
2558 static void
2559 bfa_iocpf_stop(struct bfa_ioc *ioc)
2561 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_STOP);
2564 void
2565 bfa_nw_iocpf_timeout(void *ioc_arg)
2567 struct bfa_ioc *ioc = (struct bfa_ioc *) ioc_arg;
2568 enum bfa_iocpf_state iocpf_st;
2570 iocpf_st = bfa_sm_to_state(iocpf_sm_table, ioc->iocpf.fsm);
2572 if (iocpf_st == BFA_IOCPF_HWINIT)
2573 bfa_ioc_poll_fwinit(ioc);
2574 else
2575 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_TIMEOUT);
2578 void
2579 bfa_nw_iocpf_sem_timeout(void *ioc_arg)
2581 struct bfa_ioc *ioc = (struct bfa_ioc *) ioc_arg;
2583 bfa_ioc_hw_sem_get(ioc);
2586 static void
2587 bfa_ioc_poll_fwinit(struct bfa_ioc *ioc)
2589 u32 fwstate = readl(ioc->ioc_regs.ioc_fwstate);
2591 if (fwstate == BFI_IOC_DISABLED) {
2592 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_FWREADY);
2593 return;
2596 if (ioc->iocpf.poll_time >= BFA_IOC_TOV) {
2597 bfa_nw_iocpf_timeout(ioc);
2598 } else {
2599 ioc->iocpf.poll_time += BFA_IOC_POLL_TOV;
2600 mod_timer(&ioc->iocpf_timer, jiffies +
2601 msecs_to_jiffies(BFA_IOC_POLL_TOV));
2606 * Flash module specific
2610 * FLASH DMA buffer should be big enough to hold both MFG block and
2611 * asic block(64k) at the same time and also should be 2k aligned to
2612 * avoid write segement to cross sector boundary.
2614 #define BFA_FLASH_SEG_SZ 2048
2615 #define BFA_FLASH_DMA_BUF_SZ \
2616 roundup(0x010000 + sizeof(struct bfa_mfg_block), BFA_FLASH_SEG_SZ)
2618 static void
2619 bfa_flash_cb(struct bfa_flash *flash)
2621 flash->op_busy = 0;
2622 if (flash->cbfn)
2623 flash->cbfn(flash->cbarg, flash->status);
2626 static void
2627 bfa_flash_notify(void *cbarg, enum bfa_ioc_event event)
2629 struct bfa_flash *flash = cbarg;
2631 switch (event) {
2632 case BFA_IOC_E_DISABLED:
2633 case BFA_IOC_E_FAILED:
2634 if (flash->op_busy) {
2635 flash->status = BFA_STATUS_IOC_FAILURE;
2636 flash->cbfn(flash->cbarg, flash->status);
2637 flash->op_busy = 0;
2639 break;
2640 default:
2641 break;
2646 * Send flash write request.
2648 * @param[in] cbarg - callback argument
2650 static void
2651 bfa_flash_write_send(struct bfa_flash *flash)
2653 struct bfi_flash_write_req *msg =
2654 (struct bfi_flash_write_req *) flash->mb.msg;
2655 u32 len;
2657 msg->type = be32_to_cpu(flash->type);
2658 msg->instance = flash->instance;
2659 msg->offset = be32_to_cpu(flash->addr_off + flash->offset);
2660 len = (flash->residue < BFA_FLASH_DMA_BUF_SZ) ?
2661 flash->residue : BFA_FLASH_DMA_BUF_SZ;
2662 msg->length = be32_to_cpu(len);
2664 /* indicate if it's the last msg of the whole write operation */
2665 msg->last = (len == flash->residue) ? 1 : 0;
2667 bfi_h2i_set(msg->mh, BFI_MC_FLASH, BFI_FLASH_H2I_WRITE_REQ,
2668 bfa_ioc_portid(flash->ioc));
2669 bfa_alen_set(&msg->alen, len, flash->dbuf_pa);
2670 memcpy(flash->dbuf_kva, flash->ubuf + flash->offset, len);
2671 bfa_nw_ioc_mbox_queue(flash->ioc, &flash->mb, NULL, NULL);
2673 flash->residue -= len;
2674 flash->offset += len;
2678 * Send flash read request.
2680 * @param[in] cbarg - callback argument
2682 static void
2683 bfa_flash_read_send(void *cbarg)
2685 struct bfa_flash *flash = cbarg;
2686 struct bfi_flash_read_req *msg =
2687 (struct bfi_flash_read_req *) flash->mb.msg;
2688 u32 len;
2690 msg->type = be32_to_cpu(flash->type);
2691 msg->instance = flash->instance;
2692 msg->offset = be32_to_cpu(flash->addr_off + flash->offset);
2693 len = (flash->residue < BFA_FLASH_DMA_BUF_SZ) ?
2694 flash->residue : BFA_FLASH_DMA_BUF_SZ;
2695 msg->length = be32_to_cpu(len);
2696 bfi_h2i_set(msg->mh, BFI_MC_FLASH, BFI_FLASH_H2I_READ_REQ,
2697 bfa_ioc_portid(flash->ioc));
2698 bfa_alen_set(&msg->alen, len, flash->dbuf_pa);
2699 bfa_nw_ioc_mbox_queue(flash->ioc, &flash->mb, NULL, NULL);
2703 * Process flash response messages upon receiving interrupts.
2705 * @param[in] flasharg - flash structure
2706 * @param[in] msg - message structure
2708 static void
2709 bfa_flash_intr(void *flasharg, struct bfi_mbmsg *msg)
2711 struct bfa_flash *flash = flasharg;
2712 u32 status;
2714 union {
2715 struct bfi_flash_query_rsp *query;
2716 struct bfi_flash_write_rsp *write;
2717 struct bfi_flash_read_rsp *read;
2718 struct bfi_mbmsg *msg;
2719 } m;
2721 m.msg = msg;
2723 /* receiving response after ioc failure */
2724 if (!flash->op_busy && msg->mh.msg_id != BFI_FLASH_I2H_EVENT)
2725 return;
2727 switch (msg->mh.msg_id) {
2728 case BFI_FLASH_I2H_QUERY_RSP:
2729 status = be32_to_cpu(m.query->status);
2730 if (status == BFA_STATUS_OK) {
2731 u32 i;
2732 struct bfa_flash_attr *attr, *f;
2734 attr = (struct bfa_flash_attr *) flash->ubuf;
2735 f = (struct bfa_flash_attr *) flash->dbuf_kva;
2736 attr->status = be32_to_cpu(f->status);
2737 attr->npart = be32_to_cpu(f->npart);
2738 for (i = 0; i < attr->npart; i++) {
2739 attr->part[i].part_type =
2740 be32_to_cpu(f->part[i].part_type);
2741 attr->part[i].part_instance =
2742 be32_to_cpu(f->part[i].part_instance);
2743 attr->part[i].part_off =
2744 be32_to_cpu(f->part[i].part_off);
2745 attr->part[i].part_size =
2746 be32_to_cpu(f->part[i].part_size);
2747 attr->part[i].part_len =
2748 be32_to_cpu(f->part[i].part_len);
2749 attr->part[i].part_status =
2750 be32_to_cpu(f->part[i].part_status);
2753 flash->status = status;
2754 bfa_flash_cb(flash);
2755 break;
2756 case BFI_FLASH_I2H_WRITE_RSP:
2757 status = be32_to_cpu(m.write->status);
2758 if (status != BFA_STATUS_OK || flash->residue == 0) {
2759 flash->status = status;
2760 bfa_flash_cb(flash);
2761 } else
2762 bfa_flash_write_send(flash);
2763 break;
2764 case BFI_FLASH_I2H_READ_RSP:
2765 status = be32_to_cpu(m.read->status);
2766 if (status != BFA_STATUS_OK) {
2767 flash->status = status;
2768 bfa_flash_cb(flash);
2769 } else {
2770 u32 len = be32_to_cpu(m.read->length);
2771 memcpy(flash->ubuf + flash->offset,
2772 flash->dbuf_kva, len);
2773 flash->residue -= len;
2774 flash->offset += len;
2775 if (flash->residue == 0) {
2776 flash->status = status;
2777 bfa_flash_cb(flash);
2778 } else
2779 bfa_flash_read_send(flash);
2781 break;
2782 case BFI_FLASH_I2H_BOOT_VER_RSP:
2783 case BFI_FLASH_I2H_EVENT:
2784 break;
2785 default:
2786 WARN_ON(1);
2791 * Flash memory info API.
2794 bfa_nw_flash_meminfo(void)
2796 return roundup(BFA_FLASH_DMA_BUF_SZ, BFA_DMA_ALIGN_SZ);
2800 * Flash attach API.
2802 * @param[in] flash - flash structure
2803 * @param[in] ioc - ioc structure
2804 * @param[in] dev - device structure
2806 void
2807 bfa_nw_flash_attach(struct bfa_flash *flash, struct bfa_ioc *ioc, void *dev)
2809 flash->ioc = ioc;
2810 flash->cbfn = NULL;
2811 flash->cbarg = NULL;
2812 flash->op_busy = 0;
2814 bfa_nw_ioc_mbox_regisr(flash->ioc, BFI_MC_FLASH, bfa_flash_intr, flash);
2815 bfa_q_qe_init(&flash->ioc_notify);
2816 bfa_ioc_notify_init(&flash->ioc_notify, bfa_flash_notify, flash);
2817 list_add_tail(&flash->ioc_notify.qe, &flash->ioc->notify_q);
2821 * Claim memory for flash
2823 * @param[in] flash - flash structure
2824 * @param[in] dm_kva - pointer to virtual memory address
2825 * @param[in] dm_pa - physical memory address
2827 void
2828 bfa_nw_flash_memclaim(struct bfa_flash *flash, u8 *dm_kva, u64 dm_pa)
2830 flash->dbuf_kva = dm_kva;
2831 flash->dbuf_pa = dm_pa;
2832 memset(flash->dbuf_kva, 0, BFA_FLASH_DMA_BUF_SZ);
2833 dm_kva += roundup(BFA_FLASH_DMA_BUF_SZ, BFA_DMA_ALIGN_SZ);
2834 dm_pa += roundup(BFA_FLASH_DMA_BUF_SZ, BFA_DMA_ALIGN_SZ);
2838 * Get flash attribute.
2840 * @param[in] flash - flash structure
2841 * @param[in] attr - flash attribute structure
2842 * @param[in] cbfn - callback function
2843 * @param[in] cbarg - callback argument
2845 * Return status.
2847 enum bfa_status
2848 bfa_nw_flash_get_attr(struct bfa_flash *flash, struct bfa_flash_attr *attr,
2849 bfa_cb_flash cbfn, void *cbarg)
2851 struct bfi_flash_query_req *msg =
2852 (struct bfi_flash_query_req *) flash->mb.msg;
2854 if (!bfa_nw_ioc_is_operational(flash->ioc))
2855 return BFA_STATUS_IOC_NON_OP;
2857 if (flash->op_busy)
2858 return BFA_STATUS_DEVBUSY;
2860 flash->op_busy = 1;
2861 flash->cbfn = cbfn;
2862 flash->cbarg = cbarg;
2863 flash->ubuf = (u8 *) attr;
2865 bfi_h2i_set(msg->mh, BFI_MC_FLASH, BFI_FLASH_H2I_QUERY_REQ,
2866 bfa_ioc_portid(flash->ioc));
2867 bfa_alen_set(&msg->alen, sizeof(struct bfa_flash_attr), flash->dbuf_pa);
2868 bfa_nw_ioc_mbox_queue(flash->ioc, &flash->mb, NULL, NULL);
2870 return BFA_STATUS_OK;
2874 * Update flash partition.
2876 * @param[in] flash - flash structure
2877 * @param[in] type - flash partition type
2878 * @param[in] instance - flash partition instance
2879 * @param[in] buf - update data buffer
2880 * @param[in] len - data buffer length
2881 * @param[in] offset - offset relative to the partition starting address
2882 * @param[in] cbfn - callback function
2883 * @param[in] cbarg - callback argument
2885 * Return status.
2887 enum bfa_status
2888 bfa_nw_flash_update_part(struct bfa_flash *flash, u32 type, u8 instance,
2889 void *buf, u32 len, u32 offset,
2890 bfa_cb_flash cbfn, void *cbarg)
2892 if (!bfa_nw_ioc_is_operational(flash->ioc))
2893 return BFA_STATUS_IOC_NON_OP;
2896 * 'len' must be in word (4-byte) boundary
2898 if (!len || (len & 0x03))
2899 return BFA_STATUS_FLASH_BAD_LEN;
2901 if (type == BFA_FLASH_PART_MFG)
2902 return BFA_STATUS_EINVAL;
2904 if (flash->op_busy)
2905 return BFA_STATUS_DEVBUSY;
2907 flash->op_busy = 1;
2908 flash->cbfn = cbfn;
2909 flash->cbarg = cbarg;
2910 flash->type = type;
2911 flash->instance = instance;
2912 flash->residue = len;
2913 flash->offset = 0;
2914 flash->addr_off = offset;
2915 flash->ubuf = buf;
2917 bfa_flash_write_send(flash);
2919 return BFA_STATUS_OK;
2923 * Read flash partition.
2925 * @param[in] flash - flash structure
2926 * @param[in] type - flash partition type
2927 * @param[in] instance - flash partition instance
2928 * @param[in] buf - read data buffer
2929 * @param[in] len - data buffer length
2930 * @param[in] offset - offset relative to the partition starting address
2931 * @param[in] cbfn - callback function
2932 * @param[in] cbarg - callback argument
2934 * Return status.
2936 enum bfa_status
2937 bfa_nw_flash_read_part(struct bfa_flash *flash, u32 type, u8 instance,
2938 void *buf, u32 len, u32 offset,
2939 bfa_cb_flash cbfn, void *cbarg)
2941 if (!bfa_nw_ioc_is_operational(flash->ioc))
2942 return BFA_STATUS_IOC_NON_OP;
2945 * 'len' must be in word (4-byte) boundary
2947 if (!len || (len & 0x03))
2948 return BFA_STATUS_FLASH_BAD_LEN;
2950 if (flash->op_busy)
2951 return BFA_STATUS_DEVBUSY;
2953 flash->op_busy = 1;
2954 flash->cbfn = cbfn;
2955 flash->cbarg = cbarg;
2956 flash->type = type;
2957 flash->instance = instance;
2958 flash->residue = len;
2959 flash->offset = 0;
2960 flash->addr_off = offset;
2961 flash->ubuf = buf;
2963 bfa_flash_read_send(flash);
2965 return BFA_STATUS_OK;