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>
52 #include <linux/slab.h>
54 #include <asm/uaccess.h>
55 #include <asm/atomic.h>
57 #ifdef CONFIG_ATM_NICSTAR_USE_SUNI
59 #endif /* CONFIG_ATM_NICSTAR_USE_SUNI */
60 #ifdef CONFIG_ATM_NICSTAR_USE_IDT77105
62 #endif /* CONFIG_ATM_NICSTAR_USE_IDT77105 */
64 #if BITS_PER_LONG != 32
65 # error FIXME: this driver requires a 32-bit platform
68 /* Additional code ************************************************************/
70 #include "nicstarmac.c"
73 /* Configurable parameters ****************************************************/
81 #undef NS_USE_DESTRUCTORS /* For now keep this undefined unless you know
82 you're going to use only raw ATM */
85 /* Do not touch these *********************************************************/
88 #define TXPRINTK(args...) printk(args)
90 #define TXPRINTK(args...)
94 #define RXPRINTK(args...) printk(args)
96 #define RXPRINTK(args...)
100 #define PRINTK(args...) printk(args)
102 #define PRINTK(args...)
103 #endif /* GENERAL_DEBUG */
106 #define XPRINTK(args...) printk(args)
108 #define XPRINTK(args...)
109 #endif /* EXTRA_DEBUG */
112 /* Macros *********************************************************************/
114 #define CMD_BUSY(card) (readl((card)->membase + STAT) & NS_STAT_CMDBZ)
116 #define NS_DELAY mdelay(1)
118 #define ALIGN_BUS_ADDR(addr, alignment) \
119 ((((u32) (addr)) + (((u32) (alignment)) - 1)) & ~(((u32) (alignment)) - 1))
120 #define ALIGN_ADDRESS(addr, alignment) \
121 bus_to_virt(ALIGN_BUS_ADDR(virt_to_bus(addr), alignment))
126 #define ATM_SKB(s) (&(s)->atm)
130 /* Function declarations ******************************************************/
132 static u32 ns_read_sram(ns_dev *card, u32 sram_address);
133 static void ns_write_sram(ns_dev *card, u32 sram_address, u32 *value, int count);
134 static int __devinit ns_init_card(int i, struct pci_dev *pcidev);
135 static void __devinit ns_init_card_error(ns_dev *card, int error);
136 static scq_info *get_scq(int size, u32 scd);
137 static void free_scq(scq_info *scq, struct atm_vcc *vcc);
138 static void push_rxbufs(ns_dev *, struct sk_buff *);
139 static irqreturn_t ns_irq_handler(int irq, void *dev_id);
140 static int ns_open(struct atm_vcc *vcc);
141 static void ns_close(struct atm_vcc *vcc);
142 static void fill_tst(ns_dev *card, int n, vc_map *vc);
143 static int ns_send(struct atm_vcc *vcc, struct sk_buff *skb);
144 static int push_scqe(ns_dev *card, vc_map *vc, scq_info *scq, ns_scqe *tbd,
145 struct sk_buff *skb);
146 static void process_tsq(ns_dev *card);
147 static void drain_scq(ns_dev *card, scq_info *scq, int pos);
148 static void process_rsq(ns_dev *card);
149 static void dequeue_rx(ns_dev *card, ns_rsqe *rsqe);
150 #ifdef NS_USE_DESTRUCTORS
151 static void ns_sb_destructor(struct sk_buff *sb);
152 static void ns_lb_destructor(struct sk_buff *lb);
153 static void ns_hb_destructor(struct sk_buff *hb);
154 #endif /* NS_USE_DESTRUCTORS */
155 static void recycle_rx_buf(ns_dev *card, struct sk_buff *skb);
156 static void recycle_iovec_rx_bufs(ns_dev *card, struct iovec *iov, int count);
157 static void recycle_iov_buf(ns_dev *card, struct sk_buff *iovb);
158 static void dequeue_sm_buf(ns_dev *card, struct sk_buff *sb);
159 static void dequeue_lg_buf(ns_dev *card, struct sk_buff *lb);
160 static int ns_proc_read(struct atm_dev *dev, loff_t *pos, char *page);
161 static int ns_ioctl(struct atm_dev *dev, unsigned int cmd, void __user *arg);
162 static void which_list(ns_dev *card, struct sk_buff *skb);
163 static void ns_poll(unsigned long arg);
164 static int ns_parse_mac(char *mac, unsigned char *esi);
165 static short ns_h2i(char c);
166 static void ns_phy_put(struct atm_dev *dev, unsigned char value,
168 static unsigned char ns_phy_get(struct atm_dev *dev, unsigned long addr);
172 /* Global variables ***********************************************************/
174 static struct ns_dev *cards[NS_MAX_CARDS];
175 static unsigned num_cards;
176 static struct atmdev_ops atm_ops =
182 .phy_put = ns_phy_put,
183 .phy_get = ns_phy_get,
184 .proc_read = ns_proc_read,
185 .owner = THIS_MODULE,
187 static struct timer_list ns_timer;
188 static char *mac[NS_MAX_CARDS];
189 module_param_array(mac, charp, NULL, 0);
190 MODULE_LICENSE("GPL");
193 /* Functions*******************************************************************/
195 static int __devinit nicstar_init_one(struct pci_dev *pcidev,
196 const struct pci_device_id *ent)
198 static int index = -1;
204 error = ns_init_card(index, pcidev);
206 cards[index--] = NULL; /* don't increment index */
217 static void __devexit nicstar_remove_one(struct pci_dev *pcidev)
220 ns_dev *card = pci_get_drvdata(pcidev);
222 struct sk_buff *iovb;
228 if (cards[i] == NULL)
231 if (card->atmdev->phy && card->atmdev->phy->stop)
232 card->atmdev->phy->stop(card->atmdev);
234 /* Stop everything */
235 writel(0x00000000, card->membase + CFG);
237 /* De-register device */
238 atm_dev_deregister(card->atmdev);
240 /* Disable PCI device */
241 pci_disable_device(pcidev);
243 /* Free up resources */
245 PRINTK("nicstar%d: freeing %d huge buffers.\n", i, card->hbpool.count);
246 while ((hb = skb_dequeue(&card->hbpool.queue)) != NULL)
248 dev_kfree_skb_any(hb);
251 PRINTK("nicstar%d: %d huge buffers freed.\n", i, j);
253 PRINTK("nicstar%d: freeing %d iovec buffers.\n", i, card->iovpool.count);
254 while ((iovb = skb_dequeue(&card->iovpool.queue)) != NULL)
256 dev_kfree_skb_any(iovb);
259 PRINTK("nicstar%d: %d iovec buffers freed.\n", i, j);
260 while ((lb = skb_dequeue(&card->lbpool.queue)) != NULL)
261 dev_kfree_skb_any(lb);
262 while ((sb = skb_dequeue(&card->sbpool.queue)) != NULL)
263 dev_kfree_skb_any(sb);
264 free_scq(card->scq0, NULL);
265 for (j = 0; j < NS_FRSCD_NUM; j++)
267 if (card->scd2vc[j] != NULL)
268 free_scq(card->scd2vc[j]->scq, card->scd2vc[j]->tx_vcc);
270 kfree(card->rsq.org);
271 kfree(card->tsq.org);
272 free_irq(card->pcidev->irq, card);
273 iounmap(card->membase);
279 static struct pci_device_id nicstar_pci_tbl[] __devinitdata =
281 {PCI_VENDOR_ID_IDT, PCI_DEVICE_ID_IDT_IDT77201,
282 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
283 {0,} /* terminate list */
285 MODULE_DEVICE_TABLE(pci, nicstar_pci_tbl);
289 static struct pci_driver nicstar_driver = {
291 .id_table = nicstar_pci_tbl,
292 .probe = nicstar_init_one,
293 .remove = __devexit_p(nicstar_remove_one),
298 static int __init nicstar_init(void)
300 unsigned error = 0; /* Initialized to remove compile warning */
302 XPRINTK("nicstar: nicstar_init() called.\n");
304 error = pci_register_driver(&nicstar_driver);
306 TXPRINTK("nicstar: TX debug enabled.\n");
307 RXPRINTK("nicstar: RX debug enabled.\n");
308 PRINTK("nicstar: General debug enabled.\n");
310 printk("nicstar: using PHY loopback.\n");
311 #endif /* PHY_LOOPBACK */
312 XPRINTK("nicstar: nicstar_init() returned.\n");
315 init_timer(&ns_timer);
316 ns_timer.expires = jiffies + NS_POLL_PERIOD;
318 ns_timer.function = ns_poll;
319 add_timer(&ns_timer);
327 static void __exit nicstar_cleanup(void)
329 XPRINTK("nicstar: nicstar_cleanup() called.\n");
331 del_timer(&ns_timer);
333 pci_unregister_driver(&nicstar_driver);
335 XPRINTK("nicstar: nicstar_cleanup() returned.\n");
340 static u32 ns_read_sram(ns_dev *card, u32 sram_address)
345 sram_address &= 0x0007FFFC; /* address must be dword aligned */
346 sram_address |= 0x50000000; /* SRAM read command */
347 spin_lock_irqsave(&card->res_lock, flags);
348 while (CMD_BUSY(card));
349 writel(sram_address, card->membase + CMD);
350 while (CMD_BUSY(card));
351 data = readl(card->membase + DR0);
352 spin_unlock_irqrestore(&card->res_lock, flags);
358 static void ns_write_sram(ns_dev *card, u32 sram_address, u32 *value, int count)
362 count--; /* count range now is 0..3 instead of 1..4 */
364 c <<= 2; /* to use increments of 4 */
365 spin_lock_irqsave(&card->res_lock, flags);
366 while (CMD_BUSY(card));
367 for (i = 0; i <= c; i += 4)
368 writel(*(value++), card->membase + i);
369 /* Note: DR# registers are the first 4 dwords in nicstar's memspace,
370 so card->membase + DR0 == card->membase */
372 sram_address &= 0x0007FFFC;
373 sram_address |= (0x40000000 | count);
374 writel(sram_address, card->membase + CMD);
375 spin_unlock_irqrestore(&card->res_lock, flags);
379 static int __devinit ns_init_card(int i, struct pci_dev *pcidev)
382 struct ns_dev *card = NULL;
383 unsigned char pci_latency;
389 unsigned long membase;
393 if (pci_enable_device(pcidev))
395 printk("nicstar%d: can't enable PCI device\n", i);
397 ns_init_card_error(card, error);
401 if ((card = kmalloc(sizeof(ns_dev), GFP_KERNEL)) == NULL)
403 printk("nicstar%d: can't allocate memory for device structure.\n", i);
405 ns_init_card_error(card, error);
409 spin_lock_init(&card->int_lock);
410 spin_lock_init(&card->res_lock);
412 pci_set_drvdata(pcidev, card);
416 card->pcidev = pcidev;
417 membase = pci_resource_start(pcidev, 1);
418 card->membase = ioremap(membase, NS_IOREMAP_SIZE);
421 printk("nicstar%d: can't ioremap() membase.\n",i);
423 ns_init_card_error(card, error);
426 PRINTK("nicstar%d: membase at 0x%x.\n", i, card->membase);
428 pci_set_master(pcidev);
430 if (pci_read_config_byte(pcidev, PCI_LATENCY_TIMER, &pci_latency) != 0)
432 printk("nicstar%d: can't read PCI latency timer.\n", i);
434 ns_init_card_error(card, error);
437 #ifdef NS_PCI_LATENCY
438 if (pci_latency < NS_PCI_LATENCY)
440 PRINTK("nicstar%d: setting PCI latency timer to %d.\n", i, NS_PCI_LATENCY);
441 for (j = 1; j < 4; j++)
443 if (pci_write_config_byte(pcidev, PCI_LATENCY_TIMER, NS_PCI_LATENCY) != 0)
448 printk("nicstar%d: can't set PCI latency timer to %d.\n", i, NS_PCI_LATENCY);
450 ns_init_card_error(card, error);
454 #endif /* NS_PCI_LATENCY */
456 /* Clear timer overflow */
457 data = readl(card->membase + STAT);
458 if (data & NS_STAT_TMROF)
459 writel(NS_STAT_TMROF, card->membase + STAT);
462 writel(NS_CFG_SWRST, card->membase + CFG);
464 writel(0x00000000, card->membase + CFG);
467 writel(0x00000008, card->membase + GP);
469 writel(0x00000001, card->membase + GP);
471 while (CMD_BUSY(card));
472 writel(NS_CMD_WRITE_UTILITY | 0x00000100, card->membase + CMD); /* Sync UTOPIA with SAR clock */
475 /* Detect PHY type */
476 while (CMD_BUSY(card));
477 writel(NS_CMD_READ_UTILITY | 0x00000200, card->membase + CMD);
478 while (CMD_BUSY(card));
479 data = readl(card->membase + DR0);
482 printk("nicstar%d: PHY seems to be 25 Mbps.\n", i);
483 card->max_pcr = ATM_25_PCR;
484 while(CMD_BUSY(card));
485 writel(0x00000008, card->membase + DR0);
486 writel(NS_CMD_WRITE_UTILITY | 0x00000200, card->membase + CMD);
487 /* Clear an eventual pending interrupt */
488 writel(NS_STAT_SFBQF, card->membase + STAT);
490 while(CMD_BUSY(card));
491 writel(0x00000022, card->membase + DR0);
492 writel(NS_CMD_WRITE_UTILITY | 0x00000202, card->membase + CMD);
493 #endif /* PHY_LOOPBACK */
497 printk("nicstar%d: PHY seems to be 155 Mbps.\n", i);
498 card->max_pcr = ATM_OC3_PCR;
500 while(CMD_BUSY(card));
501 writel(0x00000002, card->membase + DR0);
502 writel(NS_CMD_WRITE_UTILITY | 0x00000205, card->membase + CMD);
503 #endif /* PHY_LOOPBACK */
506 printk("nicstar%d: unknown PHY type (0x%08X).\n", i, data);
508 ns_init_card_error(card, error);
511 writel(0x00000000, card->membase + GP);
513 /* Determine SRAM size */
515 ns_write_sram(card, 0x1C003, &data, 1);
517 ns_write_sram(card, 0x14003, &data, 1);
518 if (ns_read_sram(card, 0x14003) == 0x89ABCDEF &&
519 ns_read_sram(card, 0x1C003) == 0x76543210)
520 card->sram_size = 128;
522 card->sram_size = 32;
523 PRINTK("nicstar%d: %dK x 32bit SRAM size.\n", i, card->sram_size);
525 card->rct_size = NS_MAX_RCTSIZE;
527 #if (NS_MAX_RCTSIZE == 4096)
528 if (card->sram_size == 128)
529 printk("nicstar%d: limiting maximum VCI. See NS_MAX_RCTSIZE in nicstar.h\n", i);
530 #elif (NS_MAX_RCTSIZE == 16384)
531 if (card->sram_size == 32)
533 printk("nicstar%d: wasting memory. See NS_MAX_RCTSIZE in nicstar.h\n", i);
534 card->rct_size = 4096;
537 #error NS_MAX_RCTSIZE must be either 4096 or 16384 in nicstar.c
540 card->vpibits = NS_VPIBITS;
541 if (card->rct_size == 4096)
542 card->vcibits = 12 - NS_VPIBITS;
543 else /* card->rct_size == 16384 */
544 card->vcibits = 14 - NS_VPIBITS;
546 /* Initialize the nicstar eeprom/eprom stuff, for the MAC addr */
548 nicstar_init_eprom(card->membase);
550 /* Set the VPI/VCI MSb mask to zero so we can receive OAM cells */
551 writel(0x00000000, card->membase + VPM);
554 card->tsq.org = kmalloc(NS_TSQSIZE + NS_TSQ_ALIGNMENT, GFP_KERNEL);
555 if (card->tsq.org == NULL)
557 printk("nicstar%d: can't allocate TSQ.\n", i);
559 ns_init_card_error(card, error);
562 card->tsq.base = (ns_tsi *) ALIGN_ADDRESS(card->tsq.org, NS_TSQ_ALIGNMENT);
563 card->tsq.next = card->tsq.base;
564 card->tsq.last = card->tsq.base + (NS_TSQ_NUM_ENTRIES - 1);
565 for (j = 0; j < NS_TSQ_NUM_ENTRIES; j++)
566 ns_tsi_init(card->tsq.base + j);
567 writel(0x00000000, card->membase + TSQH);
568 writel((u32) virt_to_bus(card->tsq.base), card->membase + TSQB);
569 PRINTK("nicstar%d: TSQ base at 0x%x 0x%x 0x%x.\n", i, (u32) card->tsq.base,
570 (u32) virt_to_bus(card->tsq.base), readl(card->membase + TSQB));
573 card->rsq.org = kmalloc(NS_RSQSIZE + NS_RSQ_ALIGNMENT, GFP_KERNEL);
574 if (card->rsq.org == NULL)
576 printk("nicstar%d: can't allocate RSQ.\n", i);
578 ns_init_card_error(card, error);
581 card->rsq.base = (ns_rsqe *) ALIGN_ADDRESS(card->rsq.org, NS_RSQ_ALIGNMENT);
582 card->rsq.next = card->rsq.base;
583 card->rsq.last = card->rsq.base + (NS_RSQ_NUM_ENTRIES - 1);
584 for (j = 0; j < NS_RSQ_NUM_ENTRIES; j++)
585 ns_rsqe_init(card->rsq.base + j);
586 writel(0x00000000, card->membase + RSQH);
587 writel((u32) virt_to_bus(card->rsq.base), card->membase + RSQB);
588 PRINTK("nicstar%d: RSQ base at 0x%x.\n", i, (u32) card->rsq.base);
590 /* Initialize SCQ0, the only VBR SCQ used */
593 card->scq0 = get_scq(VBR_SCQSIZE, NS_VRSCD0);
594 if (card->scq0 == NULL)
596 printk("nicstar%d: can't get SCQ0.\n", i);
598 ns_init_card_error(card, error);
601 u32d[0] = (u32) virt_to_bus(card->scq0->base);
602 u32d[1] = (u32) 0x00000000;
603 u32d[2] = (u32) 0xffffffff;
604 u32d[3] = (u32) 0x00000000;
605 ns_write_sram(card, NS_VRSCD0, u32d, 4);
606 ns_write_sram(card, NS_VRSCD1, u32d, 4); /* These last two won't be used */
607 ns_write_sram(card, NS_VRSCD2, u32d, 4); /* but are initialized, just in case... */
608 card->scq0->scd = NS_VRSCD0;
609 PRINTK("nicstar%d: VBR-SCQ0 base at 0x%x.\n", i, (u32) card->scq0->base);
611 /* Initialize TSTs */
612 card->tst_addr = NS_TST0;
613 card->tst_free_entries = NS_TST_NUM_ENTRIES;
614 data = NS_TST_OPCODE_VARIABLE;
615 for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
616 ns_write_sram(card, NS_TST0 + j, &data, 1);
617 data = ns_tste_make(NS_TST_OPCODE_END, NS_TST0);
618 ns_write_sram(card, NS_TST0 + NS_TST_NUM_ENTRIES, &data, 1);
619 for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
620 ns_write_sram(card, NS_TST1 + j, &data, 1);
621 data = ns_tste_make(NS_TST_OPCODE_END, NS_TST1);
622 ns_write_sram(card, NS_TST1 + NS_TST_NUM_ENTRIES, &data, 1);
623 for (j = 0; j < NS_TST_NUM_ENTRIES; j++)
624 card->tste2vc[j] = NULL;
625 writel(NS_TST0 << 2, card->membase + TSTB);
628 /* Initialize RCT. AAL type is set on opening the VC. */
630 u32d[0] = NS_RCTE_RAWCELLINTEN;
632 u32d[0] = 0x00000000;
633 #endif /* RCQ_SUPPORT */
634 u32d[1] = 0x00000000;
635 u32d[2] = 0x00000000;
636 u32d[3] = 0xFFFFFFFF;
637 for (j = 0; j < card->rct_size; j++)
638 ns_write_sram(card, j * 4, u32d, 4);
640 memset(card->vcmap, 0, NS_MAX_RCTSIZE * sizeof(vc_map));
642 for (j = 0; j < NS_FRSCD_NUM; j++)
643 card->scd2vc[j] = NULL;
645 /* Initialize buffer levels */
646 card->sbnr.min = MIN_SB;
647 card->sbnr.init = NUM_SB;
648 card->sbnr.max = MAX_SB;
649 card->lbnr.min = MIN_LB;
650 card->lbnr.init = NUM_LB;
651 card->lbnr.max = MAX_LB;
652 card->iovnr.min = MIN_IOVB;
653 card->iovnr.init = NUM_IOVB;
654 card->iovnr.max = MAX_IOVB;
655 card->hbnr.min = MIN_HB;
656 card->hbnr.init = NUM_HB;
657 card->hbnr.max = MAX_HB;
659 card->sm_handle = 0x00000000;
660 card->sm_addr = 0x00000000;
661 card->lg_handle = 0x00000000;
662 card->lg_addr = 0x00000000;
664 card->efbie = 1; /* To prevent push_rxbufs from enabling the interrupt */
666 /* Pre-allocate some huge buffers */
667 skb_queue_head_init(&card->hbpool.queue);
668 card->hbpool.count = 0;
669 for (j = 0; j < NUM_HB; j++)
672 hb = __dev_alloc_skb(NS_HBUFSIZE, GFP_KERNEL);
675 printk("nicstar%d: can't allocate %dth of %d huge buffers.\n",
678 ns_init_card_error(card, error);
681 NS_SKB_CB(hb)->buf_type = BUF_NONE;
682 skb_queue_tail(&card->hbpool.queue, hb);
683 card->hbpool.count++;
687 /* Allocate large buffers */
688 skb_queue_head_init(&card->lbpool.queue);
689 card->lbpool.count = 0; /* Not used */
690 for (j = 0; j < NUM_LB; j++)
693 lb = __dev_alloc_skb(NS_LGSKBSIZE, GFP_KERNEL);
696 printk("nicstar%d: can't allocate %dth of %d large buffers.\n",
699 ns_init_card_error(card, error);
702 NS_SKB_CB(lb)->buf_type = BUF_LG;
703 skb_queue_tail(&card->lbpool.queue, lb);
704 skb_reserve(lb, NS_SMBUFSIZE);
705 push_rxbufs(card, lb);
706 /* Due to the implementation of push_rxbufs() this is 1, not 0 */
710 card->rawch = (u32) virt_to_bus(lb->data);
713 /* Test for strange behaviour which leads to crashes */
714 if ((bcount = ns_stat_lfbqc_get(readl(card->membase + STAT))) < card->lbnr.min)
716 printk("nicstar%d: Strange... Just allocated %d large buffers and lfbqc = %d.\n",
719 ns_init_card_error(card, error);
724 /* Allocate small buffers */
725 skb_queue_head_init(&card->sbpool.queue);
726 card->sbpool.count = 0; /* Not used */
727 for (j = 0; j < NUM_SB; j++)
730 sb = __dev_alloc_skb(NS_SMSKBSIZE, GFP_KERNEL);
733 printk("nicstar%d: can't allocate %dth of %d small buffers.\n",
736 ns_init_card_error(card, error);
739 NS_SKB_CB(sb)->buf_type = BUF_SM;
740 skb_queue_tail(&card->sbpool.queue, sb);
741 skb_reserve(sb, NS_AAL0_HEADER);
742 push_rxbufs(card, sb);
744 /* Test for strange behaviour which leads to crashes */
745 if ((bcount = ns_stat_sfbqc_get(readl(card->membase + STAT))) < card->sbnr.min)
747 printk("nicstar%d: Strange... Just allocated %d small buffers and sfbqc = %d.\n",
750 ns_init_card_error(card, error);
755 /* Allocate iovec buffers */
756 skb_queue_head_init(&card->iovpool.queue);
757 card->iovpool.count = 0;
758 for (j = 0; j < NUM_IOVB; j++)
760 struct sk_buff *iovb;
761 iovb = alloc_skb(NS_IOVBUFSIZE, GFP_KERNEL);
764 printk("nicstar%d: can't allocate %dth of %d iovec buffers.\n",
767 ns_init_card_error(card, error);
770 NS_SKB_CB(iovb)->buf_type = BUF_NONE;
771 skb_queue_tail(&card->iovpool.queue, iovb);
772 card->iovpool.count++;
775 /* Configure NICStAR */
776 if (card->rct_size == 4096)
777 ns_cfg_rctsize = NS_CFG_RCTSIZE_4096_ENTRIES;
778 else /* (card->rct_size == 16384) */
779 ns_cfg_rctsize = NS_CFG_RCTSIZE_16384_ENTRIES;
784 if (request_irq(pcidev->irq, &ns_irq_handler, IRQF_DISABLED | IRQF_SHARED, "nicstar", card) != 0)
786 printk("nicstar%d: can't allocate IRQ %d.\n", i, pcidev->irq);
788 ns_init_card_error(card, error);
792 /* Register device */
793 card->atmdev = atm_dev_register("nicstar", &atm_ops, -1, NULL);
794 if (card->atmdev == NULL)
796 printk("nicstar%d: can't register device.\n", i);
798 ns_init_card_error(card, error);
802 if (ns_parse_mac(mac[i], card->atmdev->esi)) {
803 nicstar_read_eprom(card->membase, NICSTAR_EPROM_MAC_ADDR_OFFSET,
804 card->atmdev->esi, 6);
805 if (memcmp(card->atmdev->esi, "\x00\x00\x00\x00\x00\x00", 6) == 0) {
806 nicstar_read_eprom(card->membase, NICSTAR_EPROM_MAC_ADDR_OFFSET_ALT,
807 card->atmdev->esi, 6);
811 printk("nicstar%d: MAC address %pM\n", i, card->atmdev->esi);
813 card->atmdev->dev_data = card;
814 card->atmdev->ci_range.vpi_bits = card->vpibits;
815 card->atmdev->ci_range.vci_bits = card->vcibits;
816 card->atmdev->link_rate = card->max_pcr;
817 card->atmdev->phy = NULL;
819 #ifdef CONFIG_ATM_NICSTAR_USE_SUNI
820 if (card->max_pcr == ATM_OC3_PCR)
821 suni_init(card->atmdev);
822 #endif /* CONFIG_ATM_NICSTAR_USE_SUNI */
824 #ifdef CONFIG_ATM_NICSTAR_USE_IDT77105
825 if (card->max_pcr == ATM_25_PCR)
826 idt77105_init(card->atmdev);
827 #endif /* CONFIG_ATM_NICSTAR_USE_IDT77105 */
829 if (card->atmdev->phy && card->atmdev->phy->start)
830 card->atmdev->phy->start(card->atmdev);
832 writel(NS_CFG_RXPATH |
839 NS_CFG_RXINT_NODELAY |
840 NS_CFG_RAWIE | /* Only enabled if RCQ_SUPPORT */
844 NS_CFG_TSQFIE_OPT | /* Only enabled if ENABLE_TSQFIE */
846 card->membase + CFG);
855 static void __devinit ns_init_card_error(ns_dev *card, int error)
859 writel(0x00000000, card->membase + CFG);
863 struct sk_buff *iovb;
864 while ((iovb = skb_dequeue(&card->iovpool.queue)) != NULL)
865 dev_kfree_skb_any(iovb);
870 while ((sb = skb_dequeue(&card->sbpool.queue)) != NULL)
871 dev_kfree_skb_any(sb);
872 free_scq(card->scq0, NULL);
877 while ((lb = skb_dequeue(&card->lbpool.queue)) != NULL)
878 dev_kfree_skb_any(lb);
883 while ((hb = skb_dequeue(&card->hbpool.queue)) != NULL)
884 dev_kfree_skb_any(hb);
888 kfree(card->rsq.org);
892 kfree(card->tsq.org);
896 free_irq(card->pcidev->irq, card);
900 iounmap(card->membase);
904 pci_disable_device(card->pcidev);
911 static scq_info *get_scq(int size, u32 scd)
916 if (size != VBR_SCQSIZE && size != CBR_SCQSIZE)
919 scq = kmalloc(sizeof(scq_info), GFP_KERNEL);
922 scq->org = kmalloc(2 * size, GFP_KERNEL);
923 if (scq->org == NULL)
928 scq->skb = kmalloc(sizeof(struct sk_buff *) *
929 (size / NS_SCQE_SIZE), GFP_KERNEL);
930 if (scq->skb == NULL)
936 scq->num_entries = size / NS_SCQE_SIZE;
937 scq->base = (ns_scqe *) ALIGN_ADDRESS(scq->org, size);
938 scq->next = scq->base;
939 scq->last = scq->base + (scq->num_entries - 1);
940 scq->tail = scq->last;
942 scq->num_entries = size / NS_SCQE_SIZE;
944 init_waitqueue_head(&scq->scqfull_waitq);
946 spin_lock_init(&scq->lock);
948 for (i = 0; i < scq->num_entries; i++)
956 /* For variable rate SCQ vcc must be NULL */
957 static void free_scq(scq_info *scq, struct atm_vcc *vcc)
961 if (scq->num_entries == VBR_SCQ_NUM_ENTRIES)
962 for (i = 0; i < scq->num_entries; i++)
964 if (scq->skb[i] != NULL)
966 vcc = ATM_SKB(scq->skb[i])->vcc;
967 if (vcc->pop != NULL)
968 vcc->pop(vcc, scq->skb[i]);
970 dev_kfree_skb_any(scq->skb[i]);
973 else /* vcc must be != NULL */
977 printk("nicstar: free_scq() called with vcc == NULL for fixed rate scq.");
978 for (i = 0; i < scq->num_entries; i++)
979 dev_kfree_skb_any(scq->skb[i]);
982 for (i = 0; i < scq->num_entries; i++)
984 if (scq->skb[i] != NULL)
986 if (vcc->pop != NULL)
987 vcc->pop(vcc, scq->skb[i]);
989 dev_kfree_skb_any(scq->skb[i]);
1000 /* The handles passed must be pointers to the sk_buff containing the small
1001 or large buffer(s) cast to u32. */
1002 static void push_rxbufs(ns_dev *card, struct sk_buff *skb)
1004 struct ns_skb_cb *cb = NS_SKB_CB(skb);
1008 unsigned long flags;
1011 handle2 = addr2 = 0;
1013 addr1 = (u32)virt_to_bus(skb->data);
1015 #ifdef GENERAL_DEBUG
1017 printk("nicstar%d: push_rxbufs called with addr1 = 0.\n", card->index);
1018 #endif /* GENERAL_DEBUG */
1020 stat = readl(card->membase + STAT);
1021 card->sbfqc = ns_stat_sfbqc_get(stat);
1022 card->lbfqc = ns_stat_lfbqc_get(stat);
1023 if (cb->buf_type == BUF_SM)
1029 addr2 = card->sm_addr;
1030 handle2 = card->sm_handle;
1031 card->sm_addr = 0x00000000;
1032 card->sm_handle = 0x00000000;
1034 else /* (!sm_addr) */
1036 card->sm_addr = addr1;
1037 card->sm_handle = handle1;
1041 else /* buf_type == BUF_LG */
1047 addr2 = card->lg_addr;
1048 handle2 = card->lg_handle;
1049 card->lg_addr = 0x00000000;
1050 card->lg_handle = 0x00000000;
1052 else /* (!lg_addr) */
1054 card->lg_addr = addr1;
1055 card->lg_handle = handle1;
1062 if (cb->buf_type == BUF_SM)
1064 if (card->sbfqc >= card->sbnr.max)
1066 skb_unlink((struct sk_buff *) handle1, &card->sbpool.queue);
1067 dev_kfree_skb_any((struct sk_buff *) handle1);
1068 skb_unlink((struct sk_buff *) handle2, &card->sbpool.queue);
1069 dev_kfree_skb_any((struct sk_buff *) handle2);
1075 else /* (buf_type == BUF_LG) */
1077 if (card->lbfqc >= card->lbnr.max)
1079 skb_unlink((struct sk_buff *) handle1, &card->lbpool.queue);
1080 dev_kfree_skb_any((struct sk_buff *) handle1);
1081 skb_unlink((struct sk_buff *) handle2, &card->lbpool.queue);
1082 dev_kfree_skb_any((struct sk_buff *) handle2);
1089 spin_lock_irqsave(&card->res_lock, flags);
1091 while (CMD_BUSY(card));
1092 writel(addr2, card->membase + DR3);
1093 writel(handle2, card->membase + DR2);
1094 writel(addr1, card->membase + DR1);
1095 writel(handle1, card->membase + DR0);
1096 writel(NS_CMD_WRITE_FREEBUFQ | cb->buf_type, card->membase + CMD);
1098 spin_unlock_irqrestore(&card->res_lock, flags);
1100 XPRINTK("nicstar%d: Pushing %s buffers at 0x%x and 0x%x.\n", card->index,
1101 (cb->buf_type == BUF_SM ? "small" : "large"), addr1, addr2);
1104 if (!card->efbie && card->sbfqc >= card->sbnr.min &&
1105 card->lbfqc >= card->lbnr.min)
1108 writel((readl(card->membase + CFG) | NS_CFG_EFBIE), card->membase + CFG);
1116 static irqreturn_t ns_irq_handler(int irq, void *dev_id)
1120 struct atm_dev *dev;
1121 unsigned long flags;
1123 card = (ns_dev *) dev_id;
1127 PRINTK("nicstar%d: NICStAR generated an interrupt\n", card->index);
1129 spin_lock_irqsave(&card->int_lock, flags);
1131 stat_r = readl(card->membase + STAT);
1133 /* Transmit Status Indicator has been written to T. S. Queue */
1134 if (stat_r & NS_STAT_TSIF)
1136 TXPRINTK("nicstar%d: TSI interrupt\n", card->index);
1138 writel(NS_STAT_TSIF, card->membase + STAT);
1141 /* Incomplete CS-PDU has been transmitted */
1142 if (stat_r & NS_STAT_TXICP)
1144 writel(NS_STAT_TXICP, card->membase + STAT);
1145 TXPRINTK("nicstar%d: Incomplete CS-PDU transmitted.\n",
1149 /* Transmit Status Queue 7/8 full */
1150 if (stat_r & NS_STAT_TSQF)
1152 writel(NS_STAT_TSQF, card->membase + STAT);
1153 PRINTK("nicstar%d: TSQ full.\n", card->index);
1157 /* Timer overflow */
1158 if (stat_r & NS_STAT_TMROF)
1160 writel(NS_STAT_TMROF, card->membase + STAT);
1161 PRINTK("nicstar%d: Timer overflow.\n", card->index);
1164 /* PHY device interrupt signal active */
1165 if (stat_r & NS_STAT_PHYI)
1167 writel(NS_STAT_PHYI, card->membase + STAT);
1168 PRINTK("nicstar%d: PHY interrupt.\n", card->index);
1169 if (dev->phy && dev->phy->interrupt) {
1170 dev->phy->interrupt(dev);
1174 /* Small Buffer Queue is full */
1175 if (stat_r & NS_STAT_SFBQF)
1177 writel(NS_STAT_SFBQF, card->membase + STAT);
1178 printk("nicstar%d: Small free buffer queue is full.\n", card->index);
1181 /* Large Buffer Queue is full */
1182 if (stat_r & NS_STAT_LFBQF)
1184 writel(NS_STAT_LFBQF, card->membase + STAT);
1185 printk("nicstar%d: Large free buffer queue is full.\n", card->index);
1188 /* Receive Status Queue is full */
1189 if (stat_r & NS_STAT_RSQF)
1191 writel(NS_STAT_RSQF, card->membase + STAT);
1192 printk("nicstar%d: RSQ full.\n", card->index);
1196 /* Complete CS-PDU received */
1197 if (stat_r & NS_STAT_EOPDU)
1199 RXPRINTK("nicstar%d: End of CS-PDU received.\n", card->index);
1201 writel(NS_STAT_EOPDU, card->membase + STAT);
1204 /* Raw cell received */
1205 if (stat_r & NS_STAT_RAWCF)
1207 writel(NS_STAT_RAWCF, card->membase + STAT);
1209 printk("nicstar%d: Raw cell received and no support yet...\n",
1211 #endif /* RCQ_SUPPORT */
1212 /* NOTE: the following procedure may keep a raw cell pending until the
1213 next interrupt. As this preliminary support is only meant to
1214 avoid buffer leakage, this is not an issue. */
1215 while (readl(card->membase + RAWCT) != card->rawch)
1219 rawcell = (ns_rcqe *) bus_to_virt(card->rawch);
1220 if (ns_rcqe_islast(rawcell))
1222 struct sk_buff *oldbuf;
1224 oldbuf = card->rcbuf;
1225 card->rcbuf = (struct sk_buff *) ns_rcqe_nextbufhandle(rawcell);
1226 card->rawch = (u32) virt_to_bus(card->rcbuf->data);
1227 recycle_rx_buf(card, oldbuf);
1230 card->rawch += NS_RCQE_SIZE;
1234 /* Small buffer queue is empty */
1235 if (stat_r & NS_STAT_SFBQE)
1240 writel(NS_STAT_SFBQE, card->membase + STAT);
1241 printk("nicstar%d: Small free buffer queue empty.\n",
1243 for (i = 0; i < card->sbnr.min; i++)
1245 sb = dev_alloc_skb(NS_SMSKBSIZE);
1248 writel(readl(card->membase + CFG) & ~NS_CFG_EFBIE, card->membase + CFG);
1252 NS_SKB_CB(sb)->buf_type = BUF_SM;
1253 skb_queue_tail(&card->sbpool.queue, sb);
1254 skb_reserve(sb, NS_AAL0_HEADER);
1255 push_rxbufs(card, sb);
1261 /* Large buffer queue empty */
1262 if (stat_r & NS_STAT_LFBQE)
1267 writel(NS_STAT_LFBQE, card->membase + STAT);
1268 printk("nicstar%d: Large free buffer queue empty.\n",
1270 for (i = 0; i < card->lbnr.min; i++)
1272 lb = dev_alloc_skb(NS_LGSKBSIZE);
1275 writel(readl(card->membase + CFG) & ~NS_CFG_EFBIE, card->membase + CFG);
1279 NS_SKB_CB(lb)->buf_type = BUF_LG;
1280 skb_queue_tail(&card->lbpool.queue, lb);
1281 skb_reserve(lb, NS_SMBUFSIZE);
1282 push_rxbufs(card, lb);
1288 /* Receive Status Queue is 7/8 full */
1289 if (stat_r & NS_STAT_RSQAF)
1291 writel(NS_STAT_RSQAF, card->membase + STAT);
1292 RXPRINTK("nicstar%d: RSQ almost full.\n", card->index);
1296 spin_unlock_irqrestore(&card->int_lock, flags);
1297 PRINTK("nicstar%d: end of interrupt service\n", card->index);
1303 static int ns_open(struct atm_vcc *vcc)
1307 unsigned long tmpl, modl;
1308 int tcr, tcra; /* target cell rate, and absolute value */
1309 int n = 0; /* Number of entries in the TST. Initialized to remove
1310 the compiler warning. */
1312 int frscdi = 0; /* Index of the SCD. Initialized to remove the compiler
1313 warning. How I wish compilers were clever enough to
1314 tell which variables can truly be used
1316 int inuse; /* tx or rx vc already in use by another vcc */
1317 short vpi = vcc->vpi;
1320 card = (ns_dev *) vcc->dev->dev_data;
1321 PRINTK("nicstar%d: opening vpi.vci %d.%d \n", card->index, (int) vpi, vci);
1322 if (vcc->qos.aal != ATM_AAL5 && vcc->qos.aal != ATM_AAL0)
1324 PRINTK("nicstar%d: unsupported AAL.\n", card->index);
1328 vc = &(card->vcmap[vpi << card->vcibits | vci]);
1332 if (vcc->qos.txtp.traffic_class != ATM_NONE && vc->tx)
1334 if (vcc->qos.rxtp.traffic_class != ATM_NONE && vc->rx)
1338 printk("nicstar%d: %s vci already in use.\n", card->index,
1339 inuse == 1 ? "tx" : inuse == 2 ? "rx" : "tx and rx");
1343 set_bit(ATM_VF_ADDR,&vcc->flags);
1345 /* NOTE: You are not allowed to modify an open connection's QOS. To change
1346 that, remove the ATM_VF_PARTIAL flag checking. There may be other changes
1347 needed to do that. */
1348 if (!test_bit(ATM_VF_PARTIAL,&vcc->flags))
1352 set_bit(ATM_VF_PARTIAL,&vcc->flags);
1353 if (vcc->qos.txtp.traffic_class == ATM_CBR)
1355 /* Check requested cell rate and availability of SCD */
1356 if (vcc->qos.txtp.max_pcr == 0 && vcc->qos.txtp.pcr == 0 &&
1357 vcc->qos.txtp.min_pcr == 0)
1359 PRINTK("nicstar%d: trying to open a CBR vc with cell rate = 0 \n",
1361 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1362 clear_bit(ATM_VF_ADDR,&vcc->flags);
1366 tcr = atm_pcr_goal(&(vcc->qos.txtp));
1367 tcra = tcr >= 0 ? tcr : -tcr;
1369 PRINTK("nicstar%d: target cell rate = %d.\n", card->index,
1370 vcc->qos.txtp.max_pcr);
1372 tmpl = (unsigned long)tcra * (unsigned long)NS_TST_NUM_ENTRIES;
1373 modl = tmpl % card->max_pcr;
1375 n = (int)(tmpl / card->max_pcr);
1382 if ((n = (card->tst_free_entries - NS_TST_RESERVED)) <= 0)
1384 PRINTK("nicstar%d: no CBR bandwidth free.\n", card->index);
1385 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1386 clear_bit(ATM_VF_ADDR,&vcc->flags);
1393 printk("nicstar%d: selected bandwidth < granularity.\n", card->index);
1394 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1395 clear_bit(ATM_VF_ADDR,&vcc->flags);
1399 if (n > (card->tst_free_entries - NS_TST_RESERVED))
1401 PRINTK("nicstar%d: not enough free CBR bandwidth.\n", card->index);
1402 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1403 clear_bit(ATM_VF_ADDR,&vcc->flags);
1407 card->tst_free_entries -= n;
1409 XPRINTK("nicstar%d: writing %d tst entries.\n", card->index, n);
1410 for (frscdi = 0; frscdi < NS_FRSCD_NUM; frscdi++)
1412 if (card->scd2vc[frscdi] == NULL)
1414 card->scd2vc[frscdi] = vc;
1418 if (frscdi == NS_FRSCD_NUM)
1420 PRINTK("nicstar%d: no SCD available for CBR channel.\n", card->index);
1421 card->tst_free_entries += n;
1422 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1423 clear_bit(ATM_VF_ADDR,&vcc->flags);
1427 vc->cbr_scd = NS_FRSCD + frscdi * NS_FRSCD_SIZE;
1429 scq = get_scq(CBR_SCQSIZE, vc->cbr_scd);
1432 PRINTK("nicstar%d: can't get fixed rate SCQ.\n", card->index);
1433 card->scd2vc[frscdi] = NULL;
1434 card->tst_free_entries += n;
1435 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1436 clear_bit(ATM_VF_ADDR,&vcc->flags);
1440 u32d[0] = (u32) virt_to_bus(scq->base);
1441 u32d[1] = (u32) 0x00000000;
1442 u32d[2] = (u32) 0xffffffff;
1443 u32d[3] = (u32) 0x00000000;
1444 ns_write_sram(card, vc->cbr_scd, u32d, 4);
1446 fill_tst(card, n, vc);
1448 else if (vcc->qos.txtp.traffic_class == ATM_UBR)
1450 vc->cbr_scd = 0x00000000;
1451 vc->scq = card->scq0;
1454 if (vcc->qos.txtp.traffic_class != ATM_NONE)
1460 if (vcc->qos.rxtp.traffic_class != ATM_NONE)
1468 /* Open the connection in hardware */
1469 if (vcc->qos.aal == ATM_AAL5)
1470 status = NS_RCTE_AAL5 | NS_RCTE_CONNECTOPEN;
1471 else /* vcc->qos.aal == ATM_AAL0 */
1472 status = NS_RCTE_AAL0 | NS_RCTE_CONNECTOPEN;
1474 status |= NS_RCTE_RAWCELLINTEN;
1475 #endif /* RCQ_SUPPORT */
1476 ns_write_sram(card, NS_RCT + (vpi << card->vcibits | vci) *
1477 NS_RCT_ENTRY_SIZE, &status, 1);
1482 set_bit(ATM_VF_READY,&vcc->flags);
1488 static void ns_close(struct atm_vcc *vcc)
1496 card = vcc->dev->dev_data;
1497 PRINTK("nicstar%d: closing vpi.vci %d.%d \n", card->index,
1498 (int) vcc->vpi, vcc->vci);
1500 clear_bit(ATM_VF_READY,&vcc->flags);
1502 if (vcc->qos.rxtp.traffic_class != ATM_NONE)
1505 unsigned long flags;
1507 addr = NS_RCT + (vcc->vpi << card->vcibits | vcc->vci) * NS_RCT_ENTRY_SIZE;
1508 spin_lock_irqsave(&card->res_lock, flags);
1509 while(CMD_BUSY(card));
1510 writel(NS_CMD_CLOSE_CONNECTION | addr << 2, card->membase + CMD);
1511 spin_unlock_irqrestore(&card->res_lock, flags);
1514 if (vc->rx_iov != NULL)
1516 struct sk_buff *iovb;
1519 stat = readl(card->membase + STAT);
1520 card->sbfqc = ns_stat_sfbqc_get(stat);
1521 card->lbfqc = ns_stat_lfbqc_get(stat);
1523 PRINTK("nicstar%d: closing a VC with pending rx buffers.\n",
1526 recycle_iovec_rx_bufs(card, (struct iovec *) iovb->data,
1527 NS_SKB(iovb)->iovcnt);
1528 NS_SKB(iovb)->iovcnt = 0;
1529 NS_SKB(iovb)->vcc = NULL;
1530 spin_lock_irqsave(&card->int_lock, flags);
1531 recycle_iov_buf(card, iovb);
1532 spin_unlock_irqrestore(&card->int_lock, flags);
1537 if (vcc->qos.txtp.traffic_class != ATM_NONE)
1542 if (vcc->qos.txtp.traffic_class == ATM_CBR)
1544 unsigned long flags;
1552 spin_lock_irqsave(&scq->lock, flags);
1554 if (scqep == scq->base)
1558 if (scqep == scq->tail)
1560 spin_unlock_irqrestore(&scq->lock, flags);
1563 /* If the last entry is not a TSR, place one in the SCQ in order to
1564 be able to completely drain it and then close. */
1565 if (!ns_scqe_is_tsr(scqep) && scq->tail != scq->next)
1572 tsr.word_1 = ns_tsr_mkword_1(NS_TSR_INTENABLE);
1573 scdi = (vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE;
1574 scqi = scq->next - scq->base;
1575 tsr.word_2 = ns_tsr_mkword_2(scdi, scqi);
1576 tsr.word_3 = 0x00000000;
1577 tsr.word_4 = 0x00000000;
1580 scq->skb[index] = NULL;
1581 if (scq->next == scq->last)
1582 scq->next = scq->base;
1585 data = (u32) virt_to_bus(scq->next);
1586 ns_write_sram(card, scq->scd, &data, 1);
1588 spin_unlock_irqrestore(&scq->lock, flags);
1592 /* Free all TST entries */
1593 data = NS_TST_OPCODE_VARIABLE;
1594 for (i = 0; i < NS_TST_NUM_ENTRIES; i++)
1596 if (card->tste2vc[i] == vc)
1598 ns_write_sram(card, card->tst_addr + i, &data, 1);
1599 card->tste2vc[i] = NULL;
1600 card->tst_free_entries++;
1604 card->scd2vc[(vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE] = NULL;
1605 free_scq(vc->scq, vcc);
1608 /* remove all references to vcc before deleting it */
1609 if (vcc->qos.txtp.traffic_class != ATM_NONE)
1611 unsigned long flags;
1612 scq_info *scq = card->scq0;
1614 spin_lock_irqsave(&scq->lock, flags);
1616 for(i = 0; i < scq->num_entries; i++) {
1617 if(scq->skb[i] && ATM_SKB(scq->skb[i])->vcc == vcc) {
1618 ATM_SKB(scq->skb[i])->vcc = NULL;
1619 atm_return(vcc, scq->skb[i]->truesize);
1620 PRINTK("nicstar: deleted pending vcc mapping\n");
1624 spin_unlock_irqrestore(&scq->lock, flags);
1627 vcc->dev_data = NULL;
1628 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
1629 clear_bit(ATM_VF_ADDR,&vcc->flags);
1634 stat = readl(card->membase + STAT);
1635 cfg = readl(card->membase + CFG);
1636 printk("STAT = 0x%08X CFG = 0x%08X \n", stat, cfg);
1637 printk("TSQ: base = 0x%08X next = 0x%08X last = 0x%08X TSQT = 0x%08X \n",
1638 (u32) card->tsq.base, (u32) card->tsq.next,(u32) card->tsq.last,
1639 readl(card->membase + TSQT));
1640 printk("RSQ: base = 0x%08X next = 0x%08X last = 0x%08X RSQT = 0x%08X \n",
1641 (u32) card->rsq.base, (u32) card->rsq.next,(u32) card->rsq.last,
1642 readl(card->membase + RSQT));
1643 printk("Empty free buffer queue interrupt %s \n",
1644 card->efbie ? "enabled" : "disabled");
1645 printk("SBCNT = %d count = %d LBCNT = %d count = %d \n",
1646 ns_stat_sfbqc_get(stat), card->sbpool.count,
1647 ns_stat_lfbqc_get(stat), card->lbpool.count);
1648 printk("hbpool.count = %d iovpool.count = %d \n",
1649 card->hbpool.count, card->iovpool.count);
1651 #endif /* RX_DEBUG */
1656 static void fill_tst(ns_dev *card, int n, vc_map *vc)
1663 /* It would be very complicated to keep the two TSTs synchronized while
1664 assuring that writes are only made to the inactive TST. So, for now I
1665 will use only one TST. If problems occur, I will change this again */
1667 new_tst = card->tst_addr;
1669 /* Fill procedure */
1671 for (e = 0; e < NS_TST_NUM_ENTRIES; e++)
1673 if (card->tste2vc[e] == NULL)
1676 if (e == NS_TST_NUM_ENTRIES) {
1677 printk("nicstar%d: No free TST entries found. \n", card->index);
1682 cl = NS_TST_NUM_ENTRIES;
1683 data = ns_tste_make(NS_TST_OPCODE_FIXED, vc->cbr_scd);
1687 if (cl >= NS_TST_NUM_ENTRIES && card->tste2vc[e] == NULL)
1689 card->tste2vc[e] = vc;
1690 ns_write_sram(card, new_tst + e, &data, 1);
1691 cl -= NS_TST_NUM_ENTRIES;
1695 if (++e == NS_TST_NUM_ENTRIES) {
1701 /* End of fill procedure */
1703 data = ns_tste_make(NS_TST_OPCODE_END, new_tst);
1704 ns_write_sram(card, new_tst + NS_TST_NUM_ENTRIES, &data, 1);
1705 ns_write_sram(card, card->tst_addr + NS_TST_NUM_ENTRIES, &data, 1);
1706 card->tst_addr = new_tst;
1711 static int ns_send(struct atm_vcc *vcc, struct sk_buff *skb)
1716 unsigned long buflen;
1718 u32 flags; /* TBD flags, not CPU flags */
1720 card = vcc->dev->dev_data;
1721 TXPRINTK("nicstar%d: ns_send() called.\n", card->index);
1722 if ((vc = (vc_map *) vcc->dev_data) == NULL)
1724 printk("nicstar%d: vcc->dev_data == NULL on ns_send().\n", card->index);
1725 atomic_inc(&vcc->stats->tx_err);
1726 dev_kfree_skb_any(skb);
1732 printk("nicstar%d: Trying to transmit on a non-tx VC.\n", card->index);
1733 atomic_inc(&vcc->stats->tx_err);
1734 dev_kfree_skb_any(skb);
1738 if (vcc->qos.aal != ATM_AAL5 && vcc->qos.aal != ATM_AAL0)
1740 printk("nicstar%d: Only AAL0 and AAL5 are supported.\n", card->index);
1741 atomic_inc(&vcc->stats->tx_err);
1742 dev_kfree_skb_any(skb);
1746 if (skb_shinfo(skb)->nr_frags != 0)
1748 printk("nicstar%d: No scatter-gather yet.\n", card->index);
1749 atomic_inc(&vcc->stats->tx_err);
1750 dev_kfree_skb_any(skb);
1754 ATM_SKB(skb)->vcc = vcc;
1756 if (vcc->qos.aal == ATM_AAL5)
1758 buflen = (skb->len + 47 + 8) / 48 * 48; /* Multiple of 48 */
1759 flags = NS_TBD_AAL5;
1760 scqe.word_2 = cpu_to_le32((u32) virt_to_bus(skb->data));
1761 scqe.word_3 = cpu_to_le32((u32) skb->len);
1762 scqe.word_4 = ns_tbd_mkword_4(0, (u32) vcc->vpi, (u32) vcc->vci, 0,
1763 ATM_SKB(skb)->atm_options & ATM_ATMOPT_CLP ? 1 : 0);
1764 flags |= NS_TBD_EOPDU;
1766 else /* (vcc->qos.aal == ATM_AAL0) */
1768 buflen = ATM_CELL_PAYLOAD; /* i.e., 48 bytes */
1769 flags = NS_TBD_AAL0;
1770 scqe.word_2 = cpu_to_le32((u32) virt_to_bus(skb->data) + NS_AAL0_HEADER);
1771 scqe.word_3 = cpu_to_le32(0x00000000);
1772 if (*skb->data & 0x02) /* Payload type 1 - end of pdu */
1773 flags |= NS_TBD_EOPDU;
1774 scqe.word_4 = cpu_to_le32(*((u32 *) skb->data) & ~NS_TBD_VC_MASK);
1775 /* Force the VPI/VCI to be the same as in VCC struct */
1776 scqe.word_4 |= cpu_to_le32((((u32) vcc->vpi) << NS_TBD_VPI_SHIFT |
1777 ((u32) vcc->vci) << NS_TBD_VCI_SHIFT) &
1781 if (vcc->qos.txtp.traffic_class == ATM_CBR)
1783 scqe.word_1 = ns_tbd_mkword_1_novbr(flags, (u32) buflen);
1784 scq = ((vc_map *) vcc->dev_data)->scq;
1788 scqe.word_1 = ns_tbd_mkword_1(flags, (u32) 1, (u32) 1, (u32) buflen);
1792 if (push_scqe(card, vc, scq, &scqe, skb) != 0)
1794 atomic_inc(&vcc->stats->tx_err);
1795 dev_kfree_skb_any(skb);
1798 atomic_inc(&vcc->stats->tx);
1805 static int push_scqe(ns_dev *card, vc_map *vc, scq_info *scq, ns_scqe *tbd,
1806 struct sk_buff *skb)
1808 unsigned long flags;
1815 spin_lock_irqsave(&scq->lock, flags);
1816 while (scq->tail == scq->next)
1818 if (in_interrupt()) {
1819 spin_unlock_irqrestore(&scq->lock, flags);
1820 printk("nicstar%d: Error pushing TBD.\n", card->index);
1825 spin_unlock_irqrestore(&scq->lock, flags);
1826 interruptible_sleep_on_timeout(&scq->scqfull_waitq, SCQFULL_TIMEOUT);
1827 spin_lock_irqsave(&scq->lock, flags);
1830 spin_unlock_irqrestore(&scq->lock, flags);
1831 printk("nicstar%d: Timeout pushing TBD.\n", card->index);
1836 index = (int) (scq->next - scq->base);
1837 scq->skb[index] = skb;
1838 XPRINTK("nicstar%d: sending skb at 0x%x (pos %d).\n",
1839 card->index, (u32) skb, index);
1840 XPRINTK("nicstar%d: TBD written:\n0x%x\n0x%x\n0x%x\n0x%x\n at 0x%x.\n",
1841 card->index, le32_to_cpu(tbd->word_1), le32_to_cpu(tbd->word_2),
1842 le32_to_cpu(tbd->word_3), le32_to_cpu(tbd->word_4),
1844 if (scq->next == scq->last)
1845 scq->next = scq->base;
1850 if (scq->num_entries == VBR_SCQ_NUM_ENTRIES)
1858 if (vc->tbd_count >= MAX_TBD_PER_VC || scq->tbd_count >= MAX_TBD_PER_SCQ)
1862 while (scq->tail == scq->next)
1864 if (in_interrupt()) {
1865 data = (u32) virt_to_bus(scq->next);
1866 ns_write_sram(card, scq->scd, &data, 1);
1867 spin_unlock_irqrestore(&scq->lock, flags);
1868 printk("nicstar%d: Error pushing TSR.\n", card->index);
1873 if (has_run++) break;
1874 spin_unlock_irqrestore(&scq->lock, flags);
1875 interruptible_sleep_on_timeout(&scq->scqfull_waitq, SCQFULL_TIMEOUT);
1876 spin_lock_irqsave(&scq->lock, flags);
1881 tsr.word_1 = ns_tsr_mkword_1(NS_TSR_INTENABLE);
1883 scdi = NS_TSR_SCDISVBR;
1885 scdi = (vc->cbr_scd - NS_FRSCD) / NS_FRSCD_SIZE;
1886 scqi = scq->next - scq->base;
1887 tsr.word_2 = ns_tsr_mkword_2(scdi, scqi);
1888 tsr.word_3 = 0x00000000;
1889 tsr.word_4 = 0x00000000;
1893 scq->skb[index] = NULL;
1894 XPRINTK("nicstar%d: TSR written:\n0x%x\n0x%x\n0x%x\n0x%x\n at 0x%x.\n",
1895 card->index, le32_to_cpu(tsr.word_1), le32_to_cpu(tsr.word_2),
1896 le32_to_cpu(tsr.word_3), le32_to_cpu(tsr.word_4),
1898 if (scq->next == scq->last)
1899 scq->next = scq->base;
1906 PRINTK("nicstar%d: Timeout pushing TSR.\n", card->index);
1908 data = (u32) virt_to_bus(scq->next);
1909 ns_write_sram(card, scq->scd, &data, 1);
1911 spin_unlock_irqrestore(&scq->lock, flags);
1918 static void process_tsq(ns_dev *card)
1922 ns_tsi *previous = NULL, *one_ahead, *two_ahead;
1923 int serviced_entries; /* flag indicating at least on entry was serviced */
1925 serviced_entries = 0;
1927 if (card->tsq.next == card->tsq.last)
1928 one_ahead = card->tsq.base;
1930 one_ahead = card->tsq.next + 1;
1932 if (one_ahead == card->tsq.last)
1933 two_ahead = card->tsq.base;
1935 two_ahead = one_ahead + 1;
1937 while (!ns_tsi_isempty(card->tsq.next) || !ns_tsi_isempty(one_ahead) ||
1938 !ns_tsi_isempty(two_ahead))
1939 /* At most two empty, as stated in the 77201 errata */
1941 serviced_entries = 1;
1943 /* Skip the one or two possible empty entries */
1944 while (ns_tsi_isempty(card->tsq.next)) {
1945 if (card->tsq.next == card->tsq.last)
1946 card->tsq.next = card->tsq.base;
1951 if (!ns_tsi_tmrof(card->tsq.next))
1953 scdi = ns_tsi_getscdindex(card->tsq.next);
1954 if (scdi == NS_TSI_SCDISVBR)
1958 if (card->scd2vc[scdi] == NULL)
1960 printk("nicstar%d: could not find VC from SCD index.\n",
1962 ns_tsi_init(card->tsq.next);
1965 scq = card->scd2vc[scdi]->scq;
1967 drain_scq(card, scq, ns_tsi_getscqpos(card->tsq.next));
1969 wake_up_interruptible(&(scq->scqfull_waitq));
1972 ns_tsi_init(card->tsq.next);
1973 previous = card->tsq.next;
1974 if (card->tsq.next == card->tsq.last)
1975 card->tsq.next = card->tsq.base;
1979 if (card->tsq.next == card->tsq.last)
1980 one_ahead = card->tsq.base;
1982 one_ahead = card->tsq.next + 1;
1984 if (one_ahead == card->tsq.last)
1985 two_ahead = card->tsq.base;
1987 two_ahead = one_ahead + 1;
1990 if (serviced_entries) {
1991 writel((((u32) previous) - ((u32) card->tsq.base)),
1992 card->membase + TSQH);
1998 static void drain_scq(ns_dev *card, scq_info *scq, int pos)
2000 struct atm_vcc *vcc;
2001 struct sk_buff *skb;
2003 unsigned long flags;
2005 XPRINTK("nicstar%d: drain_scq() called, scq at 0x%x, pos %d.\n",
2006 card->index, (u32) scq, pos);
2007 if (pos >= scq->num_entries)
2009 printk("nicstar%d: Bad index on drain_scq().\n", card->index);
2013 spin_lock_irqsave(&scq->lock, flags);
2014 i = (int) (scq->tail - scq->base);
2015 if (++i == scq->num_entries)
2020 XPRINTK("nicstar%d: freeing skb at 0x%x (index %d).\n",
2021 card->index, (u32) skb, i);
2024 vcc = ATM_SKB(skb)->vcc;
2025 if (vcc && vcc->pop != NULL) {
2028 dev_kfree_skb_irq(skb);
2032 if (++i == scq->num_entries)
2035 scq->tail = scq->base + pos;
2036 spin_unlock_irqrestore(&scq->lock, flags);
2041 static void process_rsq(ns_dev *card)
2045 if (!ns_rsqe_valid(card->rsq.next))
2048 dequeue_rx(card, card->rsq.next);
2049 ns_rsqe_init(card->rsq.next);
2050 previous = card->rsq.next;
2051 if (card->rsq.next == card->rsq.last)
2052 card->rsq.next = card->rsq.base;
2055 } while (ns_rsqe_valid(card->rsq.next));
2056 writel((((u32) previous) - ((u32) card->rsq.base)),
2057 card->membase + RSQH);
2062 static void dequeue_rx(ns_dev *card, ns_rsqe *rsqe)
2066 struct sk_buff *iovb;
2068 struct atm_vcc *vcc;
2069 struct sk_buff *skb;
2070 unsigned short aal5_len;
2074 stat = readl(card->membase + STAT);
2075 card->sbfqc = ns_stat_sfbqc_get(stat);
2076 card->lbfqc = ns_stat_lfbqc_get(stat);
2078 skb = (struct sk_buff *) le32_to_cpu(rsqe->buffer_handle);
2079 vpi = ns_rsqe_vpi(rsqe);
2080 vci = ns_rsqe_vci(rsqe);
2081 if (vpi >= 1UL << card->vpibits || vci >= 1UL << card->vcibits)
2083 printk("nicstar%d: SDU received for out-of-range vc %d.%d.\n",
2084 card->index, vpi, vci);
2085 recycle_rx_buf(card, skb);
2089 vc = &(card->vcmap[vpi << card->vcibits | vci]);
2092 RXPRINTK("nicstar%d: SDU received on non-rx vc %d.%d.\n",
2093 card->index, vpi, vci);
2094 recycle_rx_buf(card, skb);
2100 if (vcc->qos.aal == ATM_AAL0)
2103 unsigned char *cell;
2107 for (i = ns_rsqe_cellcount(rsqe); i; i--)
2109 if ((sb = dev_alloc_skb(NS_SMSKBSIZE)) == NULL)
2111 printk("nicstar%d: Can't allocate buffers for aal0.\n",
2113 atomic_add(i,&vcc->stats->rx_drop);
2116 if (!atm_charge(vcc, sb->truesize))
2118 RXPRINTK("nicstar%d: atm_charge() dropped aal0 packets.\n",
2120 atomic_add(i-1,&vcc->stats->rx_drop); /* already increased by 1 */
2121 dev_kfree_skb_any(sb);
2124 /* Rebuild the header */
2125 *((u32 *) sb->data) = le32_to_cpu(rsqe->word_1) << 4 |
2126 (ns_rsqe_clp(rsqe) ? 0x00000001 : 0x00000000);
2127 if (i == 1 && ns_rsqe_eopdu(rsqe))
2128 *((u32 *) sb->data) |= 0x00000002;
2129 skb_put(sb, NS_AAL0_HEADER);
2130 memcpy(skb_tail_pointer(sb), cell, ATM_CELL_PAYLOAD);
2131 skb_put(sb, ATM_CELL_PAYLOAD);
2132 ATM_SKB(sb)->vcc = vcc;
2133 __net_timestamp(sb);
2135 atomic_inc(&vcc->stats->rx);
2136 cell += ATM_CELL_PAYLOAD;
2139 recycle_rx_buf(card, skb);
2143 /* To reach this point, the AAL layer can only be AAL5 */
2145 if ((iovb = vc->rx_iov) == NULL)
2147 iovb = skb_dequeue(&(card->iovpool.queue));
2148 if (iovb == NULL) /* No buffers in the queue */
2150 iovb = alloc_skb(NS_IOVBUFSIZE, GFP_ATOMIC);
2153 printk("nicstar%d: Out of iovec buffers.\n", card->index);
2154 atomic_inc(&vcc->stats->rx_drop);
2155 recycle_rx_buf(card, skb);
2158 NS_SKB_CB(iovb)->buf_type = BUF_NONE;
2161 if (--card->iovpool.count < card->iovnr.min)
2163 struct sk_buff *new_iovb;
2164 if ((new_iovb = alloc_skb(NS_IOVBUFSIZE, GFP_ATOMIC)) != NULL)
2166 NS_SKB_CB(iovb)->buf_type = BUF_NONE;
2167 skb_queue_tail(&card->iovpool.queue, new_iovb);
2168 card->iovpool.count++;
2172 NS_SKB(iovb)->iovcnt = 0;
2174 iovb->data = iovb->head;
2175 skb_reset_tail_pointer(iovb);
2176 NS_SKB(iovb)->vcc = vcc;
2177 /* IMPORTANT: a pointer to the sk_buff containing the small or large
2178 buffer is stored as iovec base, NOT a pointer to the
2179 small or large buffer itself. */
2181 else if (NS_SKB(iovb)->iovcnt >= NS_MAX_IOVECS)
2183 printk("nicstar%d: received too big AAL5 SDU.\n", card->index);
2184 atomic_inc(&vcc->stats->rx_err);
2185 recycle_iovec_rx_bufs(card, (struct iovec *) iovb->data, NS_MAX_IOVECS);
2186 NS_SKB(iovb)->iovcnt = 0;
2188 iovb->data = iovb->head;
2189 skb_reset_tail_pointer(iovb);
2190 NS_SKB(iovb)->vcc = vcc;
2192 iov = &((struct iovec *) iovb->data)[NS_SKB(iovb)->iovcnt++];
2193 iov->iov_base = (void *) skb;
2194 iov->iov_len = ns_rsqe_cellcount(rsqe) * 48;
2195 iovb->len += iov->iov_len;
2197 if (NS_SKB(iovb)->iovcnt == 1)
2199 if (NS_SKB_CB(skb)->buf_type != BUF_SM)
2201 printk("nicstar%d: Expected a small buffer, and this is not one.\n",
2203 which_list(card, skb);
2204 atomic_inc(&vcc->stats->rx_err);
2205 recycle_rx_buf(card, skb);
2207 recycle_iov_buf(card, iovb);
2211 else /* NS_SKB(iovb)->iovcnt >= 2 */
2213 if (NS_SKB_CB(skb)->buf_type != BUF_LG)
2215 printk("nicstar%d: Expected a large buffer, and this is not one.\n",
2217 which_list(card, skb);
2218 atomic_inc(&vcc->stats->rx_err);
2219 recycle_iovec_rx_bufs(card, (struct iovec *) iovb->data,
2220 NS_SKB(iovb)->iovcnt);
2222 recycle_iov_buf(card, iovb);
2227 if (ns_rsqe_eopdu(rsqe))
2229 /* This works correctly regardless of the endianness of the host */
2230 unsigned char *L1L2 = (unsigned char *)((u32)skb->data +
2232 aal5_len = L1L2[0] << 8 | L1L2[1];
2233 len = (aal5_len == 0x0000) ? 0x10000 : aal5_len;
2234 if (ns_rsqe_crcerr(rsqe) ||
2235 len + 8 > iovb->len || len + (47 + 8) < iovb->len)
2237 printk("nicstar%d: AAL5 CRC error", card->index);
2238 if (len + 8 > iovb->len || len + (47 + 8) < iovb->len)
2239 printk(" - PDU size mismatch.\n");
2242 atomic_inc(&vcc->stats->rx_err);
2243 recycle_iovec_rx_bufs(card, (struct iovec *) iovb->data,
2244 NS_SKB(iovb)->iovcnt);
2246 recycle_iov_buf(card, iovb);
2250 /* By this point we (hopefully) have a complete SDU without errors. */
2252 if (NS_SKB(iovb)->iovcnt == 1) /* Just a small buffer */
2254 /* skb points to a small buffer */
2255 if (!atm_charge(vcc, skb->truesize))
2257 push_rxbufs(card, skb);
2258 atomic_inc(&vcc->stats->rx_drop);
2263 dequeue_sm_buf(card, skb);
2264 #ifdef NS_USE_DESTRUCTORS
2265 skb->destructor = ns_sb_destructor;
2266 #endif /* NS_USE_DESTRUCTORS */
2267 ATM_SKB(skb)->vcc = vcc;
2268 __net_timestamp(skb);
2269 vcc->push(vcc, skb);
2270 atomic_inc(&vcc->stats->rx);
2273 else if (NS_SKB(iovb)->iovcnt == 2) /* One small plus one large buffer */
2277 sb = (struct sk_buff *) (iov - 1)->iov_base;
2278 /* skb points to a large buffer */
2280 if (len <= NS_SMBUFSIZE)
2282 if (!atm_charge(vcc, sb->truesize))
2284 push_rxbufs(card, sb);
2285 atomic_inc(&vcc->stats->rx_drop);
2290 dequeue_sm_buf(card, sb);
2291 #ifdef NS_USE_DESTRUCTORS
2292 sb->destructor = ns_sb_destructor;
2293 #endif /* NS_USE_DESTRUCTORS */
2294 ATM_SKB(sb)->vcc = vcc;
2295 __net_timestamp(sb);
2297 atomic_inc(&vcc->stats->rx);
2300 push_rxbufs(card, skb);
2303 else /* len > NS_SMBUFSIZE, the usual case */
2305 if (!atm_charge(vcc, skb->truesize))
2307 push_rxbufs(card, skb);
2308 atomic_inc(&vcc->stats->rx_drop);
2312 dequeue_lg_buf(card, skb);
2313 #ifdef NS_USE_DESTRUCTORS
2314 skb->destructor = ns_lb_destructor;
2315 #endif /* NS_USE_DESTRUCTORS */
2316 skb_push(skb, NS_SMBUFSIZE);
2317 skb_copy_from_linear_data(sb, skb->data, NS_SMBUFSIZE);
2318 skb_put(skb, len - NS_SMBUFSIZE);
2319 ATM_SKB(skb)->vcc = vcc;
2320 __net_timestamp(skb);
2321 vcc->push(vcc, skb);
2322 atomic_inc(&vcc->stats->rx);
2325 push_rxbufs(card, sb);
2330 else /* Must push a huge buffer */
2332 struct sk_buff *hb, *sb, *lb;
2333 int remaining, tocopy;
2336 hb = skb_dequeue(&(card->hbpool.queue));
2337 if (hb == NULL) /* No buffers in the queue */
2340 hb = dev_alloc_skb(NS_HBUFSIZE);
2343 printk("nicstar%d: Out of huge buffers.\n", card->index);
2344 atomic_inc(&vcc->stats->rx_drop);
2345 recycle_iovec_rx_bufs(card, (struct iovec *) iovb->data,
2346 NS_SKB(iovb)->iovcnt);
2348 recycle_iov_buf(card, iovb);
2351 else if (card->hbpool.count < card->hbnr.min)
2353 struct sk_buff *new_hb;
2354 if ((new_hb = dev_alloc_skb(NS_HBUFSIZE)) != NULL)
2356 skb_queue_tail(&card->hbpool.queue, new_hb);
2357 card->hbpool.count++;
2360 NS_SKB_CB(hb)->buf_type = BUF_NONE;
2363 if (--card->hbpool.count < card->hbnr.min)
2365 struct sk_buff *new_hb;
2366 if ((new_hb = dev_alloc_skb(NS_HBUFSIZE)) != NULL)
2368 NS_SKB_CB(new_hb)->buf_type = BUF_NONE;
2369 skb_queue_tail(&card->hbpool.queue, new_hb);
2370 card->hbpool.count++;
2372 if (card->hbpool.count < card->hbnr.min)
2374 if ((new_hb = dev_alloc_skb(NS_HBUFSIZE)) != NULL)
2376 NS_SKB_CB(new_hb)->buf_type = BUF_NONE;
2377 skb_queue_tail(&card->hbpool.queue, new_hb);
2378 card->hbpool.count++;
2383 iov = (struct iovec *) iovb->data;
2385 if (!atm_charge(vcc, hb->truesize))
2387 recycle_iovec_rx_bufs(card, iov, NS_SKB(iovb)->iovcnt);
2388 if (card->hbpool.count < card->hbnr.max)
2390 skb_queue_tail(&card->hbpool.queue, hb);
2391 card->hbpool.count++;
2394 dev_kfree_skb_any(hb);
2395 atomic_inc(&vcc->stats->rx_drop);
2399 /* Copy the small buffer to the huge buffer */
2400 sb = (struct sk_buff *) iov->iov_base;
2401 skb_copy_from_linear_data(sb, hb->data, iov->iov_len);
2402 skb_put(hb, iov->iov_len);
2403 remaining = len - iov->iov_len;
2405 /* Free the small buffer */
2406 push_rxbufs(card, sb);
2408 /* Copy all large buffers to the huge buffer and free them */
2409 for (j = 1; j < NS_SKB(iovb)->iovcnt; j++)
2411 lb = (struct sk_buff *) iov->iov_base;
2412 tocopy = min_t(int, remaining, iov->iov_len);
2413 skb_copy_from_linear_data(lb, skb_tail_pointer(hb), tocopy);
2414 skb_put(hb, tocopy);
2416 remaining -= tocopy;
2417 push_rxbufs(card, lb);
2420 if (remaining != 0 || hb->len != len)
2421 printk("nicstar%d: Huge buffer len mismatch.\n", card->index);
2422 #endif /* EXTRA_DEBUG */
2423 ATM_SKB(hb)->vcc = vcc;
2424 #ifdef NS_USE_DESTRUCTORS
2425 hb->destructor = ns_hb_destructor;
2426 #endif /* NS_USE_DESTRUCTORS */
2427 __net_timestamp(hb);
2429 atomic_inc(&vcc->stats->rx);
2434 recycle_iov_buf(card, iovb);
2441 #ifdef NS_USE_DESTRUCTORS
2443 static void ns_sb_destructor(struct sk_buff *sb)
2448 card = (ns_dev *) ATM_SKB(sb)->vcc->dev->dev_data;
2449 stat = readl(card->membase + STAT);
2450 card->sbfqc = ns_stat_sfbqc_get(stat);
2451 card->lbfqc = ns_stat_lfbqc_get(stat);
2455 sb = __dev_alloc_skb(NS_SMSKBSIZE, GFP_KERNEL);
2458 NS_SKB_CB(sb)->buf_type = BUF_SM;
2459 skb_queue_tail(&card->sbpool.queue, sb);
2460 skb_reserve(sb, NS_AAL0_HEADER);
2461 push_rxbufs(card, sb);
2462 } while (card->sbfqc < card->sbnr.min);
2467 static void ns_lb_destructor(struct sk_buff *lb)
2472 card = (ns_dev *) ATM_SKB(lb)->vcc->dev->dev_data;
2473 stat = readl(card->membase + STAT);
2474 card->sbfqc = ns_stat_sfbqc_get(stat);
2475 card->lbfqc = ns_stat_lfbqc_get(stat);
2479 lb = __dev_alloc_skb(NS_LGSKBSIZE, GFP_KERNEL);
2482 NS_SKB_CB(lb)->buf_type = BUF_LG;
2483 skb_queue_tail(&card->lbpool.queue, lb);
2484 skb_reserve(lb, NS_SMBUFSIZE);
2485 push_rxbufs(card, lb);
2486 } while (card->lbfqc < card->lbnr.min);
2491 static void ns_hb_destructor(struct sk_buff *hb)
2495 card = (ns_dev *) ATM_SKB(hb)->vcc->dev->dev_data;
2497 while (card->hbpool.count < card->hbnr.init)
2499 hb = __dev_alloc_skb(NS_HBUFSIZE, GFP_KERNEL);
2502 NS_SKB_CB(hb)->buf_type = BUF_NONE;
2503 skb_queue_tail(&card->hbpool.queue, hb);
2504 card->hbpool.count++;
2508 #endif /* NS_USE_DESTRUCTORS */
2511 static void recycle_rx_buf(ns_dev *card, struct sk_buff *skb)
2513 struct ns_skb_cb *cb = NS_SKB_CB(skb);
2515 if (unlikely(cb->buf_type == BUF_NONE)) {
2516 printk("nicstar%d: What kind of rx buffer is this?\n", card->index);
2517 dev_kfree_skb_any(skb);
2519 push_rxbufs(card, skb);
2523 static void recycle_iovec_rx_bufs(ns_dev *card, struct iovec *iov, int count)
2526 recycle_rx_buf(card, (struct sk_buff *) (iov++)->iov_base);
2530 static void recycle_iov_buf(ns_dev *card, struct sk_buff *iovb)
2532 if (card->iovpool.count < card->iovnr.max)
2534 skb_queue_tail(&card->iovpool.queue, iovb);
2535 card->iovpool.count++;
2538 dev_kfree_skb_any(iovb);
2543 static void dequeue_sm_buf(ns_dev *card, struct sk_buff *sb)
2545 skb_unlink(sb, &card->sbpool.queue);
2546 #ifdef NS_USE_DESTRUCTORS
2547 if (card->sbfqc < card->sbnr.min)
2549 if (card->sbfqc < card->sbnr.init)
2551 struct sk_buff *new_sb;
2552 if ((new_sb = dev_alloc_skb(NS_SMSKBSIZE)) != NULL)
2554 NS_SKB_CB(new_sb)->buf_type = BUF_SM;
2555 skb_queue_tail(&card->sbpool.queue, new_sb);
2556 skb_reserve(new_sb, NS_AAL0_HEADER);
2557 push_rxbufs(card, new_sb);
2560 if (card->sbfqc < card->sbnr.init)
2561 #endif /* NS_USE_DESTRUCTORS */
2563 struct sk_buff *new_sb;
2564 if ((new_sb = dev_alloc_skb(NS_SMSKBSIZE)) != NULL)
2566 NS_SKB_CB(new_sb)->buf_type = BUF_SM;
2567 skb_queue_tail(&card->sbpool.queue, new_sb);
2568 skb_reserve(new_sb, NS_AAL0_HEADER);
2569 push_rxbufs(card, new_sb);
2576 static void dequeue_lg_buf(ns_dev *card, struct sk_buff *lb)
2578 skb_unlink(lb, &card->lbpool.queue);
2579 #ifdef NS_USE_DESTRUCTORS
2580 if (card->lbfqc < card->lbnr.min)
2582 if (card->lbfqc < card->lbnr.init)
2584 struct sk_buff *new_lb;
2585 if ((new_lb = dev_alloc_skb(NS_LGSKBSIZE)) != NULL)
2587 NS_SKB_CB(new_lb)->buf_type = BUF_LG;
2588 skb_queue_tail(&card->lbpool.queue, new_lb);
2589 skb_reserve(new_lb, NS_SMBUFSIZE);
2590 push_rxbufs(card, new_lb);
2593 if (card->lbfqc < card->lbnr.init)
2594 #endif /* NS_USE_DESTRUCTORS */
2596 struct sk_buff *new_lb;
2597 if ((new_lb = dev_alloc_skb(NS_LGSKBSIZE)) != NULL)
2599 NS_SKB_CB(new_lb)->buf_type = BUF_LG;
2600 skb_queue_tail(&card->lbpool.queue, new_lb);
2601 skb_reserve(new_lb, NS_SMBUFSIZE);
2602 push_rxbufs(card, new_lb);
2609 static int ns_proc_read(struct atm_dev *dev, loff_t *pos, char *page)
2616 card = (ns_dev *) dev->dev_data;
2617 stat = readl(card->membase + STAT);
2619 return sprintf(page, "Pool count min init max \n");
2621 return sprintf(page, "Small %5d %5d %5d %5d \n",
2622 ns_stat_sfbqc_get(stat), card->sbnr.min, card->sbnr.init,
2625 return sprintf(page, "Large %5d %5d %5d %5d \n",
2626 ns_stat_lfbqc_get(stat), card->lbnr.min, card->lbnr.init,
2629 return sprintf(page, "Huge %5d %5d %5d %5d \n", card->hbpool.count,
2630 card->hbnr.min, card->hbnr.init, card->hbnr.max);
2632 return sprintf(page, "Iovec %5d %5d %5d %5d \n", card->iovpool.count,
2633 card->iovnr.min, card->iovnr.init, card->iovnr.max);
2637 retval = sprintf(page, "Interrupt counter: %u \n", card->intcnt);
2642 /* Dump 25.6 Mbps PHY registers */
2643 /* Now there's a 25.6 Mbps PHY driver this code isn't needed. I left it
2644 here just in case it's needed for debugging. */
2645 if (card->max_pcr == ATM_25_PCR && !left--)
2650 for (i = 0; i < 4; i++)
2652 while (CMD_BUSY(card));
2653 writel(NS_CMD_READ_UTILITY | 0x00000200 | i, card->membase + CMD);
2654 while (CMD_BUSY(card));
2655 phy_regs[i] = readl(card->membase + DR0) & 0x000000FF;
2658 return sprintf(page, "PHY regs: 0x%02X 0x%02X 0x%02X 0x%02X \n",
2659 phy_regs[0], phy_regs[1], phy_regs[2], phy_regs[3]);
2661 #endif /* 0 - Dump 25.6 Mbps PHY registers */
2664 if (left-- < NS_TST_NUM_ENTRIES)
2666 if (card->tste2vc[left + 1] == NULL)
2667 return sprintf(page, "%5d - VBR/UBR \n", left + 1);
2669 return sprintf(page, "%5d - %d %d \n", left + 1,
2670 card->tste2vc[left + 1]->tx_vcc->vpi,
2671 card->tste2vc[left + 1]->tx_vcc->vci);
2679 static int ns_ioctl(struct atm_dev *dev, unsigned int cmd, void __user *arg)
2684 unsigned long flags;
2686 card = dev->dev_data;
2690 if (get_user(pl.buftype, &((pool_levels __user *) arg)->buftype))
2694 case NS_BUFTYPE_SMALL:
2695 pl.count = ns_stat_sfbqc_get(readl(card->membase + STAT));
2696 pl.level.min = card->sbnr.min;
2697 pl.level.init = card->sbnr.init;
2698 pl.level.max = card->sbnr.max;
2701 case NS_BUFTYPE_LARGE:
2702 pl.count = ns_stat_lfbqc_get(readl(card->membase + STAT));
2703 pl.level.min = card->lbnr.min;
2704 pl.level.init = card->lbnr.init;
2705 pl.level.max = card->lbnr.max;
2708 case NS_BUFTYPE_HUGE:
2709 pl.count = card->hbpool.count;
2710 pl.level.min = card->hbnr.min;
2711 pl.level.init = card->hbnr.init;
2712 pl.level.max = card->hbnr.max;
2715 case NS_BUFTYPE_IOVEC:
2716 pl.count = card->iovpool.count;
2717 pl.level.min = card->iovnr.min;
2718 pl.level.init = card->iovnr.init;
2719 pl.level.max = card->iovnr.max;
2723 return -ENOIOCTLCMD;
2726 if (!copy_to_user((pool_levels __user *) arg, &pl, sizeof(pl)))
2727 return (sizeof(pl));
2732 if (!capable(CAP_NET_ADMIN))
2734 if (copy_from_user(&pl, (pool_levels __user *) arg, sizeof(pl)))
2736 if (pl.level.min >= pl.level.init || pl.level.init >= pl.level.max)
2738 if (pl.level.min == 0)
2742 case NS_BUFTYPE_SMALL:
2743 if (pl.level.max > TOP_SB)
2745 card->sbnr.min = pl.level.min;
2746 card->sbnr.init = pl.level.init;
2747 card->sbnr.max = pl.level.max;
2750 case NS_BUFTYPE_LARGE:
2751 if (pl.level.max > TOP_LB)
2753 card->lbnr.min = pl.level.min;
2754 card->lbnr.init = pl.level.init;
2755 card->lbnr.max = pl.level.max;
2758 case NS_BUFTYPE_HUGE:
2759 if (pl.level.max > TOP_HB)
2761 card->hbnr.min = pl.level.min;
2762 card->hbnr.init = pl.level.init;
2763 card->hbnr.max = pl.level.max;
2766 case NS_BUFTYPE_IOVEC:
2767 if (pl.level.max > TOP_IOVB)
2769 card->iovnr.min = pl.level.min;
2770 card->iovnr.init = pl.level.init;
2771 card->iovnr.max = pl.level.max;
2781 if (!capable(CAP_NET_ADMIN))
2783 btype = (long) arg; /* a long is the same size as a pointer or bigger */
2786 case NS_BUFTYPE_SMALL:
2787 while (card->sbfqc < card->sbnr.init)
2791 sb = __dev_alloc_skb(NS_SMSKBSIZE, GFP_KERNEL);
2794 NS_SKB_CB(sb)->buf_type = BUF_SM;
2795 skb_queue_tail(&card->sbpool.queue, sb);
2796 skb_reserve(sb, NS_AAL0_HEADER);
2797 push_rxbufs(card, sb);
2801 case NS_BUFTYPE_LARGE:
2802 while (card->lbfqc < card->lbnr.init)
2806 lb = __dev_alloc_skb(NS_LGSKBSIZE, GFP_KERNEL);
2809 NS_SKB_CB(lb)->buf_type = BUF_LG;
2810 skb_queue_tail(&card->lbpool.queue, lb);
2811 skb_reserve(lb, NS_SMBUFSIZE);
2812 push_rxbufs(card, lb);
2816 case NS_BUFTYPE_HUGE:
2817 while (card->hbpool.count > card->hbnr.init)
2821 spin_lock_irqsave(&card->int_lock, flags);
2822 hb = skb_dequeue(&card->hbpool.queue);
2823 card->hbpool.count--;
2824 spin_unlock_irqrestore(&card->int_lock, flags);
2826 printk("nicstar%d: huge buffer count inconsistent.\n",
2829 dev_kfree_skb_any(hb);
2832 while (card->hbpool.count < card->hbnr.init)
2836 hb = __dev_alloc_skb(NS_HBUFSIZE, GFP_KERNEL);
2839 NS_SKB_CB(hb)->buf_type = BUF_NONE;
2840 spin_lock_irqsave(&card->int_lock, flags);
2841 skb_queue_tail(&card->hbpool.queue, hb);
2842 card->hbpool.count++;
2843 spin_unlock_irqrestore(&card->int_lock, flags);
2847 case NS_BUFTYPE_IOVEC:
2848 while (card->iovpool.count > card->iovnr.init)
2850 struct sk_buff *iovb;
2852 spin_lock_irqsave(&card->int_lock, flags);
2853 iovb = skb_dequeue(&card->iovpool.queue);
2854 card->iovpool.count--;
2855 spin_unlock_irqrestore(&card->int_lock, flags);
2857 printk("nicstar%d: iovec buffer count inconsistent.\n",
2860 dev_kfree_skb_any(iovb);
2863 while (card->iovpool.count < card->iovnr.init)
2865 struct sk_buff *iovb;
2867 iovb = alloc_skb(NS_IOVBUFSIZE, GFP_KERNEL);
2870 NS_SKB_CB(iovb)->buf_type = BUF_NONE;
2871 spin_lock_irqsave(&card->int_lock, flags);
2872 skb_queue_tail(&card->iovpool.queue, iovb);
2873 card->iovpool.count++;
2874 spin_unlock_irqrestore(&card->int_lock, flags);
2885 if (dev->phy && dev->phy->ioctl) {
2886 return dev->phy->ioctl(dev, cmd, arg);
2889 printk("nicstar%d: %s == NULL \n", card->index,
2890 dev->phy ? "dev->phy->ioctl" : "dev->phy");
2891 return -ENOIOCTLCMD;
2897 static void which_list(ns_dev *card, struct sk_buff *skb)
2899 printk("skb buf_type: 0x%08x\n", NS_SKB_CB(skb)->buf_type);
2903 static void ns_poll(unsigned long arg)
2907 unsigned long flags;
2910 PRINTK("nicstar: Entering ns_poll().\n");
2911 for (i = 0; i < num_cards; i++)
2914 if (spin_is_locked(&card->int_lock)) {
2915 /* Probably it isn't worth spinning */
2918 spin_lock_irqsave(&card->int_lock, flags);
2921 stat_r = readl(card->membase + STAT);
2922 if (stat_r & NS_STAT_TSIF)
2923 stat_w |= NS_STAT_TSIF;
2924 if (stat_r & NS_STAT_EOPDU)
2925 stat_w |= NS_STAT_EOPDU;
2930 writel(stat_w, card->membase + STAT);
2931 spin_unlock_irqrestore(&card->int_lock, flags);
2933 mod_timer(&ns_timer, jiffies + NS_POLL_PERIOD);
2934 PRINTK("nicstar: Leaving ns_poll().\n");
2939 static int ns_parse_mac(char *mac, unsigned char *esi)
2944 if (mac == NULL || esi == NULL)
2947 for (i = 0; i < 6; i++)
2949 if ((byte1 = ns_h2i(mac[j++])) < 0)
2951 if ((byte0 = ns_h2i(mac[j++])) < 0)
2953 esi[i] = (unsigned char) (byte1 * 16 + byte0);
2956 if (mac[j++] != ':')
2965 static short ns_h2i(char c)
2967 if (c >= '0' && c <= '9')
2968 return (short) (c - '0');
2969 if (c >= 'A' && c <= 'F')
2970 return (short) (c - 'A' + 10);
2971 if (c >= 'a' && c <= 'f')
2972 return (short) (c - 'a' + 10);
2978 static void ns_phy_put(struct atm_dev *dev, unsigned char value,
2982 unsigned long flags;
2984 card = dev->dev_data;
2985 spin_lock_irqsave(&card->res_lock, flags);
2986 while(CMD_BUSY(card));
2987 writel((unsigned long) value, card->membase + DR0);
2988 writel(NS_CMD_WRITE_UTILITY | 0x00000200 | (addr & 0x000000FF),
2989 card->membase + CMD);
2990 spin_unlock_irqrestore(&card->res_lock, flags);
2995 static unsigned char ns_phy_get(struct atm_dev *dev, unsigned long addr)
2998 unsigned long flags;
3001 card = dev->dev_data;
3002 spin_lock_irqsave(&card->res_lock, flags);
3003 while(CMD_BUSY(card));
3004 writel(NS_CMD_READ_UTILITY | 0x00000200 | (addr & 0x000000FF),
3005 card->membase + CMD);
3006 while(CMD_BUSY(card));
3007 data = readl(card->membase + DR0) & 0x000000FF;
3008 spin_unlock_irqrestore(&card->res_lock, flags);
3009 return (unsigned char) data;
3014 module_init(nicstar_init);
3015 module_exit(nicstar_cleanup);