blob: 2e2ddec9c0b6321436c05c750713898c3dca3080 [file] [log] [blame]
James Bottomley2908d772006-08-29 09:22:51 -05001/*
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
Adrian Bunk81e56de2008-03-28 14:48:34 -070038static int asd_get_ddb(struct asd_ha_struct *asd_ha)
James Bottomley2908d772006-08-29 09:22:51 -050039{
James Bottomley2908d772006-08-29 09:22:51 -050040 int ddb, i;
41
James Bottomley2908d772006-08-29 09:22:51 -050042 ddb = FIND_FREE_DDB(asd_ha);
43 if (ddb >= asd_ha->hw_prof.max_ddbs) {
44 ddb = -ENOMEM;
James Bottomley2908d772006-08-29 09:22:51 -050045 goto out;
46 }
47 SET_DDB(ddb, asd_ha);
James Bottomley2908d772006-08-29 09:22:51 -050048
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
Adrian Bunk81e56de2008-03-28 14:48:34 -070074static void asd_free_ddb(struct asd_ha_struct *asd_ha, int ddb)
James Bottomley2908d772006-08-29 09:22:51 -050075{
James Bottomley2908d772006-08-29 09:22:51 -050076 if (!ddb || ddb >= 0xFFFF)
77 return;
78 asd_ddbsite_write_byte(asd_ha, ddb, DDB_TYPE, DDB_TYPE_UNUSED);
James Bottomley2908d772006-08-29 09:22:51 -050079 CLEAR_DDB(ddb, asd_ha);
James Bottomley2908d772006-08-29 09:22:51 -050080}
81
Adrian Bunk81e56de2008-03-28 14:48:34 -070082static void asd_set_ddb_type(struct domain_device *dev)
James Bottomley2908d772006-08-29 09:22:51 -050083{
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 == 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
Adrian Bunk81e56de2008-03-28 14:48:34 -0700112static int asd_init_sata(struct domain_device *dev)
James Bottomley2908d772006-08-29 09:22:51 -0500113{
114 struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
115 int ddb = (int) (unsigned long) dev->lldd_dev;
116 u32 qdepth = 0;
117 int res = 0;
118
119 asd_ddbsite_write_word(asd_ha, ddb, ATA_CMD_SCBPTR, 0xFFFF);
120 if ((dev->dev_type == SATA_DEV || dev->dev_type == SATA_PM_PORT) &&
121 dev->sata_dev.identify_device &&
122 dev->sata_dev.identify_device[10] != 0) {
123 u16 w75 = le16_to_cpu(dev->sata_dev.identify_device[75]);
124 u16 w76 = le16_to_cpu(dev->sata_dev.identify_device[76]);
125
126 if (w76 & 0x100) /* NCQ? */
127 qdepth = (w75 & 0x1F) + 1;
128 asd_ddbsite_write_dword(asd_ha, ddb, SATA_TAG_ALLOC_MASK,
Darrick J. Wong797f49d2006-10-05 15:12:37 -0700129 (1ULL<<qdepth)-1);
James Bottomley2908d772006-08-29 09:22:51 -0500130 asd_ddbsite_write_byte(asd_ha, ddb, NUM_SATA_TAGS, qdepth);
131 }
132 if (dev->dev_type == SATA_DEV || dev->dev_type == SATA_PM ||
133 dev->dev_type == SATA_PM_PORT) {
134 struct dev_to_host_fis *fis = (struct dev_to_host_fis *)
135 dev->frame_rcvd;
136 asd_ddbsite_write_byte(asd_ha, ddb, SATA_STATUS, fis->status);
137 }
138 asd_ddbsite_write_word(asd_ha, ddb, NCQ_DATA_SCB_PTR, 0xFFFF);
139 if (qdepth > 0)
140 res = asd_init_sata_tag_ddb(dev);
141 return res;
142}
143
144static int asd_init_target_ddb(struct domain_device *dev)
145{
146 int ddb, i;
147 struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
148 u8 flags = 0;
149
150 ddb = asd_get_ddb(asd_ha);
151 if (ddb < 0)
152 return ddb;
153
154 dev->lldd_dev = (void *) (unsigned long) ddb;
155
156 asd_ddbsite_write_byte(asd_ha, ddb, 0, DDB_TP_CONN_TYPE);
157 asd_ddbsite_write_byte(asd_ha, ddb, 1, 0);
158 asd_ddbsite_write_word(asd_ha, ddb, INIT_CONN_TAG, 0xFFFF);
159 for (i = 0; i < SAS_ADDR_SIZE; i++)
160 asd_ddbsite_write_byte(asd_ha, ddb, DEST_SAS_ADDR+i,
161 dev->sas_addr[i]);
162 asd_ddbsite_write_word(asd_ha, ddb, SEND_QUEUE_HEAD, 0xFFFF);
163 asd_set_ddb_type(dev);
164 asd_ddbsite_write_byte(asd_ha, ddb, CONN_MASK, dev->port->phy_mask);
165 if (dev->port->oob_mode != SATA_OOB_MODE) {
166 flags |= OPEN_REQUIRED;
167 if ((dev->dev_type == SATA_DEV) ||
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800168 (dev->tproto & SAS_PROTOCOL_STP)) {
James Bottomley2908d772006-08-29 09:22:51 -0500169 struct smp_resp *rps_resp = &dev->sata_dev.rps_resp;
170 if (rps_resp->frame_type == SMP_RESPONSE &&
171 rps_resp->function == SMP_REPORT_PHY_SATA &&
172 rps_resp->result == SMP_RESP_FUNC_ACC) {
173 if (rps_resp->rps.affil_valid)
174 flags |= STP_AFFIL_POL;
175 if (rps_resp->rps.affil_supp)
176 flags |= SUPPORTS_AFFIL;
177 }
178 } else {
179 flags |= CONCURRENT_CONN_SUPP;
180 if (!dev->parent &&
181 (dev->dev_type == EDGE_DEV ||
182 dev->dev_type == FANOUT_DEV))
183 asd_ddbsite_write_byte(asd_ha, ddb, MAX_CCONN,
184 4);
185 else
186 asd_ddbsite_write_byte(asd_ha, ddb, MAX_CCONN,
187 dev->pathways);
188 asd_ddbsite_write_byte(asd_ha, ddb, NUM_CTX, 1);
189 }
190 }
191 if (dev->dev_type == SATA_PM)
192 flags |= SATA_MULTIPORT;
193 asd_ddbsite_write_byte(asd_ha, ddb, DDB_TARG_FLAGS, flags);
194
195 flags = 0;
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800196 if (dev->tproto & SAS_PROTOCOL_STP)
James Bottomley2908d772006-08-29 09:22:51 -0500197 flags |= STP_CL_POL_NO_TX;
198 asd_ddbsite_write_byte(asd_ha, ddb, DDB_TARG_FLAGS2, flags);
199
200 asd_ddbsite_write_word(asd_ha, ddb, EXEC_QUEUE_TAIL, 0xFFFF);
201 asd_ddbsite_write_word(asd_ha, ddb, SEND_QUEUE_TAIL, 0xFFFF);
202 asd_ddbsite_write_word(asd_ha, ddb, SISTER_DDB, 0xFFFF);
203
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800204 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
James Bottomley2908d772006-08-29 09:22:51 -0500205 i = asd_init_sata(dev);
206 if (i < 0) {
207 asd_free_ddb(asd_ha, ddb);
208 return i;
209 }
210 }
211
212 if (dev->dev_type == SAS_END_DEV) {
213 struct sas_end_device *rdev = rphy_to_end_device(dev->rphy);
214 if (rdev->I_T_nexus_loss_timeout > 0)
215 asd_ddbsite_write_word(asd_ha, ddb, ITNL_TIMEOUT,
216 min(rdev->I_T_nexus_loss_timeout,
217 (u16)ITNL_TIMEOUT_CONST));
218 else
219 asd_ddbsite_write_word(asd_ha, ddb, ITNL_TIMEOUT,
220 (u16)ITNL_TIMEOUT_CONST);
221 }
222 return 0;
223}
224
225static int asd_init_sata_pm_table_ddb(struct domain_device *dev)
226{
227 struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
228 int ddb, i;
229
230 ddb = asd_get_ddb(asd_ha);
231 if (ddb < 0)
232 return ddb;
233
234 for (i = 0; i < 32; i += 2)
235 asd_ddbsite_write_word(asd_ha, ddb, i, 0xFFFF);
236
237 asd_ddbsite_write_word(asd_ha, (int) (unsigned long) dev->lldd_dev,
238 SISTER_DDB, ddb);
239
240 return 0;
241}
242
243#define PM_PORT_FLAGS offsetof(struct asd_ddb_sata_pm_port, pm_port_flags)
244#define PARENT_DDB offsetof(struct asd_ddb_sata_pm_port, parent_ddb)
245
246/**
247 * asd_init_sata_pm_port_ddb -- SATA Port Multiplier Port
248 * dev: pointer to domain device
249 *
250 * For SATA Port Multiplier Ports we need to allocate one SATA Port
251 * Multiplier Port DDB and depending on whether the target on it
252 * supports SATA II NCQ, one SATA Tag DDB.
253 */
254static int asd_init_sata_pm_port_ddb(struct domain_device *dev)
255{
256 int ddb, i, parent_ddb, pmtable_ddb;
257 struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
258 u8 flags;
259
260 ddb = asd_get_ddb(asd_ha);
261 if (ddb < 0)
262 return ddb;
263
264 asd_set_ddb_type(dev);
265 flags = (dev->sata_dev.port_no << 4) | PM_PORT_SET;
266 asd_ddbsite_write_byte(asd_ha, ddb, PM_PORT_FLAGS, flags);
267 asd_ddbsite_write_word(asd_ha, ddb, SISTER_DDB, 0xFFFF);
268 asd_ddbsite_write_word(asd_ha, ddb, ATA_CMD_SCBPTR, 0xFFFF);
269 asd_init_sata(dev);
270
271 parent_ddb = (int) (unsigned long) dev->parent->lldd_dev;
272 asd_ddbsite_write_word(asd_ha, ddb, PARENT_DDB, parent_ddb);
273 pmtable_ddb = asd_ddbsite_read_word(asd_ha, parent_ddb, SISTER_DDB);
274 asd_ddbsite_write_word(asd_ha, pmtable_ddb, dev->sata_dev.port_no,ddb);
275
276 if (asd_ddbsite_read_byte(asd_ha, ddb, NUM_SATA_TAGS) > 0) {
277 i = asd_init_sata_tag_ddb(dev);
278 if (i < 0) {
279 asd_free_ddb(asd_ha, ddb);
280 return i;
281 }
282 }
283 return 0;
284}
285
286static int asd_init_initiator_ddb(struct domain_device *dev)
287{
288 return -ENODEV;
289}
290
291/**
292 * asd_init_sata_pm_ddb -- SATA Port Multiplier
293 * dev: pointer to domain device
294 *
295 * For STP and direct-attached SATA Port Multipliers we need
296 * one target port DDB entry and one SATA PM table DDB entry.
297 */
298static int asd_init_sata_pm_ddb(struct domain_device *dev)
299{
300 int res = 0;
301
302 res = asd_init_target_ddb(dev);
303 if (res)
304 goto out;
305 res = asd_init_sata_pm_table_ddb(dev);
306 if (res)
307 asd_free_ddb(dev->port->ha->lldd_ha,
308 (int) (unsigned long) dev->lldd_dev);
309out:
310 return res;
311}
312
313int asd_dev_found(struct domain_device *dev)
314{
Darrick J. Wong57ba07d2007-01-11 14:15:32 -0800315 unsigned long flags;
James Bottomley2908d772006-08-29 09:22:51 -0500316 int res = 0;
Darrick J. Wong57ba07d2007-01-11 14:15:32 -0800317 struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
James Bottomley2908d772006-08-29 09:22:51 -0500318
Darrick J. Wong57ba07d2007-01-11 14:15:32 -0800319 spin_lock_irqsave(&asd_ha->hw_prof.ddb_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500320 switch (dev->dev_type) {
321 case SATA_PM:
322 res = asd_init_sata_pm_ddb(dev);
323 break;
324 case SATA_PM_PORT:
325 res = asd_init_sata_pm_port_ddb(dev);
326 break;
327 default:
328 if (dev->tproto)
329 res = asd_init_target_ddb(dev);
330 else
331 res = asd_init_initiator_ddb(dev);
332 }
Darrick J. Wong57ba07d2007-01-11 14:15:32 -0800333 spin_unlock_irqrestore(&asd_ha->hw_prof.ddb_lock, flags);
334
James Bottomley2908d772006-08-29 09:22:51 -0500335 return res;
336}
337
338void asd_dev_gone(struct domain_device *dev)
339{
340 int ddb, sister_ddb;
Darrick J. Wong57ba07d2007-01-11 14:15:32 -0800341 unsigned long flags;
James Bottomley2908d772006-08-29 09:22:51 -0500342 struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
343
Darrick J. Wong57ba07d2007-01-11 14:15:32 -0800344 spin_lock_irqsave(&asd_ha->hw_prof.ddb_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500345 ddb = (int) (unsigned long) dev->lldd_dev;
346 sister_ddb = asd_ddbsite_read_word(asd_ha, ddb, SISTER_DDB);
347
348 if (sister_ddb != 0xFFFF)
349 asd_free_ddb(asd_ha, sister_ddb);
350 asd_free_ddb(asd_ha, ddb);
351 dev->lldd_dev = NULL;
Darrick J. Wong57ba07d2007-01-11 14:15:32 -0800352 spin_unlock_irqrestore(&asd_ha->hw_prof.ddb_lock, flags);
James Bottomley2908d772006-08-29 09:22:51 -0500353}