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.
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
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.
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.
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.
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 #include <scsi/sas_ata.h>
64 * enum isci_tmf_cb_state - This enum defines the possible states in which the
65 * TMF callback function is invoked during the TMF execution process.
69 enum isci_tmf_cb_state {
71 isci_tmf_init_state = 0,
77 * enum isci_tmf_function_codes - This enum defines the possible preparations
78 * of task management requests.
82 enum isci_tmf_function_codes {
84 isci_tmf_func_none = 0,
85 isci_tmf_ssp_task_abort = TMF_ABORT_TASK,
86 isci_tmf_ssp_lun_reset = TMF_LU_RESET,
87 isci_tmf_sata_srst_high = TMF_LU_RESET + 0x100, /* Non SCSI */
88 isci_tmf_sata_srst_low = TMF_LU_RESET + 0x101 /* Non SCSI */
91 * struct isci_tmf - This class represents the task management object which
92 * acts as an interface to libsas for processing task management requests
98 struct completion *complete;
99 enum sas_protocol proto;
101 struct ssp_response_iu resp_iu;
102 struct dev_to_host_fis d2h_fis;
104 unsigned char lun[8];
106 struct isci_remote_device *device;
107 enum isci_tmf_function_codes tmf_code;
110 /* The optional callback function allows the user process to
111 * track the TMF transmit / timeout conditions.
113 void (*cb_state_func)(
114 enum isci_tmf_cb_state,
115 struct isci_tmf *, void *);
120 static inline void isci_print_tmf(struct isci_tmf *tmf)
122 if (SAS_PROTOCOL_SATA == tmf->proto)
123 dev_dbg(&tmf->device->isci_port->isci_host->pdev->dev,
125 "tmf->resp.d2h_fis.status = %x\n"
126 "tmf->resp.d2h_fis.error = %x\n",
129 tmf->resp.d2h_fis.status,
130 tmf->resp.d2h_fis.error);
132 dev_dbg(&tmf->device->isci_port->isci_host->pdev->dev,
134 "tmf->resp.resp_iu.data_present = %x\n"
135 "tmf->resp.resp_iu.status = %x\n"
136 "tmf->resp.resp_iu.data_length = %x\n"
137 "tmf->resp.resp_iu.data[0] = %x\n"
138 "tmf->resp.resp_iu.data[1] = %x\n"
139 "tmf->resp.resp_iu.data[2] = %x\n"
140 "tmf->resp.resp_iu.data[3] = %x\n",
143 tmf->resp.resp_iu.datapres,
144 tmf->resp.resp_iu.status,
145 be32_to_cpu(tmf->resp.resp_iu.response_data_len),
146 tmf->resp.resp_iu.resp_data[0],
147 tmf->resp.resp_iu.resp_data[1],
148 tmf->resp.resp_iu.resp_data[2],
149 tmf->resp.resp_iu.resp_data[3]);
153 int isci_task_execute_task(
154 struct sas_task *task,
158 int isci_task_abort_task(
159 struct sas_task *task);
161 int isci_task_abort_task_set(
162 struct domain_device *d_device,
165 int isci_task_clear_aca(
166 struct domain_device *d_device,
169 int isci_task_clear_task_set(
170 struct domain_device *d_device,
173 int isci_task_query_task(
174 struct sas_task *task);
176 int isci_task_lu_reset(
177 struct domain_device *d_device,
180 int isci_task_clear_nexus_port(
181 struct asd_sas_port *port);
183 int isci_task_clear_nexus_ha(
184 struct sas_ha_struct *ha);
186 int isci_task_I_T_nexus_reset(
187 struct domain_device *d_device);
189 void isci_task_request_complete(
190 struct isci_host *isci_host,
191 struct isci_request *request,
192 enum sci_task_status completion_status);
194 u16 isci_task_ssp_request_get_io_tag_to_manage(
195 struct isci_request *request);
197 u8 isci_task_ssp_request_get_function(
198 struct isci_request *request);
201 void *isci_task_ssp_request_get_response_data_address(
202 struct isci_request *request);
204 u32 isci_task_ssp_request_get_response_data_length(
205 struct isci_request *request);
207 int isci_queuecommand(
208 struct scsi_cmnd *scsi_cmd,
209 void (*donefunc)(struct scsi_cmnd *));
211 int isci_bus_reset_handler(struct scsi_cmnd *cmd);
213 void isci_task_build_tmf(
214 struct isci_tmf *tmf,
215 struct isci_remote_device *isci_device,
216 enum isci_tmf_function_codes code,
217 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
223 int isci_task_execute_tmf(
224 struct isci_host *isci_host,
225 struct isci_tmf *tmf,
226 unsigned long timeout_ms);
229 * enum isci_completion_selection - This enum defines the possible actions to
230 * take with respect to a given request's notification back to libsas.
234 enum isci_completion_selection {
236 isci_perform_normal_io_completion, /* Normal notify (task_done) */
237 isci_perform_aborted_io_completion, /* No notification. */
238 isci_perform_error_io_completion /* Use sas_task_abort */
241 static inline void isci_set_task_doneflags(
242 struct sas_task *task)
244 /* Since no futher action will be taken on this task,
245 * make sure to mark it complete from the lldd perspective.
247 task->task_state_flags |= SAS_TASK_STATE_DONE;
248 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
249 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
252 * isci_task_all_done() - This function clears the task bits to indicate the
253 * LLDD is done with the task.
257 static inline void isci_task_all_done(
258 struct sas_task *task)
262 /* Since no futher action will be taken on this task,
263 * make sure to mark it complete from the lldd perspective.
265 spin_lock_irqsave(&task->task_state_lock, flags);
266 isci_set_task_doneflags(task);
267 spin_unlock_irqrestore(&task->task_state_lock, flags);
271 * isci_task_set_completion_status() - This function sets the completion status
273 * @task: This parameter is the completed request.
274 * @response: This parameter is the response code for the completed task.
275 * @status: This parameter is the status code for the completed task.
277 * @return The new notification mode for the request.
279 static inline enum isci_completion_selection
280 isci_task_set_completion_status(
281 struct sas_task *task,
282 enum service_response response,
283 enum exec_status status,
284 enum isci_completion_selection task_notification_selection)
288 spin_lock_irqsave(&task->task_state_lock, flags);
290 /* If a device reset is being indicated, make sure the I/O
291 * is in the error path.
293 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET) {
294 /* Fail the I/O to make sure it goes into the error path. */
295 response = SAS_TASK_UNDELIVERED;
296 status = SAM_STAT_TASK_ABORTED;
298 task_notification_selection = isci_perform_error_io_completion;
300 task->task_status.resp = response;
301 task->task_status.stat = status;
303 switch (task_notification_selection) {
305 case isci_perform_error_io_completion:
307 if (task->task_proto == SAS_PROTOCOL_SMP) {
308 /* There is no error escalation in the SMP case.
309 * Convert to a normal completion to avoid the
310 * timeout in the discovery path and to let the
311 * next action take place quickly.
313 task_notification_selection
314 = isci_perform_normal_io_completion;
316 /* Fall through to the normal case... */
318 /* Use sas_task_abort */
319 /* Leave SAS_TASK_STATE_DONE clear
320 * Leave SAS_TASK_AT_INITIATOR set.
325 case isci_perform_aborted_io_completion:
326 /* This path can occur with task-managed requests as well as
327 * requests terminated because of LUN or device resets.
329 /* Fall through to the normal case... */
330 case isci_perform_normal_io_completion:
331 /* Normal notification (task_done) */
332 isci_set_task_doneflags(task);
335 WARN_ONCE(1, "unknown task_notification_selection: %d\n",
336 task_notification_selection);
340 spin_unlock_irqrestore(&task->task_state_lock, flags);
342 return task_notification_selection;
346 * isci_execpath_callback() - This function is called from the task
347 * execute path when the task needs to callback libsas about the submit-time
348 * task failure. The callback occurs either through the task's done function
349 * or through sas_task_abort. In the case of regular non-discovery SATA/STP I/O
350 * requests, libsas takes the host lock before calling execute task. Therefore
351 * in this situation the host lock must be managed before calling the func.
353 * @ihost: This parameter is the controller to which the I/O request was sent.
354 * @task: This parameter is the I/O request.
355 * @func: This parameter is the function to call in the correct context.
356 * @status: This parameter is the status code for the completed task.
359 static inline void isci_execpath_callback(struct isci_host *ihost,
360 struct sas_task *task,
361 void (*func)(struct sas_task *))
363 struct domain_device *dev = task->dev;
365 if (dev_is_sata(dev) && task->uldd_task) {
368 /* Since we are still in the submit path, and since
369 * libsas takes the host lock on behalf of SATA
370 * devices before I/O starts (in the non-discovery case),
371 * we need to unlock before we can call the callback function.
373 raw_local_irq_save(flags);
374 spin_unlock(dev->sata_dev.ap->lock);
376 spin_lock(dev->sata_dev.ap->lock);
377 raw_local_irq_restore(flags);
381 #endif /* !defined(_SCI_TASK_H_) */