linux/drivers/staging/altpciechdma/altpciechdma.c
<<
>>
Prefs
   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 */
  95static 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 */
 111struct 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 */
 144struct 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 */
 175struct 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 */
 192struct 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 */
 238static const struct pci_device_id ids[] = {
 239        { PCI_DEVICE(0x1172, 0xE001), },
 240        { PCI_DEVICE(0x2071, 0x2071), },
 241        { 0, }
 242};
 243MODULE_DEVICE_TABLE(pci, ids);
 244
 245#if ALTPCIECHDMA_CDEV
 246/* prototypes for character device */
 247static int sg_init(struct ape_dev *ape);
 248static void sg_exit(struct ape_dev *ape);
 249#endif
 250
 251/**
 252 * altpciechdma_isr() - Interrupt handler
 253 *
 254 */
 255static 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
 264static 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 */
 282static 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 */
 300static 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",
 324                        i, bar_length, bar_min_len[i]);
 325                        rc = -1;
 326                        goto fail;
 327                }
 328                /* map the device memory or IO region into kernel virtual
 329                 * address space */
 330                ape->bar[i] = pci_iomap(dev, i, bar_min_len[i]);
 331                if (!ape->bar[i]) {
 332                        printk(KERN_DEBUG "Could not map BAR #%d.\n", i);
 333                        rc = -1;
 334                        goto fail;
 335                }
 336                printk(KERN_DEBUG "BAR[%d] mapped at 0x%p with length %lu(/%lu).\n", i,
 337                ape->bar[i], bar_min_len[i], bar_length);
 338        }
 339        /* succesfully mapped all required BAR regions */
 340        rc = 0;
 341        goto success;
 342fail:
 343        /* unmap any BARs that we did map */
 344        unmap_bars(ape, dev);
 345success:
 346        return rc;
 347}
 348
 349#if 0 /* not yet implemented fully FIXME add opcode */
 350static void __devinit rcslave_test(struct ape_dev *ape, struct pci_dev *dev)
 351{
 352        u32 *rcslave_mem = (u32 *)ape->bar[APE_BAR_RCSLAVE];
 353        u32 result = 0;
 354        /** this number is assumed to be different each time this test runs */
 355        u32 seed = (u32)jiffies;
 356        u32 value = seed;
 357        int i;
 358
 359        /* write loop */
 360        value = seed;
 361        for (i = 1024; i < 32768 / 4 ; i++) {
 362                printk(KERN_DEBUG "Writing 0x%08x to 0x%p.\n",
 363                        (u32)value, (void *)rcslave_mem + i);
 364                iowrite32(value, rcslave_mem + i);
 365                value++;
 366        }
 367        /* read-back loop */
 368        value = seed;
 369        for (i = 1024; i < 32768 / 4; i++) {
 370                result = ioread32(rcslave_mem + i);
 371                if (result != value) {
 372                        printk(KERN_DEBUG "Wrote 0x%08x to 0x%p, but read back 0x%08x.\n",
 373                                (u32)value, (void *)rcslave_mem + i, (u32)result);
 374                        break;
 375                }
 376                value++;
 377        }
 378}
 379#endif
 380
 381/* obtain the 32 most significant (high) bits of a 32-bit or 64-bit address */
 382#define pci_dma_h(addr) ((addr >> 16) >> 16)
 383/* obtain the 32 least significant (low) bits of a 32-bit or 64-bit address */
 384#define pci_dma_l(addr) (addr & 0xffffffffUL)
 385
 386/* ape_fill_chdma_desc() - Fill a Altera PCI Express Chaining DMA descriptor
 387 *
 388 * @desc pointer to descriptor to be filled
 389 * @addr root complex address
 390 * @ep_addr end point address
 391 * @len number of bytes, must be a multiple of 4.
 392 */
 393static inline void ape_chdma_desc_set(struct ape_chdma_desc *desc, dma_addr_t addr, u32 ep_addr, int len)
 394{
 395  BUG_ON(len & 3);
 396        desc->w0 = cpu_to_le32(len / 4);
 397        desc->ep_addr = cpu_to_le32(ep_addr);
 398        desc->rc_addr_h = cpu_to_le32(pci_dma_h(addr));
 399        desc->rc_addr_l = cpu_to_le32(pci_dma_l(addr));
 400}
 401
 402#if ALTPCIECHDMA_CDEV
 403/*
 404 * ape_sg_to_chdma_table() - Create a device descriptor table from a scatterlist.
 405 *
 406 * The scatterlist must have been mapped by pci_map_sg(sgm->sgl).
 407 *
 408 * @sgl scatterlist.
 409 * @nents Number of entries in the scatterlist.
 410 * @first Start index in the scatterlist sgm->sgl.
 411 * @ep_addr End Point address for the scatter/gather transfer.
 412 * @desc pointer to first descriptor
 413 *
 414 * Returns Number of entries in the table on success, -1 on error.
 415 */
 416static int ape_sg_to_chdma_table(struct scatterlist *sgl, int nents, int first, struct ape_chdma_desc *desc, u32 ep_addr)
 417{
 418        int i = first, j = 0;
 419        /* inspect first entry */
 420        dma_addr_t addr = sg_dma_address(&sgl[i]);
 421        unsigned int len = sg_dma_len(&sgl[i]);
 422        /* contiguous block */
 423        dma_addr_t cont_addr = addr;
 424        unsigned int cont_len = len;
 425        /* iterate over remaining entries */
 426        for (; j < 25 && i < nents - 1; i++) {
 427                /* bus address of next entry i + 1 */
 428                dma_addr_t next = sg_dma_address(&sgl[i + 1]);
 429                /* length of this entry i */
 430                len = sg_dma_len(&sgl[i]);
 431                printk(KERN_DEBUG "%04d: addr=0x%Lx length=0x%08x\n", i,
 432                        (unsigned long long)addr, len);
 433                /* entry i + 1 is non-contiguous with entry i? */
 434                if (next != addr + len) {
 435                        /* TODO create entry here (we could overwrite i) */
 436                        printk(KERN_DEBUG "%4d: cont_addr=0x%Lx cont_len=0x%08x\n", j,
 437                                (unsigned long long)cont_addr, cont_len);
 438                        /* set descriptor for contiguous transfer */
 439                        ape_chdma_desc_set(&desc[j], cont_addr, ep_addr, cont_len);
 440                        /* next end point memory address */
 441                        ep_addr += cont_len;
 442                        /* start new contiguous block */
 443                        cont_addr = next;
 444                        cont_len = 0;
 445                        j++;
 446                }
 447                /* add entry i + 1 to current contiguous block */
 448                cont_len += len;
 449                /* goto entry i + 1 */
 450                addr = next;
 451        }
 452        /* TODO create entry here  (we could overwrite i) */
 453        printk(KERN_DEBUG "%04d: addr=0x%Lx length=0x%08x\n", i,
 454                (unsigned long long)addr, len);
 455        printk(KERN_DEBUG "%4d: cont_addr=0x%Lx length=0x%08x\n", j,
 456                (unsigned long long)cont_addr, cont_len);
 457        j++;
 458        return j;
 459}
 460#endif
 461
 462/* compare buffers */
 463static inline int compare(u32 *p, u32 *q, int len)
 464{
 465        int result = -1;
 466        int fail = 0;
 467        int i;
 468        for (i = 0; i < len / 4; i++) {
 469                if (*p == *q) {
 470                        /* every so many u32 words, show equals */
 471                        if ((i & 255) == 0)
 472                                printk(KERN_DEBUG "[%p] = 0x%08x    [%p] = 0x%08x\n", p, *p, q, *q);
 473                } else {
 474                        fail++;
 475                        /* show the first few miscompares */
 476                        if (fail < 10)
 477                                printk(KERN_DEBUG "[%p] = 0x%08x != [%p] = 0x%08x ?!\n", p, *p, q, *q);
 478                                /* but stop after a while */
 479                        else if (fail == 10)
 480                                printk(KERN_DEBUG "---more errors follow! not printed---\n");
 481                        else
 482                                /* stop compare after this many errors */
 483                        break;
 484                }
 485                p++;
 486                q++;
 487        }
 488        if (!fail)
 489                result = 0;
 490        return result;
 491}
 492
 493/* dma_test() - Perform DMA loop back test to end point and back to root complex.
 494 *
 495 * Allocate a cache-coherent buffer in host memory, consisting of four pages.
 496 *
 497 * Fill the four memory pages such that each 32-bit word contains its own address.
 498 *
 499 * Now perform a loop back test, have the end point device copy the first buffer
 500 * half to end point memory, then have it copy back into the second half.
 501 *
 502 *   Create a descriptor table to copy the first buffer half into End Point
 503 *   memory. Instruct the End Point to do a DMA read using that table.
 504 *
 505 *   Create a descriptor table to copy End Point memory to the second buffer
 506 *   half. Instruct the End Point to do a DMA write using that table.
 507 *
 508 * Compare results, fail or pass.
 509 *
 510 */
 511static int __devinit dma_test(struct ape_dev *ape, struct pci_dev *dev)
 512{
 513        /* test result; guilty until proven innocent */
 514        int result = -1;
 515        /* the DMA read header sits at address 0x00 of the DMA engine BAR */
 516        struct ape_chdma_header *write_header = (struct ape_chdma_header *)ape->bar[APE_BAR_HEADER];
 517        /* the write DMA header sits after the read header at address 0x10 */
 518        struct ape_chdma_header *read_header = write_header + 1;
 519        /* virtual address of the allocated buffer */
 520        u8 *buffer_virt = 0;
 521        /* bus address of the allocated buffer */
 522        dma_addr_t buffer_bus = 0;
 523        int i, n = 0, irq_count;
 524
 525        /* temporary value used to construct 32-bit data words */
 526        u32 w;
 527
 528        printk(KERN_DEBUG "bar_tests(), PAGE_SIZE = 0x%0x\n", (int)PAGE_SIZE);
 529        printk(KERN_DEBUG "write_header = 0x%p.\n", write_header);
 530        printk(KERN_DEBUG "read_header = 0x%p.\n", read_header);
 531        printk(KERN_DEBUG "&write_header->w3 = 0x%p\n", &write_header->w3);
 532        printk(KERN_DEBUG "&read_header->w3 = 0x%p\n", &read_header->w3);
 533        printk(KERN_DEBUG "ape->table_virt = 0x%p.\n", ape->table_virt);
 534
 535        if (!write_header || !read_header || !ape->table_virt)
 536                goto fail;
 537
 538        /* allocate and map coherently-cached memory for a DMA-able buffer */
 539        /* @see Documentation/PCI/PCI-DMA-mapping.txt, near line 318 */
 540        buffer_virt = (u8 *)pci_alloc_consistent(dev, PAGE_SIZE * 4, &buffer_bus);
 541        if (!buffer_virt) {
 542                printk(KERN_DEBUG "Could not allocate coherent DMA buffer.\n");
 543                goto fail;
 544        }
 545        printk(KERN_DEBUG "Allocated cache-coherent DMA buffer (virtual address = %p, bus address = 0x%016llx).\n",
 546               buffer_virt, (u64)buffer_bus);
 547
 548        /* fill first half of buffer with its virtual address as data */
 549        for (i = 0; i < 4 * PAGE_SIZE; i += 4)
 550#if 0
 551                *(u32 *)(buffer_virt + i) = i / PAGE_SIZE + 1;
 552#else
 553                *(u32 *)(buffer_virt + i) = (u32)(unsigned long)(buffer_virt + i);
 554#endif
 555#if 0
 556  compare((u32 *)buffer_virt, (u32 *)(buffer_virt + 2 * PAGE_SIZE), 8192);
 557#endif
 558
 559#if 0
 560        /* fill second half of buffer with zeroes */
 561        for (i = 2 * PAGE_SIZE; i < 4 * PAGE_SIZE; i += 4)
 562                *(u32 *)(buffer_virt + i) = 0;
 563#endif
 564
 565        /* invalidate EPLAST, outside 0-255, 0xFADE is from the testbench */
 566        ape->table_virt->w3 = cpu_to_le32(0x0000FADE);
 567
 568        /* fill in first descriptor */
 569        n = 0;
 570        /* read 8192 bytes from RC buffer to EP address 4096 */
 571        ape_chdma_desc_set(&ape->table_virt->desc[n], buffer_bus, 4096, 2 * PAGE_SIZE);
 572#if 1
 573        for (i = 0; i < 255; i++)
 574                ape_chdma_desc_set(&ape->table_virt->desc[i], buffer_bus, 4096, 2 * PAGE_SIZE);
 575        /* index of last descriptor */
 576        n = i - 1;
 577#endif
 578#if 0
 579        /* fill in next descriptor */
 580        n++;
 581        /* read 1024 bytes from RC buffer to EP address 4096 + 1024 */
 582        ape_chdma_desc_set(&ape->table_virt->desc[n], buffer_bus + 1024, 4096 + 1024, 1024);
 583#endif
 584
 585#if 1
 586        /* enable MSI after the last descriptor is completed */
 587        if (ape->msi_enabled)
 588                ape->table_virt->desc[n].w0 |= cpu_to_le32(1UL << 16)/*local MSI*/;
 589#endif
 590#if 0
 591        /* dump descriptor table for debugging */
 592        printk(KERN_DEBUG "Descriptor Table (Read, in Root Complex Memory, # = %d)\n", n + 1);
 593        for (i = 0; i < 4 + (n + 1) * 4; i += 4) {
 594                u32 *p = (u32 *)ape->table_virt;
 595                p += i;
 596                printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (LEN=0x%x)\n", (u32)p, (u32)p & 15, *p, 4 * le32_to_cpu(*p));
 597                p++;
 598                printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (EPA=0x%x)\n", (u32)p, (u32)p & 15, *p, le32_to_cpu(*p));
 599                p++;
 600                printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (RCH=0x%x)\n", (u32)p, (u32)p & 15, *p, le32_to_cpu(*p));
 601                p++;
 602                printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (RCL=0x%x)\n", (u32)p, (u32)p & 15, *p, le32_to_cpu(*p));
 603        }
 604#endif
 605        /* set available number of descriptors in table */
 606        w = (u32)(n + 1);
 607        w |= (1UL << 18)/*global EPLAST_EN*/;
 608#if 0
 609        if (ape->msi_enabled)
 610                w |= (1UL << 17)/*global MSI*/;
 611#endif
 612        printk(KERN_DEBUG "writing 0x%08x to 0x%p\n", w, (void *)&read_header->w0);
 613        iowrite32(w, &read_header->w0);
 614
 615        /* write table address (higher 32-bits) */
 616        printk(KERN_DEBUG "writing 0x%08x to 0x%p\n", (u32)((ape->table_bus >> 16) >> 16), (void *)&read_header->bdt_addr_h);
 617        iowrite32(pci_dma_h(ape->table_bus), &read_header->bdt_addr_h);
 618
 619        /* write table address (lower 32-bits) */
 620        printk(KERN_DEBUG "writing 0x%08x to 0x%p\n", (u32)(ape->table_bus & 0xffffffffUL), (void *)&read_header->bdt_addr_l);
 621        iowrite32(pci_dma_l(ape->table_bus), &read_header->bdt_addr_l);
 622
 623        /* memory write barrier */
 624        wmb();
 625        printk(KERN_DEBUG "Flush posted writes\n");
 626        /** FIXME Add dummy read to flush posted writes but need a readable location! */
 627#if 0
 628        (void)ioread32();
 629#endif
 630
 631        /* remember IRQ count before the transfer */
 632        irq_count = ape->irq_count;
 633        /* write number of descriptors - this starts the DMA */
 634        printk(KERN_DEBUG "\nStart DMA read\n");
 635        printk(KERN_DEBUG "writing 0x%08x to 0x%p\n", (u32)n, (void *)&read_header->w3);
 636        iowrite32(n, &read_header->w3);
 637        printk(KERN_DEBUG "EPLAST = %lu\n", le32_to_cpu(*(u32 *)&ape->table_virt->w3) & 0xffffUL);
 638
 639        /** memory write barrier */
 640        wmb();
 641        /* dummy read to flush posted writes */
 642        /* FIXME Need a readable location! */
 643#if 0
 644        (void)ioread32();
 645#endif
 646        printk(KERN_DEBUG "POLL FOR READ:\n");
 647        /* poll for chain completion, 1000 times 1 millisecond */
 648        for (i = 0; i < 100; i++) {
 649                volatile u32 *p = &ape->table_virt->w3;
 650                u32 eplast = le32_to_cpu(*p) & 0xffffUL;
 651                printk(KERN_DEBUG "EPLAST = %u, n = %d\n", eplast, n);
 652                if (eplast == n) {
 653                        printk(KERN_DEBUG "DONE\n");
 654                        /* print IRQ count before the transfer */
 655                        printk(KERN_DEBUG "#IRQs during transfer: %d\n", ape->irq_count - irq_count);
 656                        break;
 657                }
 658                udelay(100);
 659        }
 660
 661        /* invalidate EPLAST, outside 0-255, 0xFADE is from the testbench */
 662        ape->table_virt->w3 = cpu_to_le32(0x0000FADE);
 663
 664        /* setup first descriptor */
 665        n = 0;
 666        ape_chdma_desc_set(&ape->table_virt->desc[n], buffer_bus + 8192, 4096, 2 * PAGE_SIZE);
 667#if 1
 668        for (i = 0; i < 255; i++)
 669                ape_chdma_desc_set(&ape->table_virt->desc[i], buffer_bus + 8192, 4096, 2 * PAGE_SIZE);
 670
 671        /* index of last descriptor */
 672        n = i - 1;
 673#endif
 674#if 1 /* test variable, make a module option later */
 675        if (ape->msi_enabled)
 676                ape->table_virt->desc[n].w0 |= cpu_to_le32(1UL << 16)/*local MSI*/;
 677#endif
 678#if 0
 679        /* dump descriptor table for debugging */
 680        printk(KERN_DEBUG "Descriptor Table (Write, in Root Complex Memory, # = %d)\n", n + 1);
 681        for (i = 0; i < 4 + (n + 1) * 4; i += 4) {
 682                u32 *p = (u32 *)ape->table_virt;
 683                p += i;
 684                printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (LEN=0x%x)\n", (u32)p, (u32)p & 15, *p, 4 * le32_to_cpu(*p));
 685                p++;
 686                printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (EPA=0x%x)\n", (u32)p, (u32)p & 15, *p, le32_to_cpu(*p));
 687                p++;
 688                printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (RCH=0x%x)\n", (u32)p, (u32)p & 15, *p, le32_to_cpu(*p));
 689                p++;
 690                printk(KERN_DEBUG "0x%08x/0x%02x: 0x%08x (RCL=0x%x)\n", (u32)p, (u32)p & 15, *p, le32_to_cpu(*p));
 691        }
 692#endif
 693
 694        /* set number of available descriptors in the table */
 695        w = (u32)(n + 1);
 696        /* enable updates of eplast for each descriptor completion */
 697        w |= (u32)(1UL << 18)/*global EPLAST_EN*/;
 698#if 0   /* test variable, make a module option later */
 699        /* enable MSI for each descriptor completion */
 700        if (ape->msi_enabled)
 701                w |= (1UL << 17)/*global MSI*/;
 702#endif
 703        iowrite32(w, &write_header->w0);
 704        iowrite32(pci_dma_h(ape->table_bus), &write_header->bdt_addr_h);
 705        iowrite32(pci_dma_l(ape->table_bus), &write_header->bdt_addr_l);
 706
 707        /** memory write barrier and flush posted writes */
 708        wmb();
 709        /* dummy read to flush posted writes */
 710        /* FIXME Need a readable location! */
 711#if 0
 712        (void)ioread32();
 713#endif
 714        irq_count = ape->irq_count;
 715
 716        printk(KERN_DEBUG "\nStart DMA write\n");
 717        iowrite32(n, &write_header->w3);
 718
 719        /** memory write barrier */
 720        wmb();
 721        /** dummy read to flush posted writes */
 722        /* (void) ioread32(); */
 723
 724        printk(KERN_DEBUG "POLL FOR WRITE:\n");
 725        /* poll for completion, 1000 times 1 millisecond */
 726        for (i = 0; i < 100; i++) {
 727                volatile u32 *p = &ape->table_virt->w3;
 728                u32 eplast = le32_to_cpu(*p) & 0xffffUL;
 729                printk(KERN_DEBUG "EPLAST = %u, n = %d\n", eplast, n);
 730                if (eplast == n) {
 731                        printk(KERN_DEBUG "DONE\n");
 732                        /* print IRQ count before the transfer */
 733                        printk(KERN_DEBUG "#IRQs during transfer: %d\n", ape->irq_count - irq_count);
 734                        break;
 735                }
 736                udelay(100);
 737        }
 738        /* soft-reset DMA write engine */
 739        iowrite32(0x0000ffffUL, &write_header->w0);
 740        /* soft-reset DMA read engine */
 741        iowrite32(0x0000ffffUL, &read_header->w0);
 742
 743        /** memory write barrier */
 744        wmb();
 745        /* dummy read to flush posted writes */
 746        /* FIXME Need a readable location! */
 747#if 0
 748        (void)ioread32();
 749#endif
 750        /* compare first half of buffer with second half, should be identical */
 751        result = compare((u32 *)buffer_virt, (u32 *)(buffer_virt + 2 * PAGE_SIZE), 8192);
 752        printk(KERN_DEBUG "DMA loop back test %s.\n", result ? "FAILED" : "PASSED");
 753
 754        pci_free_consistent(dev, 4 * PAGE_SIZE, buffer_virt, buffer_bus);
 755fail:
 756        printk(KERN_DEBUG "bar_tests() end, result %d\n", result);
 757        return result;
 758}
 759
 760/* Called when the PCI sub system thinks we can control the given device.
 761 * Inspect if we can support the device and if so take control of it.
 762 *
 763 * Return 0 when we have taken control of the given device.
 764 *
 765 * - allocate board specific bookkeeping
 766 * - allocate coherently-mapped memory for the descriptor table
 767 * - enable the board
 768 * - verify board revision
 769 * - request regions
 770 * - query DMA mask
 771 * - obtain and request irq
 772 * - map regions into kernel address space
 773 */
 774static int __devinit probe(struct pci_dev *dev, const struct pci_device_id *id)
 775{
 776        int rc = 0;
 777        struct ape_dev *ape = NULL;
 778        u8 irq_pin, irq_line;
 779        printk(KERN_DEBUG "probe(dev = 0x%p, pciid = 0x%p)\n", dev, id);
 780
 781        /* allocate memory for per-board book keeping */
 782        ape = kzalloc(sizeof(struct ape_dev), GFP_KERNEL);
 783        if (!ape) {
 784                printk(KERN_DEBUG "Could not kzalloc()ate memory.\n");
 785                goto err_ape;
 786        }
 787        ape->pci_dev = dev;
 788        dev_set_drvdata(&dev->dev, ape);
 789        printk(KERN_DEBUG "probe() ape = 0x%p\n", ape);
 790
 791        printk(KERN_DEBUG "sizeof(struct ape_chdma_table) = %d.\n",
 792                (int)sizeof(struct ape_chdma_table));
 793        /* the reference design has a size restriction on the table size */
 794        BUG_ON(sizeof(struct ape_chdma_table) > APE_CHDMA_TABLE_SIZE);
 795
 796        /* allocate and map coherently-cached memory for a descriptor table */
 797        /* @see LDD3 page 446 */
 798        ape->table_virt = (struct ape_chdma_table *)pci_alloc_consistent(dev,
 799                APE_CHDMA_TABLE_SIZE, &ape->table_bus);
 800        /* could not allocate table? */
 801        if (!ape->table_virt) {
 802                printk(KERN_DEBUG "Could not dma_alloc()ate_coherent memory.\n");
 803                goto err_table;
 804        }
 805
 806        printk(KERN_DEBUG "table_virt = %p, table_bus = 0x%16llx.\n",
 807                ape->table_virt, (u64)ape->table_bus);
 808
 809        /* enable device */
 810        rc = pci_enable_device(dev);
 811        if (rc) {
 812                printk(KERN_DEBUG "pci_enable_device() failed\n");
 813                goto err_enable;
 814        }
 815
 816        /* enable bus master capability on device */
 817        pci_set_master(dev);
 818        /* enable message signaled interrupts */
 819        rc = pci_enable_msi(dev);
 820        /* could not use MSI? */
 821        if (rc) {
 822                /* resort to legacy interrupts */
 823                printk(KERN_DEBUG "Could not enable MSI interrupting.\n");
 824                ape->msi_enabled = 0;
 825        /* MSI enabled, remember for cleanup */
 826        } else {
 827                printk(KERN_DEBUG "Enabled MSI interrupting.\n");
 828                ape->msi_enabled = 1;
 829        }
 830
 831        pci_read_config_byte(dev, PCI_REVISION_ID, &ape->revision);
 832#if 0 /* example */
 833        /* (for example) this driver does not support revision 0x42 */
 834    if (ape->revision == 0x42) {
 835                printk(KERN_DEBUG "Revision 0x42 is not supported by this driver.\n");
 836                rc = -ENODEV;
 837                goto err_rev;
 838        }
 839#endif
 840        /** XXX check for native or legacy PCIe endpoint? */
 841
 842        rc = pci_request_regions(dev, DRV_NAME);
 843        /* could not request all regions? */
 844        if (rc) {
 845                /* assume device is in use (and do not disable it later!) */
 846                ape->in_use = 1;
 847                goto err_regions;
 848        }
 849        ape->got_regions = 1;
 850
 851#if 1   /* @todo For now, disable 64-bit, because I do not understand the implications (DAC!) */
 852        /* query for DMA transfer */
 853        /* @see Documentation/PCI/PCI-DMA-mapping.txt */
 854        if (!pci_set_dma_mask(dev, DMA_BIT_MASK(64))) {
 855                pci_set_consistent_dma_mask(dev, DMA_BIT_MASK(64));
 856                /* use 64-bit DMA */
 857                printk(KERN_DEBUG "Using a 64-bit DMA mask.\n");
 858        } else
 859#endif
 860        if (!pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
 861                printk(KERN_DEBUG "Could not set 64-bit DMA mask.\n");
 862                pci_set_consistent_dma_mask(dev, DMA_BIT_MASK(32));
 863                /* use 32-bit DMA */
 864                printk(KERN_DEBUG "Using a 32-bit DMA mask.\n");
 865        } else {
 866                printk(KERN_DEBUG "No suitable DMA possible.\n");
 867                /** @todo Choose proper error return code */
 868                rc = -1;
 869                goto err_mask;
 870        }
 871
 872        rc = pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq_pin);
 873        /* could not read? */
 874        if (rc)
 875                goto err_irq;
 876        printk(KERN_DEBUG "IRQ pin #%d (0=none, 1=INTA#...4=INTD#).\n", irq_pin);
 877
 878        /* @see LDD3, page 318 */
 879        rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq_line);
 880        /* could not read? */
 881        if (rc) {
 882                printk(KERN_DEBUG "Could not query PCI_INTERRUPT_LINE, error %d\n", rc);
 883                goto err_irq;
 884        }
 885        printk(KERN_DEBUG "IRQ line #%d.\n", irq_line);
 886#if 1
 887        irq_line = dev->irq;
 888        /* @see LDD3, page 259 */
 889        rc = request_irq(irq_line, altpciechdma_isr, IRQF_SHARED, DRV_NAME, (void *)ape);
 890        if (rc) {
 891                printk(KERN_DEBUG "Could not request IRQ #%d, error %d\n", irq_line, rc);
 892                ape->irq_line = -1;
 893                goto err_irq;
 894        }
 895        /* remember which irq we allocated */
 896        ape->irq_line = (int)irq_line;
 897        printk(KERN_DEBUG "Succesfully requested IRQ #%d with dev_id 0x%p\n", irq_line, ape);
 898#endif
 899        /* show BARs */
 900        scan_bars(ape, dev);
 901        /* map BARs */
 902        rc = map_bars(ape, dev);
 903        if (rc)
 904                goto err_map;
 905#if ALTPCIECHDMA_CDEV
 906        /* initialize character device */
 907        rc = sg_init(ape);
 908        if (rc)
 909                goto err_cdev;
 910#endif
 911        /* perform DMA engines loop back test */
 912        rc = dma_test(ape, dev);
 913        (void)rc;
 914        /* succesfully took the device */
 915        rc = 0;
 916        printk(KERN_DEBUG "probe() successful.\n");
 917        goto end;
 918#if ALTPCIECHDMA_CDEV
 919err_cdev:
 920        /* unmap the BARs */
 921        unmap_bars(ape, dev);
 922#endif
 923err_map:
 924        /* free allocated irq */
 925        if (ape->irq_line >= 0)
 926                free_irq(ape->irq_line, (void *)ape);
 927err_irq:
 928        if (ape->msi_enabled)
 929                pci_disable_msi(dev);
 930        /* disable the device iff it is not in use */
 931        if (!ape->in_use)
 932                pci_disable_device(dev);
 933        if (ape->got_regions)
 934                pci_release_regions(dev);
 935err_mask:
 936err_regions:
 937/*err_rev:*/
 938/* clean up everything before device enable() */
 939err_enable:
 940        if (ape->table_virt)
 941                pci_free_consistent(dev, APE_CHDMA_TABLE_SIZE, ape->table_virt, ape->table_bus);
 942/* clean up everything before allocating descriptor table */
 943err_table:
 944        if (ape)
 945                kfree(ape);
 946err_ape:
 947end:
 948        return rc;
 949}
 950
 951static void __devexit remove(struct pci_dev *dev)
 952{
 953        struct ape_dev *ape = dev_get_drvdata(&dev->dev);
 954
 955        printk(KERN_DEBUG "remove(0x%p)\n", dev);
 956        printk(KERN_DEBUG "remove(dev = 0x%p) where ape = 0x%p\n", dev, ape);
 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 */
 994static 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 */
1010static 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
1020static 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 */
1040static 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                /* limit DMA transfer size */
1052                transfer_len = (remaining < APE_CHDMA_MAX_TRANSFER_LEN) ? remaining :
1053                        APE_CHDMA_MAX_TRANSFER_LEN;
1054                /* get all user space buffer pages and create a scattergather list */
1055                sgm_map_user_pages(ape->sgm, transfer_addr, transfer_len, 0/*read from userspace*/);
1056                printk(KERN_DEBUG DRV_NAME "mapped_pages=%d\n", ape->sgm->mapped_pages);
1057                /* map all entries in the scattergather list */
1058                hwnents = pci_map_sg(ape->pci_dev, ape->sgm->sgl, ape->sgm->mapped_pages, DMA_TO_DEVICE);
1059                printk(KERN_DEBUG DRV_NAME "hwnents=%d\n", hwnents);
1060                /* build device descriptor tables and submit them to the DMA engine */
1061                tents = ape_sg_to_chdma_table(ape->sgm->sgl, hwnents, 0, &ape->table_virt->desc[0], 4096);
1062                printk(KERN_DEBUG DRV_NAME "tents=%d\n", hwnents);
1063#if 0
1064                while (tables) {
1065                        /* TODO build table */
1066                        /* TODO submit table to the device */
1067                        /* if engine stopped and unfinished work then start engine */
1068                }
1069                put ourselves on wait queue
1070#endif
1071
1072                dma_unmap_sg(NULL, ape->sgm->sgl, ape->sgm->mapped_pages, DMA_TO_DEVICE);
1073                /* dirty and free the pages */
1074                sgm_unmap_user_pages(ape->sgm, 1/*dirtied*/);
1075                /* book keeping */
1076                transfer_addr += transfer_len;
1077                remaining -= transfer_len;
1078                done += transfer_len;
1079        }
1080        return done;
1081}
1082
1083/*
1084 * character device file operations
1085 */
1086static const struct file_operations sg_fops = {
1087        .owner = THIS_MODULE,
1088        .open = sg_open,
1089        .release = sg_close,
1090        .read = sg_read,
1091        .write = sg_write,
1092};
1093
1094/* sg_init() - Initialize character device
1095 *
1096 * XXX Should ideally be tied to the device, on device probe, not module init.
1097 */
1098static int sg_init(struct ape_dev *ape)
1099{
1100        int rc;
1101        printk(KERN_DEBUG DRV_NAME " sg_init()\n");
1102        /* allocate a dynamically allocated character device node */
1103        rc = alloc_chrdev_region(&ape->cdevno, 0/*requested minor*/, 1/*count*/, DRV_NAME);
1104        /* allocation failed? */
1105        if (rc < 0) {
1106                printk("alloc_chrdev_region() = %d\n", rc);
1107                goto fail_alloc;
1108        }
1109        /* couple the device file operations to the character device */
1110        cdev_init(&ape->cdev, &sg_fops);
1111        ape->cdev.owner = THIS_MODULE;
1112        /* bring character device live */
1113        rc = cdev_add(&ape->cdev, ape->cdevno, 1/*count*/);
1114        if (rc < 0) {
1115                printk("cdev_add() = %d\n", rc);
1116                goto fail_add;
1117        }
1118        printk(KERN_DEBUG "altpciechdma = %d:%d\n", MAJOR(ape->cdevno), MINOR(ape->cdevno));
1119        return 0;
1120fail_add:
1121        /* free the dynamically allocated character device node */
1122    unregister_chrdev_region(ape->cdevno, 1/*count*/);
1123fail_alloc:
1124        return -1;
1125}
1126
1127/* sg_exit() - Cleanup character device
1128 *
1129 * XXX Should ideally be tied to the device, on device remove, not module exit.
1130 */
1131
1132static void sg_exit(struct ape_dev *ape)
1133{
1134        printk(KERN_DEBUG DRV_NAME " sg_exit()\n");
1135        /* remove the character device */
1136        cdev_del(&ape->cdev);
1137        /* free the dynamically allocated character device node */
1138        unregister_chrdev_region(ape->cdevno, 1/*count*/);
1139}
1140
1141#endif /* ALTPCIECHDMA_CDEV */
1142
1143/* used to register the driver with the PCI kernel sub system
1144 * @see LDD3 page 311
1145 */
1146static struct pci_driver pci_driver = {
1147        .name = DRV_NAME,
1148        .id_table = ids,
1149        .probe = probe,
1150        .remove = __devexit_p(remove),
1151        /* resume, suspend are optional */
1152};
1153
1154/**
1155 * alterapciechdma_init() - Module initialization, registers devices.
1156 */
1157static int __init alterapciechdma_init(void)
1158{
1159        int rc = 0;
1160        printk(KERN_DEBUG DRV_NAME " init(), built at " __DATE__ " " __TIME__ "\n");
1161        /* register this driver with the PCI bus driver */
1162        rc = pci_register_driver(&pci_driver);
1163        if (rc < 0)
1164                return rc;
1165        return 0;
1166}
1167
1168/**
1169 * alterapciechdma_init() - Module cleanup, unregisters devices.
1170 */
1171static void __exit alterapciechdma_exit(void)
1172{
1173        printk(KERN_DEBUG DRV_NAME " exit(), built at " __DATE__ " " __TIME__ "\n");
1174        /* unregister this driver from the PCI bus driver */
1175        pci_unregister_driver(&pci_driver);
1176}
1177
1178MODULE_LICENSE("GPL");
1179
1180module_init(alterapciechdma_init);
1181module_exit(alterapciechdma_exit);
1182
1183