nicstar.c 75.7 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * nicstar.c
 *
 * Device driver supporting CBR for IDT 77201/77211 "NICStAR" based cards.
 *
 * IMPORTANT: The included file nicstarmac.c was NOT WRITTEN BY ME.
 *            It was taken from the frle-0.22 device driver.
 *            As the file doesn't have a copyright notice, in the file
 *            nicstarmac.copyright I put the copyright notice from the
 *            frle-0.22 device driver.
 *            Some code is based on the nicstar driver by M. Welsh.
 *
 * Author: Rui Prior (rprior@inescn.pt)
 * PowerPC support by Jay Talbott (jay_talbott@mcg.mot.com) April 1999
 *
 *
 * (C) INESC 1999
18
 */
L
Linus Torvalds 已提交
19

20 21
/*
 * IMPORTANT INFORMATION
L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29 30
 *
 * There are currently three types of spinlocks:
 *
 * 1 - Per card interrupt spinlock (to protect structures and such)
 * 2 - Per SCQ scq spinlock
 * 3 - Per card resource spinlock (to access registers, etc.)
 *
 * These must NEVER be grabbed in reverse order.
 *
31
 */
L
Linus Torvalds 已提交
32

33
/* Header files */
L
Linus Torvalds 已提交
34 35 36 37 38 39 40

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <linux/atmdev.h>
#include <linux/atm.h>
#include <linux/pci.h>
41
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
42 43 44 45 46 47 48 49
#include <linux/types.h>
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/bitops.h>
50
#include <linux/slab.h>
51
#include <linux/idr.h>
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59 60 61 62
#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include "nicstar.h"
#ifdef CONFIG_ATM_NICSTAR_USE_SUNI
#include "suni.h"
#endif /* CONFIG_ATM_NICSTAR_USE_SUNI */
#ifdef CONFIG_ATM_NICSTAR_USE_IDT77105
#include "idt77105.h"
#endif /* CONFIG_ATM_NICSTAR_USE_IDT77105 */

63
/* Additional code */
L
Linus Torvalds 已提交
64 65 66

#include "nicstarmac.c"

67
/* Configurable parameters */
L
Linus Torvalds 已提交
68 69 70 71 72 73 74

#undef PHY_LOOPBACK
#undef TX_DEBUG
#undef RX_DEBUG
#undef GENERAL_DEBUG
#undef EXTRA_DEBUG

75 76
#undef NS_USE_DESTRUCTORS	/* For now keep this undefined unless you know
				   you're going to use only raw ATM */
L
Linus Torvalds 已提交
77

78
/* Do not touch these */
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

#ifdef TX_DEBUG
#define TXPRINTK(args...) printk(args)
#else
#define TXPRINTK(args...)
#endif /* TX_DEBUG */

#ifdef RX_DEBUG
#define RXPRINTK(args...) printk(args)
#else
#define RXPRINTK(args...)
#endif /* RX_DEBUG */

#ifdef GENERAL_DEBUG
#define PRINTK(args...) printk(args)
#else
#define PRINTK(args...)
#endif /* GENERAL_DEBUG */

#ifdef EXTRA_DEBUG
#define XPRINTK(args...) printk(args)
#else
#define XPRINTK(args...)
#endif /* EXTRA_DEBUG */

104
/* Macros */
L
Linus Torvalds 已提交
105 106 107 108 109

#define CMD_BUSY(card) (readl((card)->membase + STAT) & NS_STAT_CMDBZ)

#define NS_DELAY mdelay(1)

110
#define PTR_DIFF(a, b)	((u32)((unsigned long)(a) - (unsigned long)(b)))
L
Linus Torvalds 已提交
111 112 113 114 115

#ifndef ATM_SKB
#define ATM_SKB(s) (&(s)->atm)
#endif

116 117 118
#define scq_virt_to_bus(scq, p) \
		(scq->dma + ((unsigned long)(p) - (unsigned long)(scq)->org))

119
/* Function declarations */
L
Linus Torvalds 已提交
120

121 122 123
static u32 ns_read_sram(ns_dev * card, u32 sram_address);
static void ns_write_sram(ns_dev * card, u32 sram_address, u32 * value,
			  int count);
L
Linus Torvalds 已提交
124
static int __devinit ns_init_card(int i, struct pci_dev *pcidev);
125
static void __devinit ns_init_card_error(ns_dev * card, int error);
126 127
static scq_info *get_scq(ns_dev *card, int size, u32 scd);
static void free_scq(ns_dev *card, scq_info * scq, struct atm_vcc *vcc);
D
David S. Miller 已提交
128
static void push_rxbufs(ns_dev *, struct sk_buff *);
129
static irqreturn_t ns_irq_handler(int irq, void *dev_id);
L
Linus Torvalds 已提交
130 131
static int ns_open(struct atm_vcc *vcc);
static void ns_close(struct atm_vcc *vcc);
132
static void fill_tst(ns_dev * card, int n, vc_map * vc);
L
Linus Torvalds 已提交
133
static int ns_send(struct atm_vcc *vcc, struct sk_buff *skb);
134 135 136 137 138 139
static int push_scqe(ns_dev * card, vc_map * vc, scq_info * scq, ns_scqe * tbd,
		     struct sk_buff *skb);
static void process_tsq(ns_dev * card);
static void drain_scq(ns_dev * card, scq_info * scq, int pos);
static void process_rsq(ns_dev * card);
static void dequeue_rx(ns_dev * card, ns_rsqe * rsqe);
L
Linus Torvalds 已提交
140 141 142 143 144
#ifdef NS_USE_DESTRUCTORS
static void ns_sb_destructor(struct sk_buff *sb);
static void ns_lb_destructor(struct sk_buff *lb);
static void ns_hb_destructor(struct sk_buff *hb);
#endif /* NS_USE_DESTRUCTORS */
145 146 147 148 149 150 151
static void recycle_rx_buf(ns_dev * card, struct sk_buff *skb);
static void recycle_iovec_rx_bufs(ns_dev * card, struct iovec *iov, int count);
static void recycle_iov_buf(ns_dev * card, struct sk_buff *iovb);
static void dequeue_sm_buf(ns_dev * card, struct sk_buff *sb);
static void dequeue_lg_buf(ns_dev * card, struct sk_buff *lb);
static int ns_proc_read(struct atm_dev *dev, loff_t * pos, char *page);
static int ns_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg);
152
#ifdef EXTRA_DEBUG
153
static void which_list(ns_dev * card, struct sk_buff *skb);
154
#endif
L
Linus Torvalds 已提交
155 156 157 158
static void ns_poll(unsigned long arg);
static int ns_parse_mac(char *mac, unsigned char *esi);
static short ns_h2i(char c);
static void ns_phy_put(struct atm_dev *dev, unsigned char value,
159
		       unsigned long addr);
L
Linus Torvalds 已提交
160 161
static unsigned char ns_phy_get(struct atm_dev *dev, unsigned long addr);

162
/* Global variables */
L
Linus Torvalds 已提交
163 164 165

static struct ns_dev *cards[NS_MAX_CARDS];
static unsigned num_cards;
166 167 168 169 170 171 172 173 174
static struct atmdev_ops atm_ops = {
	.open = ns_open,
	.close = ns_close,
	.ioctl = ns_ioctl,
	.send = ns_send,
	.phy_put = ns_phy_put,
	.phy_get = ns_phy_get,
	.proc_read = ns_proc_read,
	.owner = THIS_MODULE,
L
Linus Torvalds 已提交
175
};
176

L
Linus Torvalds 已提交
177 178 179 180 181
static struct timer_list ns_timer;
static char *mac[NS_MAX_CARDS];
module_param_array(mac, charp, NULL, 0);
MODULE_LICENSE("GPL");

182
/* Functions */
L
Linus Torvalds 已提交
183 184 185 186

static int __devinit nicstar_init_one(struct pci_dev *pcidev,
				      const struct pci_device_id *ent)
{
187 188
	static int index = -1;
	unsigned int error;
L
Linus Torvalds 已提交
189

190 191
	index++;
	cards[index] = NULL;
L
Linus Torvalds 已提交
192

193 194 195 196 197
	error = ns_init_card(index, pcidev);
	if (error) {
		cards[index--] = NULL;	/* don't increment index */
		goto err_out;
	}
L
Linus Torvalds 已提交
198

199
	return 0;
L
Linus Torvalds 已提交
200
err_out:
201
	return -ENODEV;
L
Linus Torvalds 已提交
202 203 204 205
}

static void __devexit nicstar_remove_one(struct pci_dev *pcidev)
{
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	int i, j;
	ns_dev *card = pci_get_drvdata(pcidev);
	struct sk_buff *hb;
	struct sk_buff *iovb;
	struct sk_buff *lb;
	struct sk_buff *sb;

	i = card->index;

	if (cards[i] == NULL)
		return;

	if (card->atmdev->phy && card->atmdev->phy->stop)
		card->atmdev->phy->stop(card->atmdev);

	/* Stop everything */
	writel(0x00000000, card->membase + CFG);

	/* De-register device */
	atm_dev_deregister(card->atmdev);

	/* Disable PCI device */
	pci_disable_device(pcidev);

	/* Free up resources */
	j = 0;
	PRINTK("nicstar%d: freeing %d huge buffers.\n", i, card->hbpool.count);
	while ((hb = skb_dequeue(&card->hbpool.queue)) != NULL) {
		dev_kfree_skb_any(hb);
		j++;
	}
	PRINTK("nicstar%d: %d huge buffers freed.\n", i, j);
	j = 0;
	PRINTK("nicstar%d: freeing %d iovec buffers.\n", i,
	       card->iovpool.count);
	while ((iovb = skb_dequeue(&card->iovpool.queue)) != NULL) {
		dev_kfree_skb_any(iovb);
		j++;
	}
	PRINTK("nicstar%d: %d iovec buffers freed.\n", i, j);
	while ((lb = skb_dequeue(&card->lbpool.queue)) != NULL)
		dev_kfree_skb_any(lb);
	while ((sb = skb_dequeue(&card->sbpool.queue)) != NULL)
		dev_kfree_skb_any(sb);
250
	free_scq(card, card->scq0, NULL);
251 252
	for (j = 0; j < NS_FRSCD_NUM; j++) {
		if (card->scd2vc[j] != NULL)
253 254 255 256 257 258 259 260
			free_scq(card, card->scd2vc[j]->scq, card->scd2vc[j]->tx_vcc);
	}
	idr_remove_all(&card->idr);
	idr_destroy(&card->idr);
	pci_free_consistent(card->pcidev, NS_RSQSIZE + NS_RSQ_ALIGNMENT,
			    card->rsq.org, card->rsq.dma);
	pci_free_consistent(card->pcidev, NS_TSQSIZE + NS_TSQ_ALIGNMENT,
			    card->tsq.org, card->tsq.dma);
261 262 263
	free_irq(card->pcidev->irq, card);
	iounmap(card->membase);
	kfree(card);
L
Linus Torvalds 已提交
264 265
}

266
static struct pci_device_id nicstar_pci_tbl[] __devinitdata = {
L
Linus Torvalds 已提交
267 268 269 270 271
	{PCI_VENDOR_ID_IDT, PCI_DEVICE_ID_IDT_IDT77201,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{0,}			/* terminate list */
};

272
MODULE_DEVICE_TABLE(pci, nicstar_pci_tbl);
L
Linus Torvalds 已提交
273 274

static struct pci_driver nicstar_driver = {
275 276 277 278
	.name = "nicstar",
	.id_table = nicstar_pci_tbl,
	.probe = nicstar_init_one,
	.remove = __devexit_p(nicstar_remove_one),
L
Linus Torvalds 已提交
279 280 281 282
};

static int __init nicstar_init(void)
{
283
	unsigned error = 0;	/* Initialized to remove compile warning */
L
Linus Torvalds 已提交
284

285
	XPRINTK("nicstar: nicstar_init() called.\n");
L
Linus Torvalds 已提交
286

287 288 289 290 291
	error = pci_register_driver(&nicstar_driver);

	TXPRINTK("nicstar: TX debug enabled.\n");
	RXPRINTK("nicstar: RX debug enabled.\n");
	PRINTK("nicstar: General debug enabled.\n");
L
Linus Torvalds 已提交
292
#ifdef PHY_LOOPBACK
293
	printk("nicstar: using PHY loopback.\n");
L
Linus Torvalds 已提交
294
#endif /* PHY_LOOPBACK */
295
	XPRINTK("nicstar: nicstar_init() returned.\n");
L
Linus Torvalds 已提交
296

297 298 299 300 301 302 303
	if (!error) {
		init_timer(&ns_timer);
		ns_timer.expires = jiffies + NS_POLL_PERIOD;
		ns_timer.data = 0UL;
		ns_timer.function = ns_poll;
		add_timer(&ns_timer);
	}
L
Linus Torvalds 已提交
304

305 306
	return error;
}
L
Linus Torvalds 已提交
307 308 309

static void __exit nicstar_cleanup(void)
{
310
	XPRINTK("nicstar: nicstar_cleanup() called.\n");
L
Linus Torvalds 已提交
311

312
	del_timer(&ns_timer);
L
Linus Torvalds 已提交
313

314
	pci_unregister_driver(&nicstar_driver);
L
Linus Torvalds 已提交
315

316
	XPRINTK("nicstar: nicstar_cleanup() returned.\n");
L
Linus Torvalds 已提交
317 318
}

319
static u32 ns_read_sram(ns_dev * card, u32 sram_address)
L
Linus Torvalds 已提交
320
{
321 322 323 324 325 326 327 328 329 330 331 332
	unsigned long flags;
	u32 data;
	sram_address <<= 2;
	sram_address &= 0x0007FFFC;	/* address must be dword aligned */
	sram_address |= 0x50000000;	/* SRAM read command */
	spin_lock_irqsave(&card->res_lock, flags);
	while (CMD_BUSY(card)) ;
	writel(sram_address, card->membase + CMD);
	while (CMD_BUSY(card)) ;
	data = readl(card->membase + DR0);
	spin_unlock_irqrestore(&card->res_lock, flags);
	return data;
L
Linus Torvalds 已提交
333 334
}

335 336
static void ns_write_sram(ns_dev * card, u32 sram_address, u32 * value,
			  int count)
L
Linus Torvalds 已提交
337
{
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	unsigned long flags;
	int i, c;
	count--;		/* count range now is 0..3 instead of 1..4 */
	c = count;
	c <<= 2;		/* to use increments of 4 */
	spin_lock_irqsave(&card->res_lock, flags);
	while (CMD_BUSY(card)) ;
	for (i = 0; i <= c; i += 4)
		writel(*(value++), card->membase + i);
	/* Note: DR# registers are the first 4 dwords in nicstar's memspace,
	   so card->membase + DR0 == card->membase */
	sram_address <<= 2;
	sram_address &= 0x0007FFFC;
	sram_address |= (0x40000000 | count);
	writel(sram_address, card->membase + CMD);
	spin_unlock_irqrestore(&card->res_lock, flags);
L
Linus Torvalds 已提交
354 355 356 357
}

static int __devinit ns_init_card(int i, struct pci_dev *pcidev)
{
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
	int j;
	struct ns_dev *card = NULL;
	unsigned char pci_latency;
	unsigned error;
	u32 data;
	u32 u32d[4];
	u32 ns_cfg_rctsize;
	int bcount;
	unsigned long membase;

	error = 0;

	if (pci_enable_device(pcidev)) {
		printk("nicstar%d: can't enable PCI device\n", i);
		error = 2;
		ns_init_card_error(card, error);
		return error;
	}
376 377 378 379 380 381 382 383
        if ((pci_set_dma_mask(pcidev, DMA_BIT_MASK(32)) != 0) ||
	    (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32)) != 0)) {
                printk(KERN_WARNING
		       "nicstar%d: No suitable DMA available.\n", i);
		error = 2;
		ns_init_card_error(card, error);
		return error;
        }
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

	if ((card = kmalloc(sizeof(ns_dev), GFP_KERNEL)) == NULL) {
		printk
		    ("nicstar%d: can't allocate memory for device structure.\n",
		     i);
		error = 2;
		ns_init_card_error(card, error);
		return error;
	}
	cards[i] = card;
	spin_lock_init(&card->int_lock);
	spin_lock_init(&card->res_lock);

	pci_set_drvdata(pcidev, card);

	card->index = i;
	card->atmdev = NULL;
	card->pcidev = pcidev;
	membase = pci_resource_start(pcidev, 1);
	card->membase = ioremap(membase, NS_IOREMAP_SIZE);
	if (!card->membase) {
		printk("nicstar%d: can't ioremap() membase.\n", i);
		error = 3;
		ns_init_card_error(card, error);
		return error;
	}
