Blame view

drivers/misc/hpilo.c 21.8 KB
89bcb05d9   David Altobelli   HP iLO driver
1
  /*
1ce873abe   dann frazier   hpilo: Despecific...
2
   * Driver for the HP iLO management processor.
89bcb05d9   David Altobelli   HP iLO driver
3
4
   *
   * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
6b1eb1450   Masanari Iida   misc: hpilo: Chan...
5
   *	David Altobelli <david.altobelli@hpe.com>
89bcb05d9   David Altobelli   HP iLO driver
6
7
8
9
10
11
12
13
14
15
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   */
  #include <linux/kernel.h>
  #include <linux/types.h>
  #include <linux/module.h>
  #include <linux/fs.h>
  #include <linux/pci.h>
9f7048412   David Altobelli   hpilo: add interr...
16
  #include <linux/interrupt.h>
89bcb05d9   David Altobelli   HP iLO driver
17
18
19
20
  #include <linux/ioport.h>
  #include <linux/device.h>
  #include <linux/file.h>
  #include <linux/cdev.h>
d43c36dc6   Alexey Dobriyan   headers: remove s...
21
  #include <linux/sched.h>
89bcb05d9   David Altobelli   HP iLO driver
22
23
24
25
  #include <linux/spinlock.h>
  #include <linux/delay.h>
  #include <linux/uaccess.h>
  #include <linux/io.h>
9f7048412   David Altobelli   hpilo: add interr...
26
  #include <linux/wait.h>
4a351471d   David Altobelli   hpilo: add poll f_op
27
  #include <linux/poll.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
28
  #include <linux/slab.h>
89bcb05d9   David Altobelli   HP iLO driver
29
30
31
32
  #include "hpilo.h"
  
  static struct class *ilo_class;
  static unsigned int ilo_major;
ebf1b764a   Mark Rusk   misc: hpilo: igno...
33
  static unsigned int max_ccb = 16;
89bcb05d9   David Altobelli   HP iLO driver
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  static char ilo_hwdev[MAX_ILO_DEV];
  
  static inline int get_entry_id(int entry)
  {
  	return (entry & ENTRY_MASK_DESCRIPTOR) >> ENTRY_BITPOS_DESCRIPTOR;
  }
  
  static inline int get_entry_len(int entry)
  {
  	return ((entry & ENTRY_MASK_QWORDS) >> ENTRY_BITPOS_QWORDS) << 3;
  }
  
  static inline int mk_entry(int id, int len)
  {
  	int qlen = len & 7 ? (len >> 3) + 1 : len >> 3;
  	return id << ENTRY_BITPOS_DESCRIPTOR | qlen << ENTRY_BITPOS_QWORDS;
  }
  
  static inline int desc_mem_sz(int nr_entry)
  {
  	return nr_entry << L2_QENTRY_SZ;
  }
  
  /*
   * FIFO queues, shared with hardware.
   *
   * If a queue has empty slots, an entry is added to the queue tail,
   * and that entry is marked as occupied.
   * Entries can be dequeued from the head of the list, when the device
   * has marked the entry as consumed.
   *
   * Returns true on successful queue/dequeue, false on failure.
   */
  static int fifo_enqueue(struct ilo_hwinfo *hw, char *fifobar, int entry)
  {
  	struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
9f7048412   David Altobelli   hpilo: add interr...
70
  	unsigned long flags;
89bcb05d9   David Altobelli   HP iLO driver
71
  	int ret = 0;
9f7048412   David Altobelli   hpilo: add interr...
72
  	spin_lock_irqsave(&hw->fifo_lock, flags);
89bcb05d9   David Altobelli   HP iLO driver
73
74
75
76
77
78
79
  	if (!(fifo_q->fifobar[(fifo_q->tail + 1) & fifo_q->imask]
  	      & ENTRY_MASK_O)) {
  		fifo_q->fifobar[fifo_q->tail & fifo_q->imask] |=
  				(entry & ENTRY_MASK_NOSTATE) | fifo_q->merge;
  		fifo_q->tail += 1;
  		ret = 1;
  	}
9f7048412   David Altobelli   hpilo: add interr...
80
  	spin_unlock_irqrestore(&hw->fifo_lock, flags);
89bcb05d9   David Altobelli   HP iLO driver
81
82
83
84
85
86
87
  
  	return ret;
  }
  
  static int fifo_dequeue(struct ilo_hwinfo *hw, char *fifobar, int *entry)
  {
  	struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
9f7048412   David Altobelli   hpilo: add interr...
88
  	unsigned long flags;
89bcb05d9   David Altobelli   HP iLO driver
89
90
  	int ret = 0;
  	u64 c;
9f7048412   David Altobelli   hpilo: add interr...
91
  	spin_lock_irqsave(&hw->fifo_lock, flags);
89bcb05d9   David Altobelli   HP iLO driver
92
93
94
95
96
97
98
99
100
101
  	c = fifo_q->fifobar[fifo_q->head & fifo_q->imask];
  	if (c & ENTRY_MASK_C) {
  		if (entry)
  			*entry = c & ENTRY_MASK_NOSTATE;
  
  		fifo_q->fifobar[fifo_q->head & fifo_q->imask] =
  							(c | ENTRY_MASK) + 1;
  		fifo_q->head += 1;
  		ret = 1;
  	}
9f7048412   David Altobelli   hpilo: add interr...
102
  	spin_unlock_irqrestore(&hw->fifo_lock, flags);
89bcb05d9   David Altobelli   HP iLO driver
103
104
105
  
  	return ret;
  }
4a351471d   David Altobelli   hpilo: add poll f_op
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  static int fifo_check_recv(struct ilo_hwinfo *hw, char *fifobar)
  {
  	struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
  	unsigned long flags;
  	int ret = 0;
  	u64 c;
  
  	spin_lock_irqsave(&hw->fifo_lock, flags);
  	c = fifo_q->fifobar[fifo_q->head & fifo_q->imask];
  	if (c & ENTRY_MASK_C)
  		ret = 1;
  	spin_unlock_irqrestore(&hw->fifo_lock, flags);
  
  	return ret;
  }
