dabbb30e7610e36af0385890063b0f407f06cf8c
[cascardo/linux.git] / drivers / net / ethernet / brocade / bna / bfa_ioc.c
1 /*
2  * Linux network driver for QLogic BR-series Converged Network Adapter.
3  *
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
7  *
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.
12  */
13 /*
14  * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
15  * Copyright (c) 2014-2015 QLogic Corporation
16  * All rights reserved
17  * www.qlogic.com
18  */
19
20 #include "bfa_ioc.h"
21 #include "bfi_reg.h"
22 #include "bfa_defs.h"
23
24 /* IOC local definitions */
25
26 /* Asic specific macros : see bfa_hw_cb.c and bfa_hw_ct.c for details. */
27
28 #define bfa_ioc_firmware_lock(__ioc)                    \
29                         ((__ioc)->ioc_hwif->ioc_firmware_lock(__ioc))
30 #define bfa_ioc_firmware_unlock(__ioc)                  \
31                         ((__ioc)->ioc_hwif->ioc_firmware_unlock(__ioc))
32 #define bfa_ioc_reg_init(__ioc) ((__ioc)->ioc_hwif->ioc_reg_init(__ioc))
33 #define bfa_ioc_map_port(__ioc) ((__ioc)->ioc_hwif->ioc_map_port(__ioc))
34 #define bfa_ioc_notify_fail(__ioc)                      \
35                         ((__ioc)->ioc_hwif->ioc_notify_fail(__ioc))
36 #define bfa_ioc_sync_start(__ioc)               \
37                         ((__ioc)->ioc_hwif->ioc_sync_start(__ioc))
38 #define bfa_ioc_sync_join(__ioc)                        \
39                         ((__ioc)->ioc_hwif->ioc_sync_join(__ioc))
40 #define bfa_ioc_sync_leave(__ioc)                       \
41                         ((__ioc)->ioc_hwif->ioc_sync_leave(__ioc))
42 #define bfa_ioc_sync_ack(__ioc)                         \
43                         ((__ioc)->ioc_hwif->ioc_sync_ack(__ioc))
44 #define bfa_ioc_sync_complete(__ioc)                    \
45                         ((__ioc)->ioc_hwif->ioc_sync_complete(__ioc))
46 #define bfa_ioc_set_cur_ioc_fwstate(__ioc, __fwstate)           \
47                         ((__ioc)->ioc_hwif->ioc_set_fwstate(__ioc, __fwstate))
48 #define bfa_ioc_get_cur_ioc_fwstate(__ioc)              \
49                         ((__ioc)->ioc_hwif->ioc_get_fwstate(__ioc))
50 #define bfa_ioc_set_alt_ioc_fwstate(__ioc, __fwstate)           \
51                 ((__ioc)->ioc_hwif->ioc_set_alt_fwstate(__ioc, __fwstate))
52
53 static bool bfa_nw_auto_recover = true;
54
55 /*
56  * forward declarations
57  */
58 static void bfa_ioc_hw_sem_init(struct bfa_ioc *ioc);
59 static void bfa_ioc_hw_sem_get(struct bfa_ioc *ioc);
60 static void bfa_ioc_hw_sem_get_cancel(struct bfa_ioc *ioc);
61 static void bfa_ioc_hwinit(struct bfa_ioc *ioc, bool force);
62 static void bfa_ioc_poll_fwinit(struct bfa_ioc *ioc);
63 static void bfa_ioc_send_enable(struct bfa_ioc *ioc);
64 static void bfa_ioc_send_disable(struct bfa_ioc *ioc);
65 static void bfa_ioc_send_getattr(struct bfa_ioc *ioc);
66 static void bfa_ioc_hb_monitor(struct bfa_ioc *ioc);
67 static void bfa_ioc_hb_stop(struct bfa_ioc *ioc);
68 static void bfa_ioc_reset(struct bfa_ioc *ioc, bool force);
69 static void bfa_ioc_mbox_poll(struct bfa_ioc *ioc);
70 static void bfa_ioc_mbox_flush(struct bfa_ioc *ioc);
71 static void bfa_ioc_recover(struct bfa_ioc *ioc);
72 static void bfa_ioc_event_notify(struct bfa_ioc *, enum bfa_ioc_event);
73 static void bfa_ioc_disable_comp(struct bfa_ioc *ioc);
74 static void bfa_ioc_lpu_stop(struct bfa_ioc *ioc);
75 static void bfa_nw_ioc_debug_save_ftrc(struct bfa_ioc *ioc);
76 static void bfa_ioc_fail_notify(struct bfa_ioc *ioc);
77 static void bfa_ioc_pf_enabled(struct bfa_ioc *ioc);
78 static void bfa_ioc_pf_disabled(struct bfa_ioc *ioc);
79 static void bfa_ioc_pf_failed(struct bfa_ioc *ioc);
80 static void bfa_ioc_pf_hwfailed(struct bfa_ioc *ioc);
81 static void bfa_ioc_pf_fwmismatch(struct bfa_ioc *ioc);
82 static enum bfa_status bfa_ioc_boot(struct bfa_ioc *ioc,
83                         enum bfi_fwboot_type boot_type, u32 boot_param);
84 static u32 bfa_ioc_smem_pgnum(struct bfa_ioc *ioc, u32 fmaddr);
85 static void bfa_ioc_get_adapter_serial_num(struct bfa_ioc *ioc,
86                                                 char *serial_num);
87 static void bfa_ioc_get_adapter_fw_ver(struct bfa_ioc *ioc,
88                                                 char *fw_ver);
89 static void bfa_ioc_get_pci_chip_rev(struct bfa_ioc *ioc,
90                                                 char *chip_rev);
91 static void bfa_ioc_get_adapter_optrom_ver(struct bfa_ioc *ioc,
92                                                 char *optrom_ver);
93 static void bfa_ioc_get_adapter_manufacturer(struct bfa_ioc *ioc,
94                                                 char *manufacturer);
95 static void bfa_ioc_get_adapter_model(struct bfa_ioc *ioc, char *model);
96 static u64 bfa_ioc_get_pwwn(struct bfa_ioc *ioc);
97
98 /* IOC state machine definitions/declarations */
99 enum ioc_event {
100         IOC_E_RESET             = 1,    /*!< IOC reset request          */
101         IOC_E_ENABLE            = 2,    /*!< IOC enable request         */
102         IOC_E_DISABLE           = 3,    /*!< IOC disable request        */
103         IOC_E_DETACH            = 4,    /*!< driver detach cleanup      */
104         IOC_E_ENABLED           = 5,    /*!< f/w enabled                */
105         IOC_E_FWRSP_GETATTR     = 6,    /*!< IOC get attribute response */
106         IOC_E_DISABLED          = 7,    /*!< f/w disabled               */
107         IOC_E_PFFAILED          = 8,    /*!< failure notice by iocpf sm */
108         IOC_E_HBFAIL            = 9,    /*!< heartbeat failure          */
109         IOC_E_HWERROR           = 10,   /*!< hardware error interrupt   */
110         IOC_E_TIMEOUT           = 11,   /*!< timeout                    */
111         IOC_E_HWFAILED          = 12,   /*!< PCI mapping failure notice */
112 };
113
114 bfa_fsm_state_decl(bfa_ioc, uninit, struct bfa_ioc, enum ioc_event);
115 bfa_fsm_state_decl(bfa_ioc, reset, struct bfa_ioc, enum ioc_event);
116 bfa_fsm_state_decl(bfa_ioc, enabling, struct bfa_ioc, enum ioc_event);
117 bfa_fsm_state_decl(bfa_ioc, getattr, struct bfa_ioc, enum ioc_event);
118 bfa_fsm_state_decl(bfa_ioc, op, struct bfa_ioc, enum ioc_event);
119 bfa_fsm_state_decl(bfa_ioc, fail_retry, struct bfa_ioc, enum ioc_event);
120 bfa_fsm_state_decl(bfa_ioc, fail, struct bfa_ioc, enum ioc_event);
121 bfa_fsm_state_decl(bfa_ioc, disabling, struct bfa_ioc, enum ioc_event);
122 bfa_fsm_state_decl(bfa_ioc, disabled, struct bfa_ioc, enum ioc_event);
123 bfa_fsm_state_decl(bfa_ioc, hwfail, struct bfa_ioc, enum ioc_event);
124
125 static struct bfa_sm_table ioc_sm_table[] = {
126         {BFA_SM(bfa_ioc_sm_uninit), BFA_IOC_UNINIT},
127         {BFA_SM(bfa_ioc_sm_reset), BFA_IOC_RESET},
128         {BFA_SM(bfa_ioc_sm_enabling), BFA_IOC_ENABLING},
129         {BFA_SM(bfa_ioc_sm_getattr), BFA_IOC_GETATTR},
130         {BFA_SM(bfa_ioc_sm_op), BFA_IOC_OPERATIONAL},
131         {BFA_SM(bfa_ioc_sm_fail_retry), BFA_IOC_INITFAIL},
132         {BFA_SM(bfa_ioc_sm_fail), BFA_IOC_FAIL},
133         {BFA_SM(bfa_ioc_sm_disabling), BFA_IOC_DISABLING},
134         {BFA_SM(bfa_ioc_sm_disabled), BFA_IOC_DISABLED},
135         {BFA_SM(bfa_ioc_sm_hwfail), BFA_IOC_HWFAIL},
136 };
137
138 /*
139  * Forward declareations for iocpf state machine
140  */
141 static void bfa_iocpf_enable(struct bfa_ioc *ioc);
142 static void bfa_iocpf_disable(struct bfa_ioc *ioc);
143 static void bfa_iocpf_fail(struct bfa_ioc *ioc);
144 static void bfa_iocpf_initfail(struct bfa_ioc *ioc);
145 static void bfa_iocpf_getattrfail(struct bfa_ioc *ioc);
146 static void bfa_iocpf_stop(struct bfa_ioc *ioc);
147
148 /* IOCPF state machine events */
149 enum iocpf_event {
150         IOCPF_E_ENABLE          = 1,    /*!< IOCPF enable request       */
151         IOCPF_E_DISABLE         = 2,    /*!< IOCPF disable request      */
152         IOCPF_E_STOP            = 3,    /*!< stop on driver detach      */
153         IOCPF_E_FWREADY         = 4,    /*!< f/w initialization done    */
154         IOCPF_E_FWRSP_ENABLE    = 5,    /*!< enable f/w response        */
155         IOCPF_E_FWRSP_DISABLE   = 6,    /*!< disable f/w response       */
156         IOCPF_E_FAIL            = 7,    /*!< failure notice by ioc sm   */
157         IOCPF_E_INITFAIL        = 8,    /*!< init fail notice by ioc sm */
158         IOCPF_E_GETATTRFAIL     = 9,    /*!< init fail notice by ioc sm */
159         IOCPF_E_SEMLOCKED       = 10,   /*!< h/w semaphore is locked    */
160         IOCPF_E_TIMEOUT         = 11,   /*!< f/w response timeout       */
161         IOCPF_E_SEM_ERROR       = 12,   /*!< h/w sem mapping error      */
162 };
163
164 /* IOCPF states */
165 enum bfa_iocpf_state {
166         BFA_IOCPF_RESET         = 1,    /*!< IOC is in reset state */
167         BFA_IOCPF_SEMWAIT       = 2,    /*!< Waiting for IOC h/w semaphore */
168         BFA_IOCPF_HWINIT        = 3,    /*!< IOC h/w is being initialized */
169         BFA_IOCPF_READY         = 4,    /*!< IOCPF is initialized */
170         BFA_IOCPF_INITFAIL      = 5,    /*!< IOCPF failed */
171         BFA_IOCPF_FAIL          = 6,    /*!< IOCPF failed */
172         BFA_IOCPF_DISABLING     = 7,    /*!< IOCPF is being disabled */
173         BFA_IOCPF_DISABLED      = 8,    /*!< IOCPF is disabled */
174         BFA_IOCPF_FWMISMATCH    = 9,    /*!< IOC f/w different from drivers */
175 };
176
177 bfa_fsm_state_decl(bfa_iocpf, reset, struct bfa_iocpf, enum iocpf_event);
178 bfa_fsm_state_decl(bfa_iocpf, fwcheck, struct bfa_iocpf, enum iocpf_event);
179 bfa_fsm_state_decl(bfa_iocpf, mismatch, struct bfa_iocpf, enum iocpf_event);
180 bfa_fsm_state_decl(bfa_iocpf, semwait, struct bfa_iocpf, enum iocpf_event);
181 bfa_fsm_state_decl(bfa_iocpf, hwinit, struct bfa_iocpf, enum iocpf_event);
182 bfa_fsm_state_decl(bfa_iocpf, enabling, struct bfa_iocpf, enum iocpf_event);
183 bfa_fsm_state_decl(bfa_iocpf, ready, struct bfa_iocpf, enum iocpf_event);
184 bfa_fsm_state_decl(bfa_iocpf, initfail_sync, struct bfa_iocpf,
185                                                 enum iocpf_event);
186 bfa_fsm_state_decl(bfa_iocpf, initfail, struct bfa_iocpf, enum iocpf_event);
187 bfa_fsm_state_decl(bfa_iocpf, fail_sync, struct bfa_iocpf, enum iocpf_event);
188 bfa_fsm_state_decl(bfa_iocpf, fail, struct bfa_iocpf, enum iocpf_event);
189 bfa_fsm_state_decl(bfa_iocpf, disabling, struct bfa_iocpf, enum iocpf_event);
190 bfa_fsm_state_decl(bfa_iocpf, disabling_sync, struct bfa_iocpf,
191                                                 enum iocpf_event);
192 bfa_fsm_state_decl(bfa_iocpf, disabled, struct bfa_iocpf, enum iocpf_event);
193
194 static struct bfa_sm_table iocpf_sm_table[] = {
195         {BFA_SM(bfa_iocpf_sm_reset), BFA_IOCPF_RESET},
196         {BFA_SM(bfa_iocpf_sm_fwcheck), BFA_IOCPF_FWMISMATCH},
197         {BFA_SM(bfa_iocpf_sm_mismatch), BFA_IOCPF_FWMISMATCH},
198         {BFA_SM(bfa_iocpf_sm_semwait), BFA_IOCPF_SEMWAIT},
199         {BFA_SM(bfa_iocpf_sm_hwinit), BFA_IOCPF_HWINIT},
200         {BFA_SM(bfa_iocpf_sm_enabling), BFA_IOCPF_HWINIT},
201         {BFA_SM(bfa_iocpf_sm_ready), BFA_IOCPF_READY},
202         {BFA_SM(bfa_iocpf_sm_initfail_sync), BFA_IOCPF_INITFAIL},
203         {BFA_SM(bfa_iocpf_sm_initfail), BFA_IOCPF_INITFAIL},
204         {BFA_SM(bfa_iocpf_sm_fail_sync), BFA_IOCPF_FAIL},
205         {BFA_SM(bfa_iocpf_sm_fail), BFA_IOCPF_FAIL},
206         {BFA_SM(bfa_iocpf_sm_disabling), BFA_IOCPF_DISABLING},
207         {BFA_SM(bfa_iocpf_sm_disabling_sync), BFA_IOCPF_DISABLING},
208         {BFA_SM(bfa_iocpf_sm_disabled), BFA_IOCPF_DISABLED},
209 };
210
211 /* IOC State Machine */
212
213 /* Beginning state. IOC uninit state. */
214 static void
215 bfa_ioc_sm_uninit_entry(struct bfa_ioc *ioc)
216 {
217 }
218
219 /* IOC is in uninit state. */
220 static void
221 bfa_ioc_sm_uninit(struct bfa_ioc *ioc, enum ioc_event event)
222 {
223         switch (event) {
224         case IOC_E_RESET:
225                 bfa_fsm_set_state(ioc, bfa_ioc_sm_reset);
226                 break;
227
228         default:
229                 bfa_sm_fault(event);
230         }
231 }
232
233 /* Reset entry actions -- initialize state machine */
234 static void
235 bfa_ioc_sm_reset_entry(struct bfa_ioc *ioc)
236 {
237         bfa_fsm_set_state(&ioc->iocpf, bfa_iocpf_sm_reset);
238 }
239
240 /* IOC is in reset state. */
241 static void
242 bfa_ioc_sm_reset(struct bfa_ioc *ioc, enum ioc_event event)
243 {
244         switch (event) {
245         case IOC_E_ENABLE:
246                 bfa_fsm_set_state(ioc, bfa_ioc_sm_enabling);
247                 break;
248
249         case IOC_E_DISABLE:
250                 bfa_ioc_disable_comp(ioc);
251                 break;
252
253         case IOC_E_DETACH:
254                 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
255                 break;
256
257         default:
258                 bfa_sm_fault(event);
259         }
260 }
261
262 static void
263 bfa_ioc_sm_enabling_entry(struct bfa_ioc *ioc)
264 {
265         bfa_iocpf_enable(ioc);
266 }
267
268 /* Host IOC function is being enabled, awaiting response from firmware.
269  * Semaphore is acquired.
270  */
271 static void
272 bfa_ioc_sm_enabling(struct bfa_ioc *ioc, enum ioc_event event)
273 {
274         switch (event) {
275         case IOC_E_ENABLED:
276                 bfa_fsm_set_state(ioc, bfa_ioc_sm_getattr);
277                 break;
278
279         case IOC_E_PFFAILED:
280                 /* !!! fall through !!! */
281         case IOC_E_HWERROR:
282                 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
283                 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
284                 if (event != IOC_E_PFFAILED)
285                         bfa_iocpf_initfail(ioc);
286                 break;
287
288         case IOC_E_HWFAILED:
289                 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
290                 bfa_fsm_set_state(ioc, bfa_ioc_sm_hwfail);
291                 break;
292
293         case IOC_E_DISABLE:
294                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
295                 break;
296
297         case IOC_E_DETACH:
298                 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
299                 bfa_iocpf_stop(ioc);
300                 break;
301
302         case IOC_E_ENABLE:
303                 break;
304
305         default:
306                 bfa_sm_fault(event);
307         }
308 }
309
310 /* Semaphore should be acquired for version check. */
311 static void
312 bfa_ioc_sm_getattr_entry(struct bfa_ioc *ioc)
313 {
314         mod_timer(&ioc->ioc_timer, jiffies +
315                 msecs_to_jiffies(BFA_IOC_TOV));
316         bfa_ioc_send_getattr(ioc);
317 }
318
319 /* IOC configuration in progress. Timer is active. */
320 static void
321 bfa_ioc_sm_getattr(struct bfa_ioc *ioc, enum ioc_event event)
322 {
323         switch (event) {
324         case IOC_E_FWRSP_GETATTR:
325                 del_timer(&ioc->ioc_timer);
326                 bfa_fsm_set_state(ioc, bfa_ioc_sm_op);
327                 break;
328
329         case IOC_E_PFFAILED:
330         case IOC_E_HWERROR:
331                 del_timer(&ioc->ioc_timer);
332                 /* fall through */
333         case IOC_E_TIMEOUT:
334                 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
335                 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
336                 if (event != IOC_E_PFFAILED)
337                         bfa_iocpf_getattrfail(ioc);
338                 break;
339
340         case IOC_E_DISABLE:
341                 del_timer(&ioc->ioc_timer);
342                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
343                 break;
344
345         case IOC_E_ENABLE:
346                 break;
347
348         default:
349                 bfa_sm_fault(event);
350         }
351 }
352
353 static void
354 bfa_ioc_sm_op_entry(struct bfa_ioc *ioc)
355 {
356         ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_OK);
357         bfa_ioc_event_notify(ioc, BFA_IOC_E_ENABLED);
358         bfa_ioc_hb_monitor(ioc);
359 }
360
361 static void
362 bfa_ioc_sm_op(struct bfa_ioc *ioc, enum ioc_event event)
363 {
364         switch (event) {
365         case IOC_E_ENABLE:
366                 break;
367
368         case IOC_E_DISABLE:
369                 bfa_ioc_hb_stop(ioc);
370                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
371                 break;
372
373         case IOC_E_PFFAILED:
374         case IOC_E_HWERROR:
375                 bfa_ioc_hb_stop(ioc);
376                 /* !!! fall through !!! */
377         case IOC_E_HBFAIL:
378                 if (ioc->iocpf.auto_recover)
379                         bfa_fsm_set_state(ioc, bfa_ioc_sm_fail_retry);
380                 else
381                         bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
382
383                 bfa_ioc_fail_notify(ioc);
384
385                 if (event != IOC_E_PFFAILED)
386                         bfa_iocpf_fail(ioc);
387                 break;
388
389         default:
390                 bfa_sm_fault(event);
391         }
392 }
393
394 static void
395 bfa_ioc_sm_disabling_entry(struct bfa_ioc *ioc)
396 {
397         bfa_iocpf_disable(ioc);
398 }
399
400 /* IOC is being disabled */
401 static void
402 bfa_ioc_sm_disabling(struct bfa_ioc *ioc, enum ioc_event event)
403 {
404         switch (event) {
405         case IOC_E_DISABLED:
406                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
407                 break;
408
409         case IOC_E_HWERROR:
410                 /*
411                  * No state change.  Will move to disabled state
412                  * after iocpf sm completes failure processing and
413                  * moves to disabled state.
414                  */
415                 bfa_iocpf_fail(ioc);
416                 break;
417
418         case IOC_E_HWFAILED:
419                 bfa_fsm_set_state(ioc, bfa_ioc_sm_hwfail);
420                 bfa_ioc_disable_comp(ioc);
421                 break;
422
423         default:
424                 bfa_sm_fault(event);
425         }
426 }
427
428 /* IOC disable completion entry. */
429 static void
430 bfa_ioc_sm_disabled_entry(struct bfa_ioc *ioc)
431 {
432         bfa_ioc_disable_comp(ioc);
433 }
434
435 static void
436 bfa_ioc_sm_disabled(struct bfa_ioc *ioc, enum ioc_event event)
437 {
438         switch (event) {
439         case IOC_E_ENABLE:
440                 bfa_fsm_set_state(ioc, bfa_ioc_sm_enabling);
441                 break;
442
443         case IOC_E_DISABLE:
444                 ioc->cbfn->disable_cbfn(ioc->bfa);
445                 break;
446
447         case IOC_E_DETACH:
448                 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
449                 bfa_iocpf_stop(ioc);
450                 break;
451
452         default:
453                 bfa_sm_fault(event);
454         }
455 }
456
457 static void
458 bfa_ioc_sm_fail_retry_entry(struct bfa_ioc *ioc)
459 {
460 }
461
462 /* Hardware initialization retry. */
463 static void
464 bfa_ioc_sm_fail_retry(struct bfa_ioc *ioc, enum ioc_event event)
465 {
466         switch (event) {
467         case IOC_E_ENABLED:
468                 bfa_fsm_set_state(ioc, bfa_ioc_sm_getattr);
469                 break;
470
471         case IOC_E_PFFAILED:
472         case IOC_E_HWERROR:
473                 /**
474                  * Initialization retry failed.
475                  */
476                 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
477                 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
478                 if (event != IOC_E_PFFAILED)
479                         bfa_iocpf_initfail(ioc);
480                 break;
481
482         case IOC_E_HWFAILED:
483                 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
484                 bfa_fsm_set_state(ioc, bfa_ioc_sm_hwfail);
485                 break;
486
487         case IOC_E_ENABLE:
488                 break;
489
490         case IOC_E_DISABLE:
491                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
492                 break;
493
494         case IOC_E_DETACH:
495                 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
496                 bfa_iocpf_stop(ioc);
497                 break;
498
499         default:
500                 bfa_sm_fault(event);
501         }
502 }
503
504 static void
505 bfa_ioc_sm_fail_entry(struct bfa_ioc *ioc)
506 {
507 }
508
509 /* IOC failure. */
510 static void
511 bfa_ioc_sm_fail(struct bfa_ioc *ioc, enum ioc_event event)
512 {
513         switch (event) {
514         case IOC_E_ENABLE:
515                 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
516                 break;
517
518         case IOC_E_DISABLE:
519                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
520                 break;
521
522         case IOC_E_DETACH:
523                 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
524                 bfa_iocpf_stop(ioc);
525                 break;
526
527         case IOC_E_HWERROR:
528                 /* HB failure notification, ignore. */
529                 break;
530
531         default:
532                 bfa_sm_fault(event);
533         }
534 }
535
536 static void
537 bfa_ioc_sm_hwfail_entry(struct bfa_ioc *ioc)
538 {
539 }
540
541 /* IOC failure. */
542 static void
543 bfa_ioc_sm_hwfail(struct bfa_ioc *ioc, enum ioc_event event)
544 {
545         switch (event) {
546
547         case IOC_E_ENABLE:
548                 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
549                 break;
550
551         case IOC_E_DISABLE:
552                 ioc->cbfn->disable_cbfn(ioc->bfa);
553                 break;
554
555         case IOC_E_DETACH:
556                 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
557                 break;
558
559         default:
560                 bfa_sm_fault(event);
561         }
562 }
563
564 /* IOCPF State Machine */
565
566 /* Reset entry actions -- initialize state machine */
567 static void
568 bfa_iocpf_sm_reset_entry(struct bfa_iocpf *iocpf)
569 {
570         iocpf->fw_mismatch_notified = false;
571         iocpf->auto_recover = bfa_nw_auto_recover;
572 }
573
574 /* Beginning state. IOC is in reset state. */
575 static void
576 bfa_iocpf_sm_reset(struct bfa_iocpf *iocpf, enum iocpf_event event)
577 {
578         switch (event) {
579         case IOCPF_E_ENABLE:
580                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fwcheck);
581                 break;
582
583         case IOCPF_E_STOP:
584                 break;
585
586         default:
587                 bfa_sm_fault(event);
588         }
589 }
590
591 /* Semaphore should be acquired for version check. */
592 static void
593 bfa_iocpf_sm_fwcheck_entry(struct bfa_iocpf *iocpf)
594 {
595         bfa_ioc_hw_sem_init(iocpf->ioc);
596         bfa_ioc_hw_sem_get(iocpf->ioc);
597 }
598
599 /* Awaiting h/w semaphore to continue with version check. */
600 static void
601 bfa_iocpf_sm_fwcheck(struct bfa_iocpf *iocpf, enum iocpf_event event)
602 {
603         struct bfa_ioc *ioc = iocpf->ioc;
604
605         switch (event) {
606         case IOCPF_E_SEMLOCKED:
607                 if (bfa_ioc_firmware_lock(ioc)) {
608                         if (bfa_ioc_sync_start(ioc)) {
609                                 bfa_ioc_sync_join(ioc);
610                                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_hwinit);
611                         } else {
612                                 bfa_ioc_firmware_unlock(ioc);
613                                 bfa_nw_ioc_hw_sem_release(ioc);
614                                 mod_timer(&ioc->sem_timer, jiffies +
615                                         msecs_to_jiffies(BFA_IOC_HWSEM_TOV));
616                         }
617                 } else {
618                         bfa_nw_ioc_hw_sem_release(ioc);
619                         bfa_fsm_set_state(iocpf, bfa_iocpf_sm_mismatch);
620                 }
621                 break;
622
623         case IOCPF_E_SEM_ERROR:
624                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
625                 bfa_ioc_pf_hwfailed(ioc);
626                 break;
627
628         case IOCPF_E_DISABLE:
629                 bfa_ioc_hw_sem_get_cancel(ioc);
630                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
631                 bfa_ioc_pf_disabled(ioc);
632                 break;
633
634         case IOCPF_E_STOP:
635                 bfa_ioc_hw_sem_get_cancel(ioc);
636                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
637                 break;
638
639         default:
640                 bfa_sm_fault(event);
641         }
642 }
643
644 /* Notify enable completion callback */
645 static void
646 bfa_iocpf_sm_mismatch_entry(struct bfa_iocpf *iocpf)
647 {
648         /* Call only the first time sm enters fwmismatch state. */
649         if (!iocpf->fw_mismatch_notified)
650                 bfa_ioc_pf_fwmismatch(iocpf->ioc);
651
652         iocpf->fw_mismatch_notified = true;
653         mod_timer(&(iocpf->ioc)->iocpf_timer, jiffies +
654                 msecs_to_jiffies(BFA_IOC_TOV));
655 }
656
657 /* Awaiting firmware version match. */
658 static void
659 bfa_iocpf_sm_mismatch(struct bfa_iocpf *iocpf, enum iocpf_event event)
660 {
661         struct bfa_ioc *ioc = iocpf->ioc;
662
663         switch (event) {
664         case IOCPF_E_TIMEOUT:
665                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fwcheck);
666                 break;
667
668         case IOCPF_E_DISABLE:
669                 del_timer(&ioc->iocpf_timer);
670                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
671                 bfa_ioc_pf_disabled(ioc);
672                 break;
673
674         case IOCPF_E_STOP:
675                 del_timer(&ioc->iocpf_timer);
676                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
677                 break;
678
679         default:
680                 bfa_sm_fault(event);
681         }
682 }
683
684 /* Request for semaphore. */
685 static void
686 bfa_iocpf_sm_semwait_entry(struct bfa_iocpf *iocpf)
687 {
688         bfa_ioc_hw_sem_get(iocpf->ioc);
689 }
690
691 /* Awaiting semaphore for h/w initialzation. */
692 static void
693 bfa_iocpf_sm_semwait(struct bfa_iocpf *iocpf, enum iocpf_event event)
694 {
695         struct bfa_ioc *ioc = iocpf->ioc;
696
697         switch (event) {
698         case IOCPF_E_SEMLOCKED:
699                 if (bfa_ioc_sync_complete(ioc)) {
700                         bfa_ioc_sync_join(ioc);
701                         bfa_fsm_set_state(iocpf, bfa_iocpf_sm_hwinit);
702                 } else {
703                         bfa_nw_ioc_hw_sem_release(ioc);
704                         mod_timer(&ioc->sem_timer, jiffies +
705                                 msecs_to_jiffies(BFA_IOC_HWSEM_TOV));
706                 }
707                 break;
708
709         case IOCPF_E_SEM_ERROR:
710                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
711                 bfa_ioc_pf_hwfailed(ioc);
712                 break;
713
714         case IOCPF_E_DISABLE:
715                 bfa_ioc_hw_sem_get_cancel(ioc);
716                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
717                 break;
718
719         default:
720                 bfa_sm_fault(event);
721         }
722 }
723
724 static void
725 bfa_iocpf_sm_hwinit_entry(struct bfa_iocpf *iocpf)
726 {
727         iocpf->poll_time = 0;
728         bfa_ioc_reset(iocpf->ioc, false);
729 }
730
731 /* Hardware is being initialized. Interrupts are enabled.
732  * Holding hardware semaphore lock.
733  */
734 static void
735 bfa_iocpf_sm_hwinit(struct bfa_iocpf *iocpf, enum iocpf_event event)
736 {
737         struct bfa_ioc *ioc = iocpf->ioc;
738
739         switch (event) {
740         case IOCPF_E_FWREADY:
741                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_enabling);
742                 break;
743
744         case IOCPF_E_TIMEOUT:
745                 bfa_nw_ioc_hw_sem_release(ioc);
746                         bfa_ioc_pf_failed(ioc);
747                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_initfail_sync);
748                 break;
749
750         case IOCPF_E_DISABLE:
751                 del_timer(&ioc->iocpf_timer);
752                 bfa_ioc_sync_leave(ioc);
753                 bfa_nw_ioc_hw_sem_release(ioc);
754                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabled);
755                 break;
756
757         default:
758                 bfa_sm_fault(event);
759         }
760 }
761
762 static void
763 bfa_iocpf_sm_enabling_entry(struct bfa_iocpf *iocpf)
764 {
765         mod_timer(&(iocpf->ioc)->iocpf_timer, jiffies +
766                 msecs_to_jiffies(BFA_IOC_TOV));
767         /**
768          * Enable Interrupts before sending fw IOC ENABLE cmd.
769          */
770         iocpf->ioc->cbfn->reset_cbfn(iocpf->ioc->bfa);
771         bfa_ioc_send_enable(iocpf->ioc);
772 }
773
774 /* Host IOC function is being enabled, awaiting response from firmware.
775  * Semaphore is acquired.
776  */
777 static void
778 bfa_iocpf_sm_enabling(struct bfa_iocpf *iocpf, enum iocpf_event event)
779 {
780         struct bfa_ioc *ioc = iocpf->ioc;
781
782         switch (event) {
783         case IOCPF_E_FWRSP_ENABLE:
784                 del_timer(&ioc->iocpf_timer);
785                 bfa_nw_ioc_hw_sem_release(ioc);
786                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_ready);
787                 break;
788
789         case IOCPF_E_INITFAIL:
790                 del_timer(&ioc->iocpf_timer);
791                 /*
792                  * !!! fall through !!!
793                  */
794         case IOCPF_E_TIMEOUT:
795                 bfa_nw_ioc_hw_sem_release(ioc);
796                 if (event == IOCPF_E_TIMEOUT)
797                         bfa_ioc_pf_failed(ioc);
798                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_initfail_sync);
799                 break;
800
801         case IOCPF_E_DISABLE:
802                 del_timer(&ioc->iocpf_timer);
803                 bfa_nw_ioc_hw_sem_release(ioc);
804                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling);
805                 break;
806
807         default:
808                 bfa_sm_fault(event);
809         }
810 }
811
812 static void
813 bfa_iocpf_sm_ready_entry(struct bfa_iocpf *iocpf)
814 {
815         bfa_ioc_pf_enabled(iocpf->ioc);
816 }
817
818 static void
819 bfa_iocpf_sm_ready(struct bfa_iocpf *iocpf, enum iocpf_event event)
820 {
821         switch (event) {
822         case IOCPF_E_DISABLE:
823                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling);
824                 break;
825
826         case IOCPF_E_GETATTRFAIL:
827                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_initfail_sync);
828                 break;
829
830         case IOCPF_E_FAIL:
831                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail_sync);
832                 break;
833
834         default:
835                 bfa_sm_fault(event);
836         }
837 }
838
839 static void
840 bfa_iocpf_sm_disabling_entry(struct bfa_iocpf *iocpf)
841 {
842         mod_timer(&(iocpf->ioc)->iocpf_timer, jiffies +
843                 msecs_to_jiffies(BFA_IOC_TOV));
844         bfa_ioc_send_disable(iocpf->ioc);
845 }
846
847 /* IOC is being disabled */
848 static void
849 bfa_iocpf_sm_disabling(struct bfa_iocpf *iocpf, enum iocpf_event event)
850 {
851         struct bfa_ioc *ioc = iocpf->ioc;
852
853         switch (event) {
854         case IOCPF_E_FWRSP_DISABLE:
855                 del_timer(&ioc->iocpf_timer);
856                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
857                 break;
858
859         case IOCPF_E_FAIL:
860                 del_timer(&ioc->iocpf_timer);
861                 /*
862                  * !!! fall through !!!
863                  */
864
865         case IOCPF_E_TIMEOUT:
866                 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_FAIL);
867                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
868                 break;
869
870         case IOCPF_E_FWRSP_ENABLE:
871                 break;
872
873         default:
874                 bfa_sm_fault(event);
875         }
876 }
877
878 static void
879 bfa_iocpf_sm_disabling_sync_entry(struct bfa_iocpf *iocpf)
880 {
881         bfa_ioc_hw_sem_get(iocpf->ioc);
882 }
883
884 /* IOC hb ack request is being removed. */
885 static void
886 bfa_iocpf_sm_disabling_sync(struct bfa_iocpf *iocpf, enum iocpf_event event)
887 {
888         struct bfa_ioc *ioc = iocpf->ioc;
889
890         switch (event) {
891         case IOCPF_E_SEMLOCKED:
892                 bfa_ioc_sync_leave(ioc);
893                 bfa_nw_ioc_hw_sem_release(ioc);
894                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabled);
895                 break;
896
897         case IOCPF_E_SEM_ERROR:
898                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
899                 bfa_ioc_pf_hwfailed(ioc);
900                 break;
901
902         case IOCPF_E_FAIL:
903                 break;
904
905         default:
906                 bfa_sm_fault(event);
907         }
908 }
909
910 /* IOC disable completion entry. */
911 static void
912 bfa_iocpf_sm_disabled_entry(struct bfa_iocpf *iocpf)
913 {
914         bfa_ioc_mbox_flush(iocpf->ioc);
915         bfa_ioc_pf_disabled(iocpf->ioc);
916 }
917
918 static void
919 bfa_iocpf_sm_disabled(struct bfa_iocpf *iocpf, enum iocpf_event event)
920 {
921         struct bfa_ioc *ioc = iocpf->ioc;
922
923         switch (event) {
924         case IOCPF_E_ENABLE:
925                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_semwait);
926                 break;
927
928         case IOCPF_E_STOP:
929                 bfa_ioc_firmware_unlock(ioc);
930                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
931                 break;
932
933         default:
934                 bfa_sm_fault(event);
935         }
936 }
937
938 static void
939 bfa_iocpf_sm_initfail_sync_entry(struct bfa_iocpf *iocpf)
940 {
941         bfa_nw_ioc_debug_save_ftrc(iocpf->ioc);
942         bfa_ioc_hw_sem_get(iocpf->ioc);
943 }
944
945 /* Hardware initialization failed. */
946 static void
947 bfa_iocpf_sm_initfail_sync(struct bfa_iocpf *iocpf, enum iocpf_event event)
948 {
949         struct bfa_ioc *ioc = iocpf->ioc;
950
951         switch (event) {
952         case IOCPF_E_SEMLOCKED:
953                 bfa_ioc_notify_fail(ioc);
954                 bfa_ioc_sync_leave(ioc);
955                 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_FAIL);
956                 bfa_nw_ioc_hw_sem_release(ioc);
957                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_initfail);
958                 break;
959
960         case IOCPF_E_SEM_ERROR:
961                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
962                 bfa_ioc_pf_hwfailed(ioc);
963                 break;
964
965         case IOCPF_E_DISABLE:
966                 bfa_ioc_hw_sem_get_cancel(ioc);
967                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
968                 break;
969
970         case IOCPF_E_STOP:
971                 bfa_ioc_hw_sem_get_cancel(ioc);
972                 bfa_ioc_firmware_unlock(ioc);
973                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
974                 break;
975
976         case IOCPF_E_FAIL:
977                 break;
978
979         default:
980                 bfa_sm_fault(event);
981         }
982 }
983
984 static void
985 bfa_iocpf_sm_initfail_entry(struct bfa_iocpf *iocpf)
986 {
987 }
988
989 /* Hardware initialization failed. */
990 static void
991 bfa_iocpf_sm_initfail(struct bfa_iocpf *iocpf, enum iocpf_event event)
992 {
993         struct bfa_ioc *ioc = iocpf->ioc;
994
995         switch (event) {
996         case IOCPF_E_DISABLE:
997                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabled);
998                 break;
999
1000         case IOCPF_E_STOP:
1001                 bfa_ioc_firmware_unlock(ioc);
1002                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
1003                 break;
1004
1005         default:
1006                 bfa_sm_fault(event);
1007         }
1008 }
1009
1010 static void
1011 bfa_iocpf_sm_fail_sync_entry(struct bfa_iocpf *iocpf)
1012 {
1013         /**
1014          * Mark IOC as failed in hardware and stop firmware.
1015          */
1016         bfa_ioc_lpu_stop(iocpf->ioc);
1017
1018         /**
1019          * Flush any queued up mailbox requests.
1020          */
1021         bfa_ioc_mbox_flush(iocpf->ioc);
1022         bfa_ioc_hw_sem_get(iocpf->ioc);
1023 }
1024
1025 /* IOC is in failed state. */
1026 static void
1027 bfa_iocpf_sm_fail_sync(struct bfa_iocpf *iocpf, enum iocpf_event event)
1028 {
1029         struct bfa_ioc *ioc = iocpf->ioc;
1030
1031         switch (event) {
1032         case IOCPF_E_SEMLOCKED:
1033                 bfa_ioc_sync_ack(ioc);
1034                 bfa_ioc_notify_fail(ioc);
1035                 if (!iocpf->auto_recover) {
1036                         bfa_ioc_sync_leave(ioc);
1037                         bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_FAIL);
1038                         bfa_nw_ioc_hw_sem_release(ioc);
1039                         bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
1040                 } else {
1041                         if (bfa_ioc_sync_complete(ioc))
1042                                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_hwinit);
1043                         else {
1044                                 bfa_nw_ioc_hw_sem_release(ioc);
1045                                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_semwait);
1046                         }
1047                 }
1048                 break;
1049
1050         case IOCPF_E_SEM_ERROR:
1051                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
1052                 bfa_ioc_pf_hwfailed(ioc);
1053                 break;
1054
1055         case IOCPF_E_DISABLE:
1056                 bfa_ioc_hw_sem_get_cancel(ioc);
1057                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
1058                 break;
1059
1060         case IOCPF_E_FAIL:
1061                 break;
1062
1063         default:
1064                 bfa_sm_fault(event);
1065         }
1066 }
1067
1068 static void
1069 bfa_iocpf_sm_fail_entry(struct bfa_iocpf *iocpf)
1070 {
1071 }
1072
1073 /* IOC is in failed state. */
1074 static void
1075 bfa_iocpf_sm_fail(struct bfa_iocpf *iocpf, enum iocpf_event event)
1076 {
1077         switch (event) {
1078         case IOCPF_E_DISABLE:
1079                 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabled);
1080                 break;
1081
1082         default:
1083                 bfa_sm_fault(event);
1084         }
1085 }
1086
1087 /* BFA IOC private functions */
1088
1089 /* Notify common modules registered for notification. */
1090 static void
1091 bfa_ioc_event_notify(struct bfa_ioc *ioc, enum bfa_ioc_event event)
1092 {
1093         struct bfa_ioc_notify *notify;
1094         struct list_head                        *qe;
1095
1096         list_for_each(qe, &ioc->notify_q) {
1097                 notify = (struct bfa_ioc_notify *)qe;
1098                 notify->cbfn(notify->cbarg, event);
1099         }
1100 }
1101
1102 static void
1103 bfa_ioc_disable_comp(struct bfa_ioc *ioc)
1104 {
1105         ioc->cbfn->disable_cbfn(ioc->bfa);
1106         bfa_ioc_event_notify(ioc, BFA_IOC_E_DISABLED);
1107 }
1108
1109 bool
1110 bfa_nw_ioc_sem_get(void __iomem *sem_reg)
1111 {
1112         u32 r32;
1113         int cnt = 0;
1114 #define BFA_SEM_SPINCNT 3000
1115
1116         r32 = readl(sem_reg);
1117
1118         while ((r32 & 1) && (cnt < BFA_SEM_SPINCNT)) {
1119                 cnt++;
1120                 udelay(2);
1121                 r32 = readl(sem_reg);
1122         }
1123
1124         if (!(r32 & 1))
1125                 return true;
1126
1127         return false;
1128 }
1129
1130 void
1131 bfa_nw_ioc_sem_release(void __iomem *sem_reg)
1132 {
1133         readl(sem_reg);
1134         writel(1, sem_reg);
1135 }
1136
1137 /* Clear fwver hdr */
1138 static void
1139 bfa_ioc_fwver_clear(struct bfa_ioc *ioc)
1140 {
1141         u32 pgnum, pgoff, loff = 0;
1142         int i;
1143
1144         pgnum = PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, loff);
1145         pgoff = PSS_SMEM_PGOFF(loff);
1146         writel(pgnum, ioc->ioc_regs.host_page_num_fn);
1147
1148         for (i = 0; i < (sizeof(struct bfi_ioc_image_hdr) / sizeof(u32)); i++) {
1149                 writel(0, ioc->ioc_regs.smem_page_start + loff);
1150                 loff += sizeof(u32);
1151         }
1152 }
1153
1154
1155 static void
1156 bfa_ioc_hw_sem_init(struct bfa_ioc *ioc)
1157 {
1158         struct bfi_ioc_image_hdr fwhdr;
1159         u32 fwstate, r32;
1160
1161         /* Spin on init semaphore to serialize. */
1162         r32 = readl(ioc->ioc_regs.ioc_init_sem_reg);
1163         while (r32 & 0x1) {
1164                 udelay(20);
1165                 r32 = readl(ioc->ioc_regs.ioc_init_sem_reg);
1166         }
1167
1168         fwstate = bfa_ioc_get_cur_ioc_fwstate(ioc);
1169         if (fwstate == BFI_IOC_UNINIT) {
1170                 writel(1, ioc->ioc_regs.ioc_init_sem_reg);
1171                 return;
1172         }
1173
1174         bfa_nw_ioc_fwver_get(ioc, &fwhdr);
1175
1176         if (swab32(fwhdr.exec) == BFI_FWBOOT_TYPE_NORMAL) {
1177                 writel(1, ioc->ioc_regs.ioc_init_sem_reg);
1178                 return;
1179         }
1180
1181         bfa_ioc_fwver_clear(ioc);
1182         bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_UNINIT);
1183         bfa_ioc_set_alt_ioc_fwstate(ioc, BFI_IOC_UNINIT);
1184
1185         /*
1186          * Try to lock and then unlock the semaphore.
1187          */
1188         readl(ioc->ioc_regs.ioc_sem_reg);
1189         writel(1, ioc->ioc_regs.ioc_sem_reg);
1190
1191         /* Unlock init semaphore */
1192         writel(1, ioc->ioc_regs.ioc_init_sem_reg);
1193 }
1194
1195 static void
1196 bfa_ioc_hw_sem_get(struct bfa_ioc *ioc)
1197 {
1198         u32     r32;
1199
1200         /**
1201          * First read to the semaphore register will return 0, subsequent reads
1202          * will return 1. Semaphore is released by writing 1 to the register
1203          */
1204         r32 = readl(ioc->ioc_regs.ioc_sem_reg);
1205         if (r32 == ~0) {
1206                 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_SEM_ERROR);
1207                 return;
1208         }
1209         if (!(r32 & 1)) {
1210                 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_SEMLOCKED);
1211                 return;
1212         }
1213
1214         mod_timer(&ioc->sem_timer, jiffies +
1215                 msecs_to_jiffies(BFA_IOC_HWSEM_TOV));
1216 }
1217
1218 void
1219 bfa_nw_ioc_hw_sem_release(struct bfa_ioc *ioc)
1220 {
1221         writel(1, ioc->ioc_regs.ioc_sem_reg);
1222 }
1223
1224 static void
1225 bfa_ioc_hw_sem_get_cancel(struct bfa_ioc *ioc)
1226 {
1227         del_timer(&ioc->sem_timer);
1228 }
1229
1230 /* Initialize LPU local memory (aka secondary memory / SRAM) */
1231 static void
1232 bfa_ioc_lmem_init(struct bfa_ioc *ioc)
1233 {
1234         u32     pss_ctl;
1235         int             i;
1236 #define PSS_LMEM_INIT_TIME  10000
1237
1238         pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1239         pss_ctl &= ~__PSS_LMEM_RESET;
1240         pss_ctl |= __PSS_LMEM_INIT_EN;
1241
1242         /*
1243          * i2c workaround 12.5khz clock
1244          */
1245         pss_ctl |= __PSS_I2C_CLK_DIV(3UL);
1246         writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1247
1248         /**
1249          * wait for memory initialization to be complete
1250          */
1251         i = 0;
1252         do {
1253                 pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1254                 i++;
1255         } while (!(pss_ctl & __PSS_LMEM_INIT_DONE) && (i < PSS_LMEM_INIT_TIME));
1256
1257         /**
1258          * If memory initialization is not successful, IOC timeout will catch
1259          * such failures.
1260          */
1261         BUG_ON(!(pss_ctl & __PSS_LMEM_INIT_DONE));
1262
1263         pss_ctl &= ~(__PSS_LMEM_INIT_DONE | __PSS_LMEM_INIT_EN);
1264         writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1265 }
1266
1267 static void
1268 bfa_ioc_lpu_start(struct bfa_ioc *ioc)
1269 {
1270         u32     pss_ctl;
1271
1272         /**
1273          * Take processor out of reset.
1274          */
1275         pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1276         pss_ctl &= ~__PSS_LPU0_RESET;
1277
1278         writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1279 }
1280
1281 static void
1282 bfa_ioc_lpu_stop(struct bfa_ioc *ioc)
1283 {
1284         u32     pss_ctl;
1285
1286         /**
1287          * Put processors in reset.
1288          */
1289         pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1290         pss_ctl |= (__PSS_LPU0_RESET | __PSS_LPU1_RESET);
1291
1292         writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1293 }
1294
1295 /* Get driver and firmware versions. */
1296 void
1297 bfa_nw_ioc_fwver_get(struct bfa_ioc *ioc, struct bfi_ioc_image_hdr *fwhdr)
1298 {
1299         u32     pgnum;
1300         u32     loff = 0;
1301         int             i;
1302         u32     *fwsig = (u32 *) fwhdr;
1303
1304         pgnum = bfa_ioc_smem_pgnum(ioc, loff);
1305         writel(pgnum, ioc->ioc_regs.host_page_num_fn);
1306
1307         for (i = 0; i < (sizeof(struct bfi_ioc_image_hdr) / sizeof(u32));
1308              i++) {
1309                 fwsig[i] =
1310                         swab32(readl((loff) + (ioc->ioc_regs.smem_page_start)));
1311                 loff += sizeof(u32);
1312         }
1313 }
1314
1315 static bool
1316 bfa_ioc_fwver_md5_check(struct bfi_ioc_image_hdr *fwhdr_1,
1317                         struct bfi_ioc_image_hdr *fwhdr_2)
1318 {
1319         int i;
1320
1321         for (i = 0; i < BFI_IOC_MD5SUM_SZ; i++) {
1322                 if (fwhdr_1->md5sum[i] != fwhdr_2->md5sum[i])
1323                         return false;
1324         }
1325
1326         return true;
1327 }
1328
1329 /* Returns TRUE if major minor and maintenance are same.
1330  * If patch version are same, check for MD5 Checksum to be same.
1331  */
1332 static bool
1333 bfa_ioc_fw_ver_compatible(struct bfi_ioc_image_hdr *drv_fwhdr,
1334                           struct bfi_ioc_image_hdr *fwhdr_to_cmp)
1335 {
1336         if (drv_fwhdr->signature != fwhdr_to_cmp->signature)
1337                 return false;
1338         if (drv_fwhdr->fwver.major != fwhdr_to_cmp->fwver.major)
1339                 return false;
1340         if (drv_fwhdr->fwver.minor != fwhdr_to_cmp->fwver.minor)
1341                 return false;
1342         if (drv_fwhdr->fwver.maint != fwhdr_to_cmp->fwver.maint)
1343                 return false;
1344         if (drv_fwhdr->fwver.patch == fwhdr_to_cmp->fwver.patch &&
1345             drv_fwhdr->fwver.phase == fwhdr_to_cmp->fwver.phase &&
1346             drv_fwhdr->fwver.build == fwhdr_to_cmp->fwver.build)
1347                 return bfa_ioc_fwver_md5_check(drv_fwhdr, fwhdr_to_cmp);
1348
1349         return true;
1350 }
1351
1352 static bool
1353 bfa_ioc_flash_fwver_valid(struct bfi_ioc_image_hdr *flash_fwhdr)
1354 {
1355         if (flash_fwhdr->fwver.major == 0 || flash_fwhdr->fwver.major == 0xFF)
1356                 return false;
1357
1358         return true;
1359 }
1360
1361 static bool
1362 fwhdr_is_ga(struct bfi_ioc_image_hdr *fwhdr)
1363 {
1364         if (fwhdr->fwver.phase == 0 &&
1365             fwhdr->fwver.build == 0)
1366                 return false;
1367
1368         return true;
1369 }
1370
1371 /* Returns TRUE if both are compatible and patch of fwhdr_to_cmp is better. */
1372 static enum bfi_ioc_img_ver_cmp
1373 bfa_ioc_fw_ver_patch_cmp(struct bfi_ioc_image_hdr *base_fwhdr,
1374                          struct bfi_ioc_image_hdr *fwhdr_to_cmp)
1375 {
1376         if (!bfa_ioc_fw_ver_compatible(base_fwhdr, fwhdr_to_cmp))
1377                 return BFI_IOC_IMG_VER_INCOMP;
1378
1379         if (fwhdr_to_cmp->fwver.patch > base_fwhdr->fwver.patch)
1380                 return BFI_IOC_IMG_VER_BETTER;
1381         else if (fwhdr_to_cmp->fwver.patch < base_fwhdr->fwver.patch)
1382                 return BFI_IOC_IMG_VER_OLD;
1383
1384         /* GA takes priority over internal builds of the same patch stream.
1385          * At this point major minor maint and patch numbers are same.
1386          */
1387         if (fwhdr_is_ga(base_fwhdr))
1388                 if (fwhdr_is_ga(fwhdr_to_cmp))
1389                         return BFI_IOC_IMG_VER_SAME;
1390                 else
1391                         return BFI_IOC_IMG_VER_OLD;
1392         else
1393                 if (fwhdr_is_ga(fwhdr_to_cmp))
1394                         return BFI_IOC_IMG_VER_BETTER;
1395
1396         if (fwhdr_to_cmp->fwver.phase > base_fwhdr->fwver.phase)
1397                 return BFI_IOC_IMG_VER_BETTER;
1398         else if (fwhdr_to_cmp->fwver.phase < base_fwhdr->fwver.phase)
1399                 return BFI_IOC_IMG_VER_OLD;
1400
1401         if (fwhdr_to_cmp->fwver.build > base_fwhdr->fwver.build)
1402                 return BFI_IOC_IMG_VER_BETTER;
1403         else if (fwhdr_to_cmp->fwver.build < base_fwhdr->fwver.build)
1404                 return BFI_IOC_IMG_VER_OLD;
1405
1406         /* All Version Numbers are equal.
1407          * Md5 check to be done as a part of compatibility check.
1408          */
1409         return BFI_IOC_IMG_VER_SAME;
1410 }
1411
1412 /* register definitions */
1413 #define FLI_CMD_REG                     0x0001d000
1414 #define FLI_WRDATA_REG                  0x0001d00c
1415 #define FLI_RDDATA_REG                  0x0001d010
1416 #define FLI_ADDR_REG                    0x0001d004
1417 #define FLI_DEV_STATUS_REG              0x0001d014
1418
1419 #define BFA_FLASH_FIFO_SIZE             128     /* fifo size */
1420 #define BFA_FLASH_CHECK_MAX             10000   /* max # of status check */
1421 #define BFA_FLASH_BLOCKING_OP_MAX       1000000 /* max # of blocking op check */
1422 #define BFA_FLASH_WIP_MASK              0x01    /* write in progress bit mask */
1423
1424 #define NFC_STATE_RUNNING               0x20000001
1425 #define NFC_STATE_PAUSED                0x00004560
1426 #define NFC_VER_VALID                   0x147
1427
1428 enum bfa_flash_cmd {
1429         BFA_FLASH_FAST_READ     = 0x0b, /* fast read */
1430         BFA_FLASH_WRITE_ENABLE  = 0x06, /* write enable */
1431         BFA_FLASH_SECTOR_ERASE  = 0xd8, /* sector erase */
1432         BFA_FLASH_WRITE         = 0x02, /* write */
1433         BFA_FLASH_READ_STATUS   = 0x05, /* read status */
1434 };
1435
1436 /* hardware error definition */
1437 enum bfa_flash_err {
1438         BFA_FLASH_NOT_PRESENT   = -1,   /*!< flash not present */
1439         BFA_FLASH_UNINIT        = -2,   /*!< flash not initialized */
1440         BFA_FLASH_BAD           = -3,   /*!< flash bad */
1441         BFA_FLASH_BUSY          = -4,   /*!< flash busy */
1442         BFA_FLASH_ERR_CMD_ACT   = -5,   /*!< command active never cleared */
1443         BFA_FLASH_ERR_FIFO_CNT  = -6,   /*!< fifo count never cleared */
1444         BFA_FLASH_ERR_WIP       = -7,   /*!< write-in-progress never cleared */
1445         BFA_FLASH_ERR_TIMEOUT   = -8,   /*!< fli timeout */
1446         BFA_FLASH_ERR_LEN       = -9,   /*!< invalid length */
1447 };
1448
1449 /* flash command register data structure */
1450 union bfa_flash_cmd_reg {
1451         struct {
1452 #ifdef __BIG_ENDIAN
1453                 u32     act:1;
1454                 u32     rsv:1;
1455                 u32     write_cnt:9;
1456                 u32     read_cnt:9;
1457                 u32     addr_cnt:4;
1458                 u32     cmd:8;
1459 #else
1460                 u32     cmd:8;
1461                 u32     addr_cnt:4;
1462                 u32     read_cnt:9;
1463                 u32     write_cnt:9;
1464                 u32     rsv:1;
1465                 u32     act:1;
1466 #endif
1467         } r;
1468         u32     i;
1469 };
1470
1471 /* flash device status register data structure */
1472 union bfa_flash_dev_status_reg {
1473         struct {
1474 #ifdef __BIG_ENDIAN
1475                 u32     rsv:21;
1476                 u32     fifo_cnt:6;
1477                 u32     busy:1;
1478                 u32     init_status:1;
1479                 u32     present:1;
1480                 u32     bad:1;
1481                 u32     good:1;
1482 #else
1483                 u32     good:1;
1484                 u32     bad:1;
1485                 u32     present:1;
1486                 u32     init_status:1;
1487                 u32     busy:1;
1488                 u32     fifo_cnt:6;
1489                 u32     rsv:21;
1490 #endif
1491         } r;
1492         u32     i;
1493 };
1494
1495 /* flash address register data structure */
1496 union bfa_flash_addr_reg {
1497         struct {
1498 #ifdef __BIG_ENDIAN
1499                 u32     addr:24;
1500                 u32     dummy:8;
1501 #else
1502                 u32     dummy:8;
1503                 u32     addr:24;
1504 #endif
1505         } r;
1506         u32     i;
1507 };
1508
1509 /* Flash raw private functions */
1510 static void
1511 bfa_flash_set_cmd(void __iomem *pci_bar, u8 wr_cnt,
1512                   u8 rd_cnt, u8 ad_cnt, u8 op)
1513 {
1514         union bfa_flash_cmd_reg cmd;
1515
1516         cmd.i = 0;
1517         cmd.r.act = 1;
1518         cmd.r.write_cnt = wr_cnt;
1519         cmd.r.read_cnt = rd_cnt;
1520         cmd.r.addr_cnt = ad_cnt;
1521         cmd.r.cmd = op;
1522         writel(cmd.i, (pci_bar + FLI_CMD_REG));
1523 }
1524
1525 static void
1526 bfa_flash_set_addr(void __iomem *pci_bar, u32 address)
1527 {
1528         union bfa_flash_addr_reg addr;
1529
1530         addr.r.addr = address & 0x00ffffff;
1531         addr.r.dummy = 0;
1532         writel(addr.i, (pci_bar + FLI_ADDR_REG));
1533 }
1534
1535 static int
1536 bfa_flash_cmd_act_check(void __iomem *pci_bar)
1537 {
1538         union bfa_flash_cmd_reg cmd;
1539
1540         cmd.i = readl(pci_bar + FLI_CMD_REG);
1541
1542         if (cmd.r.act)
1543                 return BFA_FLASH_ERR_CMD_ACT;
1544
1545         return 0;
1546 }
1547
1548 /* Flush FLI data fifo. */
1549 static u32
1550 bfa_flash_fifo_flush(void __iomem *pci_bar)
1551 {
1552         u32 i;
1553         u32 t;
1554         union bfa_flash_dev_status_reg dev_status;
1555
1556         dev_status.i = readl(pci_bar + FLI_DEV_STATUS_REG);
1557
1558         if (!dev_status.r.fifo_cnt)
1559                 return 0;
1560
1561         /* fifo counter in terms of words */
1562         for (i = 0; i < dev_status.r.fifo_cnt; i++)
1563                 t = readl(pci_bar + FLI_RDDATA_REG);
1564
1565         /* Check the device status. It may take some time. */
1566         for (i = 0; i < BFA_FLASH_CHECK_MAX; i++) {
1567                 dev_status.i = readl(pci_bar + FLI_DEV_STATUS_REG);
1568                 if (!dev_status.r.fifo_cnt)
1569                         break;
1570         }
1571
1572         if (dev_status.r.fifo_cnt)
1573                 return BFA_FLASH_ERR_FIFO_CNT;
1574
1575         return 0;
1576 }
1577
1578 /* Read flash status. */
1579 static u32
1580 bfa_flash_status_read(void __iomem *pci_bar)
1581 {
1582         union bfa_flash_dev_status_reg  dev_status;
1583         u32                             status;
1584         u32                     ret_status;
1585         int                             i;
1586
1587         status = bfa_flash_fifo_flush(pci_bar);
1588         if (status < 0)
1589                 return status;
1590
1591         bfa_flash_set_cmd(pci_bar, 0, 4, 0, BFA_FLASH_READ_STATUS);
1592
1593         for (i = 0; i < BFA_FLASH_CHECK_MAX; i++) {
1594                 status = bfa_flash_cmd_act_check(pci_bar);
1595                 if (!status)
1596                         break;
1597         }
1598
1599         if (status)
1600                 return status;
1601
1602         dev_status.i = readl(pci_bar + FLI_DEV_STATUS_REG);
1603         if (!dev_status.r.fifo_cnt)
1604                 return BFA_FLASH_BUSY;
1605
1606         ret_status = readl(pci_bar + FLI_RDDATA_REG);
1607         ret_status >>= 24;
1608
1609         status = bfa_flash_fifo_flush(pci_bar);
1610         if (status < 0)
1611                 return status;
1612
1613         return ret_status;
1614 }
1615
1616 /* Start flash read operation. */
1617 static u32
1618 bfa_flash_read_start(void __iomem *pci_bar, u32 offset, u32 len,
1619                      char *buf)
1620 {
1621         u32 status;
1622
1623         /* len must be mutiple of 4 and not exceeding fifo size */
1624         if (len == 0 || len > BFA_FLASH_FIFO_SIZE || (len & 0x03) != 0)
1625                 return BFA_FLASH_ERR_LEN;
1626
1627         /* check status */
1628         status = bfa_flash_status_read(pci_bar);
1629         if (status == BFA_FLASH_BUSY)
1630                 status = bfa_flash_status_read(pci_bar);
1631
1632         if (status < 0)
1633                 return status;
1634
1635         /* check if write-in-progress bit is cleared */
1636         if (status & BFA_FLASH_WIP_MASK)
1637                 return BFA_FLASH_ERR_WIP;
1638
1639         bfa_flash_set_addr(pci_bar, offset);
1640
1641         bfa_flash_set_cmd(pci_bar, 0, (u8)len, 4, BFA_FLASH_FAST_READ);
1642
1643         return 0;
1644 }
1645
1646 /* Check flash read operation. */
1647 static u32
1648 bfa_flash_read_check(void __iomem *pci_bar)
1649 {
1650         if (bfa_flash_cmd_act_check(pci_bar))
1651                 return 1;
1652
1653         return 0;
1654 }
1655
1656 /* End flash read operation. */
1657 static void
1658 bfa_flash_read_end(void __iomem *pci_bar, u32 len, char *buf)
1659 {
1660         u32 i;
1661
1662         /* read data fifo up to 32 words */
1663         for (i = 0; i < len; i += 4) {
1664                 u32 w = readl(pci_bar + FLI_RDDATA_REG);
1665                 *((u32 *)(buf + i)) = swab32(w);
1666         }
1667
1668         bfa_flash_fifo_flush(pci_bar);
1669 }
1670
1671 /* Perform flash raw read. */
1672
1673 #define FLASH_BLOCKING_OP_MAX   500
1674 #define FLASH_SEM_LOCK_REG      0x18820
1675
1676 static int
1677 bfa_raw_sem_get(void __iomem *bar)
1678 {
1679         int     locked;
1680
1681         locked = readl((bar + FLASH_SEM_LOCK_REG));
1682
1683         return !locked;
1684 }
1685
1686 static enum bfa_status
1687 bfa_flash_sem_get(void __iomem *bar)
1688 {
1689         u32 n = FLASH_BLOCKING_OP_MAX;
1690
1691         while (!bfa_raw_sem_get(bar)) {
1692                 if (--n <= 0)
1693                         return BFA_STATUS_BADFLASH;
1694                 mdelay(10);
1695         }
1696         return BFA_STATUS_OK;
1697 }
1698
1699 static void
1700 bfa_flash_sem_put(void __iomem *bar)
1701 {
1702         writel(0, (bar + FLASH_SEM_LOCK_REG));
1703 }
1704
1705 static enum bfa_status
1706 bfa_flash_raw_read(void __iomem *pci_bar, u32 offset, char *buf,
1707                    u32 len)
1708 {
1709         u32 n, status;
1710         u32 off, l, s, residue, fifo_sz;
1711
1712         residue = len;
1713         off = 0;
1714         fifo_sz = BFA_FLASH_FIFO_SIZE;
1715         status = bfa_flash_sem_get(pci_bar);
1716         if (status != BFA_STATUS_OK)
1717                 return status;
1718
1719         while (residue) {
1720                 s = offset + off;
1721                 n = s / fifo_sz;
1722                 l = (n + 1) * fifo_sz - s;
1723                 if (l > residue)
1724                         l = residue;
1725
1726                 status = bfa_flash_read_start(pci_bar, offset + off, l,
1727                                                                 &buf[off]);
1728                 if (status < 0) {
1729                         bfa_flash_sem_put(pci_bar);
1730                         return BFA_STATUS_FAILED;
1731                 }
1732
1733                 n = BFA_FLASH_BLOCKING_OP_MAX;
1734                 while (bfa_flash_read_check(pci_bar)) {
1735                         if (--n <= 0) {
1736                                 bfa_flash_sem_put(pci_bar);
1737                                 return BFA_STATUS_FAILED;
1738                         }
1739                 }
1740
1741                 bfa_flash_read_end(pci_bar, l, &buf[off]);
1742
1743                 residue -= l;
1744                 off += l;
1745         }
1746         bfa_flash_sem_put(pci_bar);
1747
1748         return BFA_STATUS_OK;
1749 }
1750
1751 #define BFA_FLASH_PART_FWIMG_ADDR       0x100000 /* fw image address */
1752
1753 static enum bfa_status
1754 bfa_nw_ioc_flash_img_get_chnk(struct bfa_ioc *ioc, u32 off,
1755                               u32 *fwimg)
1756 {
1757         return bfa_flash_raw_read(ioc->pcidev.pci_bar_kva,
1758                         BFA_FLASH_PART_FWIMG_ADDR + (off * sizeof(u32)),
1759                         (char *)fwimg, BFI_FLASH_CHUNK_SZ);
1760 }
1761
1762 static enum bfi_ioc_img_ver_cmp
1763 bfa_ioc_flash_fwver_cmp(struct bfa_ioc *ioc,
1764                         struct bfi_ioc_image_hdr *base_fwhdr)
1765 {
1766         struct bfi_ioc_image_hdr *flash_fwhdr;
1767         enum bfa_status status;
1768         u32 fwimg[BFI_FLASH_CHUNK_SZ_WORDS];
1769
1770         status = bfa_nw_ioc_flash_img_get_chnk(ioc, 0, fwimg);
1771         if (status != BFA_STATUS_OK)
1772                 return BFI_IOC_IMG_VER_INCOMP;
1773
1774         flash_fwhdr = (struct bfi_ioc_image_hdr *)fwimg;
1775         if (bfa_ioc_flash_fwver_valid(flash_fwhdr))
1776                 return bfa_ioc_fw_ver_patch_cmp(base_fwhdr, flash_fwhdr);
1777         else
1778                 return BFI_IOC_IMG_VER_INCOMP;
1779 }
1780
1781 /**
1782  * Returns TRUE if driver is willing to work with current smem f/w version.
1783  */
1784 bool
1785 bfa_nw_ioc_fwver_cmp(struct bfa_ioc *ioc, struct bfi_ioc_image_hdr *fwhdr)
1786 {
1787         struct bfi_ioc_image_hdr *drv_fwhdr;
1788         enum bfi_ioc_img_ver_cmp smem_flash_cmp, drv_smem_cmp;
1789
1790         drv_fwhdr = (struct bfi_ioc_image_hdr *)
1791                 bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc), 0);
1792
1793         /* If smem is incompatible or old, driver should not work with it. */
1794         drv_smem_cmp = bfa_ioc_fw_ver_patch_cmp(drv_fwhdr, fwhdr);
1795         if (drv_smem_cmp == BFI_IOC_IMG_VER_INCOMP ||
1796             drv_smem_cmp == BFI_IOC_IMG_VER_OLD) {
1797                 return false;
1798         }
1799
1800         /* IF Flash has a better F/W than smem do not work with smem.
1801          * If smem f/w == flash f/w, as smem f/w not old | incmp, work with it.
1802          * If Flash is old or incomp work with smem iff smem f/w == drv f/w.
1803          */
1804         smem_flash_cmp = bfa_ioc_flash_fwver_cmp(ioc, fwhdr);
1805
1806         if (smem_flash_cmp == BFI_IOC_IMG_VER_BETTER)
1807                 return false;
1808         else if (smem_flash_cmp == BFI_IOC_IMG_VER_SAME)
1809                 return true;
1810         else
1811                 return (drv_smem_cmp == BFI_IOC_IMG_VER_SAME) ?
1812                         true : false;
1813 }
1814
1815 /* Return true if current running version is valid. Firmware signature and
1816  * execution context (driver/bios) must match.
1817  */
1818 static bool
1819 bfa_ioc_fwver_valid(struct bfa_ioc *ioc, u32 boot_env)
1820 {
1821         struct bfi_ioc_image_hdr fwhdr;
1822
1823         bfa_nw_ioc_fwver_get(ioc, &fwhdr);
1824         if (swab32(fwhdr.bootenv) != boot_env)
1825                 return false;
1826
1827         return bfa_nw_ioc_fwver_cmp(ioc, &fwhdr);
1828 }
1829
1830 /* Conditionally flush any pending message from firmware at start. */
1831 static void
1832 bfa_ioc_msgflush(struct bfa_ioc *ioc)
1833 {
1834         u32     r32;
1835
1836         r32 = readl(ioc->ioc_regs.lpu_mbox_cmd);
1837         if (r32)
1838                 writel(1, ioc->ioc_regs.lpu_mbox_cmd);
1839 }
1840
1841 static void
1842 bfa_ioc_hwinit(struct bfa_ioc *ioc, bool force)
1843 {
1844         enum bfi_ioc_state ioc_fwstate;
1845         bool fwvalid;
1846         u32 boot_env;
1847
1848         ioc_fwstate = bfa_ioc_get_cur_ioc_fwstate(ioc);
1849
1850         if (force)
1851                 ioc_fwstate = BFI_IOC_UNINIT;
1852
1853         boot_env = BFI_FWBOOT_ENV_OS;
1854
1855         /**
1856          * check if firmware is valid
1857          */
1858         fwvalid = (ioc_fwstate == BFI_IOC_UNINIT) ?
1859                 false : bfa_ioc_fwver_valid(ioc, boot_env);
1860
1861         if (!fwvalid) {
1862                 if (bfa_ioc_boot(ioc, BFI_FWBOOT_TYPE_NORMAL, boot_env) ==
1863                                                                 BFA_STATUS_OK)
1864                         bfa_ioc_poll_fwinit(ioc);
1865
1866                 return;
1867         }
1868
1869         /**
1870          * If hardware initialization is in progress (initialized by other IOC),
1871          * just wait for an initialization completion interrupt.
1872          */
1873         if (ioc_fwstate == BFI_IOC_INITING) {
1874                 bfa_ioc_poll_fwinit(ioc);
1875                 return;
1876         }
1877
1878         /**
1879          * If IOC function is disabled and firmware version is same,
1880          * just re-enable IOC.
1881          */
1882         if (ioc_fwstate == BFI_IOC_DISABLED || ioc_fwstate == BFI_IOC_OP) {
1883                 /**
1884                  * When using MSI-X any pending firmware ready event should
1885                  * be flushed. Otherwise MSI-X interrupts are not delivered.
1886                  */
1887                 bfa_ioc_msgflush(ioc);
1888                 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_FWREADY);
1889                 return;
1890         }
1891
1892         /**
1893          * Initialize the h/w for any other states.
1894          */
1895         if (bfa_ioc_boot(ioc, BFI_FWBOOT_TYPE_NORMAL, boot_env) ==
1896                                                         BFA_STATUS_OK)
1897                 bfa_ioc_poll_fwinit(ioc);
1898 }
1899
1900 void
1901 bfa_nw_ioc_timeout(void *ioc_arg)
1902 {
1903         struct bfa_ioc *ioc = (struct bfa_ioc *) ioc_arg;
1904
1905         bfa_fsm_send_event(ioc, IOC_E_TIMEOUT);
1906 }
1907
1908 static void
1909 bfa_ioc_mbox_send(struct bfa_ioc *ioc, void *ioc_msg, int len)
1910 {
1911         u32 *msgp = (u32 *) ioc_msg;
1912         u32 i;
1913
1914         BUG_ON(!(len <= BFI_IOC_MSGLEN_MAX));
1915
1916         /*
1917          * first write msg to mailbox registers
1918          */
1919         for (i = 0; i < len / sizeof(u32); i++)
1920                 writel(cpu_to_le32(msgp[i]),
1921                               ioc->ioc_regs.hfn_mbox + i * sizeof(u32));
1922
1923         for (; i < BFI_IOC_MSGLEN_MAX / sizeof(u32); i++)
1924                 writel(0, ioc->ioc_regs.hfn_mbox + i * sizeof(u32));
1925
1926         /*
1927          * write 1 to mailbox CMD to trigger LPU event
1928          */
1929         writel(1, ioc->ioc_regs.hfn_mbox_cmd);
1930         (void) readl(ioc->ioc_regs.hfn_mbox_cmd);
1931 }
1932
1933 static void
1934 bfa_ioc_send_enable(struct bfa_ioc *ioc)
1935 {
1936         struct bfi_ioc_ctrl_req enable_req;
1937         struct timeval tv;
1938
1939         bfi_h2i_set(enable_req.mh, BFI_MC_IOC, BFI_IOC_H2I_ENABLE_REQ,
1940                     bfa_ioc_portid(ioc));
1941         enable_req.clscode = htons(ioc->clscode);
1942         do_gettimeofday(&tv);
1943         enable_req.tv_sec = ntohl(tv.tv_sec);
1944         bfa_ioc_mbox_send(ioc, &enable_req, sizeof(struct bfi_ioc_ctrl_req));
1945 }
1946
1947 static void
1948 bfa_ioc_send_disable(struct bfa_ioc *ioc)
1949 {
1950         struct bfi_ioc_ctrl_req disable_req;
1951
1952         bfi_h2i_set(disable_req.mh, BFI_MC_IOC, BFI_IOC_H2I_DISABLE_REQ,
1953                     bfa_ioc_portid(ioc));
1954         bfa_ioc_mbox_send(ioc, &disable_req, sizeof(struct bfi_ioc_ctrl_req));
1955 }
1956
1957 static void
1958 bfa_ioc_send_getattr(struct bfa_ioc *ioc)
1959 {
1960         struct bfi_ioc_getattr_req attr_req;
1961
1962         bfi_h2i_set(attr_req.mh, BFI_MC_IOC, BFI_IOC_H2I_GETATTR_REQ,
1963                     bfa_ioc_portid(ioc));
1964         bfa_dma_be_addr_set(attr_req.attr_addr, ioc->attr_dma.pa);
1965         bfa_ioc_mbox_send(ioc, &attr_req, sizeof(attr_req));
1966 }
1967
1968 void
1969 bfa_nw_ioc_hb_check(void *cbarg)
1970 {
1971         struct bfa_ioc *ioc = cbarg;
1972         u32     hb_count;
1973
1974         hb_count = readl(ioc->ioc_regs.heartbeat);
1975         if (ioc->hb_count == hb_count) {
1976                 bfa_ioc_recover(ioc);
1977                 return;
1978         } else {
1979                 ioc->hb_count = hb_count;
1980         }
1981
1982         bfa_ioc_mbox_poll(ioc);
1983         mod_timer(&ioc->hb_timer, jiffies +
1984                 msecs_to_jiffies(BFA_IOC_HB_TOV));
1985 }
1986
1987 static void
1988 bfa_ioc_hb_monitor(struct bfa_ioc *ioc)
1989 {
1990         ioc->hb_count = readl(ioc->ioc_regs.heartbeat);
1991         mod_timer(&ioc->hb_timer, jiffies +
1992                 msecs_to_jiffies(BFA_IOC_HB_TOV));
1993 }
1994
1995 static void
1996 bfa_ioc_hb_stop(struct bfa_ioc *ioc)
1997 {
1998         del_timer(&ioc->hb_timer);
1999 }
2000
2001 /* Initiate a full firmware download. */
2002 static enum bfa_status
2003 bfa_ioc_download_fw(struct bfa_ioc *ioc, u32 boot_type,
2004                     u32 boot_env)
2005 {
2006         u32 *fwimg;
2007         u32 pgnum;
2008         u32 loff = 0;
2009         u32 chunkno = 0;
2010         u32 i;
2011         u32 asicmode;
2012         u32 fwimg_size;
2013         u32 fwimg_buf[BFI_FLASH_CHUNK_SZ_WORDS];
2014         enum bfa_status status;
2015
2016         if (boot_env == BFI_FWBOOT_ENV_OS &&
2017             boot_type == BFI_FWBOOT_TYPE_FLASH) {
2018                 fwimg_size = BFI_FLASH_IMAGE_SZ/sizeof(u32);
2019
2020                 status = bfa_nw_ioc_flash_img_get_chnk(ioc,
2021                         BFA_IOC_FLASH_CHUNK_ADDR(chunkno), fwimg_buf);
2022                 if (status != BFA_STATUS_OK)
2023                         return status;
2024
2025                 fwimg = fwimg_buf;
2026         } else {
2027                 fwimg_size = bfa_cb_image_get_size(bfa_ioc_asic_gen(ioc));
2028                 fwimg = bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc),
2029                                         BFA_IOC_FLASH_CHUNK_ADDR(chunkno));
2030         }
2031
2032         pgnum = bfa_ioc_smem_pgnum(ioc, loff);
2033
2034         writel(pgnum, ioc->ioc_regs.host_page_num_fn);
2035
2036         for (i = 0; i < fwimg_size; i++) {
2037                 if (BFA_IOC_FLASH_CHUNK_NO(i) != chunkno) {
2038                         chunkno = BFA_IOC_FLASH_CHUNK_NO(i);
2039                         if (boot_env == BFI_FWBOOT_ENV_OS &&
2040                             boot_type == BFI_FWBOOT_TYPE_FLASH) {
2041                                 status = bfa_nw_ioc_flash_img_get_chnk(ioc,
2042                                         BFA_IOC_FLASH_CHUNK_ADDR(chunkno),
2043                                         fwimg_buf);
2044                                 if (status != BFA_STATUS_OK)
2045                                         return status;
2046
2047                                 fwimg = fwimg_buf;
2048                         } else {
2049                                 fwimg = bfa_cb_image_get_chunk(
2050                                         bfa_ioc_asic_gen(ioc),
2051                                         BFA_IOC_FLASH_CHUNK_ADDR(chunkno));
2052                         }
2053                 }
2054
2055                 /**
2056                  * write smem
2057                  */
2058                 writel((swab32(fwimg[BFA_IOC_FLASH_OFFSET_IN_CHUNK(i)])),
2059                               ((ioc->ioc_regs.smem_page_start) + (loff)));
2060
2061                 loff += sizeof(u32);
2062
2063                 /**
2064                  * handle page offset wrap around
2065                  */
2066                 loff = PSS_SMEM_PGOFF(loff);
2067                 if (loff == 0) {
2068                         pgnum++;
2069                         writel(pgnum,
2070                                       ioc->ioc_regs.host_page_num_fn);
2071                 }
2072         }
2073
2074         writel(bfa_ioc_smem_pgnum(ioc, 0),
2075                       ioc->ioc_regs.host_page_num_fn);
2076
2077         /*
2078          * Set boot type, env and device mode at the end.
2079         */
2080         if (boot_env == BFI_FWBOOT_ENV_OS &&
2081             boot_type == BFI_FWBOOT_TYPE_FLASH) {
2082                 boot_type = BFI_FWBOOT_TYPE_NORMAL;
2083         }
2084         asicmode = BFI_FWBOOT_DEVMODE(ioc->asic_gen, ioc->asic_mode,
2085                                         ioc->port0_mode, ioc->port1_mode);
2086         writel(asicmode, ((ioc->ioc_regs.smem_page_start)
2087                         + BFI_FWBOOT_DEVMODE_OFF));
2088         writel(boot_type, ((ioc->ioc_regs.smem_page_start)
2089                         + (BFI_FWBOOT_TYPE_OFF)));
2090         writel(boot_env, ((ioc->ioc_regs.smem_page_start)
2091                         + (BFI_FWBOOT_ENV_OFF)));
2092         return BFA_STATUS_OK;
2093 }
2094
2095 static void
2096 bfa_ioc_reset(struct bfa_ioc *ioc, bool force)
2097 {
2098         bfa_ioc_hwinit(ioc, force);
2099 }
2100
2101 /* BFA ioc enable reply by firmware */
2102 static void
2103 bfa_ioc_enable_reply(struct bfa_ioc *ioc, enum bfa_mode port_mode,
2104                         u8 cap_bm)
2105 {
2106         struct bfa_iocpf *iocpf = &ioc->iocpf;
2107
2108         ioc->port_mode = ioc->port_mode_cfg = port_mode;
2109         ioc->ad_cap_bm = cap_bm;
2110         bfa_fsm_send_event(iocpf, IOCPF_E_FWRSP_ENABLE);
2111 }
2112
2113 /* Update BFA configuration from firmware configuration. */
2114 static void
2115 bfa_ioc_getattr_reply(struct bfa_ioc *ioc)
2116 {
2117         struct bfi_ioc_attr *attr = ioc->attr;
2118
2119         attr->adapter_prop  = ntohl(attr->adapter_prop);
2120         attr->card_type     = ntohl(attr->card_type);
2121         attr->maxfrsize     = ntohs(attr->maxfrsize);
2122
2123         bfa_fsm_send_event(ioc, IOC_E_FWRSP_GETATTR);
2124 }
2125
2126 /* Attach time initialization of mbox logic. */
2127 static void
2128 bfa_ioc_mbox_attach(struct bfa_ioc *ioc)
2129 {
2130         struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2131         int     mc;
2132
2133         INIT_LIST_HEAD(&mod->cmd_q);
2134         for (mc = 0; mc < BFI_MC_MAX; mc++) {
2135                 mod->mbhdlr[mc].cbfn = NULL;
2136                 mod->mbhdlr[mc].cbarg = ioc->bfa;
2137         }
2138 }
2139
2140 /* Mbox poll timer -- restarts any pending mailbox requests. */
2141 static void
2142 bfa_ioc_mbox_poll(struct bfa_ioc *ioc)
2143 {
2144         struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2145         struct bfa_mbox_cmd *cmd;
2146         bfa_mbox_cmd_cbfn_t cbfn;
2147         void *cbarg;
2148         u32 stat;
2149
2150         /**
2151          * If no command pending, do nothing
2152          */
2153         if (list_empty(&mod->cmd_q))
2154                 return;
2155
2156         /**
2157          * If previous command is not yet fetched by firmware, do nothing
2158          */
2159         stat = readl(ioc->ioc_regs.hfn_mbox_cmd);
2160         if (stat)
2161                 return;
2162
2163         /**
2164          * Enqueue command to firmware.
2165          */
2166         cmd = list_first_entry(&mod->cmd_q, struct bfa_mbox_cmd, qe);
2167         list_del(&cmd->qe);
2168         bfa_ioc_mbox_send(ioc, cmd->msg, sizeof(cmd->msg));
2169
2170         /**
2171          * Give a callback to the client, indicating that the command is sent
2172          */
2173         if (cmd->cbfn) {
2174                 cbfn = cmd->cbfn;
2175                 cbarg = cmd->cbarg;
2176                 cmd->cbfn = NULL;
2177                 cbfn(cbarg);
2178         }
2179 }
2180
2181 /* Cleanup any pending requests. */
2182 static void
2183 bfa_ioc_mbox_flush(struct bfa_ioc *ioc)
2184 {
2185         struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2186         struct bfa_mbox_cmd *cmd;
2187
2188         while (!list_empty(&mod->cmd_q)) {
2189                 cmd = list_first_entry(&mod->cmd_q, struct bfa_mbox_cmd, qe);
2190                 list_del(&cmd->qe);
2191         }
2192 }
2193
2194 /**
2195  * bfa_nw_ioc_smem_read - Read data from SMEM to host through PCI memmap
2196  *
2197  * @ioc:     memory for IOC
2198  * @tbuf:    app memory to store data from smem
2199  * @soff:    smem offset
2200  * @sz:      size of smem in bytes
2201  */
2202 static int
2203 bfa_nw_ioc_smem_read(struct bfa_ioc *ioc, void *tbuf, u32 soff, u32 sz)
2204 {
2205         u32 pgnum, loff, r32;
2206         int i, len;
2207         u32 *buf = tbuf;
2208
2209         pgnum = PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, soff);
2210         loff = PSS_SMEM_PGOFF(soff);
2211
2212         /*
2213          *  Hold semaphore to serialize pll init and fwtrc.
2214         */
2215         if (!bfa_nw_ioc_sem_get(ioc->ioc_regs.ioc_init_sem_reg))
2216                 return 1;
2217
2218         writel(pgnum, ioc->ioc_regs.host_page_num_fn);
2219
2220         len = sz/sizeof(u32);
2221         for (i = 0; i < len; i++) {
2222                 r32 = swab32(readl((loff) + (ioc->ioc_regs.smem_page_start)));
2223                 buf[i] = be32_to_cpu(r32);
2224                 loff += sizeof(u32);
2225
2226                 /**
2227                  * handle page offset wrap around
2228                  */
2229                 loff = PSS_SMEM_PGOFF(loff);
2230                 if (loff == 0) {
2231                         pgnum++;
2232                         writel(pgnum, ioc->ioc_regs.host_page_num_fn);
2233                 }
2234         }
2235
2236         writel(PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, 0),
2237                ioc->ioc_regs.host_page_num_fn);
2238
2239         /*
2240          * release semaphore
2241          */
2242         readl(ioc->ioc_regs.ioc_init_sem_reg);
2243         writel(1, ioc->ioc_regs.ioc_init_sem_reg);
2244         return 0;
2245 }
2246
2247 /* Retrieve saved firmware trace from a prior IOC failure. */
2248 int
2249 bfa_nw_ioc_debug_fwtrc(struct bfa_ioc *ioc, void *trcdata, int *trclen)
2250 {
2251         u32 loff = BFI_IOC_TRC_OFF + BNA_DBG_FWTRC_LEN * ioc->port_id;
2252         int tlen, status = 0;
2253
2254         tlen = *trclen;
2255         if (tlen > BNA_DBG_FWTRC_LEN)
2256                 tlen = BNA_DBG_FWTRC_LEN;
2257
2258         status = bfa_nw_ioc_smem_read(ioc, trcdata, loff, tlen);
2259         *trclen = tlen;
2260         return status;
2261 }
2262
2263 /* Save firmware trace if configured. */
2264 static void
2265 bfa_nw_ioc_debug_save_ftrc(struct bfa_ioc *ioc)
2266 {
2267         int tlen;
2268
2269         if (ioc->dbg_fwsave_once) {
2270                 ioc->dbg_fwsave_once = false;
2271                 if (ioc->dbg_fwsave_len) {
2272                         tlen = ioc->dbg_fwsave_len;
2273                         bfa_nw_ioc_debug_fwtrc(ioc, ioc->dbg_fwsave, &tlen);
2274                 }
2275         }
2276 }
2277
2278 /* Retrieve saved firmware trace from a prior IOC failure. */
2279 int
2280 bfa_nw_ioc_debug_fwsave(struct bfa_ioc *ioc, void *trcdata, int *trclen)
2281 {
2282         int tlen;
2283
2284         if (ioc->dbg_fwsave_len == 0)
2285                 return BFA_STATUS_ENOFSAVE;
2286
2287         tlen = *trclen;
2288         if (tlen > ioc->dbg_fwsave_len)
2289                 tlen = ioc->dbg_fwsave_len;
2290
2291         memcpy(trcdata, ioc->dbg_fwsave, tlen);
2292         *trclen = tlen;
2293         return BFA_STATUS_OK;
2294 }
2295
2296 static void
2297 bfa_ioc_fail_notify(struct bfa_ioc *ioc)
2298 {
2299         /**
2300          * Notify driver and common modules registered for notification.
2301          */
2302         ioc->cbfn->hbfail_cbfn(ioc->bfa);
2303         bfa_ioc_event_notify(ioc, BFA_IOC_E_FAILED);
2304         bfa_nw_ioc_debug_save_ftrc(ioc);
2305 }
2306
2307 /* IOCPF to IOC interface */
2308 static void
2309 bfa_ioc_pf_enabled(struct bfa_ioc *ioc)
2310 {
2311         bfa_fsm_send_event(ioc, IOC_E_ENABLED);
2312 }
2313
2314 static void
2315 bfa_ioc_pf_disabled(struct bfa_ioc *ioc)
2316 {
2317         bfa_fsm_send_event(ioc, IOC_E_DISABLED);
2318 }
2319
2320 static void
2321 bfa_ioc_pf_failed(struct bfa_ioc *ioc)
2322 {
2323         bfa_fsm_send_event(ioc, IOC_E_PFFAILED);
2324 }
2325
2326 static void
2327 bfa_ioc_pf_hwfailed(struct bfa_ioc *ioc)
2328 {
2329         bfa_fsm_send_event(ioc, IOC_E_HWFAILED);
2330 }
2331
2332 static void
2333 bfa_ioc_pf_fwmismatch(struct bfa_ioc *ioc)
2334 {
2335         /**
2336          * Provide enable completion callback and AEN notification.
2337          */
2338         ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
2339 }
2340
2341 /* IOC public */
2342 static enum bfa_status
2343 bfa_ioc_pll_init(struct bfa_ioc *ioc)
2344 {
2345         /*
2346          *  Hold semaphore so that nobody can access the chip during init.
2347          */
2348         bfa_nw_ioc_sem_get(ioc->ioc_regs.ioc_init_sem_reg);
2349
2350         bfa_ioc_pll_init_asic(ioc);
2351
2352         ioc->pllinit = true;
2353
2354         /* Initialize LMEM */
2355         bfa_ioc_lmem_init(ioc);
2356
2357         /*
2358          *  release semaphore.
2359          */
2360         bfa_nw_ioc_sem_release(ioc->ioc_regs.ioc_init_sem_reg);
2361
2362         return BFA_STATUS_OK;
2363 }
2364
2365 /* Interface used by diag module to do firmware boot with memory test
2366  * as the entry vector.
2367  */
2368 static enum bfa_status
2369 bfa_ioc_boot(struct bfa_ioc *ioc, enum bfi_fwboot_type boot_type,
2370                 u32 boot_env)
2371 {
2372         struct bfi_ioc_image_hdr *drv_fwhdr;
2373         enum bfa_status status;
2374         bfa_ioc_stats(ioc, ioc_boots);
2375
2376         if (bfa_ioc_pll_init(ioc) != BFA_STATUS_OK)
2377                 return BFA_STATUS_FAILED;
2378         if (boot_env == BFI_FWBOOT_ENV_OS &&
2379             boot_type == BFI_FWBOOT_TYPE_NORMAL) {
2380                 drv_fwhdr = (struct bfi_ioc_image_hdr *)
2381                         bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc), 0);
2382                 /* Work with Flash iff flash f/w is better than driver f/w.
2383                  * Otherwise push drivers firmware.
2384                  */
2385                 if (bfa_ioc_flash_fwver_cmp(ioc, drv_fwhdr) ==
2386                         BFI_IOC_IMG_VER_BETTER)
2387                         boot_type = BFI_FWBOOT_TYPE_FLASH;
2388         }
2389
2390         /**
2391          * Initialize IOC state of all functions on a chip reset.
2392          */
2393         if (boot_type == BFI_FWBOOT_TYPE_MEMTEST) {
2394                 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_MEMTEST);
2395                 bfa_ioc_set_alt_ioc_fwstate(ioc, BFI_IOC_MEMTEST);
2396         } else {
2397                 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_INITING);
2398                 bfa_ioc_set_alt_ioc_fwstate(ioc, BFI_IOC_INITING);
2399         }
2400
2401         bfa_ioc_msgflush(ioc);
2402         status = bfa_ioc_download_fw(ioc, boot_type, boot_env);
2403         if (status == BFA_STATUS_OK)
2404                 bfa_ioc_lpu_start(ioc);
2405         else
2406                 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_TIMEOUT);
2407
2408         return status;
2409 }
2410
2411 /* Enable/disable IOC failure auto recovery. */
2412 void
2413 bfa_nw_ioc_auto_recover(bool auto_recover)
2414 {
2415         bfa_nw_auto_recover = auto_recover;
2416 }
2417
2418 static bool
2419 bfa_ioc_msgget(struct bfa_ioc *ioc, void *mbmsg)
2420 {
2421         u32     *msgp = mbmsg;
2422         u32     r32;
2423         int             i;
2424
2425         r32 = readl(ioc->ioc_regs.lpu_mbox_cmd);
2426         if ((r32 & 1) == 0)
2427                 return false;
2428
2429         /**
2430          * read the MBOX msg
2431          */
2432         for (i = 0; i < (sizeof(union bfi_ioc_i2h_msg_u) / sizeof(u32));
2433              i++) {
2434                 r32 = readl(ioc->ioc_regs.lpu_mbox +
2435                                    i * sizeof(u32));
2436                 msgp[i] = htonl(r32);
2437         }
2438
2439         /**
2440          * turn off mailbox interrupt by clearing mailbox status
2441          */
2442         writel(1, ioc->ioc_regs.lpu_mbox_cmd);
2443         readl(ioc->ioc_regs.lpu_mbox_cmd);
2444
2445         return true;
2446 }
2447
2448 static void
2449 bfa_ioc_isr(struct bfa_ioc *ioc, struct bfi_mbmsg *m)
2450 {
2451         union bfi_ioc_i2h_msg_u *msg;
2452         struct bfa_iocpf *iocpf = &ioc->iocpf;
2453
2454         msg = (union bfi_ioc_i2h_msg_u *) m;
2455
2456         bfa_ioc_stats(ioc, ioc_isrs);
2457
2458         switch (msg->mh.msg_id) {
2459         case BFI_IOC_I2H_HBEAT:
2460                 break;
2461
2462         case BFI_IOC_I2H_ENABLE_REPLY:
2463                 bfa_ioc_enable_reply(ioc,
2464                         (enum bfa_mode)msg->fw_event.port_mode,
2465                         msg->fw_event.cap_bm);
2466                 break;
2467
2468         case BFI_IOC_I2H_DISABLE_REPLY:
2469                 bfa_fsm_send_event(iocpf, IOCPF_E_FWRSP_DISABLE);
2470                 break;
2471
2472         case BFI_IOC_I2H_GETATTR_REPLY:
2473                 bfa_ioc_getattr_reply(ioc);
2474                 break;
2475
2476         default:
2477                 BUG_ON(1);
2478         }
2479 }
2480
2481 /**
2482  * bfa_nw_ioc_attach - IOC attach time initialization and setup.
2483  *
2484  * @ioc:        memory for IOC
2485  * @bfa:        driver instance structure
2486  */
2487 void
2488 bfa_nw_ioc_attach(struct bfa_ioc *ioc, void *bfa, struct bfa_ioc_cbfn *cbfn)
2489 {
2490         ioc->bfa        = bfa;
2491         ioc->cbfn       = cbfn;
2492         ioc->fcmode     = false;
2493         ioc->pllinit    = false;
2494         ioc->dbg_fwsave_once = true;
2495         ioc->iocpf.ioc  = ioc;
2496
2497         bfa_ioc_mbox_attach(ioc);
2498         INIT_LIST_HEAD(&ioc->notify_q);
2499
2500         bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
2501         bfa_fsm_send_event(ioc, IOC_E_RESET);
2502 }
2503
2504 /* Driver detach time IOC cleanup. */
2505 void
2506 bfa_nw_ioc_detach(struct bfa_ioc *ioc)
2507 {
2508         bfa_fsm_send_event(ioc, IOC_E_DETACH);
2509
2510         /* Done with detach, empty the notify_q. */
2511         INIT_LIST_HEAD(&ioc->notify_q);
2512 }
2513
2514 /**
2515  * bfa_nw_ioc_pci_init - Setup IOC PCI properties.
2516  *
2517  * @pcidev:     PCI device information for this IOC
2518  */
2519 void
2520 bfa_nw_ioc_pci_init(struct bfa_ioc *ioc, struct bfa_pcidev *pcidev,
2521                  enum bfi_pcifn_class clscode)
2522 {
2523         ioc->clscode    = clscode;
2524         ioc->pcidev     = *pcidev;
2525
2526         /**
2527          * Initialize IOC and device personality
2528          */
2529         ioc->port0_mode = ioc->port1_mode = BFI_PORT_MODE_FC;
2530         ioc->asic_mode  = BFI_ASIC_MODE_FC;
2531
2532         switch (pcidev->device_id) {
2533         case PCI_DEVICE_ID_BROCADE_CT:
2534                 ioc->asic_gen = BFI_ASIC_GEN_CT;
2535                 ioc->port0_mode = ioc->port1_mode = BFI_PORT_MODE_ETH;
2536                 ioc->asic_mode  = BFI_ASIC_MODE_ETH;
2537                 ioc->port_mode = ioc->port_mode_cfg = BFA_MODE_CNA;
2538                 ioc->ad_cap_bm = BFA_CM_CNA;
2539                 break;
2540
2541         case BFA_PCI_DEVICE_ID_CT2:
2542                 ioc->asic_gen = BFI_ASIC_GEN_CT2;
2543                 if (clscode == BFI_PCIFN_CLASS_FC &&
2544                         pcidev->ssid == BFA_PCI_CT2_SSID_FC) {
2545                         ioc->asic_mode  = BFI_ASIC_MODE_FC16;
2546                         ioc->fcmode = true;
2547                         ioc->port_mode = ioc->port_mode_cfg = BFA_MODE_HBA;
2548                         ioc->ad_cap_bm = BFA_CM_HBA;
2549                 } else {
2550                         ioc->port0_mode = ioc->port1_mode = BFI_PORT_MODE_ETH;
2551                         ioc->asic_mode  = BFI_ASIC_MODE_ETH;
2552                         if (pcidev->ssid == BFA_PCI_CT2_SSID_FCoE) {
2553                                 ioc->port_mode =
2554                                 ioc->port_mode_cfg = BFA_MODE_CNA;
2555                                 ioc->ad_cap_bm = BFA_CM_CNA;
2556                         } else {
2557                                 ioc->port_mode =
2558                                 ioc->port_mode_cfg = BFA_MODE_NIC;
2559                                 ioc->ad_cap_bm = BFA_CM_NIC;
2560                         }
2561                 }
2562                 break;
2563
2564         default:
2565                 BUG_ON(1);
2566         }
2567
2568         /**
2569          * Set asic specific interfaces.
2570          */
2571         if (ioc->asic_gen == BFI_ASIC_GEN_CT)
2572                 bfa_nw_ioc_set_ct_hwif(ioc);
2573         else {
2574                 WARN_ON(ioc->asic_gen != BFI_ASIC_GEN_CT2);
2575                 bfa_nw_ioc_set_ct2_hwif(ioc);
2576                 bfa_nw_ioc_ct2_poweron(ioc);
2577         }
2578
2579         bfa_ioc_map_port(ioc);
2580         bfa_ioc_reg_init(ioc);
2581 }
2582
2583 /**
2584  * bfa_nw_ioc_mem_claim - Initialize IOC dma memory
2585  *
2586  * @dm_kva:     kernel virtual address of IOC dma memory
2587  * @dm_pa:      physical address of IOC dma memory
2588  */
2589 void
2590 bfa_nw_ioc_mem_claim(struct bfa_ioc *ioc,  u8 *dm_kva, u64 dm_pa)
2591 {
2592         /**
2593          * dma memory for firmware attribute
2594          */
2595         ioc->attr_dma.kva = dm_kva;
2596         ioc->attr_dma.pa = dm_pa;
2597         ioc->attr = (struct bfi_ioc_attr *) dm_kva;
2598 }
2599
2600 /* Return size of dma memory required. */
2601 u32
2602 bfa_nw_ioc_meminfo(void)
2603 {
2604         return roundup(sizeof(struct bfi_ioc_attr), BFA_DMA_ALIGN_SZ);
2605 }
2606
2607 void
2608 bfa_nw_ioc_enable(struct bfa_ioc *ioc)
2609 {
2610         bfa_ioc_stats(ioc, ioc_enables);
2611         ioc->dbg_fwsave_once = true;
2612
2613         bfa_fsm_send_event(ioc, IOC_E_ENABLE);
2614 }
2615
2616 void
2617 bfa_nw_ioc_disable(struct bfa_ioc *ioc)
2618 {
2619         bfa_ioc_stats(ioc, ioc_disables);
2620         bfa_fsm_send_event(ioc, IOC_E_DISABLE);
2621 }
2622
2623 /* Initialize memory for saving firmware trace. */
2624 void
2625 bfa_nw_ioc_debug_memclaim(struct bfa_ioc *ioc, void *dbg_fwsave)
2626 {
2627         ioc->dbg_fwsave = dbg_fwsave;
2628         ioc->dbg_fwsave_len = ioc->iocpf.auto_recover ? BNA_DBG_FWTRC_LEN : 0;
2629 }
2630
2631 static u32
2632 bfa_ioc_smem_pgnum(struct bfa_ioc *ioc, u32 fmaddr)
2633 {
2634         return PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, fmaddr);
2635 }
2636
2637 /* Register mailbox message handler function, to be called by common modules */
2638 void
2639 bfa_nw_ioc_mbox_regisr(struct bfa_ioc *ioc, enum bfi_mclass mc,
2640                     bfa_ioc_mbox_mcfunc_t cbfn, void *cbarg)
2641 {
2642         struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2643
2644         mod->mbhdlr[mc].cbfn    = cbfn;
2645         mod->mbhdlr[mc].cbarg = cbarg;
2646 }
2647
2648 /**
2649  * bfa_nw_ioc_mbox_queue - Queue a mailbox command request to firmware.
2650  *
2651  * @ioc:        IOC instance
2652  * @cmd:        Mailbox command
2653  *
2654  * Waits if mailbox is busy. Responsibility of caller to serialize
2655  */
2656 bool
2657 bfa_nw_ioc_mbox_queue(struct bfa_ioc *ioc, struct bfa_mbox_cmd *cmd,
2658                         bfa_mbox_cmd_cbfn_t cbfn, void *cbarg)
2659 {
2660         struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2661         u32                     stat;
2662
2663         cmd->cbfn = cbfn;
2664         cmd->cbarg = cbarg;
2665
2666         /**
2667          * If a previous command is pending, queue new command
2668          */
2669         if (!list_empty(&mod->cmd_q)) {
2670                 list_add_tail(&cmd->qe, &mod->cmd_q);
2671                 return true;
2672         }
2673
2674         /**
2675          * If mailbox is busy, queue command for poll timer
2676          */
2677         stat = readl(ioc->ioc_regs.hfn_mbox_cmd);
2678         if (stat) {
2679                 list_add_tail(&cmd->qe, &mod->cmd_q);
2680                 return true;
2681         }
2682
2683         /**
2684          * mailbox is free -- queue command to firmware
2685          */
2686         bfa_ioc_mbox_send(ioc, cmd->msg, sizeof(cmd->msg));
2687
2688         return false;
2689 }
2690
2691 /* Handle mailbox interrupts */
2692 void
2693 bfa_nw_ioc_mbox_isr(struct bfa_ioc *ioc)
2694 {
2695         struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2696         struct bfi_mbmsg m;
2697         int                             mc;
2698
2699         if (bfa_ioc_msgget(ioc, &m)) {
2700                 /**
2701                  * Treat IOC message class as special.
2702                  */
2703                 mc = m.mh.msg_class;
2704                 if (mc == BFI_MC_IOC) {
2705                         bfa_ioc_isr(ioc, &m);
2706                         return;
2707                 }
2708
2709                 if ((mc >= BFI_MC_MAX) || (mod->mbhdlr[mc].cbfn == NULL))
2710                         return;
2711
2712                 mod->mbhdlr[mc].cbfn(mod->mbhdlr[mc].cbarg, &m);
2713         }
2714
2715         bfa_ioc_lpu_read_stat(ioc);
2716
2717         /**
2718          * Try to send pending mailbox commands
2719          */
2720         bfa_ioc_mbox_poll(ioc);
2721 }
2722
2723 void
2724 bfa_nw_ioc_error_isr(struct bfa_ioc *ioc)
2725 {
2726         bfa_ioc_stats(ioc, ioc_hbfails);
2727         bfa_ioc_stats_hb_count(ioc, ioc->hb_count);
2728         bfa_fsm_send_event(ioc, IOC_E_HWERROR);
2729 }
2730
2731 /* return true if IOC is disabled */
2732 bool
2733 bfa_nw_ioc_is_disabled(struct bfa_ioc *ioc)
2734 {
2735         return bfa_fsm_cmp_state(ioc, bfa_ioc_sm_disabling) ||
2736                 bfa_fsm_cmp_state(ioc, bfa_ioc_sm_disabled);
2737 }
2738
2739 /* return true if IOC is operational */
2740 bool
2741 bfa_nw_ioc_is_operational(struct bfa_ioc *ioc)
2742 {
2743         return bfa_fsm_cmp_state(ioc, bfa_ioc_sm_op);
2744 }
2745
2746 /* Add to IOC heartbeat failure notification queue. To be used by common
2747  * modules such as cee, port, diag.
2748  */
2749 void
2750 bfa_nw_ioc_notify_register(struct bfa_ioc *ioc,
2751                         struct bfa_ioc_notify *notify)
2752 {
2753         list_add_tail(&notify->qe, &ioc->notify_q);
2754 }
2755
2756 #define BFA_MFG_NAME "QLogic"
2757 static void
2758 bfa_ioc_get_adapter_attr(struct bfa_ioc *ioc,
2759                          struct bfa_adapter_attr *ad_attr)
2760 {
2761         struct bfi_ioc_attr *ioc_attr;
2762
2763         ioc_attr = ioc->attr;
2764
2765         bfa_ioc_get_adapter_serial_num(ioc, ad_attr->serial_num);
2766         bfa_ioc_get_adapter_fw_ver(ioc, ad_attr->fw_ver);
2767         bfa_ioc_get_adapter_optrom_ver(ioc, ad_attr->optrom_ver);
2768         bfa_ioc_get_adapter_manufacturer(ioc, ad_attr->manufacturer);
2769         memcpy(&ad_attr->vpd, &ioc_attr->vpd,
2770                       sizeof(struct bfa_mfg_vpd));
2771
2772         ad_attr->nports = bfa_ioc_get_nports(ioc);
2773         ad_attr->max_speed = bfa_ioc_speed_sup(ioc);
2774
2775         bfa_ioc_get_adapter_model(ioc, ad_attr->model);
2776         /* For now, model descr uses same model string */
2777         bfa_ioc_get_adapter_model(ioc, ad_attr->model_descr);
2778
2779         ad_attr->card_type = ioc_attr->card_type;
2780         ad_attr->is_mezz = bfa_mfg_is_mezz(ioc_attr->card_type);
2781
2782         if (BFI_ADAPTER_IS_SPECIAL(ioc_attr->adapter_prop))
2783                 ad_attr->prototype = 1;
2784         else
2785                 ad_attr->prototype = 0;
2786
2787         ad_attr->pwwn = bfa_ioc_get_pwwn(ioc);
2788         bfa_nw_ioc_get_mac(ioc, ad_attr->mac);
2789
2790         ad_attr->pcie_gen = ioc_attr->pcie_gen;
2791         ad_attr->pcie_lanes = ioc_attr->pcie_lanes;
2792         ad_attr->pcie_lanes_orig = ioc_attr->pcie_lanes_orig;
2793         ad_attr->asic_rev = ioc_attr->asic_rev;
2794
2795         bfa_ioc_get_pci_chip_rev(ioc, ad_attr->hw_ver);
2796 }
2797
2798 static enum bfa_ioc_type
2799 bfa_ioc_get_type(struct bfa_ioc *ioc)
2800 {
2801         if (ioc->clscode == BFI_PCIFN_CLASS_ETH)
2802                 return BFA_IOC_TYPE_LL;
2803
2804         BUG_ON(!(ioc->clscode == BFI_PCIFN_CLASS_FC));
2805
2806         return (ioc->attr->port_mode == BFI_PORT_MODE_FC)
2807                 ? BFA_IOC_TYPE_FC : BFA_IOC_TYPE_FCoE;
2808 }
2809
2810 static void
2811 bfa_ioc_get_adapter_serial_num(struct bfa_ioc *ioc, char *serial_num)
2812 {
2813         memcpy(serial_num,
2814                         (void *)ioc->attr->brcd_serialnum,
2815                         BFA_ADAPTER_SERIAL_NUM_LEN);
2816 }
2817
2818 static void
2819 bfa_ioc_get_adapter_fw_ver(struct bfa_ioc *ioc, char *fw_ver)
2820 {
2821         memcpy(fw_ver, ioc->attr->fw_version, BFA_VERSION_LEN);
2822 }
2823
2824 static void
2825 bfa_ioc_get_pci_chip_rev(struct bfa_ioc *ioc, char *chip_rev)
2826 {
2827         BUG_ON(!(chip_rev));
2828
2829         memset(chip_rev, 0, BFA_IOC_CHIP_REV_LEN);
2830
2831         chip_rev[0] = 'R';
2832         chip_rev[1] = 'e';
2833         chip_rev[2] = 'v';
2834         chip_rev[3] = '-';
2835         chip_rev[4] = ioc->attr->asic_rev;
2836         chip_rev[5] = '\0';
2837 }
2838
2839 static void
2840 bfa_ioc_get_adapter_optrom_ver(struct bfa_ioc *ioc, char *optrom_ver)
2841 {
2842         memcpy(optrom_ver, ioc->attr->optrom_version,
2843                       BFA_VERSION_LEN);
2844 }
2845
2846 static void
2847 bfa_ioc_get_adapter_manufacturer(struct bfa_ioc *ioc, char *manufacturer)
2848 {
2849         memcpy(manufacturer, BFA_MFG_NAME, BFA_ADAPTER_MFG_NAME_LEN);
2850 }
2851
2852 static void
2853 bfa_ioc_get_adapter_model(struct bfa_ioc *ioc, char *model)
2854 {
2855         struct bfi_ioc_attr *ioc_attr;
2856
2857         BUG_ON(!(model));
2858         memset(model, 0, BFA_ADAPTER_MODEL_NAME_LEN);
2859
2860         ioc_attr = ioc->attr;
2861
2862         snprintf(model, BFA_ADAPTER_MODEL_NAME_LEN, "%s-%u",
2863                 BFA_MFG_NAME, ioc_attr->card_type);
2864 }
2865
2866 static enum bfa_ioc_state
2867 bfa_ioc_get_state(struct bfa_ioc *ioc)
2868 {
2869         enum bfa_iocpf_state iocpf_st;
2870         enum bfa_ioc_state ioc_st = bfa_sm_to_state(ioc_sm_table, ioc->fsm);
2871
2872         if (ioc_st == BFA_IOC_ENABLING ||
2873                 ioc_st == BFA_IOC_FAIL || ioc_st == BFA_IOC_INITFAIL) {
2874
2875                 iocpf_st = bfa_sm_to_state(iocpf_sm_table, ioc->iocpf.fsm);
2876
2877                 switch (iocpf_st) {
2878                 case BFA_IOCPF_SEMWAIT:
2879                         ioc_st = BFA_IOC_SEMWAIT;
2880                         break;
2881
2882                 case BFA_IOCPF_HWINIT:
2883                         ioc_st = BFA_IOC_HWINIT;
2884                         break;
2885
2886                 case BFA_IOCPF_FWMISMATCH:
2887                         ioc_st = BFA_IOC_FWMISMATCH;
2888                         break;
2889
2890                 case BFA_IOCPF_FAIL:
2891                         ioc_st = BFA_IOC_FAIL;
2892                         break;
2893
2894                 case BFA_IOCPF_INITFAIL:
2895                         ioc_st = BFA_IOC_INITFAIL;
2896                         break;
2897
2898                 default:
2899                         break;
2900                 }
2901         }
2902         return ioc_st;
2903 }
2904
2905 void
2906 bfa_nw_ioc_get_attr(struct bfa_ioc *ioc, struct bfa_ioc_attr *ioc_attr)
2907 {
2908         memset((void *)ioc_attr, 0, sizeof(struct bfa_ioc_attr));
2909
2910         ioc_attr->state = bfa_ioc_get_state(ioc);
2911         ioc_attr->port_id = bfa_ioc_portid(ioc);
2912         ioc_attr->port_mode = ioc->port_mode;
2913
2914         ioc_attr->port_mode_cfg = ioc->port_mode_cfg;
2915         ioc_attr->cap_bm = ioc->ad_cap_bm;
2916
2917         ioc_attr->ioc_type = bfa_ioc_get_type(ioc);
2918
2919         bfa_ioc_get_adapter_attr(ioc, &ioc_attr->adapter_attr);
2920
2921         ioc_attr->pci_attr.device_id = bfa_ioc_devid(ioc);
2922         ioc_attr->pci_attr.pcifn = bfa_ioc_pcifn(ioc);
2923         ioc_attr->def_fn = bfa_ioc_is_default(ioc);
2924         bfa_ioc_get_pci_chip_rev(ioc, ioc_attr->pci_attr.chip_rev);
2925 }
2926
2927 /* WWN public */
2928 static u64
2929 bfa_ioc_get_pwwn(struct bfa_ioc *ioc)
2930 {
2931         return ioc->attr->pwwn;
2932 }
2933
2934 void
2935 bfa_nw_ioc_get_mac(struct bfa_ioc *ioc, u8 *mac)
2936 {
2937         ether_addr_copy(mac, ioc->attr->mac);
2938 }
2939
2940 /* Firmware failure detected. Start recovery actions. */
2941 static void
2942 bfa_ioc_recover(struct bfa_ioc *ioc)
2943 {
2944         pr_crit("Heart Beat of IOC has failed\n");
2945         bfa_ioc_stats(ioc, ioc_hbfails);
2946         bfa_ioc_stats_hb_count(ioc, ioc->hb_count);
2947         bfa_fsm_send_event(ioc, IOC_E_HBFAIL);
2948 }
2949
2950 /* BFA IOC PF private functions */
2951
2952 static void
2953 bfa_iocpf_enable(struct bfa_ioc *ioc)
2954 {
2955         bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_ENABLE);
2956 }
2957
2958 static void
2959 bfa_iocpf_disable(struct bfa_ioc *ioc)
2960 {
2961         bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_DISABLE);
2962 }
2963
2964 static void
2965 bfa_iocpf_fail(struct bfa_ioc *ioc)
2966 {
2967         bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_FAIL);
2968 }
2969
2970 static void
2971 bfa_iocpf_initfail(struct bfa_ioc *ioc)
2972 {
2973         bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_INITFAIL);
2974 }
2975
2976 static void
2977 bfa_iocpf_getattrfail(struct bfa_ioc *ioc)
2978 {
2979         bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_GETATTRFAIL);
2980 }
2981
2982 static void
2983 bfa_iocpf_stop(struct bfa_ioc *ioc)
2984 {
2985         bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_STOP);
2986 }
2987
2988 void
2989 bfa_nw_iocpf_timeout(void *ioc_arg)
2990 {
2991         struct bfa_ioc  *ioc = (struct bfa_ioc *) ioc_arg;
2992         enum bfa_iocpf_state iocpf_st;
2993
2994         iocpf_st = bfa_sm_to_state(iocpf_sm_table, ioc->iocpf.fsm);
2995
2996         if (iocpf_st == BFA_IOCPF_HWINIT)
2997                 bfa_ioc_poll_fwinit(ioc);
2998         else
2999                 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_TIMEOUT);
3000 }
3001
3002 void
3003 bfa_nw_iocpf_sem_timeout(void *ioc_arg)
3004 {
3005         struct bfa_ioc  *ioc = (struct bfa_ioc *) ioc_arg;
3006
3007         bfa_ioc_hw_sem_get(ioc);
3008 }
3009
3010 static void
3011 bfa_ioc_poll_fwinit(struct bfa_ioc *ioc)
3012 {
3013         u32 fwstate = bfa_ioc_get_cur_ioc_fwstate(ioc);
3014
3015         if (fwstate == BFI_IOC_DISABLED) {
3016                 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_FWREADY);
3017                 return;
3018         }
3019
3020         if (ioc->iocpf.poll_time >= BFA_IOC_TOV) {
3021                 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_TIMEOUT);
3022         } else {
3023                 ioc->iocpf.poll_time += BFA_IOC_POLL_TOV;
3024                 mod_timer(&ioc->iocpf_timer, jiffies +
3025                         msecs_to_jiffies(BFA_IOC_POLL_TOV));
3026         }
3027 }
3028
3029 /*
3030  *      Flash module specific
3031  */
3032
3033 /*
3034  * FLASH DMA buffer should be big enough to hold both MFG block and
3035  * asic block(64k) at the same time and also should be 2k aligned to
3036  * avoid write segement to cross sector boundary.
3037  */
3038 #define BFA_FLASH_SEG_SZ        2048
3039 #define BFA_FLASH_DMA_BUF_SZ    \
3040         roundup(0x010000 + sizeof(struct bfa_mfg_block), BFA_FLASH_SEG_SZ)
3041
3042 static void
3043 bfa_flash_cb(struct bfa_flash *flash)
3044 {
3045         flash->op_busy = 0;
3046         if (flash->cbfn)
3047                 flash->cbfn(flash->cbarg, flash->status);
3048 }
3049
3050 static void
3051 bfa_flash_notify(void *cbarg, enum bfa_ioc_event event)
3052 {
3053         struct bfa_flash *flash = cbarg;
3054
3055         switch (event) {
3056         case BFA_IOC_E_DISABLED:
3057         case BFA_IOC_E_FAILED:
3058                 if (flash->op_busy) {
3059                         flash->status = BFA_STATUS_IOC_FAILURE;
3060                         flash->cbfn(flash->cbarg, flash->status);
3061                         flash->op_busy = 0;
3062                 }
3063                 break;
3064         default:
3065                 break;
3066         }
3067 }
3068
3069 /*
3070  * Send flash write request.
3071  */
3072 static void
3073 bfa_flash_write_send(struct bfa_flash *flash)
3074 {
3075         struct bfi_flash_write_req *msg =
3076                         (struct bfi_flash_write_req *) flash->mb.msg;
3077         u32     len;
3078
3079         msg->type = be32_to_cpu(flash->type);
3080         msg->instance = flash->instance;
3081         msg->offset = be32_to_cpu(flash->addr_off + flash->offset);
3082         len = (flash->residue < BFA_FLASH_DMA_BUF_SZ) ?
3083                flash->residue : BFA_FLASH_DMA_BUF_SZ;
3084         msg->length = be32_to_cpu(len);
3085
3086         /* indicate if it's the last msg of the whole write operation */
3087         msg->last = (len == flash->residue) ? 1 : 0;
3088
3089         bfi_h2i_set(msg->mh, BFI_MC_FLASH, BFI_FLASH_H2I_WRITE_REQ,
3090                     bfa_ioc_portid(flash->ioc));
3091         bfa_alen_set(&msg->alen, len, flash->dbuf_pa);
3092         memcpy(flash->dbuf_kva, flash->ubuf + flash->offset, len);
3093         bfa_nw_ioc_mbox_queue(flash->ioc, &flash->mb, NULL, NULL);
3094
3095         flash->residue -= len;
3096         flash->offset += len;
3097 }
3098
3099 /**
3100  * bfa_flash_read_send - Send flash read request.
3101  *
3102  * @cbarg: callback argument
3103  */
3104 static void
3105 bfa_flash_read_send(void *cbarg)
3106 {
3107         struct bfa_flash *flash = cbarg;
3108         struct bfi_flash_read_req *msg =
3109                         (struct bfi_flash_read_req *) flash->mb.msg;
3110         u32     len;
3111
3112         msg->type = be32_to_cpu(flash->type);
3113         msg->instance = flash->instance;
3114         msg->offset = be32_to_cpu(flash->addr_off + flash->offset);
3115         len = (flash->residue < BFA_FLASH_DMA_BUF_SZ) ?
3116                flash->residue : BFA_FLASH_DMA_BUF_SZ;
3117         msg->length = be32_to_cpu(len);
3118         bfi_h2i_set(msg->mh, BFI_MC_FLASH, BFI_FLASH_H2I_READ_REQ,
3119                     bfa_ioc_portid(flash->ioc));
3120         bfa_alen_set(&msg->alen, len, flash->dbuf_pa);
3121         bfa_nw_ioc_mbox_queue(flash->ioc, &flash->mb, NULL, NULL);
3122 }
3123
3124 /**
3125  * bfa_flash_intr - Process flash response messages upon receiving interrupts.
3126  *
3127  * @flasharg: flash structure
3128  * @msg: message structure
3129  */
3130 static void
3131 bfa_flash_intr(void *flasharg, struct bfi_mbmsg *msg)
3132 {
3133         struct bfa_flash *flash = flasharg;
3134         u32     status;
3135
3136         union {
3137                 struct bfi_flash_query_rsp *query;
3138                 struct bfi_flash_write_rsp *write;
3139                 struct bfi_flash_read_rsp *read;
3140                 struct bfi_mbmsg   *msg;
3141         } m;
3142
3143         m.msg = msg;
3144
3145         /* receiving response after ioc failure */
3146         if (!flash->op_busy && msg->mh.msg_id != BFI_FLASH_I2H_EVENT)
3147                 return;
3148
3149         switch (msg->mh.msg_id) {
3150         case BFI_FLASH_I2H_QUERY_RSP:
3151                 status = be32_to_cpu(m.query->status);
3152                 if (status == BFA_STATUS_OK) {
3153                         u32     i;
3154                         struct bfa_flash_attr *attr, *f;
3155
3156                         attr = (struct bfa_flash_attr *) flash->ubuf;
3157                         f = (struct bfa_flash_attr *) flash->dbuf_kva;
3158                         attr->status = be32_to_cpu(f->status);
3159                         attr->npart = be32_to_cpu(f->npart);
3160                         for (i = 0; i < attr->npart; i++) {
3161                                 attr->part[i].part_type =
3162                                         be32_to_cpu(f->part[i].part_type);
3163                                 attr->part[i].part_instance =
3164                                         be32_to_cpu(f->part[i].part_instance);
3165                                 attr->part[i].part_off =
3166                                         be32_to_cpu(f->part[i].part_off);
3167                                 attr->part[i].part_size =
3168                                         be32_to_cpu(f->part[i].part_size);
3169                                 attr->part[i].part_len =
3170                                         be32_to_cpu(f->part[i].part_len);
3171                                 attr->part[i].part_status =
3172                                         be32_to_cpu(f->part[i].part_status);
3173                         }
3174                 }
3175                 flash->status = status;
3176                 bfa_flash_cb(flash);
3177                 break;
3178         case BFI_FLASH_I2H_WRITE_RSP:
3179                 status = be32_to_cpu(m.write->status);
3180                 if (status != BFA_STATUS_OK || flash->residue == 0) {
3181                         flash->status = status;
3182                         bfa_flash_cb(flash);
3183                 } else
3184                         bfa_flash_write_send(flash);
3185                 break;
3186         case BFI_FLASH_I2H_READ_RSP:
3187                 status = be32_to_cpu(m.read->status);
3188                 if (status != BFA_STATUS_OK) {
3189                         flash->status = status;
3190                         bfa_flash_cb(flash);
3191                 } else {
3192                         u32 len = be32_to_cpu(m.read->length);
3193                         memcpy(flash->ubuf + flash->offset,
3194                                flash->dbuf_kva, len);
3195                         flash->residue -= len;
3196                         flash->offset += len;
3197                         if (flash->residue == 0) {
3198                                 flash->status = status;
3199                                 bfa_flash_cb(flash);
3200                         } else
3201                                 bfa_flash_read_send(flash);
3202                 }
3203                 break;
3204         case BFI_FLASH_I2H_BOOT_VER_RSP:
3205         case BFI_FLASH_I2H_EVENT:
3206                 break;
3207         default:
3208                 WARN_ON(1);
3209         }
3210 }
3211
3212 /*
3213  * Flash memory info API.
3214  */
3215 u32
3216 bfa_nw_flash_meminfo(void)
3217 {
3218         return roundup(BFA_FLASH_DMA_BUF_SZ, BFA_DMA_ALIGN_SZ);
3219 }
3220
3221 /**
3222  * bfa_nw_flash_attach - Flash attach API.
3223  *
3224  * @flash: flash structure
3225  * @ioc: ioc structure
3226  * @dev: device structure
3227  */
3228 void
3229 bfa_nw_flash_attach(struct bfa_flash *flash, struct bfa_ioc *ioc, void *dev)
3230 {
3231         flash->ioc = ioc;
3232         flash->cbfn = NULL;
3233         flash->cbarg = NULL;
3234         flash->op_busy = 0;
3235
3236         bfa_nw_ioc_mbox_regisr(flash->ioc, BFI_MC_FLASH, bfa_flash_intr, flash);
3237         bfa_ioc_notify_init(&flash->ioc_notify, bfa_flash_notify, flash);
3238         list_add_tail(&flash->ioc_notify.qe, &flash->ioc->notify_q);
3239 }
3240
3241 /**
3242  * bfa_nw_flash_memclaim - Claim memory for flash
3243  *
3244  * @flash: flash structure
3245  * @dm_kva: pointer to virtual memory address
3246  * @dm_pa: physical memory address
3247  */
3248 void
3249 bfa_nw_flash_memclaim(struct bfa_flash *flash, u8 *dm_kva, u64 dm_pa)
3250 {
3251         flash->dbuf_kva = dm_kva;
3252         flash->dbuf_pa = dm_pa;
3253         memset(flash->dbuf_kva, 0, BFA_FLASH_DMA_BUF_SZ);
3254         dm_kva += roundup(BFA_FLASH_DMA_BUF_SZ, BFA_DMA_ALIGN_SZ);
3255         dm_pa += roundup(BFA_FLASH_DMA_BUF_SZ, BFA_DMA_ALIGN_SZ);
3256 }
3257
3258 /**
3259  * bfa_nw_flash_get_attr - Get flash attribute.
3260  *
3261  * @flash: flash structure
3262  * @attr: flash attribute structure
3263  * @cbfn: callback function
3264  * @cbarg: callback argument
3265  *
3266  * Return status.
3267  */
3268 enum bfa_status
3269 bfa_nw_flash_get_attr(struct bfa_flash *flash, struct bfa_flash_attr *attr,
3270                       bfa_cb_flash cbfn, void *cbarg)
3271 {
3272         struct bfi_flash_query_req *msg =
3273                         (struct bfi_flash_query_req *) flash->mb.msg;
3274
3275         if (!bfa_nw_ioc_is_operational(flash->ioc))
3276                 return BFA_STATUS_IOC_NON_OP;
3277
3278         if (flash->op_busy)
3279                 return BFA_STATUS_DEVBUSY;
3280
3281         flash->op_busy = 1;
3282         flash->cbfn = cbfn;
3283         flash->cbarg = cbarg;
3284         flash->ubuf = (u8 *) attr;
3285
3286         bfi_h2i_set(msg->mh, BFI_MC_FLASH, BFI_FLASH_H2I_QUERY_REQ,
3287                     bfa_ioc_portid(flash->ioc));
3288         bfa_alen_set(&msg->alen, sizeof(struct bfa_flash_attr), flash->dbuf_pa);
3289         bfa_nw_ioc_mbox_queue(flash->ioc, &flash->mb, NULL, NULL);
3290
3291         return BFA_STATUS_OK;
3292 }
3293
3294 /**
3295  * bfa_nw_flash_update_part - Update flash partition.
3296  *
3297  * @flash: flash structure
3298  * @type: flash partition type
3299  * @instance: flash partition instance
3300  * @buf: update data buffer
3301  * @len: data buffer length
3302  * @offset: offset relative to the partition starting address
3303  * @cbfn: callback function
3304  * @cbarg: callback argument
3305  *
3306  * Return status.
3307  */
3308 enum bfa_status
3309 bfa_nw_flash_update_part(struct bfa_flash *flash, u32 type, u8 instance,
3310                          void *buf, u32 len, u32 offset,
3311                          bfa_cb_flash cbfn, void *cbarg)
3312 {
3313         if (!bfa_nw_ioc_is_operational(flash->ioc))
3314                 return BFA_STATUS_IOC_NON_OP;
3315
3316         /*
3317          * 'len' must be in word (4-byte) boundary
3318          */
3319         if (!len || (len & 0x03))
3320                 return BFA_STATUS_FLASH_BAD_LEN;
3321
3322         if (type == BFA_FLASH_PART_MFG)
3323                 return BFA_STATUS_EINVAL;
3324
3325         if (flash->op_busy)
3326                 return BFA_STATUS_DEVBUSY;
3327
3328         flash->op_busy = 1;
3329         flash->cbfn = cbfn;
3330         flash->cbarg = cbarg;
3331         flash->type = type;
3332         flash->instance = instance;
3333         flash->residue = len;
3334         flash->offset = 0;
3335         flash->addr_off = offset;
3336         flash->ubuf = buf;
3337
3338         bfa_flash_write_send(flash);
3339
3340         return BFA_STATUS_OK;
3341 }
3342
3343 /**
3344  * bfa_nw_flash_read_part - Read flash partition.
3345  *
3346  * @flash: flash structure
3347  * @type: flash partition type
3348  * @instance: flash partition instance
3349  * @buf: read data buffer
3350  * @len: data buffer length
3351  * @offset: offset relative to the partition starting address
3352  * @cbfn: callback function
3353  * @cbarg: callback argument
3354  *
3355  * Return status.
3356  */
3357 enum bfa_status
3358 bfa_nw_flash_read_part(struct bfa_flash *flash, u32 type, u8 instance,
3359                        void *buf, u32 len, u32 offset,
3360                        bfa_cb_flash cbfn, void *cbarg)
3361 {
3362         if (!bfa_nw_ioc_is_operational(flash->ioc))
3363                 return BFA_STATUS_IOC_NON_OP;
3364
3365         /*
3366          * 'len' must be in word (4-byte) boundary
3367          */
3368         if (!len || (len & 0x03))
3369                 return BFA_STATUS_FLASH_BAD_LEN;
3370
3371         if (flash->op_busy)
3372                 return BFA_STATUS_DEVBUSY;
3373
3374         flash->op_busy = 1;
3375         flash->cbfn = cbfn;
3376         flash->cbarg = cbarg;
3377         flash->type = type;
3378         flash->instance = instance;
3379         flash->residue = len;
3380         flash->offset = 0;
3381         flash->addr_off = offset;
3382         flash->ubuf = buf;
3383
3384         bfa_flash_read_send(flash);
3385
3386         return BFA_STATUS_OK;
3387 }