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