ibmphp_hpc.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * IBM Hot Plug Controller Driver
  4. *
  5. * Written By: Jyoti Shah, IBM Corporation
  6. *
  7. * Copyright (C) 2001-2003 IBM Corp.
  8. *
  9. * All rights reserved.
  10. *
  11. * Send feedback to <[email protected]>
  12. * <[email protected]>
  13. *
  14. */
  15. #include <linux/wait.h>
  16. #include <linux/time.h>
  17. #include <linux/completion.h>
  18. #include <linux/delay.h>
  19. #include <linux/module.h>
  20. #include <linux/pci.h>
  21. #include <linux/init.h>
  22. #include <linux/mutex.h>
  23. #include <linux/sched.h>
  24. #include <linux/kthread.h>
  25. #include "ibmphp.h"
  26. static int to_debug = 0;
  27. #define debug_polling(fmt, arg...) do { if (to_debug) debug(fmt, arg); } while (0)
  28. //----------------------------------------------------------------------------
  29. // timeout values
  30. //----------------------------------------------------------------------------
  31. #define CMD_COMPLETE_TOUT_SEC 60 // give HPC 60 sec to finish cmd
  32. #define HPC_CTLR_WORKING_TOUT 60 // give HPC 60 sec to finish cmd
  33. #define HPC_GETACCESS_TIMEOUT 60 // seconds
  34. #define POLL_INTERVAL_SEC 2 // poll HPC every 2 seconds
  35. #define POLL_LATCH_CNT 5 // poll latch 5 times, then poll slots
  36. //----------------------------------------------------------------------------
  37. // Winnipeg Architected Register Offsets
  38. //----------------------------------------------------------------------------
  39. #define WPG_I2CMBUFL_OFFSET 0x08 // I2C Message Buffer Low
  40. #define WPG_I2CMOSUP_OFFSET 0x10 // I2C Master Operation Setup Reg
  41. #define WPG_I2CMCNTL_OFFSET 0x20 // I2C Master Control Register
  42. #define WPG_I2CPARM_OFFSET 0x40 // I2C Parameter Register
  43. #define WPG_I2CSTAT_OFFSET 0x70 // I2C Status Register
  44. //----------------------------------------------------------------------------
  45. // Winnipeg Store Type commands (Add this commands to the register offset)
  46. //----------------------------------------------------------------------------
  47. #define WPG_I2C_AND 0x1000 // I2C AND operation
  48. #define WPG_I2C_OR 0x2000 // I2C OR operation
  49. //----------------------------------------------------------------------------
  50. // Command set for I2C Master Operation Setup Register
  51. //----------------------------------------------------------------------------
  52. #define WPG_READATADDR_MASK 0x00010000 // read,bytes,I2C shifted,index
  53. #define WPG_WRITEATADDR_MASK 0x40010000 // write,bytes,I2C shifted,index
  54. #define WPG_READDIRECT_MASK 0x10010000
  55. #define WPG_WRITEDIRECT_MASK 0x60010000
  56. //----------------------------------------------------------------------------
  57. // bit masks for I2C Master Control Register
  58. //----------------------------------------------------------------------------
  59. #define WPG_I2CMCNTL_STARTOP_MASK 0x00000002 // Start the Operation
  60. //----------------------------------------------------------------------------
  61. //
  62. //----------------------------------------------------------------------------
  63. #define WPG_I2C_IOREMAP_SIZE 0x2044 // size of linear address interval
  64. //----------------------------------------------------------------------------
  65. // command index
  66. //----------------------------------------------------------------------------
  67. #define WPG_1ST_SLOT_INDEX 0x01 // index - 1st slot for ctlr
  68. #define WPG_CTLR_INDEX 0x0F // index - ctlr
  69. #define WPG_1ST_EXTSLOT_INDEX 0x10 // index - 1st ext slot for ctlr
  70. #define WPG_1ST_BUS_INDEX 0x1F // index - 1st bus for ctlr
  71. //----------------------------------------------------------------------------
  72. // macro utilities
  73. //----------------------------------------------------------------------------
  74. // if bits 20,22,25,26,27,29,30 are OFF return 1
  75. #define HPC_I2CSTATUS_CHECK(s) ((u8)((s & 0x00000A76) ? 0 : 1))
  76. //----------------------------------------------------------------------------
  77. // global variables
  78. //----------------------------------------------------------------------------
  79. static DEFINE_MUTEX(sem_hpcaccess); // lock access to HPC
  80. static DEFINE_MUTEX(operations_mutex); // lock all operations and
  81. // access to data structures
  82. static DECLARE_COMPLETION(exit_complete); // make sure polling thread goes away
  83. static struct task_struct *ibmphp_poll_thread;
  84. //----------------------------------------------------------------------------
  85. // local function prototypes
  86. //----------------------------------------------------------------------------
  87. static u8 i2c_ctrl_read(struct controller *, void __iomem *, u8);
  88. static u8 i2c_ctrl_write(struct controller *, void __iomem *, u8, u8);
  89. static u8 hpc_writecmdtoindex(u8, u8);
  90. static u8 hpc_readcmdtoindex(u8, u8);
  91. static void get_hpc_access(void);
  92. static void free_hpc_access(void);
  93. static int poll_hpc(void *data);
  94. static int process_changeinstatus(struct slot *, struct slot *);
  95. static int process_changeinlatch(u8, u8, struct controller *);
  96. static int hpc_wait_ctlr_notworking(int, struct controller *, void __iomem *, u8 *);
  97. //----------------------------------------------------------------------------
  98. /*----------------------------------------------------------------------
  99. * Name: i2c_ctrl_read
  100. *
  101. * Action: read from HPC over I2C
  102. *
  103. *---------------------------------------------------------------------*/
  104. static u8 i2c_ctrl_read(struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index)
  105. {
  106. u8 status;
  107. int i;
  108. void __iomem *wpg_addr; // base addr + offset
  109. unsigned long wpg_data; // data to/from WPG LOHI format
  110. unsigned long ultemp;
  111. unsigned long data; // actual data HILO format
  112. debug_polling("%s - Entry WPGBbar[%p] index[%x] \n", __func__, WPGBbar, index);
  113. //--------------------------------------------------------------------
  114. // READ - step 1
  115. // read at address, byte length, I2C address (shifted), index
  116. // or read direct, byte length, index
  117. if (ctlr_ptr->ctlr_type == 0x02) {
  118. data = WPG_READATADDR_MASK;
  119. // fill in I2C address
  120. ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
  121. ultemp = ultemp >> 1;
  122. data |= (ultemp << 8);
  123. // fill in index
  124. data |= (unsigned long)index;
  125. } else if (ctlr_ptr->ctlr_type == 0x04) {
  126. data = WPG_READDIRECT_MASK;
  127. // fill in index
  128. ultemp = (unsigned long)index;
  129. ultemp = ultemp << 8;
  130. data |= ultemp;
  131. } else {
  132. err("this controller type is not supported \n");
  133. return HPC_ERROR;
  134. }
  135. wpg_data = swab32(data); // swap data before writing
  136. wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
  137. writel(wpg_data, wpg_addr);
  138. //--------------------------------------------------------------------
  139. // READ - step 2 : clear the message buffer
  140. data = 0x00000000;
  141. wpg_data = swab32(data);
  142. wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
  143. writel(wpg_data, wpg_addr);
  144. //--------------------------------------------------------------------
  145. // READ - step 3 : issue start operation, I2C master control bit 30:ON
  146. // 2020 : [20] OR operation at [20] offset 0x20
  147. data = WPG_I2CMCNTL_STARTOP_MASK;
  148. wpg_data = swab32(data);
  149. wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
  150. writel(wpg_data, wpg_addr);
  151. //--------------------------------------------------------------------
  152. // READ - step 4 : wait until start operation bit clears
  153. i = CMD_COMPLETE_TOUT_SEC;
  154. while (i) {
  155. msleep(10);
  156. wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
  157. wpg_data = readl(wpg_addr);
  158. data = swab32(wpg_data);
  159. if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
  160. break;
  161. i--;
  162. }
  163. if (i == 0) {
  164. debug("%s - Error : WPG timeout\n", __func__);
  165. return HPC_ERROR;
  166. }
  167. //--------------------------------------------------------------------
  168. // READ - step 5 : read I2C status register
  169. i = CMD_COMPLETE_TOUT_SEC;
  170. while (i) {
  171. msleep(10);
  172. wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
  173. wpg_data = readl(wpg_addr);
  174. data = swab32(wpg_data);
  175. if (HPC_I2CSTATUS_CHECK(data))
  176. break;
  177. i--;
  178. }
  179. if (i == 0) {
  180. debug("ctrl_read - Exit Error:I2C timeout\n");
  181. return HPC_ERROR;
  182. }
  183. //--------------------------------------------------------------------
  184. // READ - step 6 : get DATA
  185. wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
  186. wpg_data = readl(wpg_addr);
  187. data = swab32(wpg_data);
  188. status = (u8) data;
  189. debug_polling("%s - Exit index[%x] status[%x]\n", __func__, index, status);
  190. return (status);
  191. }
  192. /*----------------------------------------------------------------------
  193. * Name: i2c_ctrl_write
  194. *
  195. * Action: write to HPC over I2C
  196. *
  197. * Return 0 or error codes
  198. *---------------------------------------------------------------------*/
  199. static u8 i2c_ctrl_write(struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index, u8 cmd)
  200. {
  201. u8 rc;
  202. void __iomem *wpg_addr; // base addr + offset
  203. unsigned long wpg_data; // data to/from WPG LOHI format
  204. unsigned long ultemp;
  205. unsigned long data; // actual data HILO format
  206. int i;
  207. debug_polling("%s - Entry WPGBbar[%p] index[%x] cmd[%x]\n", __func__, WPGBbar, index, cmd);
  208. rc = 0;
  209. //--------------------------------------------------------------------
  210. // WRITE - step 1
  211. // write at address, byte length, I2C address (shifted), index
  212. // or write direct, byte length, index
  213. data = 0x00000000;
  214. if (ctlr_ptr->ctlr_type == 0x02) {
  215. data = WPG_WRITEATADDR_MASK;
  216. // fill in I2C address
  217. ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
  218. ultemp = ultemp >> 1;
  219. data |= (ultemp << 8);
  220. // fill in index
  221. data |= (unsigned long)index;
  222. } else if (ctlr_ptr->ctlr_type == 0x04) {
  223. data = WPG_WRITEDIRECT_MASK;
  224. // fill in index
  225. ultemp = (unsigned long)index;
  226. ultemp = ultemp << 8;
  227. data |= ultemp;
  228. } else {
  229. err("this controller type is not supported \n");
  230. return HPC_ERROR;
  231. }
  232. wpg_data = swab32(data); // swap data before writing
  233. wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
  234. writel(wpg_data, wpg_addr);
  235. //--------------------------------------------------------------------
  236. // WRITE - step 2 : clear the message buffer
  237. data = 0x00000000 | (unsigned long)cmd;
  238. wpg_data = swab32(data);
  239. wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
  240. writel(wpg_data, wpg_addr);
  241. //--------------------------------------------------------------------
  242. // WRITE - step 3 : issue start operation,I2C master control bit 30:ON
  243. // 2020 : [20] OR operation at [20] offset 0x20
  244. data = WPG_I2CMCNTL_STARTOP_MASK;
  245. wpg_data = swab32(data);
  246. wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
  247. writel(wpg_data, wpg_addr);
  248. //--------------------------------------------------------------------
  249. // WRITE - step 4 : wait until start operation bit clears
  250. i = CMD_COMPLETE_TOUT_SEC;
  251. while (i) {
  252. msleep(10);
  253. wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
  254. wpg_data = readl(wpg_addr);
  255. data = swab32(wpg_data);
  256. if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
  257. break;
  258. i--;
  259. }
  260. if (i == 0) {
  261. debug("%s - Exit Error:WPG timeout\n", __func__);
  262. rc = HPC_ERROR;
  263. }
  264. //--------------------------------------------------------------------
  265. // WRITE - step 5 : read I2C status register
  266. i = CMD_COMPLETE_TOUT_SEC;
  267. while (i) {
  268. msleep(10);
  269. wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
  270. wpg_data = readl(wpg_addr);
  271. data = swab32(wpg_data);
  272. if (HPC_I2CSTATUS_CHECK(data))
  273. break;
  274. i--;
  275. }
  276. if (i == 0) {
  277. debug("ctrl_read - Error : I2C timeout\n");
  278. rc = HPC_ERROR;
  279. }
  280. debug_polling("%s Exit rc[%x]\n", __func__, rc);
  281. return (rc);
  282. }
  283. //------------------------------------------------------------
  284. // Read from ISA type HPC
  285. //------------------------------------------------------------
  286. static u8 isa_ctrl_read(struct controller *ctlr_ptr, u8 offset)
  287. {
  288. u16 start_address;
  289. u8 data;
  290. start_address = ctlr_ptr->u.isa_ctlr.io_start;
  291. data = inb(start_address + offset);
  292. return data;
  293. }
  294. //--------------------------------------------------------------
  295. // Write to ISA type HPC
  296. //--------------------------------------------------------------
  297. static void isa_ctrl_write(struct controller *ctlr_ptr, u8 offset, u8 data)
  298. {
  299. u16 start_address;
  300. u16 port_address;
  301. start_address = ctlr_ptr->u.isa_ctlr.io_start;
  302. port_address = start_address + (u16) offset;
  303. outb(data, port_address);
  304. }
  305. static u8 pci_ctrl_read(struct controller *ctrl, u8 offset)
  306. {
  307. u8 data = 0x00;
  308. debug("inside pci_ctrl_read\n");
  309. if (ctrl->ctrl_dev)
  310. pci_read_config_byte(ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, &data);
  311. return data;
  312. }
  313. static u8 pci_ctrl_write(struct controller *ctrl, u8 offset, u8 data)
  314. {
  315. u8 rc = -ENODEV;
  316. debug("inside pci_ctrl_write\n");
  317. if (ctrl->ctrl_dev) {
  318. pci_write_config_byte(ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, data);
  319. rc = 0;
  320. }
  321. return rc;
  322. }
  323. static u8 ctrl_read(struct controller *ctlr, void __iomem *base, u8 offset)
  324. {
  325. u8 rc;
  326. switch (ctlr->ctlr_type) {
  327. case 0:
  328. rc = isa_ctrl_read(ctlr, offset);
  329. break;
  330. case 1:
  331. rc = pci_ctrl_read(ctlr, offset);
  332. break;
  333. case 2:
  334. case 4:
  335. rc = i2c_ctrl_read(ctlr, base, offset);
  336. break;
  337. default:
  338. return -ENODEV;
  339. }
  340. return rc;
  341. }
  342. static u8 ctrl_write(struct controller *ctlr, void __iomem *base, u8 offset, u8 data)
  343. {
  344. u8 rc = 0;
  345. switch (ctlr->ctlr_type) {
  346. case 0:
  347. isa_ctrl_write(ctlr, offset, data);
  348. break;
  349. case 1:
  350. rc = pci_ctrl_write(ctlr, offset, data);
  351. break;
  352. case 2:
  353. case 4:
  354. rc = i2c_ctrl_write(ctlr, base, offset, data);
  355. break;
  356. default:
  357. return -ENODEV;
  358. }
  359. return rc;
  360. }
  361. /*----------------------------------------------------------------------
  362. * Name: hpc_writecmdtoindex()
  363. *
  364. * Action: convert a write command to proper index within a controller
  365. *
  366. * Return index, HPC_ERROR
  367. *---------------------------------------------------------------------*/
  368. static u8 hpc_writecmdtoindex(u8 cmd, u8 index)
  369. {
  370. u8 rc;
  371. switch (cmd) {
  372. case HPC_CTLR_ENABLEIRQ: // 0x00.N.15
  373. case HPC_CTLR_CLEARIRQ: // 0x06.N.15
  374. case HPC_CTLR_RESET: // 0x07.N.15
  375. case HPC_CTLR_IRQSTEER: // 0x08.N.15
  376. case HPC_CTLR_DISABLEIRQ: // 0x01.N.15
  377. case HPC_ALLSLOT_ON: // 0x11.N.15
  378. case HPC_ALLSLOT_OFF: // 0x12.N.15
  379. rc = 0x0F;
  380. break;
  381. case HPC_SLOT_OFF: // 0x02.Y.0-14
  382. case HPC_SLOT_ON: // 0x03.Y.0-14
  383. case HPC_SLOT_ATTNOFF: // 0x04.N.0-14
  384. case HPC_SLOT_ATTNON: // 0x05.N.0-14
  385. case HPC_SLOT_BLINKLED: // 0x13.N.0-14
  386. rc = index;
  387. break;
  388. case HPC_BUS_33CONVMODE:
  389. case HPC_BUS_66CONVMODE:
  390. case HPC_BUS_66PCIXMODE:
  391. case HPC_BUS_100PCIXMODE:
  392. case HPC_BUS_133PCIXMODE:
  393. rc = index + WPG_1ST_BUS_INDEX - 1;
  394. break;
  395. default:
  396. err("hpc_writecmdtoindex - Error invalid cmd[%x]\n", cmd);
  397. rc = HPC_ERROR;
  398. }
  399. return rc;
  400. }
  401. /*----------------------------------------------------------------------
  402. * Name: hpc_readcmdtoindex()
  403. *
  404. * Action: convert a read command to proper index within a controller
  405. *
  406. * Return index, HPC_ERROR
  407. *---------------------------------------------------------------------*/
  408. static u8 hpc_readcmdtoindex(u8 cmd, u8 index)
  409. {
  410. u8 rc;
  411. switch (cmd) {
  412. case READ_CTLRSTATUS:
  413. rc = 0x0F;
  414. break;
  415. case READ_SLOTSTATUS:
  416. case READ_ALLSTAT:
  417. rc = index;
  418. break;
  419. case READ_EXTSLOTSTATUS:
  420. rc = index + WPG_1ST_EXTSLOT_INDEX;
  421. break;
  422. case READ_BUSSTATUS:
  423. rc = index + WPG_1ST_BUS_INDEX - 1;
  424. break;
  425. case READ_SLOTLATCHLOWREG:
  426. rc = 0x28;
  427. break;
  428. case READ_REVLEVEL:
  429. rc = 0x25;
  430. break;
  431. case READ_HPCOPTIONS:
  432. rc = 0x27;
  433. break;
  434. default:
  435. rc = HPC_ERROR;
  436. }
  437. return rc;
  438. }
  439. /*----------------------------------------------------------------------
  440. * Name: HPCreadslot()
  441. *
  442. * Action: issue a READ command to HPC
  443. *
  444. * Input: pslot - cannot be NULL for READ_ALLSTAT
  445. * pstatus - can be NULL for READ_ALLSTAT
  446. *
  447. * Return 0 or error codes
  448. *---------------------------------------------------------------------*/
  449. int ibmphp_hpc_readslot(struct slot *pslot, u8 cmd, u8 *pstatus)
  450. {
  451. void __iomem *wpg_bbar = NULL;
  452. struct controller *ctlr_ptr;
  453. u8 index, status;
  454. int rc = 0;
  455. int busindex;
  456. debug_polling("%s - Entry pslot[%p] cmd[%x] pstatus[%p]\n", __func__, pslot, cmd, pstatus);
  457. if ((pslot == NULL)
  458. || ((pstatus == NULL) && (cmd != READ_ALLSTAT) && (cmd != READ_BUSSTATUS))) {
  459. rc = -EINVAL;
  460. err("%s - Error invalid pointer, rc[%d]\n", __func__, rc);
  461. return rc;
  462. }
  463. if (cmd == READ_BUSSTATUS) {
  464. busindex = ibmphp_get_bus_index(pslot->bus);
  465. if (busindex < 0) {
  466. rc = -EINVAL;
  467. err("%s - Exit Error:invalid bus, rc[%d]\n", __func__, rc);
  468. return rc;
  469. } else
  470. index = (u8) busindex;
  471. } else
  472. index = pslot->ctlr_index;
  473. index = hpc_readcmdtoindex(cmd, index);
  474. if (index == HPC_ERROR) {
  475. rc = -EINVAL;
  476. err("%s - Exit Error:invalid index, rc[%d]\n", __func__, rc);
  477. return rc;
  478. }
  479. ctlr_ptr = pslot->ctrl;
  480. get_hpc_access();
  481. //--------------------------------------------------------------------
  482. // map physical address to logical address
  483. //--------------------------------------------------------------------
  484. if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
  485. wpg_bbar = ioremap(ctlr_ptr->u.wpeg_ctlr.wpegbbar, WPG_I2C_IOREMAP_SIZE);
  486. //--------------------------------------------------------------------
  487. // check controller status before reading
  488. //--------------------------------------------------------------------
  489. rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar, &status);
  490. if (!rc) {
  491. switch (cmd) {
  492. case READ_ALLSTAT:
  493. // update the slot structure
  494. pslot->ctrl->status = status;
  495. pslot->status = ctrl_read(ctlr_ptr, wpg_bbar, index);
  496. rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar,
  497. &status);
  498. if (!rc)
  499. pslot->ext_status = ctrl_read(ctlr_ptr, wpg_bbar, index + WPG_1ST_EXTSLOT_INDEX);
  500. break;
  501. case READ_SLOTSTATUS:
  502. // DO NOT update the slot structure
  503. *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
  504. break;
  505. case READ_EXTSLOTSTATUS:
  506. // DO NOT update the slot structure
  507. *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
  508. break;
  509. case READ_CTLRSTATUS:
  510. // DO NOT update the slot structure
  511. *pstatus = status;
  512. break;
  513. case READ_BUSSTATUS:
  514. pslot->busstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
  515. break;
  516. case READ_REVLEVEL:
  517. *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
  518. break;
  519. case READ_HPCOPTIONS:
  520. *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
  521. break;
  522. case READ_SLOTLATCHLOWREG:
  523. // DO NOT update the slot structure
  524. *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
  525. break;
  526. // Not used
  527. case READ_ALLSLOT:
  528. list_for_each_entry(pslot, &ibmphp_slot_head,
  529. ibm_slot_list) {
  530. index = pslot->ctlr_index;
  531. rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr,
  532. wpg_bbar, &status);
  533. if (!rc) {
  534. pslot->status = ctrl_read(ctlr_ptr, wpg_bbar, index);
  535. rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT,
  536. ctlr_ptr, wpg_bbar, &status);
  537. if (!rc)
  538. pslot->ext_status =
  539. ctrl_read(ctlr_ptr, wpg_bbar,
  540. index + WPG_1ST_EXTSLOT_INDEX);
  541. } else {
  542. err("%s - Error ctrl_read failed\n", __func__);
  543. rc = -EINVAL;
  544. break;
  545. }
  546. }
  547. break;
  548. default:
  549. rc = -EINVAL;
  550. break;
  551. }
  552. }
  553. //--------------------------------------------------------------------
  554. // cleanup
  555. //--------------------------------------------------------------------
  556. // remove physical to logical address mapping
  557. if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
  558. iounmap(wpg_bbar);
  559. free_hpc_access();
  560. debug_polling("%s - Exit rc[%d]\n", __func__, rc);
  561. return rc;
  562. }
  563. /*----------------------------------------------------------------------
  564. * Name: ibmphp_hpc_writeslot()
  565. *
  566. * Action: issue a WRITE command to HPC
  567. *---------------------------------------------------------------------*/
  568. int ibmphp_hpc_writeslot(struct slot *pslot, u8 cmd)
  569. {
  570. void __iomem *wpg_bbar = NULL;
  571. struct controller *ctlr_ptr;
  572. u8 index, status;
  573. int busindex;
  574. u8 done;
  575. int rc = 0;
  576. int timeout;
  577. debug_polling("%s - Entry pslot[%p] cmd[%x]\n", __func__, pslot, cmd);
  578. if (pslot == NULL) {
  579. rc = -EINVAL;
  580. err("%s - Error Exit rc[%d]\n", __func__, rc);
  581. return rc;
  582. }
  583. if ((cmd == HPC_BUS_33CONVMODE) || (cmd == HPC_BUS_66CONVMODE) ||
  584. (cmd == HPC_BUS_66PCIXMODE) || (cmd == HPC_BUS_100PCIXMODE) ||
  585. (cmd == HPC_BUS_133PCIXMODE)) {
  586. busindex = ibmphp_get_bus_index(pslot->bus);
  587. if (busindex < 0) {
  588. rc = -EINVAL;
  589. err("%s - Exit Error:invalid bus, rc[%d]\n", __func__, rc);
  590. return rc;
  591. } else
  592. index = (u8) busindex;
  593. } else
  594. index = pslot->ctlr_index;
  595. index = hpc_writecmdtoindex(cmd, index);
  596. if (index == HPC_ERROR) {
  597. rc = -EINVAL;
  598. err("%s - Error Exit rc[%d]\n", __func__, rc);
  599. return rc;
  600. }
  601. ctlr_ptr = pslot->ctrl;
  602. get_hpc_access();
  603. //--------------------------------------------------------------------
  604. // map physical address to logical address
  605. //--------------------------------------------------------------------
  606. if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4)) {
  607. wpg_bbar = ioremap(ctlr_ptr->u.wpeg_ctlr.wpegbbar, WPG_I2C_IOREMAP_SIZE);
  608. debug("%s - ctlr id[%x] physical[%lx] logical[%lx] i2c[%x]\n", __func__,
  609. ctlr_ptr->ctlr_id, (ulong) (ctlr_ptr->u.wpeg_ctlr.wpegbbar), (ulong) wpg_bbar,
  610. ctlr_ptr->u.wpeg_ctlr.i2c_addr);
  611. }
  612. //--------------------------------------------------------------------
  613. // check controller status before writing
  614. //--------------------------------------------------------------------
  615. rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar, &status);
  616. if (!rc) {
  617. ctrl_write(ctlr_ptr, wpg_bbar, index, cmd);
  618. //--------------------------------------------------------------------
  619. // check controller is still not working on the command
  620. //--------------------------------------------------------------------
  621. timeout = CMD_COMPLETE_TOUT_SEC;
  622. done = 0;
  623. while (!done) {
  624. rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar,
  625. &status);
  626. if (!rc) {
  627. if (NEEDTOCHECK_CMDSTATUS(cmd)) {
  628. if (CTLR_FINISHED(status) == HPC_CTLR_FINISHED_YES)
  629. done = 1;
  630. } else
  631. done = 1;
  632. }
  633. if (!done) {
  634. msleep(1000);
  635. if (timeout < 1) {
  636. done = 1;
  637. err("%s - Error command complete timeout\n", __func__);
  638. rc = -EFAULT;
  639. } else
  640. timeout--;
  641. }
  642. }
  643. ctlr_ptr->status = status;
  644. }
  645. // cleanup
  646. // remove physical to logical address mapping
  647. if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
  648. iounmap(wpg_bbar);
  649. free_hpc_access();
  650. debug_polling("%s - Exit rc[%d]\n", __func__, rc);
  651. return rc;
  652. }
  653. /*----------------------------------------------------------------------
  654. * Name: get_hpc_access()
  655. *
  656. * Action: make sure only one process can access HPC at one time
  657. *---------------------------------------------------------------------*/
  658. static void get_hpc_access(void)
  659. {
  660. mutex_lock(&sem_hpcaccess);
  661. }
  662. /*----------------------------------------------------------------------
  663. * Name: free_hpc_access()
  664. *---------------------------------------------------------------------*/
  665. void free_hpc_access(void)
  666. {
  667. mutex_unlock(&sem_hpcaccess);
  668. }
  669. /*----------------------------------------------------------------------
  670. * Name: ibmphp_lock_operations()
  671. *
  672. * Action: make sure only one process can change the data structure
  673. *---------------------------------------------------------------------*/
  674. void ibmphp_lock_operations(void)
  675. {
  676. mutex_lock(&operations_mutex);
  677. to_debug = 1;
  678. }
  679. /*----------------------------------------------------------------------
  680. * Name: ibmphp_unlock_operations()
  681. *---------------------------------------------------------------------*/
  682. void ibmphp_unlock_operations(void)
  683. {
  684. debug("%s - Entry\n", __func__);
  685. mutex_unlock(&operations_mutex);
  686. to_debug = 0;
  687. debug("%s - Exit\n", __func__);
  688. }
  689. /*----------------------------------------------------------------------
  690. * Name: poll_hpc()
  691. *---------------------------------------------------------------------*/
  692. #define POLL_LATCH_REGISTER 0
  693. #define POLL_SLOTS 1
  694. #define POLL_SLEEP 2
  695. static int poll_hpc(void *data)
  696. {
  697. struct slot myslot;
  698. struct slot *pslot = NULL;
  699. int rc;
  700. int poll_state = POLL_LATCH_REGISTER;
  701. u8 oldlatchlow = 0x00;
  702. u8 curlatchlow = 0x00;
  703. int poll_count = 0;
  704. u8 ctrl_count = 0x00;
  705. debug("%s - Entry\n", __func__);
  706. while (!kthread_should_stop()) {
  707. /* try to get the lock to do some kind of hardware access */
  708. mutex_lock(&operations_mutex);
  709. switch (poll_state) {
  710. case POLL_LATCH_REGISTER:
  711. oldlatchlow = curlatchlow;
  712. ctrl_count = 0x00;
  713. list_for_each_entry(pslot, &ibmphp_slot_head,
  714. ibm_slot_list) {
  715. if (ctrl_count >= ibmphp_get_total_controllers())
  716. break;
  717. if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
  718. ctrl_count++;
  719. if (READ_SLOT_LATCH(pslot->ctrl)) {
  720. rc = ibmphp_hpc_readslot(pslot,
  721. READ_SLOTLATCHLOWREG,
  722. &curlatchlow);
  723. if (oldlatchlow != curlatchlow)
  724. process_changeinlatch(oldlatchlow,
  725. curlatchlow,
  726. pslot->ctrl);
  727. }
  728. }
  729. }
  730. ++poll_count;
  731. poll_state = POLL_SLEEP;
  732. break;
  733. case POLL_SLOTS:
  734. list_for_each_entry(pslot, &ibmphp_slot_head,
  735. ibm_slot_list) {
  736. // make a copy of the old status
  737. memcpy((void *) &myslot, (void *) pslot,
  738. sizeof(struct slot));
  739. rc = ibmphp_hpc_readslot(pslot, READ_ALLSTAT, NULL);
  740. if ((myslot.status != pslot->status)
  741. || (myslot.ext_status != pslot->ext_status))
  742. process_changeinstatus(pslot, &myslot);
  743. }
  744. ctrl_count = 0x00;
  745. list_for_each_entry(pslot, &ibmphp_slot_head,
  746. ibm_slot_list) {
  747. if (ctrl_count >= ibmphp_get_total_controllers())
  748. break;
  749. if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
  750. ctrl_count++;
  751. if (READ_SLOT_LATCH(pslot->ctrl))
  752. rc = ibmphp_hpc_readslot(pslot,
  753. READ_SLOTLATCHLOWREG,
  754. &curlatchlow);
  755. }
  756. }
  757. ++poll_count;
  758. poll_state = POLL_SLEEP;
  759. break;
  760. case POLL_SLEEP:
  761. /* don't sleep with a lock on the hardware */
  762. mutex_unlock(&operations_mutex);
  763. msleep(POLL_INTERVAL_SEC * 1000);
  764. if (kthread_should_stop())
  765. goto out_sleep;
  766. mutex_lock(&operations_mutex);
  767. if (poll_count >= POLL_LATCH_CNT) {
  768. poll_count = 0;
  769. poll_state = POLL_SLOTS;
  770. } else
  771. poll_state = POLL_LATCH_REGISTER;
  772. break;
  773. }
  774. /* give up the hardware semaphore */
  775. mutex_unlock(&operations_mutex);
  776. /* sleep for a short time just for good measure */
  777. out_sleep:
  778. msleep(100);
  779. }
  780. complete(&exit_complete);
  781. debug("%s - Exit\n", __func__);
  782. return 0;
  783. }
  784. /*----------------------------------------------------------------------
  785. * Name: process_changeinstatus
  786. *
  787. * Action: compare old and new slot status, process the change in status
  788. *
  789. * Input: pointer to slot struct, old slot struct
  790. *
  791. * Return 0 or error codes
  792. * Value:
  793. *
  794. * Side
  795. * Effects: None.
  796. *
  797. * Notes:
  798. *---------------------------------------------------------------------*/
  799. static int process_changeinstatus(struct slot *pslot, struct slot *poldslot)
  800. {
  801. u8 status;
  802. int rc = 0;
  803. u8 disable = 0;
  804. u8 update = 0;
  805. debug("process_changeinstatus - Entry pslot[%p], poldslot[%p]\n", pslot, poldslot);
  806. // bit 0 - HPC_SLOT_POWER
  807. if ((pslot->status & 0x01) != (poldslot->status & 0x01))
  808. update = 1;
  809. // bit 1 - HPC_SLOT_CONNECT
  810. // ignore
  811. // bit 2 - HPC_SLOT_ATTN
  812. if ((pslot->status & 0x04) != (poldslot->status & 0x04))
  813. update = 1;
  814. // bit 3 - HPC_SLOT_PRSNT2
  815. // bit 4 - HPC_SLOT_PRSNT1
  816. if (((pslot->status & 0x08) != (poldslot->status & 0x08))
  817. || ((pslot->status & 0x10) != (poldslot->status & 0x10)))
  818. update = 1;
  819. // bit 5 - HPC_SLOT_PWRGD
  820. if ((pslot->status & 0x20) != (poldslot->status & 0x20))
  821. // OFF -> ON: ignore, ON -> OFF: disable slot
  822. if ((poldslot->status & 0x20) && (SLOT_CONNECT(poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT(poldslot->status)))
  823. disable = 1;
  824. // bit 6 - HPC_SLOT_BUS_SPEED
  825. // ignore
  826. // bit 7 - HPC_SLOT_LATCH
  827. if ((pslot->status & 0x80) != (poldslot->status & 0x80)) {
  828. update = 1;
  829. // OPEN -> CLOSE
  830. if (pslot->status & 0x80) {
  831. if (SLOT_PWRGD(pslot->status)) {
  832. // power goes on and off after closing latch
  833. // check again to make sure power is still ON
  834. msleep(1000);
  835. rc = ibmphp_hpc_readslot(pslot, READ_SLOTSTATUS, &status);
  836. if (SLOT_PWRGD(status))
  837. update = 1;
  838. else // overwrite power in pslot to OFF
  839. pslot->status &= ~HPC_SLOT_POWER;
  840. }
  841. }
  842. // CLOSE -> OPEN
  843. else if ((SLOT_PWRGD(poldslot->status) == HPC_SLOT_PWRGD_GOOD)
  844. && (SLOT_CONNECT(poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT(poldslot->status))) {
  845. disable = 1;
  846. }
  847. // else - ignore
  848. }
  849. // bit 4 - HPC_SLOT_BLINK_ATTN
  850. if ((pslot->ext_status & 0x08) != (poldslot->ext_status & 0x08))
  851. update = 1;
  852. if (disable) {
  853. debug("process_changeinstatus - disable slot\n");
  854. pslot->flag = 0;
  855. rc = ibmphp_do_disable_slot(pslot);
  856. }
  857. if (update || disable)
  858. ibmphp_update_slot_info(pslot);
  859. debug("%s - Exit rc[%d] disable[%x] update[%x]\n", __func__, rc, disable, update);
  860. return rc;
  861. }
  862. /*----------------------------------------------------------------------
  863. * Name: process_changeinlatch
  864. *
  865. * Action: compare old and new latch reg status, process the change
  866. *
  867. * Input: old and current latch register status
  868. *
  869. * Return 0 or error codes
  870. * Value:
  871. *---------------------------------------------------------------------*/
  872. static int process_changeinlatch(u8 old, u8 new, struct controller *ctrl)
  873. {
  874. struct slot myslot, *pslot;
  875. u8 i;
  876. u8 mask;
  877. int rc = 0;
  878. debug("%s - Entry old[%x], new[%x]\n", __func__, old, new);
  879. // bit 0 reserved, 0 is LSB, check bit 1-6 for 6 slots
  880. for (i = ctrl->starting_slot_num; i <= ctrl->ending_slot_num; i++) {
  881. mask = 0x01 << i;
  882. if ((mask & old) != (mask & new)) {
  883. pslot = ibmphp_get_slot_from_physical_num(i);
  884. if (pslot) {
  885. memcpy((void *) &myslot, (void *) pslot, sizeof(struct slot));
  886. rc = ibmphp_hpc_readslot(pslot, READ_ALLSTAT, NULL);
  887. debug("%s - call process_changeinstatus for slot[%d]\n", __func__, i);
  888. process_changeinstatus(pslot, &myslot);
  889. } else {
  890. rc = -EINVAL;
  891. err("%s - Error bad pointer for slot[%d]\n", __func__, i);
  892. }
  893. }
  894. }
  895. debug("%s - Exit rc[%d]\n", __func__, rc);
  896. return rc;
  897. }
  898. /*----------------------------------------------------------------------
  899. * Name: ibmphp_hpc_start_poll_thread
  900. *
  901. * Action: start polling thread
  902. *---------------------------------------------------------------------*/
  903. int __init ibmphp_hpc_start_poll_thread(void)
  904. {
  905. debug("%s - Entry\n", __func__);
  906. ibmphp_poll_thread = kthread_run(poll_hpc, NULL, "hpc_poll");
  907. if (IS_ERR(ibmphp_poll_thread)) {
  908. err("%s - Error, thread not started\n", __func__);
  909. return PTR_ERR(ibmphp_poll_thread);
  910. }
  911. return 0;
  912. }
  913. /*----------------------------------------------------------------------
  914. * Name: ibmphp_hpc_stop_poll_thread
  915. *
  916. * Action: stop polling thread and cleanup
  917. *---------------------------------------------------------------------*/
  918. void __exit ibmphp_hpc_stop_poll_thread(void)
  919. {
  920. debug("%s - Entry\n", __func__);
  921. kthread_stop(ibmphp_poll_thread);
  922. debug("before locking operations\n");
  923. ibmphp_lock_operations();
  924. debug("after locking operations\n");
  925. // wait for poll thread to exit
  926. debug("before exit_complete down\n");
  927. wait_for_completion(&exit_complete);
  928. debug("after exit_completion down\n");
  929. // cleanup
  930. debug("before free_hpc_access\n");
  931. free_hpc_access();
  932. debug("after free_hpc_access\n");
  933. ibmphp_unlock_operations();
  934. debug("after unlock operations\n");
  935. debug("%s - Exit\n", __func__);
  936. }
  937. /*----------------------------------------------------------------------
  938. * Name: hpc_wait_ctlr_notworking
  939. *
  940. * Action: wait until the controller is in a not working state
  941. *
  942. * Return 0, HPC_ERROR
  943. * Value:
  944. *---------------------------------------------------------------------*/
  945. static int hpc_wait_ctlr_notworking(int timeout, struct controller *ctlr_ptr, void __iomem *wpg_bbar,
  946. u8 *pstatus)
  947. {
  948. int rc = 0;
  949. u8 done = 0;
  950. debug_polling("hpc_wait_ctlr_notworking - Entry timeout[%d]\n", timeout);
  951. while (!done) {
  952. *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, WPG_CTLR_INDEX);
  953. if (*pstatus == HPC_ERROR) {
  954. rc = HPC_ERROR;
  955. done = 1;
  956. }
  957. if (CTLR_WORKING(*pstatus) == HPC_CTLR_WORKING_NO)
  958. done = 1;
  959. if (!done) {
  960. msleep(1000);
  961. if (timeout < 1) {
  962. done = 1;
  963. err("HPCreadslot - Error ctlr timeout\n");
  964. rc = HPC_ERROR;
  965. } else
  966. timeout--;
  967. }
  968. }
  969. debug_polling("hpc_wait_ctlr_notworking - Exit rc[%x] status[%x]\n", rc, *pstatus);
  970. return rc;
  971. }