89bcb05d9   David Altobelli   HP iLO driver
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  static int ilo_pkt_enqueue(struct ilo_hwinfo *hw, struct ccb *ccb,
  			   int dir, int id, int len)
  {
  	char *fifobar;
  	int entry;
  
  	if (dir == SENDQ)
  		fifobar = ccb->ccb_u1.send_fifobar;
  	else
  		fifobar = ccb->ccb_u3.recv_fifobar;
  
  	entry = mk_entry(id, len);
  	return fifo_enqueue(hw, fifobar, entry);
  }
  
  static int ilo_pkt_dequeue(struct ilo_hwinfo *hw, struct ccb *ccb,
  			   int dir, int *id, int *len, void **pkt)
  {
  	char *fifobar, *desc;
  	int entry = 0, pkt_id = 0;
  	int ret;
  
  	if (dir == SENDQ) {
  		fifobar = ccb->ccb_u1.send_fifobar;
  		desc = ccb->ccb_u2.send_desc;
  	} else {
  		fifobar = ccb->ccb_u3.recv_fifobar;
  		desc = ccb->ccb_u4.recv_desc;
  	}
  
  	ret = fifo_dequeue(hw, fifobar, &entry);
  	if (ret) {
  		pkt_id = get_entry_id(entry);
  		if (id)
  			*id = pkt_id;
  		if (len)
  			*len = get_entry_len(entry);
  		if (pkt)
  			*pkt = (void *)(desc + desc_mem_sz(pkt_id));
  	}
  
  	return ret;
  }
4a351471d   David Altobelli   hpilo: add poll f_op
164
165
166
167
168
169
  static int ilo_pkt_recv(struct ilo_hwinfo *hw, struct ccb *ccb)
  {
  	char *fifobar = ccb->ccb_u3.recv_fifobar;
  
  	return fifo_check_recv(hw, fifobar);
  }
89bcb05d9   David Altobelli   HP iLO driver
170
171
172
173
174
175
176
177
178
  static inline void doorbell_set(struct ccb *ccb)
  {
  	iowrite8(1, ccb->ccb_u5.db_base);
  }
  
  static inline void doorbell_clr(struct ccb *ccb)
  {
  	iowrite8(2, ccb->ccb_u5.db_base);
  }
66d5e5169   David Altobelli   hpilo: staging fo...
179

89bcb05d9   David Altobelli   HP iLO driver
180
181
182
183
184
185
186
187
188
  static inline int ctrl_set(int l2sz, int idxmask, int desclim)
  {
  	int active = 0, go = 1;
  	return l2sz << CTRL_BITPOS_L2SZ |
  	       idxmask << CTRL_BITPOS_FIFOINDEXMASK |
  	       desclim << CTRL_BITPOS_DESCLIMIT |
  	       active << CTRL_BITPOS_A |
  	       go << CTRL_BITPOS_G;
  }
66d5e5169   David Altobelli   hpilo: staging fo...
189

