pciehp_hpc.c 32 KB

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