pciehp_hpc.c 38 KB

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