89bcb05d9   David Altobelli   HP iLO driver
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  static void ctrl_setup(struct ccb *ccb, int nr_desc, int l2desc_sz)
  {
  	/* for simplicity, use the same parameters for send and recv ctrls */
  	ccb->send_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);
  	ccb->recv_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);
  }
  
  static inline int fifo_sz(int nr_entry)
  {
  	/* size of a fifo is determined by the number of entries it contains */
  	return (nr_entry * sizeof(u64)) + FIFOHANDLESIZE;
  }
  
  static void fifo_setup(void *base_addr, int nr_entry)
  {
  	struct fifo *fifo_q = base_addr;
  	int i;
  
  	/* set up an empty fifo */
  	fifo_q->head = 0;
  	fifo_q->tail = 0;
  	fifo_q->reset = 0;
  	fifo_q->nrents = nr_entry;
  	fifo_q->imask = nr_entry - 1;
  	fifo_q->merge = ENTRY_MASK_O;
  
  	for (i = 0; i < nr_entry; i++)
  		fifo_q->fifobar[i] = 0;
  }
  
  static void ilo_ccb_close(struct pci_dev *pdev, struct ccb_data *data)
  {
66d5e5169   David Altobelli   hpilo: staging fo...
222
223
  	struct ccb *driver_ccb = &data->driver_ccb;
  	struct ccb __iomem *device_ccb = data->mapped_ccb;
89bcb05d9   David Altobelli   HP iLO driver
224
  	int retries;
89bcb05d9   David Altobelli   HP iLO driver
225
226
227
228
229
230
231
232
  	/* complicated dance to tell the hw we are stopping */
  	doorbell_clr(driver_ccb);
  	iowrite32(ioread32(&device_ccb->send_ctrl) & ~(1 << CTRL_BITPOS_G),
  		  &device_ccb->send_ctrl);
  	iowrite32(ioread32(&device_ccb->recv_ctrl) & ~(1 << CTRL_BITPOS_G),
  		  &device_ccb->recv_ctrl);
  
  	/* give iLO some time to process stop request */
c073b2db0   David Altobelli   hpilo: open/close...
233
  	for (retries = MAX_WAIT; retries > 0; retries--) {
89bcb05d9   David Altobelli   HP iLO driver
234
  		doorbell_set(driver_ccb);
891f7d73e   David Altobelli   hpilo: reduce fre...
235
  		udelay(WAIT_TIME);
89bcb05d9   David Altobelli   HP iLO driver
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  		if (!(ioread32(&device_ccb->send_ctrl) & (1 << CTRL_BITPOS_A))
  		    &&
  		    !(ioread32(&device_ccb->recv_ctrl) & (1 << CTRL_BITPOS_A)))
  			break;
  	}
  	if (retries == 0)
  		dev_err(&pdev->dev, "Closing, but controller still active
  ");
  
  	/* clear the hw ccb */
  	memset_io(device_ccb, 0, sizeof(struct ccb));
  
  	/* free resources used to back send/recv queues */
  	pci_free_consistent(pdev, data->dma_size, data->dma_va, data->dma_pa);
  }
66d5e5169   David Altobelli   hpilo: staging fo...
251
  static int ilo_ccb_setup(struct ilo_hwinfo *hw, struct ccb_data *data, int slot)
89bcb05d9   David Altobelli   HP iLO driver
252
  {
cdf8afca2   Prarit Bhargava   hpilo: fix pointe...
253
254
  	char *dma_va;
  	dma_addr_t dma_pa;
89bcb05d9   David Altobelli   HP iLO driver
255
  	struct ccb *driver_ccb, *ilo_ccb;
89bcb05d9   David Altobelli   HP iLO driver
256
257
258
  
  	driver_ccb = &data->driver_ccb;
  	ilo_ccb = &data->ilo_ccb;
89bcb05d9   David Altobelli   HP iLO driver
259
260
261
262
  
  	data->dma_size = 2 * fifo_sz(NR_QENTRY) +
  			 2 * desc_mem_sz(NR_QENTRY) +
  			 ILO_START_ALIGN + ILO_CACHE_SZ;
66d5e5169   David Altobelli   hpilo: staging fo...
263
  	data->dma_va = pci_alloc_consistent(hw->ilo_dev, data->dma_size,
89bcb05d9   David Altobelli   HP iLO driver
264
265
  					    &data->dma_pa);
  	if (!data->dma_va)
66d5e5169   David Altobelli   hpilo: staging fo...
266
  		return -ENOMEM;
89bcb05d9   David Altobelli   HP iLO driver
267
268
  
  	dma_va = (char *)data->dma_va;
cdf8afca2   Prarit Bhargava   hpilo: fix pointe...
269
  	dma_pa = data->dma_pa;
89bcb05d9   David Altobelli   HP iLO driver
270
271
272
273
  
  	memset(dma_va, 0, data->dma_size);
  
  	dma_va = (char *)roundup((unsigned long)dma_va, ILO_START_ALIGN);
cdf8afca2   Prarit Bhargava   hpilo: fix pointe...
274
  	dma_pa = roundup(dma_pa, ILO_START_ALIGN);
89bcb05d9   David Altobelli   HP iLO driver
275
276
277
278
279
280
281
282
283
284
  
  	/*
  	 * Create two ccb's, one with virt addrs, one with phys addrs.
  	 * Copy the phys addr ccb to device shared mem.
  	 */
  	ctrl_setup(driver_ccb, NR_QENTRY, L2_QENTRY_SZ);
  	ctrl_setup(ilo_ccb, NR_QENTRY, L2_QENTRY_SZ);
  
  	fifo_setup(dma_va, NR_QENTRY);
  	driver_ccb->ccb_u1.send_fifobar = dma_va + FIFOHANDLESIZE;
cdf8afca2   Prarit Bhargava   hpilo: fix pointe...
285
  	ilo_ccb->ccb_u1.send_fifobar_pa = dma_pa + FIFOHANDLESIZE;
89bcb05d9   David Altobelli   HP iLO driver
286
287
288
289
  	dma_va += fifo_sz(NR_QENTRY);
  	dma_pa += fifo_sz(NR_QENTRY);
  
  	dma_va = (char *)roundup((unsigned long)dma_va, ILO_CACHE_SZ);
cdf8afca2   Prarit Bhargava   hpilo: fix pointe...
290
  	dma_pa = roundup(dma_pa, ILO_CACHE_SZ);
89bcb05d9   David Altobelli   HP iLO driver
291
292
293
  
  	fifo_setup(dma_va, NR_QENTRY);
  	driver_ccb->ccb_u3.recv_fifobar = dma_va + FIFOHANDLESIZE;
cdf8afca2   Prarit Bhargava   hpilo: fix pointe...
294
  	ilo_ccb->ccb_u3.recv_fifobar_pa = dma_pa + FIFOHANDLESIZE;
89bcb05d9   David Altobelli   HP iLO driver
295
296
297
298
  	dma_va += fifo_sz(NR_QENTRY);
  	dma_pa += fifo_sz(NR_QENTRY);
  
  	driver_ccb->ccb_u2.send_desc = dma_va;
cdf8afca2   Prarit Bhargava   hpilo: fix pointe...
299
  	ilo_ccb->ccb_u2.send_desc_pa = dma_pa;
89bcb05d9   David Altobelli   HP iLO driver
300
301
302
303
  	dma_pa += desc_mem_sz(NR_QENTRY);
  	dma_va += desc_mem_sz(NR_QENTRY);
  
  	driver_ccb->ccb_u4.recv_desc = dma_va;
cdf8afca2   Prarit Bhargava   hpilo: fix pointe...
304
  	ilo_ccb->ccb_u4.recv_desc_pa = dma_pa;
89bcb05d9   David Altobelli   HP iLO driver
305
306
307
308
309
310
  
  	driver_ccb->channel = slot;
  	ilo_ccb->channel = slot;
  
  	driver_ccb->ccb_u5.db_base = hw->db_vaddr + (slot << L2_DB_SIZE);
  	ilo_ccb->ccb_u5.db_base = NULL; /* hw ccb's doorbell is not used */
66d5e5169   David Altobelli   hpilo: staging fo...
311
312
313
314
315
316
317
  	return 0;
  }
  
  static void ilo_ccb_open(struct ilo_hwinfo *hw, struct ccb_data *data, int slot)
  {
  	int pkt_id, pkt_sz;
  	struct ccb *driver_ccb = &data->driver_ccb;
89bcb05d9   David Altobelli   HP iLO driver
318
319
320
  	/* copy the ccb with physical addrs to device memory */
  	data->mapped_ccb = (struct ccb __iomem *)
  				(hw->ram_vaddr + (slot * ILOHW_CCB_SZ));
66d5e5169   David Altobelli   hpilo: staging fo...
321
  	memcpy_toio(data->mapped_ccb, &data->ilo_ccb, sizeof(struct ccb));
89bcb05d9   David Altobelli   HP iLO driver
322
323
324
325
326
327
328
329
330
331
332
  
  	/* put packets on the send and receive queues */
  	pkt_sz = 0;
  	for (pkt_id = 0; pkt_id < NR_QENTRY; pkt_id++) {
  		ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, pkt_sz);
  		doorbell_set(driver_ccb);
  	}
  
  	pkt_sz = desc_mem_sz(1);
  	for (pkt_id = 0; pkt_id < NR_QENTRY; pkt_id++)
  		ilo_pkt_enqueue(hw, driver_ccb, RECVQ, pkt_id, pkt_sz);
66d5e5169   David Altobelli   hpilo: staging fo...
333
  	/* the ccb is ready to use */
89bcb05d9   David Altobelli   HP iLO driver
334
  	doorbell_clr(driver_ccb);
66d5e5169   David Altobelli   hpilo: staging fo...
335
336
337
338
339
340
  }
  
  static int ilo_ccb_verify(struct ilo_hwinfo *hw, struct ccb_data *data)
  {
  	int pkt_id, i;
  	struct ccb *driver_ccb = &data->driver_ccb;
89bcb05d9   David Altobelli   HP iLO driver
341
342
  
  	/* make sure iLO is really handling requests */
c073b2db0   David Altobelli   hpilo: open/close...
343
  	for (i = MAX_WAIT; i > 0; i--) {
89bcb05d9   David Altobelli   HP iLO driver
344
345
  		if (ilo_pkt_dequeue(hw, driver_ccb, SENDQ, &pkt_id, NULL, NULL))
  			break;
891f7d73e   David Altobelli   hpilo: reduce fre...
346
  		udelay(WAIT_TIME);
89bcb05d9   David Altobelli   HP iLO driver
347
  	}
66d5e5169   David Altobelli   hpilo: staging fo...
348
349
350
351
  	if (i == 0) {
  		dev_err(&hw->ilo_dev->dev, "Open could not dequeue a packet
  ");
  		return -EBUSY;
89bcb05d9   David Altobelli   HP iLO driver
352
  	}
66d5e5169   David Altobelli   hpilo: staging fo...
353
354
  	ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, 0);
  	doorbell_set(driver_ccb);
89bcb05d9   David Altobelli   HP iLO driver
355
  	return 0;
89bcb05d9   David Altobelli   HP iLO driver
356
357
358
359
360
361
362
363
364
365
366
367
368
  }
  
  static inline int is_channel_reset(struct ccb *ccb)
  {
  	/* check for this particular channel needing a reset */
  	return FIFOBARTOHANDLE(ccb->ccb_u1.send_fifobar)->reset;
  }
  
  static inline void set_channel_reset(struct ccb *ccb)
  {
  	/* set a flag indicating this channel needs a reset */
  	FIFOBARTOHANDLE(ccb->ccb_u1.send_fifobar)->reset = 1;
  }
