isci: unify request data structures
[pandora-kernel.git] / drivers / scsi / isci / host.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
57 #if !defined(_SCI_HOST_H_)
58 #define _SCI_HOST_H_
59
60 #include "phy.h"
61 #include "scic_sds_controller.h"
62 #include "timers.h"
63 #include "remote_device.h"
64
65 #define DRV_NAME "isci"
66 #define SCI_PCI_BAR_COUNT 2
67 #define SCI_NUM_MSI_X_INT 2
68 #define SCI_SMU_BAR       0
69 #define SCI_SMU_BAR_SIZE  (16*1024)
70 #define SCI_SCU_BAR       1
71 #define SCI_SCU_BAR_SIZE  (4*1024*1024)
72 #define SCI_IO_SPACE_BAR0 2
73 #define SCI_IO_SPACE_BAR1 3
74 #define ISCI_CAN_QUEUE_VAL 250 /* < SCI_MAX_IO_REQUESTS ? */
75 #define SCIC_CONTROLLER_STOP_TIMEOUT 5000
76
77 struct isci_host {
78         struct scic_sds_controller sci;
79         union scic_oem_parameters oem_parameters;
80
81         int id; /* unique within a given pci device */
82         struct list_head timers;
83         void *core_ctrl_memory;
84         struct dma_pool *dma_pool;
85         struct isci_phy phys[SCI_MAX_PHYS];
86         struct isci_port ports[SCI_MAX_PORTS + 1]; /* includes dummy port */
87         struct sas_ha_struct sas_ha;
88
89         int can_queue;
90         spinlock_t queue_lock;
91         spinlock_t state_lock;
92
93         struct pci_dev *pdev;
94
95         enum isci_status status;
96         #define IHOST_START_PENDING 0
97         #define IHOST_STOP_PENDING 1
98         unsigned long flags;
99         wait_queue_head_t eventq;
100         struct Scsi_Host *shost;
101         struct tasklet_struct completion_tasklet;
102         struct list_head requests_to_complete;
103         struct list_head requests_to_errorback;
104         spinlock_t scic_lock;
105
106         struct isci_remote_device devices[SCI_MAX_REMOTE_DEVICES];
107 };
108
109 /**
110  * struct isci_pci_info - This class represents the pci function containing the
111  *    controllers. Depending on PCI SKU, there could be up to 2 controllers in
112  *    the PCI function.
113  */
114 #define SCI_MAX_MSIX_INT (SCI_NUM_MSI_X_INT*SCI_MAX_CONTROLLERS)
115
116 struct isci_pci_info {
117         struct msix_entry msix_entries[SCI_MAX_MSIX_INT];
118         struct isci_host *hosts[SCI_MAX_CONTROLLERS];
119         struct isci_orom *orom;
120 };
121
122 static inline struct isci_pci_info *to_pci_info(struct pci_dev *pdev)
123 {
124         return pci_get_drvdata(pdev);
125 }
126
127 #define for_each_isci_host(id, ihost, pdev) \
128         for (id = 0, ihost = to_pci_info(pdev)->hosts[id]; \
129              id < ARRAY_SIZE(to_pci_info(pdev)->hosts) && ihost; \
130              ihost = to_pci_info(pdev)->hosts[++id])
131
132 static inline
133 enum isci_status isci_host_get_state(
134         struct isci_host *isci_host)
135 {
136         return isci_host->status;
137 }
138
139
140 static inline void isci_host_change_state(
141         struct isci_host *isci_host,
142         enum isci_status status)
143 {
144         unsigned long flags;
145
146         dev_dbg(&isci_host->pdev->dev,
147                 "%s: isci_host = %p, state = 0x%x",
148                 __func__,
149                 isci_host,
150                 status);
151         spin_lock_irqsave(&isci_host->state_lock, flags);
152         isci_host->status = status;
153         spin_unlock_irqrestore(&isci_host->state_lock, flags);
154
155 }
156
157 static inline int isci_host_can_queue(
158         struct isci_host *isci_host,
159         int num)
160 {
161         int ret = 0;
162         unsigned long flags;
163
164         spin_lock_irqsave(&isci_host->queue_lock, flags);
165         if ((isci_host->can_queue - num) < 0) {
166                 dev_dbg(&isci_host->pdev->dev,
167                         "%s: isci_host->can_queue = %d\n",
168                         __func__,
169                         isci_host->can_queue);
170                 ret = -SAS_QUEUE_FULL;
171
172         } else
173                 isci_host->can_queue -= num;
174
175         spin_unlock_irqrestore(&isci_host->queue_lock, flags);
176
177         return ret;
178 }
179
180 static inline void isci_host_can_dequeue(
181         struct isci_host *isci_host,
182         int num)
183 {
184         unsigned long flags;
185
186         spin_lock_irqsave(&isci_host->queue_lock, flags);
187         isci_host->can_queue += num;
188         spin_unlock_irqrestore(&isci_host->queue_lock, flags);
189 }
190
191 static inline void wait_for_start(struct isci_host *ihost)
192 {
193         wait_event(ihost->eventq, !test_bit(IHOST_START_PENDING, &ihost->flags));
194 }
195
196 static inline void wait_for_stop(struct isci_host *ihost)
197 {
198         wait_event(ihost->eventq, !test_bit(IHOST_STOP_PENDING, &ihost->flags));
199 }
200
201 static inline void wait_for_device_start(struct isci_host *ihost, struct isci_remote_device *idev)
202 {
203         wait_event(ihost->eventq, !test_bit(IDEV_START_PENDING, &idev->flags));
204 }
205
206 static inline void wait_for_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
207 {
208         wait_event(ihost->eventq, !test_bit(IDEV_STOP_PENDING, &idev->flags));
209 }
210
211 static inline struct isci_host *dev_to_ihost(struct domain_device *dev)
212 {
213         return dev->port->ha->lldd_ha;
214 }
215
216 static inline struct isci_host *scic_to_ihost(struct scic_sds_controller *scic)
217 {
218         /* XXX delete after merging scic_sds_contoller and isci_host */
219         struct isci_host *ihost = container_of(scic, typeof(*ihost), sci);
220
221         return ihost;
222 }
223
224 /**
225  * isci_host_scan_finished() -
226  *
227  * This function is one of the SCSI Host Template functions. The SCSI midlayer
228  * calls this function during a target scan, approx. once every 10 millisecs.
229  */
230 int isci_host_scan_finished(
231         struct Scsi_Host *,
232         unsigned long);
233
234
235 /**
236  * isci_host_scan_start() -
237  *
238  * This function is one of the SCSI Host Template function, called by the SCSI
239  * mid layer berfore a target scan begins. The core library controller start
240  * routine is called from here.
241  */
242 void isci_host_scan_start(
243         struct Scsi_Host *);
244
245 /**
246  * isci_host_start_complete() -
247  *
248  * This function is called by the core library, through the ISCI Module, to
249  * indicate controller start status.
250  */
251 void isci_host_start_complete(
252         struct isci_host *,
253         enum sci_status);
254
255 void isci_host_stop_complete(
256         struct isci_host *isci_host,
257         enum sci_status completion_status);
258
259 int isci_host_init(struct isci_host *);
260
261 void isci_host_init_controller_names(
262         struct isci_host *isci_host,
263         unsigned int controller_idx);
264
265 void isci_host_deinit(
266         struct isci_host *);
267
268 void isci_host_port_link_up(
269         struct isci_host *,
270         struct scic_sds_port *,
271         struct scic_sds_phy *);
272 int isci_host_dev_found(struct domain_device *);
273
274 void isci_host_remote_device_start_complete(
275         struct isci_host *,
276         struct isci_remote_device *,
277         enum sci_status);
278
279 #endif /* !defined(_SCI_HOST_H_) */