pciehp_hpc.c 39 KB

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