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