]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/scsi/isci/host.c
isci: kill isci_host list in favor of an array
[karo-tx-linux.git] / 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_remote_device.h"
59 #include "scic_port.h"
60
61 #include "port.h"
62 #include "request.h"
63 #include "host.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         struct pci_dev *pdev = data;
79         struct isci_host *ihost;
80         irqreturn_t ret = IRQ_NONE;
81         int i;
82
83         for_each_isci_host(i, ihost, pdev) {
84                 struct scic_sds_controller *scic = ihost->core_controller;
85
86                 if (scic_sds_controller_isr(scic)) {
87                         tasklet_schedule(&ihost->completion_tasklet);
88                         ret = IRQ_HANDLED;
89                 } else if (scic_sds_controller_error_isr(scic)) {
90                         spin_lock(&ihost->scic_lock);
91                         scic_sds_controller_error_handler(scic);
92                         spin_unlock(&ihost->scic_lock);
93                         ret = IRQ_HANDLED;
94                 }
95         }
96
97         return ret;
98 }
99
100 irqreturn_t isci_error_isr(int vec, void *data)
101 {
102         struct isci_host *ihost = data;
103         struct scic_sds_controller *scic = ihost->core_controller;
104
105         if (scic_sds_controller_error_isr(scic))
106                 scic_sds_controller_error_handler(scic);
107
108         return IRQ_HANDLED;
109 }
110
111 /**
112  * isci_host_start_complete() - This function is called by the core library,
113  *    through the ISCI Module, to indicate controller start status.
114  * @isci_host: This parameter specifies the ISCI host object
115  * @completion_status: This parameter specifies the completion status from the
116  *    core library.
117  *
118  */
119 void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
120 {
121         if (completion_status != SCI_SUCCESS)
122                 dev_info(&ihost->pdev->dev,
123                         "controller start timed out, continuing...\n");
124         isci_host_change_state(ihost, isci_ready);
125         clear_bit(IHOST_START_PENDING, &ihost->flags);
126         wake_up(&ihost->eventq);
127 }
128
129 int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
130 {
131         struct isci_host *ihost = isci_host_from_sas_ha(SHOST_TO_SAS_HA(shost));
132
133         if (test_bit(IHOST_START_PENDING, &ihost->flags))
134                 return 0;
135
136         /* todo: use sas_flush_discovery once it is upstream */
137         scsi_flush_work(shost);
138
139         scsi_flush_work(shost);
140
141         dev_dbg(&ihost->pdev->dev,
142                 "%s: ihost->status = %d, time = %ld\n",
143                  __func__, isci_host_get_state(ihost), time);
144
145         return 1;
146
147 }
148
149 void isci_host_scan_start(struct Scsi_Host *shost)
150 {
151         struct isci_host *ihost = isci_host_from_sas_ha(SHOST_TO_SAS_HA(shost));
152         struct scic_sds_controller *scic = ihost->core_controller;
153         unsigned long tmo = scic_controller_get_suggested_start_timeout(scic);
154
155         set_bit(IHOST_START_PENDING, &ihost->flags);
156
157         spin_lock_irq(&ihost->scic_lock);
158         scic_controller_start(scic, tmo);
159         scic_controller_enable_interrupts(scic);
160         spin_unlock_irq(&ihost->scic_lock);
161 }
162
163 void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
164 {
165         isci_host_change_state(ihost, isci_stopped);
166         scic_controller_disable_interrupts(ihost->core_controller);
167         clear_bit(IHOST_STOP_PENDING, &ihost->flags);
168         wake_up(&ihost->eventq);
169 }
170
171 static struct coherent_memory_info *isci_host_alloc_mdl_struct(
172         struct isci_host *isci_host,
173         u32 size)
174 {
175         struct coherent_memory_info *mdl_struct;
176         void *uncached_address = NULL;
177
178
179         mdl_struct = devm_kzalloc(&isci_host->pdev->dev,
180                                   sizeof(*mdl_struct),
181                                   GFP_KERNEL);
182         if (!mdl_struct)
183                 return NULL;
184
185         INIT_LIST_HEAD(&mdl_struct->node);
186
187         uncached_address = dmam_alloc_coherent(&isci_host->pdev->dev,
188                                                size,
189                                                &mdl_struct->dma_handle,
190                                                GFP_KERNEL);
191         if (!uncached_address)
192                 return NULL;
193
194         /* memset the whole memory area. */
195         memset((char *)uncached_address, 0, size);
196         mdl_struct->vaddr = uncached_address;
197         mdl_struct->size = (size_t)size;
198
199         return mdl_struct;
200 }
201
202 static void isci_host_build_mde(
203         struct sci_physical_memory_descriptor *mde_struct,
204         struct coherent_memory_info *mdl_struct)
205 {
206         unsigned long address = 0;
207         dma_addr_t dma_addr = 0;
208
209         address = (unsigned long)mdl_struct->vaddr;
210         dma_addr = mdl_struct->dma_handle;
211
212         /* to satisfy the alignment. */
213         if ((address % mde_struct->constant_memory_alignment) != 0) {
214                 int align_offset
215                         = (mde_struct->constant_memory_alignment
216                            - (address % mde_struct->constant_memory_alignment));
217                 address += align_offset;
218                 dma_addr += align_offset;
219         }
220
221         mde_struct->virtual_address = (void *)address;
222         mde_struct->physical_address = dma_addr;
223         mdl_struct->mde = mde_struct;
224 }
225
226 static int isci_host_mdl_allocate_coherent(
227         struct isci_host *isci_host)
228 {
229         struct sci_physical_memory_descriptor *current_mde;
230         struct coherent_memory_info *mdl_struct;
231         u32 size = 0;
232
233         struct sci_base_memory_descriptor_list *mdl_handle
234                 = sci_controller_get_memory_descriptor_list_handle(
235                 isci_host->core_controller);
236
237         sci_mdl_first_entry(mdl_handle);
238
239         current_mde = sci_mdl_get_current_entry(mdl_handle);
240
241         while (current_mde != NULL) {
242
243                 size = (current_mde->constant_memory_size
244                         + current_mde->constant_memory_alignment);
245
246                 mdl_struct = isci_host_alloc_mdl_struct(isci_host, size);
247                 if (!mdl_struct)
248                         return -ENOMEM;
249
250                 list_add_tail(&mdl_struct->node, &isci_host->mdl_struct_list);
251
252                 isci_host_build_mde(current_mde, mdl_struct);
253
254                 sci_mdl_next_entry(mdl_handle);
255                 current_mde = sci_mdl_get_current_entry(mdl_handle);
256         }
257
258         return 0;
259 }
260
261
262 /**
263  * isci_host_completion_routine() - This function is the delayed service
264  *    routine that calls the sci core library's completion handler. It's
265  *    scheduled as a tasklet from the interrupt service routine when interrupts
266  *    in use, or set as the timeout function in polled mode.
267  * @data: This parameter specifies the ISCI host object
268  *
269  */
270 static void isci_host_completion_routine(unsigned long data)
271 {
272         struct isci_host *isci_host = (struct isci_host *)data;
273         struct list_head completed_request_list;
274         struct list_head aborted_request_list;
275         struct list_head *current_position;
276         struct list_head *next_position;
277         struct isci_request *request;
278         struct isci_request *next_request;
279         struct sas_task *task;
280
281         INIT_LIST_HEAD(&completed_request_list);
282         INIT_LIST_HEAD(&aborted_request_list);
283
284         spin_lock_irq(&isci_host->scic_lock);
285
286         scic_sds_controller_completion_handler(isci_host->core_controller);
287
288         /* Take the lists of completed I/Os from the host. */
289         list_splice_init(&isci_host->requests_to_complete,
290                          &completed_request_list);
291
292         list_splice_init(&isci_host->requests_to_abort,
293                          &aborted_request_list);
294
295         spin_unlock_irq(&isci_host->scic_lock);
296
297         /* Process any completions in the lists. */
298         list_for_each_safe(current_position, next_position,
299                            &completed_request_list) {
300
301                 request = list_entry(current_position, struct isci_request,
302                                      completed_node);
303                 task = isci_request_access_task(request);
304
305                 /* Normal notification (task_done) */
306                 dev_dbg(&isci_host->pdev->dev,
307                         "%s: Normal - request/task = %p/%p\n",
308                         __func__,
309                         request,
310                         task);
311
312                 task->task_done(task);
313                 task->lldd_task = NULL;
314
315                 /* Free the request object. */
316                 isci_request_free(isci_host, request);
317         }
318         list_for_each_entry_safe(request, next_request, &aborted_request_list,
319                                  completed_node) {
320
321                 task = isci_request_access_task(request);
322
323                 /* Use sas_task_abort */
324                 dev_warn(&isci_host->pdev->dev,
325                          "%s: Error - request/task = %p/%p\n",
326                          __func__,
327                          request,
328                          task);
329
330                 /* Put the task into the abort path. */
331                 sas_task_abort(task);
332         }
333
334 }
335
336 void isci_host_deinit(struct isci_host *ihost)
337 {
338         struct scic_sds_controller *scic = ihost->core_controller;
339         int i;
340
341         isci_host_change_state(ihost, isci_stopping);
342         for (i = 0; i < SCI_MAX_PORTS; i++) {
343                 struct isci_port *port = &ihost->isci_ports[i];
344                 struct isci_remote_device *idev, *d;
345
346                 list_for_each_entry_safe(idev, d, &port->remote_dev_list, node) {
347                         isci_remote_device_change_state(idev, isci_stopping);
348                         isci_remote_device_stop(idev);
349                 }
350         }
351
352         set_bit(IHOST_STOP_PENDING, &ihost->flags);
353
354         spin_lock_irq(&ihost->scic_lock);
355         scic_controller_stop(scic, SCIC_CONTROLLER_STOP_TIMEOUT);
356         spin_unlock_irq(&ihost->scic_lock);
357
358         wait_for_stop(ihost);
359         scic_controller_reset(scic);
360         isci_timer_list_destroy(ihost);
361 }
362
363 static void __iomem *scu_base(struct isci_host *isci_host)
364 {
365         struct pci_dev *pdev = isci_host->pdev;
366         int id = isci_host->id;
367
368         return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
369 }
370
371 static void __iomem *smu_base(struct isci_host *isci_host)
372 {
373         struct pci_dev *pdev = isci_host->pdev;
374         int id = isci_host->id;
375
376         return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
377 }
378
379 int isci_host_init(struct isci_host *isci_host)
380 {
381         int err = 0;
382         int index = 0;
383         enum sci_status status;
384         struct scic_sds_controller *controller;
385         union scic_oem_parameters scic_oem_params;
386         union scic_user_parameters scic_user_params;
387
388         isci_timer_list_construct(isci_host);
389
390         controller = scic_controller_alloc(&isci_host->pdev->dev);
391
392         if (!controller) {
393                 dev_err(&isci_host->pdev->dev,
394                         "%s: failed (%d)\n",
395                         __func__,
396                         err);
397                 return -ENOMEM;
398         }
399
400         isci_host->core_controller = controller;
401         spin_lock_init(&isci_host->state_lock);
402         spin_lock_init(&isci_host->scic_lock);
403         spin_lock_init(&isci_host->queue_lock);
404         init_waitqueue_head(&isci_host->eventq);
405
406         isci_host_change_state(isci_host, isci_starting);
407         isci_host->can_queue = ISCI_CAN_QUEUE_VAL;
408
409         status = scic_controller_construct(controller, scu_base(isci_host),
410                                            smu_base(isci_host));
411
412         if (status != SCI_SUCCESS) {
413                 dev_err(&isci_host->pdev->dev,
414                         "%s: scic_controller_construct failed - status = %x\n",
415                         __func__,
416                         status);
417                 return -ENODEV;
418         }
419
420         isci_host->sas_ha.dev = &isci_host->pdev->dev;
421         isci_host->sas_ha.lldd_ha = isci_host;
422
423         /*----------- SCIC controller Initialization Stuff ------------------
424          * set association host adapter struct in core controller.
425          */
426         sci_object_set_association(isci_host->core_controller,
427                                    (void *)isci_host);
428
429         /* grab initial values stored in the controller object for OEM and USER
430          * parameters */
431         scic_oem_parameters_get(controller, &scic_oem_params);
432         scic_user_parameters_get(controller, &scic_user_params);
433
434         if (isci_firmware) {
435                 /* grab any OEM and USER parameters specified in binary blob */
436                 status = isci_parse_oem_parameters(&scic_oem_params,
437                                                    isci_host->id,
438                                                    isci_firmware);
439                 if (status != SCI_SUCCESS) {
440                         dev_warn(&isci_host->pdev->dev,
441                                  "parsing firmware oem parameters failed\n");
442                         return -EINVAL;
443                 }
444
445                 status = isci_parse_user_parameters(&scic_user_params,
446                                                     isci_host->id,
447                                                     isci_firmware);
448                 if (status != SCI_SUCCESS) {
449                         dev_warn(&isci_host->pdev->dev,
450                                  "%s: isci_parse_user_parameters"
451                                  " failed\n", __func__);
452                         return -EINVAL;
453                 }
454         } else {
455                 status = scic_oem_parameters_set(isci_host->core_controller,
456                                                  &scic_oem_params);
457                 if (status != SCI_SUCCESS) {
458                         dev_warn(&isci_host->pdev->dev,
459                                  "%s: scic_oem_parameters_set failed\n",
460                                  __func__);
461                         return -ENODEV;
462                 }
463
464
465                 status = scic_user_parameters_set(isci_host->core_controller,
466                                                   &scic_user_params);
467                 if (status != SCI_SUCCESS) {
468                         dev_warn(&isci_host->pdev->dev,
469                                  "%s: scic_user_parameters_set failed\n",
470                                  __func__);
471                         return -ENODEV;
472                 }
473         }
474
475         tasklet_init(&isci_host->completion_tasklet,
476                      isci_host_completion_routine, (unsigned long)isci_host);
477
478         INIT_LIST_HEAD(&(isci_host->mdl_struct_list));
479
480         INIT_LIST_HEAD(&isci_host->requests_to_complete);
481         INIT_LIST_HEAD(&isci_host->requests_to_abort);
482
483         spin_lock_irq(&isci_host->scic_lock);
484         status = scic_controller_initialize(isci_host->core_controller);
485         spin_unlock_irq(&isci_host->scic_lock);
486         if (status != SCI_SUCCESS) {
487                 dev_warn(&isci_host->pdev->dev,
488                          "%s: scic_controller_initialize failed -"
489                          " status = 0x%x\n",
490                          __func__, status);
491                 return -ENODEV;
492         }
493
494         /* populate mdl with dma memory. scu_mdl_allocate_coherent() */
495         err = isci_host_mdl_allocate_coherent(isci_host);
496         if (err)
497                 return err;
498
499         /*
500          * keep the pool alloc size around, will use it for a bounds checking
501          * when trying to convert virtual addresses to physical addresses
502          */
503         isci_host->dma_pool_alloc_size = sizeof(struct isci_request) +
504                                          scic_io_request_get_object_size();
505         isci_host->dma_pool = dmam_pool_create(DRV_NAME, &isci_host->pdev->dev,
506                                                isci_host->dma_pool_alloc_size,
507                                                SLAB_HWCACHE_ALIGN, 0);
508
509         if (!isci_host->dma_pool)
510                 return -ENOMEM;
511
512         for (index = 0; index < SCI_MAX_PORTS; index++)
513                 isci_port_init(&isci_host->isci_ports[index],
514                                isci_host,
515                                index);
516
517         for (index = 0; index < SCI_MAX_PHYS; index++)
518                 isci_phy_init(&isci_host->phys[index], isci_host, index);
519
520         return 0;
521 }