pciehp_hpc.c 32 KB

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