pciehp_hpc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /*
  2. * PCI Express PCI Hot Plug Driver
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. * Copyright (C) 2003-2004 Intel Corporation
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  19. * NON INFRINGEMENT. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
  27. *
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/types.h>
  32. #include <linux/signal.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/timer.h>
  35. #include <linux/pci.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/time.h>
  38. #include "../pci.h"
  39. #include "pciehp.h"
  40. static atomic_t pciehp_num_controllers = ATOMIC_INIT(0);
  41. struct ctrl_reg {
  42. u8 cap_id;
  43. u8 nxt_ptr;
  44. u16 cap_reg;
  45. u32 dev_cap;
  46. u16 dev_ctrl;
  47. u16 dev_status;
  48. u32 lnk_cap;
  49. u16 lnk_ctrl;
  50. u16 lnk_status;
  51. u32 slot_cap;
  52. u16 slot_ctrl;
  53. u16 slot_status;
  54. u16 root_ctrl;
  55. u16 rsvp;
  56. u32 root_status;
  57. } __attribute__ ((packed));
  58. /* offsets to the controller registers based on the above structure layout */
  59. enum ctrl_offsets {
  60. PCIECAPID = offsetof(struct ctrl_reg, cap_id),
  61. NXTCAPPTR = offsetof(struct ctrl_reg, nxt_ptr),
  62. CAPREG = offsetof(struct ctrl_reg, cap_reg),
  63. DEVCAP = offsetof(struct ctrl_reg, dev_cap),
  64. DEVCTRL = offsetof(struct ctrl_reg, dev_ctrl),
  65. DEVSTATUS = offsetof(struct ctrl_reg, dev_status),
  66. LNKCAP = offsetof(struct ctrl_reg, lnk_cap),
  67. LNKCTRL = offsetof(struct ctrl_reg, lnk_ctrl),
  68. LNKSTATUS = offsetof(struct ctrl_reg, lnk_status),
  69. SLOTCAP = offsetof(struct ctrl_reg, slot_cap),
  70. SLOTCTRL = offsetof(struct ctrl_reg, slot_ctrl),
  71. SLOTSTATUS = offsetof(struct ctrl_reg, slot_status),
  72. ROOTCTRL = offsetof(struct ctrl_reg, root_ctrl),
  73. ROOTSTATUS = offsetof(struct ctrl_reg, root_status),
  74. };
  75. static inline int pciehp_readw(struct controller *ctrl, int reg, u16 *value)
  76. {
  77. struct pci_dev *dev = ctrl->pci_dev;
  78. return pci_read_config_word(dev, ctrl->cap_base + reg, value);
  79. }
  80. static inline int pciehp_readl(struct controller *ctrl, int reg, u32 *value)
  81. {
  82. struct pci_dev *dev = ctrl->pci_dev;
  83. return pci_read_config_dword(dev, ctrl->cap_base + reg, value);
  84. }
  85. static inline int pciehp_writew(struct controller *ctrl, int reg, u16 value)
  86. {
  87. struct pci_dev *dev = ctrl->pci_dev;
  88. return pci_write_config_word(dev, ctrl->cap_base + reg, value);
  89. }
  90. static inline int pciehp_writel(struct controller *ctrl, int reg, u32 value)
  91. {
  92. struct pci_dev *dev = ctrl->pci_dev;
  93. return pci_write_config_dword(dev, ctrl->cap_base + reg, value);
  94. }
  95. /* Field definitions in PCI Express Capabilities Register */
  96. #define CAP_VER 0x000F
  97. #define DEV_PORT_TYPE 0x00F0
  98. #define SLOT_IMPL 0x0100
  99. #define MSG_NUM 0x3E00
  100. /* Device or Port Type */
  101. #define NAT_ENDPT 0x00
  102. #define LEG_ENDPT 0x01
  103. #define ROOT_PORT 0x04
  104. #define UP_STREAM 0x05
  105. #define DN_STREAM 0x06
  106. #define PCIE_PCI_BRDG 0x07
  107. #define PCI_PCIE_BRDG 0x10
  108. /* Field definitions in Device Capabilities Register */
  109. #define DATTN_BUTTN_PRSN 0x1000
  110. #define DATTN_LED_PRSN 0x2000
  111. #define DPWR_LED_PRSN 0x4000
  112. /* Field definitions in Link Capabilities Register */
  113. #define MAX_LNK_SPEED 0x000F
  114. #define MAX_LNK_WIDTH 0x03F0
  115. /* Link Width Encoding */
  116. #define LNK_X1 0x01
  117. #define LNK_X2 0x02
  118. #define LNK_X4 0x04
  119. #define LNK_X8 0x08
  120. #define LNK_X12 0x0C
  121. #define LNK_X16 0x10
  122. #define LNK_X32 0x20
  123. /*Field definitions of Link Status Register */
  124. #define LNK_SPEED 0x000F
  125. #define NEG_LINK_WD 0x03F0
  126. #define LNK_TRN_ERR 0x0400
  127. #define LNK_TRN 0x0800
  128. #define SLOT_CLK_CONF 0x1000
  129. /* Field definitions in Slot Capabilities Register */
  130. #define ATTN_BUTTN_PRSN 0x00000001
  131. #define PWR_CTRL_PRSN 0x00000002
  132. #define MRL_SENS_PRSN 0x00000004
  133. #define ATTN_LED_PRSN 0x00000008
  134. #define PWR_LED_PRSN 0x00000010
  135. #define HP_SUPR_RM_SUP 0x00000020
  136. #define HP_CAP 0x00000040
  137. #define SLOT_PWR_VALUE 0x000003F8
  138. #define SLOT_PWR_LIMIT 0x00000C00
  139. #define PSN 0xFFF80000 /* PSN: Physical Slot Number */
  140. /* Field definitions in Slot Control Register */
  141. #define ATTN_BUTTN_ENABLE 0x0001
  142. #define PWR_FAULT_DETECT_ENABLE 0x0002
  143. #define MRL_DETECT_ENABLE 0x0004
  144. #define PRSN_DETECT_ENABLE 0x0008
  145. #define CMD_CMPL_INTR_ENABLE 0x0010
  146. #define HP_INTR_ENABLE 0x0020
  147. #define ATTN_LED_CTRL 0x00C0
  148. #define PWR_LED_CTRL 0x0300
  149. #define PWR_CTRL 0x0400
  150. #define EMI_CTRL 0x0800
  151. /* Attention indicator and Power indicator states */
  152. #define LED_ON 0x01
  153. #define LED_BLINK 0x10
  154. #define LED_OFF 0x11
  155. /* Power Control Command */
  156. #define POWER_ON 0
  157. #define POWER_OFF 0x0400
  158. /* EMI Status defines */
  159. #define EMI_DISENGAGED 0
  160. #define EMI_ENGAGED 1
  161. /* Field definitions in Slot Status Register */
  162. #define ATTN_BUTTN_PRESSED 0x0001
  163. #define PWR_FAULT_DETECTED 0x0002
  164. #define MRL_SENS_CHANGED 0x0004
  165. #define PRSN_DETECT_CHANGED 0x0008
  166. #define CMD_COMPLETED 0x0010
  167. #define MRL_STATE 0x0020
  168. #define PRSN_STATE 0x0040
  169. #define EMI_STATE 0x0080
  170. #define EMI_STATUS_BIT 7
  171. static irqreturn_t pcie_isr(int irq, void *dev_id);
  172. static void start_int_poll_timer(struct controller *ctrl, int sec);
  173. /* This is the interrupt polling timeout function. */
  174. static void int_poll_timeout(unsigned long data)
  175. {
  176. struct controller *ctrl = (struct controller *)data;
  177. /* Poll for interrupt events. regs == NULL => polling */
  178. pcie_isr(0, ctrl);
  179. init_timer(&ctrl->poll_timer);
  180. if (!pciehp_poll_time)
  181. pciehp_poll_time = 2; /* default polling interval is 2 sec */
  182. start_int_poll_timer(ctrl, pciehp_poll_time);
  183. }
  184. /* This function starts the interrupt polling timer. */
  185. static void start_int_poll_timer(struct controller *ctrl, int sec)
  186. {
  187. /* Clamp to sane value */
  188. if ((sec <= 0) || (sec > 60))
  189. sec = 2;
  190. ctrl->poll_timer.function = &int_poll_timeout;
  191. ctrl->poll_timer.data = (unsigned long)ctrl;
  192. ctrl->poll_timer.expires = jiffies + sec * HZ;
  193. add_timer(&ctrl->poll_timer);
  194. }
  195. static inline int pciehp_request_irq(struct controller *ctrl)
  196. {
  197. int retval, irq = ctrl->pcie->irq;
  198. /* Install interrupt polling timer. Start with 10 sec delay */
  199. if (pciehp_poll_mode) {
  200. init_timer(&ctrl->poll_timer);
  201. start_int_poll_timer(ctrl, 10);
  202. return 0;
  203. }
  204. /* Installs the interrupt handler */
  205. retval = request_irq(irq, pcie_isr, IRQF_SHARED, MY_NAME, ctrl);
  206. if (retval)
  207. ctrl_err(ctrl, "Cannot get irq %d for the hotplug controller\n",
  208. irq);
  209. return retval;
  210. }
  211. static inline void pciehp_free_irq(struct controller *ctrl)
  212. {
  213. if (pciehp_poll_mode)
  214. del_timer_sync(&ctrl->poll_timer);
  215. else
  216. free_irq(ctrl->pcie->irq, ctrl);
  217. }
  218. static int pcie_poll_cmd(struct controller *ctrl)
  219. {
  220. u16 slot_status;
  221. int timeout = 1000;
  222. if (!pciehp_readw(ctrl, SLOTSTATUS, &slot_status)) {
  223. if (slot_status & CMD_COMPLETED) {
  224. pciehp_writew(ctrl, SLOTSTATUS, CMD_COMPLETED);
  225. return 1;
  226. }
  227. }
  228. while (timeout > 0) {
  229. msleep(10);
  230. timeout -= 10;
  231. if (!pciehp_readw(ctrl, SLOTSTATUS, &slot_status)) {
  232. if (slot_status & CMD_COMPLETED) {
  233. pciehp_writew(ctrl, SLOTSTATUS, CMD_COMPLETED);
  234. return 1;
  235. }
  236. }
  237. }
  238. return 0; /* timeout */
  239. }
  240. static void pcie_wait_cmd(struct controller *ctrl, int poll)
  241. {
  242. unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
  243. unsigned long timeout = msecs_to_jiffies(msecs);
  244. int rc;
  245. if (poll)
  246. rc = pcie_poll_cmd(ctrl);
  247. else
  248. rc = wait_event_timeout(ctrl->queue, !ctrl->cmd_busy, timeout);
  249. if (!rc)
  250. ctrl_dbg(ctrl, "Command not completed in 1000 msec\n");
  251. }
  252. /**
  253. * pcie_write_cmd - Issue controller command
  254. * @ctrl: controller to which the command is issued
  255. * @cmd: command value written to slot control register
  256. * @mask: bitmask of slot control register to be modified
  257. */
  258. static int pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
  259. {
  260. int retval = 0;
  261. u16 slot_status;
  262. u16 slot_ctrl;
  263. mutex_lock(&ctrl->ctrl_lock);
  264. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  265. if (retval) {
  266. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS register\n",
  267. __func__);
  268. goto out;
  269. }
  270. if (slot_status & CMD_COMPLETED) {
  271. if (!ctrl->no_cmd_complete) {
  272. /*
  273. * After 1 sec and CMD_COMPLETED still not set, just
  274. * proceed forward to issue the next command according
  275. * to spec. Just print out the error message.
  276. */
  277. ctrl_dbg(ctrl,
  278. "%s: CMD_COMPLETED not clear after 1 sec.\n",
  279. __func__);
  280. } else if (!NO_CMD_CMPL(ctrl)) {
  281. /*
  282. * This controller semms to notify of command completed
  283. * event even though it supports none of power
  284. * controller, attention led, power led and EMI.
  285. */
  286. ctrl_dbg(ctrl, "%s: Unexpected CMD_COMPLETED. Need to "
  287. "wait for command completed event.\n",
  288. __func__);
  289. ctrl->no_cmd_complete = 0;
  290. } else {
  291. ctrl_dbg(ctrl, "%s: Unexpected CMD_COMPLETED. Maybe "
  292. "the controller is broken.\n", __func__);
  293. }
  294. }
  295. retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
  296. if (retval) {
  297. ctrl_err(ctrl, "%s: Cannot read SLOTCTRL register\n", __func__);
  298. goto out;
  299. }
  300. slot_ctrl &= ~mask;
  301. slot_ctrl |= (cmd & mask);
  302. ctrl->cmd_busy = 1;
  303. smp_mb();
  304. retval = pciehp_writew(ctrl, SLOTCTRL, slot_ctrl);
  305. if (retval)
  306. ctrl_err(ctrl, "%s: Cannot write to SLOTCTRL register\n",
  307. __func__);
  308. /*
  309. * Wait for command completion.
  310. */
  311. if (!retval && !ctrl->no_cmd_complete) {
  312. int poll = 0;
  313. /*
  314. * if hotplug interrupt is not enabled or command
  315. * completed interrupt is not enabled, we need to poll
  316. * command completed event.
  317. */
  318. if (!(slot_ctrl & HP_INTR_ENABLE) ||
  319. !(slot_ctrl & CMD_CMPL_INTR_ENABLE))
  320. poll = 1;
  321. pcie_wait_cmd(ctrl, poll);
  322. }
  323. out:
  324. mutex_unlock(&ctrl->ctrl_lock);
  325. return retval;
  326. }
  327. static int hpc_check_lnk_status(struct controller *ctrl)
  328. {
  329. u16 lnk_status;
  330. int retval = 0;
  331. retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
  332. if (retval) {
  333. ctrl_err(ctrl, "%s: Cannot read LNKSTATUS register\n",
  334. __func__);
  335. return retval;
  336. }
  337. ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
  338. if ( (lnk_status & LNK_TRN) || (lnk_status & LNK_TRN_ERR) ||
  339. !(lnk_status & NEG_LINK_WD)) {
  340. ctrl_err(ctrl, "%s : Link Training Error occurs \n", __func__);
  341. retval = -1;
  342. return retval;
  343. }
  344. return retval;
  345. }
  346. static int hpc_get_attention_status(struct slot *slot, u8 *status)
  347. {
  348. struct controller *ctrl = slot->ctrl;
  349. u16 slot_ctrl;
  350. u8 atten_led_state;
  351. int retval = 0;
  352. retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
  353. if (retval) {
  354. ctrl_err(ctrl, "%s: Cannot read SLOTCTRL register\n", __func__);
  355. return retval;
  356. }
  357. ctrl_dbg(ctrl, "%s: SLOTCTRL %x, value read %x\n",
  358. __func__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
  359. atten_led_state = (slot_ctrl & ATTN_LED_CTRL) >> 6;
  360. switch (atten_led_state) {
  361. case 0:
  362. *status = 0xFF; /* Reserved */
  363. break;
  364. case 1:
  365. *status = 1; /* On */
  366. break;
  367. case 2:
  368. *status = 2; /* Blink */
  369. break;
  370. case 3:
  371. *status = 0; /* Off */
  372. break;
  373. default:
  374. *status = 0xFF;
  375. break;
  376. }
  377. return 0;
  378. }
  379. static int hpc_get_power_status(struct slot *slot, u8 *status)
  380. {
  381. struct controller *ctrl = slot->ctrl;
  382. u16 slot_ctrl;
  383. u8 pwr_state;
  384. int retval = 0;
  385. retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
  386. if (retval) {
  387. ctrl_err(ctrl, "%s: Cannot read SLOTCTRL register\n", __func__);
  388. return retval;
  389. }
  390. ctrl_dbg(ctrl, "%s: SLOTCTRL %x value read %x\n",
  391. __func__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
  392. pwr_state = (slot_ctrl & PWR_CTRL) >> 10;
  393. switch (pwr_state) {
  394. case 0:
  395. *status = 1;
  396. break;
  397. case 1:
  398. *status = 0;
  399. break;
  400. default:
  401. *status = 0xFF;
  402. break;
  403. }
  404. return retval;
  405. }
  406. static int hpc_get_latch_status(struct slot *slot, u8 *status)
  407. {
  408. struct controller *ctrl = slot->ctrl;
  409. u16 slot_status;
  410. int retval = 0;
  411. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  412. if (retval) {
  413. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS register\n",
  414. __func__);
  415. return retval;
  416. }
  417. *status = (((slot_status & MRL_STATE) >> 5) == 0) ? 0 : 1;
  418. return 0;
  419. }
  420. static int hpc_get_adapter_status(struct slot *slot, u8 *status)
  421. {
  422. struct controller *ctrl = slot->ctrl;
  423. u16 slot_status;
  424. u8 card_state;
  425. int retval = 0;
  426. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  427. if (retval) {
  428. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS register\n",
  429. __func__);
  430. return retval;
  431. }
  432. card_state = (u8)((slot_status & PRSN_STATE) >> 6);
  433. *status = (card_state == 1) ? 1 : 0;
  434. return 0;
  435. }
  436. static int hpc_query_power_fault(struct slot *slot)
  437. {
  438. struct controller *ctrl = slot->ctrl;
  439. u16 slot_status;
  440. u8 pwr_fault;
  441. int retval = 0;
  442. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  443. if (retval) {
  444. ctrl_err(ctrl, "%s: Cannot check for power fault\n", __func__);
  445. return retval;
  446. }
  447. pwr_fault = (u8)((slot_status & PWR_FAULT_DETECTED) >> 1);
  448. return pwr_fault;
  449. }
  450. static int hpc_get_emi_status(struct slot *slot, u8 *status)
  451. {
  452. struct controller *ctrl = slot->ctrl;
  453. u16 slot_status;
  454. int retval = 0;
  455. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  456. if (retval) {
  457. ctrl_err(ctrl, "%s : Cannot check EMI status\n", __func__);
  458. return retval;
  459. }
  460. *status = (slot_status & EMI_STATE) >> EMI_STATUS_BIT;
  461. return retval;
  462. }
  463. static int hpc_toggle_emi(struct slot *slot)
  464. {
  465. u16 slot_cmd;
  466. u16 cmd_mask;
  467. int rc;
  468. slot_cmd = EMI_CTRL;
  469. cmd_mask = EMI_CTRL;
  470. rc = pcie_write_cmd(slot->ctrl, slot_cmd, cmd_mask);
  471. slot->last_emi_toggle = get_seconds();
  472. return rc;
  473. }
  474. static int hpc_set_attention_status(struct slot *slot, u8 value)
  475. {
  476. struct controller *ctrl = slot->ctrl;
  477. u16 slot_cmd;
  478. u16 cmd_mask;
  479. int rc;
  480. cmd_mask = ATTN_LED_CTRL;
  481. switch (value) {
  482. case 0 : /* turn off */
  483. slot_cmd = 0x00C0;
  484. break;
  485. case 1: /* turn on */
  486. slot_cmd = 0x0040;
  487. break;
  488. case 2: /* turn blink */
  489. slot_cmd = 0x0080;
  490. break;
  491. default:
  492. return -1;
  493. }
  494. rc = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  495. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  496. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  497. return rc;
  498. }
  499. static void hpc_set_green_led_on(struct slot *slot)
  500. {
  501. struct controller *ctrl = slot->ctrl;
  502. u16 slot_cmd;
  503. u16 cmd_mask;
  504. slot_cmd = 0x0100;
  505. cmd_mask = PWR_LED_CTRL;
  506. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  507. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  508. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  509. }
  510. static void hpc_set_green_led_off(struct slot *slot)
  511. {
  512. struct controller *ctrl = slot->ctrl;
  513. u16 slot_cmd;
  514. u16 cmd_mask;
  515. slot_cmd = 0x0300;
  516. cmd_mask = PWR_LED_CTRL;
  517. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  518. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  519. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  520. }
  521. static void hpc_set_green_led_blink(struct slot *slot)
  522. {
  523. struct controller *ctrl = slot->ctrl;
  524. u16 slot_cmd;
  525. u16 cmd_mask;
  526. slot_cmd = 0x0200;
  527. cmd_mask = PWR_LED_CTRL;
  528. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  529. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  530. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  531. }
  532. static int hpc_power_on_slot(struct slot * slot)
  533. {
  534. struct controller *ctrl = slot->ctrl;
  535. u16 slot_cmd;
  536. u16 cmd_mask;
  537. u16 slot_status;
  538. int retval = 0;
  539. ctrl_dbg(ctrl, "%s: slot->hp_slot %x\n", __func__, slot->hp_slot);
  540. /* Clear sticky power-fault bit from previous power failures */
  541. retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
  542. if (retval) {
  543. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS register\n",
  544. __func__);
  545. return retval;
  546. }
  547. slot_status &= PWR_FAULT_DETECTED;
  548. if (slot_status) {
  549. retval = pciehp_writew(ctrl, SLOTSTATUS, slot_status);
  550. if (retval) {
  551. ctrl_err(ctrl,
  552. "%s: Cannot write to SLOTSTATUS register\n",
  553. __func__);
  554. return retval;
  555. }
  556. }
  557. slot_cmd = POWER_ON;
  558. cmd_mask = PWR_CTRL;
  559. /* Enable detection that we turned off at slot power-off time */
  560. if (!pciehp_poll_mode) {
  561. slot_cmd |= (PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE |
  562. PRSN_DETECT_ENABLE);
  563. cmd_mask |= (PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE |
  564. PRSN_DETECT_ENABLE);
  565. }
  566. retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  567. if (retval) {
  568. ctrl_err(ctrl, "%s: Write %x command failed!\n",
  569. __func__, slot_cmd);
  570. return -1;
  571. }
  572. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  573. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  574. return retval;
  575. }
  576. static inline int pcie_mask_bad_dllp(struct controller *ctrl)
  577. {
  578. struct pci_dev *dev = ctrl->pci_dev;
  579. int pos;
  580. u32 reg;
  581. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  582. if (!pos)
  583. return 0;
  584. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg);
  585. if (reg & PCI_ERR_COR_BAD_DLLP)
  586. return 0;
  587. reg |= PCI_ERR_COR_BAD_DLLP;
  588. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg);
  589. return 1;
  590. }
  591. static inline void pcie_unmask_bad_dllp(struct controller *ctrl)
  592. {
  593. struct pci_dev *dev = ctrl->pci_dev;
  594. u32 reg;
  595. int pos;
  596. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  597. if (!pos)
  598. return;
  599. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg);
  600. if (!(reg & PCI_ERR_COR_BAD_DLLP))
  601. return;
  602. reg &= ~PCI_ERR_COR_BAD_DLLP;
  603. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg);
  604. }
  605. static int hpc_power_off_slot(struct slot * slot)
  606. {
  607. struct controller *ctrl = slot->ctrl;
  608. u16 slot_cmd;
  609. u16 cmd_mask;
  610. int retval = 0;
  611. int changed;
  612. ctrl_dbg(ctrl, "%s: slot->hp_slot %x\n", __func__, slot->hp_slot);
  613. /*
  614. * Set Bad DLLP Mask bit in Correctable Error Mask
  615. * Register. This is the workaround against Bad DLLP error
  616. * that sometimes happens during turning power off the slot
  617. * which conforms to PCI Express 1.0a spec.
  618. */
  619. changed = pcie_mask_bad_dllp(ctrl);
  620. slot_cmd = POWER_OFF;
  621. cmd_mask = PWR_CTRL;
  622. /*
  623. * If we get MRL or presence detect interrupts now, the isr
  624. * will notice the sticky power-fault bit too and issue power
  625. * indicator change commands. This will lead to an endless loop
  626. * of command completions, since the power-fault bit remains on
  627. * till the slot is powered on again.
  628. */
  629. if (!pciehp_poll_mode) {
  630. slot_cmd &= ~(PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE |
  631. PRSN_DETECT_ENABLE);
  632. cmd_mask |= (PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE |
  633. PRSN_DETECT_ENABLE);
  634. }
  635. retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  636. if (retval) {
  637. ctrl_err(ctrl, "%s: Write command failed!\n", __func__);
  638. retval = -1;
  639. goto out;
  640. }
  641. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  642. __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
  643. out:
  644. if (changed)
  645. pcie_unmask_bad_dllp(ctrl);
  646. return retval;
  647. }
  648. static irqreturn_t pcie_isr(int irq, void *dev_id)
  649. {
  650. struct controller *ctrl = (struct controller *)dev_id;
  651. u16 detected, intr_loc;
  652. struct slot *p_slot;
  653. /*
  654. * In order to guarantee that all interrupt events are
  655. * serviced, we need to re-inspect Slot Status register after
  656. * clearing what is presumed to be the last pending interrupt.
  657. */
  658. intr_loc = 0;
  659. do {
  660. if (pciehp_readw(ctrl, SLOTSTATUS, &detected)) {
  661. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS\n",
  662. __func__);
  663. return IRQ_NONE;
  664. }
  665. detected &= (ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED |
  666. MRL_SENS_CHANGED | PRSN_DETECT_CHANGED |
  667. CMD_COMPLETED);
  668. intr_loc |= detected;
  669. if (!intr_loc)
  670. return IRQ_NONE;
  671. if (detected && pciehp_writew(ctrl, SLOTSTATUS, detected)) {
  672. ctrl_err(ctrl, "%s: Cannot write to SLOTSTATUS\n",
  673. __func__);
  674. return IRQ_NONE;
  675. }
  676. } while (detected);
  677. ctrl_dbg(ctrl, "%s: intr_loc %x\n", __func__, intr_loc);
  678. /* Check Command Complete Interrupt Pending */
  679. if (intr_loc & CMD_COMPLETED) {
  680. ctrl->cmd_busy = 0;
  681. smp_mb();
  682. wake_up(&ctrl->queue);
  683. }
  684. if (!(intr_loc & ~CMD_COMPLETED))
  685. return IRQ_HANDLED;
  686. p_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
  687. /* Check MRL Sensor Changed */
  688. if (intr_loc & MRL_SENS_CHANGED)
  689. pciehp_handle_switch_change(p_slot);
  690. /* Check Attention Button Pressed */
  691. if (intr_loc & ATTN_BUTTN_PRESSED)
  692. pciehp_handle_attention_button(p_slot);
  693. /* Check Presence Detect Changed */
  694. if (intr_loc & PRSN_DETECT_CHANGED)
  695. pciehp_handle_presence_change(p_slot);
  696. /* Check Power Fault Detected */
  697. if (intr_loc & PWR_FAULT_DETECTED)
  698. pciehp_handle_power_fault(p_slot);
  699. return IRQ_HANDLED;
  700. }
  701. static int hpc_get_max_lnk_speed(struct slot *slot, enum pci_bus_speed *value)
  702. {
  703. struct controller *ctrl = slot->ctrl;
  704. enum pcie_link_speed lnk_speed;
  705. u32 lnk_cap;
  706. int retval = 0;
  707. retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
  708. if (retval) {
  709. ctrl_err(ctrl, "%s: Cannot read LNKCAP register\n", __func__);
  710. return retval;
  711. }
  712. switch (lnk_cap & 0x000F) {
  713. case 1:
  714. lnk_speed = PCIE_2PT5GB;
  715. break;
  716. default:
  717. lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
  718. break;
  719. }
  720. *value = lnk_speed;
  721. ctrl_dbg(ctrl, "Max link speed = %d\n", lnk_speed);
  722. return retval;
  723. }
  724. static int hpc_get_max_lnk_width(struct slot *slot,
  725. enum pcie_link_width *value)
  726. {
  727. struct controller *ctrl = slot->ctrl;
  728. enum pcie_link_width lnk_wdth;
  729. u32 lnk_cap;
  730. int retval = 0;
  731. retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
  732. if (retval) {
  733. ctrl_err(ctrl, "%s: Cannot read LNKCAP register\n", __func__);
  734. return retval;
  735. }
  736. switch ((lnk_cap & 0x03F0) >> 4){
  737. case 0:
  738. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  739. break;
  740. case 1:
  741. lnk_wdth = PCIE_LNK_X1;
  742. break;
  743. case 2:
  744. lnk_wdth = PCIE_LNK_X2;
  745. break;
  746. case 4:
  747. lnk_wdth = PCIE_LNK_X4;
  748. break;
  749. case 8:
  750. lnk_wdth = PCIE_LNK_X8;
  751. break;
  752. case 12:
  753. lnk_wdth = PCIE_LNK_X12;
  754. break;
  755. case 16:
  756. lnk_wdth = PCIE_LNK_X16;
  757. break;
  758. case 32:
  759. lnk_wdth = PCIE_LNK_X32;
  760. break;
  761. default:
  762. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  763. break;
  764. }
  765. *value = lnk_wdth;
  766. ctrl_dbg(ctrl, "Max link width = %d\n", lnk_wdth);
  767. return retval;
  768. }
  769. static int hpc_get_cur_lnk_speed(struct slot *slot, enum pci_bus_speed *value)
  770. {
  771. struct controller *ctrl = slot->ctrl;
  772. enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
  773. int retval = 0;
  774. u16 lnk_status;
  775. retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
  776. if (retval) {
  777. ctrl_err(ctrl, "%s: Cannot read LNKSTATUS register\n",
  778. __func__);
  779. return retval;
  780. }
  781. switch (lnk_status & 0x0F) {
  782. case 1:
  783. lnk_speed = PCIE_2PT5GB;
  784. break;
  785. default:
  786. lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
  787. break;
  788. }
  789. *value = lnk_speed;
  790. ctrl_dbg(ctrl, "Current link speed = %d\n", lnk_speed);
  791. return retval;
  792. }
  793. static int hpc_get_cur_lnk_width(struct slot *slot,
  794. enum pcie_link_width *value)
  795. {
  796. struct controller *ctrl = slot->ctrl;
  797. enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  798. int retval = 0;
  799. u16 lnk_status;
  800. retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
  801. if (retval) {
  802. ctrl_err(ctrl, "%s: Cannot read LNKSTATUS register\n",
  803. __func__);
  804. return retval;
  805. }
  806. switch ((lnk_status & 0x03F0) >> 4){
  807. case 0:
  808. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  809. break;
  810. case 1:
  811. lnk_wdth = PCIE_LNK_X1;
  812. break;
  813. case 2:
  814. lnk_wdth = PCIE_LNK_X2;
  815. break;
  816. case 4:
  817. lnk_wdth = PCIE_LNK_X4;
  818. break;
  819. case 8:
  820. lnk_wdth = PCIE_LNK_X8;
  821. break;
  822. case 12:
  823. lnk_wdth = PCIE_LNK_X12;
  824. break;
  825. case 16:
  826. lnk_wdth = PCIE_LNK_X16;
  827. break;
  828. case 32:
  829. lnk_wdth = PCIE_LNK_X32;
  830. break;
  831. default:
  832. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  833. break;
  834. }
  835. *value = lnk_wdth;
  836. ctrl_dbg(ctrl, "Current link width = %d\n", lnk_wdth);
  837. return retval;
  838. }
  839. static void pcie_release_ctrl(struct controller *ctrl);
  840. static struct hpc_ops pciehp_hpc_ops = {
  841. .power_on_slot = hpc_power_on_slot,
  842. .power_off_slot = hpc_power_off_slot,
  843. .set_attention_status = hpc_set_attention_status,
  844. .get_power_status = hpc_get_power_status,
  845. .get_attention_status = hpc_get_attention_status,
  846. .get_latch_status = hpc_get_latch_status,
  847. .get_adapter_status = hpc_get_adapter_status,
  848. .get_emi_status = hpc_get_emi_status,
  849. .toggle_emi = hpc_toggle_emi,
  850. .get_max_bus_speed = hpc_get_max_lnk_speed,
  851. .get_cur_bus_speed = hpc_get_cur_lnk_speed,
  852. .get_max_lnk_width = hpc_get_max_lnk_width,
  853. .get_cur_lnk_width = hpc_get_cur_lnk_width,
  854. .query_power_fault = hpc_query_power_fault,
  855. .green_led_on = hpc_set_green_led_on,
  856. .green_led_off = hpc_set_green_led_off,
  857. .green_led_blink = hpc_set_green_led_blink,
  858. .release_ctlr = pcie_release_ctrl,
  859. .check_lnk_status = hpc_check_lnk_status,
  860. };
  861. int pcie_enable_notification(struct controller *ctrl)
  862. {
  863. u16 cmd, mask;
  864. cmd = PRSN_DETECT_ENABLE;
  865. if (ATTN_BUTTN(ctrl))
  866. cmd |= ATTN_BUTTN_ENABLE;
  867. if (POWER_CTRL(ctrl))
  868. cmd |= PWR_FAULT_DETECT_ENABLE;
  869. if (MRL_SENS(ctrl))
  870. cmd |= MRL_DETECT_ENABLE;
  871. if (!pciehp_poll_mode)
  872. cmd |= HP_INTR_ENABLE | CMD_CMPL_INTR_ENABLE;
  873. mask = PRSN_DETECT_ENABLE | ATTN_BUTTN_ENABLE | MRL_DETECT_ENABLE |
  874. PWR_FAULT_DETECT_ENABLE | HP_INTR_ENABLE | CMD_CMPL_INTR_ENABLE;
  875. if (pcie_write_cmd(ctrl, cmd, mask)) {
  876. ctrl_err(ctrl, "%s: Cannot enable software notification\n",
  877. __func__);
  878. return -1;
  879. }
  880. return 0;
  881. }
  882. static void pcie_disable_notification(struct controller *ctrl)
  883. {
  884. u16 mask;
  885. mask = PRSN_DETECT_ENABLE | ATTN_BUTTN_ENABLE | MRL_DETECT_ENABLE |
  886. PWR_FAULT_DETECT_ENABLE | HP_INTR_ENABLE | CMD_CMPL_INTR_ENABLE;
  887. if (pcie_write_cmd(ctrl, 0, mask))
  888. ctrl_warn(ctrl, "%s: Cannot disable software notification\n",
  889. __func__);
  890. }
  891. static int pcie_init_notification(struct controller *ctrl)
  892. {
  893. if (pciehp_request_irq(ctrl))
  894. return -1;
  895. if (pcie_enable_notification(ctrl)) {
  896. pciehp_free_irq(ctrl);
  897. return -1;
  898. }
  899. return 0;
  900. }
  901. static void pcie_shutdown_notification(struct controller *ctrl)
  902. {
  903. pcie_disable_notification(ctrl);
  904. pciehp_free_irq(ctrl);
  905. }
  906. static int pcie_init_slot(struct controller *ctrl)
  907. {
  908. struct slot *slot;
  909. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  910. if (!slot)
  911. return -ENOMEM;
  912. slot->hp_slot = 0;
  913. slot->ctrl = ctrl;
  914. slot->bus = ctrl->pci_dev->subordinate->number;
  915. slot->device = ctrl->slot_device_offset + slot->hp_slot;
  916. slot->hpc_ops = ctrl->hpc_ops;
  917. slot->number = ctrl->first_slot;
  918. mutex_init(&slot->lock);
  919. INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
  920. list_add(&slot->slot_list, &ctrl->slot_list);
  921. return 0;
  922. }
  923. static void pcie_cleanup_slot(struct controller *ctrl)
  924. {
  925. struct slot *slot;
  926. slot = list_first_entry(&ctrl->slot_list, struct slot, slot_list);
  927. list_del(&slot->slot_list);
  928. cancel_delayed_work(&slot->work);
  929. flush_scheduled_work();
  930. flush_workqueue(pciehp_wq);
  931. kfree(slot);
  932. }
  933. static inline void dbg_ctrl(struct controller *ctrl)
  934. {
  935. int i;
  936. u16 reg16;
  937. struct pci_dev *pdev = ctrl->pci_dev;
  938. if (!pciehp_debug)
  939. return;
  940. ctrl_info(ctrl, "Hotplug Controller:\n");
  941. ctrl_info(ctrl, " Seg/Bus/Dev/Func/IRQ : %s IRQ %d\n",
  942. pci_name(pdev), pdev->irq);
  943. ctrl_info(ctrl, " Vendor ID : 0x%04x\n", pdev->vendor);
  944. ctrl_info(ctrl, " Device ID : 0x%04x\n", pdev->device);
  945. ctrl_info(ctrl, " Subsystem ID : 0x%04x\n",
  946. pdev->subsystem_device);
  947. ctrl_info(ctrl, " Subsystem Vendor ID : 0x%04x\n",
  948. pdev->subsystem_vendor);
  949. ctrl_info(ctrl, " PCIe Cap offset : 0x%02x\n", ctrl->cap_base);
  950. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  951. if (!pci_resource_len(pdev, i))
  952. continue;
  953. ctrl_info(ctrl, " PCI resource [%d] : 0x%llx@0x%llx\n",
  954. i, (unsigned long long)pci_resource_len(pdev, i),
  955. (unsigned long long)pci_resource_start(pdev, i));
  956. }
  957. ctrl_info(ctrl, "Slot Capabilities : 0x%08x\n", ctrl->slot_cap);
  958. ctrl_info(ctrl, " Physical Slot Number : %d\n", ctrl->first_slot);
  959. ctrl_info(ctrl, " Attention Button : %3s\n",
  960. ATTN_BUTTN(ctrl) ? "yes" : "no");
  961. ctrl_info(ctrl, " Power Controller : %3s\n",
  962. POWER_CTRL(ctrl) ? "yes" : "no");
  963. ctrl_info(ctrl, " MRL Sensor : %3s\n",
  964. MRL_SENS(ctrl) ? "yes" : "no");
  965. ctrl_info(ctrl, " Attention Indicator : %3s\n",
  966. ATTN_LED(ctrl) ? "yes" : "no");
  967. ctrl_info(ctrl, " Power Indicator : %3s\n",
  968. PWR_LED(ctrl) ? "yes" : "no");
  969. ctrl_info(ctrl, " Hot-Plug Surprise : %3s\n",
  970. HP_SUPR_RM(ctrl) ? "yes" : "no");
  971. ctrl_info(ctrl, " EMI Present : %3s\n",
  972. EMI(ctrl) ? "yes" : "no");
  973. ctrl_info(ctrl, " Command Completed : %3s\n",
  974. NO_CMD_CMPL(ctrl) ? "no" : "yes");
  975. pciehp_readw(ctrl, SLOTSTATUS, &reg16);
  976. ctrl_info(ctrl, "Slot Status : 0x%04x\n", reg16);
  977. pciehp_readw(ctrl, SLOTCTRL, &reg16);
  978. ctrl_info(ctrl, "Slot Control : 0x%04x\n", reg16);
  979. }
  980. struct controller *pcie_init(struct pcie_device *dev)
  981. {
  982. struct controller *ctrl;
  983. u32 slot_cap;
  984. struct pci_dev *pdev = dev->port;
  985. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  986. if (!ctrl) {
  987. dev_err(&dev->device, "%s : out of memory\n", __func__);
  988. goto abort;
  989. }
  990. INIT_LIST_HEAD(&ctrl->slot_list);
  991. ctrl->pcie = dev;
  992. ctrl->pci_dev = pdev;
  993. ctrl->cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  994. if (!ctrl->cap_base) {
  995. ctrl_err(ctrl, "%s: Cannot find PCI Express capability\n",
  996. __func__);
  997. goto abort_ctrl;
  998. }
  999. if (pciehp_readl(ctrl, SLOTCAP, &slot_cap)) {
  1000. ctrl_err(ctrl, "%s: Cannot read SLOTCAP register\n", __func__);
  1001. goto abort_ctrl;
  1002. }
  1003. ctrl->slot_cap = slot_cap;
  1004. ctrl->first_slot = slot_cap >> 19;
  1005. ctrl->slot_device_offset = 0;
  1006. ctrl->num_slots = 1;
  1007. ctrl->hpc_ops = &pciehp_hpc_ops;
  1008. mutex_init(&ctrl->crit_sect);
  1009. mutex_init(&ctrl->ctrl_lock);
  1010. init_waitqueue_head(&ctrl->queue);
  1011. dbg_ctrl(ctrl);
  1012. /*
  1013. * Controller doesn't notify of command completion if the "No
  1014. * Command Completed Support" bit is set in Slot Capability
  1015. * register or the controller supports none of power
  1016. * controller, attention led, power led and EMI.
  1017. */
  1018. if (NO_CMD_CMPL(ctrl) ||
  1019. !(POWER_CTRL(ctrl) | ATTN_LED(ctrl) | PWR_LED(ctrl) | EMI(ctrl)))
  1020. ctrl->no_cmd_complete = 1;
  1021. /* Clear all remaining event bits in Slot Status register */
  1022. if (pciehp_writew(ctrl, SLOTSTATUS, 0x1f))
  1023. goto abort_ctrl;
  1024. /* Disable sotfware notification */
  1025. pcie_disable_notification(ctrl);
  1026. /*
  1027. * If this is the first controller to be initialized,
  1028. * initialize the pciehp work queue
  1029. */
  1030. if (atomic_add_return(1, &pciehp_num_controllers) == 1) {
  1031. pciehp_wq = create_singlethread_workqueue("pciehpd");
  1032. if (!pciehp_wq)
  1033. goto abort_ctrl;
  1034. }
  1035. ctrl_info(ctrl, "HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n",
  1036. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  1037. pdev->subsystem_device);
  1038. if (pcie_init_slot(ctrl))
  1039. goto abort_ctrl;
  1040. if (pcie_init_notification(ctrl))
  1041. goto abort_slot;
  1042. return ctrl;
  1043. abort_slot:
  1044. pcie_cleanup_slot(ctrl);
  1045. abort_ctrl:
  1046. kfree(ctrl);
  1047. abort:
  1048. return NULL;
  1049. }
  1050. void pcie_release_ctrl(struct controller *ctrl)
  1051. {
  1052. pcie_shutdown_notification(ctrl);
  1053. pcie_cleanup_slot(ctrl);
  1054. /*
  1055. * If this is the last controller to be released, destroy the
  1056. * pciehp work queue
  1057. */
  1058. if (atomic_dec_and_test(&pciehp_num_controllers))
  1059. destroy_workqueue(pciehp_wq);
  1060. kfree(ctrl);
  1061. }