blob: 8d00df9243c438a45c726007f038284e7cc97ede [file] [log] [blame]
Dhaval Shah92d5f4c2017-12-07 11:01:48 +05301// SPDX-License-Identifier: GPL-2.0
David Altobelli89bcb052008-07-02 09:38:53 -06002/*
dann frazier1ce873a2010-10-15 10:14:34 -06003 * Driver for the HP iLO management processor.
David Altobelli89bcb052008-07-02 09:38:53 -06004 *
5 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
Masanari Iida6b1eb142015-09-18 12:32:13 +09006 * David Altobelli <david.altobelli@hpe.com>
David Altobelli89bcb052008-07-02 09:38:53 -06007 */
8#include <linux/kernel.h>
9#include <linux/types.h>
10#include <linux/module.h>
11#include <linux/fs.h>
12#include <linux/pci.h>
David Altobelli9f704842009-08-17 17:07:33 -060013#include <linux/interrupt.h>
David Altobelli89bcb052008-07-02 09:38:53 -060014#include <linux/ioport.h>
15#include <linux/device.h>
16#include <linux/file.h>
17#include <linux/cdev.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040018#include <linux/sched.h>
David Altobelli89bcb052008-07-02 09:38:53 -060019#include <linux/spinlock.h>
20#include <linux/delay.h>
21#include <linux/uaccess.h>
22#include <linux/io.h>
David Altobelli9f704842009-08-17 17:07:33 -060023#include <linux/wait.h>
David Altobelli4a351472009-08-17 17:08:01 -060024#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
David Altobelli89bcb052008-07-02 09:38:53 -060026#include "hpilo.h"
27
28static struct class *ilo_class;
29static unsigned int ilo_major;
Mark Ruskebf1b762012-11-06 14:33:13 -060030static unsigned int max_ccb = 16;
David Altobelli89bcb052008-07-02 09:38:53 -060031static char ilo_hwdev[MAX_ILO_DEV];
Matt Hsiaobc7de892019-02-27 11:25:29 +080032static const struct pci_device_id ilo_blacklist[] = {
33 /* auxiliary iLO */
34 {PCI_DEVICE_SUB(PCI_VENDOR_ID_HP, 0x3307, PCI_VENDOR_ID_HP, 0x1979)},
Matt Hsiao9b6dba72019-02-27 11:25:30 +080035 /* CL */
36 {PCI_DEVICE_SUB(PCI_VENDOR_ID_HP, 0x3307, PCI_VENDOR_ID_HP_3PAR, 0x0289)},
Matt Hsiaobc7de892019-02-27 11:25:29 +080037 {}
38};
David Altobelli89bcb052008-07-02 09:38:53 -060039
40static inline int get_entry_id(int entry)
41{
42 return (entry & ENTRY_MASK_DESCRIPTOR) >> ENTRY_BITPOS_DESCRIPTOR;
43}
44
45static inline int get_entry_len(int entry)
46{
47 return ((entry & ENTRY_MASK_QWORDS) >> ENTRY_BITPOS_QWORDS) << 3;
48}
49
50static inline int mk_entry(int id, int len)
51{
52 int qlen = len & 7 ? (len >> 3) + 1 : len >> 3;
53 return id << ENTRY_BITPOS_DESCRIPTOR | qlen << ENTRY_BITPOS_QWORDS;
54}
55
56static inline int desc_mem_sz(int nr_entry)
57{
58 return nr_entry << L2_QENTRY_SZ;
59}
60
61/*
62 * FIFO queues, shared with hardware.
63 *
64 * If a queue has empty slots, an entry is added to the queue tail,
65 * and that entry is marked as occupied.
66 * Entries can be dequeued from the head of the list, when the device
67 * has marked the entry as consumed.
68 *
69 * Returns true on successful queue/dequeue, false on failure.
70 */
71static int fifo_enqueue(struct ilo_hwinfo *hw, char *fifobar, int entry)
72{
73 struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
David Altobelli9f704842009-08-17 17:07:33 -060074 unsigned long flags;
David Altobelli89bcb052008-07-02 09:38:53 -060075 int ret = 0;
76
David Altobelli9f704842009-08-17 17:07:33 -060077 spin_lock_irqsave(&hw->fifo_lock, flags);
David Altobelli89bcb052008-07-02 09:38:53 -060078 if (!(fifo_q->fifobar[(fifo_q->tail + 1) & fifo_q->imask]
79 & ENTRY_MASK_O)) {
80 fifo_q->fifobar[fifo_q->tail & fifo_q->imask] |=
81 (entry & ENTRY_MASK_NOSTATE) | fifo_q->merge;
82 fifo_q->tail += 1;
83 ret = 1;
84 }
David Altobelli9f704842009-08-17 17:07:33 -060085 spin_unlock_irqrestore(&hw->fifo_lock, flags);
David Altobelli89bcb052008-07-02 09:38:53 -060086
87 return ret;
88}
89
90static int fifo_dequeue(struct ilo_hwinfo *hw, char *fifobar, int *entry)
91{
92 struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
David Altobelli9f704842009-08-17 17:07:33 -060093 unsigned long flags;
David Altobelli89bcb052008-07-02 09:38:53 -060094 int ret = 0;
95 u64 c;
96
David Altobelli9f704842009-08-17 17:07:33 -060097 spin_lock_irqsave(&hw->fifo_lock, flags);
David Altobelli89bcb052008-07-02 09:38:53 -060098 c = fifo_q->fifobar[fifo_q->head & fifo_q->imask];
99 if (c & ENTRY_MASK_C) {
100 if (entry)
101 *entry = c & ENTRY_MASK_NOSTATE;
102
103 fifo_q->fifobar[fifo_q->head & fifo_q->imask] =
104 (c | ENTRY_MASK) + 1;
105 fifo_q->head += 1;
106 ret = 1;
107 }
David Altobelli9f704842009-08-17 17:07:33 -0600108 spin_unlock_irqrestore(&hw->fifo_lock, flags);
David Altobelli89bcb052008-07-02 09:38:53 -0600109
110 return ret;
111}
112
David Altobelli4a351472009-08-17 17:08:01 -0600113static int fifo_check_recv(struct ilo_hwinfo *hw, char *fifobar)
114{
115 struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
116 unsigned long flags;
117 int ret = 0;
118 u64 c;
119
120 spin_lock_irqsave(&hw->fifo_lock, flags);
121 c = fifo_q->fifobar[fifo_q->head & fifo_q->imask];
122 if (c & ENTRY_MASK_C)
123 ret = 1;
124 spin_unlock_irqrestore(&hw->fifo_lock, flags);
125
126 return ret;
127}
128
David Altobelli89bcb052008-07-02 09:38:53 -0600129static int ilo_pkt_enqueue(struct ilo_hwinfo *hw, struct ccb *ccb,
130 int dir, int id, int len)
131{
132 char *fifobar;
133 int entry;
134
135 if (dir == SENDQ)
136 fifobar = ccb->ccb_u1.send_fifobar;
137 else
138 fifobar = ccb->ccb_u3.recv_fifobar;
139
140 entry = mk_entry(id, len);
141 return fifo_enqueue(hw, fifobar, entry);
142}
143
144static int ilo_pkt_dequeue(struct ilo_hwinfo *hw, struct ccb *ccb,
145 int dir, int *id, int *len, void **pkt)
146{
147 char *fifobar, *desc;
148 int entry = 0, pkt_id = 0;
149 int ret;
150
151 if (dir == SENDQ) {
152 fifobar = ccb->ccb_u1.send_fifobar;
153 desc = ccb->ccb_u2.send_desc;
154 } else {
155 fifobar = ccb->ccb_u3.recv_fifobar;
156 desc = ccb->ccb_u4.recv_desc;
157 }
158
159 ret = fifo_dequeue(hw, fifobar, &entry);
160 if (ret) {
161 pkt_id = get_entry_id(entry);
162 if (id)
163 *id = pkt_id;
164 if (len)
165 *len = get_entry_len(entry);
166 if (pkt)
167 *pkt = (void *)(desc + desc_mem_sz(pkt_id));
168 }
169
170 return ret;
171}
172
David Altobelli4a351472009-08-17 17:08:01 -0600173static int ilo_pkt_recv(struct ilo_hwinfo *hw, struct ccb *ccb)
174{
175 char *fifobar = ccb->ccb_u3.recv_fifobar;
176
177 return fifo_check_recv(hw, fifobar);
178}
179
David Altobelli89bcb052008-07-02 09:38:53 -0600180static inline void doorbell_set(struct ccb *ccb)
181{
182 iowrite8(1, ccb->ccb_u5.db_base);
183}
184
185static inline void doorbell_clr(struct ccb *ccb)
186{
187 iowrite8(2, ccb->ccb_u5.db_base);
188}
David Altobelli66d5e512009-08-17 17:07:03 -0600189
David Altobelli89bcb052008-07-02 09:38:53 -0600190static inline int ctrl_set(int l2sz, int idxmask, int desclim)
191{
192 int active = 0, go = 1;
193 return l2sz << CTRL_BITPOS_L2SZ |
194 idxmask << CTRL_BITPOS_FIFOINDEXMASK |
195 desclim << CTRL_BITPOS_DESCLIMIT |
196 active << CTRL_BITPOS_A |
197 go << CTRL_BITPOS_G;
198}
David Altobelli66d5e512009-08-17 17:07:03 -0600199
David Altobelli89bcb052008-07-02 09:38:53 -0600200static void ctrl_setup(struct ccb *ccb, int nr_desc, int l2desc_sz)
201{
202 /* for simplicity, use the same parameters for send and recv ctrls */
203 ccb->send_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);
204 ccb->recv_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);
205}
206
207static inline int fifo_sz(int nr_entry)
208{
209 /* size of a fifo is determined by the number of entries it contains */
Gustavo A. R. Silvafadbfc32020-07-14 10:44:49 -0500210 return nr_entry * sizeof(u64) + FIFOHANDLESIZE;
David Altobelli89bcb052008-07-02 09:38:53 -0600211}
212
213static void fifo_setup(void *base_addr, int nr_entry)
214{
215 struct fifo *fifo_q = base_addr;
216 int i;
217
218 /* set up an empty fifo */
219 fifo_q->head = 0;
220 fifo_q->tail = 0;
221 fifo_q->reset = 0;
222 fifo_q->nrents = nr_entry;
223 fifo_q->imask = nr_entry - 1;
224 fifo_q->merge = ENTRY_MASK_O;
225
226 for (i = 0; i < nr_entry; i++)
227 fifo_q->fifobar[i] = 0;
228}
229
230static void ilo_ccb_close(struct pci_dev *pdev, struct ccb_data *data)
231{
David Altobelli66d5e512009-08-17 17:07:03 -0600232 struct ccb *driver_ccb = &data->driver_ccb;
233 struct ccb __iomem *device_ccb = data->mapped_ccb;
David Altobelli89bcb052008-07-02 09:38:53 -0600234 int retries;
235
David Altobelli89bcb052008-07-02 09:38:53 -0600236 /* complicated dance to tell the hw we are stopping */
237 doorbell_clr(driver_ccb);
238 iowrite32(ioread32(&device_ccb->send_ctrl) & ~(1 << CTRL_BITPOS_G),
239 &device_ccb->send_ctrl);
240 iowrite32(ioread32(&device_ccb->recv_ctrl) & ~(1 << CTRL_BITPOS_G),
241 &device_ccb->recv_ctrl);
242
243 /* give iLO some time to process stop request */
David Altobellic073b2d2009-02-04 15:11:58 -0800244 for (retries = MAX_WAIT; retries > 0; retries--) {
David Altobelli89bcb052008-07-02 09:38:53 -0600245 doorbell_set(driver_ccb);
David Altobelli891f7d72009-03-31 15:23:53 -0700246 udelay(WAIT_TIME);
David Altobelli89bcb052008-07-02 09:38:53 -0600247 if (!(ioread32(&device_ccb->send_ctrl) & (1 << CTRL_BITPOS_A))
248 &&
249 !(ioread32(&device_ccb->recv_ctrl) & (1 << CTRL_BITPOS_A)))
250 break;
251 }
252 if (retries == 0)
253 dev_err(&pdev->dev, "Closing, but controller still active\n");
254
255 /* clear the hw ccb */
256 memset_io(device_ccb, 0, sizeof(struct ccb));
257
258 /* free resources used to back send/recv queues */
Christophe JAILLET146f90a2020-07-18 09:02:24 +0200259 dma_free_coherent(&pdev->dev, data->dma_size, data->dma_va,
260 data->dma_pa);
David Altobelli89bcb052008-07-02 09:38:53 -0600261}
262
David Altobelli66d5e512009-08-17 17:07:03 -0600263static int ilo_ccb_setup(struct ilo_hwinfo *hw, struct ccb_data *data, int slot)
David Altobelli89bcb052008-07-02 09:38:53 -0600264{
Prarit Bhargavacdf8afc2010-08-09 17:20:27 -0700265 char *dma_va;
266 dma_addr_t dma_pa;
David Altobelli89bcb052008-07-02 09:38:53 -0600267 struct ccb *driver_ccb, *ilo_ccb;
David Altobelli89bcb052008-07-02 09:38:53 -0600268
269 driver_ccb = &data->driver_ccb;
270 ilo_ccb = &data->ilo_ccb;
David Altobelli89bcb052008-07-02 09:38:53 -0600271
272 data->dma_size = 2 * fifo_sz(NR_QENTRY) +
273 2 * desc_mem_sz(NR_QENTRY) +
274 ILO_START_ALIGN + ILO_CACHE_SZ;
275
Christophe JAILLET146f90a2020-07-18 09:02:24 +0200276 data->dma_va = dma_alloc_coherent(&hw->ilo_dev->dev, data->dma_size,
277 &data->dma_pa, GFP_ATOMIC);
David Altobelli89bcb052008-07-02 09:38:53 -0600278 if (!data->dma_va)
David Altobelli66d5e512009-08-17 17:07:03 -0600279 return -ENOMEM;
David Altobelli89bcb052008-07-02 09:38:53 -0600280
281 dma_va = (char *)data->dma_va;
Prarit Bhargavacdf8afc2010-08-09 17:20:27 -0700282 dma_pa = data->dma_pa;
David Altobelli89bcb052008-07-02 09:38:53 -0600283
David Altobelli89bcb052008-07-02 09:38:53 -0600284 dma_va = (char *)roundup((unsigned long)dma_va, ILO_START_ALIGN);
Prarit Bhargavacdf8afc2010-08-09 17:20:27 -0700285 dma_pa = roundup(dma_pa, ILO_START_ALIGN);
David Altobelli89bcb052008-07-02 09:38:53 -0600286
287 /*
288 * Create two ccb's, one with virt addrs, one with phys addrs.
289 * Copy the phys addr ccb to device shared mem.
290 */
291 ctrl_setup(driver_ccb, NR_QENTRY, L2_QENTRY_SZ);
292 ctrl_setup(ilo_ccb, NR_QENTRY, L2_QENTRY_SZ);
293
294 fifo_setup(dma_va, NR_QENTRY);
295 driver_ccb->ccb_u1.send_fifobar = dma_va + FIFOHANDLESIZE;
Prarit Bhargavacdf8afc2010-08-09 17:20:27 -0700296 ilo_ccb->ccb_u1.send_fifobar_pa = dma_pa + FIFOHANDLESIZE;
David Altobelli89bcb052008-07-02 09:38:53 -0600297 dma_va += fifo_sz(NR_QENTRY);
298 dma_pa += fifo_sz(NR_QENTRY);
299
300 dma_va = (char *)roundup((unsigned long)dma_va, ILO_CACHE_SZ);
Prarit Bhargavacdf8afc2010-08-09 17:20:27 -0700301 dma_pa = roundup(dma_pa, ILO_CACHE_SZ);
David Altobelli89bcb052008-07-02 09:38:53 -0600302
303 fifo_setup(dma_va, NR_QENTRY);
304 driver_ccb->ccb_u3.recv_fifobar = dma_va + FIFOHANDLESIZE;
Prarit Bhargavacdf8afc2010-08-09 17:20:27 -0700305 ilo_ccb->ccb_u3.recv_fifobar_pa = dma_pa + FIFOHANDLESIZE;
David Altobelli89bcb052008-07-02 09:38:53 -0600306 dma_va += fifo_sz(NR_QENTRY);
307 dma_pa += fifo_sz(NR_QENTRY);
308
309 driver_ccb->ccb_u2.send_desc = dma_va;
Prarit Bhargavacdf8afc2010-08-09 17:20:27 -0700310 ilo_ccb->ccb_u2.send_desc_pa = dma_pa;
David Altobelli89bcb052008-07-02 09:38:53 -0600311 dma_pa += desc_mem_sz(NR_QENTRY);
312 dma_va += desc_mem_sz(NR_QENTRY);
313
314 driver_ccb->ccb_u4.recv_desc = dma_va;
Prarit Bhargavacdf8afc2010-08-09 17:20:27 -0700315 ilo_ccb->ccb_u4.recv_desc_pa = dma_pa;
David Altobelli89bcb052008-07-02 09:38:53 -0600316
317 driver_ccb->channel = slot;
318 ilo_ccb->channel = slot;
319
320 driver_ccb->ccb_u5.db_base = hw->db_vaddr + (slot << L2_DB_SIZE);
321 ilo_ccb->ccb_u5.db_base = NULL; /* hw ccb's doorbell is not used */
322
David Altobelli66d5e512009-08-17 17:07:03 -0600323 return 0;
324}
325
326static void ilo_ccb_open(struct ilo_hwinfo *hw, struct ccb_data *data, int slot)
327{
328 int pkt_id, pkt_sz;
329 struct ccb *driver_ccb = &data->driver_ccb;
330
David Altobelli89bcb052008-07-02 09:38:53 -0600331 /* copy the ccb with physical addrs to device memory */
332 data->mapped_ccb = (struct ccb __iomem *)
333 (hw->ram_vaddr + (slot * ILOHW_CCB_SZ));
David Altobelli66d5e512009-08-17 17:07:03 -0600334 memcpy_toio(data->mapped_ccb, &data->ilo_ccb, sizeof(struct ccb));
David Altobelli89bcb052008-07-02 09:38:53 -0600335
336 /* put packets on the send and receive queues */
337 pkt_sz = 0;
338 for (pkt_id = 0; pkt_id < NR_QENTRY; pkt_id++) {
339 ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, pkt_sz);
340 doorbell_set(driver_ccb);
341 }
342
343 pkt_sz = desc_mem_sz(1);
344 for (pkt_id = 0; pkt_id < NR_QENTRY; pkt_id++)
345 ilo_pkt_enqueue(hw, driver_ccb, RECVQ, pkt_id, pkt_sz);
346
David Altobelli66d5e512009-08-17 17:07:03 -0600347 /* the ccb is ready to use */
David Altobelli89bcb052008-07-02 09:38:53 -0600348 doorbell_clr(driver_ccb);
David Altobelli66d5e512009-08-17 17:07:03 -0600349}
350
351static int ilo_ccb_verify(struct ilo_hwinfo *hw, struct ccb_data *data)
352{
353 int pkt_id, i;
354 struct ccb *driver_ccb = &data->driver_ccb;
David Altobelli89bcb052008-07-02 09:38:53 -0600355
356 /* make sure iLO is really handling requests */
David Altobellic073b2d2009-02-04 15:11:58 -0800357 for (i = MAX_WAIT; i > 0; i--) {
David Altobelli89bcb052008-07-02 09:38:53 -0600358 if (ilo_pkt_dequeue(hw, driver_ccb, SENDQ, &pkt_id, NULL, NULL))
359 break;
David Altobelli891f7d72009-03-31 15:23:53 -0700360 udelay(WAIT_TIME);
David Altobelli89bcb052008-07-02 09:38:53 -0600361 }
362
David Altobelli66d5e512009-08-17 17:07:03 -0600363 if (i == 0) {
364 dev_err(&hw->ilo_dev->dev, "Open could not dequeue a packet\n");
365 return -EBUSY;
David Altobelli89bcb052008-07-02 09:38:53 -0600366 }
367
David Altobelli66d5e512009-08-17 17:07:03 -0600368 ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, 0);
369 doorbell_set(driver_ccb);
David Altobelli89bcb052008-07-02 09:38:53 -0600370 return 0;
David Altobelli89bcb052008-07-02 09:38:53 -0600371}
372
373static inline int is_channel_reset(struct ccb *ccb)
374{
375 /* check for this particular channel needing a reset */
376 return FIFOBARTOHANDLE(ccb->ccb_u1.send_fifobar)->reset;
377}
378
379static inline void set_channel_reset(struct ccb *ccb)
380{
381 /* set a flag indicating this channel needs a reset */
382 FIFOBARTOHANDLE(ccb->ccb_u1.send_fifobar)->reset = 1;
383}
384
David Altobelli66d5e512009-08-17 17:07:03 -0600385static inline int get_device_outbound(struct ilo_hwinfo *hw)
386{
387 return ioread32(&hw->mmio_vaddr[DB_OUT]);
388}
389
390static inline int is_db_reset(int db_out)
391{
392 return db_out & (1 << DB_RESET);
393}
394
David Altobelli89bcb052008-07-02 09:38:53 -0600395static inline int is_device_reset(struct ilo_hwinfo *hw)
396{
397 /* check for global reset condition */
David Altobelli66d5e512009-08-17 17:07:03 -0600398 return is_db_reset(get_device_outbound(hw));
399}
400
401static inline void clear_pending_db(struct ilo_hwinfo *hw, int clr)
402{
403 iowrite32(clr, &hw->mmio_vaddr[DB_OUT]);
David Altobelli89bcb052008-07-02 09:38:53 -0600404}
405
406static inline void clear_device(struct ilo_hwinfo *hw)
407{
408 /* clear the device (reset bits, pending channel entries) */
David Altobelli66d5e512009-08-17 17:07:03 -0600409 clear_pending_db(hw, -1);
David Altobelli89bcb052008-07-02 09:38:53 -0600410}
411
David Altobelli9f704842009-08-17 17:07:33 -0600412static inline void ilo_enable_interrupts(struct ilo_hwinfo *hw)
413{
414 iowrite8(ioread8(&hw->mmio_vaddr[DB_IRQ]) | 1, &hw->mmio_vaddr[DB_IRQ]);
415}
416
417static inline void ilo_disable_interrupts(struct ilo_hwinfo *hw)
418{
419 iowrite8(ioread8(&hw->mmio_vaddr[DB_IRQ]) & ~1,
420 &hw->mmio_vaddr[DB_IRQ]);
421}
422
423static void ilo_set_reset(struct ilo_hwinfo *hw)
David Altobelli89bcb052008-07-02 09:38:53 -0600424{
425 int slot;
426
427 /*
428 * Mapped memory is zeroed on ilo reset, so set a per ccb flag
429 * to indicate that this ccb needs to be closed and reopened.
430 */
Camuso, Tony98dcd592012-06-10 14:39:20 +0100431 for (slot = 0; slot < max_ccb; slot++) {
David Altobelli89bcb052008-07-02 09:38:53 -0600432 if (!hw->ccb_alloc[slot])
433 continue;
434 set_channel_reset(&hw->ccb_alloc[slot]->driver_ccb);
435 }
David Altobelli89bcb052008-07-02 09:38:53 -0600436}
437
438static ssize_t ilo_read(struct file *fp, char __user *buf,
439 size_t len, loff_t *off)
440{
441 int err, found, cnt, pkt_id, pkt_len;
David Altobelli66d5e512009-08-17 17:07:03 -0600442 struct ccb_data *data = fp->private_data;
443 struct ccb *driver_ccb = &data->driver_ccb;
444 struct ilo_hwinfo *hw = data->ilo_hw;
David Altobelli89bcb052008-07-02 09:38:53 -0600445 void *pkt;
446
David Altobelli9f704842009-08-17 17:07:33 -0600447 if (is_channel_reset(driver_ccb)) {
David Altobelli89bcb052008-07-02 09:38:53 -0600448 /*
449 * If the device has been reset, applications
450 * need to close and reopen all ccbs.
451 */
David Altobelli89bcb052008-07-02 09:38:53 -0600452 return -ENODEV;
453 }
454
455 /*
456 * This function is to be called when data is expected
457 * in the channel, and will return an error if no packet is found
458 * during the loop below. The sleep/retry logic is to allow
459 * applications to call read() immediately post write(),
460 * and give iLO some time to process the sent packet.
461 */
462 cnt = 20;
463 do {
464 /* look for a received packet */
465 found = ilo_pkt_dequeue(hw, driver_ccb, RECVQ, &pkt_id,
466 &pkt_len, &pkt);
467 if (found)
468 break;
469 cnt--;
470 msleep(100);
471 } while (!found && cnt);
472
473 if (!found)
474 return -EAGAIN;
475
476 /* only copy the length of the received packet */
477 if (pkt_len < len)
478 len = pkt_len;
479
480 err = copy_to_user(buf, pkt, len);
481
482 /* return the received packet to the queue */
483 ilo_pkt_enqueue(hw, driver_ccb, RECVQ, pkt_id, desc_mem_sz(1));
484
485 return err ? -EFAULT : len;
486}
487
488static ssize_t ilo_write(struct file *fp, const char __user *buf,
489 size_t len, loff_t *off)
490{
491 int err, pkt_id, pkt_len;
David Altobelli66d5e512009-08-17 17:07:03 -0600492 struct ccb_data *data = fp->private_data;
493 struct ccb *driver_ccb = &data->driver_ccb;
494 struct ilo_hwinfo *hw = data->ilo_hw;
David Altobelli89bcb052008-07-02 09:38:53 -0600495 void *pkt;
496
David Altobelli9f704842009-08-17 17:07:33 -0600497 if (is_channel_reset(driver_ccb))
David Altobelli89bcb052008-07-02 09:38:53 -0600498 return -ENODEV;
David Altobelli89bcb052008-07-02 09:38:53 -0600499
500 /* get a packet to send the user command */
501 if (!ilo_pkt_dequeue(hw, driver_ccb, SENDQ, &pkt_id, &pkt_len, &pkt))
502 return -EBUSY;
503
504 /* limit the length to the length of the packet */
505 if (pkt_len < len)
506 len = pkt_len;
507
508 /* on failure, set the len to 0 to return empty packet to the device */
509 err = copy_from_user(pkt, buf, len);
510 if (err)
511 len = 0;
512
513 /* send the packet */
514 ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, len);
515 doorbell_set(driver_ccb);
516
517 return err ? -EFAULT : len;
518}
519
Al Viroafc9a422017-07-03 06:39:46 -0400520static __poll_t ilo_poll(struct file *fp, poll_table *wait)
David Altobelli4a351472009-08-17 17:08:01 -0600521{
522 struct ccb_data *data = fp->private_data;
523 struct ccb *driver_ccb = &data->driver_ccb;
524
525 poll_wait(fp, &data->ccb_waitq, wait);
526
527 if (is_channel_reset(driver_ccb))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800528 return EPOLLERR;
David Altobelli4a351472009-08-17 17:08:01 -0600529 else if (ilo_pkt_recv(data->ilo_hw, driver_ccb))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800530 return EPOLLIN | EPOLLRDNORM;
David Altobelli4a351472009-08-17 17:08:01 -0600531
532 return 0;
533}
534
David Altobelli89bcb052008-07-02 09:38:53 -0600535static int ilo_close(struct inode *ip, struct file *fp)
536{
537 int slot;
538 struct ccb_data *data;
539 struct ilo_hwinfo *hw;
David Altobelli9f704842009-08-17 17:07:33 -0600540 unsigned long flags;
David Altobelli89bcb052008-07-02 09:38:53 -0600541
Camuso, Tony98dcd592012-06-10 14:39:20 +0100542 slot = iminor(ip) % max_ccb;
David Altobelli89bcb052008-07-02 09:38:53 -0600543 hw = container_of(ip->i_cdev, struct ilo_hwinfo, cdev);
544
David Altobelli9f704842009-08-17 17:07:33 -0600545 spin_lock(&hw->open_lock);
David Altobelli89bcb052008-07-02 09:38:53 -0600546
547 if (hw->ccb_alloc[slot]->ccb_cnt == 1) {
548
549 data = fp->private_data;
550
David Altobelli9f704842009-08-17 17:07:33 -0600551 spin_lock_irqsave(&hw->alloc_lock, flags);
552 hw->ccb_alloc[slot] = NULL;
553 spin_unlock_irqrestore(&hw->alloc_lock, flags);
554
David Altobelli89bcb052008-07-02 09:38:53 -0600555 ilo_ccb_close(hw->ilo_dev, data);
556
557 kfree(data);
David Altobelli89bcb052008-07-02 09:38:53 -0600558 } else
559 hw->ccb_alloc[slot]->ccb_cnt--;
560
David Altobelli9f704842009-08-17 17:07:33 -0600561 spin_unlock(&hw->open_lock);
David Altobelli89bcb052008-07-02 09:38:53 -0600562
563 return 0;
564}
565
566static int ilo_open(struct inode *ip, struct file *fp)
567{
568 int slot, error;
569 struct ccb_data *data;
570 struct ilo_hwinfo *hw;
David Altobelli9f704842009-08-17 17:07:33 -0600571 unsigned long flags;
David Altobelli89bcb052008-07-02 09:38:53 -0600572
Camuso, Tony98dcd592012-06-10 14:39:20 +0100573 slot = iminor(ip) % max_ccb;
David Altobelli89bcb052008-07-02 09:38:53 -0600574 hw = container_of(ip->i_cdev, struct ilo_hwinfo, cdev);
575
576 /* new ccb allocation */
577 data = kzalloc(sizeof(*data), GFP_KERNEL);
578 if (!data)
579 return -ENOMEM;
580
David Altobelli9f704842009-08-17 17:07:33 -0600581 spin_lock(&hw->open_lock);
David Altobelli89bcb052008-07-02 09:38:53 -0600582
583 /* each fd private_data holds sw/hw view of ccb */
584 if (hw->ccb_alloc[slot] == NULL) {
585 /* create a channel control block for this minor */
David Altobelli66d5e512009-08-17 17:07:03 -0600586 error = ilo_ccb_setup(hw, data, slot);
587 if (error) {
David Altobelli89bcb052008-07-02 09:38:53 -0600588 kfree(data);
David Altobelli66d5e512009-08-17 17:07:03 -0600589 goto out;
590 }
591
David Altobelli9f704842009-08-17 17:07:33 -0600592 data->ccb_cnt = 1;
593 data->ccb_excl = fp->f_flags & O_EXCL;
594 data->ilo_hw = hw;
595 init_waitqueue_head(&data->ccb_waitq);
596
David Altobelli66d5e512009-08-17 17:07:03 -0600597 /* write the ccb to hw */
David Altobelli9f704842009-08-17 17:07:33 -0600598 spin_lock_irqsave(&hw->alloc_lock, flags);
David Altobelli66d5e512009-08-17 17:07:03 -0600599 ilo_ccb_open(hw, data, slot);
David Altobelli9f704842009-08-17 17:07:33 -0600600 hw->ccb_alloc[slot] = data;
601 spin_unlock_irqrestore(&hw->alloc_lock, flags);
David Altobelli66d5e512009-08-17 17:07:03 -0600602
603 /* make sure the channel is functional */
604 error = ilo_ccb_verify(hw, data);
605 if (error) {
David Altobelli9f704842009-08-17 17:07:33 -0600606
607 spin_lock_irqsave(&hw->alloc_lock, flags);
608 hw->ccb_alloc[slot] = NULL;
609 spin_unlock_irqrestore(&hw->alloc_lock, flags);
610
David Altobelli66d5e512009-08-17 17:07:03 -0600611 ilo_ccb_close(hw->ilo_dev, data);
David Altobelli9f704842009-08-17 17:07:33 -0600612
David Altobelli66d5e512009-08-17 17:07:03 -0600613 kfree(data);
614 goto out;
615 }
616
David Altobelli89bcb052008-07-02 09:38:53 -0600617 } else {
618 kfree(data);
619 if (fp->f_flags & O_EXCL || hw->ccb_alloc[slot]->ccb_excl) {
620 /*
621 * The channel exists, and either this open
622 * or a previous open of this channel wants
623 * exclusive access.
624 */
625 error = -EBUSY;
626 } else {
627 hw->ccb_alloc[slot]->ccb_cnt++;
628 error = 0;
629 }
630 }
David Altobelli66d5e512009-08-17 17:07:03 -0600631out:
David Altobelli9f704842009-08-17 17:07:33 -0600632 spin_unlock(&hw->open_lock);
David Altobelli89bcb052008-07-02 09:38:53 -0600633
634 if (!error)
635 fp->private_data = hw->ccb_alloc[slot];
636
637 return error;
638}
639
640static const struct file_operations ilo_fops = {
641 .owner = THIS_MODULE,
642 .read = ilo_read,
643 .write = ilo_write,
David Altobelli4a351472009-08-17 17:08:01 -0600644 .poll = ilo_poll,
David Altobelli89bcb052008-07-02 09:38:53 -0600645 .open = ilo_open,
646 .release = ilo_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200647 .llseek = noop_llseek,
David Altobelli89bcb052008-07-02 09:38:53 -0600648};
649
David Altobelli9f704842009-08-17 17:07:33 -0600650static irqreturn_t ilo_isr(int irq, void *data)
651{
652 struct ilo_hwinfo *hw = data;
653 int pending, i;
654
655 spin_lock(&hw->alloc_lock);
656
657 /* check for ccbs which have data */
658 pending = get_device_outbound(hw);
659 if (!pending) {
660 spin_unlock(&hw->alloc_lock);
661 return IRQ_NONE;
662 }
663
664 if (is_db_reset(pending)) {
665 /* wake up all ccbs if the device was reset */
666 pending = -1;
667 ilo_set_reset(hw);
668 }
669
Camuso, Tony98dcd592012-06-10 14:39:20 +0100670 for (i = 0; i < max_ccb; i++) {
David Altobelli9f704842009-08-17 17:07:33 -0600671 if (!hw->ccb_alloc[i])
672 continue;
673 if (pending & (1 << i))
674 wake_up_interruptible(&hw->ccb_alloc[i]->ccb_waitq);
675 }
676
677 /* clear the device of the channels that have been handled */
678 clear_pending_db(hw, pending);
679
680 spin_unlock(&hw->alloc_lock);
681
682 return IRQ_HANDLED;
683}
684
David Altobelli89bcb052008-07-02 09:38:53 -0600685static void ilo_unmap_device(struct pci_dev *pdev, struct ilo_hwinfo *hw)
686{
687 pci_iounmap(pdev, hw->db_vaddr);
688 pci_iounmap(pdev, hw->ram_vaddr);
689 pci_iounmap(pdev, hw->mmio_vaddr);
690}
691
Bill Pemberton80c8ae22012-11-19 13:23:05 -0500692static int ilo_map_device(struct pci_dev *pdev, struct ilo_hwinfo *hw)
David Altobelli89bcb052008-07-02 09:38:53 -0600693{
Rusk, Markc9fef1c2016-09-19 19:50:01 +0000694 int bar;
695 unsigned long off;
Matt Hsiao23d51b82021-05-31 16:55:51 +0800696 u8 pci_rev_id;
697 int rc;
David Altobelli89bcb052008-07-02 09:38:53 -0600698
699 /* map the memory mapped i/o registers */
700 hw->mmio_vaddr = pci_iomap(pdev, 1, 0);
701 if (hw->mmio_vaddr == NULL) {
702 dev_err(&pdev->dev, "Error mapping mmio\n");
703 goto out;
704 }
705
706 /* map the adapter shared memory region */
Matt Hsiao23d51b82021-05-31 16:55:51 +0800707 rc = pci_read_config_byte(pdev, PCI_REVISION_ID, &pci_rev_id);
708 if (rc != 0) {
709 dev_err(&pdev->dev, "Error reading PCI rev id: %d\n", rc);
710 goto out;
711 }
712
713 if (pci_rev_id >= PCI_REV_ID_NECHES) {
Rusk, Markc9fef1c2016-09-19 19:50:01 +0000714 bar = 5;
715 /* Last 8k is reserved for CCBs */
716 off = pci_resource_len(pdev, bar) - 0x2000;
717 } else {
718 bar = 2;
719 off = 0;
720 }
721 hw->ram_vaddr = pci_iomap_range(pdev, bar, off, max_ccb * ILOHW_CCB_SZ);
David Altobelli89bcb052008-07-02 09:38:53 -0600722 if (hw->ram_vaddr == NULL) {
723 dev_err(&pdev->dev, "Error mapping shared mem\n");
724 goto mmio_free;
725 }
726
727 /* map the doorbell aperture */
Camuso, Tony98dcd592012-06-10 14:39:20 +0100728 hw->db_vaddr = pci_iomap(pdev, 3, max_ccb * ONE_DB_SIZE);
David Altobelli89bcb052008-07-02 09:38:53 -0600729 if (hw->db_vaddr == NULL) {
730 dev_err(&pdev->dev, "Error mapping doorbell\n");
731 goto ram_free;
732 }
733
734 return 0;
735ram_free:
736 pci_iounmap(pdev, hw->ram_vaddr);
737mmio_free:
738 pci_iounmap(pdev, hw->mmio_vaddr);
739out:
Rusk, Markc9fef1c2016-09-19 19:50:01 +0000740 return -ENOMEM;
David Altobelli89bcb052008-07-02 09:38:53 -0600741}
742
743static void ilo_remove(struct pci_dev *pdev)
744{
745 int i, minor;
746 struct ilo_hwinfo *ilo_hw = pci_get_drvdata(pdev);
747
Mark Ruskebf1b762012-11-06 14:33:13 -0600748 if (!ilo_hw)
749 return;
750
David Altobelli89bcb052008-07-02 09:38:53 -0600751 clear_device(ilo_hw);
752
753 minor = MINOR(ilo_hw->cdev.dev);
Camuso, Tony98dcd592012-06-10 14:39:20 +0100754 for (i = minor; i < minor + max_ccb; i++)
David Altobelli89bcb052008-07-02 09:38:53 -0600755 device_destroy(ilo_class, MKDEV(ilo_major, i));
756
757 cdev_del(&ilo_hw->cdev);
David Altobelli9f704842009-08-17 17:07:33 -0600758 ilo_disable_interrupts(ilo_hw);
759 free_irq(pdev->irq, ilo_hw);
David Altobelli89bcb052008-07-02 09:38:53 -0600760 ilo_unmap_device(pdev, ilo_hw);
761 pci_release_regions(pdev);
Jiri Slabybcdee042012-09-13 16:06:48 +0200762 /*
763 * pci_disable_device(pdev) used to be here. But this PCI device has
764 * two functions with interrupt lines connected to a single pin. The
765 * other one is a USB host controller. So when we disable the PIN here
766 * e.g. by rmmod hpilo, the controller stops working. It is because
767 * the interrupt link is disabled in ACPI since it is not refcounted
768 * yet. See acpi_pci_link_free_irq called from acpi_pci_irq_disable.
769 */
David Altobelli89bcb052008-07-02 09:38:53 -0600770 kfree(ilo_hw);
Camuso, Tony98dcd592012-06-10 14:39:20 +0100771 ilo_hwdev[(minor / max_ccb)] = 0;
David Altobelli89bcb052008-07-02 09:38:53 -0600772}
773
Bill Pemberton80c8ae22012-11-19 13:23:05 -0500774static int ilo_probe(struct pci_dev *pdev,
David Altobelli89bcb052008-07-02 09:38:53 -0600775 const struct pci_device_id *ent)
776{
Mark Ruskebf1b762012-11-06 14:33:13 -0600777 int devnum, minor, start, error = 0;
David Altobelli89bcb052008-07-02 09:38:53 -0600778 struct ilo_hwinfo *ilo_hw;
779
Matt Hsiaobc7de892019-02-27 11:25:29 +0800780 if (pci_match_id(ilo_blacklist, pdev)) {
781 dev_dbg(&pdev->dev, "Not supported on this device\n");
782 return -ENODEV;
783 }
Mark Ruskebf1b762012-11-06 14:33:13 -0600784
Camuso, Tony98dcd592012-06-10 14:39:20 +0100785 if (max_ccb > MAX_CCB)
786 max_ccb = MAX_CCB;
787 else if (max_ccb < MIN_CCB)
788 max_ccb = MIN_CCB;
789
David Altobelli89bcb052008-07-02 09:38:53 -0600790 /* find a free range for device files */
791 for (devnum = 0; devnum < MAX_ILO_DEV; devnum++) {
792 if (ilo_hwdev[devnum] == 0) {
793 ilo_hwdev[devnum] = 1;
794 break;
795 }
796 }
797
798 if (devnum == MAX_ILO_DEV) {
799 dev_err(&pdev->dev, "Error finding free device\n");
800 return -ENODEV;
801 }
802
803 /* track global allocations for this device */
804 error = -ENOMEM;
805 ilo_hw = kzalloc(sizeof(*ilo_hw), GFP_KERNEL);
806 if (!ilo_hw)
807 goto out;
808
809 ilo_hw->ilo_dev = pdev;
810 spin_lock_init(&ilo_hw->alloc_lock);
811 spin_lock_init(&ilo_hw->fifo_lock);
David Altobelli9f704842009-08-17 17:07:33 -0600812 spin_lock_init(&ilo_hw->open_lock);
David Altobelli89bcb052008-07-02 09:38:53 -0600813
814 error = pci_enable_device(pdev);
815 if (error)
816 goto free;
817
818 pci_set_master(pdev);
819
820 error = pci_request_regions(pdev, ILO_NAME);
821 if (error)
822 goto disable;
823
824 error = ilo_map_device(pdev, ilo_hw);
825 if (error)
826 goto free_regions;
827
828 pci_set_drvdata(pdev, ilo_hw);
829 clear_device(ilo_hw);
830
David Altobelli9f704842009-08-17 17:07:33 -0600831 error = request_irq(pdev->irq, ilo_isr, IRQF_SHARED, "hpilo", ilo_hw);
832 if (error)
833 goto unmap;
834
835 ilo_enable_interrupts(ilo_hw);
836
David Altobelli89bcb052008-07-02 09:38:53 -0600837 cdev_init(&ilo_hw->cdev, &ilo_fops);
838 ilo_hw->cdev.owner = THIS_MODULE;
Camuso, Tony98dcd592012-06-10 14:39:20 +0100839 start = devnum * max_ccb;
840 error = cdev_add(&ilo_hw->cdev, MKDEV(ilo_major, start), max_ccb);
David Altobelli89bcb052008-07-02 09:38:53 -0600841 if (error) {
842 dev_err(&pdev->dev, "Could not add cdev\n");
David Altobelli9f704842009-08-17 17:07:33 -0600843 goto remove_isr;
David Altobelli89bcb052008-07-02 09:38:53 -0600844 }
845
Camuso, Tony98dcd592012-06-10 14:39:20 +0100846 for (minor = 0 ; minor < max_ccb; minor++) {
David Altobelli89bcb052008-07-02 09:38:53 -0600847 struct device *dev;
848 dev = device_create(ilo_class, &pdev->dev,
849 MKDEV(ilo_major, minor), NULL,
850 "hpilo!d%dccb%d", devnum, minor);
851 if (IS_ERR(dev))
852 dev_err(&pdev->dev, "Could not create files\n");
853 }
854
855 return 0;
David Altobelli9f704842009-08-17 17:07:33 -0600856remove_isr:
857 ilo_disable_interrupts(ilo_hw);
858 free_irq(pdev->irq, ilo_hw);
David Altobelli89bcb052008-07-02 09:38:53 -0600859unmap:
860 ilo_unmap_device(pdev, ilo_hw);
861free_regions:
862 pci_release_regions(pdev);
863disable:
Jiri Slabybcdee042012-09-13 16:06:48 +0200864/* pci_disable_device(pdev); see comment in ilo_remove */
David Altobelli89bcb052008-07-02 09:38:53 -0600865free:
866 kfree(ilo_hw);
867out:
868 ilo_hwdev[devnum] = 0;
869 return error;
870}
871
Arvind Yadav8efa6fb2017-07-30 10:56:35 +0530872static const struct pci_device_id ilo_devices[] = {
David Altobelli89bcb052008-07-02 09:38:53 -0600873 { PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB204) },
David Altobelli31d8b562009-02-27 14:03:09 -0800874 { PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3307) },
David Altobelli89bcb052008-07-02 09:38:53 -0600875 { }
876};
877MODULE_DEVICE_TABLE(pci, ilo_devices);
878
879static struct pci_driver ilo_driver = {
880 .name = ILO_NAME,
881 .id_table = ilo_devices,
882 .probe = ilo_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -0500883 .remove = ilo_remove,
David Altobelli89bcb052008-07-02 09:38:53 -0600884};
885
886static int __init ilo_init(void)
887{
888 int error;
889 dev_t dev;
890
891 ilo_class = class_create(THIS_MODULE, "iLO");
892 if (IS_ERR(ilo_class)) {
893 error = PTR_ERR(ilo_class);
894 goto out;
895 }
896
897 error = alloc_chrdev_region(&dev, 0, MAX_OPEN, ILO_NAME);
898 if (error)
899 goto class_destroy;
900
901 ilo_major = MAJOR(dev);
902
903 error = pci_register_driver(&ilo_driver);
904 if (error)
905 goto chr_remove;
906
907 return 0;
908chr_remove:
909 unregister_chrdev_region(dev, MAX_OPEN);
910class_destroy:
911 class_destroy(ilo_class);
912out:
913 return error;
914}
915
916static void __exit ilo_exit(void)
917{
918 pci_unregister_driver(&ilo_driver);
919 unregister_chrdev_region(MKDEV(ilo_major, 0), MAX_OPEN);
920 class_destroy(ilo_class);
921}
922
Rusk, Markc9fef1c2016-09-19 19:50:01 +0000923MODULE_VERSION("1.5.0");
David Altobelli89bcb052008-07-02 09:38:53 -0600924MODULE_ALIAS(ILO_NAME);
925MODULE_DESCRIPTION(ILO_NAME);
Masanari Iida6b1eb142015-09-18 12:32:13 +0900926MODULE_AUTHOR("David Altobelli <david.altobelli@hpe.com>");
David Altobelli89bcb052008-07-02 09:38:53 -0600927MODULE_LICENSE("GPL v2");
928
Camuso, Tony98dcd592012-06-10 14:39:20 +0100929module_param(max_ccb, uint, 0444);
Masanari Iida7a56f322015-09-18 12:32:12 +0900930MODULE_PARM_DESC(max_ccb, "Maximum number of HP iLO channels to attach (8-24)(default=16)");
Camuso, Tony98dcd592012-06-10 14:39:20 +0100931
David Altobelli89bcb052008-07-02 09:38:53 -0600932module_init(ilo_init);
933module_exit(ilo_exit);