12908d778ab3e244900c310974e1fc1c69066e450James Bottomley/*
22908d778ab3e244900c310974e1fc1c69066e450James Bottomley * Aic94xx SAS/SATA DDB management
32908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
42908d778ab3e244900c310974e1fc1c69066e450James Bottomley * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
52908d778ab3e244900c310974e1fc1c69066e450James Bottomley * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
62908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
72908d778ab3e244900c310974e1fc1c69066e450James Bottomley * This file is licensed under GPLv2.
82908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
92908d778ab3e244900c310974e1fc1c69066e450James Bottomley * This file is part of the aic94xx driver.
102908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
112908d778ab3e244900c310974e1fc1c69066e450James Bottomley * The aic94xx driver is free software; you can redistribute it and/or
122908d778ab3e244900c310974e1fc1c69066e450James Bottomley * modify it under the terms of the GNU General Public License as
132908d778ab3e244900c310974e1fc1c69066e450James Bottomley * published by the Free Software Foundation; version 2 of the
142908d778ab3e244900c310974e1fc1c69066e450James Bottomley * License.
152908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
162908d778ab3e244900c310974e1fc1c69066e450James Bottomley * The aic94xx driver is distributed in the hope that it will be useful,
172908d778ab3e244900c310974e1fc1c69066e450James Bottomley * but WITHOUT ANY WARRANTY; without even the implied warranty of
182908d778ab3e244900c310974e1fc1c69066e450James Bottomley * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
192908d778ab3e244900c310974e1fc1c69066e450James Bottomley * General Public License for more details.
202908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
212908d778ab3e244900c310974e1fc1c69066e450James Bottomley * You should have received a copy of the GNU General Public License
222908d778ab3e244900c310974e1fc1c69066e450James Bottomley * along with the aic94xx driver; if not, write to the Free Software
232908d778ab3e244900c310974e1fc1c69066e450James Bottomley * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
242908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
252908d778ab3e244900c310974e1fc1c69066e450James Bottomley * $Id: //depot/aic94xx/aic94xx_dev.c#21 $
262908d778ab3e244900c310974e1fc1c69066e450James Bottomley */
272908d778ab3e244900c310974e1fc1c69066e450James Bottomley
282908d778ab3e244900c310974e1fc1c69066e450James Bottomley#include "aic94xx.h"
292908d778ab3e244900c310974e1fc1c69066e450James Bottomley#include "aic94xx_hwi.h"
302908d778ab3e244900c310974e1fc1c69066e450James Bottomley#include "aic94xx_reg.h"
312908d778ab3e244900c310974e1fc1c69066e450James Bottomley#include "aic94xx_sas.h"
322908d778ab3e244900c310974e1fc1c69066e450James Bottomley
332908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define FIND_FREE_DDB(_ha) find_first_zero_bit((_ha)->hw_prof.ddb_bitmap, \
342908d778ab3e244900c310974e1fc1c69066e450James Bottomley					       (_ha)->hw_prof.max_ddbs)
352908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define SET_DDB(_ddb, _ha) set_bit(_ddb, (_ha)->hw_prof.ddb_bitmap)
362908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define CLEAR_DDB(_ddb, _ha) clear_bit(_ddb, (_ha)->hw_prof.ddb_bitmap)
372908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3881e56ded878aeb8730f18c1d0a70d5face788be3Adrian Bunkstatic int asd_get_ddb(struct asd_ha_struct *asd_ha)
392908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
402908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int ddb, i;
412908d778ab3e244900c310974e1fc1c69066e450James Bottomley
422908d778ab3e244900c310974e1fc1c69066e450James Bottomley	ddb = FIND_FREE_DDB(asd_ha);
432908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (ddb >= asd_ha->hw_prof.max_ddbs) {
442908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ddb = -ENOMEM;
452908d778ab3e244900c310974e1fc1c69066e450James Bottomley		goto out;
462908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
472908d778ab3e244900c310974e1fc1c69066e450James Bottomley	SET_DDB(ddb, asd_ha);
482908d778ab3e244900c310974e1fc1c69066e450James Bottomley
492908d778ab3e244900c310974e1fc1c69066e450James Bottomley	for (i = 0; i < sizeof(struct asd_ddb_ssp_smp_target_port); i+= 4)
502908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_ddbsite_write_dword(asd_ha, ddb, i, 0);
512908d778ab3e244900c310974e1fc1c69066e450James Bottomleyout:
522908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return ddb;
532908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
542908d778ab3e244900c310974e1fc1c69066e450James Bottomley
552908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define INIT_CONN_TAG   offsetof(struct asd_ddb_ssp_smp_target_port, init_conn_tag)
562908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define DEST_SAS_ADDR   offsetof(struct asd_ddb_ssp_smp_target_port, dest_sas_addr)
572908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define SEND_QUEUE_HEAD offsetof(struct asd_ddb_ssp_smp_target_port, send_queue_head)
582908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define DDB_TYPE        offsetof(struct asd_ddb_ssp_smp_target_port, ddb_type)
592908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define CONN_MASK       offsetof(struct asd_ddb_ssp_smp_target_port, conn_mask)
602908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define DDB_TARG_FLAGS  offsetof(struct asd_ddb_ssp_smp_target_port, flags)
612908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define DDB_TARG_FLAGS2 offsetof(struct asd_ddb_stp_sata_target_port, flags2)
622908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define EXEC_QUEUE_TAIL offsetof(struct asd_ddb_ssp_smp_target_port, exec_queue_tail)
632908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define SEND_QUEUE_TAIL offsetof(struct asd_ddb_ssp_smp_target_port, send_queue_tail)
642908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define SISTER_DDB      offsetof(struct asd_ddb_ssp_smp_target_port, sister_ddb)
652908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define MAX_CCONN       offsetof(struct asd_ddb_ssp_smp_target_port, max_concurrent_conn)
662908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define NUM_CTX         offsetof(struct asd_ddb_ssp_smp_target_port, num_contexts)
672908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define ATA_CMD_SCBPTR  offsetof(struct asd_ddb_stp_sata_target_port, ata_cmd_scbptr)
682908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define SATA_TAG_ALLOC_MASK offsetof(struct asd_ddb_stp_sata_target_port, sata_tag_alloc_mask)
692908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define NUM_SATA_TAGS   offsetof(struct asd_ddb_stp_sata_target_port, num_sata_tags)
702908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define SATA_STATUS     offsetof(struct asd_ddb_stp_sata_target_port, sata_status)
712908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define NCQ_DATA_SCB_PTR offsetof(struct asd_ddb_stp_sata_target_port, ncq_data_scb_ptr)
722908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define ITNL_TIMEOUT    offsetof(struct asd_ddb_ssp_smp_target_port, itnl_timeout)
732908d778ab3e244900c310974e1fc1c69066e450James Bottomley
7481e56ded878aeb8730f18c1d0a70d5face788be3Adrian Bunkstatic void asd_free_ddb(struct asd_ha_struct *asd_ha, int ddb)
752908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
762908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (!ddb || ddb >= 0xFFFF)
772908d778ab3e244900c310974e1fc1c69066e450James Bottomley		return;
782908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_byte(asd_ha, ddb, DDB_TYPE, DDB_TYPE_UNUSED);
792908d778ab3e244900c310974e1fc1c69066e450James Bottomley	CLEAR_DDB(ddb, asd_ha);
802908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
812908d778ab3e244900c310974e1fc1c69066e450James Bottomley
8281e56ded878aeb8730f18c1d0a70d5face788be3Adrian Bunkstatic void asd_set_ddb_type(struct domain_device *dev)
832908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
842908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
852908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int ddb = (int) (unsigned long) dev->lldd_dev;
862908d778ab3e244900c310974e1fc1c69066e450James Bottomley
87aa9f8328fc51460e15da129caf622b6560fa8c99James Bottomley	if (dev->dev_type == SAS_SATA_PM_PORT)
882908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_ddbsite_write_byte(asd_ha,ddb, DDB_TYPE, DDB_TYPE_PM_PORT);
892908d778ab3e244900c310974e1fc1c69066e450James Bottomley	else if (dev->tproto)
902908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_ddbsite_write_byte(asd_ha,ddb, DDB_TYPE, DDB_TYPE_TARGET);
912908d778ab3e244900c310974e1fc1c69066e450James Bottomley	else
922908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_ddbsite_write_byte(asd_ha,ddb,DDB_TYPE,DDB_TYPE_INITIATOR);
932908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
942908d778ab3e244900c310974e1fc1c69066e450James Bottomley
952908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic int asd_init_sata_tag_ddb(struct domain_device *dev)
962908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
972908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
982908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int ddb, i;
992908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1002908d778ab3e244900c310974e1fc1c69066e450James Bottomley	ddb = asd_get_ddb(asd_ha);
1012908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (ddb < 0)
1022908d778ab3e244900c310974e1fc1c69066e450James Bottomley		return ddb;
1032908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1042908d778ab3e244900c310974e1fc1c69066e450James Bottomley	for (i = 0; i < sizeof(struct asd_ddb_sata_tag); i += 2)
1052908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_ddbsite_write_word(asd_ha, ddb, i, 0xFFFF);
1062908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1072908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_word(asd_ha, (int) (unsigned long) dev->lldd_dev,
1082908d778ab3e244900c310974e1fc1c69066e450James Bottomley			       SISTER_DDB, ddb);
1092908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return 0;
1102908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
1112908d778ab3e244900c310974e1fc1c69066e450James Bottomley
112b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williamsvoid asd_set_dmamode(struct domain_device *dev)
1132908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
1142908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
115b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams	struct ata_device *ata_dev = sas_to_ata_dev(dev);
1162908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int ddb = (int) (unsigned long) dev->lldd_dev;
1172908d778ab3e244900c310974e1fc1c69066e450James Bottomley	u32 qdepth = 0;
1182908d778ab3e244900c310974e1fc1c69066e450James Bottomley
119aa9f8328fc51460e15da129caf622b6560fa8c99James Bottomley	if (dev->dev_type == SAS_SATA_DEV || dev->dev_type == SAS_SATA_PM_PORT) {
120b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams		if (ata_id_has_ncq(ata_dev->id))
121b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams			qdepth = ata_id_queue_depth(ata_dev->id);
1222908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_ddbsite_write_dword(asd_ha, ddb, SATA_TAG_ALLOC_MASK,
123797f49de3d95d964a360bcf0167cc20e249bb90bDarrick J. Wong					(1ULL<<qdepth)-1);
1242908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_ddbsite_write_byte(asd_ha, ddb, NUM_SATA_TAGS, qdepth);
1252908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
126b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams
127b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams	if (qdepth > 0)
128b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams		if (asd_init_sata_tag_ddb(dev) != 0) {
129b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams			unsigned long flags;
130b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams
131b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams			spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
132b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams			ata_dev->flags |= ATA_DFLAG_NCQ_OFF;
133b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams			spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
134b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams		}
135b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams}
136b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams
137b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williamsstatic int asd_init_sata(struct domain_device *dev)
138b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams{
139b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
140b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams	int ddb = (int) (unsigned long) dev->lldd_dev;
141b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams
142b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams	asd_ddbsite_write_word(asd_ha, ddb, ATA_CMD_SCBPTR, 0xFFFF);
143aa9f8328fc51460e15da129caf622b6560fa8c99James Bottomley	if (dev->dev_type == SAS_SATA_DEV || dev->dev_type == SAS_SATA_PM ||
144aa9f8328fc51460e15da129caf622b6560fa8c99James Bottomley	    dev->dev_type == SAS_SATA_PM_PORT) {
1452908d778ab3e244900c310974e1fc1c69066e450James Bottomley		struct dev_to_host_fis *fis = (struct dev_to_host_fis *)
1462908d778ab3e244900c310974e1fc1c69066e450James Bottomley			dev->frame_rcvd;
1472908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_ddbsite_write_byte(asd_ha, ddb, SATA_STATUS, fis->status);
1482908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
1492908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_word(asd_ha, ddb, NCQ_DATA_SCB_PTR, 0xFFFF);
150b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams
151b91bb296188118eea9fdc6093cfcf76bbe8589baDan Williams	return 0;
1522908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
1532908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1542908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic int asd_init_target_ddb(struct domain_device *dev)
1552908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
1562908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int ddb, i;
1572908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
1582908d778ab3e244900c310974e1fc1c69066e450James Bottomley	u8 flags = 0;
1592908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1602908d778ab3e244900c310974e1fc1c69066e450James Bottomley	ddb = asd_get_ddb(asd_ha);
1612908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (ddb < 0)
1622908d778ab3e244900c310974e1fc1c69066e450James Bottomley		return ddb;
1632908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1642908d778ab3e244900c310974e1fc1c69066e450James Bottomley	dev->lldd_dev = (void *) (unsigned long) ddb;
1652908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1662908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_byte(asd_ha, ddb, 0, DDB_TP_CONN_TYPE);
1672908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_byte(asd_ha, ddb, 1, 0);
1682908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_word(asd_ha, ddb, INIT_CONN_TAG, 0xFFFF);
1692908d778ab3e244900c310974e1fc1c69066e450James Bottomley	for (i = 0; i < SAS_ADDR_SIZE; i++)
1702908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_ddbsite_write_byte(asd_ha, ddb, DEST_SAS_ADDR+i,
1712908d778ab3e244900c310974e1fc1c69066e450James Bottomley				       dev->sas_addr[i]);
1722908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_word(asd_ha, ddb, SEND_QUEUE_HEAD, 0xFFFF);
1732908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_set_ddb_type(dev);
1742908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_byte(asd_ha, ddb, CONN_MASK, dev->port->phy_mask);
1752908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (dev->port->oob_mode != SATA_OOB_MODE) {
1762908d778ab3e244900c310974e1fc1c69066e450James Bottomley		flags |= OPEN_REQUIRED;
177aa9f8328fc51460e15da129caf622b6560fa8c99James Bottomley		if ((dev->dev_type == SAS_SATA_DEV) ||
1785929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong		    (dev->tproto & SAS_PROTOCOL_STP)) {
1792908d778ab3e244900c310974e1fc1c69066e450James Bottomley			struct smp_resp *rps_resp = &dev->sata_dev.rps_resp;
1802908d778ab3e244900c310974e1fc1c69066e450James Bottomley			if (rps_resp->frame_type == SMP_RESPONSE &&
1812908d778ab3e244900c310974e1fc1c69066e450James Bottomley			    rps_resp->function == SMP_REPORT_PHY_SATA &&
1822908d778ab3e244900c310974e1fc1c69066e450James Bottomley			    rps_resp->result == SMP_RESP_FUNC_ACC) {
1832908d778ab3e244900c310974e1fc1c69066e450James Bottomley				if (rps_resp->rps.affil_valid)
1842908d778ab3e244900c310974e1fc1c69066e450James Bottomley					flags |= STP_AFFIL_POL;
1852908d778ab3e244900c310974e1fc1c69066e450James Bottomley				if (rps_resp->rps.affil_supp)
1862908d778ab3e244900c310974e1fc1c69066e450James Bottomley					flags |= SUPPORTS_AFFIL;
1872908d778ab3e244900c310974e1fc1c69066e450James Bottomley			}
1882908d778ab3e244900c310974e1fc1c69066e450James Bottomley		} else {
1892908d778ab3e244900c310974e1fc1c69066e450James Bottomley			flags |= CONCURRENT_CONN_SUPP;
1902908d778ab3e244900c310974e1fc1c69066e450James Bottomley			if (!dev->parent &&
191aa9f8328fc51460e15da129caf622b6560fa8c99James Bottomley			    (dev->dev_type == SAS_EDGE_EXPANDER_DEVICE ||
192aa9f8328fc51460e15da129caf622b6560fa8c99James Bottomley			     dev->dev_type == SAS_FANOUT_EXPANDER_DEVICE))
1932908d778ab3e244900c310974e1fc1c69066e450James Bottomley				asd_ddbsite_write_byte(asd_ha, ddb, MAX_CCONN,
1942908d778ab3e244900c310974e1fc1c69066e450James Bottomley						       4);
1952908d778ab3e244900c310974e1fc1c69066e450James Bottomley			else
1962908d778ab3e244900c310974e1fc1c69066e450James Bottomley				asd_ddbsite_write_byte(asd_ha, ddb, MAX_CCONN,
1972908d778ab3e244900c310974e1fc1c69066e450James Bottomley						       dev->pathways);
1982908d778ab3e244900c310974e1fc1c69066e450James Bottomley			asd_ddbsite_write_byte(asd_ha, ddb, NUM_CTX, 1);
1992908d778ab3e244900c310974e1fc1c69066e450James Bottomley		}
2002908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
201aa9f8328fc51460e15da129caf622b6560fa8c99James Bottomley	if (dev->dev_type == SAS_SATA_PM)
2022908d778ab3e244900c310974e1fc1c69066e450James Bottomley		flags |= SATA_MULTIPORT;
2032908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_byte(asd_ha, ddb, DDB_TARG_FLAGS, flags);
2042908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2052908d778ab3e244900c310974e1fc1c69066e450James Bottomley	flags = 0;
2065929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong	if (dev->tproto & SAS_PROTOCOL_STP)
2072908d778ab3e244900c310974e1fc1c69066e450James Bottomley		flags |= STP_CL_POL_NO_TX;
2082908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_byte(asd_ha, ddb, DDB_TARG_FLAGS2, flags);
2092908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2102908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_word(asd_ha, ddb, EXEC_QUEUE_TAIL, 0xFFFF);
2112908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_word(asd_ha, ddb, SEND_QUEUE_TAIL, 0xFFFF);
2122908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_word(asd_ha, ddb, SISTER_DDB, 0xFFFF);
2132908d778ab3e244900c310974e1fc1c69066e450James Bottomley
214aa9f8328fc51460e15da129caf622b6560fa8c99James Bottomley	if (dev->dev_type == SAS_SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
2152908d778ab3e244900c310974e1fc1c69066e450James Bottomley		i = asd_init_sata(dev);
2162908d778ab3e244900c310974e1fc1c69066e450James Bottomley		if (i < 0) {
2172908d778ab3e244900c310974e1fc1c69066e450James Bottomley			asd_free_ddb(asd_ha, ddb);
2182908d778ab3e244900c310974e1fc1c69066e450James Bottomley			return i;
2192908d778ab3e244900c310974e1fc1c69066e450James Bottomley		}
2202908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
2212908d778ab3e244900c310974e1fc1c69066e450James Bottomley
222aa9f8328fc51460e15da129caf622b6560fa8c99James Bottomley	if (dev->dev_type == SAS_END_DEVICE) {
2232908d778ab3e244900c310974e1fc1c69066e450James Bottomley		struct sas_end_device *rdev = rphy_to_end_device(dev->rphy);
2242908d778ab3e244900c310974e1fc1c69066e450James Bottomley		if (rdev->I_T_nexus_loss_timeout > 0)
2252908d778ab3e244900c310974e1fc1c69066e450James Bottomley			asd_ddbsite_write_word(asd_ha, ddb, ITNL_TIMEOUT,
2262908d778ab3e244900c310974e1fc1c69066e450James Bottomley					       min(rdev->I_T_nexus_loss_timeout,
2272908d778ab3e244900c310974e1fc1c69066e450James Bottomley						   (u16)ITNL_TIMEOUT_CONST));
2282908d778ab3e244900c310974e1fc1c69066e450James Bottomley		else
2292908d778ab3e244900c310974e1fc1c69066e450James Bottomley			asd_ddbsite_write_word(asd_ha, ddb, ITNL_TIMEOUT,
2302908d778ab3e244900c310974e1fc1c69066e450James Bottomley					       (u16)ITNL_TIMEOUT_CONST);
2312908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
2322908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return 0;
2332908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
2342908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2352908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic int asd_init_sata_pm_table_ddb(struct domain_device *dev)
2362908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
2372908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
2382908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int ddb, i;
2392908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2402908d778ab3e244900c310974e1fc1c69066e450James Bottomley	ddb = asd_get_ddb(asd_ha);
2412908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (ddb < 0)
2422908d778ab3e244900c310974e1fc1c69066e450James Bottomley		return ddb;
2432908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2442908d778ab3e244900c310974e1fc1c69066e450James Bottomley	for (i = 0; i < 32; i += 2)
2452908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_ddbsite_write_word(asd_ha, ddb, i, 0xFFFF);
2462908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2472908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_word(asd_ha, (int) (unsigned long) dev->lldd_dev,
2482908d778ab3e244900c310974e1fc1c69066e450James Bottomley			       SISTER_DDB, ddb);
2492908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2502908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return 0;
2512908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
2522908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2532908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define PM_PORT_FLAGS offsetof(struct asd_ddb_sata_pm_port, pm_port_flags)
2542908d778ab3e244900c310974e1fc1c69066e450James Bottomley#define PARENT_DDB    offsetof(struct asd_ddb_sata_pm_port, parent_ddb)
2552908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2562908d778ab3e244900c310974e1fc1c69066e450James Bottomley/**
2572908d778ab3e244900c310974e1fc1c69066e450James Bottomley * asd_init_sata_pm_port_ddb -- SATA Port Multiplier Port
2582908d778ab3e244900c310974e1fc1c69066e450James Bottomley * dev: pointer to domain device
2592908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
2602908d778ab3e244900c310974e1fc1c69066e450James Bottomley * For SATA Port Multiplier Ports we need to allocate one SATA Port
2612908d778ab3e244900c310974e1fc1c69066e450James Bottomley * Multiplier Port DDB and depending on whether the target on it
2622908d778ab3e244900c310974e1fc1c69066e450James Bottomley * supports SATA II NCQ, one SATA Tag DDB.
2632908d778ab3e244900c310974e1fc1c69066e450James Bottomley */
2642908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic int asd_init_sata_pm_port_ddb(struct domain_device *dev)
2652908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
2662908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int ddb, i, parent_ddb, pmtable_ddb;
2672908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
2682908d778ab3e244900c310974e1fc1c69066e450James Bottomley	u8  flags;
2692908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2702908d778ab3e244900c310974e1fc1c69066e450James Bottomley	ddb = asd_get_ddb(asd_ha);
2712908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (ddb < 0)
2722908d778ab3e244900c310974e1fc1c69066e450James Bottomley		return ddb;
2732908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2742908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_set_ddb_type(dev);
2752908d778ab3e244900c310974e1fc1c69066e450James Bottomley	flags = (dev->sata_dev.port_no << 4) | PM_PORT_SET;
2762908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_byte(asd_ha, ddb, PM_PORT_FLAGS, flags);
2772908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_word(asd_ha, ddb, SISTER_DDB, 0xFFFF);
2782908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_word(asd_ha, ddb, ATA_CMD_SCBPTR, 0xFFFF);
2792908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_init_sata(dev);
2802908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2812908d778ab3e244900c310974e1fc1c69066e450James Bottomley	parent_ddb = (int) (unsigned long) dev->parent->lldd_dev;
2822908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_word(asd_ha, ddb, PARENT_DDB, parent_ddb);
2832908d778ab3e244900c310974e1fc1c69066e450James Bottomley	pmtable_ddb = asd_ddbsite_read_word(asd_ha, parent_ddb, SISTER_DDB);
2842908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ddbsite_write_word(asd_ha, pmtable_ddb, dev->sata_dev.port_no,ddb);
2852908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2862908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (asd_ddbsite_read_byte(asd_ha, ddb, NUM_SATA_TAGS) > 0) {
2872908d778ab3e244900c310974e1fc1c69066e450James Bottomley		i = asd_init_sata_tag_ddb(dev);
2882908d778ab3e244900c310974e1fc1c69066e450James Bottomley		if (i < 0) {
2892908d778ab3e244900c310974e1fc1c69066e450James Bottomley			asd_free_ddb(asd_ha, ddb);
2902908d778ab3e244900c310974e1fc1c69066e450James Bottomley			return i;
2912908d778ab3e244900c310974e1fc1c69066e450James Bottomley		}
2922908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
2932908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return 0;
2942908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
2952908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2962908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic int asd_init_initiator_ddb(struct domain_device *dev)
2972908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
2982908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return -ENODEV;
2992908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
3002908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3012908d778ab3e244900c310974e1fc1c69066e450James Bottomley/**
3022908d778ab3e244900c310974e1fc1c69066e450James Bottomley * asd_init_sata_pm_ddb -- SATA Port Multiplier
3032908d778ab3e244900c310974e1fc1c69066e450James Bottomley * dev: pointer to domain device
3042908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
3052908d778ab3e244900c310974e1fc1c69066e450James Bottomley * For STP and direct-attached SATA Port Multipliers we need
3062908d778ab3e244900c310974e1fc1c69066e450James Bottomley * one target port DDB entry and one SATA PM table DDB entry.
3072908d778ab3e244900c310974e1fc1c69066e450James Bottomley */
3082908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic int asd_init_sata_pm_ddb(struct domain_device *dev)
3092908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
3102908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int res = 0;
3112908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3122908d778ab3e244900c310974e1fc1c69066e450James Bottomley	res = asd_init_target_ddb(dev);
3132908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (res)
3142908d778ab3e244900c310974e1fc1c69066e450James Bottomley		goto out;
3152908d778ab3e244900c310974e1fc1c69066e450James Bottomley	res = asd_init_sata_pm_table_ddb(dev);
3162908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (res)
3172908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_free_ddb(dev->port->ha->lldd_ha,
3182908d778ab3e244900c310974e1fc1c69066e450James Bottomley			     (int) (unsigned long) dev->lldd_dev);
3192908d778ab3e244900c310974e1fc1c69066e450James Bottomleyout:
3202908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return res;
3212908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
3222908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3232908d778ab3e244900c310974e1fc1c69066e450James Bottomleyint asd_dev_found(struct domain_device *dev)
3242908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
32557ba07dc54b7657e69fe8ac42d83df21e415c85bDarrick J. Wong	unsigned long flags;
3262908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int res = 0;
32757ba07dc54b7657e69fe8ac42d83df21e415c85bDarrick J. Wong	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
3282908d778ab3e244900c310974e1fc1c69066e450James Bottomley
32957ba07dc54b7657e69fe8ac42d83df21e415c85bDarrick J. Wong	spin_lock_irqsave(&asd_ha->hw_prof.ddb_lock, flags);
3302908d778ab3e244900c310974e1fc1c69066e450James Bottomley	switch (dev->dev_type) {
331aa9f8328fc51460e15da129caf622b6560fa8c99James Bottomley	case SAS_SATA_PM:
3322908d778ab3e244900c310974e1fc1c69066e450James Bottomley		res = asd_init_sata_pm_ddb(dev);
3332908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
334aa9f8328fc51460e15da129caf622b6560fa8c99James Bottomley	case SAS_SATA_PM_PORT:
3352908d778ab3e244900c310974e1fc1c69066e450James Bottomley		res = asd_init_sata_pm_port_ddb(dev);
3362908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
3372908d778ab3e244900c310974e1fc1c69066e450James Bottomley	default:
3382908d778ab3e244900c310974e1fc1c69066e450James Bottomley		if (dev->tproto)
3392908d778ab3e244900c310974e1fc1c69066e450James Bottomley			res = asd_init_target_ddb(dev);
3402908d778ab3e244900c310974e1fc1c69066e450James Bottomley		else
3412908d778ab3e244900c310974e1fc1c69066e450James Bottomley			res = asd_init_initiator_ddb(dev);
3422908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
34357ba07dc54b7657e69fe8ac42d83df21e415c85bDarrick J. Wong	spin_unlock_irqrestore(&asd_ha->hw_prof.ddb_lock, flags);
34457ba07dc54b7657e69fe8ac42d83df21e415c85bDarrick J. Wong
3452908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return res;
3462908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
3472908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3482908d778ab3e244900c310974e1fc1c69066e450James Bottomleyvoid asd_dev_gone(struct domain_device *dev)
3492908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
3502908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int ddb, sister_ddb;
35157ba07dc54b7657e69fe8ac42d83df21e415c85bDarrick J. Wong	unsigned long flags;
3522908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
3532908d778ab3e244900c310974e1fc1c69066e450James Bottomley
35457ba07dc54b7657e69fe8ac42d83df21e415c85bDarrick J. Wong	spin_lock_irqsave(&asd_ha->hw_prof.ddb_lock, flags);
3552908d778ab3e244900c310974e1fc1c69066e450James Bottomley	ddb = (int) (unsigned long) dev->lldd_dev;
3562908d778ab3e244900c310974e1fc1c69066e450James Bottomley	sister_ddb = asd_ddbsite_read_word(asd_ha, ddb, SISTER_DDB);
3572908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3582908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (sister_ddb != 0xFFFF)
3592908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_free_ddb(asd_ha, sister_ddb);
3602908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_free_ddb(asd_ha, ddb);
3612908d778ab3e244900c310974e1fc1c69066e450James Bottomley	dev->lldd_dev = NULL;
36257ba07dc54b7657e69fe8ac42d83df21e415c85bDarrick J. Wong	spin_unlock_irqrestore(&asd_ha->hw_prof.ddb_lock, flags);
3632908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
364