2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc.
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
8 * Copyright (c) 2000-2010 Adaptec, Inc.
9 * 2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10 * 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 * Abstract: This supports the initialization of the host adapter commuication interface.
30 * This is a platform dependent module for the pci cyclone board.
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36 #include <linux/types.h>
37 #include <linux/pci.h>
38 #include <linux/spinlock.h>
39 #include <linux/slab.h>
40 #include <linux/blkdev.h>
41 #include <linux/delay.h>
42 #include <linux/completion.h>
44 #include <scsi/scsi_host.h>
48 struct aac_common aac_config = {
52 static inline int aac_is_msix_mode(struct aac_dev *dev)
56 if (dev->pdev->device == PMC_DEVICE_S6 ||
57 dev->pdev->device == PMC_DEVICE_S7 ||
58 dev->pdev->device == PMC_DEVICE_S8) {
59 status = src_readl(dev, MUnit.OMR);
61 return (status & AAC_INT_MODE_MSIX);
64 static inline void aac_change_to_intx(struct aac_dev *dev)
66 aac_src_access_devreg(dev, AAC_DISABLE_MSIX);
67 aac_src_access_devreg(dev, AAC_ENABLE_INTX);
70 static int aac_alloc_comm(struct aac_dev *dev, void **commaddr, unsigned long commsize, unsigned long commalign)
73 unsigned long size, align;
74 const unsigned long fibsize = dev->max_fib_size;
75 const unsigned long printfbufsiz = 256;
76 unsigned long host_rrq_size, aac_init_size;
79 unsigned long aac_max_hostphysmempages;
81 if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) ||
82 (dev->comm_interface == AAC_COMM_MESSAGE_TYPE2) ||
83 (dev->comm_interface == AAC_COMM_MESSAGE_TYPE3 &&
86 (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB)
88 aac_init_size = sizeof(union aac_init);
89 } else if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE3 &&
91 host_rrq_size = (dev->scsi_host_ptr->can_queue
92 + AAC_NUM_MGT_FIB) * sizeof(u32) * AAC_MAX_MSIX;
93 aac_init_size = sizeof(union aac_init) +
94 (AAC_MAX_HRRQ - 1) * sizeof(struct _rrq);
97 aac_init_size = sizeof(union aac_init);
99 size = fibsize + aac_init_size + commsize + commalign +
100 printfbufsiz + host_rrq_size;
102 base = pci_alloc_consistent(dev->pdev, size, &phys);
105 printk(KERN_ERR "aacraid: unable to create mapping.\n");
109 dev->comm_addr = (void *)base;
110 dev->comm_phys = phys;
111 dev->comm_size = size;
113 if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) ||
114 (dev->comm_interface == AAC_COMM_MESSAGE_TYPE2) ||
115 (dev->comm_interface == AAC_COMM_MESSAGE_TYPE3)) {
116 dev->host_rrq = (u32 *)(base + fibsize);
117 dev->host_rrq_pa = phys + fibsize;
118 memset(dev->host_rrq, 0, host_rrq_size);
121 dev->init = (union aac_init *)(base + fibsize + host_rrq_size);
122 dev->init_pa = phys + fibsize + host_rrq_size;
126 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) {
130 init->r8.init_struct_revision =
131 cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_8);
132 init->r8.init_flags = cpu_to_le32(INITFLAGS_NEW_COMM_SUPPORTED |
133 INITFLAGS_DRIVER_USES_UTC_TIME |
134 INITFLAGS_DRIVER_SUPPORTS_PM);
135 init->r8.init_flags |=
136 cpu_to_le32(INITFLAGS_DRIVER_SUPPORTS_HBA_MODE);
137 init->r8.rr_queue_count = cpu_to_le32(dev->max_msix);
138 init->r8.max_io_size =
139 cpu_to_le32(dev->scsi_host_ptr->max_sectors << 9);
140 init->r8.max_num_aif = init->r8.reserved1 =
141 init->r8.reserved2 = 0;
143 for (i = 0; i < dev->max_msix; i++) {
144 addr = (u64)dev->host_rrq_pa + dev->vector_cap * i *
146 init->r8.rrq[i].host_addr_high = cpu_to_le32(
147 upper_32_bits(addr));
148 init->r8.rrq[i].host_addr_low = cpu_to_le32(
149 lower_32_bits(addr));
150 init->r8.rrq[i].msix_id = i;
151 init->r8.rrq[i].element_count = cpu_to_le16(
152 (u16)dev->vector_cap);
153 init->r8.rrq[i].comp_thresh =
154 init->r8.rrq[i].unused = 0;
157 pr_warn("aacraid: Comm Interface type3 enabled\n");
159 init->r7.init_struct_revision =
160 cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION);
161 if (dev->max_fib_size != sizeof(struct hw_fib))
162 init->r7.init_struct_revision =
163 cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_4);
164 init->r7.no_of_msix_vectors = cpu_to_le32(SA_MINIPORT_REVISION);
165 init->r7.fsrev = cpu_to_le32(dev->fsrev);
168 * Adapter Fibs are the first thing allocated so that they
171 dev->aif_base_va = (struct hw_fib *)base;
173 init->r7.adapter_fibs_virtual_address = 0;
174 init->r7.adapter_fibs_physical_address = cpu_to_le32((u32)phys);
175 init->r7.adapter_fibs_size = cpu_to_le32(fibsize);
176 init->r7.adapter_fib_align = cpu_to_le32(sizeof(struct hw_fib));
179 * number of 4k pages of host physical memory. The aacraid fw
180 * needs this number to be less than 4gb worth of pages. New
181 * firmware doesn't have any issues with the mapping system, but
182 * older Firmware did, and had *troubles* dealing with the math
183 * overloading past 32 bits, thus we must limit this field.
185 aac_max_hostphysmempages =
186 dma_get_required_mask(&dev->pdev->dev) >> 12;
187 if (aac_max_hostphysmempages < AAC_MAX_HOSTPHYSMEMPAGES)
188 init->r7.host_phys_mem_pages =
189 cpu_to_le32(aac_max_hostphysmempages);
191 init->r7.host_phys_mem_pages =
192 cpu_to_le32(AAC_MAX_HOSTPHYSMEMPAGES);
194 init->r7.init_flags =
195 cpu_to_le32(INITFLAGS_DRIVER_USES_UTC_TIME |
196 INITFLAGS_DRIVER_SUPPORTS_PM);
197 init->r7.max_io_commands =
198 cpu_to_le32(dev->scsi_host_ptr->can_queue +
200 init->r7.max_io_size =
201 cpu_to_le32(dev->scsi_host_ptr->max_sectors << 9);
202 init->r7.max_fib_size = cpu_to_le32(dev->max_fib_size);
203 init->r7.max_num_aif = cpu_to_le32(dev->max_num_aif);
205 if (dev->comm_interface == AAC_COMM_MESSAGE) {
206 init->r7.init_flags |=
207 cpu_to_le32(INITFLAGS_NEW_COMM_SUPPORTED);
208 pr_warn("aacraid: Comm Interface enabled\n");
209 } else if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) {
210 init->r7.init_struct_revision =
211 cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_6);
212 init->r7.init_flags |=
213 cpu_to_le32(INITFLAGS_NEW_COMM_SUPPORTED |
214 INITFLAGS_NEW_COMM_TYPE1_SUPPORTED |
215 INITFLAGS_FAST_JBOD_SUPPORTED);
216 init->r7.host_rrq_addr_high =
217 cpu_to_le32(upper_32_bits(dev->host_rrq_pa));
218 init->r7.host_rrq_addr_low =
219 cpu_to_le32(lower_32_bits(dev->host_rrq_pa));
220 pr_warn("aacraid: Comm Interface type1 enabled\n");
221 } else if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE2) {
222 init->r7.init_struct_revision =
223 cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_7);
224 init->r7.init_flags |=
225 cpu_to_le32(INITFLAGS_NEW_COMM_SUPPORTED |
226 INITFLAGS_NEW_COMM_TYPE2_SUPPORTED |
227 INITFLAGS_FAST_JBOD_SUPPORTED);
228 init->r7.host_rrq_addr_high =
229 cpu_to_le32(upper_32_bits(dev->host_rrq_pa));
230 init->r7.host_rrq_addr_low =
231 cpu_to_le32(lower_32_bits(dev->host_rrq_pa));
232 init->r7.no_of_msix_vectors =
233 cpu_to_le32(dev->max_msix);
234 /* must be the COMM_PREFERRED_SETTINGS values */
235 pr_warn("aacraid: Comm Interface type2 enabled\n");
240 * Increment the base address by the amount already used
242 base = base + fibsize + host_rrq_size + aac_init_size;
243 phys = (dma_addr_t)((ulong)phys + fibsize + host_rrq_size +
247 * Align the beginning of Headers to commalign
249 align = (commalign - ((uintptr_t)(base) & (commalign - 1)));
253 * Fill in addresses of the Comm Area Headers and Queues
256 if (dev->comm_interface != AAC_COMM_MESSAGE_TYPE3)
257 init->r7.comm_header_address = cpu_to_le32((u32)phys);
259 * Increment the base address by the size of the CommArea
261 base = base + commsize;
262 phys = phys + commsize;
264 * Place the Printf buffer area after the Fast I/O comm area.
266 dev->printfbuf = (void *)base;
267 if (dev->comm_interface != AAC_COMM_MESSAGE_TYPE3) {
268 init->r7.printfbuf = cpu_to_le32(phys);
269 init->r7.printfbufsiz = cpu_to_le32(printfbufsiz);
271 memset(base, 0, printfbufsiz);
275 static void aac_queue_init(struct aac_dev * dev, struct aac_queue * q, u32 *mem, int qsize)
277 atomic_set(&q->numpending, 0);
279 init_waitqueue_head(&q->cmdready);
280 INIT_LIST_HEAD(&q->cmdq);
281 init_waitqueue_head(&q->qfull);
282 spin_lock_init(&q->lockdata);
283 q->lock = &q->lockdata;
284 q->headers.producer = (__le32 *)mem;
285 q->headers.consumer = (__le32 *)(mem+1);
286 *(q->headers.producer) = cpu_to_le32(qsize);
287 *(q->headers.consumer) = cpu_to_le32(qsize);
292 * aac_send_shutdown - shutdown an adapter
293 * @dev: Adapter to shutdown
295 * This routine will send a VM_CloseAll (shutdown) request to the adapter.
298 int aac_send_shutdown(struct aac_dev * dev)
301 struct aac_close *cmd;
304 fibctx = aac_fib_alloc(dev);
307 aac_fib_init(fibctx);
309 mutex_lock(&dev->ioctl_mutex);
310 dev->adapter_shutdown = 1;
311 mutex_unlock(&dev->ioctl_mutex);
313 cmd = (struct aac_close *) fib_data(fibctx);
314 cmd->command = cpu_to_le32(VM_CloseAll);
315 cmd->cid = cpu_to_le32(0xfffffffe);
317 status = aac_fib_send(ContainerCommand,
319 sizeof(struct aac_close),
321 -2 /* Timeout silently */, 1,
325 aac_fib_complete(fibctx);
326 /* FIB should be freed only after getting the response from the F/W */
327 if (status != -ERESTARTSYS)
328 aac_fib_free(fibctx);
329 if ((dev->pdev->device == PMC_DEVICE_S7 ||
330 dev->pdev->device == PMC_DEVICE_S8 ||
331 dev->pdev->device == PMC_DEVICE_S9) &&
333 aac_src_access_devreg(dev, AAC_ENABLE_INTX);
338 * aac_comm_init - Initialise FSA data structures
339 * @dev: Adapter to initialise
341 * Initializes the data structures that are required for the FSA commuication
342 * interface to operate.
344 * 1 - if we were able to init the commuication interface.
345 * 0 - If there were errors initing. This is a fatal error.
348 static int aac_comm_init(struct aac_dev * dev)
350 unsigned long hdrsize = (sizeof(u32) * NUMBER_OF_COMM_QUEUES) * 2;
351 unsigned long queuesize = sizeof(struct aac_entry) * TOTAL_QUEUE_ENTRIES;
353 struct aac_entry * queues;
355 struct aac_queue_block * comm = dev->queues;
357 * Now allocate and initialize the zone structures used as our
358 * pool of FIB context records. The size of the zone is based
359 * on the system memory size. We also initialize the mutex used
360 * to protect the zone.
362 spin_lock_init(&dev->fib_lock);
365 * Allocate the physically contiguous space for the commuication
369 size = hdrsize + queuesize;
371 if (!aac_alloc_comm(dev, (void * *)&headers, size, QUEUE_ALIGNMENT))
374 queues = (struct aac_entry *)(((ulong)headers) + hdrsize);
376 /* Adapter to Host normal priority Command queue */
377 comm->queue[HostNormCmdQueue].base = queues;
378 aac_queue_init(dev, &comm->queue[HostNormCmdQueue], headers, HOST_NORM_CMD_ENTRIES);
379 queues += HOST_NORM_CMD_ENTRIES;
382 /* Adapter to Host high priority command queue */
383 comm->queue[HostHighCmdQueue].base = queues;
384 aac_queue_init(dev, &comm->queue[HostHighCmdQueue], headers, HOST_HIGH_CMD_ENTRIES);
386 queues += HOST_HIGH_CMD_ENTRIES;
389 /* Host to adapter normal priority command queue */
390 comm->queue[AdapNormCmdQueue].base = queues;
391 aac_queue_init(dev, &comm->queue[AdapNormCmdQueue], headers, ADAP_NORM_CMD_ENTRIES);
393 queues += ADAP_NORM_CMD_ENTRIES;
396 /* host to adapter high priority command queue */
397 comm->queue[AdapHighCmdQueue].base = queues;
398 aac_queue_init(dev, &comm->queue[AdapHighCmdQueue], headers, ADAP_HIGH_CMD_ENTRIES);
400 queues += ADAP_HIGH_CMD_ENTRIES;
403 /* adapter to host normal priority response queue */
404 comm->queue[HostNormRespQueue].base = queues;
405 aac_queue_init(dev, &comm->queue[HostNormRespQueue], headers, HOST_NORM_RESP_ENTRIES);
406 queues += HOST_NORM_RESP_ENTRIES;
409 /* adapter to host high priority response queue */
410 comm->queue[HostHighRespQueue].base = queues;
411 aac_queue_init(dev, &comm->queue[HostHighRespQueue], headers, HOST_HIGH_RESP_ENTRIES);
413 queues += HOST_HIGH_RESP_ENTRIES;
416 /* host to adapter normal priority response queue */
417 comm->queue[AdapNormRespQueue].base = queues;
418 aac_queue_init(dev, &comm->queue[AdapNormRespQueue], headers, ADAP_NORM_RESP_ENTRIES);
420 queues += ADAP_NORM_RESP_ENTRIES;
423 /* host to adapter high priority response queue */
424 comm->queue[AdapHighRespQueue].base = queues;
425 aac_queue_init(dev, &comm->queue[AdapHighRespQueue], headers, ADAP_HIGH_RESP_ENTRIES);
427 comm->queue[AdapNormCmdQueue].lock = comm->queue[HostNormRespQueue].lock;
428 comm->queue[AdapHighCmdQueue].lock = comm->queue[HostHighRespQueue].lock;
429 comm->queue[AdapNormRespQueue].lock = comm->queue[HostNormCmdQueue].lock;
430 comm->queue[AdapHighRespQueue].lock = comm->queue[HostHighCmdQueue].lock;
435 void aac_define_int_mode(struct aac_dev *dev)
437 int i, msi_count, min_msix;
440 /* max. vectors from GET_COMM_PREFERRED_SETTINGS */
441 if (dev->max_msix == 0 ||
442 dev->pdev->device == PMC_DEVICE_S6 ||
446 dev->scsi_host_ptr->can_queue +
451 /* Don't bother allocating more MSI-X vectors than cpus */
452 msi_count = min(dev->max_msix,
453 (unsigned int)num_online_cpus());
455 dev->max_msix = msi_count;
457 if (msi_count > AAC_MAX_MSIX)
458 msi_count = AAC_MAX_MSIX;
461 pci_find_capability(dev->pdev, PCI_CAP_ID_MSIX)) {
463 i = pci_alloc_irq_vectors(dev->pdev,
465 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
467 dev->msi_enabled = 1;
470 dev->msi_enabled = 0;
471 dev_err(&dev->pdev->dev,
472 "MSIX not supported!! Will try INTX 0x%x.\n", i);
476 if (!dev->msi_enabled)
477 dev->max_msix = msi_count = 1;
479 if (dev->max_msix > msi_count)
480 dev->max_msix = msi_count;
482 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE3 && dev->sa_firmware)
483 dev->vector_cap = dev->scsi_host_ptr->can_queue +
486 dev->vector_cap = (dev->scsi_host_ptr->can_queue +
487 AAC_NUM_MGT_FIB) / msi_count;
490 struct aac_dev *aac_init_adapter(struct aac_dev *dev)
493 struct Scsi_Host * host = dev->scsi_host_ptr;
494 extern int aac_sync_mode;
497 * Check the preferred comm settings, defaults from template.
499 dev->management_fib_count = 0;
500 spin_lock_init(&dev->manage_lock);
501 spin_lock_init(&dev->sync_lock);
502 spin_lock_init(&dev->iq_lock);
503 dev->max_fib_size = sizeof(struct hw_fib);
504 dev->sg_tablesize = host->sg_tablesize = (dev->max_fib_size
505 - sizeof(struct aac_fibhdr)
506 - sizeof(struct aac_write) + sizeof(struct sgentry))
507 / sizeof(struct sgentry);
508 dev->comm_interface = AAC_COMM_PRODUCER;
509 dev->raw_io_interface = dev->raw_io_64 = 0;
513 * Enable INTX mode, if not done already Enabled
515 if (aac_is_msix_mode(dev)) {
516 aac_change_to_intx(dev);
517 dev_info(&dev->pdev->dev, "Changed firmware to INTX mode");
520 if ((!aac_adapter_sync_cmd(dev, GET_ADAPTER_PROPERTIES,
522 status+0, status+1, status+2, status+3, status+4)) &&
523 (status[0] == 0x00000001)) {
524 dev->doorbell_mask = status[3];
525 if (status[1] & AAC_OPT_NEW_COMM_64)
527 dev->sync_mode = aac_sync_mode;
528 if (dev->a_ops.adapter_comm &&
529 (status[1] & AAC_OPT_NEW_COMM)) {
530 dev->comm_interface = AAC_COMM_MESSAGE;
531 dev->raw_io_interface = 1;
532 if ((status[1] & AAC_OPT_NEW_COMM_TYPE1)) {
533 /* driver supports TYPE1 (Tupelo) */
534 dev->comm_interface = AAC_COMM_MESSAGE_TYPE1;
535 } else if (status[1] & AAC_OPT_NEW_COMM_TYPE2) {
536 /* driver supports TYPE2 (Denali, Yosemite) */
537 dev->comm_interface = AAC_COMM_MESSAGE_TYPE2;
538 } else if (status[1] & AAC_OPT_NEW_COMM_TYPE3) {
539 /* driver supports TYPE3 (Yosemite, Thor) */
540 dev->comm_interface = AAC_COMM_MESSAGE_TYPE3;
541 } else if (status[1] & AAC_OPT_NEW_COMM_TYPE4) {
542 /* not supported TYPE - switch to sync. mode */
543 dev->comm_interface = AAC_COMM_MESSAGE_TYPE2;
547 if ((status[1] & le32_to_cpu(AAC_OPT_EXTENDED)) &&
548 (status[4] & le32_to_cpu(AAC_EXTOPT_SA_FIRMWARE)))
549 dev->sa_firmware = 1;
551 dev->sa_firmware = 0;
553 if ((dev->comm_interface == AAC_COMM_MESSAGE) &&
554 (status[2] > dev->base_size)) {
555 aac_adapter_ioremap(dev, 0);
556 dev->base_size = status[2];
557 if (aac_adapter_ioremap(dev, status[2])) {
558 /* remap failed, go back ... */
559 dev->comm_interface = AAC_COMM_PRODUCER;
560 if (aac_adapter_ioremap(dev, AAC_MIN_FOOTPRINT_SIZE)) {
562 "aacraid: unable to map adapter.\n");
569 dev->msi_enabled = 0;
570 dev->adapter_shutdown = 0;
571 if ((!aac_adapter_sync_cmd(dev, GET_COMM_PREFERRED_SETTINGS,
573 status+0, status+1, status+2, status+3, status+4))
574 && (status[0] == 0x00000001)) {
576 * status[1] >> 16 maximum command size in KB
577 * status[1] & 0xFFFF maximum FIB size
578 * status[2] >> 16 maximum SG elements to driver
579 * status[2] & 0xFFFF maximum SG elements from driver
580 * status[3] & 0xFFFF maximum number FIBs outstanding
582 host->max_sectors = (status[1] >> 16) << 1;
583 /* Multiple of 32 for PMC */
584 dev->max_fib_size = status[1] & 0xFFE0;
585 host->sg_tablesize = status[2] >> 16;
586 dev->sg_tablesize = status[2] & 0xFFFF;
587 if (dev->pdev->device == PMC_DEVICE_S7 ||
588 dev->pdev->device == PMC_DEVICE_S8 ||
589 dev->pdev->device == PMC_DEVICE_S9) {
590 if (host->can_queue > (status[3] >> 16) -
592 host->can_queue = (status[3] >> 16) -
594 } else if (host->can_queue > (status[3] & 0xFFFF) -
596 host->can_queue = (status[3] & 0xFFFF) -
599 dev->max_num_aif = status[4] & 0xFFFF;
602 if (numacb < host->can_queue)
603 host->can_queue = numacb;
605 pr_warn("numacb=%d ignored\n", numacb);
608 if (dev->pdev->device == PMC_DEVICE_S6 ||
609 dev->pdev->device == PMC_DEVICE_S7 ||
610 dev->pdev->device == PMC_DEVICE_S8 ||
611 dev->pdev->device == PMC_DEVICE_S9)
612 aac_define_int_mode(dev);
614 * Ok now init the communication subsystem
617 dev->queues = kzalloc(sizeof(struct aac_queue_block), GFP_KERNEL);
618 if (dev->queues == NULL) {
619 printk(KERN_ERR "Error could not allocate comm region.\n");
623 if (aac_comm_init(dev)<0){
628 * Initialize the list of fibs
630 if (aac_fib_setup(dev) < 0) {
635 INIT_LIST_HEAD(&dev->fib_list);
636 INIT_LIST_HEAD(&dev->sync_fib_list);