2 * Driver for HP iLO/iLO2 management processor.
4 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
5 * David Altobelli <david.altobelli@hp.com>
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.
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/ioport.h>
17 #include <linux/device.h>
18 #include <linux/file.h>
19 #include <linux/cdev.h>
20 #include <linux/spinlock.h>
21 #include <linux/delay.h>
22 #include <linux/uaccess.h>
26 static struct class *ilo_class
;
27 static unsigned int ilo_major
;
28 static char ilo_hwdev
[MAX_ILO_DEV
];
30 static inline int get_entry_id(int entry
)
32 return (entry
& ENTRY_MASK_DESCRIPTOR
) >> ENTRY_BITPOS_DESCRIPTOR
;
35 static inline int get_entry_len(int entry
)
37 return ((entry
& ENTRY_MASK_QWORDS
) >> ENTRY_BITPOS_QWORDS
) << 3;
40 static inline int mk_entry(int id
, int len
)
42 int qlen
= len
& 7 ? (len
>> 3) + 1 : len
>> 3;
43 return id
<< ENTRY_BITPOS_DESCRIPTOR
| qlen
<< ENTRY_BITPOS_QWORDS
;
46 static inline int desc_mem_sz(int nr_entry
)
48 return nr_entry
<< L2_QENTRY_SZ
;
52 * FIFO queues, shared with hardware.
54 * If a queue has empty slots, an entry is added to the queue tail,
55 * and that entry is marked as occupied.
56 * Entries can be dequeued from the head of the list, when the device
57 * has marked the entry as consumed.
59 * Returns true on successful queue/dequeue, false on failure.
61 static int fifo_enqueue(struct ilo_hwinfo
*hw
, char *fifobar
, int entry
)
63 struct fifo
*fifo_q
= FIFOBARTOHANDLE(fifobar
);
66 spin_lock(&hw
->fifo_lock
);
67 if (!(fifo_q
->fifobar
[(fifo_q
->tail
+ 1) & fifo_q
->imask
]
69 fifo_q
->fifobar
[fifo_q
->tail
& fifo_q
->imask
] |=
70 (entry
& ENTRY_MASK_NOSTATE
) | fifo_q
->merge
;
74 spin_unlock(&hw
->fifo_lock
);
79 static int fifo_dequeue(struct ilo_hwinfo
*hw
, char *fifobar
, int *entry
)
81 struct fifo
*fifo_q
= FIFOBARTOHANDLE(fifobar
);
85 spin_lock(&hw
->fifo_lock
);
86 c
= fifo_q
->fifobar
[fifo_q
->head
& fifo_q
->imask
];
87 if (c
& ENTRY_MASK_C
) {
89 *entry
= c
& ENTRY_MASK_NOSTATE
;
91 fifo_q
->fifobar
[fifo_q
->head
& fifo_q
->imask
] =
96 spin_unlock(&hw
->fifo_lock
);
101 static int ilo_pkt_enqueue(struct ilo_hwinfo
*hw
, struct ccb
*ccb
,
102 int dir
, int id
, int len
)
108 fifobar
= ccb
->ccb_u1
.send_fifobar
;
110 fifobar
= ccb
->ccb_u3
.recv_fifobar
;
112 entry
= mk_entry(id
, len
);
113 return fifo_enqueue(hw
, fifobar
, entry
);
116 static int ilo_pkt_dequeue(struct ilo_hwinfo
*hw
, struct ccb
*ccb
,
117 int dir
, int *id
, int *len
, void **pkt
)
119 char *fifobar
, *desc
;
120 int entry
= 0, pkt_id
= 0;
124 fifobar
= ccb
->ccb_u1
.send_fifobar
;
125 desc
= ccb
->ccb_u2
.send_desc
;
127 fifobar
= ccb
->ccb_u3
.recv_fifobar
;
128 desc
= ccb
->ccb_u4
.recv_desc
;
131 ret
= fifo_dequeue(hw
, fifobar
, &entry
);
133 pkt_id
= get_entry_id(entry
);
137 *len
= get_entry_len(entry
);
139 *pkt
= (void *)(desc
+ desc_mem_sz(pkt_id
));
145 static inline void doorbell_set(struct ccb
*ccb
)
147 iowrite8(1, ccb
->ccb_u5
.db_base
);
150 static inline void doorbell_clr(struct ccb
*ccb
)
152 iowrite8(2, ccb
->ccb_u5
.db_base
);
154 static inline int ctrl_set(int l2sz
, int idxmask
, int desclim
)
156 int active
= 0, go
= 1;
157 return l2sz
<< CTRL_BITPOS_L2SZ
|
158 idxmask
<< CTRL_BITPOS_FIFOINDEXMASK
|
159 desclim
<< CTRL_BITPOS_DESCLIMIT
|
160 active
<< CTRL_BITPOS_A
|
163 static void ctrl_setup(struct ccb
*ccb
, int nr_desc
, int l2desc_sz
)
165 /* for simplicity, use the same parameters for send and recv ctrls */
166 ccb
->send_ctrl
= ctrl_set(l2desc_sz
, nr_desc
-1, nr_desc
-1);
167 ccb
->recv_ctrl
= ctrl_set(l2desc_sz
, nr_desc
-1, nr_desc
-1);
170 static inline int fifo_sz(int nr_entry
)
172 /* size of a fifo is determined by the number of entries it contains */
173 return (nr_entry
* sizeof(u64
)) + FIFOHANDLESIZE
;
176 static void fifo_setup(void *base_addr
, int nr_entry
)
178 struct fifo
*fifo_q
= base_addr
;
181 /* set up an empty fifo */
185 fifo_q
->nrents
= nr_entry
;
186 fifo_q
->imask
= nr_entry
- 1;
187 fifo_q
->merge
= ENTRY_MASK_O
;
189 for (i
= 0; i
< nr_entry
; i
++)
190 fifo_q
->fifobar
[i
] = 0;
193 static void ilo_ccb_close(struct pci_dev
*pdev
, struct ccb_data
*data
)
195 struct ccb
*driver_ccb
;
196 struct ccb __iomem
*device_ccb
;
199 driver_ccb
= &data
->driver_ccb
;
200 device_ccb
= data
->mapped_ccb
;
202 /* complicated dance to tell the hw we are stopping */
203 doorbell_clr(driver_ccb
);
204 iowrite32(ioread32(&device_ccb
->send_ctrl
) & ~(1 << CTRL_BITPOS_G
),
205 &device_ccb
->send_ctrl
);
206 iowrite32(ioread32(&device_ccb
->recv_ctrl
) & ~(1 << CTRL_BITPOS_G
),
207 &device_ccb
->recv_ctrl
);
209 /* give iLO some time to process stop request */
210 for (retries
= 1000; retries
> 0; retries
--) {
211 doorbell_set(driver_ccb
);
213 if (!(ioread32(&device_ccb
->send_ctrl
) & (1 << CTRL_BITPOS_A
))
215 !(ioread32(&device_ccb
->recv_ctrl
) & (1 << CTRL_BITPOS_A
)))
219 dev_err(&pdev
->dev
, "Closing, but controller still active\n");
221 /* clear the hw ccb */
222 memset_io(device_ccb
, 0, sizeof(struct ccb
));
224 /* free resources used to back send/recv queues */
225 pci_free_consistent(pdev
, data
->dma_size
, data
->dma_va
, data
->dma_pa
);
228 static int ilo_ccb_open(struct ilo_hwinfo
*hw
, struct ccb_data
*data
, int slot
)
230 char *dma_va
, *dma_pa
;
231 int pkt_id
, pkt_sz
, i
, error
;
232 struct ccb
*driver_ccb
, *ilo_ccb
;
233 struct pci_dev
*pdev
;
235 driver_ccb
= &data
->driver_ccb
;
236 ilo_ccb
= &data
->ilo_ccb
;
239 data
->dma_size
= 2 * fifo_sz(NR_QENTRY
) +
240 2 * desc_mem_sz(NR_QENTRY
) +
241 ILO_START_ALIGN
+ ILO_CACHE_SZ
;
244 data
->dma_va
= pci_alloc_consistent(pdev
, data
->dma_size
,
249 dma_va
= (char *)data
->dma_va
;
250 dma_pa
= (char *)data
->dma_pa
;
252 memset(dma_va
, 0, data
->dma_size
);
254 dma_va
= (char *)roundup((unsigned long)dma_va
, ILO_START_ALIGN
);
255 dma_pa
= (char *)roundup((unsigned long)dma_pa
, ILO_START_ALIGN
);
258 * Create two ccb's, one with virt addrs, one with phys addrs.
259 * Copy the phys addr ccb to device shared mem.
261 ctrl_setup(driver_ccb
, NR_QENTRY
, L2_QENTRY_SZ
);
262 ctrl_setup(ilo_ccb
, NR_QENTRY
, L2_QENTRY_SZ
);
264 fifo_setup(dma_va
, NR_QENTRY
);
265 driver_ccb
->ccb_u1
.send_fifobar
= dma_va
+ FIFOHANDLESIZE
;
266 ilo_ccb
->ccb_u1
.send_fifobar
= dma_pa
+ FIFOHANDLESIZE
;
267 dma_va
+= fifo_sz(NR_QENTRY
);
268 dma_pa
+= fifo_sz(NR_QENTRY
);
270 dma_va
= (char *)roundup((unsigned long)dma_va
, ILO_CACHE_SZ
);
271 dma_pa
= (char *)roundup((unsigned long)dma_pa
, ILO_CACHE_SZ
);
273 fifo_setup(dma_va
, NR_QENTRY
);
274 driver_ccb
->ccb_u3
.recv_fifobar
= dma_va
+ FIFOHANDLESIZE
;
275 ilo_ccb
->ccb_u3
.recv_fifobar
= dma_pa
+ FIFOHANDLESIZE
;
276 dma_va
+= fifo_sz(NR_QENTRY
);
277 dma_pa
+= fifo_sz(NR_QENTRY
);
279 driver_ccb
->ccb_u2
.send_desc
= dma_va
;
280 ilo_ccb
->ccb_u2
.send_desc
= dma_pa
;
281 dma_pa
+= desc_mem_sz(NR_QENTRY
);
282 dma_va
+= desc_mem_sz(NR_QENTRY
);
284 driver_ccb
->ccb_u4
.recv_desc
= dma_va
;
285 ilo_ccb
->ccb_u4
.recv_desc
= dma_pa
;
287 driver_ccb
->channel
= slot
;
288 ilo_ccb
->channel
= slot
;
290 driver_ccb
->ccb_u5
.db_base
= hw
->db_vaddr
+ (slot
<< L2_DB_SIZE
);
291 ilo_ccb
->ccb_u5
.db_base
= NULL
; /* hw ccb's doorbell is not used */
293 /* copy the ccb with physical addrs to device memory */
294 data
->mapped_ccb
= (struct ccb __iomem
*)
295 (hw
->ram_vaddr
+ (slot
* ILOHW_CCB_SZ
));
296 memcpy_toio(data
->mapped_ccb
, ilo_ccb
, sizeof(struct ccb
));
298 /* put packets on the send and receive queues */
300 for (pkt_id
= 0; pkt_id
< NR_QENTRY
; pkt_id
++) {
301 ilo_pkt_enqueue(hw
, driver_ccb
, SENDQ
, pkt_id
, pkt_sz
);
302 doorbell_set(driver_ccb
);
305 pkt_sz
= desc_mem_sz(1);
306 for (pkt_id
= 0; pkt_id
< NR_QENTRY
; pkt_id
++)
307 ilo_pkt_enqueue(hw
, driver_ccb
, RECVQ
, pkt_id
, pkt_sz
);
309 doorbell_clr(driver_ccb
);
311 /* make sure iLO is really handling requests */
312 for (i
= 1000; i
> 0; i
--) {
313 if (ilo_pkt_dequeue(hw
, driver_ccb
, SENDQ
, &pkt_id
, NULL
, NULL
))
319 ilo_pkt_enqueue(hw
, driver_ccb
, SENDQ
, pkt_id
, 0);
320 doorbell_set(driver_ccb
);
322 dev_err(&pdev
->dev
, "Open could not dequeue a packet\n");
329 pci_free_consistent(pdev
, data
->dma_size
, data
->dma_va
, data
->dma_pa
);
334 static inline int is_channel_reset(struct ccb
*ccb
)
336 /* check for this particular channel needing a reset */
337 return FIFOBARTOHANDLE(ccb
->ccb_u1
.send_fifobar
)->reset
;
340 static inline void set_channel_reset(struct ccb
*ccb
)
342 /* set a flag indicating this channel needs a reset */
343 FIFOBARTOHANDLE(ccb
->ccb_u1
.send_fifobar
)->reset
= 1;
346 static inline int is_device_reset(struct ilo_hwinfo
*hw
)
348 /* check for global reset condition */
349 return ioread32(&hw
->mmio_vaddr
[DB_OUT
]) & (1 << DB_RESET
);
352 static inline void clear_device(struct ilo_hwinfo
*hw
)
354 /* clear the device (reset bits, pending channel entries) */
355 iowrite32(-1, &hw
->mmio_vaddr
[DB_OUT
]);
358 static void ilo_locked_reset(struct ilo_hwinfo
*hw
)
363 * Mapped memory is zeroed on ilo reset, so set a per ccb flag
364 * to indicate that this ccb needs to be closed and reopened.
366 for (slot
= 0; slot
< MAX_CCB
; slot
++) {
367 if (!hw
->ccb_alloc
[slot
])
369 set_channel_reset(&hw
->ccb_alloc
[slot
]->driver_ccb
);
375 static void ilo_reset(struct ilo_hwinfo
*hw
)
377 spin_lock(&hw
->alloc_lock
);
379 /* reset might have been handled after lock was taken */
380 if (is_device_reset(hw
))
381 ilo_locked_reset(hw
);
383 spin_unlock(&hw
->alloc_lock
);
386 static ssize_t
ilo_read(struct file
*fp
, char __user
*buf
,
387 size_t len
, loff_t
*off
)
389 int err
, found
, cnt
, pkt_id
, pkt_len
;
390 struct ccb_data
*data
;
391 struct ccb
*driver_ccb
;
392 struct ilo_hwinfo
*hw
;
395 data
= fp
->private_data
;
396 driver_ccb
= &data
->driver_ccb
;
399 if (is_device_reset(hw
) || is_channel_reset(driver_ccb
)) {
401 * If the device has been reset, applications
402 * need to close and reopen all ccbs.
409 * This function is to be called when data is expected
410 * in the channel, and will return an error if no packet is found
411 * during the loop below. The sleep/retry logic is to allow
412 * applications to call read() immediately post write(),
413 * and give iLO some time to process the sent packet.
417 /* look for a received packet */
418 found
= ilo_pkt_dequeue(hw
, driver_ccb
, RECVQ
, &pkt_id
,
424 } while (!found
&& cnt
);
429 /* only copy the length of the received packet */
433 err
= copy_to_user(buf
, pkt
, len
);
435 /* return the received packet to the queue */
436 ilo_pkt_enqueue(hw
, driver_ccb
, RECVQ
, pkt_id
, desc_mem_sz(1));
438 return err
? -EFAULT
: len
;
441 static ssize_t
ilo_write(struct file
*fp
, const char __user
*buf
,
442 size_t len
, loff_t
*off
)
444 int err
, pkt_id
, pkt_len
;
445 struct ccb_data
*data
;
446 struct ccb
*driver_ccb
;
447 struct ilo_hwinfo
*hw
;
450 data
= fp
->private_data
;
451 driver_ccb
= &data
->driver_ccb
;
454 if (is_device_reset(hw
) || is_channel_reset(driver_ccb
)) {
456 * If the device has been reset, applications
457 * need to close and reopen all ccbs.
463 /* get a packet to send the user command */
464 if (!ilo_pkt_dequeue(hw
, driver_ccb
, SENDQ
, &pkt_id
, &pkt_len
, &pkt
))
467 /* limit the length to the length of the packet */
471 /* on failure, set the len to 0 to return empty packet to the device */
472 err
= copy_from_user(pkt
, buf
, len
);
476 /* send the packet */
477 ilo_pkt_enqueue(hw
, driver_ccb
, SENDQ
, pkt_id
, len
);
478 doorbell_set(driver_ccb
);
480 return err
? -EFAULT
: len
;
483 static int ilo_close(struct inode
*ip
, struct file
*fp
)
486 struct ccb_data
*data
;
487 struct ilo_hwinfo
*hw
;
489 slot
= iminor(ip
) % MAX_CCB
;
490 hw
= container_of(ip
->i_cdev
, struct ilo_hwinfo
, cdev
);
492 spin_lock(&hw
->alloc_lock
);
494 if (is_device_reset(hw
))
495 ilo_locked_reset(hw
);
497 if (hw
->ccb_alloc
[slot
]->ccb_cnt
== 1) {
499 data
= fp
->private_data
;
501 ilo_ccb_close(hw
->ilo_dev
, data
);
504 hw
->ccb_alloc
[slot
] = NULL
;
506 hw
->ccb_alloc
[slot
]->ccb_cnt
--;
508 spin_unlock(&hw
->alloc_lock
);
513 static int ilo_open(struct inode
*ip
, struct file
*fp
)
516 struct ccb_data
*data
;
517 struct ilo_hwinfo
*hw
;
519 slot
= iminor(ip
) % MAX_CCB
;
520 hw
= container_of(ip
->i_cdev
, struct ilo_hwinfo
, cdev
);
522 /* new ccb allocation */
523 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
527 spin_lock(&hw
->alloc_lock
);
529 if (is_device_reset(hw
))
530 ilo_locked_reset(hw
);
532 /* each fd private_data holds sw/hw view of ccb */
533 if (hw
->ccb_alloc
[slot
] == NULL
) {
534 /* create a channel control block for this minor */
535 error
= ilo_ccb_open(hw
, data
, slot
);
537 hw
->ccb_alloc
[slot
] = data
;
538 hw
->ccb_alloc
[slot
]->ccb_cnt
= 1;
539 hw
->ccb_alloc
[slot
]->ccb_excl
= fp
->f_flags
& O_EXCL
;
540 hw
->ccb_alloc
[slot
]->ilo_hw
= hw
;
545 if (fp
->f_flags
& O_EXCL
|| hw
->ccb_alloc
[slot
]->ccb_excl
) {
547 * The channel exists, and either this open
548 * or a previous open of this channel wants
553 hw
->ccb_alloc
[slot
]->ccb_cnt
++;
557 spin_unlock(&hw
->alloc_lock
);
560 fp
->private_data
= hw
->ccb_alloc
[slot
];
565 static const struct file_operations ilo_fops
= {
566 .owner
= THIS_MODULE
,
570 .release
= ilo_close
,
573 static void ilo_unmap_device(struct pci_dev
*pdev
, struct ilo_hwinfo
*hw
)
575 pci_iounmap(pdev
, hw
->db_vaddr
);
576 pci_iounmap(pdev
, hw
->ram_vaddr
);
577 pci_iounmap(pdev
, hw
->mmio_vaddr
);
580 static int __devinit
ilo_map_device(struct pci_dev
*pdev
, struct ilo_hwinfo
*hw
)
584 /* map the memory mapped i/o registers */
585 hw
->mmio_vaddr
= pci_iomap(pdev
, 1, 0);
586 if (hw
->mmio_vaddr
== NULL
) {
587 dev_err(&pdev
->dev
, "Error mapping mmio\n");
591 /* map the adapter shared memory region */
592 hw
->ram_vaddr
= pci_iomap(pdev
, 2, MAX_CCB
* ILOHW_CCB_SZ
);
593 if (hw
->ram_vaddr
== NULL
) {
594 dev_err(&pdev
->dev
, "Error mapping shared mem\n");
598 /* map the doorbell aperture */
599 hw
->db_vaddr
= pci_iomap(pdev
, 3, MAX_CCB
* ONE_DB_SIZE
);
600 if (hw
->db_vaddr
== NULL
) {
601 dev_err(&pdev
->dev
, "Error mapping doorbell\n");
607 pci_iounmap(pdev
, hw
->ram_vaddr
);
609 pci_iounmap(pdev
, hw
->mmio_vaddr
);
614 static void ilo_remove(struct pci_dev
*pdev
)
617 struct ilo_hwinfo
*ilo_hw
= pci_get_drvdata(pdev
);
619 clear_device(ilo_hw
);
621 minor
= MINOR(ilo_hw
->cdev
.dev
);
622 for (i
= minor
; i
< minor
+ MAX_CCB
; i
++)
623 device_destroy(ilo_class
, MKDEV(ilo_major
, i
));
625 cdev_del(&ilo_hw
->cdev
);
626 ilo_unmap_device(pdev
, ilo_hw
);
627 pci_release_regions(pdev
);
628 pci_disable_device(pdev
);
630 ilo_hwdev
[(minor
/ MAX_CCB
)] = 0;
633 static int __devinit
ilo_probe(struct pci_dev
*pdev
,
634 const struct pci_device_id
*ent
)
636 int devnum
, minor
, start
, error
;
637 struct ilo_hwinfo
*ilo_hw
;
639 /* find a free range for device files */
640 for (devnum
= 0; devnum
< MAX_ILO_DEV
; devnum
++) {
641 if (ilo_hwdev
[devnum
] == 0) {
642 ilo_hwdev
[devnum
] = 1;
647 if (devnum
== MAX_ILO_DEV
) {
648 dev_err(&pdev
->dev
, "Error finding free device\n");
652 /* track global allocations for this device */
654 ilo_hw
= kzalloc(sizeof(*ilo_hw
), GFP_KERNEL
);
658 ilo_hw
->ilo_dev
= pdev
;
659 spin_lock_init(&ilo_hw
->alloc_lock
);
660 spin_lock_init(&ilo_hw
->fifo_lock
);
662 error
= pci_enable_device(pdev
);
666 pci_set_master(pdev
);
668 error
= pci_request_regions(pdev
, ILO_NAME
);
672 error
= ilo_map_device(pdev
, ilo_hw
);
676 pci_set_drvdata(pdev
, ilo_hw
);
677 clear_device(ilo_hw
);
679 cdev_init(&ilo_hw
->cdev
, &ilo_fops
);
680 ilo_hw
->cdev
.owner
= THIS_MODULE
;
681 start
= devnum
* MAX_CCB
;
682 error
= cdev_add(&ilo_hw
->cdev
, MKDEV(ilo_major
, start
), MAX_CCB
);
684 dev_err(&pdev
->dev
, "Could not add cdev\n");
688 for (minor
= 0 ; minor
< MAX_CCB
; minor
++) {
690 dev
= device_create(ilo_class
, &pdev
->dev
,
691 MKDEV(ilo_major
, minor
), NULL
,
692 "hpilo!d%dccb%d", devnum
, minor
);
694 dev_err(&pdev
->dev
, "Could not create files\n");
699 ilo_unmap_device(pdev
, ilo_hw
);
701 pci_release_regions(pdev
);
703 pci_disable_device(pdev
);
707 ilo_hwdev
[devnum
] = 0;
711 static struct pci_device_id ilo_devices
[] = {
712 { PCI_DEVICE(PCI_VENDOR_ID_COMPAQ
, 0xB204) },
715 MODULE_DEVICE_TABLE(pci
, ilo_devices
);
717 static struct pci_driver ilo_driver
= {
719 .id_table
= ilo_devices
,
721 .remove
= __devexit_p(ilo_remove
),
724 static int __init
ilo_init(void)
729 ilo_class
= class_create(THIS_MODULE
, "iLO");
730 if (IS_ERR(ilo_class
)) {
731 error
= PTR_ERR(ilo_class
);
735 error
= alloc_chrdev_region(&dev
, 0, MAX_OPEN
, ILO_NAME
);
739 ilo_major
= MAJOR(dev
);
741 error
= pci_register_driver(&ilo_driver
);
747 unregister_chrdev_region(dev
, MAX_OPEN
);
749 class_destroy(ilo_class
);
754 static void __exit
ilo_exit(void)
756 pci_unregister_driver(&ilo_driver
);
757 unregister_chrdev_region(MKDEV(ilo_major
, 0), MAX_OPEN
);
758 class_destroy(ilo_class
);
761 MODULE_VERSION("0.05");
762 MODULE_ALIAS(ILO_NAME
);
763 MODULE_DESCRIPTION(ILO_NAME
);
764 MODULE_AUTHOR("David Altobelli <david.altobelli@hp.com>");
765 MODULE_LICENSE("GPL v2");
767 module_init(ilo_init
);
768 module_exit(ilo_exit
);