pciehp_hpc.c 41 KB

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