pciehp_hpc.c 29 KB

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