1 /******************************************************************************
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
21 ******************************************************************************/
24 /**** IMPORTANT INFORMATION ***************************************************
26 * There are currently three types of spinlocks:
28 * 1 - Per card interrupt spinlock (to protect structures and such)
29 * 2 - Per SCQ scq spinlock
30 * 3 - Per card resource spinlock (to access registers, etc.)
32 * These must NEVER be grabbed in reverse order.
34 ******************************************************************************/
36 /* Header files ***************************************************************/
38 #include <linux/module.h>
39 #include <linux/kernel.h>
40 #include <linux/skbuff.h>
41 #include <linux/atmdev.h>
42 #include <linux/atm.h>
43 #include <linux/pci.h>
44 #include <linux/types.h>
45 #include <linux/string.h>
46 #include <linux/delay.h>
47 #include <linux/init.h>
48 #include <linux/sched.h>
49 #include <linux/timer.h>
50 #include <linux/interrupt.h>
51 #include <linux/bitops.h>
53 #include <asm/uaccess.h>
54 #include <asm/atomic.h>
56 #ifdef CONFIG_ATM_NICSTAR_USE_SUNI
58 #endif /* CONFIG_ATM_NICSTAR_USE_SUNI */
59 #ifdef CONFIG_ATM_NICSTAR_USE_IDT77105
61 #endif /* CONFIG_ATM_NICSTAR_USE_IDT77105 */
63 #if BITS_PER_LONG != 32
64 # error FIXME: this driver requires a 32-bit platform
67 /* Additional code ************************************************************/
69 #include "nicstarmac.c"
72 /* Configurable parameters ****************************************************/
80 #undef NS_USE_DESTRUCTORS /* For now keep this undefined unless you know
81 you're going to use only raw ATM */
84 /* Do not touch these *********************************************************/
87 #define TXPRINTK(args...) printk(args)
89 #define TXPRINTK(args...)
93 #define RXPRINTK(args...) printk(args)
95 #define RXPRINTK(args...)
99 #define PRINTK(args...) printk(args)
101 #define PRINTK(args...)
102 #endif /* GENERAL_DEBUG */
105 #define XPRINTK(args...) printk(args)
107 #define XPRINTK(args...)
108 #endif /* EXTRA_DEBUG */
111 /* Macros *********************************************************************/
113 #define CMD_BUSY(card) (readl((card)->membase + STAT) & NS_STAT_CMDBZ)
115 #define NS_DELAY mdelay(1)
117 #define ALIGN_BUS_ADDR(addr, alignment) \
118 ((((u32) (addr)) + (((u32) (alignment)) - 1)) & ~(((u32) (alignment)) - 1))
119 #define ALIGN_ADDRESS(addr, alignment) \
120 bus_to_virt(ALIGN_BUS_ADDR(virt_to_bus(addr), alignment))
125 #define ATM_SKB(s) (&(s)->atm)
129 /* Function declarations ******************************************************/
131 static u32 ns_read_sram(ns_dev *card, u32 sram_address);
132 static void ns_write_sram(ns_dev *card, u32 sram_address, u32 *value, int count);
133 static int __devinit ns_init_card(int i, struct pci_dev *pcidev);
134 static void __devinit ns_init_card_error(ns_dev *card, int error);
135 static scq_info *get_scq(int size, u32 scd);
136 static void free_scq(scq_info *scq, struct atm_vcc *vcc);
137 static void push_rxbufs(ns_dev *, struct sk_buff *);
138 static irqreturn_t ns_irq_handler(int irq, void *dev_id);
139 static int ns_open(struct atm_vcc *vcc);
140 static void ns_close(struct atm_vcc *vcc);
141 static void fill_tst(ns_dev *card, int n, vc_map *vc);
142 static int ns_send(struct atm_vcc *vcc, struct sk_buff *skb);
143 static int push_scqe(ns_dev *card, vc_map *vc, scq_info *scq, ns_scqe *tbd,
144 struct sk_buff *skb);
145 static void process_tsq(ns_dev *card);
146 static void drain_scq(ns_dev *card, scq_info *scq, int pos);
147 static void process_rsq(ns_dev *card);
148 static void dequeue_rx(ns_dev *card, ns_rsqe *rsqe);
149 #ifdef NS_USE_DESTRUCTORS
150 static void ns_sb_destructor(struct sk_buff *sb);
151 static void ns_lb_destructor(struct sk_buff *lb);
152 static void ns_hb_destructor(struct sk_buff *hb);
153 #endif /* NS_USE_DESTRUCTORS */
154 static void recycle_rx_buf(ns_dev *card, struct sk_buff *skb);
155 static void recycle_iovec_rx_bufs(ns_dev *card, struct iovec *iov, int count);
156 static void recycle_iov_buf(ns_dev *card, struct sk_buff *iovb);
157 static void dequeue_sm_buf(ns_dev *card, struct sk_buff *sb);
158 static void dequeue_lg_buf(ns_dev *card, struct sk_buff *lb);
159 static int ns_proc_read(struct atm_dev *dev, loff_t *pos, char *page);
160 static int ns_ioctl(struct atm_dev *dev, unsigned int cmd, void __user *arg);
161 static void which_list(ns_dev *card, struct sk_buff *skb);
162 static void ns_poll(unsigned long arg);
163 static int ns_parse_mac(char *mac, unsigned char *esi);
164 static short ns_h2i(char c);
165 static void ns_phy_put(struct atm_dev *dev, unsigned char value,
167 static unsigned char ns_phy_get(struct atm_dev *dev, unsigned long addr);
171 /* Global variables ***********************************************************/
173 static struct ns_dev *cards[NS_MAX_CARDS];
174 static unsigned num_cards;
175 static struct atmdev_ops atm_ops =
181 .phy_put = ns_phy_put,
182 .phy_get = ns_phy_get,
183 .proc_read = ns_proc_read,
184 .owner = THIS_MODULE,
186 static struct timer_list ns_timer;
187 static char *mac[NS_MAX_CARDS];
188 module_param_array(mac, charp, NULL, 0);
189 MODULE_LICENSE("GPL");
192 /* Functions*******************************************************************/
194 static int __devinit nicstar_init_one(struct pci_dev *pcidev,
195 const struct pci_device_id *ent)
197 static int index = -1;
203 error = ns_init_card(index, pcidev);
205 cards[index--] = NULL; /* don't increment index */
216 static void __devexit nicstar_remove_one(struct pci_dev *pcidev)
219 ns_dev *card = pci_get_drvdata(pcidev);
221 struct sk_buff *iovb;
227 if (cards[i] == NULL)
230 if (card->atmdev->phy && card->atmdev->phy->stop)
231 card->atmdev->phy->stop(card->atmdev);
233 /* Stop everything */
234 writel(0x00000000, card->membase + CFG);
236 /* De-register device */
237 atm_dev_deregister(card->atmdev);
239 /* Disable PCI device */
240 pci_disable_device(pcidev);
242 /* Free up resources */
244 PRINTK("nicstar%d: freeing %d huge buffers.\n", i, card->hbpool.count);
245 while ((hb = skb_dequeue(&card->hbpool.queue)) != NULL)
247 dev_kfree_skb_any(hb);
250 PRINTK("nicstar%d: %d huge buffers freed.\n", i, j);
252 PRINTK("nicstar%d: freeing %d iovec buffers.\n", i, card->iovpool.count);
253 while ((iovb = skb_dequeue(&card->iovpool.queue)) != NULL)
255 dev_kfree_skb_any(iovb);
258 PRINTK("nicstar%d: %d iovec buffers freed.\n", i, j);
259 while ((lb = skb_dequeue(&card->lbpool.queue)) != NULL)
260 dev_kfree_skb_any(lb);
261 while ((sb = skb_dequeue(&card->sbpool.queue)) != NULL)
262 dev_kfree_skb_any(sb);
263 free_scq(card->scq0, NULL);
264 for (j = 0; j < NS_FRSCD_NUM; j++)
266 if (card->scd2vc[j] != NULL)
267 free_scq(card->scd2vc[j]->scq, card->scd2vc[j]->tx_vcc);
269 kfree(card->rsq.org);
270 kfree(card->tsq.org);
271 free_irq(card->pcidev->irq, card);
272 iounmap(card->membase);
278 static struct pci_device_id nicstar_pci_tbl[] __devinitdata =
280 {PCI_VENDOR_ID_IDT, PCI_DEVICE_ID_IDT_IDT77201,
281 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
282 {0,} /* terminate list */
284 MODULE_DEVICE_TABLE(pci, nicstar_pci_tbl);
288 static struct pci_driver nicstar_driver = {
290 .id_table = nicstar_pci_tbl,
291 .probe = nicstar_init_one,
292 .remove = __devexit_p(nicstar_remove_one),
297 static int __init nicstar_init(void)
299 unsigned error = 0; /* Initialized to remove compile warning */
301 XPRINTK("nicstar: nicstar_init() called.\n");
303 error = pci_register_driver(&nicstar_driver);
305 TXPRINTK("nicstar: TX debug enabled.\n");
306 RXPRINTK("nicstar: RX debug enabled.\n");
307 PRINTK("nicstar: General debug enabled.\n");
309 printk("nicstar: using PHY loopback.\n");
310 #endif /* PHY_LOOPBACK */
311 XPRINTK("nicstar: nicstar_init() returned.\n");
314 init_timer(&ns_timer);
315 ns_timer.expires = jiffies + NS_POLL_PERIOD;
317 ns_timer.function = ns_poll;
318 add_timer(&ns_timer);
326 static void __exit nicstar_cleanup(void)
328 XPRINTK("nicstar: nicstar_cleanup() called.\n");
330 del_timer(&ns_timer);
332 pci_unregister_driver(&nicstar_driver);
334 XPRINTK("nicstar: nicstar_cleanup() returned.\n");
339 static u32 ns_read_sram(ns_dev *card, u32 sram_address)
344 sram_address &= 0x0007FFFC; /* address must be dword aligned */
345 sram_address |= 0x50000000; /* SRAM read command */
346 spin_lock_irqsave(&card->res_lock, flags);
347 while (CMD_BUSY(card));
348 writel(sram_address, card->membase + CMD);
349 while (CMD_BUSY(card));
350 data = readl(card->membase + DR0);
351 spin_unlock_irqrestore(&card->res_lock, flags);
357 static void ns_write_sram(ns_dev *card, u32 sram_address, u32 *value, int count)
361 count--; /* count range now is 0..3 instead of 1..4 */
363 c <<= 2; /* to use increments of 4 */
364 spin_lock_irqsave(&card->res_lock, flags);
365 while (CMD_BUSY(card));
366 for (i = 0; i <= c; i += 4)
367 writel(*(value++), card->membase + i);
368 /* Note: DR# registers are the first 4 dwords in nicstar's memspace,
369 so card->membase + DR0 == card->membase */
371 sram_address &= 0x0007FFFC;
372 sram_address |= (0x40000000 | count);
373 writel(sram_address, card->membase + CMD);
374 spin_unlock_irqrestore(&card->res_lock, flags);
378 static int __devinit ns_init_card(int i, struct pci_dev *pcidev)
381 struct ns_dev *card = NULL;
382 unsigned char pci_latency;
388 unsigned long membase;
392 if (pci_enable_device(pcidev))
394 printk("nicstar%d: can't enable PCI device\n", i);
396 ns_init_card_error(card, error);
400 if ((card = kmalloc(sizeof(ns_dev), GFP_KERNEL)) == NULL)
402 printk("nicstar%d: can't allocate memory for device structure.\n", i);
404 ns_init_card_error(card, error);
408 spin_lock_init(&card->int_lock);
409 spin_lock_init(&card->res_lock);
411 pci_set_drvdata(pcidev, card);
415 card->pcidev = pcidev;
416 membase = pci_resource_start(pcidev, 1);
417 card->membase = ioremap(membase, NS_IOREMAP_SIZE);
418 if (card->membase == 0)
420 printk("nicstar%d: can't ioremap() membase.\n",i);
422 ns_init_card_error(card, error);
425 PRINTK("nicstar%d: membase at 0x%x.\n", i, card->membase);
427 pci_set_master(pcidev);
429 if (pci_read_config_byte(pcidev, PCI_LATENCY_TIMER, &pci_latency) != 0)
431 printk("nicstar%d: can't read PCI latency timer.\n", i);
433 ns_init_card_error(card, error);
436 #ifdef NS_PCI_LATENCY
437 if (pci_latency < NS_PCI_LATENCY)
439 PRINTK("nicstar%d: setting PCI latency timer to %d.\n", i, NS_PCI_LATENCY);
440 for (j = 1; j < 4; j++)
442 if (pci_write_config_byte(pcidev, PCI_LATENCY_TIMER, NS_PCI_LATENCY) != 0)
447 printk("nicstar%d: can't set PCI latency timer to %d.\n", i, NS_PCI_LATENCY);
449 ns_init_card_error(card, error);
453 #endif /* NS_PCI_LATENCY */
455 /* Clear timer overflow */
456 data = readl(card->membase + STAT);
457 if (data & NS_STAT_TMROF)
458 writel(NS_STAT_TMROF, card->membase + STAT);
461 writel(NS_CFG_SWRST, card->membase + CFG);
463 writel(0x00000000, card->membase + CFG);
466 writel(0x00000008, card->membase + GP);
468 writel(0x00000001, card->membase + GP);
470 while (CMD_BUSY(card));
471 writel(NS_CMD_WRITE_UTILITY | 0x00000100, card->membase + CMD); /* Sync UTOPIA with SAR clock */
474 /* Detect PHY type */
475 while (CMD_BUSY(card));
476 writel(NS_CMD_READ_UTILITY | 0x00000200, card->membase + CMD);
477 while (CMD_BUSY(card));
478 data = readl(card->membase + DR0);
481 printk("nicstar%d: PHY seems to be 25 Mbps.\n", i);
482 card->max_pcr = ATM_25_PCR;
483 while(CMD_BUSY(card));
484 writel(0x00000008, card->membase + DR0);
485 writel(NS_CMD_WRITE_UTILITY | 0x00000200, card->membase + CMD);
486 /* Clear an eventual pending interrupt */
487 writel(NS_STAT_SFBQF, card->membase + STAT);
489 while(CMD_BUSY(card));
490 writel(0x00000022, card->membase + DR0);
491 writel(NS_CMD_WRITE_UTILITY | 0x00000202, card->membase + CMD);
492 #endif /* PHY_LOOPBACK */
496 printk("nicstar%d: PHY seems to be 155 Mbps.\n", i);
497 card->max_pcr = ATM_OC3_PCR;
499 while(CMD_BUSY(card));
500 writel(0x00000002, card->membase + DR0);
501 writel(NS_CMD_WRITE_UTILITY | 0x00000205, card->membase + CMD);
502 #endif /* PHY_LOOPBACK */
505 printk("nicstar%d: unknown PHY type (0x%08X).\n", i, data);
507 ns_init_card_error(card, error);
510 writel(0x00000000, card->membase + GP);
512 /* Determine SRAM size */
514 ns_write_sram(card, 0x1C003, &data, 1);
516 ns_write_sram(card, 0x14003, &data, 1);
517 if (ns_read_sram(card, 0x14003) == 0x89ABCDEF &&
518 ns_read_sram(card, 0x1C003) == 0x76543210)
519 card->sram_size = 128;
521 card->sram_size = 32;
522 PRINTK("nicstar%d: %dK x 32bit SRAM size.\n", i, card->sram_size);
524 card->rct_size = NS_MAX_RCTSIZE;
526 #if (NS_MAX_RCTSIZE == 4096)
527 if (card->sram_size == 128)
528 printk("nicstar%d: limiting maximum VCI. See NS_MAX_RCTSIZE in nicstar.h\n", i);
529 #elif (NS_MAX_RCTSIZE == 16384)
530 if (card->sram_size == 32)
532 printk("nicstar%d: wasting memory. See NS_MAX_RCTSIZE in nicstar.h\n", i);
533 card->rct_size = 4096;
536 #error NS_MAX_RCTSIZE must be either 4096 or 16384 in nicstar.c
539 card->vpibits = NS_VPIBITS;
540 if (card->rct_size == 4096)
541 card->vcibits = 12 - NS_VPIBITS;
542 else /* card->rct_size == 16384 */
543 card->vcibits = 14 - NS_VPIBITS;
545 /* Initialize the nicstar eeprom/eprom stuff, for the MAC addr */
547 nicstar_init_eprom(card->membase);
549 /* Set the VPI/VCI MSb mask to zero so we can receive OAM cells */
550 writel(0x00000000, card->membase + VPM);
553 card->tsq.org = kmalloc(NS_TSQSIZE + NS_TSQ_ALIGNMENT, GFP_KERNEL);
554 if (card->tsq.org == NULL)
556 printk("nicstar%d: can't allocate TSQ.\n", i);
558 ns_init_card_error(card, error);
561 card->tsq.base = (ns_tsi *) ALIGN_ADDRESS(card->tsq.org, NS_TSQ_ALIGNMENT);
562 card->tsq.next = card->tsq.base;
563 card->tsq.last = card->tsq.base + (NS_TSQ_NUM_ENTRIES - 1);
564 for (j = 0; j < NS_TSQ_NUM_ENTRIES; j++)
565 ns_tsi_init(card->tsq.base + j);
566 writel(0x00000000, card->membase + TSQH);
567 writel((u32) virt_to_bus(card->tsq.base), card->membase + TSQB);
568 PRINTK("nicstar%d: TSQ base at 0x%x 0x%x 0x%x.\n", i, (u32) card->tsq.base,
569 (u32) virt_to_bus(card->tsq.base), readl(card->membase + TSQB));
572 card->rsq.org = kmalloc(NS_RSQSIZE + NS_RSQ_ALIGNMENT, GFP_KERNEL);
573 if (card->rsq.org == NULL)
575 printk("nicstar%d: can't allocate RSQ.\n", i);
577 ns_init_card_error(card, error);
580 card->rsq.base = (ns_rsqe *) ALIGN_ADDRESS(card->rsq.org, NS_RSQ_ALIGNMENT);
581 card->rsq.next = card->rsq.base;
582 card->rsq.last = card->rsq.base + (NS_RSQ_NUM_ENTRIES - 1);
583 for (j = 0; j < NS_RSQ_NUM_ENTRIES; j++)
584 ns_rsqe_init(card->rsq.base + j);
585 writel(0x00000000, card->membase + RSQH);
586 writel((u32) virt_to_bus(card->rsq.base), card->membase + RSQB);
587 PRINTK("nicstar%d: RSQ base at 0x%x.\n", i, (u32) card->rsq.base);
589 /* Initialize SCQ0, the only VBR SCQ used */
592 card->scq0 = get_scq(VBR_SCQSIZE, NS_VRSCD0);
593 if (card->scq0 == NULL)
595 printk("nicstar%d: can't get SCQ0.\n", i);
597 ns_init_card_error(card, error);
600 u32d[0] = (u32) virt_to_bus(card->scq0->base);
601 u32d[1] = (u32) 0x00000000;
602 u32d[2] = (u32) 0xffffffff;
603 u32d[3] = (u32) 0x00000000;
604 ns_write_sram(card, NS_VRSCD0, u32d, 4);
605 ns_write_sram(card, NS_VRSCD1, u32d, 4); /* These last two won't be used */
606 ns_write_sram(card, NS_VRSCD2, u32d, 4); /* but are initialized, just in case... */
607 card->scq0->scd = NS_VRSCD0;
608 PRINTK("nicstar%d: VBR-SCQ0 base at 0x%x.\n", i, (u32) card->scq0->base);
610 /* Initialize TSTs */
611 card->tst_addr = NS_TST0;
612 card->tst_free_entries = NS_TST_NUM_ENTRIES;
613 data = NS_TST_OPCODE_VARIABLE;
614 for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
615 ns_write_sram(card, NS_TST0 + j, &data, 1);
616 data = ns_tste_make(NS_TST_OPCODE_END, NS_TST0);
617 ns_write_sram(card, NS_TST0 + NS_TST_NUM_ENTRIES, &data, 1);
618 for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
619 ns_write_sram(card, NS_TST1 + j, &data, 1);
620 data = ns_tste_make(NS_TST_OPCODE_END, NS_TST1);
621 ns_write_sram(card, NS_TST1 + NS_TST_NUM_ENTRIES, &data, 1);
622 for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
623 card->tste2vc[j] = NULL;
624 writel(NS_TST0 << 2, card->membase + TSTB);
627 /* Initialize RCT. AAL type is set on opening the VC. */
629 u32d[0] = NS_RCTE_RAWCELLINTEN;
631 u32d[0] = 0x00000000;
632 #endif /* RCQ_SUPPORT */
633 u32d[1] = 0x00000000;
634 u32d[2] = 0x00000000;
635 u32d[3] = 0xFFFFFFFF;
636 for (j = 0; j < card->rct_size; j++)
637 ns_write_sram(card, j * 4, u32d, 4);
639 memset(card->vcmap, 0, NS_MAX_RCTSIZE * sizeof(vc_map));
641 for (j = 0; j < NS_FRSCD_NUM; j++)
642 card->scd2vc[j] = NULL;
644 /* Initialize buffer levels */
645 card->sbnr.min = MIN_SB;
646 card->sbnr.init = NUM_SB;
647 card->sbnr.max = MAX_SB;
648 card->lbnr.min = MIN_LB;
649 card->lbnr.init = NUM_LB;
650 card->lbnr.max = MAX_LB;
651 card->iovnr.min = MIN_IOVB;
652 card->iovnr.init = NUM_IOVB;
653 card->iovnr.max = MAX_IOVB;
654 card->hbnr.min = MIN_HB;
655 card->hbnr.init = NUM_HB;
656 card->hbnr.max = MAX_HB;
658 card->sm_handle = 0x00000000;
659 card->sm_addr = 0x00000000;
660 card->lg_handle = 0x00000000;
661 card->lg_addr = 0x00000000;
663 card->efbie = 1; /* To prevent push_rxbufs from enabling the interrupt */
665 /* Pre-allocate some huge buffers */
666 skb_queue_head_init(&card->hbpool.queue);
667 card->hbpool.count = 0;
668 for (j = 0; j < NUM_HB; j++)
671 hb = __dev_alloc_skb(NS_HBUFSIZE, GFP_KERNEL);
674 printk("nicstar%d: can't allocate %dth of %d huge buffers.\n",
677 ns_init_card_error(card, error);
680 NS_SKB_CB(hb)->buf_type = BUF_NONE;
681 skb_queue_tail(&card->hbpool.queue, hb);
682 card->hbpool.count++;
686 /* Allocate large buffers */
687 skb_queue_head_init(&card->lbpool.queue);
688 card->lbpool.count = 0; /* Not used */
689 for (j = 0; j < NUM_LB; j++)
692 lb = __dev_alloc_skb(NS_LGSKBSIZE, GFP_KERNEL);
695 printk("nicstar%d: can't allocate %dth of %d large buffers.\n",
698 ns_init_card_error(card, error);
701 NS_SKB_CB(lb)->buf_type = BUF_LG;
702 skb_queue_tail(&card->lbpool.queue, lb);
703 skb_reserve(lb, NS_SMBUFSIZE);
704 push_rxbufs(card, lb);
705 /* Due to the implementation of push_rxbufs() this is 1, not 0 */
709 card->rawch = (u32) virt_to_bus(lb->data);
712 /* Test for strange behaviour which leads to crashes */
713 if ((bcount = ns_stat_lfbqc_get(readl(card->membase + STAT))) < card->lbnr.min)
715 printk("nicstar%d: Strange... Just allocated %d large buffers and lfbqc = %d.\n",
718 ns_init_card_error(card, error);
723 /* Allocate small buffers */
724 skb_queue_head_init(&card->sbpool.queue);
725 card->sbpool.count = 0; /* Not used */
726 for (j = 0; j < NUM_SB; j++)
729 sb = __dev_alloc_skb(NS_SMSKBSIZE, GFP_KERNEL);
732 printk("nicstar%d: can't allocate %dth of %d small buffers.\n",
735 ns_init_card_error(card, error);
738 NS_SKB_CB(sb)->buf_type = BUF_SM;
739 skb_queue_tail(&card->sbpool.queue, sb);
740 skb_reserve(sb, NS_AAL0_HEADER);
741 push_rxbufs(card, sb);
743 /* Test for strange behaviour which leads to crashes */
744 if ((bcount = ns_stat_sfbqc_get(readl(card->membase + STAT))) < card->sbnr.min)
746 printk("nicstar%d: Strange... Just allocated %d small buffers and sfbqc = %d.\n",
749 ns_init_card_error(card, error);
754 /* Allocate iovec buffers */
755 skb_queue_head_init(&card->iovpool.queue);
756 card->iovpool.count = 0;
757 for (j = 0; j < NUM_IOVB; j++)
759 struct sk_buff *iovb;
760 iovb = alloc_skb(NS_IOVBUFSIZE, GFP_KERNEL);
763 printk("nicstar%d: can't allocate %dth of %d iovec buffers.\n",
766 ns_init_card_error(card, error);
769 NS_SKB_CB(iovb)->buf_type = BUF_NONE;
770 skb_queue_tail(&card->iovpool.queue, iovb);
771 card->iovpool.count++;
774 /* Configure NICStAR */
775 if (card->rct_size == 4096)
776 ns_cfg_rctsize = NS_CFG_RCTSIZE_4096_ENTRIES;
777 else /* (card->rct_size == 16384) */
778 ns_cfg_rctsize = NS_CFG_RCTSIZE_16384_ENTRIES;
783 if (request_irq(pcidev->irq, &ns_irq_handler, IRQF_DISABLED | IRQF_SHARED, "nicstar", card) != 0)
785 printk("nicstar%d: can't allocate IRQ %d.\n", i, pcidev->irq);
787 ns_init_card_error(card, error);
791 /* Register device */
792 card->atmdev = atm_dev_register("nicstar", &atm_ops, -1, NULL);
793 if (card->atmdev == NULL)
795 printk("nicstar%d: can't register device.\n", i);
797 ns_init_card_error(card, error);
801 if (ns_parse_mac(mac[i], card->atmdev->esi)) {
802 nicstar_read_eprom(card->membase, NICSTAR_EPROM_MAC_ADDR_OFFSET,
803 card->atmdev->esi, 6);
804 if (memcmp(card->atmdev->esi, "\x00\x00\x00\x00\x00\x00", 6) == 0) {
805 nicstar_read_eprom(card->membase, NICSTAR_EPROM_MAC_ADDR_OFFSET_ALT,
806 card->atmdev->esi, 6);
810 printk("nicstar%d: MAC address %02X:%02X:%02X:%02X:%02X:%02X\n", i,
811 card->atmdev->esi[0], card->atmdev->esi[1], card->atmdev->esi[2],
812 card->atmdev->esi[3], card->atmdev->esi[4], card->atmdev->esi[5]);
814 card->atmdev->dev_data = card;
815 card->atmdev->ci_range.vpi_bits = card->vpibits;
816 card->atmdev->ci_range.vci_bits = card->vcibits;
817 card->atmdev->link_rate = card->max_pcr;
818 card->atmdev->phy = NULL;
820 #ifdef CONFIG_ATM_NICSTAR_USE_SUNI
821 if (card->max_pcr == ATM_OC3_PCR)
822 suni_init(card->atmdev);
823 #endif /* CONFIG_ATM_NICSTAR_USE_SUNI */
825 #ifdef CONFIG_ATM_NICSTAR_USE_IDT77105
826 if (card->max_pcr == ATM_25_PCR)
827 idt77105_init(card->atmdev);
828 #endif /* CONFIG_ATM_NICSTAR_USE_IDT77105 */
830 if (card->atmdev->phy && card->atmdev->phy->start)
831 card->atmdev->phy->start(card->atmdev);
833 writel(NS_CFG_RXPATH |
840 NS_CFG_RXINT_NODELAY |
841 NS_CFG_RAWIE | /* Only enabled if RCQ_SUPPORT */
845 NS_CFG_TSQFIE_OPT | /* Only enabled if ENABLE_TSQFIE */
847 card->membase + CFG);
856 static void __devinit ns_init_card_error(ns_dev *card, int error)
860 writel(0x00000000, card->membase + CFG);
864 struct sk_buff *iovb;
865 while ((iovb = skb_dequeue(&card->iovpool.queue)) != NULL)
866 dev_kfree_skb_any(iovb);
871 while ((sb = skb_dequeue(&card->sbpool.queue)) != NULL)
872 dev_kfree_skb_any(sb);
873 free_scq(card->scq0, NULL);
878 while ((lb = skb_dequeue(&card->lbpool.queue)) != NULL)
879 dev_kfree_skb_any(lb);
884 while ((hb = skb_dequeue(&card->hbpool.queue)) != NULL)
885 dev_kfree_skb_any(hb);
889 kfree(card->rsq.org);
893 kfree(card->tsq.org);
897 free_irq(card->pcidev->irq, card);
901 iounmap(card->membase);
905 pci_disable_device(card->pcidev);
912 static scq_info *get_scq(int size, u32 scd)
917 if (size != VBR_SCQSIZE && size != CBR_SCQSIZE)
920 scq = kmalloc(sizeof(scq_info), GFP_KERNEL);
923 scq->org = kmalloc(2 * size, GFP_KERNEL);
924 if (scq->org == NULL)
929 scq->skb = kmalloc(sizeof(struct sk_buff *) *
930 (size / NS_SCQE_SIZE), GFP_KERNEL);
931 if (scq->skb == NULL)
937 scq->num_entries = size / NS_SCQE_SIZE;
938 scq->base = (ns_scqe *) ALIGN_ADDRESS(scq->org, size);
939 scq->next = scq->base;
940 scq->last = scq->base + (scq->num_entries - 1);
941 scq->tail = scq->last;
943 scq->num_entries = size / NS_SCQE_SIZE;
945 init_waitqueue_head(&scq->scqfull_waitq);
947 spin_lock_init(&scq->lock);
949 for (i = 0; i < scq->num_entries; i++)
957 /* For variable rate SCQ vcc must be NULL */
958 static void free_scq(scq_info *scq, struct atm_vcc *vcc)
962 if (scq->num_entries == VBR_SCQ_NUM_ENTRIES)
963 for (i = 0; i < scq->num_entries; i++)
965 if (scq->skb[i] != NULL)
967 vcc = ATM_SKB(scq->skb[i])->vcc;
968 if (vcc->pop != NULL)
969 vcc->pop(vcc, scq->skb[i]);
971 dev_kfree_skb_any(scq->skb[i]);
974 else /* vcc must be != NULL */
978 printk("nicstar: free_scq() called with vcc == NULL for fixed rate scq.");
979 for (i = 0; i < scq->num_entries; i++)
980 dev_kfree_skb_any(scq->skb[i]);
983 for (i = 0; i < scq->num_entries; i++)
985 if (scq->skb[i] != NULL)
987 if (vcc->pop != NULL)
988 vcc->pop(vcc, scq->skb[i]);
990 dev_kfree_skb_any(scq->skb[i]);
1001 /* The handles passed must be pointers to the sk_buff containing the small
1002 or large buffer(s) cast to u32. */
1003 static void push_rxbufs(ns_dev *card, struct sk_buff *skb)
1005 struct ns_skb_cb *cb = NS_SKB_CB(skb);
1009 unsigned long flags;
1012 handle2 = addr2 = 0;
1014 addr1 = (u32)virt_to_bus(skb->data);
1016 #ifdef GENERAL_DEBUG
1018 printk("nicstar%d: push_rxbufs called with addr1 = 0.\n", card->index);
1019 #endif /* GENERAL_DEBUG */
1021 stat = readl(card->membase + STAT);
1022 card->sbfqc = ns_stat_sfbqc_get(stat);
1023 card->lbfqc = ns_stat_lfbqc_get(stat);
1024 if (cb->buf_type == BUF_SM)
1030 addr2 = card->sm_addr;
1031 handle2 = card->sm_handle;
1032 card->sm_addr = 0x00000000;
1033 card->sm_handle = 0x00000000;
1035 else /* (!sm_addr) */
1037 card->sm_addr = addr1;
1038 card->sm_handle = handle1;
1042 else /* buf_type == BUF_LG */
1048 addr2 = card->lg_addr;
1049 handle2 = card->lg_handle;
1050 card->lg_addr = 0x00000000;
1051 card->lg_handle = 0x00000000;
1053 else /* (!lg_addr) */
1055 card->lg_addr = addr1;
1056 card->lg_handle = handle1;
1063 if (cb->buf_type == BUF_SM)
1065 if (card->sbfqc >= card->sbnr.max)
1067 skb_unlink((struct sk_buff *) handle1, &card->sbpool.queue);
1068 dev_kfree_skb_any((struct sk_buff *) handle1);
1069 skb_unlink((struct sk_buff *) handle2, &card->sbpool.queue);
1070 dev_kfree_skb_any((struct sk_buff *) handle2);
1076 else /* (buf_type == BUF_LG) */
1078 if (card->lbfqc >= card->lbnr.max)
1080 skb_unlink((struct sk_buff *) handle1, &card->lbpool.queue);
1081 dev_kfree_skb_any((struct sk_buff *) handle1);
1082 skb_unlink((struct sk_buff *) handle2, &card->lbpool.queue);
1083 dev_kfree_skb_any((struct sk_buff *) handle2);
1090 spin_lock_irqsave(&card->res_lock, flags);
1092 while (CMD_BUSY(card));
1093 writel(addr2, card->membase + DR3);
1094 writel(handle2, card->membase + DR2);
1095 writel(addr1, card->membase + DR1);
1096 writel(handle1, card->membase + DR0);
1097 writel(NS_CMD_WRITE_FREEBUFQ | cb->buf_type, card->membase + CMD);
1099 spin_unlock_irqrestore(&card->res_lock, flags);
1101 XPRINTK("nicstar%d: Pushing %s buffers at 0x%x and 0x%x.\n", card->index,
1102 (cb->buf_type == BUF_SM ? "small" : "large"), addr1, addr2);
1105 if (!card->efbie && card->sbfqc >= card->sbnr.min &&
1106 card->lbfqc >= card->lbnr.min)
1109 writel((readl(card->membase + CFG) | NS_CFG_EFBIE), card->membase + CFG);
1117 static irqreturn_t ns_irq_handler(int irq, void *dev_id)
1121 struct atm_dev *dev;
1122 unsigned long flags;
1124 card = (ns_dev *) dev_id;
1128 PRINTK("nicstar%d: NICStAR generated an interrupt\n", card->index);
1130 spin_lock_irqsave(&card->int_lock, flags);
1132 stat_r = readl(card->membase + STAT);
1134 /* Transmit Status Indicator has been written to T. S. Queue */
1135 if (stat_r & NS_STAT_TSIF)
1137 TXPRINTK("nicstar%d: TSI interrupt\n", card->index);
1139 writel(NS_STAT_TSIF, card->membase + STAT);
1142 /* Incomplete CS-PDU has been transmitted */
1143 if (stat_r & NS_STAT_TXICP)
1145 writel(NS_STAT_TXICP, card->membase + STAT);
1146 TXPRINTK("nicstar%d: Incomplete CS-PDU transmitted.\n",
1150 /* Transmit Status Queue 7/8 full */
1151 if (stat_r & NS_STAT_TSQF)
1153 writel(NS_STAT_TSQF, card->membase + STAT);
1154 PRINTK("nicstar%d: TSQ full.\n", card->index);
1158 /* Timer overflow */
1159 if (stat_r & NS_STAT_TMROF)
1161 writel(NS_STAT_TMROF, card->membase + STAT);
1162 PRINTK("nicstar%d: Timer overflow.\n", card->index);
1165 /* PHY device interrupt signal active */
1166 if (stat_r & NS_STAT_PHYI)
1168 writel(NS_STAT_PHYI, card->membase + STAT);
1169 PRINTK("nicstar%d: PHY interrupt.\n", card->index);
1170 if (dev->phy && dev->phy->interrupt) {
1171 dev->phy->interrupt(dev);
1175 /* Small Buffer Queue is full */
1176 if (stat_r & NS_STAT_SFBQF)
1178 writel(NS_STAT_SFBQF, card->membase + STAT);
1179 printk("nicstar%d: Small free buffer queue is full.\n", card->index);
1182 /* Large Buffer Queue is full */
1183 if (stat_r & NS_STAT_LFBQF)
1185 writel(NS_STAT_LFBQF, card->membase + STAT);
1186 printk("nicstar%d: Large free buffer queue is full.\n", card->index);
1189 /* Receive Status Queue is full */
1190 if (stat_r & NS_STAT_RSQF)
1192 writel(NS_STAT_RSQF, card->membase + STAT);
1193 printk("nicstar%d: RSQ full.\n", card->index);
1197 /* Complete CS-PDU received */
1198 if (stat_r & NS_STAT_EOPDU)
1200 RXPRINTK("nicstar%d: End of CS-PDU received.\n", card->index);
1202 writel(NS_STAT_EOPDU, card->membase + STAT);
1205 /* Raw cell received */
1206 if (stat_r & NS_STAT_RAWCF)
1208 writel(NS_STAT_RAWCF, card->membase + STAT);
1210 printk("nicstar%d: Raw cell received and no support yet...\n",
1212 #endif /* RCQ_SUPPORT */
1213 /* NOTE: the following procedure may keep a raw cell pending until the
1214 next interrupt. As this preliminary support is only meant to
1215 avoid buffer leakage, this is not an issue. */
1216 while (readl(card->membase + RAWCT) != card->rawch)
1220 rawcell = (ns_rcqe *) bus_to_virt(card->rawch);
1221 if (ns_rcqe_islast(rawcell))
1223 struct sk_buff *oldbuf;
1225 oldbuf = card->rcbuf;
1226 card->rcbuf = (struct sk_buff *) ns_rcqe_nextbufhandle(rawcell);
1227 card->rawch = (u32) virt_to_bus(card->rcbuf->data);
1228 recycle_rx_buf(card, oldbuf);
1231 card->rawch += NS_RCQE_SIZE;
1235 /* Small buffer queue is empty */
1236 if (stat_r & NS_STAT_SFBQE)
1241 writel(NS_STAT_SFBQE, card->membase + STAT);
1242 printk("nicstar%d: Small free buffer queue empty.\n",
1244 for (i = 0; i < card->sbnr.min; i++)
1246 sb = dev_alloc_skb(NS_SMSKBSIZE);
1249 writel(readl(card->membase + CFG) & ~NS_CFG_EFBIE, card->membase + CFG);
1253 NS_SKB_CB(sb)->buf_type = BUF_SM;
1254 skb_queue_tail(&card->sbpool.queue, sb);
1255 skb_reserve(sb, NS_AAL0_HEADER);
1256 push_rxbufs(card, sb);
1262 /* Large buffer queue empty */
1263 if (stat_r & NS_STAT_LFBQE)
1268 writel(NS_STAT_LFBQE, card->membase + STAT);
1269 printk("nicstar%d: Large free buffer queue empty.\n",
1271 for (i = 0; i < card->lbnr.min; i++)
1273 lb = dev_alloc_skb(NS_LGSKBSIZE);
1276 writel(readl(card->membase + CFG) & ~NS_CFG_EFBIE, card->membase + CFG);
1280 NS_SKB_CB(lb)->buf_type = BUF_LG;
1281 skb_queue_tail(&card->lbpool.queue, lb);
1282 skb_reserve(lb, NS_SMBUFSIZE);
1283 push_rxbufs(card, lb);
1289 /* Receive Status Queue is 7/8 full */
1290 if (stat_r & NS_STAT_RSQAF)
1292 writel(NS_STAT_RSQAF, card->membase + STAT);
1293 RXPRINTK("nicstar%d: RSQ almost full.\n", card->index);
1297 spin_unlock_irqrestore(&card->int_lock, flags);
1298 PRINTK("nicstar%d: end of interrupt service\n", card->index);
1304 static int ns_open(struct atm_vcc *vcc)
1308 unsigned long tmpl, modl;
1309 int tcr, tcra; /* target cell rate, and absolute value */
1310 int n = 0; /* Number of entries in the TST. Initialized to remove
1311 the compiler warning. */
1313 int frscdi = 0; /* Index of the SCD. Initialized to remove the compiler
1314 warning. How I wish compilers were clever enough to
1315 tell which variables can truly be used
1317 int inuse; /* tx or rx vc already in use by another vcc */
1318 short vpi = vcc->vpi;
1321 card = (ns_dev *) vcc->dev->dev_data;
1322 PRINTK("nicstar%d: opening vpi.vci %d.%d \n", card->index, (int) vpi, vci);
1323 if (vcc->qos.aal != ATM_AAL5 && vcc->qos.aal != ATM_AAL0)
1325 PRINTK("nicstar%d: unsupported AAL.\n", card->index);
1329 vc = &(card->vcmap[vpi << card->vcibits | vci]);
1333 if (vcc->qos.txtp.traffic_class != ATM_NONE && vc->tx)
1335 if (vcc->qos.rxtp.traffic_class != ATM_NONE && vc->rx)
1339 printk("nicstar%d: %s vci already in use.\n", card->index,
1340 inuse == 1 ? "tx" : inuse == 2 ? "rx" : "tx and rx");
1344 set_bit(ATM_VF_ADDR,&vcc->flags);
1346 /* NOTE: You are not allowed to modify an open connection's QOS. To change
1347 that, remove the ATM_VF_PARTIAL flag checking. There may be other changes
1348 needed to do that. */
1349 if (!test_bit(ATM_VF_PARTIAL,&vcc->flags))
1353 set_bit(ATM_VF_PARTIAL,&vcc->flags);
1354 if (vcc->qos.txtp.traffic_class == ATM_CBR)
1356 /* Check requested cell rate and availability of SCD */
1357 if (vcc->qos.txtp.max_pcr == 0 && vcc->qos.txtp.pcr == 0 &&
1358 vcc->qos.txtp.min_pcr == 0)
1360 PRINTK("nicstar%d: trying to open a CBR vc with cell rate = 0 \n",
1362 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1363 clear_bit(ATM_VF_ADDR,&vcc->flags);
1367 tcr = atm_pcr_goal(&(vcc->qos.txtp));
1368 tcra = tcr >= 0 ? tcr : -tcr;
1370 PRINTK("nicstar%d: target cell rate = %d.\n", card->index,
1371 vcc->qos.txtp.max_pcr);
1373 tmpl = (unsigned long)tcra * (unsigned long)NS_TST_NUM_ENTRIES;
1374 modl = tmpl % card->max_pcr;
1376 n = (int)(tmpl / card->max_pcr);
1383 if ((n = (card->tst_free_entries - NS_TST_RESERVED)) <= 0)
1385 PRINTK("nicstar%d: no CBR bandwidth free.\n", card->index);
1386 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1387 clear_bit(ATM_VF_ADDR,&vcc->flags);
1394 printk("nicstar%d: selected bandwidth < granularity.\n", card->index);
1395 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1396 clear_bit(ATM_VF_ADDR,&vcc->flags);
1400 if (n > (card->tst_free_entries - NS_TST_RESERVED))
1402 PRINTK("nicstar%d: not enough free CBR bandwidth.\n", card->index);
1403 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1404 clear_bit(ATM_VF_ADDR,&vcc->flags);
1408 card->tst_free_entries -= n;
1410 XPRINTK("nicstar%d: writing %d tst entries.\n", card->index, n);
1411 for (frscdi = 0; frscdi < NS_FRSCD_NUM; frscdi++)
1413 if (card->scd2vc[frscdi] == NULL)
1415 card->scd2vc[frscdi] = vc;
1419 if (frscdi == NS_FRSCD_NUM)
1421 PRINTK("nicstar%d: no SCD available for CBR channel.\n", card->index);
1422 card->tst_free_entries += n;
1423 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1424 clear_bit(ATM_VF_ADDR,&vcc->flags);
1428 vc->cbr_scd = NS_FRSCD + frscdi * NS_FRSCD_SIZE;
1430 scq = get_scq(CBR_SCQSIZE, vc->cbr_scd);
1433 PRINTK("nicstar%d: can't get fixed rate SCQ.\n", card->index);
1434 card->scd2vc[frscdi] = NULL;
1435 card->tst_free_entries += n;
1436 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1437 clear_bit(ATM_VF_ADDR,&vcc->flags);
1441 u32d[0] = (u32) virt_to_bus(scq->base);
1442 u32d[1] = (u32) 0x00000000;
1443 u32d[2] = (u32) 0xffffffff;
1444 u32d[3] = (u32) 0x00000000;
1445 ns_write_sram(card, vc->cbr_scd, u32d, 4);
1447 fill_tst(card, n, vc);
1449 else if (vcc->qos.txtp.traffic_class == ATM_UBR)
1451 vc->cbr_scd = 0x00000000;
1452 vc->scq = card->scq0;
1455 if (vcc->qos.txtp.traffic_class != ATM_NONE)
1461 if (vcc->qos.rxtp.traffic_class != ATM_NONE)
1469 /* Open the connection in hardware */
1470 if (vcc->qos.aal == ATM_AAL5)
1471 status = NS_RCTE_AAL5 | NS_RCTE_CONNECTOPEN;
1472 else /* vcc->qos.aal == ATM_AAL0 */
1473 status = NS_RCTE_AAL0 | NS_RCTE_CONNECTOPEN;
1475 status |= NS_RCTE_RAWCELLINTEN;
1476 #endif /* RCQ_SUPPORT */
1477 ns_write_sram(card, NS_RCT + (vpi << card->vcibits | vci) *
1478 NS_RCT_ENTRY_SIZE, &status, 1);
1483 set_bit(ATM_VF_READY,&vcc->flags);
1489 static void ns_close(struct atm_vcc *vcc)
1497 card = vcc->dev->dev_data;
1498 PRINTK("nicstar%d: closing vpi.vci %d.%d \n", card->index,
1499 (int) vcc->vpi, vcc->vci);
1501 clear_bit(ATM_VF_READY,&vcc->flags);
1503 if (vcc->qos.rxtp.traffic_class != ATM_NONE)
1506 unsigned long flags;
1508 addr = NS_RCT + (vcc->vpi << card->vcibits | vcc->vci) * NS_RCT_ENTRY_SIZE;
1509 spin_lock_irqsave(&card->res_lock, flags);
1510 while(CMD_BUSY(card));
1511 writel(NS_CMD_CLOSE_CONNECTION | addr << 2, card->membase + CMD);
1512 spin_unlock_irqrestore(&card->res_lock, flags);
1515 if (vc->rx_iov != NULL)
1517 struct sk_buff *iovb;
1520 stat = readl(card->membase + STAT);
1521 card->sbfqc = ns_stat_sfbqc_get(stat);
1522 card->lbfqc = ns_stat_lfbqc_get(stat);
1524 PRINTK("nicstar%d: closing a VC with pending rx buffers.\n",
1527 recycle_iovec_rx_bufs(card, (struct iovec *) iovb->data,
1528 NS_SKB(iovb)->iovcnt);
1529 NS_SKB(iovb)->iovcnt = 0;
1530 NS_SKB(iovb)->vcc = NULL;
1531 spin_lock_irqsave(&card->int_lock, flags);
1532 recycle_iov_buf(card, iovb);
1533 spin_unlock_irqrestore(&card->int_lock, flags);
1538 if (vcc->qos.txtp.traffic_class != ATM_NONE)
1543 if (vcc->qos.txtp.traffic_class == ATM_CBR)
1545 unsigned long flags;
1553 spin_lock_irqsave(&scq->lock, flags);
1555 if (scqep == scq->base)
1559 if (scqep == scq->tail)
1561 spin_unlock_irqrestore(&scq->lock, flags);
1564 /* If the last entry is not a TSR, place one in the SCQ in order to
1565 be able to completely drain it and then close. */
1566 if (!ns_scqe_is_tsr(scqep) && scq->tail != scq->next)
1573 tsr.word_1 = ns_tsr_mkword_1(NS_TSR_INTENABLE);
1574 scdi = (vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE;
1575 scqi = scq->next - scq->base;
1576 tsr.word_2 = ns_tsr_mkword_2(scdi, scqi);
1577 tsr.word_3 = 0x00000000;
1578 tsr.word_4 = 0x00000000;
1581 scq->skb[index] = NULL;
1582 if (scq->next == scq->last)
1583 scq->next = scq->base;
1586 data = (u32) virt_to_bus(scq->next);
1587 ns_write_sram(card, scq->scd, &data, 1);
1589 spin_unlock_irqrestore(&scq->lock, flags);
1593 /* Free all TST entries */
1594 data = NS_TST_OPCODE_VARIABLE;
1595 for (i = 0; i < NS_TST_NUM_ENTRIES; i++)
1597 if (card->tste2vc[i] == vc)
1599 ns_write_sram(card, card->tst_addr + i, &data, 1);
1600 card->tste2vc[i] = NULL;
1601 card->tst_free_entries++;
1605 card->scd2vc[(vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE] = NULL;
1606 free_scq(vc->scq, vcc);
1609 /* remove all references to vcc before deleting it */
1610 if (vcc->qos.txtp.traffic_class != ATM_NONE)
1612 unsigned long flags;
1613 scq_info *scq = card->scq0;
1615 spin_lock_irqsave(&scq->lock, flags);
1617 for(i = 0; i < scq->num_entries; i++) {
1618 if(scq->skb[i] && ATM_SKB(scq->skb[i])->vcc == vcc) {
1619 ATM_SKB(scq->skb[i])->vcc = NULL;
1620 atm_return(vcc, scq->skb[i]->truesize);
1621 PRINTK("nicstar: deleted pending vcc mapping\n");
1625 spin_unlock_irqrestore(&scq->lock, flags);
1628 vcc->dev_data = NULL;
1629 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1630 clear_bit(ATM_VF_ADDR,&vcc->flags);
1635 stat = readl(card->membase + STAT);
1636 cfg = readl(card->membase + CFG);
1637 printk("STAT = 0x%08X CFG = 0x%08X \n", stat, cfg);
1638 printk("TSQ: base = 0x%08X next = 0x%08X last = 0x%08X TSQT = 0x%08X \n",
1639 (u32) card->tsq.base, (u32) card->tsq.next,(u32) card->tsq.last,
1640 readl(card->membase + TSQT));
1641 printk("RSQ: base = 0x%08X next = 0x%08X last = 0x%08X RSQT = 0x%08X \n",
1642 (u32) card->rsq.base, (u32) card->rsq.next,(u32) card->rsq.last,
1643 readl(card->membase + RSQT));
1644 printk("Empty free buffer queue interrupt %s \n",
1645 card->efbie ? "enabled" : "disabled");
1646 printk("SBCNT = %d count = %d LBCNT = %d count = %d \n",
1647 ns_stat_sfbqc_get(stat), card->sbpool.count,
1648 ns_stat_lfbqc_get(stat), card->lbpool.count);
1649 printk("hbpool.count = %d iovpool.count = %d \n",
1650 card->hbpool.count, card->iovpool.count);
1652 #endif /* RX_DEBUG */
1657 static void fill_tst(ns_dev *card, int n, vc_map *vc)
1664 /* It would be very complicated to keep the two TSTs synchronized while
1665 assuring that writes are only made to the inactive TST. So, for now I
1666 will use only one TST. If problems occur, I will change this again */
1668 new_tst = card->tst_addr;
1670 /* Fill procedure */
1672 for (e = 0; e < NS_TST_NUM_ENTRIES; e++)
1674 if (card->tste2vc[e] == NULL)
1677 if (e == NS_TST_NUM_ENTRIES) {
1678 printk("nicstar%d: No free TST entries found. \n", card->index);
1683 cl = NS_TST_NUM_ENTRIES;
1684 data = ns_tste_make(NS_TST_OPCODE_FIXED, vc->cbr_scd);
1688 if (cl >= NS_TST_NUM_ENTRIES && card->tste2vc[e] == NULL)
1690 card->tste2vc[e] = vc;
1691 ns_write_sram(card, new_tst + e, &data, 1);
1692 cl -= NS_TST_NUM_ENTRIES;
1696 if (++e == NS_TST_NUM_ENTRIES) {
1702 /* End of fill procedure */
1704 data = ns_tste_make(NS_TST_OPCODE_END, new_tst);
1705 ns_write_sram(card, new_tst + NS_TST_NUM_ENTRIES, &data, 1);
1706 ns_write_sram(card, card->tst_addr + NS_TST_NUM_ENTRIES, &data, 1);
1707 card->tst_addr = new_tst;
1712 static int ns_send(struct atm_vcc *vcc, struct sk_buff *skb)
1717 unsigned long buflen;
1719 u32 flags; /* TBD flags, not CPU flags */
1721 card = vcc->dev->dev_data;
1722 TXPRINTK("nicstar%d: ns_send() called.\n", card->index);
1723 if ((vc = (vc_map *) vcc->dev_data) == NULL)
1725 printk("nicstar%d: vcc->dev_data == NULL on ns_send().\n", card->index);
1726 atomic_inc(&vcc->stats->tx_err);
1727 dev_kfree_skb_any(skb);
1733 printk("nicstar%d: Trying to transmit on a non-tx VC.\n", card->index);
1734 atomic_inc(&vcc->stats->tx_err);
1735 dev_kfree_skb_any(skb);
1739 if (vcc->qos.aal != ATM_AAL5 && vcc->qos.aal != ATM_AAL0)
1741 printk("nicstar%d: Only AAL0 and AAL5 are supported.\n", card->index);
1742 atomic_inc(&vcc->stats->tx_err);
1743 dev_kfree_skb_any(skb);
1747 if (skb_shinfo(skb)->nr_frags != 0)
1749 printk("nicstar%d: No scatter-gather yet.\n", card->index);
1750 atomic_inc(&vcc->stats->tx_err);
1751 dev_kfree_skb_any(skb);
1755 ATM_SKB(skb)->vcc = vcc;
1757 if (vcc->qos.aal == ATM_AAL5)
1759 buflen = (skb->len + 47 + 8) / 48 * 48; /* Multiple of 48 */
1760 flags = NS_TBD_AAL5;
1761 scqe.word_2 = cpu_to_le32((u32) virt_to_bus(skb->data));
1762 scqe.word_3 = cpu_to_le32((u32) skb->len);
1763 scqe.word_4 = ns_tbd_mkword_4(0, (u32) vcc->vpi, (u32) vcc->vci, 0,
1764 ATM_SKB(skb)->atm_options & ATM_ATMOPT_CLP ? 1 : 0);
1765 flags |= NS_TBD_EOPDU;
1767 else /* (vcc->qos.aal == ATM_AAL0) */
1769 buflen = ATM_CELL_PAYLOAD; /* i.e., 48 bytes */
1770 flags = NS_TBD_AAL0;
1771 scqe.word_2 = cpu_to_le32((u32) virt_to_bus(skb->data) + NS_AAL0_HEADER);
1772 scqe.word_3 = cpu_to_le32(0x00000000);
1773 if (*skb->data & 0x02) /* Payload type 1 - end of pdu */
1774 flags |= NS_TBD_EOPDU;
1775 scqe.word_4 = cpu_to_le32(*((u32 *) skb->data) & ~NS_TBD_VC_MASK);
1776 /* Force the VPI/VCI to be the same as in VCC struct */
1777 scqe.word_4 |= cpu_to_le32((((u32) vcc->vpi) << NS_TBD_VPI_SHIFT |
1778 ((u32) vcc->vci) << NS_TBD_VCI_SHIFT) &
1782 if (vcc->qos.txtp.traffic_class == ATM_CBR)
1784 scqe.word_1 = ns_tbd_mkword_1_novbr(flags, (u32) buflen);
1785 scq = ((vc_map *) vcc->dev_data)->scq;
1789 scqe.word_1 = ns_tbd_mkword_1(flags, (u32) 1, (u32) 1, (u32) buflen);
1793 if (push_scqe(card, vc, scq, &scqe, skb) != 0)
1795 atomic_inc(&vcc->stats->tx_err);
1796 dev_kfree_skb_any(skb);
1799 atomic_inc(&vcc->stats->tx);
1806 static int push_scqe(ns_dev *card, vc_map *vc, scq_info *scq, ns_scqe *tbd,
1807 struct sk_buff *skb)
1809 unsigned long flags;
1816 spin_lock_irqsave(&scq->lock, flags);
1817 while (scq->tail == scq->next)
1819 if (in_interrupt()) {
1820 spin_unlock_irqrestore(&scq->lock, flags);
1821 printk("nicstar%d: Error pushing TBD.\n", card->index);
1826 spin_unlock_irqrestore(&scq->lock, flags);
1827 interruptible_sleep_on_timeout(&scq->scqfull_waitq, SCQFULL_TIMEOUT);
1828 spin_lock_irqsave(&scq->lock, flags);
1831 spin_unlock_irqrestore(&scq->lock, flags);
1832 printk("nicstar%d: Timeout pushing TBD.\n", card->index);
1837 index = (int) (scq->next - scq->base);
1838 scq->skb[index] = skb;
1839 XPRINTK("nicstar%d: sending skb at 0x%x (pos %d).\n",
1840 card->index, (u32) skb, index);
1841 XPRINTK("nicstar%d: TBD written:\n0x%x\n0x%x\n0x%x\n0x%x\n at 0x%x.\n",
1842 card->index, le32_to_cpu(tbd->word_1), le32_to_cpu(tbd->word_2),
1843 le32_to_cpu(tbd->word_3), le32_to_cpu(tbd->word_4),
1845 if (scq->next == scq->last)
1846 scq->next = scq->base;
1851 if (scq->num_entries == VBR_SCQ_NUM_ENTRIES)
1859 if (vc->tbd_count >= MAX_TBD_PER_VC || scq->tbd_count >= MAX_TBD_PER_SCQ)
1863 while (scq->tail == scq->next)
1865 if (in_interrupt()) {
1866 data = (u32) virt_to_bus(scq->next);
1867 ns_write_sram(card, scq->scd, &data, 1);
1868 spin_unlock_irqrestore(&scq->lock, flags);
1869 printk("nicstar%d: Error pushing TSR.\n", card->index);
1874 if (has_run++) break;
1875 spin_unlock_irqrestore(&scq->lock, flags);
1876 interruptible_sleep_on_timeout(&scq->scqfull_waitq, SCQFULL_TIMEOUT);
1877 spin_lock_irqsave(&scq->lock, flags);
1882 tsr.word_1 = ns_tsr_mkword_1(NS_TSR_INTENABLE);
1884 scdi = NS_TSR_SCDISVBR;
1886 scdi = (vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE;
1887 scqi = scq->next - scq->base;
1888 tsr.word_2 = ns_tsr_mkword_2(scdi, scqi);
1889 tsr.word_3 = 0x00000000;
1890 tsr.word_4 = 0x00000000;
1894 scq->skb[index] = NULL;
1895 XPRINTK("nicstar%d: TSR written:\n0x%x\n0x%x\n0x%x\n0x%x\n at 0x%x.\n",
1896 card->index, le32_to_cpu(tsr.word_1), le32_to_cpu(tsr.word_2),
1897 le32_to_cpu(tsr.word_3), le32_to_cpu(tsr.word_4),
1899 if (scq->next == scq->last)
1900 scq->next = scq->base;
1907 PRINTK("nicstar%d: Timeout pushing TSR.\n", card->index);
1909 data = (u32) virt_to_bus(scq->next);
1910 ns_write_sram(card, scq->scd, &data, 1);
1912 spin_unlock_irqrestore(&scq->lock, flags);
1919 static void process_tsq(ns_dev *card)
1923 ns_tsi *previous = NULL, *one_ahead, *two_ahead;
1924 int serviced_entries; /* flag indicating at least on entry was serviced */
1926 serviced_entries = 0;
1928 if (card->tsq.next == card->tsq.last)
1929 one_ahead = card->tsq.base;
1931 one_ahead = card->tsq.next + 1;
1933 if (one_ahead == card->tsq.last)
1934 two_ahead = card->tsq.base;
1936 two_ahead = one_ahead + 1;
1938 while (!ns_tsi_isempty(card->tsq.next) || !ns_tsi_isempty(one_ahead) ||
1939 !ns_tsi_isempty(two_ahead))
1940 /* At most two empty, as stated in the 77201 errata */
1942 serviced_entries = 1;
1944 /* Skip the one or two possible empty entries */
1945 while (ns_tsi_isempty(card->tsq.next)) {
1946 if (card->tsq.next == card->tsq.last)
1947 card->tsq.next = card->tsq.base;
1952 if (!ns_tsi_tmrof(card->tsq.next))
1954 scdi = ns_tsi_getscdindex(card->tsq.next);
1955 if (scdi == NS_TSI_SCDISVBR)
1959 if (card->scd2vc[scdi] == NULL)
1961 printk("nicstar%d: could not find VC from SCD index.\n",
1963 ns_tsi_init(card->tsq.next);
1966 scq = card->scd2vc[scdi]->scq;
1968 drain_scq(card, scq, ns_tsi_getscqpos(card->tsq.next));
1970 wake_up_interruptible(&(scq->scqfull_waitq));
1973 ns_tsi_init(card->tsq.next);
1974 previous = card->tsq.next;
1975 if (card->tsq.next == card->tsq.last)
1976 card->tsq.next = card->tsq.base;
1980 if (card->tsq.next == card->tsq.last)
1981 one_ahead = card->tsq.base;
1983 one_ahead = card->tsq.next + 1;
1985 if (one_ahead == card->tsq.last)
1986 two_ahead = card->tsq.base;
1988 two_ahead = one_ahead + 1;
1991 if (serviced_entries) {
1992 writel((((u32) previous) - ((u32) card->tsq.base)),
1993 card->membase + TSQH);
1999 static void drain_scq(ns_dev *card, scq_info *scq, int pos)
2001 struct atm_vcc *vcc;
2002 struct sk_buff *skb;
2004 unsigned long flags;
2006 XPRINTK("nicstar%d: drain_scq() called, scq at 0x%x, pos %d.\n",
2007 card->index, (u32) scq, pos);
2008 if (pos >= scq->num_entries)
2010 printk("nicstar%d: Bad index on drain_scq().\n", card->index);
2014 spin_lock_irqsave(&scq->lock, flags);
2015 i = (int) (scq->tail - scq->base);
2016 if (++i == scq->num_entries)
2021 XPRINTK("nicstar%d: freeing skb at 0x%x (index %d).\n",
2022 card->index, (u32) skb, i);
2025 vcc = ATM_SKB(skb)->vcc;
2026 if (vcc && vcc->pop != NULL) {
2029 dev_kfree_skb_irq(skb);
2033 if (++i == scq->num_entries)
2036 scq->tail = scq->base + pos;
2037 spin_unlock_irqrestore(&scq->lock, flags);
2042 static void process_rsq(ns_dev *card)
2046 if (!ns_rsqe_valid(card->rsq.next))
2049 dequeue_rx(card, card->rsq.next);
2050 ns_rsqe_init(card->rsq.next);
2051 previous = card->rsq.next;
2052 if (card->rsq.next == card->rsq.last)
2053 card->rsq.next = card->rsq.base;
2056 } while (ns_rsqe_valid(card->rsq.next));
2057 writel((((u32) previous) - ((u32) card->rsq.base)),
2058 card->membase + RSQH);
2063 static void dequeue_rx(ns_dev *card, ns_rsqe *rsqe)
2067 struct sk_buff *iovb;
2069 struct atm_vcc *vcc;
2070 struct sk_buff *skb;
2071 unsigned short aal5_len;
2075 stat = readl(card->membase + STAT);
2076 card->sbfqc = ns_stat_sfbqc_get(stat);
2077 card->lbfqc = ns_stat_lfbqc_get(stat);
2079 skb = (struct sk_buff *) le32_to_cpu(rsqe->buffer_handle);
2080 vpi = ns_rsqe_vpi(rsqe);
2081 vci = ns_rsqe_vci(rsqe);
2082 if (vpi >= 1UL << card->vpibits || vci >= 1UL << card->vcibits)
2084 printk("nicstar%d: SDU received for out-of-range vc %d.%d.\n",
2085 card->index, vpi, vci);
2086 recycle_rx_buf(card, skb);
2090 vc = &(card->vcmap[vpi << card->vcibits | vci]);
2093 RXPRINTK("nicstar%d: SDU received on non-rx vc %d.%d.\n",
2094 card->index, vpi, vci);
2095 recycle_rx_buf(card, skb);
2101 if (vcc->qos.aal == ATM_AAL0)
2104 unsigned char *cell;
2108 for (i = ns_rsqe_cellcount(rsqe); i; i--)
2110 if ((sb = dev_alloc_skb(NS_SMSKBSIZE)) == NULL)
2112 printk("nicstar%d: Can't allocate buffers for aal0.\n",
2114 atomic_add(i,&vcc->stats->rx_drop);
2117 if (!atm_charge(vcc, sb->truesize))
2119 RXPRINTK("nicstar%d: atm_charge() dropped aal0 packets.\n",
2121 atomic_add(i-1,&vcc->stats->rx_drop); /* already increased by 1 */
2122 dev_kfree_skb_any(sb);
2125 /* Rebuild the header */
2126 *((u32 *) sb->data) = le32_to_cpu(rsqe->word_1) << 4 |
2127 (ns_rsqe_clp(rsqe) ? 0x00000001 : 0x00000000);
2128 if (i == 1 && ns_rsqe_eopdu(rsqe))
2129 *((u32 *) sb->data) |= 0x00000002;
2130 skb_put(sb, NS_AAL0_HEADER);
2131 memcpy(skb_tail_pointer(sb), cell, ATM_CELL_PAYLOAD);
2132 skb_put(sb, ATM_CELL_PAYLOAD);
2133 ATM_SKB(sb)->vcc = vcc;
2134 __net_timestamp(sb);
2136 atomic_inc(&vcc->stats->rx);
2137 cell += ATM_CELL_PAYLOAD;
2140 recycle_rx_buf(card, skb);
2144 /* To reach this point, the AAL layer can only be AAL5 */
2146 if ((iovb = vc->rx_iov) == NULL)
2148 iovb = skb_dequeue(&(card->iovpool.queue));
2149 if (iovb == NULL) /* No buffers in the queue */
2151 iovb = alloc_skb(NS_IOVBUFSIZE, GFP_ATOMIC);
2154 printk("nicstar%d: Out of iovec buffers.\n", card->index);
2155 atomic_inc(&vcc->stats->rx_drop);
2156 recycle_rx_buf(card, skb);
2159 NS_SKB_CB(iovb)->buf_type = BUF_NONE;
2162 if (--card->iovpool.count < card->iovnr.min)
2164 struct sk_buff *new_iovb;
2165 if ((new_iovb = alloc_skb(NS_IOVBUFSIZE, GFP_ATOMIC)) != NULL)
2167 NS_SKB_CB(iovb)->buf_type = BUF_NONE;
2168 skb_queue_tail(&card->iovpool.queue, new_iovb);
2169 card->iovpool.count++;
2173 NS_SKB(iovb)->iovcnt = 0;
2175 iovb->data = iovb->head;
2176 skb_reset_tail_pointer(iovb);
2177 NS_SKB(iovb)->vcc = vcc;
2178 /* IMPORTANT: a pointer to the sk_buff containing the small or large
2179 buffer is stored as iovec base, NOT a pointer to the
2180 small or large buffer itself. */
2182 else if (NS_SKB(iovb)->iovcnt >= NS_MAX_IOVECS)
2184 printk("nicstar%d: received too big AAL5 SDU.\n", card->index);
2185 atomic_inc(&vcc->stats->rx_err);
2186 recycle_iovec_rx_bufs(card, (struct iovec *) iovb->data, NS_MAX_IOVECS);
2187 NS_SKB(iovb)->iovcnt = 0;
2189 iovb->data = iovb->head;
2190 skb_reset_tail_pointer(iovb);
2191 NS_SKB(iovb)->vcc = vcc;
2193 iov = &((struct iovec *) iovb->data)[NS_SKB(iovb)->iovcnt++];
2194 iov->iov_base = (void *) skb;
2195 iov->iov_len = ns_rsqe_cellcount(rsqe) * 48;
2196 iovb->len += iov->iov_len;
2198 if (NS_SKB(iovb)->iovcnt == 1)
2200 if (NS_SKB_CB(skb)->buf_type != BUF_SM)
2202 printk("nicstar%d: Expected a small buffer, and this is not one.\n",
2204 which_list(card, skb);
2205 atomic_inc(&vcc->stats->rx_err);
2206 recycle_rx_buf(card, skb);
2208 recycle_iov_buf(card, iovb);
2212 else /* NS_SKB(iovb)->iovcnt >= 2 */
2214 if (NS_SKB_CB(skb)->buf_type != BUF_LG)
2216 printk("nicstar%d: Expected a large buffer, and this is not one.\n",
2218 which_list(card, skb);
2219 atomic_inc(&vcc->stats->rx_err);
2220 recycle_iovec_rx_bufs(card, (struct iovec *) iovb->data,
2221 NS_SKB(iovb)->iovcnt);
2223 recycle_iov_buf(card, iovb);
2228 if (ns_rsqe_eopdu(rsqe))
2230 /* This works correctly regardless of the endianness of the host */
2231 unsigned char *L1L2 = (unsigned char *)((u32)skb->data +
2233 aal5_len = L1L2[0] << 8 | L1L2[1];
2234 len = (aal5_len == 0x0000) ? 0x10000 : aal5_len;
2235 if (ns_rsqe_crcerr(rsqe) ||
2236 len + 8 > iovb->len || len + (47 + 8) < iovb->len)
2238 printk("nicstar%d: AAL5 CRC error", card->index);
2239 if (len + 8 > iovb->len || len + (47 + 8) < iovb->len)
2240 printk(" - PDU size mismatch.\n");
2243 atomic_inc(&vcc->stats->rx_err);
2244 recycle_iovec_rx_bufs(card, (struct iovec *) iovb->data,
2245 NS_SKB(iovb)->iovcnt);
2247 recycle_iov_buf(card, iovb);
2251 /* By this point we (hopefully) have a complete SDU without errors. */
2253 if (NS_SKB(iovb)->iovcnt == 1) /* Just a small buffer */
2255 /* skb points to a small buffer */
2256 if (!atm_charge(vcc, skb->truesize))
2258 push_rxbufs(card, skb);
2259 atomic_inc(&vcc->stats->rx_drop);
2264 dequeue_sm_buf(card, skb);
2265 #ifdef NS_USE_DESTRUCTORS
2266 skb->destructor = ns_sb_destructor;
2267 #endif /* NS_USE_DESTRUCTORS */
2268 ATM_SKB(skb)->vcc = vcc;
2269 __net_timestamp(skb);
2270 vcc->push(vcc, skb);
2271 atomic_inc(&vcc->stats->rx);
2274 else if (NS_SKB(iovb)->iovcnt == 2) /* One small plus one large buffer */
2278 sb = (struct sk_buff *) (iov - 1)->iov_base;
2279 /* skb points to a large buffer */
2281 if (len <= NS_SMBUFSIZE)
2283 if (!atm_charge(vcc, sb->truesize))
2285 push_rxbufs(card, sb);
2286 atomic_inc(&vcc->stats->rx_drop);
2291 dequeue_sm_buf(card, sb);
2292 #ifdef NS_USE_DESTRUCTORS
2293 sb->destructor = ns_sb_destructor;
2294 #endif /* NS_USE_DESTRUCTORS */
2295 ATM_SKB(sb)->vcc = vcc;
2296 __net_timestamp(sb);
2298 atomic_inc(&vcc->stats->rx);
2301 push_rxbufs(card, skb);
2304 else /* len > NS_SMBUFSIZE, the usual case */
2306 if (!atm_charge(vcc, skb->truesize))
2308 push_rxbufs(card, skb);
2309 atomic_inc(&vcc->stats->rx_drop);
2313 dequeue_lg_buf(card, skb);
2314 #ifdef NS_USE_DESTRUCTORS
2315 skb->destructor = ns_lb_destructor;
2316 #endif /* NS_USE_DESTRUCTORS */
2317 skb_push(skb, NS_SMBUFSIZE);
2318 skb_copy_from_linear_data(sb, skb->data, NS_SMBUFSIZE);
2319 skb_put(skb, len - NS_SMBUFSIZE);
2320 ATM_SKB(skb)->vcc = vcc;
2321 __net_timestamp(skb);
2322 vcc->push(vcc, skb);
2323 atomic_inc(&vcc->stats->rx);
2326 push_rxbufs(card, sb);
2331 else /* Must push a huge buffer */
2333 struct sk_buff *hb, *sb, *lb;
2334 int remaining, tocopy;
2337 hb = skb_dequeue(&(card->hbpool.queue));
2338 if (hb == NULL) /* No buffers in the queue */
2341 hb = dev_alloc_skb(NS_HBUFSIZE);
2344 printk("nicstar%d: Out of huge buffers.\n", card->index);
2345 atomic_inc(&vcc->stats->rx_drop);
2346 recycle_iovec_rx_bufs(card, (struct iovec *) iovb->data,
2347 NS_SKB(iovb)->iovcnt);
2349 recycle_iov_buf(card, iovb);
2352 else if (card->hbpool.count < card->hbnr.min)
2354 struct sk_buff *new_hb;
2355 if ((new_hb = dev_alloc_skb(NS_HBUFSIZE)) != NULL)
2357 skb_queue_tail(&card->hbpool.queue, new_hb);
2358 card->hbpool.count++;
2361 NS_SKB_CB(hb)->buf_type = BUF_NONE;
2364 if (--card->hbpool.count < card->hbnr.min)
2366 struct sk_buff *new_hb;
2367 if ((new_hb = dev_alloc_skb(NS_HBUFSIZE)) != NULL)
2369 NS_SKB_CB(new_hb)->buf_type = BUF_NONE;
2370 skb_queue_tail(&card->hbpool.queue, new_hb);
2371 card->hbpool.count++;
2373 if (card->hbpool.count < card->hbnr.min)
2375 if ((new_hb = dev_alloc_skb(NS_HBUFSIZE)) != NULL)
2377 NS_SKB_CB(new_hb)->buf_type = BUF_NONE;
2378 skb_queue_tail(&card->hbpool.queue, new_hb);
2379 card->hbpool.count++;
2384 iov = (struct iovec *) iovb->data;
2386 if (!atm_charge(vcc, hb->truesize))
2388 recycle_iovec_rx_bufs(card, iov, NS_SKB(iovb)->iovcnt);
2389 if (card->hbpool.count < card->hbnr.max)
2391 skb_queue_tail(&card->hbpool.queue, hb);
2392 card->hbpool.count++;
2395 dev_kfree_skb_any(hb);
2396 atomic_inc(&vcc->stats->rx_drop);
2400 /* Copy the small buffer to the huge buffer */
2401 sb = (struct sk_buff *) iov->iov_base;
2402 skb_copy_from_linear_data(sb, hb->data, iov->iov_len);
2403 skb_put(hb, iov->iov_len);
2404 remaining = len - iov->iov_len;
2406 /* Free the small buffer */
2407 push_rxbufs(card, sb);
2409 /* Copy all large buffers to the huge buffer and free them */
2410 for (j = 1; j < NS_SKB(iovb)->iovcnt; j++)
2412 lb = (struct sk_buff *) iov->iov_base;
2413 tocopy = min_t(int, remaining, iov->iov_len);
2414 skb_copy_from_linear_data(lb, skb_tail_pointer(hb), tocopy);
2415 skb_put(hb, tocopy);
2417 remaining -= tocopy;
2418 push_rxbufs(card, lb);
2421 if (remaining != 0 || hb->len != len)
2422 printk("nicstar%d: Huge buffer len mismatch.\n", card->index);
2423 #endif /* EXTRA_DEBUG */
2424 ATM_SKB(hb)->vcc = vcc;
2425 #ifdef NS_USE_DESTRUCTORS
2426 hb->destructor = ns_hb_destructor;
2427 #endif /* NS_USE_DESTRUCTORS */
2428 __net_timestamp(hb);
2430 atomic_inc(&vcc->stats->rx);
2435 recycle_iov_buf(card, iovb);
2442 #ifdef NS_USE_DESTRUCTORS
2444 static void ns_sb_destructor(struct sk_buff *sb)
2449 card = (ns_dev *) ATM_SKB(sb)->vcc->dev->dev_data;
2450 stat = readl(card->membase + STAT);
2451 card->sbfqc = ns_stat_sfbqc_get(stat);
2452 card->lbfqc = ns_stat_lfbqc_get(stat);
2456 sb = __dev_alloc_skb(NS_SMSKBSIZE, GFP_KERNEL);
2459 NS_SKB_CB(sb)->buf_type = BUF_SM;
2460 skb_queue_tail(&card->sbpool.queue, sb);
2461 skb_reserve(sb, NS_AAL0_HEADER);
2462 push_rxbufs(card, sb);
2463 } while (card->sbfqc < card->sbnr.min);
2468 static void ns_lb_destructor(struct sk_buff *lb)
2473 card = (ns_dev *) ATM_SKB(lb)->vcc->dev->dev_data;
2474 stat = readl(card->membase + STAT);
2475 card->sbfqc = ns_stat_sfbqc_get(stat);
2476 card->lbfqc = ns_stat_lfbqc_get(stat);
2480 lb = __dev_alloc_skb(NS_LGSKBSIZE, GFP_KERNEL);
2483 NS_SKB_CB(lb)->buf_type = BUF_LG;
2484 skb_queue_tail(&card->lbpool.queue, lb);
2485 skb_reserve(lb, NS_SMBUFSIZE);
2486 push_rxbufs(card, lb);
2487 } while (card->lbfqc < card->lbnr.min);
2492 static void ns_hb_destructor(struct sk_buff *hb)
2496 card = (ns_dev *) ATM_SKB(hb)->vcc->dev->dev_data;
2498 while (card->hbpool.count < card->hbnr.init)
2500 hb = __dev_alloc_skb(NS_HBUFSIZE, GFP_KERNEL);
2503 NS_SKB_CB(hb)->buf_type = BUF_NONE;
2504 skb_queue_tail(&card->hbpool.queue, hb);
2505 card->hbpool.count++;
2509 #endif /* NS_USE_DESTRUCTORS */
2512 static void recycle_rx_buf(ns_dev *card, struct sk_buff *skb)
2514 struct ns_skb_cb *cb = NS_SKB_CB(skb);
2516 if (unlikely(cb->buf_type == BUF_NONE)) {
2517 printk("nicstar%d: What kind of rx buffer is this?\n", card->index);
2518 dev_kfree_skb_any(skb);
2520 push_rxbufs(card, skb);
2524 static void recycle_iovec_rx_bufs(ns_dev *card, struct iovec *iov, int count)
2527 recycle_rx_buf(card, (struct sk_buff *) (iov++)->iov_base);
2531 static void recycle_iov_buf(ns_dev *card, struct sk_buff *iovb)
2533 if (card->iovpool.count < card->iovnr.max)
2535 skb_queue_tail(&card->iovpool.queue, iovb);
2536 card->iovpool.count++;
2539 dev_kfree_skb_any(iovb);
2544 static void dequeue_sm_buf(ns_dev *card, struct sk_buff *sb)
2546 skb_unlink(sb, &card->sbpool.queue);
2547 #ifdef NS_USE_DESTRUCTORS
2548 if (card->sbfqc < card->sbnr.min)
2550 if (card->sbfqc < card->sbnr.init)
2552 struct sk_buff *new_sb;
2553 if ((new_sb = dev_alloc_skb(NS_SMSKBSIZE)) != NULL)
2555 NS_SKB_CB(new_sb)->buf_type = BUF_SM;
2556 skb_queue_tail(&card->sbpool.queue, new_sb);
2557 skb_reserve(new_sb, NS_AAL0_HEADER);
2558 push_rxbufs(card, new_sb);
2561 if (card->sbfqc < card->sbnr.init)
2562 #endif /* NS_USE_DESTRUCTORS */
2564 struct sk_buff *new_sb;
2565 if ((new_sb = dev_alloc_skb(NS_SMSKBSIZE)) != NULL)
2567 NS_SKB_CB(new_sb)->buf_type = BUF_SM;
2568 skb_queue_tail(&card->sbpool.queue, new_sb);
2569 skb_reserve(new_sb, NS_AAL0_HEADER);
2570 push_rxbufs(card, new_sb);
2577 static void dequeue_lg_buf(ns_dev *card, struct sk_buff *lb)
2579 skb_unlink(lb, &card->lbpool.queue);
2580 #ifdef NS_USE_DESTRUCTORS
2581 if (card->lbfqc < card->lbnr.min)
2583 if (card->lbfqc < card->lbnr.init)
2585 struct sk_buff *new_lb;
2586 if ((new_lb = dev_alloc_skb(NS_LGSKBSIZE)) != NULL)
2588 NS_SKB_CB(new_lb)->buf_type = BUF_LG;
2589 skb_queue_tail(&card->lbpool.queue, new_lb);
2590 skb_reserve(new_lb, NS_SMBUFSIZE);
2591 push_rxbufs(card, new_lb);
2594 if (card->lbfqc < card->lbnr.init)
2595 #endif /* NS_USE_DESTRUCTORS */
2597 struct sk_buff *new_lb;
2598 if ((new_lb = dev_alloc_skb(NS_LGSKBSIZE)) != NULL)
2600 NS_SKB_CB(new_lb)->buf_type = BUF_LG;
2601 skb_queue_tail(&card->lbpool.queue, new_lb);
2602 skb_reserve(new_lb, NS_SMBUFSIZE);
2603 push_rxbufs(card, new_lb);
2610 static int ns_proc_read(struct atm_dev *dev, loff_t *pos, char *page)
2617 card = (ns_dev *) dev->dev_data;
2618 stat = readl(card->membase + STAT);
2620 return sprintf(page, "Pool count min init max \n");
2622 return sprintf(page, "Small %5d %5d %5d %5d \n",
2623 ns_stat_sfbqc_get(stat), card->sbnr.min, card->sbnr.init,
2626 return sprintf(page, "Large %5d %5d %5d %5d \n",
2627 ns_stat_lfbqc_get(stat), card->lbnr.min, card->lbnr.init,
2630 return sprintf(page, "Huge %5d %5d %5d %5d \n", card->hbpool.count,
2631 card->hbnr.min, card->hbnr.init, card->hbnr.max);
2633 return sprintf(page, "Iovec %5d %5d %5d %5d \n", card->iovpool.count,
2634 card->iovnr.min, card->iovnr.init, card->iovnr.max);
2638 retval = sprintf(page, "Interrupt counter: %u \n", card->intcnt);
2643 /* Dump 25.6 Mbps PHY registers */
2644 /* Now there's a 25.6 Mbps PHY driver this code isn't needed. I left it
2645 here just in case it's needed for debugging. */
2646 if (card->max_pcr == ATM_25_PCR && !left--)
2651 for (i = 0; i < 4; i++)
2653 while (CMD_BUSY(card));
2654 writel(NS_CMD_READ_UTILITY | 0x00000200 | i, card->membase + CMD);
2655 while (CMD_BUSY(card));
2656 phy_regs[i] = readl(card->membase + DR0) & 0x000000FF;
2659 return sprintf(page, "PHY regs: 0x%02X 0x%02X 0x%02X 0x%02X \n",
2660 phy_regs[0], phy_regs[1], phy_regs[2], phy_regs[3]);
2662 #endif /* 0 - Dump 25.6 Mbps PHY registers */
2665 if (left-- < NS_TST_NUM_ENTRIES)
2667 if (card->tste2vc[left + 1] == NULL)
2668 return sprintf(page, "%5d - VBR/UBR \n", left + 1);
2670 return sprintf(page, "%5d - %d %d \n", left + 1,
2671 card->tste2vc[left + 1]->tx_vcc->vpi,
2672 card->tste2vc[left + 1]->tx_vcc->vci);
2680 static int ns_ioctl(struct atm_dev *dev, unsigned int cmd, void __user *arg)
2685 unsigned long flags;
2687 card = dev->dev_data;
2691 if (get_user(pl.buftype, &((pool_levels __user *) arg)->buftype))
2695 case NS_BUFTYPE_SMALL:
2696 pl.count = ns_stat_sfbqc_get(readl(card->membase + STAT));
2697 pl.level.min = card->sbnr.min;
2698 pl.level.init = card->sbnr.init;
2699 pl.level.max = card->sbnr.max;
2702 case NS_BUFTYPE_LARGE:
2703 pl.count = ns_stat_lfbqc_get(readl(card->membase + STAT));
2704 pl.level.min = card->lbnr.min;
2705 pl.level.init = card->lbnr.init;
2706 pl.level.max = card->lbnr.max;
2709 case NS_BUFTYPE_HUGE:
2710 pl.count = card->hbpool.count;
2711 pl.level.min = card->hbnr.min;
2712 pl.level.init = card->hbnr.init;
2713 pl.level.max = card->hbnr.max;
2716 case NS_BUFTYPE_IOVEC:
2717 pl.count = card->iovpool.count;
2718 pl.level.min = card->iovnr.min;
2719 pl.level.init = card->iovnr.init;
2720 pl.level.max = card->iovnr.max;
2724 return -ENOIOCTLCMD;
2727 if (!copy_to_user((pool_levels __user *) arg, &pl, sizeof(pl)))
2728 return (sizeof(pl));
2733 if (!capable(CAP_NET_ADMIN))
2735 if (copy_from_user(&pl, (pool_levels __user *) arg, sizeof(pl)))
2737 if (pl.level.min >= pl.level.init || pl.level.init >= pl.level.max)
2739 if (pl.level.min == 0)
2743 case NS_BUFTYPE_SMALL:
2744 if (pl.level.max > TOP_SB)
2746 card->sbnr.min = pl.level.min;
2747 card->sbnr.init = pl.level.init;
2748 card->sbnr.max = pl.level.max;
2751 case NS_BUFTYPE_LARGE:
2752 if (pl.level.max > TOP_LB)
2754 card->lbnr.min = pl.level.min;
2755 card->lbnr.init = pl.level.init;
2756 card->lbnr.max = pl.level.max;
2759 case NS_BUFTYPE_HUGE:
2760 if (pl.level.max > TOP_HB)
2762 card->hbnr.min = pl.level.min;
2763 card->hbnr.init = pl.level.init;
2764 card->hbnr.max = pl.level.max;
2767 case NS_BUFTYPE_IOVEC:
2768 if (pl.level.max > TOP_IOVB)
2770 card->iovnr.min = pl.level.min;
2771 card->iovnr.init = pl.level.init;
2772 card->iovnr.max = pl.level.max;
2782 if (!capable(CAP_NET_ADMIN))
2784 btype = (long) arg; /* a long is the same size as a pointer or bigger */
2787 case NS_BUFTYPE_SMALL:
2788 while (card->sbfqc < card->sbnr.init)
2792 sb = __dev_alloc_skb(NS_SMSKBSIZE, GFP_KERNEL);
2795 NS_SKB_CB(sb)->buf_type = BUF_SM;
2796 skb_queue_tail(&card->sbpool.queue, sb);
2797 skb_reserve(sb, NS_AAL0_HEADER);
2798 push_rxbufs(card, sb);
2802 case NS_BUFTYPE_LARGE:
2803 while (card->lbfqc < card->lbnr.init)
2807 lb = __dev_alloc_skb(NS_LGSKBSIZE, GFP_KERNEL);
2810 NS_SKB_CB(lb)->buf_type = BUF_LG;
2811 skb_queue_tail(&card->lbpool.queue, lb);
2812 skb_reserve(lb, NS_SMBUFSIZE);
2813 push_rxbufs(card, lb);
2817 case NS_BUFTYPE_HUGE:
2818 while (card->hbpool.count > card->hbnr.init)
2822 spin_lock_irqsave(&card->int_lock, flags);
2823 hb = skb_dequeue(&card->hbpool.queue);
2824 card->hbpool.count--;
2825 spin_unlock_irqrestore(&card->int_lock, flags);
2827 printk("nicstar%d: huge buffer count inconsistent.\n",
2830 dev_kfree_skb_any(hb);
2833 while (card->hbpool.count < card->hbnr.init)
2837 hb = __dev_alloc_skb(NS_HBUFSIZE, GFP_KERNEL);
2840 NS_SKB_CB(hb)->buf_type = BUF_NONE;
2841 spin_lock_irqsave(&card->int_lock, flags);
2842 skb_queue_tail(&card->hbpool.queue, hb);
2843 card->hbpool.count++;
2844 spin_unlock_irqrestore(&card->int_lock, flags);
2848 case NS_BUFTYPE_IOVEC:
2849 while (card->iovpool.count > card->iovnr.init)
2851 struct sk_buff *iovb;
2853 spin_lock_irqsave(&card->int_lock, flags);
2854 iovb = skb_dequeue(&card->iovpool.queue);
2855 card->iovpool.count--;
2856 spin_unlock_irqrestore(&card->int_lock, flags);
2858 printk("nicstar%d: iovec buffer count inconsistent.\n",
2861 dev_kfree_skb_any(iovb);
2864 while (card->iovpool.count < card->iovnr.init)
2866 struct sk_buff *iovb;
2868 iovb = alloc_skb(NS_IOVBUFSIZE, GFP_KERNEL);
2871 NS_SKB_CB(iovb)->buf_type = BUF_NONE;
2872 spin_lock_irqsave(&card->int_lock, flags);
2873 skb_queue_tail(&card->iovpool.queue, iovb);
2874 card->iovpool.count++;
2875 spin_unlock_irqrestore(&card->int_lock, flags);
2886 if (dev->phy && dev->phy->ioctl) {
2887 return dev->phy->ioctl(dev, cmd, arg);
2890 printk("nicstar%d: %s == NULL \n", card->index,
2891 dev->phy ? "dev->phy->ioctl" : "dev->phy");
2892 return -ENOIOCTLCMD;
2898 static void which_list(ns_dev *card, struct sk_buff *skb)
2900 printk("skb buf_type: 0x%08x\n", NS_SKB_CB(skb)->buf_type);
2904 static void ns_poll(unsigned long arg)
2908 unsigned long flags;
2911 PRINTK("nicstar: Entering ns_poll().\n");
2912 for (i = 0; i < num_cards; i++)
2915 if (spin_is_locked(&card->int_lock)) {
2916 /* Probably it isn't worth spinning */
2919 spin_lock_irqsave(&card->int_lock, flags);
2922 stat_r = readl(card->membase + STAT);
2923 if (stat_r & NS_STAT_TSIF)
2924 stat_w |= NS_STAT_TSIF;
2925 if (stat_r & NS_STAT_EOPDU)
2926 stat_w |= NS_STAT_EOPDU;
2931 writel(stat_w, card->membase + STAT);
2932 spin_unlock_irqrestore(&card->int_lock, flags);
2934 mod_timer(&ns_timer, jiffies + NS_POLL_PERIOD);
2935 PRINTK("nicstar: Leaving ns_poll().\n");
2940 static int ns_parse_mac(char *mac, unsigned char *esi)
2945 if (mac == NULL || esi == NULL)
2948 for (i = 0; i < 6; i++)
2950 if ((byte1 = ns_h2i(mac[j++])) < 0)
2952 if ((byte0 = ns_h2i(mac[j++])) < 0)
2954 esi[i] = (unsigned char) (byte1 * 16 + byte0);
2957 if (mac[j++] != ':')
2966 static short ns_h2i(char c)
2968 if (c >= '0' && c <= '9')
2969 return (short) (c - '0');
2970 if (c >= 'A' && c <= 'F')
2971 return (short) (c - 'A' + 10);
2972 if (c >= 'a' && c <= 'f')
2973 return (short) (c - 'a' + 10);
2979 static void ns_phy_put(struct atm_dev *dev, unsigned char value,
2983 unsigned long flags;
2985 card = dev->dev_data;
2986 spin_lock_irqsave(&card->res_lock, flags);
2987 while(CMD_BUSY(card));
2988 writel((unsigned long) value, card->membase + DR0);
2989 writel(NS_CMD_WRITE_UTILITY | 0x00000200 | (addr & 0x000000FF),
2990 card->membase + CMD);
2991 spin_unlock_irqrestore(&card->res_lock, flags);
2996 static unsigned char ns_phy_get(struct atm_dev *dev, unsigned long addr)
2999 unsigned long flags;
3002 card = dev->dev_data;
3003 spin_lock_irqsave(&card->res_lock, flags);
3004 while(CMD_BUSY(card));
3005 writel(NS_CMD_READ_UTILITY | 0x00000200 | (addr & 0x000000FF),
3006 card->membase + CMD);
3007 while(CMD_BUSY(card));
3008 data = readl(card->membase + DR0) & 0x000000FF;
3009 spin_unlock_irqrestore(&card->res_lock, flags);
3010 return (unsigned char) data;
3015 module_init(nicstar_init);
3016 module_exit(nicstar_cleanup);