11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/module.h>
15 #include <linux/pci.h>
18 #include <linux/device.h>
21 #include <linux/sched.h>
26 #include <linux/wait.h>
27 #include <linux/poll.h>
28 #include <linux/slab.h>
31 static struct class *ilo_class;
32 static unsigned int ilo_major;
33 static unsigned int max_ccb =
MIN_CCB;
36 static inline int get_entry_id(
int entry)
41 static inline int get_entry_len(
int entry)
46 static inline int mk_entry(
int id,
int len)
48 int qlen = len & 7 ? (len >> 3) + 1 : len >> 3;
52 static inline int desc_mem_sz(
int nr_entry)
67 static int fifo_enqueue(
struct ilo_hwinfo *
hw,
char *fifobar,
int entry)
81 spin_unlock_irqrestore(&hw->
fifo_lock, flags);
86 static int fifo_dequeue(
struct ilo_hwinfo *hw,
char *fifobar,
int *entry)
104 spin_unlock_irqrestore(&hw->
fifo_lock, flags);
109 static int fifo_check_recv(
struct ilo_hwinfo *hw,
char *fifobar)
118 if (c & ENTRY_MASK_C)
120 spin_unlock_irqrestore(&hw->
fifo_lock, flags);
126 int dir,
int id,
int len)
136 entry = mk_entry(
id, len);
137 return fifo_enqueue(hw, fifobar, entry);
140 static int ilo_pkt_dequeue(
struct ilo_hwinfo *hw,
struct ccb *ccb,
141 int dir,
int *
id,
int *len,
void **pkt)
144 int entry = 0, pkt_id = 0;
155 ret = fifo_dequeue(hw, fifobar, &entry);
157 pkt_id = get_entry_id(entry);
161 *len = get_entry_len(entry);
163 *pkt = (
void *)(desc + desc_mem_sz(pkt_id));
169 static int ilo_pkt_recv(
struct ilo_hwinfo *hw,
struct ccb *ccb)
173 return fifo_check_recv(hw, fifobar);
176 static inline void doorbell_set(
struct ccb *ccb)
181 static inline void doorbell_clr(
struct ccb *ccb)
186 static inline int ctrl_set(
int l2sz,
int idxmask,
int desclim)
196 static void ctrl_setup(
struct ccb *ccb,
int nr_desc,
int l2desc_sz)
199 ccb->
send_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);
200 ccb->
recv_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);
203 static inline int fifo_sz(
int nr_entry)
209 static void fifo_setup(
void *base_addr,
int nr_entry)
211 struct fifo *fifo_q = base_addr;
218 fifo_q->
nrents = nr_entry;
219 fifo_q->
imask = nr_entry - 1;
222 for (i = 0; i < nr_entry; i++)
233 doorbell_clr(driver_ccb);
240 for (retries =
MAX_WAIT; retries > 0; retries--) {
241 doorbell_set(driver_ccb);
249 dev_err(&pdev->
dev,
"Closing, but controller still active\n");
252 memset_io(device_ccb, 0,
sizeof(
struct ccb));
262 struct ccb *driver_ccb, *ilo_ccb;
276 dma_va = (
char *)data->
dma_va;
298 dma_pa =
roundup(dma_pa, ILO_CACHE_SZ);
335 for (pkt_id = 0; pkt_id <
NR_QENTRY; pkt_id++) {
336 ilo_pkt_enqueue(hw, driver_ccb,
SENDQ, pkt_id, pkt_sz);
337 doorbell_set(driver_ccb);
340 pkt_sz = desc_mem_sz(1);
341 for (pkt_id = 0; pkt_id <
NR_QENTRY; pkt_id++)
342 ilo_pkt_enqueue(hw, driver_ccb,
RECVQ, pkt_id, pkt_sz);
345 doorbell_clr(driver_ccb);
355 if (ilo_pkt_dequeue(hw, driver_ccb,
SENDQ, &pkt_id,
NULL,
NULL))
365 ilo_pkt_enqueue(hw, driver_ccb,
SENDQ, pkt_id, 0);
366 doorbell_set(driver_ccb);
370 static inline int is_channel_reset(
struct ccb *ccb)
376 static inline void set_channel_reset(
struct ccb *ccb)
382 static inline int get_device_outbound(
struct ilo_hwinfo *hw)
387 static inline int is_db_reset(
int db_out)
392 static inline int is_device_reset(
struct ilo_hwinfo *hw)
395 return is_db_reset(get_device_outbound(hw));
398 static inline void clear_pending_db(
struct ilo_hwinfo *hw,
int clr)
403 static inline void clear_device(
struct ilo_hwinfo *hw)
406 clear_pending_db(hw, -1);
409 static inline void ilo_enable_interrupts(
struct ilo_hwinfo *hw)
414 static inline void ilo_disable_interrupts(
struct ilo_hwinfo *hw)
420 static void ilo_set_reset(
struct ilo_hwinfo *hw)
428 for (slot = 0; slot < max_ccb; slot++) {
431 set_channel_reset(&hw->
ccb_alloc[slot]->driver_ccb);
436 size_t len, loff_t *off)
444 if (is_channel_reset(driver_ccb)) {
462 found = ilo_pkt_dequeue(hw, driver_ccb,
RECVQ, &pkt_id,
468 }
while (!found && cnt);
480 ilo_pkt_enqueue(hw, driver_ccb,
RECVQ, pkt_id, desc_mem_sz(1));
482 return err ? -
EFAULT : len;
485 static ssize_t ilo_write(
struct file *fp,
const char __user *buf,
486 size_t len, loff_t *off)
494 if (is_channel_reset(driver_ccb))
498 if (!ilo_pkt_dequeue(hw, driver_ccb,
SENDQ, &pkt_id, &pkt_len, &pkt))
511 ilo_pkt_enqueue(hw, driver_ccb,
SENDQ, pkt_id, len);
512 doorbell_set(driver_ccb);
514 return err ? -
EFAULT : len;
524 if (is_channel_reset(driver_ccb))
526 else if (ilo_pkt_recv(data->
ilo_hw, driver_ccb))
532 static int ilo_close(
struct inode *
ip,
struct file *fp)
539 slot = iminor(ip) % max_ccb;
550 spin_unlock_irqrestore(&hw->
alloc_lock, flags);
552 ilo_ccb_close(hw->
ilo_dev, data);
563 static int ilo_open(
struct inode *ip,
struct file *fp)
570 slot = iminor(ip) % max_ccb;
583 error = ilo_ccb_setup(hw, data, slot);
596 ilo_ccb_open(hw, data, slot);
598 spin_unlock_irqrestore(&hw->
alloc_lock, flags);
601 error = ilo_ccb_verify(hw, data);
606 spin_unlock_irqrestore(&hw->
alloc_lock, flags);
608 ilo_ccb_close(hw->
ilo_dev, data);
643 .release = ilo_close,
655 pending = get_device_outbound(hw);
661 if (is_db_reset(pending)) {
667 for (i = 0; i < max_ccb; i++) {
670 if (pending & (1 << i))
675 clear_pending_db(hw, pending);
701 hw->
ram_vaddr = pci_iomap(pdev, 2, max_ccb * ILOHW_CCB_SZ);
703 dev_err(&pdev->
dev,
"Error mapping shared mem\n");
710 dev_err(&pdev->
dev,
"Error mapping doorbell\n");
723 static void ilo_remove(
struct pci_dev *pdev)
726 struct ilo_hwinfo *ilo_hw = pci_get_drvdata(pdev);
728 clear_device(ilo_hw);
731 for (i = minor; i < minor + max_ccb; i++)
735 ilo_disable_interrupts(ilo_hw);
737 ilo_unmap_device(pdev, ilo_hw);
748 ilo_hwdev[(minor / max_ccb)] = 0;
764 if (ilo_hwdev[devnum] == 0) {
770 if (devnum == MAX_ILO_DEV) {
771 dev_err(&pdev->
dev,
"Error finding free device\n");
777 ilo_hw = kzalloc(
sizeof(*ilo_hw),
GFP_KERNEL);
796 error = ilo_map_device(pdev, ilo_hw);
800 pci_set_drvdata(pdev, ilo_hw);
801 clear_device(ilo_hw);
807 ilo_enable_interrupts(ilo_hw);
811 start = devnum * max_ccb;
818 for (minor = 0 ; minor < max_ccb; minor++) {
822 "hpilo!d%dccb%d", devnum, minor);
824 dev_err(&pdev->
dev,
"Could not create files\n");
829 ilo_disable_interrupts(ilo_hw);
832 ilo_unmap_device(pdev, ilo_hw);
853 .id_table = ilo_devices,
858 static int __init ilo_init(
void)
864 if (IS_ERR(ilo_class)) {
865 error = PTR_ERR(ilo_class);
873 ilo_major =
MAJOR(dev);
875 error = pci_register_driver(&ilo_driver);
888 static void __exit ilo_exit(
void)
902 MODULE_PARM_DESC(max_ccb,
"Maximum number of HP iLO channels to attach (8)");