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