]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/staging/altpciechdma/altpciechdma.c
Staging: remove duplicated #include's
[linux-2.6-omap-h63xx.git] / drivers / staging / altpciechdma / altpciechdma.c
1 /**
2  * Driver for Altera PCIe core chaining DMA reference design.
3  *
4  * Copyright (C) 2008 Leon Woestenberg  <leon.woestenberg@axon.tv>
5  * Copyright (C) 2008 Nickolas Heppermann  <heppermannwdt@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  *
22  * Rationale: This driver exercises the chaining DMA read and write engine
23  * in the reference design. It is meant as a complementary reference
24  * driver that can be used for testing early designs as well as a basis to
25  * write your custom driver.
26  *
27  * Status: Test results from Leon Woestenberg  <leon.woestenberg@axon.tv>:
28  *
29  * Sendero Board w/ Cyclone II EP2C35F672C6N, PX1011A PCIe x1 PHY on a
30  * Dell Precision 370 PC, x86, kernel 2.6.20 from Ubuntu 7.04.
31  *
32  * Sendero Board w/ Cyclone II EP2C35F672C6N, PX1011A PCIe x1 PHY on a
33  * Freescale MPC8313E-RDB board, PowerPC, 2.6.24 w/ Freescale patches.
34  *
35  * Driver tests passed with PCIe Compiler 8.1. With PCIe 8.0 the DMA
36  * loopback test had reproducable compare errors. I assume a change
37  * in the compiler or reference design, but could not find evidence nor
38  * documentation on a change or fix in that direction.
39  *
40  * The reference design does not have readable locations and thus a
41  * dummy read, used to flush PCI posted writes, cannot be performed.
42  *
43  */
44
45 #include <linux/kernel.h>
46 #include <linux/cdev.h>
47 #include <linux/delay.h>
48 #include <linux/dma-mapping.h>
49 #include <linux/init.h>
50 #include <linux/interrupt.h>
51 #include <linux/io.h>
52 #include <linux/jiffies.h>
53 #include <linux/module.h>
54 #include <linux/pci.h>
55
56
57 /* by default do not build the character device interface */
58 /* XXX It is non-functional yet */
59 #ifndef ALTPCIECHDMA_CDEV
60 #  define ALTPCIECHDMA_CDEV 0
61 #endif
62
63 /* build the character device interface? */
64 #if ALTPCIECHDMA_CDEV
65 #  define MAX_CHDMA_SIZE (8 * 1024 * 1024)
66 #  include "mapper_user_to_sg.h"
67 #endif
68
69 /** driver name, mimicks Altera naming of the reference design */
70 #define DRV_NAME "altpciechdma"
71 /** number of BARs on the device */
72 #define APE_BAR_NUM (6)
73 /** BAR number where the RCSLAVE memory sits */
74 #define APE_BAR_RCSLAVE (0)
75 /** BAR number where the Descriptor Header sits */
76 #define APE_BAR_HEADER (2)
77
78 /** maximum size in bytes of the descriptor table, chdma logic limit */
79 #define APE_CHDMA_TABLE_SIZE (4096)
80 /* single transfer must not exceed 255 table entries. worst case this can be
81  * achieved by 255 scattered pages, with only a single byte in the head and
82  * tail pages. 253 * PAGE_SIZE is a safe upper bound for the transfer size.
83  */
84 #define APE_CHDMA_MAX_TRANSFER_LEN (253 * PAGE_SIZE)
85
86 /**
87  * Specifies those BARs to be mapped and the length of each mapping.
88  *
89  * Zero (0) means do not map, otherwise specifies the BAR lengths to be mapped.
90  * If the actual BAR length is less, this is considered an error; then
91  * reconfigure your PCIe core.
92  *
93  * @see ug_pci_express 8.0, table 7-2 at page 7-13.
94  */
95 static const unsigned long bar_min_len[APE_BAR_NUM] =
96         { 32768, 0, 256, 0, 32768, 0 };
97
98 /**
99  * Descriptor Header, controls the DMA read engine or write engine.
100  *
101  * The descriptor header is the main data structure for starting DMA transfers.
102  *
103  * It sits in End Point (FPGA) memory BAR[2] for 32-bit or BAR[3:2] for 64-bit.
104  * It references a descriptor table which exists in Root Complex (PC) memory.
105  * Writing the rclast field starts the DMA operation, thus all other structures
106  * and fields must be setup before doing so.
107  *
108  * @see ug_pci_express 8.0, tables 7-3, 7-4 and 7-5 at page 7-14.
109  * @note This header must be written in four 32-bit (PCI DWORD) writes.
110  */
111 struct ape_chdma_header {
112         /**
113          * w0 consists of two 16-bit fields:
114          * lsb u16 number; number of descriptors in ape_chdma_table
115          * msb u16 control; global control flags
116          */
117         u32 w0;
118         /* bus address to ape_chdma_table in Root Complex memory */
119         u32 bdt_addr_h;
120         u32 bdt_addr_l;
121         /**
122          * w3 consists of two 16-bit fields:
123          * - lsb u16 rclast; last descriptor number available in Root Complex
124          *    - zero (0) means the first descriptor is ready,
125          *    - one (1) means two descriptors are ready, etc.
126          * - msb u16 reserved;
127          *
128          * @note writing to this memory location starts the DMA operation!
129          */
130         u32 w3;
131 } __attribute__ ((packed));
132
133 /**
134  * Descriptor Entry, describing a (non-scattered) single memory block transfer.
135  *
136  * There is one descriptor for each memory block involved in the transfer, a
137  * block being a contiguous address range on the bus.
138  *
139  * Multiple descriptors are chained by means of the ape_chdma_table data
140  * structure.
141  *
142  * @see ug_pci_express 8.0, tables 7-6, 7-7 and 7-8 at page 7-14 and page 7-15.
143  */
144 struct ape_chdma_desc {
145         /**
146          * w0 consists of two 16-bit fields:
147          * number of DWORDS to transfer
148          * - lsb u16 length;
149          * global control
150          * - msb u16 control;
151          */
152         u32 w0;
153         /* address of memory in the End Point */
154         u32 ep_addr;
155         /* bus address of source or destination memory in the Root Complex */
156         u32 rc_addr_h;
157         u32 rc_addr_l;
158 } __attribute__ ((packed));
159
160 /**
161  * Descriptor Table, an array of descriptors describing a chained transfer.
162  *
163  * An array of descriptors, preceded by workspace for the End Point.
164  * It exists in Root Complex memory.
165  *
166  * The End Point can update its last completed descriptor number in the
167  * eplast field if requested by setting the EPLAST_ENA bit either
168  * globally in the header's or locally in any descriptor's control field.
169  *
170  * @note this structure may not exceed 4096 bytes. This results in a
171  * maximum of 4096 / (4 * 4) - 1 = 255 descriptors per chained transfer.
172  *
173  * @see ug_pci_express 8.0, tables 7-9, 7-10 and 7-11 at page 7-17 and page 7-18.
174  */
175 struct ape_chdma_table {
176         /* workspace 0x00-0x0b, reserved */
177         u32 reserved1[3];
178         /* workspace 0x0c-0x0f, last descriptor handled by End Point */
179         u32 w3;
180         /* the actual array of descriptors
181     * 0x10-0x1f, 0x20-0x2f, ... 0xff0-0xfff (255 entries)
182     */
183         struct ape_chdma_desc desc[255];
184 } __attribute__ ((packed));
185
186 /**
187  * Altera PCI Express ('ape') board specific book keeping data
188  *
189  * Keeps state of the PCIe core and the Chaining DMA controller
190  * application.
191  */
192 struct ape_dev {
193         /** the kernel pci device data structure provided by probe() */
194         struct pci_dev *pci_dev;
195         /**
196          * kernel virtual address of the mapped BAR memory and IO regions of
197          * the End Point. Used by map_bars()/unmap_bars().
198          */
199         void * __iomem bar[APE_BAR_NUM];
200         /** kernel virtual address for Descriptor Table in Root Complex memory */
201         struct ape_chdma_table *table_virt;
202         /**
203          * bus address for the Descriptor Table in Root Complex memory, in
204          * CPU-native endianess
205          */
206         dma_addr_t table_bus;
207         /* if the device regions could not be allocated, assume and remember it
208          * is in use by another driver; this driver must not disable the device.
209          */
210         int in_use;
211         /* whether this driver enabled msi for the device */
212         int msi_enabled;
213         /* whether this driver could obtain the regions */
214         int got_regions;
215         /* irq line succesfully requested by this driver, -1 otherwise */
216         int irq_line;
217         /* board revision */
218         u8 revision;
219         /* interrupt count, incremented by the interrupt handler */
220         int irq_count;
221 #if ALTPCIECHDMA_CDEV
222         /* character device */
223         dev_t cdevno;
224         struct cdev cdev;
225         /* user space scatter gather mapper */
226         struct sg_mapping_t *sgm;
227 #endif
228 };
229
230 /**
231  * Using the subsystem vendor id and subsystem id, it is possible to
232  * distinguish between different cards bases around the same
233  * (third-party) logic core.
234  *
235  * Default Altera vendor and device ID's, and some (non-reserved)
236  * ID's are now used here that are used amongst the testers/developers.
237  */
238 static const struct pci_device_id ids[] = {
239         { PCI_DEVICE(0x1172, 0xE001), },
240         { PCI_DEVICE(0x2071, 0x2071), },
241         { 0, }
242 };
243 MODULE_DEVICE_TABLE(pci, ids);
244
245 #if ALTPCIECHDMA_CDEV
246 /* prototypes for character device */
247 static int sg_init(struct ape_dev *ape);
248 static void sg_exit(struct ape_dev *ape);
249 #endif
250
251 /**
252  * altpciechdma_isr() - Interrupt handler
253  *
254  */
255 static irqreturn_t altpciechdma_isr(int irq, void *dev_id)
256 {
257         struct ape_dev *ape = (struct ape_dev *)dev_id;
258         if (!ape)
259                 return IRQ_NONE;
260         ape->irq_count++;
261         return IRQ_HANDLED;
262 }
263
264 static int __devinit scan_bars(struct ape_dev *ape, struct pci_dev *dev)
265 {
266         int i;
267         for (i = 0; i < APE_BAR_NUM; i++) {
268                 unsigned long bar_start = pci_resource_start(dev, i);
269                 if (bar_start) {
270                         unsigned long bar_end = pci_resource_end(dev, i);
271                         unsigned long bar_flags = pci_resource_flags(dev, i);
272                         printk(KERN_DEBUG "BAR%d 0x%08lx-0x%08lx flags 0x%08lx\n",
273                           i, bar_start, bar_end, bar_flags);
274                 }
275         }
276         return 0;
277 }
278
279 /**
280  * Unmap the BAR regions that had been mapped earlier using map_bars()
281  */
282 static void unmap_bars(struct ape_dev *ape, struct pci_dev *dev)
283 {
284         int i;
285         for (i = 0; i < APE_BAR_NUM; i++) {
286           /* is this BAR mapped? */
287                 if (ape->bar[i]) {
288                         /* unmap BAR */
289                         pci_iounmap(dev, ape->bar[i]);
290                         ape->bar[i] = NULL;
291                 }
292         }
293 }
294
295 /**
296  * Map the device memory regions into kernel virtual address space after
297  * verifying their sizes respect the minimum sizes needed, given by the
298  * bar_min_len[] array.
299  */
300 static int __devinit map_bars(struct ape_dev *ape, struct pci_dev *dev)
301 {
302         int rc;
303         int i;
304         /* iterate through all the BARs */
305         for (i = 0; i < APE_BAR_NUM; i++) {
306                 unsigned long bar_start = pci_resource_start(dev, i);
307                 unsigned long bar_end = pci_resource_end(dev, i);
308                 unsigned long bar_length = bar_end - bar_start + 1;
309                 ape->bar[i] = NULL;
310                 /* do not map, and skip, BARs with length 0 */
311                 if (!bar_min_len[i])
312                         continue;
313                 /* do not map BARs with address 0 */
314                 if (!bar_start || !bar_end) {
315             printk(KERN_DEBUG "BAR #%d is not present?!\n", i);
316                         rc = -1;
317                         goto fail;
318                 }
319                 bar_length = bar_end - bar_start + 1;
320                 /* BAR length is less than driver requires? */
321                 if (bar_length < bar_min_len[i]) {
322             printk(KERN_DEBUG "BAR #%d length = %lu bytes but driver "
323             "requires at least %lu bytes\n", i, bar_length, bar_min_len[i]);
324                         rc = -1;
325                         goto fail;
326                 }
327                 /* map the device memory or IO region into kernel virtual
328                  * address space */
329                 ape->bar[i] = pci_iomap(dev, i, bar_min_len[i]);
330                 if (!ape->bar[i]) {
331                         printk(KERN_DEBUG "Could not map BAR #%d.\n", i);
332                         rc = -1;
333                         goto fail;
334                 }
335         printk(KERN_DEBUG "BAR[%d] mapped at 0x%p with length %lu(/%lu).\n", i,
336                         ape->bar[i], bar_min_len[i], bar_length);
337         }
338         /* succesfully mapped all required BAR regions */
339         rc = 0;
340         goto success;
341 fail:
342         /* unmap any BARs that we did map */
343         unmap_bars(ape, dev);
344 success:
345         return rc;
346 }
347
348 #if 0 /* not yet implemented fully FIXME add opcode */
349 static void __devinit rcslave_test(struct ape_dev *ape, struct pci_dev *dev)
350 {
351         u32 *rcslave_mem = (u32 *)ape->bar[APE_BAR_RCSLAVE];
352         u32 result = 0;
353         /** this number is assumed to be different each time this test runs */
354         u32 seed = (u32)jiffies;
355         u32 value = seed;
356         int i;
357
358         /* write loop */
359         value = seed;
360         for (i = 1024; i < 32768 / 4 ; i++) {
361                 printk(KERN_DEBUG "Writing 0x%08x to 0x%p.\n",
362                         (u32)value, (void *)rcslave_mem + i);
363                 iowrite32(value, rcslave_mem + i);
364                 value++;
365         }
366         /* read-back loop */
367         value = seed;
368         for (i = 1024; i < 32768 / 4; i++) {
369                 result = ioread32(rcslave_mem + i);
370                 if (result != value) {
371                         printk(KERN_DEBUG "Wrote 0x%08x to 0x%p, but read back 0x%08x.\n",
372                                 (u32)value, (void *)rcslave_mem + i, (u32)result);
373                         break;
374                 }
375                 value++;
376         }
377 }
378 #endif
379
380 /* obtain the 32 most significant (high) bits of a 32-bit or 64-bit address */
381 #define pci_dma_h(addr) ((addr >> 16) >> 16)
382 /* obtain the 32 least significant (low) bits of a 32-bit or 64-bit address */
383 #define pci_dma_l(addr) (addr & 0xffffffffUL)
384
385 /* ape_fill_chdma_desc() - Fill a Altera PCI Express Chaining DMA descriptor
386  *
387  * @desc pointer to descriptor to be filled
388  * @addr root complex address
389  * @ep_addr end point address
390  * @len number of bytes, must be a multiple of 4.
391  */
392 static inline void ape_chdma_desc_set(struct ape_chdma_desc *desc, dma_addr_t addr, u32 ep_addr, int len)
393 {
394   BUG_ON(len & 3);
395         desc->w0 = cpu_to_le32(len / 4);
396         desc->ep_addr = cpu_to_le32(ep_addr);
397         desc->rc_addr_h = cpu_to_le32(pci_dma_h(addr));
398         desc->rc_addr_l = cpu_to_le32(pci_dma_l(addr));
399 }
400
401 /*
402  * ape_sg_to_chdma_table() - Create a device descriptor table from a scatterlist.
403  *
404  * The scatterlist must have been mapped by pci_map_sg(sgm->sgl).
405  *
406  * @sgl scatterlist.
407  * @nents Number of entries in the scatterlist.
408  * @first Start index in the scatterlist sgm->sgl.
409  * @ep_addr End Point address for the scatter/gather transfer.
410  * @desc pointer to first descriptor
411  *
412  * Returns Number of entries in the table on success, -1 on error.
413  */
414 static int ape_sg_to_chdma_table(struct scatterlist *sgl, int nents, int first, struct ape_chdma_desc *desc, u32 ep_addr)
415 {
416         int i = first, j = 0;
417         /* inspect first entry */
418         dma_addr_t addr = sg_dma_address(&sgl[i]);
419         unsigned int len = sg_dma_len(&sgl[i]);
420         /* contiguous block */
421         dma_addr_t cont_addr = addr;
422         unsigned int cont_len = len;
423         /* iterate over remaining entries */
424         for (; j < 25 && i < nents - 1; i++) {
425                 /* bus address of next entry i + 1 */
426                 dma_addr_t next = sg_dma_address(&sgl[i + 1]);
427                 /* length of this entry i */
428                 len = sg_dma_len(&sgl[i]);
429                 printk(KERN_DEBUG "%04d: addr=0x%08x length=0x%08x\n", i, addr, len);
430                 /* entry i + 1 is non-contiguous with entry i? */
431                 if (next != addr + len) {
432                         /* TODO create entry here (we could overwrite i) */
433                         printk(KERN_DEBUG "%4d: cont_addr=0x%08x cont_len=0x%08x\n", j, cont_addr, cont_len);
434                         /* set descriptor for contiguous transfer */
435                         ape_chdma_desc_set(&desc[j], cont_addr, ep_addr, cont_len);
436                         /* next end point memory address */
437                         ep_addr += cont_len;
438                         /* start new contiguous block */
439                         cont_addr = next;
440                         cont_len = 0;
441                         j++;
442                 }
443                 /* add entry i + 1 to current contiguous block */
444                 cont_len += len;
445                 /* goto entry i + 1 */
446                 addr = next;
447         }
448         /* TODO create entry here  (we could overwrite i) */
449         printk(KERN_DEBUG "%04d: addr=0x%08x length=0x%08x\n", i, addr, len);
450         printk(KERN_DEBUG "%4d: cont_addr=0x%08x length=0x%08x\n", j, cont_addr, cont_len);
451         j++;
452         return j;
453 }
454
455 /* compare buffers */
456 static inline int compare(u32 *p, u32 *q, int len)
457 {
458         int result = -1;
459         int fail = 0;
460         int i;
461         for (i = 0; i < len / 4; i++) {
462                 if (*p == *q) {
463                         /* every so many u32 words, show equals */
464                         if ((i & 255) == 0)
465                                 printk(KERN_DEBUG "[%p] = 0x%08x    [%p] = 0x%08x\n", p, *p, q, *q);
466                 } else {
467                         fail++;
468                         /* show the first few miscompares */
469                         if (fail < 10) {
470                 printk(KERN_DEBUG "[%p] = 0x%08x != [%p] = 0x%08x ?!\n", p, *p, q, *q);
471             /* but stop after a while */
472             } else if (fail == 10) {
473                 printk(KERN_DEBUG "---more errors follow! not printed---\n");
474                         } else {
475                                 /* stop compare after this many errors */
476                 break;
477             }
478                 }
479                 p++;
480                 q++;
481         }
482         if (!fail)
483                 result = 0;
484         return result;
485 }
486
487 /* dma_test() - Perform DMA loop back test to end point and back to root complex.
488  *
489  * Allocate a cache-coherent buffer in host memory, consisting of four pages.
490  *
491  * Fill the four memory pages such that each 32-bit word contains its own address.
492  *
493  * Now perform a loop back test, have the end point device copy the first buffer
494  * half to end point memory, then have it copy back into the second half.
495  *
496  *   Create a descriptor table to copy the first buffer half into End Point
497  *   memory. Instruct the End Point to do a DMA read using that table.
498  *
499  *   Create a descriptor table to copy End Point memory to the second buffer
500  *   half. Instruct the End Point to do a DMA write using that table.
501  *
502  * Compare results, fail or pass.
503  *
504  */
505 static int __devinit dma_test(struct ape_dev *ape, struct pci_dev *dev)
506 {
507         /* test result; guilty until proven innocent */
508         int result = -1;
509         /* the DMA read header sits at address 0x00 of the DMA engine BAR */
510         struct ape_chdma_header *write_header = (struct ape_chdma_header *)ape->bar[APE_BAR_HEADER];
511         /* the write DMA header sits after the read header at address 0x10 */
512         struct ape_chdma_header *read_header = write_header + 1;
513         /* virtual address of the allocated buffer */
514         u8 *buffer_virt = 0;
515         /* bus address of the allocated buffer */
516         dma_addr_t buffer_bus = 0;
517         int i, n = 0, irq_count;
518
519         /* temporary value used to construct 32-bit data words */
520         u32 w;
521
522         printk(KERN_DEBUG "bar_tests(), PAGE_SIZE = 0x%0x\n", (int)PAGE_SIZE);
523         printk(KERN_DEBUG "write_header = 0x%p.\n", write_header);
524         printk(KERN_DEBUG "read_header = 0x%p.\n", read_header);
525         printk(KERN_DEBUG "&write_header->w3 = 0x%p\n", &write_header->w3);
526         printk(KERN_DEBUG "&read_header->w3 = 0x%p\n", &read_header->w3);
527         printk(KERN_DEBUG "ape->table_virt = 0x%p.\n", ape->table_virt);
528
529         if (!write_header || !read_header || !ape->table_virt)
530         goto fail;
531
532         /* allocate and map coherently-cached memory for a DMA-able buffer */
533         /* @see Documentation/PCI/PCI-DMA-mapping.txt, near line 318 */
534         buffer_virt = (u8 *)pci_alloc_consistent(dev, PAGE_SIZE * 4, &buffer_bus);
535         if (!buffer_virt) {
536                 printk(KERN_DEBUG "Could not allocate coherent DMA buffer.\n");
537                 goto fail;
538         }
539         printk(KERN_DEBUG "Allocated cache-coherent DMA buffer (virtual address = 0x%016llx, bus address = 0x%016llx).\n",
540                 (u64)buffer_virt, (u64)buffer_bus);
541
542         /* fill first half of buffer with its virtual address as data */
543         for (i = 0; i < 4 * PAGE_SIZE; i += 4)
544 #if 0
545                 *(u32 *)(buffer_virt + i) = i / PAGE_SIZE + 1;
546 #else
547                 *(u32 *)(buffer_virt + i) = (buffer_virt + i);
548 #endif
549 #if 0
550   compare((u32 *)buffer_virt, (u32 *)(buffer_virt + 2 * PAGE_SIZE), 8192);
551 #endif
552
553 #if 0
554         /* fill second half of buffer with zeroes */
555         for (i = 2 * PAGE_SIZE; i < 4 * PAGE_SIZE; i += 4)
556                 *(u32 *)(buffer_virt + i) = 0;
557 #endif
558
559         /* invalidate EPLAST, outside 0-255, 0xFADE is from the testbench */
560         ape->table_virt->w3 = cpu_to_le32(0x0000FADE);
561
562         /* fill in first descriptor */
563         n = 0;
564         /* read 8192 bytes from RC buffer to EP address 4096 */
565         ape_chdma_desc_set(&ape->table_virt->desc[n], buffer_bus, 4096, 2 * PAGE_SIZE);
566 #if 1
567         for (i = 0; i < 255; i++) {
568                 ape_chdma_desc_set(&ape->table_virt->desc[i], buffer_bus, 4096, 2 * PAGE_SIZE);
569         }
570         /* index of last descriptor */
571         n = i - 1;
572 #endif
573 #if 0
574         /* fill in next descriptor */
575         n++;
576         /* read 1024 bytes from RC buffer to EP address 4096 + 1024 */
577         ape_chdma_desc_set(&ape->table_virt->desc[n], buffer_bus + 1024, 4096 + 1024, 1024);
578 #endif
579
580 #if 1
581         /* enable MSI after the last descriptor is completed */
582         if (ape->msi_enabled)
583                 ape->table_virt->desc[n].w0 |= cpu_to_le32(1UL << 16)/*local MSI*/;
584 #endif
585 #if 0
586         /* dump descriptor table for debugging */
587         printk(KERN_DEBUG "Descriptor Table (Read, in Root Complex Memory, # = %d)\n", n + 1);
588         for (i = 0; i < 4 + (n + 1) * 4; i += 4) {
589                 u32 *p = (u32 *)ape->table_virt;
590                 p += i;
591                 printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (LEN=0x%x)\n", (u32)p, (u32)p & 15, *p, 4 * le32_to_cpu(*p));
592                 p++;
593                 printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (EPA=0x%x)\n", (u32)p, (u32)p & 15, *p, le32_to_cpu(*p));
594                 p++;
595                 printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (RCH=0x%x)\n", (u32)p, (u32)p & 15, *p, le32_to_cpu(*p));
596                 p++;
597                 printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (RCL=0x%x)\n", (u32)p, (u32)p & 15, *p, le32_to_cpu(*p));
598         }
599 #endif
600         /* set available number of descriptors in table */
601         w = (u32)(n + 1);
602         w |= (1UL << 18)/*global EPLAST_EN*/;
603 #if 0
604         if (ape->msi_enabled)
605                 w |= (1UL << 17)/*global MSI*/;
606 #endif
607         printk(KERN_DEBUG "writing 0x%08x to 0x%p\n", w, (void *)&read_header->w0);
608         iowrite32(w, &read_header->w0);
609
610         /* write table address (higher 32-bits) */
611         printk(KERN_DEBUG "writing 0x%08x to 0x%p\n", (u32)((ape->table_bus >> 16) >> 16), (void *)&read_header->bdt_addr_h);
612         iowrite32(pci_dma_h(ape->table_bus), &read_header->bdt_addr_h);
613
614         /* write table address (lower 32-bits) */
615         printk(KERN_DEBUG "writing 0x%08x to 0x%p\n", (u32)(ape->table_bus & 0xffffffffUL), (void *)&read_header->bdt_addr_l);
616         iowrite32(pci_dma_l(ape->table_bus), &read_header->bdt_addr_l);
617
618         /* memory write barrier */
619         wmb();
620         printk(KERN_DEBUG "Flush posted writes\n");
621         /** FIXME Add dummy read to flush posted writes but need a readable location! */
622 #if 0
623         (void)ioread32();
624 #endif
625
626         /* remember IRQ count before the transfer */
627         irq_count = ape->irq_count;
628         /* write number of descriptors - this starts the DMA */
629         printk(KERN_DEBUG "\nStart DMA read\n");
630         printk(KERN_DEBUG "writing 0x%08x to 0x%p\n", (u32)n, (void *)&read_header->w3);
631         iowrite32(n, &read_header->w3);
632         printk(KERN_DEBUG "EPLAST = %lu\n", le32_to_cpu(*(u32 *)&ape->table_virt->w3) & 0xffffUL);
633
634         /** memory write barrier */
635         wmb();
636         /* dummy read to flush posted writes */
637         /* FIXME Need a readable location! */
638 #if 0
639         (void)ioread32();
640 #endif
641         printk(KERN_DEBUG "POLL FOR READ:\n");
642         /* poll for chain completion, 1000 times 1 millisecond */
643         for (i = 0; i < 100; i++) {
644                 volatile u32 *p = &ape->table_virt->w3;
645                 u32 eplast = le32_to_cpu(*p) & 0xffffUL;
646                 printk(KERN_DEBUG "EPLAST = %u, n = %d\n", eplast, n);
647                 if (eplast == n) {
648                         printk(KERN_DEBUG "DONE\n");
649             /* print IRQ count before the transfer */
650                         printk(KERN_DEBUG "#IRQs during transfer: %d\n", ape->irq_count - irq_count);
651                         break;
652                 }
653                 udelay(100);
654         }
655
656         /* invalidate EPLAST, outside 0-255, 0xFADE is from the testbench */
657         ape->table_virt->w3 = cpu_to_le32(0x0000FADE);
658
659         /* setup first descriptor */
660         n = 0;
661         ape_chdma_desc_set(&ape->table_virt->desc[n], buffer_bus + 8192, 4096, 2 * PAGE_SIZE);
662 #if 1
663         for (i = 0; i < 255; i++) {
664                 ape_chdma_desc_set(&ape->table_virt->desc[i], buffer_bus + 8192, 4096, 2 * PAGE_SIZE);
665         }
666         /* index of last descriptor */
667         n = i - 1;
668 #endif
669 #if 1 /* test variable, make a module option later */
670         if (ape->msi_enabled)
671                 ape->table_virt->desc[n].w0 |= cpu_to_le32(1UL << 16)/*local MSI*/;
672 #endif
673 #if 0
674         /* dump descriptor table for debugging */
675         printk(KERN_DEBUG "Descriptor Table (Write, in Root Complex Memory, # = %d)\n", n + 1);
676         for (i = 0; i < 4 + (n + 1) * 4; i += 4) {
677                 u32 *p = (u32 *)ape->table_virt;
678                 p += i;
679                 printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (LEN=0x%x)\n", (u32)p, (u32)p & 15, *p, 4 * le32_to_cpu(*p));
680                 p++;
681                 printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (EPA=0x%x)\n", (u32)p, (u32)p & 15, *p, le32_to_cpu(*p));
682                 p++;
683                 printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (RCH=0x%x)\n", (u32)p, (u32)p & 15, *p, le32_to_cpu(*p));
684                 p++;
685                 printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (RCL=0x%x)\n", (u32)p, (u32)p & 15, *p, le32_to_cpu(*p));
686         }
687 #endif
688
689         /* set number of available descriptors in the table */
690         w = (u32)(n + 1);
691         /* enable updates of eplast for each descriptor completion */
692         w |= (u32)(1UL << 18)/*global EPLAST_EN*/;
693 #if 0 // test variable, make a module option later
694         /* enable MSI for each descriptor completion */
695         if (ape->msi_enabled)
696                 w |= (1UL << 17)/*global MSI*/;
697 #endif
698         iowrite32(w, &write_header->w0);
699         iowrite32(pci_dma_h(ape->table_bus), &write_header->bdt_addr_h);
700         iowrite32(pci_dma_l(ape->table_bus), &write_header->bdt_addr_l);
701
702         /** memory write barrier and flush posted writes */
703         wmb();
704         /* dummy read to flush posted writes */
705         /* FIXME Need a readable location! */
706 #if 0
707         (void)ioread32();
708 #endif
709         irq_count = ape->irq_count;
710
711         printk(KERN_DEBUG "\nStart DMA write\n");
712         iowrite32(n, &write_header->w3);
713
714         /** memory write barrier */
715         wmb();
716         /** dummy read to flush posted writes */
717         //(void)ioread32();
718
719         printk(KERN_DEBUG "POLL FOR WRITE:\n");
720         /* poll for completion, 1000 times 1 millisecond */
721         for (i = 0; i < 100; i++) {
722                 volatile u32 *p = &ape->table_virt->w3;
723                 u32 eplast = le32_to_cpu(*p) & 0xffffUL;
724                 printk(KERN_DEBUG "EPLAST = %u, n = %d\n", eplast, n);
725                 if (eplast == n) {
726                         printk(KERN_DEBUG "DONE\n");
727                         /* print IRQ count before the transfer */
728                         printk(KERN_DEBUG "#IRQs during transfer: %d\n", ape->irq_count - irq_count);
729                         break;
730                 }
731                 udelay(100);
732         }
733         /* soft-reset DMA write engine */
734         iowrite32(0x0000ffffUL, &write_header->w0);
735         /* soft-reset DMA read engine */
736         iowrite32(0x0000ffffUL, &read_header->w0);
737
738         /** memory write barrier */
739         wmb();
740         /* dummy read to flush posted writes */
741         /* FIXME Need a readable location! */
742 #if 0
743         (void)ioread32();
744 #endif
745         /* compare first half of buffer with second half, should be identical */
746         result = compare((u32 *)buffer_virt, (u32 *)(buffer_virt + 2 * PAGE_SIZE), 8192);
747         printk(KERN_DEBUG "DMA loop back test %s.\n", result ? "FAILED" : "PASSED");
748
749         pci_free_consistent(dev, 4 * PAGE_SIZE, buffer_virt, buffer_bus);
750 fail:
751         printk(KERN_DEBUG "bar_tests() end, result %d\n", result);
752         return result;
753 }
754
755 /* Called when the PCI sub system thinks we can control the given device.
756  * Inspect if we can support the device and if so take control of it.
757  *
758  * Return 0 when we have taken control of the given device.
759  *
760  * - allocate board specific bookkeeping
761  * - allocate coherently-mapped memory for the descriptor table
762  * - enable the board
763  * - verify board revision
764  * - request regions
765  * - query DMA mask
766  * - obtain and request irq
767  * - map regions into kernel address space
768  */
769 static int __devinit probe(struct pci_dev *dev, const struct pci_device_id *id)
770 {
771         int rc = 0;
772         struct ape_dev *ape = NULL;
773         u8 irq_pin, irq_line;
774         printk(KERN_DEBUG "probe(dev = 0x%p, pciid = 0x%p)\n", dev, id);
775
776         /* allocate memory for per-board book keeping */
777         ape = kzalloc(sizeof(struct ape_dev), GFP_KERNEL);
778         if (!ape) {
779                 printk(KERN_DEBUG "Could not kzalloc()ate memory.\n");
780                 goto err_ape;
781         }
782         ape->pci_dev = dev;
783         dev->dev.driver_data = (void *)ape;
784         printk(KERN_DEBUG "probe() ape = 0x%p\n", ape);
785
786         printk(KERN_DEBUG "sizeof(struct ape_chdma_table) = %d.\n",
787                 (int)sizeof(struct ape_chdma_table));
788         /* the reference design has a size restriction on the table size */
789         BUG_ON(sizeof(struct ape_chdma_table) > APE_CHDMA_TABLE_SIZE);
790
791         /* allocate and map coherently-cached memory for a descriptor table */
792         /* @see LDD3 page 446 */
793         ape->table_virt = (struct ape_chdma_table *)pci_alloc_consistent(dev,
794                 APE_CHDMA_TABLE_SIZE, &ape->table_bus);
795         /* could not allocate table? */
796         if (!ape->table_virt) {
797                 printk(KERN_DEBUG "Could not dma_alloc()ate_coherent memory.\n");
798                 goto err_table;
799         }
800
801         printk(KERN_DEBUG "table_virt = 0x%16llx, table_bus = 0x%16llx.\n",
802                 (u64)ape->table_virt, (u64)ape->table_bus);
803
804         /* enable device */
805         rc = pci_enable_device(dev);
806         if (rc) {
807                 printk(KERN_DEBUG "pci_enable_device() failed\n");
808                 goto err_enable;
809         }
810
811         /* enable bus master capability on device */
812         pci_set_master(dev);
813         /* enable message signaled interrupts */
814         rc = pci_enable_msi(dev);
815         /* could not use MSI? */
816         if (rc) {
817                 /* resort to legacy interrupts */
818                 printk(KERN_DEBUG "Could not enable MSI interrupting.\n");
819                 ape->msi_enabled = 0;
820         /* MSI enabled, remember for cleanup */
821         } else {
822                 printk(KERN_DEBUG "Enabled MSI interrupting.\n");
823                 ape->msi_enabled = 1;
824         }
825
826         pci_read_config_byte(dev, PCI_REVISION_ID, &ape->revision);
827 #if 0 /* example */
828         /* (for example) this driver does not support revision 0x42 */
829     if (ape->revision == 0x42) {
830                 printk(KERN_DEBUG "Revision 0x42 is not supported by this driver.\n");
831                 rc = -ENODEV;
832                 goto err_rev;
833         }
834 #endif
835         /** XXX check for native or legacy PCIe endpoint? */
836
837         rc = pci_request_regions(dev, DRV_NAME);
838         /* could not request all regions? */
839         if (rc) {
840                 /* assume device is in use (and do not disable it later!) */
841                 ape->in_use = 1;
842                 goto err_regions;
843         }
844         ape->got_regions = 1;
845
846 #if 1 // @todo For now, disable 64-bit, because I do not understand the implications (DAC!)
847         /* query for DMA transfer */
848         /* @see Documentation/PCI/PCI-DMA-mapping.txt */
849         if (!pci_set_dma_mask(dev, DMA_64BIT_MASK)) {
850                 pci_set_consistent_dma_mask(dev, DMA_64BIT_MASK);
851                 /* use 64-bit DMA */
852                 printk(KERN_DEBUG "Using a 64-bit DMA mask.\n");
853         } else
854 #endif
855         if (!pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
856                 printk(KERN_DEBUG "Could not set 64-bit DMA mask.\n");
857                 pci_set_consistent_dma_mask(dev, DMA_32BIT_MASK);
858                 /* use 32-bit DMA */
859                 printk(KERN_DEBUG "Using a 32-bit DMA mask.\n");
860         } else {
861                 printk(KERN_DEBUG "No suitable DMA possible.\n");
862                 /** @todo Choose proper error return code */
863                 rc = -1;
864                 goto err_mask;
865         }
866
867         rc = pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq_pin);
868         /* could not read? */
869         if (rc)
870                 goto err_irq;
871         printk(KERN_DEBUG "IRQ pin #%d (0=none, 1=INTA#...4=INTD#).\n", irq_pin);
872
873         /* @see LDD3, page 318 */
874         rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq_line);
875         /* could not read? */
876         if (rc) {
877                 printk(KERN_DEBUG "Could not query PCI_INTERRUPT_LINE, error %d\n", rc);
878                 goto err_irq;
879         }
880         printk(KERN_DEBUG "IRQ line #%d.\n", irq_line);
881 #if 1
882         irq_line = dev->irq;
883         /* @see LDD3, page 259 */
884         rc = request_irq(irq_line, altpciechdma_isr, IRQF_SHARED, DRV_NAME, (void *)ape);
885         if (rc) {
886                 printk(KERN_DEBUG "Could not request IRQ #%d, error %d\n", irq_line, rc);
887                 ape->irq_line = -1;
888                 goto err_irq;
889         }
890         /* remember which irq we allocated */
891         ape->irq_line = (int)irq_line;
892         printk(KERN_DEBUG "Succesfully requested IRQ #%d with dev_id 0x%p\n", irq_line, ape);
893 #endif
894         /* show BARs */
895         scan_bars(ape, dev);
896         /* map BARs */
897         rc = map_bars(ape, dev);
898         if (rc)
899                 goto err_map;
900 #if ALTPCIECHDMA_CDEV
901         /* initialize character device */
902         rc = sg_init(ape);
903         if (rc)
904                 goto err_cdev;
905 #endif
906         /* perform DMA engines loop back test */
907         rc = dma_test(ape, dev);
908         (void)rc;
909         /* succesfully took the device */
910         rc = 0;
911         printk(KERN_DEBUG "probe() successful.\n");
912         goto end;
913 err_cdev:
914         /* unmap the BARs */
915         unmap_bars(ape, dev);
916 err_map:
917         /* free allocated irq */
918         if (ape->irq_line >= 0)
919                 free_irq(ape->irq_line, (void *)ape);
920 err_irq:
921         if (ape->msi_enabled)
922                 pci_disable_msi(dev);
923         /* disable the device iff it is not in use */
924         if (!ape->in_use)
925                 pci_disable_device(dev);
926         if (ape->got_regions)
927                 pci_release_regions(dev);
928 err_mask:
929 err_regions:
930 err_rev:
931 /* clean up everything before device enable() */
932 err_enable:
933         if (ape->table_virt)
934                 pci_free_consistent(dev, APE_CHDMA_TABLE_SIZE, ape->table_virt, ape->table_bus);
935 /* clean up everything before allocating descriptor table */
936 err_table:
937         if (ape)
938                 kfree(ape);
939 err_ape:
940 end:
941         return rc;
942 }
943
944 static void __devexit remove(struct pci_dev *dev)
945 {
946         struct ape_dev *ape;
947         printk(KERN_DEBUG "remove(0x%p)\n", dev);
948         if ((dev == 0) || (dev->dev.driver_data == 0)) {
949                 printk(KERN_DEBUG "remove(dev = 0x%p) dev->dev.driver_data = 0x%p\n", dev, dev->dev.driver_data);
950                 return;
951         }
952         ape = (struct ape_dev *)dev->dev.driver_data;
953         printk(KERN_DEBUG "remove(dev = 0x%p) where dev->dev.driver_data = 0x%p\n", dev, ape);
954         if (ape->pci_dev != dev) {
955                 printk(KERN_DEBUG "dev->dev.driver_data->pci_dev (0x%08lx) != dev (0x%08lx)\n",
956                 (unsigned long)ape->pci_dev, (unsigned long)dev);
957         }
958         /* remove character device */
959 #if ALTPCIECHDMA_CDEV
960         sg_exit(ape);
961 #endif
962
963         if (ape->table_virt)
964                 pci_free_consistent(dev, APE_CHDMA_TABLE_SIZE, ape->table_virt, ape->table_bus);
965
966         /* free IRQ
967          * @see LDD3 page 279
968          */
969         if (ape->irq_line >= 0) {
970                 printk(KERN_DEBUG "Freeing IRQ #%d for dev_id 0x%08lx.\n",
971                 ape->irq_line, (unsigned long)ape);
972                 free_irq(ape->irq_line, (void *)ape);
973         }
974         /* MSI was enabled? */
975         if (ape->msi_enabled) {
976                 /* Disable MSI @see Documentation/MSI-HOWTO.txt */
977                 pci_disable_msi(dev);
978                 ape->msi_enabled = 0;
979         }
980         /* unmap the BARs */
981         unmap_bars(ape, dev);
982         if (!ape->in_use)
983                 pci_disable_device(dev);
984         if (ape->got_regions)
985                 /* to be called after device disable */
986                 pci_release_regions(dev);
987 }
988
989 #if ALTPCIECHDMA_CDEV
990
991 /*
992  * Called when the device goes from unused to used.
993  */
994 static int sg_open(struct inode *inode, struct file *file)
995 {
996         struct ape_dev *ape;
997         printk(KERN_DEBUG DRV_NAME "_open()\n");
998         /* pointer to containing data structure of the character device inode */
999         ape = container_of(inode->i_cdev, struct ape_dev, cdev);
1000         /* create a reference to our device state in the opened file */
1001         file->private_data = ape;
1002         /* create virtual memory mapper */
1003         ape->sgm = sg_create_mapper(MAX_CHDMA_SIZE);
1004         return 0;
1005 }
1006
1007 /*
1008  * Called when the device goes from used to unused.
1009  */
1010 static int sg_close(struct inode *inode, struct file *file)
1011 {
1012         /* fetch device specific data stored earlier during open */
1013         struct ape_dev *ape = (struct ape_dev *)file->private_data;
1014         printk(KERN_DEBUG DRV_NAME "_close()\n");
1015         /* destroy virtual memory mapper */
1016         sg_destroy_mapper(ape->sgm);
1017         return 0;
1018 }
1019
1020 static ssize_t sg_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
1021 {
1022         /* fetch device specific data stored earlier during open */
1023         struct ape_dev *ape = (struct ape_dev *)file->private_data;
1024         (void)ape;
1025         printk(KERN_DEBUG DRV_NAME "_read(buf=0x%p, count=%lld, pos=%llu)\n", buf, (s64)count, (u64)*pos);
1026         return count;
1027 }
1028
1029 /* sg_write() - Write to the device
1030  *
1031  * @buf userspace buffer
1032  * @count number of bytes in the userspace buffer
1033  *
1034  * Iterate over the userspace buffer, taking at most 255 * PAGE_SIZE bytes for
1035  * each DMA transfer.
1036  *   For each transfer, get the user pages, build a sglist, map, build a
1037  *   descriptor table. submit the transfer. wait for the interrupt handler
1038  *   to wake us on completion.
1039  */
1040 static ssize_t sg_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
1041 {
1042         int hwnents, tents;
1043         size_t transfer_len, remaining = count, done = 0;
1044         u64 transfer_addr = (u64)buf;
1045         /* fetch device specific data stored earlier during open */
1046         struct ape_dev *ape = (struct ape_dev *)file->private_data;
1047         printk(KERN_DEBUG DRV_NAME "_write(buf=0x%p, count=%lld, pos=%llu)\n",
1048                 buf, (s64)count, (u64)*pos);
1049         /* TODO transfer boundaries at PAGE_SIZE granularity */
1050         while (remaining > 0)
1051         {
1052                 /* limit DMA transfer size */
1053                 transfer_len = (remaining < APE_CHDMA_MAX_TRANSFER_LEN)? remaining:
1054                         APE_CHDMA_MAX_TRANSFER_LEN;
1055                 /* get all user space buffer pages and create a scattergather list */
1056                 sgm_map_user_pages(ape->sgm, transfer_addr, transfer_len, 0/*read from userspace*/);
1057                 printk(KERN_DEBUG DRV_NAME "mapped_pages=%d\n", ape->sgm->mapped_pages);
1058                 /* map all entries in the scattergather list */
1059                 hwnents = pci_map_sg(ape->pci_dev, ape->sgm->sgl, ape->sgm->mapped_pages, DMA_TO_DEVICE);
1060                 printk(KERN_DEBUG DRV_NAME "hwnents=%d\n", hwnents);
1061                 /* build device descriptor tables and submit them to the DMA engine */
1062                 tents = ape_sg_to_chdma_table(ape->sgm->sgl, hwnents, 0, &ape->table_virt->desc[0], 4096);
1063                 printk(KERN_DEBUG DRV_NAME "tents=%d\n", hwnents);
1064 #if 0
1065                 while (tables) {
1066                         /* TODO build table */
1067                         /* TODO submit table to the device */
1068                         /* if engine stopped and unfinished work then start engine */
1069                 }
1070                 put ourselves on wait queue
1071 #endif
1072
1073                 dma_unmap_sg(NULL, ape->sgm->sgl, ape->sgm->mapped_pages, DMA_TO_DEVICE);
1074                 /* dirty and free the pages */
1075                 sgm_unmap_user_pages(ape->sgm, 1/*dirtied*/);
1076                 /* book keeping */
1077                 transfer_addr += transfer_len;
1078                 remaining -= transfer_len;
1079                 done += transfer_len;
1080         }
1081         return done;
1082 }
1083
1084 /*
1085  * character device file operations
1086  */
1087 static struct file_operations sg_fops = {
1088   .owner = THIS_MODULE,
1089   .open = sg_open,
1090   .release = sg_close,
1091   .read = sg_read,
1092   .write = sg_write,
1093 };
1094
1095 /* sg_init() - Initialize character device
1096  *
1097  * XXX Should ideally be tied to the device, on device probe, not module init.
1098  */
1099 static int sg_init(struct ape_dev *ape)
1100 {
1101         int rc;
1102         printk(KERN_DEBUG DRV_NAME " sg_init()\n");
1103         /* allocate a dynamically allocated character device node */
1104         rc = alloc_chrdev_region(&ape->cdevno, 0/*requested minor*/, 1/*count*/, DRV_NAME);
1105         /* allocation failed? */
1106         if (rc < 0) {
1107                 printk("alloc_chrdev_region() = %d\n", rc);
1108                 goto fail_alloc;
1109         }
1110         /* couple the device file operations to the character device */
1111         cdev_init(&ape->cdev, &sg_fops);
1112         ape->cdev.owner = THIS_MODULE;
1113         /* bring character device live */
1114         rc = cdev_add(&ape->cdev, ape->cdevno, 1/*count*/);
1115         if (rc < 0) {
1116                 printk("cdev_add() = %d\n", rc);
1117                 goto fail_add;
1118         }
1119         printk(KERN_DEBUG "altpciechdma = %d:%d\n", MAJOR(ape->cdevno), MINOR(ape->cdevno));
1120         return 0;
1121 fail_add:
1122         /* free the dynamically allocated character device node */
1123     unregister_chrdev_region(ape->cdevno, 1/*count*/);
1124 fail_alloc:
1125         return -1;
1126 }
1127
1128 /* sg_exit() - Cleanup character device
1129  *
1130  * XXX Should ideally be tied to the device, on device remove, not module exit.
1131  */
1132
1133 static void sg_exit(struct ape_dev *ape)
1134 {
1135         printk(KERN_DEBUG DRV_NAME " sg_exit()\n");
1136         /* remove the character device */
1137         cdev_del(&ape->cdev);
1138         /* free the dynamically allocated character device node */
1139         unregister_chrdev_region(ape->cdevno, 1/*count*/);
1140 }
1141
1142 #endif /* ALTPCIECHDMA_CDEV */
1143
1144 /* used to register the driver with the PCI kernel sub system
1145  * @see LDD3 page 311
1146  */
1147 static struct pci_driver pci_driver = {
1148         .name = DRV_NAME,
1149         .id_table = ids,
1150         .probe = probe,
1151         .remove = remove,
1152         /* resume, suspend are optional */
1153 };
1154
1155 /**
1156  * alterapciechdma_init() - Module initialization, registers devices.
1157  */
1158 static int __init alterapciechdma_init(void)
1159 {
1160   int rc = 0;
1161         printk(KERN_DEBUG DRV_NAME " init(), built at " __DATE__ " " __TIME__ "\n");
1162         /* register this driver with the PCI bus driver */
1163         rc = pci_register_driver(&pci_driver);
1164         if (rc < 0)
1165           return rc;
1166         return 0;
1167 }
1168
1169 /**
1170  * alterapciechdma_init() - Module cleanup, unregisters devices.
1171  */
1172 static void __exit alterapciechdma_exit(void)
1173 {
1174         printk(KERN_DEBUG DRV_NAME " exit(), built at " __DATE__ " " __TIME__ "\n");
1175         /* unregister this driver from the PCI bus driver */
1176         pci_unregister_driver(&pci_driver);
1177 }
1178
1179 MODULE_LICENSE("GPL");
1180
1181 module_init(alterapciechdma_init);
1182 module_exit(alterapciechdma_exit);
1183