blob: b70c15fb1e97a78f5d35f4779399fb80ad144712 [file] [log] [blame]
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301/*
2 * This file is part of the Chelsio FCoE driver for Linux.
3 *
4 * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35#include <linux/pci.h>
36#include <linux/pci_regs.h>
37#include <linux/firmware.h>
38#include <linux/stddef.h>
39#include <linux/delay.h>
40#include <linux/string.h>
41#include <linux/compiler.h>
42#include <linux/jiffies.h>
43#include <linux/kernel.h>
44#include <linux/log2.h>
45
46#include "csio_hw.h"
47#include "csio_lnode.h"
48#include "csio_rnode.h"
49
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +053050int csio_dbg_level = 0xFEFF;
51unsigned int csio_port_mask = 0xf;
52
53/* Default FW event queue entries. */
54static uint32_t csio_evtq_sz = CSIO_EVTQ_SIZE;
55
56/* Default MSI param level */
57int csio_msi = 2;
58
59/* FCoE function instances */
60static int dev_num;
61
62/* FCoE Adapter types & its description */
Arvind Bhushan7cc16382013-03-14 05:09:08 +000063static const struct csio_adap_desc csio_t4_fcoe_adapters[] = {
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +053064 {"T440-Dbg 10G", "Chelsio T440-Dbg 10G [FCoE]"},
65 {"T420-CR 10G", "Chelsio T420-CR 10G [FCoE]"},
66 {"T422-CR 10G/1G", "Chelsio T422-CR 10G/1G [FCoE]"},
67 {"T440-CR 10G", "Chelsio T440-CR 10G [FCoE]"},
68 {"T420-BCH 10G", "Chelsio T420-BCH 10G [FCoE]"},
69 {"T440-BCH 10G", "Chelsio T440-BCH 10G [FCoE]"},
70 {"T440-CH 10G", "Chelsio T440-CH 10G [FCoE]"},
71 {"T420-SO 10G", "Chelsio T420-SO 10G [FCoE]"},
72 {"T420-CX4 10G", "Chelsio T420-CX4 10G [FCoE]"},
73 {"T420-BT 10G", "Chelsio T420-BT 10G [FCoE]"},
74 {"T404-BT 1G", "Chelsio T404-BT 1G [FCoE]"},
75 {"B420-SR 10G", "Chelsio B420-SR 10G [FCoE]"},
76 {"B404-BT 1G", "Chelsio B404-BT 1G [FCoE]"},
77 {"T480-CR 10G", "Chelsio T480-CR 10G [FCoE]"},
78 {"T440-LP-CR 10G", "Chelsio T440-LP-CR 10G [FCoE]"},
Arvind Bhushan7cc16382013-03-14 05:09:08 +000079 {"AMSTERDAM 10G", "Chelsio AMSTERDAM 10G [FCoE]"},
80 {"HUAWEI T480 10G", "Chelsio HUAWEI T480 10G [FCoE]"},
81 {"HUAWEI T440 10G", "Chelsio HUAWEI T440 10G [FCoE]"},
82 {"HUAWEI STG 10G", "Chelsio HUAWEI STG 10G [FCoE]"},
83 {"ACROMAG XAUI 10G", "Chelsio ACROMAG XAUI 10G [FCoE]"},
84 {"ACROMAG SFP+ 10G", "Chelsio ACROMAG SFP+ 10G [FCoE]"},
85 {"QUANTA SFP+ 10G", "Chelsio QUANTA SFP+ 10G [FCoE]"},
86 {"HUAWEI 10Gbase-T", "Chelsio HUAWEI 10Gbase-T [FCoE]"},
87 {"HUAWEI T4TOE 10G", "Chelsio HUAWEI T4TOE 10G [FCoE]"}
88};
89
90static const struct csio_adap_desc csio_t5_fcoe_adapters[] = {
91 {"T580-Dbg 10G", "Chelsio T580-Dbg 10G [FCoE]"},
92 {"T520-CR 10G", "Chelsio T520-CR 10G [FCoE]"},
93 {"T522-CR 10G/1G", "Chelsio T452-CR 10G/1G [FCoE]"},
94 {"T540-CR 10G", "Chelsio T540-CR 10G [FCoE]"},
95 {"T520-BCH 10G", "Chelsio T520-BCH 10G [FCoE]"},
96 {"T540-BCH 10G", "Chelsio T540-BCH 10G [FCoE]"},
97 {"T540-CH 10G", "Chelsio T540-CH 10G [FCoE]"},
98 {"T520-SO 10G", "Chelsio T520-SO 10G [FCoE]"},
99 {"T520-CX4 10G", "Chelsio T520-CX4 10G [FCoE]"},
100 {"T520-BT 10G", "Chelsio T520-BT 10G [FCoE]"},
101 {"T504-BT 1G", "Chelsio T504-BT 1G [FCoE]"},
102 {"B520-SR 10G", "Chelsio B520-SR 10G [FCoE]"},
103 {"B504-BT 1G", "Chelsio B504-BT 1G [FCoE]"},
104 {"T580-CR 10G", "Chelsio T580-CR 10G [FCoE]"},
105 {"T540-LP-CR 10G", "Chelsio T540-LP-CR 10G [FCoE]"},
106 {"AMSTERDAM 10G", "Chelsio AMSTERDAM 10G [FCoE]"},
107 {"T580-LP-CR 40G", "Chelsio T580-LP-CR 40G [FCoE]"},
108 {"T520-LL-CR 10G", "Chelsio T520-LL-CR 10G [FCoE]"},
109 {"T560-CR 40G", "Chelsio T560-CR 40G [FCoE]"},
110 {"T580-CR 40G", "Chelsio T580-CR 40G [FCoE]"}
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530111};
112
113static void csio_mgmtm_cleanup(struct csio_mgmtm *);
114static void csio_hw_mbm_cleanup(struct csio_hw *);
115
116/* State machine forward declarations */
117static void csio_hws_uninit(struct csio_hw *, enum csio_hw_ev);
118static void csio_hws_configuring(struct csio_hw *, enum csio_hw_ev);
119static void csio_hws_initializing(struct csio_hw *, enum csio_hw_ev);
120static void csio_hws_ready(struct csio_hw *, enum csio_hw_ev);
121static void csio_hws_quiescing(struct csio_hw *, enum csio_hw_ev);
122static void csio_hws_quiesced(struct csio_hw *, enum csio_hw_ev);
123static void csio_hws_resetting(struct csio_hw *, enum csio_hw_ev);
124static void csio_hws_removing(struct csio_hw *, enum csio_hw_ev);
125static void csio_hws_pcierr(struct csio_hw *, enum csio_hw_ev);
126
127static void csio_hw_initialize(struct csio_hw *hw);
128static void csio_evtq_stop(struct csio_hw *hw);
129static void csio_evtq_start(struct csio_hw *hw);
130
131int csio_is_hw_ready(struct csio_hw *hw)
132{
133 return csio_match_state(hw, csio_hws_ready);
134}
135
136int csio_is_hw_removing(struct csio_hw *hw)
137{
138 return csio_match_state(hw, csio_hws_removing);
139}
140
141
142/*
143 * csio_hw_wait_op_done_val - wait until an operation is completed
144 * @hw: the HW module
145 * @reg: the register to check for completion
146 * @mask: a single-bit field within @reg that indicates completion
147 * @polarity: the value of the field when the operation is completed
148 * @attempts: number of check iterations
149 * @delay: delay in usecs between iterations
150 * @valp: where to store the value of the register at completion time
151 *
152 * Wait until an operation is completed by checking a bit in a register
153 * up to @attempts times. If @valp is not NULL the value of the register
154 * at the time it indicated completion is stored there. Returns 0 if the
155 * operation completes and -EAGAIN otherwise.
156 */
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000157int
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530158csio_hw_wait_op_done_val(struct csio_hw *hw, int reg, uint32_t mask,
159 int polarity, int attempts, int delay, uint32_t *valp)
160{
161 uint32_t val;
162 while (1) {
163 val = csio_rd_reg32(hw, reg);
164
165 if (!!(val & mask) == polarity) {
166 if (valp)
167 *valp = val;
168 return 0;
169 }
170
171 if (--attempts == 0)
172 return -EAGAIN;
173 if (delay)
174 udelay(delay);
175 }
176}
177
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000178/*
179 * csio_hw_tp_wr_bits_indirect - set/clear bits in an indirect TP register
180 * @hw: the adapter
181 * @addr: the indirect TP register address
182 * @mask: specifies the field within the register to modify
183 * @val: new value for the field
184 *
185 * Sets a field of an indirect TP register to the given value.
186 */
187void
188csio_hw_tp_wr_bits_indirect(struct csio_hw *hw, unsigned int addr,
189 unsigned int mask, unsigned int val)
190{
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530191 csio_wr_reg32(hw, addr, TP_PIO_ADDR_A);
192 val |= csio_rd_reg32(hw, TP_PIO_DATA_A) & ~mask;
193 csio_wr_reg32(hw, val, TP_PIO_DATA_A);
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000194}
195
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530196void
197csio_set_reg_field(struct csio_hw *hw, uint32_t reg, uint32_t mask,
198 uint32_t value)
199{
200 uint32_t val = csio_rd_reg32(hw, reg) & ~mask;
201
202 csio_wr_reg32(hw, val | value, reg);
203 /* Flush */
204 csio_rd_reg32(hw, reg);
205
206}
207
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530208static int
Naresh Kumar Inna5036f0a2012-11-20 18:15:40 +0530209csio_memory_write(struct csio_hw *hw, int mtype, u32 addr, u32 len, u32 *buf)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530210{
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000211 return hw->chip_ops->chip_memory_rw(hw, MEMWIN_CSIOSTOR, mtype,
212 addr, len, buf, 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530213}
214
215/*
216 * EEPROM reads take a few tens of us while writes can take a bit over 5 ms.
217 */
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000218#define EEPROM_MAX_RD_POLL 40
219#define EEPROM_MAX_WR_POLL 6
220#define EEPROM_STAT_ADDR 0x7bfc
221#define VPD_BASE 0x400
222#define VPD_BASE_OLD 0
223#define VPD_LEN 1024
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530224#define VPD_INFO_FLD_HDR_SIZE 3
225
226/*
227 * csio_hw_seeprom_read - read a serial EEPROM location
228 * @hw: hw to read
229 * @addr: EEPROM virtual address
230 * @data: where to store the read data
231 *
232 * Read a 32-bit word from a location in serial EEPROM using the card's PCI
233 * VPD capability. Note that this function must be called with a virtual
234 * address.
235 */
236static int
237csio_hw_seeprom_read(struct csio_hw *hw, uint32_t addr, uint32_t *data)
238{
239 uint16_t val = 0;
240 int attempts = EEPROM_MAX_RD_POLL;
241 uint32_t base = hw->params.pci.vpd_cap_addr;
242
243 if (addr >= EEPROMVSIZE || (addr & 3))
244 return -EINVAL;
245
246 pci_write_config_word(hw->pdev, base + PCI_VPD_ADDR, (uint16_t)addr);
247
248 do {
249 udelay(10);
250 pci_read_config_word(hw->pdev, base + PCI_VPD_ADDR, &val);
251 } while (!(val & PCI_VPD_ADDR_F) && --attempts);
252
253 if (!(val & PCI_VPD_ADDR_F)) {
254 csio_err(hw, "reading EEPROM address 0x%x failed\n", addr);
255 return -EINVAL;
256 }
257
258 pci_read_config_dword(hw->pdev, base + PCI_VPD_DATA, data);
259 *data = le32_to_cpu(*data);
Naresh Kumar Inna5036f0a2012-11-20 18:15:40 +0530260
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530261 return 0;
262}
263
264/*
265 * Partial EEPROM Vital Product Data structure. Includes only the ID and
266 * VPD-R sections.
267 */
268struct t4_vpd_hdr {
269 u8 id_tag;
270 u8 id_len[2];
271 u8 id_data[ID_LEN];
272 u8 vpdr_tag;
273 u8 vpdr_len[2];
274};
275
276/*
277 * csio_hw_get_vpd_keyword_val - Locates an information field keyword in
278 * the VPD
279 * @v: Pointer to buffered vpd data structure
280 * @kw: The keyword to search for
281 *
282 * Returns the value of the information field keyword or
283 * -EINVAL otherwise.
284 */
285static int
286csio_hw_get_vpd_keyword_val(const struct t4_vpd_hdr *v, const char *kw)
287{
288 int32_t i;
289 int32_t offset , len;
290 const uint8_t *buf = &v->id_tag;
291 const uint8_t *vpdr_len = &v->vpdr_tag;
292 offset = sizeof(struct t4_vpd_hdr);
293 len = (uint16_t)vpdr_len[1] + ((uint16_t)vpdr_len[2] << 8);
294
295 if (len + sizeof(struct t4_vpd_hdr) > VPD_LEN)
296 return -EINVAL;
297
298 for (i = offset; (i + VPD_INFO_FLD_HDR_SIZE) <= (offset + len);) {
299 if (memcmp(buf + i , kw, 2) == 0) {
300 i += VPD_INFO_FLD_HDR_SIZE;
301 return i;
302 }
303
304 i += VPD_INFO_FLD_HDR_SIZE + buf[i+2];
305 }
306
307 return -EINVAL;
308}
309
310static int
311csio_pci_capability(struct pci_dev *pdev, int cap, int *pos)
312{
313 *pos = pci_find_capability(pdev, cap);
314 if (*pos)
315 return 0;
316
317 return -1;
318}
319
320/*
321 * csio_hw_get_vpd_params - read VPD parameters from VPD EEPROM
322 * @hw: HW module
323 * @p: where to store the parameters
324 *
325 * Reads card parameters stored in VPD EEPROM.
326 */
327static int
328csio_hw_get_vpd_params(struct csio_hw *hw, struct csio_vpd *p)
329{
330 int i, ret, ec, sn, addr;
331 uint8_t *vpd, csum;
332 const struct t4_vpd_hdr *v;
333 /* To get around compilation warning from strstrip */
334 char *s;
335
336 if (csio_is_valid_vpd(hw))
337 return 0;
338
339 ret = csio_pci_capability(hw->pdev, PCI_CAP_ID_VPD,
340 &hw->params.pci.vpd_cap_addr);
341 if (ret)
342 return -EINVAL;
343
344 vpd = kzalloc(VPD_LEN, GFP_ATOMIC);
345 if (vpd == NULL)
346 return -ENOMEM;
347
348 /*
349 * Card information normally starts at VPD_BASE but early cards had
350 * it at 0.
351 */
352 ret = csio_hw_seeprom_read(hw, VPD_BASE, (uint32_t *)(vpd));
353 addr = *vpd == 0x82 ? VPD_BASE : VPD_BASE_OLD;
354
355 for (i = 0; i < VPD_LEN; i += 4) {
356 ret = csio_hw_seeprom_read(hw, addr + i, (uint32_t *)(vpd + i));
357 if (ret) {
358 kfree(vpd);
359 return ret;
360 }
361 }
362
363 /* Reset the VPD flag! */
364 hw->flags &= (~CSIO_HWF_VPD_VALID);
365
366 v = (const struct t4_vpd_hdr *)vpd;
367
368#define FIND_VPD_KW(var, name) do { \
369 var = csio_hw_get_vpd_keyword_val(v, name); \
370 if (var < 0) { \
371 csio_err(hw, "missing VPD keyword " name "\n"); \
372 kfree(vpd); \
373 return -EINVAL; \
374 } \
375} while (0)
376
377 FIND_VPD_KW(i, "RV");
378 for (csum = 0; i >= 0; i--)
379 csum += vpd[i];
380
381 if (csum) {
382 csio_err(hw, "corrupted VPD EEPROM, actual csum %u\n", csum);
383 kfree(vpd);
384 return -EINVAL;
385 }
386 FIND_VPD_KW(ec, "EC");
387 FIND_VPD_KW(sn, "SN");
388#undef FIND_VPD_KW
389
390 memcpy(p->id, v->id_data, ID_LEN);
391 s = strstrip(p->id);
392 memcpy(p->ec, vpd + ec, EC_LEN);
393 s = strstrip(p->ec);
394 i = vpd[sn - VPD_INFO_FLD_HDR_SIZE + 2];
395 memcpy(p->sn, vpd + sn, min(i, SERNUM_LEN));
396 s = strstrip(p->sn);
397
398 csio_valid_vpd_copied(hw);
399
400 kfree(vpd);
401 return 0;
402}
403
404/*
405 * csio_hw_sf1_read - read data from the serial flash
406 * @hw: the HW module
407 * @byte_cnt: number of bytes to read
408 * @cont: whether another operation will be chained
409 * @lock: whether to lock SF for PL access only
410 * @valp: where to store the read data
411 *
412 * Reads up to 4 bytes of data from the serial flash. The location of
413 * the read needs to be specified prior to calling this by issuing the
414 * appropriate commands to the serial flash.
415 */
416static int
417csio_hw_sf1_read(struct csio_hw *hw, uint32_t byte_cnt, int32_t cont,
418 int32_t lock, uint32_t *valp)
419{
420 int ret;
421
422 if (!byte_cnt || byte_cnt > 4)
423 return -EINVAL;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530424 if (csio_rd_reg32(hw, SF_OP_A) & SF_BUSY_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530425 return -EBUSY;
426
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530427 csio_wr_reg32(hw, SF_LOCK_V(lock) | SF_CONT_V(cont) |
428 BYTECNT_V(byte_cnt - 1), SF_OP_A);
429 ret = csio_hw_wait_op_done_val(hw, SF_OP_A, SF_BUSY_F, 0, SF_ATTEMPTS,
430 10, NULL);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530431 if (!ret)
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530432 *valp = csio_rd_reg32(hw, SF_DATA_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530433 return ret;
434}
435
436/*
437 * csio_hw_sf1_write - write data to the serial flash
438 * @hw: the HW module
439 * @byte_cnt: number of bytes to write
440 * @cont: whether another operation will be chained
441 * @lock: whether to lock SF for PL access only
442 * @val: value to write
443 *
444 * Writes up to 4 bytes of data to the serial flash. The location of
445 * the write needs to be specified prior to calling this by issuing the
446 * appropriate commands to the serial flash.
447 */
448static int
449csio_hw_sf1_write(struct csio_hw *hw, uint32_t byte_cnt, uint32_t cont,
450 int32_t lock, uint32_t val)
451{
452 if (!byte_cnt || byte_cnt > 4)
453 return -EINVAL;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530454 if (csio_rd_reg32(hw, SF_OP_A) & SF_BUSY_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530455 return -EBUSY;
456
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530457 csio_wr_reg32(hw, val, SF_DATA_A);
458 csio_wr_reg32(hw, SF_CONT_V(cont) | BYTECNT_V(byte_cnt - 1) |
459 OP_V(1) | SF_LOCK_V(lock), SF_OP_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530460
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530461 return csio_hw_wait_op_done_val(hw, SF_OP_A, SF_BUSY_F, 0, SF_ATTEMPTS,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530462 10, NULL);
463}
464
465/*
466 * csio_hw_flash_wait_op - wait for a flash operation to complete
467 * @hw: the HW module
468 * @attempts: max number of polls of the status register
469 * @delay: delay between polls in ms
470 *
471 * Wait for a flash operation to complete by polling the status register.
472 */
473static int
474csio_hw_flash_wait_op(struct csio_hw *hw, int32_t attempts, int32_t delay)
475{
476 int ret;
477 uint32_t status;
478
479 while (1) {
480 ret = csio_hw_sf1_write(hw, 1, 1, 1, SF_RD_STATUS);
481 if (ret != 0)
482 return ret;
483
484 ret = csio_hw_sf1_read(hw, 1, 0, 1, &status);
485 if (ret != 0)
486 return ret;
487
488 if (!(status & 1))
489 return 0;
490 if (--attempts == 0)
491 return -EAGAIN;
492 if (delay)
493 msleep(delay);
494 }
495}
496
497/*
498 * csio_hw_read_flash - read words from serial flash
499 * @hw: the HW module
500 * @addr: the start address for the read
501 * @nwords: how many 32-bit words to read
502 * @data: where to store the read data
503 * @byte_oriented: whether to store data as bytes or as words
504 *
505 * Read the specified number of 32-bit words from the serial flash.
506 * If @byte_oriented is set the read data is stored as a byte array
507 * (i.e., big-endian), otherwise as 32-bit words in the platform's
508 * natural endianess.
509 */
510static int
511csio_hw_read_flash(struct csio_hw *hw, uint32_t addr, uint32_t nwords,
512 uint32_t *data, int32_t byte_oriented)
513{
514 int ret;
515
516 if (addr + nwords * sizeof(uint32_t) > hw->params.sf_size || (addr & 3))
517 return -EINVAL;
518
519 addr = swab32(addr) | SF_RD_DATA_FAST;
520
521 ret = csio_hw_sf1_write(hw, 4, 1, 0, addr);
522 if (ret != 0)
523 return ret;
524
525 ret = csio_hw_sf1_read(hw, 1, 1, 0, data);
526 if (ret != 0)
527 return ret;
528
529 for ( ; nwords; nwords--, data++) {
530 ret = csio_hw_sf1_read(hw, 4, nwords > 1, nwords == 1, data);
531 if (nwords == 1)
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530532 csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530533 if (ret)
534 return ret;
535 if (byte_oriented)
536 *data = htonl(*data);
537 }
538 return 0;
539}
540
541/*
542 * csio_hw_write_flash - write up to a page of data to the serial flash
543 * @hw: the hw
544 * @addr: the start address to write
545 * @n: length of data to write in bytes
546 * @data: the data to write
547 *
548 * Writes up to a page of data (256 bytes) to the serial flash starting
549 * at the given address. All the data must be written to the same page.
550 */
551static int
552csio_hw_write_flash(struct csio_hw *hw, uint32_t addr,
553 uint32_t n, const uint8_t *data)
554{
555 int ret = -EINVAL;
556 uint32_t buf[64];
557 uint32_t i, c, left, val, offset = addr & 0xff;
558
559 if (addr >= hw->params.sf_size || offset + n > SF_PAGE_SIZE)
560 return -EINVAL;
561
562 val = swab32(addr) | SF_PROG_PAGE;
563
564 ret = csio_hw_sf1_write(hw, 1, 0, 1, SF_WR_ENABLE);
565 if (ret != 0)
566 goto unlock;
567
568 ret = csio_hw_sf1_write(hw, 4, 1, 1, val);
569 if (ret != 0)
570 goto unlock;
571
572 for (left = n; left; left -= c) {
573 c = min(left, 4U);
574 for (val = 0, i = 0; i < c; ++i)
575 val = (val << 8) + *data++;
576
577 ret = csio_hw_sf1_write(hw, c, c != left, 1, val);
578 if (ret)
579 goto unlock;
580 }
581 ret = csio_hw_flash_wait_op(hw, 8, 1);
582 if (ret)
583 goto unlock;
584
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530585 csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530586
587 /* Read the page to verify the write succeeded */
588 ret = csio_hw_read_flash(hw, addr & ~0xff, ARRAY_SIZE(buf), buf, 1);
589 if (ret)
590 return ret;
591
592 if (memcmp(data - n, (uint8_t *)buf + offset, n)) {
593 csio_err(hw,
594 "failed to correctly write the flash page at %#x\n",
595 addr);
596 return -EINVAL;
597 }
598
599 return 0;
600
601unlock:
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530602 csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530603 return ret;
604}
605
606/*
607 * csio_hw_flash_erase_sectors - erase a range of flash sectors
608 * @hw: the HW module
609 * @start: the first sector to erase
610 * @end: the last sector to erase
611 *
612 * Erases the sectors in the given inclusive range.
613 */
614static int
615csio_hw_flash_erase_sectors(struct csio_hw *hw, int32_t start, int32_t end)
616{
617 int ret = 0;
618
619 while (start <= end) {
620
621 ret = csio_hw_sf1_write(hw, 1, 0, 1, SF_WR_ENABLE);
622 if (ret != 0)
623 goto out;
624
625 ret = csio_hw_sf1_write(hw, 4, 0, 1,
626 SF_ERASE_SECTOR | (start << 8));
627 if (ret != 0)
628 goto out;
629
630 ret = csio_hw_flash_wait_op(hw, 14, 500);
631 if (ret != 0)
632 goto out;
633
634 start++;
635 }
636out:
637 if (ret)
638 csio_err(hw, "erase of flash sector %d failed, error %d\n",
639 start, ret);
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530640 csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530641 return 0;
642}
643
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530644static void
645csio_hw_print_fw_version(struct csio_hw *hw, char *str)
646{
647 csio_info(hw, "%s: %u.%u.%u.%u\n", str,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +0530648 FW_HDR_FW_VER_MAJOR_G(hw->fwrev),
649 FW_HDR_FW_VER_MINOR_G(hw->fwrev),
650 FW_HDR_FW_VER_MICRO_G(hw->fwrev),
651 FW_HDR_FW_VER_BUILD_G(hw->fwrev));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530652}
653
654/*
655 * csio_hw_get_fw_version - read the firmware version
656 * @hw: HW module
657 * @vers: where to place the version
658 *
659 * Reads the FW version from flash.
660 */
661static int
662csio_hw_get_fw_version(struct csio_hw *hw, uint32_t *vers)
663{
664 return csio_hw_read_flash(hw, FW_IMG_START +
665 offsetof(struct fw_hdr, fw_ver), 1,
666 vers, 0);
667}
668
669/*
670 * csio_hw_get_tp_version - read the TP microcode version
671 * @hw: HW module
672 * @vers: where to place the version
673 *
674 * Reads the TP microcode version from flash.
675 */
676static int
677csio_hw_get_tp_version(struct csio_hw *hw, u32 *vers)
678{
679 return csio_hw_read_flash(hw, FLASH_FW_START +
680 offsetof(struct fw_hdr, tp_microcode_ver), 1,
681 vers, 0);
682}
683
684/*
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530685 * csio_hw_fw_dload - download firmware.
686 * @hw: HW module
687 * @fw_data: firmware image to write.
688 * @size: image size
689 *
690 * Write the supplied firmware image to the card's serial flash.
691 */
692static int
693csio_hw_fw_dload(struct csio_hw *hw, uint8_t *fw_data, uint32_t size)
694{
695 uint32_t csum;
696 int32_t addr;
697 int ret;
698 uint32_t i;
699 uint8_t first_page[SF_PAGE_SIZE];
Naresh Kumar Inna5036f0a2012-11-20 18:15:40 +0530700 const __be32 *p = (const __be32 *)fw_data;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530701 struct fw_hdr *hdr = (struct fw_hdr *)fw_data;
702 uint32_t sf_sec_size;
703
704 if ((!hw->params.sf_size) || (!hw->params.sf_nsec)) {
705 csio_err(hw, "Serial Flash data invalid\n");
706 return -EINVAL;
707 }
708
709 if (!size) {
710 csio_err(hw, "FW image has no data\n");
711 return -EINVAL;
712 }
713
714 if (size & 511) {
715 csio_err(hw, "FW image size not multiple of 512 bytes\n");
716 return -EINVAL;
717 }
718
719 if (ntohs(hdr->len512) * 512 != size) {
720 csio_err(hw, "FW image size differs from size in FW header\n");
721 return -EINVAL;
722 }
723
724 if (size > FW_MAX_SIZE) {
725 csio_err(hw, "FW image too large, max is %u bytes\n",
726 FW_MAX_SIZE);
727 return -EINVAL;
728 }
729
730 for (csum = 0, i = 0; i < size / sizeof(csum); i++)
731 csum += ntohl(p[i]);
732
733 if (csum != 0xffffffff) {
734 csio_err(hw, "corrupted firmware image, checksum %#x\n", csum);
735 return -EINVAL;
736 }
737
738 sf_sec_size = hw->params.sf_size / hw->params.sf_nsec;
739 i = DIV_ROUND_UP(size, sf_sec_size); /* # of sectors spanned */
740
741 csio_dbg(hw, "Erasing sectors... start:%d end:%d\n",
742 FW_START_SEC, FW_START_SEC + i - 1);
743
744 ret = csio_hw_flash_erase_sectors(hw, FW_START_SEC,
745 FW_START_SEC + i - 1);
746 if (ret) {
747 csio_err(hw, "Flash Erase failed\n");
748 goto out;
749 }
750
751 /*
752 * We write the correct version at the end so the driver can see a bad
753 * version if the FW write fails. Start by writing a copy of the
754 * first page with a bad version.
755 */
756 memcpy(first_page, fw_data, SF_PAGE_SIZE);
757 ((struct fw_hdr *)first_page)->fw_ver = htonl(0xffffffff);
758 ret = csio_hw_write_flash(hw, FW_IMG_START, SF_PAGE_SIZE, first_page);
759 if (ret)
760 goto out;
761
762 csio_dbg(hw, "Writing Flash .. start:%d end:%d\n",
763 FW_IMG_START, FW_IMG_START + size);
764
765 addr = FW_IMG_START;
766 for (size -= SF_PAGE_SIZE; size; size -= SF_PAGE_SIZE) {
767 addr += SF_PAGE_SIZE;
768 fw_data += SF_PAGE_SIZE;
769 ret = csio_hw_write_flash(hw, addr, SF_PAGE_SIZE, fw_data);
770 if (ret)
771 goto out;
772 }
773
774 ret = csio_hw_write_flash(hw,
775 FW_IMG_START +
776 offsetof(struct fw_hdr, fw_ver),
777 sizeof(hdr->fw_ver),
778 (const uint8_t *)&hdr->fw_ver);
779
780out:
781 if (ret)
782 csio_err(hw, "firmware download failed, error %d\n", ret);
783 return ret;
784}
785
786static int
787csio_hw_get_flash_params(struct csio_hw *hw)
788{
789 int ret;
790 uint32_t info = 0;
791
792 ret = csio_hw_sf1_write(hw, 1, 1, 0, SF_RD_ID);
793 if (!ret)
794 ret = csio_hw_sf1_read(hw, 3, 0, 1, &info);
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530795 csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530796 if (ret != 0)
797 return ret;
798
799 if ((info & 0xff) != 0x20) /* not a Numonix flash */
800 return -EINVAL;
801 info >>= 16; /* log2 of size */
802 if (info >= 0x14 && info < 0x18)
803 hw->params.sf_nsec = 1 << (info - 16);
804 else if (info == 0x18)
805 hw->params.sf_nsec = 64;
806 else
807 return -EINVAL;
808 hw->params.sf_size = 1 << info;
809
810 return 0;
811}
812
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530813/*****************************************************************************/
814/* HW State machine assists */
815/*****************************************************************************/
816
817static int
818csio_hw_dev_ready(struct csio_hw *hw)
819{
820 uint32_t reg;
821 int cnt = 6;
822
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530823 while (((reg = csio_rd_reg32(hw, PL_WHOAMI_A)) == 0xFFFFFFFF) &&
824 (--cnt != 0))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530825 mdelay(100);
826
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530827 if ((cnt == 0) && (((int32_t)(SOURCEPF_G(reg)) < 0) ||
828 (SOURCEPF_G(reg) >= CSIO_MAX_PFN))) {
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530829 csio_err(hw, "PL_WHOAMI returned 0x%x, cnt:%d\n", reg, cnt);
830 return -EIO;
831 }
832
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530833 hw->pfn = SOURCEPF_G(reg);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530834
835 return 0;
836}
837
838/*
839 * csio_do_hello - Perform the HELLO FW Mailbox command and process response.
840 * @hw: HW module
841 * @state: Device state
842 *
843 * FW_HELLO_CMD has to be polled for completion.
844 */
845static int
846csio_do_hello(struct csio_hw *hw, enum csio_dev_state *state)
847{
848 struct csio_mb *mbp;
849 int rv = 0;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530850 enum fw_retval retval;
851 uint8_t mpfn;
852 char state_str[16];
853 int retries = FW_CMD_HELLO_RETRIES;
854
855 memset(state_str, 0, sizeof(state_str));
856
857 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
858 if (!mbp) {
859 rv = -ENOMEM;
860 CSIO_INC_STATS(hw, n_err_nomem);
861 goto out;
862 }
863
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530864retry:
865 csio_mb_hello(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn,
Hariprasad Shenai666224d2014-12-11 11:11:43 +0530866 hw->pfn, CSIO_MASTER_MAY, NULL);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530867
868 rv = csio_mb_issue(hw, mbp);
869 if (rv) {
870 csio_err(hw, "failed to issue HELLO cmd. ret:%d.\n", rv);
871 goto out_free_mb;
872 }
873
874 csio_mb_process_hello_rsp(hw, mbp, &retval, state, &mpfn);
875 if (retval != FW_SUCCESS) {
876 csio_err(hw, "HELLO cmd failed with ret: %d\n", retval);
877 rv = -EINVAL;
878 goto out_free_mb;
879 }
880
881 /* Firmware has designated us to be master */
882 if (hw->pfn == mpfn) {
883 hw->flags |= CSIO_HWF_MASTER;
884 } else if (*state == CSIO_DEV_STATE_UNINIT) {
885 /*
886 * If we're not the Master PF then we need to wait around for
887 * the Master PF Driver to finish setting up the adapter.
888 *
889 * Note that we also do this wait if we're a non-Master-capable
890 * PF and there is no current Master PF; a Master PF may show up
891 * momentarily and we wouldn't want to fail pointlessly. (This
892 * can happen when an OS loads lots of different drivers rapidly
893 * at the same time). In this case, the Master PF returned by
894 * the firmware will be PCIE_FW_MASTER_MASK so the test below
895 * will work ...
896 */
897
898 int waiting = FW_CMD_HELLO_TIMEOUT;
899
900 /*
901 * Wait for the firmware to either indicate an error or
902 * initialized state. If we see either of these we bail out
903 * and report the issue to the caller. If we exhaust the
904 * "hello timeout" and we haven't exhausted our retries, try
905 * again. Otherwise bail with a timeout error.
906 */
907 for (;;) {
908 uint32_t pcie_fw;
909
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000910 spin_unlock_irq(&hw->lock);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530911 msleep(50);
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000912 spin_lock_irq(&hw->lock);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530913 waiting -= 50;
914
915 /*
916 * If neither Error nor Initialialized are indicated
917 * by the firmware keep waiting till we exaust our
918 * timeout ... and then retry if we haven't exhausted
919 * our retries ...
920 */
Hariprasad Shenaif061de42015-01-05 16:30:44 +0530921 pcie_fw = csio_rd_reg32(hw, PCIE_FW_A);
922 if (!(pcie_fw & (PCIE_FW_ERR_F|PCIE_FW_INIT_F))) {
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530923 if (waiting <= 0) {
924 if (retries-- > 0)
925 goto retry;
926
927 rv = -ETIMEDOUT;
928 break;
929 }
930 continue;
931 }
932
933 /*
934 * We either have an Error or Initialized condition
935 * report errors preferentially.
936 */
937 if (state) {
Hariprasad Shenaif061de42015-01-05 16:30:44 +0530938 if (pcie_fw & PCIE_FW_ERR_F) {
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530939 *state = CSIO_DEV_STATE_ERR;
940 rv = -ETIMEDOUT;
Hariprasad Shenaif061de42015-01-05 16:30:44 +0530941 } else if (pcie_fw & PCIE_FW_INIT_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530942 *state = CSIO_DEV_STATE_INIT;
943 }
944
945 /*
946 * If we arrived before a Master PF was selected and
947 * there's not a valid Master PF, grab its identity
948 * for our caller.
949 */
Hariprasad Shenaif061de42015-01-05 16:30:44 +0530950 if (mpfn == PCIE_FW_MASTER_M &&
951 (pcie_fw & PCIE_FW_MASTER_VLD_F))
952 mpfn = PCIE_FW_MASTER_G(pcie_fw);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530953 break;
954 }
955 hw->flags &= ~CSIO_HWF_MASTER;
956 }
957
958 switch (*state) {
959 case CSIO_DEV_STATE_UNINIT:
960 strcpy(state_str, "Initializing");
961 break;
962 case CSIO_DEV_STATE_INIT:
963 strcpy(state_str, "Initialized");
964 break;
965 case CSIO_DEV_STATE_ERR:
966 strcpy(state_str, "Error");
967 break;
968 default:
969 strcpy(state_str, "Unknown");
970 break;
971 }
972
973 if (hw->pfn == mpfn)
974 csio_info(hw, "PF: %d, Coming up as MASTER, HW state: %s\n",
975 hw->pfn, state_str);
976 else
977 csio_info(hw,
978 "PF: %d, Coming up as SLAVE, Master PF: %d, HW state: %s\n",
979 hw->pfn, mpfn, state_str);
980
981out_free_mb:
982 mempool_free(mbp, hw->mb_mempool);
983out:
984 return rv;
985}
986
987/*
988 * csio_do_bye - Perform the BYE FW Mailbox command and process response.
989 * @hw: HW module
990 *
991 */
992static int
993csio_do_bye(struct csio_hw *hw)
994{
995 struct csio_mb *mbp;
996 enum fw_retval retval;
997
998 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
999 if (!mbp) {
1000 CSIO_INC_STATS(hw, n_err_nomem);
1001 return -ENOMEM;
1002 }
1003
1004 csio_mb_bye(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
1005
1006 if (csio_mb_issue(hw, mbp)) {
1007 csio_err(hw, "Issue of BYE command failed\n");
1008 mempool_free(mbp, hw->mb_mempool);
1009 return -EINVAL;
1010 }
1011
1012 retval = csio_mb_fw_retval(mbp);
1013 if (retval != FW_SUCCESS) {
1014 mempool_free(mbp, hw->mb_mempool);
1015 return -EINVAL;
1016 }
1017
1018 mempool_free(mbp, hw->mb_mempool);
1019
1020 return 0;
1021}
1022
1023/*
1024 * csio_do_reset- Perform the device reset.
1025 * @hw: HW module
1026 * @fw_rst: FW reset
1027 *
1028 * If fw_rst is set, issues FW reset mbox cmd otherwise
1029 * does PIO reset.
1030 * Performs reset of the function.
1031 */
1032static int
1033csio_do_reset(struct csio_hw *hw, bool fw_rst)
1034{
1035 struct csio_mb *mbp;
1036 enum fw_retval retval;
1037
1038 if (!fw_rst) {
1039 /* PIO reset */
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301040 csio_wr_reg32(hw, PIORSTMODE_F | PIORST_F, PL_RST_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301041 mdelay(2000);
1042 return 0;
1043 }
1044
1045 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1046 if (!mbp) {
1047 CSIO_INC_STATS(hw, n_err_nomem);
1048 return -ENOMEM;
1049 }
1050
1051 csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301052 PIORSTMODE_F | PIORST_F, 0, NULL);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301053
1054 if (csio_mb_issue(hw, mbp)) {
1055 csio_err(hw, "Issue of RESET command failed.n");
1056 mempool_free(mbp, hw->mb_mempool);
1057 return -EINVAL;
1058 }
1059
1060 retval = csio_mb_fw_retval(mbp);
1061 if (retval != FW_SUCCESS) {
1062 csio_err(hw, "RESET cmd failed with ret:0x%x.\n", retval);
1063 mempool_free(mbp, hw->mb_mempool);
1064 return -EINVAL;
1065 }
1066
1067 mempool_free(mbp, hw->mb_mempool);
1068
1069 return 0;
1070}
1071
1072static int
1073csio_hw_validate_caps(struct csio_hw *hw, struct csio_mb *mbp)
1074{
1075 struct fw_caps_config_cmd *rsp = (struct fw_caps_config_cmd *)mbp->mb;
1076 uint16_t caps;
1077
1078 caps = ntohs(rsp->fcoecaps);
1079
1080 if (!(caps & FW_CAPS_CONFIG_FCOE_INITIATOR)) {
1081 csio_err(hw, "No FCoE Initiator capability in the firmware.\n");
1082 return -EINVAL;
1083 }
1084
1085 if (!(caps & FW_CAPS_CONFIG_FCOE_CTRL_OFLD)) {
1086 csio_err(hw, "No FCoE Control Offload capability\n");
1087 return -EINVAL;
1088 }
1089
1090 return 0;
1091}
1092
1093/*
1094 * csio_hw_fw_halt - issue a reset/halt to FW and put uP into RESET
1095 * @hw: the HW module
1096 * @mbox: mailbox to use for the FW RESET command (if desired)
1097 * @force: force uP into RESET even if FW RESET command fails
1098 *
1099 * Issues a RESET command to firmware (if desired) with a HALT indication
1100 * and then puts the microprocessor into RESET state. The RESET command
1101 * will only be issued if a legitimate mailbox is provided (mbox <=
1102 * PCIE_FW_MASTER_MASK).
1103 *
1104 * This is generally used in order for the host to safely manipulate the
1105 * adapter without fear of conflicting with whatever the firmware might
1106 * be doing. The only way out of this state is to RESTART the firmware
1107 * ...
1108 */
1109static int
1110csio_hw_fw_halt(struct csio_hw *hw, uint32_t mbox, int32_t force)
1111{
1112 enum fw_retval retval = 0;
1113
1114 /*
1115 * If a legitimate mailbox is provided, issue a RESET command
1116 * with a HALT indication.
1117 */
Hariprasad Shenaif061de42015-01-05 16:30:44 +05301118 if (mbox <= PCIE_FW_MASTER_M) {
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301119 struct csio_mb *mbp;
1120
1121 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1122 if (!mbp) {
1123 CSIO_INC_STATS(hw, n_err_nomem);
1124 return -ENOMEM;
1125 }
1126
1127 csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301128 PIORSTMODE_F | PIORST_F, FW_RESET_CMD_HALT_F,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301129 NULL);
1130
1131 if (csio_mb_issue(hw, mbp)) {
1132 csio_err(hw, "Issue of RESET command failed!\n");
1133 mempool_free(mbp, hw->mb_mempool);
1134 return -EINVAL;
1135 }
1136
1137 retval = csio_mb_fw_retval(mbp);
1138 mempool_free(mbp, hw->mb_mempool);
1139 }
1140
1141 /*
1142 * Normally we won't complete the operation if the firmware RESET
1143 * command fails but if our caller insists we'll go ahead and put the
1144 * uP into RESET. This can be useful if the firmware is hung or even
1145 * missing ... We'll have to take the risk of putting the uP into
1146 * RESET without the cooperation of firmware in that case.
1147 *
1148 * We also force the firmware's HALT flag to be on in case we bypassed
1149 * the firmware RESET command above or we're dealing with old firmware
1150 * which doesn't have the HALT capability. This will serve as a flag
1151 * for the incoming firmware to know that it's coming out of a HALT
1152 * rather than a RESET ... if it's new enough to understand that ...
1153 */
1154 if (retval == 0 || force) {
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05301155 csio_set_reg_field(hw, CIM_BOOT_CFG_A, UPCRST_F, UPCRST_F);
Hariprasad Shenaif061de42015-01-05 16:30:44 +05301156 csio_set_reg_field(hw, PCIE_FW_A, PCIE_FW_HALT_F,
1157 PCIE_FW_HALT_F);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301158 }
1159
1160 /*
1161 * And we always return the result of the firmware RESET command
1162 * even when we force the uP into RESET ...
1163 */
1164 return retval ? -EINVAL : 0;
1165}
1166
1167/*
1168 * csio_hw_fw_restart - restart the firmware by taking the uP out of RESET
1169 * @hw: the HW module
1170 * @reset: if we want to do a RESET to restart things
1171 *
1172 * Restart firmware previously halted by csio_hw_fw_halt(). On successful
1173 * return the previous PF Master remains as the new PF Master and there
1174 * is no need to issue a new HELLO command, etc.
1175 *
1176 * We do this in two ways:
1177 *
1178 * 1. If we're dealing with newer firmware we'll simply want to take
1179 * the chip's microprocessor out of RESET. This will cause the
1180 * firmware to start up from its start vector. And then we'll loop
1181 * until the firmware indicates it's started again (PCIE_FW.HALT
1182 * reset to 0) or we timeout.
1183 *
1184 * 2. If we're dealing with older firmware then we'll need to RESET
1185 * the chip since older firmware won't recognize the PCIE_FW.HALT
1186 * flag and automatically RESET itself on startup.
1187 */
1188static int
1189csio_hw_fw_restart(struct csio_hw *hw, uint32_t mbox, int32_t reset)
1190{
1191 if (reset) {
1192 /*
1193 * Since we're directing the RESET instead of the firmware
1194 * doing it automatically, we need to clear the PCIE_FW.HALT
1195 * bit.
1196 */
Hariprasad Shenaif061de42015-01-05 16:30:44 +05301197 csio_set_reg_field(hw, PCIE_FW_A, PCIE_FW_HALT_F, 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301198
1199 /*
1200 * If we've been given a valid mailbox, first try to get the
1201 * firmware to do the RESET. If that works, great and we can
1202 * return success. Otherwise, if we haven't been given a
1203 * valid mailbox or the RESET command failed, fall back to
1204 * hitting the chip with a hammer.
1205 */
Hariprasad Shenaif061de42015-01-05 16:30:44 +05301206 if (mbox <= PCIE_FW_MASTER_M) {
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05301207 csio_set_reg_field(hw, CIM_BOOT_CFG_A, UPCRST_F, 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301208 msleep(100);
1209 if (csio_do_reset(hw, true) == 0)
1210 return 0;
1211 }
1212
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301213 csio_wr_reg32(hw, PIORSTMODE_F | PIORST_F, PL_RST_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301214 msleep(2000);
1215 } else {
1216 int ms;
1217
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05301218 csio_set_reg_field(hw, CIM_BOOT_CFG_A, UPCRST_F, 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301219 for (ms = 0; ms < FW_CMD_MAX_TIMEOUT; ) {
Hariprasad Shenaif061de42015-01-05 16:30:44 +05301220 if (!(csio_rd_reg32(hw, PCIE_FW_A) & PCIE_FW_HALT_F))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301221 return 0;
1222 msleep(100);
1223 ms += 100;
1224 }
1225 return -ETIMEDOUT;
1226 }
1227 return 0;
1228}
1229
1230/*
1231 * csio_hw_fw_upgrade - perform all of the steps necessary to upgrade FW
1232 * @hw: the HW module
1233 * @mbox: mailbox to use for the FW RESET command (if desired)
1234 * @fw_data: the firmware image to write
1235 * @size: image size
1236 * @force: force upgrade even if firmware doesn't cooperate
1237 *
1238 * Perform all of the steps necessary for upgrading an adapter's
1239 * firmware image. Normally this requires the cooperation of the
1240 * existing firmware in order to halt all existing activities
1241 * but if an invalid mailbox token is passed in we skip that step
1242 * (though we'll still put the adapter microprocessor into RESET in
1243 * that case).
1244 *
1245 * On successful return the new firmware will have been loaded and
1246 * the adapter will have been fully RESET losing all previous setup
1247 * state. On unsuccessful return the adapter may be completely hosed ...
1248 * positive errno indicates that the adapter is ~probably~ intact, a
1249 * negative errno indicates that things are looking bad ...
1250 */
1251static int
1252csio_hw_fw_upgrade(struct csio_hw *hw, uint32_t mbox,
1253 const u8 *fw_data, uint32_t size, int32_t force)
1254{
1255 const struct fw_hdr *fw_hdr = (const struct fw_hdr *)fw_data;
1256 int reset, ret;
1257
1258 ret = csio_hw_fw_halt(hw, mbox, force);
1259 if (ret != 0 && !force)
1260 return ret;
1261
1262 ret = csio_hw_fw_dload(hw, (uint8_t *) fw_data, size);
1263 if (ret != 0)
1264 return ret;
1265
1266 /*
1267 * Older versions of the firmware don't understand the new
1268 * PCIE_FW.HALT flag and so won't know to perform a RESET when they
1269 * restart. So for newly loaded older firmware we'll have to do the
1270 * RESET for it so it starts up on a clean slate. We can tell if
1271 * the newly loaded firmware will handle this right by checking
1272 * its header flags to see if it advertises the capability.
1273 */
1274 reset = ((ntohl(fw_hdr->flags) & FW_HDR_FLAGS_RESET_HALT) == 0);
1275 return csio_hw_fw_restart(hw, mbox, reset);
1276}
1277
1278
1279/*
1280 * csio_hw_fw_config_file - setup an adapter via a Configuration File
1281 * @hw: the HW module
1282 * @mbox: mailbox to use for the FW command
1283 * @mtype: the memory type where the Configuration File is located
1284 * @maddr: the memory address where the Configuration File is located
1285 * @finiver: return value for CF [fini] version
1286 * @finicsum: return value for CF [fini] checksum
1287 * @cfcsum: return value for CF computed checksum
1288 *
1289 * Issue a command to get the firmware to process the Configuration
1290 * File located at the specified mtype/maddress. If the Configuration
1291 * File is processed successfully and return value pointers are
1292 * provided, the Configuration File "[fini] section version and
1293 * checksum values will be returned along with the computed checksum.
1294 * It's up to the caller to decide how it wants to respond to the
1295 * checksums not matching but it recommended that a prominant warning
1296 * be emitted in order to help people rapidly identify changed or
1297 * corrupted Configuration Files.
1298 *
1299 * Also note that it's possible to modify things like "niccaps",
1300 * "toecaps",etc. between processing the Configuration File and telling
1301 * the firmware to use the new configuration. Callers which want to
1302 * do this will need to "hand-roll" their own CAPS_CONFIGS commands for
1303 * Configuration Files if they want to do this.
1304 */
1305static int
1306csio_hw_fw_config_file(struct csio_hw *hw,
1307 unsigned int mtype, unsigned int maddr,
1308 uint32_t *finiver, uint32_t *finicsum, uint32_t *cfcsum)
1309{
1310 struct csio_mb *mbp;
1311 struct fw_caps_config_cmd *caps_cmd;
1312 int rv = -EINVAL;
1313 enum fw_retval ret;
1314
1315 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1316 if (!mbp) {
1317 CSIO_INC_STATS(hw, n_err_nomem);
1318 return -ENOMEM;
1319 }
1320 /*
1321 * Tell the firmware to process the indicated Configuration File.
1322 * If there are no errors and the caller has provided return value
1323 * pointers for the [fini] section version, checksum and computed
1324 * checksum, pass those back to the caller.
1325 */
1326 caps_cmd = (struct fw_caps_config_cmd *)(mbp->mb);
1327 CSIO_INIT_MBP(mbp, caps_cmd, CSIO_MB_DEFAULT_TMO, hw, NULL, 1);
1328 caps_cmd->op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301329 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
1330 FW_CMD_REQUEST_F |
1331 FW_CMD_READ_F);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301332 caps_cmd->cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05301333 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
1334 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
1335 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301336 FW_LEN16(*caps_cmd));
1337
1338 if (csio_mb_issue(hw, mbp)) {
1339 csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD failed!\n");
1340 goto out;
1341 }
1342
1343 ret = csio_mb_fw_retval(mbp);
1344 if (ret != FW_SUCCESS) {
1345 csio_dbg(hw, "FW_CAPS_CONFIG_CMD returned %d!\n", rv);
1346 goto out;
1347 }
1348
1349 if (finiver)
1350 *finiver = ntohl(caps_cmd->finiver);
1351 if (finicsum)
1352 *finicsum = ntohl(caps_cmd->finicsum);
1353 if (cfcsum)
1354 *cfcsum = ntohl(caps_cmd->cfcsum);
1355
1356 /* Validate device capabilities */
1357 if (csio_hw_validate_caps(hw, mbp)) {
1358 rv = -ENOENT;
1359 goto out;
1360 }
1361
1362 /*
1363 * And now tell the firmware to use the configuration we just loaded.
1364 */
1365 caps_cmd->op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301366 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
1367 FW_CMD_REQUEST_F |
1368 FW_CMD_WRITE_F);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301369 caps_cmd->cfvalid_to_len16 = htonl(FW_LEN16(*caps_cmd));
1370
1371 if (csio_mb_issue(hw, mbp)) {
1372 csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD failed!\n");
1373 goto out;
1374 }
1375
1376 ret = csio_mb_fw_retval(mbp);
1377 if (ret != FW_SUCCESS) {
1378 csio_dbg(hw, "FW_CAPS_CONFIG_CMD returned %d!\n", rv);
1379 goto out;
1380 }
1381
1382 rv = 0;
1383out:
1384 mempool_free(mbp, hw->mb_mempool);
1385 return rv;
1386}
1387
1388/*
1389 * csio_get_device_params - Get device parameters.
1390 * @hw: HW module
1391 *
1392 */
1393static int
1394csio_get_device_params(struct csio_hw *hw)
1395{
1396 struct csio_wrm *wrm = csio_hw_to_wrm(hw);
1397 struct csio_mb *mbp;
1398 enum fw_retval retval;
1399 u32 param[6];
1400 int i, j = 0;
1401
1402 /* Initialize portids to -1 */
1403 for (i = 0; i < CSIO_MAX_PPORTS; i++)
1404 hw->pport[i].portid = -1;
1405
1406 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1407 if (!mbp) {
1408 CSIO_INC_STATS(hw, n_err_nomem);
1409 return -ENOMEM;
1410 }
1411
1412 /* Get port vec information. */
1413 param[0] = FW_PARAM_DEV(PORTVEC);
1414
1415 /* Get Core clock. */
1416 param[1] = FW_PARAM_DEV(CCLK);
1417
1418 /* Get EQ id start and end. */
1419 param[2] = FW_PARAM_PFVF(EQ_START);
1420 param[3] = FW_PARAM_PFVF(EQ_END);
1421
1422 /* Get IQ id start and end. */
1423 param[4] = FW_PARAM_PFVF(IQFLINT_START);
1424 param[5] = FW_PARAM_PFVF(IQFLINT_END);
1425
1426 csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
1427 ARRAY_SIZE(param), param, NULL, false, NULL);
1428 if (csio_mb_issue(hw, mbp)) {
1429 csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
1430 mempool_free(mbp, hw->mb_mempool);
1431 return -EINVAL;
1432 }
1433
1434 csio_mb_process_read_params_rsp(hw, mbp, &retval,
1435 ARRAY_SIZE(param), param);
1436 if (retval != FW_SUCCESS) {
1437 csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
1438 retval);
1439 mempool_free(mbp, hw->mb_mempool);
1440 return -EINVAL;
1441 }
1442
1443 /* cache the information. */
1444 hw->port_vec = param[0];
1445 hw->vpd.cclk = param[1];
1446 wrm->fw_eq_start = param[2];
1447 wrm->fw_iq_start = param[4];
1448
1449 /* Using FW configured max iqs & eqs */
1450 if ((hw->flags & CSIO_HWF_USING_SOFT_PARAMS) ||
1451 !csio_is_hw_master(hw)) {
1452 hw->cfg_niq = param[5] - param[4] + 1;
1453 hw->cfg_neq = param[3] - param[2] + 1;
1454 csio_dbg(hw, "Using fwconfig max niqs %d neqs %d\n",
1455 hw->cfg_niq, hw->cfg_neq);
1456 }
1457
1458 hw->port_vec &= csio_port_mask;
1459
1460 hw->num_pports = hweight32(hw->port_vec);
1461
1462 csio_dbg(hw, "Port vector: 0x%x, #ports: %d\n",
1463 hw->port_vec, hw->num_pports);
1464
1465 for (i = 0; i < hw->num_pports; i++) {
1466 while ((hw->port_vec & (1 << j)) == 0)
1467 j++;
1468 hw->pport[i].portid = j++;
1469 csio_dbg(hw, "Found Port:%d\n", hw->pport[i].portid);
1470 }
1471 mempool_free(mbp, hw->mb_mempool);
1472
1473 return 0;
1474}
1475
1476
1477/*
1478 * csio_config_device_caps - Get and set device capabilities.
1479 * @hw: HW module
1480 *
1481 */
1482static int
1483csio_config_device_caps(struct csio_hw *hw)
1484{
1485 struct csio_mb *mbp;
1486 enum fw_retval retval;
1487 int rv = -EINVAL;
1488
1489 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1490 if (!mbp) {
1491 CSIO_INC_STATS(hw, n_err_nomem);
1492 return -ENOMEM;
1493 }
1494
1495 /* Get device capabilities */
1496 csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, 0, 0, 0, 0, NULL);
1497
1498 if (csio_mb_issue(hw, mbp)) {
1499 csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(r) failed!\n");
1500 goto out;
1501 }
1502
1503 retval = csio_mb_fw_retval(mbp);
1504 if (retval != FW_SUCCESS) {
1505 csio_err(hw, "FW_CAPS_CONFIG_CMD(r) returned %d!\n", retval);
1506 goto out;
1507 }
1508
1509 /* Validate device capabilities */
1510 if (csio_hw_validate_caps(hw, mbp))
1511 goto out;
1512
1513 /* Don't config device capabilities if already configured */
1514 if (hw->fw_state == CSIO_DEV_STATE_INIT) {
1515 rv = 0;
1516 goto out;
1517 }
1518
1519 /* Write back desired device capabilities */
1520 csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, true, true,
1521 false, true, NULL);
1522
1523 if (csio_mb_issue(hw, mbp)) {
1524 csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(w) failed!\n");
1525 goto out;
1526 }
1527
1528 retval = csio_mb_fw_retval(mbp);
1529 if (retval != FW_SUCCESS) {
1530 csio_err(hw, "FW_CAPS_CONFIG_CMD(w) returned %d!\n", retval);
1531 goto out;
1532 }
1533
1534 rv = 0;
1535out:
1536 mempool_free(mbp, hw->mb_mempool);
1537 return rv;
1538}
1539
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301540/*
1541 * csio_enable_ports - Bring up all available ports.
1542 * @hw: HW module.
1543 *
1544 */
1545static int
1546csio_enable_ports(struct csio_hw *hw)
1547{
1548 struct csio_mb *mbp;
1549 enum fw_retval retval;
1550 uint8_t portid;
1551 int i;
1552
1553 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1554 if (!mbp) {
1555 CSIO_INC_STATS(hw, n_err_nomem);
1556 return -ENOMEM;
1557 }
1558
1559 for (i = 0; i < hw->num_pports; i++) {
1560 portid = hw->pport[i].portid;
1561
1562 /* Read PORT information */
1563 csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid,
1564 false, 0, 0, NULL);
1565
1566 if (csio_mb_issue(hw, mbp)) {
1567 csio_err(hw, "failed to issue FW_PORT_CMD(r) port:%d\n",
1568 portid);
1569 mempool_free(mbp, hw->mb_mempool);
1570 return -EINVAL;
1571 }
1572
1573 csio_mb_process_read_port_rsp(hw, mbp, &retval,
1574 &hw->pport[i].pcap);
1575 if (retval != FW_SUCCESS) {
1576 csio_err(hw, "FW_PORT_CMD(r) port:%d failed: 0x%x\n",
1577 portid, retval);
1578 mempool_free(mbp, hw->mb_mempool);
1579 return -EINVAL;
1580 }
1581
1582 /* Write back PORT information */
1583 csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid, true,
1584 (PAUSE_RX | PAUSE_TX), hw->pport[i].pcap, NULL);
1585
1586 if (csio_mb_issue(hw, mbp)) {
1587 csio_err(hw, "failed to issue FW_PORT_CMD(w) port:%d\n",
1588 portid);
1589 mempool_free(mbp, hw->mb_mempool);
1590 return -EINVAL;
1591 }
1592
1593 retval = csio_mb_fw_retval(mbp);
1594 if (retval != FW_SUCCESS) {
1595 csio_err(hw, "FW_PORT_CMD(w) port:%d failed :0x%x\n",
1596 portid, retval);
1597 mempool_free(mbp, hw->mb_mempool);
1598 return -EINVAL;
1599 }
1600
1601 } /* For all ports */
1602
1603 mempool_free(mbp, hw->mb_mempool);
1604
1605 return 0;
1606}
1607
1608/*
1609 * csio_get_fcoe_resinfo - Read fcoe fw resource info.
1610 * @hw: HW module
1611 * Issued with lock held.
1612 */
1613static int
1614csio_get_fcoe_resinfo(struct csio_hw *hw)
1615{
1616 struct csio_fcoe_res_info *res_info = &hw->fres_info;
1617 struct fw_fcoe_res_info_cmd *rsp;
1618 struct csio_mb *mbp;
1619 enum fw_retval retval;
1620
1621 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1622 if (!mbp) {
1623 CSIO_INC_STATS(hw, n_err_nomem);
1624 return -ENOMEM;
1625 }
1626
1627 /* Get FCoE FW resource information */
1628 csio_fcoe_read_res_info_init_mb(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
1629
1630 if (csio_mb_issue(hw, mbp)) {
1631 csio_err(hw, "failed to issue FW_FCOE_RES_INFO_CMD\n");
1632 mempool_free(mbp, hw->mb_mempool);
1633 return -EINVAL;
1634 }
1635
1636 rsp = (struct fw_fcoe_res_info_cmd *)(mbp->mb);
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301637 retval = FW_CMD_RETVAL_G(ntohl(rsp->retval_len16));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301638 if (retval != FW_SUCCESS) {
1639 csio_err(hw, "FW_FCOE_RES_INFO_CMD failed with ret x%x\n",
1640 retval);
1641 mempool_free(mbp, hw->mb_mempool);
1642 return -EINVAL;
1643 }
1644
1645 res_info->e_d_tov = ntohs(rsp->e_d_tov);
1646 res_info->r_a_tov_seq = ntohs(rsp->r_a_tov_seq);
1647 res_info->r_a_tov_els = ntohs(rsp->r_a_tov_els);
1648 res_info->r_r_tov = ntohs(rsp->r_r_tov);
1649 res_info->max_xchgs = ntohl(rsp->max_xchgs);
1650 res_info->max_ssns = ntohl(rsp->max_ssns);
1651 res_info->used_xchgs = ntohl(rsp->used_xchgs);
1652 res_info->used_ssns = ntohl(rsp->used_ssns);
1653 res_info->max_fcfs = ntohl(rsp->max_fcfs);
1654 res_info->max_vnps = ntohl(rsp->max_vnps);
1655 res_info->used_fcfs = ntohl(rsp->used_fcfs);
1656 res_info->used_vnps = ntohl(rsp->used_vnps);
1657
1658 csio_dbg(hw, "max ssns:%d max xchgs:%d\n", res_info->max_ssns,
1659 res_info->max_xchgs);
1660 mempool_free(mbp, hw->mb_mempool);
1661
1662 return 0;
1663}
1664
1665static int
1666csio_hw_check_fwconfig(struct csio_hw *hw, u32 *param)
1667{
1668 struct csio_mb *mbp;
1669 enum fw_retval retval;
1670 u32 _param[1];
1671
1672 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1673 if (!mbp) {
1674 CSIO_INC_STATS(hw, n_err_nomem);
1675 return -ENOMEM;
1676 }
1677
1678 /*
1679 * Find out whether we're dealing with a version of
1680 * the firmware which has configuration file support.
1681 */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301682 _param[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
1683 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301684
1685 csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
1686 ARRAY_SIZE(_param), _param, NULL, false, NULL);
1687 if (csio_mb_issue(hw, mbp)) {
1688 csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
1689 mempool_free(mbp, hw->mb_mempool);
1690 return -EINVAL;
1691 }
1692
1693 csio_mb_process_read_params_rsp(hw, mbp, &retval,
1694 ARRAY_SIZE(_param), _param);
1695 if (retval != FW_SUCCESS) {
1696 csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
1697 retval);
1698 mempool_free(mbp, hw->mb_mempool);
1699 return -EINVAL;
1700 }
1701
1702 mempool_free(mbp, hw->mb_mempool);
1703 *param = _param[0];
1704
1705 return 0;
1706}
1707
1708static int
1709csio_hw_flash_config(struct csio_hw *hw, u32 *fw_cfg_param, char *path)
1710{
1711 int ret = 0;
1712 const struct firmware *cf;
1713 struct pci_dev *pci_dev = hw->pdev;
1714 struct device *dev = &pci_dev->dev;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301715 unsigned int mtype = 0, maddr = 0;
1716 uint32_t *cfg_data;
1717 int value_to_add = 0;
1718
Arvind Bhushan7cc16382013-03-14 05:09:08 +00001719 if (request_firmware(&cf, CSIO_CF_FNAME(hw), dev) < 0) {
1720 csio_err(hw, "could not find config file %s, err: %d\n",
1721 CSIO_CF_FNAME(hw), ret);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301722 return -ENOENT;
1723 }
1724
1725 if (cf->size%4 != 0)
1726 value_to_add = 4 - (cf->size % 4);
1727
1728 cfg_data = kzalloc(cf->size+value_to_add, GFP_KERNEL);
Jesper Juhl02db3db2012-12-26 21:31:51 +01001729 if (cfg_data == NULL) {
1730 ret = -ENOMEM;
1731 goto leave;
1732 }
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301733
1734 memcpy((void *)cfg_data, (const void *)cf->data, cf->size);
Jesper Juhl02db3db2012-12-26 21:31:51 +01001735 if (csio_hw_check_fwconfig(hw, fw_cfg_param) != 0) {
1736 ret = -EINVAL;
1737 goto leave;
1738 }
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301739
Hariprasad Shenai51678652014-11-21 12:52:02 +05301740 mtype = FW_PARAMS_PARAM_Y_G(*fw_cfg_param);
1741 maddr = FW_PARAMS_PARAM_Z_G(*fw_cfg_param) << 16;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301742
1743 ret = csio_memory_write(hw, mtype, maddr,
1744 cf->size + value_to_add, cfg_data);
Arvind Bhushan7cc16382013-03-14 05:09:08 +00001745
1746 if ((ret == 0) && (value_to_add != 0)) {
1747 union {
1748 u32 word;
1749 char buf[4];
1750 } last;
1751 size_t size = cf->size & ~0x3;
1752 int i;
1753
1754 last.word = cfg_data[size >> 2];
1755 for (i = value_to_add; i < 4; i++)
1756 last.buf[i] = 0;
1757 ret = csio_memory_write(hw, mtype, maddr + size, 4, &last.word);
1758 }
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301759 if (ret == 0) {
Arvind Bhushan7cc16382013-03-14 05:09:08 +00001760 csio_info(hw, "config file upgraded to %s\n",
1761 CSIO_CF_FNAME(hw));
1762 snprintf(path, 64, "%s%s", "/lib/firmware/", CSIO_CF_FNAME(hw));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301763 }
1764
Jesper Juhl02db3db2012-12-26 21:31:51 +01001765leave:
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301766 kfree(cfg_data);
1767 release_firmware(cf);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301768 return ret;
1769}
1770
1771/*
1772 * HW initialization: contact FW, obtain config, perform basic init.
1773 *
1774 * If the firmware we're dealing with has Configuration File support, then
1775 * we use that to perform all configuration -- either using the configuration
1776 * file stored in flash on the adapter or using a filesystem-local file
1777 * if available.
1778 *
1779 * If we don't have configuration file support in the firmware, then we'll
1780 * have to set things up the old fashioned way with hard-coded register
1781 * writes and firmware commands ...
1782 */
1783
1784/*
1785 * Attempt to initialize the HW via a Firmware Configuration File.
1786 */
1787static int
1788csio_hw_use_fwconfig(struct csio_hw *hw, int reset, u32 *fw_cfg_param)
1789{
1790 unsigned int mtype, maddr;
1791 int rv;
Arvind Bhushan7cc16382013-03-14 05:09:08 +00001792 uint32_t finiver = 0, finicsum = 0, cfcsum = 0;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301793 int using_flash;
1794 char path[64];
1795
1796 /*
1797 * Reset device if necessary
1798 */
1799 if (reset) {
1800 rv = csio_do_reset(hw, true);
1801 if (rv != 0)
1802 goto bye;
1803 }
1804
1805 /*
1806 * If we have a configuration file in host ,
1807 * then use that. Otherwise, use the configuration file stored
1808 * in the HW flash ...
1809 */
1810 spin_unlock_irq(&hw->lock);
1811 rv = csio_hw_flash_config(hw, fw_cfg_param, path);
1812 spin_lock_irq(&hw->lock);
1813 if (rv != 0) {
1814 if (rv == -ENOENT) {
1815 /*
1816 * config file was not found. Use default
1817 * config file from flash.
1818 */
1819 mtype = FW_MEMTYPE_CF_FLASH;
Arvind Bhushan7cc16382013-03-14 05:09:08 +00001820 maddr = hw->chip_ops->chip_flash_cfg_addr(hw);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301821 using_flash = 1;
1822 } else {
1823 /*
1824 * we revert back to the hardwired config if
1825 * flashing failed.
1826 */
1827 goto bye;
1828 }
1829 } else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05301830 mtype = FW_PARAMS_PARAM_Y_G(*fw_cfg_param);
1831 maddr = FW_PARAMS_PARAM_Z_G(*fw_cfg_param) << 16;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301832 using_flash = 0;
1833 }
1834
1835 hw->cfg_store = (uint8_t)mtype;
1836
1837 /*
1838 * Issue a Capability Configuration command to the firmware to get it
1839 * to parse the Configuration File.
1840 */
1841 rv = csio_hw_fw_config_file(hw, mtype, maddr, &finiver,
1842 &finicsum, &cfcsum);
1843 if (rv != 0)
1844 goto bye;
1845
1846 hw->cfg_finiver = finiver;
1847 hw->cfg_finicsum = finicsum;
1848 hw->cfg_cfcsum = cfcsum;
1849 hw->cfg_csum_status = true;
1850
1851 if (finicsum != cfcsum) {
1852 csio_warn(hw,
1853 "Config File checksum mismatch: csum=%#x, computed=%#x\n",
1854 finicsum, cfcsum);
1855
1856 hw->cfg_csum_status = false;
1857 }
1858
1859 /*
1860 * Note that we're operating with parameters
1861 * not supplied by the driver, rather than from hard-wired
1862 * initialization constants buried in the driver.
1863 */
1864 hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
1865
1866 /* device parameters */
1867 rv = csio_get_device_params(hw);
1868 if (rv != 0)
1869 goto bye;
1870
1871 /* Configure SGE */
1872 csio_wr_sge_init(hw);
1873
1874 /*
1875 * And finally tell the firmware to initialize itself using the
1876 * parameters from the Configuration File.
1877 */
1878 /* Post event to notify completion of configuration */
1879 csio_post_event(&hw->sm, CSIO_HWE_INIT);
1880
1881 csio_info(hw,
1882 "Firmware Configuration File %s, version %#x, computed checksum %#x\n",
1883 (using_flash ? "in device FLASH" : path), finiver, cfcsum);
1884
1885 return 0;
1886
1887 /*
1888 * Something bad happened. Return the error ...
1889 */
1890bye:
1891 hw->flags &= ~CSIO_HWF_USING_SOFT_PARAMS;
1892 csio_dbg(hw, "Configuration file error %d\n", rv);
1893 return rv;
1894}
1895
1896/*
1897 * Attempt to initialize the adapter via hard-coded, driver supplied
1898 * parameters ...
1899 */
1900static int
1901csio_hw_no_fwconfig(struct csio_hw *hw, int reset)
1902{
1903 int rv;
1904 /*
1905 * Reset device if necessary
1906 */
1907 if (reset) {
1908 rv = csio_do_reset(hw, true);
1909 if (rv != 0)
1910 goto out;
1911 }
1912
1913 /* Get and set device capabilities */
1914 rv = csio_config_device_caps(hw);
1915 if (rv != 0)
1916 goto out;
1917
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301918 /* device parameters */
1919 rv = csio_get_device_params(hw);
1920 if (rv != 0)
1921 goto out;
1922
1923 /* Configure SGE */
1924 csio_wr_sge_init(hw);
1925
1926 /* Post event to notify completion of configuration */
1927 csio_post_event(&hw->sm, CSIO_HWE_INIT);
1928
1929out:
1930 return rv;
1931}
1932
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05301933/* Is the given firmware API compatible with the one the driver was compiled
1934 * with?
1935 */
1936static int fw_compatible(const struct fw_hdr *hdr1, const struct fw_hdr *hdr2)
1937{
1938
1939 /* short circuit if it's the exact same firmware version */
1940 if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
1941 return 1;
1942
1943#define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
1944 if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
1945 SAME_INTF(ri) && SAME_INTF(iscsi) && SAME_INTF(fcoe))
1946 return 1;
1947#undef SAME_INTF
1948
1949 return 0;
1950}
1951
1952/* The firmware in the filesystem is usable, but should it be installed?
1953 * This routine explains itself in detail if it indicates the filesystem
1954 * firmware should be installed.
1955 */
1956static int csio_should_install_fs_fw(struct csio_hw *hw, int card_fw_usable,
1957 int k, int c)
1958{
1959 const char *reason;
1960
1961 if (!card_fw_usable) {
1962 reason = "incompatible or unusable";
1963 goto install;
1964 }
1965
1966 if (k > c) {
1967 reason = "older than the version supported with this driver";
1968 goto install;
1969 }
1970
1971 return 0;
1972
1973install:
1974 csio_err(hw, "firmware on card (%u.%u.%u.%u) is %s, "
1975 "installing firmware %u.%u.%u.%u on card.\n",
1976 FW_HDR_FW_VER_MAJOR_G(c), FW_HDR_FW_VER_MINOR_G(c),
1977 FW_HDR_FW_VER_MICRO_G(c), FW_HDR_FW_VER_BUILD_G(c), reason,
1978 FW_HDR_FW_VER_MAJOR_G(k), FW_HDR_FW_VER_MINOR_G(k),
1979 FW_HDR_FW_VER_MICRO_G(k), FW_HDR_FW_VER_BUILD_G(k));
1980
1981 return 1;
1982}
1983
1984static struct fw_info fw_info_array[] = {
1985 {
1986 .chip = CHELSIO_T5,
1987 .fs_name = FW_CFG_NAME_T5,
1988 .fw_mod_name = FW_FNAME_T5,
1989 .fw_hdr = {
1990 .chip = FW_HDR_CHIP_T5,
1991 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
1992 .intfver_nic = FW_INTFVER(T5, NIC),
1993 .intfver_vnic = FW_INTFVER(T5, VNIC),
1994 .intfver_ri = FW_INTFVER(T5, RI),
1995 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
1996 .intfver_fcoe = FW_INTFVER(T5, FCOE),
1997 },
1998 }
1999};
2000
2001static struct fw_info *find_fw_info(int chip)
2002{
2003 int i;
2004
2005 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
2006 if (fw_info_array[i].chip == chip)
2007 return &fw_info_array[i];
2008 }
2009 return NULL;
2010}
2011
2012int csio_hw_prep_fw(struct csio_hw *hw, struct fw_info *fw_info,
2013 const u8 *fw_data, unsigned int fw_size,
2014 struct fw_hdr *card_fw, enum csio_dev_state state,
2015 int *reset)
2016{
2017 int ret, card_fw_usable, fs_fw_usable;
2018 const struct fw_hdr *fs_fw;
2019 const struct fw_hdr *drv_fw;
2020
2021 drv_fw = &fw_info->fw_hdr;
2022
2023 /* Read the header of the firmware on the card */
2024 ret = csio_hw_read_flash(hw, FLASH_FW_START,
2025 sizeof(*card_fw) / sizeof(uint32_t),
2026 (uint32_t *)card_fw, 1);
2027 if (ret == 0) {
2028 card_fw_usable = fw_compatible(drv_fw, (const void *)card_fw);
2029 } else {
2030 csio_err(hw,
2031 "Unable to read card's firmware header: %d\n", ret);
2032 card_fw_usable = 0;
2033 }
2034
2035 if (fw_data != NULL) {
2036 fs_fw = (const void *)fw_data;
2037 fs_fw_usable = fw_compatible(drv_fw, fs_fw);
2038 } else {
2039 fs_fw = NULL;
2040 fs_fw_usable = 0;
2041 }
2042
2043 if (card_fw_usable && card_fw->fw_ver == drv_fw->fw_ver &&
2044 (!fs_fw_usable || fs_fw->fw_ver == drv_fw->fw_ver)) {
2045 /* Common case: the firmware on the card is an exact match and
2046 * the filesystem one is an exact match too, or the filesystem
2047 * one is absent/incompatible.
2048 */
2049 } else if (fs_fw_usable && state == CSIO_DEV_STATE_UNINIT &&
2050 csio_should_install_fs_fw(hw, card_fw_usable,
2051 be32_to_cpu(fs_fw->fw_ver),
2052 be32_to_cpu(card_fw->fw_ver))) {
2053 ret = csio_hw_fw_upgrade(hw, hw->pfn, fw_data,
2054 fw_size, 0);
2055 if (ret != 0) {
2056 csio_err(hw,
2057 "failed to install firmware: %d\n", ret);
2058 goto bye;
2059 }
2060
2061 /* Installed successfully, update the cached header too. */
2062 memcpy(card_fw, fs_fw, sizeof(*card_fw));
2063 card_fw_usable = 1;
2064 *reset = 0; /* already reset as part of load_fw */
2065 }
2066
2067 if (!card_fw_usable) {
2068 uint32_t d, c, k;
2069
2070 d = be32_to_cpu(drv_fw->fw_ver);
2071 c = be32_to_cpu(card_fw->fw_ver);
2072 k = fs_fw ? be32_to_cpu(fs_fw->fw_ver) : 0;
2073
2074 csio_err(hw, "Cannot find a usable firmware: "
2075 "chip state %d, "
2076 "driver compiled with %d.%d.%d.%d, "
2077 "card has %d.%d.%d.%d, filesystem has %d.%d.%d.%d\n",
2078 state,
2079 FW_HDR_FW_VER_MAJOR_G(d), FW_HDR_FW_VER_MINOR_G(d),
2080 FW_HDR_FW_VER_MICRO_G(d), FW_HDR_FW_VER_BUILD_G(d),
2081 FW_HDR_FW_VER_MAJOR_G(c), FW_HDR_FW_VER_MINOR_G(c),
2082 FW_HDR_FW_VER_MICRO_G(c), FW_HDR_FW_VER_BUILD_G(c),
2083 FW_HDR_FW_VER_MAJOR_G(k), FW_HDR_FW_VER_MINOR_G(k),
2084 FW_HDR_FW_VER_MICRO_G(k), FW_HDR_FW_VER_BUILD_G(k));
2085 ret = EINVAL;
2086 goto bye;
2087 }
2088
2089 /* We're using whatever's on the card and it's known to be good. */
2090 hw->fwrev = be32_to_cpu(card_fw->fw_ver);
2091 hw->tp_vers = be32_to_cpu(card_fw->tp_microcode_ver);
2092
2093bye:
2094 return ret;
2095}
2096
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302097/*
2098 * Returns -EINVAL if attempts to flash the firmware failed
2099 * else returns 0,
2100 * if flashing was not attempted because the card had the
2101 * latest firmware ECANCELED is returned
2102 */
2103static int
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302104csio_hw_flash_fw(struct csio_hw *hw, int *reset)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302105{
2106 int ret = -ECANCELED;
2107 const struct firmware *fw;
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302108 struct fw_info *fw_info;
2109 struct fw_hdr *card_fw;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302110 struct pci_dev *pci_dev = hw->pdev;
2111 struct device *dev = &pci_dev->dev ;
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302112 const u8 *fw_data = NULL;
2113 unsigned int fw_size = 0;
2114
2115 /* This is the firmware whose headers the driver was compiled
2116 * against
2117 */
2118 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(hw->chip_id));
2119 if (fw_info == NULL) {
2120 csio_err(hw,
2121 "unable to get firmware info for chip %d.\n",
2122 CHELSIO_CHIP_VERSION(hw->chip_id));
2123 return -EINVAL;
2124 }
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302125
Arvind Bhushan7cc16382013-03-14 05:09:08 +00002126 if (request_firmware(&fw, CSIO_FW_FNAME(hw), dev) < 0) {
2127 csio_err(hw, "could not find firmware image %s, err: %d\n",
2128 CSIO_FW_FNAME(hw), ret);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302129 return -EINVAL;
2130 }
2131
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302132 /* allocate memory to read the header of the firmware on the
2133 * card
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302134 */
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302135 card_fw = kmalloc(sizeof(*card_fw), GFP_KERNEL);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302136
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302137 fw_data = fw->data;
2138 fw_size = fw->size;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302139
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302140 /* upgrade FW logic */
2141 ret = csio_hw_prep_fw(hw, fw_info, fw_data, fw_size, card_fw,
2142 hw->fw_state, reset);
2143
2144 /* Cleaning up */
2145 if (fw != NULL)
2146 release_firmware(fw);
2147 kfree(card_fw);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302148 return ret;
2149}
2150
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302151/*
2152 * csio_hw_configure - Configure HW
2153 * @hw - HW module
2154 *
2155 */
2156static void
2157csio_hw_configure(struct csio_hw *hw)
2158{
2159 int reset = 1;
2160 int rv;
2161 u32 param[1];
2162
2163 rv = csio_hw_dev_ready(hw);
2164 if (rv != 0) {
2165 CSIO_INC_STATS(hw, n_err_fatal);
2166 csio_post_event(&hw->sm, CSIO_HWE_FATAL);
2167 goto out;
2168 }
2169
2170 /* HW version */
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302171 hw->chip_ver = (char)csio_rd_reg32(hw, PL_REV_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302172
2173 /* Needed for FW download */
2174 rv = csio_hw_get_flash_params(hw);
2175 if (rv != 0) {
2176 csio_err(hw, "Failed to get serial flash params rv:%d\n", rv);
2177 csio_post_event(&hw->sm, CSIO_HWE_FATAL);
2178 goto out;
2179 }
2180
Yijing Wangad4d35f2013-09-05 15:55:26 +08002181 /* Set PCIe completion timeout to 4 seconds */
2182 if (pci_is_pcie(hw->pdev))
2183 pcie_capability_clear_and_set_word(hw->pdev, PCI_EXP_DEVCTL2,
2184 PCI_EXP_DEVCTL2_COMP_TIMEOUT, 0xd);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302185
Arvind Bhushan7cc16382013-03-14 05:09:08 +00002186 hw->chip_ops->chip_set_mem_win(hw, MEMWIN_CSIOSTOR);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302187
2188 rv = csio_hw_get_fw_version(hw, &hw->fwrev);
2189 if (rv != 0)
2190 goto out;
2191
2192 csio_hw_print_fw_version(hw, "Firmware revision");
2193
2194 rv = csio_do_hello(hw, &hw->fw_state);
2195 if (rv != 0) {
2196 CSIO_INC_STATS(hw, n_err_fatal);
2197 csio_post_event(&hw->sm, CSIO_HWE_FATAL);
2198 goto out;
2199 }
2200
2201 /* Read vpd */
2202 rv = csio_hw_get_vpd_params(hw, &hw->vpd);
2203 if (rv != 0)
2204 goto out;
2205
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302206 csio_hw_get_fw_version(hw, &hw->fwrev);
2207 csio_hw_get_tp_version(hw, &hw->tp_vers);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302208 if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302209
2210 /* Do firmware update */
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302211 spin_unlock_irq(&hw->lock);
2212 rv = csio_hw_flash_fw(hw, &reset);
2213 spin_lock_irq(&hw->lock);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302214
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302215 if (rv != 0)
2216 goto out;
2217
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302218 /*
2219 * If the firmware doesn't support Configuration
2220 * Files, use the old Driver-based, hard-wired
2221 * initialization. Otherwise, try using the
2222 * Configuration File support and fall back to the
2223 * Driver-based initialization if there's no
2224 * Configuration File found.
2225 */
2226 if (csio_hw_check_fwconfig(hw, param) == 0) {
2227 rv = csio_hw_use_fwconfig(hw, reset, param);
2228 if (rv == -ENOENT)
2229 goto out;
2230 if (rv != 0) {
2231 csio_info(hw,
2232 "No Configuration File present "
2233 "on adapter. Using hard-wired "
2234 "configuration parameters.\n");
2235 rv = csio_hw_no_fwconfig(hw, reset);
2236 }
2237 } else {
2238 rv = csio_hw_no_fwconfig(hw, reset);
2239 }
2240
2241 if (rv != 0)
2242 goto out;
2243
2244 } else {
2245 if (hw->fw_state == CSIO_DEV_STATE_INIT) {
2246
Arvind Bhushan7cc16382013-03-14 05:09:08 +00002247 hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
2248
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302249 /* device parameters */
2250 rv = csio_get_device_params(hw);
2251 if (rv != 0)
2252 goto out;
2253
2254 /* Get device capabilities */
2255 rv = csio_config_device_caps(hw);
2256 if (rv != 0)
2257 goto out;
2258
2259 /* Configure SGE */
2260 csio_wr_sge_init(hw);
2261
2262 /* Post event to notify completion of configuration */
2263 csio_post_event(&hw->sm, CSIO_HWE_INIT);
2264 goto out;
2265 }
2266 } /* if not master */
2267
2268out:
2269 return;
2270}
2271
2272/*
2273 * csio_hw_initialize - Initialize HW
2274 * @hw - HW module
2275 *
2276 */
2277static void
2278csio_hw_initialize(struct csio_hw *hw)
2279{
2280 struct csio_mb *mbp;
2281 enum fw_retval retval;
2282 int rv;
2283 int i;
2284
2285 if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
2286 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
2287 if (!mbp)
2288 goto out;
2289
2290 csio_mb_initialize(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
2291
2292 if (csio_mb_issue(hw, mbp)) {
2293 csio_err(hw, "Issue of FW_INITIALIZE_CMD failed!\n");
2294 goto free_and_out;
2295 }
2296
2297 retval = csio_mb_fw_retval(mbp);
2298 if (retval != FW_SUCCESS) {
2299 csio_err(hw, "FW_INITIALIZE_CMD returned 0x%x!\n",
2300 retval);
2301 goto free_and_out;
2302 }
2303
2304 mempool_free(mbp, hw->mb_mempool);
2305 }
2306
2307 rv = csio_get_fcoe_resinfo(hw);
2308 if (rv != 0) {
2309 csio_err(hw, "Failed to read fcoe resource info: %d\n", rv);
2310 goto out;
2311 }
2312
2313 spin_unlock_irq(&hw->lock);
2314 rv = csio_config_queues(hw);
2315 spin_lock_irq(&hw->lock);
2316
2317 if (rv != 0) {
2318 csio_err(hw, "Config of queues failed!: %d\n", rv);
2319 goto out;
2320 }
2321
2322 for (i = 0; i < hw->num_pports; i++)
2323 hw->pport[i].mod_type = FW_PORT_MOD_TYPE_NA;
2324
2325 if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
2326 rv = csio_enable_ports(hw);
2327 if (rv != 0) {
2328 csio_err(hw, "Failed to enable ports: %d\n", rv);
2329 goto out;
2330 }
2331 }
2332
2333 csio_post_event(&hw->sm, CSIO_HWE_INIT_DONE);
2334 return;
2335
2336free_and_out:
2337 mempool_free(mbp, hw->mb_mempool);
2338out:
2339 return;
2340}
2341
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302342#define PF_INTR_MASK (PFSW_F | PFCIM_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302343
2344/*
2345 * csio_hw_intr_enable - Enable HW interrupts
2346 * @hw: Pointer to HW module.
2347 *
2348 * Enable interrupts in HW registers.
2349 */
2350static void
2351csio_hw_intr_enable(struct csio_hw *hw)
2352{
2353 uint16_t vec = (uint16_t)csio_get_mb_intr_idx(csio_hw_to_mbm(hw));
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302354 uint32_t pf = SOURCEPF_G(csio_rd_reg32(hw, PL_WHOAMI_A));
2355 uint32_t pl = csio_rd_reg32(hw, PL_INT_ENABLE_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302356
2357 /*
2358 * Set aivec for MSI/MSIX. PCIE_PF_CFG.INTXType is set up
2359 * by FW, so do nothing for INTX.
2360 */
2361 if (hw->intr_mode == CSIO_IM_MSIX)
Hariprasad Shenaif061de42015-01-05 16:30:44 +05302362 csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG_A),
2363 AIVEC_V(AIVEC_M), vec);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302364 else if (hw->intr_mode == CSIO_IM_MSI)
Hariprasad Shenaif061de42015-01-05 16:30:44 +05302365 csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG_A),
2366 AIVEC_V(AIVEC_M), 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302367
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302368 csio_wr_reg32(hw, PF_INTR_MASK, MYPF_REG(PL_PF_INT_ENABLE_A));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302369
2370 /* Turn on MB interrupts - this will internally flush PIO as well */
2371 csio_mb_intr_enable(hw);
2372
2373 /* These are common registers - only a master can modify them */
2374 if (csio_is_hw_master(hw)) {
2375 /*
2376 * Disable the Serial FLASH interrupt, if enabled!
2377 */
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302378 pl &= (~SF_F);
2379 csio_wr_reg32(hw, pl, PL_INT_ENABLE_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302380
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302381 csio_wr_reg32(hw, ERR_CPL_EXCEED_IQE_SIZE_F |
2382 EGRESS_SIZE_ERR_F | ERR_INVALID_CIDX_INC_F |
2383 ERR_CPL_OPCODE_0_F | ERR_DROPPED_DB_F |
2384 ERR_DATA_CPL_ON_HIGH_QID1_F |
2385 ERR_DATA_CPL_ON_HIGH_QID0_F | ERR_BAD_DB_PIDX3_F |
2386 ERR_BAD_DB_PIDX2_F | ERR_BAD_DB_PIDX1_F |
2387 ERR_BAD_DB_PIDX0_F | ERR_ING_CTXT_PRIO_F |
2388 ERR_EGR_CTXT_PRIO_F | INGRESS_SIZE_ERR_F,
2389 SGE_INT_ENABLE3_A);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302390 csio_set_reg_field(hw, PL_INT_MAP0_A, 0, 1 << pf);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302391 }
2392
2393 hw->flags |= CSIO_HWF_HW_INTR_ENABLED;
2394
2395}
2396
2397/*
2398 * csio_hw_intr_disable - Disable HW interrupts
2399 * @hw: Pointer to HW module.
2400 *
2401 * Turn off Mailbox and PCI_PF_CFG interrupts.
2402 */
2403void
2404csio_hw_intr_disable(struct csio_hw *hw)
2405{
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302406 uint32_t pf = SOURCEPF_G(csio_rd_reg32(hw, PL_WHOAMI_A));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302407
2408 if (!(hw->flags & CSIO_HWF_HW_INTR_ENABLED))
2409 return;
2410
2411 hw->flags &= ~CSIO_HWF_HW_INTR_ENABLED;
2412
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302413 csio_wr_reg32(hw, 0, MYPF_REG(PL_PF_INT_ENABLE_A));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302414 if (csio_is_hw_master(hw))
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302415 csio_set_reg_field(hw, PL_INT_MAP0_A, 1 << pf, 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302416
2417 /* Turn off MB interrupts */
2418 csio_mb_intr_disable(hw);
2419
2420}
2421
Arvind Bhushan7cc16382013-03-14 05:09:08 +00002422void
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302423csio_hw_fatal_err(struct csio_hw *hw)
2424{
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302425 csio_set_reg_field(hw, SGE_CONTROL_A, GLOBALENABLE_F, 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302426 csio_hw_intr_disable(hw);
2427
2428 /* Do not reset HW, we may need FW state for debugging */
2429 csio_fatal(hw, "HW Fatal error encountered!\n");
2430}
2431
2432/*****************************************************************************/
2433/* START: HW SM */
2434/*****************************************************************************/
2435/*
2436 * csio_hws_uninit - Uninit state
2437 * @hw - HW module
2438 * @evt - Event
2439 *
2440 */
2441static void
2442csio_hws_uninit(struct csio_hw *hw, enum csio_hw_ev evt)
2443{
2444 hw->prev_evt = hw->cur_evt;
2445 hw->cur_evt = evt;
2446 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2447
2448 switch (evt) {
2449 case CSIO_HWE_CFG:
2450 csio_set_state(&hw->sm, csio_hws_configuring);
2451 csio_hw_configure(hw);
2452 break;
2453
2454 default:
2455 CSIO_INC_STATS(hw, n_evt_unexp);
2456 break;
2457 }
2458}
2459
2460/*
2461 * csio_hws_configuring - Configuring state
2462 * @hw - HW module
2463 * @evt - Event
2464 *
2465 */
2466static void
2467csio_hws_configuring(struct csio_hw *hw, enum csio_hw_ev evt)
2468{
2469 hw->prev_evt = hw->cur_evt;
2470 hw->cur_evt = evt;
2471 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2472
2473 switch (evt) {
2474 case CSIO_HWE_INIT:
2475 csio_set_state(&hw->sm, csio_hws_initializing);
2476 csio_hw_initialize(hw);
2477 break;
2478
2479 case CSIO_HWE_INIT_DONE:
2480 csio_set_state(&hw->sm, csio_hws_ready);
2481 /* Fan out event to all lnode SMs */
2482 csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
2483 break;
2484
2485 case CSIO_HWE_FATAL:
2486 csio_set_state(&hw->sm, csio_hws_uninit);
2487 break;
2488
2489 case CSIO_HWE_PCI_REMOVE:
2490 csio_do_bye(hw);
2491 break;
2492 default:
2493 CSIO_INC_STATS(hw, n_evt_unexp);
2494 break;
2495 }
2496}
2497
2498/*
2499 * csio_hws_initializing - Initialiazing state
2500 * @hw - HW module
2501 * @evt - Event
2502 *
2503 */
2504static void
2505csio_hws_initializing(struct csio_hw *hw, enum csio_hw_ev evt)
2506{
2507 hw->prev_evt = hw->cur_evt;
2508 hw->cur_evt = evt;
2509 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2510
2511 switch (evt) {
2512 case CSIO_HWE_INIT_DONE:
2513 csio_set_state(&hw->sm, csio_hws_ready);
2514
2515 /* Fan out event to all lnode SMs */
2516 csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
2517
2518 /* Enable interrupts */
2519 csio_hw_intr_enable(hw);
2520 break;
2521
2522 case CSIO_HWE_FATAL:
2523 csio_set_state(&hw->sm, csio_hws_uninit);
2524 break;
2525
2526 case CSIO_HWE_PCI_REMOVE:
2527 csio_do_bye(hw);
2528 break;
2529
2530 default:
2531 CSIO_INC_STATS(hw, n_evt_unexp);
2532 break;
2533 }
2534}
2535
2536/*
2537 * csio_hws_ready - Ready state
2538 * @hw - HW module
2539 * @evt - Event
2540 *
2541 */
2542static void
2543csio_hws_ready(struct csio_hw *hw, enum csio_hw_ev evt)
2544{
2545 /* Remember the event */
2546 hw->evtflag = evt;
2547
2548 hw->prev_evt = hw->cur_evt;
2549 hw->cur_evt = evt;
2550 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2551
2552 switch (evt) {
2553 case CSIO_HWE_HBA_RESET:
2554 case CSIO_HWE_FW_DLOAD:
2555 case CSIO_HWE_SUSPEND:
2556 case CSIO_HWE_PCI_REMOVE:
2557 case CSIO_HWE_PCIERR_DETECTED:
2558 csio_set_state(&hw->sm, csio_hws_quiescing);
2559 /* cleanup all outstanding cmds */
2560 if (evt == CSIO_HWE_HBA_RESET ||
2561 evt == CSIO_HWE_PCIERR_DETECTED)
2562 csio_scsim_cleanup_io(csio_hw_to_scsim(hw), false);
2563 else
2564 csio_scsim_cleanup_io(csio_hw_to_scsim(hw), true);
2565
2566 csio_hw_intr_disable(hw);
2567 csio_hw_mbm_cleanup(hw);
2568 csio_evtq_stop(hw);
2569 csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWSTOP);
2570 csio_evtq_flush(hw);
2571 csio_mgmtm_cleanup(csio_hw_to_mgmtm(hw));
2572 csio_post_event(&hw->sm, CSIO_HWE_QUIESCED);
2573 break;
2574
2575 case CSIO_HWE_FATAL:
2576 csio_set_state(&hw->sm, csio_hws_uninit);
2577 break;
2578
2579 default:
2580 CSIO_INC_STATS(hw, n_evt_unexp);
2581 break;
2582 }
2583}
2584
2585/*
2586 * csio_hws_quiescing - Quiescing state
2587 * @hw - HW module
2588 * @evt - Event
2589 *
2590 */
2591static void
2592csio_hws_quiescing(struct csio_hw *hw, enum csio_hw_ev evt)
2593{
2594 hw->prev_evt = hw->cur_evt;
2595 hw->cur_evt = evt;
2596 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2597
2598 switch (evt) {
2599 case CSIO_HWE_QUIESCED:
2600 switch (hw->evtflag) {
2601 case CSIO_HWE_FW_DLOAD:
2602 csio_set_state(&hw->sm, csio_hws_resetting);
2603 /* Download firmware */
2604 /* Fall through */
2605
2606 case CSIO_HWE_HBA_RESET:
2607 csio_set_state(&hw->sm, csio_hws_resetting);
2608 /* Start reset of the HBA */
2609 csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWRESET);
2610 csio_wr_destroy_queues(hw, false);
2611 csio_do_reset(hw, false);
2612 csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET_DONE);
2613 break;
2614
2615 case CSIO_HWE_PCI_REMOVE:
2616 csio_set_state(&hw->sm, csio_hws_removing);
2617 csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREMOVE);
2618 csio_wr_destroy_queues(hw, true);
2619 /* Now send the bye command */
2620 csio_do_bye(hw);
2621 break;
2622
2623 case CSIO_HWE_SUSPEND:
2624 csio_set_state(&hw->sm, csio_hws_quiesced);
2625 break;
2626
2627 case CSIO_HWE_PCIERR_DETECTED:
2628 csio_set_state(&hw->sm, csio_hws_pcierr);
2629 csio_wr_destroy_queues(hw, false);
2630 break;
2631
2632 default:
2633 CSIO_INC_STATS(hw, n_evt_unexp);
2634 break;
2635
2636 }
2637 break;
2638
2639 default:
2640 CSIO_INC_STATS(hw, n_evt_unexp);
2641 break;
2642 }
2643}
2644
2645/*
2646 * csio_hws_quiesced - Quiesced state
2647 * @hw - HW module
2648 * @evt - Event
2649 *
2650 */
2651static void
2652csio_hws_quiesced(struct csio_hw *hw, enum csio_hw_ev evt)
2653{
2654 hw->prev_evt = hw->cur_evt;
2655 hw->cur_evt = evt;
2656 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2657
2658 switch (evt) {
2659 case CSIO_HWE_RESUME:
2660 csio_set_state(&hw->sm, csio_hws_configuring);
2661 csio_hw_configure(hw);
2662 break;
2663
2664 default:
2665 CSIO_INC_STATS(hw, n_evt_unexp);
2666 break;
2667 }
2668}
2669
2670/*
2671 * csio_hws_resetting - HW Resetting state
2672 * @hw - HW module
2673 * @evt - Event
2674 *
2675 */
2676static void
2677csio_hws_resetting(struct csio_hw *hw, enum csio_hw_ev evt)
2678{
2679 hw->prev_evt = hw->cur_evt;
2680 hw->cur_evt = evt;
2681 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2682
2683 switch (evt) {
2684 case CSIO_HWE_HBA_RESET_DONE:
2685 csio_evtq_start(hw);
2686 csio_set_state(&hw->sm, csio_hws_configuring);
2687 csio_hw_configure(hw);
2688 break;
2689
2690 default:
2691 CSIO_INC_STATS(hw, n_evt_unexp);
2692 break;
2693 }
2694}
2695
2696/*
2697 * csio_hws_removing - PCI Hotplug removing state
2698 * @hw - HW module
2699 * @evt - Event
2700 *
2701 */
2702static void
2703csio_hws_removing(struct csio_hw *hw, enum csio_hw_ev evt)
2704{
2705 hw->prev_evt = hw->cur_evt;
2706 hw->cur_evt = evt;
2707 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2708
2709 switch (evt) {
2710 case CSIO_HWE_HBA_RESET:
2711 if (!csio_is_hw_master(hw))
2712 break;
2713 /*
2714 * The BYE should have alerady been issued, so we cant
2715 * use the mailbox interface. Hence we use the PL_RST
2716 * register directly.
2717 */
2718 csio_err(hw, "Resetting HW and waiting 2 seconds...\n");
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302719 csio_wr_reg32(hw, PIORSTMODE_F | PIORST_F, PL_RST_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302720 mdelay(2000);
2721 break;
2722
2723 /* Should never receive any new events */
2724 default:
2725 CSIO_INC_STATS(hw, n_evt_unexp);
2726 break;
2727
2728 }
2729}
2730
2731/*
2732 * csio_hws_pcierr - PCI Error state
2733 * @hw - HW module
2734 * @evt - Event
2735 *
2736 */
2737static void
2738csio_hws_pcierr(struct csio_hw *hw, enum csio_hw_ev evt)
2739{
2740 hw->prev_evt = hw->cur_evt;
2741 hw->cur_evt = evt;
2742 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2743
2744 switch (evt) {
2745 case CSIO_HWE_PCIERR_SLOT_RESET:
2746 csio_evtq_start(hw);
2747 csio_set_state(&hw->sm, csio_hws_configuring);
2748 csio_hw_configure(hw);
2749 break;
2750
2751 default:
2752 CSIO_INC_STATS(hw, n_evt_unexp);
2753 break;
2754 }
2755}
2756
2757/*****************************************************************************/
2758/* END: HW SM */
2759/*****************************************************************************/
2760
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302761/*
2762 * csio_handle_intr_status - table driven interrupt handler
2763 * @hw: HW instance
2764 * @reg: the interrupt status register to process
2765 * @acts: table of interrupt actions
2766 *
2767 * A table driven interrupt handler that applies a set of masks to an
2768 * interrupt status word and performs the corresponding actions if the
2769 * interrupts described by the mask have occured. The actions include
2770 * optionally emitting a warning or alert message. The table is terminated
2771 * by an entry specifying mask 0. Returns the number of fatal interrupt
2772 * conditions.
2773 */
Arvind Bhushan7cc16382013-03-14 05:09:08 +00002774int
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302775csio_handle_intr_status(struct csio_hw *hw, unsigned int reg,
2776 const struct intr_info *acts)
2777{
2778 int fatal = 0;
2779 unsigned int mask = 0;
2780 unsigned int status = csio_rd_reg32(hw, reg);
2781
2782 for ( ; acts->mask; ++acts) {
2783 if (!(status & acts->mask))
2784 continue;
2785 if (acts->fatal) {
2786 fatal++;
2787 csio_fatal(hw, "Fatal %s (0x%x)\n",
2788 acts->msg, status & acts->mask);
2789 } else if (acts->msg)
2790 csio_info(hw, "%s (0x%x)\n",
2791 acts->msg, status & acts->mask);
2792 mask |= acts->mask;
2793 }
2794 status &= mask;
2795 if (status) /* clear processed interrupts */
2796 csio_wr_reg32(hw, status, reg);
2797 return fatal;
2798}
2799
2800/*
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302801 * TP interrupt handler.
2802 */
2803static void csio_tp_intr_handler(struct csio_hw *hw)
2804{
2805 static struct intr_info tp_intr_info[] = {
2806 { 0x3fffffff, "TP parity error", -1, 1 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302807 { FLMTXFLSTEMPTY_F, "TP out of Tx pages", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302808 { 0, NULL, 0, 0 }
2809 };
2810
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302811 if (csio_handle_intr_status(hw, TP_INT_CAUSE_A, tp_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302812 csio_hw_fatal_err(hw);
2813}
2814
2815/*
2816 * SGE interrupt handler.
2817 */
2818static void csio_sge_intr_handler(struct csio_hw *hw)
2819{
2820 uint64_t v;
2821
2822 static struct intr_info sge_intr_info[] = {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302823 { ERR_CPL_EXCEED_IQE_SIZE_F,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302824 "SGE received CPL exceeding IQE size", -1, 1 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302825 { ERR_INVALID_CIDX_INC_F,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302826 "SGE GTS CIDX increment too large", -1, 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302827 { ERR_CPL_OPCODE_0_F, "SGE received 0-length CPL", -1, 0 },
2828 { ERR_DROPPED_DB_F, "SGE doorbell dropped", -1, 0 },
2829 { ERR_DATA_CPL_ON_HIGH_QID1_F | ERR_DATA_CPL_ON_HIGH_QID0_F,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302830 "SGE IQID > 1023 received CPL for FL", -1, 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302831 { ERR_BAD_DB_PIDX3_F, "SGE DBP 3 pidx increment too large", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302832 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302833 { ERR_BAD_DB_PIDX2_F, "SGE DBP 2 pidx increment too large", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302834 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302835 { ERR_BAD_DB_PIDX1_F, "SGE DBP 1 pidx increment too large", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302836 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302837 { ERR_BAD_DB_PIDX0_F, "SGE DBP 0 pidx increment too large", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302838 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302839 { ERR_ING_CTXT_PRIO_F,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302840 "SGE too many priority ingress contexts", -1, 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302841 { ERR_EGR_CTXT_PRIO_F,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302842 "SGE too many priority egress contexts", -1, 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302843 { INGRESS_SIZE_ERR_F, "SGE illegal ingress QID", -1, 0 },
2844 { EGRESS_SIZE_ERR_F, "SGE illegal egress QID", -1, 0 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302845 { 0, NULL, 0, 0 }
2846 };
2847
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302848 v = (uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE1_A) |
2849 ((uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE2_A) << 32);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302850 if (v) {
2851 csio_fatal(hw, "SGE parity error (%#llx)\n",
2852 (unsigned long long)v);
2853 csio_wr_reg32(hw, (uint32_t)(v & 0xFFFFFFFF),
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302854 SGE_INT_CAUSE1_A);
2855 csio_wr_reg32(hw, (uint32_t)(v >> 32), SGE_INT_CAUSE2_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302856 }
2857
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302858 v |= csio_handle_intr_status(hw, SGE_INT_CAUSE3_A, sge_intr_info);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302859
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302860 if (csio_handle_intr_status(hw, SGE_INT_CAUSE3_A, sge_intr_info) ||
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302861 v != 0)
2862 csio_hw_fatal_err(hw);
2863}
2864
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05302865#define CIM_OBQ_INTR (OBQULP0PARERR_F | OBQULP1PARERR_F | OBQULP2PARERR_F |\
2866 OBQULP3PARERR_F | OBQSGEPARERR_F | OBQNCSIPARERR_F)
2867#define CIM_IBQ_INTR (IBQTP0PARERR_F | IBQTP1PARERR_F | IBQULPPARERR_F |\
2868 IBQSGEHIPARERR_F | IBQSGELOPARERR_F | IBQNCSIPARERR_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302869
2870/*
2871 * CIM interrupt handler.
2872 */
2873static void csio_cim_intr_handler(struct csio_hw *hw)
2874{
2875 static struct intr_info cim_intr_info[] = {
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05302876 { PREFDROPINT_F, "CIM control register prefetch drop", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302877 { CIM_OBQ_INTR, "CIM OBQ parity error", -1, 1 },
2878 { CIM_IBQ_INTR, "CIM IBQ parity error", -1, 1 },
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05302879 { MBUPPARERR_F, "CIM mailbox uP parity error", -1, 1 },
2880 { MBHOSTPARERR_F, "CIM mailbox host parity error", -1, 1 },
2881 { TIEQINPARERRINT_F, "CIM TIEQ outgoing parity error", -1, 1 },
2882 { TIEQOUTPARERRINT_F, "CIM TIEQ incoming parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302883 { 0, NULL, 0, 0 }
2884 };
2885 static struct intr_info cim_upintr_info[] = {
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05302886 { RSVDSPACEINT_F, "CIM reserved space access", -1, 1 },
2887 { ILLTRANSINT_F, "CIM illegal transaction", -1, 1 },
2888 { ILLWRINT_F, "CIM illegal write", -1, 1 },
2889 { ILLRDINT_F, "CIM illegal read", -1, 1 },
2890 { ILLRDBEINT_F, "CIM illegal read BE", -1, 1 },
2891 { ILLWRBEINT_F, "CIM illegal write BE", -1, 1 },
2892 { SGLRDBOOTINT_F, "CIM single read from boot space", -1, 1 },
2893 { SGLWRBOOTINT_F, "CIM single write to boot space", -1, 1 },
2894 { BLKWRBOOTINT_F, "CIM block write to boot space", -1, 1 },
2895 { SGLRDFLASHINT_F, "CIM single read from flash space", -1, 1 },
2896 { SGLWRFLASHINT_F, "CIM single write to flash space", -1, 1 },
2897 { BLKWRFLASHINT_F, "CIM block write to flash space", -1, 1 },
2898 { SGLRDEEPROMINT_F, "CIM single EEPROM read", -1, 1 },
2899 { SGLWREEPROMINT_F, "CIM single EEPROM write", -1, 1 },
2900 { BLKRDEEPROMINT_F, "CIM block EEPROM read", -1, 1 },
2901 { BLKWREEPROMINT_F, "CIM block EEPROM write", -1, 1 },
2902 { SGLRDCTLINT_F, "CIM single read from CTL space", -1, 1 },
2903 { SGLWRCTLINT_F, "CIM single write to CTL space", -1, 1 },
2904 { BLKRDCTLINT_F, "CIM block read from CTL space", -1, 1 },
2905 { BLKWRCTLINT_F, "CIM block write to CTL space", -1, 1 },
2906 { SGLRDPLINT_F, "CIM single read from PL space", -1, 1 },
2907 { SGLWRPLINT_F, "CIM single write to PL space", -1, 1 },
2908 { BLKRDPLINT_F, "CIM block read from PL space", -1, 1 },
2909 { BLKWRPLINT_F, "CIM block write to PL space", -1, 1 },
2910 { REQOVRLOOKUPINT_F, "CIM request FIFO overwrite", -1, 1 },
2911 { RSPOVRLOOKUPINT_F, "CIM response FIFO overwrite", -1, 1 },
2912 { TIMEOUTINT_F, "CIM PIF timeout", -1, 1 },
2913 { TIMEOUTMAINT_F, "CIM PIF MA timeout", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302914 { 0, NULL, 0, 0 }
2915 };
2916
2917 int fat;
2918
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05302919 fat = csio_handle_intr_status(hw, CIM_HOST_INT_CAUSE_A,
2920 cim_intr_info) +
2921 csio_handle_intr_status(hw, CIM_HOST_UPACC_INT_CAUSE_A,
2922 cim_upintr_info);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302923 if (fat)
2924 csio_hw_fatal_err(hw);
2925}
2926
2927/*
2928 * ULP RX interrupt handler.
2929 */
2930static void csio_ulprx_intr_handler(struct csio_hw *hw)
2931{
2932 static struct intr_info ulprx_intr_info[] = {
2933 { 0x1800000, "ULPRX context error", -1, 1 },
2934 { 0x7fffff, "ULPRX parity error", -1, 1 },
2935 { 0, NULL, 0, 0 }
2936 };
2937
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302938 if (csio_handle_intr_status(hw, ULP_RX_INT_CAUSE_A, ulprx_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302939 csio_hw_fatal_err(hw);
2940}
2941
2942/*
2943 * ULP TX interrupt handler.
2944 */
2945static void csio_ulptx_intr_handler(struct csio_hw *hw)
2946{
2947 static struct intr_info ulptx_intr_info[] = {
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302948 { PBL_BOUND_ERR_CH3_F, "ULPTX channel 3 PBL out of bounds", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302949 0 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302950 { PBL_BOUND_ERR_CH2_F, "ULPTX channel 2 PBL out of bounds", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302951 0 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302952 { PBL_BOUND_ERR_CH1_F, "ULPTX channel 1 PBL out of bounds", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302953 0 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302954 { PBL_BOUND_ERR_CH0_F, "ULPTX channel 0 PBL out of bounds", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302955 0 },
2956 { 0xfffffff, "ULPTX parity error", -1, 1 },
2957 { 0, NULL, 0, 0 }
2958 };
2959
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302960 if (csio_handle_intr_status(hw, ULP_TX_INT_CAUSE_A, ulptx_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302961 csio_hw_fatal_err(hw);
2962}
2963
2964/*
2965 * PM TX interrupt handler.
2966 */
2967static void csio_pmtx_intr_handler(struct csio_hw *hw)
2968{
2969 static struct intr_info pmtx_intr_info[] = {
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302970 { PCMD_LEN_OVFL0_F, "PMTX channel 0 pcmd too large", -1, 1 },
2971 { PCMD_LEN_OVFL1_F, "PMTX channel 1 pcmd too large", -1, 1 },
2972 { PCMD_LEN_OVFL2_F, "PMTX channel 2 pcmd too large", -1, 1 },
2973 { ZERO_C_CMD_ERROR_F, "PMTX 0-length pcmd", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302974 { 0xffffff0, "PMTX framing error", -1, 1 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302975 { OESPI_PAR_ERROR_F, "PMTX oespi parity error", -1, 1 },
2976 { DB_OPTIONS_PAR_ERROR_F, "PMTX db_options parity error", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302977 1 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302978 { ICSPI_PAR_ERROR_F, "PMTX icspi parity error", -1, 1 },
2979 { PMTX_C_PCMD_PAR_ERROR_F, "PMTX c_pcmd parity error", -1, 1},
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302980 { 0, NULL, 0, 0 }
2981 };
2982
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302983 if (csio_handle_intr_status(hw, PM_TX_INT_CAUSE_A, pmtx_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302984 csio_hw_fatal_err(hw);
2985}
2986
2987/*
2988 * PM RX interrupt handler.
2989 */
2990static void csio_pmrx_intr_handler(struct csio_hw *hw)
2991{
2992 static struct intr_info pmrx_intr_info[] = {
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302993 { ZERO_E_CMD_ERROR_F, "PMRX 0-length pcmd", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302994 { 0x3ffff0, "PMRX framing error", -1, 1 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302995 { OCSPI_PAR_ERROR_F, "PMRX ocspi parity error", -1, 1 },
2996 { DB_OPTIONS_PAR_ERROR_F, "PMRX db_options parity error", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302997 1 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302998 { IESPI_PAR_ERROR_F, "PMRX iespi parity error", -1, 1 },
2999 { PMRX_E_PCMD_PAR_ERROR_F, "PMRX e_pcmd parity error", -1, 1},
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303000 { 0, NULL, 0, 0 }
3001 };
3002
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303003 if (csio_handle_intr_status(hw, PM_RX_INT_CAUSE_A, pmrx_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303004 csio_hw_fatal_err(hw);
3005}
3006
3007/*
3008 * CPL switch interrupt handler.
3009 */
3010static void csio_cplsw_intr_handler(struct csio_hw *hw)
3011{
3012 static struct intr_info cplsw_intr_info[] = {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303013 { CIM_OP_MAP_PERR_F, "CPLSW CIM op_map parity error", -1, 1 },
3014 { CIM_OVFL_ERROR_F, "CPLSW CIM overflow", -1, 1 },
3015 { TP_FRAMING_ERROR_F, "CPLSW TP framing error", -1, 1 },
3016 { SGE_FRAMING_ERROR_F, "CPLSW SGE framing error", -1, 1 },
3017 { CIM_FRAMING_ERROR_F, "CPLSW CIM framing error", -1, 1 },
3018 { ZERO_SWITCH_ERROR_F, "CPLSW no-switch error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303019 { 0, NULL, 0, 0 }
3020 };
3021
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303022 if (csio_handle_intr_status(hw, CPL_INTR_CAUSE_A, cplsw_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303023 csio_hw_fatal_err(hw);
3024}
3025
3026/*
3027 * LE interrupt handler.
3028 */
3029static void csio_le_intr_handler(struct csio_hw *hw)
3030{
3031 static struct intr_info le_intr_info[] = {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303032 { LIPMISS_F, "LE LIP miss", -1, 0 },
3033 { LIP0_F, "LE 0 LIP error", -1, 0 },
3034 { PARITYERR_F, "LE parity error", -1, 1 },
3035 { UNKNOWNCMD_F, "LE unknown command", -1, 1 },
3036 { REQQPARERR_F, "LE request queue parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303037 { 0, NULL, 0, 0 }
3038 };
3039
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303040 if (csio_handle_intr_status(hw, LE_DB_INT_CAUSE_A, le_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303041 csio_hw_fatal_err(hw);
3042}
3043
3044/*
3045 * MPS interrupt handler.
3046 */
3047static void csio_mps_intr_handler(struct csio_hw *hw)
3048{
3049 static struct intr_info mps_rx_intr_info[] = {
3050 { 0xffffff, "MPS Rx parity error", -1, 1 },
3051 { 0, NULL, 0, 0 }
3052 };
3053 static struct intr_info mps_tx_intr_info[] = {
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303054 { TPFIFO_V(TPFIFO_M), "MPS Tx TP FIFO parity error", -1, 1 },
3055 { NCSIFIFO_F, "MPS Tx NC-SI FIFO parity error", -1, 1 },
3056 { TXDATAFIFO_V(TXDATAFIFO_M), "MPS Tx data FIFO parity error",
3057 -1, 1 },
3058 { TXDESCFIFO_V(TXDESCFIFO_M), "MPS Tx desc FIFO parity error",
3059 -1, 1 },
3060 { BUBBLE_F, "MPS Tx underflow", -1, 1 },
3061 { SECNTERR_F, "MPS Tx SOP/EOP error", -1, 1 },
3062 { FRMERR_F, "MPS Tx framing error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303063 { 0, NULL, 0, 0 }
3064 };
3065 static struct intr_info mps_trc_intr_info[] = {
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303066 { FILTMEM_V(FILTMEM_M), "MPS TRC filter parity error", -1, 1 },
3067 { PKTFIFO_V(PKTFIFO_M), "MPS TRC packet FIFO parity error",
3068 -1, 1 },
3069 { MISCPERR_F, "MPS TRC misc parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303070 { 0, NULL, 0, 0 }
3071 };
3072 static struct intr_info mps_stat_sram_intr_info[] = {
3073 { 0x1fffff, "MPS statistics SRAM parity error", -1, 1 },
3074 { 0, NULL, 0, 0 }
3075 };
3076 static struct intr_info mps_stat_tx_intr_info[] = {
3077 { 0xfffff, "MPS statistics Tx FIFO parity error", -1, 1 },
3078 { 0, NULL, 0, 0 }
3079 };
3080 static struct intr_info mps_stat_rx_intr_info[] = {
3081 { 0xffffff, "MPS statistics Rx FIFO parity error", -1, 1 },
3082 { 0, NULL, 0, 0 }
3083 };
3084 static struct intr_info mps_cls_intr_info[] = {
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303085 { MATCHSRAM_F, "MPS match SRAM parity error", -1, 1 },
3086 { MATCHTCAM_F, "MPS match TCAM parity error", -1, 1 },
3087 { HASHSRAM_F, "MPS hash SRAM parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303088 { 0, NULL, 0, 0 }
3089 };
3090
3091 int fat;
3092
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303093 fat = csio_handle_intr_status(hw, MPS_RX_PERR_INT_CAUSE_A,
3094 mps_rx_intr_info) +
3095 csio_handle_intr_status(hw, MPS_TX_INT_CAUSE_A,
3096 mps_tx_intr_info) +
3097 csio_handle_intr_status(hw, MPS_TRC_INT_CAUSE_A,
3098 mps_trc_intr_info) +
3099 csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_SRAM_A,
3100 mps_stat_sram_intr_info) +
3101 csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_TX_FIFO_A,
3102 mps_stat_tx_intr_info) +
3103 csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_RX_FIFO_A,
3104 mps_stat_rx_intr_info) +
3105 csio_handle_intr_status(hw, MPS_CLS_INT_CAUSE_A,
3106 mps_cls_intr_info);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303107
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303108 csio_wr_reg32(hw, 0, MPS_INT_CAUSE_A);
3109 csio_rd_reg32(hw, MPS_INT_CAUSE_A); /* flush */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303110 if (fat)
3111 csio_hw_fatal_err(hw);
3112}
3113
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303114#define MEM_INT_MASK (PERR_INT_CAUSE_F | ECC_CE_INT_CAUSE_F | \
3115 ECC_UE_INT_CAUSE_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303116
3117/*
3118 * EDC/MC interrupt handler.
3119 */
3120static void csio_mem_intr_handler(struct csio_hw *hw, int idx)
3121{
3122 static const char name[3][5] = { "EDC0", "EDC1", "MC" };
3123
3124 unsigned int addr, cnt_addr, v;
3125
3126 if (idx <= MEM_EDC1) {
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303127 addr = EDC_REG(EDC_INT_CAUSE_A, idx);
3128 cnt_addr = EDC_REG(EDC_ECC_STATUS_A, idx);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303129 } else {
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303130 addr = MC_INT_CAUSE_A;
3131 cnt_addr = MC_ECC_STATUS_A;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303132 }
3133
3134 v = csio_rd_reg32(hw, addr) & MEM_INT_MASK;
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303135 if (v & PERR_INT_CAUSE_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303136 csio_fatal(hw, "%s FIFO parity error\n", name[idx]);
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303137 if (v & ECC_CE_INT_CAUSE_F) {
3138 uint32_t cnt = ECC_CECNT_G(csio_rd_reg32(hw, cnt_addr));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303139
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303140 csio_wr_reg32(hw, ECC_CECNT_V(ECC_CECNT_M), cnt_addr);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303141 csio_warn(hw, "%u %s correctable ECC data error%s\n",
3142 cnt, name[idx], cnt > 1 ? "s" : "");
3143 }
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303144 if (v & ECC_UE_INT_CAUSE_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303145 csio_fatal(hw, "%s uncorrectable ECC data error\n", name[idx]);
3146
3147 csio_wr_reg32(hw, v, addr);
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303148 if (v & (PERR_INT_CAUSE_F | ECC_UE_INT_CAUSE_F))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303149 csio_hw_fatal_err(hw);
3150}
3151
3152/*
3153 * MA interrupt handler.
3154 */
3155static void csio_ma_intr_handler(struct csio_hw *hw)
3156{
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303157 uint32_t v, status = csio_rd_reg32(hw, MA_INT_CAUSE_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303158
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303159 if (status & MEM_PERR_INT_CAUSE_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303160 csio_fatal(hw, "MA parity error, parity status %#x\n",
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303161 csio_rd_reg32(hw, MA_PARITY_ERROR_STATUS_A));
3162 if (status & MEM_WRAP_INT_CAUSE_F) {
3163 v = csio_rd_reg32(hw, MA_INT_WRAP_STATUS_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303164 csio_fatal(hw,
3165 "MA address wrap-around error by client %u to address %#x\n",
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303166 MEM_WRAP_CLIENT_NUM_G(v), MEM_WRAP_ADDRESS_G(v) << 4);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303167 }
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303168 csio_wr_reg32(hw, status, MA_INT_CAUSE_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303169 csio_hw_fatal_err(hw);
3170}
3171
3172/*
3173 * SMB interrupt handler.
3174 */
3175static void csio_smb_intr_handler(struct csio_hw *hw)
3176{
3177 static struct intr_info smb_intr_info[] = {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303178 { MSTTXFIFOPARINT_F, "SMB master Tx FIFO parity error", -1, 1 },
3179 { MSTRXFIFOPARINT_F, "SMB master Rx FIFO parity error", -1, 1 },
3180 { SLVFIFOPARINT_F, "SMB slave FIFO parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303181 { 0, NULL, 0, 0 }
3182 };
3183
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303184 if (csio_handle_intr_status(hw, SMB_INT_CAUSE_A, smb_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303185 csio_hw_fatal_err(hw);
3186}
3187
3188/*
3189 * NC-SI interrupt handler.
3190 */
3191static void csio_ncsi_intr_handler(struct csio_hw *hw)
3192{
3193 static struct intr_info ncsi_intr_info[] = {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303194 { CIM_DM_PRTY_ERR_F, "NC-SI CIM parity error", -1, 1 },
3195 { MPS_DM_PRTY_ERR_F, "NC-SI MPS parity error", -1, 1 },
3196 { TXFIFO_PRTY_ERR_F, "NC-SI Tx FIFO parity error", -1, 1 },
3197 { RXFIFO_PRTY_ERR_F, "NC-SI Rx FIFO parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303198 { 0, NULL, 0, 0 }
3199 };
3200
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303201 if (csio_handle_intr_status(hw, NCSI_INT_CAUSE_A, ncsi_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303202 csio_hw_fatal_err(hw);
3203}
3204
3205/*
3206 * XGMAC interrupt handler.
3207 */
3208static void csio_xgmac_intr_handler(struct csio_hw *hw, int port)
3209{
Arvind Bhushan7cc16382013-03-14 05:09:08 +00003210 uint32_t v = csio_rd_reg32(hw, CSIO_MAC_INT_CAUSE_REG(hw, port));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303211
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303212 v &= TXFIFO_PRTY_ERR_F | RXFIFO_PRTY_ERR_F;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303213 if (!v)
3214 return;
3215
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303216 if (v & TXFIFO_PRTY_ERR_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303217 csio_fatal(hw, "XGMAC %d Tx FIFO parity error\n", port);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303218 if (v & RXFIFO_PRTY_ERR_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303219 csio_fatal(hw, "XGMAC %d Rx FIFO parity error\n", port);
Arvind Bhushan7cc16382013-03-14 05:09:08 +00003220 csio_wr_reg32(hw, v, CSIO_MAC_INT_CAUSE_REG(hw, port));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303221 csio_hw_fatal_err(hw);
3222}
3223
3224/*
3225 * PL interrupt handler.
3226 */
3227static void csio_pl_intr_handler(struct csio_hw *hw)
3228{
3229 static struct intr_info pl_intr_info[] = {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303230 { FATALPERR_F, "T4 fatal parity error", -1, 1 },
3231 { PERRVFID_F, "PL VFID_MAP parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303232 { 0, NULL, 0, 0 }
3233 };
3234
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303235 if (csio_handle_intr_status(hw, PL_PL_INT_CAUSE_A, pl_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303236 csio_hw_fatal_err(hw);
3237}
3238
3239/*
3240 * csio_hw_slow_intr_handler - control path interrupt handler
3241 * @hw: HW module
3242 *
3243 * Interrupt handler for non-data global interrupt events, e.g., errors.
3244 * The designation 'slow' is because it involves register reads, while
3245 * data interrupts typically don't involve any MMIOs.
3246 */
3247int
3248csio_hw_slow_intr_handler(struct csio_hw *hw)
3249{
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303250 uint32_t cause = csio_rd_reg32(hw, PL_INT_CAUSE_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303251
3252 if (!(cause & CSIO_GLBL_INTR_MASK)) {
3253 CSIO_INC_STATS(hw, n_plint_unexp);
3254 return 0;
3255 }
3256
3257 csio_dbg(hw, "Slow interrupt! cause: 0x%x\n", cause);
3258
3259 CSIO_INC_STATS(hw, n_plint_cnt);
3260
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303261 if (cause & CIM_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303262 csio_cim_intr_handler(hw);
3263
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303264 if (cause & MPS_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303265 csio_mps_intr_handler(hw);
3266
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303267 if (cause & NCSI_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303268 csio_ncsi_intr_handler(hw);
3269
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303270 if (cause & PL_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303271 csio_pl_intr_handler(hw);
3272
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303273 if (cause & SMB_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303274 csio_smb_intr_handler(hw);
3275
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303276 if (cause & XGMAC0_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303277 csio_xgmac_intr_handler(hw, 0);
3278
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303279 if (cause & XGMAC1_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303280 csio_xgmac_intr_handler(hw, 1);
3281
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303282 if (cause & XGMAC_KR0_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303283 csio_xgmac_intr_handler(hw, 2);
3284
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303285 if (cause & XGMAC_KR1_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303286 csio_xgmac_intr_handler(hw, 3);
3287
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303288 if (cause & PCIE_F)
Arvind Bhushan7cc16382013-03-14 05:09:08 +00003289 hw->chip_ops->chip_pcie_intr_handler(hw);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303290
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303291 if (cause & MC_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303292 csio_mem_intr_handler(hw, MEM_MC);
3293
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303294 if (cause & EDC0_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303295 csio_mem_intr_handler(hw, MEM_EDC0);
3296
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303297 if (cause & EDC1_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303298 csio_mem_intr_handler(hw, MEM_EDC1);
3299
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303300 if (cause & LE_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303301 csio_le_intr_handler(hw);
3302
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303303 if (cause & TP_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303304 csio_tp_intr_handler(hw);
3305
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303306 if (cause & MA_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303307 csio_ma_intr_handler(hw);
3308
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303309 if (cause & PM_TX_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303310 csio_pmtx_intr_handler(hw);
3311
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303312 if (cause & PM_RX_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303313 csio_pmrx_intr_handler(hw);
3314
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303315 if (cause & ULP_RX_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303316 csio_ulprx_intr_handler(hw);
3317
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303318 if (cause & CPL_SWITCH_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303319 csio_cplsw_intr_handler(hw);
3320
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303321 if (cause & SGE_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303322 csio_sge_intr_handler(hw);
3323
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303324 if (cause & ULP_TX_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303325 csio_ulptx_intr_handler(hw);
3326
3327 /* Clear the interrupts just processed for which we are the master. */
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303328 csio_wr_reg32(hw, cause & CSIO_GLBL_INTR_MASK, PL_INT_CAUSE_A);
3329 csio_rd_reg32(hw, PL_INT_CAUSE_A); /* flush */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303330
3331 return 1;
3332}
3333
3334/*****************************************************************************
3335 * HW <--> mailbox interfacing routines.
3336 ****************************************************************************/
3337/*
3338 * csio_mberr_worker - Worker thread (dpc) for mailbox/error completions
3339 *
3340 * @data: Private data pointer.
3341 *
3342 * Called from worker thread context.
3343 */
3344static void
3345csio_mberr_worker(void *data)
3346{
3347 struct csio_hw *hw = (struct csio_hw *)data;
3348 struct csio_mbm *mbm = &hw->mbm;
3349 LIST_HEAD(cbfn_q);
3350 struct csio_mb *mbp_next;
3351 int rv;
3352
3353 del_timer_sync(&mbm->timer);
3354
3355 spin_lock_irq(&hw->lock);
3356 if (list_empty(&mbm->cbfn_q)) {
3357 spin_unlock_irq(&hw->lock);
3358 return;
3359 }
3360
3361 list_splice_tail_init(&mbm->cbfn_q, &cbfn_q);
3362 mbm->stats.n_cbfnq = 0;
3363
3364 /* Try to start waiting mailboxes */
3365 if (!list_empty(&mbm->req_q)) {
3366 mbp_next = list_first_entry(&mbm->req_q, struct csio_mb, list);
3367 list_del_init(&mbp_next->list);
3368
3369 rv = csio_mb_issue(hw, mbp_next);
3370 if (rv != 0)
3371 list_add_tail(&mbp_next->list, &mbm->req_q);
3372 else
3373 CSIO_DEC_STATS(mbm, n_activeq);
3374 }
3375 spin_unlock_irq(&hw->lock);
3376
3377 /* Now callback completions */
3378 csio_mb_completions(hw, &cbfn_q);
3379}
3380
3381/*
3382 * csio_hw_mb_timer - Top-level Mailbox timeout handler.
3383 *
3384 * @data: private data pointer
3385 *
3386 **/
3387static void
3388csio_hw_mb_timer(uintptr_t data)
3389{
3390 struct csio_hw *hw = (struct csio_hw *)data;
3391 struct csio_mb *mbp = NULL;
3392
3393 spin_lock_irq(&hw->lock);
3394 mbp = csio_mb_tmo_handler(hw);
3395 spin_unlock_irq(&hw->lock);
3396
3397 /* Call back the function for the timed-out Mailbox */
3398 if (mbp)
3399 mbp->mb_cbfn(hw, mbp);
3400
3401}
3402
3403/*
3404 * csio_hw_mbm_cleanup - Cleanup Mailbox module.
3405 * @hw: HW module
3406 *
3407 * Called with lock held, should exit with lock held.
3408 * Cancels outstanding mailboxes (waiting, in-flight) and gathers them
3409 * into a local queue. Drops lock and calls the completions. Holds
3410 * lock and returns.
3411 */
3412static void
3413csio_hw_mbm_cleanup(struct csio_hw *hw)
3414{
3415 LIST_HEAD(cbfn_q);
3416
3417 csio_mb_cancel_all(hw, &cbfn_q);
3418
3419 spin_unlock_irq(&hw->lock);
3420 csio_mb_completions(hw, &cbfn_q);
3421 spin_lock_irq(&hw->lock);
3422}
3423
3424/*****************************************************************************
3425 * Event handling
3426 ****************************************************************************/
3427int
3428csio_enqueue_evt(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
3429 uint16_t len)
3430{
3431 struct csio_evt_msg *evt_entry = NULL;
3432
3433 if (type >= CSIO_EVT_MAX)
3434 return -EINVAL;
3435
3436 if (len > CSIO_EVT_MSG_SIZE)
3437 return -EINVAL;
3438
3439 if (hw->flags & CSIO_HWF_FWEVT_STOP)
3440 return -EINVAL;
3441
3442 if (list_empty(&hw->evt_free_q)) {
3443 csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
3444 type, len);
3445 return -ENOMEM;
3446 }
3447
3448 evt_entry = list_first_entry(&hw->evt_free_q,
3449 struct csio_evt_msg, list);
3450 list_del_init(&evt_entry->list);
3451
3452 /* copy event msg and queue the event */
3453 evt_entry->type = type;
3454 memcpy((void *)evt_entry->data, evt_msg, len);
3455 list_add_tail(&evt_entry->list, &hw->evt_active_q);
3456
3457 CSIO_DEC_STATS(hw, n_evt_freeq);
3458 CSIO_INC_STATS(hw, n_evt_activeq);
3459
3460 return 0;
3461}
3462
3463static int
3464csio_enqueue_evt_lock(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
3465 uint16_t len, bool msg_sg)
3466{
3467 struct csio_evt_msg *evt_entry = NULL;
3468 struct csio_fl_dma_buf *fl_sg;
3469 uint32_t off = 0;
3470 unsigned long flags;
3471 int n, ret = 0;
3472
3473 if (type >= CSIO_EVT_MAX)
3474 return -EINVAL;
3475
3476 if (len > CSIO_EVT_MSG_SIZE)
3477 return -EINVAL;
3478
3479 spin_lock_irqsave(&hw->lock, flags);
3480 if (hw->flags & CSIO_HWF_FWEVT_STOP) {
3481 ret = -EINVAL;
3482 goto out;
3483 }
3484
3485 if (list_empty(&hw->evt_free_q)) {
3486 csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
3487 type, len);
3488 ret = -ENOMEM;
3489 goto out;
3490 }
3491
3492 evt_entry = list_first_entry(&hw->evt_free_q,
3493 struct csio_evt_msg, list);
3494 list_del_init(&evt_entry->list);
3495
3496 /* copy event msg and queue the event */
3497 evt_entry->type = type;
3498
3499 /* If Payload in SG list*/
3500 if (msg_sg) {
3501 fl_sg = (struct csio_fl_dma_buf *) evt_msg;
3502 for (n = 0; (n < CSIO_MAX_FLBUF_PER_IQWR && off < len); n++) {
3503 memcpy((void *)((uintptr_t)evt_entry->data + off),
3504 fl_sg->flbufs[n].vaddr,
3505 fl_sg->flbufs[n].len);
3506 off += fl_sg->flbufs[n].len;
3507 }
3508 } else
3509 memcpy((void *)evt_entry->data, evt_msg, len);
3510
3511 list_add_tail(&evt_entry->list, &hw->evt_active_q);
3512 CSIO_DEC_STATS(hw, n_evt_freeq);
3513 CSIO_INC_STATS(hw, n_evt_activeq);
3514out:
3515 spin_unlock_irqrestore(&hw->lock, flags);
3516 return ret;
3517}
3518
3519static void
3520csio_free_evt(struct csio_hw *hw, struct csio_evt_msg *evt_entry)
3521{
3522 if (evt_entry) {
3523 spin_lock_irq(&hw->lock);
3524 list_del_init(&evt_entry->list);
3525 list_add_tail(&evt_entry->list, &hw->evt_free_q);
3526 CSIO_DEC_STATS(hw, n_evt_activeq);
3527 CSIO_INC_STATS(hw, n_evt_freeq);
3528 spin_unlock_irq(&hw->lock);
3529 }
3530}
3531
3532void
3533csio_evtq_flush(struct csio_hw *hw)
3534{
3535 uint32_t count;
3536 count = 30;
3537 while (hw->flags & CSIO_HWF_FWEVT_PENDING && count--) {
3538 spin_unlock_irq(&hw->lock);
3539 msleep(2000);
3540 spin_lock_irq(&hw->lock);
3541 }
3542
3543 CSIO_DB_ASSERT(!(hw->flags & CSIO_HWF_FWEVT_PENDING));
3544}
3545
3546static void
3547csio_evtq_stop(struct csio_hw *hw)
3548{
3549 hw->flags |= CSIO_HWF_FWEVT_STOP;
3550}
3551
3552static void
3553csio_evtq_start(struct csio_hw *hw)
3554{
3555 hw->flags &= ~CSIO_HWF_FWEVT_STOP;
3556}
3557
3558static void
3559csio_evtq_cleanup(struct csio_hw *hw)
3560{
3561 struct list_head *evt_entry, *next_entry;
3562
3563 /* Release outstanding events from activeq to freeq*/
3564 if (!list_empty(&hw->evt_active_q))
3565 list_splice_tail_init(&hw->evt_active_q, &hw->evt_free_q);
3566
3567 hw->stats.n_evt_activeq = 0;
3568 hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
3569
3570 /* Freeup event entry */
3571 list_for_each_safe(evt_entry, next_entry, &hw->evt_free_q) {
3572 kfree(evt_entry);
3573 CSIO_DEC_STATS(hw, n_evt_freeq);
3574 }
3575
3576 hw->stats.n_evt_freeq = 0;
3577}
3578
3579
3580static void
3581csio_process_fwevtq_entry(struct csio_hw *hw, void *wr, uint32_t len,
3582 struct csio_fl_dma_buf *flb, void *priv)
3583{
3584 __u8 op;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303585 void *msg = NULL;
3586 uint32_t msg_len = 0;
3587 bool msg_sg = 0;
3588
3589 op = ((struct rss_header *) wr)->opcode;
3590 if (op == CPL_FW6_PLD) {
3591 CSIO_INC_STATS(hw, n_cpl_fw6_pld);
3592 if (!flb || !flb->totlen) {
3593 CSIO_INC_STATS(hw, n_cpl_unexp);
3594 return;
3595 }
3596
3597 msg = (void *) flb;
3598 msg_len = flb->totlen;
3599 msg_sg = 1;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303600 } else if (op == CPL_FW6_MSG || op == CPL_FW4_MSG) {
3601
3602 CSIO_INC_STATS(hw, n_cpl_fw6_msg);
3603 /* skip RSS header */
3604 msg = (void *)((uintptr_t)wr + sizeof(__be64));
3605 msg_len = (op == CPL_FW6_MSG) ? sizeof(struct cpl_fw6_msg) :
3606 sizeof(struct cpl_fw4_msg);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303607 } else {
3608 csio_warn(hw, "unexpected CPL %#x on FW event queue\n", op);
3609 CSIO_INC_STATS(hw, n_cpl_unexp);
3610 return;
3611 }
3612
3613 /*
3614 * Enqueue event to EventQ. Events processing happens
3615 * in Event worker thread context
3616 */
3617 if (csio_enqueue_evt_lock(hw, CSIO_EVT_FW, msg,
3618 (uint16_t)msg_len, msg_sg))
3619 CSIO_INC_STATS(hw, n_evt_drop);
3620}
3621
3622void
3623csio_evtq_worker(struct work_struct *work)
3624{
3625 struct csio_hw *hw = container_of(work, struct csio_hw, evtq_work);
3626 struct list_head *evt_entry, *next_entry;
3627 LIST_HEAD(evt_q);
3628 struct csio_evt_msg *evt_msg;
3629 struct cpl_fw6_msg *msg;
3630 struct csio_rnode *rn;
3631 int rv = 0;
3632 uint8_t evtq_stop = 0;
3633
3634 csio_dbg(hw, "event worker thread active evts#%d\n",
3635 hw->stats.n_evt_activeq);
3636
3637 spin_lock_irq(&hw->lock);
3638 while (!list_empty(&hw->evt_active_q)) {
3639 list_splice_tail_init(&hw->evt_active_q, &evt_q);
3640 spin_unlock_irq(&hw->lock);
3641
3642 list_for_each_safe(evt_entry, next_entry, &evt_q) {
3643 evt_msg = (struct csio_evt_msg *) evt_entry;
3644
3645 /* Drop events if queue is STOPPED */
3646 spin_lock_irq(&hw->lock);
3647 if (hw->flags & CSIO_HWF_FWEVT_STOP)
3648 evtq_stop = 1;
3649 spin_unlock_irq(&hw->lock);
3650 if (evtq_stop) {
3651 CSIO_INC_STATS(hw, n_evt_drop);
3652 goto free_evt;
3653 }
3654
3655 switch (evt_msg->type) {
3656 case CSIO_EVT_FW:
3657 msg = (struct cpl_fw6_msg *)(evt_msg->data);
3658
3659 if ((msg->opcode == CPL_FW6_MSG ||
3660 msg->opcode == CPL_FW4_MSG) &&
3661 !msg->type) {
3662 rv = csio_mb_fwevt_handler(hw,
3663 msg->data);
3664 if (!rv)
3665 break;
3666 /* Handle any remaining fw events */
3667 csio_fcoe_fwevt_handler(hw,
3668 msg->opcode, msg->data);
3669 } else if (msg->opcode == CPL_FW6_PLD) {
3670
3671 csio_fcoe_fwevt_handler(hw,
3672 msg->opcode, msg->data);
3673 } else {
3674 csio_warn(hw,
3675 "Unhandled FW msg op %x type %x\n",
3676 msg->opcode, msg->type);
3677 CSIO_INC_STATS(hw, n_evt_drop);
3678 }
3679 break;
3680
3681 case CSIO_EVT_MBX:
3682 csio_mberr_worker(hw);
3683 break;
3684
3685 case CSIO_EVT_DEV_LOSS:
3686 memcpy(&rn, evt_msg->data, sizeof(rn));
3687 csio_rnode_devloss_handler(rn);
3688 break;
3689
3690 default:
3691 csio_warn(hw, "Unhandled event %x on evtq\n",
3692 evt_msg->type);
3693 CSIO_INC_STATS(hw, n_evt_unexp);
3694 break;
3695 }
3696free_evt:
3697 csio_free_evt(hw, evt_msg);
3698 }
3699
3700 spin_lock_irq(&hw->lock);
3701 }
3702 hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
3703 spin_unlock_irq(&hw->lock);
3704}
3705
3706int
3707csio_fwevtq_handler(struct csio_hw *hw)
3708{
3709 int rv;
3710
3711 if (csio_q_iqid(hw, hw->fwevt_iq_idx) == CSIO_MAX_QID) {
3712 CSIO_INC_STATS(hw, n_int_stray);
3713 return -EINVAL;
3714 }
3715
3716 rv = csio_wr_process_iq_idx(hw, hw->fwevt_iq_idx,
3717 csio_process_fwevtq_entry, NULL);
3718 return rv;
3719}
3720
3721/****************************************************************************
3722 * Entry points
3723 ****************************************************************************/
3724
3725/* Management module */
3726/*
3727 * csio_mgmt_req_lookup - Lookup the given IO req exist in Active Q.
3728 * mgmt - mgmt module
3729 * @io_req - io request
3730 *
3731 * Return - 0:if given IO Req exists in active Q.
3732 * -EINVAL :if lookup fails.
3733 */
3734int
3735csio_mgmt_req_lookup(struct csio_mgmtm *mgmtm, struct csio_ioreq *io_req)
3736{
3737 struct list_head *tmp;
3738
3739 /* Lookup ioreq in the ACTIVEQ */
3740 list_for_each(tmp, &mgmtm->active_q) {
3741 if (io_req == (struct csio_ioreq *)tmp)
3742 return 0;
3743 }
3744 return -EINVAL;
3745}
3746
3747#define ECM_MIN_TMO 1000 /* Minimum timeout value for req */
3748
3749/*
3750 * csio_mgmts_tmo_handler - MGMT IO Timeout handler.
3751 * @data - Event data.
3752 *
3753 * Return - none.
3754 */
3755static void
3756csio_mgmt_tmo_handler(uintptr_t data)
3757{
3758 struct csio_mgmtm *mgmtm = (struct csio_mgmtm *) data;
3759 struct list_head *tmp;
3760 struct csio_ioreq *io_req;
3761
3762 csio_dbg(mgmtm->hw, "Mgmt timer invoked!\n");
3763
3764 spin_lock_irq(&mgmtm->hw->lock);
3765
3766 list_for_each(tmp, &mgmtm->active_q) {
3767 io_req = (struct csio_ioreq *) tmp;
3768 io_req->tmo -= min_t(uint32_t, io_req->tmo, ECM_MIN_TMO);
3769
3770 if (!io_req->tmo) {
3771 /* Dequeue the request from retry Q. */
3772 tmp = csio_list_prev(tmp);
3773 list_del_init(&io_req->sm.sm_list);
3774 if (io_req->io_cbfn) {
3775 /* io_req will be freed by completion handler */
3776 io_req->wr_status = -ETIMEDOUT;
3777 io_req->io_cbfn(mgmtm->hw, io_req);
3778 } else {
3779 CSIO_DB_ASSERT(0);
3780 }
3781 }
3782 }
3783
3784 /* If retry queue is not empty, re-arm timer */
3785 if (!list_empty(&mgmtm->active_q))
3786 mod_timer(&mgmtm->mgmt_timer,
3787 jiffies + msecs_to_jiffies(ECM_MIN_TMO));
3788 spin_unlock_irq(&mgmtm->hw->lock);
3789}
3790
3791static void
3792csio_mgmtm_cleanup(struct csio_mgmtm *mgmtm)
3793{
3794 struct csio_hw *hw = mgmtm->hw;
3795 struct csio_ioreq *io_req;
3796 struct list_head *tmp;
3797 uint32_t count;
3798
3799 count = 30;
3800 /* Wait for all outstanding req to complete gracefully */
3801 while ((!list_empty(&mgmtm->active_q)) && count--) {
3802 spin_unlock_irq(&hw->lock);
3803 msleep(2000);
3804 spin_lock_irq(&hw->lock);
3805 }
3806
3807 /* release outstanding req from ACTIVEQ */
3808 list_for_each(tmp, &mgmtm->active_q) {
3809 io_req = (struct csio_ioreq *) tmp;
3810 tmp = csio_list_prev(tmp);
3811 list_del_init(&io_req->sm.sm_list);
3812 mgmtm->stats.n_active--;
3813 if (io_req->io_cbfn) {
3814 /* io_req will be freed by completion handler */
3815 io_req->wr_status = -ETIMEDOUT;
3816 io_req->io_cbfn(mgmtm->hw, io_req);
3817 }
3818 }
3819}
3820
3821/*
3822 * csio_mgmt_init - Mgmt module init entry point
3823 * @mgmtsm - mgmt module
3824 * @hw - HW module
3825 *
3826 * Initialize mgmt timer, resource wait queue, active queue,
3827 * completion q. Allocate Egress and Ingress
3828 * WR queues and save off the queue index returned by the WR
3829 * module for future use. Allocate and save off mgmt reqs in the
3830 * mgmt_req_freelist for future use. Make sure their SM is initialized
3831 * to uninit state.
3832 * Returns: 0 - on success
3833 * -ENOMEM - on error.
3834 */
3835static int
3836csio_mgmtm_init(struct csio_mgmtm *mgmtm, struct csio_hw *hw)
3837{
3838 struct timer_list *timer = &mgmtm->mgmt_timer;
3839
3840 init_timer(timer);
3841 timer->function = csio_mgmt_tmo_handler;
3842 timer->data = (unsigned long)mgmtm;
3843
3844 INIT_LIST_HEAD(&mgmtm->active_q);
3845 INIT_LIST_HEAD(&mgmtm->cbfn_q);
3846
3847 mgmtm->hw = hw;
3848 /*mgmtm->iq_idx = hw->fwevt_iq_idx;*/
3849
3850 return 0;
3851}
3852
3853/*
3854 * csio_mgmtm_exit - MGMT module exit entry point
3855 * @mgmtsm - mgmt module
3856 *
3857 * This function called during MGMT module uninit.
3858 * Stop timers, free ioreqs allocated.
3859 * Returns: None
3860 *
3861 */
3862static void
3863csio_mgmtm_exit(struct csio_mgmtm *mgmtm)
3864{
3865 del_timer_sync(&mgmtm->mgmt_timer);
3866}
3867
3868
3869/**
3870 * csio_hw_start - Kicks off the HW State machine
3871 * @hw: Pointer to HW module.
3872 *
3873 * It is assumed that the initialization is a synchronous operation.
3874 * So when we return afer posting the event, the HW SM should be in
3875 * the ready state, if there were no errors during init.
3876 */
3877int
3878csio_hw_start(struct csio_hw *hw)
3879{
3880 spin_lock_irq(&hw->lock);
3881 csio_post_event(&hw->sm, CSIO_HWE_CFG);
3882 spin_unlock_irq(&hw->lock);
3883
3884 if (csio_is_hw_ready(hw))
3885 return 0;
3886 else
3887 return -EINVAL;
3888}
3889
3890int
3891csio_hw_stop(struct csio_hw *hw)
3892{
3893 csio_post_event(&hw->sm, CSIO_HWE_PCI_REMOVE);
3894
3895 if (csio_is_hw_removing(hw))
3896 return 0;
3897 else
3898 return -EINVAL;
3899}
3900
3901/* Max reset retries */
3902#define CSIO_MAX_RESET_RETRIES 3
3903
3904/**
3905 * csio_hw_reset - Reset the hardware
3906 * @hw: HW module.
3907 *
3908 * Caller should hold lock across this function.
3909 */
3910int
3911csio_hw_reset(struct csio_hw *hw)
3912{
3913 if (!csio_is_hw_master(hw))
3914 return -EPERM;
3915
3916 if (hw->rst_retries >= CSIO_MAX_RESET_RETRIES) {
3917 csio_dbg(hw, "Max hw reset attempts reached..");
3918 return -EINVAL;
3919 }
3920
3921 hw->rst_retries++;
3922 csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET);
3923
3924 if (csio_is_hw_ready(hw)) {
3925 hw->rst_retries = 0;
3926 hw->stats.n_reset_start = jiffies_to_msecs(jiffies);
3927 return 0;
3928 } else
3929 return -EINVAL;
3930}
3931
3932/*
3933 * csio_hw_get_device_id - Caches the Adapter's vendor & device id.
3934 * @hw: HW module.
3935 */
3936static void
3937csio_hw_get_device_id(struct csio_hw *hw)
3938{
3939 /* Is the adapter device id cached already ?*/
3940 if (csio_is_dev_id_cached(hw))
3941 return;
3942
3943 /* Get the PCI vendor & device id */
3944 pci_read_config_word(hw->pdev, PCI_VENDOR_ID,
3945 &hw->params.pci.vendor_id);
3946 pci_read_config_word(hw->pdev, PCI_DEVICE_ID,
3947 &hw->params.pci.device_id);
3948
3949 csio_dev_id_cached(hw);
Arvind Bhushan7cc16382013-03-14 05:09:08 +00003950 hw->chip_id = (hw->params.pci.device_id & CSIO_HW_CHIP_MASK);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303951
3952} /* csio_hw_get_device_id */
3953
3954/*
3955 * csio_hw_set_description - Set the model, description of the hw.
3956 * @hw: HW module.
3957 * @ven_id: PCI Vendor ID
3958 * @dev_id: PCI Device ID
3959 */
3960static void
3961csio_hw_set_description(struct csio_hw *hw, uint16_t ven_id, uint16_t dev_id)
3962{
3963 uint32_t adap_type, prot_type;
3964
3965 if (ven_id == CSIO_VENDOR_ID) {
3966 prot_type = (dev_id & CSIO_ASIC_DEVID_PROTO_MASK);
3967 adap_type = (dev_id & CSIO_ASIC_DEVID_TYPE_MASK);
3968
Arvind Bhushan7cc16382013-03-14 05:09:08 +00003969 if (prot_type == CSIO_T4_FCOE_ASIC) {
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303970 memcpy(hw->hw_ver,
Arvind Bhushan7cc16382013-03-14 05:09:08 +00003971 csio_t4_fcoe_adapters[adap_type].model_no, 16);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303972 memcpy(hw->model_desc,
Arvind Bhushan7cc16382013-03-14 05:09:08 +00003973 csio_t4_fcoe_adapters[adap_type].description,
3974 32);
3975 } else if (prot_type == CSIO_T5_FCOE_ASIC) {
3976 memcpy(hw->hw_ver,
3977 csio_t5_fcoe_adapters[adap_type].model_no, 16);
3978 memcpy(hw->model_desc,
3979 csio_t5_fcoe_adapters[adap_type].description,
3980 32);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303981 } else {
3982 char tempName[32] = "Chelsio FCoE Controller";
3983 memcpy(hw->model_desc, tempName, 32);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303984 }
3985 }
3986} /* csio_hw_set_description */
3987
3988/**
3989 * csio_hw_init - Initialize HW module.
3990 * @hw: Pointer to HW module.
3991 *
3992 * Initialize the members of the HW module.
3993 */
3994int
3995csio_hw_init(struct csio_hw *hw)
3996{
3997 int rv = -EINVAL;
3998 uint32_t i;
3999 uint16_t ven_id, dev_id;
4000 struct csio_evt_msg *evt_entry;
4001
4002 INIT_LIST_HEAD(&hw->sm.sm_list);
4003 csio_init_state(&hw->sm, csio_hws_uninit);
4004 spin_lock_init(&hw->lock);
4005 INIT_LIST_HEAD(&hw->sln_head);
4006
4007 /* Get the PCI vendor & device id */
4008 csio_hw_get_device_id(hw);
4009
4010 strcpy(hw->name, CSIO_HW_NAME);
4011
Arvind Bhushan7cc16382013-03-14 05:09:08 +00004012 /* Initialize the HW chip ops with T4/T5 specific ops */
4013 hw->chip_ops = csio_is_t4(hw->chip_id) ? &t4_ops : &t5_ops;
4014
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05304015 /* Set the model & its description */
4016
4017 ven_id = hw->params.pci.vendor_id;
4018 dev_id = hw->params.pci.device_id;
4019
4020 csio_hw_set_description(hw, ven_id, dev_id);
4021
4022 /* Initialize default log level */
4023 hw->params.log_level = (uint32_t) csio_dbg_level;
4024
4025 csio_set_fwevt_intr_idx(hw, -1);
4026 csio_set_nondata_intr_idx(hw, -1);
4027
4028 /* Init all the modules: Mailbox, WorkRequest and Transport */
4029 if (csio_mbm_init(csio_hw_to_mbm(hw), hw, csio_hw_mb_timer))
4030 goto err;
4031
4032 rv = csio_wrm_init(csio_hw_to_wrm(hw), hw);
4033 if (rv)
4034 goto err_mbm_exit;
4035
4036 rv = csio_scsim_init(csio_hw_to_scsim(hw), hw);
4037 if (rv)
4038 goto err_wrm_exit;
4039
4040 rv = csio_mgmtm_init(csio_hw_to_mgmtm(hw), hw);
4041 if (rv)
4042 goto err_scsim_exit;
4043 /* Pre-allocate evtq and initialize them */
4044 INIT_LIST_HEAD(&hw->evt_active_q);
4045 INIT_LIST_HEAD(&hw->evt_free_q);
4046 for (i = 0; i < csio_evtq_sz; i++) {
4047
4048 evt_entry = kzalloc(sizeof(struct csio_evt_msg), GFP_KERNEL);
4049 if (!evt_entry) {
4050 csio_err(hw, "Failed to initialize eventq");
4051 goto err_evtq_cleanup;
4052 }
4053
4054 list_add_tail(&evt_entry->list, &hw->evt_free_q);
4055 CSIO_INC_STATS(hw, n_evt_freeq);
4056 }
4057
4058 hw->dev_num = dev_num;
4059 dev_num++;
4060
4061 return 0;
4062
4063err_evtq_cleanup:
4064 csio_evtq_cleanup(hw);
4065 csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
4066err_scsim_exit:
4067 csio_scsim_exit(csio_hw_to_scsim(hw));
4068err_wrm_exit:
4069 csio_wrm_exit(csio_hw_to_wrm(hw), hw);
4070err_mbm_exit:
4071 csio_mbm_exit(csio_hw_to_mbm(hw));
4072err:
4073 return rv;
4074}
4075
4076/**
4077 * csio_hw_exit - Un-initialize HW module.
4078 * @hw: Pointer to HW module.
4079 *
4080 */
4081void
4082csio_hw_exit(struct csio_hw *hw)
4083{
4084 csio_evtq_cleanup(hw);
4085 csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
4086 csio_scsim_exit(csio_hw_to_scsim(hw));
4087 csio_wrm_exit(csio_hw_to_wrm(hw), hw);
4088 csio_mbm_exit(csio_hw_to_mbm(hw));
4089}