isci: atomic device lookup and reference counting
[deliverable/linux.git] / drivers / scsi / isci / remote_device.h
CommitLineData
6f231dda
DW
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
88f3b62a 56#ifndef _ISCI_REMOTE_DEVICE_H_
6f231dda 57#define _ISCI_REMOTE_DEVICE_H_
88f3b62a 58#include <scsi/libsas.h>
209fae14 59#include <linux/kref.h>
88f3b62a
DW
60#include "scu_remote_node_context.h"
61#include "remote_node_context.h"
62#include "port.h"
6f231dda 63
88f3b62a
DW
64enum scic_remote_device_not_ready_reason_code {
65 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED,
66 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED,
67 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED,
68 SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED,
69 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED,
88f3b62a 70 SCIC_REMOTE_DEVICE_NOT_READY_REASON_CODE_MAX
88f3b62a
DW
71};
72
73struct scic_sds_remote_device {
88f3b62a
DW
74 /**
75 * This field contains the information for the base remote device state
76 * machine.
77 */
e301370a 78 struct sci_base_state_machine sm;
88f3b62a
DW
79
80 /**
81 * This field is the programmed device port width. This value is
82 * written to the RCN data structure to tell the SCU how many open
83 * connections this device can have.
84 */
85 u32 device_port_width;
86
87 /**
88 * This field is the programmed connection rate for this remote device. It is
89 * used to program the TC with the maximum allowed connection rate.
90 */
91 enum sas_linkrate connection_rate;
92
88f3b62a
DW
93 /**
94 * This filed is assinged the value of true if the device is directly
95 * attached to the port.
96 */
97 bool is_direct_attached;
98
88f3b62a
DW
99 /**
100 * This filed contains a pointer back to the port to which this device
101 * is assigned.
102 */
103 struct scic_sds_port *owning_port;
104
105 /**
106 * This field contains the SCU silicon remote node context specific
107 * information.
108 */
109 struct scic_sds_remote_node_context rnc;
110
111 /**
112 * This field contains the stated request count for the remote device. The
e301370a 113 * device can not reach the SCI_DEV_STOPPED until all
88f3b62a
DW
114 * requests are complete and the rnc_posted value is false.
115 */
116 u32 started_request_count;
117
118 /**
119 * This field contains a pointer to the working request object. It is only
120 * used only for SATA requests since the unsolicited frames we get from the
121 * hardware have no Tag value to look up the io request object.
122 */
123 struct scic_sds_request *working_request;
124
125 /**
126 * This field contains the reason for the remote device going not_ready. It is
127 * assigned in the state handlers and used in the state transition.
128 */
129 u32 not_ready_reason;
88f3b62a 130};
6f231dda
DW
131
132struct isci_remote_device {
6f231dda 133 enum isci_status status;
6ad31fec
DW
134 #define IDEV_START_PENDING 0
135 #define IDEV_STOP_PENDING 1
d9c37390 136 #define IDEV_ALLOCATED 2
d06b487b 137 #define IDEV_EH 3
209fae14 138 #define IDEV_GONE 4
6ad31fec 139 unsigned long flags;
209fae14 140 struct kref kref;
6f231dda
DW
141 struct isci_port *isci_port;
142 struct domain_device *domain_dev;
6f231dda
DW
143 struct list_head node;
144 struct list_head reqs_in_process;
6f231dda 145 spinlock_t state_lock;
57f20f4e 146 struct scic_sds_remote_device sci;
6f231dda
DW
147};
148
6f231dda
DW
149#define ISCI_REMOTE_DEVICE_START_TIMEOUT 5000
150
209fae14
DW
151/* device reference routines must be called under scic_lock */
152static inline struct isci_remote_device *isci_lookup_device(struct domain_device *dev)
153{
154 struct isci_remote_device *idev = dev->lldd_dev;
155
156 if (idev && !test_bit(IDEV_GONE, &idev->flags)) {
157 kref_get(&idev->kref);
158 return idev;
159 }
160
161 return NULL;
162}
163
164void isci_remote_device_release(struct kref *kref);
165static inline void isci_put_device(struct isci_remote_device *idev)
166{
167 if (idev)
168 kref_put(&idev->kref, isci_remote_device_release);
169}
170
6ad31fec
DW
171enum sci_status isci_remote_device_stop(struct isci_host *ihost,
172 struct isci_remote_device *idev);
4393aa4e
DW
173void isci_remote_device_nuke_requests(struct isci_host *ihost,
174 struct isci_remote_device *idev);
4393aa4e
DW
175void isci_remote_device_gone(struct domain_device *domain_dev);
176int isci_remote_device_found(struct domain_device *domain_dev);
177bool isci_device_is_reset_pending(struct isci_host *ihost,
178 struct isci_remote_device *idev);
179void isci_device_clear_reset_pending(struct isci_host *ihost,
180 struct isci_remote_device *idev);
181void isci_remote_device_change_state(struct isci_remote_device *idev,
182 enum isci_status status);
88f3b62a
DW
183/**
184 * scic_remote_device_stop() - This method will stop both transmission and
185 * reception of link activity for the supplied remote device. This method
186 * disables normal IO requests from flowing through to the remote device.
187 * @remote_device: This parameter specifies the device to be stopped.
188 * @timeout: This parameter specifies the number of milliseconds in which the
189 * stop operation should complete.
190 *
191 * An indication of whether the device was successfully stopped. SCI_SUCCESS
192 * This value is returned if the transmission and reception for the device was
193 * successfully stopped.
194 */
195enum sci_status scic_remote_device_stop(
196 struct scic_sds_remote_device *remote_device,
197 u32 timeout);
198
199/**
200 * scic_remote_device_reset() - This method will reset the device making it
201 * ready for operation. This method must be called anytime the device is
202 * reset either through a SMP phy control or a port hard reset request.
203 * @remote_device: This parameter specifies the device to be reset.
204 *
205 * This method does not actually cause the device hardware to be reset. This
206 * method resets the software object so that it will be operational after a
207 * device hardware reset completes. An indication of whether the device reset
208 * was accepted. SCI_SUCCESS This value is returned if the device reset is
209 * started.
210 */
211enum sci_status scic_remote_device_reset(
212 struct scic_sds_remote_device *remote_device);
213
214/**
215 * scic_remote_device_reset_complete() - This method informs the device object
216 * that the reset operation is complete and the device can resume operation
217 * again.
218 * @remote_device: This parameter specifies the device which is to be informed
219 * of the reset complete operation.
220 *
221 * An indication that the device is resuming operation. SCI_SUCCESS the device
222 * is resuming operation.
223 */
224enum sci_status scic_remote_device_reset_complete(
225 struct scic_sds_remote_device *remote_device);
226
88f3b62a 227#define scic_remote_device_is_atapi(device_handle) false
88f3b62a
DW
228
229/**
230 * enum scic_sds_remote_device_states - This enumeration depicts all the states
231 * for the common remote device state machine.
232 *
233 *
234 */
235enum scic_sds_remote_device_states {
236 /**
237 * Simply the initial state for the base remote device state machine.
238 */
e301370a 239 SCI_DEV_INITIAL,
88f3b62a
DW
240
241 /**
242 * This state indicates that the remote device has successfully been
243 * stopped. In this state no new IO operations are permitted.
244 * This state is entered from the INITIAL state.
245 * This state is entered from the STOPPING state.
246 */
e301370a 247 SCI_DEV_STOPPED,
88f3b62a
DW
248
249 /**
250 * This state indicates the the remote device is in the process of
251 * becoming ready (i.e. starting). In this state no new IO operations
252 * are permitted.
253 * This state is entered from the STOPPED state.
254 */
e301370a 255 SCI_DEV_STARTING,
88f3b62a
DW
256
257 /**
258 * This state indicates the remote device is now ready. Thus, the user
259 * is able to perform IO operations on the remote device.
260 * This state is entered from the STARTING state.
261 */
e301370a 262 SCI_DEV_READY,
88f3b62a 263
88f3b62a
DW
264 /**
265 * This is the idle substate for the stp remote device. When there are no
266 * active IO for the device it is is in this state.
267 */
e301370a 268 SCI_STP_DEV_IDLE,
88f3b62a
DW
269
270 /**
271 * This is the command state for for the STP remote device. This state is
272 * entered when the device is processing a non-NCQ command. The device object
273 * will fail any new start IO requests until this command is complete.
274 */
e301370a 275 SCI_STP_DEV_CMD,
88f3b62a
DW
276
277 /**
278 * This is the NCQ state for the STP remote device. This state is entered
279 * when the device is processing an NCQ reuqest. It will remain in this state
280 * so long as there is one or more NCQ requests being processed.
281 */
e301370a 282 SCI_STP_DEV_NCQ,
88f3b62a
DW
283
284 /**
285 * This is the NCQ error state for the STP remote device. This state is
286 * entered when an SDB error FIS is received by the device object while in the
287 * NCQ state. The device object will only accept a READ LOG command while in
288 * this state.
289 */
e301370a 290 SCI_STP_DEV_NCQ_ERROR,
88f3b62a 291
88f3b62a
DW
292 /**
293 * This is the READY substate indicates the device is waiting for the RESET task
294 * coming to be recovered from certain hardware specific error.
295 */
e301370a 296 SCI_STP_DEV_AWAIT_RESET,
88f3b62a 297
88f3b62a
DW
298 /**
299 * This is the ready operational substate for the remote device. This is the
300 * normal operational state for a remote device.
301 */
e301370a 302 SCI_SMP_DEV_IDLE,
88f3b62a
DW
303
304 /**
305 * This is the suspended state for the remote device. This is the state that
306 * the device is placed in when a RNC suspend is received by the SCU hardware.
307 */
e301370a 308 SCI_SMP_DEV_CMD,
ab2e8f7d
DW
309
310 /**
311 * This state indicates that the remote device is in the process of
312 * stopping. In this state no new IO operations are permitted, but
313 * existing IO operations are allowed to complete.
314 * This state is entered from the READY state.
315 * This state is entered from the FAILED state.
316 */
e301370a 317 SCI_DEV_STOPPING,
ab2e8f7d
DW
318
319 /**
320 * This state indicates that the remote device has failed.
321 * In this state no new IO operations are permitted.
322 * This state is entered from the INITIALIZING state.
323 * This state is entered from the READY state.
324 */
e301370a 325 SCI_DEV_FAILED,
ab2e8f7d
DW
326
327 /**
328 * This state indicates the device is being reset.
329 * In this state no new IO operations are permitted.
330 * This state is entered from the READY state.
331 */
e301370a 332 SCI_DEV_RESETTING,
ab2e8f7d
DW
333
334 /**
335 * Simply the final state for the base remote device state machine.
336 */
e301370a 337 SCI_DEV_FINAL,
88f3b62a
DW
338};
339
340static inline struct scic_sds_remote_device *rnc_to_dev(struct scic_sds_remote_node_context *rnc)
341{
342 struct scic_sds_remote_device *sci_dev;
343
344 sci_dev = container_of(rnc, typeof(*sci_dev), rnc);
345
346 return sci_dev;
347}
348
5d937e96 349static inline struct isci_remote_device *sci_dev_to_idev(struct scic_sds_remote_device *sci_dev)
a1a113b0
DW
350{
351 struct isci_remote_device *idev = container_of(sci_dev, typeof(*idev), sci);
352
5d937e96
MP
353 return idev;
354}
355
356static inline struct domain_device *sci_dev_to_domain(struct scic_sds_remote_device *sci_dev)
357{
358 return sci_dev_to_idev(sci_dev)->domain_dev;
a1a113b0
DW
359}
360
361static inline bool dev_is_expander(struct domain_device *dev)
362{
363 return dev->dev_type == EDGE_DEV || dev->dev_type == FANOUT_DEV;
364}
365
88f3b62a
DW
366/**
367 * scic_sds_remote_device_increment_request_count() -
368 *
369 * This macro incrments the request count for this device
370 */
371#define scic_sds_remote_device_increment_request_count(sci_dev) \
372 ((sci_dev)->started_request_count++)
373
374/**
375 * scic_sds_remote_device_decrement_request_count() -
376 *
377 * This macro decrements the request count for this device. This count will
378 * never decrment past 0.
379 */
380#define scic_sds_remote_device_decrement_request_count(sci_dev) \
381 ((sci_dev)->started_request_count > 0 ? \
382 (sci_dev)->started_request_count-- : 0)
383
384/**
385 * scic_sds_remote_device_get_request_count() -
386 *
387 * This is a helper macro to return the current device request count.
388 */
389#define scic_sds_remote_device_get_request_count(sci_dev) \
390 ((sci_dev)->started_request_count)
391
392/**
393 * scic_sds_remote_device_get_port() -
394 *
395 * This macro returns the owning port of this remote device obejct.
396 */
397#define scic_sds_remote_device_get_port(sci_dev) \
398 ((sci_dev)->owning_port)
399
400/**
401 * scic_sds_remote_device_get_controller() -
402 *
403 * This macro returns the controller object that contains this device object
404 */
405#define scic_sds_remote_device_get_controller(sci_dev) \
406 scic_sds_port_get_controller(scic_sds_remote_device_get_port(sci_dev))
407
88f3b62a
DW
408/**
409 * scic_sds_remote_device_get_port() -
410 *
411 * This macro returns the owning port of this device
412 */
413#define scic_sds_remote_device_get_port(sci_dev) \
414 ((sci_dev)->owning_port)
415
416/**
417 * scic_sds_remote_device_get_sequence() -
418 *
419 * This macro returns the remote device sequence value
420 */
421#define scic_sds_remote_device_get_sequence(sci_dev) \
422 (\
423 scic_sds_remote_device_get_controller(sci_dev)-> \
424 remote_device_sequence[(sci_dev)->rnc.remote_node_index] \
425 )
426
427/**
428 * scic_sds_remote_device_get_controller_peg() -
429 *
430 * This macro returns the controllers protocol engine group
431 */
432#define scic_sds_remote_device_get_controller_peg(sci_dev) \
433 (\
434 scic_sds_controller_get_protocol_engine_group(\
435 scic_sds_port_get_controller(\
436 scic_sds_remote_device_get_port(sci_dev) \
437 ) \
438 ) \
439 )
440
88f3b62a
DW
441/**
442 * scic_sds_remote_device_get_index() -
443 *
444 * This macro returns the remote node index for this device object
445 */
446#define scic_sds_remote_device_get_index(sci_dev) \
447 ((sci_dev)->rnc.remote_node_index)
448
449/**
450 * scic_sds_remote_device_build_command_context() -
451 *
452 * This macro builds a remote device context for the SCU post request operation
453 */
454#define scic_sds_remote_device_build_command_context(device, command) \
455 ((command) \
456 | (scic_sds_remote_device_get_controller_peg((device)) << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) \
1f4fa1f9 457 | ((device)->owning_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) \
88f3b62a
DW
458 | (scic_sds_remote_device_get_index((device))) \
459 )
460
461/**
462 * scic_sds_remote_device_set_working_request() -
463 *
464 * This macro makes the working request assingment for the remote device
465 * object. To clear the working request use this macro with a NULL request
466 * object.
467 */
468#define scic_sds_remote_device_set_working_request(device, request) \
469 ((device)->working_request = (request))
470
471enum sci_status scic_sds_remote_device_frame_handler(
472 struct scic_sds_remote_device *sci_dev,
473 u32 frame_index);
474
475enum sci_status scic_sds_remote_device_event_handler(
476 struct scic_sds_remote_device *sci_dev,
477 u32 event_code);
478
479enum sci_status scic_sds_remote_device_start_io(
480 struct scic_sds_controller *controller,
481 struct scic_sds_remote_device *sci_dev,
482 struct scic_sds_request *io_request);
483
ab2e8f7d 484enum sci_status scic_sds_remote_device_start_task(
88f3b62a
DW
485 struct scic_sds_controller *controller,
486 struct scic_sds_remote_device *sci_dev,
487 struct scic_sds_request *io_request);
488
ab2e8f7d
DW
489enum sci_status scic_sds_remote_device_complete_io(
490 struct scic_sds_controller *controller,
491 struct scic_sds_remote_device *sci_dev,
492 struct scic_sds_request *io_request);
88f3b62a
DW
493
494enum sci_status scic_sds_remote_device_suspend(
495 struct scic_sds_remote_device *sci_dev,
496 u32 suspend_type);
497
88f3b62a
DW
498void scic_sds_remote_device_post_request(
499 struct scic_sds_remote_device *sci_dev,
500 u32 request);
501
88f3b62a 502#define scic_sds_remote_device_is_atapi(sci_dev) false
88f3b62a 503
6f231dda 504#endif /* !defined(_ISCI_REMOTE_DEVICE_H_) */
This page took 0.068962 seconds and 5 git commands to generate.