pciehp_hpc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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. * @ctrl: controller 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 controller *ctrl, u16 cmd, u16 mask)
  218. {
  219. int retval = 0;
  220. u16 slot_status;
  221. u16 slot_ctrl;
  222. mutex_lock(&ctrl->ctrl_lock);
  223. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  224. if (retval) {
  225. err("%s: Cannot read SLOTSTATUS register\n", __func__);
  226. goto out;
  227. }
  228. if ((slot_status & CMD_COMPLETED) == CMD_COMPLETED ) {
  229. /* After 1 sec and CMD_COMPLETED still not set, just
  230. proceed forward to issue the next command according
  231. to spec. Just print out the error message */
  232. dbg("%s: CMD_COMPLETED not clear after 1 sec.\n",
  233. __func__);
  234. }
  235. retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
  236. if (retval) {
  237. err("%s: Cannot read SLOTCTRL register\n", __func__);
  238. goto out;
  239. }
  240. slot_ctrl &= ~mask;
  241. slot_ctrl |= ((cmd & mask) | CMD_CMPL_INTR_ENABLE);
  242. ctrl->cmd_busy = 1;
  243. smp_mb();
  244. retval = pciehp_writew(ctrl, SLOTCTRL, slot_ctrl);
  245. if (retval)
  246. err("%s: Cannot write to SLOTCTRL register\n", __func__);
  247. /*
  248. * Wait for command completion.
  249. */
  250. if (!retval)
  251. retval = pcie_wait_cmd(ctrl);
  252. out:
  253. mutex_unlock(&ctrl->ctrl_lock);
  254. return retval;
  255. }
  256. static int hpc_check_lnk_status(struct controller *ctrl)
  257. {
  258. u16 lnk_status;
  259. int retval = 0;
  260. retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
  261. if (retval) {
  262. err("%s: Cannot read LNKSTATUS register\n", __func__);
  263. return retval;
  264. }
  265. dbg("%s: lnk_status = %x\n", __func__, lnk_status);
  266. if ( (lnk_status & LNK_TRN) || (lnk_status & LNK_TRN_ERR) ||
  267. !(lnk_status & NEG_LINK_WD)) {
  268. err("%s : Link Training Error occurs \n", __func__);
  269. retval = -1;
  270. return retval;
  271. }
  272. return retval;
  273. }
  274. static int hpc_get_attention_status(struct slot *slot, u8 *status)
  275. {
  276. struct controller *ctrl = slot->ctrl;
  277. u16 slot_ctrl;
  278. u8 atten_led_state;
  279. int retval = 0;
  280. retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
  281. if (retval) {
  282. err("%s: Cannot read SLOTCTRL register\n", __func__);
  283. return retval;
  284. }
  285. dbg("%s: SLOTCTRL %x, value read %x\n",
  286. __func__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
  287. atten_led_state = (slot_ctrl & ATTN_LED_CTRL) >> 6;
  288. switch (atten_led_state) {
  289. case 0:
  290. *status = 0xFF; /* Reserved */
  291. break;
  292. case 1:
  293. *status = 1; /* On */
  294. break;
  295. case 2:
  296. *status = 2; /* Blink */
  297. break;
  298. case 3:
  299. *status = 0; /* Off */
  300. break;
  301. default:
  302. *status = 0xFF;
  303. break;
  304. }
  305. return 0;
  306. }
  307. static int hpc_get_power_status(struct slot *slot, u8 *status)
  308. {
  309. struct controller *ctrl = slot->ctrl;
  310. u16 slot_ctrl;
  311. u8 pwr_state;
  312. int retval = 0;
  313. retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
  314. if (retval) {
  315. err("%s: Cannot read SLOTCTRL register\n", __func__);
  316. return retval;
  317. }
  318. dbg("%s: SLOTCTRL %x value read %x\n",
  319. __func__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
  320. pwr_state = (slot_ctrl & PWR_CTRL) >> 10;
  321. switch (pwr_state) {
  322. case 0:
  323. *status = 1;
  324. break;
  325. case 1:
  326. *status = 0;
  327. break;
  328. default:
  329. *status = 0xFF;
  330. break;
  331. }
  332. return retval;
  333. }
  334. static int hpc_get_latch_status(struct slot *slot, u8 *status)
  335. {
  336. struct controller *ctrl = slot->ctrl;
  337. u16 slot_status;
  338. int retval = 0;
  339. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  340. if (retval) {
  341. err("%s: Cannot read SLOTSTATUS register\n", __func__);
  342. return retval;
  343. }
  344. *status = (((slot_status & MRL_STATE) >> 5) == 0) ? 0 : 1;
  345. return 0;
  346. }
  347. static int hpc_get_adapter_status(struct slot *slot, u8 *status)
  348. {
  349. struct controller *ctrl = slot->ctrl;
  350. u16 slot_status;
  351. u8 card_state;
  352. int retval = 0;
  353. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  354. if (retval) {
  355. err("%s: Cannot read SLOTSTATUS register\n", __func__);
  356. return retval;
  357. }
  358. card_state = (u8)((slot_status & PRSN_STATE) >> 6);
  359. *status = (card_state == 1) ? 1 : 0;
  360. return 0;
  361. }
  362. static int hpc_query_power_fault(struct slot *slot)
  363. {
  364. struct controller *ctrl = slot->ctrl;
  365. u16 slot_status;
  366. u8 pwr_fault;
  367. int retval = 0;
  368. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  369. if (retval) {
  370. err("%s: Cannot check for power fault\n", __func__);
  371. return retval;
  372. }
  373. pwr_fault = (u8)((slot_status & PWR_FAULT_DETECTED) >> 1);
  374. return pwr_fault;
  375. }
  376. static int hpc_get_emi_status(struct slot *slot, u8 *status)
  377. {
  378. struct controller *ctrl = slot->ctrl;
  379. u16 slot_status;
  380. int retval = 0;
  381. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  382. if (retval) {
  383. err("%s : Cannot check EMI status\n", __func__);
  384. return retval;
  385. }
  386. *status = (slot_status & EMI_STATE) >> EMI_STATUS_BIT;
  387. return retval;
  388. }
  389. static int hpc_toggle_emi(struct slot *slot)
  390. {
  391. u16 slot_cmd;
  392. u16 cmd_mask;
  393. int rc;
  394. slot_cmd = EMI_CTRL;
  395. cmd_mask = EMI_CTRL;
  396. rc = pcie_write_cmd(slot->ctrl, slot_cmd, cmd_mask);
  397. slot->last_emi_toggle = get_seconds();
  398. return rc;
  399. }
  400. static int hpc_set_attention_status(struct slot *slot, u8 value)
  401. {
  402. struct controller *ctrl = slot->ctrl;
  403. u16 slot_cmd;
  404. u16 cmd_mask;
  405. int rc;
  406. cmd_mask = ATTN_LED_CTRL;
  407. switch (value) {
  408. case 0 : /* turn off */
  409. slot_cmd = 0x00C0;
  410. break;
  411. case 1: /* turn on */
  412. slot_cmd = 0x0040;
  413. break;
  414. case 2: /* turn blink */
  415. slot_cmd = 0x0080;
  416. break;
  417. default:
  418. return -1;
  419. }
  420. rc = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  421. dbg("%s: SLOTCTRL %x write cmd %x\n",
  422. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  423. return rc;
  424. }
  425. static void hpc_set_green_led_on(struct slot *slot)
  426. {
  427. struct controller *ctrl = slot->ctrl;
  428. u16 slot_cmd;
  429. u16 cmd_mask;
  430. slot_cmd = 0x0100;
  431. cmd_mask = PWR_LED_CTRL;
  432. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  433. dbg("%s: SLOTCTRL %x write cmd %x\n",
  434. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  435. }
  436. static void hpc_set_green_led_off(struct slot *slot)
  437. {
  438. struct controller *ctrl = slot->ctrl;
  439. u16 slot_cmd;
  440. u16 cmd_mask;
  441. slot_cmd = 0x0300;
  442. cmd_mask = PWR_LED_CTRL;
  443. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  444. dbg("%s: SLOTCTRL %x write cmd %x\n",
  445. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  446. }
  447. static void hpc_set_green_led_blink(struct slot *slot)
  448. {
  449. struct controller *ctrl = slot->ctrl;
  450. u16 slot_cmd;
  451. u16 cmd_mask;
  452. slot_cmd = 0x0200;
  453. cmd_mask = PWR_LED_CTRL;
  454. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  455. dbg("%s: SLOTCTRL %x write cmd %x\n",
  456. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  457. }
  458. static void hpc_release_ctlr(struct controller *ctrl)
  459. {
  460. /* Mask Hot-plug Interrupt Enable */
  461. if (pcie_write_cmd(ctrl, 0, HP_INTR_ENABLE | CMD_CMPL_INTR_ENABLE))
  462. err("%s: Cannot mask hotplut interrupt enable\n", __func__);
  463. if (pciehp_poll_mode)
  464. del_timer(&ctrl->poll_timer);
  465. else
  466. free_irq(ctrl->pci_dev->irq, ctrl);
  467. /*
  468. * If this is the last controller to be released, destroy the
  469. * pciehp work queue
  470. */
  471. if (atomic_dec_and_test(&pciehp_num_controllers))
  472. destroy_workqueue(pciehp_wq);
  473. }
  474. static int hpc_power_on_slot(struct slot * slot)
  475. {
  476. struct controller *ctrl = slot->ctrl;
  477. u16 slot_cmd;
  478. u16 cmd_mask;
  479. u16 slot_status;
  480. int retval = 0;
  481. dbg("%s: slot->hp_slot %x\n", __func__, slot->hp_slot);
  482. /* Clear sticky power-fault bit from previous power failures */
  483. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  484. if (retval) {
  485. err("%s: Cannot read SLOTSTATUS register\n", __func__);
  486. return retval;
  487. }
  488. slot_status &= PWR_FAULT_DETECTED;
  489. if (slot_status) {
  490. retval = pciehp_writew(ctrl, SLOTSTATUS, slot_status);
  491. if (retval) {
  492. err("%s: Cannot write to SLOTSTATUS register\n",
  493. __func__);
  494. return retval;
  495. }
  496. }
  497. slot_cmd = POWER_ON;
  498. cmd_mask = PWR_CTRL;
  499. /* Enable detection that we turned off at slot power-off time */
  500. if (!pciehp_poll_mode) {
  501. slot_cmd |= (PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE |
  502. PRSN_DETECT_ENABLE);
  503. cmd_mask |= (PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE |
  504. PRSN_DETECT_ENABLE);
  505. }
  506. retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  507. if (retval) {
  508. err("%s: Write %x command failed!\n", __func__, slot_cmd);
  509. return -1;
  510. }
  511. dbg("%s: SLOTCTRL %x write cmd %x\n",
  512. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  513. return retval;
  514. }
  515. static inline int pcie_mask_bad_dllp(struct controller *ctrl)
  516. {
  517. struct pci_dev *dev = ctrl->pci_dev;
  518. int pos;
  519. u32 reg;
  520. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  521. if (!pos)
  522. return 0;
  523. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg);
  524. if (reg & PCI_ERR_COR_BAD_DLLP)
  525. return 0;
  526. reg |= PCI_ERR_COR_BAD_DLLP;
  527. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg);
  528. return 1;
  529. }
  530. static inline void pcie_unmask_bad_dllp(struct controller *ctrl)
  531. {
  532. struct pci_dev *dev = ctrl->pci_dev;
  533. u32 reg;
  534. int pos;
  535. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  536. if (!pos)
  537. return;
  538. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg);
  539. if (!(reg & PCI_ERR_COR_BAD_DLLP))
  540. return;
  541. reg &= ~PCI_ERR_COR_BAD_DLLP;
  542. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg);
  543. }
  544. static int hpc_power_off_slot(struct slot * slot)
  545. {
  546. struct controller *ctrl = slot->ctrl;
  547. u16 slot_cmd;
  548. u16 cmd_mask;
  549. int retval = 0;
  550. int changed;
  551. dbg("%s: slot->hp_slot %x\n", __func__, slot->hp_slot);
  552. /*
  553. * Set Bad DLLP Mask bit in Correctable Error Mask
  554. * Register. This is the workaround against Bad DLLP error
  555. * that sometimes happens during turning power off the slot
  556. * which conforms to PCI Express 1.0a spec.
  557. */
  558. changed = pcie_mask_bad_dllp(ctrl);
  559. slot_cmd = POWER_OFF;
  560. cmd_mask = PWR_CTRL;
  561. /*
  562. * If we get MRL or presence detect interrupts now, the isr
  563. * will notice the sticky power-fault bit too and issue power
  564. * indicator change commands. This will lead to an endless loop
  565. * of command completions, since the power-fault bit remains on
  566. * till the slot is powered on again.
  567. */
  568. if (!pciehp_poll_mode) {
  569. slot_cmd &= ~(PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE |
  570. PRSN_DETECT_ENABLE);
  571. cmd_mask |= (PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE |
  572. PRSN_DETECT_ENABLE);
  573. }
  574. retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  575. if (retval) {
  576. err("%s: Write command failed!\n", __func__);
  577. retval = -1;
  578. goto out;
  579. }
  580. dbg("%s: SLOTCTRL %x write cmd %x\n",
  581. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  582. /*
  583. * After turning power off, we must wait for at least 1 second
  584. * before taking any action that relies on power having been
  585. * removed from the slot/adapter.
  586. */
  587. msleep(1000);
  588. out:
  589. if (changed)
  590. pcie_unmask_bad_dllp(ctrl);
  591. return retval;
  592. }
  593. static irqreturn_t pcie_isr(int irq, void *dev_id)
  594. {
  595. struct controller *ctrl = (struct controller *)dev_id;
  596. u16 detected, intr_loc;
  597. /*
  598. * In order to guarantee that all interrupt events are
  599. * serviced, we need to re-inspect Slot Status register after
  600. * clearing what is presumed to be the last pending interrupt.
  601. */
  602. intr_loc = 0;
  603. do {
  604. if (pciehp_readw(ctrl, SLOTSTATUS, &detected)) {
  605. err("%s: Cannot read SLOTSTATUS\n", __func__);
  606. return IRQ_NONE;
  607. }
  608. detected &= (ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED |
  609. MRL_SENS_CHANGED | PRSN_DETECT_CHANGED |
  610. CMD_COMPLETED);
  611. intr_loc |= detected;
  612. if (!intr_loc)
  613. return IRQ_NONE;
  614. if (pciehp_writew(ctrl, SLOTSTATUS, detected)) {
  615. err("%s: Cannot write to SLOTSTATUS\n", __func__);
  616. return IRQ_NONE;
  617. }
  618. } while (detected);
  619. dbg("%s: intr_loc %x\n", __FUNCTION__, intr_loc);
  620. /* Check Command Complete Interrupt Pending */
  621. if (intr_loc & CMD_COMPLETED) {
  622. ctrl->cmd_busy = 0;
  623. smp_mb();
  624. wake_up_interruptible(&ctrl->queue);
  625. }
  626. /* Check MRL Sensor Changed */
  627. if (intr_loc & MRL_SENS_CHANGED)
  628. pciehp_handle_switch_change(0, ctrl);
  629. /* Check Attention Button Pressed */
  630. if (intr_loc & ATTN_BUTTN_PRESSED)
  631. pciehp_handle_attention_button(0, ctrl);
  632. /* Check Presence Detect Changed */
  633. if (intr_loc & PRSN_DETECT_CHANGED)
  634. pciehp_handle_presence_change(0, ctrl);
  635. /* Check Power Fault Detected */
  636. if (intr_loc & PWR_FAULT_DETECTED)
  637. pciehp_handle_power_fault(0, ctrl);
  638. return IRQ_HANDLED;
  639. }
  640. static int hpc_get_max_lnk_speed(struct slot *slot, enum pci_bus_speed *value)
  641. {
  642. struct controller *ctrl = slot->ctrl;
  643. enum pcie_link_speed lnk_speed;
  644. u32 lnk_cap;
  645. int retval = 0;
  646. retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
  647. if (retval) {
  648. err("%s: Cannot read LNKCAP register\n", __func__);
  649. return retval;
  650. }
  651. switch (lnk_cap & 0x000F) {
  652. case 1:
  653. lnk_speed = PCIE_2PT5GB;
  654. break;
  655. default:
  656. lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
  657. break;
  658. }
  659. *value = lnk_speed;
  660. dbg("Max link speed = %d\n", lnk_speed);
  661. return retval;
  662. }
  663. static int hpc_get_max_lnk_width(struct slot *slot,
  664. enum pcie_link_width *value)
  665. {
  666. struct controller *ctrl = slot->ctrl;
  667. enum pcie_link_width lnk_wdth;
  668. u32 lnk_cap;
  669. int retval = 0;
  670. retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
  671. if (retval) {
  672. err("%s: Cannot read LNKCAP register\n", __func__);
  673. return retval;
  674. }
  675. switch ((lnk_cap & 0x03F0) >> 4){
  676. case 0:
  677. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  678. break;
  679. case 1:
  680. lnk_wdth = PCIE_LNK_X1;
  681. break;
  682. case 2:
  683. lnk_wdth = PCIE_LNK_X2;
  684. break;
  685. case 4:
  686. lnk_wdth = PCIE_LNK_X4;
  687. break;
  688. case 8:
  689. lnk_wdth = PCIE_LNK_X8;
  690. break;
  691. case 12:
  692. lnk_wdth = PCIE_LNK_X12;
  693. break;
  694. case 16:
  695. lnk_wdth = PCIE_LNK_X16;
  696. break;
  697. case 32:
  698. lnk_wdth = PCIE_LNK_X32;
  699. break;
  700. default:
  701. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  702. break;
  703. }
  704. *value = lnk_wdth;
  705. dbg("Max link width = %d\n", lnk_wdth);
  706. return retval;
  707. }
  708. static int hpc_get_cur_lnk_speed(struct slot *slot, enum pci_bus_speed *value)
  709. {
  710. struct controller *ctrl = slot->ctrl;
  711. enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
  712. int retval = 0;
  713. u16 lnk_status;
  714. retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
  715. if (retval) {
  716. err("%s: Cannot read LNKSTATUS register\n", __func__);
  717. return retval;
  718. }
  719. switch (lnk_status & 0x0F) {
  720. case 1:
  721. lnk_speed = PCIE_2PT5GB;
  722. break;
  723. default:
  724. lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
  725. break;
  726. }
  727. *value = lnk_speed;
  728. dbg("Current link speed = %d\n", lnk_speed);
  729. return retval;
  730. }
  731. static int hpc_get_cur_lnk_width(struct slot *slot,
  732. enum pcie_link_width *value)
  733. {
  734. struct controller *ctrl = slot->ctrl;
  735. enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  736. int retval = 0;
  737. u16 lnk_status;
  738. retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
  739. if (retval) {
  740. err("%s: Cannot read LNKSTATUS register\n", __func__);
  741. return retval;
  742. }
  743. switch ((lnk_status & 0x03F0) >> 4){
  744. case 0:
  745. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  746. break;
  747. case 1:
  748. lnk_wdth = PCIE_LNK_X1;
  749. break;
  750. case 2:
  751. lnk_wdth = PCIE_LNK_X2;
  752. break;
  753. case 4:
  754. lnk_wdth = PCIE_LNK_X4;
  755. break;
  756. case 8:
  757. lnk_wdth = PCIE_LNK_X8;
  758. break;
  759. case 12:
  760. lnk_wdth = PCIE_LNK_X12;
  761. break;
  762. case 16:
  763. lnk_wdth = PCIE_LNK_X16;
  764. break;
  765. case 32:
  766. lnk_wdth = PCIE_LNK_X32;
  767. break;
  768. default:
  769. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  770. break;
  771. }
  772. *value = lnk_wdth;
  773. dbg("Current link width = %d\n", lnk_wdth);
  774. return retval;
  775. }
  776. static struct hpc_ops pciehp_hpc_ops = {
  777. .power_on_slot = hpc_power_on_slot,
  778. .power_off_slot = hpc_power_off_slot,
  779. .set_attention_status = hpc_set_attention_status,
  780. .get_power_status = hpc_get_power_status,
  781. .get_attention_status = hpc_get_attention_status,
  782. .get_latch_status = hpc_get_latch_status,
  783. .get_adapter_status = hpc_get_adapter_status,
  784. .get_emi_status = hpc_get_emi_status,
  785. .toggle_emi = hpc_toggle_emi,
  786. .get_max_bus_speed = hpc_get_max_lnk_speed,
  787. .get_cur_bus_speed = hpc_get_cur_lnk_speed,
  788. .get_max_lnk_width = hpc_get_max_lnk_width,
  789. .get_cur_lnk_width = hpc_get_cur_lnk_width,
  790. .query_power_fault = hpc_query_power_fault,
  791. .green_led_on = hpc_set_green_led_on,
  792. .green_led_off = hpc_set_green_led_off,
  793. .green_led_blink = hpc_set_green_led_blink,
  794. .release_ctlr = hpc_release_ctlr,
  795. .check_lnk_status = hpc_check_lnk_status,
  796. };
  797. #ifdef CONFIG_ACPI
  798. int pciehp_acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev)
  799. {
  800. acpi_status status;
  801. acpi_handle chandle, handle = DEVICE_ACPI_HANDLE(&(dev->dev));
  802. struct pci_dev *pdev = dev;
  803. struct pci_bus *parent;
  804. struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
  805. /*
  806. * Per PCI firmware specification, we should run the ACPI _OSC
  807. * method to get control of hotplug hardware before using it.
  808. * If an _OSC is missing, we look for an OSHP to do the same thing.
  809. * To handle different BIOS behavior, we look for _OSC and OSHP
  810. * within the scope of the hotplug controller and its parents, upto
  811. * the host bridge under which this controller exists.
  812. */
  813. while (!handle) {
  814. /*
  815. * This hotplug controller was not listed in the ACPI name
  816. * space at all. Try to get acpi handle of parent pci bus.
  817. */
  818. if (!pdev || !pdev->bus->parent)
  819. break;
  820. parent = pdev->bus->parent;
  821. dbg("Could not find %s in acpi namespace, trying parent\n",
  822. pci_name(pdev));
  823. if (!parent->self)
  824. /* Parent must be a host bridge */
  825. handle = acpi_get_pci_rootbridge_handle(
  826. pci_domain_nr(parent),
  827. parent->number);
  828. else
  829. handle = DEVICE_ACPI_HANDLE(
  830. &(parent->self->dev));
  831. pdev = parent->self;
  832. }
  833. while (handle) {
  834. acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
  835. dbg("Trying to get hotplug control for %s \n",
  836. (char *)string.pointer);
  837. status = pci_osc_control_set(handle,
  838. OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL |
  839. OSC_PCI_EXPRESS_NATIVE_HP_CONTROL);
  840. if (status == AE_NOT_FOUND)
  841. status = acpi_run_oshp(handle);
  842. if (ACPI_SUCCESS(status)) {
  843. dbg("Gained control for hotplug HW for pci %s (%s)\n",
  844. pci_name(dev), (char *)string.pointer);
  845. kfree(string.pointer);
  846. return 0;
  847. }
  848. if (acpi_root_bridge(handle))
  849. break;
  850. chandle = handle;
  851. status = acpi_get_parent(chandle, &handle);
  852. if (ACPI_FAILURE(status))
  853. break;
  854. }
  855. err("Cannot get control of hotplug hardware for pci %s\n",
  856. pci_name(dev));
  857. kfree(string.pointer);
  858. return -1;
  859. }
  860. #endif
  861. static int pcie_init_hardware_part1(struct controller *ctrl,
  862. struct pcie_device *dev)
  863. {
  864. /* Mask Hot-plug Interrupt Enable */
  865. if (pcie_write_cmd(ctrl, 0, HP_INTR_ENABLE | CMD_CMPL_INTR_ENABLE)) {
  866. err("%s: Cannot mask hotplug interrupt enable\n", __func__);
  867. return -1;
  868. }
  869. return 0;
  870. }
  871. int pcie_init_hardware_part2(struct controller *ctrl, struct pcie_device *dev)
  872. {
  873. u16 cmd, mask;
  874. /*
  875. * We need to clear all events before enabling hotplug interrupt
  876. * notification mechanism in order for hotplug controler to
  877. * generate interrupts.
  878. */
  879. if (pciehp_writew(ctrl, SLOTSTATUS, 0x1f)) {
  880. err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
  881. return -1;
  882. }
  883. cmd = PRSN_DETECT_ENABLE;
  884. if (ATTN_BUTTN(ctrl))
  885. cmd |= ATTN_BUTTN_ENABLE;
  886. if (POWER_CTRL(ctrl))
  887. cmd |= PWR_FAULT_DETECT_ENABLE;
  888. if (MRL_SENS(ctrl))
  889. cmd |= MRL_DETECT_ENABLE;
  890. if (!pciehp_poll_mode)
  891. cmd |= HP_INTR_ENABLE;
  892. mask = PRSN_DETECT_ENABLE | ATTN_BUTTN_ENABLE |
  893. PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE | HP_INTR_ENABLE;
  894. if (pcie_write_cmd(ctrl, cmd, mask)) {
  895. err("%s: Cannot enable software notification\n", __func__);
  896. goto abort;
  897. }
  898. if (pciehp_force)
  899. dbg("Bypassing BIOS check for pciehp use on %s\n",
  900. pci_name(ctrl->pci_dev));
  901. else if (pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev))
  902. goto abort_disable_intr;
  903. return 0;
  904. /* We end up here for the many possible ways to fail this API. */
  905. abort_disable_intr:
  906. if (pcie_write_cmd(ctrl, 0, HP_INTR_ENABLE))
  907. err("%s : disabling interrupts failed\n", __func__);
  908. abort:
  909. return -1;
  910. }
  911. int pcie_init(struct controller *ctrl, struct pcie_device *dev)
  912. {
  913. int rc;
  914. u16 cap_reg;
  915. u32 slot_cap;
  916. int cap_base;
  917. u16 slot_status, slot_ctrl;
  918. struct pci_dev *pdev;
  919. pdev = dev->port;
  920. ctrl->pci_dev = pdev; /* save pci_dev in context */
  921. dbg("%s: hotplug controller vendor id 0x%x device id 0x%x\n",
  922. __func__, pdev->vendor, pdev->device);
  923. cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  924. if (cap_base == 0) {
  925. dbg("%s: Can't find PCI_CAP_ID_EXP (0x10)\n", __func__);
  926. goto abort;
  927. }
  928. ctrl->cap_base = cap_base;
  929. dbg("%s: pcie_cap_base %x\n", __func__, cap_base);
  930. rc = pciehp_readw(ctrl, CAPREG, &cap_reg);
  931. if (rc) {
  932. err("%s: Cannot read CAPREG register\n", __func__);
  933. goto abort;
  934. }
  935. dbg("%s: CAPREG offset %x cap_reg %x\n",
  936. __func__, ctrl->cap_base + CAPREG, cap_reg);
  937. if (((cap_reg & SLOT_IMPL) == 0) ||
  938. (((cap_reg & DEV_PORT_TYPE) != 0x0040)
  939. && ((cap_reg & DEV_PORT_TYPE) != 0x0060))) {
  940. dbg("%s : This is not a root port or the port is not "
  941. "connected to a slot\n", __func__);
  942. goto abort;
  943. }
  944. rc = pciehp_readl(ctrl, SLOTCAP, &slot_cap);
  945. if (rc) {
  946. err("%s: Cannot read SLOTCAP register\n", __func__);
  947. goto abort;
  948. }
  949. dbg("%s: SLOTCAP offset %x slot_cap %x\n",
  950. __func__, ctrl->cap_base + SLOTCAP, slot_cap);
  951. if (!(slot_cap & HP_CAP)) {
  952. dbg("%s : This slot is not hot-plug capable\n", __func__);
  953. goto abort;
  954. }
  955. /* For debugging purpose */
  956. rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  957. if (rc) {
  958. err("%s: Cannot read SLOTSTATUS register\n", __func__);
  959. goto abort;
  960. }
  961. dbg("%s: SLOTSTATUS offset %x slot_status %x\n",
  962. __func__, ctrl->cap_base + SLOTSTATUS, slot_status);
  963. rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
  964. if (rc) {
  965. err("%s: Cannot read SLOTCTRL register\n", __func__);
  966. goto abort;
  967. }
  968. dbg("%s: SLOTCTRL offset %x slot_ctrl %x\n",
  969. __func__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
  970. for (rc = 0; rc < DEVICE_COUNT_RESOURCE; rc++)
  971. if (pci_resource_len(pdev, rc) > 0)
  972. dbg("pci resource[%d] start=0x%llx(len=0x%llx)\n", rc,
  973. (unsigned long long)pci_resource_start(pdev, rc),
  974. (unsigned long long)pci_resource_len(pdev, rc));
  975. info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n",
  976. pdev->vendor, pdev->device,
  977. pdev->subsystem_vendor, pdev->subsystem_device);
  978. mutex_init(&ctrl->crit_sect);
  979. mutex_init(&ctrl->ctrl_lock);
  980. /* setup wait queue */
  981. init_waitqueue_head(&ctrl->queue);
  982. /* return PCI Controller Info */
  983. ctrl->slot_device_offset = 0;
  984. ctrl->num_slots = 1;
  985. ctrl->first_slot = slot_cap >> 19;
  986. ctrl->slot_cap = slot_cap;
  987. rc = pcie_init_hardware_part1(ctrl, dev);
  988. if (rc)
  989. goto abort;
  990. if (pciehp_poll_mode) {
  991. /* Install interrupt polling timer. Start with 10 sec delay */
  992. init_timer(&ctrl->poll_timer);
  993. start_int_poll_timer(ctrl, 10);
  994. } else {
  995. /* Installs the interrupt handler */
  996. rc = request_irq(ctrl->pci_dev->irq, pcie_isr, IRQF_SHARED,
  997. MY_NAME, (void *)ctrl);
  998. dbg("%s: request_irq %d for hpc%d (returns %d)\n",
  999. __func__, ctrl->pci_dev->irq,
  1000. atomic_read(&pciehp_num_controllers), rc);
  1001. if (rc) {
  1002. err("Can't get irq %d for the hotplug controller\n",
  1003. ctrl->pci_dev->irq);
  1004. goto abort;
  1005. }
  1006. }
  1007. dbg("pciehp ctrl b:d:f:irq=0x%x:%x:%x:%x\n", pdev->bus->number,
  1008. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), dev->irq);
  1009. /*
  1010. * If this is the first controller to be initialized,
  1011. * initialize the pciehp work queue
  1012. */
  1013. if (atomic_add_return(1, &pciehp_num_controllers) == 1) {
  1014. pciehp_wq = create_singlethread_workqueue("pciehpd");
  1015. if (!pciehp_wq) {
  1016. rc = -ENOMEM;
  1017. goto abort_free_irq;
  1018. }
  1019. }
  1020. rc = pcie_init_hardware_part2(ctrl, dev);
  1021. if (rc == 0) {
  1022. ctrl->hpc_ops = &pciehp_hpc_ops;
  1023. return 0;
  1024. }
  1025. abort_free_irq:
  1026. if (pciehp_poll_mode)
  1027. del_timer_sync(&ctrl->poll_timer);
  1028. else
  1029. free_irq(ctrl->pci_dev->irq, ctrl);
  1030. abort:
  1031. return -1;
  1032. }