device.rs 10.3 KB
Newer Older
1
use std::mem;
2 3

use dma::Dma;
4
use io::{Mmio, Io, ReadOnly};
5 6 7 8 9 10
use syscall::error::{Error, EACCES, EWOULDBLOCK, Result};
use syscall::scheme::SchemeMut;

#[repr(packed)]
struct Regs {
    mac: [Mmio<u32>; 2],
11 12 13 14 15
    _mar: [Mmio<u32>; 2],
    _dtccr: [Mmio<u32>; 2],
    _rsv0: [Mmio<u32>; 2],
    tnpds: [Mmio<u32>; 2],
    thpds: [Mmio<u32>; 2],
16 17
    _rsv1: [Mmio<u8>; 7],
    cmd: Mmio<u8>,
18
    tppoll: Mmio<u8>,
19 20 21 22 23
    _rsv2: [Mmio<u8>; 3],
    imr: Mmio<u16>,
    isr: Mmio<u16>,
    tcr: Mmio<u32>,
    rcr: Mmio<u32>,
24
    _tctr: Mmio<u32>,
25 26
    _rsv3: Mmio<u32>,
    cmd_9346: Mmio<u8>,
27
    _config: [Mmio<u8>; 6],
28
    _rsv4: Mmio<u8>,
29
    _timer_int: Mmio<u32>,
30
    _rsv5: Mmio<u32>,
31 32
    _phys_ar: Mmio<u32>,
    _rsv6: [Mmio<u32>; 2],
33 34
    phys_sts: ReadOnly<Mmio<u8>>,
    _rsv7: [Mmio<u8>; 23],
35 36
    _wakeup: [Mmio<u32>; 16],
    _crc: [Mmio<u16>; 5],
37 38 39
    _rsv8: [Mmio<u8>; 12],
    rms: Mmio<u16>,
    _rsv9: Mmio<u32>,
40
    _c_plus_cr: Mmio<u16>,
41
    _rsv10: Mmio<u16>,
42
    rdsar: [Mmio<u32>; 2],
43 44 45 46
    mtps: Mmio<u8>,
    _rsv11: [Mmio<u8>; 19],
}

47 48 49 50
const OWN: u32 = 1 << 31;
const EOR: u32 = 1 << 30;
const FS: u32 = 1 << 29;
const LS: u32 = 1 << 28;
51 52 53

#[repr(packed)]
struct Rd {
54 55
    ctrl: Mmio<u32>,
    _vlan: Mmio<u32>,
56 57 58 59 60
    buffer: Mmio<u64>
}

#[repr(packed)]
struct Td {
61 62
    ctrl: Mmio<u32>,
    _vlan: Mmio<u32>,
63 64 65 66 67 68 69 70
    buffer: Mmio<u64>
}

pub struct Rtl8168 {
    regs: &'static mut Regs,
    irq: u8,
    receive_buffer: [Dma<[u8; 0x1FF8]>; 16],
    receive_ring: Dma<[Rd; 16]>,
Jeremy Soller's avatar
Jeremy Soller committed
71
    transmit_buffer: [Dma<[u8; 7552]>; 16],
72
    transmit_ring: Dma<[Td; 16]>,
Jeremy Soller's avatar
Jeremy Soller committed
73
    transmit_buffer_h: [Dma<[u8; 7552]>; 1],
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    transmit_ring_h: Dma<[Td; 1]>
}

impl SchemeMut for Rtl8168 {
    fn open(&mut self, _path: &[u8], _flags: usize, uid: u32, _gid: u32) -> Result<usize> {
        if uid == 0 {
            Ok(0)
        } else {
            Err(Error::new(EACCES))
        }
    }

    fn dup(&mut self, id: usize) -> Result<usize> {
        Ok(id)
    }

    fn read(&mut self, _id: usize, buf: &mut [u8]) -> Result<usize> {
        println!("Try Receive {}", buf.len());
        for (rd_i, rd) in self.receive_ring.iter_mut().enumerate() {
93 94
            if ! rd.ctrl.readf(OWN) {
                let rd_len = rd.ctrl.read() & 0x3FFF;
95

96 97 98
                println!("Receive {}: {}", rd_i, rd_len);

                let data = &self.receive_buffer[rd_i as usize];
99 100

                let mut i = 0;
101
                while i < buf.len() && i < rd_len as usize {
102 103 104 105
                    buf[i] = data[i];
                    i += 1;
                }

106 107
                let eor = rd.ctrl.read() & EOR;
                rd.ctrl.write(OWN | eor | data.len() as u32);
108 109 110 111 112 113 114 115 116 117 118 119

                return Ok(i);
            }
        }

        Err(Error::new(EWOULDBLOCK))
    }

