GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / scsi / csiostor / csio_hw.c
1 /*
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
50 int csio_dbg_level = 0xFEFF;
51 unsigned int csio_port_mask = 0xf;
52
53 /* Default FW event queue entries. */
54 static uint32_t csio_evtq_sz = CSIO_EVTQ_SIZE;
55
56 /* Default MSI param level */
57 int csio_msi = 2;
58
59 /* FCoE function instances */
60 static int dev_num;
61
62 /* FCoE Adapter types & its description */
63 static 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]"},
66         {"T522-CR 10G/1G", "Chelsio T522-CR 10G/1G [FCoE]"},
67         {"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]"},
83         {"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]"}
86 };
87
88 static void csio_mgmtm_cleanup(struct csio_mgmtm *);
89 static void csio_hw_mbm_cleanup(struct csio_hw *);
90
91 /* State machine forward declarations */
92 static void csio_hws_uninit(struct csio_hw *, enum csio_hw_ev);
93 static void csio_hws_configuring(struct csio_hw *, enum csio_hw_ev);
94 static void csio_hws_initializing(struct csio_hw *, enum csio_hw_ev);
95 static void csio_hws_ready(struct csio_hw *, enum csio_hw_ev);
96 static void csio_hws_quiescing(struct csio_hw *, enum csio_hw_ev);
97 static void csio_hws_quiesced(struct csio_hw *, enum csio_hw_ev);
98 static void csio_hws_resetting(struct csio_hw *, enum csio_hw_ev);
99 static void csio_hws_removing(struct csio_hw *, enum csio_hw_ev);
100 static void csio_hws_pcierr(struct csio_hw *, enum csio_hw_ev);
101
102 static void csio_hw_initialize(struct csio_hw *hw);
103 static void csio_evtq_stop(struct csio_hw *hw);
104 static void csio_evtq_start(struct csio_hw *hw);
105
106 int csio_is_hw_ready(struct csio_hw *hw)
107 {
108         return csio_match_state(hw, csio_hws_ready);
109 }
110
111 int 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  */
132 int
133 csio_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
153 /*
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  */
162 void
163 csio_hw_tp_wr_bits_indirect(struct csio_hw *hw, unsigned int addr,
164                         unsigned int mask, unsigned int val)
165 {
166         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);
169 }
170
171 void
172 csio_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
183 static int
184 csio_memory_write(struct csio_hw *hw, int mtype, u32 addr, u32 len, u32 *buf)
185 {
186         return hw->chip_ops->chip_memory_rw(hw, MEMWIN_CSIOSTOR, mtype,
187                                             addr, len, buf, 0);
188 }
189
190 /*
191  * EEPROM reads take a few tens of us while writes can take a bit over 5 ms.
192  */
193 #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
199 #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  */
211 static int
212 csio_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);
234         *data = le32_to_cpu(*(__le32 *)data);
235
236         return 0;
237 }
238
239 /*
240  * Partial EEPROM Vital Product Data structure.  Includes only the ID and
241  * VPD-R sections.
242  */
243 struct 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  */
260 static int
261 csio_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
285 static int
286 csio_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  */
302 static int
303 csio_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  */
391 static int
392 csio_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;
399         if (csio_rd_reg32(hw, SF_OP_A) & SF_BUSY_F)
400                 return -EBUSY;
401
402         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);
406         if (!ret)
407                 *valp = csio_rd_reg32(hw, SF_DATA_A);
408         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  */
423 static int
424 csio_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;
429         if (csio_rd_reg32(hw, SF_OP_A) & SF_BUSY_F)
430                 return -EBUSY;
431
432         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);
435
436         return csio_hw_wait_op_done_val(hw, SF_OP_A, SF_BUSY_F, 0, SF_ATTEMPTS,
437                                         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  */
448 static int
449 csio_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  */
485 static int
486 csio_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)
507                         csio_wr_reg32(hw, 0, SF_OP_A);    /* unlock SF */
508                 if (ret)
509                         return ret;
510                 if (byte_oriented)
511                         *data = (__force __u32) htonl(*data);
512         }
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  */
526 static int
527 csio_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
560         csio_wr_reg32(hw, 0, SF_OP_A);    /* unlock SF */
561
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
576 unlock:
577         csio_wr_reg32(hw, 0, SF_OP_A);    /* unlock SF */
578         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  */
589 static int
590 csio_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         }
611 out:
612         if (ret)
613                 csio_err(hw, "erase of flash sector %d failed, error %d\n",
614                          start, ret);
615         csio_wr_reg32(hw, 0, SF_OP_A);    /* unlock SF */
616         return 0;
617 }
618
619 static void
620 csio_hw_print_fw_version(struct csio_hw *hw, char *str)
621 {
622         csio_info(hw, "%s: %u.%u.%u.%u\n", str,
623                     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));
627 }
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  */
636 static int
637 csio_hw_get_fw_version(struct csio_hw *hw, uint32_t *vers)
638 {
639         return csio_hw_read_flash(hw, FLASH_FW_START +
640                                   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  */
651 static int
652 csio_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 /*
660  * 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  */
667 static int
668 csio_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];
675         const __be32 *p = (const __be32 *)fw_data;
676         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
699         if (size > FLASH_FW_MAX_SIZE) {
700                 csio_err(hw, "FW image too large, max is %u bytes\n",
701                             FLASH_FW_MAX_SIZE);
702                 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",
717                           FLASH_FW_START_SEC, FLASH_FW_START_SEC + i - 1);
718
719         ret = csio_hw_flash_erase_sectors(hw, FLASH_FW_START_SEC,
720                                           FLASH_FW_START_SEC + i - 1);
721         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);
733         ret = csio_hw_write_flash(hw, FLASH_FW_START, SF_PAGE_SIZE, first_page);
734         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
740         addr = FLASH_FW_START;
741         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,
750                                   FLASH_FW_START +
751                                         offsetof(struct fw_hdr, fw_ver),
752                                   sizeof(hdr->fw_ver),
753                                   (const uint8_t *)&hdr->fw_ver);
754
755 out:
756         if (ret)
757                 csio_err(hw, "firmware download failed, error %d\n", ret);
758         return ret;
759 }
760
761 static int
762 csio_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);
770         csio_wr_reg32(hw, 0, SF_OP_A);    /* unlock SF */
771         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
788 /*****************************************************************************/
789 /* HW State machine assists                                                  */
790 /*****************************************************************************/
791
792 static int
793 csio_hw_dev_ready(struct csio_hw *hw)
794 {
795         uint32_t reg;
796         int cnt = 6;
797         int src_pf;
798
799         while (((reg = csio_rd_reg32(hw, PL_WHOAMI_A)) == 0xFFFFFFFF) &&
800                (--cnt != 0))
801                 mdelay(100);
802
803         if (csio_is_t5(hw->pdev->device & CSIO_HW_CHIP_MASK))
804                 src_pf = SOURCEPF_G(reg);
805         else
806                 src_pf = T6_SOURCEPF_G(reg);
807
808         if ((cnt == 0) && (((int32_t)(src_pf) < 0) ||
809                            (src_pf >= CSIO_MAX_PFN))) {
810                 csio_err(hw, "PL_WHOAMI returned 0x%x, cnt:%d\n", reg, cnt);
811                 return -EIO;
812         }
813
814         hw->pfn = src_pf;
815
816         return 0;
817 }
818
819 /*
820  * csio_do_hello - Perform the HELLO FW Mailbox command and process response.
821  * @hw: HW module
822  * @state: Device state
823  *
824  * FW_HELLO_CMD has to be polled for completion.
825  */
826 static int
827 csio_do_hello(struct csio_hw *hw, enum csio_dev_state *state)
828 {
829         struct csio_mb  *mbp;
830         int     rv = 0;
831         enum fw_retval retval;
832         uint8_t mpfn;
833         char state_str[16];
834         int retries = FW_CMD_HELLO_RETRIES;
835
836         memset(state_str, 0, sizeof(state_str));
837
838         mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
839         if (!mbp) {
840                 rv = -ENOMEM;
841                 CSIO_INC_STATS(hw, n_err_nomem);
842                 goto out;
843         }
844
845 retry:
846         csio_mb_hello(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn,
847                       hw->pfn, CSIO_MASTER_MAY, NULL);
848
849         rv = csio_mb_issue(hw, mbp);
850         if (rv) {
851                 csio_err(hw, "failed to issue HELLO cmd. ret:%d.\n", rv);
852                 goto out_free_mb;
853         }
854
855         csio_mb_process_hello_rsp(hw, mbp, &retval, state, &mpfn);
856         if (retval != FW_SUCCESS) {
857                 csio_err(hw, "HELLO cmd failed with ret: %d\n", retval);
858                 rv = -EINVAL;
859                 goto out_free_mb;
860         }
861
862         /* Firmware has designated us to be master */
863         if (hw->pfn == mpfn) {
864                 hw->flags |= CSIO_HWF_MASTER;
865         } else if (*state == CSIO_DEV_STATE_UNINIT) {
866                 /*
867                  * If we're not the Master PF then we need to wait around for
868                  * the Master PF Driver to finish setting up the adapter.
869                  *
870                  * Note that we also do this wait if we're a non-Master-capable
871                  * PF and there is no current Master PF; a Master PF may show up
872                  * momentarily and we wouldn't want to fail pointlessly.  (This
873                  * can happen when an OS loads lots of different drivers rapidly
874                  * at the same time). In this case, the Master PF returned by
875                  * the firmware will be PCIE_FW_MASTER_MASK so the test below
876                  * will work ...
877                  */
878
879                 int waiting = FW_CMD_HELLO_TIMEOUT;
880
881                 /*
882                  * Wait for the firmware to either indicate an error or
883                  * initialized state.  If we see either of these we bail out
884                  * and report the issue to the caller.  If we exhaust the
885                  * "hello timeout" and we haven't exhausted our retries, try
886                  * again.  Otherwise bail with a timeout error.
887                  */
888                 for (;;) {
889                         uint32_t pcie_fw;
890
891                         spin_unlock_irq(&hw->lock);
892                         msleep(50);
893                         spin_lock_irq(&hw->lock);
894                         waiting -= 50;
895
896                         /*
897                          * If neither Error nor Initialialized are indicated
898                          * by the firmware keep waiting till we exaust our
899                          * timeout ... and then retry if we haven't exhausted
900                          * our retries ...
901                          */
902                         pcie_fw = csio_rd_reg32(hw, PCIE_FW_A);
903                         if (!(pcie_fw & (PCIE_FW_ERR_F|PCIE_FW_INIT_F))) {
904                                 if (waiting <= 0) {
905                                         if (retries-- > 0)
906                                                 goto retry;
907
908                                         rv = -ETIMEDOUT;
909                                         break;
910                                 }
911                                 continue;
912                         }
913
914                         /*
915                          * We either have an Error or Initialized condition
916                          * report errors preferentially.
917                          */
918                         if (state) {
919                                 if (pcie_fw & PCIE_FW_ERR_F) {
920                                         *state = CSIO_DEV_STATE_ERR;
921                                         rv = -ETIMEDOUT;
922                                 } else if (pcie_fw & PCIE_FW_INIT_F)
923                                         *state = CSIO_DEV_STATE_INIT;
924                         }
925
926                         /*
927                          * If we arrived before a Master PF was selected and
928                          * there's not a valid Master PF, grab its identity
929                          * for our caller.
930                          */
931                         if (mpfn == PCIE_FW_MASTER_M &&
932                             (pcie_fw & PCIE_FW_MASTER_VLD_F))
933                                 mpfn = PCIE_FW_MASTER_G(pcie_fw);
934                         break;
935                 }
936                 hw->flags &= ~CSIO_HWF_MASTER;
937         }
938
939         switch (*state) {
940         case CSIO_DEV_STATE_UNINIT:
941                 strcpy(state_str, "Initializing");
942                 break;
943         case CSIO_DEV_STATE_INIT:
944                 strcpy(state_str, "Initialized");
945                 break;
946         case CSIO_DEV_STATE_ERR:
947                 strcpy(state_str, "Error");
948                 break;
949         default:
950                 strcpy(state_str, "Unknown");
951                 break;
952         }
953
954         if (hw->pfn == mpfn)
955                 csio_info(hw, "PF: %d, Coming up as MASTER, HW state: %s\n",
956                         hw->pfn, state_str);
957         else
958                 csio_info(hw,
959                     "PF: %d, Coming up as SLAVE, Master PF: %d, HW state: %s\n",
960                     hw->pfn, mpfn, state_str);
961
962 out_free_mb:
963         mempool_free(mbp, hw->mb_mempool);
964 out:
965         return rv;
966 }
967
968 /*
969  * csio_do_bye - Perform the BYE FW Mailbox command and process response.
970  * @hw: HW module
971  *
972  */
973 static int
974 csio_do_bye(struct csio_hw *hw)
975 {
976         struct csio_mb  *mbp;
977         enum fw_retval retval;
978
979         mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
980         if (!mbp) {
981                 CSIO_INC_STATS(hw, n_err_nomem);
982                 return -ENOMEM;
983         }
984
985         csio_mb_bye(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
986
987         if (csio_mb_issue(hw, mbp)) {
988                 csio_err(hw, "Issue of BYE command failed\n");
989                 mempool_free(mbp, hw->mb_mempool);
990                 return -EINVAL;
991         }
992
993         retval = csio_mb_fw_retval(mbp);
994         if (retval != FW_SUCCESS) {
995                 mempool_free(mbp, hw->mb_mempool);
996                 return -EINVAL;
997         }
998
999         mempool_free(mbp, hw->mb_mempool);
1000
1001         return 0;
1002 }
1003
1004 /*
1005  * csio_do_reset- Perform the device reset.
1006  * @hw: HW module
1007  * @fw_rst: FW reset
1008  *
1009  * If fw_rst is set, issues FW reset mbox cmd otherwise
1010  * does PIO reset.
1011  * Performs reset of the function.
1012  */
1013 static int
1014 csio_do_reset(struct csio_hw *hw, bool fw_rst)
1015 {
1016         struct csio_mb  *mbp;
1017         enum fw_retval retval;
1018
1019         if (!fw_rst) {
1020                 /* PIO reset */
1021                 csio_wr_reg32(hw, PIORSTMODE_F | PIORST_F, PL_RST_A);
1022                 mdelay(2000);
1023                 return 0;
1024         }
1025
1026         mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1027         if (!mbp) {
1028                 CSIO_INC_STATS(hw, n_err_nomem);
1029                 return -ENOMEM;
1030         }
1031
1032         csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
1033                       PIORSTMODE_F | PIORST_F, 0, NULL);
1034
1035         if (csio_mb_issue(hw, mbp)) {
1036                 csio_err(hw, "Issue of RESET command failed.n");
1037                 mempool_free(mbp, hw->mb_mempool);
1038                 return -EINVAL;
1039         }
1040
1041         retval = csio_mb_fw_retval(mbp);
1042         if (retval != FW_SUCCESS) {
1043                 csio_err(hw, "RESET cmd failed with ret:0x%x.\n", retval);
1044                 mempool_free(mbp, hw->mb_mempool);
1045                 return -EINVAL;
1046         }
1047
1048         mempool_free(mbp, hw->mb_mempool);
1049
1050         return 0;
1051 }
1052
1053 static int
1054 csio_hw_validate_caps(struct csio_hw *hw, struct csio_mb *mbp)
1055 {
1056         struct fw_caps_config_cmd *rsp = (struct fw_caps_config_cmd *)mbp->mb;
1057         uint16_t caps;
1058
1059         caps = ntohs(rsp->fcoecaps);
1060
1061         if (!(caps & FW_CAPS_CONFIG_FCOE_INITIATOR)) {
1062                 csio_err(hw, "No FCoE Initiator capability in the firmware.\n");
1063                 return -EINVAL;
1064         }
1065
1066         if (!(caps & FW_CAPS_CONFIG_FCOE_CTRL_OFLD)) {
1067                 csio_err(hw, "No FCoE Control Offload capability\n");
1068                 return -EINVAL;
1069         }
1070
1071         return 0;
1072 }
1073
1074 /*
1075  *      csio_hw_fw_halt - issue a reset/halt to FW and put uP into RESET
1076  *      @hw: the HW module
1077  *      @mbox: mailbox to use for the FW RESET command (if desired)
1078  *      @force: force uP into RESET even if FW RESET command fails
1079  *
1080  *      Issues a RESET command to firmware (if desired) with a HALT indication
1081  *      and then puts the microprocessor into RESET state.  The RESET command
1082  *      will only be issued if a legitimate mailbox is provided (mbox <=
1083  *      PCIE_FW_MASTER_MASK).
1084  *
1085  *      This is generally used in order for the host to safely manipulate the
1086  *      adapter without fear of conflicting with whatever the firmware might
1087  *      be doing.  The only way out of this state is to RESTART the firmware
1088  *      ...
1089  */
1090 static int
1091 csio_hw_fw_halt(struct csio_hw *hw, uint32_t mbox, int32_t force)
1092 {
1093         enum fw_retval retval = 0;
1094
1095         /*
1096          * If a legitimate mailbox is provided, issue a RESET command
1097          * with a HALT indication.
1098          */
1099         if (mbox <= PCIE_FW_MASTER_M) {
1100                 struct csio_mb  *mbp;
1101
1102                 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1103                 if (!mbp) {
1104                         CSIO_INC_STATS(hw, n_err_nomem);
1105                         return -ENOMEM;
1106                 }
1107
1108                 csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
1109                               PIORSTMODE_F | PIORST_F, FW_RESET_CMD_HALT_F,
1110                               NULL);
1111
1112                 if (csio_mb_issue(hw, mbp)) {
1113                         csio_err(hw, "Issue of RESET command failed!\n");
1114                         mempool_free(mbp, hw->mb_mempool);
1115                         return -EINVAL;
1116                 }
1117
1118                 retval = csio_mb_fw_retval(mbp);
1119                 mempool_free(mbp, hw->mb_mempool);
1120         }
1121
1122         /*
1123          * Normally we won't complete the operation if the firmware RESET
1124          * command fails but if our caller insists we'll go ahead and put the
1125          * uP into RESET.  This can be useful if the firmware is hung or even
1126          * missing ...  We'll have to take the risk of putting the uP into
1127          * RESET without the cooperation of firmware in that case.
1128          *
1129          * We also force the firmware's HALT flag to be on in case we bypassed
1130          * the firmware RESET command above or we're dealing with old firmware
1131          * which doesn't have the HALT capability.  This will serve as a flag
1132          * for the incoming firmware to know that it's coming out of a HALT
1133          * rather than a RESET ... if it's new enough to understand that ...
1134          */
1135         if (retval == 0 || force) {
1136                 csio_set_reg_field(hw, CIM_BOOT_CFG_A, UPCRST_F, UPCRST_F);
1137                 csio_set_reg_field(hw, PCIE_FW_A, PCIE_FW_HALT_F,
1138                                    PCIE_FW_HALT_F);
1139         }
1140
1141         /*
1142          * And we always return the result of the firmware RESET command
1143          * even when we force the uP into RESET ...
1144          */
1145         return retval ? -EINVAL : 0;
1146 }
1147
1148 /*
1149  *      csio_hw_fw_restart - restart the firmware by taking the uP out of RESET
1150  *      @hw: the HW module
1151  *      @reset: if we want to do a RESET to restart things
1152  *
1153  *      Restart firmware previously halted by csio_hw_fw_halt().  On successful
1154  *      return the previous PF Master remains as the new PF Master and there
1155  *      is no need to issue a new HELLO command, etc.
1156  *
1157  *      We do this in two ways:
1158  *
1159  *       1. If we're dealing with newer firmware we'll simply want to take
1160  *          the chip's microprocessor out of RESET.  This will cause the
1161  *          firmware to start up from its start vector.  And then we'll loop
1162  *          until the firmware indicates it's started again (PCIE_FW.HALT
1163  *          reset to 0) or we timeout.
1164  *
1165  *       2. If we're dealing with older firmware then we'll need to RESET
1166  *          the chip since older firmware won't recognize the PCIE_FW.HALT
1167  *          flag and automatically RESET itself on startup.
1168  */
1169 static int
1170 csio_hw_fw_restart(struct csio_hw *hw, uint32_t mbox, int32_t reset)
1171 {
1172         if (reset) {
1173                 /*
1174                  * Since we're directing the RESET instead of the firmware
1175                  * doing it automatically, we need to clear the PCIE_FW.HALT
1176                  * bit.
1177                  */
1178                 csio_set_reg_field(hw, PCIE_FW_A, PCIE_FW_HALT_F, 0);
1179
1180                 /*
1181                  * If we've been given a valid mailbox, first try to get the
1182                  * firmware to do the RESET.  If that works, great and we can
1183                  * return success.  Otherwise, if we haven't been given a
1184                  * valid mailbox or the RESET command failed, fall back to
1185                  * hitting the chip with a hammer.
1186                  */
1187                 if (mbox <= PCIE_FW_MASTER_M) {
1188                         csio_set_reg_field(hw, CIM_BOOT_CFG_A, UPCRST_F, 0);
1189                         msleep(100);
1190                         if (csio_do_reset(hw, true) == 0)
1191                                 return 0;
1192                 }
1193
1194                 csio_wr_reg32(hw, PIORSTMODE_F | PIORST_F, PL_RST_A);
1195                 msleep(2000);
1196         } else {
1197                 int ms;
1198
1199                 csio_set_reg_field(hw, CIM_BOOT_CFG_A, UPCRST_F, 0);
1200                 for (ms = 0; ms < FW_CMD_MAX_TIMEOUT; ) {
1201                         if (!(csio_rd_reg32(hw, PCIE_FW_A) & PCIE_FW_HALT_F))
1202                                 return 0;
1203                         msleep(100);
1204                         ms += 100;
1205                 }
1206                 return -ETIMEDOUT;
1207         }
1208         return 0;
1209 }
1210
1211 /*
1212  *      csio_hw_fw_upgrade - perform all of the steps necessary to upgrade FW
1213  *      @hw: the HW module
1214  *      @mbox: mailbox to use for the FW RESET command (if desired)
1215  *      @fw_data: the firmware image to write
1216  *      @size: image size
1217  *      @force: force upgrade even if firmware doesn't cooperate
1218  *
1219  *      Perform all of the steps necessary for upgrading an adapter's
1220  *      firmware image.  Normally this requires the cooperation of the
1221  *      existing firmware in order to halt all existing activities
1222  *      but if an invalid mailbox token is passed in we skip that step
1223  *      (though we'll still put the adapter microprocessor into RESET in
1224  *      that case).
1225  *
1226  *      On successful return the new firmware will have been loaded and
1227  *      the adapter will have been fully RESET losing all previous setup
1228  *      state.  On unsuccessful return the adapter may be completely hosed ...
1229  *      positive errno indicates that the adapter is ~probably~ intact, a
1230  *      negative errno indicates that things are looking bad ...
1231  */
1232 static int
1233 csio_hw_fw_upgrade(struct csio_hw *hw, uint32_t mbox,
1234                   const u8 *fw_data, uint32_t size, int32_t force)
1235 {
1236         const struct fw_hdr *fw_hdr = (const struct fw_hdr *)fw_data;
1237         int reset, ret;
1238
1239         ret = csio_hw_fw_halt(hw, mbox, force);
1240         if (ret != 0 && !force)
1241                 return ret;
1242
1243         ret = csio_hw_fw_dload(hw, (uint8_t *) fw_data, size);
1244         if (ret != 0)
1245                 return ret;
1246
1247         /*
1248          * Older versions of the firmware don't understand the new
1249          * PCIE_FW.HALT flag and so won't know to perform a RESET when they
1250          * restart.  So for newly loaded older firmware we'll have to do the
1251          * RESET for it so it starts up on a clean slate.  We can tell if
1252          * the newly loaded firmware will handle this right by checking
1253          * its header flags to see if it advertises the capability.
1254          */
1255         reset = ((ntohl(fw_hdr->flags) & FW_HDR_FLAGS_RESET_HALT) == 0);
1256         return csio_hw_fw_restart(hw, mbox, reset);
1257 }
1258
1259 /*
1260  * csio_get_device_params - Get device parameters.
1261  * @hw: HW module
1262  *
1263  */
1264 static int
1265 csio_get_device_params(struct csio_hw *hw)
1266 {
1267         struct csio_wrm *wrm    = csio_hw_to_wrm(hw);
1268         struct csio_mb  *mbp;
1269         enum fw_retval retval;
1270         u32 param[6];
1271         int i, j = 0;
1272
1273         /* Initialize portids to -1 */
1274         for (i = 0; i < CSIO_MAX_PPORTS; i++)
1275                 hw->pport[i].portid = -1;
1276
1277         mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1278         if (!mbp) {
1279                 CSIO_INC_STATS(hw, n_err_nomem);
1280                 return -ENOMEM;
1281         }
1282
1283         /* Get port vec information. */
1284         param[0] = FW_PARAM_DEV(PORTVEC);
1285
1286         /* Get Core clock. */
1287         param[1] = FW_PARAM_DEV(CCLK);
1288
1289         /* Get EQ id start and end. */
1290         param[2] = FW_PARAM_PFVF(EQ_START);
1291         param[3] = FW_PARAM_PFVF(EQ_END);
1292
1293         /* Get IQ id start and end. */
1294         param[4] = FW_PARAM_PFVF(IQFLINT_START);
1295         param[5] = FW_PARAM_PFVF(IQFLINT_END);
1296
1297         csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
1298                        ARRAY_SIZE(param), param, NULL, false, NULL);
1299         if (csio_mb_issue(hw, mbp)) {
1300                 csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
1301                 mempool_free(mbp, hw->mb_mempool);
1302                 return -EINVAL;
1303         }
1304
1305         csio_mb_process_read_params_rsp(hw, mbp, &retval,
1306                         ARRAY_SIZE(param), param);
1307         if (retval != FW_SUCCESS) {
1308                 csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
1309                                 retval);
1310                 mempool_free(mbp, hw->mb_mempool);
1311                 return -EINVAL;
1312         }
1313
1314         /* cache the information. */
1315         hw->port_vec = param[0];
1316         hw->vpd.cclk = param[1];
1317         wrm->fw_eq_start = param[2];
1318         wrm->fw_iq_start = param[4];
1319
1320         /* Using FW configured max iqs & eqs */
1321         if ((hw->flags & CSIO_HWF_USING_SOFT_PARAMS) ||
1322                 !csio_is_hw_master(hw)) {
1323                 hw->cfg_niq = param[5] - param[4] + 1;
1324                 hw->cfg_neq = param[3] - param[2] + 1;
1325                 csio_dbg(hw, "Using fwconfig max niqs %d neqs %d\n",
1326                         hw->cfg_niq, hw->cfg_neq);
1327         }
1328
1329         hw->port_vec &= csio_port_mask;
1330
1331         hw->num_pports  = hweight32(hw->port_vec);
1332
1333         csio_dbg(hw, "Port vector: 0x%x, #ports: %d\n",
1334                     hw->port_vec, hw->num_pports);
1335
1336         for (i = 0; i < hw->num_pports; i++) {
1337                 while ((hw->port_vec & (1 << j)) == 0)
1338                         j++;
1339                 hw->pport[i].portid = j++;
1340                 csio_dbg(hw, "Found Port:%d\n", hw->pport[i].portid);
1341         }
1342         mempool_free(mbp, hw->mb_mempool);
1343
1344         return 0;
1345 }
1346
1347
1348 /*
1349  * csio_config_device_caps - Get and set device capabilities.
1350  * @hw: HW module
1351  *
1352  */
1353 static int
1354 csio_config_device_caps(struct csio_hw *hw)
1355 {
1356         struct csio_mb  *mbp;
1357         enum fw_retval retval;
1358         int rv = -EINVAL;
1359
1360         mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1361         if (!mbp) {
1362                 CSIO_INC_STATS(hw, n_err_nomem);
1363                 return -ENOMEM;
1364         }
1365
1366         /* Get device capabilities */
1367         csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, 0, 0, 0, 0, NULL);
1368
1369         if (csio_mb_issue(hw, mbp)) {
1370                 csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(r) failed!\n");
1371                 goto out;
1372         }
1373
1374         retval = csio_mb_fw_retval(mbp);
1375         if (retval != FW_SUCCESS) {
1376                 csio_err(hw, "FW_CAPS_CONFIG_CMD(r) returned %d!\n", retval);
1377                 goto out;
1378         }
1379
1380         /* Validate device capabilities */
1381         rv = csio_hw_validate_caps(hw, mbp);
1382         if (rv != 0)
1383                 goto out;
1384
1385         /* Don't config device capabilities if already configured */
1386         if (hw->fw_state == CSIO_DEV_STATE_INIT) {
1387                 rv = 0;
1388                 goto out;
1389         }
1390
1391         /* Write back desired device capabilities */
1392         csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, true, true,
1393                             false, true, NULL);
1394
1395         if (csio_mb_issue(hw, mbp)) {
1396                 csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(w) failed!\n");
1397                 goto out;
1398         }
1399
1400         retval = csio_mb_fw_retval(mbp);
1401         if (retval != FW_SUCCESS) {
1402                 csio_err(hw, "FW_CAPS_CONFIG_CMD(w) returned %d!\n", retval);
1403                 goto out;
1404         }
1405
1406         rv = 0;
1407 out:
1408         mempool_free(mbp, hw->mb_mempool);
1409         return rv;
1410 }
1411
1412 /*
1413  * csio_enable_ports - Bring up all available ports.
1414  * @hw: HW module.
1415  *
1416  */
1417 static int
1418 csio_enable_ports(struct csio_hw *hw)
1419 {
1420         struct csio_mb  *mbp;
1421         enum fw_retval retval;
1422         uint8_t portid;
1423         int i;
1424
1425         mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1426         if (!mbp) {
1427                 CSIO_INC_STATS(hw, n_err_nomem);
1428                 return -ENOMEM;
1429         }
1430
1431         for (i = 0; i < hw->num_pports; i++) {
1432                 portid = hw->pport[i].portid;
1433
1434                 /* Read PORT information */
1435                 csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid,
1436                              false, 0, 0, NULL);
1437
1438                 if (csio_mb_issue(hw, mbp)) {
1439                         csio_err(hw, "failed to issue FW_PORT_CMD(r) port:%d\n",
1440                                  portid);
1441                         mempool_free(mbp, hw->mb_mempool);
1442                         return -EINVAL;
1443                 }
1444
1445                 csio_mb_process_read_port_rsp(hw, mbp, &retval,
1446                                               &hw->pport[i].pcap);
1447                 if (retval != FW_SUCCESS) {
1448                         csio_err(hw, "FW_PORT_CMD(r) port:%d failed: 0x%x\n",
1449                                  portid, retval);
1450                         mempool_free(mbp, hw->mb_mempool);
1451                         return -EINVAL;
1452                 }
1453
1454                 /* Write back PORT information */
1455                 csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid, true,
1456                              (PAUSE_RX | PAUSE_TX), hw->pport[i].pcap, NULL);
1457
1458                 if (csio_mb_issue(hw, mbp)) {
1459                         csio_err(hw, "failed to issue FW_PORT_CMD(w) port:%d\n",
1460                                  portid);
1461                         mempool_free(mbp, hw->mb_mempool);
1462                         return -EINVAL;
1463                 }
1464
1465                 retval = csio_mb_fw_retval(mbp);
1466                 if (retval != FW_SUCCESS) {
1467                         csio_err(hw, "FW_PORT_CMD(w) port:%d failed :0x%x\n",
1468                                  portid, retval);
1469                         mempool_free(mbp, hw->mb_mempool);
1470                         return -EINVAL;
1471                 }
1472
1473         } /* For all ports */
1474
1475         mempool_free(mbp, hw->mb_mempool);
1476
1477         return 0;
1478 }
1479
1480 /*
1481  * csio_get_fcoe_resinfo - Read fcoe fw resource info.
1482  * @hw: HW module
1483  * Issued with lock held.
1484  */
1485 static int
1486 csio_get_fcoe_resinfo(struct csio_hw *hw)
1487 {
1488         struct csio_fcoe_res_info *res_info = &hw->fres_info;
1489         struct fw_fcoe_res_info_cmd *rsp;
1490         struct csio_mb  *mbp;
1491         enum fw_retval retval;
1492
1493         mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1494         if (!mbp) {
1495                 CSIO_INC_STATS(hw, n_err_nomem);
1496                 return -ENOMEM;
1497         }
1498
1499         /* Get FCoE FW resource information */
1500         csio_fcoe_read_res_info_init_mb(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
1501
1502         if (csio_mb_issue(hw, mbp)) {
1503                 csio_err(hw, "failed to issue FW_FCOE_RES_INFO_CMD\n");
1504                 mempool_free(mbp, hw->mb_mempool);
1505                 return -EINVAL;
1506         }
1507
1508         rsp = (struct fw_fcoe_res_info_cmd *)(mbp->mb);
1509         retval = FW_CMD_RETVAL_G(ntohl(rsp->retval_len16));
1510         if (retval != FW_SUCCESS) {
1511                 csio_err(hw, "FW_FCOE_RES_INFO_CMD failed with ret x%x\n",
1512                          retval);
1513                 mempool_free(mbp, hw->mb_mempool);
1514                 return -EINVAL;
1515         }
1516
1517         res_info->e_d_tov = ntohs(rsp->e_d_tov);
1518         res_info->r_a_tov_seq = ntohs(rsp->r_a_tov_seq);
1519         res_info->r_a_tov_els = ntohs(rsp->r_a_tov_els);
1520         res_info->r_r_tov = ntohs(rsp->r_r_tov);
1521         res_info->max_xchgs = ntohl(rsp->max_xchgs);
1522         res_info->max_ssns = ntohl(rsp->max_ssns);
1523         res_info->used_xchgs = ntohl(rsp->used_xchgs);
1524         res_info->used_ssns = ntohl(rsp->used_ssns);
1525         res_info->max_fcfs = ntohl(rsp->max_fcfs);
1526         res_info->max_vnps = ntohl(rsp->max_vnps);
1527         res_info->used_fcfs = ntohl(rsp->used_fcfs);
1528         res_info->used_vnps = ntohl(rsp->used_vnps);
1529
1530         csio_dbg(hw, "max ssns:%d max xchgs:%d\n", res_info->max_ssns,
1531                                                   res_info->max_xchgs);
1532         mempool_free(mbp, hw->mb_mempool);
1533
1534         return 0;
1535 }
1536
1537 static int
1538 csio_hw_check_fwconfig(struct csio_hw *hw, u32 *param)
1539 {
1540         struct csio_mb  *mbp;
1541         enum fw_retval retval;
1542         u32 _param[1];
1543
1544         mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1545         if (!mbp) {
1546                 CSIO_INC_STATS(hw, n_err_nomem);
1547                 return -ENOMEM;
1548         }
1549
1550         /*
1551          * Find out whether we're dealing with a version of
1552          * the firmware which has configuration file support.
1553          */
1554         _param[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
1555                      FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
1556
1557         csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
1558                        ARRAY_SIZE(_param), _param, NULL, false, NULL);
1559         if (csio_mb_issue(hw, mbp)) {
1560                 csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
1561                 mempool_free(mbp, hw->mb_mempool);
1562                 return -EINVAL;
1563         }
1564
1565         csio_mb_process_read_params_rsp(hw, mbp, &retval,
1566                         ARRAY_SIZE(_param), _param);
1567         if (retval != FW_SUCCESS) {
1568                 csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
1569                                 retval);
1570                 mempool_free(mbp, hw->mb_mempool);
1571                 return -EINVAL;
1572         }
1573
1574         mempool_free(mbp, hw->mb_mempool);
1575         *param = _param[0];
1576
1577         return 0;
1578 }
1579
1580 static int
1581 csio_hw_flash_config(struct csio_hw *hw, u32 *fw_cfg_param, char *path)
1582 {
1583         int ret = 0;
1584         const struct firmware *cf;
1585         struct pci_dev *pci_dev = hw->pdev;
1586         struct device *dev = &pci_dev->dev;
1587         unsigned int mtype = 0, maddr = 0;
1588         uint32_t *cfg_data;
1589         int value_to_add = 0;
1590         const char *fw_cfg_file;
1591
1592         if (csio_is_t5(pci_dev->device & CSIO_HW_CHIP_MASK))
1593                 fw_cfg_file = FW_CFG_NAME_T5;
1594         else
1595                 fw_cfg_file = FW_CFG_NAME_T6;
1596
1597         if (reject_firmware(&cf, fw_cfg_file, dev) < 0) {
1598                 csio_err(hw, "could not find config file %s, err: %d\n",
1599                          fw_cfg_file, ret);
1600                 return -ENOENT;
1601         }
1602
1603         if (cf->size%4 != 0)
1604                 value_to_add = 4 - (cf->size % 4);
1605
1606         cfg_data = kzalloc(cf->size+value_to_add, GFP_KERNEL);
1607         if (cfg_data == NULL) {
1608                 ret = -ENOMEM;
1609                 goto leave;
1610         }
1611
1612         memcpy((void *)cfg_data, (const void *)cf->data, cf->size);
1613         if (csio_hw_check_fwconfig(hw, fw_cfg_param) != 0) {
1614                 ret = -EINVAL;
1615                 goto leave;
1616         }
1617
1618         mtype = FW_PARAMS_PARAM_Y_G(*fw_cfg_param);
1619         maddr = FW_PARAMS_PARAM_Z_G(*fw_cfg_param) << 16;
1620
1621         ret = csio_memory_write(hw, mtype, maddr,
1622                                 cf->size + value_to_add, cfg_data);
1623
1624         if ((ret == 0) && (value_to_add != 0)) {
1625                 union {
1626                         u32 word;
1627                         char buf[4];
1628                 } last;
1629                 size_t size = cf->size & ~0x3;
1630                 int i;
1631
1632                 last.word = cfg_data[size >> 2];
1633                 for (i = value_to_add; i < 4; i++)
1634                         last.buf[i] = 0;
1635                 ret = csio_memory_write(hw, mtype, maddr + size, 4, &last.word);
1636         }
1637         if (ret == 0) {
1638                 csio_info(hw, "config file upgraded to %s\n", fw_cfg_file);
1639                 snprintf(path, 64, "%s%s", "/lib/firmware/", fw_cfg_file);
1640         }
1641
1642 leave:
1643         kfree(cfg_data);
1644         release_firmware(cf);
1645         return ret;
1646 }
1647
1648 /*
1649  * HW initialization: contact FW, obtain config, perform basic init.
1650  *
1651  * If the firmware we're dealing with has Configuration File support, then
1652  * we use that to perform all configuration -- either using the configuration
1653  * file stored in flash on the adapter or using a filesystem-local file
1654  * if available.
1655  *
1656  * If we don't have configuration file support in the firmware, then we'll
1657  * have to set things up the old fashioned way with hard-coded register
1658  * writes and firmware commands ...
1659  */
1660
1661 /*
1662  * Attempt to initialize the HW via a Firmware Configuration File.
1663  */
1664 static int
1665 csio_hw_use_fwconfig(struct csio_hw *hw, int reset, u32 *fw_cfg_param)
1666 {
1667         struct csio_mb  *mbp = NULL;
1668         struct fw_caps_config_cmd *caps_cmd;
1669         unsigned int mtype, maddr;
1670         int rv = -EINVAL;
1671         uint32_t finiver = 0, finicsum = 0, cfcsum = 0;
1672         char path[64];
1673         char *config_name = NULL;
1674
1675         /*
1676          * Reset device if necessary
1677          */
1678         if (reset) {
1679                 rv = csio_do_reset(hw, true);
1680                 if (rv != 0)
1681                         goto bye;
1682         }
1683
1684         /*
1685          * If we have a configuration file in host ,
1686          * then use that.  Otherwise, use the configuration file stored
1687          * in the HW flash ...
1688          */
1689         spin_unlock_irq(&hw->lock);
1690         rv = csio_hw_flash_config(hw, fw_cfg_param, path);
1691         spin_lock_irq(&hw->lock);
1692         if (rv != 0) {
1693                 /*
1694                  * config file was not found. Use default
1695                  * config file from flash.
1696                  */
1697                 config_name = "On FLASH";
1698                 mtype = FW_MEMTYPE_CF_FLASH;
1699                 maddr = hw->chip_ops->chip_flash_cfg_addr(hw);
1700         } else {
1701                 config_name = path;
1702                 mtype = FW_PARAMS_PARAM_Y_G(*fw_cfg_param);
1703                 maddr = FW_PARAMS_PARAM_Z_G(*fw_cfg_param) << 16;
1704         }
1705
1706         mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1707         if (!mbp) {
1708                 CSIO_INC_STATS(hw, n_err_nomem);
1709                 return -ENOMEM;
1710         }
1711         /*
1712          * Tell the firmware to process the indicated Configuration File.
1713          * If there are no errors and the caller has provided return value
1714          * pointers for the [fini] section version, checksum and computed
1715          * checksum, pass those back to the caller.
1716          */
1717         caps_cmd = (struct fw_caps_config_cmd *)(mbp->mb);
1718         CSIO_INIT_MBP(mbp, caps_cmd, CSIO_MB_DEFAULT_TMO, hw, NULL, 1);
1719         caps_cmd->op_to_write =
1720                 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
1721                       FW_CMD_REQUEST_F |
1722                       FW_CMD_READ_F);
1723         caps_cmd->cfvalid_to_len16 =
1724                 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
1725                       FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
1726                       FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
1727                       FW_LEN16(*caps_cmd));
1728
1729         if (csio_mb_issue(hw, mbp)) {
1730                 rv = -EINVAL;
1731                 goto bye;
1732         }
1733
1734         rv = csio_mb_fw_retval(mbp);
1735          /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
1736           * Configuration File in FLASH), our last gasp effort is to use the
1737           * Firmware Configuration File which is embedded in the
1738           * firmware.  A very few early versions of the firmware didn't
1739           * have one embedded but we can ignore those.
1740           */
1741         if (rv == ENOENT) {
1742                 CSIO_INIT_MBP(mbp, caps_cmd, CSIO_MB_DEFAULT_TMO, hw, NULL, 1);
1743                 caps_cmd->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
1744                                               FW_CMD_REQUEST_F |
1745                                               FW_CMD_READ_F);
1746                 caps_cmd->cfvalid_to_len16 = htonl(FW_LEN16(*caps_cmd));
1747
1748                 if (csio_mb_issue(hw, mbp)) {
1749                         rv = -EINVAL;
1750                         goto bye;
1751                 }
1752
1753                 rv = csio_mb_fw_retval(mbp);
1754                 config_name = "Firmware Default";
1755         }
1756         if (rv != FW_SUCCESS)
1757                 goto bye;
1758
1759         finiver = ntohl(caps_cmd->finiver);
1760         finicsum = ntohl(caps_cmd->finicsum);
1761         cfcsum = ntohl(caps_cmd->cfcsum);
1762
1763         /*
1764          * And now tell the firmware to use the configuration we just loaded.
1765          */
1766         caps_cmd->op_to_write =
1767                 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
1768                       FW_CMD_REQUEST_F |
1769                       FW_CMD_WRITE_F);
1770         caps_cmd->cfvalid_to_len16 = htonl(FW_LEN16(*caps_cmd));
1771
1772         if (csio_mb_issue(hw, mbp)) {
1773                 rv = -EINVAL;
1774                 goto bye;
1775         }
1776
1777         rv = csio_mb_fw_retval(mbp);
1778         if (rv != FW_SUCCESS) {
1779                 csio_dbg(hw, "FW_CAPS_CONFIG_CMD returned %d!\n", rv);
1780                 goto bye;
1781         }
1782
1783         if (finicsum != cfcsum) {
1784                 csio_warn(hw,
1785                       "Config File checksum mismatch: csum=%#x, computed=%#x\n",
1786                       finicsum, cfcsum);
1787         }
1788
1789         /* Validate device capabilities */
1790         rv = csio_hw_validate_caps(hw, mbp);
1791         if (rv != 0)
1792                 goto bye;
1793
1794         mempool_free(mbp, hw->mb_mempool);
1795         mbp = NULL;
1796
1797         /*
1798          * Note that we're operating with parameters
1799          * not supplied by the driver, rather than from hard-wired
1800          * initialization constants buried in the driver.
1801          */
1802         hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
1803
1804         /* device parameters */
1805         rv = csio_get_device_params(hw);
1806         if (rv != 0)
1807                 goto bye;
1808
1809         /* Configure SGE */
1810         csio_wr_sge_init(hw);
1811
1812         /*
1813          * And finally tell the firmware to initialize itself using the
1814          * parameters from the Configuration File.
1815          */
1816         /* Post event to notify completion of configuration */
1817         csio_post_event(&hw->sm, CSIO_HWE_INIT);
1818
1819         csio_info(hw, "Successfully configure using Firmware "
1820                   "Configuration File %s, version %#x, computed checksum %#x\n",
1821                   config_name, finiver, cfcsum);
1822         return 0;
1823
1824         /*
1825          * Something bad happened.  Return the error ...
1826          */
1827 bye:
1828         if (mbp)
1829                 mempool_free(mbp, hw->mb_mempool);
1830         hw->flags &= ~CSIO_HWF_USING_SOFT_PARAMS;
1831         csio_warn(hw, "Configuration file error %d\n", rv);
1832         return rv;
1833 }
1834
1835 /* Is the given firmware API compatible with the one the driver was compiled
1836  * with?
1837  */
1838 static int fw_compatible(const struct fw_hdr *hdr1, const struct fw_hdr *hdr2)
1839 {
1840
1841         /* short circuit if it's the exact same firmware version */
1842         if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
1843                 return 1;
1844
1845 #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
1846         if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
1847             SAME_INTF(ri) && SAME_INTF(iscsi) && SAME_INTF(fcoe))
1848                 return 1;
1849 #undef SAME_INTF
1850
1851         return 0;
1852 }
1853
1854 /* The firmware in the filesystem is usable, but should it be installed?
1855  * This routine explains itself in detail if it indicates the filesystem
1856  * firmware should be installed.
1857  */
1858 static int csio_should_install_fs_fw(struct csio_hw *hw, int card_fw_usable,
1859                                 int k, int c)
1860 {
1861         const char *reason;
1862
1863         if (!card_fw_usable) {
1864                 reason = "incompatible or unusable";
1865                 goto install;
1866         }
1867
1868         if (k > c) {
1869                 reason = "older than the version supported with this driver";
1870                 goto install;
1871         }
1872
1873         return 0;
1874
1875 install:
1876         csio_err(hw, "firmware on card (%u.%u.%u.%u) is %s, "
1877                 "installing firmware %u.%u.%u.%u on card.\n",
1878                 FW_HDR_FW_VER_MAJOR_G(c), FW_HDR_FW_VER_MINOR_G(c),
1879                 FW_HDR_FW_VER_MICRO_G(c), FW_HDR_FW_VER_BUILD_G(c), reason,
1880                 FW_HDR_FW_VER_MAJOR_G(k), FW_HDR_FW_VER_MINOR_G(k),
1881                 FW_HDR_FW_VER_MICRO_G(k), FW_HDR_FW_VER_BUILD_G(k));
1882
1883         return 1;
1884 }
1885
1886 static struct fw_info fw_info_array[] = {
1887         {
1888                 .chip = CHELSIO_T5,
1889                 .fs_name = FW_CFG_NAME_T5,
1890                 .fw_mod_name = FW_FNAME_T5,
1891                 .fw_hdr = {
1892                         .chip = FW_HDR_CHIP_T5,
1893                         .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
1894                         .intfver_nic = FW_INTFVER(T5, NIC),
1895                         .intfver_vnic = FW_INTFVER(T5, VNIC),
1896                         .intfver_ri = FW_INTFVER(T5, RI),
1897                         .intfver_iscsi = FW_INTFVER(T5, ISCSI),
1898                         .intfver_fcoe = FW_INTFVER(T5, FCOE),
1899                 },
1900         }, {
1901                 .chip = CHELSIO_T6,
1902                 .fs_name = FW_CFG_NAME_T6,
1903                 .fw_mod_name = FW_FNAME_T6,
1904                 .fw_hdr = {
1905                         .chip = FW_HDR_CHIP_T6,
1906                         .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
1907                         .intfver_nic = FW_INTFVER(T6, NIC),
1908                         .intfver_vnic = FW_INTFVER(T6, VNIC),
1909                         .intfver_ri = FW_INTFVER(T6, RI),
1910                         .intfver_iscsi = FW_INTFVER(T6, ISCSI),
1911                         .intfver_fcoe = FW_INTFVER(T6, FCOE),
1912                 },
1913         }
1914 };
1915
1916 static struct fw_info *find_fw_info(int chip)
1917 {
1918         int i;
1919
1920         for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
1921                 if (fw_info_array[i].chip == chip)
1922                         return &fw_info_array[i];
1923         }
1924         return NULL;
1925 }
1926
1927 static int csio_hw_prep_fw(struct csio_hw *hw, struct fw_info *fw_info,
1928                const u8 *fw_data, unsigned int fw_size,
1929                struct fw_hdr *card_fw, enum csio_dev_state state,
1930                int *reset)
1931 {
1932         int ret, card_fw_usable, fs_fw_usable;
1933         const struct fw_hdr *fs_fw;
1934         const struct fw_hdr *drv_fw;
1935
1936         drv_fw = &fw_info->fw_hdr;
1937
1938         /* Read the header of the firmware on the card */
1939         ret = csio_hw_read_flash(hw, FLASH_FW_START,
1940                             sizeof(*card_fw) / sizeof(uint32_t),
1941                             (uint32_t *)card_fw, 1);
1942         if (ret == 0) {
1943                 card_fw_usable = fw_compatible(drv_fw, (const void *)card_fw);
1944         } else {
1945                 csio_err(hw,
1946                         "Unable to read card's firmware header: %d\n", ret);
1947                 card_fw_usable = 0;
1948         }
1949
1950         if (fw_data != NULL) {
1951                 fs_fw = (const void *)fw_data;
1952                 fs_fw_usable = fw_compatible(drv_fw, fs_fw);
1953         } else {
1954                 fs_fw = NULL;
1955                 fs_fw_usable = 0;
1956         }
1957
1958         if (card_fw_usable && card_fw->fw_ver == drv_fw->fw_ver &&
1959             (!fs_fw_usable || fs_fw->fw_ver == drv_fw->fw_ver)) {
1960                 /* Common case: the firmware on the card is an exact match and
1961                  * the filesystem one is an exact match too, or the filesystem
1962                  * one is absent/incompatible.
1963                  */
1964         } else if (fs_fw_usable && state == CSIO_DEV_STATE_UNINIT &&
1965                    csio_should_install_fs_fw(hw, card_fw_usable,
1966                                         be32_to_cpu(fs_fw->fw_ver),
1967                                         be32_to_cpu(card_fw->fw_ver))) {
1968                 ret = csio_hw_fw_upgrade(hw, hw->pfn, fw_data,
1969                                      fw_size, 0);
1970                 if (ret != 0) {
1971                         csio_err(hw,
1972                                 "failed to install firmware: %d\n", ret);
1973                         goto bye;
1974                 }
1975
1976                 /* Installed successfully, update the cached header too. */
1977                 memcpy(card_fw, fs_fw, sizeof(*card_fw));
1978                 card_fw_usable = 1;
1979                 *reset = 0;     /* already reset as part of load_fw */
1980         }
1981
1982         if (!card_fw_usable) {
1983                 uint32_t d, c, k;
1984
1985                 d = be32_to_cpu(drv_fw->fw_ver);
1986                 c = be32_to_cpu(card_fw->fw_ver);
1987                 k = fs_fw ? be32_to_cpu(fs_fw->fw_ver) : 0;
1988
1989                 csio_err(hw, "Cannot find a usable firmware: "
1990                         "chip state %d, "
1991                         "driver compiled with %d.%d.%d.%d, "
1992                         "card has %d.%d.%d.%d, filesystem has %d.%d.%d.%d\n",
1993                         state,
1994                         FW_HDR_FW_VER_MAJOR_G(d), FW_HDR_FW_VER_MINOR_G(d),
1995                         FW_HDR_FW_VER_MICRO_G(d), FW_HDR_FW_VER_BUILD_G(d),
1996                         FW_HDR_FW_VER_MAJOR_G(c), FW_HDR_FW_VER_MINOR_G(c),
1997                         FW_HDR_FW_VER_MICRO_G(c), FW_HDR_FW_VER_BUILD_G(c),
1998                         FW_HDR_FW_VER_MAJOR_G(k), FW_HDR_FW_VER_MINOR_G(k),
1999                         FW_HDR_FW_VER_MICRO_G(k), FW_HDR_FW_VER_BUILD_G(k));
2000                 ret = -EINVAL;
2001                 goto bye;
2002         }
2003
2004         /* We're using whatever's on the card and it's known to be good. */
2005         hw->fwrev = be32_to_cpu(card_fw->fw_ver);
2006         hw->tp_vers = be32_to_cpu(card_fw->tp_microcode_ver);
2007
2008 bye:
2009         return ret;
2010 }
2011
2012 /*
2013  * Returns -EINVAL if attempts to flash the firmware failed,
2014  * -ENOMEM if memory allocation failed else returns 0,
2015  * if flashing was not attempted because the card had the
2016  * latest firmware ECANCELED is returned
2017  */
2018 static int
2019 csio_hw_flash_fw(struct csio_hw *hw, int *reset)
2020 {
2021         int ret = -ECANCELED;
2022         const struct firmware *fw;
2023         struct fw_info *fw_info;
2024         struct fw_hdr *card_fw;
2025         struct pci_dev *pci_dev = hw->pdev;
2026         struct device *dev = &pci_dev->dev ;
2027         const u8 *fw_data = NULL;
2028         unsigned int fw_size = 0;
2029         const char *fw_bin_file;
2030
2031         /* This is the firmware whose headers the driver was compiled
2032          * against
2033          */
2034         fw_info = find_fw_info(CHELSIO_CHIP_VERSION(hw->chip_id));
2035         if (fw_info == NULL) {
2036                 csio_err(hw,
2037                         "unable to get firmware info for chip %d.\n",
2038                         CHELSIO_CHIP_VERSION(hw->chip_id));
2039                 return -EINVAL;
2040         }
2041
2042         /* allocate memory to read the header of the firmware on the
2043          * card
2044          */
2045         card_fw = kmalloc(sizeof(*card_fw), GFP_KERNEL);
2046         if (!card_fw)
2047                 return -ENOMEM;
2048
2049         if (csio_is_t5(pci_dev->device & CSIO_HW_CHIP_MASK))
2050                 fw_bin_file = FW_FNAME_T5;
2051         else
2052                 fw_bin_file = FW_FNAME_T6;
2053
2054         if (reject_firmware(&fw, fw_bin_file, dev) < 0) {
2055                 csio_err(hw, "could not find firmware image %s, err: %d\n",
2056                          fw_bin_file, ret);
2057         } else {
2058                 fw_data = fw->data;
2059                 fw_size = fw->size;
2060         }
2061
2062         /* upgrade FW logic */
2063         ret = csio_hw_prep_fw(hw, fw_info, fw_data, fw_size, card_fw,
2064                          hw->fw_state, reset);
2065
2066         /* Cleaning up */
2067         if (fw != NULL)
2068                 release_firmware(fw);
2069         kfree(card_fw);
2070         return ret;
2071 }
2072
2073 static int csio_hw_check_fwver(struct csio_hw *hw)
2074 {
2075         if (csio_is_t6(hw->pdev->device & CSIO_HW_CHIP_MASK) &&
2076             (hw->fwrev < CSIO_MIN_T6_FW)) {
2077                 csio_hw_print_fw_version(hw, "T6 unsupported fw");
2078                 return -1;
2079         }
2080
2081         return 0;
2082 }
2083
2084 /*
2085  * csio_hw_configure - Configure HW
2086  * @hw - HW module
2087  *
2088  */
2089 static void
2090 csio_hw_configure(struct csio_hw *hw)
2091 {
2092         int reset = 1;
2093         int rv;
2094         u32 param[1];
2095
2096         rv = csio_hw_dev_ready(hw);
2097         if (rv != 0) {
2098                 CSIO_INC_STATS(hw, n_err_fatal);
2099                 csio_post_event(&hw->sm, CSIO_HWE_FATAL);
2100                 goto out;
2101         }
2102
2103         /* HW version */
2104         hw->chip_ver = (char)csio_rd_reg32(hw, PL_REV_A);
2105
2106         /* Needed for FW download */
2107         rv = csio_hw_get_flash_params(hw);
2108         if (rv != 0) {
2109                 csio_err(hw, "Failed to get serial flash params rv:%d\n", rv);
2110                 csio_post_event(&hw->sm, CSIO_HWE_FATAL);
2111                 goto out;
2112         }
2113
2114         /* Set PCIe completion timeout to 4 seconds */
2115         if (pci_is_pcie(hw->pdev))
2116                 pcie_capability_clear_and_set_word(hw->pdev, PCI_EXP_DEVCTL2,
2117                                 PCI_EXP_DEVCTL2_COMP_TIMEOUT, 0xd);
2118
2119         hw->chip_ops->chip_set_mem_win(hw, MEMWIN_CSIOSTOR);
2120
2121         rv = csio_hw_get_fw_version(hw, &hw->fwrev);
2122         if (rv != 0)
2123                 goto out;
2124
2125         csio_hw_print_fw_version(hw, "Firmware revision");
2126
2127         rv = csio_do_hello(hw, &hw->fw_state);
2128         if (rv != 0) {
2129                 CSIO_INC_STATS(hw, n_err_fatal);
2130                 csio_post_event(&hw->sm, CSIO_HWE_FATAL);
2131                 goto out;
2132         }
2133
2134         /* Read vpd */
2135         rv = csio_hw_get_vpd_params(hw, &hw->vpd);
2136         if (rv != 0)
2137                 goto out;
2138
2139         csio_hw_get_fw_version(hw, &hw->fwrev);
2140         csio_hw_get_tp_version(hw, &hw->tp_vers);
2141         if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
2142
2143                         /* Do firmware update */
2144                 spin_unlock_irq(&hw->lock);
2145                 rv = csio_hw_flash_fw(hw, &reset);
2146                 spin_lock_irq(&hw->lock);
2147
2148                 if (rv != 0)
2149                         goto out;
2150
2151                 rv = csio_hw_check_fwver(hw);
2152                 if (rv < 0)
2153                         goto out;
2154
2155                 /* If the firmware doesn't support Configuration Files,
2156                  * return an error.
2157                  */
2158                 rv = csio_hw_check_fwconfig(hw, param);
2159                 if (rv != 0) {
2160                         csio_info(hw, "Firmware doesn't support "
2161                                   "Firmware Configuration files\n");
2162                         goto out;
2163                 }
2164
2165                 /* The firmware provides us with a memory buffer where we can
2166                  * load a Configuration File from the host if we want to
2167                  * override the Configuration File in flash.
2168                  */
2169                 rv = csio_hw_use_fwconfig(hw, reset, param);
2170                 if (rv == -ENOENT) {
2171                         csio_info(hw, "Could not initialize "
2172                                   "adapter, error%d\n", rv);
2173                         goto out;
2174                 }
2175                 if (rv != 0) {
2176                         csio_info(hw, "Could not initialize "
2177                                   "adapter, error%d\n", rv);
2178                         goto out;
2179                 }
2180
2181         } else {
2182                 rv = csio_hw_check_fwver(hw);
2183                 if (rv < 0)
2184                         goto out;
2185
2186                 if (hw->fw_state == CSIO_DEV_STATE_INIT) {
2187
2188                         hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
2189
2190                         /* device parameters */
2191                         rv = csio_get_device_params(hw);
2192                         if (rv != 0)
2193                                 goto out;
2194
2195                         /* Get device capabilities */
2196                         rv = csio_config_device_caps(hw);
2197                         if (rv != 0)
2198                                 goto out;
2199
2200                         /* Configure SGE */
2201                         csio_wr_sge_init(hw);
2202
2203                         /* Post event to notify completion of configuration */
2204                         csio_post_event(&hw->sm, CSIO_HWE_INIT);
2205                         goto out;
2206                 }
2207         } /* if not master */
2208
2209 out:
2210         return;
2211 }
2212
2213 /*
2214  * csio_hw_initialize - Initialize HW
2215  * @hw - HW module
2216  *
2217  */
2218 static void
2219 csio_hw_initialize(struct csio_hw *hw)
2220 {
2221         struct csio_mb  *mbp;
2222         enum fw_retval retval;
2223         int rv;
2224         int i;
2225
2226         if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
2227                 mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
2228                 if (!mbp)
2229                         goto out;
2230
2231                 csio_mb_initialize(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
2232
2233                 if (csio_mb_issue(hw, mbp)) {
2234                         csio_err(hw, "Issue of FW_INITIALIZE_CMD failed!\n");
2235                         goto free_and_out;
2236                 }
2237
2238                 retval = csio_mb_fw_retval(mbp);
2239                 if (retval != FW_SUCCESS) {
2240                         csio_err(hw, "FW_INITIALIZE_CMD returned 0x%x!\n",
2241                                  retval);
2242                         goto free_and_out;
2243                 }
2244
2245                 mempool_free(mbp, hw->mb_mempool);
2246         }
2247
2248         rv = csio_get_fcoe_resinfo(hw);
2249         if (rv != 0) {
2250                 csio_err(hw, "Failed to read fcoe resource info: %d\n", rv);
2251                 goto out;
2252         }
2253
2254         spin_unlock_irq(&hw->lock);
2255         rv = csio_config_queues(hw);
2256         spin_lock_irq(&hw->lock);
2257
2258         if (rv != 0) {
2259                 csio_err(hw, "Config of queues failed!: %d\n", rv);
2260                 goto out;
2261         }
2262
2263         for (i = 0; i < hw->num_pports; i++)
2264                 hw->pport[i].mod_type = FW_PORT_MOD_TYPE_NA;
2265
2266         if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
2267                 rv = csio_enable_ports(hw);
2268                 if (rv != 0) {
2269                         csio_err(hw, "Failed to enable ports: %d\n", rv);
2270                         goto out;
2271                 }
2272         }
2273
2274         csio_post_event(&hw->sm, CSIO_HWE_INIT_DONE);
2275         return;
2276
2277 free_and_out:
2278         mempool_free(mbp, hw->mb_mempool);
2279 out:
2280         return;
2281 }
2282
2283 #define PF_INTR_MASK (PFSW_F | PFCIM_F)
2284
2285 /*
2286  * csio_hw_intr_enable - Enable HW interrupts
2287  * @hw: Pointer to HW module.
2288  *
2289  * Enable interrupts in HW registers.
2290  */
2291 static void
2292 csio_hw_intr_enable(struct csio_hw *hw)
2293 {
2294         uint16_t vec = (uint16_t)csio_get_mb_intr_idx(csio_hw_to_mbm(hw));
2295         u32 pf = 0;
2296         uint32_t pl = csio_rd_reg32(hw, PL_INT_ENABLE_A);
2297
2298         if (csio_is_t5(hw->pdev->device & CSIO_HW_CHIP_MASK))
2299                 pf = SOURCEPF_G(csio_rd_reg32(hw, PL_WHOAMI_A));
2300         else
2301                 pf = T6_SOURCEPF_G(csio_rd_reg32(hw, PL_WHOAMI_A));
2302
2303         /*
2304          * Set aivec for MSI/MSIX. PCIE_PF_CFG.INTXType is set up
2305          * by FW, so do nothing for INTX.
2306          */
2307         if (hw->intr_mode == CSIO_IM_MSIX)
2308                 csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG_A),
2309                                    AIVEC_V(AIVEC_M), vec);
2310         else if (hw->intr_mode == CSIO_IM_MSI)
2311                 csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG_A),
2312                                    AIVEC_V(AIVEC_M), 0);
2313
2314         csio_wr_reg32(hw, PF_INTR_MASK, MYPF_REG(PL_PF_INT_ENABLE_A));
2315
2316         /* Turn on MB interrupts - this will internally flush PIO as well */
2317         csio_mb_intr_enable(hw);
2318
2319         /* These are common registers - only a master can modify them */
2320         if (csio_is_hw_master(hw)) {
2321                 /*
2322                  * Disable the Serial FLASH interrupt, if enabled!
2323                  */
2324                 pl &= (~SF_F);
2325                 csio_wr_reg32(hw, pl, PL_INT_ENABLE_A);
2326
2327                 csio_wr_reg32(hw, ERR_CPL_EXCEED_IQE_SIZE_F |
2328                               EGRESS_SIZE_ERR_F | ERR_INVALID_CIDX_INC_F |
2329                               ERR_CPL_OPCODE_0_F | ERR_DROPPED_DB_F |
2330                               ERR_DATA_CPL_ON_HIGH_QID1_F |
2331                               ERR_DATA_CPL_ON_HIGH_QID0_F | ERR_BAD_DB_PIDX3_F |
2332                               ERR_BAD_DB_PIDX2_F | ERR_BAD_DB_PIDX1_F |
2333                               ERR_BAD_DB_PIDX0_F | ERR_ING_CTXT_PRIO_F |
2334                               ERR_EGR_CTXT_PRIO_F | INGRESS_SIZE_ERR_F,
2335                               SGE_INT_ENABLE3_A);
2336                 csio_set_reg_field(hw, PL_INT_MAP0_A, 0, 1 << pf);
2337         }
2338
2339         hw->flags |= CSIO_HWF_HW_INTR_ENABLED;
2340
2341 }
2342
2343 /*
2344  * csio_hw_intr_disable - Disable HW interrupts
2345  * @hw: Pointer to HW module.
2346  *
2347  * Turn off Mailbox and PCI_PF_CFG interrupts.
2348  */
2349 void
2350 csio_hw_intr_disable(struct csio_hw *hw)
2351 {
2352         u32 pf = 0;
2353
2354         if (csio_is_t5(hw->pdev->device & CSIO_HW_CHIP_MASK))
2355                 pf = SOURCEPF_G(csio_rd_reg32(hw, PL_WHOAMI_A));
2356         else
2357                 pf = T6_SOURCEPF_G(csio_rd_reg32(hw, PL_WHOAMI_A));
2358
2359         if (!(hw->flags & CSIO_HWF_HW_INTR_ENABLED))
2360                 return;
2361
2362         hw->flags &= ~CSIO_HWF_HW_INTR_ENABLED;
2363
2364         csio_wr_reg32(hw, 0, MYPF_REG(PL_PF_INT_ENABLE_A));
2365         if (csio_is_hw_master(hw))
2366                 csio_set_reg_field(hw, PL_INT_MAP0_A, 1 << pf, 0);
2367
2368         /* Turn off MB interrupts */
2369         csio_mb_intr_disable(hw);
2370
2371 }
2372
2373 void
2374 csio_hw_fatal_err(struct csio_hw *hw)
2375 {
2376         csio_set_reg_field(hw, SGE_CONTROL_A, GLOBALENABLE_F, 0);
2377         csio_hw_intr_disable(hw);
2378
2379         /* Do not reset HW, we may need FW state for debugging */
2380         csio_fatal(hw, "HW Fatal error encountered!\n");
2381 }
2382
2383 /*****************************************************************************/
2384 /* START: HW SM                                                              */
2385 /*****************************************************************************/
2386 /*
2387  * csio_hws_uninit - Uninit state
2388  * @hw - HW module
2389  * @evt - Event
2390  *
2391  */
2392 static void
2393 csio_hws_uninit(struct csio_hw *hw, enum csio_hw_ev evt)
2394 {
2395         hw->prev_evt = hw->cur_evt;
2396         hw->cur_evt = evt;
2397         CSIO_INC_STATS(hw, n_evt_sm[evt]);
2398
2399         switch (evt) {
2400         case CSIO_HWE_CFG:
2401                 csio_set_state(&hw->sm, csio_hws_configuring);
2402                 csio_hw_configure(hw);
2403                 break;
2404
2405         default:
2406                 CSIO_INC_STATS(hw, n_evt_unexp);
2407                 break;
2408         }
2409 }
2410
2411 /*
2412  * csio_hws_configuring - Configuring state
2413  * @hw - HW module
2414  * @evt - Event
2415  *
2416  */
2417 static void
2418 csio_hws_configuring(struct csio_hw *hw, enum csio_hw_ev evt)
2419 {
2420         hw->prev_evt = hw->cur_evt;
2421         hw->cur_evt = evt;
2422         CSIO_INC_STATS(hw, n_evt_sm[evt]);
2423
2424         switch (evt) {
2425         case CSIO_HWE_INIT:
2426                 csio_set_state(&hw->sm, csio_hws_initializing);
2427                 csio_hw_initialize(hw);
2428                 break;
2429
2430         case CSIO_HWE_INIT_DONE:
2431                 csio_set_state(&hw->sm, csio_hws_ready);
2432                 /* Fan out event to all lnode SMs */
2433                 csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
2434                 break;
2435
2436         case CSIO_HWE_FATAL:
2437                 csio_set_state(&hw->sm, csio_hws_uninit);
2438                 break;
2439
2440         case CSIO_HWE_PCI_REMOVE:
2441                 csio_do_bye(hw);
2442                 break;
2443         default:
2444                 CSIO_INC_STATS(hw, n_evt_unexp);
2445                 break;
2446         }
2447 }
2448
2449 /*
2450  * csio_hws_initializing - Initialiazing state
2451  * @hw - HW module
2452  * @evt - Event
2453  *
2454  */
2455 static void
2456 csio_hws_initializing(struct csio_hw *hw, enum csio_hw_ev evt)
2457 {
2458         hw->prev_evt = hw->cur_evt;
2459         hw->cur_evt = evt;
2460         CSIO_INC_STATS(hw, n_evt_sm[evt]);
2461
2462         switch (evt) {
2463         case CSIO_HWE_INIT_DONE:
2464                 csio_set_state(&hw->sm, csio_hws_ready);
2465
2466                 /* Fan out event to all lnode SMs */
2467                 csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
2468
2469                 /* Enable interrupts */
2470                 csio_hw_intr_enable(hw);
2471                 break;
2472
2473         case CSIO_HWE_FATAL:
2474                 csio_set_state(&hw->sm, csio_hws_uninit);
2475                 break;
2476
2477         case CSIO_HWE_PCI_REMOVE:
2478                 csio_do_bye(hw);
2479                 break;
2480
2481         default:
2482                 CSIO_INC_STATS(hw, n_evt_unexp);
2483                 break;
2484         }
2485 }
2486
2487 /*
2488  * csio_hws_ready - Ready state
2489  * @hw - HW module
2490  * @evt - Event
2491  *
2492  */
2493 static void
2494 csio_hws_ready(struct csio_hw *hw, enum csio_hw_ev evt)
2495 {
2496         /* Remember the event */
2497         hw->evtflag = evt;
2498
2499         hw->prev_evt = hw->cur_evt;
2500         hw->cur_evt = evt;
2501         CSIO_INC_STATS(hw, n_evt_sm[evt]);
2502
2503         switch (evt) {
2504         case CSIO_HWE_HBA_RESET:
2505         case CSIO_HWE_FW_DLOAD:
2506         case CSIO_HWE_SUSPEND:
2507         case CSIO_HWE_PCI_REMOVE:
2508         case CSIO_HWE_PCIERR_DETECTED:
2509                 csio_set_state(&hw->sm, csio_hws_quiescing);
2510                 /* cleanup all outstanding cmds */
2511                 if (evt == CSIO_HWE_HBA_RESET ||
2512                     evt == CSIO_HWE_PCIERR_DETECTED)
2513                         csio_scsim_cleanup_io(csio_hw_to_scsim(hw), false);
2514                 else
2515                         csio_scsim_cleanup_io(csio_hw_to_scsim(hw), true);
2516
2517                 csio_hw_intr_disable(hw);
2518                 csio_hw_mbm_cleanup(hw);
2519                 csio_evtq_stop(hw);
2520                 csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWSTOP);
2521                 csio_evtq_flush(hw);
2522                 csio_mgmtm_cleanup(csio_hw_to_mgmtm(hw));
2523                 csio_post_event(&hw->sm, CSIO_HWE_QUIESCED);
2524                 break;
2525
2526         case CSIO_HWE_FATAL:
2527                 csio_set_state(&hw->sm, csio_hws_uninit);
2528                 break;
2529
2530         default:
2531                 CSIO_INC_STATS(hw, n_evt_unexp);
2532                 break;
2533         }
2534 }
2535
2536 /*
2537  * csio_hws_quiescing - Quiescing state
2538  * @hw - HW module
2539  * @evt - Event
2540  *
2541  */
2542 static void
2543 csio_hws_quiescing(struct csio_hw *hw, enum csio_hw_ev evt)
2544 {
2545         hw->prev_evt = hw->cur_evt;
2546         hw->cur_evt = evt;
2547         CSIO_INC_STATS(hw, n_evt_sm[evt]);
2548
2549         switch (evt) {
2550         case CSIO_HWE_QUIESCED:
2551                 switch (hw->evtflag) {
2552                 case CSIO_HWE_FW_DLOAD:
2553                         csio_set_state(&hw->sm, csio_hws_resetting);
2554                         /* Download firmware */
2555                         /* Fall through */
2556
2557                 case CSIO_HWE_HBA_RESET:
2558                         csio_set_state(&hw->sm, csio_hws_resetting);
2559                         /* Start reset of the HBA */
2560                         csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWRESET);
2561                         csio_wr_destroy_queues(hw, false);
2562                         csio_do_reset(hw, false);
2563                         csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET_DONE);
2564                         break;
2565
2566                 case CSIO_HWE_PCI_REMOVE:
2567                         csio_set_state(&hw->sm, csio_hws_removing);
2568                         csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREMOVE);
2569                         csio_wr_destroy_queues(hw, true);
2570                         /* Now send the bye command */
2571                         csio_do_bye(hw);
2572                         break;
2573
2574                 case CSIO_HWE_SUSPEND:
2575                         csio_set_state(&hw->sm, csio_hws_quiesced);
2576                         break;
2577
2578                 case CSIO_HWE_PCIERR_DETECTED:
2579                         csio_set_state(&hw->sm, csio_hws_pcierr);
2580                         csio_wr_destroy_queues(hw, false);
2581                         break;
2582
2583                 default:
2584                         CSIO_INC_STATS(hw, n_evt_unexp);
2585                         break;
2586
2587                 }
2588                 break;
2589
2590         default:
2591                 CSIO_INC_STATS(hw, n_evt_unexp);
2592                 break;
2593         }
2594 }
2595
2596 /*
2597  * csio_hws_quiesced - Quiesced state
2598  * @hw - HW module
2599  * @evt - Event
2600  *
2601  */
2602 static void
2603 csio_hws_quiesced(struct csio_hw *hw, enum csio_hw_ev evt)
2604 {
2605         hw->prev_evt = hw->cur_evt;
2606         hw->cur_evt = evt;
2607         CSIO_INC_STATS(hw, n_evt_sm[evt]);
2608
2609         switch (evt) {
2610         case CSIO_HWE_RESUME:
2611                 csio_set_state(&hw->sm, csio_hws_configuring);
2612                 csio_hw_configure(hw);
2613                 break;
2614
2615         default:
2616                 CSIO_INC_STATS(hw, n_evt_unexp);
2617                 break;
2618         }
2619 }
2620
2621 /*
2622  * csio_hws_resetting - HW Resetting state
2623  * @hw - HW module
2624  * @evt - Event
2625  *
2626  */
2627 static void
2628 csio_hws_resetting(struct csio_hw *hw, enum csio_hw_ev evt)
2629 {
2630         hw->prev_evt = hw->cur_evt;
2631         hw->cur_evt = evt;
2632         CSIO_INC_STATS(hw, n_evt_sm[evt]);
2633
2634         switch (evt) {
2635         case CSIO_HWE_HBA_RESET_DONE:
2636                 csio_evtq_start(hw);
2637                 csio_set_state(&hw->sm, csio_hws_configuring);
2638                 csio_hw_configure(hw);
2639                 break;
2640
2641         default:
2642                 CSIO_INC_STATS(hw, n_evt_unexp);
2643                 break;
2644         }
2645 }
2646
2647 /*
2648  * csio_hws_removing - PCI Hotplug removing state
2649  * @hw - HW module
2650  * @evt - Event
2651  *
2652  */
2653 static void
2654 csio_hws_removing(struct csio_hw *hw, enum csio_hw_ev evt)
2655 {
2656         hw->prev_evt = hw->cur_evt;
2657         hw->cur_evt = evt;
2658         CSIO_INC_STATS(hw, n_evt_sm[evt]);
2659
2660         switch (evt) {
2661         case CSIO_HWE_HBA_RESET:
2662                 if (!csio_is_hw_master(hw))
2663                         break;
2664                 /*
2665                  * The BYE should have alerady been issued, so we cant
2666                  * use the mailbox interface. Hence we use the PL_RST
2667                  * register directly.
2668                  */
2669                 csio_err(hw, "Resetting HW and waiting 2 seconds...\n");
2670                 csio_wr_reg32(hw, PIORSTMODE_F | PIORST_F, PL_RST_A);
2671                 mdelay(2000);
2672                 break;
2673
2674         /* Should never receive any new events */
2675         default:
2676                 CSIO_INC_STATS(hw, n_evt_unexp);
2677                 break;
2678
2679         }
2680 }
2681
2682 /*
2683  * csio_hws_pcierr - PCI Error state
2684  * @hw - HW module
2685  * @evt - Event
2686  *
2687  */
2688 static void
2689 csio_hws_pcierr(struct csio_hw *hw, enum csio_hw_ev evt)
2690 {
2691         hw->prev_evt = hw->cur_evt;
2692         hw->cur_evt = evt;
2693         CSIO_INC_STATS(hw, n_evt_sm[evt]);
2694
2695         switch (evt) {
2696         case CSIO_HWE_PCIERR_SLOT_RESET:
2697                 csio_evtq_start(hw);
2698                 csio_set_state(&hw->sm, csio_hws_configuring);
2699                 csio_hw_configure(hw);
2700                 break;
2701
2702         default:
2703                 CSIO_INC_STATS(hw, n_evt_unexp);
2704                 break;
2705         }
2706 }
2707
2708 /*****************************************************************************/
2709 /* END: HW SM                                                                */
2710 /*****************************************************************************/
2711
2712 /*
2713  *      csio_handle_intr_status - table driven interrupt handler
2714  *      @hw: HW instance
2715  *      @reg: the interrupt status register to process
2716  *      @acts: table of interrupt actions
2717  *
2718  *      A table driven interrupt handler that applies a set of masks to an
2719  *      interrupt status word and performs the corresponding actions if the
2720  *      interrupts described by the mask have occured.  The actions include
2721  *      optionally emitting a warning or alert message. The table is terminated
2722  *      by an entry specifying mask 0.  Returns the number of fatal interrupt
2723  *      conditions.
2724  */
2725 int
2726 csio_handle_intr_status(struct csio_hw *hw, unsigned int reg,
2727                                  const struct intr_info *acts)
2728 {
2729         int fatal = 0;
2730         unsigned int mask = 0;
2731         unsigned int status = csio_rd_reg32(hw, reg);
2732
2733         for ( ; acts->mask; ++acts) {
2734                 if (!(status & acts->mask))
2735                         continue;
2736                 if (acts->fatal) {
2737                         fatal++;
2738                         csio_fatal(hw, "Fatal %s (0x%x)\n",
2739                                     acts->msg, status & acts->mask);
2740                 } else if (acts->msg)
2741                         csio_info(hw, "%s (0x%x)\n",
2742                                     acts->msg, status & acts->mask);
2743                 mask |= acts->mask;
2744         }
2745         status &= mask;
2746         if (status)                           /* clear processed interrupts */
2747                 csio_wr_reg32(hw, status, reg);
2748         return fatal;
2749 }
2750
2751 /*
2752  * TP interrupt handler.
2753  */
2754 static void csio_tp_intr_handler(struct csio_hw *hw)
2755 {
2756         static struct intr_info tp_intr_info[] = {
2757                 { 0x3fffffff, "TP parity error", -1, 1 },
2758                 { FLMTXFLSTEMPTY_F, "TP out of Tx pages", -1, 1 },
2759                 { 0, NULL, 0, 0 }
2760         };
2761
2762         if (csio_handle_intr_status(hw, TP_INT_CAUSE_A, tp_intr_info))
2763                 csio_hw_fatal_err(hw);
2764 }
2765
2766 /*
2767  * SGE interrupt handler.
2768  */
2769 static void csio_sge_intr_handler(struct csio_hw *hw)
2770 {
2771         uint64_t v;
2772
2773         static struct intr_info sge_intr_info[] = {
2774                 { ERR_CPL_EXCEED_IQE_SIZE_F,
2775                   "SGE received CPL exceeding IQE size", -1, 1 },
2776                 { ERR_INVALID_CIDX_INC_F,
2777                   "SGE GTS CIDX increment too large", -1, 0 },
2778                 { ERR_CPL_OPCODE_0_F, "SGE received 0-length CPL", -1, 0 },
2779                 { ERR_DROPPED_DB_F, "SGE doorbell dropped", -1, 0 },
2780                 { ERR_DATA_CPL_ON_HIGH_QID1_F | ERR_DATA_CPL_ON_HIGH_QID0_F,
2781                   "SGE IQID > 1023 received CPL for FL", -1, 0 },
2782                 { ERR_BAD_DB_PIDX3_F, "SGE DBP 3 pidx increment too large", -1,
2783                   0 },
2784                 { ERR_BAD_DB_PIDX2_F, "SGE DBP 2 pidx increment too large", -1,
2785                   0 },
2786                 { ERR_BAD_DB_PIDX1_F, "SGE DBP 1 pidx increment too large", -1,
2787                   0 },
2788                 { ERR_BAD_DB_PIDX0_F, "SGE DBP 0 pidx increment too large", -1,
2789                   0 },
2790                 { ERR_ING_CTXT_PRIO_F,
2791                   "SGE too many priority ingress contexts", -1, 0 },
2792                 { ERR_EGR_CTXT_PRIO_F,
2793                   "SGE too many priority egress contexts", -1, 0 },
2794                 { INGRESS_SIZE_ERR_F, "SGE illegal ingress QID", -1, 0 },
2795                 { EGRESS_SIZE_ERR_F, "SGE illegal egress QID", -1, 0 },
2796                 { 0, NULL, 0, 0 }
2797         };
2798
2799         v = (uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE1_A) |
2800             ((uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE2_A) << 32);
2801         if (v) {
2802                 csio_fatal(hw, "SGE parity error (%#llx)\n",
2803                             (unsigned long long)v);
2804                 csio_wr_reg32(hw, (uint32_t)(v & 0xFFFFFFFF),
2805                                                 SGE_INT_CAUSE1_A);
2806                 csio_wr_reg32(hw, (uint32_t)(v >> 32), SGE_INT_CAUSE2_A);
2807         }
2808
2809         v |= csio_handle_intr_status(hw, SGE_INT_CAUSE3_A, sge_intr_info);
2810
2811         if (csio_handle_intr_status(hw, SGE_INT_CAUSE3_A, sge_intr_info) ||
2812             v != 0)
2813                 csio_hw_fatal_err(hw);
2814 }
2815
2816 #define CIM_OBQ_INTR (OBQULP0PARERR_F | OBQULP1PARERR_F | OBQULP2PARERR_F |\
2817                       OBQULP3PARERR_F | OBQSGEPARERR_F | OBQNCSIPARERR_F)
2818 #define CIM_IBQ_INTR (IBQTP0PARERR_F | IBQTP1PARERR_F | IBQULPPARERR_F |\
2819                       IBQSGEHIPARERR_F | IBQSGELOPARERR_F | IBQNCSIPARERR_F)
2820
2821 /*
2822  * CIM interrupt handler.
2823  */
2824 static void csio_cim_intr_handler(struct csio_hw *hw)
2825 {
2826         static struct intr_info cim_intr_info[] = {
2827                 { PREFDROPINT_F, "CIM control register prefetch drop", -1, 1 },
2828                 { CIM_OBQ_INTR, "CIM OBQ parity error", -1, 1 },
2829                 { CIM_IBQ_INTR, "CIM IBQ parity error", -1, 1 },
2830                 { MBUPPARERR_F, "CIM mailbox uP parity error", -1, 1 },
2831                 { MBHOSTPARERR_F, "CIM mailbox host parity error", -1, 1 },
2832                 { TIEQINPARERRINT_F, "CIM TIEQ outgoing parity error", -1, 1 },
2833                 { TIEQOUTPARERRINT_F, "CIM TIEQ incoming parity error", -1, 1 },
2834                 { 0, NULL, 0, 0 }
2835         };
2836         static struct intr_info cim_upintr_info[] = {
2837                 { RSVDSPACEINT_F, "CIM reserved space access", -1, 1 },
2838                 { ILLTRANSINT_F, "CIM illegal transaction", -1, 1 },
2839                 { ILLWRINT_F, "CIM illegal write", -1, 1 },
2840                 { ILLRDINT_F, "CIM illegal read", -1, 1 },
2841                 { ILLRDBEINT_F, "CIM illegal read BE", -1, 1 },
2842                 { ILLWRBEINT_F, "CIM illegal write BE", -1, 1 },
2843                 { SGLRDBOOTINT_F, "CIM single read from boot space", -1, 1 },
2844                 { SGLWRBOOTINT_F, "CIM single write to boot space", -1, 1 },
2845                 { BLKWRBOOTINT_F, "CIM block write to boot space", -1, 1 },
2846                 { SGLRDFLASHINT_F, "CIM single read from flash space", -1, 1 },
2847                 { SGLWRFLASHINT_F, "CIM single write to flash space", -1, 1 },
2848                 { BLKWRFLASHINT_F, "CIM block write to flash space", -1, 1 },
2849                 { SGLRDEEPROMINT_F, "CIM single EEPROM read", -1, 1 },
2850                 { SGLWREEPROMINT_F, "CIM single EEPROM write", -1, 1 },
2851                 { BLKRDEEPROMINT_F, "CIM block EEPROM read", -1, 1 },
2852                 { BLKWREEPROMINT_F, "CIM block EEPROM write", -1, 1 },
2853                 { SGLRDCTLINT_F, "CIM single read from CTL space", -1, 1 },
2854                 { SGLWRCTLINT_F, "CIM single write to CTL space", -1, 1 },
2855                 { BLKRDCTLINT_F, "CIM block read from CTL space", -1, 1 },
2856                 { BLKWRCTLINT_F, "CIM block write to CTL space", -1, 1 },
2857                 { SGLRDPLINT_F, "CIM single read from PL space", -1, 1 },
2858                 { SGLWRPLINT_F, "CIM single write to PL space", -1, 1 },
2859                 { BLKRDPLINT_F, "CIM block read from PL space", -1, 1 },
2860                 { BLKWRPLINT_F, "CIM block write to PL space", -1, 1 },
2861                 { REQOVRLOOKUPINT_F, "CIM request FIFO overwrite", -1, 1 },
2862                 { RSPOVRLOOKUPINT_F, "CIM response FIFO overwrite", -1, 1 },
2863                 { TIMEOUTINT_F, "CIM PIF timeout", -1, 1 },
2864                 { TIMEOUTMAINT_F, "CIM PIF MA timeout", -1, 1 },
2865                 { 0, NULL, 0, 0 }
2866         };
2867
2868         int fat;
2869
2870         fat = csio_handle_intr_status(hw, CIM_HOST_INT_CAUSE_A,
2871                                       cim_intr_info) +
2872               csio_handle_intr_status(hw, CIM_HOST_UPACC_INT_CAUSE_A,
2873                                       cim_upintr_info);
2874         if (fat)
2875                 csio_hw_fatal_err(hw);
2876 }
2877
2878 /*
2879  * ULP RX interrupt handler.
2880  */
2881 static void csio_ulprx_intr_handler(struct csio_hw *hw)
2882 {
2883         static struct intr_info ulprx_intr_info[] = {
2884                 { 0x1800000, "ULPRX context error", -1, 1 },
2885                 { 0x7fffff, "ULPRX parity error", -1, 1 },
2886                 { 0, NULL, 0, 0 }
2887         };
2888
2889         if (csio_handle_intr_status(hw, ULP_RX_INT_CAUSE_A, ulprx_intr_info))
2890                 csio_hw_fatal_err(hw);
2891 }
2892
2893 /*
2894  * ULP TX interrupt handler.
2895  */
2896 static void csio_ulptx_intr_handler(struct csio_hw *hw)
2897 {
2898         static struct intr_info ulptx_intr_info[] = {
2899                 { PBL_BOUND_ERR_CH3_F, "ULPTX channel 3 PBL out of bounds", -1,
2900                   0 },
2901                 { PBL_BOUND_ERR_CH2_F, "ULPTX channel 2 PBL out of bounds", -1,
2902                   0 },
2903                 { PBL_BOUND_ERR_CH1_F, "ULPTX channel 1 PBL out of bounds", -1,
2904                   0 },
2905                 { PBL_BOUND_ERR_CH0_F, "ULPTX channel 0 PBL out of bounds", -1,
2906                   0 },
2907                 { 0xfffffff, "ULPTX parity error", -1, 1 },
2908                 { 0, NULL, 0, 0 }
2909         };
2910
2911         if (csio_handle_intr_status(hw, ULP_TX_INT_CAUSE_A, ulptx_intr_info))
2912                 csio_hw_fatal_err(hw);
2913 }
2914
2915 /*
2916  * PM TX interrupt handler.
2917  */
2918 static void csio_pmtx_intr_handler(struct csio_hw *hw)
2919 {
2920         static struct intr_info pmtx_intr_info[] = {
2921                 { PCMD_LEN_OVFL0_F, "PMTX channel 0 pcmd too large", -1, 1 },
2922                 { PCMD_LEN_OVFL1_F, "PMTX channel 1 pcmd too large", -1, 1 },
2923                 { PCMD_LEN_OVFL2_F, "PMTX channel 2 pcmd too large", -1, 1 },
2924                 { ZERO_C_CMD_ERROR_F, "PMTX 0-length pcmd", -1, 1 },
2925                 { 0xffffff0, "PMTX framing error", -1, 1 },
2926                 { OESPI_PAR_ERROR_F, "PMTX oespi parity error", -1, 1 },
2927                 { DB_OPTIONS_PAR_ERROR_F, "PMTX db_options parity error", -1,
2928                   1 },
2929                 { ICSPI_PAR_ERROR_F, "PMTX icspi parity error", -1, 1 },
2930                 { PMTX_C_PCMD_PAR_ERROR_F, "PMTX c_pcmd parity error", -1, 1},
2931                 { 0, NULL, 0, 0 }
2932         };
2933
2934         if (csio_handle_intr_status(hw, PM_TX_INT_CAUSE_A, pmtx_intr_info))
2935                 csio_hw_fatal_err(hw);
2936 }
2937
2938 /*
2939  * PM RX interrupt handler.
2940  */
2941 static void csio_pmrx_intr_handler(struct csio_hw *hw)
2942 {
2943         static struct intr_info pmrx_intr_info[] = {
2944                 { ZERO_E_CMD_ERROR_F, "PMRX 0-length pcmd", -1, 1 },
2945                 { 0x3ffff0, "PMRX framing error", -1, 1 },
2946                 { OCSPI_PAR_ERROR_F, "PMRX ocspi parity error", -1, 1 },
2947                 { DB_OPTIONS_PAR_ERROR_F, "PMRX db_options parity error", -1,
2948                   1 },
2949                 { IESPI_PAR_ERROR_F, "PMRX iespi parity error", -1, 1 },
2950                 { PMRX_E_PCMD_PAR_ERROR_F, "PMRX e_pcmd parity error", -1, 1},
2951                 { 0, NULL, 0, 0 }
2952         };
2953
2954         if (csio_handle_intr_status(hw, PM_RX_INT_CAUSE_A, pmrx_intr_info))
2955                 csio_hw_fatal_err(hw);
2956 }
2957
2958 /*
2959  * CPL switch interrupt handler.
2960  */
2961 static void csio_cplsw_intr_handler(struct csio_hw *hw)
2962 {
2963         static struct intr_info cplsw_intr_info[] = {
2964                 { CIM_OP_MAP_PERR_F, "CPLSW CIM op_map parity error", -1, 1 },
2965                 { CIM_OVFL_ERROR_F, "CPLSW CIM overflow", -1, 1 },
2966                 { TP_FRAMING_ERROR_F, "CPLSW TP framing error", -1, 1 },
2967                 { SGE_FRAMING_ERROR_F, "CPLSW SGE framing error", -1, 1 },
2968                 { CIM_FRAMING_ERROR_F, "CPLSW CIM framing error", -1, 1 },
2969                 { ZERO_SWITCH_ERROR_F, "CPLSW no-switch error", -1, 1 },
2970                 { 0, NULL, 0, 0 }
2971         };
2972
2973         if (csio_handle_intr_status(hw, CPL_INTR_CAUSE_A, cplsw_intr_info))
2974                 csio_hw_fatal_err(hw);
2975 }
2976
2977 /*
2978  * LE interrupt handler.
2979  */
2980 static void csio_le_intr_handler(struct csio_hw *hw)
2981 {
2982         enum chip_type chip = CHELSIO_CHIP_VERSION(hw->chip_id);
2983
2984         static struct intr_info le_intr_info[] = {
2985                 { LIPMISS_F, "LE LIP miss", -1, 0 },
2986                 { LIP0_F, "LE 0 LIP error", -1, 0 },
2987                 { PARITYERR_F, "LE parity error", -1, 1 },
2988                 { UNKNOWNCMD_F, "LE unknown command", -1, 1 },
2989                 { REQQPARERR_F, "LE request queue parity error", -1, 1 },
2990                 { 0, NULL, 0, 0 }
2991         };
2992
2993         static struct intr_info t6_le_intr_info[] = {
2994                 { T6_LIPMISS_F, "LE LIP miss", -1, 0 },
2995                 { T6_LIP0_F, "LE 0 LIP error", -1, 0 },
2996                 { TCAMINTPERR_F, "LE parity error", -1, 1 },
2997                 { T6_UNKNOWNCMD_F, "LE unknown command", -1, 1 },
2998                 { SSRAMINTPERR_F, "LE request queue parity error", -1, 1 },
2999                 { 0, NULL, 0, 0 }
3000         };
3001
3002         if (csio_handle_intr_status(hw, LE_DB_INT_CAUSE_A,
3003                                     (chip == CHELSIO_T5) ?
3004                                     le_intr_info : t6_le_intr_info))
3005                 csio_hw_fatal_err(hw);
3006 }
3007
3008 /*
3009  * MPS interrupt handler.
3010  */
3011 static void csio_mps_intr_handler(struct csio_hw *hw)
3012 {
3013         static struct intr_info mps_rx_intr_info[] = {
3014                 { 0xffffff, "MPS Rx parity error", -1, 1 },
3015                 { 0, NULL, 0, 0 }
3016         };
3017         static struct intr_info mps_tx_intr_info[] = {
3018                 { TPFIFO_V(TPFIFO_M), "MPS Tx TP FIFO parity error", -1, 1 },
3019                 { NCSIFIFO_F, "MPS Tx NC-SI FIFO parity error", -1, 1 },
3020                 { TXDATAFIFO_V(TXDATAFIFO_M), "MPS Tx data FIFO parity error",
3021                   -1, 1 },
3022                 { TXDESCFIFO_V(TXDESCFIFO_M), "MPS Tx desc FIFO parity error",
3023                   -1, 1 },
3024                 { BUBBLE_F, "MPS Tx underflow", -1, 1 },
3025                 { SECNTERR_F, "MPS Tx SOP/EOP error", -1, 1 },
3026                 { FRMERR_F, "MPS Tx framing error", -1, 1 },
3027                 { 0, NULL, 0, 0 }
3028         };
3029         static struct intr_info mps_trc_intr_info[] = {
3030                 { FILTMEM_V(FILTMEM_M), "MPS TRC filter parity error", -1, 1 },
3031                 { PKTFIFO_V(PKTFIFO_M), "MPS TRC packet FIFO parity error",
3032                   -1, 1 },
3033                 { MISCPERR_F, "MPS TRC misc parity error", -1, 1 },
3034                 { 0, NULL, 0, 0 }
3035         };
3036         static struct intr_info mps_stat_sram_intr_info[] = {
3037                 { 0x1fffff, "MPS statistics SRAM parity error", -1, 1 },
3038                 { 0, NULL, 0, 0 }
3039         };
3040         static struct intr_info mps_stat_tx_intr_info[] = {
3041                 { 0xfffff, "MPS statistics Tx FIFO parity error", -1, 1 },
3042                 { 0, NULL, 0, 0 }
3043         };
3044         static struct intr_info mps_stat_rx_intr_info[] = {
3045                 { 0xffffff, "MPS statistics Rx FIFO parity error", -1, 1 },
3046                 { 0, NULL, 0, 0 }
3047         };
3048         static struct intr_info mps_cls_intr_info[] = {
3049                 { MATCHSRAM_F, "MPS match SRAM parity error", -1, 1 },
3050                 { MATCHTCAM_F, "MPS match TCAM parity error", -1, 1 },
3051                 { HASHSRAM_F, "MPS hash SRAM parity error", -1, 1 },
3052                 { 0, NULL, 0, 0 }
3053         };
3054
3055         int fat;
3056
3057         fat = csio_handle_intr_status(hw, MPS_RX_PERR_INT_CAUSE_A,
3058                                       mps_rx_intr_info) +
3059               csio_handle_intr_status(hw, MPS_TX_INT_CAUSE_A,
3060                                       mps_tx_intr_info) +
3061               csio_handle_intr_status(hw, MPS_TRC_INT_CAUSE_A,
3062                                       mps_trc_intr_info) +
3063               csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_SRAM_A,
3064                                       mps_stat_sram_intr_info) +
3065               csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_TX_FIFO_A,
3066                                       mps_stat_tx_intr_info) +
3067               csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_RX_FIFO_A,
3068                                       mps_stat_rx_intr_info) +
3069               csio_handle_intr_status(hw, MPS_CLS_INT_CAUSE_A,
3070                                       mps_cls_intr_info);
3071
3072         csio_wr_reg32(hw, 0, MPS_INT_CAUSE_A);
3073         csio_rd_reg32(hw, MPS_INT_CAUSE_A);                    /* flush */
3074         if (fat)
3075                 csio_hw_fatal_err(hw);
3076 }
3077
3078 #define MEM_INT_MASK (PERR_INT_CAUSE_F | ECC_CE_INT_CAUSE_F | \
3079                       ECC_UE_INT_CAUSE_F)
3080
3081 /*
3082  * EDC/MC interrupt handler.
3083  */
3084 static void csio_mem_intr_handler(struct csio_hw *hw, int idx)
3085 {
3086         static const char name[3][5] = { "EDC0", "EDC1", "MC" };
3087
3088         unsigned int addr, cnt_addr, v;
3089
3090         if (idx <= MEM_EDC1) {
3091                 addr = EDC_REG(EDC_INT_CAUSE_A, idx);
3092                 cnt_addr = EDC_REG(EDC_ECC_STATUS_A, idx);
3093         } else {
3094                 addr = MC_INT_CAUSE_A;
3095                 cnt_addr = MC_ECC_STATUS_A;
3096         }
3097
3098         v = csio_rd_reg32(hw, addr) & MEM_INT_MASK;
3099         if (v & PERR_INT_CAUSE_F)
3100                 csio_fatal(hw, "%s FIFO parity error\n", name[idx]);
3101         if (v & ECC_CE_INT_CAUSE_F) {
3102                 uint32_t cnt = ECC_CECNT_G(csio_rd_reg32(hw, cnt_addr));
3103
3104                 csio_wr_reg32(hw, ECC_CECNT_V(ECC_CECNT_M), cnt_addr);
3105                 csio_warn(hw, "%u %s correctable ECC data error%s\n",
3106                             cnt, name[idx], cnt > 1 ? "s" : "");
3107         }
3108         if (v & ECC_UE_INT_CAUSE_F)
3109                 csio_fatal(hw, "%s uncorrectable ECC data error\n", name[idx]);
3110
3111         csio_wr_reg32(hw, v, addr);
3112         if (v & (PERR_INT_CAUSE_F | ECC_UE_INT_CAUSE_F))
3113                 csio_hw_fatal_err(hw);
3114 }
3115
3116 /*
3117  * MA interrupt handler.
3118  */
3119 static void csio_ma_intr_handler(struct csio_hw *hw)
3120 {
3121         uint32_t v, status = csio_rd_reg32(hw, MA_INT_CAUSE_A);
3122
3123         if (status & MEM_PERR_INT_CAUSE_F)
3124                 csio_fatal(hw, "MA parity error, parity status %#x\n",
3125                             csio_rd_reg32(hw, MA_PARITY_ERROR_STATUS_A));
3126         if (status & MEM_WRAP_INT_CAUSE_F) {
3127                 v = csio_rd_reg32(hw, MA_INT_WRAP_STATUS_A);
3128                 csio_fatal(hw,
3129                    "MA address wrap-around error by client %u to address %#x\n",
3130                    MEM_WRAP_CLIENT_NUM_G(v), MEM_WRAP_ADDRESS_G(v) << 4);
3131         }
3132         csio_wr_reg32(hw, status, MA_INT_CAUSE_A);
3133         csio_hw_fatal_err(hw);
3134 }
3135
3136 /*
3137  * SMB interrupt handler.
3138  */
3139 static void csio_smb_intr_handler(struct csio_hw *hw)
3140 {
3141         static struct intr_info smb_intr_info[] = {
3142                 { MSTTXFIFOPARINT_F, "SMB master Tx FIFO parity error", -1, 1 },
3143                 { MSTRXFIFOPARINT_F, "SMB master Rx FIFO parity error", -1, 1 },
3144                 { SLVFIFOPARINT_F, "SMB slave FIFO parity error", -1, 1 },
3145                 { 0, NULL, 0, 0 }
3146         };
3147
3148         if (csio_handle_intr_status(hw, SMB_INT_CAUSE_A, smb_intr_info))
3149                 csio_hw_fatal_err(hw);
3150 }
3151
3152 /*
3153  * NC-SI interrupt handler.
3154  */
3155 static void csio_ncsi_intr_handler(struct csio_hw *hw)
3156 {
3157         static struct intr_info ncsi_intr_info[] = {
3158                 { CIM_DM_PRTY_ERR_F, "NC-SI CIM parity error", -1, 1 },
3159                 { MPS_DM_PRTY_ERR_F, "NC-SI MPS parity error", -1, 1 },
3160                 { TXFIFO_PRTY_ERR_F, "NC-SI Tx FIFO parity error", -1, 1 },
3161                 { RXFIFO_PRTY_ERR_F, "NC-SI Rx FIFO parity error", -1, 1 },
3162                 { 0, NULL, 0, 0 }
3163         };
3164
3165         if (csio_handle_intr_status(hw, NCSI_INT_CAUSE_A, ncsi_intr_info))
3166                 csio_hw_fatal_err(hw);
3167 }
3168
3169 /*
3170  * XGMAC interrupt handler.
3171  */
3172 static void csio_xgmac_intr_handler(struct csio_hw *hw, int port)
3173 {
3174         uint32_t v = csio_rd_reg32(hw, T5_PORT_REG(port, MAC_PORT_INT_CAUSE_A));
3175
3176         v &= TXFIFO_PRTY_ERR_F | RXFIFO_PRTY_ERR_F;
3177         if (!v)
3178                 return;
3179
3180         if (v & TXFIFO_PRTY_ERR_F)
3181                 csio_fatal(hw, "XGMAC %d Tx FIFO parity error\n", port);
3182         if (v & RXFIFO_PRTY_ERR_F)
3183                 csio_fatal(hw, "XGMAC %d Rx FIFO parity error\n", port);
3184         csio_wr_reg32(hw, v, T5_PORT_REG(port, MAC_PORT_INT_CAUSE_A));
3185         csio_hw_fatal_err(hw);
3186 }
3187
3188 /*
3189  * PL interrupt handler.
3190  */
3191 static void csio_pl_intr_handler(struct csio_hw *hw)
3192 {
3193         static struct intr_info pl_intr_info[] = {
3194                 { FATALPERR_F, "T4 fatal parity error", -1, 1 },
3195                 { PERRVFID_F, "PL VFID_MAP parity error", -1, 1 },
3196                 { 0, NULL, 0, 0 }
3197         };
3198
3199         if (csio_handle_intr_status(hw, PL_PL_INT_CAUSE_A, pl_intr_info))
3200                 csio_hw_fatal_err(hw);
3201 }
3202
3203 /*
3204  *      csio_hw_slow_intr_handler - control path interrupt handler
3205  *      @hw: HW module
3206  *
3207  *      Interrupt handler for non-data global interrupt events, e.g., errors.
3208  *      The designation 'slow' is because it involves register reads, while
3209  *      data interrupts typically don't involve any MMIOs.
3210  */
3211 int
3212 csio_hw_slow_intr_handler(struct csio_hw *hw)
3213 {
3214         uint32_t cause = csio_rd_reg32(hw, PL_INT_CAUSE_A);
3215
3216         if (!(cause & CSIO_GLBL_INTR_MASK)) {
3217                 CSIO_INC_STATS(hw, n_plint_unexp);
3218                 return 0;
3219         }
3220
3221         csio_dbg(hw, "Slow interrupt! cause: 0x%x\n", cause);
3222
3223         CSIO_INC_STATS(hw, n_plint_cnt);
3224
3225         if (cause & CIM_F)
3226                 csio_cim_intr_handler(hw);
3227
3228         if (cause & MPS_F)
3229                 csio_mps_intr_handler(hw);
3230
3231         if (cause & NCSI_F)
3232                 csio_ncsi_intr_handler(hw);
3233
3234         if (cause & PL_F)
3235                 csio_pl_intr_handler(hw);
3236
3237         if (cause & SMB_F)
3238                 csio_smb_intr_handler(hw);
3239
3240         if (cause & XGMAC0_F)
3241                 csio_xgmac_intr_handler(hw, 0);
3242
3243         if (cause & XGMAC1_F)
3244                 csio_xgmac_intr_handler(hw, 1);
3245
3246         if (cause & XGMAC_KR0_F)
3247                 csio_xgmac_intr_handler(hw, 2);
3248
3249         if (cause & XGMAC_KR1_F)
3250                 csio_xgmac_intr_handler(hw, 3);
3251
3252         if (cause & PCIE_F)
3253                 hw->chip_ops->chip_pcie_intr_handler(hw);
3254
3255         if (cause & MC_F)
3256                 csio_mem_intr_handler(hw, MEM_MC);
3257
3258         if (cause & EDC0_F)
3259                 csio_mem_intr_handler(hw, MEM_EDC0);
3260
3261         if (cause & EDC1_F)
3262                 csio_mem_intr_handler(hw, MEM_EDC1);
3263
3264         if (cause & LE_F)
3265                 csio_le_intr_handler(hw);
3266
3267         if (cause & TP_F)
3268                 csio_tp_intr_handler(hw);
3269
3270         if (cause & MA_F)
3271                 csio_ma_intr_handler(hw);
3272
3273         if (cause & PM_TX_F)
3274                 csio_pmtx_intr_handler(hw);
3275
3276         if (cause & PM_RX_F)
3277                 csio_pmrx_intr_handler(hw);
3278
3279         if (cause & ULP_RX_F)
3280                 csio_ulprx_intr_handler(hw);
3281
3282         if (cause & CPL_SWITCH_F)
3283                 csio_cplsw_intr_handler(hw);
3284
3285         if (cause & SGE_F)
3286                 csio_sge_intr_handler(hw);
3287
3288         if (cause & ULP_TX_F)
3289                 csio_ulptx_intr_handler(hw);
3290
3291         /* Clear the interrupts just processed for which we are the master. */
3292         csio_wr_reg32(hw, cause & CSIO_GLBL_INTR_MASK, PL_INT_CAUSE_A);
3293         csio_rd_reg32(hw, PL_INT_CAUSE_A); /* flush */
3294
3295         return 1;
3296 }
3297
3298 /*****************************************************************************
3299  * HW <--> mailbox interfacing routines.
3300  ****************************************************************************/
3301 /*
3302  * csio_mberr_worker - Worker thread (dpc) for mailbox/error completions
3303  *
3304  * @data: Private data pointer.
3305  *
3306  * Called from worker thread context.
3307  */
3308 static void
3309 csio_mberr_worker(void *data)
3310 {
3311         struct csio_hw *hw = (struct csio_hw *)data;
3312         struct csio_mbm *mbm = &hw->mbm;
3313         LIST_HEAD(cbfn_q);
3314         struct csio_mb *mbp_next;
3315         int rv;
3316
3317         del_timer_sync(&mbm->timer);
3318
3319         spin_lock_irq(&hw->lock);
3320         if (list_empty(&mbm->cbfn_q)) {
3321                 spin_unlock_irq(&hw->lock);
3322                 return;
3323         }
3324
3325         list_splice_tail_init(&mbm->cbfn_q, &cbfn_q);
3326         mbm->stats.n_cbfnq = 0;
3327
3328         /* Try to start waiting mailboxes */
3329         if (!list_empty(&mbm->req_q)) {
3330                 mbp_next = list_first_entry(&mbm->req_q, struct csio_mb, list);
3331                 list_del_init(&mbp_next->list);
3332
3333                 rv = csio_mb_issue(hw, mbp_next);
3334                 if (rv != 0)
3335                         list_add_tail(&mbp_next->list, &mbm->req_q);
3336                 else
3337                         CSIO_DEC_STATS(mbm, n_activeq);
3338         }
3339         spin_unlock_irq(&hw->lock);
3340
3341         /* Now callback completions */
3342         csio_mb_completions(hw, &cbfn_q);
3343 }
3344
3345 /*
3346  * csio_hw_mb_timer - Top-level Mailbox timeout handler.
3347  *
3348  * @data: private data pointer
3349  *
3350  **/
3351 static void
3352 csio_hw_mb_timer(uintptr_t data)
3353 {
3354         struct csio_hw *hw = (struct csio_hw *)data;
3355         struct csio_mb *mbp = NULL;
3356
3357         spin_lock_irq(&hw->lock);
3358         mbp = csio_mb_tmo_handler(hw);
3359         spin_unlock_irq(&hw->lock);
3360
3361         /* Call back the function for the timed-out Mailbox */
3362         if (mbp)
3363                 mbp->mb_cbfn(hw, mbp);
3364
3365 }
3366
3367 /*
3368  * csio_hw_mbm_cleanup - Cleanup Mailbox module.
3369  * @hw: HW module
3370  *
3371  * Called with lock held, should exit with lock held.
3372  * Cancels outstanding mailboxes (waiting, in-flight) and gathers them
3373  * into a local queue. Drops lock and calls the completions. Holds
3374  * lock and returns.
3375  */
3376 static void
3377 csio_hw_mbm_cleanup(struct csio_hw *hw)
3378 {
3379         LIST_HEAD(cbfn_q);
3380
3381         csio_mb_cancel_all(hw, &cbfn_q);
3382
3383         spin_unlock_irq(&hw->lock);
3384         csio_mb_completions(hw, &cbfn_q);
3385         spin_lock_irq(&hw->lock);
3386 }
3387
3388 /*****************************************************************************
3389  * Event handling
3390  ****************************************************************************/
3391 int
3392 csio_enqueue_evt(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
3393                         uint16_t len)
3394 {
3395         struct csio_evt_msg *evt_entry = NULL;
3396
3397         if (type >= CSIO_EVT_MAX)
3398                 return -EINVAL;
3399
3400         if (len > CSIO_EVT_MSG_SIZE)
3401                 return -EINVAL;
3402
3403         if (hw->flags & CSIO_HWF_FWEVT_STOP)
3404                 return -EINVAL;
3405
3406         if (list_empty(&hw->evt_free_q)) {
3407                 csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
3408                          type, len);
3409                 return -ENOMEM;
3410         }
3411
3412         evt_entry = list_first_entry(&hw->evt_free_q,
3413                                      struct csio_evt_msg, list);
3414         list_del_init(&evt_entry->list);
3415
3416         /* copy event msg and queue the event */
3417         evt_entry->type = type;
3418         memcpy((void *)evt_entry->data, evt_msg, len);
3419         list_add_tail(&evt_entry->list, &hw->evt_active_q);
3420
3421         CSIO_DEC_STATS(hw, n_evt_freeq);
3422         CSIO_INC_STATS(hw, n_evt_activeq);
3423
3424         return 0;
3425 }
3426
3427 static int
3428 csio_enqueue_evt_lock(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
3429                         uint16_t len, bool msg_sg)
3430 {
3431         struct csio_evt_msg *evt_entry = NULL;
3432         struct csio_fl_dma_buf *fl_sg;
3433         uint32_t off = 0;
3434         unsigned long flags;
3435         int n, ret = 0;
3436
3437         if (type >= CSIO_EVT_MAX)
3438                 return -EINVAL;
3439
3440         if (len > CSIO_EVT_MSG_SIZE)
3441                 return -EINVAL;
3442
3443         spin_lock_irqsave(&hw->lock, flags);
3444         if (hw->flags & CSIO_HWF_FWEVT_STOP) {
3445                 ret = -EINVAL;
3446                 goto out;
3447         }
3448
3449         if (list_empty(&hw->evt_free_q)) {
3450                 csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
3451                          type, len);
3452                 ret = -ENOMEM;
3453                 goto out;
3454         }
3455
3456         evt_entry = list_first_entry(&hw->evt_free_q,
3457                                      struct csio_evt_msg, list);
3458         list_del_init(&evt_entry->list);
3459
3460         /* copy event msg and queue the event */
3461         evt_entry->type = type;
3462
3463         /* If Payload in SG list*/
3464         if (msg_sg) {
3465                 fl_sg = (struct csio_fl_dma_buf *) evt_msg;
3466                 for (n = 0; (n < CSIO_MAX_FLBUF_PER_IQWR && off < len); n++) {
3467                         memcpy((void *)((uintptr_t)evt_entry->data + off),
3468                                 fl_sg->flbufs[n].vaddr,
3469                                 fl_sg->flbufs[n].len);
3470                         off += fl_sg->flbufs[n].len;
3471                 }
3472         } else
3473                 memcpy((void *)evt_entry->data, evt_msg, len);
3474
3475         list_add_tail(&evt_entry->list, &hw->evt_active_q);
3476         CSIO_DEC_STATS(hw, n_evt_freeq);
3477         CSIO_INC_STATS(hw, n_evt_activeq);
3478 out:
3479         spin_unlock_irqrestore(&hw->lock, flags);
3480         return ret;
3481 }
3482
3483 static void
3484 csio_free_evt(struct csio_hw *hw, struct csio_evt_msg *evt_entry)
3485 {
3486         if (evt_entry) {
3487                 spin_lock_irq(&hw->lock);
3488                 list_del_init(&evt_entry->list);
3489                 list_add_tail(&evt_entry->list, &hw->evt_free_q);
3490                 CSIO_DEC_STATS(hw, n_evt_activeq);
3491                 CSIO_INC_STATS(hw, n_evt_freeq);
3492                 spin_unlock_irq(&hw->lock);
3493         }
3494 }
3495
3496 void
3497 csio_evtq_flush(struct csio_hw *hw)
3498 {
3499         uint32_t count;
3500         count = 30;
3501         while (hw->flags & CSIO_HWF_FWEVT_PENDING && count--) {
3502                 spin_unlock_irq(&hw->lock);
3503                 msleep(2000);
3504                 spin_lock_irq(&hw->lock);
3505         }
3506
3507         CSIO_DB_ASSERT(!(hw->flags & CSIO_HWF_FWEVT_PENDING));
3508 }
3509
3510 static void
3511 csio_evtq_stop(struct csio_hw *hw)
3512 {
3513         hw->flags |= CSIO_HWF_FWEVT_STOP;
3514 }
3515
3516 static void
3517 csio_evtq_start(struct csio_hw *hw)
3518 {
3519         hw->flags &= ~CSIO_HWF_FWEVT_STOP;
3520 }
3521
3522 static void
3523 csio_evtq_cleanup(struct csio_hw *hw)
3524 {
3525         struct list_head *evt_entry, *next_entry;
3526
3527         /* Release outstanding events from activeq to freeq*/
3528         if (!list_empty(&hw->evt_active_q))
3529                 list_splice_tail_init(&hw->evt_active_q, &hw->evt_free_q);
3530
3531         hw->stats.n_evt_activeq = 0;
3532         hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
3533
3534         /* Freeup event entry */
3535         list_for_each_safe(evt_entry, next_entry, &hw->evt_free_q) {
3536                 kfree(evt_entry);
3537                 CSIO_DEC_STATS(hw, n_evt_freeq);
3538         }
3539
3540         hw->stats.n_evt_freeq = 0;
3541 }
3542
3543
3544 static void
3545 csio_process_fwevtq_entry(struct csio_hw *hw, void *wr, uint32_t len,
3546                           struct csio_fl_dma_buf *flb, void *priv)
3547 {
3548         __u8 op;
3549         void *msg = NULL;
3550         uint32_t msg_len = 0;
3551         bool msg_sg = 0;
3552
3553         op = ((struct rss_header *) wr)->opcode;
3554         if (op == CPL_FW6_PLD) {
3555                 CSIO_INC_STATS(hw, n_cpl_fw6_pld);
3556                 if (!flb || !flb->totlen) {
3557                         CSIO_INC_STATS(hw, n_cpl_unexp);
3558                         return;
3559                 }
3560
3561                 msg = (void *) flb;
3562                 msg_len = flb->totlen;
3563                 msg_sg = 1;
3564         } else if (op == CPL_FW6_MSG || op == CPL_FW4_MSG) {
3565
3566                 CSIO_INC_STATS(hw, n_cpl_fw6_msg);
3567                 /* skip RSS header */
3568                 msg = (void *)((uintptr_t)wr + sizeof(__be64));
3569                 msg_len = (op == CPL_FW6_MSG) ? sizeof(struct cpl_fw6_msg) :
3570                            sizeof(struct cpl_fw4_msg);
3571         } else {
3572                 csio_warn(hw, "unexpected CPL %#x on FW event queue\n", op);
3573                 CSIO_INC_STATS(hw, n_cpl_unexp);
3574                 return;
3575         }
3576
3577         /*
3578          * Enqueue event to EventQ. Events processing happens
3579          * in Event worker thread context
3580          */
3581         if (csio_enqueue_evt_lock(hw, CSIO_EVT_FW, msg,
3582                                   (uint16_t)msg_len, msg_sg))
3583                 CSIO_INC_STATS(hw, n_evt_drop);
3584 }
3585
3586 void
3587 csio_evtq_worker(struct work_struct *work)
3588 {
3589         struct csio_hw *hw = container_of(work, struct csio_hw, evtq_work);
3590         struct list_head *evt_entry, *next_entry;
3591         LIST_HEAD(evt_q);
3592         struct csio_evt_msg     *evt_msg;
3593         struct cpl_fw6_msg *msg;
3594         struct csio_rnode *rn;
3595         int rv = 0;
3596         uint8_t evtq_stop = 0;
3597
3598         csio_dbg(hw, "event worker thread active evts#%d\n",
3599                  hw->stats.n_evt_activeq);
3600
3601         spin_lock_irq(&hw->lock);
3602         while (!list_empty(&hw->evt_active_q)) {
3603                 list_splice_tail_init(&hw->evt_active_q, &evt_q);
3604                 spin_unlock_irq(&hw->lock);
3605
3606                 list_for_each_safe(evt_entry, next_entry, &evt_q) {
3607                         evt_msg = (struct csio_evt_msg *) evt_entry;
3608
3609                         /* Drop events if queue is STOPPED */
3610                         spin_lock_irq(&hw->lock);
3611                         if (hw->flags & CSIO_HWF_FWEVT_STOP)
3612                                 evtq_stop = 1;
3613                         spin_unlock_irq(&hw->lock);
3614                         if (evtq_stop) {
3615                                 CSIO_INC_STATS(hw, n_evt_drop);
3616                                 goto free_evt;
3617                         }
3618
3619                         switch (evt_msg->type) {
3620                         case CSIO_EVT_FW:
3621                                 msg = (struct cpl_fw6_msg *)(evt_msg->data);
3622
3623                                 if ((msg->opcode == CPL_FW6_MSG ||
3624                                      msg->opcode == CPL_FW4_MSG) &&
3625                                     !msg->type) {
3626                                         rv = csio_mb_fwevt_handler(hw,
3627                                                                 msg->data);
3628                                         if (!rv)
3629                                                 break;
3630                                         /* Handle any remaining fw events */
3631                                         csio_fcoe_fwevt_handler(hw,
3632                                                         msg->opcode, msg->data);
3633                                 } else if (msg->opcode == CPL_FW6_PLD) {
3634
3635                                         csio_fcoe_fwevt_handler(hw,
3636                                                         msg->opcode, msg->data);
3637                                 } else {
3638                                         csio_warn(hw,
3639                                              "Unhandled FW msg op %x type %x\n",
3640                                                   msg->opcode, msg->type);
3641                                         CSIO_INC_STATS(hw, n_evt_drop);
3642                                 }
3643                                 break;
3644
3645                         case CSIO_EVT_MBX:
3646                                 csio_mberr_worker(hw);
3647                                 break;
3648
3649                         case CSIO_EVT_DEV_LOSS:
3650                                 memcpy(&rn, evt_msg->data, sizeof(rn));
3651                                 csio_rnode_devloss_handler(rn);
3652                                 break;
3653
3654                         default:
3655                                 csio_warn(hw, "Unhandled event %x on evtq\n",
3656                                           evt_msg->type);
3657                                 CSIO_INC_STATS(hw, n_evt_unexp);
3658                                 break;
3659                         }
3660 free_evt:
3661                         csio_free_evt(hw, evt_msg);
3662                 }
3663
3664                 spin_lock_irq(&hw->lock);
3665         }
3666         hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
3667         spin_unlock_irq(&hw->lock);
3668 }
3669
3670 int
3671 csio_fwevtq_handler(struct csio_hw *hw)
3672 {
3673         int rv;
3674
3675         if (csio_q_iqid(hw, hw->fwevt_iq_idx) == CSIO_MAX_QID) {
3676                 CSIO_INC_STATS(hw, n_int_stray);
3677                 return -EINVAL;
3678         }
3679
3680         rv = csio_wr_process_iq_idx(hw, hw->fwevt_iq_idx,
3681                            csio_process_fwevtq_entry, NULL);
3682         return rv;
3683 }
3684
3685 /****************************************************************************
3686  * Entry points
3687  ****************************************************************************/
3688
3689 /* Management module */
3690 /*
3691  * csio_mgmt_req_lookup - Lookup the given IO req exist in Active Q.
3692  * mgmt - mgmt module
3693  * @io_req - io request
3694  *
3695  * Return - 0:if given IO Req exists in active Q.
3696  *          -EINVAL  :if lookup fails.
3697  */
3698 int
3699 csio_mgmt_req_lookup(struct csio_mgmtm *mgmtm, struct csio_ioreq *io_req)
3700 {
3701         struct list_head *tmp;
3702
3703         /* Lookup ioreq in the ACTIVEQ */
3704         list_for_each(tmp, &mgmtm->active_q) {
3705                 if (io_req == (struct csio_ioreq *)tmp)
3706                         return 0;
3707         }
3708         return -EINVAL;
3709 }
3710
3711 #define ECM_MIN_TMO     1000    /* Minimum timeout value for req */
3712
3713 /*
3714  * csio_mgmts_tmo_handler - MGMT IO Timeout handler.
3715  * @data - Event data.
3716  *
3717  * Return - none.
3718  */
3719 static void
3720 csio_mgmt_tmo_handler(uintptr_t data)
3721 {
3722         struct csio_mgmtm *mgmtm = (struct csio_mgmtm *) data;
3723         struct list_head *tmp;
3724         struct csio_ioreq *io_req;
3725
3726         csio_dbg(mgmtm->hw, "Mgmt timer invoked!\n");
3727
3728         spin_lock_irq(&mgmtm->hw->lock);
3729
3730         list_for_each(tmp, &mgmtm->active_q) {
3731                 io_req = (struct csio_ioreq *) tmp;
3732                 io_req->tmo -= min_t(uint32_t, io_req->tmo, ECM_MIN_TMO);
3733
3734                 if (!io_req->tmo) {
3735                         /* Dequeue the request from retry Q. */
3736                         tmp = csio_list_prev(tmp);
3737                         list_del_init(&io_req->sm.sm_list);
3738                         if (io_req->io_cbfn) {
3739                                 /* io_req will be freed by completion handler */
3740                                 io_req->wr_status = -ETIMEDOUT;
3741                                 io_req->io_cbfn(mgmtm->hw, io_req);
3742                         } else {
3743                                 CSIO_DB_ASSERT(0);
3744                         }
3745                 }
3746         }
3747
3748         /* If retry queue is not empty, re-arm timer */
3749         if (!list_empty(&mgmtm->active_q))
3750                 mod_timer(&mgmtm->mgmt_timer,
3751                           jiffies + msecs_to_jiffies(ECM_MIN_TMO));
3752         spin_unlock_irq(&mgmtm->hw->lock);
3753 }
3754
3755 static void
3756 csio_mgmtm_cleanup(struct csio_mgmtm *mgmtm)
3757 {
3758         struct csio_hw *hw = mgmtm->hw;
3759         struct csio_ioreq *io_req;
3760         struct list_head *tmp;
3761         uint32_t count;
3762
3763         count = 30;
3764         /* Wait for all outstanding req to complete gracefully */
3765         while ((!list_empty(&mgmtm->active_q)) && count--) {
3766                 spin_unlock_irq(&hw->lock);
3767                 msleep(2000);
3768                 spin_lock_irq(&hw->lock);
3769         }
3770
3771         /* release outstanding req from ACTIVEQ */
3772         list_for_each(tmp, &mgmtm->active_q) {
3773                 io_req = (struct csio_ioreq *) tmp;
3774                 tmp = csio_list_prev(tmp);
3775                 list_del_init(&io_req->sm.sm_list);
3776                 mgmtm->stats.n_active--;
3777                 if (io_req->io_cbfn) {
3778                         /* io_req will be freed by completion handler */
3779                         io_req->wr_status = -ETIMEDOUT;
3780                         io_req->io_cbfn(mgmtm->hw, io_req);
3781                 }
3782         }
3783 }
3784
3785 /*
3786  * csio_mgmt_init - Mgmt module init entry point
3787  * @mgmtsm - mgmt module
3788  * @hw   - HW module
3789  *
3790  * Initialize mgmt timer, resource wait queue, active queue,
3791  * completion q. Allocate Egress and Ingress
3792  * WR queues and save off the queue index returned by the WR
3793  * module for future use. Allocate and save off mgmt reqs in the
3794  * mgmt_req_freelist for future use. Make sure their SM is initialized
3795  * to uninit state.
3796  * Returns: 0 - on success
3797  *          -ENOMEM   - on error.
3798  */
3799 static int
3800 csio_mgmtm_init(struct csio_mgmtm *mgmtm, struct csio_hw *hw)
3801 {
3802         struct timer_list *timer = &mgmtm->mgmt_timer;
3803
3804         init_timer(timer);
3805         timer->function = csio_mgmt_tmo_handler;
3806         timer->data = (unsigned long)mgmtm;
3807
3808         INIT_LIST_HEAD(&mgmtm->active_q);
3809         INIT_LIST_HEAD(&mgmtm->cbfn_q);
3810
3811         mgmtm->hw = hw;
3812         /*mgmtm->iq_idx = hw->fwevt_iq_idx;*/
3813
3814         return 0;
3815 }
3816
3817 /*
3818  * csio_mgmtm_exit - MGMT module exit entry point
3819  * @mgmtsm - mgmt module
3820  *
3821  * This function called during MGMT module uninit.
3822  * Stop timers, free ioreqs allocated.
3823  * Returns: None
3824  *
3825  */
3826 static void
3827 csio_mgmtm_exit(struct csio_mgmtm *mgmtm)
3828 {
3829         del_timer_sync(&mgmtm->mgmt_timer);
3830 }
3831
3832
3833 /**
3834  * csio_hw_start - Kicks off the HW State machine
3835  * @hw:         Pointer to HW module.
3836  *
3837  * It is assumed that the initialization is a synchronous operation.
3838  * So when we return afer posting the event, the HW SM should be in
3839  * the ready state, if there were no errors during init.
3840  */
3841 int
3842 csio_hw_start(struct csio_hw *hw)
3843 {
3844         spin_lock_irq(&hw->lock);
3845         csio_post_event(&hw->sm, CSIO_HWE_CFG);
3846         spin_unlock_irq(&hw->lock);
3847
3848         if (csio_is_hw_ready(hw))
3849                 return 0;
3850         else if (csio_match_state(hw, csio_hws_uninit))
3851                 return -EINVAL;
3852         else
3853                 return -ENODEV;
3854 }
3855
3856 int
3857 csio_hw_stop(struct csio_hw *hw)
3858 {
3859         csio_post_event(&hw->sm, CSIO_HWE_PCI_REMOVE);
3860
3861         if (csio_is_hw_removing(hw))
3862                 return 0;
3863         else
3864                 return -EINVAL;
3865 }
3866
3867 /* Max reset retries */
3868 #define CSIO_MAX_RESET_RETRIES  3
3869
3870 /**
3871  * csio_hw_reset - Reset the hardware
3872  * @hw:         HW module.
3873  *
3874  * Caller should hold lock across this function.
3875  */
3876 int
3877 csio_hw_reset(struct csio_hw *hw)
3878 {
3879         if (!csio_is_hw_master(hw))
3880                 return -EPERM;
3881
3882         if (hw->rst_retries >= CSIO_MAX_RESET_RETRIES) {
3883                 csio_dbg(hw, "Max hw reset attempts reached..");
3884                 return -EINVAL;
3885         }
3886
3887         hw->rst_retries++;
3888         csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET);
3889
3890         if (csio_is_hw_ready(hw)) {
3891                 hw->rst_retries = 0;
3892                 hw->stats.n_reset_start = jiffies_to_msecs(jiffies);
3893                 return 0;
3894         } else
3895                 return -EINVAL;
3896 }
3897
3898 /*
3899  * csio_hw_get_device_id - Caches the Adapter's vendor & device id.
3900  * @hw: HW module.
3901  */
3902 static void
3903 csio_hw_get_device_id(struct csio_hw *hw)
3904 {
3905         /* Is the adapter device id cached already ?*/
3906         if (csio_is_dev_id_cached(hw))
3907                 return;
3908
3909         /* Get the PCI vendor & device id */
3910         pci_read_config_word(hw->pdev, PCI_VENDOR_ID,
3911                              &hw->params.pci.vendor_id);
3912         pci_read_config_word(hw->pdev, PCI_DEVICE_ID,
3913                              &hw->params.pci.device_id);
3914
3915         csio_dev_id_cached(hw);
3916         hw->chip_id = (hw->params.pci.device_id & CSIO_HW_CHIP_MASK);
3917
3918 } /* csio_hw_get_device_id */
3919
3920 /*
3921  * csio_hw_set_description - Set the model, description of the hw.
3922  * @hw: HW module.
3923  * @ven_id: PCI Vendor ID
3924  * @dev_id: PCI Device ID
3925  */
3926 static void
3927 csio_hw_set_description(struct csio_hw *hw, uint16_t ven_id, uint16_t dev_id)
3928 {
3929         uint32_t adap_type, prot_type;
3930
3931         if (ven_id == CSIO_VENDOR_ID) {
3932                 prot_type = (dev_id & CSIO_ASIC_DEVID_PROTO_MASK);
3933                 adap_type = (dev_id & CSIO_ASIC_DEVID_TYPE_MASK);
3934
3935                 if (prot_type == CSIO_T5_FCOE_ASIC) {
3936                         memcpy(hw->hw_ver,
3937                                csio_t5_fcoe_adapters[adap_type].model_no, 16);
3938                         memcpy(hw->model_desc,
3939                                csio_t5_fcoe_adapters[adap_type].description,
3940                                32);
3941                 } else {
3942                         char tempName[32] = "Chelsio FCoE Controller";
3943                         memcpy(hw->model_desc, tempName, 32);
3944                 }
3945         }
3946 } /* csio_hw_set_description */
3947
3948 /**
3949  * csio_hw_init - Initialize HW module.
3950  * @hw:         Pointer to HW module.
3951  *
3952  * Initialize the members of the HW module.
3953  */
3954 int
3955 csio_hw_init(struct csio_hw *hw)
3956 {
3957         int rv = -EINVAL;
3958         uint32_t i;
3959         uint16_t ven_id, dev_id;
3960         struct csio_evt_msg     *evt_entry;
3961
3962         INIT_LIST_HEAD(&hw->sm.sm_list);
3963         csio_init_state(&hw->sm, csio_hws_uninit);
3964         spin_lock_init(&hw->lock);
3965         INIT_LIST_HEAD(&hw->sln_head);
3966
3967         /* Get the PCI vendor & device id */
3968         csio_hw_get_device_id(hw);
3969
3970         strcpy(hw->name, CSIO_HW_NAME);
3971
3972         /* Initialize the HW chip ops T5 specific ops */
3973         hw->chip_ops = &t5_ops;
3974
3975         /* Set the model & its description */
3976
3977         ven_id = hw->params.pci.vendor_id;
3978         dev_id = hw->params.pci.device_id;
3979
3980         csio_hw_set_description(hw, ven_id, dev_id);
3981
3982         /* Initialize default log level */
3983         hw->params.log_level = (uint32_t) csio_dbg_level;
3984
3985         csio_set_fwevt_intr_idx(hw, -1);
3986         csio_set_nondata_intr_idx(hw, -1);
3987
3988         /* Init all the modules: Mailbox, WorkRequest and Transport */
3989         if (csio_mbm_init(csio_hw_to_mbm(hw), hw, csio_hw_mb_timer))
3990                 goto err;
3991
3992         rv = csio_wrm_init(csio_hw_to_wrm(hw), hw);
3993         if (rv)
3994                 goto err_mbm_exit;
3995
3996         rv = csio_scsim_init(csio_hw_to_scsim(hw), hw);
3997         if (rv)
3998                 goto err_wrm_exit;
3999
4000         rv = csio_mgmtm_init(csio_hw_to_mgmtm(hw), hw);
4001         if (rv)
4002                 goto err_scsim_exit;
4003         /* Pre-allocate evtq and initialize them */
4004         INIT_LIST_HEAD(&hw->evt_active_q);
4005         INIT_LIST_HEAD(&hw->evt_free_q);
4006         for (i = 0; i < csio_evtq_sz; i++) {
4007
4008                 evt_entry = kzalloc(sizeof(struct csio_evt_msg), GFP_KERNEL);
4009                 if (!evt_entry) {
4010                         rv = -ENOMEM;
4011                         csio_err(hw, "Failed to initialize eventq");
4012                         goto err_evtq_cleanup;
4013                 }
4014
4015                 list_add_tail(&evt_entry->list, &hw->evt_free_q);
4016                 CSIO_INC_STATS(hw, n_evt_freeq);
4017         }
4018
4019         hw->dev_num = dev_num;
4020         dev_num++;
4021
4022         return 0;
4023
4024 err_evtq_cleanup:
4025         csio_evtq_cleanup(hw);
4026         csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
4027 err_scsim_exit:
4028         csio_scsim_exit(csio_hw_to_scsim(hw));
4029 err_wrm_exit:
4030         csio_wrm_exit(csio_hw_to_wrm(hw), hw);
4031 err_mbm_exit:
4032         csio_mbm_exit(csio_hw_to_mbm(hw));
4033 err:
4034         return rv;
4035 }
4036
4037 /**
4038  * csio_hw_exit - Un-initialize HW module.
4039  * @hw:         Pointer to HW module.
4040  *
4041  */
4042 void
4043 csio_hw_exit(struct csio_hw *hw)
4044 {
4045         csio_evtq_cleanup(hw);
4046         csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
4047         csio_scsim_exit(csio_hw_to_scsim(hw));
4048         csio_wrm_exit(csio_hw_to_wrm(hw), hw);
4049         csio_mbm_exit(csio_hw_to_mbm(hw));
4050 }