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