1f11c7f63SJim Harris /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3718cf2ccSPedro F. Giffuni  *
4f11c7f63SJim Harris  * This file is provided under a dual BSD/GPLv2 license.  When using or
5f11c7f63SJim Harris  * redistributing this file, you may do so under either license.
6f11c7f63SJim Harris  *
7f11c7f63SJim Harris  * GPL LICENSE SUMMARY
8f11c7f63SJim Harris  *
9f11c7f63SJim Harris  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
10f11c7f63SJim Harris  *
11f11c7f63SJim Harris  * This program is free software; you can redistribute it and/or modify
12f11c7f63SJim Harris  * it under the terms of version 2 of the GNU General Public License as
13f11c7f63SJim Harris  * published by the Free Software Foundation.
14f11c7f63SJim Harris  *
15f11c7f63SJim Harris  * This program is distributed in the hope that it will be useful, but
16f11c7f63SJim Harris  * WITHOUT ANY WARRANTY; without even the implied warranty of
17f11c7f63SJim Harris  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18f11c7f63SJim Harris  * General Public License for more details.
19f11c7f63SJim Harris  *
20f11c7f63SJim Harris  * You should have received a copy of the GNU General Public License
21f11c7f63SJim Harris  * along with this program; if not, write to the Free Software
22f11c7f63SJim Harris  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23f11c7f63SJim Harris  * The full GNU General Public License is included in this distribution
24f11c7f63SJim Harris  * in the file called LICENSE.GPL.
25f11c7f63SJim Harris  *
26f11c7f63SJim Harris  * BSD LICENSE
27f11c7f63SJim Harris  *
28f11c7f63SJim Harris  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29f11c7f63SJim Harris  * All rights reserved.
30f11c7f63SJim Harris  *
31f11c7f63SJim Harris  * Redistribution and use in source and binary forms, with or without
32f11c7f63SJim Harris  * modification, are permitted provided that the following conditions
33f11c7f63SJim Harris  * are met:
34f11c7f63SJim Harris  *
35f11c7f63SJim Harris  *   * Redistributions of source code must retain the above copyright
36f11c7f63SJim Harris  *     notice, this list of conditions and the following disclaimer.
37f11c7f63SJim Harris  *   * Redistributions in binary form must reproduce the above copyright
38f11c7f63SJim Harris  *     notice, this list of conditions and the following disclaimer in
39f11c7f63SJim Harris  *     the documentation and/or other materials provided with the
40f11c7f63SJim Harris  *     distribution.
41f11c7f63SJim Harris  *
42f11c7f63SJim Harris  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43f11c7f63SJim Harris  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44f11c7f63SJim Harris  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45f11c7f63SJim Harris  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46f11c7f63SJim Harris  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47f11c7f63SJim Harris  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48f11c7f63SJim Harris  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49f11c7f63SJim Harris  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50f11c7f63SJim Harris  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51f11c7f63SJim Harris  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52f11c7f63SJim Harris  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53f11c7f63SJim Harris  */
54f11c7f63SJim Harris 
55f11c7f63SJim Harris #include <sys/cdefs.h>
56f11c7f63SJim Harris /**
57f11c7f63SJim Harris  * @file
58f11c7f63SJim Harris  *
59f11c7f63SJim Harris  * @brief This file contains the protected interface structures, constants,
60f11c7f63SJim Harris  *        and methods for the SCIF_SAS_STP_REMOTE_DEVICE object.
61f11c7f63SJim Harris  */
62f11c7f63SJim Harris 
63f11c7f63SJim Harris #include <dev/isci/scil/scif_sas_stp_remote_device.h>
64f11c7f63SJim Harris #include <dev/isci/scil/scif_sas_remote_device.h>
65f11c7f63SJim Harris #include <dev/isci/scil/scif_sas_domain.h>
66f11c7f63SJim Harris #include <dev/isci/scil/scif_sas_controller.h>
67f11c7f63SJim Harris #include <dev/isci/scil/scif_sas_logger.h>
68f11c7f63SJim Harris #include <dev/isci/scil/intel_sat.h>
69f11c7f63SJim Harris 
70f11c7f63SJim Harris /**
71f11c7f63SJim Harris  * @brief This method performs SATA/STP specific construction of the
72f11c7f63SJim Harris  *        STP remote device object.
73f11c7f63SJim Harris  *
74f11c7f63SJim Harris  * @param[in] device This parameter specifies the STP remote device
75f11c7f63SJim Harris  *            object to be constructed.
76f11c7f63SJim Harris  *
77f11c7f63SJim Harris  * @return none
78f11c7f63SJim Harris  */
scif_sas_stp_remote_device_construct(SCIF_SAS_REMOTE_DEVICE_T * device)79f11c7f63SJim Harris void scif_sas_stp_remote_device_construct(
80f11c7f63SJim Harris    SCIF_SAS_REMOTE_DEVICE_T * device
81f11c7f63SJim Harris )
82f11c7f63SJim Harris {
83f11c7f63SJim Harris    sati_device_construct(
84f11c7f63SJim Harris       &device->protocol_device.stp_device.sati_device,
85f11c7f63SJim Harris       device->domain->controller->user_parameters.sas.is_sata_ncq_enabled,
86f11c7f63SJim Harris       (U8) device->domain->controller->user_parameters.sas.max_ncq_depth,
87f11c7f63SJim Harris       device->domain->controller->user_parameters.sas.ignore_fua
88f11c7f63SJim Harris    );
89f11c7f63SJim Harris 
90f11c7f63SJim Harris    device->protocol_device.stp_device.s_active = 0;
91f11c7f63SJim Harris }
92f11c7f63SJim Harris 
93f11c7f63SJim Harris /**
94f11c7f63SJim Harris  * @brief This method attempts to allocate a valid NCQ tag from the list
95f11c7f63SJim Harris  *        of available tags in the remote device.
96f11c7f63SJim Harris  *
97f11c7f63SJim Harris  * @todo Attempt to find a CLZ like instruction to optimize this routine
98f11c7f63SJim Harris  *       down into a few instructions.  I know there is one like it for IA.
99f11c7f63SJim Harris  *
100f11c7f63SJim Harris  * @param[in] fw_device This parameter specifies the remote device
101f11c7f63SJim Harris  *            for which to allocate an available NCQ tag.
102f11c7f63SJim Harris  *
103f11c7f63SJim Harris  * @return Return an available NCQ tag.
104f11c7f63SJim Harris  * @retval 0-31 These values indicate an available tag was successfully
105f11c7f63SJim Harris  *         allocated.
106f11c7f63SJim Harris  * @return SCIF_SAS_STP_INVALID_NCQ_TAG This value indicates that there are
107f11c7f63SJim Harris  *         no available NCQ tags.
108f11c7f63SJim Harris  */
scif_sas_stp_remote_device_allocate_ncq_tag(SCIF_SAS_REMOTE_DEVICE_T * fw_device)109f11c7f63SJim Harris U8 scif_sas_stp_remote_device_allocate_ncq_tag(
110f11c7f63SJim Harris    SCIF_SAS_REMOTE_DEVICE_T * fw_device
111f11c7f63SJim Harris )
112f11c7f63SJim Harris {
113f11c7f63SJim Harris    U8  ncq_tag  = 0;
114f11c7f63SJim Harris    U32 tag_mask = 1;
115f11c7f63SJim Harris 
116f11c7f63SJim Harris    SCIF_LOG_TRACE((
117f11c7f63SJim Harris       sci_base_object_get_logger(fw_device),
118f11c7f63SJim Harris       SCIF_LOG_OBJECT_REMOTE_DEVICE | SCIF_LOG_OBJECT_IO_REQUEST,
119f11c7f63SJim Harris       "scif_sas_stp_remote_device_allocate_ncq_tag(0x%x)\n",
120f11c7f63SJim Harris       fw_device
121f11c7f63SJim Harris    ));
122f11c7f63SJim Harris 
123f11c7f63SJim Harris    // Try to find an unused NCQ tag.
124f11c7f63SJim Harris    while (  (fw_device->protocol_device.stp_device.s_active & tag_mask)
125f11c7f63SJim Harris          && (ncq_tag < fw_device->protocol_device.stp_device.sati_device.ncq_depth) )
126f11c7f63SJim Harris    {
127f11c7f63SJim Harris       tag_mask <<= 1;
128f11c7f63SJim Harris       ncq_tag++;
129f11c7f63SJim Harris    }
130f11c7f63SJim Harris 
131f11c7f63SJim Harris    // Check to see if we were able to find an available NCQ tag.
132f11c7f63SJim Harris    if (ncq_tag < fw_device->protocol_device.stp_device.sati_device.ncq_depth)
133f11c7f63SJim Harris    {
134f11c7f63SJim Harris       SCIF_LOG_INFO((
135f11c7f63SJim Harris          sci_base_object_get_logger(fw_device),
136f11c7f63SJim Harris          SCIF_LOG_OBJECT_REMOTE_DEVICE | SCIF_LOG_OBJECT_IO_REQUEST,
137f11c7f63SJim Harris          "RemoteDevice:0x%x NcqTag:0x%x successful NCQ TAG allocation\n",
138f11c7f63SJim Harris          fw_device, ncq_tag
139f11c7f63SJim Harris       ));
140f11c7f63SJim Harris 
141f11c7f63SJim Harris       fw_device->protocol_device.stp_device.s_active |= tag_mask;
142f11c7f63SJim Harris       return ncq_tag;
143f11c7f63SJim Harris    }
144f11c7f63SJim Harris 
145f11c7f63SJim Harris    SCIF_LOG_INFO((
146f11c7f63SJim Harris       sci_base_object_get_logger(fw_device),
147f11c7f63SJim Harris       SCIF_LOG_OBJECT_REMOTE_DEVICE | SCIF_LOG_OBJECT_IO_REQUEST,
148f11c7f63SJim Harris       "RemoteDevice:0x%x unable to allocate NCQ TAG\n",
149f11c7f63SJim Harris       fw_device
150f11c7f63SJim Harris    ));
151f11c7f63SJim Harris 
152f11c7f63SJim Harris    // All NCQ tags are in use.
153f11c7f63SJim Harris    return SCIF_SAS_INVALID_NCQ_TAG;
154f11c7f63SJim Harris }
155f11c7f63SJim Harris 
156f11c7f63SJim Harris /**
157f11c7f63SJim Harris  * @brief This method removes the specified tag from the list of
158f11c7f63SJim Harris  *        outstanding tags.  It doesn't return any values.
159f11c7f63SJim Harris  *
160f11c7f63SJim Harris  * @param[in] fw_device This parameter specifies the remote device for
161f11c7f63SJim Harris  *            which to free an NCQ tag.
162f11c7f63SJim Harris  * @param[in] ncq_tag This parameter specifies the NCQ tag that is
163f11c7f63SJim Harris  *            to be freed.
164f11c7f63SJim Harris  *
165f11c7f63SJim Harris  * @return none
166f11c7f63SJim Harris  */
scif_sas_stp_remote_device_free_ncq_tag(struct SCIF_SAS_REMOTE_DEVICE * fw_device,U8 ncq_tag)167f11c7f63SJim Harris void scif_sas_stp_remote_device_free_ncq_tag(
168f11c7f63SJim Harris    struct SCIF_SAS_REMOTE_DEVICE * fw_device,
169f11c7f63SJim Harris    U8                              ncq_tag
170f11c7f63SJim Harris )
171f11c7f63SJim Harris {
172f11c7f63SJim Harris    SCIF_LOG_INFO((
173f11c7f63SJim Harris       sci_base_object_get_logger(fw_device),
174f11c7f63SJim Harris       SCIF_LOG_OBJECT_REMOTE_DEVICE | SCIF_LOG_OBJECT_IO_REQUEST,
175f11c7f63SJim Harris       "RemoteDevice:0x%x NcqTag:0x%x freeing NCQ TAG\n",
176f11c7f63SJim Harris       fw_device, ncq_tag
177f11c7f63SJim Harris    ));
178f11c7f63SJim Harris 
179f11c7f63SJim Harris    fw_device->protocol_device.stp_device.s_active &= ~(1 << ncq_tag);
180f11c7f63SJim Harris }
181f11c7f63SJim Harris 
182f11c7f63SJim Harris struct SCIF_SAS_REQUEST *
scif_sas_stp_remote_device_get_request_by_ncq_tag(struct SCIF_SAS_REMOTE_DEVICE * fw_device,U8 ncq_tag)183f11c7f63SJim Harris scif_sas_stp_remote_device_get_request_by_ncq_tag(
184f11c7f63SJim Harris    struct SCIF_SAS_REMOTE_DEVICE * fw_device,
185f11c7f63SJim Harris    U8                              ncq_tag
186f11c7f63SJim Harris )
187f11c7f63SJim Harris {
188f11c7f63SJim Harris    SCIF_SAS_DOMAIN_T                * fw_domain = fw_device->domain;
189f11c7f63SJim Harris    SCI_FAST_LIST_ELEMENT_T          * pending_request_element;
190f11c7f63SJim Harris    SCIF_SAS_REQUEST_T               * pending_request = NULL;
191f11c7f63SJim Harris    SCIF_SAS_REQUEST_T               * matching_request = NULL;
192f11c7f63SJim Harris 
193f11c7f63SJim Harris    pending_request_element = fw_domain->request_list.list_head;
194f11c7f63SJim Harris 
195f11c7f63SJim Harris    while (pending_request_element != NULL)
196f11c7f63SJim Harris    {
197f11c7f63SJim Harris       pending_request =
198f11c7f63SJim Harris          (SCIF_SAS_REQUEST_T*) sci_fast_list_get_object(pending_request_element);
199f11c7f63SJim Harris 
200453130d9SPedro F. Giffuni       // The current element may be deleted from the list because of
201f11c7f63SJim Harris       // IO completion so advance to the next element early
202f11c7f63SJim Harris       pending_request_element = sci_fast_list_get_next(pending_request_element);
203f11c7f63SJim Harris 
204f11c7f63SJim Harris       if (
205f11c7f63SJim Harris             (pending_request->device == fw_device) &&
206f11c7f63SJim Harris             (pending_request->stp.sequence.protocol == SAT_PROTOCOL_FPDMA) &&
207f11c7f63SJim Harris             (pending_request->stp.ncq_tag == ncq_tag)
208f11c7f63SJim Harris          )
209f11c7f63SJim Harris       {
210f11c7f63SJim Harris          matching_request = pending_request;
211f11c7f63SJim Harris       }
212f11c7f63SJim Harris    }
213f11c7f63SJim Harris 
214f11c7f63SJim Harris    return matching_request;
215f11c7f63SJim Harris }
216