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 = dma_alloc_coherent(&dev->pdev->dev, size, &phys, GFP_KERNEL);
104 printk(KERN_ERR "aacraid: unable to create mapping.\n");
108 dev->comm_addr = (void *)base;
109 dev->comm_phys = phys;
110 dev->comm_size = size;
112 if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) ||
113 (dev->comm_interface == AAC_COMM_MESSAGE_TYPE2) ||
114 (dev->comm_interface == AAC_COMM_MESSAGE_TYPE3)) {
115 dev->host_rrq = (u32 *)(base + fibsize);
116 dev->host_rrq_pa = phys + fibsize;
117 memset(dev->host_rrq, 0, host_rrq_size);
120 dev->init = (union aac_init *)(base + fibsize + host_rrq_size);
121 dev->init_pa = phys + fibsize + host_rrq_size;
125 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) {
129 init->r8.init_struct_revision =
130 cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_8);
131 init->r8.init_flags = cpu_to_le32(INITFLAGS_NEW_COMM_SUPPORTED |
132 INITFLAGS_DRIVER_USES_UTC_TIME |
133 INITFLAGS_DRIVER_SUPPORTS_PM);
134 init->r8.init_flags |=
135 cpu_to_le32(INITFLAGS_DRIVER_SUPPORTS_HBA_MODE);
136 init->r8.rr_queue_count = cpu_to_le32(dev->max_msix);
137 init->r8.max_io_size =
138 cpu_to_le32(dev->scsi_host_ptr->max_sectors << 9);
139 init->r8.max_num_aif = init->r8.reserved1 =
140 init->r8.reserved2 = 0;
142 for (i = 0; i < dev->max_msix; i++) {
143 addr = (u64)dev->host_rrq_pa + dev->vector_cap * i *
145 init->r8.rrq[i].host_addr_high = cpu_to_le32(
146 upper_32_bits(addr));
147 init->r8.rrq[i].host_addr_low = cpu_to_le32(
148 lower_32_bits(addr));
149 init->r8.rrq[i].msix_id = i;
150 init->r8.rrq[i].element_count = cpu_to_le16(
151 (u16)dev->vector_cap);
152 init->r8.rrq[i].comp_thresh =
153 init->r8.rrq[i].unused = 0;
156 pr_warn("aacraid: Comm Interface type3 enabled\n");
158 init->r7.init_struct_revision =
159 cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION);
160 if (dev->max_fib_size != sizeof(struct hw_fib))
161 init->r7.init_struct_revision =
162 cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_4);
163 init->r7.no_of_msix_vectors = cpu_to_le32(SA_MINIPORT_REVISION);
164 init->r7.fsrev = cpu_to_le32(dev->fsrev);
167 * Adapter Fibs are the first thing allocated so that they
170 dev->aif_base_va = (struct hw_fib *)base;
172 init->r7.adapter_fibs_virtual_address = 0;
173 init->r7.adapter_fibs_physical_address = cpu_to_le32((u32)phys);
174 init->r7.adapter_fibs_size = cpu_to_le32(fibsize);
175 init->r7.adapter_fib_align = cpu_to_le32(sizeof(struct hw_fib));
178 * number of 4k pages of host physical memory. The aacraid fw
179 * needs this number to be less than 4gb worth of pages. New
180 * firmware doesn't have any issues with the mapping system, but
181 * older Firmware did, and had *troubles* dealing with the math
182 * overloading past 32 bits, thus we must limit this field.
184 aac_max_hostphysmempages =
185 dma_get_required_mask(&dev->pdev->dev) >> 12;
186 if (aac_max_hostphysmempages < AAC_MAX_HOSTPHYSMEMPAGES)
187 init->r7.host_phys_mem_pages =
188 cpu_to_le32(aac_max_hostphysmempages);
190 init->r7.host_phys_mem_pages =
191 cpu_to_le32(AAC_MAX_HOSTPHYSMEMPAGES);
193 init->r7.init_flags =
194 cpu_to_le32(INITFLAGS_DRIVER_USES_UTC_TIME |
195 INITFLAGS_DRIVER_SUPPORTS_PM);
196 init->r7.max_io_commands =
197 cpu_to_le32(dev->scsi_host_ptr->can_queue +
199 init->r7.max_io_size =
200 cpu_to_le32(dev->scsi_host_ptr->max_sectors << 9);
201 init->r7.max_fib_size = cpu_to_le32(dev->max_fib_size);
202 init->r7.max_num_aif = cpu_to_le32(dev->max_num_aif);
204 if (dev->comm_interface == AAC_COMM_MESSAGE) {
205 init->r7.init_flags |=
206 cpu_to_le32(INITFLAGS_NEW_COMM_SUPPORTED);
207 pr_warn("aacraid: Comm Interface enabled\n");
208 } else if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) {
209 init->r7.init_struct_revision =
210 cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_6);
211 init->r7.init_flags |=
212 cpu_to_le32(INITFLAGS_NEW_COMM_SUPPORTED |
213 INITFLAGS_NEW_COMM_TYPE1_SUPPORTED |
214 INITFLAGS_FAST_JBOD_SUPPORTED);
215 init->r7.host_rrq_addr_high =
216 cpu_to_le32(upper_32_bits(dev->host_rrq_pa));
217 init->r7.host_rrq_addr_low =
218 cpu_to_le32(lower_32_bits(dev->host_rrq_pa));
219 pr_warn("aacraid: Comm Interface type1 enabled\n");
220 } else if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE2) {
221 init->r7.init_struct_revision =
222 cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_7);
223 init->r7.init_flags |=
224 cpu_to_le32(INITFLAGS_NEW_COMM_SUPPORTED |
225 INITFLAGS_NEW_COMM_TYPE2_SUPPORTED |
226 INITFLAGS_FAST_JBOD_SUPPORTED);
227 init->r7.host_rrq_addr_high =
228 cpu_to_le32(upper_32_bits(dev->host_rrq_pa));
229 init->r7.host_rrq_addr_low =
230 cpu_to_le32(lower_32_bits(dev->host_rrq_pa));
231 init->r7.no_of_msix_vectors =
232 cpu_to_le32(dev->max_msix);
233 /* must be the COMM_PREFERRED_SETTINGS values */
234 pr_warn("aacraid: Comm Interface type2 enabled\n");
239 * Increment the base address by the amount already used
241 base = base + fibsize + host_rrq_size + aac_init_size;
242 phys = (dma_addr_t)((ulong)phys + fibsize + host_rrq_size +
246 * Align the beginning of Headers to commalign
248 align = (commalign - ((uintptr_t)(base) & (commalign - 1)));
252 * Fill in addresses of the Comm Area Headers and Queues
255 if (dev->comm_interface != AAC_COMM_MESSAGE_TYPE3)
256 init->r7.comm_header_address = cpu_to_le32((u32)phys);
258 * Increment the base address by the size of the CommArea
260 base = base + commsize;
261 phys = phys + commsize;
263 * Place the Printf buffer area after the Fast I/O comm area.
265 dev->printfbuf = (void *)base;
266 if (dev->comm_interface != AAC_COMM_MESSAGE_TYPE3) {
267 init->r7.printfbuf = cpu_to_le32(phys);
268 init->r7.printfbufsiz = cpu_to_le32(printfbufsiz);
270 memset(base, 0, printfbufsiz);
274 static void aac_queue_init(struct aac_dev * dev, struct aac_queue * q, u32 *mem, int qsize)
276 atomic_set(&q->numpending, 0);
278 init_waitqueue_head(&q->cmdready);
279 INIT_LIST_HEAD(&q->cmdq);
280 init_waitqueue_head(&q->qfull);
281 spin_lock_init(&q->lockdata);
282 q->lock = &q->lockdata;
283 q->headers.producer = (__le32 *)mem;
284 q->headers.consumer = (__le32 *)(mem+1);
285 *(q->headers.producer) = cpu_to_le32(qsize);
286 *(q->headers.consumer) = cpu_to_le32(qsize);
291 * aac_send_shutdown - shutdown an adapter
292 * @dev: Adapter to shutdown
294 * This routine will send a VM_CloseAll (shutdown) request to the adapter.
297 int aac_send_shutdown(struct aac_dev * dev)
300 struct aac_close *cmd;
303 fibctx = aac_fib_alloc(dev);
306 aac_fib_init(fibctx);
308 mutex_lock(&dev->ioctl_mutex);
309 dev->adapter_shutdown = 1;
310 mutex_unlock(&dev->ioctl_mutex);
312 cmd = (struct aac_close *) fib_data(fibctx);
313 cmd->command = cpu_to_le32(VM_CloseAll);
314 cmd->cid = cpu_to_le32(0xfffffffe);
316 status = aac_fib_send(ContainerCommand,
318 sizeof(struct aac_close),
320 -2 /* Timeout silently */, 1,
324 aac_fib_complete(fibctx);
325 /* FIB should be freed only after getting the response from the F/W */
326 if (status != -ERESTARTSYS)
327 aac_fib_free(fibctx);
328 if ((dev->pdev->device == PMC_DEVICE_S7 ||
329 dev->pdev->device == PMC_DEVICE_S8 ||
330 dev->pdev->device == PMC_DEVICE_S9) &&
332 aac_set_intx_mode(dev);
337 * aac_comm_init - Initialise FSA data structures
338 * @dev: Adapter to initialise
340 * Initializes the data structures that are required for the FSA commuication
341 * interface to operate.
343 * 1 - if we were able to init the commuication interface.
344 * 0 - If there were errors initing. This is a fatal error.
347 static int aac_comm_init(struct aac_dev * dev)
349 unsigned long hdrsize = (sizeof(u32) * NUMBER_OF_COMM_QUEUES) * 2;
350 unsigned long queuesize = sizeof(struct aac_entry) * TOTAL_QUEUE_ENTRIES;
352 struct aac_entry * queues;
354 struct aac_queue_block * comm = dev->queues;
356 * Now allocate and initialize the zone structures used as our
357 * pool of FIB context records. The size of the zone is based
358 * on the system memory size. We also initialize the mutex used
359 * to protect the zone.
361 spin_lock_init(&dev->fib_lock);
364 * Allocate the physically contiguous space for the commuication
368 size = hdrsize + queuesize;
370 if (!aac_alloc_comm(dev, (void * *)&headers, size, QUEUE_ALIGNMENT))
373 queues = (struct aac_entry *)(((ulong)headers) + hdrsize);
375 /* Adapter to Host normal priority Command queue */
376 comm->queue[HostNormCmdQueue].base = queues;
377 aac_queue_init(dev, &comm->queue[HostNormCmdQueue], headers, HOST_NORM_CMD_ENTRIES);
378 queues += HOST_NORM_CMD_ENTRIES;
381 /* Adapter to Host high priority command queue */
382 comm->queue[HostHighCmdQueue].base = queues;
383 aac_queue_init(dev, &comm->queue[HostHighCmdQueue], headers, HOST_HIGH_CMD_ENTRIES);
385 queues += HOST_HIGH_CMD_ENTRIES;
388 /* Host to adapter normal priority command queue */
389 comm->queue[AdapNormCmdQueue].base = queues;
390 aac_queue_init(dev, &comm->queue[AdapNormCmdQueue], headers, ADAP_NORM_CMD_ENTRIES);
392 queues += ADAP_NORM_CMD_ENTRIES;
395 /* host to adapter high priority command queue */
396 comm->queue[AdapHighCmdQueue].base = queues;
397 aac_queue_init(dev, &comm->queue[AdapHighCmdQueue], headers, ADAP_HIGH_CMD_ENTRIES);
399 queues += ADAP_HIGH_CMD_ENTRIES;
402 /* adapter to host normal priority response queue */
403 comm->queue[HostNormRespQueue].base = queues;
404 aac_queue_init(dev, &comm->queue[HostNormRespQueue], headers, HOST_NORM_RESP_ENTRIES);
405 queues += HOST_NORM_RESP_ENTRIES;
408 /* adapter to host high priority response queue */
409 comm->queue[HostHighRespQueue].base = queues;
410 aac_queue_init(dev, &comm->queue[HostHighRespQueue], headers, HOST_HIGH_RESP_ENTRIES);
412 queues += HOST_HIGH_RESP_ENTRIES;
415 /* host to adapter normal priority response queue */
416 comm->queue[AdapNormRespQueue].base = queues;
417 aac_queue_init(dev, &comm->queue[AdapNormRespQueue], headers, ADAP_NORM_RESP_ENTRIES);
419 queues += ADAP_NORM_RESP_ENTRIES;
422 /* host to adapter high priority response queue */
423 comm->queue[AdapHighRespQueue].base = queues;
424 aac_queue_init(dev, &comm->queue[AdapHighRespQueue], headers, ADAP_HIGH_RESP_ENTRIES);
426 comm->queue[AdapNormCmdQueue].lock = comm->queue[HostNormRespQueue].lock;
427 comm->queue[AdapHighCmdQueue].lock = comm->queue[HostHighRespQueue].lock;
428 comm->queue[AdapNormRespQueue].lock = comm->queue[HostNormCmdQueue].lock;
429 comm->queue[AdapHighRespQueue].lock = comm->queue[HostHighCmdQueue].lock;
434 void aac_define_int_mode(struct aac_dev *dev)
436 int i, msi_count, min_msix;
439 /* max. vectors from GET_COMM_PREFERRED_SETTINGS */
440 if (dev->max_msix == 0 ||
441 dev->pdev->device == PMC_DEVICE_S6 ||
445 dev->scsi_host_ptr->can_queue +
450 /* Don't bother allocating more MSI-X vectors than cpus */
451 msi_count = min(dev->max_msix,
452 (unsigned int)num_online_cpus());
454 dev->max_msix = msi_count;
456 if (msi_count > AAC_MAX_MSIX)
457 msi_count = AAC_MAX_MSIX;
460 pci_find_capability(dev->pdev, PCI_CAP_ID_MSIX)) {
462 i = pci_alloc_irq_vectors(dev->pdev,
464 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
466 dev->msi_enabled = 1;
469 dev->msi_enabled = 0;
470 dev_err(&dev->pdev->dev,
471 "MSIX not supported!! Will try INTX 0x%x.\n", i);
475 if (!dev->msi_enabled)
476 dev->max_msix = msi_count = 1;
478 if (dev->max_msix > msi_count)
479 dev->max_msix = msi_count;
481 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE3 && dev->sa_firmware)
482 dev->vector_cap = dev->scsi_host_ptr->can_queue +
485 dev->vector_cap = (dev->scsi_host_ptr->can_queue +
486 AAC_NUM_MGT_FIB) / msi_count;
489 struct aac_dev *aac_init_adapter(struct aac_dev *dev)
492 struct Scsi_Host * host = dev->scsi_host_ptr;
493 extern int aac_sync_mode;
496 * Check the preferred comm settings, defaults from template.
498 dev->management_fib_count = 0;
499 spin_lock_init(&dev->manage_lock);
500 spin_lock_init(&dev->sync_lock);
501 spin_lock_init(&dev->iq_lock);
502 dev->max_fib_size = sizeof(struct hw_fib);
503 dev->sg_tablesize = host->sg_tablesize = (dev->max_fib_size
504 - sizeof(struct aac_fibhdr)
505 - sizeof(struct aac_write) + sizeof(struct sgentry))
506 / sizeof(struct sgentry);
507 dev->comm_interface = AAC_COMM_PRODUCER;
508 dev->raw_io_interface = dev->raw_io_64 = 0;
512 * Enable INTX mode, if not done already Enabled
514 if (aac_is_msix_mode(dev)) {
515 aac_change_to_intx(dev);
516 dev_info(&dev->pdev->dev, "Changed firmware to INTX mode");
519 if ((!aac_adapter_sync_cmd(dev, GET_ADAPTER_PROPERTIES,
521 status+0, status+1, status+2, status+3, status+4)) &&
522 (status[0] == 0x00000001)) {
523 dev->doorbell_mask = status[3];
524 if (status[1] & AAC_OPT_NEW_COMM_64)
526 dev->sync_mode = aac_sync_mode;
527 if (dev->a_ops.adapter_comm &&
528 (status[1] & AAC_OPT_NEW_COMM)) {
529 dev->comm_interface = AAC_COMM_MESSAGE;
530 dev->raw_io_interface = 1;
531 if ((status[1] & AAC_OPT_NEW_COMM_TYPE1)) {
532 /* driver supports TYPE1 (Tupelo) */
533 dev->comm_interface = AAC_COMM_MESSAGE_TYPE1;
534 } else if (status[1] & AAC_OPT_NEW_COMM_TYPE2) {
535 /* driver supports TYPE2 (Denali, Yosemite) */
536 dev->comm_interface = AAC_COMM_MESSAGE_TYPE2;
537 } else if (status[1] & AAC_OPT_NEW_COMM_TYPE3) {
538 /* driver supports TYPE3 (Yosemite, Thor) */
539 dev->comm_interface = AAC_COMM_MESSAGE_TYPE3;
540 } else if (status[1] & AAC_OPT_NEW_COMM_TYPE4) {
541 /* not supported TYPE - switch to sync. mode */
542 dev->comm_interface = AAC_COMM_MESSAGE_TYPE2;
546 if ((status[1] & le32_to_cpu(AAC_OPT_EXTENDED)) &&
547 (status[4] & le32_to_cpu(AAC_EXTOPT_SA_FIRMWARE)))
548 dev->sa_firmware = 1;
550 dev->sa_firmware = 0;
552 if ((dev->comm_interface == AAC_COMM_MESSAGE) &&
553 (status[2] > dev->base_size)) {
554 aac_adapter_ioremap(dev, 0);
555 dev->base_size = status[2];
556 if (aac_adapter_ioremap(dev, status[2])) {
557 /* remap failed, go back ... */
558 dev->comm_interface = AAC_COMM_PRODUCER;
559 if (aac_adapter_ioremap(dev, AAC_MIN_FOOTPRINT_SIZE)) {
561 "aacraid: unable to map adapter.\n");
568 dev->msi_enabled = 0;
569 dev->adapter_shutdown = 0;
570 if ((!aac_adapter_sync_cmd(dev, GET_COMM_PREFERRED_SETTINGS,
572 status+0, status+1, status+2, status+3, status+4))
573 && (status[0] == 0x00000001)) {
575 * status[1] >> 16 maximum command size in KB
576 * status[1] & 0xFFFF maximum FIB size
577 * status[2] >> 16 maximum SG elements to driver
578 * status[2] & 0xFFFF maximum SG elements from driver
579 * status[3] & 0xFFFF maximum number FIBs outstanding
581 host->max_sectors = (status[1] >> 16) << 1;
582 /* Multiple of 32 for PMC */
583 dev->max_fib_size = status[1] & 0xFFE0;
584 host->sg_tablesize = status[2] >> 16;
585 dev->sg_tablesize = status[2] & 0xFFFF;
586 if (dev->pdev->device == PMC_DEVICE_S7 ||
587 dev->pdev->device == PMC_DEVICE_S8 ||
588 dev->pdev->device == PMC_DEVICE_S9) {
589 if (host->can_queue > (status[3] >> 16) -
591 host->can_queue = (status[3] >> 16) -
593 } else if (host->can_queue > (status[3] & 0xFFFF) -
595 host->can_queue = (status[3] & 0xFFFF) -
598 dev->max_num_aif = status[4] & 0xFFFF;
601 if (numacb < host->can_queue)
602 host->can_queue = numacb;
604 pr_warn("numacb=%d ignored\n", numacb);
607 if (dev->pdev->device == PMC_DEVICE_S6 ||
608 dev->pdev->device == PMC_DEVICE_S7 ||
609 dev->pdev->device == PMC_DEVICE_S8 ||
610 dev->pdev->device == PMC_DEVICE_S9)
611 aac_define_int_mode(dev);
613 * Ok now init the communication subsystem
616 dev->queues = kzalloc(sizeof(struct aac_queue_block), GFP_KERNEL);
617 if (dev->queues == NULL) {
618 printk(KERN_ERR "Error could not allocate comm region.\n");
622 if (aac_comm_init(dev)<0){
627 * Initialize the list of fibs
629 if (aac_fib_setup(dev) < 0) {
634 INIT_LIST_HEAD(&dev->fib_list);
635 INIT_LIST_HEAD(&dev->sync_fib_list);