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