xref: /freebsd/sys/dev/isci/scil/scif_sas_stp_remote_device.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
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 __FBSDID("$FreeBSD$");
57 
58 /**
59  * @file
60  *
61  * @brief This file contains the protected interface structures, constants,
62  *        and methods for the SCIF_SAS_STP_REMOTE_DEVICE object.
63  */
64 
65 #include <dev/isci/scil/scif_sas_stp_remote_device.h>
66 #include <dev/isci/scil/scif_sas_remote_device.h>
67 #include <dev/isci/scil/scif_sas_domain.h>
68 #include <dev/isci/scil/scif_sas_controller.h>
69 #include <dev/isci/scil/scif_sas_logger.h>
70 #include <dev/isci/scil/intel_sat.h>
71 
72 /**
73  * @brief This method performs SATA/STP specific construction of the
74  *        STP remote device object.
75  *
76  * @param[in] device This parameter specifies the STP remote device
77  *            object to be constructed.
78  *
79  * @return none
80  */
81 void scif_sas_stp_remote_device_construct(
82    SCIF_SAS_REMOTE_DEVICE_T * device
83 )
84 {
85    sati_device_construct(
86       &device->protocol_device.stp_device.sati_device,
87       device->domain->controller->user_parameters.sas.is_sata_ncq_enabled,
88       (U8) device->domain->controller->user_parameters.sas.max_ncq_depth,
89       device->domain->controller->user_parameters.sas.ignore_fua
90    );
91 
92    device->protocol_device.stp_device.s_active = 0;
93 }
94 
95 /**
96  * @brief This method attempts to allocate a valid NCQ tag from the list
97  *        of available tags in the remote device.
98  *
99  * @todo Attempt to find a CLZ like instruction to optimize this routine
100  *       down into a few instructions.  I know there is one like it for IA.
101  *
102  * @param[in] fw_device This parameter specifies the remote device
103  *            for which to allocate an available NCQ tag.
104  *
105  * @return Return an available NCQ tag.
106  * @retval 0-31 These values indicate an available tag was successfully
107  *         allocated.
108  * @return SCIF_SAS_STP_INVALID_NCQ_TAG This value indicates that there are
109  *         no available NCQ tags.
110  */
111 U8 scif_sas_stp_remote_device_allocate_ncq_tag(
112    SCIF_SAS_REMOTE_DEVICE_T * fw_device
113 )
114 {
115    U8  ncq_tag  = 0;
116    U32 tag_mask = 1;
117 
118    SCIF_LOG_TRACE((
119       sci_base_object_get_logger(fw_device),
120       SCIF_LOG_OBJECT_REMOTE_DEVICE | SCIF_LOG_OBJECT_IO_REQUEST,
121       "scif_sas_stp_remote_device_allocate_ncq_tag(0x%x)\n",
122       fw_device
123    ));
124 
125    // Try to find an unused NCQ tag.
126    while (  (fw_device->protocol_device.stp_device.s_active & tag_mask)
127          && (ncq_tag < fw_device->protocol_device.stp_device.sati_device.ncq_depth) )
128    {
129       tag_mask <<= 1;
130       ncq_tag++;
131    }
132 
133    // Check to see if we were able to find an available NCQ tag.
134    if (ncq_tag < fw_device->protocol_device.stp_device.sati_device.ncq_depth)
135    {
136       SCIF_LOG_INFO((
137          sci_base_object_get_logger(fw_device),
138          SCIF_LOG_OBJECT_REMOTE_DEVICE | SCIF_LOG_OBJECT_IO_REQUEST,
139          "RemoteDevice:0x%x NcqTag:0x%x successful NCQ TAG allocation\n",
140          fw_device, ncq_tag
141       ));
142 
143       fw_device->protocol_device.stp_device.s_active |= tag_mask;
144       return ncq_tag;
145    }
146 
147    SCIF_LOG_INFO((
148       sci_base_object_get_logger(fw_device),
149       SCIF_LOG_OBJECT_REMOTE_DEVICE | SCIF_LOG_OBJECT_IO_REQUEST,
150       "RemoteDevice:0x%x unable to allocate NCQ TAG\n",
151       fw_device
152    ));
153 
154    // All NCQ tags are in use.
155    return SCIF_SAS_INVALID_NCQ_TAG;
156 }
157 
158 /**
159  * @brief This method removes the specified tag from the list of
160  *        outstanding tags.  It doesn't return any values.
161  *
162  * @param[in] fw_device This parameter specifies the remote device for
163  *            which to free an NCQ tag.
164  * @param[in] ncq_tag This parameter specifies the NCQ tag that is
165  *            to be freed.
166  *
167  * @return none
168  */
169 void scif_sas_stp_remote_device_free_ncq_tag(
170    struct SCIF_SAS_REMOTE_DEVICE * fw_device,
171    U8                              ncq_tag
172 )
173 {
174    SCIF_LOG_INFO((
175       sci_base_object_get_logger(fw_device),
176       SCIF_LOG_OBJECT_REMOTE_DEVICE | SCIF_LOG_OBJECT_IO_REQUEST,
177       "RemoteDevice:0x%x NcqTag:0x%x freeing NCQ TAG\n",
178       fw_device, ncq_tag
179    ));
180 
181    fw_device->protocol_device.stp_device.s_active &= ~(1 << ncq_tag);
182 }
183 
184 struct SCIF_SAS_REQUEST *
185 scif_sas_stp_remote_device_get_request_by_ncq_tag(
186    struct SCIF_SAS_REMOTE_DEVICE * fw_device,
187    U8                              ncq_tag
188 )
189 {
190    SCIF_SAS_DOMAIN_T                * fw_domain = fw_device->domain;
191    SCI_FAST_LIST_ELEMENT_T          * pending_request_element;
192    SCIF_SAS_REQUEST_T               * pending_request = NULL;
193    SCIF_SAS_REQUEST_T               * matching_request = NULL;
194 
195    pending_request_element = fw_domain->request_list.list_head;
196 
197    while (pending_request_element != NULL)
198    {
199       pending_request =
200          (SCIF_SAS_REQUEST_T*) sci_fast_list_get_object(pending_request_element);
201 
202       // The current element may be deleted from the list because of
203       // IO completion so advance to the next element early
204       pending_request_element = sci_fast_list_get_next(pending_request_element);
205 
206       if (
207             (pending_request->device == fw_device) &&
208             (pending_request->stp.sequence.protocol == SAT_PROTOCOL_FPDMA) &&
209             (pending_request->stp.ncq_tag == ncq_tag)
210          )
211       {
212          matching_request = pending_request;
213       }
214    }
215 
216    return matching_request;
217 }
218