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