410
	PRINTK("nicstar%d: membase at 0x%p.\n", i, card->membase);
411 412 413 414 415 416 417 418 419

	pci_set_master(pcidev);

	if (pci_read_config_byte(pcidev, PCI_LATENCY_TIMER, &pci_latency) != 0) {
		printk("nicstar%d: can't read PCI latency timer.\n", i);
		error = 6;
		ns_init_card_error(card, error);
		return error;
	}
L
Linus Torvalds 已提交
420
#ifdef NS_PCI_LATENCY
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
	if (pci_latency < NS_PCI_LATENCY) {
		PRINTK("nicstar%d: setting PCI latency timer to %d.\n", i,
		       NS_PCI_LATENCY);
		for (j = 1; j < 4; j++) {
			if (pci_write_config_byte
			    (pcidev, PCI_LATENCY_TIMER, NS_PCI_LATENCY) != 0)
				break;
		}
		if (j == 4) {
			printk
			    ("nicstar%d: can't set PCI latency timer to %d.\n",
			     i, NS_PCI_LATENCY);
			error = 7;
			ns_init_card_error(card, error);
			return error;
		}
	}
L
Linus Torvalds 已提交
438
#endif /* NS_PCI_LATENCY */
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

	/* Clear timer overflow */
	data = readl(card->membase + STAT);
	if (data & NS_STAT_TMROF)
		writel(NS_STAT_TMROF, card->membase + STAT);

	/* Software reset */
	writel(NS_CFG_SWRST, card->membase + CFG);
	NS_DELAY;
	writel(0x00000000, card->membase + CFG);

	/* PHY reset */
	writel(0x00000008, card->membase + GP);
	NS_DELAY;
	writel(0x00000001, card->membase + GP);
	NS_DELAY;
	while (CMD_BUSY(card)) ;
	writel(NS_CMD_WRITE_UTILITY | 0x00000100, card->membase + CMD);	/* Sync UTOPIA with SAR clock */
	NS_DELAY;

	/* Detect PHY type */
	while (CMD_BUSY(card)) ;
	writel(NS_CMD_READ_UTILITY | 0x00000200, card->membase + CMD);
	while (CMD_BUSY(card)) ;
	data = readl(card->membase + DR0);
	switch (data) {
	case 0x00000009:
		printk("nicstar%d: PHY seems to be 25 Mbps.\n", i);
		card->max_pcr = ATM_25_PCR;
		while (CMD_BUSY(card)) ;
		writel(0x00000008, card->membase + DR0);
		writel(NS_CMD_WRITE_UTILITY | 0x00000200, card->membase + CMD);
		/* Clear an eventual pending interrupt */
		writel(NS_STAT_SFBQF, card->membase + STAT);
L
Linus Torvalds 已提交
473
#ifdef PHY_LOOPBACK
474 475 476
		while (CMD_BUSY(card)) ;
		writel(0x00000022, card->membase + DR0);
		writel(NS_CMD_WRITE_UTILITY | 0x00000202, card->membase + CMD);
L
Linus Torvalds 已提交
477
#endif /* PHY_LOOPBACK */
478 479 480 481 482
		break;
	case 0x00000030:
	case 0x00000031:
		printk("nicstar%d: PHY seems to be 155 Mbps.\n", i);
		card->max_pcr = ATM_OC3_PCR;
L
Linus Torvalds 已提交
483
#ifdef PHY_LOOPBACK
484 485 486
		while (CMD_BUSY(card)) ;
		writel(0x00000002, card->membase + DR0);
		writel(NS_CMD_WRITE_UTILITY | 0x00000205, card->membase + CMD);
L
Linus Torvalds 已提交
487
#endif /* PHY_LOOPBACK */
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
		break;
	default:
		printk("nicstar%d: unknown PHY type (0x%08X).\n", i, data);
		error = 8;
		ns_init_card_error(card, error);
		return error;
	}
	writel(0x00000000, card->membase + GP);

	/* Determine SRAM size */
	data = 0x76543210;
	ns_write_sram(card, 0x1C003, &data, 1);
	data = 0x89ABCDEF;
	ns_write_sram(card, 0x14003, &data, 1);
	if (ns_read_sram(card, 0x14003) == 0x89ABCDEF &&
	    ns_read_sram(card, 0x1C003) == 0x76543210)
		card->sram_size = 128;
	else
		card->sram_size = 32;
	PRINTK("nicstar%d: %dK x 32bit SRAM size.\n", i, card->sram_size);

	card->rct_size = NS_MAX_RCTSIZE;
L
Linus Torvalds 已提交
510 511

#if (NS_MAX_RCTSIZE == 4096)
512 513 514 515
	if (card->sram_size == 128)
		printk
		    ("nicstar%d: limiting maximum VCI. See NS_MAX_RCTSIZE in nicstar.h\n",
		     i);
L
Linus Torvalds 已提交
516
#elif (NS_MAX_RCTSIZE == 16384)
517 518 519 520 521 522
	if (card->sram_size == 32) {
		printk
		    ("nicstar%d: wasting memory. See NS_MAX_RCTSIZE in nicstar.h\n",
		     i);
		card->rct_size = 4096;
	}
L
Linus Torvalds 已提交
523 524 525 526
#else
#error NS_MAX_RCTSIZE must be either 4096 or 16384 in nicstar.c
#endif

527 528 529 530 531 532 533 534 535 536 537 538 539 540
	card->vpibits = NS_VPIBITS;
	if (card->rct_size == 4096)
		card->vcibits = 12 - NS_VPIBITS;
	else			/* card->rct_size == 16384 */
		card->vcibits = 14 - NS_VPIBITS;

	/* Initialize the nicstar eeprom/eprom stuff, for the MAC addr */
	if (mac[i] == NULL)
		nicstar_init_eprom(card->membase);

	/* Set the VPI/VCI MSb mask to zero so we can receive OAM cells */
	writel(0x00000000, card->membase + VPM);

	/* Initialize TSQ */
541 542 543
	card->tsq.org = pci_alloc_consistent(card->pcidev,
					     NS_TSQSIZE + NS_TSQ_ALIGNMENT,
					     &card->tsq.dma);
544 545 546 547 548 549
	if (card->tsq.org == NULL) {
		printk("nicstar%d: can't allocate TSQ.\n", i);
		error = 10;
		ns_init_card_error(card, error);
		return error;
	}
550
	card->tsq.base = PTR_ALIGN(card->tsq.org, NS_TSQ_ALIGNMENT);
551 552 553 554 555
	card->tsq.next = card->tsq.base;
	card->tsq.last = card->tsq.base + (NS_TSQ_NUM_ENTRIES - 1);
	for (j = 0; j < NS_TSQ_NUM_ENTRIES; j++)
		ns_tsi_init(card->tsq.base + j);
	writel(0x00000000, card->membase + TSQH);
556 557
	writel(ALIGN(card->tsq.dma, NS_TSQ_ALIGNMENT), card->membase + TSQB);
	PRINTK("nicstar%d: TSQ base at 0x%p.\n", i, card->tsq.base);
558 559

	/* Initialize RSQ */
560 561 562
	card->rsq.org = pci_alloc_consistent(card->pcidev,
					     NS_RSQSIZE + NS_RSQ_ALIGNMENT,
					     &card->rsq.dma);
563 564 565 566 567 568
	if (card->rsq.org == NULL) {
		printk("nicstar%d: can't allocate RSQ.\n", i);
		error = 11;
		ns_init_card_error(card, error);
		return error;
	}
569
	card->rsq.base = PTR_ALIGN(card->rsq.org, NS_RSQ_ALIGNMENT);
570 571 572 573 574
	card->rsq.next = card->rsq.base;
	card->rsq.last = card->rsq.base + (NS_RSQ_NUM_ENTRIES - 1);
	for (j = 0; j < NS_RSQ_NUM_ENTRIES; j++)
		ns_rsqe_init(card->rsq.base + j);
	writel(0x00000000, card->membase + RSQH);
575 576
	writel(ALIGN(card->rsq.dma, NS_RSQ_ALIGNMENT), card->membase + RSQB);
	PRINTK("nicstar%d: RSQ base at 0x%p.\n", i, card->rsq.base);
577 578 579 580

	/* Initialize SCQ0, the only VBR SCQ used */
	card->scq1 = NULL;
	card->scq2 = NULL;
581
	card->scq0 = get_scq(card, VBR_SCQSIZE, NS_VRSCD0);
582 583 584 585 586 587
	if (card->scq0 == NULL) {
		printk("nicstar%d: can't get SCQ0.\n", i);
		error = 12;
		ns_init_card_error(card, error);
		return error;
	}
588
	u32d[0] = scq_virt_to_bus(card->scq0, card->scq0->base);
589 590 591 592 593 594 595
	u32d[1] = (u32) 0x00000000;
	u32d[2] = (u32) 0xffffffff;
	u32d[3] = (u32) 0x00000000;
	ns_write_sram(card, NS_VRSCD0, u32d, 4);
	ns_write_sram(card, NS_VRSCD1, u32d, 4);	/* These last two won't be used */
	ns_write_sram(card, NS_VRSCD2, u32d, 4);	/* but are initialized, just in case... */
	card->scq0->scd = NS_VRSCD0;
596
	PRINTK("nicstar%d: VBR-SCQ0 base at 0x%p.\n", i, card->scq0->base);
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614

	/* Initialize TSTs */
	card->tst_addr = NS_TST0;
	card->tst_free_entries = NS_TST_NUM_ENTRIES;
	data = NS_TST_OPCODE_VARIABLE;
	for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
		ns_write_sram(card, NS_TST0 + j, &data, 1);
	data = ns_tste_make(NS_TST_OPCODE_END, NS_TST0);
	ns_write_sram(card, NS_TST0 + NS_TST_NUM_ENTRIES, &data, 1);
	for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
		ns_write_sram(card, NS_TST1 + j, &data, 1);
	data = ns_tste_make(NS_TST_OPCODE_END, NS_TST1);
	ns_write_sram(card, NS_TST1 + NS_TST_NUM_ENTRIES, &data, 1);
	for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
		card->tste2vc[j] = NULL;
	writel(NS_TST0 << 2, card->membase + TSTB);

	/* Initialize RCT. AAL type is set on opening the VC. */
L
Linus Torvalds 已提交
615
#ifdef RCQ_SUPPORT
616
	u32d[0] = NS_RCTE_RAWCELLINTEN;
L
Linus Torvalds 已提交
617
#else
618
	u32d[0] = 0x00000000;
L
Linus Torvalds 已提交
619
#endif /* RCQ_SUPPORT */
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
	u32d[1] = 0x00000000;
	u32d[2] = 0x00000000;
	u32d[3] = 0xFFFFFFFF;
	for (j = 0; j < card->rct_size; j++)
		ns_write_sram(card, j * 4, u32d, 4);

	memset(card->vcmap, 0, NS_MAX_RCTSIZE * sizeof(vc_map));

	for (j = 0; j < NS_FRSCD_NUM; j++)
		card->scd2vc[j] = NULL;

	/* Initialize buffer levels */
	card->sbnr.min = MIN_SB;
	card->sbnr.init = NUM_SB;
	card->sbnr.max = MAX_SB;
	card->lbnr.min = MIN_LB;
	card->lbnr.init = NUM_LB;
	card->lbnr.max = MAX_LB;
	card->iovnr.min = MIN_IOVB;
	card->iovnr.init = NUM_IOVB;
	card->iovnr.max = MAX_IOVB;
	card->hbnr.min = MIN_HB;
	card->hbnr.init = NUM_HB;
	card->hbnr.max = MAX_HB;

	card->sm_handle = 0x00000000;
	card->sm_addr = 0x00000000;
	card->lg_handle = 0x00000000;
	card->lg_addr = 0x00000000;

	card->efbie = 1;	/* To prevent push_rxbufs from enabling the interrupt */

652 653
	idr_init(&card->idr);

