pciehp_hpc.c 32 KB

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