pciehp_hpc.c 34 KB

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