pciehp_hpc.c 29 KB

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