xref: /freebsd/sys/dev/isci/scil/scif_sas_stp_remote_device.c (revision 1c05a6ea6b849ff95e539c31adea887c644a6a01)
1 /*-
2  * This file is provided under a dual BSD/GPLv2 license.  When using or
3  * redistributing this file, you may do so under either license.
4  *
5  * GPL LICENSE SUMMARY
6  *
7  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
23  *
24  * BSD LICENSE
25  *
26  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  *
33  *   * Redistributions of source code must retain the above copyright
34  *     notice, this list of conditions and the following disclaimer.
35  *   * Redistributions in binary form must reproduce the above copyright
36  *     notice, this list of conditions and the following disclaimer in
37  *     the documentation and/or other materials provided with the
38  *     distribution.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51  */
52 
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55 
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  */
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  */
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  */
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 *
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