Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
nes.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006 - 2011 Intel Corporation. All rights reserved.
3  * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses. You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  * Redistribution and use in source and binary forms, with or
12  * without modification, are permitted provided that the following
13  * conditions are met:
14  *
15  * - Redistributions of source code must retain the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer.
18  *
19  * - Redistributions in binary form must reproduce the above
20  * copyright notice, this list of conditions and the following
21  * disclaimer in the documentation and/or other materials
22  * provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/ethtool.h>
39 #include <linux/mii.h>
40 #include <linux/if_vlan.h>
41 #include <linux/crc32.h>
42 #include <linux/in.h>
43 #include <linux/fs.h>
44 #include <linux/init.h>
45 #include <linux/if_arp.h>
46 #include <linux/highmem.h>
47 #include <linux/slab.h>
48 #include <asm/io.h>
49 #include <asm/irq.h>
50 #include <asm/byteorder.h>
51 #include <rdma/ib_smi.h>
52 #include <rdma/ib_verbs.h>
53 #include <rdma/ib_pack.h>
54 #include <rdma/iw_cm.h>
55 
56 #include "nes.h"
57 
58 #include <net/netevent.h>
59 #include <net/neighbour.h>
60 #include <linux/route.h>
61 #include <net/ip_fib.h>
62 
63 MODULE_AUTHOR("NetEffect");
64 MODULE_DESCRIPTION("NetEffect RNIC Low-level iWARP Driver");
65 MODULE_LICENSE("Dual BSD/GPL");
67 
68 int max_mtu = 9000;
70 
71 
72 /* Interoperability */
73 int mpa_version = 1;
74 module_param(mpa_version, int, 0644);
75 MODULE_PARM_DESC(mpa_version, "MPA version to be used int MPA Req/Resp (0 or 1)");
76 
77 /* Interoperability */
79 module_param(disable_mpa_crc, int, 0644);
80 MODULE_PARM_DESC(disable_mpa_crc, "Disable checking of MPA CRC");
81 
83 module_param(nes_drv_opt, int, 0644);
84 MODULE_PARM_DESC(nes_drv_opt, "Driver option parameters");
85 
86 unsigned int nes_debug_level = 0;
87 module_param_named(debug_level, nes_debug_level, uint, 0644);
88 MODULE_PARM_DESC(debug_level, "Enable debug output level");
89 
90 unsigned int wqm_quanta = 0x10000;
91 module_param(wqm_quanta, int, 0644);
92 MODULE_PARM_DESC(wqm_quanta, "WQM quanta");
93 
94 static bool limit_maxrdreqsz;
95 module_param(limit_maxrdreqsz, bool, 0644);
96 MODULE_PARM_DESC(limit_maxrdreqsz, "Limit max read request size to 256 Bytes");
97 
98 LIST_HEAD(nes_adapter_list);
99 static LIST_HEAD(nes_dev_list);
100 
102 
103 static unsigned int ee_flsh_adapter;
104 static unsigned int sysfs_nonidx_addr;
105 static unsigned int sysfs_idx_addr;
106 
107 static struct pci_device_id nes_pci_table[] = {
110  {0}
111 };
112 
113 MODULE_DEVICE_TABLE(pci, nes_pci_table);
114 
115 static int nes_inetaddr_event(struct notifier_block *, unsigned long, void *);
116 static int nes_net_event(struct notifier_block *, unsigned long, void *);
117 static int nes_notifiers_registered;
118 
119 
120 static struct notifier_block nes_inetaddr_notifier = {
121  .notifier_call = nes_inetaddr_event
122 };
123 
124 static struct notifier_block nes_net_notifier = {
125  .notifier_call = nes_net_event
126 };
127 
131 static int nes_inetaddr_event(struct notifier_block *notifier,
132  unsigned long event, void *ptr)
133 {
134  struct in_ifaddr *ifa = ptr;
135  struct net_device *event_netdev = ifa->ifa_dev->dev;
136  struct nes_device *nesdev;
137  struct net_device *netdev;
138  struct nes_vnic *nesvnic;
139  unsigned int is_bonded;
140 
141  nes_debug(NES_DBG_NETDEV, "nes_inetaddr_event: ip address %pI4, netmask %pI4.\n",
142  &ifa->ifa_address, &ifa->ifa_mask);
143  list_for_each_entry(nesdev, &nes_dev_list, list) {
144  nes_debug(NES_DBG_NETDEV, "Nesdev list entry = 0x%p. (%s)\n",
145  nesdev, nesdev->netdev[0]->name);
146  netdev = nesdev->netdev[0];
147  nesvnic = netdev_priv(netdev);
148  is_bonded = netif_is_bond_slave(netdev) &&
149  (netdev->master == event_netdev);
150  if ((netdev == event_netdev) || is_bonded) {
151  if (nesvnic->rdma_enabled == 0) {
152  nes_debug(NES_DBG_NETDEV, "Returning without processing event for %s since"
153  " RDMA is not enabled.\n",
154  netdev->name);
155  return NOTIFY_OK;
156  }
157  /* we have ifa->ifa_address/mask here if we need it */
158  switch (event) {
159  case NETDEV_DOWN:
160  nes_debug(NES_DBG_NETDEV, "event:DOWN\n");
161  nes_write_indexed(nesdev,
162  NES_IDX_DST_IP_ADDR+(0x10*PCI_FUNC(nesdev->pcidev->devfn)), 0);
163 
164  nes_manage_arp_cache(netdev, netdev->dev_addr,
165  ntohl(nesvnic->local_ipaddr), NES_ARP_DELETE);
166  nesvnic->local_ipaddr = 0;
167  if (is_bonded)
168  continue;
169  else
170  return NOTIFY_OK;
171  break;
172  case NETDEV_UP:
173  nes_debug(NES_DBG_NETDEV, "event:UP\n");
174 
175  if (nesvnic->local_ipaddr != 0) {
176  nes_debug(NES_DBG_NETDEV, "Interface already has local_ipaddr\n");
177  return NOTIFY_OK;
178  }
179  /* fall through */
180  case NETDEV_CHANGEADDR:
181  /* Add the address to the IP table */
182  if (netdev->master)
183  nesvnic->local_ipaddr =
184  ((struct in_device *)netdev->master->ip_ptr)->ifa_list->ifa_address;
185  else
186  nesvnic->local_ipaddr = ifa->ifa_address;
187 
188  nes_write_indexed(nesdev,
189  NES_IDX_DST_IP_ADDR+(0x10*PCI_FUNC(nesdev->pcidev->devfn)),
190  ntohl(nesvnic->local_ipaddr));
191  nes_manage_arp_cache(netdev, netdev->dev_addr,
192  ntohl(nesvnic->local_ipaddr), NES_ARP_ADD);
193  if (is_bonded)
194  continue;
195  else
196  return NOTIFY_OK;
197  break;
198  default:
199  break;
200  }
201  }
202  }
203 
204  return NOTIFY_DONE;
205 }
206 
207 
211 static int nes_net_event(struct notifier_block *notifier,
212  unsigned long event, void *ptr)
213 {
214  struct neighbour *neigh = ptr;
215  struct nes_device *nesdev;
216  struct net_device *netdev;
217  struct nes_vnic *nesvnic;
218 
219  switch (event) {
221  list_for_each_entry(nesdev, &nes_dev_list, list) {
222  /* nes_debug(NES_DBG_NETDEV, "Nesdev list entry = 0x%p.\n", nesdev); */
223  netdev = nesdev->netdev[0];
224  nesvnic = netdev_priv(netdev);
225  if (netdev == neigh->dev) {
226  if (nesvnic->rdma_enabled == 0) {
227  nes_debug(NES_DBG_NETDEV, "Skipping device %s since no RDMA\n",
228  netdev->name);
229  } else {
230  if (neigh->nud_state & NUD_VALID) {
231  nes_manage_arp_cache(neigh->dev, neigh->ha,
232  ntohl(*(__be32 *)neigh->primary_key), NES_ARP_ADD);
233  } else {
234  nes_manage_arp_cache(neigh->dev, neigh->ha,
235  ntohl(*(__be32 *)neigh->primary_key), NES_ARP_DELETE);
236  }
237  }
238  return NOTIFY_OK;
239  }
240  }
241  break;
242  default:
243  nes_debug(NES_DBG_NETDEV, "NETEVENT_ %lu undefined\n", event);
244  break;
245  }
246 
247  return NOTIFY_DONE;
248 }
249 
250 
254 void nes_add_ref(struct ib_qp *ibqp)
255 {
256  struct nes_qp *nesqp;
257 
258  nesqp = to_nesqp(ibqp);
259  nes_debug(NES_DBG_QP, "Bumping refcount for QP%u. Pre-inc value = %u\n",
260  ibqp->qp_num, atomic_read(&nesqp->refcount));
261  atomic_inc(&nesqp->refcount);
262 }
263 
264 static void nes_cqp_rem_ref_callback(struct nes_device *nesdev, struct nes_cqp_request *cqp_request)
265 {
266  unsigned long flags;
267  struct nes_qp *nesqp = cqp_request->cqp_callback_pointer;
268  struct nes_adapter *nesadapter = nesdev->nesadapter;
269 
270  atomic_inc(&qps_destroyed);
271 
272  /* Free the control structures */
273 
274  if (nesqp->pbl_vbase) {
275  pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
276  nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
277  spin_lock_irqsave(&nesadapter->pbl_lock, flags);
278  nesadapter->free_256pbl++;
279  spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
280  pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
281  nesqp->pbl_vbase = NULL;
282 
283  } else {
284  pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
285  nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
286  }
287  nes_free_resource(nesadapter, nesadapter->allocated_qps, nesqp->hwqp.qp_id);
288 
289  nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = NULL;
290  kfree(nesqp->allocated_buffer);
291 
292 }
293 
297 void nes_rem_ref(struct ib_qp *ibqp)
298 {
299  u64 u64temp;
300  struct nes_qp *nesqp;
301  struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
302  struct nes_device *nesdev = nesvnic->nesdev;
303  struct nes_hw_cqp_wqe *cqp_wqe;
304  struct nes_cqp_request *cqp_request;
305  u32 opcode;
306 
307  nesqp = to_nesqp(ibqp);
308 
309  if (atomic_read(&nesqp->refcount) == 0) {
310  printk(KERN_INFO PFX "%s: Reference count already 0 for QP%d, last aeq = 0x%04X.\n",
311  __func__, ibqp->qp_num, nesqp->last_aeq);
312  BUG();
313  }
314 
315  if (atomic_dec_and_test(&nesqp->refcount)) {
316  if (nesqp->pau_mode)
317  nes_destroy_pau_qp(nesdev, nesqp);
318 
319  /* Destroy the QP */
320  cqp_request = nes_get_cqp_request(nesdev);
321  if (cqp_request == NULL) {
322  nes_debug(NES_DBG_QP, "Failed to get a cqp_request.\n");
323  return;
324  }
325  cqp_request->waiting = 0;
326  cqp_request->callback = 1;
327  cqp_request->cqp_callback = nes_cqp_rem_ref_callback;
328  cqp_request->cqp_callback_pointer = nesqp;
329  cqp_wqe = &cqp_request->cqp_wqe;
330 
331  nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
333 
334  if (nesqp->hte_added) {
335  opcode |= NES_CQP_QP_DEL_HTE;
336  nesqp->hte_added = 0;
337  }
338  set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
339  set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
340  u64temp = (u64)nesqp->nesqp_context_pbase;
341  set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
342  nes_post_cqp_request(nesdev, cqp_request);
343  }
344 }
345 
346 
350 struct ib_qp *nes_get_qp(struct ib_device *device, int qpn)
351 {
352  struct nes_vnic *nesvnic = to_nesvnic(device);
353  struct nes_device *nesdev = nesvnic->nesdev;
354  struct nes_adapter *nesadapter = nesdev->nesadapter;
355 
356  if ((qpn < NES_FIRST_QPN) || (qpn >= (NES_FIRST_QPN + nesadapter->max_qp)))
357  return NULL;
358 
359  return &nesadapter->qp_table[qpn - NES_FIRST_QPN]->ibqp;
360 }
361 
362 
366 static void nes_print_macaddr(struct net_device *netdev)
367 {
368  nes_debug(NES_DBG_INIT, "%s: %pM, IRQ %u\n",
369  netdev->name, netdev->dev_addr, netdev->irq);
370 }
371 
375 static irqreturn_t nes_interrupt(int irq, void *dev_id)
376 {
377  struct nes_device *nesdev = (struct nes_device *)dev_id;
378  int handled = 0;
379  u32 int_mask;
380  u32 int_req;
381  u32 int_stat;
382  u32 intf_int_stat;
383  u32 timer_stat;
384 
385  if (nesdev->msi_enabled) {
386  /* No need to read the interrupt pending register if msi is enabled */
387  handled = 1;
388  } else {
389  if (unlikely(nesdev->nesadapter->hw_rev == NE020_REV)) {
390  /* Master interrupt enable provides synchronization for kicking off bottom half
391  when interrupt sharing is going on */
392  int_mask = nes_read32(nesdev->regs + NES_INT_MASK);
393  if (int_mask & 0x80000000) {
394  /* Check interrupt status to see if this might be ours */
395  int_stat = nes_read32(nesdev->regs + NES_INT_STAT);
396  int_req = nesdev->int_req;
397  if (int_stat&int_req) {
398  /* if interesting CEQ or AEQ is pending, claim the interrupt */
399  if ((int_stat&int_req) & (~(NES_INT_TIMER|NES_INT_INTF))) {
400  handled = 1;
401  } else {
402  if (((int_stat & int_req) & NES_INT_TIMER) == NES_INT_TIMER) {
403  /* Timer might be running but might be for another function */
404  timer_stat = nes_read32(nesdev->regs + NES_TIMER_STAT);
405  if ((timer_stat & nesdev->timer_int_req) != 0) {
406  handled = 1;
407  }
408  }
409  if ((((int_stat & int_req) & NES_INT_INTF) == NES_INT_INTF) &&
410  (handled == 0)) {
411  intf_int_stat = nes_read32(nesdev->regs+NES_INTF_INT_STAT);
412  if ((intf_int_stat & nesdev->intf_int_req) != 0) {
413  handled = 1;
414  }
415  }
416  }
417  if (handled) {
418  nes_write32(nesdev->regs+NES_INT_MASK, int_mask & (~0x80000000));
419  int_mask = nes_read32(nesdev->regs+NES_INT_MASK);
420  /* Save off the status to save an additional read */
421  nesdev->int_stat = int_stat;
422  nesdev->napi_isr_ran = 1;
423  }
424  }
425  }
426  } else {
427  handled = nes_read32(nesdev->regs+NES_INT_PENDING);
428  }
429  }
430 
431  if (handled) {
432 
433  if (nes_napi_isr(nesdev) == 0) {
434  tasklet_schedule(&nesdev->dpc_tasklet);
435 
436  }
437  return IRQ_HANDLED;
438  } else {
439  return IRQ_NONE;
440  }
441 }
442 
443 
447 static int __devinit nes_probe(struct pci_dev *pcidev, const struct pci_device_id *ent)
448 {
449  struct net_device *netdev = NULL;
450  struct nes_device *nesdev = NULL;
451  int ret = 0;
452  void __iomem *mmio_regs = NULL;
453  u8 hw_rev;
454 
455  assert(pcidev != NULL);
456  assert(ent != NULL);
457 
458  printk(KERN_INFO PFX "NetEffect RNIC driver v%s loading. (%s)\n",
459  DRV_VERSION, pci_name(pcidev));
460 
461  ret = pci_enable_device(pcidev);
462  if (ret) {
463  printk(KERN_ERR PFX "Unable to enable PCI device. (%s)\n", pci_name(pcidev));
464  goto bail0;
465  }
466 
467  nes_debug(NES_DBG_INIT, "BAR0 (@0x%08lX) size = 0x%lX bytes\n",
468  (long unsigned int)pci_resource_start(pcidev, BAR_0),
469  (long unsigned int)pci_resource_len(pcidev, BAR_0));
470  nes_debug(NES_DBG_INIT, "BAR1 (@0x%08lX) size = 0x%lX bytes\n",
471  (long unsigned int)pci_resource_start(pcidev, BAR_1),
472  (long unsigned int)pci_resource_len(pcidev, BAR_1));
473 
474  /* Make sure PCI base addr are MMIO */
475  if (!(pci_resource_flags(pcidev, BAR_0) & IORESOURCE_MEM) ||
476  !(pci_resource_flags(pcidev, BAR_1) & IORESOURCE_MEM)) {
477  printk(KERN_ERR PFX "PCI regions not an MMIO resource\n");
478  ret = -ENODEV;
479  goto bail1;
480  }
481 
482  /* Reserve PCI I/O and memory resources */
483  ret = pci_request_regions(pcidev, DRV_NAME);
484  if (ret) {
485  printk(KERN_ERR PFX "Unable to request regions. (%s)\n", pci_name(pcidev));
486  goto bail1;
487  }
488 
489  if ((sizeof(dma_addr_t) > 4)) {
490  ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(64));
491  if (ret < 0) {
492  printk(KERN_ERR PFX "64b DMA mask configuration failed\n");
493  goto bail2;
494  }
495  ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64));
496  if (ret) {
497  printk(KERN_ERR PFX "64b DMA consistent mask configuration failed\n");
498  goto bail2;
499  }
500  } else {
501  ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32));
502  if (ret < 0) {
503  printk(KERN_ERR PFX "32b DMA mask configuration failed\n");
504  goto bail2;
505  }
506  ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
507  if (ret) {
508  printk(KERN_ERR PFX "32b DMA consistent mask configuration failed\n");
509  goto bail2;
510  }
511  }
512 
513  pci_set_master(pcidev);
514 
515  /* Allocate hardware structure */
516  nesdev = kzalloc(sizeof(struct nes_device), GFP_KERNEL);
517  if (!nesdev) {
518  printk(KERN_ERR PFX "%s: Unable to alloc hardware struct\n", pci_name(pcidev));
519  ret = -ENOMEM;
520  goto bail2;
521  }
522 
523  nes_debug(NES_DBG_INIT, "Allocated nes device at %p\n", nesdev);
524  nesdev->pcidev = pcidev;
525  pci_set_drvdata(pcidev, nesdev);
526 
527  pci_read_config_byte(pcidev, 0x0008, &hw_rev);
528  nes_debug(NES_DBG_INIT, "hw_rev=%u\n", hw_rev);
529 
531 
532  /* Remap the PCI registers in adapter BAR0 to kernel VA space */
533  mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0),
534  pci_resource_len(pcidev, BAR_0));
535  if (mmio_regs == NULL) {
536  printk(KERN_ERR PFX "Unable to remap BAR0\n");
537  ret = -EIO;
538  goto bail3;
539  }
540  nesdev->regs = mmio_regs;
541  nesdev->index_reg = 0x50 + (PCI_FUNC(pcidev->devfn)*8) + mmio_regs;
542 
543  /* Ensure interrupts are disabled */
544  nes_write32(nesdev->regs+NES_INT_MASK, 0x7fffffff);
545 
547  if (!pci_enable_msi(nesdev->pcidev)) {
548  nesdev->msi_enabled = 1;
549  nes_debug(NES_DBG_INIT, "MSI is enabled for device %s\n",
550  pci_name(pcidev));
551  } else {
552  nes_debug(NES_DBG_INIT, "MSI is disabled by linux for device %s\n",
553  pci_name(pcidev));
554  }
555  } else {
556  nes_debug(NES_DBG_INIT, "MSI not requested due to driver options for device %s\n",
557  pci_name(pcidev));
558  }
559 
560  nesdev->csr_start = pci_resource_start(nesdev->pcidev, BAR_0);
561  nesdev->doorbell_region = pci_resource_start(nesdev->pcidev, BAR_1);
562 
563  /* Init the adapter */
564  nesdev->nesadapter = nes_init_adapter(nesdev, hw_rev);
565  if (!nesdev->nesadapter) {
566  printk(KERN_ERR PFX "Unable to initialize adapter.\n");
567  ret = -ENOMEM;
568  goto bail5;
569  }
570  nesdev->nesadapter->et_rx_coalesce_usecs_irq = interrupt_mod_interval;
571  nesdev->nesadapter->wqm_quanta = wqm_quanta;
572 
573  /* nesdev->base_doorbell_index =
574  nesdev->nesadapter->pd_config_base[PCI_FUNC(nesdev->pcidev->devfn)]; */
575  nesdev->base_doorbell_index = 1;
576  nesdev->doorbell_start = nesdev->nesadapter->doorbell_start;
577  if (nesdev->nesadapter->phy_type[0] == NES_PHY_TYPE_PUMA_1G) {
578  switch (PCI_FUNC(nesdev->pcidev->devfn) %
579  nesdev->nesadapter->port_count) {
580  case 1:
581  nesdev->mac_index = 2;
582  break;
583  case 2:
584  nesdev->mac_index = 1;
585  break;
586  case 3:
587  nesdev->mac_index = 3;
588  break;
589  case 0:
590  default:
591  nesdev->mac_index = 0;
592  }
593  } else {
594  nesdev->mac_index = PCI_FUNC(nesdev->pcidev->devfn) %
595  nesdev->nesadapter->port_count;
596  }
597 
598  if ((limit_maxrdreqsz ||
599  ((nesdev->nesadapter->phy_type[0] == NES_PHY_TYPE_GLADIUS) &&
600  (hw_rev == NE020_REV1))) &&
601  (pcie_get_readrq(pcidev) > 256)) {
602  if (pcie_set_readrq(pcidev, 256))
603  printk(KERN_ERR PFX "Unable to set max read request"
604  " to 256 bytes\n");
605  else
606  nes_debug(NES_DBG_INIT, "Max read request size set"
607  " to 256 bytes\n");
608  }
609 
610  tasklet_init(&nesdev->dpc_tasklet, nes_dpc, (unsigned long)nesdev);
611 
612  /* bring up the Control QP */
613  if (nes_init_cqp(nesdev)) {
614  ret = -ENODEV;
615  goto bail6;
616  }
617 
618  /* Arm the CCQ */
619  nes_write32(nesdev->regs+NES_CQE_ALLOC, NES_CQE_ALLOC_NOTIFY_NEXT |
620  PCI_FUNC(nesdev->pcidev->devfn));
621  nes_read32(nesdev->regs+NES_CQE_ALLOC);
622 
623  /* Enable the interrupts */
624  nesdev->int_req = (0x101 << PCI_FUNC(nesdev->pcidev->devfn)) |
625  (1 << (PCI_FUNC(nesdev->pcidev->devfn)+16));
626  if (PCI_FUNC(nesdev->pcidev->devfn) < 4) {
627  nesdev->int_req |= (1 << (PCI_FUNC(nesdev->mac_index)+24));
628  }
629 
630  /* TODO: This really should be the first driver to load, not function 0 */
631  if (PCI_FUNC(nesdev->pcidev->devfn) == 0) {
632  /* pick up PCI and critical errors if the first driver to load */
634  nesdev->int_req |= NES_INT_INTF;
635  } else {
636  nesdev->intf_int_req = 0;
637  }
638  nesdev->intf_int_req |= (1 << (PCI_FUNC(nesdev->pcidev->devfn)+16));
639  nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS0, 0);
640  nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS1, 0);
641  nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS2, 0x00001265);
642  nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS4, 0x18021804);
643 
644  nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS3, 0x17801790);
645 
646  /* deal with both periodic and one_shot */
647  nesdev->timer_int_req = 0x101 << PCI_FUNC(nesdev->pcidev->devfn);
648  nesdev->nesadapter->timer_int_req |= nesdev->timer_int_req;
649  nes_debug(NES_DBG_INIT, "setting int_req for function %u, nesdev = 0x%04X, adapter = 0x%04X\n",
650  PCI_FUNC(nesdev->pcidev->devfn),
651  nesdev->timer_int_req, nesdev->nesadapter->timer_int_req);
652 
653  nes_write32(nesdev->regs+NES_INTF_INT_MASK, ~(nesdev->intf_int_req));
654 
655  list_add_tail(&nesdev->list, &nes_dev_list);
656 
657  /* Request an interrupt line for the driver */
658  ret = request_irq(pcidev->irq, nes_interrupt, IRQF_SHARED, DRV_NAME, nesdev);
659  if (ret) {
660  printk(KERN_ERR PFX "%s: requested IRQ %u is busy\n",
661  pci_name(pcidev), pcidev->irq);
662  goto bail65;
663  }
664 
665  nes_write32(nesdev->regs+NES_INT_MASK, ~nesdev->int_req);
666 
667  if (nes_notifiers_registered == 0) {
668  register_inetaddr_notifier(&nes_inetaddr_notifier);
669  register_netevent_notifier(&nes_net_notifier);
670  }
671  nes_notifiers_registered++;
672 
674 
675  /* Initialize network devices */
676  if ((netdev = nes_netdev_init(nesdev, mmio_regs)) == NULL)
677  goto bail7;
678 
679  /* Register network device */
680  ret = register_netdev(netdev);
681  if (ret) {
682  printk(KERN_ERR PFX "Unable to register netdev, ret = %d\n", ret);
683  nes_netdev_destroy(netdev);
684  goto bail7;
685  }
686 
687  nes_print_macaddr(netdev);
688 
689  nesdev->netdev_count++;
690  nesdev->nesadapter->netdev_count++;
691 
692  printk(KERN_INFO PFX "%s: NetEffect RNIC driver successfully loaded.\n",
693  pci_name(pcidev));
694  return 0;
695 
696  bail7:
697  printk(KERN_ERR PFX "bail7\n");
698  while (nesdev->netdev_count > 0) {
699  nesdev->netdev_count--;
700  nesdev->nesadapter->netdev_count--;
701 
702  unregister_netdev(nesdev->netdev[nesdev->netdev_count]);
703  nes_netdev_destroy(nesdev->netdev[nesdev->netdev_count]);
704  }
705 
706  nes_debug(NES_DBG_INIT, "netdev_count=%d, nesadapter->netdev_count=%d\n",
707  nesdev->netdev_count, nesdev->nesadapter->netdev_count);
708 
709  nes_notifiers_registered--;
710  if (nes_notifiers_registered == 0) {
711  unregister_netevent_notifier(&nes_net_notifier);
712  unregister_inetaddr_notifier(&nes_inetaddr_notifier);
713  }
714 
715  list_del(&nesdev->list);
716  nes_destroy_cqp(nesdev);
717 
718  bail65:
719  printk(KERN_ERR PFX "bail65\n");
720  free_irq(pcidev->irq, nesdev);
721  if (nesdev->msi_enabled) {
722  pci_disable_msi(pcidev);
723  }
724  bail6:
725  printk(KERN_ERR PFX "bail6\n");
726  tasklet_kill(&nesdev->dpc_tasklet);
727  /* Deallocate the Adapter Structure */
729 
730  bail5:
731  printk(KERN_ERR PFX "bail5\n");
732  iounmap(nesdev->regs);
733 
734  bail3:
735  printk(KERN_ERR PFX "bail3\n");
736  kfree(nesdev);
737 
738  bail2:
739  pci_release_regions(pcidev);
740 
741  bail1:
742  pci_disable_device(pcidev);
743 
744  bail0:
745  return ret;
746 }
747 
748 
752 static void __devexit nes_remove(struct pci_dev *pcidev)
753 {
754  struct nes_device *nesdev = pci_get_drvdata(pcidev);
755  struct net_device *netdev;
756  int netdev_index = 0;
757  unsigned long flags;
758 
759  if (nesdev->netdev_count) {
760  netdev = nesdev->netdev[netdev_index];
761  if (netdev) {
762  netif_stop_queue(netdev);
763  unregister_netdev(netdev);
764  nes_netdev_destroy(netdev);
765 
766  nesdev->netdev[netdev_index] = NULL;
767  nesdev->netdev_count--;
768  nesdev->nesadapter->netdev_count--;
769  }
770  }
771 
772  nes_notifiers_registered--;
773  if (nes_notifiers_registered == 0) {
774  unregister_netevent_notifier(&nes_net_notifier);
775  unregister_inetaddr_notifier(&nes_inetaddr_notifier);
776  }
777 
778  list_del(&nesdev->list);
779  nes_destroy_cqp(nesdev);
780 
781  free_irq(pcidev->irq, nesdev);
782  tasklet_kill(&nesdev->dpc_tasklet);
783 
784  spin_lock_irqsave(&nesdev->nesadapter->phy_lock, flags);
785  if (nesdev->link_recheck) {
786  spin_unlock_irqrestore(&nesdev->nesadapter->phy_lock, flags);
787  cancel_delayed_work_sync(&nesdev->work);
788  } else {
789  spin_unlock_irqrestore(&nesdev->nesadapter->phy_lock, flags);
790  }
791 
792  /* Deallocate the Adapter Structure */
794 
795  if (nesdev->msi_enabled) {
796  pci_disable_msi(pcidev);
797  }
798 
799  iounmap(nesdev->regs);
800  kfree(nesdev);
801 
802  /* nes_debug(NES_DBG_SHUTDOWN, "calling pci_release_regions.\n"); */
803  pci_release_regions(pcidev);
804  pci_disable_device(pcidev);
805  pci_set_drvdata(pcidev, NULL);
806 }
807 
808 
809 static struct pci_driver nes_pci_driver = {
810  .name = DRV_NAME,
811  .id_table = nes_pci_table,
812  .probe = nes_probe,
813  .remove = __devexit_p(nes_remove),
814 };
815 
816 static ssize_t nes_show_adapter(struct device_driver *ddp, char *buf)
817 {
818  unsigned int devfn = 0xffffffff;
819  unsigned char bus_number = 0xff;
820  unsigned int i = 0;
821  struct nes_device *nesdev;
822 
823  list_for_each_entry(nesdev, &nes_dev_list, list) {
824  if (i == ee_flsh_adapter) {
825  devfn = nesdev->pcidev->devfn;
826  bus_number = nesdev->pcidev->bus->number;
827  break;
828  }
829  i++;
830  }
831 
832  return snprintf(buf, PAGE_SIZE, "%x:%x\n", bus_number, devfn);
833 }
834 
835 static ssize_t nes_store_adapter(struct device_driver *ddp,
836  const char *buf, size_t count)
837 {
838  char *p = (char *)buf;
839 
840  ee_flsh_adapter = simple_strtoul(p, &p, 10);
841  return strnlen(buf, count);
842 }
843 
844 static ssize_t nes_show_ee_cmd(struct device_driver *ddp, char *buf)
845 {
846  u32 eeprom_cmd = 0xdead;
847  u32 i = 0;
848  struct nes_device *nesdev;
849 
850  list_for_each_entry(nesdev, &nes_dev_list, list) {
851  if (i == ee_flsh_adapter) {
852  eeprom_cmd = nes_read32(nesdev->regs + NES_EEPROM_COMMAND);
853  break;
854  }
855  i++;
856  }
857  return snprintf(buf, PAGE_SIZE, "0x%x\n", eeprom_cmd);
858 }
859 
860 static ssize_t nes_store_ee_cmd(struct device_driver *ddp,
861  const char *buf, size_t count)
862 {
863  char *p = (char *)buf;
864  u32 val;
865  u32 i = 0;
866  struct nes_device *nesdev;
867 
868  if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
869  val = simple_strtoul(p, &p, 16);
870  list_for_each_entry(nesdev, &nes_dev_list, list) {
871  if (i == ee_flsh_adapter) {
872  nes_write32(nesdev->regs + NES_EEPROM_COMMAND, val);
873  break;
874  }
875  i++;
876  }
877  }
878  return strnlen(buf, count);
879 }
880 
881 static ssize_t nes_show_ee_data(struct device_driver *ddp, char *buf)
882 {
883  u32 eeprom_data = 0xdead;
884  u32 i = 0;
885  struct nes_device *nesdev;
886 
887  list_for_each_entry(nesdev, &nes_dev_list, list) {
888  if (i == ee_flsh_adapter) {
889  eeprom_data = nes_read32(nesdev->regs + NES_EEPROM_DATA);
890  break;
891  }
892  i++;
893  }
894 
895  return snprintf(buf, PAGE_SIZE, "0x%x\n", eeprom_data);
896 }
897 
898 static ssize_t nes_store_ee_data(struct device_driver *ddp,
899  const char *buf, size_t count)
900 {
901  char *p = (char *)buf;
902  u32 val;
903  u32 i = 0;
904  struct nes_device *nesdev;
905 
906  if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
907  val = simple_strtoul(p, &p, 16);
908  list_for_each_entry(nesdev, &nes_dev_list, list) {
909  if (i == ee_flsh_adapter) {
910  nes_write32(nesdev->regs + NES_EEPROM_DATA, val);
911  break;
912  }
913  i++;
914  }
915  }
916  return strnlen(buf, count);
917 }
918 
919 static ssize_t nes_show_flash_cmd(struct device_driver *ddp, char *buf)
920 {
921  u32 flash_cmd = 0xdead;
922  u32 i = 0;
923  struct nes_device *nesdev;
924 
925  list_for_each_entry(nesdev, &nes_dev_list, list) {
926  if (i == ee_flsh_adapter) {
927  flash_cmd = nes_read32(nesdev->regs + NES_FLASH_COMMAND);
928  break;
929  }
930  i++;
931  }
932 
933  return snprintf(buf, PAGE_SIZE, "0x%x\n", flash_cmd);
934 }
935 
936 static ssize_t nes_store_flash_cmd(struct device_driver *ddp,
937  const char *buf, size_t count)
938 {
939  char *p = (char *)buf;
940  u32 val;
941  u32 i = 0;
942  struct nes_device *nesdev;
943 
944  if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
945  val = simple_strtoul(p, &p, 16);
946  list_for_each_entry(nesdev, &nes_dev_list, list) {
947  if (i == ee_flsh_adapter) {
948  nes_write32(nesdev->regs + NES_FLASH_COMMAND, val);
949  break;
950  }
951  i++;
952  }
953  }
954  return strnlen(buf, count);
955 }
956 
957 static ssize_t nes_show_flash_data(struct device_driver *ddp, char *buf)
958 {
959  u32 flash_data = 0xdead;
960  u32 i = 0;
961  struct nes_device *nesdev;
962 
963  list_for_each_entry(nesdev, &nes_dev_list, list) {
964  if (i == ee_flsh_adapter) {
965  flash_data = nes_read32(nesdev->regs + NES_FLASH_DATA);
966  break;
967  }
968  i++;
969  }
970 
971  return snprintf(buf, PAGE_SIZE, "0x%x\n", flash_data);
972 }
973 
974 static ssize_t nes_store_flash_data(struct device_driver *ddp,
975  const char *buf, size_t count)
976 {
977  char *p = (char *)buf;
978  u32 val;
979  u32 i = 0;
980  struct nes_device *nesdev;
981 
982  if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
983  val = simple_strtoul(p, &p, 16);
984  list_for_each_entry(nesdev, &nes_dev_list, list) {
985  if (i == ee_flsh_adapter) {
986  nes_write32(nesdev->regs + NES_FLASH_DATA, val);
987  break;
988  }
989  i++;
990  }
991  }
992  return strnlen(buf, count);
993 }
994 
995 static ssize_t nes_show_nonidx_addr(struct device_driver *ddp, char *buf)
996 {
997  return snprintf(buf, PAGE_SIZE, "0x%x\n", sysfs_nonidx_addr);
998 }
999 
1000 static ssize_t nes_store_nonidx_addr(struct device_driver *ddp,
1001  const char *buf, size_t count)
1002 {
1003  char *p = (char *)buf;
1004 
1005  if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X')
1006  sysfs_nonidx_addr = simple_strtoul(p, &p, 16);
1007 
1008  return strnlen(buf, count);
1009 }
1010 
1011 static ssize_t nes_show_nonidx_data(struct device_driver *ddp, char *buf)
1012 {
1013  u32 nonidx_data = 0xdead;
1014  u32 i = 0;
1015  struct nes_device *nesdev;
1016 
1017  list_for_each_entry(nesdev, &nes_dev_list, list) {
1018  if (i == ee_flsh_adapter) {
1019  nonidx_data = nes_read32(nesdev->regs + sysfs_nonidx_addr);
1020  break;
1021  }
1022  i++;
1023  }
1024 
1025  return snprintf(buf, PAGE_SIZE, "0x%x\n", nonidx_data);
1026 }
1027 
1028 static ssize_t nes_store_nonidx_data(struct device_driver *ddp,
1029  const char *buf, size_t count)
1030 {
1031  char *p = (char *)buf;
1032  u32 val;
1033  u32 i = 0;
1034  struct nes_device *nesdev;
1035 
1036  if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
1037  val = simple_strtoul(p, &p, 16);
1038  list_for_each_entry(nesdev, &nes_dev_list, list) {
1039  if (i == ee_flsh_adapter) {
1040  nes_write32(nesdev->regs + sysfs_nonidx_addr, val);
1041  break;
1042  }
1043  i++;
1044  }
1045  }
1046  return strnlen(buf, count);
1047 }
1048 
1049 static ssize_t nes_show_idx_addr(struct device_driver *ddp, char *buf)
1050 {
1051  return snprintf(buf, PAGE_SIZE, "0x%x\n", sysfs_idx_addr);
1052 }
1053 
1054 static ssize_t nes_store_idx_addr(struct device_driver *ddp,
1055  const char *buf, size_t count)
1056 {
1057  char *p = (char *)buf;
1058 
1059  if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X')
1060  sysfs_idx_addr = simple_strtoul(p, &p, 16);
1061 
1062  return strnlen(buf, count);
1063 }
1064 
1065 static ssize_t nes_show_idx_data(struct device_driver *ddp, char *buf)
1066 {
1067  u32 idx_data = 0xdead;
1068  u32 i = 0;
1069  struct nes_device *nesdev;
1070 
1071  list_for_each_entry(nesdev, &nes_dev_list, list) {
1072  if (i == ee_flsh_adapter) {
1073  idx_data = nes_read_indexed(nesdev, sysfs_idx_addr);
1074  break;
1075  }
1076  i++;
1077  }
1078 
1079  return snprintf(buf, PAGE_SIZE, "0x%x\n", idx_data);
1080 }
1081 
1082 static ssize_t nes_store_idx_data(struct device_driver *ddp,
1083  const char *buf, size_t count)
1084 {
1085  char *p = (char *)buf;
1086  u32 val;
1087  u32 i = 0;
1088  struct nes_device *nesdev;
1089 
1090  if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
1091  val = simple_strtoul(p, &p, 16);
1092  list_for_each_entry(nesdev, &nes_dev_list, list) {
1093  if (i == ee_flsh_adapter) {
1094  nes_write_indexed(nesdev, sysfs_idx_addr, val);
1095  break;
1096  }
1097  i++;
1098  }
1099  }
1100  return strnlen(buf, count);
1101 }
1102 
1103 
1107 static ssize_t nes_show_wqm_quanta(struct device_driver *ddp, char *buf)
1108 {
1109  u32 wqm_quanta_value = 0xdead;
1110  u32 i = 0;
1111  struct nes_device *nesdev;
1112 
1113  list_for_each_entry(nesdev, &nes_dev_list, list) {
1114  if (i == ee_flsh_adapter) {
1115  wqm_quanta_value = nesdev->nesadapter->wqm_quanta;
1116  break;
1117  }
1118  i++;
1119  }
1120 
1121  return snprintf(buf, PAGE_SIZE, "0x%X\n", wqm_quanta_value);
1122 }
1123 
1124 
1128 static ssize_t nes_store_wqm_quanta(struct device_driver *ddp,
1129  const char *buf, size_t count)
1130 {
1131  unsigned long wqm_quanta_value;
1132  u32 wqm_config1;
1133  u32 i = 0;
1134  struct nes_device *nesdev;
1135 
1136  if (kstrtoul(buf, 0, &wqm_quanta_value) < 0)
1137  return -EINVAL;
1138 
1139  list_for_each_entry(nesdev, &nes_dev_list, list) {
1140  if (i == ee_flsh_adapter) {
1141  nesdev->nesadapter->wqm_quanta = wqm_quanta_value;
1142  wqm_config1 = nes_read_indexed(nesdev,
1144  nes_write_indexed(nesdev, NES_IDX_WQM_CONFIG1,
1145  ((wqm_quanta_value << 1) |
1146  (wqm_config1 & 0x00000001)));
1147  break;
1148  }
1149  i++;
1150  }
1151  return strnlen(buf, count);
1152 }
1153 
1155  nes_show_adapter, nes_store_adapter);
1156 static DRIVER_ATTR(eeprom_cmd, S_IRUSR | S_IWUSR,
1157  nes_show_ee_cmd, nes_store_ee_cmd);
1158 static DRIVER_ATTR(eeprom_data, S_IRUSR | S_IWUSR,
1159  nes_show_ee_data, nes_store_ee_data);
1160 static DRIVER_ATTR(flash_cmd, S_IRUSR | S_IWUSR,
1161  nes_show_flash_cmd, nes_store_flash_cmd);
1162 static DRIVER_ATTR(flash_data, S_IRUSR | S_IWUSR,
1163  nes_show_flash_data, nes_store_flash_data);
1164 static DRIVER_ATTR(nonidx_addr, S_IRUSR | S_IWUSR,
1165  nes_show_nonidx_addr, nes_store_nonidx_addr);
1166 static DRIVER_ATTR(nonidx_data, S_IRUSR | S_IWUSR,
1167  nes_show_nonidx_data, nes_store_nonidx_data);
1168 static DRIVER_ATTR(idx_addr, S_IRUSR | S_IWUSR,
1169  nes_show_idx_addr, nes_store_idx_addr);
1170 static DRIVER_ATTR(idx_data, S_IRUSR | S_IWUSR,
1171  nes_show_idx_data, nes_store_idx_data);
1173  nes_show_wqm_quanta, nes_store_wqm_quanta);
1174 
1175 static int nes_create_driver_sysfs(struct pci_driver *drv)
1176 {
1177  int error;
1178  error = driver_create_file(&drv->driver, &driver_attr_adapter);
1179  error |= driver_create_file(&drv->driver, &driver_attr_eeprom_cmd);
1180  error |= driver_create_file(&drv->driver, &driver_attr_eeprom_data);
1181  error |= driver_create_file(&drv->driver, &driver_attr_flash_cmd);
1182  error |= driver_create_file(&drv->driver, &driver_attr_flash_data);
1183  error |= driver_create_file(&drv->driver, &driver_attr_nonidx_addr);
1184  error |= driver_create_file(&drv->driver, &driver_attr_nonidx_data);
1185  error |= driver_create_file(&drv->driver, &driver_attr_idx_addr);
1186  error |= driver_create_file(&drv->driver, &driver_attr_idx_data);
1187  error |= driver_create_file(&drv->driver, &driver_attr_wqm_quanta);
1188  return error;
1189 }
1190 
1191 static void nes_remove_driver_sysfs(struct pci_driver *drv)
1192 {
1193  driver_remove_file(&drv->driver, &driver_attr_adapter);
1194  driver_remove_file(&drv->driver, &driver_attr_eeprom_cmd);
1195  driver_remove_file(&drv->driver, &driver_attr_eeprom_data);
1196  driver_remove_file(&drv->driver, &driver_attr_flash_cmd);
1197  driver_remove_file(&drv->driver, &driver_attr_flash_data);
1198  driver_remove_file(&drv->driver, &driver_attr_nonidx_addr);
1199  driver_remove_file(&drv->driver, &driver_attr_nonidx_data);
1200  driver_remove_file(&drv->driver, &driver_attr_idx_addr);
1201  driver_remove_file(&drv->driver, &driver_attr_idx_data);
1202  driver_remove_file(&drv->driver, &driver_attr_wqm_quanta);
1203 }
1204 
1208 static int __init nes_init_module(void)
1209 {
1210  int retval;
1211  int retval1;
1212 
1213  retval = nes_cm_start();
1214  if (retval) {
1215  printk(KERN_ERR PFX "Unable to start NetEffect iWARP CM.\n");
1216  return retval;
1217  }
1218  retval = pci_register_driver(&nes_pci_driver);
1219  if (retval >= 0) {
1220  retval1 = nes_create_driver_sysfs(&nes_pci_driver);
1221  if (retval1 < 0)
1222  printk(KERN_ERR PFX "Unable to create NetEffect sys files.\n");
1223  }
1224  return retval;
1225 }
1226 
1227 
1231 static void __exit nes_exit_module(void)
1232 {
1233  nes_cm_stop();
1234  nes_remove_driver_sysfs(&nes_pci_driver);
1235 
1236  pci_unregister_driver(&nes_pci_driver);
1237 }
1238 
1239 
1240 module_init(nes_init_module);
1241 module_exit(nes_exit_module);