1 /*
2  * Standard Hot Plug Controller Driver
3  *
4  * Copyright (C) 1995,2001 Compaq Computer Corporation
5  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6  * Copyright (C) 2001 IBM Corp.
7  * Copyright (C) 2003-2004 Intel Corporation
8  *
9  * All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or (at
14  * your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19  * NON INFRINGEMENT.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
27  *
28  */
29 
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/pci.h>
35 #include "shpchp.h"
36 
37 /* Global variables */
38 int shpchp_debug;
39 int shpchp_poll_mode;
40 int shpchp_poll_time;
41 struct controller *shpchp_ctrl_list;	/* = NULL */
42 
43 #define DRIVER_VERSION	"0.4"
44 #define DRIVER_AUTHOR	"Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
45 #define DRIVER_DESC	"Standard Hot Plug PCI Controller Driver"
46 
47 MODULE_AUTHOR(DRIVER_AUTHOR);
48 MODULE_DESCRIPTION(DRIVER_DESC);
49 MODULE_LICENSE("GPL");
50 
51 module_param(shpchp_debug, bool, 0644);
52 module_param(shpchp_poll_mode, bool, 0644);
53 module_param(shpchp_poll_time, int, 0644);
54 MODULE_PARM_DESC(shpchp_debug, "Debugging mode enabled or not");
55 MODULE_PARM_DESC(shpchp_poll_mode, "Using polling mechanism for hot-plug events or not");
56 MODULE_PARM_DESC(shpchp_poll_time, "Polling mechanism frequency, in seconds");
57 
58 #define SHPC_MODULE_NAME "shpchp"
59 
60 static int shpc_start_thread (void);
61 static int set_attention_status (struct hotplug_slot *slot, u8 value);
62 static int enable_slot		(struct hotplug_slot *slot);
63 static int disable_slot		(struct hotplug_slot *slot);
64 static int get_power_status	(struct hotplug_slot *slot, u8 *value);
65 static int get_attention_status	(struct hotplug_slot *slot, u8 *value);
66 static int get_latch_status	(struct hotplug_slot *slot, u8 *value);
67 static int get_adapter_status	(struct hotplug_slot *slot, u8 *value);
68 static int get_address		(struct hotplug_slot *slot, u32 *value);
69 static int get_max_bus_speed	(struct hotplug_slot *slot, enum pci_bus_speed *value);
70 static int get_cur_bus_speed	(struct hotplug_slot *slot, enum pci_bus_speed *value);
71 
72 static struct hotplug_slot_ops shpchp_hotplug_slot_ops = {
73 	.owner =		THIS_MODULE,
74 	.set_attention_status =	set_attention_status,
75 	.enable_slot =		enable_slot,
76 	.disable_slot =		disable_slot,
77 	.get_power_status =	get_power_status,
78 	.get_attention_status =	get_attention_status,
79 	.get_latch_status =	get_latch_status,
80 	.get_adapter_status =	get_adapter_status,
81 	.get_address =		get_address,
82 	.get_max_bus_speed =	get_max_bus_speed,
83 	.get_cur_bus_speed =	get_cur_bus_speed,
84 };
85 
86 /**
87  * release_slot - free up the memory used by a slot
88  * @hotplug_slot: slot to free
89  */
90 static void release_slot(struct hotplug_slot *hotplug_slot)
91 {
92 	struct slot *slot = hotplug_slot->private;
93 
94 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
95 
96 	kfree(slot->hotplug_slot->info);
97 	kfree(slot->hotplug_slot->name);
98 	kfree(slot->hotplug_slot);
99 	kfree(slot);
100 }
101 
102 static int init_slots(struct controller *ctrl)
103 {
104 	struct slot *new_slot;
105 	u8 number_of_slots;
106 	u8 slot_device;
107 	u32 slot_number, sun;
108 	int result = -ENOMEM;
109 
110 	number_of_slots = ctrl->num_slots;
111 	slot_device = ctrl->slot_device_offset;
112 	slot_number = ctrl->first_slot;
113 
114 	while (number_of_slots) {
115 		new_slot = (struct slot *) kmalloc(sizeof(struct slot), GFP_KERNEL);
116 		if (!new_slot)
117 			goto error;
118 
119 		memset(new_slot, 0, sizeof(struct slot));
120 		new_slot->hotplug_slot = kmalloc (sizeof (struct hotplug_slot), GFP_KERNEL);
121 		if (!new_slot->hotplug_slot)
122 			goto error_slot;
123 		memset(new_slot->hotplug_slot, 0, sizeof (struct hotplug_slot));
124 
125 		new_slot->hotplug_slot->info = kmalloc (sizeof (struct hotplug_slot_info), GFP_KERNEL);
126 		if (!new_slot->hotplug_slot->info)
127 			goto error_hpslot;
128 		memset(new_slot->hotplug_slot->info, 0, sizeof (struct hotplug_slot_info));
129 		new_slot->hotplug_slot->name = kmalloc (SLOT_NAME_SIZE, GFP_KERNEL);
130 		if (!new_slot->hotplug_slot->name)
131 			goto error_info;
132 
133 		new_slot->magic = SLOT_MAGIC;
134 		new_slot->ctrl = ctrl;
135 		new_slot->bus = ctrl->slot_bus;
136 		new_slot->device = slot_device;
137 		new_slot->hpc_ops = ctrl->hpc_ops;
138 
139 		if (shpchprm_get_physical_slot_number(ctrl, &sun,
140 					new_slot->bus, new_slot->device))
141 			goto error_name;
142 
143 		new_slot->number = sun;
144 		new_slot->hp_slot = slot_device - ctrl->slot_device_offset;
145 
146 		/* register this slot with the hotplug pci core */
147 		new_slot->hotplug_slot->private = new_slot;
148 		new_slot->hotplug_slot->release = &release_slot;
149 		make_slot_name(new_slot->hotplug_slot->name, SLOT_NAME_SIZE, new_slot);
150 		new_slot->hotplug_slot->ops = &shpchp_hotplug_slot_ops;
151 
152 		new_slot->hpc_ops->get_power_status(new_slot, &(new_slot->hotplug_slot->info->power_status));
153 		new_slot->hpc_ops->get_attention_status(new_slot, &(new_slot->hotplug_slot->info->attention_status));
154 		new_slot->hpc_ops->get_latch_status(new_slot, &(new_slot->hotplug_slot->info->latch_status));
155 		new_slot->hpc_ops->get_adapter_status(new_slot, &(new_slot->hotplug_slot->info->adapter_status));
156 
157 		dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x slot_device_offset=%x\n", new_slot->bus,
158 			new_slot->device, new_slot->hp_slot, new_slot->number, ctrl->slot_device_offset);
159 		result = pci_hp_register (new_slot->hotplug_slot);
160 		if (result) {
161 			err ("pci_hp_register failed with error %d\n", result);
162 			goto error_name;
163 		}
164 
165 		new_slot->next = ctrl->slot;
166 		ctrl->slot = new_slot;
167 
168 		number_of_slots--;
169 		slot_device++;
170 		slot_number += ctrl->slot_num_inc;
171 	}
172 
173 	return 0;
174 
175 error_name:
176 	kfree(new_slot->hotplug_slot->name);
177 error_info:
178 	kfree(new_slot->hotplug_slot->info);
179 error_hpslot:
180 	kfree(new_slot->hotplug_slot);
181 error_slot:
182 	kfree(new_slot);
183 error:
184 	return result;
185 }
186 
187 static void cleanup_slots(struct controller *ctrl)
188 {
189 	struct slot *old_slot, *next_slot;
190 
191 	old_slot = ctrl->slot;
192 	ctrl->slot = NULL;
193 
194 	while (old_slot) {
195 		next_slot = old_slot->next;
196 		pci_hp_deregister(old_slot->hotplug_slot);
197 		old_slot = next_slot;
198 	}
199 }
200 
201 static int get_ctlr_slot_config(struct controller *ctrl)
202 {
203 	int num_ctlr_slots;
204 	int first_device_num;
205 	int physical_slot_num;
206 	int updown;
207 	int rc;
208 	int flags;
209 
210 	rc = shpc_get_ctlr_slot_config(ctrl, &num_ctlr_slots, &first_device_num, &physical_slot_num, &updown, &flags);
211 	if (rc) {
212 		err("%s: get_ctlr_slot_config fail for b:d (%x:%x)\n", __FUNCTION__, ctrl->bus, ctrl->device);
213 		return -1;
214 	}
215 
216 	ctrl->num_slots = num_ctlr_slots;
217 	ctrl->slot_device_offset = first_device_num;
218 	ctrl->first_slot = physical_slot_num;
219 	ctrl->slot_num_inc = updown;		/* either -1 or 1 */
220 
221 	dbg("%s: num_slot(0x%x) 1st_dev(0x%x) psn(0x%x) updown(%d) for b:d (%x:%x)\n",
222 		__FUNCTION__, num_ctlr_slots, first_device_num, physical_slot_num, updown, ctrl->bus, ctrl->device);
223 
224 	return 0;
225 }
226 
227 
228 /*
229  * set_attention_status - Turns the Amber LED for a slot on, off or blink
230  */
231 static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
232 {
233 	struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
234 
235 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
236 
237 	hotplug_slot->info->attention_status = status;
238 	slot->hpc_ops->set_attention_status(slot, status);
239 
240 	return 0;
241 }
242 
243 
244 static int enable_slot (struct hotplug_slot *hotplug_slot)
245 {
246 	struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
247 
248 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
249 
250 	return shpchp_enable_slot(slot);
251 }
252 
253 
254 static int disable_slot (struct hotplug_slot *hotplug_slot)
255 {
256 	struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
257 
258 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
259 
260 	return shpchp_disable_slot(slot);
261 }
262 
263 static int get_power_status (struct hotplug_slot *hotplug_slot, u8 *value)
264 {
265 	struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
266 	int retval;
267 
268 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
269 
270 	retval = slot->hpc_ops->get_power_status(slot, value);
271 	if (retval < 0)
272 		*value = hotplug_slot->info->power_status;
273 
274 	return 0;
275 }
276 
277 static int get_attention_status (struct hotplug_slot *hotplug_slot, u8 *value)
278 {
279 	struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
280 	int retval;
281 
282 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
283 
284 	retval = slot->hpc_ops->get_attention_status(slot, value);
285 	if (retval < 0)
286 		*value = hotplug_slot->info->attention_status;
287 
288 	return 0;
289 }
290 
291 static int get_latch_status (struct hotplug_slot *hotplug_slot, u8 *value)
292 {
293 	struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
294 	int retval;
295 
296 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
297 
298 	retval = slot->hpc_ops->get_latch_status(slot, value);
299 	if (retval < 0)
300 		*value = hotplug_slot->info->latch_status;
301 
302 	return 0;
303 }
304 
305 static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
306 {
307 	struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
308 	int retval;
309 
310 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
311 
312 	retval = slot->hpc_ops->get_adapter_status(slot, value);
313 	if (retval < 0)
314 		*value = hotplug_slot->info->adapter_status;
315 
316 	return 0;
317 }
318 
319 static int get_address (struct hotplug_slot *hotplug_slot, u32 *value)
320 {
321 	struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
322 	struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
323 
324 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
325 
326 	*value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
327 
328 	return 0;
329 }
330 
331 static int get_max_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
332 {
333 	struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
334 	int retval;
335 
336 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
337 
338 	retval = slot->hpc_ops->get_max_bus_speed(slot, value);
339 	if (retval < 0)
340 		*value = PCI_SPEED_UNKNOWN;
341 
342 	return 0;
343 }
344 
345 static int get_cur_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
346 {
347 	struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
348 	int retval;
349 
350 	dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
351 
352 	retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
353 	if (retval < 0)
354 		*value = PCI_SPEED_UNKNOWN;
355 
356 	return 0;
357 }
358 
359 static int is_shpc_capable(struct pci_dev *dev)
360 {
361        if ((dev->vendor == PCI_VENDOR_ID_AMD) || (dev->device ==
362                                PCI_DEVICE_ID_AMD_GOLAM_7450))
363                return 1;
364        if (pci_find_capability(dev, PCI_CAP_ID_SHPC))
365                return 1;
366 
367        return 0;
368 }
369 
370 static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
371 {
372 	int rc;
373 	struct controller *ctrl;
374 	struct slot *t_slot;
375 	int first_device_num;	/* first PCI device number supported by this SHPC */
376 	int num_ctlr_slots;	/* number of slots supported by this SHPC */
377 
378 	if (!is_shpc_capable(pdev))
379 		return -ENODEV;
380 
381 	ctrl = (struct controller *) kmalloc(sizeof(struct controller), GFP_KERNEL);
382 	if (!ctrl) {
383 		err("%s : out of memory\n", __FUNCTION__);
384 		goto err_out_none;
385 	}
386 	memset(ctrl, 0, sizeof(struct controller));
387 
388 	rc = shpc_init(ctrl, pdev);
389 	if (rc) {
390 		dbg("%s: controller initialization failed\n", SHPC_MODULE_NAME);
391 		goto err_out_free_ctrl;
392 	}
393 
394 	pci_set_drvdata(pdev, ctrl);
395 
396 	ctrl->pci_bus = kmalloc (sizeof (*ctrl->pci_bus), GFP_KERNEL);
397 	if (!ctrl->pci_bus) {
398 		err("out of memory\n");
399 		rc = -ENOMEM;
400 		goto err_out_unmap_mmio_region;
401 	}
402 
403 	memcpy (ctrl->pci_bus, pdev->bus, sizeof (*ctrl->pci_bus));
404 	ctrl->bus = pdev->bus->number;
405 	ctrl->slot_bus = pdev->subordinate->number;
406 
407 	ctrl->device = PCI_SLOT(pdev->devfn);
408 	ctrl->function = PCI_FUNC(pdev->devfn);
409 	dbg("ctrl bus=0x%x, device=%x, function=%x, irq=%x\n", ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
410 
411 	/*
412 	 *	Save configuration headers for this and subordinate PCI buses
413 	 */
414 
415 	rc = get_ctlr_slot_config(ctrl);
416 	if (rc) {
417 		err(msg_initialization_err, rc);
418 		goto err_out_free_ctrl_bus;
419 	}
420 	first_device_num = ctrl->slot_device_offset;
421 	num_ctlr_slots = ctrl->num_slots;
422 
423 	ctrl->add_support = 1;
424 
425 	/* Setup the slot information structures */
426 	rc = init_slots(ctrl);
427 	if (rc) {
428 		err(msg_initialization_err, 6);
429 		goto err_out_free_ctrl_slot;
430 	}
431 
432 	/* Now hpc_functions (slot->hpc_ops->functions) are ready  */
433 	t_slot = shpchp_find_slot(ctrl, first_device_num);
434 
435 	/* Check for operation bus speed */
436 	rc = t_slot->hpc_ops->get_cur_bus_speed(t_slot, &ctrl->speed);
437 	dbg("%s: t_slot->hp_slot %x\n", __FUNCTION__,t_slot->hp_slot);
438 
439 	if (rc || ctrl->speed == PCI_SPEED_UNKNOWN) {
440 		err(SHPC_MODULE_NAME ": Can't get current bus speed. Set to 33MHz PCI.\n");
441 		ctrl->speed = PCI_SPEED_33MHz;
442 	}
443 
444 	/* Finish setting up the hot plug ctrl device */
445 	ctrl->next_event = 0;
446 
447 	if (!shpchp_ctrl_list) {
448 		shpchp_ctrl_list = ctrl;
449 		ctrl->next = NULL;
450 	} else {
451 		ctrl->next = shpchp_ctrl_list;
452 		shpchp_ctrl_list = ctrl;
453 	}
454 
455 	shpchp_create_ctrl_files(ctrl);
456 
457 	return 0;
458 
459 err_out_free_ctrl_slot:
460 	cleanup_slots(ctrl);
461 err_out_free_ctrl_bus:
462 	kfree(ctrl->pci_bus);
463 err_out_unmap_mmio_region:
464 	ctrl->hpc_ops->release_ctlr(ctrl);
465 err_out_free_ctrl:
466 	kfree(ctrl);
467 err_out_none:
468 	return -ENODEV;
469 }
470 
471 
472 static int shpc_start_thread(void)
473 {
474 	int retval = 0;
475 
476 	dbg("Initialize + Start the notification/polling mechanism \n");
477 
478 	retval = shpchp_event_start_thread();
479 	if (retval) {
480 		dbg("shpchp_event_start_thread() failed\n");
481 		return retval;
482 	}
483 
484 	return retval;
485 }
486 
487 static void __exit unload_shpchpd(void)
488 {
489 	struct controller *ctrl;
490 	struct controller *tctrl;
491 
492 	ctrl = shpchp_ctrl_list;
493 
494 	while (ctrl) {
495 		shpchp_remove_ctrl_files(ctrl);
496 		cleanup_slots(ctrl);
497 
498 		kfree (ctrl->pci_bus);
499 		ctrl->hpc_ops->release_ctlr(ctrl);
500 
501 		tctrl = ctrl;
502 		ctrl = ctrl->next;
503 
504 		kfree(tctrl);
505 	}
506 
507 	/* Stop the notification mechanism */
508 	shpchp_event_stop_thread();
509 
510 }
511 
512 
513 static struct pci_device_id shpcd_pci_tbl[] = {
514 	{
515 	.class =        ((PCI_CLASS_BRIDGE_PCI << 8) | 0x00),
516 	.class_mask =	~0,
517 	.vendor =       PCI_ANY_ID,
518 	.device =       PCI_ANY_ID,
519 	.subvendor =    PCI_ANY_ID,
520 	.subdevice =    PCI_ANY_ID,
521 	},
522 
523 	{ /* end: all zeroes */ }
524 };
525 
526 MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl);
527 
528 
529 
530 static struct pci_driver shpc_driver = {
531 	.name =		SHPC_MODULE_NAME,
532 	.id_table =	shpcd_pci_tbl,
533 	.probe =	shpc_probe,
534 	/* remove:	shpc_remove_one, */
535 };
536 
537 
538 
539 static int __init shpcd_init(void)
540 {
541 	int retval = 0;
542 
543 #ifdef CONFIG_HOTPLUG_PCI_SHPC_POLL_EVENT_MODE
544 	shpchp_poll_mode = 1;
545 #endif
546 
547 	retval = shpc_start_thread();
548 	if (retval)
549 		goto error_hpc_init;
550 
551 	retval = pci_register_driver(&shpc_driver);
552 	dbg("%s: pci_register_driver = %d\n", __FUNCTION__, retval);
553 	info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
554 
555 error_hpc_init:
556 	if (retval) {
557 		shpchp_event_stop_thread();
558 	}
559 	return retval;
560 }
561 
562 static void __exit shpcd_cleanup(void)
563 {
564 	dbg("unload_shpchpd()\n");
565 	unload_shpchpd();
566 
567 	pci_unregister_driver(&shpc_driver);
568 
569 	info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
570 }
571 
572 module_init(shpcd_init);
573 module_exit(shpcd_cleanup);
574