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