Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / infiniband / hw / nes / nes.c
blob42b68aa999fcfa2e16396a267bd292d24927f80f
1 /*
2 * Copyright (c) 2006 - 2011 Intel Corporation. All rights reserved.
3 * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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.
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.
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>
56 #include "nes.h"
58 #include <net/netevent.h>
59 #include <net/neighbour.h>
60 #include <linux/route.h>
61 #include <net/ip_fib.h>
63 MODULE_AUTHOR("NetEffect");
64 MODULE_DESCRIPTION("NetEffect RNIC Low-level iWARP Driver");
65 MODULE_LICENSE("Dual BSD/GPL");
67 int interrupt_mod_interval = 0;
69 /* Interoperability */
70 int mpa_version = 1;
71 module_param(mpa_version, int, 0644);
72 MODULE_PARM_DESC(mpa_version, "MPA version to be used int MPA Req/Resp (0 or 1)");
74 /* Interoperability */
75 int disable_mpa_crc = 0;
76 module_param(disable_mpa_crc, int, 0644);
77 MODULE_PARM_DESC(disable_mpa_crc, "Disable checking of MPA CRC");
79 unsigned int nes_drv_opt = NES_DRV_OPT_DISABLE_INT_MOD | NES_DRV_OPT_ENABLE_PAU;
80 module_param(nes_drv_opt, int, 0644);
81 MODULE_PARM_DESC(nes_drv_opt, "Driver option parameters");
83 unsigned int nes_debug_level = 0;
84 module_param_named(debug_level, nes_debug_level, uint, 0644);
85 MODULE_PARM_DESC(debug_level, "Enable debug output level");
87 unsigned int wqm_quanta = 0x10000;
88 module_param(wqm_quanta, int, 0644);
89 MODULE_PARM_DESC(wqm_quanta, "WQM quanta");
91 static bool limit_maxrdreqsz;
92 module_param(limit_maxrdreqsz, bool, 0644);
93 MODULE_PARM_DESC(limit_maxrdreqsz, "Limit max read request size to 256 Bytes");
95 LIST_HEAD(nes_adapter_list);
96 static LIST_HEAD(nes_dev_list);
98 atomic_t qps_destroyed;
100 static unsigned int ee_flsh_adapter;
101 static unsigned int sysfs_nonidx_addr;
102 static unsigned int sysfs_idx_addr;
104 static const struct pci_device_id nes_pci_table[] = {
105 { PCI_VDEVICE(NETEFFECT, PCI_DEVICE_ID_NETEFFECT_NE020), },
106 { PCI_VDEVICE(NETEFFECT, PCI_DEVICE_ID_NETEFFECT_NE020_KR), },
110 MODULE_DEVICE_TABLE(pci, nes_pci_table);
112 static int nes_inetaddr_event(struct notifier_block *, unsigned long, void *);
113 static int nes_net_event(struct notifier_block *, unsigned long, void *);
114 static int nes_notifiers_registered;
117 static struct notifier_block nes_inetaddr_notifier = {
118 .notifier_call = nes_inetaddr_event
121 static struct notifier_block nes_net_notifier = {
122 .notifier_call = nes_net_event
126 * nes_inetaddr_event
128 static int nes_inetaddr_event(struct notifier_block *notifier,
129 unsigned long event, void *ptr)
131 struct in_ifaddr *ifa = ptr;
132 struct net_device *event_netdev = ifa->ifa_dev->dev;
133 struct nes_device *nesdev;
134 struct net_device *netdev;
135 struct net_device *upper_dev;
136 struct nes_vnic *nesvnic;
137 unsigned int is_bonded;
139 nes_debug(NES_DBG_NETDEV, "nes_inetaddr_event: ip address %pI4, netmask %pI4.\n",
140 &ifa->ifa_address, &ifa->ifa_mask);
141 list_for_each_entry(nesdev, &nes_dev_list, list) {
142 nes_debug(NES_DBG_NETDEV, "Nesdev list entry = 0x%p. (%s)\n",
143 nesdev, nesdev->netdev[0]->name);
144 netdev = nesdev->netdev[0];
145 nesvnic = netdev_priv(netdev);
146 upper_dev = netdev_master_upper_dev_get(netdev);
147 is_bonded = netif_is_bond_slave(netdev) &&
148 (upper_dev == event_netdev);
149 if ((netdev == event_netdev) || is_bonded) {
150 if (nesvnic->rdma_enabled == 0) {
151 nes_debug(NES_DBG_NETDEV, "Returning without processing event for %s since"
152 " RDMA is not enabled.\n",
153 netdev->name);
154 return NOTIFY_OK;
156 /* we have ifa->ifa_address/mask here if we need it */
157 switch (event) {
158 case NETDEV_DOWN:
159 nes_debug(NES_DBG_NETDEV, "event:DOWN\n");
160 nes_write_indexed(nesdev,
161 NES_IDX_DST_IP_ADDR+(0x10*PCI_FUNC(nesdev->pcidev->devfn)), 0);
163 nes_manage_arp_cache(netdev, netdev->dev_addr,
164 ntohl(nesvnic->local_ipaddr), NES_ARP_DELETE);
165 nesvnic->local_ipaddr = 0;
166 if (is_bonded)
167 continue;
168 else
169 return NOTIFY_OK;
170 break;
171 case NETDEV_UP:
172 nes_debug(NES_DBG_NETDEV, "event:UP\n");
174 if (nesvnic->local_ipaddr != 0) {
175 nes_debug(NES_DBG_NETDEV, "Interface already has local_ipaddr\n");
176 return NOTIFY_OK;
178 /* fall through */
179 case NETDEV_CHANGEADDR:
180 /* Add the address to the IP table */
181 if (upper_dev) {
182 struct in_device *in;
184 rcu_read_lock();
185 in = __in_dev_get_rcu(upper_dev);
186 nesvnic->local_ipaddr = in->ifa_list->ifa_address;
187 rcu_read_unlock();
188 } else {
189 nesvnic->local_ipaddr = ifa->ifa_address;
192 nes_write_indexed(nesdev,
193 NES_IDX_DST_IP_ADDR+(0x10*PCI_FUNC(nesdev->pcidev->devfn)),
194 ntohl(nesvnic->local_ipaddr));
195 nes_manage_arp_cache(netdev, netdev->dev_addr,
196 ntohl(nesvnic->local_ipaddr), NES_ARP_ADD);
197 if (is_bonded)
198 continue;
199 else
200 return NOTIFY_OK;
201 break;
202 default:
203 break;
208 return NOTIFY_DONE;
213 * nes_net_event
215 static int nes_net_event(struct notifier_block *notifier,
216 unsigned long event, void *ptr)
218 struct neighbour *neigh = ptr;
219 struct nes_device *nesdev;
220 struct net_device *netdev;
221 struct nes_vnic *nesvnic;
223 switch (event) {
224 case NETEVENT_NEIGH_UPDATE:
225 list_for_each_entry(nesdev, &nes_dev_list, list) {
226 /* nes_debug(NES_DBG_NETDEV, "Nesdev list entry = 0x%p.\n", nesdev); */
227 netdev = nesdev->netdev[0];
228 nesvnic = netdev_priv(netdev);
229 if (netdev == neigh->dev) {
230 if (nesvnic->rdma_enabled == 0) {
231 nes_debug(NES_DBG_NETDEV, "Skipping device %s since no RDMA\n",
232 netdev->name);
233 } else {
234 if (neigh->nud_state & NUD_VALID) {
235 nes_manage_arp_cache(neigh->dev, neigh->ha,
236 ntohl(*(__be32 *)neigh->primary_key), NES_ARP_ADD);
237 } else {
238 nes_manage_arp_cache(neigh->dev, neigh->ha,
239 ntohl(*(__be32 *)neigh->primary_key), NES_ARP_DELETE);
242 return NOTIFY_OK;
245 break;
246 default:
247 nes_debug(NES_DBG_NETDEV, "NETEVENT_ %lu undefined\n", event);
248 break;
251 return NOTIFY_DONE;
256 * nes_add_ref
258 void nes_add_ref(struct ib_qp *ibqp)
260 struct nes_qp *nesqp;
262 nesqp = to_nesqp(ibqp);
263 nes_debug(NES_DBG_QP, "Bumping refcount for QP%u. Pre-inc value = %u\n",
264 ibqp->qp_num, atomic_read(&nesqp->refcount));
265 atomic_inc(&nesqp->refcount);
268 static void nes_cqp_rem_ref_callback(struct nes_device *nesdev, struct nes_cqp_request *cqp_request)
270 unsigned long flags;
271 struct nes_qp *nesqp = cqp_request->cqp_callback_pointer;
272 struct nes_adapter *nesadapter = nesdev->nesadapter;
274 atomic_inc(&qps_destroyed);
276 /* Free the control structures */
278 if (nesqp->pbl_vbase) {
279 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
280 nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
281 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
282 nesadapter->free_256pbl++;
283 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
284 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
285 nesqp->pbl_vbase = NULL;
287 } else {
288 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
289 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
291 nes_free_resource(nesadapter, nesadapter->allocated_qps, nesqp->hwqp.qp_id);
293 nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = NULL;
294 kfree(nesqp->allocated_buffer);
299 * nes_rem_ref
301 void nes_rem_ref(struct ib_qp *ibqp)
303 u64 u64temp;
304 struct nes_qp *nesqp;
305 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
306 struct nes_device *nesdev = nesvnic->nesdev;
307 struct nes_hw_cqp_wqe *cqp_wqe;
308 struct nes_cqp_request *cqp_request;
309 u32 opcode;
311 nesqp = to_nesqp(ibqp);
313 if (atomic_read(&nesqp->refcount) == 0) {
314 printk(KERN_INFO PFX "%s: Reference count already 0 for QP%d, last aeq = 0x%04X.\n",
315 __func__, ibqp->qp_num, nesqp->last_aeq);
316 BUG();
319 if (atomic_dec_and_test(&nesqp->refcount)) {
320 if (nesqp->pau_mode)
321 nes_destroy_pau_qp(nesdev, nesqp);
323 /* Destroy the QP */
324 cqp_request = nes_get_cqp_request(nesdev);
325 if (cqp_request == NULL) {
326 nes_debug(NES_DBG_QP, "Failed to get a cqp_request.\n");
327 return;
329 cqp_request->waiting = 0;
330 cqp_request->callback = 1;
331 cqp_request->cqp_callback = nes_cqp_rem_ref_callback;
332 cqp_request->cqp_callback_pointer = nesqp;
333 cqp_wqe = &cqp_request->cqp_wqe;
335 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
336 opcode = NES_CQP_DESTROY_QP | NES_CQP_QP_TYPE_IWARP;
338 if (nesqp->hte_added) {
339 opcode |= NES_CQP_QP_DEL_HTE;
340 nesqp->hte_added = 0;
342 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
343 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
344 u64temp = (u64)nesqp->nesqp_context_pbase;
345 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
346 nes_post_cqp_request(nesdev, cqp_request);
352 * nes_get_qp
354 struct ib_qp *nes_get_qp(struct ib_device *device, int qpn)
356 struct nes_vnic *nesvnic = to_nesvnic(device);
357 struct nes_device *nesdev = nesvnic->nesdev;
358 struct nes_adapter *nesadapter = nesdev->nesadapter;
360 if ((qpn < NES_FIRST_QPN) || (qpn >= (NES_FIRST_QPN + nesadapter->max_qp)))
361 return NULL;
363 return &nesadapter->qp_table[qpn - NES_FIRST_QPN]->ibqp;
368 * nes_print_macaddr
370 static void nes_print_macaddr(struct net_device *netdev)
372 nes_debug(NES_DBG_INIT, "%s: %pM, IRQ %u\n",
373 netdev->name, netdev->dev_addr, netdev->irq);
377 * nes_interrupt - handle interrupts
379 static irqreturn_t nes_interrupt(int irq, void *dev_id)
381 struct nes_device *nesdev = (struct nes_device *)dev_id;
382 int handled = 0;
383 u32 int_mask;
384 u32 int_req;
385 u32 int_stat;
386 u32 intf_int_stat;
387 u32 timer_stat;
389 if (nesdev->msi_enabled) {
390 /* No need to read the interrupt pending register if msi is enabled */
391 handled = 1;
392 } else {
393 if (unlikely(nesdev->nesadapter->hw_rev == NE020_REV)) {
394 /* Master interrupt enable provides synchronization for kicking off bottom half
395 when interrupt sharing is going on */
396 int_mask = nes_read32(nesdev->regs + NES_INT_MASK);
397 if (int_mask & 0x80000000) {
398 /* Check interrupt status to see if this might be ours */
399 int_stat = nes_read32(nesdev->regs + NES_INT_STAT);
400 int_req = nesdev->int_req;
401 if (int_stat&int_req) {
402 /* if interesting CEQ or AEQ is pending, claim the interrupt */
403 if ((int_stat&int_req) & (~(NES_INT_TIMER|NES_INT_INTF))) {
404 handled = 1;
405 } else {
406 if (((int_stat & int_req) & NES_INT_TIMER) == NES_INT_TIMER) {
407 /* Timer might be running but might be for another function */
408 timer_stat = nes_read32(nesdev->regs + NES_TIMER_STAT);
409 if ((timer_stat & nesdev->timer_int_req) != 0) {
410 handled = 1;
413 if ((((int_stat & int_req) & NES_INT_INTF) == NES_INT_INTF) &&
414 (handled == 0)) {
415 intf_int_stat = nes_read32(nesdev->regs+NES_INTF_INT_STAT);
416 if ((intf_int_stat & nesdev->intf_int_req) != 0) {
417 handled = 1;
421 if (handled) {
422 nes_write32(nesdev->regs+NES_INT_MASK, int_mask & (~0x80000000));
423 int_mask = nes_read32(nesdev->regs+NES_INT_MASK);
424 /* Save off the status to save an additional read */
425 nesdev->int_stat = int_stat;
426 nesdev->napi_isr_ran = 1;
430 } else {
431 handled = nes_read32(nesdev->regs+NES_INT_PENDING);
435 if (handled) {
437 if (nes_napi_isr(nesdev) == 0) {
438 tasklet_schedule(&nesdev->dpc_tasklet);
441 return IRQ_HANDLED;
442 } else {
443 return IRQ_NONE;
449 * nes_probe - Device initialization
451 static int nes_probe(struct pci_dev *pcidev, const struct pci_device_id *ent)
453 struct net_device *netdev = NULL;
454 struct nes_device *nesdev = NULL;
455 int ret = 0;
456 void __iomem *mmio_regs = NULL;
457 u8 hw_rev;
459 assert(pcidev != NULL);
460 assert(ent != NULL);
462 printk(KERN_INFO PFX "NetEffect RNIC driver v%s loading. (%s)\n",
463 DRV_VERSION, pci_name(pcidev));
465 ret = pci_enable_device(pcidev);
466 if (ret) {
467 printk(KERN_ERR PFX "Unable to enable PCI device. (%s)\n", pci_name(pcidev));
468 goto bail0;
471 nes_debug(NES_DBG_INIT, "BAR0 (@0x%08lX) size = 0x%lX bytes\n",
472 (long unsigned int)pci_resource_start(pcidev, BAR_0),
473 (long unsigned int)pci_resource_len(pcidev, BAR_0));
474 nes_debug(NES_DBG_INIT, "BAR1 (@0x%08lX) size = 0x%lX bytes\n",
475 (long unsigned int)pci_resource_start(pcidev, BAR_1),
476 (long unsigned int)pci_resource_len(pcidev, BAR_1));
478 /* Make sure PCI base addr are MMIO */
479 if (!(pci_resource_flags(pcidev, BAR_0) & IORESOURCE_MEM) ||
480 !(pci_resource_flags(pcidev, BAR_1) & IORESOURCE_MEM)) {
481 printk(KERN_ERR PFX "PCI regions not an MMIO resource\n");
482 ret = -ENODEV;
483 goto bail1;
486 /* Reserve PCI I/O and memory resources */
487 ret = pci_request_regions(pcidev, DRV_NAME);
488 if (ret) {
489 printk(KERN_ERR PFX "Unable to request regions. (%s)\n", pci_name(pcidev));
490 goto bail1;
493 if ((sizeof(dma_addr_t) > 4)) {
494 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(64));
495 if (ret < 0) {
496 printk(KERN_ERR PFX "64b DMA mask configuration failed\n");
497 goto bail2;
499 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64));
500 if (ret) {
501 printk(KERN_ERR PFX "64b DMA consistent mask configuration failed\n");
502 goto bail2;
504 } else {
505 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32));
506 if (ret < 0) {
507 printk(KERN_ERR PFX "32b DMA mask configuration failed\n");
508 goto bail2;
510 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
511 if (ret) {
512 printk(KERN_ERR PFX "32b DMA consistent mask configuration failed\n");
513 goto bail2;
517 pci_set_master(pcidev);
519 /* Allocate hardware structure */
520 nesdev = kzalloc(sizeof(struct nes_device), GFP_KERNEL);
521 if (!nesdev) {
522 ret = -ENOMEM;
523 goto bail2;
526 nes_debug(NES_DBG_INIT, "Allocated nes device at %p\n", nesdev);
527 nesdev->pcidev = pcidev;
528 pci_set_drvdata(pcidev, nesdev);
530 pci_read_config_byte(pcidev, 0x0008, &hw_rev);
531 nes_debug(NES_DBG_INIT, "hw_rev=%u\n", hw_rev);
533 spin_lock_init(&nesdev->indexed_regs_lock);
535 /* Remap the PCI registers in adapter BAR0 to kernel VA space */
536 mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0),
537 pci_resource_len(pcidev, BAR_0));
538 if (mmio_regs == NULL) {
539 printk(KERN_ERR PFX "Unable to remap BAR0\n");
540 ret = -EIO;
541 goto bail3;
543 nesdev->regs = mmio_regs;
544 nesdev->index_reg = 0x50 + (PCI_FUNC(pcidev->devfn)*8) + mmio_regs;
546 /* Ensure interrupts are disabled */
547 nes_write32(nesdev->regs+NES_INT_MASK, 0x7fffffff);
549 if (nes_drv_opt & NES_DRV_OPT_ENABLE_MSI) {
550 if (!pci_enable_msi(nesdev->pcidev)) {
551 nesdev->msi_enabled = 1;
552 nes_debug(NES_DBG_INIT, "MSI is enabled for device %s\n",
553 pci_name(pcidev));
554 } else {
555 nes_debug(NES_DBG_INIT, "MSI is disabled by linux for device %s\n",
556 pci_name(pcidev));
558 } else {
559 nes_debug(NES_DBG_INIT, "MSI not requested due to driver options for device %s\n",
560 pci_name(pcidev));
563 nesdev->csr_start = pci_resource_start(nesdev->pcidev, BAR_0);
564 nesdev->doorbell_region = pci_resource_start(nesdev->pcidev, BAR_1);
566 /* Init the adapter */
567 nesdev->nesadapter = nes_init_adapter(nesdev, hw_rev);
568 if (!nesdev->nesadapter) {
569 printk(KERN_ERR PFX "Unable to initialize adapter.\n");
570 ret = -ENOMEM;
571 goto bail5;
573 nesdev->nesadapter->et_rx_coalesce_usecs_irq = interrupt_mod_interval;
574 nesdev->nesadapter->wqm_quanta = wqm_quanta;
576 /* nesdev->base_doorbell_index =
577 nesdev->nesadapter->pd_config_base[PCI_FUNC(nesdev->pcidev->devfn)]; */
578 nesdev->base_doorbell_index = 1;
579 nesdev->doorbell_start = nesdev->nesadapter->doorbell_start;
580 if (nesdev->nesadapter->phy_type[0] == NES_PHY_TYPE_PUMA_1G) {
581 switch (PCI_FUNC(nesdev->pcidev->devfn) %
582 nesdev->nesadapter->port_count) {
583 case 1:
584 nesdev->mac_index = 2;
585 break;
586 case 2:
587 nesdev->mac_index = 1;
588 break;
589 case 3:
590 nesdev->mac_index = 3;
591 break;
592 case 0:
593 default:
594 nesdev->mac_index = 0;
596 } else {
597 nesdev->mac_index = PCI_FUNC(nesdev->pcidev->devfn) %
598 nesdev->nesadapter->port_count;
601 if ((limit_maxrdreqsz ||
602 ((nesdev->nesadapter->phy_type[0] == NES_PHY_TYPE_GLADIUS) &&
603 (hw_rev == NE020_REV1))) &&
604 (pcie_get_readrq(pcidev) > 256)) {
605 if (pcie_set_readrq(pcidev, 256))
606 printk(KERN_ERR PFX "Unable to set max read request"
607 " to 256 bytes\n");
608 else
609 nes_debug(NES_DBG_INIT, "Max read request size set"
610 " to 256 bytes\n");
613 tasklet_init(&nesdev->dpc_tasklet, nes_dpc, (unsigned long)nesdev);
615 /* bring up the Control QP */
616 if (nes_init_cqp(nesdev)) {
617 ret = -ENODEV;
618 goto bail6;
621 /* Arm the CCQ */
622 nes_write32(nesdev->regs+NES_CQE_ALLOC, NES_CQE_ALLOC_NOTIFY_NEXT |
623 PCI_FUNC(nesdev->pcidev->devfn));
624 nes_read32(nesdev->regs+NES_CQE_ALLOC);
626 /* Enable the interrupts */
627 nesdev->int_req = (0x101 << PCI_FUNC(nesdev->pcidev->devfn)) |
628 (1 << (PCI_FUNC(nesdev->pcidev->devfn)+16));
629 if (PCI_FUNC(nesdev->pcidev->devfn) < 4) {
630 nesdev->int_req |= (1 << (PCI_FUNC(nesdev->mac_index)+24));
633 /* TODO: This really should be the first driver to load, not function 0 */
634 if (PCI_FUNC(nesdev->pcidev->devfn) == 0) {
635 /* pick up PCI and critical errors if the first driver to load */
636 nesdev->intf_int_req = NES_INTF_INT_PCIERR | NES_INTF_INT_CRITERR;
637 nesdev->int_req |= NES_INT_INTF;
638 } else {
639 nesdev->intf_int_req = 0;
641 nesdev->intf_int_req |= (1 << (PCI_FUNC(nesdev->pcidev->devfn)+16));
642 nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS0, 0);
643 nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS1, 0);
644 nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS2, 0x00001265);
645 nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS4, 0x18021804);
647 nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS3, 0x17801790);
649 /* deal with both periodic and one_shot */
650 nesdev->timer_int_req = 0x101 << PCI_FUNC(nesdev->pcidev->devfn);
651 nesdev->nesadapter->timer_int_req |= nesdev->timer_int_req;
652 nes_debug(NES_DBG_INIT, "setting int_req for function %u, nesdev = 0x%04X, adapter = 0x%04X\n",
653 PCI_FUNC(nesdev->pcidev->devfn),
654 nesdev->timer_int_req, nesdev->nesadapter->timer_int_req);
656 nes_write32(nesdev->regs+NES_INTF_INT_MASK, ~(nesdev->intf_int_req));
658 list_add_tail(&nesdev->list, &nes_dev_list);
660 /* Request an interrupt line for the driver */
661 ret = request_irq(pcidev->irq, nes_interrupt, IRQF_SHARED, DRV_NAME, nesdev);
662 if (ret) {
663 printk(KERN_ERR PFX "%s: requested IRQ %u is busy\n",
664 pci_name(pcidev), pcidev->irq);
665 goto bail65;
668 nes_write32(nesdev->regs+NES_INT_MASK, ~nesdev->int_req);
670 if (nes_notifiers_registered == 0) {
671 register_inetaddr_notifier(&nes_inetaddr_notifier);
672 register_netevent_notifier(&nes_net_notifier);
674 nes_notifiers_registered++;
676 INIT_DELAYED_WORK(&nesdev->work, nes_recheck_link_status);
678 /* Initialize network devices */
679 netdev = nes_netdev_init(nesdev, mmio_regs);
680 if (netdev == NULL) {
681 ret = -ENOMEM;
682 goto bail7;
685 /* Register network device */
686 ret = register_netdev(netdev);
687 if (ret) {
688 printk(KERN_ERR PFX "Unable to register netdev, ret = %d\n", ret);
689 nes_netdev_destroy(netdev);
690 goto bail7;
693 nes_print_macaddr(netdev);
695 nesdev->netdev_count++;
696 nesdev->nesadapter->netdev_count++;
698 printk(KERN_INFO PFX "%s: NetEffect RNIC driver successfully loaded.\n",
699 pci_name(pcidev));
700 return 0;
702 bail7:
703 printk(KERN_ERR PFX "bail7\n");
704 while (nesdev->netdev_count > 0) {
705 nesdev->netdev_count--;
706 nesdev->nesadapter->netdev_count--;
708 unregister_netdev(nesdev->netdev[nesdev->netdev_count]);
709 nes_netdev_destroy(nesdev->netdev[nesdev->netdev_count]);
712 nes_debug(NES_DBG_INIT, "netdev_count=%d, nesadapter->netdev_count=%d\n",
713 nesdev->netdev_count, nesdev->nesadapter->netdev_count);
715 nes_notifiers_registered--;
716 if (nes_notifiers_registered == 0) {
717 unregister_netevent_notifier(&nes_net_notifier);
718 unregister_inetaddr_notifier(&nes_inetaddr_notifier);
721 list_del(&nesdev->list);
722 nes_destroy_cqp(nesdev);
724 bail65:
725 printk(KERN_ERR PFX "bail65\n");
726 free_irq(pcidev->irq, nesdev);
727 if (nesdev->msi_enabled) {
728 pci_disable_msi(pcidev);
730 bail6:
731 printk(KERN_ERR PFX "bail6\n");
732 tasklet_kill(&nesdev->dpc_tasklet);
733 /* Deallocate the Adapter Structure */
734 nes_destroy_adapter(nesdev->nesadapter);
736 bail5:
737 printk(KERN_ERR PFX "bail5\n");
738 iounmap(nesdev->regs);
740 bail3:
741 printk(KERN_ERR PFX "bail3\n");
742 kfree(nesdev);
744 bail2:
745 pci_release_regions(pcidev);
747 bail1:
748 pci_disable_device(pcidev);
750 bail0:
751 return ret;
756 * nes_remove - unload from kernel
758 static void nes_remove(struct pci_dev *pcidev)
760 struct nes_device *nesdev = pci_get_drvdata(pcidev);
761 struct net_device *netdev;
762 int netdev_index = 0;
763 unsigned long flags;
765 if (nesdev->netdev_count) {
766 netdev = nesdev->netdev[netdev_index];
767 if (netdev) {
768 netif_stop_queue(netdev);
769 unregister_netdev(netdev);
770 nes_netdev_destroy(netdev);
772 nesdev->netdev[netdev_index] = NULL;
773 nesdev->netdev_count--;
774 nesdev->nesadapter->netdev_count--;
778 nes_notifiers_registered--;
779 if (nes_notifiers_registered == 0) {
780 unregister_netevent_notifier(&nes_net_notifier);
781 unregister_inetaddr_notifier(&nes_inetaddr_notifier);
784 list_del(&nesdev->list);
785 nes_destroy_cqp(nesdev);
787 free_irq(pcidev->irq, nesdev);
788 tasklet_kill(&nesdev->dpc_tasklet);
790 spin_lock_irqsave(&nesdev->nesadapter->phy_lock, flags);
791 if (nesdev->link_recheck) {
792 spin_unlock_irqrestore(&nesdev->nesadapter->phy_lock, flags);
793 cancel_delayed_work_sync(&nesdev->work);
794 } else {
795 spin_unlock_irqrestore(&nesdev->nesadapter->phy_lock, flags);
798 /* Deallocate the Adapter Structure */
799 nes_destroy_adapter(nesdev->nesadapter);
801 if (nesdev->msi_enabled) {
802 pci_disable_msi(pcidev);
805 iounmap(nesdev->regs);
806 kfree(nesdev);
808 /* nes_debug(NES_DBG_SHUTDOWN, "calling pci_release_regions.\n"); */
809 pci_release_regions(pcidev);
810 pci_disable_device(pcidev);
811 pci_set_drvdata(pcidev, NULL);
815 static ssize_t adapter_show(struct device_driver *ddp, char *buf)
817 unsigned int devfn = 0xffffffff;
818 unsigned char bus_number = 0xff;
819 unsigned int i = 0;
820 struct nes_device *nesdev;
822 list_for_each_entry(nesdev, &nes_dev_list, list) {
823 if (i == ee_flsh_adapter) {
824 devfn = nesdev->pcidev->devfn;
825 bus_number = nesdev->pcidev->bus->number;
826 break;
828 i++;
831 return snprintf(buf, PAGE_SIZE, "%x:%x\n", bus_number, devfn);
834 static ssize_t adapter_store(struct device_driver *ddp,
835 const char *buf, size_t count)
837 char *p = (char *)buf;
839 ee_flsh_adapter = simple_strtoul(p, &p, 10);
840 return strnlen(buf, count);
843 static ssize_t eeprom_cmd_show(struct device_driver *ddp, char *buf)
845 u32 eeprom_cmd = 0xdead;
846 u32 i = 0;
847 struct nes_device *nesdev;
849 list_for_each_entry(nesdev, &nes_dev_list, list) {
850 if (i == ee_flsh_adapter) {
851 eeprom_cmd = nes_read32(nesdev->regs + NES_EEPROM_COMMAND);
852 break;
854 i++;
856 return snprintf(buf, PAGE_SIZE, "0x%x\n", eeprom_cmd);
859 static ssize_t eeprom_cmd_store(struct device_driver *ddp,
860 const char *buf, size_t count)
862 char *p = (char *)buf;
863 u32 val;
864 u32 i = 0;
865 struct nes_device *nesdev;
867 if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
868 val = simple_strtoul(p, &p, 16);
869 list_for_each_entry(nesdev, &nes_dev_list, list) {
870 if (i == ee_flsh_adapter) {
871 nes_write32(nesdev->regs + NES_EEPROM_COMMAND, val);
872 break;
874 i++;
877 return strnlen(buf, count);
880 static ssize_t eeprom_data_show(struct device_driver *ddp, char *buf)
882 u32 eeprom_data = 0xdead;
883 u32 i = 0;
884 struct nes_device *nesdev;
886 list_for_each_entry(nesdev, &nes_dev_list, list) {
887 if (i == ee_flsh_adapter) {
888 eeprom_data = nes_read32(nesdev->regs + NES_EEPROM_DATA);
889 break;
891 i++;
894 return snprintf(buf, PAGE_SIZE, "0x%x\n", eeprom_data);
897 static ssize_t eeprom_data_store(struct device_driver *ddp,
898 const char *buf, size_t count)
900 char *p = (char *)buf;
901 u32 val;
902 u32 i = 0;
903 struct nes_device *nesdev;
905 if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
906 val = simple_strtoul(p, &p, 16);
907 list_for_each_entry(nesdev, &nes_dev_list, list) {
908 if (i == ee_flsh_adapter) {
909 nes_write32(nesdev->regs + NES_EEPROM_DATA, val);
910 break;
912 i++;
915 return strnlen(buf, count);
918 static ssize_t flash_cmd_show(struct device_driver *ddp, char *buf)
920 u32 flash_cmd = 0xdead;
921 u32 i = 0;
922 struct nes_device *nesdev;
924 list_for_each_entry(nesdev, &nes_dev_list, list) {
925 if (i == ee_flsh_adapter) {
926 flash_cmd = nes_read32(nesdev->regs + NES_FLASH_COMMAND);
927 break;
929 i++;
932 return snprintf(buf, PAGE_SIZE, "0x%x\n", flash_cmd);
935 static ssize_t flash_cmd_store(struct device_driver *ddp,
936 const char *buf, size_t count)
938 char *p = (char *)buf;
939 u32 val;
940 u32 i = 0;
941 struct nes_device *nesdev;
943 if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
944 val = simple_strtoul(p, &p, 16);
945 list_for_each_entry(nesdev, &nes_dev_list, list) {
946 if (i == ee_flsh_adapter) {
947 nes_write32(nesdev->regs + NES_FLASH_COMMAND, val);
948 break;
950 i++;
953 return strnlen(buf, count);
956 static ssize_t flash_data_show(struct device_driver *ddp, char *buf)
958 u32 flash_data = 0xdead;
959 u32 i = 0;
960 struct nes_device *nesdev;
962 list_for_each_entry(nesdev, &nes_dev_list, list) {
963 if (i == ee_flsh_adapter) {
964 flash_data = nes_read32(nesdev->regs + NES_FLASH_DATA);
965 break;
967 i++;
970 return snprintf(buf, PAGE_SIZE, "0x%x\n", flash_data);
973 static ssize_t flash_data_store(struct device_driver *ddp,
974 const char *buf, size_t count)
976 char *p = (char *)buf;
977 u32 val;
978 u32 i = 0;
979 struct nes_device *nesdev;
981 if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
982 val = simple_strtoul(p, &p, 16);
983 list_for_each_entry(nesdev, &nes_dev_list, list) {
984 if (i == ee_flsh_adapter) {
985 nes_write32(nesdev->regs + NES_FLASH_DATA, val);
986 break;
988 i++;
991 return strnlen(buf, count);
994 static ssize_t nonidx_addr_show(struct device_driver *ddp, char *buf)
996 return snprintf(buf, PAGE_SIZE, "0x%x\n", sysfs_nonidx_addr);
999 static ssize_t nonidx_addr_store(struct device_driver *ddp,
1000 const char *buf, size_t count)
1002 char *p = (char *)buf;
1004 if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X')
1005 sysfs_nonidx_addr = simple_strtoul(p, &p, 16);
1007 return strnlen(buf, count);
1010 static ssize_t nonidx_data_show(struct device_driver *ddp, char *buf)
1012 u32 nonidx_data = 0xdead;
1013 u32 i = 0;
1014 struct nes_device *nesdev;
1016 list_for_each_entry(nesdev, &nes_dev_list, list) {
1017 if (i == ee_flsh_adapter) {
1018 nonidx_data = nes_read32(nesdev->regs + sysfs_nonidx_addr);
1019 break;
1021 i++;
1024 return snprintf(buf, PAGE_SIZE, "0x%x\n", nonidx_data);
1027 static ssize_t nonidx_data_store(struct device_driver *ddp,
1028 const char *buf, size_t count)
1030 char *p = (char *)buf;
1031 u32 val;
1032 u32 i = 0;
1033 struct nes_device *nesdev;
1035 if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
1036 val = simple_strtoul(p, &p, 16);
1037 list_for_each_entry(nesdev, &nes_dev_list, list) {
1038 if (i == ee_flsh_adapter) {
1039 nes_write32(nesdev->regs + sysfs_nonidx_addr, val);
1040 break;
1042 i++;
1045 return strnlen(buf, count);
1048 static ssize_t idx_addr_show(struct device_driver *ddp, char *buf)
1050 return snprintf(buf, PAGE_SIZE, "0x%x\n", sysfs_idx_addr);
1053 static ssize_t idx_addr_store(struct device_driver *ddp,
1054 const char *buf, size_t count)
1056 char *p = (char *)buf;
1058 if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X')
1059 sysfs_idx_addr = simple_strtoul(p, &p, 16);
1061 return strnlen(buf, count);
1064 static ssize_t idx_data_show(struct device_driver *ddp, char *buf)
1066 u32 idx_data = 0xdead;
1067 u32 i = 0;
1068 struct nes_device *nesdev;
1070 list_for_each_entry(nesdev, &nes_dev_list, list) {
1071 if (i == ee_flsh_adapter) {
1072 idx_data = nes_read_indexed(nesdev, sysfs_idx_addr);
1073 break;
1075 i++;
1078 return snprintf(buf, PAGE_SIZE, "0x%x\n", idx_data);
1081 static ssize_t idx_data_store(struct device_driver *ddp,
1082 const char *buf, size_t count)
1084 char *p = (char *)buf;
1085 u32 val;
1086 u32 i = 0;
1087 struct nes_device *nesdev;
1089 if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
1090 val = simple_strtoul(p, &p, 16);
1091 list_for_each_entry(nesdev, &nes_dev_list, list) {
1092 if (i == ee_flsh_adapter) {
1093 nes_write_indexed(nesdev, sysfs_idx_addr, val);
1094 break;
1096 i++;
1099 return strnlen(buf, count);
1102 static ssize_t wqm_quanta_show(struct device_driver *ddp, char *buf)
1104 u32 wqm_quanta_value = 0xdead;
1105 u32 i = 0;
1106 struct nes_device *nesdev;
1108 list_for_each_entry(nesdev, &nes_dev_list, list) {
1109 if (i == ee_flsh_adapter) {
1110 wqm_quanta_value = nesdev->nesadapter->wqm_quanta;
1111 break;
1113 i++;
1116 return snprintf(buf, PAGE_SIZE, "0x%X\n", wqm_quanta_value);
1119 static ssize_t wqm_quanta_store(struct device_driver *ddp, const char *buf,
1120 size_t count)
1122 unsigned long wqm_quanta_value;
1123 u32 wqm_config1;
1124 u32 i = 0;
1125 struct nes_device *nesdev;
1127 if (kstrtoul(buf, 0, &wqm_quanta_value) < 0)
1128 return -EINVAL;
1130 list_for_each_entry(nesdev, &nes_dev_list, list) {
1131 if (i == ee_flsh_adapter) {
1132 nesdev->nesadapter->wqm_quanta = wqm_quanta_value;
1133 wqm_config1 = nes_read_indexed(nesdev,
1134 NES_IDX_WQM_CONFIG1);
1135 nes_write_indexed(nesdev, NES_IDX_WQM_CONFIG1,
1136 ((wqm_quanta_value << 1) |
1137 (wqm_config1 & 0x00000001)));
1138 break;
1140 i++;
1142 return strnlen(buf, count);
1145 static DRIVER_ATTR_RW(adapter);
1146 static DRIVER_ATTR_RW(eeprom_cmd);
1147 static DRIVER_ATTR_RW(eeprom_data);
1148 static DRIVER_ATTR_RW(flash_cmd);
1149 static DRIVER_ATTR_RW(flash_data);
1150 static DRIVER_ATTR_RW(nonidx_addr);
1151 static DRIVER_ATTR_RW(nonidx_data);
1152 static DRIVER_ATTR_RW(idx_addr);
1153 static DRIVER_ATTR_RW(idx_data);
1154 static DRIVER_ATTR_RW(wqm_quanta);
1156 static struct attribute *nes_attrs[] = {
1157 &driver_attr_adapter.attr,
1158 &driver_attr_eeprom_cmd.attr,
1159 &driver_attr_eeprom_data.attr,
1160 &driver_attr_flash_cmd.attr,
1161 &driver_attr_flash_data.attr,
1162 &driver_attr_nonidx_addr.attr,
1163 &driver_attr_nonidx_data.attr,
1164 &driver_attr_idx_addr.attr,
1165 &driver_attr_idx_data.attr,
1166 &driver_attr_wqm_quanta.attr,
1167 NULL,
1169 ATTRIBUTE_GROUPS(nes);
1171 static struct pci_driver nes_pci_driver = {
1172 .name = DRV_NAME,
1173 .id_table = nes_pci_table,
1174 .probe = nes_probe,
1175 .remove = nes_remove,
1176 .groups = nes_groups,
1181 * nes_init_module - module initialization entry point
1183 static int __init nes_init_module(void)
1185 int retval;
1187 retval = nes_cm_start();
1188 if (retval) {
1189 printk(KERN_ERR PFX "Unable to start NetEffect iWARP CM.\n");
1190 return retval;
1192 return pci_register_driver(&nes_pci_driver);
1197 * nes_exit_module - module unload entry point
1199 static void __exit nes_exit_module(void)
1201 nes_cm_stop();
1203 pci_unregister_driver(&nes_pci_driver);
1207 module_init(nes_init_module);
1208 module_exit(nes_exit_module);