blob: b81132a8ee793b25fcdc322d139f55a561a23a6a [file] [log] [blame]
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001/*
2 * Linux network driver for Brocade Converged Network Adapter.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License (GPL) Version 2 as
6 * published by the Free Software Foundation
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13/*
14 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
15 * All rights reserved
16 * www.brocade.com
17 */
18
19#include "bfa_ioc.h"
Rasesh Modya9602492011-08-02 12:36:06 +000020#include "bfi_reg.h"
Rasesh Mody8b230ed2010-08-23 20:24:12 -070021#include "bfa_defs.h"
22
Ben Hutchings1aa8b472012-07-10 10:56:59 +000023/* IOC local definitions */
Rasesh Mody8b230ed2010-08-23 20:24:12 -070024
Ben Hutchings1aa8b472012-07-10 10:56:59 +000025/* Asic specific macros : see bfa_hw_cb.c and bfa_hw_ct.c for details. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -070026
27#define bfa_ioc_firmware_lock(__ioc) \
28 ((__ioc)->ioc_hwif->ioc_firmware_lock(__ioc))
29#define bfa_ioc_firmware_unlock(__ioc) \
30 ((__ioc)->ioc_hwif->ioc_firmware_unlock(__ioc))
31#define bfa_ioc_reg_init(__ioc) ((__ioc)->ioc_hwif->ioc_reg_init(__ioc))
32#define bfa_ioc_map_port(__ioc) ((__ioc)->ioc_hwif->ioc_map_port(__ioc))
Rasesh Mody1d32f762010-12-23 21:45:09 +000033#define bfa_ioc_notify_fail(__ioc) \
34 ((__ioc)->ioc_hwif->ioc_notify_fail(__ioc))
Rasesh Mody79ea6c82011-04-14 08:05:18 +000035#define bfa_ioc_sync_start(__ioc) \
36 ((__ioc)->ioc_hwif->ioc_sync_start(__ioc))
Rasesh Mody1d32f762010-12-23 21:45:09 +000037#define bfa_ioc_sync_join(__ioc) \
38 ((__ioc)->ioc_hwif->ioc_sync_join(__ioc))
39#define bfa_ioc_sync_leave(__ioc) \
40 ((__ioc)->ioc_hwif->ioc_sync_leave(__ioc))
41#define bfa_ioc_sync_ack(__ioc) \
42 ((__ioc)->ioc_hwif->ioc_sync_ack(__ioc))
43#define bfa_ioc_sync_complete(__ioc) \
44 ((__ioc)->ioc_hwif->ioc_sync_complete(__ioc))
Rasesh Mody41ed9032013-12-17 17:07:32 -080045#define bfa_ioc_set_cur_ioc_fwstate(__ioc, __fwstate) \
46 ((__ioc)->ioc_hwif->ioc_set_fwstate(__ioc, __fwstate))
47#define bfa_ioc_get_cur_ioc_fwstate(__ioc) \
48 ((__ioc)->ioc_hwif->ioc_get_fwstate(__ioc))
49#define bfa_ioc_set_alt_ioc_fwstate(__ioc, __fwstate) \
50 ((__ioc)->ioc_hwif->ioc_set_alt_fwstate(__ioc, __fwstate))
51#define bfa_ioc_get_alt_ioc_fwstate(__ioc) \
52 ((__ioc)->ioc_hwif->ioc_get_alt_fwstate(__ioc))
Rasesh Mody8b230ed2010-08-23 20:24:12 -070053
Rasesh Mody8b230ed2010-08-23 20:24:12 -070054#define bfa_ioc_mbox_cmd_pending(__ioc) \
55 (!list_empty(&((__ioc)->mbox_mod.cmd_q)) || \
56 readl((__ioc)->ioc_regs.hfn_mbox_cmd))
57
Rasesh Modyb7ee31c52010-10-05 15:46:05 +000058static bool bfa_nw_auto_recover = true;
Rasesh Mody8b230ed2010-08-23 20:24:12 -070059
60/*
61 * forward declarations
62 */
Rasesh Modyd4e16d42011-07-22 08:07:47 +000063static void bfa_ioc_hw_sem_init(struct bfa_ioc *ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -070064static void bfa_ioc_hw_sem_get(struct bfa_ioc *ioc);
65static void bfa_ioc_hw_sem_get_cancel(struct bfa_ioc *ioc);
66static void bfa_ioc_hwinit(struct bfa_ioc *ioc, bool force);
Rasesh Mody078086f2011-08-08 16:21:39 +000067static void bfa_ioc_poll_fwinit(struct bfa_ioc *ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -070068static void bfa_ioc_send_enable(struct bfa_ioc *ioc);
69static void bfa_ioc_send_disable(struct bfa_ioc *ioc);
70static void bfa_ioc_send_getattr(struct bfa_ioc *ioc);
71static void bfa_ioc_hb_monitor(struct bfa_ioc *ioc);
72static void bfa_ioc_hb_stop(struct bfa_ioc *ioc);
73static void bfa_ioc_reset(struct bfa_ioc *ioc, bool force);
74static void bfa_ioc_mbox_poll(struct bfa_ioc *ioc);
Rasesh Modyfdad4002011-07-22 08:07:45 +000075static void bfa_ioc_mbox_flush(struct bfa_ioc *ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -070076static void bfa_ioc_recover(struct bfa_ioc *ioc);
Rasesh Modybd5a92e2011-07-22 08:07:42 +000077static void bfa_ioc_event_notify(struct bfa_ioc *, enum bfa_ioc_event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -070078static void bfa_ioc_disable_comp(struct bfa_ioc *ioc);
79static void bfa_ioc_lpu_stop(struct bfa_ioc *ioc);
Krishna Gudipati7afc5db2011-12-22 13:30:19 +000080static void bfa_nw_ioc_debug_save_ftrc(struct bfa_ioc *ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +000081static void bfa_ioc_fail_notify(struct bfa_ioc *ioc);
82static void bfa_ioc_pf_enabled(struct bfa_ioc *ioc);
83static void bfa_ioc_pf_disabled(struct bfa_ioc *ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +000084static void bfa_ioc_pf_failed(struct bfa_ioc *ioc);
Rasesh Mody078086f2011-08-08 16:21:39 +000085static void bfa_ioc_pf_hwfailed(struct bfa_ioc *ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +000086static void bfa_ioc_pf_fwmismatch(struct bfa_ioc *ioc);
stephen hemminger7068a672013-03-08 09:07:40 +000087static void bfa_ioc_boot(struct bfa_ioc *ioc, enum bfi_fwboot_type boot_type,
Rasesh Mody8a891422010-08-25 23:00:27 -070088 u32 boot_param);
89static u32 bfa_ioc_smem_pgnum(struct bfa_ioc *ioc, u32 fmaddr);
Rasesh Mody8a891422010-08-25 23:00:27 -070090static void bfa_ioc_get_adapter_serial_num(struct bfa_ioc *ioc,
91 char *serial_num);
92static void bfa_ioc_get_adapter_fw_ver(struct bfa_ioc *ioc,
93 char *fw_ver);
94static void bfa_ioc_get_pci_chip_rev(struct bfa_ioc *ioc,
95 char *chip_rev);
96static void bfa_ioc_get_adapter_optrom_ver(struct bfa_ioc *ioc,
97 char *optrom_ver);
98static void bfa_ioc_get_adapter_manufacturer(struct bfa_ioc *ioc,
99 char *manufacturer);
100static void bfa_ioc_get_adapter_model(struct bfa_ioc *ioc, char *model);
101static u64 bfa_ioc_get_pwwn(struct bfa_ioc *ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700102
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000103/* IOC state machine definitions/declarations */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700104enum ioc_event {
Rasesh Mody1d32f762010-12-23 21:45:09 +0000105 IOC_E_RESET = 1, /*!< IOC reset request */
106 IOC_E_ENABLE = 2, /*!< IOC enable request */
107 IOC_E_DISABLE = 3, /*!< IOC disable request */
108 IOC_E_DETACH = 4, /*!< driver detach cleanup */
109 IOC_E_ENABLED = 5, /*!< f/w enabled */
110 IOC_E_FWRSP_GETATTR = 6, /*!< IOC get attribute response */
111 IOC_E_DISABLED = 7, /*!< f/w disabled */
Rasesh Mody078086f2011-08-08 16:21:39 +0000112 IOC_E_PFFAILED = 8, /*!< failure notice by iocpf sm */
113 IOC_E_HBFAIL = 9, /*!< heartbeat failure */
114 IOC_E_HWERROR = 10, /*!< hardware error interrupt */
115 IOC_E_TIMEOUT = 11, /*!< timeout */
116 IOC_E_HWFAILED = 12, /*!< PCI mapping failure notice */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700117};
118
Rasesh Mody1d32f762010-12-23 21:45:09 +0000119bfa_fsm_state_decl(bfa_ioc, uninit, struct bfa_ioc, enum ioc_event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700120bfa_fsm_state_decl(bfa_ioc, reset, struct bfa_ioc, enum ioc_event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700121bfa_fsm_state_decl(bfa_ioc, enabling, struct bfa_ioc, enum ioc_event);
122bfa_fsm_state_decl(bfa_ioc, getattr, struct bfa_ioc, enum ioc_event);
123bfa_fsm_state_decl(bfa_ioc, op, struct bfa_ioc, enum ioc_event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000124bfa_fsm_state_decl(bfa_ioc, fail_retry, struct bfa_ioc, enum ioc_event);
125bfa_fsm_state_decl(bfa_ioc, fail, struct bfa_ioc, enum ioc_event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700126bfa_fsm_state_decl(bfa_ioc, disabling, struct bfa_ioc, enum ioc_event);
127bfa_fsm_state_decl(bfa_ioc, disabled, struct bfa_ioc, enum ioc_event);
Rasesh Mody078086f2011-08-08 16:21:39 +0000128bfa_fsm_state_decl(bfa_ioc, hwfail, struct bfa_ioc, enum ioc_event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700129
130static struct bfa_sm_table ioc_sm_table[] = {
Rasesh Mody1d32f762010-12-23 21:45:09 +0000131 {BFA_SM(bfa_ioc_sm_uninit), BFA_IOC_UNINIT},
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700132 {BFA_SM(bfa_ioc_sm_reset), BFA_IOC_RESET},
Rasesh Mody1d32f762010-12-23 21:45:09 +0000133 {BFA_SM(bfa_ioc_sm_enabling), BFA_IOC_ENABLING},
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700134 {BFA_SM(bfa_ioc_sm_getattr), BFA_IOC_GETATTR},
135 {BFA_SM(bfa_ioc_sm_op), BFA_IOC_OPERATIONAL},
Rasesh Mody1d32f762010-12-23 21:45:09 +0000136 {BFA_SM(bfa_ioc_sm_fail_retry), BFA_IOC_INITFAIL},
137 {BFA_SM(bfa_ioc_sm_fail), BFA_IOC_FAIL},
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700138 {BFA_SM(bfa_ioc_sm_disabling), BFA_IOC_DISABLING},
139 {BFA_SM(bfa_ioc_sm_disabled), BFA_IOC_DISABLED},
Rasesh Mody078086f2011-08-08 16:21:39 +0000140 {BFA_SM(bfa_ioc_sm_hwfail), BFA_IOC_HWFAIL},
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700141};
142
Rasesh Mody1d32f762010-12-23 21:45:09 +0000143/*
144 * Forward declareations for iocpf state machine
145 */
146static void bfa_iocpf_enable(struct bfa_ioc *ioc);
147static void bfa_iocpf_disable(struct bfa_ioc *ioc);
148static void bfa_iocpf_fail(struct bfa_ioc *ioc);
149static void bfa_iocpf_initfail(struct bfa_ioc *ioc);
150static void bfa_iocpf_getattrfail(struct bfa_ioc *ioc);
151static void bfa_iocpf_stop(struct bfa_ioc *ioc);
152
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000153/* IOCPF state machine events */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000154enum iocpf_event {
155 IOCPF_E_ENABLE = 1, /*!< IOCPF enable request */
156 IOCPF_E_DISABLE = 2, /*!< IOCPF disable request */
157 IOCPF_E_STOP = 3, /*!< stop on driver detach */
Rasesh Mody0120b992011-07-22 08:07:41 +0000158 IOCPF_E_FWREADY = 4, /*!< f/w initialization done */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000159 IOCPF_E_FWRSP_ENABLE = 5, /*!< enable f/w response */
160 IOCPF_E_FWRSP_DISABLE = 6, /*!< disable f/w response */
161 IOCPF_E_FAIL = 7, /*!< failure notice by ioc sm */
162 IOCPF_E_INITFAIL = 8, /*!< init fail notice by ioc sm */
163 IOCPF_E_GETATTRFAIL = 9, /*!< init fail notice by ioc sm */
164 IOCPF_E_SEMLOCKED = 10, /*!< h/w semaphore is locked */
165 IOCPF_E_TIMEOUT = 11, /*!< f/w response timeout */
Rasesh Mody078086f2011-08-08 16:21:39 +0000166 IOCPF_E_SEM_ERROR = 12, /*!< h/w sem mapping error */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000167};
168
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000169/* IOCPF states */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000170enum bfa_iocpf_state {
171 BFA_IOCPF_RESET = 1, /*!< IOC is in reset state */
172 BFA_IOCPF_SEMWAIT = 2, /*!< Waiting for IOC h/w semaphore */
173 BFA_IOCPF_HWINIT = 3, /*!< IOC h/w is being initialized */
174 BFA_IOCPF_READY = 4, /*!< IOCPF is initialized */
175 BFA_IOCPF_INITFAIL = 5, /*!< IOCPF failed */
176 BFA_IOCPF_FAIL = 6, /*!< IOCPF failed */
177 BFA_IOCPF_DISABLING = 7, /*!< IOCPF is being disabled */
178 BFA_IOCPF_DISABLED = 8, /*!< IOCPF is disabled */
179 BFA_IOCPF_FWMISMATCH = 9, /*!< IOC f/w different from drivers */
180};
181
182bfa_fsm_state_decl(bfa_iocpf, reset, struct bfa_iocpf, enum iocpf_event);
183bfa_fsm_state_decl(bfa_iocpf, fwcheck, struct bfa_iocpf, enum iocpf_event);
184bfa_fsm_state_decl(bfa_iocpf, mismatch, struct bfa_iocpf, enum iocpf_event);
185bfa_fsm_state_decl(bfa_iocpf, semwait, struct bfa_iocpf, enum iocpf_event);
186bfa_fsm_state_decl(bfa_iocpf, hwinit, struct bfa_iocpf, enum iocpf_event);
187bfa_fsm_state_decl(bfa_iocpf, enabling, struct bfa_iocpf, enum iocpf_event);
188bfa_fsm_state_decl(bfa_iocpf, ready, struct bfa_iocpf, enum iocpf_event);
189bfa_fsm_state_decl(bfa_iocpf, initfail_sync, struct bfa_iocpf,
190 enum iocpf_event);
191bfa_fsm_state_decl(bfa_iocpf, initfail, struct bfa_iocpf, enum iocpf_event);
192bfa_fsm_state_decl(bfa_iocpf, fail_sync, struct bfa_iocpf, enum iocpf_event);
193bfa_fsm_state_decl(bfa_iocpf, fail, struct bfa_iocpf, enum iocpf_event);
194bfa_fsm_state_decl(bfa_iocpf, disabling, struct bfa_iocpf, enum iocpf_event);
195bfa_fsm_state_decl(bfa_iocpf, disabling_sync, struct bfa_iocpf,
196 enum iocpf_event);
197bfa_fsm_state_decl(bfa_iocpf, disabled, struct bfa_iocpf, enum iocpf_event);
198
199static struct bfa_sm_table iocpf_sm_table[] = {
200 {BFA_SM(bfa_iocpf_sm_reset), BFA_IOCPF_RESET},
201 {BFA_SM(bfa_iocpf_sm_fwcheck), BFA_IOCPF_FWMISMATCH},
202 {BFA_SM(bfa_iocpf_sm_mismatch), BFA_IOCPF_FWMISMATCH},
203 {BFA_SM(bfa_iocpf_sm_semwait), BFA_IOCPF_SEMWAIT},
204 {BFA_SM(bfa_iocpf_sm_hwinit), BFA_IOCPF_HWINIT},
205 {BFA_SM(bfa_iocpf_sm_enabling), BFA_IOCPF_HWINIT},
206 {BFA_SM(bfa_iocpf_sm_ready), BFA_IOCPF_READY},
207 {BFA_SM(bfa_iocpf_sm_initfail_sync), BFA_IOCPF_INITFAIL},
208 {BFA_SM(bfa_iocpf_sm_initfail), BFA_IOCPF_INITFAIL},
209 {BFA_SM(bfa_iocpf_sm_fail_sync), BFA_IOCPF_FAIL},
210 {BFA_SM(bfa_iocpf_sm_fail), BFA_IOCPF_FAIL},
211 {BFA_SM(bfa_iocpf_sm_disabling), BFA_IOCPF_DISABLING},
212 {BFA_SM(bfa_iocpf_sm_disabling_sync), BFA_IOCPF_DISABLING},
213 {BFA_SM(bfa_iocpf_sm_disabled), BFA_IOCPF_DISABLED},
214};
215
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000216/* IOC State Machine */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000217
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000218/* Beginning state. IOC uninit state. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000219static void
220bfa_ioc_sm_uninit_entry(struct bfa_ioc *ioc)
221{
222}
223
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000224/* IOC is in uninit state. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000225static void
226bfa_ioc_sm_uninit(struct bfa_ioc *ioc, enum ioc_event event)
227{
228 switch (event) {
229 case IOC_E_RESET:
230 bfa_fsm_set_state(ioc, bfa_ioc_sm_reset);
231 break;
232
233 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000234 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000235 }
236}
237
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000238/* Reset entry actions -- initialize state machine */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700239static void
240bfa_ioc_sm_reset_entry(struct bfa_ioc *ioc)
241{
Rasesh Mody1d32f762010-12-23 21:45:09 +0000242 bfa_fsm_set_state(&ioc->iocpf, bfa_iocpf_sm_reset);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700243}
244
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000245/* IOC is in reset state. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700246static void
247bfa_ioc_sm_reset(struct bfa_ioc *ioc, enum ioc_event event)
248{
249 switch (event) {
250 case IOC_E_ENABLE:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000251 bfa_fsm_set_state(ioc, bfa_ioc_sm_enabling);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700252 break;
253
254 case IOC_E_DISABLE:
255 bfa_ioc_disable_comp(ioc);
256 break;
257
258 case IOC_E_DETACH:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000259 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
260 break;
261
262 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000263 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000264 }
265}
266
267static void
268bfa_ioc_sm_enabling_entry(struct bfa_ioc *ioc)
269{
270 bfa_iocpf_enable(ioc);
271}
272
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000273/* Host IOC function is being enabled, awaiting response from firmware.
Rasesh Mody1d32f762010-12-23 21:45:09 +0000274 * Semaphore is acquired.
275 */
276static void
277bfa_ioc_sm_enabling(struct bfa_ioc *ioc, enum ioc_event event)
278{
279 switch (event) {
280 case IOC_E_ENABLED:
281 bfa_fsm_set_state(ioc, bfa_ioc_sm_getattr);
282 break;
283
Rasesh Modyf374b362011-07-22 08:07:46 +0000284 case IOC_E_PFFAILED:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000285 /* !!! fall through !!! */
286 case IOC_E_HWERROR:
287 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
Rasesh Mody078086f2011-08-08 16:21:39 +0000288 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
Rasesh Modyf374b362011-07-22 08:07:46 +0000289 if (event != IOC_E_PFFAILED)
Rasesh Mody1d32f762010-12-23 21:45:09 +0000290 bfa_iocpf_initfail(ioc);
291 break;
292
Rasesh Mody078086f2011-08-08 16:21:39 +0000293 case IOC_E_HWFAILED:
294 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
295 bfa_fsm_set_state(ioc, bfa_ioc_sm_hwfail);
296 break;
297
Rasesh Mody1d32f762010-12-23 21:45:09 +0000298 case IOC_E_DISABLE:
299 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
300 break;
301
302 case IOC_E_DETACH:
303 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
304 bfa_iocpf_stop(ioc);
305 break;
306
307 case IOC_E_ENABLE:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700308 break;
309
310 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000311 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700312 }
313}
314
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000315/* Semaphore should be acquired for version check. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700316static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700317bfa_ioc_sm_getattr_entry(struct bfa_ioc *ioc)
318{
Rasesh Mody1d32f762010-12-23 21:45:09 +0000319 mod_timer(&ioc->ioc_timer, jiffies +
320 msecs_to_jiffies(BFA_IOC_TOV));
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700321 bfa_ioc_send_getattr(ioc);
322}
323
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000324/* IOC configuration in progress. Timer is active. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700325static void
326bfa_ioc_sm_getattr(struct bfa_ioc *ioc, enum ioc_event event)
327{
328 switch (event) {
329 case IOC_E_FWRSP_GETATTR:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000330 del_timer(&ioc->ioc_timer);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700331 bfa_fsm_set_state(ioc, bfa_ioc_sm_op);
332 break;
333
Rasesh Modyf374b362011-07-22 08:07:46 +0000334 case IOC_E_PFFAILED:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700335 case IOC_E_HWERROR:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000336 del_timer(&ioc->ioc_timer);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700337 /* fall through */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700338 case IOC_E_TIMEOUT:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000339 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
Rasesh Mody078086f2011-08-08 16:21:39 +0000340 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
Rasesh Modyf374b362011-07-22 08:07:46 +0000341 if (event != IOC_E_PFFAILED)
Rasesh Mody1d32f762010-12-23 21:45:09 +0000342 bfa_iocpf_getattrfail(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700343 break;
344
345 case IOC_E_DISABLE:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000346 del_timer(&ioc->ioc_timer);
347 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
348 break;
349
350 case IOC_E_ENABLE:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700351 break;
352
353 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000354 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700355 }
356}
357
358static void
359bfa_ioc_sm_op_entry(struct bfa_ioc *ioc)
360{
361 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_OK);
Rasesh Mody078086f2011-08-08 16:21:39 +0000362 bfa_ioc_event_notify(ioc, BFA_IOC_E_ENABLED);
Jing Huangf96c1d22012-04-04 05:42:54 +0000363 bfa_ioc_hb_monitor(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700364}
365
366static void
367bfa_ioc_sm_op(struct bfa_ioc *ioc, enum ioc_event event)
368{
369 switch (event) {
370 case IOC_E_ENABLE:
371 break;
372
373 case IOC_E_DISABLE:
374 bfa_ioc_hb_stop(ioc);
375 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
376 break;
377
Rasesh Modyf374b362011-07-22 08:07:46 +0000378 case IOC_E_PFFAILED:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700379 case IOC_E_HWERROR:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700380 bfa_ioc_hb_stop(ioc);
381 /* !!! fall through !!! */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700382 case IOC_E_HBFAIL:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000383 if (ioc->iocpf.auto_recover)
384 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail_retry);
385 else
386 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
387
Rasesh Mody078086f2011-08-08 16:21:39 +0000388 bfa_ioc_fail_notify(ioc);
389
Rasesh Modyf374b362011-07-22 08:07:46 +0000390 if (event != IOC_E_PFFAILED)
Rasesh Mody1d32f762010-12-23 21:45:09 +0000391 bfa_iocpf_fail(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700392 break;
393
394 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000395 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700396 }
397}
398
399static void
400bfa_ioc_sm_disabling_entry(struct bfa_ioc *ioc)
401{
Rasesh Mody1d32f762010-12-23 21:45:09 +0000402 bfa_iocpf_disable(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700403}
404
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000405/* IOC is being disabled */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700406static void
407bfa_ioc_sm_disabling(struct bfa_ioc *ioc, enum ioc_event event)
408{
409 switch (event) {
Rasesh Mody1d32f762010-12-23 21:45:09 +0000410 case IOC_E_DISABLED:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700411 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
412 break;
413
414 case IOC_E_HWERROR:
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700415 /*
Rasesh Mody1d32f762010-12-23 21:45:09 +0000416 * No state change. Will move to disabled state
417 * after iocpf sm completes failure processing and
418 * moves to disabled state.
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700419 */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000420 bfa_iocpf_fail(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700421 break;
422
Rasesh Mody078086f2011-08-08 16:21:39 +0000423 case IOC_E_HWFAILED:
424 bfa_fsm_set_state(ioc, bfa_ioc_sm_hwfail);
425 bfa_ioc_disable_comp(ioc);
426 break;
427
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700428 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000429 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700430 }
431}
432
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000433/* IOC disable completion entry. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700434static void
435bfa_ioc_sm_disabled_entry(struct bfa_ioc *ioc)
436{
437 bfa_ioc_disable_comp(ioc);
438}
439
440static void
441bfa_ioc_sm_disabled(struct bfa_ioc *ioc, enum ioc_event event)
442{
443 switch (event) {
444 case IOC_E_ENABLE:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000445 bfa_fsm_set_state(ioc, bfa_ioc_sm_enabling);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700446 break;
447
448 case IOC_E_DISABLE:
449 ioc->cbfn->disable_cbfn(ioc->bfa);
450 break;
451
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700452 case IOC_E_DETACH:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000453 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
454 bfa_iocpf_stop(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700455 break;
456
457 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000458 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700459 }
460}
461
462static void
Rasesh Mody1d32f762010-12-23 21:45:09 +0000463bfa_ioc_sm_fail_retry_entry(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700464{
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700465}
466
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000467/* Hardware initialization retry. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700468static void
Rasesh Mody1d32f762010-12-23 21:45:09 +0000469bfa_ioc_sm_fail_retry(struct bfa_ioc *ioc, enum ioc_event event)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700470{
471 switch (event) {
Rasesh Mody1d32f762010-12-23 21:45:09 +0000472 case IOC_E_ENABLED:
473 bfa_fsm_set_state(ioc, bfa_ioc_sm_getattr);
474 break;
475
Rasesh Modyf374b362011-07-22 08:07:46 +0000476 case IOC_E_PFFAILED:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000477 case IOC_E_HWERROR:
478 /**
479 * Initialization retry failed.
480 */
481 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
Rasesh Mody078086f2011-08-08 16:21:39 +0000482 bfa_fsm_set_state(ioc, bfa_ioc_sm_fail);
Rasesh Modyf374b362011-07-22 08:07:46 +0000483 if (event != IOC_E_PFFAILED)
Rasesh Mody1d32f762010-12-23 21:45:09 +0000484 bfa_iocpf_initfail(ioc);
485 break;
486
Rasesh Mody078086f2011-08-08 16:21:39 +0000487 case IOC_E_HWFAILED:
488 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
489 bfa_fsm_set_state(ioc, bfa_ioc_sm_hwfail);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000490 break;
491
492 case IOC_E_ENABLE:
493 break;
494
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700495 case IOC_E_DISABLE:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000496 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700497 break;
498
499 case IOC_E_DETACH:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000500 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
501 bfa_iocpf_stop(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700502 break;
503
504 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000505 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700506 }
507}
508
509static void
Rasesh Mody1d32f762010-12-23 21:45:09 +0000510bfa_ioc_sm_fail_entry(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700511{
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700512}
513
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000514/* IOC failure. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700515static void
Rasesh Mody1d32f762010-12-23 21:45:09 +0000516bfa_ioc_sm_fail(struct bfa_ioc *ioc, enum ioc_event event)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700517{
518 switch (event) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700519 case IOC_E_ENABLE:
520 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
521 break;
522
523 case IOC_E_DISABLE:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000524 bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700525 break;
526
Rasesh Mody1d32f762010-12-23 21:45:09 +0000527 case IOC_E_DETACH:
528 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
529 bfa_iocpf_stop(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700530 break;
531
532 case IOC_E_HWERROR:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000533 /* HB failure notification, ignore. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700534 break;
Rasesh Mody1d32f762010-12-23 21:45:09 +0000535
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700536 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000537 bfa_sm_fault(event);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700538 }
539}
540
Rasesh Mody078086f2011-08-08 16:21:39 +0000541static void
542bfa_ioc_sm_hwfail_entry(struct bfa_ioc *ioc)
543{
544}
545
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000546/* IOC failure. */
Rasesh Mody078086f2011-08-08 16:21:39 +0000547static void
548bfa_ioc_sm_hwfail(struct bfa_ioc *ioc, enum ioc_event event)
549{
550 switch (event) {
551
552 case IOC_E_ENABLE:
553 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
554 break;
555
556 case IOC_E_DISABLE:
557 ioc->cbfn->disable_cbfn(ioc->bfa);
558 break;
559
560 case IOC_E_DETACH:
561 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
562 break;
563
564 default:
565 bfa_sm_fault(event);
566 }
567}
568
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000569/* IOCPF State Machine */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000570
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000571/* Reset entry actions -- initialize state machine */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000572static void
573bfa_iocpf_sm_reset_entry(struct bfa_iocpf *iocpf)
574{
Rasesh Mody078086f2011-08-08 16:21:39 +0000575 iocpf->fw_mismatch_notified = false;
Rasesh Mody1d32f762010-12-23 21:45:09 +0000576 iocpf->auto_recover = bfa_nw_auto_recover;
577}
578
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000579/* Beginning state. IOC is in reset state. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000580static void
581bfa_iocpf_sm_reset(struct bfa_iocpf *iocpf, enum iocpf_event event)
582{
583 switch (event) {
584 case IOCPF_E_ENABLE:
585 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fwcheck);
586 break;
587
588 case IOCPF_E_STOP:
589 break;
590
591 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000592 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000593 }
594}
595
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000596/* Semaphore should be acquired for version check. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000597static void
598bfa_iocpf_sm_fwcheck_entry(struct bfa_iocpf *iocpf)
599{
Rasesh Modyd4e16d42011-07-22 08:07:47 +0000600 bfa_ioc_hw_sem_init(iocpf->ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000601 bfa_ioc_hw_sem_get(iocpf->ioc);
602}
603
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000604/* Awaiting h/w semaphore to continue with version check. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000605static void
606bfa_iocpf_sm_fwcheck(struct bfa_iocpf *iocpf, enum iocpf_event event)
607{
608 struct bfa_ioc *ioc = iocpf->ioc;
609
610 switch (event) {
611 case IOCPF_E_SEMLOCKED:
612 if (bfa_ioc_firmware_lock(ioc)) {
Rasesh Mody79ea6c82011-04-14 08:05:18 +0000613 if (bfa_ioc_sync_start(ioc)) {
Rasesh Mody1d32f762010-12-23 21:45:09 +0000614 bfa_ioc_sync_join(ioc);
615 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_hwinit);
616 } else {
617 bfa_ioc_firmware_unlock(ioc);
618 bfa_nw_ioc_hw_sem_release(ioc);
619 mod_timer(&ioc->sem_timer, jiffies +
620 msecs_to_jiffies(BFA_IOC_HWSEM_TOV));
621 }
622 } else {
623 bfa_nw_ioc_hw_sem_release(ioc);
624 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_mismatch);
625 }
626 break;
627
Rasesh Mody078086f2011-08-08 16:21:39 +0000628 case IOCPF_E_SEM_ERROR:
629 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
630 bfa_ioc_pf_hwfailed(ioc);
631 break;
632
Rasesh Mody1d32f762010-12-23 21:45:09 +0000633 case IOCPF_E_DISABLE:
634 bfa_ioc_hw_sem_get_cancel(ioc);
635 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
636 bfa_ioc_pf_disabled(ioc);
637 break;
638
639 case IOCPF_E_STOP:
640 bfa_ioc_hw_sem_get_cancel(ioc);
641 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
642 break;
643
644 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000645 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000646 }
647}
648
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000649/* Notify enable completion callback */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000650static void
651bfa_iocpf_sm_mismatch_entry(struct bfa_iocpf *iocpf)
652{
653 /* Call only the first time sm enters fwmismatch state. */
Joe Perches23677ce2012-02-09 11:17:23 +0000654 if (!iocpf->fw_mismatch_notified)
Rasesh Mody1d32f762010-12-23 21:45:09 +0000655 bfa_ioc_pf_fwmismatch(iocpf->ioc);
656
Rasesh Mody078086f2011-08-08 16:21:39 +0000657 iocpf->fw_mismatch_notified = true;
Rasesh Mody1d32f762010-12-23 21:45:09 +0000658 mod_timer(&(iocpf->ioc)->iocpf_timer, jiffies +
659 msecs_to_jiffies(BFA_IOC_TOV));
660}
661
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000662/* Awaiting firmware version match. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000663static void
664bfa_iocpf_sm_mismatch(struct bfa_iocpf *iocpf, enum iocpf_event event)
665{
666 struct bfa_ioc *ioc = iocpf->ioc;
667
668 switch (event) {
669 case IOCPF_E_TIMEOUT:
670 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fwcheck);
671 break;
672
673 case IOCPF_E_DISABLE:
674 del_timer(&ioc->iocpf_timer);
675 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
676 bfa_ioc_pf_disabled(ioc);
677 break;
678
679 case IOCPF_E_STOP:
680 del_timer(&ioc->iocpf_timer);
681 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
682 break;
683
684 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000685 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000686 }
687}
688
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000689/* Request for semaphore. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000690static void
691bfa_iocpf_sm_semwait_entry(struct bfa_iocpf *iocpf)
692{
693 bfa_ioc_hw_sem_get(iocpf->ioc);
694}
695
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000696/* Awaiting semaphore for h/w initialzation. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000697static void
698bfa_iocpf_sm_semwait(struct bfa_iocpf *iocpf, enum iocpf_event event)
699{
700 struct bfa_ioc *ioc = iocpf->ioc;
701
702 switch (event) {
703 case IOCPF_E_SEMLOCKED:
704 if (bfa_ioc_sync_complete(ioc)) {
705 bfa_ioc_sync_join(ioc);
706 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_hwinit);
707 } else {
708 bfa_nw_ioc_hw_sem_release(ioc);
709 mod_timer(&ioc->sem_timer, jiffies +
710 msecs_to_jiffies(BFA_IOC_HWSEM_TOV));
711 }
712 break;
713
Rasesh Mody078086f2011-08-08 16:21:39 +0000714 case IOCPF_E_SEM_ERROR:
715 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
716 bfa_ioc_pf_hwfailed(ioc);
717 break;
718
Rasesh Mody1d32f762010-12-23 21:45:09 +0000719 case IOCPF_E_DISABLE:
720 bfa_ioc_hw_sem_get_cancel(ioc);
721 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
722 break;
723
724 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000725 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000726 }
727}
728
729static void
730bfa_iocpf_sm_hwinit_entry(struct bfa_iocpf *iocpf)
731{
Rasesh Mody078086f2011-08-08 16:21:39 +0000732 iocpf->poll_time = 0;
Rasesh Modyaafd5c22011-09-27 10:39:09 +0000733 bfa_ioc_reset(iocpf->ioc, false);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000734}
735
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000736/* Hardware is being initialized. Interrupts are enabled.
Rasesh Mody1d32f762010-12-23 21:45:09 +0000737 * Holding hardware semaphore lock.
738 */
739static void
740bfa_iocpf_sm_hwinit(struct bfa_iocpf *iocpf, enum iocpf_event event)
741{
742 struct bfa_ioc *ioc = iocpf->ioc;
743
744 switch (event) {
745 case IOCPF_E_FWREADY:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000746 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_enabling);
747 break;
748
Rasesh Mody1d32f762010-12-23 21:45:09 +0000749 case IOCPF_E_TIMEOUT:
750 bfa_nw_ioc_hw_sem_release(ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000751 bfa_ioc_pf_failed(ioc);
752 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_initfail_sync);
753 break;
754
755 case IOCPF_E_DISABLE:
756 del_timer(&ioc->iocpf_timer);
757 bfa_ioc_sync_leave(ioc);
758 bfa_nw_ioc_hw_sem_release(ioc);
759 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabled);
760 break;
761
762 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000763 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000764 }
765}
766
767static void
768bfa_iocpf_sm_enabling_entry(struct bfa_iocpf *iocpf)
769{
770 mod_timer(&(iocpf->ioc)->iocpf_timer, jiffies +
771 msecs_to_jiffies(BFA_IOC_TOV));
Rasesh Mody078086f2011-08-08 16:21:39 +0000772 /**
773 * Enable Interrupts before sending fw IOC ENABLE cmd.
774 */
775 iocpf->ioc->cbfn->reset_cbfn(iocpf->ioc->bfa);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000776 bfa_ioc_send_enable(iocpf->ioc);
777}
778
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000779/* Host IOC function is being enabled, awaiting response from firmware.
Rasesh Mody1d32f762010-12-23 21:45:09 +0000780 * Semaphore is acquired.
781 */
782static void
783bfa_iocpf_sm_enabling(struct bfa_iocpf *iocpf, enum iocpf_event event)
784{
785 struct bfa_ioc *ioc = iocpf->ioc;
786
787 switch (event) {
788 case IOCPF_E_FWRSP_ENABLE:
789 del_timer(&ioc->iocpf_timer);
790 bfa_nw_ioc_hw_sem_release(ioc);
791 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_ready);
792 break;
793
794 case IOCPF_E_INITFAIL:
795 del_timer(&ioc->iocpf_timer);
796 /*
797 * !!! fall through !!!
798 */
799 case IOCPF_E_TIMEOUT:
800 bfa_nw_ioc_hw_sem_release(ioc);
801 if (event == IOCPF_E_TIMEOUT)
802 bfa_ioc_pf_failed(ioc);
803 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_initfail_sync);
804 break;
805
806 case IOCPF_E_DISABLE:
807 del_timer(&ioc->iocpf_timer);
808 bfa_nw_ioc_hw_sem_release(ioc);
809 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling);
810 break;
811
Rasesh Mody1d32f762010-12-23 21:45:09 +0000812 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000813 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000814 }
815}
816
Rasesh Mody1d32f762010-12-23 21:45:09 +0000817static void
818bfa_iocpf_sm_ready_entry(struct bfa_iocpf *iocpf)
819{
820 bfa_ioc_pf_enabled(iocpf->ioc);
821}
822
823static void
824bfa_iocpf_sm_ready(struct bfa_iocpf *iocpf, enum iocpf_event event)
825{
Rasesh Mody1d32f762010-12-23 21:45:09 +0000826 switch (event) {
827 case IOCPF_E_DISABLE:
828 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling);
829 break;
830
831 case IOCPF_E_GETATTRFAIL:
832 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_initfail_sync);
833 break;
834
835 case IOCPF_E_FAIL:
836 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail_sync);
837 break;
838
Rasesh Mody1d32f762010-12-23 21:45:09 +0000839 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000840 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000841 }
842}
843
844static void
845bfa_iocpf_sm_disabling_entry(struct bfa_iocpf *iocpf)
846{
847 mod_timer(&(iocpf->ioc)->iocpf_timer, jiffies +
848 msecs_to_jiffies(BFA_IOC_TOV));
849 bfa_ioc_send_disable(iocpf->ioc);
850}
851
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000852/* IOC is being disabled */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000853static void
854bfa_iocpf_sm_disabling(struct bfa_iocpf *iocpf, enum iocpf_event event)
855{
856 struct bfa_ioc *ioc = iocpf->ioc;
857
858 switch (event) {
859 case IOCPF_E_FWRSP_DISABLE:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000860 del_timer(&ioc->iocpf_timer);
861 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
862 break;
863
864 case IOCPF_E_FAIL:
865 del_timer(&ioc->iocpf_timer);
866 /*
867 * !!! fall through !!!
868 */
869
870 case IOCPF_E_TIMEOUT:
Rasesh Mody41ed9032013-12-17 17:07:32 -0800871 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_FAIL);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000872 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
873 break;
874
875 case IOCPF_E_FWRSP_ENABLE:
876 break;
877
878 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000879 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000880 }
881}
882
883static void
884bfa_iocpf_sm_disabling_sync_entry(struct bfa_iocpf *iocpf)
885{
886 bfa_ioc_hw_sem_get(iocpf->ioc);
887}
888
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000889/* IOC hb ack request is being removed. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000890static void
891bfa_iocpf_sm_disabling_sync(struct bfa_iocpf *iocpf, enum iocpf_event event)
892{
893 struct bfa_ioc *ioc = iocpf->ioc;
894
895 switch (event) {
896 case IOCPF_E_SEMLOCKED:
897 bfa_ioc_sync_leave(ioc);
898 bfa_nw_ioc_hw_sem_release(ioc);
899 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabled);
900 break;
901
Rasesh Mody078086f2011-08-08 16:21:39 +0000902 case IOCPF_E_SEM_ERROR:
903 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
904 bfa_ioc_pf_hwfailed(ioc);
905 break;
906
Rasesh Mody1d32f762010-12-23 21:45:09 +0000907 case IOCPF_E_FAIL:
908 break;
909
910 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000911 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000912 }
913}
914
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000915/* IOC disable completion entry. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000916static void
917bfa_iocpf_sm_disabled_entry(struct bfa_iocpf *iocpf)
918{
Rasesh Modyfdad4002011-07-22 08:07:45 +0000919 bfa_ioc_mbox_flush(iocpf->ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000920 bfa_ioc_pf_disabled(iocpf->ioc);
921}
922
923static void
924bfa_iocpf_sm_disabled(struct bfa_iocpf *iocpf, enum iocpf_event event)
925{
926 struct bfa_ioc *ioc = iocpf->ioc;
927
928 switch (event) {
929 case IOCPF_E_ENABLE:
Rasesh Mody1d32f762010-12-23 21:45:09 +0000930 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_semwait);
931 break;
932
933 case IOCPF_E_STOP:
934 bfa_ioc_firmware_unlock(ioc);
935 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
936 break;
937
938 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000939 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000940 }
941}
942
943static void
944bfa_iocpf_sm_initfail_sync_entry(struct bfa_iocpf *iocpf)
945{
Krishna Gudipati7afc5db2011-12-22 13:30:19 +0000946 bfa_nw_ioc_debug_save_ftrc(iocpf->ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000947 bfa_ioc_hw_sem_get(iocpf->ioc);
948}
949
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000950/* Hardware initialization failed. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000951static void
952bfa_iocpf_sm_initfail_sync(struct bfa_iocpf *iocpf, enum iocpf_event event)
953{
954 struct bfa_ioc *ioc = iocpf->ioc;
955
956 switch (event) {
957 case IOCPF_E_SEMLOCKED:
958 bfa_ioc_notify_fail(ioc);
Rasesh Mody078086f2011-08-08 16:21:39 +0000959 bfa_ioc_sync_leave(ioc);
Rasesh Mody41ed9032013-12-17 17:07:32 -0800960 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_FAIL);
Rasesh Mody078086f2011-08-08 16:21:39 +0000961 bfa_nw_ioc_hw_sem_release(ioc);
962 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_initfail);
963 break;
964
965 case IOCPF_E_SEM_ERROR:
966 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
967 bfa_ioc_pf_hwfailed(ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000968 break;
969
970 case IOCPF_E_DISABLE:
971 bfa_ioc_hw_sem_get_cancel(ioc);
972 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
973 break;
974
975 case IOCPF_E_STOP:
976 bfa_ioc_hw_sem_get_cancel(ioc);
977 bfa_ioc_firmware_unlock(ioc);
978 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
979 break;
980
981 case IOCPF_E_FAIL:
982 break;
983
984 default:
Rasesh Modyac51f602011-07-22 08:07:43 +0000985 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +0000986 }
987}
988
989static void
990bfa_iocpf_sm_initfail_entry(struct bfa_iocpf *iocpf)
991{
Rasesh Mody1d32f762010-12-23 21:45:09 +0000992}
993
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000994/* Hardware initialization failed. */
Rasesh Mody1d32f762010-12-23 21:45:09 +0000995static void
996bfa_iocpf_sm_initfail(struct bfa_iocpf *iocpf, enum iocpf_event event)
997{
998 struct bfa_ioc *ioc = iocpf->ioc;
999
1000 switch (event) {
1001 case IOCPF_E_DISABLE:
1002 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabled);
1003 break;
1004
1005 case IOCPF_E_STOP:
1006 bfa_ioc_firmware_unlock(ioc);
1007 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_reset);
1008 break;
1009
1010 default:
Rasesh Modyac51f602011-07-22 08:07:43 +00001011 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001012 }
1013}
1014
1015static void
1016bfa_iocpf_sm_fail_sync_entry(struct bfa_iocpf *iocpf)
1017{
1018 /**
1019 * Mark IOC as failed in hardware and stop firmware.
1020 */
1021 bfa_ioc_lpu_stop(iocpf->ioc);
1022
1023 /**
1024 * Flush any queued up mailbox requests.
1025 */
Rasesh Modyfdad4002011-07-22 08:07:45 +00001026 bfa_ioc_mbox_flush(iocpf->ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001027 bfa_ioc_hw_sem_get(iocpf->ioc);
1028}
1029
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001030/* IOC is in failed state. */
Rasesh Mody1d32f762010-12-23 21:45:09 +00001031static void
1032bfa_iocpf_sm_fail_sync(struct bfa_iocpf *iocpf, enum iocpf_event event)
1033{
1034 struct bfa_ioc *ioc = iocpf->ioc;
1035
1036 switch (event) {
1037 case IOCPF_E_SEMLOCKED:
Rasesh Mody1d32f762010-12-23 21:45:09 +00001038 bfa_ioc_sync_ack(ioc);
1039 bfa_ioc_notify_fail(ioc);
1040 if (!iocpf->auto_recover) {
1041 bfa_ioc_sync_leave(ioc);
Rasesh Mody41ed9032013-12-17 17:07:32 -08001042 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_FAIL);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001043 bfa_nw_ioc_hw_sem_release(ioc);
1044 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
1045 } else {
1046 if (bfa_ioc_sync_complete(ioc))
1047 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_hwinit);
1048 else {
1049 bfa_nw_ioc_hw_sem_release(ioc);
1050 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_semwait);
1051 }
1052 }
1053 break;
1054
Rasesh Mody078086f2011-08-08 16:21:39 +00001055 case IOCPF_E_SEM_ERROR:
1056 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_fail);
1057 bfa_ioc_pf_hwfailed(ioc);
1058 break;
1059
Rasesh Mody1d32f762010-12-23 21:45:09 +00001060 case IOCPF_E_DISABLE:
1061 bfa_ioc_hw_sem_get_cancel(ioc);
1062 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabling_sync);
1063 break;
1064
1065 case IOCPF_E_FAIL:
1066 break;
1067
1068 default:
Rasesh Modyac51f602011-07-22 08:07:43 +00001069 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001070 }
1071}
1072
1073static void
1074bfa_iocpf_sm_fail_entry(struct bfa_iocpf *iocpf)
1075{
1076}
1077
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001078/* IOC is in failed state. */
Rasesh Mody1d32f762010-12-23 21:45:09 +00001079static void
1080bfa_iocpf_sm_fail(struct bfa_iocpf *iocpf, enum iocpf_event event)
1081{
1082 switch (event) {
1083 case IOCPF_E_DISABLE:
1084 bfa_fsm_set_state(iocpf, bfa_iocpf_sm_disabled);
1085 break;
1086
1087 default:
Rasesh Modyac51f602011-07-22 08:07:43 +00001088 bfa_sm_fault(event);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001089 }
1090}
1091
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001092/* BFA IOC private functions */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001093
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001094/* Notify common modules registered for notification. */
Rasesh Modybd5a92e2011-07-22 08:07:42 +00001095static void
1096bfa_ioc_event_notify(struct bfa_ioc *ioc, enum bfa_ioc_event event)
1097{
1098 struct bfa_ioc_notify *notify;
1099 struct list_head *qe;
1100
1101 list_for_each(qe, &ioc->notify_q) {
1102 notify = (struct bfa_ioc_notify *)qe;
1103 notify->cbfn(notify->cbarg, event);
1104 }
1105}
1106
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001107static void
1108bfa_ioc_disable_comp(struct bfa_ioc *ioc)
1109{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001110 ioc->cbfn->disable_cbfn(ioc->bfa);
Rasesh Modybd5a92e2011-07-22 08:07:42 +00001111 bfa_ioc_event_notify(ioc, BFA_IOC_E_DISABLED);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001112}
1113
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001114bool
Rasesh Mody8a891422010-08-25 23:00:27 -07001115bfa_nw_ioc_sem_get(void __iomem *sem_reg)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001116{
1117 u32 r32;
1118 int cnt = 0;
1119#define BFA_SEM_SPINCNT 3000
1120
1121 r32 = readl(sem_reg);
1122
Rasesh Mody078086f2011-08-08 16:21:39 +00001123 while ((r32 & 1) && (cnt < BFA_SEM_SPINCNT)) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001124 cnt++;
1125 udelay(2);
1126 r32 = readl(sem_reg);
1127 }
1128
Rasesh Mody078086f2011-08-08 16:21:39 +00001129 if (!(r32 & 1))
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001130 return true;
1131
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001132 return false;
1133}
1134
1135void
Rasesh Mody8a891422010-08-25 23:00:27 -07001136bfa_nw_ioc_sem_release(void __iomem *sem_reg)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001137{
Rasesh Mody1d51a132011-09-16 15:06:46 +00001138 readl(sem_reg);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001139 writel(1, sem_reg);
1140}
1141
Jing Huange491c772012-04-04 05:42:08 +00001142/* Clear fwver hdr */
1143static void
1144bfa_ioc_fwver_clear(struct bfa_ioc *ioc)
1145{
1146 u32 pgnum, pgoff, loff = 0;
1147 int i;
1148
1149 pgnum = PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, loff);
1150 pgoff = PSS_SMEM_PGOFF(loff);
1151 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
1152
1153 for (i = 0; i < (sizeof(struct bfi_ioc_image_hdr) / sizeof(u32)); i++) {
1154 writel(0, ioc->ioc_regs.smem_page_start + loff);
1155 loff += sizeof(u32);
1156 }
1157}
1158
1159
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001160static void
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001161bfa_ioc_hw_sem_init(struct bfa_ioc *ioc)
1162{
1163 struct bfi_ioc_image_hdr fwhdr;
Jing Huange491c772012-04-04 05:42:08 +00001164 u32 fwstate, r32;
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001165
Jing Huange491c772012-04-04 05:42:08 +00001166 /* Spin on init semaphore to serialize. */
1167 r32 = readl(ioc->ioc_regs.ioc_init_sem_reg);
1168 while (r32 & 0x1) {
1169 udelay(20);
1170 r32 = readl(ioc->ioc_regs.ioc_init_sem_reg);
1171 }
1172
Rasesh Mody41ed9032013-12-17 17:07:32 -08001173 fwstate = bfa_ioc_get_cur_ioc_fwstate(ioc);
Jing Huange491c772012-04-04 05:42:08 +00001174 if (fwstate == BFI_IOC_UNINIT) {
1175 writel(1, ioc->ioc_regs.ioc_init_sem_reg);
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001176 return;
Jing Huange491c772012-04-04 05:42:08 +00001177 }
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001178
1179 bfa_nw_ioc_fwver_get(ioc, &fwhdr);
1180
Jing Huange491c772012-04-04 05:42:08 +00001181 if (swab32(fwhdr.exec) == BFI_FWBOOT_TYPE_NORMAL) {
1182 writel(1, ioc->ioc_regs.ioc_init_sem_reg);
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001183 return;
Jing Huange491c772012-04-04 05:42:08 +00001184 }
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001185
Jing Huange491c772012-04-04 05:42:08 +00001186 bfa_ioc_fwver_clear(ioc);
Rasesh Mody41ed9032013-12-17 17:07:32 -08001187 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_UNINIT);
1188 bfa_ioc_set_alt_ioc_fwstate(ioc, BFI_IOC_UNINIT);
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001189
1190 /*
1191 * Try to lock and then unlock the semaphore.
1192 */
1193 readl(ioc->ioc_regs.ioc_sem_reg);
1194 writel(1, ioc->ioc_regs.ioc_sem_reg);
Jing Huange491c772012-04-04 05:42:08 +00001195
1196 /* Unlock init semaphore */
1197 writel(1, ioc->ioc_regs.ioc_init_sem_reg);
Rasesh Modyd4e16d42011-07-22 08:07:47 +00001198}
1199
1200static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001201bfa_ioc_hw_sem_get(struct bfa_ioc *ioc)
1202{
1203 u32 r32;
1204
1205 /**
1206 * First read to the semaphore register will return 0, subsequent reads
1207 * will return 1. Semaphore is released by writing 1 to the register
1208 */
1209 r32 = readl(ioc->ioc_regs.ioc_sem_reg);
Rasesh Mody078086f2011-08-08 16:21:39 +00001210 if (r32 == ~0) {
1211 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_SEM_ERROR);
1212 return;
1213 }
1214 if (!(r32 & 1)) {
Rasesh Mody1d32f762010-12-23 21:45:09 +00001215 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_SEMLOCKED);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001216 return;
1217 }
1218
1219 mod_timer(&ioc->sem_timer, jiffies +
1220 msecs_to_jiffies(BFA_IOC_HWSEM_TOV));
1221}
1222
1223void
Rasesh Mody8a891422010-08-25 23:00:27 -07001224bfa_nw_ioc_hw_sem_release(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001225{
1226 writel(1, ioc->ioc_regs.ioc_sem_reg);
1227}
1228
1229static void
1230bfa_ioc_hw_sem_get_cancel(struct bfa_ioc *ioc)
1231{
1232 del_timer(&ioc->sem_timer);
1233}
1234
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001235/* Initialize LPU local memory (aka secondary memory / SRAM) */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001236static void
1237bfa_ioc_lmem_init(struct bfa_ioc *ioc)
1238{
1239 u32 pss_ctl;
1240 int i;
1241#define PSS_LMEM_INIT_TIME 10000
1242
1243 pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1244 pss_ctl &= ~__PSS_LMEM_RESET;
1245 pss_ctl |= __PSS_LMEM_INIT_EN;
1246
1247 /*
1248 * i2c workaround 12.5khz clock
1249 */
1250 pss_ctl |= __PSS_I2C_CLK_DIV(3UL);
1251 writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1252
1253 /**
1254 * wait for memory initialization to be complete
1255 */
1256 i = 0;
1257 do {
1258 pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1259 i++;
1260 } while (!(pss_ctl & __PSS_LMEM_INIT_DONE) && (i < PSS_LMEM_INIT_TIME));
1261
1262 /**
1263 * If memory initialization is not successful, IOC timeout will catch
1264 * such failures.
1265 */
1266 BUG_ON(!(pss_ctl & __PSS_LMEM_INIT_DONE));
1267
1268 pss_ctl &= ~(__PSS_LMEM_INIT_DONE | __PSS_LMEM_INIT_EN);
1269 writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1270}
1271
1272static void
1273bfa_ioc_lpu_start(struct bfa_ioc *ioc)
1274{
1275 u32 pss_ctl;
1276
1277 /**
1278 * Take processor out of reset.
1279 */
1280 pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1281 pss_ctl &= ~__PSS_LPU0_RESET;
1282
1283 writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1284}
1285
1286static void
1287bfa_ioc_lpu_stop(struct bfa_ioc *ioc)
1288{
1289 u32 pss_ctl;
1290
1291 /**
1292 * Put processors in reset.
1293 */
1294 pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
1295 pss_ctl |= (__PSS_LPU0_RESET | __PSS_LPU1_RESET);
1296
1297 writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
1298}
1299
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001300/* Get driver and firmware versions. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001301void
Rasesh Mody8a891422010-08-25 23:00:27 -07001302bfa_nw_ioc_fwver_get(struct bfa_ioc *ioc, struct bfi_ioc_image_hdr *fwhdr)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001303{
David S. Miller58598542011-04-17 16:51:36 -07001304 u32 pgnum;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001305 u32 loff = 0;
1306 int i;
1307 u32 *fwsig = (u32 *) fwhdr;
1308
1309 pgnum = bfa_ioc_smem_pgnum(ioc, loff);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001310 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
1311
1312 for (i = 0; i < (sizeof(struct bfi_ioc_image_hdr) / sizeof(u32));
1313 i++) {
1314 fwsig[i] =
1315 swab32(readl((loff) + (ioc->ioc_regs.smem_page_start)));
1316 loff += sizeof(u32);
1317 }
1318}
1319
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001320/* Returns TRUE if same. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001321bool
Rasesh Mody8a891422010-08-25 23:00:27 -07001322bfa_nw_ioc_fwver_cmp(struct bfa_ioc *ioc, struct bfi_ioc_image_hdr *fwhdr)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001323{
1324 struct bfi_ioc_image_hdr *drv_fwhdr;
1325 int i;
1326
1327 drv_fwhdr = (struct bfi_ioc_image_hdr *)
Rasesh Mody078086f2011-08-08 16:21:39 +00001328 bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc), 0);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001329
1330 for (i = 0; i < BFI_IOC_MD5SUM_SZ; i++) {
1331 if (fwhdr->md5sum[i] != drv_fwhdr->md5sum[i])
1332 return false;
1333 }
1334
1335 return true;
1336}
1337
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001338/* Return true if current running version is valid. Firmware signature and
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001339 * execution context (driver/bios) must match.
1340 */
1341static bool
Rasesh Mody79ea6c82011-04-14 08:05:18 +00001342bfa_ioc_fwver_valid(struct bfa_ioc *ioc, u32 boot_env)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001343{
1344 struct bfi_ioc_image_hdr fwhdr, *drv_fwhdr;
1345
Rasesh Mody8a891422010-08-25 23:00:27 -07001346 bfa_nw_ioc_fwver_get(ioc, &fwhdr);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001347 drv_fwhdr = (struct bfi_ioc_image_hdr *)
Rasesh Mody078086f2011-08-08 16:21:39 +00001348 bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc), 0);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001349
1350 if (fwhdr.signature != drv_fwhdr->signature)
1351 return false;
1352
Rasesh Mody078086f2011-08-08 16:21:39 +00001353 if (swab32(fwhdr.bootenv) != boot_env)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001354 return false;
1355
Rasesh Mody8a891422010-08-25 23:00:27 -07001356 return bfa_nw_ioc_fwver_cmp(ioc, &fwhdr);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001357}
1358
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001359/* Conditionally flush any pending message from firmware at start. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001360static void
1361bfa_ioc_msgflush(struct bfa_ioc *ioc)
1362{
1363 u32 r32;
1364
1365 r32 = readl(ioc->ioc_regs.lpu_mbox_cmd);
1366 if (r32)
1367 writel(1, ioc->ioc_regs.lpu_mbox_cmd);
1368}
1369
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001370static void
1371bfa_ioc_hwinit(struct bfa_ioc *ioc, bool force)
1372{
1373 enum bfi_ioc_state ioc_fwstate;
1374 bool fwvalid;
Rasesh Mody79ea6c82011-04-14 08:05:18 +00001375 u32 boot_env;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001376
Rasesh Mody41ed9032013-12-17 17:07:32 -08001377 ioc_fwstate = bfa_ioc_get_cur_ioc_fwstate(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001378
1379 if (force)
1380 ioc_fwstate = BFI_IOC_UNINIT;
1381
Rasesh Mody078086f2011-08-08 16:21:39 +00001382 boot_env = BFI_FWBOOT_ENV_OS;
1383
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001384 /**
1385 * check if firmware is valid
1386 */
1387 fwvalid = (ioc_fwstate == BFI_IOC_UNINIT) ?
Rasesh Mody79ea6c82011-04-14 08:05:18 +00001388 false : bfa_ioc_fwver_valid(ioc, boot_env);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001389
1390 if (!fwvalid) {
Rasesh Mody078086f2011-08-08 16:21:39 +00001391 bfa_ioc_boot(ioc, BFI_FWBOOT_TYPE_NORMAL, boot_env);
1392 bfa_ioc_poll_fwinit(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001393 return;
1394 }
1395
1396 /**
1397 * If hardware initialization is in progress (initialized by other IOC),
1398 * just wait for an initialization completion interrupt.
1399 */
1400 if (ioc_fwstate == BFI_IOC_INITING) {
Rasesh Mody078086f2011-08-08 16:21:39 +00001401 bfa_ioc_poll_fwinit(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001402 return;
1403 }
1404
1405 /**
1406 * If IOC function is disabled and firmware version is same,
1407 * just re-enable IOC.
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001408 */
Rasesh Mody2c7d3822010-12-23 21:45:06 +00001409 if (ioc_fwstate == BFI_IOC_DISABLED || ioc_fwstate == BFI_IOC_OP) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001410 /**
1411 * When using MSI-X any pending firmware ready event should
1412 * be flushed. Otherwise MSI-X interrupts are not delivered.
1413 */
1414 bfa_ioc_msgflush(ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001415 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_FWREADY);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001416 return;
1417 }
1418
1419 /**
1420 * Initialize the h/w for any other states.
1421 */
Rasesh Mody078086f2011-08-08 16:21:39 +00001422 bfa_ioc_boot(ioc, BFI_FWBOOT_TYPE_NORMAL, boot_env);
1423 bfa_ioc_poll_fwinit(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001424}
1425
1426void
Rasesh Mody8a891422010-08-25 23:00:27 -07001427bfa_nw_ioc_timeout(void *ioc_arg)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001428{
1429 struct bfa_ioc *ioc = (struct bfa_ioc *) ioc_arg;
1430
1431 bfa_fsm_send_event(ioc, IOC_E_TIMEOUT);
1432}
1433
Rasesh Mody8a891422010-08-25 23:00:27 -07001434static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001435bfa_ioc_mbox_send(struct bfa_ioc *ioc, void *ioc_msg, int len)
1436{
1437 u32 *msgp = (u32 *) ioc_msg;
1438 u32 i;
1439
1440 BUG_ON(!(len <= BFI_IOC_MSGLEN_MAX));
1441
1442 /*
1443 * first write msg to mailbox registers
1444 */
1445 for (i = 0; i < len / sizeof(u32); i++)
1446 writel(cpu_to_le32(msgp[i]),
1447 ioc->ioc_regs.hfn_mbox + i * sizeof(u32));
1448
1449 for (; i < BFI_IOC_MSGLEN_MAX / sizeof(u32); i++)
1450 writel(0, ioc->ioc_regs.hfn_mbox + i * sizeof(u32));
1451
1452 /*
1453 * write 1 to mailbox CMD to trigger LPU event
1454 */
1455 writel(1, ioc->ioc_regs.hfn_mbox_cmd);
1456 (void) readl(ioc->ioc_regs.hfn_mbox_cmd);
1457}
1458
1459static void
1460bfa_ioc_send_enable(struct bfa_ioc *ioc)
1461{
1462 struct bfi_ioc_ctrl_req enable_req;
1463 struct timeval tv;
1464
1465 bfi_h2i_set(enable_req.mh, BFI_MC_IOC, BFI_IOC_H2I_ENABLE_REQ,
1466 bfa_ioc_portid(ioc));
Rasesh Mody078086f2011-08-08 16:21:39 +00001467 enable_req.clscode = htons(ioc->clscode);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001468 do_gettimeofday(&tv);
1469 enable_req.tv_sec = ntohl(tv.tv_sec);
1470 bfa_ioc_mbox_send(ioc, &enable_req, sizeof(struct bfi_ioc_ctrl_req));
1471}
1472
1473static void
1474bfa_ioc_send_disable(struct bfa_ioc *ioc)
1475{
1476 struct bfi_ioc_ctrl_req disable_req;
1477
1478 bfi_h2i_set(disable_req.mh, BFI_MC_IOC, BFI_IOC_H2I_DISABLE_REQ,
1479 bfa_ioc_portid(ioc));
1480 bfa_ioc_mbox_send(ioc, &disable_req, sizeof(struct bfi_ioc_ctrl_req));
1481}
1482
1483static void
1484bfa_ioc_send_getattr(struct bfa_ioc *ioc)
1485{
1486 struct bfi_ioc_getattr_req attr_req;
1487
1488 bfi_h2i_set(attr_req.mh, BFI_MC_IOC, BFI_IOC_H2I_GETATTR_REQ,
1489 bfa_ioc_portid(ioc));
1490 bfa_dma_be_addr_set(attr_req.attr_addr, ioc->attr_dma.pa);
1491 bfa_ioc_mbox_send(ioc, &attr_req, sizeof(attr_req));
1492}
1493
1494void
Rasesh Mody8a891422010-08-25 23:00:27 -07001495bfa_nw_ioc_hb_check(void *cbarg)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001496{
1497 struct bfa_ioc *ioc = cbarg;
1498 u32 hb_count;
1499
1500 hb_count = readl(ioc->ioc_regs.heartbeat);
1501 if (ioc->hb_count == hb_count) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001502 bfa_ioc_recover(ioc);
1503 return;
1504 } else {
1505 ioc->hb_count = hb_count;
1506 }
1507
1508 bfa_ioc_mbox_poll(ioc);
1509 mod_timer(&ioc->hb_timer, jiffies +
1510 msecs_to_jiffies(BFA_IOC_HB_TOV));
1511}
1512
1513static void
1514bfa_ioc_hb_monitor(struct bfa_ioc *ioc)
1515{
1516 ioc->hb_count = readl(ioc->ioc_regs.heartbeat);
1517 mod_timer(&ioc->hb_timer, jiffies +
1518 msecs_to_jiffies(BFA_IOC_HB_TOV));
1519}
1520
1521static void
1522bfa_ioc_hb_stop(struct bfa_ioc *ioc)
1523{
1524 del_timer(&ioc->hb_timer);
1525}
1526
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001527/* Initiate a full firmware download. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001528static void
1529bfa_ioc_download_fw(struct bfa_ioc *ioc, u32 boot_type,
Rasesh Mody79ea6c82011-04-14 08:05:18 +00001530 u32 boot_env)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001531{
1532 u32 *fwimg;
David S. Miller58598542011-04-17 16:51:36 -07001533 u32 pgnum;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001534 u32 loff = 0;
1535 u32 chunkno = 0;
1536 u32 i;
Rasesh Mody078086f2011-08-08 16:21:39 +00001537 u32 asicmode;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001538
Rasesh Mody078086f2011-08-08 16:21:39 +00001539 fwimg = bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc), chunkno);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001540
1541 pgnum = bfa_ioc_smem_pgnum(ioc, loff);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001542
1543 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
1544
Rasesh Mody078086f2011-08-08 16:21:39 +00001545 for (i = 0; i < bfa_cb_image_get_size(bfa_ioc_asic_gen(ioc)); i++) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001546 if (BFA_IOC_FLASH_CHUNK_NO(i) != chunkno) {
1547 chunkno = BFA_IOC_FLASH_CHUNK_NO(i);
Rasesh Mody078086f2011-08-08 16:21:39 +00001548 fwimg = bfa_cb_image_get_chunk(bfa_ioc_asic_gen(ioc),
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001549 BFA_IOC_FLASH_CHUNK_ADDR(chunkno));
1550 }
1551
1552 /**
1553 * write smem
1554 */
1555 writel((swab32(fwimg[BFA_IOC_FLASH_OFFSET_IN_CHUNK(i)])),
1556 ((ioc->ioc_regs.smem_page_start) + (loff)));
1557
1558 loff += sizeof(u32);
1559
1560 /**
1561 * handle page offset wrap around
1562 */
1563 loff = PSS_SMEM_PGOFF(loff);
1564 if (loff == 0) {
1565 pgnum++;
1566 writel(pgnum,
1567 ioc->ioc_regs.host_page_num_fn);
1568 }
1569 }
1570
1571 writel(bfa_ioc_smem_pgnum(ioc, 0),
1572 ioc->ioc_regs.host_page_num_fn);
1573
1574 /*
Rasesh Mody078086f2011-08-08 16:21:39 +00001575 * Set boot type, env and device mode at the end.
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001576 */
Rasesh Mody078086f2011-08-08 16:21:39 +00001577 asicmode = BFI_FWBOOT_DEVMODE(ioc->asic_gen, ioc->asic_mode,
1578 ioc->port0_mode, ioc->port1_mode);
1579 writel(asicmode, ((ioc->ioc_regs.smem_page_start)
1580 + BFI_FWBOOT_DEVMODE_OFF));
Rasesh Mody79ea6c82011-04-14 08:05:18 +00001581 writel(boot_type, ((ioc->ioc_regs.smem_page_start)
Rasesh Mody078086f2011-08-08 16:21:39 +00001582 + (BFI_FWBOOT_TYPE_OFF)));
Rasesh Mody79ea6c82011-04-14 08:05:18 +00001583 writel(boot_env, ((ioc->ioc_regs.smem_page_start)
Rasesh Mody078086f2011-08-08 16:21:39 +00001584 + (BFI_FWBOOT_ENV_OFF)));
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001585}
1586
1587static void
1588bfa_ioc_reset(struct bfa_ioc *ioc, bool force)
1589{
1590 bfa_ioc_hwinit(ioc, force);
1591}
1592
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001593/* BFA ioc enable reply by firmware */
Rasesh Mody078086f2011-08-08 16:21:39 +00001594static void
1595bfa_ioc_enable_reply(struct bfa_ioc *ioc, enum bfa_mode port_mode,
1596 u8 cap_bm)
1597{
1598 struct bfa_iocpf *iocpf = &ioc->iocpf;
1599
1600 ioc->port_mode = ioc->port_mode_cfg = port_mode;
1601 ioc->ad_cap_bm = cap_bm;
1602 bfa_fsm_send_event(iocpf, IOCPF_E_FWRSP_ENABLE);
1603}
1604
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001605/* Update BFA configuration from firmware configuration. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001606static void
1607bfa_ioc_getattr_reply(struct bfa_ioc *ioc)
1608{
1609 struct bfi_ioc_attr *attr = ioc->attr;
1610
1611 attr->adapter_prop = ntohl(attr->adapter_prop);
1612 attr->card_type = ntohl(attr->card_type);
1613 attr->maxfrsize = ntohs(attr->maxfrsize);
1614
1615 bfa_fsm_send_event(ioc, IOC_E_FWRSP_GETATTR);
1616}
1617
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001618/* Attach time initialization of mbox logic. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001619static void
1620bfa_ioc_mbox_attach(struct bfa_ioc *ioc)
1621{
1622 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
1623 int mc;
1624
1625 INIT_LIST_HEAD(&mod->cmd_q);
1626 for (mc = 0; mc < BFI_MC_MAX; mc++) {
1627 mod->mbhdlr[mc].cbfn = NULL;
1628 mod->mbhdlr[mc].cbarg = ioc->bfa;
1629 }
1630}
1631
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001632/* Mbox poll timer -- restarts any pending mailbox requests. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001633static void
1634bfa_ioc_mbox_poll(struct bfa_ioc *ioc)
1635{
1636 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
1637 struct bfa_mbox_cmd *cmd;
Rasesh Mody078086f2011-08-08 16:21:39 +00001638 bfa_mbox_cmd_cbfn_t cbfn;
1639 void *cbarg;
1640 u32 stat;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001641
1642 /**
1643 * If no command pending, do nothing
1644 */
1645 if (list_empty(&mod->cmd_q))
1646 return;
1647
1648 /**
1649 * If previous command is not yet fetched by firmware, do nothing
1650 */
1651 stat = readl(ioc->ioc_regs.hfn_mbox_cmd);
1652 if (stat)
1653 return;
1654
1655 /**
1656 * Enqueue command to firmware.
1657 */
1658 bfa_q_deq(&mod->cmd_q, &cmd);
1659 bfa_ioc_mbox_send(ioc, cmd->msg, sizeof(cmd->msg));
Rasesh Mody078086f2011-08-08 16:21:39 +00001660
1661 /**
1662 * Give a callback to the client, indicating that the command is sent
1663 */
1664 if (cmd->cbfn) {
1665 cbfn = cmd->cbfn;
1666 cbarg = cmd->cbarg;
1667 cmd->cbfn = NULL;
1668 cbfn(cbarg);
1669 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001670}
1671
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001672/* Cleanup any pending requests. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001673static void
Rasesh Modyfdad4002011-07-22 08:07:45 +00001674bfa_ioc_mbox_flush(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001675{
1676 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
1677 struct bfa_mbox_cmd *cmd;
1678
1679 while (!list_empty(&mod->cmd_q))
1680 bfa_q_deq(&mod->cmd_q, &cmd);
1681}
1682
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00001683/**
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001684 * bfa_nw_ioc_smem_read - Read data from SMEM to host through PCI memmap
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00001685 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001686 * @ioc: memory for IOC
1687 * @tbuf: app memory to store data from smem
1688 * @soff: smem offset
1689 * @sz: size of smem in bytes
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00001690 */
1691static int
1692bfa_nw_ioc_smem_read(struct bfa_ioc *ioc, void *tbuf, u32 soff, u32 sz)
1693{
1694 u32 pgnum, loff, r32;
1695 int i, len;
1696 u32 *buf = tbuf;
1697
1698 pgnum = PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, soff);
1699 loff = PSS_SMEM_PGOFF(soff);
1700
1701 /*
1702 * Hold semaphore to serialize pll init and fwtrc.
1703 */
1704 if (bfa_nw_ioc_sem_get(ioc->ioc_regs.ioc_init_sem_reg) == 0)
1705 return 1;
1706
1707 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
1708
1709 len = sz/sizeof(u32);
1710 for (i = 0; i < len; i++) {
1711 r32 = swab32(readl((loff) + (ioc->ioc_regs.smem_page_start)));
1712 buf[i] = be32_to_cpu(r32);
1713 loff += sizeof(u32);
1714
1715 /**
1716 * handle page offset wrap around
1717 */
1718 loff = PSS_SMEM_PGOFF(loff);
1719 if (loff == 0) {
1720 pgnum++;
1721 writel(pgnum, ioc->ioc_regs.host_page_num_fn);
1722 }
1723 }
1724
1725 writel(PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, 0),
1726 ioc->ioc_regs.host_page_num_fn);
1727
1728 /*
1729 * release semaphore
1730 */
1731 readl(ioc->ioc_regs.ioc_init_sem_reg);
1732 writel(1, ioc->ioc_regs.ioc_init_sem_reg);
1733 return 0;
1734}
1735
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001736/* Retrieve saved firmware trace from a prior IOC failure. */
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00001737int
1738bfa_nw_ioc_debug_fwtrc(struct bfa_ioc *ioc, void *trcdata, int *trclen)
1739{
1740 u32 loff = BFI_IOC_TRC_OFF + BNA_DBG_FWTRC_LEN * ioc->port_id;
1741 int tlen, status = 0;
1742
1743 tlen = *trclen;
1744 if (tlen > BNA_DBG_FWTRC_LEN)
1745 tlen = BNA_DBG_FWTRC_LEN;
1746
1747 status = bfa_nw_ioc_smem_read(ioc, trcdata, loff, tlen);
1748 *trclen = tlen;
1749 return status;
1750}
1751
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001752/* Save firmware trace if configured. */
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00001753static void
1754bfa_nw_ioc_debug_save_ftrc(struct bfa_ioc *ioc)
1755{
1756 int tlen;
1757
1758 if (ioc->dbg_fwsave_once) {
1759 ioc->dbg_fwsave_once = 0;
1760 if (ioc->dbg_fwsave_len) {
1761 tlen = ioc->dbg_fwsave_len;
1762 bfa_nw_ioc_debug_fwtrc(ioc, ioc->dbg_fwsave, &tlen);
1763 }
1764 }
1765}
1766
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001767/* Retrieve saved firmware trace from a prior IOC failure. */
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00001768int
1769bfa_nw_ioc_debug_fwsave(struct bfa_ioc *ioc, void *trcdata, int *trclen)
1770{
1771 int tlen;
1772
1773 if (ioc->dbg_fwsave_len == 0)
1774 return BFA_STATUS_ENOFSAVE;
1775
1776 tlen = *trclen;
1777 if (tlen > ioc->dbg_fwsave_len)
1778 tlen = ioc->dbg_fwsave_len;
1779
1780 memcpy(trcdata, ioc->dbg_fwsave, tlen);
1781 *trclen = tlen;
1782 return BFA_STATUS_OK;
1783}
1784
Rasesh Mody1d32f762010-12-23 21:45:09 +00001785static void
1786bfa_ioc_fail_notify(struct bfa_ioc *ioc)
1787{
Rasesh Mody1d32f762010-12-23 21:45:09 +00001788 /**
1789 * Notify driver and common modules registered for notification.
1790 */
1791 ioc->cbfn->hbfail_cbfn(ioc->bfa);
Rasesh Modybd5a92e2011-07-22 08:07:42 +00001792 bfa_ioc_event_notify(ioc, BFA_IOC_E_FAILED);
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00001793 bfa_nw_ioc_debug_save_ftrc(ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001794}
1795
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001796/* IOCPF to IOC interface */
Rasesh Mody1d32f762010-12-23 21:45:09 +00001797static void
1798bfa_ioc_pf_enabled(struct bfa_ioc *ioc)
1799{
1800 bfa_fsm_send_event(ioc, IOC_E_ENABLED);
1801}
1802
1803static void
1804bfa_ioc_pf_disabled(struct bfa_ioc *ioc)
1805{
1806 bfa_fsm_send_event(ioc, IOC_E_DISABLED);
1807}
1808
1809static void
Rasesh Mody1d32f762010-12-23 21:45:09 +00001810bfa_ioc_pf_failed(struct bfa_ioc *ioc)
1811{
Rasesh Modyf374b362011-07-22 08:07:46 +00001812 bfa_fsm_send_event(ioc, IOC_E_PFFAILED);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001813}
1814
1815static void
Rasesh Mody078086f2011-08-08 16:21:39 +00001816bfa_ioc_pf_hwfailed(struct bfa_ioc *ioc)
1817{
1818 bfa_fsm_send_event(ioc, IOC_E_HWFAILED);
1819}
1820
1821static void
Rasesh Mody1d32f762010-12-23 21:45:09 +00001822bfa_ioc_pf_fwmismatch(struct bfa_ioc *ioc)
1823{
1824 /**
1825 * Provide enable completion callback and AEN notification.
1826 */
1827 ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
1828}
1829
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001830/* IOC public */
Rasesh Mody8a891422010-08-25 23:00:27 -07001831static enum bfa_status
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001832bfa_ioc_pll_init(struct bfa_ioc *ioc)
1833{
1834 /*
1835 * Hold semaphore so that nobody can access the chip during init.
1836 */
Rasesh Mody8a891422010-08-25 23:00:27 -07001837 bfa_nw_ioc_sem_get(ioc->ioc_regs.ioc_init_sem_reg);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001838
1839 bfa_ioc_pll_init_asic(ioc);
1840
1841 ioc->pllinit = true;
Jing Huange491c772012-04-04 05:42:08 +00001842
1843 /* Initialize LMEM */
1844 bfa_ioc_lmem_init(ioc);
1845
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001846 /*
1847 * release semaphore.
1848 */
Rasesh Mody8a891422010-08-25 23:00:27 -07001849 bfa_nw_ioc_sem_release(ioc->ioc_regs.ioc_init_sem_reg);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001850
1851 return BFA_STATUS_OK;
1852}
1853
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001854/* Interface used by diag module to do firmware boot with memory test
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001855 * as the entry vector.
1856 */
Rasesh Mody8a891422010-08-25 23:00:27 -07001857static void
Rasesh Mody078086f2011-08-08 16:21:39 +00001858bfa_ioc_boot(struct bfa_ioc *ioc, enum bfi_fwboot_type boot_type,
1859 u32 boot_env)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001860{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001861 bfa_ioc_stats(ioc, ioc_boots);
1862
1863 if (bfa_ioc_pll_init(ioc) != BFA_STATUS_OK)
1864 return;
1865
1866 /**
1867 * Initialize IOC state of all functions on a chip reset.
1868 */
Rasesh Mody078086f2011-08-08 16:21:39 +00001869 if (boot_type == BFI_FWBOOT_TYPE_MEMTEST) {
Rasesh Mody41ed9032013-12-17 17:07:32 -08001870 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_MEMTEST);
1871 bfa_ioc_set_alt_ioc_fwstate(ioc, BFI_IOC_MEMTEST);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001872 } else {
Rasesh Mody41ed9032013-12-17 17:07:32 -08001873 bfa_ioc_set_cur_ioc_fwstate(ioc, BFI_IOC_INITING);
1874 bfa_ioc_set_alt_ioc_fwstate(ioc, BFI_IOC_INITING);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001875 }
1876
1877 bfa_ioc_msgflush(ioc);
Rasesh Mody79ea6c82011-04-14 08:05:18 +00001878 bfa_ioc_download_fw(ioc, boot_type, boot_env);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001879 bfa_ioc_lpu_start(ioc);
1880}
1881
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001882/* Enable/disable IOC failure auto recovery. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001883void
Rasesh Mody8a891422010-08-25 23:00:27 -07001884bfa_nw_ioc_auto_recover(bool auto_recover)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001885{
Rasesh Mody8a891422010-08-25 23:00:27 -07001886 bfa_nw_auto_recover = auto_recover;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001887}
1888
Rasesh Mody078086f2011-08-08 16:21:39 +00001889static bool
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001890bfa_ioc_msgget(struct bfa_ioc *ioc, void *mbmsg)
1891{
1892 u32 *msgp = mbmsg;
1893 u32 r32;
1894 int i;
1895
Rasesh Mody078086f2011-08-08 16:21:39 +00001896 r32 = readl(ioc->ioc_regs.lpu_mbox_cmd);
1897 if ((r32 & 1) == 0)
1898 return false;
1899
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001900 /**
1901 * read the MBOX msg
1902 */
1903 for (i = 0; i < (sizeof(union bfi_ioc_i2h_msg_u) / sizeof(u32));
1904 i++) {
1905 r32 = readl(ioc->ioc_regs.lpu_mbox +
1906 i * sizeof(u32));
1907 msgp[i] = htonl(r32);
1908 }
1909
1910 /**
1911 * turn off mailbox interrupt by clearing mailbox status
1912 */
1913 writel(1, ioc->ioc_regs.lpu_mbox_cmd);
1914 readl(ioc->ioc_regs.lpu_mbox_cmd);
Rasesh Mody078086f2011-08-08 16:21:39 +00001915
1916 return true;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001917}
1918
Rasesh Mody8a891422010-08-25 23:00:27 -07001919static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001920bfa_ioc_isr(struct bfa_ioc *ioc, struct bfi_mbmsg *m)
1921{
1922 union bfi_ioc_i2h_msg_u *msg;
Rasesh Mody1d32f762010-12-23 21:45:09 +00001923 struct bfa_iocpf *iocpf = &ioc->iocpf;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001924
1925 msg = (union bfi_ioc_i2h_msg_u *) m;
1926
1927 bfa_ioc_stats(ioc, ioc_isrs);
1928
1929 switch (msg->mh.msg_id) {
1930 case BFI_IOC_I2H_HBEAT:
1931 break;
1932
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001933 case BFI_IOC_I2H_ENABLE_REPLY:
Rasesh Mody078086f2011-08-08 16:21:39 +00001934 bfa_ioc_enable_reply(ioc,
1935 (enum bfa_mode)msg->fw_event.port_mode,
1936 msg->fw_event.cap_bm);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001937 break;
1938
1939 case BFI_IOC_I2H_DISABLE_REPLY:
Rasesh Mody1d32f762010-12-23 21:45:09 +00001940 bfa_fsm_send_event(iocpf, IOCPF_E_FWRSP_DISABLE);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001941 break;
1942
1943 case BFI_IOC_I2H_GETATTR_REPLY:
1944 bfa_ioc_getattr_reply(ioc);
1945 break;
1946
1947 default:
1948 BUG_ON(1);
1949 }
1950}
1951
1952/**
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001953 * bfa_nw_ioc_attach - IOC attach time initialization and setup.
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001954 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001955 * @ioc: memory for IOC
1956 * @bfa: driver instance structure
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001957 */
1958void
Rasesh Mody8a891422010-08-25 23:00:27 -07001959bfa_nw_ioc_attach(struct bfa_ioc *ioc, void *bfa, struct bfa_ioc_cbfn *cbfn)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001960{
1961 ioc->bfa = bfa;
1962 ioc->cbfn = cbfn;
1963 ioc->fcmode = false;
1964 ioc->pllinit = false;
1965 ioc->dbg_fwsave_once = true;
Rasesh Mody1d32f762010-12-23 21:45:09 +00001966 ioc->iocpf.ioc = ioc;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001967
1968 bfa_ioc_mbox_attach(ioc);
Rasesh Modybd5a92e2011-07-22 08:07:42 +00001969 INIT_LIST_HEAD(&ioc->notify_q);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001970
Rasesh Mody1d32f762010-12-23 21:45:09 +00001971 bfa_fsm_set_state(ioc, bfa_ioc_sm_uninit);
1972 bfa_fsm_send_event(ioc, IOC_E_RESET);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001973}
1974
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001975/* Driver detach time IOC cleanup. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001976void
Rasesh Mody8a891422010-08-25 23:00:27 -07001977bfa_nw_ioc_detach(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001978{
1979 bfa_fsm_send_event(ioc, IOC_E_DETACH);
Rasesh Mody078086f2011-08-08 16:21:39 +00001980
1981 /* Done with detach, empty the notify_q. */
1982 INIT_LIST_HEAD(&ioc->notify_q);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001983}
1984
1985/**
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001986 * bfa_nw_ioc_pci_init - Setup IOC PCI properties.
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001987 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00001988 * @pcidev: PCI device information for this IOC
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001989 */
1990void
Rasesh Mody8a891422010-08-25 23:00:27 -07001991bfa_nw_ioc_pci_init(struct bfa_ioc *ioc, struct bfa_pcidev *pcidev,
Rasesh Mody078086f2011-08-08 16:21:39 +00001992 enum bfi_pcifn_class clscode)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001993{
Rasesh Mody078086f2011-08-08 16:21:39 +00001994 ioc->clscode = clscode;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001995 ioc->pcidev = *pcidev;
Rasesh Mody078086f2011-08-08 16:21:39 +00001996
1997 /**
1998 * Initialize IOC and device personality
1999 */
2000 ioc->port0_mode = ioc->port1_mode = BFI_PORT_MODE_FC;
2001 ioc->asic_mode = BFI_ASIC_MODE_FC;
2002
2003 switch (pcidev->device_id) {
2004 case PCI_DEVICE_ID_BROCADE_CT:
2005 ioc->asic_gen = BFI_ASIC_GEN_CT;
2006 ioc->port0_mode = ioc->port1_mode = BFI_PORT_MODE_ETH;
2007 ioc->asic_mode = BFI_ASIC_MODE_ETH;
2008 ioc->port_mode = ioc->port_mode_cfg = BFA_MODE_CNA;
2009 ioc->ad_cap_bm = BFA_CM_CNA;
2010 break;
2011
Rasesh Mody586b28162011-09-27 10:39:08 +00002012 case BFA_PCI_DEVICE_ID_CT2:
2013 ioc->asic_gen = BFI_ASIC_GEN_CT2;
2014 if (clscode == BFI_PCIFN_CLASS_FC &&
2015 pcidev->ssid == BFA_PCI_CT2_SSID_FC) {
2016 ioc->asic_mode = BFI_ASIC_MODE_FC16;
2017 ioc->fcmode = true;
2018 ioc->port_mode = ioc->port_mode_cfg = BFA_MODE_HBA;
2019 ioc->ad_cap_bm = BFA_CM_HBA;
2020 } else {
2021 ioc->port0_mode = ioc->port1_mode = BFI_PORT_MODE_ETH;
2022 ioc->asic_mode = BFI_ASIC_MODE_ETH;
2023 if (pcidev->ssid == BFA_PCI_CT2_SSID_FCoE) {
2024 ioc->port_mode =
2025 ioc->port_mode_cfg = BFA_MODE_CNA;
2026 ioc->ad_cap_bm = BFA_CM_CNA;
2027 } else {
2028 ioc->port_mode =
2029 ioc->port_mode_cfg = BFA_MODE_NIC;
2030 ioc->ad_cap_bm = BFA_CM_NIC;
2031 }
2032 }
2033 break;
2034
Rasesh Mody078086f2011-08-08 16:21:39 +00002035 default:
2036 BUG_ON(1);
2037 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002038
Rasesh Modybe3a84d2011-09-27 10:39:05 +00002039 /**
2040 * Set asic specific interfaces.
2041 */
2042 if (ioc->asic_gen == BFI_ASIC_GEN_CT)
2043 bfa_nw_ioc_set_ct_hwif(ioc);
Rasesh Mody70f1438122011-10-04 23:04:01 -04002044 else {
2045 WARN_ON(ioc->asic_gen != BFI_ASIC_GEN_CT2);
Rasesh Modybe3a84d2011-09-27 10:39:05 +00002046 bfa_nw_ioc_set_ct2_hwif(ioc);
Rasesh Mody70f1438122011-10-04 23:04:01 -04002047 bfa_nw_ioc_ct2_poweron(ioc);
2048 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002049
2050 bfa_ioc_map_port(ioc);
2051 bfa_ioc_reg_init(ioc);
2052}
2053
2054/**
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002055 * bfa_nw_ioc_mem_claim - Initialize IOC dma memory
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002056 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002057 * @dm_kva: kernel virtual address of IOC dma memory
2058 * @dm_pa: physical address of IOC dma memory
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002059 */
2060void
Rasesh Mody8a891422010-08-25 23:00:27 -07002061bfa_nw_ioc_mem_claim(struct bfa_ioc *ioc, u8 *dm_kva, u64 dm_pa)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002062{
2063 /**
2064 * dma memory for firmware attribute
2065 */
2066 ioc->attr_dma.kva = dm_kva;
2067 ioc->attr_dma.pa = dm_pa;
2068 ioc->attr = (struct bfi_ioc_attr *) dm_kva;
2069}
2070
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002071/* Return size of dma memory required. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002072u32
Rasesh Mody8a891422010-08-25 23:00:27 -07002073bfa_nw_ioc_meminfo(void)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002074{
2075 return roundup(sizeof(struct bfi_ioc_attr), BFA_DMA_ALIGN_SZ);
2076}
2077
2078void
Rasesh Mody8a891422010-08-25 23:00:27 -07002079bfa_nw_ioc_enable(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002080{
2081 bfa_ioc_stats(ioc, ioc_enables);
2082 ioc->dbg_fwsave_once = true;
2083
2084 bfa_fsm_send_event(ioc, IOC_E_ENABLE);
2085}
2086
2087void
Rasesh Mody8a891422010-08-25 23:00:27 -07002088bfa_nw_ioc_disable(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002089{
2090 bfa_ioc_stats(ioc, ioc_disables);
2091 bfa_fsm_send_event(ioc, IOC_E_DISABLE);
2092}
2093
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002094/* Initialize memory for saving firmware trace. */
Krishna Gudipati7afc5db2011-12-22 13:30:19 +00002095void
2096bfa_nw_ioc_debug_memclaim(struct bfa_ioc *ioc, void *dbg_fwsave)
2097{
2098 ioc->dbg_fwsave = dbg_fwsave;
2099 ioc->dbg_fwsave_len = ioc->iocpf.auto_recover ? BNA_DBG_FWTRC_LEN : 0;
2100}
2101
Rasesh Mody8a891422010-08-25 23:00:27 -07002102static u32
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002103bfa_ioc_smem_pgnum(struct bfa_ioc *ioc, u32 fmaddr)
2104{
2105 return PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, fmaddr);
2106}
2107
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002108/* Register mailbox message handler function, to be called by common modules */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002109void
Rasesh Mody8a891422010-08-25 23:00:27 -07002110bfa_nw_ioc_mbox_regisr(struct bfa_ioc *ioc, enum bfi_mclass mc,
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002111 bfa_ioc_mbox_mcfunc_t cbfn, void *cbarg)
2112{
2113 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2114
2115 mod->mbhdlr[mc].cbfn = cbfn;
2116 mod->mbhdlr[mc].cbarg = cbarg;
2117}
2118
2119/**
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002120 * bfa_nw_ioc_mbox_queue - Queue a mailbox command request to firmware.
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002121 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002122 * @ioc: IOC instance
2123 * @cmd: Mailbox command
2124 *
2125 * Waits if mailbox is busy. Responsibility of caller to serialize
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002126 */
Rasesh Modyaf027a32011-08-08 16:21:35 +00002127bool
2128bfa_nw_ioc_mbox_queue(struct bfa_ioc *ioc, struct bfa_mbox_cmd *cmd,
2129 bfa_mbox_cmd_cbfn_t cbfn, void *cbarg)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002130{
2131 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2132 u32 stat;
2133
Rasesh Modyaf027a32011-08-08 16:21:35 +00002134 cmd->cbfn = cbfn;
2135 cmd->cbarg = cbarg;
2136
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002137 /**
2138 * If a previous command is pending, queue new command
2139 */
2140 if (!list_empty(&mod->cmd_q)) {
2141 list_add_tail(&cmd->qe, &mod->cmd_q);
Rasesh Modyaf027a32011-08-08 16:21:35 +00002142 return true;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002143 }
2144
2145 /**
2146 * If mailbox is busy, queue command for poll timer
2147 */
2148 stat = readl(ioc->ioc_regs.hfn_mbox_cmd);
2149 if (stat) {
2150 list_add_tail(&cmd->qe, &mod->cmd_q);
Rasesh Modyaf027a32011-08-08 16:21:35 +00002151 return true;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002152 }
2153
2154 /**
2155 * mailbox is free -- queue command to firmware
2156 */
2157 bfa_ioc_mbox_send(ioc, cmd->msg, sizeof(cmd->msg));
Rasesh Modybd5a92e2011-07-22 08:07:42 +00002158
Rasesh Modyaf027a32011-08-08 16:21:35 +00002159 return false;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002160}
2161
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002162/* Handle mailbox interrupts */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002163void
Rasesh Mody8a891422010-08-25 23:00:27 -07002164bfa_nw_ioc_mbox_isr(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002165{
2166 struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
2167 struct bfi_mbmsg m;
2168 int mc;
2169
Rasesh Mody078086f2011-08-08 16:21:39 +00002170 if (bfa_ioc_msgget(ioc, &m)) {
2171 /**
2172 * Treat IOC message class as special.
2173 */
2174 mc = m.mh.msg_class;
2175 if (mc == BFI_MC_IOC) {
2176 bfa_ioc_isr(ioc, &m);
2177 return;
2178 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002179
Rasesh Mody078086f2011-08-08 16:21:39 +00002180 if ((mc >= BFI_MC_MAX) || (mod->mbhdlr[mc].cbfn == NULL))
2181 return;
2182
2183 mod->mbhdlr[mc].cbfn(mod->mbhdlr[mc].cbarg, &m);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002184 }
2185
Rasesh Mody078086f2011-08-08 16:21:39 +00002186 bfa_ioc_lpu_read_stat(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002187
Rasesh Mody078086f2011-08-08 16:21:39 +00002188 /**
2189 * Try to send pending mailbox commands
2190 */
2191 bfa_ioc_mbox_poll(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002192}
2193
2194void
Rasesh Mody8a891422010-08-25 23:00:27 -07002195bfa_nw_ioc_error_isr(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002196{
Rasesh Mody9b08a4f2011-07-22 08:07:48 +00002197 bfa_ioc_stats(ioc, ioc_hbfails);
2198 bfa_ioc_stats_hb_count(ioc, ioc->hb_count);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002199 bfa_fsm_send_event(ioc, IOC_E_HWERROR);
2200}
2201
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002202/* return true if IOC is disabled */
Rasesh Modybd5a92e2011-07-22 08:07:42 +00002203bool
2204bfa_nw_ioc_is_disabled(struct bfa_ioc *ioc)
2205{
2206 return bfa_fsm_cmp_state(ioc, bfa_ioc_sm_disabling) ||
2207 bfa_fsm_cmp_state(ioc, bfa_ioc_sm_disabled);
2208}
2209
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002210/* return true if IOC is operational */
Krishna Gudipati72a97302011-12-22 13:29:45 +00002211bool
2212bfa_nw_ioc_is_operational(struct bfa_ioc *ioc)
2213{
2214 return bfa_fsm_cmp_state(ioc, bfa_ioc_sm_op);
2215}
2216
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002217/* Add to IOC heartbeat failure notification queue. To be used by common
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002218 * modules such as cee, port, diag.
2219 */
2220void
Rasesh Modybd5a92e2011-07-22 08:07:42 +00002221bfa_nw_ioc_notify_register(struct bfa_ioc *ioc,
2222 struct bfa_ioc_notify *notify)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002223{
Rasesh Modybd5a92e2011-07-22 08:07:42 +00002224 list_add_tail(&notify->qe, &ioc->notify_q);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002225}
2226
2227#define BFA_MFG_NAME "Brocade"
Rasesh Mody8a891422010-08-25 23:00:27 -07002228static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002229bfa_ioc_get_adapter_attr(struct bfa_ioc *ioc,
2230 struct bfa_adapter_attr *ad_attr)
2231{
2232 struct bfi_ioc_attr *ioc_attr;
2233
2234 ioc_attr = ioc->attr;
2235
2236 bfa_ioc_get_adapter_serial_num(ioc, ad_attr->serial_num);
2237 bfa_ioc_get_adapter_fw_ver(ioc, ad_attr->fw_ver);
2238 bfa_ioc_get_adapter_optrom_ver(ioc, ad_attr->optrom_ver);
2239 bfa_ioc_get_adapter_manufacturer(ioc, ad_attr->manufacturer);
2240 memcpy(&ad_attr->vpd, &ioc_attr->vpd,
2241 sizeof(struct bfa_mfg_vpd));
2242
2243 ad_attr->nports = bfa_ioc_get_nports(ioc);
2244 ad_attr->max_speed = bfa_ioc_speed_sup(ioc);
2245
2246 bfa_ioc_get_adapter_model(ioc, ad_attr->model);
2247 /* For now, model descr uses same model string */
2248 bfa_ioc_get_adapter_model(ioc, ad_attr->model_descr);
2249
2250 ad_attr->card_type = ioc_attr->card_type;
2251 ad_attr->is_mezz = bfa_mfg_is_mezz(ioc_attr->card_type);
2252
2253 if (BFI_ADAPTER_IS_SPECIAL(ioc_attr->adapter_prop))
2254 ad_attr->prototype = 1;
2255 else
2256 ad_attr->prototype = 0;
2257
2258 ad_attr->pwwn = bfa_ioc_get_pwwn(ioc);
Rasesh Mody8a891422010-08-25 23:00:27 -07002259 ad_attr->mac = bfa_nw_ioc_get_mac(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002260
2261 ad_attr->pcie_gen = ioc_attr->pcie_gen;
2262 ad_attr->pcie_lanes = ioc_attr->pcie_lanes;
2263 ad_attr->pcie_lanes_orig = ioc_attr->pcie_lanes_orig;
2264 ad_attr->asic_rev = ioc_attr->asic_rev;
2265
2266 bfa_ioc_get_pci_chip_rev(ioc, ad_attr->hw_ver);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002267}
2268
Rasesh Mody8a891422010-08-25 23:00:27 -07002269static enum bfa_ioc_type
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002270bfa_ioc_get_type(struct bfa_ioc *ioc)
2271{
Rasesh Mody078086f2011-08-08 16:21:39 +00002272 if (ioc->clscode == BFI_PCIFN_CLASS_ETH)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002273 return BFA_IOC_TYPE_LL;
Rasesh Mody078086f2011-08-08 16:21:39 +00002274
2275 BUG_ON(!(ioc->clscode == BFI_PCIFN_CLASS_FC));
2276
2277 return (ioc->attr->port_mode == BFI_PORT_MODE_FC)
2278 ? BFA_IOC_TYPE_FC : BFA_IOC_TYPE_FCoE;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002279}
2280
Rasesh Mody8a891422010-08-25 23:00:27 -07002281static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002282bfa_ioc_get_adapter_serial_num(struct bfa_ioc *ioc, char *serial_num)
2283{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002284 memcpy(serial_num,
2285 (void *)ioc->attr->brcd_serialnum,
2286 BFA_ADAPTER_SERIAL_NUM_LEN);
2287}
2288
Rasesh Mody8a891422010-08-25 23:00:27 -07002289static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002290bfa_ioc_get_adapter_fw_ver(struct bfa_ioc *ioc, char *fw_ver)
2291{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002292 memcpy(fw_ver, ioc->attr->fw_version, BFA_VERSION_LEN);
2293}
2294
Rasesh Mody8a891422010-08-25 23:00:27 -07002295static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002296bfa_ioc_get_pci_chip_rev(struct bfa_ioc *ioc, char *chip_rev)
2297{
2298 BUG_ON(!(chip_rev));
2299
2300 memset(chip_rev, 0, BFA_IOC_CHIP_REV_LEN);
2301
2302 chip_rev[0] = 'R';
2303 chip_rev[1] = 'e';
2304 chip_rev[2] = 'v';
2305 chip_rev[3] = '-';
2306 chip_rev[4] = ioc->attr->asic_rev;
2307 chip_rev[5] = '\0';
2308}
2309
Rasesh Mody8a891422010-08-25 23:00:27 -07002310static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002311bfa_ioc_get_adapter_optrom_ver(struct bfa_ioc *ioc, char *optrom_ver)
2312{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002313 memcpy(optrom_ver, ioc->attr->optrom_version,
2314 BFA_VERSION_LEN);
2315}
2316
Rasesh Mody8a891422010-08-25 23:00:27 -07002317static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002318bfa_ioc_get_adapter_manufacturer(struct bfa_ioc *ioc, char *manufacturer)
2319{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002320 memcpy(manufacturer, BFA_MFG_NAME, BFA_ADAPTER_MFG_NAME_LEN);
2321}
2322
Rasesh Mody8a891422010-08-25 23:00:27 -07002323static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002324bfa_ioc_get_adapter_model(struct bfa_ioc *ioc, char *model)
2325{
2326 struct bfi_ioc_attr *ioc_attr;
2327
2328 BUG_ON(!(model));
2329 memset(model, 0, BFA_ADAPTER_MODEL_NAME_LEN);
2330
2331 ioc_attr = ioc->attr;
2332
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002333 snprintf(model, BFA_ADAPTER_MODEL_NAME_LEN, "%s-%u",
2334 BFA_MFG_NAME, ioc_attr->card_type);
2335}
2336
Rasesh Mody8a891422010-08-25 23:00:27 -07002337static enum bfa_ioc_state
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002338bfa_ioc_get_state(struct bfa_ioc *ioc)
2339{
Rasesh Mody1d32f762010-12-23 21:45:09 +00002340 enum bfa_iocpf_state iocpf_st;
2341 enum bfa_ioc_state ioc_st = bfa_sm_to_state(ioc_sm_table, ioc->fsm);
2342
2343 if (ioc_st == BFA_IOC_ENABLING ||
2344 ioc_st == BFA_IOC_FAIL || ioc_st == BFA_IOC_INITFAIL) {
2345
2346 iocpf_st = bfa_sm_to_state(iocpf_sm_table, ioc->iocpf.fsm);
2347
2348 switch (iocpf_st) {
2349 case BFA_IOCPF_SEMWAIT:
2350 ioc_st = BFA_IOC_SEMWAIT;
2351 break;
2352
2353 case BFA_IOCPF_HWINIT:
2354 ioc_st = BFA_IOC_HWINIT;
2355 break;
2356
2357 case BFA_IOCPF_FWMISMATCH:
2358 ioc_st = BFA_IOC_FWMISMATCH;
2359 break;
2360
2361 case BFA_IOCPF_FAIL:
2362 ioc_st = BFA_IOC_FAIL;
2363 break;
2364
2365 case BFA_IOCPF_INITFAIL:
2366 ioc_st = BFA_IOC_INITFAIL;
2367 break;
2368
2369 default:
2370 break;
2371 }
2372 }
2373 return ioc_st;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002374}
2375
2376void
Rasesh Mody8a891422010-08-25 23:00:27 -07002377bfa_nw_ioc_get_attr(struct bfa_ioc *ioc, struct bfa_ioc_attr *ioc_attr)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002378{
2379 memset((void *)ioc_attr, 0, sizeof(struct bfa_ioc_attr));
2380
2381 ioc_attr->state = bfa_ioc_get_state(ioc);
Rasesh Mody43c07ad2013-05-20 10:08:03 +00002382 ioc_attr->port_id = bfa_ioc_portid(ioc);
Rasesh Mody078086f2011-08-08 16:21:39 +00002383 ioc_attr->port_mode = ioc->port_mode;
2384
2385 ioc_attr->port_mode_cfg = ioc->port_mode_cfg;
2386 ioc_attr->cap_bm = ioc->ad_cap_bm;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002387
2388 ioc_attr->ioc_type = bfa_ioc_get_type(ioc);
2389
2390 bfa_ioc_get_adapter_attr(ioc, &ioc_attr->adapter_attr);
2391
Rasesh Mody43c07ad2013-05-20 10:08:03 +00002392 ioc_attr->pci_attr.device_id = bfa_ioc_devid(ioc);
2393 ioc_attr->pci_attr.pcifn = bfa_ioc_pcifn(ioc);
2394 ioc_attr->def_fn = bfa_ioc_is_default(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002395 bfa_ioc_get_pci_chip_rev(ioc, ioc_attr->pci_attr.chip_rev);
2396}
2397
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002398/* WWN public */
Rasesh Mody8a891422010-08-25 23:00:27 -07002399static u64
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002400bfa_ioc_get_pwwn(struct bfa_ioc *ioc)
2401{
2402 return ioc->attr->pwwn;
2403}
2404
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002405mac_t
Rasesh Mody8a891422010-08-25 23:00:27 -07002406bfa_nw_ioc_get_mac(struct bfa_ioc *ioc)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002407{
Rasesh Mody2c7d3822010-12-23 21:45:06 +00002408 return ioc->attr->mac;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002409}
2410
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002411/* Firmware failure detected. Start recovery actions. */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002412static void
2413bfa_ioc_recover(struct bfa_ioc *ioc)
2414{
Rasesh Mody1e581482011-04-04 08:29:59 +00002415 pr_crit("Heart Beat of IOC has failed\n");
2416 bfa_ioc_stats(ioc, ioc_hbfails);
Rasesh Mody9b08a4f2011-07-22 08:07:48 +00002417 bfa_ioc_stats_hb_count(ioc, ioc->hb_count);
Rasesh Mody1e581482011-04-04 08:29:59 +00002418 bfa_fsm_send_event(ioc, IOC_E_HBFAIL);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002419}
2420
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002421/* BFA IOC PF private functions */
Rasesh Mody1d32f762010-12-23 21:45:09 +00002422
2423static void
2424bfa_iocpf_enable(struct bfa_ioc *ioc)
2425{
2426 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_ENABLE);
2427}
2428
2429static void
2430bfa_iocpf_disable(struct bfa_ioc *ioc)
2431{
2432 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_DISABLE);
2433}
2434
2435static void
2436bfa_iocpf_fail(struct bfa_ioc *ioc)
2437{
2438 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_FAIL);
2439}
2440
2441static void
2442bfa_iocpf_initfail(struct bfa_ioc *ioc)
2443{
2444 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_INITFAIL);
2445}
2446
2447static void
2448bfa_iocpf_getattrfail(struct bfa_ioc *ioc)
2449{
2450 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_GETATTRFAIL);
2451}
2452
2453static void
2454bfa_iocpf_stop(struct bfa_ioc *ioc)
2455{
2456 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_STOP);
2457}
2458
2459void
2460bfa_nw_iocpf_timeout(void *ioc_arg)
2461{
2462 struct bfa_ioc *ioc = (struct bfa_ioc *) ioc_arg;
Rasesh Mody078086f2011-08-08 16:21:39 +00002463 enum bfa_iocpf_state iocpf_st;
Rasesh Mody1d32f762010-12-23 21:45:09 +00002464
Rasesh Mody078086f2011-08-08 16:21:39 +00002465 iocpf_st = bfa_sm_to_state(iocpf_sm_table, ioc->iocpf.fsm);
2466
2467 if (iocpf_st == BFA_IOCPF_HWINIT)
2468 bfa_ioc_poll_fwinit(ioc);
2469 else
2470 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_TIMEOUT);
Rasesh Mody1d32f762010-12-23 21:45:09 +00002471}
2472
2473void
2474bfa_nw_iocpf_sem_timeout(void *ioc_arg)
2475{
2476 struct bfa_ioc *ioc = (struct bfa_ioc *) ioc_arg;
2477
2478 bfa_ioc_hw_sem_get(ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002479}
Rasesh Mody078086f2011-08-08 16:21:39 +00002480
2481static void
2482bfa_ioc_poll_fwinit(struct bfa_ioc *ioc)
2483{
Rasesh Mody41ed9032013-12-17 17:07:32 -08002484 u32 fwstate = bfa_ioc_get_cur_ioc_fwstate(ioc);
Rasesh Mody078086f2011-08-08 16:21:39 +00002485
2486 if (fwstate == BFI_IOC_DISABLED) {
2487 bfa_fsm_send_event(&ioc->iocpf, IOCPF_E_FWREADY);
2488 return;
2489 }
2490
2491 if (ioc->iocpf.poll_time >= BFA_IOC_TOV) {
2492 bfa_nw_iocpf_timeout(ioc);
2493 } else {
2494 ioc->iocpf.poll_time += BFA_IOC_POLL_TOV;
2495 mod_timer(&ioc->iocpf_timer, jiffies +
2496 msecs_to_jiffies(BFA_IOC_POLL_TOV));
2497 }
2498}
Krishna Gudipati72a97302011-12-22 13:29:45 +00002499
2500/*
2501 * Flash module specific
2502 */
2503
2504/*
2505 * FLASH DMA buffer should be big enough to hold both MFG block and
2506 * asic block(64k) at the same time and also should be 2k aligned to
2507 * avoid write segement to cross sector boundary.
2508 */
2509#define BFA_FLASH_SEG_SZ 2048
2510#define BFA_FLASH_DMA_BUF_SZ \
2511 roundup(0x010000 + sizeof(struct bfa_mfg_block), BFA_FLASH_SEG_SZ)
2512
2513static void
2514bfa_flash_cb(struct bfa_flash *flash)
2515{
2516 flash->op_busy = 0;
2517 if (flash->cbfn)
2518 flash->cbfn(flash->cbarg, flash->status);
2519}
2520
2521static void
2522bfa_flash_notify(void *cbarg, enum bfa_ioc_event event)
2523{
2524 struct bfa_flash *flash = cbarg;
2525
2526 switch (event) {
2527 case BFA_IOC_E_DISABLED:
2528 case BFA_IOC_E_FAILED:
2529 if (flash->op_busy) {
2530 flash->status = BFA_STATUS_IOC_FAILURE;
2531 flash->cbfn(flash->cbarg, flash->status);
2532 flash->op_busy = 0;
2533 }
2534 break;
2535 default:
2536 break;
2537 }
2538}
2539
2540/*
2541 * Send flash write request.
Krishna Gudipati72a97302011-12-22 13:29:45 +00002542 */
2543static void
2544bfa_flash_write_send(struct bfa_flash *flash)
2545{
2546 struct bfi_flash_write_req *msg =
2547 (struct bfi_flash_write_req *) flash->mb.msg;
2548 u32 len;
2549
2550 msg->type = be32_to_cpu(flash->type);
2551 msg->instance = flash->instance;
2552 msg->offset = be32_to_cpu(flash->addr_off + flash->offset);
2553 len = (flash->residue < BFA_FLASH_DMA_BUF_SZ) ?
2554 flash->residue : BFA_FLASH_DMA_BUF_SZ;
2555 msg->length = be32_to_cpu(len);
2556
2557 /* indicate if it's the last msg of the whole write operation */
2558 msg->last = (len == flash->residue) ? 1 : 0;
2559
2560 bfi_h2i_set(msg->mh, BFI_MC_FLASH, BFI_FLASH_H2I_WRITE_REQ,
2561 bfa_ioc_portid(flash->ioc));
2562 bfa_alen_set(&msg->alen, len, flash->dbuf_pa);
2563 memcpy(flash->dbuf_kva, flash->ubuf + flash->offset, len);
2564 bfa_nw_ioc_mbox_queue(flash->ioc, &flash->mb, NULL, NULL);
2565
2566 flash->residue -= len;
2567 flash->offset += len;
2568}
2569
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002570/**
2571 * bfa_flash_read_send - Send flash read request.
Krishna Gudipati72a97302011-12-22 13:29:45 +00002572 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002573 * @cbarg: callback argument
Krishna Gudipati72a97302011-12-22 13:29:45 +00002574 */
2575static void
2576bfa_flash_read_send(void *cbarg)
2577{
2578 struct bfa_flash *flash = cbarg;
2579 struct bfi_flash_read_req *msg =
2580 (struct bfi_flash_read_req *) flash->mb.msg;
2581 u32 len;
2582
2583 msg->type = be32_to_cpu(flash->type);
2584 msg->instance = flash->instance;
2585 msg->offset = be32_to_cpu(flash->addr_off + flash->offset);
2586 len = (flash->residue < BFA_FLASH_DMA_BUF_SZ) ?
2587 flash->residue : BFA_FLASH_DMA_BUF_SZ;
2588 msg->length = be32_to_cpu(len);
2589 bfi_h2i_set(msg->mh, BFI_MC_FLASH, BFI_FLASH_H2I_READ_REQ,
2590 bfa_ioc_portid(flash->ioc));
2591 bfa_alen_set(&msg->alen, len, flash->dbuf_pa);
2592 bfa_nw_ioc_mbox_queue(flash->ioc, &flash->mb, NULL, NULL);
2593}
2594
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002595/**
2596 * bfa_flash_intr - Process flash response messages upon receiving interrupts.
Krishna Gudipati72a97302011-12-22 13:29:45 +00002597 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002598 * @flasharg: flash structure
2599 * @msg: message structure
Krishna Gudipati72a97302011-12-22 13:29:45 +00002600 */
2601static void
2602bfa_flash_intr(void *flasharg, struct bfi_mbmsg *msg)
2603{
2604 struct bfa_flash *flash = flasharg;
2605 u32 status;
2606
2607 union {
2608 struct bfi_flash_query_rsp *query;
2609 struct bfi_flash_write_rsp *write;
2610 struct bfi_flash_read_rsp *read;
2611 struct bfi_mbmsg *msg;
2612 } m;
2613
2614 m.msg = msg;
2615
2616 /* receiving response after ioc failure */
2617 if (!flash->op_busy && msg->mh.msg_id != BFI_FLASH_I2H_EVENT)
2618 return;
2619
2620 switch (msg->mh.msg_id) {
2621 case BFI_FLASH_I2H_QUERY_RSP:
2622 status = be32_to_cpu(m.query->status);
2623 if (status == BFA_STATUS_OK) {
2624 u32 i;
2625 struct bfa_flash_attr *attr, *f;
2626
2627 attr = (struct bfa_flash_attr *) flash->ubuf;
2628 f = (struct bfa_flash_attr *) flash->dbuf_kva;
2629 attr->status = be32_to_cpu(f->status);
2630 attr->npart = be32_to_cpu(f->npart);
2631 for (i = 0; i < attr->npart; i++) {
2632 attr->part[i].part_type =
2633 be32_to_cpu(f->part[i].part_type);
2634 attr->part[i].part_instance =
2635 be32_to_cpu(f->part[i].part_instance);
2636 attr->part[i].part_off =
2637 be32_to_cpu(f->part[i].part_off);
2638 attr->part[i].part_size =
2639 be32_to_cpu(f->part[i].part_size);
2640 attr->part[i].part_len =
2641 be32_to_cpu(f->part[i].part_len);
2642 attr->part[i].part_status =
2643 be32_to_cpu(f->part[i].part_status);
2644 }
2645 }
2646 flash->status = status;
2647 bfa_flash_cb(flash);
2648 break;
2649 case BFI_FLASH_I2H_WRITE_RSP:
2650 status = be32_to_cpu(m.write->status);
2651 if (status != BFA_STATUS_OK || flash->residue == 0) {
2652 flash->status = status;
2653 bfa_flash_cb(flash);
2654 } else
2655 bfa_flash_write_send(flash);
2656 break;
2657 case BFI_FLASH_I2H_READ_RSP:
2658 status = be32_to_cpu(m.read->status);
2659 if (status != BFA_STATUS_OK) {
2660 flash->status = status;
2661 bfa_flash_cb(flash);
2662 } else {
2663 u32 len = be32_to_cpu(m.read->length);
2664 memcpy(flash->ubuf + flash->offset,
2665 flash->dbuf_kva, len);
2666 flash->residue -= len;
2667 flash->offset += len;
2668 if (flash->residue == 0) {
2669 flash->status = status;
2670 bfa_flash_cb(flash);
2671 } else
2672 bfa_flash_read_send(flash);
2673 }
2674 break;
2675 case BFI_FLASH_I2H_BOOT_VER_RSP:
2676 case BFI_FLASH_I2H_EVENT:
2677 break;
2678 default:
2679 WARN_ON(1);
2680 }
2681}
2682
2683/*
2684 * Flash memory info API.
2685 */
2686u32
2687bfa_nw_flash_meminfo(void)
2688{
2689 return roundup(BFA_FLASH_DMA_BUF_SZ, BFA_DMA_ALIGN_SZ);
2690}
2691
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002692/**
2693 * bfa_nw_flash_attach - Flash attach API.
Krishna Gudipati72a97302011-12-22 13:29:45 +00002694 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002695 * @flash: flash structure
2696 * @ioc: ioc structure
2697 * @dev: device structure
Krishna Gudipati72a97302011-12-22 13:29:45 +00002698 */
2699void
2700bfa_nw_flash_attach(struct bfa_flash *flash, struct bfa_ioc *ioc, void *dev)
2701{
2702 flash->ioc = ioc;
2703 flash->cbfn = NULL;
2704 flash->cbarg = NULL;
2705 flash->op_busy = 0;
2706
2707 bfa_nw_ioc_mbox_regisr(flash->ioc, BFI_MC_FLASH, bfa_flash_intr, flash);
2708 bfa_q_qe_init(&flash->ioc_notify);
2709 bfa_ioc_notify_init(&flash->ioc_notify, bfa_flash_notify, flash);
2710 list_add_tail(&flash->ioc_notify.qe, &flash->ioc->notify_q);
2711}
2712
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002713/**
2714 * bfa_nw_flash_memclaim - Claim memory for flash
Krishna Gudipati72a97302011-12-22 13:29:45 +00002715 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002716 * @flash: flash structure
2717 * @dm_kva: pointer to virtual memory address
2718 * @dm_pa: physical memory address
Krishna Gudipati72a97302011-12-22 13:29:45 +00002719 */
2720void
2721bfa_nw_flash_memclaim(struct bfa_flash *flash, u8 *dm_kva, u64 dm_pa)
2722{
2723 flash->dbuf_kva = dm_kva;
2724 flash->dbuf_pa = dm_pa;
2725 memset(flash->dbuf_kva, 0, BFA_FLASH_DMA_BUF_SZ);
2726 dm_kva += roundup(BFA_FLASH_DMA_BUF_SZ, BFA_DMA_ALIGN_SZ);
2727 dm_pa += roundup(BFA_FLASH_DMA_BUF_SZ, BFA_DMA_ALIGN_SZ);
2728}
2729
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002730/**
2731 * bfa_nw_flash_get_attr - Get flash attribute.
Krishna Gudipati72a97302011-12-22 13:29:45 +00002732 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002733 * @flash: flash structure
2734 * @attr: flash attribute structure
2735 * @cbfn: callback function
2736 * @cbarg: callback argument
Krishna Gudipati72a97302011-12-22 13:29:45 +00002737 *
2738 * Return status.
2739 */
2740enum bfa_status
2741bfa_nw_flash_get_attr(struct bfa_flash *flash, struct bfa_flash_attr *attr,
2742 bfa_cb_flash cbfn, void *cbarg)
2743{
2744 struct bfi_flash_query_req *msg =
2745 (struct bfi_flash_query_req *) flash->mb.msg;
2746
2747 if (!bfa_nw_ioc_is_operational(flash->ioc))
2748 return BFA_STATUS_IOC_NON_OP;
2749
2750 if (flash->op_busy)
2751 return BFA_STATUS_DEVBUSY;
2752
2753 flash->op_busy = 1;
2754 flash->cbfn = cbfn;
2755 flash->cbarg = cbarg;
2756 flash->ubuf = (u8 *) attr;
2757
2758 bfi_h2i_set(msg->mh, BFI_MC_FLASH, BFI_FLASH_H2I_QUERY_REQ,
2759 bfa_ioc_portid(flash->ioc));
2760 bfa_alen_set(&msg->alen, sizeof(struct bfa_flash_attr), flash->dbuf_pa);
2761 bfa_nw_ioc_mbox_queue(flash->ioc, &flash->mb, NULL, NULL);
2762
2763 return BFA_STATUS_OK;
2764}
2765
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002766/**
2767 * bfa_nw_flash_update_part - Update flash partition.
Krishna Gudipati72a97302011-12-22 13:29:45 +00002768 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002769 * @flash: flash structure
2770 * @type: flash partition type
2771 * @instance: flash partition instance
2772 * @buf: update data buffer
2773 * @len: data buffer length
2774 * @offset: offset relative to the partition starting address
2775 * @cbfn: callback function
2776 * @cbarg: callback argument
Krishna Gudipati72a97302011-12-22 13:29:45 +00002777 *
2778 * Return status.
2779 */
2780enum bfa_status
2781bfa_nw_flash_update_part(struct bfa_flash *flash, u32 type, u8 instance,
2782 void *buf, u32 len, u32 offset,
2783 bfa_cb_flash cbfn, void *cbarg)
2784{
2785 if (!bfa_nw_ioc_is_operational(flash->ioc))
2786 return BFA_STATUS_IOC_NON_OP;
2787
2788 /*
2789 * 'len' must be in word (4-byte) boundary
2790 */
2791 if (!len || (len & 0x03))
2792 return BFA_STATUS_FLASH_BAD_LEN;
2793
2794 if (type == BFA_FLASH_PART_MFG)
2795 return BFA_STATUS_EINVAL;
2796
2797 if (flash->op_busy)
2798 return BFA_STATUS_DEVBUSY;
2799
2800 flash->op_busy = 1;
2801 flash->cbfn = cbfn;
2802 flash->cbarg = cbarg;
2803 flash->type = type;
2804 flash->instance = instance;
2805 flash->residue = len;
2806 flash->offset = 0;
2807 flash->addr_off = offset;
2808 flash->ubuf = buf;
2809
2810 bfa_flash_write_send(flash);
2811
2812 return BFA_STATUS_OK;
2813}
2814
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002815/**
2816 * bfa_nw_flash_read_part - Read flash partition.
Krishna Gudipati72a97302011-12-22 13:29:45 +00002817 *
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002818 * @flash: flash structure
2819 * @type: flash partition type
2820 * @instance: flash partition instance
2821 * @buf: read data buffer
2822 * @len: data buffer length
2823 * @offset: offset relative to the partition starting address
2824 * @cbfn: callback function
2825 * @cbarg: callback argument
Krishna Gudipati72a97302011-12-22 13:29:45 +00002826 *
2827 * Return status.
2828 */
2829enum bfa_status
2830bfa_nw_flash_read_part(struct bfa_flash *flash, u32 type, u8 instance,
2831 void *buf, u32 len, u32 offset,
2832 bfa_cb_flash cbfn, void *cbarg)
2833{
2834 if (!bfa_nw_ioc_is_operational(flash->ioc))
2835 return BFA_STATUS_IOC_NON_OP;
2836
2837 /*
2838 * 'len' must be in word (4-byte) boundary
2839 */
2840 if (!len || (len & 0x03))
2841 return BFA_STATUS_FLASH_BAD_LEN;
2842
2843 if (flash->op_busy)
2844 return BFA_STATUS_DEVBUSY;
2845
2846 flash->op_busy = 1;
2847 flash->cbfn = cbfn;
2848 flash->cbarg = cbarg;
2849 flash->type = type;
2850 flash->instance = instance;
2851 flash->residue = len;
2852 flash->offset = 0;
2853 flash->addr_off = offset;
2854 flash->ubuf = buf;
2855
2856 bfa_flash_read_send(flash);
2857
2858 return BFA_STATUS_OK;
2859}