654 655 656 657 658 659 660 661 662 663 664 665 666 667
	/* Pre-allocate some huge buffers */
	skb_queue_head_init(&card->hbpool.queue);
	card->hbpool.count = 0;
	for (j = 0; j < NUM_HB; j++) {
		struct sk_buff *hb;
		hb = __dev_alloc_skb(NS_HBUFSIZE, GFP_KERNEL);
		if (hb == NULL) {
			printk
			    ("nicstar%d: can't allocate %dth of %d huge buffers.\n",
			     i, j, NUM_HB);
			error = 13;
			ns_init_card_error(card, error);
			return error;
		}
668
		NS_PRV_BUFTYPE(hb) = BUF_NONE;
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
		skb_queue_tail(&card->hbpool.queue, hb);
		card->hbpool.count++;
	}

	/* Allocate large buffers */
	skb_queue_head_init(&card->lbpool.queue);
	card->lbpool.count = 0;	/* Not used */
	for (j = 0; j < NUM_LB; j++) {
		struct sk_buff *lb;
		lb = __dev_alloc_skb(NS_LGSKBSIZE, GFP_KERNEL);
		if (lb == NULL) {
			printk
			    ("nicstar%d: can't allocate %dth of %d large buffers.\n",
			     i, j, NUM_LB);
			error = 14;
			ns_init_card_error(card, error);
			return error;
		}
687
		NS_PRV_BUFTYPE(lb) = BUF_LG;
688 689 690 691 692 693
		skb_queue_tail(&card->lbpool.queue, lb);
		skb_reserve(lb, NS_SMBUFSIZE);
		push_rxbufs(card, lb);
		/* Due to the implementation of push_rxbufs() this is 1, not 0 */
		if (j == 1) {
			card->rcbuf = lb;
694 695
			card->rawcell = (struct ns_rcqe *) lb->data;
			card->rawch = NS_PRV_DMA(lb);
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
		}
	}
	/* Test for strange behaviour which leads to crashes */
	if ((bcount =
	     ns_stat_lfbqc_get(readl(card->membase + STAT))) < card->lbnr.min) {
		printk
		    ("nicstar%d: Strange... Just allocated %d large buffers and lfbqc = %d.\n",
		     i, j, bcount);
		error = 14;
		ns_init_card_error(card, error);
		return error;
	}

	/* Allocate small buffers */
	skb_queue_head_init(&card->sbpool.queue);
	card->sbpool.count = 0;	/* Not used */
	for (j = 0; j < NUM_SB; j++) {
		struct sk_buff *sb;
		sb = __dev_alloc_skb(NS_SMSKBSIZE, GFP_KERNEL);
		if (sb == NULL) {
			printk
			    ("nicstar%d: can't allocate %dth of %d small buffers.\n",
			     i, j, NUM_SB);
			error = 15;
			ns_init_card_error(card, error);
			return error;
		}
723
		NS_PRV_BUFTYPE(sb) = BUF_SM;
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
		skb_queue_tail(&card->sbpool.queue, sb);
		skb_reserve(sb, NS_AAL0_HEADER);
		push_rxbufs(card, sb);
	}
	/* Test for strange behaviour which leads to crashes */
	if ((bcount =
	     ns_stat_sfbqc_get(readl(card->membase + STAT))) < card->sbnr.min) {
		printk
		    ("nicstar%d: Strange... Just allocated %d small buffers and sfbqc = %d.\n",
		     i, j, bcount);
		error = 15;
		ns_init_card_error(card, error);
		return error;
	}

	/* Allocate iovec buffers */
	skb_queue_head_init(&card->iovpool.queue);
	card->iovpool.count = 0;
	for (j = 0; j < NUM_IOVB; j++) {
		struct sk_buff *iovb;
		iovb = alloc_skb(NS_IOVBUFSIZE, GFP_KERNEL);
		if (iovb == NULL) {
			printk
			    ("nicstar%d: can't allocate %dth of %d iovec buffers.\n",
			     i, j, NUM_IOVB);
			error = 16;
			ns_init_card_error(card, error);
			return error;
		}
753
		NS_PRV_BUFTYPE(iovb) = BUF_NONE;
754 755 756 757 758 759 760 761 762 763 764 765 766 767
		skb_queue_tail(&card->iovpool.queue, iovb);
		card->iovpool.count++;
	}

	/* Configure NICStAR */
	if (card->rct_size == 4096)
		ns_cfg_rctsize = NS_CFG_RCTSIZE_4096_ENTRIES;
	else			/* (card->rct_size == 16384) */
		ns_cfg_rctsize = NS_CFG_RCTSIZE_16384_ENTRIES;

	card->efbie = 1;

	card->intcnt = 0;
	if (request_irq
768
	    (pcidev->irq, &ns_irq_handler, IRQF_SHARED, "nicstar", card) != 0) {
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
		printk("nicstar%d: can't allocate IRQ %d.\n", i, pcidev->irq);
		error = 9;
		ns_init_card_error(card, error);
		return error;
	}

	/* Register device */
	card->atmdev = atm_dev_register("nicstar", &atm_ops, -1, NULL);
	if (card->atmdev == NULL) {
		printk("nicstar%d: can't register device.\n", i);
		error = 17;
		ns_init_card_error(card, error);
		return error;
	}

	if (ns_parse_mac(mac[i], card->atmdev->esi)) {
		nicstar_read_eprom(card->membase, NICSTAR_EPROM_MAC_ADDR_OFFSET,
				   card->atmdev->esi, 6);
		if (memcmp(card->atmdev->esi, "\x00\x00\x00\x00\x00\x00", 6) ==
		    0) {
			nicstar_read_eprom(card->membase,
					   NICSTAR_EPROM_MAC_ADDR_OFFSET_ALT,
					   card->atmdev->esi, 6);
		}
	}

	printk("nicstar%d: MAC address %pM\n", i, card->atmdev->esi);

	card->atmdev->dev_data = card;
	card->atmdev->ci_range.vpi_bits = card->vpibits;
	card->atmdev->ci_range.vci_bits = card->vcibits;
	card->atmdev->link_rate = card->max_pcr;
	card->atmdev->phy = NULL;
L
Linus Torvalds 已提交
802 803

#ifdef CONFIG_ATM_NICSTAR_USE_SUNI
804 805
	if (card->max_pcr == ATM_OC3_PCR)
		suni_init(card->atmdev);
L
Linus Torvalds 已提交
806 807 808
#endif /* CONFIG_ATM_NICSTAR_USE_SUNI */

#ifdef CONFIG_ATM_NICSTAR_USE_IDT77105
809 810
	if (card->max_pcr == ATM_25_PCR)
		idt77105_init(card->atmdev);
L
Linus Torvalds 已提交
811 812
#endif /* CONFIG_ATM_NICSTAR_USE_IDT77105 */

813 814
	if (card->atmdev->phy && card->atmdev->phy->start)
		card->atmdev->phy->start(card->atmdev);
L
Linus Torvalds 已提交
815

816 817 818
	writel(NS_CFG_RXPATH | NS_CFG_SMBUFSIZE | NS_CFG_LGBUFSIZE | NS_CFG_EFBIE | NS_CFG_RSQSIZE | NS_CFG_VPIBITS | ns_cfg_rctsize | NS_CFG_RXINT_NODELAY | NS_CFG_RAWIE |	/* Only enabled if RCQ_SUPPORT */
	       NS_CFG_RSQAFIE | NS_CFG_TXEN | NS_CFG_TXIE | NS_CFG_TSQFIE_OPT |	/* Only enabled if ENABLE_TSQFIE */
	       NS_CFG_PHYIE, card->membase + CFG);
L
Linus Torvalds 已提交
819

820
	num_cards++;
L
Linus Torvalds 已提交
821

822
	return error;
L
Linus Torvalds 已提交
823 824
}

825 826 827 828 829 830 831 832 833 834 835 836 837 838
static void __devinit ns_init_card_error(ns_dev * card, int error)
{
	if (error >= 17) {
		writel(0x00000000, card->membase + CFG);
	}
	if (error >= 16) {
		struct sk_buff *iovb;
		while ((iovb = skb_dequeue(&card->iovpool.queue)) != NULL)
			dev_kfree_skb_any(iovb);
	}
	if (error >= 15) {
		struct sk_buff *sb;
		while ((sb = skb_dequeue(&card->sbpool.queue)) != NULL)
			dev_kfree_skb_any(sb);
839
		free_scq(card, card->scq0, NULL);
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
	}
	if (error >= 14) {
		struct sk_buff *lb;
		while ((lb = skb_dequeue(&card->lbpool.queue)) != NULL)
			dev_kfree_skb_any(lb);
	}
	if (error >= 13) {
		struct sk_buff *hb;
		while ((hb = skb_dequeue(&card->hbpool.queue)) != NULL)
			dev_kfree_skb_any(hb);
	}
	if (error >= 12) {
		kfree(card->rsq.org);
	}
	if (error >= 11) {
		kfree(card->tsq.org);
	}
	if (error >= 10) {
		free_irq(card->pcidev->irq, card);
	}
	if (error >= 4) {
		iounmap(card->membase);
	}
	if (error >= 3) {
		pci_disable_device(card->pcidev);
		kfree(card);
	}
}
L
Linus Torvalds 已提交
868

869
static scq_info *get_scq(ns_dev *card, int size, u32 scd)
L
Linus Torvalds 已提交
870
{
871 872 873 874 875 876 877
	scq_info *scq;
	int i;

	if (size != VBR_SCQSIZE && size != CBR_SCQSIZE)
		return NULL;

	scq = kmalloc(sizeof(scq_info), GFP_KERNEL);
878
	if (!scq)
879
		return NULL;
880 881
        scq->org = pci_alloc_consistent(card->pcidev, 2 * size, &scq->dma);
	if (!scq->org) {
882 883 884 885 886
		kfree(scq);
		return NULL;
	}
	scq->skb = kmalloc(sizeof(struct sk_buff *) *
			   (size / NS_SCQE_SIZE), GFP_KERNEL);
887
	if (!scq->skb) {
888 889 890 891 892
		kfree(scq->org);
		kfree(scq);
		return NULL;
	}
	scq->num_entries = size / NS_SCQE_SIZE;
893
	scq->base = PTR_ALIGN(scq->org, size);
894 895 896 897 898 899 900 901 902 903 904 905 906 907
	scq->next = scq->base;
	scq->last = scq->base + (scq->num_entries - 1);
	scq->tail = scq->last;
	scq->scd = scd;
	scq->num_entries = size / NS_SCQE_SIZE;
	scq->tbd_count = 0;
	init_waitqueue_head(&scq->scqfull_waitq);
	scq->full = 0;
	spin_lock_init(&scq->lock);

	for (i = 0; i < scq->num_entries; i++)
		scq->skb[i] = NULL;

	return scq;
L
Linus Torvalds 已提交
908 909 910
}

/* For variable rate SCQ vcc must be NULL */
911
static void free_scq(ns_dev *card, scq_info *scq, struct atm_vcc *vcc)
L
Linus Torvalds 已提交
912
{
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
	int i;

	if (scq->num_entries == VBR_SCQ_NUM_ENTRIES)
		for (i = 0; i < scq->num_entries; i++) {
			if (scq->skb[i] != NULL) {
				vcc = ATM_SKB(scq->skb[i])->vcc;
				if (vcc->pop != NULL)
					vcc->pop(vcc, scq->skb[i]);
				else
					dev_kfree_skb_any(scq->skb[i]);
			}
	} else {		/* vcc must be != NULL */

		if (vcc == NULL) {
			printk
			    ("nicstar: free_scq() called with vcc == NULL for fixed rate scq.");
			for (i = 0; i < scq->num_entries; i++)
				dev_kfree_skb_any(scq->skb[i]);
		} else
			for (i = 0; i < scq->num_entries; i++) {
				if (scq->skb[i] != NULL) {
					if (vcc->pop != NULL)
						vcc->pop(vcc, scq->skb[i]);
					else
						dev_kfree_skb_any(scq->skb[i]);
				}
			}
	}
	kfree(scq->skb);
942 943 944 945
	pci_free_consistent(card->pcidev,
			    2 * (scq->num_entries == VBR_SCQ_NUM_ENTRIES ?
				 VBR_SCQSIZE : CBR_SCQSIZE),
			    scq->org, scq->dma);
946
	kfree(scq);
L
Linus Torvalds 已提交
947 948 949 950
}

/* The handles passed must be pointers to the sk_buff containing the small
   or large buffer(s) cast to u32. */
951
static void push_rxbufs(ns_dev * card, struct sk_buff *skb)
L
Linus Torvalds 已提交
952
{
953 954 955
	struct sk_buff *handle1, *handle2;
	u32 id1 = 0, id2 = 0;
	u32 addr1, addr2;
956 957
	u32 stat;
	unsigned long flags;
958
	int err;
959 960

	/* *BARF* */
961 962 963 964 965 966 967 968 969
	handle2 = NULL;
	addr2 = 0;
	handle1 = skb;
	addr1 = pci_map_single(card->pcidev,
			       skb->data,
			       (NS_PRV_BUFTYPE(skb) == BUF_SM
				? NS_SMSKBSIZE : NS_LGSKBSIZE),
			       PCI_DMA_TODEVICE);
	NS_PRV_DMA(skb) = addr1; /* save so we can unmap later */
L
Linus Torvalds 已提交
970 971

#ifdef GENERAL_DEBUG
972 973 974
	if (!addr1)
		printk("nicstar%d: push_rxbufs called with addr1 = 0.\n",
		       card->index);
L
Linus Torvalds 已提交
975 976
#endif /* GENERAL_DEBUG */

977 978 979
	stat = readl(card->membase + STAT);
	card->sbfqc = ns_stat_sfbqc_get(stat);
	card->lbfqc = ns_stat_lfbqc_get(stat);
980
	if (NS_PRV_BUFTYPE(skb) == BUF_SM) {
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
		if (!addr2) {
			if (card->sm_addr) {
				addr2 = card->sm_addr;
				handle2 = card->sm_handle;
				card->sm_addr = 0x00000000;
				card->sm_handle = 0x00000000;
			} else {	/* (!sm_addr) */

				card->sm_addr = addr1;
				card->sm_handle = handle1;
			}
		}
	} else {		/* buf_type == BUF_LG */

		if (!addr2) {
			if (card->lg_addr) {
				addr2 = card->lg_addr;
				handle2 = card->lg_handle;
				card->lg_addr = 0x00000000;
				card->lg_handle = 0x00000000;
			} else {	/* (!lg_addr) */

				card->lg_addr = addr1;
				card->lg_handle = handle1;
			}
		}
	}

	if (addr2) {
1010
		if (NS_PRV_BUFTYPE(skb) == BUF_SM) {
1011
			if (card->sbfqc >= card->sbnr.max) {
1012 1013 1014 1015
				skb_unlink(handle1, &card->sbpool.queue);
				dev_kfree_skb_any(handle1);
				skb_unlink(handle2, &card->sbpool.queue);
				dev_kfree_skb_any(handle2);
1016 1017 1018 1019 1020 1021
				return;
			} else
				card->sbfqc += 2;
		} else {	/* (buf_type == BUF_LG) */

			if (card->lbfqc >= card->lbnr.max) {
1022 1023 1024 1025
				skb_unlink(handle1, &card->lbpool.queue);
				dev_kfree_skb_any(handle1);
				skb_unlink(handle2, &card->lbpool.queue);
				dev_kfree_skb_any(handle2);
1026 1027 1028 1029 1030
				return;
			} else
				card->lbfqc += 2;
		}

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
		do {
			if (!idr_pre_get(&card->idr, GFP_ATOMIC)) {
				printk(KERN_ERR
				       "nicstar%d: no free memory for idr\n",
				       card->index);
				goto out;
			}

			if (!id1)
				err = idr_get_new_above(&card->idr, handle1, 0, &id1);

			if (!id2 && err == 0)
				err = idr_get_new_above(&card->idr, handle2, 0, &id2);

		} while (err == -EAGAIN);
1046

1047 1048 1049 1050
		if (err)
			goto out;

		spin_lock_irqsave(&card->res_lock, flags);
1051 1052
		while (CMD_BUSY(card)) ;
		writel(addr2, card->membase + DR3);
1053
		writel(id2, card->membase + DR2);
1054
		writel(addr1, card->membase + DR1);
1055 1056
		writel(id1, card->membase + DR0);
		writel(NS_CMD_WRITE_FREEBUFQ | NS_PRV_BUFTYPE(skb),
1057 1058 1059 1060 1061
		       card->membase + CMD);
		spin_unlock_irqrestore(&card->res_lock, flags);

		XPRINTK("nicstar%d: Pushing %s buffers at 0x%x and 0x%x.\n",
			card->index,
1062 1063
			(NS_PRV_BUFTYPE(skb) == BUF_SM ? "small" : "large"),
			addr1, addr2);
1064 1065 1066 1067 1068 1069 1070 1071 1072
	}

	if (!card->efbie && card->sbfqc >= card->sbnr.min &&
	    card->lbfqc >= card->lbnr.min) {
		card->efbie = 1;
		writel((readl(card->membase + CFG) | NS_CFG_EFBIE),
		       card->membase + CFG);
	}

1073
out:
1074
	return;
L
Linus Torvalds 已提交
1075 1076
}