    fn write(&mut self, _id: usize, buf: &[u8]) -> Result<usize> {
        println!("Try Transmit {}", buf.len());
        loop {
            for (td_i, td) in self.transmit_ring.iter_mut().enumerate() {
120
                if ! td.ctrl.readf(OWN) {
121 122 123 124 125 126 127 128 129 130
                    println!("Transmit {}: Setup {}", td_i, buf.len());

                    let mut data = &mut self.transmit_buffer[td_i as usize];

                    let mut i = 0;
                    while i < buf.len() && i < data.len() {
                        data[i] = buf[i];
                        i += 1;
                    }

Jeremy Soller's avatar
Jeremy Soller committed
131
                    println!("Transmit {}: Before: Control {:X}: Buffer {:X} TPPoll: {:X} ISR: {:X}", td_i, td.ctrl.read(), td.buffer.read(), self.regs.tppoll.read(), self.regs.isr.read());
132

133 134
                    let eor = td.ctrl.read() & EOR;
                    td.ctrl.write(OWN | eor | FS | LS | i as u32);
135 136 137

                    self.regs.tppoll.writef(1 << 6, true); //Notify of normal priority packet

Jeremy Soller's avatar
Jeremy Soller committed
138 139 140 141
                    println!("Transmit {}: During: Control {:X}: Buffer {:X} TPPoll: {:X} ISR: {:X}", td_i, td.ctrl.read(), td.buffer.read(), self.regs.tppoll.read(), self.regs.isr.read());

                    while self.regs.tppoll.readf(1 << 6) {
                        unsafe { asm!("pause" : : : "memory" : "intel", "volatile"); }
142 143
                    }

Jeremy Soller's avatar
Jeremy Soller committed
144
                    println!("Transmit {}: After: Control {:X}: Buffer {:X} TPPoll: {:X} ISR: {:X}", td_i, td.ctrl.read(), td.buffer.read(), self.regs.tppoll.read(), self.regs.isr.read());
145

146 147 148 149 150 151 152 153
                    return Ok(i);
                }
            }

            unsafe { asm!("pause" : : : "memory" : "intel", "volatile"); }
        }
    }

154 155 156 157
    fn fevent(&mut self, _id: usize, _flags: usize) -> Result<usize> {
        Ok(0)
    }

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    fn fsync(&mut self, _id: usize) -> Result<usize> {
        Ok(0)
    }

    fn close(&mut self, _id: usize) -> Result<usize> {
        Ok(0)
    }
}

impl Rtl8168 {
    pub unsafe fn new(base: usize, irq: u8) -> Result<Self> {
        assert_eq!(mem::size_of::<Regs>(), 256);

        let regs = &mut *(base as *mut Regs);
        assert_eq!(&regs.tnpds as *const _ as usize - base, 0x20);
        assert_eq!(&regs.cmd as *const _ as usize - base, 0x37);
        assert_eq!(&regs.tcr as *const _ as usize - base, 0x40);
        assert_eq!(&regs.rcr as *const _ as usize - base, 0x44);
        assert_eq!(&regs.cmd_9346 as *const _ as usize - base, 0x50);
        assert_eq!(&regs.phys_sts as *const _ as usize - base, 0x6C);
        assert_eq!(&regs.rms as *const _ as usize - base, 0xDA);
        assert_eq!(&regs.rdsar as *const _ as usize - base, 0xE4);
        assert_eq!(&regs.mtps as *const _ as usize - base, 0xEC);

        let mut module = Rtl8168 {
            regs: regs,
            irq: irq,
            receive_buffer: [Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?,
                            Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?,
                            Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?,
                            Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?],
            receive_ring: Dma::zeroed()?,
            transmit_buffer: [Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?,
                            Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?,
                            Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?,
                            Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?, Dma::zeroed()?],
            transmit_ring: Dma::zeroed()?,
            transmit_buffer_h: [Dma::zeroed()?],
            transmit_ring_h: Dma::zeroed()?
        };

        module.init();

        Ok(module)
    }

    pub unsafe fn irq(&mut self) -> u16 {
        // Read and then clear the ISR
        let isr = self.regs.isr.read();
        self.regs.isr.write(isr);
208 209
        let imr = self.regs.imr.read();
        isr & imr
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    }

