Linux Audio

Check our new training course

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