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