linux/drivers/misc/hpilo.c
<<
>>
Prefs
   1/*
   2 * Driver for the HP iLO management processor.
   3 *
   4 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
   5 *      David Altobelli <david.altobelli@hpe.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 version 2 as
   9 * published by the Free Software Foundation.
  10 */
  11#include <linux/kernel.h>
  12#include <linux/types.h>
  13#include <linux/module.h>
  14#include <linux/fs.h>
  15#include <linux/pci.h>
  16#include <linux/interrupt.h>
  17#include <linux/ioport.h>
  18#include <linux/device.h>
  19#include <linux/file.h>
  20#include <linux/cdev.h>
  21#include <linux/sched.h>
  22#include <linux/spinlock.h>
  23#include <linux/delay.h>
  24#include <linux/uaccess.h>
  25#include <linux/io.h>
  26#include <linux/wait.h>
  27#include <linux/poll.h>
  28#include <linux/slab.h>
  29#include "hpilo.h"
  30
  31static struct class *ilo_class;
  32static unsigned int ilo_major;
  33static unsigned int max_ccb = 16;
  34static char ilo_hwdev[MAX_ILO_DEV];
  35static const struct pci_device_id ilo_blacklist[] = {
  36        /* auxiliary iLO */
  37        {PCI_DEVICE_SUB(PCI_VENDOR_ID_HP, 0x3307, PCI_VENDOR_ID_HP, 0x1979)},
  38        /* CL */
  39        {PCI_DEVICE_SUB(PCI_VENDOR_ID_HP, 0x3307, PCI_VENDOR_ID_HP_3PAR, 0x0289)},
  40        {}
  41};
  42
  43static inline int get_entry_id(int entry)
  44{
  45        return (entry & ENTRY_MASK_DESCRIPTOR) >> ENTRY_BITPOS_DESCRIPTOR;
  46}
  47
  48static inline int get_entry_len(int entry)
  49{
  50        return ((entry & ENTRY_MASK_QWORDS) >> ENTRY_BITPOS_QWORDS) << 3;
  51}
  52
  53static inline int mk_entry(int id, int len)
  54{
  55        int qlen = len & 7 ? (len >> 3) + 1 : len >> 3;
  56        return id << ENTRY_BITPOS_DESCRIPTOR | qlen << ENTRY_BITPOS_QWORDS;
  57}
  58
  59static inline int desc_mem_sz(int nr_entry)
  60{
  61        return nr_entry << L2_QENTRY_SZ;
  62}
  63
  64/*
  65 * FIFO queues, shared with hardware.
  66 *
  67 * If a queue has empty slots, an entry is added to the queue tail,
  68 * and that entry is marked as occupied.
  69 * Entries can be dequeued from the head of the list, when the device
  70 * has marked the entry as consumed.
  71 *
  72 * Returns true on successful queue/dequeue, false on failure.
  73 */
  74static int fifo_enqueue(struct ilo_hwinfo *hw, char *fifobar, int entry)
  75{
  76        struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
  77        unsigned long flags;
  78        int ret = 0;
  79
  80        spin_lock_irqsave(&hw->fifo_lock, flags);
  81        if (!(fifo_q->fifobar[(fifo_q->tail + 1) & fifo_q->imask]
  82              & ENTRY_MASK_O)) {
  83                fifo_q->fifobar[fifo_q->tail & fifo_q->imask] |=
  84                                (entry & ENTRY_MASK_NOSTATE) | fifo_q->merge;
  85                fifo_q->tail += 1;
  86                ret = 1;
  87        }
  88        spin_unlock_irqrestore(&hw->fifo_lock, flags);
  89
  90        return ret;
  91}
  92
  93static int fifo_dequeue(struct ilo_hwinfo *hw, char *fifobar, int *entry)
  94{
  95        struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
  96        unsigned long flags;
  97        int ret = 0;
  98        u64 c;
  99
 100        spin_lock_irqsave(&hw->fifo_lock, flags);
 101        c = fifo_q->fifobar[fifo_q->head & fifo_q->imask];
 102        if (c & ENTRY_MASK_C) {
 103                if (entry)
 104                        *entry = c & ENTRY_MASK_NOSTATE;
 105
 106                fifo_q->fifobar[fifo_q->head & fifo_q->imask] =
 107                                                        (c | ENTRY_MASK) + 1;
 108                fifo_q->head += 1;
 109                ret = 1;
 110        }
 111        spin_unlock_irqrestore(&hw->fifo_lock, flags);
 112
 113        return ret;
 114}
 115
 116static int fifo_check_recv(struct ilo_hwinfo *hw, char *fifobar)
 117{
 118        struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
 119        unsigned long flags;
 120        int ret = 0;
 121        u64 c;
 122
 123        spin_lock_irqsave(&hw->fifo_lock, flags);
 124        c = fifo_q->fifobar[fifo_q->head & fifo_q->imask];
 125        if (c & ENTRY_MASK_C)
 126                ret = 1;
 127        spin_unlock_irqrestore(&hw->fifo_lock, flags);
 128
 129        return ret;
 130}
 131
 132static int ilo_pkt_enqueue(struct ilo_hwinfo *hw, struct ccb *ccb,
 133                           int dir, int id, int len)
 134{
 135        char *fifobar;
 136        int entry;
 137
 138        if (dir == SENDQ)
 139                fifobar = ccb->ccb_u1.send_fifobar;
 140        else
 141                fifobar = ccb->ccb_u3.recv_fifobar;
 142
 143        entry = mk_entry(id, len);
 144        return fifo_enqueue(hw, fifobar, entry);
 145}
 146
 147static int ilo_pkt_dequeue(struct ilo_hwinfo *hw, struct ccb *ccb,
 148                           int dir, int *id, int *len, void **pkt)
 149{
 150        char *fifobar, *desc;
 151        int entry = 0, pkt_id = 0;
 152        int ret;
 153
 154        if (dir == SENDQ) {
 155                fifobar = ccb->ccb_u1.send_fifobar;
 156                desc = ccb->ccb_u2.send_desc;
 157        } else {
 158                fifobar = ccb->ccb_u3.recv_fifobar;
 159                desc = ccb->ccb_u4.recv_desc;
 160        }
 161
 162        ret = fifo_dequeue(hw, fifobar, &entry);
 163        if (ret) {
 164                pkt_id = get_entry_id(entry);
 165                if (id)
 166                        *id = pkt_id;
 167                if (len)
 168                        *len = get_entry_len(entry);
 169                if (pkt)
 170                        *pkt = (void *)(desc + desc_mem_sz(pkt_id));
 171        }
 172
 173        return ret;
 174}
 175
 176static int ilo_pkt_recv(struct ilo_hwinfo *hw, struct ccb *ccb)
 177{
 178        char *fifobar = ccb->ccb_u3.recv_fifobar;
 179
 180        return fifo_check_recv(hw, fifobar);
 181}
 182
 183static inline void doorbell_set(struct ccb *ccb)
 184{
 185        iowrite8(1, ccb->ccb_u5.db_base);
 186}
 187
 188static inline void doorbell_clr(struct ccb *ccb)
 189{
 190        iowrite8(2, ccb->ccb_u5.db_base);
 191}
 192
 193static inline int ctrl_set(int l2sz, int idxmask, int desclim)
 194{
 195        int active = 0, go = 1;
 196        return l2sz << CTRL_BITPOS_L2SZ |
 197               idxmask << CTRL_BITPOS_FIFOINDEXMASK |
 198               desclim << CTRL_BITPOS_DESCLIMIT |
 199               active << CTRL_BITPOS_A |
 200               go << CTRL_BITPOS_G;
 201}
 202
 203static void ctrl_setup(struct ccb *ccb, int nr_desc, int l2desc_sz)
 204{
 205        /* for simplicity, use the same parameters for send and recv ctrls */
 206        ccb->send_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);
 207        ccb->recv_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);
 208}
 209
 210static inline int fifo_sz(int nr_entry)
 211{
 212        /* size of a fifo is determined by the number of entries it contains */
 213        return (nr_entry * sizeof(u64)) + FIFOHANDLESIZE;
 214}
 215
 216static void fifo_setup(void *base_addr, int nr_entry)
 217{
 218        struct fifo *fifo_q = base_addr;
 219        int i;
 220
 221        /* set up an empty fifo */
 222        fifo_q->head = 0;
 223        fifo_q->tail = 0;
 224        fifo_q->reset = 0;
 225        fifo_q->nrents = nr_entry;
 226        fifo_q->imask = nr_entry - 1;
 227        fifo_q->merge = ENTRY_MASK_O;
 228
 229        for (i = 0; i < nr_entry; i++)
 230                fifo_q->fifobar[i] = 0;
 231}
 232
 233static void ilo_ccb_close(struct pci_dev *pdev, struct ccb_data *data)
 234{
 235        struct ccb *driver_ccb = &data->driver_ccb;
 236        struct ccb __iomem *device_ccb = data->mapped_ccb;
 237        int retries;
 238
 239        /* complicated dance to tell the hw we are stopping */
 240        doorbell_clr(driver_ccb);
 241        iowrite32(ioread32(&device_ccb->send_ctrl) & ~(1 << CTRL_BITPOS_G),
 242                  &device_ccb->send_ctrl);
 243        iowrite32(ioread32(&device_ccb->recv_ctrl) & ~(1 << CTRL_BITPOS_G),
 244                  &device_ccb->recv_ctrl);
 245
 246        /* give iLO some time to process stop request */
 247        for (retries = MAX_WAIT; retries > 0; retries--) {
 248                doorbell_set(driver_ccb);
 249                udelay(WAIT_TIME);
 250                if (!(ioread32(&device_ccb->send_ctrl) & (1 << CTRL_BITPOS_A))
 251                    &&
 252                    !(ioread32(&device_ccb->recv_ctrl) & (1 << CTRL_BITPOS_A)))
 253                        break;
 254        }
 255        if (retries == 0)
 256                dev_err(&pdev->dev, "Closing, but controller still active\n");
 257
 258        /* clear the hw ccb */
 259        memset_io(device_ccb, 0, sizeof(struct ccb));
 260
 261        /* free resources used to back send/recv queues */
 262        pci_free_consistent(pdev, data->dma_size, data->dma_va, data->dma_pa);
 263}
 264
 265static int ilo_ccb_setup(struct ilo_hwinfo *hw, struct ccb_data *data, int slot)
 266{
 267        char *dma_va;
 268        dma_addr_t dma_pa;
 269        struct ccb *driver_ccb, *ilo_ccb;
 270
 271        driver_ccb = &data->driver_ccb;
 272        ilo_ccb = &data->ilo_ccb;
 273
 274        data->dma_size = 2 * fifo_sz(NR_QENTRY) +
 275                         2 * desc_mem_sz(NR_QENTRY) +
 276                         ILO_START_ALIGN + ILO_CACHE_SZ;
 277
 278        data->dma_va = pci_alloc_consistent(hw->ilo_dev, data->dma_size,
 279                                            &data->dma_pa);
 280        if (!data->dma_va)
 281                return -ENOMEM;
 282
 283        dma_va = (char *)data->dma_va;
 284        dma_pa = data->dma_pa;
 285
 286        memset(dma_va, 0, data->dma_size);
 287
 288        dma_va = (char *)roundup((unsigned long)dma_va, ILO_START_ALIGN);
 289        dma_pa = roundup(dma_pa, ILO_START_ALIGN);
 290
 291        /*
 292         * Create two ccb's, one with virt addrs, one with phys addrs.
 293         * Copy the phys addr ccb to device shared mem.
 294         */
 295        ctrl_setup(driver_ccb, NR_QENTRY, L2_QENTRY_SZ);
 296        ctrl_setup(ilo_ccb, NR_QENTRY, L2_QENTRY_SZ);
 297
 298        fifo_setup(dma_va, NR_QENTRY);
 299        driver_ccb->ccb_u1.send_fifobar = dma_va + FIFOHANDLESIZE;
 300        ilo_ccb->ccb_u1.send_fifobar_pa = dma_pa + FIFOHANDLESIZE;
 301        dma_va += fifo_sz(NR_QENTRY);
 302        dma_pa += fifo_sz(NR_QENTRY);
 303
 304        dma_va = (char *)roundup((unsigned long)dma_va, ILO_CACHE_SZ);
 305        dma_pa = roundup(dma_pa, ILO_CACHE_SZ);
 306
 307        fifo_setup(dma_va, NR_QENTRY);
 308        driver_ccb->ccb_u3.recv_fifobar = dma_va + FIFOHANDLESIZE;
 309        ilo_ccb->ccb_u3.recv_fifobar_pa = dma_pa + FIFOHANDLESIZE;
 310        dma_va += fifo_sz(NR_QENTRY);
 311        dma_pa += fifo_sz(NR_QENTRY);
 312
 313        driver_ccb->ccb_u2.send_desc = dma_va;
 314        ilo_ccb->ccb_u2.send_desc_pa = dma_pa;
 315        dma_pa += desc_mem_sz(NR_QENTRY);
 316        dma_va += desc_mem_sz(NR_QENTRY);
 317
 318        driver_ccb->ccb_u4.recv_desc = dma_va;
 319        ilo_ccb->ccb_u4.recv_desc_pa = dma_pa;
 320
 321        driver_ccb->channel = slot;
 322        ilo_ccb->channel = slot;
 323
 324        driver_ccb->ccb_u5.db_base = hw->db_vaddr + (slot << L2_DB_SIZE);
 325        ilo_ccb->ccb_u5.db_base = NULL; /* hw ccb's doorbell is not used */
 326
 327        return 0;
 328}
 329
 330static void ilo_ccb_open(struct ilo_hwinfo *hw, struct ccb_data *data, int slot)
 331{
 332        int pkt_id, pkt_sz;
 333        struct ccb *driver_ccb = &data->driver_ccb;
 334
 335        /* copy the ccb with physical addrs to device memory */
 336        data->mapped_ccb = (struct ccb __iomem *)
 337                                (hw->ram_vaddr + (slot * ILOHW_CCB_SZ));
 338        memcpy_toio(data->mapped_ccb, &data->ilo_ccb, sizeof(struct ccb));
 339
 340        /* put packets on the send and receive queues */
 341        pkt_sz = 0;
 342        for (pkt_id = 0; pkt_id < NR_QENTRY; pkt_id++) {
 343                ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, pkt_sz);
 344                doorbell_set(driver_ccb);
 345        }
 346
 347        pkt_sz = desc_mem_sz(1);
 348        for (pkt_id = 0; pkt_id < NR_QENTRY; pkt_id++)
 349                ilo_pkt_enqueue(hw, driver_ccb, RECVQ, pkt_id, pkt_sz);
 350
 351        /* the ccb is ready to use */
 352        doorbell_clr(driver_ccb);
 353}
 354
 355static int ilo_ccb_verify(struct ilo_hwinfo *hw, struct ccb_data *data)
 356{
 357        int pkt_id, i;
 358        struct ccb *driver_ccb = &data->driver_ccb;
 359
 360        /* make sure iLO is really handling requests */
 361        for (i = MAX_WAIT; i > 0; i--) {
 362                if (ilo_pkt_dequeue(hw, driver_ccb, SENDQ, &pkt_id, NULL, NULL))
 363                        break;
 364                udelay(WAIT_TIME);
 365        }
 366
 367        if (i == 0) {
 368                dev_err(&hw->ilo_dev->dev, "Open could not dequeue a packet\n");
 369                return -EBUSY;
 370        }
 371
 372        ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, 0);
 373        doorbell_set(driver_ccb);
 374        return 0;
 375}
 376
 377static inline int is_channel_reset(struct ccb *ccb)
 378{
 379        /* check for this particular channel needing a reset */
 380        return FIFOBARTOHANDLE(ccb->ccb_u1.send_fifobar)->reset;
 381}
 382
 383static inline void set_channel_reset(struct ccb *ccb)
 384{
 385        /* set a flag indicating this channel needs a reset */
 386        FIFOBARTOHANDLE(ccb->ccb_u1.send_fifobar)->reset = 1;
 387}
 388
 389static inline int get_device_outbound(struct ilo_hwinfo *hw)
 390{
 391        return ioread32(&hw->mmio_vaddr[DB_OUT]);
 392}
 393
 394static inline int is_db_reset(int db_out)
 395{
 396        return db_out & (1 << DB_RESET);
 397}
 398
 399static inline int is_device_reset(struct ilo_hwinfo *hw)
 400{
 401        /* check for global reset condition */
 402        return is_db_reset(get_device_outbound(hw));
 403}
 404
 405static inline void clear_pending_db(struct ilo_hwinfo *hw, int clr)
 406{
 407        iowrite32(clr, &hw->mmio_vaddr[DB_OUT]);
 408}
 409
 410static inline void clear_device(struct ilo_hwinfo *hw)
 411{
 412        /* clear the device (reset bits, pending channel entries) */
 413        clear_pending_db(hw, -1);
 414}
 415
 416static inline void ilo_enable_interrupts(struct ilo_hwinfo *hw)
 417{
 418        iowrite8(ioread8(&hw->mmio_vaddr[DB_IRQ]) | 1, &hw->mmio_vaddr[DB_IRQ]);
 419}
 420
 421static inline void ilo_disable_interrupts(struct ilo_hwinfo *hw)
 422{
 423        iowrite8(ioread8(&hw->mmio_vaddr[DB_IRQ]) & ~1,
 424                 &hw->mmio_vaddr[DB_IRQ]);
 425}
 426
 427static void ilo_set_reset(struct ilo_hwinfo *hw)
 428{
 429        int slot;
 430
 431        /*
 432         * Mapped memory is zeroed on ilo reset, so set a per ccb flag
 433         * to indicate that this ccb needs to be closed and reopened.
 434         */
 435        for (slot = 0; slot < max_ccb; slot++) {
 436                if (!hw->ccb_alloc[slot])
 437                        continue;
 438                set_channel_reset(&hw->ccb_alloc[slot]->driver_ccb);
 439        }
 440}
 441
 442static ssize_t ilo_read(struct file *fp, char __user *buf,
 443                        size_t len, loff_t *off)
 444{
 445        int err, found, cnt, pkt_id, pkt_len;
 446        struct ccb_data *data = fp->private_data;
 447        struct ccb *driver_ccb = &data->driver_ccb;
 448        struct ilo_hwinfo *hw = data->ilo_hw;
 449        void *pkt;
 450
 451        if (is_channel_reset(driver_ccb)) {
 452                /*
 453                 * If the device has been reset, applications
 454                 * need to close and reopen all ccbs.
 455                 */
 456                return -ENODEV;
 457        }
 458
 459        /*
 460         * This function is to be called when data is expected
 461         * in the channel, and will return an error if no packet is found
 462         * during the loop below.  The sleep/retry logic is to allow
 463         * applications to call read() immediately post write(),
 464         * and give iLO some time to process the sent packet.
 465         */
 466        cnt = 20;
 467        do {
 468                /* look for a received packet */
 469                found = ilo_pkt_dequeue(hw, driver_ccb, RECVQ, &pkt_id,
 470                                        &pkt_len, &pkt);
 471                if (found)
 472                        break;
 473                cnt--;
 474                msleep(100);
 475        } while (!found && cnt);
 476
 477        if (!found)
 478                return -EAGAIN;
 479
 480        /* only copy the length of the received packet */
 481        if (pkt_len < len)
 482                len = pkt_len;
 483
 484        err = copy_to_user(buf, pkt, len);
 485
 486        /* return the received packet to the queue */
 487        ilo_pkt_enqueue(hw, driver_ccb, RECVQ, pkt_id, desc_mem_sz(1));
 488
 489        return err ? -EFAULT : len;
 490}
 491
 492static ssize_t ilo_write(struct file *fp, const char __user *buf,
 493                         size_t len, loff_t *off)
 494{
 495        int err, pkt_id, pkt_len;
 496        struct ccb_data *data = fp->private_data;
 497        struct ccb *driver_ccb = &data->driver_ccb;
 498        struct ilo_hwinfo *hw = data->ilo_hw;
 499        void *pkt;
 500
 501        if (is_channel_reset(driver_ccb))
 502                return -ENODEV;
 503
 504        /* get a packet to send the user command */
 505        if (!ilo_pkt_dequeue(hw, driver_ccb, SENDQ, &pkt_id, &pkt_len, &pkt))
 506                return -EBUSY;
 507
 508        /* limit the length to the length of the packet */
 509        if (pkt_len < len)
 510                len = pkt_len;
 511
 512        /* on failure, set the len to 0 to return empty packet to the device */
 513        err = copy_from_user(pkt, buf, len);
 514        if (err)
 515                len = 0;
 516
 517        /* send the packet */
 518        ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, len);
 519        doorbell_set(driver_ccb);
 520
 521        return err ? -EFAULT : len;
 522}
 523
 524static unsigned int ilo_poll(struct file *fp, poll_table *wait)
 525{
 526        struct ccb_data *data = fp->private_data;
 527        struct ccb *driver_ccb = &data->driver_ccb;
 528
 529        poll_wait(fp, &data->ccb_waitq, wait);
 530
 531        if (is_channel_reset(driver_ccb))
 532                return POLLERR;
 533        else if (ilo_pkt_recv(data->ilo_hw, driver_ccb))
 534                return POLLIN | POLLRDNORM;
 535
 536        return 0;
 537}
 538
 539static int ilo_close(struct inode *ip, struct file *fp)
 540{
 541        int slot;
 542        struct ccb_data *data;
 543        struct ilo_hwinfo *hw;
 544        unsigned long flags;
 545
 546        slot = iminor(ip) % max_ccb;
 547        hw = container_of(ip->i_cdev, struct ilo_hwinfo, cdev);
 548
 549        spin_lock(&hw->open_lock);
 550
 551        if (hw->ccb_alloc[slot]->ccb_cnt == 1) {
 552
 553                data = fp->private_data;
 554
 555                spin_lock_irqsave(&hw->alloc_lock, flags);
 556                hw->ccb_alloc[slot] = NULL;
 557                spin_unlock_irqrestore(&hw->alloc_lock, flags);
 558
 559                ilo_ccb_close(hw->ilo_dev, data);
 560
 561                kfree(data);
 562        } else
 563                hw->ccb_alloc[slot]->ccb_cnt--;
 564
 565        spin_unlock(&hw->open_lock);
 566
 567        return 0;
 568}
 569
 570static int ilo_open(struct inode *ip, struct file *fp)
 571{
 572        int slot, error;
 573        struct ccb_data *data;
 574        struct ilo_hwinfo *hw;
 575        unsigned long flags;
 576
 577        slot = iminor(ip) % max_ccb;
 578        hw = container_of(ip->i_cdev, struct ilo_hwinfo, cdev);
 579
 580        /* new ccb allocation */
 581        data = kzalloc(sizeof(*data), GFP_KERNEL);
 582        if (!data)
 583                return -ENOMEM;
 584
 585        spin_lock(&hw->open_lock);
 586
 587        /* each fd private_data holds sw/hw view of ccb */
 588        if (hw->ccb_alloc[slot] == NULL) {
 589                /* create a channel control block for this minor */
 590                error = ilo_ccb_setup(hw, data, slot);
 591                if (error) {
 592                        kfree(data);
 593                        goto out;
 594                }
 595
 596                data->ccb_cnt = 1;
 597                data->ccb_excl = fp->f_flags & O_EXCL;
 598                data->ilo_hw = hw;
 599                init_waitqueue_head(&data->ccb_waitq);
 600
 601                /* write the ccb to hw */
 602                spin_lock_irqsave(&hw->alloc_lock, flags);
 603                ilo_ccb_open(hw, data, slot);
 604                hw->ccb_alloc[slot] = data;
 605                spin_unlock_irqrestore(&hw->alloc_lock, flags);
 606
 607                /* make sure the channel is functional */
 608                error = ilo_ccb_verify(hw, data);
 609                if (error) {
 610
 611                        spin_lock_irqsave(&hw->alloc_lock, flags);
 612                        hw->ccb_alloc[slot] = NULL;
 613                        spin_unlock_irqrestore(&hw->alloc_lock, flags);
 614
 615                        ilo_ccb_close(hw->ilo_dev, data);
 616
 617                        kfree(data);
 618                        goto out;
 619                }
 620
 621        } else {
 622                kfree(data);
 623                if (fp->f_flags & O_EXCL || hw->ccb_alloc[slot]->ccb_excl) {
 624                        /*
 625                         * The channel exists, and either this open
 626                         * or a previous open of this channel wants
 627                         * exclusive access.
 628                         */
 629                        error = -EBUSY;
 630                } else {
 631                        hw->ccb_alloc[slot]->ccb_cnt++;
 632                        error = 0;
 633                }
 634        }
 635out:
 636        spin_unlock(&hw->open_lock);
 637
 638        if (!error)
 639                fp->private_data = hw->ccb_alloc[slot];
 640
 641        return error;
 642}
 643
 644static const struct file_operations ilo_fops = {
 645        .owner          = THIS_MODULE,
 646        .read           = ilo_read,
 647        .write          = ilo_write,
 648        .poll           = ilo_poll,
 649        .open           = ilo_open,
 650        .release        = ilo_close,
 651        .llseek         = noop_llseek,
 652};
 653
 654static irqreturn_t ilo_isr(int irq, void *data)
 655{
 656        struct ilo_hwinfo *hw = data;
 657        int pending, i;
 658
 659        spin_lock(&hw->alloc_lock);
 660
 661        /* check for ccbs which have data */
 662        pending = get_device_outbound(hw);
 663        if (!pending) {
 664                spin_unlock(&hw->alloc_lock);
 665                return IRQ_NONE;
 666        }
 667
 668        if (is_db_reset(pending)) {
 669                /* wake up all ccbs if the device was reset */
 670                pending = -1;
 671                ilo_set_reset(hw);
 672        }
 673
 674        for (i = 0; i < max_ccb; i++) {
 675                if (!hw->ccb_alloc[i])
 676                        continue;
 677                if (pending & (1 << i))
 678                        wake_up_interruptible(&hw->ccb_alloc[i]->ccb_waitq);
 679        }
 680
 681        /* clear the device of the channels that have been handled */
 682        clear_pending_db(hw, pending);
 683
 684        spin_unlock(&hw->alloc_lock);
 685
 686        return IRQ_HANDLED;
 687}
 688
 689static void ilo_unmap_device(struct pci_dev *pdev, struct ilo_hwinfo *hw)
 690{
 691        pci_iounmap(pdev, hw->db_vaddr);
 692        pci_iounmap(pdev, hw->ram_vaddr);
 693        pci_iounmap(pdev, hw->mmio_vaddr);
 694}
 695
 696static int ilo_map_device(struct pci_dev *pdev, struct ilo_hwinfo *hw)
 697{
 698        int bar;
 699        unsigned long off;
 700
 701        /* map the memory mapped i/o registers */
 702        hw->mmio_vaddr = pci_iomap(pdev, 1, 0);
 703        if (hw->mmio_vaddr == NULL) {
 704                dev_err(&pdev->dev, "Error mapping mmio\n");
 705                goto out;
 706        }
 707
 708        /* map the adapter shared memory region */
 709        if (pdev->subsystem_device == 0x00E4) {
 710                bar = 5;
 711                /* Last 8k is reserved for CCBs */
 712                off = pci_resource_len(pdev, bar) - 0x2000;
 713        } else {
 714                bar = 2;
 715                off = 0;
 716        }
 717        hw->ram_vaddr = pci_iomap_range(pdev, bar, off, max_ccb * ILOHW_CCB_SZ);
 718        if (hw->ram_vaddr == NULL) {
 719                dev_err(&pdev->dev, "Error mapping shared mem\n");
 720                goto mmio_free;
 721        }
 722
 723        /* map the doorbell aperture */
 724        hw->db_vaddr = pci_iomap(pdev, 3, max_ccb * ONE_DB_SIZE);
 725        if (hw->db_vaddr == NULL) {
 726                dev_err(&pdev->dev, "Error mapping doorbell\n");
 727                goto ram_free;
 728        }
 729
 730        return 0;
 731ram_free:
 732        pci_iounmap(pdev, hw->ram_vaddr);
 733mmio_free:
 734        pci_iounmap(pdev, hw->mmio_vaddr);
 735out:
 736        return -ENOMEM;
 737}
 738
 739static void ilo_remove(struct pci_dev *pdev)
 740{
 741        int i, minor;
 742        struct ilo_hwinfo *ilo_hw = pci_get_drvdata(pdev);
 743
 744        if (!ilo_hw)
 745                return;
 746
 747        clear_device(ilo_hw);
 748
 749        minor = MINOR(ilo_hw->cdev.dev);
 750        for (i = minor; i < minor + max_ccb; i++)
 751                device_destroy(ilo_class, MKDEV(ilo_major, i));
 752
 753        cdev_del(&ilo_hw->cdev);
 754        ilo_disable_interrupts(ilo_hw);
 755        free_irq(pdev->irq, ilo_hw);
 756        ilo_unmap_device(pdev, ilo_hw);
 757        pci_release_regions(pdev);
 758        /*
 759         * pci_disable_device(pdev) used to be here. But this PCI device has
 760         * two functions with interrupt lines connected to a single pin. The
 761         * other one is a USB host controller. So when we disable the PIN here
 762         * e.g. by rmmod hpilo, the controller stops working. It is because
 763         * the interrupt link is disabled in ACPI since it is not refcounted
 764         * yet. See acpi_pci_link_free_irq called from acpi_pci_irq_disable.
 765         */
 766        kfree(ilo_hw);
 767        ilo_hwdev[(minor / max_ccb)] = 0;
 768}
 769
 770static int ilo_probe(struct pci_dev *pdev,
 771                               const struct pci_device_id *ent)
 772{
 773        int devnum, minor, start, error = 0;
 774        struct ilo_hwinfo *ilo_hw;
 775
 776        if (pci_match_id(ilo_blacklist, pdev)) {
 777                dev_dbg(&pdev->dev, "Not supported on this device\n");
 778                return -ENODEV;
 779        }
 780
 781        if (max_ccb > MAX_CCB)
 782                max_ccb = MAX_CCB;
 783        else if (max_ccb < MIN_CCB)
 784                max_ccb = MIN_CCB;
 785
 786        /* find a free range for device files */
 787        for (devnum = 0; devnum < MAX_ILO_DEV; devnum++) {
 788                if (ilo_hwdev[devnum] == 0) {
 789                        ilo_hwdev[devnum] = 1;
 790                        break;
 791                }
 792        }
 793
 794        if (devnum == MAX_ILO_DEV) {
 795                dev_err(&pdev->dev, "Error finding free device\n");
 796                return -ENODEV;
 797        }
 798
 799        /* track global allocations for this device */
 800        error = -ENOMEM;
 801        ilo_hw = kzalloc(sizeof(*ilo_hw), GFP_KERNEL);
 802        if (!ilo_hw)
 803                goto out;
 804
 805        ilo_hw->ilo_dev = pdev;
 806        spin_lock_init(&ilo_hw->alloc_lock);
 807        spin_lock_init(&ilo_hw->fifo_lock);
 808        spin_lock_init(&ilo_hw->open_lock);
 809
 810        error = pci_enable_device(pdev);
 811        if (error)
 812                goto free;
 813
 814        pci_set_master(pdev);
 815
 816        error = pci_request_regions(pdev, ILO_NAME);
 817        if (error)
 818                goto disable;
 819
 820        error = ilo_map_device(pdev, ilo_hw);
 821        if (error)
 822                goto free_regions;
 823
 824        pci_set_drvdata(pdev, ilo_hw);
 825        clear_device(ilo_hw);
 826
 827        error = request_irq(pdev->irq, ilo_isr, IRQF_SHARED, "hpilo", ilo_hw);
 828        if (error)
 829                goto unmap;
 830
 831        ilo_enable_interrupts(ilo_hw);
 832
 833        cdev_init(&ilo_hw->cdev, &ilo_fops);
 834        ilo_hw->cdev.owner = THIS_MODULE;
 835        start = devnum * max_ccb;
 836        error = cdev_add(&ilo_hw->cdev, MKDEV(ilo_major, start), max_ccb);
 837        if (error) {
 838                dev_err(&pdev->dev, "Could not add cdev\n");
 839                goto remove_isr;
 840        }
 841
 842        for (minor = 0 ; minor < max_ccb; minor++) {
 843                struct device *dev;
 844                dev = device_create(ilo_class, &pdev->dev,
 845                                    MKDEV(ilo_major, minor), NULL,
 846                                    "hpilo!d%dccb%d", devnum, minor);
 847                if (IS_ERR(dev))
 848                        dev_err(&pdev->dev, "Could not create files\n");
 849        }
 850
 851        return 0;
 852remove_isr:
 853        ilo_disable_interrupts(ilo_hw);
 854        free_irq(pdev->irq, ilo_hw);
 855unmap:
 856        ilo_unmap_device(pdev, ilo_hw);
 857free_regions:
 858        pci_release_regions(pdev);
 859disable:
 860/*      pci_disable_device(pdev);  see comment in ilo_remove */
 861free:
 862        kfree(ilo_hw);
 863out:
 864        ilo_hwdev[devnum] = 0;
 865        return error;
 866}
 867
 868static struct pci_device_id ilo_devices[] = {
 869        { PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB204) },
 870        { PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3307) },
 871        { }
 872};
 873MODULE_DEVICE_TABLE(pci, ilo_devices);
 874
 875static struct pci_driver ilo_driver = {
 876        .name     = ILO_NAME,
 877        .id_table = ilo_devices,
 878        .probe    = ilo_probe,
 879        .remove   = ilo_remove,
 880};
 881
 882static int __init ilo_init(void)
 883{
 884        int error;
 885        dev_t dev;
 886
 887        ilo_class = class_create(THIS_MODULE, "iLO");
 888        if (IS_ERR(ilo_class)) {
 889                error = PTR_ERR(ilo_class);
 890                goto out;
 891        }
 892
 893        error = alloc_chrdev_region(&dev, 0, MAX_OPEN, ILO_NAME);
 894        if (error)
 895                goto class_destroy;
 896
 897        ilo_major = MAJOR(dev);
 898
 899        error = pci_register_driver(&ilo_driver);
 900        if (error)
 901                goto chr_remove;
 902
 903        return 0;
 904chr_remove:
 905        unregister_chrdev_region(dev, MAX_OPEN);
 906class_destroy:
 907        class_destroy(ilo_class);
 908out:
 909        return error;
 910}
 911
 912static void __exit ilo_exit(void)
 913{
 914        pci_unregister_driver(&ilo_driver);
 915        unregister_chrdev_region(MKDEV(ilo_major, 0), MAX_OPEN);
 916        class_destroy(ilo_class);
 917}
 918
 919MODULE_VERSION("1.5.0");
 920MODULE_ALIAS(ILO_NAME);
 921MODULE_DESCRIPTION(ILO_NAME);
 922MODULE_AUTHOR("David Altobelli <david.altobelli@hpe.com>");
 923MODULE_LICENSE("GPL v2");
 924
 925module_param(max_ccb, uint, 0444);
 926MODULE_PARM_DESC(max_ccb, "Maximum number of HP iLO channels to attach (8-24)(default=16)");
 927
 928module_init(ilo_init);
 929module_exit(ilo_exit);
 930