pciehp_hpc.c 38 KB

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