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