pciehp_hpc.c 34 KB

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