66d5e5169   David Altobelli   hpilo: staging fo...
369
370
371
372
373
374
375
376
377
  static inline int get_device_outbound(struct ilo_hwinfo *hw)
  {
  	return ioread32(&hw->mmio_vaddr[DB_OUT]);
  }
  
  static inline int is_db_reset(int db_out)
  {
  	return db_out & (1 << DB_RESET);
  }
89bcb05d9   David Altobelli   HP iLO driver
378
379
380
  static inline int is_device_reset(struct ilo_hwinfo *hw)
  {
  	/* check for global reset condition */
66d5e5169   David Altobelli   hpilo: staging fo...
381
382
383
384
385
386
  	return is_db_reset(get_device_outbound(hw));
  }
  
  static inline void clear_pending_db(struct ilo_hwinfo *hw, int clr)
  {
  	iowrite32(clr, &hw->mmio_vaddr[DB_OUT]);
89bcb05d9   David Altobelli   HP iLO driver
387
388
389
390
391
  }
  
  static inline void clear_device(struct ilo_hwinfo *hw)
  {
  	/* clear the device (reset bits, pending channel entries) */
66d5e5169   David Altobelli   hpilo: staging fo...
392
  	clear_pending_db(hw, -1);
89bcb05d9   David Altobelli   HP iLO driver
393
  }
9f7048412   David Altobelli   hpilo: add interr...
394
395
396
397
398
399
400
401
402
403
404
405
  static inline void ilo_enable_interrupts(struct ilo_hwinfo *hw)
  {
  	iowrite8(ioread8(&hw->mmio_vaddr[DB_IRQ]) | 1, &hw->mmio_vaddr[DB_IRQ]);
  }
  
  static inline void ilo_disable_interrupts(struct ilo_hwinfo *hw)
  {
  	iowrite8(ioread8(&hw->mmio_vaddr[DB_IRQ]) & ~1,
  		 &hw->mmio_vaddr[DB_IRQ]);
  }
  
  static void ilo_set_reset(struct ilo_hwinfo *hw)
