1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Standard Hot Plug Controller Driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1995,2001 Compaq Computer Corporation
6*4882a593Smuzhiyun * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7*4882a593Smuzhiyun * Copyright (C) 2001 IBM Corp.
8*4882a593Smuzhiyun * Copyright (C) 2003-2004 Intel Corporation
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * All rights reserved.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun */
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <linux/module.h>
17*4882a593Smuzhiyun #include <linux/kernel.h>
18*4882a593Smuzhiyun #include <linux/types.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/pci.h>
21*4882a593Smuzhiyun #include "../pci.h"
22*4882a593Smuzhiyun #include "shpchp.h"
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun static void interrupt_event_handler(struct work_struct *work);
25*4882a593Smuzhiyun static int shpchp_enable_slot(struct slot *p_slot);
26*4882a593Smuzhiyun static int shpchp_disable_slot(struct slot *p_slot);
27*4882a593Smuzhiyun
queue_interrupt_event(struct slot * p_slot,u32 event_type)28*4882a593Smuzhiyun static int queue_interrupt_event(struct slot *p_slot, u32 event_type)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun struct event_info *info;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun info = kmalloc(sizeof(*info), GFP_ATOMIC);
33*4882a593Smuzhiyun if (!info)
34*4882a593Smuzhiyun return -ENOMEM;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun info->event_type = event_type;
37*4882a593Smuzhiyun info->p_slot = p_slot;
38*4882a593Smuzhiyun INIT_WORK(&info->work, interrupt_event_handler);
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun queue_work(p_slot->wq, &info->work);
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun return 0;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
shpchp_handle_attention_button(u8 hp_slot,struct controller * ctrl)45*4882a593Smuzhiyun u8 shpchp_handle_attention_button(u8 hp_slot, struct controller *ctrl)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun struct slot *p_slot;
48*4882a593Smuzhiyun u32 event_type;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* Attention Button Change */
51*4882a593Smuzhiyun ctrl_dbg(ctrl, "Attention button interrupt received\n");
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
54*4882a593Smuzhiyun p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun * Button pressed - See if need to TAKE ACTION!!!
58*4882a593Smuzhiyun */
59*4882a593Smuzhiyun ctrl_info(ctrl, "Button pressed on Slot(%s)\n", slot_name(p_slot));
60*4882a593Smuzhiyun event_type = INT_BUTTON_PRESS;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun queue_interrupt_event(p_slot, event_type);
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun return 0;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
shpchp_handle_switch_change(u8 hp_slot,struct controller * ctrl)68*4882a593Smuzhiyun u8 shpchp_handle_switch_change(u8 hp_slot, struct controller *ctrl)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun struct slot *p_slot;
71*4882a593Smuzhiyun u8 getstatus;
72*4882a593Smuzhiyun u32 event_type;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* Switch Change */
75*4882a593Smuzhiyun ctrl_dbg(ctrl, "Switch interrupt received\n");
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
78*4882a593Smuzhiyun p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
79*4882a593Smuzhiyun p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
80*4882a593Smuzhiyun ctrl_dbg(ctrl, "Card present %x Power status %x\n",
81*4882a593Smuzhiyun p_slot->presence_save, p_slot->pwr_save);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun if (getstatus) {
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun * Switch opened
86*4882a593Smuzhiyun */
87*4882a593Smuzhiyun ctrl_info(ctrl, "Latch open on Slot(%s)\n", slot_name(p_slot));
88*4882a593Smuzhiyun event_type = INT_SWITCH_OPEN;
89*4882a593Smuzhiyun if (p_slot->pwr_save && p_slot->presence_save) {
90*4882a593Smuzhiyun event_type = INT_POWER_FAULT;
91*4882a593Smuzhiyun ctrl_err(ctrl, "Surprise Removal of card\n");
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun } else {
94*4882a593Smuzhiyun /*
95*4882a593Smuzhiyun * Switch closed
96*4882a593Smuzhiyun */
97*4882a593Smuzhiyun ctrl_info(ctrl, "Latch close on Slot(%s)\n", slot_name(p_slot));
98*4882a593Smuzhiyun event_type = INT_SWITCH_CLOSE;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun queue_interrupt_event(p_slot, event_type);
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun return 1;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
shpchp_handle_presence_change(u8 hp_slot,struct controller * ctrl)106*4882a593Smuzhiyun u8 shpchp_handle_presence_change(u8 hp_slot, struct controller *ctrl)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun struct slot *p_slot;
109*4882a593Smuzhiyun u32 event_type;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /* Presence Change */
112*4882a593Smuzhiyun ctrl_dbg(ctrl, "Presence/Notify input change\n");
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /*
117*4882a593Smuzhiyun * Save the presence state
118*4882a593Smuzhiyun */
119*4882a593Smuzhiyun p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
120*4882a593Smuzhiyun if (p_slot->presence_save) {
121*4882a593Smuzhiyun /*
122*4882a593Smuzhiyun * Card Present
123*4882a593Smuzhiyun */
124*4882a593Smuzhiyun ctrl_info(ctrl, "Card present on Slot(%s)\n",
125*4882a593Smuzhiyun slot_name(p_slot));
126*4882a593Smuzhiyun event_type = INT_PRESENCE_ON;
127*4882a593Smuzhiyun } else {
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun * Not Present
130*4882a593Smuzhiyun */
131*4882a593Smuzhiyun ctrl_info(ctrl, "Card not present on Slot(%s)\n",
132*4882a593Smuzhiyun slot_name(p_slot));
133*4882a593Smuzhiyun event_type = INT_PRESENCE_OFF;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun queue_interrupt_event(p_slot, event_type);
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun return 1;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
shpchp_handle_power_fault(u8 hp_slot,struct controller * ctrl)141*4882a593Smuzhiyun u8 shpchp_handle_power_fault(u8 hp_slot, struct controller *ctrl)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun struct slot *p_slot;
144*4882a593Smuzhiyun u32 event_type;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /* Power fault */
147*4882a593Smuzhiyun ctrl_dbg(ctrl, "Power fault interrupt received\n");
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun if (!(p_slot->hpc_ops->query_power_fault(p_slot))) {
152*4882a593Smuzhiyun /*
153*4882a593Smuzhiyun * Power fault Cleared
154*4882a593Smuzhiyun */
155*4882a593Smuzhiyun ctrl_info(ctrl, "Power fault cleared on Slot(%s)\n",
156*4882a593Smuzhiyun slot_name(p_slot));
157*4882a593Smuzhiyun p_slot->status = 0x00;
158*4882a593Smuzhiyun event_type = INT_POWER_FAULT_CLEAR;
159*4882a593Smuzhiyun } else {
160*4882a593Smuzhiyun /*
161*4882a593Smuzhiyun * Power fault
162*4882a593Smuzhiyun */
163*4882a593Smuzhiyun ctrl_info(ctrl, "Power fault on Slot(%s)\n", slot_name(p_slot));
164*4882a593Smuzhiyun event_type = INT_POWER_FAULT;
165*4882a593Smuzhiyun /* set power fault status for this board */
166*4882a593Smuzhiyun p_slot->status = 0xFF;
167*4882a593Smuzhiyun ctrl_info(ctrl, "Power fault bit %x set\n", hp_slot);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun queue_interrupt_event(p_slot, event_type);
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun return 1;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /* The following routines constitute the bulk of the
176*4882a593Smuzhiyun hotplug controller logic
177*4882a593Smuzhiyun */
change_bus_speed(struct controller * ctrl,struct slot * p_slot,enum pci_bus_speed speed)178*4882a593Smuzhiyun static int change_bus_speed(struct controller *ctrl, struct slot *p_slot,
179*4882a593Smuzhiyun enum pci_bus_speed speed)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun int rc = 0;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun ctrl_dbg(ctrl, "Change speed to %d\n", speed);
184*4882a593Smuzhiyun rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, speed);
185*4882a593Smuzhiyun if (rc) {
186*4882a593Smuzhiyun ctrl_err(ctrl, "%s: Issue of set bus speed mode command failed\n",
187*4882a593Smuzhiyun __func__);
188*4882a593Smuzhiyun return WRONG_BUS_FREQUENCY;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun return rc;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
fix_bus_speed(struct controller * ctrl,struct slot * pslot,u8 flag,enum pci_bus_speed asp,enum pci_bus_speed bsp,enum pci_bus_speed msp)193*4882a593Smuzhiyun static int fix_bus_speed(struct controller *ctrl, struct slot *pslot,
194*4882a593Smuzhiyun u8 flag, enum pci_bus_speed asp, enum pci_bus_speed bsp,
195*4882a593Smuzhiyun enum pci_bus_speed msp)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun int rc = 0;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun /*
200*4882a593Smuzhiyun * If other slots on the same bus are occupied, we cannot
201*4882a593Smuzhiyun * change the bus speed.
202*4882a593Smuzhiyun */
203*4882a593Smuzhiyun if (flag) {
204*4882a593Smuzhiyun if (asp < bsp) {
205*4882a593Smuzhiyun ctrl_err(ctrl, "Speed of bus %x and adapter %x mismatch\n",
206*4882a593Smuzhiyun bsp, asp);
207*4882a593Smuzhiyun rc = WRONG_BUS_FREQUENCY;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun return rc;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun if (asp < msp) {
213*4882a593Smuzhiyun if (bsp != asp)
214*4882a593Smuzhiyun rc = change_bus_speed(ctrl, pslot, asp);
215*4882a593Smuzhiyun } else {
216*4882a593Smuzhiyun if (bsp != msp)
217*4882a593Smuzhiyun rc = change_bus_speed(ctrl, pslot, msp);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun return rc;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /**
223*4882a593Smuzhiyun * board_added - Called after a board has been added to the system.
224*4882a593Smuzhiyun * @p_slot: target &slot
225*4882a593Smuzhiyun *
226*4882a593Smuzhiyun * Turns power on for the board.
227*4882a593Smuzhiyun * Configures board.
228*4882a593Smuzhiyun */
board_added(struct slot * p_slot)229*4882a593Smuzhiyun static int board_added(struct slot *p_slot)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun u8 hp_slot;
232*4882a593Smuzhiyun u8 slots_not_empty = 0;
233*4882a593Smuzhiyun int rc = 0;
234*4882a593Smuzhiyun enum pci_bus_speed asp, bsp, msp;
235*4882a593Smuzhiyun struct controller *ctrl = p_slot->ctrl;
236*4882a593Smuzhiyun struct pci_bus *parent = ctrl->pci_dev->subordinate;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun hp_slot = p_slot->device - ctrl->slot_device_offset;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun ctrl_dbg(ctrl, "%s: p_slot->device, slot_offset, hp_slot = %d, %d ,%d\n",
241*4882a593Smuzhiyun __func__, p_slot->device, ctrl->slot_device_offset, hp_slot);
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /* Power on slot without connecting to bus */
244*4882a593Smuzhiyun rc = p_slot->hpc_ops->power_on_slot(p_slot);
245*4882a593Smuzhiyun if (rc) {
246*4882a593Smuzhiyun ctrl_err(ctrl, "Failed to power on slot\n");
247*4882a593Smuzhiyun return -1;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun if ((ctrl->pci_dev->vendor == 0x8086) && (ctrl->pci_dev->device == 0x0332)) {
251*4882a593Smuzhiyun rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, PCI_SPEED_33MHz);
252*4882a593Smuzhiyun if (rc) {
253*4882a593Smuzhiyun ctrl_err(ctrl, "%s: Issue of set bus speed mode command failed\n",
254*4882a593Smuzhiyun __func__);
255*4882a593Smuzhiyun return WRONG_BUS_FREQUENCY;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun /* turn on board, blink green LED, turn off Amber LED */
259*4882a593Smuzhiyun rc = p_slot->hpc_ops->slot_enable(p_slot);
260*4882a593Smuzhiyun if (rc) {
261*4882a593Smuzhiyun ctrl_err(ctrl, "Issue of Slot Enable command failed\n");
262*4882a593Smuzhiyun return rc;
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun rc = p_slot->hpc_ops->get_adapter_speed(p_slot, &asp);
267*4882a593Smuzhiyun if (rc) {
268*4882a593Smuzhiyun ctrl_err(ctrl, "Can't get adapter speed or bus mode mismatch\n");
269*4882a593Smuzhiyun return WRONG_BUS_FREQUENCY;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun bsp = ctrl->pci_dev->subordinate->cur_bus_speed;
273*4882a593Smuzhiyun msp = ctrl->pci_dev->subordinate->max_bus_speed;
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun /* Check if there are other slots or devices on the same bus */
276*4882a593Smuzhiyun if (!list_empty(&ctrl->pci_dev->subordinate->devices))
277*4882a593Smuzhiyun slots_not_empty = 1;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun ctrl_dbg(ctrl, "%s: slots_not_empty %d, adapter_speed %d, bus_speed %d, max_bus_speed %d\n",
280*4882a593Smuzhiyun __func__, slots_not_empty, asp,
281*4882a593Smuzhiyun bsp, msp);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, asp, bsp, msp);
284*4882a593Smuzhiyun if (rc)
285*4882a593Smuzhiyun return rc;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /* turn on board, blink green LED, turn off Amber LED */
288*4882a593Smuzhiyun rc = p_slot->hpc_ops->slot_enable(p_slot);
289*4882a593Smuzhiyun if (rc) {
290*4882a593Smuzhiyun ctrl_err(ctrl, "Issue of Slot Enable command failed\n");
291*4882a593Smuzhiyun return rc;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun /* Wait for ~1 second */
295*4882a593Smuzhiyun msleep(1000);
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun ctrl_dbg(ctrl, "%s: slot status = %x\n", __func__, p_slot->status);
298*4882a593Smuzhiyun /* Check for a power fault */
299*4882a593Smuzhiyun if (p_slot->status == 0xFF) {
300*4882a593Smuzhiyun /* power fault occurred, but it was benign */
301*4882a593Smuzhiyun ctrl_dbg(ctrl, "%s: Power fault\n", __func__);
302*4882a593Smuzhiyun p_slot->status = 0;
303*4882a593Smuzhiyun goto err_exit;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun if (shpchp_configure_device(p_slot)) {
307*4882a593Smuzhiyun ctrl_err(ctrl, "Cannot add device at %04x:%02x:%02x\n",
308*4882a593Smuzhiyun pci_domain_nr(parent), p_slot->bus, p_slot->device);
309*4882a593Smuzhiyun goto err_exit;
310*4882a593Smuzhiyun }
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun p_slot->status = 0;
313*4882a593Smuzhiyun p_slot->is_a_board = 0x01;
314*4882a593Smuzhiyun p_slot->pwr_save = 1;
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun p_slot->hpc_ops->green_led_on(p_slot);
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun return 0;
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun err_exit:
321*4882a593Smuzhiyun /* turn off slot, turn on Amber LED, turn off Green LED */
322*4882a593Smuzhiyun rc = p_slot->hpc_ops->slot_disable(p_slot);
323*4882a593Smuzhiyun if (rc) {
324*4882a593Smuzhiyun ctrl_err(ctrl, "%s: Issue of Slot Disable command failed\n",
325*4882a593Smuzhiyun __func__);
326*4882a593Smuzhiyun return rc;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun return(rc);
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun /**
334*4882a593Smuzhiyun * remove_board - Turns off slot and LEDs
335*4882a593Smuzhiyun * @p_slot: target &slot
336*4882a593Smuzhiyun */
remove_board(struct slot * p_slot)337*4882a593Smuzhiyun static int remove_board(struct slot *p_slot)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun struct controller *ctrl = p_slot->ctrl;
340*4882a593Smuzhiyun u8 hp_slot;
341*4882a593Smuzhiyun int rc;
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun shpchp_unconfigure_device(p_slot);
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun hp_slot = p_slot->device - ctrl->slot_device_offset;
346*4882a593Smuzhiyun p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun ctrl_dbg(ctrl, "%s: hp_slot = %d\n", __func__, hp_slot);
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun /* Change status to shutdown */
351*4882a593Smuzhiyun if (p_slot->is_a_board)
352*4882a593Smuzhiyun p_slot->status = 0x01;
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun /* turn off slot, turn on Amber LED, turn off Green LED */
355*4882a593Smuzhiyun rc = p_slot->hpc_ops->slot_disable(p_slot);
356*4882a593Smuzhiyun if (rc) {
357*4882a593Smuzhiyun ctrl_err(ctrl, "%s: Issue of Slot Disable command failed\n",
358*4882a593Smuzhiyun __func__);
359*4882a593Smuzhiyun return rc;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun rc = p_slot->hpc_ops->set_attention_status(p_slot, 0);
363*4882a593Smuzhiyun if (rc) {
364*4882a593Smuzhiyun ctrl_err(ctrl, "Issue of Set Attention command failed\n");
365*4882a593Smuzhiyun return rc;
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun p_slot->pwr_save = 0;
369*4882a593Smuzhiyun p_slot->is_a_board = 0;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun return 0;
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun struct pushbutton_work_info {
376*4882a593Smuzhiyun struct slot *p_slot;
377*4882a593Smuzhiyun struct work_struct work;
378*4882a593Smuzhiyun };
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun /**
381*4882a593Smuzhiyun * shpchp_pushbutton_thread - handle pushbutton events
382*4882a593Smuzhiyun * @work: &struct work_struct to be handled
383*4882a593Smuzhiyun *
384*4882a593Smuzhiyun * Scheduled procedure to handle blocking stuff for the pushbuttons.
385*4882a593Smuzhiyun * Handles all pending events and exits.
386*4882a593Smuzhiyun */
shpchp_pushbutton_thread(struct work_struct * work)387*4882a593Smuzhiyun static void shpchp_pushbutton_thread(struct work_struct *work)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun struct pushbutton_work_info *info =
390*4882a593Smuzhiyun container_of(work, struct pushbutton_work_info, work);
391*4882a593Smuzhiyun struct slot *p_slot = info->p_slot;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun mutex_lock(&p_slot->lock);
394*4882a593Smuzhiyun switch (p_slot->state) {
395*4882a593Smuzhiyun case POWEROFF_STATE:
396*4882a593Smuzhiyun mutex_unlock(&p_slot->lock);
397*4882a593Smuzhiyun shpchp_disable_slot(p_slot);
398*4882a593Smuzhiyun mutex_lock(&p_slot->lock);
399*4882a593Smuzhiyun p_slot->state = STATIC_STATE;
400*4882a593Smuzhiyun break;
401*4882a593Smuzhiyun case POWERON_STATE:
402*4882a593Smuzhiyun mutex_unlock(&p_slot->lock);
403*4882a593Smuzhiyun if (shpchp_enable_slot(p_slot))
404*4882a593Smuzhiyun p_slot->hpc_ops->green_led_off(p_slot);
405*4882a593Smuzhiyun mutex_lock(&p_slot->lock);
406*4882a593Smuzhiyun p_slot->state = STATIC_STATE;
407*4882a593Smuzhiyun break;
408*4882a593Smuzhiyun default:
409*4882a593Smuzhiyun break;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun mutex_unlock(&p_slot->lock);
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun kfree(info);
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun
shpchp_queue_pushbutton_work(struct work_struct * work)416*4882a593Smuzhiyun void shpchp_queue_pushbutton_work(struct work_struct *work)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun struct slot *p_slot = container_of(work, struct slot, work.work);
419*4882a593Smuzhiyun struct pushbutton_work_info *info;
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun info = kmalloc(sizeof(*info), GFP_KERNEL);
422*4882a593Smuzhiyun if (!info) {
423*4882a593Smuzhiyun ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
424*4882a593Smuzhiyun __func__);
425*4882a593Smuzhiyun return;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun info->p_slot = p_slot;
428*4882a593Smuzhiyun INIT_WORK(&info->work, shpchp_pushbutton_thread);
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun mutex_lock(&p_slot->lock);
431*4882a593Smuzhiyun switch (p_slot->state) {
432*4882a593Smuzhiyun case BLINKINGOFF_STATE:
433*4882a593Smuzhiyun p_slot->state = POWEROFF_STATE;
434*4882a593Smuzhiyun break;
435*4882a593Smuzhiyun case BLINKINGON_STATE:
436*4882a593Smuzhiyun p_slot->state = POWERON_STATE;
437*4882a593Smuzhiyun break;
438*4882a593Smuzhiyun default:
439*4882a593Smuzhiyun kfree(info);
440*4882a593Smuzhiyun goto out;
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun queue_work(p_slot->wq, &info->work);
443*4882a593Smuzhiyun out:
444*4882a593Smuzhiyun mutex_unlock(&p_slot->lock);
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun
update_slot_info(struct slot * slot)447*4882a593Smuzhiyun static void update_slot_info(struct slot *slot)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun slot->hpc_ops->get_power_status(slot, &slot->pwr_save);
450*4882a593Smuzhiyun slot->hpc_ops->get_attention_status(slot, &slot->attention_save);
451*4882a593Smuzhiyun slot->hpc_ops->get_latch_status(slot, &slot->latch_save);
452*4882a593Smuzhiyun slot->hpc_ops->get_adapter_status(slot, &slot->presence_save);
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun /*
456*4882a593Smuzhiyun * Note: This function must be called with slot->lock held
457*4882a593Smuzhiyun */
handle_button_press_event(struct slot * p_slot)458*4882a593Smuzhiyun static void handle_button_press_event(struct slot *p_slot)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun u8 getstatus;
461*4882a593Smuzhiyun struct controller *ctrl = p_slot->ctrl;
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun switch (p_slot->state) {
464*4882a593Smuzhiyun case STATIC_STATE:
465*4882a593Smuzhiyun p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
466*4882a593Smuzhiyun if (getstatus) {
467*4882a593Smuzhiyun p_slot->state = BLINKINGOFF_STATE;
468*4882a593Smuzhiyun ctrl_info(ctrl, "PCI slot #%s - powering off due to button press\n",
469*4882a593Smuzhiyun slot_name(p_slot));
470*4882a593Smuzhiyun } else {
471*4882a593Smuzhiyun p_slot->state = BLINKINGON_STATE;
472*4882a593Smuzhiyun ctrl_info(ctrl, "PCI slot #%s - powering on due to button press\n",
473*4882a593Smuzhiyun slot_name(p_slot));
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun /* blink green LED and turn off amber */
476*4882a593Smuzhiyun p_slot->hpc_ops->green_led_blink(p_slot);
477*4882a593Smuzhiyun p_slot->hpc_ops->set_attention_status(p_slot, 0);
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun queue_delayed_work(p_slot->wq, &p_slot->work, 5*HZ);
480*4882a593Smuzhiyun break;
481*4882a593Smuzhiyun case BLINKINGOFF_STATE:
482*4882a593Smuzhiyun case BLINKINGON_STATE:
483*4882a593Smuzhiyun /*
484*4882a593Smuzhiyun * Cancel if we are still blinking; this means that we
485*4882a593Smuzhiyun * press the attention again before the 5 sec. limit
486*4882a593Smuzhiyun * expires to cancel hot-add or hot-remove
487*4882a593Smuzhiyun */
488*4882a593Smuzhiyun ctrl_info(ctrl, "Button cancel on Slot(%s)\n",
489*4882a593Smuzhiyun slot_name(p_slot));
490*4882a593Smuzhiyun cancel_delayed_work(&p_slot->work);
491*4882a593Smuzhiyun if (p_slot->state == BLINKINGOFF_STATE)
492*4882a593Smuzhiyun p_slot->hpc_ops->green_led_on(p_slot);
493*4882a593Smuzhiyun else
494*4882a593Smuzhiyun p_slot->hpc_ops->green_led_off(p_slot);
495*4882a593Smuzhiyun p_slot->hpc_ops->set_attention_status(p_slot, 0);
496*4882a593Smuzhiyun ctrl_info(ctrl, "PCI slot #%s - action canceled due to button press\n",
497*4882a593Smuzhiyun slot_name(p_slot));
498*4882a593Smuzhiyun p_slot->state = STATIC_STATE;
499*4882a593Smuzhiyun break;
500*4882a593Smuzhiyun case POWEROFF_STATE:
501*4882a593Smuzhiyun case POWERON_STATE:
502*4882a593Smuzhiyun /*
503*4882a593Smuzhiyun * Ignore if the slot is on power-on or power-off state;
504*4882a593Smuzhiyun * this means that the previous attention button action
505*4882a593Smuzhiyun * to hot-add or hot-remove is undergoing
506*4882a593Smuzhiyun */
507*4882a593Smuzhiyun ctrl_info(ctrl, "Button ignore on Slot(%s)\n",
508*4882a593Smuzhiyun slot_name(p_slot));
509*4882a593Smuzhiyun update_slot_info(p_slot);
510*4882a593Smuzhiyun break;
511*4882a593Smuzhiyun default:
512*4882a593Smuzhiyun ctrl_warn(ctrl, "Not a valid state\n");
513*4882a593Smuzhiyun break;
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun
interrupt_event_handler(struct work_struct * work)517*4882a593Smuzhiyun static void interrupt_event_handler(struct work_struct *work)
518*4882a593Smuzhiyun {
519*4882a593Smuzhiyun struct event_info *info = container_of(work, struct event_info, work);
520*4882a593Smuzhiyun struct slot *p_slot = info->p_slot;
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun mutex_lock(&p_slot->lock);
523*4882a593Smuzhiyun switch (info->event_type) {
524*4882a593Smuzhiyun case INT_BUTTON_PRESS:
525*4882a593Smuzhiyun handle_button_press_event(p_slot);
526*4882a593Smuzhiyun break;
527*4882a593Smuzhiyun case INT_POWER_FAULT:
528*4882a593Smuzhiyun ctrl_dbg(p_slot->ctrl, "%s: Power fault\n", __func__);
529*4882a593Smuzhiyun p_slot->hpc_ops->set_attention_status(p_slot, 1);
530*4882a593Smuzhiyun p_slot->hpc_ops->green_led_off(p_slot);
531*4882a593Smuzhiyun break;
532*4882a593Smuzhiyun default:
533*4882a593Smuzhiyun update_slot_info(p_slot);
534*4882a593Smuzhiyun break;
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun mutex_unlock(&p_slot->lock);
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun kfree(info);
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun
shpchp_enable_slot(struct slot * p_slot)542*4882a593Smuzhiyun static int shpchp_enable_slot (struct slot *p_slot)
543*4882a593Smuzhiyun {
544*4882a593Smuzhiyun u8 getstatus = 0;
545*4882a593Smuzhiyun int rc, retval = -ENODEV;
546*4882a593Smuzhiyun struct controller *ctrl = p_slot->ctrl;
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun /* Check to see if (latch closed, card present, power off) */
549*4882a593Smuzhiyun mutex_lock(&p_slot->ctrl->crit_sect);
550*4882a593Smuzhiyun rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
551*4882a593Smuzhiyun if (rc || !getstatus) {
552*4882a593Smuzhiyun ctrl_info(ctrl, "No adapter on slot(%s)\n", slot_name(p_slot));
553*4882a593Smuzhiyun goto out;
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
556*4882a593Smuzhiyun if (rc || getstatus) {
557*4882a593Smuzhiyun ctrl_info(ctrl, "Latch open on slot(%s)\n", slot_name(p_slot));
558*4882a593Smuzhiyun goto out;
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
561*4882a593Smuzhiyun if (rc || getstatus) {
562*4882a593Smuzhiyun ctrl_info(ctrl, "Already enabled on slot(%s)\n",
563*4882a593Smuzhiyun slot_name(p_slot));
564*4882a593Smuzhiyun goto out;
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun p_slot->is_a_board = 1;
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun /* We have to save the presence info for these slots */
570*4882a593Smuzhiyun p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
571*4882a593Smuzhiyun p_slot->hpc_ops->get_power_status(p_slot, &(p_slot->pwr_save));
572*4882a593Smuzhiyun ctrl_dbg(ctrl, "%s: p_slot->pwr_save %x\n", __func__, p_slot->pwr_save);
573*4882a593Smuzhiyun p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun if ((p_slot->ctrl->pci_dev->vendor == PCI_VENDOR_ID_AMD &&
576*4882a593Smuzhiyun p_slot->ctrl->pci_dev->device == PCI_DEVICE_ID_AMD_POGO_7458)
577*4882a593Smuzhiyun && p_slot->ctrl->num_slots == 1) {
578*4882a593Smuzhiyun /* handle AMD POGO errata; this must be done before enable */
579*4882a593Smuzhiyun amd_pogo_errata_save_misc_reg(p_slot);
580*4882a593Smuzhiyun retval = board_added(p_slot);
581*4882a593Smuzhiyun /* handle AMD POGO errata; this must be done after enable */
582*4882a593Smuzhiyun amd_pogo_errata_restore_misc_reg(p_slot);
583*4882a593Smuzhiyun } else
584*4882a593Smuzhiyun retval = board_added(p_slot);
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun if (retval) {
587*4882a593Smuzhiyun p_slot->hpc_ops->get_adapter_status(p_slot,
588*4882a593Smuzhiyun &(p_slot->presence_save));
589*4882a593Smuzhiyun p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun update_slot_info(p_slot);
593*4882a593Smuzhiyun out:
594*4882a593Smuzhiyun mutex_unlock(&p_slot->ctrl->crit_sect);
595*4882a593Smuzhiyun return retval;
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun
shpchp_disable_slot(struct slot * p_slot)599*4882a593Smuzhiyun static int shpchp_disable_slot (struct slot *p_slot)
600*4882a593Smuzhiyun {
601*4882a593Smuzhiyun u8 getstatus = 0;
602*4882a593Smuzhiyun int rc, retval = -ENODEV;
603*4882a593Smuzhiyun struct controller *ctrl = p_slot->ctrl;
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun if (!p_slot->ctrl)
606*4882a593Smuzhiyun return -ENODEV;
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun /* Check to see if (latch closed, card present, power on) */
609*4882a593Smuzhiyun mutex_lock(&p_slot->ctrl->crit_sect);
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
612*4882a593Smuzhiyun if (rc || !getstatus) {
613*4882a593Smuzhiyun ctrl_info(ctrl, "No adapter on slot(%s)\n", slot_name(p_slot));
614*4882a593Smuzhiyun goto out;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
617*4882a593Smuzhiyun if (rc || getstatus) {
618*4882a593Smuzhiyun ctrl_info(ctrl, "Latch open on slot(%s)\n", slot_name(p_slot));
619*4882a593Smuzhiyun goto out;
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
622*4882a593Smuzhiyun if (rc || !getstatus) {
623*4882a593Smuzhiyun ctrl_info(ctrl, "Already disabled on slot(%s)\n",
624*4882a593Smuzhiyun slot_name(p_slot));
625*4882a593Smuzhiyun goto out;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun retval = remove_board(p_slot);
629*4882a593Smuzhiyun update_slot_info(p_slot);
630*4882a593Smuzhiyun out:
631*4882a593Smuzhiyun mutex_unlock(&p_slot->ctrl->crit_sect);
632*4882a593Smuzhiyun return retval;
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun
shpchp_sysfs_enable_slot(struct slot * p_slot)635*4882a593Smuzhiyun int shpchp_sysfs_enable_slot(struct slot *p_slot)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun int retval = -ENODEV;
638*4882a593Smuzhiyun struct controller *ctrl = p_slot->ctrl;
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun mutex_lock(&p_slot->lock);
641*4882a593Smuzhiyun switch (p_slot->state) {
642*4882a593Smuzhiyun case BLINKINGON_STATE:
643*4882a593Smuzhiyun cancel_delayed_work(&p_slot->work);
644*4882a593Smuzhiyun fallthrough;
645*4882a593Smuzhiyun case STATIC_STATE:
646*4882a593Smuzhiyun p_slot->state = POWERON_STATE;
647*4882a593Smuzhiyun mutex_unlock(&p_slot->lock);
648*4882a593Smuzhiyun retval = shpchp_enable_slot(p_slot);
649*4882a593Smuzhiyun mutex_lock(&p_slot->lock);
650*4882a593Smuzhiyun p_slot->state = STATIC_STATE;
651*4882a593Smuzhiyun break;
652*4882a593Smuzhiyun case POWERON_STATE:
653*4882a593Smuzhiyun ctrl_info(ctrl, "Slot %s is already in powering on state\n",
654*4882a593Smuzhiyun slot_name(p_slot));
655*4882a593Smuzhiyun break;
656*4882a593Smuzhiyun case BLINKINGOFF_STATE:
657*4882a593Smuzhiyun case POWEROFF_STATE:
658*4882a593Smuzhiyun ctrl_info(ctrl, "Already enabled on slot %s\n",
659*4882a593Smuzhiyun slot_name(p_slot));
660*4882a593Smuzhiyun break;
661*4882a593Smuzhiyun default:
662*4882a593Smuzhiyun ctrl_err(ctrl, "Not a valid state on slot %s\n",
663*4882a593Smuzhiyun slot_name(p_slot));
664*4882a593Smuzhiyun break;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun mutex_unlock(&p_slot->lock);
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun return retval;
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun
shpchp_sysfs_disable_slot(struct slot * p_slot)671*4882a593Smuzhiyun int shpchp_sysfs_disable_slot(struct slot *p_slot)
672*4882a593Smuzhiyun {
673*4882a593Smuzhiyun int retval = -ENODEV;
674*4882a593Smuzhiyun struct controller *ctrl = p_slot->ctrl;
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun mutex_lock(&p_slot->lock);
677*4882a593Smuzhiyun switch (p_slot->state) {
678*4882a593Smuzhiyun case BLINKINGOFF_STATE:
679*4882a593Smuzhiyun cancel_delayed_work(&p_slot->work);
680*4882a593Smuzhiyun fallthrough;
681*4882a593Smuzhiyun case STATIC_STATE:
682*4882a593Smuzhiyun p_slot->state = POWEROFF_STATE;
683*4882a593Smuzhiyun mutex_unlock(&p_slot->lock);
684*4882a593Smuzhiyun retval = shpchp_disable_slot(p_slot);
685*4882a593Smuzhiyun mutex_lock(&p_slot->lock);
686*4882a593Smuzhiyun p_slot->state = STATIC_STATE;
687*4882a593Smuzhiyun break;
688*4882a593Smuzhiyun case POWEROFF_STATE:
689*4882a593Smuzhiyun ctrl_info(ctrl, "Slot %s is already in powering off state\n",
690*4882a593Smuzhiyun slot_name(p_slot));
691*4882a593Smuzhiyun break;
692*4882a593Smuzhiyun case BLINKINGON_STATE:
693*4882a593Smuzhiyun case POWERON_STATE:
694*4882a593Smuzhiyun ctrl_info(ctrl, "Already disabled on slot %s\n",
695*4882a593Smuzhiyun slot_name(p_slot));
696*4882a593Smuzhiyun break;
697*4882a593Smuzhiyun default:
698*4882a593Smuzhiyun ctrl_err(ctrl, "Not a valid state on slot %s\n",
699*4882a593Smuzhiyun slot_name(p_slot));
700*4882a593Smuzhiyun break;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun mutex_unlock(&p_slot->lock);
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun return retval;
705*4882a593Smuzhiyun }
706