1077
static irqreturn_t ns_irq_handler(int irq, void *dev_id)
L
Linus Torvalds 已提交
1078
{
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
	u32 stat_r;
	ns_dev *card;
	struct atm_dev *dev;
	unsigned long flags;

	card = (ns_dev *) dev_id;
	dev = card->atmdev;
	card->intcnt++;

	PRINTK("nicstar%d: NICStAR generated an interrupt\n", card->index);

	spin_lock_irqsave(&card->int_lock, flags);

	stat_r = readl(card->membase + STAT);

	/* Transmit Status Indicator has been written to T. S. Queue */
	if (stat_r & NS_STAT_TSIF) {
		TXPRINTK("nicstar%d: TSI interrupt\n", card->index);
		process_tsq(card);
		writel(NS_STAT_TSIF, card->membase + STAT);
	}

	/* Incomplete CS-PDU has been transmitted */
	if (stat_r & NS_STAT_TXICP) {
		writel(NS_STAT_TXICP, card->membase + STAT);
		TXPRINTK("nicstar%d: Incomplete CS-PDU transmitted.\n",
			 card->index);
	}

	/* Transmit Status Queue 7/8 full */
	if (stat_r & NS_STAT_TSQF) {
		writel(NS_STAT_TSQF, card->membase + STAT);
		PRINTK("nicstar%d: TSQ full.\n", card->index);
		process_tsq(card);
	}

	/* Timer overflow */
	if (stat_r & NS_STAT_TMROF) {
		writel(NS_STAT_TMROF, card->membase + STAT);
		PRINTK("nicstar%d: Timer overflow.\n", card->index);
	}

	/* PHY device interrupt signal active */
	if (stat_r & NS_STAT_PHYI) {
		writel(NS_STAT_PHYI, card->membase + STAT);
		PRINTK("nicstar%d: PHY interrupt.\n", card->index);
		if (dev->phy && dev->phy->interrupt) {
			dev->phy->interrupt(dev);
		}
	}

	/* Small Buffer Queue is full */
	if (stat_r & NS_STAT_SFBQF) {
		writel(NS_STAT_SFBQF, card->membase + STAT);
		printk("nicstar%d: Small free buffer queue is full.\n",
		       card->index);
	}

	/* Large Buffer Queue is full */
	if (stat_r & NS_STAT_LFBQF) {
		writel(NS_STAT_LFBQF, card->membase + STAT);
		printk("nicstar%d: Large free buffer queue is full.\n",
		       card->index);
	}

	/* Receive Status Queue is full */
	if (stat_r & NS_STAT_RSQF) {
		writel(NS_STAT_RSQF, card->membase + STAT);
		printk("nicstar%d: RSQ full.\n", card->index);
		process_rsq(card);
	}

	/* Complete CS-PDU received */
	if (stat_r & NS_STAT_EOPDU) {
		RXPRINTK("nicstar%d: End of CS-PDU received.\n", card->index);
		process_rsq(card);
		writel(NS_STAT_EOPDU, card->membase + STAT);
	}

	/* Raw cell received */
	if (stat_r & NS_STAT_RAWCF) {
		writel(NS_STAT_RAWCF, card->membase + STAT);
L
Linus Torvalds 已提交
1161
#ifndef RCQ_SUPPORT
1162 1163
		printk("nicstar%d: Raw cell received and no support yet...\n",
		       card->index);
L
Linus Torvalds 已提交
1164
#endif /* RCQ_SUPPORT */
1165 1166 1167 1168 1169
		/* NOTE: the following procedure may keep a raw cell pending until the
		   next interrupt. As this preliminary support is only meant to
		   avoid buffer leakage, this is not an issue. */
		while (readl(card->membase + RAWCT) != card->rawch) {

1170
			if (ns_rcqe_islast(card->rawcell)) {
1171 1172 1173
				struct sk_buff *oldbuf;

				oldbuf = card->rcbuf;
1174 1175 1176 1177 1178
				card->rcbuf = idr_find(&card->idr,
						       ns_rcqe_nextbufhandle(card->rawcell));
				card->rawch = NS_PRV_DMA(card->rcbuf);
				card->rawcell = (struct ns_rcqe *)
						card->rcbuf->data;
1179
				recycle_rx_buf(card, oldbuf);
1180
			} else {
1181
				card->rawch += NS_RCQE_SIZE;
1182 1183
				card->rawcell++;
			}
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
		}
	}

	/* Small buffer queue is empty */
	if (stat_r & NS_STAT_SFBQE) {
		int i;
		struct sk_buff *sb;

		writel(NS_STAT_SFBQE, card->membase + STAT);
		printk("nicstar%d: Small free buffer queue empty.\n",
		       card->index);
		for (i = 0; i < card->sbnr.min; i++) {
			sb = dev_alloc_skb(NS_SMSKBSIZE);
			if (sb == NULL) {
				writel(readl(card->membase + CFG) &
				       ~NS_CFG_EFBIE, card->membase + CFG);
				card->efbie = 0;
				break;
			}
1203
			NS_PRV_BUFTYPE(sb) = BUF_SM;
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
			skb_queue_tail(&card->sbpool.queue, sb);
			skb_reserve(sb, NS_AAL0_HEADER);
			push_rxbufs(card, sb);
		}
		card->sbfqc = i;
		process_rsq(card);
	}

	/* Large buffer queue empty */
	if (stat_r & NS_STAT_LFBQE) {
		int i;
		struct sk_buff *lb;

		writel(NS_STAT_LFBQE, card->membase + STAT);
		printk("nicstar%d: Large free buffer queue empty.\n",
		       card->index);
		for (i = 0; i < card->lbnr.min; i++) {
			lb = dev_alloc_skb(NS_LGSKBSIZE);
			if (lb == NULL) {
				writel(readl(card->membase + CFG) &
				       ~NS_CFG_EFBIE, card->membase + CFG);
				card->efbie = 0;
				break;
			}
1228
			NS_PRV_BUFTYPE(lb) = BUF_LG;
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
			skb_queue_tail(&card->lbpool.queue, lb);
			skb_reserve(lb, NS_SMBUFSIZE);
			push_rxbufs(card, lb);
		}
		card->lbfqc = i;
		process_rsq(card);
	}

	/* Receive Status Queue is 7/8 full */
	if (stat_r & NS_STAT_RSQAF) {
		writel(NS_STAT_RSQAF, card->membase + STAT);
		RXPRINTK("nicstar%d: RSQ almost full.\n", card->index);
		process_rsq(card);
	}

	spin_unlock_irqrestore(&card->int_lock, flags);
	PRINTK("nicstar%d: end of interrupt service\n", card->index);
	return IRQ_HANDLED;
L
Linus Torvalds 已提交
1247 1248 1249 1250
}

static int ns_open(struct atm_vcc *vcc)
{
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
	ns_dev *card;
	vc_map *vc;
	unsigned long tmpl, modl;
	int tcr, tcra;		/* target cell rate, and absolute value */
	int n = 0;		/* Number of entries in the TST. Initialized to remove
				   the compiler warning. */
	u32 u32d[4];
	int frscdi = 0;		/* Index of the SCD. Initialized to remove the compiler
				   warning. How I wish compilers were clever enough to
				   tell which variables can truly be used
				   uninitialized... */
	int inuse;		/* tx or rx vc already in use by another vcc */
	short vpi = vcc->vpi;
	int vci = vcc->vci;

	card = (ns_dev *) vcc->dev->dev_data;
	PRINTK("nicstar%d: opening vpi.vci %d.%d \n", card->index, (int)vpi,
	       vci);
	if (vcc->qos.aal != ATM_AAL5 && vcc->qos.aal != ATM_AAL0) {
		PRINTK("nicstar%d: unsupported AAL.\n", card->index);
		return -EINVAL;
	}

	vc = &(card->vcmap[vpi << card->vcibits | vci]);
	vcc->dev_data = vc;

	inuse = 0;
	if (vcc->qos.txtp.traffic_class != ATM_NONE && vc->tx)
		inuse = 1;
	if (vcc->qos.rxtp.traffic_class != ATM_NONE && vc->rx)
		inuse += 2;
	if (inuse) {
		printk("nicstar%d: %s vci already in use.\n", card->index,
		       inuse == 1 ? "tx" : inuse == 2 ? "rx" : "tx and rx");
		return -EINVAL;
	}

	set_bit(ATM_VF_ADDR, &vcc->flags);

	/* NOTE: You are not allowed to modify an open connection's QOS. To change
	   that, remove the ATM_VF_PARTIAL flag checking. There may be other changes
	   needed to do that. */
	if (!test_bit(ATM_VF_PARTIAL, &vcc->flags)) {
		scq_info *scq;

		set_bit(ATM_VF_PARTIAL, &vcc->flags);
		if (vcc->qos.txtp.traffic_class == ATM_CBR) {
			/* Check requested cell rate and availability of SCD */
			if (vcc->qos.txtp.max_pcr == 0 && vcc->qos.txtp.pcr == 0
			    && vcc->qos.txtp.min_pcr == 0) {
				PRINTK
				    ("nicstar%d: trying to open a CBR vc with cell rate = 0 \n",
				     card->index);
				clear_bit(ATM_VF_PARTIAL, &vcc->flags);
				clear_bit(ATM_VF_ADDR, &vcc->flags);
				return -EINVAL;
			}

			tcr = atm_pcr_goal(&(vcc->qos.txtp));
			tcra = tcr >= 0 ? tcr : -tcr;

			PRINTK("nicstar%d: target cell rate = %d.\n",
			       card->index, vcc->qos.txtp.max_pcr);

			tmpl =
			    (unsigned long)tcra *(unsigned long)
			    NS_TST_NUM_ENTRIES;
			modl = tmpl % card->max_pcr;

			n = (int)(tmpl / card->max_pcr);
			if (tcr > 0) {
				if (modl > 0)
					n++;
			} else if (tcr == 0) {
				if ((n =
				     (card->tst_free_entries -
				      NS_TST_RESERVED)) <= 0) {
					PRINTK
					    ("nicstar%d: no CBR bandwidth free.\n",
					     card->index);
					clear_bit(ATM_VF_PARTIAL, &vcc->flags);
					clear_bit(ATM_VF_ADDR, &vcc->flags);
					return -EINVAL;
				}
			}

			if (n == 0) {
				printk
				    ("nicstar%d: selected bandwidth < granularity.\n",
				     card->index);
				clear_bit(ATM_VF_PARTIAL, &vcc->flags);
				clear_bit(ATM_VF_ADDR, &vcc->flags);
				return -EINVAL;
			}

			if (n > (card->tst_free_entries - NS_TST_RESERVED)) {
				PRINTK
				    ("nicstar%d: not enough free CBR bandwidth.\n",
				     card->index);
				clear_bit(ATM_VF_PARTIAL, &vcc->flags);
				clear_bit(ATM_VF_ADDR, &vcc->flags);
				return -EINVAL;
			} else
				card->tst_free_entries -= n;

			XPRINTK("nicstar%d: writing %d tst entries.\n",
				card->index, n);
			for (frscdi = 0; frscdi < NS_FRSCD_NUM; frscdi++) {
				if (card->scd2vc[frscdi] == NULL) {
					card->scd2vc[frscdi] = vc;
					break;
				}
			}
			if (frscdi == NS_FRSCD_NUM) {
				PRINTK
				    ("nicstar%d: no SCD available for CBR channel.\n",
				     card->index);
				card->tst_free_entries += n;
				clear_bit(ATM_VF_PARTIAL, &vcc->flags);
				clear_bit(ATM_VF_ADDR, &vcc->flags);
				return -EBUSY;
			}

			vc->cbr_scd = NS_FRSCD + frscdi * NS_FRSCD_SIZE;

1376
			scq = get_scq(card, CBR_SCQSIZE, vc->cbr_scd);
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
			if (scq == NULL) {
				PRINTK("nicstar%d: can't get fixed rate SCQ.\n",
				       card->index);
				card->scd2vc[frscdi] = NULL;
				card->tst_free_entries += n;
				clear_bit(ATM_VF_PARTIAL, &vcc->flags);
				clear_bit(ATM_VF_ADDR, &vcc->flags);
				return -ENOMEM;
			}
			vc->scq = scq;
1387
			u32d[0] = scq_virt_to_bus(scq, scq->base);
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
			u32d[1] = (u32) 0x00000000;
			u32d[2] = (u32) 0xffffffff;
			u32d[3] = (u32) 0x00000000;
			ns_write_sram(card, vc->cbr_scd, u32d, 4);

			fill_tst(card, n, vc);
		} else if (vcc->qos.txtp.traffic_class == ATM_UBR) {
			vc->cbr_scd = 0x00000000;
			vc->scq = card->scq0;
		}

		if (vcc->qos.txtp.traffic_class != ATM_NONE) {
			vc->tx = 1;
			vc->tx_vcc = vcc;
			vc->tbd_count = 0;
		}
		if (vcc->qos.rxtp.traffic_class != ATM_NONE) {
			u32 status;

			vc->rx = 1;
			vc->rx_vcc = vcc;
			vc->rx_iov = NULL;

			/* Open the connection in hardware */
			if (vcc->qos.aal == ATM_AAL5)
				status = NS_RCTE_AAL5 | NS_RCTE_CONNECTOPEN;
			else	/* vcc->qos.aal == ATM_AAL0 */
				status = NS_RCTE_AAL0 | NS_RCTE_CONNECTOPEN;
L
Linus Torvalds 已提交
1416
#ifdef RCQ_SUPPORT
1417
			status |= NS_RCTE_RAWCELLINTEN;
L
Linus Torvalds 已提交
1418
#endif /* RCQ_SUPPORT */
1419 1420 1421 1422 1423
			ns_write_sram(card,
				      NS_RCT +
				      (vpi << card->vcibits | vci) *
				      NS_RCT_ENTRY_SIZE, &status, 1);
		}
L
Linus Torvalds 已提交
1424

1425
	}
L
Linus Torvalds 已提交
1426

1427 1428 1429
	set_bit(ATM_VF_READY, &vcc->flags);
	return 0;
}
L
Linus Torvalds 已提交
1430 1431 1432

