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