921 lines · 21.8 KB · C
RawDownload
/* * Driver for the HP iLO management processor. * * Copyright (C) 2008 Hewlett-Packard Development Company, L.P. *	David Altobelli <david.altobelli@hpe.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */#include <linux/kernel.h>#include <linux/types.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/pci.h>#include <linux/interrupt.h>#include <linux/ioport.h>#include <linux/device.h>#include <linux/file.h>#include <linux/cdev.h>#include <linux/sched.h>#include <linux/spinlock.h>#include <linux/delay.h>#include <linux/uaccess.h>#include <linux/io.h>#include <linux/wait.h>#include <linux/poll.h>#include <linux/slab.h>#include "hpilo.h" static struct class *ilo_class;static unsigned int ilo_major;static unsigned int max_ccb = 16;static char ilo_hwdev[MAX_ILO_DEV]; static inline int get_entry_id(int entry){	return (entry & ENTRY_MASK_DESCRIPTOR) >> ENTRY_BITPOS_DESCRIPTOR;} static inline int get_entry_len(int entry){	return ((entry & ENTRY_MASK_QWORDS) >> ENTRY_BITPOS_QWORDS) << 3;} static inline int mk_entry(int id, int len){	int qlen = len & 7 ? (len >> 3) + 1 : len >> 3;	return id << ENTRY_BITPOS_DESCRIPTOR | qlen << ENTRY_BITPOS_QWORDS;} static inline int desc_mem_sz(int nr_entry){	return nr_entry << L2_QENTRY_SZ;} /* * FIFO queues, shared with hardware. * * If a queue has empty slots, an entry is added to the queue tail, * and that entry is marked as occupied. * Entries can be dequeued from the head of the list, when the device * has marked the entry as consumed. * * Returns true on successful queue/dequeue, false on failure. */static int fifo_enqueue(struct ilo_hwinfo *hw, char *fifobar, int entry){	struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);	unsigned long flags;	int ret = 0; 	spin_lock_irqsave(&hw->fifo_lock, flags);	if (!(fifo_q->fifobar[(fifo_q->tail + 1) & fifo_q->imask]	      & ENTRY_MASK_O)) {		fifo_q->fifobar[fifo_q->tail & fifo_q->imask] |=				(entry & ENTRY_MASK_NOSTATE) | fifo_q->merge;		fifo_q->tail += 1;		ret = 1;	}	spin_unlock_irqrestore(&hw->fifo_lock, flags); 	return ret;} static int fifo_dequeue(struct ilo_hwinfo *hw, char *fifobar, int *entry){	struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);	unsigned long flags;	int ret = 0;	u64 c; 	spin_lock_irqsave(&hw->fifo_lock, flags);	c = fifo_q->fifobar[fifo_q->head & fifo_q->imask];	if (c & ENTRY_MASK_C) {		if (entry)			*entry = c & ENTRY_MASK_NOSTATE; 		fifo_q->fifobar[fifo_q->head & fifo_q->imask] =							(c | ENTRY_MASK) + 1;		fifo_q->head += 1;		ret = 1;	}	spin_unlock_irqrestore(&hw->fifo_lock, flags); 	return ret;} static int fifo_check_recv(struct ilo_hwinfo *hw, char *fifobar){	struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);	unsigned long flags;	int ret = 0;	u64 c; 	spin_lock_irqsave(&hw->fifo_lock, flags);	c = fifo_q->fifobar[fifo_q->head & fifo_q->imask];	if (c & ENTRY_MASK_C)		ret = 1;	spin_unlock_irqrestore(&hw->fifo_lock, flags); 	return ret;} static int ilo_pkt_enqueue(struct ilo_hwinfo *hw, struct ccb *ccb,			   int dir, int id, int len){	char *fifobar;	int entry; 	if (dir == SENDQ)		fifobar = ccb->ccb_u1.send_fifobar;	else		fifobar = ccb->ccb_u3.recv_fifobar; 	entry = mk_entry(id, len);	return fifo_enqueue(hw, fifobar, entry);} static int ilo_pkt_dequeue(struct ilo_hwinfo *hw, struct ccb *ccb,			   int dir, int *id, int *len, void **pkt){	char *fifobar, *desc;	int entry = 0, pkt_id = 0;	int ret; 	if (dir == SENDQ) {		fifobar = ccb->ccb_u1.send_fifobar;		desc = ccb->ccb_u2.send_desc;	} else {		fifobar = ccb->ccb_u3.recv_fifobar;		desc = ccb->ccb_u4.recv_desc;	} 	ret = fifo_dequeue(hw, fifobar, &entry);	if (ret) {		pkt_id = get_entry_id(entry);		if (id)			*id = pkt_id;		if (len)			*len = get_entry_len(entry);		if (pkt)			*pkt = (void *)(desc + desc_mem_sz(pkt_id));	} 	return ret;} static int ilo_pkt_recv(struct ilo_hwinfo *hw, struct ccb *ccb){	char *fifobar = ccb->ccb_u3.recv_fifobar; 	return fifo_check_recv(hw, fifobar);} static inline void doorbell_set(struct ccb *ccb){	iowrite8(1, ccb->ccb_u5.db_base);} static inline void doorbell_clr(struct ccb *ccb){	iowrite8(2, ccb->ccb_u5.db_base);} static inline int ctrl_set(int l2sz, int idxmask, int desclim){	int active = 0, go = 1;	return l2sz << CTRL_BITPOS_L2SZ |	       idxmask << CTRL_BITPOS_FIFOINDEXMASK |	       desclim << CTRL_BITPOS_DESCLIMIT |	       active << CTRL_BITPOS_A |	       go << CTRL_BITPOS_G;} static void ctrl_setup(struct ccb *ccb, int nr_desc, int l2desc_sz){	/* for simplicity, use the same parameters for send and recv ctrls */	ccb->send_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);	ccb->recv_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);} static inline int fifo_sz(int nr_entry){	/* size of a fifo is determined by the number of entries it contains */	return (nr_entry * sizeof(u64)) + FIFOHANDLESIZE;} static void fifo_setup(void *base_addr, int nr_entry){	struct fifo *fifo_q = base_addr;	int i; 	/* set up an empty fifo */	fifo_q->head = 0;	fifo_q->tail = 0;	fifo_q->reset = 0;	fifo_q->nrents = nr_entry;	fifo_q->imask = nr_entry - 1;	fifo_q->merge = ENTRY_MASK_O; 	for (i = 0; i < nr_entry; i++)		fifo_q->fifobar[i] = 0;} static void ilo_ccb_close(struct pci_dev *pdev, struct ccb_data *data){	struct ccb *driver_ccb = &data->driver_ccb;	struct ccb __iomem *device_ccb = data->mapped_ccb;	int retries; 	/* complicated dance to tell the hw we are stopping */	doorbell_clr(driver_ccb);	iowrite32(ioread32(&device_ccb->send_ctrl) & ~(1 << CTRL_BITPOS_G),		  &device_ccb->send_ctrl);	iowrite32(ioread32(&device_ccb->recv_ctrl) & ~(1 << CTRL_BITPOS_G),		  &device_ccb->recv_ctrl); 	/* give iLO some time to process stop request */	for (retries = MAX_WAIT; retries > 0; retries--) {		doorbell_set(driver_ccb);		udelay(WAIT_TIME);		if (!(ioread32(&device_ccb->send_ctrl) & (1 << CTRL_BITPOS_A))		    &&		    !(ioread32(&device_ccb->recv_ctrl) & (1 << CTRL_BITPOS_A)))			break;	}	if (retries == 0)		dev_err(&pdev->dev, "Closing, but controller still active\n"); 	/* clear the hw ccb */	memset_io(device_ccb, 0, sizeof(struct ccb)); 	/* free resources used to back send/recv queues */	pci_free_consistent(pdev, data->dma_size, data->dma_va, data->dma_pa);} static int ilo_ccb_setup(struct ilo_hwinfo *hw, struct ccb_data *data, int slot){	char *dma_va;	dma_addr_t dma_pa;	struct ccb *driver_ccb, *ilo_ccb; 	driver_ccb = &data->driver_ccb;	ilo_ccb = &data->ilo_ccb; 	data->dma_size = 2 * fifo_sz(NR_QENTRY) +			 2 * desc_mem_sz(NR_QENTRY) +			 ILO_START_ALIGN + ILO_CACHE_SZ; 	data->dma_va = pci_alloc_consistent(hw->ilo_dev, data->dma_size,					    &data->dma_pa);	if (!data->dma_va)		return -ENOMEM; 	dma_va = (char *)data->dma_va;	dma_pa = data->dma_pa; 	memset(dma_va, 0, data->dma_size); 	dma_va = (char *)roundup((unsigned long)dma_va, ILO_START_ALIGN);	dma_pa = roundup(dma_pa, ILO_START_ALIGN); 	/*	 * Create two ccb's, one with virt addrs, one with phys addrs.	 * Copy the phys addr ccb to device shared mem.	 */	ctrl_setup(driver_ccb, NR_QENTRY, L2_QENTRY_SZ);	ctrl_setup(ilo_ccb, NR_QENTRY, L2_QENTRY_SZ); 	fifo_setup(dma_va, NR_QENTRY);	driver_ccb->ccb_u1.send_fifobar = dma_va + FIFOHANDLESIZE;	ilo_ccb->ccb_u1.send_fifobar_pa = dma_pa + FIFOHANDLESIZE;	dma_va += fifo_sz(NR_QENTRY);	dma_pa += fifo_sz(NR_QENTRY); 	dma_va = (char *)roundup((unsigned long)dma_va, ILO_CACHE_SZ);	dma_pa = roundup(dma_pa, ILO_CACHE_SZ); 	fifo_setup(dma_va, NR_QENTRY);	driver_ccb->ccb_u3.recv_fifobar = dma_va + FIFOHANDLESIZE;	ilo_ccb->ccb_u3.recv_fifobar_pa = dma_pa + FIFOHANDLESIZE;	dma_va += fifo_sz(NR_QENTRY);	dma_pa += fifo_sz(NR_QENTRY); 	driver_ccb->ccb_u2.send_desc = dma_va;	ilo_ccb->ccb_u2.send_desc_pa = dma_pa;	dma_pa += desc_mem_sz(NR_QENTRY);	dma_va += desc_mem_sz(NR_QENTRY); 	driver_ccb->ccb_u4.recv_desc = dma_va;	ilo_ccb->ccb_u4.recv_desc_pa = dma_pa; 	driver_ccb->channel = slot;	ilo_ccb->channel = slot; 	driver_ccb->ccb_u5.db_base = hw->db_vaddr + (slot << L2_DB_SIZE);	ilo_ccb->ccb_u5.db_base = NULL; /* hw ccb's doorbell is not used */ 	return 0;} static void ilo_ccb_open(struct ilo_hwinfo *hw, struct ccb_data *data, int slot){	int pkt_id, pkt_sz;	struct ccb *driver_ccb = &data->driver_ccb; 	/* copy the ccb with physical addrs to device memory */	data->mapped_ccb = (struct ccb __iomem *)				(hw->ram_vaddr + (slot * ILOHW_CCB_SZ));	memcpy_toio(data->mapped_ccb, &data->ilo_ccb, sizeof(struct ccb)); 	/* put packets on the send and receive queues */	pkt_sz = 0;	for (pkt_id = 0; pkt_id < NR_QENTRY; pkt_id++) {		ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, pkt_sz);		doorbell_set(driver_ccb);	} 	pkt_sz = desc_mem_sz(1);	for (pkt_id = 0; pkt_id < NR_QENTRY; pkt_id++)		ilo_pkt_enqueue(hw, driver_ccb, RECVQ, pkt_id, pkt_sz); 	/* the ccb is ready to use */	doorbell_clr(driver_ccb);} static int ilo_ccb_verify(struct ilo_hwinfo *hw, struct ccb_data *data){	int pkt_id, i;	struct ccb *driver_ccb = &data->driver_ccb; 	/* make sure iLO is really handling requests */	for (i = MAX_WAIT; i > 0; i--) {		if (ilo_pkt_dequeue(hw, driver_ccb, SENDQ, &pkt_id, NULL, NULL))			break;		udelay(WAIT_TIME);	} 	if (i == 0) {		dev_err(&hw->ilo_dev->dev, "Open could not dequeue a packet\n");		return -EBUSY;	} 	ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, 0);	doorbell_set(driver_ccb);	return 0;} static inline int is_channel_reset(struct ccb *ccb){	/* check for this particular channel needing a reset */	return FIFOBARTOHANDLE(ccb->ccb_u1.send_fifobar)->reset;} static inline void set_channel_reset(struct ccb *ccb){	/* set a flag indicating this channel needs a reset */	FIFOBARTOHANDLE(ccb->ccb_u1.send_fifobar)->reset = 1;} static inline int get_device_outbound(struct ilo_hwinfo *hw){	return ioread32(&hw->mmio_vaddr[DB_OUT]);} static inline int is_db_reset(int db_out){	return db_out & (1 << DB_RESET);} static inline int is_device_reset(struct ilo_hwinfo *hw){	/* check for global reset condition */	return is_db_reset(get_device_outbound(hw));} static inline void clear_pending_db(struct ilo_hwinfo *hw, int clr){	iowrite32(clr, &hw->mmio_vaddr[DB_OUT]);} static inline void clear_device(struct ilo_hwinfo *hw){	/* clear the device (reset bits, pending channel entries) */	clear_pending_db(hw, -1);} static inline void ilo_enable_interrupts(struct ilo_hwinfo *hw){	iowrite8(ioread8(&hw->mmio_vaddr[DB_IRQ]) | 1, &hw->mmio_vaddr[DB_IRQ]);} static inline void ilo_disable_interrupts(struct ilo_hwinfo *hw){	iowrite8(ioread8(&hw->mmio_vaddr[DB_IRQ]) & ~1,		 &hw->mmio_vaddr[DB_IRQ]);} static void ilo_set_reset(struct ilo_hwinfo *hw){	int slot; 	/*	 * Mapped memory is zeroed on ilo reset, so set a per ccb flag	 * to indicate that this ccb needs to be closed and reopened.	 */	for (slot = 0; slot < max_ccb; slot++) {		if (!hw->ccb_alloc[slot])			continue;		set_channel_reset(&hw->ccb_alloc[slot]->driver_ccb);	}} static ssize_t ilo_read(struct file *fp, char __user *buf,			size_t len, loff_t *off){	int err, found, cnt, pkt_id, pkt_len;	struct ccb_data *data = fp->private_data;	struct ccb *driver_ccb = &data->driver_ccb;	struct ilo_hwinfo *hw = data->ilo_hw;	void *pkt; 	if (is_channel_reset(driver_ccb)) {		/*		 * If the device has been reset, applications		 * need to close and reopen all ccbs.		 */		return -ENODEV;	} 	/*	 * This function is to be called when data is expected	 * in the channel, and will return an error if no packet is found	 * during the loop below.  The sleep/retry logic is to allow	 * applications to call read() immediately post write(),	 * and give iLO some time to process the sent packet.	 */	cnt = 20;	do {		/* look for a received packet */		found = ilo_pkt_dequeue(hw, driver_ccb, RECVQ, &pkt_id,					&pkt_len, &pkt);		if (found)			break;		cnt--;		msleep(100);	} while (!found && cnt); 	if (!found)		return -EAGAIN; 	/* only copy the length of the received packet */	if (pkt_len < len)		len = pkt_len; 	err = copy_to_user(buf, pkt, len); 	/* return the received packet to the queue */	ilo_pkt_enqueue(hw, driver_ccb, RECVQ, pkt_id, desc_mem_sz(1)); 	return err ? -EFAULT : len;} static ssize_t ilo_write(struct file *fp, const char __user *buf,			 size_t len, loff_t *off){	int err, pkt_id, pkt_len;	struct ccb_data *data = fp->private_data;	struct ccb *driver_ccb = &data->driver_ccb;	struct ilo_hwinfo *hw = data->ilo_hw;	void *pkt; 	if (is_channel_reset(driver_ccb))		return -ENODEV; 	/* get a packet to send the user command */	if (!ilo_pkt_dequeue(hw, driver_ccb, SENDQ, &pkt_id, &pkt_len, &pkt))		return -EBUSY; 

Lines 1–500 of 921.