blob: 720dfe5fc48a41cc3609e364992e441e75a2212e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PCI Express PCI Hot Plug 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 *
Kristen Accardi8cf4c192005-08-16 15:16:10 -070026 * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/types.h>
Tim Schmielaude259682006-01-08 01:02:05 -080033#include <linux/signal.h>
34#include <linux/jiffies.h>
35#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/pci.h>
Andrew Morton5d1b8c92005-11-13 16:06:39 -080037#include <linux/interrupt.h>
Kristen Carlson Accardi34d03412007-01-09 13:02:36 -080038#include <linux/time.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Andrew Morton5d1b8c92005-11-13 16:06:39 -080040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "../pci.h"
42#include "pciehp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Bjorn Helgaascd84d342013-05-09 11:26:16 -060044static inline struct pci_dev *ctrl_dev(struct controller *ctrl)
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -080045{
Bjorn Helgaascd84d342013-05-09 11:26:16 -060046 return ctrl->pcie->port;
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -080047}
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Kenji Kaneshige48fe3912006-12-21 17:01:04 -080049static irqreturn_t pcie_isr(int irq, void *dev_id);
50static void start_int_poll_timer(struct controller *ctrl, int sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* This is the interrupt polling timeout function. */
Kenji Kaneshige48fe3912006-12-21 17:01:04 -080053static void int_poll_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -080055 struct controller *ctrl = (struct controller *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 /* Poll for interrupt events. regs == NULL => polling */
Kenji Kaneshige48fe3912006-12-21 17:01:04 -080058 pcie_isr(0, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Kenji Kaneshige48fe3912006-12-21 17:01:04 -080060 init_timer(&ctrl->poll_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (!pciehp_poll_time)
Kenji Kaneshige40730d12007-08-09 16:09:38 -070062 pciehp_poll_time = 2; /* default polling interval is 2 sec */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Kenji Kaneshige48fe3912006-12-21 17:01:04 -080064 start_int_poll_timer(ctrl, pciehp_poll_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67/* This function starts the interrupt polling timer. */
Kenji Kaneshige48fe3912006-12-21 17:01:04 -080068static void start_int_poll_timer(struct controller *ctrl, int sec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -080070 /* Clamp to sane value */
71 if ((sec <= 0) || (sec > 60))
Bjorn Helgaasf7625982013-11-14 11:28:18 -070072 sec = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Kenji Kaneshige48fe3912006-12-21 17:01:04 -080074 ctrl->poll_timer.function = &int_poll_timeout;
75 ctrl->poll_timer.data = (unsigned long)ctrl;
76 ctrl->poll_timer.expires = jiffies + sec * HZ;
77 add_timer(&ctrl->poll_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -070080static inline int pciehp_request_irq(struct controller *ctrl)
81{
Kenji Kaneshigef7a10e32008-08-22 17:16:48 +090082 int retval, irq = ctrl->pcie->irq;
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -070083
84 /* Install interrupt polling timer. Start with 10 sec delay */
85 if (pciehp_poll_mode) {
86 init_timer(&ctrl->poll_timer);
87 start_int_poll_timer(ctrl, 10);
88 return 0;
89 }
90
91 /* Installs the interrupt handler */
92 retval = request_irq(irq, pcie_isr, IRQF_SHARED, MY_NAME, ctrl);
93 if (retval)
Taku Izumi7f2feec2008-09-05 12:11:26 +090094 ctrl_err(ctrl, "Cannot get irq %d for the hotplug controller\n",
95 irq);
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -070096 return retval;
97}
98
99static inline void pciehp_free_irq(struct controller *ctrl)
100{
101 if (pciehp_poll_mode)
102 del_timer_sync(&ctrl->poll_timer);
103 else
Kenji Kaneshigef7a10e32008-08-22 17:16:48 +0900104 free_irq(ctrl->pcie->irq, ctrl);
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -0700105}
106
Bjorn Helgaas40b96082014-06-14 09:55:49 -0600107static int pcie_poll_cmd(struct controller *ctrl, int timeout)
Kenji Kaneshige6592e022008-05-27 19:05:26 +0900108{
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600109 struct pci_dev *pdev = ctrl_dev(ctrl);
Kenji Kaneshige6592e022008-05-27 19:05:26 +0900110 u16 slot_status;
Kenji Kaneshige6592e022008-05-27 19:05:26 +0900111
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700112 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
113 if (slot_status & PCI_EXP_SLTSTA_CC) {
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600114 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
115 PCI_EXP_SLTSTA_CC);
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900116 return 1;
Kenji Kaneshige820943b2008-06-20 12:04:33 +0900117 }
Adrian Bunka5827f42008-08-28 01:05:26 +0300118 while (timeout > 0) {
Kenji Kaneshige66618ba2008-06-20 12:05:12 +0900119 msleep(10);
120 timeout -= 10;
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700121 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
122 if (slot_status & PCI_EXP_SLTSTA_CC) {
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600123 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
124 PCI_EXP_SLTSTA_CC);
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900125 return 1;
Kenji Kaneshige820943b2008-06-20 12:04:33 +0900126 }
Kenji Kaneshige6592e022008-05-27 19:05:26 +0900127 }
128 return 0; /* timeout */
Kenji Kaneshige6592e022008-05-27 19:05:26 +0900129}
130
Bjorn Helgaas4283c702014-06-13 13:58:35 -0600131static void pcie_wait_cmd(struct controller *ctrl)
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800132{
Kenji Kaneshige262303fe2006-12-21 17:01:10 -0800133 unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
Bjorn Helgaas40b96082014-06-14 09:55:49 -0600134 unsigned long duration = msecs_to_jiffies(msecs);
135 unsigned long cmd_timeout = ctrl->cmd_started + duration;
136 unsigned long now, timeout;
Kenji Kaneshige262303fe2006-12-21 17:01:10 -0800137 int rc;
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800138
Bjorn Helgaas4283c702014-06-13 13:58:35 -0600139 /*
140 * If the controller does not generate notifications for command
141 * completions, we never need to wait between writes.
142 */
143 if (ctrl->no_cmd_complete)
144 return;
145
146 if (!ctrl->cmd_busy)
147 return;
148
Bjorn Helgaas40b96082014-06-14 09:55:49 -0600149 /*
150 * Even if the command has already timed out, we want to call
151 * pcie_poll_cmd() so it can clear PCI_EXP_SLTSTA_CC.
152 */
153 now = jiffies;
154 if (time_before_eq(cmd_timeout, now))
155 timeout = 1;
156 else
157 timeout = cmd_timeout - now;
158
Bjorn Helgaas4283c702014-06-13 13:58:35 -0600159 if (ctrl->slot_ctrl & PCI_EXP_SLTCTL_HPIE &&
160 ctrl->slot_ctrl & PCI_EXP_SLTCTL_CCIE)
Kenji Kaneshiged737bdc2008-05-28 14:59:44 +0900161 rc = wait_event_timeout(ctrl->queue, !ctrl->cmd_busy, timeout);
Bjorn Helgaas4283c702014-06-13 13:58:35 -0600162 else
Bjorn Helgaas40b96082014-06-14 09:55:49 -0600163 rc = pcie_poll_cmd(ctrl, timeout);
164
165 /*
166 * Controllers with errata like Intel CF118 don't generate
167 * completion notifications unless the power/indicator/interlock
168 * control bits are changed. On such controllers, we'll emit this
169 * timeout message when we wait for completion of commands that
170 * don't change those bits, e.g., commands that merely enable
171 * interrupts.
172 */
Kenji Kaneshige262303fe2006-12-21 17:01:10 -0800173 if (!rc)
Bjorn Helgaas40b96082014-06-14 09:55:49 -0600174 ctrl_info(ctrl, "Timeout on hotplug command %#010x (issued %u msec ago)\n",
175 ctrl->slot_ctrl,
176 jiffies_to_msecs(now - ctrl->cmd_started));
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800177}
178
Kenji Kaneshigef4778362007-05-31 09:43:34 -0700179/**
180 * pcie_write_cmd - Issue controller command
Kenji Kaneshigec27fb8832008-04-25 14:39:05 -0700181 * @ctrl: controller to which the command is issued
Kenji Kaneshigef4778362007-05-31 09:43:34 -0700182 * @cmd: command value written to slot control register
183 * @mask: bitmask of slot control register to be modified
184 */
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700185static void pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600187 struct pci_dev *pdev = ctrl_dev(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 u16 slot_status;
Kenji Kaneshigef4778362007-05-31 09:43:34 -0700189 u16 slot_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800191 mutex_lock(&ctrl->ctrl_lock);
192
Bjorn Helgaas3461a062014-06-13 15:06:40 -0600193 /* Wait for any previous command that might still be in progress */
194 pcie_wait_cmd(ctrl);
195
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700196 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900197 if (slot_status & PCI_EXP_SLTSTA_CC) {
Rajat Jain476a3572014-02-20 17:42:31 -0800198 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
199 PCI_EXP_SLTSTA_CC);
Kenji Kaneshige58086392008-05-27 19:04:30 +0900200 if (!ctrl->no_cmd_complete) {
201 /*
202 * After 1 sec and CMD_COMPLETED still not set, just
203 * proceed forward to issue the next command according
204 * to spec. Just print out the error message.
205 */
Taku Izumi18b341b2008-10-23 11:47:32 +0900206 ctrl_dbg(ctrl, "CMD_COMPLETED not clear after 1 sec\n");
Kenji Kaneshige58086392008-05-27 19:04:30 +0900207 } else if (!NO_CMD_CMPL(ctrl)) {
208 /*
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700209 * This controller seems to notify of command completed
Kenji Kaneshige58086392008-05-27 19:04:30 +0900210 * event even though it supports none of power
211 * controller, attention led, power led and EMI.
212 */
Ryan Desfosses227f0642014-04-18 20:13:50 -0400213 ctrl_dbg(ctrl, "Unexpected CMD_COMPLETED. Need to wait for command completed event\n");
Kenji Kaneshige58086392008-05-27 19:04:30 +0900214 ctrl->no_cmd_complete = 0;
215 } else {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400216 ctrl_dbg(ctrl, "Unexpected CMD_COMPLETED. Maybe the controller is broken\n");
Kenji Kaneshige58086392008-05-27 19:04:30 +0900217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700220 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
Kenji Kaneshigef4778362007-05-31 09:43:34 -0700221 slot_ctrl &= ~mask;
Kenji Kaneshigeb7aa1f12008-04-25 14:39:14 -0700222 slot_ctrl |= (cmd & mask);
Kenji Kaneshigef4778362007-05-31 09:43:34 -0700223 ctrl->cmd_busy = 1;
Kenji Kaneshige2d32a9a2008-04-25 14:39:02 -0700224 smp_mb();
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700225 pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, slot_ctrl);
Bjorn Helgaas40b96082014-06-14 09:55:49 -0600226 ctrl->cmd_started = jiffies;
Bjorn Helgaas4283c702014-06-13 13:58:35 -0600227 ctrl->slot_ctrl = slot_ctrl;
Kenji Kaneshigef4778362007-05-31 09:43:34 -0700228
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800229 mutex_unlock(&ctrl->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Rajat Jain47033892014-02-04 18:28:43 -0800232bool pciehp_check_link_active(struct controller *ctrl)
Kenji Kaneshigef18e9622008-10-22 14:31:44 +0900233{
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600234 struct pci_dev *pdev = ctrl_dev(ctrl);
Yinghai Lu4e2ce402012-01-27 10:55:12 -0800235 u16 lnk_status;
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700236 bool ret;
Kenji Kaneshigef18e9622008-10-22 14:31:44 +0900237
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700238 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
Yinghai Lu4e2ce402012-01-27 10:55:12 -0800239 ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
240
241 if (ret)
242 ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
243
244 return ret;
Kenji Kaneshigef18e9622008-10-22 14:31:44 +0900245}
246
Yinghai Lubffe4f72012-01-27 10:55:13 -0800247static void __pcie_wait_link_active(struct controller *ctrl, bool active)
Kenji Kaneshigef18e9622008-10-22 14:31:44 +0900248{
249 int timeout = 1000;
250
Rajat Jain47033892014-02-04 18:28:43 -0800251 if (pciehp_check_link_active(ctrl) == active)
Kenji Kaneshigef18e9622008-10-22 14:31:44 +0900252 return;
253 while (timeout > 0) {
254 msleep(10);
255 timeout -= 10;
Rajat Jain47033892014-02-04 18:28:43 -0800256 if (pciehp_check_link_active(ctrl) == active)
Kenji Kaneshigef18e9622008-10-22 14:31:44 +0900257 return;
258 }
Yinghai Lubffe4f72012-01-27 10:55:13 -0800259 ctrl_dbg(ctrl, "Data Link Layer Link Active not %s in 1000 msec\n",
260 active ? "set" : "cleared");
261}
262
263static void pcie_wait_link_active(struct controller *ctrl)
264{
265 __pcie_wait_link_active(ctrl, true);
266}
267
Yinghai Lu2f5d8e42012-01-27 10:55:11 -0800268static bool pci_bus_check_dev(struct pci_bus *bus, int devfn)
269{
270 u32 l;
271 int count = 0;
272 int delay = 1000, step = 20;
273 bool found = false;
274
275 do {
276 found = pci_bus_read_dev_vendor_id(bus, devfn, &l, 0);
277 count++;
278
279 if (found)
280 break;
281
282 msleep(step);
283 delay -= step;
284 } while (delay > 0);
285
286 if (count > 1 && pciehp_debug)
287 printk(KERN_DEBUG "pci %04x:%02x:%02x.%d id reading try %d times with interval %d ms to get %08x\n",
288 pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
289 PCI_FUNC(devfn), count, step, l);
290
291 return found;
292}
293
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900294int pciehp_check_link_status(struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600296 struct pci_dev *pdev = ctrl_dev(ctrl);
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700297 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 u16 lnk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400300 /*
301 * Data Link Layer Link Active Reporting must be capable for
302 * hot-plug capable downstream port. But old controller might
303 * not implement it. In this case, we wait for 1000 ms.
304 */
305 if (ctrl->link_active_reporting)
306 pcie_wait_link_active(ctrl);
307 else
308 msleep(1000);
Kenji Kaneshigef18e9622008-10-22 14:31:44 +0900309
Yinghai Lu2f5d8e42012-01-27 10:55:11 -0800310 /* wait 100ms before read pci conf, and try in 1s */
311 msleep(100);
312 found = pci_bus_check_dev(ctrl->pcie->port->subordinate,
313 PCI_DEVFN(0, 0));
Kenji Kaneshige0027cb32011-11-10 16:40:37 +0900314
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700315 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
Taku Izumi7f2feec2008-09-05 12:11:26 +0900316 ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900317 if ((lnk_status & PCI_EXP_LNKSTA_LT) ||
318 !(lnk_status & PCI_EXP_LNKSTA_NLW)) {
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400319 ctrl_err(ctrl, "Link Training Error occurs\n");
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700320 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
Yinghai Lufdbd3ce2011-11-07 07:53:23 -0800323 pcie_update_link_speed(ctrl->pcie->port->subordinate, lnk_status);
324
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700325 if (!found)
326 return -1;
Yinghai Lu2f5d8e42012-01-27 10:55:11 -0800327
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Yinghai Lu7f822992012-01-27 10:55:14 -0800331static int __pciehp_link_set(struct controller *ctrl, bool enable)
332{
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600333 struct pci_dev *pdev = ctrl_dev(ctrl);
Yinghai Lu7f822992012-01-27 10:55:14 -0800334 u16 lnk_ctrl;
Yinghai Lu7f822992012-01-27 10:55:14 -0800335
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700336 pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &lnk_ctrl);
Yinghai Lu7f822992012-01-27 10:55:14 -0800337
338 if (enable)
339 lnk_ctrl &= ~PCI_EXP_LNKCTL_LD;
340 else
341 lnk_ctrl |= PCI_EXP_LNKCTL_LD;
342
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700343 pcie_capability_write_word(pdev, PCI_EXP_LNKCTL, lnk_ctrl);
Yinghai Lu7f822992012-01-27 10:55:14 -0800344 ctrl_dbg(ctrl, "%s: lnk_ctrl = %x\n", __func__, lnk_ctrl);
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700345 return 0;
Yinghai Lu7f822992012-01-27 10:55:14 -0800346}
347
348static int pciehp_link_enable(struct controller *ctrl)
349{
350 return __pciehp_link_set(ctrl, true);
351}
352
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700353void pciehp_get_attention_status(struct slot *slot, u8 *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800355 struct controller *ctrl = slot->ctrl;
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600356 struct pci_dev *pdev = ctrl_dev(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 u16 slot_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700359 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
Kenji Kaneshige1518c172009-11-11 14:34:52 +0900360 ctrl_dbg(ctrl, "%s: SLOTCTRL %x, value read %x\n", __func__,
361 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700363 switch (slot_ctrl & PCI_EXP_SLTCTL_AIC) {
364 case PCI_EXP_SLTCTL_ATTN_IND_ON:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *status = 1; /* On */
366 break;
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700367 case PCI_EXP_SLTCTL_ATTN_IND_BLINK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 *status = 2; /* Blink */
369 break;
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700370 case PCI_EXP_SLTCTL_ATTN_IND_OFF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 *status = 0; /* Off */
372 break;
373 default:
374 *status = 0xFF;
375 break;
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700379void pciehp_get_power_status(struct slot *slot, u8 *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800381 struct controller *ctrl = slot->ctrl;
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600382 struct pci_dev *pdev = ctrl_dev(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 u16 slot_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700385 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
Kenji Kaneshige1518c172009-11-11 14:34:52 +0900386 ctrl_dbg(ctrl, "%s: SLOTCTRL %x value read %x\n", __func__,
387 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700389 switch (slot_ctrl & PCI_EXP_SLTCTL_PCC) {
390 case PCI_EXP_SLTCTL_PWR_ON:
391 *status = 1; /* On */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 break;
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700393 case PCI_EXP_SLTCTL_PWR_OFF:
394 *status = 0; /* Off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 break;
396 default:
397 *status = 0xFF;
398 break;
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700402void pciehp_get_latch_status(struct slot *slot, u8 *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700404 struct pci_dev *pdev = ctrl_dev(slot->ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 u16 slot_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700407 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900408 *status = !!(slot_status & PCI_EXP_SLTSTA_MRLSS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700411void pciehp_get_adapter_status(struct slot *slot, u8 *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700413 struct pci_dev *pdev = ctrl_dev(slot->ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 u16 slot_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700416 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900417 *status = !!(slot_status & PCI_EXP_SLTSTA_PDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900420int pciehp_query_power_fault(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700422 struct pci_dev *pdev = ctrl_dev(slot->ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 u16 slot_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700425 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900426 return !!(slot_status & PCI_EXP_SLTSTA_PFD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700429void pciehp_set_attention_status(struct slot *slot, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800431 struct controller *ctrl = slot->ctrl;
Kenji Kaneshigef4778362007-05-31 09:43:34 -0700432 u16 slot_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Bjorn Helgaasaf9ab792013-12-15 17:23:54 -0700434 if (!ATTN_LED(ctrl))
435 return;
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 switch (value) {
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400438 case 0: /* turn off */
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700439 slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_OFF;
Kenji Kaneshige445f7982009-10-05 17:42:59 +0900440 break;
441 case 1: /* turn on */
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700442 slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_ON;
Kenji Kaneshige445f7982009-10-05 17:42:59 +0900443 break;
444 case 2: /* turn blink */
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700445 slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_BLINK;
Kenji Kaneshige445f7982009-10-05 17:42:59 +0900446 break;
447 default:
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700448 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Kenji Kaneshige1518c172009-11-11 14:34:52 +0900450 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
451 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd);
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700452 pcie_write_cmd(ctrl, slot_cmd, PCI_EXP_SLTCTL_AIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900455void pciehp_green_led_on(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800457 struct controller *ctrl = slot->ctrl;
Kenji Kaneshige71ad5562007-08-09 16:09:34 -0700458
Bjorn Helgaasaf9ab792013-12-15 17:23:54 -0700459 if (!PWR_LED(ctrl))
460 return;
461
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700462 pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_IND_ON, PCI_EXP_SLTCTL_PIC);
Kenji Kaneshige1518c172009-11-11 14:34:52 +0900463 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700464 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
465 PCI_EXP_SLTCTL_PWR_IND_ON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900468void pciehp_green_led_off(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800470 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Bjorn Helgaasaf9ab792013-12-15 17:23:54 -0700472 if (!PWR_LED(ctrl))
473 return;
474
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700475 pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF, PCI_EXP_SLTCTL_PIC);
Kenji Kaneshige1518c172009-11-11 14:34:52 +0900476 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700477 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
478 PCI_EXP_SLTCTL_PWR_IND_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900481void pciehp_green_led_blink(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800483 struct controller *ctrl = slot->ctrl;
Kenji Kaneshige71ad5562007-08-09 16:09:34 -0700484
Bjorn Helgaasaf9ab792013-12-15 17:23:54 -0700485 if (!PWR_LED(ctrl))
486 return;
487
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700488 pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_IND_BLINK, PCI_EXP_SLTCTL_PIC);
Kenji Kaneshige1518c172009-11-11 14:34:52 +0900489 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700490 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
491 PCI_EXP_SLTCTL_PWR_IND_BLINK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400494int pciehp_power_on_slot(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800496 struct controller *ctrl = slot->ctrl;
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600497 struct pci_dev *pdev = ctrl_dev(ctrl);
Kenji Kaneshigef4778362007-05-31 09:43:34 -0700498 u16 slot_status;
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700499 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Rajesh Shah5a49f202005-11-23 15:44:54 -0800501 /* Clear sticky power-fault bit from previous power failures */
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700502 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
Bjorn Helgaas2f2ed41c2013-12-14 13:06:40 -0700503 if (slot_status & PCI_EXP_SLTSTA_PFD)
504 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
505 PCI_EXP_SLTSTA_PFD);
Kenji Kaneshige5651c48c2009-11-13 15:14:10 +0900506 ctrl->power_fault_detected = 0;
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800507
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700508 pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_ON, PCI_EXP_SLTCTL_PCC);
Kenji Kaneshige1518c172009-11-11 14:34:52 +0900509 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700510 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
511 PCI_EXP_SLTCTL_PWR_ON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Yinghai Lu2debd922012-01-27 10:55:15 -0800513 retval = pciehp_link_enable(ctrl);
514 if (retval)
515 ctrl_err(ctrl, "%s: Can not enable the link!\n", __func__);
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return retval;
518}
519
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400520void pciehp_power_off_slot(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800522 struct controller *ctrl = slot->ctrl;
Kenji Kaneshigef1050a32007-12-20 19:45:09 +0900523
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700524 pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_OFF, PCI_EXP_SLTCTL_PCC);
Kenji Kaneshige1518c172009-11-11 14:34:52 +0900525 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
Bjorn Helgaase7b4f0d72013-12-14 13:06:53 -0700526 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
527 PCI_EXP_SLTCTL_PWR_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800530static irqreturn_t pcie_isr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800532 struct controller *ctrl = (struct controller *)dev_id;
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600533 struct pci_dev *pdev = ctrl_dev(ctrl);
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900534 struct slot *slot = ctrl->slot;
Kenji Kaneshigec6b069e2008-04-25 14:38:57 -0700535 u16 detected, intr_loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Kenji Kaneshigec6b069e2008-04-25 14:38:57 -0700537 /*
538 * In order to guarantee that all interrupt events are
539 * serviced, we need to re-inspect Slot Status register after
540 * clearing what is presumed to be the last pending interrupt.
541 */
542 intr_loc = 0;
543 do {
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700544 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &detected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900546 detected &= (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
547 PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC |
Rajat Jaine48f1b62014-02-04 18:29:10 -0800548 PCI_EXP_SLTSTA_CC | PCI_EXP_SLTSTA_DLLSC);
Kenji Kaneshige81b840c2009-02-03 15:06:13 +0900549 detected &= ~intr_loc;
Kenji Kaneshigec6b069e2008-04-25 14:38:57 -0700550 intr_loc |= detected;
551 if (!intr_loc)
552 return IRQ_NONE;
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700553 if (detected)
554 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
555 intr_loc);
Kenji Kaneshigec6b069e2008-04-25 14:38:57 -0700556 } while (detected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Taku Izumi7f2feec2008-09-05 12:11:26 +0900558 ctrl_dbg(ctrl, "%s: intr_loc %x\n", __func__, intr_loc);
Kenji Kaneshige71ad5562007-08-09 16:09:34 -0700559
Kenji Kaneshigec6b069e2008-04-25 14:38:57 -0700560 /* Check Command Complete Interrupt Pending */
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900561 if (intr_loc & PCI_EXP_SLTSTA_CC) {
Kenji Kaneshige262303fe2006-12-21 17:01:10 -0800562 ctrl->cmd_busy = 0;
Kenji Kaneshige2d32a9a2008-04-25 14:39:02 -0700563 smp_mb();
Kenji Kaneshiged737bdc2008-05-28 14:59:44 +0900564 wake_up(&ctrl->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900567 if (!(intr_loc & ~PCI_EXP_SLTSTA_CC))
Kenji Kaneshigedbd79ae2008-05-27 19:03:16 +0900568 return IRQ_HANDLED;
569
Kenji Kaneshigec6b069e2008-04-25 14:38:57 -0700570 /* Check MRL Sensor Changed */
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900571 if (intr_loc & PCI_EXP_SLTSTA_MRLSC)
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900572 pciehp_handle_switch_change(slot);
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800573
Kenji Kaneshigec6b069e2008-04-25 14:38:57 -0700574 /* Check Attention Button Pressed */
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900575 if (intr_loc & PCI_EXP_SLTSTA_ABP)
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900576 pciehp_handle_attention_button(slot);
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800577
Kenji Kaneshigec6b069e2008-04-25 14:38:57 -0700578 /* Check Presence Detect Changed */
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900579 if (intr_loc & PCI_EXP_SLTSTA_PDC)
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900580 pciehp_handle_presence_change(slot);
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800581
Kenji Kaneshigec6b069e2008-04-25 14:38:57 -0700582 /* Check Power Fault Detected */
Kenji Kaneshige99f01692009-02-03 15:06:16 +0900583 if ((intr_loc & PCI_EXP_SLTSTA_PFD) && !ctrl->power_fault_detected) {
584 ctrl->power_fault_detected = 1;
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900585 pciehp_handle_power_fault(slot);
Kenji Kaneshige99f01692009-02-03 15:06:16 +0900586 }
Rajat Jaine48f1b62014-02-04 18:29:10 -0800587
588 if (intr_loc & PCI_EXP_SLTSTA_DLLSC)
589 pciehp_handle_linkstate_change(slot);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return IRQ_HANDLED;
592}
593
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700594void pcie_enable_notification(struct controller *ctrl)
Mark Lordecdde932007-11-21 15:07:55 -0800595{
Kenji Kaneshigec27fb8832008-04-25 14:39:05 -0700596 u16 cmd, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Kenji Kaneshige5651c48c2009-11-13 15:14:10 +0900598 /*
599 * TBD: Power fault detected software notification support.
600 *
601 * Power fault detected software notification is not enabled
602 * now, because it caused power fault detected interrupt storm
603 * on some machines. On those machines, power fault detected
604 * bit in the slot status register was set again immediately
605 * when it is cleared in the interrupt service routine, and
606 * next power fault detected interrupt was notified again.
607 */
Rajat Jain4f854f22014-02-04 18:29:23 -0800608
609 /*
610 * Always enable link events: thus link-up and link-down shall
611 * always be treated as hotplug and unplug respectively. Enable
612 * presence detect only if Attention Button is not present.
613 */
614 cmd = PCI_EXP_SLTCTL_DLLSCE;
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700615 if (ATTN_BUTTN(ctrl))
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900616 cmd |= PCI_EXP_SLTCTL_ABPE;
Rajat Jain4f854f22014-02-04 18:29:23 -0800617 else
618 cmd |= PCI_EXP_SLTCTL_PDCE;
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700619 if (MRL_SENS(ctrl))
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900620 cmd |= PCI_EXP_SLTCTL_MRLSCE;
Kenji Kaneshigec27fb8832008-04-25 14:39:05 -0700621 if (!pciehp_poll_mode)
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900622 cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
Kenji Kaneshigec27fb8832008-04-25 14:39:05 -0700623
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900624 mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
625 PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
Rajat Jain4f854f22014-02-04 18:29:23 -0800626 PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
627 PCI_EXP_SLTCTL_DLLSCE);
Kenji Kaneshigec27fb8832008-04-25 14:39:05 -0700628
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700629 pcie_write_cmd(ctrl, cmd, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
Mark Lord08e7a7d2007-11-28 15:11:46 -0800631
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900632static void pcie_disable_notification(struct controller *ctrl)
633{
634 u16 mask;
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700635
Kenji Kaneshige322162a2008-12-19 15:19:02 +0900636 mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
637 PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
Kenji Kaneshigef22daf12009-10-05 17:40:02 +0900638 PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
639 PCI_EXP_SLTCTL_DLLSCE);
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700640 pcie_write_cmd(ctrl, 0, mask);
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900641}
642
Alex Williamson2e35afa2013-08-08 14:09:37 -0600643/*
644 * pciehp has a 1:1 bus:slot relationship so we ultimately want a secondary
Rajat Jain2b3940b2014-02-18 18:53:19 -0800645 * bus reset of the bridge, but at the same time we want to ensure that it is
646 * not seen as a hot-unplug, followed by the hot-plug of the device. Thus,
647 * disable link state notification and presence detection change notification
648 * momentarily, if we see that they could interfere. Also, clear any spurious
Alex Williamson2e35afa2013-08-08 14:09:37 -0600649 * events after.
650 */
651int pciehp_reset_slot(struct slot *slot, int probe)
652{
653 struct controller *ctrl = slot->ctrl;
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600654 struct pci_dev *pdev = ctrl_dev(ctrl);
Rajat Jain06a8d892014-02-04 18:30:40 -0800655 u16 stat_mask = 0, ctrl_mask = 0;
Alex Williamson2e35afa2013-08-08 14:09:37 -0600656
657 if (probe)
658 return 0;
659
Rajat Jain2b3940b2014-02-18 18:53:19 -0800660 if (!ATTN_BUTTN(ctrl)) {
Rajat Jain06a8d892014-02-04 18:30:40 -0800661 ctrl_mask |= PCI_EXP_SLTCTL_PDCE;
662 stat_mask |= PCI_EXP_SLTSTA_PDC;
Alex Williamson2e35afa2013-08-08 14:09:37 -0600663 }
Rajat Jain06a8d892014-02-04 18:30:40 -0800664 ctrl_mask |= PCI_EXP_SLTCTL_DLLSCE;
665 stat_mask |= PCI_EXP_SLTSTA_DLLSC;
666
667 pcie_write_cmd(ctrl, 0, ctrl_mask);
668 if (pciehp_poll_mode)
669 del_timer_sync(&ctrl->poll_timer);
Alex Williamson2e35afa2013-08-08 14:09:37 -0600670
671 pci_reset_bridge_secondary_bus(ctrl->pcie->port);
672
Rajat Jain06a8d892014-02-04 18:30:40 -0800673 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, stat_mask);
674 pcie_write_cmd(ctrl, ctrl_mask, ctrl_mask);
675 if (pciehp_poll_mode)
676 int_poll_timeout(ctrl->poll_timer.data);
Alex Williamson2e35afa2013-08-08 14:09:37 -0600677
678 return 0;
679}
680
Eric W. Biedermandbc7e1e2009-01-28 19:31:18 -0800681int pcie_init_notification(struct controller *ctrl)
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900682{
683 if (pciehp_request_irq(ctrl))
684 return -1;
Bjorn Helgaas6dae6202013-12-14 13:06:16 -0700685 pcie_enable_notification(ctrl);
Eric W. Biedermandbc7e1e2009-01-28 19:31:18 -0800686 ctrl->notification_enabled = 1;
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900687 return 0;
688}
689
690static void pcie_shutdown_notification(struct controller *ctrl)
691{
Eric W. Biedermandbc7e1e2009-01-28 19:31:18 -0800692 if (ctrl->notification_enabled) {
693 pcie_disable_notification(ctrl);
694 pciehp_free_irq(ctrl);
695 ctrl->notification_enabled = 0;
696 }
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900697}
698
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900699static int pcie_init_slot(struct controller *ctrl)
700{
701 struct slot *slot;
702
703 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
704 if (!slot)
705 return -ENOMEM;
706
Kees Cookd8537542013-07-03 15:04:57 -0700707 slot->wq = alloc_workqueue("pciehp-%u", 0, 0, PSN(ctrl));
Yijing Wangc2be6f92013-01-11 10:15:54 +0800708 if (!slot->wq)
709 goto abort;
710
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900711 slot->ctrl = ctrl;
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900712 mutex_init(&slot->lock);
Rajat Jain50b52fd2014-02-04 18:31:11 -0800713 mutex_init(&slot->hotplug_lock);
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900714 INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900715 ctrl->slot = slot;
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900716 return 0;
Yijing Wangc2be6f92013-01-11 10:15:54 +0800717abort:
718 kfree(slot);
719 return -ENOMEM;
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900720}
721
722static void pcie_cleanup_slot(struct controller *ctrl)
723{
Kenji Kaneshige8720d272009-09-15 17:24:46 +0900724 struct slot *slot = ctrl->slot;
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900725 cancel_delayed_work(&slot->work);
Yijing Wangc2be6f92013-01-11 10:15:54 +0800726 destroy_workqueue(slot->wq);
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900727 kfree(slot);
728}
729
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -0700730static inline void dbg_ctrl(struct controller *ctrl)
731{
732 int i;
733 u16 reg16;
Kenji Kaneshige385e2492009-09-15 17:30:14 +0900734 struct pci_dev *pdev = ctrl->pcie->port;
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -0700735
736 if (!pciehp_debug)
737 return;
738
Taku Izumi7f2feec2008-09-05 12:11:26 +0900739 ctrl_info(ctrl, "Hotplug Controller:\n");
740 ctrl_info(ctrl, " Seg/Bus/Dev/Func/IRQ : %s IRQ %d\n",
741 pci_name(pdev), pdev->irq);
742 ctrl_info(ctrl, " Vendor ID : 0x%04x\n", pdev->vendor);
743 ctrl_info(ctrl, " Device ID : 0x%04x\n", pdev->device);
744 ctrl_info(ctrl, " Subsystem ID : 0x%04x\n",
745 pdev->subsystem_device);
746 ctrl_info(ctrl, " Subsystem Vendor ID : 0x%04x\n",
747 pdev->subsystem_vendor);
Kenji Kaneshige1518c172009-11-11 14:34:52 +0900748 ctrl_info(ctrl, " PCIe Cap offset : 0x%02x\n",
749 pci_pcie_cap(pdev));
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -0700750 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
751 if (!pci_resource_len(pdev, i))
752 continue;
Bjorn Helgaase1944c62010-03-16 15:53:08 -0600753 ctrl_info(ctrl, " PCI resource [%d] : %pR\n",
754 i, &pdev->resource[i]);
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -0700755 }
Taku Izumi7f2feec2008-09-05 12:11:26 +0900756 ctrl_info(ctrl, "Slot Capabilities : 0x%08x\n", ctrl->slot_cap);
Kenji Kaneshiged54798f2009-09-15 17:28:53 +0900757 ctrl_info(ctrl, " Physical Slot Number : %d\n", PSN(ctrl));
Taku Izumi7f2feec2008-09-05 12:11:26 +0900758 ctrl_info(ctrl, " Attention Button : %3s\n",
759 ATTN_BUTTN(ctrl) ? "yes" : "no");
760 ctrl_info(ctrl, " Power Controller : %3s\n",
761 POWER_CTRL(ctrl) ? "yes" : "no");
762 ctrl_info(ctrl, " MRL Sensor : %3s\n",
763 MRL_SENS(ctrl) ? "yes" : "no");
764 ctrl_info(ctrl, " Attention Indicator : %3s\n",
765 ATTN_LED(ctrl) ? "yes" : "no");
766 ctrl_info(ctrl, " Power Indicator : %3s\n",
767 PWR_LED(ctrl) ? "yes" : "no");
768 ctrl_info(ctrl, " Hot-Plug Surprise : %3s\n",
769 HP_SUPR_RM(ctrl) ? "yes" : "no");
770 ctrl_info(ctrl, " EMI Present : %3s\n",
771 EMI(ctrl) ? "yes" : "no");
772 ctrl_info(ctrl, " Command Completed : %3s\n",
773 NO_CMD_CMPL(ctrl) ? "no" : "yes");
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600774 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &reg16);
Taku Izumi7f2feec2008-09-05 12:11:26 +0900775 ctrl_info(ctrl, "Slot Status : 0x%04x\n", reg16);
Bjorn Helgaascd84d342013-05-09 11:26:16 -0600776 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &reg16);
Taku Izumi7f2feec2008-09-05 12:11:26 +0900777 ctrl_info(ctrl, "Slot Control : 0x%04x\n", reg16);
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -0700778}
779
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400780#define FLAG(x, y) (((x) & (y)) ? '+' : '-')
Bjorn Helgaasafe24782013-12-14 13:06:36 -0700781
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900782struct controller *pcie_init(struct pcie_device *dev)
Mark Lord08e7a7d2007-11-28 15:11:46 -0800783{
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900784 struct controller *ctrl;
Kenji Kaneshigef18e9622008-10-22 14:31:44 +0900785 u32 slot_cap, link_cap;
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -0700786 struct pci_dev *pdev = dev->port;
Mark Lord08e7a7d2007-11-28 15:11:46 -0800787
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900788 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
789 if (!ctrl) {
Taku Izumi18b341b2008-10-23 11:47:32 +0900790 dev_err(&dev->device, "%s: Out of memory\n", __func__);
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900791 goto abort;
792 }
Kenji Kaneshigef7a10e32008-08-22 17:16:48 +0900793 ctrl->pcie = dev;
Bjorn Helgaas1a84b992013-12-14 13:06:07 -0700794 pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -0700795 ctrl->slot_cap = slot_cap;
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -0700796 mutex_init(&ctrl->ctrl_lock);
797 init_waitqueue_head(&ctrl->queue);
798 dbg_ctrl(ctrl);
Kenji Kaneshige58086392008-05-27 19:04:30 +0900799 /*
800 * Controller doesn't notify of command completion if the "No
801 * Command Completed Support" bit is set in Slot Capability
802 * register or the controller supports none of power
803 * controller, attention led, power led and EMI.
804 */
805 if (NO_CMD_CMPL(ctrl) ||
806 !(POWER_CTRL(ctrl) | ATTN_LED(ctrl) | PWR_LED(ctrl) | EMI(ctrl)))
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400807 ctrl->no_cmd_complete = 1;
Mark Lord08e7a7d2007-11-28 15:11:46 -0800808
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400809 /* Check if Data Link Layer Link Active Reporting is implemented */
810 pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
811 if (link_cap & PCI_EXP_LNKCAP_DLLLARC) {
812 ctrl_dbg(ctrl, "Link Active Reporting supported\n");
813 ctrl->link_active_reporting = 1;
814 }
Kenji Kaneshigef18e9622008-10-22 14:31:44 +0900815
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900816 /* Clear all remaining event bits in Slot Status register */
Bjorn Helgaasdf726482013-12-14 13:06:47 -0700817 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
818 PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
819 PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC |
820 PCI_EXP_SLTSTA_CC);
Mark Lord08e7a7d2007-11-28 15:11:46 -0800821
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700822 /* Disable software notification */
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900823 pcie_disable_notification(ctrl);
Mark Lordecdde932007-11-21 15:07:55 -0800824
Bjorn Helgaasafe24782013-12-14 13:06:36 -0700825 ctrl_info(ctrl, "Slot #%d AttnBtn%c AttnInd%c PwrInd%c PwrCtrl%c MRL%c Interlock%c NoCompl%c LLActRep%c\n",
826 (slot_cap & PCI_EXP_SLTCAP_PSN) >> 19,
827 FLAG(slot_cap, PCI_EXP_SLTCAP_ABP),
828 FLAG(slot_cap, PCI_EXP_SLTCAP_AIP),
829 FLAG(slot_cap, PCI_EXP_SLTCAP_PIP),
830 FLAG(slot_cap, PCI_EXP_SLTCAP_PCP),
831 FLAG(slot_cap, PCI_EXP_SLTCAP_MRLSP),
832 FLAG(slot_cap, PCI_EXP_SLTCAP_EIP),
833 FLAG(slot_cap, PCI_EXP_SLTCAP_NCCS),
834 FLAG(link_cap, PCI_EXP_LNKCAP_DLLLARC));
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -0700835
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900836 if (pcie_init_slot(ctrl))
837 goto abort_ctrl;
Kenji Kaneshige2aeeef12008-04-25 14:39:08 -0700838
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900839 return ctrl;
840
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900841abort_ctrl:
842 kfree(ctrl);
Mark Lord08e7a7d2007-11-28 15:11:46 -0800843abort:
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900844 return NULL;
845}
846
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900847void pciehp_release_ctrl(struct controller *ctrl)
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900848{
849 pcie_shutdown_notification(ctrl);
850 pcie_cleanup_slot(ctrl);
Kenji Kaneshigec4635eb2008-06-20 12:07:08 +0900851 kfree(ctrl);
Mark Lord08e7a7d2007-11-28 15:11:46 -0800852}