4 * Device driver supporting CBR for IDT 77201/77211 "NICStAR" based cards.
6 * IMPORTANT: The included file nicstarmac.c was NOT WRITTEN BY ME.
7 * It was taken from the frle-0.22 device driver.
8 * As the file doesn't have a copyright notice, in the file
9 * nicstarmac.copyright I put the copyright notice from the
10 * frle-0.22 device driver.
11 * Some code is based on the nicstar driver by M. Welsh.
13 * Author: Rui Prior (rprior@inescn.pt)
14 * PowerPC support by Jay Talbott (jay_talbott@mcg.mot.com) April 1999
21 * IMPORTANT INFORMATION
23 * There are currently three types of spinlocks:
25 * 1 - Per card interrupt spinlock (to protect structures and such)
26 * 2 - Per SCQ scq spinlock
27 * 3 - Per card resource spinlock (to access registers, etc.)
29 * These must NEVER be grabbed in reverse order.
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/skbuff.h>
38 #include <linux/atmdev.h>
39 #include <linux/atm.h>
40 #include <linux/pci.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/types.h>
43 #include <linux/string.h>
44 #include <linux/delay.h>
45 #include <linux/init.h>
46 #include <linux/sched.h>
47 #include <linux/timer.h>
48 #include <linux/interrupt.h>
49 #include <linux/bitops.h>
50 #include <linux/slab.h>
51 #include <linux/idr.h>
53 #include <linux/uaccess.h>
54 #include <linux/atomic.h>
55 #include <linux/etherdevice.h>
57 #ifdef CONFIG_ATM_NICSTAR_USE_SUNI
59 #endif /* CONFIG_ATM_NICSTAR_USE_SUNI */
60 #ifdef CONFIG_ATM_NICSTAR_USE_IDT77105
62 #endif /* CONFIG_ATM_NICSTAR_USE_IDT77105 */
66 #include "nicstarmac.c"
68 /* Configurable parameters */
76 /* Do not touch these */
79 #define TXPRINTK(args...) printk(args)
81 #define TXPRINTK(args...)
85 #define RXPRINTK(args...) printk(args)
87 #define RXPRINTK(args...)
91 #define PRINTK(args...) printk(args)
93 #define PRINTK(args...)
94 #endif /* GENERAL_DEBUG */
97 #define XPRINTK(args...) printk(args)
99 #define XPRINTK(args...)
100 #endif /* EXTRA_DEBUG */
104 #define CMD_BUSY(card) (readl((card)->membase + STAT) & NS_STAT_CMDBZ)
106 #define NS_DELAY mdelay(1)
108 #define PTR_DIFF(a, b) ((u32)((unsigned long)(a) - (unsigned long)(b)))
111 #define ATM_SKB(s) (&(s)->atm)
114 #define scq_virt_to_bus(scq, p) \
115 (scq->dma + ((unsigned long)(p) - (unsigned long)(scq)->org))
117 /* Function declarations */
119 static u32 ns_read_sram(ns_dev * card, u32 sram_address);
120 static void ns_write_sram(ns_dev * card, u32 sram_address, u32 * value,
122 static int ns_init_card(int i, struct pci_dev *pcidev);
123 static void ns_init_card_error(ns_dev * card, int error);
124 static scq_info *get_scq(ns_dev *card, int size, u32 scd);
125 static void free_scq(ns_dev *card, scq_info * scq, struct atm_vcc *vcc);
126 static void push_rxbufs(ns_dev *, struct sk_buff *);
127 static irqreturn_t ns_irq_handler(int irq, void *dev_id);
128 static int ns_open(struct atm_vcc *vcc);
129 static void ns_close(struct atm_vcc *vcc);
130 static void fill_tst(ns_dev * card, int n, vc_map * vc);
131 static int ns_send(struct atm_vcc *vcc, struct sk_buff *skb);
132 static int push_scqe(ns_dev * card, vc_map * vc, scq_info * scq, ns_scqe * tbd,
133 struct sk_buff *skb);
134 static void process_tsq(ns_dev * card);
135 static void drain_scq(ns_dev * card, scq_info * scq, int pos);
136 static void process_rsq(ns_dev * card);
137 static void dequeue_rx(ns_dev * card, ns_rsqe * rsqe);
138 static void recycle_rx_buf(ns_dev * card, struct sk_buff *skb);
139 static void recycle_iovec_rx_bufs(ns_dev * card, struct iovec *iov, int count);
140 static void recycle_iov_buf(ns_dev * card, struct sk_buff *iovb);
141 static void dequeue_sm_buf(ns_dev * card, struct sk_buff *sb);
142 static void dequeue_lg_buf(ns_dev * card, struct sk_buff *lb);
143 static int ns_proc_read(struct atm_dev *dev, loff_t * pos, char *page);
144 static int ns_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg);
146 static void which_list(ns_dev * card, struct sk_buff *skb);
148 static void ns_poll(unsigned long arg);
149 static void ns_phy_put(struct atm_dev *dev, unsigned char value,
151 static unsigned char ns_phy_get(struct atm_dev *dev, unsigned long addr);
153 /* Global variables */
155 static struct ns_dev *cards[NS_MAX_CARDS];
156 static unsigned num_cards;
157 static struct atmdev_ops atm_ops = {
162 .phy_put = ns_phy_put,
163 .phy_get = ns_phy_get,
164 .proc_read = ns_proc_read,
165 .owner = THIS_MODULE,
168 static struct timer_list ns_timer;
169 static char *mac[NS_MAX_CARDS];
170 module_param_array(mac, charp, NULL, 0);
171 MODULE_LICENSE("GPL");
175 static int nicstar_init_one(struct pci_dev *pcidev,
176 const struct pci_device_id *ent)
178 static int index = -1;
184 error = ns_init_card(index, pcidev);
186 cards[index--] = NULL; /* don't increment index */
195 static void nicstar_remove_one(struct pci_dev *pcidev)
198 ns_dev *card = pci_get_drvdata(pcidev);
200 struct sk_buff *iovb;
206 if (cards[i] == NULL)
209 if (card->atmdev->phy && card->atmdev->phy->stop)
210 card->atmdev->phy->stop(card->atmdev);
212 /* Stop everything */
213 writel(0x00000000, card->membase + CFG);
215 /* De-register device */
216 atm_dev_deregister(card->atmdev);
218 /* Disable PCI device */
219 pci_disable_device(pcidev);
221 /* Free up resources */
223 PRINTK("nicstar%d: freeing %d huge buffers.\n", i, card->hbpool.count);
224 while ((hb = skb_dequeue(&card->hbpool.queue)) != NULL) {
225 dev_kfree_skb_any(hb);
228 PRINTK("nicstar%d: %d huge buffers freed.\n", i, j);
230 PRINTK("nicstar%d: freeing %d iovec buffers.\n", i,
231 card->iovpool.count);
232 while ((iovb = skb_dequeue(&card->iovpool.queue)) != NULL) {
233 dev_kfree_skb_any(iovb);
236 PRINTK("nicstar%d: %d iovec buffers freed.\n", i, j);
237 while ((lb = skb_dequeue(&card->lbpool.queue)) != NULL)
238 dev_kfree_skb_any(lb);
239 while ((sb = skb_dequeue(&card->sbpool.queue)) != NULL)
240 dev_kfree_skb_any(sb);
241 free_scq(card, card->scq0, NULL);
242 for (j = 0; j < NS_FRSCD_NUM; j++) {
243 if (card->scd2vc[j] != NULL)
244 free_scq(card, card->scd2vc[j]->scq, card->scd2vc[j]->tx_vcc);
246 idr_destroy(&card->idr);
247 dma_free_coherent(&card->pcidev->dev, NS_RSQSIZE + NS_RSQ_ALIGNMENT,
248 card->rsq.org, card->rsq.dma);
249 dma_free_coherent(&card->pcidev->dev, NS_TSQSIZE + NS_TSQ_ALIGNMENT,
250 card->tsq.org, card->tsq.dma);
251 free_irq(card->pcidev->irq, card);
252 iounmap(card->membase);
256 static const struct pci_device_id nicstar_pci_tbl[] = {
257 { PCI_VDEVICE(IDT, PCI_DEVICE_ID_IDT_IDT77201), 0 },
258 {0,} /* terminate list */
261 MODULE_DEVICE_TABLE(pci, nicstar_pci_tbl);
263 static struct pci_driver nicstar_driver = {
265 .id_table = nicstar_pci_tbl,
266 .probe = nicstar_init_one,
267 .remove = nicstar_remove_one,
270 static int __init nicstar_init(void)
272 unsigned error = 0; /* Initialized to remove compile warning */
274 XPRINTK("nicstar: nicstar_init() called.\n");
276 error = pci_register_driver(&nicstar_driver);
278 TXPRINTK("nicstar: TX debug enabled.\n");
279 RXPRINTK("nicstar: RX debug enabled.\n");
280 PRINTK("nicstar: General debug enabled.\n");
282 printk("nicstar: using PHY loopback.\n");
283 #endif /* PHY_LOOPBACK */
284 XPRINTK("nicstar: nicstar_init() returned.\n");
287 init_timer(&ns_timer);
288 ns_timer.expires = jiffies + NS_POLL_PERIOD;
290 ns_timer.function = ns_poll;
291 add_timer(&ns_timer);
297 static void __exit nicstar_cleanup(void)
299 XPRINTK("nicstar: nicstar_cleanup() called.\n");
301 del_timer(&ns_timer);
303 pci_unregister_driver(&nicstar_driver);
305 XPRINTK("nicstar: nicstar_cleanup() returned.\n");
308 static u32 ns_read_sram(ns_dev * card, u32 sram_address)
313 sram_address &= 0x0007FFFC; /* address must be dword aligned */
314 sram_address |= 0x50000000; /* SRAM read command */
315 spin_lock_irqsave(&card->res_lock, flags);
316 while (CMD_BUSY(card)) ;
317 writel(sram_address, card->membase + CMD);
318 while (CMD_BUSY(card)) ;
319 data = readl(card->membase + DR0);
320 spin_unlock_irqrestore(&card->res_lock, flags);
324 static void ns_write_sram(ns_dev * card, u32 sram_address, u32 * value,
329 count--; /* count range now is 0..3 instead of 1..4 */
331 c <<= 2; /* to use increments of 4 */
332 spin_lock_irqsave(&card->res_lock, flags);
333 while (CMD_BUSY(card)) ;
334 for (i = 0; i <= c; i += 4)
335 writel(*(value++), card->membase + i);
336 /* Note: DR# registers are the first 4 dwords in nicstar's memspace,
337 so card->membase + DR0 == card->membase */
339 sram_address &= 0x0007FFFC;
340 sram_address |= (0x40000000 | count);
341 writel(sram_address, card->membase + CMD);
342 spin_unlock_irqrestore(&card->res_lock, flags);
345 static int ns_init_card(int i, struct pci_dev *pcidev)
348 struct ns_dev *card = NULL;
349 unsigned char pci_latency;
355 unsigned long membase;
359 if (pci_enable_device(pcidev)) {
360 printk("nicstar%d: can't enable PCI device\n", i);
362 ns_init_card_error(card, error);
365 if (dma_set_mask_and_coherent(&pcidev->dev, DMA_BIT_MASK(32)) != 0) {
367 "nicstar%d: No suitable DMA available.\n", i);
369 ns_init_card_error(card, error);
373 card = kmalloc(sizeof(*card), GFP_KERNEL);
376 ("nicstar%d: can't allocate memory for device structure.\n",
379 ns_init_card_error(card, error);
383 spin_lock_init(&card->int_lock);
384 spin_lock_init(&card->res_lock);
386 pci_set_drvdata(pcidev, card);
390 card->pcidev = pcidev;
391 membase = pci_resource_start(pcidev, 1);
392 card->membase = ioremap(membase, NS_IOREMAP_SIZE);
393 if (!card->membase) {
394 printk("nicstar%d: can't ioremap() membase.\n", i);
396 ns_init_card_error(card, error);
399 PRINTK("nicstar%d: membase at 0x%p.\n", i, card->membase);
401 pci_set_master(pcidev);
403 if (pci_read_config_byte(pcidev, PCI_LATENCY_TIMER, &pci_latency) != 0) {
404 printk("nicstar%d: can't read PCI latency timer.\n", i);
406 ns_init_card_error(card, error);
409 #ifdef NS_PCI_LATENCY
410 if (pci_latency < NS_PCI_LATENCY) {
411 PRINTK("nicstar%d: setting PCI latency timer to %d.\n", i,
413 for (j = 1; j < 4; j++) {
414 if (pci_write_config_byte
415 (pcidev, PCI_LATENCY_TIMER, NS_PCI_LATENCY) != 0)
420 ("nicstar%d: can't set PCI latency timer to %d.\n",
423 ns_init_card_error(card, error);
427 #endif /* NS_PCI_LATENCY */
429 /* Clear timer overflow */
430 data = readl(card->membase + STAT);
431 if (data & NS_STAT_TMROF)
432 writel(NS_STAT_TMROF, card->membase + STAT);
435 writel(NS_CFG_SWRST, card->membase + CFG);
437 writel(0x00000000, card->membase + CFG);
440 writel(0x00000008, card->membase + GP);
442 writel(0x00000001, card->membase + GP);
444 while (CMD_BUSY(card)) ;
445 writel(NS_CMD_WRITE_UTILITY | 0x00000100, card->membase + CMD); /* Sync UTOPIA with SAR clock */
448 /* Detect PHY type */
449 while (CMD_BUSY(card)) ;
450 writel(NS_CMD_READ_UTILITY | 0x00000200, card->membase + CMD);
451 while (CMD_BUSY(card)) ;
452 data = readl(card->membase + DR0);
455 printk("nicstar%d: PHY seems to be 25 Mbps.\n", i);
456 card->max_pcr = ATM_25_PCR;
457 while (CMD_BUSY(card)) ;
458 writel(0x00000008, card->membase + DR0);
459 writel(NS_CMD_WRITE_UTILITY | 0x00000200, card->membase + CMD);
460 /* Clear an eventual pending interrupt */
461 writel(NS_STAT_SFBQF, card->membase + STAT);
463 while (CMD_BUSY(card)) ;
464 writel(0x00000022, card->membase + DR0);
465 writel(NS_CMD_WRITE_UTILITY | 0x00000202, card->membase + CMD);
466 #endif /* PHY_LOOPBACK */
470 printk("nicstar%d: PHY seems to be 155 Mbps.\n", i);
471 card->max_pcr = ATM_OC3_PCR;
473 while (CMD_BUSY(card)) ;
474 writel(0x00000002, card->membase + DR0);
475 writel(NS_CMD_WRITE_UTILITY | 0x00000205, card->membase + CMD);
476 #endif /* PHY_LOOPBACK */
479 printk("nicstar%d: unknown PHY type (0x%08X).\n", i, data);
481 ns_init_card_error(card, error);
484 writel(0x00000000, card->membase + GP);
486 /* Determine SRAM size */
488 ns_write_sram(card, 0x1C003, &data, 1);
490 ns_write_sram(card, 0x14003, &data, 1);
491 if (ns_read_sram(card, 0x14003) == 0x89ABCDEF &&
492 ns_read_sram(card, 0x1C003) == 0x76543210)
493 card->sram_size = 128;
495 card->sram_size = 32;
496 PRINTK("nicstar%d: %dK x 32bit SRAM size.\n", i, card->sram_size);
498 card->rct_size = NS_MAX_RCTSIZE;
500 #if (NS_MAX_RCTSIZE == 4096)
501 if (card->sram_size == 128)
503 ("nicstar%d: limiting maximum VCI. See NS_MAX_RCTSIZE in nicstar.h\n",
505 #elif (NS_MAX_RCTSIZE == 16384)
506 if (card->sram_size == 32) {
508 ("nicstar%d: wasting memory. See NS_MAX_RCTSIZE in nicstar.h\n",
510 card->rct_size = 4096;
513 #error NS_MAX_RCTSIZE must be either 4096 or 16384 in nicstar.c
516 card->vpibits = NS_VPIBITS;
517 if (card->rct_size == 4096)
518 card->vcibits = 12 - NS_VPIBITS;
519 else /* card->rct_size == 16384 */
520 card->vcibits = 14 - NS_VPIBITS;
522 /* Initialize the nicstar eeprom/eprom stuff, for the MAC addr */
524 nicstar_init_eprom(card->membase);
526 /* Set the VPI/VCI MSb mask to zero so we can receive OAM cells */
527 writel(0x00000000, card->membase + VPM);
530 card->tsq.org = dma_alloc_coherent(&card->pcidev->dev,
531 NS_TSQSIZE + NS_TSQ_ALIGNMENT,
532 &card->tsq.dma, GFP_KERNEL);
533 if (card->tsq.org == NULL) {
534 printk("nicstar%d: can't allocate TSQ.\n", i);
536 ns_init_card_error(card, error);
539 card->tsq.base = PTR_ALIGN(card->tsq.org, NS_TSQ_ALIGNMENT);
540 card->tsq.next = card->tsq.base;
541 card->tsq.last = card->tsq.base + (NS_TSQ_NUM_ENTRIES - 1);
542 for (j = 0; j < NS_TSQ_NUM_ENTRIES; j++)
543 ns_tsi_init(card->tsq.base + j);
544 writel(0x00000000, card->membase + TSQH);
545 writel(ALIGN(card->tsq.dma, NS_TSQ_ALIGNMENT), card->membase + TSQB);
546 PRINTK("nicstar%d: TSQ base at 0x%p.\n", i, card->tsq.base);
549 card->rsq.org = dma_alloc_coherent(&card->pcidev->dev,
550 NS_RSQSIZE + NS_RSQ_ALIGNMENT,
551 &card->rsq.dma, GFP_KERNEL);
552 if (card->rsq.org == NULL) {
553 printk("nicstar%d: can't allocate RSQ.\n", i);
555 ns_init_card_error(card, error);
558 card->rsq.base = PTR_ALIGN(card->rsq.org, NS_RSQ_ALIGNMENT);
559 card->rsq.next = card->rsq.base;
560 card->rsq.last = card->rsq.base + (NS_RSQ_NUM_ENTRIES - 1);
561 for (j = 0; j < NS_RSQ_NUM_ENTRIES; j++)
562 ns_rsqe_init(card->rsq.base + j);
563 writel(0x00000000, card->membase + RSQH);
564 writel(ALIGN(card->rsq.dma, NS_RSQ_ALIGNMENT), card->membase + RSQB);
565 PRINTK("nicstar%d: RSQ base at 0x%p.\n", i, card->rsq.base);
567 /* Initialize SCQ0, the only VBR SCQ used */
570 card->scq0 = get_scq(card, VBR_SCQSIZE, NS_VRSCD0);
571 if (card->scq0 == NULL) {
572 printk("nicstar%d: can't get SCQ0.\n", i);
574 ns_init_card_error(card, error);
577 u32d[0] = scq_virt_to_bus(card->scq0, card->scq0->base);
578 u32d[1] = (u32) 0x00000000;
579 u32d[2] = (u32) 0xffffffff;
580 u32d[3] = (u32) 0x00000000;
581 ns_write_sram(card, NS_VRSCD0, u32d, 4);
582 ns_write_sram(card, NS_VRSCD1, u32d, 4); /* These last two won't be used */
583 ns_write_sram(card, NS_VRSCD2, u32d, 4); /* but are initialized, just in case... */
584 card->scq0->scd = NS_VRSCD0;
585 PRINTK("nicstar%d: VBR-SCQ0 base at 0x%p.\n", i, card->scq0->base);
587 /* Initialize TSTs */
588 card->tst_addr = NS_TST0;
589 card->tst_free_entries = NS_TST_NUM_ENTRIES;
590 data = NS_TST_OPCODE_VARIABLE;
591 for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
592 ns_write_sram(card, NS_TST0 + j, &data, 1);
593 data = ns_tste_make(NS_TST_OPCODE_END, NS_TST0);
594 ns_write_sram(card, NS_TST0 + NS_TST_NUM_ENTRIES, &data, 1);
595 for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
596 ns_write_sram(card, NS_TST1 + j, &data, 1);
597 data = ns_tste_make(NS_TST_OPCODE_END, NS_TST1);
598 ns_write_sram(card, NS_TST1 + NS_TST_NUM_ENTRIES, &data, 1);
599 for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
600 card->tste2vc[j] = NULL;
601 writel(NS_TST0 << 2, card->membase + TSTB);
603 /* Initialize RCT. AAL type is set on opening the VC. */
605 u32d[0] = NS_RCTE_RAWCELLINTEN;
607 u32d[0] = 0x00000000;
608 #endif /* RCQ_SUPPORT */
609 u32d[1] = 0x00000000;
610 u32d[2] = 0x00000000;
611 u32d[3] = 0xFFFFFFFF;
612 for (j = 0; j < card->rct_size; j++)
613 ns_write_sram(card, j * 4, u32d, 4);
615 memset(card->vcmap, 0, sizeof(card->vcmap));
617 for (j = 0; j < NS_FRSCD_NUM; j++)
618 card->scd2vc[j] = NULL;
620 /* Initialize buffer levels */
621 card->sbnr.min = MIN_SB;
622 card->sbnr.init = NUM_SB;
623 card->sbnr.max = MAX_SB;
624 card->lbnr.min = MIN_LB;
625 card->lbnr.init = NUM_LB;
626 card->lbnr.max = MAX_LB;
627 card->iovnr.min = MIN_IOVB;
628 card->iovnr.init = NUM_IOVB;
629 card->iovnr.max = MAX_IOVB;
630 card->hbnr.min = MIN_HB;
631 card->hbnr.init = NUM_HB;
632 card->hbnr.max = MAX_HB;
634 card->sm_handle = NULL;
635 card->sm_addr = 0x00000000;
636 card->lg_handle = NULL;
637 card->lg_addr = 0x00000000;
639 card->efbie = 1; /* To prevent push_rxbufs from enabling the interrupt */
641 idr_init(&card->idr);
643 /* Pre-allocate some huge buffers */
644 skb_queue_head_init(&card->hbpool.queue);
645 card->hbpool.count = 0;
646 for (j = 0; j < NUM_HB; j++) {
648 hb = __dev_alloc_skb(NS_HBUFSIZE, GFP_KERNEL);
651 ("nicstar%d: can't allocate %dth of %d huge buffers.\n",
654 ns_init_card_error(card, error);
657 NS_PRV_BUFTYPE(hb) = BUF_NONE;
658 skb_queue_tail(&card->hbpool.queue, hb);
659 card->hbpool.count++;
662 /* Allocate large buffers */
663 skb_queue_head_init(&card->lbpool.queue);
664 card->lbpool.count = 0; /* Not used */
665 for (j = 0; j < NUM_LB; j++) {
667 lb = __dev_alloc_skb(NS_LGSKBSIZE, GFP_KERNEL);
670 ("nicstar%d: can't allocate %dth of %d large buffers.\n",
673 ns_init_card_error(card, error);
676 NS_PRV_BUFTYPE(lb) = BUF_LG;
677 skb_queue_tail(&card->lbpool.queue, lb);
678 skb_reserve(lb, NS_SMBUFSIZE);
679 push_rxbufs(card, lb);
680 /* Due to the implementation of push_rxbufs() this is 1, not 0 */
683 card->rawcell = (struct ns_rcqe *) lb->data;
684 card->rawch = NS_PRV_DMA(lb);
687 /* Test for strange behaviour which leads to crashes */
689 ns_stat_lfbqc_get(readl(card->membase + STAT))) < card->lbnr.min) {
691 ("nicstar%d: Strange... Just allocated %d large buffers and lfbqc = %d.\n",
694 ns_init_card_error(card, error);
698 /* Allocate small buffers */
699 skb_queue_head_init(&card->sbpool.queue);
700 card->sbpool.count = 0; /* Not used */
701 for (j = 0; j < NUM_SB; j++) {
703 sb = __dev_alloc_skb(NS_SMSKBSIZE, GFP_KERNEL);
706 ("nicstar%d: can't allocate %dth of %d small buffers.\n",
709 ns_init_card_error(card, error);
712 NS_PRV_BUFTYPE(sb) = BUF_SM;
713 skb_queue_tail(&card->sbpool.queue, sb);
714 skb_reserve(sb, NS_AAL0_HEADER);
715 push_rxbufs(card, sb);
717 /* Test for strange behaviour which leads to crashes */
719 ns_stat_sfbqc_get(readl(card->membase + STAT))) < card->sbnr.min) {
721 ("nicstar%d: Strange... Just allocated %d small buffers and sfbqc = %d.\n",
724 ns_init_card_error(card, error);
728 /* Allocate iovec buffers */
729 skb_queue_head_init(&card->iovpool.queue);
730 card->iovpool.count = 0;
731 for (j = 0; j < NUM_IOVB; j++) {
732 struct sk_buff *iovb;
733 iovb = alloc_skb(NS_IOVBUFSIZE, GFP_KERNEL);
736 ("nicstar%d: can't allocate %dth of %d iovec buffers.\n",
739 ns_init_card_error(card, error);
742 NS_PRV_BUFTYPE(iovb) = BUF_NONE;
743 skb_queue_tail(&card->iovpool.queue, iovb);
744 card->iovpool.count++;
747 /* Configure NICStAR */
748 if (card->rct_size == 4096)
749 ns_cfg_rctsize = NS_CFG_RCTSIZE_4096_ENTRIES;
750 else /* (card->rct_size == 16384) */
751 ns_cfg_rctsize = NS_CFG_RCTSIZE_16384_ENTRIES;
757 (pcidev->irq, &ns_irq_handler, IRQF_SHARED, "nicstar", card) != 0) {
758 printk("nicstar%d: can't allocate IRQ %d.\n", i, pcidev->irq);
760 ns_init_card_error(card, error);
764 /* Register device */
765 card->atmdev = atm_dev_register("nicstar", &card->pcidev->dev, &atm_ops,
767 if (card->atmdev == NULL) {
768 printk("nicstar%d: can't register device.\n", i);
770 ns_init_card_error(card, error);
774 if (mac[i] == NULL || !mac_pton(mac[i], card->atmdev->esi)) {
775 nicstar_read_eprom(card->membase, NICSTAR_EPROM_MAC_ADDR_OFFSET,
776 card->atmdev->esi, 6);
777 if (ether_addr_equal(card->atmdev->esi, "\x00\x00\x00\x00\x00\x00")) {
778 nicstar_read_eprom(card->membase,
779 NICSTAR_EPROM_MAC_ADDR_OFFSET_ALT,
780 card->atmdev->esi, 6);
784 printk("nicstar%d: MAC address %pM\n", i, card->atmdev->esi);
786 card->atmdev->dev_data = card;
787 card->atmdev->ci_range.vpi_bits = card->vpibits;
788 card->atmdev->ci_range.vci_bits = card->vcibits;
789 card->atmdev->link_rate = card->max_pcr;
790 card->atmdev->phy = NULL;
792 #ifdef CONFIG_ATM_NICSTAR_USE_SUNI
793 if (card->max_pcr == ATM_OC3_PCR)
794 suni_init(card->atmdev);
795 #endif /* CONFIG_ATM_NICSTAR_USE_SUNI */
797 #ifdef CONFIG_ATM_NICSTAR_USE_IDT77105
798 if (card->max_pcr == ATM_25_PCR)
799 idt77105_init(card->atmdev);
800 #endif /* CONFIG_ATM_NICSTAR_USE_IDT77105 */
802 if (card->atmdev->phy && card->atmdev->phy->start)
803 card->atmdev->phy->start(card->atmdev);
805 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 */
806 NS_CFG_RSQAFIE | NS_CFG_TXEN | NS_CFG_TXIE | NS_CFG_TSQFIE_OPT | /* Only enabled if ENABLE_TSQFIE */
807 NS_CFG_PHYIE, card->membase + CFG);
814 static void ns_init_card_error(ns_dev *card, int error)
817 writel(0x00000000, card->membase + CFG);
820 struct sk_buff *iovb;
821 while ((iovb = skb_dequeue(&card->iovpool.queue)) != NULL)
822 dev_kfree_skb_any(iovb);
826 while ((sb = skb_dequeue(&card->sbpool.queue)) != NULL)
827 dev_kfree_skb_any(sb);
828 free_scq(card, card->scq0, NULL);
832 while ((lb = skb_dequeue(&card->lbpool.queue)) != NULL)
833 dev_kfree_skb_any(lb);
837 while ((hb = skb_dequeue(&card->hbpool.queue)) != NULL)
838 dev_kfree_skb_any(hb);
841 kfree(card->rsq.org);
844 kfree(card->tsq.org);
847 free_irq(card->pcidev->irq, card);
850 iounmap(card->membase);
853 pci_disable_device(card->pcidev);
858 static scq_info *get_scq(ns_dev *card, int size, u32 scd)
863 if (size != VBR_SCQSIZE && size != CBR_SCQSIZE)
866 scq = kmalloc(sizeof(*scq), GFP_KERNEL);
869 scq->org = dma_alloc_coherent(&card->pcidev->dev,
870 2 * size, &scq->dma, GFP_KERNEL);
875 scq->skb = kmalloc_array(size / NS_SCQE_SIZE,
879 dma_free_coherent(&card->pcidev->dev,
880 2 * size, scq->org, scq->dma);
884 scq->num_entries = size / NS_SCQE_SIZE;
885 scq->base = PTR_ALIGN(scq->org, size);
886 scq->next = scq->base;
887 scq->last = scq->base + (scq->num_entries - 1);
888 scq->tail = scq->last;
890 scq->num_entries = size / NS_SCQE_SIZE;
892 init_waitqueue_head(&scq->scqfull_waitq);
894 spin_lock_init(&scq->lock);
896 for (i = 0; i < scq->num_entries; i++)
902 /* For variable rate SCQ vcc must be NULL */
903 static void free_scq(ns_dev *card, scq_info *scq, struct atm_vcc *vcc)
907 if (scq->num_entries == VBR_SCQ_NUM_ENTRIES)
908 for (i = 0; i < scq->num_entries; i++) {
909 if (scq->skb[i] != NULL) {
910 vcc = ATM_SKB(scq->skb[i])->vcc;
911 if (vcc->pop != NULL)
912 vcc->pop(vcc, scq->skb[i]);
914 dev_kfree_skb_any(scq->skb[i]);
916 } else { /* vcc must be != NULL */
920 ("nicstar: free_scq() called with vcc == NULL for fixed rate scq.");
921 for (i = 0; i < scq->num_entries; i++)
922 dev_kfree_skb_any(scq->skb[i]);
924 for (i = 0; i < scq->num_entries; i++) {
925 if (scq->skb[i] != NULL) {
926 if (vcc->pop != NULL)
927 vcc->pop(vcc, scq->skb[i]);
929 dev_kfree_skb_any(scq->skb[i]);
934 dma_free_coherent(&card->pcidev->dev,
935 2 * (scq->num_entries == VBR_SCQ_NUM_ENTRIES ?
936 VBR_SCQSIZE : CBR_SCQSIZE),
941 /* The handles passed must be pointers to the sk_buff containing the small
942 or large buffer(s) cast to u32. */
943 static void push_rxbufs(ns_dev * card, struct sk_buff *skb)
945 struct sk_buff *handle1, *handle2;
955 addr1 = dma_map_single(&card->pcidev->dev,
957 (NS_PRV_BUFTYPE(skb) == BUF_SM
958 ? NS_SMSKBSIZE : NS_LGSKBSIZE),
960 NS_PRV_DMA(skb) = addr1; /* save so we can unmap later */
964 printk("nicstar%d: push_rxbufs called with addr1 = 0.\n",
966 #endif /* GENERAL_DEBUG */
968 stat = readl(card->membase + STAT);
969 card->sbfqc = ns_stat_sfbqc_get(stat);
970 card->lbfqc = ns_stat_lfbqc_get(stat);
971 if (NS_PRV_BUFTYPE(skb) == BUF_SM) {
974 addr2 = card->sm_addr;
975 handle2 = card->sm_handle;
976 card->sm_addr = 0x00000000;
977 card->sm_handle = NULL;
978 } else { /* (!sm_addr) */
980 card->sm_addr = addr1;
981 card->sm_handle = handle1;
984 } else { /* buf_type == BUF_LG */
988 addr2 = card->lg_addr;
989 handle2 = card->lg_handle;
990 card->lg_addr = 0x00000000;
991 card->lg_handle = NULL;
992 } else { /* (!lg_addr) */
994 card->lg_addr = addr1;
995 card->lg_handle = handle1;
1001 if (NS_PRV_BUFTYPE(skb) == BUF_SM) {
1002 if (card->sbfqc >= card->sbnr.max) {
1003 skb_unlink(handle1, &card->sbpool.queue);
1004 dev_kfree_skb_any(handle1);
1005 skb_unlink(handle2, &card->sbpool.queue);
1006 dev_kfree_skb_any(handle2);
1010 } else { /* (buf_type == BUF_LG) */
1012 if (card->lbfqc >= card->lbnr.max) {
1013 skb_unlink(handle1, &card->lbpool.queue);
1014 dev_kfree_skb_any(handle1);
1015 skb_unlink(handle2, &card->lbpool.queue);
1016 dev_kfree_skb_any(handle2);
1022 id1 = idr_alloc(&card->idr, handle1, 0, 0, GFP_ATOMIC);
1026 id2 = idr_alloc(&card->idr, handle2, 0, 0, GFP_ATOMIC);
1030 spin_lock_irqsave(&card->res_lock, flags);
1031 while (CMD_BUSY(card)) ;
1032 writel(addr2, card->membase + DR3);
1033 writel(id2, card->membase + DR2);
1034 writel(addr1, card->membase + DR1);
1035 writel(id1, card->membase + DR0);
1036 writel(NS_CMD_WRITE_FREEBUFQ | NS_PRV_BUFTYPE(skb),
1037 card->membase + CMD);
1038 spin_unlock_irqrestore(&card->res_lock, flags);
1040 XPRINTK("nicstar%d: Pushing %s buffers at 0x%x and 0x%x.\n",
1042 (NS_PRV_BUFTYPE(skb) == BUF_SM ? "small" : "large"),
1046 if (!card->efbie && card->sbfqc >= card->sbnr.min &&
1047 card->lbfqc >= card->lbnr.min) {
1049 writel((readl(card->membase + CFG) | NS_CFG_EFBIE),
1050 card->membase + CFG);
1057 static irqreturn_t ns_irq_handler(int irq, void *dev_id)
1061 struct atm_dev *dev;
1062 unsigned long flags;
1064 card = (ns_dev *) dev_id;
1068 PRINTK("nicstar%d: NICStAR generated an interrupt\n", card->index);
1070 spin_lock_irqsave(&card->int_lock, flags);
1072 stat_r = readl(card->membase + STAT);
1074 /* Transmit Status Indicator has been written to T. S. Queue */
1075 if (stat_r & NS_STAT_TSIF) {
1076 TXPRINTK("nicstar%d: TSI interrupt\n", card->index);
1078 writel(NS_STAT_TSIF, card->membase + STAT);
1081 /* Incomplete CS-PDU has been transmitted */
1082 if (stat_r & NS_STAT_TXICP) {
1083 writel(NS_STAT_TXICP, card->membase + STAT);
1084 TXPRINTK("nicstar%d: Incomplete CS-PDU transmitted.\n",
1088 /* Transmit Status Queue 7/8 full */
1089 if (stat_r & NS_STAT_TSQF) {
1090 writel(NS_STAT_TSQF, card->membase + STAT);
1091 PRINTK("nicstar%d: TSQ full.\n", card->index);
1095 /* Timer overflow */
1096 if (stat_r & NS_STAT_TMROF) {
1097 writel(NS_STAT_TMROF, card->membase + STAT);
1098 PRINTK("nicstar%d: Timer overflow.\n", card->index);
1101 /* PHY device interrupt signal active */
1102 if (stat_r & NS_STAT_PHYI) {
1103 writel(NS_STAT_PHYI, card->membase + STAT);
1104 PRINTK("nicstar%d: PHY interrupt.\n", card->index);
1105 if (dev->phy && dev->phy->interrupt) {
1106 dev->phy->interrupt(dev);
1110 /* Small Buffer Queue is full */
1111 if (stat_r & NS_STAT_SFBQF) {
1112 writel(NS_STAT_SFBQF, card->membase + STAT);
1113 printk("nicstar%d: Small free buffer queue is full.\n",
1117 /* Large Buffer Queue is full */
1118 if (stat_r & NS_STAT_LFBQF) {
1119 writel(NS_STAT_LFBQF, card->membase + STAT);
1120 printk("nicstar%d: Large free buffer queue is full.\n",
1124 /* Receive Status Queue is full */
1125 if (stat_r & NS_STAT_RSQF) {
1126 writel(NS_STAT_RSQF, card->membase + STAT);
1127 printk("nicstar%d: RSQ full.\n", card->index);
1131 /* Complete CS-PDU received */
1132 if (stat_r & NS_STAT_EOPDU) {
1133 RXPRINTK("nicstar%d: End of CS-PDU received.\n", card->index);
1135 writel(NS_STAT_EOPDU, card->membase + STAT);
1138 /* Raw cell received */
1139 if (stat_r & NS_STAT_RAWCF) {
1140 writel(NS_STAT_RAWCF, card->membase + STAT);
1142 printk("nicstar%d: Raw cell received and no support yet...\n",
1144 #endif /* RCQ_SUPPORT */
1145 /* NOTE: the following procedure may keep a raw cell pending until the
1146 next interrupt. As this preliminary support is only meant to
1147 avoid buffer leakage, this is not an issue. */
1148 while (readl(card->membase + RAWCT) != card->rawch) {
1150 if (ns_rcqe_islast(card->rawcell)) {
1151 struct sk_buff *oldbuf;
1153 oldbuf = card->rcbuf;
1154 card->rcbuf = idr_find(&card->idr,
1155 ns_rcqe_nextbufhandle(card->rawcell));
1156 card->rawch = NS_PRV_DMA(card->rcbuf);
1157 card->rawcell = (struct ns_rcqe *)
1159 recycle_rx_buf(card, oldbuf);
1161 card->rawch += NS_RCQE_SIZE;
1167 /* Small buffer queue is empty */
1168 if (stat_r & NS_STAT_SFBQE) {
1172 writel(NS_STAT_SFBQE, card->membase + STAT);
1173 printk("nicstar%d: Small free buffer queue empty.\n",
1175 for (i = 0; i < card->sbnr.min; i++) {
1176 sb = dev_alloc_skb(NS_SMSKBSIZE);
1178 writel(readl(card->membase + CFG) &
1179 ~NS_CFG_EFBIE, card->membase + CFG);
1183 NS_PRV_BUFTYPE(sb) = BUF_SM;
1184 skb_queue_tail(&card->sbpool.queue, sb);
1185 skb_reserve(sb, NS_AAL0_HEADER);
1186 push_rxbufs(card, sb);
1192 /* Large buffer queue empty */
1193 if (stat_r & NS_STAT_LFBQE) {
1197 writel(NS_STAT_LFBQE, card->membase + STAT);
1198 printk("nicstar%d: Large free buffer queue empty.\n",
1200 for (i = 0; i < card->lbnr.min; i++) {
1201 lb = dev_alloc_skb(NS_LGSKBSIZE);
1203 writel(readl(card->membase + CFG) &
1204 ~NS_CFG_EFBIE, card->membase + CFG);
1208 NS_PRV_BUFTYPE(lb) = BUF_LG;
1209 skb_queue_tail(&card->lbpool.queue, lb);
1210 skb_reserve(lb, NS_SMBUFSIZE);
1211 push_rxbufs(card, lb);
1217 /* Receive Status Queue is 7/8 full */
1218 if (stat_r & NS_STAT_RSQAF) {
1219 writel(NS_STAT_RSQAF, card->membase + STAT);
1220 RXPRINTK("nicstar%d: RSQ almost full.\n", card->index);
1224 spin_unlock_irqrestore(&card->int_lock, flags);
1225 PRINTK("nicstar%d: end of interrupt service\n", card->index);
1229 static int ns_open(struct atm_vcc *vcc)
1233 unsigned long tmpl, modl;
1234 int tcr, tcra; /* target cell rate, and absolute value */
1235 int n = 0; /* Number of entries in the TST. Initialized to remove
1236 the compiler warning. */
1238 int frscdi = 0; /* Index of the SCD. Initialized to remove the compiler
1239 warning. How I wish compilers were clever enough to
1240 tell which variables can truly be used
1242 int inuse; /* tx or rx vc already in use by another vcc */
1243 short vpi = vcc->vpi;
1246 card = (ns_dev *) vcc->dev->dev_data;
1247 PRINTK("nicstar%d: opening vpi.vci %d.%d \n", card->index, (int)vpi,
1249 if (vcc->qos.aal != ATM_AAL5 && vcc->qos.aal != ATM_AAL0) {
1250 PRINTK("nicstar%d: unsupported AAL.\n", card->index);
1254 vc = &(card->vcmap[vpi << card->vcibits | vci]);
1258 if (vcc->qos.txtp.traffic_class != ATM_NONE && vc->tx)
1260 if (vcc->qos.rxtp.traffic_class != ATM_NONE && vc->rx)
1263 printk("nicstar%d: %s vci already in use.\n", card->index,
1264 inuse == 1 ? "tx" : inuse == 2 ? "rx" : "tx and rx");
1268 set_bit(ATM_VF_ADDR, &vcc->flags);
1270 /* NOTE: You are not allowed to modify an open connection's QOS. To change
1271 that, remove the ATM_VF_PARTIAL flag checking. There may be other changes
1272 needed to do that. */
1273 if (!test_bit(ATM_VF_PARTIAL, &vcc->flags)) {
1276 set_bit(ATM_VF_PARTIAL, &vcc->flags);
1277 if (vcc->qos.txtp.traffic_class == ATM_CBR) {
1278 /* Check requested cell rate and availability of SCD */
1279 if (vcc->qos.txtp.max_pcr == 0 && vcc->qos.txtp.pcr == 0
1280 && vcc->qos.txtp.min_pcr == 0) {
1282 ("nicstar%d: trying to open a CBR vc with cell rate = 0 \n",
1284 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1285 clear_bit(ATM_VF_ADDR, &vcc->flags);
1289 tcr = atm_pcr_goal(&(vcc->qos.txtp));
1290 tcra = tcr >= 0 ? tcr : -tcr;
1292 PRINTK("nicstar%d: target cell rate = %d.\n",
1293 card->index, vcc->qos.txtp.max_pcr);
1296 (unsigned long)tcra *(unsigned long)
1298 modl = tmpl % card->max_pcr;
1300 n = (int)(tmpl / card->max_pcr);
1304 } else if (tcr == 0) {
1306 (card->tst_free_entries -
1307 NS_TST_RESERVED)) <= 0) {
1309 ("nicstar%d: no CBR bandwidth free.\n",
1311 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1312 clear_bit(ATM_VF_ADDR, &vcc->flags);
1319 ("nicstar%d: selected bandwidth < granularity.\n",
1321 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1322 clear_bit(ATM_VF_ADDR, &vcc->flags);
1326 if (n > (card->tst_free_entries - NS_TST_RESERVED)) {
1328 ("nicstar%d: not enough free CBR bandwidth.\n",
1330 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1331 clear_bit(ATM_VF_ADDR, &vcc->flags);
1334 card->tst_free_entries -= n;
1336 XPRINTK("nicstar%d: writing %d tst entries.\n",
1338 for (frscdi = 0; frscdi < NS_FRSCD_NUM; frscdi++) {
1339 if (card->scd2vc[frscdi] == NULL) {
1340 card->scd2vc[frscdi] = vc;
1344 if (frscdi == NS_FRSCD_NUM) {
1346 ("nicstar%d: no SCD available for CBR channel.\n",
1348 card->tst_free_entries += n;
1349 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1350 clear_bit(ATM_VF_ADDR, &vcc->flags);
1354 vc->cbr_scd = NS_FRSCD + frscdi * NS_FRSCD_SIZE;
1356 scq = get_scq(card, CBR_SCQSIZE, vc->cbr_scd);
1358 PRINTK("nicstar%d: can't get fixed rate SCQ.\n",
1360 card->scd2vc[frscdi] = NULL;
1361 card->tst_free_entries += n;
1362 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1363 clear_bit(ATM_VF_ADDR, &vcc->flags);
1367 u32d[0] = scq_virt_to_bus(scq, scq->base);
1368 u32d[1] = (u32) 0x00000000;
1369 u32d[2] = (u32) 0xffffffff;
1370 u32d[3] = (u32) 0x00000000;
1371 ns_write_sram(card, vc->cbr_scd, u32d, 4);
1373 fill_tst(card, n, vc);
1374 } else if (vcc->qos.txtp.traffic_class == ATM_UBR) {
1375 vc->cbr_scd = 0x00000000;
1376 vc->scq = card->scq0;
1379 if (vcc->qos.txtp.traffic_class != ATM_NONE) {
1384 if (vcc->qos.rxtp.traffic_class != ATM_NONE) {
1391 /* Open the connection in hardware */
1392 if (vcc->qos.aal == ATM_AAL5)
1393 status = NS_RCTE_AAL5 | NS_RCTE_CONNECTOPEN;
1394 else /* vcc->qos.aal == ATM_AAL0 */
1395 status = NS_RCTE_AAL0 | NS_RCTE_CONNECTOPEN;
1397 status |= NS_RCTE_RAWCELLINTEN;
1398 #endif /* RCQ_SUPPORT */
1401 (vpi << card->vcibits | vci) *
1402 NS_RCT_ENTRY_SIZE, &status, 1);
1407 set_bit(ATM_VF_READY, &vcc->flags);
1411 static void ns_close(struct atm_vcc *vcc)
1419 card = vcc->dev->dev_data;
1420 PRINTK("nicstar%d: closing vpi.vci %d.%d \n", card->index,
1421 (int)vcc->vpi, vcc->vci);
1423 clear_bit(ATM_VF_READY, &vcc->flags);
1425 if (vcc->qos.rxtp.traffic_class != ATM_NONE) {
1427 unsigned long flags;
1431 (vcc->vpi << card->vcibits | vcc->vci) * NS_RCT_ENTRY_SIZE;
1432 spin_lock_irqsave(&card->res_lock, flags);
1433 while (CMD_BUSY(card)) ;
1434 writel(NS_CMD_CLOSE_CONNECTION | addr << 2,
1435 card->membase + CMD);
1436 spin_unlock_irqrestore(&card->res_lock, flags);
1439 if (vc->rx_iov != NULL) {
1440 struct sk_buff *iovb;
1443 stat = readl(card->membase + STAT);
1444 card->sbfqc = ns_stat_sfbqc_get(stat);
1445 card->lbfqc = ns_stat_lfbqc_get(stat);
1448 ("nicstar%d: closing a VC with pending rx buffers.\n",
1451 recycle_iovec_rx_bufs(card, (struct iovec *)iovb->data,
1452 NS_PRV_IOVCNT(iovb));
1453 NS_PRV_IOVCNT(iovb) = 0;
1454 spin_lock_irqsave(&card->int_lock, flags);
1455 recycle_iov_buf(card, iovb);
1456 spin_unlock_irqrestore(&card->int_lock, flags);
1461 if (vcc->qos.txtp.traffic_class != ATM_NONE) {
1465 if (vcc->qos.txtp.traffic_class == ATM_CBR) {
1466 unsigned long flags;
1473 spin_lock_irqsave(&scq->lock, flags);
1475 if (scqep == scq->base)
1479 if (scqep == scq->tail) {
1480 spin_unlock_irqrestore(&scq->lock, flags);
1483 /* If the last entry is not a TSR, place one in the SCQ in order to
1484 be able to completely drain it and then close. */
1485 if (!ns_scqe_is_tsr(scqep) && scq->tail != scq->next) {
1491 tsr.word_1 = ns_tsr_mkword_1(NS_TSR_INTENABLE);
1492 scdi = (vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE;
1493 scqi = scq->next - scq->base;
1494 tsr.word_2 = ns_tsr_mkword_2(scdi, scqi);
1495 tsr.word_3 = 0x00000000;
1496 tsr.word_4 = 0x00000000;
1499 scq->skb[index] = NULL;
1500 if (scq->next == scq->last)
1501 scq->next = scq->base;
1504 data = scq_virt_to_bus(scq, scq->next);
1505 ns_write_sram(card, scq->scd, &data, 1);
1507 spin_unlock_irqrestore(&scq->lock, flags);
1511 /* Free all TST entries */
1512 data = NS_TST_OPCODE_VARIABLE;
1513 for (i = 0; i < NS_TST_NUM_ENTRIES; i++) {
1514 if (card->tste2vc[i] == vc) {
1515 ns_write_sram(card, card->tst_addr + i, &data,
1517 card->tste2vc[i] = NULL;
1518 card->tst_free_entries++;
1522 card->scd2vc[(vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE] = NULL;
1523 free_scq(card, vc->scq, vcc);
1526 /* remove all references to vcc before deleting it */
1527 if (vcc->qos.txtp.traffic_class != ATM_NONE) {
1528 unsigned long flags;
1529 scq_info *scq = card->scq0;
1531 spin_lock_irqsave(&scq->lock, flags);
1533 for (i = 0; i < scq->num_entries; i++) {
1534 if (scq->skb[i] && ATM_SKB(scq->skb[i])->vcc == vcc) {
1535 ATM_SKB(scq->skb[i])->vcc = NULL;
1536 atm_return(vcc, scq->skb[i]->truesize);
1538 ("nicstar: deleted pending vcc mapping\n");
1542 spin_unlock_irqrestore(&scq->lock, flags);
1545 vcc->dev_data = NULL;
1546 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1547 clear_bit(ATM_VF_ADDR, &vcc->flags);
1552 stat = readl(card->membase + STAT);
1553 cfg = readl(card->membase + CFG);
1554 printk("STAT = 0x%08X CFG = 0x%08X \n", stat, cfg);
1556 ("TSQ: base = 0x%p next = 0x%p last = 0x%p TSQT = 0x%08X \n",
1557 card->tsq.base, card->tsq.next,
1558 card->tsq.last, readl(card->membase + TSQT));
1560 ("RSQ: base = 0x%p next = 0x%p last = 0x%p RSQT = 0x%08X \n",
1561 card->rsq.base, card->rsq.next,
1562 card->rsq.last, readl(card->membase + RSQT));
1563 printk("Empty free buffer queue interrupt %s \n",
1564 card->efbie ? "enabled" : "disabled");
1565 printk("SBCNT = %d count = %d LBCNT = %d count = %d \n",
1566 ns_stat_sfbqc_get(stat), card->sbpool.count,
1567 ns_stat_lfbqc_get(stat), card->lbpool.count);
1568 printk("hbpool.count = %d iovpool.count = %d \n",
1569 card->hbpool.count, card->iovpool.count);
1571 #endif /* RX_DEBUG */
1574 static void fill_tst(ns_dev * card, int n, vc_map * vc)
1581 /* It would be very complicated to keep the two TSTs synchronized while
1582 assuring that writes are only made to the inactive TST. So, for now I
1583 will use only one TST. If problems occur, I will change this again */
1585 new_tst = card->tst_addr;
1587 /* Fill procedure */
1589 for (e = 0; e < NS_TST_NUM_ENTRIES; e++) {
1590 if (card->tste2vc[e] == NULL)
1593 if (e == NS_TST_NUM_ENTRIES) {
1594 printk("nicstar%d: No free TST entries found. \n", card->index);
1599 cl = NS_TST_NUM_ENTRIES;
1600 data = ns_tste_make(NS_TST_OPCODE_FIXED, vc->cbr_scd);
1603 if (cl >= NS_TST_NUM_ENTRIES && card->tste2vc[e] == NULL) {
1604 card->tste2vc[e] = vc;
1605 ns_write_sram(card, new_tst + e, &data, 1);
1606 cl -= NS_TST_NUM_ENTRIES;
1610 if (++e == NS_TST_NUM_ENTRIES) {
1616 /* End of fill procedure */
1618 data = ns_tste_make(NS_TST_OPCODE_END, new_tst);
1619 ns_write_sram(card, new_tst + NS_TST_NUM_ENTRIES, &data, 1);
1620 ns_write_sram(card, card->tst_addr + NS_TST_NUM_ENTRIES, &data, 1);
1621 card->tst_addr = new_tst;
1624 static int ns_send(struct atm_vcc *vcc, struct sk_buff *skb)
1629 unsigned long buflen;
1631 u32 flags; /* TBD flags, not CPU flags */
1633 card = vcc->dev->dev_data;
1634 TXPRINTK("nicstar%d: ns_send() called.\n", card->index);
1635 if ((vc = (vc_map *) vcc->dev_data) == NULL) {
1636 printk("nicstar%d: vcc->dev_data == NULL on ns_send().\n",
1638 atomic_inc(&vcc->stats->tx_err);
1639 dev_kfree_skb_any(skb);
1644 printk("nicstar%d: Trying to transmit on a non-tx VC.\n",
1646 atomic_inc(&vcc->stats->tx_err);
1647 dev_kfree_skb_any(skb);
1651 if (vcc->qos.aal != ATM_AAL5 && vcc->qos.aal != ATM_AAL0) {
1652 printk("nicstar%d: Only AAL0 and AAL5 are supported.\n",
1654 atomic_inc(&vcc->stats->tx_err);
1655 dev_kfree_skb_any(skb);
1659 if (skb_shinfo(skb)->nr_frags != 0) {
1660 printk("nicstar%d: No scatter-gather yet.\n", card->index);
1661 atomic_inc(&vcc->stats->tx_err);
1662 dev_kfree_skb_any(skb);
1666 ATM_SKB(skb)->vcc = vcc;
1668 NS_PRV_DMA(skb) = dma_map_single(&card->pcidev->dev, skb->data,
1669 skb->len, DMA_TO_DEVICE);
1671 if (vcc->qos.aal == ATM_AAL5) {
1672 buflen = (skb->len + 47 + 8) / 48 * 48; /* Multiple of 48 */
1673 flags = NS_TBD_AAL5;
1674 scqe.word_2 = cpu_to_le32(NS_PRV_DMA(skb));
1675 scqe.word_3 = cpu_to_le32(skb->len);
1677 ns_tbd_mkword_4(0, (u32) vcc->vpi, (u32) vcc->vci, 0,
1679 atm_options & ATM_ATMOPT_CLP ? 1 : 0);
1680 flags |= NS_TBD_EOPDU;
1681 } else { /* (vcc->qos.aal == ATM_AAL0) */
1683 buflen = ATM_CELL_PAYLOAD; /* i.e., 48 bytes */
1684 flags = NS_TBD_AAL0;
1685 scqe.word_2 = cpu_to_le32(NS_PRV_DMA(skb) + NS_AAL0_HEADER);
1686 scqe.word_3 = cpu_to_le32(0x00000000);
1687 if (*skb->data & 0x02) /* Payload type 1 - end of pdu */
1688 flags |= NS_TBD_EOPDU;
1690 cpu_to_le32(*((u32 *) skb->data) & ~NS_TBD_VC_MASK);
1691 /* Force the VPI/VCI to be the same as in VCC struct */
1693 cpu_to_le32((((u32) vcc->
1694 vpi) << NS_TBD_VPI_SHIFT | ((u32) vcc->
1696 NS_TBD_VCI_SHIFT) & NS_TBD_VC_MASK);
1699 if (vcc->qos.txtp.traffic_class == ATM_CBR) {
1700 scqe.word_1 = ns_tbd_mkword_1_novbr(flags, (u32) buflen);
1701 scq = ((vc_map *) vcc->dev_data)->scq;
1704 ns_tbd_mkword_1(flags, (u32) 1, (u32) 1, (u32) buflen);
1708 if (push_scqe(card, vc, scq, &scqe, skb) != 0) {
1709 atomic_inc(&vcc->stats->tx_err);
1710 dev_kfree_skb_any(skb);
1713 atomic_inc(&vcc->stats->tx);
1718 static int push_scqe(ns_dev * card, vc_map * vc, scq_info * scq, ns_scqe * tbd,
1719 struct sk_buff *skb)
1721 unsigned long flags;
1728 spin_lock_irqsave(&scq->lock, flags);
1729 while (scq->tail == scq->next) {
1730 if (in_interrupt()) {
1731 spin_unlock_irqrestore(&scq->lock, flags);
1732 printk("nicstar%d: Error pushing TBD.\n", card->index);
1737 wait_event_interruptible_lock_irq_timeout(scq->scqfull_waitq,
1738 scq->tail != scq->next,
1743 spin_unlock_irqrestore(&scq->lock, flags);
1744 printk("nicstar%d: Timeout pushing TBD.\n",
1750 index = (int)(scq->next - scq->base);
1751 scq->skb[index] = skb;
1752 XPRINTK("nicstar%d: sending skb at 0x%p (pos %d).\n",
1753 card->index, skb, index);
1754 XPRINTK("nicstar%d: TBD written:\n0x%x\n0x%x\n0x%x\n0x%x\n at 0x%p.\n",
1755 card->index, le32_to_cpu(tbd->word_1), le32_to_cpu(tbd->word_2),
1756 le32_to_cpu(tbd->word_3), le32_to_cpu(tbd->word_4),
1758 if (scq->next == scq->last)
1759 scq->next = scq->base;
1764 if (scq->num_entries == VBR_SCQ_NUM_ENTRIES) {
1770 if (vc->tbd_count >= MAX_TBD_PER_VC
1771 || scq->tbd_count >= MAX_TBD_PER_SCQ) {
1774 while (scq->tail == scq->next) {
1775 if (in_interrupt()) {
1776 data = scq_virt_to_bus(scq, scq->next);
1777 ns_write_sram(card, scq->scd, &data, 1);
1778 spin_unlock_irqrestore(&scq->lock, flags);
1779 printk("nicstar%d: Error pushing TSR.\n",
1787 wait_event_interruptible_lock_irq_timeout(scq->scqfull_waitq,
1788 scq->tail != scq->next,
1794 tsr.word_1 = ns_tsr_mkword_1(NS_TSR_INTENABLE);
1796 scdi = NS_TSR_SCDISVBR;
1798 scdi = (vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE;
1799 scqi = scq->next - scq->base;
1800 tsr.word_2 = ns_tsr_mkword_2(scdi, scqi);
1801 tsr.word_3 = 0x00000000;
1802 tsr.word_4 = 0x00000000;
1806 scq->skb[index] = NULL;
1808 ("nicstar%d: TSR written:\n0x%x\n0x%x\n0x%x\n0x%x\n at 0x%p.\n",
1809 card->index, le32_to_cpu(tsr.word_1),
1810 le32_to_cpu(tsr.word_2), le32_to_cpu(tsr.word_3),
1811 le32_to_cpu(tsr.word_4), scq->next);
1812 if (scq->next == scq->last)
1813 scq->next = scq->base;
1819 PRINTK("nicstar%d: Timeout pushing TSR.\n",
1822 data = scq_virt_to_bus(scq, scq->next);
1823 ns_write_sram(card, scq->scd, &data, 1);
1825 spin_unlock_irqrestore(&scq->lock, flags);
1830 static void process_tsq(ns_dev * card)
1834 ns_tsi *previous = NULL, *one_ahead, *two_ahead;
1835 int serviced_entries; /* flag indicating at least on entry was serviced */
1837 serviced_entries = 0;
1839 if (card->tsq.next == card->tsq.last)
1840 one_ahead = card->tsq.base;
1842 one_ahead = card->tsq.next + 1;
1844 if (one_ahead == card->tsq.last)
1845 two_ahead = card->tsq.base;
1847 two_ahead = one_ahead + 1;
1849 while (!ns_tsi_isempty(card->tsq.next) || !ns_tsi_isempty(one_ahead) ||
1850 !ns_tsi_isempty(two_ahead))
1851 /* At most two empty, as stated in the 77201 errata */
1853 serviced_entries = 1;
1855 /* Skip the one or two possible empty entries */
1856 while (ns_tsi_isempty(card->tsq.next)) {
1857 if (card->tsq.next == card->tsq.last)
1858 card->tsq.next = card->tsq.base;
1863 if (!ns_tsi_tmrof(card->tsq.next)) {
1864 scdi = ns_tsi_getscdindex(card->tsq.next);
1865 if (scdi == NS_TSI_SCDISVBR)
1868 if (card->scd2vc[scdi] == NULL) {
1870 ("nicstar%d: could not find VC from SCD index.\n",
1872 ns_tsi_init(card->tsq.next);
1875 scq = card->scd2vc[scdi]->scq;
1877 drain_scq(card, scq, ns_tsi_getscqpos(card->tsq.next));
1879 wake_up_interruptible(&(scq->scqfull_waitq));
1882 ns_tsi_init(card->tsq.next);
1883 previous = card->tsq.next;
1884 if (card->tsq.next == card->tsq.last)
1885 card->tsq.next = card->tsq.base;
1889 if (card->tsq.next == card->tsq.last)
1890 one_ahead = card->tsq.base;
1892 one_ahead = card->tsq.next + 1;
1894 if (one_ahead == card->tsq.last)
1895 two_ahead = card->tsq.base;
1897 two_ahead = one_ahead + 1;
1900 if (serviced_entries)
1901 writel(PTR_DIFF(previous, card->tsq.base),
1902 card->membase + TSQH);
1905 static void drain_scq(ns_dev * card, scq_info * scq, int pos)
1907 struct atm_vcc *vcc;
1908 struct sk_buff *skb;
1910 unsigned long flags;
1912 XPRINTK("nicstar%d: drain_scq() called, scq at 0x%p, pos %d.\n",
1913 card->index, scq, pos);
1914 if (pos >= scq->num_entries) {
1915 printk("nicstar%d: Bad index on drain_scq().\n", card->index);
1919 spin_lock_irqsave(&scq->lock, flags);
1920 i = (int)(scq->tail - scq->base);
1921 if (++i == scq->num_entries)
1925 XPRINTK("nicstar%d: freeing skb at 0x%p (index %d).\n",
1926 card->index, skb, i);
1928 dma_unmap_single(&card->pcidev->dev,
1932 vcc = ATM_SKB(skb)->vcc;
1933 if (vcc && vcc->pop != NULL) {
1936 dev_kfree_skb_irq(skb);
1940 if (++i == scq->num_entries)
1943 scq->tail = scq->base + pos;
1944 spin_unlock_irqrestore(&scq->lock, flags);
1947 static void process_rsq(ns_dev * card)
1951 if (!ns_rsqe_valid(card->rsq.next))
1954 dequeue_rx(card, card->rsq.next);
1955 ns_rsqe_init(card->rsq.next);
1956 previous = card->rsq.next;
1957 if (card->rsq.next == card->rsq.last)
1958 card->rsq.next = card->rsq.base;
1961 } while (ns_rsqe_valid(card->rsq.next));
1962 writel(PTR_DIFF(previous, card->rsq.base), card->membase + RSQH);
1965 static void dequeue_rx(ns_dev * card, ns_rsqe * rsqe)
1969 struct sk_buff *iovb;
1971 struct atm_vcc *vcc;
1972 struct sk_buff *skb;
1973 unsigned short aal5_len;
1978 stat = readl(card->membase + STAT);
1979 card->sbfqc = ns_stat_sfbqc_get(stat);
1980 card->lbfqc = ns_stat_lfbqc_get(stat);
1982 id = le32_to_cpu(rsqe->buffer_handle);
1983 skb = idr_remove(&card->idr, id);
1986 "nicstar%d: skb not found!\n", card->index);
1989 dma_sync_single_for_cpu(&card->pcidev->dev,
1991 (NS_PRV_BUFTYPE(skb) == BUF_SM
1992 ? NS_SMSKBSIZE : NS_LGSKBSIZE),
1994 dma_unmap_single(&card->pcidev->dev,
1996 (NS_PRV_BUFTYPE(skb) == BUF_SM
1997 ? NS_SMSKBSIZE : NS_LGSKBSIZE),
1999 vpi = ns_rsqe_vpi(rsqe);
2000 vci = ns_rsqe_vci(rsqe);
2001 if (vpi >= 1UL << card->vpibits || vci >= 1UL << card->vcibits) {
2002 printk("nicstar%d: SDU received for out-of-range vc %d.%d.\n",
2003 card->index, vpi, vci);
2004 recycle_rx_buf(card, skb);
2008 vc = &(card->vcmap[vpi << card->vcibits | vci]);
2010 RXPRINTK("nicstar%d: SDU received on non-rx vc %d.%d.\n",
2011 card->index, vpi, vci);
2012 recycle_rx_buf(card, skb);
2018 if (vcc->qos.aal == ATM_AAL0) {
2020 unsigned char *cell;
2024 for (i = ns_rsqe_cellcount(rsqe); i; i--) {
2025 sb = dev_alloc_skb(NS_SMSKBSIZE);
2028 ("nicstar%d: Can't allocate buffers for aal0.\n",
2030 atomic_add(i, &vcc->stats->rx_drop);
2033 if (!atm_charge(vcc, sb->truesize)) {
2035 ("nicstar%d: atm_charge() dropped aal0 packets.\n",
2037 atomic_add(i - 1, &vcc->stats->rx_drop); /* already increased by 1 */
2038 dev_kfree_skb_any(sb);
2041 /* Rebuild the header */
2042 *((u32 *) sb->data) = le32_to_cpu(rsqe->word_1) << 4 |
2043 (ns_rsqe_clp(rsqe) ? 0x00000001 : 0x00000000);
2044 if (i == 1 && ns_rsqe_eopdu(rsqe))
2045 *((u32 *) sb->data) |= 0x00000002;
2046 skb_put(sb, NS_AAL0_HEADER);
2047 memcpy(skb_tail_pointer(sb), cell, ATM_CELL_PAYLOAD);
2048 skb_put(sb, ATM_CELL_PAYLOAD);
2049 ATM_SKB(sb)->vcc = vcc;
2050 __net_timestamp(sb);
2052 atomic_inc(&vcc->stats->rx);
2053 cell += ATM_CELL_PAYLOAD;
2056 recycle_rx_buf(card, skb);
2060 /* To reach this point, the AAL layer can only be AAL5 */
2062 if ((iovb = vc->rx_iov) == NULL) {
2063 iovb = skb_dequeue(&(card->iovpool.queue));
2064 if (iovb == NULL) { /* No buffers in the queue */
2065 iovb = alloc_skb(NS_IOVBUFSIZE, GFP_ATOMIC);
2067 printk("nicstar%d: Out of iovec buffers.\n",
2069 atomic_inc(&vcc->stats->rx_drop);
2070 recycle_rx_buf(card, skb);
2073 NS_PRV_BUFTYPE(iovb) = BUF_NONE;
2074 } else if (--card->iovpool.count < card->iovnr.min) {
2075 struct sk_buff *new_iovb;
2077 alloc_skb(NS_IOVBUFSIZE, GFP_ATOMIC)) != NULL) {
2078 NS_PRV_BUFTYPE(iovb) = BUF_NONE;
2079 skb_queue_tail(&card->iovpool.queue, new_iovb);
2080 card->iovpool.count++;
2084 NS_PRV_IOVCNT(iovb) = 0;
2086 iovb->data = iovb->head;
2087 skb_reset_tail_pointer(iovb);
2088 /* IMPORTANT: a pointer to the sk_buff containing the small or large
2089 buffer is stored as iovec base, NOT a pointer to the
2090 small or large buffer itself. */
2091 } else if (NS_PRV_IOVCNT(iovb) >= NS_MAX_IOVECS) {
2092 printk("nicstar%d: received too big AAL5 SDU.\n", card->index);
2093 atomic_inc(&vcc->stats->rx_err);
2094 recycle_iovec_rx_bufs(card, (struct iovec *)iovb->data,
2096 NS_PRV_IOVCNT(iovb) = 0;
2098 iovb->data = iovb->head;
2099 skb_reset_tail_pointer(iovb);
2101 iov = &((struct iovec *)iovb->data)[NS_PRV_IOVCNT(iovb)++];
2102 iov->iov_base = (void *)skb;
2103 iov->iov_len = ns_rsqe_cellcount(rsqe) * 48;
2104 iovb->len += iov->iov_len;
2107 if (NS_PRV_IOVCNT(iovb) == 1) {
2108 if (NS_PRV_BUFTYPE(skb) != BUF_SM) {
2110 ("nicstar%d: Expected a small buffer, and this is not one.\n",
2112 which_list(card, skb);
2113 atomic_inc(&vcc->stats->rx_err);
2114 recycle_rx_buf(card, skb);
2116 recycle_iov_buf(card, iovb);
2119 } else { /* NS_PRV_IOVCNT(iovb) >= 2 */
2121 if (NS_PRV_BUFTYPE(skb) != BUF_LG) {
2123 ("nicstar%d: Expected a large buffer, and this is not one.\n",
2125 which_list(card, skb);
2126 atomic_inc(&vcc->stats->rx_err);
2127 recycle_iovec_rx_bufs(card, (struct iovec *)iovb->data,
2128 NS_PRV_IOVCNT(iovb));
2130 recycle_iov_buf(card, iovb);
2134 #endif /* EXTRA_DEBUG */
2136 if (ns_rsqe_eopdu(rsqe)) {
2137 /* This works correctly regardless of the endianness of the host */
2138 unsigned char *L1L2 = (unsigned char *)
2139 (skb->data + iov->iov_len - 6);
2140 aal5_len = L1L2[0] << 8 | L1L2[1];
2141 len = (aal5_len == 0x0000) ? 0x10000 : aal5_len;
2142 if (ns_rsqe_crcerr(rsqe) ||
2143 len + 8 > iovb->len || len + (47 + 8) < iovb->len) {
2144 printk("nicstar%d: AAL5 CRC error", card->index);
2145 if (len + 8 > iovb->len || len + (47 + 8) < iovb->len)
2146 printk(" - PDU size mismatch.\n");
2149 atomic_inc(&vcc->stats->rx_err);
2150 recycle_iovec_rx_bufs(card, (struct iovec *)iovb->data,
2151 NS_PRV_IOVCNT(iovb));
2153 recycle_iov_buf(card, iovb);
2157 /* By this point we (hopefully) have a complete SDU without errors. */
2159 if (NS_PRV_IOVCNT(iovb) == 1) { /* Just a small buffer */
2160 /* skb points to a small buffer */
2161 if (!atm_charge(vcc, skb->truesize)) {
2162 push_rxbufs(card, skb);
2163 atomic_inc(&vcc->stats->rx_drop);
2166 dequeue_sm_buf(card, skb);
2167 ATM_SKB(skb)->vcc = vcc;
2168 __net_timestamp(skb);
2169 vcc->push(vcc, skb);
2170 atomic_inc(&vcc->stats->rx);
2172 } else if (NS_PRV_IOVCNT(iovb) == 2) { /* One small plus one large buffer */
2175 sb = (struct sk_buff *)(iov - 1)->iov_base;
2176 /* skb points to a large buffer */
2178 if (len <= NS_SMBUFSIZE) {
2179 if (!atm_charge(vcc, sb->truesize)) {
2180 push_rxbufs(card, sb);
2181 atomic_inc(&vcc->stats->rx_drop);
2184 dequeue_sm_buf(card, sb);
2185 ATM_SKB(sb)->vcc = vcc;
2186 __net_timestamp(sb);
2188 atomic_inc(&vcc->stats->rx);
2191 push_rxbufs(card, skb);
2193 } else { /* len > NS_SMBUFSIZE, the usual case */
2195 if (!atm_charge(vcc, skb->truesize)) {
2196 push_rxbufs(card, skb);
2197 atomic_inc(&vcc->stats->rx_drop);
2199 dequeue_lg_buf(card, skb);
2200 skb_push(skb, NS_SMBUFSIZE);
2201 skb_copy_from_linear_data(sb, skb->data,
2203 skb_put(skb, len - NS_SMBUFSIZE);
2204 ATM_SKB(skb)->vcc = vcc;
2205 __net_timestamp(skb);
2206 vcc->push(vcc, skb);
2207 atomic_inc(&vcc->stats->rx);
2210 push_rxbufs(card, sb);
2214 } else { /* Must push a huge buffer */
2216 struct sk_buff *hb, *sb, *lb;
2217 int remaining, tocopy;
2220 hb = skb_dequeue(&(card->hbpool.queue));
2221 if (hb == NULL) { /* No buffers in the queue */
2223 hb = dev_alloc_skb(NS_HBUFSIZE);
2226 ("nicstar%d: Out of huge buffers.\n",
2228 atomic_inc(&vcc->stats->rx_drop);
2229 recycle_iovec_rx_bufs(card,
2232 NS_PRV_IOVCNT(iovb));
2234 recycle_iov_buf(card, iovb);
2236 } else if (card->hbpool.count < card->hbnr.min) {
2237 struct sk_buff *new_hb;
2239 dev_alloc_skb(NS_HBUFSIZE)) !=
2241 skb_queue_tail(&card->hbpool.
2243 card->hbpool.count++;
2246 NS_PRV_BUFTYPE(hb) = BUF_NONE;
2247 } else if (--card->hbpool.count < card->hbnr.min) {
2248 struct sk_buff *new_hb;
2250 dev_alloc_skb(NS_HBUFSIZE)) != NULL) {
2251 NS_PRV_BUFTYPE(new_hb) = BUF_NONE;
2252 skb_queue_tail(&card->hbpool.queue,
2254 card->hbpool.count++;
2256 if (card->hbpool.count < card->hbnr.min) {
2258 dev_alloc_skb(NS_HBUFSIZE)) !=
2260 NS_PRV_BUFTYPE(new_hb) =
2262 skb_queue_tail(&card->hbpool.
2264 card->hbpool.count++;
2269 iov = (struct iovec *)iovb->data;
2271 if (!atm_charge(vcc, hb->truesize)) {
2272 recycle_iovec_rx_bufs(card, iov,
2273 NS_PRV_IOVCNT(iovb));
2274 if (card->hbpool.count < card->hbnr.max) {
2275 skb_queue_tail(&card->hbpool.queue, hb);
2276 card->hbpool.count++;
2278 dev_kfree_skb_any(hb);
2279 atomic_inc(&vcc->stats->rx_drop);
2281 /* Copy the small buffer to the huge buffer */
2282 sb = (struct sk_buff *)iov->iov_base;
2283 skb_copy_from_linear_data(sb, hb->data,
2285 skb_put(hb, iov->iov_len);
2286 remaining = len - iov->iov_len;
2288 /* Free the small buffer */
2289 push_rxbufs(card, sb);
2291 /* Copy all large buffers to the huge buffer and free them */
2292 for (j = 1; j < NS_PRV_IOVCNT(iovb); j++) {
2293 lb = (struct sk_buff *)iov->iov_base;
2295 min_t(int, remaining, iov->iov_len);
2296 skb_copy_from_linear_data(lb,
2299 skb_put(hb, tocopy);
2301 remaining -= tocopy;
2302 push_rxbufs(card, lb);
2305 if (remaining != 0 || hb->len != len)
2307 ("nicstar%d: Huge buffer len mismatch.\n",
2309 #endif /* EXTRA_DEBUG */
2310 ATM_SKB(hb)->vcc = vcc;
2311 __net_timestamp(hb);
2313 atomic_inc(&vcc->stats->rx);
2318 recycle_iov_buf(card, iovb);
2323 static void recycle_rx_buf(ns_dev * card, struct sk_buff *skb)
2325 if (unlikely(NS_PRV_BUFTYPE(skb) == BUF_NONE)) {
2326 printk("nicstar%d: What kind of rx buffer is this?\n",
2328 dev_kfree_skb_any(skb);
2330 push_rxbufs(card, skb);
2333 static void recycle_iovec_rx_bufs(ns_dev * card, struct iovec *iov, int count)
2336 recycle_rx_buf(card, (struct sk_buff *)(iov++)->iov_base);
2339 static void recycle_iov_buf(ns_dev * card, struct sk_buff *iovb)
2341 if (card->iovpool.count < card->iovnr.max) {
2342 skb_queue_tail(&card->iovpool.queue, iovb);
2343 card->iovpool.count++;
2345 dev_kfree_skb_any(iovb);
2348 static void dequeue_sm_buf(ns_dev * card, struct sk_buff *sb)
2350 skb_unlink(sb, &card->sbpool.queue);
2351 if (card->sbfqc < card->sbnr.init) {
2352 struct sk_buff *new_sb;
2353 if ((new_sb = dev_alloc_skb(NS_SMSKBSIZE)) != NULL) {
2354 NS_PRV_BUFTYPE(new_sb) = BUF_SM;
2355 skb_queue_tail(&card->sbpool.queue, new_sb);
2356 skb_reserve(new_sb, NS_AAL0_HEADER);
2357 push_rxbufs(card, new_sb);
2360 if (card->sbfqc < card->sbnr.init)
2362 struct sk_buff *new_sb;
2363 if ((new_sb = dev_alloc_skb(NS_SMSKBSIZE)) != NULL) {
2364 NS_PRV_BUFTYPE(new_sb) = BUF_SM;
2365 skb_queue_tail(&card->sbpool.queue, new_sb);
2366 skb_reserve(new_sb, NS_AAL0_HEADER);
2367 push_rxbufs(card, new_sb);
2372 static void dequeue_lg_buf(ns_dev * card, struct sk_buff *lb)
2374 skb_unlink(lb, &card->lbpool.queue);
2375 if (card->lbfqc < card->lbnr.init) {
2376 struct sk_buff *new_lb;
2377 if ((new_lb = dev_alloc_skb(NS_LGSKBSIZE)) != NULL) {
2378 NS_PRV_BUFTYPE(new_lb) = BUF_LG;
2379 skb_queue_tail(&card->lbpool.queue, new_lb);
2380 skb_reserve(new_lb, NS_SMBUFSIZE);
2381 push_rxbufs(card, new_lb);
2384 if (card->lbfqc < card->lbnr.init)
2386 struct sk_buff *new_lb;
2387 if ((new_lb = dev_alloc_skb(NS_LGSKBSIZE)) != NULL) {
2388 NS_PRV_BUFTYPE(new_lb) = BUF_LG;
2389 skb_queue_tail(&card->lbpool.queue, new_lb);
2390 skb_reserve(new_lb, NS_SMBUFSIZE);
2391 push_rxbufs(card, new_lb);
2396 static int ns_proc_read(struct atm_dev *dev, loff_t * pos, char *page)
2403 card = (ns_dev *) dev->dev_data;
2404 stat = readl(card->membase + STAT);
2406 return sprintf(page, "Pool count min init max \n");
2408 return sprintf(page, "Small %5d %5d %5d %5d \n",
2409 ns_stat_sfbqc_get(stat), card->sbnr.min,
2410 card->sbnr.init, card->sbnr.max);
2412 return sprintf(page, "Large %5d %5d %5d %5d \n",
2413 ns_stat_lfbqc_get(stat), card->lbnr.min,
2414 card->lbnr.init, card->lbnr.max);
2416 return sprintf(page, "Huge %5d %5d %5d %5d \n",
2417 card->hbpool.count, card->hbnr.min,
2418 card->hbnr.init, card->hbnr.max);
2420 return sprintf(page, "Iovec %5d %5d %5d %5d \n",
2421 card->iovpool.count, card->iovnr.min,
2422 card->iovnr.init, card->iovnr.max);
2426 sprintf(page, "Interrupt counter: %u \n", card->intcnt);
2431 /* Dump 25.6 Mbps PHY registers */
2432 /* Now there's a 25.6 Mbps PHY driver this code isn't needed. I left it
2433 here just in case it's needed for debugging. */
2434 if (card->max_pcr == ATM_25_PCR && !left--) {
2438 for (i = 0; i < 4; i++) {
2439 while (CMD_BUSY(card)) ;
2440 writel(NS_CMD_READ_UTILITY | 0x00000200 | i,
2441 card->membase + CMD);
2442 while (CMD_BUSY(card)) ;
2443 phy_regs[i] = readl(card->membase + DR0) & 0x000000FF;
2446 return sprintf(page, "PHY regs: 0x%02X 0x%02X 0x%02X 0x%02X \n",
2447 phy_regs[0], phy_regs[1], phy_regs[2],
2450 #endif /* 0 - Dump 25.6 Mbps PHY registers */
2453 if (left-- < NS_TST_NUM_ENTRIES) {
2454 if (card->tste2vc[left + 1] == NULL)
2455 return sprintf(page, "%5d - VBR/UBR \n", left + 1);
2457 return sprintf(page, "%5d - %d %d \n", left + 1,
2458 card->tste2vc[left + 1]->tx_vcc->vpi,
2459 card->tste2vc[left + 1]->tx_vcc->vci);
2465 static int ns_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg)
2470 unsigned long flags;
2472 card = dev->dev_data;
2476 (pl.buftype, &((pool_levels __user *) arg)->buftype))
2478 switch (pl.buftype) {
2479 case NS_BUFTYPE_SMALL:
2481 ns_stat_sfbqc_get(readl(card->membase + STAT));
2482 pl.level.min = card->sbnr.min;
2483 pl.level.init = card->sbnr.init;
2484 pl.level.max = card->sbnr.max;
2487 case NS_BUFTYPE_LARGE:
2489 ns_stat_lfbqc_get(readl(card->membase + STAT));
2490 pl.level.min = card->lbnr.min;
2491 pl.level.init = card->lbnr.init;
2492 pl.level.max = card->lbnr.max;
2495 case NS_BUFTYPE_HUGE:
2496 pl.count = card->hbpool.count;
2497 pl.level.min = card->hbnr.min;
2498 pl.level.init = card->hbnr.init;
2499 pl.level.max = card->hbnr.max;
2502 case NS_BUFTYPE_IOVEC:
2503 pl.count = card->iovpool.count;
2504 pl.level.min = card->iovnr.min;
2505 pl.level.init = card->iovnr.init;
2506 pl.level.max = card->iovnr.max;
2510 return -ENOIOCTLCMD;
2513 if (!copy_to_user((pool_levels __user *) arg, &pl, sizeof(pl)))
2514 return (sizeof(pl));
2519 if (!capable(CAP_NET_ADMIN))
2521 if (copy_from_user(&pl, (pool_levels __user *) arg, sizeof(pl)))
2523 if (pl.level.min >= pl.level.init
2524 || pl.level.init >= pl.level.max)
2526 if (pl.level.min == 0)
2528 switch (pl.buftype) {
2529 case NS_BUFTYPE_SMALL:
2530 if (pl.level.max > TOP_SB)
2532 card->sbnr.min = pl.level.min;
2533 card->sbnr.init = pl.level.init;
2534 card->sbnr.max = pl.level.max;
2537 case NS_BUFTYPE_LARGE:
2538 if (pl.level.max > TOP_LB)
2540 card->lbnr.min = pl.level.min;
2541 card->lbnr.init = pl.level.init;
2542 card->lbnr.max = pl.level.max;
2545 case NS_BUFTYPE_HUGE:
2546 if (pl.level.max > TOP_HB)
2548 card->hbnr.min = pl.level.min;
2549 card->hbnr.init = pl.level.init;
2550 card->hbnr.max = pl.level.max;
2553 case NS_BUFTYPE_IOVEC:
2554 if (pl.level.max > TOP_IOVB)
2556 card->iovnr.min = pl.level.min;
2557 card->iovnr.init = pl.level.init;
2558 card->iovnr.max = pl.level.max;
2568 if (!capable(CAP_NET_ADMIN))
2570 btype = (long)arg; /* a long is the same size as a pointer or bigger */
2572 case NS_BUFTYPE_SMALL:
2573 while (card->sbfqc < card->sbnr.init) {
2576 sb = __dev_alloc_skb(NS_SMSKBSIZE, GFP_KERNEL);
2579 NS_PRV_BUFTYPE(sb) = BUF_SM;
2580 skb_queue_tail(&card->sbpool.queue, sb);
2581 skb_reserve(sb, NS_AAL0_HEADER);
2582 push_rxbufs(card, sb);
2586 case NS_BUFTYPE_LARGE:
2587 while (card->lbfqc < card->lbnr.init) {
2590 lb = __dev_alloc_skb(NS_LGSKBSIZE, GFP_KERNEL);
2593 NS_PRV_BUFTYPE(lb) = BUF_LG;
2594 skb_queue_tail(&card->lbpool.queue, lb);
2595 skb_reserve(lb, NS_SMBUFSIZE);
2596 push_rxbufs(card, lb);
2600 case NS_BUFTYPE_HUGE:
2601 while (card->hbpool.count > card->hbnr.init) {
2604 spin_lock_irqsave(&card->int_lock, flags);
2605 hb = skb_dequeue(&card->hbpool.queue);
2606 card->hbpool.count--;
2607 spin_unlock_irqrestore(&card->int_lock, flags);
2610 ("nicstar%d: huge buffer count inconsistent.\n",
2613 dev_kfree_skb_any(hb);
2616 while (card->hbpool.count < card->hbnr.init) {
2619 hb = __dev_alloc_skb(NS_HBUFSIZE, GFP_KERNEL);
2622 NS_PRV_BUFTYPE(hb) = BUF_NONE;
2623 spin_lock_irqsave(&card->int_lock, flags);
2624 skb_queue_tail(&card->hbpool.queue, hb);
2625 card->hbpool.count++;
2626 spin_unlock_irqrestore(&card->int_lock, flags);
2630 case NS_BUFTYPE_IOVEC:
2631 while (card->iovpool.count > card->iovnr.init) {
2632 struct sk_buff *iovb;
2634 spin_lock_irqsave(&card->int_lock, flags);
2635 iovb = skb_dequeue(&card->iovpool.queue);
2636 card->iovpool.count--;
2637 spin_unlock_irqrestore(&card->int_lock, flags);
2640 ("nicstar%d: iovec buffer count inconsistent.\n",
2643 dev_kfree_skb_any(iovb);
2646 while (card->iovpool.count < card->iovnr.init) {
2647 struct sk_buff *iovb;
2649 iovb = alloc_skb(NS_IOVBUFSIZE, GFP_KERNEL);
2652 NS_PRV_BUFTYPE(iovb) = BUF_NONE;
2653 spin_lock_irqsave(&card->int_lock, flags);
2654 skb_queue_tail(&card->iovpool.queue, iovb);
2655 card->iovpool.count++;
2656 spin_unlock_irqrestore(&card->int_lock, flags);
2667 if (dev->phy && dev->phy->ioctl) {
2668 return dev->phy->ioctl(dev, cmd, arg);
2670 printk("nicstar%d: %s == NULL \n", card->index,
2671 dev->phy ? "dev->phy->ioctl" : "dev->phy");
2672 return -ENOIOCTLCMD;
2678 static void which_list(ns_dev * card, struct sk_buff *skb)
2680 printk("skb buf_type: 0x%08x\n", NS_PRV_BUFTYPE(skb));
2682 #endif /* EXTRA_DEBUG */
2684 static void ns_poll(unsigned long arg)
2688 unsigned long flags;
2691 PRINTK("nicstar: Entering ns_poll().\n");
2692 for (i = 0; i < num_cards; i++) {
2694 if (spin_is_locked(&card->int_lock)) {
2695 /* Probably it isn't worth spinning */
2698 spin_lock_irqsave(&card->int_lock, flags);
2701 stat_r = readl(card->membase + STAT);
2702 if (stat_r & NS_STAT_TSIF)
2703 stat_w |= NS_STAT_TSIF;
2704 if (stat_r & NS_STAT_EOPDU)
2705 stat_w |= NS_STAT_EOPDU;
2710 writel(stat_w, card->membase + STAT);
2711 spin_unlock_irqrestore(&card->int_lock, flags);
2713 mod_timer(&ns_timer, jiffies + NS_POLL_PERIOD);
2714 PRINTK("nicstar: Leaving ns_poll().\n");
2717 static void ns_phy_put(struct atm_dev *dev, unsigned char value,
2721 unsigned long flags;
2723 card = dev->dev_data;
2724 spin_lock_irqsave(&card->res_lock, flags);
2725 while (CMD_BUSY(card)) ;
2726 writel((u32) value, card->membase + DR0);
2727 writel(NS_CMD_WRITE_UTILITY | 0x00000200 | (addr & 0x000000FF),
2728 card->membase + CMD);
2729 spin_unlock_irqrestore(&card->res_lock, flags);
2732 static unsigned char ns_phy_get(struct atm_dev *dev, unsigned long addr)
2735 unsigned long flags;
2738 card = dev->dev_data;
2739 spin_lock_irqsave(&card->res_lock, flags);
2740 while (CMD_BUSY(card)) ;
2741 writel(NS_CMD_READ_UTILITY | 0x00000200 | (addr & 0x000000FF),
2742 card->membase + CMD);
2743 while (CMD_BUSY(card)) ;
2744 data = readl(card->membase + DR0) & 0x000000FF;
2745 spin_unlock_irqrestore(&card->res_lock, flags);
2746 return (unsigned char)data;
2749 module_init(nicstar_init);
2750 module_exit(nicstar_cleanup);