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