89bcb05d9   David Altobelli   HP iLO driver
406
407
408
409
410
411
412
  {
  	int slot;
  
  	/*
  	 * Mapped memory is zeroed on ilo reset, so set a per ccb flag
  	 * to indicate that this ccb needs to be closed and reopened.
  	 */
98dcd59dd   Camuso, Tony   misc: hpilo: incr...
413
  	for (slot = 0; slot < max_ccb; slot++) {
89bcb05d9   David Altobelli   HP iLO driver
414
415
416
417
  		if (!hw->ccb_alloc[slot])
  			continue;
  		set_channel_reset(&hw->ccb_alloc[slot]->driver_ccb);
  	}
89bcb05d9   David Altobelli   HP iLO driver
418
419
420
421
422
423
  }
  
  static ssize_t ilo_read(struct file *fp, char __user *buf,
  			size_t len, loff_t *off)
  {
  	int err, found, cnt, pkt_id, pkt_len;
66d5e5169   David Altobelli   hpilo: staging fo...
424
425
426
  	struct ccb_data *data = fp->private_data;
  	struct ccb *driver_ccb = &data->driver_ccb;
  	struct ilo_hwinfo *hw = data->ilo_hw;
89bcb05d9   David Altobelli   HP iLO driver
427
  	void *pkt;
9f7048412   David Altobelli   hpilo: add interr...
428
  	if (is_channel_reset(driver_ccb)) {
89bcb05d9   David Altobelli   HP iLO driver
429
430
431
432
  		/*
  		 * If the device has been reset, applications
  		 * need to close and reopen all ccbs.
  		 */
89bcb05d9   David Altobelli   HP iLO driver
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
  		return -ENODEV;
  	}
  
  	/*
  	 * This function is to be called when data is expected
  	 * in the channel, and will return an error if no packet is found
  	 * during the loop below.  The sleep/retry logic is to allow
  	 * applications to call read() immediately post write(),
  	 * and give iLO some time to process the sent packet.
  	 */
  	cnt = 20;
  	do {
  		/* look for a received packet */
  		found = ilo_pkt_dequeue(hw, driver_ccb, RECVQ, &pkt_id,
  					&pkt_len, &pkt);
  		if (found)
  			break;
  		cnt--;
  		msleep(100);
  	} while (!found && cnt);
  
  	if (!found)
  		return -EAGAIN;
  
  	/* only copy the length of the received packet */
  	if (pkt_len < len)
  		len = pkt_len;
  
  	err = copy_to_user(buf, pkt, len);
  
  	/* return the received packet to the queue */
  	ilo_pkt_enqueue(hw, driver_ccb, RECVQ, pkt_id, desc_mem_sz(1));
  
  	return err ? -EFAULT : len;
  }
  
  static ssize_t ilo_write(struct file *fp, const char __user *buf,
  			 size_t len, loff_t *off)
  {
  	int err, pkt_id, pkt_len;
66d5e5169   David Altobelli   hpilo: staging fo...
473
474
475
  	struct ccb_data *data = fp->private_data;
  	struct ccb *driver_ccb = &data->driver_ccb;
  	struct ilo_hwinfo *hw = data->ilo_hw;
89bcb05d9   David Altobelli   HP iLO driver
476
  	void *pkt;
9f7048412   David Altobelli   hpilo: add interr...
477
  	if (is_channel_reset(driver_ccb))
89bcb05d9   David Altobelli   HP iLO driver
478
  		return -ENODEV;
89bcb05d9   David Altobelli   HP iLO driver
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
  
  	/* get a packet to send the user command */
  	if (!ilo_pkt_dequeue(hw, driver_ccb, SENDQ, &pkt_id, &pkt_len, &pkt))
  		return -EBUSY;
  
  	/* limit the length to the length of the packet */
  	if (pkt_len < len)
  		len = pkt_len;
  
  	/* on failure, set the len to 0 to return empty packet to the device */
  	err = copy_from_user(pkt, buf, len);
  	if (err)
  		len = 0;
  
  	/* send the packet */
  	ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, len);
  	doorbell_set(driver_ccb);
  
  	return err ? -EFAULT : len;
  }
4a351471d   David Altobelli   hpilo: add poll f_op
499
500
501
502
503
504
505
506
507
508
509
510
511
512
  static unsigned int ilo_poll(struct file *fp, poll_table *wait)
  {
  	struct ccb_data *data = fp->private_data;
  	struct ccb *driver_ccb = &data->driver_ccb;
  
  	poll_wait(fp, &data->ccb_waitq, wait);
  
  	if (is_channel_reset(driver_ccb))
  		return POLLERR;
  	else if (ilo_pkt_recv(data->ilo_hw, driver_ccb))
  		return POLLIN | POLLRDNORM;
  
  	return 0;
  }
