blob: b7a0f7879de2d3ee4d2b419a3b26c5e7661224c6 [file] [log] [blame]
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001/*
Rasesh Mody2732ba52015-02-19 16:02:31 -05002 * Linux network driver for QLogic BR-series Converged Network Adapter.
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003 *
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/*
Rasesh Mody2732ba52015-02-19 16:02:31 -050014 * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
15 * Copyright (c) 2014-2015 QLogic Corporation
Rasesh Mody8b230ed2010-08-23 20:24:12 -070016 * All rights reserved
Rasesh Mody2732ba52015-02-19 16:02:31 -050017 * www.qlogic.com
Rasesh Mody8b230ed2010-08-23 20:24:12 -070018 */
19
20#include "bfa_ioc.h"
Rasesh Modya9602492011-08-02 12:36:06 +000021#include "bfi_reg.h"
Rasesh Mody8b230ed2010-08-23 20:24:12 -070022#include "bfa_defs.h"
23
Ben Hutchings1aa8b472012-07-10 10:56:59 +000024/* IOC local definitions */
Rasesh Mody8b230ed2010-08-23 20:24:12 -070025
Ben Hutchings1aa8b472012-07-10 10:56:59 +000026/* Asic specific macros : see bfa_hw_cb.c and bfa_hw_ct.c for details. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -070027
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))
Rasesh Mody1d32f762010-12-23 21:45:09 +000034#define bfa_ioc_notify_fail(__ioc) \
35 ((__ioc)->ioc_hwif->ioc_notify_fail(__ioc))
Rasesh Mody79ea6c82011-04-14 08:05:18 +000036#define bfa_ioc_sync_start(__ioc) \
37 ((__ioc)->ioc_hwif->ioc_sync_start(__ioc))
Rasesh Mody1d32f762010-12-23 21:45:09 +000038#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))
Rasesh Mody41ed9032013-12-17 17:07:32 -080046#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))
Rasesh Mody8b230ed2010-08-23 20:24:12 -070052
Rasesh Modyb7ee31c52010-10-05 15:46:05 +000053static bool bfa_nw_auto_recover = true;
Rasesh Mody8b230ed2010-08-23 20:24:12 -070054
55/*
56 * forward declarations
57 */
Rasesh Modyd4e16d42011-07-22 08:07:47 +000058static void bfa_ioc_hw_sem_init(struct bfa_ioc *ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -070059static void bfa_ioc_hw_sem_get(struct bfa_ioc *ioc);
60static void bfa_ioc_hw_sem_get_cancel(struct bfa_ioc *ioc);
61static void bfa_ioc_hwinit(struct bfa_ioc *ioc, bool force);
Rasesh Mody078086f2011-08-08 16:21:39 +000062static void bfa_ioc_poll_fwinit(struct bfa_ioc *ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -070063static void bfa_ioc_send_enable(struct bfa_ioc *ioc);
64static void bfa_ioc_send_disable(struct bfa_ioc *ioc);
65static void bfa_ioc_send_getattr(struct bfa_ioc *ioc);
66static void bfa_ioc_hb_monitor(struct bfa_ioc *ioc);
67static void bfa_ioc_hb_stop(struct bfa_ioc *ioc);
68static void bfa_ioc_reset(struct bfa_ioc *ioc, bool force);
69static void bfa_ioc_mbox_poll(struct bfa_ioc *ioc);
Rasesh Modyfdad4002011-07-22 08:07:45 +000070static void bfa_ioc_mbox_flush(struct bfa_ioc *ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -070071static void bfa_ioc_recover(struct bfa_ioc *ioc);
Rasesh Modybd5a92e2011-07-22 08:07:42 +000072static void bfa_ioc_event_notify(struct bfa_ioc *, enum bfa_ioc_event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -070073static void bfa_ioc_disable_comp(struct bfa_ioc *ioc);
74static void bfa_ioc_lpu_stop(struct bfa_ioc *ioc);
Krishna Gudipati7afc5db2011-12-22 13:30:19 +000075static void bfa_nw_ioc_debug_save_ftrc(struct bfa_ioc *ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +000076static void bfa_ioc_fail_notify(struct bfa_ioc *ioc);
77static void bfa_ioc_pf_enabled(struct bfa_ioc *ioc);
78static void bfa_ioc_pf_disabled(struct bfa_ioc *ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +000079static void bfa_ioc_pf_failed(struct bfa_ioc *ioc);
Rasesh Mody078086f2011-08-08 16:21:39 +000080static void bfa_ioc_pf_hwfailed(struct bfa_ioc *ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +000081static void bfa_ioc_pf_fwmismatch(struct bfa_ioc *ioc);
Rasesh Modyc107ba12013-12-17 17:07:41 -080082static enum bfa_status bfa_ioc_boot(struct bfa_ioc *ioc,
83 enum bfi_fwboot_type boot_type, u32 boot_param);
Rasesh Mody8a891422010-08-25 23:00:27 -070084static u32 bfa_ioc_smem_pgnum(struct bfa_ioc *ioc, u32 fmaddr);
Rasesh Mody8a891422010-08-25 23:00:27 -070085static void bfa_ioc_get_adapter_serial_num(struct bfa_ioc *ioc,
86 char *serial_num);
87static void bfa_ioc_get_adapter_fw_ver(struct bfa_ioc *ioc,
88 char *fw_ver);
89static void bfa_ioc_get_pci_chip_rev(struct bfa_ioc *ioc,
90 char *chip_rev);
91static void bfa_ioc_get_adapter_optrom_ver(struct bfa_ioc *ioc,
92 char *optrom_ver);
93static void bfa_ioc_get_adapter_manufacturer(struct bfa_ioc *ioc,
94 char *manufacturer);
95static void bfa_ioc_get_adapter_model(struct bfa_ioc *ioc, char *model);
96static u64 bfa_ioc_get_pwwn(struct bfa_ioc *ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -070097
Ben Hutchings1aa8b472012-07-10 10:56:59 +000098/* IOC state machine definitions/declarations */
Rasesh Mody8b230ed2010-08-23 20:24:12 -070099enum ioc_event {
Rasesh Mody1d32f762010-12-23 21:45:09 +0000100 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 */
Rasesh Mody078086f2011-08-08 16:21:39 +0000107 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 */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700112};
113
Rasesh Mody1d32f762010-12-23 21:45:09 +0000114bfa_fsm_state_decl(bfa_ioc, uninit, struct bfa_ioc, enum ioc_event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700115bfa_fsm_state_decl(bfa_ioc, reset, struct bfa_ioc, enum ioc_event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700116bfa_fsm_state_decl(bfa_ioc, enabling, struct bfa_ioc, enum ioc_event);
117bfa_fsm_state_decl(bfa_ioc, getattr, struct bfa_ioc, enum ioc_event);
118bfa_fsm_state_decl(bfa_ioc, op, struct bfa_ioc, enum ioc_event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000119bfa_fsm_state_decl(bfa_ioc, fail_retry, struct bfa_ioc, enum ioc_event);
120bfa_fsm_state_decl(bfa_ioc, fail, struct bfa_ioc, enum ioc_event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700121bfa_fsm_state_decl(bfa_ioc, disabling, struct bfa_ioc, enum ioc_event);
122bfa_fsm_state_decl(bfa_ioc, disabled, struct bfa_ioc, enum ioc_event);
Rasesh Mody078086f2011-08-08 16:21:39 +0000123bfa_fsm_state_decl(bfa_ioc, hwfail, struct bfa_ioc, enum ioc_event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700124
125static struct bfa_sm_table ioc_sm_table[] = {
Rasesh Mody1d32f762010-12-23 21:45:09 +0000126 {BFA_SM(bfa_ioc_sm_uninit), BFA_IOC_UNINIT},
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700127 {BFA_SM(bfa_ioc_sm_reset), BFA_IOC_RESET},
Rasesh Mody1d32f762010-12-23 21:45:09 +0000128 {BFA_SM(bfa_ioc_sm_enabling), BFA_IOC_ENABLING},
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700129 {BFA_SM(bfa_ioc_sm_getattr), BFA_IOC_GETATTR},
130 {BFA_SM(bfa_ioc_sm_op), BFA_IOC_OPERATIONAL},
Rasesh Mody1d32f762010-12-23 21:45:09 +0000131 {BFA_SM(bfa_ioc_sm_fail_retry), BFA_IOC_INITFAIL},
132 {BFA_SM(bfa_ioc_sm_fail), BFA_IOC_FAIL},
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700133 {BFA_SM(bfa_ioc_sm_disabling), BFA_IOC_DISABLING},
134 {BFA_SM(bfa_ioc_sm_disabled), BFA_IOC_DISABLED},
Rasesh Mody078086f2011-08-08 16:21:39 +0000135 {BFA_SM(bfa_ioc_sm_hwfail), BFA_IOC_HWFAIL},
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700136};
137
Rasesh Mody1d32f762010-12-23 21:45:09 +0000138/*
139 * Forward declareations for iocpf state machine
140 */
141static void bfa_iocpf_enable(struct bfa_ioc *ioc);
142static void bfa_iocpf_disable(struct bfa_ioc *ioc);
143static void bfa_iocpf_fail(struct bfa_ioc *ioc);
144static void bfa_iocpf_initfail(struct bfa_ioc *ioc);
145static void bfa_iocpf_getattrfail(struct bfa_ioc *ioc);
146static void bfa_iocpf_stop(struct bfa_ioc *ioc);
147
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000148/* IOCPF state machine events */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000149enum 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 */
Rasesh Mody0120b992011-07-22 08:07:41 +0000153 IOCPF_E_FWREADY = 4, /*!< f/w initialization done */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000154 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 */
Rasesh Mody078086f2011-08-08 16:21:39 +0000161 IOCPF_E_SEM_ERROR = 12, /*!< h/w sem mapping error */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000162};
163
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000164/* IOCPF states */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000165enum 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
177bfa_fsm_state_decl(bfa_iocpf, reset, struct bfa_iocpf, enum iocpf_event);
178bfa_fsm_state_decl(bfa_iocpf, fwcheck, struct bfa_iocpf, enum iocpf_event);
179bfa_fsm_state_decl(bfa_iocpf, mismatch, struct bfa_iocpf, enum iocpf_event);
180bfa_fsm_state_decl(bfa_iocpf, semwait, struct bfa_iocpf, enum iocpf_event);
181bfa_fsm_state_decl(bfa_iocpf, hwinit, struct bfa_iocpf, enum iocpf_event);
182bfa_fsm_state_decl(bfa_iocpf, enabling, struct bfa_iocpf, enum iocpf_event);
183bfa_fsm_state_decl(bfa_iocpf, ready, struct bfa_iocpf, enum iocpf_event);
184bfa_fsm_state_decl(bfa_iocpf, initfail_sync, struct bfa_iocpf,
185 enum iocpf_event);
186bfa_fsm_state_decl(bfa_iocpf, initfail, struct bfa_iocpf, enum iocpf_event);
187bfa_fsm_state_decl(bfa_iocpf, fail_sync, struct bfa_iocpf, enum iocpf_event);
188bfa_fsm_state_decl(bfa_iocpf, fail, struct bfa_iocpf, enum iocpf_event);
189bfa_fsm_state_decl(bfa_iocpf, disabling, struct bfa_iocpf, enum iocpf_event);
190bfa_fsm_state_decl(bfa_iocpf, disabling_sync, struct bfa_iocpf,
191 enum iocpf_event);
192bfa_fsm_state_decl(bfa_iocpf, disabled, struct bfa_iocpf, enum iocpf_event);
193
194static 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
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000211/* IOC State Machine */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000212
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000213/* Beginning state. IOC uninit state. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000214static void
215bfa_ioc_sm_uninit_entry(struct bfa_ioc *ioc)
216{
217}
218
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000219/* IOC is in uninit state. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000220static void
221bfa_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:
Rasesh Modyac51f602011-07-22 08:07:43 +0000229 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000230 }
231}
232
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000233/* Reset entry actions -- initialize state machine */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700234static void
235bfa_ioc_sm_reset_entry(struct bfa_ioc *ioc)
236{
Rasesh Mody1d32f762010-12-23 21:45:09 +0000237 bfa_fsm_set_state(&ioc->iocpf, bfa_iocpf_sm_reset);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700238}
239
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000240/* IOC is in reset state. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700241static void
242bfa_ioc_sm_reset(struct bfa_ioc *ioc, enum ioc_event event)
243{
244 switch (event) {
245 case IOC_E_ENABLE:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000246 bfa_fsm_set_state(ioc, bfa_ioc_sm_enabling);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700247 break;
248
249 case IOC_E_DISABLE:
250 bfa_ioc_disable_comp(ioc);
251 break;
252
253 case IOC_E_DETACH:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000254 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
255 break;
256
257 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000258 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000259 }
260}
261
262static void
263bfa_ioc_sm_enabling_entry(struct bfa_ioc *ioc)
264{
265 bfa_iocpf_enable(ioc);
266}
267
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000268/* Host IOC function is being enabled, awaiting response from firmware.
Rasesh Mody1d32f762010-12-23 21:45:09 +0000269 * Semaphore is acquired.
270 */
271static void
272bfa_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
Rasesh Modyf374b362011-07-22 08:07:46 +0000279 case IOC_E_PFFAILED:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000280 /* !!! fall through !!! */
281 case IOC_E_HWERROR:
282 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
Rasesh Mody078086f2011-08-08 16:21:39 +0000283 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
Rasesh Modyf374b362011-07-22 08:07:46 +0000284 if (event != IOC_E_PFFAILED)
Rasesh Mody1d32f762010-12-23 21:45:09 +0000285 bfa_iocpf_initfail(ioc);
286 break;
287
Rasesh Mody078086f2011-08-08 16:21:39 +0000288 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
Rasesh Mody1d32f762010-12-23 21:45:09 +0000293 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:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700303 break;
304
305 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000306 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700307 }
308}
309
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000310/* Semaphore should be acquired for version check. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700311static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700312bfa_ioc_sm_getattr_entry(struct bfa_ioc *ioc)
313{
Rasesh Mody1d32f762010-12-23 21:45:09 +0000314 mod_timer(&ioc->ioc_timer, jiffies +
315 msecs_to_jiffies(BFA_IOC_TOV));
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700316 bfa_ioc_send_getattr(ioc);
317}
318
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000319/* IOC configuration in progress. Timer is active. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700320static void
321bfa_ioc_sm_getattr(struct bfa_ioc *ioc, enum ioc_event event)
322{
323 switch (event) {
324 case IOC_E_FWRSP_GETATTR:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000325 del_timer(&ioc->ioc_timer);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700326 bfa_fsm_set_state(ioc, bfa_ioc_sm_op);
327 break;
328
Rasesh Modyf374b362011-07-22 08:07:46 +0000329 case IOC_E_PFFAILED:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700330 case IOC_E_HWERROR:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000331 del_timer(&ioc->ioc_timer);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700332 /* fall through */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700333 case IOC_E_TIMEOUT:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000334 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
Rasesh Mody078086f2011-08-08 16:21:39 +0000335 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
Rasesh Modyf374b362011-07-22 08:07:46 +0000336 if (event != IOC_E_PFFAILED)
Rasesh Mody1d32f762010-12-23 21:45:09 +0000337 bfa_iocpf_getattrfail(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700338 break;
339
340 case IOC_E_DISABLE:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000341 del_timer(&ioc->ioc_timer);
342 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
343 break;
344
345 case IOC_E_ENABLE:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700346 break;
347
348 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000349 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700350 }
351}
352
353static void
354bfa_ioc_sm_op_entry(struct bfa_ioc *ioc)
355{
356 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_OK);
Rasesh Mody078086f2011-08-08 16:21:39 +0000357 bfa_ioc_event_notify(ioc, BFA_IOC_E_ENABLED);
Jing Huangf96c1d22012-04-04 05:42:54 +0000358 bfa_ioc_hb_monitor(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700359}
360
361static void
362bfa_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
Rasesh Modyf374b362011-07-22 08:07:46 +0000373 case IOC_E_PFFAILED:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700374 case IOC_E_HWERROR:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700375 bfa_ioc_hb_stop(ioc);
376 /* !!! fall through !!! */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700377 case IOC_E_HBFAIL:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000378 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
Rasesh Mody078086f2011-08-08 16:21:39 +0000383 bfa_ioc_fail_notify(ioc);
384
Rasesh Modyf374b362011-07-22 08:07:46 +0000385 if (event != IOC_E_PFFAILED)
Rasesh Mody1d32f762010-12-23 21:45:09 +0000386 bfa_iocpf_fail(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700387 break;
388
389 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000390 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700391 }
392}
393
394static void
395bfa_ioc_sm_disabling_entry(struct bfa_ioc *ioc)
396{
Rasesh Mody1d32f762010-12-23 21:45:09 +0000397 bfa_iocpf_disable(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700398}
399
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000400/* IOC is being disabled */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700401static void
402bfa_ioc_sm_disabling(struct bfa_ioc *ioc, enum ioc_event event)
403{
404 switch (event) {
Rasesh Mody1d32f762010-12-23 21:45:09 +0000405 case IOC_E_DISABLED:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700406 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
407 break;
408
409 case IOC_E_HWERROR:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700410 /*
Rasesh Mody1d32f762010-12-23 21:45:09 +0000411 * No state change. Will move to disabled state
412 * after iocpf sm completes failure processing and
413 * moves to disabled state.
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700414 */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000415 bfa_iocpf_fail(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700416 break;
417
Rasesh Mody078086f2011-08-08 16:21:39 +0000418 case IOC_E_HWFAILED:
419 bfa_fsm_set_state(ioc, bfa_ioc_sm_hwfail);
420 bfa_ioc_disable_comp(ioc);
421 break;
422
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700423 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000424 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700425 }
426}
427
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000428/* IOC disable completion entry. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700429static void
430bfa_ioc_sm_disabled_entry(struct bfa_ioc *ioc)
431{
432 bfa_ioc_disable_comp(ioc);
433}
434
435static void
436bfa_ioc_sm_disabled(struct bfa_ioc *ioc, enum ioc_event event)
437{
438 switch (event) {
439 case IOC_E_ENABLE:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000440 bfa_fsm_set_state(ioc, bfa_ioc_sm_enabling);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700441 break;
442
443 case IOC_E_DISABLE:
444 ioc->cbfn->disable_cbfn(ioc->bfa);
445 break;
446
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700447 case IOC_E_DETACH:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000448 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
449 bfa_iocpf_stop(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700450 break;
451
452 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000453 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700454 }
455}
456
457static void
Rasesh Mody1d32f762010-12-23 21:45:09 +0000458bfa_ioc_sm_fail_retry_entry(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700459{
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700460}
461
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000462/* Hardware initialization retry. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700463static void
Rasesh Mody1d32f762010-12-23 21:45:09 +0000464bfa_ioc_sm_fail_retry(struct bfa_ioc *ioc, enum ioc_event event)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700465{
466 switch (event) {
Rasesh Mody1d32f762010-12-23 21:45:09 +0000467 case IOC_E_ENABLED:
468 bfa_fsm_set_state(ioc, bfa_ioc_sm_getattr);
469 break;
470
Rasesh Modyf374b362011-07-22 08:07:46 +0000471 case IOC_E_PFFAILED:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000472 case IOC_E_HWERROR:
473 /**
474 * Initialization retry failed.
475 */
476 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
Rasesh Mody078086f2011-08-08 16:21:39 +0000477 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
Rasesh Modyf374b362011-07-22 08:07:46 +0000478 if (event != IOC_E_PFFAILED)
Rasesh Mody1d32f762010-12-23 21:45:09 +0000479 bfa_iocpf_initfail(ioc);
480 break;
481
Rasesh Mody078086f2011-08-08 16:21:39 +0000482 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);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000485 break;
486
487 case IOC_E_ENABLE:
488 break;
489
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700490 case IOC_E_DISABLE:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000491 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700492 break;
493
494 case IOC_E_DETACH:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000495 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
496 bfa_iocpf_stop(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700497 break;
498
499 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000500 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700501 }
502}
503
504static void
Rasesh Mody1d32f762010-12-23 21:45:09 +0000505bfa_ioc_sm_fail_entry(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700506{
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700507}
508
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000509/* IOC failure. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700510static void
Rasesh Mody1d32f762010-12-23 21:45:09 +0000511bfa_ioc_sm_fail(struct bfa_ioc *ioc, enum ioc_event event)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700512{
513 switch (event) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700514 case IOC_E_ENABLE:
515 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
516 break;
517
518 case IOC_E_DISABLE:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000519 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700520 break;
521
Rasesh Mody1d32f762010-12-23 21:45:09 +0000522 case IOC_E_DETACH:
523 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
524 bfa_iocpf_stop(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700525 break;
526
527 case IOC_E_HWERROR:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000528 /* HB failure notification, ignore. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700529 break;
Rasesh Mody1d32f762010-12-23 21:45:09 +0000530
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700531 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000532 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700533 }
534}
535
Rasesh Mody078086f2011-08-08 16:21:39 +0000536static void
537bfa_ioc_sm_hwfail_entry(struct bfa_ioc *ioc)
538{
539}
540
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000541/* IOC failure. */
Rasesh Mody078086f2011-08-08 16:21:39 +0000542static void
543bfa_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
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000564/* IOCPF State Machine */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000565
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000566/* Reset entry actions -- initialize state machine */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000567static void
568bfa_iocpf_sm_reset_entry(struct bfa_iocpf *iocpf)
569{
Rasesh Mody078086f2011-08-08 16:21:39 +0000570 iocpf->fw_mismatch_notified = false;
Rasesh Mody1d32f762010-12-23 21:45:09 +0000571 iocpf->auto_recover = bfa_nw_auto_recover;
572}
573
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000574/* Beginning state. IOC is in reset state. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000575static void
576bfa_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:
Rasesh Modyac51f602011-07-22 08:07:43 +0000587 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000588 }
589}
590
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000591/* Semaphore should be acquired for version check. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000592static void
593bfa_iocpf_sm_fwcheck_entry(struct bfa_iocpf *iocpf)
594{
Rasesh Modyd4e16d42011-07-22 08:07:47 +0000595 bfa_ioc_hw_sem_init(iocpf->ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000596 bfa_ioc_hw_sem_get(iocpf->ioc);
597}
598
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000599/* Awaiting h/w semaphore to continue with version check. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000600static void
601bfa_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)) {
Rasesh Mody79ea6c82011-04-14 08:05:18 +0000608 if (bfa_ioc_sync_start(ioc)) {
Rasesh Mody1d32f762010-12-23 21:45:09 +0000609 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
Rasesh Mody078086f2011-08-08 16:21:39 +0000623 case IOCPF_E_SEM_ERROR:
624 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
625 bfa_ioc_pf_hwfailed(ioc);
626 break;
627
Rasesh Mody1d32f762010-12-23 21:45:09 +0000628 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:
Rasesh Modyac51f602011-07-22 08:07:43 +0000640 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000641 }
642}
643
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000644/* Notify enable completion callback */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000645static void
646bfa_iocpf_sm_mismatch_entry(struct bfa_iocpf *iocpf)
647{
648 /* Call only the first time sm enters fwmismatch state. */
Joe Perches23677ce2012-02-09 11:17:23 +0000649 if (!iocpf->fw_mismatch_notified)
Rasesh Mody1d32f762010-12-23 21:45:09 +0000650 bfa_ioc_pf_fwmismatch(iocpf->ioc);
651
Rasesh Mody078086f2011-08-08 16:21:39 +0000652 iocpf->fw_mismatch_notified = true;
Rasesh Mody1d32f762010-12-23 21:45:09 +0000653 mod_timer(&(iocpf->ioc)->iocpf_timer, jiffies +
654 msecs_to_jiffies(BFA_IOC_TOV));
655}
656
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000657/* Awaiting firmware version match. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000658static void
659bfa_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:
Rasesh Modyac51f602011-07-22 08:07:43 +0000680 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000681 }
682}
683
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000684/* Request for semaphore. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000685static void
686bfa_iocpf_sm_semwait_entry(struct bfa_iocpf *iocpf)
687{
688 bfa_ioc_hw_sem_get(iocpf->ioc);
689}
690
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000691/* Awaiting semaphore for h/w initialzation. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000692static void
693bfa_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
Rasesh Mody078086f2011-08-08 16:21:39 +0000709 case IOCPF_E_SEM_ERROR:
710 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
711 bfa_ioc_pf_hwfailed(ioc);
712 break;
713
Rasesh Mody1d32f762010-12-23 21:45:09 +0000714 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:
Rasesh Modyac51f602011-07-22 08:07:43 +0000720 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000721 }
722}
723
724static void
725bfa_iocpf_sm_hwinit_entry(struct bfa_iocpf *iocpf)
726{
Rasesh Mody078086f2011-08-08 16:21:39 +0000727 iocpf->poll_time = 0;
Rasesh Modyaafd5c22011-09-27 10:39:09 +0000728 bfa_ioc_reset(iocpf->ioc, false);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000729}
730
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000731/* Hardware is being initialized. Interrupts are enabled.
Rasesh Mody1d32f762010-12-23 21:45:09 +0000732 * Holding hardware semaphore lock.
733 */
734static void
735bfa_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:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000741 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_enabling);
742 break;
743
Rasesh Mody1d32f762010-12-23 21:45:09 +0000744 case IOCPF_E_TIMEOUT:
745 bfa_nw_ioc_hw_sem_release(ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000746 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:
Rasesh Modyac51f602011-07-22 08:07:43 +0000758 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000759 }
760}
761
762static void
763bfa_iocpf_sm_enabling_entry(struct bfa_iocpf *iocpf)
764{
765 mod_timer(&(iocpf->ioc)->iocpf_timer, jiffies +
766 msecs_to_jiffies(BFA_IOC_TOV));
Rasesh Mody078086f2011-08-08 16:21:39 +0000767 /**
768 * Enable Interrupts before sending fw IOC ENABLE cmd.
769 */
770 iocpf->ioc->cbfn->reset_cbfn(iocpf->ioc->bfa);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000771 bfa_ioc_send_enable(iocpf->ioc);
772}
773
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000774/* Host IOC function is being enabled, awaiting response from firmware.
Rasesh Mody1d32f762010-12-23 21:45:09 +0000775 * Semaphore is acquired.
776 */
777static void
778bfa_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
Rasesh Mody1d32f762010-12-23 21:45:09 +0000807 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000808 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000809 }
810}
811
Rasesh Mody1d32f762010-12-23 21:45:09 +0000812static void
813bfa_iocpf_sm_ready_entry(struct bfa_iocpf *iocpf)
814{
815 bfa_ioc_pf_enabled(iocpf->ioc);
816}
817
818static void
819bfa_iocpf_sm_ready(struct bfa_iocpf *iocpf, enum iocpf_event event)
820{
Rasesh Mody1d32f762010-12-23 21:45:09 +0000821 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
Rasesh Mody1d32f762010-12-23 21:45:09 +0000834 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000835 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000836 }
837}
838
839static void
840bfa_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
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000847/* IOC is being disabled */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000848static void
849bfa_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:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000855 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:
Rasesh Mody41ed9032013-12-17 17:07:32 -0800866 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_FAIL);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000867 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
868 break;
869
870 case IOCPF_E_FWRSP_ENABLE:
871 break;
872
873 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000874 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000875 }
876}
877
878static void
879bfa_iocpf_sm_disabling_sync_entry(struct bfa_iocpf *iocpf)
880{
881 bfa_ioc_hw_sem_get(iocpf->ioc);
882}
883
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000884/* IOC hb ack request is being removed. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000885static void
886bfa_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
Rasesh Mody078086f2011-08-08 16:21:39 +0000897 case IOCPF_E_SEM_ERROR:
898 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
899 bfa_ioc_pf_hwfailed(ioc);
900 break;
901
Rasesh Mody1d32f762010-12-23 21:45:09 +0000902 case IOCPF_E_FAIL:
903 break;
904
905 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000906 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000907 }
908}
909
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000910/* IOC disable completion entry. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000911static void
912bfa_iocpf_sm_disabled_entry(struct bfa_iocpf *iocpf)
913{
Rasesh Modyfdad4002011-07-22 08:07:45 +0000914 bfa_ioc_mbox_flush(iocpf->ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000915 bfa_ioc_pf_disabled(iocpf->ioc);
916}
917
918static void
919bfa_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:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000925 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:
Rasesh Modyac51f602011-07-22 08:07:43 +0000934 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000935 }
936}
937
938static void
939bfa_iocpf_sm_initfail_sync_entry(struct bfa_iocpf *iocpf)
940{
Krishna Gudipati7afc5db2011-12-22 13:30:19 +0000941 bfa_nw_ioc_debug_save_ftrc(iocpf->ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000942 bfa_ioc_hw_sem_get(iocpf->ioc);
943}
944
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000945/* Hardware initialization failed. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000946static void
947bfa_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);
Rasesh Mody078086f2011-08-08 16:21:39 +0000954 bfa_ioc_sync_leave(ioc);
Rasesh Mody41ed9032013-12-17 17:07:32 -0800955 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_FAIL);
Rasesh Mody078086f2011-08-08 16:21:39 +0000956 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);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000963 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:
Rasesh Modyac51f602011-07-22 08:07:43 +0000980 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000981 }
982}
983
984static void
985bfa_iocpf_sm_initfail_entry(struct bfa_iocpf *iocpf)
986{
Rasesh Mody1d32f762010-12-23 21:45:09 +0000987}
988
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000989/* Hardware initialization failed. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000990static void
991bfa_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:
Rasesh Modyac51f602011-07-22 08:07:43 +00001006 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001007 }
1008}
1009
1010static void
1011bfa_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 */
Rasesh Modyfdad4002011-07-22 08:07:45 +00001021 bfa_ioc_mbox_flush(iocpf->ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001022 bfa_ioc_hw_sem_get(iocpf->ioc);
1023}
1024
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001025/* IOC is in failed state. */
Rasesh Mody1d32f762010-12-23 21:45:09 +00001026static void
1027bfa_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:
Rasesh Mody1d32f762010-12-23 21:45:09 +00001033 bfa_ioc_sync_ack(ioc);
1034 bfa_ioc_notify_fail(ioc);
1035 if (!iocpf->auto_recover) {
1036 bfa_ioc_sync_leave(ioc);
Rasesh Mody41ed9032013-12-17 17:07:32 -08001037 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_FAIL);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001038 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
Rasesh Mody078086f2011-08-08 16:21:39 +00001050 case IOCPF_E_SEM_ERROR:
1051 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
1052 bfa_ioc_pf_hwfailed(ioc);
1053 break;
1054
Rasesh Mody1d32f762010-12-23 21:45:09 +00001055 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:
Rasesh Modyac51f602011-07-22 08:07:43 +00001064 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001065 }
1066}
1067
1068static void
1069bfa_iocpf_sm_fail_entry(struct bfa_iocpf *iocpf)
1070{
1071}
1072
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001073/* IOC is in failed state. */
Rasesh Mody1d32f762010-12-23 21:45:09 +00001074static void
1075bfa_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:
Rasesh Modyac51f602011-07-22 08:07:43 +00001083 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001084 }
1085}
1086
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001087/* BFA IOC private functions */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001088
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001089/* Notify common modules registered for notification. */
Rasesh Modybd5a92e2011-07-22 08:07:42 +00001090static void
1091bfa_ioc_event_notify(struct bfa_ioc *ioc, enum bfa_ioc_event event)
1092{
1093 struct bfa_ioc_notify *notify;
Rasesh Modybd5a92e2011-07-22 08:07:42 +00001094
Ivan Vecera16712c52015-06-11 15:52:29 +02001095 list_for_each_entry(notify, &ioc->notify_q, qe)
Rasesh Modybd5a92e2011-07-22 08:07:42 +00001096 notify->cbfn(notify->cbarg, event);
Rasesh Modybd5a92e2011-07-22 08:07:42 +00001097}
1098
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001099static void
1100bfa_ioc_disable_comp(struct bfa_ioc *ioc)
1101{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001102 ioc->cbfn->disable_cbfn(ioc->bfa);
Rasesh Modybd5a92e2011-07-22 08:07:42 +00001103 bfa_ioc_event_notify(ioc, BFA_IOC_E_DISABLED);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001104}
1105
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001106bool
Rasesh Mody8a891422010-08-25 23:00:27 -07001107bfa_nw_ioc_sem_get(void __iomem *sem_reg)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001108{
1109 u32 r32;
1110 int cnt = 0;
1111#define BFA_SEM_SPINCNT 3000
1112
1113 r32 = readl(sem_reg);
1114
Rasesh Mody078086f2011-08-08 16:21:39 +00001115 while ((r32 & 1) && (cnt < BFA_SEM_SPINCNT)) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001116 cnt++;
1117 udelay(2);
1118 r32 = readl(sem_reg);
1119 }
1120
Rasesh Mody078086f2011-08-08 16:21:39 +00001121 if (!(r32 & 1))
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001122 return true;
1123
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001124 return false;
1125}
1126
1127void
Rasesh Mody8a891422010-08-25 23:00:27 -07001128bfa_nw_ioc_sem_release(void __iomem *sem_reg)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001129{
Rasesh Mody1d51a132011-09-16 15:06:46 +00001130 readl(sem_reg);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001131 writel(1, sem_reg);
1132}
1133
Jing Huange491c772012-04-04 05:42:08 +00001134/* Clear fwver hdr */
1135static void
1136bfa_ioc_fwver_clear(struct bfa_ioc *ioc)
1137{
1138 u32 pgnum, pgoff, loff = 0;
1139 int i;
1140
1141 pgnum = PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, loff);
1142 pgoff = PSS_SMEM_PGOFF(loff);
1143 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
1144
1145 for (i = 0; i < (sizeof(struct bfi_ioc_image_hdr) / sizeof(u32)); i++) {
1146 writel(0, ioc->ioc_regs.smem_page_start + loff);
1147 loff += sizeof(u32);
1148 }
1149}
1150
1151
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001152static void
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001153bfa_ioc_hw_sem_init(struct bfa_ioc *ioc)
1154{
1155 struct bfi_ioc_image_hdr fwhdr;
Jing Huange491c772012-04-04 05:42:08 +00001156 u32 fwstate, r32;
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001157
Jing Huange491c772012-04-04 05:42:08 +00001158 /* Spin on init semaphore to serialize. */
1159 r32 = readl(ioc->ioc_regs.ioc_init_sem_reg);
1160 while (r32 & 0x1) {
1161 udelay(20);
1162 r32 = readl(ioc->ioc_regs.ioc_init_sem_reg);
1163 }
1164
Rasesh Mody41ed9032013-12-17 17:07:32 -08001165 fwstate = bfa_ioc_get_cur_ioc_fwstate(ioc);
Jing Huange491c772012-04-04 05:42:08 +00001166 if (fwstate == BFI_IOC_UNINIT) {
1167 writel(1, ioc->ioc_regs.ioc_init_sem_reg);
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001168 return;
Jing Huange491c772012-04-04 05:42:08 +00001169 }
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001170
1171 bfa_nw_ioc_fwver_get(ioc, &fwhdr);
1172
Jing Huange491c772012-04-04 05:42:08 +00001173 if (swab32(fwhdr.exec) == BFI_FWBOOT_TYPE_NORMAL) {
1174 writel(1, ioc->ioc_regs.ioc_init_sem_reg);
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001175 return;
Jing Huange491c772012-04-04 05:42:08 +00001176 }
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001177
Jing Huange491c772012-04-04 05:42:08 +00001178 bfa_ioc_fwver_clear(ioc);
Rasesh Mody41ed9032013-12-17 17:07:32 -08001179 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_UNINIT);
1180 bfa_ioc_set_alt_ioc_fwstate(ioc, BFI_IOC_UNINIT);
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001181
1182 /*
1183 * Try to lock and then unlock the semaphore.
1184 */
1185 readl(ioc->ioc_regs.ioc_sem_reg);
1186 writel(1, ioc->ioc_regs.ioc_sem_reg);
Jing Huange491c772012-04-04 05:42:08 +00001187
1188 /* Unlock init semaphore */
1189 writel(1, ioc->ioc_regs.ioc_init_sem_reg);
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001190}
1191
1192static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001193bfa_ioc_hw_sem_get(struct bfa_ioc *ioc)
1194{
1195 u32 r32;
1196
1197 /**
1198 * First read to the semaphore register will return 0, subsequent reads
1199 * will return 1. Semaphore is released by writing 1 to the register
1200 */
1201 r32 = readl(ioc->ioc_regs.ioc_sem_reg);
Rasesh Mody078086f2011-08-08 16:21:39 +00001202 if (r32 == ~0) {
1203 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_SEM_ERROR);
1204 return;
1205 }
1206 if (!(r32 & 1)) {
Rasesh Mody1d32f762010-12-23 21:45:09 +00001207 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_SEMLOCKED);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001208 return;
1209 }
1210
1211 mod_timer(&ioc->sem_timer, jiffies +
1212 msecs_to_jiffies(BFA_IOC_HWSEM_TOV));
1213}
1214
1215void
Rasesh Mody8a891422010-08-25 23:00:27 -07001216bfa_nw_ioc_hw_sem_release(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001217{
1218 writel(1, ioc->ioc_regs.ioc_sem_reg);
1219}
1220
1221static void
1222bfa_ioc_hw_sem_get_cancel(struct bfa_ioc *ioc)
1223{
1224 del_timer(&ioc->sem_timer);
1225}
1226
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001227/* Initialize LPU local memory (aka secondary memory / SRAM) */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001228static void
1229bfa_ioc_lmem_init(struct bfa_ioc *ioc)
1230{
1231 u32 pss_ctl;
1232 int i;
1233#define PSS_LMEM_INIT_TIME 10000
1234
1235 pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1236 pss_ctl &= ~__PSS_LMEM_RESET;
1237 pss_ctl |= __PSS_LMEM_INIT_EN;
1238
1239 /*
1240 * i2c workaround 12.5khz clock
1241 */
1242 pss_ctl |= __PSS_I2C_CLK_DIV(3UL);
1243 writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1244
1245 /**
1246 * wait for memory initialization to be complete
1247 */
1248 i = 0;
1249 do {
1250 pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1251 i++;
1252 } while (!(pss_ctl & __PSS_LMEM_INIT_DONE) && (i < PSS_LMEM_INIT_TIME));
1253
1254 /**
1255 * If memory initialization is not successful, IOC timeout will catch
1256 * such failures.
1257 */
1258 BUG_ON(!(pss_ctl & __PSS_LMEM_INIT_DONE));
1259
1260 pss_ctl &= ~(__PSS_LMEM_INIT_DONE | __PSS_LMEM_INIT_EN);
1261 writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1262}
1263
1264static void
1265bfa_ioc_lpu_start(struct bfa_ioc *ioc)
1266{
1267 u32 pss_ctl;
1268
1269 /**
1270 * Take processor out of reset.
1271 */
1272 pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1273 pss_ctl &= ~__PSS_LPU0_RESET;
1274
1275 writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1276}
1277
1278static void
1279bfa_ioc_lpu_stop(struct bfa_ioc *ioc)
1280{
1281 u32 pss_ctl;
1282
1283 /**
1284 * Put processors in reset.
1285 */
1286 pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1287 pss_ctl |= (__PSS_LPU0_RESET | __PSS_LPU1_RESET);
1288
1289 writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1290}
1291
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001292/* Get driver and firmware versions. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001293void
Rasesh Mody8a891422010-08-25 23:00:27 -07001294bfa_nw_ioc_fwver_get(struct bfa_ioc *ioc, struct bfi_ioc_image_hdr *fwhdr)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001295{
David S. Miller58598542011-04-17 16:51:36 -07001296 u32 pgnum;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001297 u32 loff = 0;
1298 int i;
1299 u32 *fwsig = (u32 *) fwhdr;
1300
1301 pgnum = bfa_ioc_smem_pgnum(ioc, loff);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001302 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
1303
1304 for (i = 0; i < (sizeof(struct bfi_ioc_image_hdr) / sizeof(u32));
1305 i++) {
1306 fwsig[i] =
Ivan Veceraebb56d32015-06-17 18:41:58 +02001307 swab32(readl(loff + ioc->ioc_regs.smem_page_start));
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001308 loff += sizeof(u32);
1309 }
1310}
1311
Rasesh Modyc107ba12013-12-17 17:07:41 -08001312static bool
1313bfa_ioc_fwver_md5_check(struct bfi_ioc_image_hdr *fwhdr_1,
1314 struct bfi_ioc_image_hdr *fwhdr_2)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001315{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001316 int i;
1317
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001318 for (i = 0; i < BFI_IOC_MD5SUM_SZ; i++) {
Rasesh Modyc107ba12013-12-17 17:07:41 -08001319 if (fwhdr_1->md5sum[i] != fwhdr_2->md5sum[i])
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001320 return false;
1321 }
1322
1323 return true;
1324}
1325
Joe Perchesdbedd442015-03-06 20:49:12 -08001326/* Returns TRUE if major minor and maintenance are same.
Rasesh Modyc107ba12013-12-17 17:07:41 -08001327 * If patch version are same, check for MD5 Checksum to be same.
1328 */
1329static bool
1330bfa_ioc_fw_ver_compatible(struct bfi_ioc_image_hdr *drv_fwhdr,
1331 struct bfi_ioc_image_hdr *fwhdr_to_cmp)
1332{
1333 if (drv_fwhdr->signature != fwhdr_to_cmp->signature)
1334 return false;
1335 if (drv_fwhdr->fwver.major != fwhdr_to_cmp->fwver.major)
1336 return false;
1337 if (drv_fwhdr->fwver.minor != fwhdr_to_cmp->fwver.minor)
1338 return false;
1339 if (drv_fwhdr->fwver.maint != fwhdr_to_cmp->fwver.maint)
1340 return false;
1341 if (drv_fwhdr->fwver.patch == fwhdr_to_cmp->fwver.patch &&
1342 drv_fwhdr->fwver.phase == fwhdr_to_cmp->fwver.phase &&
1343 drv_fwhdr->fwver.build == fwhdr_to_cmp->fwver.build)
1344 return bfa_ioc_fwver_md5_check(drv_fwhdr, fwhdr_to_cmp);
1345
1346 return true;
1347}
1348
1349static bool
1350bfa_ioc_flash_fwver_valid(struct bfi_ioc_image_hdr *flash_fwhdr)
1351{
1352 if (flash_fwhdr->fwver.major == 0 || flash_fwhdr->fwver.major == 0xFF)
1353 return false;
1354
1355 return true;
1356}
1357
1358static bool
1359fwhdr_is_ga(struct bfi_ioc_image_hdr *fwhdr)
1360{
1361 if (fwhdr->fwver.phase == 0 &&
1362 fwhdr->fwver.build == 0)
1363 return false;
1364
1365 return true;
1366}
1367
1368/* Returns TRUE if both are compatible and patch of fwhdr_to_cmp is better. */
1369static enum bfi_ioc_img_ver_cmp
1370bfa_ioc_fw_ver_patch_cmp(struct bfi_ioc_image_hdr *base_fwhdr,
1371 struct bfi_ioc_image_hdr *fwhdr_to_cmp)
1372{
Ivan Vecera93719d262015-06-11 15:52:25 +02001373 if (!bfa_ioc_fw_ver_compatible(base_fwhdr, fwhdr_to_cmp))
Rasesh Modyc107ba12013-12-17 17:07:41 -08001374 return BFI_IOC_IMG_VER_INCOMP;
1375
1376 if (fwhdr_to_cmp->fwver.patch > base_fwhdr->fwver.patch)
1377 return BFI_IOC_IMG_VER_BETTER;
1378 else if (fwhdr_to_cmp->fwver.patch < base_fwhdr->fwver.patch)
1379 return BFI_IOC_IMG_VER_OLD;
1380
1381 /* GA takes priority over internal builds of the same patch stream.
1382 * At this point major minor maint and patch numbers are same.
1383 */
Ivan Vecera93719d262015-06-11 15:52:25 +02001384 if (fwhdr_is_ga(base_fwhdr))
Rasesh Modyc107ba12013-12-17 17:07:41 -08001385 if (fwhdr_is_ga(fwhdr_to_cmp))
1386 return BFI_IOC_IMG_VER_SAME;
1387 else
1388 return BFI_IOC_IMG_VER_OLD;
1389 else
1390 if (fwhdr_is_ga(fwhdr_to_cmp))
1391 return BFI_IOC_IMG_VER_BETTER;
1392
1393 if (fwhdr_to_cmp->fwver.phase > base_fwhdr->fwver.phase)
1394 return BFI_IOC_IMG_VER_BETTER;
1395 else if (fwhdr_to_cmp->fwver.phase < base_fwhdr->fwver.phase)
1396 return BFI_IOC_IMG_VER_OLD;
1397
1398 if (fwhdr_to_cmp->fwver.build > base_fwhdr->fwver.build)
1399 return BFI_IOC_IMG_VER_BETTER;
1400 else if (fwhdr_to_cmp->fwver.build < base_fwhdr->fwver.build)
1401 return BFI_IOC_IMG_VER_OLD;
1402
1403 /* All Version Numbers are equal.
1404 * Md5 check to be done as a part of compatibility check.
1405 */
1406 return BFI_IOC_IMG_VER_SAME;
1407}
1408
1409/* register definitions */
1410#define FLI_CMD_REG 0x0001d000
1411#define FLI_WRDATA_REG 0x0001d00c
1412#define FLI_RDDATA_REG 0x0001d010
1413#define FLI_ADDR_REG 0x0001d004
1414#define FLI_DEV_STATUS_REG 0x0001d014
1415
1416#define BFA_FLASH_FIFO_SIZE 128 /* fifo size */
1417#define BFA_FLASH_CHECK_MAX 10000 /* max # of status check */
1418#define BFA_FLASH_BLOCKING_OP_MAX 1000000 /* max # of blocking op check */
1419#define BFA_FLASH_WIP_MASK 0x01 /* write in progress bit mask */
1420
1421#define NFC_STATE_RUNNING 0x20000001
1422#define NFC_STATE_PAUSED 0x00004560
1423#define NFC_VER_VALID 0x147
1424
1425enum bfa_flash_cmd {
1426 BFA_FLASH_FAST_READ = 0x0b, /* fast read */
1427 BFA_FLASH_WRITE_ENABLE = 0x06, /* write enable */
1428 BFA_FLASH_SECTOR_ERASE = 0xd8, /* sector erase */
1429 BFA_FLASH_WRITE = 0x02, /* write */
1430 BFA_FLASH_READ_STATUS = 0x05, /* read status */
1431};
1432
1433/* hardware error definition */
1434enum bfa_flash_err {
1435 BFA_FLASH_NOT_PRESENT = -1, /*!< flash not present */
1436 BFA_FLASH_UNINIT = -2, /*!< flash not initialized */
1437 BFA_FLASH_BAD = -3, /*!< flash bad */
1438 BFA_FLASH_BUSY = -4, /*!< flash busy */
1439 BFA_FLASH_ERR_CMD_ACT = -5, /*!< command active never cleared */
1440 BFA_FLASH_ERR_FIFO_CNT = -6, /*!< fifo count never cleared */
1441 BFA_FLASH_ERR_WIP = -7, /*!< write-in-progress never cleared */
1442 BFA_FLASH_ERR_TIMEOUT = -8, /*!< fli timeout */
1443 BFA_FLASH_ERR_LEN = -9, /*!< invalid length */
1444};
1445
1446/* flash command register data structure */
1447union bfa_flash_cmd_reg {
1448 struct {
1449#ifdef __BIG_ENDIAN
1450 u32 act:1;
1451 u32 rsv:1;
1452 u32 write_cnt:9;
1453 u32 read_cnt:9;
1454 u32 addr_cnt:4;
1455 u32 cmd:8;
1456#else
1457 u32 cmd:8;
1458 u32 addr_cnt:4;
1459 u32 read_cnt:9;
1460 u32 write_cnt:9;
1461 u32 rsv:1;
1462 u32 act:1;
1463#endif
1464 } r;
1465 u32 i;
1466};
1467
1468/* flash device status register data structure */
1469union bfa_flash_dev_status_reg {
1470 struct {
1471#ifdef __BIG_ENDIAN
1472 u32 rsv:21;
1473 u32 fifo_cnt:6;
1474 u32 busy:1;
1475 u32 init_status:1;
1476 u32 present:1;
1477 u32 bad:1;
1478 u32 good:1;
1479#else
1480 u32 good:1;
1481 u32 bad:1;
1482 u32 present:1;
1483 u32 init_status:1;
1484 u32 busy:1;
1485 u32 fifo_cnt:6;
1486 u32 rsv:21;
1487#endif
1488 } r;
1489 u32 i;
1490};
1491
1492/* flash address register data structure */
1493union bfa_flash_addr_reg {
1494 struct {
1495#ifdef __BIG_ENDIAN
1496 u32 addr:24;
1497 u32 dummy:8;
1498#else
1499 u32 dummy:8;
1500 u32 addr:24;
1501#endif
1502 } r;
1503 u32 i;
1504};
1505
1506/* Flash raw private functions */
1507static void
1508bfa_flash_set_cmd(void __iomem *pci_bar, u8 wr_cnt,
1509 u8 rd_cnt, u8 ad_cnt, u8 op)
1510{
1511 union bfa_flash_cmd_reg cmd;
1512
1513 cmd.i = 0;
1514 cmd.r.act = 1;
1515 cmd.r.write_cnt = wr_cnt;
1516 cmd.r.read_cnt = rd_cnt;
1517 cmd.r.addr_cnt = ad_cnt;
1518 cmd.r.cmd = op;
1519 writel(cmd.i, (pci_bar + FLI_CMD_REG));
1520}
1521
1522static void
1523bfa_flash_set_addr(void __iomem *pci_bar, u32 address)
1524{
1525 union bfa_flash_addr_reg addr;
1526
1527 addr.r.addr = address & 0x00ffffff;
1528 addr.r.dummy = 0;
1529 writel(addr.i, (pci_bar + FLI_ADDR_REG));
1530}
1531
1532static int
1533bfa_flash_cmd_act_check(void __iomem *pci_bar)
1534{
1535 union bfa_flash_cmd_reg cmd;
1536
1537 cmd.i = readl(pci_bar + FLI_CMD_REG);
1538
1539 if (cmd.r.act)
1540 return BFA_FLASH_ERR_CMD_ACT;
1541
1542 return 0;
1543}
1544
1545/* Flush FLI data fifo. */
1546static u32
1547bfa_flash_fifo_flush(void __iomem *pci_bar)
1548{
1549 u32 i;
1550 u32 t;
1551 union bfa_flash_dev_status_reg dev_status;
1552
1553 dev_status.i = readl(pci_bar + FLI_DEV_STATUS_REG);
1554
1555 if (!dev_status.r.fifo_cnt)
1556 return 0;
1557
1558 /* fifo counter in terms of words */
1559 for (i = 0; i < dev_status.r.fifo_cnt; i++)
1560 t = readl(pci_bar + FLI_RDDATA_REG);
1561
1562 /* Check the device status. It may take some time. */
1563 for (i = 0; i < BFA_FLASH_CHECK_MAX; i++) {
1564 dev_status.i = readl(pci_bar + FLI_DEV_STATUS_REG);
1565 if (!dev_status.r.fifo_cnt)
1566 break;
1567 }
1568
1569 if (dev_status.r.fifo_cnt)
1570 return BFA_FLASH_ERR_FIFO_CNT;
1571
1572 return 0;
1573}
1574
1575/* Read flash status. */
1576static u32
1577bfa_flash_status_read(void __iomem *pci_bar)
1578{
1579 union bfa_flash_dev_status_reg dev_status;
1580 u32 status;
1581 u32 ret_status;
1582 int i;
1583
1584 status = bfa_flash_fifo_flush(pci_bar);
1585 if (status < 0)
1586 return status;
1587
1588 bfa_flash_set_cmd(pci_bar, 0, 4, 0, BFA_FLASH_READ_STATUS);
1589
1590 for (i = 0; i < BFA_FLASH_CHECK_MAX; i++) {
1591 status = bfa_flash_cmd_act_check(pci_bar);
1592 if (!status)
1593 break;
1594 }
1595
1596 if (status)
1597 return status;
1598
1599 dev_status.i = readl(pci_bar + FLI_DEV_STATUS_REG);
1600 if (!dev_status.r.fifo_cnt)
1601 return BFA_FLASH_BUSY;
1602
1603 ret_status = readl(pci_bar + FLI_RDDATA_REG);
1604 ret_status >>= 24;
1605
1606 status = bfa_flash_fifo_flush(pci_bar);
1607 if (status < 0)
1608 return status;
1609
1610 return ret_status;
1611}
1612
1613/* Start flash read operation. */
1614static u32
1615bfa_flash_read_start(void __iomem *pci_bar, u32 offset, u32 len,
1616 char *buf)
1617{
1618 u32 status;
1619
1620 /* len must be mutiple of 4 and not exceeding fifo size */
1621 if (len == 0 || len > BFA_FLASH_FIFO_SIZE || (len & 0x03) != 0)
1622 return BFA_FLASH_ERR_LEN;
1623
1624 /* check status */
1625 status = bfa_flash_status_read(pci_bar);
1626 if (status == BFA_FLASH_BUSY)
1627 status = bfa_flash_status_read(pci_bar);
1628
1629 if (status < 0)
1630 return status;
1631
1632 /* check if write-in-progress bit is cleared */
1633 if (status & BFA_FLASH_WIP_MASK)
1634 return BFA_FLASH_ERR_WIP;
1635
1636 bfa_flash_set_addr(pci_bar, offset);
1637
1638 bfa_flash_set_cmd(pci_bar, 0, (u8)len, 4, BFA_FLASH_FAST_READ);
1639
1640 return 0;
1641}
1642
1643/* Check flash read operation. */
1644static u32
1645bfa_flash_read_check(void __iomem *pci_bar)
1646{
1647 if (bfa_flash_cmd_act_check(pci_bar))
1648 return 1;
1649
1650 return 0;
1651}
1652
1653/* End flash read operation. */
1654static void
1655bfa_flash_read_end(void __iomem *pci_bar, u32 len, char *buf)
1656{
1657 u32 i;
1658
1659 /* read data fifo up to 32 words */
1660 for (i = 0; i < len; i += 4) {
1661 u32 w = readl(pci_bar + FLI_RDDATA_REG);
1662 *((u32 *)(buf + i)) = swab32(w);
1663 }
1664
1665 bfa_flash_fifo_flush(pci_bar);
1666}
1667
1668/* Perform flash raw read. */
1669
1670#define FLASH_BLOCKING_OP_MAX 500
1671#define FLASH_SEM_LOCK_REG 0x18820
1672
1673static int
1674bfa_raw_sem_get(void __iomem *bar)
1675{
1676 int locked;
1677
Ivan Veceraebb56d32015-06-17 18:41:58 +02001678 locked = readl(bar + FLASH_SEM_LOCK_REG);
Rasesh Modyc107ba12013-12-17 17:07:41 -08001679
1680 return !locked;
1681}
1682
1683static enum bfa_status
1684bfa_flash_sem_get(void __iomem *bar)
1685{
1686 u32 n = FLASH_BLOCKING_OP_MAX;
1687
1688 while (!bfa_raw_sem_get(bar)) {
1689 if (--n <= 0)
1690 return BFA_STATUS_BADFLASH;
Ben Hutchingsbc48bc82014-03-09 04:03:22 +00001691 mdelay(10);
Rasesh Modyc107ba12013-12-17 17:07:41 -08001692 }
1693 return BFA_STATUS_OK;
1694}
1695
1696static void
1697bfa_flash_sem_put(void __iomem *bar)
1698{
1699 writel(0, (bar + FLASH_SEM_LOCK_REG));
1700}
1701
1702static enum bfa_status
1703bfa_flash_raw_read(void __iomem *pci_bar, u32 offset, char *buf,
1704 u32 len)
1705{
1706 u32 n, status;
1707 u32 off, l, s, residue, fifo_sz;
1708
1709 residue = len;
1710 off = 0;
1711 fifo_sz = BFA_FLASH_FIFO_SIZE;
1712 status = bfa_flash_sem_get(pci_bar);
1713 if (status != BFA_STATUS_OK)
1714 return status;
1715
1716 while (residue) {
1717 s = offset + off;
1718 n = s / fifo_sz;
1719 l = (n + 1) * fifo_sz - s;
1720 if (l > residue)
1721 l = residue;
1722
1723 status = bfa_flash_read_start(pci_bar, offset + off, l,
1724 &buf[off]);
1725 if (status < 0) {
1726 bfa_flash_sem_put(pci_bar);
1727 return BFA_STATUS_FAILED;
1728 }
1729
1730 n = BFA_FLASH_BLOCKING_OP_MAX;
1731 while (bfa_flash_read_check(pci_bar)) {
1732 if (--n <= 0) {
1733 bfa_flash_sem_put(pci_bar);
1734 return BFA_STATUS_FAILED;
1735 }
1736 }
1737
1738 bfa_flash_read_end(pci_bar, l, &buf[off]);
1739
1740 residue -= l;
1741 off += l;
1742 }
1743 bfa_flash_sem_put(pci_bar);
1744
1745 return BFA_STATUS_OK;
1746}
1747
Rasesh Modyc107ba12013-12-17 17:07:41 -08001748#define BFA_FLASH_PART_FWIMG_ADDR 0x100000 /* fw image address */
1749
stephen hemminger2fd888a2014-01-15 08:24:21 -08001750static enum bfa_status
Rasesh Modyc107ba12013-12-17 17:07:41 -08001751bfa_nw_ioc_flash_img_get_chnk(struct bfa_ioc *ioc, u32 off,
1752 u32 *fwimg)
1753{
1754 return bfa_flash_raw_read(ioc->pcidev.pci_bar_kva,
1755 BFA_FLASH_PART_FWIMG_ADDR + (off * sizeof(u32)),
1756 (char *)fwimg, BFI_FLASH_CHUNK_SZ);
1757}
1758
1759static enum bfi_ioc_img_ver_cmp
1760bfa_ioc_flash_fwver_cmp(struct bfa_ioc *ioc,
1761 struct bfi_ioc_image_hdr *base_fwhdr)
1762{
1763 struct bfi_ioc_image_hdr *flash_fwhdr;
1764 enum bfa_status status;
1765 u32 fwimg[BFI_FLASH_CHUNK_SZ_WORDS];
1766
1767 status = bfa_nw_ioc_flash_img_get_chnk(ioc, 0, fwimg);
1768 if (status != BFA_STATUS_OK)
1769 return BFI_IOC_IMG_VER_INCOMP;
1770
1771 flash_fwhdr = (struct bfi_ioc_image_hdr *)fwimg;
1772 if (bfa_ioc_flash_fwver_valid(flash_fwhdr))
1773 return bfa_ioc_fw_ver_patch_cmp(base_fwhdr, flash_fwhdr);
1774 else
1775 return BFI_IOC_IMG_VER_INCOMP;
1776}
1777
1778/**
1779 * Returns TRUE if driver is willing to work with current smem f/w version.
1780 */
1781bool
1782bfa_nw_ioc_fwver_cmp(struct bfa_ioc *ioc, struct bfi_ioc_image_hdr *fwhdr)
1783{
1784 struct bfi_ioc_image_hdr *drv_fwhdr;
1785 enum bfi_ioc_img_ver_cmp smem_flash_cmp, drv_smem_cmp;
1786
1787 drv_fwhdr = (struct bfi_ioc_image_hdr *)
1788 bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc), 0);
1789
1790 /* If smem is incompatible or old, driver should not work with it. */
1791 drv_smem_cmp = bfa_ioc_fw_ver_patch_cmp(drv_fwhdr, fwhdr);
1792 if (drv_smem_cmp == BFI_IOC_IMG_VER_INCOMP ||
1793 drv_smem_cmp == BFI_IOC_IMG_VER_OLD) {
1794 return false;
1795 }
1796
1797 /* IF Flash has a better F/W than smem do not work with smem.
1798 * If smem f/w == flash f/w, as smem f/w not old | incmp, work with it.
1799 * If Flash is old or incomp work with smem iff smem f/w == drv f/w.
1800 */
1801 smem_flash_cmp = bfa_ioc_flash_fwver_cmp(ioc, fwhdr);
1802
1803 if (smem_flash_cmp == BFI_IOC_IMG_VER_BETTER)
1804 return false;
1805 else if (smem_flash_cmp == BFI_IOC_IMG_VER_SAME)
1806 return true;
1807 else
1808 return (drv_smem_cmp == BFI_IOC_IMG_VER_SAME) ?
1809 true : false;
1810}
1811
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001812/* Return true if current running version is valid. Firmware signature and
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001813 * execution context (driver/bios) must match.
1814 */
1815static bool
Rasesh Mody79ea6c82011-04-14 08:05:18 +00001816bfa_ioc_fwver_valid(struct bfa_ioc *ioc, u32 boot_env)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001817{
Rasesh Modyc107ba12013-12-17 17:07:41 -08001818 struct bfi_ioc_image_hdr fwhdr;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001819
Rasesh Mody8a891422010-08-25 23:00:27 -07001820 bfa_nw_ioc_fwver_get(ioc, &fwhdr);
Rasesh Mody078086f2011-08-08 16:21:39 +00001821 if (swab32(fwhdr.bootenv) != boot_env)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001822 return false;
1823
Rasesh Mody8a891422010-08-25 23:00:27 -07001824 return bfa_nw_ioc_fwver_cmp(ioc, &fwhdr);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001825}
1826
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001827/* Conditionally flush any pending message from firmware at start. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001828static void
1829bfa_ioc_msgflush(struct bfa_ioc *ioc)
1830{
1831 u32 r32;
1832
1833 r32 = readl(ioc->ioc_regs.lpu_mbox_cmd);
1834 if (r32)
1835 writel(1, ioc->ioc_regs.lpu_mbox_cmd);
1836}
1837
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001838static void
1839bfa_ioc_hwinit(struct bfa_ioc *ioc, bool force)
1840{
1841 enum bfi_ioc_state ioc_fwstate;
1842 bool fwvalid;
Rasesh Mody79ea6c82011-04-14 08:05:18 +00001843 u32 boot_env;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001844
Rasesh Mody41ed9032013-12-17 17:07:32 -08001845 ioc_fwstate = bfa_ioc_get_cur_ioc_fwstate(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001846
1847 if (force)
1848 ioc_fwstate = BFI_IOC_UNINIT;
1849
Rasesh Mody078086f2011-08-08 16:21:39 +00001850 boot_env = BFI_FWBOOT_ENV_OS;
1851
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001852 /**
1853 * check if firmware is valid
1854 */
1855 fwvalid = (ioc_fwstate == BFI_IOC_UNINIT) ?
Rasesh Mody79ea6c82011-04-14 08:05:18 +00001856 false : bfa_ioc_fwver_valid(ioc, boot_env);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001857
1858 if (!fwvalid) {
Rasesh Modyc107ba12013-12-17 17:07:41 -08001859 if (bfa_ioc_boot(ioc, BFI_FWBOOT_TYPE_NORMAL, boot_env) ==
1860 BFA_STATUS_OK)
1861 bfa_ioc_poll_fwinit(ioc);
1862
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001863 return;
1864 }
1865
1866 /**
1867 * If hardware initialization is in progress (initialized by other IOC),
1868 * just wait for an initialization completion interrupt.
1869 */
1870 if (ioc_fwstate == BFI_IOC_INITING) {
Rasesh Mody078086f2011-08-08 16:21:39 +00001871 bfa_ioc_poll_fwinit(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001872 return;
1873 }
1874
1875 /**
1876 * If IOC function is disabled and firmware version is same,
1877 * just re-enable IOC.
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001878 */
Rasesh Mody2c7d3822010-12-23 21:45:06 +00001879 if (ioc_fwstate == BFI_IOC_DISABLED || ioc_fwstate == BFI_IOC_OP) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001880 /**
1881 * When using MSI-X any pending firmware ready event should
1882 * be flushed. Otherwise MSI-X interrupts are not delivered.
1883 */
1884 bfa_ioc_msgflush(ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001885 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_FWREADY);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001886 return;
1887 }
1888
1889 /**
1890 * Initialize the h/w for any other states.
1891 */
Rasesh Modyc107ba12013-12-17 17:07:41 -08001892 if (bfa_ioc_boot(ioc, BFI_FWBOOT_TYPE_NORMAL, boot_env) ==
1893 BFA_STATUS_OK)
1894 bfa_ioc_poll_fwinit(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001895}
1896
1897void
Ivan Veceraad24d6f2015-06-11 15:52:30 +02001898bfa_nw_ioc_timeout(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001899{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001900 bfa_fsm_send_event(ioc, IOC_E_TIMEOUT);
1901}
1902
Rasesh Mody8a891422010-08-25 23:00:27 -07001903static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001904bfa_ioc_mbox_send(struct bfa_ioc *ioc, void *ioc_msg, int len)
1905{
1906 u32 *msgp = (u32 *) ioc_msg;
1907 u32 i;
1908
1909 BUG_ON(!(len <= BFI_IOC_MSGLEN_MAX));
1910
1911 /*
1912 * first write msg to mailbox registers
1913 */
1914 for (i = 0; i < len / sizeof(u32); i++)
1915 writel(cpu_to_le32(msgp[i]),
1916 ioc->ioc_regs.hfn_mbox + i * sizeof(u32));
1917
1918 for (; i < BFI_IOC_MSGLEN_MAX / sizeof(u32); i++)
1919 writel(0, ioc->ioc_regs.hfn_mbox + i * sizeof(u32));
1920
1921 /*
1922 * write 1 to mailbox CMD to trigger LPU event
1923 */
1924 writel(1, ioc->ioc_regs.hfn_mbox_cmd);
1925 (void) readl(ioc->ioc_regs.hfn_mbox_cmd);
1926}
1927
1928static void
1929bfa_ioc_send_enable(struct bfa_ioc *ioc)
1930{
1931 struct bfi_ioc_ctrl_req enable_req;
1932 struct timeval tv;
1933
1934 bfi_h2i_set(enable_req.mh, BFI_MC_IOC, BFI_IOC_H2I_ENABLE_REQ,
1935 bfa_ioc_portid(ioc));
Rasesh Mody078086f2011-08-08 16:21:39 +00001936 enable_req.clscode = htons(ioc->clscode);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001937 do_gettimeofday(&tv);
1938 enable_req.tv_sec = ntohl(tv.tv_sec);
1939 bfa_ioc_mbox_send(ioc, &enable_req, sizeof(struct bfi_ioc_ctrl_req));
1940}
1941
1942static void
1943bfa_ioc_send_disable(struct bfa_ioc *ioc)
1944{
1945 struct bfi_ioc_ctrl_req disable_req;
1946
1947 bfi_h2i_set(disable_req.mh, BFI_MC_IOC, BFI_IOC_H2I_DISABLE_REQ,
1948 bfa_ioc_portid(ioc));
1949 bfa_ioc_mbox_send(ioc, &disable_req, sizeof(struct bfi_ioc_ctrl_req));
1950}
1951
1952static void
1953bfa_ioc_send_getattr(struct bfa_ioc *ioc)
1954{
1955 struct bfi_ioc_getattr_req attr_req;
1956
1957 bfi_h2i_set(attr_req.mh, BFI_MC_IOC, BFI_IOC_H2I_GETATTR_REQ,
1958 bfa_ioc_portid(ioc));
1959 bfa_dma_be_addr_set(attr_req.attr_addr, ioc->attr_dma.pa);
1960 bfa_ioc_mbox_send(ioc, &attr_req, sizeof(attr_req));
1961}
1962
1963void
Ivan Veceraad24d6f2015-06-11 15:52:30 +02001964bfa_nw_ioc_hb_check(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001965{
Ivan Veceraad24d6f2015-06-11 15:52:30 +02001966 u32 hb_count;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001967
1968 hb_count = readl(ioc->ioc_regs.heartbeat);
1969 if (ioc->hb_count == hb_count) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001970 bfa_ioc_recover(ioc);
1971 return;
1972 } else {
1973 ioc->hb_count = hb_count;
1974 }
1975
1976 bfa_ioc_mbox_poll(ioc);
1977 mod_timer(&ioc->hb_timer, jiffies +
1978 msecs_to_jiffies(BFA_IOC_HB_TOV));
1979}
1980
1981static void
1982bfa_ioc_hb_monitor(struct bfa_ioc *ioc)
1983{
1984 ioc->hb_count = readl(ioc->ioc_regs.heartbeat);
1985 mod_timer(&ioc->hb_timer, jiffies +
1986 msecs_to_jiffies(BFA_IOC_HB_TOV));
1987}
1988
1989static void
1990bfa_ioc_hb_stop(struct bfa_ioc *ioc)
1991{
1992 del_timer(&ioc->hb_timer);
1993}
1994
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001995/* Initiate a full firmware download. */
Rasesh Modyc107ba12013-12-17 17:07:41 -08001996static enum bfa_status
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001997bfa_ioc_download_fw(struct bfa_ioc *ioc, u32 boot_type,
Rasesh Mody79ea6c82011-04-14 08:05:18 +00001998 u32 boot_env)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001999{
2000 u32 *fwimg;
David S. Miller58598542011-04-17 16:51:36 -07002001 u32 pgnum;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002002 u32 loff = 0;
2003 u32 chunkno = 0;
2004 u32 i;
Rasesh Mody078086f2011-08-08 16:21:39 +00002005 u32 asicmode;
Rasesh Modyc107ba12013-12-17 17:07:41 -08002006 u32 fwimg_size;
2007 u32 fwimg_buf[BFI_FLASH_CHUNK_SZ_WORDS];
2008 enum bfa_status status;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002009
Rasesh Modyc107ba12013-12-17 17:07:41 -08002010 if (boot_env == BFI_FWBOOT_ENV_OS &&
2011 boot_type == BFI_FWBOOT_TYPE_FLASH) {
2012 fwimg_size = BFI_FLASH_IMAGE_SZ/sizeof(u32);
2013
2014 status = bfa_nw_ioc_flash_img_get_chnk(ioc,
2015 BFA_IOC_FLASH_CHUNK_ADDR(chunkno), fwimg_buf);
2016 if (status != BFA_STATUS_OK)
2017 return status;
2018
2019 fwimg = fwimg_buf;
2020 } else {
2021 fwimg_size = bfa_cb_image_get_size(bfa_ioc_asic_gen(ioc));
2022 fwimg = bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc),
2023 BFA_IOC_FLASH_CHUNK_ADDR(chunkno));
2024 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002025
2026 pgnum = bfa_ioc_smem_pgnum(ioc, loff);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002027
2028 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
2029
Rasesh Modyc107ba12013-12-17 17:07:41 -08002030 for (i = 0; i < fwimg_size; i++) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002031 if (BFA_IOC_FLASH_CHUNK_NO(i) != chunkno) {
2032 chunkno = BFA_IOC_FLASH_CHUNK_NO(i);
Rasesh Modyc107ba12013-12-17 17:07:41 -08002033 if (boot_env == BFI_FWBOOT_ENV_OS &&
2034 boot_type == BFI_FWBOOT_TYPE_FLASH) {
2035 status = bfa_nw_ioc_flash_img_get_chnk(ioc,
2036 BFA_IOC_FLASH_CHUNK_ADDR(chunkno),
2037 fwimg_buf);
2038 if (status != BFA_STATUS_OK)
2039 return status;
2040
2041 fwimg = fwimg_buf;
2042 } else {
2043 fwimg = bfa_cb_image_get_chunk(
2044 bfa_ioc_asic_gen(ioc),
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002045 BFA_IOC_FLASH_CHUNK_ADDR(chunkno));
Rasesh Modyc107ba12013-12-17 17:07:41 -08002046 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002047 }
2048
2049 /**
2050 * write smem
2051 */
Ivan Veceraebb56d32015-06-17 18:41:58 +02002052 writel(swab32(fwimg[BFA_IOC_FLASH_OFFSET_IN_CHUNK(i)]),
2053 ioc->ioc_regs.smem_page_start + loff);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002054
2055 loff += sizeof(u32);
2056
2057 /**
2058 * handle page offset wrap around
2059 */
2060 loff = PSS_SMEM_PGOFF(loff);
2061 if (loff == 0) {
2062 pgnum++;
2063 writel(pgnum,
2064 ioc->ioc_regs.host_page_num_fn);
2065 }
2066 }
2067
2068 writel(bfa_ioc_smem_pgnum(ioc, 0),
2069 ioc->ioc_regs.host_page_num_fn);
2070
2071 /*
Rasesh Mody078086f2011-08-08 16:21:39 +00002072 * Set boot type, env and device mode at the end.
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002073 */
Rasesh Modyc107ba12013-12-17 17:07:41 -08002074 if (boot_env == BFI_FWBOOT_ENV_OS &&
2075 boot_type == BFI_FWBOOT_TYPE_FLASH) {
2076 boot_type = BFI_FWBOOT_TYPE_NORMAL;
2077 }
Rasesh Mody078086f2011-08-08 16:21:39 +00002078 asicmode = BFI_FWBOOT_DEVMODE(ioc->asic_gen, ioc->asic_mode,
2079 ioc->port0_mode, ioc->port1_mode);
2080 writel(asicmode, ((ioc->ioc_regs.smem_page_start)
2081 + BFI_FWBOOT_DEVMODE_OFF));
Rasesh Mody79ea6c82011-04-14 08:05:18 +00002082 writel(boot_type, ((ioc->ioc_regs.smem_page_start)
Rasesh Mody078086f2011-08-08 16:21:39 +00002083 + (BFI_FWBOOT_TYPE_OFF)));
Rasesh Mody79ea6c82011-04-14 08:05:18 +00002084 writel(boot_env, ((ioc->ioc_regs.smem_page_start)
Rasesh Mody078086f2011-08-08 16:21:39 +00002085 + (BFI_FWBOOT_ENV_OFF)));
Rasesh Modyc107ba12013-12-17 17:07:41 -08002086 return BFA_STATUS_OK;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002087}
2088
2089static void
2090bfa_ioc_reset(struct bfa_ioc *ioc, bool force)
2091{
2092 bfa_ioc_hwinit(ioc, force);
2093}
2094
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002095/* BFA ioc enable reply by firmware */
Rasesh Mody078086f2011-08-08 16:21:39 +00002096static void
2097bfa_ioc_enable_reply(struct bfa_ioc *ioc, enum bfa_mode port_mode,
2098 u8 cap_bm)
2099{
2100 struct bfa_iocpf *iocpf = &ioc->iocpf;
2101
2102 ioc->port_mode = ioc->port_mode_cfg = port_mode;
2103 ioc->ad_cap_bm = cap_bm;
2104 bfa_fsm_send_event(iocpf, IOCPF_E_FWRSP_ENABLE);
2105}
2106
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002107/* Update BFA configuration from firmware configuration. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002108static void
2109bfa_ioc_getattr_reply(struct bfa_ioc *ioc)
2110{
2111 struct bfi_ioc_attr *attr = ioc->attr;
2112
2113 attr->adapter_prop = ntohl(attr->adapter_prop);
2114 attr->card_type = ntohl(attr->card_type);
2115 attr->maxfrsize = ntohs(attr->maxfrsize);
2116
2117 bfa_fsm_send_event(ioc, IOC_E_FWRSP_GETATTR);
2118}
2119
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002120/* Attach time initialization of mbox logic. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002121static void
2122bfa_ioc_mbox_attach(struct bfa_ioc *ioc)
2123{
2124 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2125 int mc;
2126
2127 INIT_LIST_HEAD(&mod->cmd_q);
2128 for (mc = 0; mc < BFI_MC_MAX; mc++) {
2129 mod->mbhdlr[mc].cbfn = NULL;
2130 mod->mbhdlr[mc].cbarg = ioc->bfa;
2131 }
2132}
2133
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002134/* Mbox poll timer -- restarts any pending mailbox requests. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002135static void
2136bfa_ioc_mbox_poll(struct bfa_ioc *ioc)
2137{
2138 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2139 struct bfa_mbox_cmd *cmd;
Rasesh Mody078086f2011-08-08 16:21:39 +00002140 bfa_mbox_cmd_cbfn_t cbfn;
2141 void *cbarg;
2142 u32 stat;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002143
2144 /**
2145 * If no command pending, do nothing
2146 */
2147 if (list_empty(&mod->cmd_q))
2148 return;
2149
2150 /**
2151 * If previous command is not yet fetched by firmware, do nothing
2152 */
2153 stat = readl(ioc->ioc_regs.hfn_mbox_cmd);
2154 if (stat)
2155 return;
2156
2157 /**
2158 * Enqueue command to firmware.
2159 */
Ivan Vecera2b26fb92015-06-11 15:52:28 +02002160 cmd = list_first_entry(&mod->cmd_q, struct bfa_mbox_cmd, qe);
2161 list_del(&cmd->qe);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002162 bfa_ioc_mbox_send(ioc, cmd->msg, sizeof(cmd->msg));
Rasesh Mody078086f2011-08-08 16:21:39 +00002163
2164 /**
2165 * Give a callback to the client, indicating that the command is sent
2166 */
2167 if (cmd->cbfn) {
2168 cbfn = cmd->cbfn;
2169 cbarg = cmd->cbarg;
2170 cmd->cbfn = NULL;
2171 cbfn(cbarg);
2172 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002173}
2174
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002175/* Cleanup any pending requests. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002176static void
Rasesh Modyfdad4002011-07-22 08:07:45 +00002177bfa_ioc_mbox_flush(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002178{
2179 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2180 struct bfa_mbox_cmd *cmd;
2181
Ivan Vecera2b26fb92015-06-11 15:52:28 +02002182 while (!list_empty(&mod->cmd_q)) {
2183 cmd = list_first_entry(&mod->cmd_q, struct bfa_mbox_cmd, qe);
2184 list_del(&cmd->qe);
2185 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002186}
2187
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00002188/**
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002189 * bfa_nw_ioc_smem_read - Read data from SMEM to host through PCI memmap
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00002190 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002191 * @ioc: memory for IOC
2192 * @tbuf: app memory to store data from smem
2193 * @soff: smem offset
2194 * @sz: size of smem in bytes
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00002195 */
2196static int
2197bfa_nw_ioc_smem_read(struct bfa_ioc *ioc, void *tbuf, u32 soff, u32 sz)
2198{
2199 u32 pgnum, loff, r32;
2200 int i, len;
2201 u32 *buf = tbuf;
2202
2203 pgnum = PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, soff);
2204 loff = PSS_SMEM_PGOFF(soff);
2205
2206 /*
2207 * Hold semaphore to serialize pll init and fwtrc.
2208 */
Ivan Vecera93719d262015-06-11 15:52:25 +02002209 if (!bfa_nw_ioc_sem_get(ioc->ioc_regs.ioc_init_sem_reg))
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00002210 return 1;
2211
2212 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
2213
2214 len = sz/sizeof(u32);
2215 for (i = 0; i < len; i++) {
Ivan Veceraebb56d32015-06-17 18:41:58 +02002216 r32 = swab32(readl(loff + ioc->ioc_regs.smem_page_start));
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00002217 buf[i] = be32_to_cpu(r32);
2218 loff += sizeof(u32);
2219
2220 /**
2221 * handle page offset wrap around
2222 */
2223 loff = PSS_SMEM_PGOFF(loff);
2224 if (loff == 0) {
2225 pgnum++;
2226 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
2227 }
2228 }
2229
2230 writel(PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, 0),
2231 ioc->ioc_regs.host_page_num_fn);
2232
2233 /*
2234 * release semaphore
2235 */
2236 readl(ioc->ioc_regs.ioc_init_sem_reg);
2237 writel(1, ioc->ioc_regs.ioc_init_sem_reg);
2238 return 0;
2239}
2240
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002241/* Retrieve saved firmware trace from a prior IOC failure. */
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00002242int
2243bfa_nw_ioc_debug_fwtrc(struct bfa_ioc *ioc, void *trcdata, int *trclen)
2244{
2245 u32 loff = BFI_IOC_TRC_OFF + BNA_DBG_FWTRC_LEN * ioc->port_id;
2246 int tlen, status = 0;
2247
2248 tlen = *trclen;
2249 if (tlen > BNA_DBG_FWTRC_LEN)
2250 tlen = BNA_DBG_FWTRC_LEN;
2251
2252 status = bfa_nw_ioc_smem_read(ioc, trcdata, loff, tlen);
2253 *trclen = tlen;
2254 return status;
2255}
2256
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002257/* Save firmware trace if configured. */
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00002258static void
2259bfa_nw_ioc_debug_save_ftrc(struct bfa_ioc *ioc)
2260{
2261 int tlen;
2262
2263 if (ioc->dbg_fwsave_once) {
Ivan Vecera93719d262015-06-11 15:52:25 +02002264 ioc->dbg_fwsave_once = false;
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00002265 if (ioc->dbg_fwsave_len) {
2266 tlen = ioc->dbg_fwsave_len;
2267 bfa_nw_ioc_debug_fwtrc(ioc, ioc->dbg_fwsave, &tlen);
2268 }
2269 }
2270}
2271
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002272/* Retrieve saved firmware trace from a prior IOC failure. */
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00002273int
2274bfa_nw_ioc_debug_fwsave(struct bfa_ioc *ioc, void *trcdata, int *trclen)
2275{
2276 int tlen;
2277
2278 if (ioc->dbg_fwsave_len == 0)
2279 return BFA_STATUS_ENOFSAVE;
2280
2281 tlen = *trclen;
2282 if (tlen > ioc->dbg_fwsave_len)
2283 tlen = ioc->dbg_fwsave_len;
2284
2285 memcpy(trcdata, ioc->dbg_fwsave, tlen);
2286 *trclen = tlen;
2287 return BFA_STATUS_OK;
2288}
2289
Rasesh Mody1d32f762010-12-23 21:45:09 +00002290static void
2291bfa_ioc_fail_notify(struct bfa_ioc *ioc)
2292{
Rasesh Mody1d32f762010-12-23 21:45:09 +00002293 /**
2294 * Notify driver and common modules registered for notification.
2295 */
2296 ioc->cbfn->hbfail_cbfn(ioc->bfa);
Rasesh Modybd5a92e2011-07-22 08:07:42 +00002297 bfa_ioc_event_notify(ioc, BFA_IOC_E_FAILED);
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00002298 bfa_nw_ioc_debug_save_ftrc(ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +00002299}
2300
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002301/* IOCPF to IOC interface */
Rasesh Mody1d32f762010-12-23 21:45:09 +00002302static void
2303bfa_ioc_pf_enabled(struct bfa_ioc *ioc)
2304{
2305 bfa_fsm_send_event(ioc, IOC_E_ENABLED);
2306}
2307
2308static void
2309bfa_ioc_pf_disabled(struct bfa_ioc *ioc)
2310{
2311 bfa_fsm_send_event(ioc, IOC_E_DISABLED);
2312}
2313
2314static void
Rasesh Mody1d32f762010-12-23 21:45:09 +00002315bfa_ioc_pf_failed(struct bfa_ioc *ioc)
2316{
Rasesh Modyf374b362011-07-22 08:07:46 +00002317 bfa_fsm_send_event(ioc, IOC_E_PFFAILED);
Rasesh Mody1d32f762010-12-23 21:45:09 +00002318}
2319
2320static void
Rasesh Mody078086f2011-08-08 16:21:39 +00002321bfa_ioc_pf_hwfailed(struct bfa_ioc *ioc)
2322{
2323 bfa_fsm_send_event(ioc, IOC_E_HWFAILED);
2324}
2325
2326static void
Rasesh Mody1d32f762010-12-23 21:45:09 +00002327bfa_ioc_pf_fwmismatch(struct bfa_ioc *ioc)
2328{
2329 /**
2330 * Provide enable completion callback and AEN notification.
2331 */
2332 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
2333}
2334
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002335/* IOC public */
Rasesh Mody8a891422010-08-25 23:00:27 -07002336static enum bfa_status
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002337bfa_ioc_pll_init(struct bfa_ioc *ioc)
2338{
2339 /*
2340 * Hold semaphore so that nobody can access the chip during init.
2341 */
Rasesh Mody8a891422010-08-25 23:00:27 -07002342 bfa_nw_ioc_sem_get(ioc->ioc_regs.ioc_init_sem_reg);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002343
2344 bfa_ioc_pll_init_asic(ioc);
2345
2346 ioc->pllinit = true;
Jing Huange491c772012-04-04 05:42:08 +00002347
2348 /* Initialize LMEM */
2349 bfa_ioc_lmem_init(ioc);
2350
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002351 /*
2352 * release semaphore.
2353 */
Rasesh Mody8a891422010-08-25 23:00:27 -07002354 bfa_nw_ioc_sem_release(ioc->ioc_regs.ioc_init_sem_reg);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002355
2356 return BFA_STATUS_OK;
2357}
2358
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002359/* Interface used by diag module to do firmware boot with memory test
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002360 * as the entry vector.
2361 */
Rasesh Modyc107ba12013-12-17 17:07:41 -08002362static enum bfa_status
Rasesh Mody078086f2011-08-08 16:21:39 +00002363bfa_ioc_boot(struct bfa_ioc *ioc, enum bfi_fwboot_type boot_type,
2364 u32 boot_env)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002365{
Rasesh Modyc107ba12013-12-17 17:07:41 -08002366 struct bfi_ioc_image_hdr *drv_fwhdr;
2367 enum bfa_status status;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002368 bfa_ioc_stats(ioc, ioc_boots);
2369
2370 if (bfa_ioc_pll_init(ioc) != BFA_STATUS_OK)
Rasesh Modyc107ba12013-12-17 17:07:41 -08002371 return BFA_STATUS_FAILED;
2372 if (boot_env == BFI_FWBOOT_ENV_OS &&
2373 boot_type == BFI_FWBOOT_TYPE_NORMAL) {
2374 drv_fwhdr = (struct bfi_ioc_image_hdr *)
2375 bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc), 0);
2376 /* Work with Flash iff flash f/w is better than driver f/w.
2377 * Otherwise push drivers firmware.
2378 */
2379 if (bfa_ioc_flash_fwver_cmp(ioc, drv_fwhdr) ==
2380 BFI_IOC_IMG_VER_BETTER)
2381 boot_type = BFI_FWBOOT_TYPE_FLASH;
2382 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002383
2384 /**
2385 * Initialize IOC state of all functions on a chip reset.
2386 */
Rasesh Mody078086f2011-08-08 16:21:39 +00002387 if (boot_type == BFI_FWBOOT_TYPE_MEMTEST) {
Rasesh Mody41ed9032013-12-17 17:07:32 -08002388 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_MEMTEST);
2389 bfa_ioc_set_alt_ioc_fwstate(ioc, BFI_IOC_MEMTEST);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002390 } else {
Rasesh Mody41ed9032013-12-17 17:07:32 -08002391 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_INITING);
2392 bfa_ioc_set_alt_ioc_fwstate(ioc, BFI_IOC_INITING);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002393 }
2394
2395 bfa_ioc_msgflush(ioc);
Rasesh Modyc107ba12013-12-17 17:07:41 -08002396 status = bfa_ioc_download_fw(ioc, boot_type, boot_env);
2397 if (status == BFA_STATUS_OK)
2398 bfa_ioc_lpu_start(ioc);
2399 else
Ivan Vecera4818e8562015-05-28 23:10:08 +02002400 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_TIMEOUT);
Rasesh Modyc107ba12013-12-17 17:07:41 -08002401
2402 return status;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002403}
2404
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002405/* Enable/disable IOC failure auto recovery. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002406void
Rasesh Mody8a891422010-08-25 23:00:27 -07002407bfa_nw_ioc_auto_recover(bool auto_recover)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002408{
Rasesh Mody8a891422010-08-25 23:00:27 -07002409 bfa_nw_auto_recover = auto_recover;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002410}
2411
Rasesh Mody078086f2011-08-08 16:21:39 +00002412static bool
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002413bfa_ioc_msgget(struct bfa_ioc *ioc, void *mbmsg)
2414{
2415 u32 *msgp = mbmsg;
2416 u32 r32;
2417 int i;
2418
Rasesh Mody078086f2011-08-08 16:21:39 +00002419 r32 = readl(ioc->ioc_regs.lpu_mbox_cmd);
2420 if ((r32 & 1) == 0)
2421 return false;
2422
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002423 /**
2424 * read the MBOX msg
2425 */
2426 for (i = 0; i < (sizeof(union bfi_ioc_i2h_msg_u) / sizeof(u32));
2427 i++) {
2428 r32 = readl(ioc->ioc_regs.lpu_mbox +
2429 i * sizeof(u32));
2430 msgp[i] = htonl(r32);
2431 }
2432
2433 /**
2434 * turn off mailbox interrupt by clearing mailbox status
2435 */
2436 writel(1, ioc->ioc_regs.lpu_mbox_cmd);
2437 readl(ioc->ioc_regs.lpu_mbox_cmd);
Rasesh Mody078086f2011-08-08 16:21:39 +00002438
2439 return true;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002440}
2441
Rasesh Mody8a891422010-08-25 23:00:27 -07002442static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002443bfa_ioc_isr(struct bfa_ioc *ioc, struct bfi_mbmsg *m)
2444{
2445 union bfi_ioc_i2h_msg_u *msg;
Rasesh Mody1d32f762010-12-23 21:45:09 +00002446 struct bfa_iocpf *iocpf = &ioc->iocpf;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002447
2448 msg = (union bfi_ioc_i2h_msg_u *) m;
2449
2450 bfa_ioc_stats(ioc, ioc_isrs);
2451
2452 switch (msg->mh.msg_id) {
2453 case BFI_IOC_I2H_HBEAT:
2454 break;
2455
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002456 case BFI_IOC_I2H_ENABLE_REPLY:
Rasesh Mody078086f2011-08-08 16:21:39 +00002457 bfa_ioc_enable_reply(ioc,
2458 (enum bfa_mode)msg->fw_event.port_mode,
2459 msg->fw_event.cap_bm);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002460 break;
2461
2462 case BFI_IOC_I2H_DISABLE_REPLY:
Rasesh Mody1d32f762010-12-23 21:45:09 +00002463 bfa_fsm_send_event(iocpf, IOCPF_E_FWRSP_DISABLE);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002464 break;
2465
2466 case BFI_IOC_I2H_GETATTR_REPLY:
2467 bfa_ioc_getattr_reply(ioc);
2468 break;
2469
2470 default:
2471 BUG_ON(1);
2472 }
2473}
2474
2475/**
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002476 * bfa_nw_ioc_attach - IOC attach time initialization and setup.
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002477 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002478 * @ioc: memory for IOC
2479 * @bfa: driver instance structure
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002480 */
2481void
Rasesh Mody8a891422010-08-25 23:00:27 -07002482bfa_nw_ioc_attach(struct bfa_ioc *ioc, void *bfa, struct bfa_ioc_cbfn *cbfn)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002483{
2484 ioc->bfa = bfa;
2485 ioc->cbfn = cbfn;
2486 ioc->fcmode = false;
2487 ioc->pllinit = false;
2488 ioc->dbg_fwsave_once = true;
Rasesh Mody1d32f762010-12-23 21:45:09 +00002489 ioc->iocpf.ioc = ioc;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002490
2491 bfa_ioc_mbox_attach(ioc);
Rasesh Modybd5a92e2011-07-22 08:07:42 +00002492 INIT_LIST_HEAD(&ioc->notify_q);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002493
Rasesh Mody1d32f762010-12-23 21:45:09 +00002494 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
2495 bfa_fsm_send_event(ioc, IOC_E_RESET);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002496}
2497
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002498/* Driver detach time IOC cleanup. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002499void
Rasesh Mody8a891422010-08-25 23:00:27 -07002500bfa_nw_ioc_detach(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002501{
2502 bfa_fsm_send_event(ioc, IOC_E_DETACH);
Rasesh Mody078086f2011-08-08 16:21:39 +00002503
2504 /* Done with detach, empty the notify_q. */
2505 INIT_LIST_HEAD(&ioc->notify_q);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002506}
2507
2508/**
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002509 * bfa_nw_ioc_pci_init - Setup IOC PCI properties.
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002510 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002511 * @pcidev: PCI device information for this IOC
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002512 */
2513void
Rasesh Mody8a891422010-08-25 23:00:27 -07002514bfa_nw_ioc_pci_init(struct bfa_ioc *ioc, struct bfa_pcidev *pcidev,
Rasesh Mody078086f2011-08-08 16:21:39 +00002515 enum bfi_pcifn_class clscode)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002516{
Rasesh Mody078086f2011-08-08 16:21:39 +00002517 ioc->clscode = clscode;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002518 ioc->pcidev = *pcidev;
Rasesh Mody078086f2011-08-08 16:21:39 +00002519
2520 /**
2521 * Initialize IOC and device personality
2522 */
2523 ioc->port0_mode = ioc->port1_mode = BFI_PORT_MODE_FC;
2524 ioc->asic_mode = BFI_ASIC_MODE_FC;
2525
2526 switch (pcidev->device_id) {
2527 case PCI_DEVICE_ID_BROCADE_CT:
2528 ioc->asic_gen = BFI_ASIC_GEN_CT;
2529 ioc->port0_mode = ioc->port1_mode = BFI_PORT_MODE_ETH;
2530 ioc->asic_mode = BFI_ASIC_MODE_ETH;
2531 ioc->port_mode = ioc->port_mode_cfg = BFA_MODE_CNA;
2532 ioc->ad_cap_bm = BFA_CM_CNA;
2533 break;
2534
Rasesh Mody586b28162011-09-27 10:39:08 +00002535 case BFA_PCI_DEVICE_ID_CT2:
2536 ioc->asic_gen = BFI_ASIC_GEN_CT2;
2537 if (clscode == BFI_PCIFN_CLASS_FC &&
2538 pcidev->ssid == BFA_PCI_CT2_SSID_FC) {
2539 ioc->asic_mode = BFI_ASIC_MODE_FC16;
2540 ioc->fcmode = true;
2541 ioc->port_mode = ioc->port_mode_cfg = BFA_MODE_HBA;
2542 ioc->ad_cap_bm = BFA_CM_HBA;
2543 } else {
2544 ioc->port0_mode = ioc->port1_mode = BFI_PORT_MODE_ETH;
2545 ioc->asic_mode = BFI_ASIC_MODE_ETH;
2546 if (pcidev->ssid == BFA_PCI_CT2_SSID_FCoE) {
2547 ioc->port_mode =
2548 ioc->port_mode_cfg = BFA_MODE_CNA;
2549 ioc->ad_cap_bm = BFA_CM_CNA;
2550 } else {
2551 ioc->port_mode =
2552 ioc->port_mode_cfg = BFA_MODE_NIC;
2553 ioc->ad_cap_bm = BFA_CM_NIC;
2554 }
2555 }
2556 break;
2557
Rasesh Mody078086f2011-08-08 16:21:39 +00002558 default:
2559 BUG_ON(1);
2560 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002561
Rasesh Modybe3a84d2011-09-27 10:39:05 +00002562 /**
2563 * Set asic specific interfaces.
2564 */
2565 if (ioc->asic_gen == BFI_ASIC_GEN_CT)
2566 bfa_nw_ioc_set_ct_hwif(ioc);
Rasesh Mody70f1438122011-10-04 23:04:01 -04002567 else {
2568 WARN_ON(ioc->asic_gen != BFI_ASIC_GEN_CT2);
Rasesh Modybe3a84d2011-09-27 10:39:05 +00002569 bfa_nw_ioc_set_ct2_hwif(ioc);
Rasesh Mody70f1438122011-10-04 23:04:01 -04002570 bfa_nw_ioc_ct2_poweron(ioc);
2571 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002572
2573 bfa_ioc_map_port(ioc);
2574 bfa_ioc_reg_init(ioc);
2575}
2576
2577/**
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002578 * bfa_nw_ioc_mem_claim - Initialize IOC dma memory
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002579 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002580 * @dm_kva: kernel virtual address of IOC dma memory
2581 * @dm_pa: physical address of IOC dma memory
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002582 */
2583void
Rasesh Mody8a891422010-08-25 23:00:27 -07002584bfa_nw_ioc_mem_claim(struct bfa_ioc *ioc, u8 *dm_kva, u64 dm_pa)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002585{
2586 /**
2587 * dma memory for firmware attribute
2588 */
2589 ioc->attr_dma.kva = dm_kva;
2590 ioc->attr_dma.pa = dm_pa;
2591 ioc->attr = (struct bfi_ioc_attr *) dm_kva;
2592}
2593
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002594/* Return size of dma memory required. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002595u32
Rasesh Mody8a891422010-08-25 23:00:27 -07002596bfa_nw_ioc_meminfo(void)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002597{
2598 return roundup(sizeof(struct bfi_ioc_attr), BFA_DMA_ALIGN_SZ);
2599}
2600
2601void
Rasesh Mody8a891422010-08-25 23:00:27 -07002602bfa_nw_ioc_enable(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002603{
2604 bfa_ioc_stats(ioc, ioc_enables);
2605 ioc->dbg_fwsave_once = true;
2606
2607 bfa_fsm_send_event(ioc, IOC_E_ENABLE);
2608}
2609
2610void
Rasesh Mody8a891422010-08-25 23:00:27 -07002611bfa_nw_ioc_disable(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002612{
2613 bfa_ioc_stats(ioc, ioc_disables);
2614 bfa_fsm_send_event(ioc, IOC_E_DISABLE);
2615}
2616
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002617/* Initialize memory for saving firmware trace. */
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00002618void
2619bfa_nw_ioc_debug_memclaim(struct bfa_ioc *ioc, void *dbg_fwsave)
2620{
2621 ioc->dbg_fwsave = dbg_fwsave;
2622 ioc->dbg_fwsave_len = ioc->iocpf.auto_recover ? BNA_DBG_FWTRC_LEN : 0;
2623}
2624
Rasesh Mody8a891422010-08-25 23:00:27 -07002625static u32
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002626bfa_ioc_smem_pgnum(struct bfa_ioc *ioc, u32 fmaddr)
2627{
2628 return PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, fmaddr);
2629}
2630
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002631/* Register mailbox message handler function, to be called by common modules */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002632void
Rasesh Mody8a891422010-08-25 23:00:27 -07002633bfa_nw_ioc_mbox_regisr(struct bfa_ioc *ioc, enum bfi_mclass mc,
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002634 bfa_ioc_mbox_mcfunc_t cbfn, void *cbarg)
2635{
2636 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2637
2638 mod->mbhdlr[mc].cbfn = cbfn;
2639 mod->mbhdlr[mc].cbarg = cbarg;
2640}
2641
2642/**
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002643 * bfa_nw_ioc_mbox_queue - Queue a mailbox command request to firmware.
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002644 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002645 * @ioc: IOC instance
2646 * @cmd: Mailbox command
2647 *
2648 * Waits if mailbox is busy. Responsibility of caller to serialize
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002649 */
Rasesh Modyaf027a32011-08-08 16:21:35 +00002650bool
2651bfa_nw_ioc_mbox_queue(struct bfa_ioc *ioc, struct bfa_mbox_cmd *cmd,
2652 bfa_mbox_cmd_cbfn_t cbfn, void *cbarg)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002653{
2654 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2655 u32 stat;
2656
Rasesh Modyaf027a32011-08-08 16:21:35 +00002657 cmd->cbfn = cbfn;
2658 cmd->cbarg = cbarg;
2659
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002660 /**
2661 * If a previous command is pending, queue new command
2662 */
2663 if (!list_empty(&mod->cmd_q)) {
2664 list_add_tail(&cmd->qe, &mod->cmd_q);
Rasesh Modyaf027a32011-08-08 16:21:35 +00002665 return true;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002666 }
2667
2668 /**
2669 * If mailbox is busy, queue command for poll timer
2670 */
2671 stat = readl(ioc->ioc_regs.hfn_mbox_cmd);
2672 if (stat) {
2673 list_add_tail(&cmd->qe, &mod->cmd_q);
Rasesh Modyaf027a32011-08-08 16:21:35 +00002674 return true;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002675 }
2676
2677 /**
2678 * mailbox is free -- queue command to firmware
2679 */
2680 bfa_ioc_mbox_send(ioc, cmd->msg, sizeof(cmd->msg));
Rasesh Modybd5a92e2011-07-22 08:07:42 +00002681
Rasesh Modyaf027a32011-08-08 16:21:35 +00002682 return false;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002683}
2684
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002685/* Handle mailbox interrupts */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002686void
Rasesh Mody8a891422010-08-25 23:00:27 -07002687bfa_nw_ioc_mbox_isr(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002688{
2689 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2690 struct bfi_mbmsg m;
2691 int mc;
2692
Rasesh Mody078086f2011-08-08 16:21:39 +00002693 if (bfa_ioc_msgget(ioc, &m)) {
2694 /**
2695 * Treat IOC message class as special.
2696 */
2697 mc = m.mh.msg_class;
2698 if (mc == BFI_MC_IOC) {
2699 bfa_ioc_isr(ioc, &m);
2700 return;
2701 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002702
Rasesh Mody078086f2011-08-08 16:21:39 +00002703 if ((mc >= BFI_MC_MAX) || (mod->mbhdlr[mc].cbfn == NULL))
2704 return;
2705
2706 mod->mbhdlr[mc].cbfn(mod->mbhdlr[mc].cbarg, &m);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002707 }
2708
Rasesh Mody078086f2011-08-08 16:21:39 +00002709 bfa_ioc_lpu_read_stat(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002710
Rasesh Mody078086f2011-08-08 16:21:39 +00002711 /**
2712 * Try to send pending mailbox commands
2713 */
2714 bfa_ioc_mbox_poll(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002715}
2716
2717void
Rasesh Mody8a891422010-08-25 23:00:27 -07002718bfa_nw_ioc_error_isr(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002719{
Rasesh Mody9b08a4f2011-07-22 08:07:48 +00002720 bfa_ioc_stats(ioc, ioc_hbfails);
2721 bfa_ioc_stats_hb_count(ioc, ioc->hb_count);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002722 bfa_fsm_send_event(ioc, IOC_E_HWERROR);
2723}
2724
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002725/* return true if IOC is disabled */
Rasesh Modybd5a92e2011-07-22 08:07:42 +00002726bool
2727bfa_nw_ioc_is_disabled(struct bfa_ioc *ioc)
2728{
2729 return bfa_fsm_cmp_state(ioc, bfa_ioc_sm_disabling) ||
2730 bfa_fsm_cmp_state(ioc, bfa_ioc_sm_disabled);
2731}
2732
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002733/* return true if IOC is operational */
Krishna Gudipati72a97302011-12-22 13:29:45 +00002734bool
2735bfa_nw_ioc_is_operational(struct bfa_ioc *ioc)
2736{
2737 return bfa_fsm_cmp_state(ioc, bfa_ioc_sm_op);
2738}
2739
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002740/* Add to IOC heartbeat failure notification queue. To be used by common
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002741 * modules such as cee, port, diag.
2742 */
2743void
Rasesh Modybd5a92e2011-07-22 08:07:42 +00002744bfa_nw_ioc_notify_register(struct bfa_ioc *ioc,
2745 struct bfa_ioc_notify *notify)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002746{
Rasesh Modybd5a92e2011-07-22 08:07:42 +00002747 list_add_tail(&notify->qe, &ioc->notify_q);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002748}
2749
Rasesh Mody2732ba52015-02-19 16:02:31 -05002750#define BFA_MFG_NAME "QLogic"
Rasesh Mody8a891422010-08-25 23:00:27 -07002751static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002752bfa_ioc_get_adapter_attr(struct bfa_ioc *ioc,
2753 struct bfa_adapter_attr *ad_attr)
2754{
2755 struct bfi_ioc_attr *ioc_attr;
2756
2757 ioc_attr = ioc->attr;
2758
2759 bfa_ioc_get_adapter_serial_num(ioc, ad_attr->serial_num);
2760 bfa_ioc_get_adapter_fw_ver(ioc, ad_attr->fw_ver);
2761 bfa_ioc_get_adapter_optrom_ver(ioc, ad_attr->optrom_ver);
2762 bfa_ioc_get_adapter_manufacturer(ioc, ad_attr->manufacturer);
2763 memcpy(&ad_attr->vpd, &ioc_attr->vpd,
2764 sizeof(struct bfa_mfg_vpd));
2765
2766 ad_attr->nports = bfa_ioc_get_nports(ioc);
2767 ad_attr->max_speed = bfa_ioc_speed_sup(ioc);
2768
2769 bfa_ioc_get_adapter_model(ioc, ad_attr->model);
2770 /* For now, model descr uses same model string */
2771 bfa_ioc_get_adapter_model(ioc, ad_attr->model_descr);
2772
2773 ad_attr->card_type = ioc_attr->card_type;
2774 ad_attr->is_mezz = bfa_mfg_is_mezz(ioc_attr->card_type);
2775
2776 if (BFI_ADAPTER_IS_SPECIAL(ioc_attr->adapter_prop))
2777 ad_attr->prototype = 1;
2778 else
2779 ad_attr->prototype = 0;
2780
2781 ad_attr->pwwn = bfa_ioc_get_pwwn(ioc);
Ivan Vecerad6b30592015-06-11 15:52:14 +02002782 bfa_nw_ioc_get_mac(ioc, ad_attr->mac);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002783
2784 ad_attr->pcie_gen = ioc_attr->pcie_gen;
2785 ad_attr->pcie_lanes = ioc_attr->pcie_lanes;
2786 ad_attr->pcie_lanes_orig = ioc_attr->pcie_lanes_orig;
2787 ad_attr->asic_rev = ioc_attr->asic_rev;
2788
2789 bfa_ioc_get_pci_chip_rev(ioc, ad_attr->hw_ver);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002790}
2791
Rasesh Mody8a891422010-08-25 23:00:27 -07002792static enum bfa_ioc_type
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002793bfa_ioc_get_type(struct bfa_ioc *ioc)
2794{
Rasesh Mody078086f2011-08-08 16:21:39 +00002795 if (ioc->clscode == BFI_PCIFN_CLASS_ETH)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002796 return BFA_IOC_TYPE_LL;
Rasesh Mody078086f2011-08-08 16:21:39 +00002797
2798 BUG_ON(!(ioc->clscode == BFI_PCIFN_CLASS_FC));
2799
2800 return (ioc->attr->port_mode == BFI_PORT_MODE_FC)
2801 ? BFA_IOC_TYPE_FC : BFA_IOC_TYPE_FCoE;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002802}
2803
Rasesh Mody8a891422010-08-25 23:00:27 -07002804static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002805bfa_ioc_get_adapter_serial_num(struct bfa_ioc *ioc, char *serial_num)
2806{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002807 memcpy(serial_num,
2808 (void *)ioc->attr->brcd_serialnum,
2809 BFA_ADAPTER_SERIAL_NUM_LEN);
2810}
2811
Rasesh Mody8a891422010-08-25 23:00:27 -07002812static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002813bfa_ioc_get_adapter_fw_ver(struct bfa_ioc *ioc, char *fw_ver)
2814{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002815 memcpy(fw_ver, ioc->attr->fw_version, BFA_VERSION_LEN);
2816}
2817
Rasesh Mody8a891422010-08-25 23:00:27 -07002818static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002819bfa_ioc_get_pci_chip_rev(struct bfa_ioc *ioc, char *chip_rev)
2820{
2821 BUG_ON(!(chip_rev));
2822
2823 memset(chip_rev, 0, BFA_IOC_CHIP_REV_LEN);
2824
2825 chip_rev[0] = 'R';
2826 chip_rev[1] = 'e';
2827 chip_rev[2] = 'v';
2828 chip_rev[3] = '-';
2829 chip_rev[4] = ioc->attr->asic_rev;
2830 chip_rev[5] = '\0';
2831}
2832
Rasesh Mody8a891422010-08-25 23:00:27 -07002833static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002834bfa_ioc_get_adapter_optrom_ver(struct bfa_ioc *ioc, char *optrom_ver)
2835{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002836 memcpy(optrom_ver, ioc->attr->optrom_version,
2837 BFA_VERSION_LEN);
2838}
2839
Rasesh Mody8a891422010-08-25 23:00:27 -07002840static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002841bfa_ioc_get_adapter_manufacturer(struct bfa_ioc *ioc, char *manufacturer)
2842{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002843 memcpy(manufacturer, BFA_MFG_NAME, BFA_ADAPTER_MFG_NAME_LEN);
2844}
2845
Rasesh Mody8a891422010-08-25 23:00:27 -07002846static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002847bfa_ioc_get_adapter_model(struct bfa_ioc *ioc, char *model)
2848{
2849 struct bfi_ioc_attr *ioc_attr;
2850
2851 BUG_ON(!(model));
2852 memset(model, 0, BFA_ADAPTER_MODEL_NAME_LEN);
2853
2854 ioc_attr = ioc->attr;
2855
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002856 snprintf(model, BFA_ADAPTER_MODEL_NAME_LEN, "%s-%u",
2857 BFA_MFG_NAME, ioc_attr->card_type);
2858}
2859
Rasesh Mody8a891422010-08-25 23:00:27 -07002860static enum bfa_ioc_state
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002861bfa_ioc_get_state(struct bfa_ioc *ioc)
2862{
Rasesh Mody1d32f762010-12-23 21:45:09 +00002863 enum bfa_iocpf_state iocpf_st;
2864 enum bfa_ioc_state ioc_st = bfa_sm_to_state(ioc_sm_table, ioc->fsm);
2865
2866 if (ioc_st == BFA_IOC_ENABLING ||
2867 ioc_st == BFA_IOC_FAIL || ioc_st == BFA_IOC_INITFAIL) {
2868
2869 iocpf_st = bfa_sm_to_state(iocpf_sm_table, ioc->iocpf.fsm);
2870
2871 switch (iocpf_st) {
2872 case BFA_IOCPF_SEMWAIT:
2873 ioc_st = BFA_IOC_SEMWAIT;
2874 break;
2875
2876 case BFA_IOCPF_HWINIT:
2877 ioc_st = BFA_IOC_HWINIT;
2878 break;
2879
2880 case BFA_IOCPF_FWMISMATCH:
2881 ioc_st = BFA_IOC_FWMISMATCH;
2882 break;
2883
2884 case BFA_IOCPF_FAIL:
2885 ioc_st = BFA_IOC_FAIL;
2886 break;
2887
2888 case BFA_IOCPF_INITFAIL:
2889 ioc_st = BFA_IOC_INITFAIL;
2890 break;
2891
2892 default:
2893 break;
2894 }
2895 }
2896 return ioc_st;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002897}
2898
2899void
Rasesh Mody8a891422010-08-25 23:00:27 -07002900bfa_nw_ioc_get_attr(struct bfa_ioc *ioc, struct bfa_ioc_attr *ioc_attr)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002901{
2902 memset((void *)ioc_attr, 0, sizeof(struct bfa_ioc_attr));
2903
2904 ioc_attr->state = bfa_ioc_get_state(ioc);
Rasesh Mody43c07ad2013-05-20 10:08:03 +00002905 ioc_attr->port_id = bfa_ioc_portid(ioc);
Rasesh Mody078086f2011-08-08 16:21:39 +00002906 ioc_attr->port_mode = ioc->port_mode;
2907
2908 ioc_attr->port_mode_cfg = ioc->port_mode_cfg;
2909 ioc_attr->cap_bm = ioc->ad_cap_bm;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002910
2911 ioc_attr->ioc_type = bfa_ioc_get_type(ioc);
2912
2913 bfa_ioc_get_adapter_attr(ioc, &ioc_attr->adapter_attr);
2914
Rasesh Mody43c07ad2013-05-20 10:08:03 +00002915 ioc_attr->pci_attr.device_id = bfa_ioc_devid(ioc);
2916 ioc_attr->pci_attr.pcifn = bfa_ioc_pcifn(ioc);
2917 ioc_attr->def_fn = bfa_ioc_is_default(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002918 bfa_ioc_get_pci_chip_rev(ioc, ioc_attr->pci_attr.chip_rev);
2919}
2920
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002921/* WWN public */
Rasesh Mody8a891422010-08-25 23:00:27 -07002922static u64
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002923bfa_ioc_get_pwwn(struct bfa_ioc *ioc)
2924{
2925 return ioc->attr->pwwn;
2926}
2927
Ivan Vecerad6b30592015-06-11 15:52:14 +02002928void
2929bfa_nw_ioc_get_mac(struct bfa_ioc *ioc, u8 *mac)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002930{
Ivan Vecerad6b30592015-06-11 15:52:14 +02002931 ether_addr_copy(mac, ioc->attr->mac);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002932}
2933
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002934/* Firmware failure detected. Start recovery actions. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002935static void
2936bfa_ioc_recover(struct bfa_ioc *ioc)
2937{
Rasesh Mody1e581482011-04-04 08:29:59 +00002938 pr_crit("Heart Beat of IOC has failed\n");
2939 bfa_ioc_stats(ioc, ioc_hbfails);
Rasesh Mody9b08a4f2011-07-22 08:07:48 +00002940 bfa_ioc_stats_hb_count(ioc, ioc->hb_count);
Rasesh Mody1e581482011-04-04 08:29:59 +00002941 bfa_fsm_send_event(ioc, IOC_E_HBFAIL);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002942}
2943
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002944/* BFA IOC PF private functions */
Rasesh Mody1d32f762010-12-23 21:45:09 +00002945
2946static void
2947bfa_iocpf_enable(struct bfa_ioc *ioc)
2948{
2949 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_ENABLE);
2950}
2951
2952static void
2953bfa_iocpf_disable(struct bfa_ioc *ioc)
2954{
2955 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_DISABLE);
2956}
2957
2958static void
2959bfa_iocpf_fail(struct bfa_ioc *ioc)
2960{
2961 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_FAIL);
2962}
2963
2964static void
2965bfa_iocpf_initfail(struct bfa_ioc *ioc)
2966{
2967 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_INITFAIL);
2968}
2969
2970static void
2971bfa_iocpf_getattrfail(struct bfa_ioc *ioc)
2972{
2973 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_GETATTRFAIL);
2974}
2975
2976static void
2977bfa_iocpf_stop(struct bfa_ioc *ioc)
2978{
2979 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_STOP);
2980}
2981
2982void
Ivan Veceraad24d6f2015-06-11 15:52:30 +02002983bfa_nw_iocpf_timeout(struct bfa_ioc *ioc)
Rasesh Mody1d32f762010-12-23 21:45:09 +00002984{
Rasesh Mody078086f2011-08-08 16:21:39 +00002985 enum bfa_iocpf_state iocpf_st;
Rasesh Mody1d32f762010-12-23 21:45:09 +00002986
Rasesh Mody078086f2011-08-08 16:21:39 +00002987 iocpf_st = bfa_sm_to_state(iocpf_sm_table, ioc->iocpf.fsm);
2988
2989 if (iocpf_st == BFA_IOCPF_HWINIT)
2990 bfa_ioc_poll_fwinit(ioc);
2991 else
2992 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_TIMEOUT);
Rasesh Mody1d32f762010-12-23 21:45:09 +00002993}
2994
2995void
Ivan Veceraad24d6f2015-06-11 15:52:30 +02002996bfa_nw_iocpf_sem_timeout(struct bfa_ioc *ioc)
Rasesh Mody1d32f762010-12-23 21:45:09 +00002997{
Rasesh Mody1d32f762010-12-23 21:45:09 +00002998 bfa_ioc_hw_sem_get(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002999}
Rasesh Mody078086f2011-08-08 16:21:39 +00003000
3001static void
3002bfa_ioc_poll_fwinit(struct bfa_ioc *ioc)
3003{
Rasesh Mody41ed9032013-12-17 17:07:32 -08003004 u32 fwstate = bfa_ioc_get_cur_ioc_fwstate(ioc);
Rasesh Mody078086f2011-08-08 16:21:39 +00003005
3006 if (fwstate == BFI_IOC_DISABLED) {
3007 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_FWREADY);
3008 return;
3009 }
3010
3011 if (ioc->iocpf.poll_time >= BFA_IOC_TOV) {
Ivan Vecera4818e8562015-05-28 23:10:08 +02003012 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_TIMEOUT);
Rasesh Mody078086f2011-08-08 16:21:39 +00003013 } else {
3014 ioc->iocpf.poll_time += BFA_IOC_POLL_TOV;
3015 mod_timer(&ioc->iocpf_timer, jiffies +
3016 msecs_to_jiffies(BFA_IOC_POLL_TOV));
3017 }
3018}
Krishna Gudipati72a97302011-12-22 13:29:45 +00003019
3020/*
3021 * Flash module specific
3022 */
3023
3024/*
3025 * FLASH DMA buffer should be big enough to hold both MFG block and
3026 * asic block(64k) at the same time and also should be 2k aligned to
3027 * avoid write segement to cross sector boundary.
3028 */
3029#define BFA_FLASH_SEG_SZ 2048
3030#define BFA_FLASH_DMA_BUF_SZ \
3031 roundup(0x010000 + sizeof(struct bfa_mfg_block), BFA_FLASH_SEG_SZ)
3032
3033static void
3034bfa_flash_cb(struct bfa_flash *flash)
3035{
3036 flash->op_busy = 0;
3037 if (flash->cbfn)
3038 flash->cbfn(flash->cbarg, flash->status);
3039}
3040
3041static void
3042bfa_flash_notify(void *cbarg, enum bfa_ioc_event event)
3043{
3044 struct bfa_flash *flash = cbarg;
3045
3046 switch (event) {
3047 case BFA_IOC_E_DISABLED:
3048 case BFA_IOC_E_FAILED:
3049 if (flash->op_busy) {
3050 flash->status = BFA_STATUS_IOC_FAILURE;
3051 flash->cbfn(flash->cbarg, flash->status);
3052 flash->op_busy = 0;
3053 }
3054 break;
3055 default:
3056 break;
3057 }
3058}
3059
3060/*
3061 * Send flash write request.
Krishna Gudipati72a97302011-12-22 13:29:45 +00003062 */
3063static void
3064bfa_flash_write_send(struct bfa_flash *flash)
3065{
3066 struct bfi_flash_write_req *msg =
3067 (struct bfi_flash_write_req *) flash->mb.msg;
3068 u32 len;
3069
3070 msg->type = be32_to_cpu(flash->type);
3071 msg->instance = flash->instance;
3072 msg->offset = be32_to_cpu(flash->addr_off + flash->offset);
3073 len = (flash->residue < BFA_FLASH_DMA_BUF_SZ) ?
3074 flash->residue : BFA_FLASH_DMA_BUF_SZ;
3075 msg->length = be32_to_cpu(len);
3076
3077 /* indicate if it's the last msg of the whole write operation */
3078 msg->last = (len == flash->residue) ? 1 : 0;
3079
3080 bfi_h2i_set(msg->mh, BFI_MC_FLASH, BFI_FLASH_H2I_WRITE_REQ,
3081 bfa_ioc_portid(flash->ioc));
3082 bfa_alen_set(&msg->alen, len, flash->dbuf_pa);
3083 memcpy(flash->dbuf_kva, flash->ubuf + flash->offset, len);
3084 bfa_nw_ioc_mbox_queue(flash->ioc, &flash->mb, NULL, NULL);
3085
3086 flash->residue -= len;
3087 flash->offset += len;
3088}
3089
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003090/**
3091 * bfa_flash_read_send - Send flash read request.
Krishna Gudipati72a97302011-12-22 13:29:45 +00003092 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003093 * @cbarg: callback argument
Krishna Gudipati72a97302011-12-22 13:29:45 +00003094 */
3095static void
3096bfa_flash_read_send(void *cbarg)
3097{
3098 struct bfa_flash *flash = cbarg;
3099 struct bfi_flash_read_req *msg =
3100 (struct bfi_flash_read_req *) flash->mb.msg;
3101 u32 len;
3102
3103 msg->type = be32_to_cpu(flash->type);
3104 msg->instance = flash->instance;
3105 msg->offset = be32_to_cpu(flash->addr_off + flash->offset);
3106 len = (flash->residue < BFA_FLASH_DMA_BUF_SZ) ?
3107 flash->residue : BFA_FLASH_DMA_BUF_SZ;
3108 msg->length = be32_to_cpu(len);
3109 bfi_h2i_set(msg->mh, BFI_MC_FLASH, BFI_FLASH_H2I_READ_REQ,
3110 bfa_ioc_portid(flash->ioc));
3111 bfa_alen_set(&msg->alen, len, flash->dbuf_pa);
3112 bfa_nw_ioc_mbox_queue(flash->ioc, &flash->mb, NULL, NULL);
3113}
3114
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003115/**
3116 * bfa_flash_intr - Process flash response messages upon receiving interrupts.
Krishna Gudipati72a97302011-12-22 13:29:45 +00003117 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003118 * @flasharg: flash structure
3119 * @msg: message structure
Krishna Gudipati72a97302011-12-22 13:29:45 +00003120 */
3121static void
3122bfa_flash_intr(void *flasharg, struct bfi_mbmsg *msg)
3123{
3124 struct bfa_flash *flash = flasharg;
3125 u32 status;
3126
3127 union {
3128 struct bfi_flash_query_rsp *query;
3129 struct bfi_flash_write_rsp *write;
3130 struct bfi_flash_read_rsp *read;
3131 struct bfi_mbmsg *msg;
3132 } m;
3133
3134 m.msg = msg;
3135
3136 /* receiving response after ioc failure */
3137 if (!flash->op_busy && msg->mh.msg_id != BFI_FLASH_I2H_EVENT)
3138 return;
3139
3140 switch (msg->mh.msg_id) {
3141 case BFI_FLASH_I2H_QUERY_RSP:
3142 status = be32_to_cpu(m.query->status);
3143 if (status == BFA_STATUS_OK) {
3144 u32 i;
3145 struct bfa_flash_attr *attr, *f;
3146
3147 attr = (struct bfa_flash_attr *) flash->ubuf;
3148 f = (struct bfa_flash_attr *) flash->dbuf_kva;
3149 attr->status = be32_to_cpu(f->status);
3150 attr->npart = be32_to_cpu(f->npart);
3151 for (i = 0; i < attr->npart; i++) {
3152 attr->part[i].part_type =
3153 be32_to_cpu(f->part[i].part_type);
3154 attr->part[i].part_instance =
3155 be32_to_cpu(f->part[i].part_instance);
3156 attr->part[i].part_off =
3157 be32_to_cpu(f->part[i].part_off);
3158 attr->part[i].part_size =
3159 be32_to_cpu(f->part[i].part_size);
3160 attr->part[i].part_len =
3161 be32_to_cpu(f->part[i].part_len);
3162 attr->part[i].part_status =
3163 be32_to_cpu(f->part[i].part_status);
3164 }
3165 }
3166 flash->status = status;
3167 bfa_flash_cb(flash);
3168 break;
3169 case BFI_FLASH_I2H_WRITE_RSP:
3170 status = be32_to_cpu(m.write->status);
3171 if (status != BFA_STATUS_OK || flash->residue == 0) {
3172 flash->status = status;
3173 bfa_flash_cb(flash);
3174 } else
3175 bfa_flash_write_send(flash);
3176 break;
3177 case BFI_FLASH_I2H_READ_RSP:
3178 status = be32_to_cpu(m.read->status);
3179 if (status != BFA_STATUS_OK) {
3180 flash->status = status;
3181 bfa_flash_cb(flash);
3182 } else {
3183 u32 len = be32_to_cpu(m.read->length);
3184 memcpy(flash->ubuf + flash->offset,
3185 flash->dbuf_kva, len);
3186 flash->residue -= len;
3187 flash->offset += len;
3188 if (flash->residue == 0) {
3189 flash->status = status;
3190 bfa_flash_cb(flash);
3191 } else
3192 bfa_flash_read_send(flash);
3193 }
3194 break;
3195 case BFI_FLASH_I2H_BOOT_VER_RSP:
3196 case BFI_FLASH_I2H_EVENT:
3197 break;
3198 default:
3199 WARN_ON(1);
3200 }
3201}
3202
3203/*
3204 * Flash memory info API.
3205 */
3206u32
3207bfa_nw_flash_meminfo(void)
3208{
3209 return roundup(BFA_FLASH_DMA_BUF_SZ, BFA_DMA_ALIGN_SZ);
3210}
3211
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003212/**
3213 * bfa_nw_flash_attach - Flash attach API.
Krishna Gudipati72a97302011-12-22 13:29:45 +00003214 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003215 * @flash: flash structure
3216 * @ioc: ioc structure
3217 * @dev: device structure
Krishna Gudipati72a97302011-12-22 13:29:45 +00003218 */
3219void
3220bfa_nw_flash_attach(struct bfa_flash *flash, struct bfa_ioc *ioc, void *dev)
3221{
3222 flash->ioc = ioc;
3223 flash->cbfn = NULL;
3224 flash->cbarg = NULL;
3225 flash->op_busy = 0;
3226
3227 bfa_nw_ioc_mbox_regisr(flash->ioc, BFI_MC_FLASH, bfa_flash_intr, flash);
Krishna Gudipati72a97302011-12-22 13:29:45 +00003228 bfa_ioc_notify_init(&flash->ioc_notify, bfa_flash_notify, flash);
3229 list_add_tail(&flash->ioc_notify.qe, &flash->ioc->notify_q);
3230}
3231
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003232/**
3233 * bfa_nw_flash_memclaim - Claim memory for flash
Krishna Gudipati72a97302011-12-22 13:29:45 +00003234 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003235 * @flash: flash structure
3236 * @dm_kva: pointer to virtual memory address
3237 * @dm_pa: physical memory address
Krishna Gudipati72a97302011-12-22 13:29:45 +00003238 */
3239void
3240bfa_nw_flash_memclaim(struct bfa_flash *flash, u8 *dm_kva, u64 dm_pa)
3241{
3242 flash->dbuf_kva = dm_kva;
3243 flash->dbuf_pa = dm_pa;
3244 memset(flash->dbuf_kva, 0, BFA_FLASH_DMA_BUF_SZ);
3245 dm_kva += roundup(BFA_FLASH_DMA_BUF_SZ, BFA_DMA_ALIGN_SZ);
3246 dm_pa += roundup(BFA_FLASH_DMA_BUF_SZ, BFA_DMA_ALIGN_SZ);
3247}
3248
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003249/**
3250 * bfa_nw_flash_get_attr - Get flash attribute.
Krishna Gudipati72a97302011-12-22 13:29:45 +00003251 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003252 * @flash: flash structure
3253 * @attr: flash attribute structure
3254 * @cbfn: callback function
3255 * @cbarg: callback argument
Krishna Gudipati72a97302011-12-22 13:29:45 +00003256 *
3257 * Return status.
3258 */
3259enum bfa_status
3260bfa_nw_flash_get_attr(struct bfa_flash *flash, struct bfa_flash_attr *attr,
3261 bfa_cb_flash cbfn, void *cbarg)
3262{
3263 struct bfi_flash_query_req *msg =
3264 (struct bfi_flash_query_req *) flash->mb.msg;
3265
3266 if (!bfa_nw_ioc_is_operational(flash->ioc))
3267 return BFA_STATUS_IOC_NON_OP;
3268
3269 if (flash->op_busy)
3270 return BFA_STATUS_DEVBUSY;
3271
3272 flash->op_busy = 1;
3273 flash->cbfn = cbfn;
3274 flash->cbarg = cbarg;
3275 flash->ubuf = (u8 *) attr;
3276
3277 bfi_h2i_set(msg->mh, BFI_MC_FLASH, BFI_FLASH_H2I_QUERY_REQ,
3278 bfa_ioc_portid(flash->ioc));
3279 bfa_alen_set(&msg->alen, sizeof(struct bfa_flash_attr), flash->dbuf_pa);
3280 bfa_nw_ioc_mbox_queue(flash->ioc, &flash->mb, NULL, NULL);
3281
3282 return BFA_STATUS_OK;
3283}
3284
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003285/**
3286 * bfa_nw_flash_update_part - Update flash partition.
Krishna Gudipati72a97302011-12-22 13:29:45 +00003287 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003288 * @flash: flash structure
3289 * @type: flash partition type
3290 * @instance: flash partition instance
3291 * @buf: update data buffer
3292 * @len: data buffer length
3293 * @offset: offset relative to the partition starting address
3294 * @cbfn: callback function
3295 * @cbarg: callback argument
Krishna Gudipati72a97302011-12-22 13:29:45 +00003296 *
3297 * Return status.
3298 */
3299enum bfa_status
3300bfa_nw_flash_update_part(struct bfa_flash *flash, u32 type, u8 instance,
3301 void *buf, u32 len, u32 offset,
3302 bfa_cb_flash cbfn, void *cbarg)
3303{
3304 if (!bfa_nw_ioc_is_operational(flash->ioc))
3305 return BFA_STATUS_IOC_NON_OP;
3306
3307 /*
3308 * 'len' must be in word (4-byte) boundary
3309 */
3310 if (!len || (len & 0x03))
3311 return BFA_STATUS_FLASH_BAD_LEN;
3312
3313 if (type == BFA_FLASH_PART_MFG)
3314 return BFA_STATUS_EINVAL;
3315
3316 if (flash->op_busy)
3317 return BFA_STATUS_DEVBUSY;
3318
3319 flash->op_busy = 1;
3320 flash->cbfn = cbfn;
3321 flash->cbarg = cbarg;
3322 flash->type = type;
3323 flash->instance = instance;
3324 flash->residue = len;
3325 flash->offset = 0;
3326 flash->addr_off = offset;
3327 flash->ubuf = buf;
3328
3329 bfa_flash_write_send(flash);
3330
3331 return BFA_STATUS_OK;
3332}
3333
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003334/**
3335 * bfa_nw_flash_read_part - Read flash partition.
Krishna Gudipati72a97302011-12-22 13:29:45 +00003336 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00003337 * @flash: flash structure
3338 * @type: flash partition type
3339 * @instance: flash partition instance
3340 * @buf: read data buffer
3341 * @len: data buffer length
3342 * @offset: offset relative to the partition starting address
3343 * @cbfn: callback function
3344 * @cbarg: callback argument
Krishna Gudipati72a97302011-12-22 13:29:45 +00003345 *
3346 * Return status.
3347 */
3348enum bfa_status
3349bfa_nw_flash_read_part(struct bfa_flash *flash, u32 type, u8 instance,
3350 void *buf, u32 len, u32 offset,
3351 bfa_cb_flash cbfn, void *cbarg)
3352{
3353 if (!bfa_nw_ioc_is_operational(flash->ioc))
3354 return BFA_STATUS_IOC_NON_OP;
3355
3356 /*
3357 * 'len' must be in word (4-byte) boundary
3358 */
3359 if (!len || (len & 0x03))
3360 return BFA_STATUS_FLASH_BAD_LEN;
3361
3362 if (flash->op_busy)
3363 return BFA_STATUS_DEVBUSY;
3364
3365 flash->op_busy = 1;
3366 flash->cbfn = cbfn;
3367 flash->cbarg = cbarg;
3368 flash->type = type;
3369 flash->instance = instance;
3370 flash->residue = len;
3371 flash->offset = 0;
3372 flash->addr_off = offset;
3373 flash->ubuf = buf;
3374
3375 bfa_flash_read_send(flash);
3376
3377 return BFA_STATUS_OK;
3378}