pciehp_hpc.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. /*
  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. *
  26. * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
  27. *
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/types.h>
  32. #include <linux/signal.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/timer.h>
  35. #include <linux/pci.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/time.h>
  38. #include "../pci.h"
  39. #include "pciehp.h"
  40. static atomic_t pciehp_num_controllers = ATOMIC_INIT(0);
  41. struct ctrl_reg {
  42. u8 cap_id;
  43. u8 nxt_ptr;
  44. u16 cap_reg;
  45. u32 dev_cap;
  46. u16 dev_ctrl;
  47. u16 dev_status;
  48. u32 lnk_cap;
  49. u16 lnk_ctrl;
  50. u16 lnk_status;
  51. u32 slot_cap;
  52. u16 slot_ctrl;
  53. u16 slot_status;
  54. u16 root_ctrl;
  55. u16 rsvp;
  56. u32 root_status;
  57. } __attribute__ ((packed));
  58. /* offsets to the controller registers based on the above structure layout */
  59. enum ctrl_offsets {
  60. PCIECAPID = offsetof(struct ctrl_reg, cap_id),
  61. NXTCAPPTR = offsetof(struct ctrl_reg, nxt_ptr),
  62. CAPREG = offsetof(struct ctrl_reg, cap_reg),
  63. DEVCAP = offsetof(struct ctrl_reg, dev_cap),
  64. DEVCTRL = offsetof(struct ctrl_reg, dev_ctrl),
  65. DEVSTATUS = offsetof(struct ctrl_reg, dev_status),
  66. LNKCAP = offsetof(struct ctrl_reg, lnk_cap),
  67. LNKCTRL = offsetof(struct ctrl_reg, lnk_ctrl),
  68. LNKSTATUS = offsetof(struct ctrl_reg, lnk_status),
  69. SLOTCAP = offsetof(struct ctrl_reg, slot_cap),
  70. SLOTCTRL = offsetof(struct ctrl_reg, slot_ctrl),
  71. SLOTSTATUS = offsetof(struct ctrl_reg, slot_status),
  72. ROOTCTRL = offsetof(struct ctrl_reg, root_ctrl),
  73. ROOTSTATUS = offsetof(struct ctrl_reg, root_status),
  74. };
  75. static inline int pciehp_readw(struct controller *ctrl, int reg, u16 *value)
  76. {
  77. struct pci_dev *dev = ctrl->pci_dev;
  78. return pci_read_config_word(dev, ctrl->cap_base + reg, value);
  79. }
  80. static inline int pciehp_readl(struct controller *ctrl, int reg, u32 *value)
  81. {
  82. struct pci_dev *dev = ctrl->pci_dev;
  83. return pci_read_config_dword(dev, ctrl->cap_base + reg, value);
  84. }
  85. static inline int pciehp_writew(struct controller *ctrl, int reg, u16 value)
  86. {
  87. struct pci_dev *dev = ctrl->pci_dev;
  88. return pci_write_config_word(dev, ctrl->cap_base + reg, value);
  89. }
  90. static inline int pciehp_writel(struct controller *ctrl, int reg, u32 value)
  91. {
  92. struct pci_dev *dev = ctrl->pci_dev;
  93. return pci_write_config_dword(dev, ctrl->cap_base + reg, value);
  94. }
  95. /* Field definitions in PCI Express Capabilities Register */
  96. #define CAP_VER 0x000F
  97. #define DEV_PORT_TYPE 0x00F0
  98. #define SLOT_IMPL 0x0100
  99. #define MSG_NUM 0x3E00
  100. /* Device or Port Type */
  101. #define NAT_ENDPT 0x00
  102. #define LEG_ENDPT 0x01
  103. #define ROOT_PORT 0x04
  104. #define UP_STREAM 0x05
  105. #define DN_STREAM 0x06
  106. #define PCIE_PCI_BRDG 0x07
  107. #define PCI_PCIE_BRDG 0x10
  108. /* Field definitions in Device Capabilities Register */
  109. #define DATTN_BUTTN_PRSN 0x1000
  110. #define DATTN_LED_PRSN 0x2000
  111. #define DPWR_LED_PRSN 0x4000
  112. /* Field definitions in Link Capabilities Register */
  113. #define MAX_LNK_SPEED 0x000F
  114. #define MAX_LNK_WIDTH 0x03F0
  115. /* Link Width Encoding */
  116. #define LNK_X1 0x01
  117. #define LNK_X2 0x02
  118. #define LNK_X4 0x04
  119. #define LNK_X8 0x08
  120. #define LNK_X12 0x0C
  121. #define LNK_X16 0x10
  122. #define LNK_X32 0x20
  123. /*Field definitions of Link Status Register */
  124. #define LNK_SPEED 0x000F
  125. #define NEG_LINK_WD 0x03F0
  126. #define LNK_TRN_ERR 0x0400
  127. #define LNK_TRN 0x0800
  128. #define SLOT_CLK_CONF 0x1000
  129. /* Field definitions in Slot Capabilities Register */
  130. #define ATTN_BUTTN_PRSN 0x00000001
  131. #define PWR_CTRL_PRSN 0x00000002
  132. #define MRL_SENS_PRSN 0x00000004
  133. #define ATTN_LED_PRSN 0x00000008
  134. #define PWR_LED_PRSN 0x00000010
  135. #define HP_SUPR_RM_SUP 0x00000020
  136. #define HP_CAP 0x00000040
  137. #define SLOT_PWR_VALUE 0x000003F8
  138. #define SLOT_PWR_LIMIT 0x00000C00
  139. #define PSN 0xFFF80000 /* PSN: Physical Slot Number */
  140. /* Field definitions in Slot Control Register */
  141. #define ATTN_BUTTN_ENABLE 0x0001
  142. #define PWR_FAULT_DETECT_ENABLE 0x0002
  143. #define MRL_DETECT_ENABLE 0x0004
  144. #define PRSN_DETECT_ENABLE 0x0008
  145. #define CMD_CMPL_INTR_ENABLE 0x0010
  146. #define HP_INTR_ENABLE 0x0020
  147. #define ATTN_LED_CTRL 0x00C0
  148. #define PWR_LED_CTRL 0x0300
  149. #define PWR_CTRL 0x0400
  150. #define EMI_CTRL 0x0800
  151. /* Attention indicator and Power indicator states */
  152. #define LED_ON 0x01
  153. #define LED_BLINK 0x10
  154. #define LED_OFF 0x11
  155. /* Power Control Command */
  156. #define POWER_ON 0
  157. #define POWER_OFF 0x0400
  158. /* EMI Status defines */
  159. #define EMI_DISENGAGED 0
  160. #define EMI_ENGAGED 1
  161. /* Field definitions in Slot Status Register */
  162. #define ATTN_BUTTN_PRESSED 0x0001
  163. #define PWR_FAULT_DETECTED 0x0002
  164. #define MRL_SENS_CHANGED 0x0004
  165. #define PRSN_DETECT_CHANGED 0x0008
  166. #define CMD_COMPLETED 0x0010
  167. #define MRL_STATE 0x0020
  168. #define PRSN_STATE 0x0040
  169. #define EMI_STATE 0x0080
  170. #define EMI_STATUS_BIT 7
  171. static irqreturn_t pcie_isr(int irq, void *dev_id);
  172. static void start_int_poll_timer(struct controller *ctrl, int sec);
  173. /* This is the interrupt polling timeout function. */
  174. static void int_poll_timeout(unsigned long data)
  175. {
  176. struct controller *ctrl = (struct controller *)data;
  177. /* Poll for interrupt events. regs == NULL => polling */
  178. pcie_isr(0, ctrl);
  179. init_timer(&ctrl->poll_timer);
  180. if (!pciehp_poll_time)
  181. pciehp_poll_time = 2; /* default polling interval is 2 sec */
  182. start_int_poll_timer(ctrl, pciehp_poll_time);
  183. }
  184. /* This function starts the interrupt polling timer. */
  185. static void start_int_poll_timer(struct controller *ctrl, int sec)
  186. {
  187. /* Clamp to sane value */
  188. if ((sec <= 0) || (sec > 60))
  189. sec = 2;
  190. ctrl->poll_timer.function = &int_poll_timeout;
  191. ctrl->poll_timer.data = (unsigned long)ctrl;
  192. ctrl->poll_timer.expires = jiffies + sec * HZ;
  193. add_timer(&ctrl->poll_timer);
  194. }
  195. static inline int pcie_wait_cmd(struct controller *ctrl)
  196. {
  197. int retval = 0;
  198. unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
  199. unsigned long timeout = msecs_to_jiffies(msecs);
  200. int rc;
  201. rc = wait_event_interruptible_timeout(ctrl->queue,
  202. !ctrl->cmd_busy, timeout);
  203. if (!rc)
  204. dbg("Command not completed in 1000 msec\n");
  205. else if (rc < 0) {
  206. retval = -EINTR;
  207. info("Command was interrupted by a signal\n");
  208. }
  209. return retval;
  210. }
  211. /**
  212. * pcie_write_cmd - Issue controller command
  213. * @ctrl: controller to which the command is issued
  214. * @cmd: command value written to slot control register
  215. * @mask: bitmask of slot control register to be modified
  216. */
  217. static int pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
  218. {
  219. int retval = 0;
  220. u16 slot_status;
  221. u16 slot_ctrl;
  222. mutex_lock(&ctrl->ctrl_lock);
  223. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  224. if (retval) {
  225. err("%s: Cannot read SLOTSTATUS register\n", __func__);
  226. goto out;
  227. }
  228. if ((slot_status & CMD_COMPLETED) == CMD_COMPLETED ) {
  229. /* After 1 sec and CMD_COMPLETED still not set, just
  230. proceed forward to issue the next command according
  231. to spec. Just print out the error message */
  232. dbg("%s: CMD_COMPLETED not clear after 1 sec.\n",
  233. __func__);
  234. }
  235. retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
  236. if (retval) {
  237. err("%s: Cannot read SLOTCTRL register\n", __func__);
  238. goto out;
  239. }
  240. slot_ctrl &= ~mask;
  241. slot_ctrl |= ((cmd & mask) | CMD_CMPL_INTR_ENABLE);
  242. ctrl->cmd_busy = 1;
  243. smp_mb();
  244. retval = pciehp_writew(ctrl, SLOTCTRL, slot_ctrl);
  245. if (retval)
  246. err("%s: Cannot write to SLOTCTRL register\n", __func__);
  247. /*
  248. * Wait for command completion.
  249. */
  250. if (!retval)
  251. retval = pcie_wait_cmd(ctrl);
  252. out:
  253. mutex_unlock(&ctrl->ctrl_lock);
  254. return retval;
  255. }
  256. static int hpc_check_lnk_status(struct controller *ctrl)
  257. {
  258. u16 lnk_status;
  259. int retval = 0;
  260. retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
  261. if (retval) {
  262. err("%s: Cannot read LNKSTATUS register\n", __func__);
  263. return retval;
  264. }
  265. dbg("%s: lnk_status = %x\n", __func__, lnk_status);
  266. if ( (lnk_status & LNK_TRN) || (lnk_status & LNK_TRN_ERR) ||
  267. !(lnk_status & NEG_LINK_WD)) {
  268. err("%s : Link Training Error occurs \n", __func__);
  269. retval = -1;
  270. return retval;
  271. }
  272. return retval;
  273. }
  274. static int hpc_get_attention_status(struct slot *slot, u8 *status)
  275. {
  276. struct controller *ctrl = slot->ctrl;
  277. u16 slot_ctrl;
  278. u8 atten_led_state;
  279. int retval = 0;
  280. retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
  281. if (retval) {
  282. err("%s: Cannot read SLOTCTRL register\n", __func__);
  283. return retval;
  284. }
  285. dbg("%s: SLOTCTRL %x, value read %x\n",
  286. __func__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
  287. atten_led_state = (slot_ctrl & ATTN_LED_CTRL) >> 6;
  288. switch (atten_led_state) {
  289. case 0:
  290. *status = 0xFF; /* Reserved */
  291. break;
  292. case 1:
  293. *status = 1; /* On */
  294. break;
  295. case 2:
  296. *status = 2; /* Blink */
  297. break;
  298. case 3:
  299. *status = 0; /* Off */
  300. break;
  301. default:
  302. *status = 0xFF;
  303. break;
  304. }
  305. return 0;
  306. }
  307. static int hpc_get_power_status(struct slot *slot, u8 *status)
  308. {
  309. struct controller *ctrl = slot->ctrl;
  310. u16 slot_ctrl;
  311. u8 pwr_state;
  312. int retval = 0;
  313. retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
  314. if (retval) {
  315. err("%s: Cannot read SLOTCTRL register\n", __func__);
  316. return retval;
  317. }
  318. dbg("%s: SLOTCTRL %x value read %x\n",
  319. __func__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
  320. pwr_state = (slot_ctrl & PWR_CTRL) >> 10;
  321. switch (pwr_state) {
  322. case 0:
  323. *status = 1;
  324. break;
  325. case 1:
  326. *status = 0;
  327. break;
  328. default:
  329. *status = 0xFF;
  330. break;
  331. }
  332. return retval;
  333. }
  334. static int hpc_get_latch_status(struct slot *slot, u8 *status)
  335. {
  336. struct controller *ctrl = slot->ctrl;
  337. u16 slot_status;
  338. int retval = 0;
  339. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  340. if (retval) {
  341. err("%s: Cannot read SLOTSTATUS register\n", __func__);
  342. return retval;
  343. }
  344. *status = (((slot_status & MRL_STATE) >> 5) == 0) ? 0 : 1;
  345. return 0;
  346. }
  347. static int hpc_get_adapter_status(struct slot *slot, u8 *status)
  348. {
  349. struct controller *ctrl = slot->ctrl;
  350. u16 slot_status;
  351. u8 card_state;
  352. int retval = 0;
  353. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  354. if (retval) {
  355. err("%s: Cannot read SLOTSTATUS register\n", __func__);
  356. return retval;
  357. }
  358. card_state = (u8)((slot_status & PRSN_STATE) >> 6);
  359. *status = (card_state == 1) ? 1 : 0;
  360. return 0;
  361. }
  362. static int hpc_query_power_fault(struct slot *slot)
  363. {
  364. struct controller *ctrl = slot->ctrl;
  365. u16 slot_status;
  366. u8 pwr_fault;
  367. int retval = 0;
  368. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  369. if (retval) {
  370. err("%s: Cannot check for power fault\n", __func__);
  371. return retval;
  372. }
  373. pwr_fault = (u8)((slot_status & PWR_FAULT_DETECTED) >> 1);
  374. return pwr_fault;
  375. }
  376. static int hpc_get_emi_status(struct slot *slot, u8 *status)
  377. {
  378. struct controller *ctrl = slot->ctrl;
  379. u16 slot_status;
  380. int retval = 0;
  381. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  382. if (retval) {
  383. err("%s : Cannot check EMI status\n", __func__);
  384. return retval;
  385. }
  386. *status = (slot_status & EMI_STATE) >> EMI_STATUS_BIT;
  387. return retval;
  388. }
  389. static int hpc_toggle_emi(struct slot *slot)
  390. {
  391. u16 slot_cmd;
  392. u16 cmd_mask;
  393. int rc;
  394. slot_cmd = EMI_CTRL;
  395. cmd_mask = EMI_CTRL;
  396. if (!pciehp_poll_mode) {
  397. slot_cmd = slot_cmd | HP_INTR_ENABLE;
  398. cmd_mask = cmd_mask | HP_INTR_ENABLE;
  399. }
  400. rc = pcie_write_cmd(slot->ctrl, slot_cmd, cmd_mask);
  401. slot->last_emi_toggle = get_seconds();
  402. return rc;
  403. }
  404. static int hpc_set_attention_status(struct slot *slot, u8 value)
  405. {
  406. struct controller *ctrl = slot->ctrl;
  407. u16 slot_cmd;
  408. u16 cmd_mask;
  409. int rc;
  410. cmd_mask = ATTN_LED_CTRL;
  411. switch (value) {
  412. case 0 : /* turn off */
  413. slot_cmd = 0x00C0;
  414. break;
  415. case 1: /* turn on */
  416. slot_cmd = 0x0040;
  417. break;
  418. case 2: /* turn blink */
  419. slot_cmd = 0x0080;
  420. break;
  421. default:
  422. return -1;
  423. }
  424. if (!pciehp_poll_mode) {
  425. slot_cmd = slot_cmd | HP_INTR_ENABLE;
  426. cmd_mask = cmd_mask | HP_INTR_ENABLE;
  427. }
  428. rc = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  429. dbg("%s: SLOTCTRL %x write cmd %x\n",
  430. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  431. return rc;
  432. }
  433. static void hpc_set_green_led_on(struct slot *slot)
  434. {
  435. struct controller *ctrl = slot->ctrl;
  436. u16 slot_cmd;
  437. u16 cmd_mask;
  438. slot_cmd = 0x0100;
  439. cmd_mask = PWR_LED_CTRL;
  440. if (!pciehp_poll_mode) {
  441. slot_cmd = slot_cmd | HP_INTR_ENABLE;
  442. cmd_mask = cmd_mask | HP_INTR_ENABLE;
  443. }
  444. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  445. dbg("%s: SLOTCTRL %x write cmd %x\n",
  446. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  447. }
  448. static void hpc_set_green_led_off(struct slot *slot)
  449. {
  450. struct controller *ctrl = slot->ctrl;
  451. u16 slot_cmd;
  452. u16 cmd_mask;
  453. slot_cmd = 0x0300;
  454. cmd_mask = PWR_LED_CTRL;
  455. if (!pciehp_poll_mode) {
  456. slot_cmd = slot_cmd | HP_INTR_ENABLE;
  457. cmd_mask = cmd_mask | HP_INTR_ENABLE;
  458. }
  459. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  460. dbg("%s: SLOTCTRL %x write cmd %x\n",
  461. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  462. }
  463. static void hpc_set_green_led_blink(struct slot *slot)
  464. {
  465. struct controller *ctrl = slot->ctrl;
  466. u16 slot_cmd;
  467. u16 cmd_mask;
  468. slot_cmd = 0x0200;
  469. cmd_mask = PWR_LED_CTRL;
  470. if (!pciehp_poll_mode) {
  471. slot_cmd = slot_cmd | HP_INTR_ENABLE;
  472. cmd_mask = cmd_mask | HP_INTR_ENABLE;
  473. }
  474. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  475. dbg("%s: SLOTCTRL %x write cmd %x\n",
  476. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  477. }
  478. static void hpc_release_ctlr(struct controller *ctrl)
  479. {
  480. if (pciehp_poll_mode)
  481. del_timer(&ctrl->poll_timer);
  482. else
  483. free_irq(ctrl->pci_dev->irq, ctrl);
  484. /*
  485. * If this is the last controller to be released, destroy the
  486. * pciehp work queue
  487. */
  488. if (atomic_dec_and_test(&pciehp_num_controllers))
  489. destroy_workqueue(pciehp_wq);
  490. }
  491. static int hpc_power_on_slot(struct slot * slot)
  492. {
  493. struct controller *ctrl = slot->ctrl;
  494. u16 slot_cmd;
  495. u16 cmd_mask;
  496. u16 slot_status;
  497. int retval = 0;
  498. dbg("%s: slot->hp_slot %x\n", __func__, slot->hp_slot);
  499. /* Clear sticky power-fault bit from previous power failures */
  500. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  501. if (retval) {
  502. err("%s: Cannot read SLOTSTATUS register\n", __func__);
  503. return retval;
  504. }
  505. slot_status &= PWR_FAULT_DETECTED;
  506. if (slot_status) {
  507. retval = pciehp_writew(ctrl, SLOTSTATUS, slot_status);
  508. if (retval) {
  509. err("%s: Cannot write to SLOTSTATUS register\n",
  510. __func__);
  511. return retval;
  512. }
  513. }
  514. slot_cmd = POWER_ON;
  515. cmd_mask = PWR_CTRL;
  516. /* Enable detection that we turned off at slot power-off time */
  517. if (!pciehp_poll_mode) {
  518. slot_cmd = slot_cmd |
  519. PWR_FAULT_DETECT_ENABLE |
  520. MRL_DETECT_ENABLE |
  521. PRSN_DETECT_ENABLE |
  522. HP_INTR_ENABLE;
  523. cmd_mask = cmd_mask |
  524. PWR_FAULT_DETECT_ENABLE |
  525. MRL_DETECT_ENABLE |
  526. PRSN_DETECT_ENABLE |
  527. HP_INTR_ENABLE;
  528. }
  529. retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  530. if (retval) {
  531. err("%s: Write %x command failed!\n", __func__, slot_cmd);
  532. return -1;
  533. }
  534. dbg("%s: SLOTCTRL %x write cmd %x\n",
  535. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  536. return retval;
  537. }
  538. static inline int pcie_mask_bad_dllp(struct controller *ctrl)
  539. {
  540. struct pci_dev *dev = ctrl->pci_dev;
  541. int pos;
  542. u32 reg;
  543. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  544. if (!pos)
  545. return 0;
  546. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg);
  547. if (reg & PCI_ERR_COR_BAD_DLLP)
  548. return 0;
  549. reg |= PCI_ERR_COR_BAD_DLLP;
  550. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg);
  551. return 1;
  552. }
  553. static inline void pcie_unmask_bad_dllp(struct controller *ctrl)
  554. {
  555. struct pci_dev *dev = ctrl->pci_dev;
  556. u32 reg;
  557. int pos;
  558. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  559. if (!pos)
  560. return;
  561. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg);
  562. if (!(reg & PCI_ERR_COR_BAD_DLLP))
  563. return;
  564. reg &= ~PCI_ERR_COR_BAD_DLLP;
  565. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg);
  566. }
  567. static int hpc_power_off_slot(struct slot * slot)
  568. {
  569. struct controller *ctrl = slot->ctrl;
  570. u16 slot_cmd;
  571. u16 cmd_mask;
  572. int retval = 0;
  573. int changed;
  574. dbg("%s: slot->hp_slot %x\n", __func__, slot->hp_slot);
  575. /*
  576. * Set Bad DLLP Mask bit in Correctable Error Mask
  577. * Register. This is the workaround against Bad DLLP error
  578. * that sometimes happens during turning power off the slot
  579. * which conforms to PCI Express 1.0a spec.
  580. */
  581. changed = pcie_mask_bad_dllp(ctrl);
  582. slot_cmd = POWER_OFF;
  583. cmd_mask = PWR_CTRL;
  584. /*
  585. * If we get MRL or presence detect interrupts now, the isr
  586. * will notice the sticky power-fault bit too and issue power
  587. * indicator change commands. This will lead to an endless loop
  588. * of command completions, since the power-fault bit remains on
  589. * till the slot is powered on again.
  590. */
  591. if (!pciehp_poll_mode) {
  592. slot_cmd = (slot_cmd &
  593. ~PWR_FAULT_DETECT_ENABLE &
  594. ~MRL_DETECT_ENABLE &
  595. ~PRSN_DETECT_ENABLE) | HP_INTR_ENABLE;
  596. cmd_mask = cmd_mask |
  597. PWR_FAULT_DETECT_ENABLE |
  598. MRL_DETECT_ENABLE |
  599. PRSN_DETECT_ENABLE |
  600. HP_INTR_ENABLE;
  601. }
  602. retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  603. if (retval) {
  604. err("%s: Write command failed!\n", __func__);
  605. retval = -1;
  606. goto out;
  607. }
  608. dbg("%s: SLOTCTRL %x write cmd %x\n",
  609. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  610. /*
  611. * After turning power off, we must wait for at least 1 second
  612. * before taking any action that relies on power having been
  613. * removed from the slot/adapter.
  614. */
  615. msleep(1000);
  616. out:
  617. if (changed)
  618. pcie_unmask_bad_dllp(ctrl);
  619. return retval;
  620. }
  621. static irqreturn_t pcie_isr(int irq, void *dev_id)
  622. {
  623. struct controller *ctrl = (struct controller *)dev_id;
  624. u16 detected, intr_loc;
  625. /*
  626. * In order to guarantee that all interrupt events are
  627. * serviced, we need to re-inspect Slot Status register after
  628. * clearing what is presumed to be the last pending interrupt.
  629. */
  630. intr_loc = 0;
  631. do {
  632. if (pciehp_readw(ctrl, SLOTSTATUS, &detected)) {
  633. err("%s: Cannot read SLOTSTATUS\n", __func__);
  634. return IRQ_NONE;
  635. }
  636. detected &= (ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED |
  637. MRL_SENS_CHANGED | PRSN_DETECT_CHANGED |
  638. CMD_COMPLETED);
  639. intr_loc |= detected;
  640. if (!intr_loc)
  641. return IRQ_NONE;
  642. if (pciehp_writew(ctrl, SLOTSTATUS, detected)) {
  643. err("%s: Cannot write to SLOTSTATUS\n", __func__);
  644. return IRQ_NONE;
  645. }
  646. } while (detected);
  647. dbg("%s: intr_loc %x\n", __FUNCTION__, intr_loc);
  648. /* Check Command Complete Interrupt Pending */
  649. if (intr_loc & CMD_COMPLETED) {
  650. ctrl->cmd_busy = 0;
  651. smp_mb();
  652. wake_up_interruptible(&ctrl->queue);
  653. }
  654. /* Check MRL Sensor Changed */
  655. if (intr_loc & MRL_SENS_CHANGED)
  656. pciehp_handle_switch_change(0, ctrl);
  657. /* Check Attention Button Pressed */
  658. if (intr_loc & ATTN_BUTTN_PRESSED)
  659. pciehp_handle_attention_button(0, ctrl);
  660. /* Check Presence Detect Changed */
  661. if (intr_loc & PRSN_DETECT_CHANGED)
  662. pciehp_handle_presence_change(0, ctrl);
  663. /* Check Power Fault Detected */
  664. if (intr_loc & PWR_FAULT_DETECTED)
  665. pciehp_handle_power_fault(0, ctrl);
  666. return IRQ_HANDLED;
  667. }
  668. static int hpc_get_max_lnk_speed(struct slot *slot, enum pci_bus_speed *value)
  669. {
  670. struct controller *ctrl = slot->ctrl;
  671. enum pcie_link_speed lnk_speed;
  672. u32 lnk_cap;
  673. int retval = 0;
  674. retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
  675. if (retval) {
  676. err("%s: Cannot read LNKCAP register\n", __func__);
  677. return retval;
  678. }
  679. switch (lnk_cap & 0x000F) {
  680. case 1:
  681. lnk_speed = PCIE_2PT5GB;
  682. break;
  683. default:
  684. lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
  685. break;
  686. }
  687. *value = lnk_speed;
  688. dbg("Max link speed = %d\n", lnk_speed);
  689. return retval;
  690. }
  691. static int hpc_get_max_lnk_width(struct slot *slot,
  692. enum pcie_link_width *value)
  693. {
  694. struct controller *ctrl = slot->ctrl;
  695. enum pcie_link_width lnk_wdth;
  696. u32 lnk_cap;
  697. int retval = 0;
  698. retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
  699. if (retval) {
  700. err("%s: Cannot read LNKCAP register\n", __func__);
  701. return retval;
  702. }
  703. switch ((lnk_cap & 0x03F0) >> 4){
  704. case 0:
  705. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  706. break;
  707. case 1:
  708. lnk_wdth = PCIE_LNK_X1;
  709. break;
  710. case 2:
  711. lnk_wdth = PCIE_LNK_X2;
  712. break;
  713. case 4:
  714. lnk_wdth = PCIE_LNK_X4;
  715. break;
  716. case 8:
  717. lnk_wdth = PCIE_LNK_X8;
  718. break;
  719. case 12:
  720. lnk_wdth = PCIE_LNK_X12;
  721. break;
  722. case 16:
  723. lnk_wdth = PCIE_LNK_X16;
  724. break;
  725. case 32:
  726. lnk_wdth = PCIE_LNK_X32;
  727. break;
  728. default:
  729. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  730. break;
  731. }
  732. *value = lnk_wdth;
  733. dbg("Max link width = %d\n", lnk_wdth);
  734. return retval;
  735. }
  736. static int hpc_get_cur_lnk_speed(struct slot *slot, enum pci_bus_speed *value)
  737. {
  738. struct controller *ctrl = slot->ctrl;
  739. enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
  740. int retval = 0;
  741. u16 lnk_status;
  742. retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
  743. if (retval) {
  744. err("%s: Cannot read LNKSTATUS register\n", __func__);
  745. return retval;
  746. }
  747. switch (lnk_status & 0x0F) {
  748. case 1:
  749. lnk_speed = PCIE_2PT5GB;
  750. break;
  751. default:
  752. lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
  753. break;
  754. }
  755. *value = lnk_speed;
  756. dbg("Current link speed = %d\n", lnk_speed);
  757. return retval;
  758. }
  759. static int hpc_get_cur_lnk_width(struct slot *slot,
  760. enum pcie_link_width *value)
  761. {
  762. struct controller *ctrl = slot->ctrl;
  763. enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  764. int retval = 0;
  765. u16 lnk_status;
  766. retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
  767. if (retval) {
  768. err("%s: Cannot read LNKSTATUS register\n", __func__);
  769. return retval;
  770. }
  771. switch ((lnk_status & 0x03F0) >> 4){
  772. case 0:
  773. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  774. break;
  775. case 1:
  776. lnk_wdth = PCIE_LNK_X1;
  777. break;
  778. case 2:
  779. lnk_wdth = PCIE_LNK_X2;
  780. break;
  781. case 4:
  782. lnk_wdth = PCIE_LNK_X4;
  783. break;
  784. case 8:
  785. lnk_wdth = PCIE_LNK_X8;
  786. break;
  787. case 12:
  788. lnk_wdth = PCIE_LNK_X12;
  789. break;
  790. case 16:
  791. lnk_wdth = PCIE_LNK_X16;
  792. break;
  793. case 32:
  794. lnk_wdth = PCIE_LNK_X32;
  795. break;
  796. default:
  797. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  798. break;
  799. }
  800. *value = lnk_wdth;
  801. dbg("Current link width = %d\n", lnk_wdth);
  802. return retval;
  803. }
  804. static struct hpc_ops pciehp_hpc_ops = {
  805. .power_on_slot = hpc_power_on_slot,
  806. .power_off_slot = hpc_power_off_slot,
  807. .set_attention_status = hpc_set_attention_status,
  808. .get_power_status = hpc_get_power_status,
  809. .get_attention_status = hpc_get_attention_status,
  810. .get_latch_status = hpc_get_latch_status,
  811. .get_adapter_status = hpc_get_adapter_status,
  812. .get_emi_status = hpc_get_emi_status,
  813. .toggle_emi = hpc_toggle_emi,
  814. .get_max_bus_speed = hpc_get_max_lnk_speed,
  815. .get_cur_bus_speed = hpc_get_cur_lnk_speed,
  816. .get_max_lnk_width = hpc_get_max_lnk_width,
  817. .get_cur_lnk_width = hpc_get_cur_lnk_width,
  818. .query_power_fault = hpc_query_power_fault,
  819. .green_led_on = hpc_set_green_led_on,
  820. .green_led_off = hpc_set_green_led_off,
  821. .green_led_blink = hpc_set_green_led_blink,
  822. .release_ctlr = hpc_release_ctlr,
  823. .check_lnk_status = hpc_check_lnk_status,
  824. };
  825. #ifdef CONFIG_ACPI
  826. int pciehp_acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev)
  827. {
  828. acpi_status status;
  829. acpi_handle chandle, handle = DEVICE_ACPI_HANDLE(&(dev->dev));
  830. struct pci_dev *pdev = dev;
  831. struct pci_bus *parent;
  832. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  833. /*
  834. * Per PCI firmware specification, we should run the ACPI _OSC
  835. * method to get control of hotplug hardware before using it.
  836. * If an _OSC is missing, we look for an OSHP to do the same thing.
  837. * To handle different BIOS behavior, we look for _OSC and OSHP
  838. * within the scope of the hotplug controller and its parents, upto
  839. * the host bridge under which this controller exists.
  840. */
  841. while (!handle) {
  842. /*
  843. * This hotplug controller was not listed in the ACPI name
  844. * space at all. Try to get acpi handle of parent pci bus.
  845. */
  846. if (!pdev || !pdev->bus->parent)
  847. break;
  848. parent = pdev->bus->parent;
  849. dbg("Could not find %s in acpi namespace, trying parent\n",
  850. pci_name(pdev));
  851. if (!parent->self)
  852. /* Parent must be a host bridge */
  853. handle = acpi_get_pci_rootbridge_handle(
  854. pci_domain_nr(parent),
  855. parent->number);
  856. else
  857. handle = DEVICE_ACPI_HANDLE(
  858. &(parent->self->dev));
  859. pdev = parent->self;
  860. }
  861. while (handle) {
  862. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  863. dbg("Trying to get hotplug control for %s \n",
  864. (char *)string.pointer);
  865. status = pci_osc_control_set(handle,
  866. OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL |
  867. OSC_PCI_EXPRESS_NATIVE_HP_CONTROL);
  868. if (status == AE_NOT_FOUND)
  869. status = acpi_run_oshp(handle);
  870. if (ACPI_SUCCESS(status)) {
  871. dbg("Gained control for hotplug HW for pci %s (%s)\n",
  872. pci_name(dev), (char *)string.pointer);
  873. kfree(string.pointer);
  874. return 0;
  875. }
  876. if (acpi_root_bridge(handle))
  877. break;
  878. chandle = handle;
  879. status = acpi_get_parent(chandle, &handle);
  880. if (ACPI_FAILURE(status))
  881. break;
  882. }
  883. err("Cannot get control of hotplug hardware for pci %s\n",
  884. pci_name(dev));
  885. kfree(string.pointer);
  886. return -1;
  887. }
  888. #endif
  889. static int pcie_init_hardware_part1(struct controller *ctrl,
  890. struct pcie_device *dev)
  891. {
  892. /* Mask Hot-plug Interrupt Enable */
  893. if (pcie_write_cmd(ctrl, 0, HP_INTR_ENABLE | CMD_CMPL_INTR_ENABLE)) {
  894. err("%s: Cannot mask hotplug interrupt enable\n", __func__);
  895. return -1;
  896. }
  897. return 0;
  898. }
  899. int pcie_init_hardware_part2(struct controller *ctrl, struct pcie_device *dev)
  900. {
  901. u16 cmd, mask;
  902. /*
  903. * We need to clear all events before enabling hotplug interrupt
  904. * notification mechanism in order for hotplug controler to
  905. * generate interrupts.
  906. */
  907. if (pciehp_writew(ctrl, SLOTSTATUS, 0x1f)) {
  908. err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
  909. return -1;
  910. }
  911. cmd = PRSN_DETECT_ENABLE;
  912. if (ATTN_BUTTN(ctrl->ctrlcap))
  913. cmd |= ATTN_BUTTN_ENABLE;
  914. if (POWER_CTRL(ctrl->ctrlcap))
  915. cmd |= PWR_FAULT_DETECT_ENABLE;
  916. if (MRL_SENS(ctrl->ctrlcap))
  917. cmd |= MRL_DETECT_ENABLE;
  918. if (!pciehp_poll_mode)
  919. cmd |= HP_INTR_ENABLE;
  920. mask = PRSN_DETECT_ENABLE | ATTN_BUTTN_ENABLE |
  921. PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE | HP_INTR_ENABLE;
  922. if (pcie_write_cmd(ctrl, cmd, mask)) {
  923. err("%s: Cannot enable software notification\n", __func__);
  924. goto abort;
  925. }
  926. if (pciehp_force)
  927. dbg("Bypassing BIOS check for pciehp use on %s\n",
  928. pci_name(ctrl->pci_dev));
  929. else if (pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev))
  930. goto abort_disable_intr;
  931. return 0;
  932. /* We end up here for the many possible ways to fail this API. */
  933. abort_disable_intr:
  934. if (pcie_write_cmd(ctrl, 0, HP_INTR_ENABLE))
  935. err("%s : disabling interrupts failed\n", __func__);
  936. abort:
  937. return -1;
  938. }
  939. int pcie_init(struct controller *ctrl, struct pcie_device *dev)
  940. {
  941. int rc;
  942. u16 cap_reg;
  943. u32 slot_cap;
  944. int cap_base;
  945. u16 slot_status, slot_ctrl;
  946. struct pci_dev *pdev;
  947. pdev = dev->port;
  948. ctrl->pci_dev = pdev; /* save pci_dev in context */
  949. dbg("%s: hotplug controller vendor id 0x%x device id 0x%x\n",
  950. __func__, pdev->vendor, pdev->device);
  951. cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  952. if (cap_base == 0) {
  953. dbg("%s: Can't find PCI_CAP_ID_EXP (0x10)\n", __func__);
  954. goto abort;
  955. }
  956. ctrl->cap_base = cap_base;
  957. dbg("%s: pcie_cap_base %x\n", __func__, cap_base);
  958. rc = pciehp_readw(ctrl, CAPREG, &cap_reg);
  959. if (rc) {
  960. err("%s: Cannot read CAPREG register\n", __func__);
  961. goto abort;
  962. }
  963. dbg("%s: CAPREG offset %x cap_reg %x\n",
  964. __func__, ctrl->cap_base + CAPREG, cap_reg);
  965. if (((cap_reg & SLOT_IMPL) == 0) ||
  966. (((cap_reg & DEV_PORT_TYPE) != 0x0040)
  967. && ((cap_reg & DEV_PORT_TYPE) != 0x0060))) {
  968. dbg("%s : This is not a root port or the port is not "
  969. "connected to a slot\n", __func__);
  970. goto abort;
  971. }
  972. rc = pciehp_readl(ctrl, SLOTCAP, &slot_cap);
  973. if (rc) {
  974. err("%s: Cannot read SLOTCAP register\n", __func__);
  975. goto abort;
  976. }
  977. dbg("%s: SLOTCAP offset %x slot_cap %x\n",
  978. __func__, ctrl->cap_base + SLOTCAP, slot_cap);
  979. if (!(slot_cap & HP_CAP)) {
  980. dbg("%s : This slot is not hot-plug capable\n", __func__);
  981. goto abort;
  982. }
  983. /* For debugging purpose */
  984. rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  985. if (rc) {
  986. err("%s: Cannot read SLOTSTATUS register\n", __func__);
  987. goto abort;
  988. }
  989. dbg("%s: SLOTSTATUS offset %x slot_status %x\n",
  990. __func__, ctrl->cap_base + SLOTSTATUS, slot_status);
  991. rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
  992. if (rc) {
  993. err("%s: Cannot read SLOTCTRL register\n", __func__);
  994. goto abort;
  995. }
  996. dbg("%s: SLOTCTRL offset %x slot_ctrl %x\n",
  997. __func__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
  998. for (rc = 0; rc < DEVICE_COUNT_RESOURCE; rc++)
  999. if (pci_resource_len(pdev, rc) > 0)
  1000. dbg("pci resource[%d] start=0x%llx(len=0x%llx)\n", rc,
  1001. (unsigned long long)pci_resource_start(pdev, rc),
  1002. (unsigned long long)pci_resource_len(pdev, rc));
  1003. info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n",
  1004. pdev->vendor, pdev->device,
  1005. pdev->subsystem_vendor, pdev->subsystem_device);
  1006. mutex_init(&ctrl->crit_sect);
  1007. mutex_init(&ctrl->ctrl_lock);
  1008. /* setup wait queue */
  1009. init_waitqueue_head(&ctrl->queue);
  1010. /* return PCI Controller Info */
  1011. ctrl->slot_device_offset = 0;
  1012. ctrl->num_slots = 1;
  1013. ctrl->first_slot = slot_cap >> 19;
  1014. ctrl->ctrlcap = slot_cap & 0x0000007f;
  1015. rc = pcie_init_hardware_part1(ctrl, dev);
  1016. if (rc)
  1017. goto abort;
  1018. if (pciehp_poll_mode) {
  1019. /* Install interrupt polling timer. Start with 10 sec delay */
  1020. init_timer(&ctrl->poll_timer);
  1021. start_int_poll_timer(ctrl, 10);
  1022. } else {
  1023. /* Installs the interrupt handler */
  1024. rc = request_irq(ctrl->pci_dev->irq, pcie_isr, IRQF_SHARED,
  1025. MY_NAME, (void *)ctrl);
  1026. dbg("%s: request_irq %d for hpc%d (returns %d)\n",
  1027. __func__, ctrl->pci_dev->irq,
  1028. atomic_read(&pciehp_num_controllers), rc);
  1029. if (rc) {
  1030. err("Can't get irq %d for the hotplug controller\n",
  1031. ctrl->pci_dev->irq);
  1032. goto abort;
  1033. }
  1034. }
  1035. dbg("pciehp ctrl b:d:f:irq=0x%x:%x:%x:%x\n", pdev->bus->number,
  1036. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), dev->irq);
  1037. /*
  1038. * If this is the first controller to be initialized,
  1039. * initialize the pciehp work queue
  1040. */
  1041. if (atomic_add_return(1, &pciehp_num_controllers) == 1) {
  1042. pciehp_wq = create_singlethread_workqueue("pciehpd");
  1043. if (!pciehp_wq) {
  1044. rc = -ENOMEM;
  1045. goto abort_free_irq;
  1046. }
  1047. }
  1048. rc = pcie_init_hardware_part2(ctrl, dev);
  1049. if (rc == 0) {
  1050. ctrl->hpc_ops = &pciehp_hpc_ops;
  1051. return 0;
  1052. }
  1053. abort_free_irq:
  1054. if (pciehp_poll_mode)
  1055. del_timer_sync(&ctrl->poll_timer);
  1056. else
  1057. free_irq(ctrl->pci_dev->irq, ctrl);
  1058. abort:
  1059. return -1;
  1060. }