89bcb05d9   David Altobelli   HP iLO driver
513
514
515
516
517
  static int ilo_close(struct inode *ip, struct file *fp)
  {
  	int slot;
  	struct ccb_data *data;
  	struct ilo_hwinfo *hw;
9f7048412   David Altobelli   hpilo: add interr...
518
  	unsigned long flags;
89bcb05d9   David Altobelli   HP iLO driver
519

98dcd59dd   Camuso, Tony   misc: hpilo: incr...
520
  	slot = iminor(ip) % max_ccb;
89bcb05d9   David Altobelli   HP iLO driver
521
  	hw = container_of(ip->i_cdev, struct ilo_hwinfo, cdev);
9f7048412   David Altobelli   hpilo: add interr...
522
  	spin_lock(&hw->open_lock);
89bcb05d9   David Altobelli   HP iLO driver
523
524
525
526
  
  	if (hw->ccb_alloc[slot]->ccb_cnt == 1) {
  
  		data = fp->private_data;
9f7048412   David Altobelli   hpilo: add interr...
527
528
529
  		spin_lock_irqsave(&hw->alloc_lock, flags);
  		hw->ccb_alloc[slot] = NULL;
  		spin_unlock_irqrestore(&hw->alloc_lock, flags);
89bcb05d9   David Altobelli   HP iLO driver
530
531
532
  		ilo_ccb_close(hw->ilo_dev, data);
  
  		kfree(data);
89bcb05d9   David Altobelli   HP iLO driver
533
534
  	} else
  		hw->ccb_alloc[slot]->ccb_cnt--;
9f7048412   David Altobelli   hpilo: add interr...
535
  	spin_unlock(&hw->open_lock);
89bcb05d9   David Altobelli   HP iLO driver
536
537
538
539
540
541
542
543
544
  
  	return 0;
  }
  
  static int ilo_open(struct inode *ip, struct file *fp)
  {
  	int slot, error;
  	struct ccb_data *data;
  	struct ilo_hwinfo *hw;
9f7048412   David Altobelli   hpilo: add interr...
545
  	unsigned long flags;
89bcb05d9   David Altobelli   HP iLO driver
546

98dcd59dd   Camuso, Tony   misc: hpilo: incr...
547
  	slot = iminor(ip) % max_ccb;
89bcb05d9   David Altobelli   HP iLO driver
548
549
550
551
552
553
  	hw = container_of(ip->i_cdev, struct ilo_hwinfo, cdev);
  
  	/* new ccb allocation */
  	data = kzalloc(sizeof(*data), GFP_KERNEL);
  	if (!data)
  		return -ENOMEM;
9f7048412   David Altobelli   hpilo: add interr...
554
  	spin_lock(&hw->open_lock);
89bcb05d9   David Altobelli   HP iLO driver
555
556
557
558
  
  	/* each fd private_data holds sw/hw view of ccb */
  	if (hw->ccb_alloc[slot] == NULL) {
  		/* create a channel control block for this minor */
66d5e5169   David Altobelli   hpilo: staging fo...
559
560
  		error = ilo_ccb_setup(hw, data, slot);
  		if (error) {
89bcb05d9   David Altobelli   HP iLO driver
561
  			kfree(data);
66d5e5169   David Altobelli   hpilo: staging fo...
562
563
  			goto out;
  		}
9f7048412   David Altobelli   hpilo: add interr...
564
565
566
567
  		data->ccb_cnt = 1;
  		data->ccb_excl = fp->f_flags & O_EXCL;
  		data->ilo_hw = hw;
  		init_waitqueue_head(&data->ccb_waitq);
66d5e5169   David Altobelli   hpilo: staging fo...
568
  		/* write the ccb to hw */
9f7048412   David Altobelli   hpilo: add interr...
569
  		spin_lock_irqsave(&hw->alloc_lock, flags);
66d5e5169   David Altobelli   hpilo: staging fo...
570
  		ilo_ccb_open(hw, data, slot);
9f7048412   David Altobelli   hpilo: add interr...
571
572
  		hw->ccb_alloc[slot] = data;
  		spin_unlock_irqrestore(&hw->alloc_lock, flags);
66d5e5169   David Altobelli   hpilo: staging fo...
573
574
575
576
  
  		/* make sure the channel is functional */
  		error = ilo_ccb_verify(hw, data);
  		if (error) {
9f7048412   David Altobelli   hpilo: add interr...
577
578
579
580
  
  			spin_lock_irqsave(&hw->alloc_lock, flags);
  			hw->ccb_alloc[slot] = NULL;
  			spin_unlock_irqrestore(&hw->alloc_lock, flags);
66d5e5169   David Altobelli   hpilo: staging fo...
581
  			ilo_ccb_close(hw->ilo_dev, data);
9f7048412   David Altobelli   hpilo: add interr...
582

66d5e5169   David Altobelli   hpilo: staging fo...
583
584
585
  			kfree(data);
  			goto out;
  		}
89bcb05d9   David Altobelli   HP iLO driver
586
587
588
589
590
591
592
593
594
595
596
597
598
599
  	} else {
  		kfree(data);
  		if (fp->f_flags & O_EXCL || hw->ccb_alloc[slot]->ccb_excl) {
  			/*
  			 * The channel exists, and either this open
  			 * or a previous open of this channel wants
  			 * exclusive access.
  			 */
  			error = -EBUSY;
  		} else {
  			hw->ccb_alloc[slot]->ccb_cnt++;
  			error = 0;
  		}
  	}
66d5e5169   David Altobelli   hpilo: staging fo...
600
  out:
9f7048412   David Altobelli   hpilo: add interr...
601
  	spin_unlock(&hw->open_lock);
89bcb05d9   David Altobelli   HP iLO driver
602
603
604
605
606
607
608
609
610
611
612
  
  	if (!error)
  		fp->private_data = hw->ccb_alloc[slot];
  
  	return error;
  }
  
  static const struct file_operations ilo_fops = {
  	.owner		= THIS_MODULE,
  	.read		= ilo_read,
  	.write		= ilo_write,
4a351471d   David Altobelli   hpilo: add poll f_op
613
  	.poll		= ilo_poll,
89bcb05d9   David Altobelli   HP iLO driver
614
615
  	.open 		= ilo_open,
  	.release 	= ilo_close,
6038f373a   Arnd Bergmann   llseek: automatic...
616
  	.llseek		= noop_llseek,
89bcb05d9   David Altobelli   HP iLO driver
617
  };
9f7048412   David Altobelli   hpilo: add interr...
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
  static irqreturn_t ilo_isr(int irq, void *data)
  {
  	struct ilo_hwinfo *hw = data;
  	int pending, i;
  
  	spin_lock(&hw->alloc_lock);
  
  	/* check for ccbs which have data */
  	pending = get_device_outbound(hw);
  	if (!pending) {
  		spin_unlock(&hw->alloc_lock);
  		return IRQ_NONE;
  	}
  
  	if (is_db_reset(pending)) {
  		/* wake up all ccbs if the device was reset */
  		pending = -1;
  		ilo_set_reset(hw);
  	}
98dcd59dd   Camuso, Tony   misc: hpilo: incr...
637
  	for (i = 0; i < max_ccb; i++) {
9f7048412   David Altobelli   hpilo: add interr...
638
639
640
641
642
643
644
645
646
647
648
649
650
  		if (!hw->ccb_alloc[i])
  			continue;
  		if (pending & (1 << i))
  			wake_up_interruptible(&hw->ccb_alloc[i]->ccb_waitq);
  	}
  
  	/* clear the device of the channels that have been handled */
  	clear_pending_db(hw, pending);
  
  	spin_unlock(&hw->alloc_lock);
  
  	return IRQ_HANDLED;
  }
89bcb05d9   David Altobelli   HP iLO driver
651
652
653
654
655
656
  static void ilo_unmap_device(struct pci_dev *pdev, struct ilo_hwinfo *hw)
  {
  	pci_iounmap(pdev, hw->db_vaddr);
  	pci_iounmap(pdev, hw->ram_vaddr);
  	pci_iounmap(pdev, hw->mmio_vaddr);
  }
80c8ae289   Bill Pemberton   misc: remove use ...
657
  static int ilo_map_device(struct pci_dev *pdev, struct ilo_hwinfo *hw)
