Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
qlcnic_init.c
Go to the documentation of this file.
1 /*
2  * QLogic qlcnic NIC Driver
3  * Copyright (c) 2009-2010 QLogic Corporation
4  *
5  * See LICENSE.qlcnic for copyright and licensing details.
6  */
7 
8 #include <linux/netdevice.h>
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/if_vlan.h>
12 #include "qlcnic.h"
13 
14 struct crb_addr_pair {
15  u32 addr;
16  u32 data;
17 };
18 
19 #define QLCNIC_MAX_CRB_XFORM 60
20 static unsigned int crb_addr_xform[QLCNIC_MAX_CRB_XFORM];
21 
22 #define crb_addr_transform(name) \
23  (crb_addr_xform[QLCNIC_HW_PX_MAP_CRB_##name] = \
24  QLCNIC_HW_CRB_HUB_AGT_ADR_##name << 20)
25 
26 #define QLCNIC_ADDR_ERROR (0xffffffff)
27 
28 static void
29 qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
30  struct qlcnic_host_rds_ring *rds_ring);
31 
32 static int
33 qlcnic_check_fw_hearbeat(struct qlcnic_adapter *adapter);
34 
35 static void crb_addr_transform_setup(void)
36 {
37  crb_addr_transform(XDMA);
40  crb_addr_transform(SQN3);
41  crb_addr_transform(SQN2);
42  crb_addr_transform(SQN1);
43  crb_addr_transform(SQN0);
44  crb_addr_transform(SQS3);
45  crb_addr_transform(SQS2);
46  crb_addr_transform(SQS1);
47  crb_addr_transform(SQS0);
48  crb_addr_transform(RPMX7);
49  crb_addr_transform(RPMX6);
50  crb_addr_transform(RPMX5);
51  crb_addr_transform(RPMX4);
52  crb_addr_transform(RPMX3);
53  crb_addr_transform(RPMX2);
54  crb_addr_transform(RPMX1);
55  crb_addr_transform(RPMX0);
56  crb_addr_transform(ROMUSB);
58  crb_addr_transform(QMN);
59  crb_addr_transform(QMS);
60  crb_addr_transform(PGNI);
61  crb_addr_transform(PGND);
62  crb_addr_transform(PGN3);
63  crb_addr_transform(PGN2);
64  crb_addr_transform(PGN1);
65  crb_addr_transform(PGN0);
66  crb_addr_transform(PGSI);
67  crb_addr_transform(PGSD);
68  crb_addr_transform(PGS3);
69  crb_addr_transform(PGS2);
70  crb_addr_transform(PGS1);
71  crb_addr_transform(PGS0);
74  crb_addr_transform(NIU);
75  crb_addr_transform(I2Q);
79  crb_addr_transform(CAS2);
80  crb_addr_transform(CAS1);
81  crb_addr_transform(CAS0);
82  crb_addr_transform(CAM);
83  crb_addr_transform(C2C1);
84  crb_addr_transform(C2C0);
86  crb_addr_transform(OCM0);
88 }
89 
91 {
92  struct qlcnic_recv_context *recv_ctx;
93  struct qlcnic_host_rds_ring *rds_ring;
94  struct qlcnic_rx_buffer *rx_buf;
95  int i, ring;
96 
97  recv_ctx = adapter->recv_ctx;
98  for (ring = 0; ring < adapter->max_rds_rings; ring++) {
99  rds_ring = &recv_ctx->rds_rings[ring];
100  for (i = 0; i < rds_ring->num_desc; ++i) {
101  rx_buf = &(rds_ring->rx_buf_arr[i]);
102  if (rx_buf->skb == NULL)
103  continue;
104 
105  pci_unmap_single(adapter->pdev,
106  rx_buf->dma,
107  rds_ring->dma_size,
109 
110  dev_kfree_skb_any(rx_buf->skb);
111  }
112  }
113 }
114 
116 {
117  struct qlcnic_recv_context *recv_ctx;
118  struct qlcnic_host_rds_ring *rds_ring;
119  struct qlcnic_rx_buffer *rx_buf;
120  int i, ring;
121 
122  recv_ctx = adapter->recv_ctx;
123  for (ring = 0; ring < adapter->max_rds_rings; ring++) {
124  rds_ring = &recv_ctx->rds_rings[ring];
125 
126  INIT_LIST_HEAD(&rds_ring->free_list);
127 
128  rx_buf = rds_ring->rx_buf_arr;
129  for (i = 0; i < rds_ring->num_desc; i++) {
130  list_add_tail(&rx_buf->list,
131  &rds_ring->free_list);
132  rx_buf++;
133  }
134  }
135 }
136 
138 {
139  struct qlcnic_cmd_buffer *cmd_buf;
140  struct qlcnic_skb_frag *buffrag;
141  int i, j;
142  struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
143 
144  cmd_buf = tx_ring->cmd_buf_arr;
145  for (i = 0; i < tx_ring->num_desc; i++) {
146  buffrag = cmd_buf->frag_array;
147  if (buffrag->dma) {
148  pci_unmap_single(adapter->pdev, buffrag->dma,
149  buffrag->length, PCI_DMA_TODEVICE);
150  buffrag->dma = 0ULL;
151  }
152  for (j = 0; j < cmd_buf->frag_count; j++) {
153  buffrag++;
154  if (buffrag->dma) {
155  pci_unmap_page(adapter->pdev, buffrag->dma,
156  buffrag->length,
158  buffrag->dma = 0ULL;
159  }
160  }
161  if (cmd_buf->skb) {
162  dev_kfree_skb_any(cmd_buf->skb);
163  cmd_buf->skb = NULL;
164  }
165  cmd_buf++;
166  }
167 }
168 
170 {
171  struct qlcnic_recv_context *recv_ctx;
172  struct qlcnic_host_rds_ring *rds_ring;
174  int ring;
175 
176  recv_ctx = adapter->recv_ctx;
177 
178  if (recv_ctx->rds_rings == NULL)
179  goto skip_rds;
180 
181  for (ring = 0; ring < adapter->max_rds_rings; ring++) {
182  rds_ring = &recv_ctx->rds_rings[ring];
183  vfree(rds_ring->rx_buf_arr);
184  rds_ring->rx_buf_arr = NULL;
185  }
186  kfree(recv_ctx->rds_rings);
187 
188 skip_rds:
189  if (adapter->tx_ring == NULL)
190  return;
191 
192  tx_ring = adapter->tx_ring;
193  vfree(tx_ring->cmd_buf_arr);
194  tx_ring->cmd_buf_arr = NULL;
195  kfree(adapter->tx_ring);
196  adapter->tx_ring = NULL;
197 }
198 
200 {
201  struct qlcnic_recv_context *recv_ctx;
202  struct qlcnic_host_rds_ring *rds_ring;
203  struct qlcnic_host_sds_ring *sds_ring;
205  struct qlcnic_rx_buffer *rx_buf;
206  int ring, i, size;
207 
208  struct qlcnic_cmd_buffer *cmd_buf_arr;
209  struct net_device *netdev = adapter->netdev;
210 
211  size = sizeof(struct qlcnic_host_tx_ring);
212  tx_ring = kzalloc(size, GFP_KERNEL);
213  if (tx_ring == NULL) {
214  dev_err(&netdev->dev, "failed to allocate tx ring struct\n");
215  return -ENOMEM;
216  }
217  adapter->tx_ring = tx_ring;
218 
219  tx_ring->num_desc = adapter->num_txd;
220  tx_ring->txq = netdev_get_tx_queue(netdev, 0);
221 
222  cmd_buf_arr = vzalloc(TX_BUFF_RINGSIZE(tx_ring));
223  if (cmd_buf_arr == NULL) {
224  dev_err(&netdev->dev, "failed to allocate cmd buffer ring\n");
225  goto err_out;
226  }
227  tx_ring->cmd_buf_arr = cmd_buf_arr;
228 
229  recv_ctx = adapter->recv_ctx;
230 
231  size = adapter->max_rds_rings * sizeof(struct qlcnic_host_rds_ring);
232  rds_ring = kzalloc(size, GFP_KERNEL);
233  if (rds_ring == NULL) {
234  dev_err(&netdev->dev, "failed to allocate rds ring struct\n");
235  goto err_out;
236  }
237  recv_ctx->rds_rings = rds_ring;
238 
239  for (ring = 0; ring < adapter->max_rds_rings; ring++) {
240  rds_ring = &recv_ctx->rds_rings[ring];
241  switch (ring) {
242  case RCV_RING_NORMAL:
243  rds_ring->num_desc = adapter->num_rxd;
245  rds_ring->skb_size = rds_ring->dma_size + NET_IP_ALIGN;
246  break;
247 
248  case RCV_RING_JUMBO:
249  rds_ring->num_desc = adapter->num_jumbo_rxd;
250  rds_ring->dma_size =
252 
254  rds_ring->dma_size += QLCNIC_LRO_BUFFER_EXTRA;
255 
256  rds_ring->skb_size =
257  rds_ring->dma_size + NET_IP_ALIGN;
258  break;
259  }
260  rds_ring->rx_buf_arr = vzalloc(RCV_BUFF_RINGSIZE(rds_ring));
261  if (rds_ring->rx_buf_arr == NULL) {
262  dev_err(&netdev->dev, "Failed to allocate "
263  "rx buffer ring %d\n", ring);
264  goto err_out;
265  }
266  INIT_LIST_HEAD(&rds_ring->free_list);
267  /*
268  * Now go through all of them, set reference handles
269  * and put them in the queues.
270  */
271  rx_buf = rds_ring->rx_buf_arr;
272  for (i = 0; i < rds_ring->num_desc; i++) {
273  list_add_tail(&rx_buf->list,
274  &rds_ring->free_list);
275  rx_buf->ref_handle = i;
276  rx_buf++;
277  }
278  spin_lock_init(&rds_ring->lock);
279  }
280 
281  for (ring = 0; ring < adapter->max_sds_rings; ring++) {
282  sds_ring = &recv_ctx->sds_rings[ring];
283  sds_ring->irq = adapter->msix_entries[ring].vector;
284  sds_ring->adapter = adapter;
285  sds_ring->num_desc = adapter->num_rxd;
286 
287  for (i = 0; i < NUM_RCV_DESC_RINGS; i++)
288  INIT_LIST_HEAD(&sds_ring->free_list[i]);
289  }
290 
291  return 0;
292 
293 err_out:
294  qlcnic_free_sw_resources(adapter);
295  return -ENOMEM;
296 }
297 
298 /*
299  * Utility to translate from internal Phantom CRB address
300  * to external PCI CRB address.
301  */
302 static u32 qlcnic_decode_crb_addr(u32 addr)
303 {
304  int i;
305  u32 base_addr, offset, pci_base;
306 
307  crb_addr_transform_setup();
308 
309  pci_base = QLCNIC_ADDR_ERROR;
310  base_addr = addr & 0xfff00000;
311  offset = addr & 0x000fffff;
312 
313  for (i = 0; i < QLCNIC_MAX_CRB_XFORM; i++) {
314  if (crb_addr_xform[i] == base_addr) {
315  pci_base = i << 20;
316  break;
317  }
318  }
319  if (pci_base == QLCNIC_ADDR_ERROR)
320  return pci_base;
321  else
322  return pci_base + offset;
323 }
324 
325 #define QLCNIC_MAX_ROM_WAIT_USEC 100
326 
327 static int qlcnic_wait_rom_done(struct qlcnic_adapter *adapter)
328 {
329  long timeout = 0;
330  long done = 0;
331 
332  cond_resched();
333 
334  while (done == 0) {
335  done = QLCRD32(adapter, QLCNIC_ROMUSB_GLB_STATUS);
336  done &= 2;
337  if (++timeout >= QLCNIC_MAX_ROM_WAIT_USEC) {
338  dev_err(&adapter->pdev->dev,
339  "Timeout reached waiting for rom done");
340  return -EIO;
341  }
342  udelay(1);
343  }
344  return 0;
345 }
346 
347 static int do_rom_fast_read(struct qlcnic_adapter *adapter,
348  u32 addr, u32 *valp)
349 {
350  QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ADDRESS, addr);
354  if (qlcnic_wait_rom_done(adapter)) {
355  dev_err(&adapter->pdev->dev, "Error waiting for rom done\n");
356  return -EIO;
357  }
358  /* reset abyte_cnt and dummy_byte_cnt */
360  udelay(10);
362 
363  *valp = QLCRD32(adapter, QLCNIC_ROMUSB_ROM_RDATA);
364  return 0;
365 }
366 
367 static int do_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr,
368  u8 *bytes, size_t size)
369 {
370  int addridx;
371  int ret = 0;
372 
373  for (addridx = addr; addridx < (addr + size); addridx += 4) {
374  int v;
375  ret = do_rom_fast_read(adapter, addridx, &v);
376  if (ret != 0)
377  break;
378  *(__le32 *)bytes = cpu_to_le32(v);
379  bytes += 4;
380  }
381 
382  return ret;
383 }
384 
385 int
386 qlcnic_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr,
387  u8 *bytes, size_t size)
388 {
389  int ret;
390 
391  ret = qlcnic_rom_lock(adapter);
392  if (ret < 0)
393  return ret;
394 
395  ret = do_rom_fast_read_words(adapter, addr, bytes, size);
396 
397  qlcnic_rom_unlock(adapter);
398  return ret;
399 }
400 
401 int qlcnic_rom_fast_read(struct qlcnic_adapter *adapter, u32 addr, u32 *valp)
402 {
403  int ret;
404 
405  if (qlcnic_rom_lock(adapter) != 0)
406  return -EIO;
407 
408  ret = do_rom_fast_read(adapter, addr, valp);
409  qlcnic_rom_unlock(adapter);
410  return ret;
411 }
412 
414 {
415  int addr, val;
416  int i, n, init_delay;
417  struct crb_addr_pair *buf;
418  unsigned offset;
419  u32 off;
420  struct pci_dev *pdev = adapter->pdev;
421 
422  QLCWR32(adapter, CRB_CMDPEG_STATE, 0);
423  QLCWR32(adapter, CRB_RCVPEG_STATE, 0);
424 
425  /* Halt all the indiviual PEGs and other blocks */
426  /* disable all I2Q */
427  QLCWR32(adapter, QLCNIC_CRB_I2Q + 0x10, 0x0);
428  QLCWR32(adapter, QLCNIC_CRB_I2Q + 0x14, 0x0);
429  QLCWR32(adapter, QLCNIC_CRB_I2Q + 0x18, 0x0);
430  QLCWR32(adapter, QLCNIC_CRB_I2Q + 0x1c, 0x0);
431  QLCWR32(adapter, QLCNIC_CRB_I2Q + 0x20, 0x0);
432  QLCWR32(adapter, QLCNIC_CRB_I2Q + 0x24, 0x0);
433 
434  /* disable all niu interrupts */
435  QLCWR32(adapter, QLCNIC_CRB_NIU + 0x40, 0xff);
436  /* disable xge rx/tx */
437  QLCWR32(adapter, QLCNIC_CRB_NIU + 0x70000, 0x00);
438  /* disable xg1 rx/tx */
439  QLCWR32(adapter, QLCNIC_CRB_NIU + 0x80000, 0x00);
440  /* disable sideband mac */
441  QLCWR32(adapter, QLCNIC_CRB_NIU + 0x90000, 0x00);
442  /* disable ap0 mac */
443  QLCWR32(adapter, QLCNIC_CRB_NIU + 0xa0000, 0x00);
444  /* disable ap1 mac */
445  QLCWR32(adapter, QLCNIC_CRB_NIU + 0xb0000, 0x00);
446 
447  /* halt sre */
448  val = QLCRD32(adapter, QLCNIC_CRB_SRE + 0x1000);
449  QLCWR32(adapter, QLCNIC_CRB_SRE + 0x1000, val & (~(0x1)));
450 
451  /* halt epg */
452  QLCWR32(adapter, QLCNIC_CRB_EPG + 0x1300, 0x1);
453 
454  /* halt timers */
455  QLCWR32(adapter, QLCNIC_CRB_TIMER + 0x0, 0x0);
456  QLCWR32(adapter, QLCNIC_CRB_TIMER + 0x8, 0x0);
457  QLCWR32(adapter, QLCNIC_CRB_TIMER + 0x10, 0x0);
458  QLCWR32(adapter, QLCNIC_CRB_TIMER + 0x18, 0x0);
459  QLCWR32(adapter, QLCNIC_CRB_TIMER + 0x100, 0x0);
460  QLCWR32(adapter, QLCNIC_CRB_TIMER + 0x200, 0x0);
461  /* halt pegs */
462  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x3c, 1);
463  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x3c, 1);
464  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x3c, 1);
465  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x3c, 1);
466  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x3c, 1);
467  msleep(20);
468 
469  qlcnic_rom_unlock(adapter);
470  /* big hammer don't reset CAM block on reset */
471  QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0xfeffffff);
472 
473  /* Init HW CRB block */
474  if (qlcnic_rom_fast_read(adapter, 0, &n) != 0 || (n != 0xcafecafe) ||
475  qlcnic_rom_fast_read(adapter, 4, &n) != 0) {
476  dev_err(&pdev->dev, "ERROR Reading crb_init area: val:%x\n", n);
477  return -EIO;
478  }
479  offset = n & 0xffffU;
480  n = (n >> 16) & 0xffffU;
481 
482  if (n >= 1024) {
483  dev_err(&pdev->dev, "QLOGIC card flash not initialized.\n");
484  return -EIO;
485  }
486 
487  buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
488  if (buf == NULL) {
489  dev_err(&pdev->dev, "Unable to calloc memory for rom read.\n");
490  return -ENOMEM;
491  }
492 
493  for (i = 0; i < n; i++) {
494  if (qlcnic_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
495  qlcnic_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) {
496  kfree(buf);
497  return -EIO;
498  }
499 
500  buf[i].addr = addr;
501  buf[i].data = val;
502  }
503 
504  for (i = 0; i < n; i++) {
505 
506  off = qlcnic_decode_crb_addr(buf[i].addr);
507  if (off == QLCNIC_ADDR_ERROR) {
508  dev_err(&pdev->dev, "CRB init value out of range %x\n",
509  buf[i].addr);
510  continue;
511  }
512  off += QLCNIC_PCI_CRBSPACE;
513 
514  if (off & 1)
515  continue;
516 
517  /* skipping cold reboot MAGIC */
518  if (off == QLCNIC_CAM_RAM(0x1fc))
519  continue;
520  if (off == (QLCNIC_CRB_I2C0 + 0x1c))
521  continue;
522  if (off == (ROMUSB_GLB + 0xbc)) /* do not reset PCI */
523  continue;
524  if (off == (ROMUSB_GLB + 0xa8))
525  continue;
526  if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
527  continue;
528  if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
529  continue;
530  if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
531  continue;
532  if ((off & 0x0ff00000) == QLCNIC_CRB_DDR_NET)
533  continue;
534  /* skip the function enable register */
536  continue;
538  continue;
539  if ((off & 0x0ff00000) == QLCNIC_CRB_SMB)
540  continue;
541 
542  init_delay = 1;
543  /* After writing this register, HW needs time for CRB */
544  /* to quiet down (else crb_window returns 0xffffffff) */
545  if (off == QLCNIC_ROMUSB_GLB_SW_RESET)
546  init_delay = 1000;
547 
548  QLCWR32(adapter, off, buf[i].data);
549 
550  msleep(init_delay);
551  }
552  kfree(buf);
553 
554  /* Initialize protocol process engine */
555  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0xec, 0x1e);
556  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0x4c, 8);
557  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_I + 0x4c, 8);
558  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x8, 0);
559  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0xc, 0);
560  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x8, 0);
561  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0xc, 0);
562  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x8, 0);
563  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0xc, 0);
564  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x8, 0);
565  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0xc, 0);
566  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x8, 0);
567  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_4 + 0xc, 0);
568  msleep(1);
569 
570  QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS1, 0);
571  QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS2, 0);
572 
573  return 0;
574 }
575 
576 static int qlcnic_cmd_peg_ready(struct qlcnic_adapter *adapter)
577 {
578  u32 val;
580 
581  do {
582  val = QLCRD32(adapter, CRB_CMDPEG_STATE);
583 
584  switch (val) {
586  case PHAN_INITIALIZE_ACK:
587  return 0;
589  goto out_err;
590  default:
591  break;
592  }
593 
595 
596  } while (--retries);
597 
599 
600 out_err:
601  dev_err(&adapter->pdev->dev, "Command Peg initialization not "
602  "complete, state: 0x%x.\n", val);
603  return -EIO;
604 }
605 
606 static int
607 qlcnic_receive_peg_ready(struct qlcnic_adapter *adapter)
608 {
609  u32 val;
610  int retries = QLCNIC_RCVPEG_CHECK_RETRY_COUNT;
611 
612  do {
613  val = QLCRD32(adapter, CRB_RCVPEG_STATE);
614 
615  if (val == PHAN_PEG_RCV_INITIALIZED)
616  return 0;
617 
619 
620  } while (--retries);
621 
622  if (!retries) {
623  dev_err(&adapter->pdev->dev, "Receive Peg initialization not "
624  "complete, state: 0x%x.\n", val);
625  return -EIO;
626  }
627 
628  return 0;
629 }
630 
631 int
633 {
634  int err;
635 
636  err = qlcnic_cmd_peg_ready(adapter);
637  if (err)
638  return err;
639 
640  err = qlcnic_receive_peg_ready(adapter);
641  if (err)
642  return err;
643 
645 
646  return err;
647 }
648 
649 int
651 
652  int timeo;
653  u32 val;
654 
655  val = QLCRD32(adapter, QLCNIC_CRB_DEV_PARTITION_INFO);
656  val = QLC_DEV_GET_DRV(val, adapter->portnum);
657  if ((val & 0x3) != QLCNIC_TYPE_NIC) {
658  dev_err(&adapter->pdev->dev,
659  "Not an Ethernet NIC func=%u\n", val);
660  return -EIO;
661  }
662  adapter->physical_port = (val >> 2);
664  timeo = QLCNIC_INIT_TIMEOUT_SECS;
665 
666  adapter->dev_init_timeo = timeo;
667 
670 
671  adapter->reset_ack_timeo = timeo;
672 
673  return 0;
674 }
675 
676 static int qlcnic_get_flt_entry(struct qlcnic_adapter *adapter, u8 region,
677  struct qlcnic_flt_entry *region_entry)
678 {
679  struct qlcnic_flt_header flt_hdr;
680  struct qlcnic_flt_entry *flt_entry;
681  int i = 0, ret;
682  u32 entry_size;
683 
684  memset(region_entry, 0, sizeof(struct qlcnic_flt_entry));
686  (u8 *)&flt_hdr,
687  sizeof(struct qlcnic_flt_header));
688  if (ret) {
689  dev_warn(&adapter->pdev->dev,
690  "error reading flash layout header\n");
691  return -EIO;
692  }
693 
694  entry_size = flt_hdr.len - sizeof(struct qlcnic_flt_header);
695  flt_entry = (struct qlcnic_flt_entry *)vzalloc(entry_size);
696  if (flt_entry == NULL) {
697  dev_warn(&adapter->pdev->dev, "error allocating memory\n");
698  return -EIO;
699  }
700 
702  sizeof(struct qlcnic_flt_header),
703  (u8 *)flt_entry, entry_size);
704  if (ret) {
705  dev_warn(&adapter->pdev->dev,
706  "error reading flash layout entries\n");
707  goto err_out;
708  }
709 
710  while (i < (entry_size/sizeof(struct qlcnic_flt_entry))) {
711  if (flt_entry[i].region == region)
712  break;
713  i++;
714  }
715  if (i >= (entry_size/sizeof(struct qlcnic_flt_entry))) {
716  dev_warn(&adapter->pdev->dev,
717  "region=%x not found in %d regions\n", region, i);
718  ret = -EIO;
719  goto err_out;
720  }
721  memcpy(region_entry, &flt_entry[i], sizeof(struct qlcnic_flt_entry));
722 
723 err_out:
724  vfree(flt_entry);
725  return ret;
726 }
727 
728 int
730 {
731  struct qlcnic_flt_entry fw_entry;
732  u32 ver = -1, min_ver;
733  int ret;
734 
735  if (adapter->ahw->revision_id == QLCNIC_P3P_C0)
736  ret = qlcnic_get_flt_entry(adapter, QLCNIC_C0_FW_IMAGE_REGION,
737  &fw_entry);
738  else
739  ret = qlcnic_get_flt_entry(adapter, QLCNIC_B0_FW_IMAGE_REGION,
740  &fw_entry);
741 
742  if (!ret)
743  /* 0-4:-signature, 4-8:-fw version */
744  qlcnic_rom_fast_read(adapter, fw_entry.start_addr + 4,
745  (int *)&ver);
746  else
748  (int *)&ver);
749 
750  ver = QLCNIC_DECODE_VERSION(ver);
751  min_ver = QLCNIC_MIN_FW_VERSION;
752 
753  if (ver < min_ver) {
754  dev_err(&adapter->pdev->dev,
755  "firmware version %d.%d.%d unsupported."
756  "Min supported version %d.%d.%d\n",
757  _major(ver), _minor(ver), _build(ver),
758  _major(min_ver), _minor(min_ver), _build(min_ver));
759  return -EINVAL;
760  }
761 
762  return 0;
763 }
764 
765 static int
766 qlcnic_has_mn(struct qlcnic_adapter *adapter)
767 {
768  u32 capability;
769  capability = 0;
770 
771  capability = QLCRD32(adapter, QLCNIC_PEG_TUNE_CAPABILITY);
772  if (capability & QLCNIC_PEG_TUNE_MN_PRESENT)
773  return 1;
774 
775  return 0;
776 }
777 
778 static
779 struct uni_table_desc *qlcnic_get_table_desc(const u8 *unirom, int section)
780 {
781  u32 i;
782  struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
783  __le32 entries = cpu_to_le32(directory->num_entries);
784 
785  for (i = 0; i < entries; i++) {
786 
787  __le32 offs = cpu_to_le32(directory->findex) +
788  (i * cpu_to_le32(directory->entry_size));
789  __le32 tab_type = cpu_to_le32(*((u32 *)&unirom[offs] + 8));
790 
791  if (tab_type == section)
792  return (struct uni_table_desc *) &unirom[offs];
793  }
794 
795  return NULL;
796 }
797 
798 #define FILEHEADER_SIZE (14 * 4)
799 
800 static int
801 qlcnic_validate_header(struct qlcnic_adapter *adapter)
802 {
803  const u8 *unirom = adapter->fw->data;
804  struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0];
805  __le32 fw_file_size = adapter->fw->size;
806  __le32 entries;
808  __le32 tab_size;
809 
810  if (fw_file_size < FILEHEADER_SIZE)
811  return -EINVAL;
812 
813  entries = cpu_to_le32(directory->num_entries);
814  entry_size = cpu_to_le32(directory->entry_size);
815  tab_size = cpu_to_le32(directory->findex) + (entries * entry_size);
816 
817  if (fw_file_size < tab_size)
818  return -EINVAL;
819 
820  return 0;
821 }
822 
823 static int
824 qlcnic_validate_bootld(struct qlcnic_adapter *adapter)
825 {
826  struct uni_table_desc *tab_desc;
827  struct uni_data_desc *descr;
828  const u8 *unirom = adapter->fw->data;
829  int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
831  __le32 offs;
832  __le32 tab_size;
834 
835  tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_BOOTLD);
836 
837  if (!tab_desc)
838  return -EINVAL;
839 
840  tab_size = cpu_to_le32(tab_desc->findex) +
841  (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
842 
843  if (adapter->fw->size < tab_size)
844  return -EINVAL;
845 
846  offs = cpu_to_le32(tab_desc->findex) +
847  (cpu_to_le32(tab_desc->entry_size) * (idx));
848  descr = (struct uni_data_desc *)&unirom[offs];
849 
850  data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
851 
852  if (adapter->fw->size < data_size)
853  return -EINVAL;
854 
855  return 0;
856 }
857 
858 static int
859 qlcnic_validate_fw(struct qlcnic_adapter *adapter)
860 {
861  struct uni_table_desc *tab_desc;
862  struct uni_data_desc *descr;
863  const u8 *unirom = adapter->fw->data;
864  int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
866  __le32 offs;
867  __le32 tab_size;
869 
870  tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_FW);
871 
872  if (!tab_desc)
873  return -EINVAL;
874 
875  tab_size = cpu_to_le32(tab_desc->findex) +
876  (cpu_to_le32(tab_desc->entry_size) * (idx + 1));
877 
878  if (adapter->fw->size < tab_size)
879  return -EINVAL;
880 
881  offs = cpu_to_le32(tab_desc->findex) +
882  (cpu_to_le32(tab_desc->entry_size) * (idx));
883  descr = (struct uni_data_desc *)&unirom[offs];
884  data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size);
885 
886  if (adapter->fw->size < data_size)
887  return -EINVAL;
888 
889  return 0;
890 }
891 
892 static int
893 qlcnic_validate_product_offs(struct qlcnic_adapter *adapter)
894 {
895  struct uni_table_desc *ptab_descr;
896  const u8 *unirom = adapter->fw->data;
897  int mn_present = qlcnic_has_mn(adapter);
898  __le32 entries;
900  __le32 tab_size;
901  u32 i;
902 
903  ptab_descr = qlcnic_get_table_desc(unirom,
905  if (!ptab_descr)
906  return -EINVAL;
907 
908  entries = cpu_to_le32(ptab_descr->num_entries);
909  entry_size = cpu_to_le32(ptab_descr->entry_size);
910  tab_size = cpu_to_le32(ptab_descr->findex) + (entries * entry_size);
911 
912  if (adapter->fw->size < tab_size)
913  return -EINVAL;
914 
915 nomn:
916  for (i = 0; i < entries; i++) {
917 
918  __le32 flags, file_chiprev, offs;
919  u8 chiprev = adapter->ahw->revision_id;
920  u32 flagbit;
921 
922  offs = cpu_to_le32(ptab_descr->findex) +
923  (i * cpu_to_le32(ptab_descr->entry_size));
924  flags = cpu_to_le32(*((int *)&unirom[offs] +
926  file_chiprev = cpu_to_le32(*((int *)&unirom[offs] +
928 
929  flagbit = mn_present ? 1 : 2;
930 
931  if ((chiprev == file_chiprev) &&
932  ((1ULL << flagbit) & flags)) {
933  adapter->file_prd_off = offs;
934  return 0;
935  }
936  }
937  if (mn_present) {
938  mn_present = 0;
939  goto nomn;
940  }
941  return -EINVAL;
942 }
943 
944 static int
945 qlcnic_validate_unified_romimage(struct qlcnic_adapter *adapter)
946 {
947  if (qlcnic_validate_header(adapter)) {
948  dev_err(&adapter->pdev->dev,
949  "unified image: header validation failed\n");
950  return -EINVAL;
951  }
952 
953  if (qlcnic_validate_product_offs(adapter)) {
954  dev_err(&adapter->pdev->dev,
955  "unified image: product validation failed\n");
956  return -EINVAL;
957  }
958 
959  if (qlcnic_validate_bootld(adapter)) {
960  dev_err(&adapter->pdev->dev,
961  "unified image: bootld validation failed\n");
962  return -EINVAL;
963  }
964 
965  if (qlcnic_validate_fw(adapter)) {
966  dev_err(&adapter->pdev->dev,
967  "unified image: firmware validation failed\n");
968  return -EINVAL;
969  }
970 
971  return 0;
972 }
973 
974 static
975 struct uni_data_desc *qlcnic_get_data_desc(struct qlcnic_adapter *adapter,
976  u32 section, u32 idx_offset)
977 {
978  const u8 *unirom = adapter->fw->data;
979  int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] +
980  idx_offset));
981  struct uni_table_desc *tab_desc;
982  __le32 offs;
983 
984  tab_desc = qlcnic_get_table_desc(unirom, section);
985 
986  if (tab_desc == NULL)
987  return NULL;
988 
989  offs = cpu_to_le32(tab_desc->findex) +
990  (cpu_to_le32(tab_desc->entry_size) * idx);
991 
992  return (struct uni_data_desc *)&unirom[offs];
993 }
994 
995 static u8 *
996 qlcnic_get_bootld_offs(struct qlcnic_adapter *adapter)
997 {
998  u32 offs = QLCNIC_BOOTLD_START;
999 
1000  if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
1001  offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
1004 
1005  return (u8 *)&adapter->fw->data[offs];
1006 }
1007 
1008 static u8 *
1009 qlcnic_get_fw_offs(struct qlcnic_adapter *adapter)
1010 {
1011  u32 offs = QLCNIC_IMAGE_START;
1012 
1013  if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
1014  offs = cpu_to_le32((qlcnic_get_data_desc(adapter,
1017 
1018  return (u8 *)&adapter->fw->data[offs];
1019 }
1020 
1021 static __le32
1022 qlcnic_get_fw_size(struct qlcnic_adapter *adapter)
1023 {
1024  if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE)
1025  return cpu_to_le32((qlcnic_get_data_desc(adapter,
1027  QLCNIC_UNI_FIRMWARE_IDX_OFF))->size);
1028  else
1029  return cpu_to_le32(
1030  *(u32 *)&adapter->fw->data[QLCNIC_FW_SIZE_OFFSET]);
1031 }
1032 
1033 static __le32
1034 qlcnic_get_fw_version(struct qlcnic_adapter *adapter)
1035 {
1036  struct uni_data_desc *fw_data_desc;
1037  const struct firmware *fw = adapter->fw;
1038  __le32 major, minor, sub;
1039  const u8 *ver_str;
1040  int i, ret;
1041 
1042  if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
1043  return cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_VERSION_OFFSET]);
1044 
1045  fw_data_desc = qlcnic_get_data_desc(adapter, QLCNIC_UNI_DIR_SECT_FW,
1047  ver_str = fw->data + cpu_to_le32(fw_data_desc->findex) +
1048  cpu_to_le32(fw_data_desc->size) - 17;
1049 
1050  for (i = 0; i < 12; i++) {
1051  if (!strncmp(&ver_str[i], "REV=", 4)) {
1052  ret = sscanf(&ver_str[i+4], "%u.%u.%u ",
1053  &major, &minor, &sub);
1054  if (ret != 3)
1055  return 0;
1056  else
1057  return major + (minor << 8) + (sub << 16);
1058  }
1059  }
1060 
1061  return 0;
1062 }
1063 
1064 static __le32
1065 qlcnic_get_bios_version(struct qlcnic_adapter *adapter)
1066 {
1067  const struct firmware *fw = adapter->fw;
1068  __le32 bios_ver, prd_off = adapter->file_prd_off;
1069 
1070  if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE)
1071  return cpu_to_le32(
1072  *(u32 *)&fw->data[QLCNIC_BIOS_VERSION_OFFSET]);
1073 
1074  bios_ver = cpu_to_le32(*((u32 *) (&fw->data[prd_off])
1076 
1077  return (bios_ver << 16) + ((bios_ver >> 8) & 0xff00) + (bios_ver >> 24);
1078 }
1079 
1080 static void qlcnic_rom_lock_recovery(struct qlcnic_adapter *adapter)
1081 {
1082  if (qlcnic_pcie_sem_lock(adapter, 2, QLCNIC_ROM_LOCK_ID))
1083  dev_info(&adapter->pdev->dev, "Resetting rom_lock\n");
1084 
1085  qlcnic_pcie_sem_unlock(adapter, 2);
1086 }
1087 
1088 static int
1089 qlcnic_check_fw_hearbeat(struct qlcnic_adapter *adapter)
1090 {
1091  u32 heartbeat, ret = -EIO;
1092  int retries = QLCNIC_HEARTBEAT_CHECK_RETRY_COUNT;
1093 
1094  adapter->heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
1095 
1096  do {
1098  heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
1099  if (heartbeat != adapter->heartbeat) {
1100  ret = QLCNIC_RCODE_SUCCESS;
1101  break;
1102  }
1103  } while (--retries);
1104 
1105  return ret;
1106 }
1107 
1108 int
1110 {
1111  if ((adapter->flags & QLCNIC_FW_HANG) ||
1112  qlcnic_check_fw_hearbeat(adapter)) {
1113  qlcnic_rom_lock_recovery(adapter);
1114  return 1;
1115  }
1116 
1117  if (adapter->need_fw_reset)
1118  return 1;
1119 
1120  if (adapter->fw)
1121  return 1;
1122 
1123  return 0;
1124 }
1125 
1126 static const char *fw_name[] = {
1129 };
1130 
1131 int
1133 {
1134  u64 *ptr64;
1135  u32 i, flashaddr, size;
1136  const struct firmware *fw = adapter->fw;
1137  struct pci_dev *pdev = adapter->pdev;
1138 
1139  dev_info(&pdev->dev, "loading firmware from %s\n",
1140  fw_name[adapter->fw_type]);
1141 
1142  if (fw) {
1143  __le64 data;
1144 
1145  size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
1146 
1147  ptr64 = (u64 *)qlcnic_get_bootld_offs(adapter);
1148  flashaddr = QLCNIC_BOOTLD_START;
1149 
1150  for (i = 0; i < size; i++) {
1151  data = cpu_to_le64(ptr64[i]);
1152 
1153  if (qlcnic_pci_mem_write_2M(adapter, flashaddr, data))
1154  return -EIO;
1155 
1156  flashaddr += 8;
1157  }
1158 
1159  size = (__force u32)qlcnic_get_fw_size(adapter) / 8;
1160 
1161  ptr64 = (u64 *)qlcnic_get_fw_offs(adapter);
1162  flashaddr = QLCNIC_IMAGE_START;
1163 
1164  for (i = 0; i < size; i++) {
1165  data = cpu_to_le64(ptr64[i]);
1166 
1167  if (qlcnic_pci_mem_write_2M(adapter,
1168  flashaddr, data))
1169  return -EIO;
1170 
1171  flashaddr += 8;
1172  }
1173 
1174  size = (__force u32)qlcnic_get_fw_size(adapter) % 8;
1175  if (size) {
1176  data = cpu_to_le64(ptr64[i]);
1177 
1178  if (qlcnic_pci_mem_write_2M(adapter,
1179  flashaddr, data))
1180  return -EIO;
1181  }
1182 
1183  } else {
1184  u64 data;
1185  u32 hi, lo;
1186  int ret;
1187  struct qlcnic_flt_entry bootld_entry;
1188 
1189  ret = qlcnic_get_flt_entry(adapter, QLCNIC_BOOTLD_REGION,
1190  &bootld_entry);
1191  if (!ret) {
1192  size = bootld_entry.size / 8;
1193  flashaddr = bootld_entry.start_addr;
1194  } else {
1195  size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8;
1196  flashaddr = QLCNIC_BOOTLD_START;
1197  dev_info(&pdev->dev,
1198  "using legacy method to get flash fw region");
1199  }
1200 
1201  for (i = 0; i < size; i++) {
1202  if (qlcnic_rom_fast_read(adapter,
1203  flashaddr, (int *)&lo) != 0)
1204  return -EIO;
1205  if (qlcnic_rom_fast_read(adapter,
1206  flashaddr + 4, (int *)&hi) != 0)
1207  return -EIO;
1208 
1209  data = (((u64)hi << 32) | lo);
1210 
1211  if (qlcnic_pci_mem_write_2M(adapter,
1212  flashaddr, data))
1213  return -EIO;
1214 
1215  flashaddr += 8;
1216  }
1217  }
1218  msleep(1);
1219 
1220  QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x18, 0x1020);
1221  QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0x80001e);
1222  return 0;
1223 }
1224 
1225 static int
1226 qlcnic_validate_firmware(struct qlcnic_adapter *adapter)
1227 {
1228  __le32 val;
1229  u32 ver, bios, min_size;
1230  struct pci_dev *pdev = adapter->pdev;
1231  const struct firmware *fw = adapter->fw;
1232  u8 fw_type = adapter->fw_type;
1233 
1234  if (fw_type == QLCNIC_UNIFIED_ROMIMAGE) {
1235  if (qlcnic_validate_unified_romimage(adapter))
1236  return -EINVAL;
1237 
1238  min_size = QLCNIC_UNI_FW_MIN_SIZE;
1239  } else {
1240  val = cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_MAGIC_OFFSET]);
1241  if ((__force u32)val != QLCNIC_BDINFO_MAGIC)
1242  return -EINVAL;
1243 
1244  min_size = QLCNIC_FW_MIN_SIZE;
1245  }
1246 
1247  if (fw->size < min_size)
1248  return -EINVAL;
1249 
1250  val = qlcnic_get_fw_version(adapter);
1251  ver = QLCNIC_DECODE_VERSION(val);
1252 
1253  if (ver < QLCNIC_MIN_FW_VERSION) {
1254  dev_err(&pdev->dev,
1255  "%s: firmware version %d.%d.%d unsupported\n",
1256  fw_name[fw_type], _major(ver), _minor(ver), _build(ver));
1257  return -EINVAL;
1258  }
1259 
1260  val = qlcnic_get_bios_version(adapter);
1261  qlcnic_rom_fast_read(adapter, QLCNIC_BIOS_VERSION_OFFSET, (int *)&bios);
1262  if ((__force u32)val != bios) {
1263  dev_err(&pdev->dev, "%s: firmware bios is incompatible\n",
1264  fw_name[fw_type]);
1265  return -EINVAL;
1266  }
1267 
1268  QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
1269  return 0;
1270 }
1271 
1272 static void
1273 qlcnic_get_next_fwtype(struct qlcnic_adapter *adapter)
1274 {
1275  u8 fw_type;
1276 
1277  switch (adapter->fw_type) {
1279  fw_type = QLCNIC_UNIFIED_ROMIMAGE;
1280  break;
1281 
1283  default:
1284  fw_type = QLCNIC_FLASH_ROMIMAGE;
1285  break;
1286  }
1287 
1288  adapter->fw_type = fw_type;
1289 }
1290 
1291 
1292 
1294 {
1295  struct pci_dev *pdev = adapter->pdev;
1296  int rc;
1297 
1298  adapter->fw_type = QLCNIC_UNKNOWN_ROMIMAGE;
1299 
1300 next:
1301  qlcnic_get_next_fwtype(adapter);
1302 
1303  if (adapter->fw_type == QLCNIC_FLASH_ROMIMAGE) {
1304  adapter->fw = NULL;
1305  } else {
1306  rc = request_firmware(&adapter->fw,
1307  fw_name[adapter->fw_type], &pdev->dev);
1308  if (rc != 0)
1309  goto next;
1310 
1311  rc = qlcnic_validate_firmware(adapter);
1312  if (rc != 0) {
1313  release_firmware(adapter->fw);
1314  msleep(1);
1315  goto next;
1316  }
1317  }
1318 }
1319 
1320 
1321 void
1323 {
1324  release_firmware(adapter->fw);
1325  adapter->fw = NULL;
1326 }
1327 
1328 static void
1329 qlcnic_handle_linkevent(struct qlcnic_adapter *adapter,
1330  struct qlcnic_fw_msg *msg)
1331 {
1332  u32 cable_OUI;
1333  u16 cable_len;
1334  u16 link_speed;
1336  u8 lb_status = 0;
1337  struct net_device *netdev = adapter->netdev;
1338 
1339  adapter->has_link_events = 1;
1340 
1341  cable_OUI = msg->body[1] & 0xffffffff;
1342  cable_len = (msg->body[1] >> 32) & 0xffff;
1343  link_speed = (msg->body[1] >> 48) & 0xffff;
1344 
1345  link_status = msg->body[2] & 0xff;
1346  duplex = (msg->body[2] >> 16) & 0xff;
1347  autoneg = (msg->body[2] >> 24) & 0xff;
1348  lb_status = (msg->body[2] >> 32) & 0x3;
1349 
1350  module = (msg->body[2] >> 8) & 0xff;
1352  dev_info(&netdev->dev, "unsupported cable: OUI 0x%x, "
1353  "length %d\n", cable_OUI, cable_len);
1355  dev_info(&netdev->dev, "unsupported cable length %d\n",
1356  cable_len);
1357 
1358  if (!link_status && (lb_status == QLCNIC_ILB_MODE ||
1359  lb_status == QLCNIC_ELB_MODE))
1360  adapter->ahw->loopback_state |= QLCNIC_LINKEVENT;
1361 
1362  qlcnic_advert_link_change(adapter, link_status);
1363 
1364  if (duplex == LINKEVENT_FULL_DUPLEX)
1365  adapter->link_duplex = DUPLEX_FULL;
1366  else
1367  adapter->link_duplex = DUPLEX_HALF;
1368 
1369  adapter->module_type = module;
1370  adapter->link_autoneg = autoneg;
1371 
1372  if (link_status) {
1373  adapter->link_speed = link_speed;
1374  } else {
1375  adapter->link_speed = SPEED_UNKNOWN;
1376  adapter->link_duplex = DUPLEX_UNKNOWN;
1377  }
1378 }
1379 
1380 static void
1381 qlcnic_handle_fw_message(int desc_cnt, int index,
1382  struct qlcnic_host_sds_ring *sds_ring)
1383 {
1384  struct qlcnic_fw_msg msg;
1385  struct status_desc *desc;
1386  struct qlcnic_adapter *adapter;
1387  struct device *dev;
1388  int i = 0, opcode, ret;
1389 
1390  while (desc_cnt > 0 && i < 8) {
1391  desc = &sds_ring->desc_head[index];
1392  msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
1393  msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
1394 
1395  index = get_next_index(index, sds_ring->num_desc);
1396  desc_cnt--;
1397  }
1398 
1399  adapter = sds_ring->adapter;
1400  dev = &adapter->pdev->dev;
1402 
1403  switch (opcode) {
1405  qlcnic_handle_linkevent(adapter, &msg);
1406  break;
1408  ret = (u32)(msg.body[1]);
1409  switch (ret) {
1410  case 0:
1411  adapter->ahw->loopback_state |= QLCNIC_LB_RESPONSE;
1412  break;
1413  case 1:
1414  dev_info(dev, "loopback already in progress\n");
1415  adapter->diag_cnt = -QLCNIC_TEST_IN_PROGRESS;
1416  break;
1417  case 2:
1418  dev_info(dev, "loopback cable is not connected\n");
1419  adapter->diag_cnt = -QLCNIC_LB_CABLE_NOT_CONN;
1420  break;
1421  default:
1422  dev_info(dev, "loopback configure request failed,"
1423  " ret %x\n", ret);
1424  adapter->diag_cnt = -QLCNIC_UNDEFINED_ERROR;
1425  break;
1426  }
1427  break;
1428  default:
1429  break;
1430  }
1431 }
1432 
1433 static int
1434 qlcnic_alloc_rx_skb(struct qlcnic_adapter *adapter,
1435  struct qlcnic_host_rds_ring *rds_ring,
1436  struct qlcnic_rx_buffer *buffer)
1437 {
1438  struct sk_buff *skb;
1439  dma_addr_t dma;
1440  struct pci_dev *pdev = adapter->pdev;
1441 
1442  skb = netdev_alloc_skb(adapter->netdev, rds_ring->skb_size);
1443  if (!skb) {
1444  adapter->stats.skb_alloc_failure++;
1445  return -ENOMEM;
1446  }
1447 
1448  skb_reserve(skb, NET_IP_ALIGN);
1449 
1450  dma = pci_map_single(pdev, skb->data,
1451  rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1452 
1453  if (pci_dma_mapping_error(pdev, dma)) {
1454  adapter->stats.rx_dma_map_error++;
1455  dev_kfree_skb_any(skb);
1456  return -ENOMEM;
1457  }
1458 
1459  buffer->skb = skb;
1460  buffer->dma = dma;
1461 
1462  return 0;
1463 }
1464 
1465 static struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *adapter,
1466  struct qlcnic_host_rds_ring *rds_ring, u16 index, u16 cksum)
1467 {
1468  struct qlcnic_rx_buffer *buffer;
1469  struct sk_buff *skb;
1470 
1471  buffer = &rds_ring->rx_buf_arr[index];
1472 
1473  if (unlikely(buffer->skb == NULL)) {
1474  WARN_ON(1);
1475  return NULL;
1476  }
1477 
1478  pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1480 
1481  skb = buffer->skb;
1482 
1483  if (likely((adapter->netdev->features & NETIF_F_RXCSUM) &&
1484  (cksum == STATUS_CKSUM_OK || cksum == STATUS_CKSUM_LOOP))) {
1485  adapter->stats.csummed++;
1487  } else {
1488  skb_checksum_none_assert(skb);
1489  }
1490 
1491  buffer->skb = NULL;
1492 
1493  return skb;
1494 }
1495 
1496 static inline int
1497 qlcnic_check_rx_tagging(struct qlcnic_adapter *adapter, struct sk_buff *skb,
1498  u16 *vlan_tag)
1499 {
1500  struct ethhdr *eth_hdr;
1501 
1502  if (!__vlan_get_tag(skb, vlan_tag)) {
1503  eth_hdr = (struct ethhdr *) skb->data;
1504  memmove(skb->data + VLAN_HLEN, eth_hdr, ETH_ALEN * 2);
1505  skb_pull(skb, VLAN_HLEN);
1506  }
1507  if (!adapter->pvid)
1508  return 0;
1509 
1510  if (*vlan_tag == adapter->pvid) {
1511  /* Outer vlan tag. Packet should follow non-vlan path */
1512  *vlan_tag = 0xffff;
1513  return 0;
1514  }
1515  if (adapter->flags & QLCNIC_TAGGING_ENABLED)
1516  return 0;
1517 
1518  return -EINVAL;
1519 }
1520 
1521 static struct qlcnic_rx_buffer *
1522 qlcnic_process_rcv(struct qlcnic_adapter *adapter,
1523  struct qlcnic_host_sds_ring *sds_ring,
1524  int ring, u64 sts_data0)
1525 {
1526  struct net_device *netdev = adapter->netdev;
1527  struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1528  struct qlcnic_rx_buffer *buffer;
1529  struct sk_buff *skb;
1530  struct qlcnic_host_rds_ring *rds_ring;
1531  int index, length, cksum, pkt_offset;
1532  u16 vid = 0xffff;
1533 
1534  if (unlikely(ring >= adapter->max_rds_rings))
1535  return NULL;
1536 
1537  rds_ring = &recv_ctx->rds_rings[ring];
1538 
1539  index = qlcnic_get_sts_refhandle(sts_data0);
1540  if (unlikely(index >= rds_ring->num_desc))
1541  return NULL;
1542 
1543  buffer = &rds_ring->rx_buf_arr[index];
1544 
1545  length = qlcnic_get_sts_totallength(sts_data0);
1546  cksum = qlcnic_get_sts_status(sts_data0);
1547  pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1548 
1549  skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1550  if (!skb)
1551  return buffer;
1552 
1553  if (length > rds_ring->skb_size)
1554  skb_put(skb, rds_ring->skb_size);
1555  else
1556  skb_put(skb, length);
1557 
1558  if (pkt_offset)
1559  skb_pull(skb, pkt_offset);
1560 
1561  if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1562  adapter->stats.rxdropped++;
1563  dev_kfree_skb(skb);
1564  return buffer;
1565  }
1566 
1567  skb->protocol = eth_type_trans(skb, netdev);
1568 
1569  if (vid != 0xffff)
1570  __vlan_hwaccel_put_tag(skb, vid);
1571 
1572  napi_gro_receive(&sds_ring->napi, skb);
1573 
1574  adapter->stats.rx_pkts++;
1575  adapter->stats.rxbytes += length;
1576 
1577  return buffer;
1578 }
1579 
1580 #define QLC_TCP_HDR_SIZE 20
1581 #define QLC_TCP_TS_OPTION_SIZE 12
1582 #define QLC_TCP_TS_HDR_SIZE (QLC_TCP_HDR_SIZE + QLC_TCP_TS_OPTION_SIZE)
1583 
1584 static struct qlcnic_rx_buffer *
1585 qlcnic_process_lro(struct qlcnic_adapter *adapter,
1586  struct qlcnic_host_sds_ring *sds_ring,
1587  int ring, u64 sts_data0, u64 sts_data1)
1588 {
1589  struct net_device *netdev = adapter->netdev;
1590  struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1591  struct qlcnic_rx_buffer *buffer;
1592  struct sk_buff *skb;
1593  struct qlcnic_host_rds_ring *rds_ring;
1594  struct iphdr *iph;
1595  struct tcphdr *th;
1596  bool push, timestamp;
1597  int l2_hdr_offset, l4_hdr_offset;
1598  int index;
1599  u16 lro_length, length, data_offset;
1600  u32 seq_number;
1601  u16 vid = 0xffff;
1602 
1603  if (unlikely(ring > adapter->max_rds_rings))
1604  return NULL;
1605 
1606  rds_ring = &recv_ctx->rds_rings[ring];
1607 
1608  index = qlcnic_get_lro_sts_refhandle(sts_data0);
1609  if (unlikely(index > rds_ring->num_desc))
1610  return NULL;
1611 
1612  buffer = &rds_ring->rx_buf_arr[index];
1613 
1614  timestamp = qlcnic_get_lro_sts_timestamp(sts_data0);
1615  lro_length = qlcnic_get_lro_sts_length(sts_data0);
1616  l2_hdr_offset = qlcnic_get_lro_sts_l2_hdr_offset(sts_data0);
1617  l4_hdr_offset = qlcnic_get_lro_sts_l4_hdr_offset(sts_data0);
1618  push = qlcnic_get_lro_sts_push_flag(sts_data0);
1619  seq_number = qlcnic_get_lro_sts_seq_number(sts_data1);
1620 
1621  skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1622  if (!skb)
1623  return buffer;
1624 
1625  if (timestamp)
1626  data_offset = l4_hdr_offset + QLC_TCP_TS_HDR_SIZE;
1627  else
1628  data_offset = l4_hdr_offset + QLC_TCP_HDR_SIZE;
1629 
1630  skb_put(skb, lro_length + data_offset);
1631 
1632  skb_pull(skb, l2_hdr_offset);
1633 
1634  if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1635  adapter->stats.rxdropped++;
1636  dev_kfree_skb(skb);
1637  return buffer;
1638  }
1639 
1640  skb->protocol = eth_type_trans(skb, netdev);
1641 
1642  iph = (struct iphdr *)skb->data;
1643  th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1644 
1645  length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1646  iph->tot_len = htons(length);
1647  iph->check = 0;
1648  iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1649  th->psh = push;
1650  th->seq = htonl(seq_number);
1651 
1652  length = skb->len;
1653 
1654  if (adapter->flags & QLCNIC_FW_LRO_MSS_CAP)
1655  skb_shinfo(skb)->gso_size = qlcnic_get_lro_sts_mss(sts_data1);
1656 
1657  if (vid != 0xffff)
1658  __vlan_hwaccel_put_tag(skb, vid);
1659  netif_receive_skb(skb);
1660 
1661  adapter->stats.lro_pkts++;
1662  adapter->stats.lrobytes += length;
1663 
1664  return buffer;
1665 }
1666 
1667 int
1669 {
1670  struct qlcnic_adapter *adapter = sds_ring->adapter;
1671  struct list_head *cur;
1672  struct status_desc *desc;
1673  struct qlcnic_rx_buffer *rxbuf;
1674  u64 sts_data0, sts_data1;
1675 
1676  int count = 0;
1677  int opcode, ring, desc_cnt;
1678  u32 consumer = sds_ring->consumer;
1679 
1680  while (count < max) {
1681  desc = &sds_ring->desc_head[consumer];
1682  sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1683 
1684  if (!(sts_data0 & STATUS_OWNER_HOST))
1685  break;
1686 
1687  desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1688  opcode = qlcnic_get_sts_opcode(sts_data0);
1689 
1690  switch (opcode) {
1691  case QLCNIC_RXPKT_DESC:
1692  case QLCNIC_OLD_RXPKT_DESC:
1693  case QLCNIC_SYN_OFFLOAD:
1694  ring = qlcnic_get_sts_type(sts_data0);
1695  rxbuf = qlcnic_process_rcv(adapter, sds_ring,
1696  ring, sts_data0);
1697  break;
1698  case QLCNIC_LRO_DESC:
1699  ring = qlcnic_get_lro_sts_type(sts_data0);
1700  sts_data1 = le64_to_cpu(desc->status_desc_data[1]);
1701  rxbuf = qlcnic_process_lro(adapter, sds_ring,
1702  ring, sts_data0, sts_data1);
1703  break;
1704  case QLCNIC_RESPONSE_DESC:
1705  qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1706  default:
1707  goto skip;
1708  }
1709 
1710  WARN_ON(desc_cnt > 1);
1711 
1712  if (likely(rxbuf))
1713  list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1714  else
1715  adapter->stats.null_rxbuf++;
1716 
1717 skip:
1718  for (; desc_cnt > 0; desc_cnt--) {
1719  desc = &sds_ring->desc_head[consumer];
1720  desc->status_desc_data[0] =
1722  consumer = get_next_index(consumer, sds_ring->num_desc);
1723  }
1724  count++;
1725  }
1726 
1727  for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1728  struct qlcnic_host_rds_ring *rds_ring =
1729  &adapter->recv_ctx->rds_rings[ring];
1730 
1731  if (!list_empty(&sds_ring->free_list[ring])) {
1732  list_for_each(cur, &sds_ring->free_list[ring]) {
1733  rxbuf = list_entry(cur,
1734  struct qlcnic_rx_buffer, list);
1735  qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1736  }
1737  spin_lock(&rds_ring->lock);
1738  list_splice_tail_init(&sds_ring->free_list[ring],
1739  &rds_ring->free_list);
1740  spin_unlock(&rds_ring->lock);
1741  }
1742 
1743  qlcnic_post_rx_buffers_nodb(adapter, rds_ring);
1744  }
1745 
1746  if (count) {
1747  sds_ring->consumer = consumer;
1748  writel(consumer, sds_ring->crb_sts_consumer);
1749  }
1750 
1751  return count;
1752 }
1753 
1754 void
1756  struct qlcnic_host_rds_ring *rds_ring)
1757 {
1758  struct rcv_desc *pdesc;
1759  struct qlcnic_rx_buffer *buffer;
1760  int count = 0;
1761  u32 producer;
1762  struct list_head *head;
1763 
1764  producer = rds_ring->producer;
1765 
1766  head = &rds_ring->free_list;
1767  while (!list_empty(head)) {
1768 
1769  buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1770 
1771  if (!buffer->skb) {
1772  if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1773  break;
1774  }
1775 
1776  count++;
1777  list_del(&buffer->list);
1778 
1779  /* make a rcv descriptor */
1780  pdesc = &rds_ring->desc_head[producer];
1781  pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1782  pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1783  pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1784 
1785  producer = get_next_index(producer, rds_ring->num_desc);
1786  }
1787 
1788  if (count) {
1789  rds_ring->producer = producer;
1790  writel((producer-1) & (rds_ring->num_desc-1),
1791  rds_ring->crb_rcv_producer);
1792  }
1793 }
1794 
1795 static void
1796 qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
1797  struct qlcnic_host_rds_ring *rds_ring)
1798 {
1799  struct rcv_desc *pdesc;
1800  struct qlcnic_rx_buffer *buffer;
1801  int count = 0;
1802  uint32_t producer;
1803  struct list_head *head;
1804 
1805  if (!spin_trylock(&rds_ring->lock))
1806  return;
1807 
1808  producer = rds_ring->producer;
1809 
1810  head = &rds_ring->free_list;
1811  while (!list_empty(head)) {
1812 
1813  buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1814 
1815  if (!buffer->skb) {
1816  if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1817  break;
1818  }
1819 
1820  count++;
1821  list_del(&buffer->list);
1822 
1823  /* make a rcv descriptor */
1824  pdesc = &rds_ring->desc_head[producer];
1825  pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1826  pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1827  pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1828 
1829  producer = get_next_index(producer, rds_ring->num_desc);
1830  }
1831 
1832  if (count) {
1833  rds_ring->producer = producer;
1834  writel((producer - 1) & (rds_ring->num_desc - 1),
1835  rds_ring->crb_rcv_producer);
1836  }
1837  spin_unlock(&rds_ring->lock);
1838 }
1839 
1840 static void dump_skb(struct sk_buff *skb, struct qlcnic_adapter *adapter)
1841 {
1842  int i;
1843  unsigned char *data = skb->data;
1844 
1845  printk(KERN_INFO "\n");
1846  for (i = 0; i < skb->len; i++) {
1847  QLCDB(adapter, DRV, "%02x ", data[i]);
1848  if ((i & 0x0f) == 8)
1849  printk(KERN_INFO "\n");
1850  }
1851 }
1852 
1854  struct qlcnic_host_sds_ring *sds_ring,
1855  int ring, u64 sts_data0)
1856 {
1857  struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1858  struct sk_buff *skb;
1859  struct qlcnic_host_rds_ring *rds_ring;
1860  int index, length, cksum, pkt_offset;
1861 
1862  if (unlikely(ring >= adapter->max_rds_rings))
1863  return;
1864 
1865  rds_ring = &recv_ctx->rds_rings[ring];
1866 
1867  index = qlcnic_get_sts_refhandle(sts_data0);
1868  length = qlcnic_get_sts_totallength(sts_data0);
1869  if (unlikely(index >= rds_ring->num_desc))
1870  return;
1871 
1872  cksum = qlcnic_get_sts_status(sts_data0);
1873  pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1874 
1875  skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1876  if (!skb)
1877  return;
1878 
1879  if (length > rds_ring->skb_size)
1880  skb_put(skb, rds_ring->skb_size);
1881  else
1882  skb_put(skb, length);
1883 
1884  if (pkt_offset)
1885  skb_pull(skb, pkt_offset);
1886 
1887  if (!qlcnic_check_loopback_buff(skb->data, adapter->mac_addr))
1888  adapter->diag_cnt++;
1889  else
1890  dump_skb(skb, adapter);
1891 
1892  dev_kfree_skb_any(skb);
1893  adapter->stats.rx_pkts++;
1894  adapter->stats.rxbytes += length;
1895 
1896  return;
1897 }
1898 
1899 void
1901 {
1902  struct qlcnic_adapter *adapter = sds_ring->adapter;
1903  struct status_desc *desc;
1904  u64 sts_data0;
1905  int ring, opcode, desc_cnt;
1906 
1907  u32 consumer = sds_ring->consumer;
1908 
1909  desc = &sds_ring->desc_head[consumer];
1910  sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1911 
1912  if (!(sts_data0 & STATUS_OWNER_HOST))
1913  return;
1914 
1915  desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1916  opcode = qlcnic_get_sts_opcode(sts_data0);
1917  switch (opcode) {
1918  case QLCNIC_RESPONSE_DESC:
1919  qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1920  break;
1921  default:
1922  ring = qlcnic_get_sts_type(sts_data0);
1923  qlcnic_process_rcv_diag(adapter, sds_ring, ring, sts_data0);
1924  break;
1925  }
1926 
1927  for (; desc_cnt > 0; desc_cnt--) {
1928  desc = &sds_ring->desc_head[consumer];
1930  consumer = get_next_index(consumer, sds_ring->num_desc);
1931  }
1932 
1933  sds_ring->consumer = consumer;
1934  writel(consumer, sds_ring->crb_sts_consumer);
1935 }
1936 
1937 void
1938 qlcnic_fetch_mac(struct qlcnic_adapter *adapter, u32 off1, u32 off2,
1939  u8 alt_mac, u8 *mac)
1940 {
1941  u32 mac_low, mac_high;
1942  int i;
1943 
1944  mac_low = off1;
1945  mac_high = off2;
1946 
1947  if (alt_mac) {
1948  mac_low |= (mac_low >> 16) | (mac_high << 16);
1949  mac_high >>= 16;
1950  }
1951 
1952  for (i = 0; i < 2; i++)
1953  mac[i] = (u8)(mac_high >> ((1 - i) * 8));
1954  for (i = 2; i < 6; i++)
1955  mac[i] = (u8)(mac_low >> ((5 - i) * 8));
1956 }