added mtd driver
[linux-2.4.git] / drivers / atm / horizon.c
1 /*
2   Madge Horizon 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 /*
24   IMPORTANT NOTE: Madge Networks no longer makes the adapters
25   supported by this driver and makes no commitment to maintain it.
26 */
27
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/mm.h>
31 #include <linux/pci.h>
32 #include <linux/errno.h>
33 #include <linux/atm.h>
34 #include <linux/atmdev.h>
35 #include <linux/sonet.h>
36 #include <linux/skbuff.h>
37 #include <linux/time.h>
38 #include <linux/delay.h>
39 #include <linux/uio.h>
40 #include <linux/init.h>
41 #include <linux/ioport.h>
42
43 #include <asm/system.h>
44 #include <asm/io.h>
45 #include <asm/atomic.h>
46 #include <asm/uaccess.h>
47 #include <asm/string.h>
48 #include <asm/byteorder.h>
49
50 #include "horizon.h"
51
52 #define maintainer_string "Giuliano Procida at Madge Networks <gprocida@madge.com>"
53 #define description_string "Madge ATM Horizon [Ultra] driver"
54 #define version_string "1.2.1"
55
56 static inline void __init show_version (void) {
57   printk ("%s version %s\n", description_string, version_string);
58 }
59
60 /*
61   
62   CREDITS
63   
64   Driver and documentation by:
65   
66   Chris Aston        Madge Networks
67   Giuliano Procida   Madge Networks
68   Simon Benham       Madge Networks
69   Simon Johnson      Madge Networks
70   Various Others     Madge Networks
71   
72   Some inspiration taken from other drivers by:
73   
74   Alexandru Cucos    UTBv
75   Kari Mettinen      University of Helsinki
76   Werner Almesberger EPFL LRC
77   
78   Theory of Operation
79   
80   I Hardware, detection, initialisation and shutdown.
81   
82   1. Supported Hardware
83   
84   This driver should handle all variants of the PCI Madge ATM adapters
85   with the Horizon chipset. These are all PCI cards supporting PIO, BM
86   DMA and a form of MMIO (registers only, not internal RAM).
87   
88   The driver is only known to work with SONET and UTP Horizon Ultra
89   cards at 155Mb/s. However, code is in place to deal with both the
90   original Horizon and 25Mb/s operation.
91   
92   There are two revisions of the Horizon ASIC: the original and the
93   Ultra. Details of hardware bugs are in section III.
94   
95   The ASIC version can be distinguished by chip markings but is NOT
96   indicated by the PCI revision (all adapters seem to have PCI rev 1).
97   
98   I believe that:
99   
100   Horizon       => Collage  25 PCI Adapter (UTP and STP)
101   Horizon Ultra => Collage 155 PCI Client (UTP or SONET)
102   Ambassador x  => Collage 155 PCI Server (completely different)
103   
104   Horizon (25Mb/s) is fitted with UTP and STP connectors. It seems to
105   have a Madge B154 plus glue logic serializer. I have also found a
106   really ancient version of this with slightly different glue. It
107   comes with the revision 0 (140-025-01) ASIC.
108   
109   Horizon Ultra (155Mb/s) is fitted with either a Pulse Medialink
110   output (UTP) or an HP HFBR 5205 output (SONET). It has either
111   Madge's SAMBA framer or a SUNI-lite device (early versions). It
112   comes with the revision 1 (140-027-01) ASIC.
113   
114   2. Detection
115   
116   All Horizon-based cards present with the same PCI Vendor and Device
117   IDs. The standard Linux 2.2 PCI API is used to locate any cards and
118   to enable bus-mastering (with appropriate latency).
119   
120   ATM_LAYER_STATUS in the control register distinguishes between the
121   two possible physical layers (25 and 155). It is not clear whether
122   the 155 cards can also operate at 25Mbps. We rely on the fact that a
123   card operates at 155 if and only if it has the newer Horizon Ultra
124   ASIC.
125   
126   For 155 cards the two possible framers are probed for and then set
127   up for loop-timing.
128   
129   3. Initialisation
130   
131   The card is reset and then put into a known state. The physical
132   layer is configured for normal operation at the appropriate speed;
133   in the case of the 155 cards, the framer is initialised with
134   line-based timing; the internal RAM is zeroed and the allocation of
135   buffers for RX and TX is made; the Burnt In Address is read and
136   copied to the ATM ESI; various policy settings for RX (VPI bits,
137   unknown VCs, oam cells) are made. Ideally all policy items should be
138   configurable at module load (if not actually on-demand), however,
139   only the vpi vs vci bit allocation can be specified at insmod.
140   
141   4. Shutdown
142   
143   This is in response to module_cleaup. No VCs are in use and the card
144   should be idle; it is reset.
145   
146   II Driver software (as it should be)
147   
148   0. Traffic Parameters
149   
150   The traffic classes (not an enumeration) are currently: ATM_NONE (no
151   traffic), ATM_UBR, ATM_CBR, ATM_VBR and ATM_ABR, ATM_ANYCLASS
152   (compatible with everything). Together with (perhaps only some of)
153   the following items they make up the traffic specification.
154   
155   struct atm_trafprm {
156     unsigned char traffic_class; traffic class (ATM_UBR, ...)
157     int           max_pcr;       maximum PCR in cells per second
158     int           pcr;           desired PCR in cells per second
159     int           min_pcr;       minimum PCR in cells per second
160     int           max_cdv;       maximum CDV in microseconds
161     int           max_sdu;       maximum SDU in bytes
162   };
163   
164   Note that these denote bandwidth available not bandwidth used; the
165   possibilities according to ATMF are:
166   
167   Real Time (cdv and max CDT given)
168   
169   CBR(pcr)             pcr bandwidth always available
170   rtVBR(pcr,scr,mbs)   scr bandwidth always available, upto pcr at mbs too
171   
172   Non Real Time
173   
174   nrtVBR(pcr,scr,mbs)  scr bandwidth always available, upto pcr at mbs too
175   UBR()
176   ABR(mcr,pcr)         mcr bandwidth always available, upto pcr (depending) too
177   
178   mbs is max burst size (bucket)
179   pcr and scr have associated cdvt values
180   mcr is like scr but has no cdtv
181   cdtv may differ at each hop
182   
183   Some of the above items are qos items (as opposed to traffic
184   parameters). We have nothing to do with qos. All except ABR can have
185   their traffic parameters converted to GCRA parameters. The GCRA may
186   be implemented as a (real-number) leaky bucket. The GCRA can be used
187   in complicated ways by switches and in simpler ways by end-stations.
188   It can be used both to filter incoming cells and shape out-going
189   cells.
190   
191   ATM Linux actually supports:
192   
193   ATM_NONE() (no traffic in this direction)
194   ATM_UBR(max_frame_size)
195   ATM_CBR(max/min_pcr, max_cdv, max_frame_size)
196   
197   0 or ATM_MAX_PCR are used to indicate maximum available PCR
198   
199   A traffic specification consists of the AAL type and separate
200   traffic specifications for either direction. In ATM Linux it is:
201   
202   struct atm_qos {
203   struct atm_trafprm txtp;
204   struct atm_trafprm rxtp;
205   unsigned char aal;
206   };
207   
208   AAL types are:
209   
210   ATM_NO_AAL    AAL not specified
211   ATM_AAL0      "raw" ATM cells
212   ATM_AAL1      AAL1 (CBR)
213   ATM_AAL2      AAL2 (VBR)
214   ATM_AAL34     AAL3/4 (data)
215   ATM_AAL5      AAL5 (data)
216   ATM_SAAL      signaling AAL
217   
218   The Horizon has support for AAL frame types: 0, 3/4 and 5. However,
219   it does not implement AAL 3/4 SAR and it has a different notion of
220   "raw cell" to ATM Linux's (48 bytes vs. 52 bytes) so neither are
221   supported by this driver.
222   
223   The Horizon has limited support for ABR (including UBR), VBR and
224   CBR. Each TX channel has a bucket (containing up to 31 cell units)
225   and two timers (PCR and SCR) associated with it that can be used to
226   govern cell emissions and host notification (in the case of ABR this
227   is presumably so that RM cells may be emitted at appropriate times).
228   The timers may either be disabled or may be set to any of 240 values
229   (determined by the clock crystal, a fixed (?) per-device divider, a
230   configurable divider and a configurable timer preload value).
231   
232   At the moment only UBR and CBR are supported by the driver. VBR will
233   be supported as soon as ATM for Linux supports it. ABR support is
234   very unlikely as RM cell handling is completely up to the driver.
235   
236   1. TX (TX channel setup and TX transfer)
237   
238   The TX half of the driver owns the TX Horizon registers. The TX
239   component in the IRQ handler is the BM completion handler. This can
240   only be entered when tx_busy is true (enforced by hardware). The
241   other TX component can only be entered when tx_busy is false
242   (enforced by driver). So TX is single-threaded.
243   
244   Apart from a minor optimisation to not re-select the last channel,
245   the TX send component works as follows:
246   
247   Atomic test and set tx_busy until we succeed; we should implement
248   some sort of timeout so that tx_busy will never be stuck at true.
249   
250   If no TX channel is set up for this VC we wait for an idle one (if
251   necessary) and set it up.
252   
253   At this point we have a TX channel ready for use. We wait for enough
254   buffers to become available then start a TX transmit (set the TX
255   descriptor, schedule transfer, exit).
256   
257   The IRQ component handles TX completion (stats, free buffer, tx_busy
258   unset, exit). We also re-schedule further transfers for the same
259   frame if needed.
260   
261   TX setup in more detail:
262   
263   TX open is a nop, the relevant information is held in the hrz_vcc
264   (vcc->dev_data) structure and is "cached" on the card.
265   
266   TX close gets the TX lock and clears the channel from the "cache".
267   
268   2. RX (Data Available and RX transfer)
269   
270   The RX half of the driver owns the RX registers. There are two RX
271   components in the IRQ handler: the data available handler deals with
272   fresh data that has arrived on the card, the BM completion handler
273   is very similar to the TX completion handler. The data available
274   handler grabs the rx_lock and it is only released once the data has
275   been discarded or completely transferred to the host. The BM
276   completion handler only runs when the lock is held; the data
277   available handler is locked out over the same period.
278   
279   Data available on the card triggers an interrupt. If the data is not
280   suitable for our existing RX channels or we cannot allocate a buffer
281   it is flushed. Otherwise an RX receive is scheduled. Multiple RX
282   transfers may be scheduled for the same frame.
283   
284   RX setup in more detail:
285   
286   RX open...
287   RX close...
288   
289   III Hardware Bugs
290   
291   0. Byte vs Word addressing of adapter RAM.
292   
293   A design feature; see the .h file (especially the memory map).
294   
295   1. Bus Master Data Transfers (original Horizon only, fixed in Ultra)
296   
297   The host must not start a transmit direction transfer at a
298   non-four-byte boundary in host memory. Instead the host should
299   perform a byte, or a two byte, or one byte followed by two byte
300   transfer in order to start the rest of the transfer on a four byte
301   boundary. RX is OK.
302   
303   Simultaneous transmit and receive direction bus master transfers are
304   not allowed.
305   
306   The simplest solution to these two is to always do PIO (never DMA)
307   in the TX direction on the original Horizon. More complicated
308   solutions are likely to hurt my brain.
309   
310   2. Loss of buffer on close VC
311   
312   When a VC is being closed, the buffer associated with it is not
313   returned to the pool. The host must store the reference to this
314   buffer and when opening a new VC then give it to that new VC.
315   
316   The host intervention currently consists of stacking such a buffer
317   pointer at VC close and checking the stack at VC open.
318   
319   3. Failure to close a VC
320   
321   If a VC is currently receiving a frame then closing the VC may fail
322   and the frame continues to be received.
323   
324   The solution is to make sure any received frames are flushed when
325   ready. This is currently done just before the solution to 2.
326   
327   4. PCI bus (original Horizon only, fixed in Ultra)
328   
329   Reading from the data port prior to initialisation will hang the PCI
330   bus. Just don't do that then! We don't.
331   
332   IV To Do List
333   
334   . Timer code may be broken.
335   
336   . Allow users to specify buffer allocation split for TX and RX.
337   
338   . Deal once and for all with buggy VC close.
339   
340   . Handle interrupted and/or non-blocking operations.
341   
342   . Change some macros to functions and move from .h to .c.
343   
344   . Try to limit the number of TX frames each VC may have queued, in
345     order to reduce the chances of TX buffer exhaustion.
346   
347   . Implement VBR (bucket and timers not understood) and ABR (need to
348     do RM cells manually); also no Linux support for either.
349   
350   . Implement QoS changes on open VCs (involves extracting parts of VC open
351     and close into separate functions and using them to make changes).
352   
353 */
354
355 /********** globals **********/
356
357 static hrz_dev * hrz_devs = NULL;
358 static struct timer_list housekeeping;
359
360 static unsigned short debug = 0;
361 static unsigned short vpi_bits = 0;
362 static int max_tx_size = 9000;
363 static int max_rx_size = 9000;
364 static unsigned char pci_lat = 0;
365
366 /********** access functions **********/
367
368 /* Read / Write Horizon registers */
369 static inline void wr_regl (const hrz_dev * dev, unsigned char reg, u32 data) {
370   outl (cpu_to_le32 (data), dev->iobase + reg);
371 }
372
373 static inline u32 rd_regl (const hrz_dev * dev, unsigned char reg) {
374   return le32_to_cpu (inl (dev->iobase + reg));
375 }
376
377 static inline void wr_regw (const hrz_dev * dev, unsigned char reg, u16 data) {
378   outw (cpu_to_le16 (data), dev->iobase + reg);
379 }
380
381 static inline u16 rd_regw (const hrz_dev * dev, unsigned char reg) {
382   return le16_to_cpu (inw (dev->iobase + reg));
383 }
384
385 static inline void wrs_regb (const hrz_dev * dev, unsigned char reg, void * addr, u32 len) {
386   outsb (dev->iobase + reg, addr, len);
387 }
388
389 static inline void rds_regb (const hrz_dev * dev, unsigned char reg, void * addr, u32 len) {
390   insb (dev->iobase + reg, addr, len);
391 }
392
393 /* Read / Write to a given address in Horizon buffer memory.
394    Interrupts must be disabled between the address register and data
395    port accesses as these must form an atomic operation. */
396 static inline void wr_mem (const hrz_dev * dev, HDW * addr, u32 data) {
397   // wr_regl (dev, MEM_WR_ADDR_REG_OFF, (u32) addr);
398   wr_regl (dev, MEM_WR_ADDR_REG_OFF, (addr - (HDW *) 0) * sizeof(HDW));
399   wr_regl (dev, MEMORY_PORT_OFF, data);
400 }
401
402 static inline u32 rd_mem (const hrz_dev * dev, HDW * addr) {
403   // wr_regl (dev, MEM_RD_ADDR_REG_OFF, (u32) addr);
404   wr_regl (dev, MEM_RD_ADDR_REG_OFF, (addr - (HDW *) 0) * sizeof(HDW));
405   return rd_regl (dev, MEMORY_PORT_OFF);
406 }
407
408 static inline void wr_framer (const hrz_dev * dev, u32 addr, u32 data) {
409   wr_regl (dev, MEM_WR_ADDR_REG_OFF, (u32) addr | 0x80000000);
410   wr_regl (dev, MEMORY_PORT_OFF, data);
411 }
412
413 static inline u32 rd_framer (const hrz_dev * dev, u32 addr) {
414   wr_regl (dev, MEM_RD_ADDR_REG_OFF, (u32) addr | 0x80000000);
415   return rd_regl (dev, MEMORY_PORT_OFF);
416 }
417
418 /********** specialised access functions **********/
419
420 /* RX */
421
422 static inline void FLUSH_RX_CHANNEL (hrz_dev * dev, u16 channel) {
423   wr_regw (dev, RX_CHANNEL_PORT_OFF, FLUSH_CHANNEL | channel);
424   return;
425 }
426
427 static inline void WAIT_FLUSH_RX_COMPLETE (hrz_dev * dev) {
428   while (rd_regw (dev, RX_CHANNEL_PORT_OFF) & FLUSH_CHANNEL)
429     ;
430   return;
431 }
432
433 static inline void SELECT_RX_CHANNEL (hrz_dev * dev, u16 channel) {
434   wr_regw (dev, RX_CHANNEL_PORT_OFF, channel);
435   return;
436 }
437
438 static inline void WAIT_UPDATE_COMPLETE (hrz_dev * dev) {
439   while (rd_regw (dev, RX_CHANNEL_PORT_OFF) & RX_CHANNEL_UPDATE_IN_PROGRESS)
440     ;
441   return;
442 }
443
444 /* TX */
445
446 static inline void SELECT_TX_CHANNEL (hrz_dev * dev, u16 tx_channel) {
447   wr_regl (dev, TX_CHANNEL_PORT_OFF, tx_channel);
448   return;
449 }
450
451 /* Update or query one configuration parameter of a particular channel. */
452
453 static inline void update_tx_channel_config (hrz_dev * dev, short chan, u8 mode, u16 value) {
454   wr_regw (dev, TX_CHANNEL_CONFIG_COMMAND_OFF,
455            chan * TX_CHANNEL_CONFIG_MULT | mode);
456     wr_regw (dev, TX_CHANNEL_CONFIG_DATA_OFF, value);
457     return;
458 }
459
460 static inline u16 query_tx_channel_config (hrz_dev * dev, short chan, u8 mode) {
461   wr_regw (dev, TX_CHANNEL_CONFIG_COMMAND_OFF,
462            chan * TX_CHANNEL_CONFIG_MULT | mode);
463     return rd_regw (dev, TX_CHANNEL_CONFIG_DATA_OFF);
464 }
465
466 /********** dump functions **********/
467
468 static inline void dump_skb (char * prefix, unsigned int vc, struct sk_buff * skb) {
469 #ifdef DEBUG_HORIZON
470   unsigned int i;
471   unsigned char * data = skb->data;
472   PRINTDB (DBG_DATA, "%s(%u) ", prefix, vc);
473   for (i=0; i<skb->len && i < 256;i++)
474     PRINTDM (DBG_DATA, "%02x ", data[i]);
475   PRINTDE (DBG_DATA,"");
476 #else
477   (void) prefix;
478   (void) vc;
479   (void) skb;
480 #endif
481   return;
482 }
483
484 #if 0 /* unused and in conflict with <asm-ppc/system.h> */
485 static inline void dump_regs (hrz_dev * dev) {
486 #ifdef DEBUG_HORIZON
487   PRINTD (DBG_REGS, "CONTROL 0: %#x", rd_regl (dev, CONTROL_0_REG));
488   PRINTD (DBG_REGS, "RX CONFIG: %#x", rd_regw (dev, RX_CONFIG_OFF));
489   PRINTD (DBG_REGS, "TX CONFIG: %#x", rd_regw (dev, TX_CONFIG_OFF));
490   PRINTD (DBG_REGS, "TX STATUS: %#x", rd_regw (dev, TX_STATUS_OFF));
491   PRINTD (DBG_REGS, "IRQ ENBLE: %#x", rd_regl (dev, INT_ENABLE_REG_OFF));
492   PRINTD (DBG_REGS, "IRQ SORCE: %#x", rd_regl (dev, INT_SOURCE_REG_OFF));
493 #else
494   (void) dev;
495 #endif
496   return;
497 }
498 #endif
499
500 static inline void dump_framer (hrz_dev * dev) {
501 #ifdef DEBUG_HORIZON
502   unsigned int i;
503   PRINTDB (DBG_REGS, "framer registers:");
504   for (i = 0; i < 0x10; ++i)
505     PRINTDM (DBG_REGS, " %02x", rd_framer (dev, i));
506   PRINTDE (DBG_REGS,"");
507 #else
508   (void) dev;
509 #endif
510   return;
511 }
512
513 /********** VPI/VCI <-> (RX) channel conversions **********/
514
515 /* RX channels are 10 bit integers, these fns are quite paranoid */
516
517 static inline int channel_to_vpivci (const u16 channel, short * vpi, int * vci) {
518   unsigned short vci_bits = 10 - vpi_bits;
519   if ((channel & RX_CHANNEL_MASK) == channel) {
520     *vci = channel & ((~0)<<vci_bits);
521     *vpi = channel >> vci_bits;
522     return channel ? 0 : -EINVAL;
523   }
524   return -EINVAL;
525 }
526
527 static inline int vpivci_to_channel (u16 * channel, const short vpi, const int vci) {
528   unsigned short vci_bits = 10 - vpi_bits;
529   if (0 <= vpi && vpi < 1<<vpi_bits && 0 <= vci && vci < 1<<vci_bits) {
530     *channel = vpi<<vci_bits | vci;
531     return *channel ? 0 : -EINVAL;
532   }
533   return -EINVAL;
534 }
535
536 /********** decode RX queue entries **********/
537
538 static inline u16 rx_q_entry_to_length (u32 x) {
539   return x & RX_Q_ENTRY_LENGTH_MASK;
540 }
541
542 static inline u16 rx_q_entry_to_rx_channel (u32 x) {
543   return (x>>RX_Q_ENTRY_CHANNEL_SHIFT) & RX_CHANNEL_MASK;
544 }
545
546 /* Cell Transmit Rate Values
547  *
548  * the cell transmit rate (cells per sec) can be set to a variety of
549  * different values by specifying two parameters: a timer preload from
550  * 1 to 16 (stored as 0 to 15) and a clock divider (2 to the power of
551  * an exponent from 0 to 14; the special value 15 disables the timer).
552  *
553  * cellrate = baserate / (preload * 2^divider)
554  *
555  * The maximum cell rate that can be specified is therefore just the
556  * base rate. Halving the preload is equivalent to adding 1 to the
557  * divider and so values 1 to 8 of the preload are redundant except
558  * in the case of a maximal divider (14).
559  *
560  * Given a desired cell rate, an algorithm to determine the preload
561  * and divider is:
562  * 
563  * a) x = baserate / cellrate, want p * 2^d = x (as far as possible)
564  * b) if x > 16 * 2^14 then set p = 16, d = 14 (min rate), done
565  *    if x <= 16 then set p = x, d = 0 (high rates), done
566  * c) now have 16 < x <= 2^18, or 1 < x/16 <= 2^14 and we want to
567  *    know n such that 2^(n-1) < x/16 <= 2^n, so slide a bit until
568  *    we find the range (n will be between 1 and 14), set d = n
569  * d) Also have 8 < x/2^n <= 16, so set p nearest x/2^n
570  *
571  * The algorithm used below is a minor variant of the above.
572  *
573  * The base rate is derived from the oscillator frequency (Hz) using a
574  * fixed divider:
575  *
576  * baserate = freq / 32 in the case of some Unknown Card
577  * baserate = freq / 8  in the case of the Horizon        25
578  * baserate = freq / 8  in the case of the Horizon Ultra 155
579  *
580  * The Horizon cards have oscillators and base rates as follows:
581  *
582  * Card               Oscillator  Base Rate
583  * Unknown Card       33 MHz      1.03125 MHz (33 MHz = PCI freq)
584  * Horizon        25  32 MHz      4       MHz
585  * Horizon Ultra 155  40 MHz      5       MHz
586  *
587  * The following defines give the base rates in Hz. These were
588  * previously a factor of 100 larger, no doubt someone was using
589  * cps*100.
590  */
591
592 #define BR_UKN 1031250l
593 #define BR_HRZ 4000000l
594 #define BR_ULT 5000000l
595
596 // d is an exponent
597 #define CR_MIND 0
598 #define CR_MAXD 14
599
600 // p ranges from 1 to a power of 2
601 #define CR_MAXPEXP 4
602
603 static int make_rate (const hrz_dev * dev, u32 c, rounding r,
604                       u16 * bits, unsigned int * actual) {
605   
606   // note: rounding the rate down means rounding 'p' up
607   
608   const unsigned long br = test_bit (ultra, (hrz_flags *) &dev->flags) ?
609     BR_ULT : BR_HRZ;
610   
611   u32 div = CR_MIND;
612   u32 pre;
613   
614   // local fn to build the timer bits
615   int set_cr (void) {
616     // paranoia
617     if (div > CR_MAXD || (!pre) || pre > 1<<CR_MAXPEXP) {
618       PRINTD (DBG_QOS, "set_cr internal failure: d=%u p=%u",
619               div, pre);
620       return -EINVAL;
621     } else {
622       if (bits)
623         *bits = (div<<CLOCK_SELECT_SHIFT) | (pre-1);
624       if (actual) {
625         *actual = (br + (pre<<div) - 1) / (pre<<div);
626         PRINTD (DBG_QOS, "actual rate: %u", *actual);
627       }
628       return 0;
629     }
630   }
631   
632   // br_exp and br_man are used to avoid overflowing (c*maxp*2^d) in
633   // the tests below. We could think harder about exact possibilities
634   // of failure...
635   
636   unsigned long br_man = br;
637   unsigned int br_exp = 0;
638   
639   PRINTD (DBG_QOS|DBG_FLOW, "make_rate b=%lu, c=%u, %s", br, c,
640           (r == round_up) ? "up" : (r == round_down) ? "down" : "nearest");
641   
642   // avoid div by zero
643   if (!c) {
644     PRINTD (DBG_QOS|DBG_ERR, "zero rate is not allowed!");
645     return -EINVAL;
646   }
647   
648   while (br_exp < CR_MAXPEXP + CR_MIND && (br_man % 2 == 0)) {
649     br_man = br_man >> 1;
650     ++br_exp;
651   }
652   // (br >>br_exp) <<br_exp == br and
653   // br_exp <= CR_MAXPEXP+CR_MIND
654   
655   if (br_man <= (c << (CR_MAXPEXP+CR_MIND-br_exp))) {
656     // Equivalent to: B <= (c << (MAXPEXP+MIND))
657     // take care of rounding
658     switch (r) {
659       case round_down:
660         pre = (br+(c<<div)-1)/(c<<div);
661         // but p must be non-zero
662         if (!pre)
663           pre = 1;
664         break;
665       case round_nearest:
666         pre = (br+(c<<div)/2)/(c<<div);
667         // but p must be non-zero
668         if (!pre)
669           pre = 1;
670         break;
671       case round_up:
672         pre = br/(c<<div);
673         // but p must be non-zero
674         if (!pre)
675           return -EINVAL;
676         break;
677     }
678     PRINTD (DBG_QOS, "A: p=%u, d=%u", pre, div);
679     return set_cr ();
680   }
681   
682   // at this point we have
683   // d == MIND and (c << (MAXPEXP+MIND)) < B
684   while (div < CR_MAXD) {
685     div++;
686     if (br_man <= (c << (CR_MAXPEXP+div-br_exp))) {
687       // Equivalent to: B <= (c << (MAXPEXP+d))
688       // c << (MAXPEXP+d-1) < B <= c << (MAXPEXP+d)
689       // 1 << (MAXPEXP-1) < B/2^d/c <= 1 << MAXPEXP
690       // MAXP/2 < B/c2^d <= MAXP
691       // take care of rounding
692       switch (r) {
693         case round_down:
694           pre = (br+(c<<div)-1)/(c<<div);
695           break;
696         case round_nearest:
697           pre = (br+(c<<div)/2)/(c<<div);
698           break;
699         case round_up:
700           pre = br/(c<<div);
701           break;
702       }
703       PRINTD (DBG_QOS, "B: p=%u, d=%u", pre, div);
704       return set_cr ();
705     }
706   }
707   // at this point we have
708   // d == MAXD and (c << (MAXPEXP+MAXD)) < B
709   // but we cannot go any higher
710   // take care of rounding
711   switch (r) {
712     case round_down:
713       return -EINVAL;
714       break;
715     case round_nearest:
716       break;
717     case round_up:
718       break;
719   }
720   pre = 1 << CR_MAXPEXP;
721   PRINTD (DBG_QOS, "C: p=%u, d=%u", pre, div);
722   return set_cr ();
723 }
724
725 static int make_rate_with_tolerance (const hrz_dev * dev, u32 c, rounding r, unsigned int tol,
726                                      u16 * bit_pattern, unsigned int * actual) {
727   unsigned int my_actual;
728   
729   PRINTD (DBG_QOS|DBG_FLOW, "make_rate_with_tolerance c=%u, %s, tol=%u",
730           c, (r == round_up) ? "up" : (r == round_down) ? "down" : "nearest", tol);
731   
732   if (!actual)
733     // actual rate is not returned
734     actual = &my_actual;
735   
736   if (make_rate (dev, c, round_nearest, bit_pattern, actual))
737     // should never happen as round_nearest always succeeds
738     return -1;
739   
740   if (c - tol <= *actual && *actual <= c + tol)
741     // within tolerance
742     return 0;
743   else
744     // intolerant, try rounding instead
745     return make_rate (dev, c, r, bit_pattern, actual);
746 }
747
748 /********** Listen on a VC **********/
749
750 static int hrz_open_rx (hrz_dev * dev, u16 channel) {
751   // is there any guarantee that we don't get two simulataneous
752   // identical calls of this function from different processes? yes
753   // rate_lock
754   unsigned long flags;
755   u32 channel_type; // u16?
756   
757   u16 buf_ptr = RX_CHANNEL_IDLE;
758   
759   rx_ch_desc * rx_desc = &memmap->rx_descs[channel];
760   
761   PRINTD (DBG_FLOW, "hrz_open_rx %x", channel);
762   
763   spin_lock_irqsave (&dev->mem_lock, flags);
764   channel_type = rd_mem (dev, &rx_desc->wr_buf_type) & BUFFER_PTR_MASK;
765   spin_unlock_irqrestore (&dev->mem_lock, flags);
766   
767   // very serious error, should never occur
768   if (channel_type != RX_CHANNEL_DISABLED) {
769     PRINTD (DBG_ERR|DBG_VCC, "RX channel for VC already open");
770     return -EBUSY; // clean up?
771   }
772   
773   // Give back spare buffer
774   if (dev->noof_spare_buffers) {
775     buf_ptr = dev->spare_buffers[--dev->noof_spare_buffers];
776     PRINTD (DBG_VCC, "using a spare buffer: %u", buf_ptr);
777     // should never occur
778     if (buf_ptr == RX_CHANNEL_DISABLED || buf_ptr == RX_CHANNEL_IDLE) {
779       // but easy to recover from
780       PRINTD (DBG_ERR|DBG_VCC, "bad spare buffer pointer, using IDLE");
781       buf_ptr = RX_CHANNEL_IDLE;
782     }
783   } else {
784     PRINTD (DBG_VCC, "using IDLE buffer pointer");
785   }
786   
787   // Channel is currently disabled so change its status to idle
788   
789   // do we really need to save the flags again?
790   spin_lock_irqsave (&dev->mem_lock, flags);
791   
792   wr_mem (dev, &rx_desc->wr_buf_type,
793           buf_ptr | CHANNEL_TYPE_AAL5 | FIRST_CELL_OF_AAL5_FRAME);
794   if (buf_ptr != RX_CHANNEL_IDLE)
795     wr_mem (dev, &rx_desc->rd_buf_type, buf_ptr);
796   
797   spin_unlock_irqrestore (&dev->mem_lock, flags);
798   
799   // rxer->rate = make_rate (qos->peak_cells);
800   
801   PRINTD (DBG_FLOW, "hrz_open_rx ok");
802   
803   return 0;
804 }
805
806 #if 0
807 /********** change vc rate for a given vc **********/
808
809 static void hrz_change_vc_qos (ATM_RXER * rxer, MAAL_QOS * qos) {
810   rxer->rate = make_rate (qos->peak_cells);
811 }
812 #endif
813
814 /********** free an skb (as per ATM device driver documentation) **********/
815
816 static inline void hrz_kfree_skb (struct sk_buff * skb) {
817   if (ATM_SKB(skb)->vcc->pop) {
818     ATM_SKB(skb)->vcc->pop (ATM_SKB(skb)->vcc, skb);
819   } else {
820     dev_kfree_skb_any (skb);
821   }
822 }
823
824 /********** cancel listen on a VC **********/
825
826 static void hrz_close_rx (hrz_dev * dev, u16 vc) {
827   unsigned long flags;
828   
829   u32 value;
830   
831   u32 r1, r2;
832   
833   rx_ch_desc * rx_desc = &memmap->rx_descs[vc];
834   
835   int was_idle = 0;
836   
837   spin_lock_irqsave (&dev->mem_lock, flags);
838   value = rd_mem (dev, &rx_desc->wr_buf_type) & BUFFER_PTR_MASK;
839   spin_unlock_irqrestore (&dev->mem_lock, flags);
840   
841   if (value == RX_CHANNEL_DISABLED) {
842     // I suppose this could happen once we deal with _NONE traffic properly
843     PRINTD (DBG_VCC, "closing VC: RX channel %u already disabled", vc);
844     return;
845   }
846   if (value == RX_CHANNEL_IDLE)
847     was_idle = 1;
848   
849   spin_lock_irqsave (&dev->mem_lock, flags);
850   
851   for (;;) {
852     wr_mem (dev, &rx_desc->wr_buf_type, RX_CHANNEL_DISABLED);
853     
854     if ((rd_mem (dev, &rx_desc->wr_buf_type) & BUFFER_PTR_MASK) == RX_CHANNEL_DISABLED)
855       break;
856     
857     was_idle = 0;
858   }
859   
860   if (was_idle) {
861     spin_unlock_irqrestore (&dev->mem_lock, flags);
862     return;
863   }
864   
865   WAIT_FLUSH_RX_COMPLETE(dev);
866   
867   // XXX Is this all really necessary? We can rely on the rx_data_av
868   // handler to discard frames that remain queued for delivery. If the
869   // worry is that immediately reopening the channel (perhaps by a
870   // different process) may cause some data to be mis-delivered then
871   // there may still be a simpler solution (such as busy-waiting on
872   // rx_busy once the channel is disabled or before a new one is
873   // opened - does this leave any holes?). Arguably setting up and
874   // tearing down the TX and RX halves of each virtual circuit could
875   // most safely be done within ?x_busy protected regions.
876   
877   // OK, current changes are that Simon's marker is disabled and we DO
878   // look for NULL rxer elsewhere. The code here seems flush frames
879   // and then remember the last dead cell belonging to the channel
880   // just disabled - the cell gets relinked at the next vc_open.
881   // However, when all VCs are closed or only a few opened there are a
882   // handful of buffers that are unusable.
883   
884   // Does anyone feel like documenting spare_buffers properly?
885   // Does anyone feel like fixing this in a nicer way?
886   
887   // Flush any data which is left in the channel
888   for (;;) {
889     // Change the rx channel port to something different to the RX
890     // channel we are trying to close to force Horizon to flush the rx
891     // channel read and write pointers.
892     
893     u16 other = vc^(RX_CHANS/2);
894     
895     SELECT_RX_CHANNEL (dev, other);
896     WAIT_UPDATE_COMPLETE (dev);
897     
898     r1 = rd_mem (dev, &rx_desc->rd_buf_type);
899     
900     // Select this RX channel. Flush doesn't seem to work unless we
901     // select an RX channel before hand
902     
903     SELECT_RX_CHANNEL (dev, vc);
904     WAIT_UPDATE_COMPLETE (dev);
905     
906     // Attempt to flush a frame on this RX channel
907     
908     FLUSH_RX_CHANNEL (dev, vc);
909     WAIT_FLUSH_RX_COMPLETE (dev);
910     
911     // Force Horizon to flush rx channel read and write pointers as before
912     
913     SELECT_RX_CHANNEL (dev, other);
914     WAIT_UPDATE_COMPLETE (dev);
915     
916     r2 = rd_mem (dev, &rx_desc->rd_buf_type);
917     
918     PRINTD (DBG_VCC|DBG_RX, "r1 = %u, r2 = %u", r1, r2);
919     
920     if (r1 == r2) {
921       dev->spare_buffers[dev->noof_spare_buffers++] = (u16)r1;
922       break;
923     }
924   }
925   
926 #if 0
927   {
928     rx_q_entry * wr_ptr = &memmap->rx_q_entries[rd_regw (dev, RX_QUEUE_WR_PTR_OFF)];
929     rx_q_entry * rd_ptr = dev->rx_q_entry;
930     
931     PRINTD (DBG_VCC|DBG_RX, "rd_ptr = %u, wr_ptr = %u", rd_ptr, wr_ptr);
932     
933     while (rd_ptr != wr_ptr) {
934       u32 x = rd_mem (dev, (HDW *) rd_ptr);
935       
936       if (vc == rx_q_entry_to_rx_channel (x)) {
937         x |= SIMONS_DODGEY_MARKER;
938         
939         PRINTD (DBG_RX|DBG_VCC|DBG_WARN, "marking a frame as dodgey");
940         
941         wr_mem (dev, (HDW *) rd_ptr, x);
942       }
943       
944       if (rd_ptr == dev->rx_q_wrap)
945         rd_ptr = dev->rx_q_reset;
946       else
947         rd_ptr++;
948     }
949   }
950 #endif
951   
952   spin_unlock_irqrestore (&dev->mem_lock, flags);
953   
954   return;
955 }
956
957 /********** schedule RX transfers **********/
958
959 // Note on tail recursion: a GCC developer said that it is not likely
960 // to be fixed soon, so do not define TAILRECUSRIONWORKS unless you
961 // are sure it does as you may otherwise overflow the kernel stack.
962
963 // giving this fn a return value would help GCC, alledgedly
964
965 static void rx_schedule (hrz_dev * dev, int irq) {
966   unsigned int rx_bytes;
967   
968   int pio_instead = 0;
969 #ifndef TAILRECURSIONWORKS
970   pio_instead = 1;
971   while (pio_instead) {
972 #endif
973     // bytes waiting for RX transfer
974     rx_bytes = dev->rx_bytes;
975     
976 #if 0
977     spin_count = 0;
978     while (rd_regl (dev, MASTER_RX_COUNT_REG_OFF)) {
979       PRINTD (DBG_RX|DBG_WARN, "RX error: other PCI Bus Master RX still in progress!");
980       if (++spin_count > 10) {
981         PRINTD (DBG_RX|DBG_ERR, "spun out waiting PCI Bus Master RX completion");
982         wr_regl (dev, MASTER_RX_COUNT_REG_OFF, 0);
983         clear_bit (rx_busy, &dev->flags);
984         hrz_kfree_skb (dev->rx_skb);
985         return;
986       }
987     }
988 #endif
989     
990     // this code follows the TX code but (at the moment) there is only
991     // one region - the skb itself. I don't know if this will change,
992     // but it doesn't hurt to have the code here, disabled.
993     
994     if (rx_bytes) {
995       // start next transfer within same region
996       if (rx_bytes <= MAX_PIO_COUNT) {
997         PRINTD (DBG_RX|DBG_BUS, "(pio)");
998         pio_instead = 1;
999       }
1000       if (rx_bytes <= MAX_TRANSFER_COUNT) {
1001         PRINTD (DBG_RX|DBG_BUS, "(simple or last multi)");
1002         dev->rx_bytes = 0;
1003       } else {
1004         PRINTD (DBG_RX|DBG_BUS, "(continuing multi)");
1005         dev->rx_bytes = rx_bytes - MAX_TRANSFER_COUNT;
1006         rx_bytes = MAX_TRANSFER_COUNT;
1007       }
1008     } else {
1009       // rx_bytes == 0 -- we're between regions
1010       // regions remaining to transfer
1011 #if 0
1012       unsigned int rx_regions = dev->rx_regions;
1013 #else
1014       unsigned int rx_regions = 0;
1015 #endif
1016       
1017       if (rx_regions) {
1018 #if 0
1019         // start a new region
1020         dev->rx_addr = dev->rx_iovec->iov_base;
1021         rx_bytes = dev->rx_iovec->iov_len;
1022         ++dev->rx_iovec;
1023         dev->rx_regions = rx_regions - 1;
1024         
1025         if (rx_bytes <= MAX_PIO_COUNT) {
1026           PRINTD (DBG_RX|DBG_BUS, "(pio)");
1027           pio_instead = 1;
1028         }
1029         if (rx_bytes <= MAX_TRANSFER_COUNT) {
1030           PRINTD (DBG_RX|DBG_BUS, "(full region)");
1031           dev->rx_bytes = 0;
1032         } else {
1033           PRINTD (DBG_RX|DBG_BUS, "(start multi region)");
1034           dev->rx_bytes = rx_bytes - MAX_TRANSFER_COUNT;
1035           rx_bytes = MAX_TRANSFER_COUNT;
1036         }
1037 #endif
1038       } else {
1039         // rx_regions == 0
1040         // that's all folks - end of frame
1041         struct sk_buff * skb = dev->rx_skb;
1042         // dev->rx_iovec = 0;
1043         
1044         FLUSH_RX_CHANNEL (dev, dev->rx_channel);
1045         
1046         dump_skb ("<<<", dev->rx_channel, skb);
1047         
1048         PRINTD (DBG_RX|DBG_SKB, "push %p %u", skb->data, skb->len);
1049         
1050         {
1051           struct atm_vcc * vcc = ATM_SKB(skb)->vcc;
1052           // VC layer stats
1053           atomic_inc(&vcc->stats->rx);
1054           skb->stamp = xtime;
1055           // end of our responsability
1056           vcc->push (vcc, skb);
1057         }
1058       }
1059     }
1060     
1061     // note: writing RX_COUNT clears any interrupt condition
1062     if (rx_bytes) {
1063       if (pio_instead) {
1064         if (irq)
1065           wr_regl (dev, MASTER_RX_COUNT_REG_OFF, 0);
1066         rds_regb (dev, DATA_PORT_OFF, dev->rx_addr, rx_bytes);
1067       } else {
1068         wr_regl (dev, MASTER_RX_ADDR_REG_OFF, virt_to_bus (dev->rx_addr));
1069         wr_regl (dev, MASTER_RX_COUNT_REG_OFF, rx_bytes);
1070       }
1071       dev->rx_addr += rx_bytes;
1072     } else {
1073       if (irq)
1074         wr_regl (dev, MASTER_RX_COUNT_REG_OFF, 0);
1075       // allow another RX thread to start
1076       YELLOW_LED_ON(dev);
1077       clear_bit (rx_busy, &dev->flags);
1078       PRINTD (DBG_RX, "cleared rx_busy for dev %p", dev);
1079     }
1080     
1081 #ifdef TAILRECURSIONWORKS
1082     // and we all bless optimised tail calls
1083     if (pio_instead)
1084       return rx_schedule (dev, 0);
1085     return;
1086 #else
1087     // grrrrrrr!
1088     irq = 0;
1089   }
1090   return;
1091 #endif
1092 }
1093
1094 /********** handle RX bus master complete events **********/
1095
1096 static inline void rx_bus_master_complete_handler (hrz_dev * dev) {
1097   if (test_bit (rx_busy, &dev->flags)) {
1098     rx_schedule (dev, 1);
1099   } else {
1100     PRINTD (DBG_RX|DBG_ERR, "unexpected RX bus master completion");
1101     // clear interrupt condition on adapter
1102     wr_regl (dev, MASTER_RX_COUNT_REG_OFF, 0);
1103   }
1104   return;
1105 }
1106
1107 /********** (queue to) become the next TX thread **********/
1108
1109 static inline int tx_hold (hrz_dev * dev) {
1110   while (test_and_set_bit (tx_busy, &dev->flags)) {
1111     PRINTD (DBG_TX, "sleeping at tx lock %p %u", dev, dev->flags);
1112     interruptible_sleep_on (&dev->tx_queue);
1113     PRINTD (DBG_TX, "woken at tx lock %p %u", dev, dev->flags);
1114     if (signal_pending (current))
1115       return -1;
1116   }
1117   PRINTD (DBG_TX, "set tx_busy for dev %p", dev);
1118   return 0;
1119 }
1120
1121 /********** allow another TX thread to start **********/
1122
1123 static inline void tx_release (hrz_dev * dev) {
1124   clear_bit (tx_busy, &dev->flags);
1125   PRINTD (DBG_TX, "cleared tx_busy for dev %p", dev);
1126   wake_up_interruptible (&dev->tx_queue);
1127 }
1128
1129 /********** schedule TX transfers **********/
1130
1131 static void tx_schedule (hrz_dev * const dev, int irq) {
1132   unsigned int tx_bytes;
1133   
1134   int append_desc = 0;
1135   
1136   int pio_instead = 0;
1137 #ifndef TAILRECURSIONWORKS
1138   pio_instead = 1;
1139   while (pio_instead) {
1140 #endif
1141     // bytes in current region waiting for TX transfer
1142     tx_bytes = dev->tx_bytes;
1143     
1144 #if 0
1145     spin_count = 0;
1146     while (rd_regl (dev, MASTER_TX_COUNT_REG_OFF)) {
1147       PRINTD (DBG_TX|DBG_WARN, "TX error: other PCI Bus Master TX still in progress!");
1148       if (++spin_count > 10) {
1149         PRINTD (DBG_TX|DBG_ERR, "spun out waiting PCI Bus Master TX completion");
1150         wr_regl (dev, MASTER_TX_COUNT_REG_OFF, 0);
1151         tx_release (dev);
1152         hrz_kfree_skb (dev->tx_skb);
1153         return;
1154       }
1155     }
1156 #endif
1157     
1158     if (tx_bytes) {
1159       // start next transfer within same region
1160       if (!test_bit (ultra, &dev->flags) || tx_bytes <= MAX_PIO_COUNT) {
1161         PRINTD (DBG_TX|DBG_BUS, "(pio)");
1162         pio_instead = 1;
1163       }
1164       if (tx_bytes <= MAX_TRANSFER_COUNT) {
1165         PRINTD (DBG_TX|DBG_BUS, "(simple or last multi)");
1166         if (!dev->tx_iovec) {
1167           // end of last region
1168           append_desc = 1;
1169         }
1170         dev->tx_bytes = 0;
1171       } else {
1172         PRINTD (DBG_TX|DBG_BUS, "(continuing multi)");
1173         dev->tx_bytes = tx_bytes - MAX_TRANSFER_COUNT;
1174         tx_bytes = MAX_TRANSFER_COUNT;
1175       }
1176     } else {
1177       // tx_bytes == 0 -- we're between regions
1178       // regions remaining to transfer
1179       unsigned int tx_regions = dev->tx_regions;
1180       
1181       if (tx_regions) {
1182         // start a new region
1183         dev->tx_addr = dev->tx_iovec->iov_base;
1184         tx_bytes = dev->tx_iovec->iov_len;
1185         ++dev->tx_iovec;
1186         dev->tx_regions = tx_regions - 1;
1187         
1188         if (!test_bit (ultra, &dev->flags) || tx_bytes <= MAX_PIO_COUNT) {
1189           PRINTD (DBG_TX|DBG_BUS, "(pio)");
1190           pio_instead = 1;
1191         }
1192         if (tx_bytes <= MAX_TRANSFER_COUNT) {
1193           PRINTD (DBG_TX|DBG_BUS, "(full region)");
1194           dev->tx_bytes = 0;
1195         } else {
1196           PRINTD (DBG_TX|DBG_BUS, "(start multi region)");
1197           dev->tx_bytes = tx_bytes - MAX_TRANSFER_COUNT;
1198           tx_bytes = MAX_TRANSFER_COUNT;
1199         }
1200       } else {
1201         // tx_regions == 0
1202         // that's all folks - end of frame
1203         struct sk_buff * skb = dev->tx_skb;
1204         dev->tx_iovec = 0;
1205         
1206         // VC layer stats
1207         atomic_inc(&ATM_SKB(skb)->vcc->stats->tx);
1208         
1209         // free the skb
1210         hrz_kfree_skb (skb);
1211       }
1212     }
1213     
1214     // note: writing TX_COUNT clears any interrupt condition
1215     if (tx_bytes) {
1216       if (pio_instead) {
1217         if (irq)
1218           wr_regl (dev, MASTER_TX_COUNT_REG_OFF, 0);
1219         wrs_regb (dev, DATA_PORT_OFF, dev->tx_addr, tx_bytes);
1220         if (append_desc)
1221           wr_regl (dev, TX_DESCRIPTOR_PORT_OFF, cpu_to_be32 (dev->tx_skb->len));
1222       } else {
1223         wr_regl (dev, MASTER_TX_ADDR_REG_OFF, virt_to_bus (dev->tx_addr));
1224         if (append_desc)
1225           wr_regl (dev, TX_DESCRIPTOR_REG_OFF, cpu_to_be32 (dev->tx_skb->len));
1226         wr_regl (dev, MASTER_TX_COUNT_REG_OFF,
1227                  append_desc
1228                  ? tx_bytes | MASTER_TX_AUTO_APPEND_DESC
1229                  : tx_bytes);
1230       }
1231       dev->tx_addr += tx_bytes;
1232     } else {
1233       if (irq)
1234         wr_regl (dev, MASTER_TX_COUNT_REG_OFF, 0);
1235       YELLOW_LED_ON(dev);
1236       tx_release (dev);
1237     }
1238     
1239 #ifdef TAILRECURSIONWORKS
1240     // and we all bless optimised tail calls
1241     if (pio_instead)
1242       return tx_schedule (dev, 0);
1243     return;
1244 #else
1245     // grrrrrrr!
1246     irq = 0;
1247   }
1248   return;
1249 #endif
1250 }
1251
1252 /********** handle TX bus master complete events **********/
1253
1254 static inline void tx_bus_master_complete_handler (hrz_dev * dev) {
1255   if (test_bit (tx_busy, &dev->flags)) {
1256     tx_schedule (dev, 1);
1257   } else {
1258     PRINTD (DBG_TX|DBG_ERR, "unexpected TX bus master completion");
1259     // clear interrupt condition on adapter
1260     wr_regl (dev, MASTER_TX_COUNT_REG_OFF, 0);
1261   }
1262   return;
1263 }
1264
1265 /********** move RX Q pointer to next item in circular buffer **********/
1266
1267 // called only from IRQ sub-handler
1268 static inline u32 rx_queue_entry_next (hrz_dev * dev) {
1269   u32 rx_queue_entry;
1270   spin_lock (&dev->mem_lock);
1271   rx_queue_entry = rd_mem (dev, &dev->rx_q_entry->entry);
1272   if (dev->rx_q_entry == dev->rx_q_wrap)
1273     dev->rx_q_entry = dev->rx_q_reset;
1274   else
1275     dev->rx_q_entry++;
1276   wr_regw (dev, RX_QUEUE_RD_PTR_OFF, dev->rx_q_entry - dev->rx_q_reset);
1277   spin_unlock (&dev->mem_lock);
1278   return rx_queue_entry;
1279 }
1280
1281 /********** handle RX disabled by device **********/
1282
1283 static inline void rx_disabled_handler (hrz_dev * dev) {
1284   wr_regw (dev, RX_CONFIG_OFF, rd_regw (dev, RX_CONFIG_OFF) | RX_ENABLE);
1285   // count me please
1286   PRINTK (KERN_WARNING, "RX was disabled!");
1287 }
1288
1289 /********** handle RX data received by device **********/
1290
1291 // called from IRQ handler
1292 static inline void rx_data_av_handler (hrz_dev * dev) {
1293   u32 rx_queue_entry;
1294   u32 rx_queue_entry_flags;
1295   u16 rx_len;
1296   u16 rx_channel;
1297   
1298   PRINTD (DBG_FLOW, "hrz_data_av_handler");
1299   
1300   // try to grab rx lock (not possible during RX bus mastering)
1301   if (test_and_set_bit (rx_busy, &dev->flags)) {
1302     PRINTD (DBG_RX, "locked out of rx lock");
1303     return;
1304   }
1305   PRINTD (DBG_RX, "set rx_busy for dev %p", dev);
1306   // lock is cleared if we fail now, o/w after bus master completion
1307   
1308   YELLOW_LED_OFF(dev);
1309   
1310   rx_queue_entry = rx_queue_entry_next (dev);
1311   
1312   rx_len = rx_q_entry_to_length (rx_queue_entry);
1313   rx_channel = rx_q_entry_to_rx_channel (rx_queue_entry);
1314   
1315   WAIT_FLUSH_RX_COMPLETE (dev);
1316   
1317   SELECT_RX_CHANNEL (dev, rx_channel);
1318   
1319   PRINTD (DBG_RX, "rx_queue_entry is: %#x", rx_queue_entry);
1320   rx_queue_entry_flags = rx_queue_entry & (RX_CRC_32_OK|RX_COMPLETE_FRAME|SIMONS_DODGEY_MARKER);
1321   
1322   if (!rx_len) {
1323     // (at least) bus-mastering breaks if we try to handle a
1324     // zero-length frame, besides AAL5 does not support them
1325     PRINTK (KERN_ERR, "zero-length frame!");
1326     rx_queue_entry_flags &= ~RX_COMPLETE_FRAME;
1327   }
1328   
1329   if (rx_queue_entry_flags & SIMONS_DODGEY_MARKER) {
1330     PRINTD (DBG_RX|DBG_ERR, "Simon's marker detected!");
1331   }
1332   if (rx_queue_entry_flags == (RX_CRC_32_OK | RX_COMPLETE_FRAME)) {
1333     struct atm_vcc * atm_vcc;
1334     
1335     PRINTD (DBG_RX, "got a frame on rx_channel %x len %u", rx_channel, rx_len);
1336     
1337     atm_vcc = dev->rxer[rx_channel];
1338     // if no vcc is assigned to this channel, we should drop the frame
1339     // (is this what SIMONS etc. was trying to achieve?)
1340     
1341     if (atm_vcc) {
1342       
1343       if (atm_vcc->qos.rxtp.traffic_class != ATM_NONE) {
1344         
1345         if (rx_len <= atm_vcc->qos.rxtp.max_sdu) {
1346             
1347           struct sk_buff * skb = atm_alloc_charge (atm_vcc, rx_len, GFP_ATOMIC);
1348           if (skb) {
1349             // remember this so we can push it later
1350             dev->rx_skb = skb;
1351             // remember this so we can flush it later
1352             dev->rx_channel = rx_channel;
1353             
1354             // prepare socket buffer
1355             skb_put (skb, rx_len);
1356             ATM_SKB(skb)->vcc = atm_vcc;
1357             
1358             // simple transfer
1359             // dev->rx_regions = 0;
1360             // dev->rx_iovec = 0;
1361             dev->rx_bytes = rx_len;
1362             dev->rx_addr = skb->data;
1363             PRINTD (DBG_RX, "RX start simple transfer (addr %p, len %d)",
1364                     skb->data, rx_len);
1365             
1366             // do the business
1367             rx_schedule (dev, 0);
1368             return;
1369             
1370           } else {
1371             PRINTD (DBG_SKB|DBG_WARN, "failed to get skb");
1372           }
1373           
1374         } else {
1375           PRINTK (KERN_INFO, "frame received on TX-only VC %x", rx_channel);
1376           // do we count this?
1377         }
1378         
1379       } else {
1380         PRINTK (KERN_WARNING, "dropped over-size frame");
1381         // do we count this?
1382       }
1383       
1384     } else {
1385       PRINTD (DBG_WARN|DBG_VCC|DBG_RX, "no VCC for this frame (VC closed)");
1386       // do we count this?
1387     }
1388     
1389   } else {
1390     // Wait update complete ? SPONG
1391   }
1392   
1393   // RX was aborted
1394   YELLOW_LED_ON(dev);
1395   
1396   FLUSH_RX_CHANNEL (dev,rx_channel);
1397   clear_bit (rx_busy, &dev->flags);
1398   
1399   return;
1400 }
1401
1402 /********** interrupt handler **********/
1403
1404 static void interrupt_handler (int irq, void * dev_id, struct pt_regs * pt_regs) {
1405   hrz_dev * dev = hrz_devs;
1406   u32 int_source;
1407   unsigned int irq_ok;
1408   (void) pt_regs;
1409   
1410   PRINTD (DBG_FLOW, "interrupt_handler: %p", dev_id);
1411   
1412   if (!dev_id) {
1413     PRINTD (DBG_IRQ|DBG_ERR, "irq with NULL dev_id: %d", irq);
1414     return;
1415   }
1416   // Did one of our cards generate the interrupt?
1417   while (dev) {
1418     if (dev == dev_id)
1419       break;
1420     dev = dev->prev;
1421   }
1422   if (!dev) {
1423     PRINTD (DBG_IRQ, "irq not for me: %d", irq);
1424     return;
1425   }
1426   if (irq != dev->irq) {
1427     PRINTD (DBG_IRQ|DBG_ERR, "irq mismatch: %d", irq);
1428     return;
1429   }
1430   
1431   // definitely for us
1432   irq_ok = 0;
1433   while ((int_source = rd_regl (dev, INT_SOURCE_REG_OFF)
1434           & INTERESTING_INTERRUPTS)) {
1435     // In the interests of fairness, the (inline) handlers below are
1436     // called in sequence and without immediate return to the head of
1437     // the while loop. This is only of issue for slow hosts (or when
1438     // debugging messages are on). Really slow hosts may find a fast
1439     // sender keeps them permanently in the IRQ handler. :(
1440     
1441     // (only an issue for slow hosts) RX completion goes before
1442     // rx_data_av as the former implies rx_busy and so the latter
1443     // would just abort. If it reschedules another transfer
1444     // (continuing the same frame) then it will not clear rx_busy.
1445     
1446     // (only an issue for slow hosts) TX completion goes before RX
1447     // data available as it is a much shorter routine - there is the
1448     // chance that any further transfers it schedules will be complete
1449     // by the time of the return to the head of the while loop
1450     
1451     if (int_source & RX_BUS_MASTER_COMPLETE) {
1452       ++irq_ok;
1453       PRINTD (DBG_IRQ|DBG_BUS|DBG_RX, "rx_bus_master_complete asserted");
1454       rx_bus_master_complete_handler (dev);
1455     }
1456     if (int_source & TX_BUS_MASTER_COMPLETE) {
1457       ++irq_ok;
1458       PRINTD (DBG_IRQ|DBG_BUS|DBG_TX, "tx_bus_master_complete asserted");
1459       tx_bus_master_complete_handler (dev);
1460     }
1461     if (int_source & RX_DATA_AV) {
1462       ++irq_ok;
1463       PRINTD (DBG_IRQ|DBG_RX, "rx_data_av asserted");
1464       rx_data_av_handler (dev);
1465     }
1466   }
1467   if (irq_ok) {
1468     PRINTD (DBG_IRQ, "work done: %u", irq_ok);
1469   } else {
1470     PRINTD (DBG_IRQ|DBG_WARN, "spurious interrupt source: %#x", int_source);
1471   }
1472   
1473   PRINTD (DBG_IRQ|DBG_FLOW, "interrupt_handler done: %p", dev_id);
1474 }
1475
1476 /********** housekeeping **********/
1477
1478 static void set_timer (struct timer_list * timer, unsigned int delay) {
1479   timer->expires = jiffies + delay;
1480   add_timer (timer);
1481   return;
1482 }
1483
1484 static void do_housekeeping (unsigned long arg) {
1485   // just stats at the moment
1486   hrz_dev * dev = hrz_devs;
1487   (void) arg;
1488   // data is set to zero at module unload
1489   if (housekeeping.data) {
1490     while (dev) {
1491       // collect device-specific (not driver/atm-linux) stats here
1492       dev->tx_cell_count += rd_regw (dev, TX_CELL_COUNT_OFF);
1493       dev->rx_cell_count += rd_regw (dev, RX_CELL_COUNT_OFF);
1494       dev->hec_error_count += rd_regw (dev, HEC_ERROR_COUNT_OFF);
1495       dev->unassigned_cell_count += rd_regw (dev, UNASSIGNED_CELL_COUNT_OFF);
1496       dev = dev->prev;
1497     }
1498     set_timer (&housekeeping, HZ/10);
1499   }
1500   return;
1501 }
1502
1503 /********** find an idle channel for TX and set it up **********/
1504
1505 // called with tx_busy set
1506 static inline short setup_idle_tx_channel (hrz_dev * dev, hrz_vcc * vcc) {
1507   unsigned short idle_channels;
1508   short tx_channel = -1;
1509   unsigned int spin_count;
1510   PRINTD (DBG_FLOW|DBG_TX, "setup_idle_tx_channel %p", dev);
1511   
1512   // better would be to fail immediately, the caller can then decide whether
1513   // to wait or drop (depending on whether this is UBR etc.)
1514   spin_count = 0;
1515   while (!(idle_channels = rd_regw (dev, TX_STATUS_OFF) & IDLE_CHANNELS_MASK)) {
1516     PRINTD (DBG_TX|DBG_WARN, "waiting for idle TX channel");
1517     // delay a bit here
1518     if (++spin_count > 100) {
1519       PRINTD (DBG_TX|DBG_ERR, "spun out waiting for idle TX channel");
1520       return -EBUSY;
1521     }
1522   }
1523   
1524   // got an idle channel
1525   {
1526     // tx_idle ensures we look for idle channels in RR order
1527     int chan = dev->tx_idle;
1528     
1529     int keep_going = 1;
1530     while (keep_going) {
1531       if (idle_channels & (1<<chan)) {
1532         tx_channel = chan;
1533         keep_going = 0;
1534       }
1535       ++chan;
1536       if (chan == TX_CHANS)
1537         chan = 0;
1538     }
1539     
1540     dev->tx_idle = chan;
1541   }
1542   
1543   // set up the channel we found
1544   {
1545     // Initialise the cell header in the transmit channel descriptor
1546     // a.k.a. prepare the channel and remember that we have done so.
1547     
1548     tx_ch_desc * tx_desc = &memmap->tx_descs[tx_channel];
1549     u16 rd_ptr;
1550     u16 wr_ptr;
1551     u16 channel = vcc->channel;
1552     
1553     unsigned long flags;
1554     spin_lock_irqsave (&dev->mem_lock, flags);
1555     
1556     // Update the transmit channel record.
1557     dev->tx_channel_record[tx_channel] = channel;
1558     
1559     // xBR channel
1560     update_tx_channel_config (dev, tx_channel, RATE_TYPE_ACCESS,
1561                               vcc->tx_xbr_bits);
1562     
1563     // Update the PCR counter preload value etc.
1564     update_tx_channel_config (dev, tx_channel, PCR_TIMER_ACCESS,
1565                               vcc->tx_pcr_bits);
1566
1567 #if 0
1568     if (vcc->tx_xbr_bits == VBR_RATE_TYPE) {
1569       // SCR timer
1570       update_tx_channel_config (dev, tx_channel, SCR_TIMER_ACCESS,
1571                                 vcc->tx_scr_bits);
1572       
1573       // Bucket size...
1574       update_tx_channel_config (dev, tx_channel, BUCKET_CAPACITY_ACCESS,
1575                                 vcc->tx_bucket_bits);
1576       
1577       // ... and fullness
1578       update_tx_channel_config (dev, tx_channel, BUCKET_FULLNESS_ACCESS,
1579                                 vcc->tx_bucket_bits);
1580     }
1581 #endif
1582
1583     // Initialise the read and write buffer pointers
1584     rd_ptr = rd_mem (dev, &tx_desc->rd_buf_type) & BUFFER_PTR_MASK;
1585     wr_ptr = rd_mem (dev, &tx_desc->wr_buf_type) & BUFFER_PTR_MASK;
1586     
1587     // idle TX channels should have identical pointers
1588     if (rd_ptr != wr_ptr) {
1589       PRINTD (DBG_TX|DBG_ERR, "TX buffer pointers are broken!");
1590       // spin_unlock... return -E...
1591       // I wonder if gcc would get rid of one of the pointer aliases
1592     }
1593     PRINTD (DBG_TX, "TX buffer pointers are: rd %x, wr %x.",
1594             rd_ptr, wr_ptr);
1595     
1596     switch (vcc->aal) {
1597       case aal0:
1598         PRINTD (DBG_QOS|DBG_TX, "tx_channel: aal0");
1599         rd_ptr |= CHANNEL_TYPE_RAW_CELLS;
1600         wr_ptr |= CHANNEL_TYPE_RAW_CELLS;
1601         break;
1602       case aal34:
1603         PRINTD (DBG_QOS|DBG_TX, "tx_channel: aal34");
1604         rd_ptr |= CHANNEL_TYPE_AAL3_4;
1605         wr_ptr |= CHANNEL_TYPE_AAL3_4;
1606         break;
1607       case aal5:
1608         rd_ptr |= CHANNEL_TYPE_AAL5;
1609         wr_ptr |= CHANNEL_TYPE_AAL5;
1610         // Initialise the CRC
1611         wr_mem (dev, &tx_desc->partial_crc, INITIAL_CRC);
1612         break;
1613     }
1614     
1615     wr_mem (dev, &tx_desc->rd_buf_type, rd_ptr);
1616     wr_mem (dev, &tx_desc->wr_buf_type, wr_ptr);
1617     
1618     // Write the Cell Header
1619     // Payload Type, CLP and GFC would go here if non-zero
1620     wr_mem (dev, &tx_desc->cell_header, channel);
1621     
1622     spin_unlock_irqrestore (&dev->mem_lock, flags);
1623   }
1624   
1625   return tx_channel;
1626 }
1627
1628 /********** send a frame **********/
1629
1630 static int hrz_send (struct atm_vcc * atm_vcc, struct sk_buff * skb) {
1631   unsigned int spin_count;
1632   int free_buffers;
1633   hrz_dev * dev = HRZ_DEV(atm_vcc->dev);
1634   hrz_vcc * vcc = HRZ_VCC(atm_vcc);
1635   u16 channel = vcc->channel;
1636   
1637   u32 buffers_required;
1638   
1639   /* signed for error return */
1640   short tx_channel;
1641   
1642   PRINTD (DBG_FLOW|DBG_TX, "hrz_send vc %x data %p len %u",
1643           channel, skb->data, skb->len);
1644   
1645   dump_skb (">>>", channel, skb);
1646   
1647   if (atm_vcc->qos.txtp.traffic_class == ATM_NONE) {
1648     PRINTK (KERN_ERR, "attempt to send on RX-only VC %x", channel);
1649     hrz_kfree_skb (skb);
1650     return -EIO;
1651   }
1652   
1653   // don't understand this
1654   ATM_SKB(skb)->vcc = atm_vcc;
1655   
1656   if (skb->len > atm_vcc->qos.txtp.max_sdu) {
1657     PRINTK (KERN_ERR, "sk_buff length greater than agreed max_sdu, dropping...");
1658     hrz_kfree_skb (skb);
1659     return -EIO;
1660   }
1661   
1662   if (!channel) {
1663     PRINTD (DBG_ERR|DBG_TX, "attempt to transmit on zero (rx_)channel");
1664     hrz_kfree_skb (skb);
1665     return -EIO;
1666   }
1667   
1668 #if 0
1669   {
1670     // where would be a better place for this? housekeeping?
1671     u16 status;
1672     pci_read_config_word (dev->pci_dev, PCI_STATUS, &status);
1673     if (status & PCI_STATUS_REC_MASTER_ABORT) {
1674       PRINTD (DBG_BUS|DBG_ERR, "Clearing PCI Master Abort (and cleaning up)");
1675       status &= ~PCI_STATUS_REC_MASTER_ABORT;
1676       pci_write_config_word (dev->pci_dev, PCI_STATUS, status);
1677       if (test_bit (tx_busy, &dev->flags)) {
1678         hrz_kfree_skb (dev->tx_skb);
1679         tx_release (dev);
1680       }
1681     }
1682   }
1683 #endif
1684   
1685 #ifdef DEBUG_HORIZON
1686   /* wey-hey! */
1687   if (channel == 1023) {
1688     unsigned int i;
1689     unsigned short d = 0;
1690     char * s = skb->data;
1691     if (*s++ == 'D') {
1692       for (i = 0; i < 4; ++i) {
1693         d = (d<<4) | ((*s <= '9') ? (*s - '0') : (*s - 'a' + 10));
1694         ++s;
1695       }
1696       PRINTK (KERN_INFO, "debug bitmap is now %hx", debug = d);
1697     }
1698   }
1699 #endif
1700   
1701   // wait until TX is free and grab lock
1702   if (tx_hold (dev)) {
1703     hrz_kfree_skb (skb);
1704     return -ERESTARTSYS;
1705   }
1706  
1707   // Wait for enough space to be available in transmit buffer memory.
1708   
1709   // should be number of cells needed + 2 (according to hardware docs)
1710   // = ((framelen+8)+47) / 48 + 2
1711   // = (framelen+7) / 48 + 3, hmm... faster to put addition inside XXX
1712   buffers_required = (skb->len+(ATM_AAL5_TRAILER-1)) / ATM_CELL_PAYLOAD + 3;
1713   
1714   // replace with timer and sleep, add dev->tx_buffers_queue (max 1 entry)
1715   spin_count = 0;
1716   while ((free_buffers = rd_regw (dev, TX_FREE_BUFFER_COUNT_OFF)) < buffers_required) {
1717     PRINTD (DBG_TX, "waiting for free TX buffers, got %d of %d",
1718             free_buffers, buffers_required);
1719     // what is the appropriate delay? implement a timeout? (depending on line speed?)
1720     // mdelay (1);
1721     // what happens if we kill (current_pid, SIGKILL) ?
1722     schedule();
1723     if (++spin_count > 1000) {
1724       PRINTD (DBG_TX|DBG_ERR, "spun out waiting for tx buffers, got %d of %d",
1725               free_buffers, buffers_required);
1726       tx_release (dev);
1727       hrz_kfree_skb (skb);
1728       return -ERESTARTSYS;
1729     }
1730   }
1731   
1732   // Select a channel to transmit the frame on.
1733   if (channel == dev->last_vc) {
1734     PRINTD (DBG_TX, "last vc hack: hit");
1735     tx_channel = dev->tx_last;
1736   } else {
1737     PRINTD (DBG_TX, "last vc hack: miss");
1738     // Are we currently transmitting this VC on one of the channels?
1739     for (tx_channel = 0; tx_channel < TX_CHANS; ++tx_channel)
1740       if (dev->tx_channel_record[tx_channel] == channel) {
1741         PRINTD (DBG_TX, "vc already on channel: hit");
1742         break;
1743       }
1744     if (tx_channel == TX_CHANS) { 
1745       PRINTD (DBG_TX, "vc already on channel: miss");
1746       // Find and set up an idle channel.
1747       tx_channel = setup_idle_tx_channel (dev, vcc);
1748       if (tx_channel < 0) {
1749         PRINTD (DBG_TX|DBG_ERR, "failed to get channel");
1750         tx_release (dev);
1751         return tx_channel;
1752       }
1753     }
1754     
1755     PRINTD (DBG_TX, "got channel");
1756     SELECT_TX_CHANNEL(dev, tx_channel);
1757     
1758     dev->last_vc = channel;
1759     dev->tx_last = tx_channel;
1760   }
1761   
1762   PRINTD (DBG_TX, "using channel %u", tx_channel);
1763   
1764   YELLOW_LED_OFF(dev);
1765   
1766   // TX start transfer
1767   
1768   {
1769     unsigned int tx_len = skb->len;
1770     unsigned int tx_iovcnt = skb_shinfo(skb)->nr_frags;
1771     // remember this so we can free it later
1772     dev->tx_skb = skb;
1773     
1774     if (tx_iovcnt) {
1775       // scatter gather transfer
1776       dev->tx_regions = tx_iovcnt;
1777       dev->tx_iovec = 0;                /* @@@ needs rewritten */
1778       dev->tx_bytes = 0;
1779       PRINTD (DBG_TX|DBG_BUS, "TX start scatter-gather transfer (iovec %p, len %d)",
1780               skb->data, tx_len);
1781       tx_release (dev);
1782       hrz_kfree_skb (skb);
1783       return -EIO;
1784     } else {
1785       // simple transfer
1786       dev->tx_regions = 0;
1787       dev->tx_iovec = 0;
1788       dev->tx_bytes = tx_len;
1789       dev->tx_addr = skb->data;
1790       PRINTD (DBG_TX|DBG_BUS, "TX start simple transfer (addr %p, len %d)",
1791               skb->data, tx_len);
1792     }
1793     
1794     // and do the business
1795     tx_schedule (dev, 0);
1796     
1797   }
1798   
1799   return 0;
1800 }
1801
1802 /********** reset a card **********/
1803
1804 static void __init hrz_reset (const hrz_dev * dev) {
1805   u32 control_0_reg = rd_regl (dev, CONTROL_0_REG);
1806   
1807   // why not set RESET_HORIZON to one and wait for the card to
1808   // reassert that bit as zero? Like so:
1809   control_0_reg = control_0_reg & RESET_HORIZON;
1810   wr_regl (dev, CONTROL_0_REG, control_0_reg);
1811   while (control_0_reg & RESET_HORIZON)
1812     control_0_reg = rd_regl (dev, CONTROL_0_REG);
1813   
1814   // old reset code retained:
1815   wr_regl (dev, CONTROL_0_REG, control_0_reg |
1816            RESET_ATM | RESET_RX | RESET_TX | RESET_HOST);
1817   // just guessing here
1818   udelay (1000);
1819   
1820   wr_regl (dev, CONTROL_0_REG, control_0_reg);
1821 }
1822
1823 /********** read the burnt in address **********/
1824
1825 static u16 __init read_bia (const hrz_dev * dev, u16 addr) {
1826   
1827   u32 ctrl = rd_regl (dev, CONTROL_0_REG);
1828   
1829   void WRITE_IT_WAIT (void) {
1830     wr_regl (dev, CONTROL_0_REG, ctrl);
1831     udelay (5);
1832   }
1833   
1834   void CLOCK_IT (void) {
1835     // DI must be valid around rising SK edge
1836     ctrl &= ~SEEPROM_SK;
1837     WRITE_IT_WAIT();
1838     ctrl |= SEEPROM_SK;
1839     WRITE_IT_WAIT();
1840   }
1841   
1842   const unsigned int addr_bits = 6;
1843   const unsigned int data_bits = 16;
1844   
1845   unsigned int i;
1846   
1847   u16 res;
1848   
1849   ctrl &= ~(SEEPROM_CS | SEEPROM_SK | SEEPROM_DI);
1850   WRITE_IT_WAIT();
1851   
1852   // wake Serial EEPROM and send 110 (READ) command
1853   ctrl |=  (SEEPROM_CS | SEEPROM_DI);
1854   CLOCK_IT();
1855   
1856   ctrl |= SEEPROM_DI;
1857   CLOCK_IT();
1858   
1859   ctrl &= ~SEEPROM_DI;
1860   CLOCK_IT();
1861   
1862   for (i=0; i<addr_bits; i++) {
1863     if (addr & (1 << (addr_bits-1)))
1864       ctrl |= SEEPROM_DI;
1865     else
1866       ctrl &= ~SEEPROM_DI;
1867     
1868     CLOCK_IT();
1869     
1870     addr = addr << 1;
1871   }
1872   
1873   // we could check that we have DO = 0 here
1874   ctrl &= ~SEEPROM_DI;
1875   
1876   res = 0;
1877   for (i=0;i<data_bits;i++) {
1878     res = res >> 1;
1879     
1880     CLOCK_IT();
1881     
1882     if (rd_regl (dev, CONTROL_0_REG) & SEEPROM_DO)
1883       res |= (1 << (data_bits-1));
1884   }
1885   
1886   ctrl &= ~(SEEPROM_SK | SEEPROM_CS);
1887   WRITE_IT_WAIT();
1888   
1889   return res;
1890 }
1891
1892 /********** initialise a card **********/
1893
1894 static int __init hrz_init (hrz_dev * dev) {
1895   int onefivefive;
1896   
1897   u16 chan;
1898   
1899   int buff_count;
1900   
1901   HDW * mem;
1902   
1903   cell_buf * tx_desc;
1904   cell_buf * rx_desc;
1905   
1906   u32 ctrl;
1907   
1908   ctrl = rd_regl (dev, CONTROL_0_REG);
1909   PRINTD (DBG_INFO, "ctrl0reg is %#x", ctrl);
1910   onefivefive = ctrl & ATM_LAYER_STATUS;
1911   
1912   if (onefivefive)
1913     printk (DEV_LABEL ": Horizon Ultra (at 155.52 MBps)");
1914   else
1915     printk (DEV_LABEL ": Horizon (at 25 MBps)");
1916   
1917   printk (":");
1918   // Reset the card to get everything in a known state
1919   
1920   printk (" reset");
1921   hrz_reset (dev);
1922   
1923   // Clear all the buffer memory
1924   
1925   printk (" clearing memory");
1926   
1927   for (mem = (HDW *) memmap; mem < (HDW *) (memmap + 1); ++mem)
1928     wr_mem (dev, mem, 0);
1929   
1930   printk (" tx channels");
1931   
1932   // All transmit eight channels are set up as AAL5 ABR channels with
1933   // a 16us cell spacing. Why?
1934   
1935   // Channel 0 gets the free buffer at 100h, channel 1 gets the free
1936   // buffer at 110h etc.
1937   
1938   for (chan = 0; chan < TX_CHANS; ++chan) {
1939     tx_ch_desc * tx_desc = &memmap->tx_descs[chan];
1940     cell_buf * buf = &memmap->inittxbufs[chan];
1941     
1942     // initialise the read and write buffer pointers
1943     wr_mem (dev, &tx_desc->rd_buf_type, BUF_PTR(buf));
1944     wr_mem (dev, &tx_desc->wr_buf_type, BUF_PTR(buf));
1945     
1946     // set the status of the initial buffers to empty
1947     wr_mem (dev, &buf->next, BUFF_STATUS_EMPTY);
1948   }
1949   
1950   // Use space bufn3 at the moment for tx buffers
1951   
1952   printk (" tx buffers");
1953   
1954   tx_desc = memmap->bufn3;
1955   
1956   wr_mem (dev, &memmap->txfreebufstart.next, BUF_PTR(tx_desc) | BUFF_STATUS_EMPTY);
1957   
1958   for (buff_count = 0; buff_count < BUFN3_SIZE-1; buff_count++) {
1959     wr_mem (dev, &tx_desc->next, BUF_PTR(tx_desc+1) | BUFF_STATUS_EMPTY);
1960     tx_desc++;
1961   }
1962   
1963   wr_mem (dev, &tx_desc->next, BUF_PTR(&memmap->txfreebufend) | BUFF_STATUS_EMPTY);
1964   
1965   // Initialise the transmit free buffer count
1966   wr_regw (dev, TX_FREE_BUFFER_COUNT_OFF, BUFN3_SIZE);
1967   
1968   printk (" rx channels");
1969   
1970   // Initialise all of the receive channels to be AAL5 disabled with
1971   // an interrupt threshold of 0
1972   
1973   for (chan = 0; chan < RX_CHANS; ++chan) {
1974     rx_ch_desc * rx_desc = &memmap->rx_descs[chan];
1975     
1976     wr_mem (dev, &rx_desc->wr_buf_type, CHANNEL_TYPE_AAL5 | RX_CHANNEL_DISABLED);
1977   }
1978   
1979   printk (" rx buffers");
1980   
1981   // Use space bufn4 at the moment for rx buffers
1982   
1983   rx_desc = memmap->bufn4;
1984   
1985   wr_mem (dev, &memmap->rxfreebufstart.next, BUF_PTR(rx_desc) | BUFF_STATUS_EMPTY);
1986   
1987   for (buff_count = 0; buff_count < BUFN4_SIZE-1; buff_count++) {
1988     wr_mem (dev, &rx_desc->next, BUF_PTR(rx_desc+1) | BUFF_STATUS_EMPTY);
1989     
1990     rx_desc++;
1991   }
1992   
1993   wr_mem (dev, &rx_desc->next, BUF_PTR(&memmap->rxfreebufend) | BUFF_STATUS_EMPTY);
1994   
1995   // Initialise the receive free buffer count
1996   wr_regw (dev, RX_FREE_BUFFER_COUNT_OFF, BUFN4_SIZE);
1997   
1998   // Initialize Horizons registers
1999   
2000   // TX config
2001   wr_regw (dev, TX_CONFIG_OFF,
2002            ABR_ROUND_ROBIN | TX_NORMAL_OPERATION | DRVR_DRVRBAR_ENABLE);
2003   
2004   // RX config. Use 10-x VC bits, x VP bits, non user cells in channel 0.
2005   wr_regw (dev, RX_CONFIG_OFF,
2006            DISCARD_UNUSED_VPI_VCI_BITS_SET | NON_USER_CELLS_IN_ONE_CHANNEL | vpi_bits);
2007   
2008   // RX line config
2009   wr_regw (dev, RX_LINE_CONFIG_OFF,
2010            LOCK_DETECT_ENABLE | FREQUENCY_DETECT_ENABLE | GXTALOUT_SELECT_DIV4);
2011   
2012   // Set the max AAL5 cell count to be just enough to contain the
2013   // largest AAL5 frame that the user wants to receive
2014   wr_regw (dev, MAX_AAL5_CELL_COUNT_OFF,
2015            (max_rx_size + ATM_AAL5_TRAILER + ATM_CELL_PAYLOAD - 1) / ATM_CELL_PAYLOAD);
2016   
2017   // Enable receive
2018   wr_regw (dev, RX_CONFIG_OFF, rd_regw (dev, RX_CONFIG_OFF) | RX_ENABLE);
2019   
2020   printk (" control");
2021   
2022   // Drive the OE of the LEDs then turn the green LED on
2023   ctrl |= GREEN_LED_OE | YELLOW_LED_OE | GREEN_LED | YELLOW_LED;
2024   wr_regl (dev, CONTROL_0_REG, ctrl);
2025   
2026   // Test for a 155-capable card
2027   
2028   if (onefivefive) {
2029     // Select 155 mode... make this a choice (or: how do we detect
2030     // external line speed and switch?)
2031     ctrl |= ATM_LAYER_SELECT;
2032     wr_regl (dev, CONTROL_0_REG, ctrl);
2033     
2034     // test SUNI-lite vs SAMBA
2035     
2036     // Register 0x00 in the SUNI will have some of bits 3-7 set, and
2037     // they will always be zero for the SAMBA.  Ha!  Bloody hardware
2038     // engineers.  It'll never work.
2039     
2040     if (rd_framer (dev, 0) & 0x00f0) {
2041       // SUNI
2042       printk (" SUNI");
2043       
2044       // Reset, just in case
2045       wr_framer (dev, 0x00, 0x0080);
2046       wr_framer (dev, 0x00, 0x0000);
2047       
2048       // Configure transmit FIFO
2049       wr_framer (dev, 0x63, rd_framer (dev, 0x63) | 0x0002);
2050       
2051       // Set line timed mode
2052       wr_framer (dev, 0x05, rd_framer (dev, 0x05) | 0x0001);
2053     } else {
2054       // SAMBA
2055       printk (" SAMBA");
2056       
2057       // Reset, just in case
2058       wr_framer (dev, 0, rd_framer (dev, 0) | 0x0001);
2059       wr_framer (dev, 0, rd_framer (dev, 0) &~ 0x0001);
2060       
2061       // Turn off diagnostic loopback and enable line-timed mode
2062       wr_framer (dev, 0, 0x0002);
2063       
2064       // Turn on transmit outputs
2065       wr_framer (dev, 2, 0x0B80);
2066     }
2067   } else {
2068     // Select 25 mode
2069     ctrl &= ~ATM_LAYER_SELECT;
2070     
2071     // Madge B154 setup
2072     // none required?
2073   }
2074   
2075   printk (" LEDs");
2076   
2077   GREEN_LED_ON(dev);
2078   YELLOW_LED_ON(dev);
2079   
2080   printk (" ESI=");
2081   
2082   {
2083     u16 b = 0;
2084     int i;
2085     u8 * esi = dev->atm_dev->esi;
2086     
2087     // in the card I have, EEPROM
2088     // addresses 0, 1, 2 contain 0
2089     // addresess 5, 6 etc. contain ffff
2090     // NB: Madge prefix is 00 00 f6 (which is 00 00 6f in Ethernet bit order)
2091     // the read_bia routine gets the BIA in Ethernet bit order
2092     
2093     for (i=0; i < ESI_LEN; ++i) {
2094       if (i % 2 == 0)
2095         b = read_bia (dev, i/2 + 2);
2096       else
2097         b = b >> 8;
2098       esi[i] = b & 0xFF;
2099       printk ("%02x", esi[i]);
2100     }
2101   }
2102   
2103   // Enable RX_Q and ?X_COMPLETE interrupts only
2104   wr_regl (dev, INT_ENABLE_REG_OFF, INTERESTING_INTERRUPTS);
2105   printk (" IRQ on");
2106   
2107   printk (".\n");
2108   
2109   return onefivefive;
2110 }
2111
2112 /********** check max_sdu **********/
2113
2114 static int check_max_sdu (hrz_aal aal, struct atm_trafprm * tp, unsigned int max_frame_size) {
2115   PRINTD (DBG_FLOW|DBG_QOS, "check_max_sdu");
2116   
2117   switch (aal) {
2118     case aal0:
2119       if (!(tp->max_sdu)) {
2120         PRINTD (DBG_QOS, "defaulting max_sdu");
2121         tp->max_sdu = ATM_AAL0_SDU;
2122       } else if (tp->max_sdu != ATM_AAL0_SDU) {
2123         PRINTD (DBG_QOS|DBG_ERR, "rejecting max_sdu");
2124         return -EINVAL;
2125       }
2126       break;
2127     case aal34:
2128       if (tp->max_sdu == 0 || tp->max_sdu > ATM_MAX_AAL34_PDU) {
2129         PRINTD (DBG_QOS, "%sing max_sdu", tp->max_sdu ? "capp" : "default");
2130         tp->max_sdu = ATM_MAX_AAL34_PDU;
2131       }
2132       break;
2133     case aal5:
2134       if (tp->max_sdu == 0 || tp->max_sdu > max_frame_size) {
2135         PRINTD (DBG_QOS, "%sing max_sdu", tp->max_sdu ? "capp" : "default");
2136         tp->max_sdu = max_frame_size;
2137       }
2138       break;
2139   }
2140   return 0;
2141 }
2142
2143 /********** check pcr **********/
2144
2145 // something like this should be part of ATM Linux
2146 static int atm_pcr_check (struct atm_trafprm * tp, unsigned int pcr) {
2147   // we are assuming non-UBR, and non-special values of pcr
2148   if (tp->min_pcr == ATM_MAX_PCR)
2149     PRINTD (DBG_QOS, "luser gave min_pcr = ATM_MAX_PCR");
2150   else if (tp->min_pcr < 0)
2151     PRINTD (DBG_QOS, "luser gave negative min_pcr");
2152   else if (tp->min_pcr && tp->min_pcr > pcr)
2153     PRINTD (DBG_QOS, "pcr less than min_pcr");
2154   else
2155     // !! max_pcr = UNSPEC (0) is equivalent to max_pcr = MAX (-1)
2156     // easier to #define ATM_MAX_PCR 0 and have all rates unsigned?
2157     // [this would get rid of next two conditionals]
2158     if ((0) && tp->max_pcr == ATM_MAX_PCR)
2159       PRINTD (DBG_QOS, "luser gave max_pcr = ATM_MAX_PCR");
2160     else if ((tp->max_pcr != ATM_MAX_PCR) && tp->max_pcr < 0)
2161       PRINTD (DBG_QOS, "luser gave negative max_pcr");
2162     else if (tp->max_pcr && tp->max_pcr != ATM_MAX_PCR && tp->max_pcr < pcr)
2163       PRINTD (DBG_QOS, "pcr greater than max_pcr");
2164     else {
2165       // each limit unspecified or not violated
2166       PRINTD (DBG_QOS, "xBR(pcr) OK");
2167       return 0;
2168     }
2169   PRINTD (DBG_QOS, "pcr=%u, tp: min_pcr=%d, pcr=%d, max_pcr=%d",
2170           pcr, tp->min_pcr, tp->pcr, tp->max_pcr);
2171   return -EINVAL;
2172 }
2173
2174 /********** open VC **********/
2175
2176 static int hrz_open (struct atm_vcc * atm_vcc, short vpi, int vci) {
2177   int error;
2178   u16 channel;
2179   
2180   struct atm_qos * qos;
2181   struct atm_trafprm * txtp;
2182   struct atm_trafprm * rxtp;
2183   
2184   hrz_dev * dev = HRZ_DEV(atm_vcc->dev);
2185   hrz_vcc vcc;
2186   hrz_vcc * vccp; // allocated late
2187   PRINTD (DBG_FLOW|DBG_VCC, "hrz_open %x %x", vpi, vci);
2188   
2189 #ifdef ATM_VPI_UNSPEC
2190   // UNSPEC is deprecated, remove this code eventually
2191   if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC) {
2192     PRINTK (KERN_WARNING, "rejecting open with unspecified VPI/VCI (deprecated)");
2193     return -EINVAL;
2194   }
2195 #endif
2196   
2197   // deal with possibly wildcarded VCs
2198   error = atm_find_ci (atm_vcc, &vpi, &vci);
2199   if (error) {
2200     PRINTD (DBG_WARN|DBG_VCC, "atm_find_ci failed!");
2201     return error;
2202   }
2203   PRINTD (DBG_VCC, "atm_find_ci gives %x %x", vpi, vci);
2204   
2205   error = vpivci_to_channel (&channel, vpi, vci);
2206   if (error) {
2207     PRINTD (DBG_WARN|DBG_VCC, "VPI/VCI out of range: %hd/%d", vpi, vci);
2208     return error;
2209   }
2210   
2211   vcc.channel = channel;
2212   // max speed for the moment
2213   vcc.tx_rate = 0x0;
2214   
2215   qos = &atm_vcc->qos;
2216   
2217   // check AAL and remember it
2218   switch (qos->aal) {
2219     case ATM_AAL0:
2220       // we would if it were 48 bytes and not 52!
2221       PRINTD (DBG_QOS|DBG_VCC, "AAL0");
2222       vcc.aal = aal0;
2223       break;
2224     case ATM_AAL34:
2225       // we would if I knew how do the SAR!
2226       PRINTD (DBG_QOS|DBG_VCC, "AAL3/4");
2227       vcc.aal = aal34;
2228       break;
2229     case ATM_AAL5:
2230       PRINTD (DBG_QOS|DBG_VCC, "AAL5");
2231       vcc.aal = aal5;
2232       break;
2233     default:
2234       PRINTD (DBG_QOS|DBG_VCC, "Bad AAL!");
2235       return -EINVAL;
2236       break;
2237   }
2238   
2239   // TX traffic parameters
2240   
2241   // there are two, interrelated problems here: 1. the reservation of
2242   // PCR is not a binary choice, we are given bounds and/or a
2243   // desirable value; 2. the device is only capable of certain values,
2244   // most of which are not integers. It is almost certainly acceptable
2245   // to be off by a maximum of 1 to 10 cps.
2246   
2247   // Pragmatic choice: always store an integral PCR as that which has
2248   // been allocated, even if we allocate a little (or a lot) less,
2249   // after rounding. The actual allocation depends on what we can
2250   // manage with our rate selection algorithm. The rate selection
2251   // algorithm is given an integral PCR and a tolerance and told
2252   // whether it should round the value up or down if the tolerance is
2253   // exceeded; it returns: a) the actual rate selected (rounded up to
2254   // the nearest integer), b) a bit pattern to feed to the timer
2255   // register, and c) a failure value if no applicable rate exists.
2256   
2257   // Part of the job is done by atm_pcr_goal which gives us a PCR
2258   // specification which says: EITHER grab the maximum available PCR
2259   // (and perhaps a lower bound which we musn't pass), OR grab this
2260   // amount, rounding down if you have to (and perhaps a lower bound
2261   // which we musn't pass) OR grab this amount, rounding up if you
2262   // have to (and perhaps an upper bound which we musn't pass). If any
2263   // bounds ARE passed we fail. Note that rounding is only rounding to
2264   // match device limitations, we do not round down to satisfy
2265   // bandwidth availability even if this would not violate any given
2266   // lower bound.
2267   
2268   // Note: telephony = 64kb/s = 48 byte cell payload @ 500/3 cells/s
2269   // (say) so this is not even a binary fixpoint cell rate (but this
2270   // device can do it). To avoid this sort of hassle we use a
2271   // tolerance parameter (currently fixed at 10 cps).
2272   
2273   PRINTD (DBG_QOS, "TX:");
2274   
2275   txtp = &qos->txtp;
2276   
2277   // set up defaults for no traffic
2278   vcc.tx_rate = 0;
2279   // who knows what would actually happen if you try and send on this?
2280   vcc.tx_xbr_bits = IDLE_RATE_TYPE;
2281   vcc.tx_pcr_bits = CLOCK_DISABLE;
2282 #if 0
2283   vcc.tx_scr_bits = CLOCK_DISABLE;
2284   vcc.tx_bucket_bits = 0;
2285 #endif
2286   
2287   if (txtp->traffic_class != ATM_NONE) {
2288     error = check_max_sdu (vcc.aal, txtp, max_tx_size);
2289     if (error) {
2290       PRINTD (DBG_QOS, "TX max_sdu check failed");
2291       return error;
2292     }
2293     
2294     switch (txtp->traffic_class) {
2295       case ATM_UBR: {
2296         // we take "the PCR" as a rate-cap
2297         // not reserved
2298         vcc.tx_rate = 0;
2299         make_rate (dev, 1<<30, round_nearest, &vcc.tx_pcr_bits, 0);
2300         vcc.tx_xbr_bits = ABR_RATE_TYPE;
2301         break;
2302       }
2303 #if 0
2304       case ATM_ABR: {
2305         // reserve min, allow up to max
2306         vcc.tx_rate = 0; // ?
2307         make_rate (dev, 1<<30, round_nearest, &vcc.tx_pcr_bits, 0);
2308         vcc.tx_xbr_bits = ABR_RATE_TYPE;
2309         break;
2310       }
2311 #endif
2312       case ATM_CBR: {
2313         int pcr = atm_pcr_goal (txtp);
2314         rounding r;
2315         if (!pcr) {
2316           // down vs. up, remaining bandwidth vs. unlimited bandwidth!!
2317           // should really have: once someone gets unlimited bandwidth
2318           // that no more non-UBR channels can be opened until the
2319           // unlimited one closes?? For the moment, round_down means
2320           // greedy people actually get something and not nothing
2321           r = round_down;
2322           // slight race (no locking) here so we may get -EAGAIN
2323           // later; the greedy bastards would deserve it :)
2324           PRINTD (DBG_QOS, "snatching all remaining TX bandwidth");
2325           pcr = dev->tx_avail;
2326         } else if (pcr < 0) {
2327           r = round_down;
2328           pcr = -pcr;
2329         } else {
2330           r = round_up;
2331         }
2332         error = make_rate_with_tolerance (dev, pcr, r, 10,
2333                                           &vcc.tx_pcr_bits, &vcc.tx_rate);
2334         if (error) {
2335           PRINTD (DBG_QOS, "could not make rate from TX PCR");
2336           return error;
2337         }
2338         // not really clear what further checking is needed
2339         error = atm_pcr_check (txtp, vcc.tx_rate);
2340         if (error) {
2341           PRINTD (DBG_QOS, "TX PCR failed consistency check");
2342           return error;
2343         }
2344         vcc.tx_xbr_bits = CBR_RATE_TYPE;
2345         break;
2346       }
2347 #if 0
2348       case ATM_VBR: {
2349         int pcr = atm_pcr_goal (txtp);
2350         // int scr = atm_scr_goal (txtp);
2351         int scr = pcr/2; // just for fun
2352         unsigned int mbs = 60; // just for fun
2353         rounding pr;
2354         rounding sr;
2355         unsigned int bucket;
2356         if (!pcr) {
2357           pr = round_nearest;
2358           pcr = 1<<30;
2359         } else if (pcr < 0) {
2360           pr = round_down;
2361           pcr = -pcr;
2362         } else {
2363           pr = round_up;
2364         }
2365         error = make_rate_with_tolerance (dev, pcr, pr, 10,
2366                                           &vcc.tx_pcr_bits, 0);
2367         if (!scr) {
2368           // see comments for PCR with CBR above
2369           sr = round_down;
2370           // slight race (no locking) here so we may get -EAGAIN
2371           // later; the greedy bastards would deserve it :)
2372           PRINTD (DBG_QOS, "snatching all remaining TX bandwidth");
2373           scr = dev->tx_avail;
2374         } else if (scr < 0) {
2375           sr = round_down;
2376           scr = -scr;
2377         } else {
2378           sr = round_up;
2379         }
2380         error = make_rate_with_tolerance (dev, scr, sr, 10,
2381                                           &vcc.tx_scr_bits, &vcc.tx_rate);
2382         if (error) {
2383           PRINTD (DBG_QOS, "could not make rate from TX SCR");
2384           return error;
2385         }
2386         // not really clear what further checking is needed
2387         // error = atm_scr_check (txtp, vcc.tx_rate);
2388         if (error) {
2389           PRINTD (DBG_QOS, "TX SCR failed consistency check");
2390           return error;
2391         }
2392         // bucket calculations (from a piece of paper...) cell bucket
2393         // capacity must be largest integer smaller than m(p-s)/p + 1
2394         // where m = max burst size, p = pcr, s = scr
2395         bucket = mbs*(pcr-scr)/pcr;
2396         if (bucket*pcr != mbs*(pcr-scr))
2397           bucket += 1;
2398         if (bucket > BUCKET_MAX_SIZE) {
2399           PRINTD (DBG_QOS, "shrinking bucket from %u to %u",
2400                   bucket, BUCKET_MAX_SIZE);
2401           bucket = BUCKET_MAX_SIZE;
2402         }
2403         vcc.tx_xbr_bits = VBR_RATE_TYPE;
2404         vcc.tx_bucket_bits = bucket;
2405         break;
2406       }
2407 #endif
2408       default: {
2409         PRINTD (DBG_QOS, "unsupported TX traffic class");
2410         return -EINVAL;
2411         break;
2412       }
2413     }
2414   }
2415   
2416   // RX traffic parameters
2417   
2418   PRINTD (DBG_QOS, "RX:");
2419   
2420   rxtp = &qos->rxtp;
2421   
2422   // set up defaults for no traffic
2423   vcc.rx_rate = 0;
2424   
2425   if (rxtp->traffic_class != ATM_NONE) {
2426     error = check_max_sdu (vcc.aal, rxtp, max_rx_size);
2427     if (error) {
2428       PRINTD (DBG_QOS, "RX max_sdu check failed");
2429       return error;
2430     }
2431     switch (rxtp->traffic_class) {
2432       case ATM_UBR: {
2433         // not reserved
2434         break;
2435       }
2436 #if 0
2437       case ATM_ABR: {
2438         // reserve min
2439         vcc.rx_rate = 0; // ?
2440         break;
2441       }
2442 #endif
2443       case ATM_CBR: {
2444         int pcr = atm_pcr_goal (rxtp);
2445         if (!pcr) {
2446           // slight race (no locking) here so we may get -EAGAIN
2447           // later; the greedy bastards would deserve it :)
2448           PRINTD (DBG_QOS, "snatching all remaining RX bandwidth");
2449           pcr = dev->rx_avail;
2450         } else if (pcr < 0) {
2451           pcr = -pcr;
2452         }
2453         vcc.rx_rate = pcr;
2454         // not really clear what further checking is needed
2455         error = atm_pcr_check (rxtp, vcc.rx_rate);
2456         if (error) {
2457           PRINTD (DBG_QOS, "RX PCR failed consistency check");
2458           return error;
2459         }
2460         break;
2461       }
2462 #if 0
2463       case ATM_VBR: {
2464         // int scr = atm_scr_goal (rxtp);
2465         int scr = 1<<16; // just for fun
2466         if (!scr) {
2467           // slight race (no locking) here so we may get -EAGAIN
2468           // later; the greedy bastards would deserve it :)
2469           PRINTD (DBG_QOS, "snatching all remaining RX bandwidth");
2470           scr = dev->rx_avail;
2471         } else if (scr < 0) {
2472           scr = -scr;
2473         }
2474         vcc.rx_rate = scr;
2475         // not really clear what further checking is needed
2476         // error = atm_scr_check (rxtp, vcc.rx_rate);
2477         if (error) {
2478           PRINTD (DBG_QOS, "RX SCR failed consistency check");
2479           return error;
2480         }
2481         break;
2482       }
2483 #endif
2484       default: {
2485         PRINTD (DBG_QOS, "unsupported RX traffic class");
2486         return -EINVAL;
2487         break;
2488       }
2489     }
2490   }
2491   
2492   
2493   // late abort useful for diagnostics
2494   if (vcc.aal != aal5) {
2495     PRINTD (DBG_QOS, "AAL not supported");
2496     return -EINVAL;
2497   }
2498   
2499   // get space for our vcc stuff and copy parameters into it
2500   vccp = kmalloc (sizeof(hrz_vcc), GFP_KERNEL);
2501   if (!vccp) {
2502     PRINTK (KERN_ERR, "out of memory!");
2503     return -ENOMEM;
2504   }
2505   *vccp = vcc;
2506   
2507   // clear error and grab cell rate resource lock
2508   error = 0;
2509   spin_lock (&dev->rate_lock);
2510   
2511   if (vcc.tx_rate > dev->tx_avail) {
2512     PRINTD (DBG_QOS, "not enough TX PCR left");
2513     error = -EAGAIN;
2514   }
2515   
2516   if (vcc.rx_rate > dev->rx_avail) {
2517     PRINTD (DBG_QOS, "not enough RX PCR left");
2518     error = -EAGAIN;
2519   }
2520   
2521   if (!error) {
2522     // really consume cell rates
2523     dev->tx_avail -= vcc.tx_rate;
2524     dev->rx_avail -= vcc.rx_rate;
2525     PRINTD (DBG_QOS|DBG_VCC, "reserving %u TX PCR and %u RX PCR",
2526             vcc.tx_rate, vcc.rx_rate);
2527   }
2528   
2529   // release lock and exit on error
2530   spin_unlock (&dev->rate_lock);
2531   if (error) {
2532     PRINTD (DBG_QOS|DBG_VCC, "insufficient cell rate resources");
2533     kfree (vccp);
2534     return error;
2535   }
2536   
2537   // this is "immediately before allocating the connection identifier
2538   // in hardware" - so long as the next call does not fail :)
2539   set_bit(ATM_VF_ADDR,&atm_vcc->flags);
2540   
2541   // any errors here are very serious and should never occur
2542   
2543   if (rxtp->traffic_class != ATM_NONE) {
2544     if (dev->rxer[channel]) {
2545       PRINTD (DBG_ERR|DBG_VCC, "VC already open for RX");
2546       error = -EBUSY;
2547     }
2548     if (!error)
2549       error = hrz_open_rx (dev, channel);
2550     if (error) {
2551       kfree (vccp);
2552       return error;
2553     }
2554     // this link allows RX frames through
2555     dev->rxer[channel] = atm_vcc;
2556   }
2557   
2558   // success, set elements of atm_vcc
2559   atm_vcc->vpi = vpi;
2560   atm_vcc->vci = vci;
2561   atm_vcc->dev_data = (void *) vccp;
2562   
2563   // indicate readiness
2564   set_bit(ATM_VF_READY,&atm_vcc->flags);
2565   
2566   return 0;
2567 }
2568
2569 /********** close VC **********/
2570
2571 static void hrz_close (struct atm_vcc * atm_vcc) {
2572   hrz_dev * dev = HRZ_DEV(atm_vcc->dev);
2573   hrz_vcc * vcc = HRZ_VCC(atm_vcc);
2574   u16 channel = vcc->channel;
2575   PRINTD (DBG_VCC|DBG_FLOW, "hrz_close");
2576   
2577   // indicate unreadiness
2578   clear_bit(ATM_VF_READY,&atm_vcc->flags);
2579
2580   if (atm_vcc->qos.txtp.traffic_class != ATM_NONE) {
2581     unsigned int i;
2582     
2583     // let any TX on this channel that has started complete
2584     // no restart, just keep trying
2585     while (tx_hold (dev))
2586       ;
2587     // remove record of any tx_channel having been setup for this channel
2588     for (i = 0; i < TX_CHANS; ++i)
2589       if (dev->tx_channel_record[i] == channel) {
2590         dev->tx_channel_record[i] = -1;
2591         break;
2592       }
2593     if (dev->last_vc == channel)
2594       dev->tx_last = -1;
2595     tx_release (dev);
2596   }
2597
2598   if (atm_vcc->qos.rxtp.traffic_class != ATM_NONE) {
2599     // disable RXing - it tries quite hard
2600     hrz_close_rx (dev, channel);
2601     // forget the vcc - no more skbs will be pushed
2602     if (atm_vcc != dev->rxer[channel])
2603       PRINTK (KERN_ERR, "%s atm_vcc=%p rxer[channel]=%p",
2604               "arghhh! we're going to die!",
2605               atm_vcc, dev->rxer[channel]);
2606     dev->rxer[channel] = 0;
2607   }
2608   
2609   // atomically release our rate reservation
2610   spin_lock (&dev->rate_lock);
2611   PRINTD (DBG_QOS|DBG_VCC, "releasing %u TX PCR and %u RX PCR",
2612           vcc->tx_rate, vcc->rx_rate);
2613   dev->tx_avail += vcc->tx_rate;
2614   dev->rx_avail += vcc->rx_rate;
2615   spin_unlock (&dev->rate_lock);
2616   
2617   // free our structure
2618   kfree (vcc);
2619   // say the VPI/VCI is free again
2620   clear_bit(ATM_VF_ADDR,&atm_vcc->flags);
2621 }
2622
2623 #if 0
2624 static int hrz_getsockopt (struct atm_vcc * atm_vcc, int level, int optname,
2625                            void *optval, int optlen) {
2626   hrz_dev * dev = HRZ_DEV(atm_vcc->dev);
2627   PRINTD (DBG_FLOW|DBG_VCC, "hrz_getsockopt");
2628   switch (level) {
2629     case SOL_SOCKET:
2630       switch (optname) {
2631 //      case SO_BCTXOPT:
2632 //        break;
2633 //      case SO_BCRXOPT:
2634 //        break;
2635         default:
2636           return -ENOPROTOOPT;
2637           break;
2638       };
2639       break;
2640   }
2641   return -EINVAL;
2642 }
2643
2644 static int hrz_setsockopt (struct atm_vcc * atm_vcc, int level, int optname,
2645                            void *optval, int optlen) {
2646   hrz_dev * dev = HRZ_DEV(atm_vcc->dev);
2647   PRINTD (DBG_FLOW|DBG_VCC, "hrz_setsockopt");
2648   switch (level) {
2649     case SOL_SOCKET:
2650       switch (optname) {
2651 //      case SO_BCTXOPT:
2652 //        break;
2653 //      case SO_BCRXOPT:
2654 //        break;
2655         default:
2656           return -ENOPROTOOPT;
2657           break;
2658       };
2659       break;
2660   }
2661   return -EINVAL;
2662 }
2663 #endif
2664
2665 static int hrz_sg_send (struct atm_vcc * atm_vcc,
2666                         unsigned long start,
2667                         unsigned long size) {
2668   if (atm_vcc->qos.aal == ATM_AAL5) {
2669     PRINTD (DBG_FLOW|DBG_VCC, "hrz_sg_send: yes");
2670     return 1;
2671   } else {
2672     PRINTD (DBG_FLOW|DBG_VCC, "hrz_sg_send: no");
2673     return 0;
2674   }
2675 }
2676
2677 #if 0
2678 static int hrz_ioctl (struct atm_dev * atm_dev, unsigned int cmd, void *arg) {
2679   hrz_dev * dev = HRZ_DEV(atm_dev);
2680   PRINTD (DBG_FLOW, "hrz_ioctl");
2681   return -1;
2682 }
2683
2684 unsigned char hrz_phy_get (struct atm_dev * atm_dev, unsigned long addr) {
2685   hrz_dev * dev = HRZ_DEV(atm_dev);
2686   PRINTD (DBG_FLOW, "hrz_phy_get");
2687   return 0;
2688 }
2689
2690 static void hrz_phy_put (struct atm_dev * atm_dev, unsigned char value,
2691                          unsigned long addr) {
2692   hrz_dev * dev = HRZ_DEV(atm_dev);
2693   PRINTD (DBG_FLOW, "hrz_phy_put");
2694 }
2695
2696 static int hrz_change_qos (struct atm_vcc * atm_vcc, struct atm_qos *qos, int flgs) {
2697   hrz_dev * dev = HRZ_DEV(vcc->dev);
2698   PRINTD (DBG_FLOW, "hrz_change_qos");
2699   return -1;
2700 }
2701 #endif
2702
2703 /********** proc file contents **********/
2704
2705 static int hrz_proc_read (struct atm_dev * atm_dev, loff_t * pos, char * page) {
2706   hrz_dev * dev = HRZ_DEV(atm_dev);
2707   int left = *pos;
2708   PRINTD (DBG_FLOW, "hrz_proc_read");
2709   
2710   /* more diagnostics here? */
2711   
2712 #if 0
2713   if (!left--) {
2714     unsigned int count = sprintf (page, "vbr buckets:");
2715     unsigned int i;
2716     for (i = 0; i < TX_CHANS; ++i)
2717       count += sprintf (page, " %u/%u",
2718                         query_tx_channel_config (dev, i, BUCKET_FULLNESS_ACCESS),
2719                         query_tx_channel_config (dev, i, BUCKET_CAPACITY_ACCESS));
2720     count += sprintf (page+count, ".\n");
2721     return count;
2722   }
2723 #endif
2724   
2725   if (!left--)
2726     return sprintf (page,
2727                     "cells: TX %lu, RX %lu, HEC errors %lu, unassigned %lu.\n",
2728                     dev->tx_cell_count, dev->rx_cell_count,
2729                     dev->hec_error_count, dev->unassigned_cell_count);
2730   
2731   if (!left--)
2732     return sprintf (page,
2733                     "free cell buffers: TX %hu, RX %hu+%hu.\n",
2734                     rd_regw (dev, TX_FREE_BUFFER_COUNT_OFF),
2735                     rd_regw (dev, RX_FREE_BUFFER_COUNT_OFF),
2736                     dev->noof_spare_buffers);
2737   
2738   if (!left--)
2739     return sprintf (page,
2740                     "cps remaining: TX %u, RX %u\n",
2741                     dev->tx_avail, dev->rx_avail);
2742   
2743   return 0;
2744 }
2745
2746 static const struct atmdev_ops hrz_ops = {
2747   open:         hrz_open,
2748   close:        hrz_close,
2749   send:         hrz_send,
2750   sg_send:      hrz_sg_send,
2751   proc_read:    hrz_proc_read,
2752   owner:        THIS_MODULE,
2753 };
2754
2755 static int __init hrz_probe (void) {
2756   struct pci_dev * pci_dev;
2757   int devs;
2758   
2759   PRINTD (DBG_FLOW, "hrz_probe");
2760   
2761   devs = 0;
2762   pci_dev = NULL;
2763   while ((pci_dev = pci_find_device
2764           (PCI_VENDOR_ID_MADGE, PCI_DEVICE_ID_MADGE_HORIZON, pci_dev)
2765           )) {
2766     hrz_dev * dev;
2767     
2768     // adapter slot free, read resources from PCI configuration space
2769     u32 iobase = pci_resource_start (pci_dev, 0);
2770     u32 * membase = bus_to_virt (pci_resource_start (pci_dev, 1));
2771     u8 irq = pci_dev->irq;
2772     
2773     /* XXX DEV_LABEL is a guess */
2774     if (!request_region (iobase, HRZ_IO_EXTENT, DEV_LABEL))
2775           continue;
2776
2777     if (pci_enable_device (pci_dev))
2778       continue;
2779     
2780     dev = kmalloc (sizeof(hrz_dev), GFP_KERNEL);
2781     if (!dev) {
2782       // perhaps we should be nice: deregister all adapters and abort?
2783       PRINTD (DBG_ERR, "out of memory");
2784       continue;
2785     }
2786     
2787     memset (dev, 0, sizeof(hrz_dev));
2788     
2789     // grab IRQ and install handler - move this someplace more sensible
2790     if (request_irq (irq,
2791                      interrupt_handler,
2792                      SA_SHIRQ, /* irqflags guess */
2793                      DEV_LABEL, /* name guess */
2794                      dev)) {
2795       PRINTD (DBG_WARN, "request IRQ failed!");
2796       // free_irq is at "endif"
2797     } else {
2798       
2799       PRINTD (DBG_INFO, "found Madge ATM adapter (hrz) at: IO %x, IRQ %u, MEM %p",
2800               iobase, irq, membase);
2801       
2802       dev->atm_dev = atm_dev_register (DEV_LABEL, &hrz_ops, -1, NULL);
2803       if (!(dev->atm_dev)) {
2804         PRINTD (DBG_ERR, "failed to register Madge ATM adapter");
2805       } else {
2806         unsigned char lat;
2807         
2808         PRINTD (DBG_INFO, "registered Madge ATM adapter (no. %d) (%p) at %p",
2809                 dev->atm_dev->number, dev, dev->atm_dev);
2810         dev->atm_dev->dev_data = (void *) dev;
2811         dev->pci_dev = pci_dev; 
2812         
2813         // enable bus master accesses
2814         pci_set_master (pci_dev);
2815         
2816         // frobnicate latency (upwards, usually)
2817         pci_read_config_byte (pci_dev, PCI_LATENCY_TIMER, &lat);
2818         if (pci_lat) {
2819           PRINTD (DBG_INFO, "%s PCI latency timer from %hu to %hu",
2820                   "changing", lat, pci_lat);
2821           pci_write_config_byte (pci_dev, PCI_LATENCY_TIMER, pci_lat);
2822         } else if (lat < MIN_PCI_LATENCY) {
2823           PRINTK (KERN_INFO, "%s PCI latency timer from %hu to %hu",
2824                   "increasing", lat, MIN_PCI_LATENCY);
2825           pci_write_config_byte (pci_dev, PCI_LATENCY_TIMER, MIN_PCI_LATENCY);
2826         }
2827         
2828         dev->iobase = iobase;
2829         dev->irq = irq; 
2830         dev->membase = membase; 
2831         
2832         dev->rx_q_entry = dev->rx_q_reset = &memmap->rx_q_entries[0];
2833         dev->rx_q_wrap  = &memmap->rx_q_entries[RX_CHANS-1];
2834         
2835         // these next three are performance hacks
2836         dev->last_vc = -1;
2837         dev->tx_last = -1;
2838         dev->tx_idle = 0;
2839         
2840         dev->tx_regions = 0;
2841         dev->tx_bytes = 0;
2842         dev->tx_skb = 0;
2843         dev->tx_iovec = 0;
2844         
2845         dev->tx_cell_count = 0;
2846         dev->rx_cell_count = 0;
2847         dev->hec_error_count = 0;
2848         dev->unassigned_cell_count = 0;
2849         
2850         dev->noof_spare_buffers = 0;
2851         
2852         {
2853           unsigned int i;
2854           for (i = 0; i < TX_CHANS; ++i)
2855             dev->tx_channel_record[i] = -1;
2856         }
2857         
2858         dev->flags = 0;
2859         
2860         // Allocate cell rates and remember ASIC version
2861         // Fibre: ATM_OC3_PCR = 1555200000/8/270*260/53 - 29/53
2862         // Copper: (WRONG) we want 6 into the above, close to 25Mb/s
2863         // Copper: (plagarise!) 25600000/8/270*260/53 - n/53
2864         
2865         if (hrz_init (dev)) {
2866           // to be really pedantic, this should be ATM_OC3c_PCR
2867           dev->tx_avail = ATM_OC3_PCR;
2868           dev->rx_avail = ATM_OC3_PCR;
2869           set_bit (ultra, &dev->flags); // NOT "|= ultra" !
2870         } else {
2871           dev->tx_avail = ((25600000/8)*26)/(27*53);
2872           dev->rx_avail = ((25600000/8)*26)/(27*53);
2873           PRINTD (DBG_WARN, "Buggy ASIC: no TX bus-mastering.");
2874         }
2875         
2876         // rate changes spinlock
2877         spin_lock_init (&dev->rate_lock);
2878         
2879         // on-board memory access spinlock; we want atomic reads and
2880         // writes to adapter memory (handles IRQ and SMP)
2881         spin_lock_init (&dev->mem_lock);
2882         
2883 #if LINUX_VERSION_CODE >= 0x20303
2884         init_waitqueue_head (&dev->tx_queue);
2885 #else
2886         dev->tx_queue = 0;
2887 #endif
2888         
2889         // vpi in 0..4, vci in 6..10
2890         dev->atm_dev->ci_range.vpi_bits = vpi_bits;
2891         dev->atm_dev->ci_range.vci_bits = 10-vpi_bits;
2892         
2893         // update count and linked list
2894         ++devs;
2895         dev->prev = hrz_devs;
2896         hrz_devs = dev;
2897         // success
2898         continue;
2899         
2900         /* not currently reached */
2901         atm_dev_deregister (dev->atm_dev);
2902       } /* atm_dev_register */
2903       free_irq (irq, dev);
2904         
2905     } /* request_irq */
2906     kfree (dev);
2907     release_region(iobase, HRZ_IO_EXTENT);
2908   } /* kmalloc and while */
2909   return devs;
2910 }
2911
2912 static void __init hrz_check_args (void) {
2913 #ifdef DEBUG_HORIZON
2914   PRINTK (KERN_NOTICE, "debug bitmap is %hx", debug &= DBG_MASK);
2915 #else
2916   if (debug)
2917     PRINTK (KERN_NOTICE, "no debug support in this image");
2918 #endif
2919   
2920   if (vpi_bits > HRZ_MAX_VPI)
2921     PRINTK (KERN_ERR, "vpi_bits has been limited to %hu",
2922             vpi_bits = HRZ_MAX_VPI);
2923   
2924   if (max_tx_size < 0 || max_tx_size > TX_AAL5_LIMIT)
2925     PRINTK (KERN_NOTICE, "max_tx_size has been limited to %hu",
2926             max_tx_size = TX_AAL5_LIMIT);
2927   
2928   if (max_rx_size < 0 || max_rx_size > RX_AAL5_LIMIT)
2929     PRINTK (KERN_NOTICE, "max_rx_size has been limited to %hu",
2930             max_rx_size = RX_AAL5_LIMIT);
2931   
2932   return;
2933 }
2934
2935 #ifdef MODULE
2936 EXPORT_NO_SYMBOLS;
2937
2938 MODULE_AUTHOR(maintainer_string);
2939 MODULE_DESCRIPTION(description_string);
2940 MODULE_LICENSE("GPL");
2941 MODULE_PARM(debug, "h");
2942 MODULE_PARM(vpi_bits, "h");
2943 MODULE_PARM(max_tx_size, "i");
2944 MODULE_PARM(max_rx_size, "i");
2945 MODULE_PARM(pci_lat, "b");
2946 MODULE_PARM_DESC(debug, "debug bitmap, see .h file");
2947 MODULE_PARM_DESC(vpi_bits, "number of bits (0..4) to allocate to VPIs");
2948 MODULE_PARM_DESC(max_tx_size, "maximum size of TX AAL5 frames");
2949 MODULE_PARM_DESC(max_rx_size, "maximum size of RX AAL5 frames");
2950 MODULE_PARM_DESC(pci_lat, "PCI latency in bus cycles");
2951
2952 /********** module entry **********/
2953
2954 int init_module (void) {
2955   int devs;
2956   
2957   // sanity check - cast is needed since printk does not support %Zu
2958   if (sizeof(struct MEMMAP) != 128*1024/4) {
2959     PRINTK (KERN_ERR, "Fix struct MEMMAP (is %lu fakewords).",
2960             (unsigned long) sizeof(struct MEMMAP));
2961     return -ENOMEM;
2962   }
2963   
2964   show_version();
2965   
2966   // check arguments
2967   hrz_check_args();
2968   
2969   // get the juice
2970   devs = hrz_probe();
2971   
2972   if (devs) {
2973     init_timer (&housekeeping);
2974     housekeeping.function = do_housekeeping;
2975     // paranoia
2976     housekeeping.data = 1;
2977     set_timer (&housekeeping, 0);
2978   } else {
2979     PRINTK (KERN_ERR, "no (usable) adapters found");
2980   }
2981   
2982   return devs ? 0 : -ENODEV;
2983 }
2984
2985 /********** module exit **********/
2986
2987 void cleanup_module (void) {
2988   hrz_dev * dev;
2989   PRINTD (DBG_FLOW, "cleanup_module");
2990   
2991   // paranoia
2992   housekeeping.data = 0;
2993   del_timer (&housekeeping);
2994   
2995   while (hrz_devs) {
2996     dev = hrz_devs;
2997     hrz_devs = dev->prev;
2998     
2999     PRINTD (DBG_INFO, "closing %p (atm_dev = %p)", dev, dev->atm_dev);
3000     hrz_reset (dev);
3001     atm_dev_deregister (dev->atm_dev);
3002     free_irq (dev->irq, dev);
3003     release_region (dev->iobase, HRZ_IO_EXTENT);
3004     kfree (dev);
3005   }
3006   
3007   return;
3008 }
3009
3010 #else
3011
3012 /********** monolithic entry **********/
3013
3014 int __init hrz_detect (void) {
3015   int devs;
3016   
3017   // sanity check - cast is needed since printk does not support %Zu
3018   if (sizeof(struct MEMMAP) != 128*1024/4) {
3019     PRINTK (KERN_ERR, "Fix struct MEMMAP (is %lu fakewords).",
3020             (unsigned long) sizeof(struct MEMMAP));
3021     return 0;
3022   }
3023   
3024   show_version();
3025   
3026   // what about command line arguments?
3027   // check arguments
3028   hrz_check_args();
3029   
3030   // get the juice
3031   devs = hrz_probe();
3032   
3033   if (devs) {
3034     init_timer (&housekeeping);
3035     housekeeping.function = do_housekeeping;
3036     // paranoia
3037     housekeeping.data = 1;
3038     set_timer (&housekeeping, 0);
3039   } else {
3040     PRINTK (KERN_ERR, "no (usable) adapters found");
3041   }
3042
3043   return devs;
3044 }
3045
3046 #endif