pciehp_hpc.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  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. acpi_os_free(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. acpi_os_free(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%lx(len=0x%lx)\n", rc,
  1150. pci_resource_start(pdev, rc), pci_resource_len(pdev, rc));
  1151. info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device,
  1152. pdev->subsystem_vendor, pdev->subsystem_device);
  1153. if (pci_enable_device(pdev))
  1154. goto abort_free_ctlr;
  1155. mutex_init(&ctrl->crit_sect);
  1156. /* setup wait queue */
  1157. init_waitqueue_head(&ctrl->queue);
  1158. /* find the IRQ */
  1159. php_ctlr->irq = dev->irq;
  1160. /* Save interrupt callback info */
  1161. php_ctlr->attention_button_callback = pciehp_handle_attention_button;
  1162. php_ctlr->switch_change_callback = pciehp_handle_switch_change;
  1163. php_ctlr->presence_change_callback = pciehp_handle_presence_change;
  1164. php_ctlr->power_fault_callback = pciehp_handle_power_fault;
  1165. php_ctlr->callback_instance_id = instance_id;
  1166. /* return PCI Controller Info */
  1167. php_ctlr->slot_device_offset = 0;
  1168. php_ctlr->num_slots = 1;
  1169. /* Mask Hot-plug Interrupt Enable */
  1170. rc = hp_register_read_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
  1171. if (rc) {
  1172. err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
  1173. goto abort_free_ctlr;
  1174. }
  1175. dbg("%s: SLOT_CTRL %x value read %x\n", __FUNCTION__, SLOT_CTRL(ctrl->cap_base), temp_word);
  1176. temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
  1177. rc = hp_register_write_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
  1178. if (rc) {
  1179. err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
  1180. goto abort_free_ctlr;
  1181. }
  1182. rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
  1183. if (rc) {
  1184. err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
  1185. goto abort_free_ctlr;
  1186. }
  1187. temp_word = 0x1F; /* Clear all events */
  1188. rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
  1189. if (rc) {
  1190. err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
  1191. goto abort_free_ctlr;
  1192. }
  1193. if (pciehp_poll_mode) {/* Install interrupt polling code */
  1194. /* Install and start the interrupt polling timer */
  1195. init_timer(&php_ctlr->int_poll_timer);
  1196. start_int_poll_timer( php_ctlr, 10 ); /* start with 10 second delay */
  1197. } else {
  1198. /* Installs the interrupt handler */
  1199. rc = request_irq(php_ctlr->irq, pcie_isr, SA_SHIRQ, MY_NAME, (void *) ctrl);
  1200. dbg("%s: request_irq %d for hpc%d (returns %d)\n", __FUNCTION__, php_ctlr->irq, ctlr_seq_num, rc);
  1201. if (rc) {
  1202. err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
  1203. goto abort_free_ctlr;
  1204. }
  1205. }
  1206. dbg("pciehp ctrl b:d:f:irq=0x%x:%x:%x:%x\n", pdev->bus->number,
  1207. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), dev->irq);
  1208. rc = hp_register_read_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
  1209. if (rc) {
  1210. err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
  1211. goto abort_free_ctlr;
  1212. }
  1213. intr_enable = intr_enable | PRSN_DETECT_ENABLE;
  1214. if (ATTN_BUTTN(slot_cap))
  1215. intr_enable = intr_enable | ATTN_BUTTN_ENABLE;
  1216. if (POWER_CTRL(slot_cap))
  1217. intr_enable = intr_enable | PWR_FAULT_DETECT_ENABLE;
  1218. if (MRL_SENS(slot_cap))
  1219. intr_enable = intr_enable | MRL_DETECT_ENABLE;
  1220. temp_word = (temp_word & ~intr_enable) | intr_enable;
  1221. if (pciehp_poll_mode) {
  1222. temp_word = (temp_word & ~HP_INTR_ENABLE) | 0x0;
  1223. } else {
  1224. temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
  1225. }
  1226. /* Unmask Hot-plug Interrupt Enable for the interrupt notification mechanism case */
  1227. rc = hp_register_write_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
  1228. if (rc) {
  1229. err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
  1230. goto abort_free_ctlr;
  1231. }
  1232. rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
  1233. if (rc) {
  1234. err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
  1235. goto abort_free_ctlr;
  1236. }
  1237. temp_word = 0x1F; /* Clear all events */
  1238. rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
  1239. if (rc) {
  1240. err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
  1241. goto abort_free_ctlr;
  1242. }
  1243. if (pciehp_force) {
  1244. dbg("Bypassing BIOS check for pciehp use on %s\n",
  1245. pci_name(ctrl->pci_dev));
  1246. } else {
  1247. rc = pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev);
  1248. if (rc)
  1249. goto abort_free_ctlr;
  1250. }
  1251. /* Add this HPC instance into the HPC list */
  1252. spin_lock(&list_lock);
  1253. if (php_ctlr_list_head == 0) {
  1254. php_ctlr_list_head = php_ctlr;
  1255. p = php_ctlr_list_head;
  1256. p->pnext = NULL;
  1257. } else {
  1258. p = php_ctlr_list_head;
  1259. while (p->pnext)
  1260. p = p->pnext;
  1261. p->pnext = php_ctlr;
  1262. }
  1263. spin_unlock(&list_lock);
  1264. ctlr_seq_num++;
  1265. ctrl->hpc_ctlr_handle = php_ctlr;
  1266. ctrl->hpc_ops = &pciehp_hpc_ops;
  1267. DBG_LEAVE_ROUTINE
  1268. return 0;
  1269. /* We end up here for the many possible ways to fail this API. */
  1270. abort_free_ctlr:
  1271. pcie_cap_base = saved_cap_base;
  1272. kfree(php_ctlr);
  1273. abort:
  1274. DBG_LEAVE_ROUTINE
  1275. return -1;
  1276. }