]> Pileus Git - ~andy/linux/blob - drivers/scsi/isci/host.c
55bfa3dbfb8f4687cb61cb2fb8ae0bb28e7b5984
[~andy/linux] / drivers / scsi / isci / host.c
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 #include "isci.h"
57 #include "scic_io_request.h"
58 #include "scic_port.h"
59 #include "port.h"
60 #include "request.h"
61 #include "host.h"
62 #include "probe_roms.h"
63 #include "scic_sds_controller.h"
64
65 irqreturn_t isci_msix_isr(int vec, void *data)
66 {
67         struct isci_host *ihost = data;
68         struct scic_sds_controller *scic = ihost->core_controller;
69
70         if (scic_sds_controller_isr(scic))
71                 tasklet_schedule(&ihost->completion_tasklet);
72
73         return IRQ_HANDLED;
74 }
75
76 irqreturn_t isci_intx_isr(int vec, void *data)
77 {
78         irqreturn_t ret = IRQ_NONE;
79         struct isci_host *ihost = data;
80         struct scic_sds_controller *scic = ihost->core_controller;
81
82         if (scic_sds_controller_isr(scic)) {
83                 writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
84                 tasklet_schedule(&ihost->completion_tasklet);
85                 ret = IRQ_HANDLED;
86         } else if (scic_sds_controller_error_isr(scic)) {
87                 spin_lock(&ihost->scic_lock);
88                 scic_sds_controller_error_handler(scic);
89                 spin_unlock(&ihost->scic_lock);
90                 ret = IRQ_HANDLED;
91         }
92
93         return ret;
94 }
95
96 irqreturn_t isci_error_isr(int vec, void *data)
97 {
98         struct isci_host *ihost = data;
99         struct scic_sds_controller *scic = ihost->core_controller;
100
101         if (scic_sds_controller_error_isr(scic))
102                 scic_sds_controller_error_handler(scic);
103
104         return IRQ_HANDLED;
105 }
106
107 /**
108  * isci_host_start_complete() - This function is called by the core library,
109  *    through the ISCI Module, to indicate controller start status.
110  * @isci_host: This parameter specifies the ISCI host object
111  * @completion_status: This parameter specifies the completion status from the
112  *    core library.
113  *
114  */
115 void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
116 {
117         if (completion_status != SCI_SUCCESS)
118                 dev_info(&ihost->pdev->dev,
119                         "controller start timed out, continuing...\n");
120         isci_host_change_state(ihost, isci_ready);
121         clear_bit(IHOST_START_PENDING, &ihost->flags);
122         wake_up(&ihost->eventq);
123 }
124
125 int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
126 {
127         struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
128
129         if (test_bit(IHOST_START_PENDING, &ihost->flags))
130                 return 0;
131
132         /* todo: use sas_flush_discovery once it is upstream */
133         scsi_flush_work(shost);
134
135         scsi_flush_work(shost);
136
137         dev_dbg(&ihost->pdev->dev,
138                 "%s: ihost->status = %d, time = %ld\n",
139                  __func__, isci_host_get_state(ihost), time);
140
141         return 1;
142
143 }
144
145 void isci_host_scan_start(struct Scsi_Host *shost)
146 {
147         struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
148         struct scic_sds_controller *scic = ihost->core_controller;
149         unsigned long tmo = scic_controller_get_suggested_start_timeout(scic);
150
151         set_bit(IHOST_START_PENDING, &ihost->flags);
152
153         spin_lock_irq(&ihost->scic_lock);
154         scic_controller_start(scic, tmo);
155         scic_controller_enable_interrupts(scic);
156         spin_unlock_irq(&ihost->scic_lock);
157 }
158
159 void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
160 {
161         isci_host_change_state(ihost, isci_stopped);
162         scic_controller_disable_interrupts(ihost->core_controller);
163         clear_bit(IHOST_STOP_PENDING, &ihost->flags);
164         wake_up(&ihost->eventq);
165 }
166
167 /**
168  * isci_host_completion_routine() - This function is the delayed service
169  *    routine that calls the sci core library's completion handler. It's
170  *    scheduled as a tasklet from the interrupt service routine when interrupts
171  *    in use, or set as the timeout function in polled mode.
172  * @data: This parameter specifies the ISCI host object
173  *
174  */
175 static void isci_host_completion_routine(unsigned long data)
176 {
177         struct isci_host *isci_host = (struct isci_host *)data;
178         struct list_head    completed_request_list;
179         struct list_head    errored_request_list;
180         struct list_head    *current_position;
181         struct list_head    *next_position;
182         struct isci_request *request;
183         struct isci_request *next_request;
184         struct sas_task     *task;
185
186         INIT_LIST_HEAD(&completed_request_list);
187         INIT_LIST_HEAD(&errored_request_list);
188
189         spin_lock_irq(&isci_host->scic_lock);
190
191         scic_sds_controller_completion_handler(isci_host->core_controller);
192
193         /* Take the lists of completed I/Os from the host. */
194
195         list_splice_init(&isci_host->requests_to_complete,
196                          &completed_request_list);
197
198         /* Take the list of errored I/Os from the host. */
199         list_splice_init(&isci_host->requests_to_errorback,
200                          &errored_request_list);
201
202         spin_unlock_irq(&isci_host->scic_lock);
203
204         /* Process any completions in the lists. */
205         list_for_each_safe(current_position, next_position,
206                            &completed_request_list) {
207
208                 request = list_entry(current_position, struct isci_request,
209                                      completed_node);
210                 task = isci_request_access_task(request);
211
212                 /* Normal notification (task_done) */
213                 dev_dbg(&isci_host->pdev->dev,
214                         "%s: Normal - request/task = %p/%p\n",
215                         __func__,
216                         request,
217                         task);
218
219                 /* Return the task to libsas */
220                 if (task != NULL) {
221
222                         task->lldd_task = NULL;
223                         if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
224
225                                 /* If the task is already in the abort path,
226                                 * the task_done callback cannot be called.
227                                 */
228                                 task->task_done(task);
229                         }
230                 }
231                 /* Free the request object. */
232                 isci_request_free(isci_host, request);
233         }
234         list_for_each_entry_safe(request, next_request, &errored_request_list,
235                                  completed_node) {
236
237                 task = isci_request_access_task(request);
238
239                 /* Use sas_task_abort */
240                 dev_warn(&isci_host->pdev->dev,
241                          "%s: Error - request/task = %p/%p\n",
242                          __func__,
243                          request,
244                          task);
245
246                 if (task != NULL) {
247
248                         /* Put the task into the abort path if it's not there
249                          * already.
250                          */
251                         if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
252                                 sas_task_abort(task);
253
254                 } else {
255                         /* This is a case where the request has completed with a
256                          * status such that it needed further target servicing,
257                          * but the sas_task reference has already been removed
258                          * from the request.  Since it was errored, it was not
259                          * being aborted, so there is nothing to do except free
260                          * it.
261                          */
262
263                         spin_lock_irq(&isci_host->scic_lock);
264                         /* Remove the request from the remote device's list
265                         * of pending requests.
266                         */
267                         list_del_init(&request->dev_node);
268                         spin_unlock_irq(&isci_host->scic_lock);
269
270                         /* Free the request object. */
271                         isci_request_free(isci_host, request);
272                 }
273         }
274
275 }
276
277 void isci_host_deinit(struct isci_host *ihost)
278 {
279         struct scic_sds_controller *scic = ihost->core_controller;
280         int i;
281
282         isci_host_change_state(ihost, isci_stopping);
283         for (i = 0; i < SCI_MAX_PORTS; i++) {
284                 struct isci_port *port = &ihost->isci_ports[i];
285                 struct isci_remote_device *idev, *d;
286
287                 list_for_each_entry_safe(idev, d, &port->remote_dev_list, node) {
288                         isci_remote_device_change_state(idev, isci_stopping);
289                         isci_remote_device_stop(ihost, idev);
290                 }
291         }
292
293         set_bit(IHOST_STOP_PENDING, &ihost->flags);
294
295         spin_lock_irq(&ihost->scic_lock);
296         scic_controller_stop(scic, SCIC_CONTROLLER_STOP_TIMEOUT);
297         spin_unlock_irq(&ihost->scic_lock);
298
299         wait_for_stop(ihost);
300         scic_controller_reset(scic);
301         isci_timer_list_destroy(ihost);
302 }
303
304 static void __iomem *scu_base(struct isci_host *isci_host)
305 {
306         struct pci_dev *pdev = isci_host->pdev;
307         int id = isci_host->id;
308
309         return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
310 }
311
312 static void __iomem *smu_base(struct isci_host *isci_host)
313 {
314         struct pci_dev *pdev = isci_host->pdev;
315         int id = isci_host->id;
316
317         return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
318 }
319
320 static void isci_user_parameters_get(
321                 struct isci_host *isci_host,
322                 union scic_user_parameters *scic_user_params)
323 {
324         struct scic_sds_user_parameters *u = &scic_user_params->sds1;
325         int i;
326
327         for (i = 0; i < SCI_MAX_PHYS; i++) {
328                 struct sci_phy_user_params *u_phy = &u->phys[i];
329
330                 u_phy->max_speed_generation = phy_gen;
331
332                 /* we are not exporting these for now */
333                 u_phy->align_insertion_frequency = 0x7f;
334                 u_phy->in_connection_align_insertion_frequency = 0xff;
335                 u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
336         }
337
338         u->stp_inactivity_timeout = stp_inactive_to;
339         u->ssp_inactivity_timeout = ssp_inactive_to;
340         u->stp_max_occupancy_timeout = stp_max_occ_to;
341         u->ssp_max_occupancy_timeout = ssp_max_occ_to;
342         u->no_outbound_task_timeout = no_outbound_task_to;
343         u->max_number_concurrent_device_spin_up = max_concurr_spinup;
344 }
345
346 int isci_host_init(struct isci_host *isci_host)
347 {
348         int err = 0, i;
349         enum sci_status status;
350         struct scic_sds_controller *controller;
351         union scic_oem_parameters oem;
352         union scic_user_parameters scic_user_params;
353         struct isci_pci_info *pci_info = to_pci_info(isci_host->pdev);
354
355         isci_timer_list_construct(isci_host);
356
357         controller = scic_controller_alloc(&isci_host->pdev->dev);
358
359         if (!controller) {
360                 dev_err(&isci_host->pdev->dev,
361                         "%s: failed (%d)\n",
362                         __func__,
363                         err);
364                 return -ENOMEM;
365         }
366
367         isci_host->core_controller = controller;
368         controller->ihost = isci_host;
369         spin_lock_init(&isci_host->state_lock);
370         spin_lock_init(&isci_host->scic_lock);
371         spin_lock_init(&isci_host->queue_lock);
372         init_waitqueue_head(&isci_host->eventq);
373
374         isci_host_change_state(isci_host, isci_starting);
375         isci_host->can_queue = ISCI_CAN_QUEUE_VAL;
376
377         status = scic_controller_construct(controller, scu_base(isci_host),
378                                            smu_base(isci_host));
379
380         if (status != SCI_SUCCESS) {
381                 dev_err(&isci_host->pdev->dev,
382                         "%s: scic_controller_construct failed - status = %x\n",
383                         __func__,
384                         status);
385                 return -ENODEV;
386         }
387
388         isci_host->sas_ha.dev = &isci_host->pdev->dev;
389         isci_host->sas_ha.lldd_ha = isci_host;
390
391         /*
392          * grab initial values stored in the controller object for OEM and USER
393          * parameters
394          */
395         isci_user_parameters_get(isci_host, &scic_user_params);
396         status = scic_user_parameters_set(isci_host->core_controller,
397                                           &scic_user_params);
398         if (status != SCI_SUCCESS) {
399                 dev_warn(&isci_host->pdev->dev,
400                          "%s: scic_user_parameters_set failed\n",
401                          __func__);
402                 return -ENODEV;
403         }
404
405         scic_oem_parameters_get(controller, &oem);
406
407         /* grab any OEM parameters specified in orom */
408         if (pci_info->orom) {
409                 status = isci_parse_oem_parameters(&oem,
410                                                    pci_info->orom,
411                                                    isci_host->id);
412                 if (status != SCI_SUCCESS) {
413                         dev_warn(&isci_host->pdev->dev,
414                                  "parsing firmware oem parameters failed\n");
415                         return -EINVAL;
416                 }
417         }
418
419         status = scic_oem_parameters_set(isci_host->core_controller, &oem);
420         if (status != SCI_SUCCESS) {
421                 dev_warn(&isci_host->pdev->dev,
422                                 "%s: scic_oem_parameters_set failed\n",
423                                 __func__);
424                 return -ENODEV;
425         }
426
427         tasklet_init(&isci_host->completion_tasklet,
428                      isci_host_completion_routine, (unsigned long)isci_host);
429
430         INIT_LIST_HEAD(&isci_host->requests_to_complete);
431         INIT_LIST_HEAD(&isci_host->requests_to_errorback);
432
433         spin_lock_irq(&isci_host->scic_lock);
434         status = scic_controller_initialize(isci_host->core_controller);
435         spin_unlock_irq(&isci_host->scic_lock);
436         if (status != SCI_SUCCESS) {
437                 dev_warn(&isci_host->pdev->dev,
438                          "%s: scic_controller_initialize failed -"
439                          " status = 0x%x\n",
440                          __func__, status);
441                 return -ENODEV;
442         }
443
444         err = scic_controller_mem_init(isci_host->core_controller);
445         if (err)
446                 return err;
447
448         /*
449          * keep the pool alloc size around, will use it for a bounds checking
450          * when trying to convert virtual addresses to physical addresses
451          */
452         isci_host->dma_pool_alloc_size = sizeof(struct isci_request) +
453                                          scic_io_request_get_object_size();
454         isci_host->dma_pool = dmam_pool_create(DRV_NAME, &isci_host->pdev->dev,
455                                                isci_host->dma_pool_alloc_size,
456                                                SLAB_HWCACHE_ALIGN, 0);
457
458         if (!isci_host->dma_pool)
459                 return -ENOMEM;
460
461         for (i = 0; i < SCI_MAX_PORTS; i++)
462                 isci_port_init(&isci_host->isci_ports[i], isci_host, i);
463
464         for (i = 0; i < SCI_MAX_PHYS; i++)
465                 isci_phy_init(&isci_host->phys[i], isci_host, i);
466
467         for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
468                 struct isci_remote_device *idev = &isci_host->devices[i];
469
470                 INIT_LIST_HEAD(&idev->reqs_in_process);
471                 INIT_LIST_HEAD(&idev->node);
472                 spin_lock_init(&idev->state_lock);
473         }
474
475         return 0;
476 }