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.
11 * Send feedback to <gregkh@us.ibm.com>
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
)
331 start_address
= ctlr_ptr
->u
.isa_ctlr
.io_start
;
332 end_address
= ctlr_ptr
->u
.isa_ctlr
.io_end
;
333 data
= inb(start_address
+ offset
);
337 //--------------------------------------------------------------
338 // Write to ISA type HPC
339 //--------------------------------------------------------------
340 static void isa_ctrl_write(struct controller
*ctlr_ptr
, u8 offset
, u8 data
)
345 start_address
= ctlr_ptr
->u
.isa_ctlr
.io_start
;
346 port_address
= start_address
+ (u16
) offset
;
347 outb(data
, port_address
);
350 static u8
pci_ctrl_read(struct controller
*ctrl
, u8 offset
)
353 debug("inside pci_ctrl_read\n");
355 pci_read_config_byte(ctrl
->ctrl_dev
, HPC_PCI_OFFSET
+ offset
, &data
);
359 static u8
pci_ctrl_write(struct controller
*ctrl
, u8 offset
, u8 data
)
362 debug("inside pci_ctrl_write\n");
363 if (ctrl
->ctrl_dev
) {
364 pci_write_config_byte(ctrl
->ctrl_dev
, HPC_PCI_OFFSET
+ offset
, data
);
370 static u8
ctrl_read(struct controller
*ctlr
, void __iomem
*base
, u8 offset
)
373 switch (ctlr
->ctlr_type
) {
375 rc
= isa_ctrl_read(ctlr
, offset
);
378 rc
= pci_ctrl_read(ctlr
, offset
);
382 rc
= i2c_ctrl_read(ctlr
, base
, offset
);
390 static u8
ctrl_write(struct controller
*ctlr
, void __iomem
*base
, u8 offset
, u8 data
)
393 switch (ctlr
->ctlr_type
) {
395 isa_ctrl_write(ctlr
, offset
, data
);
398 rc
= pci_ctrl_write(ctlr
, offset
, data
);
402 rc
= i2c_ctrl_write(ctlr
, base
, offset
, data
);
409 /*----------------------------------------------------------------------
410 * Name: hpc_writecmdtoindex()
412 * Action: convert a write command to proper index within a controller
414 * Return index, HPC_ERROR
415 *---------------------------------------------------------------------*/
416 static u8
hpc_writecmdtoindex(u8 cmd
, u8 index
)
421 case HPC_CTLR_ENABLEIRQ
: // 0x00.N.15
422 case HPC_CTLR_CLEARIRQ
: // 0x06.N.15
423 case HPC_CTLR_RESET
: // 0x07.N.15
424 case HPC_CTLR_IRQSTEER
: // 0x08.N.15
425 case HPC_CTLR_DISABLEIRQ
: // 0x01.N.15
426 case HPC_ALLSLOT_ON
: // 0x11.N.15
427 case HPC_ALLSLOT_OFF
: // 0x12.N.15
431 case HPC_SLOT_OFF
: // 0x02.Y.0-14
432 case HPC_SLOT_ON
: // 0x03.Y.0-14
433 case HPC_SLOT_ATTNOFF
: // 0x04.N.0-14
434 case HPC_SLOT_ATTNON
: // 0x05.N.0-14
435 case HPC_SLOT_BLINKLED
: // 0x13.N.0-14
439 case HPC_BUS_33CONVMODE
:
440 case HPC_BUS_66CONVMODE
:
441 case HPC_BUS_66PCIXMODE
:
442 case HPC_BUS_100PCIXMODE
:
443 case HPC_BUS_133PCIXMODE
:
444 rc
= index
+ WPG_1ST_BUS_INDEX
- 1;
448 err("hpc_writecmdtoindex - Error invalid cmd[%x]\n", cmd
);
455 /*----------------------------------------------------------------------
456 * Name: hpc_readcmdtoindex()
458 * Action: convert a read command to proper index within a controller
460 * Return index, HPC_ERROR
461 *---------------------------------------------------------------------*/
462 static u8
hpc_readcmdtoindex(u8 cmd
, u8 index
)
467 case READ_CTLRSTATUS
:
470 case READ_SLOTSTATUS
:
474 case READ_EXTSLOTSTATUS
:
475 rc
= index
+ WPG_1ST_EXTSLOT_INDEX
;
478 rc
= index
+ WPG_1ST_BUS_INDEX
- 1;
480 case READ_SLOTLATCHLOWREG
:
486 case READ_HPCOPTIONS
:
495 /*----------------------------------------------------------------------
496 * Name: HPCreadslot()
498 * Action: issue a READ command to HPC
500 * Input: pslot - cannot be NULL for READ_ALLSTAT
501 * pstatus - can be NULL for READ_ALLSTAT
503 * Return 0 or error codes
504 *---------------------------------------------------------------------*/
505 int ibmphp_hpc_readslot(struct slot
*pslot
, u8 cmd
, u8
*pstatus
)
507 void __iomem
*wpg_bbar
= NULL
;
508 struct controller
*ctlr_ptr
;
513 debug_polling("%s - Entry pslot[%p] cmd[%x] pstatus[%p]\n", __func__
, pslot
, cmd
, pstatus
);
516 || ((pstatus
== NULL
) && (cmd
!= READ_ALLSTAT
) && (cmd
!= READ_BUSSTATUS
))) {
518 err("%s - Error invalid pointer, rc[%d]\n", __func__
, rc
);
522 if (cmd
== READ_BUSSTATUS
) {
523 busindex
= ibmphp_get_bus_index(pslot
->bus
);
526 err("%s - Exit Error:invalid bus, rc[%d]\n", __func__
, rc
);
529 index
= (u8
) busindex
;
531 index
= pslot
->ctlr_index
;
533 index
= hpc_readcmdtoindex(cmd
, index
);
535 if (index
== HPC_ERROR
) {
537 err("%s - Exit Error:invalid index, rc[%d]\n", __func__
, rc
);
541 ctlr_ptr
= pslot
->ctrl
;
545 //--------------------------------------------------------------------
546 // map physical address to logical address
547 //--------------------------------------------------------------------
548 if ((ctlr_ptr
->ctlr_type
== 2) || (ctlr_ptr
->ctlr_type
== 4))
549 wpg_bbar
= ioremap(ctlr_ptr
->u
.wpeg_ctlr
.wpegbbar
, WPG_I2C_IOREMAP_SIZE
);
551 //--------------------------------------------------------------------
552 // check controller status before reading
553 //--------------------------------------------------------------------
554 rc
= hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT
, ctlr_ptr
, wpg_bbar
, &status
);
558 // update the slot structure
559 pslot
->ctrl
->status
= status
;
560 pslot
->status
= ctrl_read(ctlr_ptr
, wpg_bbar
, index
);
561 rc
= hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT
, ctlr_ptr
, wpg_bbar
,
564 pslot
->ext_status
= ctrl_read(ctlr_ptr
, wpg_bbar
, index
+ WPG_1ST_EXTSLOT_INDEX
);
568 case READ_SLOTSTATUS
:
569 // DO NOT update the slot structure
570 *pstatus
= ctrl_read(ctlr_ptr
, wpg_bbar
, index
);
573 case READ_EXTSLOTSTATUS
:
574 // DO NOT update the slot structure
575 *pstatus
= ctrl_read(ctlr_ptr
, wpg_bbar
, index
);
578 case READ_CTLRSTATUS
:
579 // DO NOT update the slot structure
584 pslot
->busstatus
= ctrl_read(ctlr_ptr
, wpg_bbar
, index
);
587 *pstatus
= ctrl_read(ctlr_ptr
, wpg_bbar
, index
);
589 case READ_HPCOPTIONS
:
590 *pstatus
= ctrl_read(ctlr_ptr
, wpg_bbar
, index
);
592 case READ_SLOTLATCHLOWREG
:
593 // DO NOT update the slot structure
594 *pstatus
= ctrl_read(ctlr_ptr
, wpg_bbar
, index
);
599 list_for_each_entry(pslot
, &ibmphp_slot_head
,
601 index
= pslot
->ctlr_index
;
602 rc
= hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT
, ctlr_ptr
,
605 pslot
->status
= ctrl_read(ctlr_ptr
, wpg_bbar
, index
);
606 rc
= hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT
,
607 ctlr_ptr
, wpg_bbar
, &status
);
610 ctrl_read(ctlr_ptr
, wpg_bbar
,
611 index
+ WPG_1ST_EXTSLOT_INDEX
);
613 err("%s - Error ctrl_read failed\n", __func__
);
624 //--------------------------------------------------------------------
626 //--------------------------------------------------------------------
628 // remove physical to logical address mapping
629 if ((ctlr_ptr
->ctlr_type
== 2) || (ctlr_ptr
->ctlr_type
== 4))
634 debug_polling("%s - Exit rc[%d]\n", __func__
, rc
);
638 /*----------------------------------------------------------------------
639 * Name: ibmphp_hpc_writeslot()
641 * Action: issue a WRITE command to HPC
642 *---------------------------------------------------------------------*/
643 int ibmphp_hpc_writeslot(struct slot
*pslot
, u8 cmd
)
645 void __iomem
*wpg_bbar
= NULL
;
646 struct controller
*ctlr_ptr
;
653 debug_polling("%s - Entry pslot[%p] cmd[%x]\n", __func__
, pslot
, cmd
);
656 err("%s - Error Exit rc[%d]\n", __func__
, rc
);
660 if ((cmd
== HPC_BUS_33CONVMODE
) || (cmd
== HPC_BUS_66CONVMODE
) ||
661 (cmd
== HPC_BUS_66PCIXMODE
) || (cmd
== HPC_BUS_100PCIXMODE
) ||
662 (cmd
== HPC_BUS_133PCIXMODE
)) {
663 busindex
= ibmphp_get_bus_index(pslot
->bus
);
666 err("%s - Exit Error:invalid bus, rc[%d]\n", __func__
, rc
);
669 index
= (u8
) busindex
;
671 index
= pslot
->ctlr_index
;
673 index
= hpc_writecmdtoindex(cmd
, index
);
675 if (index
== HPC_ERROR
) {
677 err("%s - Error Exit rc[%d]\n", __func__
, rc
);
681 ctlr_ptr
= pslot
->ctrl
;
685 //--------------------------------------------------------------------
686 // map physical address to logical address
687 //--------------------------------------------------------------------
688 if ((ctlr_ptr
->ctlr_type
== 2) || (ctlr_ptr
->ctlr_type
== 4)) {
689 wpg_bbar
= ioremap(ctlr_ptr
->u
.wpeg_ctlr
.wpegbbar
, WPG_I2C_IOREMAP_SIZE
);
691 debug("%s - ctlr id[%x] physical[%lx] logical[%lx] i2c[%x]\n", __func__
,
692 ctlr_ptr
->ctlr_id
, (ulong
) (ctlr_ptr
->u
.wpeg_ctlr
.wpegbbar
), (ulong
) wpg_bbar
,
693 ctlr_ptr
->u
.wpeg_ctlr
.i2c_addr
);
695 //--------------------------------------------------------------------
696 // check controller status before writing
697 //--------------------------------------------------------------------
698 rc
= hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT
, ctlr_ptr
, wpg_bbar
, &status
);
701 ctrl_write(ctlr_ptr
, wpg_bbar
, index
, cmd
);
703 //--------------------------------------------------------------------
704 // check controller is still not working on the command
705 //--------------------------------------------------------------------
706 timeout
= CMD_COMPLETE_TOUT_SEC
;
709 rc
= hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT
, ctlr_ptr
, wpg_bbar
,
712 if (NEEDTOCHECK_CMDSTATUS(cmd
)) {
713 if (CTLR_FINISHED(status
) == HPC_CTLR_FINISHED_YES
)
722 err("%s - Error command complete timeout\n", __func__
);
728 ctlr_ptr
->status
= status
;
732 // remove physical to logical address mapping
733 if ((ctlr_ptr
->ctlr_type
== 2) || (ctlr_ptr
->ctlr_type
== 4))
737 debug_polling("%s - Exit rc[%d]\n", __func__
, rc
);
741 /*----------------------------------------------------------------------
742 * Name: get_hpc_access()
744 * Action: make sure only one process can access HPC at one time
745 *---------------------------------------------------------------------*/
746 static void get_hpc_access(void)
748 mutex_lock(&sem_hpcaccess
);
751 /*----------------------------------------------------------------------
752 * Name: free_hpc_access()
753 *---------------------------------------------------------------------*/
754 void free_hpc_access(void)
756 mutex_unlock(&sem_hpcaccess
);
759 /*----------------------------------------------------------------------
760 * Name: ibmphp_lock_operations()
762 * Action: make sure only one process can change the data structure
763 *---------------------------------------------------------------------*/
764 void ibmphp_lock_operations(void)
766 mutex_lock(&operations_mutex
);
770 /*----------------------------------------------------------------------
771 * Name: ibmphp_unlock_operations()
772 *---------------------------------------------------------------------*/
773 void ibmphp_unlock_operations(void)
775 debug("%s - Entry\n", __func__
);
776 mutex_unlock(&operations_mutex
);
778 debug("%s - Exit\n", __func__
);
781 /*----------------------------------------------------------------------
783 *---------------------------------------------------------------------*/
784 #define POLL_LATCH_REGISTER 0
787 static int poll_hpc(void *data
)
790 struct slot
*pslot
= NULL
;
792 int poll_state
= POLL_LATCH_REGISTER
;
793 u8 oldlatchlow
= 0x00;
794 u8 curlatchlow
= 0x00;
796 u8 ctrl_count
= 0x00;
798 debug("%s - Entry\n", __func__
);
800 while (!kthread_should_stop()) {
801 /* try to get the lock to do some kind of hardware access */
802 mutex_lock(&operations_mutex
);
804 switch (poll_state
) {
805 case POLL_LATCH_REGISTER
:
806 oldlatchlow
= curlatchlow
;
808 list_for_each_entry(pslot
, &ibmphp_slot_head
,
810 if (ctrl_count
>= ibmphp_get_total_controllers())
812 if (pslot
->ctrl
->ctlr_relative_id
== ctrl_count
) {
814 if (READ_SLOT_LATCH(pslot
->ctrl
)) {
815 rc
= ibmphp_hpc_readslot(pslot
,
816 READ_SLOTLATCHLOWREG
,
818 if (oldlatchlow
!= curlatchlow
)
819 process_changeinlatch(oldlatchlow
,
826 poll_state
= POLL_SLEEP
;
829 list_for_each_entry(pslot
, &ibmphp_slot_head
,
831 // make a copy of the old status
832 memcpy((void *) &myslot
, (void *) pslot
,
833 sizeof(struct slot
));
834 rc
= ibmphp_hpc_readslot(pslot
, READ_ALLSTAT
, NULL
);
835 if ((myslot
.status
!= pslot
->status
)
836 || (myslot
.ext_status
!= pslot
->ext_status
))
837 process_changeinstatus(pslot
, &myslot
);
840 list_for_each_entry(pslot
, &ibmphp_slot_head
,
842 if (ctrl_count
>= ibmphp_get_total_controllers())
844 if (pslot
->ctrl
->ctlr_relative_id
== ctrl_count
) {
846 if (READ_SLOT_LATCH(pslot
->ctrl
))
847 rc
= ibmphp_hpc_readslot(pslot
,
848 READ_SLOTLATCHLOWREG
,
853 poll_state
= POLL_SLEEP
;
856 /* don't sleep with a lock on the hardware */
857 mutex_unlock(&operations_mutex
);
858 msleep(POLL_INTERVAL_SEC
* 1000);
860 if (kthread_should_stop())
863 mutex_lock(&operations_mutex
);
865 if (poll_count
>= POLL_LATCH_CNT
) {
867 poll_state
= POLL_SLOTS
;
869 poll_state
= POLL_LATCH_REGISTER
;
872 /* give up the hardware semaphore */
873 mutex_unlock(&operations_mutex
);
874 /* sleep for a short time just for good measure */
878 complete(&exit_complete
);
879 debug("%s - Exit\n", __func__
);
884 /*----------------------------------------------------------------------
885 * Name: process_changeinstatus
887 * Action: compare old and new slot status, process the change in status
889 * Input: pointer to slot struct, old slot struct
891 * Return 0 or error codes
898 *---------------------------------------------------------------------*/
899 static int process_changeinstatus(struct slot
*pslot
, struct slot
*poldslot
)
906 debug("process_changeinstatus - Entry pslot[%p], poldslot[%p]\n", pslot
, poldslot
);
908 // bit 0 - HPC_SLOT_POWER
909 if ((pslot
->status
& 0x01) != (poldslot
->status
& 0x01))
912 // bit 1 - HPC_SLOT_CONNECT
915 // bit 2 - HPC_SLOT_ATTN
916 if ((pslot
->status
& 0x04) != (poldslot
->status
& 0x04))
919 // bit 3 - HPC_SLOT_PRSNT2
920 // bit 4 - HPC_SLOT_PRSNT1
921 if (((pslot
->status
& 0x08) != (poldslot
->status
& 0x08))
922 || ((pslot
->status
& 0x10) != (poldslot
->status
& 0x10)))
925 // bit 5 - HPC_SLOT_PWRGD
926 if ((pslot
->status
& 0x20) != (poldslot
->status
& 0x20))
927 // OFF -> ON: ignore, ON -> OFF: disable slot
928 if ((poldslot
->status
& 0x20) && (SLOT_CONNECT(poldslot
->status
) == HPC_SLOT_CONNECTED
) && (SLOT_PRESENT(poldslot
->status
)))
931 // bit 6 - HPC_SLOT_BUS_SPEED
934 // bit 7 - HPC_SLOT_LATCH
935 if ((pslot
->status
& 0x80) != (poldslot
->status
& 0x80)) {
938 if (pslot
->status
& 0x80) {
939 if (SLOT_PWRGD(pslot
->status
)) {
940 // power goes on and off after closing latch
941 // check again to make sure power is still ON
943 rc
= ibmphp_hpc_readslot(pslot
, READ_SLOTSTATUS
, &status
);
944 if (SLOT_PWRGD(status
))
946 else // overwrite power in pslot to OFF
947 pslot
->status
&= ~HPC_SLOT_POWER
;
951 else if ((SLOT_PWRGD(poldslot
->status
) == HPC_SLOT_PWRGD_GOOD
)
952 && (SLOT_CONNECT(poldslot
->status
) == HPC_SLOT_CONNECTED
) && (SLOT_PRESENT(poldslot
->status
))) {
957 // bit 4 - HPC_SLOT_BLINK_ATTN
958 if ((pslot
->ext_status
& 0x08) != (poldslot
->ext_status
& 0x08))
962 debug("process_changeinstatus - disable slot\n");
964 rc
= ibmphp_do_disable_slot(pslot
);
967 if (update
|| disable
)
968 ibmphp_update_slot_info(pslot
);
970 debug("%s - Exit rc[%d] disable[%x] update[%x]\n", __func__
, rc
, disable
, update
);
975 /*----------------------------------------------------------------------
976 * Name: process_changeinlatch
978 * Action: compare old and new latch reg status, process the change
980 * Input: old and current latch register status
982 * Return 0 or error codes
984 *---------------------------------------------------------------------*/
985 static int process_changeinlatch(u8 old
, u8
new, struct controller
*ctrl
)
987 struct slot myslot
, *pslot
;
992 debug("%s - Entry old[%x], new[%x]\n", __func__
, old
, new);
993 // bit 0 reserved, 0 is LSB, check bit 1-6 for 6 slots
995 for (i
= ctrl
->starting_slot_num
; i
<= ctrl
->ending_slot_num
; i
++) {
997 if ((mask
& old
) != (mask
& new)) {
998 pslot
= ibmphp_get_slot_from_physical_num(i
);
1000 memcpy((void *) &myslot
, (void *) pslot
, sizeof(struct slot
));
1001 rc
= ibmphp_hpc_readslot(pslot
, READ_ALLSTAT
, NULL
);
1002 debug("%s - call process_changeinstatus for slot[%d]\n", __func__
, i
);
1003 process_changeinstatus(pslot
, &myslot
);
1006 err("%s - Error bad pointer for slot[%d]\n", __func__
, i
);
1010 debug("%s - Exit rc[%d]\n", __func__
, rc
);
1014 /*----------------------------------------------------------------------
1015 * Name: ibmphp_hpc_start_poll_thread
1017 * Action: start polling thread
1018 *---------------------------------------------------------------------*/
1019 int __init
ibmphp_hpc_start_poll_thread(void)
1021 debug("%s - Entry\n", __func__
);
1023 ibmphp_poll_thread
= kthread_run(poll_hpc
, NULL
, "hpc_poll");
1024 if (IS_ERR(ibmphp_poll_thread
)) {
1025 err("%s - Error, thread not started\n", __func__
);
1026 return PTR_ERR(ibmphp_poll_thread
);
1031 /*----------------------------------------------------------------------
1032 * Name: ibmphp_hpc_stop_poll_thread
1034 * Action: stop polling thread and cleanup
1035 *---------------------------------------------------------------------*/
1036 void __exit
ibmphp_hpc_stop_poll_thread(void)
1038 debug("%s - Entry\n", __func__
);
1040 kthread_stop(ibmphp_poll_thread
);
1041 debug("before locking operations\n");
1042 ibmphp_lock_operations();
1043 debug("after locking operations\n");
1045 // wait for poll thread to exit
1046 debug("before exit_complete down\n");
1047 wait_for_completion(&exit_complete
);
1048 debug("after exit_completion down\n");
1051 debug("before free_hpc_access\n");
1053 debug("after free_hpc_access\n");
1054 ibmphp_unlock_operations();
1055 debug("after unlock operations\n");
1057 debug("%s - Exit\n", __func__
);
1060 /*----------------------------------------------------------------------
1061 * Name: hpc_wait_ctlr_notworking
1063 * Action: wait until the controller is in a not working state
1065 * Return 0, HPC_ERROR
1067 *---------------------------------------------------------------------*/
1068 static int hpc_wait_ctlr_notworking(int timeout
, struct controller
*ctlr_ptr
, void __iomem
*wpg_bbar
,
1074 debug_polling("hpc_wait_ctlr_notworking - Entry timeout[%d]\n", timeout
);
1077 *pstatus
= ctrl_read(ctlr_ptr
, wpg_bbar
, WPG_CTLR_INDEX
);
1078 if (*pstatus
== HPC_ERROR
) {
1082 if (CTLR_WORKING(*pstatus
) == HPC_CTLR_WORKING_NO
)
1088 err("HPCreadslot - Error ctlr timeout\n");
1094 debug_polling("hpc_wait_ctlr_notworking - Exit rc[%x] status[%x]\n", rc
, *pstatus
);