1230557Sjimharris/*-
2230557Sjimharris * This file is provided under a dual BSD/GPLv2 license.  When using or
3230557Sjimharris * redistributing this file, you may do so under either license.
4230557Sjimharris *
5230557Sjimharris * GPL LICENSE SUMMARY
6230557Sjimharris *
7230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8230557Sjimharris *
9230557Sjimharris * This program is free software; you can redistribute it and/or modify
10230557Sjimharris * it under the terms of version 2 of the GNU General Public License as
11230557Sjimharris * published by the Free Software Foundation.
12230557Sjimharris *
13230557Sjimharris * This program is distributed in the hope that it will be useful, but
14230557Sjimharris * WITHOUT ANY WARRANTY; without even the implied warranty of
15230557Sjimharris * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16230557Sjimharris * General Public License for more details.
17230557Sjimharris *
18230557Sjimharris * You should have received a copy of the GNU General Public License
19230557Sjimharris * along with this program; if not, write to the Free Software
20230557Sjimharris * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21230557Sjimharris * The full GNU General Public License is included in this distribution
22230557Sjimharris * in the file called LICENSE.GPL.
23230557Sjimharris *
24230557Sjimharris * BSD LICENSE
25230557Sjimharris *
26230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27230557Sjimharris * All rights reserved.
28230557Sjimharris *
29230557Sjimharris * Redistribution and use in source and binary forms, with or without
30230557Sjimharris * modification, are permitted provided that the following conditions
31230557Sjimharris * are met:
32230557Sjimharris *
33230557Sjimharris *   * Redistributions of source code must retain the above copyright
34230557Sjimharris *     notice, this list of conditions and the following disclaimer.
35230557Sjimharris *   * Redistributions in binary form must reproduce the above copyright
36230557Sjimharris *     notice, this list of conditions and the following disclaimer in
37230557Sjimharris *     the documentation and/or other materials provided with the
38230557Sjimharris *     distribution.
39230557Sjimharris *
40230557Sjimharris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41230557Sjimharris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42230557Sjimharris * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43230557Sjimharris * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44230557Sjimharris * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45230557Sjimharris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46230557Sjimharris * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47230557Sjimharris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48230557Sjimharris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49230557Sjimharris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50230557Sjimharris * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51230557Sjimharris */
52230557Sjimharris
53230557Sjimharris#include <sys/cdefs.h>
54230557Sjimharris__FBSDID("$FreeBSD$");
55230557Sjimharris
56230557Sjimharris/**
57230557Sjimharris * @file
58230557Sjimharris *
59230557Sjimharris * @brief This file contains the protected interface structures, constants,
60230557Sjimharris *        and methods for the SCIF_SAS_STP_REMOTE_DEVICE object.
61230557Sjimharris */
62230557Sjimharris
63230557Sjimharris#include <dev/isci/scil/scif_sas_stp_remote_device.h>
64230557Sjimharris#include <dev/isci/scil/scif_sas_remote_device.h>
65230557Sjimharris#include <dev/isci/scil/scif_sas_domain.h>
66230557Sjimharris#include <dev/isci/scil/scif_sas_controller.h>
67230557Sjimharris#include <dev/isci/scil/scif_sas_logger.h>
68230557Sjimharris#include <dev/isci/scil/intel_sat.h>
69230557Sjimharris
70230557Sjimharris/**
71230557Sjimharris * @brief This method performs SATA/STP specific construction of the
72230557Sjimharris *        STP remote device object.
73230557Sjimharris *
74230557Sjimharris * @param[in] device This parameter specifies the STP remote device
75230557Sjimharris *            object to be constructed.
76230557Sjimharris *
77230557Sjimharris * @return none
78230557Sjimharris */
79230557Sjimharrisvoid scif_sas_stp_remote_device_construct(
80230557Sjimharris   SCIF_SAS_REMOTE_DEVICE_T * device
81230557Sjimharris)
82230557Sjimharris{
83230557Sjimharris   sati_device_construct(
84230557Sjimharris      &device->protocol_device.stp_device.sati_device,
85230557Sjimharris      device->domain->controller->user_parameters.sas.is_sata_ncq_enabled,
86230557Sjimharris      (U8) device->domain->controller->user_parameters.sas.max_ncq_depth,
87230557Sjimharris      device->domain->controller->user_parameters.sas.ignore_fua
88230557Sjimharris   );
89230557Sjimharris
90230557Sjimharris   device->protocol_device.stp_device.s_active = 0;
91230557Sjimharris}
92230557Sjimharris
93230557Sjimharris/**
94230557Sjimharris * @brief This method attempts to allocate a valid NCQ tag from the list
95230557Sjimharris *        of available tags in the remote device.
96230557Sjimharris *
97230557Sjimharris * @todo Attempt to find a CLZ like instruction to optimize this routine
98230557Sjimharris *       down into a few instructions.  I know there is one like it for IA.
99230557Sjimharris *
100230557Sjimharris * @param[in] fw_device This parameter specifies the remote device
101230557Sjimharris *            for which to allocate an available NCQ tag.
102230557Sjimharris *
103230557Sjimharris * @return Return an available NCQ tag.
104230557Sjimharris * @retval 0-31 These values indicate an available tag was successfully
105230557Sjimharris *         allocated.
106230557Sjimharris * @return SCIF_SAS_STP_INVALID_NCQ_TAG This value indicates that there are
107230557Sjimharris *         no available NCQ tags.
108230557Sjimharris */
109230557SjimharrisU8 scif_sas_stp_remote_device_allocate_ncq_tag(
110230557Sjimharris   SCIF_SAS_REMOTE_DEVICE_T * fw_device
111230557Sjimharris)
112230557Sjimharris{
113230557Sjimharris   U8  ncq_tag  = 0;
114230557Sjimharris   U32 tag_mask = 1;
115230557Sjimharris
116230557Sjimharris   SCIF_LOG_TRACE((
117230557Sjimharris      sci_base_object_get_logger(fw_device),
118230557Sjimharris      SCIF_LOG_OBJECT_REMOTE_DEVICE | SCIF_LOG_OBJECT_IO_REQUEST,
119230557Sjimharris      "scif_sas_stp_remote_device_allocate_ncq_tag(0x%x)\n",
120230557Sjimharris      fw_device
121230557Sjimharris   ));
122230557Sjimharris
123230557Sjimharris   // Try to find an unused NCQ tag.
124230557Sjimharris   while (  (fw_device->protocol_device.stp_device.s_active & tag_mask)
125230557Sjimharris         && (ncq_tag < fw_device->protocol_device.stp_device.sati_device.ncq_depth) )
126230557Sjimharris   {
127230557Sjimharris      tag_mask <<= 1;
128230557Sjimharris      ncq_tag++;
129230557Sjimharris   }
130230557Sjimharris
131230557Sjimharris   // Check to see if we were able to find an available NCQ tag.
132230557Sjimharris   if (ncq_tag < fw_device->protocol_device.stp_device.sati_device.ncq_depth)
133230557Sjimharris   {
134230557Sjimharris      SCIF_LOG_INFO((
135230557Sjimharris         sci_base_object_get_logger(fw_device),
136230557Sjimharris         SCIF_LOG_OBJECT_REMOTE_DEVICE | SCIF_LOG_OBJECT_IO_REQUEST,
137230557Sjimharris         "RemoteDevice:0x%x NcqTag:0x%x successful NCQ TAG allocation\n",
138230557Sjimharris         fw_device, ncq_tag
139230557Sjimharris      ));
140230557Sjimharris
141230557Sjimharris      fw_device->protocol_device.stp_device.s_active |= tag_mask;
142230557Sjimharris      return ncq_tag;
143230557Sjimharris   }
144230557Sjimharris
145230557Sjimharris   SCIF_LOG_INFO((
146230557Sjimharris      sci_base_object_get_logger(fw_device),
147230557Sjimharris      SCIF_LOG_OBJECT_REMOTE_DEVICE | SCIF_LOG_OBJECT_IO_REQUEST,
148230557Sjimharris      "RemoteDevice:0x%x unable to allocate NCQ TAG\n",
149230557Sjimharris      fw_device
150230557Sjimharris   ));
151230557Sjimharris
152230557Sjimharris   // All NCQ tags are in use.
153230557Sjimharris   return SCIF_SAS_INVALID_NCQ_TAG;
154230557Sjimharris}
155230557Sjimharris
156230557Sjimharris/**
157230557Sjimharris * @brief This method removes the specified tag from the list of
158230557Sjimharris *        outstanding tags.  It doesn't return any values.
159230557Sjimharris *
160230557Sjimharris * @param[in] fw_device This parameter specifies the remote device for
161230557Sjimharris *            which to free an NCQ tag.
162230557Sjimharris * @param[in] ncq_tag This parameter specifies the NCQ tag that is
163230557Sjimharris *            to be freed.
164230557Sjimharris *
165230557Sjimharris * @return none
166230557Sjimharris */
167230557Sjimharrisvoid scif_sas_stp_remote_device_free_ncq_tag(
168230557Sjimharris   struct SCIF_SAS_REMOTE_DEVICE * fw_device,
169230557Sjimharris   U8                              ncq_tag
170230557Sjimharris)
171230557Sjimharris{
172230557Sjimharris   SCIF_LOG_INFO((
173230557Sjimharris      sci_base_object_get_logger(fw_device),
174230557Sjimharris      SCIF_LOG_OBJECT_REMOTE_DEVICE | SCIF_LOG_OBJECT_IO_REQUEST,
175230557Sjimharris      "RemoteDevice:0x%x NcqTag:0x%x freeing NCQ TAG\n",
176230557Sjimharris      fw_device, ncq_tag
177230557Sjimharris   ));
178230557Sjimharris
179230557Sjimharris   fw_device->protocol_device.stp_device.s_active &= ~(1 << ncq_tag);
180230557Sjimharris}
181230557Sjimharris
182230557Sjimharrisstruct SCIF_SAS_REQUEST *
183230557Sjimharrisscif_sas_stp_remote_device_get_request_by_ncq_tag(
184230557Sjimharris   struct SCIF_SAS_REMOTE_DEVICE * fw_device,
185230557Sjimharris   U8                              ncq_tag
186230557Sjimharris)
187230557Sjimharris{
188230557Sjimharris   SCIF_SAS_DOMAIN_T                * fw_domain = fw_device->domain;
189230557Sjimharris   SCI_FAST_LIST_ELEMENT_T          * pending_request_element;
190230557Sjimharris   SCIF_SAS_REQUEST_T               * pending_request = NULL;
191230557Sjimharris   SCIF_SAS_REQUEST_T               * matching_request = NULL;
192230557Sjimharris
193230557Sjimharris   pending_request_element = fw_domain->request_list.list_head;
194230557Sjimharris
195230557Sjimharris   while (pending_request_element != NULL)
196230557Sjimharris   {
197230557Sjimharris      pending_request =
198230557Sjimharris         (SCIF_SAS_REQUEST_T*) sci_fast_list_get_object(pending_request_element);
199230557Sjimharris
200298955Spfg      // The current element may be deleted from the list because of
201230557Sjimharris      // IO completion so advance to the next element early
202230557Sjimharris      pending_request_element = sci_fast_list_get_next(pending_request_element);
203230557Sjimharris
204230557Sjimharris      if (
205230557Sjimharris            (pending_request->device == fw_device) &&
206230557Sjimharris            (pending_request->stp.sequence.protocol == SAT_PROTOCOL_FPDMA) &&
207230557Sjimharris            (pending_request->stp.ncq_tag == ncq_tag)
208230557Sjimharris         )
209230557Sjimharris      {
210230557Sjimharris         matching_request = pending_request;
211230557Sjimharris      }
212230557Sjimharris   }
213230557Sjimharris
214230557Sjimharris   return matching_request;
215230557Sjimharris}
216