Return-Path: Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3wJmLq2flzzDqFV for ; Fri, 5 May 2017 06:08:51 +1000 (AEST) Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v44K3btn048965 for ; Thu, 4 May 2017 16:08:42 -0400 Received: from e12.ny.us.ibm.com (e12.ny.us.ibm.com [129.33.205.202]) by mx0b-001b2d01.pphosted.com with ESMTP id 2a89spbk51-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 04 May 2017 16:08:41 -0400 Received: from localhost by e12.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 4 May 2017 16:08:41 -0400 Received: from b01cxnp23032.gho.pok.ibm.com (9.57.198.27) by e12.ny.us.ibm.com (146.89.104.199) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 4 May 2017 16:08:39 -0400 Received: from b01ledav002.gho.pok.ibm.com (b01ledav002.gho.pok.ibm.com [9.57.199.107]) by b01cxnp23032.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v44K8dlO42860560; Thu, 4 May 2017 20:08:39 GMT Received: from b01ledav002.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 505D3124035; Thu, 4 May 2017 16:08:33 -0400 (EDT) Received: from oc3016140333.ibm.com (unknown [9.41.179.225]) by b01ledav002.gho.pok.ibm.com (Postfix) with ESMTP id EBBA9124044; Thu, 4 May 2017 16:08:32 -0400 (EDT) From: Eddie James To: openbmc@lists.ozlabs.org Cc: cbostic@linux.vnet.ibm.com, joel@jms.id.au, "Edward A. James" Subject: [PATCH linux dev-4.10 4/6] drivers: i2c: Add I2C master locking to FSI algorithm Date: Thu, 4 May 2017 15:08:23 -0500 X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1493928505-30816-1-git-send-email-eajames@linux.vnet.ibm.com> References: <1493928505-30816-1-git-send-email-eajames@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17050420-0048-0000-0000-000001775AF8 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007022; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000209; SDB=6.00856257; UDB=6.00423874; IPR=6.00635441; BA=6.00005326; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00015308; XFM=3.00000014; UTC=2017-05-04 20:08:40 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17050420-0049-0000-0000-000040FFCCFB Message-Id: <1493928505-30816-5-git-send-email-eajames@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-05-04_12:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1703280000 definitions=main-1705040290 X-BeenThere: openbmc@lists.ozlabs.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Development list for OpenBMC List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 May 2017 20:08:52 -0000 From: "Edward A. James" Since there are many ports per master, each with it's own adapter and chardev, we need some locking to prevent xfers from changingg the master state while other xfers are in progress. Signed-off-by: Edward A. James --- drivers/i2c/busses/i2c-fsi.c | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/drivers/i2c/busses/i2c-fsi.c b/drivers/i2c/busses/i2c-fsi.c index fa77a2b..3bc9bbe 100644 --- a/drivers/i2c/busses/i2c-fsi.c +++ b/drivers/i2c/busses/i2c-fsi.c @@ -138,6 +138,8 @@ struct fsi_i2c_master { unsigned long clk; struct list_head ports; struct ida ida; + wait_queue_head_t wait; + struct semaphore lock; }; struct fsi_i2c_port { @@ -173,6 +175,29 @@ static int fsi_i2c_write_reg(struct fsi_device *fsi, unsigned int reg, return fsi_device_write(fsi, reg, &_data, sizeof(_data)); } +static int fsi_i2c_lock_master(struct fsi_i2c_master *i2c, int timeout) +{ + int rc; + + rc = down_trylock(&i2c->lock); + if (!rc) + return 0; + + rc = wait_event_interruptible_timeout(i2c->wait, + !down_trylock(&i2c->lock), + timeout); + if (rc > 0) + return 0; + + return -EBUSY; +} + +static void fsi_i2c_unlock_master(struct fsi_i2c_master *i2c) +{ + up(&i2c->lock); + wake_up(&i2c->wait); +} + static unsigned long fsi_i2c_get_clk_div(struct fsi_i2c_master *i2c) { unsigned long clk_div = I2C_DEFAULT_CLK_DIV; @@ -421,25 +446,31 @@ static int fsi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, struct fsi_i2c_port *port = adap->algo_data; struct i2c_msg *msg; - rc = fsi_i2c_set_port(port); + rc = fsi_i2c_lock_master(port->master, adap->timeout); if (rc) return rc; + rc = fsi_i2c_set_port(port); + if (rc) + goto unlock; + for (i = 0; i < num; ++i) { msg = msgs + i; start_time = jiffies; rc = fsi_i2c_start(port, msg, i == num - 1); if (rc) - return rc; + goto unlock; rc = fsi_i2c_wait(port, msg, adap->timeout - (jiffies - start_time)); if (rc) - return rc; + goto unlock; } - return 0; +unlock: + fsi_i2c_unlock_master(port->master); + return rc; } static u32 fsi_i2c_functionality(struct i2c_adapter *adap) @@ -464,6 +495,8 @@ static int fsi_i2c_probe(struct device *dev) if (!i2c) return -ENOMEM; + init_waitqueue_head(&i2c->wait); + sema_init(&i2c->lock, 1); i2c->fsi = to_fsi_dev(dev); i2c->idx = ida_simple_get(&fsi_i2c_ida, 1, INT_MAX, GFP_KERNEL); i2c->clk = I2C_DEFAULT_CLK; -- 1.8.3.1