blob: c81b06bcf827b8b1fb261c1f360a2ce3d14e3d93 [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_t5_fcoe_adapters[] = {
64 {"T580-Dbg 10G", "Chelsio T580-Dbg 10G [FCoE]"},
65 {"T520-CR 10G", "Chelsio T520-CR 10G [FCoE]"},
Praveen Madhavan3fb4c222015-01-16 21:30:19 +053066 {"T522-CR 10G/1G", "Chelsio T522-CR 10G/1G [FCoE]"},
Arvind Bhushan7cc16382013-03-14 05:09:08 +000067 {"T540-CR 10G", "Chelsio T540-CR 10G [FCoE]"},
68 {"T520-BCH 10G", "Chelsio T520-BCH 10G [FCoE]"},
69 {"T540-BCH 10G", "Chelsio T540-BCH 10G [FCoE]"},
70 {"T540-CH 10G", "Chelsio T540-CH 10G [FCoE]"},
71 {"T520-SO 10G", "Chelsio T520-SO 10G [FCoE]"},
72 {"T520-CX4 10G", "Chelsio T520-CX4 10G [FCoE]"},
73 {"T520-BT 10G", "Chelsio T520-BT 10G [FCoE]"},
74 {"T504-BT 1G", "Chelsio T504-BT 1G [FCoE]"},
75 {"B520-SR 10G", "Chelsio B520-SR 10G [FCoE]"},
76 {"B504-BT 1G", "Chelsio B504-BT 1G [FCoE]"},
77 {"T580-CR 10G", "Chelsio T580-CR 10G [FCoE]"},
78 {"T540-LP-CR 10G", "Chelsio T540-LP-CR 10G [FCoE]"},
79 {"AMSTERDAM 10G", "Chelsio AMSTERDAM 10G [FCoE]"},
80 {"T580-LP-CR 40G", "Chelsio T580-LP-CR 40G [FCoE]"},
81 {"T520-LL-CR 10G", "Chelsio T520-LL-CR 10G [FCoE]"},
82 {"T560-CR 40G", "Chelsio T560-CR 40G [FCoE]"},
Praveen Madhavan3fb4c222015-01-16 21:30:19 +053083 {"T580-CR 40G", "Chelsio T580-CR 40G [FCoE]"},
84 {"T580-SO 40G", "Chelsio T580-SO 40G [FCoE]"},
85 {"T502-BT 1G", "Chelsio T502-BT 1G [FCoE]"}
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +053086};
87
88static void csio_mgmtm_cleanup(struct csio_mgmtm *);
89static void csio_hw_mbm_cleanup(struct csio_hw *);
90
91/* State machine forward declarations */
92static void csio_hws_uninit(struct csio_hw *, enum csio_hw_ev);
93static void csio_hws_configuring(struct csio_hw *, enum csio_hw_ev);
94static void csio_hws_initializing(struct csio_hw *, enum csio_hw_ev);
95static void csio_hws_ready(struct csio_hw *, enum csio_hw_ev);
96static void csio_hws_quiescing(struct csio_hw *, enum csio_hw_ev);
97static void csio_hws_quiesced(struct csio_hw *, enum csio_hw_ev);
98static void csio_hws_resetting(struct csio_hw *, enum csio_hw_ev);
99static void csio_hws_removing(struct csio_hw *, enum csio_hw_ev);
100static void csio_hws_pcierr(struct csio_hw *, enum csio_hw_ev);
101
102static void csio_hw_initialize(struct csio_hw *hw);
103static void csio_evtq_stop(struct csio_hw *hw);
104static void csio_evtq_start(struct csio_hw *hw);
105
106int csio_is_hw_ready(struct csio_hw *hw)
107{
108 return csio_match_state(hw, csio_hws_ready);
109}
110
111int csio_is_hw_removing(struct csio_hw *hw)
112{
113 return csio_match_state(hw, csio_hws_removing);
114}
115
116
117/*
118 * csio_hw_wait_op_done_val - wait until an operation is completed
119 * @hw: the HW module
120 * @reg: the register to check for completion
121 * @mask: a single-bit field within @reg that indicates completion
122 * @polarity: the value of the field when the operation is completed
123 * @attempts: number of check iterations
124 * @delay: delay in usecs between iterations
125 * @valp: where to store the value of the register at completion time
126 *
127 * Wait until an operation is completed by checking a bit in a register
128 * up to @attempts times. If @valp is not NULL the value of the register
129 * at the time it indicated completion is stored there. Returns 0 if the
130 * operation completes and -EAGAIN otherwise.
131 */
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000132int
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530133csio_hw_wait_op_done_val(struct csio_hw *hw, int reg, uint32_t mask,
134 int polarity, int attempts, int delay, uint32_t *valp)
135{
136 uint32_t val;
137 while (1) {
138 val = csio_rd_reg32(hw, reg);
139
140 if (!!(val & mask) == polarity) {
141 if (valp)
142 *valp = val;
143 return 0;
144 }
145
146 if (--attempts == 0)
147 return -EAGAIN;
148 if (delay)
149 udelay(delay);
150 }
151}
152
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000153/*
154 * csio_hw_tp_wr_bits_indirect - set/clear bits in an indirect TP register
155 * @hw: the adapter
156 * @addr: the indirect TP register address
157 * @mask: specifies the field within the register to modify
158 * @val: new value for the field
159 *
160 * Sets a field of an indirect TP register to the given value.
161 */
162void
163csio_hw_tp_wr_bits_indirect(struct csio_hw *hw, unsigned int addr,
164 unsigned int mask, unsigned int val)
165{
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530166 csio_wr_reg32(hw, addr, TP_PIO_ADDR_A);
167 val |= csio_rd_reg32(hw, TP_PIO_DATA_A) & ~mask;
168 csio_wr_reg32(hw, val, TP_PIO_DATA_A);
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000169}
170
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530171void
172csio_set_reg_field(struct csio_hw *hw, uint32_t reg, uint32_t mask,
173 uint32_t value)
174{
175 uint32_t val = csio_rd_reg32(hw, reg) & ~mask;
176
177 csio_wr_reg32(hw, val | value, reg);
178 /* Flush */
179 csio_rd_reg32(hw, reg);
180
181}
182
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530183static int
Naresh Kumar Inna5036f0a2012-11-20 18:15:40 +0530184csio_memory_write(struct csio_hw *hw, int mtype, u32 addr, u32 len, u32 *buf)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530185{
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000186 return hw->chip_ops->chip_memory_rw(hw, MEMWIN_CSIOSTOR, mtype,
187 addr, len, buf, 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530188}
189
190/*
191 * EEPROM reads take a few tens of us while writes can take a bit over 5 ms.
192 */
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000193#define EEPROM_MAX_RD_POLL 40
194#define EEPROM_MAX_WR_POLL 6
195#define EEPROM_STAT_ADDR 0x7bfc
196#define VPD_BASE 0x400
197#define VPD_BASE_OLD 0
198#define VPD_LEN 1024
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530199#define VPD_INFO_FLD_HDR_SIZE 3
200
201/*
202 * csio_hw_seeprom_read - read a serial EEPROM location
203 * @hw: hw to read
204 * @addr: EEPROM virtual address
205 * @data: where to store the read data
206 *
207 * Read a 32-bit word from a location in serial EEPROM using the card's PCI
208 * VPD capability. Note that this function must be called with a virtual
209 * address.
210 */
211static int
212csio_hw_seeprom_read(struct csio_hw *hw, uint32_t addr, uint32_t *data)
213{
214 uint16_t val = 0;
215 int attempts = EEPROM_MAX_RD_POLL;
216 uint32_t base = hw->params.pci.vpd_cap_addr;
217
218 if (addr >= EEPROMVSIZE || (addr & 3))
219 return -EINVAL;
220
221 pci_write_config_word(hw->pdev, base + PCI_VPD_ADDR, (uint16_t)addr);
222
223 do {
224 udelay(10);
225 pci_read_config_word(hw->pdev, base + PCI_VPD_ADDR, &val);
226 } while (!(val & PCI_VPD_ADDR_F) && --attempts);
227
228 if (!(val & PCI_VPD_ADDR_F)) {
229 csio_err(hw, "reading EEPROM address 0x%x failed\n", addr);
230 return -EINVAL;
231 }
232
233 pci_read_config_dword(hw->pdev, base + PCI_VPD_DATA, data);
Praveen Madhavan78890ed2015-01-09 21:25:16 +0530234 *data = le32_to_cpu(*(__le32 *)data);
Naresh Kumar Inna5036f0a2012-11-20 18:15:40 +0530235
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530236 return 0;
237}
238
239/*
240 * Partial EEPROM Vital Product Data structure. Includes only the ID and
241 * VPD-R sections.
242 */
243struct t4_vpd_hdr {
244 u8 id_tag;
245 u8 id_len[2];
246 u8 id_data[ID_LEN];
247 u8 vpdr_tag;
248 u8 vpdr_len[2];
249};
250
251/*
252 * csio_hw_get_vpd_keyword_val - Locates an information field keyword in
253 * the VPD
254 * @v: Pointer to buffered vpd data structure
255 * @kw: The keyword to search for
256 *
257 * Returns the value of the information field keyword or
258 * -EINVAL otherwise.
259 */
260static int
261csio_hw_get_vpd_keyword_val(const struct t4_vpd_hdr *v, const char *kw)
262{
263 int32_t i;
264 int32_t offset , len;
265 const uint8_t *buf = &v->id_tag;
266 const uint8_t *vpdr_len = &v->vpdr_tag;
267 offset = sizeof(struct t4_vpd_hdr);
268 len = (uint16_t)vpdr_len[1] + ((uint16_t)vpdr_len[2] << 8);
269
270 if (len + sizeof(struct t4_vpd_hdr) > VPD_LEN)
271 return -EINVAL;
272
273 for (i = offset; (i + VPD_INFO_FLD_HDR_SIZE) <= (offset + len);) {
274 if (memcmp(buf + i , kw, 2) == 0) {
275 i += VPD_INFO_FLD_HDR_SIZE;
276 return i;
277 }
278
279 i += VPD_INFO_FLD_HDR_SIZE + buf[i+2];
280 }
281
282 return -EINVAL;
283}
284
285static int
286csio_pci_capability(struct pci_dev *pdev, int cap, int *pos)
287{
288 *pos = pci_find_capability(pdev, cap);
289 if (*pos)
290 return 0;
291
292 return -1;
293}
294
295/*
296 * csio_hw_get_vpd_params - read VPD parameters from VPD EEPROM
297 * @hw: HW module
298 * @p: where to store the parameters
299 *
300 * Reads card parameters stored in VPD EEPROM.
301 */
302static int
303csio_hw_get_vpd_params(struct csio_hw *hw, struct csio_vpd *p)
304{
305 int i, ret, ec, sn, addr;
306 uint8_t *vpd, csum;
307 const struct t4_vpd_hdr *v;
308 /* To get around compilation warning from strstrip */
309 char *s;
310
311 if (csio_is_valid_vpd(hw))
312 return 0;
313
314 ret = csio_pci_capability(hw->pdev, PCI_CAP_ID_VPD,
315 &hw->params.pci.vpd_cap_addr);
316 if (ret)
317 return -EINVAL;
318
319 vpd = kzalloc(VPD_LEN, GFP_ATOMIC);
320 if (vpd == NULL)
321 return -ENOMEM;
322
323 /*
324 * Card information normally starts at VPD_BASE but early cards had
325 * it at 0.
326 */
327 ret = csio_hw_seeprom_read(hw, VPD_BASE, (uint32_t *)(vpd));
328 addr = *vpd == 0x82 ? VPD_BASE : VPD_BASE_OLD;
329
330 for (i = 0; i < VPD_LEN; i += 4) {
331 ret = csio_hw_seeprom_read(hw, addr + i, (uint32_t *)(vpd + i));
332 if (ret) {
333 kfree(vpd);
334 return ret;
335 }
336 }
337
338 /* Reset the VPD flag! */
339 hw->flags &= (~CSIO_HWF_VPD_VALID);
340
341 v = (const struct t4_vpd_hdr *)vpd;
342
343#define FIND_VPD_KW(var, name) do { \
344 var = csio_hw_get_vpd_keyword_val(v, name); \
345 if (var < 0) { \
346 csio_err(hw, "missing VPD keyword " name "\n"); \
347 kfree(vpd); \
348 return -EINVAL; \
349 } \
350} while (0)
351
352 FIND_VPD_KW(i, "RV");
353 for (csum = 0; i >= 0; i--)
354 csum += vpd[i];
355
356 if (csum) {
357 csio_err(hw, "corrupted VPD EEPROM, actual csum %u\n", csum);
358 kfree(vpd);
359 return -EINVAL;
360 }
361 FIND_VPD_KW(ec, "EC");
362 FIND_VPD_KW(sn, "SN");
363#undef FIND_VPD_KW
364
365 memcpy(p->id, v->id_data, ID_LEN);
366 s = strstrip(p->id);
367 memcpy(p->ec, vpd + ec, EC_LEN);
368 s = strstrip(p->ec);
369 i = vpd[sn - VPD_INFO_FLD_HDR_SIZE + 2];
370 memcpy(p->sn, vpd + sn, min(i, SERNUM_LEN));
371 s = strstrip(p->sn);
372
373 csio_valid_vpd_copied(hw);
374
375 kfree(vpd);
376 return 0;
377}
378
379/*
380 * csio_hw_sf1_read - read data from the serial flash
381 * @hw: the HW module
382 * @byte_cnt: number of bytes to read
383 * @cont: whether another operation will be chained
384 * @lock: whether to lock SF for PL access only
385 * @valp: where to store the read data
386 *
387 * Reads up to 4 bytes of data from the serial flash. The location of
388 * the read needs to be specified prior to calling this by issuing the
389 * appropriate commands to the serial flash.
390 */
391static int
392csio_hw_sf1_read(struct csio_hw *hw, uint32_t byte_cnt, int32_t cont,
393 int32_t lock, uint32_t *valp)
394{
395 int ret;
396
397 if (!byte_cnt || byte_cnt > 4)
398 return -EINVAL;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530399 if (csio_rd_reg32(hw, SF_OP_A) & SF_BUSY_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530400 return -EBUSY;
401
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530402 csio_wr_reg32(hw, SF_LOCK_V(lock) | SF_CONT_V(cont) |
403 BYTECNT_V(byte_cnt - 1), SF_OP_A);
404 ret = csio_hw_wait_op_done_val(hw, SF_OP_A, SF_BUSY_F, 0, SF_ATTEMPTS,
405 10, NULL);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530406 if (!ret)
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530407 *valp = csio_rd_reg32(hw, SF_DATA_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530408 return ret;
409}
410
411/*
412 * csio_hw_sf1_write - write data to the serial flash
413 * @hw: the HW module
414 * @byte_cnt: number of bytes to write
415 * @cont: whether another operation will be chained
416 * @lock: whether to lock SF for PL access only
417 * @val: value to write
418 *
419 * Writes up to 4 bytes of data to the serial flash. The location of
420 * the write needs to be specified prior to calling this by issuing the
421 * appropriate commands to the serial flash.
422 */
423static int
424csio_hw_sf1_write(struct csio_hw *hw, uint32_t byte_cnt, uint32_t cont,
425 int32_t lock, uint32_t val)
426{
427 if (!byte_cnt || byte_cnt > 4)
428 return -EINVAL;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530429 if (csio_rd_reg32(hw, SF_OP_A) & SF_BUSY_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530430 return -EBUSY;
431
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530432 csio_wr_reg32(hw, val, SF_DATA_A);
433 csio_wr_reg32(hw, SF_CONT_V(cont) | BYTECNT_V(byte_cnt - 1) |
434 OP_V(1) | SF_LOCK_V(lock), SF_OP_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530435
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530436 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 +0530437 10, NULL);
438}
439
440/*
441 * csio_hw_flash_wait_op - wait for a flash operation to complete
442 * @hw: the HW module
443 * @attempts: max number of polls of the status register
444 * @delay: delay between polls in ms
445 *
446 * Wait for a flash operation to complete by polling the status register.
447 */
448static int
449csio_hw_flash_wait_op(struct csio_hw *hw, int32_t attempts, int32_t delay)
450{
451 int ret;
452 uint32_t status;
453
454 while (1) {
455 ret = csio_hw_sf1_write(hw, 1, 1, 1, SF_RD_STATUS);
456 if (ret != 0)
457 return ret;
458
459 ret = csio_hw_sf1_read(hw, 1, 0, 1, &status);
460 if (ret != 0)
461 return ret;
462
463 if (!(status & 1))
464 return 0;
465 if (--attempts == 0)
466 return -EAGAIN;
467 if (delay)
468 msleep(delay);
469 }
470}
471
472/*
473 * csio_hw_read_flash - read words from serial flash
474 * @hw: the HW module
475 * @addr: the start address for the read
476 * @nwords: how many 32-bit words to read
477 * @data: where to store the read data
478 * @byte_oriented: whether to store data as bytes or as words
479 *
480 * Read the specified number of 32-bit words from the serial flash.
481 * If @byte_oriented is set the read data is stored as a byte array
482 * (i.e., big-endian), otherwise as 32-bit words in the platform's
483 * natural endianess.
484 */
485static int
486csio_hw_read_flash(struct csio_hw *hw, uint32_t addr, uint32_t nwords,
487 uint32_t *data, int32_t byte_oriented)
488{
489 int ret;
490
491 if (addr + nwords * sizeof(uint32_t) > hw->params.sf_size || (addr & 3))
492 return -EINVAL;
493
494 addr = swab32(addr) | SF_RD_DATA_FAST;
495
496 ret = csio_hw_sf1_write(hw, 4, 1, 0, addr);
497 if (ret != 0)
498 return ret;
499
500 ret = csio_hw_sf1_read(hw, 1, 1, 0, data);
501 if (ret != 0)
502 return ret;
503
504 for ( ; nwords; nwords--, data++) {
505 ret = csio_hw_sf1_read(hw, 4, nwords > 1, nwords == 1, data);
506 if (nwords == 1)
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530507 csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530508 if (ret)
509 return ret;
510 if (byte_oriented)
Praveen Madhavan78890ed2015-01-09 21:25:16 +0530511 *data = (__force __u32) htonl(*data);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530512 }
513 return 0;
514}
515
516/*
517 * csio_hw_write_flash - write up to a page of data to the serial flash
518 * @hw: the hw
519 * @addr: the start address to write
520 * @n: length of data to write in bytes
521 * @data: the data to write
522 *
523 * Writes up to a page of data (256 bytes) to the serial flash starting
524 * at the given address. All the data must be written to the same page.
525 */
526static int
527csio_hw_write_flash(struct csio_hw *hw, uint32_t addr,
528 uint32_t n, const uint8_t *data)
529{
530 int ret = -EINVAL;
531 uint32_t buf[64];
532 uint32_t i, c, left, val, offset = addr & 0xff;
533
534 if (addr >= hw->params.sf_size || offset + n > SF_PAGE_SIZE)
535 return -EINVAL;
536
537 val = swab32(addr) | SF_PROG_PAGE;
538
539 ret = csio_hw_sf1_write(hw, 1, 0, 1, SF_WR_ENABLE);
540 if (ret != 0)
541 goto unlock;
542
543 ret = csio_hw_sf1_write(hw, 4, 1, 1, val);
544 if (ret != 0)
545 goto unlock;
546
547 for (left = n; left; left -= c) {
548 c = min(left, 4U);
549 for (val = 0, i = 0; i < c; ++i)
550 val = (val << 8) + *data++;
551
552 ret = csio_hw_sf1_write(hw, c, c != left, 1, val);
553 if (ret)
554 goto unlock;
555 }
556 ret = csio_hw_flash_wait_op(hw, 8, 1);
557 if (ret)
558 goto unlock;
559
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530560 csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530561
562 /* Read the page to verify the write succeeded */
563 ret = csio_hw_read_flash(hw, addr & ~0xff, ARRAY_SIZE(buf), buf, 1);
564 if (ret)
565 return ret;
566
567 if (memcmp(data - n, (uint8_t *)buf + offset, n)) {
568 csio_err(hw,
569 "failed to correctly write the flash page at %#x\n",
570 addr);
571 return -EINVAL;
572 }
573
574 return 0;
575
576unlock:
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530577 csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530578 return ret;
579}
580
581/*
582 * csio_hw_flash_erase_sectors - erase a range of flash sectors
583 * @hw: the HW module
584 * @start: the first sector to erase
585 * @end: the last sector to erase
586 *
587 * Erases the sectors in the given inclusive range.
588 */
589static int
590csio_hw_flash_erase_sectors(struct csio_hw *hw, int32_t start, int32_t end)
591{
592 int ret = 0;
593
594 while (start <= end) {
595
596 ret = csio_hw_sf1_write(hw, 1, 0, 1, SF_WR_ENABLE);
597 if (ret != 0)
598 goto out;
599
600 ret = csio_hw_sf1_write(hw, 4, 0, 1,
601 SF_ERASE_SECTOR | (start << 8));
602 if (ret != 0)
603 goto out;
604
605 ret = csio_hw_flash_wait_op(hw, 14, 500);
606 if (ret != 0)
607 goto out;
608
609 start++;
610 }
611out:
612 if (ret)
613 csio_err(hw, "erase of flash sector %d failed, error %d\n",
614 start, ret);
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530615 csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530616 return 0;
617}
618
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530619static void
620csio_hw_print_fw_version(struct csio_hw *hw, char *str)
621{
622 csio_info(hw, "%s: %u.%u.%u.%u\n", str,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +0530623 FW_HDR_FW_VER_MAJOR_G(hw->fwrev),
624 FW_HDR_FW_VER_MINOR_G(hw->fwrev),
625 FW_HDR_FW_VER_MICRO_G(hw->fwrev),
626 FW_HDR_FW_VER_BUILD_G(hw->fwrev));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530627}
628
629/*
630 * csio_hw_get_fw_version - read the firmware version
631 * @hw: HW module
632 * @vers: where to place the version
633 *
634 * Reads the FW version from flash.
635 */
636static int
637csio_hw_get_fw_version(struct csio_hw *hw, uint32_t *vers)
638{
Praveen Madhavan216ce692015-01-27 19:46:37 +0530639 return csio_hw_read_flash(hw, FLASH_FW_START +
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530640 offsetof(struct fw_hdr, fw_ver), 1,
641 vers, 0);
642}
643
644/*
645 * csio_hw_get_tp_version - read the TP microcode version
646 * @hw: HW module
647 * @vers: where to place the version
648 *
649 * Reads the TP microcode version from flash.
650 */
651static int
652csio_hw_get_tp_version(struct csio_hw *hw, u32 *vers)
653{
654 return csio_hw_read_flash(hw, FLASH_FW_START +
655 offsetof(struct fw_hdr, tp_microcode_ver), 1,
656 vers, 0);
657}
658
659/*
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530660 * csio_hw_fw_dload - download firmware.
661 * @hw: HW module
662 * @fw_data: firmware image to write.
663 * @size: image size
664 *
665 * Write the supplied firmware image to the card's serial flash.
666 */
667static int
668csio_hw_fw_dload(struct csio_hw *hw, uint8_t *fw_data, uint32_t size)
669{
670 uint32_t csum;
671 int32_t addr;
672 int ret;
673 uint32_t i;
674 uint8_t first_page[SF_PAGE_SIZE];
Naresh Kumar Inna5036f0a2012-11-20 18:15:40 +0530675 const __be32 *p = (const __be32 *)fw_data;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530676 struct fw_hdr *hdr = (struct fw_hdr *)fw_data;
677 uint32_t sf_sec_size;
678
679 if ((!hw->params.sf_size) || (!hw->params.sf_nsec)) {
680 csio_err(hw, "Serial Flash data invalid\n");
681 return -EINVAL;
682 }
683
684 if (!size) {
685 csio_err(hw, "FW image has no data\n");
686 return -EINVAL;
687 }
688
689 if (size & 511) {
690 csio_err(hw, "FW image size not multiple of 512 bytes\n");
691 return -EINVAL;
692 }
693
694 if (ntohs(hdr->len512) * 512 != size) {
695 csio_err(hw, "FW image size differs from size in FW header\n");
696 return -EINVAL;
697 }
698
Praveen Madhavan216ce692015-01-27 19:46:37 +0530699 if (size > FLASH_FW_MAX_SIZE) {
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530700 csio_err(hw, "FW image too large, max is %u bytes\n",
Praveen Madhavan216ce692015-01-27 19:46:37 +0530701 FLASH_FW_MAX_SIZE);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530702 return -EINVAL;
703 }
704
705 for (csum = 0, i = 0; i < size / sizeof(csum); i++)
706 csum += ntohl(p[i]);
707
708 if (csum != 0xffffffff) {
709 csio_err(hw, "corrupted firmware image, checksum %#x\n", csum);
710 return -EINVAL;
711 }
712
713 sf_sec_size = hw->params.sf_size / hw->params.sf_nsec;
714 i = DIV_ROUND_UP(size, sf_sec_size); /* # of sectors spanned */
715
716 csio_dbg(hw, "Erasing sectors... start:%d end:%d\n",
Praveen Madhavan216ce692015-01-27 19:46:37 +0530717 FLASH_FW_START_SEC, FLASH_FW_START_SEC + i - 1);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530718
Praveen Madhavan216ce692015-01-27 19:46:37 +0530719 ret = csio_hw_flash_erase_sectors(hw, FLASH_FW_START_SEC,
720 FLASH_FW_START_SEC + i - 1);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530721 if (ret) {
722 csio_err(hw, "Flash Erase failed\n");
723 goto out;
724 }
725
726 /*
727 * We write the correct version at the end so the driver can see a bad
728 * version if the FW write fails. Start by writing a copy of the
729 * first page with a bad version.
730 */
731 memcpy(first_page, fw_data, SF_PAGE_SIZE);
732 ((struct fw_hdr *)first_page)->fw_ver = htonl(0xffffffff);
Praveen Madhavan216ce692015-01-27 19:46:37 +0530733 ret = csio_hw_write_flash(hw, FLASH_FW_START, SF_PAGE_SIZE, first_page);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530734 if (ret)
735 goto out;
736
737 csio_dbg(hw, "Writing Flash .. start:%d end:%d\n",
738 FW_IMG_START, FW_IMG_START + size);
739
Praveen Madhavan216ce692015-01-27 19:46:37 +0530740 addr = FLASH_FW_START;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530741 for (size -= SF_PAGE_SIZE; size; size -= SF_PAGE_SIZE) {
742 addr += SF_PAGE_SIZE;
743 fw_data += SF_PAGE_SIZE;
744 ret = csio_hw_write_flash(hw, addr, SF_PAGE_SIZE, fw_data);
745 if (ret)
746 goto out;
747 }
748
749 ret = csio_hw_write_flash(hw,
Praveen Madhavan216ce692015-01-27 19:46:37 +0530750 FLASH_FW_START +
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530751 offsetof(struct fw_hdr, fw_ver),
752 sizeof(hdr->fw_ver),
753 (const uint8_t *)&hdr->fw_ver);
754
755out:
756 if (ret)
757 csio_err(hw, "firmware download failed, error %d\n", ret);
758 return ret;
759}
760
761static int
762csio_hw_get_flash_params(struct csio_hw *hw)
763{
764 int ret;
765 uint32_t info = 0;
766
767 ret = csio_hw_sf1_write(hw, 1, 1, 0, SF_RD_ID);
768 if (!ret)
769 ret = csio_hw_sf1_read(hw, 3, 0, 1, &info);
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530770 csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530771 if (ret != 0)
772 return ret;
773
774 if ((info & 0xff) != 0x20) /* not a Numonix flash */
775 return -EINVAL;
776 info >>= 16; /* log2 of size */
777 if (info >= 0x14 && info < 0x18)
778 hw->params.sf_nsec = 1 << (info - 16);
779 else if (info == 0x18)
780 hw->params.sf_nsec = 64;
781 else
782 return -EINVAL;
783 hw->params.sf_size = 1 << info;
784
785 return 0;
786}
787
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530788/*****************************************************************************/
789/* HW State machine assists */
790/*****************************************************************************/
791
792static int
793csio_hw_dev_ready(struct csio_hw *hw)
794{
795 uint32_t reg;
796 int cnt = 6;
797
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530798 while (((reg = csio_rd_reg32(hw, PL_WHOAMI_A)) == 0xFFFFFFFF) &&
799 (--cnt != 0))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530800 mdelay(100);
801
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530802 if ((cnt == 0) && (((int32_t)(SOURCEPF_G(reg)) < 0) ||
803 (SOURCEPF_G(reg) >= CSIO_MAX_PFN))) {
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530804 csio_err(hw, "PL_WHOAMI returned 0x%x, cnt:%d\n", reg, cnt);
805 return -EIO;
806 }
807
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530808 hw->pfn = SOURCEPF_G(reg);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530809
810 return 0;
811}
812
813/*
814 * csio_do_hello - Perform the HELLO FW Mailbox command and process response.
815 * @hw: HW module
816 * @state: Device state
817 *
818 * FW_HELLO_CMD has to be polled for completion.
819 */
820static int
821csio_do_hello(struct csio_hw *hw, enum csio_dev_state *state)
822{
823 struct csio_mb *mbp;
824 int rv = 0;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530825 enum fw_retval retval;
826 uint8_t mpfn;
827 char state_str[16];
828 int retries = FW_CMD_HELLO_RETRIES;
829
830 memset(state_str, 0, sizeof(state_str));
831
832 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
833 if (!mbp) {
834 rv = -ENOMEM;
835 CSIO_INC_STATS(hw, n_err_nomem);
836 goto out;
837 }
838
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530839retry:
840 csio_mb_hello(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn,
Hariprasad Shenai666224d2014-12-11 11:11:43 +0530841 hw->pfn, CSIO_MASTER_MAY, NULL);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530842
843 rv = csio_mb_issue(hw, mbp);
844 if (rv) {
845 csio_err(hw, "failed to issue HELLO cmd. ret:%d.\n", rv);
846 goto out_free_mb;
847 }
848
849 csio_mb_process_hello_rsp(hw, mbp, &retval, state, &mpfn);
850 if (retval != FW_SUCCESS) {
851 csio_err(hw, "HELLO cmd failed with ret: %d\n", retval);
852 rv = -EINVAL;
853 goto out_free_mb;
854 }
855
856 /* Firmware has designated us to be master */
857 if (hw->pfn == mpfn) {
858 hw->flags |= CSIO_HWF_MASTER;
859 } else if (*state == CSIO_DEV_STATE_UNINIT) {
860 /*
861 * If we're not the Master PF then we need to wait around for
862 * the Master PF Driver to finish setting up the adapter.
863 *
864 * Note that we also do this wait if we're a non-Master-capable
865 * PF and there is no current Master PF; a Master PF may show up
866 * momentarily and we wouldn't want to fail pointlessly. (This
867 * can happen when an OS loads lots of different drivers rapidly
868 * at the same time). In this case, the Master PF returned by
869 * the firmware will be PCIE_FW_MASTER_MASK so the test below
870 * will work ...
871 */
872
873 int waiting = FW_CMD_HELLO_TIMEOUT;
874
875 /*
876 * Wait for the firmware to either indicate an error or
877 * initialized state. If we see either of these we bail out
878 * and report the issue to the caller. If we exhaust the
879 * "hello timeout" and we haven't exhausted our retries, try
880 * again. Otherwise bail with a timeout error.
881 */
882 for (;;) {
883 uint32_t pcie_fw;
884
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000885 spin_unlock_irq(&hw->lock);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530886 msleep(50);
Arvind Bhushan7cc16382013-03-14 05:09:08 +0000887 spin_lock_irq(&hw->lock);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530888 waiting -= 50;
889
890 /*
891 * If neither Error nor Initialialized are indicated
892 * by the firmware keep waiting till we exaust our
893 * timeout ... and then retry if we haven't exhausted
894 * our retries ...
895 */
Hariprasad Shenaif061de42015-01-05 16:30:44 +0530896 pcie_fw = csio_rd_reg32(hw, PCIE_FW_A);
897 if (!(pcie_fw & (PCIE_FW_ERR_F|PCIE_FW_INIT_F))) {
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530898 if (waiting <= 0) {
899 if (retries-- > 0)
900 goto retry;
901
902 rv = -ETIMEDOUT;
903 break;
904 }
905 continue;
906 }
907
908 /*
909 * We either have an Error or Initialized condition
910 * report errors preferentially.
911 */
912 if (state) {
Hariprasad Shenaif061de42015-01-05 16:30:44 +0530913 if (pcie_fw & PCIE_FW_ERR_F) {
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530914 *state = CSIO_DEV_STATE_ERR;
915 rv = -ETIMEDOUT;
Hariprasad Shenaif061de42015-01-05 16:30:44 +0530916 } else if (pcie_fw & PCIE_FW_INIT_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530917 *state = CSIO_DEV_STATE_INIT;
918 }
919
920 /*
921 * If we arrived before a Master PF was selected and
922 * there's not a valid Master PF, grab its identity
923 * for our caller.
924 */
Hariprasad Shenaif061de42015-01-05 16:30:44 +0530925 if (mpfn == PCIE_FW_MASTER_M &&
926 (pcie_fw & PCIE_FW_MASTER_VLD_F))
927 mpfn = PCIE_FW_MASTER_G(pcie_fw);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +0530928 break;
929 }
930 hw->flags &= ~CSIO_HWF_MASTER;
931 }
932
933 switch (*state) {
934 case CSIO_DEV_STATE_UNINIT:
935 strcpy(state_str, "Initializing");
936 break;
937 case CSIO_DEV_STATE_INIT:
938 strcpy(state_str, "Initialized");
939 break;
940 case CSIO_DEV_STATE_ERR:
941 strcpy(state_str, "Error");
942 break;
943 default:
944 strcpy(state_str, "Unknown");
945 break;
946 }
947
948 if (hw->pfn == mpfn)
949 csio_info(hw, "PF: %d, Coming up as MASTER, HW state: %s\n",
950 hw->pfn, state_str);
951 else
952 csio_info(hw,
953 "PF: %d, Coming up as SLAVE, Master PF: %d, HW state: %s\n",
954 hw->pfn, mpfn, state_str);
955
956out_free_mb:
957 mempool_free(mbp, hw->mb_mempool);
958out:
959 return rv;
960}
961
962/*
963 * csio_do_bye - Perform the BYE FW Mailbox command and process response.
964 * @hw: HW module
965 *
966 */
967static int
968csio_do_bye(struct csio_hw *hw)
969{
970 struct csio_mb *mbp;
971 enum fw_retval retval;
972
973 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
974 if (!mbp) {
975 CSIO_INC_STATS(hw, n_err_nomem);
976 return -ENOMEM;
977 }
978
979 csio_mb_bye(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
980
981 if (csio_mb_issue(hw, mbp)) {
982 csio_err(hw, "Issue of BYE command failed\n");
983 mempool_free(mbp, hw->mb_mempool);
984 return -EINVAL;
985 }
986
987 retval = csio_mb_fw_retval(mbp);
988 if (retval != FW_SUCCESS) {
989 mempool_free(mbp, hw->mb_mempool);
990 return -EINVAL;
991 }
992
993 mempool_free(mbp, hw->mb_mempool);
994
995 return 0;
996}
997
998/*
999 * csio_do_reset- Perform the device reset.
1000 * @hw: HW module
1001 * @fw_rst: FW reset
1002 *
1003 * If fw_rst is set, issues FW reset mbox cmd otherwise
1004 * does PIO reset.
1005 * Performs reset of the function.
1006 */
1007static int
1008csio_do_reset(struct csio_hw *hw, bool fw_rst)
1009{
1010 struct csio_mb *mbp;
1011 enum fw_retval retval;
1012
1013 if (!fw_rst) {
1014 /* PIO reset */
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301015 csio_wr_reg32(hw, PIORSTMODE_F | PIORST_F, PL_RST_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301016 mdelay(2000);
1017 return 0;
1018 }
1019
1020 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1021 if (!mbp) {
1022 CSIO_INC_STATS(hw, n_err_nomem);
1023 return -ENOMEM;
1024 }
1025
1026 csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301027 PIORSTMODE_F | PIORST_F, 0, NULL);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301028
1029 if (csio_mb_issue(hw, mbp)) {
1030 csio_err(hw, "Issue of RESET command failed.n");
1031 mempool_free(mbp, hw->mb_mempool);
1032 return -EINVAL;
1033 }
1034
1035 retval = csio_mb_fw_retval(mbp);
1036 if (retval != FW_SUCCESS) {
1037 csio_err(hw, "RESET cmd failed with ret:0x%x.\n", retval);
1038 mempool_free(mbp, hw->mb_mempool);
1039 return -EINVAL;
1040 }
1041
1042 mempool_free(mbp, hw->mb_mempool);
1043
1044 return 0;
1045}
1046
1047static int
1048csio_hw_validate_caps(struct csio_hw *hw, struct csio_mb *mbp)
1049{
1050 struct fw_caps_config_cmd *rsp = (struct fw_caps_config_cmd *)mbp->mb;
1051 uint16_t caps;
1052
1053 caps = ntohs(rsp->fcoecaps);
1054
1055 if (!(caps & FW_CAPS_CONFIG_FCOE_INITIATOR)) {
1056 csio_err(hw, "No FCoE Initiator capability in the firmware.\n");
1057 return -EINVAL;
1058 }
1059
1060 if (!(caps & FW_CAPS_CONFIG_FCOE_CTRL_OFLD)) {
1061 csio_err(hw, "No FCoE Control Offload capability\n");
1062 return -EINVAL;
1063 }
1064
1065 return 0;
1066}
1067
1068/*
1069 * csio_hw_fw_halt - issue a reset/halt to FW and put uP into RESET
1070 * @hw: the HW module
1071 * @mbox: mailbox to use for the FW RESET command (if desired)
1072 * @force: force uP into RESET even if FW RESET command fails
1073 *
1074 * Issues a RESET command to firmware (if desired) with a HALT indication
1075 * and then puts the microprocessor into RESET state. The RESET command
1076 * will only be issued if a legitimate mailbox is provided (mbox <=
1077 * PCIE_FW_MASTER_MASK).
1078 *
1079 * This is generally used in order for the host to safely manipulate the
1080 * adapter without fear of conflicting with whatever the firmware might
1081 * be doing. The only way out of this state is to RESTART the firmware
1082 * ...
1083 */
1084static int
1085csio_hw_fw_halt(struct csio_hw *hw, uint32_t mbox, int32_t force)
1086{
1087 enum fw_retval retval = 0;
1088
1089 /*
1090 * If a legitimate mailbox is provided, issue a RESET command
1091 * with a HALT indication.
1092 */
Hariprasad Shenaif061de42015-01-05 16:30:44 +05301093 if (mbox <= PCIE_FW_MASTER_M) {
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301094 struct csio_mb *mbp;
1095
1096 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1097 if (!mbp) {
1098 CSIO_INC_STATS(hw, n_err_nomem);
1099 return -ENOMEM;
1100 }
1101
1102 csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301103 PIORSTMODE_F | PIORST_F, FW_RESET_CMD_HALT_F,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301104 NULL);
1105
1106 if (csio_mb_issue(hw, mbp)) {
1107 csio_err(hw, "Issue of RESET command failed!\n");
1108 mempool_free(mbp, hw->mb_mempool);
1109 return -EINVAL;
1110 }
1111
1112 retval = csio_mb_fw_retval(mbp);
1113 mempool_free(mbp, hw->mb_mempool);
1114 }
1115
1116 /*
1117 * Normally we won't complete the operation if the firmware RESET
1118 * command fails but if our caller insists we'll go ahead and put the
1119 * uP into RESET. This can be useful if the firmware is hung or even
1120 * missing ... We'll have to take the risk of putting the uP into
1121 * RESET without the cooperation of firmware in that case.
1122 *
1123 * We also force the firmware's HALT flag to be on in case we bypassed
1124 * the firmware RESET command above or we're dealing with old firmware
1125 * which doesn't have the HALT capability. This will serve as a flag
1126 * for the incoming firmware to know that it's coming out of a HALT
1127 * rather than a RESET ... if it's new enough to understand that ...
1128 */
1129 if (retval == 0 || force) {
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05301130 csio_set_reg_field(hw, CIM_BOOT_CFG_A, UPCRST_F, UPCRST_F);
Hariprasad Shenaif061de42015-01-05 16:30:44 +05301131 csio_set_reg_field(hw, PCIE_FW_A, PCIE_FW_HALT_F,
1132 PCIE_FW_HALT_F);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301133 }
1134
1135 /*
1136 * And we always return the result of the firmware RESET command
1137 * even when we force the uP into RESET ...
1138 */
1139 return retval ? -EINVAL : 0;
1140}
1141
1142/*
1143 * csio_hw_fw_restart - restart the firmware by taking the uP out of RESET
1144 * @hw: the HW module
1145 * @reset: if we want to do a RESET to restart things
1146 *
1147 * Restart firmware previously halted by csio_hw_fw_halt(). On successful
1148 * return the previous PF Master remains as the new PF Master and there
1149 * is no need to issue a new HELLO command, etc.
1150 *
1151 * We do this in two ways:
1152 *
1153 * 1. If we're dealing with newer firmware we'll simply want to take
1154 * the chip's microprocessor out of RESET. This will cause the
1155 * firmware to start up from its start vector. And then we'll loop
1156 * until the firmware indicates it's started again (PCIE_FW.HALT
1157 * reset to 0) or we timeout.
1158 *
1159 * 2. If we're dealing with older firmware then we'll need to RESET
1160 * the chip since older firmware won't recognize the PCIE_FW.HALT
1161 * flag and automatically RESET itself on startup.
1162 */
1163static int
1164csio_hw_fw_restart(struct csio_hw *hw, uint32_t mbox, int32_t reset)
1165{
1166 if (reset) {
1167 /*
1168 * Since we're directing the RESET instead of the firmware
1169 * doing it automatically, we need to clear the PCIE_FW.HALT
1170 * bit.
1171 */
Hariprasad Shenaif061de42015-01-05 16:30:44 +05301172 csio_set_reg_field(hw, PCIE_FW_A, PCIE_FW_HALT_F, 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301173
1174 /*
1175 * If we've been given a valid mailbox, first try to get the
1176 * firmware to do the RESET. If that works, great and we can
1177 * return success. Otherwise, if we haven't been given a
1178 * valid mailbox or the RESET command failed, fall back to
1179 * hitting the chip with a hammer.
1180 */
Hariprasad Shenaif061de42015-01-05 16:30:44 +05301181 if (mbox <= PCIE_FW_MASTER_M) {
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05301182 csio_set_reg_field(hw, CIM_BOOT_CFG_A, UPCRST_F, 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301183 msleep(100);
1184 if (csio_do_reset(hw, true) == 0)
1185 return 0;
1186 }
1187
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301188 csio_wr_reg32(hw, PIORSTMODE_F | PIORST_F, PL_RST_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301189 msleep(2000);
1190 } else {
1191 int ms;
1192
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05301193 csio_set_reg_field(hw, CIM_BOOT_CFG_A, UPCRST_F, 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301194 for (ms = 0; ms < FW_CMD_MAX_TIMEOUT; ) {
Hariprasad Shenaif061de42015-01-05 16:30:44 +05301195 if (!(csio_rd_reg32(hw, PCIE_FW_A) & PCIE_FW_HALT_F))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301196 return 0;
1197 msleep(100);
1198 ms += 100;
1199 }
1200 return -ETIMEDOUT;
1201 }
1202 return 0;
1203}
1204
1205/*
1206 * csio_hw_fw_upgrade - perform all of the steps necessary to upgrade FW
1207 * @hw: the HW module
1208 * @mbox: mailbox to use for the FW RESET command (if desired)
1209 * @fw_data: the firmware image to write
1210 * @size: image size
1211 * @force: force upgrade even if firmware doesn't cooperate
1212 *
1213 * Perform all of the steps necessary for upgrading an adapter's
1214 * firmware image. Normally this requires the cooperation of the
1215 * existing firmware in order to halt all existing activities
1216 * but if an invalid mailbox token is passed in we skip that step
1217 * (though we'll still put the adapter microprocessor into RESET in
1218 * that case).
1219 *
1220 * On successful return the new firmware will have been loaded and
1221 * the adapter will have been fully RESET losing all previous setup
1222 * state. On unsuccessful return the adapter may be completely hosed ...
1223 * positive errno indicates that the adapter is ~probably~ intact, a
1224 * negative errno indicates that things are looking bad ...
1225 */
1226static int
1227csio_hw_fw_upgrade(struct csio_hw *hw, uint32_t mbox,
1228 const u8 *fw_data, uint32_t size, int32_t force)
1229{
1230 const struct fw_hdr *fw_hdr = (const struct fw_hdr *)fw_data;
1231 int reset, ret;
1232
1233 ret = csio_hw_fw_halt(hw, mbox, force);
1234 if (ret != 0 && !force)
1235 return ret;
1236
1237 ret = csio_hw_fw_dload(hw, (uint8_t *) fw_data, size);
1238 if (ret != 0)
1239 return ret;
1240
1241 /*
1242 * Older versions of the firmware don't understand the new
1243 * PCIE_FW.HALT flag and so won't know to perform a RESET when they
1244 * restart. So for newly loaded older firmware we'll have to do the
1245 * RESET for it so it starts up on a clean slate. We can tell if
1246 * the newly loaded firmware will handle this right by checking
1247 * its header flags to see if it advertises the capability.
1248 */
1249 reset = ((ntohl(fw_hdr->flags) & FW_HDR_FLAGS_RESET_HALT) == 0);
1250 return csio_hw_fw_restart(hw, mbox, reset);
1251}
1252
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301253/*
1254 * csio_get_device_params - Get device parameters.
1255 * @hw: HW module
1256 *
1257 */
1258static int
1259csio_get_device_params(struct csio_hw *hw)
1260{
1261 struct csio_wrm *wrm = csio_hw_to_wrm(hw);
1262 struct csio_mb *mbp;
1263 enum fw_retval retval;
1264 u32 param[6];
1265 int i, j = 0;
1266
1267 /* Initialize portids to -1 */
1268 for (i = 0; i < CSIO_MAX_PPORTS; i++)
1269 hw->pport[i].portid = -1;
1270
1271 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1272 if (!mbp) {
1273 CSIO_INC_STATS(hw, n_err_nomem);
1274 return -ENOMEM;
1275 }
1276
1277 /* Get port vec information. */
1278 param[0] = FW_PARAM_DEV(PORTVEC);
1279
1280 /* Get Core clock. */
1281 param[1] = FW_PARAM_DEV(CCLK);
1282
1283 /* Get EQ id start and end. */
1284 param[2] = FW_PARAM_PFVF(EQ_START);
1285 param[3] = FW_PARAM_PFVF(EQ_END);
1286
1287 /* Get IQ id start and end. */
1288 param[4] = FW_PARAM_PFVF(IQFLINT_START);
1289 param[5] = FW_PARAM_PFVF(IQFLINT_END);
1290
1291 csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
1292 ARRAY_SIZE(param), param, NULL, false, NULL);
1293 if (csio_mb_issue(hw, mbp)) {
1294 csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
1295 mempool_free(mbp, hw->mb_mempool);
1296 return -EINVAL;
1297 }
1298
1299 csio_mb_process_read_params_rsp(hw, mbp, &retval,
1300 ARRAY_SIZE(param), param);
1301 if (retval != FW_SUCCESS) {
1302 csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
1303 retval);
1304 mempool_free(mbp, hw->mb_mempool);
1305 return -EINVAL;
1306 }
1307
1308 /* cache the information. */
1309 hw->port_vec = param[0];
1310 hw->vpd.cclk = param[1];
1311 wrm->fw_eq_start = param[2];
1312 wrm->fw_iq_start = param[4];
1313
1314 /* Using FW configured max iqs & eqs */
1315 if ((hw->flags & CSIO_HWF_USING_SOFT_PARAMS) ||
1316 !csio_is_hw_master(hw)) {
1317 hw->cfg_niq = param[5] - param[4] + 1;
1318 hw->cfg_neq = param[3] - param[2] + 1;
1319 csio_dbg(hw, "Using fwconfig max niqs %d neqs %d\n",
1320 hw->cfg_niq, hw->cfg_neq);
1321 }
1322
1323 hw->port_vec &= csio_port_mask;
1324
1325 hw->num_pports = hweight32(hw->port_vec);
1326
1327 csio_dbg(hw, "Port vector: 0x%x, #ports: %d\n",
1328 hw->port_vec, hw->num_pports);
1329
1330 for (i = 0; i < hw->num_pports; i++) {
1331 while ((hw->port_vec & (1 << j)) == 0)
1332 j++;
1333 hw->pport[i].portid = j++;
1334 csio_dbg(hw, "Found Port:%d\n", hw->pport[i].portid);
1335 }
1336 mempool_free(mbp, hw->mb_mempool);
1337
1338 return 0;
1339}
1340
1341
1342/*
1343 * csio_config_device_caps - Get and set device capabilities.
1344 * @hw: HW module
1345 *
1346 */
1347static int
1348csio_config_device_caps(struct csio_hw *hw)
1349{
1350 struct csio_mb *mbp;
1351 enum fw_retval retval;
1352 int rv = -EINVAL;
1353
1354 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1355 if (!mbp) {
1356 CSIO_INC_STATS(hw, n_err_nomem);
1357 return -ENOMEM;
1358 }
1359
1360 /* Get device capabilities */
1361 csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, 0, 0, 0, 0, NULL);
1362
1363 if (csio_mb_issue(hw, mbp)) {
1364 csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(r) failed!\n");
1365 goto out;
1366 }
1367
1368 retval = csio_mb_fw_retval(mbp);
1369 if (retval != FW_SUCCESS) {
1370 csio_err(hw, "FW_CAPS_CONFIG_CMD(r) returned %d!\n", retval);
1371 goto out;
1372 }
1373
1374 /* Validate device capabilities */
1375 if (csio_hw_validate_caps(hw, mbp))
1376 goto out;
1377
1378 /* Don't config device capabilities if already configured */
1379 if (hw->fw_state == CSIO_DEV_STATE_INIT) {
1380 rv = 0;
1381 goto out;
1382 }
1383
1384 /* Write back desired device capabilities */
1385 csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, true, true,
1386 false, true, NULL);
1387
1388 if (csio_mb_issue(hw, mbp)) {
1389 csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(w) failed!\n");
1390 goto out;
1391 }
1392
1393 retval = csio_mb_fw_retval(mbp);
1394 if (retval != FW_SUCCESS) {
1395 csio_err(hw, "FW_CAPS_CONFIG_CMD(w) returned %d!\n", retval);
1396 goto out;
1397 }
1398
1399 rv = 0;
1400out:
1401 mempool_free(mbp, hw->mb_mempool);
1402 return rv;
1403}
1404
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301405/*
1406 * csio_enable_ports - Bring up all available ports.
1407 * @hw: HW module.
1408 *
1409 */
1410static int
1411csio_enable_ports(struct csio_hw *hw)
1412{
1413 struct csio_mb *mbp;
1414 enum fw_retval retval;
1415 uint8_t portid;
1416 int i;
1417
1418 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1419 if (!mbp) {
1420 CSIO_INC_STATS(hw, n_err_nomem);
1421 return -ENOMEM;
1422 }
1423
1424 for (i = 0; i < hw->num_pports; i++) {
1425 portid = hw->pport[i].portid;
1426
1427 /* Read PORT information */
1428 csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid,
1429 false, 0, 0, NULL);
1430
1431 if (csio_mb_issue(hw, mbp)) {
1432 csio_err(hw, "failed to issue FW_PORT_CMD(r) port:%d\n",
1433 portid);
1434 mempool_free(mbp, hw->mb_mempool);
1435 return -EINVAL;
1436 }
1437
1438 csio_mb_process_read_port_rsp(hw, mbp, &retval,
1439 &hw->pport[i].pcap);
1440 if (retval != FW_SUCCESS) {
1441 csio_err(hw, "FW_PORT_CMD(r) port:%d failed: 0x%x\n",
1442 portid, retval);
1443 mempool_free(mbp, hw->mb_mempool);
1444 return -EINVAL;
1445 }
1446
1447 /* Write back PORT information */
1448 csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid, true,
1449 (PAUSE_RX | PAUSE_TX), hw->pport[i].pcap, NULL);
1450
1451 if (csio_mb_issue(hw, mbp)) {
1452 csio_err(hw, "failed to issue FW_PORT_CMD(w) port:%d\n",
1453 portid);
1454 mempool_free(mbp, hw->mb_mempool);
1455 return -EINVAL;
1456 }
1457
1458 retval = csio_mb_fw_retval(mbp);
1459 if (retval != FW_SUCCESS) {
1460 csio_err(hw, "FW_PORT_CMD(w) port:%d failed :0x%x\n",
1461 portid, retval);
1462 mempool_free(mbp, hw->mb_mempool);
1463 return -EINVAL;
1464 }
1465
1466 } /* For all ports */
1467
1468 mempool_free(mbp, hw->mb_mempool);
1469
1470 return 0;
1471}
1472
1473/*
1474 * csio_get_fcoe_resinfo - Read fcoe fw resource info.
1475 * @hw: HW module
1476 * Issued with lock held.
1477 */
1478static int
1479csio_get_fcoe_resinfo(struct csio_hw *hw)
1480{
1481 struct csio_fcoe_res_info *res_info = &hw->fres_info;
1482 struct fw_fcoe_res_info_cmd *rsp;
1483 struct csio_mb *mbp;
1484 enum fw_retval retval;
1485
1486 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1487 if (!mbp) {
1488 CSIO_INC_STATS(hw, n_err_nomem);
1489 return -ENOMEM;
1490 }
1491
1492 /* Get FCoE FW resource information */
1493 csio_fcoe_read_res_info_init_mb(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
1494
1495 if (csio_mb_issue(hw, mbp)) {
1496 csio_err(hw, "failed to issue FW_FCOE_RES_INFO_CMD\n");
1497 mempool_free(mbp, hw->mb_mempool);
1498 return -EINVAL;
1499 }
1500
1501 rsp = (struct fw_fcoe_res_info_cmd *)(mbp->mb);
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301502 retval = FW_CMD_RETVAL_G(ntohl(rsp->retval_len16));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301503 if (retval != FW_SUCCESS) {
1504 csio_err(hw, "FW_FCOE_RES_INFO_CMD failed with ret x%x\n",
1505 retval);
1506 mempool_free(mbp, hw->mb_mempool);
1507 return -EINVAL;
1508 }
1509
1510 res_info->e_d_tov = ntohs(rsp->e_d_tov);
1511 res_info->r_a_tov_seq = ntohs(rsp->r_a_tov_seq);
1512 res_info->r_a_tov_els = ntohs(rsp->r_a_tov_els);
1513 res_info->r_r_tov = ntohs(rsp->r_r_tov);
1514 res_info->max_xchgs = ntohl(rsp->max_xchgs);
1515 res_info->max_ssns = ntohl(rsp->max_ssns);
1516 res_info->used_xchgs = ntohl(rsp->used_xchgs);
1517 res_info->used_ssns = ntohl(rsp->used_ssns);
1518 res_info->max_fcfs = ntohl(rsp->max_fcfs);
1519 res_info->max_vnps = ntohl(rsp->max_vnps);
1520 res_info->used_fcfs = ntohl(rsp->used_fcfs);
1521 res_info->used_vnps = ntohl(rsp->used_vnps);
1522
1523 csio_dbg(hw, "max ssns:%d max xchgs:%d\n", res_info->max_ssns,
1524 res_info->max_xchgs);
1525 mempool_free(mbp, hw->mb_mempool);
1526
1527 return 0;
1528}
1529
1530static int
1531csio_hw_check_fwconfig(struct csio_hw *hw, u32 *param)
1532{
1533 struct csio_mb *mbp;
1534 enum fw_retval retval;
1535 u32 _param[1];
1536
1537 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1538 if (!mbp) {
1539 CSIO_INC_STATS(hw, n_err_nomem);
1540 return -ENOMEM;
1541 }
1542
1543 /*
1544 * Find out whether we're dealing with a version of
1545 * the firmware which has configuration file support.
1546 */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301547 _param[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
1548 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301549
1550 csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
1551 ARRAY_SIZE(_param), _param, NULL, false, NULL);
1552 if (csio_mb_issue(hw, mbp)) {
1553 csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
1554 mempool_free(mbp, hw->mb_mempool);
1555 return -EINVAL;
1556 }
1557
1558 csio_mb_process_read_params_rsp(hw, mbp, &retval,
1559 ARRAY_SIZE(_param), _param);
1560 if (retval != FW_SUCCESS) {
1561 csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
1562 retval);
1563 mempool_free(mbp, hw->mb_mempool);
1564 return -EINVAL;
1565 }
1566
1567 mempool_free(mbp, hw->mb_mempool);
1568 *param = _param[0];
1569
1570 return 0;
1571}
1572
1573static int
1574csio_hw_flash_config(struct csio_hw *hw, u32 *fw_cfg_param, char *path)
1575{
1576 int ret = 0;
1577 const struct firmware *cf;
1578 struct pci_dev *pci_dev = hw->pdev;
1579 struct device *dev = &pci_dev->dev;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301580 unsigned int mtype = 0, maddr = 0;
1581 uint32_t *cfg_data;
1582 int value_to_add = 0;
1583
Praveen Madhavan3fb4c222015-01-16 21:30:19 +05301584 if (request_firmware(&cf, FW_CFG_NAME_T5, dev) < 0) {
Arvind Bhushan7cc16382013-03-14 05:09:08 +00001585 csio_err(hw, "could not find config file %s, err: %d\n",
Praveen Madhavan3fb4c222015-01-16 21:30:19 +05301586 FW_CFG_NAME_T5, ret);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301587 return -ENOENT;
1588 }
1589
1590 if (cf->size%4 != 0)
1591 value_to_add = 4 - (cf->size % 4);
1592
1593 cfg_data = kzalloc(cf->size+value_to_add, GFP_KERNEL);
Jesper Juhl02db3db2012-12-26 21:31:51 +01001594 if (cfg_data == NULL) {
1595 ret = -ENOMEM;
1596 goto leave;
1597 }
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301598
1599 memcpy((void *)cfg_data, (const void *)cf->data, cf->size);
Jesper Juhl02db3db2012-12-26 21:31:51 +01001600 if (csio_hw_check_fwconfig(hw, fw_cfg_param) != 0) {
1601 ret = -EINVAL;
1602 goto leave;
1603 }
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301604
Hariprasad Shenai51678652014-11-21 12:52:02 +05301605 mtype = FW_PARAMS_PARAM_Y_G(*fw_cfg_param);
1606 maddr = FW_PARAMS_PARAM_Z_G(*fw_cfg_param) << 16;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301607
1608 ret = csio_memory_write(hw, mtype, maddr,
1609 cf->size + value_to_add, cfg_data);
Arvind Bhushan7cc16382013-03-14 05:09:08 +00001610
1611 if ((ret == 0) && (value_to_add != 0)) {
1612 union {
1613 u32 word;
1614 char buf[4];
1615 } last;
1616 size_t size = cf->size & ~0x3;
1617 int i;
1618
1619 last.word = cfg_data[size >> 2];
1620 for (i = value_to_add; i < 4; i++)
1621 last.buf[i] = 0;
1622 ret = csio_memory_write(hw, mtype, maddr + size, 4, &last.word);
1623 }
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301624 if (ret == 0) {
Arvind Bhushan7cc16382013-03-14 05:09:08 +00001625 csio_info(hw, "config file upgraded to %s\n",
Praveen Madhavan3fb4c222015-01-16 21:30:19 +05301626 FW_CFG_NAME_T5);
1627 snprintf(path, 64, "%s%s", "/lib/firmware/", FW_CFG_NAME_T5);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301628 }
1629
Jesper Juhl02db3db2012-12-26 21:31:51 +01001630leave:
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301631 kfree(cfg_data);
1632 release_firmware(cf);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301633 return ret;
1634}
1635
1636/*
1637 * HW initialization: contact FW, obtain config, perform basic init.
1638 *
1639 * If the firmware we're dealing with has Configuration File support, then
1640 * we use that to perform all configuration -- either using the configuration
1641 * file stored in flash on the adapter or using a filesystem-local file
1642 * if available.
1643 *
1644 * If we don't have configuration file support in the firmware, then we'll
1645 * have to set things up the old fashioned way with hard-coded register
1646 * writes and firmware commands ...
1647 */
1648
1649/*
1650 * Attempt to initialize the HW via a Firmware Configuration File.
1651 */
1652static int
1653csio_hw_use_fwconfig(struct csio_hw *hw, int reset, u32 *fw_cfg_param)
1654{
Praveen Madhavan216ce692015-01-27 19:46:37 +05301655 struct csio_mb *mbp = NULL;
1656 struct fw_caps_config_cmd *caps_cmd;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301657 unsigned int mtype, maddr;
Praveen Madhavan216ce692015-01-27 19:46:37 +05301658 int rv = -EINVAL;
Arvind Bhushan7cc16382013-03-14 05:09:08 +00001659 uint32_t finiver = 0, finicsum = 0, cfcsum = 0;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301660 char path[64];
Praveen Madhavan216ce692015-01-27 19:46:37 +05301661 char *config_name = NULL;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301662
1663 /*
1664 * Reset device if necessary
1665 */
1666 if (reset) {
1667 rv = csio_do_reset(hw, true);
1668 if (rv != 0)
1669 goto bye;
1670 }
1671
1672 /*
1673 * If we have a configuration file in host ,
1674 * then use that. Otherwise, use the configuration file stored
1675 * in the HW flash ...
1676 */
1677 spin_unlock_irq(&hw->lock);
1678 rv = csio_hw_flash_config(hw, fw_cfg_param, path);
1679 spin_lock_irq(&hw->lock);
1680 if (rv != 0) {
Praveen Madhavan216ce692015-01-27 19:46:37 +05301681 /*
1682 * config file was not found. Use default
1683 * config file from flash.
1684 */
1685 config_name = "On FLASH";
1686 mtype = FW_MEMTYPE_CF_FLASH;
1687 maddr = hw->chip_ops->chip_flash_cfg_addr(hw);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301688 } else {
Praveen Madhavan216ce692015-01-27 19:46:37 +05301689 config_name = path;
Hariprasad Shenai51678652014-11-21 12:52:02 +05301690 mtype = FW_PARAMS_PARAM_Y_G(*fw_cfg_param);
1691 maddr = FW_PARAMS_PARAM_Z_G(*fw_cfg_param) << 16;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301692 }
1693
Praveen Madhavan216ce692015-01-27 19:46:37 +05301694 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1695 if (!mbp) {
1696 CSIO_INC_STATS(hw, n_err_nomem);
1697 return -ENOMEM;
1698 }
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301699 /*
Praveen Madhavan216ce692015-01-27 19:46:37 +05301700 * Tell the firmware to process the indicated Configuration File.
1701 * If there are no errors and the caller has provided return value
1702 * pointers for the [fini] section version, checksum and computed
1703 * checksum, pass those back to the caller.
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301704 */
Praveen Madhavan216ce692015-01-27 19:46:37 +05301705 caps_cmd = (struct fw_caps_config_cmd *)(mbp->mb);
1706 CSIO_INIT_MBP(mbp, caps_cmd, CSIO_MB_DEFAULT_TMO, hw, NULL, 1);
1707 caps_cmd->op_to_write =
1708 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
1709 FW_CMD_REQUEST_F |
1710 FW_CMD_READ_F);
1711 caps_cmd->cfvalid_to_len16 =
1712 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
1713 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
1714 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
1715 FW_LEN16(*caps_cmd));
1716
1717 if (csio_mb_issue(hw, mbp)) {
1718 rv = -EINVAL;
1719 goto bye;
1720 }
1721
1722 rv = csio_mb_fw_retval(mbp);
1723 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
1724 * Configuration File in FLASH), our last gasp effort is to use the
1725 * Firmware Configuration File which is embedded in the
1726 * firmware. A very few early versions of the firmware didn't
1727 * have one embedded but we can ignore those.
1728 */
1729 if (rv == ENOENT) {
1730 CSIO_INIT_MBP(mbp, caps_cmd, CSIO_MB_DEFAULT_TMO, hw, NULL, 1);
1731 caps_cmd->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
1732 FW_CMD_REQUEST_F |
1733 FW_CMD_READ_F);
1734 caps_cmd->cfvalid_to_len16 = htonl(FW_LEN16(*caps_cmd));
1735
1736 if (csio_mb_issue(hw, mbp)) {
1737 rv = -EINVAL;
1738 goto bye;
1739 }
1740
1741 rv = csio_mb_fw_retval(mbp);
1742 config_name = "Firmware Default";
1743 }
1744 if (rv != FW_SUCCESS)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301745 goto bye;
1746
Praveen Madhavan216ce692015-01-27 19:46:37 +05301747 finiver = ntohl(caps_cmd->finiver);
1748 finicsum = ntohl(caps_cmd->finicsum);
1749 cfcsum = ntohl(caps_cmd->cfcsum);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301750
Praveen Madhavan216ce692015-01-27 19:46:37 +05301751 /*
1752 * And now tell the firmware to use the configuration we just loaded.
1753 */
1754 caps_cmd->op_to_write =
1755 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
1756 FW_CMD_REQUEST_F |
1757 FW_CMD_WRITE_F);
1758 caps_cmd->cfvalid_to_len16 = htonl(FW_LEN16(*caps_cmd));
1759
1760 if (csio_mb_issue(hw, mbp)) {
1761 rv = -EINVAL;
1762 goto bye;
1763 }
1764
1765 rv = csio_mb_fw_retval(mbp);
1766 if (rv != FW_SUCCESS) {
1767 csio_dbg(hw, "FW_CAPS_CONFIG_CMD returned %d!\n", rv);
1768 goto bye;
1769 }
1770
1771 mempool_free(mbp, hw->mb_mempool);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301772 if (finicsum != cfcsum) {
1773 csio_warn(hw,
1774 "Config File checksum mismatch: csum=%#x, computed=%#x\n",
1775 finicsum, cfcsum);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301776 }
1777
Praveen Madhavan216ce692015-01-27 19:46:37 +05301778 /* Validate device capabilities */
1779 if (csio_hw_validate_caps(hw, mbp))
1780 goto bye;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301781 /*
1782 * Note that we're operating with parameters
1783 * not supplied by the driver, rather than from hard-wired
1784 * initialization constants buried in the driver.
1785 */
1786 hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
1787
1788 /* device parameters */
1789 rv = csio_get_device_params(hw);
1790 if (rv != 0)
1791 goto bye;
1792
1793 /* Configure SGE */
1794 csio_wr_sge_init(hw);
1795
1796 /*
1797 * And finally tell the firmware to initialize itself using the
1798 * parameters from the Configuration File.
1799 */
1800 /* Post event to notify completion of configuration */
1801 csio_post_event(&hw->sm, CSIO_HWE_INIT);
1802
Praveen Madhavan216ce692015-01-27 19:46:37 +05301803 csio_info(hw, "Successfully configure using Firmware "
1804 "Configuration File %s, version %#x, computed checksum %#x\n",
1805 config_name, finiver, cfcsum);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301806 return 0;
1807
1808 /*
1809 * Something bad happened. Return the error ...
1810 */
1811bye:
Praveen Madhavan216ce692015-01-27 19:46:37 +05301812 if (mbp)
1813 mempool_free(mbp, hw->mb_mempool);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301814 hw->flags &= ~CSIO_HWF_USING_SOFT_PARAMS;
Praveen Madhavan216ce692015-01-27 19:46:37 +05301815 csio_warn(hw, "Configuration file error %d\n", rv);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301816 return rv;
1817}
1818
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05301819/* Is the given firmware API compatible with the one the driver was compiled
1820 * with?
1821 */
1822static int fw_compatible(const struct fw_hdr *hdr1, const struct fw_hdr *hdr2)
1823{
1824
1825 /* short circuit if it's the exact same firmware version */
1826 if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
1827 return 1;
1828
1829#define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
1830 if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
1831 SAME_INTF(ri) && SAME_INTF(iscsi) && SAME_INTF(fcoe))
1832 return 1;
1833#undef SAME_INTF
1834
1835 return 0;
1836}
1837
1838/* The firmware in the filesystem is usable, but should it be installed?
1839 * This routine explains itself in detail if it indicates the filesystem
1840 * firmware should be installed.
1841 */
1842static int csio_should_install_fs_fw(struct csio_hw *hw, int card_fw_usable,
1843 int k, int c)
1844{
1845 const char *reason;
1846
1847 if (!card_fw_usable) {
1848 reason = "incompatible or unusable";
1849 goto install;
1850 }
1851
1852 if (k > c) {
1853 reason = "older than the version supported with this driver";
1854 goto install;
1855 }
1856
1857 return 0;
1858
1859install:
1860 csio_err(hw, "firmware on card (%u.%u.%u.%u) is %s, "
1861 "installing firmware %u.%u.%u.%u on card.\n",
1862 FW_HDR_FW_VER_MAJOR_G(c), FW_HDR_FW_VER_MINOR_G(c),
1863 FW_HDR_FW_VER_MICRO_G(c), FW_HDR_FW_VER_BUILD_G(c), reason,
1864 FW_HDR_FW_VER_MAJOR_G(k), FW_HDR_FW_VER_MINOR_G(k),
1865 FW_HDR_FW_VER_MICRO_G(k), FW_HDR_FW_VER_BUILD_G(k));
1866
1867 return 1;
1868}
1869
1870static struct fw_info fw_info_array[] = {
1871 {
1872 .chip = CHELSIO_T5,
1873 .fs_name = FW_CFG_NAME_T5,
1874 .fw_mod_name = FW_FNAME_T5,
1875 .fw_hdr = {
1876 .chip = FW_HDR_CHIP_T5,
1877 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
1878 .intfver_nic = FW_INTFVER(T5, NIC),
1879 .intfver_vnic = FW_INTFVER(T5, VNIC),
1880 .intfver_ri = FW_INTFVER(T5, RI),
1881 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
1882 .intfver_fcoe = FW_INTFVER(T5, FCOE),
1883 },
1884 }
1885};
1886
1887static struct fw_info *find_fw_info(int chip)
1888{
1889 int i;
1890
1891 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
1892 if (fw_info_array[i].chip == chip)
1893 return &fw_info_array[i];
1894 }
1895 return NULL;
1896}
1897
Praveen Madhavan78890ed2015-01-09 21:25:16 +05301898static int csio_hw_prep_fw(struct csio_hw *hw, struct fw_info *fw_info,
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05301899 const u8 *fw_data, unsigned int fw_size,
1900 struct fw_hdr *card_fw, enum csio_dev_state state,
1901 int *reset)
1902{
1903 int ret, card_fw_usable, fs_fw_usable;
1904 const struct fw_hdr *fs_fw;
1905 const struct fw_hdr *drv_fw;
1906
1907 drv_fw = &fw_info->fw_hdr;
1908
1909 /* Read the header of the firmware on the card */
1910 ret = csio_hw_read_flash(hw, FLASH_FW_START,
1911 sizeof(*card_fw) / sizeof(uint32_t),
1912 (uint32_t *)card_fw, 1);
1913 if (ret == 0) {
1914 card_fw_usable = fw_compatible(drv_fw, (const void *)card_fw);
1915 } else {
1916 csio_err(hw,
1917 "Unable to read card's firmware header: %d\n", ret);
1918 card_fw_usable = 0;
1919 }
1920
1921 if (fw_data != NULL) {
1922 fs_fw = (const void *)fw_data;
1923 fs_fw_usable = fw_compatible(drv_fw, fs_fw);
1924 } else {
1925 fs_fw = NULL;
1926 fs_fw_usable = 0;
1927 }
1928
1929 if (card_fw_usable && card_fw->fw_ver == drv_fw->fw_ver &&
1930 (!fs_fw_usable || fs_fw->fw_ver == drv_fw->fw_ver)) {
1931 /* Common case: the firmware on the card is an exact match and
1932 * the filesystem one is an exact match too, or the filesystem
1933 * one is absent/incompatible.
1934 */
1935 } else if (fs_fw_usable && state == CSIO_DEV_STATE_UNINIT &&
1936 csio_should_install_fs_fw(hw, card_fw_usable,
1937 be32_to_cpu(fs_fw->fw_ver),
1938 be32_to_cpu(card_fw->fw_ver))) {
1939 ret = csio_hw_fw_upgrade(hw, hw->pfn, fw_data,
1940 fw_size, 0);
1941 if (ret != 0) {
1942 csio_err(hw,
1943 "failed to install firmware: %d\n", ret);
1944 goto bye;
1945 }
1946
1947 /* Installed successfully, update the cached header too. */
1948 memcpy(card_fw, fs_fw, sizeof(*card_fw));
1949 card_fw_usable = 1;
1950 *reset = 0; /* already reset as part of load_fw */
1951 }
1952
1953 if (!card_fw_usable) {
1954 uint32_t d, c, k;
1955
1956 d = be32_to_cpu(drv_fw->fw_ver);
1957 c = be32_to_cpu(card_fw->fw_ver);
1958 k = fs_fw ? be32_to_cpu(fs_fw->fw_ver) : 0;
1959
1960 csio_err(hw, "Cannot find a usable firmware: "
1961 "chip state %d, "
1962 "driver compiled with %d.%d.%d.%d, "
1963 "card has %d.%d.%d.%d, filesystem has %d.%d.%d.%d\n",
1964 state,
1965 FW_HDR_FW_VER_MAJOR_G(d), FW_HDR_FW_VER_MINOR_G(d),
1966 FW_HDR_FW_VER_MICRO_G(d), FW_HDR_FW_VER_BUILD_G(d),
1967 FW_HDR_FW_VER_MAJOR_G(c), FW_HDR_FW_VER_MINOR_G(c),
1968 FW_HDR_FW_VER_MICRO_G(c), FW_HDR_FW_VER_BUILD_G(c),
1969 FW_HDR_FW_VER_MAJOR_G(k), FW_HDR_FW_VER_MINOR_G(k),
1970 FW_HDR_FW_VER_MICRO_G(k), FW_HDR_FW_VER_BUILD_G(k));
1971 ret = EINVAL;
1972 goto bye;
1973 }
1974
1975 /* We're using whatever's on the card and it's known to be good. */
1976 hw->fwrev = be32_to_cpu(card_fw->fw_ver);
1977 hw->tp_vers = be32_to_cpu(card_fw->tp_microcode_ver);
1978
1979bye:
1980 return ret;
1981}
1982
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301983/*
1984 * Returns -EINVAL if attempts to flash the firmware failed
1985 * else returns 0,
1986 * if flashing was not attempted because the card had the
1987 * latest firmware ECANCELED is returned
1988 */
1989static int
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05301990csio_hw_flash_fw(struct csio_hw *hw, int *reset)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301991{
1992 int ret = -ECANCELED;
1993 const struct firmware *fw;
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05301994 struct fw_info *fw_info;
1995 struct fw_hdr *card_fw;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05301996 struct pci_dev *pci_dev = hw->pdev;
1997 struct device *dev = &pci_dev->dev ;
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05301998 const u8 *fw_data = NULL;
1999 unsigned int fw_size = 0;
2000
2001 /* This is the firmware whose headers the driver was compiled
2002 * against
2003 */
2004 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(hw->chip_id));
2005 if (fw_info == NULL) {
2006 csio_err(hw,
2007 "unable to get firmware info for chip %d.\n",
2008 CHELSIO_CHIP_VERSION(hw->chip_id));
2009 return -EINVAL;
2010 }
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302011
Praveen Madhavan3fb4c222015-01-16 21:30:19 +05302012 if (request_firmware(&fw, FW_FNAME_T5, dev) < 0) {
Arvind Bhushan7cc16382013-03-14 05:09:08 +00002013 csio_err(hw, "could not find firmware image %s, err: %d\n",
Praveen Madhavan3fb4c222015-01-16 21:30:19 +05302014 FW_FNAME_T5, ret);
Praveen Madhavan216ce692015-01-27 19:46:37 +05302015 } else {
2016 fw_data = fw->data;
2017 fw_size = fw->size;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302018 }
2019
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302020 /* allocate memory to read the header of the firmware on the
2021 * card
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302022 */
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302023 card_fw = kmalloc(sizeof(*card_fw), GFP_KERNEL);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302024
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302025 /* upgrade FW logic */
2026 ret = csio_hw_prep_fw(hw, fw_info, fw_data, fw_size, card_fw,
2027 hw->fw_state, reset);
2028
2029 /* Cleaning up */
2030 if (fw != NULL)
2031 release_firmware(fw);
2032 kfree(card_fw);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302033 return ret;
2034}
2035
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302036/*
2037 * csio_hw_configure - Configure HW
2038 * @hw - HW module
2039 *
2040 */
2041static void
2042csio_hw_configure(struct csio_hw *hw)
2043{
2044 int reset = 1;
2045 int rv;
2046 u32 param[1];
2047
2048 rv = csio_hw_dev_ready(hw);
2049 if (rv != 0) {
2050 CSIO_INC_STATS(hw, n_err_fatal);
2051 csio_post_event(&hw->sm, CSIO_HWE_FATAL);
2052 goto out;
2053 }
2054
2055 /* HW version */
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302056 hw->chip_ver = (char)csio_rd_reg32(hw, PL_REV_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302057
2058 /* Needed for FW download */
2059 rv = csio_hw_get_flash_params(hw);
2060 if (rv != 0) {
2061 csio_err(hw, "Failed to get serial flash params rv:%d\n", rv);
2062 csio_post_event(&hw->sm, CSIO_HWE_FATAL);
2063 goto out;
2064 }
2065
Yijing Wangad4d35f2013-09-05 15:55:26 +08002066 /* Set PCIe completion timeout to 4 seconds */
2067 if (pci_is_pcie(hw->pdev))
2068 pcie_capability_clear_and_set_word(hw->pdev, PCI_EXP_DEVCTL2,
2069 PCI_EXP_DEVCTL2_COMP_TIMEOUT, 0xd);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302070
Arvind Bhushan7cc16382013-03-14 05:09:08 +00002071 hw->chip_ops->chip_set_mem_win(hw, MEMWIN_CSIOSTOR);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302072
2073 rv = csio_hw_get_fw_version(hw, &hw->fwrev);
2074 if (rv != 0)
2075 goto out;
2076
2077 csio_hw_print_fw_version(hw, "Firmware revision");
2078
2079 rv = csio_do_hello(hw, &hw->fw_state);
2080 if (rv != 0) {
2081 CSIO_INC_STATS(hw, n_err_fatal);
2082 csio_post_event(&hw->sm, CSIO_HWE_FATAL);
2083 goto out;
2084 }
2085
2086 /* Read vpd */
2087 rv = csio_hw_get_vpd_params(hw, &hw->vpd);
2088 if (rv != 0)
2089 goto out;
2090
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302091 csio_hw_get_fw_version(hw, &hw->fwrev);
2092 csio_hw_get_tp_version(hw, &hw->tp_vers);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302093 if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302094
2095 /* Do firmware update */
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302096 spin_unlock_irq(&hw->lock);
2097 rv = csio_hw_flash_fw(hw, &reset);
2098 spin_lock_irq(&hw->lock);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302099
Praveen Madhavanf40e74f2015-01-07 19:16:28 +05302100 if (rv != 0)
2101 goto out;
2102
Praveen Madhavan216ce692015-01-27 19:46:37 +05302103 /* If the firmware doesn't support Configuration Files,
2104 * return an error.
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302105 */
Praveen Madhavan216ce692015-01-27 19:46:37 +05302106 rv = csio_hw_check_fwconfig(hw, param);
2107 if (rv != 0) {
2108 csio_info(hw, "Firmware doesn't support "
2109 "Firmware Configuration files\n");
2110 goto out;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302111 }
2112
Praveen Madhavan216ce692015-01-27 19:46:37 +05302113 /* The firmware provides us with a memory buffer where we can
2114 * load a Configuration File from the host if we want to
2115 * override the Configuration File in flash.
2116 */
2117 rv = csio_hw_use_fwconfig(hw, reset, param);
2118 if (rv == -ENOENT) {
2119 csio_info(hw, "Could not initialize "
2120 "adapter, error%d\n", rv);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302121 goto out;
Praveen Madhavan216ce692015-01-27 19:46:37 +05302122 }
2123 if (rv != 0) {
2124 csio_info(hw, "Could not initialize "
2125 "adapter, error%d\n", rv);
2126 goto out;
2127 }
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302128
2129 } else {
2130 if (hw->fw_state == CSIO_DEV_STATE_INIT) {
2131
Arvind Bhushan7cc16382013-03-14 05:09:08 +00002132 hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
2133
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302134 /* device parameters */
2135 rv = csio_get_device_params(hw);
2136 if (rv != 0)
2137 goto out;
2138
2139 /* Get device capabilities */
2140 rv = csio_config_device_caps(hw);
2141 if (rv != 0)
2142 goto out;
2143
2144 /* Configure SGE */
2145 csio_wr_sge_init(hw);
2146
2147 /* Post event to notify completion of configuration */
2148 csio_post_event(&hw->sm, CSIO_HWE_INIT);
2149 goto out;
2150 }
2151 } /* if not master */
2152
2153out:
2154 return;
2155}
2156
2157/*
2158 * csio_hw_initialize - Initialize HW
2159 * @hw - HW module
2160 *
2161 */
2162static void
2163csio_hw_initialize(struct csio_hw *hw)
2164{
2165 struct csio_mb *mbp;
2166 enum fw_retval retval;
2167 int rv;
2168 int i;
2169
2170 if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
2171 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
2172 if (!mbp)
2173 goto out;
2174
2175 csio_mb_initialize(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
2176
2177 if (csio_mb_issue(hw, mbp)) {
2178 csio_err(hw, "Issue of FW_INITIALIZE_CMD failed!\n");
2179 goto free_and_out;
2180 }
2181
2182 retval = csio_mb_fw_retval(mbp);
2183 if (retval != FW_SUCCESS) {
2184 csio_err(hw, "FW_INITIALIZE_CMD returned 0x%x!\n",
2185 retval);
2186 goto free_and_out;
2187 }
2188
2189 mempool_free(mbp, hw->mb_mempool);
2190 }
2191
2192 rv = csio_get_fcoe_resinfo(hw);
2193 if (rv != 0) {
2194 csio_err(hw, "Failed to read fcoe resource info: %d\n", rv);
2195 goto out;
2196 }
2197
2198 spin_unlock_irq(&hw->lock);
2199 rv = csio_config_queues(hw);
2200 spin_lock_irq(&hw->lock);
2201
2202 if (rv != 0) {
2203 csio_err(hw, "Config of queues failed!: %d\n", rv);
2204 goto out;
2205 }
2206
2207 for (i = 0; i < hw->num_pports; i++)
2208 hw->pport[i].mod_type = FW_PORT_MOD_TYPE_NA;
2209
2210 if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
2211 rv = csio_enable_ports(hw);
2212 if (rv != 0) {
2213 csio_err(hw, "Failed to enable ports: %d\n", rv);
2214 goto out;
2215 }
2216 }
2217
2218 csio_post_event(&hw->sm, CSIO_HWE_INIT_DONE);
2219 return;
2220
2221free_and_out:
2222 mempool_free(mbp, hw->mb_mempool);
2223out:
2224 return;
2225}
2226
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302227#define PF_INTR_MASK (PFSW_F | PFCIM_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302228
2229/*
2230 * csio_hw_intr_enable - Enable HW interrupts
2231 * @hw: Pointer to HW module.
2232 *
2233 * Enable interrupts in HW registers.
2234 */
2235static void
2236csio_hw_intr_enable(struct csio_hw *hw)
2237{
2238 uint16_t vec = (uint16_t)csio_get_mb_intr_idx(csio_hw_to_mbm(hw));
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302239 uint32_t pf = SOURCEPF_G(csio_rd_reg32(hw, PL_WHOAMI_A));
2240 uint32_t pl = csio_rd_reg32(hw, PL_INT_ENABLE_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302241
2242 /*
2243 * Set aivec for MSI/MSIX. PCIE_PF_CFG.INTXType is set up
2244 * by FW, so do nothing for INTX.
2245 */
2246 if (hw->intr_mode == CSIO_IM_MSIX)
Hariprasad Shenaif061de42015-01-05 16:30:44 +05302247 csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG_A),
2248 AIVEC_V(AIVEC_M), vec);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302249 else if (hw->intr_mode == CSIO_IM_MSI)
Hariprasad Shenaif061de42015-01-05 16:30:44 +05302250 csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG_A),
2251 AIVEC_V(AIVEC_M), 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302252
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302253 csio_wr_reg32(hw, PF_INTR_MASK, MYPF_REG(PL_PF_INT_ENABLE_A));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302254
2255 /* Turn on MB interrupts - this will internally flush PIO as well */
2256 csio_mb_intr_enable(hw);
2257
2258 /* These are common registers - only a master can modify them */
2259 if (csio_is_hw_master(hw)) {
2260 /*
2261 * Disable the Serial FLASH interrupt, if enabled!
2262 */
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302263 pl &= (~SF_F);
2264 csio_wr_reg32(hw, pl, PL_INT_ENABLE_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302265
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302266 csio_wr_reg32(hw, ERR_CPL_EXCEED_IQE_SIZE_F |
2267 EGRESS_SIZE_ERR_F | ERR_INVALID_CIDX_INC_F |
2268 ERR_CPL_OPCODE_0_F | ERR_DROPPED_DB_F |
2269 ERR_DATA_CPL_ON_HIGH_QID1_F |
2270 ERR_DATA_CPL_ON_HIGH_QID0_F | ERR_BAD_DB_PIDX3_F |
2271 ERR_BAD_DB_PIDX2_F | ERR_BAD_DB_PIDX1_F |
2272 ERR_BAD_DB_PIDX0_F | ERR_ING_CTXT_PRIO_F |
2273 ERR_EGR_CTXT_PRIO_F | INGRESS_SIZE_ERR_F,
2274 SGE_INT_ENABLE3_A);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302275 csio_set_reg_field(hw, PL_INT_MAP0_A, 0, 1 << pf);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302276 }
2277
2278 hw->flags |= CSIO_HWF_HW_INTR_ENABLED;
2279
2280}
2281
2282/*
2283 * csio_hw_intr_disable - Disable HW interrupts
2284 * @hw: Pointer to HW module.
2285 *
2286 * Turn off Mailbox and PCI_PF_CFG interrupts.
2287 */
2288void
2289csio_hw_intr_disable(struct csio_hw *hw)
2290{
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302291 uint32_t pf = SOURCEPF_G(csio_rd_reg32(hw, PL_WHOAMI_A));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302292
2293 if (!(hw->flags & CSIO_HWF_HW_INTR_ENABLED))
2294 return;
2295
2296 hw->flags &= ~CSIO_HWF_HW_INTR_ENABLED;
2297
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302298 csio_wr_reg32(hw, 0, MYPF_REG(PL_PF_INT_ENABLE_A));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302299 if (csio_is_hw_master(hw))
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302300 csio_set_reg_field(hw, PL_INT_MAP0_A, 1 << pf, 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302301
2302 /* Turn off MB interrupts */
2303 csio_mb_intr_disable(hw);
2304
2305}
2306
Arvind Bhushan7cc16382013-03-14 05:09:08 +00002307void
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302308csio_hw_fatal_err(struct csio_hw *hw)
2309{
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302310 csio_set_reg_field(hw, SGE_CONTROL_A, GLOBALENABLE_F, 0);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302311 csio_hw_intr_disable(hw);
2312
2313 /* Do not reset HW, we may need FW state for debugging */
2314 csio_fatal(hw, "HW Fatal error encountered!\n");
2315}
2316
2317/*****************************************************************************/
2318/* START: HW SM */
2319/*****************************************************************************/
2320/*
2321 * csio_hws_uninit - Uninit state
2322 * @hw - HW module
2323 * @evt - Event
2324 *
2325 */
2326static void
2327csio_hws_uninit(struct csio_hw *hw, enum csio_hw_ev evt)
2328{
2329 hw->prev_evt = hw->cur_evt;
2330 hw->cur_evt = evt;
2331 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2332
2333 switch (evt) {
2334 case CSIO_HWE_CFG:
2335 csio_set_state(&hw->sm, csio_hws_configuring);
2336 csio_hw_configure(hw);
2337 break;
2338
2339 default:
2340 CSIO_INC_STATS(hw, n_evt_unexp);
2341 break;
2342 }
2343}
2344
2345/*
2346 * csio_hws_configuring - Configuring state
2347 * @hw - HW module
2348 * @evt - Event
2349 *
2350 */
2351static void
2352csio_hws_configuring(struct csio_hw *hw, enum csio_hw_ev evt)
2353{
2354 hw->prev_evt = hw->cur_evt;
2355 hw->cur_evt = evt;
2356 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2357
2358 switch (evt) {
2359 case CSIO_HWE_INIT:
2360 csio_set_state(&hw->sm, csio_hws_initializing);
2361 csio_hw_initialize(hw);
2362 break;
2363
2364 case CSIO_HWE_INIT_DONE:
2365 csio_set_state(&hw->sm, csio_hws_ready);
2366 /* Fan out event to all lnode SMs */
2367 csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
2368 break;
2369
2370 case CSIO_HWE_FATAL:
2371 csio_set_state(&hw->sm, csio_hws_uninit);
2372 break;
2373
2374 case CSIO_HWE_PCI_REMOVE:
2375 csio_do_bye(hw);
2376 break;
2377 default:
2378 CSIO_INC_STATS(hw, n_evt_unexp);
2379 break;
2380 }
2381}
2382
2383/*
2384 * csio_hws_initializing - Initialiazing state
2385 * @hw - HW module
2386 * @evt - Event
2387 *
2388 */
2389static void
2390csio_hws_initializing(struct csio_hw *hw, enum csio_hw_ev evt)
2391{
2392 hw->prev_evt = hw->cur_evt;
2393 hw->cur_evt = evt;
2394 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2395
2396 switch (evt) {
2397 case CSIO_HWE_INIT_DONE:
2398 csio_set_state(&hw->sm, csio_hws_ready);
2399
2400 /* Fan out event to all lnode SMs */
2401 csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
2402
2403 /* Enable interrupts */
2404 csio_hw_intr_enable(hw);
2405 break;
2406
2407 case CSIO_HWE_FATAL:
2408 csio_set_state(&hw->sm, csio_hws_uninit);
2409 break;
2410
2411 case CSIO_HWE_PCI_REMOVE:
2412 csio_do_bye(hw);
2413 break;
2414
2415 default:
2416 CSIO_INC_STATS(hw, n_evt_unexp);
2417 break;
2418 }
2419}
2420
2421/*
2422 * csio_hws_ready - Ready state
2423 * @hw - HW module
2424 * @evt - Event
2425 *
2426 */
2427static void
2428csio_hws_ready(struct csio_hw *hw, enum csio_hw_ev evt)
2429{
2430 /* Remember the event */
2431 hw->evtflag = evt;
2432
2433 hw->prev_evt = hw->cur_evt;
2434 hw->cur_evt = evt;
2435 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2436
2437 switch (evt) {
2438 case CSIO_HWE_HBA_RESET:
2439 case CSIO_HWE_FW_DLOAD:
2440 case CSIO_HWE_SUSPEND:
2441 case CSIO_HWE_PCI_REMOVE:
2442 case CSIO_HWE_PCIERR_DETECTED:
2443 csio_set_state(&hw->sm, csio_hws_quiescing);
2444 /* cleanup all outstanding cmds */
2445 if (evt == CSIO_HWE_HBA_RESET ||
2446 evt == CSIO_HWE_PCIERR_DETECTED)
2447 csio_scsim_cleanup_io(csio_hw_to_scsim(hw), false);
2448 else
2449 csio_scsim_cleanup_io(csio_hw_to_scsim(hw), true);
2450
2451 csio_hw_intr_disable(hw);
2452 csio_hw_mbm_cleanup(hw);
2453 csio_evtq_stop(hw);
2454 csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWSTOP);
2455 csio_evtq_flush(hw);
2456 csio_mgmtm_cleanup(csio_hw_to_mgmtm(hw));
2457 csio_post_event(&hw->sm, CSIO_HWE_QUIESCED);
2458 break;
2459
2460 case CSIO_HWE_FATAL:
2461 csio_set_state(&hw->sm, csio_hws_uninit);
2462 break;
2463
2464 default:
2465 CSIO_INC_STATS(hw, n_evt_unexp);
2466 break;
2467 }
2468}
2469
2470/*
2471 * csio_hws_quiescing - Quiescing state
2472 * @hw - HW module
2473 * @evt - Event
2474 *
2475 */
2476static void
2477csio_hws_quiescing(struct csio_hw *hw, enum csio_hw_ev evt)
2478{
2479 hw->prev_evt = hw->cur_evt;
2480 hw->cur_evt = evt;
2481 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2482
2483 switch (evt) {
2484 case CSIO_HWE_QUIESCED:
2485 switch (hw->evtflag) {
2486 case CSIO_HWE_FW_DLOAD:
2487 csio_set_state(&hw->sm, csio_hws_resetting);
2488 /* Download firmware */
2489 /* Fall through */
2490
2491 case CSIO_HWE_HBA_RESET:
2492 csio_set_state(&hw->sm, csio_hws_resetting);
2493 /* Start reset of the HBA */
2494 csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWRESET);
2495 csio_wr_destroy_queues(hw, false);
2496 csio_do_reset(hw, false);
2497 csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET_DONE);
2498 break;
2499
2500 case CSIO_HWE_PCI_REMOVE:
2501 csio_set_state(&hw->sm, csio_hws_removing);
2502 csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREMOVE);
2503 csio_wr_destroy_queues(hw, true);
2504 /* Now send the bye command */
2505 csio_do_bye(hw);
2506 break;
2507
2508 case CSIO_HWE_SUSPEND:
2509 csio_set_state(&hw->sm, csio_hws_quiesced);
2510 break;
2511
2512 case CSIO_HWE_PCIERR_DETECTED:
2513 csio_set_state(&hw->sm, csio_hws_pcierr);
2514 csio_wr_destroy_queues(hw, false);
2515 break;
2516
2517 default:
2518 CSIO_INC_STATS(hw, n_evt_unexp);
2519 break;
2520
2521 }
2522 break;
2523
2524 default:
2525 CSIO_INC_STATS(hw, n_evt_unexp);
2526 break;
2527 }
2528}
2529
2530/*
2531 * csio_hws_quiesced - Quiesced state
2532 * @hw - HW module
2533 * @evt - Event
2534 *
2535 */
2536static void
2537csio_hws_quiesced(struct csio_hw *hw, enum csio_hw_ev evt)
2538{
2539 hw->prev_evt = hw->cur_evt;
2540 hw->cur_evt = evt;
2541 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2542
2543 switch (evt) {
2544 case CSIO_HWE_RESUME:
2545 csio_set_state(&hw->sm, csio_hws_configuring);
2546 csio_hw_configure(hw);
2547 break;
2548
2549 default:
2550 CSIO_INC_STATS(hw, n_evt_unexp);
2551 break;
2552 }
2553}
2554
2555/*
2556 * csio_hws_resetting - HW Resetting state
2557 * @hw - HW module
2558 * @evt - Event
2559 *
2560 */
2561static void
2562csio_hws_resetting(struct csio_hw *hw, enum csio_hw_ev evt)
2563{
2564 hw->prev_evt = hw->cur_evt;
2565 hw->cur_evt = evt;
2566 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2567
2568 switch (evt) {
2569 case CSIO_HWE_HBA_RESET_DONE:
2570 csio_evtq_start(hw);
2571 csio_set_state(&hw->sm, csio_hws_configuring);
2572 csio_hw_configure(hw);
2573 break;
2574
2575 default:
2576 CSIO_INC_STATS(hw, n_evt_unexp);
2577 break;
2578 }
2579}
2580
2581/*
2582 * csio_hws_removing - PCI Hotplug removing state
2583 * @hw - HW module
2584 * @evt - Event
2585 *
2586 */
2587static void
2588csio_hws_removing(struct csio_hw *hw, enum csio_hw_ev evt)
2589{
2590 hw->prev_evt = hw->cur_evt;
2591 hw->cur_evt = evt;
2592 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2593
2594 switch (evt) {
2595 case CSIO_HWE_HBA_RESET:
2596 if (!csio_is_hw_master(hw))
2597 break;
2598 /*
2599 * The BYE should have alerady been issued, so we cant
2600 * use the mailbox interface. Hence we use the PL_RST
2601 * register directly.
2602 */
2603 csio_err(hw, "Resetting HW and waiting 2 seconds...\n");
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302604 csio_wr_reg32(hw, PIORSTMODE_F | PIORST_F, PL_RST_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302605 mdelay(2000);
2606 break;
2607
2608 /* Should never receive any new events */
2609 default:
2610 CSIO_INC_STATS(hw, n_evt_unexp);
2611 break;
2612
2613 }
2614}
2615
2616/*
2617 * csio_hws_pcierr - PCI Error state
2618 * @hw - HW module
2619 * @evt - Event
2620 *
2621 */
2622static void
2623csio_hws_pcierr(struct csio_hw *hw, enum csio_hw_ev evt)
2624{
2625 hw->prev_evt = hw->cur_evt;
2626 hw->cur_evt = evt;
2627 CSIO_INC_STATS(hw, n_evt_sm[evt]);
2628
2629 switch (evt) {
2630 case CSIO_HWE_PCIERR_SLOT_RESET:
2631 csio_evtq_start(hw);
2632 csio_set_state(&hw->sm, csio_hws_configuring);
2633 csio_hw_configure(hw);
2634 break;
2635
2636 default:
2637 CSIO_INC_STATS(hw, n_evt_unexp);
2638 break;
2639 }
2640}
2641
2642/*****************************************************************************/
2643/* END: HW SM */
2644/*****************************************************************************/
2645
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302646/*
2647 * csio_handle_intr_status - table driven interrupt handler
2648 * @hw: HW instance
2649 * @reg: the interrupt status register to process
2650 * @acts: table of interrupt actions
2651 *
2652 * A table driven interrupt handler that applies a set of masks to an
2653 * interrupt status word and performs the corresponding actions if the
2654 * interrupts described by the mask have occured. The actions include
2655 * optionally emitting a warning or alert message. The table is terminated
2656 * by an entry specifying mask 0. Returns the number of fatal interrupt
2657 * conditions.
2658 */
Arvind Bhushan7cc16382013-03-14 05:09:08 +00002659int
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302660csio_handle_intr_status(struct csio_hw *hw, unsigned int reg,
2661 const struct intr_info *acts)
2662{
2663 int fatal = 0;
2664 unsigned int mask = 0;
2665 unsigned int status = csio_rd_reg32(hw, reg);
2666
2667 for ( ; acts->mask; ++acts) {
2668 if (!(status & acts->mask))
2669 continue;
2670 if (acts->fatal) {
2671 fatal++;
2672 csio_fatal(hw, "Fatal %s (0x%x)\n",
2673 acts->msg, status & acts->mask);
2674 } else if (acts->msg)
2675 csio_info(hw, "%s (0x%x)\n",
2676 acts->msg, status & acts->mask);
2677 mask |= acts->mask;
2678 }
2679 status &= mask;
2680 if (status) /* clear processed interrupts */
2681 csio_wr_reg32(hw, status, reg);
2682 return fatal;
2683}
2684
2685/*
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302686 * TP interrupt handler.
2687 */
2688static void csio_tp_intr_handler(struct csio_hw *hw)
2689{
2690 static struct intr_info tp_intr_info[] = {
2691 { 0x3fffffff, "TP parity error", -1, 1 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302692 { FLMTXFLSTEMPTY_F, "TP out of Tx pages", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302693 { 0, NULL, 0, 0 }
2694 };
2695
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302696 if (csio_handle_intr_status(hw, TP_INT_CAUSE_A, tp_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302697 csio_hw_fatal_err(hw);
2698}
2699
2700/*
2701 * SGE interrupt handler.
2702 */
2703static void csio_sge_intr_handler(struct csio_hw *hw)
2704{
2705 uint64_t v;
2706
2707 static struct intr_info sge_intr_info[] = {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302708 { ERR_CPL_EXCEED_IQE_SIZE_F,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302709 "SGE received CPL exceeding IQE size", -1, 1 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302710 { ERR_INVALID_CIDX_INC_F,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302711 "SGE GTS CIDX increment too large", -1, 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302712 { ERR_CPL_OPCODE_0_F, "SGE received 0-length CPL", -1, 0 },
2713 { ERR_DROPPED_DB_F, "SGE doorbell dropped", -1, 0 },
2714 { ERR_DATA_CPL_ON_HIGH_QID1_F | ERR_DATA_CPL_ON_HIGH_QID0_F,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302715 "SGE IQID > 1023 received CPL for FL", -1, 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302716 { ERR_BAD_DB_PIDX3_F, "SGE DBP 3 pidx increment too large", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302717 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302718 { ERR_BAD_DB_PIDX2_F, "SGE DBP 2 pidx increment too large", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302719 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302720 { ERR_BAD_DB_PIDX1_F, "SGE DBP 1 pidx increment too large", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302721 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302722 { ERR_BAD_DB_PIDX0_F, "SGE DBP 0 pidx increment too large", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302723 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302724 { ERR_ING_CTXT_PRIO_F,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302725 "SGE too many priority ingress contexts", -1, 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302726 { ERR_EGR_CTXT_PRIO_F,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302727 "SGE too many priority egress contexts", -1, 0 },
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302728 { INGRESS_SIZE_ERR_F, "SGE illegal ingress QID", -1, 0 },
2729 { EGRESS_SIZE_ERR_F, "SGE illegal egress QID", -1, 0 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302730 { 0, NULL, 0, 0 }
2731 };
2732
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302733 v = (uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE1_A) |
2734 ((uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE2_A) << 32);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302735 if (v) {
2736 csio_fatal(hw, "SGE parity error (%#llx)\n",
2737 (unsigned long long)v);
2738 csio_wr_reg32(hw, (uint32_t)(v & 0xFFFFFFFF),
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302739 SGE_INT_CAUSE1_A);
2740 csio_wr_reg32(hw, (uint32_t)(v >> 32), SGE_INT_CAUSE2_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302741 }
2742
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302743 v |= csio_handle_intr_status(hw, SGE_INT_CAUSE3_A, sge_intr_info);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302744
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302745 if (csio_handle_intr_status(hw, SGE_INT_CAUSE3_A, sge_intr_info) ||
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302746 v != 0)
2747 csio_hw_fatal_err(hw);
2748}
2749
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05302750#define CIM_OBQ_INTR (OBQULP0PARERR_F | OBQULP1PARERR_F | OBQULP2PARERR_F |\
2751 OBQULP3PARERR_F | OBQSGEPARERR_F | OBQNCSIPARERR_F)
2752#define CIM_IBQ_INTR (IBQTP0PARERR_F | IBQTP1PARERR_F | IBQULPPARERR_F |\
2753 IBQSGEHIPARERR_F | IBQSGELOPARERR_F | IBQNCSIPARERR_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302754
2755/*
2756 * CIM interrupt handler.
2757 */
2758static void csio_cim_intr_handler(struct csio_hw *hw)
2759{
2760 static struct intr_info cim_intr_info[] = {
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05302761 { PREFDROPINT_F, "CIM control register prefetch drop", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302762 { CIM_OBQ_INTR, "CIM OBQ parity error", -1, 1 },
2763 { CIM_IBQ_INTR, "CIM IBQ parity error", -1, 1 },
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05302764 { MBUPPARERR_F, "CIM mailbox uP parity error", -1, 1 },
2765 { MBHOSTPARERR_F, "CIM mailbox host parity error", -1, 1 },
2766 { TIEQINPARERRINT_F, "CIM TIEQ outgoing parity error", -1, 1 },
2767 { TIEQOUTPARERRINT_F, "CIM TIEQ incoming parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302768 { 0, NULL, 0, 0 }
2769 };
2770 static struct intr_info cim_upintr_info[] = {
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05302771 { RSVDSPACEINT_F, "CIM reserved space access", -1, 1 },
2772 { ILLTRANSINT_F, "CIM illegal transaction", -1, 1 },
2773 { ILLWRINT_F, "CIM illegal write", -1, 1 },
2774 { ILLRDINT_F, "CIM illegal read", -1, 1 },
2775 { ILLRDBEINT_F, "CIM illegal read BE", -1, 1 },
2776 { ILLWRBEINT_F, "CIM illegal write BE", -1, 1 },
2777 { SGLRDBOOTINT_F, "CIM single read from boot space", -1, 1 },
2778 { SGLWRBOOTINT_F, "CIM single write to boot space", -1, 1 },
2779 { BLKWRBOOTINT_F, "CIM block write to boot space", -1, 1 },
2780 { SGLRDFLASHINT_F, "CIM single read from flash space", -1, 1 },
2781 { SGLWRFLASHINT_F, "CIM single write to flash space", -1, 1 },
2782 { BLKWRFLASHINT_F, "CIM block write to flash space", -1, 1 },
2783 { SGLRDEEPROMINT_F, "CIM single EEPROM read", -1, 1 },
2784 { SGLWREEPROMINT_F, "CIM single EEPROM write", -1, 1 },
2785 { BLKRDEEPROMINT_F, "CIM block EEPROM read", -1, 1 },
2786 { BLKWREEPROMINT_F, "CIM block EEPROM write", -1, 1 },
2787 { SGLRDCTLINT_F, "CIM single read from CTL space", -1, 1 },
2788 { SGLWRCTLINT_F, "CIM single write to CTL space", -1, 1 },
2789 { BLKRDCTLINT_F, "CIM block read from CTL space", -1, 1 },
2790 { BLKWRCTLINT_F, "CIM block write to CTL space", -1, 1 },
2791 { SGLRDPLINT_F, "CIM single read from PL space", -1, 1 },
2792 { SGLWRPLINT_F, "CIM single write to PL space", -1, 1 },
2793 { BLKRDPLINT_F, "CIM block read from PL space", -1, 1 },
2794 { BLKWRPLINT_F, "CIM block write to PL space", -1, 1 },
2795 { REQOVRLOOKUPINT_F, "CIM request FIFO overwrite", -1, 1 },
2796 { RSPOVRLOOKUPINT_F, "CIM response FIFO overwrite", -1, 1 },
2797 { TIMEOUTINT_F, "CIM PIF timeout", -1, 1 },
2798 { TIMEOUTMAINT_F, "CIM PIF MA timeout", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302799 { 0, NULL, 0, 0 }
2800 };
2801
2802 int fat;
2803
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05302804 fat = csio_handle_intr_status(hw, CIM_HOST_INT_CAUSE_A,
2805 cim_intr_info) +
2806 csio_handle_intr_status(hw, CIM_HOST_UPACC_INT_CAUSE_A,
2807 cim_upintr_info);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302808 if (fat)
2809 csio_hw_fatal_err(hw);
2810}
2811
2812/*
2813 * ULP RX interrupt handler.
2814 */
2815static void csio_ulprx_intr_handler(struct csio_hw *hw)
2816{
2817 static struct intr_info ulprx_intr_info[] = {
2818 { 0x1800000, "ULPRX context error", -1, 1 },
2819 { 0x7fffff, "ULPRX parity error", -1, 1 },
2820 { 0, NULL, 0, 0 }
2821 };
2822
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302823 if (csio_handle_intr_status(hw, ULP_RX_INT_CAUSE_A, ulprx_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302824 csio_hw_fatal_err(hw);
2825}
2826
2827/*
2828 * ULP TX interrupt handler.
2829 */
2830static void csio_ulptx_intr_handler(struct csio_hw *hw)
2831{
2832 static struct intr_info ulptx_intr_info[] = {
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302833 { PBL_BOUND_ERR_CH3_F, "ULPTX channel 3 PBL out of bounds", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302834 0 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302835 { PBL_BOUND_ERR_CH2_F, "ULPTX channel 2 PBL out of bounds", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302836 0 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302837 { PBL_BOUND_ERR_CH1_F, "ULPTX channel 1 PBL out of bounds", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302838 0 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302839 { PBL_BOUND_ERR_CH0_F, "ULPTX channel 0 PBL out of bounds", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302840 0 },
2841 { 0xfffffff, "ULPTX parity error", -1, 1 },
2842 { 0, NULL, 0, 0 }
2843 };
2844
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302845 if (csio_handle_intr_status(hw, ULP_TX_INT_CAUSE_A, ulptx_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302846 csio_hw_fatal_err(hw);
2847}
2848
2849/*
2850 * PM TX interrupt handler.
2851 */
2852static void csio_pmtx_intr_handler(struct csio_hw *hw)
2853{
2854 static struct intr_info pmtx_intr_info[] = {
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302855 { PCMD_LEN_OVFL0_F, "PMTX channel 0 pcmd too large", -1, 1 },
2856 { PCMD_LEN_OVFL1_F, "PMTX channel 1 pcmd too large", -1, 1 },
2857 { PCMD_LEN_OVFL2_F, "PMTX channel 2 pcmd too large", -1, 1 },
2858 { ZERO_C_CMD_ERROR_F, "PMTX 0-length pcmd", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302859 { 0xffffff0, "PMTX framing error", -1, 1 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302860 { OESPI_PAR_ERROR_F, "PMTX oespi parity error", -1, 1 },
2861 { DB_OPTIONS_PAR_ERROR_F, "PMTX db_options parity error", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302862 1 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302863 { ICSPI_PAR_ERROR_F, "PMTX icspi parity error", -1, 1 },
2864 { PMTX_C_PCMD_PAR_ERROR_F, "PMTX c_pcmd parity error", -1, 1},
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302865 { 0, NULL, 0, 0 }
2866 };
2867
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302868 if (csio_handle_intr_status(hw, PM_TX_INT_CAUSE_A, pmtx_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302869 csio_hw_fatal_err(hw);
2870}
2871
2872/*
2873 * PM RX interrupt handler.
2874 */
2875static void csio_pmrx_intr_handler(struct csio_hw *hw)
2876{
2877 static struct intr_info pmrx_intr_info[] = {
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302878 { ZERO_E_CMD_ERROR_F, "PMRX 0-length pcmd", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302879 { 0x3ffff0, "PMRX framing error", -1, 1 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302880 { OCSPI_PAR_ERROR_F, "PMRX ocspi parity error", -1, 1 },
2881 { DB_OPTIONS_PAR_ERROR_F, "PMRX db_options parity error", -1,
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302882 1 },
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302883 { IESPI_PAR_ERROR_F, "PMRX iespi parity error", -1, 1 },
2884 { PMRX_E_PCMD_PAR_ERROR_F, "PMRX e_pcmd parity error", -1, 1},
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302885 { 0, NULL, 0, 0 }
2886 };
2887
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302888 if (csio_handle_intr_status(hw, PM_RX_INT_CAUSE_A, pmrx_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302889 csio_hw_fatal_err(hw);
2890}
2891
2892/*
2893 * CPL switch interrupt handler.
2894 */
2895static void csio_cplsw_intr_handler(struct csio_hw *hw)
2896{
2897 static struct intr_info cplsw_intr_info[] = {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302898 { CIM_OP_MAP_PERR_F, "CPLSW CIM op_map parity error", -1, 1 },
2899 { CIM_OVFL_ERROR_F, "CPLSW CIM overflow", -1, 1 },
2900 { TP_FRAMING_ERROR_F, "CPLSW TP framing error", -1, 1 },
2901 { SGE_FRAMING_ERROR_F, "CPLSW SGE framing error", -1, 1 },
2902 { CIM_FRAMING_ERROR_F, "CPLSW CIM framing error", -1, 1 },
2903 { ZERO_SWITCH_ERROR_F, "CPLSW no-switch error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302904 { 0, NULL, 0, 0 }
2905 };
2906
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302907 if (csio_handle_intr_status(hw, CPL_INTR_CAUSE_A, cplsw_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302908 csio_hw_fatal_err(hw);
2909}
2910
2911/*
2912 * LE interrupt handler.
2913 */
2914static void csio_le_intr_handler(struct csio_hw *hw)
2915{
2916 static struct intr_info le_intr_info[] = {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302917 { LIPMISS_F, "LE LIP miss", -1, 0 },
2918 { LIP0_F, "LE 0 LIP error", -1, 0 },
2919 { PARITYERR_F, "LE parity error", -1, 1 },
2920 { UNKNOWNCMD_F, "LE unknown command", -1, 1 },
2921 { REQQPARERR_F, "LE request queue parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302922 { 0, NULL, 0, 0 }
2923 };
2924
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302925 if (csio_handle_intr_status(hw, LE_DB_INT_CAUSE_A, le_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302926 csio_hw_fatal_err(hw);
2927}
2928
2929/*
2930 * MPS interrupt handler.
2931 */
2932static void csio_mps_intr_handler(struct csio_hw *hw)
2933{
2934 static struct intr_info mps_rx_intr_info[] = {
2935 { 0xffffff, "MPS Rx parity error", -1, 1 },
2936 { 0, NULL, 0, 0 }
2937 };
2938 static struct intr_info mps_tx_intr_info[] = {
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302939 { TPFIFO_V(TPFIFO_M), "MPS Tx TP FIFO parity error", -1, 1 },
2940 { NCSIFIFO_F, "MPS Tx NC-SI FIFO parity error", -1, 1 },
2941 { TXDATAFIFO_V(TXDATAFIFO_M), "MPS Tx data FIFO parity error",
2942 -1, 1 },
2943 { TXDESCFIFO_V(TXDESCFIFO_M), "MPS Tx desc FIFO parity error",
2944 -1, 1 },
2945 { BUBBLE_F, "MPS Tx underflow", -1, 1 },
2946 { SECNTERR_F, "MPS Tx SOP/EOP error", -1, 1 },
2947 { FRMERR_F, "MPS Tx framing error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302948 { 0, NULL, 0, 0 }
2949 };
2950 static struct intr_info mps_trc_intr_info[] = {
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302951 { FILTMEM_V(FILTMEM_M), "MPS TRC filter parity error", -1, 1 },
2952 { PKTFIFO_V(PKTFIFO_M), "MPS TRC packet FIFO parity error",
2953 -1, 1 },
2954 { MISCPERR_F, "MPS TRC misc parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302955 { 0, NULL, 0, 0 }
2956 };
2957 static struct intr_info mps_stat_sram_intr_info[] = {
2958 { 0x1fffff, "MPS statistics SRAM parity error", -1, 1 },
2959 { 0, NULL, 0, 0 }
2960 };
2961 static struct intr_info mps_stat_tx_intr_info[] = {
2962 { 0xfffff, "MPS statistics Tx FIFO parity error", -1, 1 },
2963 { 0, NULL, 0, 0 }
2964 };
2965 static struct intr_info mps_stat_rx_intr_info[] = {
2966 { 0xffffff, "MPS statistics Rx FIFO parity error", -1, 1 },
2967 { 0, NULL, 0, 0 }
2968 };
2969 static struct intr_info mps_cls_intr_info[] = {
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302970 { MATCHSRAM_F, "MPS match SRAM parity error", -1, 1 },
2971 { MATCHTCAM_F, "MPS match TCAM parity error", -1, 1 },
2972 { HASHSRAM_F, "MPS hash SRAM parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302973 { 0, NULL, 0, 0 }
2974 };
2975
2976 int fat;
2977
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302978 fat = csio_handle_intr_status(hw, MPS_RX_PERR_INT_CAUSE_A,
2979 mps_rx_intr_info) +
2980 csio_handle_intr_status(hw, MPS_TX_INT_CAUSE_A,
2981 mps_tx_intr_info) +
2982 csio_handle_intr_status(hw, MPS_TRC_INT_CAUSE_A,
2983 mps_trc_intr_info) +
2984 csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_SRAM_A,
2985 mps_stat_sram_intr_info) +
2986 csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_TX_FIFO_A,
2987 mps_stat_tx_intr_info) +
2988 csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_RX_FIFO_A,
2989 mps_stat_rx_intr_info) +
2990 csio_handle_intr_status(hw, MPS_CLS_INT_CAUSE_A,
2991 mps_cls_intr_info);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302992
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302993 csio_wr_reg32(hw, 0, MPS_INT_CAUSE_A);
2994 csio_rd_reg32(hw, MPS_INT_CAUSE_A); /* flush */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05302995 if (fat)
2996 csio_hw_fatal_err(hw);
2997}
2998
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05302999#define MEM_INT_MASK (PERR_INT_CAUSE_F | ECC_CE_INT_CAUSE_F | \
3000 ECC_UE_INT_CAUSE_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303001
3002/*
3003 * EDC/MC interrupt handler.
3004 */
3005static void csio_mem_intr_handler(struct csio_hw *hw, int idx)
3006{
3007 static const char name[3][5] = { "EDC0", "EDC1", "MC" };
3008
3009 unsigned int addr, cnt_addr, v;
3010
3011 if (idx <= MEM_EDC1) {
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303012 addr = EDC_REG(EDC_INT_CAUSE_A, idx);
3013 cnt_addr = EDC_REG(EDC_ECC_STATUS_A, idx);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303014 } else {
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303015 addr = MC_INT_CAUSE_A;
3016 cnt_addr = MC_ECC_STATUS_A;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303017 }
3018
3019 v = csio_rd_reg32(hw, addr) & MEM_INT_MASK;
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303020 if (v & PERR_INT_CAUSE_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303021 csio_fatal(hw, "%s FIFO parity error\n", name[idx]);
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303022 if (v & ECC_CE_INT_CAUSE_F) {
3023 uint32_t cnt = ECC_CECNT_G(csio_rd_reg32(hw, cnt_addr));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303024
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303025 csio_wr_reg32(hw, ECC_CECNT_V(ECC_CECNT_M), cnt_addr);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303026 csio_warn(hw, "%u %s correctable ECC data error%s\n",
3027 cnt, name[idx], cnt > 1 ? "s" : "");
3028 }
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303029 if (v & ECC_UE_INT_CAUSE_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303030 csio_fatal(hw, "%s uncorrectable ECC data error\n", name[idx]);
3031
3032 csio_wr_reg32(hw, v, addr);
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303033 if (v & (PERR_INT_CAUSE_F | ECC_UE_INT_CAUSE_F))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303034 csio_hw_fatal_err(hw);
3035}
3036
3037/*
3038 * MA interrupt handler.
3039 */
3040static void csio_ma_intr_handler(struct csio_hw *hw)
3041{
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303042 uint32_t v, status = csio_rd_reg32(hw, MA_INT_CAUSE_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303043
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303044 if (status & MEM_PERR_INT_CAUSE_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303045 csio_fatal(hw, "MA parity error, parity status %#x\n",
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303046 csio_rd_reg32(hw, MA_PARITY_ERROR_STATUS_A));
3047 if (status & MEM_WRAP_INT_CAUSE_F) {
3048 v = csio_rd_reg32(hw, MA_INT_WRAP_STATUS_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303049 csio_fatal(hw,
3050 "MA address wrap-around error by client %u to address %#x\n",
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303051 MEM_WRAP_CLIENT_NUM_G(v), MEM_WRAP_ADDRESS_G(v) << 4);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303052 }
Hariprasad Shenai89c3a862015-01-05 16:30:45 +05303053 csio_wr_reg32(hw, status, MA_INT_CAUSE_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303054 csio_hw_fatal_err(hw);
3055}
3056
3057/*
3058 * SMB interrupt handler.
3059 */
3060static void csio_smb_intr_handler(struct csio_hw *hw)
3061{
3062 static struct intr_info smb_intr_info[] = {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303063 { MSTTXFIFOPARINT_F, "SMB master Tx FIFO parity error", -1, 1 },
3064 { MSTRXFIFOPARINT_F, "SMB master Rx FIFO parity error", -1, 1 },
3065 { SLVFIFOPARINT_F, "SMB slave FIFO parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303066 { 0, NULL, 0, 0 }
3067 };
3068
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303069 if (csio_handle_intr_status(hw, SMB_INT_CAUSE_A, smb_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303070 csio_hw_fatal_err(hw);
3071}
3072
3073/*
3074 * NC-SI interrupt handler.
3075 */
3076static void csio_ncsi_intr_handler(struct csio_hw *hw)
3077{
3078 static struct intr_info ncsi_intr_info[] = {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303079 { CIM_DM_PRTY_ERR_F, "NC-SI CIM parity error", -1, 1 },
3080 { MPS_DM_PRTY_ERR_F, "NC-SI MPS parity error", -1, 1 },
3081 { TXFIFO_PRTY_ERR_F, "NC-SI Tx FIFO parity error", -1, 1 },
3082 { RXFIFO_PRTY_ERR_F, "NC-SI Rx FIFO parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303083 { 0, NULL, 0, 0 }
3084 };
3085
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303086 if (csio_handle_intr_status(hw, NCSI_INT_CAUSE_A, ncsi_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303087 csio_hw_fatal_err(hw);
3088}
3089
3090/*
3091 * XGMAC interrupt handler.
3092 */
3093static void csio_xgmac_intr_handler(struct csio_hw *hw, int port)
3094{
Praveen Madhavan3fb4c222015-01-16 21:30:19 +05303095 uint32_t v = csio_rd_reg32(hw, T5_PORT_REG(port, MAC_PORT_INT_CAUSE_A));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303096
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303097 v &= TXFIFO_PRTY_ERR_F | RXFIFO_PRTY_ERR_F;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303098 if (!v)
3099 return;
3100
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303101 if (v & TXFIFO_PRTY_ERR_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303102 csio_fatal(hw, "XGMAC %d Tx FIFO parity error\n", port);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303103 if (v & RXFIFO_PRTY_ERR_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303104 csio_fatal(hw, "XGMAC %d Rx FIFO parity error\n", port);
Praveen Madhavan3fb4c222015-01-16 21:30:19 +05303105 csio_wr_reg32(hw, v, T5_PORT_REG(port, MAC_PORT_INT_CAUSE_A));
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303106 csio_hw_fatal_err(hw);
3107}
3108
3109/*
3110 * PL interrupt handler.
3111 */
3112static void csio_pl_intr_handler(struct csio_hw *hw)
3113{
3114 static struct intr_info pl_intr_info[] = {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303115 { FATALPERR_F, "T4 fatal parity error", -1, 1 },
3116 { PERRVFID_F, "PL VFID_MAP parity error", -1, 1 },
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303117 { 0, NULL, 0, 0 }
3118 };
3119
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303120 if (csio_handle_intr_status(hw, PL_PL_INT_CAUSE_A, pl_intr_info))
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303121 csio_hw_fatal_err(hw);
3122}
3123
3124/*
3125 * csio_hw_slow_intr_handler - control path interrupt handler
3126 * @hw: HW module
3127 *
3128 * Interrupt handler for non-data global interrupt events, e.g., errors.
3129 * The designation 'slow' is because it involves register reads, while
3130 * data interrupts typically don't involve any MMIOs.
3131 */
3132int
3133csio_hw_slow_intr_handler(struct csio_hw *hw)
3134{
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303135 uint32_t cause = csio_rd_reg32(hw, PL_INT_CAUSE_A);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303136
3137 if (!(cause & CSIO_GLBL_INTR_MASK)) {
3138 CSIO_INC_STATS(hw, n_plint_unexp);
3139 return 0;
3140 }
3141
3142 csio_dbg(hw, "Slow interrupt! cause: 0x%x\n", cause);
3143
3144 CSIO_INC_STATS(hw, n_plint_cnt);
3145
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303146 if (cause & CIM_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303147 csio_cim_intr_handler(hw);
3148
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303149 if (cause & MPS_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303150 csio_mps_intr_handler(hw);
3151
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303152 if (cause & NCSI_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303153 csio_ncsi_intr_handler(hw);
3154
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303155 if (cause & PL_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303156 csio_pl_intr_handler(hw);
3157
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303158 if (cause & SMB_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303159 csio_smb_intr_handler(hw);
3160
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303161 if (cause & XGMAC0_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303162 csio_xgmac_intr_handler(hw, 0);
3163
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303164 if (cause & XGMAC1_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303165 csio_xgmac_intr_handler(hw, 1);
3166
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303167 if (cause & XGMAC_KR0_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303168 csio_xgmac_intr_handler(hw, 2);
3169
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303170 if (cause & XGMAC_KR1_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303171 csio_xgmac_intr_handler(hw, 3);
3172
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303173 if (cause & PCIE_F)
Arvind Bhushan7cc16382013-03-14 05:09:08 +00003174 hw->chip_ops->chip_pcie_intr_handler(hw);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303175
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303176 if (cause & MC_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303177 csio_mem_intr_handler(hw, MEM_MC);
3178
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303179 if (cause & EDC0_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303180 csio_mem_intr_handler(hw, MEM_EDC0);
3181
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303182 if (cause & EDC1_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303183 csio_mem_intr_handler(hw, MEM_EDC1);
3184
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303185 if (cause & LE_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303186 csio_le_intr_handler(hw);
3187
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303188 if (cause & TP_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303189 csio_tp_intr_handler(hw);
3190
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303191 if (cause & MA_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303192 csio_ma_intr_handler(hw);
3193
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303194 if (cause & PM_TX_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303195 csio_pmtx_intr_handler(hw);
3196
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303197 if (cause & PM_RX_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303198 csio_pmrx_intr_handler(hw);
3199
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303200 if (cause & ULP_RX_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303201 csio_ulprx_intr_handler(hw);
3202
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303203 if (cause & CPL_SWITCH_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303204 csio_cplsw_intr_handler(hw);
3205
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303206 if (cause & SGE_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303207 csio_sge_intr_handler(hw);
3208
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303209 if (cause & ULP_TX_F)
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303210 csio_ulptx_intr_handler(hw);
3211
3212 /* Clear the interrupts just processed for which we are the master. */
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303213 csio_wr_reg32(hw, cause & CSIO_GLBL_INTR_MASK, PL_INT_CAUSE_A);
3214 csio_rd_reg32(hw, PL_INT_CAUSE_A); /* flush */
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303215
3216 return 1;
3217}
3218
3219/*****************************************************************************
3220 * HW <--> mailbox interfacing routines.
3221 ****************************************************************************/
3222/*
3223 * csio_mberr_worker - Worker thread (dpc) for mailbox/error completions
3224 *
3225 * @data: Private data pointer.
3226 *
3227 * Called from worker thread context.
3228 */
3229static void
3230csio_mberr_worker(void *data)
3231{
3232 struct csio_hw *hw = (struct csio_hw *)data;
3233 struct csio_mbm *mbm = &hw->mbm;
3234 LIST_HEAD(cbfn_q);
3235 struct csio_mb *mbp_next;
3236 int rv;
3237
3238 del_timer_sync(&mbm->timer);
3239
3240 spin_lock_irq(&hw->lock);
3241 if (list_empty(&mbm->cbfn_q)) {
3242 spin_unlock_irq(&hw->lock);
3243 return;
3244 }
3245
3246 list_splice_tail_init(&mbm->cbfn_q, &cbfn_q);
3247 mbm->stats.n_cbfnq = 0;
3248
3249 /* Try to start waiting mailboxes */
3250 if (!list_empty(&mbm->req_q)) {
3251 mbp_next = list_first_entry(&mbm->req_q, struct csio_mb, list);
3252 list_del_init(&mbp_next->list);
3253
3254 rv = csio_mb_issue(hw, mbp_next);
3255 if (rv != 0)
3256 list_add_tail(&mbp_next->list, &mbm->req_q);
3257 else
3258 CSIO_DEC_STATS(mbm, n_activeq);
3259 }
3260 spin_unlock_irq(&hw->lock);
3261
3262 /* Now callback completions */
3263 csio_mb_completions(hw, &cbfn_q);
3264}
3265
3266/*
3267 * csio_hw_mb_timer - Top-level Mailbox timeout handler.
3268 *
3269 * @data: private data pointer
3270 *
3271 **/
3272static void
3273csio_hw_mb_timer(uintptr_t data)
3274{
3275 struct csio_hw *hw = (struct csio_hw *)data;
3276 struct csio_mb *mbp = NULL;
3277
3278 spin_lock_irq(&hw->lock);
3279 mbp = csio_mb_tmo_handler(hw);
3280 spin_unlock_irq(&hw->lock);
3281
3282 /* Call back the function for the timed-out Mailbox */
3283 if (mbp)
3284 mbp->mb_cbfn(hw, mbp);
3285
3286}
3287
3288/*
3289 * csio_hw_mbm_cleanup - Cleanup Mailbox module.
3290 * @hw: HW module
3291 *
3292 * Called with lock held, should exit with lock held.
3293 * Cancels outstanding mailboxes (waiting, in-flight) and gathers them
3294 * into a local queue. Drops lock and calls the completions. Holds
3295 * lock and returns.
3296 */
3297static void
3298csio_hw_mbm_cleanup(struct csio_hw *hw)
3299{
3300 LIST_HEAD(cbfn_q);
3301
3302 csio_mb_cancel_all(hw, &cbfn_q);
3303
3304 spin_unlock_irq(&hw->lock);
3305 csio_mb_completions(hw, &cbfn_q);
3306 spin_lock_irq(&hw->lock);
3307}
3308
3309/*****************************************************************************
3310 * Event handling
3311 ****************************************************************************/
3312int
3313csio_enqueue_evt(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
3314 uint16_t len)
3315{
3316 struct csio_evt_msg *evt_entry = NULL;
3317
3318 if (type >= CSIO_EVT_MAX)
3319 return -EINVAL;
3320
3321 if (len > CSIO_EVT_MSG_SIZE)
3322 return -EINVAL;
3323
3324 if (hw->flags & CSIO_HWF_FWEVT_STOP)
3325 return -EINVAL;
3326
3327 if (list_empty(&hw->evt_free_q)) {
3328 csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
3329 type, len);
3330 return -ENOMEM;
3331 }
3332
3333 evt_entry = list_first_entry(&hw->evt_free_q,
3334 struct csio_evt_msg, list);
3335 list_del_init(&evt_entry->list);
3336
3337 /* copy event msg and queue the event */
3338 evt_entry->type = type;
3339 memcpy((void *)evt_entry->data, evt_msg, len);
3340 list_add_tail(&evt_entry->list, &hw->evt_active_q);
3341
3342 CSIO_DEC_STATS(hw, n_evt_freeq);
3343 CSIO_INC_STATS(hw, n_evt_activeq);
3344
3345 return 0;
3346}
3347
3348static int
3349csio_enqueue_evt_lock(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
3350 uint16_t len, bool msg_sg)
3351{
3352 struct csio_evt_msg *evt_entry = NULL;
3353 struct csio_fl_dma_buf *fl_sg;
3354 uint32_t off = 0;
3355 unsigned long flags;
3356 int n, ret = 0;
3357
3358 if (type >= CSIO_EVT_MAX)
3359 return -EINVAL;
3360
3361 if (len > CSIO_EVT_MSG_SIZE)
3362 return -EINVAL;
3363
3364 spin_lock_irqsave(&hw->lock, flags);
3365 if (hw->flags & CSIO_HWF_FWEVT_STOP) {
3366 ret = -EINVAL;
3367 goto out;
3368 }
3369
3370 if (list_empty(&hw->evt_free_q)) {
3371 csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
3372 type, len);
3373 ret = -ENOMEM;
3374 goto out;
3375 }
3376
3377 evt_entry = list_first_entry(&hw->evt_free_q,
3378 struct csio_evt_msg, list);
3379 list_del_init(&evt_entry->list);
3380
3381 /* copy event msg and queue the event */
3382 evt_entry->type = type;
3383
3384 /* If Payload in SG list*/
3385 if (msg_sg) {
3386 fl_sg = (struct csio_fl_dma_buf *) evt_msg;
3387 for (n = 0; (n < CSIO_MAX_FLBUF_PER_IQWR && off < len); n++) {
3388 memcpy((void *)((uintptr_t)evt_entry->data + off),
3389 fl_sg->flbufs[n].vaddr,
3390 fl_sg->flbufs[n].len);
3391 off += fl_sg->flbufs[n].len;
3392 }
3393 } else
3394 memcpy((void *)evt_entry->data, evt_msg, len);
3395
3396 list_add_tail(&evt_entry->list, &hw->evt_active_q);
3397 CSIO_DEC_STATS(hw, n_evt_freeq);
3398 CSIO_INC_STATS(hw, n_evt_activeq);
3399out:
3400 spin_unlock_irqrestore(&hw->lock, flags);
3401 return ret;
3402}
3403
3404static void
3405csio_free_evt(struct csio_hw *hw, struct csio_evt_msg *evt_entry)
3406{
3407 if (evt_entry) {
3408 spin_lock_irq(&hw->lock);
3409 list_del_init(&evt_entry->list);
3410 list_add_tail(&evt_entry->list, &hw->evt_free_q);
3411 CSIO_DEC_STATS(hw, n_evt_activeq);
3412 CSIO_INC_STATS(hw, n_evt_freeq);
3413 spin_unlock_irq(&hw->lock);
3414 }
3415}
3416
3417void
3418csio_evtq_flush(struct csio_hw *hw)
3419{
3420 uint32_t count;
3421 count = 30;
3422 while (hw->flags & CSIO_HWF_FWEVT_PENDING && count--) {
3423 spin_unlock_irq(&hw->lock);
3424 msleep(2000);
3425 spin_lock_irq(&hw->lock);
3426 }
3427
3428 CSIO_DB_ASSERT(!(hw->flags & CSIO_HWF_FWEVT_PENDING));
3429}
3430
3431static void
3432csio_evtq_stop(struct csio_hw *hw)
3433{
3434 hw->flags |= CSIO_HWF_FWEVT_STOP;
3435}
3436
3437static void
3438csio_evtq_start(struct csio_hw *hw)
3439{
3440 hw->flags &= ~CSIO_HWF_FWEVT_STOP;
3441}
3442
3443static void
3444csio_evtq_cleanup(struct csio_hw *hw)
3445{
3446 struct list_head *evt_entry, *next_entry;
3447
3448 /* Release outstanding events from activeq to freeq*/
3449 if (!list_empty(&hw->evt_active_q))
3450 list_splice_tail_init(&hw->evt_active_q, &hw->evt_free_q);
3451
3452 hw->stats.n_evt_activeq = 0;
3453 hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
3454
3455 /* Freeup event entry */
3456 list_for_each_safe(evt_entry, next_entry, &hw->evt_free_q) {
3457 kfree(evt_entry);
3458 CSIO_DEC_STATS(hw, n_evt_freeq);
3459 }
3460
3461 hw->stats.n_evt_freeq = 0;
3462}
3463
3464
3465static void
3466csio_process_fwevtq_entry(struct csio_hw *hw, void *wr, uint32_t len,
3467 struct csio_fl_dma_buf *flb, void *priv)
3468{
3469 __u8 op;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303470 void *msg = NULL;
3471 uint32_t msg_len = 0;
3472 bool msg_sg = 0;
3473
3474 op = ((struct rss_header *) wr)->opcode;
3475 if (op == CPL_FW6_PLD) {
3476 CSIO_INC_STATS(hw, n_cpl_fw6_pld);
3477 if (!flb || !flb->totlen) {
3478 CSIO_INC_STATS(hw, n_cpl_unexp);
3479 return;
3480 }
3481
3482 msg = (void *) flb;
3483 msg_len = flb->totlen;
3484 msg_sg = 1;
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303485 } else if (op == CPL_FW6_MSG || op == CPL_FW4_MSG) {
3486
3487 CSIO_INC_STATS(hw, n_cpl_fw6_msg);
3488 /* skip RSS header */
3489 msg = (void *)((uintptr_t)wr + sizeof(__be64));
3490 msg_len = (op == CPL_FW6_MSG) ? sizeof(struct cpl_fw6_msg) :
3491 sizeof(struct cpl_fw4_msg);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303492 } else {
3493 csio_warn(hw, "unexpected CPL %#x on FW event queue\n", op);
3494 CSIO_INC_STATS(hw, n_cpl_unexp);
3495 return;
3496 }
3497
3498 /*
3499 * Enqueue event to EventQ. Events processing happens
3500 * in Event worker thread context
3501 */
3502 if (csio_enqueue_evt_lock(hw, CSIO_EVT_FW, msg,
3503 (uint16_t)msg_len, msg_sg))
3504 CSIO_INC_STATS(hw, n_evt_drop);
3505}
3506
3507void
3508csio_evtq_worker(struct work_struct *work)
3509{
3510 struct csio_hw *hw = container_of(work, struct csio_hw, evtq_work);
3511 struct list_head *evt_entry, *next_entry;
3512 LIST_HEAD(evt_q);
3513 struct csio_evt_msg *evt_msg;
3514 struct cpl_fw6_msg *msg;
3515 struct csio_rnode *rn;
3516 int rv = 0;
3517 uint8_t evtq_stop = 0;
3518
3519 csio_dbg(hw, "event worker thread active evts#%d\n",
3520 hw->stats.n_evt_activeq);
3521
3522 spin_lock_irq(&hw->lock);
3523 while (!list_empty(&hw->evt_active_q)) {
3524 list_splice_tail_init(&hw->evt_active_q, &evt_q);
3525 spin_unlock_irq(&hw->lock);
3526
3527 list_for_each_safe(evt_entry, next_entry, &evt_q) {
3528 evt_msg = (struct csio_evt_msg *) evt_entry;
3529
3530 /* Drop events if queue is STOPPED */
3531 spin_lock_irq(&hw->lock);
3532 if (hw->flags & CSIO_HWF_FWEVT_STOP)
3533 evtq_stop = 1;
3534 spin_unlock_irq(&hw->lock);
3535 if (evtq_stop) {
3536 CSIO_INC_STATS(hw, n_evt_drop);
3537 goto free_evt;
3538 }
3539
3540 switch (evt_msg->type) {
3541 case CSIO_EVT_FW:
3542 msg = (struct cpl_fw6_msg *)(evt_msg->data);
3543
3544 if ((msg->opcode == CPL_FW6_MSG ||
3545 msg->opcode == CPL_FW4_MSG) &&
3546 !msg->type) {
3547 rv = csio_mb_fwevt_handler(hw,
3548 msg->data);
3549 if (!rv)
3550 break;
3551 /* Handle any remaining fw events */
3552 csio_fcoe_fwevt_handler(hw,
3553 msg->opcode, msg->data);
3554 } else if (msg->opcode == CPL_FW6_PLD) {
3555
3556 csio_fcoe_fwevt_handler(hw,
3557 msg->opcode, msg->data);
3558 } else {
3559 csio_warn(hw,
3560 "Unhandled FW msg op %x type %x\n",
3561 msg->opcode, msg->type);
3562 CSIO_INC_STATS(hw, n_evt_drop);
3563 }
3564 break;
3565
3566 case CSIO_EVT_MBX:
3567 csio_mberr_worker(hw);
3568 break;
3569
3570 case CSIO_EVT_DEV_LOSS:
3571 memcpy(&rn, evt_msg->data, sizeof(rn));
3572 csio_rnode_devloss_handler(rn);
3573 break;
3574
3575 default:
3576 csio_warn(hw, "Unhandled event %x on evtq\n",
3577 evt_msg->type);
3578 CSIO_INC_STATS(hw, n_evt_unexp);
3579 break;
3580 }
3581free_evt:
3582 csio_free_evt(hw, evt_msg);
3583 }
3584
3585 spin_lock_irq(&hw->lock);
3586 }
3587 hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
3588 spin_unlock_irq(&hw->lock);
3589}
3590
3591int
3592csio_fwevtq_handler(struct csio_hw *hw)
3593{
3594 int rv;
3595
3596 if (csio_q_iqid(hw, hw->fwevt_iq_idx) == CSIO_MAX_QID) {
3597 CSIO_INC_STATS(hw, n_int_stray);
3598 return -EINVAL;
3599 }
3600
3601 rv = csio_wr_process_iq_idx(hw, hw->fwevt_iq_idx,
3602 csio_process_fwevtq_entry, NULL);
3603 return rv;
3604}
3605
3606/****************************************************************************
3607 * Entry points
3608 ****************************************************************************/
3609
3610/* Management module */
3611/*
3612 * csio_mgmt_req_lookup - Lookup the given IO req exist in Active Q.
3613 * mgmt - mgmt module
3614 * @io_req - io request
3615 *
3616 * Return - 0:if given IO Req exists in active Q.
3617 * -EINVAL :if lookup fails.
3618 */
3619int
3620csio_mgmt_req_lookup(struct csio_mgmtm *mgmtm, struct csio_ioreq *io_req)
3621{
3622 struct list_head *tmp;
3623
3624 /* Lookup ioreq in the ACTIVEQ */
3625 list_for_each(tmp, &mgmtm->active_q) {
3626 if (io_req == (struct csio_ioreq *)tmp)
3627 return 0;
3628 }
3629 return -EINVAL;
3630}
3631
3632#define ECM_MIN_TMO 1000 /* Minimum timeout value for req */
3633
3634/*
3635 * csio_mgmts_tmo_handler - MGMT IO Timeout handler.
3636 * @data - Event data.
3637 *
3638 * Return - none.
3639 */
3640static void
3641csio_mgmt_tmo_handler(uintptr_t data)
3642{
3643 struct csio_mgmtm *mgmtm = (struct csio_mgmtm *) data;
3644 struct list_head *tmp;
3645 struct csio_ioreq *io_req;
3646
3647 csio_dbg(mgmtm->hw, "Mgmt timer invoked!\n");
3648
3649 spin_lock_irq(&mgmtm->hw->lock);
3650
3651 list_for_each(tmp, &mgmtm->active_q) {
3652 io_req = (struct csio_ioreq *) tmp;
3653 io_req->tmo -= min_t(uint32_t, io_req->tmo, ECM_MIN_TMO);
3654
3655 if (!io_req->tmo) {
3656 /* Dequeue the request from retry Q. */
3657 tmp = csio_list_prev(tmp);
3658 list_del_init(&io_req->sm.sm_list);
3659 if (io_req->io_cbfn) {
3660 /* io_req will be freed by completion handler */
3661 io_req->wr_status = -ETIMEDOUT;
3662 io_req->io_cbfn(mgmtm->hw, io_req);
3663 } else {
3664 CSIO_DB_ASSERT(0);
3665 }
3666 }
3667 }
3668
3669 /* If retry queue is not empty, re-arm timer */
3670 if (!list_empty(&mgmtm->active_q))
3671 mod_timer(&mgmtm->mgmt_timer,
3672 jiffies + msecs_to_jiffies(ECM_MIN_TMO));
3673 spin_unlock_irq(&mgmtm->hw->lock);
3674}
3675
3676static void
3677csio_mgmtm_cleanup(struct csio_mgmtm *mgmtm)
3678{
3679 struct csio_hw *hw = mgmtm->hw;
3680 struct csio_ioreq *io_req;
3681 struct list_head *tmp;
3682 uint32_t count;
3683
3684 count = 30;
3685 /* Wait for all outstanding req to complete gracefully */
3686 while ((!list_empty(&mgmtm->active_q)) && count--) {
3687 spin_unlock_irq(&hw->lock);
3688 msleep(2000);
3689 spin_lock_irq(&hw->lock);
3690 }
3691
3692 /* release outstanding req from ACTIVEQ */
3693 list_for_each(tmp, &mgmtm->active_q) {
3694 io_req = (struct csio_ioreq *) tmp;
3695 tmp = csio_list_prev(tmp);
3696 list_del_init(&io_req->sm.sm_list);
3697 mgmtm->stats.n_active--;
3698 if (io_req->io_cbfn) {
3699 /* io_req will be freed by completion handler */
3700 io_req->wr_status = -ETIMEDOUT;
3701 io_req->io_cbfn(mgmtm->hw, io_req);
3702 }
3703 }
3704}
3705
3706/*
3707 * csio_mgmt_init - Mgmt module init entry point
3708 * @mgmtsm - mgmt module
3709 * @hw - HW module
3710 *
3711 * Initialize mgmt timer, resource wait queue, active queue,
3712 * completion q. Allocate Egress and Ingress
3713 * WR queues and save off the queue index returned by the WR
3714 * module for future use. Allocate and save off mgmt reqs in the
3715 * mgmt_req_freelist for future use. Make sure their SM is initialized
3716 * to uninit state.
3717 * Returns: 0 - on success
3718 * -ENOMEM - on error.
3719 */
3720static int
3721csio_mgmtm_init(struct csio_mgmtm *mgmtm, struct csio_hw *hw)
3722{
3723 struct timer_list *timer = &mgmtm->mgmt_timer;
3724
3725 init_timer(timer);
3726 timer->function = csio_mgmt_tmo_handler;
3727 timer->data = (unsigned long)mgmtm;
3728
3729 INIT_LIST_HEAD(&mgmtm->active_q);
3730 INIT_LIST_HEAD(&mgmtm->cbfn_q);
3731
3732 mgmtm->hw = hw;
3733 /*mgmtm->iq_idx = hw->fwevt_iq_idx;*/
3734
3735 return 0;
3736}
3737
3738/*
3739 * csio_mgmtm_exit - MGMT module exit entry point
3740 * @mgmtsm - mgmt module
3741 *
3742 * This function called during MGMT module uninit.
3743 * Stop timers, free ioreqs allocated.
3744 * Returns: None
3745 *
3746 */
3747static void
3748csio_mgmtm_exit(struct csio_mgmtm *mgmtm)
3749{
3750 del_timer_sync(&mgmtm->mgmt_timer);
3751}
3752
3753
3754/**
3755 * csio_hw_start - Kicks off the HW State machine
3756 * @hw: Pointer to HW module.
3757 *
3758 * It is assumed that the initialization is a synchronous operation.
3759 * So when we return afer posting the event, the HW SM should be in
3760 * the ready state, if there were no errors during init.
3761 */
3762int
3763csio_hw_start(struct csio_hw *hw)
3764{
3765 spin_lock_irq(&hw->lock);
3766 csio_post_event(&hw->sm, CSIO_HWE_CFG);
3767 spin_unlock_irq(&hw->lock);
3768
3769 if (csio_is_hw_ready(hw))
3770 return 0;
3771 else
3772 return -EINVAL;
3773}
3774
3775int
3776csio_hw_stop(struct csio_hw *hw)
3777{
3778 csio_post_event(&hw->sm, CSIO_HWE_PCI_REMOVE);
3779
3780 if (csio_is_hw_removing(hw))
3781 return 0;
3782 else
3783 return -EINVAL;
3784}
3785
3786/* Max reset retries */
3787#define CSIO_MAX_RESET_RETRIES 3
3788
3789/**
3790 * csio_hw_reset - Reset the hardware
3791 * @hw: HW module.
3792 *
3793 * Caller should hold lock across this function.
3794 */
3795int
3796csio_hw_reset(struct csio_hw *hw)
3797{
3798 if (!csio_is_hw_master(hw))
3799 return -EPERM;
3800
3801 if (hw->rst_retries >= CSIO_MAX_RESET_RETRIES) {
3802 csio_dbg(hw, "Max hw reset attempts reached..");
3803 return -EINVAL;
3804 }
3805
3806 hw->rst_retries++;
3807 csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET);
3808
3809 if (csio_is_hw_ready(hw)) {
3810 hw->rst_retries = 0;
3811 hw->stats.n_reset_start = jiffies_to_msecs(jiffies);
3812 return 0;
3813 } else
3814 return -EINVAL;
3815}
3816
3817/*
3818 * csio_hw_get_device_id - Caches the Adapter's vendor & device id.
3819 * @hw: HW module.
3820 */
3821static void
3822csio_hw_get_device_id(struct csio_hw *hw)
3823{
3824 /* Is the adapter device id cached already ?*/
3825 if (csio_is_dev_id_cached(hw))
3826 return;
3827
3828 /* Get the PCI vendor & device id */
3829 pci_read_config_word(hw->pdev, PCI_VENDOR_ID,
3830 &hw->params.pci.vendor_id);
3831 pci_read_config_word(hw->pdev, PCI_DEVICE_ID,
3832 &hw->params.pci.device_id);
3833
3834 csio_dev_id_cached(hw);
Arvind Bhushan7cc16382013-03-14 05:09:08 +00003835 hw->chip_id = (hw->params.pci.device_id & CSIO_HW_CHIP_MASK);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303836
3837} /* csio_hw_get_device_id */
3838
3839/*
3840 * csio_hw_set_description - Set the model, description of the hw.
3841 * @hw: HW module.
3842 * @ven_id: PCI Vendor ID
3843 * @dev_id: PCI Device ID
3844 */
3845static void
3846csio_hw_set_description(struct csio_hw *hw, uint16_t ven_id, uint16_t dev_id)
3847{
3848 uint32_t adap_type, prot_type;
3849
3850 if (ven_id == CSIO_VENDOR_ID) {
3851 prot_type = (dev_id & CSIO_ASIC_DEVID_PROTO_MASK);
3852 adap_type = (dev_id & CSIO_ASIC_DEVID_TYPE_MASK);
3853
Praveen Madhavan3fb4c222015-01-16 21:30:19 +05303854 if (prot_type == CSIO_T5_FCOE_ASIC) {
Arvind Bhushan7cc16382013-03-14 05:09:08 +00003855 memcpy(hw->hw_ver,
3856 csio_t5_fcoe_adapters[adap_type].model_no, 16);
3857 memcpy(hw->model_desc,
3858 csio_t5_fcoe_adapters[adap_type].description,
3859 32);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303860 } else {
3861 char tempName[32] = "Chelsio FCoE Controller";
3862 memcpy(hw->model_desc, tempName, 32);
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303863 }
3864 }
3865} /* csio_hw_set_description */
3866
3867/**
3868 * csio_hw_init - Initialize HW module.
3869 * @hw: Pointer to HW module.
3870 *
3871 * Initialize the members of the HW module.
3872 */
3873int
3874csio_hw_init(struct csio_hw *hw)
3875{
3876 int rv = -EINVAL;
3877 uint32_t i;
3878 uint16_t ven_id, dev_id;
3879 struct csio_evt_msg *evt_entry;
3880
3881 INIT_LIST_HEAD(&hw->sm.sm_list);
3882 csio_init_state(&hw->sm, csio_hws_uninit);
3883 spin_lock_init(&hw->lock);
3884 INIT_LIST_HEAD(&hw->sln_head);
3885
3886 /* Get the PCI vendor & device id */
3887 csio_hw_get_device_id(hw);
3888
3889 strcpy(hw->name, CSIO_HW_NAME);
3890
Praveen Madhavan3fb4c222015-01-16 21:30:19 +05303891 /* Initialize the HW chip ops T5 specific ops */
3892 hw->chip_ops = &t5_ops;
Arvind Bhushan7cc16382013-03-14 05:09:08 +00003893
Naresh Kumar Innaa3667aa2012-11-15 22:41:18 +05303894 /* Set the model & its description */
3895
3896 ven_id = hw->params.pci.vendor_id;
3897 dev_id = hw->params.pci.device_id;
3898
3899 csio_hw_set_description(hw, ven_id, dev_id);
3900
3901 /* Initialize default log level */
3902 hw->params.log_level = (uint32_t) csio_dbg_level;
3903
3904 csio_set_fwevt_intr_idx(hw, -1);
3905 csio_set_nondata_intr_idx(hw, -1);
3906
3907 /* Init all the modules: Mailbox, WorkRequest and Transport */
3908 if (csio_mbm_init(csio_hw_to_mbm(hw), hw, csio_hw_mb_timer))
3909 goto err;
3910
3911 rv = csio_wrm_init(csio_hw_to_wrm(hw), hw);
3912 if (rv)
3913 goto err_mbm_exit;
3914
3915 rv = csio_scsim_init(csio_hw_to_scsim(hw), hw);
3916 if (rv)
3917 goto err_wrm_exit;
3918
3919 rv = csio_mgmtm_init(csio_hw_to_mgmtm(hw), hw);
3920 if (rv)
3921 goto err_scsim_exit;
3922 /* Pre-allocate evtq and initialize them */
3923 INIT_LIST_HEAD(&hw->evt_active_q);
3924 INIT_LIST_HEAD(&hw->evt_free_q);
3925 for (i = 0; i < csio_evtq_sz; i++) {
3926
3927 evt_entry = kzalloc(sizeof(struct csio_evt_msg), GFP_KERNEL);
3928 if (!evt_entry) {
3929 csio_err(hw, "Failed to initialize eventq");
3930 goto err_evtq_cleanup;
3931 }
3932
3933 list_add_tail(&evt_entry->list, &hw->evt_free_q);
3934 CSIO_INC_STATS(hw, n_evt_freeq);
3935 }
3936
3937 hw->dev_num = dev_num;
3938 dev_num++;
3939
3940 return 0;
3941
3942err_evtq_cleanup:
3943 csio_evtq_cleanup(hw);
3944 csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
3945err_scsim_exit:
3946 csio_scsim_exit(csio_hw_to_scsim(hw));
3947err_wrm_exit:
3948 csio_wrm_exit(csio_hw_to_wrm(hw), hw);
3949err_mbm_exit:
3950 csio_mbm_exit(csio_hw_to_mbm(hw));
3951err:
3952 return rv;
3953}
3954
3955/**
3956 * csio_hw_exit - Un-initialize HW module.
3957 * @hw: Pointer to HW module.
3958 *
3959 */
3960void
3961csio_hw_exit(struct csio_hw *hw)
3962{
3963 csio_evtq_cleanup(hw);
3964 csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
3965 csio_scsim_exit(csio_hw_to_scsim(hw));
3966 csio_wrm_exit(csio_hw_to_wrm(hw), hw);
3967 csio_mbm_exit(csio_hw_to_mbm(hw));
3968}