Linux Audio

Check our new training course

Loading...
v5.9
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/* lanai.c -- Copyright 1999-2003 by Mitchell Blank Jr <mitch@sfgoth.com>
   3 *
 
 
 
 
 
   4 * This driver supports ATM cards based on the Efficient "Lanai"
   5 * chipset such as the Speedstream 3010 and the ENI-25p.  The
   6 * Speedstream 3060 is currently not supported since we don't
   7 * have the code to drive the on-board Alcatel DSL chipset (yet).
   8 *
   9 * Thanks to Efficient for supporting this project with hardware,
  10 * documentation, and by answering my questions.
  11 *
  12 * Things not working yet:
  13 *
  14 * o  We don't support the Speedstream 3060 yet - this card has
  15 *    an on-board DSL modem chip by Alcatel and the driver will
  16 *    need some extra code added to handle it
  17 *
  18 * o  Note that due to limitations of the Lanai only one VCC can be
  19 *    in CBR at once
  20 *
  21 * o We don't currently parse the EEPROM at all.  The code is all
  22 *   there as per the spec, but it doesn't actually work.  I think
  23 *   there may be some issues with the docs.  Anyway, do NOT
  24 *   enable it yet - bugs in that code may actually damage your
  25 *   hardware!  Because of this you should hardware an ESI before
  26 *   trying to use this in a LANE or MPOA environment.
  27 *
  28 * o  AAL0 is stubbed in but the actual rx/tx path isn't written yet:
  29 *	vcc_tx_aal0() needs to send or queue a SKB
  30 *	vcc_tx_unqueue_aal0() needs to attempt to send queued SKBs
  31 *	vcc_rx_aal0() needs to handle AAL0 interrupts
  32 *    This isn't too much work - I just wanted to get other things
  33 *    done first.
  34 *
  35 * o  lanai_change_qos() isn't written yet
  36 *
  37 * o  There aren't any ioctl's yet -- I'd like to eventually support
  38 *    setting loopback and LED modes that way.
  39 *
  40 * o  If the segmentation engine or DMA gets shut down we should restart
  41 *    card as per section 17.0i.  (see lanai_reset)
  42 *
  43 * o setsockopt(SO_CIRANGE) isn't done (although despite what the
  44 *   API says it isn't exactly commonly implemented)
  45 */
  46
  47/* Version history:
  48 *   v.1.00 -- 26-JUL-2003 -- PCI/DMA updates
  49 *   v.0.02 -- 11-JAN-2000 -- Endian fixes
  50 *   v.0.01 -- 30-NOV-1999 -- Initial release
  51 */
  52
  53#include <linux/module.h>
  54#include <linux/slab.h>
  55#include <linux/mm.h>
  56#include <linux/atmdev.h>
  57#include <asm/io.h>
  58#include <asm/byteorder.h>
  59#include <linux/spinlock.h>
  60#include <linux/pci.h>
  61#include <linux/dma-mapping.h>
  62#include <linux/init.h>
  63#include <linux/delay.h>
  64#include <linux/interrupt.h>
  65
  66/* -------------------- TUNABLE PARAMATERS: */
  67
  68/*
  69 * Maximum number of VCIs per card.  Setting it lower could theoretically
  70 * save some memory, but since we allocate our vcc list with get_free_pages,
  71 * it's not really likely for most architectures
  72 */
  73#define NUM_VCI			(1024)
  74
  75/*
  76 * Enable extra debugging
  77 */
  78#define DEBUG
  79/*
  80 * Debug _all_ register operations with card, except the memory test.
  81 * Also disables the timed poll to prevent extra chattiness.  This
  82 * isn't for normal use
  83 */
  84#undef DEBUG_RW
  85
  86/*
  87 * The programming guide specifies a full test of the on-board SRAM
  88 * at initialization time.  Undefine to remove this
  89 */
  90#define FULL_MEMORY_TEST
  91
  92/*
  93 * This is the number of (4 byte) service entries that we will
  94 * try to allocate at startup.  Note that we will end up with
  95 * one PAGE_SIZE's worth regardless of what this is set to
  96 */
  97#define SERVICE_ENTRIES		(1024)
  98/* TODO: make above a module load-time option */
  99
 100/*
 101 * We normally read the onboard EEPROM in order to discover our MAC
 102 * address.  Undefine to _not_ do this
 103 */
 104/* #define READ_EEPROM */ /* ***DONT ENABLE YET*** */
 105/* TODO: make above a module load-time option (also) */
 106
 107/*
 108 * Depth of TX fifo (in 128 byte units; range 2-31)
 109 * Smaller numbers are better for network latency
 110 * Larger numbers are better for PCI latency
 111 * I'm really sure where the best tradeoff is, but the BSD driver uses
 112 * 7 and it seems to work ok.
 113 */
 114#define TX_FIFO_DEPTH		(7)
 115/* TODO: make above a module load-time option */
 116
 117/*
 118 * How often (in jiffies) we will try to unstick stuck connections -
 119 * shouldn't need to happen much
 120 */
 121#define LANAI_POLL_PERIOD	(10*HZ)
 122/* TODO: make above a module load-time option */
 123
 124/*
 125 * When allocating an AAL5 receiving buffer, try to make it at least
 126 * large enough to hold this many max_sdu sized PDUs
 127 */
 128#define AAL5_RX_MULTIPLIER	(3)
 129/* TODO: make above a module load-time option */
 130
 131/*
 132 * Same for transmitting buffer
 133 */
 134#define AAL5_TX_MULTIPLIER	(3)
 135/* TODO: make above a module load-time option */
 136
 137/*
 138 * When allocating an AAL0 transmiting buffer, how many cells should fit.
 139 * Remember we'll end up with a PAGE_SIZE of them anyway, so this isn't
 140 * really critical
 141 */
 142#define AAL0_TX_MULTIPLIER	(40)
 143/* TODO: make above a module load-time option */
 144
 145/*
 146 * How large should we make the AAL0 receiving buffer.  Remember that this
 147 * is shared between all AAL0 VC's
 148 */
 149#define AAL0_RX_BUFFER_SIZE	(PAGE_SIZE)
 150/* TODO: make above a module load-time option */
 151
 152/*
 153 * Should we use Lanai's "powerdown" feature when no vcc's are bound?
 154 */
 155/* #define USE_POWERDOWN */
 156/* TODO: make above a module load-time option (also) */
 157
 158/* -------------------- DEBUGGING AIDS: */
 159
 160#define DEV_LABEL "lanai"
 161
 162#ifdef DEBUG
 163
 164#define DPRINTK(format, args...) \
 165	printk(KERN_DEBUG DEV_LABEL ": " format, ##args)
 166#define APRINTK(truth, format, args...) \
 167	do { \
 168		if (unlikely(!(truth))) \
 169			printk(KERN_ERR DEV_LABEL ": " format, ##args); \
 170	} while (0)
 171
 172#else /* !DEBUG */
 173
 174#define DPRINTK(format, args...)
 175#define APRINTK(truth, format, args...)
 176
 177#endif /* DEBUG */
 178
 179#ifdef DEBUG_RW
 180#define RWDEBUG(format, args...) \
 181	printk(KERN_DEBUG DEV_LABEL ": " format, ##args)
 182#else /* !DEBUG_RW */
 183#define RWDEBUG(format, args...)
 184#endif
 185
 186/* -------------------- DATA DEFINITIONS: */
 187
 188#define LANAI_MAPPING_SIZE	(0x40000)
 189#define LANAI_EEPROM_SIZE	(128)
 190
 191typedef int vci_t;
 192typedef void __iomem *bus_addr_t;
 193
 194/* DMA buffer in host memory for TX, RX, or service list. */
 195struct lanai_buffer {
 196	u32 *start;	/* From get_free_pages */
 197	u32 *end;	/* One past last byte */
 198	u32 *ptr;	/* Pointer to current host location */
 199	dma_addr_t dmaaddr;
 200};
 201
 202struct lanai_vcc_stats {
 203	unsigned rx_nomem;
 204	union {
 205		struct {
 206			unsigned rx_badlen;
 207			unsigned service_trash;
 208			unsigned service_stream;
 209			unsigned service_rxcrc;
 210		} aal5;
 211		struct {
 212		} aal0;
 213	} x;
 214};
 215
 216struct lanai_dev;			/* Forward declaration */
 217
 218/*
 219 * This is the card-specific per-vcc data.  Note that unlike some other
 220 * drivers there is NOT a 1-to-1 correspondance between these and
 221 * atm_vcc's - each one of these represents an actual 2-way vcc, but
 222 * an atm_vcc can be 1-way and share with a 1-way vcc in the other
 223 * direction.  To make it weirder, there can even be 0-way vccs
 224 * bound to us, waiting to do a change_qos
 225 */
 226struct lanai_vcc {
 227	bus_addr_t vbase;		/* Base of VCC's registers */
 228	struct lanai_vcc_stats stats;
 229	int nref;			/* # of atm_vcc's who reference us */
 230	vci_t vci;
 231	struct {
 232		struct lanai_buffer buf;
 233		struct atm_vcc *atmvcc;	/* atm_vcc who is receiver */
 234	} rx;
 235	struct {
 236		struct lanai_buffer buf;
 237		struct atm_vcc *atmvcc;	/* atm_vcc who is transmitter */
 238		int endptr;		/* last endptr from service entry */
 239		struct sk_buff_head backlog;
 240		void (*unqueue)(struct lanai_dev *, struct lanai_vcc *, int);
 241	} tx;
 242};
 243
 244enum lanai_type {
 245	lanai2	= PCI_DEVICE_ID_EF_ATM_LANAI2,
 246	lanaihb	= PCI_DEVICE_ID_EF_ATM_LANAIHB
 247};
 248
 249struct lanai_dev_stats {
 250	unsigned ovfl_trash;	/* # of cells dropped - buffer overflow */
 251	unsigned vci_trash;	/* # of cells dropped - closed vci */
 252	unsigned hec_err;	/* # of cells dropped - bad HEC */
 253	unsigned atm_ovfl;	/* # of cells dropped - rx fifo overflow */
 254	unsigned pcierr_parity_detect;
 255	unsigned pcierr_serr_set;
 256	unsigned pcierr_master_abort;
 257	unsigned pcierr_m_target_abort;
 258	unsigned pcierr_s_target_abort;
 259	unsigned pcierr_master_parity;
 260	unsigned service_notx;
 261	unsigned service_norx;
 262	unsigned service_rxnotaal5;
 263	unsigned dma_reenable;
 264	unsigned card_reset;
 265};
 266
 267struct lanai_dev {
 268	bus_addr_t base;
 269	struct lanai_dev_stats stats;
 270	struct lanai_buffer service;
 271	struct lanai_vcc **vccs;
 272#ifdef USE_POWERDOWN
 273	int nbound;			/* number of bound vccs */
 274#endif
 275	enum lanai_type type;
 276	vci_t num_vci;			/* Currently just NUM_VCI */
 277	u8 eeprom[LANAI_EEPROM_SIZE];
 278	u32 serialno, magicno;
 279	struct pci_dev *pci;
 280	DECLARE_BITMAP(backlog_vccs, NUM_VCI);   /* VCCs with tx backlog */
 281	DECLARE_BITMAP(transmit_ready, NUM_VCI); /* VCCs with transmit space */
 282	struct timer_list timer;
 283	int naal0;
 284	struct lanai_buffer aal0buf;	/* AAL0 RX buffers */
 285	u32 conf1, conf2;		/* CONFIG[12] registers */
 286	u32 status;			/* STATUS register */
 287	spinlock_t endtxlock;
 288	spinlock_t servicelock;
 289	struct atm_vcc *cbrvcc;
 290	int number;
 291	int board_rev;
 292/* TODO - look at race conditions with maintence of conf1/conf2 */
 293/* TODO - transmit locking: should we use _irq not _irqsave? */
 294/* TODO - organize above in some rational fashion (see <asm/cache.h>) */
 295};
 296
 297/*
 298 * Each device has two bitmaps for each VCC (baclog_vccs and transmit_ready)
 299 * This function iterates one of these, calling a given function for each
 300 * vci with their bit set
 301 */
 302static void vci_bitfield_iterate(struct lanai_dev *lanai,
 303	const unsigned long *lp,
 304	void (*func)(struct lanai_dev *,vci_t vci))
 305{
 306	vci_t vci;
 307
 308	for_each_set_bit(vci, lp, NUM_VCI)
 309		func(lanai, vci);
 310}
 311
 312/* -------------------- BUFFER  UTILITIES: */
 313
 314/*
 315 * Lanai needs DMA buffers aligned to 256 bytes of at least 1024 bytes -
 316 * usually any page allocation will do.  Just to be safe in case
 317 * PAGE_SIZE is insanely tiny, though...
 318 */
 319#define LANAI_PAGE_SIZE   ((PAGE_SIZE >= 1024) ? PAGE_SIZE : 1024)
 320
 321/*
 322 * Allocate a buffer in host RAM for service list, RX, or TX
 323 * Returns buf->start==NULL if no memory
 324 * Note that the size will be rounded up 2^n bytes, and
 325 * if we can't allocate that we'll settle for something smaller
 326 * until minbytes
 327 */
 328static void lanai_buf_allocate(struct lanai_buffer *buf,
 329	size_t bytes, size_t minbytes, struct pci_dev *pci)
 330{
 331	int size;
 332
 333	if (bytes > (128 * 1024))	/* max lanai buffer size */
 334		bytes = 128 * 1024;
 335	for (size = LANAI_PAGE_SIZE; size < bytes; size *= 2)
 336		;
 337	if (minbytes < LANAI_PAGE_SIZE)
 338		minbytes = LANAI_PAGE_SIZE;
 339	do {
 340		/*
 341		 * Technically we could use non-consistent mappings for
 342		 * everything, but the way the lanai uses DMA memory would
 343		 * make that a terrific pain.  This is much simpler.
 344		 */
 345		buf->start = dma_alloc_coherent(&pci->dev,
 346						size, &buf->dmaaddr, GFP_KERNEL);
 347		if (buf->start != NULL) {	/* Success */
 348			/* Lanai requires 256-byte alignment of DMA bufs */
 349			APRINTK((buf->dmaaddr & ~0xFFFFFF00) == 0,
 350			    "bad dmaaddr: 0x%lx\n",
 351			    (unsigned long) buf->dmaaddr);
 352			buf->ptr = buf->start;
 353			buf->end = (u32 *)
 354			    (&((unsigned char *) buf->start)[size]);
 355			memset(buf->start, 0, size);
 356			break;
 357		}
 358		size /= 2;
 359	} while (size >= minbytes);
 360}
 361
 362/* size of buffer in bytes */
 363static inline size_t lanai_buf_size(const struct lanai_buffer *buf)
 364{
 365	return ((unsigned long) buf->end) - ((unsigned long) buf->start);
 366}
 367
 368static void lanai_buf_deallocate(struct lanai_buffer *buf,
 369	struct pci_dev *pci)
 370{
 371	if (buf->start != NULL) {
 372		dma_free_coherent(&pci->dev, lanai_buf_size(buf),
 373				  buf->start, buf->dmaaddr);
 374		buf->start = buf->end = buf->ptr = NULL;
 375	}
 376}
 377
 378/* size of buffer as "card order" (0=1k .. 7=128k) */
 379static int lanai_buf_size_cardorder(const struct lanai_buffer *buf)
 380{
 381	int order = get_order(lanai_buf_size(buf)) + (PAGE_SHIFT - 10);
 382
 383	/* This can only happen if PAGE_SIZE is gigantic, but just in case */
 384	if (order > 7)
 385		order = 7;
 386	return order;
 387}
 388
 389/* -------------------- PORT I/O UTILITIES: */
 390
 391/* Registers (and their bit-fields) */
 392enum lanai_register {
 393	Reset_Reg		= 0x00,	/* Reset; read for chip type; bits: */
 394#define   RESET_GET_BOARD_REV(x)    (((x)>> 0)&0x03)	/* Board revision */
 395#define   RESET_GET_BOARD_ID(x)	    (((x)>> 2)&0x03)	/* Board ID */
 396#define     BOARD_ID_LANAI256		(0)	/* 25.6M adapter card */
 397	Endian_Reg		= 0x04,	/* Endian setting */
 398	IntStatus_Reg		= 0x08,	/* Interrupt status */
 399	IntStatusMasked_Reg	= 0x0C,	/* Interrupt status (masked) */
 400	IntAck_Reg		= 0x10,	/* Interrupt acknowledge */
 401	IntAckMasked_Reg	= 0x14,	/* Interrupt acknowledge (masked) */
 402	IntStatusSet_Reg	= 0x18,	/* Get status + enable/disable */
 403	IntStatusSetMasked_Reg	= 0x1C,	/* Get status + en/di (masked) */
 404	IntControlEna_Reg	= 0x20,	/* Interrupt control enable */
 405	IntControlDis_Reg	= 0x24,	/* Interrupt control disable */
 406	Status_Reg		= 0x28,	/* Status */
 407#define   STATUS_PROMDATA	 (0x00000001)	/* PROM_DATA pin */
 408#define   STATUS_WAITING	 (0x00000002)	/* Interrupt being delayed */
 409#define	  STATUS_SOOL		 (0x00000004)	/* SOOL alarm */
 410#define   STATUS_LOCD		 (0x00000008)	/* LOCD alarm */
 411#define	  STATUS_LED		 (0x00000010)	/* LED (HAPPI) output */
 412#define   STATUS_GPIN		 (0x00000020)	/* GPIN pin */
 413#define   STATUS_BUTTBUSY	 (0x00000040)	/* Butt register is pending */
 414	Config1_Reg		= 0x2C,	/* Config word 1; bits: */
 415#define   CONFIG1_PROMDATA	 (0x00000001)	/* PROM_DATA pin */
 416#define   CONFIG1_PROMCLK	 (0x00000002)	/* PROM_CLK pin */
 417#define   CONFIG1_SET_READMODE(x) ((x)*0x004)	/* PCI BM reads; values: */
 418#define     READMODE_PLAIN	    (0)		/*   Plain memory read */
 419#define     READMODE_LINE	    (2)		/*   Memory read line */
 420#define     READMODE_MULTIPLE	    (3)		/*   Memory read multiple */
 421#define   CONFIG1_DMA_ENABLE	 (0x00000010)	/* Turn on DMA */
 422#define   CONFIG1_POWERDOWN	 (0x00000020)	/* Turn off clocks */
 423#define   CONFIG1_SET_LOOPMODE(x) ((x)*0x080)	/* Clock&loop mode; values: */
 424#define     LOOPMODE_NORMAL	    (0)		/*   Normal - no loop */
 425#define     LOOPMODE_TIME	    (1)
 426#define     LOOPMODE_DIAG	    (2)
 427#define     LOOPMODE_LINE	    (3)
 428#define   CONFIG1_MASK_LOOPMODE  (0x00000180)
 429#define   CONFIG1_SET_LEDMODE(x) ((x)*0x0200)	/* Mode of LED; values: */
 430#define     LEDMODE_NOT_SOOL	    (0)		/*   !SOOL */
 431#define	    LEDMODE_OFF		    (1)		/*   0     */
 432#define	    LEDMODE_ON		    (2)		/*   1     */
 433#define	    LEDMODE_NOT_LOCD	    (3)		/*   !LOCD */
 434#define	    LEDMORE_GPIN	    (4)		/*   GPIN  */
 435#define     LEDMODE_NOT_GPIN	    (7)		/*   !GPIN */
 436#define   CONFIG1_MASK_LEDMODE	 (0x00000E00)
 437#define   CONFIG1_GPOUT1	 (0x00001000)	/* Toggle for reset */
 438#define   CONFIG1_GPOUT2	 (0x00002000)	/* Loopback PHY */
 439#define   CONFIG1_GPOUT3	 (0x00004000)	/* Loopback lanai */
 440	Config2_Reg		= 0x30,	/* Config word 2; bits: */
 441#define   CONFIG2_HOWMANY	 (0x00000001)	/* >512 VCIs? */
 442#define   CONFIG2_PTI7_MODE	 (0x00000002)	/* Make PTI=7 RM, not OAM */
 443#define   CONFIG2_VPI_CHK_DIS	 (0x00000004)	/* Ignore RX VPI value */
 444#define   CONFIG2_HEC_DROP	 (0x00000008)	/* Drop cells w/ HEC errors */
 445#define   CONFIG2_VCI0_NORMAL	 (0x00000010)	/* Treat VCI=0 normally */
 446#define   CONFIG2_CBR_ENABLE	 (0x00000020)	/* Deal with CBR traffic */
 447#define   CONFIG2_TRASH_ALL	 (0x00000040)	/* Trashing incoming cells */
 448#define   CONFIG2_TX_DISABLE	 (0x00000080)	/* Trashing outgoing cells */
 449#define   CONFIG2_SET_TRASH	 (0x00000100)	/* Turn trashing on */
 450	Statistics_Reg		= 0x34,	/* Statistics; bits: */
 451#define   STATS_GET_FIFO_OVFL(x)    (((x)>> 0)&0xFF)	/* FIFO overflowed */
 452#define   STATS_GET_HEC_ERR(x)      (((x)>> 8)&0xFF)	/* HEC was bad */
 453#define   STATS_GET_BAD_VCI(x)      (((x)>>16)&0xFF)	/* VCI not open */
 454#define   STATS_GET_BUF_OVFL(x)     (((x)>>24)&0xFF)	/* VCC buffer full */
 455	ServiceStuff_Reg	= 0x38,	/* Service stuff; bits: */
 456#define   SSTUFF_SET_SIZE(x) ((x)*0x20000000)	/* size of service buffer */
 457#define   SSTUFF_SET_ADDR(x)	    ((x)>>8)	/* set address of buffer */
 458	ServWrite_Reg		= 0x3C,	/* ServWrite Pointer */
 459	ServRead_Reg		= 0x40,	/* ServRead Pointer */
 460	TxDepth_Reg		= 0x44,	/* FIFO Transmit Depth */
 461	Butt_Reg		= 0x48,	/* Butt register */
 462	CBR_ICG_Reg		= 0x50,
 463	CBR_PTR_Reg		= 0x54,
 464	PingCount_Reg		= 0x58,	/* Ping count */
 465	DMA_Addr_Reg		= 0x5C	/* DMA address */
 466};
 467
 468static inline bus_addr_t reg_addr(const struct lanai_dev *lanai,
 469	enum lanai_register reg)
 470{
 471	return lanai->base + reg;
 472}
 473
 474static inline u32 reg_read(const struct lanai_dev *lanai,
 475	enum lanai_register reg)
 476{
 477	u32 t;
 478	t = readl(reg_addr(lanai, reg));
 479	RWDEBUG("R [0x%08X] 0x%02X = 0x%08X\n", (unsigned int) lanai->base,
 480	    (int) reg, t);
 481	return t;
 482}
 483
 484static inline void reg_write(const struct lanai_dev *lanai, u32 val,
 485	enum lanai_register reg)
 486{
 487	RWDEBUG("W [0x%08X] 0x%02X < 0x%08X\n", (unsigned int) lanai->base,
 488	    (int) reg, val);
 489	writel(val, reg_addr(lanai, reg));
 490}
 491
 492static inline void conf1_write(const struct lanai_dev *lanai)
 493{
 494	reg_write(lanai, lanai->conf1, Config1_Reg);
 495}
 496
 497static inline void conf2_write(const struct lanai_dev *lanai)
 498{
 499	reg_write(lanai, lanai->conf2, Config2_Reg);
 500}
 501
 502/* Same as conf2_write(), but defers I/O if we're powered down */
 503static inline void conf2_write_if_powerup(const struct lanai_dev *lanai)
 504{
 505#ifdef USE_POWERDOWN
 506	if (unlikely((lanai->conf1 & CONFIG1_POWERDOWN) != 0))
 507		return;
 508#endif /* USE_POWERDOWN */
 509	conf2_write(lanai);
 510}
 511
 512static inline void reset_board(const struct lanai_dev *lanai)
 513{
 514	DPRINTK("about to reset board\n");
 515	reg_write(lanai, 0, Reset_Reg);
 516	/*
 517	 * If we don't delay a little while here then we can end up
 518	 * leaving the card in a VERY weird state and lock up the
 519	 * PCI bus.  This isn't documented anywhere but I've convinced
 520	 * myself after a lot of painful experimentation
 521	 */
 522	udelay(5);
 523}
 524
 525/* -------------------- CARD SRAM UTILITIES: */
 526
 527/* The SRAM is mapped into normal PCI memory space - the only catch is
 528 * that it is only 16-bits wide but must be accessed as 32-bit.  The
 529 * 16 high bits will be zero.  We don't hide this, since they get
 530 * programmed mostly like discrete registers anyway
 531 */
 532#define SRAM_START (0x20000)
 533#define SRAM_BYTES (0x20000)	/* Again, half don't really exist */
 534
 535static inline bus_addr_t sram_addr(const struct lanai_dev *lanai, int offset)
 536{
 537	return lanai->base + SRAM_START + offset;
 538}
 539
 540static inline u32 sram_read(const struct lanai_dev *lanai, int offset)
 541{
 542	return readl(sram_addr(lanai, offset));
 543}
 544
 545static inline void sram_write(const struct lanai_dev *lanai,
 546	u32 val, int offset)
 547{
 548	writel(val, sram_addr(lanai, offset));
 549}
 550
 551static int sram_test_word(const struct lanai_dev *lanai, int offset,
 552			  u32 pattern)
 553{
 554	u32 readback;
 555	sram_write(lanai, pattern, offset);
 556	readback = sram_read(lanai, offset);
 557	if (likely(readback == pattern))
 558		return 0;
 559	printk(KERN_ERR DEV_LABEL
 560	    "(itf %d): SRAM word at %d bad: wrote 0x%X, read 0x%X\n",
 561	    lanai->number, offset,
 562	    (unsigned int) pattern, (unsigned int) readback);
 563	return -EIO;
 564}
 565
 566static int sram_test_pass(const struct lanai_dev *lanai, u32 pattern)
 567{
 568	int offset, result = 0;
 569	for (offset = 0; offset < SRAM_BYTES && result == 0; offset += 4)
 570		result = sram_test_word(lanai, offset, pattern);
 571	return result;
 572}
 573
 574static int sram_test_and_clear(const struct lanai_dev *lanai)
 575{
 576#ifdef FULL_MEMORY_TEST
 577	int result;
 578	DPRINTK("testing SRAM\n");
 579	if ((result = sram_test_pass(lanai, 0x5555)) != 0)
 580		return result;
 581	if ((result = sram_test_pass(lanai, 0xAAAA)) != 0)
 582		return result;
 583#endif
 584	DPRINTK("clearing SRAM\n");
 585	return sram_test_pass(lanai, 0x0000);
 586}
 587
 588/* -------------------- CARD-BASED VCC TABLE UTILITIES: */
 589
 590/* vcc table */
 591enum lanai_vcc_offset {
 592	vcc_rxaddr1		= 0x00,	/* Location1, plus bits: */
 593#define   RXADDR1_SET_SIZE(x) ((x)*0x0000100)	/* size of RX buffer */
 594#define   RXADDR1_SET_RMMODE(x) ((x)*0x00800)	/* RM cell action; values: */
 595#define     RMMODE_TRASH	  (0)		/*   discard */
 596#define     RMMODE_PRESERVE	  (1)		/*   input as AAL0 */
 597#define     RMMODE_PIPE		  (2)		/*   pipe to coscheduler */
 598#define     RMMODE_PIPEALL	  (3)		/*   pipe non-RM too */
 599#define   RXADDR1_OAM_PRESERVE	 (0x00002000)	/* Input OAM cells as AAL0 */
 600#define   RXADDR1_SET_MODE(x) ((x)*0x0004000)	/* Reassembly mode */
 601#define     RXMODE_TRASH	  (0)		/*   discard */
 602#define     RXMODE_AAL0		  (1)		/*   non-AAL5 mode */
 603#define     RXMODE_AAL5		  (2)		/*   AAL5, intr. each PDU */
 604#define     RXMODE_AAL5_STREAM	  (3)		/*   AAL5 w/o per-PDU intr */
 605	vcc_rxaddr2		= 0x04,	/* Location2 */
 606	vcc_rxcrc1		= 0x08,	/* RX CRC claculation space */
 607	vcc_rxcrc2		= 0x0C,
 608	vcc_rxwriteptr		= 0x10, /* RX writeptr, plus bits: */
 609#define   RXWRITEPTR_LASTEFCI	 (0x00002000)	/* Last PDU had EFCI bit */
 610#define   RXWRITEPTR_DROPPING	 (0x00004000)	/* Had error, dropping */
 611#define   RXWRITEPTR_TRASHING	 (0x00008000)	/* Trashing */
 612	vcc_rxbufstart		= 0x14,	/* RX bufstart, plus bits: */
 613#define   RXBUFSTART_CLP	 (0x00004000)
 614#define   RXBUFSTART_CI		 (0x00008000)
 615	vcc_rxreadptr		= 0x18,	/* RX readptr */
 616	vcc_txicg		= 0x1C, /* TX ICG */
 617	vcc_txaddr1		= 0x20,	/* Location1, plus bits: */
 618#define   TXADDR1_SET_SIZE(x) ((x)*0x0000100)	/* size of TX buffer */
 619#define   TXADDR1_ABR		 (0x00008000)	/* use ABR (doesn't work) */
 620	vcc_txaddr2		= 0x24,	/* Location2 */
 621	vcc_txcrc1		= 0x28,	/* TX CRC claculation space */
 622	vcc_txcrc2		= 0x2C,
 623	vcc_txreadptr		= 0x30, /* TX Readptr, plus bits: */
 624#define   TXREADPTR_GET_PTR(x) ((x)&0x01FFF)
 625#define   TXREADPTR_MASK_DELTA	(0x0000E000)	/* ? */
 626	vcc_txendptr		= 0x34, /* TX Endptr, plus bits: */
 627#define   TXENDPTR_CLP		(0x00002000)
 628#define   TXENDPTR_MASK_PDUMODE	(0x0000C000)	/* PDU mode; values: */
 629#define     PDUMODE_AAL0	 (0*0x04000)
 630#define     PDUMODE_AAL5	 (2*0x04000)
 631#define     PDUMODE_AAL5STREAM	 (3*0x04000)
 632	vcc_txwriteptr		= 0x38,	/* TX Writeptr */
 633#define   TXWRITEPTR_GET_PTR(x) ((x)&0x1FFF)
 634	vcc_txcbr_next		= 0x3C	/* # of next CBR VCI in ring */
 635#define   TXCBR_NEXT_BOZO	(0x00008000)	/* "bozo bit" */
 636};
 637
 638#define CARDVCC_SIZE	(0x40)
 639
 640static inline bus_addr_t cardvcc_addr(const struct lanai_dev *lanai,
 641	vci_t vci)
 642{
 643	return sram_addr(lanai, vci * CARDVCC_SIZE);
 644}
 645
 646static inline u32 cardvcc_read(const struct lanai_vcc *lvcc,
 647	enum lanai_vcc_offset offset)
 648{
 649	u32 val;
 650	APRINTK(lvcc->vbase != NULL, "cardvcc_read: unbound vcc!\n");
 651	val= readl(lvcc->vbase + offset);
 652	RWDEBUG("VR vci=%04d 0x%02X = 0x%08X\n",
 653	    lvcc->vci, (int) offset, val);
 654	return val;
 655}
 656
 657static inline void cardvcc_write(const struct lanai_vcc *lvcc,
 658	u32 val, enum lanai_vcc_offset offset)
 659{
 660	APRINTK(lvcc->vbase != NULL, "cardvcc_write: unbound vcc!\n");
 661	APRINTK((val & ~0xFFFF) == 0,
 662	    "cardvcc_write: bad val 0x%X (vci=%d, addr=0x%02X)\n",
 663	    (unsigned int) val, lvcc->vci, (unsigned int) offset);
 664	RWDEBUG("VW vci=%04d 0x%02X > 0x%08X\n",
 665	    lvcc->vci, (unsigned int) offset, (unsigned int) val);
 666	writel(val, lvcc->vbase + offset);
 667}
 668
 669/* -------------------- COMPUTE SIZE OF AN AAL5 PDU: */
 670
 671/* How many bytes will an AAL5 PDU take to transmit - remember that:
 672 *   o  we need to add 8 bytes for length, CPI, UU, and CRC
 673 *   o  we need to round up to 48 bytes for cells
 674 */
 675static inline int aal5_size(int size)
 676{
 677	int cells = (size + 8 + 47) / 48;
 678	return cells * 48;
 679}
 680
 681/* -------------------- FREE AN ATM SKB: */
 682
 683static inline void lanai_free_skb(struct atm_vcc *atmvcc, struct sk_buff *skb)
 684{
 685	if (atmvcc->pop != NULL)
 686		atmvcc->pop(atmvcc, skb);
 687	else
 688		dev_kfree_skb_any(skb);
 689}
 690
 691/* -------------------- TURN VCCS ON AND OFF: */
 692
 693static void host_vcc_start_rx(const struct lanai_vcc *lvcc)
 694{
 695	u32 addr1;
 696	if (lvcc->rx.atmvcc->qos.aal == ATM_AAL5) {
 697		dma_addr_t dmaaddr = lvcc->rx.buf.dmaaddr;
 698		cardvcc_write(lvcc, 0xFFFF, vcc_rxcrc1);
 699		cardvcc_write(lvcc, 0xFFFF, vcc_rxcrc2);
 700		cardvcc_write(lvcc, 0, vcc_rxwriteptr);
 701		cardvcc_write(lvcc, 0, vcc_rxbufstart);
 702		cardvcc_write(lvcc, 0, vcc_rxreadptr);
 703		cardvcc_write(lvcc, (dmaaddr >> 16) & 0xFFFF, vcc_rxaddr2);
 704		addr1 = ((dmaaddr >> 8) & 0xFF) |
 705		    RXADDR1_SET_SIZE(lanai_buf_size_cardorder(&lvcc->rx.buf))|
 706		    RXADDR1_SET_RMMODE(RMMODE_TRASH) |	/* ??? */
 707		 /* RXADDR1_OAM_PRESERVE |	--- no OAM support yet */
 708		    RXADDR1_SET_MODE(RXMODE_AAL5);
 709	} else
 710		addr1 = RXADDR1_SET_RMMODE(RMMODE_PRESERVE) | /* ??? */
 711		    RXADDR1_OAM_PRESERVE |		      /* ??? */
 712		    RXADDR1_SET_MODE(RXMODE_AAL0);
 713	/* This one must be last! */
 714	cardvcc_write(lvcc, addr1, vcc_rxaddr1);
 715}
 716
 717static void host_vcc_start_tx(const struct lanai_vcc *lvcc)
 718{
 719	dma_addr_t dmaaddr = lvcc->tx.buf.dmaaddr;
 720	cardvcc_write(lvcc, 0, vcc_txicg);
 721	cardvcc_write(lvcc, 0xFFFF, vcc_txcrc1);
 722	cardvcc_write(lvcc, 0xFFFF, vcc_txcrc2);
 723	cardvcc_write(lvcc, 0, vcc_txreadptr);
 724	cardvcc_write(lvcc, 0, vcc_txendptr);
 725	cardvcc_write(lvcc, 0, vcc_txwriteptr);
 726	cardvcc_write(lvcc,
 727		(lvcc->tx.atmvcc->qos.txtp.traffic_class == ATM_CBR) ?
 728		TXCBR_NEXT_BOZO | lvcc->vci : 0, vcc_txcbr_next);
 729	cardvcc_write(lvcc, (dmaaddr >> 16) & 0xFFFF, vcc_txaddr2);
 730	cardvcc_write(lvcc,
 731	    ((dmaaddr >> 8) & 0xFF) |
 732	    TXADDR1_SET_SIZE(lanai_buf_size_cardorder(&lvcc->tx.buf)),
 733	    vcc_txaddr1);
 734}
 735
 736/* Shutdown receiving on card */
 737static void lanai_shutdown_rx_vci(const struct lanai_vcc *lvcc)
 738{
 739	if (lvcc->vbase == NULL)	/* We were never bound to a VCI */
 740		return;
 741	/* 15.1.1 - set to trashing, wait one cell time (15us) */
 742	cardvcc_write(lvcc,
 743	    RXADDR1_SET_RMMODE(RMMODE_TRASH) |
 744	    RXADDR1_SET_MODE(RXMODE_TRASH), vcc_rxaddr1);
 745	udelay(15);
 746	/* 15.1.2 - clear rest of entries */
 747	cardvcc_write(lvcc, 0, vcc_rxaddr2);
 748	cardvcc_write(lvcc, 0, vcc_rxcrc1);
 749	cardvcc_write(lvcc, 0, vcc_rxcrc2);
 750	cardvcc_write(lvcc, 0, vcc_rxwriteptr);
 751	cardvcc_write(lvcc, 0, vcc_rxbufstart);
 752	cardvcc_write(lvcc, 0, vcc_rxreadptr);
 753}
 754
 755/* Shutdown transmitting on card.
 756 * Unfortunately the lanai needs us to wait until all the data
 757 * drains out of the buffer before we can dealloc it, so this
 758 * can take awhile -- up to 370ms for a full 128KB buffer
 759 * assuming everone else is quiet.  In theory the time is
 760 * boundless if there's a CBR VCC holding things up.
 761 */
 762static void lanai_shutdown_tx_vci(struct lanai_dev *lanai,
 763	struct lanai_vcc *lvcc)
 764{
 765	struct sk_buff *skb;
 766	unsigned long flags, timeout;
 767	int read, write, lastread = -1;
 768	APRINTK(!in_interrupt(),
 769	    "lanai_shutdown_tx_vci called w/o process context!\n");
 770	if (lvcc->vbase == NULL)	/* We were never bound to a VCI */
 771		return;
 772	/* 15.2.1 - wait for queue to drain */
 773	while ((skb = skb_dequeue(&lvcc->tx.backlog)) != NULL)
 774		lanai_free_skb(lvcc->tx.atmvcc, skb);
 775	read_lock_irqsave(&vcc_sklist_lock, flags);
 776	__clear_bit(lvcc->vci, lanai->backlog_vccs);
 777	read_unlock_irqrestore(&vcc_sklist_lock, flags);
 778	/*
 779	 * We need to wait for the VCC to drain but don't wait forever.  We
 780	 * give each 1K of buffer size 1/128th of a second to clear out.
 781	 * TODO: maybe disable CBR if we're about to timeout?
 782	 */
 783	timeout = jiffies +
 784	    (((lanai_buf_size(&lvcc->tx.buf) / 1024) * HZ) >> 7);
 785	write = TXWRITEPTR_GET_PTR(cardvcc_read(lvcc, vcc_txwriteptr));
 786	for (;;) {
 787		read = TXREADPTR_GET_PTR(cardvcc_read(lvcc, vcc_txreadptr));
 788		if (read == write &&	   /* Is TX buffer empty? */
 789		    (lvcc->tx.atmvcc->qos.txtp.traffic_class != ATM_CBR ||
 790		    (cardvcc_read(lvcc, vcc_txcbr_next) &
 791		    TXCBR_NEXT_BOZO) == 0))
 792			break;
 793		if (read != lastread) {	   /* Has there been any progress? */
 794			lastread = read;
 795			timeout += HZ / 10;
 796		}
 797		if (unlikely(time_after(jiffies, timeout))) {
 798			printk(KERN_ERR DEV_LABEL "(itf %d): Timed out on "
 799			    "backlog closing vci %d\n",
 800			    lvcc->tx.atmvcc->dev->number, lvcc->vci);
 801			DPRINTK("read, write = %d, %d\n", read, write);
 802			break;
 803		}
 804		msleep(40);
 805	}
 806	/* 15.2.2 - clear out all tx registers */
 807	cardvcc_write(lvcc, 0, vcc_txreadptr);
 808	cardvcc_write(lvcc, 0, vcc_txwriteptr);
 809	cardvcc_write(lvcc, 0, vcc_txendptr);
 810	cardvcc_write(lvcc, 0, vcc_txcrc1);
 811	cardvcc_write(lvcc, 0, vcc_txcrc2);
 812	cardvcc_write(lvcc, 0, vcc_txaddr2);
 813	cardvcc_write(lvcc, 0, vcc_txaddr1);
 814}
 815
 816/* -------------------- MANAGING AAL0 RX BUFFER: */
 817
 818static inline int aal0_buffer_allocate(struct lanai_dev *lanai)
 819{
 820	DPRINTK("aal0_buffer_allocate: allocating AAL0 RX buffer\n");
 821	lanai_buf_allocate(&lanai->aal0buf, AAL0_RX_BUFFER_SIZE, 80,
 822			   lanai->pci);
 823	return (lanai->aal0buf.start == NULL) ? -ENOMEM : 0;
 824}
 825
 826static inline void aal0_buffer_free(struct lanai_dev *lanai)
 827{
 828	DPRINTK("aal0_buffer_allocate: freeing AAL0 RX buffer\n");
 829	lanai_buf_deallocate(&lanai->aal0buf, lanai->pci);
 830}
 831
 832/* -------------------- EEPROM UTILITIES: */
 833
 834/* Offsets of data in the EEPROM */
 835#define EEPROM_COPYRIGHT	(0)
 836#define EEPROM_COPYRIGHT_LEN	(44)
 837#define EEPROM_CHECKSUM		(62)
 838#define EEPROM_CHECKSUM_REV	(63)
 839#define EEPROM_MAC		(64)
 840#define EEPROM_MAC_REV		(70)
 841#define EEPROM_SERIAL		(112)
 842#define EEPROM_SERIAL_REV	(116)
 843#define EEPROM_MAGIC		(120)
 844#define EEPROM_MAGIC_REV	(124)
 845
 846#define EEPROM_MAGIC_VALUE	(0x5AB478D2)
 847
 848#ifndef READ_EEPROM
 849
 850/* Stub functions to use if EEPROM reading is disabled */
 851static int eeprom_read(struct lanai_dev *lanai)
 852{
 853	printk(KERN_INFO DEV_LABEL "(itf %d): *NOT* reading EEPROM\n",
 854	    lanai->number);
 855	memset(&lanai->eeprom[EEPROM_MAC], 0, 6);
 856	return 0;
 857}
 858
 859static int eeprom_validate(struct lanai_dev *lanai)
 860{
 861	lanai->serialno = 0;
 862	lanai->magicno = EEPROM_MAGIC_VALUE;
 863	return 0;
 864}
 865
 866#else /* READ_EEPROM */
 867
 868static int eeprom_read(struct lanai_dev *lanai)
 869{
 870	int i, address;
 871	u8 data;
 872	u32 tmp;
 873#define set_config1(x)   do { lanai->conf1 = x; conf1_write(lanai); \
 874			    } while (0)
 875#define clock_h()	 set_config1(lanai->conf1 | CONFIG1_PROMCLK)
 876#define clock_l()	 set_config1(lanai->conf1 &~ CONFIG1_PROMCLK)
 877#define data_h()	 set_config1(lanai->conf1 | CONFIG1_PROMDATA)
 878#define data_l()	 set_config1(lanai->conf1 &~ CONFIG1_PROMDATA)
 879#define pre_read()	 do { data_h(); clock_h(); udelay(5); } while (0)
 880#define read_pin()	 (reg_read(lanai, Status_Reg) & STATUS_PROMDATA)
 881#define send_stop()	 do { data_l(); udelay(5); clock_h(); udelay(5); \
 882			      data_h(); udelay(5); } while (0)
 883	/* start with both clock and data high */
 884	data_h(); clock_h(); udelay(5);
 885	for (address = 0; address < LANAI_EEPROM_SIZE; address++) {
 886		data = (address << 1) | 1;	/* Command=read + address */
 887		/* send start bit */
 888		data_l(); udelay(5);
 889		clock_l(); udelay(5);
 890		for (i = 128; i != 0; i >>= 1) {   /* write command out */
 891			tmp = (lanai->conf1 & ~CONFIG1_PROMDATA) |
 892			    ((data & i) ? CONFIG1_PROMDATA : 0);
 893			if (lanai->conf1 != tmp) {
 894				set_config1(tmp);
 895				udelay(5);	/* Let new data settle */
 896			}
 897			clock_h(); udelay(5); clock_l(); udelay(5);
 898		}
 899		/* look for ack */
 900		data_h(); clock_h(); udelay(5);
 901		if (read_pin() != 0)
 902			goto error;	/* No ack seen */
 903		clock_l(); udelay(5);
 904		/* read back result */
 905		for (data = 0, i = 7; i >= 0; i--) {
 906			data_h(); clock_h(); udelay(5);
 907			data = (data << 1) | !!read_pin();
 908			clock_l(); udelay(5);
 909		}
 910		/* look again for ack */
 911		data_h(); clock_h(); udelay(5);
 912		if (read_pin() == 0)
 913			goto error;	/* Spurious ack */
 914		clock_l(); udelay(5);
 915		send_stop();
 916		lanai->eeprom[address] = data;
 917		DPRINTK("EEPROM 0x%04X %02X\n",
 918		    (unsigned int) address, (unsigned int) data);
 919	}
 920	return 0;
 921    error:
 922	clock_l(); udelay(5);		/* finish read */
 923	send_stop();
 924	printk(KERN_ERR DEV_LABEL "(itf %d): error reading EEPROM byte %d\n",
 925	    lanai->number, address);
 926	return -EIO;
 927#undef set_config1
 928#undef clock_h
 929#undef clock_l
 930#undef data_h
 931#undef data_l
 932#undef pre_read
 933#undef read_pin
 934#undef send_stop
 935}
 936
 937/* read a big-endian 4-byte value out of eeprom */
 938static inline u32 eeprom_be4(const struct lanai_dev *lanai, int address)
 939{
 940	return be32_to_cpup((const u32 *) &lanai->eeprom[address]);
 941}
 942
 943/* Checksum/validate EEPROM contents */
 944static int eeprom_validate(struct lanai_dev *lanai)
 945{
 946	int i, s;
 947	u32 v;
 948	const u8 *e = lanai->eeprom;
 949#ifdef DEBUG
 950	/* First, see if we can get an ASCIIZ string out of the copyright */
 951	for (i = EEPROM_COPYRIGHT;
 952	    i < (EEPROM_COPYRIGHT + EEPROM_COPYRIGHT_LEN); i++)
 953		if (e[i] < 0x20 || e[i] > 0x7E)
 954			break;
 955	if ( i != EEPROM_COPYRIGHT &&
 956	    i != EEPROM_COPYRIGHT + EEPROM_COPYRIGHT_LEN && e[i] == '\0')
 957		DPRINTK("eeprom: copyright = \"%s\"\n",
 958		    (char *) &e[EEPROM_COPYRIGHT]);
 959	else
 960		DPRINTK("eeprom: copyright not found\n");
 961#endif
 962	/* Validate checksum */
 963	for (i = s = 0; i < EEPROM_CHECKSUM; i++)
 964		s += e[i];
 965	s &= 0xFF;
 966	if (s != e[EEPROM_CHECKSUM]) {
 967		printk(KERN_ERR DEV_LABEL "(itf %d): EEPROM checksum bad "
 968		    "(wanted 0x%02X, got 0x%02X)\n", lanai->number,
 969		    (unsigned int) s, (unsigned int) e[EEPROM_CHECKSUM]);
 970		return -EIO;
 971	}
 972	s ^= 0xFF;
 973	if (s != e[EEPROM_CHECKSUM_REV]) {
 974		printk(KERN_ERR DEV_LABEL "(itf %d): EEPROM inverse checksum "
 975		    "bad (wanted 0x%02X, got 0x%02X)\n", lanai->number,
 976		    (unsigned int) s, (unsigned int) e[EEPROM_CHECKSUM_REV]);
 977		return -EIO;
 978	}
 979	/* Verify MAC address */
 980	for (i = 0; i < 6; i++)
 981		if ((e[EEPROM_MAC + i] ^ e[EEPROM_MAC_REV + i]) != 0xFF) {
 982			printk(KERN_ERR DEV_LABEL
 983			    "(itf %d) : EEPROM MAC addresses don't match "
 984			    "(0x%02X, inverse 0x%02X)\n", lanai->number,
 985			    (unsigned int) e[EEPROM_MAC + i],
 986			    (unsigned int) e[EEPROM_MAC_REV + i]);
 987			return -EIO;
 988		}
 989	DPRINTK("eeprom: MAC address = %pM\n", &e[EEPROM_MAC]);
 990	/* Verify serial number */
 991	lanai->serialno = eeprom_be4(lanai, EEPROM_SERIAL);
 992	v = eeprom_be4(lanai, EEPROM_SERIAL_REV);
 993	if ((lanai->serialno ^ v) != 0xFFFFFFFF) {
 994		printk(KERN_ERR DEV_LABEL "(itf %d): EEPROM serial numbers "
 995		    "don't match (0x%08X, inverse 0x%08X)\n", lanai->number,
 996		    (unsigned int) lanai->serialno, (unsigned int) v);
 997		return -EIO;
 998	}
 999	DPRINTK("eeprom: Serial number = %d\n", (unsigned int) lanai->serialno);
1000	/* Verify magic number */
1001	lanai->magicno = eeprom_be4(lanai, EEPROM_MAGIC);
1002	v = eeprom_be4(lanai, EEPROM_MAGIC_REV);
1003	if ((lanai->magicno ^ v) != 0xFFFFFFFF) {
1004		printk(KERN_ERR DEV_LABEL "(itf %d): EEPROM magic numbers "
1005		    "don't match (0x%08X, inverse 0x%08X)\n", lanai->number,
1006		    lanai->magicno, v);
1007		return -EIO;
1008	}
1009	DPRINTK("eeprom: Magic number = 0x%08X\n", lanai->magicno);
1010	if (lanai->magicno != EEPROM_MAGIC_VALUE)
1011		printk(KERN_WARNING DEV_LABEL "(itf %d): warning - EEPROM "
1012		    "magic not what expected (got 0x%08X, not 0x%08X)\n",
1013		    lanai->number, (unsigned int) lanai->magicno,
1014		    (unsigned int) EEPROM_MAGIC_VALUE);
1015	return 0;
1016}
1017
1018#endif /* READ_EEPROM */
1019
1020static inline const u8 *eeprom_mac(const struct lanai_dev *lanai)
1021{
1022	return &lanai->eeprom[EEPROM_MAC];
1023}
1024
1025/* -------------------- INTERRUPT HANDLING UTILITIES: */
1026
1027/* Interrupt types */
1028#define INT_STATS	(0x00000002)	/* Statistics counter overflow */
1029#define INT_SOOL	(0x00000004)	/* SOOL changed state */
1030#define INT_LOCD	(0x00000008)	/* LOCD changed state */
1031#define INT_LED		(0x00000010)	/* LED (HAPPI) changed state */
1032#define INT_GPIN	(0x00000020)	/* GPIN changed state */
1033#define INT_PING	(0x00000040)	/* PING_COUNT fulfilled */
1034#define INT_WAKE	(0x00000080)	/* Lanai wants bus */
1035#define INT_CBR0	(0x00000100)	/* CBR sched hit VCI 0 */
1036#define INT_LOCK	(0x00000200)	/* Service list overflow */
1037#define INT_MISMATCH	(0x00000400)	/* TX magic list mismatch */
1038#define INT_AAL0_STR	(0x00000800)	/* Non-AAL5 buffer half filled */
1039#define INT_AAL0	(0x00001000)	/* Non-AAL5 data available */
1040#define INT_SERVICE	(0x00002000)	/* Service list entries available */
1041#define INT_TABORTSENT	(0x00004000)	/* Target abort sent by lanai */
1042#define INT_TABORTBM	(0x00008000)	/* Abort rcv'd as bus master */
1043#define INT_TIMEOUTBM	(0x00010000)	/* No response to bus master */
1044#define INT_PCIPARITY	(0x00020000)	/* Parity error on PCI */
1045
1046/* Sets of the above */
1047#define INT_ALL		(0x0003FFFE)	/* All interrupts */
1048#define INT_STATUS	(0x0000003C)	/* Some status pin changed */
1049#define INT_DMASHUT	(0x00038000)	/* DMA engine got shut down */
1050#define INT_SEGSHUT	(0x00000700)	/* Segmentation got shut down */
1051
1052static inline u32 intr_pending(const struct lanai_dev *lanai)
1053{
1054	return reg_read(lanai, IntStatusMasked_Reg);
1055}
1056
1057static inline void intr_enable(const struct lanai_dev *lanai, u32 i)
1058{
1059	reg_write(lanai, i, IntControlEna_Reg);
1060}
1061
1062static inline void intr_disable(const struct lanai_dev *lanai, u32 i)
1063{
1064	reg_write(lanai, i, IntControlDis_Reg);
1065}
1066
1067/* -------------------- CARD/PCI STATUS: */
1068
1069static void status_message(int itf, const char *name, int status)
1070{
1071	static const char *onoff[2] = { "off to on", "on to off" };
1072	printk(KERN_INFO DEV_LABEL "(itf %d): %s changed from %s\n",
1073	    itf, name, onoff[!status]);
1074}
1075
1076static void lanai_check_status(struct lanai_dev *lanai)
1077{
1078	u32 new = reg_read(lanai, Status_Reg);
1079	u32 changes = new ^ lanai->status;
1080	lanai->status = new;
1081#define e(flag, name) \
1082		if (changes & flag) \
1083			status_message(lanai->number, name, new & flag)
1084	e(STATUS_SOOL, "SOOL");
1085	e(STATUS_LOCD, "LOCD");
1086	e(STATUS_LED, "LED");
1087	e(STATUS_GPIN, "GPIN");
1088#undef e
1089}
1090
1091static void pcistatus_got(int itf, const char *name)
1092{
1093	printk(KERN_INFO DEV_LABEL "(itf %d): PCI got %s error\n", itf, name);
1094}
1095
1096static void pcistatus_check(struct lanai_dev *lanai, int clearonly)
1097{
1098	u16 s;
1099	int result;
1100	result = pci_read_config_word(lanai->pci, PCI_STATUS, &s);
1101	if (result != PCIBIOS_SUCCESSFUL) {
1102		printk(KERN_ERR DEV_LABEL "(itf %d): can't read PCI_STATUS: "
1103		    "%d\n", lanai->number, result);
1104		return;
1105	}
1106	s &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
1107	    PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT |
1108	    PCI_STATUS_SIG_TARGET_ABORT | PCI_STATUS_PARITY;
1109	if (s == 0)
1110		return;
1111	result = pci_write_config_word(lanai->pci, PCI_STATUS, s);
1112	if (result != PCIBIOS_SUCCESSFUL)
1113		printk(KERN_ERR DEV_LABEL "(itf %d): can't write PCI_STATUS: "
1114		    "%d\n", lanai->number, result);
1115	if (clearonly)
1116		return;
1117#define e(flag, name, stat) \
1118		if (s & flag) { \
1119			pcistatus_got(lanai->number, name); \
1120			++lanai->stats.pcierr_##stat; \
1121		}
1122	e(PCI_STATUS_DETECTED_PARITY, "parity", parity_detect);
1123	e(PCI_STATUS_SIG_SYSTEM_ERROR, "signalled system", serr_set);
1124	e(PCI_STATUS_REC_MASTER_ABORT, "master", master_abort);
1125	e(PCI_STATUS_REC_TARGET_ABORT, "master target", m_target_abort);
1126	e(PCI_STATUS_SIG_TARGET_ABORT, "slave", s_target_abort);
1127	e(PCI_STATUS_PARITY, "master parity", master_parity);
1128#undef e
1129}
1130
1131/* -------------------- VCC TX BUFFER UTILITIES: */
1132
1133/* space left in tx buffer in bytes */
1134static inline int vcc_tx_space(const struct lanai_vcc *lvcc, int endptr)
1135{
1136	int r;
1137	r = endptr * 16;
1138	r -= ((unsigned long) lvcc->tx.buf.ptr) -
1139	    ((unsigned long) lvcc->tx.buf.start);
1140	r -= 16;	/* Leave "bubble" - if start==end it looks empty */
1141	if (r < 0)
1142		r += lanai_buf_size(&lvcc->tx.buf);
1143	return r;
1144}
1145
1146/* test if VCC is currently backlogged */
1147static inline int vcc_is_backlogged(const struct lanai_vcc *lvcc)
1148{
1149	return !skb_queue_empty(&lvcc->tx.backlog);
1150}
1151
1152/* Bit fields in the segmentation buffer descriptor */
1153#define DESCRIPTOR_MAGIC	(0xD0000000)
1154#define DESCRIPTOR_AAL5		(0x00008000)
1155#define DESCRIPTOR_AAL5_STREAM	(0x00004000)
1156#define DESCRIPTOR_CLP		(0x00002000)
1157
1158/* Add 32-bit descriptor with its padding */
1159static inline void vcc_tx_add_aal5_descriptor(struct lanai_vcc *lvcc,
1160	u32 flags, int len)
1161{
1162	int pos;
1163	APRINTK((((unsigned long) lvcc->tx.buf.ptr) & 15) == 0,
1164	    "vcc_tx_add_aal5_descriptor: bad ptr=%p\n", lvcc->tx.buf.ptr);
1165	lvcc->tx.buf.ptr += 4;	/* Hope the values REALLY don't matter */
1166	pos = ((unsigned char *) lvcc->tx.buf.ptr) -
1167	    (unsigned char *) lvcc->tx.buf.start;
1168	APRINTK((pos & ~0x0001FFF0) == 0,
1169	    "vcc_tx_add_aal5_descriptor: bad pos (%d) before, vci=%d, "
1170	    "start,ptr,end=%p,%p,%p\n", pos, lvcc->vci,
1171	    lvcc->tx.buf.start, lvcc->tx.buf.ptr, lvcc->tx.buf.end);
1172	pos = (pos + len) & (lanai_buf_size(&lvcc->tx.buf) - 1);
1173	APRINTK((pos & ~0x0001FFF0) == 0,
1174	    "vcc_tx_add_aal5_descriptor: bad pos (%d) after, vci=%d, "
1175	    "start,ptr,end=%p,%p,%p\n", pos, lvcc->vci,
1176	    lvcc->tx.buf.start, lvcc->tx.buf.ptr, lvcc->tx.buf.end);
1177	lvcc->tx.buf.ptr[-1] =
1178	    cpu_to_le32(DESCRIPTOR_MAGIC | DESCRIPTOR_AAL5 |
1179	    ((lvcc->tx.atmvcc->atm_options & ATM_ATMOPT_CLP) ?
1180	    DESCRIPTOR_CLP : 0) | flags | pos >> 4);
1181	if (lvcc->tx.buf.ptr >= lvcc->tx.buf.end)
1182		lvcc->tx.buf.ptr = lvcc->tx.buf.start;
1183}
1184
1185/* Add 32-bit AAL5 trailer and leave room for its CRC */
1186static inline void vcc_tx_add_aal5_trailer(struct lanai_vcc *lvcc,
1187	int len, int cpi, int uu)
1188{
1189	APRINTK((((unsigned long) lvcc->tx.buf.ptr) & 15) == 8,
1190	    "vcc_tx_add_aal5_trailer: bad ptr=%p\n", lvcc->tx.buf.ptr);
1191	lvcc->tx.buf.ptr += 2;
1192	lvcc->tx.buf.ptr[-2] = cpu_to_be32((uu << 24) | (cpi << 16) | len);
1193	if (lvcc->tx.buf.ptr >= lvcc->tx.buf.end)
1194		lvcc->tx.buf.ptr = lvcc->tx.buf.start;
1195}
1196
1197static inline void vcc_tx_memcpy(struct lanai_vcc *lvcc,
1198	const unsigned char *src, int n)
1199{
1200	unsigned char *e;
1201	int m;
1202	e = ((unsigned char *) lvcc->tx.buf.ptr) + n;
1203	m = e - (unsigned char *) lvcc->tx.buf.end;
1204	if (m < 0)
1205		m = 0;
1206	memcpy(lvcc->tx.buf.ptr, src, n - m);
1207	if (m != 0) {
1208		memcpy(lvcc->tx.buf.start, src + n - m, m);
1209		e = ((unsigned char *) lvcc->tx.buf.start) + m;
1210	}
1211	lvcc->tx.buf.ptr = (u32 *) e;
1212}
1213
1214static inline void vcc_tx_memzero(struct lanai_vcc *lvcc, int n)
1215{
1216	unsigned char *e;
1217	int m;
1218	if (n == 0)
1219		return;
1220	e = ((unsigned char *) lvcc->tx.buf.ptr) + n;
1221	m = e - (unsigned char *) lvcc->tx.buf.end;
1222	if (m < 0)
1223		m = 0;
1224	memset(lvcc->tx.buf.ptr, 0, n - m);
1225	if (m != 0) {
1226		memset(lvcc->tx.buf.start, 0, m);
1227		e = ((unsigned char *) lvcc->tx.buf.start) + m;
1228	}
1229	lvcc->tx.buf.ptr = (u32 *) e;
1230}
1231
1232/* Update "butt" register to specify new WritePtr */
1233static inline void lanai_endtx(struct lanai_dev *lanai,
1234	const struct lanai_vcc *lvcc)
1235{
1236	int i, ptr = ((unsigned char *) lvcc->tx.buf.ptr) -
1237	    (unsigned char *) lvcc->tx.buf.start;
1238	APRINTK((ptr & ~0x0001FFF0) == 0,
1239	    "lanai_endtx: bad ptr (%d), vci=%d, start,ptr,end=%p,%p,%p\n",
1240	    ptr, lvcc->vci, lvcc->tx.buf.start, lvcc->tx.buf.ptr,
1241	    lvcc->tx.buf.end);
1242
1243	/*
1244	 * Since the "butt register" is a shared resounce on the card we
1245	 * serialize all accesses to it through this spinlock.  This is
1246	 * mostly just paranoia since the register is rarely "busy" anyway
1247	 * but is needed for correctness.
1248	 */
1249	spin_lock(&lanai->endtxlock);
1250	/*
1251	 * We need to check if the "butt busy" bit is set before
1252	 * updating the butt register.  In theory this should
1253	 * never happen because the ATM card is plenty fast at
1254	 * updating the register.  Still, we should make sure
1255	 */
1256	for (i = 0; reg_read(lanai, Status_Reg) & STATUS_BUTTBUSY; i++) {
1257		if (unlikely(i > 50)) {
1258			printk(KERN_ERR DEV_LABEL "(itf %d): butt register "
1259			    "always busy!\n", lanai->number);
1260			break;
1261		}
1262		udelay(5);
1263	}
1264	/*
1265	 * Before we tall the card to start work we need to be sure 100% of
1266	 * the info in the service buffer has been written before we tell
1267	 * the card about it
1268	 */
1269	wmb();
1270	reg_write(lanai, (ptr << 12) | lvcc->vci, Butt_Reg);
1271	spin_unlock(&lanai->endtxlock);
1272}
1273
1274/*
1275 * Add one AAL5 PDU to lvcc's transmit buffer.  Caller garauntees there's
1276 * space available.  "pdusize" is the number of bytes the PDU will take
1277 */
1278static void lanai_send_one_aal5(struct lanai_dev *lanai,
1279	struct lanai_vcc *lvcc, struct sk_buff *skb, int pdusize)
1280{
1281	int pad;
1282	APRINTK(pdusize == aal5_size(skb->len),
1283	    "lanai_send_one_aal5: wrong size packet (%d != %d)\n",
1284	    pdusize, aal5_size(skb->len));
1285	vcc_tx_add_aal5_descriptor(lvcc, 0, pdusize);
1286	pad = pdusize - skb->len - 8;
1287	APRINTK(pad >= 0, "pad is negative (%d)\n", pad);
1288	APRINTK(pad < 48, "pad is too big (%d)\n", pad);
1289	vcc_tx_memcpy(lvcc, skb->data, skb->len);
1290	vcc_tx_memzero(lvcc, pad);
1291	vcc_tx_add_aal5_trailer(lvcc, skb->len, 0, 0);
1292	lanai_endtx(lanai, lvcc);
1293	lanai_free_skb(lvcc->tx.atmvcc, skb);
1294	atomic_inc(&lvcc->tx.atmvcc->stats->tx);
1295}
1296
1297/* Try to fill the buffer - don't call unless there is backlog */
1298static void vcc_tx_unqueue_aal5(struct lanai_dev *lanai,
1299	struct lanai_vcc *lvcc, int endptr)
1300{
1301	int n;
1302	struct sk_buff *skb;
1303	int space = vcc_tx_space(lvcc, endptr);
1304	APRINTK(vcc_is_backlogged(lvcc),
1305	    "vcc_tx_unqueue() called with empty backlog (vci=%d)\n",
1306	    lvcc->vci);
1307	while (space >= 64) {
1308		skb = skb_dequeue(&lvcc->tx.backlog);
1309		if (skb == NULL)
1310			goto no_backlog;
1311		n = aal5_size(skb->len);
1312		if (n + 16 > space) {
1313			/* No room for this packet - put it back on queue */
1314			skb_queue_head(&lvcc->tx.backlog, skb);
1315			return;
1316		}
1317		lanai_send_one_aal5(lanai, lvcc, skb, n);
1318		space -= n + 16;
1319	}
1320	if (!vcc_is_backlogged(lvcc)) {
1321	    no_backlog:
1322		__clear_bit(lvcc->vci, lanai->backlog_vccs);
1323	}
1324}
1325
1326/* Given an skb that we want to transmit either send it now or queue */
1327static void vcc_tx_aal5(struct lanai_dev *lanai, struct lanai_vcc *lvcc,
1328	struct sk_buff *skb)
1329{
1330	int space, n;
1331	if (vcc_is_backlogged(lvcc))		/* Already backlogged */
1332		goto queue_it;
1333	space = vcc_tx_space(lvcc,
1334		    TXREADPTR_GET_PTR(cardvcc_read(lvcc, vcc_txreadptr)));
1335	n = aal5_size(skb->len);
1336	APRINTK(n + 16 >= 64, "vcc_tx_aal5: n too small (%d)\n", n);
1337	if (space < n + 16) {			/* No space for this PDU */
1338		__set_bit(lvcc->vci, lanai->backlog_vccs);
1339	    queue_it:
1340		skb_queue_tail(&lvcc->tx.backlog, skb);
1341		return;
1342	}
1343	lanai_send_one_aal5(lanai, lvcc, skb, n);
1344}
1345
1346static void vcc_tx_unqueue_aal0(struct lanai_dev *lanai,
1347	struct lanai_vcc *lvcc, int endptr)
1348{
1349	printk(KERN_INFO DEV_LABEL
1350	    ": vcc_tx_unqueue_aal0: not implemented\n");
1351}
1352
1353static void vcc_tx_aal0(struct lanai_dev *lanai, struct lanai_vcc *lvcc,
1354	struct sk_buff *skb)
1355{
1356	printk(KERN_INFO DEV_LABEL ": vcc_tx_aal0: not implemented\n");
1357	/* Remember to increment lvcc->tx.atmvcc->stats->tx */
1358	lanai_free_skb(lvcc->tx.atmvcc, skb);
1359}
1360
1361/* -------------------- VCC RX BUFFER UTILITIES: */
1362
1363/* unlike the _tx_ cousins, this doesn't update ptr */
1364static inline void vcc_rx_memcpy(unsigned char *dest,
1365	const struct lanai_vcc *lvcc, int n)
1366{
1367	int m = ((const unsigned char *) lvcc->rx.buf.ptr) + n -
1368	    ((const unsigned char *) (lvcc->rx.buf.end));
1369	if (m < 0)
1370		m = 0;
1371	memcpy(dest, lvcc->rx.buf.ptr, n - m);
1372	memcpy(dest + n - m, lvcc->rx.buf.start, m);
1373	/* Make sure that these copies don't get reordered */
1374	barrier();
1375}
1376
1377/* Receive AAL5 data on a VCC with a particular endptr */
1378static void vcc_rx_aal5(struct lanai_vcc *lvcc, int endptr)
1379{
1380	int size;
1381	struct sk_buff *skb;
1382	const u32 *x;
1383	u32 *end = &lvcc->rx.buf.start[endptr * 4];
1384	int n = ((unsigned long) end) - ((unsigned long) lvcc->rx.buf.ptr);
1385	if (n < 0)
1386		n += lanai_buf_size(&lvcc->rx.buf);
1387	APRINTK(n >= 0 && n < lanai_buf_size(&lvcc->rx.buf) && !(n & 15),
1388	    "vcc_rx_aal5: n out of range (%d/%zu)\n",
1389	    n, lanai_buf_size(&lvcc->rx.buf));
1390	/* Recover the second-to-last word to get true pdu length */
1391	if ((x = &end[-2]) < lvcc->rx.buf.start)
1392		x = &lvcc->rx.buf.end[-2];
1393	/*
1394	 * Before we actually read from the buffer, make sure the memory
1395	 * changes have arrived
1396	 */
1397	rmb();
1398	size = be32_to_cpup(x) & 0xffff;
1399	if (unlikely(n != aal5_size(size))) {
1400		/* Make sure size matches padding */
1401		printk(KERN_INFO DEV_LABEL "(itf %d): Got bad AAL5 length "
1402		    "on vci=%d - size=%d n=%d\n",
1403		    lvcc->rx.atmvcc->dev->number, lvcc->vci, size, n);
1404		lvcc->stats.x.aal5.rx_badlen++;
1405		goto out;
1406	}
1407	skb = atm_alloc_charge(lvcc->rx.atmvcc, size, GFP_ATOMIC);
1408	if (unlikely(skb == NULL)) {
1409		lvcc->stats.rx_nomem++;
1410		goto out;
1411	}
1412	skb_put(skb, size);
1413	vcc_rx_memcpy(skb->data, lvcc, size);
1414	ATM_SKB(skb)->vcc = lvcc->rx.atmvcc;
1415	__net_timestamp(skb);
1416	lvcc->rx.atmvcc->push(lvcc->rx.atmvcc, skb);
1417	atomic_inc(&lvcc->rx.atmvcc->stats->rx);
1418    out:
1419	lvcc->rx.buf.ptr = end;
1420	cardvcc_write(lvcc, endptr, vcc_rxreadptr);
1421}
1422
1423static void vcc_rx_aal0(struct lanai_dev *lanai)
1424{
1425	printk(KERN_INFO DEV_LABEL ": vcc_rx_aal0: not implemented\n");
1426	/* Remember to get read_lock(&vcc_sklist_lock) while looking up VC */
1427	/* Remember to increment lvcc->rx.atmvcc->stats->rx */
1428}
1429
1430/* -------------------- MANAGING HOST-BASED VCC TABLE: */
1431
1432/* Decide whether to use vmalloc or get_zeroed_page for VCC table */
1433#if (NUM_VCI * BITS_PER_LONG) <= PAGE_SIZE
1434#define VCCTABLE_GETFREEPAGE
1435#else
1436#include <linux/vmalloc.h>
1437#endif
1438
1439static int vcc_table_allocate(struct lanai_dev *lanai)
1440{
1441#ifdef VCCTABLE_GETFREEPAGE
1442	APRINTK((lanai->num_vci) * sizeof(struct lanai_vcc *) <= PAGE_SIZE,
1443	    "vcc table > PAGE_SIZE!");
1444	lanai->vccs = (struct lanai_vcc **) get_zeroed_page(GFP_KERNEL);
1445	return (lanai->vccs == NULL) ? -ENOMEM : 0;
1446#else
1447	int bytes = (lanai->num_vci) * sizeof(struct lanai_vcc *);
1448	lanai->vccs = vzalloc(bytes);
1449	if (unlikely(lanai->vccs == NULL))
1450		return -ENOMEM;
1451	return 0;
1452#endif
1453}
1454
1455static inline void vcc_table_deallocate(const struct lanai_dev *lanai)
1456{
1457#ifdef VCCTABLE_GETFREEPAGE
1458	free_page((unsigned long) lanai->vccs);
1459#else
1460	vfree(lanai->vccs);
1461#endif
1462}
1463
1464/* Allocate a fresh lanai_vcc, with the appropriate things cleared */
1465static inline struct lanai_vcc *new_lanai_vcc(void)
1466{
1467	struct lanai_vcc *lvcc;
1468	lvcc =  kzalloc(sizeof(*lvcc), GFP_KERNEL);
1469	if (likely(lvcc != NULL)) {
1470		skb_queue_head_init(&lvcc->tx.backlog);
1471#ifdef DEBUG
1472		lvcc->vci = -1;
1473#endif
1474	}
1475	return lvcc;
1476}
1477
1478static int lanai_get_sized_buffer(struct lanai_dev *lanai,
1479	struct lanai_buffer *buf, int max_sdu, int multiplier,
1480	const char *name)
1481{
1482	int size;
1483	if (unlikely(max_sdu < 1))
1484		max_sdu = 1;
1485	max_sdu = aal5_size(max_sdu);
1486	size = (max_sdu + 16) * multiplier + 16;
1487	lanai_buf_allocate(buf, size, max_sdu + 32, lanai->pci);
1488	if (unlikely(buf->start == NULL))
1489		return -ENOMEM;
1490	if (unlikely(lanai_buf_size(buf) < size))
1491		printk(KERN_WARNING DEV_LABEL "(itf %d): wanted %d bytes "
1492		    "for %s buffer, got only %zu\n", lanai->number, size,
1493		    name, lanai_buf_size(buf));
1494	DPRINTK("Allocated %zu byte %s buffer\n", lanai_buf_size(buf), name);
1495	return 0;
1496}
1497
1498/* Setup a RX buffer for a currently unbound AAL5 vci */
1499static inline int lanai_setup_rx_vci_aal5(struct lanai_dev *lanai,
1500	struct lanai_vcc *lvcc, const struct atm_qos *qos)
1501{
1502	return lanai_get_sized_buffer(lanai, &lvcc->rx.buf,
1503	    qos->rxtp.max_sdu, AAL5_RX_MULTIPLIER, "RX");
1504}
1505
1506/* Setup a TX buffer for a currently unbound AAL5 vci */
1507static int lanai_setup_tx_vci(struct lanai_dev *lanai, struct lanai_vcc *lvcc,
1508	const struct atm_qos *qos)
1509{
1510	int max_sdu, multiplier;
1511	if (qos->aal == ATM_AAL0) {
1512		lvcc->tx.unqueue = vcc_tx_unqueue_aal0;
1513		max_sdu = ATM_CELL_SIZE - 1;
1514		multiplier = AAL0_TX_MULTIPLIER;
1515	} else {
1516		lvcc->tx.unqueue = vcc_tx_unqueue_aal5;
1517		max_sdu = qos->txtp.max_sdu;
1518		multiplier = AAL5_TX_MULTIPLIER;
1519	}
1520	return lanai_get_sized_buffer(lanai, &lvcc->tx.buf, max_sdu,
1521	    multiplier, "TX");
1522}
1523
1524static inline void host_vcc_bind(struct lanai_dev *lanai,
1525	struct lanai_vcc *lvcc, vci_t vci)
1526{
1527	if (lvcc->vbase != NULL)
1528		return;    /* We already were bound in the other direction */
1529	DPRINTK("Binding vci %d\n", vci);
1530#ifdef USE_POWERDOWN
1531	if (lanai->nbound++ == 0) {
1532		DPRINTK("Coming out of powerdown\n");
1533		lanai->conf1 &= ~CONFIG1_POWERDOWN;
1534		conf1_write(lanai);
1535		conf2_write(lanai);
1536	}
1537#endif
1538	lvcc->vbase = cardvcc_addr(lanai, vci);
1539	lanai->vccs[lvcc->vci = vci] = lvcc;
1540}
1541
1542static inline void host_vcc_unbind(struct lanai_dev *lanai,
1543	struct lanai_vcc *lvcc)
1544{
1545	if (lvcc->vbase == NULL)
1546		return;	/* This vcc was never bound */
1547	DPRINTK("Unbinding vci %d\n", lvcc->vci);
1548	lvcc->vbase = NULL;
1549	lanai->vccs[lvcc->vci] = NULL;
1550#ifdef USE_POWERDOWN
1551	if (--lanai->nbound == 0) {
1552		DPRINTK("Going into powerdown\n");
1553		lanai->conf1 |= CONFIG1_POWERDOWN;
1554		conf1_write(lanai);
1555	}
1556#endif
1557}
1558
1559/* -------------------- RESET CARD: */
1560
1561static void lanai_reset(struct lanai_dev *lanai)
1562{
1563	printk(KERN_CRIT DEV_LABEL "(itf %d): *NOT* resetting - not "
1564	    "implemented\n", lanai->number);
1565	/* TODO */
1566	/* The following is just a hack until we write the real
1567	 * resetter - at least ack whatever interrupt sent us
1568	 * here
1569	 */
1570	reg_write(lanai, INT_ALL, IntAck_Reg);
1571	lanai->stats.card_reset++;
1572}
1573
1574/* -------------------- SERVICE LIST UTILITIES: */
1575
1576/*
1577 * Allocate service buffer and tell card about it
1578 */
1579static int service_buffer_allocate(struct lanai_dev *lanai)
1580{
1581	lanai_buf_allocate(&lanai->service, SERVICE_ENTRIES * 4, 8,
1582	    lanai->pci);
1583	if (unlikely(lanai->service.start == NULL))
1584		return -ENOMEM;
1585	DPRINTK("allocated service buffer at %p, size %zu(%d)\n",
1586	    lanai->service.start,
1587	    lanai_buf_size(&lanai->service),
1588	    lanai_buf_size_cardorder(&lanai->service));
1589	/* Clear ServWrite register to be safe */
1590	reg_write(lanai, 0, ServWrite_Reg);
1591	/* ServiceStuff register contains size and address of buffer */
1592	reg_write(lanai,
1593	    SSTUFF_SET_SIZE(lanai_buf_size_cardorder(&lanai->service)) |
1594	    SSTUFF_SET_ADDR(lanai->service.dmaaddr),
1595	    ServiceStuff_Reg);
1596	return 0;
1597}
1598
1599static inline void service_buffer_deallocate(struct lanai_dev *lanai)
1600{
1601	lanai_buf_deallocate(&lanai->service, lanai->pci);
1602}
1603
1604/* Bitfields in service list */
1605#define SERVICE_TX	(0x80000000)	/* Was from transmission */
1606#define SERVICE_TRASH	(0x40000000)	/* RXed PDU was trashed */
1607#define SERVICE_CRCERR	(0x20000000)	/* RXed PDU had CRC error */
1608#define SERVICE_CI	(0x10000000)	/* RXed PDU had CI set */
1609#define SERVICE_CLP	(0x08000000)	/* RXed PDU had CLP set */
1610#define SERVICE_STREAM	(0x04000000)	/* RX Stream mode */
1611#define SERVICE_GET_VCI(x) (((x)>>16)&0x3FF)
1612#define SERVICE_GET_END(x) ((x)&0x1FFF)
1613
1614/* Handle one thing from the service list - returns true if it marked a
1615 * VCC ready for xmit
1616 */
1617static int handle_service(struct lanai_dev *lanai, u32 s)
1618{
1619	vci_t vci = SERVICE_GET_VCI(s);
1620	struct lanai_vcc *lvcc;
1621	read_lock(&vcc_sklist_lock);
1622	lvcc = lanai->vccs[vci];
1623	if (unlikely(lvcc == NULL)) {
1624		read_unlock(&vcc_sklist_lock);
1625		DPRINTK("(itf %d) got service entry 0x%X for nonexistent "
1626		    "vcc %d\n", lanai->number, (unsigned int) s, vci);
1627		if (s & SERVICE_TX)
1628			lanai->stats.service_notx++;
1629		else
1630			lanai->stats.service_norx++;
1631		return 0;
1632	}
1633	if (s & SERVICE_TX) {			/* segmentation interrupt */
1634		if (unlikely(lvcc->tx.atmvcc == NULL)) {
1635			read_unlock(&vcc_sklist_lock);
1636			DPRINTK("(itf %d) got service entry 0x%X for non-TX "
1637			    "vcc %d\n", lanai->number, (unsigned int) s, vci);
1638			lanai->stats.service_notx++;
1639			return 0;
1640		}
1641		__set_bit(vci, lanai->transmit_ready);
1642		lvcc->tx.endptr = SERVICE_GET_END(s);
1643		read_unlock(&vcc_sklist_lock);
1644		return 1;
1645	}
1646	if (unlikely(lvcc->rx.atmvcc == NULL)) {
1647		read_unlock(&vcc_sklist_lock);
1648		DPRINTK("(itf %d) got service entry 0x%X for non-RX "
1649		    "vcc %d\n", lanai->number, (unsigned int) s, vci);
1650		lanai->stats.service_norx++;
1651		return 0;
1652	}
1653	if (unlikely(lvcc->rx.atmvcc->qos.aal != ATM_AAL5)) {
1654		read_unlock(&vcc_sklist_lock);
1655		DPRINTK("(itf %d) got RX service entry 0x%X for non-AAL5 "
1656		    "vcc %d\n", lanai->number, (unsigned int) s, vci);
1657		lanai->stats.service_rxnotaal5++;
1658		atomic_inc(&lvcc->rx.atmvcc->stats->rx_err);
1659		return 0;
1660	}
1661	if (likely(!(s & (SERVICE_TRASH | SERVICE_STREAM | SERVICE_CRCERR)))) {
1662		vcc_rx_aal5(lvcc, SERVICE_GET_END(s));
1663		read_unlock(&vcc_sklist_lock);
1664		return 0;
1665	}
1666	if (s & SERVICE_TRASH) {
1667		int bytes;
1668		read_unlock(&vcc_sklist_lock);
1669		DPRINTK("got trashed rx pdu on vci %d\n", vci);
1670		atomic_inc(&lvcc->rx.atmvcc->stats->rx_err);
1671		lvcc->stats.x.aal5.service_trash++;
1672		bytes = (SERVICE_GET_END(s) * 16) -
1673		    (((unsigned long) lvcc->rx.buf.ptr) -
1674		    ((unsigned long) lvcc->rx.buf.start)) + 47;
1675		if (bytes < 0)
1676			bytes += lanai_buf_size(&lvcc->rx.buf);
1677		lanai->stats.ovfl_trash += (bytes / 48);
1678		return 0;
1679	}
1680	if (s & SERVICE_STREAM) {
1681		read_unlock(&vcc_sklist_lock);
1682		atomic_inc(&lvcc->rx.atmvcc->stats->rx_err);
1683		lvcc->stats.x.aal5.service_stream++;
1684		printk(KERN_ERR DEV_LABEL "(itf %d): Got AAL5 stream "
1685		    "PDU on VCI %d!\n", lanai->number, vci);
1686		lanai_reset(lanai);
1687		return 0;
1688	}
1689	DPRINTK("got rx crc error on vci %d\n", vci);
1690	atomic_inc(&lvcc->rx.atmvcc->stats->rx_err);
1691	lvcc->stats.x.aal5.service_rxcrc++;
1692	lvcc->rx.buf.ptr = &lvcc->rx.buf.start[SERVICE_GET_END(s) * 4];
1693	cardvcc_write(lvcc, SERVICE_GET_END(s), vcc_rxreadptr);
1694	read_unlock(&vcc_sklist_lock);
1695	return 0;
1696}
1697
1698/* Try transmitting on all VCIs that we marked ready to serve */
1699static void iter_transmit(struct lanai_dev *lanai, vci_t vci)
1700{
1701	struct lanai_vcc *lvcc = lanai->vccs[vci];
1702	if (vcc_is_backlogged(lvcc))
1703		lvcc->tx.unqueue(lanai, lvcc, lvcc->tx.endptr);
1704}
1705
1706/* Run service queue -- called from interrupt context or with
1707 * interrupts otherwise disabled and with the lanai->servicelock
1708 * lock held
1709 */
1710static void run_service(struct lanai_dev *lanai)
1711{
1712	int ntx = 0;
1713	u32 wreg = reg_read(lanai, ServWrite_Reg);
1714	const u32 *end = lanai->service.start + wreg;
1715	while (lanai->service.ptr != end) {
1716		ntx += handle_service(lanai,
1717		    le32_to_cpup(lanai->service.ptr++));
1718		if (lanai->service.ptr >= lanai->service.end)
1719			lanai->service.ptr = lanai->service.start;
1720	}
1721	reg_write(lanai, wreg, ServRead_Reg);
1722	if (ntx != 0) {
1723		read_lock(&vcc_sklist_lock);
1724		vci_bitfield_iterate(lanai, lanai->transmit_ready,
1725		    iter_transmit);
1726		bitmap_zero(lanai->transmit_ready, NUM_VCI);
1727		read_unlock(&vcc_sklist_lock);
1728	}
1729}
1730
1731/* -------------------- GATHER STATISTICS: */
1732
1733static void get_statistics(struct lanai_dev *lanai)
1734{
1735	u32 statreg = reg_read(lanai, Statistics_Reg);
1736	lanai->stats.atm_ovfl += STATS_GET_FIFO_OVFL(statreg);
1737	lanai->stats.hec_err += STATS_GET_HEC_ERR(statreg);
1738	lanai->stats.vci_trash += STATS_GET_BAD_VCI(statreg);
1739	lanai->stats.ovfl_trash += STATS_GET_BUF_OVFL(statreg);
1740}
1741
1742/* -------------------- POLLING TIMER: */
1743
1744#ifndef DEBUG_RW
1745/* Try to undequeue 1 backlogged vcc */
1746static void iter_dequeue(struct lanai_dev *lanai, vci_t vci)
1747{
1748	struct lanai_vcc *lvcc = lanai->vccs[vci];
1749	int endptr;
1750	if (lvcc == NULL || lvcc->tx.atmvcc == NULL ||
1751	    !vcc_is_backlogged(lvcc)) {
1752		__clear_bit(vci, lanai->backlog_vccs);
1753		return;
1754	}
1755	endptr = TXREADPTR_GET_PTR(cardvcc_read(lvcc, vcc_txreadptr));
1756	lvcc->tx.unqueue(lanai, lvcc, endptr);
1757}
1758#endif /* !DEBUG_RW */
1759
1760static void lanai_timed_poll(struct timer_list *t)
1761{
1762	struct lanai_dev *lanai = from_timer(lanai, t, timer);
1763#ifndef DEBUG_RW
1764	unsigned long flags;
1765#ifdef USE_POWERDOWN
1766	if (lanai->conf1 & CONFIG1_POWERDOWN)
1767		return;
1768#endif /* USE_POWERDOWN */
1769	local_irq_save(flags);
1770	/* If we can grab the spinlock, check if any services need to be run */
1771	if (spin_trylock(&lanai->servicelock)) {
1772		run_service(lanai);
1773		spin_unlock(&lanai->servicelock);
1774	}
1775	/* ...and see if any backlogged VCs can make progress */
1776	/* unfortunately linux has no read_trylock() currently */
1777	read_lock(&vcc_sklist_lock);
1778	vci_bitfield_iterate(lanai, lanai->backlog_vccs, iter_dequeue);
1779	read_unlock(&vcc_sklist_lock);
1780	local_irq_restore(flags);
1781
1782	get_statistics(lanai);
1783#endif /* !DEBUG_RW */
1784	mod_timer(&lanai->timer, jiffies + LANAI_POLL_PERIOD);
1785}
1786
1787static inline void lanai_timed_poll_start(struct lanai_dev *lanai)
1788{
1789	timer_setup(&lanai->timer, lanai_timed_poll, 0);
1790	lanai->timer.expires = jiffies + LANAI_POLL_PERIOD;
 
 
1791	add_timer(&lanai->timer);
1792}
1793
1794static inline void lanai_timed_poll_stop(struct lanai_dev *lanai)
1795{
1796	del_timer_sync(&lanai->timer);
1797}
1798
1799/* -------------------- INTERRUPT SERVICE: */
1800
1801static inline void lanai_int_1(struct lanai_dev *lanai, u32 reason)
1802{
1803	u32 ack = 0;
1804	if (reason & INT_SERVICE) {
1805		ack = INT_SERVICE;
1806		spin_lock(&lanai->servicelock);
1807		run_service(lanai);
1808		spin_unlock(&lanai->servicelock);
1809	}
1810	if (reason & (INT_AAL0_STR | INT_AAL0)) {
1811		ack |= reason & (INT_AAL0_STR | INT_AAL0);
1812		vcc_rx_aal0(lanai);
1813	}
1814	/* The rest of the interrupts are pretty rare */
1815	if (ack == reason)
1816		goto done;
1817	if (reason & INT_STATS) {
1818		reason &= ~INT_STATS;	/* No need to ack */
1819		get_statistics(lanai);
1820	}
1821	if (reason & INT_STATUS) {
1822		ack |= reason & INT_STATUS;
1823		lanai_check_status(lanai);
1824	}
1825	if (unlikely(reason & INT_DMASHUT)) {
1826		printk(KERN_ERR DEV_LABEL "(itf %d): driver error - DMA "
1827		    "shutdown, reason=0x%08X, address=0x%08X\n",
1828		    lanai->number, (unsigned int) (reason & INT_DMASHUT),
1829		    (unsigned int) reg_read(lanai, DMA_Addr_Reg));
1830		if (reason & INT_TABORTBM) {
1831			lanai_reset(lanai);
1832			return;
1833		}
1834		ack |= (reason & INT_DMASHUT);
1835		printk(KERN_ERR DEV_LABEL "(itf %d): re-enabling DMA\n",
1836		    lanai->number);
1837		conf1_write(lanai);
1838		lanai->stats.dma_reenable++;
1839		pcistatus_check(lanai, 0);
1840	}
1841	if (unlikely(reason & INT_TABORTSENT)) {
1842		ack |= (reason & INT_TABORTSENT);
1843		printk(KERN_ERR DEV_LABEL "(itf %d): sent PCI target abort\n",
1844		    lanai->number);
1845		pcistatus_check(lanai, 0);
1846	}
1847	if (unlikely(reason & INT_SEGSHUT)) {
1848		printk(KERN_ERR DEV_LABEL "(itf %d): driver error - "
1849		    "segmentation shutdown, reason=0x%08X\n", lanai->number,
1850		    (unsigned int) (reason & INT_SEGSHUT));
1851		lanai_reset(lanai);
1852		return;
1853	}
1854	if (unlikely(reason & (INT_PING | INT_WAKE))) {
1855		printk(KERN_ERR DEV_LABEL "(itf %d): driver error - "
1856		    "unexpected interrupt 0x%08X, resetting\n",
1857		    lanai->number,
1858		    (unsigned int) (reason & (INT_PING | INT_WAKE)));
1859		lanai_reset(lanai);
1860		return;
1861	}
1862#ifdef DEBUG
1863	if (unlikely(ack != reason)) {
1864		DPRINTK("unacked ints: 0x%08X\n",
1865		    (unsigned int) (reason & ~ack));
1866		ack = reason;
1867	}
1868#endif
1869   done:
1870	if (ack != 0)
1871		reg_write(lanai, ack, IntAck_Reg);
1872}
1873
1874static irqreturn_t lanai_int(int irq, void *devid)
1875{
1876	struct lanai_dev *lanai = devid;
1877	u32 reason;
1878
1879#ifdef USE_POWERDOWN
1880	/*
1881	 * If we're powered down we shouldn't be generating any interrupts -
1882	 * so assume that this is a shared interrupt line and it's for someone
1883	 * else
1884	 */
1885	if (unlikely(lanai->conf1 & CONFIG1_POWERDOWN))
1886		return IRQ_NONE;
1887#endif
1888
1889	reason = intr_pending(lanai);
1890	if (reason == 0)
1891		return IRQ_NONE;	/* Must be for someone else */
1892
1893	do {
1894		if (unlikely(reason == 0xFFFFFFFF))
1895			break;		/* Maybe we've been unplugged? */
1896		lanai_int_1(lanai, reason);
1897		reason = intr_pending(lanai);
1898	} while (reason != 0);
1899
1900	return IRQ_HANDLED;
1901}
1902
1903/* TODO - it would be nice if we could use the "delayed interrupt" system
1904 *   to some advantage
1905 */
1906
1907/* -------------------- CHECK BOARD ID/REV: */
1908
1909/*
1910 * The board id and revision are stored both in the reset register and
1911 * in the PCI configuration space - the documentation says to check
1912 * each of them.  If revp!=NULL we store the revision there
1913 */
1914static int check_board_id_and_rev(const char *name, u32 val, int *revp)
1915{
1916	DPRINTK("%s says board_id=%d, board_rev=%d\n", name,
1917		(int) RESET_GET_BOARD_ID(val),
1918		(int) RESET_GET_BOARD_REV(val));
1919	if (RESET_GET_BOARD_ID(val) != BOARD_ID_LANAI256) {
1920		printk(KERN_ERR DEV_LABEL ": Found %s board-id %d -- not a "
1921		    "Lanai 25.6\n", name, (int) RESET_GET_BOARD_ID(val));
1922		return -ENODEV;
1923	}
1924	if (revp != NULL)
1925		*revp = RESET_GET_BOARD_REV(val);
1926	return 0;
1927}
1928
1929/* -------------------- PCI INITIALIZATION/SHUTDOWN: */
1930
1931static int lanai_pci_start(struct lanai_dev *lanai)
1932{
1933	struct pci_dev *pci = lanai->pci;
1934	int result;
1935
1936	if (pci_enable_device(pci) != 0) {
1937		printk(KERN_ERR DEV_LABEL "(itf %d): can't enable "
1938		    "PCI device", lanai->number);
1939		return -ENXIO;
1940	}
1941	pci_set_master(pci);
1942	if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32)) != 0) {
1943		printk(KERN_WARNING DEV_LABEL
1944		    "(itf %d): No suitable DMA available.\n", lanai->number);
1945		return -EBUSY;
1946	}
1947	result = check_board_id_and_rev("PCI", pci->subsystem_device, NULL);
1948	if (result != 0)
1949		return result;
1950	/* Set latency timer to zero as per lanai docs */
1951	result = pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0);
1952	if (result != PCIBIOS_SUCCESSFUL) {
1953		printk(KERN_ERR DEV_LABEL "(itf %d): can't write "
1954		    "PCI_LATENCY_TIMER: %d\n", lanai->number, result);
1955		return -EINVAL;
1956	}
1957	pcistatus_check(lanai, 1);
1958	pcistatus_check(lanai, 0);
1959	return 0;
1960}
1961
1962/* -------------------- VPI/VCI ALLOCATION: */
1963
1964/*
1965 * We _can_ use VCI==0 for normal traffic, but only for UBR (or we'll
1966 * get a CBRZERO interrupt), and we can use it only if no one is receiving
1967 * AAL0 traffic (since they will use the same queue) - according to the
1968 * docs we shouldn't even use it for AAL0 traffic
1969 */
1970static inline int vci0_is_ok(struct lanai_dev *lanai,
1971	const struct atm_qos *qos)
1972{
1973	if (qos->txtp.traffic_class == ATM_CBR || qos->aal == ATM_AAL0)
1974		return 0;
1975	if (qos->rxtp.traffic_class != ATM_NONE) {
1976		if (lanai->naal0 != 0)
1977			return 0;
1978		lanai->conf2 |= CONFIG2_VCI0_NORMAL;
1979		conf2_write_if_powerup(lanai);
1980	}
1981	return 1;
1982}
1983
1984/* return true if vci is currently unused, or if requested qos is
1985 * compatible
1986 */
1987static int vci_is_ok(struct lanai_dev *lanai, vci_t vci,
1988	const struct atm_vcc *atmvcc)
1989{
1990	const struct atm_qos *qos = &atmvcc->qos;
1991	const struct lanai_vcc *lvcc = lanai->vccs[vci];
1992	if (vci == 0 && !vci0_is_ok(lanai, qos))
1993		return 0;
1994	if (unlikely(lvcc != NULL)) {
1995		if (qos->rxtp.traffic_class != ATM_NONE &&
1996		    lvcc->rx.atmvcc != NULL && lvcc->rx.atmvcc != atmvcc)
1997			return 0;
1998		if (qos->txtp.traffic_class != ATM_NONE &&
1999		    lvcc->tx.atmvcc != NULL && lvcc->tx.atmvcc != atmvcc)
2000			return 0;
2001		if (qos->txtp.traffic_class == ATM_CBR &&
2002		    lanai->cbrvcc != NULL && lanai->cbrvcc != atmvcc)
2003			return 0;
2004	}
2005	if (qos->aal == ATM_AAL0 && lanai->naal0 == 0 &&
2006	    qos->rxtp.traffic_class != ATM_NONE) {
2007		const struct lanai_vcc *vci0 = lanai->vccs[0];
2008		if (vci0 != NULL && vci0->rx.atmvcc != NULL)
2009			return 0;
2010		lanai->conf2 &= ~CONFIG2_VCI0_NORMAL;
2011		conf2_write_if_powerup(lanai);
2012	}
2013	return 1;
2014}
2015
2016static int lanai_normalize_ci(struct lanai_dev *lanai,
2017	const struct atm_vcc *atmvcc, short *vpip, vci_t *vcip)
2018{
2019	switch (*vpip) {
2020		case ATM_VPI_ANY:
2021			*vpip = 0;
2022			fallthrough;
2023		case 0:
2024			break;
2025		default:
2026			return -EADDRINUSE;
2027	}
2028	switch (*vcip) {
2029		case ATM_VCI_ANY:
2030			for (*vcip = ATM_NOT_RSV_VCI; *vcip < lanai->num_vci;
2031			    (*vcip)++)
2032				if (vci_is_ok(lanai, *vcip, atmvcc))
2033					return 0;
2034			return -EADDRINUSE;
2035		default:
2036			if (*vcip >= lanai->num_vci || *vcip < 0 ||
2037			    !vci_is_ok(lanai, *vcip, atmvcc))
2038				return -EADDRINUSE;
2039	}
2040	return 0;
2041}
2042
2043/* -------------------- MANAGE CBR: */
2044
2045/*
2046 * CBR ICG is stored as a fixed-point number with 4 fractional bits.
2047 * Note that storing a number greater than 2046.0 will result in
2048 * incorrect shaping
2049 */
2050#define CBRICG_FRAC_BITS	(4)
2051#define CBRICG_MAX		(2046 << CBRICG_FRAC_BITS)
2052
2053/*
2054 * ICG is related to PCR with the formula PCR = MAXPCR / (ICG + 1)
2055 * where MAXPCR is (according to the docs) 25600000/(54*8),
2056 * which is equal to (3125<<9)/27.
2057 *
2058 * Solving for ICG, we get:
2059 *    ICG = MAXPCR/PCR - 1
2060 *    ICG = (3125<<9)/(27*PCR) - 1
2061 *    ICG = ((3125<<9) - (27*PCR)) / (27*PCR)
2062 *
2063 * The end result is supposed to be a fixed-point number with FRAC_BITS
2064 * bits of a fractional part, so we keep everything in the numerator
2065 * shifted by that much as we compute
2066 *
2067 */
2068static int pcr_to_cbricg(const struct atm_qos *qos)
2069{
2070	int rounddown = 0;	/* 1 = Round PCR down, i.e. round ICG _up_ */
2071	int x, icg, pcr = atm_pcr_goal(&qos->txtp);
2072	if (pcr == 0)		/* Use maximum bandwidth */
2073		return 0;
2074	if (pcr < 0) {
2075		rounddown = 1;
2076		pcr = -pcr;
2077	}
2078	x = pcr * 27;
2079	icg = (3125 << (9 + CBRICG_FRAC_BITS)) - (x << CBRICG_FRAC_BITS);
2080	if (rounddown)
2081		icg += x - 1;
2082	icg /= x;
2083	if (icg > CBRICG_MAX)
2084		icg = CBRICG_MAX;
2085	DPRINTK("pcr_to_cbricg: pcr=%d rounddown=%c icg=%d\n",
2086	    pcr, rounddown ? 'Y' : 'N', icg);
2087	return icg;
2088}
2089
2090static inline void lanai_cbr_setup(struct lanai_dev *lanai)
2091{
2092	reg_write(lanai, pcr_to_cbricg(&lanai->cbrvcc->qos), CBR_ICG_Reg);
2093	reg_write(lanai, lanai->cbrvcc->vci, CBR_PTR_Reg);
2094	lanai->conf2 |= CONFIG2_CBR_ENABLE;
2095	conf2_write(lanai);
2096}
2097
2098static inline void lanai_cbr_shutdown(struct lanai_dev *lanai)
2099{
2100	lanai->conf2 &= ~CONFIG2_CBR_ENABLE;
2101	conf2_write(lanai);
2102}
2103
2104/* -------------------- OPERATIONS: */
2105
2106/* setup a newly detected device */
2107static int lanai_dev_open(struct atm_dev *atmdev)
2108{
2109	struct lanai_dev *lanai = (struct lanai_dev *) atmdev->dev_data;
2110	unsigned long raw_base;
2111	int result;
2112
2113	DPRINTK("In lanai_dev_open()\n");
2114	/* Basic device fields */
2115	lanai->number = atmdev->number;
2116	lanai->num_vci = NUM_VCI;
2117	bitmap_zero(lanai->backlog_vccs, NUM_VCI);
2118	bitmap_zero(lanai->transmit_ready, NUM_VCI);
2119	lanai->naal0 = 0;
2120#ifdef USE_POWERDOWN
2121	lanai->nbound = 0;
2122#endif
2123	lanai->cbrvcc = NULL;
2124	memset(&lanai->stats, 0, sizeof lanai->stats);
2125	spin_lock_init(&lanai->endtxlock);
2126	spin_lock_init(&lanai->servicelock);
2127	atmdev->ci_range.vpi_bits = 0;
2128	atmdev->ci_range.vci_bits = 0;
2129	while (1 << atmdev->ci_range.vci_bits < lanai->num_vci)
2130		atmdev->ci_range.vci_bits++;
2131	atmdev->link_rate = ATM_25_PCR;
2132
2133	/* 3.2: PCI initialization */
2134	if ((result = lanai_pci_start(lanai)) != 0)
2135		goto error;
2136	raw_base = lanai->pci->resource[0].start;
2137	lanai->base = (bus_addr_t) ioremap(raw_base, LANAI_MAPPING_SIZE);
2138	if (lanai->base == NULL) {
2139		printk(KERN_ERR DEV_LABEL ": couldn't remap I/O space\n");
2140		result = -ENOMEM;
2141		goto error_pci;
2142	}
2143	/* 3.3: Reset lanai and PHY */
2144	reset_board(lanai);
2145	lanai->conf1 = reg_read(lanai, Config1_Reg);
2146	lanai->conf1 &= ~(CONFIG1_GPOUT1 | CONFIG1_POWERDOWN |
2147	    CONFIG1_MASK_LEDMODE);
2148	lanai->conf1 |= CONFIG1_SET_LEDMODE(LEDMODE_NOT_SOOL);
2149	reg_write(lanai, lanai->conf1 | CONFIG1_GPOUT1, Config1_Reg);
2150	udelay(1000);
2151	conf1_write(lanai);
2152
2153	/*
2154	 * 3.4: Turn on endian mode for big-endian hardware
2155	 *   We don't actually want to do this - the actual bit fields
2156	 *   in the endian register are not documented anywhere.
2157	 *   Instead we do the bit-flipping ourselves on big-endian
2158	 *   hardware.
2159	 *
2160	 * 3.5: get the board ID/rev by reading the reset register
2161	 */
2162	result = check_board_id_and_rev("register",
2163	    reg_read(lanai, Reset_Reg), &lanai->board_rev);
2164	if (result != 0)
2165		goto error_unmap;
2166
2167	/* 3.6: read EEPROM */
2168	if ((result = eeprom_read(lanai)) != 0)
2169		goto error_unmap;
2170	if ((result = eeprom_validate(lanai)) != 0)
2171		goto error_unmap;
2172
2173	/* 3.7: re-reset PHY, do loopback tests, setup PHY */
2174	reg_write(lanai, lanai->conf1 | CONFIG1_GPOUT1, Config1_Reg);
2175	udelay(1000);
2176	conf1_write(lanai);
2177	/* TODO - loopback tests */
2178	lanai->conf1 |= (CONFIG1_GPOUT2 | CONFIG1_GPOUT3 | CONFIG1_DMA_ENABLE);
2179	conf1_write(lanai);
2180
2181	/* 3.8/3.9: test and initialize card SRAM */
2182	if ((result = sram_test_and_clear(lanai)) != 0)
2183		goto error_unmap;
2184
2185	/* 3.10: initialize lanai registers */
2186	lanai->conf1 |= CONFIG1_DMA_ENABLE;
2187	conf1_write(lanai);
2188	if ((result = service_buffer_allocate(lanai)) != 0)
2189		goto error_unmap;
2190	if ((result = vcc_table_allocate(lanai)) != 0)
2191		goto error_service;
2192	lanai->conf2 = (lanai->num_vci >= 512 ? CONFIG2_HOWMANY : 0) |
2193	    CONFIG2_HEC_DROP |	/* ??? */ CONFIG2_PTI7_MODE;
2194	conf2_write(lanai);
2195	reg_write(lanai, TX_FIFO_DEPTH, TxDepth_Reg);
2196	reg_write(lanai, 0, CBR_ICG_Reg);	/* CBR defaults to no limit */
2197	if ((result = request_irq(lanai->pci->irq, lanai_int, IRQF_SHARED,
2198	    DEV_LABEL, lanai)) != 0) {
2199		printk(KERN_ERR DEV_LABEL ": can't allocate interrupt\n");
2200		goto error_vcctable;
2201	}
2202	mb();				/* Make sure that all that made it */
2203	intr_enable(lanai, INT_ALL & ~(INT_PING | INT_WAKE));
2204	/* 3.11: initialize loop mode (i.e. turn looping off) */
2205	lanai->conf1 = (lanai->conf1 & ~CONFIG1_MASK_LOOPMODE) |
2206	    CONFIG1_SET_LOOPMODE(LOOPMODE_NORMAL) |
2207	    CONFIG1_GPOUT2 | CONFIG1_GPOUT3;
2208	conf1_write(lanai);
2209	lanai->status = reg_read(lanai, Status_Reg);
2210	/* We're now done initializing this card */
2211#ifdef USE_POWERDOWN
2212	lanai->conf1 |= CONFIG1_POWERDOWN;
2213	conf1_write(lanai);
2214#endif
2215	memcpy(atmdev->esi, eeprom_mac(lanai), ESI_LEN);
2216	lanai_timed_poll_start(lanai);
2217	printk(KERN_NOTICE DEV_LABEL "(itf %d): rev.%d, base=%p, irq=%u "
2218		"(%pMF)\n", lanai->number, (int) lanai->pci->revision,
2219		lanai->base, lanai->pci->irq, atmdev->esi);
2220	printk(KERN_NOTICE DEV_LABEL "(itf %d): LANAI%s, serialno=%u(0x%X), "
2221	    "board_rev=%d\n", lanai->number,
2222	    lanai->type==lanai2 ? "2" : "HB", (unsigned int) lanai->serialno,
2223	    (unsigned int) lanai->serialno, lanai->board_rev);
2224	return 0;
2225
2226    error_vcctable:
2227	vcc_table_deallocate(lanai);
2228    error_service:
2229	service_buffer_deallocate(lanai);
2230    error_unmap:
2231	reset_board(lanai);
2232#ifdef USE_POWERDOWN
2233	lanai->conf1 = reg_read(lanai, Config1_Reg) | CONFIG1_POWERDOWN;
2234	conf1_write(lanai);
2235#endif
2236	iounmap(lanai->base);
2237    error_pci:
2238	pci_disable_device(lanai->pci);
2239    error:
2240	return result;
2241}
2242
2243/* called when device is being shutdown, and all vcc's are gone - higher
2244 * levels will deallocate the atm device for us
2245 */
2246static void lanai_dev_close(struct atm_dev *atmdev)
2247{
2248	struct lanai_dev *lanai = (struct lanai_dev *) atmdev->dev_data;
2249	printk(KERN_INFO DEV_LABEL "(itf %d): shutting down interface\n",
2250	    lanai->number);
2251	lanai_timed_poll_stop(lanai);
2252#ifdef USE_POWERDOWN
2253	lanai->conf1 = reg_read(lanai, Config1_Reg) & ~CONFIG1_POWERDOWN;
2254	conf1_write(lanai);
2255#endif
2256	intr_disable(lanai, INT_ALL);
2257	free_irq(lanai->pci->irq, lanai);
2258	reset_board(lanai);
2259#ifdef USE_POWERDOWN
2260	lanai->conf1 |= CONFIG1_POWERDOWN;
2261	conf1_write(lanai);
2262#endif
2263	pci_disable_device(lanai->pci);
2264	vcc_table_deallocate(lanai);
2265	service_buffer_deallocate(lanai);
2266	iounmap(lanai->base);
2267	kfree(lanai);
2268}
2269
2270/* close a vcc */
2271static void lanai_close(struct atm_vcc *atmvcc)
2272{
2273	struct lanai_vcc *lvcc = (struct lanai_vcc *) atmvcc->dev_data;
2274	struct lanai_dev *lanai = (struct lanai_dev *) atmvcc->dev->dev_data;
2275	if (lvcc == NULL)
2276		return;
2277	clear_bit(ATM_VF_READY, &atmvcc->flags);
2278	clear_bit(ATM_VF_PARTIAL, &atmvcc->flags);
2279	if (lvcc->rx.atmvcc == atmvcc) {
2280		lanai_shutdown_rx_vci(lvcc);
2281		if (atmvcc->qos.aal == ATM_AAL0) {
2282			if (--lanai->naal0 <= 0)
2283				aal0_buffer_free(lanai);
2284		} else
2285			lanai_buf_deallocate(&lvcc->rx.buf, lanai->pci);
2286		lvcc->rx.atmvcc = NULL;
2287	}
2288	if (lvcc->tx.atmvcc == atmvcc) {
2289		if (atmvcc == lanai->cbrvcc) {
2290			if (lvcc->vbase != NULL)
2291				lanai_cbr_shutdown(lanai);
2292			lanai->cbrvcc = NULL;
2293		}
2294		lanai_shutdown_tx_vci(lanai, lvcc);
2295		lanai_buf_deallocate(&lvcc->tx.buf, lanai->pci);
2296		lvcc->tx.atmvcc = NULL;
2297	}
2298	if (--lvcc->nref == 0) {
2299		host_vcc_unbind(lanai, lvcc);
2300		kfree(lvcc);
2301	}
2302	atmvcc->dev_data = NULL;
2303	clear_bit(ATM_VF_ADDR, &atmvcc->flags);
2304}
2305
2306/* open a vcc on the card to vpi/vci */
2307static int lanai_open(struct atm_vcc *atmvcc)
2308{
2309	struct lanai_dev *lanai;
2310	struct lanai_vcc *lvcc;
2311	int result = 0;
2312	int vci = atmvcc->vci;
2313	short vpi = atmvcc->vpi;
2314	/* we don't support partial open - it's not really useful anyway */
2315	if ((test_bit(ATM_VF_PARTIAL, &atmvcc->flags)) ||
2316	    (vpi == ATM_VPI_UNSPEC) || (vci == ATM_VCI_UNSPEC))
2317		return -EINVAL;
2318	lanai = (struct lanai_dev *) atmvcc->dev->dev_data;
2319	result = lanai_normalize_ci(lanai, atmvcc, &vpi, &vci);
2320	if (unlikely(result != 0))
2321		goto out;
2322	set_bit(ATM_VF_ADDR, &atmvcc->flags);
2323	if (atmvcc->qos.aal != ATM_AAL0 && atmvcc->qos.aal != ATM_AAL5)
2324		return -EINVAL;
2325	DPRINTK(DEV_LABEL "(itf %d): open %d.%d\n", lanai->number,
2326	    (int) vpi, vci);
2327	lvcc = lanai->vccs[vci];
2328	if (lvcc == NULL) {
2329		lvcc = new_lanai_vcc();
2330		if (unlikely(lvcc == NULL))
2331			return -ENOMEM;
2332		atmvcc->dev_data = lvcc;
2333	}
2334	lvcc->nref++;
2335	if (atmvcc->qos.rxtp.traffic_class != ATM_NONE) {
2336		APRINTK(lvcc->rx.atmvcc == NULL, "rx.atmvcc!=NULL, vci=%d\n",
2337		    vci);
2338		if (atmvcc->qos.aal == ATM_AAL0) {
2339			if (lanai->naal0 == 0)
2340				result = aal0_buffer_allocate(lanai);
2341		} else
2342			result = lanai_setup_rx_vci_aal5(
2343			    lanai, lvcc, &atmvcc->qos);
2344		if (unlikely(result != 0))
2345			goto out_free;
2346		lvcc->rx.atmvcc = atmvcc;
2347		lvcc->stats.rx_nomem = 0;
2348		lvcc->stats.x.aal5.rx_badlen = 0;
2349		lvcc->stats.x.aal5.service_trash = 0;
2350		lvcc->stats.x.aal5.service_stream = 0;
2351		lvcc->stats.x.aal5.service_rxcrc = 0;
2352		if (atmvcc->qos.aal == ATM_AAL0)
2353			lanai->naal0++;
2354	}
2355	if (atmvcc->qos.txtp.traffic_class != ATM_NONE) {
2356		APRINTK(lvcc->tx.atmvcc == NULL, "tx.atmvcc!=NULL, vci=%d\n",
2357		    vci);
2358		result = lanai_setup_tx_vci(lanai, lvcc, &atmvcc->qos);
2359		if (unlikely(result != 0))
2360			goto out_free;
2361		lvcc->tx.atmvcc = atmvcc;
2362		if (atmvcc->qos.txtp.traffic_class == ATM_CBR) {
2363			APRINTK(lanai->cbrvcc == NULL,
2364			    "cbrvcc!=NULL, vci=%d\n", vci);
2365			lanai->cbrvcc = atmvcc;
2366		}
2367	}
2368	host_vcc_bind(lanai, lvcc, vci);
2369	/*
2370	 * Make sure everything made it to RAM before we tell the card about
2371	 * the VCC
2372	 */
2373	wmb();
2374	if (atmvcc == lvcc->rx.atmvcc)
2375		host_vcc_start_rx(lvcc);
2376	if (atmvcc == lvcc->tx.atmvcc) {
2377		host_vcc_start_tx(lvcc);
2378		if (lanai->cbrvcc == atmvcc)
2379			lanai_cbr_setup(lanai);
2380	}
2381	set_bit(ATM_VF_READY, &atmvcc->flags);
2382	return 0;
2383    out_free:
2384	lanai_close(atmvcc);
2385    out:
2386	return result;
2387}
2388
2389static int lanai_send(struct atm_vcc *atmvcc, struct sk_buff *skb)
2390{
2391	struct lanai_vcc *lvcc = (struct lanai_vcc *) atmvcc->dev_data;
2392	struct lanai_dev *lanai = (struct lanai_dev *) atmvcc->dev->dev_data;
2393	unsigned long flags;
2394	if (unlikely(lvcc == NULL || lvcc->vbase == NULL ||
2395	      lvcc->tx.atmvcc != atmvcc))
2396		goto einval;
2397#ifdef DEBUG
2398	if (unlikely(skb == NULL)) {
2399		DPRINTK("lanai_send: skb==NULL for vci=%d\n", atmvcc->vci);
2400		goto einval;
2401	}
2402	if (unlikely(lanai == NULL)) {
2403		DPRINTK("lanai_send: lanai==NULL for vci=%d\n", atmvcc->vci);
2404		goto einval;
2405	}
2406#endif
2407	ATM_SKB(skb)->vcc = atmvcc;
2408	switch (atmvcc->qos.aal) {
2409		case ATM_AAL5:
2410			read_lock_irqsave(&vcc_sklist_lock, flags);
2411			vcc_tx_aal5(lanai, lvcc, skb);
2412			read_unlock_irqrestore(&vcc_sklist_lock, flags);
2413			return 0;
2414		case ATM_AAL0:
2415			if (unlikely(skb->len != ATM_CELL_SIZE-1))
2416				goto einval;
2417  /* NOTE - this next line is technically invalid - we haven't unshared skb */
2418			cpu_to_be32s((u32 *) skb->data);
2419			read_lock_irqsave(&vcc_sklist_lock, flags);
2420			vcc_tx_aal0(lanai, lvcc, skb);
2421			read_unlock_irqrestore(&vcc_sklist_lock, flags);
2422			return 0;
2423	}
2424	DPRINTK("lanai_send: bad aal=%d on vci=%d\n", (int) atmvcc->qos.aal,
2425	    atmvcc->vci);
2426    einval:
2427	lanai_free_skb(atmvcc, skb);
2428	return -EINVAL;
2429}
2430
2431static int lanai_change_qos(struct atm_vcc *atmvcc,
2432	/*const*/ struct atm_qos *qos, int flags)
2433{
2434	return -EBUSY;		/* TODO: need to write this */
2435}
2436
2437#ifndef CONFIG_PROC_FS
2438#define lanai_proc_read NULL
2439#else
2440static int lanai_proc_read(struct atm_dev *atmdev, loff_t *pos, char *page)
2441{
2442	struct lanai_dev *lanai = (struct lanai_dev *) atmdev->dev_data;
2443	loff_t left = *pos;
2444	struct lanai_vcc *lvcc;
2445	if (left-- == 0)
2446		return sprintf(page, DEV_LABEL "(itf %d): chip=LANAI%s, "
2447		    "serial=%u, magic=0x%08X, num_vci=%d\n",
2448		    atmdev->number, lanai->type==lanai2 ? "2" : "HB",
2449		    (unsigned int) lanai->serialno,
2450		    (unsigned int) lanai->magicno, lanai->num_vci);
2451	if (left-- == 0)
2452		return sprintf(page, "revision: board=%d, pci_if=%d\n",
2453		    lanai->board_rev, (int) lanai->pci->revision);
2454	if (left-- == 0)
2455		return sprintf(page, "EEPROM ESI: %pM\n",
2456		    &lanai->eeprom[EEPROM_MAC]);
2457	if (left-- == 0)
2458		return sprintf(page, "status: SOOL=%d, LOCD=%d, LED=%d, "
2459		    "GPIN=%d\n", (lanai->status & STATUS_SOOL) ? 1 : 0,
2460		    (lanai->status & STATUS_LOCD) ? 1 : 0,
2461		    (lanai->status & STATUS_LED) ? 1 : 0,
2462		    (lanai->status & STATUS_GPIN) ? 1 : 0);
2463	if (left-- == 0)
2464		return sprintf(page, "global buffer sizes: service=%zu, "
2465		    "aal0_rx=%zu\n", lanai_buf_size(&lanai->service),
2466		    lanai->naal0 ? lanai_buf_size(&lanai->aal0buf) : 0);
2467	if (left-- == 0) {
2468		get_statistics(lanai);
2469		return sprintf(page, "cells in error: overflow=%u, "
2470		    "closed_vci=%u, bad_HEC=%u, rx_fifo=%u\n",
2471		    lanai->stats.ovfl_trash, lanai->stats.vci_trash,
2472		    lanai->stats.hec_err, lanai->stats.atm_ovfl);
2473	}
2474	if (left-- == 0)
2475		return sprintf(page, "PCI errors: parity_detect=%u, "
2476		    "master_abort=%u, master_target_abort=%u,\n",
2477		    lanai->stats.pcierr_parity_detect,
2478		    lanai->stats.pcierr_serr_set,
2479		    lanai->stats.pcierr_m_target_abort);
2480	if (left-- == 0)
2481		return sprintf(page, "            slave_target_abort=%u, "
2482		    "master_parity=%u\n", lanai->stats.pcierr_s_target_abort,
2483		    lanai->stats.pcierr_master_parity);
2484	if (left-- == 0)
2485		return sprintf(page, "                     no_tx=%u, "
2486		    "no_rx=%u, bad_rx_aal=%u\n", lanai->stats.service_norx,
2487		    lanai->stats.service_notx,
2488		    lanai->stats.service_rxnotaal5);
2489	if (left-- == 0)
2490		return sprintf(page, "resets: dma=%u, card=%u\n",
2491		    lanai->stats.dma_reenable, lanai->stats.card_reset);
2492	/* At this point, "left" should be the VCI we're looking for */
2493	read_lock(&vcc_sklist_lock);
2494	for (; ; left++) {
2495		if (left >= NUM_VCI) {
2496			left = 0;
2497			goto out;
2498		}
2499		if ((lvcc = lanai->vccs[left]) != NULL)
2500			break;
2501		(*pos)++;
2502	}
2503	/* Note that we re-use "left" here since we're done with it */
2504	left = sprintf(page, "VCI %4d: nref=%d, rx_nomem=%u",  (vci_t) left,
2505	    lvcc->nref, lvcc->stats.rx_nomem);
2506	if (lvcc->rx.atmvcc != NULL) {
2507		left += sprintf(&page[left], ",\n          rx_AAL=%d",
2508		    lvcc->rx.atmvcc->qos.aal == ATM_AAL5 ? 5 : 0);
2509		if (lvcc->rx.atmvcc->qos.aal == ATM_AAL5)
2510			left += sprintf(&page[left], ", rx_buf_size=%zu, "
2511			    "rx_bad_len=%u,\n          rx_service_trash=%u, "
2512			    "rx_service_stream=%u, rx_bad_crc=%u",
2513			    lanai_buf_size(&lvcc->rx.buf),
2514			    lvcc->stats.x.aal5.rx_badlen,
2515			    lvcc->stats.x.aal5.service_trash,
2516			    lvcc->stats.x.aal5.service_stream,
2517			    lvcc->stats.x.aal5.service_rxcrc);
2518	}
2519	if (lvcc->tx.atmvcc != NULL)
2520		left += sprintf(&page[left], ",\n          tx_AAL=%d, "
2521		    "tx_buf_size=%zu, tx_qos=%cBR, tx_backlogged=%c",
2522		    lvcc->tx.atmvcc->qos.aal == ATM_AAL5 ? 5 : 0,
2523		    lanai_buf_size(&lvcc->tx.buf),
2524		    lvcc->tx.atmvcc == lanai->cbrvcc ? 'C' : 'U',
2525		    vcc_is_backlogged(lvcc) ? 'Y' : 'N');
2526	page[left++] = '\n';
2527	page[left] = '\0';
2528    out:
2529	read_unlock(&vcc_sklist_lock);
2530	return left;
2531}
2532#endif /* CONFIG_PROC_FS */
2533
2534/* -------------------- HOOKS: */
2535
2536static const struct atmdev_ops ops = {
2537	.dev_close	= lanai_dev_close,
2538	.open		= lanai_open,
2539	.close		= lanai_close,
 
 
2540	.send		= lanai_send,
2541	.phy_put	= NULL,
2542	.phy_get	= NULL,
2543	.change_qos	= lanai_change_qos,
2544	.proc_read	= lanai_proc_read,
2545	.owner		= THIS_MODULE
2546};
2547
2548/* initialize one probed card */
2549static int lanai_init_one(struct pci_dev *pci,
2550			  const struct pci_device_id *ident)
2551{
2552	struct lanai_dev *lanai;
2553	struct atm_dev *atmdev;
2554	int result;
2555
2556	lanai = kmalloc(sizeof(*lanai), GFP_KERNEL);
2557	if (lanai == NULL) {
2558		printk(KERN_ERR DEV_LABEL
2559		       ": couldn't allocate dev_data structure!\n");
2560		return -ENOMEM;
2561	}
2562
2563	atmdev = atm_dev_register(DEV_LABEL, &pci->dev, &ops, -1, NULL);
2564	if (atmdev == NULL) {
2565		printk(KERN_ERR DEV_LABEL
2566		    ": couldn't register atm device!\n");
2567		kfree(lanai);
2568		return -EBUSY;
2569	}
2570
2571	atmdev->dev_data = lanai;
2572	lanai->pci = pci;
2573	lanai->type = (enum lanai_type) ident->device;
2574
2575	result = lanai_dev_open(atmdev);
2576	if (result != 0) {
2577		DPRINTK("lanai_start() failed, err=%d\n", -result);
2578		atm_dev_deregister(atmdev);
2579		kfree(lanai);
2580	}
2581	return result;
2582}
2583
2584static const struct pci_device_id lanai_pci_tbl[] = {
2585	{ PCI_VDEVICE(EF, PCI_DEVICE_ID_EF_ATM_LANAI2) },
2586	{ PCI_VDEVICE(EF, PCI_DEVICE_ID_EF_ATM_LANAIHB) },
2587	{ 0, }	/* terminal entry */
2588};
2589MODULE_DEVICE_TABLE(pci, lanai_pci_tbl);
2590
2591static struct pci_driver lanai_driver = {
2592	.name     = DEV_LABEL,
2593	.id_table = lanai_pci_tbl,
2594	.probe    = lanai_init_one,
2595};
2596
2597module_pci_driver(lanai_driver);
2598
2599MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
2600MODULE_DESCRIPTION("Efficient Networks Speedstream 3010 driver");
2601MODULE_LICENSE("GPL");
v4.10.11
 
   1/* lanai.c -- Copyright 1999-2003 by Mitchell Blank Jr <mitch@sfgoth.com>
   2 *
   3 *  This program is free software; you can redistribute it and/or
   4 *  modify it under the terms of the GNU General Public License
   5 *  as published by the Free Software Foundation; either version
   6 *  2 of the License, or (at your option) any later version.
   7 *
   8 * This driver supports ATM cards based on the Efficient "Lanai"
   9 * chipset such as the Speedstream 3010 and the ENI-25p.  The
  10 * Speedstream 3060 is currently not supported since we don't
  11 * have the code to drive the on-board Alcatel DSL chipset (yet).
  12 *
  13 * Thanks to Efficient for supporting this project with hardware,
  14 * documentation, and by answering my questions.
  15 *
  16 * Things not working yet:
  17 *
  18 * o  We don't support the Speedstream 3060 yet - this card has
  19 *    an on-board DSL modem chip by Alcatel and the driver will
  20 *    need some extra code added to handle it
  21 *
  22 * o  Note that due to limitations of the Lanai only one VCC can be
  23 *    in CBR at once
  24 *
  25 * o We don't currently parse the EEPROM at all.  The code is all
  26 *   there as per the spec, but it doesn't actually work.  I think
  27 *   there may be some issues with the docs.  Anyway, do NOT
  28 *   enable it yet - bugs in that code may actually damage your
  29 *   hardware!  Because of this you should hardware an ESI before
  30 *   trying to use this in a LANE or MPOA environment.
  31 *
  32 * o  AAL0 is stubbed in but the actual rx/tx path isn't written yet:
  33 *	vcc_tx_aal0() needs to send or queue a SKB
  34 *	vcc_tx_unqueue_aal0() needs to attempt to send queued SKBs
  35 *	vcc_rx_aal0() needs to handle AAL0 interrupts
  36 *    This isn't too much work - I just wanted to get other things
  37 *    done first.
  38 *
  39 * o  lanai_change_qos() isn't written yet
  40 *
  41 * o  There aren't any ioctl's yet -- I'd like to eventually support
  42 *    setting loopback and LED modes that way.
  43 *
  44 * o  If the segmentation engine or DMA gets shut down we should restart
  45 *    card as per section 17.0i.  (see lanai_reset)
  46 *
  47 * o setsockopt(SO_CIRANGE) isn't done (although despite what the
  48 *   API says it isn't exactly commonly implemented)
  49 */
  50
  51/* Version history:
  52 *   v.1.00 -- 26-JUL-2003 -- PCI/DMA updates
  53 *   v.0.02 -- 11-JAN-2000 -- Endian fixes
  54 *   v.0.01 -- 30-NOV-1999 -- Initial release
  55 */
  56
  57#include <linux/module.h>
  58#include <linux/slab.h>
  59#include <linux/mm.h>
  60#include <linux/atmdev.h>
  61#include <asm/io.h>
  62#include <asm/byteorder.h>
  63#include <linux/spinlock.h>
  64#include <linux/pci.h>
  65#include <linux/dma-mapping.h>
  66#include <linux/init.h>
  67#include <linux/delay.h>
  68#include <linux/interrupt.h>
  69
  70/* -------------------- TUNABLE PARAMATERS: */
  71
  72/*
  73 * Maximum number of VCIs per card.  Setting it lower could theoretically
  74 * save some memory, but since we allocate our vcc list with get_free_pages,
  75 * it's not really likely for most architectures
  76 */
  77#define NUM_VCI			(1024)
  78
  79/*
  80 * Enable extra debugging
  81 */
  82#define DEBUG
  83/*
  84 * Debug _all_ register operations with card, except the memory test.
  85 * Also disables the timed poll to prevent extra chattiness.  This
  86 * isn't for normal use
  87 */
  88#undef DEBUG_RW
  89
  90/*
  91 * The programming guide specifies a full test of the on-board SRAM
  92 * at initialization time.  Undefine to remove this
  93 */
  94#define FULL_MEMORY_TEST
  95
  96/*
  97 * This is the number of (4 byte) service entries that we will
  98 * try to allocate at startup.  Note that we will end up with
  99 * one PAGE_SIZE's worth regardless of what this is set to
 100 */
 101#define SERVICE_ENTRIES		(1024)
 102/* TODO: make above a module load-time option */
 103
 104/*
 105 * We normally read the onboard EEPROM in order to discover our MAC
 106 * address.  Undefine to _not_ do this
 107 */
 108/* #define READ_EEPROM */ /* ***DONT ENABLE YET*** */
 109/* TODO: make above a module load-time option (also) */
 110
 111/*
 112 * Depth of TX fifo (in 128 byte units; range 2-31)
 113 * Smaller numbers are better for network latency
 114 * Larger numbers are better for PCI latency
 115 * I'm really sure where the best tradeoff is, but the BSD driver uses
 116 * 7 and it seems to work ok.
 117 */
 118#define TX_FIFO_DEPTH		(7)
 119/* TODO: make above a module load-time option */
 120
 121/*
 122 * How often (in jiffies) we will try to unstick stuck connections -
 123 * shouldn't need to happen much
 124 */
 125#define LANAI_POLL_PERIOD	(10*HZ)
 126/* TODO: make above a module load-time option */
 127
 128/*
 129 * When allocating an AAL5 receiving buffer, try to make it at least
 130 * large enough to hold this many max_sdu sized PDUs
 131 */
 132#define AAL5_RX_MULTIPLIER	(3)
 133/* TODO: make above a module load-time option */
 134
 135/*
 136 * Same for transmitting buffer
 137 */
 138#define AAL5_TX_MULTIPLIER	(3)
 139/* TODO: make above a module load-time option */
 140
 141/*
 142 * When allocating an AAL0 transmiting buffer, how many cells should fit.
 143 * Remember we'll end up with a PAGE_SIZE of them anyway, so this isn't
 144 * really critical
 145 */
 146#define AAL0_TX_MULTIPLIER	(40)
 147/* TODO: make above a module load-time option */
 148
 149/*
 150 * How large should we make the AAL0 receiving buffer.  Remember that this
 151 * is shared between all AAL0 VC's
 152 */
 153#define AAL0_RX_BUFFER_SIZE	(PAGE_SIZE)
 154/* TODO: make above a module load-time option */
 155
 156/*
 157 * Should we use Lanai's "powerdown" feature when no vcc's are bound?
 158 */
 159/* #define USE_POWERDOWN */
 160/* TODO: make above a module load-time option (also) */
 161
 162/* -------------------- DEBUGGING AIDS: */
 163
 164#define DEV_LABEL "lanai"
 165
 166#ifdef DEBUG
 167
 168#define DPRINTK(format, args...) \
 169	printk(KERN_DEBUG DEV_LABEL ": " format, ##args)
 170#define APRINTK(truth, format, args...) \
 171	do { \
 172		if (unlikely(!(truth))) \
 173			printk(KERN_ERR DEV_LABEL ": " format, ##args); \
 174	} while (0)
 175
 176#else /* !DEBUG */
 177
 178#define DPRINTK(format, args...)
 179#define APRINTK(truth, format, args...)
 180
 181#endif /* DEBUG */
 182
 183#ifdef DEBUG_RW
 184#define RWDEBUG(format, args...) \
 185	printk(KERN_DEBUG DEV_LABEL ": " format, ##args)
 186#else /* !DEBUG_RW */
 187#define RWDEBUG(format, args...)
 188#endif
 189
 190/* -------------------- DATA DEFINITIONS: */
 191
 192#define LANAI_MAPPING_SIZE	(0x40000)
 193#define LANAI_EEPROM_SIZE	(128)
 194
 195typedef int vci_t;
 196typedef void __iomem *bus_addr_t;
 197
 198/* DMA buffer in host memory for TX, RX, or service list. */
 199struct lanai_buffer {
 200	u32 *start;	/* From get_free_pages */
 201	u32 *end;	/* One past last byte */
 202	u32 *ptr;	/* Pointer to current host location */
 203	dma_addr_t dmaaddr;
 204};
 205
 206struct lanai_vcc_stats {
 207	unsigned rx_nomem;
 208	union {
 209		struct {
 210			unsigned rx_badlen;
 211			unsigned service_trash;
 212			unsigned service_stream;
 213			unsigned service_rxcrc;
 214		} aal5;
 215		struct {
 216		} aal0;
 217	} x;
 218};
 219
 220struct lanai_dev;			/* Forward declaration */
 221
 222/*
 223 * This is the card-specific per-vcc data.  Note that unlike some other
 224 * drivers there is NOT a 1-to-1 correspondance between these and
 225 * atm_vcc's - each one of these represents an actual 2-way vcc, but
 226 * an atm_vcc can be 1-way and share with a 1-way vcc in the other
 227 * direction.  To make it weirder, there can even be 0-way vccs
 228 * bound to us, waiting to do a change_qos
 229 */
 230struct lanai_vcc {
 231	bus_addr_t vbase;		/* Base of VCC's registers */
 232	struct lanai_vcc_stats stats;
 233	int nref;			/* # of atm_vcc's who reference us */
 234	vci_t vci;
 235	struct {
 236		struct lanai_buffer buf;
 237		struct atm_vcc *atmvcc;	/* atm_vcc who is receiver */
 238	} rx;
 239	struct {
 240		struct lanai_buffer buf;
 241		struct atm_vcc *atmvcc;	/* atm_vcc who is transmitter */
 242		int endptr;		/* last endptr from service entry */
 243		struct sk_buff_head backlog;
 244		void (*unqueue)(struct lanai_dev *, struct lanai_vcc *, int);
 245	} tx;
 246};
 247
 248enum lanai_type {
 249	lanai2	= PCI_DEVICE_ID_EF_ATM_LANAI2,
 250	lanaihb	= PCI_DEVICE_ID_EF_ATM_LANAIHB
 251};
 252
 253struct lanai_dev_stats {
 254	unsigned ovfl_trash;	/* # of cells dropped - buffer overflow */
 255	unsigned vci_trash;	/* # of cells dropped - closed vci */
 256	unsigned hec_err;	/* # of cells dropped - bad HEC */
 257	unsigned atm_ovfl;	/* # of cells dropped - rx fifo overflow */
 258	unsigned pcierr_parity_detect;
 259	unsigned pcierr_serr_set;
 260	unsigned pcierr_master_abort;
 261	unsigned pcierr_m_target_abort;
 262	unsigned pcierr_s_target_abort;
 263	unsigned pcierr_master_parity;
 264	unsigned service_notx;
 265	unsigned service_norx;
 266	unsigned service_rxnotaal5;
 267	unsigned dma_reenable;
 268	unsigned card_reset;
 269};
 270
 271struct lanai_dev {
 272	bus_addr_t base;
 273	struct lanai_dev_stats stats;
 274	struct lanai_buffer service;
 275	struct lanai_vcc **vccs;
 276#ifdef USE_POWERDOWN
 277	int nbound;			/* number of bound vccs */
 278#endif
 279	enum lanai_type type;
 280	vci_t num_vci;			/* Currently just NUM_VCI */
 281	u8 eeprom[LANAI_EEPROM_SIZE];
 282	u32 serialno, magicno;
 283	struct pci_dev *pci;
 284	DECLARE_BITMAP(backlog_vccs, NUM_VCI);   /* VCCs with tx backlog */
 285	DECLARE_BITMAP(transmit_ready, NUM_VCI); /* VCCs with transmit space */
 286	struct timer_list timer;
 287	int naal0;
 288	struct lanai_buffer aal0buf;	/* AAL0 RX buffers */
 289	u32 conf1, conf2;		/* CONFIG[12] registers */
 290	u32 status;			/* STATUS register */
 291	spinlock_t endtxlock;
 292	spinlock_t servicelock;
 293	struct atm_vcc *cbrvcc;
 294	int number;
 295	int board_rev;
 296/* TODO - look at race conditions with maintence of conf1/conf2 */
 297/* TODO - transmit locking: should we use _irq not _irqsave? */
 298/* TODO - organize above in some rational fashion (see <asm/cache.h>) */
 299};
 300
 301/*
 302 * Each device has two bitmaps for each VCC (baclog_vccs and transmit_ready)
 303 * This function iterates one of these, calling a given function for each
 304 * vci with their bit set
 305 */
 306static void vci_bitfield_iterate(struct lanai_dev *lanai,
 307	const unsigned long *lp,
 308	void (*func)(struct lanai_dev *,vci_t vci))
 309{
 310	vci_t vci;
 311
 312	for_each_set_bit(vci, lp, NUM_VCI)
 313		func(lanai, vci);
 314}
 315
 316/* -------------------- BUFFER  UTILITIES: */
 317
 318/*
 319 * Lanai needs DMA buffers aligned to 256 bytes of at least 1024 bytes -
 320 * usually any page allocation will do.  Just to be safe in case
 321 * PAGE_SIZE is insanely tiny, though...
 322 */
 323#define LANAI_PAGE_SIZE   ((PAGE_SIZE >= 1024) ? PAGE_SIZE : 1024)
 324
 325/*
 326 * Allocate a buffer in host RAM for service list, RX, or TX
 327 * Returns buf->start==NULL if no memory
 328 * Note that the size will be rounded up 2^n bytes, and
 329 * if we can't allocate that we'll settle for something smaller
 330 * until minbytes
 331 */
 332static void lanai_buf_allocate(struct lanai_buffer *buf,
 333	size_t bytes, size_t minbytes, struct pci_dev *pci)
 334{
 335	int size;
 336
 337	if (bytes > (128 * 1024))	/* max lanai buffer size */
 338		bytes = 128 * 1024;
 339	for (size = LANAI_PAGE_SIZE; size < bytes; size *= 2)
 340		;
 341	if (minbytes < LANAI_PAGE_SIZE)
 342		minbytes = LANAI_PAGE_SIZE;
 343	do {
 344		/*
 345		 * Technically we could use non-consistent mappings for
 346		 * everything, but the way the lanai uses DMA memory would
 347		 * make that a terrific pain.  This is much simpler.
 348		 */
 349		buf->start = dma_alloc_coherent(&pci->dev,
 350						size, &buf->dmaaddr, GFP_KERNEL);
 351		if (buf->start != NULL) {	/* Success */
 352			/* Lanai requires 256-byte alignment of DMA bufs */
 353			APRINTK((buf->dmaaddr & ~0xFFFFFF00) == 0,
 354			    "bad dmaaddr: 0x%lx\n",
 355			    (unsigned long) buf->dmaaddr);
 356			buf->ptr = buf->start;
 357			buf->end = (u32 *)
 358			    (&((unsigned char *) buf->start)[size]);
 359			memset(buf->start, 0, size);
 360			break;
 361		}
 362		size /= 2;
 363	} while (size >= minbytes);
 364}
 365
 366/* size of buffer in bytes */
 367static inline size_t lanai_buf_size(const struct lanai_buffer *buf)
 368{
 369	return ((unsigned long) buf->end) - ((unsigned long) buf->start);
 370}
 371
 372static void lanai_buf_deallocate(struct lanai_buffer *buf,
 373	struct pci_dev *pci)
 374{
 375	if (buf->start != NULL) {
 376		dma_free_coherent(&pci->dev, lanai_buf_size(buf),
 377				  buf->start, buf->dmaaddr);
 378		buf->start = buf->end = buf->ptr = NULL;
 379	}
 380}
 381
 382/* size of buffer as "card order" (0=1k .. 7=128k) */
 383static int lanai_buf_size_cardorder(const struct lanai_buffer *buf)
 384{
 385	int order = get_order(lanai_buf_size(buf)) + (PAGE_SHIFT - 10);
 386
 387	/* This can only happen if PAGE_SIZE is gigantic, but just in case */
 388	if (order > 7)
 389		order = 7;
 390	return order;
 391}
 392
 393/* -------------------- PORT I/O UTILITIES: */
 394
 395/* Registers (and their bit-fields) */
 396enum lanai_register {
 397	Reset_Reg		= 0x00,	/* Reset; read for chip type; bits: */
 398#define   RESET_GET_BOARD_REV(x)    (((x)>> 0)&0x03)	/* Board revision */
 399#define   RESET_GET_BOARD_ID(x)	    (((x)>> 2)&0x03)	/* Board ID */
 400#define     BOARD_ID_LANAI256		(0)	/* 25.6M adapter card */
 401	Endian_Reg		= 0x04,	/* Endian setting */
 402	IntStatus_Reg		= 0x08,	/* Interrupt status */
 403	IntStatusMasked_Reg	= 0x0C,	/* Interrupt status (masked) */
 404	IntAck_Reg		= 0x10,	/* Interrupt acknowledge */
 405	IntAckMasked_Reg	= 0x14,	/* Interrupt acknowledge (masked) */
 406	IntStatusSet_Reg	= 0x18,	/* Get status + enable/disable */
 407	IntStatusSetMasked_Reg	= 0x1C,	/* Get status + en/di (masked) */
 408	IntControlEna_Reg	= 0x20,	/* Interrupt control enable */
 409	IntControlDis_Reg	= 0x24,	/* Interrupt control disable */
 410	Status_Reg		= 0x28,	/* Status */
 411#define   STATUS_PROMDATA	 (0x00000001)	/* PROM_DATA pin */
 412#define   STATUS_WAITING	 (0x00000002)	/* Interrupt being delayed */
 413#define	  STATUS_SOOL		 (0x00000004)	/* SOOL alarm */
 414#define   STATUS_LOCD		 (0x00000008)	/* LOCD alarm */
 415#define	  STATUS_LED		 (0x00000010)	/* LED (HAPPI) output */
 416#define   STATUS_GPIN		 (0x00000020)	/* GPIN pin */
 417#define   STATUS_BUTTBUSY	 (0x00000040)	/* Butt register is pending */
 418	Config1_Reg		= 0x2C,	/* Config word 1; bits: */
 419#define   CONFIG1_PROMDATA	 (0x00000001)	/* PROM_DATA pin */
 420#define   CONFIG1_PROMCLK	 (0x00000002)	/* PROM_CLK pin */
 421#define   CONFIG1_SET_READMODE(x) ((x)*0x004)	/* PCI BM reads; values: */
 422#define     READMODE_PLAIN	    (0)		/*   Plain memory read */
 423#define     READMODE_LINE	    (2)		/*   Memory read line */
 424#define     READMODE_MULTIPLE	    (3)		/*   Memory read multiple */
 425#define   CONFIG1_DMA_ENABLE	 (0x00000010)	/* Turn on DMA */
 426#define   CONFIG1_POWERDOWN	 (0x00000020)	/* Turn off clocks */
 427#define   CONFIG1_SET_LOOPMODE(x) ((x)*0x080)	/* Clock&loop mode; values: */
 428#define     LOOPMODE_NORMAL	    (0)		/*   Normal - no loop */
 429#define     LOOPMODE_TIME	    (1)
 430#define     LOOPMODE_DIAG	    (2)
 431#define     LOOPMODE_LINE	    (3)
 432#define   CONFIG1_MASK_LOOPMODE  (0x00000180)
 433#define   CONFIG1_SET_LEDMODE(x) ((x)*0x0200)	/* Mode of LED; values: */
 434#define     LEDMODE_NOT_SOOL	    (0)		/*   !SOOL */
 435#define	    LEDMODE_OFF		    (1)		/*   0     */
 436#define	    LEDMODE_ON		    (2)		/*   1     */
 437#define	    LEDMODE_NOT_LOCD	    (3)		/*   !LOCD */
 438#define	    LEDMORE_GPIN	    (4)		/*   GPIN  */
 439#define     LEDMODE_NOT_GPIN	    (7)		/*   !GPIN */
 440#define   CONFIG1_MASK_LEDMODE	 (0x00000E00)
 441#define   CONFIG1_GPOUT1	 (0x00001000)	/* Toggle for reset */
 442#define   CONFIG1_GPOUT2	 (0x00002000)	/* Loopback PHY */
 443#define   CONFIG1_GPOUT3	 (0x00004000)	/* Loopback lanai */
 444	Config2_Reg		= 0x30,	/* Config word 2; bits: */
 445#define   CONFIG2_HOWMANY	 (0x00000001)	/* >512 VCIs? */
 446#define   CONFIG2_PTI7_MODE	 (0x00000002)	/* Make PTI=7 RM, not OAM */
 447#define   CONFIG2_VPI_CHK_DIS	 (0x00000004)	/* Ignore RX VPI value */
 448#define   CONFIG2_HEC_DROP	 (0x00000008)	/* Drop cells w/ HEC errors */
 449#define   CONFIG2_VCI0_NORMAL	 (0x00000010)	/* Treat VCI=0 normally */
 450#define   CONFIG2_CBR_ENABLE	 (0x00000020)	/* Deal with CBR traffic */
 451#define   CONFIG2_TRASH_ALL	 (0x00000040)	/* Trashing incoming cells */
 452#define   CONFIG2_TX_DISABLE	 (0x00000080)	/* Trashing outgoing cells */
 453#define   CONFIG2_SET_TRASH	 (0x00000100)	/* Turn trashing on */
 454	Statistics_Reg		= 0x34,	/* Statistics; bits: */
 455#define   STATS_GET_FIFO_OVFL(x)    (((x)>> 0)&0xFF)	/* FIFO overflowed */
 456#define   STATS_GET_HEC_ERR(x)      (((x)>> 8)&0xFF)	/* HEC was bad */
 457#define   STATS_GET_BAD_VCI(x)      (((x)>>16)&0xFF)	/* VCI not open */
 458#define   STATS_GET_BUF_OVFL(x)     (((x)>>24)&0xFF)	/* VCC buffer full */
 459	ServiceStuff_Reg	= 0x38,	/* Service stuff; bits: */
 460#define   SSTUFF_SET_SIZE(x) ((x)*0x20000000)	/* size of service buffer */
 461#define   SSTUFF_SET_ADDR(x)	    ((x)>>8)	/* set address of buffer */
 462	ServWrite_Reg		= 0x3C,	/* ServWrite Pointer */
 463	ServRead_Reg		= 0x40,	/* ServRead Pointer */
 464	TxDepth_Reg		= 0x44,	/* FIFO Transmit Depth */
 465	Butt_Reg		= 0x48,	/* Butt register */
 466	CBR_ICG_Reg		= 0x50,
 467	CBR_PTR_Reg		= 0x54,
 468	PingCount_Reg		= 0x58,	/* Ping count */
 469	DMA_Addr_Reg		= 0x5C	/* DMA address */
 470};
 471
 472static inline bus_addr_t reg_addr(const struct lanai_dev *lanai,
 473	enum lanai_register reg)
 474{
 475	return lanai->base + reg;
 476}
 477
 478static inline u32 reg_read(const struct lanai_dev *lanai,
 479	enum lanai_register reg)
 480{
 481	u32 t;
 482	t = readl(reg_addr(lanai, reg));
 483	RWDEBUG("R [0x%08X] 0x%02X = 0x%08X\n", (unsigned int) lanai->base,
 484	    (int) reg, t);
 485	return t;
 486}
 487
 488static inline void reg_write(const struct lanai_dev *lanai, u32 val,
 489	enum lanai_register reg)
 490{
 491	RWDEBUG("W [0x%08X] 0x%02X < 0x%08X\n", (unsigned int) lanai->base,
 492	    (int) reg, val);
 493	writel(val, reg_addr(lanai, reg));
 494}
 495
 496static inline void conf1_write(const struct lanai_dev *lanai)
 497{
 498	reg_write(lanai, lanai->conf1, Config1_Reg);
 499}
 500
 501static inline void conf2_write(const struct lanai_dev *lanai)
 502{
 503	reg_write(lanai, lanai->conf2, Config2_Reg);
 504}
 505
 506/* Same as conf2_write(), but defers I/O if we're powered down */
 507static inline void conf2_write_if_powerup(const struct lanai_dev *lanai)
 508{
 509#ifdef USE_POWERDOWN
 510	if (unlikely((lanai->conf1 & CONFIG1_POWERDOWN) != 0))
 511		return;
 512#endif /* USE_POWERDOWN */
 513	conf2_write(lanai);
 514}
 515
 516static inline void reset_board(const struct lanai_dev *lanai)
 517{
 518	DPRINTK("about to reset board\n");
 519	reg_write(lanai, 0, Reset_Reg);
 520	/*
 521	 * If we don't delay a little while here then we can end up
 522	 * leaving the card in a VERY weird state and lock up the
 523	 * PCI bus.  This isn't documented anywhere but I've convinced
 524	 * myself after a lot of painful experimentation
 525	 */
 526	udelay(5);
 527}
 528
 529/* -------------------- CARD SRAM UTILITIES: */
 530
 531/* The SRAM is mapped into normal PCI memory space - the only catch is
 532 * that it is only 16-bits wide but must be accessed as 32-bit.  The
 533 * 16 high bits will be zero.  We don't hide this, since they get
 534 * programmed mostly like discrete registers anyway
 535 */
 536#define SRAM_START (0x20000)
 537#define SRAM_BYTES (0x20000)	/* Again, half don't really exist */
 538
 539static inline bus_addr_t sram_addr(const struct lanai_dev *lanai, int offset)
 540{
 541	return lanai->base + SRAM_START + offset;
 542}
 543
 544static inline u32 sram_read(const struct lanai_dev *lanai, int offset)
 545{
 546	return readl(sram_addr(lanai, offset));
 547}
 548
 549static inline void sram_write(const struct lanai_dev *lanai,
 550	u32 val, int offset)
 551{
 552	writel(val, sram_addr(lanai, offset));
 553}
 554
 555static int sram_test_word(const struct lanai_dev *lanai, int offset,
 556			  u32 pattern)
 557{
 558	u32 readback;
 559	sram_write(lanai, pattern, offset);
 560	readback = sram_read(lanai, offset);
 561	if (likely(readback == pattern))
 562		return 0;
 563	printk(KERN_ERR DEV_LABEL
 564	    "(itf %d): SRAM word at %d bad: wrote 0x%X, read 0x%X\n",
 565	    lanai->number, offset,
 566	    (unsigned int) pattern, (unsigned int) readback);
 567	return -EIO;
 568}
 569
 570static int sram_test_pass(const struct lanai_dev *lanai, u32 pattern)
 571{
 572	int offset, result = 0;
 573	for (offset = 0; offset < SRAM_BYTES && result == 0; offset += 4)
 574		result = sram_test_word(lanai, offset, pattern);
 575	return result;
 576}
 577
 578static int sram_test_and_clear(const struct lanai_dev *lanai)
 579{
 580#ifdef FULL_MEMORY_TEST
 581	int result;
 582	DPRINTK("testing SRAM\n");
 583	if ((result = sram_test_pass(lanai, 0x5555)) != 0)
 584		return result;
 585	if ((result = sram_test_pass(lanai, 0xAAAA)) != 0)
 586		return result;
 587#endif
 588	DPRINTK("clearing SRAM\n");
 589	return sram_test_pass(lanai, 0x0000);
 590}
 591
 592/* -------------------- CARD-BASED VCC TABLE UTILITIES: */
 593
 594/* vcc table */
 595enum lanai_vcc_offset {
 596	vcc_rxaddr1		= 0x00,	/* Location1, plus bits: */
 597#define   RXADDR1_SET_SIZE(x) ((x)*0x0000100)	/* size of RX buffer */
 598#define   RXADDR1_SET_RMMODE(x) ((x)*0x00800)	/* RM cell action; values: */
 599#define     RMMODE_TRASH	  (0)		/*   discard */
 600#define     RMMODE_PRESERVE	  (1)		/*   input as AAL0 */
 601#define     RMMODE_PIPE		  (2)		/*   pipe to coscheduler */
 602#define     RMMODE_PIPEALL	  (3)		/*   pipe non-RM too */
 603#define   RXADDR1_OAM_PRESERVE	 (0x00002000)	/* Input OAM cells as AAL0 */
 604#define   RXADDR1_SET_MODE(x) ((x)*0x0004000)	/* Reassembly mode */
 605#define     RXMODE_TRASH	  (0)		/*   discard */
 606#define     RXMODE_AAL0		  (1)		/*   non-AAL5 mode */
 607#define     RXMODE_AAL5		  (2)		/*   AAL5, intr. each PDU */
 608#define     RXMODE_AAL5_STREAM	  (3)		/*   AAL5 w/o per-PDU intr */
 609	vcc_rxaddr2		= 0x04,	/* Location2 */
 610	vcc_rxcrc1		= 0x08,	/* RX CRC claculation space */
 611	vcc_rxcrc2		= 0x0C,
 612	vcc_rxwriteptr		= 0x10, /* RX writeptr, plus bits: */
 613#define   RXWRITEPTR_LASTEFCI	 (0x00002000)	/* Last PDU had EFCI bit */
 614#define   RXWRITEPTR_DROPPING	 (0x00004000)	/* Had error, dropping */
 615#define   RXWRITEPTR_TRASHING	 (0x00008000)	/* Trashing */
 616	vcc_rxbufstart		= 0x14,	/* RX bufstart, plus bits: */
 617#define   RXBUFSTART_CLP	 (0x00004000)
 618#define   RXBUFSTART_CI		 (0x00008000)
 619	vcc_rxreadptr		= 0x18,	/* RX readptr */
 620	vcc_txicg		= 0x1C, /* TX ICG */
 621	vcc_txaddr1		= 0x20,	/* Location1, plus bits: */
 622#define   TXADDR1_SET_SIZE(x) ((x)*0x0000100)	/* size of TX buffer */
 623#define   TXADDR1_ABR		 (0x00008000)	/* use ABR (doesn't work) */
 624	vcc_txaddr2		= 0x24,	/* Location2 */
 625	vcc_txcrc1		= 0x28,	/* TX CRC claculation space */
 626	vcc_txcrc2		= 0x2C,
 627	vcc_txreadptr		= 0x30, /* TX Readptr, plus bits: */
 628#define   TXREADPTR_GET_PTR(x) ((x)&0x01FFF)
 629#define   TXREADPTR_MASK_DELTA	(0x0000E000)	/* ? */
 630	vcc_txendptr		= 0x34, /* TX Endptr, plus bits: */
 631#define   TXENDPTR_CLP		(0x00002000)
 632#define   TXENDPTR_MASK_PDUMODE	(0x0000C000)	/* PDU mode; values: */
 633#define     PDUMODE_AAL0	 (0*0x04000)
 634#define     PDUMODE_AAL5	 (2*0x04000)
 635#define     PDUMODE_AAL5STREAM	 (3*0x04000)
 636	vcc_txwriteptr		= 0x38,	/* TX Writeptr */
 637#define   TXWRITEPTR_GET_PTR(x) ((x)&0x1FFF)
 638	vcc_txcbr_next		= 0x3C	/* # of next CBR VCI in ring */
 639#define   TXCBR_NEXT_BOZO	(0x00008000)	/* "bozo bit" */
 640};
 641
 642#define CARDVCC_SIZE	(0x40)
 643
 644static inline bus_addr_t cardvcc_addr(const struct lanai_dev *lanai,
 645	vci_t vci)
 646{
 647	return sram_addr(lanai, vci * CARDVCC_SIZE);
 648}
 649
 650static inline u32 cardvcc_read(const struct lanai_vcc *lvcc,
 651	enum lanai_vcc_offset offset)
 652{
 653	u32 val;
 654	APRINTK(lvcc->vbase != NULL, "cardvcc_read: unbound vcc!\n");
 655	val= readl(lvcc->vbase + offset);
 656	RWDEBUG("VR vci=%04d 0x%02X = 0x%08X\n",
 657	    lvcc->vci, (int) offset, val);
 658	return val;
 659}
 660
 661static inline void cardvcc_write(const struct lanai_vcc *lvcc,
 662	u32 val, enum lanai_vcc_offset offset)
 663{
 664	APRINTK(lvcc->vbase != NULL, "cardvcc_write: unbound vcc!\n");
 665	APRINTK((val & ~0xFFFF) == 0,
 666	    "cardvcc_write: bad val 0x%X (vci=%d, addr=0x%02X)\n",
 667	    (unsigned int) val, lvcc->vci, (unsigned int) offset);
 668	RWDEBUG("VW vci=%04d 0x%02X > 0x%08X\n",
 669	    lvcc->vci, (unsigned int) offset, (unsigned int) val);
 670	writel(val, lvcc->vbase + offset);
 671}
 672
 673/* -------------------- COMPUTE SIZE OF AN AAL5 PDU: */
 674
 675/* How many bytes will an AAL5 PDU take to transmit - remember that:
 676 *   o  we need to add 8 bytes for length, CPI, UU, and CRC
 677 *   o  we need to round up to 48 bytes for cells
 678 */
 679static inline int aal5_size(int size)
 680{
 681	int cells = (size + 8 + 47) / 48;
 682	return cells * 48;
 683}
 684
 685/* -------------------- FREE AN ATM SKB: */
 686
 687static inline void lanai_free_skb(struct atm_vcc *atmvcc, struct sk_buff *skb)
 688{
 689	if (atmvcc->pop != NULL)
 690		atmvcc->pop(atmvcc, skb);
 691	else
 692		dev_kfree_skb_any(skb);
 693}
 694
 695/* -------------------- TURN VCCS ON AND OFF: */
 696
 697static void host_vcc_start_rx(const struct lanai_vcc *lvcc)
 698{
 699	u32 addr1;
 700	if (lvcc->rx.atmvcc->qos.aal == ATM_AAL5) {
 701		dma_addr_t dmaaddr = lvcc->rx.buf.dmaaddr;
 702		cardvcc_write(lvcc, 0xFFFF, vcc_rxcrc1);
 703		cardvcc_write(lvcc, 0xFFFF, vcc_rxcrc2);
 704		cardvcc_write(lvcc, 0, vcc_rxwriteptr);
 705		cardvcc_write(lvcc, 0, vcc_rxbufstart);
 706		cardvcc_write(lvcc, 0, vcc_rxreadptr);
 707		cardvcc_write(lvcc, (dmaaddr >> 16) & 0xFFFF, vcc_rxaddr2);
 708		addr1 = ((dmaaddr >> 8) & 0xFF) |
 709		    RXADDR1_SET_SIZE(lanai_buf_size_cardorder(&lvcc->rx.buf))|
 710		    RXADDR1_SET_RMMODE(RMMODE_TRASH) |	/* ??? */
 711		 /* RXADDR1_OAM_PRESERVE |	--- no OAM support yet */
 712		    RXADDR1_SET_MODE(RXMODE_AAL5);
 713	} else
 714		addr1 = RXADDR1_SET_RMMODE(RMMODE_PRESERVE) | /* ??? */
 715		    RXADDR1_OAM_PRESERVE |		      /* ??? */
 716		    RXADDR1_SET_MODE(RXMODE_AAL0);
 717	/* This one must be last! */
 718	cardvcc_write(lvcc, addr1, vcc_rxaddr1);
 719}
 720
 721static void host_vcc_start_tx(const struct lanai_vcc *lvcc)
 722{
 723	dma_addr_t dmaaddr = lvcc->tx.buf.dmaaddr;
 724	cardvcc_write(lvcc, 0, vcc_txicg);
 725	cardvcc_write(lvcc, 0xFFFF, vcc_txcrc1);
 726	cardvcc_write(lvcc, 0xFFFF, vcc_txcrc2);
 727	cardvcc_write(lvcc, 0, vcc_txreadptr);
 728	cardvcc_write(lvcc, 0, vcc_txendptr);
 729	cardvcc_write(lvcc, 0, vcc_txwriteptr);
 730	cardvcc_write(lvcc,
 731		(lvcc->tx.atmvcc->qos.txtp.traffic_class == ATM_CBR) ?
 732		TXCBR_NEXT_BOZO | lvcc->vci : 0, vcc_txcbr_next);
 733	cardvcc_write(lvcc, (dmaaddr >> 16) & 0xFFFF, vcc_txaddr2);
 734	cardvcc_write(lvcc,
 735	    ((dmaaddr >> 8) & 0xFF) |
 736	    TXADDR1_SET_SIZE(lanai_buf_size_cardorder(&lvcc->tx.buf)),
 737	    vcc_txaddr1);
 738}
 739
 740/* Shutdown receiving on card */
 741static void lanai_shutdown_rx_vci(const struct lanai_vcc *lvcc)
 742{
 743	if (lvcc->vbase == NULL)	/* We were never bound to a VCI */
 744		return;
 745	/* 15.1.1 - set to trashing, wait one cell time (15us) */
 746	cardvcc_write(lvcc,
 747	    RXADDR1_SET_RMMODE(RMMODE_TRASH) |
 748	    RXADDR1_SET_MODE(RXMODE_TRASH), vcc_rxaddr1);
 749	udelay(15);
 750	/* 15.1.2 - clear rest of entries */
 751	cardvcc_write(lvcc, 0, vcc_rxaddr2);
 752	cardvcc_write(lvcc, 0, vcc_rxcrc1);
 753	cardvcc_write(lvcc, 0, vcc_rxcrc2);
 754	cardvcc_write(lvcc, 0, vcc_rxwriteptr);
 755	cardvcc_write(lvcc, 0, vcc_rxbufstart);
 756	cardvcc_write(lvcc, 0, vcc_rxreadptr);
 757}
 758
 759/* Shutdown transmitting on card.
 760 * Unfortunately the lanai needs us to wait until all the data
 761 * drains out of the buffer before we can dealloc it, so this
 762 * can take awhile -- up to 370ms for a full 128KB buffer
 763 * assuming everone else is quiet.  In theory the time is
 764 * boundless if there's a CBR VCC holding things up.
 765 */
 766static void lanai_shutdown_tx_vci(struct lanai_dev *lanai,
 767	struct lanai_vcc *lvcc)
 768{
 769	struct sk_buff *skb;
 770	unsigned long flags, timeout;
 771	int read, write, lastread = -1;
 772	APRINTK(!in_interrupt(),
 773	    "lanai_shutdown_tx_vci called w/o process context!\n");
 774	if (lvcc->vbase == NULL)	/* We were never bound to a VCI */
 775		return;
 776	/* 15.2.1 - wait for queue to drain */
 777	while ((skb = skb_dequeue(&lvcc->tx.backlog)) != NULL)
 778		lanai_free_skb(lvcc->tx.atmvcc, skb);
 779	read_lock_irqsave(&vcc_sklist_lock, flags);
 780	__clear_bit(lvcc->vci, lanai->backlog_vccs);
 781	read_unlock_irqrestore(&vcc_sklist_lock, flags);
 782	/*
 783	 * We need to wait for the VCC to drain but don't wait forever.  We
 784	 * give each 1K of buffer size 1/128th of a second to clear out.
 785	 * TODO: maybe disable CBR if we're about to timeout?
 786	 */
 787	timeout = jiffies +
 788	    (((lanai_buf_size(&lvcc->tx.buf) / 1024) * HZ) >> 7);
 789	write = TXWRITEPTR_GET_PTR(cardvcc_read(lvcc, vcc_txwriteptr));
 790	for (;;) {
 791		read = TXREADPTR_GET_PTR(cardvcc_read(lvcc, vcc_txreadptr));
 792		if (read == write &&	   /* Is TX buffer empty? */
 793		    (lvcc->tx.atmvcc->qos.txtp.traffic_class != ATM_CBR ||
 794		    (cardvcc_read(lvcc, vcc_txcbr_next) &
 795		    TXCBR_NEXT_BOZO) == 0))
 796			break;
 797		if (read != lastread) {	   /* Has there been any progress? */
 798			lastread = read;
 799			timeout += HZ / 10;
 800		}
 801		if (unlikely(time_after(jiffies, timeout))) {
 802			printk(KERN_ERR DEV_LABEL "(itf %d): Timed out on "
 803			    "backlog closing vci %d\n",
 804			    lvcc->tx.atmvcc->dev->number, lvcc->vci);
 805			DPRINTK("read, write = %d, %d\n", read, write);
 806			break;
 807		}
 808		msleep(40);
 809	}
 810	/* 15.2.2 - clear out all tx registers */
 811	cardvcc_write(lvcc, 0, vcc_txreadptr);
 812	cardvcc_write(lvcc, 0, vcc_txwriteptr);
 813	cardvcc_write(lvcc, 0, vcc_txendptr);
 814	cardvcc_write(lvcc, 0, vcc_txcrc1);
 815	cardvcc_write(lvcc, 0, vcc_txcrc2);
 816	cardvcc_write(lvcc, 0, vcc_txaddr2);
 817	cardvcc_write(lvcc, 0, vcc_txaddr1);
 818}
 819
 820/* -------------------- MANAGING AAL0 RX BUFFER: */
 821
 822static inline int aal0_buffer_allocate(struct lanai_dev *lanai)
 823{
 824	DPRINTK("aal0_buffer_allocate: allocating AAL0 RX buffer\n");
 825	lanai_buf_allocate(&lanai->aal0buf, AAL0_RX_BUFFER_SIZE, 80,
 826			   lanai->pci);
 827	return (lanai->aal0buf.start == NULL) ? -ENOMEM : 0;
 828}
 829
 830static inline void aal0_buffer_free(struct lanai_dev *lanai)
 831{
 832	DPRINTK("aal0_buffer_allocate: freeing AAL0 RX buffer\n");
 833	lanai_buf_deallocate(&lanai->aal0buf, lanai->pci);
 834}
 835
 836/* -------------------- EEPROM UTILITIES: */
 837
 838/* Offsets of data in the EEPROM */
 839#define EEPROM_COPYRIGHT	(0)
 840#define EEPROM_COPYRIGHT_LEN	(44)
 841#define EEPROM_CHECKSUM		(62)
 842#define EEPROM_CHECKSUM_REV	(63)
 843#define EEPROM_MAC		(64)
 844#define EEPROM_MAC_REV		(70)
 845#define EEPROM_SERIAL		(112)
 846#define EEPROM_SERIAL_REV	(116)
 847#define EEPROM_MAGIC		(120)
 848#define EEPROM_MAGIC_REV	(124)
 849
 850#define EEPROM_MAGIC_VALUE	(0x5AB478D2)
 851
 852#ifndef READ_EEPROM
 853
 854/* Stub functions to use if EEPROM reading is disabled */
 855static int eeprom_read(struct lanai_dev *lanai)
 856{
 857	printk(KERN_INFO DEV_LABEL "(itf %d): *NOT* reading EEPROM\n",
 858	    lanai->number);
 859	memset(&lanai->eeprom[EEPROM_MAC], 0, 6);
 860	return 0;
 861}
 862
 863static int eeprom_validate(struct lanai_dev *lanai)
 864{
 865	lanai->serialno = 0;
 866	lanai->magicno = EEPROM_MAGIC_VALUE;
 867	return 0;
 868}
 869
 870#else /* READ_EEPROM */
 871
 872static int eeprom_read(struct lanai_dev *lanai)
 873{
 874	int i, address;
 875	u8 data;
 876	u32 tmp;
 877#define set_config1(x)   do { lanai->conf1 = x; conf1_write(lanai); \
 878			    } while (0)
 879#define clock_h()	 set_config1(lanai->conf1 | CONFIG1_PROMCLK)
 880#define clock_l()	 set_config1(lanai->conf1 &~ CONFIG1_PROMCLK)
 881#define data_h()	 set_config1(lanai->conf1 | CONFIG1_PROMDATA)
 882#define data_l()	 set_config1(lanai->conf1 &~ CONFIG1_PROMDATA)
 883#define pre_read()	 do { data_h(); clock_h(); udelay(5); } while (0)
 884#define read_pin()	 (reg_read(lanai, Status_Reg) & STATUS_PROMDATA)
 885#define send_stop()	 do { data_l(); udelay(5); clock_h(); udelay(5); \
 886			      data_h(); udelay(5); } while (0)
 887	/* start with both clock and data high */
 888	data_h(); clock_h(); udelay(5);
 889	for (address = 0; address < LANAI_EEPROM_SIZE; address++) {
 890		data = (address << 1) | 1;	/* Command=read + address */
 891		/* send start bit */
 892		data_l(); udelay(5);
 893		clock_l(); udelay(5);
 894		for (i = 128; i != 0; i >>= 1) {   /* write command out */
 895			tmp = (lanai->conf1 & ~CONFIG1_PROMDATA) |
 896			    ((data & i) ? CONFIG1_PROMDATA : 0);
 897			if (lanai->conf1 != tmp) {
 898				set_config1(tmp);
 899				udelay(5);	/* Let new data settle */
 900			}
 901			clock_h(); udelay(5); clock_l(); udelay(5);
 902		}
 903		/* look for ack */
 904		data_h(); clock_h(); udelay(5);
 905		if (read_pin() != 0)
 906			goto error;	/* No ack seen */
 907		clock_l(); udelay(5);
 908		/* read back result */
 909		for (data = 0, i = 7; i >= 0; i--) {
 910			data_h(); clock_h(); udelay(5);
 911			data = (data << 1) | !!read_pin();
 912			clock_l(); udelay(5);
 913		}
 914		/* look again for ack */
 915		data_h(); clock_h(); udelay(5);
 916		if (read_pin() == 0)
 917			goto error;	/* Spurious ack */
 918		clock_l(); udelay(5);
 919		send_stop();
 920		lanai->eeprom[address] = data;
 921		DPRINTK("EEPROM 0x%04X %02X\n",
 922		    (unsigned int) address, (unsigned int) data);
 923	}
 924	return 0;
 925    error:
 926	clock_l(); udelay(5);		/* finish read */
 927	send_stop();
 928	printk(KERN_ERR DEV_LABEL "(itf %d): error reading EEPROM byte %d\n",
 929	    lanai->number, address);
 930	return -EIO;
 931#undef set_config1
 932#undef clock_h
 933#undef clock_l
 934#undef data_h
 935#undef data_l
 936#undef pre_read
 937#undef read_pin
 938#undef send_stop
 939}
 940
 941/* read a big-endian 4-byte value out of eeprom */
 942static inline u32 eeprom_be4(const struct lanai_dev *lanai, int address)
 943{
 944	return be32_to_cpup((const u32 *) &lanai->eeprom[address]);
 945}
 946
 947/* Checksum/validate EEPROM contents */
 948static int eeprom_validate(struct lanai_dev *lanai)
 949{
 950	int i, s;
 951	u32 v;
 952	const u8 *e = lanai->eeprom;
 953#ifdef DEBUG
 954	/* First, see if we can get an ASCIIZ string out of the copyright */
 955	for (i = EEPROM_COPYRIGHT;
 956	    i < (EEPROM_COPYRIGHT + EEPROM_COPYRIGHT_LEN); i++)
 957		if (e[i] < 0x20 || e[i] > 0x7E)
 958			break;
 959	if ( i != EEPROM_COPYRIGHT &&
 960	    i != EEPROM_COPYRIGHT + EEPROM_COPYRIGHT_LEN && e[i] == '\0')
 961		DPRINTK("eeprom: copyright = \"%s\"\n",
 962		    (char *) &e[EEPROM_COPYRIGHT]);
 963	else
 964		DPRINTK("eeprom: copyright not found\n");
 965#endif
 966	/* Validate checksum */
 967	for (i = s = 0; i < EEPROM_CHECKSUM; i++)
 968		s += e[i];
 969	s &= 0xFF;
 970	if (s != e[EEPROM_CHECKSUM]) {
 971		printk(KERN_ERR DEV_LABEL "(itf %d): EEPROM checksum bad "
 972		    "(wanted 0x%02X, got 0x%02X)\n", lanai->number,
 973		    (unsigned int) s, (unsigned int) e[EEPROM_CHECKSUM]);
 974		return -EIO;
 975	}
 976	s ^= 0xFF;
 977	if (s != e[EEPROM_CHECKSUM_REV]) {
 978		printk(KERN_ERR DEV_LABEL "(itf %d): EEPROM inverse checksum "
 979		    "bad (wanted 0x%02X, got 0x%02X)\n", lanai->number,
 980		    (unsigned int) s, (unsigned int) e[EEPROM_CHECKSUM_REV]);
 981		return -EIO;
 982	}
 983	/* Verify MAC address */
 984	for (i = 0; i < 6; i++)
 985		if ((e[EEPROM_MAC + i] ^ e[EEPROM_MAC_REV + i]) != 0xFF) {
 986			printk(KERN_ERR DEV_LABEL
 987			    "(itf %d) : EEPROM MAC addresses don't match "
 988			    "(0x%02X, inverse 0x%02X)\n", lanai->number,
 989			    (unsigned int) e[EEPROM_MAC + i],
 990			    (unsigned int) e[EEPROM_MAC_REV + i]);
 991			return -EIO;
 992		}
 993	DPRINTK("eeprom: MAC address = %pM\n", &e[EEPROM_MAC]);
 994	/* Verify serial number */
 995	lanai->serialno = eeprom_be4(lanai, EEPROM_SERIAL);
 996	v = eeprom_be4(lanai, EEPROM_SERIAL_REV);
 997	if ((lanai->serialno ^ v) != 0xFFFFFFFF) {
 998		printk(KERN_ERR DEV_LABEL "(itf %d): EEPROM serial numbers "
 999		    "don't match (0x%08X, inverse 0x%08X)\n", lanai->number,
1000		    (unsigned int) lanai->serialno, (unsigned int) v);
1001		return -EIO;
1002	}
1003	DPRINTK("eeprom: Serial number = %d\n", (unsigned int) lanai->serialno);
1004	/* Verify magic number */
1005	lanai->magicno = eeprom_be4(lanai, EEPROM_MAGIC);
1006	v = eeprom_be4(lanai, EEPROM_MAGIC_REV);
1007	if ((lanai->magicno ^ v) != 0xFFFFFFFF) {
1008		printk(KERN_ERR DEV_LABEL "(itf %d): EEPROM magic numbers "
1009		    "don't match (0x%08X, inverse 0x%08X)\n", lanai->number,
1010		    lanai->magicno, v);
1011		return -EIO;
1012	}
1013	DPRINTK("eeprom: Magic number = 0x%08X\n", lanai->magicno);
1014	if (lanai->magicno != EEPROM_MAGIC_VALUE)
1015		printk(KERN_WARNING DEV_LABEL "(itf %d): warning - EEPROM "
1016		    "magic not what expected (got 0x%08X, not 0x%08X)\n",
1017		    lanai->number, (unsigned int) lanai->magicno,
1018		    (unsigned int) EEPROM_MAGIC_VALUE);
1019	return 0;
1020}
1021
1022#endif /* READ_EEPROM */
1023
1024static inline const u8 *eeprom_mac(const struct lanai_dev *lanai)
1025{
1026	return &lanai->eeprom[EEPROM_MAC];
1027}
1028
1029/* -------------------- INTERRUPT HANDLING UTILITIES: */
1030
1031/* Interrupt types */
1032#define INT_STATS	(0x00000002)	/* Statistics counter overflow */
1033#define INT_SOOL	(0x00000004)	/* SOOL changed state */
1034#define INT_LOCD	(0x00000008)	/* LOCD changed state */
1035#define INT_LED		(0x00000010)	/* LED (HAPPI) changed state */
1036#define INT_GPIN	(0x00000020)	/* GPIN changed state */
1037#define INT_PING	(0x00000040)	/* PING_COUNT fulfilled */
1038#define INT_WAKE	(0x00000080)	/* Lanai wants bus */
1039#define INT_CBR0	(0x00000100)	/* CBR sched hit VCI 0 */
1040#define INT_LOCK	(0x00000200)	/* Service list overflow */
1041#define INT_MISMATCH	(0x00000400)	/* TX magic list mismatch */
1042#define INT_AAL0_STR	(0x00000800)	/* Non-AAL5 buffer half filled */
1043#define INT_AAL0	(0x00001000)	/* Non-AAL5 data available */
1044#define INT_SERVICE	(0x00002000)	/* Service list entries available */
1045#define INT_TABORTSENT	(0x00004000)	/* Target abort sent by lanai */
1046#define INT_TABORTBM	(0x00008000)	/* Abort rcv'd as bus master */
1047#define INT_TIMEOUTBM	(0x00010000)	/* No response to bus master */
1048#define INT_PCIPARITY	(0x00020000)	/* Parity error on PCI */
1049
1050/* Sets of the above */
1051#define INT_ALL		(0x0003FFFE)	/* All interrupts */
1052#define INT_STATUS	(0x0000003C)	/* Some status pin changed */
1053#define INT_DMASHUT	(0x00038000)	/* DMA engine got shut down */
1054#define INT_SEGSHUT	(0x00000700)	/* Segmentation got shut down */
1055
1056static inline u32 intr_pending(const struct lanai_dev *lanai)
1057{
1058	return reg_read(lanai, IntStatusMasked_Reg);
1059}
1060
1061static inline void intr_enable(const struct lanai_dev *lanai, u32 i)
1062{
1063	reg_write(lanai, i, IntControlEna_Reg);
1064}
1065
1066static inline void intr_disable(const struct lanai_dev *lanai, u32 i)
1067{
1068	reg_write(lanai, i, IntControlDis_Reg);
1069}
1070
1071/* -------------------- CARD/PCI STATUS: */
1072
1073static void status_message(int itf, const char *name, int status)
1074{
1075	static const char *onoff[2] = { "off to on", "on to off" };
1076	printk(KERN_INFO DEV_LABEL "(itf %d): %s changed from %s\n",
1077	    itf, name, onoff[!status]);
1078}
1079
1080static void lanai_check_status(struct lanai_dev *lanai)
1081{
1082	u32 new = reg_read(lanai, Status_Reg);
1083	u32 changes = new ^ lanai->status;
1084	lanai->status = new;
1085#define e(flag, name) \
1086		if (changes & flag) \
1087			status_message(lanai->number, name, new & flag)
1088	e(STATUS_SOOL, "SOOL");
1089	e(STATUS_LOCD, "LOCD");
1090	e(STATUS_LED, "LED");
1091	e(STATUS_GPIN, "GPIN");
1092#undef e
1093}
1094
1095static void pcistatus_got(int itf, const char *name)
1096{
1097	printk(KERN_INFO DEV_LABEL "(itf %d): PCI got %s error\n", itf, name);
1098}
1099
1100static void pcistatus_check(struct lanai_dev *lanai, int clearonly)
1101{
1102	u16 s;
1103	int result;
1104	result = pci_read_config_word(lanai->pci, PCI_STATUS, &s);
1105	if (result != PCIBIOS_SUCCESSFUL) {
1106		printk(KERN_ERR DEV_LABEL "(itf %d): can't read PCI_STATUS: "
1107		    "%d\n", lanai->number, result);
1108		return;
1109	}
1110	s &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
1111	    PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT |
1112	    PCI_STATUS_SIG_TARGET_ABORT | PCI_STATUS_PARITY;
1113	if (s == 0)
1114		return;
1115	result = pci_write_config_word(lanai->pci, PCI_STATUS, s);
1116	if (result != PCIBIOS_SUCCESSFUL)
1117		printk(KERN_ERR DEV_LABEL "(itf %d): can't write PCI_STATUS: "
1118		    "%d\n", lanai->number, result);
1119	if (clearonly)
1120		return;
1121#define e(flag, name, stat) \
1122		if (s & flag) { \
1123			pcistatus_got(lanai->number, name); \
1124			++lanai->stats.pcierr_##stat; \
1125		}
1126	e(PCI_STATUS_DETECTED_PARITY, "parity", parity_detect);
1127	e(PCI_STATUS_SIG_SYSTEM_ERROR, "signalled system", serr_set);
1128	e(PCI_STATUS_REC_MASTER_ABORT, "master", master_abort);
1129	e(PCI_STATUS_REC_TARGET_ABORT, "master target", m_target_abort);
1130	e(PCI_STATUS_SIG_TARGET_ABORT, "slave", s_target_abort);
1131	e(PCI_STATUS_PARITY, "master parity", master_parity);
1132#undef e
1133}
1134
1135/* -------------------- VCC TX BUFFER UTILITIES: */
1136
1137/* space left in tx buffer in bytes */
1138static inline int vcc_tx_space(const struct lanai_vcc *lvcc, int endptr)
1139{
1140	int r;
1141	r = endptr * 16;
1142	r -= ((unsigned long) lvcc->tx.buf.ptr) -
1143	    ((unsigned long) lvcc->tx.buf.start);
1144	r -= 16;	/* Leave "bubble" - if start==end it looks empty */
1145	if (r < 0)
1146		r += lanai_buf_size(&lvcc->tx.buf);
1147	return r;
1148}
1149
1150/* test if VCC is currently backlogged */
1151static inline int vcc_is_backlogged(const struct lanai_vcc *lvcc)
1152{
1153	return !skb_queue_empty(&lvcc->tx.backlog);
1154}
1155
1156/* Bit fields in the segmentation buffer descriptor */
1157#define DESCRIPTOR_MAGIC	(0xD0000000)
1158#define DESCRIPTOR_AAL5		(0x00008000)
1159#define DESCRIPTOR_AAL5_STREAM	(0x00004000)
1160#define DESCRIPTOR_CLP		(0x00002000)
1161
1162/* Add 32-bit descriptor with its padding */
1163static inline void vcc_tx_add_aal5_descriptor(struct lanai_vcc *lvcc,
1164	u32 flags, int len)
1165{
1166	int pos;
1167	APRINTK((((unsigned long) lvcc->tx.buf.ptr) & 15) == 0,
1168	    "vcc_tx_add_aal5_descriptor: bad ptr=%p\n", lvcc->tx.buf.ptr);
1169	lvcc->tx.buf.ptr += 4;	/* Hope the values REALLY don't matter */
1170	pos = ((unsigned char *) lvcc->tx.buf.ptr) -
1171	    (unsigned char *) lvcc->tx.buf.start;
1172	APRINTK((pos & ~0x0001FFF0) == 0,
1173	    "vcc_tx_add_aal5_descriptor: bad pos (%d) before, vci=%d, "
1174	    "start,ptr,end=%p,%p,%p\n", pos, lvcc->vci,
1175	    lvcc->tx.buf.start, lvcc->tx.buf.ptr, lvcc->tx.buf.end);
1176	pos = (pos + len) & (lanai_buf_size(&lvcc->tx.buf) - 1);
1177	APRINTK((pos & ~0x0001FFF0) == 0,
1178	    "vcc_tx_add_aal5_descriptor: bad pos (%d) after, vci=%d, "
1179	    "start,ptr,end=%p,%p,%p\n", pos, lvcc->vci,
1180	    lvcc->tx.buf.start, lvcc->tx.buf.ptr, lvcc->tx.buf.end);
1181	lvcc->tx.buf.ptr[-1] =
1182	    cpu_to_le32(DESCRIPTOR_MAGIC | DESCRIPTOR_AAL5 |
1183	    ((lvcc->tx.atmvcc->atm_options & ATM_ATMOPT_CLP) ?
1184	    DESCRIPTOR_CLP : 0) | flags | pos >> 4);
1185	if (lvcc->tx.buf.ptr >= lvcc->tx.buf.end)
1186		lvcc->tx.buf.ptr = lvcc->tx.buf.start;
1187}
1188
1189/* Add 32-bit AAL5 trailer and leave room for its CRC */
1190static inline void vcc_tx_add_aal5_trailer(struct lanai_vcc *lvcc,
1191	int len, int cpi, int uu)
1192{
1193	APRINTK((((unsigned long) lvcc->tx.buf.ptr) & 15) == 8,
1194	    "vcc_tx_add_aal5_trailer: bad ptr=%p\n", lvcc->tx.buf.ptr);
1195	lvcc->tx.buf.ptr += 2;
1196	lvcc->tx.buf.ptr[-2] = cpu_to_be32((uu << 24) | (cpi << 16) | len);
1197	if (lvcc->tx.buf.ptr >= lvcc->tx.buf.end)
1198		lvcc->tx.buf.ptr = lvcc->tx.buf.start;
1199}
1200
1201static inline void vcc_tx_memcpy(struct lanai_vcc *lvcc,
1202	const unsigned char *src, int n)
1203{
1204	unsigned char *e;
1205	int m;
1206	e = ((unsigned char *) lvcc->tx.buf.ptr) + n;
1207	m = e - (unsigned char *) lvcc->tx.buf.end;
1208	if (m < 0)
1209		m = 0;
1210	memcpy(lvcc->tx.buf.ptr, src, n - m);
1211	if (m != 0) {
1212		memcpy(lvcc->tx.buf.start, src + n - m, m);
1213		e = ((unsigned char *) lvcc->tx.buf.start) + m;
1214	}
1215	lvcc->tx.buf.ptr = (u32 *) e;
1216}
1217
1218static inline void vcc_tx_memzero(struct lanai_vcc *lvcc, int n)
1219{
1220	unsigned char *e;
1221	int m;
1222	if (n == 0)
1223		return;
1224	e = ((unsigned char *) lvcc->tx.buf.ptr) + n;
1225	m = e - (unsigned char *) lvcc->tx.buf.end;
1226	if (m < 0)
1227		m = 0;
1228	memset(lvcc->tx.buf.ptr, 0, n - m);
1229	if (m != 0) {
1230		memset(lvcc->tx.buf.start, 0, m);
1231		e = ((unsigned char *) lvcc->tx.buf.start) + m;
1232	}
1233	lvcc->tx.buf.ptr = (u32 *) e;
1234}
1235
1236/* Update "butt" register to specify new WritePtr */
1237static inline void lanai_endtx(struct lanai_dev *lanai,
1238	const struct lanai_vcc *lvcc)
1239{
1240	int i, ptr = ((unsigned char *) lvcc->tx.buf.ptr) -
1241	    (unsigned char *) lvcc->tx.buf.start;
1242	APRINTK((ptr & ~0x0001FFF0) == 0,
1243	    "lanai_endtx: bad ptr (%d), vci=%d, start,ptr,end=%p,%p,%p\n",
1244	    ptr, lvcc->vci, lvcc->tx.buf.start, lvcc->tx.buf.ptr,
1245	    lvcc->tx.buf.end);
1246
1247	/*
1248	 * Since the "butt register" is a shared resounce on the card we
1249	 * serialize all accesses to it through this spinlock.  This is
1250	 * mostly just paranoia since the register is rarely "busy" anyway
1251	 * but is needed for correctness.
1252	 */
1253	spin_lock(&lanai->endtxlock);
1254	/*
1255	 * We need to check if the "butt busy" bit is set before
1256	 * updating the butt register.  In theory this should
1257	 * never happen because the ATM card is plenty fast at
1258	 * updating the register.  Still, we should make sure
1259	 */
1260	for (i = 0; reg_read(lanai, Status_Reg) & STATUS_BUTTBUSY; i++) {
1261		if (unlikely(i > 50)) {
1262			printk(KERN_ERR DEV_LABEL "(itf %d): butt register "
1263			    "always busy!\n", lanai->number);
1264			break;
1265		}
1266		udelay(5);
1267	}
1268	/*
1269	 * Before we tall the card to start work we need to be sure 100% of
1270	 * the info in the service buffer has been written before we tell
1271	 * the card about it
1272	 */
1273	wmb();
1274	reg_write(lanai, (ptr << 12) | lvcc->vci, Butt_Reg);
1275	spin_unlock(&lanai->endtxlock);
1276}
1277
1278/*
1279 * Add one AAL5 PDU to lvcc's transmit buffer.  Caller garauntees there's
1280 * space available.  "pdusize" is the number of bytes the PDU will take
1281 */
1282static void lanai_send_one_aal5(struct lanai_dev *lanai,
1283	struct lanai_vcc *lvcc, struct sk_buff *skb, int pdusize)
1284{
1285	int pad;
1286	APRINTK(pdusize == aal5_size(skb->len),
1287	    "lanai_send_one_aal5: wrong size packet (%d != %d)\n",
1288	    pdusize, aal5_size(skb->len));
1289	vcc_tx_add_aal5_descriptor(lvcc, 0, pdusize);
1290	pad = pdusize - skb->len - 8;
1291	APRINTK(pad >= 0, "pad is negative (%d)\n", pad);
1292	APRINTK(pad < 48, "pad is too big (%d)\n", pad);
1293	vcc_tx_memcpy(lvcc, skb->data, skb->len);
1294	vcc_tx_memzero(lvcc, pad);
1295	vcc_tx_add_aal5_trailer(lvcc, skb->len, 0, 0);
1296	lanai_endtx(lanai, lvcc);
1297	lanai_free_skb(lvcc->tx.atmvcc, skb);
1298	atomic_inc(&lvcc->tx.atmvcc->stats->tx);
1299}
1300
1301/* Try to fill the buffer - don't call unless there is backlog */
1302static void vcc_tx_unqueue_aal5(struct lanai_dev *lanai,
1303	struct lanai_vcc *lvcc, int endptr)
1304{
1305	int n;
1306	struct sk_buff *skb;
1307	int space = vcc_tx_space(lvcc, endptr);
1308	APRINTK(vcc_is_backlogged(lvcc),
1309	    "vcc_tx_unqueue() called with empty backlog (vci=%d)\n",
1310	    lvcc->vci);
1311	while (space >= 64) {
1312		skb = skb_dequeue(&lvcc->tx.backlog);
1313		if (skb == NULL)
1314			goto no_backlog;
1315		n = aal5_size(skb->len);
1316		if (n + 16 > space) {
1317			/* No room for this packet - put it back on queue */
1318			skb_queue_head(&lvcc->tx.backlog, skb);
1319			return;
1320		}
1321		lanai_send_one_aal5(lanai, lvcc, skb, n);
1322		space -= n + 16;
1323	}
1324	if (!vcc_is_backlogged(lvcc)) {
1325	    no_backlog:
1326		__clear_bit(lvcc->vci, lanai->backlog_vccs);
1327	}
1328}
1329
1330/* Given an skb that we want to transmit either send it now or queue */
1331static void vcc_tx_aal5(struct lanai_dev *lanai, struct lanai_vcc *lvcc,
1332	struct sk_buff *skb)
1333{
1334	int space, n;
1335	if (vcc_is_backlogged(lvcc))		/* Already backlogged */
1336		goto queue_it;
1337	space = vcc_tx_space(lvcc,
1338		    TXREADPTR_GET_PTR(cardvcc_read(lvcc, vcc_txreadptr)));
1339	n = aal5_size(skb->len);
1340	APRINTK(n + 16 >= 64, "vcc_tx_aal5: n too small (%d)\n", n);
1341	if (space < n + 16) {			/* No space for this PDU */
1342		__set_bit(lvcc->vci, lanai->backlog_vccs);
1343	    queue_it:
1344		skb_queue_tail(&lvcc->tx.backlog, skb);
1345		return;
1346	}
1347	lanai_send_one_aal5(lanai, lvcc, skb, n);
1348}
1349
1350static void vcc_tx_unqueue_aal0(struct lanai_dev *lanai,
1351	struct lanai_vcc *lvcc, int endptr)
1352{
1353	printk(KERN_INFO DEV_LABEL
1354	    ": vcc_tx_unqueue_aal0: not implemented\n");
1355}
1356
1357static void vcc_tx_aal0(struct lanai_dev *lanai, struct lanai_vcc *lvcc,
1358	struct sk_buff *skb)
1359{
1360	printk(KERN_INFO DEV_LABEL ": vcc_tx_aal0: not implemented\n");
1361	/* Remember to increment lvcc->tx.atmvcc->stats->tx */
1362	lanai_free_skb(lvcc->tx.atmvcc, skb);
1363}
1364
1365/* -------------------- VCC RX BUFFER UTILITIES: */
1366
1367/* unlike the _tx_ cousins, this doesn't update ptr */
1368static inline void vcc_rx_memcpy(unsigned char *dest,
1369	const struct lanai_vcc *lvcc, int n)
1370{
1371	int m = ((const unsigned char *) lvcc->rx.buf.ptr) + n -
1372	    ((const unsigned char *) (lvcc->rx.buf.end));
1373	if (m < 0)
1374		m = 0;
1375	memcpy(dest, lvcc->rx.buf.ptr, n - m);
1376	memcpy(dest + n - m, lvcc->rx.buf.start, m);
1377	/* Make sure that these copies don't get reordered */
1378	barrier();
1379}
1380
1381/* Receive AAL5 data on a VCC with a particular endptr */
1382static void vcc_rx_aal5(struct lanai_vcc *lvcc, int endptr)
1383{
1384	int size;
1385	struct sk_buff *skb;
1386	const u32 *x;
1387	u32 *end = &lvcc->rx.buf.start[endptr * 4];
1388	int n = ((unsigned long) end) - ((unsigned long) lvcc->rx.buf.ptr);
1389	if (n < 0)
1390		n += lanai_buf_size(&lvcc->rx.buf);
1391	APRINTK(n >= 0 && n < lanai_buf_size(&lvcc->rx.buf) && !(n & 15),
1392	    "vcc_rx_aal5: n out of range (%d/%Zu)\n",
1393	    n, lanai_buf_size(&lvcc->rx.buf));
1394	/* Recover the second-to-last word to get true pdu length */
1395	if ((x = &end[-2]) < lvcc->rx.buf.start)
1396		x = &lvcc->rx.buf.end[-2];
1397	/*
1398	 * Before we actually read from the buffer, make sure the memory
1399	 * changes have arrived
1400	 */
1401	rmb();
1402	size = be32_to_cpup(x) & 0xffff;
1403	if (unlikely(n != aal5_size(size))) {
1404		/* Make sure size matches padding */
1405		printk(KERN_INFO DEV_LABEL "(itf %d): Got bad AAL5 length "
1406		    "on vci=%d - size=%d n=%d\n",
1407		    lvcc->rx.atmvcc->dev->number, lvcc->vci, size, n);
1408		lvcc->stats.x.aal5.rx_badlen++;
1409		goto out;
1410	}
1411	skb = atm_alloc_charge(lvcc->rx.atmvcc, size, GFP_ATOMIC);
1412	if (unlikely(skb == NULL)) {
1413		lvcc->stats.rx_nomem++;
1414		goto out;
1415	}
1416	skb_put(skb, size);
1417	vcc_rx_memcpy(skb->data, lvcc, size);
1418	ATM_SKB(skb)->vcc = lvcc->rx.atmvcc;
1419	__net_timestamp(skb);
1420	lvcc->rx.atmvcc->push(lvcc->rx.atmvcc, skb);
1421	atomic_inc(&lvcc->rx.atmvcc->stats->rx);
1422    out:
1423	lvcc->rx.buf.ptr = end;
1424	cardvcc_write(lvcc, endptr, vcc_rxreadptr);
1425}
1426
1427static void vcc_rx_aal0(struct lanai_dev *lanai)
1428{
1429	printk(KERN_INFO DEV_LABEL ": vcc_rx_aal0: not implemented\n");
1430	/* Remember to get read_lock(&vcc_sklist_lock) while looking up VC */
1431	/* Remember to increment lvcc->rx.atmvcc->stats->rx */
1432}
1433
1434/* -------------------- MANAGING HOST-BASED VCC TABLE: */
1435
1436/* Decide whether to use vmalloc or get_zeroed_page for VCC table */
1437#if (NUM_VCI * BITS_PER_LONG) <= PAGE_SIZE
1438#define VCCTABLE_GETFREEPAGE
1439#else
1440#include <linux/vmalloc.h>
1441#endif
1442
1443static int vcc_table_allocate(struct lanai_dev *lanai)
1444{
1445#ifdef VCCTABLE_GETFREEPAGE
1446	APRINTK((lanai->num_vci) * sizeof(struct lanai_vcc *) <= PAGE_SIZE,
1447	    "vcc table > PAGE_SIZE!");
1448	lanai->vccs = (struct lanai_vcc **) get_zeroed_page(GFP_KERNEL);
1449	return (lanai->vccs == NULL) ? -ENOMEM : 0;
1450#else
1451	int bytes = (lanai->num_vci) * sizeof(struct lanai_vcc *);
1452	lanai->vccs = vzalloc(bytes);
1453	if (unlikely(lanai->vccs == NULL))
1454		return -ENOMEM;
1455	return 0;
1456#endif
1457}
1458
1459static inline void vcc_table_deallocate(const struct lanai_dev *lanai)
1460{
1461#ifdef VCCTABLE_GETFREEPAGE
1462	free_page((unsigned long) lanai->vccs);
1463#else
1464	vfree(lanai->vccs);
1465#endif
1466}
1467
1468/* Allocate a fresh lanai_vcc, with the appropriate things cleared */
1469static inline struct lanai_vcc *new_lanai_vcc(void)
1470{
1471	struct lanai_vcc *lvcc;
1472	lvcc =  kzalloc(sizeof(*lvcc), GFP_KERNEL);
1473	if (likely(lvcc != NULL)) {
1474		skb_queue_head_init(&lvcc->tx.backlog);
1475#ifdef DEBUG
1476		lvcc->vci = -1;
1477#endif
1478	}
1479	return lvcc;
1480}
1481
1482static int lanai_get_sized_buffer(struct lanai_dev *lanai,
1483	struct lanai_buffer *buf, int max_sdu, int multiplier,
1484	const char *name)
1485{
1486	int size;
1487	if (unlikely(max_sdu < 1))
1488		max_sdu = 1;
1489	max_sdu = aal5_size(max_sdu);
1490	size = (max_sdu + 16) * multiplier + 16;
1491	lanai_buf_allocate(buf, size, max_sdu + 32, lanai->pci);
1492	if (unlikely(buf->start == NULL))
1493		return -ENOMEM;
1494	if (unlikely(lanai_buf_size(buf) < size))
1495		printk(KERN_WARNING DEV_LABEL "(itf %d): wanted %d bytes "
1496		    "for %s buffer, got only %Zu\n", lanai->number, size,
1497		    name, lanai_buf_size(buf));
1498	DPRINTK("Allocated %Zu byte %s buffer\n", lanai_buf_size(buf), name);
1499	return 0;
1500}
1501
1502/* Setup a RX buffer for a currently unbound AAL5 vci */
1503static inline int lanai_setup_rx_vci_aal5(struct lanai_dev *lanai,
1504	struct lanai_vcc *lvcc, const struct atm_qos *qos)
1505{
1506	return lanai_get_sized_buffer(lanai, &lvcc->rx.buf,
1507	    qos->rxtp.max_sdu, AAL5_RX_MULTIPLIER, "RX");
1508}
1509
1510/* Setup a TX buffer for a currently unbound AAL5 vci */
1511static int lanai_setup_tx_vci(struct lanai_dev *lanai, struct lanai_vcc *lvcc,
1512	const struct atm_qos *qos)
1513{
1514	int max_sdu, multiplier;
1515	if (qos->aal == ATM_AAL0) {
1516		lvcc->tx.unqueue = vcc_tx_unqueue_aal0;
1517		max_sdu = ATM_CELL_SIZE - 1;
1518		multiplier = AAL0_TX_MULTIPLIER;
1519	} else {
1520		lvcc->tx.unqueue = vcc_tx_unqueue_aal5;
1521		max_sdu = qos->txtp.max_sdu;
1522		multiplier = AAL5_TX_MULTIPLIER;
1523	}
1524	return lanai_get_sized_buffer(lanai, &lvcc->tx.buf, max_sdu,
1525	    multiplier, "TX");
1526}
1527
1528static inline void host_vcc_bind(struct lanai_dev *lanai,
1529	struct lanai_vcc *lvcc, vci_t vci)
1530{
1531	if (lvcc->vbase != NULL)
1532		return;    /* We already were bound in the other direction */
1533	DPRINTK("Binding vci %d\n", vci);
1534#ifdef USE_POWERDOWN
1535	if (lanai->nbound++ == 0) {
1536		DPRINTK("Coming out of powerdown\n");
1537		lanai->conf1 &= ~CONFIG1_POWERDOWN;
1538		conf1_write(lanai);
1539		conf2_write(lanai);
1540	}
1541#endif
1542	lvcc->vbase = cardvcc_addr(lanai, vci);
1543	lanai->vccs[lvcc->vci = vci] = lvcc;
1544}
1545
1546static inline void host_vcc_unbind(struct lanai_dev *lanai,
1547	struct lanai_vcc *lvcc)
1548{
1549	if (lvcc->vbase == NULL)
1550		return;	/* This vcc was never bound */
1551	DPRINTK("Unbinding vci %d\n", lvcc->vci);
1552	lvcc->vbase = NULL;
1553	lanai->vccs[lvcc->vci] = NULL;
1554#ifdef USE_POWERDOWN
1555	if (--lanai->nbound == 0) {
1556		DPRINTK("Going into powerdown\n");
1557		lanai->conf1 |= CONFIG1_POWERDOWN;
1558		conf1_write(lanai);
1559	}
1560#endif
1561}
1562
1563/* -------------------- RESET CARD: */
1564
1565static void lanai_reset(struct lanai_dev *lanai)
1566{
1567	printk(KERN_CRIT DEV_LABEL "(itf %d): *NOT* resetting - not "
1568	    "implemented\n", lanai->number);
1569	/* TODO */
1570	/* The following is just a hack until we write the real
1571	 * resetter - at least ack whatever interrupt sent us
1572	 * here
1573	 */
1574	reg_write(lanai, INT_ALL, IntAck_Reg);
1575	lanai->stats.card_reset++;
1576}
1577
1578/* -------------------- SERVICE LIST UTILITIES: */
1579
1580/*
1581 * Allocate service buffer and tell card about it
1582 */
1583static int service_buffer_allocate(struct lanai_dev *lanai)
1584{
1585	lanai_buf_allocate(&lanai->service, SERVICE_ENTRIES * 4, 8,
1586	    lanai->pci);
1587	if (unlikely(lanai->service.start == NULL))
1588		return -ENOMEM;
1589	DPRINTK("allocated service buffer at 0x%08lX, size %Zu(%d)\n",
1590	    (unsigned long) lanai->service.start,
1591	    lanai_buf_size(&lanai->service),
1592	    lanai_buf_size_cardorder(&lanai->service));
1593	/* Clear ServWrite register to be safe */
1594	reg_write(lanai, 0, ServWrite_Reg);
1595	/* ServiceStuff register contains size and address of buffer */
1596	reg_write(lanai,
1597	    SSTUFF_SET_SIZE(lanai_buf_size_cardorder(&lanai->service)) |
1598	    SSTUFF_SET_ADDR(lanai->service.dmaaddr),
1599	    ServiceStuff_Reg);
1600	return 0;
1601}
1602
1603static inline void service_buffer_deallocate(struct lanai_dev *lanai)
1604{
1605	lanai_buf_deallocate(&lanai->service, lanai->pci);
1606}
1607
1608/* Bitfields in service list */
1609#define SERVICE_TX	(0x80000000)	/* Was from transmission */
1610#define SERVICE_TRASH	(0x40000000)	/* RXed PDU was trashed */
1611#define SERVICE_CRCERR	(0x20000000)	/* RXed PDU had CRC error */
1612#define SERVICE_CI	(0x10000000)	/* RXed PDU had CI set */
1613#define SERVICE_CLP	(0x08000000)	/* RXed PDU had CLP set */
1614#define SERVICE_STREAM	(0x04000000)	/* RX Stream mode */
1615#define SERVICE_GET_VCI(x) (((x)>>16)&0x3FF)
1616#define SERVICE_GET_END(x) ((x)&0x1FFF)
1617
1618/* Handle one thing from the service list - returns true if it marked a
1619 * VCC ready for xmit
1620 */
1621static int handle_service(struct lanai_dev *lanai, u32 s)
1622{
1623	vci_t vci = SERVICE_GET_VCI(s);
1624	struct lanai_vcc *lvcc;
1625	read_lock(&vcc_sklist_lock);
1626	lvcc = lanai->vccs[vci];
1627	if (unlikely(lvcc == NULL)) {
1628		read_unlock(&vcc_sklist_lock);
1629		DPRINTK("(itf %d) got service entry 0x%X for nonexistent "
1630		    "vcc %d\n", lanai->number, (unsigned int) s, vci);
1631		if (s & SERVICE_TX)
1632			lanai->stats.service_notx++;
1633		else
1634			lanai->stats.service_norx++;
1635		return 0;
1636	}
1637	if (s & SERVICE_TX) {			/* segmentation interrupt */
1638		if (unlikely(lvcc->tx.atmvcc == NULL)) {
1639			read_unlock(&vcc_sklist_lock);
1640			DPRINTK("(itf %d) got service entry 0x%X for non-TX "
1641			    "vcc %d\n", lanai->number, (unsigned int) s, vci);
1642			lanai->stats.service_notx++;
1643			return 0;
1644		}
1645		__set_bit(vci, lanai->transmit_ready);
1646		lvcc->tx.endptr = SERVICE_GET_END(s);
1647		read_unlock(&vcc_sklist_lock);
1648		return 1;
1649	}
1650	if (unlikely(lvcc->rx.atmvcc == NULL)) {
1651		read_unlock(&vcc_sklist_lock);
1652		DPRINTK("(itf %d) got service entry 0x%X for non-RX "
1653		    "vcc %d\n", lanai->number, (unsigned int) s, vci);
1654		lanai->stats.service_norx++;
1655		return 0;
1656	}
1657	if (unlikely(lvcc->rx.atmvcc->qos.aal != ATM_AAL5)) {
1658		read_unlock(&vcc_sklist_lock);
1659		DPRINTK("(itf %d) got RX service entry 0x%X for non-AAL5 "
1660		    "vcc %d\n", lanai->number, (unsigned int) s, vci);
1661		lanai->stats.service_rxnotaal5++;
1662		atomic_inc(&lvcc->rx.atmvcc->stats->rx_err);
1663		return 0;
1664	}
1665	if (likely(!(s & (SERVICE_TRASH | SERVICE_STREAM | SERVICE_CRCERR)))) {
1666		vcc_rx_aal5(lvcc, SERVICE_GET_END(s));
1667		read_unlock(&vcc_sklist_lock);
1668		return 0;
1669	}
1670	if (s & SERVICE_TRASH) {
1671		int bytes;
1672		read_unlock(&vcc_sklist_lock);
1673		DPRINTK("got trashed rx pdu on vci %d\n", vci);
1674		atomic_inc(&lvcc->rx.atmvcc->stats->rx_err);
1675		lvcc->stats.x.aal5.service_trash++;
1676		bytes = (SERVICE_GET_END(s) * 16) -
1677		    (((unsigned long) lvcc->rx.buf.ptr) -
1678		    ((unsigned long) lvcc->rx.buf.start)) + 47;
1679		if (bytes < 0)
1680			bytes += lanai_buf_size(&lvcc->rx.buf);
1681		lanai->stats.ovfl_trash += (bytes / 48);
1682		return 0;
1683	}
1684	if (s & SERVICE_STREAM) {
1685		read_unlock(&vcc_sklist_lock);
1686		atomic_inc(&lvcc->rx.atmvcc->stats->rx_err);
1687		lvcc->stats.x.aal5.service_stream++;
1688		printk(KERN_ERR DEV_LABEL "(itf %d): Got AAL5 stream "
1689		    "PDU on VCI %d!\n", lanai->number, vci);
1690		lanai_reset(lanai);
1691		return 0;
1692	}
1693	DPRINTK("got rx crc error on vci %d\n", vci);
1694	atomic_inc(&lvcc->rx.atmvcc->stats->rx_err);
1695	lvcc->stats.x.aal5.service_rxcrc++;
1696	lvcc->rx.buf.ptr = &lvcc->rx.buf.start[SERVICE_GET_END(s) * 4];
1697	cardvcc_write(lvcc, SERVICE_GET_END(s), vcc_rxreadptr);
1698	read_unlock(&vcc_sklist_lock);
1699	return 0;
1700}
1701
1702/* Try transmitting on all VCIs that we marked ready to serve */
1703static void iter_transmit(struct lanai_dev *lanai, vci_t vci)
1704{
1705	struct lanai_vcc *lvcc = lanai->vccs[vci];
1706	if (vcc_is_backlogged(lvcc))
1707		lvcc->tx.unqueue(lanai, lvcc, lvcc->tx.endptr);
1708}
1709
1710/* Run service queue -- called from interrupt context or with
1711 * interrupts otherwise disabled and with the lanai->servicelock
1712 * lock held
1713 */
1714static void run_service(struct lanai_dev *lanai)
1715{
1716	int ntx = 0;
1717	u32 wreg = reg_read(lanai, ServWrite_Reg);
1718	const u32 *end = lanai->service.start + wreg;
1719	while (lanai->service.ptr != end) {
1720		ntx += handle_service(lanai,
1721		    le32_to_cpup(lanai->service.ptr++));
1722		if (lanai->service.ptr >= lanai->service.end)
1723			lanai->service.ptr = lanai->service.start;
1724	}
1725	reg_write(lanai, wreg, ServRead_Reg);
1726	if (ntx != 0) {
1727		read_lock(&vcc_sklist_lock);
1728		vci_bitfield_iterate(lanai, lanai->transmit_ready,
1729		    iter_transmit);
1730		bitmap_zero(lanai->transmit_ready, NUM_VCI);
1731		read_unlock(&vcc_sklist_lock);
1732	}
1733}
1734
1735/* -------------------- GATHER STATISTICS: */
1736
1737static void get_statistics(struct lanai_dev *lanai)
1738{
1739	u32 statreg = reg_read(lanai, Statistics_Reg);
1740	lanai->stats.atm_ovfl += STATS_GET_FIFO_OVFL(statreg);
1741	lanai->stats.hec_err += STATS_GET_HEC_ERR(statreg);
1742	lanai->stats.vci_trash += STATS_GET_BAD_VCI(statreg);
1743	lanai->stats.ovfl_trash += STATS_GET_BUF_OVFL(statreg);
1744}
1745
1746/* -------------------- POLLING TIMER: */
1747
1748#ifndef DEBUG_RW
1749/* Try to undequeue 1 backlogged vcc */
1750static void iter_dequeue(struct lanai_dev *lanai, vci_t vci)
1751{
1752	struct lanai_vcc *lvcc = lanai->vccs[vci];
1753	int endptr;
1754	if (lvcc == NULL || lvcc->tx.atmvcc == NULL ||
1755	    !vcc_is_backlogged(lvcc)) {
1756		__clear_bit(vci, lanai->backlog_vccs);
1757		return;
1758	}
1759	endptr = TXREADPTR_GET_PTR(cardvcc_read(lvcc, vcc_txreadptr));
1760	lvcc->tx.unqueue(lanai, lvcc, endptr);
1761}
1762#endif /* !DEBUG_RW */
1763
1764static void lanai_timed_poll(unsigned long arg)
1765{
1766	struct lanai_dev *lanai = (struct lanai_dev *) arg;
1767#ifndef DEBUG_RW
1768	unsigned long flags;
1769#ifdef USE_POWERDOWN
1770	if (lanai->conf1 & CONFIG1_POWERDOWN)
1771		return;
1772#endif /* USE_POWERDOWN */
1773	local_irq_save(flags);
1774	/* If we can grab the spinlock, check if any services need to be run */
1775	if (spin_trylock(&lanai->servicelock)) {
1776		run_service(lanai);
1777		spin_unlock(&lanai->servicelock);
1778	}
1779	/* ...and see if any backlogged VCs can make progress */
1780	/* unfortunately linux has no read_trylock() currently */
1781	read_lock(&vcc_sklist_lock);
1782	vci_bitfield_iterate(lanai, lanai->backlog_vccs, iter_dequeue);
1783	read_unlock(&vcc_sklist_lock);
1784	local_irq_restore(flags);
1785
1786	get_statistics(lanai);
1787#endif /* !DEBUG_RW */
1788	mod_timer(&lanai->timer, jiffies + LANAI_POLL_PERIOD);
1789}
1790
1791static inline void lanai_timed_poll_start(struct lanai_dev *lanai)
1792{
1793	init_timer(&lanai->timer);
1794	lanai->timer.expires = jiffies + LANAI_POLL_PERIOD;
1795	lanai->timer.data = (unsigned long) lanai;
1796	lanai->timer.function = lanai_timed_poll;
1797	add_timer(&lanai->timer);
1798}
1799
1800static inline void lanai_timed_poll_stop(struct lanai_dev *lanai)
1801{
1802	del_timer_sync(&lanai->timer);
1803}
1804
1805/* -------------------- INTERRUPT SERVICE: */
1806
1807static inline void lanai_int_1(struct lanai_dev *lanai, u32 reason)
1808{
1809	u32 ack = 0;
1810	if (reason & INT_SERVICE) {
1811		ack = INT_SERVICE;
1812		spin_lock(&lanai->servicelock);
1813		run_service(lanai);
1814		spin_unlock(&lanai->servicelock);
1815	}
1816	if (reason & (INT_AAL0_STR | INT_AAL0)) {
1817		ack |= reason & (INT_AAL0_STR | INT_AAL0);
1818		vcc_rx_aal0(lanai);
1819	}
1820	/* The rest of the interrupts are pretty rare */
1821	if (ack == reason)
1822		goto done;
1823	if (reason & INT_STATS) {
1824		reason &= ~INT_STATS;	/* No need to ack */
1825		get_statistics(lanai);
1826	}
1827	if (reason & INT_STATUS) {
1828		ack |= reason & INT_STATUS;
1829		lanai_check_status(lanai);
1830	}
1831	if (unlikely(reason & INT_DMASHUT)) {
1832		printk(KERN_ERR DEV_LABEL "(itf %d): driver error - DMA "
1833		    "shutdown, reason=0x%08X, address=0x%08X\n",
1834		    lanai->number, (unsigned int) (reason & INT_DMASHUT),
1835		    (unsigned int) reg_read(lanai, DMA_Addr_Reg));
1836		if (reason & INT_TABORTBM) {
1837			lanai_reset(lanai);
1838			return;
1839		}
1840		ack |= (reason & INT_DMASHUT);
1841		printk(KERN_ERR DEV_LABEL "(itf %d): re-enabling DMA\n",
1842		    lanai->number);
1843		conf1_write(lanai);
1844		lanai->stats.dma_reenable++;
1845		pcistatus_check(lanai, 0);
1846	}
1847	if (unlikely(reason & INT_TABORTSENT)) {
1848		ack |= (reason & INT_TABORTSENT);
1849		printk(KERN_ERR DEV_LABEL "(itf %d): sent PCI target abort\n",
1850		    lanai->number);
1851		pcistatus_check(lanai, 0);
1852	}
1853	if (unlikely(reason & INT_SEGSHUT)) {
1854		printk(KERN_ERR DEV_LABEL "(itf %d): driver error - "
1855		    "segmentation shutdown, reason=0x%08X\n", lanai->number,
1856		    (unsigned int) (reason & INT_SEGSHUT));
1857		lanai_reset(lanai);
1858		return;
1859	}
1860	if (unlikely(reason & (INT_PING | INT_WAKE))) {
1861		printk(KERN_ERR DEV_LABEL "(itf %d): driver error - "
1862		    "unexpected interrupt 0x%08X, resetting\n",
1863		    lanai->number,
1864		    (unsigned int) (reason & (INT_PING | INT_WAKE)));
1865		lanai_reset(lanai);
1866		return;
1867	}
1868#ifdef DEBUG
1869	if (unlikely(ack != reason)) {
1870		DPRINTK("unacked ints: 0x%08X\n",
1871		    (unsigned int) (reason & ~ack));
1872		ack = reason;
1873	}
1874#endif
1875   done:
1876	if (ack != 0)
1877		reg_write(lanai, ack, IntAck_Reg);
1878}
1879
1880static irqreturn_t lanai_int(int irq, void *devid)
1881{
1882	struct lanai_dev *lanai = devid;
1883	u32 reason;
1884
1885#ifdef USE_POWERDOWN
1886	/*
1887	 * If we're powered down we shouldn't be generating any interrupts -
1888	 * so assume that this is a shared interrupt line and it's for someone
1889	 * else
1890	 */
1891	if (unlikely(lanai->conf1 & CONFIG1_POWERDOWN))
1892		return IRQ_NONE;
1893#endif
1894
1895	reason = intr_pending(lanai);
1896	if (reason == 0)
1897		return IRQ_NONE;	/* Must be for someone else */
1898
1899	do {
1900		if (unlikely(reason == 0xFFFFFFFF))
1901			break;		/* Maybe we've been unplugged? */
1902		lanai_int_1(lanai, reason);
1903		reason = intr_pending(lanai);
1904	} while (reason != 0);
1905
1906	return IRQ_HANDLED;
1907}
1908
1909/* TODO - it would be nice if we could use the "delayed interrupt" system
1910 *   to some advantage
1911 */
1912
1913/* -------------------- CHECK BOARD ID/REV: */
1914
1915/*
1916 * The board id and revision are stored both in the reset register and
1917 * in the PCI configuration space - the documentation says to check
1918 * each of them.  If revp!=NULL we store the revision there
1919 */
1920static int check_board_id_and_rev(const char *name, u32 val, int *revp)
1921{
1922	DPRINTK("%s says board_id=%d, board_rev=%d\n", name,
1923		(int) RESET_GET_BOARD_ID(val),
1924		(int) RESET_GET_BOARD_REV(val));
1925	if (RESET_GET_BOARD_ID(val) != BOARD_ID_LANAI256) {
1926		printk(KERN_ERR DEV_LABEL ": Found %s board-id %d -- not a "
1927		    "Lanai 25.6\n", name, (int) RESET_GET_BOARD_ID(val));
1928		return -ENODEV;
1929	}
1930	if (revp != NULL)
1931		*revp = RESET_GET_BOARD_REV(val);
1932	return 0;
1933}
1934
1935/* -------------------- PCI INITIALIZATION/SHUTDOWN: */
1936
1937static int lanai_pci_start(struct lanai_dev *lanai)
1938{
1939	struct pci_dev *pci = lanai->pci;
1940	int result;
1941
1942	if (pci_enable_device(pci) != 0) {
1943		printk(KERN_ERR DEV_LABEL "(itf %d): can't enable "
1944		    "PCI device", lanai->number);
1945		return -ENXIO;
1946	}
1947	pci_set_master(pci);
1948	if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32)) != 0) {
1949		printk(KERN_WARNING DEV_LABEL
1950		    "(itf %d): No suitable DMA available.\n", lanai->number);
1951		return -EBUSY;
1952	}
1953	result = check_board_id_and_rev("PCI", pci->subsystem_device, NULL);
1954	if (result != 0)
1955		return result;
1956	/* Set latency timer to zero as per lanai docs */
1957	result = pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0);
1958	if (result != PCIBIOS_SUCCESSFUL) {
1959		printk(KERN_ERR DEV_LABEL "(itf %d): can't write "
1960		    "PCI_LATENCY_TIMER: %d\n", lanai->number, result);
1961		return -EINVAL;
1962	}
1963	pcistatus_check(lanai, 1);
1964	pcistatus_check(lanai, 0);
1965	return 0;
1966}
1967
1968/* -------------------- VPI/VCI ALLOCATION: */
1969
1970/*
1971 * We _can_ use VCI==0 for normal traffic, but only for UBR (or we'll
1972 * get a CBRZERO interrupt), and we can use it only if no one is receiving
1973 * AAL0 traffic (since they will use the same queue) - according to the
1974 * docs we shouldn't even use it for AAL0 traffic
1975 */
1976static inline int vci0_is_ok(struct lanai_dev *lanai,
1977	const struct atm_qos *qos)
1978{
1979	if (qos->txtp.traffic_class == ATM_CBR || qos->aal == ATM_AAL0)
1980		return 0;
1981	if (qos->rxtp.traffic_class != ATM_NONE) {
1982		if (lanai->naal0 != 0)
1983			return 0;
1984		lanai->conf2 |= CONFIG2_VCI0_NORMAL;
1985		conf2_write_if_powerup(lanai);
1986	}
1987	return 1;
1988}
1989
1990/* return true if vci is currently unused, or if requested qos is
1991 * compatible
1992 */
1993static int vci_is_ok(struct lanai_dev *lanai, vci_t vci,
1994	const struct atm_vcc *atmvcc)
1995{
1996	const struct atm_qos *qos = &atmvcc->qos;
1997	const struct lanai_vcc *lvcc = lanai->vccs[vci];
1998	if (vci == 0 && !vci0_is_ok(lanai, qos))
1999		return 0;
2000	if (unlikely(lvcc != NULL)) {
2001		if (qos->rxtp.traffic_class != ATM_NONE &&
2002		    lvcc->rx.atmvcc != NULL && lvcc->rx.atmvcc != atmvcc)
2003			return 0;
2004		if (qos->txtp.traffic_class != ATM_NONE &&
2005		    lvcc->tx.atmvcc != NULL && lvcc->tx.atmvcc != atmvcc)
2006			return 0;
2007		if (qos->txtp.traffic_class == ATM_CBR &&
2008		    lanai->cbrvcc != NULL && lanai->cbrvcc != atmvcc)
2009			return 0;
2010	}
2011	if (qos->aal == ATM_AAL0 && lanai->naal0 == 0 &&
2012	    qos->rxtp.traffic_class != ATM_NONE) {
2013		const struct lanai_vcc *vci0 = lanai->vccs[0];
2014		if (vci0 != NULL && vci0->rx.atmvcc != NULL)
2015			return 0;
2016		lanai->conf2 &= ~CONFIG2_VCI0_NORMAL;
2017		conf2_write_if_powerup(lanai);
2018	}
2019	return 1;
2020}
2021
2022static int lanai_normalize_ci(struct lanai_dev *lanai,
2023	const struct atm_vcc *atmvcc, short *vpip, vci_t *vcip)
2024{
2025	switch (*vpip) {
2026		case ATM_VPI_ANY:
2027			*vpip = 0;
2028			/* FALLTHROUGH */
2029		case 0:
2030			break;
2031		default:
2032			return -EADDRINUSE;
2033	}
2034	switch (*vcip) {
2035		case ATM_VCI_ANY:
2036			for (*vcip = ATM_NOT_RSV_VCI; *vcip < lanai->num_vci;
2037			    (*vcip)++)
2038				if (vci_is_ok(lanai, *vcip, atmvcc))
2039					return 0;
2040			return -EADDRINUSE;
2041		default:
2042			if (*vcip >= lanai->num_vci || *vcip < 0 ||
2043			    !vci_is_ok(lanai, *vcip, atmvcc))
2044				return -EADDRINUSE;
2045	}
2046	return 0;
2047}
2048
2049/* -------------------- MANAGE CBR: */
2050
2051/*
2052 * CBR ICG is stored as a fixed-point number with 4 fractional bits.
2053 * Note that storing a number greater than 2046.0 will result in
2054 * incorrect shaping
2055 */
2056#define CBRICG_FRAC_BITS	(4)
2057#define CBRICG_MAX		(2046 << CBRICG_FRAC_BITS)
2058
2059/*
2060 * ICG is related to PCR with the formula PCR = MAXPCR / (ICG + 1)
2061 * where MAXPCR is (according to the docs) 25600000/(54*8),
2062 * which is equal to (3125<<9)/27.
2063 *
2064 * Solving for ICG, we get:
2065 *    ICG = MAXPCR/PCR - 1
2066 *    ICG = (3125<<9)/(27*PCR) - 1
2067 *    ICG = ((3125<<9) - (27*PCR)) / (27*PCR)
2068 *
2069 * The end result is supposed to be a fixed-point number with FRAC_BITS
2070 * bits of a fractional part, so we keep everything in the numerator
2071 * shifted by that much as we compute
2072 *
2073 */
2074static int pcr_to_cbricg(const struct atm_qos *qos)
2075{
2076	int rounddown = 0;	/* 1 = Round PCR down, i.e. round ICG _up_ */
2077	int x, icg, pcr = atm_pcr_goal(&qos->txtp);
2078	if (pcr == 0)		/* Use maximum bandwidth */
2079		return 0;
2080	if (pcr < 0) {
2081		rounddown = 1;
2082		pcr = -pcr;
2083	}
2084	x = pcr * 27;
2085	icg = (3125 << (9 + CBRICG_FRAC_BITS)) - (x << CBRICG_FRAC_BITS);
2086	if (rounddown)
2087		icg += x - 1;
2088	icg /= x;
2089	if (icg > CBRICG_MAX)
2090		icg = CBRICG_MAX;
2091	DPRINTK("pcr_to_cbricg: pcr=%d rounddown=%c icg=%d\n",
2092	    pcr, rounddown ? 'Y' : 'N', icg);
2093	return icg;
2094}
2095
2096static inline void lanai_cbr_setup(struct lanai_dev *lanai)
2097{
2098	reg_write(lanai, pcr_to_cbricg(&lanai->cbrvcc->qos), CBR_ICG_Reg);
2099	reg_write(lanai, lanai->cbrvcc->vci, CBR_PTR_Reg);
2100	lanai->conf2 |= CONFIG2_CBR_ENABLE;
2101	conf2_write(lanai);
2102}
2103
2104static inline void lanai_cbr_shutdown(struct lanai_dev *lanai)
2105{
2106	lanai->conf2 &= ~CONFIG2_CBR_ENABLE;
2107	conf2_write(lanai);
2108}
2109
2110/* -------------------- OPERATIONS: */
2111
2112/* setup a newly detected device */
2113static int lanai_dev_open(struct atm_dev *atmdev)
2114{
2115	struct lanai_dev *lanai = (struct lanai_dev *) atmdev->dev_data;
2116	unsigned long raw_base;
2117	int result;
2118
2119	DPRINTK("In lanai_dev_open()\n");
2120	/* Basic device fields */
2121	lanai->number = atmdev->number;
2122	lanai->num_vci = NUM_VCI;
2123	bitmap_zero(lanai->backlog_vccs, NUM_VCI);
2124	bitmap_zero(lanai->transmit_ready, NUM_VCI);
2125	lanai->naal0 = 0;
2126#ifdef USE_POWERDOWN
2127	lanai->nbound = 0;
2128#endif
2129	lanai->cbrvcc = NULL;
2130	memset(&lanai->stats, 0, sizeof lanai->stats);
2131	spin_lock_init(&lanai->endtxlock);
2132	spin_lock_init(&lanai->servicelock);
2133	atmdev->ci_range.vpi_bits = 0;
2134	atmdev->ci_range.vci_bits = 0;
2135	while (1 << atmdev->ci_range.vci_bits < lanai->num_vci)
2136		atmdev->ci_range.vci_bits++;
2137	atmdev->link_rate = ATM_25_PCR;
2138
2139	/* 3.2: PCI initialization */
2140	if ((result = lanai_pci_start(lanai)) != 0)
2141		goto error;
2142	raw_base = lanai->pci->resource[0].start;
2143	lanai->base = (bus_addr_t) ioremap(raw_base, LANAI_MAPPING_SIZE);
2144	if (lanai->base == NULL) {
2145		printk(KERN_ERR DEV_LABEL ": couldn't remap I/O space\n");
2146		result = -ENOMEM;
2147		goto error_pci;
2148	}
2149	/* 3.3: Reset lanai and PHY */
2150	reset_board(lanai);
2151	lanai->conf1 = reg_read(lanai, Config1_Reg);
2152	lanai->conf1 &= ~(CONFIG1_GPOUT1 | CONFIG1_POWERDOWN |
2153	    CONFIG1_MASK_LEDMODE);
2154	lanai->conf1 |= CONFIG1_SET_LEDMODE(LEDMODE_NOT_SOOL);
2155	reg_write(lanai, lanai->conf1 | CONFIG1_GPOUT1, Config1_Reg);
2156	udelay(1000);
2157	conf1_write(lanai);
2158
2159	/*
2160	 * 3.4: Turn on endian mode for big-endian hardware
2161	 *   We don't actually want to do this - the actual bit fields
2162	 *   in the endian register are not documented anywhere.
2163	 *   Instead we do the bit-flipping ourselves on big-endian
2164	 *   hardware.
2165	 *
2166	 * 3.5: get the board ID/rev by reading the reset register
2167	 */
2168	result = check_board_id_and_rev("register",
2169	    reg_read(lanai, Reset_Reg), &lanai->board_rev);
2170	if (result != 0)
2171		goto error_unmap;
2172
2173	/* 3.6: read EEPROM */
2174	if ((result = eeprom_read(lanai)) != 0)
2175		goto error_unmap;
2176	if ((result = eeprom_validate(lanai)) != 0)
2177		goto error_unmap;
2178
2179	/* 3.7: re-reset PHY, do loopback tests, setup PHY */
2180	reg_write(lanai, lanai->conf1 | CONFIG1_GPOUT1, Config1_Reg);
2181	udelay(1000);
2182	conf1_write(lanai);
2183	/* TODO - loopback tests */
2184	lanai->conf1 |= (CONFIG1_GPOUT2 | CONFIG1_GPOUT3 | CONFIG1_DMA_ENABLE);
2185	conf1_write(lanai);
2186
2187	/* 3.8/3.9: test and initialize card SRAM */
2188	if ((result = sram_test_and_clear(lanai)) != 0)
2189		goto error_unmap;
2190
2191	/* 3.10: initialize lanai registers */
2192	lanai->conf1 |= CONFIG1_DMA_ENABLE;
2193	conf1_write(lanai);
2194	if ((result = service_buffer_allocate(lanai)) != 0)
2195		goto error_unmap;
2196	if ((result = vcc_table_allocate(lanai)) != 0)
2197		goto error_service;
2198	lanai->conf2 = (lanai->num_vci >= 512 ? CONFIG2_HOWMANY : 0) |
2199	    CONFIG2_HEC_DROP |	/* ??? */ CONFIG2_PTI7_MODE;
2200	conf2_write(lanai);
2201	reg_write(lanai, TX_FIFO_DEPTH, TxDepth_Reg);
2202	reg_write(lanai, 0, CBR_ICG_Reg);	/* CBR defaults to no limit */
2203	if ((result = request_irq(lanai->pci->irq, lanai_int, IRQF_SHARED,
2204	    DEV_LABEL, lanai)) != 0) {
2205		printk(KERN_ERR DEV_LABEL ": can't allocate interrupt\n");
2206		goto error_vcctable;
2207	}
2208	mb();				/* Make sure that all that made it */
2209	intr_enable(lanai, INT_ALL & ~(INT_PING | INT_WAKE));
2210	/* 3.11: initialize loop mode (i.e. turn looping off) */
2211	lanai->conf1 = (lanai->conf1 & ~CONFIG1_MASK_LOOPMODE) |
2212	    CONFIG1_SET_LOOPMODE(LOOPMODE_NORMAL) |
2213	    CONFIG1_GPOUT2 | CONFIG1_GPOUT3;
2214	conf1_write(lanai);
2215	lanai->status = reg_read(lanai, Status_Reg);
2216	/* We're now done initializing this card */
2217#ifdef USE_POWERDOWN
2218	lanai->conf1 |= CONFIG1_POWERDOWN;
2219	conf1_write(lanai);
2220#endif
2221	memcpy(atmdev->esi, eeprom_mac(lanai), ESI_LEN);
2222	lanai_timed_poll_start(lanai);
2223	printk(KERN_NOTICE DEV_LABEL "(itf %d): rev.%d, base=0x%lx, irq=%u "
2224		"(%pMF)\n", lanai->number, (int) lanai->pci->revision,
2225		(unsigned long) lanai->base, lanai->pci->irq, atmdev->esi);
2226	printk(KERN_NOTICE DEV_LABEL "(itf %d): LANAI%s, serialno=%u(0x%X), "
2227	    "board_rev=%d\n", lanai->number,
2228	    lanai->type==lanai2 ? "2" : "HB", (unsigned int) lanai->serialno,
2229	    (unsigned int) lanai->serialno, lanai->board_rev);
2230	return 0;
2231
2232    error_vcctable:
2233	vcc_table_deallocate(lanai);
2234    error_service:
2235	service_buffer_deallocate(lanai);
2236    error_unmap:
2237	reset_board(lanai);
2238#ifdef USE_POWERDOWN
2239	lanai->conf1 = reg_read(lanai, Config1_Reg) | CONFIG1_POWERDOWN;
2240	conf1_write(lanai);
2241#endif
2242	iounmap(lanai->base);
2243    error_pci:
2244	pci_disable_device(lanai->pci);
2245    error:
2246	return result;
2247}
2248
2249/* called when device is being shutdown, and all vcc's are gone - higher
2250 * levels will deallocate the atm device for us
2251 */
2252static void lanai_dev_close(struct atm_dev *atmdev)
2253{
2254	struct lanai_dev *lanai = (struct lanai_dev *) atmdev->dev_data;
2255	printk(KERN_INFO DEV_LABEL "(itf %d): shutting down interface\n",
2256	    lanai->number);
2257	lanai_timed_poll_stop(lanai);
2258#ifdef USE_POWERDOWN
2259	lanai->conf1 = reg_read(lanai, Config1_Reg) & ~CONFIG1_POWERDOWN;
2260	conf1_write(lanai);
2261#endif
2262	intr_disable(lanai, INT_ALL);
2263	free_irq(lanai->pci->irq, lanai);
2264	reset_board(lanai);
2265#ifdef USE_POWERDOWN
2266	lanai->conf1 |= CONFIG1_POWERDOWN;
2267	conf1_write(lanai);
2268#endif
2269	pci_disable_device(lanai->pci);
2270	vcc_table_deallocate(lanai);
2271	service_buffer_deallocate(lanai);
2272	iounmap(lanai->base);
2273	kfree(lanai);
2274}
2275
2276/* close a vcc */
2277static void lanai_close(struct atm_vcc *atmvcc)
2278{
2279	struct lanai_vcc *lvcc = (struct lanai_vcc *) atmvcc->dev_data;
2280	struct lanai_dev *lanai = (struct lanai_dev *) atmvcc->dev->dev_data;
2281	if (lvcc == NULL)
2282		return;
2283	clear_bit(ATM_VF_READY, &atmvcc->flags);
2284	clear_bit(ATM_VF_PARTIAL, &atmvcc->flags);
2285	if (lvcc->rx.atmvcc == atmvcc) {
2286		lanai_shutdown_rx_vci(lvcc);
2287		if (atmvcc->qos.aal == ATM_AAL0) {
2288			if (--lanai->naal0 <= 0)
2289				aal0_buffer_free(lanai);
2290		} else
2291			lanai_buf_deallocate(&lvcc->rx.buf, lanai->pci);
2292		lvcc->rx.atmvcc = NULL;
2293	}
2294	if (lvcc->tx.atmvcc == atmvcc) {
2295		if (atmvcc == lanai->cbrvcc) {
2296			if (lvcc->vbase != NULL)
2297				lanai_cbr_shutdown(lanai);
2298			lanai->cbrvcc = NULL;
2299		}
2300		lanai_shutdown_tx_vci(lanai, lvcc);
2301		lanai_buf_deallocate(&lvcc->tx.buf, lanai->pci);
2302		lvcc->tx.atmvcc = NULL;
2303	}
2304	if (--lvcc->nref == 0) {
2305		host_vcc_unbind(lanai, lvcc);
2306		kfree(lvcc);
2307	}
2308	atmvcc->dev_data = NULL;
2309	clear_bit(ATM_VF_ADDR, &atmvcc->flags);
2310}
2311
2312/* open a vcc on the card to vpi/vci */
2313static int lanai_open(struct atm_vcc *atmvcc)
2314{
2315	struct lanai_dev *lanai;
2316	struct lanai_vcc *lvcc;
2317	int result = 0;
2318	int vci = atmvcc->vci;
2319	short vpi = atmvcc->vpi;
2320	/* we don't support partial open - it's not really useful anyway */
2321	if ((test_bit(ATM_VF_PARTIAL, &atmvcc->flags)) ||
2322	    (vpi == ATM_VPI_UNSPEC) || (vci == ATM_VCI_UNSPEC))
2323		return -EINVAL;
2324	lanai = (struct lanai_dev *) atmvcc->dev->dev_data;
2325	result = lanai_normalize_ci(lanai, atmvcc, &vpi, &vci);
2326	if (unlikely(result != 0))
2327		goto out;
2328	set_bit(ATM_VF_ADDR, &atmvcc->flags);
2329	if (atmvcc->qos.aal != ATM_AAL0 && atmvcc->qos.aal != ATM_AAL5)
2330		return -EINVAL;
2331	DPRINTK(DEV_LABEL "(itf %d): open %d.%d\n", lanai->number,
2332	    (int) vpi, vci);
2333	lvcc = lanai->vccs[vci];
2334	if (lvcc == NULL) {
2335		lvcc = new_lanai_vcc();
2336		if (unlikely(lvcc == NULL))
2337			return -ENOMEM;
2338		atmvcc->dev_data = lvcc;
2339	}
2340	lvcc->nref++;
2341	if (atmvcc->qos.rxtp.traffic_class != ATM_NONE) {
2342		APRINTK(lvcc->rx.atmvcc == NULL, "rx.atmvcc!=NULL, vci=%d\n",
2343		    vci);
2344		if (atmvcc->qos.aal == ATM_AAL0) {
2345			if (lanai->naal0 == 0)
2346				result = aal0_buffer_allocate(lanai);
2347		} else
2348			result = lanai_setup_rx_vci_aal5(
2349			    lanai, lvcc, &atmvcc->qos);
2350		if (unlikely(result != 0))
2351			goto out_free;
2352		lvcc->rx.atmvcc = atmvcc;
2353		lvcc->stats.rx_nomem = 0;
2354		lvcc->stats.x.aal5.rx_badlen = 0;
2355		lvcc->stats.x.aal5.service_trash = 0;
2356		lvcc->stats.x.aal5.service_stream = 0;
2357		lvcc->stats.x.aal5.service_rxcrc = 0;
2358		if (atmvcc->qos.aal == ATM_AAL0)
2359			lanai->naal0++;
2360	}
2361	if (atmvcc->qos.txtp.traffic_class != ATM_NONE) {
2362		APRINTK(lvcc->tx.atmvcc == NULL, "tx.atmvcc!=NULL, vci=%d\n",
2363		    vci);
2364		result = lanai_setup_tx_vci(lanai, lvcc, &atmvcc->qos);
2365		if (unlikely(result != 0))
2366			goto out_free;
2367		lvcc->tx.atmvcc = atmvcc;
2368		if (atmvcc->qos.txtp.traffic_class == ATM_CBR) {
2369			APRINTK(lanai->cbrvcc == NULL,
2370			    "cbrvcc!=NULL, vci=%d\n", vci);
2371			lanai->cbrvcc = atmvcc;
2372		}
2373	}
2374	host_vcc_bind(lanai, lvcc, vci);
2375	/*
2376	 * Make sure everything made it to RAM before we tell the card about
2377	 * the VCC
2378	 */
2379	wmb();
2380	if (atmvcc == lvcc->rx.atmvcc)
2381		host_vcc_start_rx(lvcc);
2382	if (atmvcc == lvcc->tx.atmvcc) {
2383		host_vcc_start_tx(lvcc);
2384		if (lanai->cbrvcc == atmvcc)
2385			lanai_cbr_setup(lanai);
2386	}
2387	set_bit(ATM_VF_READY, &atmvcc->flags);
2388	return 0;
2389    out_free:
2390	lanai_close(atmvcc);
2391    out:
2392	return result;
2393}
2394
2395static int lanai_send(struct atm_vcc *atmvcc, struct sk_buff *skb)
2396{
2397	struct lanai_vcc *lvcc = (struct lanai_vcc *) atmvcc->dev_data;
2398	struct lanai_dev *lanai = (struct lanai_dev *) atmvcc->dev->dev_data;
2399	unsigned long flags;
2400	if (unlikely(lvcc == NULL || lvcc->vbase == NULL ||
2401	      lvcc->tx.atmvcc != atmvcc))
2402		goto einval;
2403#ifdef DEBUG
2404	if (unlikely(skb == NULL)) {
2405		DPRINTK("lanai_send: skb==NULL for vci=%d\n", atmvcc->vci);
2406		goto einval;
2407	}
2408	if (unlikely(lanai == NULL)) {
2409		DPRINTK("lanai_send: lanai==NULL for vci=%d\n", atmvcc->vci);
2410		goto einval;
2411	}
2412#endif
2413	ATM_SKB(skb)->vcc = atmvcc;
2414	switch (atmvcc->qos.aal) {
2415		case ATM_AAL5:
2416			read_lock_irqsave(&vcc_sklist_lock, flags);
2417			vcc_tx_aal5(lanai, lvcc, skb);
2418			read_unlock_irqrestore(&vcc_sklist_lock, flags);
2419			return 0;
2420		case ATM_AAL0:
2421			if (unlikely(skb->len != ATM_CELL_SIZE-1))
2422				goto einval;
2423  /* NOTE - this next line is technically invalid - we haven't unshared skb */
2424			cpu_to_be32s((u32 *) skb->data);
2425			read_lock_irqsave(&vcc_sklist_lock, flags);
2426			vcc_tx_aal0(lanai, lvcc, skb);
2427			read_unlock_irqrestore(&vcc_sklist_lock, flags);
2428			return 0;
2429	}
2430	DPRINTK("lanai_send: bad aal=%d on vci=%d\n", (int) atmvcc->qos.aal,
2431	    atmvcc->vci);
2432    einval:
2433	lanai_free_skb(atmvcc, skb);
2434	return -EINVAL;
2435}
2436
2437static int lanai_change_qos(struct atm_vcc *atmvcc,
2438	/*const*/ struct atm_qos *qos, int flags)
2439{
2440	return -EBUSY;		/* TODO: need to write this */
2441}
2442
2443#ifndef CONFIG_PROC_FS
2444#define lanai_proc_read NULL
2445#else
2446static int lanai_proc_read(struct atm_dev *atmdev, loff_t *pos, char *page)
2447{
2448	struct lanai_dev *lanai = (struct lanai_dev *) atmdev->dev_data;
2449	loff_t left = *pos;
2450	struct lanai_vcc *lvcc;
2451	if (left-- == 0)
2452		return sprintf(page, DEV_LABEL "(itf %d): chip=LANAI%s, "
2453		    "serial=%u, magic=0x%08X, num_vci=%d\n",
2454		    atmdev->number, lanai->type==lanai2 ? "2" : "HB",
2455		    (unsigned int) lanai->serialno,
2456		    (unsigned int) lanai->magicno, lanai->num_vci);
2457	if (left-- == 0)
2458		return sprintf(page, "revision: board=%d, pci_if=%d\n",
2459		    lanai->board_rev, (int) lanai->pci->revision);
2460	if (left-- == 0)
2461		return sprintf(page, "EEPROM ESI: %pM\n",
2462		    &lanai->eeprom[EEPROM_MAC]);
2463	if (left-- == 0)
2464		return sprintf(page, "status: SOOL=%d, LOCD=%d, LED=%d, "
2465		    "GPIN=%d\n", (lanai->status & STATUS_SOOL) ? 1 : 0,
2466		    (lanai->status & STATUS_LOCD) ? 1 : 0,
2467		    (lanai->status & STATUS_LED) ? 1 : 0,
2468		    (lanai->status & STATUS_GPIN) ? 1 : 0);
2469	if (left-- == 0)
2470		return sprintf(page, "global buffer sizes: service=%Zu, "
2471		    "aal0_rx=%Zu\n", lanai_buf_size(&lanai->service),
2472		    lanai->naal0 ? lanai_buf_size(&lanai->aal0buf) : 0);
2473	if (left-- == 0) {
2474		get_statistics(lanai);
2475		return sprintf(page, "cells in error: overflow=%u, "
2476		    "closed_vci=%u, bad_HEC=%u, rx_fifo=%u\n",
2477		    lanai->stats.ovfl_trash, lanai->stats.vci_trash,
2478		    lanai->stats.hec_err, lanai->stats.atm_ovfl);
2479	}
2480	if (left-- == 0)
2481		return sprintf(page, "PCI errors: parity_detect=%u, "
2482		    "master_abort=%u, master_target_abort=%u,\n",
2483		    lanai->stats.pcierr_parity_detect,
2484		    lanai->stats.pcierr_serr_set,
2485		    lanai->stats.pcierr_m_target_abort);
2486	if (left-- == 0)
2487		return sprintf(page, "            slave_target_abort=%u, "
2488		    "master_parity=%u\n", lanai->stats.pcierr_s_target_abort,
2489		    lanai->stats.pcierr_master_parity);
2490	if (left-- == 0)
2491		return sprintf(page, "                     no_tx=%u, "
2492		    "no_rx=%u, bad_rx_aal=%u\n", lanai->stats.service_norx,
2493		    lanai->stats.service_notx,
2494		    lanai->stats.service_rxnotaal5);
2495	if (left-- == 0)
2496		return sprintf(page, "resets: dma=%u, card=%u\n",
2497		    lanai->stats.dma_reenable, lanai->stats.card_reset);
2498	/* At this point, "left" should be the VCI we're looking for */
2499	read_lock(&vcc_sklist_lock);
2500	for (; ; left++) {
2501		if (left >= NUM_VCI) {
2502			left = 0;
2503			goto out;
2504		}
2505		if ((lvcc = lanai->vccs[left]) != NULL)
2506			break;
2507		(*pos)++;
2508	}
2509	/* Note that we re-use "left" here since we're done with it */
2510	left = sprintf(page, "VCI %4d: nref=%d, rx_nomem=%u",  (vci_t) left,
2511	    lvcc->nref, lvcc->stats.rx_nomem);
2512	if (lvcc->rx.atmvcc != NULL) {
2513		left += sprintf(&page[left], ",\n          rx_AAL=%d",
2514		    lvcc->rx.atmvcc->qos.aal == ATM_AAL5 ? 5 : 0);
2515		if (lvcc->rx.atmvcc->qos.aal == ATM_AAL5)
2516			left += sprintf(&page[left], ", rx_buf_size=%Zu, "
2517			    "rx_bad_len=%u,\n          rx_service_trash=%u, "
2518			    "rx_service_stream=%u, rx_bad_crc=%u",
2519			    lanai_buf_size(&lvcc->rx.buf),
2520			    lvcc->stats.x.aal5.rx_badlen,
2521			    lvcc->stats.x.aal5.service_trash,
2522			    lvcc->stats.x.aal5.service_stream,
2523			    lvcc->stats.x.aal5.service_rxcrc);
2524	}
2525	if (lvcc->tx.atmvcc != NULL)
2526		left += sprintf(&page[left], ",\n          tx_AAL=%d, "
2527		    "tx_buf_size=%Zu, tx_qos=%cBR, tx_backlogged=%c",
2528		    lvcc->tx.atmvcc->qos.aal == ATM_AAL5 ? 5 : 0,
2529		    lanai_buf_size(&lvcc->tx.buf),
2530		    lvcc->tx.atmvcc == lanai->cbrvcc ? 'C' : 'U',
2531		    vcc_is_backlogged(lvcc) ? 'Y' : 'N');
2532	page[left++] = '\n';
2533	page[left] = '\0';
2534    out:
2535	read_unlock(&vcc_sklist_lock);
2536	return left;
2537}
2538#endif /* CONFIG_PROC_FS */
2539
2540/* -------------------- HOOKS: */
2541
2542static const struct atmdev_ops ops = {
2543	.dev_close	= lanai_dev_close,
2544	.open		= lanai_open,
2545	.close		= lanai_close,
2546	.getsockopt	= NULL,
2547	.setsockopt	= NULL,
2548	.send		= lanai_send,
2549	.phy_put	= NULL,
2550	.phy_get	= NULL,
2551	.change_qos	= lanai_change_qos,
2552	.proc_read	= lanai_proc_read,
2553	.owner		= THIS_MODULE
2554};
2555
2556/* initialize one probed card */
2557static int lanai_init_one(struct pci_dev *pci,
2558			  const struct pci_device_id *ident)
2559{
2560	struct lanai_dev *lanai;
2561	struct atm_dev *atmdev;
2562	int result;
2563
2564	lanai = kmalloc(sizeof(*lanai), GFP_KERNEL);
2565	if (lanai == NULL) {
2566		printk(KERN_ERR DEV_LABEL
2567		       ": couldn't allocate dev_data structure!\n");
2568		return -ENOMEM;
2569	}
2570
2571	atmdev = atm_dev_register(DEV_LABEL, &pci->dev, &ops, -1, NULL);
2572	if (atmdev == NULL) {
2573		printk(KERN_ERR DEV_LABEL
2574		    ": couldn't register atm device!\n");
2575		kfree(lanai);
2576		return -EBUSY;
2577	}
2578
2579	atmdev->dev_data = lanai;
2580	lanai->pci = pci;
2581	lanai->type = (enum lanai_type) ident->device;
2582
2583	result = lanai_dev_open(atmdev);
2584	if (result != 0) {
2585		DPRINTK("lanai_start() failed, err=%d\n", -result);
2586		atm_dev_deregister(atmdev);
2587		kfree(lanai);
2588	}
2589	return result;
2590}
2591
2592static struct pci_device_id lanai_pci_tbl[] = {
2593	{ PCI_VDEVICE(EF, PCI_DEVICE_ID_EF_ATM_LANAI2) },
2594	{ PCI_VDEVICE(EF, PCI_DEVICE_ID_EF_ATM_LANAIHB) },
2595	{ 0, }	/* terminal entry */
2596};
2597MODULE_DEVICE_TABLE(pci, lanai_pci_tbl);
2598
2599static struct pci_driver lanai_driver = {
2600	.name     = DEV_LABEL,
2601	.id_table = lanai_pci_tbl,
2602	.probe    = lanai_init_one,
2603};
2604
2605module_pci_driver(lanai_driver);
2606
2607MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
2608MODULE_DESCRIPTION("Efficient Networks Speedstream 3010 driver");
2609MODULE_LICENSE("GPL");