[SCSI] bfa: update to support firmware configuation
[pandora-kernel.git] / drivers / scsi / bfa / bfa_ioc.c
1 /*
2  * Copyright (c) 2005-2009 Brocade Communications Systems, Inc.
3  * All rights reserved
4  * www.brocade.com
5  *
6  * Linux driver for Brocade Fibre Channel Host Bus Adapter.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License (GPL) Version 2 as
10  * published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  */
17
18 #include <bfa.h>
19 #include <bfa_ioc.h>
20 #include <bfa_fwimg_priv.h>
21 #include <cna/bfa_cna_trcmod.h>
22 #include <cs/bfa_debug.h>
23 #include <bfi/bfi_ioc.h>
24 #include <bfi/bfi_ctreg.h>
25 #include <aen/bfa_aen_ioc.h>
26 #include <aen/bfa_aen.h>
27 #include <log/bfa_log_hal.h>
28 #include <defs/bfa_defs_pci.h>
29
30 BFA_TRC_FILE(CNA, IOC);
31
32 /**
33  * IOC local definitions
34  */
35 #define BFA_IOC_TOV             2000    /* msecs */
36 #define BFA_IOC_HWSEM_TOV       500     /* msecs */
37 #define BFA_IOC_HB_TOV          500     /* msecs */
38 #define BFA_IOC_HWINIT_MAX      2
39 #define BFA_IOC_FWIMG_MINSZ     (16 * 1024)
40 #define BFA_IOC_TOV_RECOVER      BFA_IOC_HB_TOV
41
42 #define bfa_ioc_timer_start(__ioc)                                      \
43         bfa_timer_begin((__ioc)->timer_mod, &(__ioc)->ioc_timer,        \
44                         bfa_ioc_timeout, (__ioc), BFA_IOC_TOV)
45 #define bfa_ioc_timer_stop(__ioc)   bfa_timer_stop(&(__ioc)->ioc_timer)
46
47 #define BFA_DBG_FWTRC_ENTS      (BFI_IOC_TRC_ENTS)
48 #define BFA_DBG_FWTRC_LEN                                       \
49         (BFA_DBG_FWTRC_ENTS * sizeof(struct bfa_trc_s) +        \
50          (sizeof(struct bfa_trc_mod_s) -                        \
51           BFA_TRC_MAX * sizeof(struct bfa_trc_s)))
52 #define BFA_DBG_FWTRC_OFF(_fn)  (BFI_IOC_TRC_OFF + BFA_DBG_FWTRC_LEN * (_fn))
53
54 /**
55  * Asic specific macros : see bfa_hw_cb.c and bfa_hw_ct.c for details.
56  */
57
58 #define bfa_ioc_firmware_lock(__ioc)                    \
59                         ((__ioc)->ioc_hwif->ioc_firmware_lock(__ioc))
60 #define bfa_ioc_firmware_unlock(__ioc)                  \
61                         ((__ioc)->ioc_hwif->ioc_firmware_unlock(__ioc))
62 #define bfa_ioc_reg_init(__ioc) ((__ioc)->ioc_hwif->ioc_reg_init(__ioc))
63 #define bfa_ioc_map_port(__ioc) ((__ioc)->ioc_hwif->ioc_map_port(__ioc))
64 #define bfa_ioc_notify_hbfail(__ioc)                    \
65                         ((__ioc)->ioc_hwif->ioc_notify_hbfail(__ioc))
66 #define bfa_ioc_is_optrom(__ioc)        \
67         (bfi_image_get_size(BFA_IOC_FWIMG_TYPE(__ioc)) < BFA_IOC_FWIMG_MINSZ)
68
69 bfa_boolean_t   bfa_auto_recover = BFA_TRUE;
70
71 /*
72  * forward declarations
73  */
74 static void     bfa_ioc_hw_sem_get(struct bfa_ioc_s *ioc);
75 static void     bfa_ioc_hw_sem_get_cancel(struct bfa_ioc_s *ioc);
76 static void     bfa_ioc_hwinit(struct bfa_ioc_s *ioc, bfa_boolean_t force);
77 static void     bfa_ioc_timeout(void *ioc);
78 static void     bfa_ioc_send_enable(struct bfa_ioc_s *ioc);
79 static void     bfa_ioc_send_disable(struct bfa_ioc_s *ioc);
80 static void     bfa_ioc_send_getattr(struct bfa_ioc_s *ioc);
81 static void     bfa_ioc_hb_monitor(struct bfa_ioc_s *ioc);
82 static void     bfa_ioc_hb_stop(struct bfa_ioc_s *ioc);
83 static void     bfa_ioc_reset(struct bfa_ioc_s *ioc, bfa_boolean_t force);
84 static void     bfa_ioc_mbox_poll(struct bfa_ioc_s *ioc);
85 static void     bfa_ioc_mbox_hbfail(struct bfa_ioc_s *ioc);
86 static void     bfa_ioc_recover(struct bfa_ioc_s *ioc);
87 static void     bfa_ioc_disable_comp(struct bfa_ioc_s *ioc);
88 static void     bfa_ioc_lpu_stop(struct bfa_ioc_s *ioc);
89
90 /**
91  *  bfa_ioc_sm
92  */
93
94 /**
95  * IOC state machine events
96  */
97 enum ioc_event {
98         IOC_E_ENABLE = 1,       /*  IOC enable request */
99         IOC_E_DISABLE = 2,      /*  IOC disable request */
100         IOC_E_TIMEOUT = 3,      /*  f/w response timeout */
101         IOC_E_FWREADY = 4,      /*  f/w initialization done */
102         IOC_E_FWRSP_GETATTR = 5,        /*  IOC get attribute response */
103         IOC_E_FWRSP_ENABLE = 6, /*  enable f/w response */
104         IOC_E_FWRSP_DISABLE = 7,        /*  disable f/w response */
105         IOC_E_HBFAIL = 8,       /*  heartbeat failure */
106         IOC_E_HWERROR = 9,      /*  hardware error interrupt */
107         IOC_E_SEMLOCKED = 10,   /*  h/w semaphore is locked */
108         IOC_E_DETACH = 11,      /*  driver detach cleanup */
109 };
110
111 bfa_fsm_state_decl(bfa_ioc, reset, struct bfa_ioc_s, enum ioc_event);
112 bfa_fsm_state_decl(bfa_ioc, fwcheck, struct bfa_ioc_s, enum ioc_event);
113 bfa_fsm_state_decl(bfa_ioc, mismatch, struct bfa_ioc_s, enum ioc_event);
114 bfa_fsm_state_decl(bfa_ioc, semwait, struct bfa_ioc_s, enum ioc_event);
115 bfa_fsm_state_decl(bfa_ioc, hwinit, struct bfa_ioc_s, enum ioc_event);
116 bfa_fsm_state_decl(bfa_ioc, enabling, struct bfa_ioc_s, enum ioc_event);
117 bfa_fsm_state_decl(bfa_ioc, getattr, struct bfa_ioc_s, enum ioc_event);
118 bfa_fsm_state_decl(bfa_ioc, op, struct bfa_ioc_s, enum ioc_event);
119 bfa_fsm_state_decl(bfa_ioc, initfail, struct bfa_ioc_s, enum ioc_event);
120 bfa_fsm_state_decl(bfa_ioc, hbfail, struct bfa_ioc_s, enum ioc_event);
121 bfa_fsm_state_decl(bfa_ioc, disabling, struct bfa_ioc_s, enum ioc_event);
122 bfa_fsm_state_decl(bfa_ioc, disabled, struct bfa_ioc_s, enum ioc_event);
123
124 static struct bfa_sm_table_s ioc_sm_table[] = {
125         {BFA_SM(bfa_ioc_sm_reset), BFA_IOC_RESET},
126         {BFA_SM(bfa_ioc_sm_fwcheck), BFA_IOC_FWMISMATCH},
127         {BFA_SM(bfa_ioc_sm_mismatch), BFA_IOC_FWMISMATCH},
128         {BFA_SM(bfa_ioc_sm_semwait), BFA_IOC_SEMWAIT},
129         {BFA_SM(bfa_ioc_sm_hwinit), BFA_IOC_HWINIT},
130         {BFA_SM(bfa_ioc_sm_enabling), BFA_IOC_HWINIT},
131         {BFA_SM(bfa_ioc_sm_getattr), BFA_IOC_GETATTR},
132         {BFA_SM(bfa_ioc_sm_op), BFA_IOC_OPERATIONAL},
133         {BFA_SM(bfa_ioc_sm_initfail), BFA_IOC_INITFAIL},
134         {BFA_SM(bfa_ioc_sm_hbfail), BFA_IOC_HBFAIL},
135         {BFA_SM(bfa_ioc_sm_disabling), BFA_IOC_DISABLING},
136         {BFA_SM(bfa_ioc_sm_disabled), BFA_IOC_DISABLED},
137 };
138
139 /**
140  * Reset entry actions -- initialize state machine
141  */
142 static void
143 bfa_ioc_sm_reset_entry(struct bfa_ioc_s *ioc)
144 {
145         ioc->retry_count = 0;
146         ioc->auto_recover = bfa_auto_recover;
147 }
148
149 /**
150  * Beginning state. IOC is in reset state.
151  */
152 static void
153 bfa_ioc_sm_reset(struct bfa_ioc_s *ioc, enum ioc_event event)
154 {
155         bfa_trc(ioc, event);
156
157         switch (event) {
158         case IOC_E_ENABLE:
159                 bfa_fsm_set_state(ioc, bfa_ioc_sm_fwcheck);
160                 break;
161
162         case IOC_E_DISABLE:
163                 bfa_ioc_disable_comp(ioc);
164                 break;
165
166         case IOC_E_DETACH:
167                 break;
168
169         default:
170                 bfa_sm_fault(ioc, event);
171         }
172 }
173
174 /**
175  * Semaphore should be acquired for version check.
176  */
177 static void
178 bfa_ioc_sm_fwcheck_entry(struct bfa_ioc_s *ioc)
179 {
180         bfa_ioc_hw_sem_get(ioc);
181 }
182
183 /**
184  * Awaiting h/w semaphore to continue with version check.
185  */
186 static void
187 bfa_ioc_sm_fwcheck(struct bfa_ioc_s *ioc, enum ioc_event event)
188 {
189         bfa_trc(ioc, event);
190
191         switch (event) {
192         case IOC_E_SEMLOCKED:
193                 if (bfa_ioc_firmware_lock(ioc)) {
194                         ioc->retry_count = 0;
195                         bfa_fsm_set_state(ioc, bfa_ioc_sm_hwinit);
196                 } else {
197                         bfa_ioc_hw_sem_release(ioc);
198                         bfa_fsm_set_state(ioc, bfa_ioc_sm_mismatch);
199                 }
200                 break;
201
202         case IOC_E_DISABLE:
203                 bfa_ioc_disable_comp(ioc);
204                 /*
205                  * fall through
206                  */
207
208         case IOC_E_DETACH:
209                 bfa_ioc_hw_sem_get_cancel(ioc);
210                 bfa_fsm_set_state(ioc, bfa_ioc_sm_reset);
211                 break;
212
213         case IOC_E_FWREADY:
214                 break;
215
216         default:
217                 bfa_sm_fault(ioc, event);
218         }
219 }
220
221 /**
222  * Notify enable completion callback and generate mismatch AEN.
223  */
224 static void
225 bfa_ioc_sm_mismatch_entry(struct bfa_ioc_s *ioc)
226 {
227         /**
228          * Provide enable completion callback and AEN notification only once.
229          */
230         if (ioc->retry_count == 0) {
231                 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
232                 bfa_ioc_aen_post(ioc, BFA_IOC_AEN_FWMISMATCH);
233         }
234         ioc->retry_count++;
235         bfa_ioc_timer_start(ioc);
236 }
237
238 /**
239  * Awaiting firmware version match.
240  */
241 static void
242 bfa_ioc_sm_mismatch(struct bfa_ioc_s *ioc, enum ioc_event event)
243 {
244         bfa_trc(ioc, event);
245
246         switch (event) {
247         case IOC_E_TIMEOUT:
248                 bfa_fsm_set_state(ioc, bfa_ioc_sm_fwcheck);
249                 break;
250
251         case IOC_E_DISABLE:
252                 bfa_ioc_disable_comp(ioc);
253                 /*
254                  * fall through
255                  */
256
257         case IOC_E_DETACH:
258                 bfa_ioc_timer_stop(ioc);
259                 bfa_fsm_set_state(ioc, bfa_ioc_sm_reset);
260                 break;
261
262         case IOC_E_FWREADY:
263                 break;
264
265         default:
266                 bfa_sm_fault(ioc, event);
267         }
268 }
269
270 /**
271  * Request for semaphore.
272  */
273 static void
274 bfa_ioc_sm_semwait_entry(struct bfa_ioc_s *ioc)
275 {
276         bfa_ioc_hw_sem_get(ioc);
277 }
278
279 /**
280  * Awaiting semaphore for h/w initialzation.
281  */
282 static void
283 bfa_ioc_sm_semwait(struct bfa_ioc_s *ioc, enum ioc_event event)
284 {
285         bfa_trc(ioc, event);
286
287         switch (event) {
288         case IOC_E_SEMLOCKED:
289                 ioc->retry_count = 0;
290                 bfa_fsm_set_state(ioc, bfa_ioc_sm_hwinit);
291                 break;
292
293         case IOC_E_DISABLE:
294                 bfa_ioc_hw_sem_get_cancel(ioc);
295                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
296                 break;
297
298         default:
299                 bfa_sm_fault(ioc, event);
300         }
301 }
302
303
304 static void
305 bfa_ioc_sm_hwinit_entry(struct bfa_ioc_s *ioc)
306 {
307         bfa_ioc_timer_start(ioc);
308         bfa_ioc_reset(ioc, BFA_FALSE);
309 }
310
311 /**
312  * Hardware is being initialized. Interrupts are enabled.
313  * Holding hardware semaphore lock.
314  */
315 static void
316 bfa_ioc_sm_hwinit(struct bfa_ioc_s *ioc, enum ioc_event event)
317 {
318         bfa_trc(ioc, event);
319
320         switch (event) {
321         case IOC_E_FWREADY:
322                 bfa_ioc_timer_stop(ioc);
323                 bfa_fsm_set_state(ioc, bfa_ioc_sm_enabling);
324                 break;
325
326         case IOC_E_HWERROR:
327                 bfa_ioc_timer_stop(ioc);
328                 /*
329                  * fall through
330                  */
331
332         case IOC_E_TIMEOUT:
333                 ioc->retry_count++;
334                 if (ioc->retry_count < BFA_IOC_HWINIT_MAX) {
335                         bfa_ioc_timer_start(ioc);
336                         bfa_ioc_reset(ioc, BFA_TRUE);
337                         break;
338                 }
339
340                 bfa_ioc_hw_sem_release(ioc);
341                 bfa_fsm_set_state(ioc, bfa_ioc_sm_initfail);
342                 break;
343
344         case IOC_E_DISABLE:
345                 bfa_ioc_hw_sem_release(ioc);
346                 bfa_ioc_timer_stop(ioc);
347                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
348                 break;
349
350         default:
351                 bfa_sm_fault(ioc, event);
352         }
353 }
354
355
356 static void
357 bfa_ioc_sm_enabling_entry(struct bfa_ioc_s *ioc)
358 {
359         bfa_ioc_timer_start(ioc);
360         bfa_ioc_send_enable(ioc);
361 }
362
363 /**
364  * Host IOC function is being enabled, awaiting response from firmware.
365  * Semaphore is acquired.
366  */
367 static void
368 bfa_ioc_sm_enabling(struct bfa_ioc_s *ioc, enum ioc_event event)
369 {
370         bfa_trc(ioc, event);
371
372         switch (event) {
373         case IOC_E_FWRSP_ENABLE:
374                 bfa_ioc_timer_stop(ioc);
375                 bfa_ioc_hw_sem_release(ioc);
376                 bfa_fsm_set_state(ioc, bfa_ioc_sm_getattr);
377                 break;
378
379         case IOC_E_HWERROR:
380                 bfa_ioc_timer_stop(ioc);
381                 /*
382                  * fall through
383                  */
384
385         case IOC_E_TIMEOUT:
386                 ioc->retry_count++;
387                 if (ioc->retry_count < BFA_IOC_HWINIT_MAX) {
388                         bfa_reg_write(ioc->ioc_regs.ioc_fwstate,
389                                       BFI_IOC_UNINIT);
390                         bfa_fsm_set_state(ioc, bfa_ioc_sm_hwinit);
391                         break;
392                 }
393
394                 bfa_ioc_hw_sem_release(ioc);
395                 bfa_fsm_set_state(ioc, bfa_ioc_sm_initfail);
396                 break;
397
398         case IOC_E_DISABLE:
399                 bfa_ioc_timer_stop(ioc);
400                 bfa_ioc_hw_sem_release(ioc);
401                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
402                 break;
403
404         case IOC_E_FWREADY:
405                 bfa_ioc_send_enable(ioc);
406                 break;
407
408         default:
409                 bfa_sm_fault(ioc, event);
410         }
411 }
412
413
414 static void
415 bfa_ioc_sm_getattr_entry(struct bfa_ioc_s *ioc)
416 {
417         bfa_ioc_timer_start(ioc);
418         bfa_ioc_send_getattr(ioc);
419 }
420
421 /**
422  * IOC configuration in progress. Timer is active.
423  */
424 static void
425 bfa_ioc_sm_getattr(struct bfa_ioc_s *ioc, enum ioc_event event)
426 {
427         bfa_trc(ioc, event);
428
429         switch (event) {
430         case IOC_E_FWRSP_GETATTR:
431                 bfa_ioc_timer_stop(ioc);
432                 bfa_fsm_set_state(ioc, bfa_ioc_sm_op);
433                 break;
434
435         case IOC_E_HWERROR:
436                 bfa_ioc_timer_stop(ioc);
437                 /*
438                  * fall through
439                  */
440
441         case IOC_E_TIMEOUT:
442                 bfa_fsm_set_state(ioc, bfa_ioc_sm_initfail);
443                 break;
444
445         case IOC_E_DISABLE:
446                 bfa_ioc_timer_stop(ioc);
447                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
448                 break;
449
450         default:
451                 bfa_sm_fault(ioc, event);
452         }
453 }
454
455
456 static void
457 bfa_ioc_sm_op_entry(struct bfa_ioc_s *ioc)
458 {
459         ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_OK);
460         bfa_ioc_hb_monitor(ioc);
461         bfa_ioc_aen_post(ioc, BFA_IOC_AEN_ENABLE);
462 }
463
464 static void
465 bfa_ioc_sm_op(struct bfa_ioc_s *ioc, enum ioc_event event)
466 {
467         bfa_trc(ioc, event);
468
469         switch (event) {
470         case IOC_E_ENABLE:
471                 break;
472
473         case IOC_E_DISABLE:
474                 bfa_ioc_hb_stop(ioc);
475                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
476                 break;
477
478         case IOC_E_HWERROR:
479         case IOC_E_FWREADY:
480                 /**
481                  * Hard error or IOC recovery by other function.
482                  * Treat it same as heartbeat failure.
483                  */
484                 bfa_ioc_hb_stop(ioc);
485                 /*
486                  * !!! fall through !!!
487                  */
488
489         case IOC_E_HBFAIL:
490                 bfa_fsm_set_state(ioc, bfa_ioc_sm_hbfail);
491                 break;
492
493         default:
494                 bfa_sm_fault(ioc, event);
495         }
496 }
497
498
499 static void
500 bfa_ioc_sm_disabling_entry(struct bfa_ioc_s *ioc)
501 {
502         bfa_ioc_aen_post(ioc, BFA_IOC_AEN_DISABLE);
503         bfa_ioc_timer_start(ioc);
504         bfa_ioc_send_disable(ioc);
505 }
506
507 /**
508  * IOC is being disabled
509  */
510 static void
511 bfa_ioc_sm_disabling(struct bfa_ioc_s *ioc, enum ioc_event event)
512 {
513         bfa_trc(ioc, event);
514
515         switch (event) {
516         case IOC_E_FWRSP_DISABLE:
517                 bfa_ioc_timer_stop(ioc);
518                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
519                 break;
520
521         case IOC_E_HWERROR:
522                 bfa_ioc_timer_stop(ioc);
523                 /*
524                  * !!! fall through !!!
525                  */
526
527         case IOC_E_TIMEOUT:
528                 bfa_reg_write(ioc->ioc_regs.ioc_fwstate, BFI_IOC_FAIL);
529                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
530                 break;
531
532         default:
533                 bfa_sm_fault(ioc, event);
534         }
535 }
536
537 /**
538  * IOC disable completion entry.
539  */
540 static void
541 bfa_ioc_sm_disabled_entry(struct bfa_ioc_s *ioc)
542 {
543         bfa_ioc_disable_comp(ioc);
544 }
545
546 static void
547 bfa_ioc_sm_disabled(struct bfa_ioc_s *ioc, enum ioc_event event)
548 {
549         bfa_trc(ioc, event);
550
551         switch (event) {
552         case IOC_E_ENABLE:
553                 bfa_fsm_set_state(ioc, bfa_ioc_sm_semwait);
554                 break;
555
556         case IOC_E_DISABLE:
557                 ioc->cbfn->disable_cbfn(ioc->bfa);
558                 break;
559
560         case IOC_E_FWREADY:
561                 break;
562
563         case IOC_E_DETACH:
564                 bfa_ioc_firmware_unlock(ioc);
565                 bfa_fsm_set_state(ioc, bfa_ioc_sm_reset);
566                 break;
567
568         default:
569                 bfa_sm_fault(ioc, event);
570         }
571 }
572
573
574 static void
575 bfa_ioc_sm_initfail_entry(struct bfa_ioc_s *ioc)
576 {
577         ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
578         bfa_ioc_timer_start(ioc);
579 }
580
581 /**
582  * Hardware initialization failed.
583  */
584 static void
585 bfa_ioc_sm_initfail(struct bfa_ioc_s *ioc, enum ioc_event event)
586 {
587         bfa_trc(ioc, event);
588
589         switch (event) {
590         case IOC_E_DISABLE:
591                 bfa_ioc_timer_stop(ioc);
592                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
593                 break;
594
595         case IOC_E_DETACH:
596                 bfa_ioc_timer_stop(ioc);
597                 bfa_ioc_firmware_unlock(ioc);
598                 bfa_fsm_set_state(ioc, bfa_ioc_sm_reset);
599                 break;
600
601         case IOC_E_TIMEOUT:
602                 bfa_fsm_set_state(ioc, bfa_ioc_sm_semwait);
603                 break;
604
605         default:
606                 bfa_sm_fault(ioc, event);
607         }
608 }
609
610
611 static void
612 bfa_ioc_sm_hbfail_entry(struct bfa_ioc_s *ioc)
613 {
614         struct list_head *qe;
615         struct bfa_ioc_hbfail_notify_s *notify;
616
617         /**
618          * Mark IOC as failed in hardware and stop firmware.
619          */
620         bfa_ioc_lpu_stop(ioc);
621         bfa_reg_write(ioc->ioc_regs.ioc_fwstate, BFI_IOC_FAIL);
622
623         /**
624          * Notify other functions on HB failure.
625          */
626         bfa_ioc_notify_hbfail(ioc);
627
628         /**
629          * Notify driver and common modules registered for notification.
630          */
631         ioc->cbfn->hbfail_cbfn(ioc->bfa);
632         list_for_each(qe, &ioc->hb_notify_q) {
633                 notify = (struct bfa_ioc_hbfail_notify_s *)qe;
634                 notify->cbfn(notify->cbarg);
635         }
636
637         /**
638          * Flush any queued up mailbox requests.
639          */
640         bfa_ioc_mbox_hbfail(ioc);
641         bfa_ioc_aen_post(ioc, BFA_IOC_AEN_HBFAIL);
642
643         /**
644          * Trigger auto-recovery after a delay.
645          */
646         if (ioc->auto_recover) {
647                 bfa_timer_begin(ioc->timer_mod, &ioc->ioc_timer,
648                                 bfa_ioc_timeout, ioc, BFA_IOC_TOV_RECOVER);
649         }
650 }
651
652 /**
653  * IOC heartbeat failure.
654  */
655 static void
656 bfa_ioc_sm_hbfail(struct bfa_ioc_s *ioc, enum ioc_event event)
657 {
658         bfa_trc(ioc, event);
659
660         switch (event) {
661
662         case IOC_E_ENABLE:
663                 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
664                 break;
665
666         case IOC_E_DISABLE:
667                 if (ioc->auto_recover)
668                         bfa_ioc_timer_stop(ioc);
669                 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
670                 break;
671
672         case IOC_E_TIMEOUT:
673                 bfa_fsm_set_state(ioc, bfa_ioc_sm_semwait);
674                 break;
675
676         case IOC_E_FWREADY:
677                 /**
678                  * Recovery is already initiated by other function.
679                  */
680                 break;
681
682         case IOC_E_HWERROR:
683                 /*
684                  * HB failure notification, ignore.
685                  */
686                 break;
687
688         default:
689                 bfa_sm_fault(ioc, event);
690         }
691 }
692
693
694
695 /**
696  *  bfa_ioc_pvt BFA IOC private functions
697  */
698
699 static void
700 bfa_ioc_disable_comp(struct bfa_ioc_s *ioc)
701 {
702         struct list_head *qe;
703         struct bfa_ioc_hbfail_notify_s *notify;
704
705         ioc->cbfn->disable_cbfn(ioc->bfa);
706
707         /**
708          * Notify common modules registered for notification.
709          */
710         list_for_each(qe, &ioc->hb_notify_q) {
711                 notify = (struct bfa_ioc_hbfail_notify_s *)qe;
712                 notify->cbfn(notify->cbarg);
713         }
714 }
715
716 void
717 bfa_ioc_sem_timeout(void *ioc_arg)
718 {
719         struct bfa_ioc_s *ioc = (struct bfa_ioc_s *)ioc_arg;
720
721         bfa_ioc_hw_sem_get(ioc);
722 }
723
724 bfa_boolean_t
725 bfa_ioc_sem_get(bfa_os_addr_t sem_reg)
726 {
727         u32 r32;
728         int cnt = 0;
729 #define BFA_SEM_SPINCNT 3000
730
731         r32 = bfa_reg_read(sem_reg);
732
733         while (r32 && (cnt < BFA_SEM_SPINCNT)) {
734                 cnt++;
735                 bfa_os_udelay(2);
736                 r32 = bfa_reg_read(sem_reg);
737         }
738
739         if (r32 == 0)
740                 return BFA_TRUE;
741
742         bfa_assert(cnt < BFA_SEM_SPINCNT);
743         return BFA_FALSE;
744 }
745
746 void
747 bfa_ioc_sem_release(bfa_os_addr_t sem_reg)
748 {
749         bfa_reg_write(sem_reg, 1);
750 }
751
752 static void
753 bfa_ioc_hw_sem_get(struct bfa_ioc_s *ioc)
754 {
755         u32        r32;
756
757         /**
758          * First read to the semaphore register will return 0, subsequent reads
759          * will return 1. Semaphore is released by writing 1 to the register
760          */
761         r32 = bfa_reg_read(ioc->ioc_regs.ioc_sem_reg);
762         if (r32 == 0) {
763                 bfa_fsm_send_event(ioc, IOC_E_SEMLOCKED);
764                 return;
765         }
766
767         bfa_timer_begin(ioc->timer_mod, &ioc->sem_timer, bfa_ioc_sem_timeout,
768                         ioc, BFA_IOC_HWSEM_TOV);
769 }
770
771 void
772 bfa_ioc_hw_sem_release(struct bfa_ioc_s *ioc)
773 {
774         bfa_reg_write(ioc->ioc_regs.ioc_sem_reg, 1);
775 }
776
777 static void
778 bfa_ioc_hw_sem_get_cancel(struct bfa_ioc_s *ioc)
779 {
780         bfa_timer_stop(&ioc->sem_timer);
781 }
782
783 /**
784  * Initialize LPU local memory (aka secondary memory / SRAM)
785  */
786 static void
787 bfa_ioc_lmem_init(struct bfa_ioc_s *ioc)
788 {
789         u32        pss_ctl;
790         int             i;
791 #define PSS_LMEM_INIT_TIME  10000
792
793         pss_ctl = bfa_reg_read(ioc->ioc_regs.pss_ctl_reg);
794         pss_ctl &= ~__PSS_LMEM_RESET;
795         pss_ctl |= __PSS_LMEM_INIT_EN;
796         pss_ctl |= __PSS_I2C_CLK_DIV(3UL); /* i2c workaround 12.5khz clock */
797         bfa_reg_write(ioc->ioc_regs.pss_ctl_reg, pss_ctl);
798
799         /**
800          * wait for memory initialization to be complete
801          */
802         i = 0;
803         do {
804                 pss_ctl = bfa_reg_read(ioc->ioc_regs.pss_ctl_reg);
805                 i++;
806         } while (!(pss_ctl & __PSS_LMEM_INIT_DONE) && (i < PSS_LMEM_INIT_TIME));
807
808         /**
809          * If memory initialization is not successful, IOC timeout will catch
810          * such failures.
811          */
812         bfa_assert(pss_ctl & __PSS_LMEM_INIT_DONE);
813         bfa_trc(ioc, pss_ctl);
814
815         pss_ctl &= ~(__PSS_LMEM_INIT_DONE | __PSS_LMEM_INIT_EN);
816         bfa_reg_write(ioc->ioc_regs.pss_ctl_reg, pss_ctl);
817 }
818
819 static void
820 bfa_ioc_lpu_start(struct bfa_ioc_s *ioc)
821 {
822         u32        pss_ctl;
823
824         /**
825          * Take processor out of reset.
826          */
827         pss_ctl = bfa_reg_read(ioc->ioc_regs.pss_ctl_reg);
828         pss_ctl &= ~__PSS_LPU0_RESET;
829
830         bfa_reg_write(ioc->ioc_regs.pss_ctl_reg, pss_ctl);
831 }
832
833 static void
834 bfa_ioc_lpu_stop(struct bfa_ioc_s *ioc)
835 {
836         u32        pss_ctl;
837
838         /**
839          * Put processors in reset.
840          */
841         pss_ctl = bfa_reg_read(ioc->ioc_regs.pss_ctl_reg);
842         pss_ctl |= (__PSS_LPU0_RESET | __PSS_LPU1_RESET);
843
844         bfa_reg_write(ioc->ioc_regs.pss_ctl_reg, pss_ctl);
845 }
846
847 /**
848  * Get driver and firmware versions.
849  */
850 void
851 bfa_ioc_fwver_get(struct bfa_ioc_s *ioc, struct bfi_ioc_image_hdr_s *fwhdr)
852 {
853         u32        pgnum, pgoff;
854         u32        loff = 0;
855         int             i;
856         u32       *fwsig = (u32 *) fwhdr;
857
858         pgnum = bfa_ioc_smem_pgnum(ioc, loff);
859         pgoff = bfa_ioc_smem_pgoff(ioc, loff);
860         bfa_reg_write(ioc->ioc_regs.host_page_num_fn, pgnum);
861
862         for (i = 0; i < (sizeof(struct bfi_ioc_image_hdr_s) / sizeof(u32));
863              i++) {
864                 fwsig[i] = bfa_mem_read(ioc->ioc_regs.smem_page_start, loff);
865                 loff += sizeof(u32);
866         }
867 }
868
869 /**
870  * Returns TRUE if same.
871  */
872 bfa_boolean_t
873 bfa_ioc_fwver_cmp(struct bfa_ioc_s *ioc, struct bfi_ioc_image_hdr_s *fwhdr)
874 {
875         struct bfi_ioc_image_hdr_s *drv_fwhdr;
876         int             i;
877
878         drv_fwhdr = (struct bfi_ioc_image_hdr_s *)
879                         bfi_image_get_chunk(BFA_IOC_FWIMG_TYPE(ioc), 0);
880
881         for (i = 0; i < BFI_IOC_MD5SUM_SZ; i++) {
882                 if (fwhdr->md5sum[i] != drv_fwhdr->md5sum[i]) {
883                         bfa_trc(ioc, i);
884                         bfa_trc(ioc, fwhdr->md5sum[i]);
885                         bfa_trc(ioc, drv_fwhdr->md5sum[i]);
886                         return BFA_FALSE;
887                 }
888         }
889
890         bfa_trc(ioc, fwhdr->md5sum[0]);
891         return BFA_TRUE;
892 }
893
894 /**
895  * Return true if current running version is valid. Firmware signature and
896  * execution context (driver/bios) must match.
897  */
898 static          bfa_boolean_t
899 bfa_ioc_fwver_valid(struct bfa_ioc_s *ioc)
900 {
901         struct bfi_ioc_image_hdr_s fwhdr, *drv_fwhdr;
902
903         /**
904          * If bios/efi boot (flash based) -- return true
905          */
906         if (bfa_ioc_is_optrom(ioc))
907                 return BFA_TRUE;
908
909         bfa_ioc_fwver_get(ioc, &fwhdr);
910         drv_fwhdr = (struct bfi_ioc_image_hdr_s *)
911                         bfi_image_get_chunk(BFA_IOC_FWIMG_TYPE(ioc), 0);
912
913
914         if (fwhdr.signature != drv_fwhdr->signature) {
915                 bfa_trc(ioc, fwhdr.signature);
916                 bfa_trc(ioc, drv_fwhdr->signature);
917                 return BFA_FALSE;
918         }
919
920         if (fwhdr.exec != drv_fwhdr->exec) {
921                 bfa_trc(ioc, fwhdr.exec);
922                 bfa_trc(ioc, drv_fwhdr->exec);
923                 return BFA_FALSE;
924         }
925
926         return bfa_ioc_fwver_cmp(ioc, &fwhdr);
927 }
928
929 /**
930  * Conditionally flush any pending message from firmware at start.
931  */
932 static void
933 bfa_ioc_msgflush(struct bfa_ioc_s *ioc)
934 {
935         u32        r32;
936
937         r32 = bfa_reg_read(ioc->ioc_regs.lpu_mbox_cmd);
938         if (r32)
939                 bfa_reg_write(ioc->ioc_regs.lpu_mbox_cmd, 1);
940 }
941
942
943 static void
944 bfa_ioc_hwinit(struct bfa_ioc_s *ioc, bfa_boolean_t force)
945 {
946         enum bfi_ioc_state ioc_fwstate;
947         bfa_boolean_t   fwvalid;
948
949         ioc_fwstate = bfa_reg_read(ioc->ioc_regs.ioc_fwstate);
950
951         if (force)
952                 ioc_fwstate = BFI_IOC_UNINIT;
953
954         bfa_trc(ioc, ioc_fwstate);
955
956         /**
957          * check if firmware is valid
958          */
959         fwvalid = (ioc_fwstate == BFI_IOC_UNINIT) ?
960                         BFA_FALSE : bfa_ioc_fwver_valid(ioc);
961
962         if (!fwvalid) {
963                 bfa_ioc_boot(ioc, BFI_BOOT_TYPE_NORMAL, ioc->pcidev.device_id);
964                 return;
965         }
966
967         /**
968          * If hardware initialization is in progress (initialized by other IOC),
969          * just wait for an initialization completion interrupt.
970          */
971         if (ioc_fwstate == BFI_IOC_INITING) {
972                 bfa_trc(ioc, ioc_fwstate);
973                 ioc->cbfn->reset_cbfn(ioc->bfa);
974                 return;
975         }
976
977         /**
978          * If IOC function is disabled and firmware version is same,
979          * just re-enable IOC.
980          */
981         if (ioc_fwstate == BFI_IOC_DISABLED || ioc_fwstate == BFI_IOC_OP) {
982                 bfa_trc(ioc, ioc_fwstate);
983
984                 /**
985                  * When using MSI-X any pending firmware ready event should
986                  * be flushed. Otherwise MSI-X interrupts are not delivered.
987                  */
988                 bfa_ioc_msgflush(ioc);
989                 ioc->cbfn->reset_cbfn(ioc->bfa);
990                 bfa_fsm_send_event(ioc, IOC_E_FWREADY);
991                 return;
992         }
993
994         /**
995          * Initialize the h/w for any other states.
996          */
997         bfa_ioc_boot(ioc, BFI_BOOT_TYPE_NORMAL, ioc->pcidev.device_id);
998 }
999
1000 static void
1001 bfa_ioc_timeout(void *ioc_arg)
1002 {
1003         struct bfa_ioc_s *ioc = (struct bfa_ioc_s *)ioc_arg;
1004
1005         bfa_trc(ioc, 0);
1006         bfa_fsm_send_event(ioc, IOC_E_TIMEOUT);
1007 }
1008
1009 void
1010 bfa_ioc_mbox_send(struct bfa_ioc_s *ioc, void *ioc_msg, int len)
1011 {
1012         u32       *msgp = (u32 *) ioc_msg;
1013         u32        i;
1014
1015         bfa_trc(ioc, msgp[0]);
1016         bfa_trc(ioc, len);
1017
1018         bfa_assert(len <= BFI_IOC_MSGLEN_MAX);
1019
1020         /*
1021          * first write msg to mailbox registers
1022          */
1023         for (i = 0; i < len / sizeof(u32); i++)
1024                 bfa_reg_write(ioc->ioc_regs.hfn_mbox + i * sizeof(u32),
1025                               bfa_os_wtole(msgp[i]));
1026
1027         for (; i < BFI_IOC_MSGLEN_MAX / sizeof(u32); i++)
1028                 bfa_reg_write(ioc->ioc_regs.hfn_mbox + i * sizeof(u32), 0);
1029
1030         /*
1031          * write 1 to mailbox CMD to trigger LPU event
1032          */
1033         bfa_reg_write(ioc->ioc_regs.hfn_mbox_cmd, 1);
1034         (void)bfa_reg_read(ioc->ioc_regs.hfn_mbox_cmd);
1035 }
1036
1037 static void
1038 bfa_ioc_send_enable(struct bfa_ioc_s *ioc)
1039 {
1040         struct bfi_ioc_ctrl_req_s enable_req;
1041
1042         bfi_h2i_set(enable_req.mh, BFI_MC_IOC, BFI_IOC_H2I_ENABLE_REQ,
1043                     bfa_ioc_portid(ioc));
1044         enable_req.ioc_class = ioc->ioc_mc;
1045         bfa_ioc_mbox_send(ioc, &enable_req, sizeof(struct bfi_ioc_ctrl_req_s));
1046 }
1047
1048 static void
1049 bfa_ioc_send_disable(struct bfa_ioc_s *ioc)
1050 {
1051         struct bfi_ioc_ctrl_req_s disable_req;
1052
1053         bfi_h2i_set(disable_req.mh, BFI_MC_IOC, BFI_IOC_H2I_DISABLE_REQ,
1054                     bfa_ioc_portid(ioc));
1055         bfa_ioc_mbox_send(ioc, &disable_req, sizeof(struct bfi_ioc_ctrl_req_s));
1056 }
1057
1058 static void
1059 bfa_ioc_send_getattr(struct bfa_ioc_s *ioc)
1060 {
1061         struct bfi_ioc_getattr_req_s attr_req;
1062
1063         bfi_h2i_set(attr_req.mh, BFI_MC_IOC, BFI_IOC_H2I_GETATTR_REQ,
1064                     bfa_ioc_portid(ioc));
1065         bfa_dma_be_addr_set(attr_req.attr_addr, ioc->attr_dma.pa);
1066         bfa_ioc_mbox_send(ioc, &attr_req, sizeof(attr_req));
1067 }
1068
1069 static void
1070 bfa_ioc_hb_check(void *cbarg)
1071 {
1072         struct bfa_ioc_s  *ioc = cbarg;
1073         u32     hb_count;
1074
1075         hb_count = bfa_reg_read(ioc->ioc_regs.heartbeat);
1076         if (ioc->hb_count == hb_count) {
1077                 bfa_log(ioc->logm, BFA_LOG_HAL_HEARTBEAT_FAILURE,
1078                         hb_count);
1079                 bfa_ioc_recover(ioc);
1080                 return;
1081         } else {
1082                 ioc->hb_count = hb_count;
1083         }
1084
1085         bfa_ioc_mbox_poll(ioc);
1086         bfa_timer_begin(ioc->timer_mod, &ioc->ioc_timer, bfa_ioc_hb_check,
1087                         ioc, BFA_IOC_HB_TOV);
1088 }
1089
1090 static void
1091 bfa_ioc_hb_monitor(struct bfa_ioc_s *ioc)
1092 {
1093         ioc->hb_count = bfa_reg_read(ioc->ioc_regs.heartbeat);
1094         bfa_timer_begin(ioc->timer_mod, &ioc->ioc_timer, bfa_ioc_hb_check, ioc,
1095                         BFA_IOC_HB_TOV);
1096 }
1097
1098 static void
1099 bfa_ioc_hb_stop(struct bfa_ioc_s *ioc)
1100 {
1101         bfa_timer_stop(&ioc->ioc_timer);
1102 }
1103
1104 /**
1105  *      Initiate a full firmware download.
1106  */
1107 static void
1108 bfa_ioc_download_fw(struct bfa_ioc_s *ioc, u32 boot_type,
1109                     u32 boot_param)
1110 {
1111         u32       *fwimg;
1112         u32        pgnum, pgoff;
1113         u32        loff = 0;
1114         u32        chunkno = 0;
1115         u32        i;
1116
1117         /**
1118          * Initialize LMEM first before code download
1119          */
1120         bfa_ioc_lmem_init(ioc);
1121
1122         /**
1123          * Flash based firmware boot
1124          */
1125         bfa_trc(ioc, bfi_image_get_size(BFA_IOC_FWIMG_TYPE(ioc)));
1126         if (bfa_ioc_is_optrom(ioc))
1127                 boot_type = BFI_BOOT_TYPE_FLASH;
1128         fwimg = bfi_image_get_chunk(BFA_IOC_FWIMG_TYPE(ioc), chunkno);
1129
1130
1131         pgnum = bfa_ioc_smem_pgnum(ioc, loff);
1132         pgoff = bfa_ioc_smem_pgoff(ioc, loff);
1133
1134         bfa_reg_write(ioc->ioc_regs.host_page_num_fn, pgnum);
1135
1136         for (i = 0; i < bfi_image_get_size(BFA_IOC_FWIMG_TYPE(ioc)); i++) {
1137
1138                 if (BFA_IOC_FLASH_CHUNK_NO(i) != chunkno) {
1139                         chunkno = BFA_IOC_FLASH_CHUNK_NO(i);
1140                         fwimg = bfi_image_get_chunk(BFA_IOC_FWIMG_TYPE(ioc),
1141                                         BFA_IOC_FLASH_CHUNK_ADDR(chunkno));
1142                 }
1143
1144                 /**
1145                  * write smem
1146                  */
1147                 bfa_mem_write(ioc->ioc_regs.smem_page_start, loff,
1148                               fwimg[BFA_IOC_FLASH_OFFSET_IN_CHUNK(i)]);
1149
1150                 loff += sizeof(u32);
1151
1152                 /**
1153                  * handle page offset wrap around
1154                  */
1155                 loff = PSS_SMEM_PGOFF(loff);
1156                 if (loff == 0) {
1157                         pgnum++;
1158                         bfa_reg_write(ioc->ioc_regs.host_page_num_fn, pgnum);
1159                 }
1160         }
1161
1162         bfa_reg_write(ioc->ioc_regs.host_page_num_fn,
1163                       bfa_ioc_smem_pgnum(ioc, 0));
1164
1165         /*
1166          * Set boot type and boot param at the end.
1167          */
1168         bfa_mem_write(ioc->ioc_regs.smem_page_start, BFI_BOOT_TYPE_OFF,
1169                         bfa_os_swap32(boot_type));
1170         bfa_mem_write(ioc->ioc_regs.smem_page_start, BFI_BOOT_PARAM_OFF,
1171                         bfa_os_swap32(boot_param));
1172 }
1173
1174 static void
1175 bfa_ioc_reset(struct bfa_ioc_s *ioc, bfa_boolean_t force)
1176 {
1177         bfa_ioc_hwinit(ioc, force);
1178 }
1179
1180 /**
1181  * Update BFA configuration from firmware configuration.
1182  */
1183 static void
1184 bfa_ioc_getattr_reply(struct bfa_ioc_s *ioc)
1185 {
1186         struct bfi_ioc_attr_s *attr = ioc->attr;
1187
1188         attr->adapter_prop = bfa_os_ntohl(attr->adapter_prop);
1189         attr->card_type     = bfa_os_ntohl(attr->card_type);
1190         attr->maxfrsize = bfa_os_ntohs(attr->maxfrsize);
1191
1192         bfa_fsm_send_event(ioc, IOC_E_FWRSP_GETATTR);
1193 }
1194
1195 /**
1196  * Attach time initialization of mbox logic.
1197  */
1198 static void
1199 bfa_ioc_mbox_attach(struct bfa_ioc_s *ioc)
1200 {
1201         struct bfa_ioc_mbox_mod_s *mod = &ioc->mbox_mod;
1202         int             mc;
1203
1204         INIT_LIST_HEAD(&mod->cmd_q);
1205         for (mc = 0; mc < BFI_MC_MAX; mc++) {
1206                 mod->mbhdlr[mc].cbfn = NULL;
1207                 mod->mbhdlr[mc].cbarg = ioc->bfa;
1208         }
1209 }
1210
1211 /**
1212  * Mbox poll timer -- restarts any pending mailbox requests.
1213  */
1214 static void
1215 bfa_ioc_mbox_poll(struct bfa_ioc_s *ioc)
1216 {
1217         struct bfa_ioc_mbox_mod_s *mod = &ioc->mbox_mod;
1218         struct bfa_mbox_cmd_s *cmd;
1219         u32        stat;
1220
1221         /**
1222          * If no command pending, do nothing
1223          */
1224         if (list_empty(&mod->cmd_q))
1225                 return;
1226
1227         /**
1228          * If previous command is not yet fetched by firmware, do nothing
1229          */
1230         stat = bfa_reg_read(ioc->ioc_regs.hfn_mbox_cmd);
1231         if (stat)
1232                 return;
1233
1234         /**
1235          * Enqueue command to firmware.
1236          */
1237         bfa_q_deq(&mod->cmd_q, &cmd);
1238         bfa_ioc_mbox_send(ioc, cmd->msg, sizeof(cmd->msg));
1239 }
1240
1241 /**
1242  * Cleanup any pending requests.
1243  */
1244 static void
1245 bfa_ioc_mbox_hbfail(struct bfa_ioc_s *ioc)
1246 {
1247         struct bfa_ioc_mbox_mod_s *mod = &ioc->mbox_mod;
1248         struct bfa_mbox_cmd_s *cmd;
1249
1250         while (!list_empty(&mod->cmd_q))
1251                 bfa_q_deq(&mod->cmd_q, &cmd);
1252 }
1253
1254 /**
1255  *  bfa_ioc_public
1256  */
1257
1258 /**
1259  * Interface used by diag module to do firmware boot with memory test
1260  * as the entry vector.
1261  */
1262 void
1263 bfa_ioc_boot(struct bfa_ioc_s *ioc, u32 boot_type, u32 boot_param)
1264 {
1265         bfa_os_addr_t   rb;
1266
1267         bfa_ioc_stats(ioc, ioc_boots);
1268
1269         if (bfa_ioc_pll_init(ioc) != BFA_STATUS_OK)
1270                 return;
1271
1272         /**
1273          * Initialize IOC state of all functions on a chip reset.
1274          */
1275         rb = ioc->pcidev.pci_bar_kva;
1276         if (boot_param == BFI_BOOT_TYPE_MEMTEST) {
1277                 bfa_reg_write((rb + BFA_IOC0_STATE_REG), BFI_IOC_MEMTEST);
1278                 bfa_reg_write((rb + BFA_IOC1_STATE_REG), BFI_IOC_MEMTEST);
1279         } else {
1280                 bfa_reg_write((rb + BFA_IOC0_STATE_REG), BFI_IOC_INITING);
1281                 bfa_reg_write((rb + BFA_IOC1_STATE_REG), BFI_IOC_INITING);
1282         }
1283
1284         bfa_ioc_download_fw(ioc, boot_type, boot_param);
1285
1286         /**
1287          * Enable interrupts just before starting LPU
1288          */
1289         ioc->cbfn->reset_cbfn(ioc->bfa);
1290         bfa_ioc_lpu_start(ioc);
1291 }
1292
1293 /**
1294  * Enable/disable IOC failure auto recovery.
1295  */
1296 void
1297 bfa_ioc_auto_recover(bfa_boolean_t auto_recover)
1298 {
1299         bfa_auto_recover = auto_recover;
1300 }
1301
1302
1303 bfa_boolean_t
1304 bfa_ioc_is_operational(struct bfa_ioc_s *ioc)
1305 {
1306         return bfa_fsm_cmp_state(ioc, bfa_ioc_sm_op);
1307 }
1308
1309 void
1310 bfa_ioc_msgget(struct bfa_ioc_s *ioc, void *mbmsg)
1311 {
1312         u32       *msgp = mbmsg;
1313         u32        r32;
1314         int             i;
1315
1316         /**
1317          * read the MBOX msg
1318          */
1319         for (i = 0; i < (sizeof(union bfi_ioc_i2h_msg_u) / sizeof(u32));
1320              i++) {
1321                 r32 = bfa_reg_read(ioc->ioc_regs.lpu_mbox +
1322                                    i * sizeof(u32));
1323                 msgp[i] = bfa_os_htonl(r32);
1324         }
1325
1326         /**
1327          * turn off mailbox interrupt by clearing mailbox status
1328          */
1329         bfa_reg_write(ioc->ioc_regs.lpu_mbox_cmd, 1);
1330         bfa_reg_read(ioc->ioc_regs.lpu_mbox_cmd);
1331 }
1332
1333 void
1334 bfa_ioc_isr(struct bfa_ioc_s *ioc, struct bfi_mbmsg_s *m)
1335 {
1336         union bfi_ioc_i2h_msg_u *msg;
1337
1338         msg = (union bfi_ioc_i2h_msg_u *)m;
1339
1340         bfa_ioc_stats(ioc, ioc_isrs);
1341
1342         switch (msg->mh.msg_id) {
1343         case BFI_IOC_I2H_HBEAT:
1344                 break;
1345
1346         case BFI_IOC_I2H_READY_EVENT:
1347                 bfa_fsm_send_event(ioc, IOC_E_FWREADY);
1348                 break;
1349
1350         case BFI_IOC_I2H_ENABLE_REPLY:
1351                 bfa_fsm_send_event(ioc, IOC_E_FWRSP_ENABLE);
1352                 break;
1353
1354         case BFI_IOC_I2H_DISABLE_REPLY:
1355                 bfa_fsm_send_event(ioc, IOC_E_FWRSP_DISABLE);
1356                 break;
1357
1358         case BFI_IOC_I2H_GETATTR_REPLY:
1359                 bfa_ioc_getattr_reply(ioc);
1360                 break;
1361
1362         default:
1363                 bfa_trc(ioc, msg->mh.msg_id);
1364                 bfa_assert(0);
1365         }
1366 }
1367
1368 /**
1369  * IOC attach time initialization and setup.
1370  *
1371  * @param[in]   ioc     memory for IOC
1372  * @param[in]   bfa     driver instance structure
1373  * @param[in]   trcmod  kernel trace module
1374  * @param[in]   aen     kernel aen event module
1375  * @param[in]   logm    kernel logging module
1376  */
1377 void
1378 bfa_ioc_attach(struct bfa_ioc_s *ioc, void *bfa, struct bfa_ioc_cbfn_s *cbfn,
1379                struct bfa_timer_mod_s *timer_mod, struct bfa_trc_mod_s *trcmod,
1380                struct bfa_aen_s *aen, struct bfa_log_mod_s *logm)
1381 {
1382         ioc->bfa = bfa;
1383         ioc->cbfn = cbfn;
1384         ioc->timer_mod = timer_mod;
1385         ioc->trcmod = trcmod;
1386         ioc->aen = aen;
1387         ioc->logm = logm;
1388         ioc->fcmode = BFA_FALSE;
1389         ioc->pllinit = BFA_FALSE;
1390         ioc->dbg_fwsave_once = BFA_TRUE;
1391
1392         bfa_ioc_mbox_attach(ioc);
1393         INIT_LIST_HEAD(&ioc->hb_notify_q);
1394
1395         bfa_fsm_set_state(ioc, bfa_ioc_sm_reset);
1396 }
1397
1398 /**
1399  * Driver detach time IOC cleanup.
1400  */
1401 void
1402 bfa_ioc_detach(struct bfa_ioc_s *ioc)
1403 {
1404         bfa_fsm_send_event(ioc, IOC_E_DETACH);
1405 }
1406
1407 /**
1408  * Setup IOC PCI properties.
1409  *
1410  * @param[in]   pcidev  PCI device information for this IOC
1411  */
1412 void
1413 bfa_ioc_pci_init(struct bfa_ioc_s *ioc, struct bfa_pcidev_s *pcidev,
1414                  enum bfi_mclass mc)
1415 {
1416         ioc->ioc_mc = mc;
1417         ioc->pcidev = *pcidev;
1418         ioc->ctdev  = bfa_asic_id_ct(ioc->pcidev.device_id);
1419         ioc->cna = ioc->ctdev && !ioc->fcmode;
1420
1421         /**
1422          * Set asic specific interfaces. See bfa_ioc_cb.c and bfa_ioc_ct.c
1423          */
1424         if (ioc->ctdev)
1425                 bfa_ioc_set_ct_hwif(ioc);
1426         else
1427                 bfa_ioc_set_cb_hwif(ioc);
1428
1429         bfa_ioc_map_port(ioc);
1430         bfa_ioc_reg_init(ioc);
1431 }
1432
1433 /**
1434  * Initialize IOC dma memory
1435  *
1436  * @param[in]   dm_kva  kernel virtual address of IOC dma memory
1437  * @param[in]   dm_pa   physical address of IOC dma memory
1438  */
1439 void
1440 bfa_ioc_mem_claim(struct bfa_ioc_s *ioc, u8 *dm_kva, u64 dm_pa)
1441 {
1442         /**
1443          * dma memory for firmware attribute
1444          */
1445         ioc->attr_dma.kva = dm_kva;
1446         ioc->attr_dma.pa = dm_pa;
1447         ioc->attr = (struct bfi_ioc_attr_s *)dm_kva;
1448 }
1449
1450 /**
1451  * Return size of dma memory required.
1452  */
1453 u32
1454 bfa_ioc_meminfo(void)
1455 {
1456         return BFA_ROUNDUP(sizeof(struct bfi_ioc_attr_s), BFA_DMA_ALIGN_SZ);
1457 }
1458
1459 void
1460 bfa_ioc_enable(struct bfa_ioc_s *ioc)
1461 {
1462         bfa_ioc_stats(ioc, ioc_enables);
1463         ioc->dbg_fwsave_once = BFA_TRUE;
1464
1465         bfa_fsm_send_event(ioc, IOC_E_ENABLE);
1466 }
1467
1468 void
1469 bfa_ioc_disable(struct bfa_ioc_s *ioc)
1470 {
1471         bfa_ioc_stats(ioc, ioc_disables);
1472         bfa_fsm_send_event(ioc, IOC_E_DISABLE);
1473 }
1474
1475 /**
1476  * Returns memory required for saving firmware trace in case of crash.
1477  * Driver must call this interface to allocate memory required for
1478  * automatic saving of firmware trace. Driver should call
1479  * bfa_ioc_debug_memclaim() right after bfa_ioc_attach() to setup this
1480  * trace memory.
1481  */
1482 int
1483 bfa_ioc_debug_trcsz(bfa_boolean_t auto_recover)
1484 {
1485 return (auto_recover) ? BFA_DBG_FWTRC_LEN : 0;
1486 }
1487
1488 /**
1489  * Initialize memory for saving firmware trace. Driver must initialize
1490  * trace memory before call bfa_ioc_enable().
1491  */
1492 void
1493 bfa_ioc_debug_memclaim(struct bfa_ioc_s *ioc, void *dbg_fwsave)
1494 {
1495         ioc->dbg_fwsave = dbg_fwsave;
1496         ioc->dbg_fwsave_len = bfa_ioc_debug_trcsz(ioc->auto_recover);
1497 }
1498
1499 u32
1500 bfa_ioc_smem_pgnum(struct bfa_ioc_s *ioc, u32 fmaddr)
1501 {
1502         return PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, fmaddr);
1503 }
1504
1505 u32
1506 bfa_ioc_smem_pgoff(struct bfa_ioc_s *ioc, u32 fmaddr)
1507 {
1508         return PSS_SMEM_PGOFF(fmaddr);
1509 }
1510
1511 /**
1512  * Register mailbox message handler functions
1513  *
1514  * @param[in]   ioc             IOC instance
1515  * @param[in]   mcfuncs         message class handler functions
1516  */
1517 void
1518 bfa_ioc_mbox_register(struct bfa_ioc_s *ioc, bfa_ioc_mbox_mcfunc_t *mcfuncs)
1519 {
1520         struct bfa_ioc_mbox_mod_s *mod = &ioc->mbox_mod;
1521         int             mc;
1522
1523         for (mc = 0; mc < BFI_MC_MAX; mc++)
1524                 mod->mbhdlr[mc].cbfn = mcfuncs[mc];
1525 }
1526
1527 /**
1528  * Register mailbox message handler function, to be called by common modules
1529  */
1530 void
1531 bfa_ioc_mbox_regisr(struct bfa_ioc_s *ioc, enum bfi_mclass mc,
1532                     bfa_ioc_mbox_mcfunc_t cbfn, void *cbarg)
1533 {
1534         struct bfa_ioc_mbox_mod_s *mod = &ioc->mbox_mod;
1535
1536         mod->mbhdlr[mc].cbfn = cbfn;
1537         mod->mbhdlr[mc].cbarg = cbarg;
1538 }
1539
1540 /**
1541  * Queue a mailbox command request to firmware. Waits if mailbox is busy.
1542  * Responsibility of caller to serialize
1543  *
1544  * @param[in]   ioc     IOC instance
1545  * @param[i]    cmd     Mailbox command
1546  */
1547 void
1548 bfa_ioc_mbox_queue(struct bfa_ioc_s *ioc, struct bfa_mbox_cmd_s *cmd)
1549 {
1550         struct bfa_ioc_mbox_mod_s *mod = &ioc->mbox_mod;
1551         u32        stat;
1552
1553         /**
1554          * If a previous command is pending, queue new command
1555          */
1556         if (!list_empty(&mod->cmd_q)) {
1557                 list_add_tail(&cmd->qe, &mod->cmd_q);
1558                 return;
1559         }
1560
1561         /**
1562          * If mailbox is busy, queue command for poll timer
1563          */
1564         stat = bfa_reg_read(ioc->ioc_regs.hfn_mbox_cmd);
1565         if (stat) {
1566                 list_add_tail(&cmd->qe, &mod->cmd_q);
1567                 return;
1568         }
1569
1570         /**
1571          * mailbox is free -- queue command to firmware
1572          */
1573         bfa_ioc_mbox_send(ioc, cmd->msg, sizeof(cmd->msg));
1574 }
1575
1576 /**
1577  * Handle mailbox interrupts
1578  */
1579 void
1580 bfa_ioc_mbox_isr(struct bfa_ioc_s *ioc)
1581 {
1582         struct bfa_ioc_mbox_mod_s *mod = &ioc->mbox_mod;
1583         struct bfi_mbmsg_s m;
1584         int             mc;
1585
1586         bfa_ioc_msgget(ioc, &m);
1587
1588         /**
1589          * Treat IOC message class as special.
1590          */
1591         mc = m.mh.msg_class;
1592         if (mc == BFI_MC_IOC) {
1593                 bfa_ioc_isr(ioc, &m);
1594                 return;
1595         }
1596
1597         if ((mc > BFI_MC_MAX) || (mod->mbhdlr[mc].cbfn == NULL))
1598                 return;
1599
1600         mod->mbhdlr[mc].cbfn(mod->mbhdlr[mc].cbarg, &m);
1601 }
1602
1603 void
1604 bfa_ioc_error_isr(struct bfa_ioc_s *ioc)
1605 {
1606         bfa_fsm_send_event(ioc, IOC_E_HWERROR);
1607 }
1608
1609 void
1610 bfa_ioc_set_fcmode(struct bfa_ioc_s *ioc)
1611 {
1612         ioc->fcmode  = BFA_TRUE;
1613         ioc->port_id = bfa_ioc_pcifn(ioc);
1614 }
1615
1616 #ifndef BFA_BIOS_BUILD
1617
1618 /**
1619  * return true if IOC is disabled
1620  */
1621 bfa_boolean_t
1622 bfa_ioc_is_disabled(struct bfa_ioc_s *ioc)
1623 {
1624         return bfa_fsm_cmp_state(ioc, bfa_ioc_sm_disabling)
1625                 || bfa_fsm_cmp_state(ioc, bfa_ioc_sm_disabled);
1626 }
1627
1628 /**
1629  * return true if IOC firmware is different.
1630  */
1631 bfa_boolean_t
1632 bfa_ioc_fw_mismatch(struct bfa_ioc_s *ioc)
1633 {
1634         return bfa_fsm_cmp_state(ioc, bfa_ioc_sm_reset)
1635                 || bfa_fsm_cmp_state(ioc, bfa_ioc_sm_fwcheck)
1636                 || bfa_fsm_cmp_state(ioc, bfa_ioc_sm_mismatch);
1637 }
1638
1639 #define bfa_ioc_state_disabled(__sm)            \
1640         (((__sm) == BFI_IOC_UNINIT) ||          \
1641          ((__sm) == BFI_IOC_INITING) ||         \
1642          ((__sm) == BFI_IOC_HWINIT) ||          \
1643          ((__sm) == BFI_IOC_DISABLED) ||        \
1644          ((__sm) == BFI_IOC_FAIL) ||            \
1645          ((__sm) == BFI_IOC_CFG_DISABLED))
1646
1647 /**
1648  * Check if adapter is disabled -- both IOCs should be in a disabled
1649  * state.
1650  */
1651 bfa_boolean_t
1652 bfa_ioc_adapter_is_disabled(struct bfa_ioc_s *ioc)
1653 {
1654         u32        ioc_state;
1655         bfa_os_addr_t   rb = ioc->pcidev.pci_bar_kva;
1656
1657         if (!bfa_fsm_cmp_state(ioc, bfa_ioc_sm_disabled))
1658                 return BFA_FALSE;
1659
1660         ioc_state = bfa_reg_read(rb + BFA_IOC0_STATE_REG);
1661         if (!bfa_ioc_state_disabled(ioc_state))
1662                 return BFA_FALSE;
1663
1664         ioc_state = bfa_reg_read(rb + BFA_IOC1_STATE_REG);
1665         if (!bfa_ioc_state_disabled(ioc_state))
1666                 return BFA_FALSE;
1667
1668         return BFA_TRUE;
1669 }
1670
1671 /**
1672  * Add to IOC heartbeat failure notification queue. To be used by common
1673  * modules such as
1674  */
1675 void
1676 bfa_ioc_hbfail_register(struct bfa_ioc_s *ioc,
1677                         struct bfa_ioc_hbfail_notify_s *notify)
1678 {
1679         list_add_tail(&notify->qe, &ioc->hb_notify_q);
1680 }
1681
1682 #define BFA_MFG_NAME "Brocade"
1683 void
1684 bfa_ioc_get_adapter_attr(struct bfa_ioc_s *ioc,
1685                          struct bfa_adapter_attr_s *ad_attr)
1686 {
1687         struct bfi_ioc_attr_s *ioc_attr;
1688
1689         ioc_attr = ioc->attr;
1690
1691         bfa_ioc_get_adapter_serial_num(ioc, ad_attr->serial_num);
1692         bfa_ioc_get_adapter_fw_ver(ioc, ad_attr->fw_ver);
1693         bfa_ioc_get_adapter_optrom_ver(ioc, ad_attr->optrom_ver);
1694         bfa_ioc_get_adapter_manufacturer(ioc, ad_attr->manufacturer);
1695         bfa_os_memcpy(&ad_attr->vpd, &ioc_attr->vpd,
1696                       sizeof(struct bfa_mfg_vpd_s));
1697
1698         ad_attr->nports = bfa_ioc_get_nports(ioc);
1699         ad_attr->max_speed = bfa_ioc_speed_sup(ioc);
1700
1701         bfa_ioc_get_adapter_model(ioc, ad_attr->model);
1702         /* For now, model descr uses same model string */
1703         bfa_ioc_get_adapter_model(ioc, ad_attr->model_descr);
1704
1705         ad_attr->card_type = ioc_attr->card_type;
1706         ad_attr->is_mezz = bfa_mfg_is_mezz(ioc_attr->card_type);
1707
1708         if (BFI_ADAPTER_IS_SPECIAL(ioc_attr->adapter_prop))
1709                 ad_attr->prototype = 1;
1710         else
1711                 ad_attr->prototype = 0;
1712
1713         ad_attr->pwwn = bfa_ioc_get_pwwn(ioc);
1714         ad_attr->mac = bfa_ioc_get_mac(ioc);
1715
1716         ad_attr->pcie_gen = ioc_attr->pcie_gen;
1717         ad_attr->pcie_lanes = ioc_attr->pcie_lanes;
1718         ad_attr->pcie_lanes_orig = ioc_attr->pcie_lanes_orig;
1719         ad_attr->asic_rev = ioc_attr->asic_rev;
1720
1721         bfa_ioc_get_pci_chip_rev(ioc, ad_attr->hw_ver);
1722
1723         ad_attr->cna_capable = ioc->cna;
1724 }
1725
1726 enum bfa_ioc_type_e
1727 bfa_ioc_get_type(struct bfa_ioc_s *ioc)
1728 {
1729         if (!ioc->ctdev || ioc->fcmode)
1730                 return BFA_IOC_TYPE_FC;
1731         else if (ioc->ioc_mc == BFI_MC_IOCFC)
1732                 return BFA_IOC_TYPE_FCoE;
1733         else if (ioc->ioc_mc == BFI_MC_LL)
1734                 return BFA_IOC_TYPE_LL;
1735         else {
1736                 bfa_assert(ioc->ioc_mc == BFI_MC_LL);
1737                 return BFA_IOC_TYPE_LL;
1738         }
1739 }
1740
1741 void
1742 bfa_ioc_get_adapter_serial_num(struct bfa_ioc_s *ioc, char *serial_num)
1743 {
1744         bfa_os_memset((void *)serial_num, 0, BFA_ADAPTER_SERIAL_NUM_LEN);
1745         bfa_os_memcpy((void *)serial_num,
1746                         (void *)ioc->attr->brcd_serialnum,
1747                         BFA_ADAPTER_SERIAL_NUM_LEN);
1748 }
1749
1750 void
1751 bfa_ioc_get_adapter_fw_ver(struct bfa_ioc_s *ioc, char *fw_ver)
1752 {
1753         bfa_os_memset((void *)fw_ver, 0, BFA_VERSION_LEN);
1754         bfa_os_memcpy(fw_ver, ioc->attr->fw_version, BFA_VERSION_LEN);
1755 }
1756
1757 void
1758 bfa_ioc_get_pci_chip_rev(struct bfa_ioc_s *ioc, char *chip_rev)
1759 {
1760         bfa_assert(chip_rev);
1761
1762         bfa_os_memset((void *)chip_rev, 0, BFA_IOC_CHIP_REV_LEN);
1763
1764         chip_rev[0] = 'R';
1765         chip_rev[1] = 'e';
1766         chip_rev[2] = 'v';
1767         chip_rev[3] = '-';
1768         chip_rev[4] = ioc->attr->asic_rev;
1769         chip_rev[5] = '\0';
1770 }
1771
1772 void
1773 bfa_ioc_get_adapter_optrom_ver(struct bfa_ioc_s *ioc, char *optrom_ver)
1774 {
1775         bfa_os_memset((void *)optrom_ver, 0, BFA_VERSION_LEN);
1776         bfa_os_memcpy(optrom_ver, ioc->attr->optrom_version,
1777                 BFA_VERSION_LEN);
1778 }
1779
1780 void
1781 bfa_ioc_get_adapter_manufacturer(struct bfa_ioc_s *ioc, char *manufacturer)
1782 {
1783         bfa_os_memset((void *)manufacturer, 0, BFA_ADAPTER_MFG_NAME_LEN);
1784         bfa_os_memcpy(manufacturer, BFA_MFG_NAME, BFA_ADAPTER_MFG_NAME_LEN);
1785 }
1786
1787 void
1788 bfa_ioc_get_adapter_model(struct bfa_ioc_s *ioc, char *model)
1789 {
1790         struct bfi_ioc_attr_s   *ioc_attr;
1791         u8              nports;
1792         u8              max_speed;
1793
1794         bfa_assert(model);
1795         bfa_os_memset((void *)model, 0, BFA_ADAPTER_MODEL_NAME_LEN);
1796
1797         ioc_attr = ioc->attr;
1798
1799         nports = bfa_ioc_get_nports(ioc);
1800         max_speed = bfa_ioc_speed_sup(ioc);
1801
1802         /**
1803          * model name
1804          */
1805         if (max_speed == 10) {
1806                 strcpy(model, "BR-10?0");
1807                 model[5] = '0' + nports;
1808         } else {
1809                 strcpy(model, "Brocade-??5");
1810                 model[8] = '0' + max_speed;
1811                 model[9] = '0' + nports;
1812         }
1813 }
1814
1815 enum bfa_ioc_state
1816 bfa_ioc_get_state(struct bfa_ioc_s *ioc)
1817 {
1818         return bfa_sm_to_state(ioc_sm_table, ioc->fsm);
1819 }
1820
1821 void
1822 bfa_ioc_get_attr(struct bfa_ioc_s *ioc, struct bfa_ioc_attr_s *ioc_attr)
1823 {
1824         bfa_os_memset((void *)ioc_attr, 0, sizeof(struct bfa_ioc_attr_s));
1825
1826         ioc_attr->state = bfa_ioc_get_state(ioc);
1827         ioc_attr->port_id = ioc->port_id;
1828
1829         ioc_attr->ioc_type = bfa_ioc_get_type(ioc);
1830
1831         bfa_ioc_get_adapter_attr(ioc, &ioc_attr->adapter_attr);
1832
1833         ioc_attr->pci_attr.device_id = ioc->pcidev.device_id;
1834         ioc_attr->pci_attr.pcifn = ioc->pcidev.pci_func;
1835         bfa_ioc_get_pci_chip_rev(ioc, ioc_attr->pci_attr.chip_rev);
1836 }
1837
1838 /**
1839  *  bfa_wwn_public
1840  */
1841 wwn_t
1842 bfa_ioc_get_pwwn(struct bfa_ioc_s *ioc)
1843 {
1844         return ioc->attr->pwwn;
1845 }
1846
1847 wwn_t
1848 bfa_ioc_get_nwwn(struct bfa_ioc_s *ioc)
1849 {
1850         return ioc->attr->nwwn;
1851 }
1852
1853 u64
1854 bfa_ioc_get_adid(struct bfa_ioc_s *ioc)
1855 {
1856         return ioc->attr->mfg_pwwn;
1857 }
1858
1859 mac_t
1860 bfa_ioc_get_mac(struct bfa_ioc_s *ioc)
1861 {
1862         /*
1863          * Currently mfg mac is used as FCoE enode mac (not configured by PBC)
1864          */
1865         if (bfa_ioc_get_type(ioc) == BFA_IOC_TYPE_FCoE)
1866                 return bfa_ioc_get_mfg_mac(ioc);
1867         else
1868                 return ioc->attr->mac;
1869 }
1870
1871 wwn_t
1872 bfa_ioc_get_mfg_pwwn(struct bfa_ioc_s *ioc)
1873 {
1874         return ioc->attr->mfg_pwwn;
1875 }
1876
1877 wwn_t
1878 bfa_ioc_get_mfg_nwwn(struct bfa_ioc_s *ioc)
1879 {
1880         return ioc->attr->mfg_nwwn;
1881 }
1882
1883 mac_t
1884 bfa_ioc_get_mfg_mac(struct bfa_ioc_s *ioc)
1885 {
1886         mac_t   mac;
1887
1888         mac = ioc->attr->mfg_mac;
1889         mac.mac[MAC_ADDRLEN - 1] += bfa_ioc_pcifn(ioc);
1890
1891         return mac;
1892 }
1893
1894 bfa_boolean_t
1895 bfa_ioc_get_fcmode(struct bfa_ioc_s *ioc)
1896 {
1897         return ioc->fcmode || !bfa_asic_id_ct(ioc->pcidev.device_id);
1898 }
1899
1900 /**
1901  * Send AEN notification
1902  */
1903 void
1904 bfa_ioc_aen_post(struct bfa_ioc_s *ioc, enum bfa_ioc_aen_event event)
1905 {
1906         union bfa_aen_data_u aen_data;
1907         struct bfa_log_mod_s *logmod = ioc->logm;
1908         s32         inst_num = 0;
1909         enum bfa_ioc_type_e ioc_type;
1910
1911         bfa_log(logmod, BFA_LOG_CREATE_ID(BFA_AEN_CAT_IOC, event), inst_num);
1912
1913         memset(&aen_data.ioc.pwwn, 0, sizeof(aen_data.ioc.pwwn));
1914         memset(&aen_data.ioc.mac, 0, sizeof(aen_data.ioc.mac));
1915         ioc_type = bfa_ioc_get_type(ioc);
1916         switch (ioc_type) {
1917         case BFA_IOC_TYPE_FC:
1918                 aen_data.ioc.pwwn = bfa_ioc_get_pwwn(ioc);
1919                 break;
1920         case BFA_IOC_TYPE_FCoE:
1921                 aen_data.ioc.pwwn = bfa_ioc_get_pwwn(ioc);
1922                 aen_data.ioc.mac = bfa_ioc_get_mac(ioc);
1923                 break;
1924         case BFA_IOC_TYPE_LL:
1925                 aen_data.ioc.mac = bfa_ioc_get_mac(ioc);
1926                 break;
1927         default:
1928                 bfa_assert(ioc_type == BFA_IOC_TYPE_FC);
1929                 break;
1930         }
1931         aen_data.ioc.ioc_type = ioc_type;
1932 }
1933
1934 /**
1935  * Retrieve saved firmware trace from a prior IOC failure.
1936  */
1937 bfa_status_t
1938 bfa_ioc_debug_fwsave(struct bfa_ioc_s *ioc, void *trcdata, int *trclen)
1939 {
1940         int             tlen;
1941
1942         if (ioc->dbg_fwsave_len == 0)
1943                 return BFA_STATUS_ENOFSAVE;
1944
1945         tlen = *trclen;
1946         if (tlen > ioc->dbg_fwsave_len)
1947                 tlen = ioc->dbg_fwsave_len;
1948
1949         bfa_os_memcpy(trcdata, ioc->dbg_fwsave, tlen);
1950         *trclen = tlen;
1951         return BFA_STATUS_OK;
1952 }
1953
1954 /**
1955  * Clear saved firmware trace
1956  */
1957 void
1958 bfa_ioc_debug_fwsave_clear(struct bfa_ioc_s *ioc)
1959 {
1960         ioc->dbg_fwsave_once = BFA_TRUE;
1961 }
1962
1963 /**
1964  * Retrieve saved firmware trace from a prior IOC failure.
1965  */
1966 bfa_status_t
1967 bfa_ioc_debug_fwtrc(struct bfa_ioc_s *ioc, void *trcdata, int *trclen)
1968 {
1969         u32        pgnum;
1970         u32        loff = BFA_DBG_FWTRC_OFF(bfa_ioc_portid(ioc));
1971         int             i, tlen;
1972         u32       *tbuf = trcdata, r32;
1973
1974         bfa_trc(ioc, *trclen);
1975
1976         pgnum = bfa_ioc_smem_pgnum(ioc, loff);
1977         loff = bfa_ioc_smem_pgoff(ioc, loff);
1978
1979         /*
1980          *  Hold semaphore to serialize pll init and fwtrc.
1981          */
1982         if (BFA_FALSE == bfa_ioc_sem_get(ioc->ioc_regs.ioc_init_sem_reg))
1983                 return BFA_STATUS_FAILED;
1984
1985         bfa_reg_write(ioc->ioc_regs.host_page_num_fn, pgnum);
1986
1987         tlen = *trclen;
1988         if (tlen > BFA_DBG_FWTRC_LEN)
1989                 tlen = BFA_DBG_FWTRC_LEN;
1990         tlen /= sizeof(u32);
1991
1992         bfa_trc(ioc, tlen);
1993
1994         for (i = 0; i < tlen; i++) {
1995                 r32 = bfa_mem_read(ioc->ioc_regs.smem_page_start, loff);
1996                 tbuf[i] = bfa_os_ntohl(r32);
1997                 loff += sizeof(u32);
1998
1999                 /**
2000                  * handle page offset wrap around
2001                  */
2002                 loff = PSS_SMEM_PGOFF(loff);
2003                 if (loff == 0) {
2004                         pgnum++;
2005                         bfa_reg_write(ioc->ioc_regs.host_page_num_fn, pgnum);
2006                 }
2007         }
2008         bfa_reg_write(ioc->ioc_regs.host_page_num_fn,
2009                       bfa_ioc_smem_pgnum(ioc, 0));
2010
2011         /*
2012          *  release semaphore.
2013          */
2014         bfa_ioc_sem_release(ioc->ioc_regs.ioc_init_sem_reg);
2015
2016         bfa_trc(ioc, pgnum);
2017
2018         *trclen = tlen * sizeof(u32);
2019         return BFA_STATUS_OK;
2020 }
2021
2022 /**
2023  * Save firmware trace if configured.
2024  */
2025 static void
2026 bfa_ioc_debug_save(struct bfa_ioc_s *ioc)
2027 {
2028         int             tlen;
2029
2030         if (ioc->dbg_fwsave_len) {
2031                 tlen = ioc->dbg_fwsave_len;
2032                 bfa_ioc_debug_fwtrc(ioc, ioc->dbg_fwsave, &tlen);
2033         }
2034 }
2035
2036 /**
2037  * Firmware failure detected. Start recovery actions.
2038  */
2039 static void
2040 bfa_ioc_recover(struct bfa_ioc_s *ioc)
2041 {
2042         if (ioc->dbg_fwsave_once) {
2043                 ioc->dbg_fwsave_once = BFA_FALSE;
2044                 bfa_ioc_debug_save(ioc);
2045         }
2046
2047         bfa_ioc_stats(ioc, ioc_hbfails);
2048         bfa_fsm_send_event(ioc, IOC_E_HBFAIL);
2049 }
2050
2051 #else
2052
2053 void
2054 bfa_ioc_aen_post(struct bfa_ioc_s *ioc, enum bfa_ioc_aen_event event)
2055 {
2056 }
2057
2058 static void
2059 bfa_ioc_recover(struct bfa_ioc_s *ioc)
2060 {
2061         bfa_assert(0);
2062 }
2063
2064 #endif
2065
2066