]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/scsi/isci/host.c
isci: implement error isr
[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
82         for_each_isci_host(ihost, pdev) {
83                 struct scic_sds_controller *scic = ihost->core_controller;
84
85                 if (scic_sds_controller_isr(scic)) {
86                         tasklet_schedule(&ihost->completion_tasklet);
87                         ret = IRQ_HANDLED;
88                 } else if (scic_sds_controller_error_isr(scic)) {
89                         spin_lock(&ihost->scic_lock);
90                         scic_sds_controller_error_handler(scic);
91                         spin_unlock(&ihost->scic_lock);
92                         ret = IRQ_HANDLED;
93                 }
94         }
95
96         return ret;
97 }
98
99 irqreturn_t isci_error_isr(int vec, void *data)
100 {
101         struct isci_host *ihost = data;
102         struct scic_sds_controller *scic = ihost->core_controller;
103
104         if (scic_sds_controller_error_isr(scic))
105                 scic_sds_controller_error_handler(scic);
106
107         return IRQ_HANDLED;
108 }
109
110 /**
111  * isci_host_start_complete() - This function is called by the core library,
112  *    through the ISCI Module, to indicate controller start status.
113  * @isci_host: This parameter specifies the ISCI host object
114  * @completion_status: This parameter specifies the completion status from the
115  *    core library.
116  *
117  */
118 void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
119 {
120         if (completion_status != SCI_SUCCESS)
121                 dev_info(&ihost->pdev->dev,
122                         "controller start timed out, continuing...\n");
123         isci_host_change_state(ihost, isci_ready);
124         clear_bit(IHOST_START_PENDING, &ihost->flags);
125         wake_up(&ihost->eventq);
126 }
127
128 int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
129 {
130         struct isci_host *ihost = isci_host_from_sas_ha(SHOST_TO_SAS_HA(shost));
131
132         if (test_bit(IHOST_START_PENDING, &ihost->flags))
133                 return 0;
134
135         /* todo: use sas_flush_discovery once it is upstream */
136         scsi_flush_work(shost);
137
138         scsi_flush_work(shost);
139
140         dev_dbg(&ihost->pdev->dev,
141                 "%s: ihost->status = %d, time = %ld\n",
142                  __func__, isci_host_get_state(ihost), time);
143
144         return 1;
145
146 }
147
148 void isci_host_scan_start(struct Scsi_Host *shost)
149 {
150         struct isci_host *ihost = isci_host_from_sas_ha(SHOST_TO_SAS_HA(shost));
151         struct scic_sds_controller *scic = ihost->core_controller;
152         unsigned long tmo = scic_controller_get_suggested_start_timeout(scic);
153
154         set_bit(IHOST_START_PENDING, &ihost->flags);
155
156         spin_lock_irq(&ihost->scic_lock);
157         scic_controller_start(scic, tmo);
158         scic_controller_enable_interrupts(scic);
159         spin_unlock_irq(&ihost->scic_lock);
160 }
161
162 void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
163 {
164         isci_host_change_state(ihost, isci_stopped);
165         scic_controller_disable_interrupts(ihost->core_controller);
166         clear_bit(IHOST_STOP_PENDING, &ihost->flags);
167         wake_up(&ihost->eventq);
168 }
169
170 static struct coherent_memory_info *isci_host_alloc_mdl_struct(
171         struct isci_host *isci_host,
172         u32 size)
173 {
174         struct coherent_memory_info *mdl_struct;
175         void *uncached_address = NULL;
176
177
178         mdl_struct = devm_kzalloc(&isci_host->pdev->dev,
179                                   sizeof(*mdl_struct),
180                                   GFP_KERNEL);
181         if (!mdl_struct)
182                 return NULL;
183
184         INIT_LIST_HEAD(&mdl_struct->node);
185
186         uncached_address = dmam_alloc_coherent(&isci_host->pdev->dev,
187                                                size,
188                                                &mdl_struct->dma_handle,
189                                                GFP_KERNEL);
190         if (!uncached_address)
191                 return NULL;
192
193         /* memset the whole memory area. */
194         memset((char *)uncached_address, 0, size);
195         mdl_struct->vaddr = uncached_address;
196         mdl_struct->size = (size_t)size;
197
198         return mdl_struct;
199 }
200
201 static void isci_host_build_mde(
202         struct sci_physical_memory_descriptor *mde_struct,
203         struct coherent_memory_info *mdl_struct)
204 {
205         unsigned long address = 0;
206         dma_addr_t dma_addr = 0;
207
208         address = (unsigned long)mdl_struct->vaddr;
209         dma_addr = mdl_struct->dma_handle;
210
211         /* to satisfy the alignment. */
212         if ((address % mde_struct->constant_memory_alignment) != 0) {
213                 int align_offset
214                         = (mde_struct->constant_memory_alignment
215                            - (address % mde_struct->constant_memory_alignment));
216                 address += align_offset;
217                 dma_addr += align_offset;
218         }
219
220         mde_struct->virtual_address = (void *)address;
221         mde_struct->physical_address = dma_addr;
222         mdl_struct->mde = mde_struct;
223 }
224
225 static int isci_host_mdl_allocate_coherent(
226         struct isci_host *isci_host)
227 {
228         struct sci_physical_memory_descriptor *current_mde;
229         struct coherent_memory_info *mdl_struct;
230         u32 size = 0;
231
232         struct sci_base_memory_descriptor_list *mdl_handle
233                 = sci_controller_get_memory_descriptor_list_handle(
234                 isci_host->core_controller);
235
236         sci_mdl_first_entry(mdl_handle);
237
238         current_mde = sci_mdl_get_current_entry(mdl_handle);
239
240         while (current_mde != NULL) {
241
242                 size = (current_mde->constant_memory_size
243                         + current_mde->constant_memory_alignment);
244
245                 mdl_struct = isci_host_alloc_mdl_struct(isci_host, size);
246                 if (!mdl_struct)
247                         return -ENOMEM;
248
249                 list_add_tail(&mdl_struct->node, &isci_host->mdl_struct_list);
250
251                 isci_host_build_mde(current_mde, mdl_struct);
252
253                 sci_mdl_next_entry(mdl_handle);
254                 current_mde = sci_mdl_get_current_entry(mdl_handle);
255         }
256
257         return 0;
258 }
259
260
261 /**
262  * isci_host_completion_routine() - This function is the delayed service
263  *    routine that calls the sci core library's completion handler. It's
264  *    scheduled as a tasklet from the interrupt service routine when interrupts
265  *    in use, or set as the timeout function in polled mode.
266  * @data: This parameter specifies the ISCI host object
267  *
268  */
269 static void isci_host_completion_routine(unsigned long data)
270 {
271         struct isci_host *isci_host = (struct isci_host *)data;
272         struct list_head completed_request_list;
273         struct list_head aborted_request_list;
274         struct list_head *current_position;
275         struct list_head *next_position;
276         struct isci_request *request;
277         struct isci_request *next_request;
278         struct sas_task *task;
279
280         INIT_LIST_HEAD(&completed_request_list);
281         INIT_LIST_HEAD(&aborted_request_list);
282
283         spin_lock_irq(&isci_host->scic_lock);
284
285         scic_sds_controller_completion_handler(isci_host->core_controller);
286
287         /* Take the lists of completed I/Os from the host. */
288         list_splice_init(&isci_host->requests_to_complete,
289                          &completed_request_list);
290
291         list_splice_init(&isci_host->requests_to_abort,
292                          &aborted_request_list);
293
294         spin_unlock_irq(&isci_host->scic_lock);
295
296         /* Process any completions in the lists. */
297         list_for_each_safe(current_position, next_position,
298                            &completed_request_list) {
299
300                 request = list_entry(current_position, struct isci_request,
301                                      completed_node);
302                 task = isci_request_access_task(request);
303
304                 /* Normal notification (task_done) */
305                 dev_dbg(&isci_host->pdev->dev,
306                         "%s: Normal - request/task = %p/%p\n",
307                         __func__,
308                         request,
309                         task);
310
311                 task->task_done(task);
312                 task->lldd_task = NULL;
313
314                 /* Free the request object. */
315                 isci_request_free(isci_host, request);
316         }
317         list_for_each_entry_safe(request, next_request, &aborted_request_list,
318                                  completed_node) {
319
320                 task = isci_request_access_task(request);
321
322                 /* Use sas_task_abort */
323                 dev_warn(&isci_host->pdev->dev,
324                          "%s: Error - request/task = %p/%p\n",
325                          __func__,
326                          request,
327                          task);
328
329                 /* Put the task into the abort path. */
330                 sas_task_abort(task);
331         }
332
333 }
334
335 void isci_host_deinit(struct isci_host *ihost)
336 {
337         struct scic_sds_controller *scic = ihost->core_controller;
338         int i;
339
340         isci_host_change_state(ihost, isci_stopping);
341         for (i = 0; i < SCI_MAX_PORTS; i++) {
342                 struct isci_port *port = &ihost->isci_ports[i];
343                 struct isci_remote_device *idev, *d;
344
345                 list_for_each_entry_safe(idev, d, &port->remote_dev_list, node) {
346                         isci_remote_device_change_state(idev, isci_stopping);
347                         isci_remote_device_stop(idev);
348                 }
349         }
350
351         set_bit(IHOST_STOP_PENDING, &ihost->flags);
352         scic_controller_stop(scic, SCIC_CONTROLLER_STOP_TIMEOUT);
353         wait_for_stop(ihost);
354         scic_controller_reset(scic);
355 }
356
357 static int isci_verify_firmware(const struct firmware *fw,
358                                 struct isci_firmware *isci_fw)
359 {
360         const u8 *tmp;
361
362         if (fw->size < ISCI_FIRMWARE_MIN_SIZE)
363                 return -EINVAL;
364
365         tmp = fw->data;
366
367         /* 12th char should be the NULL terminate for the ID string */
368         if (tmp[11] != '\0')
369                 return -EINVAL;
370
371         if (strncmp("#SCU MAGIC#", tmp, 11) != 0)
372                 return -EINVAL;
373
374         isci_fw->id = tmp;
375         isci_fw->version = fw->data[ISCI_FW_VER_OFS];
376         isci_fw->subversion = fw->data[ISCI_FW_SUBVER_OFS];
377
378         tmp = fw->data + ISCI_FW_DATA_OFS;
379
380         while (*tmp != ISCI_FW_HDR_EOF) {
381                 switch (*tmp) {
382                 case ISCI_FW_HDR_PHYMASK:
383                         tmp++;
384                         isci_fw->phy_masks_size = *tmp;
385                         tmp++;
386                         isci_fw->phy_masks = (const u32 *)tmp;
387                         tmp += sizeof(u32) * isci_fw->phy_masks_size;
388                         break;
389
390                 case ISCI_FW_HDR_PHYGEN:
391                         tmp++;
392                         isci_fw->phy_gens_size = *tmp;
393                         tmp++;
394                         isci_fw->phy_gens = (const u32 *)tmp;
395                         tmp += sizeof(u32) * isci_fw->phy_gens_size;
396                         break;
397
398                 case ISCI_FW_HDR_SASADDR:
399                         tmp++;
400                         isci_fw->sas_addrs_size = *tmp;
401                         tmp++;
402                         isci_fw->sas_addrs = (const u64 *)tmp;
403                         tmp += sizeof(u64) * isci_fw->sas_addrs_size;
404                         break;
405
406                 default:
407                         pr_err("bad field in firmware binary blob\n");
408                         return -EINVAL;
409                 }
410         }
411
412         pr_info("isci firmware v%u.%u loaded.\n",
413                isci_fw->version, isci_fw->subversion);
414
415         return SCI_SUCCESS;
416 }
417
418 static void __iomem *scu_base(struct isci_host *isci_host)
419 {
420         struct pci_dev *pdev = isci_host->pdev;
421         int id = isci_host->id;
422
423         return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
424 }
425
426 static void __iomem *smu_base(struct isci_host *isci_host)
427 {
428         struct pci_dev *pdev = isci_host->pdev;
429         int id = isci_host->id;
430
431         return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
432 }
433
434 #define SCI_MAX_TIMER_COUNT 25
435
436 int isci_host_init(struct isci_host *isci_host)
437 {
438         int err = 0;
439         int index = 0;
440         enum sci_status status;
441         struct scic_sds_controller *controller;
442         struct scic_sds_port *scic_port;
443         union scic_oem_parameters scic_oem_params;
444         union scic_user_parameters scic_user_params;
445         const struct firmware *fw = NULL;
446         struct isci_firmware *isci_fw = NULL;
447
448         INIT_LIST_HEAD(&isci_host->timer_list_struct.timers);
449         isci_timer_list_construct(
450                 &isci_host->timer_list_struct,
451                 SCI_MAX_TIMER_COUNT
452                 );
453
454         controller = scic_controller_alloc(&isci_host->pdev->dev);
455
456         if (!controller) {
457                 err = -ENOMEM;
458                 dev_err(&isci_host->pdev->dev, "%s: failed (%d)\n", __func__, err);
459                 goto out;
460         }
461
462         isci_host->core_controller = controller;
463         spin_lock_init(&isci_host->state_lock);
464         spin_lock_init(&isci_host->scic_lock);
465         spin_lock_init(&isci_host->queue_lock);
466         init_waitqueue_head(&isci_host->eventq);
467
468         isci_host_change_state(isci_host, isci_starting);
469         isci_host->can_queue = ISCI_CAN_QUEUE_VAL;
470
471         status = scic_controller_construct(controller, scu_base(isci_host),
472                                            smu_base(isci_host));
473
474         if (status != SCI_SUCCESS) {
475                 dev_err(&isci_host->pdev->dev,
476                         "%s: scic_controller_construct failed - status = %x\n",
477                         __func__,
478                         status);
479                 err = -ENODEV;
480                 goto out;
481         }
482
483         isci_host->sas_ha.dev = &isci_host->pdev->dev;
484         isci_host->sas_ha.lldd_ha = isci_host;
485
486         /*----------- SCIC controller Initialization Stuff ------------------
487          * set association host adapter struct in core controller.
488          */
489         sci_object_set_association(isci_host->core_controller,
490                                    (void *)isci_host
491                                    );
492
493         /* grab initial values stored in the controller object for OEM and USER
494          * parameters */
495         scic_oem_parameters_get(controller, &scic_oem_params);
496         scic_user_parameters_get(controller, &scic_user_params);
497
498         isci_fw = devm_kzalloc(&isci_host->pdev->dev,
499                                sizeof(struct isci_firmware),
500                                GFP_KERNEL);
501         if (!isci_fw) {
502                 dev_warn(&isci_host->pdev->dev,
503                          "allocating firmware struct failed\n");
504                 dev_warn(&isci_host->pdev->dev,
505                          "Default OEM configuration being used:"
506                          " 4 narrow ports, and default SAS Addresses\n");
507                 goto set_default_params;
508         }
509
510         status = request_firmware(&fw, ISCI_FW_NAME, &isci_host->pdev->dev);
511         if (status) {
512                 dev_warn(&isci_host->pdev->dev,
513                          "Loading firmware failed, using default values\n");
514                 dev_warn(&isci_host->pdev->dev,
515                          "Default OEM configuration being used:"
516                          " 4 narrow ports, and default SAS Addresses\n");
517                 goto set_default_params;
518         }
519         else {
520                 status = isci_verify_firmware(fw, isci_fw);
521                 if (status != SCI_SUCCESS) {
522                         dev_warn(&isci_host->pdev->dev,
523                                  "firmware verification failed\n");
524                         dev_warn(&isci_host->pdev->dev,
525                                  "Default OEM configuration being used:"
526                                  " 4 narrow ports, and default SAS "
527                                  "Addresses\n");
528                         goto set_default_params;
529                 }
530
531                 /* grab any OEM and USER parameters specified at module load */
532                 status = isci_parse_oem_parameters(&scic_oem_params,
533                                                    isci_host->id, isci_fw);
534                 if (status != SCI_SUCCESS) {
535                         dev_warn(&isci_host->pdev->dev,
536                                  "parsing firmware oem parameters failed\n");
537                         err = -EINVAL;
538                         goto out;
539                 }
540
541                 status = isci_parse_user_parameters(&scic_user_params,
542                                                     isci_host->id, isci_fw);
543                 if (status != SCI_SUCCESS) {
544                         dev_warn(&isci_host->pdev->dev,
545                                  "%s: isci_parse_user_parameters"
546                                  " failed\n", __func__);
547                         err = -EINVAL;
548                         goto out;
549                 }
550         }
551
552  set_default_params:
553
554         status = scic_oem_parameters_set(isci_host->core_controller,
555                                          &scic_oem_params
556                                          );
557
558         if (status != SCI_SUCCESS) {
559                 dev_warn(&isci_host->pdev->dev,
560                          "%s: scic_oem_parameters_set failed\n",
561                          __func__);
562                 err = -ENODEV;
563                 goto out;
564         }
565
566
567         status = scic_user_parameters_set(isci_host->core_controller,
568                                           &scic_user_params
569                                           );
570
571         if (status != SCI_SUCCESS) {
572                 dev_warn(&isci_host->pdev->dev,
573                          "%s: scic_user_parameters_set failed\n",
574                          __func__);
575                 err = -ENODEV;
576                 goto out;
577         }
578
579         status = scic_controller_initialize(isci_host->core_controller);
580         if (status != SCI_SUCCESS) {
581                 dev_warn(&isci_host->pdev->dev,
582                          "%s: scic_controller_initialize failed -"
583                          " status = 0x%x\n",
584                          __func__, status);
585                 err = -ENODEV;
586                 goto out;
587         }
588
589         tasklet_init(&isci_host->completion_tasklet,
590                      isci_host_completion_routine, (unsigned long)isci_host);
591
592         INIT_LIST_HEAD(&(isci_host->mdl_struct_list));
593
594         INIT_LIST_HEAD(&isci_host->requests_to_complete);
595         INIT_LIST_HEAD(&isci_host->requests_to_abort);
596
597         /* populate mdl with dma memory. scu_mdl_allocate_coherent() */
598         err = isci_host_mdl_allocate_coherent(isci_host);
599
600         if (err)
601                 goto err_out;
602
603         /*
604          * keep the pool alloc size around, will use it for a bounds checking
605          * when trying to convert virtual addresses to physical addresses
606          */
607         isci_host->dma_pool_alloc_size = sizeof(struct isci_request) +
608                                          scic_io_request_get_object_size();
609         isci_host->dma_pool = dmam_pool_create(DRV_NAME, &isci_host->pdev->dev,
610                                                isci_host->dma_pool_alloc_size,
611                                                SLAB_HWCACHE_ALIGN, 0);
612
613         if (!isci_host->dma_pool) {
614                 err = -ENOMEM;
615                 goto req_obj_err_out;
616         }
617
618         for (index = 0; index < SCI_MAX_PORTS; index++) {
619                 isci_port_init(&isci_host->isci_ports[index],
620                                isci_host, index);
621         }
622
623         for (index = 0; index < SCI_MAX_PHYS; index++)
624                 isci_phy_init(&isci_host->phys[index], isci_host, index);
625
626         /* Why are we doing this? Is this even necessary? */
627         memcpy(&isci_host->sas_addr[0], &isci_host->phys[0].sas_addr[0],
628                SAS_ADDR_SIZE);
629
630         /* Start the ports */
631         for (index = 0; index < SCI_MAX_PORTS; index++) {
632
633                 scic_controller_get_port_handle(controller, index, &scic_port);
634                 scic_port_start(scic_port);
635         }
636
637         goto out;
638
639 /* SPB_Debug: destroy request object cache */
640  req_obj_err_out:
641 /* SPB_Debug: destroy remote object cache */
642  err_out:
643 /* SPB_Debug: undo controller init, construct and alloc, remove from parent
644  * controller list. */
645  out:
646         if (fw)
647                 release_firmware(fw);
648         return err;
649 }