pciehp_hpc.c 33 KB

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