]> err.no Git - linux-2.6/blob - drivers/infiniband/hw/nes/nes.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6] / drivers / infiniband / hw / nes / nes.c
1 /*
2  * Copyright (c) 2006 - 2008 NetEffect, Inc. 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 <asm/io.h>
48 #include <asm/irq.h>
49 #include <asm/byteorder.h>
50 #include <rdma/ib_smi.h>
51 #include <rdma/ib_verbs.h>
52 #include <rdma/ib_pack.h>
53 #include <rdma/iw_cm.h>
54
55 #include "nes.h"
56
57 #include <net/netevent.h>
58 #include <net/neighbour.h>
59 #include <linux/route.h>
60 #include <net/ip_fib.h>
61
62 MODULE_AUTHOR("NetEffect");
63 MODULE_DESCRIPTION("NetEffect RNIC Low-level iWARP Driver");
64 MODULE_LICENSE("Dual BSD/GPL");
65 MODULE_VERSION(DRV_VERSION);
66
67 int max_mtu = 9000;
68 int interrupt_mod_interval = 0;
69
70
71 /* Interoperability */
72 int mpa_version = 1;
73 module_param(mpa_version, int, 0);
74 MODULE_PARM_DESC(mpa_version, "MPA version to be used int MPA Req/Resp (0 or 1)");
75
76 /* Interoperability */
77 int disable_mpa_crc = 0;
78 module_param(disable_mpa_crc, int, 0);
79 MODULE_PARM_DESC(disable_mpa_crc, "Disable checking of MPA CRC");
80
81 unsigned int send_first = 0;
82 module_param(send_first, int, 0);
83 MODULE_PARM_DESC(send_first, "Send RDMA Message First on Active Connection");
84
85
86 unsigned int nes_drv_opt = 0;
87 module_param(nes_drv_opt, int, 0);
88 MODULE_PARM_DESC(nes_drv_opt, "Driver option parameters");
89
90 unsigned int nes_debug_level = 0;
91 module_param_named(debug_level, nes_debug_level, uint, 0644);
92 MODULE_PARM_DESC(debug_level, "Enable debug output level");
93
94 LIST_HEAD(nes_adapter_list);
95 static LIST_HEAD(nes_dev_list);
96
97 atomic_t qps_destroyed;
98
99 static unsigned int ee_flsh_adapter;
100 static unsigned int sysfs_nonidx_addr;
101 static unsigned int sysfs_idx_addr;
102
103 static struct pci_device_id nes_pci_table[] = {
104         {PCI_VENDOR_ID_NETEFFECT, PCI_DEVICE_ID_NETEFFECT_NE020, PCI_ANY_ID, PCI_ANY_ID},
105         {0}
106 };
107
108 MODULE_DEVICE_TABLE(pci, nes_pci_table);
109
110 static int nes_inetaddr_event(struct notifier_block *, unsigned long, void *);
111 static int nes_net_event(struct notifier_block *, unsigned long, void *);
112 static int nes_notifiers_registered;
113
114
115 static struct notifier_block nes_inetaddr_notifier = {
116         .notifier_call = nes_inetaddr_event
117 };
118
119 static struct notifier_block nes_net_notifier = {
120         .notifier_call = nes_net_event
121 };
122
123
124
125
126 /**
127  * nes_inetaddr_event
128  */
129 static int nes_inetaddr_event(struct notifier_block *notifier,
130                 unsigned long event, void *ptr)
131 {
132         struct in_ifaddr *ifa = ptr;
133         struct net_device *event_netdev = ifa->ifa_dev->dev;
134         struct nes_device *nesdev;
135         struct net_device *netdev;
136         struct nes_vnic *nesvnic;
137         unsigned int addr;
138         unsigned int mask;
139
140         addr = ntohl(ifa->ifa_address);
141         mask = ntohl(ifa->ifa_mask);
142         nes_debug(NES_DBG_NETDEV, "nes_inetaddr_event: ip address " NIPQUAD_FMT
143                   ", netmask " NIPQUAD_FMT ".\n",
144                   HIPQUAD(addr), HIPQUAD(mask));
145         list_for_each_entry(nesdev, &nes_dev_list, list) {
146                 nes_debug(NES_DBG_NETDEV, "Nesdev list entry = 0x%p. (%s)\n",
147                                 nesdev, nesdev->netdev[0]->name);
148                 netdev = nesdev->netdev[0];
149                 nesvnic = netdev_priv(netdev);
150                 if (netdev == event_netdev) {
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                                         return NOTIFY_OK;
168                                         break;
169                                 case NETDEV_UP:
170                                         nes_debug(NES_DBG_NETDEV, "event:UP\n");
171
172                                         if (nesvnic->local_ipaddr != 0) {
173                                                 nes_debug(NES_DBG_NETDEV, "Interface already has local_ipaddr\n");
174                                                 return NOTIFY_OK;
175                                         }
176                                         /* Add the address to the IP table */
177                                         nesvnic->local_ipaddr = ifa->ifa_address;
178
179                                         nes_write_indexed(nesdev,
180                                                         NES_IDX_DST_IP_ADDR+(0x10*PCI_FUNC(nesdev->pcidev->devfn)),
181                                                         ntohl(ifa->ifa_address));
182                                         nes_manage_arp_cache(netdev, netdev->dev_addr,
183                                                         ntohl(nesvnic->local_ipaddr), NES_ARP_ADD);
184                                         return NOTIFY_OK;
185                                         break;
186                                 default:
187                                         break;
188                         }
189                 }
190         }
191
192         return NOTIFY_DONE;
193 }
194
195
196 /**
197  * nes_net_event
198  */
199 static int nes_net_event(struct notifier_block *notifier,
200                 unsigned long event, void *ptr)
201 {
202         struct neighbour *neigh = ptr;
203         struct nes_device *nesdev;
204         struct net_device *netdev;
205         struct nes_vnic *nesvnic;
206
207         switch (event) {
208                 case NETEVENT_NEIGH_UPDATE:
209                         list_for_each_entry(nesdev, &nes_dev_list, list) {
210                                 /* nes_debug(NES_DBG_NETDEV, "Nesdev list entry = 0x%p.\n", nesdev); */
211                                 netdev = nesdev->netdev[0];
212                                 nesvnic = netdev_priv(netdev);
213                                 if (netdev == neigh->dev) {
214                                         if (nesvnic->rdma_enabled == 0) {
215                                                 nes_debug(NES_DBG_NETDEV, "Skipping device %s since no RDMA\n",
216                                                                 netdev->name);
217                                         } else {
218                                                 if (neigh->nud_state & NUD_VALID) {
219                                                         nes_manage_arp_cache(neigh->dev, neigh->ha,
220                                                                         ntohl(*(__be32 *)neigh->primary_key), NES_ARP_ADD);
221                                                 } else {
222                                                         nes_manage_arp_cache(neigh->dev, neigh->ha,
223                                                                         ntohl(*(__be32 *)neigh->primary_key), NES_ARP_DELETE);
224                                                 }
225                                         }
226                                         return NOTIFY_OK;
227                                 }
228                         }
229                         break;
230                 default:
231                         nes_debug(NES_DBG_NETDEV, "NETEVENT_ %lu undefined\n", event);
232                         break;
233         }
234
235         return NOTIFY_DONE;
236 }
237
238
239 /**
240  * nes_add_ref
241  */
242 void nes_add_ref(struct ib_qp *ibqp)
243 {
244         struct nes_qp *nesqp;
245
246         nesqp = to_nesqp(ibqp);
247         nes_debug(NES_DBG_QP, "Bumping refcount for QP%u.  Pre-inc value = %u\n",
248                         ibqp->qp_num, atomic_read(&nesqp->refcount));
249         atomic_inc(&nesqp->refcount);
250 }
251
252 static void nes_cqp_rem_ref_callback(struct nes_device *nesdev, struct nes_cqp_request *cqp_request)
253 {
254         unsigned long flags;
255         struct nes_qp *nesqp = cqp_request->cqp_callback_pointer;
256         struct nes_adapter *nesadapter = nesdev->nesadapter;
257         u32 qp_id;
258
259         atomic_inc(&qps_destroyed);
260
261         /* Free the control structures */
262
263         qp_id = nesqp->hwqp.qp_id;
264         if (nesqp->pbl_vbase) {
265                 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
266                                 nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
267                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
268                 nesadapter->free_256pbl++;
269                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
270                 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
271                 nesqp->pbl_vbase = NULL;
272
273         } else {
274                 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
275                                 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
276         }
277         nes_free_resource(nesadapter, nesadapter->allocated_qps, nesqp->hwqp.qp_id);
278
279         kfree(nesqp->allocated_buffer);
280
281 }
282
283 /**
284  * nes_rem_ref
285  */
286 void nes_rem_ref(struct ib_qp *ibqp)
287 {
288         u64 u64temp;
289         struct nes_qp *nesqp;
290         struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
291         struct nes_device *nesdev = nesvnic->nesdev;
292         struct nes_adapter *nesadapter = nesdev->nesadapter;
293         struct nes_hw_cqp_wqe *cqp_wqe;
294         struct nes_cqp_request *cqp_request;
295         u32 opcode;
296
297         nesqp = to_nesqp(ibqp);
298
299         if (atomic_read(&nesqp->refcount) == 0) {
300                 printk(KERN_INFO PFX "%s: Reference count already 0 for QP%d, last aeq = 0x%04X.\n",
301                                 __func__, ibqp->qp_num, nesqp->last_aeq);
302                 BUG();
303         }
304
305         if (atomic_dec_and_test(&nesqp->refcount)) {
306                 nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = NULL;
307
308                 /* Destroy the QP */
309                 cqp_request = nes_get_cqp_request(nesdev);
310                 if (cqp_request == NULL) {
311                         nes_debug(NES_DBG_QP, "Failed to get a cqp_request.\n");
312                         return;
313                 }
314                 cqp_request->waiting = 0;
315                 cqp_request->callback = 1;
316                 cqp_request->cqp_callback = nes_cqp_rem_ref_callback;
317                 cqp_request->cqp_callback_pointer = nesqp;
318                 cqp_wqe = &cqp_request->cqp_wqe;
319
320                 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
321                 opcode = NES_CQP_DESTROY_QP | NES_CQP_QP_TYPE_IWARP;
322
323                 if (nesqp->hte_added) {
324                         opcode  |= NES_CQP_QP_DEL_HTE;
325                         nesqp->hte_added = 0;
326                 }
327                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
328                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
329                 u64temp = (u64)nesqp->nesqp_context_pbase;
330                 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
331                 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
332         }
333 }
334
335
336 /**
337  * nes_get_qp
338  */
339 struct ib_qp *nes_get_qp(struct ib_device *device, int qpn)
340 {
341         struct nes_vnic *nesvnic = to_nesvnic(device);
342         struct nes_device *nesdev = nesvnic->nesdev;
343         struct nes_adapter *nesadapter = nesdev->nesadapter;
344
345         if ((qpn < NES_FIRST_QPN) || (qpn >= (NES_FIRST_QPN + nesadapter->max_qp)))
346                 return NULL;
347
348         return &nesadapter->qp_table[qpn - NES_FIRST_QPN]->ibqp;
349 }
350
351
352 /**
353  * nes_print_macaddr
354  */
355 static void nes_print_macaddr(struct net_device *netdev)
356 {
357         DECLARE_MAC_BUF(mac);
358
359         nes_debug(NES_DBG_INIT, "%s: %s, IRQ %u\n",
360                   netdev->name, print_mac(mac, netdev->dev_addr), netdev->irq);
361 }
362
363 /**
364  * nes_interrupt - handle interrupts
365  */
366 static irqreturn_t nes_interrupt(int irq, void *dev_id)
367 {
368         struct nes_device *nesdev = (struct nes_device *)dev_id;
369         int handled = 0;
370         u32 int_mask;
371         u32 int_req;
372         u32 int_stat;
373         u32 intf_int_stat;
374         u32 timer_stat;
375
376         if (nesdev->msi_enabled) {
377                 /* No need to read the interrupt pending register if msi is enabled */
378                 handled = 1;
379         } else {
380                 if (unlikely(nesdev->nesadapter->hw_rev == NE020_REV)) {
381                         /* Master interrupt enable provides synchronization for kicking off bottom half
382                           when interrupt sharing is going on */
383                         int_mask = nes_read32(nesdev->regs + NES_INT_MASK);
384                         if (int_mask & 0x80000000) {
385                                 /* Check interrupt status to see if this might be ours */
386                                 int_stat = nes_read32(nesdev->regs + NES_INT_STAT);
387                                 int_req = nesdev->int_req;
388                                 if (int_stat&int_req) {
389                                         /* if interesting CEQ or AEQ is pending, claim the interrupt */
390                                         if ((int_stat&int_req) & (~(NES_INT_TIMER|NES_INT_INTF))) {
391                                                 handled = 1;
392                                         } else {
393                                                 if (((int_stat & int_req) & NES_INT_TIMER) == NES_INT_TIMER) {
394                                                         /* Timer might be running but might be for another function */
395                                                         timer_stat = nes_read32(nesdev->regs + NES_TIMER_STAT);
396                                                         if ((timer_stat & nesdev->timer_int_req) != 0) {
397                                                                 handled = 1;
398                                                         }
399                                                 }
400                                                 if ((((int_stat & int_req) & NES_INT_INTF) == NES_INT_INTF) &&
401                                                                 (handled == 0)) {
402                                                         intf_int_stat = nes_read32(nesdev->regs+NES_INTF_INT_STAT);
403                                                         if ((intf_int_stat & nesdev->intf_int_req) != 0) {
404                                                                 handled = 1;
405                                                         }
406                                                 }
407                                         }
408                                         if (handled) {
409                                                 nes_write32(nesdev->regs+NES_INT_MASK, int_mask & (~0x80000000));
410                                                 int_mask = nes_read32(nesdev->regs+NES_INT_MASK);
411                                                 /* Save off the status to save an additional read */
412                                                 nesdev->int_stat = int_stat;
413                                                 nesdev->napi_isr_ran = 1;
414                                         }
415                                 }
416                         }
417                 } else {
418                         handled = nes_read32(nesdev->regs+NES_INT_PENDING);
419                 }
420         }
421
422         if (handled) {
423
424                 if (nes_napi_isr(nesdev) == 0) {
425                         tasklet_schedule(&nesdev->dpc_tasklet);
426
427                 }
428                 return IRQ_HANDLED;
429         } else {
430                 return IRQ_NONE;
431         }
432 }
433
434
435 /**
436  * nes_probe - Device initialization
437  */
438 static int __devinit nes_probe(struct pci_dev *pcidev, const struct pci_device_id *ent)
439 {
440         struct net_device *netdev = NULL;
441         struct nes_device *nesdev = NULL;
442         int ret = 0;
443         struct nes_vnic *nesvnic = NULL;
444         void __iomem *mmio_regs = NULL;
445         u8 hw_rev;
446
447         assert(pcidev != NULL);
448         assert(ent != NULL);
449
450         printk(KERN_INFO PFX "NetEffect RNIC driver v%s loading. (%s)\n",
451                         DRV_VERSION, pci_name(pcidev));
452
453         ret = pci_enable_device(pcidev);
454         if (ret) {
455                 printk(KERN_ERR PFX "Unable to enable PCI device. (%s)\n", pci_name(pcidev));
456                 goto bail0;
457         }
458
459         nes_debug(NES_DBG_INIT, "BAR0 (@0x%08lX) size = 0x%lX bytes\n",
460                         (long unsigned int)pci_resource_start(pcidev, BAR_0),
461                         (long unsigned int)pci_resource_len(pcidev, BAR_0));
462         nes_debug(NES_DBG_INIT, "BAR1 (@0x%08lX) size = 0x%lX bytes\n",
463                         (long unsigned int)pci_resource_start(pcidev, BAR_1),
464                         (long unsigned int)pci_resource_len(pcidev, BAR_1));
465
466         /* Make sure PCI base addr are MMIO */
467         if (!(pci_resource_flags(pcidev, BAR_0) & IORESOURCE_MEM) ||
468                         !(pci_resource_flags(pcidev, BAR_1) & IORESOURCE_MEM)) {
469                 printk(KERN_ERR PFX "PCI regions not an MMIO resource\n");
470                 ret = -ENODEV;
471                 goto bail1;
472         }
473
474         /* Reserve PCI I/O and memory resources */
475         ret = pci_request_regions(pcidev, DRV_NAME);
476         if (ret) {
477                 printk(KERN_ERR PFX "Unable to request regions. (%s)\n", pci_name(pcidev));
478                 goto bail1;
479         }
480
481         if ((sizeof(dma_addr_t) > 4)) {
482                 ret = pci_set_dma_mask(pcidev, DMA_64BIT_MASK);
483                 if (ret < 0) {
484                         printk(KERN_ERR PFX "64b DMA mask configuration failed\n");
485                         goto bail2;
486                 }
487                 ret = pci_set_consistent_dma_mask(pcidev, DMA_64BIT_MASK);
488                 if (ret) {
489                         printk(KERN_ERR PFX "64b DMA consistent mask configuration failed\n");
490                         goto bail2;
491                 }
492         } else {
493                 ret = pci_set_dma_mask(pcidev, DMA_32BIT_MASK);
494                 if (ret < 0) {
495                         printk(KERN_ERR PFX "32b DMA mask configuration failed\n");
496                         goto bail2;
497                 }
498                 ret = pci_set_consistent_dma_mask(pcidev, DMA_32BIT_MASK);
499                 if (ret) {
500                         printk(KERN_ERR PFX "32b DMA consistent mask configuration failed\n");
501                         goto bail2;
502                 }
503         }
504
505         pci_set_master(pcidev);
506
507         /* Allocate hardware structure */
508         nesdev = kzalloc(sizeof(struct nes_device), GFP_KERNEL);
509         if (!nesdev) {
510                 printk(KERN_ERR PFX "%s: Unable to alloc hardware struct\n", pci_name(pcidev));
511                 ret = -ENOMEM;
512                 goto bail2;
513         }
514
515         nes_debug(NES_DBG_INIT, "Allocated nes device at %p\n", nesdev);
516         nesdev->pcidev = pcidev;
517         pci_set_drvdata(pcidev, nesdev);
518
519         pci_read_config_byte(pcidev, 0x0008, &hw_rev);
520         nes_debug(NES_DBG_INIT, "hw_rev=%u\n", hw_rev);
521
522         spin_lock_init(&nesdev->indexed_regs_lock);
523
524         /* Remap the PCI registers in adapter BAR0 to kernel VA space */
525         mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0), sizeof(mmio_regs));
526         if (mmio_regs == NULL) {
527                 printk(KERN_ERR PFX "Unable to remap BAR0\n");
528                 ret = -EIO;
529                 goto bail3;
530         }
531         nesdev->regs = mmio_regs;
532         nesdev->index_reg = 0x50 + (PCI_FUNC(pcidev->devfn)*8) + mmio_regs;
533
534         /* Ensure interrupts are disabled */
535         nes_write32(nesdev->regs+NES_INT_MASK, 0x7fffffff);
536
537         if (nes_drv_opt & NES_DRV_OPT_ENABLE_MSI) {
538                 if (!pci_enable_msi(nesdev->pcidev)) {
539                         nesdev->msi_enabled = 1;
540                         nes_debug(NES_DBG_INIT, "MSI is enabled for device %s\n",
541                                         pci_name(pcidev));
542                 } else {
543                         nes_debug(NES_DBG_INIT, "MSI is disabled by linux for device %s\n",
544                                         pci_name(pcidev));
545                 }
546         } else {
547                 nes_debug(NES_DBG_INIT, "MSI not requested due to driver options for device %s\n",
548                                 pci_name(pcidev));
549         }
550
551         nesdev->csr_start = pci_resource_start(nesdev->pcidev, BAR_0);
552         nesdev->doorbell_region = pci_resource_start(nesdev->pcidev, BAR_1);
553
554         /* Init the adapter */
555         nesdev->nesadapter = nes_init_adapter(nesdev, hw_rev);
556         if (!nesdev->nesadapter) {
557                 printk(KERN_ERR PFX "Unable to initialize adapter.\n");
558                 ret = -ENOMEM;
559                 goto bail5;
560         }
561         nesdev->nesadapter->et_rx_coalesce_usecs_irq = interrupt_mod_interval;
562
563         /* nesdev->base_doorbell_index =
564                         nesdev->nesadapter->pd_config_base[PCI_FUNC(nesdev->pcidev->devfn)]; */
565         nesdev->base_doorbell_index = 1;
566         nesdev->doorbell_start = nesdev->nesadapter->doorbell_start;
567         nesdev->mac_index = PCI_FUNC(nesdev->pcidev->devfn) % nesdev->nesadapter->port_count;
568
569         tasklet_init(&nesdev->dpc_tasklet, nes_dpc, (unsigned long)nesdev);
570
571         /* bring up the Control QP */
572         if (nes_init_cqp(nesdev)) {
573                 ret = -ENODEV;
574                 goto bail6;
575         }
576
577         /* Arm the CCQ */
578         nes_write32(nesdev->regs+NES_CQE_ALLOC, NES_CQE_ALLOC_NOTIFY_NEXT |
579                         PCI_FUNC(nesdev->pcidev->devfn));
580         nes_read32(nesdev->regs+NES_CQE_ALLOC);
581
582         /* Enable the interrupts */
583         nesdev->int_req = (0x101 << PCI_FUNC(nesdev->pcidev->devfn)) |
584                         (1 << (PCI_FUNC(nesdev->pcidev->devfn)+16));
585         if (PCI_FUNC(nesdev->pcidev->devfn) < 4) {
586                 nesdev->int_req |= (1 << (PCI_FUNC(nesdev->pcidev->devfn)+24));
587         }
588
589         /* TODO: This really should be the first driver to load, not function 0 */
590         if (PCI_FUNC(nesdev->pcidev->devfn) == 0) {
591                 /* pick up PCI and critical errors if the first driver to load */
592                 nesdev->intf_int_req = NES_INTF_INT_PCIERR | NES_INTF_INT_CRITERR;
593                 nesdev->int_req |= NES_INT_INTF;
594         } else {
595                 nesdev->intf_int_req = 0;
596         }
597         nesdev->intf_int_req |= (1 << (PCI_FUNC(nesdev->pcidev->devfn)+16));
598         nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS0, 0);
599         nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS1, 0);
600         nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS2, 0x00001265);
601         nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS4, 0x18021804);
602
603         nes_write_indexed(nesdev, NES_IDX_DEBUG_ERROR_MASKS3, 0x17801790);
604
605         /* deal with both periodic and one_shot */
606         nesdev->timer_int_req = 0x101 << PCI_FUNC(nesdev->pcidev->devfn);
607         nesdev->nesadapter->timer_int_req |= nesdev->timer_int_req;
608         nes_debug(NES_DBG_INIT, "setting int_req for function %u, nesdev = 0x%04X, adapter = 0x%04X\n",
609                         PCI_FUNC(nesdev->pcidev->devfn),
610                         nesdev->timer_int_req, nesdev->nesadapter->timer_int_req);
611
612         nes_write32(nesdev->regs+NES_INTF_INT_MASK, ~(nesdev->intf_int_req));
613
614         list_add_tail(&nesdev->list, &nes_dev_list);
615
616         /* Request an interrupt line for the driver */
617         ret = request_irq(pcidev->irq, nes_interrupt, IRQF_SHARED, DRV_NAME, nesdev);
618         if (ret) {
619                 printk(KERN_ERR PFX "%s: requested IRQ %u is busy\n",
620                                 pci_name(pcidev), pcidev->irq);
621                 goto bail65;
622         }
623
624         nes_write32(nesdev->regs+NES_INT_MASK, ~nesdev->int_req);
625
626         if (nes_notifiers_registered == 0) {
627                 register_inetaddr_notifier(&nes_inetaddr_notifier);
628                 register_netevent_notifier(&nes_net_notifier);
629         }
630         nes_notifiers_registered++;
631
632         /* Initialize network devices */
633                 if ((netdev = nes_netdev_init(nesdev, mmio_regs)) == NULL) {
634                         goto bail7;
635                 }
636
637                 /* Register network device */
638                 ret = register_netdev(netdev);
639                 if (ret) {
640                         printk(KERN_ERR PFX "Unable to register netdev, ret = %d\n", ret);
641                         nes_netdev_destroy(netdev);
642                         goto bail7;
643                 }
644
645                 nes_print_macaddr(netdev);
646                 /* create a CM core for this netdev */
647                 nesvnic = netdev_priv(netdev);
648
649                 nesdev->netdev_count++;
650                 nesdev->nesadapter->netdev_count++;
651
652
653         printk(KERN_ERR PFX "%s: NetEffect RNIC driver successfully loaded.\n",
654                         pci_name(pcidev));
655         return 0;
656
657         bail7:
658         printk(KERN_ERR PFX "bail7\n");
659         while (nesdev->netdev_count > 0) {
660                 nesdev->netdev_count--;
661                 nesdev->nesadapter->netdev_count--;
662
663                 unregister_netdev(nesdev->netdev[nesdev->netdev_count]);
664                 nes_netdev_destroy(nesdev->netdev[nesdev->netdev_count]);
665         }
666
667         nes_debug(NES_DBG_INIT, "netdev_count=%d, nesadapter->netdev_count=%d\n",
668                         nesdev->netdev_count, nesdev->nesadapter->netdev_count);
669
670         nes_notifiers_registered--;
671         if (nes_notifiers_registered == 0) {
672                 unregister_netevent_notifier(&nes_net_notifier);
673                 unregister_inetaddr_notifier(&nes_inetaddr_notifier);
674         }
675
676         list_del(&nesdev->list);
677         nes_destroy_cqp(nesdev);
678
679         bail65:
680         printk(KERN_ERR PFX "bail65\n");
681         free_irq(pcidev->irq, nesdev);
682         if (nesdev->msi_enabled) {
683                 pci_disable_msi(pcidev);
684         }
685         bail6:
686         printk(KERN_ERR PFX "bail6\n");
687         tasklet_kill(&nesdev->dpc_tasklet);
688         /* Deallocate the Adapter Structure */
689         nes_destroy_adapter(nesdev->nesadapter);
690
691         bail5:
692         printk(KERN_ERR PFX "bail5\n");
693         iounmap(nesdev->regs);
694
695         bail3:
696         printk(KERN_ERR PFX "bail3\n");
697         kfree(nesdev);
698
699         bail2:
700         pci_release_regions(pcidev);
701
702         bail1:
703         pci_disable_device(pcidev);
704
705         bail0:
706         return ret;
707 }
708
709
710 /**
711  * nes_remove - unload from kernel
712  */
713 static void __devexit nes_remove(struct pci_dev *pcidev)
714 {
715         struct nes_device *nesdev = pci_get_drvdata(pcidev);
716         struct net_device *netdev;
717         int netdev_index = 0;
718
719                 if (nesdev->netdev_count) {
720                         netdev = nesdev->netdev[netdev_index];
721                         if (netdev) {
722                                 netif_stop_queue(netdev);
723                                 unregister_netdev(netdev);
724                                 nes_netdev_destroy(netdev);
725
726                                 nesdev->netdev[netdev_index] = NULL;
727                                 nesdev->netdev_count--;
728                                 nesdev->nesadapter->netdev_count--;
729                         }
730                 }
731
732         nes_notifiers_registered--;
733         if (nes_notifiers_registered == 0) {
734                 unregister_netevent_notifier(&nes_net_notifier);
735                 unregister_inetaddr_notifier(&nes_inetaddr_notifier);
736         }
737
738         list_del(&nesdev->list);
739         nes_destroy_cqp(nesdev);
740
741         free_irq(pcidev->irq, nesdev);
742         tasklet_kill(&nesdev->dpc_tasklet);
743
744         /* Deallocate the Adapter Structure */
745         nes_destroy_adapter(nesdev->nesadapter);
746
747         if (nesdev->msi_enabled) {
748                 pci_disable_msi(pcidev);
749         }
750
751         iounmap(nesdev->regs);
752         kfree(nesdev);
753
754         /* nes_debug(NES_DBG_SHUTDOWN, "calling pci_release_regions.\n"); */
755         pci_release_regions(pcidev);
756         pci_disable_device(pcidev);
757         pci_set_drvdata(pcidev, NULL);
758 }
759
760
761 static struct pci_driver nes_pci_driver = {
762         .name = DRV_NAME,
763         .id_table = nes_pci_table,
764         .probe = nes_probe,
765         .remove = __devexit_p(nes_remove),
766 };
767
768 static ssize_t nes_show_adapter(struct device_driver *ddp, char *buf)
769 {
770         unsigned int  devfn = 0xffffffff;
771         unsigned char bus_number = 0xff;
772         unsigned int  i = 0;
773         struct nes_device *nesdev;
774
775         list_for_each_entry(nesdev, &nes_dev_list, list) {
776                 if (i == ee_flsh_adapter) {
777                         devfn      = nesdev->nesadapter->devfn;
778                         bus_number = nesdev->nesadapter->bus_number;
779                         break;
780                 }
781                 i++;
782         }
783
784         return snprintf(buf, PAGE_SIZE, "%x:%x", bus_number, devfn);
785 }
786
787 static ssize_t nes_store_adapter(struct device_driver *ddp,
788         const char *buf, size_t count)
789 {
790         char *p = (char *)buf;
791
792         ee_flsh_adapter = simple_strtoul(p, &p, 10);
793         return strnlen(buf, count);
794 }
795
796 static ssize_t nes_show_ee_cmd(struct device_driver *ddp, char *buf)
797 {
798         u32 eeprom_cmd = 0xdead;
799         u32 i = 0;
800         struct nes_device *nesdev;
801
802         list_for_each_entry(nesdev, &nes_dev_list, list) {
803                 if (i == ee_flsh_adapter) {
804                         eeprom_cmd = nes_read32(nesdev->regs + NES_EEPROM_COMMAND);
805                         break;
806                 }
807                 i++;
808         }
809         return snprintf(buf, PAGE_SIZE, "0x%x\n", eeprom_cmd);
810 }
811
812 static ssize_t nes_store_ee_cmd(struct device_driver *ddp,
813         const char *buf, size_t count)
814 {
815         char *p = (char *)buf;
816         u32 val;
817         u32 i = 0;
818         struct nes_device *nesdev;
819
820         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
821                 val = simple_strtoul(p, &p, 16);
822                 list_for_each_entry(nesdev, &nes_dev_list, list) {
823                         if (i == ee_flsh_adapter) {
824                                 nes_write32(nesdev->regs + NES_EEPROM_COMMAND, val);
825                                 break;
826                         }
827                         i++;
828                 }
829         }
830         return strnlen(buf, count);
831 }
832
833 static ssize_t nes_show_ee_data(struct device_driver *ddp, char *buf)
834 {
835         u32 eeprom_data = 0xdead;
836         u32 i = 0;
837         struct nes_device *nesdev;
838
839         list_for_each_entry(nesdev, &nes_dev_list, list) {
840                 if (i == ee_flsh_adapter) {
841                         eeprom_data = nes_read32(nesdev->regs + NES_EEPROM_DATA);
842                         break;
843                 }
844                 i++;
845         }
846
847         return  snprintf(buf, PAGE_SIZE, "0x%x\n", eeprom_data);
848 }
849
850 static ssize_t nes_store_ee_data(struct device_driver *ddp,
851         const char *buf, size_t count)
852 {
853         char *p = (char *)buf;
854         u32 val;
855         u32 i = 0;
856         struct nes_device *nesdev;
857
858         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
859                 val = simple_strtoul(p, &p, 16);
860                 list_for_each_entry(nesdev, &nes_dev_list, list) {
861                         if (i == ee_flsh_adapter) {
862                                 nes_write32(nesdev->regs + NES_EEPROM_DATA, val);
863                                 break;
864                         }
865                         i++;
866                 }
867         }
868         return strnlen(buf, count);
869 }
870
871 static ssize_t nes_show_flash_cmd(struct device_driver *ddp, char *buf)
872 {
873         u32 flash_cmd = 0xdead;
874         u32 i = 0;
875         struct nes_device *nesdev;
876
877         list_for_each_entry(nesdev, &nes_dev_list, list) {
878                 if (i == ee_flsh_adapter) {
879                         flash_cmd = nes_read32(nesdev->regs + NES_FLASH_COMMAND);
880                         break;
881                 }
882                 i++;
883         }
884
885         return  snprintf(buf, PAGE_SIZE, "0x%x\n", flash_cmd);
886 }
887
888 static ssize_t nes_store_flash_cmd(struct device_driver *ddp,
889         const char *buf, size_t count)
890 {
891         char *p = (char *)buf;
892         u32 val;
893         u32 i = 0;
894         struct nes_device *nesdev;
895
896         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
897                 val = simple_strtoul(p, &p, 16);
898                 list_for_each_entry(nesdev, &nes_dev_list, list) {
899                         if (i == ee_flsh_adapter) {
900                                 nes_write32(nesdev->regs + NES_FLASH_COMMAND, val);
901                                 break;
902                         }
903                         i++;
904                 }
905         }
906         return strnlen(buf, count);
907 }
908
909 static ssize_t nes_show_flash_data(struct device_driver *ddp, char *buf)
910 {
911         u32 flash_data = 0xdead;
912         u32 i = 0;
913         struct nes_device *nesdev;
914
915         list_for_each_entry(nesdev, &nes_dev_list, list) {
916                 if (i == ee_flsh_adapter) {
917                         flash_data = nes_read32(nesdev->regs + NES_FLASH_DATA);
918                         break;
919                 }
920                 i++;
921         }
922
923         return  snprintf(buf, PAGE_SIZE, "0x%x\n", flash_data);
924 }
925
926 static ssize_t nes_store_flash_data(struct device_driver *ddp,
927         const char *buf, size_t count)
928 {
929         char *p = (char *)buf;
930         u32 val;
931         u32 i = 0;
932         struct nes_device *nesdev;
933
934         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
935                 val = simple_strtoul(p, &p, 16);
936                 list_for_each_entry(nesdev, &nes_dev_list, list) {
937                         if (i == ee_flsh_adapter) {
938                                 nes_write32(nesdev->regs + NES_FLASH_DATA, val);
939                                 break;
940                         }
941                         i++;
942                 }
943         }
944         return strnlen(buf, count);
945 }
946
947 static ssize_t nes_show_nonidx_addr(struct device_driver *ddp, char *buf)
948 {
949         return  snprintf(buf, PAGE_SIZE, "0x%x\n", sysfs_nonidx_addr);
950 }
951
952 static ssize_t nes_store_nonidx_addr(struct device_driver *ddp,
953         const char *buf, size_t count)
954 {
955         char *p = (char *)buf;
956
957         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X')
958                 sysfs_nonidx_addr = simple_strtoul(p, &p, 16);
959
960         return strnlen(buf, count);
961 }
962
963 static ssize_t nes_show_nonidx_data(struct device_driver *ddp, char *buf)
964 {
965         u32 nonidx_data = 0xdead;
966         u32 i = 0;
967         struct nes_device *nesdev;
968
969         list_for_each_entry(nesdev, &nes_dev_list, list) {
970                 if (i == ee_flsh_adapter) {
971                         nonidx_data = nes_read32(nesdev->regs + sysfs_nonidx_addr);
972                         break;
973                 }
974                 i++;
975         }
976
977         return  snprintf(buf, PAGE_SIZE, "0x%x\n", nonidx_data);
978 }
979
980 static ssize_t nes_store_nonidx_data(struct device_driver *ddp,
981         const char *buf, size_t count)
982 {
983         char *p = (char *)buf;
984         u32 val;
985         u32 i = 0;
986         struct nes_device *nesdev;
987
988         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
989                 val = simple_strtoul(p, &p, 16);
990                 list_for_each_entry(nesdev, &nes_dev_list, list) {
991                         if (i == ee_flsh_adapter) {
992                                 nes_write32(nesdev->regs + sysfs_nonidx_addr, val);
993                                 break;
994                         }
995                         i++;
996                 }
997         }
998         return strnlen(buf, count);
999 }
1000
1001 static ssize_t nes_show_idx_addr(struct device_driver *ddp, char *buf)
1002 {
1003         return  snprintf(buf, PAGE_SIZE, "0x%x\n", sysfs_idx_addr);
1004 }
1005
1006 static ssize_t nes_store_idx_addr(struct device_driver *ddp,
1007         const char *buf, size_t count)
1008 {
1009         char *p = (char *)buf;
1010
1011         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X')
1012                 sysfs_idx_addr = simple_strtoul(p, &p, 16);
1013
1014         return strnlen(buf, count);
1015 }
1016
1017 static ssize_t nes_show_idx_data(struct device_driver *ddp, char *buf)
1018 {
1019         u32 idx_data = 0xdead;
1020         u32 i = 0;
1021         struct nes_device *nesdev;
1022
1023         list_for_each_entry(nesdev, &nes_dev_list, list) {
1024                 if (i == ee_flsh_adapter) {
1025                         idx_data = nes_read_indexed(nesdev, sysfs_idx_addr);
1026                         break;
1027                 }
1028                 i++;
1029         }
1030
1031         return  snprintf(buf, PAGE_SIZE, "0x%x\n", idx_data);
1032 }
1033
1034 static ssize_t nes_store_idx_data(struct device_driver *ddp,
1035         const char *buf, size_t count)
1036 {
1037         char *p = (char *)buf;
1038         u32 val;
1039         u32 i = 0;
1040         struct nes_device *nesdev;
1041
1042         if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
1043                 val = simple_strtoul(p, &p, 16);
1044                 list_for_each_entry(nesdev, &nes_dev_list, list) {
1045                         if (i == ee_flsh_adapter) {
1046                                 nes_write_indexed(nesdev, sysfs_idx_addr, val);
1047                                 break;
1048                         }
1049                         i++;
1050                 }
1051         }
1052         return strnlen(buf, count);
1053 }
1054
1055 static DRIVER_ATTR(adapter, S_IRUSR | S_IWUSR,
1056                    nes_show_adapter, nes_store_adapter);
1057 static DRIVER_ATTR(eeprom_cmd, S_IRUSR | S_IWUSR,
1058                    nes_show_ee_cmd, nes_store_ee_cmd);
1059 static DRIVER_ATTR(eeprom_data, S_IRUSR | S_IWUSR,
1060                    nes_show_ee_data, nes_store_ee_data);
1061 static DRIVER_ATTR(flash_cmd, S_IRUSR | S_IWUSR,
1062                    nes_show_flash_cmd, nes_store_flash_cmd);
1063 static DRIVER_ATTR(flash_data, S_IRUSR | S_IWUSR,
1064                    nes_show_flash_data, nes_store_flash_data);
1065 static DRIVER_ATTR(nonidx_addr, S_IRUSR | S_IWUSR,
1066                    nes_show_nonidx_addr, nes_store_nonidx_addr);
1067 static DRIVER_ATTR(nonidx_data, S_IRUSR | S_IWUSR,
1068                    nes_show_nonidx_data, nes_store_nonidx_data);
1069 static DRIVER_ATTR(idx_addr, S_IRUSR | S_IWUSR,
1070                    nes_show_idx_addr, nes_store_idx_addr);
1071 static DRIVER_ATTR(idx_data, S_IRUSR | S_IWUSR,
1072                    nes_show_idx_data, nes_store_idx_data);
1073
1074 static int nes_create_driver_sysfs(struct pci_driver *drv)
1075 {
1076         int error;
1077         error  = driver_create_file(&drv->driver, &driver_attr_adapter);
1078         error |= driver_create_file(&drv->driver, &driver_attr_eeprom_cmd);
1079         error |= driver_create_file(&drv->driver, &driver_attr_eeprom_data);
1080         error |= driver_create_file(&drv->driver, &driver_attr_flash_cmd);
1081         error |= driver_create_file(&drv->driver, &driver_attr_flash_data);
1082         error |= driver_create_file(&drv->driver, &driver_attr_nonidx_addr);
1083         error |= driver_create_file(&drv->driver, &driver_attr_nonidx_data);
1084         error |= driver_create_file(&drv->driver, &driver_attr_idx_addr);
1085         error |= driver_create_file(&drv->driver, &driver_attr_idx_data);
1086         return error;
1087 }
1088
1089 static void nes_remove_driver_sysfs(struct pci_driver *drv)
1090 {
1091         driver_remove_file(&drv->driver, &driver_attr_adapter);
1092         driver_remove_file(&drv->driver, &driver_attr_eeprom_cmd);
1093         driver_remove_file(&drv->driver, &driver_attr_eeprom_data);
1094         driver_remove_file(&drv->driver, &driver_attr_flash_cmd);
1095         driver_remove_file(&drv->driver, &driver_attr_flash_data);
1096         driver_remove_file(&drv->driver, &driver_attr_nonidx_addr);
1097         driver_remove_file(&drv->driver, &driver_attr_nonidx_data);
1098         driver_remove_file(&drv->driver, &driver_attr_idx_addr);
1099         driver_remove_file(&drv->driver, &driver_attr_idx_data);
1100 }
1101
1102 /**
1103  * nes_init_module - module initialization entry point
1104  */
1105 static int __init nes_init_module(void)
1106 {
1107         int retval;
1108         int retval1;
1109
1110         retval = nes_cm_start();
1111         if (retval) {
1112                 printk(KERN_ERR PFX "Unable to start NetEffect iWARP CM.\n");
1113                 return retval;
1114         }
1115         retval = pci_register_driver(&nes_pci_driver);
1116         if (retval >= 0) {
1117                 retval1 = nes_create_driver_sysfs(&nes_pci_driver);
1118                 if (retval1 < 0)
1119                         printk(KERN_ERR PFX "Unable to create NetEffect sys files.\n");
1120         }
1121         return retval;
1122 }
1123
1124
1125 /**
1126  * nes_exit_module - module unload entry point
1127  */
1128 static void __exit nes_exit_module(void)
1129 {
1130         nes_cm_stop();
1131         nes_remove_driver_sysfs(&nes_pci_driver);
1132
1133         pci_unregister_driver(&nes_pci_driver);
1134 }
1135
1136
1137 module_init(nes_init_module);
1138 module_exit(nes_exit_module);