pciehp_hpc.c 34 KB

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