static void ns_close(struct atm_vcc *vcc)
{
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
	vc_map *vc;
	ns_dev *card;
	u32 data;
	int i;

	vc = vcc->dev_data;
	card = vcc->dev->dev_data;
	PRINTK("nicstar%d: closing vpi.vci %d.%d \n", card->index,
	       (int)vcc->vpi, vcc->vci);

	clear_bit(ATM_VF_READY, &vcc->flags);

	if (vcc->qos.rxtp.traffic_class != ATM_NONE) {
		u32 addr;
		unsigned long flags;

		addr =
		    NS_RCT +
		    (vcc->vpi << card->vcibits | vcc->vci) * NS_RCT_ENTRY_SIZE;
		spin_lock_irqsave(&card->res_lock, flags);
		while (CMD_BUSY(card)) ;
		writel(NS_CMD_CLOSE_CONNECTION | addr << 2,
		       card->membase + CMD);
		spin_unlock_irqrestore(&card->res_lock, flags);

		vc->rx = 0;
		if (vc->rx_iov != NULL) {
			struct sk_buff *iovb;
			u32 stat;

			stat = readl(card->membase + STAT);
			card->sbfqc = ns_stat_sfbqc_get(stat);
			card->lbfqc = ns_stat_lfbqc_get(stat);

			PRINTK
			    ("nicstar%d: closing a VC with pending rx buffers.\n",
			     card->index);
			iovb = vc->rx_iov;
			recycle_iovec_rx_bufs(card, (struct iovec *)iovb->data,
1472 1473
					      NS_PRV_IOVCNT(iovb));
			NS_PRV_IOVCNT(iovb) = 0;
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
			spin_lock_irqsave(&card->int_lock, flags);
			recycle_iov_buf(card, iovb);
			spin_unlock_irqrestore(&card->int_lock, flags);
			vc->rx_iov = NULL;
		}
	}

	if (vcc->qos.txtp.traffic_class != ATM_NONE) {
		vc->tx = 0;
	}

	if (vcc->qos.txtp.traffic_class == ATM_CBR) {
		unsigned long flags;
		ns_scqe *scqep;
		scq_info *scq;

		scq = vc->scq;

		for (;;) {
			spin_lock_irqsave(&scq->lock, flags);
			scqep = scq->next;
			if (scqep == scq->base)
				scqep = scq->last;
			else
				scqep--;
			if (scqep == scq->tail) {
				spin_unlock_irqrestore(&scq->lock, flags);
				break;
			}
			/* If the last entry is not a TSR, place one in the SCQ in order to
			   be able to completely drain it and then close. */
			if (!ns_scqe_is_tsr(scqep) && scq->tail != scq->next) {
				ns_scqe tsr;
				u32 scdi, scqi;
				u32 data;
				int index;

				tsr.word_1 = ns_tsr_mkword_1(NS_TSR_INTENABLE);
				scdi = (vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE;
				scqi = scq->next - scq->base;
				tsr.word_2 = ns_tsr_mkword_2(scdi, scqi);
				tsr.word_3 = 0x00000000;
				tsr.word_4 = 0x00000000;
				*scq->next = tsr;
				index = (int)scqi;
				scq->skb[index] = NULL;
				if (scq->next == scq->last)
					scq->next = scq->base;
				else
					scq->next++;
1524
				data = scq_virt_to_bus(scq, scq->next);
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
				ns_write_sram(card, scq->scd, &data, 1);
			}
			spin_unlock_irqrestore(&scq->lock, flags);
			schedule();
		}

		/* Free all TST entries */
		data = NS_TST_OPCODE_VARIABLE;
		for (i = 0; i < NS_TST_NUM_ENTRIES; i++) {
			if (card->tste2vc[i] == vc) {
				ns_write_sram(card, card->tst_addr + i, &data,
					      1);
				card->tste2vc[i] = NULL;
				card->tst_free_entries++;
			}
		}

		card->scd2vc[(vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE] = NULL;
1543
		free_scq(card, vc->scq, vcc);
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
	}

	/* remove all references to vcc before deleting it */
	if (vcc->qos.txtp.traffic_class != ATM_NONE) {
		unsigned long flags;
		scq_info *scq = card->scq0;

		spin_lock_irqsave(&scq->lock, flags);

		for (i = 0; i < scq->num_entries; i++) {
			if (scq->skb[i] && ATM_SKB(scq->skb[i])->vcc == vcc) {
				ATM_SKB(scq->skb[i])->vcc = NULL;
				atm_return(vcc, scq->skb[i]->truesize);
				PRINTK
				    ("nicstar: deleted pending vcc mapping\n");
			}
		}

		spin_unlock_irqrestore(&scq->lock, flags);
	}

	vcc->dev_data = NULL;
	clear_bit(ATM_VF_PARTIAL, &vcc->flags);
	clear_bit(ATM_VF_ADDR, &vcc->flags);
L
Linus Torvalds 已提交
1568 1569

#ifdef RX_DEBUG
1570 1571 1572 1573 1574 1575
	{
		u32 stat, cfg;
		stat = readl(card->membase + STAT);
		cfg = readl(card->membase + CFG);
		printk("STAT = 0x%08X  CFG = 0x%08X  \n", stat, cfg);
		printk
1576 1577 1578
		    ("TSQ: base = 0x%p  next = 0x%p  last = 0x%p  TSQT = 0x%08X \n",
		     card->tsq.base, card->tsq.next,
		     card->tsq.last, readl(card->membase + TSQT));
1579
		printk
1580 1581 1582
		    ("RSQ: base = 0x%p  next = 0x%p  last = 0x%p  RSQT = 0x%08X \n",
		     card->rsq.base, card->rsq.next,
		     card->rsq.last, readl(card->membase + RSQT));
1583 1584 1585 1586 1587 1588 1589 1590
		printk("Empty free buffer queue interrupt %s \n",
		       card->efbie ? "enabled" : "disabled");
		printk("SBCNT = %d  count = %d   LBCNT = %d count = %d \n",
		       ns_stat_sfbqc_get(stat), card->sbpool.count,
		       ns_stat_lfbqc_get(stat), card->lbpool.count);
		printk("hbpool.count = %d  iovpool.count = %d \n",
		       card->hbpool.count, card->iovpool.count);
	}
L
Linus Torvalds 已提交
1591 1592 1593
#endif /* RX_DEBUG */
}

1594
static void fill_tst(ns_dev * card, int n, vc_map * vc)
L
Linus Torvalds 已提交
1595
{
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
	u32 new_tst;
	unsigned long cl;
	int e, r;
	u32 data;

	/* It would be very complicated to keep the two TSTs synchronized while
	   assuring that writes are only made to the inactive TST. So, for now I
	   will use only one TST. If problems occur, I will change this again */

	new_tst = card->tst_addr;

	/* Fill procedure */

	for (e = 0; e < NS_TST_NUM_ENTRIES; e++) {
		if (card->tste2vc[e] == NULL)
			break;
	}
	if (e == NS_TST_NUM_ENTRIES) {
		printk("nicstar%d: No free TST entries found. \n", card->index);
		return;
	}

	r = n;
	cl = NS_TST_NUM_ENTRIES;
	data = ns_tste_make(NS_TST_OPCODE_FIXED, vc->cbr_scd);

	while (r > 0) {
		if (cl >= NS_TST_NUM_ENTRIES && card->tste2vc[e] == NULL) {
			card->tste2vc[e] = vc;
			ns_write_sram(card, new_tst + e, &data, 1);
			cl -= NS_TST_NUM_ENTRIES;
			r--;
		}

		if (++e == NS_TST_NUM_ENTRIES) {
			e = 0;
		}
		cl += n;
	}

	/* End of fill procedure */

	data = ns_tste_make(NS_TST_OPCODE_END, new_tst);
	ns_write_sram(card, new_tst + NS_TST_NUM_ENTRIES, &data, 1);
	ns_write_sram(card, card->tst_addr + NS_TST_NUM_ENTRIES, &data, 1);
	card->tst_addr = new_tst;
L
Linus Torvalds 已提交
1642 1643 1644 1645
}

static int ns_send(struct atm_vcc *vcc, struct sk_buff *skb)
{
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
	ns_dev *card;
	vc_map *vc;
	scq_info *scq;
	unsigned long buflen;
	ns_scqe scqe;
	u32 flags;		/* TBD flags, not CPU flags */

	card = vcc->dev->dev_data;
	TXPRINTK("nicstar%d: ns_send() called.\n", card->index);
	if ((vc = (vc_map *) vcc->dev_data) == NULL) {
		printk("nicstar%d: vcc->dev_data == NULL on ns_send().\n",
		       card->index);
		atomic_inc(&vcc->stats->tx_err);
		dev_kfree_skb_any(skb);
		return -EINVAL;
	}
L
Linus Torvalds 已提交
1662

1663 1664 1665 1666 1667 1668 1669
	if (!vc->tx) {
		printk("nicstar%d: Trying to transmit on a non-tx VC.\n",
		       card->index);
		atomic_inc(&vcc->stats->tx_err);
		dev_kfree_skb_any(skb);
		return -EINVAL;
	}
L
Linus Torvalds 已提交
1670

1671 1672 1673 1674 1675 1676 1677
	if (vcc->qos.aal != ATM_AAL5 && vcc->qos.aal != ATM_AAL0) {
		printk("nicstar%d: Only AAL0 and AAL5 are supported.\n",
		       card->index);
		atomic_inc(&vcc->stats->tx_err);
		dev_kfree_skb_any(skb);
		return -EINVAL;
	}
L
Linus Torvalds 已提交
1678

1679 1680 1681 1682 1683 1684 1685 1686 1687
	if (skb_shinfo(skb)->nr_frags != 0) {
		printk("nicstar%d: No scatter-gather yet.\n", card->index);
		atomic_inc(&vcc->stats->tx_err);
		dev_kfree_skb_any(skb);
		return -EINVAL;
	}

	ATM_SKB(skb)->vcc = vcc;

1688 1689 1690
	NS_PRV_DMA(skb) = pci_map_single(card->pcidev, skb->data,
					 skb->len, PCI_DMA_TODEVICE);

1691 1692 1693
	if (vcc->qos.aal == ATM_AAL5) {
		buflen = (skb->len + 47 + 8) / 48 * 48;	/* Multiple of 48 */
		flags = NS_TBD_AAL5;
1694 1695
		scqe.word_2 = cpu_to_le32(NS_PRV_DMA(skb));
		scqe.word_3 = cpu_to_le32(skb->len);
1696 1697 1698 1699 1700 1701 1702 1703 1704
		scqe.word_4 =
		    ns_tbd_mkword_4(0, (u32) vcc->vpi, (u32) vcc->vci, 0,
				    ATM_SKB(skb)->
				    atm_options & ATM_ATMOPT_CLP ? 1 : 0);
		flags |= NS_TBD_EOPDU;
	} else {		/* (vcc->qos.aal == ATM_AAL0) */

		buflen = ATM_CELL_PAYLOAD;	/* i.e., 48 bytes */
		flags = NS_TBD_AAL0;
1705
		scqe.word_2 = cpu_to_le32(NS_PRV_DMA(skb) + NS_AAL0_HEADER);
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
		scqe.word_3 = cpu_to_le32(0x00000000);
		if (*skb->data & 0x02)	/* Payload type 1 - end of pdu */
			flags |= NS_TBD_EOPDU;
		scqe.word_4 =
		    cpu_to_le32(*((u32 *) skb->data) & ~NS_TBD_VC_MASK);
		/* Force the VPI/VCI to be the same as in VCC struct */
		scqe.word_4 |=
		    cpu_to_le32((((u32) vcc->
				  vpi) << NS_TBD_VPI_SHIFT | ((u32) vcc->
							      vci) <<
				 NS_TBD_VCI_SHIFT) & NS_TBD_VC_MASK);
	}

	if (vcc->qos.txtp.traffic_class == ATM_CBR) {
		scqe.word_1 = ns_tbd_mkword_1_novbr(flags, (u32) buflen);
		scq = ((vc_map *) vcc->dev_data)->scq;
	} else {
		scqe.word_1 =
		    ns_tbd_mkword_1(flags, (u32) 1, (u32) 1, (u32) buflen);
		scq = card->scq0;
	}

	if (push_scqe(card, vc, scq, &scqe, skb) != 0) {
		atomic_inc(&vcc->stats->tx_err);
		dev_kfree_skb_any(skb);
		return -EIO;
	}
	atomic_inc(&vcc->stats->tx);
L
Linus Torvalds 已提交
1734

1735 1736
	return 0;
}
L
Linus Torvalds 已提交
1737

1738 1739
static int push_scqe(ns_dev * card, vc_map * vc, scq_info * scq, ns_scqe * tbd,
		     struct sk_buff *skb)
L
Linus Torvalds 已提交
1740
{
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
	unsigned long flags;
	ns_scqe tsr;
	u32 scdi, scqi;
	int scq_is_vbr;
	u32 data;
	int index;

	spin_lock_irqsave(&scq->lock, flags);
	while (scq->tail == scq->next) {
		if (in_interrupt()) {
			spin_unlock_irqrestore(&scq->lock, flags);
			printk("nicstar%d: Error pushing TBD.\n", card->index);
			return 1;
		}

		scq->full = 1;
		spin_unlock_irqrestore(&scq->lock, flags);
		interruptible_sleep_on_timeout(&scq->scqfull_waitq,
					       SCQFULL_TIMEOUT);
		spin_lock_irqsave(&scq->lock, flags);

		if (scq->full) {
			spin_unlock_irqrestore(&scq->lock, flags);
			printk("nicstar%d: Timeout pushing TBD.\n",
			       card->index);
			return 1;
		}
	}
	*scq->next = *tbd;
	index = (int)(scq->next - scq->base);
	scq->skb[index] = skb;
1772 1773 1774
	XPRINTK("nicstar%d: sending skb at 0x%p (pos %d).\n",
		card->index, skb, index);
	XPRINTK("nicstar%d: TBD written:\n0x%x\n0x%x\n0x%x\n0x%x\n at 0x%p.\n",
1775 1776
		card->index, le32_to_cpu(tbd->word_1), le32_to_cpu(tbd->word_2),
		le32_to_cpu(tbd->word_3), le32_to_cpu(tbd->word_4),
1777
		scq->next);
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
	if (scq->next == scq->last)
		scq->next = scq->base;
	else
		scq->next++;

	vc->tbd_count++;
	if (scq->num_entries == VBR_SCQ_NUM_ENTRIES) {
		scq->tbd_count++;
		scq_is_vbr = 1;
	} else
		scq_is_vbr = 0;

	if (vc->tbd_count >= MAX_TBD_PER_VC
	    || scq->tbd_count >= MAX_TBD_PER_SCQ) {
		int has_run = 0;

		while (scq->tail == scq->next) {
			if (in_interrupt()) {
1796
				data = scq_virt_to_bus(scq, scq->next);
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
				ns_write_sram(card, scq->scd, &data, 1);
				spin_unlock_irqrestore(&scq->lock, flags);
				printk("nicstar%d: Error pushing TSR.\n",
				       card->index);
				return 0;
			}

			scq->full = 1;
			if (has_run++)
				break;
			spin_unlock_irqrestore(&scq->lock, flags);
			interruptible_sleep_on_timeout(&scq->scqfull_waitq,
						       SCQFULL_TIMEOUT);
			spin_lock_irqsave(&scq->lock, flags);
		}

		if (!scq->full) {
			tsr.word_1 = ns_tsr_mkword_1(NS_TSR_INTENABLE);
			if (scq_is_vbr)
				scdi = NS_TSR_SCDISVBR;
			else
				scdi = (vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE;
			scqi = scq->next - scq->base;
			tsr.word_2 = ns_tsr_mkword_2(scdi, scqi);
			tsr.word_3 = 0x00000000;
			tsr.word_4 = 0x00000000;

			*scq->next = tsr;
			index = (int)scqi;
			scq->skb[index] = NULL;
			XPRINTK
1828
			    ("nicstar%d: TSR written:\n0x%x\n0x%x\n0x%x\n0x%x\n at 0x%p.\n",
1829 1830
			     card->index, le32_to_cpu(tsr.word_1),
			     le32_to_cpu(tsr.word_2), le32_to_cpu(tsr.word_3),
1831
			     le32_to_cpu(tsr.word_4), scq->next);
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
			if (scq->next == scq->last)
				scq->next = scq->base;
			else
				scq->next++;
			vc->tbd_count = 0;
			scq->tbd_count = 0;
		} else
			PRINTK("nicstar%d: Timeout pushing TSR.\n",
			       card->index);
	}
1842
	data = scq_virt_to_bus(scq, scq->next);
1843 1844 1845 1846 1847
	ns_write_sram(card, scq->scd, &data, 1);

	spin_unlock_irqrestore(&scq->lock, flags);

	return 0;
L
Linus Torvalds 已提交
1848 1849
}

1850
static void process_tsq(ns_dev * card)
L
Linus Torvalds 已提交
1851
{
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
	u32 scdi;
	scq_info *scq;
	ns_tsi *previous = NULL, *one_ahead, *two_ahead;
	int serviced_entries;	/* flag indicating at least on entry was serviced */

	serviced_entries = 0;

	if (card->tsq.next == card->tsq.last)
		one_ahead = card->tsq.base;
	else
		one_ahead = card->tsq.next + 1;

	if (one_ahead == card->tsq.last)
		two_ahead = card->tsq.base;
	else
		two_ahead = one_ahead + 1;

	while (!ns_tsi_isempty(card->tsq.next) || !ns_tsi_isempty(one_ahead) ||
	       !ns_tsi_isempty(two_ahead))
		/* At most two empty, as stated in the 77201 errata */
	{
		serviced_entries = 1;

		/* Skip the one or two possible empty entries */
		while (ns_tsi_isempty(card->tsq.next)) {
			if (card->tsq.next == card->tsq.last)
				card->tsq.next = card->tsq.base;
			else
				card->tsq.next++;
		}

		if (!ns_tsi_tmrof(card->tsq.next)) {
			scdi = ns_tsi_getscdindex(card->tsq.next);
			if (scdi == NS_TSI_SCDISVBR)
				scq = card->scq0;
			else {
				if (card->scd2vc[scdi] == NULL) {
					printk
					    ("nicstar%d: could not find VC from SCD index.\n",
					     card->index);
					ns_tsi_init(card->tsq.next);
					return;
				}
				scq = card->scd2vc[scdi]->scq;
			}
			drain_scq(card, scq, ns_tsi_getscqpos(card->tsq.next));
			scq->full = 0;
			wake_up_interruptible(&(scq->scqfull_waitq));
		}

		ns_tsi_init(card->tsq.next);
		previous = card->tsq.next;
		if (card->tsq.next == card->tsq.last)
			card->tsq.next = card->tsq.base;
		else
			card->tsq.next++;

		if (card->tsq.next == card->tsq.last)
			one_ahead = card->tsq.base;
		else
			one_ahead = card->tsq.next + 1;

		if (one_ahead == card->tsq.last)
			two_ahead = card->tsq.base;
		else
			two_ahead = one_ahead + 1;
	}

1920 1921
	if (serviced_entries)
		writel(PTR_DIFF(previous, card->tsq.base),
1922
		       card->membase + TSQH);
L
Linus Torvalds 已提交
1923 1924
}

1925
static void drain_scq(ns_dev * card, scq_info * scq, int pos)
L
Linus Torvalds 已提交
1926
{
1927 1928 1929 1930 1931
	struct atm_vcc *vcc;
	struct sk_buff *skb;
	int i;
	unsigned long flags;

1932 1933
	XPRINTK("nicstar%d: drain_scq() called, scq at 0x%p, pos %d.\n",
		card->index, scq, pos);
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
	if (pos >= scq->num_entries) {
		printk("nicstar%d: Bad index on drain_scq().\n", card->index);
		return;
	}

	spin_lock_irqsave(&scq->lock, flags);
	i = (int)(scq->tail - scq->base);
	if (++i == scq->num_entries)
		i = 0;
	while (i != pos) {
		skb = scq->skb[i];
1945 1946
		XPRINTK("nicstar%d: freeing skb at 0x%p (index %d).\n",
			card->index, skb, i);
1947
		if (skb != NULL) {
1948 1949 1950 1951
			pci_unmap_single(card->pcidev,
					 NS_PRV_DMA(skb),
					 skb->len,
					 PCI_DMA_TODEVICE);
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
			vcc = ATM_SKB(skb)->vcc;
			if (vcc && vcc->pop != NULL) {
				vcc->pop(vcc, skb);
			} else {
				dev_kfree_skb_irq(skb);
			}
			scq->skb[i] = NULL;
		}
		if (++i == scq->num_entries)
			i = 0;
	}
	scq->tail = scq->base + pos;
	spin_unlock_irqrestore(&scq->lock, flags);
L
Linus Torvalds 已提交
1965 1966
}

1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
static void process_rsq(ns_dev * card)
{
	ns_rsqe *previous;

	if (!ns_rsqe_valid(card->rsq.next))
		return;
	do {
		dequeue_rx(card, card->rsq.next);
		ns_rsqe_init(card->rsq.next);
		previous = card->rsq.next;
		if (card->rsq.next == card->rsq.last)
			card->rsq.next = card->rsq.base;
		else
			card->rsq.next++;
	} while (ns_rsqe_valid(card->rsq.next));
1982
	writel(PTR_DIFF(previous, card->rsq.base), card->membase + RSQH);
1983
}
L
Linus Torvalds 已提交
1984

1985
static void dequeue_rx(ns_dev * card, ns_rsqe * rsqe)
L
Linus Torvalds 已提交
1986
{
1987 1988 1989 1990 1991 1992 1993 1994 1995
	u32 vpi, vci;
	vc_map *vc;
	struct sk_buff *iovb;
	struct iovec *iov;
	struct atm_vcc *vcc;
	struct sk_buff *skb;
	unsigned short aal5_len;
	int len;
	u32 stat;
1996
	u32 id;
1997 1998 1999 2000 2001

	stat = readl(card->membase + STAT);
	card->sbfqc = ns_stat_sfbqc_get(stat);
	card->lbfqc = ns_stat_lfbqc_get(stat);

2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
	id = le32_to_cpu(rsqe->buffer_handle);
	skb = idr_find(&card->idr, id);
	if (!skb) {
		RXPRINTK(KERN_ERR
			 "nicstar%d: idr_find() failed!\n", card->index);
		return;
	}
	idr_remove(&card->idr, id);
        pci_dma_sync_single_for_cpu(card->pcidev,
				    NS_PRV_DMA(skb),
				    (NS_PRV_BUFTYPE(skb) == BUF_SM
				     ? NS_SMSKBSIZE : NS_LGSKBSIZE),
				    PCI_DMA_FROMDEVICE);
	pci_unmap_single(card->pcidev,
			 NS_PRV_DMA(skb),
			 (NS_PRV_BUFTYPE(skb) == BUF_SM
			  ? NS_SMSKBSIZE : NS_LGSKBSIZE),
			 PCI_DMA_FROMDEVICE);
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
	vpi = ns_rsqe_vpi(rsqe);
	vci = ns_rsqe_vci(rsqe);
	if (vpi >= 1UL << card->vpibits || vci >= 1UL << card->vcibits) {
		printk("nicstar%d: SDU received for out-of-range vc %d.%d.\n",
		       card->index, vpi, vci);
		recycle_rx_buf(card, skb);
		return;
	}

	vc = &(card->vcmap[vpi << card->vcibits | vci]);
	if (!vc->rx) {
		RXPRINTK("nicstar%d: SDU received on non-rx vc %d.%d.\n",
			 card->index, vpi, vci);
		recycle_rx_buf(card, skb);
		return;
	}

	vcc = vc->rx_vcc;

	if (vcc->qos.aal == ATM_AAL0) {
		struct sk_buff *sb;
		unsigned char *cell;
		int i;

		cell = skb->data;
		for (i = ns_rsqe_cellcount(rsqe); i; i--) {
			if ((sb = dev_alloc_skb(NS_SMSKBSIZE)) == NULL) {
				printk
				    ("nicstar%d: Can't allocate buffers for aal0.\n",
				     card->index);
				atomic_add(i, &vcc->stats->rx_drop);
				break;
			}
			if (!atm_charge(vcc, sb->truesize)) {
				RXPRINTK
				    ("nicstar%d: atm_charge() dropped aal0 packets.\n",
				     card->index);
				atomic_add(i - 1, &vcc->stats->rx_drop);	/* already increased by 1 */
				dev_kfree_skb_any(sb);
				break;
			}
			/* Rebuild the header */
			*((u32 *) sb->data) = le32_to_cpu(rsqe->word_1) << 4 |
			    (ns_rsqe_clp(rsqe) ? 0x00000001 : 0x00000000);
			if (i == 1 && ns_rsqe_eopdu(rsqe))
				*((u32 *) sb->data) |= 0x00000002;
			skb_put(sb, NS_AAL0_HEADER);
			memcpy(skb_tail_pointer(sb), cell, ATM_CELL_PAYLOAD);
			skb_put(sb, ATM_CELL_PAYLOAD);
			ATM_SKB(sb)->vcc = vcc;
			__net_timestamp(sb);
			vcc->push(vcc, sb);
			atomic_inc(&vcc->stats->rx);
			cell += ATM_CELL_PAYLOAD;
		}

		recycle_rx_buf(card, skb);
		return;
	}

	/* To reach this point, the AAL layer can only be AAL5 */

	if ((iovb = vc->rx_iov) == NULL) {
		iovb = skb_dequeue(&(card->iovpool.queue));
		if (iovb == NULL) {	/* No buffers in the queue */
			iovb = alloc_skb(NS_IOVBUFSIZE, GFP_ATOMIC);
			if (iovb == NULL) {
				printk("nicstar%d: Out of iovec buffers.\n",
				       card->index);
				atomic_inc(&vcc->stats->rx_drop);
				recycle_rx_buf(card, skb);
				return;
			}
2093
			NS_PRV_BUFTYPE(iovb) = BUF_NONE;
2094 2095 2096 2097
		} else if (--card->iovpool.count < card->iovnr.min) {
			struct sk_buff *new_iovb;
			if ((new_iovb =
			     alloc_skb(NS_IOVBUFSIZE, GFP_ATOMIC)) != NULL) {
2098
				NS_PRV_BUFTYPE(iovb) = BUF_NONE;
2099 2100 2101 2102 2103
				skb_queue_tail(&card->iovpool.queue, new_iovb);
				card->iovpool.count++;
			}
		}
		vc->rx_iov = iovb;
2104
		NS_PRV_IOVCNT(iovb) = 0;
2105 2106 2107 2108 2109 2110
		iovb->len = 0;
		iovb->data = iovb->head;
		skb_reset_tail_pointer(iovb);
		/* IMPORTANT: a pointer to the sk_buff containing the small or large
		   buffer is stored as iovec base, NOT a pointer to the
		   small or large buffer itself. */
2111
	} else if (NS_PRV_IOVCNT(iovb) >= NS_MAX_IOVECS) {
2112 2113 2114 2115
		printk("nicstar%d: received too big AAL5 SDU.\n", card->index);
		atomic_inc(&vcc->stats->rx_err);
		recycle_iovec_rx_bufs(card, (struct iovec *)iovb->data,
				      NS_MAX_IOVECS);
2116
		NS_PRV_IOVCNT(iovb) = 0;
2117 2118 2119 2120
		iovb->len = 0;
		iovb->data = iovb->head;
		skb_reset_tail_pointer(iovb);
	}
2121
	iov = &((struct iovec *)iovb->data)[NS_PRV_IOVCNT(iovb)++];
2122 2123 2124 2125
	iov->iov_base = (void *)skb;
	iov->iov_len = ns_rsqe_cellcount(rsqe) * 48;
	iovb->len += iov->iov_len;

2126 2127 2128
#ifdef EXTRA_DEBUG
	if (NS_PRV_IOVCNT(iovb) == 1) {
		if (NS_PRV_BUFTYPE(skb) != BUF_SM) {
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
			printk
			    ("nicstar%d: Expected a small buffer, and this is not one.\n",
			     card->index);
			which_list(card, skb);
			atomic_inc(&vcc->stats->rx_err);
			recycle_rx_buf(card, skb);
			vc->rx_iov = NULL;
			recycle_iov_buf(card, iovb);
			return;
		}
2139
	} else {		/* NS_PRV_IOVCNT(iovb) >= 2 */
2140

2141
		if (NS_PRV_BUFTYPE(skb) != BUF_LG) {
2142 2143 2144 2145 2146 2147
			printk
			    ("nicstar%d: Expected a large buffer, and this is not one.\n",
			     card->index);
			which_list(card, skb);
			atomic_inc(&vcc->stats->rx_err);
			recycle_iovec_rx_bufs(card, (struct iovec *)iovb->data,
2148
					      NS_PRV_IOVCNT(iovb));
2149 2150 2151 2152 2153
			vc->rx_iov = NULL;
			recycle_iov_buf(card, iovb);
			return;
		}
	}
2154
#endif /* EXTRA_DEBUG */
2155 2156 2157

	if (ns_rsqe_eopdu(rsqe)) {
		/* This works correctly regardless of the endianness of the host */
2158 2159
		unsigned char *L1L2 = (unsigned char *)
						(skb->data + iov->iov_len - 6);
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
		aal5_len = L1L2[0] << 8 | L1L2[1];
		len = (aal5_len == 0x0000) ? 0x10000 : aal5_len;
		if (ns_rsqe_crcerr(rsqe) ||
		    len + 8 > iovb->len || len + (47 + 8) < iovb->len) {
			printk("nicstar%d: AAL5 CRC error", card->index);
			if (len + 8 > iovb->len || len + (47 + 8) < iovb->len)
				printk(" - PDU size mismatch.\n");
			else
				printk(".\n");
			atomic_inc(&vcc->stats->rx_err);
			recycle_iovec_rx_bufs(card, (struct iovec *)iovb->data,
2171
					      NS_PRV_IOVCNT(iovb));
2172 2173 2174 2175 2176 2177 2178
			vc->rx_iov = NULL;
			recycle_iov_buf(card, iovb);
			return;
		}

		/* By this point we (hopefully) have a complete SDU without errors. */

2179
		if (NS_PRV_IOVCNT(iovb) == 1) {	/* Just a small buffer */
2180 2181 2182 2183 2184 2185 2186
			/* skb points to a small buffer */
			if (!atm_charge(vcc, skb->truesize)) {
				push_rxbufs(card, skb);
				atomic_inc(&vcc->stats->rx_drop);
			} else {
				skb_put(skb, len);
				dequeue_sm_buf(card, skb);
L
Linus Torvalds 已提交
2187
#ifdef NS_USE_DESTRUCTORS
2188
				skb->destructor = ns_sb_destructor;
L
Linus Torvalds 已提交
2189
#endif /* NS_USE_DESTRUCTORS */
2190 2191 2192 2193 2194
				ATM_SKB(skb)->vcc = vcc;
				__net_timestamp(skb);
				vcc->push(vcc, skb);
				atomic_inc(&vcc->stats->rx);
			}
2195
		} else if (NS_PRV_IOVCNT(iovb) == 2) {	/* One small plus one large buffer */
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
			struct sk_buff *sb;

			sb = (struct sk_buff *)(iov - 1)->iov_base;
			/* skb points to a large buffer */

			if (len <= NS_SMBUFSIZE) {
				if (!atm_charge(vcc, sb->truesize)) {
					push_rxbufs(card, sb);
					atomic_inc(&vcc->stats->rx_drop);
				} else {
					skb_put(sb, len);
					dequeue_sm_buf(card, sb);
L
Linus Torvalds 已提交
2208
#ifdef NS_USE_DESTRUCTORS
2209
					sb->destructor = ns_sb_destructor;
L
Linus Torvalds 已提交
2210
#endif /* NS_USE_DESTRUCTORS */
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
					ATM_SKB(sb)->vcc = vcc;
					__net_timestamp(sb);
					vcc->push(vcc, sb);
					atomic_inc(&vcc->stats->rx);
				}

				push_rxbufs(card, skb);

			} else {	/* len > NS_SMBUFSIZE, the usual case */

				if (!atm_charge(vcc, skb->truesize)) {
					push_rxbufs(card, skb);
					atomic_inc(&vcc->stats->rx_drop);
				} else {
					dequeue_lg_buf(card, skb);
L
Linus Torvalds 已提交
2226
#ifdef NS_USE_DESTRUCTORS
2227
					skb->destructor = ns_lb_destructor;
L
Linus Torvalds 已提交
2228
#endif /* NS_USE_DESTRUCTORS */
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260
					skb_push(skb, NS_SMBUFSIZE);
					skb_copy_from_linear_data(sb, skb->data,
								  NS_SMBUFSIZE);
					skb_put(skb, len - NS_SMBUFSIZE);
					ATM_SKB(skb)->vcc = vcc;
					__net_timestamp(skb);
					vcc->push(vcc, skb);
					atomic_inc(&vcc->stats->rx);
				}

				push_rxbufs(card, sb);

			}

		} else {	/* Must push a huge buffer */

			struct sk_buff *hb, *sb, *lb;
			int remaining, tocopy;
			int j;

			hb = skb_dequeue(&(card->hbpool.queue));
			if (hb == NULL) {	/* No buffers in the queue */

				hb = dev_alloc_skb(NS_HBUFSIZE);
				if (hb == NULL) {
					printk
					    ("nicstar%d: Out of huge buffers.\n",
					     card->index);
					atomic_inc(&vcc->stats->rx_drop);
					recycle_iovec_rx_bufs(card,
							      (struct iovec *)
							      iovb->data,
2261
							      NS_PRV_IOVCNT(iovb));
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
					vc->rx_iov = NULL;
					recycle_iov_buf(card, iovb);
					return;
				} else if (card->hbpool.count < card->hbnr.min) {
					struct sk_buff *new_hb;
					if ((new_hb =
					     dev_alloc_skb(NS_HBUFSIZE)) !=
					    NULL) {
						skb_queue_tail(&card->hbpool.
							       queue, new_hb);
						card->hbpool.count++;
					}
				}
2275
				NS_PRV_BUFTYPE(hb) = BUF_NONE;
2276 2277 2278 2279
			} else if (--card->hbpool.count < card->hbnr.min) {
				struct sk_buff *new_hb;
				if ((new_hb =
				     dev_alloc_skb(NS_HBUFSIZE)) != NULL) {
2280
					NS_PRV_BUFTYPE(new_hb) = BUF_NONE;
2281 2282 2283 2284 2285 2286 2287 2288
					skb_queue_tail(&card->hbpool.queue,
						       new_hb);
					card->hbpool.count++;
				}
				if (card->hbpool.count < card->hbnr.min) {
					if ((new_hb =
					     dev_alloc_skb(NS_HBUFSIZE)) !=
					    NULL) {
2289
						NS_PRV_BUFTYPE(new_hb) =
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301
						    BUF_NONE;
						skb_queue_tail(&card->hbpool.
							       queue, new_hb);
						card->hbpool.count++;
					}
				}
			}

			iov = (struct iovec *)iovb->data;

			if (!atm_charge(vcc, hb->truesize)) {
				recycle_iovec_rx_bufs(card, iov,
2302
						      NS_PRV_IOVCNT(iovb));
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320
				if (card->hbpool.count < card->hbnr.max) {
					skb_queue_tail(&card->hbpool.queue, hb);
					card->hbpool.count++;
				} else
					dev_kfree_skb_any(hb);
				atomic_inc(&vcc->stats->rx_drop);
			} else {
				/* Copy the small buffer to the huge buffer */
				sb = (struct sk_buff *)iov->iov_base;
				skb_copy_from_linear_data(sb, hb->data,
							  iov->iov_len);
				skb_put(hb, iov->iov_len);
				remaining = len - iov->iov_len;
				iov++;
				/* Free the small buffer */
				push_rxbufs(card, sb);

				/* Copy all large buffers to the huge buffer and free them */
2321
				for (j = 1; j < NS_PRV_IOVCNT(iovb); j++) {
2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332
					lb = (struct sk_buff *)iov->iov_base;
					tocopy =
					    min_t(int, remaining, iov->iov_len);
					skb_copy_from_linear_data(lb,
								  skb_tail_pointer
								  (hb), tocopy);
					skb_put(hb, tocopy);
					iov++;
					remaining -= tocopy;
					push_rxbufs(card, lb);
				}
L
Linus Torvalds 已提交
2333
#ifdef EXTRA_DEBUG
2334 2335 2336 2337
				if (remaining != 0 || hb->len != len)
					printk
					    ("nicstar%d: Huge buffer len mismatch.\n",
					     card->index);
L
Linus Torvalds 已提交
2338
#endif /* EXTRA_DEBUG */
2339
				ATM_SKB(hb)->vcc = vcc;
L
Linus Torvalds 已提交
2340
#ifdef NS_USE_DESTRUCTORS
2341
				hb->destructor = ns_hb_destructor;
L
Linus Torvalds 已提交
2342
#endif /* NS_USE_DESTRUCTORS */
2343 2344 2345 2346 2347
				__net_timestamp(hb);
				vcc->push(vcc, hb);
				atomic_inc(&vcc->stats->rx);
			}
		}
L
Linus Torvalds 已提交
2348

2349 2350 2351
		vc->rx_iov = NULL;
		recycle_iov_buf(card, iovb);
	}
L
Linus Torvalds 已提交
2352 2353 2354 2355 2356 2357 2358

}

#ifdef NS_USE_DESTRUCTORS

static void ns_sb_destructor(struct sk_buff *sb)
{
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
	ns_dev *card;
	u32 stat;

	card = (ns_dev *) ATM_SKB(sb)->vcc->dev->dev_data;
	stat = readl(card->membase + STAT);
	card->sbfqc = ns_stat_sfbqc_get(stat);
	card->lbfqc = ns_stat_lfbqc_get(stat);

	do {
		sb = __dev_alloc_skb(NS_SMSKBSIZE, GFP_KERNEL);
		if (sb == NULL)
			break;
2371
		NS_PRV_BUFTYPE(sb) = BUF_SM;
2372 2373 2374 2375
		skb_queue_tail(&card->sbpool.queue, sb);
		skb_reserve(sb, NS_AAL0_HEADER);
		push_rxbufs(card, sb);
	} while (card->sbfqc < card->sbnr.min);
L
Linus Torvalds 已提交
2376 2377 2378 2379
}

static void ns_lb_destructor(struct sk_buff *lb)
{
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
	ns_dev *card;
	u32 stat;

	card = (ns_dev *) ATM_SKB(lb)->vcc->dev->dev_data;
	stat = readl(card->membase + STAT);
	card->sbfqc = ns_stat_sfbqc_get(stat);
	card->lbfqc = ns_stat_lfbqc_get(stat);

	do {
		lb = __dev_alloc_skb(NS_LGSKBSIZE, GFP_KERNEL);
		if (lb == NULL)
			break;
2392
		NS_PRV_BUFTYPE(lb) = BUF_LG;
2393 2394 2395 2396
		skb_queue_tail(&card->lbpool.queue, lb);
		skb_reserve(lb, NS_SMBUFSIZE);
		push_rxbufs(card, lb);
	} while (card->lbfqc < card->lbnr.min);
L
Linus Torvalds 已提交
2397 2398 2399 2400
}

static void ns_hb_destructor(struct sk_buff *hb)
{
2401 2402 2403 2404 2405 2406 2407 2408
	ns_dev *card;

	card = (ns_dev *) ATM_SKB(hb)->vcc->dev->dev_data;

	while (card->hbpool.count < card->hbnr.init) {
		hb = __dev_alloc_skb(NS_HBUFSIZE, GFP_KERNEL);
		if (hb == NULL)
			break;
2409
		NS_PRV_BUFTYPE(hb) = BUF_NONE;
2410 2411 2412
		skb_queue_tail(&card->hbpool.queue, hb);
		card->hbpool.count++;
	}
L
Linus Torvalds 已提交
2413 2414 2415 2416
}

#endif /* NS_USE_DESTRUCTORS */

2417
static void recycle_rx_buf(ns_dev * card, struct sk_buff *skb)
L
Linus Torvalds 已提交
2418
{
2419
	if (unlikely(NS_PRV_BUFTYPE(skb) == BUF_NONE)) {
2420 2421
		printk("nicstar%d: What kind of rx buffer is this?\n",
		       card->index);
D
David S. Miller 已提交
2422 2423 2424 2425
		dev_kfree_skb_any(skb);
	} else
		push_rxbufs(card, skb);
}
L
Linus Torvalds 已提交
2426

2427
static void recycle_iovec_rx_bufs(ns_dev * card, struct iovec *iov, int count)
L
Linus Torvalds 已提交
2428
{
D
David S. Miller 已提交
2429
	while (count-- > 0)
2430
		recycle_rx_buf(card, (struct sk_buff *)(iov++)->iov_base);
L
Linus Torvalds 已提交
2431 2432
}

2433
static void recycle_iov_buf(ns_dev * card, struct sk_buff *iovb)
L
Linus Torvalds 已提交
2434
{
2435 2436 2437 2438 2439
	if (card->iovpool.count < card->iovnr.max) {
		skb_queue_tail(&card->iovpool.queue, iovb);
		card->iovpool.count++;
	} else
		dev_kfree_skb_any(iovb);
L
Linus Torvalds 已提交
2440 2441
}

2442
static void dequeue_sm_buf(ns_dev * card, struct sk_buff *sb)
L
Linus Torvalds 已提交
2443
{
2444
	skb_unlink(sb, &card->sbpool.queue);
L
Linus Torvalds 已提交
2445
#ifdef NS_USE_DESTRUCTORS
2446
	if (card->sbfqc < card->sbnr.min)
L
Linus Torvalds 已提交
2447
#else
2448 2449 2450
	if (card->sbfqc < card->sbnr.init) {
		struct sk_buff *new_sb;
		if ((new_sb = dev_alloc_skb(NS_SMSKBSIZE)) != NULL) {
2451
			NS_PRV_BUFTYPE(new_sb) = BUF_SM;
2452 2453 2454 2455 2456 2457
			skb_queue_tail(&card->sbpool.queue, new_sb);
			skb_reserve(new_sb, NS_AAL0_HEADER);
			push_rxbufs(card, new_sb);
		}
	}
	if (card->sbfqc < card->sbnr.init)
L
Linus Torvalds 已提交
2458
#endif /* NS_USE_DESTRUCTORS */
2459 2460 2461
	{
		struct sk_buff *new_sb;
		if ((new_sb = dev_alloc_skb(NS_SMSKBSIZE)) != NULL) {
2462
			NS_PRV_BUFTYPE(new_sb) = BUF_SM;
2463 2464 2465 2466 2467
			skb_queue_tail(&card->sbpool.queue, new_sb);
			skb_reserve(new_sb, NS_AAL0_HEADER);
			push_rxbufs(card, new_sb);
		}
	}
L
Linus Torvalds 已提交
2468 2469
}

2470
static void dequeue_lg_buf(ns_dev * card, struct sk_buff *lb)
L
Linus Torvalds 已提交
2471
{
2472
	skb_unlink(lb, &card->lbpool.queue);
L
Linus Torvalds 已提交
2473
#ifdef NS_USE_DESTRUCTORS
2474
	if (card->lbfqc < card->lbnr.min)
L
Linus Torvalds 已提交
2475
#else
2476 2477 2478
	if (card->lbfqc < card->lbnr.init) {
		struct sk_buff *new_lb;
		if ((new_lb = dev_alloc_skb(NS_LGSKBSIZE)) != NULL) {
2479
			NS_PRV_BUFTYPE(new_lb) = BUF_LG;
2480 2481 2482 2483 2484 2485
			skb_queue_tail(&card->lbpool.queue, new_lb);
			skb_reserve(new_lb, NS_SMBUFSIZE);
			push_rxbufs(card, new_lb);
		}
	}
	if (card->lbfqc < card->lbnr.init)
L
Linus Torvalds 已提交
2486
#endif /* NS_USE_DESTRUCTORS */
2487 2488 2489
	{
		struct sk_buff *new_lb;
		if ((new_lb = dev_alloc_skb(NS_LGSKBSIZE)) != NULL) {
2490
			NS_PRV_BUFTYPE(new_lb) = BUF_LG;
2491 2492 2493 2494 2495
			skb_queue_tail(&card->lbpool.queue, new_lb);
			skb_reserve(new_lb, NS_SMBUFSIZE);
			push_rxbufs(card, new_lb);
		}
	}
L
Linus Torvalds 已提交
2496 2497
}

2498
static int ns_proc_read(struct atm_dev *dev, loff_t * pos, char *page)
L
Linus Torvalds 已提交
2499
{
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
	u32 stat;
	ns_dev *card;
	int left;

	left = (int)*pos;
	card = (ns_dev *) dev->dev_data;
	stat = readl(card->membase + STAT);
	if (!left--)
		return sprintf(page, "Pool   count    min   init    max \n");
	if (!left--)
		return sprintf(page, "Small  %5d  %5d  %5d  %5d \n",
			       ns_stat_sfbqc_get(stat), card->sbnr.min,
			       card->sbnr.init, card->sbnr.max);
	if (!left--)
		return sprintf(page, "Large  %5d  %5d  %5d  %5d \n",
			       ns_stat_lfbqc_get(stat), card->lbnr.min,
			       card->lbnr.init, card->lbnr.max);
	if (!left--)
		return sprintf(page, "Huge   %5d  %5d  %5d  %5d \n",
			       card->hbpool.count, card->hbnr.min,
			       card->hbnr.init, card->hbnr.max);
	if (!left--)
		return sprintf(page, "Iovec  %5d  %5d  %5d  %5d \n",
			       card->iovpool.count, card->iovnr.min,
			       card->iovnr.init, card->iovnr.max);
	if (!left--) {
		int retval;
		retval =
		    sprintf(page, "Interrupt counter: %u \n", card->intcnt);
		card->intcnt = 0;
		return retval;
	}
L
Linus Torvalds 已提交
2532
#if 0
2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551
	/* Dump 25.6 Mbps PHY registers */
	/* Now there's a 25.6 Mbps PHY driver this code isn't needed. I left it
	   here just in case it's needed for debugging. */
	if (card->max_pcr == ATM_25_PCR && !left--) {
		u32 phy_regs[4];
		u32 i;

		for (i = 0; i < 4; i++) {
			while (CMD_BUSY(card)) ;
			writel(NS_CMD_READ_UTILITY | 0x00000200 | i,
			       card->membase + CMD);
			while (CMD_BUSY(card)) ;
			phy_regs[i] = readl(card->membase + DR0) & 0x000000FF;
		}

		return sprintf(page, "PHY regs: 0x%02X 0x%02X 0x%02X 0x%02X \n",
			       phy_regs[0], phy_regs[1], phy_regs[2],
			       phy_regs[3]);
	}
L
Linus Torvalds 已提交
2552 2553
#endif /* 0 - Dump 25.6 Mbps PHY registers */
#if 0
2554 2555 2556 2557 2558 2559 2560 2561 2562
	/* Dump TST */
	if (left-- < NS_TST_NUM_ENTRIES) {
		if (card->tste2vc[left + 1] == NULL)
			return sprintf(page, "%5d - VBR/UBR \n", left + 1);
		else
			return sprintf(page, "%5d - %d %d \n", left + 1,
				       card->tste2vc[left + 1]->tx_vcc->vpi,
				       card->tste2vc[left + 1]->tx_vcc->vci);
	}
L
Linus Torvalds 已提交
2563
#endif /* 0 */
2564
	return 0;
L
Linus Torvalds 已提交
2565 2566
}

2567
static int ns_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg)
L
Linus Torvalds 已提交
2568
{
2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
	ns_dev *card;
	pool_levels pl;
	long btype;
	unsigned long flags;

	card = dev->dev_data;
	switch (cmd) {
	case NS_GETPSTAT:
		if (get_user
		    (pl.buftype, &((pool_levels __user *) arg)->buftype))
			return -EFAULT;
		switch (pl.buftype) {
		case NS_BUFTYPE_SMALL:
			pl.count =
			    ns_stat_sfbqc_get(readl(card->membase + STAT));
			pl.level.min = card->sbnr.min;
			pl.level.init = card->sbnr.init;
			pl.level.max = card->sbnr.max;
			break;

		case NS_BUFTYPE_LARGE:
			pl.count =
			    ns_stat_lfbqc_get(readl(card->membase + STAT));
			pl.level.min = card->lbnr.min;
			pl.level.init = card->lbnr.init;
			pl.level.max = card->lbnr.max;
			break;

		case NS_BUFTYPE_HUGE:
			pl.count = card->hbpool.count;
			pl.level.min = card->hbnr.min;
			pl.level.init = card->hbnr.init;
			pl.level.max = card->hbnr.max;
			break;

		case NS_BUFTYPE_IOVEC:
			pl.count = card->iovpool.count;
			pl.level.min = card->iovnr.min;
			pl.level.init = card->iovnr.init;
			pl.level.max = card->iovnr.max;
			break;

		default:
			return -ENOIOCTLCMD;

		}
		if (!copy_to_user((pool_levels __user *) arg, &pl, sizeof(pl)))
			return (sizeof(pl));
		else
			return -EFAULT;

	case NS_SETBUFLEV:
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		if (copy_from_user(&pl, (pool_levels __user *) arg, sizeof(pl)))
			return -EFAULT;
		if (pl.level.min >= pl.level.init
		    || pl.level.init >= pl.level.max)
			return -EINVAL;
		if (pl.level.min == 0)
			return -EINVAL;
		switch (pl.buftype) {
		case NS_BUFTYPE_SMALL:
			if (pl.level.max > TOP_SB)
				return -EINVAL;
			card->sbnr.min = pl.level.min;
			card->sbnr.init = pl.level.init;
			card->sbnr.max = pl.level.max;
			break;

		case NS_BUFTYPE_LARGE:
			if (pl.level.max > TOP_LB)
				return -EINVAL;
			card->lbnr.min = pl.level.min;
			card->lbnr.init = pl.level.init;
			card->lbnr.max = pl.level.max;
			break;

		case NS_BUFTYPE_HUGE:
			if (pl.level.max > TOP_HB)
				return -EINVAL;
			card->hbnr.min = pl.level.min;
			card->hbnr.init = pl.level.init;
			card->hbnr.max = pl.level.max;
			break;

		case NS_BUFTYPE_IOVEC:
			if (pl.level.max > TOP_IOVB)
				return -EINVAL;
			card->iovnr.min = pl.level.min;
			card->iovnr.init = pl.level.init;
			card->iovnr.max = pl.level.max;
			break;

		default:
			return -EINVAL;

		}
		return 0;

	case NS_ADJBUFLEV:
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		btype = (long)arg;	/* a long is the same size as a pointer or bigger */
		switch (btype) {
		case NS_BUFTYPE_SMALL:
			while (card->sbfqc < card->sbnr.init) {
				struct sk_buff *sb;

				sb = __dev_alloc_skb(NS_SMSKBSIZE, GFP_KERNEL);
				if (sb == NULL)
					return -ENOMEM;
2681
				NS_PRV_BUFTYPE(sb) = BUF_SM;
2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
				skb_queue_tail(&card->sbpool.queue, sb);
				skb_reserve(sb, NS_AAL0_HEADER);
				push_rxbufs(card, sb);
			}
			break;

		case NS_BUFTYPE_LARGE:
			while (card->lbfqc < card->lbnr.init) {
				struct sk_buff *lb;

				lb = __dev_alloc_skb(NS_LGSKBSIZE, GFP_KERNEL);
				if (lb == NULL)
					return -ENOMEM;
2695
				NS_PRV_BUFTYPE(lb) = BUF_LG;
2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
				skb_queue_tail(&card->lbpool.queue, lb);
				skb_reserve(lb, NS_SMBUFSIZE);
				push_rxbufs(card, lb);
			}
			break;

		case NS_BUFTYPE_HUGE:
			while (card->hbpool.count > card->hbnr.init) {
				struct sk_buff *hb;

				spin_lock_irqsave(&card->int_lock, flags);
				hb = skb_dequeue(&card->hbpool.queue);
				card->hbpool.count--;
				spin_unlock_irqrestore(&card->int_lock, flags);
				if (hb == NULL)
					printk
					    ("nicstar%d: huge buffer count inconsistent.\n",
					     card->index);
				else
					dev_kfree_skb_any(hb);

			}
			while (card->hbpool.count < card->hbnr.init) {
				struct sk_buff *hb;

				hb = __dev_alloc_skb(NS_HBUFSIZE, GFP_KERNEL);
				if (hb == NULL)
					return -ENOMEM;
2724
				NS_PRV_BUFTYPE(hb) = BUF_NONE;
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753
				spin_lock_irqsave(&card->int_lock, flags);
				skb_queue_tail(&card->hbpool.queue, hb);
				card->hbpool.count++;
				spin_unlock_irqrestore(&card->int_lock, flags);
			}
			break;

		case NS_BUFTYPE_IOVEC:
			while (card->iovpool.count > card->iovnr.init) {
				struct sk_buff *iovb;

				spin_lock_irqsave(&card->int_lock, flags);
				iovb = skb_dequeue(&card->iovpool.queue);
				card->iovpool.count--;
				spin_unlock_irqrestore(&card->int_lock, flags);
				if (iovb == NULL)
					printk
					    ("nicstar%d: iovec buffer count inconsistent.\n",
					     card->index);
				else
					dev_kfree_skb_any(iovb);

			}
			while (card->iovpool.count < card->iovnr.init) {
				struct sk_buff *iovb;

				iovb = alloc_skb(NS_IOVBUFSIZE, GFP_KERNEL);
				if (iovb == NULL)
					return -ENOMEM;
2754
				NS_PRV_BUFTYPE(iovb) = BUF_NONE;
2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776
				spin_lock_irqsave(&card->int_lock, flags);
				skb_queue_tail(&card->iovpool.queue, iovb);
				card->iovpool.count++;
				spin_unlock_irqrestore(&card->int_lock, flags);
			}
			break;

		default:
			return -EINVAL;

		}
		return 0;

	default:
		if (dev->phy && dev->phy->ioctl) {
			return dev->phy->ioctl(dev, cmd, arg);
		} else {
			printk("nicstar%d: %s == NULL \n", card->index,
			       dev->phy ? "dev->phy->ioctl" : "dev->phy");
			return -ENOIOCTLCMD;
		}
	}
L
Linus Torvalds 已提交
2777 2778
}

2779
#ifdef EXTRA_DEBUG
2780
static void which_list(ns_dev * card, struct sk_buff *skb)
L
Linus Torvalds 已提交
2781
{
2782
	printk("skb buf_type: 0x%08x\n", NS_PRV_BUFTYPE(skb));
L
Linus Torvalds 已提交
2783
}
2784
#endif /* EXTRA_DEBUG */
L
Linus Torvalds 已提交
2785 2786 2787

static void ns_poll(unsigned long arg)
{
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816
	int i;
	ns_dev *card;
	unsigned long flags;
	u32 stat_r, stat_w;

	PRINTK("nicstar: Entering ns_poll().\n");
	for (i = 0; i < num_cards; i++) {
		card = cards[i];
		if (spin_is_locked(&card->int_lock)) {
			/* Probably it isn't worth spinning */
			continue;
		}
		spin_lock_irqsave(&card->int_lock, flags);

		stat_w = 0;
		stat_r = readl(card->membase + STAT);
		if (stat_r & NS_STAT_TSIF)
			stat_w |= NS_STAT_TSIF;
		if (stat_r & NS_STAT_EOPDU)
			stat_w |= NS_STAT_EOPDU;

		process_tsq(card);
		process_rsq(card);

		writel(stat_w, card->membase + STAT);
		spin_unlock_irqrestore(&card->int_lock, flags);
	}
	mod_timer(&ns_timer, jiffies + NS_POLL_PERIOD);
	PRINTK("nicstar: Leaving ns_poll().\n");
L
Linus Torvalds 已提交
2817 2818 2819 2820
}

static int ns_parse_mac(char *mac, unsigned char *esi)
{
2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
	int i, j;
	short byte1, byte0;

	if (mac == NULL || esi == NULL)
		return -1;
	j = 0;
	for (i = 0; i < 6; i++) {
		if ((byte1 = ns_h2i(mac[j++])) < 0)
			return -1;
		if ((byte0 = ns_h2i(mac[j++])) < 0)
			return -1;
		esi[i] = (unsigned char)(byte1 * 16 + byte0);
		if (i < 5) {
			if (mac[j++] != ':')
				return -1;
		}
	}
	return 0;
L
Linus Torvalds 已提交
2839 2840 2841 2842
}

static short ns_h2i(char c)
{
2843 2844 2845 2846 2847 2848 2849
	if (c >= '0' && c <= '9')
		return (short)(c - '0');
	if (c >= 'A' && c <= 'F')
		return (short)(c - 'A' + 10);
	if (c >= 'a' && c <= 'f')
		return (short)(c - 'a' + 10);
	return -1;
L
Linus Torvalds 已提交
2850 2851 2852
}

static void ns_phy_put(struct atm_dev *dev, unsigned char value,
2853
		       unsigned long addr)
L
Linus Torvalds 已提交
2854
{
2855 2856 2857 2858 2859 2860
	ns_dev *card;
	unsigned long flags;

	card = dev->dev_data;
	spin_lock_irqsave(&card->res_lock, flags);
	while (CMD_BUSY(card)) ;
2861
	writel((u32) value, card->membase + DR0);
2862 2863 2864
	writel(NS_CMD_WRITE_UTILITY | 0x00000200 | (addr & 0x000000FF),
	       card->membase + CMD);
	spin_unlock_irqrestore(&card->res_lock, flags);
L
Linus Torvalds 已提交
2865 2866 2867 2868
}

static unsigned char ns_phy_get(struct atm_dev *dev, unsigned long addr)
{
2869 2870
	ns_dev *card;
	unsigned long flags;
2871
	u32 data;
2872 2873 2874 2875 2876 2877 2878 2879 2880 2881

	card = dev->dev_data;
	spin_lock_irqsave(&card->res_lock, flags);
	while (CMD_BUSY(card)) ;
	writel(NS_CMD_READ_UTILITY | 0x00000200 | (addr & 0x000000FF),
	       card->membase + CMD);
	while (CMD_BUSY(card)) ;
	data = readl(card->membase + DR0) & 0x000000FF;
	spin_unlock_irqrestore(&card->res_lock, flags);
	return (unsigned char)data;
L
Linus Torvalds 已提交
2882 2883 2884 2885
}

module_init(nicstar_init);
module_exit(nicstar_cleanup);