1 // SPDX-License-Identifier: GPL-2.0-only
3 * Aic94xx SAS/SATA DDB management
5 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
6 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
8 * $Id: //depot/aic94xx/aic94xx_dev.c#21 $
12 #include "aic94xx_hwi.h"
13 #include "aic94xx_reg.h"
14 #include "aic94xx_sas.h"
16 #define FIND_FREE_DDB(_ha) find_first_zero_bit((_ha)->hw_prof.ddb_bitmap, \
17 (_ha)->hw_prof.max_ddbs)
18 #define SET_DDB(_ddb, _ha) set_bit(_ddb, (_ha)->hw_prof.ddb_bitmap)
19 #define CLEAR_DDB(_ddb, _ha) clear_bit(_ddb, (_ha)->hw_prof.ddb_bitmap)
21 static int asd_get_ddb(struct asd_ha_struct
*asd_ha
)
25 ddb
= FIND_FREE_DDB(asd_ha
);
26 if (ddb
>= asd_ha
->hw_prof
.max_ddbs
) {
32 for (i
= 0; i
< sizeof(struct asd_ddb_ssp_smp_target_port
); i
+= 4)
33 asd_ddbsite_write_dword(asd_ha
, ddb
, i
, 0);
38 #define INIT_CONN_TAG offsetof(struct asd_ddb_ssp_smp_target_port, init_conn_tag)
39 #define DEST_SAS_ADDR offsetof(struct asd_ddb_ssp_smp_target_port, dest_sas_addr)
40 #define SEND_QUEUE_HEAD offsetof(struct asd_ddb_ssp_smp_target_port, send_queue_head)
41 #define DDB_TYPE offsetof(struct asd_ddb_ssp_smp_target_port, ddb_type)
42 #define CONN_MASK offsetof(struct asd_ddb_ssp_smp_target_port, conn_mask)
43 #define DDB_TARG_FLAGS offsetof(struct asd_ddb_ssp_smp_target_port, flags)
44 #define DDB_TARG_FLAGS2 offsetof(struct asd_ddb_stp_sata_target_port, flags2)
45 #define EXEC_QUEUE_TAIL offsetof(struct asd_ddb_ssp_smp_target_port, exec_queue_tail)
46 #define SEND_QUEUE_TAIL offsetof(struct asd_ddb_ssp_smp_target_port, send_queue_tail)
47 #define SISTER_DDB offsetof(struct asd_ddb_ssp_smp_target_port, sister_ddb)
48 #define MAX_CCONN offsetof(struct asd_ddb_ssp_smp_target_port, max_concurrent_conn)
49 #define NUM_CTX offsetof(struct asd_ddb_ssp_smp_target_port, num_contexts)
50 #define ATA_CMD_SCBPTR offsetof(struct asd_ddb_stp_sata_target_port, ata_cmd_scbptr)
51 #define SATA_TAG_ALLOC_MASK offsetof(struct asd_ddb_stp_sata_target_port, sata_tag_alloc_mask)
52 #define NUM_SATA_TAGS offsetof(struct asd_ddb_stp_sata_target_port, num_sata_tags)
53 #define SATA_STATUS offsetof(struct asd_ddb_stp_sata_target_port, sata_status)
54 #define NCQ_DATA_SCB_PTR offsetof(struct asd_ddb_stp_sata_target_port, ncq_data_scb_ptr)
55 #define ITNL_TIMEOUT offsetof(struct asd_ddb_ssp_smp_target_port, itnl_timeout)
57 static void asd_free_ddb(struct asd_ha_struct
*asd_ha
, int ddb
)
59 if (!ddb
|| ddb
>= 0xFFFF)
61 asd_ddbsite_write_byte(asd_ha
, ddb
, DDB_TYPE
, DDB_TYPE_UNUSED
);
62 CLEAR_DDB(ddb
, asd_ha
);
65 static void asd_set_ddb_type(struct domain_device
*dev
)
67 struct asd_ha_struct
*asd_ha
= dev
->port
->ha
->lldd_ha
;
68 int ddb
= (int) (unsigned long) dev
->lldd_dev
;
70 if (dev
->dev_type
== SAS_SATA_PM_PORT
)
71 asd_ddbsite_write_byte(asd_ha
,ddb
, DDB_TYPE
, DDB_TYPE_PM_PORT
);
73 asd_ddbsite_write_byte(asd_ha
,ddb
, DDB_TYPE
, DDB_TYPE_TARGET
);
75 asd_ddbsite_write_byte(asd_ha
,ddb
,DDB_TYPE
,DDB_TYPE_INITIATOR
);
78 static int asd_init_sata_tag_ddb(struct domain_device
*dev
)
80 struct asd_ha_struct
*asd_ha
= dev
->port
->ha
->lldd_ha
;
83 ddb
= asd_get_ddb(asd_ha
);
87 for (i
= 0; i
< sizeof(struct asd_ddb_sata_tag
); i
+= 2)
88 asd_ddbsite_write_word(asd_ha
, ddb
, i
, 0xFFFF);
90 asd_ddbsite_write_word(asd_ha
, (int) (unsigned long) dev
->lldd_dev
,
95 void asd_set_dmamode(struct domain_device
*dev
)
97 struct asd_ha_struct
*asd_ha
= dev
->port
->ha
->lldd_ha
;
98 struct ata_device
*ata_dev
= sas_to_ata_dev(dev
);
99 int ddb
= (int) (unsigned long) dev
->lldd_dev
;
102 if (dev
->dev_type
== SAS_SATA_DEV
|| dev
->dev_type
== SAS_SATA_PM_PORT
) {
103 if (ata_id_has_ncq(ata_dev
->id
))
104 qdepth
= ata_id_queue_depth(ata_dev
->id
);
105 asd_ddbsite_write_dword(asd_ha
, ddb
, SATA_TAG_ALLOC_MASK
,
107 asd_ddbsite_write_byte(asd_ha
, ddb
, NUM_SATA_TAGS
, qdepth
);
111 if (asd_init_sata_tag_ddb(dev
) != 0) {
114 spin_lock_irqsave(dev
->sata_dev
.ap
->lock
, flags
);
115 ata_dev
->flags
|= ATA_DFLAG_NCQ_OFF
;
116 spin_unlock_irqrestore(dev
->sata_dev
.ap
->lock
, flags
);
120 static int asd_init_sata(struct domain_device
*dev
)
122 struct asd_ha_struct
*asd_ha
= dev
->port
->ha
->lldd_ha
;
123 int ddb
= (int) (unsigned long) dev
->lldd_dev
;
125 asd_ddbsite_write_word(asd_ha
, ddb
, ATA_CMD_SCBPTR
, 0xFFFF);
126 if (dev
->dev_type
== SAS_SATA_DEV
|| dev
->dev_type
== SAS_SATA_PM
||
127 dev
->dev_type
== SAS_SATA_PM_PORT
) {
128 struct dev_to_host_fis
*fis
= (struct dev_to_host_fis
*)
130 asd_ddbsite_write_byte(asd_ha
, ddb
, SATA_STATUS
, fis
->status
);
132 asd_ddbsite_write_word(asd_ha
, ddb
, NCQ_DATA_SCB_PTR
, 0xFFFF);
137 static int asd_init_target_ddb(struct domain_device
*dev
)
140 struct asd_ha_struct
*asd_ha
= dev
->port
->ha
->lldd_ha
;
143 ddb
= asd_get_ddb(asd_ha
);
147 dev
->lldd_dev
= (void *) (unsigned long) ddb
;
149 asd_ddbsite_write_byte(asd_ha
, ddb
, 0, DDB_TP_CONN_TYPE
);
150 asd_ddbsite_write_byte(asd_ha
, ddb
, 1, 0);
151 asd_ddbsite_write_word(asd_ha
, ddb
, INIT_CONN_TAG
, 0xFFFF);
152 for (i
= 0; i
< SAS_ADDR_SIZE
; i
++)
153 asd_ddbsite_write_byte(asd_ha
, ddb
, DEST_SAS_ADDR
+i
,
155 asd_ddbsite_write_word(asd_ha
, ddb
, SEND_QUEUE_HEAD
, 0xFFFF);
156 asd_set_ddb_type(dev
);
157 asd_ddbsite_write_byte(asd_ha
, ddb
, CONN_MASK
, dev
->port
->phy_mask
);
158 if (dev
->port
->oob_mode
!= SATA_OOB_MODE
) {
159 flags
|= OPEN_REQUIRED
;
160 if ((dev
->dev_type
== SAS_SATA_DEV
) ||
161 (dev
->tproto
& SAS_PROTOCOL_STP
)) {
162 struct smp_resp
*rps_resp
= &dev
->sata_dev
.rps_resp
;
163 if (rps_resp
->frame_type
== SMP_RESPONSE
&&
164 rps_resp
->function
== SMP_REPORT_PHY_SATA
&&
165 rps_resp
->result
== SMP_RESP_FUNC_ACC
) {
166 if (rps_resp
->rps
.affil_valid
)
167 flags
|= STP_AFFIL_POL
;
168 if (rps_resp
->rps
.affil_supp
)
169 flags
|= SUPPORTS_AFFIL
;
172 flags
|= CONCURRENT_CONN_SUPP
;
173 if (!dev
->parent
&& dev_is_expander(dev
->dev_type
))
174 asd_ddbsite_write_byte(asd_ha
, ddb
, MAX_CCONN
,
177 asd_ddbsite_write_byte(asd_ha
, ddb
, MAX_CCONN
,
179 asd_ddbsite_write_byte(asd_ha
, ddb
, NUM_CTX
, 1);
182 if (dev
->dev_type
== SAS_SATA_PM
)
183 flags
|= SATA_MULTIPORT
;
184 asd_ddbsite_write_byte(asd_ha
, ddb
, DDB_TARG_FLAGS
, flags
);
187 if (dev
->tproto
& SAS_PROTOCOL_STP
)
188 flags
|= STP_CL_POL_NO_TX
;
189 asd_ddbsite_write_byte(asd_ha
, ddb
, DDB_TARG_FLAGS2
, flags
);
191 asd_ddbsite_write_word(asd_ha
, ddb
, EXEC_QUEUE_TAIL
, 0xFFFF);
192 asd_ddbsite_write_word(asd_ha
, ddb
, SEND_QUEUE_TAIL
, 0xFFFF);
193 asd_ddbsite_write_word(asd_ha
, ddb
, SISTER_DDB
, 0xFFFF);
195 if (dev
->dev_type
== SAS_SATA_DEV
|| (dev
->tproto
& SAS_PROTOCOL_STP
)) {
196 i
= asd_init_sata(dev
);
198 asd_free_ddb(asd_ha
, ddb
);
203 if (dev
->dev_type
== SAS_END_DEVICE
) {
204 struct sas_end_device
*rdev
= rphy_to_end_device(dev
->rphy
);
205 if (rdev
->I_T_nexus_loss_timeout
> 0)
206 asd_ddbsite_write_word(asd_ha
, ddb
, ITNL_TIMEOUT
,
207 min(rdev
->I_T_nexus_loss_timeout
,
208 (u16
)ITNL_TIMEOUT_CONST
));
210 asd_ddbsite_write_word(asd_ha
, ddb
, ITNL_TIMEOUT
,
211 (u16
)ITNL_TIMEOUT_CONST
);
216 static int asd_init_sata_pm_table_ddb(struct domain_device
*dev
)
218 struct asd_ha_struct
*asd_ha
= dev
->port
->ha
->lldd_ha
;
221 ddb
= asd_get_ddb(asd_ha
);
225 for (i
= 0; i
< 32; i
+= 2)
226 asd_ddbsite_write_word(asd_ha
, ddb
, i
, 0xFFFF);
228 asd_ddbsite_write_word(asd_ha
, (int) (unsigned long) dev
->lldd_dev
,
234 #define PM_PORT_FLAGS offsetof(struct asd_ddb_sata_pm_port, pm_port_flags)
235 #define PARENT_DDB offsetof(struct asd_ddb_sata_pm_port, parent_ddb)
238 * asd_init_sata_pm_port_ddb -- SATA Port Multiplier Port
239 * dev: pointer to domain device
241 * For SATA Port Multiplier Ports we need to allocate one SATA Port
242 * Multiplier Port DDB and depending on whether the target on it
243 * supports SATA II NCQ, one SATA Tag DDB.
245 static int asd_init_sata_pm_port_ddb(struct domain_device
*dev
)
247 int ddb
, i
, parent_ddb
, pmtable_ddb
;
248 struct asd_ha_struct
*asd_ha
= dev
->port
->ha
->lldd_ha
;
251 ddb
= asd_get_ddb(asd_ha
);
255 asd_set_ddb_type(dev
);
256 flags
= (dev
->sata_dev
.port_no
<< 4) | PM_PORT_SET
;
257 asd_ddbsite_write_byte(asd_ha
, ddb
, PM_PORT_FLAGS
, flags
);
258 asd_ddbsite_write_word(asd_ha
, ddb
, SISTER_DDB
, 0xFFFF);
259 asd_ddbsite_write_word(asd_ha
, ddb
, ATA_CMD_SCBPTR
, 0xFFFF);
262 parent_ddb
= (int) (unsigned long) dev
->parent
->lldd_dev
;
263 asd_ddbsite_write_word(asd_ha
, ddb
, PARENT_DDB
, parent_ddb
);
264 pmtable_ddb
= asd_ddbsite_read_word(asd_ha
, parent_ddb
, SISTER_DDB
);
265 asd_ddbsite_write_word(asd_ha
, pmtable_ddb
, dev
->sata_dev
.port_no
,ddb
);
267 if (asd_ddbsite_read_byte(asd_ha
, ddb
, NUM_SATA_TAGS
) > 0) {
268 i
= asd_init_sata_tag_ddb(dev
);
270 asd_free_ddb(asd_ha
, ddb
);
277 static int asd_init_initiator_ddb(struct domain_device
*dev
)
283 * asd_init_sata_pm_ddb -- SATA Port Multiplier
284 * dev: pointer to domain device
286 * For STP and direct-attached SATA Port Multipliers we need
287 * one target port DDB entry and one SATA PM table DDB entry.
289 static int asd_init_sata_pm_ddb(struct domain_device
*dev
)
293 res
= asd_init_target_ddb(dev
);
296 res
= asd_init_sata_pm_table_ddb(dev
);
298 asd_free_ddb(dev
->port
->ha
->lldd_ha
,
299 (int) (unsigned long) dev
->lldd_dev
);
304 int asd_dev_found(struct domain_device
*dev
)
308 struct asd_ha_struct
*asd_ha
= dev
->port
->ha
->lldd_ha
;
310 spin_lock_irqsave(&asd_ha
->hw_prof
.ddb_lock
, flags
);
311 switch (dev
->dev_type
) {
313 res
= asd_init_sata_pm_ddb(dev
);
315 case SAS_SATA_PM_PORT
:
316 res
= asd_init_sata_pm_port_ddb(dev
);
320 res
= asd_init_target_ddb(dev
);
322 res
= asd_init_initiator_ddb(dev
);
324 spin_unlock_irqrestore(&asd_ha
->hw_prof
.ddb_lock
, flags
);
329 void asd_dev_gone(struct domain_device
*dev
)
333 struct asd_ha_struct
*asd_ha
= dev
->port
->ha
->lldd_ha
;
335 spin_lock_irqsave(&asd_ha
->hw_prof
.ddb_lock
, flags
);
336 ddb
= (int) (unsigned long) dev
->lldd_dev
;
337 sister_ddb
= asd_ddbsite_read_word(asd_ha
, ddb
, SISTER_DDB
);
339 if (sister_ddb
!= 0xFFFF)
340 asd_free_ddb(asd_ha
, sister_ddb
);
341 asd_free_ddb(asd_ha
, ddb
);
342 dev
->lldd_dev
= NULL
;
343 spin_unlock_irqrestore(&asd_ha
->hw_prof
.ddb_lock
, flags
);