    pub unsafe fn init(&mut self) {
        println!(" + RTL8168 on: {:X}, IRQ: {}", self.regs as *mut Regs as usize, self.irq);

        let mac_low = self.regs.mac[0].read();
        let mac_high = self.regs.mac[1].read();
        let mac = [mac_low as u8,
                    (mac_low >> 8) as u8,
                    (mac_low >> 16) as u8,
                    (mac_low >> 24) as u8,
                    mac_high as u8,
                    (mac_high >> 8) as u8];
        println!("   - MAC: {:>02X}:{:>02X}:{:>02X}:{:>02X}:{:>02X}:{:>02X}", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

        // Reset - this will disable tx and rx, reinitialize FIFOs, and set the system buffer pointer to the initial value
        self.regs.cmd.writef(1 << 4, true);
        while self.regs.cmd.readf(1 << 4) {}

        // Set up rx buffers
        for i in 0..self.receive_ring.len() {
231 232 233 234
            let rd = &mut self.receive_ring[i];
            let data = &mut self.receive_buffer[i];
            rd.ctrl.write(OWN | data.len() as u32);
            rd.buffer.write(data.physical() as u64);
235 236
        }
        if let Some(mut rd) = self.receive_ring.last_mut() {
237
            rd.ctrl.writef(EOR, true);
238 239 240 241 242 243 244
        }

        // Set up normal priority tx buffers
        for i in 0..self.transmit_ring.len() {
            self.transmit_ring[i].buffer.write(self.transmit_buffer[i].physical() as u64);
        }
        if let Some(mut td) = self.transmit_ring.last_mut() {
245
            td.ctrl.writef(EOR, true);
246 247 248 249 250 251 252
        }

        // Set up high priority tx buffers
        for i in 0..self.transmit_ring_h.len() {
            self.transmit_ring_h[i].buffer.write(self.transmit_buffer_h[i].physical() as u64);
        }
        if let Some(mut td) = self.transmit_ring_h.last_mut() {
253
            td.ctrl.writef(EOR, true);
254 255 256 257 258
        }

        // Unlock config
        self.regs.cmd_9346.write(1 << 7 | 1 << 6);

259 260
        // Enable rx (bit 3) and tx (bit 2)
        self.regs.cmd.writef(1 << 3 | 1 << 2, true);
261 262 263 264 265 266 267 268

        // Max RX packet size
        self.regs.rms.write(0x1FF8);

        // Max TX packet size
        self.regs.mtps.write(0x3B);

        // Set tx low priority buffer address
269 270
        self.regs.tnpds[0].write(self.transmit_ring.physical() as u32);
        self.regs.tnpds[1].write((self.transmit_ring.physical() >> 32) as u32);
271 272

        // Set tx high priority buffer address
273 274
        self.regs.thpds[0].write(self.transmit_ring_h.physical() as u32);
        self.regs.thpds[1].write((self.transmit_ring_h.physical() >> 32) as u32);
275 276

        // Set rx buffer address
277 278
        self.regs.rdsar[0].write(self.receive_ring.physical() as u32);
        self.regs.rdsar[1].write((self.receive_ring.physical() >> 32) as u32);
279

Jeremy Soller's avatar
Jeremy Soller committed
280 281 282 283
        //Clear ISR
        let isr = self.regs.isr.read();
        self.regs.isr.write(isr);

284
        // Interrupt on tx error (bit 3), tx ok (bit 2), rx error(bit 1), and rx ok (bit 0)
285
        self.regs.imr.write(1 << 15 | 1 << 14 | 1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 3 | 1 << 2 | 1 << 1 | 1);
286

Jeremy Soller's avatar
Jeremy Soller committed
287 288 289
        // Set TX config
        self.regs.tcr.write(0b11 << 24 | 0b111 << 8);

290
        // Set RX config - Accept broadcast (bit 3), multicast (bit 2), and unicast (bit 1)
291
        self.regs.rcr.write(0xE70E);
292

293 294 295
        // Lock config
        self.regs.cmd_9346.write(0);

296 297 298 299 300 301 302 303 304 305 306 307
        println!("   - Ready CMD {:X} ISR {:X} IMR {:X} PHYS {:X} RMS {:X} MTPS {:X} RCR {:X} TCR {:X} RDSAR {:X} TNPDS {:X} THPDS {:X}",
            self.regs.cmd.read(),
            self.regs.isr.read(),
            self.regs.imr.read(),
            self.regs.phys_sts.read(),
            self.regs.rms.read(),
            self.regs.mtps.read(),
            self.regs.rcr.read(),
            self.regs.tcr.read(),
            self.regs.rdsar[0].read(),
            self.regs.tnpds[0].read(),
            self.regs.thpds[0].read());
308 309
    }
}