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