isci: Fix TMF build for SAS/SATA LUN reset cases.
[linux-2.6-microblaze.git] / drivers / scsi / isci / task.h
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  *   * 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.
42  *
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.
54  */
55
56 #if !defined(_ISCI_TASK_H_)
57 #define _ISCI_TASK_H_
58
59 struct isci_request;
60 struct isci_host;
61
62 /**
63  * enum isci_tmf_cb_state - This enum defines the possible states in which the
64  *    TMF callback function is invoked during the TMF execution process.
65  *
66  *
67  */
68 enum isci_tmf_cb_state {
69
70         isci_tmf_init_state = 0,
71         isci_tmf_started,
72         isci_tmf_timed_out
73 };
74
75 /**
76  * enum isci_tmf_function_codes - This enum defines the possible preparations
77  *    of task management requests.
78  *
79  *
80  */
81 enum isci_tmf_function_codes {
82
83         isci_tmf_func_none      = 0,
84         isci_tmf_ssp_task_abort = TMF_ABORT_TASK,
85         isci_tmf_ssp_lun_reset  = TMF_LU_RESET,
86         isci_tmf_sata_srst_high = TMF_LU_RESET + 0x100, /* Non SCSI */
87         isci_tmf_sata_srst_low  = TMF_LU_RESET + 0x101  /* Non SCSI */
88 };
89 /**
90  * struct isci_tmf - This class represents the task management object which
91  *    acts as an interface to libsas for processing task management requests
92  *
93  *
94  */
95 struct isci_tmf {
96
97         struct completion *complete;
98         enum sas_protocol proto;
99         union {
100                 struct sci_ssp_response_iu resp_iu;
101                 struct dev_to_host_fis d2h_fis;
102         }                            resp;
103         unsigned char lun[8];
104         u16 io_tag;
105         struct isci_remote_device *device;
106         enum isci_tmf_function_codes tmf_code;
107         int status;
108
109         struct isci_timer *timeout_timer;
110
111         /* The optional callback function allows the user process to
112          * track the TMF transmit / timeout conditions.
113          */
114         void (*cb_state_func)(
115                 enum isci_tmf_cb_state,
116                 struct isci_tmf *, void *);
117         void *cb_data;
118
119 };
120
121 static inline void isci_print_tmf(
122         struct isci_tmf *tmf)
123 {
124         if (SAS_PROTOCOL_SATA == tmf->proto)
125                 dev_dbg(&tmf->device->isci_port->isci_host->pdev->dev,
126                         "%s: status = %x\n"
127                         "tmf->resp.d2h_fis.status = %x\n"
128                         "tmf->resp.d2h_fis.error = %x\n",
129                         __func__,
130                         tmf->status,
131                         tmf->resp.d2h_fis.status,
132                         tmf->resp.d2h_fis.error);
133         else
134                 dev_dbg(&tmf->device->isci_port->isci_host->pdev->dev,
135                         "%s: status = %x\n"
136                         "tmf->resp.resp_iu.data_present = %x\n"
137                         "tmf->resp.resp_iu.status = %x\n"
138                         "tmf->resp.resp_iu.data_length = %x\n"
139                         "tmf->resp.resp_iu.data[0] = %x\n"
140                         "tmf->resp.resp_iu.data[1] = %x\n"
141                         "tmf->resp.resp_iu.data[2] = %x\n"
142                         "tmf->resp.resp_iu.data[3] = %x\n",
143                         __func__,
144                         tmf->status,
145                         tmf->resp.resp_iu.data_present,
146                         tmf->resp.resp_iu.status,
147                         (tmf->resp.resp_iu.response_data_length[0] << 24) +
148                         (tmf->resp.resp_iu.response_data_length[1] << 16) +
149                         (tmf->resp.resp_iu.response_data_length[2] << 8) +
150                         tmf->resp.resp_iu.response_data_length[3],
151                         tmf->resp.resp_iu.data[0],
152                         tmf->resp.resp_iu.data[1],
153                         tmf->resp.resp_iu.data[2],
154                         tmf->resp.resp_iu.data[3]);
155 }
156
157
158 int isci_task_execute_task(
159         struct sas_task *task,
160         int num,
161         gfp_t gfp_flags);
162
163 int isci_task_abort_task(
164         struct sas_task *task);
165
166 int isci_task_abort_task_set(
167         struct domain_device *d_device,
168         u8 *lun);
169
170 int isci_task_clear_aca(
171         struct domain_device *d_device,
172         u8 *lun);
173
174 int isci_task_clear_task_set(
175         struct domain_device *d_device,
176         u8 *lun);
177
178 int isci_task_query_task(
179         struct sas_task *task);
180
181 int isci_task_lu_reset(
182         struct domain_device *d_device,
183         u8 *lun);
184
185 int isci_task_clear_nexus_port(
186         struct asd_sas_port *port);
187
188 int isci_task_clear_nexus_ha(
189         struct sas_ha_struct *ha);
190
191 int isci_task_I_T_nexus_reset(
192         struct domain_device *d_device);
193
194 void isci_task_request_complete(
195         struct isci_host *isci_host,
196         struct isci_request *request,
197         enum sci_task_status completion_status);
198
199 u16 isci_task_ssp_request_get_io_tag_to_manage(
200         struct isci_request *request);
201
202 u8 isci_task_ssp_request_get_function(
203         struct isci_request *request);
204
205 u32 isci_task_ssp_request_get_lun(
206         struct isci_request *request);
207
208 void *isci_task_ssp_request_get_response_data_address(
209         struct isci_request *request);
210
211 u32 isci_task_ssp_request_get_response_data_length(
212         struct isci_request *request);
213
214 int isci_queuecommand(
215         struct scsi_cmnd *scsi_cmd,
216         void (*donefunc)(struct scsi_cmnd *));
217
218 int isci_bus_reset_handler(struct scsi_cmnd *cmd);
219
220 void isci_task_build_tmf(
221         struct isci_tmf *tmf,
222         struct isci_remote_device *isci_device,
223         enum isci_tmf_function_codes code,
224         void (*tmf_sent_cb)(enum isci_tmf_cb_state,
225                             struct isci_tmf *,
226                             void *),
227         void *cb_data);
228
229 void isci_task_build_abort_task_tmf(
230         struct isci_tmf *tmf,
231         struct isci_remote_device *isci_device,
232         enum isci_tmf_function_codes code,
233         void (*tmf_sent_cb)(
234                 enum isci_tmf_cb_state,
235                 struct isci_tmf *, void *),
236         struct isci_request *old_request);
237
238 int isci_task_execute_tmf(
239         struct isci_host *isci_host,
240         struct isci_tmf *tmf,
241         unsigned long timeout_ms);
242
243 /**
244  * enum isci_completion_selection - This enum defines the possible actions to
245  *    take with respect to a given request's notification back to libsas.
246  *
247  *
248  */
249 enum isci_completion_selection {
250
251         isci_perform_normal_io_completion,      /* Normal notify (task_done) */
252         isci_perform_aborted_io_completion,     /* No notification.   */
253         isci_perform_error_io_completion        /* Use sas_task_abort */
254 };
255
256 static inline void isci_set_task_doneflags(
257         struct sas_task *task)
258 {
259         /* Since no futher action will be taken on this task,
260          * make sure to mark it complete from the lldd perspective.
261          */
262         task->task_state_flags |= SAS_TASK_STATE_DONE;
263         task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
264         task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
265 }
266 /**
267  * isci_task_all_done() - This function clears the task bits to indicate the
268  *    LLDD is done with the task.
269  *
270  *
271  */
272 static inline void isci_task_all_done(
273         struct sas_task *task)
274 {
275         unsigned long flags;
276
277         /* Since no futher action will be taken on this task,
278          * make sure to mark it complete from the lldd perspective.
279          */
280         spin_lock_irqsave(&task->task_state_lock, flags);
281         isci_set_task_doneflags(task);
282         spin_unlock_irqrestore(&task->task_state_lock, flags);
283 }
284
285 /**
286  * isci_task_set_completion_status() - This function sets the completion status
287  *    for the request.
288  * @task: This parameter is the completed request.
289  * @response: This parameter is the response code for the completed task.
290  * @status: This parameter is the status code for the completed task.
291  *
292 * @return The new notification mode for the request.
293 */
294 static inline enum isci_completion_selection
295 isci_task_set_completion_status(
296         struct sas_task *task,
297         enum service_response response,
298         enum exec_status status,
299         enum isci_completion_selection task_notification_selection)
300 {
301         unsigned long flags;
302
303         spin_lock_irqsave(&task->task_state_lock, flags);
304
305         task->task_status.resp = response;
306         task->task_status.stat = status;
307
308         /* If a device reset is being indicated, make sure the I/O
309         * is in the error path.
310         */
311         if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
312                 task_notification_selection = isci_perform_error_io_completion;
313
314         switch (task_notification_selection) {
315
316                 case isci_perform_aborted_io_completion:
317                         /* This path can occur with task-managed requests as well as
318                         * requests terminated because of LUN or device resets.
319                         */
320                         /* Fall through to the normal case... */
321
322                 case isci_perform_normal_io_completion:
323                         /* Normal notification (task_done) */
324                         isci_set_task_doneflags(task);
325                         break;
326
327                 default:
328                         WARN_ON(FALSE);
329                         /* Fall through to the error case... */
330
331                 case isci_perform_error_io_completion:
332                         /* Use sas_task_abort */
333                         /* Leave SAS_TASK_STATE_DONE clear
334                         *  Leave SAS_TASK_AT_INITIATOR set.
335                         */
336                         break;
337         }
338
339         spin_unlock_irqrestore(&task->task_state_lock, flags);
340
341         return task_notification_selection;
342
343 }
344 /**
345  * isci_task_complete_for_upper_layer() - This function completes the request
346  *    to the upper layer driver.
347  * @host: This parameter is a pointer to the host on which the the request
348  *    should be queued (either as an error or success).
349  * @request: This parameter is the completed request.
350  * @response: This parameter is the response code for the completed task.
351  * @status: This parameter is the status code for the completed task.
352  *
353  * none.
354  */
355 static inline void isci_task_complete_for_upper_layer(
356         struct sas_task *task,
357         enum service_response response,
358         enum exec_status status,
359         enum isci_completion_selection task_notification_selection)
360 {
361         task_notification_selection
362                 = isci_task_set_completion_status(task, response, status,
363                                                   task_notification_selection);
364
365         /* Tasks aborted specifically by a call to the lldd_abort_task
366          * function should not be completed to the host in the regular path.
367          */
368         switch (task_notification_selection) {
369         case isci_perform_normal_io_completion:
370                 /* Normal notification (task_done) */
371                 dev_dbg(task->dev->port->ha->dev,
372                         "%s: Normal - task = %p, response=%d, status=%d\n",
373                         __func__, task, response, status);
374                 task->task_done(task);
375                 task->lldd_task = NULL;
376                 break;
377
378         case isci_perform_aborted_io_completion:
379                 /* No notification because this request is already in the
380                  * abort path.
381                  */
382                 dev_warn(task->dev->port->ha->dev,
383                          "%s: Aborted - task = %p, response=%d, status=%d\n",
384                          __func__, task, response, status);
385                 break;
386
387         case isci_perform_error_io_completion:
388                 /* Use sas_task_abort */
389                 dev_warn(task->dev->port->ha->dev,
390                          "%s: Error - task = %p, response=%d, status=%d\n",
391                          __func__, task, response, status);
392                 sas_task_abort(task);
393                 break;
394
395         default:
396                 dev_warn(task->dev->port->ha->dev,
397                          "%s: isci task notification default case!",
398                          __func__);
399                 sas_task_abort(task);
400                 break;
401         }
402 }
403
404 #endif /* !defined(_SCI_TASK_H_) */