1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2009, Intel Corporation.
* All rights reserved.
*/
#include <sys/conf.h>
#include <sys/cmn_err.h>
#include <sys/ddi.h>
#include <sys/file.h>
#include <sys/modctl.h>
#include <sys/pci.h>
#include <sys/policy.h>
#include <sys/stat.h>
#include <sys/sunddi.h>
#include <sys/synch.h>
#include <sys/fipe.h>
/* Configurable through /etc/system. */
int fipe_allow_attach = 1;
int fipe_allow_detach = 1;
static kmutex_t fipe_drv_lock;
static dev_info_t *fipe_drv_dip;
/*
* PCI device ID for supported hardware.
* For memory controller devices in Intel 5000/7300 series chipset, PCI vendor
* id and PCI device id is read only, PCI subvendor id and PCI subsystem id is
* write-once. So we could only rely on PCI vendor id and PCI device id here.
* For all PCI functions (0,1,2,3) in device 0x10 on bus 0, they will have the
* same PCI (vendor_id, device_id, subvendor_id, subsystem_id, class_id).
* We only need to access PCI device (0, 0x10, 1), all other PCI functions will
* be filtered out by unit address.
*/
static struct fipe_pci_id {
uint16_t venid;
uint16_t devid;
uint16_t subvenid;
uint16_t subsysid;
char *unitaddr;
} fipe_mc_pciids[] = {
{ 0x8086, 0x25f0, 0xffff, 0xffff, "10,1" }, /* Intel 5000P/V/X/Z */
{ 0x8086, 0x360c, 0xffff, 0xffff, "10,1" } /* Intel 7300 NB */
};
/*ARGSUSED*/
static int
fipe_open(dev_t *devp, int flag, int otyp, cred_t *credp)
{
if (otyp != OTYP_CHR) {
cmn_err(CE_NOTE, "!fipe: invalid otyp %d in open.", otyp);
return (EINVAL);
}
return (0);
}
/*ARGSUSED*/
static int
fipe_close(dev_t dev, int flag, int otyp, cred_t *credp)
{
return (0);
}
/*ARGSUSED*/
static int
fipe_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
int *rvalp)
{
int rc = 0;
fipe_pm_policy_t policy;
/* First check permission. */
if (secpolicy_power_mgmt(credp) != 0) {
return (EPERM);
}
switch (cmd) {
case FIPE_IOCTL_START:
if ((mode & FWRITE) == 0) {
rc = EBADF;
} else {
mutex_enter(&fipe_drv_lock);
rc = fipe_start();
mutex_exit(&fipe_drv_lock);
rc = (rc == 0) ? 0 : ENXIO;
}
break;
case FIPE_IOCTL_STOP:
if ((mode & FWRITE) == 0) {
rc = EBADF;
} else {
mutex_enter(&fipe_drv_lock);
rc = fipe_stop();
mutex_exit(&fipe_drv_lock);
rc = (rc == 0) ? 0 : ENXIO;
}
break;
case FIPE_IOCTL_GET_PMPOLICY:
if ((mode & FREAD) == 0) {
rc = EBADF;
} else {
mutex_enter(&fipe_drv_lock);
policy = fipe_get_pmpolicy();
mutex_exit(&fipe_drv_lock);
rc = ddi_copyout(&policy, (void *)arg,
sizeof (policy), mode);
rc = (rc >= 0) ? 0 : EFAULT;
}
break;
case FIPE_IOCTL_SET_PMPOLICY:
if ((mode & FWRITE) == 0) {
rc = EBADF;
} else {
mutex_enter(&fipe_drv_lock);
rc = fipe_set_pmpolicy((fipe_pm_policy_t)arg);
mutex_exit(&fipe_drv_lock);
rc = (rc == 0) ? 0 : ENXIO;
}
break;
default:
cmn_err(CE_NOTE, "!fipe: unknown ioctl command %d.", cmd);
rc = ENOTSUP;
break;
}
return (rc);
}
/*ARGSUSED*/
static int
fipe_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
if (fipe_drv_dip != NULL) {
*result = fipe_drv_dip;
return (DDI_SUCCESS);
} else {
*result = NULL;
return (DDI_FAILURE);
}
case DDI_INFO_DEVT2INSTANCE:
if (fipe_drv_dip != NULL) {
*result = (void *)(uintptr_t)
ddi_get_instance(fipe_drv_dip);
return (DDI_SUCCESS);
} else {
*result = NULL;
return (DDI_FAILURE);
}
default:
*result = NULL;
return (DDI_FAILURE);
}
}
/* Validate whether it's supported hardware. */
static int
fipe_validate_dip(dev_info_t *dip)
{
int i, rc = -1;
char *unitaddr;
struct fipe_pci_id *ip;
ddi_acc_handle_t handle;
uint16_t venid, devid, subvenid, subsysid;
/* Get device unit address, it's "devid,funcid" in hexadecimal. */
if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
"unit-address", &unitaddr) != DDI_PROP_SUCCESS) {
cmn_err(CE_CONT, "?fipe: failed to get deivce unit address.");
return (-1);
}
if (pci_config_setup(dip, &handle) != DDI_SUCCESS) {
cmn_err(CE_CONT, "?fipe: failed to setup pcicfg handler.");
ddi_prop_free(unitaddr);
return (-1);
}
venid = pci_config_get16(handle, PCI_CONF_VENID);
devid = pci_config_get16(handle, PCI_CONF_DEVID);
subvenid = pci_config_get16(handle, PCI_CONF_SUBVENID);
subsysid = pci_config_get16(handle, PCI_CONF_SUBSYSID);
/* Validate device. */
for (rc = -1, i = 0, ip = &fipe_mc_pciids[0];
i < sizeof (fipe_mc_pciids) / sizeof (fipe_mc_pciids[0]);
i++, ip++) {
if ((ip->venid == 0xffffu || ip->venid == venid) &&
(ip->devid == 0xffffu || ip->devid == devid) &&
(ip->subvenid == 0xffffu || ip->subvenid == subvenid) &&
(ip->subsysid == 0xffffu || ip->subsysid == subsysid) &&
(ip->unitaddr == NULL ||
strcmp(ip->unitaddr, unitaddr) == 0)) {
rc = 0;
break;
}
}
pci_config_teardown(&handle);
ddi_prop_free(unitaddr);
return (rc);
}
static int
fipe_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
char *ptr;
int ignore = 0, rc = DDI_FAILURE;
mutex_enter(&fipe_drv_lock);
switch (cmd) {
case DDI_ATTACH:
/* Check whether it has been disabled by user. */
if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
"disable_fipe_pm", &ptr) == DDI_SUCCESS) {
if (strcasecmp(ptr, "true") == 0 ||
strcasecmp(ptr, "yes") == 0) {
fipe_allow_attach = 0;
}
ddi_prop_free(ptr);
}
if (fipe_allow_attach == 0) {
cmn_err(CE_WARN,
"fipe: driver has been disabled by user.");
ignore = 1;
break;
}
/* Filter out unwanted PCI functions. */
if ((ignore = fipe_validate_dip(dip)) != 0) {
break;
/* There should be only one MC device in system. */
} else if (fipe_drv_dip != NULL) {
cmn_err(CE_NOTE,
"!fipe: more than one hardware instances found.");
break;
}
fipe_drv_dip = dip;
/* Initialize and start power management subsystem. */
if (fipe_init(fipe_drv_dip) != 0) {
fipe_drv_dip = NULL;
break;
} else if (fipe_start() != 0) {
(void) fipe_fini();
fipe_drv_dip = NULL;
break;
}
/* Ignore error from creating minor node. */
if (ddi_create_minor_node(dip, "fipe", S_IFCHR, 0,
"ddi_mem_pm", 0) != DDI_SUCCESS) {
cmn_err(CE_CONT,
"?fipe: failed to create device minor node.\n");
}
rc = DDI_SUCCESS;
break;
case DDI_RESUME:
if (fipe_resume() == 0) {
rc = DDI_SUCCESS;
}
break;
default:
break;
}
mutex_exit(&fipe_drv_lock);
if (ignore == 0 && rc != DDI_SUCCESS) {
cmn_err(CE_NOTE, "!fipe: failed to attach or resume device.");
}
return (rc);
}
/*ARGSUSED*/
static int
fipe_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
int rc = DDI_FAILURE;
mutex_enter(&fipe_drv_lock);
switch (cmd) {
case DDI_DETACH:
if (fipe_allow_detach == 0 || dip != fipe_drv_dip) {
break;
}
if (fipe_stop() != 0) {
break;
} else if (fipe_fini() != 0) {
(void) fipe_start();
break;
}
ddi_remove_minor_node(dip, NULL);
fipe_drv_dip = NULL;
rc = DDI_SUCCESS;
break;
case DDI_SUSPEND:
if (fipe_suspend() == 0) {
rc = DDI_SUCCESS;
}
break;
default:
break;
}
mutex_exit(&fipe_drv_lock);
if (rc != DDI_SUCCESS) {
cmn_err(CE_NOTE, "!fipe: failed to detach or suspend device.");
}
return (rc);
}
static int
fipe_quiesce(dev_info_t *dip)
{
if (dip != fipe_drv_dip) {
return (DDI_SUCCESS);
}
/* Quiesce hardware by stopping power management subsystem. */
if (fipe_suspend() != 0) {
cmn_err(CE_NOTE, "!fipe: failed to quiesce device.");
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
static struct cb_ops fipe_cb_ops = {
fipe_open,
fipe_close,
nodev, /* not a block driver */
nodev, /* no print routine */
nodev, /* no dump routine */
nodev, /* no read routine */
nodev, /* no write routine */
fipe_ioctl,
nodev, /* no devmap routine */
nodev, /* no mmap routine */
nodev, /* no segmap routine */
nochpoll, /* no chpoll routine */
ddi_prop_op,
0, /* not a STREAMS driver */
D_NEW | D_MP, /* safe for multi-thread/multi-processor */
};
static struct dev_ops fipe_ops = {
DEVO_REV, /* devo_rev */
0, /* devo_refcnt */
fipe_getinfo, /* devo_getinfo */
nulldev, /* devo_identify */
nulldev, /* devo_probe */
fipe_attach, /* devo_attach */
fipe_detach, /* devo_detach */
nodev, /* devo_reset */
&fipe_cb_ops, /* devo_cb_ops */
NULL, /* devo_bus_ops */
NULL, /* devo_power */
&fipe_quiesce, /* devo_quiesce */
};
static struct modldrv modldrv = {
&mod_driverops,
"Intel 5000/7300 memory controller driver",
&fipe_ops
};
static struct modlinkage modlinkage = {
MODREV_1,
(void *)&modldrv,
NULL
};
int
_init(void)
{
fipe_drv_dip = NULL;
mutex_init(&fipe_drv_lock, NULL, MUTEX_DRIVER, NULL);
return (mod_install(&modlinkage));
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
int
_fini(void)
{
int err;
if ((err = mod_remove(&modlinkage)) == 0) {
mutex_destroy(&fipe_drv_lock);
fipe_drv_dip = NULL;
}
return (err);
}
|