pciehp_hpc.c 35 KB

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