1/*
2 * Aic94xx SAS/SATA DDB management
3 *
4 * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This file is part of the aic94xx driver.
10 *
11 * The aic94xx driver is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; version 2 of the
14 * License.
15 *
16 * The aic94xx driver is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with the aic94xx driver; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24 *
25 * $Id: //depot/aic94xx/aic94xx_dev.c#21 $
26 */
27
28#include "aic94xx.h"
29#include "aic94xx_hwi.h"
30#include "aic94xx_reg.h"
31#include "aic94xx_sas.h"
32
33#define FIND_FREE_DDB(_ha) find_first_zero_bit((_ha)->hw_prof.ddb_bitmap, \
34					       (_ha)->hw_prof.max_ddbs)
35#define SET_DDB(_ddb, _ha) set_bit(_ddb, (_ha)->hw_prof.ddb_bitmap)
36#define CLEAR_DDB(_ddb, _ha) clear_bit(_ddb, (_ha)->hw_prof.ddb_bitmap)
37
38static int asd_get_ddb(struct asd_ha_struct *asd_ha)
39{
40	int ddb, i;
41
42	ddb = FIND_FREE_DDB(asd_ha);
43	if (ddb >= asd_ha->hw_prof.max_ddbs) {
44		ddb = -ENOMEM;
45		goto out;
46	}
47	SET_DDB(ddb, asd_ha);
48
49	for (i = 0; i < sizeof(struct asd_ddb_ssp_smp_target_port); i+= 4)
50		asd_ddbsite_write_dword(asd_ha, ddb, i, 0);
51out:
52	return ddb;
53}
54
55#define INIT_CONN_TAG   offsetof(struct asd_ddb_ssp_smp_target_port, init_conn_tag)
56#define DEST_SAS_ADDR   offsetof(struct asd_ddb_ssp_smp_target_port, dest_sas_addr)
57#define SEND_QUEUE_HEAD offsetof(struct asd_ddb_ssp_smp_target_port, send_queue_head)
58#define DDB_TYPE        offsetof(struct asd_ddb_ssp_smp_target_port, ddb_type)
59#define CONN_MASK       offsetof(struct asd_ddb_ssp_smp_target_port, conn_mask)
60#define DDB_TARG_FLAGS  offsetof(struct asd_ddb_ssp_smp_target_port, flags)
61#define DDB_TARG_FLAGS2 offsetof(struct asd_ddb_stp_sata_target_port, flags2)
62#define EXEC_QUEUE_TAIL offsetof(struct asd_ddb_ssp_smp_target_port, exec_queue_tail)
63#define SEND_QUEUE_TAIL offsetof(struct asd_ddb_ssp_smp_target_port, send_queue_tail)
64#define SISTER_DDB      offsetof(struct asd_ddb_ssp_smp_target_port, sister_ddb)
65#define MAX_CCONN       offsetof(struct asd_ddb_ssp_smp_target_port, max_concurrent_conn)
66#define NUM_CTX         offsetof(struct asd_ddb_ssp_smp_target_port, num_contexts)
67#define ATA_CMD_SCBPTR  offsetof(struct asd_ddb_stp_sata_target_port, ata_cmd_scbptr)
68#define SATA_TAG_ALLOC_MASK offsetof(struct asd_ddb_stp_sata_target_port, sata_tag_alloc_mask)
69#define NUM_SATA_TAGS   offsetof(struct asd_ddb_stp_sata_target_port, num_sata_tags)
70#define SATA_STATUS     offsetof(struct asd_ddb_stp_sata_target_port, sata_status)
71#define NCQ_DATA_SCB_PTR offsetof(struct asd_ddb_stp_sata_target_port, ncq_data_scb_ptr)
72#define ITNL_TIMEOUT    offsetof(struct asd_ddb_ssp_smp_target_port, itnl_timeout)
73
74static void asd_free_ddb(struct asd_ha_struct *asd_ha, int ddb)
75{
76	if (!ddb || ddb >= 0xFFFF)
77		return;
78	asd_ddbsite_write_byte(asd_ha, ddb, DDB_TYPE, DDB_TYPE_UNUSED);
79	CLEAR_DDB(ddb, asd_ha);
80}
81
82static void asd_set_ddb_type(struct domain_device *dev)
83{
84	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
85	int ddb = (int) (unsigned long) dev->lldd_dev;
86
87	if (dev->dev_type == SAS_SATA_PM_PORT)
88		asd_ddbsite_write_byte(asd_ha,ddb, DDB_TYPE, DDB_TYPE_PM_PORT);
89	else if (dev->tproto)
90		asd_ddbsite_write_byte(asd_ha,ddb, DDB_TYPE, DDB_TYPE_TARGET);
91	else
92		asd_ddbsite_write_byte(asd_ha,ddb,DDB_TYPE,DDB_TYPE_INITIATOR);
93}
94
95static int asd_init_sata_tag_ddb(struct domain_device *dev)
96{
97	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
98	int ddb, i;
99
100	ddb = asd_get_ddb(asd_ha);
101	if (ddb < 0)
102		return ddb;
103
104	for (i = 0; i < sizeof(struct asd_ddb_sata_tag); i += 2)
105		asd_ddbsite_write_word(asd_ha, ddb, i, 0xFFFF);
106
107	asd_ddbsite_write_word(asd_ha, (int) (unsigned long) dev->lldd_dev,
108			       SISTER_DDB, ddb);
109	return 0;
110}
111
112void asd_set_dmamode(struct domain_device *dev)
113{
114	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
115	struct ata_device *ata_dev = sas_to_ata_dev(dev);
116	int ddb = (int) (unsigned long) dev->lldd_dev;
117	u32 qdepth = 0;
118
119	if (dev->dev_type == SAS_SATA_DEV || dev->dev_type == SAS_SATA_PM_PORT) {
120		if (ata_id_has_ncq(ata_dev->id))
121			qdepth = ata_id_queue_depth(ata_dev->id);
122		asd_ddbsite_write_dword(asd_ha, ddb, SATA_TAG_ALLOC_MASK,
123					(1ULL<<qdepth)-1);
124		asd_ddbsite_write_byte(asd_ha, ddb, NUM_SATA_TAGS, qdepth);
125	}
126
127	if (qdepth > 0)
128		if (asd_init_sata_tag_ddb(dev) != 0) {
129			unsigned long flags;
130
131			spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
132			ata_dev->flags |= ATA_DFLAG_NCQ_OFF;
133			spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
134		}
135}
136
137static int asd_init_sata(struct domain_device *dev)
138{
139	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
140	int ddb = (int) (unsigned long) dev->lldd_dev;
141
142	asd_ddbsite_write_word(asd_ha, ddb, ATA_CMD_SCBPTR, 0xFFFF);
143	if (dev->dev_type == SAS_SATA_DEV || dev->dev_type == SAS_SATA_PM ||
144	    dev->dev_type == SAS_SATA_PM_PORT) {
145		struct dev_to_host_fis *fis = (struct dev_to_host_fis *)
146			dev->frame_rcvd;
147		asd_ddbsite_write_byte(asd_ha, ddb, SATA_STATUS, fis->status);
148	}
149	asd_ddbsite_write_word(asd_ha, ddb, NCQ_DATA_SCB_PTR, 0xFFFF);
150
151	return 0;
152}
153
154static int asd_init_target_ddb(struct domain_device *dev)
155{
156	int ddb, i;
157	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
158	u8 flags = 0;
159
160	ddb = asd_get_ddb(asd_ha);
161	if (ddb < 0)
162		return ddb;
163
164	dev->lldd_dev = (void *) (unsigned long) ddb;
165
166	asd_ddbsite_write_byte(asd_ha, ddb, 0, DDB_TP_CONN_TYPE);
167	asd_ddbsite_write_byte(asd_ha, ddb, 1, 0);
168	asd_ddbsite_write_word(asd_ha, ddb, INIT_CONN_TAG, 0xFFFF);
169	for (i = 0; i < SAS_ADDR_SIZE; i++)
170		asd_ddbsite_write_byte(asd_ha, ddb, DEST_SAS_ADDR+i,
171				       dev->sas_addr[i]);
172	asd_ddbsite_write_word(asd_ha, ddb, SEND_QUEUE_HEAD, 0xFFFF);
173	asd_set_ddb_type(dev);
174	asd_ddbsite_write_byte(asd_ha, ddb, CONN_MASK, dev->port->phy_mask);
175	if (dev->port->oob_mode != SATA_OOB_MODE) {
176		flags |= OPEN_REQUIRED;
177		if ((dev->dev_type == SAS_SATA_DEV) ||
178		    (dev->tproto & SAS_PROTOCOL_STP)) {
179			struct smp_resp *rps_resp = &dev->sata_dev.rps_resp;
180			if (rps_resp->frame_type == SMP_RESPONSE &&
181			    rps_resp->function == SMP_REPORT_PHY_SATA &&
182			    rps_resp->result == SMP_RESP_FUNC_ACC) {
183				if (rps_resp->rps.affil_valid)
184					flags |= STP_AFFIL_POL;
185				if (rps_resp->rps.affil_supp)
186					flags |= SUPPORTS_AFFIL;
187			}
188		} else {
189			flags |= CONCURRENT_CONN_SUPP;
190			if (!dev->parent &&
191			    (dev->dev_type == SAS_EDGE_EXPANDER_DEVICE ||
192			     dev->dev_type == SAS_FANOUT_EXPANDER_DEVICE))
193				asd_ddbsite_write_byte(asd_ha, ddb, MAX_CCONN,
194						       4);
195			else
196				asd_ddbsite_write_byte(asd_ha, ddb, MAX_CCONN,
197						       dev->pathways);
198			asd_ddbsite_write_byte(asd_ha, ddb, NUM_CTX, 1);
199		}
200	}
201	if (dev->dev_type == SAS_SATA_PM)
202		flags |= SATA_MULTIPORT;
203	asd_ddbsite_write_byte(asd_ha, ddb, DDB_TARG_FLAGS, flags);
204
205	flags = 0;
206	if (dev->tproto & SAS_PROTOCOL_STP)
207		flags |= STP_CL_POL_NO_TX;
208	asd_ddbsite_write_byte(asd_ha, ddb, DDB_TARG_FLAGS2, flags);
209
210	asd_ddbsite_write_word(asd_ha, ddb, EXEC_QUEUE_TAIL, 0xFFFF);
211	asd_ddbsite_write_word(asd_ha, ddb, SEND_QUEUE_TAIL, 0xFFFF);
212	asd_ddbsite_write_word(asd_ha, ddb, SISTER_DDB, 0xFFFF);
213
214	if (dev->dev_type == SAS_SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
215		i = asd_init_sata(dev);
216		if (i < 0) {
217			asd_free_ddb(asd_ha, ddb);
218			return i;
219		}
220	}
221
222	if (dev->dev_type == SAS_END_DEVICE) {
223		struct sas_end_device *rdev = rphy_to_end_device(dev->rphy);
224		if (rdev->I_T_nexus_loss_timeout > 0)
225			asd_ddbsite_write_word(asd_ha, ddb, ITNL_TIMEOUT,
226					       min(rdev->I_T_nexus_loss_timeout,
227						   (u16)ITNL_TIMEOUT_CONST));
228		else
229			asd_ddbsite_write_word(asd_ha, ddb, ITNL_TIMEOUT,
230					       (u16)ITNL_TIMEOUT_CONST);
231	}
232	return 0;
233}
234
235static int asd_init_sata_pm_table_ddb(struct domain_device *dev)
236{
237	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
238	int ddb, i;
239
240	ddb = asd_get_ddb(asd_ha);
241	if (ddb < 0)
242		return ddb;
243
244	for (i = 0; i < 32; i += 2)
245		asd_ddbsite_write_word(asd_ha, ddb, i, 0xFFFF);
246
247	asd_ddbsite_write_word(asd_ha, (int) (unsigned long) dev->lldd_dev,
248			       SISTER_DDB, ddb);
249
250	return 0;
251}
252
253#define PM_PORT_FLAGS offsetof(struct asd_ddb_sata_pm_port, pm_port_flags)
254#define PARENT_DDB    offsetof(struct asd_ddb_sata_pm_port, parent_ddb)
255
256/**
257 * asd_init_sata_pm_port_ddb -- SATA Port Multiplier Port
258 * dev: pointer to domain device
259 *
260 * For SATA Port Multiplier Ports we need to allocate one SATA Port
261 * Multiplier Port DDB and depending on whether the target on it
262 * supports SATA II NCQ, one SATA Tag DDB.
263 */
264static int asd_init_sata_pm_port_ddb(struct domain_device *dev)
265{
266	int ddb, i, parent_ddb, pmtable_ddb;
267	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
268	u8  flags;
269
270	ddb = asd_get_ddb(asd_ha);
271	if (ddb < 0)
272		return ddb;
273
274	asd_set_ddb_type(dev);
275	flags = (dev->sata_dev.port_no << 4) | PM_PORT_SET;
276	asd_ddbsite_write_byte(asd_ha, ddb, PM_PORT_FLAGS, flags);
277	asd_ddbsite_write_word(asd_ha, ddb, SISTER_DDB, 0xFFFF);
278	asd_ddbsite_write_word(asd_ha, ddb, ATA_CMD_SCBPTR, 0xFFFF);
279	asd_init_sata(dev);
280
281	parent_ddb = (int) (unsigned long) dev->parent->lldd_dev;
282	asd_ddbsite_write_word(asd_ha, ddb, PARENT_DDB, parent_ddb);
283	pmtable_ddb = asd_ddbsite_read_word(asd_ha, parent_ddb, SISTER_DDB);
284	asd_ddbsite_write_word(asd_ha, pmtable_ddb, dev->sata_dev.port_no,ddb);
285
286	if (asd_ddbsite_read_byte(asd_ha, ddb, NUM_SATA_TAGS) > 0) {
287		i = asd_init_sata_tag_ddb(dev);
288		if (i < 0) {
289			asd_free_ddb(asd_ha, ddb);
290			return i;
291		}
292	}
293	return 0;
294}
295
296static int asd_init_initiator_ddb(struct domain_device *dev)
297{
298	return -ENODEV;
299}
300
301/**
302 * asd_init_sata_pm_ddb -- SATA Port Multiplier
303 * dev: pointer to domain device
304 *
305 * For STP and direct-attached SATA Port Multipliers we need
306 * one target port DDB entry and one SATA PM table DDB entry.
307 */
308static int asd_init_sata_pm_ddb(struct domain_device *dev)
309{
310	int res = 0;
311
312	res = asd_init_target_ddb(dev);
313	if (res)
314		goto out;
315	res = asd_init_sata_pm_table_ddb(dev);
316	if (res)
317		asd_free_ddb(dev->port->ha->lldd_ha,
318			     (int) (unsigned long) dev->lldd_dev);
319out:
320	return res;
321}
322
323int asd_dev_found(struct domain_device *dev)
324{
325	unsigned long flags;
326	int res = 0;
327	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
328
329	spin_lock_irqsave(&asd_ha->hw_prof.ddb_lock, flags);
330	switch (dev->dev_type) {
331	case SAS_SATA_PM:
332		res = asd_init_sata_pm_ddb(dev);
333		break;
334	case SAS_SATA_PM_PORT:
335		res = asd_init_sata_pm_port_ddb(dev);
336		break;
337	default:
338		if (dev->tproto)
339			res = asd_init_target_ddb(dev);
340		else
341			res = asd_init_initiator_ddb(dev);
342	}
343	spin_unlock_irqrestore(&asd_ha->hw_prof.ddb_lock, flags);
344
345	return res;
346}
347
348void asd_dev_gone(struct domain_device *dev)
349{
350	int ddb, sister_ddb;
351	unsigned long flags;
352	struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
353
354	spin_lock_irqsave(&asd_ha->hw_prof.ddb_lock, flags);
355	ddb = (int) (unsigned long) dev->lldd_dev;
356	sister_ddb = asd_ddbsite_read_word(asd_ha, ddb, SISTER_DDB);
357
358	if (sister_ddb != 0xFFFF)
359		asd_free_ddb(asd_ha, sister_ddb);
360	asd_free_ddb(asd_ha, ddb);
361	dev->lldd_dev = NULL;
362	spin_unlock_irqrestore(&asd_ha->hw_prof.ddb_lock, flags);
363}
364