dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / scsi / esas2r / esas2r_init.c
blob78ce4d61a69bbab88e1f90d52ea8c3084c12f0fe
1 /*
2 * linux/drivers/scsi/esas2r/esas2r_init.c
3 * For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
5 * Copyright (c) 2001-2013 ATTO Technology, Inc.
6 * (mailto:linuxdrivers@attotech.com)mpt3sas/mpt3sas_trigger_diag.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
44 #include "esas2r.h"
46 static bool esas2r_initmem_alloc(struct esas2r_adapter *a,
47 struct esas2r_mem_desc *mem_desc,
48 u32 align)
50 mem_desc->esas2r_param = mem_desc->size + align;
51 mem_desc->virt_addr = NULL;
52 mem_desc->phys_addr = 0;
53 mem_desc->esas2r_data = dma_alloc_coherent(&a->pcid->dev,
54 (size_t)mem_desc->
55 esas2r_param,
56 (dma_addr_t *)&mem_desc->
57 phys_addr,
58 GFP_KERNEL);
60 if (mem_desc->esas2r_data == NULL) {
61 esas2r_log(ESAS2R_LOG_CRIT,
62 "failed to allocate %lu bytes of consistent memory!",
63 (long
64 unsigned
65 int)mem_desc->esas2r_param);
66 return false;
69 mem_desc->virt_addr = PTR_ALIGN(mem_desc->esas2r_data, align);
70 mem_desc->phys_addr = ALIGN(mem_desc->phys_addr, align);
71 memset(mem_desc->virt_addr, 0, mem_desc->size);
72 return true;
75 static void esas2r_initmem_free(struct esas2r_adapter *a,
76 struct esas2r_mem_desc *mem_desc)
78 if (mem_desc->virt_addr == NULL)
79 return;
82 * Careful! phys_addr and virt_addr may have been adjusted from the
83 * original allocation in order to return the desired alignment. That
84 * means we have to use the original address (in esas2r_data) and size
85 * (esas2r_param) and calculate the original physical address based on
86 * the difference between the requested and actual allocation size.
88 if (mem_desc->phys_addr) {
89 int unalign = ((u8 *)mem_desc->virt_addr) -
90 ((u8 *)mem_desc->esas2r_data);
92 dma_free_coherent(&a->pcid->dev,
93 (size_t)mem_desc->esas2r_param,
94 mem_desc->esas2r_data,
95 (dma_addr_t)(mem_desc->phys_addr - unalign));
96 } else {
97 kfree(mem_desc->esas2r_data);
100 mem_desc->virt_addr = NULL;
103 static bool alloc_vda_req(struct esas2r_adapter *a,
104 struct esas2r_request *rq)
106 struct esas2r_mem_desc *memdesc = kzalloc(
107 sizeof(struct esas2r_mem_desc), GFP_KERNEL);
109 if (memdesc == NULL) {
110 esas2r_hdebug("could not alloc mem for vda request memdesc\n");
111 return false;
114 memdesc->size = sizeof(union atto_vda_req) +
115 ESAS2R_DATA_BUF_LEN;
117 if (!esas2r_initmem_alloc(a, memdesc, 256)) {
118 esas2r_hdebug("could not alloc mem for vda request\n");
119 kfree(memdesc);
120 return false;
123 a->num_vrqs++;
124 list_add(&memdesc->next_desc, &a->vrq_mds_head);
126 rq->vrq_md = memdesc;
127 rq->vrq = (union atto_vda_req *)memdesc->virt_addr;
128 rq->vrq->scsi.handle = a->num_vrqs;
130 return true;
133 static void esas2r_unmap_regions(struct esas2r_adapter *a)
135 if (a->regs)
136 iounmap((void __iomem *)a->regs);
138 a->regs = NULL;
140 pci_release_region(a->pcid, 2);
142 if (a->data_window)
143 iounmap((void __iomem *)a->data_window);
145 a->data_window = NULL;
147 pci_release_region(a->pcid, 0);
150 static int esas2r_map_regions(struct esas2r_adapter *a)
152 int error;
154 a->regs = NULL;
155 a->data_window = NULL;
157 error = pci_request_region(a->pcid, 2, a->name);
158 if (error != 0) {
159 esas2r_log(ESAS2R_LOG_CRIT,
160 "pci_request_region(2) failed, error %d",
161 error);
163 return error;
166 a->regs = (void __force *)ioremap(pci_resource_start(a->pcid, 2),
167 pci_resource_len(a->pcid, 2));
168 if (a->regs == NULL) {
169 esas2r_log(ESAS2R_LOG_CRIT,
170 "ioremap failed for regs mem region\n");
171 pci_release_region(a->pcid, 2);
172 return -EFAULT;
175 error = pci_request_region(a->pcid, 0, a->name);
176 if (error != 0) {
177 esas2r_log(ESAS2R_LOG_CRIT,
178 "pci_request_region(2) failed, error %d",
179 error);
180 esas2r_unmap_regions(a);
181 return error;
184 a->data_window = (void __force *)ioremap(pci_resource_start(a->pcid,
186 pci_resource_len(a->pcid, 0));
187 if (a->data_window == NULL) {
188 esas2r_log(ESAS2R_LOG_CRIT,
189 "ioremap failed for data_window mem region\n");
190 esas2r_unmap_regions(a);
191 return -EFAULT;
194 return 0;
197 static void esas2r_setup_interrupts(struct esas2r_adapter *a, int intr_mode)
199 int i;
201 /* Set up interrupt mode based on the requested value */
202 switch (intr_mode) {
203 case INTR_MODE_LEGACY:
204 use_legacy_interrupts:
205 a->intr_mode = INTR_MODE_LEGACY;
206 break;
208 case INTR_MODE_MSI:
209 i = pci_enable_msi(a->pcid);
210 if (i != 0) {
211 esas2r_log(ESAS2R_LOG_WARN,
212 "failed to enable MSI for adapter %d, "
213 "falling back to legacy interrupts "
214 "(err=%d)", a->index,
216 goto use_legacy_interrupts;
218 a->intr_mode = INTR_MODE_MSI;
219 set_bit(AF2_MSI_ENABLED, &a->flags2);
220 break;
223 default:
224 esas2r_log(ESAS2R_LOG_WARN,
225 "unknown interrupt_mode %d requested, "
226 "falling back to legacy interrupt",
227 interrupt_mode);
228 goto use_legacy_interrupts;
232 static void esas2r_claim_interrupts(struct esas2r_adapter *a)
234 unsigned long flags = 0;
236 if (a->intr_mode == INTR_MODE_LEGACY)
237 flags |= IRQF_SHARED;
239 esas2r_log(ESAS2R_LOG_INFO,
240 "esas2r_claim_interrupts irq=%d (%p, %s, %x)",
241 a->pcid->irq, a, a->name, flags);
243 if (request_irq(a->pcid->irq,
244 (a->intr_mode ==
245 INTR_MODE_LEGACY) ? esas2r_interrupt :
246 esas2r_msi_interrupt,
247 flags,
248 a->name,
249 a)) {
250 esas2r_log(ESAS2R_LOG_CRIT, "unable to request IRQ %02X",
251 a->pcid->irq);
252 return;
255 set_bit(AF2_IRQ_CLAIMED, &a->flags2);
256 esas2r_log(ESAS2R_LOG_INFO,
257 "claimed IRQ %d flags: 0x%lx",
258 a->pcid->irq, flags);
261 int esas2r_init_adapter(struct Scsi_Host *host, struct pci_dev *pcid,
262 int index)
264 struct esas2r_adapter *a;
265 u64 bus_addr = 0;
266 int i;
267 void *next_uncached;
268 struct esas2r_request *first_request, *last_request;
270 if (index >= MAX_ADAPTERS) {
271 esas2r_log(ESAS2R_LOG_CRIT,
272 "tried to init invalid adapter index %u!",
273 index);
274 return 0;
277 if (esas2r_adapters[index]) {
278 esas2r_log(ESAS2R_LOG_CRIT,
279 "tried to init existing adapter index %u!",
280 index);
281 return 0;
284 a = (struct esas2r_adapter *)host->hostdata;
285 memset(a, 0, sizeof(struct esas2r_adapter));
286 a->pcid = pcid;
287 a->host = host;
289 if (sizeof(dma_addr_t) > 4) {
290 const uint64_t required_mask = dma_get_required_mask
291 (&pcid->dev);
292 if (required_mask > DMA_BIT_MASK(32)
293 && !pci_set_dma_mask(pcid, DMA_BIT_MASK(64))
294 && !pci_set_consistent_dma_mask(pcid,
295 DMA_BIT_MASK(64))) {
296 esas2r_log_dev(ESAS2R_LOG_INFO,
297 &(a->pcid->dev),
298 "64-bit PCI addressing enabled\n");
299 } else if (!pci_set_dma_mask(pcid, DMA_BIT_MASK(32))
300 && !pci_set_consistent_dma_mask(pcid,
301 DMA_BIT_MASK(32))) {
302 esas2r_log_dev(ESAS2R_LOG_INFO,
303 &(a->pcid->dev),
304 "32-bit PCI addressing enabled\n");
305 } else {
306 esas2r_log(ESAS2R_LOG_CRIT,
307 "failed to set DMA mask");
308 esas2r_kill_adapter(index);
309 return 0;
311 } else {
312 if (!pci_set_dma_mask(pcid, DMA_BIT_MASK(32))
313 && !pci_set_consistent_dma_mask(pcid,
314 DMA_BIT_MASK(32))) {
315 esas2r_log_dev(ESAS2R_LOG_INFO,
316 &(a->pcid->dev),
317 "32-bit PCI addressing enabled\n");
318 } else {
319 esas2r_log(ESAS2R_LOG_CRIT,
320 "failed to set DMA mask");
321 esas2r_kill_adapter(index);
322 return 0;
325 esas2r_adapters[index] = a;
326 sprintf(a->name, ESAS2R_DRVR_NAME "_%02d", index);
327 esas2r_debug("new adapter %p, name %s", a, a->name);
328 spin_lock_init(&a->request_lock);
329 spin_lock_init(&a->fw_event_lock);
330 sema_init(&a->fm_api_semaphore, 1);
331 sema_init(&a->fs_api_semaphore, 1);
332 sema_init(&a->nvram_semaphore, 1);
334 esas2r_fw_event_off(a);
335 snprintf(a->fw_event_q_name, ESAS2R_KOBJ_NAME_LEN, "esas2r/%d",
336 a->index);
337 a->fw_event_q = create_singlethread_workqueue(a->fw_event_q_name);
339 init_waitqueue_head(&a->buffered_ioctl_waiter);
340 init_waitqueue_head(&a->nvram_waiter);
341 init_waitqueue_head(&a->fm_api_waiter);
342 init_waitqueue_head(&a->fs_api_waiter);
343 init_waitqueue_head(&a->vda_waiter);
345 INIT_LIST_HEAD(&a->general_req.req_list);
346 INIT_LIST_HEAD(&a->active_list);
347 INIT_LIST_HEAD(&a->defer_list);
348 INIT_LIST_HEAD(&a->free_sg_list_head);
349 INIT_LIST_HEAD(&a->avail_request);
350 INIT_LIST_HEAD(&a->vrq_mds_head);
351 INIT_LIST_HEAD(&a->fw_event_list);
353 first_request = (struct esas2r_request *)((u8 *)(a + 1));
355 for (last_request = first_request, i = 1; i < num_requests;
356 last_request++, i++) {
357 INIT_LIST_HEAD(&last_request->req_list);
358 list_add_tail(&last_request->comp_list, &a->avail_request);
359 if (!alloc_vda_req(a, last_request)) {
360 esas2r_log(ESAS2R_LOG_CRIT,
361 "failed to allocate a VDA request!");
362 esas2r_kill_adapter(index);
363 return 0;
367 esas2r_debug("requests: %p to %p (%d, %d)", first_request,
368 last_request,
369 sizeof(*first_request),
370 num_requests);
372 if (esas2r_map_regions(a) != 0) {
373 esas2r_log(ESAS2R_LOG_CRIT, "could not map PCI regions!");
374 esas2r_kill_adapter(index);
375 return 0;
378 a->index = index;
380 /* interrupts will be disabled until we are done with init */
381 atomic_inc(&a->dis_ints_cnt);
382 atomic_inc(&a->disable_cnt);
383 set_bit(AF_CHPRST_PENDING, &a->flags);
384 set_bit(AF_DISC_PENDING, &a->flags);
385 set_bit(AF_FIRST_INIT, &a->flags);
386 set_bit(AF_LEGACY_SGE_MODE, &a->flags);
388 a->init_msg = ESAS2R_INIT_MSG_START;
389 a->max_vdareq_size = 128;
390 a->build_sgl = esas2r_build_sg_list_sge;
392 esas2r_setup_interrupts(a, interrupt_mode);
394 a->uncached_size = esas2r_get_uncached_size(a);
395 a->uncached = dma_alloc_coherent(&pcid->dev,
396 (size_t)a->uncached_size,
397 (dma_addr_t *)&bus_addr,
398 GFP_KERNEL);
399 if (a->uncached == NULL) {
400 esas2r_log(ESAS2R_LOG_CRIT,
401 "failed to allocate %d bytes of consistent memory!",
402 a->uncached_size);
403 esas2r_kill_adapter(index);
404 return 0;
407 a->uncached_phys = bus_addr;
409 esas2r_debug("%d bytes uncached memory allocated @ %p (%x:%x)",
410 a->uncached_size,
411 a->uncached,
412 upper_32_bits(bus_addr),
413 lower_32_bits(bus_addr));
414 memset(a->uncached, 0, a->uncached_size);
415 next_uncached = a->uncached;
417 if (!esas2r_init_adapter_struct(a,
418 &next_uncached)) {
419 esas2r_log(ESAS2R_LOG_CRIT,
420 "failed to initialize adapter structure (2)!");
421 esas2r_kill_adapter(index);
422 return 0;
425 tasklet_init(&a->tasklet,
426 esas2r_adapter_tasklet,
427 (unsigned long)a);
430 * Disable chip interrupts to prevent spurious interrupts
431 * until we claim the IRQ.
433 esas2r_disable_chip_interrupts(a);
434 esas2r_check_adapter(a);
436 if (!esas2r_init_adapter_hw(a, true))
437 esas2r_log(ESAS2R_LOG_CRIT, "failed to initialize hardware!");
438 else
439 esas2r_debug("esas2r_init_adapter ok");
441 esas2r_claim_interrupts(a);
443 if (test_bit(AF2_IRQ_CLAIMED, &a->flags2))
444 esas2r_enable_chip_interrupts(a);
446 set_bit(AF2_INIT_DONE, &a->flags2);
447 if (!test_bit(AF_DEGRADED_MODE, &a->flags))
448 esas2r_kickoff_timer(a);
449 esas2r_debug("esas2r_init_adapter done for %p (%d)",
450 a, a->disable_cnt);
452 return 1;
455 static void esas2r_adapter_power_down(struct esas2r_adapter *a,
456 int power_management)
458 struct esas2r_mem_desc *memdesc, *next;
460 if ((test_bit(AF2_INIT_DONE, &a->flags2))
461 && (!test_bit(AF_DEGRADED_MODE, &a->flags))) {
462 if (!power_management) {
463 del_timer_sync(&a->timer);
464 tasklet_kill(&a->tasklet);
466 esas2r_power_down(a);
469 * There are versions of firmware that do not handle the sync
470 * cache command correctly. Stall here to ensure that the
471 * cache is lazily flushed.
473 mdelay(500);
474 esas2r_debug("chip halted");
477 /* Remove sysfs binary files */
478 if (a->sysfs_fw_created) {
479 sysfs_remove_bin_file(&a->host->shost_dev.kobj, &bin_attr_fw);
480 a->sysfs_fw_created = 0;
483 if (a->sysfs_fs_created) {
484 sysfs_remove_bin_file(&a->host->shost_dev.kobj, &bin_attr_fs);
485 a->sysfs_fs_created = 0;
488 if (a->sysfs_vda_created) {
489 sysfs_remove_bin_file(&a->host->shost_dev.kobj, &bin_attr_vda);
490 a->sysfs_vda_created = 0;
493 if (a->sysfs_hw_created) {
494 sysfs_remove_bin_file(&a->host->shost_dev.kobj, &bin_attr_hw);
495 a->sysfs_hw_created = 0;
498 if (a->sysfs_live_nvram_created) {
499 sysfs_remove_bin_file(&a->host->shost_dev.kobj,
500 &bin_attr_live_nvram);
501 a->sysfs_live_nvram_created = 0;
504 if (a->sysfs_default_nvram_created) {
505 sysfs_remove_bin_file(&a->host->shost_dev.kobj,
506 &bin_attr_default_nvram);
507 a->sysfs_default_nvram_created = 0;
510 /* Clean up interrupts */
511 if (test_bit(AF2_IRQ_CLAIMED, &a->flags2)) {
512 esas2r_log_dev(ESAS2R_LOG_INFO,
513 &(a->pcid->dev),
514 "free_irq(%d) called", a->pcid->irq);
516 free_irq(a->pcid->irq, a);
517 esas2r_debug("IRQ released");
518 clear_bit(AF2_IRQ_CLAIMED, &a->flags2);
521 if (test_bit(AF2_MSI_ENABLED, &a->flags2)) {
522 pci_disable_msi(a->pcid);
523 clear_bit(AF2_MSI_ENABLED, &a->flags2);
524 esas2r_debug("MSI disabled");
527 if (a->inbound_list_md.virt_addr)
528 esas2r_initmem_free(a, &a->inbound_list_md);
530 if (a->outbound_list_md.virt_addr)
531 esas2r_initmem_free(a, &a->outbound_list_md);
533 list_for_each_entry_safe(memdesc, next, &a->free_sg_list_head,
534 next_desc) {
535 esas2r_initmem_free(a, memdesc);
538 /* Following frees everything allocated via alloc_vda_req */
539 list_for_each_entry_safe(memdesc, next, &a->vrq_mds_head, next_desc) {
540 esas2r_initmem_free(a, memdesc);
541 list_del(&memdesc->next_desc);
542 kfree(memdesc);
545 kfree(a->first_ae_req);
546 a->first_ae_req = NULL;
548 kfree(a->sg_list_mds);
549 a->sg_list_mds = NULL;
551 kfree(a->req_table);
552 a->req_table = NULL;
554 if (a->regs) {
555 esas2r_unmap_regions(a);
556 a->regs = NULL;
557 a->data_window = NULL;
558 esas2r_debug("regions unmapped");
562 /* Release/free allocated resources for specified adapters. */
563 void esas2r_kill_adapter(int i)
565 struct esas2r_adapter *a = esas2r_adapters[i];
567 if (a) {
568 unsigned long flags;
569 struct workqueue_struct *wq;
570 esas2r_debug("killing adapter %p [%d] ", a, i);
571 esas2r_fw_event_off(a);
572 esas2r_adapter_power_down(a, 0);
573 if (esas2r_buffered_ioctl &&
574 (a->pcid == esas2r_buffered_ioctl_pcid)) {
575 dma_free_coherent(&a->pcid->dev,
576 (size_t)esas2r_buffered_ioctl_size,
577 esas2r_buffered_ioctl,
578 esas2r_buffered_ioctl_addr);
579 esas2r_buffered_ioctl = NULL;
582 if (a->vda_buffer) {
583 dma_free_coherent(&a->pcid->dev,
584 (size_t)VDA_MAX_BUFFER_SIZE,
585 a->vda_buffer,
586 (dma_addr_t)a->ppvda_buffer);
587 a->vda_buffer = NULL;
589 if (a->fs_api_buffer) {
590 dma_free_coherent(&a->pcid->dev,
591 (size_t)a->fs_api_buffer_size,
592 a->fs_api_buffer,
593 (dma_addr_t)a->ppfs_api_buffer);
594 a->fs_api_buffer = NULL;
597 kfree(a->local_atto_ioctl);
598 a->local_atto_ioctl = NULL;
600 spin_lock_irqsave(&a->fw_event_lock, flags);
601 wq = a->fw_event_q;
602 a->fw_event_q = NULL;
603 spin_unlock_irqrestore(&a->fw_event_lock, flags);
604 if (wq)
605 destroy_workqueue(wq);
607 if (a->uncached) {
608 dma_free_coherent(&a->pcid->dev,
609 (size_t)a->uncached_size,
610 a->uncached,
611 (dma_addr_t)a->uncached_phys);
612 a->uncached = NULL;
613 esas2r_debug("uncached area freed");
616 esas2r_log_dev(ESAS2R_LOG_INFO,
617 &(a->pcid->dev),
618 "pci_disable_device() called. msix_enabled: %d "
619 "msi_enabled: %d irq: %d pin: %d",
620 a->pcid->msix_enabled,
621 a->pcid->msi_enabled,
622 a->pcid->irq,
623 a->pcid->pin);
625 esas2r_log_dev(ESAS2R_LOG_INFO,
626 &(a->pcid->dev),
627 "before pci_disable_device() enable_cnt: %d",
628 a->pcid->enable_cnt.counter);
630 pci_disable_device(a->pcid);
631 esas2r_log_dev(ESAS2R_LOG_INFO,
632 &(a->pcid->dev),
633 "after pci_disable_device() enable_cnt: %d",
634 a->pcid->enable_cnt.counter);
636 esas2r_log_dev(ESAS2R_LOG_INFO,
637 &(a->pcid->dev),
638 "pci_set_drv_data(%p, NULL) called",
639 a->pcid);
641 pci_set_drvdata(a->pcid, NULL);
642 esas2r_adapters[i] = NULL;
644 if (test_bit(AF2_INIT_DONE, &a->flags2)) {
645 clear_bit(AF2_INIT_DONE, &a->flags2);
647 set_bit(AF_DEGRADED_MODE, &a->flags);
649 esas2r_log_dev(ESAS2R_LOG_INFO,
650 &(a->host->shost_gendev),
651 "scsi_remove_host() called");
653 scsi_remove_host(a->host);
655 esas2r_log_dev(ESAS2R_LOG_INFO,
656 &(a->host->shost_gendev),
657 "scsi_host_put() called");
659 scsi_host_put(a->host);
664 int esas2r_cleanup(struct Scsi_Host *host)
666 struct esas2r_adapter *a;
667 int index;
669 if (host == NULL) {
670 int i;
672 esas2r_debug("esas2r_cleanup everything");
673 for (i = 0; i < MAX_ADAPTERS; i++)
674 esas2r_kill_adapter(i);
675 return -1;
678 esas2r_debug("esas2r_cleanup called for host %p", host);
679 a = (struct esas2r_adapter *)host->hostdata;
680 index = a->index;
681 esas2r_kill_adapter(index);
682 return index;
685 int esas2r_suspend(struct pci_dev *pdev, pm_message_t state)
687 struct Scsi_Host *host = pci_get_drvdata(pdev);
688 u32 device_state;
689 struct esas2r_adapter *a = (struct esas2r_adapter *)host->hostdata;
691 esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev), "suspending adapter()");
692 if (!a)
693 return -ENODEV;
695 esas2r_adapter_power_down(a, 1);
696 device_state = pci_choose_state(pdev, state);
697 esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
698 "pci_save_state() called");
699 pci_save_state(pdev);
700 esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
701 "pci_disable_device() called");
702 pci_disable_device(pdev);
703 esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
704 "pci_set_power_state() called");
705 pci_set_power_state(pdev, device_state);
706 esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev), "esas2r_suspend(): 0");
707 return 0;
710 int esas2r_resume(struct pci_dev *pdev)
712 struct Scsi_Host *host = pci_get_drvdata(pdev);
713 struct esas2r_adapter *a = (struct esas2r_adapter *)host->hostdata;
714 int rez;
716 esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev), "resuming adapter()");
717 esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
718 "pci_set_power_state(PCI_D0) "
719 "called");
720 pci_set_power_state(pdev, PCI_D0);
721 esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
722 "pci_enable_wake(PCI_D0, 0) "
723 "called");
724 pci_enable_wake(pdev, PCI_D0, 0);
725 esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
726 "pci_restore_state() called");
727 pci_restore_state(pdev);
728 esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
729 "pci_enable_device() called");
730 rez = pci_enable_device(pdev);
731 pci_set_master(pdev);
733 if (!a) {
734 rez = -ENODEV;
735 goto error_exit;
738 if (esas2r_map_regions(a) != 0) {
739 esas2r_log(ESAS2R_LOG_CRIT, "could not re-map PCI regions!");
740 rez = -ENOMEM;
741 goto error_exit;
744 /* Set up interupt mode */
745 esas2r_setup_interrupts(a, a->intr_mode);
748 * Disable chip interrupts to prevent spurious interrupts until we
749 * claim the IRQ.
751 esas2r_disable_chip_interrupts(a);
752 if (!esas2r_power_up(a, true)) {
753 esas2r_debug("yikes, esas2r_power_up failed");
754 rez = -ENOMEM;
755 goto error_exit;
758 esas2r_claim_interrupts(a);
760 if (test_bit(AF2_IRQ_CLAIMED, &a->flags2)) {
762 * Now that system interrupt(s) are claimed, we can enable
763 * chip interrupts.
765 esas2r_enable_chip_interrupts(a);
766 esas2r_kickoff_timer(a);
767 } else {
768 esas2r_debug("yikes, unable to claim IRQ");
769 esas2r_log(ESAS2R_LOG_CRIT, "could not re-claim IRQ!");
770 rez = -ENOMEM;
771 goto error_exit;
774 error_exit:
775 esas2r_log_dev(ESAS2R_LOG_CRIT, &(pdev->dev), "esas2r_resume(): %d",
776 rez);
777 return rez;
780 bool esas2r_set_degraded_mode(struct esas2r_adapter *a, char *error_str)
782 set_bit(AF_DEGRADED_MODE, &a->flags);
783 esas2r_log(ESAS2R_LOG_CRIT,
784 "setting adapter to degraded mode: %s\n", error_str);
785 return false;
788 u32 esas2r_get_uncached_size(struct esas2r_adapter *a)
790 return sizeof(struct esas2r_sas_nvram)
791 + ALIGN(ESAS2R_DISC_BUF_LEN, 8)
792 + ALIGN(sizeof(u32), 8) /* outbound list copy pointer */
794 + (num_sg_lists * (u16)sgl_page_size)
795 + ALIGN((num_requests + num_ae_requests + 1 +
796 ESAS2R_LIST_EXTRA) *
797 sizeof(struct esas2r_inbound_list_source_entry),
799 + ALIGN((num_requests + num_ae_requests + 1 +
800 ESAS2R_LIST_EXTRA) *
801 sizeof(struct atto_vda_ob_rsp), 8)
802 + 256; /* VDA request and buffer align */
805 static void esas2r_init_pci_cfg_space(struct esas2r_adapter *a)
807 int pcie_cap_reg;
809 pcie_cap_reg = pci_find_capability(a->pcid, PCI_CAP_ID_EXP);
810 if (pcie_cap_reg) {
811 u16 devcontrol;
813 pci_read_config_word(a->pcid, pcie_cap_reg + PCI_EXP_DEVCTL,
814 &devcontrol);
816 if ((devcontrol & PCI_EXP_DEVCTL_READRQ) >
817 PCI_EXP_DEVCTL_READRQ_512B) {
818 esas2r_log(ESAS2R_LOG_INFO,
819 "max read request size > 512B");
821 devcontrol &= ~PCI_EXP_DEVCTL_READRQ;
822 devcontrol |= PCI_EXP_DEVCTL_READRQ_512B;
823 pci_write_config_word(a->pcid,
824 pcie_cap_reg + PCI_EXP_DEVCTL,
825 devcontrol);
831 * Determine the organization of the uncached data area and
832 * finish initializing the adapter structure
834 bool esas2r_init_adapter_struct(struct esas2r_adapter *a,
835 void **uncached_area)
837 u32 i;
838 u8 *high;
839 struct esas2r_inbound_list_source_entry *element;
840 struct esas2r_request *rq;
841 struct esas2r_mem_desc *sgl;
843 spin_lock_init(&a->sg_list_lock);
844 spin_lock_init(&a->mem_lock);
845 spin_lock_init(&a->queue_lock);
847 a->targetdb_end = &a->targetdb[ESAS2R_MAX_TARGETS];
849 if (!alloc_vda_req(a, &a->general_req)) {
850 esas2r_hdebug(
851 "failed to allocate a VDA request for the general req!");
852 return false;
855 /* allocate requests for asynchronous events */
856 a->first_ae_req =
857 kzalloc(num_ae_requests * sizeof(struct esas2r_request),
858 GFP_KERNEL);
860 if (a->first_ae_req == NULL) {
861 esas2r_log(ESAS2R_LOG_CRIT,
862 "failed to allocate memory for asynchronous events");
863 return false;
866 /* allocate the S/G list memory descriptors */
867 a->sg_list_mds = kzalloc(
868 num_sg_lists * sizeof(struct esas2r_mem_desc), GFP_KERNEL);
870 if (a->sg_list_mds == NULL) {
871 esas2r_log(ESAS2R_LOG_CRIT,
872 "failed to allocate memory for s/g list descriptors");
873 return false;
876 /* allocate the request table */
877 a->req_table =
878 kzalloc((num_requests + num_ae_requests +
879 1) * sizeof(struct esas2r_request *), GFP_KERNEL);
881 if (a->req_table == NULL) {
882 esas2r_log(ESAS2R_LOG_CRIT,
883 "failed to allocate memory for the request table");
884 return false;
887 /* initialize PCI configuration space */
888 esas2r_init_pci_cfg_space(a);
891 * the thunder_stream boards all have a serial flash part that has a
892 * different base address on the AHB bus.
894 if ((a->pcid->subsystem_vendor == ATTO_VENDOR_ID)
895 && (a->pcid->subsystem_device & ATTO_SSDID_TBT))
896 a->flags2 |= AF2_THUNDERBOLT;
898 if (test_bit(AF2_THUNDERBOLT, &a->flags2))
899 a->flags2 |= AF2_SERIAL_FLASH;
901 if (a->pcid->subsystem_device == ATTO_TLSH_1068)
902 a->flags2 |= AF2_THUNDERLINK;
904 /* Uncached Area */
905 high = (u8 *)*uncached_area;
907 /* initialize the scatter/gather table pages */
909 for (i = 0, sgl = a->sg_list_mds; i < num_sg_lists; i++, sgl++) {
910 sgl->size = sgl_page_size;
912 list_add_tail(&sgl->next_desc, &a->free_sg_list_head);
914 if (!esas2r_initmem_alloc(a, sgl, ESAS2R_SGL_ALIGN)) {
915 /* Allow the driver to load if the minimum count met. */
916 if (i < NUM_SGL_MIN)
917 return false;
918 break;
922 /* compute the size of the lists */
923 a->list_size = num_requests + ESAS2R_LIST_EXTRA;
925 /* allocate the inbound list */
926 a->inbound_list_md.size = a->list_size *
927 sizeof(struct
928 esas2r_inbound_list_source_entry);
930 if (!esas2r_initmem_alloc(a, &a->inbound_list_md, ESAS2R_LIST_ALIGN)) {
931 esas2r_hdebug("failed to allocate IB list");
932 return false;
935 /* allocate the outbound list */
936 a->outbound_list_md.size = a->list_size *
937 sizeof(struct atto_vda_ob_rsp);
939 if (!esas2r_initmem_alloc(a, &a->outbound_list_md,
940 ESAS2R_LIST_ALIGN)) {
941 esas2r_hdebug("failed to allocate IB list");
942 return false;
945 /* allocate the NVRAM structure */
946 a->nvram = (struct esas2r_sas_nvram *)high;
947 high += sizeof(struct esas2r_sas_nvram);
949 /* allocate the discovery buffer */
950 a->disc_buffer = high;
951 high += ESAS2R_DISC_BUF_LEN;
952 high = PTR_ALIGN(high, 8);
954 /* allocate the outbound list copy pointer */
955 a->outbound_copy = (u32 volatile *)high;
956 high += sizeof(u32);
958 if (!test_bit(AF_NVR_VALID, &a->flags))
959 esas2r_nvram_set_defaults(a);
961 /* update the caller's uncached memory area pointer */
962 *uncached_area = (void *)high;
964 /* initialize the allocated memory */
965 if (test_bit(AF_FIRST_INIT, &a->flags)) {
966 memset(a->req_table, 0,
967 (num_requests + num_ae_requests +
968 1) * sizeof(struct esas2r_request *));
970 esas2r_targ_db_initialize(a);
972 /* prime parts of the inbound list */
973 element =
974 (struct esas2r_inbound_list_source_entry *)a->
975 inbound_list_md.
976 virt_addr;
978 for (i = 0; i < a->list_size; i++) {
979 element->address = 0;
980 element->reserved = 0;
981 element->length = cpu_to_le32(HWILSE_INTERFACE_F0
982 | (sizeof(union
983 atto_vda_req)
985 sizeof(u32)));
986 element++;
989 /* init the AE requests */
990 for (rq = a->first_ae_req, i = 0; i < num_ae_requests; rq++,
991 i++) {
992 INIT_LIST_HEAD(&rq->req_list);
993 if (!alloc_vda_req(a, rq)) {
994 esas2r_hdebug(
995 "failed to allocate a VDA request!");
996 return false;
999 esas2r_rq_init_request(rq, a);
1001 /* override the completion function */
1002 rq->comp_cb = esas2r_ae_complete;
1006 return true;
1009 /* This code will verify that the chip is operational. */
1010 bool esas2r_check_adapter(struct esas2r_adapter *a)
1012 u32 starttime;
1013 u32 doorbell;
1014 u64 ppaddr;
1015 u32 dw;
1018 * if the chip reset detected flag is set, we can bypass a bunch of
1019 * stuff.
1021 if (test_bit(AF_CHPRST_DETECTED, &a->flags))
1022 goto skip_chip_reset;
1025 * BEFORE WE DO ANYTHING, disable the chip interrupts! the boot driver
1026 * may have left them enabled or we may be recovering from a fault.
1028 esas2r_write_register_dword(a, MU_INT_MASK_OUT, ESAS2R_INT_DIS_MASK);
1029 esas2r_flush_register_dword(a, MU_INT_MASK_OUT);
1032 * wait for the firmware to become ready by forcing an interrupt and
1033 * waiting for a response.
1035 starttime = jiffies_to_msecs(jiffies);
1037 while (true) {
1038 esas2r_force_interrupt(a);
1039 doorbell = esas2r_read_register_dword(a, MU_DOORBELL_OUT);
1040 if (doorbell == 0xFFFFFFFF) {
1042 * Give the firmware up to two seconds to enable
1043 * register access after a reset.
1045 if ((jiffies_to_msecs(jiffies) - starttime) > 2000)
1046 return esas2r_set_degraded_mode(a,
1047 "unable to access registers");
1048 } else if (doorbell & DRBL_FORCE_INT) {
1049 u32 ver = (doorbell & DRBL_FW_VER_MSK);
1052 * This driver supports version 0 and version 1 of
1053 * the API
1055 esas2r_write_register_dword(a, MU_DOORBELL_OUT,
1056 doorbell);
1058 if (ver == DRBL_FW_VER_0) {
1059 set_bit(AF_LEGACY_SGE_MODE, &a->flags);
1061 a->max_vdareq_size = 128;
1062 a->build_sgl = esas2r_build_sg_list_sge;
1063 } else if (ver == DRBL_FW_VER_1) {
1064 clear_bit(AF_LEGACY_SGE_MODE, &a->flags);
1066 a->max_vdareq_size = 1024;
1067 a->build_sgl = esas2r_build_sg_list_prd;
1068 } else {
1069 return esas2r_set_degraded_mode(a,
1070 "unknown firmware version");
1072 break;
1075 schedule_timeout_interruptible(msecs_to_jiffies(100));
1077 if ((jiffies_to_msecs(jiffies) - starttime) > 180000) {
1078 esas2r_hdebug("FW ready TMO");
1079 esas2r_bugon();
1081 return esas2r_set_degraded_mode(a,
1082 "firmware start has timed out");
1086 /* purge any asynchronous events since we will repost them later */
1087 esas2r_write_register_dword(a, MU_DOORBELL_IN, DRBL_MSG_IFC_DOWN);
1088 starttime = jiffies_to_msecs(jiffies);
1090 while (true) {
1091 doorbell = esas2r_read_register_dword(a, MU_DOORBELL_OUT);
1092 if (doorbell & DRBL_MSG_IFC_DOWN) {
1093 esas2r_write_register_dword(a, MU_DOORBELL_OUT,
1094 doorbell);
1095 break;
1098 schedule_timeout_interruptible(msecs_to_jiffies(50));
1100 if ((jiffies_to_msecs(jiffies) - starttime) > 3000) {
1101 esas2r_hdebug("timeout waiting for interface down");
1102 break;
1105 skip_chip_reset:
1107 * first things first, before we go changing any of these registers
1108 * disable the communication lists.
1110 dw = esas2r_read_register_dword(a, MU_IN_LIST_CONFIG);
1111 dw &= ~MU_ILC_ENABLE;
1112 esas2r_write_register_dword(a, MU_IN_LIST_CONFIG, dw);
1113 dw = esas2r_read_register_dword(a, MU_OUT_LIST_CONFIG);
1114 dw &= ~MU_OLC_ENABLE;
1115 esas2r_write_register_dword(a, MU_OUT_LIST_CONFIG, dw);
1117 /* configure the communication list addresses */
1118 ppaddr = a->inbound_list_md.phys_addr;
1119 esas2r_write_register_dword(a, MU_IN_LIST_ADDR_LO,
1120 lower_32_bits(ppaddr));
1121 esas2r_write_register_dword(a, MU_IN_LIST_ADDR_HI,
1122 upper_32_bits(ppaddr));
1123 ppaddr = a->outbound_list_md.phys_addr;
1124 esas2r_write_register_dword(a, MU_OUT_LIST_ADDR_LO,
1125 lower_32_bits(ppaddr));
1126 esas2r_write_register_dword(a, MU_OUT_LIST_ADDR_HI,
1127 upper_32_bits(ppaddr));
1128 ppaddr = a->uncached_phys +
1129 ((u8 *)a->outbound_copy - a->uncached);
1130 esas2r_write_register_dword(a, MU_OUT_LIST_COPY_PTR_LO,
1131 lower_32_bits(ppaddr));
1132 esas2r_write_register_dword(a, MU_OUT_LIST_COPY_PTR_HI,
1133 upper_32_bits(ppaddr));
1135 /* reset the read and write pointers */
1136 *a->outbound_copy =
1137 a->last_write =
1138 a->last_read = a->list_size - 1;
1139 set_bit(AF_COMM_LIST_TOGGLE, &a->flags);
1140 esas2r_write_register_dword(a, MU_IN_LIST_WRITE, MU_ILW_TOGGLE |
1141 a->last_write);
1142 esas2r_write_register_dword(a, MU_OUT_LIST_COPY, MU_OLC_TOGGLE |
1143 a->last_write);
1144 esas2r_write_register_dword(a, MU_IN_LIST_READ, MU_ILR_TOGGLE |
1145 a->last_write);
1146 esas2r_write_register_dword(a, MU_OUT_LIST_WRITE,
1147 MU_OLW_TOGGLE | a->last_write);
1149 /* configure the interface select fields */
1150 dw = esas2r_read_register_dword(a, MU_IN_LIST_IFC_CONFIG);
1151 dw &= ~(MU_ILIC_LIST | MU_ILIC_DEST);
1152 esas2r_write_register_dword(a, MU_IN_LIST_IFC_CONFIG,
1153 (dw | MU_ILIC_LIST_F0 | MU_ILIC_DEST_DDR));
1154 dw = esas2r_read_register_dword(a, MU_OUT_LIST_IFC_CONFIG);
1155 dw &= ~(MU_OLIC_LIST | MU_OLIC_SOURCE);
1156 esas2r_write_register_dword(a, MU_OUT_LIST_IFC_CONFIG,
1157 (dw | MU_OLIC_LIST_F0 |
1158 MU_OLIC_SOURCE_DDR));
1160 /* finish configuring the communication lists */
1161 dw = esas2r_read_register_dword(a, MU_IN_LIST_CONFIG);
1162 dw &= ~(MU_ILC_ENTRY_MASK | MU_ILC_NUMBER_MASK);
1163 dw |= MU_ILC_ENTRY_4_DW | MU_ILC_DYNAMIC_SRC
1164 | (a->list_size << MU_ILC_NUMBER_SHIFT);
1165 esas2r_write_register_dword(a, MU_IN_LIST_CONFIG, dw);
1166 dw = esas2r_read_register_dword(a, MU_OUT_LIST_CONFIG);
1167 dw &= ~(MU_OLC_ENTRY_MASK | MU_OLC_NUMBER_MASK);
1168 dw |= MU_OLC_ENTRY_4_DW | (a->list_size << MU_OLC_NUMBER_SHIFT);
1169 esas2r_write_register_dword(a, MU_OUT_LIST_CONFIG, dw);
1172 * notify the firmware that we're done setting up the communication
1173 * list registers. wait here until the firmware is done configuring
1174 * its lists. it will signal that it is done by enabling the lists.
1176 esas2r_write_register_dword(a, MU_DOORBELL_IN, DRBL_MSG_IFC_INIT);
1177 starttime = jiffies_to_msecs(jiffies);
1179 while (true) {
1180 doorbell = esas2r_read_register_dword(a, MU_DOORBELL_OUT);
1181 if (doorbell & DRBL_MSG_IFC_INIT) {
1182 esas2r_write_register_dword(a, MU_DOORBELL_OUT,
1183 doorbell);
1184 break;
1187 schedule_timeout_interruptible(msecs_to_jiffies(100));
1189 if ((jiffies_to_msecs(jiffies) - starttime) > 3000) {
1190 esas2r_hdebug(
1191 "timeout waiting for communication list init");
1192 esas2r_bugon();
1193 return esas2r_set_degraded_mode(a,
1194 "timeout waiting for communication list init");
1199 * flag whether the firmware supports the power down doorbell. we
1200 * determine this by reading the inbound doorbell enable mask.
1202 doorbell = esas2r_read_register_dword(a, MU_DOORBELL_IN_ENB);
1203 if (doorbell & DRBL_POWER_DOWN)
1204 set_bit(AF2_VDA_POWER_DOWN, &a->flags2);
1205 else
1206 clear_bit(AF2_VDA_POWER_DOWN, &a->flags2);
1209 * enable assertion of outbound queue and doorbell interrupts in the
1210 * main interrupt cause register.
1212 esas2r_write_register_dword(a, MU_OUT_LIST_INT_MASK, MU_OLIS_MASK);
1213 esas2r_write_register_dword(a, MU_DOORBELL_OUT_ENB, DRBL_ENB_MASK);
1214 return true;
1217 /* Process the initialization message just completed and format the next one. */
1218 static bool esas2r_format_init_msg(struct esas2r_adapter *a,
1219 struct esas2r_request *rq)
1221 u32 msg = a->init_msg;
1222 struct atto_vda_cfg_init *ci;
1224 a->init_msg = 0;
1226 switch (msg) {
1227 case ESAS2R_INIT_MSG_START:
1228 case ESAS2R_INIT_MSG_REINIT:
1230 struct timeval now;
1231 do_gettimeofday(&now);
1232 esas2r_hdebug("CFG init");
1233 esas2r_build_cfg_req(a,
1235 VDA_CFG_INIT,
1237 NULL);
1238 ci = (struct atto_vda_cfg_init *)&rq->vrq->cfg.data.init;
1239 ci->sgl_page_size = cpu_to_le32(sgl_page_size);
1240 ci->epoch_time = cpu_to_le32(now.tv_sec);
1241 rq->flags |= RF_FAILURE_OK;
1242 a->init_msg = ESAS2R_INIT_MSG_INIT;
1243 break;
1246 case ESAS2R_INIT_MSG_INIT:
1247 if (rq->req_stat == RS_SUCCESS) {
1248 u32 major;
1249 u32 minor;
1250 u16 fw_release;
1252 a->fw_version = le16_to_cpu(
1253 rq->func_rsp.cfg_rsp.vda_version);
1254 a->fw_build = rq->func_rsp.cfg_rsp.fw_build;
1255 fw_release = le16_to_cpu(
1256 rq->func_rsp.cfg_rsp.fw_release);
1257 major = LOBYTE(fw_release);
1258 minor = HIBYTE(fw_release);
1259 a->fw_version += (major << 16) + (minor << 24);
1260 } else {
1261 esas2r_hdebug("FAILED");
1265 * the 2.71 and earlier releases of R6xx firmware did not error
1266 * unsupported config requests correctly.
1269 if ((test_bit(AF2_THUNDERBOLT, &a->flags2))
1270 || (be32_to_cpu(a->fw_version) > 0x00524702)) {
1271 esas2r_hdebug("CFG get init");
1272 esas2r_build_cfg_req(a,
1274 VDA_CFG_GET_INIT2,
1275 sizeof(struct atto_vda_cfg_init),
1276 NULL);
1278 rq->vrq->cfg.sg_list_offset = offsetof(
1279 struct atto_vda_cfg_req,
1280 data.sge);
1281 rq->vrq->cfg.data.prde.ctl_len =
1282 cpu_to_le32(sizeof(struct atto_vda_cfg_init));
1283 rq->vrq->cfg.data.prde.address = cpu_to_le64(
1284 rq->vrq_md->phys_addr +
1285 sizeof(union atto_vda_req));
1286 rq->flags |= RF_FAILURE_OK;
1287 a->init_msg = ESAS2R_INIT_MSG_GET_INIT;
1288 break;
1291 case ESAS2R_INIT_MSG_GET_INIT:
1292 if (msg == ESAS2R_INIT_MSG_GET_INIT) {
1293 ci = (struct atto_vda_cfg_init *)rq->data_buf;
1294 if (rq->req_stat == RS_SUCCESS) {
1295 a->num_targets_backend =
1296 le32_to_cpu(ci->num_targets_backend);
1297 a->ioctl_tunnel =
1298 le32_to_cpu(ci->ioctl_tunnel);
1299 } else {
1300 esas2r_hdebug("FAILED");
1303 /* fall through */
1305 default:
1306 rq->req_stat = RS_SUCCESS;
1307 return false;
1309 return true;
1313 * Perform initialization messages via the request queue. Messages are
1314 * performed with interrupts disabled.
1316 bool esas2r_init_msgs(struct esas2r_adapter *a)
1318 bool success = true;
1319 struct esas2r_request *rq = &a->general_req;
1321 esas2r_rq_init_request(rq, a);
1322 rq->comp_cb = esas2r_dummy_complete;
1324 if (a->init_msg == 0)
1325 a->init_msg = ESAS2R_INIT_MSG_REINIT;
1327 while (a->init_msg) {
1328 if (esas2r_format_init_msg(a, rq)) {
1329 unsigned long flags;
1330 while (true) {
1331 spin_lock_irqsave(&a->queue_lock, flags);
1332 esas2r_start_vda_request(a, rq);
1333 spin_unlock_irqrestore(&a->queue_lock, flags);
1334 esas2r_wait_request(a, rq);
1335 if (rq->req_stat != RS_PENDING)
1336 break;
1340 if (rq->req_stat == RS_SUCCESS
1341 || ((rq->flags & RF_FAILURE_OK)
1342 && rq->req_stat != RS_TIMEOUT))
1343 continue;
1345 esas2r_log(ESAS2R_LOG_CRIT, "init message %x failed (%x, %x)",
1346 a->init_msg, rq->req_stat, rq->flags);
1347 a->init_msg = ESAS2R_INIT_MSG_START;
1348 success = false;
1349 break;
1352 esas2r_rq_destroy_request(rq, a);
1353 return success;
1356 /* Initialize the adapter chip */
1357 bool esas2r_init_adapter_hw(struct esas2r_adapter *a, bool init_poll)
1359 bool rslt = false;
1360 struct esas2r_request *rq;
1361 u32 i;
1363 if (test_bit(AF_DEGRADED_MODE, &a->flags))
1364 goto exit;
1366 if (!test_bit(AF_NVR_VALID, &a->flags)) {
1367 if (!esas2r_nvram_read_direct(a))
1368 esas2r_log(ESAS2R_LOG_WARN,
1369 "invalid/missing NVRAM parameters");
1372 if (!esas2r_init_msgs(a)) {
1373 esas2r_set_degraded_mode(a, "init messages failed");
1374 goto exit;
1377 /* The firmware is ready. */
1378 clear_bit(AF_DEGRADED_MODE, &a->flags);
1379 clear_bit(AF_CHPRST_PENDING, &a->flags);
1381 /* Post all the async event requests */
1382 for (i = 0, rq = a->first_ae_req; i < num_ae_requests; i++, rq++)
1383 esas2r_start_ae_request(a, rq);
1385 if (!a->flash_rev[0])
1386 esas2r_read_flash_rev(a);
1388 if (!a->image_type[0])
1389 esas2r_read_image_type(a);
1391 if (a->fw_version == 0)
1392 a->fw_rev[0] = 0;
1393 else
1394 sprintf(a->fw_rev, "%1d.%02d",
1395 (int)LOBYTE(HIWORD(a->fw_version)),
1396 (int)HIBYTE(HIWORD(a->fw_version)));
1398 esas2r_hdebug("firmware revision: %s", a->fw_rev);
1400 if (test_bit(AF_CHPRST_DETECTED, &a->flags)
1401 && (test_bit(AF_FIRST_INIT, &a->flags))) {
1402 esas2r_enable_chip_interrupts(a);
1403 return true;
1406 /* initialize discovery */
1407 esas2r_disc_initialize(a);
1410 * wait for the device wait time to expire here if requested. this is
1411 * usually requested during initial driver load and possibly when
1412 * resuming from a low power state. deferred device waiting will use
1413 * interrupts. chip reset recovery always defers device waiting to
1414 * avoid being in a TASKLET too long.
1416 if (init_poll) {
1417 u32 currtime = a->disc_start_time;
1418 u32 nexttick = 100;
1419 u32 deltatime;
1422 * Block Tasklets from getting scheduled and indicate this is
1423 * polled discovery.
1425 set_bit(AF_TASKLET_SCHEDULED, &a->flags);
1426 set_bit(AF_DISC_POLLED, &a->flags);
1429 * Temporarily bring the disable count to zero to enable
1430 * deferred processing. Note that the count is already zero
1431 * after the first initialization.
1433 if (test_bit(AF_FIRST_INIT, &a->flags))
1434 atomic_dec(&a->disable_cnt);
1436 while (test_bit(AF_DISC_PENDING, &a->flags)) {
1437 schedule_timeout_interruptible(msecs_to_jiffies(100));
1440 * Determine the need for a timer tick based on the
1441 * delta time between this and the last iteration of
1442 * this loop. We don't use the absolute time because
1443 * then we would have to worry about when nexttick
1444 * wraps and currtime hasn't yet.
1446 deltatime = jiffies_to_msecs(jiffies) - currtime;
1447 currtime += deltatime;
1450 * Process any waiting discovery as long as the chip is
1451 * up. If a chip reset happens during initial polling,
1452 * we have to make sure the timer tick processes the
1453 * doorbell indicating the firmware is ready.
1455 if (!test_bit(AF_CHPRST_PENDING, &a->flags))
1456 esas2r_disc_check_for_work(a);
1458 /* Simulate a timer tick. */
1459 if (nexttick <= deltatime) {
1461 /* Time for a timer tick */
1462 nexttick += 100;
1463 esas2r_timer_tick(a);
1466 if (nexttick > deltatime)
1467 nexttick -= deltatime;
1469 /* Do any deferred processing */
1470 if (esas2r_is_tasklet_pending(a))
1471 esas2r_do_tasklet_tasks(a);
1475 if (test_bit(AF_FIRST_INIT, &a->flags))
1476 atomic_inc(&a->disable_cnt);
1478 clear_bit(AF_DISC_POLLED, &a->flags);
1479 clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
1483 esas2r_targ_db_report_changes(a);
1486 * For cases where (a) the initialization messages processing may
1487 * handle an interrupt for a port event and a discovery is waiting, but
1488 * we are not waiting for devices, or (b) the device wait time has been
1489 * exhausted but there is still discovery pending, start any leftover
1490 * discovery in interrupt driven mode.
1492 esas2r_disc_start_waiting(a);
1494 /* Enable chip interrupts */
1495 a->int_mask = ESAS2R_INT_STS_MASK;
1496 esas2r_enable_chip_interrupts(a);
1497 esas2r_enable_heartbeat(a);
1498 rslt = true;
1500 exit:
1502 * Regardless of whether initialization was successful, certain things
1503 * need to get done before we exit.
1506 if (test_bit(AF_CHPRST_DETECTED, &a->flags) &&
1507 test_bit(AF_FIRST_INIT, &a->flags)) {
1509 * Reinitialization was performed during the first
1510 * initialization. Only clear the chip reset flag so the
1511 * original device polling is not cancelled.
1513 if (!rslt)
1514 clear_bit(AF_CHPRST_PENDING, &a->flags);
1515 } else {
1516 /* First initialization or a subsequent re-init is complete. */
1517 if (!rslt) {
1518 clear_bit(AF_CHPRST_PENDING, &a->flags);
1519 clear_bit(AF_DISC_PENDING, &a->flags);
1523 /* Enable deferred processing after the first initialization. */
1524 if (test_bit(AF_FIRST_INIT, &a->flags)) {
1525 clear_bit(AF_FIRST_INIT, &a->flags);
1527 if (atomic_dec_return(&a->disable_cnt) == 0)
1528 esas2r_do_deferred_processes(a);
1532 return rslt;
1535 void esas2r_reset_adapter(struct esas2r_adapter *a)
1537 set_bit(AF_OS_RESET, &a->flags);
1538 esas2r_local_reset_adapter(a);
1539 esas2r_schedule_tasklet(a);
1542 void esas2r_reset_chip(struct esas2r_adapter *a)
1544 if (!esas2r_is_adapter_present(a))
1545 return;
1548 * Before we reset the chip, save off the VDA core dump. The VDA core
1549 * dump is located in the upper 512KB of the onchip SRAM. Make sure
1550 * to not overwrite a previous crash that was saved.
1552 if (test_bit(AF2_COREDUMP_AVAIL, &a->flags2) &&
1553 !test_bit(AF2_COREDUMP_SAVED, &a->flags2)) {
1554 esas2r_read_mem_block(a,
1555 a->fw_coredump_buff,
1556 MW_DATA_ADDR_SRAM + 0x80000,
1557 ESAS2R_FWCOREDUMP_SZ);
1559 set_bit(AF2_COREDUMP_SAVED, &a->flags2);
1562 clear_bit(AF2_COREDUMP_AVAIL, &a->flags2);
1564 /* Reset the chip */
1565 if (a->pcid->revision == MVR_FREY_B2)
1566 esas2r_write_register_dword(a, MU_CTL_STATUS_IN_B2,
1567 MU_CTL_IN_FULL_RST2);
1568 else
1569 esas2r_write_register_dword(a, MU_CTL_STATUS_IN,
1570 MU_CTL_IN_FULL_RST);
1573 /* Stall a little while to let the reset condition clear */
1574 mdelay(10);
1577 static void esas2r_power_down_notify_firmware(struct esas2r_adapter *a)
1579 u32 starttime;
1580 u32 doorbell;
1582 esas2r_write_register_dword(a, MU_DOORBELL_IN, DRBL_POWER_DOWN);
1583 starttime = jiffies_to_msecs(jiffies);
1585 while (true) {
1586 doorbell = esas2r_read_register_dword(a, MU_DOORBELL_OUT);
1587 if (doorbell & DRBL_POWER_DOWN) {
1588 esas2r_write_register_dword(a, MU_DOORBELL_OUT,
1589 doorbell);
1590 break;
1593 schedule_timeout_interruptible(msecs_to_jiffies(100));
1595 if ((jiffies_to_msecs(jiffies) - starttime) > 30000) {
1596 esas2r_hdebug("Timeout waiting for power down");
1597 break;
1603 * Perform power management processing including managing device states, adapter
1604 * states, interrupts, and I/O.
1606 void esas2r_power_down(struct esas2r_adapter *a)
1608 set_bit(AF_POWER_MGT, &a->flags);
1609 set_bit(AF_POWER_DOWN, &a->flags);
1611 if (!test_bit(AF_DEGRADED_MODE, &a->flags)) {
1612 u32 starttime;
1613 u32 doorbell;
1616 * We are currently running OK and will be reinitializing later.
1617 * increment the disable count to coordinate with
1618 * esas2r_init_adapter. We don't have to do this in degraded
1619 * mode since we never enabled interrupts in the first place.
1621 esas2r_disable_chip_interrupts(a);
1622 esas2r_disable_heartbeat(a);
1624 /* wait for any VDA activity to clear before continuing */
1625 esas2r_write_register_dword(a, MU_DOORBELL_IN,
1626 DRBL_MSG_IFC_DOWN);
1627 starttime = jiffies_to_msecs(jiffies);
1629 while (true) {
1630 doorbell =
1631 esas2r_read_register_dword(a, MU_DOORBELL_OUT);
1632 if (doorbell & DRBL_MSG_IFC_DOWN) {
1633 esas2r_write_register_dword(a, MU_DOORBELL_OUT,
1634 doorbell);
1635 break;
1638 schedule_timeout_interruptible(msecs_to_jiffies(100));
1640 if ((jiffies_to_msecs(jiffies) - starttime) > 3000) {
1641 esas2r_hdebug(
1642 "timeout waiting for interface down");
1643 break;
1648 * For versions of firmware that support it tell them the driver
1649 * is powering down.
1651 if (test_bit(AF2_VDA_POWER_DOWN, &a->flags2))
1652 esas2r_power_down_notify_firmware(a);
1655 /* Suspend I/O processing. */
1656 set_bit(AF_OS_RESET, &a->flags);
1657 set_bit(AF_DISC_PENDING, &a->flags);
1658 set_bit(AF_CHPRST_PENDING, &a->flags);
1660 esas2r_process_adapter_reset(a);
1662 /* Remove devices now that I/O is cleaned up. */
1663 a->prev_dev_cnt = esas2r_targ_db_get_tgt_cnt(a);
1664 esas2r_targ_db_remove_all(a, false);
1668 * Perform power management processing including managing device states, adapter
1669 * states, interrupts, and I/O.
1671 bool esas2r_power_up(struct esas2r_adapter *a, bool init_poll)
1673 bool ret;
1675 clear_bit(AF_POWER_DOWN, &a->flags);
1676 esas2r_init_pci_cfg_space(a);
1677 set_bit(AF_FIRST_INIT, &a->flags);
1678 atomic_inc(&a->disable_cnt);
1680 /* reinitialize the adapter */
1681 ret = esas2r_check_adapter(a);
1682 if (!esas2r_init_adapter_hw(a, init_poll))
1683 ret = false;
1685 /* send the reset asynchronous event */
1686 esas2r_send_reset_ae(a, true);
1688 /* clear this flag after initialization. */
1689 clear_bit(AF_POWER_MGT, &a->flags);
1690 return ret;
1693 bool esas2r_is_adapter_present(struct esas2r_adapter *a)
1695 if (test_bit(AF_NOT_PRESENT, &a->flags))
1696 return false;
1698 if (esas2r_read_register_dword(a, MU_DOORBELL_OUT) == 0xFFFFFFFF) {
1699 set_bit(AF_NOT_PRESENT, &a->flags);
1701 return false;
1703 return true;
1706 const char *esas2r_get_model_name(struct esas2r_adapter *a)
1708 switch (a->pcid->subsystem_device) {
1709 case ATTO_ESAS_R680:
1710 return "ATTO ExpressSAS R680";
1712 case ATTO_ESAS_R608:
1713 return "ATTO ExpressSAS R608";
1715 case ATTO_ESAS_R60F:
1716 return "ATTO ExpressSAS R60F";
1718 case ATTO_ESAS_R6F0:
1719 return "ATTO ExpressSAS R6F0";
1721 case ATTO_ESAS_R644:
1722 return "ATTO ExpressSAS R644";
1724 case ATTO_ESAS_R648:
1725 return "ATTO ExpressSAS R648";
1727 case ATTO_TSSC_3808:
1728 return "ATTO ThunderStream SC 3808D";
1730 case ATTO_TSSC_3808E:
1731 return "ATTO ThunderStream SC 3808E";
1733 case ATTO_TLSH_1068:
1734 return "ATTO ThunderLink SH 1068";
1737 return "ATTO SAS Controller";
1740 const char *esas2r_get_model_name_short(struct esas2r_adapter *a)
1742 switch (a->pcid->subsystem_device) {
1743 case ATTO_ESAS_R680:
1744 return "R680";
1746 case ATTO_ESAS_R608:
1747 return "R608";
1749 case ATTO_ESAS_R60F:
1750 return "R60F";
1752 case ATTO_ESAS_R6F0:
1753 return "R6F0";
1755 case ATTO_ESAS_R644:
1756 return "R644";
1758 case ATTO_ESAS_R648:
1759 return "R648";
1761 case ATTO_TSSC_3808:
1762 return "SC 3808D";
1764 case ATTO_TSSC_3808E:
1765 return "SC 3808E";
1767 case ATTO_TLSH_1068:
1768 return "SH 1068";
1771 return "unknown";