1/*
2  Madge Ambassador ATM Adapter driver.
3  Copyright (C) 1995-1999  Madge Networks Ltd.
4
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19  The GNU GPL is contained in /usr/doc/copyright/GPL on a Debian
20  system and in the file COPYING in the Linux kernel source.
21*/
22
23/* * dedicated to the memory of Graham Gordon 1971-1998 * */
24
25#include <linux/module.h>
26#include <linux/types.h>
27#include <linux/pci.h>
28#include <linux/kernel.h>
29#include <linux/init.h>
30#include <linux/ioport.h>
31#include <linux/atmdev.h>
32#include <linux/delay.h>
33#include <linux/interrupt.h>
34#include <linux/poison.h>
35#include <linux/bitrev.h>
36
37#include <asm/atomic.h>
38#include <asm/io.h>
39#include <asm/byteorder.h>
40
41#include "ambassador.h"
42
43#define maintainer_string "Giuliano Procida at Madge Networks <gprocida@madge.com>"
44#define description_string "Madge ATM Ambassador driver"
45#define version_string "1.2.4"
46
47static inline void __init show_version (void) {
48  printk ("%s version %s\n", description_string, version_string);
49}
50
51
52/********** microcode **********/
53
54#ifdef AMB_NEW_MICROCODE
55#define UCODE(x) UCODE2(atmsar12.x)
56#else
57#define UCODE(x) UCODE2(atmsar11.x)
58#endif
59#define UCODE2(x) #x
60
61static u32 __devinitdata ucode_start =
62#include UCODE(start)
63;
64
65static region __devinitdata ucode_regions[] = {
66#include UCODE(regions)
67  { 0, 0 }
68};
69
70static u32 __devinitdata ucode_data[] = {
71#include UCODE(data)
72  0xdeadbeef
73};
74
75static void do_housekeeping (unsigned long arg);
76/********** globals **********/
77
78static unsigned short debug = 0;
79static unsigned int cmds = 8;
80static unsigned int txs = 32;
81static unsigned int rxs[NUM_RX_POOLS] = { 64, 64, 64, 64 };
82static unsigned int rxs_bs[NUM_RX_POOLS] = { 4080, 12240, 36720, 65535 };
83static unsigned int rx_lats = 7;
84static unsigned char pci_lat = 0;
85
86static const unsigned long onegigmask = -1 << 30;
87
88/********** access to adapter **********/
89
90static inline void wr_plain (const amb_dev * dev, size_t addr, u32 data) {
91  PRINTD (DBG_FLOW|DBG_REGS, "wr: %08zx <- %08x", addr, data);
92#ifdef AMB_MMIO
93  dev->membase[addr / sizeof(u32)] = data;
94#else
95  outl (data, dev->iobase + addr);
96#endif
97}
98
99static inline u32 rd_plain (const amb_dev * dev, size_t addr) {
100#ifdef AMB_MMIO
101  u32 data = dev->membase[addr / sizeof(u32)];
102#else
103  u32 data = inl (dev->iobase + addr);
104#endif
105  PRINTD (DBG_FLOW|DBG_REGS, "rd: %08zx -> %08x", addr, data);
106  return data;
107}
108
109static inline void wr_mem (const amb_dev * dev, size_t addr, u32 data) {
110  __be32 be = cpu_to_be32 (data);
111  PRINTD (DBG_FLOW|DBG_REGS, "wr: %08zx <- %08x b[%08x]", addr, data, be);
112#ifdef AMB_MMIO
113  dev->membase[addr / sizeof(u32)] = be;
114#else
115  outl (be, dev->iobase + addr);
116#endif
117}
118
119static inline u32 rd_mem (const amb_dev * dev, size_t addr) {
120#ifdef AMB_MMIO
121  __be32 be = dev->membase[addr / sizeof(u32)];
122#else
123  __be32 be = inl (dev->iobase + addr);
124#endif
125  u32 data = be32_to_cpu (be);
126  PRINTD (DBG_FLOW|DBG_REGS, "rd: %08zx -> %08x b[%08x]", addr, data, be);
127  return data;
128}
129
130/********** dump routines **********/
131
132static inline void dump_registers (const amb_dev * dev) {
133#ifdef DEBUG_AMBASSADOR
134  if (debug & DBG_REGS) {
135    size_t i;
136    PRINTD (DBG_REGS, "reading PLX control: ");
137    for (i = 0x00; i < 0x30; i += sizeof(u32))
138      rd_mem (dev, i);
139    PRINTD (DBG_REGS, "reading mailboxes: ");
140    for (i = 0x40; i < 0x60; i += sizeof(u32))
141      rd_mem (dev, i);
142    PRINTD (DBG_REGS, "reading doorb irqev irqen reset:");
143    for (i = 0x60; i < 0x70; i += sizeof(u32))
144      rd_mem (dev, i);
145  }
146#else
147  (void) dev;
148#endif
149  return;
150}
151
152static inline void dump_loader_block (volatile loader_block * lb) {
153#ifdef DEBUG_AMBASSADOR
154  unsigned int i;
155  PRINTDB (DBG_LOAD, "lb @ %p; res: %d, cmd: %d, pay:",
156	   lb, be32_to_cpu (lb->result), be32_to_cpu (lb->command));
157  for (i = 0; i < MAX_COMMAND_DATA; ++i)
158    PRINTDM (DBG_LOAD, " %08x", be32_to_cpu (lb->payload.data[i]));
159  PRINTDE (DBG_LOAD, ", vld: %08x", be32_to_cpu (lb->valid));
160#else
161  (void) lb;
162#endif
163  return;
164}
165
166static inline void dump_command (command * cmd) {
167#ifdef DEBUG_AMBASSADOR
168  unsigned int i;
169  PRINTDB (DBG_CMD, "cmd @ %p, req: %08x, pars:",
170	   cmd, /*be32_to_cpu*/ (cmd->request));
171  for (i = 0; i < 3; ++i)
172    PRINTDM (DBG_CMD, " %08x", /*be32_to_cpu*/ (cmd->args.par[i]));
173  PRINTDE (DBG_CMD, "");
174#else
175  (void) cmd;
176#endif
177  return;
178}
179
180static inline void dump_skb (char * prefix, unsigned int vc, struct sk_buff * skb) {
181#ifdef DEBUG_AMBASSADOR
182  unsigned int i;
183  unsigned char * data = skb->data;
184  PRINTDB (DBG_DATA, "%s(%u) ", prefix, vc);
185  for (i=0; i<skb->len && i < 256;i++)
186    PRINTDM (DBG_DATA, "%02x ", data[i]);
187  PRINTDE (DBG_DATA,"");
188#else
189  (void) prefix;
190  (void) vc;
191  (void) skb;
192#endif
193  return;
194}
195
196/********** check memory areas for use by Ambassador **********/
197
198/* see limitations under Hardware Features */
199
200static inline int check_area (void * start, size_t length) {
201  // assumes length > 0
202  const u32 fourmegmask = -1 << 22;
203  const u32 twofivesixmask = -1 << 8;
204  const u32 starthole = 0xE0000000;
205  u32 startaddress = virt_to_bus (start);
206  u32 lastaddress = startaddress+length-1;
207  if ((startaddress ^ lastaddress) & fourmegmask ||
208      (startaddress & twofivesixmask) == starthole) {
209    PRINTK (KERN_ERR, "check_area failure: [%x,%x] - mail maintainer!",
210	    startaddress, lastaddress);
211    return -1;
212  } else {
213    return 0;
214  }
215}
216
217/********** free an skb (as per ATM device driver documentation) **********/
218
219static inline void amb_kfree_skb (struct sk_buff * skb) {
220  if (ATM_SKB(skb)->vcc->pop) {
221    ATM_SKB(skb)->vcc->pop (ATM_SKB(skb)->vcc, skb);
222  } else {
223    dev_kfree_skb_any (skb);
224  }
225}
226
227/********** TX completion **********/
228
229static inline void tx_complete (amb_dev * dev, tx_out * tx) {
230  tx_simple * tx_descr = bus_to_virt (tx->handle);
231  struct sk_buff * skb = tx_descr->skb;
232
233  PRINTD (DBG_FLOW|DBG_TX, "tx_complete %p %p", dev, tx);
234
235  // VC layer stats
236  atomic_inc(&ATM_SKB(skb)->vcc->stats->tx);
237
238  // free the descriptor
239  kfree (tx_descr);
240
241  // free the skb
242  amb_kfree_skb (skb);
243
244  dev->stats.tx_ok++;
245  return;
246}
247
248/********** RX completion **********/
249
250static void rx_complete (amb_dev * dev, rx_out * rx) {
251  struct sk_buff * skb = bus_to_virt (rx->handle);
252  u16 vc = be16_to_cpu (rx->vc);
253  // unused: u16 lec_id = be16_to_cpu (rx->lec_id);
254  u16 status = be16_to_cpu (rx->status);
255  u16 rx_len = be16_to_cpu (rx->length);
256
257  PRINTD (DBG_FLOW|DBG_RX, "rx_complete %p %p (len=%hu)", dev, rx, rx_len);
258
259  if (!status) {
260    struct atm_vcc * atm_vcc = dev->rxer[vc];
261    dev->stats.rx.ok++;
262
263    if (atm_vcc) {
264
265      if (rx_len <= atm_vcc->qos.rxtp.max_sdu) {
266
267	if (atm_charge (atm_vcc, skb->truesize)) {
268
269	  // prepare socket buffer
270	  ATM_SKB(skb)->vcc = atm_vcc;
271	  skb_put (skb, rx_len);
272
273	  dump_skb ("<<<", vc, skb);
274
275	  // VC layer stats
276	  atomic_inc(&atm_vcc->stats->rx);
277	  __net_timestamp(skb);
278	  // end of our responsability
279	  atm_vcc->push (atm_vcc, skb);
280	  return;
281
282	} else {
283	  // someone fix this (message), please!
284	  PRINTD (DBG_INFO|DBG_RX, "dropped thanks to atm_charge (vc %hu, truesize %u)", vc, skb->truesize);
285	  // drop stats incremented in atm_charge
286	}
287
288      } else {
289      	PRINTK (KERN_INFO, "dropped over-size frame");
290	// should we count this?
291	atomic_inc(&atm_vcc->stats->rx_drop);
292      }
293
294    } else {
295      PRINTD (DBG_WARN|DBG_RX, "got frame but RX closed for channel %hu", vc);
296      // this is an adapter bug, only in new version of microcode
297    }
298
299  } else {
300    dev->stats.rx.error++;
301    if (status & CRC_ERR)
302      dev->stats.rx.badcrc++;
303    if (status & LEN_ERR)
304      dev->stats.rx.toolong++;
305    if (status & ABORT_ERR)
306      dev->stats.rx.aborted++;
307    if (status & UNUSED_ERR)
308      dev->stats.rx.unused++;
309  }
310
311  dev_kfree_skb_any (skb);
312  return;
313}
314
315/*
316
317  Note on queue handling.
318
319  Here "give" and "take" refer to queue entries and a queue (pair)
320  rather than frames to or from the host or adapter. Empty frame
321  buffers are given to the RX queue pair and returned unused or
322  containing RX frames. TX frames (well, pointers to TX fragment
323  lists) are given to the TX queue pair, completions are returned.
324
325*/
326
327/********** command queue **********/
328
329// I really don't like this, but it's the best I can do at the moment
330
331// also, the callers are responsible for byte order as the microcode
332// sometimes does 16-bit accesses (yuk yuk yuk)
333
334static int command_do (amb_dev * dev, command * cmd) {
335  amb_cq * cq = &dev->cq;
336  volatile amb_cq_ptrs * ptrs = &cq->ptrs;
337  command * my_slot;
338
339  PRINTD (DBG_FLOW|DBG_CMD, "command_do %p", dev);
340
341  if (test_bit (dead, &dev->flags))
342    return 0;
343
344  spin_lock (&cq->lock);
345
346  // if not full...
347  if (cq->pending < cq->maximum) {
348    // remember my slot for later
349    my_slot = ptrs->in;
350    PRINTD (DBG_CMD, "command in slot %p", my_slot);
351
352    dump_command (cmd);
353
354    // copy command in
355    *ptrs->in = *cmd;
356    cq->pending++;
357    ptrs->in = NEXTQ (ptrs->in, ptrs->start, ptrs->limit);
358
359    // mail the command
360    wr_mem (dev, offsetof(amb_mem, mb.adapter.cmd_address), virt_to_bus (ptrs->in));
361
362    if (cq->pending > cq->high)
363      cq->high = cq->pending;
364    spin_unlock (&cq->lock);
365
366    // these comments were in a while-loop before, msleep removes the loop
367    // go to sleep
368    // PRINTD (DBG_CMD, "wait: sleeping %lu for command", timeout);
369    msleep(cq->pending);
370
371    // wait for my slot to be reached (all waiters are here or above, until...)
372    while (ptrs->out != my_slot) {
373      PRINTD (DBG_CMD, "wait: command slot (now at %p)", ptrs->out);
374      set_current_state(TASK_UNINTERRUPTIBLE);
375      schedule();
376    }
377
378    // wait on my slot (... one gets to its slot, and... )
379    while (ptrs->out->request != cpu_to_be32 (SRB_COMPLETE)) {
380      PRINTD (DBG_CMD, "wait: command slot completion");
381      set_current_state(TASK_UNINTERRUPTIBLE);
382      schedule();
383    }
384
385    PRINTD (DBG_CMD, "command complete");
386    // update queue (... moves the queue along to the next slot)
387    spin_lock (&cq->lock);
388    cq->pending--;
389    // copy command out
390    *cmd = *ptrs->out;
391    ptrs->out = NEXTQ (ptrs->out, ptrs->start, ptrs->limit);
392    spin_unlock (&cq->lock);
393
394    return 0;
395  } else {
396    cq->filled++;
397    spin_unlock (&cq->lock);
398    return -EAGAIN;
399  }
400
401}
402
403/********** TX queue pair **********/
404
405static inline int tx_give (amb_dev * dev, tx_in * tx) {
406  amb_txq * txq = &dev->txq;
407  unsigned long flags;
408
409  PRINTD (DBG_FLOW|DBG_TX, "tx_give %p", dev);
410
411  if (test_bit (dead, &dev->flags))
412    return 0;
413
414  spin_lock_irqsave (&txq->lock, flags);
415
416  if (txq->pending < txq->maximum) {
417    PRINTD (DBG_TX, "TX in slot %p", txq->in.ptr);
418
419    *txq->in.ptr = *tx;
420    txq->pending++;
421    txq->in.ptr = NEXTQ (txq->in.ptr, txq->in.start, txq->in.limit);
422    // hand over the TX and ring the bell
423    wr_mem (dev, offsetof(amb_mem, mb.adapter.tx_address), virt_to_bus (txq->in.ptr));
424    wr_mem (dev, offsetof(amb_mem, doorbell), TX_FRAME);
425
426    if (txq->pending > txq->high)
427      txq->high = txq->pending;
428    spin_unlock_irqrestore (&txq->lock, flags);
429    return 0;
430  } else {
431    txq->filled++;
432    spin_unlock_irqrestore (&txq->lock, flags);
433    return -EAGAIN;
434  }
435}
436
437static inline int tx_take (amb_dev * dev) {
438  amb_txq * txq = &dev->txq;
439  unsigned long flags;
440
441  PRINTD (DBG_FLOW|DBG_TX, "tx_take %p", dev);
442
443  spin_lock_irqsave (&txq->lock, flags);
444
445  if (txq->pending && txq->out.ptr->handle) {
446    // deal with TX completion
447    tx_complete (dev, txq->out.ptr);
448    // mark unused again
449    txq->out.ptr->handle = 0;
450    // remove item
451    txq->pending--;
452    txq->out.ptr = NEXTQ (txq->out.ptr, txq->out.start, txq->out.limit);
453
454    spin_unlock_irqrestore (&txq->lock, flags);
455    return 0;
456  } else {
457
458    spin_unlock_irqrestore (&txq->lock, flags);
459    return -1;
460  }
461}
462
463/********** RX queue pairs **********/
464
465static inline int rx_give (amb_dev * dev, rx_in * rx, unsigned char pool) {
466  amb_rxq * rxq = &dev->rxq[pool];
467  unsigned long flags;
468
469  PRINTD (DBG_FLOW|DBG_RX, "rx_give %p[%hu]", dev, pool);
470
471  spin_lock_irqsave (&rxq->lock, flags);
472
473  if (rxq->pending < rxq->maximum) {
474    PRINTD (DBG_RX, "RX in slot %p", rxq->in.ptr);
475
476    *rxq->in.ptr = *rx;
477    rxq->pending++;
478    rxq->in.ptr = NEXTQ (rxq->in.ptr, rxq->in.start, rxq->in.limit);
479    // hand over the RX buffer
480    wr_mem (dev, offsetof(amb_mem, mb.adapter.rx_address[pool]), virt_to_bus (rxq->in.ptr));
481
482    spin_unlock_irqrestore (&rxq->lock, flags);
483    return 0;
484  } else {
485    spin_unlock_irqrestore (&rxq->lock, flags);
486    return -1;
487  }
488}
489
490static inline int rx_take (amb_dev * dev, unsigned char pool) {
491  amb_rxq * rxq = &dev->rxq[pool];
492  unsigned long flags;
493
494  PRINTD (DBG_FLOW|DBG_RX, "rx_take %p[%hu]", dev, pool);
495
496  spin_lock_irqsave (&rxq->lock, flags);
497
498  if (rxq->pending && (rxq->out.ptr->status || rxq->out.ptr->length)) {
499    // deal with RX completion
500    rx_complete (dev, rxq->out.ptr);
501    // mark unused again
502    rxq->out.ptr->status = 0;
503    rxq->out.ptr->length = 0;
504    // remove item
505    rxq->pending--;
506    rxq->out.ptr = NEXTQ (rxq->out.ptr, rxq->out.start, rxq->out.limit);
507
508    if (rxq->pending < rxq->low)
509      rxq->low = rxq->pending;
510    spin_unlock_irqrestore (&rxq->lock, flags);
511    return 0;
512  } else {
513    if (!rxq->pending && rxq->buffers_wanted)
514      rxq->emptied++;
515    spin_unlock_irqrestore (&rxq->lock, flags);
516    return -1;
517  }
518}
519
520/********** RX Pool handling **********/
521
522/* pre: buffers_wanted = 0, post: pending = 0 */
523static inline void drain_rx_pool (amb_dev * dev, unsigned char pool) {
524  amb_rxq * rxq = &dev->rxq[pool];
525
526  PRINTD (DBG_FLOW|DBG_POOL, "drain_rx_pool %p %hu", dev, pool);
527
528  if (test_bit (dead, &dev->flags))
529    return;
530
531  /* we are not quite like the fill pool routines as we cannot just
532     remove one buffer, we have to remove all of them, but we might as
533     well pretend... */
534  if (rxq->pending > rxq->buffers_wanted) {
535    command cmd;
536    cmd.request = cpu_to_be32 (SRB_FLUSH_BUFFER_Q);
537    cmd.args.flush.flags = cpu_to_be32 (pool << SRB_POOL_SHIFT);
538    while (command_do (dev, &cmd))
539      schedule();
540    /* the pool may also be emptied via the interrupt handler */
541    while (rxq->pending > rxq->buffers_wanted)
542      if (rx_take (dev, pool))
543	schedule();
544  }
545
546  return;
547}
548
549static void drain_rx_pools (amb_dev * dev) {
550  unsigned char pool;
551
552  PRINTD (DBG_FLOW|DBG_POOL, "drain_rx_pools %p", dev);
553
554  for (pool = 0; pool < NUM_RX_POOLS; ++pool)
555    drain_rx_pool (dev, pool);
556}
557
558static inline void fill_rx_pool (amb_dev * dev, unsigned char pool,
559                                 gfp_t priority)
560{
561  rx_in rx;
562  amb_rxq * rxq;
563
564  PRINTD (DBG_FLOW|DBG_POOL, "fill_rx_pool %p %hu %x", dev, pool, priority);
565
566  if (test_bit (dead, &dev->flags))
567    return;
568
569  rxq = &dev->rxq[pool];
570  while (rxq->pending < rxq->maximum && rxq->pending < rxq->buffers_wanted) {
571
572    struct sk_buff * skb = alloc_skb (rxq->buffer_size, priority);
573    if (!skb) {
574      PRINTD (DBG_SKB|DBG_POOL, "failed to allocate skb for RX pool %hu", pool);
575      return;
576    }
577    if (check_area (skb->data, skb->truesize)) {
578      dev_kfree_skb_any (skb);
579      return;
580    }
581    // cast needed as there is no %? for pointer differences
582    PRINTD (DBG_SKB, "allocated skb at %p, head %p, area %li",
583	    skb, skb->head, (long) (skb_end_pointer(skb) - skb->head));
584    rx.handle = virt_to_bus (skb);
585    rx.host_address = cpu_to_be32 (virt_to_bus (skb->data));
586    if (rx_give (dev, &rx, pool))
587      dev_kfree_skb_any (skb);
588
589  }
590
591  return;
592}
593
594// top up all RX pools (can also be called as a bottom half)
595static void fill_rx_pools (amb_dev * dev) {
596  unsigned char pool;
597
598  PRINTD (DBG_FLOW|DBG_POOL, "fill_rx_pools %p", dev);
599
600  for (pool = 0; pool < NUM_RX_POOLS; ++pool)
601    fill_rx_pool (dev, pool, GFP_ATOMIC);
602
603  return;
604}
605
606/********** enable host interrupts **********/
607
608static inline void interrupts_on (amb_dev * dev) {
609  wr_plain (dev, offsetof(amb_mem, interrupt_control),
610	    rd_plain (dev, offsetof(amb_mem, interrupt_control))
611	    | AMB_INTERRUPT_BITS);
612}
613
614/********** disable host interrupts **********/
615
616static inline void interrupts_off (amb_dev * dev) {
617  wr_plain (dev, offsetof(amb_mem, interrupt_control),
618	    rd_plain (dev, offsetof(amb_mem, interrupt_control))
619	    &~ AMB_INTERRUPT_BITS);
620}
621
622/********** interrupt handling **********/
623
624static irqreturn_t interrupt_handler(int irq, void *dev_id) {
625  amb_dev * dev = dev_id;
626
627  PRINTD (DBG_IRQ|DBG_FLOW, "interrupt_handler: %p", dev_id);
628
629  {
630    u32 interrupt = rd_plain (dev, offsetof(amb_mem, interrupt));
631
632    // for us or someone else sharing the same interrupt
633    if (!interrupt) {
634      PRINTD (DBG_IRQ, "irq not for me: %d", irq);
635      return IRQ_NONE;
636    }
637
638    // definitely for us
639    PRINTD (DBG_IRQ, "FYI: interrupt was %08x", interrupt);
640    wr_plain (dev, offsetof(amb_mem, interrupt), -1);
641  }
642
643  {
644    unsigned int irq_work = 0;
645    unsigned char pool;
646    for (pool = 0; pool < NUM_RX_POOLS; ++pool)
647      while (!rx_take (dev, pool))
648	++irq_work;
649    while (!tx_take (dev))
650      ++irq_work;
651
652    if (irq_work) {
653#ifdef FILL_RX_POOLS_IN_BH
654      schedule_work (&dev->bh);
655#else
656      fill_rx_pools (dev);
657#endif
658
659      PRINTD (DBG_IRQ, "work done: %u", irq_work);
660    } else {
661      PRINTD (DBG_IRQ|DBG_WARN, "no work done");
662    }
663  }
664
665  PRINTD (DBG_IRQ|DBG_FLOW, "interrupt_handler done: %p", dev_id);
666  return IRQ_HANDLED;
667}
668
669/********** make rate (not quite as much fun as Horizon) **********/
670
671static int make_rate (unsigned int rate, rounding r,
672		      u16 * bits, unsigned int * actual) {
673  unsigned char exp = -1; // hush gcc
674  unsigned int man = -1;  // hush gcc
675
676  PRINTD (DBG_FLOW|DBG_QOS, "make_rate %u", rate);
677
678  // rates in cells per second, ITU format (nasty 16-bit floating-point)
679  // given 5-bit e and 9-bit m:
680  // rate = EITHER (1+m/2^9)*2^e    OR 0
681  // bits = EITHER 1<<14 | e<<9 | m OR 0
682  // (bit 15 is "reserved", bit 14 "non-zero")
683  // smallest rate is 0 (special representation)
684  // largest rate is (1+511/512)*2^31 = 4290772992 (< 2^32-1)
685  // smallest non-zero rate is (1+0/512)*2^0 = 1 (> 0)
686  // simple algorithm:
687  // find position of top bit, this gives e
688  // remove top bit and shift (rounding if feeling clever) by 9-e
689
690  // ucode bug: please don't set bit 14! so 0 rate not representable
691
692  if (rate > 0xffc00000U) {
693    // larger than largest representable rate
694
695    if (r == round_up) {
696	return -EINVAL;
697    } else {
698      exp = 31;
699      man = 511;
700    }
701
702  } else if (rate) {
703    // representable rate
704
705    exp = 31;
706    man = rate;
707
708    // invariant: rate = man*2^(exp-31)
709    while (!(man & (1<<31))) {
710      exp = exp - 1;
711      man = man<<1;
712    }
713
714    // man has top bit set
715    // rate = (2^31+(man-2^31))*2^(exp-31)
716    // rate = (1+(man-2^31)/2^31)*2^exp
717    man = man<<1;
718    man &= 0xffffffffU; // a nop on 32-bit systems
719    // rate = (1+man/2^32)*2^exp
720
721    // exp is in the range 0 to 31, man is in the range 0 to 2^32-1
722    // time to lose significance... we want m in the range 0 to 2^9-1
723    // rounding presents a minor problem... we first decide which way
724    // we are rounding (based on given rounding direction and possibly
725    // the bits of the mantissa that are to be discarded).
726
727    switch (r) {
728      case round_down: {
729	// just truncate
730	man = man>>(32-9);
731	break;
732      }
733      case round_up: {
734	// check all bits that we are discarding
735	if (man & (~0U>>9)) {
736	  man = (man>>(32-9)) + 1;
737	  if (man == (1<<9)) {
738	    // no need to check for round up outside of range
739	    man = 0;
740	    exp += 1;
741	  }
742	} else {
743	  man = (man>>(32-9));
744	}
745	break;
746      }
747      case round_nearest: {
748	// check msb that we are discarding
749	if (man & (1<<(32-9-1))) {
750	  man = (man>>(32-9)) + 1;
751	  if (man == (1<<9)) {
752	    // no need to check for round up outside of range
753	    man = 0;
754	    exp += 1;
755	  }
756	} else {
757	  man = (man>>(32-9));
758	}
759	break;
760      }
761    }
762
763  } else {
764    // zero rate - not representable
765
766    if (r == round_down) {
767      return -EINVAL;
768    } else {
769      exp = 0;
770      man = 0;
771    }
772
773  }
774
775  PRINTD (DBG_QOS, "rate: man=%u, exp=%hu", man, exp);
776
777  if (bits)
778    *bits = /* (1<<14) | */ (exp<<9) | man;
779
780  if (actual)
781    *actual = (exp >= 9)
782      ? (1 << exp) + (man << (exp-9))
783      : (1 << exp) + ((man + (1<<(9-exp-1))) >> (9-exp));
784
785  return 0;
786}
787
788/********** Linux ATM Operations **********/
789
790// some are not yet implemented while others do not make sense for
791// this device
792
793/********** Open a VC **********/
794
795static int amb_open (struct atm_vcc * atm_vcc)
796{
797  int error;
798
799  struct atm_qos * qos;
800  struct atm_trafprm * txtp;
801  struct atm_trafprm * rxtp;
802  u16 tx_rate_bits;
803  u16 tx_vc_bits = -1; // hush gcc
804  u16 tx_frame_bits = -1; // hush gcc
805
806  amb_dev * dev = AMB_DEV(atm_vcc->dev);
807  amb_vcc * vcc;
808  unsigned char pool = -1; // hush gcc
809  short vpi = atm_vcc->vpi;
810  int vci = atm_vcc->vci;
811
812  PRINTD (DBG_FLOW|DBG_VCC, "amb_open %x %x", vpi, vci);
813
814#ifdef ATM_VPI_UNSPEC
815  // UNSPEC is deprecated, remove this code eventually
816  if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC) {
817    PRINTK (KERN_WARNING, "rejecting open with unspecified VPI/VCI (deprecated)");
818    return -EINVAL;
819  }
820#endif
821
822  if (!(0 <= vpi && vpi < (1<<NUM_VPI_BITS) &&
823	0 <= vci && vci < (1<<NUM_VCI_BITS))) {
824    PRINTD (DBG_WARN|DBG_VCC, "VPI/VCI out of range: %hd/%d", vpi, vci);
825    return -EINVAL;
826  }
827
828  qos = &atm_vcc->qos;
829
830  if (qos->aal != ATM_AAL5) {
831    PRINTD (DBG_QOS, "AAL not supported");
832    return -EINVAL;
833  }
834
835  // traffic parameters
836
837  PRINTD (DBG_QOS, "TX:");
838  txtp = &qos->txtp;
839  if (txtp->traffic_class != ATM_NONE) {
840    switch (txtp->traffic_class) {
841      case ATM_UBR: {
842	// we take "the PCR" as a rate-cap
843	int pcr = atm_pcr_goal (txtp);
844	if (!pcr) {
845	  // no rate cap
846	  tx_rate_bits = 0;
847	  tx_vc_bits = TX_UBR;
848	  tx_frame_bits = TX_FRAME_NOTCAP;
849	} else {
850	  rounding r;
851	  if (pcr < 0) {
852	    r = round_down;
853	    pcr = -pcr;
854	  } else {
855	    r = round_up;
856	  }
857	  error = make_rate (pcr, r, &tx_rate_bits, NULL);
858	  tx_vc_bits = TX_UBR_CAPPED;
859	  tx_frame_bits = TX_FRAME_CAPPED;
860	}
861	break;
862      }
863      default: {
864	// PRINTD (DBG_QOS, "request for non-UBR/ABR denied");
865	PRINTD (DBG_QOS, "request for non-UBR denied");
866	return -EINVAL;
867      }
868    }
869    PRINTD (DBG_QOS, "tx_rate_bits=%hx, tx_vc_bits=%hx",
870	    tx_rate_bits, tx_vc_bits);
871  }
872
873  PRINTD (DBG_QOS, "RX:");
874  rxtp = &qos->rxtp;
875  if (rxtp->traffic_class == ATM_NONE) {
876    // do nothing
877  } else {
878    // choose an RX pool (arranged in increasing size)
879    for (pool = 0; pool < NUM_RX_POOLS; ++pool)
880      if ((unsigned int) rxtp->max_sdu <= dev->rxq[pool].buffer_size) {
881	PRINTD (DBG_VCC|DBG_QOS|DBG_POOL, "chose pool %hu (max_sdu %u <= %u)",
882		pool, rxtp->max_sdu, dev->rxq[pool].buffer_size);
883	break;
884      }
885    if (pool == NUM_RX_POOLS) {
886      PRINTD (DBG_WARN|DBG_VCC|DBG_QOS|DBG_POOL,
887	      "no pool suitable for VC (RX max_sdu %d is too large)",
888	      rxtp->max_sdu);
889      return -EINVAL;
890    }
891
892    switch (rxtp->traffic_class) {
893      case ATM_UBR: {
894	break;
895      }
896      default: {
897	// PRINTD (DBG_QOS, "request for non-UBR/ABR denied");
898	PRINTD (DBG_QOS, "request for non-UBR denied");
899	return -EINVAL;
900      }
901    }
902  }
903
904  // get space for our vcc stuff
905  vcc = kmalloc (sizeof(amb_vcc), GFP_KERNEL);
906  if (!vcc) {
907    PRINTK (KERN_ERR, "out of memory!");
908    return -ENOMEM;
909  }
910  atm_vcc->dev_data = (void *) vcc;
911
912  // no failures beyond this point
913
914  // we are not really "immediately before allocating the connection
915  // identifier in hardware", but it will just have to do!
916  set_bit(ATM_VF_ADDR,&atm_vcc->flags);
917
918  if (txtp->traffic_class != ATM_NONE) {
919    command cmd;
920
921    vcc->tx_frame_bits = tx_frame_bits;
922
923    down (&dev->vcc_sf);
924    if (dev->rxer[vci]) {
925      // RXer on the channel already, just modify rate...
926      cmd.request = cpu_to_be32 (SRB_MODIFY_VC_RATE);
927      cmd.args.modify_rate.vc = cpu_to_be32 (vci);  // vpi 0
928      cmd.args.modify_rate.rate = cpu_to_be32 (tx_rate_bits << SRB_RATE_SHIFT);
929      while (command_do (dev, &cmd))
930	schedule();
931      // ... and TX flags, preserving the RX pool
932      cmd.request = cpu_to_be32 (SRB_MODIFY_VC_FLAGS);
933      cmd.args.modify_flags.vc = cpu_to_be32 (vci);  // vpi 0
934      cmd.args.modify_flags.flags = cpu_to_be32
935	( (AMB_VCC(dev->rxer[vci])->rx_info.pool << SRB_POOL_SHIFT)
936	  | (tx_vc_bits << SRB_FLAGS_SHIFT) );
937      while (command_do (dev, &cmd))
938	schedule();
939    } else {
940      // no RXer on the channel, just open (with pool zero)
941      cmd.request = cpu_to_be32 (SRB_OPEN_VC);
942      cmd.args.open.vc = cpu_to_be32 (vci);  // vpi 0
943      cmd.args.open.flags = cpu_to_be32 (tx_vc_bits << SRB_FLAGS_SHIFT);
944      cmd.args.open.rate = cpu_to_be32 (tx_rate_bits << SRB_RATE_SHIFT);
945      while (command_do (dev, &cmd))
946	schedule();
947    }
948    dev->txer[vci].tx_present = 1;
949    up (&dev->vcc_sf);
950  }
951
952  if (rxtp->traffic_class != ATM_NONE) {
953    command cmd;
954
955    vcc->rx_info.pool = pool;
956
957    down (&dev->vcc_sf);
958    /* grow RX buffer pool */
959    if (!dev->rxq[pool].buffers_wanted)
960      dev->rxq[pool].buffers_wanted = rx_lats;
961    dev->rxq[pool].buffers_wanted += 1;
962    fill_rx_pool (dev, pool, GFP_KERNEL);
963
964    if (dev->txer[vci].tx_present) {
965      // TXer on the channel already
966      // switch (from pool zero) to this pool, preserving the TX bits
967      cmd.request = cpu_to_be32 (SRB_MODIFY_VC_FLAGS);
968      cmd.args.modify_flags.vc = cpu_to_be32 (vci);  // vpi 0
969      cmd.args.modify_flags.flags = cpu_to_be32
970	( (pool << SRB_POOL_SHIFT)
971	  | (dev->txer[vci].tx_vc_bits << SRB_FLAGS_SHIFT) );
972    } else {
973      // no TXer on the channel, open the VC (with no rate info)
974      cmd.request = cpu_to_be32 (SRB_OPEN_VC);
975      cmd.args.open.vc = cpu_to_be32 (vci);  // vpi 0
976      cmd.args.open.flags = cpu_to_be32 (pool << SRB_POOL_SHIFT);
977      cmd.args.open.rate = cpu_to_be32 (0);
978    }
979    while (command_do (dev, &cmd))
980      schedule();
981    // this link allows RX frames through
982    dev->rxer[vci] = atm_vcc;
983    up (&dev->vcc_sf);
984  }
985
986  // indicate readiness
987  set_bit(ATM_VF_READY,&atm_vcc->flags);
988
989  return 0;
990}
991
992/********** Close a VC **********/
993
994static void amb_close (struct atm_vcc * atm_vcc) {
995  amb_dev * dev = AMB_DEV (atm_vcc->dev);
996  amb_vcc * vcc = AMB_VCC (atm_vcc);
997  u16 vci = atm_vcc->vci;
998
999  PRINTD (DBG_VCC|DBG_FLOW, "amb_close");
1000
1001  // indicate unreadiness
1002  clear_bit(ATM_VF_READY,&atm_vcc->flags);
1003
1004  // disable TXing
1005  if (atm_vcc->qos.txtp.traffic_class != ATM_NONE) {
1006    command cmd;
1007
1008    down (&dev->vcc_sf);
1009    if (dev->rxer[vci]) {
1010      cmd.request = cpu_to_be32 (SRB_MODIFY_VC_RATE);
1011      cmd.args.modify_rate.vc = cpu_to_be32 (vci);  // vpi 0
1012      cmd.args.modify_rate.rate = cpu_to_be32 (0);
1013    } else {
1014      // no RXer on the channel, close channel
1015      cmd.request = cpu_to_be32 (SRB_CLOSE_VC);
1016      cmd.args.close.vc = cpu_to_be32 (vci); // vpi 0
1017    }
1018    dev->txer[vci].tx_present = 0;
1019    while (command_do (dev, &cmd))
1020      schedule();
1021    up (&dev->vcc_sf);
1022  }
1023
1024  // disable RXing
1025  if (atm_vcc->qos.rxtp.traffic_class != ATM_NONE) {
1026    command cmd;
1027
1028    // this is (the?) one reason why we need the amb_vcc struct
1029    unsigned char pool = vcc->rx_info.pool;
1030
1031    down (&dev->vcc_sf);
1032    if (dev->txer[vci].tx_present) {
1033      cmd.request = cpu_to_be32 (SRB_MODIFY_VC_FLAGS);
1034      cmd.args.modify_flags.vc = cpu_to_be32 (vci);  // vpi 0
1035      cmd.args.modify_flags.flags = cpu_to_be32
1036	(dev->txer[vci].tx_vc_bits << SRB_FLAGS_SHIFT);
1037    } else {
1038      // no TXer on the channel, close the VC
1039      cmd.request = cpu_to_be32 (SRB_CLOSE_VC);
1040      cmd.args.close.vc = cpu_to_be32 (vci); // vpi 0
1041    }
1042    // forget the rxer - no more skbs will be pushed
1043    if (atm_vcc != dev->rxer[vci])
1044      PRINTK (KERN_ERR, "%s vcc=%p rxer[vci]=%p",
1045	      "arghhh! we're going to die!",
1046	      vcc, dev->rxer[vci]);
1047    dev->rxer[vci] = NULL;
1048    while (command_do (dev, &cmd))
1049      schedule();
1050
1051    /* shrink RX buffer pool */
1052    dev->rxq[pool].buffers_wanted -= 1;
1053    if (dev->rxq[pool].buffers_wanted == rx_lats) {
1054      dev->rxq[pool].buffers_wanted = 0;
1055      drain_rx_pool (dev, pool);
1056    }
1057    up (&dev->vcc_sf);
1058  }
1059
1060  // free our structure
1061  kfree (vcc);
1062
1063  // say the VPI/VCI is free again
1064  clear_bit(ATM_VF_ADDR,&atm_vcc->flags);
1065
1066  return;
1067}
1068
1069/********** Set socket options for a VC **********/
1070
1071// int amb_getsockopt (struct atm_vcc * atm_vcc, int level, int optname, void * optval, int optlen);
1072
1073/********** Set socket options for a VC **********/
1074
1075// int amb_setsockopt (struct atm_vcc * atm_vcc, int level, int optname, void * optval, int optlen);
1076
1077/********** Send **********/
1078
1079static int amb_send (struct atm_vcc * atm_vcc, struct sk_buff * skb) {
1080  amb_dev * dev = AMB_DEV(atm_vcc->dev);
1081  amb_vcc * vcc = AMB_VCC(atm_vcc);
1082  u16 vc = atm_vcc->vci;
1083  unsigned int tx_len = skb->len;
1084  unsigned char * tx_data = skb->data;
1085  tx_simple * tx_descr;
1086  tx_in tx;
1087
1088  if (test_bit (dead, &dev->flags))
1089    return -EIO;
1090
1091  PRINTD (DBG_FLOW|DBG_TX, "amb_send vc %x data %p len %u",
1092	  vc, tx_data, tx_len);
1093
1094  dump_skb (">>>", vc, skb);
1095
1096  if (!dev->txer[vc].tx_present) {
1097    PRINTK (KERN_ERR, "attempt to send on RX-only VC %x", vc);
1098    return -EBADFD;
1099  }
1100
1101  // this is a driver private field so we have to set it ourselves,
1102  // despite the fact that we are _required_ to use it to check for a
1103  // pop function
1104  ATM_SKB(skb)->vcc = atm_vcc;
1105
1106  if (skb->len > (size_t) atm_vcc->qos.txtp.max_sdu) {
1107    PRINTK (KERN_ERR, "sk_buff length greater than agreed max_sdu, dropping...");
1108    return -EIO;
1109  }
1110
1111  if (check_area (skb->data, skb->len)) {
1112    atomic_inc(&atm_vcc->stats->tx_err);
1113    return -ENOMEM; // ?
1114  }
1115
1116  // allocate memory for fragments
1117  tx_descr = kmalloc (sizeof(tx_simple), GFP_KERNEL);
1118  if (!tx_descr) {
1119    PRINTK (KERN_ERR, "could not allocate TX descriptor");
1120    return -ENOMEM;
1121  }
1122  if (check_area (tx_descr, sizeof(tx_simple))) {
1123    kfree (tx_descr);
1124    return -ENOMEM;
1125  }
1126  PRINTD (DBG_TX, "fragment list allocated at %p", tx_descr);
1127
1128  tx_descr->skb = skb;
1129
1130  tx_descr->tx_frag.bytes = cpu_to_be32 (tx_len);
1131  tx_descr->tx_frag.address = cpu_to_be32 (virt_to_bus (tx_data));
1132
1133  tx_descr->tx_frag_end.handle = virt_to_bus (tx_descr);
1134  tx_descr->tx_frag_end.vc = 0;
1135  tx_descr->tx_frag_end.next_descriptor_length = 0;
1136  tx_descr->tx_frag_end.next_descriptor = 0;
1137#ifdef AMB_NEW_MICROCODE
1138  tx_descr->tx_frag_end.cpcs_uu = 0;
1139  tx_descr->tx_frag_end.cpi = 0;
1140  tx_descr->tx_frag_end.pad = 0;
1141#endif
1142
1143  tx.vc = cpu_to_be16 (vcc->tx_frame_bits | vc);
1144  tx.tx_descr_length = cpu_to_be16 (sizeof(tx_frag)+sizeof(tx_frag_end));
1145  tx.tx_descr_addr = cpu_to_be32 (virt_to_bus (&tx_descr->tx_frag));
1146
1147  while (tx_give (dev, &tx))
1148    schedule();
1149  return 0;
1150}
1151
1152/********** Change QoS on a VC **********/
1153
1154// int amb_change_qos (struct atm_vcc * atm_vcc, struct atm_qos * qos, int flags);
1155
1156/********** Free RX Socket Buffer **********/
1157
1158
1159/********** Proc File Output **********/
1160
1161static int amb_proc_read (struct atm_dev * atm_dev, loff_t * pos, char * page) {
1162  amb_dev * dev = AMB_DEV (atm_dev);
1163  int left = *pos;
1164  unsigned char pool;
1165
1166  PRINTD (DBG_FLOW, "amb_proc_read");
1167
1168  /* more diagnostics here? */
1169
1170  if (!left--) {
1171    amb_stats * s = &dev->stats;
1172    return sprintf (page,
1173		    "frames: TX OK %lu, RX OK %lu, RX bad %lu "
1174		    "(CRC %lu, long %lu, aborted %lu, unused %lu).\n",
1175		    s->tx_ok, s->rx.ok, s->rx.error,
1176		    s->rx.badcrc, s->rx.toolong,
1177		    s->rx.aborted, s->rx.unused);
1178  }
1179
1180  if (!left--) {
1181    amb_cq * c = &dev->cq;
1182    return sprintf (page, "cmd queue [cur/hi/max]: %u/%u/%u. ",
1183		    c->pending, c->high, c->maximum);
1184  }
1185
1186  if (!left--) {
1187    amb_txq * t = &dev->txq;
1188    return sprintf (page, "TX queue [cur/max high full]: %u/%u %u %u.\n",
1189		    t->pending, t->maximum, t->high, t->filled);
1190  }
1191
1192  if (!left--) {
1193    unsigned int count = sprintf (page, "RX queues [cur/max/req low empty]:");
1194    for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
1195      amb_rxq * r = &dev->rxq[pool];
1196      count += sprintf (page+count, " %u/%u/%u %u %u",
1197			r->pending, r->maximum, r->buffers_wanted, r->low, r->emptied);
1198    }
1199    count += sprintf (page+count, ".\n");
1200    return count;
1201  }
1202
1203  if (!left--) {
1204    unsigned int count = sprintf (page, "RX buffer sizes:");
1205    for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
1206      amb_rxq * r = &dev->rxq[pool];
1207      count += sprintf (page+count, " %u", r->buffer_size);
1208    }
1209    count += sprintf (page+count, ".\n");
1210    return count;
1211  }
1212
1213
1214  return 0;
1215}
1216
1217/********** Operation Structure **********/
1218
1219static const struct atmdev_ops amb_ops = {
1220  .open         = amb_open,
1221  .close	= amb_close,
1222  .send         = amb_send,
1223  .proc_read	= amb_proc_read,
1224  .owner	= THIS_MODULE,
1225};
1226
1227/********** housekeeping **********/
1228static void do_housekeeping (unsigned long arg) {
1229  amb_dev * dev = (amb_dev *) arg;
1230
1231  // could collect device-specific (not driver/atm-linux) stats here
1232
1233  // last resort refill once every ten seconds
1234  fill_rx_pools (dev);
1235  mod_timer(&dev->housekeeping, jiffies + 10*HZ);
1236
1237  return;
1238}
1239
1240/********** creation of communication queues **********/
1241
1242static int __devinit create_queues (amb_dev * dev, unsigned int cmds,
1243				 unsigned int txs, unsigned int * rxs,
1244				 unsigned int * rx_buffer_sizes) {
1245  unsigned char pool;
1246  size_t total = 0;
1247  void * memory;
1248  void * limit;
1249
1250  PRINTD (DBG_FLOW, "create_queues %p", dev);
1251
1252  total += cmds * sizeof(command);
1253
1254  total += txs * (sizeof(tx_in) + sizeof(tx_out));
1255
1256  for (pool = 0; pool < NUM_RX_POOLS; ++pool)
1257    total += rxs[pool] * (sizeof(rx_in) + sizeof(rx_out));
1258
1259  memory = kmalloc (total, GFP_KERNEL);
1260  if (!memory) {
1261    PRINTK (KERN_ERR, "could not allocate queues");
1262    return -ENOMEM;
1263  }
1264  if (check_area (memory, total)) {
1265    PRINTK (KERN_ERR, "queues allocated in nasty area");
1266    kfree (memory);
1267    return -ENOMEM;
1268  }
1269
1270  limit = memory + total;
1271  PRINTD (DBG_INIT, "queues from %p to %p", memory, limit);
1272
1273  PRINTD (DBG_CMD, "command queue at %p", memory);
1274
1275  {
1276    command * cmd = memory;
1277    amb_cq * cq = &dev->cq;
1278
1279    cq->pending = 0;
1280    cq->high = 0;
1281    cq->maximum = cmds - 1;
1282
1283    cq->ptrs.start = cmd;
1284    cq->ptrs.in = cmd;
1285    cq->ptrs.out = cmd;
1286    cq->ptrs.limit = cmd + cmds;
1287
1288    memory = cq->ptrs.limit;
1289  }
1290
1291  PRINTD (DBG_TX, "TX queue pair at %p", memory);
1292
1293  {
1294    tx_in * in = memory;
1295    tx_out * out;
1296    amb_txq * txq = &dev->txq;
1297
1298    txq->pending = 0;
1299    txq->high = 0;
1300    txq->filled = 0;
1301    txq->maximum = txs - 1;
1302
1303    txq->in.start = in;
1304    txq->in.ptr = in;
1305    txq->in.limit = in + txs;
1306
1307    memory = txq->in.limit;
1308    out = memory;
1309
1310    txq->out.start = out;
1311    txq->out.ptr = out;
1312    txq->out.limit = out + txs;
1313
1314    memory = txq->out.limit;
1315  }
1316
1317  PRINTD (DBG_RX, "RX queue pairs at %p", memory);
1318
1319  for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
1320    rx_in * in = memory;
1321    rx_out * out;
1322    amb_rxq * rxq = &dev->rxq[pool];
1323
1324    rxq->buffer_size = rx_buffer_sizes[pool];
1325    rxq->buffers_wanted = 0;
1326
1327    rxq->pending = 0;
1328    rxq->low = rxs[pool] - 1;
1329    rxq->emptied = 0;
1330    rxq->maximum = rxs[pool] - 1;
1331
1332    rxq->in.start = in;
1333    rxq->in.ptr = in;
1334    rxq->in.limit = in + rxs[pool];
1335
1336    memory = rxq->in.limit;
1337    out = memory;
1338
1339    rxq->out.start = out;
1340    rxq->out.ptr = out;
1341    rxq->out.limit = out + rxs[pool];
1342
1343    memory = rxq->out.limit;
1344  }
1345
1346  if (memory == limit) {
1347    return 0;
1348  } else {
1349    PRINTK (KERN_ERR, "bad queue alloc %p != %p (tell maintainer)", memory, limit);
1350    kfree (limit - total);
1351    return -ENOMEM;
1352  }
1353
1354}
1355
1356/********** destruction of communication queues **********/
1357
1358static void destroy_queues (amb_dev * dev) {
1359  // all queues assumed empty
1360  void * memory = dev->cq.ptrs.start;
1361  // includes txq.in, txq.out, rxq[].in and rxq[].out
1362
1363  PRINTD (DBG_FLOW, "destroy_queues %p", dev);
1364
1365  PRINTD (DBG_INIT, "freeing queues at %p", memory);
1366  kfree (memory);
1367
1368  return;
1369}
1370
1371/********** basic loader commands and error handling **********/
1372// centisecond timeouts - guessing away here
1373static unsigned int command_timeouts [] = {
1374	[host_memory_test]     = 15,
1375	[read_adapter_memory]  = 2,
1376	[write_adapter_memory] = 2,
1377	[adapter_start]        = 50,
1378	[get_version_number]   = 10,
1379	[interrupt_host]       = 1,
1380	[flash_erase_sector]   = 1,
1381	[adap_download_block]  = 1,
1382	[adap_erase_flash]     = 1,
1383	[adap_run_in_iram]     = 1,
1384	[adap_end_download]    = 1
1385};
1386
1387
1388static unsigned int command_successes [] = {
1389	[host_memory_test]     = COMMAND_PASSED_TEST,
1390	[read_adapter_memory]  = COMMAND_READ_DATA_OK,
1391	[write_adapter_memory] = COMMAND_WRITE_DATA_OK,
1392	[adapter_start]        = COMMAND_COMPLETE,
1393	[get_version_number]   = COMMAND_COMPLETE,
1394	[interrupt_host]       = COMMAND_COMPLETE,
1395	[flash_erase_sector]   = COMMAND_COMPLETE,
1396	[adap_download_block]  = COMMAND_COMPLETE,
1397	[adap_erase_flash]     = COMMAND_COMPLETE,
1398	[adap_run_in_iram]     = COMMAND_COMPLETE,
1399	[adap_end_download]    = COMMAND_COMPLETE
1400};
1401
1402static  int decode_loader_result (loader_command cmd, u32 result)
1403{
1404	int res;
1405	const char *msg;
1406
1407	if (result == command_successes[cmd])
1408		return 0;
1409
1410	switch (result) {
1411		case BAD_COMMAND:
1412			res = -EINVAL;
1413			msg = "bad command";
1414			break;
1415		case COMMAND_IN_PROGRESS:
1416			res = -ETIMEDOUT;
1417			msg = "command in progress";
1418			break;
1419		case COMMAND_PASSED_TEST:
1420			res = 0;
1421			msg = "command passed test";
1422			break;
1423		case COMMAND_FAILED_TEST:
1424			res = -EIO;
1425			msg = "command failed test";
1426			break;
1427		case COMMAND_READ_DATA_OK:
1428			res = 0;
1429			msg = "command read data ok";
1430			break;
1431		case COMMAND_READ_BAD_ADDRESS:
1432			res = -EINVAL;
1433			msg = "command read bad address";
1434			break;
1435		case COMMAND_WRITE_DATA_OK:
1436			res = 0;
1437			msg = "command write data ok";
1438			break;
1439		case COMMAND_WRITE_BAD_ADDRESS:
1440			res = -EINVAL;
1441			msg = "command write bad address";
1442			break;
1443		case COMMAND_WRITE_FLASH_FAILURE:
1444			res = -EIO;
1445			msg = "command write flash failure";
1446			break;
1447		case COMMAND_COMPLETE:
1448			res = 0;
1449			msg = "command complete";
1450			break;
1451		case COMMAND_FLASH_ERASE_FAILURE:
1452			res = -EIO;
1453			msg = "command flash erase failure";
1454			break;
1455		case COMMAND_WRITE_BAD_DATA:
1456			res = -EINVAL;
1457			msg = "command write bad data";
1458			break;
1459		default:
1460			res = -EINVAL;
1461			msg = "unknown error";
1462			PRINTD (DBG_LOAD|DBG_ERR,
1463				"decode_loader_result got %d=%x !",
1464				result, result);
1465			break;
1466	}
1467
1468	PRINTK (KERN_ERR, "%s", msg);
1469	return res;
1470}
1471
1472static int __devinit do_loader_command (volatile loader_block * lb,
1473				     const amb_dev * dev, loader_command cmd) {
1474
1475  unsigned long timeout;
1476
1477  PRINTD (DBG_FLOW|DBG_LOAD, "do_loader_command");
1478
1479  /* do a command
1480
1481     Set the return value to zero, set the command type and set the
1482     valid entry to the right magic value. The payload is already
1483     correctly byte-ordered so we leave it alone. Hit the doorbell
1484     with the bus address of this structure.
1485
1486  */
1487
1488  lb->result = 0;
1489  lb->command = cpu_to_be32 (cmd);
1490  lb->valid = cpu_to_be32 (DMA_VALID);
1491  // dump_registers (dev);
1492  // dump_loader_block (lb);
1493  wr_mem (dev, offsetof(amb_mem, doorbell), virt_to_bus (lb) & ~onegigmask);
1494
1495  timeout = command_timeouts[cmd] * 10;
1496
1497  while (!lb->result || lb->result == cpu_to_be32 (COMMAND_IN_PROGRESS))
1498    if (timeout) {
1499      timeout = msleep_interruptible(timeout);
1500    } else {
1501      PRINTD (DBG_LOAD|DBG_ERR, "command %d timed out", cmd);
1502      dump_registers (dev);
1503      dump_loader_block (lb);
1504      return -ETIMEDOUT;
1505    }
1506
1507  if (cmd == adapter_start) {
1508    // wait for start command to acknowledge...
1509    timeout = 100;
1510    while (rd_plain (dev, offsetof(amb_mem, doorbell)))
1511      if (timeout) {
1512	timeout = msleep_interruptible(timeout);
1513      } else {
1514	PRINTD (DBG_LOAD|DBG_ERR, "start command did not clear doorbell, res=%08x",
1515		be32_to_cpu (lb->result));
1516	dump_registers (dev);
1517	return -ETIMEDOUT;
1518      }
1519    return 0;
1520  } else {
1521    return decode_loader_result (cmd, be32_to_cpu (lb->result));
1522  }
1523
1524}
1525
1526/* loader: determine loader version */
1527
1528static int __devinit get_loader_version (loader_block * lb,
1529				      const amb_dev * dev, u32 * version) {
1530  int res;
1531
1532  PRINTD (DBG_FLOW|DBG_LOAD, "get_loader_version");
1533
1534  res = do_loader_command (lb, dev, get_version_number);
1535  if (res)
1536    return res;
1537  if (version)
1538    *version = be32_to_cpu (lb->payload.version);
1539  return 0;
1540}
1541
1542/* loader: write memory data blocks */
1543
1544static int __devinit loader_write (loader_block * lb,
1545				const amb_dev * dev, const u32 * data,
1546				u32 address, unsigned int count) {
1547  unsigned int i;
1548  transfer_block * tb = &lb->payload.transfer;
1549
1550  PRINTD (DBG_FLOW|DBG_LOAD, "loader_write");
1551
1552  if (count > MAX_TRANSFER_DATA)
1553    return -EINVAL;
1554  tb->address = cpu_to_be32 (address);
1555  tb->count = cpu_to_be32 (count);
1556  for (i = 0; i < count; ++i)
1557    tb->data[i] = cpu_to_be32 (data[i]);
1558  return do_loader_command (lb, dev, write_adapter_memory);
1559}
1560
1561/* loader: verify memory data blocks */
1562
1563static int __devinit loader_verify (loader_block * lb,
1564				 const amb_dev * dev, const u32 * data,
1565				 u32 address, unsigned int count) {
1566  unsigned int i;
1567  transfer_block * tb = &lb->payload.transfer;
1568  int res;
1569
1570  PRINTD (DBG_FLOW|DBG_LOAD, "loader_verify");
1571
1572  if (count > MAX_TRANSFER_DATA)
1573    return -EINVAL;
1574  tb->address = cpu_to_be32 (address);
1575  tb->count = cpu_to_be32 (count);
1576  res = do_loader_command (lb, dev, read_adapter_memory);
1577  if (!res)
1578    for (i = 0; i < count; ++i)
1579      if (tb->data[i] != cpu_to_be32 (data[i])) {
1580	res = -EINVAL;
1581	break;
1582      }
1583  return res;
1584}
1585
1586/* loader: start microcode */
1587
1588static int __devinit loader_start (loader_block * lb,
1589				const amb_dev * dev, u32 address) {
1590  PRINTD (DBG_FLOW|DBG_LOAD, "loader_start");
1591
1592  lb->payload.start = cpu_to_be32 (address);
1593  return do_loader_command (lb, dev, adapter_start);
1594}
1595
1596/********** reset card **********/
1597
1598static inline void sf (const char * msg)
1599{
1600	PRINTK (KERN_ERR, "self-test failed: %s", msg);
1601}
1602
1603static int amb_reset (amb_dev * dev, int diags) {
1604  u32 word;
1605
1606  PRINTD (DBG_FLOW|DBG_LOAD, "amb_reset");
1607
1608  word = rd_plain (dev, offsetof(amb_mem, reset_control));
1609  // put card into reset state
1610  wr_plain (dev, offsetof(amb_mem, reset_control), word | AMB_RESET_BITS);
1611  // wait a short while
1612  udelay (10);
1613  // put card into known good state
1614  wr_plain (dev, offsetof(amb_mem, interrupt_control), AMB_DOORBELL_BITS);
1615  // clear all interrupts just in case
1616  wr_plain (dev, offsetof(amb_mem, interrupt), -1);
1617  // clear self-test done flag
1618  wr_plain (dev, offsetof(amb_mem, mb.loader.ready), 0);
1619  // take card out of reset state
1620  wr_plain (dev, offsetof(amb_mem, reset_control), word &~ AMB_RESET_BITS);
1621
1622  if (diags) {
1623    unsigned long timeout;
1624    // 4.2 second wait
1625    msleep(4200);
1626    // half second time-out
1627    timeout = 500;
1628    while (!rd_plain (dev, offsetof(amb_mem, mb.loader.ready)))
1629      if (timeout) {
1630	timeout = msleep_interruptible(timeout);
1631      } else {
1632	PRINTD (DBG_LOAD|DBG_ERR, "reset timed out");
1633	return -ETIMEDOUT;
1634      }
1635
1636    // get results of self-test
1637    word = rd_mem (dev, offsetof(amb_mem, mb.loader.result));
1638    if (word & SELF_TEST_FAILURE) {
1639      if (word & GPINT_TST_FAILURE)
1640	sf ("interrupt");
1641      if (word & SUNI_DATA_PATTERN_FAILURE)
1642	sf ("SUNI data pattern");
1643      if (word & SUNI_DATA_BITS_FAILURE)
1644	sf ("SUNI data bits");
1645      if (word & SUNI_UTOPIA_FAILURE)
1646	sf ("SUNI UTOPIA interface");
1647      if (word & SUNI_FIFO_FAILURE)
1648	sf ("SUNI cell buffer FIFO");
1649      if (word & SRAM_FAILURE)
1650	sf ("bad SRAM");
1651      // better return value?
1652      return -EIO;
1653    }
1654
1655  }
1656  return 0;
1657}
1658
1659/********** transfer and start the microcode **********/
1660
1661static int __devinit ucode_init (loader_block * lb, amb_dev * dev) {
1662  unsigned int i = 0;
1663  unsigned int total = 0;
1664  const u32 * pointer = ucode_data;
1665  u32 address;
1666  unsigned int count;
1667  int res;
1668
1669  PRINTD (DBG_FLOW|DBG_LOAD, "ucode_init");
1670
1671  while (address = ucode_regions[i].start,
1672	 count = ucode_regions[i].count) {
1673    PRINTD (DBG_LOAD, "starting region (%x, %u)", address, count);
1674    while (count) {
1675      unsigned int words;
1676      if (count <= MAX_TRANSFER_DATA)
1677	words = count;
1678      else
1679	words = MAX_TRANSFER_DATA;
1680      total += words;
1681      res = loader_write (lb, dev, pointer, address, words);
1682      if (res)
1683	return res;
1684      res = loader_verify (lb, dev, pointer, address, words);
1685      if (res)
1686	return res;
1687      count -= words;
1688      address += sizeof(u32) * words;
1689      pointer += words;
1690    }
1691    i += 1;
1692  }
1693  if (*pointer == ATM_POISON) {
1694    return loader_start (lb, dev, ucode_start);
1695  } else {
1696    // cast needed as there is no %? for pointer differnces
1697    PRINTD (DBG_LOAD|DBG_ERR,
1698	    "offset=%li, *pointer=%x, address=%x, total=%u",
1699	    (long) (pointer - ucode_data), *pointer, address, total);
1700    PRINTK (KERN_ERR, "incorrect microcode data");
1701    return -ENOMEM;
1702  }
1703}
1704
1705/********** give adapter parameters **********/
1706
1707static inline __be32 bus_addr(void * addr) {
1708    return cpu_to_be32 (virt_to_bus (addr));
1709}
1710
1711static int __devinit amb_talk (amb_dev * dev) {
1712  adap_talk_block a;
1713  unsigned char pool;
1714  unsigned long timeout;
1715
1716  PRINTD (DBG_FLOW, "amb_talk %p", dev);
1717
1718  a.command_start = bus_addr (dev->cq.ptrs.start);
1719  a.command_end   = bus_addr (dev->cq.ptrs.limit);
1720  a.tx_start      = bus_addr (dev->txq.in.start);
1721  a.tx_end        = bus_addr (dev->txq.in.limit);
1722  a.txcom_start   = bus_addr (dev->txq.out.start);
1723  a.txcom_end     = bus_addr (dev->txq.out.limit);
1724
1725  for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
1726    // the other "a" items are set up by the adapter
1727    a.rec_struct[pool].buffer_start = bus_addr (dev->rxq[pool].in.start);
1728    a.rec_struct[pool].buffer_end   = bus_addr (dev->rxq[pool].in.limit);
1729    a.rec_struct[pool].rx_start     = bus_addr (dev->rxq[pool].out.start);
1730    a.rec_struct[pool].rx_end       = bus_addr (dev->rxq[pool].out.limit);
1731    a.rec_struct[pool].buffer_size = cpu_to_be32 (dev->rxq[pool].buffer_size);
1732  }
1733
1734#ifdef AMB_NEW_MICROCODE
1735  // disable fast PLX prefetching
1736  a.init_flags = 0;
1737#endif
1738
1739  // pass the structure
1740  wr_mem (dev, offsetof(amb_mem, doorbell), virt_to_bus (&a));
1741
1742  // 2.2 second wait (must not touch doorbell during 2 second DMA test)
1743  msleep(2200);
1744  // give the adapter another half second?
1745  timeout = 500;
1746  while (rd_plain (dev, offsetof(amb_mem, doorbell)))
1747    if (timeout) {
1748      timeout = msleep_interruptible(timeout);
1749    } else {
1750      PRINTD (DBG_INIT|DBG_ERR, "adapter init timed out");
1751      return -ETIMEDOUT;
1752    }
1753
1754  return 0;
1755}
1756
1757// get microcode version
1758static void __devinit amb_ucode_version (amb_dev * dev) {
1759  u32 major;
1760  u32 minor;
1761  command cmd;
1762  cmd.request = cpu_to_be32 (SRB_GET_VERSION);
1763  while (command_do (dev, &cmd)) {
1764    set_current_state(TASK_UNINTERRUPTIBLE);
1765    schedule();
1766  }
1767  major = be32_to_cpu (cmd.args.version.major);
1768  minor = be32_to_cpu (cmd.args.version.minor);
1769  PRINTK (KERN_INFO, "microcode version is %u.%u", major, minor);
1770}
1771
1772// get end station address
1773static void __devinit amb_esi (amb_dev * dev, u8 * esi) {
1774  u32 lower4;
1775  u16 upper2;
1776  command cmd;
1777
1778  cmd.request = cpu_to_be32 (SRB_GET_BIA);
1779  while (command_do (dev, &cmd)) {
1780    set_current_state(TASK_UNINTERRUPTIBLE);
1781    schedule();
1782  }
1783  lower4 = be32_to_cpu (cmd.args.bia.lower4);
1784  upper2 = be32_to_cpu (cmd.args.bia.upper2);
1785  PRINTD (DBG_LOAD, "BIA: lower4: %08x, upper2 %04x", lower4, upper2);
1786
1787  if (esi) {
1788    unsigned int i;
1789
1790    PRINTDB (DBG_INIT, "ESI:");
1791    for (i = 0; i < ESI_LEN; ++i) {
1792      if (i < 4)
1793	  esi[i] = bitrev8(lower4>>(8*i));
1794      else
1795	  esi[i] = bitrev8(upper2>>(8*(i-4)));
1796      PRINTDM (DBG_INIT, " %02x", esi[i]);
1797    }
1798
1799    PRINTDE (DBG_INIT, "");
1800  }
1801
1802  return;
1803}
1804
1805static void fixup_plx_window (amb_dev *dev, loader_block *lb)
1806{
1807	// fix up the PLX-mapped window base address to match the block
1808	unsigned long blb;
1809	u32 mapreg;
1810	blb = virt_to_bus(lb);
1811	// the kernel stack had better not ever cross a 1Gb boundary!
1812	mapreg = rd_plain (dev, offsetof(amb_mem, stuff[10]));
1813	mapreg &= ~onegigmask;
1814	mapreg |= blb & onegigmask;
1815	wr_plain (dev, offsetof(amb_mem, stuff[10]), mapreg);
1816	return;
1817}
1818
1819static int __devinit amb_init (amb_dev * dev)
1820{
1821  loader_block lb;
1822
1823  u32 version;
1824
1825  if (amb_reset (dev, 1)) {
1826    PRINTK (KERN_ERR, "card reset failed!");
1827  } else {
1828    fixup_plx_window (dev, &lb);
1829
1830    if (get_loader_version (&lb, dev, &version)) {
1831      PRINTK (KERN_INFO, "failed to get loader version");
1832    } else {
1833      PRINTK (KERN_INFO, "loader version is %08x", version);
1834
1835      if (ucode_init (&lb, dev)) {
1836	PRINTK (KERN_ERR, "microcode failure");
1837      } else if (create_queues (dev, cmds, txs, rxs, rxs_bs)) {
1838	PRINTK (KERN_ERR, "failed to get memory for queues");
1839      } else {
1840
1841	if (amb_talk (dev)) {
1842	  PRINTK (KERN_ERR, "adapter did not accept queues");
1843	} else {
1844
1845	  amb_ucode_version (dev);
1846	  return 0;
1847
1848	} /* amb_talk */
1849
1850	destroy_queues (dev);
1851      } /* create_queues, ucode_init */
1852
1853      amb_reset (dev, 0);
1854    } /* get_loader_version */
1855
1856  } /* amb_reset */
1857
1858  return -EINVAL;
1859}
1860
1861static void setup_dev(amb_dev *dev, struct pci_dev *pci_dev)
1862{
1863      unsigned char pool;
1864      memset (dev, 0, sizeof(amb_dev));
1865
1866      // set up known dev items straight away
1867      dev->pci_dev = pci_dev;
1868      pci_set_drvdata(pci_dev, dev);
1869
1870      dev->iobase = pci_resource_start (pci_dev, 1);
1871      dev->irq = pci_dev->irq;
1872      dev->membase = bus_to_virt(pci_resource_start(pci_dev, 0));
1873
1874      // flags (currently only dead)
1875      dev->flags = 0;
1876
1877      // Allocate cell rates (fibre)
1878      // ATM_OC3_PCR = 1555200000/8/270*260/53 - 29/53
1879      // to be really pedantic, this should be ATM_OC3c_PCR
1880      dev->tx_avail = ATM_OC3_PCR;
1881      dev->rx_avail = ATM_OC3_PCR;
1882
1883#ifdef FILL_RX_POOLS_IN_BH
1884      // initialise bottom half
1885      INIT_WORK(&dev->bh, (void (*)(void *)) fill_rx_pools, dev);
1886#endif
1887
1888      // semaphore for txer/rxer modifications - we cannot use a
1889      // spinlock as the critical region needs to switch processes
1890      init_MUTEX (&dev->vcc_sf);
1891      // queue manipulation spinlocks; we want atomic reads and
1892      // writes to the queue descriptors (handles IRQ and SMP)
1893      // consider replacing "int pending" -> "atomic_t available"
1894      // => problem related to who gets to move queue pointers
1895      spin_lock_init (&dev->cq.lock);
1896      spin_lock_init (&dev->txq.lock);
1897      for (pool = 0; pool < NUM_RX_POOLS; ++pool)
1898	spin_lock_init (&dev->rxq[pool].lock);
1899}
1900
1901static void setup_pci_dev(struct pci_dev *pci_dev)
1902{
1903	unsigned char lat;
1904
1905	// enable bus master accesses
1906	pci_set_master(pci_dev);
1907
1908	// frobnicate latency (upwards, usually)
1909	pci_read_config_byte (pci_dev, PCI_LATENCY_TIMER, &lat);
1910
1911	if (!pci_lat)
1912		pci_lat = (lat < MIN_PCI_LATENCY) ? MIN_PCI_LATENCY : lat;
1913
1914	if (lat != pci_lat) {
1915		PRINTK (KERN_INFO, "Changing PCI latency timer from %hu to %hu",
1916			lat, pci_lat);
1917		pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, pci_lat);
1918	}
1919}
1920
1921static int __devinit amb_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_ent)
1922{
1923	amb_dev * dev;
1924	int err;
1925	unsigned int irq;
1926
1927	err = pci_enable_device(pci_dev);
1928	if (err < 0) {
1929		PRINTK (KERN_ERR, "skipped broken (PLX rev 2) card");
1930		goto out;
1931	}
1932
1933	// read resources from PCI configuration space
1934	irq = pci_dev->irq;
1935
1936	if (pci_dev->device == PCI_DEVICE_ID_MADGE_AMBASSADOR_BAD) {
1937		PRINTK (KERN_ERR, "skipped broken (PLX rev 2) card");
1938		err = -EINVAL;
1939		goto out_disable;
1940	}
1941
1942	PRINTD (DBG_INFO, "found Madge ATM adapter (amb) at"
1943		" IO %llx, IRQ %u, MEM %p",
1944		(unsigned long long)pci_resource_start(pci_dev, 1),
1945		irq, bus_to_virt(pci_resource_start(pci_dev, 0)));
1946
1947	// check IO region
1948	err = pci_request_region(pci_dev, 1, DEV_LABEL);
1949	if (err < 0) {
1950		PRINTK (KERN_ERR, "IO range already in use!");
1951		goto out_disable;
1952	}
1953
1954	dev = kmalloc (sizeof(amb_dev), GFP_KERNEL);
1955	if (!dev) {
1956		PRINTK (KERN_ERR, "out of memory!");
1957		err = -ENOMEM;
1958		goto out_release;
1959	}
1960
1961	setup_dev(dev, pci_dev);
1962
1963	err = amb_init(dev);
1964	if (err < 0) {
1965		PRINTK (KERN_ERR, "adapter initialisation failure");
1966		goto out_free;
1967	}
1968
1969	setup_pci_dev(pci_dev);
1970
1971	// grab (but share) IRQ and install handler
1972	err = request_irq(irq, interrupt_handler, IRQF_SHARED, DEV_LABEL, dev);
1973	if (err < 0) {
1974		PRINTK (KERN_ERR, "request IRQ failed!");
1975		goto out_reset;
1976	}
1977
1978	dev->atm_dev = atm_dev_register (DEV_LABEL, &amb_ops, -1, NULL);
1979	if (!dev->atm_dev) {
1980		PRINTD (DBG_ERR, "failed to register Madge ATM adapter");
1981		err = -EINVAL;
1982		goto out_free_irq;
1983	}
1984
1985	PRINTD (DBG_INFO, "registered Madge ATM adapter (no. %d) (%p) at %p",
1986		dev->atm_dev->number, dev, dev->atm_dev);
1987		dev->atm_dev->dev_data = (void *) dev;
1988
1989	// register our address
1990	amb_esi (dev, dev->atm_dev->esi);
1991
1992	// 0 bits for vpi, 10 bits for vci
1993	dev->atm_dev->ci_range.vpi_bits = NUM_VPI_BITS;
1994	dev->atm_dev->ci_range.vci_bits = NUM_VCI_BITS;
1995
1996	init_timer(&dev->housekeeping);
1997	dev->housekeeping.function = do_housekeeping;
1998	dev->housekeeping.data = (unsigned long) dev;
1999	mod_timer(&dev->housekeeping, jiffies);
2000
2001	// enable host interrupts
2002	interrupts_on (dev);
2003
2004out:
2005	return err;
2006
2007out_free_irq:
2008	free_irq(irq, dev);
2009out_reset:
2010	amb_reset(dev, 0);
2011out_free:
2012	kfree(dev);
2013out_release:
2014	pci_release_region(pci_dev, 1);
2015out_disable:
2016	pci_disable_device(pci_dev);
2017	goto out;
2018}
2019
2020
2021static void __devexit amb_remove_one(struct pci_dev *pci_dev)
2022{
2023	struct amb_dev *dev;
2024
2025	dev = pci_get_drvdata(pci_dev);
2026
2027	PRINTD(DBG_INFO|DBG_INIT, "closing %p (atm_dev = %p)", dev, dev->atm_dev);
2028	del_timer_sync(&dev->housekeeping);
2029	// the drain should not be necessary
2030	drain_rx_pools(dev);
2031	interrupts_off(dev);
2032	amb_reset(dev, 0);
2033	free_irq(dev->irq, dev);
2034	pci_disable_device(pci_dev);
2035	destroy_queues(dev);
2036	atm_dev_deregister(dev->atm_dev);
2037	kfree(dev);
2038	pci_release_region(pci_dev, 1);
2039}
2040
2041static void __init amb_check_args (void) {
2042  unsigned char pool;
2043  unsigned int max_rx_size;
2044
2045#ifdef DEBUG_AMBASSADOR
2046  PRINTK (KERN_NOTICE, "debug bitmap is %hx", debug &= DBG_MASK);
2047#else
2048  if (debug)
2049    PRINTK (KERN_NOTICE, "no debugging support");
2050#endif
2051
2052  if (cmds < MIN_QUEUE_SIZE)
2053    PRINTK (KERN_NOTICE, "cmds has been raised to %u",
2054	    cmds = MIN_QUEUE_SIZE);
2055
2056  if (txs < MIN_QUEUE_SIZE)
2057    PRINTK (KERN_NOTICE, "txs has been raised to %u",
2058	    txs = MIN_QUEUE_SIZE);
2059
2060  for (pool = 0; pool < NUM_RX_POOLS; ++pool)
2061    if (rxs[pool] < MIN_QUEUE_SIZE)
2062      PRINTK (KERN_NOTICE, "rxs[%hu] has been raised to %u",
2063	      pool, rxs[pool] = MIN_QUEUE_SIZE);
2064
2065  // buffers sizes should be greater than zero and strictly increasing
2066  max_rx_size = 0;
2067  for (pool = 0; pool < NUM_RX_POOLS; ++pool)
2068    if (rxs_bs[pool] <= max_rx_size)
2069      PRINTK (KERN_NOTICE, "useless pool (rxs_bs[%hu] = %u)",
2070	      pool, rxs_bs[pool]);
2071    else
2072      max_rx_size = rxs_bs[pool];
2073
2074  if (rx_lats < MIN_RX_BUFFERS)
2075    PRINTK (KERN_NOTICE, "rx_lats has been raised to %u",
2076	    rx_lats = MIN_RX_BUFFERS);
2077
2078  return;
2079}
2080
2081/********** module stuff **********/
2082
2083MODULE_AUTHOR(maintainer_string);
2084MODULE_DESCRIPTION(description_string);
2085MODULE_LICENSE("GPL");
2086module_param(debug,   ushort, 0644);
2087module_param(cmds,    uint, 0);
2088module_param(txs,     uint, 0);
2089module_param_array(rxs,     uint, NULL, 0);
2090module_param_array(rxs_bs,  uint, NULL, 0);
2091module_param(rx_lats, uint, 0);
2092module_param(pci_lat, byte, 0);
2093MODULE_PARM_DESC(debug,   "debug bitmap, see .h file");
2094MODULE_PARM_DESC(cmds,    "number of command queue entries");
2095MODULE_PARM_DESC(txs,     "number of TX queue entries");
2096MODULE_PARM_DESC(rxs,     "number of RX queue entries [" __MODULE_STRING(NUM_RX_POOLS) "]");
2097MODULE_PARM_DESC(rxs_bs,  "size of RX buffers [" __MODULE_STRING(NUM_RX_POOLS) "]");
2098MODULE_PARM_DESC(rx_lats, "number of extra buffers to cope with RX latencies");
2099MODULE_PARM_DESC(pci_lat, "PCI latency in bus cycles");
2100
2101/********** module entry **********/
2102
2103static struct pci_device_id amb_pci_tbl[] = {
2104	{ PCI_VENDOR_ID_MADGE, PCI_DEVICE_ID_MADGE_AMBASSADOR, PCI_ANY_ID, PCI_ANY_ID,
2105	  0, 0, 0 },
2106	{ PCI_VENDOR_ID_MADGE, PCI_DEVICE_ID_MADGE_AMBASSADOR_BAD, PCI_ANY_ID, PCI_ANY_ID,
2107	  0, 0, 0 },
2108	{ 0, }
2109};
2110
2111MODULE_DEVICE_TABLE(pci, amb_pci_tbl);
2112
2113static struct pci_driver amb_driver = {
2114	.name =		"amb",
2115	.probe =	amb_probe,
2116	.remove =	__devexit_p(amb_remove_one),
2117	.id_table =	amb_pci_tbl,
2118};
2119
2120static int __init amb_module_init (void)
2121{
2122  PRINTD (DBG_FLOW|DBG_INIT, "init_module");
2123
2124  // sanity check - cast needed as printk does not support %Zu
2125  if (sizeof(amb_mem) != 4*16 + 4*12) {
2126    PRINTK (KERN_ERR, "Fix amb_mem (is %lu words).",
2127	    (unsigned long) sizeof(amb_mem));
2128    return -ENOMEM;
2129  }
2130
2131  show_version();
2132
2133  amb_check_args();
2134
2135  // get the juice
2136  return pci_register_driver(&amb_driver);
2137}
2138
2139/********** module exit **********/
2140
2141static void __exit amb_module_exit (void)
2142{
2143  PRINTD (DBG_FLOW|DBG_INIT, "cleanup_module");
2144
2145  pci_unregister_driver(&amb_driver);
2146}
2147
2148module_init(amb_module_init);
2149module_exit(amb_module_exit);
2150