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