1 // SPDX-License-Identifier: GPL-2.0+
3 * IBM Hot Plug Controller Driver
5 * Written By: Jyoti Shah, IBM Corporation
7 * Copyright (C) 2001-2003 IBM Corp.
16 #include <linux/wait.h>
17 #include <linux/time.h>
18 #include <linux/completion.h>
19 #include <linux/delay.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/init.h>
23 #include <linux/mutex.h>
24 #include <linux/sched.h>
25 #include <linux/kthread.h>
28 static int to_debug = 0;
29 #define debug_polling(fmt, arg...) do { if (to_debug) debug(fmt, arg); } while (0)
31 //----------------------------------------------------------------------------
33 //----------------------------------------------------------------------------
34 #define CMD_COMPLETE_TOUT_SEC 60 // give HPC 60 sec to finish cmd
35 #define HPC_CTLR_WORKING_TOUT 60 // give HPC 60 sec to finish cmd
36 #define HPC_GETACCESS_TIMEOUT 60 // seconds
37 #define POLL_INTERVAL_SEC 2 // poll HPC every 2 seconds
38 #define POLL_LATCH_CNT 5 // poll latch 5 times, then poll slots
40 //----------------------------------------------------------------------------
41 // Winnipeg Architected Register Offsets
42 //----------------------------------------------------------------------------
43 #define WPG_I2CMBUFL_OFFSET 0x08 // I2C Message Buffer Low
44 #define WPG_I2CMOSUP_OFFSET 0x10 // I2C Master Operation Setup Reg
45 #define WPG_I2CMCNTL_OFFSET 0x20 // I2C Master Control Register
46 #define WPG_I2CPARM_OFFSET 0x40 // I2C Parameter Register
47 #define WPG_I2CSTAT_OFFSET 0x70 // I2C Status Register
49 //----------------------------------------------------------------------------
50 // Winnipeg Store Type commands (Add this commands to the register offset)
51 //----------------------------------------------------------------------------
52 #define WPG_I2C_AND 0x1000 // I2C AND operation
53 #define WPG_I2C_OR 0x2000 // I2C OR operation
55 //----------------------------------------------------------------------------
56 // Command set for I2C Master Operation Setup Register
57 //----------------------------------------------------------------------------
58 #define WPG_READATADDR_MASK 0x00010000 // read,bytes,I2C shifted,index
59 #define WPG_WRITEATADDR_MASK 0x40010000 // write,bytes,I2C shifted,index
60 #define WPG_READDIRECT_MASK 0x10010000
61 #define WPG_WRITEDIRECT_MASK 0x60010000
64 //----------------------------------------------------------------------------
65 // bit masks for I2C Master Control Register
66 //----------------------------------------------------------------------------
67 #define WPG_I2CMCNTL_STARTOP_MASK 0x00000002 // Start the Operation
69 //----------------------------------------------------------------------------
71 //----------------------------------------------------------------------------
72 #define WPG_I2C_IOREMAP_SIZE 0x2044 // size of linear address interval
74 //----------------------------------------------------------------------------
76 //----------------------------------------------------------------------------
77 #define WPG_1ST_SLOT_INDEX 0x01 // index - 1st slot for ctlr
78 #define WPG_CTLR_INDEX 0x0F // index - ctlr
79 #define WPG_1ST_EXTSLOT_INDEX 0x10 // index - 1st ext slot for ctlr
80 #define WPG_1ST_BUS_INDEX 0x1F // index - 1st bus for ctlr
82 //----------------------------------------------------------------------------
84 //----------------------------------------------------------------------------
85 // if bits 20,22,25,26,27,29,30 are OFF return 1
86 #define HPC_I2CSTATUS_CHECK(s) ((u8)((s & 0x00000A76) ? 0 : 1))
88 //----------------------------------------------------------------------------
90 //----------------------------------------------------------------------------
91 static DEFINE_MUTEX(sem_hpcaccess); // lock access to HPC
92 static DEFINE_MUTEX(operations_mutex); // lock all operations and
93 // access to data structures
94 static DECLARE_COMPLETION(exit_complete); // make sure polling thread goes away
95 static struct task_struct *ibmphp_poll_thread;
96 //----------------------------------------------------------------------------
97 // local function prototypes
98 //----------------------------------------------------------------------------
99 static u8 i2c_ctrl_read(struct controller *, void __iomem *, u8);
100 static u8 i2c_ctrl_write(struct controller *, void __iomem *, u8, u8);
101 static u8 hpc_writecmdtoindex(u8, u8);
102 static u8 hpc_readcmdtoindex(u8, u8);
103 static void get_hpc_access(void);
104 static void free_hpc_access(void);
105 static int poll_hpc(void *data);
106 static int process_changeinstatus(struct slot *, struct slot *);
107 static int process_changeinlatch(u8, u8, struct controller *);
108 static int hpc_wait_ctlr_notworking(int, struct controller *, void __iomem *, u8 *);
109 //----------------------------------------------------------------------------
112 /*----------------------------------------------------------------------
113 * Name: i2c_ctrl_read
115 * Action: read from HPC over I2C
117 *---------------------------------------------------------------------*/
118 static u8 i2c_ctrl_read(struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index)
122 void __iomem *wpg_addr; // base addr + offset
123 unsigned long wpg_data; // data to/from WPG LOHI format
124 unsigned long ultemp;
125 unsigned long data; // actual data HILO format
127 debug_polling("%s - Entry WPGBbar[%p] index[%x] \n", __func__, WPGBbar, index);
129 //--------------------------------------------------------------------
131 // read at address, byte length, I2C address (shifted), index
132 // or read direct, byte length, index
133 if (ctlr_ptr->ctlr_type == 0x02) {
134 data = WPG_READATADDR_MASK;
135 // fill in I2C address
136 ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
137 ultemp = ultemp >> 1;
138 data |= (ultemp << 8);
141 data |= (unsigned long)index;
142 } else if (ctlr_ptr->ctlr_type == 0x04) {
143 data = WPG_READDIRECT_MASK;
146 ultemp = (unsigned long)index;
147 ultemp = ultemp << 8;
150 err("this controller type is not supported \n");
154 wpg_data = swab32(data); // swap data before writing
155 wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
156 writel(wpg_data, wpg_addr);
158 //--------------------------------------------------------------------
159 // READ - step 2 : clear the message buffer
161 wpg_data = swab32(data);
162 wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
163 writel(wpg_data, wpg_addr);
165 //--------------------------------------------------------------------
166 // READ - step 3 : issue start operation, I2C master control bit 30:ON
167 // 2020 : [20] OR operation at [20] offset 0x20
168 data = WPG_I2CMCNTL_STARTOP_MASK;
169 wpg_data = swab32(data);
170 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
171 writel(wpg_data, wpg_addr);
173 //--------------------------------------------------------------------
174 // READ - step 4 : wait until start operation bit clears
175 i = CMD_COMPLETE_TOUT_SEC;
178 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
179 wpg_data = readl(wpg_addr);
180 data = swab32(wpg_data);
181 if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
186 debug("%s - Error : WPG timeout\n", __func__);
189 //--------------------------------------------------------------------
190 // READ - step 5 : read I2C status register
191 i = CMD_COMPLETE_TOUT_SEC;
194 wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
195 wpg_data = readl(wpg_addr);
196 data = swab32(wpg_data);
197 if (HPC_I2CSTATUS_CHECK(data))
202 debug("ctrl_read - Exit Error:I2C timeout\n");
206 //--------------------------------------------------------------------
207 // READ - step 6 : get DATA
208 wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
209 wpg_data = readl(wpg_addr);
210 data = swab32(wpg_data);
214 debug_polling("%s - Exit index[%x] status[%x]\n", __func__, index, status);
219 /*----------------------------------------------------------------------
220 * Name: i2c_ctrl_write
222 * Action: write to HPC over I2C
224 * Return 0 or error codes
225 *---------------------------------------------------------------------*/
226 static u8 i2c_ctrl_write(struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index, u8 cmd)
229 void __iomem *wpg_addr; // base addr + offset
230 unsigned long wpg_data; // data to/from WPG LOHI format
231 unsigned long ultemp;
232 unsigned long data; // actual data HILO format
235 debug_polling("%s - Entry WPGBbar[%p] index[%x] cmd[%x]\n", __func__, WPGBbar, index, cmd);
238 //--------------------------------------------------------------------
240 // write at address, byte length, I2C address (shifted), index
241 // or write direct, byte length, index
244 if (ctlr_ptr->ctlr_type == 0x02) {
245 data = WPG_WRITEATADDR_MASK;
246 // fill in I2C address
247 ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
248 ultemp = ultemp >> 1;
249 data |= (ultemp << 8);
252 data |= (unsigned long)index;
253 } else if (ctlr_ptr->ctlr_type == 0x04) {
254 data = WPG_WRITEDIRECT_MASK;
257 ultemp = (unsigned long)index;
258 ultemp = ultemp << 8;
261 err("this controller type is not supported \n");
265 wpg_data = swab32(data); // swap data before writing
266 wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
267 writel(wpg_data, wpg_addr);
269 //--------------------------------------------------------------------
270 // WRITE - step 2 : clear the message buffer
271 data = 0x00000000 | (unsigned long)cmd;
272 wpg_data = swab32(data);
273 wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
274 writel(wpg_data, wpg_addr);
276 //--------------------------------------------------------------------
277 // WRITE - step 3 : issue start operation,I2C master control bit 30:ON
278 // 2020 : [20] OR operation at [20] offset 0x20
279 data = WPG_I2CMCNTL_STARTOP_MASK;
280 wpg_data = swab32(data);
281 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
282 writel(wpg_data, wpg_addr);
284 //--------------------------------------------------------------------
285 // WRITE - step 4 : wait until start operation bit clears
286 i = CMD_COMPLETE_TOUT_SEC;
289 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
290 wpg_data = readl(wpg_addr);
291 data = swab32(wpg_data);
292 if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
297 debug("%s - Exit Error:WPG timeout\n", __func__);
301 //--------------------------------------------------------------------
302 // WRITE - step 5 : read I2C status register
303 i = CMD_COMPLETE_TOUT_SEC;
306 wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
307 wpg_data = readl(wpg_addr);
308 data = swab32(wpg_data);
309 if (HPC_I2CSTATUS_CHECK(data))
314 debug("ctrl_read - Error : I2C timeout\n");
318 debug_polling("%s Exit rc[%x]\n", __func__, rc);
322 //------------------------------------------------------------
323 // Read from ISA type HPC
324 //------------------------------------------------------------
325 static u8 isa_ctrl_read(struct controller *ctlr_ptr, u8 offset)
330 start_address = ctlr_ptr->u.isa_ctlr.io_start;
331 data = inb(start_address + offset);
335 //--------------------------------------------------------------
336 // Write to ISA type HPC
337 //--------------------------------------------------------------
338 static void isa_ctrl_write(struct controller *ctlr_ptr, u8 offset, u8 data)
343 start_address = ctlr_ptr->u.isa_ctlr.io_start;
344 port_address = start_address + (u16) offset;
345 outb(data, port_address);
348 static u8 pci_ctrl_read(struct controller *ctrl, u8 offset)
351 debug("inside pci_ctrl_read\n");
353 pci_read_config_byte(ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, &data);
357 static u8 pci_ctrl_write(struct controller *ctrl, u8 offset, u8 data)
360 debug("inside pci_ctrl_write\n");
361 if (ctrl->ctrl_dev) {
362 pci_write_config_byte(ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, data);
368 static u8 ctrl_read(struct controller *ctlr, void __iomem *base, u8 offset)
371 switch (ctlr->ctlr_type) {
373 rc = isa_ctrl_read(ctlr, offset);
376 rc = pci_ctrl_read(ctlr, offset);
380 rc = i2c_ctrl_read(ctlr, base, offset);
388 static u8 ctrl_write(struct controller *ctlr, void __iomem *base, u8 offset, u8 data)
391 switch (ctlr->ctlr_type) {
393 isa_ctrl_write(ctlr, offset, data);
396 rc = pci_ctrl_write(ctlr, offset, data);
400 rc = i2c_ctrl_write(ctlr, base, offset, data);
407 /*----------------------------------------------------------------------
408 * Name: hpc_writecmdtoindex()
410 * Action: convert a write command to proper index within a controller
412 * Return index, HPC_ERROR
413 *---------------------------------------------------------------------*/
414 static u8 hpc_writecmdtoindex(u8 cmd, u8 index)
419 case HPC_CTLR_ENABLEIRQ: // 0x00.N.15
420 case HPC_CTLR_CLEARIRQ: // 0x06.N.15
421 case HPC_CTLR_RESET: // 0x07.N.15
422 case HPC_CTLR_IRQSTEER: // 0x08.N.15
423 case HPC_CTLR_DISABLEIRQ: // 0x01.N.15
424 case HPC_ALLSLOT_ON: // 0x11.N.15
425 case HPC_ALLSLOT_OFF: // 0x12.N.15
429 case HPC_SLOT_OFF: // 0x02.Y.0-14
430 case HPC_SLOT_ON: // 0x03.Y.0-14
431 case HPC_SLOT_ATTNOFF: // 0x04.N.0-14
432 case HPC_SLOT_ATTNON: // 0x05.N.0-14
433 case HPC_SLOT_BLINKLED: // 0x13.N.0-14
437 case HPC_BUS_33CONVMODE:
438 case HPC_BUS_66CONVMODE:
439 case HPC_BUS_66PCIXMODE:
440 case HPC_BUS_100PCIXMODE:
441 case HPC_BUS_133PCIXMODE:
442 rc = index + WPG_1ST_BUS_INDEX - 1;
446 err("hpc_writecmdtoindex - Error invalid cmd[%x]\n", cmd);
453 /*----------------------------------------------------------------------
454 * Name: hpc_readcmdtoindex()
456 * Action: convert a read command to proper index within a controller
458 * Return index, HPC_ERROR
459 *---------------------------------------------------------------------*/
460 static u8 hpc_readcmdtoindex(u8 cmd, u8 index)
465 case READ_CTLRSTATUS:
468 case READ_SLOTSTATUS:
472 case READ_EXTSLOTSTATUS:
473 rc = index + WPG_1ST_EXTSLOT_INDEX;
476 rc = index + WPG_1ST_BUS_INDEX - 1;
478 case READ_SLOTLATCHLOWREG:
484 case READ_HPCOPTIONS:
493 /*----------------------------------------------------------------------
494 * Name: HPCreadslot()
496 * Action: issue a READ command to HPC
498 * Input: pslot - cannot be NULL for READ_ALLSTAT
499 * pstatus - can be NULL for READ_ALLSTAT
501 * Return 0 or error codes
502 *---------------------------------------------------------------------*/
503 int ibmphp_hpc_readslot(struct slot *pslot, u8 cmd, u8 *pstatus)
505 void __iomem *wpg_bbar = NULL;
506 struct controller *ctlr_ptr;
511 debug_polling("%s - Entry pslot[%p] cmd[%x] pstatus[%p]\n", __func__, pslot, cmd, pstatus);
514 || ((pstatus == NULL) && (cmd != READ_ALLSTAT) && (cmd != READ_BUSSTATUS))) {
516 err("%s - Error invalid pointer, rc[%d]\n", __func__, rc);
520 if (cmd == READ_BUSSTATUS) {
521 busindex = ibmphp_get_bus_index(pslot->bus);
524 err("%s - Exit Error:invalid bus, rc[%d]\n", __func__, rc);
527 index = (u8) busindex;
529 index = pslot->ctlr_index;
531 index = hpc_readcmdtoindex(cmd, index);
533 if (index == HPC_ERROR) {
535 err("%s - Exit Error:invalid index, rc[%d]\n", __func__, rc);
539 ctlr_ptr = pslot->ctrl;
543 //--------------------------------------------------------------------
544 // map physical address to logical address
545 //--------------------------------------------------------------------
546 if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
547 wpg_bbar = ioremap(ctlr_ptr->u.wpeg_ctlr.wpegbbar, WPG_I2C_IOREMAP_SIZE);
549 //--------------------------------------------------------------------
550 // check controller status before reading
551 //--------------------------------------------------------------------
552 rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar, &status);
556 // update the slot structure
557 pslot->ctrl->status = status;
558 pslot->status = ctrl_read(ctlr_ptr, wpg_bbar, index);
559 rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar,
562 pslot->ext_status = ctrl_read(ctlr_ptr, wpg_bbar, index + WPG_1ST_EXTSLOT_INDEX);
566 case READ_SLOTSTATUS:
567 // DO NOT update the slot structure
568 *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
571 case READ_EXTSLOTSTATUS:
572 // DO NOT update the slot structure
573 *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
576 case READ_CTLRSTATUS:
577 // DO NOT update the slot structure
582 pslot->busstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
585 *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
587 case READ_HPCOPTIONS:
588 *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
590 case READ_SLOTLATCHLOWREG:
591 // DO NOT update the slot structure
592 *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
597 list_for_each_entry(pslot, &ibmphp_slot_head,
599 index = pslot->ctlr_index;
600 rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr,
603 pslot->status = ctrl_read(ctlr_ptr, wpg_bbar, index);
604 rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT,
605 ctlr_ptr, wpg_bbar, &status);
608 ctrl_read(ctlr_ptr, wpg_bbar,
609 index + WPG_1ST_EXTSLOT_INDEX);
611 err("%s - Error ctrl_read failed\n", __func__);
622 //--------------------------------------------------------------------
624 //--------------------------------------------------------------------
626 // remove physical to logical address mapping
627 if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
632 debug_polling("%s - Exit rc[%d]\n", __func__, rc);
636 /*----------------------------------------------------------------------
637 * Name: ibmphp_hpc_writeslot()
639 * Action: issue a WRITE command to HPC
640 *---------------------------------------------------------------------*/
641 int ibmphp_hpc_writeslot(struct slot *pslot, u8 cmd)
643 void __iomem *wpg_bbar = NULL;
644 struct controller *ctlr_ptr;
651 debug_polling("%s - Entry pslot[%p] cmd[%x]\n", __func__, pslot, cmd);
654 err("%s - Error Exit rc[%d]\n", __func__, rc);
658 if ((cmd == HPC_BUS_33CONVMODE) || (cmd == HPC_BUS_66CONVMODE) ||
659 (cmd == HPC_BUS_66PCIXMODE) || (cmd == HPC_BUS_100PCIXMODE) ||
660 (cmd == HPC_BUS_133PCIXMODE)) {
661 busindex = ibmphp_get_bus_index(pslot->bus);
664 err("%s - Exit Error:invalid bus, rc[%d]\n", __func__, rc);
667 index = (u8) busindex;
669 index = pslot->ctlr_index;
671 index = hpc_writecmdtoindex(cmd, index);
673 if (index == HPC_ERROR) {
675 err("%s - Error Exit rc[%d]\n", __func__, rc);
679 ctlr_ptr = pslot->ctrl;
683 //--------------------------------------------------------------------
684 // map physical address to logical address
685 //--------------------------------------------------------------------
686 if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4)) {
687 wpg_bbar = ioremap(ctlr_ptr->u.wpeg_ctlr.wpegbbar, WPG_I2C_IOREMAP_SIZE);
689 debug("%s - ctlr id[%x] physical[%lx] logical[%lx] i2c[%x]\n", __func__,
690 ctlr_ptr->ctlr_id, (ulong) (ctlr_ptr->u.wpeg_ctlr.wpegbbar), (ulong) wpg_bbar,
691 ctlr_ptr->u.wpeg_ctlr.i2c_addr);
693 //--------------------------------------------------------------------
694 // check controller status before writing
695 //--------------------------------------------------------------------
696 rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar, &status);
699 ctrl_write(ctlr_ptr, wpg_bbar, index, cmd);
701 //--------------------------------------------------------------------
702 // check controller is still not working on the command
703 //--------------------------------------------------------------------
704 timeout = CMD_COMPLETE_TOUT_SEC;
707 rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar,
710 if (NEEDTOCHECK_CMDSTATUS(cmd)) {
711 if (CTLR_FINISHED(status) == HPC_CTLR_FINISHED_YES)
720 err("%s - Error command complete timeout\n", __func__);
726 ctlr_ptr->status = status;
730 // remove physical to logical address mapping
731 if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
735 debug_polling("%s - Exit rc[%d]\n", __func__, rc);
739 /*----------------------------------------------------------------------
740 * Name: get_hpc_access()
742 * Action: make sure only one process can access HPC at one time
743 *---------------------------------------------------------------------*/
744 static void get_hpc_access(void)
746 mutex_lock(&sem_hpcaccess);
749 /*----------------------------------------------------------------------
750 * Name: free_hpc_access()
751 *---------------------------------------------------------------------*/
752 void free_hpc_access(void)
754 mutex_unlock(&sem_hpcaccess);
757 /*----------------------------------------------------------------------
758 * Name: ibmphp_lock_operations()
760 * Action: make sure only one process can change the data structure
761 *---------------------------------------------------------------------*/
762 void ibmphp_lock_operations(void)
764 mutex_lock(&operations_mutex);
768 /*----------------------------------------------------------------------
769 * Name: ibmphp_unlock_operations()
770 *---------------------------------------------------------------------*/
771 void ibmphp_unlock_operations(void)
773 debug("%s - Entry\n", __func__);
774 mutex_unlock(&operations_mutex);
776 debug("%s - Exit\n", __func__);
779 /*----------------------------------------------------------------------
781 *---------------------------------------------------------------------*/
782 #define POLL_LATCH_REGISTER 0
785 static int poll_hpc(void *data)
788 struct slot *pslot = NULL;
790 int poll_state = POLL_LATCH_REGISTER;
791 u8 oldlatchlow = 0x00;
792 u8 curlatchlow = 0x00;
794 u8 ctrl_count = 0x00;
796 debug("%s - Entry\n", __func__);
798 while (!kthread_should_stop()) {
799 /* try to get the lock to do some kind of hardware access */
800 mutex_lock(&operations_mutex);
802 switch (poll_state) {
803 case POLL_LATCH_REGISTER:
804 oldlatchlow = curlatchlow;
806 list_for_each_entry(pslot, &ibmphp_slot_head,
808 if (ctrl_count >= ibmphp_get_total_controllers())
810 if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
812 if (READ_SLOT_LATCH(pslot->ctrl)) {
813 rc = ibmphp_hpc_readslot(pslot,
814 READ_SLOTLATCHLOWREG,
816 if (oldlatchlow != curlatchlow)
817 process_changeinlatch(oldlatchlow,
824 poll_state = POLL_SLEEP;
827 list_for_each_entry(pslot, &ibmphp_slot_head,
829 // make a copy of the old status
830 memcpy((void *) &myslot, (void *) pslot,
831 sizeof(struct slot));
832 rc = ibmphp_hpc_readslot(pslot, READ_ALLSTAT, NULL);
833 if ((myslot.status != pslot->status)
834 || (myslot.ext_status != pslot->ext_status))
835 process_changeinstatus(pslot, &myslot);
838 list_for_each_entry(pslot, &ibmphp_slot_head,
840 if (ctrl_count >= ibmphp_get_total_controllers())
842 if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
844 if (READ_SLOT_LATCH(pslot->ctrl))
845 rc = ibmphp_hpc_readslot(pslot,
846 READ_SLOTLATCHLOWREG,
851 poll_state = POLL_SLEEP;
854 /* don't sleep with a lock on the hardware */
855 mutex_unlock(&operations_mutex);
856 msleep(POLL_INTERVAL_SEC * 1000);
858 if (kthread_should_stop())
861 mutex_lock(&operations_mutex);
863 if (poll_count >= POLL_LATCH_CNT) {
865 poll_state = POLL_SLOTS;
867 poll_state = POLL_LATCH_REGISTER;
870 /* give up the hardware semaphore */
871 mutex_unlock(&operations_mutex);
872 /* sleep for a short time just for good measure */
876 complete(&exit_complete);
877 debug("%s - Exit\n", __func__);
882 /*----------------------------------------------------------------------
883 * Name: process_changeinstatus
885 * Action: compare old and new slot status, process the change in status
887 * Input: pointer to slot struct, old slot struct
889 * Return 0 or error codes
896 *---------------------------------------------------------------------*/
897 static int process_changeinstatus(struct slot *pslot, struct slot *poldslot)
904 debug("process_changeinstatus - Entry pslot[%p], poldslot[%p]\n", pslot, poldslot);
906 // bit 0 - HPC_SLOT_POWER
907 if ((pslot->status & 0x01) != (poldslot->status & 0x01))
910 // bit 1 - HPC_SLOT_CONNECT
913 // bit 2 - HPC_SLOT_ATTN
914 if ((pslot->status & 0x04) != (poldslot->status & 0x04))
917 // bit 3 - HPC_SLOT_PRSNT2
918 // bit 4 - HPC_SLOT_PRSNT1
919 if (((pslot->status & 0x08) != (poldslot->status & 0x08))
920 || ((pslot->status & 0x10) != (poldslot->status & 0x10)))
923 // bit 5 - HPC_SLOT_PWRGD
924 if ((pslot->status & 0x20) != (poldslot->status & 0x20))
925 // OFF -> ON: ignore, ON -> OFF: disable slot
926 if ((poldslot->status & 0x20) && (SLOT_CONNECT(poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT(poldslot->status)))
929 // bit 6 - HPC_SLOT_BUS_SPEED
932 // bit 7 - HPC_SLOT_LATCH
933 if ((pslot->status & 0x80) != (poldslot->status & 0x80)) {
936 if (pslot->status & 0x80) {
937 if (SLOT_PWRGD(pslot->status)) {
938 // power goes on and off after closing latch
939 // check again to make sure power is still ON
941 rc = ibmphp_hpc_readslot(pslot, READ_SLOTSTATUS, &status);
942 if (SLOT_PWRGD(status))
944 else // overwrite power in pslot to OFF
945 pslot->status &= ~HPC_SLOT_POWER;
949 else if ((SLOT_PWRGD(poldslot->status) == HPC_SLOT_PWRGD_GOOD)
950 && (SLOT_CONNECT(poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT(poldslot->status))) {
955 // bit 4 - HPC_SLOT_BLINK_ATTN
956 if ((pslot->ext_status & 0x08) != (poldslot->ext_status & 0x08))
960 debug("process_changeinstatus - disable slot\n");
962 rc = ibmphp_do_disable_slot(pslot);
965 if (update || disable)
966 ibmphp_update_slot_info(pslot);
968 debug("%s - Exit rc[%d] disable[%x] update[%x]\n", __func__, rc, disable, update);
973 /*----------------------------------------------------------------------
974 * Name: process_changeinlatch
976 * Action: compare old and new latch reg status, process the change
978 * Input: old and current latch register status
980 * Return 0 or error codes
982 *---------------------------------------------------------------------*/
983 static int process_changeinlatch(u8 old, u8 new, struct controller *ctrl)
985 struct slot myslot, *pslot;
990 debug("%s - Entry old[%x], new[%x]\n", __func__, old, new);
991 // bit 0 reserved, 0 is LSB, check bit 1-6 for 6 slots
993 for (i = ctrl->starting_slot_num; i <= ctrl->ending_slot_num; i++) {
995 if ((mask & old) != (mask & new)) {
996 pslot = ibmphp_get_slot_from_physical_num(i);
998 memcpy((void *) &myslot, (void *) pslot, sizeof(struct slot));
999 rc = ibmphp_hpc_readslot(pslot, READ_ALLSTAT, NULL);
1000 debug("%s - call process_changeinstatus for slot[%d]\n", __func__, i);
1001 process_changeinstatus(pslot, &myslot);
1004 err("%s - Error bad pointer for slot[%d]\n", __func__, i);
1008 debug("%s - Exit rc[%d]\n", __func__, rc);
1012 /*----------------------------------------------------------------------
1013 * Name: ibmphp_hpc_start_poll_thread
1015 * Action: start polling thread
1016 *---------------------------------------------------------------------*/
1017 int __init ibmphp_hpc_start_poll_thread(void)
1019 debug("%s - Entry\n", __func__);
1021 ibmphp_poll_thread = kthread_run(poll_hpc, NULL, "hpc_poll");
1022 if (IS_ERR(ibmphp_poll_thread)) {
1023 err("%s - Error, thread not started\n", __func__);
1024 return PTR_ERR(ibmphp_poll_thread);
1029 /*----------------------------------------------------------------------
1030 * Name: ibmphp_hpc_stop_poll_thread
1032 * Action: stop polling thread and cleanup
1033 *---------------------------------------------------------------------*/
1034 void __exit ibmphp_hpc_stop_poll_thread(void)
1036 debug("%s - Entry\n", __func__);
1038 kthread_stop(ibmphp_poll_thread);
1039 debug("before locking operations\n");
1040 ibmphp_lock_operations();
1041 debug("after locking operations\n");
1043 // wait for poll thread to exit
1044 debug("before exit_complete down\n");
1045 wait_for_completion(&exit_complete);
1046 debug("after exit_completion down\n");
1049 debug("before free_hpc_access\n");
1051 debug("after free_hpc_access\n");
1052 ibmphp_unlock_operations();
1053 debug("after unlock operations\n");
1055 debug("%s - Exit\n", __func__);
1058 /*----------------------------------------------------------------------
1059 * Name: hpc_wait_ctlr_notworking
1061 * Action: wait until the controller is in a not working state
1063 * Return 0, HPC_ERROR
1065 *---------------------------------------------------------------------*/
1066 static int hpc_wait_ctlr_notworking(int timeout, struct controller *ctlr_ptr, void __iomem *wpg_bbar,
1072 debug_polling("hpc_wait_ctlr_notworking - Entry timeout[%d]\n", timeout);
1075 *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, WPG_CTLR_INDEX);
1076 if (*pstatus == HPC_ERROR) {
1080 if (CTLR_WORKING(*pstatus) == HPC_CTLR_WORKING_NO)
1086 err("HPCreadslot - Error ctlr timeout\n");
1092 debug_polling("hpc_wait_ctlr_notworking - Exit rc[%x] status[%x]\n", rc, *pstatus);