[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re:[rtl] How to map the memory into the users' memory space
In the file arch/i386/mm/ioremap.c it states that anything over 1 MB is
too high memory and needs to be remapped to the 640k-1MB kernel I/O
space. I have attached the files for the device I mentioned. It is a
dual ported memory device that uses ioremap, so it should be a good
example for you. The file cycx_drv.c is the kernel mode driver that
remaps the dual ported memory using ioremap() into the kernel I/O
space. The cycx_main.c file shows how to request and release a memory
region.
I hope this helps.
- Kal.
/*
* cyclomx.h Cyclom 2X WAN Link Driver.
* User-level API definitions.
*
* Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
*
* Copyright: (c) 1998-2000 Arnaldo Carvalho de Melo
*
* Based on wanpipe.h by Gene Kozin <genek@compuserve.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
* ============================================================================
* 2000/01/21 acme rename cyclomx_open to cyclomx_mod_inc_use_count
* and cyclomx_close to cyclomx_mod_dec_use_count
* 1999/05/19 acme wait_queue_head_t wait_stats(support for 2.3.*)
* 1999/01/03 acme judicious use of data types
* 1998/12/27 acme cleanup: PACKED not needed
* 1998/08/08 acme Version 0.0.1
*/
#ifndef _CYCLOMX_H
#define _CYCLOMX_H
#include <linux/config.h>
#include <linux/wanrouter.h>
#include <linux/spinlock.h>
#ifdef __KERNEL__
/* Kernel Interface */
#include <linux/cycx_drv.h> /* Cyclom 2X support module API definitions */
#include <linux/cycx_cfm.h> /* Cyclom 2X firmware module definitions */
#ifdef CONFIG_CYCLOMX_X25
#include <linux/cycx_x25.h>
#endif
#ifndef min
#define min(a,b) (((a)<(b))?(a):(b))
#endif
#ifndef max
#define max(a,b) (((a)>(b))?(a):(b))
#endif
#define is_digit(ch) (((ch)>=(unsigned)'0'&&(ch)<=(unsigned)'9')?1:0)
/* Adapter Data Space.
* This structure is needed because we handle multiple cards, otherwise
* static data would do it.
*/
typedef struct cycx {
char devname[WAN_DRVNAME_SZ+1]; /* card name */
cycxhw_t hw; /* hardware configuration */
wan_device_t wandev; /* WAN device data space */
u32 open_cnt; /* number of open interfaces */
u32 state_tick; /* link state timestamp */
spinlock_t lock;
char in_isr; /* interrupt-in-service flag */
char buff_int_mode_unbusy; /* flag for carrying out dev_tint */
#if (LINUX_VERSION_CODE >= 0x20300)
wait_queue_head_t wait_stats; /* to wait for the STATS indication */
#else
struct wait_queue* wait_stats; /* to wait for the STATS indication */
#endif
u32 mbox; /* -> mailbox */
void (*isr)(struct cycx* card); /* interrupt service routine */
int (*exec)(struct cycx* card, void* u_cmd, void* u_data);
union {
#ifdef CONFIG_CYCLOMX_X25
struct { /* X.25 specific data */
u32 lo_pvc;
u32 hi_pvc;
u32 lo_svc;
u32 hi_svc;
TX25Stats stats;
spinlock_t lock;
u32 connection_keys;
} x;
#endif
} u;
} cycx_t;
/* Public Functions */
void cyclomx_mod_inc_use_count (cycx_t *card); /* cycx_main.c */
void cyclomx_mod_dec_use_count (cycx_t *card); /* cycx_main.c */
void cyclomx_set_state (cycx_t *card, int state); /* cycx_main.c */
#ifdef CONFIG_CYCLOMX_X25
int cyx_init (cycx_t *card, wandev_conf_t *conf); /* cycx_x25.c */
#endif
#endif /* __KERNEL__ */
#endif /* _CYCLOMX_H */
/*
* cycx_cfm.h Cyclom 2X WAN Link Driver.
* Definitions for the Cyclom 2X Firmware Module (CFM).
*
* Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
*
* Copyright: (c) 1998-2000 Arnaldo Carvalho de Melo
*
* Based on sdlasfm.h by Gene Kozin <74604.152@compuserve.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
* ============================================================================
* 1998/08/08 acme Initial version.
*/
#ifndef _CYCX_CFM_H
#define _CYCX_CFM_H
/* Defines */
#define CFM_VERSION 2
#define CFM_SIGNATURE "CFM - Cyclades CYCX Firmware Module"
/* min/max */
#define CFM_IMAGE_SIZE 0x20000 /* max size of CYCX code image file */
#define CFM_DESCR_LEN 256 /* max length of description string */
#define CFM_MAX_CYCX 1 /* max number of compatible adapters */
#define CFM_LOAD_BUFSZ 0x400 /* buffer size for reset code (buffer_load) */
/* Firmware Commands */
#define GEN_POWER_ON 0x1280
#define GEN_SET_SEG 0x1401 /* boot segment setting. */
#define GEN_BOOT_DAT 0x1402 /* boot data. */
#define GEN_START 0x1403 /* board start. */
#define GEN_DEFPAR 0x1404 /* buffer length for boot. */
/* Adapter Types */
#define CYCX_2X 2
/* for now only the 2X is supported, no plans to support 8X or 16X */
#define CYCX_8X 8
#define CYCX_16X 16
#define CFID_X25_2X 5200
/* Data Types */
typedef struct cfm_info /* firmware module information */
{
unsigned short codeid; /* firmware ID */
unsigned short version; /* firmware version number */
unsigned short adapter[CFM_MAX_CYCX]; /* compatible adapter types */
unsigned long memsize; /* minimum memory size */
unsigned short reserved[2]; /* reserved */
unsigned short startoffs; /* entry point offset */
unsigned short winoffs; /* dual-port memory window offset */
unsigned short codeoffs; /* code load offset */
unsigned long codesize; /* code size */
unsigned short dataoffs; /* configuration data load offset */
unsigned long datasize; /* configuration data size */
} cfm_info_t;
typedef struct cfm /* CYCX firmware file structure */
{
char signature[80]; /* CFM file signature */
unsigned short version; /* file format version */
unsigned short checksum; /* info + image */
unsigned short reserved[6]; /* reserved */
char descr[CFM_DESCR_LEN]; /* description string */
cfm_info_t info; /* firmware module info */
unsigned char image[1]; /* code image (variable size) */
} cfm_t;
typedef struct cycx_header_s {
unsigned long reset_size;
unsigned long data_size;
unsigned long code_size;
} cycx_header_t;
#endif /* _CYCX_CFM_H */
/*
* cycx_drv.h CYCX Support Module. Kernel API Definitions.
*
* Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
*
* Copyright: (c) 1998-2000 Arnaldo Carvalho de Melo
*
* Based on sdladrv.h by Gene Kozin <genek@compuserve.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
* ============================================================================
* 1999/10/23 acme cycxhw_t cleanup
* 1999/01/03 acme more judicious use of data types...
* uclong, ucchar, etc deleted, the u8, u16, u32
* types are the portable way to go.
* 1999/01/03 acme judicious use of data types... u16, u32, etc
* 1998/12/26 acme FIXED_BUFFERS, CONF_OFFSET,
* removal of cy_read{bwl}
* 1998/08/08 acme Initial version.
*/
#ifndef _CYCX_DRV_H
#define _CYCX_DRV_H
#define CYCX_WINDOWSIZE 0x4000 /* default dual-port memory window size */
#define GEN_CYCX_INTR 0x02
#define RST_ENABLE 0x04
#define START_CPU 0x06
#define RST_DISABLE 0x08
#define FIXED_BUFFERS 0x08
#define TEST_PATTERN 0xaa55
#define CMD_OFFSET 0x20
#define CONF_OFFSET 0x0380
#define RESET_OFFSET 0x3c00 /* For reset file load */
#define DATA_OFFSET 0x0100 /* For code and data files load */
#define START_OFFSET 0x3ff0 /* 80186 starts here */
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
/* Data Structures */
/* Adapter hardware configuration. Pointer to this structure is passed to all
* APIs. */
typedef struct cycxhw {
u32 fwid; /* firmware ID */
int irq; /* interrupt request level */
u32 dpmbase; /* dual-port memory base */
u32 dpmsize; /* dual-port memory size */
u32 reserved[5];
} cycxhw_t;
/* Function Prototypes */
extern int cycx_setup (cycxhw_t *hw, void *sfm, u32 len);
extern int cycx_down (cycxhw_t *hw);
extern int cycx_peek (cycxhw_t *hw, u32 addr, void *buf, u32 len);
extern int cycx_poke (cycxhw_t *hw, u32 addr, void *buf, u32 len);
extern int cycx_exec (u32 addr);
extern void cycx_inten (cycxhw_t *hw);
extern void cycx_intr (cycxhw_t *hw);
#endif /* _CYCX_DRV_H */
/*
* cycx_x25.h Cyclom X.25 firmware API definitions.
*
* Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
*
* Copyright: (c) 1998-2000 Arnaldo Carvalho de Melo
*
* Based on sdla_x25.h by Gene Kozin <74604.152@compuserve.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
* ============================================================================
* 1999/01/03 acme judicious use of data types
* 1999/01/02 acme #define X25_ACK_N3 0x4411
* 1998/12/28 acme cleanup: lot'o'things removed
* commands listed,
* TX25Cmd & TX25Config structs
* typedef'ed
*/
#ifndef _CYCX_X25_H
#define _CYCX_X25_H
#ifndef PACKED
#define PACKED __attribute__((packed))
#endif
/* X.25 shared memory layout. */
#define X25_MBOX_OFFS 0x300 /* general mailbox block */
#define X25_RXMBOX_OFFS 0x340 /* receive mailbox */
/* Data Structures */
/* X.25 Command Block. */
typedef struct X25Cmd
{
u16 command PACKED;
u16 link PACKED; /* values: 0 or 1 */
u16 len PACKED; /* values: 0 thru 0x205 (517) */
u32 buf PACKED;
} TX25Cmd;
/* Defines for the 'command' field. */
#define X25_CONNECT_REQUEST 0x4401
#define X25_CONNECT_RESPONSE 0x4402
#define X25_DISCONNECT_REQUEST 0x4403
#define X25_DISCONNECT_RESPONSE 0x4404
#define X25_DATA_REQUEST 0x4405
#define X25_ACK_TO_VC 0x4406
#define X25_INTERRUPT_RESPONSE 0x4407
#define X25_CONFIG 0x4408
#define X25_CONNECT_INDICATION 0x4409
#define X25_CONNECT_CONFIRM 0x440A
#define X25_DISCONNECT_INDICATION 0x440B
#define X25_DISCONNECT_CONFIRM 0x440C
#define X25_DATA_INDICATION 0x440E
#define X25_INTERRUPT_INDICATION 0x440F
#define X25_ACK_FROM_VC 0x4410
#define X25_ACK_N3 0x4411
#define X25_CONNECT_COLLISION 0x4413
#define X25_N3WIN 0x4414
#define X25_LINE_ON 0x4415
#define X25_LINE_OFF 0x4416
#define X25_RESET_REQUEST 0x4417
#define X25_LOG 0x4500
#define X25_STATISTIC 0x4600
#define X25_TRACE 0x4700
#define X25_N2TRACEXC 0x4702
#define X25_N3TRACEXC 0x4703
typedef struct X25Config {
u8 link PACKED; /* link number */
u8 speed PACKED; /* line speed */
u8 clock PACKED; /* internal/external */
u8 n2 PACKED; /* # of level 2 retransm.(values: 1 thru FF) */
u8 n2win PACKED; /* level 2 window (values: 1 thru 7) */
u8 n3win PACKED; /* level 3 window (values: 1 thru 7) */
u8 nvc PACKED; /* # of logical channels (values: 1 thru 64) */
u8 pktlen PACKED; /* level 3 packet lenght - log base 2 of size */
u8 locaddr PACKED; /* my address */
u8 remaddr PACKED; /* remote address */
u16 t1 PACKED; /* time, in seconds */
u16 t2 PACKED; /* time, in seconds */
u8 t21 PACKED; /* time, in seconds */
u8 npvc PACKED; /* # of permanent virt. circuits (1 thru nvc) */
u8 t23 PACKED; /* time, in seconds */
u8 flags PACKED; /* see dosx25.doc, in portuguese, for details */
} TX25Config;
typedef struct X25Stats {
u16 rx_crc_errors PACKED;
u16 rx_over_errors PACKED;
u16 n2_tx_frames PACKED;
u16 n2_rx_frames PACKED;
u16 tx_timeouts PACKED;
u16 rx_timeouts PACKED;
u16 n3_tx_packets PACKED;
u16 n3_rx_packets PACKED;
u16 tx_aborts PACKED;
u16 rx_aborts PACKED;
} TX25Stats;
#endif /* _CYCX_X25_H */
/*
* cycx_drv.c Cyclom 2X Support Module.
*
* This module is a library of common hardware specific
* functions used by the Cyclades Cyclom 2X sync card.
*
* Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
*
* Copyright: (c) 1998-2000 Arnaldo Carvalho de Melo
*
* Based on sdladrv.c by Gene Kozin <genek@compuserve.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
* ============================================================================
* 1999/11/11 acme set_current_state(TASK_INTERRUPTIBLE), code
* cleanup
* 1999/11/08 acme init_cyc2x deleted, doing nothing
* 1999/11/06 acme back to read[bw], write[bw] and memcpy_to and
* fromio to use dpmbase ioremaped
* 1999/10/26 acme use isa_read[bw], isa_write[bw] & isa_memcpy_to
* & fromio
* 1999/10/23 acme cleanup to only supports cyclom2x: all the other
* boards are no longer manufactured by cyclades,
* if someone wants to support them... be my guest!
* 1999/05/28 acme cycx_intack & cycx_intde gone for good
* 1999/05/18 acme lots of unlogged work, submitting to Linus...
* 1999/01/03 acme more judicious use of data types
* 1999/01/03 acme judicious use of data types :>
* cycx_inten trying to reset pending interrupts
* from cyclom 2x - I think this isn't the way to
* go, but for now...
* 1999/01/02 acme cycx_intack ok, I think there's nothing to do
* to ack an int in cycx_drv.c, only handle it in
* cyx_isr (or in the other protocols: cyp_isr,
* cyf_isr, when they get implemented.
* Dec 31, 1998 acme cycx_data_boot & cycx_code_boot fixed, crossing
* fingers to see x25_configure in cycx_x25.c
* work... :)
* Dec 26, 1998 acme load implementation fixed, seems to work! :)
* cycx_2x_dpmbase_options with all the possible
* DPM addresses (20).
* cycx_intr implemented (test this!)
* general code cleanup
* Dec 8, 1998 Ivan Passos Cyclom-2X firmware load implementation.
* Aug 8, 1998 acme Initial version.
*/
#ifdef MODULE
#ifdef MODVERSIONS
#include <linux/modversions.h>
#endif
#include <linux/module.h>
#else
#define EXPORT_SYMBOL(function)
#endif
#include <linux/kernel.h> /* printk(), and other useful stuff */
#include <linux/stddef.h> /* offsetof(), etc. */
#include <linux/errno.h> /* return codes */
#include <linux/sched.h> /* for jiffies, HZ, etc. */
#include <linux/cycx_drv.h> /* API definitions */
#include <linux/cycx_cfm.h> /* CYCX firmware module definitions */
#include <linux/delay.h> /* udelay */
#include <asm/io.h> /* read[wl], write[wl], ioremap, iounmap */
#define MOD_VERSION 0
#define MOD_RELEASE 5
#ifdef MODULE
MODULE_AUTHOR("Arnaldo Carvalho de Melo");
MODULE_DESCRIPTION("Cyclom 2x Sync Card Driver");
#endif
/* Function Prototypes */
/* Module entry points. These are called by the OS and must be public. */
int init_module(void);
void cleanup_module(void);
/* Hardware-specific functions */
static int load_cyc2x(cycxhw_t *hw, cfm_t *cfm, u32 len);
static void cycx_bootcfg(cycxhw_t *hw);
static int reset_cyc2x(u32 addr);
static int detect_cyc2x(u32 addr);
/* Miscellaneous functions */
static void delay_cycx(int sec);
static int get_option_index(u32 *optlist, u32 optval);
static u16 checksum(u8 *buf, u32 len);
#define wait_cyc(addr) cycx_exec(addr + CMD_OFFSET)
#define cyc2x_readb(b) readb(b)
#define cyc2x_readw(b) readw(b)
#define cyc2x_writeb(b, addr) writeb(b, addr)
#define cyc2x_writew(w, addr) writew(w, addr)
#define cyc2x_memcpy_toio(addr, buf, len) memcpy_toio((addr), buf, len)
#define cyc2x_memcpy_fromio(buf, addr, len) memcpy_fromio(buf, (addr), len)
/* Global Data */
/* private data */
static char modname[] = "cycx_drv";
static char fullname[] = "Cyclom 2X Support Module";
static char copyright[] = "(c) 1998-2000 Arnaldo Carvalho de Melo "
"<acme@conectiva.com.br>";
/* Hardware configuration options.
* These are arrays of configuration options used by verification routines.
* The first element of each array is its size (i.e. number of options).
*/
static u32 cyc2x_dpmbase_options[] =
{
20,
0xA0000, 0xA4000, 0xA8000, 0xAC000, 0xB0000, 0xB4000, 0xB8000,
0xBC000, 0xC0000, 0xC4000, 0xC8000, 0xCC000, 0xD0000, 0xD4000,
0xD8000, 0xDC000, 0xE0000, 0xE4000, 0xE8000, 0xEC000
};
static u32 cycx_2x_irq_options[] = { 7, 3, 5, 9, 10, 11, 12, 15 };
/* Kernel Loadable Module Entry Points */
/* Module 'insert' entry point.
* o print announcement
* o initialize static data
*
* Return: 0 Ok
* < 0 error.
* Context: process */
#ifdef MODULE
int init_module(void)
{
printk(KERN_INFO "%s v%u.%u %s\n", fullname, MOD_VERSION, MOD_RELEASE,
copyright);
return 0;
}
/* Module 'remove' entry point.
* o release all remaining system resources */
void cleanup_module(void)
{
}
#endif
/* Kernel APIs */
/* Set up adapter.
* o detect adapter type
* o verify hardware configuration options
* o check for hardware conflicts
* o set up adapter shared memory
* o test adapter memory
* o load firmware
* Return: 0 ok.
* < 0 error */
EXPORT_SYMBOL(cycx_setup);
int cycx_setup(cycxhw_t *hw, void *cfm, u32 len)
{
unsigned long dpmbase = hw->dpmbase;
int err;
/* Verify IRQ configuration options */
if (!get_option_index(cycx_2x_irq_options, hw->irq)) {
printk(KERN_ERR "%s: IRQ %d is illegal!\n", modname, hw->irq);
return -EINVAL;
}
/* Setup adapter dual-port memory window and test memory */
if (!hw->dpmbase) {
printk(KERN_ERR "%s: you must specify the dpm address!\n",
modname);
return -EINVAL;
} else if (!get_option_index(cyc2x_dpmbase_options, hw->dpmbase)) {
printk(KERN_ERR "%s: memory address 0x%lX is illegal!\n",
modname, dpmbase);
return -EINVAL;
}
hw->dpmbase = (u32)ioremap(dpmbase, CYCX_WINDOWSIZE);
hw->dpmsize = CYCX_WINDOWSIZE;
if (!detect_cyc2x(hw->dpmbase)) {
printk(KERN_ERR "%s: adapter Cyclom 2X not found at "
"address 0x%lX!\n", modname, dpmbase);
return -EINVAL;
}
printk(KERN_INFO "%s: found Cyclom 2X card at address 0x%lX.\n",
modname, dpmbase);
/* Load firmware. If loader fails then shut down adapter */
err = load_cyc2x(hw, cfm, len);
if (err)
cycx_down(hw); /* shutdown adapter */
return err;
}
EXPORT_SYMBOL(cycx_down);
int cycx_down(cycxhw_t *hw)
{
iounmap((u32 *)hw->dpmbase);
return 0;
}
/* Enable interrupt generation. */
EXPORT_SYMBOL(cycx_inten);
void cycx_inten(cycxhw_t *hw)
{
cyc2x_writeb(0, hw->dpmbase);
}
/* Generate an interrupt to adapter's CPU. */
EXPORT_SYMBOL(cycx_intr);
void cycx_intr(cycxhw_t *hw)
{
cyc2x_writew(0, hw->dpmbase + GEN_CYCX_INTR);
}
/* Execute Adapter Command.
* o Set exec flag.
* o Busy-wait until flag is reset. */
EXPORT_SYMBOL(cycx_exec);
int cycx_exec(u32 addr)
{
u16 i = 0;
/* wait till addr content is zeroed */
while (cyc2x_readw(addr)) {
udelay(1000);
if (++i > 50)
return -1;
}
return 0;
}
/* Read absolute adapter memory.
* Transfer data from adapter's memory to data buffer. */
EXPORT_SYMBOL(cycx_peek);
int cycx_peek(cycxhw_t *hw, u32 addr, void *buf, u32 len)
{
if (len == 1)
*(u8*)buf = cyc2x_readb(hw->dpmbase + addr);
else
cyc2x_memcpy_fromio(buf, hw->dpmbase + addr, len);
return 0;
}
/* Write Absolute Adapter Memory.
* Transfer data from data buffer to adapter's memory. */
EXPORT_SYMBOL(cycx_poke);
int cycx_poke(cycxhw_t *hw, u32 addr, void *buf, u32 len)
{
if (len == 1)
cyc2x_writeb(*(u8*)buf, hw->dpmbase + addr);
else
cyc2x_memcpy_toio(hw->dpmbase + addr, buf, len);
return 0;
}
/* Hardware-Specific Functions */
/* Load Aux Routines */
/* Reset board hardware.
return 1 if memory exists at addr and 0 if not. */
static int memory_exists(u32 addr)
{
int tries = 0;
for (; tries < 3 ; tries++) {
cyc2x_writew(TEST_PATTERN, addr + 0x10);
if (cyc2x_readw(addr + 0x10) == TEST_PATTERN)
if (cyc2x_readw(addr + 0x10) == TEST_PATTERN)
return 1;
delay_cycx(1);
}
return 0;
}
/* Load reset code. */
static void reset_load(u32 addr, u8 *buffer, u32 cnt)
{
u32 pt_code = addr + RESET_OFFSET;
u16 i; /*, j; */
for (i = 0 ; i < cnt ; i++) {
/* for (j = 0 ; j < 50 ; j++); Delay - FIXME busy waiting... */
cyc2x_writeb(*buffer++, pt_code++);
}
}
/* Load buffer using boot interface.
* o copy data from buffer to Cyclom-X memory
* o wait for reset code to copy it to right portion of memory */
static int buffer_load(u32 addr, u8 *buffer, u32 cnt)
{
cyc2x_memcpy_toio(addr + DATA_OFFSET, buffer, cnt);
cyc2x_writew(GEN_BOOT_DAT, addr + CMD_OFFSET);
return wait_cyc(addr);
}
/* Set up entry point and kick start Cyclom-X CPU. */
static void cycx_start(u32 addr)
{
/* put in 0x30 offset the jump instruction to the code entry point */
cyc2x_writeb(0xea, addr + 0x30);
cyc2x_writeb(0x00, addr + 0x31);
cyc2x_writeb(0xc4, addr + 0x32);
cyc2x_writeb(0x00, addr + 0x33);
cyc2x_writeb(0x00, addr + 0x34);
/* cmd to start executing code */
cyc2x_writew(GEN_START, addr + CMD_OFFSET);
}
/* Load and boot reset code. */
static void cycx_reset_boot(u32 addr, u8 *code, u32 len)
{
u32 pt_start = addr + START_OFFSET;
cyc2x_writeb(0xea, pt_start++); /* jmp to f000:3f00 */
cyc2x_writeb(0x00, pt_start++);
cyc2x_writeb(0xfc, pt_start++);
cyc2x_writeb(0x00, pt_start++);
cyc2x_writeb(0xf0, pt_start);
reset_load(addr, code, len);
/* 80186 was in hold, go */
cyc2x_writeb(0, addr + START_CPU);
delay_cycx(1);
}
/* Load data.bin file through boot (reset) interface. */
static int cycx_data_boot(u32 addr, u8 *code, u32 len)
{
u32 pt_boot_cmd = addr + CMD_OFFSET;
u32 i;
/* boot buffer lenght */
cyc2x_writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
cyc2x_writew(GEN_DEFPAR, pt_boot_cmd);
if (wait_cyc(addr) < 0)
return -1;
cyc2x_writew(0, pt_boot_cmd + sizeof(u16));
cyc2x_writew(0x4000, pt_boot_cmd + 2 * sizeof(u16));
cyc2x_writew(GEN_SET_SEG, pt_boot_cmd);
if (wait_cyc(addr) < 0)
return -1;
for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
if (buffer_load(addr, code + i,
MIN(CFM_LOAD_BUFSZ, (len - i))) < 0) {
printk(KERN_ERR "%s: Error !!\n", modname);
return -1;
}
return 0;
}
/* Load code.bin file through boot (reset) interface. */
static int cycx_code_boot(u32 addr, u8 *code, u32 len)
{
u32 pt_boot_cmd = addr + CMD_OFFSET;
u32 i;
/* boot buffer lenght */
cyc2x_writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
cyc2x_writew(GEN_DEFPAR, pt_boot_cmd);
if (wait_cyc(addr) < 0)
return -1;
cyc2x_writew(0x0000, pt_boot_cmd + sizeof(u16));
cyc2x_writew(0xc400, pt_boot_cmd + 2 * sizeof(u16));
cyc2x_writew(GEN_SET_SEG, pt_boot_cmd);
if (wait_cyc(addr) < 0)
return -1;
for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
if (buffer_load(addr, code + i,MIN(CFM_LOAD_BUFSZ,(len - i)))) {
printk(KERN_ERR "%s: Error !!\n", modname);
return -1;
}
return 0;
}
/* Load adapter from the memory image of the CYCX firmware module.
* o verify firmware integrity and compatibility
* o start adapter up */
static int load_cyc2x(cycxhw_t *hw, cfm_t *cfm, u32 len)
{
int i, j;
cycx_header_t *img_hdr;
u8 *reset_image,
*data_image,
*code_image;
u32 pt_cycld = hw->dpmbase + 0x400;
u16 cksum;
/* Announce */
printk(KERN_INFO "%s: firmware signature=\"%s\"\n", modname,
cfm->signature);
/* Verify firmware signature */
if (strcmp(cfm->signature, CFM_SIGNATURE)) {
printk(KERN_ERR "%s:load_cyc2x: not Cyclom-2X firmware!\n",
modname);
return -EINVAL;
}
printk(KERN_INFO "%s: firmware version=%u\n", modname, cfm->version);
/* Verify firmware module format version */
if (cfm->version != CFM_VERSION) {
printk(KERN_ERR "%s:" __FUNCTION__ ": firmware format %u rejected! "
"Expecting %u.\n",
modname, cfm->version, CFM_VERSION);
return -EINVAL;
}
/* Verify firmware module length and checksum */
cksum = checksum((u8*)&cfm->info, sizeof(cfm_info_t) +
cfm->info.codesize);
/*
FIXME cfm->info.codesize is off by 2
if (((len - sizeof(cfm_t) - 1) != cfm->info.codesize) ||
*/
if (cksum != cfm->checksum) {
printk(KERN_ERR "%s:" __FUNCTION__ ": firmware corrupted!\n",
modname);
printk(KERN_ERR " cdsize = 0x%x (expected 0x%lx)\n",
len - sizeof(cfm_t) - 1, cfm->info.codesize);
printk(KERN_ERR " chksum = 0x%x (expected 0x%x)\n",
cksum, cfm->checksum);
return -EINVAL;
}
/* If everything is ok, set reset, data and code pointers */
img_hdr = (cycx_header_t*)(((u8*)cfm) + sizeof(cfm_t) - 1);
#ifdef FIRMWARE_DEBUG
printk(KERN_INFO "%s:" __FUNCTION__ ": image sizes\n", modname);
printk(KERN_INFO " reset=%lu\n", img_hdr->reset_size);
printk(KERN_INFO " data=%lu\n", img_hdr->data_size);
printk(KERN_INFO " code=%lu\n", img_hdr->code_size);
#endif
reset_image = ((u8 *)img_hdr) + sizeof(cycx_header_t);
data_image = reset_image + img_hdr->reset_size;
code_image = data_image + img_hdr->data_size;
/*---- Start load ----*/
/* Announce */
printk(KERN_INFO "%s: loading firmware %s (ID=%u)...\n", modname,
cfm->descr[0] ? cfm->descr : "unknown firmware",
cfm->info.codeid);
for (i = 0 ; i < 5 ; i++) {
/* Reset Cyclom hardware */
if (!reset_cyc2x(hw->dpmbase)) {
printk(KERN_ERR "%s: dpm problem or board not found\n",
modname);
return -EINVAL;
}
/* Load reset.bin */
cycx_reset_boot(hw->dpmbase, reset_image, img_hdr->reset_size);
/* reset is waiting for boot */
cyc2x_writew(GEN_POWER_ON, pt_cycld);
delay_cycx(1);
for (j = 0 ; j < 3 ; j++)
if (!cyc2x_readw(pt_cycld))
goto reset_loaded;
else
delay_cycx(1);
}
printk(KERN_ERR "%s: reset not started.\n", modname);
return -EINVAL;
reset_loaded:
/* Load data.bin */
if (cycx_data_boot(hw->dpmbase, data_image, img_hdr->data_size)) {
printk(KERN_ERR "%s: cannot load data file.\n", modname);
return -EINVAL;
}
/* Load code.bin */
if (cycx_code_boot(hw->dpmbase, code_image, img_hdr->code_size)) {
printk(KERN_ERR "%s: cannot load code file.\n", modname);
return -EINVAL;
}
/* Prepare boot-time configuration data */
cycx_bootcfg(hw);
/* kick-off CPU */
cycx_start(hw->dpmbase);
/* Arthur Ganzert's tip: wait a while after the firmware loading...
seg abr 26 17:17:12 EST 1999 - acme */
delay_cycx(7);
printk(KERN_INFO "%s: firmware loaded!\n", modname);
/* enable interrupts */
cycx_inten(hw);
return 0;
}
/* Prepare boot-time firmware configuration data.
* o initialize configuration data area
From async.doc - V_3.4.0 - 07/18/1994
- As of now, only static buffers are available to the user.
So, the bit VD_RXDIRC must be set in 'valid'. That means that user
wants to use the static transmission and reception buffers. */
static void cycx_bootcfg(cycxhw_t *hw)
{
/* use fixed buffers */
cyc2x_writeb(FIXED_BUFFERS, hw->dpmbase + CONF_OFFSET);
}
/* Detect Cyclom 2x adapter.
* Following tests are used to detect Cyclom 2x adapter:
* to be completed based on the tests done below
* Return 1 if detected o.k. or 0 if failed.
* Note: This test is destructive! Adapter will be left in shutdown
* state after the test. */
static int detect_cyc2x(u32 addr)
{
reset_cyc2x(addr);
return memory_exists(addr);
}
/* Miscellaneous */
/* Get option's index into the options list.
* Return option's index (1 .. N) or zero if option is invalid. */
static int get_option_index(u32 *optlist, u32 optval)
{
int i = 1;
for (; i <= optlist[0]; ++i)
if (optlist[i] == optval)
return i;
return 0;
}
/* Reset adapter's CPU. */
static int reset_cyc2x(u32 addr)
{
cyc2x_writeb(0, addr + RST_ENABLE);
delay_cycx(2);
cyc2x_writeb(0, addr + RST_DISABLE);
delay_cycx(2);
return memory_exists(addr);
}
/* Delay */
static void delay_cycx(int sec)
{
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(sec*HZ);
}
/* Calculate 16-bit CRC using CCITT polynomial. */
static u16 checksum(u8 *buf, u32 len)
{
u16 crc = 0;
u16 mask, flag;
for (; len; --len, ++buf)
for (mask = 0x80; mask; mask >>= 1) {
flag = (crc & 0x8000);
crc <<= 1;
crc |= ((*buf & mask) ? 1 : 0);
if (flag)
crc ^= 0x1021;
}
return crc;
}
/* End */
/*
* cycx_main.c Cyclades Cyclom 2X WAN Link Driver. Main module.
*
* Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
*
* Copyright: (c) 1998-2000 Arnaldo Carvalho de Melo
*
* Based on sdlamain.c by Gene Kozin <genek@compuserve.com> &
* Jaspreet Singh <jaspreet@sangoma.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
* ============================================================================
* 2000/01/21 acme rename cyclomx_open to cyclomx_mod_inc_use_count
* and cyclomx_close to cyclomx_mod_dec_use_count
* 2000/01/08 acme cleanup
* 1999/11/06 acme cycx_down back to life (it needs to be
* called to iounmap the dpmbase)
* 1999/08/09 acme removed references to enable_tx_int
* use spinlocks instead of cli/sti in
* cyclomx_set_state
* 1999/05/19 acme works directly linked into the kernel
* init_waitqueue_head for 2.3.* kernel
* 1999/05/18 acme major cleanup (polling not needed), etc
* 1998/08/28 acme minor cleanup (ioctls for firmware deleted)
* queue_task activated
* 1998/08/08 acme Initial version.
*/
#include <linux/config.h> /* OS configuration options */
#include <linux/stddef.h> /* offsetof(), etc. */
#include <linux/errno.h> /* return codes */
#include <linux/string.h> /* inline memset(), etc. */
#include <linux/malloc.h> /* kmalloc(), kfree() */
#include <linux/kernel.h> /* printk(), and other useful stuff */
#include <linux/module.h> /* support for loadable modules */
#include <linux/ioport.h> /* request_region(), release_region() */
#include <linux/tqueue.h> /* for kernel task queues */
#include <linux/wanrouter.h> /* WAN router definitions */
#include <linux/cyclomx.h> /* cyclomx common user API definitions */
#include <asm/uaccess.h> /* kernel <-> user copy */
#include <linux/init.h> /* __init (when not using as a module) */
#ifdef MODULE
MODULE_AUTHOR("Arnaldo Carvalho de Melo");
MODULE_DESCRIPTION("Cyclom 2X Sync Card Driver.");
#endif
/* Defines & Macros */
#define DRV_VERSION 0 /* version number */
#define DRV_RELEASE 6 /* release (minor version) number */
#define MAX_CARDS 1 /* max number of adapters */
#ifndef CONFIG_CYCLOMX_CARDS /* configurable option */
#define CONFIG_CYCLOMX_CARDS 1
#endif
/* Function Prototypes */
/* Module entry points */
int init_module (void);
void cleanup_module (void);
/* WAN link driver entry points */
static int setup (wan_device_t *wandev, wandev_conf_t *conf);
static int shutdown (wan_device_t *wandev);
static int ioctl (wan_device_t *wandev, unsigned cmd, unsigned long arg);
/* Miscellaneous functions */
static void cycx_isr (int irq, void *dev_id, struct pt_regs *regs);
/* Global Data
* Note: All data must be explicitly initialized!!!
*/
/* private data */
static char drvname[] = "cyclomx";
static char fullname[] = "CYCLOM 2X(tm) Sync Card Driver";
static char copyright[] = "(c) 1998-2000 Arnaldo Carvalho de Melo "
"<acme@conectiva.com.br>";
static int ncards = CONFIG_CYCLOMX_CARDS;
static cycx_t *card_array = NULL; /* adapter data space */
/* Kernel Loadable Module Entry Points */
/*
* Module 'insert' entry point.
* o print announcement
* o allocate adapter data space
* o initialize static data
* o register all cards with WAN router
* o calibrate Cyclom 2X shared memory access delay.
*
* Return: 0 Ok
* < 0 error.
* Context: process
*/
#ifdef MODULE
int init_module (void)
#else
int __init cyclomx_init (void)
#endif
{
int cnt, err = 0;
printk(KERN_INFO "%s v%u.%u %s\n",
fullname, DRV_VERSION, DRV_RELEASE, copyright);
/* Verify number of cards and allocate adapter data space */
ncards = min(ncards, MAX_CARDS);
ncards = max(ncards, 1);
card_array = kmalloc(sizeof(cycx_t) * ncards, GFP_KERNEL);
if (!card_array)
return -ENOMEM;
memset(card_array, 0, sizeof(cycx_t) * ncards);
/* Register adapters with WAN router */
for (cnt = 0; cnt < ncards; ++cnt) {
cycx_t *card = &card_array[cnt];
wan_device_t *wandev = &card->wandev;
sprintf(card->devname, "%s%d", drvname, cnt + 1);
wandev->magic = ROUTER_MAGIC;
wandev->name = card->devname;
wandev->private = card;
wandev->setup = &setup;
wandev->shutdown = &shutdown;
wandev->ioctl = &ioctl;
err = register_wan_device(wandev);
if (err) {
printk(KERN_ERR "%s: %s registration failed with "
"error %d!\n",
drvname, card->devname, err);
break;
}
}
if (cnt)
ncards = cnt; /* adjust actual number of cards */
else {
kfree(card_array);
err = -ENODEV;
}
return err;
}
/*
* Module 'remove' entry point.
* o unregister all adapters from the WAN router
* o release all remaining system resources
*/
#ifdef MODULE
void cleanup_module (void)
{
int i = 0;
for (; i < ncards; ++i) {
cycx_t *card = &card_array[i];
unregister_wan_device(card->devname);
}
kfree(card_array);
}
#endif
/* WAN Device Driver Entry Points */
/*
* Setup/configure WAN link driver.
* o check adapter state
* o make sure firmware is present in configuration
* o allocate interrupt vector
* o setup Cyclom 2X hardware
* o call appropriate routine to perform protocol-specific initialization
*
* This function is called when router handles ROUTER_SETUP IOCTL. The
* configuration structure is in kernel memory (including extended data, if
* any).
*/
static int setup (wan_device_t *wandev, wandev_conf_t *conf)
{
cycx_t *card;
int err = 0;
int irq;
/* Sanity checks */
if (!wandev || !wandev->private || !conf)
return -EFAULT;
card = wandev->private;
if (wandev->state != WAN_UNCONFIGURED)
return -EBUSY;
if (!conf->data_size || !conf->data) {
printk(KERN_ERR "%s: firmware not found in configuration "
"data!\n", wandev->name);
return -EINVAL;
}
if (conf->irq <= 0) {
printk(KERN_ERR "%s: can't configure without IRQ!\n",
wandev->name);
return -EINVAL;
}
/* Allocate IRQ */
irq = conf->irq == 2 ? 9 : conf->irq; /* IRQ2 -> IRQ9 */
if (request_irq(irq, cycx_isr, 0, wandev->name, card)) {
printk(KERN_ERR "%s: can't reserve IRQ %d!\n",
wandev->name, irq);
return -EINVAL;
}
/* Configure hardware, load firmware, etc. */
memset(&card->hw, 0, sizeof(cycxhw_t));
card->hw.irq = irq;
card->hw.dpmbase = conf->maddr;
card->hw.dpmsize = CYCX_WINDOWSIZE;
card->hw.fwid = CFID_X25_2X;
card->lock = SPIN_LOCK_UNLOCKED;
#if LINUX_VERSION_CODE >= 0x020300
init_waitqueue_head(&card->wait_stats);
#else
card->wait_stats = NULL;
#endif
err = cycx_setup(&card->hw, conf->data, conf->data_size);
if (err) {
free_irq(irq, card);
return err;
}
/* Initialize WAN device data space */
wandev->irq = irq;
wandev->dma = wandev->ioport = 0;
wandev->maddr = card->hw.dpmbase;
wandev->msize = card->hw.dpmsize;
wandev->hw_opt[2] = 0;
wandev->hw_opt[3] = card->hw.fwid;
/* Protocol-specific initialization */
switch (card->hw.fwid) {
#ifdef CONFIG_CYCLOMX_X25
case CFID_X25_2X:
err = cyx_init(card, conf);
break;
#endif
default:
printk(KERN_ERR "%s: this firmware is not supported!\n",
wandev->name);
err = -EINVAL;
}
if (err) {
cycx_down(&card->hw);
free_irq(irq, card);
return err;
}
return 0;
}
/*
* Shut down WAN link driver.
* o shut down adapter hardware
* o release system resources.
*
* This function is called by the router when device is being unregistered or
* when it handles ROUTER_DOWN IOCTL.
*/
static int shutdown (wan_device_t *wandev)
{
cycx_t *card;
/* sanity checks */
if (!wandev || !wandev->private)
return -EFAULT;
if (wandev->state == WAN_UNCONFIGURED)
return 0;
card = wandev->private;
wandev->state = WAN_UNCONFIGURED;
cycx_down(&card->hw);
printk(KERN_INFO "%s: irq %d being freed!\n", wandev->name,
wandev->irq);
free_irq(wandev->irq, card);
return 0;
}
/*
* Driver I/O control.
* o verify arguments
* o perform requested action
*
* This function is called when router handles one of the reserved user
* IOCTLs. Note that 'arg' still points to user address space.
*
* no reserved ioctls for the cyclom 2x up to now
*/
static int ioctl (wan_device_t *wandev, unsigned cmd, unsigned long arg)
{
return -EINVAL;
}
/* Miscellaneous */
/*
* Cyclom 2X Interrupt Service Routine.
* o acknowledge Cyclom 2X hardware interrupt.
* o call protocol-specific interrupt service routine, if any.
*/
static void cycx_isr (int irq, void *dev_id, struct pt_regs *regs)
{
#define card ((cycx_t*)dev_id)
if (!card || card->wandev.state == WAN_UNCONFIGURED)
return;
if (card->in_isr) {
printk(KERN_WARNING "%s: interrupt re-entrancy on IRQ %d!\n",
card->devname, card->wandev.irq);
return;
}
if (card->isr)
card->isr(card);
#undef card
}
/*
* This routine is called by the protocol-specific modules when network
* interface is being open. The only reason we need this, is because we
* have to call MOD_INC_USE_COUNT, but cannot include 'module.h' where it's
* defined more than once into the same kernel module.
*/
void cyclomx_mod_inc_use_count (cycx_t *card)
{
++card->open_cnt;
MOD_INC_USE_COUNT;
}
/*
* This routine is called by the protocol-specific modules when network
* interface is being closed. The only reason we need this, is because we
* have to call MOD_DEC_USE_COUNT, but cannot include 'module.h' where it's
* defined more than once into the same kernel module.
*/
void cyclomx_mod_dec_use_count (cycx_t *card)
{
--card->open_cnt;
MOD_DEC_USE_COUNT;
}
/* Set WAN device state. */
void cyclomx_set_state (cycx_t *card, int state)
{
unsigned long host_cpu_flags;
char *string_state = NULL;
spin_lock_irqsave(&card->lock, host_cpu_flags);
if (card->wandev.state != state) {
switch (state) {
case WAN_CONNECTED:
string_state = "connected!";
break;
case WAN_DISCONNECTED:
string_state = "disconnected!";
break;
}
printk(KERN_INFO "%s: link %s\n", card->devname, string_state);
card->wandev.state = state;
}
card->state_tick = jiffies;
spin_unlock_irqrestore(&card->lock, host_cpu_flags);
}
/* End */
/*
* cycx_x25.c Cyclom 2X WAN Link Driver. X.25 module.
*
* Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
*
* Copyright: (c) 1998-2000 Arnaldo Carvalho de Melo
*
* Based on sdla_x25.c by Gene Kozin <genek@compuserve.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
* ============================================================================
* 2000/01/08 acme cleanup
* 1999/10/27 acme use ARPHRD_HWX25 so that the X.25 stack know
* that we have a X.25 stack implemented in
* firmware onboard
* 1999/10/18 acme support for X.25 sockets in if_send,
* beware: socket(AF_X25...) IS WORK IN PROGRESS,
* TCP/IP over X.25 via wanrouter not affected,
* working.
* 1999/10/09 acme chan_disc renamed to chan_disconnect,
* began adding support for X.25 sockets:
* conf->protocol in new_if
* 1999/10/05 acme fixed return E... to return -E...
* 1999/08/10 acme serialized access to the card thru a spinlock
* in x25_exec
* 1999/08/09 acme removed per channel spinlocks
* removed references to enable_tx_int
* 1999/05/28 acme fixed nibble_to_byte, ackvc now properly treated
* if_send simplified
* 1999/05/25 acme fixed t1, t2, t21 & t23 configuration
* use spinlocks instead of cli/sti in some points
* 1999/05/24 acme finished the x25_get_stat function
* 1999/05/23 acme dev->type = ARPHRD_X25 (tcpdump only works,
* AFAIT, with ARPHRD_ETHER). This seems to be
* needed to use socket(AF_X25)...
* Now the config file must specify a peer media
* address for svc channels over a crossover cable.
* Removed hold_timeout from x25_channel_t,
* not used.
* A little enhancement in the DEBUG processing
* 1999/05/22 acme go to DISCONNECTED in disconnect_confirm_intr,
* instead of chan_disc.
* 1999/05/16 marcelo fixed timer initialization in SVCs
* 1999/01/05 acme x25_configure now get (most of) all
* parameters...
* 1999/01/05 acme pktlen now (correctly) uses log2 (value
* configured)
* 1999/01/03 acme judicious use of data types (u8, u16, u32, etc)
* 1999/01/03 acme cyx_isr: reset dpmbase to acknowledge
* indication (interrupt from cyclom 2x)
* 1999/01/02 acme cyx_isr: first hackings...
* 1999/01/0203 acme when initializing an array don't give less
* elements than declared...
* example: char send_cmd[6] = "?\xFF\x10";
* you'll gonna lose a couple hours, 'cause your
* brain won't admit that there's an error in the
* above declaration... the side effect is that
* memset is put into the unresolved symbols
* instead of using the inline memset functions...
* 1999/01/02 acme began chan_connect, chan_send, x25_send
* 1998/12/31 acme x25_configure
* this code can be compiled as non module
* 1998/12/27 acme code cleanup
* IPX code wiped out! let's decrease code
* complexity for now, remember: I'm learning! :)
* bps_to_speed_code OK
* 1998/12/26 acme Minimal debug code cleanup
* 1998/08/08 acme Initial version.
*/
#define CYCLOMX_X25_DEBUG 1
#include <linux/version.h>
#include <linux/kernel.h> /* printk(), and other useful stuff */
#include <linux/stddef.h> /* offsetof(), etc. */
#include <linux/errno.h> /* return codes */
#include <linux/string.h> /* inline memset(), etc. */
#include <linux/malloc.h> /* kmalloc(), kfree() */
#include <linux/wanrouter.h> /* WAN router definitions */
#include <asm/byteorder.h> /* htons(), etc. */
#include <linux/if_arp.h> /* ARPHRD_HWX25 */
#include <linux/cyclomx.h> /* Cyclom 2X common user API definitions */
#include <linux/cycx_x25.h> /* X.25 firmware API definitions */
/* Defines & Macros */
#define MAX_CMD_RETRY 5
#define X25_CHAN_MTU 2048 /* unfragmented logical channel MTU */
/* Data Structures */
/* This is an extension of the 'struct net_device' we create for each network
interface to keep the rest of X.25 channel-specific data. */
typedef struct x25_channel {
/* This member must be first. */
struct net_device *slave; /* WAN slave */
char name[WAN_IFNAME_SZ+1]; /* interface name, ASCIIZ */
char addr[WAN_ADDRESS_SZ+1]; /* media address, ASCIIZ */
char *local_addr; /* local media address, ASCIIZ -
svc thru crossover cable */
s16 lcn; /* logical channel number/conn.req.key*/
u8 link;
struct timer_list timer; /* timer used for svc channel disc. */
u16 protocol; /* ethertype, 0 - multiplexed */
u8 svc; /* 0 - permanent, 1 - switched */
u8 state; /* channel state */
u8 drop_sequence; /* mark sequence for dropping */
u32 idle_tmout; /* sec, before disconnecting */
struct sk_buff *rx_skb; /* receive socket buffer */
cycx_t *card; /* -> owner */
struct enet_statistics ifstats; /* interface statistics */
} x25_channel_t;
/* Function Prototypes */
/* WAN link driver entry points. These are called by the WAN router module. */
static int update (wan_device_t *wandev),
new_if (wan_device_t *wandev, struct net_device *dev,
wanif_conf_t *conf),
del_if (wan_device_t *wandev, struct net_device *dev);
/* Network device interface */
static int if_init (struct net_device *dev),
if_open (struct net_device *dev),
if_close (struct net_device *dev),
if_header (struct sk_buff *skb, struct net_device *dev,
u16 type, void *daddr, void *saddr, unsigned len),
if_rebuild_hdr (struct sk_buff *skb),
if_send (struct sk_buff *skb, struct net_device *dev);
static struct net_device_stats * if_stats (struct net_device *dev);
/* Interrupt handlers */
static void cyx_isr (cycx_t *card),
tx_intr (cycx_t *card, TX25Cmd *cmd),
rx_intr (cycx_t *card, TX25Cmd *cmd),
log_intr (cycx_t *card, TX25Cmd *cmd),
stat_intr (cycx_t *card, TX25Cmd *cmd),
connect_confirm_intr (cycx_t *card, TX25Cmd *cmd),
disconnect_confirm_intr (cycx_t *card, TX25Cmd *cmd),
connect_intr (cycx_t *card, TX25Cmd *cmd),
disconnect_intr (cycx_t *card, TX25Cmd *cmd),
spur_intr (cycx_t *card, TX25Cmd *cmd);
/* X.25 firmware interface functions */
static int x25_configure (cycx_t *card, TX25Config *conf),
x25_get_stats (cycx_t *card),
x25_send (cycx_t *card, u8 link, u8 lcn, u8 bitm, int len,
void *buf),
x25_connect_response (cycx_t *card, x25_channel_t *chan),
x25_disconnect_response (cycx_t *card, u8 link, u8 lcn);
/* channel functions */
static int chan_connect (struct net_device *dev),
chan_send (struct net_device *dev, struct sk_buff *skb);
static void chan_disconnect (struct net_device *dev),
chan_x25_send_event(struct net_device *dev, u8 event);
/* Miscellaneous functions */
static void set_chan_state (struct net_device *dev, u8 state),
chan_timer (unsigned long d);
static void nibble_to_byte (u8 *s, u8 *d, u8 len, u8 nibble),
reset_timer (struct net_device *dev);
static u8 bps_to_speed_code (u32 bps);
static u8 log2 (u32 n);
static unsigned dec_to_uint (u8 *str, int len);
static struct net_device *get_dev_by_lcn (wan_device_t *wandev, s16 lcn);
static struct net_device *get_dev_by_dte_addr (wan_device_t *wandev, char *dte);
#ifdef CYCLOMX_X25_DEBUG
static void hex_dump(char *msg, unsigned char *p, int len);
static void x25_dump_config(TX25Config *conf);
static void x25_dump_stats(TX25Stats *stats);
static void x25_dump_devs(wan_device_t *wandev);
#define dprintk(format, a...) printk(format, ##a)
#else
#define hex_dump(msg, p, len)
#define x25_dump_config(conf)
#define x25_dump_stats(stats)
#define x25_dump_devs(wandev)
#define dprintk(format, a...)
#endif
/* Public Functions */
/* X.25 Protocol Initialization routine.
*
* This routine is called by the main Cyclom 2X module during setup. At this
* point adapter is completely initialized and X.25 firmware is running.
* o configure adapter
* o initialize protocol-specific fields of the adapter data space.
*
* Return: 0 o.k.
* < 0 failure. */
int cyx_init (cycx_t *card, wandev_conf_t *conf)
{
TX25Config cfg;
/* Verify configuration ID */
if (conf->config_id != WANCONFIG_X25) {
printk(KERN_INFO "%s: invalid configuration ID %u!\n",
card->devname, conf->config_id);
return -EINVAL;
}
/* Initialize protocol-specific fields */
card->mbox = card->hw.dpmbase + X25_MBOX_OFFS;
card->u.x.connection_keys = 0;
card->u.x.lock = SPIN_LOCK_UNLOCKED;
/* Configure adapter. Here we set reasonable defaults, then parse
* device configuration structure and set configuration options.
* Most configuration options are verified and corrected (if
* necessary) since we can't rely on the adapter to do so and don't
* want it to fail either. */
memset(&cfg, 0, sizeof(cfg));
cfg.link = 0;
cfg.clock = conf->clocking == WANOPT_EXTERNAL ? 8 : 55;
cfg.speed = bps_to_speed_code(conf->bps);
cfg.n3win = 7;
cfg.n2win = 2;
cfg.n2 = 5;
cfg.nvc = 1;
cfg.npvc = 1;
cfg.flags = 0x02; /* default = V35 */
cfg.t1 = 10; /* line carrier timeout */
cfg.t2 = 29; /* tx timeout */
cfg.t21 = 180; /* CALL timeout */
cfg.t23 = 180; /* CLEAR timeout */
/* adjust MTU */
if (!conf->mtu || conf->mtu >= 512)
card->wandev.mtu = 512;
else if (conf->mtu >= 256)
card->wandev.mtu = 256;
else if (conf->mtu >= 128)
card->wandev.mtu = 128;
else
card->wandev.mtu = 64;
cfg.pktlen = log2(card->wandev.mtu);
if (conf->station == WANOPT_DTE) {
cfg.locaddr = 3; /* DTE */
cfg.remaddr = 1; /* DCE */
} else {
cfg.locaddr = 1; /* DCE */
cfg.remaddr = 3; /* DTE */
}
if (conf->interface == WANOPT_RS232)
cfg.flags = 0; /* FIXME just reset the 2nd bit */
if (conf->u.x25.hi_pvc) {
card->u.x.hi_pvc = min(conf->u.x25.hi_pvc, 4095);
card->u.x.lo_pvc = min(conf->u.x25.lo_pvc, card->u.x.hi_pvc);
}
if (conf->u.x25.hi_svc) {
card->u.x.hi_svc = min(conf->u.x25.hi_svc, 4095);
card->u.x.lo_svc = min(conf->u.x25.lo_svc, card->u.x.hi_svc);
}
if (card->u.x.lo_pvc == 255)
cfg.npvc = 0;
else
cfg.npvc = card->u.x.hi_pvc - card->u.x.lo_pvc + 1;
cfg.nvc = card->u.x.hi_svc - card->u.x.lo_svc + 1 + cfg.npvc;
if (conf->u.x25.hdlc_window)
cfg.n2win = min(conf->u.x25.hdlc_window, 7);
if (conf->u.x25.pkt_window)
cfg.n3win = min(conf->u.x25.pkt_window, 7);
if (conf->u.x25.t1)
cfg.t1 = min(conf->u.x25.t1, 30);
if (conf->u.x25.t2)
cfg.t2 = min(conf->u.x25.t2, 30);
if (conf->u.x25.t11_t21)
cfg.t21 = min(conf->u.x25.t11_t21, 30);
if (conf->u.x25.t13_t23)
cfg.t23 = min(conf->u.x25.t13_t23, 30);
if (conf->u.x25.n2)
cfg.n2 = min(conf->u.x25.n2, 30);
/* initialize adapter */
if (x25_configure(card, &cfg))
return -EIO;
/* Initialize protocol-specific fields of adapter data space */
card->wandev.bps = conf->bps;
card->wandev.interface = conf->interface;
card->wandev.clocking = conf->clocking;
card->wandev.station = conf->station;
card->isr = cyx_isr;
card->exec = NULL;
card->wandev.update = update;
card->wandev.new_if = new_if;
card->wandev.del_if = del_if;
card->wandev.state = WAN_DISCONNECTED;
return 0;
}
/* WAN Device Driver Entry Points */
/* Update device status & statistics. */
static int update (wan_device_t *wandev)
{
/* sanity checks */
if (!wandev || !wandev->private)
return -EFAULT;
if (wandev->state == WAN_UNCONFIGURED)
return -ENODEV;
x25_get_stats(wandev->private);
return 0;
}
/* Create new logical channel.
* This routine is called by the router when ROUTER_IFNEW IOCTL is being
* handled.
* o parse media- and hardware-specific configuration
* o make sure that a new channel can be created
* o allocate resources, if necessary
* o prepare network device structure for registration.
*
* Return: 0 o.k.
* < 0 failure (channel will not be created) */
static int new_if (wan_device_t *wandev, struct net_device *dev,
wanif_conf_t *conf)
{
cycx_t *card = wandev->private;
x25_channel_t *chan;
int err = 0;
if (!conf->name[0] || strlen(conf->name) > WAN_IFNAME_SZ) {
printk(KERN_INFO "%s: invalid interface name!\n",card->devname);
return -EINVAL;
}
/* allocate and initialize private data */
if ((chan = kmalloc(sizeof(x25_channel_t), GFP_KERNEL)) == NULL)
return -ENOMEM;
memset(chan, 0, sizeof(x25_channel_t));
strcpy(chan->name, conf->name);
chan->card = card;
chan->link = conf->port;
chan->protocol = conf->protocol ? ETH_P_X25 : ETH_P_IP;
chan->rx_skb = NULL;
/* only used in svc connected thru crossover cable */
chan->local_addr = NULL;
if (conf->addr[0] == '@') { /* SVC */
int len = strlen(conf->local_addr);
if (len) {
if (len > WAN_ADDRESS_SZ) {
printk(KERN_ERR "%s: %s local addr too long!\n",
wandev->name, chan->name);
kfree(chan);
return -EINVAL;
} else {
chan->local_addr = kmalloc(len + 1, GFP_KERNEL);
if (!chan->local_addr) {
kfree(chan);
return -ENOMEM;
}
}
strncpy(chan->local_addr, conf->local_addr,
WAN_ADDRESS_SZ);
}
chan->svc = 1;
strncpy(chan->addr, &conf->addr[1], WAN_ADDRESS_SZ);
init_timer(&chan->timer);
chan->timer.function = chan_timer;
chan->timer.data = (unsigned long)dev;
/* Set channel timeouts (default if not specified) */
chan->idle_tmout = conf->idle_timeout ? conf->idle_timeout : 90;
} else if (is_digit(conf->addr[0])) { /* PVC */
s16 lcn = dec_to_uint(conf->addr, 0);
if (lcn >= card->u.x.lo_pvc && lcn <= card->u.x.hi_pvc)
chan->lcn = lcn;
else {
printk(KERN_ERR
"%s: PVC %u is out of range on interface %s!\n",
wandev->name, lcn, chan->name);
err = -EINVAL;
}
} else {
printk(KERN_ERR "%s: invalid media address on interface %s!\n",
wandev->name, chan->name);
err = -EINVAL;
}
if (err) {
if (chan->local_addr)
kfree(chan->local_addr);
kfree(chan);
return err;
}
/* prepare network device data space for registration */
dev->name = chan->name;
dev->init = if_init;
dev->priv = chan;
return 0;
}
/* Delete logical channel. */
static int del_if (wan_device_t *wandev, struct net_device *dev)
{
if (dev->priv) {
x25_channel_t *chan = dev->priv;
if (chan->svc) {
if (chan->local_addr)
kfree(chan->local_addr);
if (chan->state == WAN_CONNECTED)
del_timer(&chan->timer);
}
kfree(chan);
dev->priv = NULL;
}
return 0;
}
/* Network Device Interface */
/* Initialize Linux network interface.
*
* This routine is called only once for each interface, during Linux network
* interface registration. Returning anything but zero will fail interface
* registration. */
static int if_init (struct net_device *dev)
{
x25_channel_t *chan = dev->priv;
cycx_t *card = chan->card;
wan_device_t *wandev = &card->wandev;
/* Initialize device driver entry points */
dev->open = if_open;
dev->stop = if_close;
dev->hard_header = if_header;
dev->rebuild_header = if_rebuild_hdr;
dev->hard_start_xmit = if_send;
dev->get_stats = if_stats;
/* Initialize media-specific parameters */
dev->mtu = X25_CHAN_MTU;
dev->type = ARPHRD_HWX25; /* ARP h/w type */
dev->hard_header_len = 0; /* media header length */
dev->addr_len = 0; /* hardware address length */
if (!chan->svc)
*(u16*)dev->dev_addr = htons(chan->lcn);
/* Initialize hardware parameters (just for reference) */
dev->irq = wandev->irq;
dev->dma = wandev->dma;
dev->base_addr = wandev->ioport;
dev->mem_start = (unsigned long)wandev->maddr;
dev->mem_end = (unsigned long)(wandev->maddr + wandev->msize - 1);
dev->flags |= IFF_NOARP;
/* Set transmit buffer queue length */
dev->tx_queue_len = 10;
/* Initialize socket buffers */
dev_init_buffers(dev);
set_chan_state(dev, WAN_DISCONNECTED);
return 0;
}
/* Open network interface.
* o prevent module from unloading by incrementing use count
* o if link is disconnected then initiate connection
*
* Return 0 if O.k. or errno. */
static int if_open (struct net_device *dev)
{
x25_channel_t *chan = dev->priv;
cycx_t *card = chan->card;
if (test_bit(LINK_STATE_START, &dev->state))
return -EBUSY; /* only one open is allowed */
netif_start_queue(dev);
cyclomx_mod_inc_use_count(card);
return 0;
}
/* Close network interface.
* o reset flags.
* o if there's no more open channels then disconnect physical link. */
static int if_close (struct net_device *dev)
{
x25_channel_t *chan = dev->priv;
cycx_t *card = chan->card;
netif_stop_queue(dev);
if (chan->state == WAN_CONNECTED || chan->state == WAN_CONNECTING)
chan_disconnect(dev);
cyclomx_mod_dec_use_count(card);
return 0;
}
/* Build media header.
* o encapsulate packet according to encapsulation type.
*
* The trick here is to put packet type (Ethertype) into 'protocol' field of
* the socket buffer, so that we don't forget it. If encapsulation fails,
* set skb->protocol to 0 and discard packet later.
*
* Return: media header length. */
static int if_header (struct sk_buff *skb, struct net_device *dev,
u16 type, void *daddr, void *saddr, unsigned len)
{
skb->protocol = type;
return dev->hard_header_len;
}
/* * Re-build media header.
* Return: 1 physical address resolved.
* 0 physical address not resolved */
static int if_rebuild_hdr (struct sk_buff *skb)
{
return 1;
}
/* Send a packet on a network interface.
* o set busy flag (marks start of the transmission).
* o check link state. If link is not up, then drop the packet.
* o check channel status. If it's down then initiate a call.
* o pass a packet to corresponding WAN device.
* o free socket buffer
*
* Return: 0 complete (socket buffer must be freed)
* non-0 packet may be re-transmitted (tbusy must be set)
*
* Notes:
* 1. This routine is called either by the protocol stack or by the "net
* bottom half" (with interrupts enabled).
* 2. Setting tbusy flag will inhibit further transmit requests from the
* protocol stack and can be used for flow control with protocol layer. */
static int if_send (struct sk_buff *skb, struct net_device *dev)
{
x25_channel_t *chan = dev->priv;
cycx_t *card = chan->card;
if (!chan->svc)
chan->protocol = skb->protocol;
if (card->wandev.state != WAN_CONNECTED)
++chan->ifstats.tx_dropped;
else if (chan->svc && chan->protocol &&
chan->protocol != skb->protocol) {
printk(KERN_INFO
"%s: unsupported Ethertype 0x%04X on interface %s!\n",
card->devname, skb->protocol, dev->name);
++chan->ifstats.tx_errors;
} else if (chan->protocol == ETH_P_IP) {
switch (chan->state) {
case WAN_DISCONNECTED:
if (chan_connect(dev)) {
netif_stop_queue(dev);
return -EBUSY;
}
/* fall thru */
case WAN_CONNECTED:
reset_timer(dev);
dev->trans_start = jiffies;
netif_stop_queue(dev);
if (chan_send(dev, skb))
return -EBUSY;
break;
default:
++chan->ifstats.tx_dropped;
++card->wandev.stats.tx_dropped;
}
} else { /* chan->protocol == ETH_P_X25 */
switch (skb->data[0]) {
case 0: break;
case 1: /* Connect request */
chan_connect(dev);
goto free_packet;
case 2: /* Disconnect request */
chan_disconnect(dev);
goto free_packet;
default:
printk(KERN_INFO
"%s: unknown %d x25-iface request on %s!\n",
card->devname, skb->data[0], dev->name);
++chan->ifstats.tx_errors;
goto free_packet;
}
skb_pull(skb, 1); /* Remove control byte */
reset_timer(dev);
dev->trans_start = jiffies;
netif_stop_queue(dev);
if (chan_send(dev, skb)) {
/* prepare for future retransmissions */
skb_push(skb, 1);
return -EBUSY;
}
}
free_packet:
dev_kfree_skb(skb);
return 0;
}
/* Get Ethernet-style interface statistics.
* Return a pointer to struct net_device_stats */
static struct net_device_stats *if_stats (struct net_device *dev)
{
x25_channel_t *chan = dev->priv;
return chan ? &chan->ifstats : NULL;
}
/* Interrupt Handlers */
/* X.25 Interrupt Service Routine. */
static void cyx_isr (cycx_t *card)
{
TX25Cmd cmd;
u16 z = 0;
card->in_isr = 1;
card->buff_int_mode_unbusy = 0;
cycx_peek(&card->hw, X25_RXMBOX_OFFS, &cmd, sizeof(cmd));
switch (cmd.command) {
case X25_DATA_INDICATION:
rx_intr(card, &cmd);
break;
case X25_ACK_FROM_VC:
tx_intr(card, &cmd);
break;
case X25_LOG:
log_intr(card, &cmd);
break;
case X25_STATISTIC:
stat_intr(card, &cmd);
break;
case X25_CONNECT_CONFIRM:
connect_confirm_intr(card, &cmd);
break;
case X25_CONNECT_INDICATION:
connect_intr(card, &cmd);
break;
case X25_DISCONNECT_INDICATION:
disconnect_intr(card, &cmd);
break;
case X25_DISCONNECT_CONFIRM:
disconnect_confirm_intr(card, &cmd);
break;
case X25_LINE_ON:
cyclomx_set_state(card, WAN_CONNECTED);
break;
case X25_LINE_OFF:
cyclomx_set_state(card, WAN_DISCONNECTED);
break;
default:
spur_intr(card, &cmd); /* unwanted interrupt */
}
cycx_poke(&card->hw, 0, &z, sizeof(z));
cycx_poke(&card->hw, X25_RXMBOX_OFFS, &z, sizeof(z));
card->in_isr = 0;
}
/* Transmit interrupt handler.
* o Release socket buffer
* o Clear 'tbusy' flag */
static void tx_intr (cycx_t *card, TX25Cmd *cmd)
{
struct net_device *dev;
wan_device_t *wandev = &card->wandev;
u8 lcn;
cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
/* unbusy device and then dev_tint(); */
if ((dev = get_dev_by_lcn(wandev, lcn)) != NULL) {
card->buff_int_mode_unbusy = 1;
netif_wake_queue(dev);
} else
printk(KERN_ERR "%s:ackvc for inexistent lcn %d\n",
card->devname, lcn);
}
/* Receive interrupt handler.
* This routine handles fragmented IP packets using M-bit according to the
* RFC1356.
* o map logical channel number to network interface.
* o allocate socket buffer or append received packet to the existing one.
* o if M-bit is reset (i.e. it's the last packet in a sequence) then
* decapsulate packet and pass socket buffer to the protocol stack.
*
* Notes:
* 1. When allocating a socket buffer, if M-bit is set then more data is
* coming and we have to allocate buffer for the maximum IP packet size
* expected on this channel.
* 2. If something goes wrong and X.25 packet has to be dropped (e.g. no
* socket buffers available) the whole packet sequence must be discarded. */
static void rx_intr (cycx_t *card, TX25Cmd *cmd)
{
wan_device_t *wandev = &card->wandev;
struct net_device *dev;
x25_channel_t *chan;
struct sk_buff *skb;
u8 bitm, lcn;
int pktlen = cmd->len - 5;
cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
cycx_peek(&card->hw, cmd->buf + 4, &bitm, sizeof(bitm));
bitm &= 0x10;
if ((dev = get_dev_by_lcn(wandev, lcn)) == NULL) {
/* Invalid channel, discard packet */
printk(KERN_INFO "%s: receiving on orphaned LCN %d!\n",
card->devname, lcn);
return;
}
chan = dev->priv;
reset_timer(dev);
if (chan->drop_sequence) {
if (!bitm)
chan->drop_sequence = 0;
else
return;
}
if ((skb = chan->rx_skb) == NULL) {
/* Allocate new socket buffer */
int bufsize = bitm ? dev->mtu : pktlen;
if ((skb = dev_alloc_skb((chan->protocol == ETH_P_X25 ? 1 : 0) +
bufsize +
dev->hard_header_len)) == NULL) {
printk(KERN_INFO "%s: no socket buffers available!\n",
card->devname);
chan->drop_sequence = 1;
++chan->ifstats.rx_dropped;
return;
}
if (chan->protocol == ETH_P_X25) /* X.25 socket layer control */
/* 0 = data packet (dev_alloc_skb zeroed skb->data) */
skb_put(skb, 1);
skb->dev = dev;
skb->protocol = htons(chan->protocol);
chan->rx_skb = skb;
}
if (skb_tailroom(skb) < pktlen) {
/* No room for the packet. Call off the whole thing! */
dev_kfree_skb(skb);
chan->rx_skb = NULL;
if (bitm)
chan->drop_sequence = 1;
printk(KERN_INFO "%s: unexpectedly long packet sequence "
"on interface %s!\n", card->devname, dev->name);
++chan->ifstats.rx_length_errors;
return;
}
/* Append packet to the socket buffer */
cycx_peek(&card->hw, cmd->buf + 5, skb_put(skb, pktlen), pktlen);
if (bitm)
return; /* more data is coming */
dev->last_rx = jiffies; /* timestamp */
chan->rx_skb = NULL; /* dequeue packet */
++chan->ifstats.rx_packets;
chan->ifstats.rx_bytes += skb->len;
skb->mac.raw = skb->data;
netif_rx(skb);
}
/* Connect interrupt handler. */
static void connect_intr (cycx_t *card, TX25Cmd *cmd)
{
wan_device_t *wandev = &card->wandev;
struct net_device *dev = NULL;
x25_channel_t *chan;
u8 d[32],
loc[24],
rem[24];
u8 lcn, sizeloc, sizerem;
cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
cycx_peek(&card->hw, cmd->buf + 5, &sizeloc, sizeof(sizeloc));
cycx_peek(&card->hw, cmd->buf + 6, d, cmd->len - 6);
sizerem = sizeloc >> 4;
sizeloc &= 0x0F;
loc[0] = rem[0] = '\0';
if (sizeloc)
nibble_to_byte(d, loc, sizeloc, 0);
if (sizerem)
nibble_to_byte(d + (sizeloc >> 1), rem, sizerem, sizeloc & 1);
dprintk(KERN_INFO "connect_intr:lcn=%d, local=%s, remote=%s\n",
lcn, loc, rem);
if ((dev = get_dev_by_dte_addr(wandev, rem)) == NULL) {
/* Invalid channel, discard packet */
printk(KERN_INFO "%s: connect not expected: remote %s!\n",
card->devname, rem);
return;
}
chan = dev->priv;
chan->lcn = lcn;
x25_connect_response(card, chan);
set_chan_state(dev, WAN_CONNECTED);
}
/* Connect confirm interrupt handler. */
static void connect_confirm_intr (cycx_t *card, TX25Cmd *cmd)
{
wan_device_t *wandev = &card->wandev;
struct net_device *dev;
x25_channel_t *chan;
u8 lcn, key;
cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
cycx_peek(&card->hw, cmd->buf + 1, &key, sizeof(key));
dprintk(KERN_INFO "%s: connect_confirm_intr:lcn=%d, key=%d\n",
card->devname, lcn, key);
if ((dev = get_dev_by_lcn(wandev, -key)) == NULL) {
/* Invalid channel, discard packet */
clear_bit(--key, (void*)&card->u.x.connection_keys);
printk(KERN_INFO "%s: connect confirm not expected: lcn %d, "
"key=%d!\n", card->devname, lcn, key);
return;
}
clear_bit(--key, (void*)&card->u.x.connection_keys);
chan = dev->priv;
chan->lcn = lcn;
set_chan_state(dev, WAN_CONNECTED);
}
/* Disconnect confirm interrupt handler. */
static void disconnect_confirm_intr (cycx_t *card, TX25Cmd *cmd)
{
wan_device_t *wandev = &card->wandev;
struct net_device *dev;
u8 lcn;
cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
dprintk(KERN_INFO "%s: disconnect_confirm_intr:lcn=%d\n",
card->devname, lcn);
if ((dev = get_dev_by_lcn(wandev, lcn)) == NULL) {
/* Invalid channel, discard packet */
printk(KERN_INFO "%s:disconnect confirm not expected!:lcn %d\n",
card->devname, lcn);
return;
}
set_chan_state(dev, WAN_DISCONNECTED);
}
/* disconnect interrupt handler. */
static void disconnect_intr (cycx_t *card, TX25Cmd *cmd)
{
wan_device_t *wandev = &card->wandev;
struct net_device *dev;
u8 lcn;
cycx_peek(&card->hw, cmd->buf, &lcn, sizeof(lcn));
dprintk(KERN_INFO "disconnect_intr:lcn=%d\n", lcn);
if ((dev = get_dev_by_lcn(wandev, lcn)) != NULL) {
x25_channel_t *chan = dev->priv;
x25_disconnect_response(card, chan->link, lcn);
set_chan_state(dev, WAN_DISCONNECTED);
} else
x25_disconnect_response(card, 0, lcn);
}
/* LOG interrupt handler. */
static void log_intr (cycx_t *card, TX25Cmd *cmd)
{
#if CYCLOMX_X25_DEBUG
char bf[20];
u16 size, toread, link, msg_code;
u8 code, routine;
cycx_peek(&card->hw, cmd->buf, &msg_code, sizeof(msg_code));
cycx_peek(&card->hw, cmd->buf + 2, &link, sizeof(link));
cycx_peek(&card->hw, cmd->buf + 4, &size, sizeof(size));
/* at most 20 bytes are available... thanks to Daniela :) */
toread = size < 20 ? size : 20;
cycx_peek(&card->hw, cmd->buf + 10, &bf, toread);
cycx_peek(&card->hw, cmd->buf + 10 + toread, &code, 1);
cycx_peek(&card->hw, cmd->buf + 10 + toread + 1, &routine, 1);
printk(KERN_INFO "cyx_isr: X25_LOG (0x4500) indic.:\n");
printk(KERN_INFO "cmd->buf=0x%X\n", cmd->buf);
printk(KERN_INFO "Log message code=0x%X\n", msg_code);
printk(KERN_INFO "Link=%d\n", link);
printk(KERN_INFO "log code=0x%X\n", code);
printk(KERN_INFO "log routine=0x%X\n", routine);
printk(KERN_INFO "Message size=%d\n", size);
hex_dump("Message", bf, toread);
#endif
}
/* STATISTIC interrupt handler. */
static void stat_intr (cycx_t *card, TX25Cmd *cmd)
{
cycx_peek(&card->hw, cmd->buf, &card->u.x.stats,
sizeof(card->u.x.stats));
hex_dump("stat_intr", (unsigned char*)&card->u.x.stats,
sizeof(card->u.x.stats));
x25_dump_stats(&card->u.x.stats);
wake_up_interruptible(&card->wait_stats);
}
/* Spurious interrupt handler.
* o print a warning
* If number of spurious interrupts exceeded some limit, then ??? */
static void spur_intr (cycx_t *card, TX25Cmd *cmd)
{
printk(KERN_INFO "%s: spurious interrupt (0x%X)!\n",
card->devname, cmd->command);
}
#ifdef CYCLOMX_X25_DEBUG
static void hex_dump(char *msg, unsigned char *p, int len)
{
unsigned char hex[1024],
* phex = hex;
if (len >= (sizeof(hex) / 2))
len = (sizeof(hex) / 2) - 1;
while (len--) {
sprintf(phex, "%02x", *p++);
phex += 2;
}
printk(KERN_INFO "%s: %s\n", msg, hex);
}
#endif
/* Cyclom 2X Firmware-Specific Functions */
/* Exec X.25 command. */
static int x25_exec (cycx_t *card, int command, int link,
void *d1, int len1, void *d2, int len2)
{
TX25Cmd c;
unsigned long flags;
u32 addr = 0x1200 + 0x2E0 * link + 0x1E2;
u8 retry = MAX_CMD_RETRY;
int err = 0;
c.command = command;
c.link = link;
c.len = len1 + len2;
spin_lock_irqsave(&card->u.x.lock, flags);
/* write command */
cycx_poke(&card->hw, X25_MBOX_OFFS, &c, sizeof(c) - sizeof(c.buf));
/* write X.25 data */
if (d1) {
cycx_poke(&card->hw, addr, d1, len1);
if (d2) {
if (len2 > 254) {
u32 addr1 = 0xA00 + 0x400 * link;
cycx_poke(&card->hw, addr + len1, d2, 249);
cycx_poke(&card->hw, addr1, ((u8*)d2) + 249,
len2 - 249);
} else
cycx_poke(&card->hw, addr + len1, d2, len2);
}
}
/* generate interruption, executing command */
cycx_intr(&card->hw);
/* wait till card->mbox == 0 */
do {
err = cycx_exec(card->mbox);
} while (retry-- && err);
spin_unlock_irqrestore(&card->u.x.lock, flags);
return err;
}
/* Configure adapter. */
static int x25_configure (cycx_t *card, TX25Config *conf)
{
struct {
u16 nlinks;
TX25Config conf[2];
} x25_cmd_conf;
memset (&x25_cmd_conf, 0, sizeof(x25_cmd_conf));
x25_cmd_conf.nlinks = 2;
x25_cmd_conf.conf[0] = *conf;
/* FIXME: we need to find a way in the wanrouter framework
to configure the second link, for now lets use it
with the same config from the first link, fixing
the interface type to RS232, the speed in 38400 and
the clock to external */
x25_cmd_conf.conf[1] = *conf;
x25_cmd_conf.conf[1].link = 1;
x25_cmd_conf.conf[1].speed = 5; /* 38400 */
x25_cmd_conf.conf[1].clock = 8;
x25_cmd_conf.conf[1].flags = 0; /* default = RS232 */
x25_dump_config(&x25_cmd_conf.conf[0]);
x25_dump_config(&x25_cmd_conf.conf[1]);
return x25_exec(card, X25_CONFIG, 0,
&x25_cmd_conf, sizeof(x25_cmd_conf), NULL, 0);
}
/* Get protocol statistics. */
static int x25_get_stats (cycx_t *card)
{
/* the firmware expects 20 in the size field!!!
thanks to Daniela */
int err = x25_exec(card, X25_STATISTIC, 0, NULL, 20, NULL, 0);
if (err)
return err;
interruptible_sleep_on(&card->wait_stats);
if (signal_pending(current))
return -EINTR;
card->wandev.stats.rx_packets = card->u.x.stats.n2_rx_frames;
card->wandev.stats.rx_over_errors = card->u.x.stats.rx_over_errors;
card->wandev.stats.rx_crc_errors = card->u.x.stats.rx_crc_errors;
card->wandev.stats.rx_length_errors = 0; /* not available from fw */
card->wandev.stats.rx_frame_errors = 0; /* not available from fw */
card->wandev.stats.rx_missed_errors = card->u.x.stats.rx_aborts;
card->wandev.stats.rx_dropped = 0; /* not available from fw */
card->wandev.stats.rx_errors = 0; /* not available from fw */
card->wandev.stats.tx_packets = card->u.x.stats.n2_tx_frames;
card->wandev.stats.tx_aborted_errors = card->u.x.stats.tx_aborts;
card->wandev.stats.tx_dropped = 0; /* not available from fw */
card->wandev.stats.collisions = 0; /* not available from fw */
card->wandev.stats.tx_errors = 0; /* not available from fw */
x25_dump_devs(&card->wandev);
return 0;
}
/* return the number of nibbles */
static int byte_to_nibble(u8 *s, u8 *d, char *nibble)
{
int i = 0;
if (*nibble && *s) {
d[i] |= *s++ - '0';
*nibble = 0;
++i;
}
while (*s) {
d[i] = (*s - '0') << 4;
if (*(s + 1))
d[i] |= *(s + 1) - '0';
else {
*nibble = 1;
break;
}
++i;
s += 2;
}
return i;
}
static void nibble_to_byte(u8 *s, u8 *d, u8 len, u8 nibble)
{
if (nibble) {
*d++ = '0' + (*s++ & 0x0F);
--len;
}
while (len) {
*d++ = '0' + (*s >> 4);
if (--len) {
*d++ = '0' + (*s & 0x0F);
--len;
} else break;
++s;
}
*d = '\0';
}
/* Place X.25 call. */
static int x25_place_call (cycx_t *card, x25_channel_t *chan)
{
int err = 0,
len;
char d[64],
nibble = 0,
mylen = chan->local_addr ? strlen(chan->local_addr) : 0,
remotelen = strlen(chan->addr);
u8 key;
if (card->u.x.connection_keys == ~0UL) {
printk(KERN_INFO "%s: too many simultaneous connection "
"requests!\n", card->devname);
return -EAGAIN;
}
key = ffz(card->u.x.connection_keys);
set_bit(key, (void*)&card->u.x.connection_keys);
++key;
dprintk(KERN_INFO "%s:x25_place_call:key=%d\n", card->devname, key);
memset(d, 0, sizeof(d));
d[1] = key; /* user key */
d[2] = 0x10;
d[4] = 0x0B;
len = byte_to_nibble(chan->addr, d + 6, &nibble);
if (chan->local_addr)
len += byte_to_nibble(chan->local_addr, d + 6 + len, &nibble);
if (nibble)
++len;
d[5] = mylen << 4 | remotelen;
d[6 + len + 1] = 0xCC; /* TCP/IP over X.25, thanks to Daniela :) */
if ((err = x25_exec(card, X25_CONNECT_REQUEST, chan->link,
&d, 7 + len + 1, NULL, 0)) != 0)
clear_bit(--key, (void*)&card->u.x.connection_keys);
else
chan->lcn = -key;
return err;
}
/* Place X.25 CONNECT RESPONSE. */
static int x25_connect_response (cycx_t *card, x25_channel_t *chan)
{
u8 d[8];
memset(d, 0, sizeof(d));
d[0] = d[3] = chan->lcn;
d[2] = 0x10;
d[4] = 0x0F;
d[7] = 0xCC; /* TCP/IP over X.25, thanks Daniela */
return x25_exec(card, X25_CONNECT_RESPONSE, chan->link, &d, 8, NULL, 0);
}
/* Place X.25 DISCONNECT RESPONSE. */
static int x25_disconnect_response (cycx_t *card, u8 link, u8 lcn)
{
char d[5];
memset(d, 0, sizeof(d));
d[0] = d[3] = lcn;
d[2] = 0x10;
d[4] = 0x17;
return x25_exec(card, X25_DISCONNECT_RESPONSE, link, &d, 5, NULL, 0);
}
/* Clear X.25 call. */
static int x25_clear_call (cycx_t *card, u8 link, u8 lcn, u8 cause, u8 diagn)
{
u8 d[7];
memset(d, 0, sizeof(d));
d[0] = d[3] = lcn;
d[2] = 0x10;
d[4] = 0x13;
d[5] = cause;
d[6] = diagn;
return x25_exec(card, X25_DISCONNECT_REQUEST, link, d, 7, NULL, 0);
}
/* Send X.25 data packet. */
static int x25_send (cycx_t *card, u8 link, u8 lcn, u8 bitm, int len, void *buf)
{
u8 d[] = "?\xFF\x10??";
d[0] = d[3] = lcn;
d[4] = bitm;
return x25_exec(card, X25_DATA_REQUEST, link, &d, 5, buf, len);
}
/* Miscellaneous */
/* Find network device by its channel number. */
static struct net_device *get_dev_by_lcn (wan_device_t *wandev, s16 lcn)
{
struct net_device *dev = wandev->dev;
x25_channel_t *chan;
while (dev) {
if (chan->lcn == lcn)
break;
dev = chan->slave;
}
return dev;
}
/* Find network device by its remote dte address. */
static struct net_device *get_dev_by_dte_addr (wan_device_t *wandev, char *dte)
{
struct net_device *dev = wandev->dev;
x25_channel_t *chan;
while (dev) {
if (!strcmp(chan->addr, dte))
break;
dev = chan->slave;
}
return dev;
}
/* Initiate connection on the logical channel.
* o for PVC we just get channel configuration
* o for SVCs place an X.25 call
*
* Return: 0 connected
* >0 connection in progress
* <0 failure */
static int chan_connect (struct net_device *dev)
{
x25_channel_t *chan = dev->priv;
cycx_t *card = chan->card;
if (chan->svc) {
if (!chan->addr[0])
return -EINVAL; /* no destination address */
dprintk(KERN_INFO "%s: placing X.25 call to %s...\n",
card->devname, chan->addr);
if (x25_place_call(card, chan))
return -EIO;
set_chan_state(dev, WAN_CONNECTING);
return 1;
} else
set_chan_state(dev, WAN_CONNECTED);
return 0;
}
/* Disconnect logical channel.
* o if SVC then clear X.25 call */
static void chan_disconnect (struct net_device *dev)
{
x25_channel_t *chan = dev->priv;
if (chan->svc) {
x25_clear_call(chan->card, chan->link, chan->lcn, 0, 0);
set_chan_state(dev, WAN_DISCONNECTING);
} else
set_chan_state(dev, WAN_DISCONNECTED);
}
/* Called by kernel timer */
static void chan_timer (unsigned long d)
{
struct net_device *dev = (struct net_device *)d;
x25_channel_t *chan = dev->priv;
if (chan->state == WAN_CONNECTED)
chan_disconnect(dev);
else
printk(KERN_ERR "%s: chan_timer for svc (%s) not connected!\n",
chan->card->devname, dev->name);
}
/* Set logical channel state. */
static void set_chan_state (struct net_device *dev, u8 state)
{
x25_channel_t *chan = dev->priv;
cycx_t *card = chan->card;
u32 flags = 0;
char *string_state = NULL;
spin_lock_irqsave(&card->lock, flags);
if (chan->state != state) {
if (chan->svc && chan->state == WAN_CONNECTED)
del_timer(&chan->timer);
switch (state) {
case WAN_CONNECTED:
string_state = "connected!";
*(u16*)dev->dev_addr = htons(chan->lcn);
netif_wake_queue(dev);
reset_timer(dev);
if (chan->protocol == ETH_P_X25)
chan_x25_send_event(dev, 1);
break;
case WAN_CONNECTING:
string_state = "connecting...";
break;
case WAN_DISCONNECTING:
string_state = "disconnecting...";
break;
case WAN_DISCONNECTED:
string_state = "disconnected!";
if (chan->svc) {
*(unsigned short*)dev->dev_addr = 0;
chan->lcn = 0;
}
if (chan->protocol == ETH_P_X25)
chan_x25_send_event(dev, 2);
netif_wake_queue(dev);
break;
}
printk (KERN_INFO "%s: interface %s %s\n", card->devname,
dev->name, string_state);
chan->state = state;
}
spin_unlock_irqrestore(&card->lock, flags);
}
/* Send packet on a logical channel.
* When this function is called, tx_skb field of the channel data space
* points to the transmit socket buffer. When transmission is complete,
* release socket buffer and reset 'tbusy' flag.
*
* Return: 0 - transmission complete
* 1 - busy
*
* Notes:
* 1. If packet length is greater than MTU for this channel, we'll fragment
* the packet into 'complete sequence' using M-bit.
* 2. When transmission is complete, an event notification should be issued
* to the router. */
static int chan_send (struct net_device *dev, struct sk_buff *skb)
{
x25_channel_t *chan = dev->priv;
cycx_t *card = chan->card;
int bitm = 0; /* final packet */
unsigned len = skb->len;
if (skb->len > card->wandev.mtu) {
len = card->wandev.mtu;
bitm = 0x10; /* set M-bit (more data) */
}
if (x25_send(card, chan->link, chan->lcn, bitm, len, skb->data))
return 1;
if (bitm) {
skb_pull(skb, len);
return 1;
}
++chan->ifstats.tx_packets;
chan->ifstats.tx_bytes += len;
return 0;
}
/* Send event (connection, disconnection, etc) to X.25 socket layer */
static void chan_x25_send_event(struct net_device *dev, u8 event)
{
struct sk_buff *skb;
unsigned char *ptr;
if ((skb = dev_alloc_skb(1)) == NULL) {
printk(KERN_ERR __FUNCTION__ ": out of memory\n");
return;
}
ptr = skb_put(skb, 1);
*ptr = event;
skb->dev = dev;
skb->protocol = htons(ETH_P_X25);
skb->mac.raw = skb->data;
skb->pkt_type = PACKET_HOST;
netif_rx(skb);
}
/* Convert line speed in bps to a number used by cyclom 2x code. */
static u8 bps_to_speed_code (u32 bps)
{
u8 number = 0; /* defaults to the lowest (1200) speed ;> */
if (bps >= 512000) number = 8;
else if (bps >= 256000) number = 7;
else if (bps >= 64000) number = 6;
else if (bps >= 38400) number = 5;
else if (bps >= 19200) number = 4;
else if (bps >= 9600) number = 3;
else if (bps >= 4800) number = 2;
else if (bps >= 2400) number = 1;
return number;
}
/* log base 2 */
static u8 log2 (u32 n)
{
u8 log = 0;
if (!n)
return 0;
while (n > 1) {
n >>= 1;
++log;
}
return log;
}
/* Convert decimal string to unsigned integer.
* If len != 0 then only 'len' characters of the string are converted. */
static unsigned dec_to_uint (u8 *str, int len)
{
unsigned val = 0;
if (!len)
len = strlen(str);
for (; len && is_digit(*str); ++str, --len)
val = (val * 10) + (*str - (unsigned) '0');
return val;
}
static void reset_timer(struct net_device *dev)
{
x25_channel_t *chan = dev->priv;
if (chan->svc) {
del_timer(&chan->timer);
chan->timer.expires = jiffies + chan->idle_tmout * HZ;
add_timer(&chan->timer);
}
}
#ifdef CYCLOMX_X25_DEBUG
static void x25_dump_config(TX25Config *conf)
{
printk(KERN_INFO "X.25 configuration\n");
printk(KERN_INFO "-----------------\n");
printk(KERN_INFO "link number=%d\n", conf->link);
printk(KERN_INFO "line speed=%d\n", conf->speed);
printk(KERN_INFO "clock=%sternal\n", conf->clock == 8 ? "Ex" : "In");
printk(KERN_INFO "# level 2 retransm.=%d\n", conf->n2);
printk(KERN_INFO "level 2 window=%d\n", conf->n2win);
printk(KERN_INFO "level 3 window=%d\n", conf->n3win);
printk(KERN_INFO "# logical channels=%d\n", conf->nvc);
printk(KERN_INFO "level 3 pkt len=%d\n", conf->pktlen);
printk(KERN_INFO "my address=%d\n", conf->locaddr);
printk(KERN_INFO "remote address=%d\n", conf->remaddr);
printk(KERN_INFO "t1=%d seconds\n", conf->t1);
printk(KERN_INFO "t2=%d seconds\n", conf->t2);
printk(KERN_INFO "t21=%d seconds\n", conf->t21);
printk(KERN_INFO "# PVCs=%d\n", conf->npvc);
printk(KERN_INFO "t23=%d seconds\n", conf->t23);
printk(KERN_INFO "flags=0x%x\n", conf->flags);
}
static void x25_dump_stats(TX25Stats *stats)
{
printk(KERN_INFO "X.25 statistics\n");
printk(KERN_INFO "--------------\n");
printk(KERN_INFO "rx_crc_errors=%d\n", stats->rx_crc_errors);
printk(KERN_INFO "rx_over_errors=%d\n", stats->rx_over_errors);
printk(KERN_INFO "n2_tx_frames=%d\n", stats->n2_tx_frames);
printk(KERN_INFO "n2_rx_frames=%d\n", stats->n2_rx_frames);
printk(KERN_INFO "tx_timeouts=%d\n", stats->tx_timeouts);
printk(KERN_INFO "rx_timeouts=%d\n", stats->rx_timeouts);
printk(KERN_INFO "n3_tx_packets=%d\n", stats->n3_tx_packets);
printk(KERN_INFO "n3_rx_packets=%d\n", stats->n3_rx_packets);
printk(KERN_INFO "tx_aborts=%d\n", stats->tx_aborts);
printk(KERN_INFO "rx_aborts=%d\n", stats->rx_aborts);
}
static void x25_dump_devs(wan_device_t *wandev)
{
struct net_device *dev = wandev->dev;
printk(KERN_INFO "X.25 dev states\n");
printk(KERN_INFO "name: addr: txoff: protocol:\n");
printk(KERN_INFO "---------------------------------------\n");
while(dev) {
x25_channel_t *chan = dev->priv;
printk(KERN_INFO "%-5.5s %-15.15s %d ETH_P_%s\n",
chan->name, chan->addr, test_bit(LINK_STATE_XOFF, &dev->state),
chan->protocol == ETH_P_IP ? "IP" : "X25");
dev = chan->slave;
}
}
#endif /* CYCLOMX_X25_DEBUG */
/* End */