1 // SPDX-License-Identifier: GPL-2.0-only
5 * Device driver supporting CBR for IDT 77201/77211 "NICStAR" based cards.
7 * IMPORTANT: The included file nicstarmac.c was NOT WRITTEN BY ME.
8 * It was taken from the frle-0.22 device driver.
9 * As the file doesn't have a copyright notice, in the file
10 * nicstarmac.copyright I put the copyright notice from the
11 * frle-0.22 device driver.
12 * Some code is based on the nicstar driver by M. Welsh.
14 * Author: Rui Prior (rprior@inescn.pt)
15 * PowerPC support by Jay Talbott (jay_talbott@mcg.mot.com) April 1999
22 * IMPORTANT INFORMATION
24 * There are currently three types of spinlocks:
26 * 1 - Per card interrupt spinlock (to protect structures and such)
27 * 2 - Per SCQ scq spinlock
28 * 3 - Per card resource spinlock (to access registers, etc.)
30 * These must NEVER be grabbed in reverse order.
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/skbuff.h>
39 #include <linux/atmdev.h>
40 #include <linux/atm.h>
41 #include <linux/pci.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/types.h>
44 #include <linux/string.h>
45 #include <linux/delay.h>
46 #include <linux/init.h>
47 #include <linux/sched.h>
48 #include <linux/timer.h>
49 #include <linux/interrupt.h>
50 #include <linux/bitops.h>
51 #include <linux/slab.h>
52 #include <linux/idr.h>
54 #include <linux/uaccess.h>
55 #include <linux/atomic.h>
56 #include <linux/etherdevice.h>
58 #ifdef CONFIG_ATM_NICSTAR_USE_SUNI
60 #endif /* CONFIG_ATM_NICSTAR_USE_SUNI */
61 #ifdef CONFIG_ATM_NICSTAR_USE_IDT77105
63 #endif /* CONFIG_ATM_NICSTAR_USE_IDT77105 */
67 #include "nicstarmac.c"
69 /* Configurable parameters */
77 /* Do not touch these */
80 #define TXPRINTK(args...) printk(args)
82 #define TXPRINTK(args...)
86 #define RXPRINTK(args...) printk(args)
88 #define RXPRINTK(args...)
92 #define PRINTK(args...) printk(args)
94 #define PRINTK(args...) do {} while (0)
95 #endif /* GENERAL_DEBUG */
98 #define XPRINTK(args...) printk(args)
100 #define XPRINTK(args...)
101 #endif /* EXTRA_DEBUG */
105 #define CMD_BUSY(card) (readl((card)->membase + STAT) & NS_STAT_CMDBZ)
107 #define NS_DELAY mdelay(1)
109 #define PTR_DIFF(a, b) ((u32)((unsigned long)(a) - (unsigned long)(b)))
112 #define ATM_SKB(s) (&(s)->atm)
115 #define scq_virt_to_bus(scq, p) \
116 (scq->dma + ((unsigned long)(p) - (unsigned long)(scq)->org))
118 /* Function declarations */
120 static u32 ns_read_sram(ns_dev * card, u32 sram_address);
121 static void ns_write_sram(ns_dev * card, u32 sram_address, u32 * value,
123 static int ns_init_card(int i, struct pci_dev *pcidev);
124 static void ns_init_card_error(ns_dev * card, int error);
125 static scq_info *get_scq(ns_dev *card, int size, u32 scd);
126 static void free_scq(ns_dev *card, scq_info * scq, struct atm_vcc *vcc);
127 static void push_rxbufs(ns_dev *, struct sk_buff *);
128 static irqreturn_t ns_irq_handler(int irq, void *dev_id);
129 static int ns_open(struct atm_vcc *vcc);
130 static void ns_close(struct atm_vcc *vcc);
131 static void fill_tst(ns_dev * card, int n, vc_map * vc);
132 static int ns_send(struct atm_vcc *vcc, struct sk_buff *skb);
133 static int ns_send_bh(struct atm_vcc *vcc, struct sk_buff *skb);
134 static int push_scqe(ns_dev * card, vc_map * vc, scq_info * scq, ns_scqe * tbd,
135 struct sk_buff *skb, bool may_sleep);
136 static void process_tsq(ns_dev * card);
137 static void drain_scq(ns_dev * card, scq_info * scq, int pos);
138 static void process_rsq(ns_dev * card);
139 static void dequeue_rx(ns_dev * card, ns_rsqe * rsqe);
140 static void recycle_rx_buf(ns_dev * card, struct sk_buff *skb);
141 static void recycle_iovec_rx_bufs(ns_dev * card, struct iovec *iov, int count);
142 static void recycle_iov_buf(ns_dev * card, struct sk_buff *iovb);
143 static void dequeue_sm_buf(ns_dev * card, struct sk_buff *sb);
144 static void dequeue_lg_buf(ns_dev * card, struct sk_buff *lb);
145 static int ns_proc_read(struct atm_dev *dev, loff_t * pos, char *page);
146 static int ns_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg);
148 static void which_list(ns_dev * card, struct sk_buff *skb);
150 static void ns_poll(struct timer_list *unused);
151 static void ns_phy_put(struct atm_dev *dev, unsigned char value,
153 static unsigned char ns_phy_get(struct atm_dev *dev, unsigned long addr);
155 /* Global variables */
157 static struct ns_dev *cards[NS_MAX_CARDS];
158 static unsigned num_cards;
159 static const struct atmdev_ops atm_ops = {
164 .send_bh = ns_send_bh,
165 .phy_put = ns_phy_put,
166 .phy_get = ns_phy_get,
167 .proc_read = ns_proc_read,
168 .owner = THIS_MODULE,
171 static struct timer_list ns_timer;
172 static char *mac[NS_MAX_CARDS];
173 module_param_array(mac, charp, NULL, 0);
174 MODULE_LICENSE("GPL");
178 static int nicstar_init_one(struct pci_dev *pcidev,
179 const struct pci_device_id *ent)
181 static int index = -1;
187 error = ns_init_card(index, pcidev);
189 cards[index--] = NULL; /* don't increment index */
198 static void nicstar_remove_one(struct pci_dev *pcidev)
201 ns_dev *card = pci_get_drvdata(pcidev);
203 struct sk_buff *iovb;
209 if (cards[i] == NULL)
212 if (card->atmdev->phy && card->atmdev->phy->stop)
213 card->atmdev->phy->stop(card->atmdev);
215 /* Stop everything */
216 writel(0x00000000, card->membase + CFG);
218 /* De-register device */
219 atm_dev_deregister(card->atmdev);
221 /* Disable PCI device */
222 pci_disable_device(pcidev);
224 /* Free up resources */
226 PRINTK("nicstar%d: freeing %d huge buffers.\n", i, card->hbpool.count);
227 while ((hb = skb_dequeue(&card->hbpool.queue)) != NULL) {
228 dev_kfree_skb_any(hb);
231 PRINTK("nicstar%d: %d huge buffers freed.\n", i, j);
233 PRINTK("nicstar%d: freeing %d iovec buffers.\n", i,
234 card->iovpool.count);
235 while ((iovb = skb_dequeue(&card->iovpool.queue)) != NULL) {
236 dev_kfree_skb_any(iovb);
239 PRINTK("nicstar%d: %d iovec buffers freed.\n", i, j);
240 while ((lb = skb_dequeue(&card->lbpool.queue)) != NULL)
241 dev_kfree_skb_any(lb);
242 while ((sb = skb_dequeue(&card->sbpool.queue)) != NULL)
243 dev_kfree_skb_any(sb);
244 free_scq(card, card->scq0, NULL);
245 for (j = 0; j < NS_FRSCD_NUM; j++) {
246 if (card->scd2vc[j] != NULL)
247 free_scq(card, card->scd2vc[j]->scq, card->scd2vc[j]->tx_vcc);
249 idr_destroy(&card->idr);
250 dma_free_coherent(&card->pcidev->dev, NS_RSQSIZE + NS_RSQ_ALIGNMENT,
251 card->rsq.org, card->rsq.dma);
252 dma_free_coherent(&card->pcidev->dev, NS_TSQSIZE + NS_TSQ_ALIGNMENT,
253 card->tsq.org, card->tsq.dma);
254 free_irq(card->pcidev->irq, card);
255 iounmap(card->membase);
259 static const struct pci_device_id nicstar_pci_tbl[] = {
260 { PCI_VDEVICE(IDT, PCI_DEVICE_ID_IDT_IDT77201), 0 },
261 {0,} /* terminate list */
264 MODULE_DEVICE_TABLE(pci, nicstar_pci_tbl);
266 static struct pci_driver nicstar_driver = {
268 .id_table = nicstar_pci_tbl,
269 .probe = nicstar_init_one,
270 .remove = nicstar_remove_one,
273 static int __init nicstar_init(void)
275 unsigned error = 0; /* Initialized to remove compile warning */
277 XPRINTK("nicstar: nicstar_init() called.\n");
279 error = pci_register_driver(&nicstar_driver);
281 TXPRINTK("nicstar: TX debug enabled.\n");
282 RXPRINTK("nicstar: RX debug enabled.\n");
283 PRINTK("nicstar: General debug enabled.\n");
285 printk("nicstar: using PHY loopback.\n");
286 #endif /* PHY_LOOPBACK */
287 XPRINTK("nicstar: nicstar_init() returned.\n");
290 timer_setup(&ns_timer, ns_poll, 0);
291 ns_timer.expires = jiffies + NS_POLL_PERIOD;
292 add_timer(&ns_timer);
298 static void __exit nicstar_cleanup(void)
300 XPRINTK("nicstar: nicstar_cleanup() called.\n");
302 del_timer_sync(&ns_timer);
304 pci_unregister_driver(&nicstar_driver);
306 XPRINTK("nicstar: nicstar_cleanup() returned.\n");
309 static u32 ns_read_sram(ns_dev * card, u32 sram_address)
314 sram_address &= 0x0007FFFC; /* address must be dword aligned */
315 sram_address |= 0x50000000; /* SRAM read command */
316 spin_lock_irqsave(&card->res_lock, flags);
317 while (CMD_BUSY(card)) ;
318 writel(sram_address, card->membase + CMD);
319 while (CMD_BUSY(card)) ;
320 data = readl(card->membase + DR0);
321 spin_unlock_irqrestore(&card->res_lock, flags);
325 static void ns_write_sram(ns_dev * card, u32 sram_address, u32 * value,
330 count--; /* count range now is 0..3 instead of 1..4 */
332 c <<= 2; /* to use increments of 4 */
333 spin_lock_irqsave(&card->res_lock, flags);
334 while (CMD_BUSY(card)) ;
335 for (i = 0; i <= c; i += 4)
336 writel(*(value++), card->membase + i);
337 /* Note: DR# registers are the first 4 dwords in nicstar's memspace,
338 so card->membase + DR0 == card->membase */
340 sram_address &= 0x0007FFFC;
341 sram_address |= (0x40000000 | count);
342 writel(sram_address, card->membase + CMD);
343 spin_unlock_irqrestore(&card->res_lock, flags);
346 static int ns_init_card(int i, struct pci_dev *pcidev)
349 struct ns_dev *card = NULL;
350 unsigned char pci_latency;
356 unsigned long membase;
360 if (pci_enable_device(pcidev)) {
361 printk("nicstar%d: can't enable PCI device\n", i);
363 ns_init_card_error(card, error);
366 if (dma_set_mask_and_coherent(&pcidev->dev, DMA_BIT_MASK(32)) != 0) {
368 "nicstar%d: No suitable DMA available.\n", i);
370 ns_init_card_error(card, error);
374 card = kmalloc(sizeof(*card), GFP_KERNEL);
377 ("nicstar%d: can't allocate memory for device structure.\n",
380 ns_init_card_error(card, error);
384 spin_lock_init(&card->int_lock);
385 spin_lock_init(&card->res_lock);
387 pci_set_drvdata(pcidev, card);
391 card->pcidev = pcidev;
392 membase = pci_resource_start(pcidev, 1);
393 card->membase = ioremap(membase, NS_IOREMAP_SIZE);
394 if (!card->membase) {
395 printk("nicstar%d: can't ioremap() membase.\n", i);
397 ns_init_card_error(card, error);
400 PRINTK("nicstar%d: membase at 0x%p.\n", i, card->membase);
402 pci_set_master(pcidev);
404 if (pci_read_config_byte(pcidev, PCI_LATENCY_TIMER, &pci_latency) != 0) {
405 printk("nicstar%d: can't read PCI latency timer.\n", i);
407 ns_init_card_error(card, error);
410 #ifdef NS_PCI_LATENCY
411 if (pci_latency < NS_PCI_LATENCY) {
412 PRINTK("nicstar%d: setting PCI latency timer to %d.\n", i,
414 for (j = 1; j < 4; j++) {
415 if (pci_write_config_byte
416 (pcidev, PCI_LATENCY_TIMER, NS_PCI_LATENCY) != 0)
421 ("nicstar%d: can't set PCI latency timer to %d.\n",
424 ns_init_card_error(card, error);
428 #endif /* NS_PCI_LATENCY */
430 /* Clear timer overflow */
431 data = readl(card->membase + STAT);
432 if (data & NS_STAT_TMROF)
433 writel(NS_STAT_TMROF, card->membase + STAT);
436 writel(NS_CFG_SWRST, card->membase + CFG);
438 writel(0x00000000, card->membase + CFG);
441 writel(0x00000008, card->membase + GP);
443 writel(0x00000001, card->membase + GP);
445 while (CMD_BUSY(card)) ;
446 writel(NS_CMD_WRITE_UTILITY | 0x00000100, card->membase + CMD); /* Sync UTOPIA with SAR clock */
449 /* Detect PHY type */
450 while (CMD_BUSY(card)) ;
451 writel(NS_CMD_READ_UTILITY | 0x00000200, card->membase + CMD);
452 while (CMD_BUSY(card)) ;
453 data = readl(card->membase + DR0);
456 printk("nicstar%d: PHY seems to be 25 Mbps.\n", i);
457 card->max_pcr = ATM_25_PCR;
458 while (CMD_BUSY(card)) ;
459 writel(0x00000008, card->membase + DR0);
460 writel(NS_CMD_WRITE_UTILITY | 0x00000200, card->membase + CMD);
461 /* Clear an eventual pending interrupt */
462 writel(NS_STAT_SFBQF, card->membase + STAT);
464 while (CMD_BUSY(card)) ;
465 writel(0x00000022, card->membase + DR0);
466 writel(NS_CMD_WRITE_UTILITY | 0x00000202, card->membase + CMD);
467 #endif /* PHY_LOOPBACK */
471 printk("nicstar%d: PHY seems to be 155 Mbps.\n", i);
472 card->max_pcr = ATM_OC3_PCR;
474 while (CMD_BUSY(card)) ;
475 writel(0x00000002, card->membase + DR0);
476 writel(NS_CMD_WRITE_UTILITY | 0x00000205, card->membase + CMD);
477 #endif /* PHY_LOOPBACK */
480 printk("nicstar%d: unknown PHY type (0x%08X).\n", i, data);
482 ns_init_card_error(card, error);
485 writel(0x00000000, card->membase + GP);
487 /* Determine SRAM size */
489 ns_write_sram(card, 0x1C003, &data, 1);
491 ns_write_sram(card, 0x14003, &data, 1);
492 if (ns_read_sram(card, 0x14003) == 0x89ABCDEF &&
493 ns_read_sram(card, 0x1C003) == 0x76543210)
494 card->sram_size = 128;
496 card->sram_size = 32;
497 PRINTK("nicstar%d: %dK x 32bit SRAM size.\n", i, card->sram_size);
499 card->rct_size = NS_MAX_RCTSIZE;
501 #if (NS_MAX_RCTSIZE == 4096)
502 if (card->sram_size == 128)
504 ("nicstar%d: limiting maximum VCI. See NS_MAX_RCTSIZE in nicstar.h\n",
506 #elif (NS_MAX_RCTSIZE == 16384)
507 if (card->sram_size == 32) {
509 ("nicstar%d: wasting memory. See NS_MAX_RCTSIZE in nicstar.h\n",
511 card->rct_size = 4096;
514 #error NS_MAX_RCTSIZE must be either 4096 or 16384 in nicstar.c
517 card->vpibits = NS_VPIBITS;
518 if (card->rct_size == 4096)
519 card->vcibits = 12 - NS_VPIBITS;
520 else /* card->rct_size == 16384 */
521 card->vcibits = 14 - NS_VPIBITS;
523 /* Initialize the nicstar eeprom/eprom stuff, for the MAC addr */
525 nicstar_init_eprom(card->membase);
527 /* Set the VPI/VCI MSb mask to zero so we can receive OAM cells */
528 writel(0x00000000, card->membase + VPM);
532 (pcidev->irq, &ns_irq_handler, IRQF_SHARED, "nicstar", card) != 0) {
533 pr_err("nicstar%d: can't allocate IRQ %d.\n", i, pcidev->irq);
535 ns_init_card_error(card, error);
540 card->tsq.org = dma_alloc_coherent(&card->pcidev->dev,
541 NS_TSQSIZE + NS_TSQ_ALIGNMENT,
542 &card->tsq.dma, GFP_KERNEL);
543 if (card->tsq.org == NULL) {
544 printk("nicstar%d: can't allocate TSQ.\n", i);
546 ns_init_card_error(card, error);
549 card->tsq.base = PTR_ALIGN(card->tsq.org, NS_TSQ_ALIGNMENT);
550 card->tsq.next = card->tsq.base;
551 card->tsq.last = card->tsq.base + (NS_TSQ_NUM_ENTRIES - 1);
552 for (j = 0; j < NS_TSQ_NUM_ENTRIES; j++)
553 ns_tsi_init(card->tsq.base + j);
554 writel(0x00000000, card->membase + TSQH);
555 writel(ALIGN(card->tsq.dma, NS_TSQ_ALIGNMENT), card->membase + TSQB);
556 PRINTK("nicstar%d: TSQ base at 0x%p.\n", i, card->tsq.base);
559 card->rsq.org = dma_alloc_coherent(&card->pcidev->dev,
560 NS_RSQSIZE + NS_RSQ_ALIGNMENT,
561 &card->rsq.dma, GFP_KERNEL);
562 if (card->rsq.org == NULL) {
563 printk("nicstar%d: can't allocate RSQ.\n", i);
565 ns_init_card_error(card, error);
568 card->rsq.base = PTR_ALIGN(card->rsq.org, NS_RSQ_ALIGNMENT);
569 card->rsq.next = card->rsq.base;
570 card->rsq.last = card->rsq.base + (NS_RSQ_NUM_ENTRIES - 1);
571 for (j = 0; j < NS_RSQ_NUM_ENTRIES; j++)
572 ns_rsqe_init(card->rsq.base + j);
573 writel(0x00000000, card->membase + RSQH);
574 writel(ALIGN(card->rsq.dma, NS_RSQ_ALIGNMENT), card->membase + RSQB);
575 PRINTK("nicstar%d: RSQ base at 0x%p.\n", i, card->rsq.base);
577 /* Initialize SCQ0, the only VBR SCQ used */
580 card->scq0 = get_scq(card, VBR_SCQSIZE, NS_VRSCD0);
581 if (card->scq0 == NULL) {
582 printk("nicstar%d: can't get SCQ0.\n", i);
584 ns_init_card_error(card, error);
587 u32d[0] = scq_virt_to_bus(card->scq0, card->scq0->base);
588 u32d[1] = (u32) 0x00000000;
589 u32d[2] = (u32) 0xffffffff;
590 u32d[3] = (u32) 0x00000000;
591 ns_write_sram(card, NS_VRSCD0, u32d, 4);
592 ns_write_sram(card, NS_VRSCD1, u32d, 4); /* These last two won't be used */
593 ns_write_sram(card, NS_VRSCD2, u32d, 4); /* but are initialized, just in case... */
594 card->scq0->scd = NS_VRSCD0;
595 PRINTK("nicstar%d: VBR-SCQ0 base at 0x%p.\n", i, card->scq0->base);
597 /* Initialize TSTs */
598 card->tst_addr = NS_TST0;
599 card->tst_free_entries = NS_TST_NUM_ENTRIES;
600 data = NS_TST_OPCODE_VARIABLE;
601 for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
602 ns_write_sram(card, NS_TST0 + j, &data, 1);
603 data = ns_tste_make(NS_TST_OPCODE_END, NS_TST0);
604 ns_write_sram(card, NS_TST0 + NS_TST_NUM_ENTRIES, &data, 1);
605 for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
606 ns_write_sram(card, NS_TST1 + j, &data, 1);
607 data = ns_tste_make(NS_TST_OPCODE_END, NS_TST1);
608 ns_write_sram(card, NS_TST1 + NS_TST_NUM_ENTRIES, &data, 1);
609 for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
610 card->tste2vc[j] = NULL;
611 writel(NS_TST0 << 2, card->membase + TSTB);
613 /* Initialize RCT. AAL type is set on opening the VC. */
615 u32d[0] = NS_RCTE_RAWCELLINTEN;
617 u32d[0] = 0x00000000;
618 #endif /* RCQ_SUPPORT */
619 u32d[1] = 0x00000000;
620 u32d[2] = 0x00000000;
621 u32d[3] = 0xFFFFFFFF;
622 for (j = 0; j < card->rct_size; j++)
623 ns_write_sram(card, j * 4, u32d, 4);
625 memset(card->vcmap, 0, sizeof(card->vcmap));
627 for (j = 0; j < NS_FRSCD_NUM; j++)
628 card->scd2vc[j] = NULL;
630 /* Initialize buffer levels */
631 card->sbnr.min = MIN_SB;
632 card->sbnr.init = NUM_SB;
633 card->sbnr.max = MAX_SB;
634 card->lbnr.min = MIN_LB;
635 card->lbnr.init = NUM_LB;
636 card->lbnr.max = MAX_LB;
637 card->iovnr.min = MIN_IOVB;
638 card->iovnr.init = NUM_IOVB;
639 card->iovnr.max = MAX_IOVB;
640 card->hbnr.min = MIN_HB;
641 card->hbnr.init = NUM_HB;
642 card->hbnr.max = MAX_HB;
644 card->sm_handle = NULL;
645 card->sm_addr = 0x00000000;
646 card->lg_handle = NULL;
647 card->lg_addr = 0x00000000;
649 card->efbie = 1; /* To prevent push_rxbufs from enabling the interrupt */
651 idr_init(&card->idr);
653 /* Pre-allocate some huge buffers */
654 skb_queue_head_init(&card->hbpool.queue);
655 card->hbpool.count = 0;
656 for (j = 0; j < NUM_HB; j++) {
658 hb = __dev_alloc_skb(NS_HBUFSIZE, GFP_KERNEL);
661 ("nicstar%d: can't allocate %dth of %d huge buffers.\n",
664 ns_init_card_error(card, error);
667 NS_PRV_BUFTYPE(hb) = BUF_NONE;
668 skb_queue_tail(&card->hbpool.queue, hb);
669 card->hbpool.count++;
672 /* Allocate large buffers */
673 skb_queue_head_init(&card->lbpool.queue);
674 card->lbpool.count = 0; /* Not used */
675 for (j = 0; j < NUM_LB; j++) {
677 lb = __dev_alloc_skb(NS_LGSKBSIZE, GFP_KERNEL);
680 ("nicstar%d: can't allocate %dth of %d large buffers.\n",
683 ns_init_card_error(card, error);
686 NS_PRV_BUFTYPE(lb) = BUF_LG;
687 skb_queue_tail(&card->lbpool.queue, lb);
688 skb_reserve(lb, NS_SMBUFSIZE);
689 push_rxbufs(card, lb);
690 /* Due to the implementation of push_rxbufs() this is 1, not 0 */
693 card->rawcell = (struct ns_rcqe *) lb->data;
694 card->rawch = NS_PRV_DMA(lb);
697 /* Test for strange behaviour which leads to crashes */
699 ns_stat_lfbqc_get(readl(card->membase + STAT))) < card->lbnr.min) {
701 ("nicstar%d: Strange... Just allocated %d large buffers and lfbqc = %d.\n",
704 ns_init_card_error(card, error);
708 /* Allocate small buffers */
709 skb_queue_head_init(&card->sbpool.queue);
710 card->sbpool.count = 0; /* Not used */
711 for (j = 0; j < NUM_SB; j++) {
713 sb = __dev_alloc_skb(NS_SMSKBSIZE, GFP_KERNEL);
716 ("nicstar%d: can't allocate %dth of %d small buffers.\n",
719 ns_init_card_error(card, error);
722 NS_PRV_BUFTYPE(sb) = BUF_SM;
723 skb_queue_tail(&card->sbpool.queue, sb);
724 skb_reserve(sb, NS_AAL0_HEADER);
725 push_rxbufs(card, sb);
727 /* Test for strange behaviour which leads to crashes */
729 ns_stat_sfbqc_get(readl(card->membase + STAT))) < card->sbnr.min) {
731 ("nicstar%d: Strange... Just allocated %d small buffers and sfbqc = %d.\n",
734 ns_init_card_error(card, error);
738 /* Allocate iovec buffers */
739 skb_queue_head_init(&card->iovpool.queue);
740 card->iovpool.count = 0;
741 for (j = 0; j < NUM_IOVB; j++) {
742 struct sk_buff *iovb;
743 iovb = alloc_skb(NS_IOVBUFSIZE, GFP_KERNEL);
746 ("nicstar%d: can't allocate %dth of %d iovec buffers.\n",
749 ns_init_card_error(card, error);
752 NS_PRV_BUFTYPE(iovb) = BUF_NONE;
753 skb_queue_tail(&card->iovpool.queue, iovb);
754 card->iovpool.count++;
757 /* Configure NICStAR */
758 if (card->rct_size == 4096)
759 ns_cfg_rctsize = NS_CFG_RCTSIZE_4096_ENTRIES;
760 else /* (card->rct_size == 16384) */
761 ns_cfg_rctsize = NS_CFG_RCTSIZE_16384_ENTRIES;
765 /* Register device */
766 card->atmdev = atm_dev_register("nicstar", &card->pcidev->dev, &atm_ops,
768 if (card->atmdev == NULL) {
769 printk("nicstar%d: can't register device.\n", i);
771 ns_init_card_error(card, error);
775 if (mac[i] == NULL || !mac_pton(mac[i], card->atmdev->esi)) {
776 nicstar_read_eprom(card->membase, NICSTAR_EPROM_MAC_ADDR_OFFSET,
777 card->atmdev->esi, 6);
778 if (ether_addr_equal(card->atmdev->esi, "\x00\x00\x00\x00\x00\x00")) {
779 nicstar_read_eprom(card->membase,
780 NICSTAR_EPROM_MAC_ADDR_OFFSET_ALT,
781 card->atmdev->esi, 6);
785 printk("nicstar%d: MAC address %pM\n", i, card->atmdev->esi);
787 card->atmdev->dev_data = card;
788 card->atmdev->ci_range.vpi_bits = card->vpibits;
789 card->atmdev->ci_range.vci_bits = card->vcibits;
790 card->atmdev->link_rate = card->max_pcr;
791 card->atmdev->phy = NULL;
793 #ifdef CONFIG_ATM_NICSTAR_USE_SUNI
794 if (card->max_pcr == ATM_OC3_PCR)
795 suni_init(card->atmdev);
796 #endif /* CONFIG_ATM_NICSTAR_USE_SUNI */
798 #ifdef CONFIG_ATM_NICSTAR_USE_IDT77105
799 if (card->max_pcr == ATM_25_PCR)
800 idt77105_init(card->atmdev);
801 #endif /* CONFIG_ATM_NICSTAR_USE_IDT77105 */
803 if (card->atmdev->phy && card->atmdev->phy->start)
804 card->atmdev->phy->start(card->atmdev);
806 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 */
807 NS_CFG_RSQAFIE | NS_CFG_TXEN | NS_CFG_TXIE | NS_CFG_TSQFIE_OPT | /* Only enabled if ENABLE_TSQFIE */
808 NS_CFG_PHYIE, card->membase + CFG);
815 static void ns_init_card_error(ns_dev *card, int error)
818 writel(0x00000000, card->membase + CFG);
821 struct sk_buff *iovb;
822 while ((iovb = skb_dequeue(&card->iovpool.queue)) != NULL)
823 dev_kfree_skb_any(iovb);
827 while ((sb = skb_dequeue(&card->sbpool.queue)) != NULL)
828 dev_kfree_skb_any(sb);
829 free_scq(card, card->scq0, NULL);
833 while ((lb = skb_dequeue(&card->lbpool.queue)) != NULL)
834 dev_kfree_skb_any(lb);
838 while ((hb = skb_dequeue(&card->hbpool.queue)) != NULL)
839 dev_kfree_skb_any(hb);
842 dma_free_coherent(&card->pcidev->dev, NS_RSQSIZE + NS_RSQ_ALIGNMENT,
843 card->rsq.org, card->rsq.dma);
846 dma_free_coherent(&card->pcidev->dev, NS_TSQSIZE + NS_TSQ_ALIGNMENT,
847 card->tsq.org, card->tsq.dma);
850 free_irq(card->pcidev->irq, card);
853 iounmap(card->membase);
856 pci_disable_device(card->pcidev);
861 static scq_info *get_scq(ns_dev *card, int size, u32 scd)
865 if (size != VBR_SCQSIZE && size != CBR_SCQSIZE)
868 scq = kmalloc(sizeof(*scq), GFP_KERNEL);
871 scq->org = dma_alloc_coherent(&card->pcidev->dev,
872 2 * size, &scq->dma, GFP_KERNEL);
877 scq->skb = kcalloc(size / NS_SCQE_SIZE, sizeof(*scq->skb),
880 dma_free_coherent(&card->pcidev->dev,
881 2 * size, scq->org, scq->dma);
885 scq->num_entries = size / NS_SCQE_SIZE;
886 scq->base = PTR_ALIGN(scq->org, size);
887 scq->next = scq->base;
888 scq->last = scq->base + (scq->num_entries - 1);
889 scq->tail = scq->last;
892 init_waitqueue_head(&scq->scqfull_waitq);
894 spin_lock_init(&scq->lock);
899 /* For variable rate SCQ vcc must be NULL */
900 static void free_scq(ns_dev *card, scq_info *scq, struct atm_vcc *vcc)
904 if (scq->num_entries == VBR_SCQ_NUM_ENTRIES)
905 for (i = 0; i < scq->num_entries; i++) {
906 if (scq->skb[i] != NULL) {
907 vcc = ATM_SKB(scq->skb[i])->vcc;
908 if (vcc->pop != NULL)
909 vcc->pop(vcc, scq->skb[i]);
911 dev_kfree_skb_any(scq->skb[i]);
913 } else { /* vcc must be != NULL */
917 ("nicstar: free_scq() called with vcc == NULL for fixed rate scq.");
918 for (i = 0; i < scq->num_entries; i++)
919 dev_kfree_skb_any(scq->skb[i]);
921 for (i = 0; i < scq->num_entries; i++) {
922 if (scq->skb[i] != NULL) {
923 if (vcc->pop != NULL)
924 vcc->pop(vcc, scq->skb[i]);
926 dev_kfree_skb_any(scq->skb[i]);
931 dma_free_coherent(&card->pcidev->dev,
932 2 * (scq->num_entries == VBR_SCQ_NUM_ENTRIES ?
933 VBR_SCQSIZE : CBR_SCQSIZE),
938 /* The handles passed must be pointers to the sk_buff containing the small
939 or large buffer(s) cast to u32. */
940 static void push_rxbufs(ns_dev * card, struct sk_buff *skb)
942 struct sk_buff *handle1, *handle2;
952 addr1 = dma_map_single(&card->pcidev->dev,
954 (NS_PRV_BUFTYPE(skb) == BUF_SM
955 ? NS_SMSKBSIZE : NS_LGSKBSIZE),
957 NS_PRV_DMA(skb) = addr1; /* save so we can unmap later */
961 printk("nicstar%d: push_rxbufs called with addr1 = 0.\n",
963 #endif /* GENERAL_DEBUG */
965 stat = readl(card->membase + STAT);
966 card->sbfqc = ns_stat_sfbqc_get(stat);
967 card->lbfqc = ns_stat_lfbqc_get(stat);
968 if (NS_PRV_BUFTYPE(skb) == BUF_SM) {
971 addr2 = card->sm_addr;
972 handle2 = card->sm_handle;
973 card->sm_addr = 0x00000000;
974 card->sm_handle = NULL;
975 } else { /* (!sm_addr) */
977 card->sm_addr = addr1;
978 card->sm_handle = handle1;
981 } else { /* buf_type == BUF_LG */
985 addr2 = card->lg_addr;
986 handle2 = card->lg_handle;
987 card->lg_addr = 0x00000000;
988 card->lg_handle = NULL;
989 } else { /* (!lg_addr) */
991 card->lg_addr = addr1;
992 card->lg_handle = handle1;
998 if (NS_PRV_BUFTYPE(skb) == BUF_SM) {
999 if (card->sbfqc >= card->sbnr.max) {
1000 skb_unlink(handle1, &card->sbpool.queue);
1001 dev_kfree_skb_any(handle1);
1002 skb_unlink(handle2, &card->sbpool.queue);
1003 dev_kfree_skb_any(handle2);
1007 } else { /* (buf_type == BUF_LG) */
1009 if (card->lbfqc >= card->lbnr.max) {
1010 skb_unlink(handle1, &card->lbpool.queue);
1011 dev_kfree_skb_any(handle1);
1012 skb_unlink(handle2, &card->lbpool.queue);
1013 dev_kfree_skb_any(handle2);
1019 id1 = idr_alloc(&card->idr, handle1, 0, 0, GFP_ATOMIC);
1023 id2 = idr_alloc(&card->idr, handle2, 0, 0, GFP_ATOMIC);
1027 spin_lock_irqsave(&card->res_lock, flags);
1028 while (CMD_BUSY(card)) ;
1029 writel(addr2, card->membase + DR3);
1030 writel(id2, card->membase + DR2);
1031 writel(addr1, card->membase + DR1);
1032 writel(id1, card->membase + DR0);
1033 writel(NS_CMD_WRITE_FREEBUFQ | NS_PRV_BUFTYPE(skb),
1034 card->membase + CMD);
1035 spin_unlock_irqrestore(&card->res_lock, flags);
1037 XPRINTK("nicstar%d: Pushing %s buffers at 0x%x and 0x%x.\n",
1039 (NS_PRV_BUFTYPE(skb) == BUF_SM ? "small" : "large"),
1043 if (!card->efbie && card->sbfqc >= card->sbnr.min &&
1044 card->lbfqc >= card->lbnr.min) {
1046 writel((readl(card->membase + CFG) | NS_CFG_EFBIE),
1047 card->membase + CFG);
1054 static irqreturn_t ns_irq_handler(int irq, void *dev_id)
1058 struct atm_dev *dev;
1059 unsigned long flags;
1061 card = (ns_dev *) dev_id;
1065 PRINTK("nicstar%d: NICStAR generated an interrupt\n", card->index);
1067 spin_lock_irqsave(&card->int_lock, flags);
1069 stat_r = readl(card->membase + STAT);
1071 /* Transmit Status Indicator has been written to T. S. Queue */
1072 if (stat_r & NS_STAT_TSIF) {
1073 TXPRINTK("nicstar%d: TSI interrupt\n", card->index);
1075 writel(NS_STAT_TSIF, card->membase + STAT);
1078 /* Incomplete CS-PDU has been transmitted */
1079 if (stat_r & NS_STAT_TXICP) {
1080 writel(NS_STAT_TXICP, card->membase + STAT);
1081 TXPRINTK("nicstar%d: Incomplete CS-PDU transmitted.\n",
1085 /* Transmit Status Queue 7/8 full */
1086 if (stat_r & NS_STAT_TSQF) {
1087 writel(NS_STAT_TSQF, card->membase + STAT);
1088 PRINTK("nicstar%d: TSQ full.\n", card->index);
1092 /* Timer overflow */
1093 if (stat_r & NS_STAT_TMROF) {
1094 writel(NS_STAT_TMROF, card->membase + STAT);
1095 PRINTK("nicstar%d: Timer overflow.\n", card->index);
1098 /* PHY device interrupt signal active */
1099 if (stat_r & NS_STAT_PHYI) {
1100 writel(NS_STAT_PHYI, card->membase + STAT);
1101 PRINTK("nicstar%d: PHY interrupt.\n", card->index);
1102 if (dev->phy && dev->phy->interrupt) {
1103 dev->phy->interrupt(dev);
1107 /* Small Buffer Queue is full */
1108 if (stat_r & NS_STAT_SFBQF) {
1109 writel(NS_STAT_SFBQF, card->membase + STAT);
1110 printk("nicstar%d: Small free buffer queue is full.\n",
1114 /* Large Buffer Queue is full */
1115 if (stat_r & NS_STAT_LFBQF) {
1116 writel(NS_STAT_LFBQF, card->membase + STAT);
1117 printk("nicstar%d: Large free buffer queue is full.\n",
1121 /* Receive Status Queue is full */
1122 if (stat_r & NS_STAT_RSQF) {
1123 writel(NS_STAT_RSQF, card->membase + STAT);
1124 printk("nicstar%d: RSQ full.\n", card->index);
1128 /* Complete CS-PDU received */
1129 if (stat_r & NS_STAT_EOPDU) {
1130 RXPRINTK("nicstar%d: End of CS-PDU received.\n", card->index);
1132 writel(NS_STAT_EOPDU, card->membase + STAT);
1135 /* Raw cell received */
1136 if (stat_r & NS_STAT_RAWCF) {
1137 writel(NS_STAT_RAWCF, card->membase + STAT);
1139 printk("nicstar%d: Raw cell received and no support yet...\n",
1141 #endif /* RCQ_SUPPORT */
1142 /* NOTE: the following procedure may keep a raw cell pending until the
1143 next interrupt. As this preliminary support is only meant to
1144 avoid buffer leakage, this is not an issue. */
1145 while (readl(card->membase + RAWCT) != card->rawch) {
1147 if (ns_rcqe_islast(card->rawcell)) {
1148 struct sk_buff *oldbuf;
1150 oldbuf = card->rcbuf;
1151 card->rcbuf = idr_find(&card->idr,
1152 ns_rcqe_nextbufhandle(card->rawcell));
1153 card->rawch = NS_PRV_DMA(card->rcbuf);
1154 card->rawcell = (struct ns_rcqe *)
1156 recycle_rx_buf(card, oldbuf);
1158 card->rawch += NS_RCQE_SIZE;
1164 /* Small buffer queue is empty */
1165 if (stat_r & NS_STAT_SFBQE) {
1169 writel(NS_STAT_SFBQE, card->membase + STAT);
1170 printk("nicstar%d: Small free buffer queue empty.\n",
1172 for (i = 0; i < card->sbnr.min; i++) {
1173 sb = dev_alloc_skb(NS_SMSKBSIZE);
1175 writel(readl(card->membase + CFG) &
1176 ~NS_CFG_EFBIE, card->membase + CFG);
1180 NS_PRV_BUFTYPE(sb) = BUF_SM;
1181 skb_queue_tail(&card->sbpool.queue, sb);
1182 skb_reserve(sb, NS_AAL0_HEADER);
1183 push_rxbufs(card, sb);
1189 /* Large buffer queue empty */
1190 if (stat_r & NS_STAT_LFBQE) {
1194 writel(NS_STAT_LFBQE, card->membase + STAT);
1195 printk("nicstar%d: Large free buffer queue empty.\n",
1197 for (i = 0; i < card->lbnr.min; i++) {
1198 lb = dev_alloc_skb(NS_LGSKBSIZE);
1200 writel(readl(card->membase + CFG) &
1201 ~NS_CFG_EFBIE, card->membase + CFG);
1205 NS_PRV_BUFTYPE(lb) = BUF_LG;
1206 skb_queue_tail(&card->lbpool.queue, lb);
1207 skb_reserve(lb, NS_SMBUFSIZE);
1208 push_rxbufs(card, lb);
1214 /* Receive Status Queue is 7/8 full */
1215 if (stat_r & NS_STAT_RSQAF) {
1216 writel(NS_STAT_RSQAF, card->membase + STAT);
1217 RXPRINTK("nicstar%d: RSQ almost full.\n", card->index);
1221 spin_unlock_irqrestore(&card->int_lock, flags);
1222 PRINTK("nicstar%d: end of interrupt service\n", card->index);
1226 static int ns_open(struct atm_vcc *vcc)
1230 unsigned long tmpl, modl;
1231 int tcr, tcra; /* target cell rate, and absolute value */
1232 int n = 0; /* Number of entries in the TST. Initialized to remove
1233 the compiler warning. */
1235 int frscdi = 0; /* Index of the SCD. Initialized to remove the compiler
1236 warning. How I wish compilers were clever enough to
1237 tell which variables can truly be used
1239 int inuse; /* tx or rx vc already in use by another vcc */
1240 short vpi = vcc->vpi;
1243 card = (ns_dev *) vcc->dev->dev_data;
1244 PRINTK("nicstar%d: opening vpi.vci %d.%d \n", card->index, (int)vpi,
1246 if (vcc->qos.aal != ATM_AAL5 && vcc->qos.aal != ATM_AAL0) {
1247 PRINTK("nicstar%d: unsupported AAL.\n", card->index);
1251 vc = &(card->vcmap[vpi << card->vcibits | vci]);
1255 if (vcc->qos.txtp.traffic_class != ATM_NONE && vc->tx)
1257 if (vcc->qos.rxtp.traffic_class != ATM_NONE && vc->rx)
1260 printk("nicstar%d: %s vci already in use.\n", card->index,
1261 inuse == 1 ? "tx" : inuse == 2 ? "rx" : "tx and rx");
1265 set_bit(ATM_VF_ADDR, &vcc->flags);
1267 /* NOTE: You are not allowed to modify an open connection's QOS. To change
1268 that, remove the ATM_VF_PARTIAL flag checking. There may be other changes
1269 needed to do that. */
1270 if (!test_bit(ATM_VF_PARTIAL, &vcc->flags)) {
1273 set_bit(ATM_VF_PARTIAL, &vcc->flags);
1274 if (vcc->qos.txtp.traffic_class == ATM_CBR) {
1275 /* Check requested cell rate and availability of SCD */
1276 if (vcc->qos.txtp.max_pcr == 0 && vcc->qos.txtp.pcr == 0
1277 && vcc->qos.txtp.min_pcr == 0) {
1279 ("nicstar%d: trying to open a CBR vc with cell rate = 0 \n",
1281 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1282 clear_bit(ATM_VF_ADDR, &vcc->flags);
1286 tcr = atm_pcr_goal(&(vcc->qos.txtp));
1287 tcra = tcr >= 0 ? tcr : -tcr;
1289 PRINTK("nicstar%d: target cell rate = %d.\n",
1290 card->index, vcc->qos.txtp.max_pcr);
1293 (unsigned long)tcra *(unsigned long)
1295 modl = tmpl % card->max_pcr;
1297 n = (int)(tmpl / card->max_pcr);
1301 } else if (tcr == 0) {
1303 (card->tst_free_entries -
1304 NS_TST_RESERVED)) <= 0) {
1306 ("nicstar%d: no CBR bandwidth free.\n",
1308 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1309 clear_bit(ATM_VF_ADDR, &vcc->flags);
1316 ("nicstar%d: selected bandwidth < granularity.\n",
1318 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1319 clear_bit(ATM_VF_ADDR, &vcc->flags);
1323 if (n > (card->tst_free_entries - NS_TST_RESERVED)) {
1325 ("nicstar%d: not enough free CBR bandwidth.\n",
1327 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1328 clear_bit(ATM_VF_ADDR, &vcc->flags);
1331 card->tst_free_entries -= n;
1333 XPRINTK("nicstar%d: writing %d tst entries.\n",
1335 for (frscdi = 0; frscdi < NS_FRSCD_NUM; frscdi++) {
1336 if (card->scd2vc[frscdi] == NULL) {
1337 card->scd2vc[frscdi] = vc;
1341 if (frscdi == NS_FRSCD_NUM) {
1343 ("nicstar%d: no SCD available for CBR channel.\n",
1345 card->tst_free_entries += n;
1346 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1347 clear_bit(ATM_VF_ADDR, &vcc->flags);
1351 vc->cbr_scd = NS_FRSCD + frscdi * NS_FRSCD_SIZE;
1353 scq = get_scq(card, CBR_SCQSIZE, vc->cbr_scd);
1355 PRINTK("nicstar%d: can't get fixed rate SCQ.\n",
1357 card->scd2vc[frscdi] = NULL;
1358 card->tst_free_entries += n;
1359 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1360 clear_bit(ATM_VF_ADDR, &vcc->flags);
1364 u32d[0] = scq_virt_to_bus(scq, scq->base);
1365 u32d[1] = (u32) 0x00000000;
1366 u32d[2] = (u32) 0xffffffff;
1367 u32d[3] = (u32) 0x00000000;
1368 ns_write_sram(card, vc->cbr_scd, u32d, 4);
1370 fill_tst(card, n, vc);
1371 } else if (vcc->qos.txtp.traffic_class == ATM_UBR) {
1372 vc->cbr_scd = 0x00000000;
1373 vc->scq = card->scq0;
1376 if (vcc->qos.txtp.traffic_class != ATM_NONE) {
1381 if (vcc->qos.rxtp.traffic_class != ATM_NONE) {
1388 /* Open the connection in hardware */
1389 if (vcc->qos.aal == ATM_AAL5)
1390 status = NS_RCTE_AAL5 | NS_RCTE_CONNECTOPEN;
1391 else /* vcc->qos.aal == ATM_AAL0 */
1392 status = NS_RCTE_AAL0 | NS_RCTE_CONNECTOPEN;
1394 status |= NS_RCTE_RAWCELLINTEN;
1395 #endif /* RCQ_SUPPORT */
1398 (vpi << card->vcibits | vci) *
1399 NS_RCT_ENTRY_SIZE, &status, 1);
1404 set_bit(ATM_VF_READY, &vcc->flags);
1408 static void ns_close(struct atm_vcc *vcc)
1416 card = vcc->dev->dev_data;
1417 PRINTK("nicstar%d: closing vpi.vci %d.%d \n", card->index,
1418 (int)vcc->vpi, vcc->vci);
1420 clear_bit(ATM_VF_READY, &vcc->flags);
1422 if (vcc->qos.rxtp.traffic_class != ATM_NONE) {
1424 unsigned long flags;
1428 (vcc->vpi << card->vcibits | vcc->vci) * NS_RCT_ENTRY_SIZE;
1429 spin_lock_irqsave(&card->res_lock, flags);
1430 while (CMD_BUSY(card)) ;
1431 writel(NS_CMD_CLOSE_CONNECTION | addr << 2,
1432 card->membase + CMD);
1433 spin_unlock_irqrestore(&card->res_lock, flags);
1436 if (vc->rx_iov != NULL) {
1437 struct sk_buff *iovb;
1440 stat = readl(card->membase + STAT);
1441 card->sbfqc = ns_stat_sfbqc_get(stat);
1442 card->lbfqc = ns_stat_lfbqc_get(stat);
1445 ("nicstar%d: closing a VC with pending rx buffers.\n",
1448 recycle_iovec_rx_bufs(card, (struct iovec *)iovb->data,
1449 NS_PRV_IOVCNT(iovb));
1450 NS_PRV_IOVCNT(iovb) = 0;
1451 spin_lock_irqsave(&card->int_lock, flags);
1452 recycle_iov_buf(card, iovb);
1453 spin_unlock_irqrestore(&card->int_lock, flags);
1458 if (vcc->qos.txtp.traffic_class != ATM_NONE) {
1462 if (vcc->qos.txtp.traffic_class == ATM_CBR) {
1463 unsigned long flags;
1470 spin_lock_irqsave(&scq->lock, flags);
1472 if (scqep == scq->base)
1476 if (scqep == scq->tail) {
1477 spin_unlock_irqrestore(&scq->lock, flags);
1480 /* If the last entry is not a TSR, place one in the SCQ in order to
1481 be able to completely drain it and then close. */
1482 if (!ns_scqe_is_tsr(scqep) && scq->tail != scq->next) {
1488 tsr.word_1 = ns_tsr_mkword_1(NS_TSR_INTENABLE);
1489 scdi = (vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE;
1490 scqi = scq->next - scq->base;
1491 tsr.word_2 = ns_tsr_mkword_2(scdi, scqi);
1492 tsr.word_3 = 0x00000000;
1493 tsr.word_4 = 0x00000000;
1496 scq->skb[index] = NULL;
1497 if (scq->next == scq->last)
1498 scq->next = scq->base;
1501 data = scq_virt_to_bus(scq, scq->next);
1502 ns_write_sram(card, scq->scd, &data, 1);
1504 spin_unlock_irqrestore(&scq->lock, flags);
1508 /* Free all TST entries */
1509 data = NS_TST_OPCODE_VARIABLE;
1510 for (i = 0; i < NS_TST_NUM_ENTRIES; i++) {
1511 if (card->tste2vc[i] == vc) {
1512 ns_write_sram(card, card->tst_addr + i, &data,
1514 card->tste2vc[i] = NULL;
1515 card->tst_free_entries++;
1519 card->scd2vc[(vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE] = NULL;
1520 free_scq(card, vc->scq, vcc);
1523 /* remove all references to vcc before deleting it */
1524 if (vcc->qos.txtp.traffic_class != ATM_NONE) {
1525 unsigned long flags;
1526 scq_info *scq = card->scq0;
1528 spin_lock_irqsave(&scq->lock, flags);
1530 for (i = 0; i < scq->num_entries; i++) {
1531 if (scq->skb[i] && ATM_SKB(scq->skb[i])->vcc == vcc) {
1532 ATM_SKB(scq->skb[i])->vcc = NULL;
1533 atm_return(vcc, scq->skb[i]->truesize);
1535 ("nicstar: deleted pending vcc mapping\n");
1539 spin_unlock_irqrestore(&scq->lock, flags);
1542 vcc->dev_data = NULL;
1543 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
1544 clear_bit(ATM_VF_ADDR, &vcc->flags);
1549 stat = readl(card->membase + STAT);
1550 cfg = readl(card->membase + CFG);
1551 printk("STAT = 0x%08X CFG = 0x%08X \n", stat, cfg);
1553 ("TSQ: base = 0x%p next = 0x%p last = 0x%p TSQT = 0x%08X \n",
1554 card->tsq.base, card->tsq.next,
1555 card->tsq.last, readl(card->membase + TSQT));
1557 ("RSQ: base = 0x%p next = 0x%p last = 0x%p RSQT = 0x%08X \n",
1558 card->rsq.base, card->rsq.next,
1559 card->rsq.last, readl(card->membase + RSQT));
1560 printk("Empty free buffer queue interrupt %s \n",
1561 card->efbie ? "enabled" : "disabled");
1562 printk("SBCNT = %d count = %d LBCNT = %d count = %d \n",
1563 ns_stat_sfbqc_get(stat), card->sbpool.count,
1564 ns_stat_lfbqc_get(stat), card->lbpool.count);
1565 printk("hbpool.count = %d iovpool.count = %d \n",
1566 card->hbpool.count, card->iovpool.count);
1568 #endif /* RX_DEBUG */
1571 static void fill_tst(ns_dev * card, int n, vc_map * vc)
1578 /* It would be very complicated to keep the two TSTs synchronized while
1579 assuring that writes are only made to the inactive TST. So, for now I
1580 will use only one TST. If problems occur, I will change this again */
1582 new_tst = card->tst_addr;
1584 /* Fill procedure */
1586 for (e = 0; e < NS_TST_NUM_ENTRIES; e++) {
1587 if (card->tste2vc[e] == NULL)
1590 if (e == NS_TST_NUM_ENTRIES) {
1591 printk("nicstar%d: No free TST entries found. \n", card->index);
1596 cl = NS_TST_NUM_ENTRIES;
1597 data = ns_tste_make(NS_TST_OPCODE_FIXED, vc->cbr_scd);
1600 if (cl >= NS_TST_NUM_ENTRIES && card->tste2vc[e] == NULL) {
1601 card->tste2vc[e] = vc;
1602 ns_write_sram(card, new_tst + e, &data, 1);
1603 cl -= NS_TST_NUM_ENTRIES;
1607 if (++e == NS_TST_NUM_ENTRIES) {
1613 /* End of fill procedure */
1615 data = ns_tste_make(NS_TST_OPCODE_END, new_tst);
1616 ns_write_sram(card, new_tst + NS_TST_NUM_ENTRIES, &data, 1);
1617 ns_write_sram(card, card->tst_addr + NS_TST_NUM_ENTRIES, &data, 1);
1618 card->tst_addr = new_tst;
1621 static int _ns_send(struct atm_vcc *vcc, struct sk_buff *skb, bool may_sleep)
1626 unsigned long buflen;
1628 u32 flags; /* TBD flags, not CPU flags */
1630 card = vcc->dev->dev_data;
1631 TXPRINTK("nicstar%d: ns_send() called.\n", card->index);
1632 if ((vc = (vc_map *) vcc->dev_data) == NULL) {
1633 printk("nicstar%d: vcc->dev_data == NULL on ns_send().\n",
1635 atomic_inc(&vcc->stats->tx_err);
1636 dev_kfree_skb_any(skb);
1641 printk("nicstar%d: Trying to transmit on a non-tx VC.\n",
1643 atomic_inc(&vcc->stats->tx_err);
1644 dev_kfree_skb_any(skb);
1648 if (vcc->qos.aal != ATM_AAL5 && vcc->qos.aal != ATM_AAL0) {
1649 printk("nicstar%d: Only AAL0 and AAL5 are supported.\n",
1651 atomic_inc(&vcc->stats->tx_err);
1652 dev_kfree_skb_any(skb);
1656 if (skb_shinfo(skb)->nr_frags != 0) {
1657 printk("nicstar%d: No scatter-gather yet.\n", card->index);
1658 atomic_inc(&vcc->stats->tx_err);
1659 dev_kfree_skb_any(skb);
1663 ATM_SKB(skb)->vcc = vcc;
1665 NS_PRV_DMA(skb) = dma_map_single(&card->pcidev->dev, skb->data,
1666 skb->len, DMA_TO_DEVICE);
1668 if (vcc->qos.aal == ATM_AAL5) {
1669 buflen = (skb->len + 47 + 8) / 48 * 48; /* Multiple of 48 */
1670 flags = NS_TBD_AAL5;
1671 scqe.word_2 = cpu_to_le32(NS_PRV_DMA(skb));
1672 scqe.word_3 = cpu_to_le32(skb->len);
1674 ns_tbd_mkword_4(0, (u32) vcc->vpi, (u32) vcc->vci, 0,
1676 atm_options & ATM_ATMOPT_CLP ? 1 : 0);
1677 flags |= NS_TBD_EOPDU;
1678 } else { /* (vcc->qos.aal == ATM_AAL0) */
1680 buflen = ATM_CELL_PAYLOAD; /* i.e., 48 bytes */
1681 flags = NS_TBD_AAL0;
1682 scqe.word_2 = cpu_to_le32(NS_PRV_DMA(skb) + NS_AAL0_HEADER);
1683 scqe.word_3 = cpu_to_le32(0x00000000);
1684 if (*skb->data & 0x02) /* Payload type 1 - end of pdu */
1685 flags |= NS_TBD_EOPDU;
1687 cpu_to_le32(*((u32 *) skb->data) & ~NS_TBD_VC_MASK);
1688 /* Force the VPI/VCI to be the same as in VCC struct */
1690 cpu_to_le32((((u32) vcc->
1691 vpi) << NS_TBD_VPI_SHIFT | ((u32) vcc->
1693 NS_TBD_VCI_SHIFT) & NS_TBD_VC_MASK);
1696 if (vcc->qos.txtp.traffic_class == ATM_CBR) {
1697 scqe.word_1 = ns_tbd_mkword_1_novbr(flags, (u32) buflen);
1698 scq = ((vc_map *) vcc->dev_data)->scq;
1701 ns_tbd_mkword_1(flags, (u32) 1, (u32) 1, (u32) buflen);
1705 if (push_scqe(card, vc, scq, &scqe, skb, may_sleep) != 0) {
1706 atomic_inc(&vcc->stats->tx_err);
1707 dma_unmap_single(&card->pcidev->dev, NS_PRV_DMA(skb), skb->len,
1709 dev_kfree_skb_any(skb);
1712 atomic_inc(&vcc->stats->tx);
1717 static int ns_send(struct atm_vcc *vcc, struct sk_buff *skb)
1719 return _ns_send(vcc, skb, true);
1722 static int ns_send_bh(struct atm_vcc *vcc, struct sk_buff *skb)
1724 return _ns_send(vcc, skb, false);
1727 static int push_scqe(ns_dev * card, vc_map * vc, scq_info * scq, ns_scqe * tbd,
1728 struct sk_buff *skb, bool may_sleep)
1730 unsigned long flags;
1737 spin_lock_irqsave(&scq->lock, flags);
1738 while (scq->tail == scq->next) {
1740 spin_unlock_irqrestore(&scq->lock, flags);
1741 printk("nicstar%d: Error pushing TBD.\n", card->index);
1746 wait_event_interruptible_lock_irq_timeout(scq->scqfull_waitq,
1747 scq->tail != scq->next,
1752 spin_unlock_irqrestore(&scq->lock, flags);
1753 printk("nicstar%d: Timeout pushing TBD.\n",
1759 index = (int)(scq->next - scq->base);
1760 scq->skb[index] = skb;
1761 XPRINTK("nicstar%d: sending skb at 0x%p (pos %d).\n",
1762 card->index, skb, index);
1763 XPRINTK("nicstar%d: TBD written:\n0x%x\n0x%x\n0x%x\n0x%x\n at 0x%p.\n",
1764 card->index, le32_to_cpu(tbd->word_1), le32_to_cpu(tbd->word_2),
1765 le32_to_cpu(tbd->word_3), le32_to_cpu(tbd->word_4),
1767 if (scq->next == scq->last)
1768 scq->next = scq->base;
1773 if (scq->num_entries == VBR_SCQ_NUM_ENTRIES) {
1779 if (vc->tbd_count >= MAX_TBD_PER_VC
1780 || scq->tbd_count >= MAX_TBD_PER_SCQ) {
1783 while (scq->tail == scq->next) {
1785 data = scq_virt_to_bus(scq, scq->next);
1786 ns_write_sram(card, scq->scd, &data, 1);
1787 spin_unlock_irqrestore(&scq->lock, flags);
1788 printk("nicstar%d: Error pushing TSR.\n",
1796 wait_event_interruptible_lock_irq_timeout(scq->scqfull_waitq,
1797 scq->tail != scq->next,
1803 tsr.word_1 = ns_tsr_mkword_1(NS_TSR_INTENABLE);
1805 scdi = NS_TSR_SCDISVBR;
1807 scdi = (vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE;
1808 scqi = scq->next - scq->base;
1809 tsr.word_2 = ns_tsr_mkword_2(scdi, scqi);
1810 tsr.word_3 = 0x00000000;
1811 tsr.word_4 = 0x00000000;
1815 scq->skb[index] = NULL;
1817 ("nicstar%d: TSR written:\n0x%x\n0x%x\n0x%x\n0x%x\n at 0x%p.\n",
1818 card->index, le32_to_cpu(tsr.word_1),
1819 le32_to_cpu(tsr.word_2), le32_to_cpu(tsr.word_3),
1820 le32_to_cpu(tsr.word_4), scq->next);
1821 if (scq->next == scq->last)
1822 scq->next = scq->base;
1828 PRINTK("nicstar%d: Timeout pushing TSR.\n",
1831 data = scq_virt_to_bus(scq, scq->next);
1832 ns_write_sram(card, scq->scd, &data, 1);
1834 spin_unlock_irqrestore(&scq->lock, flags);
1839 static void process_tsq(ns_dev * card)
1843 ns_tsi *previous = NULL, *one_ahead, *two_ahead;
1844 int serviced_entries; /* flag indicating at least on entry was serviced */
1846 serviced_entries = 0;
1848 if (card->tsq.next == card->tsq.last)
1849 one_ahead = card->tsq.base;
1851 one_ahead = card->tsq.next + 1;
1853 if (one_ahead == card->tsq.last)
1854 two_ahead = card->tsq.base;
1856 two_ahead = one_ahead + 1;
1858 while (!ns_tsi_isempty(card->tsq.next) || !ns_tsi_isempty(one_ahead) ||
1859 !ns_tsi_isempty(two_ahead))
1860 /* At most two empty, as stated in the 77201 errata */
1862 serviced_entries = 1;
1864 /* Skip the one or two possible empty entries */
1865 while (ns_tsi_isempty(card->tsq.next)) {
1866 if (card->tsq.next == card->tsq.last)
1867 card->tsq.next = card->tsq.base;
1872 if (!ns_tsi_tmrof(card->tsq.next)) {
1873 scdi = ns_tsi_getscdindex(card->tsq.next);
1874 if (scdi == NS_TSI_SCDISVBR)
1877 if (card->scd2vc[scdi] == NULL) {
1879 ("nicstar%d: could not find VC from SCD index.\n",
1881 ns_tsi_init(card->tsq.next);
1884 scq = card->scd2vc[scdi]->scq;
1886 drain_scq(card, scq, ns_tsi_getscqpos(card->tsq.next));
1888 wake_up_interruptible(&(scq->scqfull_waitq));
1891 ns_tsi_init(card->tsq.next);
1892 previous = card->tsq.next;
1893 if (card->tsq.next == card->tsq.last)
1894 card->tsq.next = card->tsq.base;
1898 if (card->tsq.next == card->tsq.last)
1899 one_ahead = card->tsq.base;
1901 one_ahead = card->tsq.next + 1;
1903 if (one_ahead == card->tsq.last)
1904 two_ahead = card->tsq.base;
1906 two_ahead = one_ahead + 1;
1909 if (serviced_entries)
1910 writel(PTR_DIFF(previous, card->tsq.base),
1911 card->membase + TSQH);
1914 static void drain_scq(ns_dev * card, scq_info * scq, int pos)
1916 struct atm_vcc *vcc;
1917 struct sk_buff *skb;
1919 unsigned long flags;
1921 XPRINTK("nicstar%d: drain_scq() called, scq at 0x%p, pos %d.\n",
1922 card->index, scq, pos);
1923 if (pos >= scq->num_entries) {
1924 printk("nicstar%d: Bad index on drain_scq().\n", card->index);
1928 spin_lock_irqsave(&scq->lock, flags);
1929 i = (int)(scq->tail - scq->base);
1930 if (++i == scq->num_entries)
1934 XPRINTK("nicstar%d: freeing skb at 0x%p (index %d).\n",
1935 card->index, skb, i);
1937 dma_unmap_single(&card->pcidev->dev,
1941 vcc = ATM_SKB(skb)->vcc;
1942 if (vcc && vcc->pop != NULL) {
1945 dev_kfree_skb_irq(skb);
1949 if (++i == scq->num_entries)
1952 scq->tail = scq->base + pos;
1953 spin_unlock_irqrestore(&scq->lock, flags);
1956 static void process_rsq(ns_dev * card)
1960 if (!ns_rsqe_valid(card->rsq.next))
1963 dequeue_rx(card, card->rsq.next);
1964 ns_rsqe_init(card->rsq.next);
1965 previous = card->rsq.next;
1966 if (card->rsq.next == card->rsq.last)
1967 card->rsq.next = card->rsq.base;
1970 } while (ns_rsqe_valid(card->rsq.next));
1971 writel(PTR_DIFF(previous, card->rsq.base), card->membase + RSQH);
1974 static void dequeue_rx(ns_dev * card, ns_rsqe * rsqe)
1978 struct sk_buff *iovb;
1980 struct atm_vcc *vcc;
1981 struct sk_buff *skb;
1982 unsigned short aal5_len;
1987 stat = readl(card->membase + STAT);
1988 card->sbfqc = ns_stat_sfbqc_get(stat);
1989 card->lbfqc = ns_stat_lfbqc_get(stat);
1991 id = le32_to_cpu(rsqe->buffer_handle);
1992 skb = idr_remove(&card->idr, id);
1995 "nicstar%d: skb not found!\n", card->index);
1998 dma_sync_single_for_cpu(&card->pcidev->dev,
2000 (NS_PRV_BUFTYPE(skb) == BUF_SM
2001 ? NS_SMSKBSIZE : NS_LGSKBSIZE),
2003 dma_unmap_single(&card->pcidev->dev,
2005 (NS_PRV_BUFTYPE(skb) == BUF_SM
2006 ? NS_SMSKBSIZE : NS_LGSKBSIZE),
2008 vpi = ns_rsqe_vpi(rsqe);
2009 vci = ns_rsqe_vci(rsqe);
2010 if (vpi >= 1UL << card->vpibits || vci >= 1UL << card->vcibits) {
2011 printk("nicstar%d: SDU received for out-of-range vc %d.%d.\n",
2012 card->index, vpi, vci);
2013 recycle_rx_buf(card, skb);
2017 vc = &(card->vcmap[vpi << card->vcibits | vci]);
2019 RXPRINTK("nicstar%d: SDU received on non-rx vc %d.%d.\n",
2020 card->index, vpi, vci);
2021 recycle_rx_buf(card, skb);
2027 if (vcc->qos.aal == ATM_AAL0) {
2029 unsigned char *cell;
2033 for (i = ns_rsqe_cellcount(rsqe); i; i--) {
2034 sb = dev_alloc_skb(NS_SMSKBSIZE);
2037 ("nicstar%d: Can't allocate buffers for aal0.\n",
2039 atomic_add(i, &vcc->stats->rx_drop);
2042 if (!atm_charge(vcc, sb->truesize)) {
2044 ("nicstar%d: atm_charge() dropped aal0 packets.\n",
2046 atomic_add(i - 1, &vcc->stats->rx_drop); /* already increased by 1 */
2047 dev_kfree_skb_any(sb);
2050 /* Rebuild the header */
2051 *((u32 *) sb->data) = le32_to_cpu(rsqe->word_1) << 4 |
2052 (ns_rsqe_clp(rsqe) ? 0x00000001 : 0x00000000);
2053 if (i == 1 && ns_rsqe_eopdu(rsqe))
2054 *((u32 *) sb->data) |= 0x00000002;
2055 skb_put(sb, NS_AAL0_HEADER);
2056 memcpy(skb_tail_pointer(sb), cell, ATM_CELL_PAYLOAD);
2057 skb_put(sb, ATM_CELL_PAYLOAD);
2058 ATM_SKB(sb)->vcc = vcc;
2059 __net_timestamp(sb);
2061 atomic_inc(&vcc->stats->rx);
2062 cell += ATM_CELL_PAYLOAD;
2065 recycle_rx_buf(card, skb);
2069 /* To reach this point, the AAL layer can only be AAL5 */
2071 if ((iovb = vc->rx_iov) == NULL) {
2072 iovb = skb_dequeue(&(card->iovpool.queue));
2073 if (iovb == NULL) { /* No buffers in the queue */
2074 iovb = alloc_skb(NS_IOVBUFSIZE, GFP_ATOMIC);
2076 printk("nicstar%d: Out of iovec buffers.\n",
2078 atomic_inc(&vcc->stats->rx_drop);
2079 recycle_rx_buf(card, skb);
2082 NS_PRV_BUFTYPE(iovb) = BUF_NONE;
2083 } else if (--card->iovpool.count < card->iovnr.min) {
2084 struct sk_buff *new_iovb;
2086 alloc_skb(NS_IOVBUFSIZE, GFP_ATOMIC)) != NULL) {
2087 NS_PRV_BUFTYPE(iovb) = BUF_NONE;
2088 skb_queue_tail(&card->iovpool.queue, new_iovb);
2089 card->iovpool.count++;
2093 NS_PRV_IOVCNT(iovb) = 0;
2095 iovb->data = iovb->head;
2096 skb_reset_tail_pointer(iovb);
2097 /* IMPORTANT: a pointer to the sk_buff containing the small or large
2098 buffer is stored as iovec base, NOT a pointer to the
2099 small or large buffer itself. */
2100 } else if (NS_PRV_IOVCNT(iovb) >= NS_MAX_IOVECS) {
2101 printk("nicstar%d: received too big AAL5 SDU.\n", card->index);
2102 atomic_inc(&vcc->stats->rx_err);
2103 recycle_iovec_rx_bufs(card, (struct iovec *)iovb->data,
2105 NS_PRV_IOVCNT(iovb) = 0;
2107 iovb->data = iovb->head;
2108 skb_reset_tail_pointer(iovb);
2110 iov = &((struct iovec *)iovb->data)[NS_PRV_IOVCNT(iovb)++];
2111 iov->iov_base = (void *)skb;
2112 iov->iov_len = ns_rsqe_cellcount(rsqe) * 48;
2113 iovb->len += iov->iov_len;
2116 if (NS_PRV_IOVCNT(iovb) == 1) {
2117 if (NS_PRV_BUFTYPE(skb) != BUF_SM) {
2119 ("nicstar%d: Expected a small buffer, and this is not one.\n",
2121 which_list(card, skb);
2122 atomic_inc(&vcc->stats->rx_err);
2123 recycle_rx_buf(card, skb);
2125 recycle_iov_buf(card, iovb);
2128 } else { /* NS_PRV_IOVCNT(iovb) >= 2 */
2130 if (NS_PRV_BUFTYPE(skb) != BUF_LG) {
2132 ("nicstar%d: Expected a large buffer, and this is not one.\n",
2134 which_list(card, skb);
2135 atomic_inc(&vcc->stats->rx_err);
2136 recycle_iovec_rx_bufs(card, (struct iovec *)iovb->data,
2137 NS_PRV_IOVCNT(iovb));
2139 recycle_iov_buf(card, iovb);
2143 #endif /* EXTRA_DEBUG */
2145 if (ns_rsqe_eopdu(rsqe)) {
2146 /* This works correctly regardless of the endianness of the host */
2147 unsigned char *L1L2 = (unsigned char *)
2148 (skb->data + iov->iov_len - 6);
2149 aal5_len = L1L2[0] << 8 | L1L2[1];
2150 len = (aal5_len == 0x0000) ? 0x10000 : aal5_len;
2151 if (ns_rsqe_crcerr(rsqe) ||
2152 len + 8 > iovb->len || len + (47 + 8) < iovb->len) {
2153 printk("nicstar%d: AAL5 CRC error", card->index);
2154 if (len + 8 > iovb->len || len + (47 + 8) < iovb->len)
2155 printk(" - PDU size mismatch.\n");
2158 atomic_inc(&vcc->stats->rx_err);
2159 recycle_iovec_rx_bufs(card, (struct iovec *)iovb->data,
2160 NS_PRV_IOVCNT(iovb));
2162 recycle_iov_buf(card, iovb);
2166 /* By this point we (hopefully) have a complete SDU without errors. */
2168 if (NS_PRV_IOVCNT(iovb) == 1) { /* Just a small buffer */
2169 /* skb points to a small buffer */
2170 if (!atm_charge(vcc, skb->truesize)) {
2171 push_rxbufs(card, skb);
2172 atomic_inc(&vcc->stats->rx_drop);
2175 dequeue_sm_buf(card, skb);
2176 ATM_SKB(skb)->vcc = vcc;
2177 __net_timestamp(skb);
2178 vcc->push(vcc, skb);
2179 atomic_inc(&vcc->stats->rx);
2181 } else if (NS_PRV_IOVCNT(iovb) == 2) { /* One small plus one large buffer */
2184 sb = (struct sk_buff *)(iov - 1)->iov_base;
2185 /* skb points to a large buffer */
2187 if (len <= NS_SMBUFSIZE) {
2188 if (!atm_charge(vcc, sb->truesize)) {
2189 push_rxbufs(card, sb);
2190 atomic_inc(&vcc->stats->rx_drop);
2193 dequeue_sm_buf(card, sb);
2194 ATM_SKB(sb)->vcc = vcc;
2195 __net_timestamp(sb);
2197 atomic_inc(&vcc->stats->rx);
2200 push_rxbufs(card, skb);
2202 } else { /* len > NS_SMBUFSIZE, the usual case */
2204 if (!atm_charge(vcc, skb->truesize)) {
2205 push_rxbufs(card, skb);
2206 atomic_inc(&vcc->stats->rx_drop);
2208 dequeue_lg_buf(card, skb);
2209 skb_push(skb, NS_SMBUFSIZE);
2210 skb_copy_from_linear_data(sb, skb->data,
2212 skb_put(skb, len - NS_SMBUFSIZE);
2213 ATM_SKB(skb)->vcc = vcc;
2214 __net_timestamp(skb);
2215 vcc->push(vcc, skb);
2216 atomic_inc(&vcc->stats->rx);
2219 push_rxbufs(card, sb);
2223 } else { /* Must push a huge buffer */
2225 struct sk_buff *hb, *sb, *lb;
2226 int remaining, tocopy;
2229 hb = skb_dequeue(&(card->hbpool.queue));
2230 if (hb == NULL) { /* No buffers in the queue */
2232 hb = dev_alloc_skb(NS_HBUFSIZE);
2235 ("nicstar%d: Out of huge buffers.\n",
2237 atomic_inc(&vcc->stats->rx_drop);
2238 recycle_iovec_rx_bufs(card,
2241 NS_PRV_IOVCNT(iovb));
2243 recycle_iov_buf(card, iovb);
2245 } else if (card->hbpool.count < card->hbnr.min) {
2246 struct sk_buff *new_hb;
2248 dev_alloc_skb(NS_HBUFSIZE)) !=
2250 skb_queue_tail(&card->hbpool.
2252 card->hbpool.count++;
2255 NS_PRV_BUFTYPE(hb) = BUF_NONE;
2256 } else if (--card->hbpool.count < card->hbnr.min) {
2257 struct sk_buff *new_hb;
2259 dev_alloc_skb(NS_HBUFSIZE)) != NULL) {
2260 NS_PRV_BUFTYPE(new_hb) = BUF_NONE;
2261 skb_queue_tail(&card->hbpool.queue,
2263 card->hbpool.count++;
2265 if (card->hbpool.count < card->hbnr.min) {
2267 dev_alloc_skb(NS_HBUFSIZE)) !=
2269 NS_PRV_BUFTYPE(new_hb) =
2271 skb_queue_tail(&card->hbpool.
2273 card->hbpool.count++;
2278 iov = (struct iovec *)iovb->data;
2280 if (!atm_charge(vcc, hb->truesize)) {
2281 recycle_iovec_rx_bufs(card, iov,
2282 NS_PRV_IOVCNT(iovb));
2283 if (card->hbpool.count < card->hbnr.max) {
2284 skb_queue_tail(&card->hbpool.queue, hb);
2285 card->hbpool.count++;
2287 dev_kfree_skb_any(hb);
2288 atomic_inc(&vcc->stats->rx_drop);
2290 /* Copy the small buffer to the huge buffer */
2291 sb = (struct sk_buff *)iov->iov_base;
2292 skb_copy_from_linear_data(sb, hb->data,
2294 skb_put(hb, iov->iov_len);
2295 remaining = len - iov->iov_len;
2297 /* Free the small buffer */
2298 push_rxbufs(card, sb);
2300 /* Copy all large buffers to the huge buffer and free them */
2301 for (j = 1; j < NS_PRV_IOVCNT(iovb); j++) {
2302 lb = (struct sk_buff *)iov->iov_base;
2304 min_t(int, remaining, iov->iov_len);
2305 skb_copy_from_linear_data(lb,
2308 skb_put(hb, tocopy);
2310 remaining -= tocopy;
2311 push_rxbufs(card, lb);
2314 if (remaining != 0 || hb->len != len)
2316 ("nicstar%d: Huge buffer len mismatch.\n",
2318 #endif /* EXTRA_DEBUG */
2319 ATM_SKB(hb)->vcc = vcc;
2320 __net_timestamp(hb);
2322 atomic_inc(&vcc->stats->rx);
2327 recycle_iov_buf(card, iovb);
2332 static void recycle_rx_buf(ns_dev * card, struct sk_buff *skb)
2334 if (unlikely(NS_PRV_BUFTYPE(skb) == BUF_NONE)) {
2335 printk("nicstar%d: What kind of rx buffer is this?\n",
2337 dev_kfree_skb_any(skb);
2339 push_rxbufs(card, skb);
2342 static void recycle_iovec_rx_bufs(ns_dev * card, struct iovec *iov, int count)
2345 recycle_rx_buf(card, (struct sk_buff *)(iov++)->iov_base);
2348 static void recycle_iov_buf(ns_dev * card, struct sk_buff *iovb)
2350 if (card->iovpool.count < card->iovnr.max) {
2351 skb_queue_tail(&card->iovpool.queue, iovb);
2352 card->iovpool.count++;
2354 dev_kfree_skb_any(iovb);
2357 static void dequeue_sm_buf(ns_dev * card, struct sk_buff *sb)
2359 skb_unlink(sb, &card->sbpool.queue);
2360 if (card->sbfqc < card->sbnr.init) {
2361 struct sk_buff *new_sb;
2362 if ((new_sb = dev_alloc_skb(NS_SMSKBSIZE)) != NULL) {
2363 NS_PRV_BUFTYPE(new_sb) = BUF_SM;
2364 skb_queue_tail(&card->sbpool.queue, new_sb);
2365 skb_reserve(new_sb, NS_AAL0_HEADER);
2366 push_rxbufs(card, new_sb);
2369 if (card->sbfqc < card->sbnr.init)
2371 struct sk_buff *new_sb;
2372 if ((new_sb = dev_alloc_skb(NS_SMSKBSIZE)) != NULL) {
2373 NS_PRV_BUFTYPE(new_sb) = BUF_SM;
2374 skb_queue_tail(&card->sbpool.queue, new_sb);
2375 skb_reserve(new_sb, NS_AAL0_HEADER);
2376 push_rxbufs(card, new_sb);
2381 static void dequeue_lg_buf(ns_dev * card, struct sk_buff *lb)
2383 skb_unlink(lb, &card->lbpool.queue);
2384 if (card->lbfqc < card->lbnr.init) {
2385 struct sk_buff *new_lb;
2386 if ((new_lb = dev_alloc_skb(NS_LGSKBSIZE)) != NULL) {
2387 NS_PRV_BUFTYPE(new_lb) = BUF_LG;
2388 skb_queue_tail(&card->lbpool.queue, new_lb);
2389 skb_reserve(new_lb, NS_SMBUFSIZE);
2390 push_rxbufs(card, new_lb);
2393 if (card->lbfqc < card->lbnr.init)
2395 struct sk_buff *new_lb;
2396 if ((new_lb = dev_alloc_skb(NS_LGSKBSIZE)) != NULL) {
2397 NS_PRV_BUFTYPE(new_lb) = BUF_LG;
2398 skb_queue_tail(&card->lbpool.queue, new_lb);
2399 skb_reserve(new_lb, NS_SMBUFSIZE);
2400 push_rxbufs(card, new_lb);
2405 static int ns_proc_read(struct atm_dev *dev, loff_t * pos, char *page)
2412 card = (ns_dev *) dev->dev_data;
2413 stat = readl(card->membase + STAT);
2415 return sprintf(page, "Pool count min init max \n");
2417 return sprintf(page, "Small %5d %5d %5d %5d \n",
2418 ns_stat_sfbqc_get(stat), card->sbnr.min,
2419 card->sbnr.init, card->sbnr.max);
2421 return sprintf(page, "Large %5d %5d %5d %5d \n",
2422 ns_stat_lfbqc_get(stat), card->lbnr.min,
2423 card->lbnr.init, card->lbnr.max);
2425 return sprintf(page, "Huge %5d %5d %5d %5d \n",
2426 card->hbpool.count, card->hbnr.min,
2427 card->hbnr.init, card->hbnr.max);
2429 return sprintf(page, "Iovec %5d %5d %5d %5d \n",
2430 card->iovpool.count, card->iovnr.min,
2431 card->iovnr.init, card->iovnr.max);
2435 sprintf(page, "Interrupt counter: %u \n", card->intcnt);
2440 /* Dump 25.6 Mbps PHY registers */
2441 /* Now there's a 25.6 Mbps PHY driver this code isn't needed. I left it
2442 here just in case it's needed for debugging. */
2443 if (card->max_pcr == ATM_25_PCR && !left--) {
2447 for (i = 0; i < 4; i++) {
2448 while (CMD_BUSY(card)) ;
2449 writel(NS_CMD_READ_UTILITY | 0x00000200 | i,
2450 card->membase + CMD);
2451 while (CMD_BUSY(card)) ;
2452 phy_regs[i] = readl(card->membase + DR0) & 0x000000FF;
2455 return sprintf(page, "PHY regs: 0x%02X 0x%02X 0x%02X 0x%02X \n",
2456 phy_regs[0], phy_regs[1], phy_regs[2],
2459 #endif /* 0 - Dump 25.6 Mbps PHY registers */
2462 if (left-- < NS_TST_NUM_ENTRIES) {
2463 if (card->tste2vc[left + 1] == NULL)
2464 return sprintf(page, "%5d - VBR/UBR \n", left + 1);
2466 return sprintf(page, "%5d - %d %d \n", left + 1,
2467 card->tste2vc[left + 1]->tx_vcc->vpi,
2468 card->tste2vc[left + 1]->tx_vcc->vci);
2474 static int ns_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg)
2479 unsigned long flags;
2481 card = dev->dev_data;
2485 (pl.buftype, &((pool_levels __user *) arg)->buftype))
2487 switch (pl.buftype) {
2488 case NS_BUFTYPE_SMALL:
2490 ns_stat_sfbqc_get(readl(card->membase + STAT));
2491 pl.level.min = card->sbnr.min;
2492 pl.level.init = card->sbnr.init;
2493 pl.level.max = card->sbnr.max;
2496 case NS_BUFTYPE_LARGE:
2498 ns_stat_lfbqc_get(readl(card->membase + STAT));
2499 pl.level.min = card->lbnr.min;
2500 pl.level.init = card->lbnr.init;
2501 pl.level.max = card->lbnr.max;
2504 case NS_BUFTYPE_HUGE:
2505 pl.count = card->hbpool.count;
2506 pl.level.min = card->hbnr.min;
2507 pl.level.init = card->hbnr.init;
2508 pl.level.max = card->hbnr.max;
2511 case NS_BUFTYPE_IOVEC:
2512 pl.count = card->iovpool.count;
2513 pl.level.min = card->iovnr.min;
2514 pl.level.init = card->iovnr.init;
2515 pl.level.max = card->iovnr.max;
2519 return -ENOIOCTLCMD;
2522 if (!copy_to_user((pool_levels __user *) arg, &pl, sizeof(pl)))
2523 return (sizeof(pl));
2528 if (!capable(CAP_NET_ADMIN))
2530 if (copy_from_user(&pl, (pool_levels __user *) arg, sizeof(pl)))
2532 if (pl.level.min >= pl.level.init
2533 || pl.level.init >= pl.level.max)
2535 if (pl.level.min == 0)
2537 switch (pl.buftype) {
2538 case NS_BUFTYPE_SMALL:
2539 if (pl.level.max > TOP_SB)
2541 card->sbnr.min = pl.level.min;
2542 card->sbnr.init = pl.level.init;
2543 card->sbnr.max = pl.level.max;
2546 case NS_BUFTYPE_LARGE:
2547 if (pl.level.max > TOP_LB)
2549 card->lbnr.min = pl.level.min;
2550 card->lbnr.init = pl.level.init;
2551 card->lbnr.max = pl.level.max;
2554 case NS_BUFTYPE_HUGE:
2555 if (pl.level.max > TOP_HB)
2557 card->hbnr.min = pl.level.min;
2558 card->hbnr.init = pl.level.init;
2559 card->hbnr.max = pl.level.max;
2562 case NS_BUFTYPE_IOVEC:
2563 if (pl.level.max > TOP_IOVB)
2565 card->iovnr.min = pl.level.min;
2566 card->iovnr.init = pl.level.init;
2567 card->iovnr.max = pl.level.max;
2577 if (!capable(CAP_NET_ADMIN))
2579 btype = (long)arg; /* a long is the same size as a pointer or bigger */
2581 case NS_BUFTYPE_SMALL:
2582 while (card->sbfqc < card->sbnr.init) {
2585 sb = __dev_alloc_skb(NS_SMSKBSIZE, GFP_KERNEL);
2588 NS_PRV_BUFTYPE(sb) = BUF_SM;
2589 skb_queue_tail(&card->sbpool.queue, sb);
2590 skb_reserve(sb, NS_AAL0_HEADER);
2591 push_rxbufs(card, sb);
2595 case NS_BUFTYPE_LARGE:
2596 while (card->lbfqc < card->lbnr.init) {
2599 lb = __dev_alloc_skb(NS_LGSKBSIZE, GFP_KERNEL);
2602 NS_PRV_BUFTYPE(lb) = BUF_LG;
2603 skb_queue_tail(&card->lbpool.queue, lb);
2604 skb_reserve(lb, NS_SMBUFSIZE);
2605 push_rxbufs(card, lb);
2609 case NS_BUFTYPE_HUGE:
2610 while (card->hbpool.count > card->hbnr.init) {
2613 spin_lock_irqsave(&card->int_lock, flags);
2614 hb = skb_dequeue(&card->hbpool.queue);
2615 card->hbpool.count--;
2616 spin_unlock_irqrestore(&card->int_lock, flags);
2619 ("nicstar%d: huge buffer count inconsistent.\n",
2622 dev_kfree_skb_any(hb);
2625 while (card->hbpool.count < card->hbnr.init) {
2628 hb = __dev_alloc_skb(NS_HBUFSIZE, GFP_KERNEL);
2631 NS_PRV_BUFTYPE(hb) = BUF_NONE;
2632 spin_lock_irqsave(&card->int_lock, flags);
2633 skb_queue_tail(&card->hbpool.queue, hb);
2634 card->hbpool.count++;
2635 spin_unlock_irqrestore(&card->int_lock, flags);
2639 case NS_BUFTYPE_IOVEC:
2640 while (card->iovpool.count > card->iovnr.init) {
2641 struct sk_buff *iovb;
2643 spin_lock_irqsave(&card->int_lock, flags);
2644 iovb = skb_dequeue(&card->iovpool.queue);
2645 card->iovpool.count--;
2646 spin_unlock_irqrestore(&card->int_lock, flags);
2649 ("nicstar%d: iovec buffer count inconsistent.\n",
2652 dev_kfree_skb_any(iovb);
2655 while (card->iovpool.count < card->iovnr.init) {
2656 struct sk_buff *iovb;
2658 iovb = alloc_skb(NS_IOVBUFSIZE, GFP_KERNEL);
2661 NS_PRV_BUFTYPE(iovb) = BUF_NONE;
2662 spin_lock_irqsave(&card->int_lock, flags);
2663 skb_queue_tail(&card->iovpool.queue, iovb);
2664 card->iovpool.count++;
2665 spin_unlock_irqrestore(&card->int_lock, flags);
2676 if (dev->phy && dev->phy->ioctl) {
2677 return dev->phy->ioctl(dev, cmd, arg);
2679 printk("nicstar%d: %s == NULL \n", card->index,
2680 dev->phy ? "dev->phy->ioctl" : "dev->phy");
2681 return -ENOIOCTLCMD;
2687 static void which_list(ns_dev * card, struct sk_buff *skb)
2689 printk("skb buf_type: 0x%08x\n", NS_PRV_BUFTYPE(skb));
2691 #endif /* EXTRA_DEBUG */
2693 static void ns_poll(struct timer_list *unused)
2697 unsigned long flags;
2700 PRINTK("nicstar: Entering ns_poll().\n");
2701 for (i = 0; i < num_cards; i++) {
2703 if (!spin_trylock_irqsave(&card->int_lock, flags)) {
2704 /* Probably it isn't worth spinning */
2709 stat_r = readl(card->membase + STAT);
2710 if (stat_r & NS_STAT_TSIF)
2711 stat_w |= NS_STAT_TSIF;
2712 if (stat_r & NS_STAT_EOPDU)
2713 stat_w |= NS_STAT_EOPDU;
2718 writel(stat_w, card->membase + STAT);
2719 spin_unlock_irqrestore(&card->int_lock, flags);
2721 mod_timer(&ns_timer, jiffies + NS_POLL_PERIOD);
2722 PRINTK("nicstar: Leaving ns_poll().\n");
2725 static void ns_phy_put(struct atm_dev *dev, unsigned char value,
2729 unsigned long flags;
2731 card = dev->dev_data;
2732 spin_lock_irqsave(&card->res_lock, flags);
2733 while (CMD_BUSY(card)) ;
2734 writel((u32) value, card->membase + DR0);
2735 writel(NS_CMD_WRITE_UTILITY | 0x00000200 | (addr & 0x000000FF),
2736 card->membase + CMD);
2737 spin_unlock_irqrestore(&card->res_lock, flags);
2740 static unsigned char ns_phy_get(struct atm_dev *dev, unsigned long addr)
2743 unsigned long flags;
2746 card = dev->dev_data;
2747 spin_lock_irqsave(&card->res_lock, flags);
2748 while (CMD_BUSY(card)) ;
2749 writel(NS_CMD_READ_UTILITY | 0x00000200 | (addr & 0x000000FF),
2750 card->membase + CMD);
2751 while (CMD_BUSY(card)) ;
2752 data = readl(card->membase + DR0) & 0x000000FF;
2753 spin_unlock_irqrestore(&card->res_lock, flags);
2754 return (unsigned char)data;
2757 module_init(nicstar_init);
2758 module_exit(nicstar_cleanup);