pciehp_hpc.c 42 KB

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