89bcb05d9   David Altobelli   HP iLO driver
658
  {
c9fef1cc3   Rusk, Mark   drivers/misc/hpil...
659
660
  	int bar;
  	unsigned long off;
89bcb05d9   David Altobelli   HP iLO driver
661
662
663
664
665
666
667
668
669
670
  
  	/* map the memory mapped i/o registers */
  	hw->mmio_vaddr = pci_iomap(pdev, 1, 0);
  	if (hw->mmio_vaddr == NULL) {
  		dev_err(&pdev->dev, "Error mapping mmio
  ");
  		goto out;
  	}
  
  	/* map the adapter shared memory region */
c9fef1cc3   Rusk, Mark   drivers/misc/hpil...
671
672
673
674
675
676
677
678
679
  	if (pdev->subsystem_device == 0x00E4) {
  		bar = 5;
  		/* Last 8k is reserved for CCBs */
  		off = pci_resource_len(pdev, bar) - 0x2000;
  	} else {
  		bar = 2;
  		off = 0;
  	}
  	hw->ram_vaddr = pci_iomap_range(pdev, bar, off, max_ccb * ILOHW_CCB_SZ);
89bcb05d9   David Altobelli   HP iLO driver
680
681
682
683
684
685
686
  	if (hw->ram_vaddr == NULL) {
  		dev_err(&pdev->dev, "Error mapping shared mem
  ");
  		goto mmio_free;
  	}
  
  	/* map the doorbell aperture */
98dcd59dd   Camuso, Tony   misc: hpilo: incr...
687
  	hw->db_vaddr = pci_iomap(pdev, 3, max_ccb * ONE_DB_SIZE);
89bcb05d9   David Altobelli   HP iLO driver
688
689
690
691
692
693
694
695
696
697
698
699
  	if (hw->db_vaddr == NULL) {
  		dev_err(&pdev->dev, "Error mapping doorbell
  ");
  		goto ram_free;
  	}
  
  	return 0;
  ram_free:
  	pci_iounmap(pdev, hw->ram_vaddr);
  mmio_free:
  	pci_iounmap(pdev, hw->mmio_vaddr);
  out:
c9fef1cc3   Rusk, Mark   drivers/misc/hpil...
700
  	return -ENOMEM;
89bcb05d9   David Altobelli   HP iLO driver
701
702
703
704
705
706
  }
  
  static void ilo_remove(struct pci_dev *pdev)
  {
  	int i, minor;
  	struct ilo_hwinfo *ilo_hw = pci_get_drvdata(pdev);
ebf1b764a   Mark Rusk   misc: hpilo: igno...
707
708
  	if (!ilo_hw)
  		return;
89bcb05d9   David Altobelli   HP iLO driver
709
710
711
  	clear_device(ilo_hw);
  
  	minor = MINOR(ilo_hw->cdev.dev);
98dcd59dd   Camuso, Tony   misc: hpilo: incr...
712
  	for (i = minor; i < minor + max_ccb; i++)
89bcb05d9   David Altobelli   HP iLO driver
713
714
715
  		device_destroy(ilo_class, MKDEV(ilo_major, i));
  
  	cdev_del(&ilo_hw->cdev);
9f7048412   David Altobelli   hpilo: add interr...
716
717
  	ilo_disable_interrupts(ilo_hw);
  	free_irq(pdev->irq, ilo_hw);
89bcb05d9   David Altobelli   HP iLO driver
718
719
  	ilo_unmap_device(pdev, ilo_hw);
  	pci_release_regions(pdev);
bcdee04ea   Jiri Slaby   MISC: hpilo, remo...
720
721
722
723
724
725
726
727
  	/*
  	 * pci_disable_device(pdev) used to be here. But this PCI device has
  	 * two functions with interrupt lines connected to a single pin. The
  	 * other one is a USB host controller. So when we disable the PIN here
  	 * e.g. by rmmod hpilo, the controller stops working. It is because
  	 * the interrupt link is disabled in ACPI since it is not refcounted
  	 * yet. See acpi_pci_link_free_irq called from acpi_pci_irq_disable.
  	 */
89bcb05d9   David Altobelli   HP iLO driver
728
  	kfree(ilo_hw);
98dcd59dd   Camuso, Tony   misc: hpilo: incr...
729
  	ilo_hwdev[(minor / max_ccb)] = 0;
89bcb05d9   David Altobelli   HP iLO driver
730
  }
80c8ae289   Bill Pemberton   misc: remove use ...
731
  static int ilo_probe(struct pci_dev *pdev,
89bcb05d9   David Altobelli   HP iLO driver
732
733
  			       const struct pci_device_id *ent)
  {
ebf1b764a   Mark Rusk   misc: hpilo: igno...
734
  	int devnum, minor, start, error = 0;
89bcb05d9   David Altobelli   HP iLO driver
735
  	struct ilo_hwinfo *ilo_hw;
ebf1b764a   Mark Rusk   misc: hpilo: igno...
736
737
  	/* Ignore subsystem_device = 0x1979 (set by BIOS)  */
  	if (pdev->subsystem_device == 0x1979)
eefbc594a   Mark Rusk   drivers/misc/hpil...
738
  		return 0;
ebf1b764a   Mark Rusk   misc: hpilo: igno...
739

98dcd59dd   Camuso, Tony   misc: hpilo: incr...
740
741
742
743
  	if (max_ccb > MAX_CCB)
  		max_ccb = MAX_CCB;
  	else if (max_ccb < MIN_CCB)
  		max_ccb = MIN_CCB;
89bcb05d9   David Altobelli   HP iLO driver
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
  	/* find a free range for device files */
  	for (devnum = 0; devnum < MAX_ILO_DEV; devnum++) {
  		if (ilo_hwdev[devnum] == 0) {
  			ilo_hwdev[devnum] = 1;
  			break;
  		}
  	}
  
  	if (devnum == MAX_ILO_DEV) {
  		dev_err(&pdev->dev, "Error finding free device
  ");
  		return -ENODEV;
  	}
  
  	/* track global allocations for this device */
  	error = -ENOMEM;
  	ilo_hw = kzalloc(sizeof(*ilo_hw), GFP_KERNEL);
  	if (!ilo_hw)
  		goto out;
  
  	ilo_hw->ilo_dev = pdev;
  	spin_lock_init(&ilo_hw->alloc_lock);
  	spin_lock_init(&ilo_hw->fifo_lock);
9f7048412   David Altobelli   hpilo: add interr...
767
  	spin_lock_init(&ilo_hw->open_lock);
89bcb05d9   David Altobelli   HP iLO driver
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
  
  	error = pci_enable_device(pdev);
  	if (error)
  		goto free;
  
  	pci_set_master(pdev);
  
  	error = pci_request_regions(pdev, ILO_NAME);
  	if (error)
  		goto disable;
  
  	error = ilo_map_device(pdev, ilo_hw);
  	if (error)
  		goto free_regions;
  
  	pci_set_drvdata(pdev, ilo_hw);
  	clear_device(ilo_hw);
9f7048412   David Altobelli   hpilo: add interr...
785
786
787
788
789
  	error = request_irq(pdev->irq, ilo_isr, IRQF_SHARED, "hpilo", ilo_hw);
  	if (error)
  		goto unmap;
  
  	ilo_enable_interrupts(ilo_hw);
89bcb05d9   David Altobelli   HP iLO driver
790
791
  	cdev_init(&ilo_hw->cdev, &ilo_fops);
  	ilo_hw->cdev.owner = THIS_MODULE;
98dcd59dd   Camuso, Tony   misc: hpilo: incr...
792
793
  	start = devnum * max_ccb;
  	error = cdev_add(&ilo_hw->cdev, MKDEV(ilo_major, start), max_ccb);
89bcb05d9   David Altobelli   HP iLO driver
794
795
796
  	if (error) {
  		dev_err(&pdev->dev, "Could not add cdev
  ");
9f7048412   David Altobelli   hpilo: add interr...
797
  		goto remove_isr;
89bcb05d9   David Altobelli   HP iLO driver
798
  	}
98dcd59dd   Camuso, Tony   misc: hpilo: incr...
799
  	for (minor = 0 ; minor < max_ccb; minor++) {
89bcb05d9   David Altobelli   HP iLO driver
800
801
802
803
804
805
806
807
808
809
  		struct device *dev;
  		dev = device_create(ilo_class, &pdev->dev,
  				    MKDEV(ilo_major, minor), NULL,
  				    "hpilo!d%dccb%d", devnum, minor);
  		if (IS_ERR(dev))
  			dev_err(&pdev->dev, "Could not create files
  ");
  	}
  
  	return 0;
9f7048412   David Altobelli   hpilo: add interr...
810
811
812
  remove_isr:
  	ilo_disable_interrupts(ilo_hw);
  	free_irq(pdev->irq, ilo_hw);
89bcb05d9   David Altobelli   HP iLO driver
813
814
815
816
817
  unmap:
  	ilo_unmap_device(pdev, ilo_hw);
  free_regions:
  	pci_release_regions(pdev);
  disable:
bcdee04ea   Jiri Slaby   MISC: hpilo, remo...
818
  /*	pci_disable_device(pdev);  see comment in ilo_remove */
89bcb05d9   David Altobelli   HP iLO driver
819
820
821
822
823
824
  free:
  	kfree(ilo_hw);
  out:
  	ilo_hwdev[devnum] = 0;
  	return error;
  }
8efa6fb50   Arvind Yadav   misc: hpilo: cons...
825
  static const struct pci_device_id ilo_devices[] = {
89bcb05d9   David Altobelli   HP iLO driver
826
  	{ PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB204) },
31d8b5631   David Altobelli   hpilo: new pci de...
827
  	{ PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3307) },
89bcb05d9   David Altobelli   HP iLO driver
828
829
830
831
832
833
834
835
  	{ }
  };
  MODULE_DEVICE_TABLE(pci, ilo_devices);
  
  static struct pci_driver ilo_driver = {
  	.name 	  = ILO_NAME,
  	.id_table = ilo_devices,
  	.probe 	  = ilo_probe,
2d6bed9ca   Bill Pemberton   drivers/misc: rem...
836
  	.remove   = ilo_remove,
89bcb05d9   David Altobelli   HP iLO driver
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
  };
  
  static int __init ilo_init(void)
  {
  	int error;
  	dev_t dev;
  
  	ilo_class = class_create(THIS_MODULE, "iLO");
  	if (IS_ERR(ilo_class)) {
  		error = PTR_ERR(ilo_class);
  		goto out;
  	}
  
  	error = alloc_chrdev_region(&dev, 0, MAX_OPEN, ILO_NAME);
  	if (error)
  		goto class_destroy;
  
  	ilo_major = MAJOR(dev);
  
  	error =	pci_register_driver(&ilo_driver);
  	if (error)
  		goto chr_remove;
  
  	return 0;
  chr_remove:
  	unregister_chrdev_region(dev, MAX_OPEN);
  class_destroy:
  	class_destroy(ilo_class);
  out:
  	return error;
  }
  
  static void __exit ilo_exit(void)
  {
  	pci_unregister_driver(&ilo_driver);
  	unregister_chrdev_region(MKDEV(ilo_major, 0), MAX_OPEN);
  	class_destroy(ilo_class);
  }
c9fef1cc3   Rusk, Mark   drivers/misc/hpil...
875
  MODULE_VERSION("1.5.0");
89bcb05d9   David Altobelli   HP iLO driver
876
877
  MODULE_ALIAS(ILO_NAME);
  MODULE_DESCRIPTION(ILO_NAME);
6b1eb1450   Masanari Iida   misc: hpilo: Chan...
878
  MODULE_AUTHOR("David Altobelli <david.altobelli@hpe.com>");
89bcb05d9   David Altobelli   HP iLO driver
879
  MODULE_LICENSE("GPL v2");
98dcd59dd   Camuso, Tony   misc: hpilo: incr...
880
  module_param(max_ccb, uint, 0444);
7a56f329c   Masanari Iida   misc: hpilo: Add ...
881
  MODULE_PARM_DESC(max_ccb, "Maximum number of HP iLO channels to attach (8-24)(default=16)");
98dcd59dd   Camuso, Tony   misc: hpilo: incr...
882

89bcb05d9   David Altobelli   HP iLO driver
883
884
  module_init(ilo_init);
  module_exit(ilo_exit);