pciehp_hpc.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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. if (!pciehp_poll_mode)
  637. slot_cmd = slot_cmd | HP_INTR_ENABLE;
  638. retval = pcie_write_cmd(slot, slot_cmd);
  639. if (retval) {
  640. err("%s: Write %x command failed!\n", __FUNCTION__, slot_cmd);
  641. return -1;
  642. }
  643. dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
  644. DBG_LEAVE_ROUTINE
  645. return retval;
  646. }
  647. static int hpc_power_off_slot(struct slot * slot)
  648. {
  649. struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
  650. u16 slot_cmd;
  651. u16 slot_ctrl;
  652. int retval = 0;
  653. DBG_ENTER_ROUTINE
  654. if (!php_ctlr) {
  655. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  656. return -1;
  657. }
  658. dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
  659. slot->hp_slot = 0;
  660. if (slot->hp_slot >= php_ctlr->num_slots) {
  661. err("%s: Invalid HPC slot number!\n", __FUNCTION__);
  662. return -1;
  663. }
  664. retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
  665. if (retval) {
  666. err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
  667. return retval;
  668. }
  669. slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_OFF;
  670. if (!pciehp_poll_mode)
  671. slot_cmd = slot_cmd | HP_INTR_ENABLE;
  672. retval = pcie_write_cmd(slot, slot_cmd);
  673. if (retval) {
  674. err("%s: Write command failed!\n", __FUNCTION__);
  675. return -1;
  676. }
  677. dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
  678. DBG_LEAVE_ROUTINE
  679. return retval;
  680. }
  681. static irqreturn_t pcie_isr(int IRQ, void *dev_id, struct pt_regs *regs)
  682. {
  683. struct controller *ctrl = NULL;
  684. struct php_ctlr_state_s *php_ctlr;
  685. u8 schedule_flag = 0;
  686. u16 slot_status, intr_detect, intr_loc;
  687. u16 temp_word;
  688. int hp_slot = 0; /* only 1 slot per PCI Express port */
  689. int rc = 0;
  690. if (!dev_id)
  691. return IRQ_NONE;
  692. if (!pciehp_poll_mode) {
  693. ctrl = dev_id;
  694. php_ctlr = ctrl->hpc_ctlr_handle;
  695. } else {
  696. php_ctlr = dev_id;
  697. ctrl = (struct controller *)php_ctlr->callback_instance_id;
  698. }
  699. if (!ctrl) {
  700. dbg("%s: dev_id %p ctlr == NULL\n", __FUNCTION__, (void*) dev_id);
  701. return IRQ_NONE;
  702. }
  703. if (!php_ctlr) {
  704. dbg("%s: php_ctlr == NULL\n", __FUNCTION__);
  705. return IRQ_NONE;
  706. }
  707. rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
  708. if (rc) {
  709. err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
  710. return IRQ_NONE;
  711. }
  712. intr_detect = ( ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED | MRL_SENS_CHANGED |
  713. PRSN_DETECT_CHANGED | CMD_COMPLETED );
  714. intr_loc = slot_status & intr_detect;
  715. /* Check to see if it was our interrupt */
  716. if ( !intr_loc )
  717. return IRQ_NONE;
  718. dbg("%s: intr_loc %x\n", __FUNCTION__, intr_loc);
  719. /* Mask Hot-plug Interrupt Enable */
  720. if (!pciehp_poll_mode) {
  721. rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
  722. if (rc) {
  723. err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
  724. return IRQ_NONE;
  725. }
  726. dbg("%s: hp_register_read_word SLOT_CTRL with value %x\n", __FUNCTION__, temp_word);
  727. temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
  728. rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
  729. if (rc) {
  730. err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
  731. return IRQ_NONE;
  732. }
  733. rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
  734. if (rc) {
  735. err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
  736. return IRQ_NONE;
  737. }
  738. dbg("%s: hp_register_read_word SLOT_STATUS with value %x\n", __FUNCTION__, slot_status);
  739. /* Clear command complete interrupt caused by this write */
  740. temp_word = 0x1f;
  741. rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
  742. if (rc) {
  743. err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
  744. return IRQ_NONE;
  745. }
  746. }
  747. if (intr_loc & CMD_COMPLETED) {
  748. /*
  749. * Command Complete Interrupt Pending
  750. */
  751. wake_up_interruptible(&ctrl->queue);
  752. }
  753. if ((php_ctlr->switch_change_callback) && (intr_loc & MRL_SENS_CHANGED))
  754. schedule_flag += php_ctlr->switch_change_callback(
  755. hp_slot, php_ctlr->callback_instance_id);
  756. if ((php_ctlr->attention_button_callback) && (intr_loc & ATTN_BUTTN_PRESSED))
  757. schedule_flag += php_ctlr->attention_button_callback(
  758. hp_slot, php_ctlr->callback_instance_id);
  759. if ((php_ctlr->presence_change_callback) && (intr_loc & PRSN_DETECT_CHANGED))
  760. schedule_flag += php_ctlr->presence_change_callback(
  761. hp_slot , php_ctlr->callback_instance_id);
  762. if ((php_ctlr->power_fault_callback) && (intr_loc & PWR_FAULT_DETECTED))
  763. schedule_flag += php_ctlr->power_fault_callback(
  764. hp_slot, php_ctlr->callback_instance_id);
  765. /* Clear all events after serving them */
  766. temp_word = 0x1F;
  767. rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
  768. if (rc) {
  769. err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
  770. return IRQ_NONE;
  771. }
  772. /* Unmask Hot-plug Interrupt Enable */
  773. if (!pciehp_poll_mode) {
  774. rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
  775. if (rc) {
  776. err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
  777. return IRQ_NONE;
  778. }
  779. dbg("%s: Unmask Hot-plug Interrupt Enable\n", __FUNCTION__);
  780. temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
  781. rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
  782. if (rc) {
  783. err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
  784. return IRQ_NONE;
  785. }
  786. rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
  787. if (rc) {
  788. err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
  789. return IRQ_NONE;
  790. }
  791. /* Clear command complete interrupt caused by this write */
  792. temp_word = 0x1F;
  793. rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
  794. if (rc) {
  795. err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
  796. return IRQ_NONE;
  797. }
  798. dbg("%s: hp_register_write_word SLOT_STATUS with value %x\n", __FUNCTION__, temp_word);
  799. }
  800. return IRQ_HANDLED;
  801. }
  802. static int hpc_get_max_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
  803. {
  804. struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
  805. enum pcie_link_speed lnk_speed;
  806. u32 lnk_cap;
  807. int retval = 0;
  808. DBG_ENTER_ROUTINE
  809. if (!php_ctlr) {
  810. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  811. return -1;
  812. }
  813. if (slot->hp_slot >= php_ctlr->num_slots) {
  814. err("%s: Invalid HPC slot number!\n", __FUNCTION__);
  815. return -1;
  816. }
  817. retval = hp_register_read_dword(php_ctlr->pci_dev, LNK_CAP(slot->ctrl->cap_base), lnk_cap);
  818. if (retval) {
  819. err("%s : hp_register_read_dword LNK_CAP failed\n", __FUNCTION__);
  820. return retval;
  821. }
  822. switch (lnk_cap & 0x000F) {
  823. case 1:
  824. lnk_speed = PCIE_2PT5GB;
  825. break;
  826. default:
  827. lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
  828. break;
  829. }
  830. *value = lnk_speed;
  831. dbg("Max link speed = %d\n", lnk_speed);
  832. DBG_LEAVE_ROUTINE
  833. return retval;
  834. }
  835. static int hpc_get_max_lnk_width (struct slot *slot, enum pcie_link_width *value)
  836. {
  837. struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
  838. enum pcie_link_width lnk_wdth;
  839. u32 lnk_cap;
  840. int retval = 0;
  841. DBG_ENTER_ROUTINE
  842. if (!php_ctlr) {
  843. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  844. return -1;
  845. }
  846. if (slot->hp_slot >= php_ctlr->num_slots) {
  847. err("%s: Invalid HPC slot number!\n", __FUNCTION__);
  848. return -1;
  849. }
  850. retval = hp_register_read_dword(php_ctlr->pci_dev, LNK_CAP(slot->ctrl->cap_base), lnk_cap);
  851. if (retval) {
  852. err("%s : hp_register_read_dword LNK_CAP failed\n", __FUNCTION__);
  853. return retval;
  854. }
  855. switch ((lnk_cap & 0x03F0) >> 4){
  856. case 0:
  857. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  858. break;
  859. case 1:
  860. lnk_wdth = PCIE_LNK_X1;
  861. break;
  862. case 2:
  863. lnk_wdth = PCIE_LNK_X2;
  864. break;
  865. case 4:
  866. lnk_wdth = PCIE_LNK_X4;
  867. break;
  868. case 8:
  869. lnk_wdth = PCIE_LNK_X8;
  870. break;
  871. case 12:
  872. lnk_wdth = PCIE_LNK_X12;
  873. break;
  874. case 16:
  875. lnk_wdth = PCIE_LNK_X16;
  876. break;
  877. case 32:
  878. lnk_wdth = PCIE_LNK_X32;
  879. break;
  880. default:
  881. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  882. break;
  883. }
  884. *value = lnk_wdth;
  885. dbg("Max link width = %d\n", lnk_wdth);
  886. DBG_LEAVE_ROUTINE
  887. return retval;
  888. }
  889. static int hpc_get_cur_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
  890. {
  891. struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
  892. enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
  893. int retval = 0;
  894. u16 lnk_status;
  895. DBG_ENTER_ROUTINE
  896. if (!php_ctlr) {
  897. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  898. return -1;
  899. }
  900. if (slot->hp_slot >= php_ctlr->num_slots) {
  901. err("%s: Invalid HPC slot number!\n", __FUNCTION__);
  902. return -1;
  903. }
  904. retval = hp_register_read_word(php_ctlr->pci_dev, LNK_STATUS(slot->ctrl->cap_base), lnk_status);
  905. if (retval) {
  906. err("%s : hp_register_read_word LNK_STATUS failed\n", __FUNCTION__);
  907. return retval;
  908. }
  909. switch (lnk_status & 0x0F) {
  910. case 1:
  911. lnk_speed = PCIE_2PT5GB;
  912. break;
  913. default:
  914. lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
  915. break;
  916. }
  917. *value = lnk_speed;
  918. dbg("Current link speed = %d\n", lnk_speed);
  919. DBG_LEAVE_ROUTINE
  920. return retval;
  921. }
  922. static int hpc_get_cur_lnk_width (struct slot *slot, enum pcie_link_width *value)
  923. {
  924. struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
  925. enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  926. int retval = 0;
  927. u16 lnk_status;
  928. DBG_ENTER_ROUTINE
  929. if (!php_ctlr) {
  930. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  931. return -1;
  932. }
  933. if (slot->hp_slot >= php_ctlr->num_slots) {
  934. err("%s: Invalid HPC slot number!\n", __FUNCTION__);
  935. return -1;
  936. }
  937. retval = hp_register_read_word(php_ctlr->pci_dev, LNK_STATUS(slot->ctrl->cap_base), lnk_status);
  938. if (retval) {
  939. err("%s : hp_register_read_word LNK_STATUS failed\n", __FUNCTION__);
  940. return retval;
  941. }
  942. switch ((lnk_status & 0x03F0) >> 4){
  943. case 0:
  944. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  945. break;
  946. case 1:
  947. lnk_wdth = PCIE_LNK_X1;
  948. break;
  949. case 2:
  950. lnk_wdth = PCIE_LNK_X2;
  951. break;
  952. case 4:
  953. lnk_wdth = PCIE_LNK_X4;
  954. break;
  955. case 8:
  956. lnk_wdth = PCIE_LNK_X8;
  957. break;
  958. case 12:
  959. lnk_wdth = PCIE_LNK_X12;
  960. break;
  961. case 16:
  962. lnk_wdth = PCIE_LNK_X16;
  963. break;
  964. case 32:
  965. lnk_wdth = PCIE_LNK_X32;
  966. break;
  967. default:
  968. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  969. break;
  970. }
  971. *value = lnk_wdth;
  972. dbg("Current link width = %d\n", lnk_wdth);
  973. DBG_LEAVE_ROUTINE
  974. return retval;
  975. }
  976. static struct hpc_ops pciehp_hpc_ops = {
  977. .power_on_slot = hpc_power_on_slot,
  978. .power_off_slot = hpc_power_off_slot,
  979. .set_attention_status = hpc_set_attention_status,
  980. .get_power_status = hpc_get_power_status,
  981. .get_attention_status = hpc_get_attention_status,
  982. .get_latch_status = hpc_get_latch_status,
  983. .get_adapter_status = hpc_get_adapter_status,
  984. .get_max_bus_speed = hpc_get_max_lnk_speed,
  985. .get_cur_bus_speed = hpc_get_cur_lnk_speed,
  986. .get_max_lnk_width = hpc_get_max_lnk_width,
  987. .get_cur_lnk_width = hpc_get_cur_lnk_width,
  988. .query_power_fault = hpc_query_power_fault,
  989. .green_led_on = hpc_set_green_led_on,
  990. .green_led_off = hpc_set_green_led_off,
  991. .green_led_blink = hpc_set_green_led_blink,
  992. .release_ctlr = hpc_release_ctlr,
  993. .check_lnk_status = hpc_check_lnk_status,
  994. };
  995. int pcie_init(struct controller * ctrl, struct pcie_device *dev)
  996. {
  997. struct php_ctlr_state_s *php_ctlr, *p;
  998. void *instance_id = ctrl;
  999. int rc;
  1000. static int first = 1;
  1001. u16 temp_word;
  1002. u16 cap_reg;
  1003. u16 intr_enable = 0;
  1004. u32 slot_cap;
  1005. int cap_base, saved_cap_base;
  1006. u16 slot_status, slot_ctrl;
  1007. struct pci_dev *pdev;
  1008. DBG_ENTER_ROUTINE
  1009. spin_lock_init(&list_lock);
  1010. php_ctlr = (struct php_ctlr_state_s *) kmalloc(sizeof(struct php_ctlr_state_s), GFP_KERNEL);
  1011. if (!php_ctlr) { /* allocate controller state data */
  1012. err("%s: HPC controller memory allocation error!\n", __FUNCTION__);
  1013. goto abort;
  1014. }
  1015. memset(php_ctlr, 0, sizeof(struct php_ctlr_state_s));
  1016. pdev = dev->port;
  1017. php_ctlr->pci_dev = pdev; /* save pci_dev in context */
  1018. dbg("%s: hotplug controller vendor id 0x%x device id 0x%x\n",
  1019. __FUNCTION__, pdev->vendor, pdev->device);
  1020. saved_cap_base = pcie_cap_base;
  1021. if ((cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP)) == 0) {
  1022. dbg("%s: Can't find PCI_CAP_ID_EXP (0x10)\n", __FUNCTION__);
  1023. goto abort_free_ctlr;
  1024. }
  1025. ctrl->cap_base = cap_base;
  1026. dbg("%s: pcie_cap_base %x\n", __FUNCTION__, pcie_cap_base);
  1027. rc = hp_register_read_word(pdev, CAP_REG(ctrl->cap_base), cap_reg);
  1028. if (rc) {
  1029. err("%s : hp_register_read_word CAP_REG failed\n", __FUNCTION__);
  1030. goto abort_free_ctlr;
  1031. }
  1032. dbg("%s: CAP_REG offset %x cap_reg %x\n", __FUNCTION__, CAP_REG(ctrl->cap_base), cap_reg);
  1033. if (((cap_reg & SLOT_IMPL) == 0) || (((cap_reg & DEV_PORT_TYPE) != 0x0040)
  1034. && ((cap_reg & DEV_PORT_TYPE) != 0x0060))) {
  1035. dbg("%s : This is not a root port or the port is not connected to a slot\n", __FUNCTION__);
  1036. goto abort_free_ctlr;
  1037. }
  1038. rc = hp_register_read_dword(php_ctlr->pci_dev, SLOT_CAP(ctrl->cap_base), slot_cap);
  1039. if (rc) {
  1040. err("%s : hp_register_read_word CAP_REG failed\n", __FUNCTION__);
  1041. goto abort_free_ctlr;
  1042. }
  1043. dbg("%s: SLOT_CAP offset %x slot_cap %x\n", __FUNCTION__, SLOT_CAP(ctrl->cap_base), slot_cap);
  1044. if (!(slot_cap & HP_CAP)) {
  1045. dbg("%s : This slot is not hot-plug capable\n", __FUNCTION__);
  1046. goto abort_free_ctlr;
  1047. }
  1048. /* For debugging purpose */
  1049. rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
  1050. if (rc) {
  1051. err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
  1052. goto abort_free_ctlr;
  1053. }
  1054. dbg("%s: SLOT_STATUS offset %x slot_status %x\n", __FUNCTION__, SLOT_STATUS(ctrl->cap_base), slot_status);
  1055. rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), slot_ctrl);
  1056. if (rc) {
  1057. err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
  1058. goto abort_free_ctlr;
  1059. }
  1060. dbg("%s: SLOT_CTRL offset %x slot_ctrl %x\n", __FUNCTION__, SLOT_CTRL(ctrl->cap_base), slot_ctrl);
  1061. if (first) {
  1062. spin_lock_init(&hpc_event_lock);
  1063. first = 0;
  1064. }
  1065. for ( rc = 0; rc < DEVICE_COUNT_RESOURCE; rc++)
  1066. if (pci_resource_len(pdev, rc) > 0)
  1067. dbg("pci resource[%d] start=0x%lx(len=0x%lx)\n", rc,
  1068. pci_resource_start(pdev, rc), pci_resource_len(pdev, rc));
  1069. info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device,
  1070. pdev->subsystem_vendor, pdev->subsystem_device);
  1071. if (pci_enable_device(pdev))
  1072. goto abort_free_ctlr;
  1073. init_MUTEX(&ctrl->crit_sect);
  1074. /* setup wait queue */
  1075. init_waitqueue_head(&ctrl->queue);
  1076. /* find the IRQ */
  1077. php_ctlr->irq = dev->irq;
  1078. /* Save interrupt callback info */
  1079. php_ctlr->attention_button_callback = pciehp_handle_attention_button;
  1080. php_ctlr->switch_change_callback = pciehp_handle_switch_change;
  1081. php_ctlr->presence_change_callback = pciehp_handle_presence_change;
  1082. php_ctlr->power_fault_callback = pciehp_handle_power_fault;
  1083. php_ctlr->callback_instance_id = instance_id;
  1084. /* return PCI Controller Info */
  1085. php_ctlr->slot_device_offset = 0;
  1086. php_ctlr->num_slots = 1;
  1087. /* Mask Hot-plug Interrupt Enable */
  1088. rc = hp_register_read_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
  1089. if (rc) {
  1090. err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
  1091. goto abort_free_ctlr;
  1092. }
  1093. dbg("%s: SLOT_CTRL %x value read %x\n", __FUNCTION__, SLOT_CTRL(ctrl->cap_base), temp_word);
  1094. temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
  1095. rc = hp_register_write_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
  1096. if (rc) {
  1097. err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
  1098. goto abort_free_ctlr;
  1099. }
  1100. rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
  1101. if (rc) {
  1102. err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
  1103. goto abort_free_ctlr;
  1104. }
  1105. temp_word = 0x1F; /* Clear all events */
  1106. rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
  1107. if (rc) {
  1108. err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
  1109. goto abort_free_ctlr;
  1110. }
  1111. if (pciehp_poll_mode) {/* Install interrupt polling code */
  1112. /* Install and start the interrupt polling timer */
  1113. init_timer(&php_ctlr->int_poll_timer);
  1114. start_int_poll_timer( php_ctlr, 10 ); /* start with 10 second delay */
  1115. } else {
  1116. /* Installs the interrupt handler */
  1117. rc = request_irq(php_ctlr->irq, pcie_isr, SA_SHIRQ, MY_NAME, (void *) ctrl);
  1118. dbg("%s: request_irq %d for hpc%d (returns %d)\n", __FUNCTION__, php_ctlr->irq, ctlr_seq_num, rc);
  1119. if (rc) {
  1120. err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
  1121. goto abort_free_ctlr;
  1122. }
  1123. }
  1124. dbg("pciehp ctrl b:d:f:irq=0x%x:%x:%x:%x\n", pdev->bus->number,
  1125. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), dev->irq);
  1126. rc = hp_register_read_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
  1127. if (rc) {
  1128. err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
  1129. goto abort_free_ctlr;
  1130. }
  1131. intr_enable = intr_enable | PRSN_DETECT_ENABLE;
  1132. if (ATTN_BUTTN(slot_cap))
  1133. intr_enable = intr_enable | ATTN_BUTTN_ENABLE;
  1134. if (POWER_CTRL(slot_cap))
  1135. intr_enable = intr_enable | PWR_FAULT_DETECT_ENABLE;
  1136. if (MRL_SENS(slot_cap))
  1137. intr_enable = intr_enable | MRL_DETECT_ENABLE;
  1138. temp_word = (temp_word & ~intr_enable) | intr_enable;
  1139. if (pciehp_poll_mode) {
  1140. temp_word = (temp_word & ~HP_INTR_ENABLE) | 0x0;
  1141. } else {
  1142. temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
  1143. }
  1144. /* Unmask Hot-plug Interrupt Enable for the interrupt notification mechanism case */
  1145. rc = hp_register_write_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
  1146. if (rc) {
  1147. err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
  1148. goto abort_free_ctlr;
  1149. }
  1150. rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
  1151. if (rc) {
  1152. err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
  1153. goto abort_free_ctlr;
  1154. }
  1155. temp_word = 0x1F; /* Clear all events */
  1156. rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
  1157. if (rc) {
  1158. err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
  1159. goto abort_free_ctlr;
  1160. }
  1161. if (pciehp_force) {
  1162. dbg("Bypassing BIOS check for pciehp use on %s\n",
  1163. pci_name(ctrl->pci_dev));
  1164. } else {
  1165. rc = pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev);
  1166. if (rc)
  1167. goto abort_free_ctlr;
  1168. }
  1169. /* Add this HPC instance into the HPC list */
  1170. spin_lock(&list_lock);
  1171. if (php_ctlr_list_head == 0) {
  1172. php_ctlr_list_head = php_ctlr;
  1173. p = php_ctlr_list_head;
  1174. p->pnext = NULL;
  1175. } else {
  1176. p = php_ctlr_list_head;
  1177. while (p->pnext)
  1178. p = p->pnext;
  1179. p->pnext = php_ctlr;
  1180. }
  1181. spin_unlock(&list_lock);
  1182. ctlr_seq_num++;
  1183. ctrl->hpc_ctlr_handle = php_ctlr;
  1184. ctrl->hpc_ops = &pciehp_hpc_ops;
  1185. DBG_LEAVE_ROUTINE
  1186. return 0;
  1187. /* We end up here for the many possible ways to fail this API. */
  1188. abort_free_ctlr:
  1189. pcie_cap_base = saved_cap_base;
  1190. kfree(php_ctlr);
  1191. abort:
  1192. DBG_LEAVE_ROUTINE
  1193. return -1;
  1194. }