Subject: pci_alloc_consistent() usage

From: "Vadivelan Mani" <[email-masked]>
Date: Tue, 13 Jul 2004 18:31:01 +0530

hi,

   I'm writing a wireless driver which requires 8 transmit and 8 receive 
buffers each of size 3KB approx.

These buffers should be in dma capable space.

I've allocated them using pci_alloc_consistent().

I've allocated 128KBytes just to make more space. I've got few doubts.

   1. Can pci_alloc_consistent() be used to allocate memory upto 128KBytes?

If this is not possible, how much can it allocate?

2.)   I would also like to know the exact use of this allocated space to 
transmit or receive a packet.

During transmission i do the following. Plz correct me if i'm wrong because 
i'm new to driver writing.

The device has a register which should be loaded with the transmit buffers 
starting address.

I copy the packet coming from the Kernel in the form of sk_buff structure 
into one of the transmit buffers that i've allocated using memcpy().
And i set the register in the device to initiate transmission of the packet.

Where does the dma transfer concept come in this?
There is no mention of the direction of data transfer in 
pci_alloc_consistent().

I also assumed that allocating buffer in dma capable space was itself enough 
to start dma transfers.

Since i do not have the device now i'm not able to test the code. But i 
would like to write the code before i get the device.

Kindly help me in this.

thanking u in advance.

regards,
vadi.

_________________________________________________________________
Marriage by choice.... http://www.shaadi.com/ptnr.php?ptnr=hmltag Log onto 
Shaadi.com

---

From: Ralf Baechle <[email-masked]>
Date: Tue, 13 Jul 2004 21:29:23 +0200

> buffers each of size 3KB approx.

I hope this device isn't really as simplistic as you make it sound ...

>   1. Can pci_alloc_consistent() be used to allocate memory upto 128KBytes?

Yes.  For MIPS MAX_ORDER defaults to 11 so you even do alloc_page (which
is the underlying allocator of pci_alloc_consistent) upto 2^11 page that
is 8MB.  Downside - memory allocation is making such large allocations
unreliable; the more unreliable the larger the allocation.  In general
try to stick to allocations of order 0 that is PAGE_SIZE which atm is
4k on MIPS.  They're ok for permanent allocations such as rx/tx rings
which only happen rarely.

> transmit or receive a packet.

pci_alloc_consistent() is meant to be used for permanent allocations
such as rx/tx rings.  It's not suitable for allocating skbufs; there are
other mechanisms available for that.

> into one of the transmit buffers that i've allocated using memcpy().

That's usually an idea only for very small packets.  pci_alloc_consistent
allocates uncached memory on a system withour hardware coherency so this
copy operation would be very slow.  In any case you should experiment to
find the breakeven point.

For larger packets you should use pci_map_single() in the start_xmit()
method of the driver, then pci_unmap_single() later when cleaning that is
typically in an interrupt handler.  Reception of packets would work
fairly similar.

> There is no mention of the direction of data transfer in 

pci_alloc_consistent will allocate consistent memory that is it's suitable
for transfers in both directions.  On the typical MIPS processor which
doesn't maintain coherency in hardware this means it will return
uncached memory.  Obviously that will work for any direction of transfer.
But: uncached memory is slow - so avoid copying packets there.

> I also assumed that allocating buffer in dma capable space was itself 
> enough to start dma transfers.

No.  DMA capable space means some memory that can be accessed somehow by
a DMA engine.  You still have to tell the device to start the operation.

> would like to write the code before i get the device.

  Ralf