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#ifndef __ISCI_H__
57#define __ISCI_H__
58
59#include <linux/interrupt.h>
60#include <linux/types.h>
61
62#define DRV_NAME "isci"
63#define SCI_PCI_BAR_COUNT 2
64#define SCI_NUM_MSI_X_INT 2
65#define SCI_SMU_BAR       0
66#define SCI_SMU_BAR_SIZE  (16*1024)
67#define SCI_SCU_BAR       1
68#define SCI_SCU_BAR_SIZE  (4*1024*1024)
69#define SCI_IO_SPACE_BAR0 2
70#define SCI_IO_SPACE_BAR1 3
71#define ISCI_CAN_QUEUE_VAL 250 /* < SCI_MAX_IO_REQUESTS ? */
72#define SCIC_CONTROLLER_STOP_TIMEOUT 5000
73
74#define SCI_CONTROLLER_INVALID_IO_TAG 0xFFFF
75
76#define SCI_MAX_PHYS  (4UL)
77#define SCI_MAX_PORTS SCI_MAX_PHYS
78#define SCI_MAX_SMP_PHYS  (384) /* not silicon constrained */
79#define SCI_MAX_REMOTE_DEVICES (256UL)
80#define SCI_MAX_IO_REQUESTS (256UL)
81#define SCI_MAX_SEQ (16)
82#define SCI_MAX_MSIX_MESSAGES  (2)
83#define SCI_MAX_SCATTER_GATHER_ELEMENTS 130 /* not silicon constrained */
84#define SCI_MAX_CONTROLLERS 2
85#define SCI_MAX_DOMAINS  SCI_MAX_PORTS
86
87#define SCU_MAX_CRITICAL_NOTIFICATIONS    (384)
88#define SCU_MAX_EVENTS_SHIFT		  (7)
89#define SCU_MAX_EVENTS                    (1 << SCU_MAX_EVENTS_SHIFT)
90#define SCU_MAX_UNSOLICITED_FRAMES        (128)
91#define SCU_MAX_COMPLETION_QUEUE_SCRATCH  (128)
92#define SCU_MAX_COMPLETION_QUEUE_ENTRIES  (SCU_MAX_CRITICAL_NOTIFICATIONS \
93					   + SCU_MAX_EVENTS \
94					   + SCU_MAX_UNSOLICITED_FRAMES	\
95					   + SCI_MAX_IO_REQUESTS \
96					   + SCU_MAX_COMPLETION_QUEUE_SCRATCH)
97#define SCU_MAX_COMPLETION_QUEUE_SHIFT	  (ilog2(SCU_MAX_COMPLETION_QUEUE_ENTRIES))
98
99#define SCU_ABSOLUTE_MAX_UNSOLICITED_FRAMES (4096)
100#define SCU_UNSOLICITED_FRAME_BUFFER_SIZE   (1024U)
101#define SCU_INVALID_FRAME_INDEX             (0xFFFF)
102
103#define SCU_IO_REQUEST_MAX_SGE_SIZE         (0x00FFFFFF)
104#define SCU_IO_REQUEST_MAX_TRANSFER_LENGTH  (0x00FFFFFF)
105
106static inline void check_sizes(void)
107{
108	BUILD_BUG_ON_NOT_POWER_OF_2(SCU_MAX_EVENTS);
109	BUILD_BUG_ON(SCU_MAX_UNSOLICITED_FRAMES <= 8);
110	BUILD_BUG_ON_NOT_POWER_OF_2(SCU_MAX_UNSOLICITED_FRAMES);
111	BUILD_BUG_ON_NOT_POWER_OF_2(SCU_MAX_COMPLETION_QUEUE_ENTRIES);
112	BUILD_BUG_ON(SCU_MAX_UNSOLICITED_FRAMES > SCU_ABSOLUTE_MAX_UNSOLICITED_FRAMES);
113	BUILD_BUG_ON_NOT_POWER_OF_2(SCI_MAX_IO_REQUESTS);
114	BUILD_BUG_ON_NOT_POWER_OF_2(SCI_MAX_SEQ);
115}
116
117/**
118 * enum sci_status - This is the general return status enumeration for non-IO,
119 *    non-task management related SCI interface methods.
120 *
121 *
122 */
123enum sci_status {
124	/**
125	 * This member indicates successful completion.
126	 */
127	SCI_SUCCESS = 0,
128
129	/**
130	 * This value indicates that the calling method completed successfully,
131	 * but that the IO may have completed before having it's start method
132	 * invoked.  This occurs during SAT translation for requests that do
133	 * not require an IO to the target or for any other requests that may
134	 * be completed without having to submit IO.
135	 */
136	SCI_SUCCESS_IO_COMPLETE_BEFORE_START,
137
138	/**
139	 *  This Value indicates that the SCU hardware returned an early response
140	 *  because the io request specified more data than is returned by the
141	 *  target device (mode pages, inquiry data, etc.). The completion routine
142	 *  will handle this case to get the actual number of bytes transferred.
143	 */
144	SCI_SUCCESS_IO_DONE_EARLY,
145
146	/**
147	 * This member indicates that the object for which a state change is
148	 * being requested is already in said state.
149	 */
150	SCI_WARNING_ALREADY_IN_STATE,
151
152	/**
153	 * This member indicates interrupt coalescence timer may cause SAS
154	 * specification compliance issues (i.e. SMP target mode response
155	 * frames must be returned within 1.9 milliseconds).
156	 */
157	SCI_WARNING_TIMER_CONFLICT,
158
159	/**
160	 * This field indicates a sequence of action is not completed yet. Mostly,
161	 * this status is used when multiple ATA commands are needed in a SATI translation.
162	 */
163	SCI_WARNING_SEQUENCE_INCOMPLETE,
164
165	/**
166	 * This member indicates that there was a general failure.
167	 */
168	SCI_FAILURE,
169
170	/**
171	 * This member indicates that the SCI implementation is unable to complete
172	 * an operation due to a critical flaw the prevents any further operation
173	 * (i.e. an invalid pointer).
174	 */
175	SCI_FATAL_ERROR,
176
177	/**
178	 * This member indicates the calling function failed, because the state
179	 * of the controller is in a state that prevents successful completion.
180	 */
181	SCI_FAILURE_INVALID_STATE,
182
183	/**
184	 * This member indicates the calling function failed, because there is
185	 * insufficient resources/memory to complete the request.
186	 */
187	SCI_FAILURE_INSUFFICIENT_RESOURCES,
188
189	/**
190	 * This member indicates the calling function failed, because the
191	 * controller object required for the operation can't be located.
192	 */
193	SCI_FAILURE_CONTROLLER_NOT_FOUND,
194
195	/**
196	 * This member indicates the calling function failed, because the
197	 * discovered controller type is not supported by the library.
198	 */
199	SCI_FAILURE_UNSUPPORTED_CONTROLLER_TYPE,
200
201	/**
202	 * This member indicates the calling function failed, because the
203	 * requested initialization data version isn't supported.
204	 */
205	SCI_FAILURE_UNSUPPORTED_INIT_DATA_VERSION,
206
207	/**
208	 * This member indicates the calling function failed, because the
209	 * requested configuration of SAS Phys into SAS Ports is not supported.
210	 */
211	SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION,
212
213	/**
214	 * This member indicates the calling function failed, because the
215	 * requested protocol is not supported by the remote device, port,
216	 * or controller.
217	 */
218	SCI_FAILURE_UNSUPPORTED_PROTOCOL,
219
220	/**
221	 * This member indicates the calling function failed, because the
222	 * requested information type is not supported by the SCI implementation.
223	 */
224	SCI_FAILURE_UNSUPPORTED_INFORMATION_TYPE,
225
226	/**
227	 * This member indicates the calling function failed, because the
228	 * device already exists.
229	 */
230	SCI_FAILURE_DEVICE_EXISTS,
231
232	/**
233	 * This member indicates the calling function failed, because adding
234	 * a phy to the object is not possible.
235	 */
236	SCI_FAILURE_ADDING_PHY_UNSUPPORTED,
237
238	/**
239	 * This member indicates the calling function failed, because the
240	 * requested information type is not supported by the SCI implementation.
241	 */
242	SCI_FAILURE_UNSUPPORTED_INFORMATION_FIELD,
243
244	/**
245	 * This member indicates the calling function failed, because the SCI
246	 * implementation does not support the supplied time limit.
247	 */
248	SCI_FAILURE_UNSUPPORTED_TIME_LIMIT,
249
250	/**
251	 * This member indicates the calling method failed, because the SCI
252	 * implementation does not contain the specified Phy.
253	 */
254	SCI_FAILURE_INVALID_PHY,
255
256	/**
257	 * This member indicates the calling method failed, because the SCI
258	 * implementation does not contain the specified Port.
259	 */
260	SCI_FAILURE_INVALID_PORT,
261
262	/**
263	 * This member indicates the calling method was partly successful
264	 * The port was reset but not all phys in port are operational
265	 */
266	SCI_FAILURE_RESET_PORT_PARTIAL_SUCCESS,
267
268	/**
269	 * This member indicates that calling method failed
270	 * The port reset did not complete because none of the phys are operational
271	 */
272	SCI_FAILURE_RESET_PORT_FAILURE,
273
274	/**
275	 * This member indicates the calling method failed, because the SCI
276	 * implementation does not contain the specified remote device.
277	 */
278	SCI_FAILURE_INVALID_REMOTE_DEVICE,
279
280	/**
281	 * This member indicates the calling method failed, because the remote
282	 * device is in a bad state and requires a reset.
283	 */
284	SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED,
285
286	/**
287	 * This member indicates the calling method failed, because the SCI
288	 * implementation does not contain or support the specified IO tag.
289	 */
290	SCI_FAILURE_INVALID_IO_TAG,
291
292	/**
293	 * This member indicates that the operation failed and the user should
294	 * check the response data associated with the IO.
295	 */
296	SCI_FAILURE_IO_RESPONSE_VALID,
297
298	/**
299	 * This member indicates that the operation failed, the failure is
300	 * controller implementation specific, and the response data associated
301	 * with the request is not valid.  You can query for the controller
302	 * specific error information via sci_controller_get_request_status()
303	 */
304	SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR,
305
306	/**
307	 * This member indicated that the operation failed because the
308	 * user requested this IO to be terminated.
309	 */
310	SCI_FAILURE_IO_TERMINATED,
311
312	/**
313	 * This member indicates that the operation failed and the associated
314	 * request requires a SCSI abort task to be sent to the target.
315	 */
316	SCI_FAILURE_IO_REQUIRES_SCSI_ABORT,
317
318	/**
319	 * This member indicates that the operation failed because the supplied
320	 * device could not be located.
321	 */
322	SCI_FAILURE_DEVICE_NOT_FOUND,
323
324	/**
325	 * This member indicates that the operation failed because the
326	 * objects association is required and is not correctly set.
327	 */
328	SCI_FAILURE_INVALID_ASSOCIATION,
329
330	/**
331	 * This member indicates that the operation failed, because a timeout
332	 * occurred.
333	 */
334	SCI_FAILURE_TIMEOUT,
335
336	/**
337	 * This member indicates that the operation failed, because the user
338	 * specified a value that is either invalid or not supported.
339	 */
340	SCI_FAILURE_INVALID_PARAMETER_VALUE,
341
342	/**
343	 * This value indicates that the operation failed, because the number
344	 * of messages (MSI-X) is not supported.
345	 */
346	SCI_FAILURE_UNSUPPORTED_MESSAGE_COUNT,
347
348	/**
349	 * This value indicates that the method failed due to a lack of
350	 * available NCQ tags.
351	 */
352	SCI_FAILURE_NO_NCQ_TAG_AVAILABLE,
353
354	/**
355	 * This value indicates that a protocol violation has occurred on the
356	 * link.
357	 */
358	SCI_FAILURE_PROTOCOL_VIOLATION,
359
360	/**
361	 * This value indicates a failure condition that retry may help to clear.
362	 */
363	SCI_FAILURE_RETRY_REQUIRED,
364
365	/**
366	 * This field indicates the retry limit was reached when a retry is attempted
367	 */
368	SCI_FAILURE_RETRY_LIMIT_REACHED,
369
370	/**
371	 * This member indicates the calling method was partly successful.
372	 * Mostly, this status is used when a LUN_RESET issued to an expander attached
373	 * STP device in READY NCQ substate needs to have RNC suspended/resumed
374	 * before posting TC.
375	 */
376	SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS,
377
378	/**
379	 * This field indicates an illegal phy connection based on the routing attribute
380	 * of both expander phy attached to each other.
381	 */
382	SCI_FAILURE_ILLEGAL_ROUTING_ATTRIBUTE_CONFIGURATION,
383
384	/**
385	 * This field indicates a CONFIG ROUTE INFO command has a response with function result
386	 * INDEX DOES NOT EXIST, usually means exceeding max route index.
387	 */
388	SCI_FAILURE_EXCEED_MAX_ROUTE_INDEX,
389
390	/**
391	 * This value indicates that an unsupported PCI device ID has been
392	 * specified.  This indicates that attempts to invoke
393	 * sci_library_allocate_controller() will fail.
394	 */
395	SCI_FAILURE_UNSUPPORTED_PCI_DEVICE_ID
396
397};
398
399/**
400 * enum sci_io_status - This enumeration depicts all of the possible IO
401 *    completion status values.  Each value in this enumeration maps directly
402 *    to a value in the enum sci_status enumeration.  Please refer to that
403 *    enumeration for detailed comments concerning what the status represents.
404 *
405 * Add the API to retrieve the SCU status from the core. Check to see that the
406 * following status are properly handled: - SCI_IO_FAILURE_UNSUPPORTED_PROTOCOL
407 * - SCI_IO_FAILURE_INVALID_IO_TAG
408 */
409enum sci_io_status {
410	SCI_IO_SUCCESS                         = SCI_SUCCESS,
411	SCI_IO_FAILURE                         = SCI_FAILURE,
412	SCI_IO_SUCCESS_COMPLETE_BEFORE_START   = SCI_SUCCESS_IO_COMPLETE_BEFORE_START,
413	SCI_IO_SUCCESS_IO_DONE_EARLY           = SCI_SUCCESS_IO_DONE_EARLY,
414	SCI_IO_FAILURE_INVALID_STATE           = SCI_FAILURE_INVALID_STATE,
415	SCI_IO_FAILURE_INSUFFICIENT_RESOURCES  = SCI_FAILURE_INSUFFICIENT_RESOURCES,
416	SCI_IO_FAILURE_UNSUPPORTED_PROTOCOL    = SCI_FAILURE_UNSUPPORTED_PROTOCOL,
417	SCI_IO_FAILURE_RESPONSE_VALID          = SCI_FAILURE_IO_RESPONSE_VALID,
418	SCI_IO_FAILURE_CONTROLLER_SPECIFIC_ERR = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR,
419	SCI_IO_FAILURE_TERMINATED              = SCI_FAILURE_IO_TERMINATED,
420	SCI_IO_FAILURE_REQUIRES_SCSI_ABORT     = SCI_FAILURE_IO_REQUIRES_SCSI_ABORT,
421	SCI_IO_FAILURE_INVALID_PARAMETER_VALUE = SCI_FAILURE_INVALID_PARAMETER_VALUE,
422	SCI_IO_FAILURE_NO_NCQ_TAG_AVAILABLE    = SCI_FAILURE_NO_NCQ_TAG_AVAILABLE,
423	SCI_IO_FAILURE_PROTOCOL_VIOLATION      = SCI_FAILURE_PROTOCOL_VIOLATION,
424
425	SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED,
426
427	SCI_IO_FAILURE_RETRY_REQUIRED      = SCI_FAILURE_RETRY_REQUIRED,
428	SCI_IO_FAILURE_RETRY_LIMIT_REACHED = SCI_FAILURE_RETRY_LIMIT_REACHED,
429	SCI_IO_FAILURE_INVALID_REMOTE_DEVICE = SCI_FAILURE_INVALID_REMOTE_DEVICE
430};
431
432/**
433 * enum sci_task_status - This enumeration depicts all of the possible task
434 *    completion status values.  Each value in this enumeration maps directly
435 *    to a value in the enum sci_status enumeration.  Please refer to that
436 *    enumeration for detailed comments concerning what the status represents.
437 *
438 * Check to see that the following status are properly handled:
439 */
440enum sci_task_status {
441	SCI_TASK_SUCCESS                         = SCI_SUCCESS,
442	SCI_TASK_FAILURE                         = SCI_FAILURE,
443	SCI_TASK_FAILURE_INVALID_STATE           = SCI_FAILURE_INVALID_STATE,
444	SCI_TASK_FAILURE_INSUFFICIENT_RESOURCES  = SCI_FAILURE_INSUFFICIENT_RESOURCES,
445	SCI_TASK_FAILURE_UNSUPPORTED_PROTOCOL    = SCI_FAILURE_UNSUPPORTED_PROTOCOL,
446	SCI_TASK_FAILURE_INVALID_TAG             = SCI_FAILURE_INVALID_IO_TAG,
447	SCI_TASK_FAILURE_RESPONSE_VALID          = SCI_FAILURE_IO_RESPONSE_VALID,
448	SCI_TASK_FAILURE_CONTROLLER_SPECIFIC_ERR = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR,
449	SCI_TASK_FAILURE_TERMINATED              = SCI_FAILURE_IO_TERMINATED,
450	SCI_TASK_FAILURE_INVALID_PARAMETER_VALUE = SCI_FAILURE_INVALID_PARAMETER_VALUE,
451
452	SCI_TASK_FAILURE_REMOTE_DEVICE_RESET_REQUIRED = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED,
453	SCI_TASK_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS = SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS
454
455};
456
457/**
458 * sci_swab32_cpy - convert between scsi and scu-hardware byte format
459 * @dest: receive the 4-byte endian swapped version of src
460 * @src: word aligned source buffer
461 *
462 * scu hardware handles SSP/SMP control, response, and unidentified
463 * frames in "big endian dword" order.  Regardless of host endian this
464 * is always a swab32()-per-dword conversion of the standard definition,
465 * i.e. single byte fields swapped and multi-byte fields in little-
466 * endian
467 */
468static inline void sci_swab32_cpy(void *_dest, void *_src, ssize_t word_cnt)
469{
470	u32 *dest = _dest, *src = _src;
471
472	while (--word_cnt >= 0)
473		dest[word_cnt] = swab32(src[word_cnt]);
474}
475
476extern unsigned char no_outbound_task_to;
477extern u16 ssp_max_occ_to;
478extern u16 stp_max_occ_to;
479extern u16 ssp_inactive_to;
480extern u16 stp_inactive_to;
481extern unsigned char phy_gen;
482extern unsigned char max_concurr_spinup;
483extern uint cable_selection_override;
484
485irqreturn_t isci_msix_isr(int vec, void *data);
486irqreturn_t isci_intx_isr(int vec, void *data);
487irqreturn_t isci_error_isr(int vec, void *data);
488
489/*
490 * Each timer is associated with a cancellation flag that is set when
491 * del_timer() is called and checked in the timer callback function. This
492 * is needed since del_timer_sync() cannot be called with sci_lock held.
493 * For deinit however, del_timer_sync() is used without holding the lock.
494 */
495struct sci_timer {
496	struct timer_list	timer;
497	bool			cancel;
498};
499
500static inline
501void sci_init_timer(struct sci_timer *tmr, void (*fn)(struct timer_list *t))
502{
503	tmr->cancel = false;
504	timer_setup(&tmr->timer, fn, 0);
505}
506
507static inline void sci_mod_timer(struct sci_timer *tmr, unsigned long msec)
508{
509	tmr->cancel = false;
510	mod_timer(&tmr->timer, jiffies + msecs_to_jiffies(msec));
511}
512
513static inline void sci_del_timer(struct sci_timer *tmr)
514{
515	tmr->cancel = true;
516	del_timer(&tmr->timer);
517}
518
519struct sci_base_state_machine {
520	const struct sci_base_state *state_table;
521	u32 initial_state_id;
522	u32 current_state_id;
523	u32 previous_state_id;
524};
525
526typedef void (*sci_state_transition_t)(struct sci_base_state_machine *sm);
527
528struct sci_base_state {
529	sci_state_transition_t enter_state;	/* Called on state entry */
530	sci_state_transition_t exit_state;	/* Called on state exit */
531};
532
533extern void sci_init_sm(struct sci_base_state_machine *sm,
534			const struct sci_base_state *state_table,
535			u32 initial_state);
536extern void sci_change_state(struct sci_base_state_machine *sm, u32 next_state);
537#endif  /* __ISCI_H__ */
538