2 *******************************************************************************
4 ** FILE NAME : arcmsr_hba.c
6 ** Description: SCSI RAID Device Driver for
7 ** ARECA RAID Host adapter
8 *******************************************************************************
9 ** Copyright (C) 2002 - 2005, Areca Technology Corporation All rights reserved
11 ** Web site: www.areca.com.tw
12 ** E-mail: support@areca.com.tw
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License version 2 as
16 ** published by the Free Software Foundation.
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.
21 *******************************************************************************
22 ** Redistribution and use in source and binary forms, with or without
23 ** modification, are permitted provided that the following conditions
25 ** 1. Redistributions of source code must retain the above copyright
26 ** notice, this list of conditions and the following disclaimer.
27 ** 2. Redistributions in binary form must reproduce the above copyright
28 ** notice, this list of conditions and the following disclaimer in the
29 ** documentation and/or other materials provided with the distribution.
30 ** 3. The name of the author may not be used to endorse or promote products
31 ** derived from this software without specific prior written permission.
33 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
34 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
36 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
37 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,BUT
38 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY
40 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 ** (INCLUDING NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF
42 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 *******************************************************************************
44 ** For history of changes, see Documentation/scsi/ChangeLog.arcmsr
45 ** Firmware Specification, see Documentation/scsi/arcmsr_spec.txt
46 *******************************************************************************
48 #include <linux/module.h>
49 #include <linux/reboot.h>
50 #include <linux/spinlock.h>
51 #include <linux/pci_ids.h>
52 #include <linux/interrupt.h>
53 #include <linux/moduleparam.h>
54 #include <linux/errno.h>
55 #include <linux/types.h>
56 #include <linux/delay.h>
57 #include <linux/dma-mapping.h>
58 #include <linux/timer.h>
59 #include <linux/pci.h>
60 #include <linux/aer.h>
63 #include <asm/system.h>
64 #include <asm/uaccess.h>
65 #include <scsi/scsi_host.h>
66 #include <scsi/scsi.h>
67 #include <scsi/scsi_cmnd.h>
68 #include <scsi/scsi_tcq.h>
69 #include <scsi/scsi_device.h>
70 #include <scsi/scsi_transport.h>
71 #include <scsi/scsicam.h>
74 MODULE_AUTHOR("Erich Chen <support@areca.com.tw>");
75 MODULE_DESCRIPTION("ARECA (ARC11xx/12xx/13xx/16xx) SATA/SAS RAID HOST Adapter");
76 MODULE_LICENSE("Dual BSD/GPL");
77 MODULE_VERSION(ARCMSR_DRIVER_VERSION);
79 static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb,
80 struct scsi_cmnd *cmd);
81 static int arcmsr_iop_confirm(struct AdapterControlBlock *acb);
82 static int arcmsr_abort(struct scsi_cmnd *);
83 static int arcmsr_bus_reset(struct scsi_cmnd *);
84 static int arcmsr_bios_param(struct scsi_device *sdev,
85 struct block_device *bdev, sector_t capacity, int *info);
86 static int arcmsr_queue_command(struct scsi_cmnd *cmd,
87 void (*done) (struct scsi_cmnd *));
88 static int arcmsr_probe(struct pci_dev *pdev,
89 const struct pci_device_id *id);
90 static void arcmsr_remove(struct pci_dev *pdev);
91 static void arcmsr_shutdown(struct pci_dev *pdev);
92 static void arcmsr_iop_init(struct AdapterControlBlock *acb);
93 static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb);
94 static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb);
95 static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb);
96 static void arcmsr_flush_hba_cache(struct AdapterControlBlock *acb);
97 static void arcmsr_flush_hbb_cache(struct AdapterControlBlock *acb);
98 static const char *arcmsr_info(struct Scsi_Host *);
99 static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb);
100 static int arcmsr_adjust_disk_queue_depth(struct scsi_device *sdev,
103 if (queue_depth > ARCMSR_MAX_CMD_PERLUN)
104 queue_depth = ARCMSR_MAX_CMD_PERLUN;
105 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
109 static struct scsi_host_template arcmsr_scsi_host_template = {
110 .module = THIS_MODULE,
111 .name = "ARCMSR ARECA SATA/SAS RAID HOST Adapter"
112 ARCMSR_DRIVER_VERSION,
114 .queuecommand = arcmsr_queue_command,
115 .eh_abort_handler = arcmsr_abort,
116 .eh_bus_reset_handler = arcmsr_bus_reset,
117 .bios_param = arcmsr_bios_param,
118 .change_queue_depth = arcmsr_adjust_disk_queue_depth,
119 .can_queue = ARCMSR_MAX_OUTSTANDING_CMD,
120 .this_id = ARCMSR_SCSI_INITIATOR_ID,
121 .sg_tablesize = ARCMSR_MAX_SG_ENTRIES,
122 .max_sectors = ARCMSR_MAX_XFER_SECTORS,
123 .cmd_per_lun = ARCMSR_MAX_CMD_PERLUN,
124 .use_clustering = ENABLE_CLUSTERING,
125 .shost_attrs = arcmsr_host_attrs,
127 #ifdef CONFIG_SCSI_ARCMSR_AER
128 static pci_ers_result_t arcmsr_pci_slot_reset(struct pci_dev *pdev);
129 static pci_ers_result_t arcmsr_pci_error_detected(struct pci_dev *pdev,
130 pci_channel_state_t state);
132 static struct pci_error_handlers arcmsr_pci_error_handlers = {
133 .error_detected = arcmsr_pci_error_detected,
134 .slot_reset = arcmsr_pci_slot_reset,
137 static struct pci_device_id arcmsr_device_id_table[] = {
138 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1110)},
139 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1120)},
140 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1130)},
141 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1160)},
142 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1170)},
143 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1200)},
144 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1201)},
145 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1202)},
146 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1210)},
147 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1220)},
148 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1230)},
149 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1260)},
150 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1270)},
151 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1280)},
152 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1380)},
153 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1381)},
154 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1680)},
155 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1681)},
156 {0, 0}, /* Terminating entry */
158 MODULE_DEVICE_TABLE(pci, arcmsr_device_id_table);
159 static struct pci_driver arcmsr_pci_driver = {
161 .id_table = arcmsr_device_id_table,
162 .probe = arcmsr_probe,
163 .remove = arcmsr_remove,
164 .shutdown = arcmsr_shutdown,
165 #ifdef CONFIG_SCSI_ARCMSR_AER
166 .err_handler = &arcmsr_pci_error_handlers,
170 static irqreturn_t arcmsr_do_interrupt(int irq, void *dev_id)
172 irqreturn_t handle_state;
173 struct AdapterControlBlock *acb = dev_id;
175 spin_lock(acb->host->host_lock);
176 handle_state = arcmsr_interrupt(acb);
177 spin_unlock(acb->host->host_lock);
182 static int arcmsr_bios_param(struct scsi_device *sdev,
183 struct block_device *bdev, sector_t capacity, int *geom)
185 int ret, heads, sectors, cylinders, total_capacity;
186 unsigned char *buffer;/* return copy of block device's partition table */
188 buffer = scsi_bios_ptable(bdev);
190 ret = scsi_partsize(buffer, capacity, &geom[2], &geom[0], &geom[1]);
195 total_capacity = capacity;
198 cylinders = total_capacity / (heads * sectors);
199 if (cylinders > 1024) {
202 cylinders = total_capacity / (heads * sectors);
210 static void arcmsr_define_adapter_type(struct AdapterControlBlock *acb)
212 struct pci_dev *pdev = acb->pdev;
214 pci_read_config_word(pdev, PCI_DEVICE_ID, &dev_id);
217 acb->adapter_type = ACB_ADAPTER_TYPE_B;
221 default : acb->adapter_type = ACB_ADAPTER_TYPE_A;
225 static int arcmsr_alloc_ccb_pool(struct AdapterControlBlock *acb)
228 switch (acb->adapter_type) {
230 case ACB_ADAPTER_TYPE_A: {
231 struct pci_dev *pdev = acb->pdev;
233 dma_addr_t dma_coherent_handle, dma_addr;
234 struct CommandControlBlock *ccb_tmp;
235 uint32_t intmask_org;
238 acb->pmuA = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
240 printk(KERN_NOTICE "arcmsr%d: memory mapping region fail \n",
245 dma_coherent = dma_alloc_coherent(&pdev->dev,
246 ARCMSR_MAX_FREECCB_NUM *
247 sizeof (struct CommandControlBlock) + 0x20,
248 &dma_coherent_handle, GFP_KERNEL);
255 acb->dma_coherent = dma_coherent;
256 acb->dma_coherent_handle = dma_coherent_handle;
258 if (((unsigned long)dma_coherent & 0x1F)) {
259 dma_coherent = dma_coherent +
260 (0x20 - ((unsigned long)dma_coherent & 0x1F));
261 dma_coherent_handle = dma_coherent_handle +
262 (0x20 - ((unsigned long)dma_coherent_handle & 0x1F));
265 dma_addr = dma_coherent_handle;
266 ccb_tmp = (struct CommandControlBlock *)dma_coherent;
267 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
268 ccb_tmp->cdb_shifted_phyaddr = dma_addr >> 5;
270 acb->pccb_pool[i] = ccb_tmp;
271 list_add_tail(&ccb_tmp->list, &acb->ccb_free_list);
272 dma_addr = dma_addr + sizeof(struct CommandControlBlock);
276 acb->vir2phy_offset = (unsigned long)ccb_tmp -(unsigned long)dma_addr;
277 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
278 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
279 acb->devstate[i][j] = ARECA_RAID_GONE;
282 ** here we need to tell iop 331 our ccb_tmp.HighPart
283 ** if ccb_tmp.HighPart is not zero
285 intmask_org = arcmsr_disable_outbound_ints(acb);
289 case ACB_ADAPTER_TYPE_B: {
291 struct pci_dev *pdev = acb->pdev;
292 struct MessageUnit_B *reg;
293 void __iomem *mem_base0, *mem_base1;
295 dma_addr_t dma_coherent_handle, dma_addr;
296 uint32_t intmask_org;
297 struct CommandControlBlock *ccb_tmp;
300 dma_coherent = dma_alloc_coherent(&pdev->dev,
301 ((ARCMSR_MAX_FREECCB_NUM *
302 sizeof(struct CommandControlBlock) + 0x20) +
303 sizeof(struct MessageUnit_B)),
304 &dma_coherent_handle, GFP_KERNEL);
308 acb->dma_coherent = dma_coherent;
309 acb->dma_coherent_handle = dma_coherent_handle;
311 if (((unsigned long)dma_coherent & 0x1F)) {
312 dma_coherent = dma_coherent +
313 (0x20 - ((unsigned long)dma_coherent & 0x1F));
314 dma_coherent_handle = dma_coherent_handle +
315 (0x20 - ((unsigned long)dma_coherent_handle & 0x1F));
318 reg = (struct MessageUnit_B *)(dma_coherent +
319 ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock));
321 dma_addr = dma_coherent_handle;
322 ccb_tmp = (struct CommandControlBlock *)dma_coherent;
323 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
324 ccb_tmp->cdb_shifted_phyaddr = dma_addr >> 5;
326 acb->pccb_pool[i] = ccb_tmp;
327 list_add_tail(&ccb_tmp->list, &acb->ccb_free_list);
328 dma_addr = dma_addr + sizeof(struct CommandControlBlock);
332 reg = (struct MessageUnit_B *)(dma_coherent +
333 ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock));
335 mem_base0 = ioremap(pci_resource_start(pdev, 0),
336 pci_resource_len(pdev, 0));
340 mem_base1 = ioremap(pci_resource_start(pdev, 2),
341 pci_resource_len(pdev, 2));
347 reg->drv2iop_doorbell_reg = mem_base0 + ARCMSR_DRV2IOP_DOORBELL;
348 reg->drv2iop_doorbell_mask_reg = mem_base0 +
349 ARCMSR_DRV2IOP_DOORBELL_MASK;
350 reg->iop2drv_doorbell_reg = mem_base0 + ARCMSR_IOP2DRV_DOORBELL;
351 reg->iop2drv_doorbell_mask_reg = mem_base0 +
352 ARCMSR_IOP2DRV_DOORBELL_MASK;
353 reg->ioctl_wbuffer_reg = mem_base1 + ARCMSR_IOCTL_WBUFFER;
354 reg->ioctl_rbuffer_reg = mem_base1 + ARCMSR_IOCTL_RBUFFER;
355 reg->msgcode_rwbuffer_reg = mem_base1 + ARCMSR_MSGCODE_RWBUFFER;
357 acb->vir2phy_offset = (unsigned long)ccb_tmp -(unsigned long)dma_addr;
358 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
359 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
360 acb->devstate[i][j] = ARECA_RAID_GOOD;
363 ** here we need to tell iop 331 our ccb_tmp.HighPart
364 ** if ccb_tmp.HighPart is not zero
366 intmask_org = arcmsr_disable_outbound_ints(acb);
373 dma_free_coherent(&acb->pdev->dev,
374 ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock) + 0x20,
375 acb->dma_coherent, acb->dma_coherent_handle);
379 static int arcmsr_probe(struct pci_dev *pdev,
380 const struct pci_device_id *id)
382 struct Scsi_Host *host;
383 struct AdapterControlBlock *acb;
384 uint8_t bus, dev_fun;
387 error = pci_enable_device(pdev);
390 pci_set_master(pdev);
392 host = scsi_host_alloc(&arcmsr_scsi_host_template,
393 sizeof(struct AdapterControlBlock));
396 goto out_disable_device;
398 acb = (struct AdapterControlBlock *)host->hostdata;
399 memset(acb, 0, sizeof (struct AdapterControlBlock));
401 error = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
403 error = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
406 "scsi%d: No suitable DMA mask available\n",
411 bus = pdev->bus->number;
412 dev_fun = pdev->devfn;
415 host->max_sectors = ARCMSR_MAX_XFER_SECTORS;
416 host->max_lun = ARCMSR_MAX_TARGETLUN;
417 host->max_id = ARCMSR_MAX_TARGETID;/*16:8*/
418 host->max_cmd_len = 16; /*this is issue of 64bit LBA, over 2T byte*/
419 host->sg_tablesize = ARCMSR_MAX_SG_ENTRIES;
420 host->can_queue = ARCMSR_MAX_FREECCB_NUM; /* max simultaneous cmds */
421 host->cmd_per_lun = ARCMSR_MAX_CMD_PERLUN;
422 host->this_id = ARCMSR_SCSI_INITIATOR_ID;
423 host->unique_id = (bus << 8) | dev_fun;
424 host->irq = pdev->irq;
425 error = pci_request_regions(pdev, "arcmsr");
429 arcmsr_define_adapter_type(acb);
431 acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
432 ACB_F_MESSAGE_RQBUFFER_CLEARED |
433 ACB_F_MESSAGE_WQBUFFER_READED);
434 acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
435 INIT_LIST_HEAD(&acb->ccb_free_list);
437 error = arcmsr_alloc_ccb_pool(acb);
439 goto out_release_regions;
441 error = request_irq(pdev->irq, arcmsr_do_interrupt,
442 IRQF_SHARED, "arcmsr", acb);
444 goto out_free_ccb_pool;
446 arcmsr_iop_init(acb);
447 pci_set_drvdata(pdev, host);
448 if (strncmp(acb->firm_version, "V1.42", 5) >= 0)
449 host->max_sectors= ARCMSR_MAX_XFER_SECTORS_B;
451 error = scsi_add_host(host, &pdev->dev);
455 error = arcmsr_alloc_sysfs_attr(acb);
459 scsi_scan_host(host);
460 #ifdef CONFIG_SCSI_ARCMSR_AER
461 pci_enable_pcie_error_reporting(pdev);
466 free_irq(pdev->irq, acb);
468 arcmsr_free_ccb_pool(acb);
470 pci_release_regions(pdev);
474 pci_disable_device(pdev);
479 static uint8_t arcmsr_hba_wait_msgint_ready(struct AdapterControlBlock *acb)
481 struct MessageUnit_A __iomem *reg = acb->pmuA;
483 uint8_t Retries = 0x00;
486 for (Index = 0; Index < 100; Index++) {
487 if (readl(®->outbound_intstatus) &
488 ARCMSR_MU_OUTBOUND_MESSAGE0_INT) {
489 writel(ARCMSR_MU_OUTBOUND_MESSAGE0_INT,
490 ®->outbound_intstatus);
496 } while (Retries++ < 20);/*max 20 sec*/
500 static uint8_t arcmsr_hbb_wait_msgint_ready(struct AdapterControlBlock *acb)
502 struct MessageUnit_B *reg = acb->pmuB;
504 uint8_t Retries = 0x00;
507 for (Index = 0; Index < 100; Index++) {
508 if (readl(reg->iop2drv_doorbell_reg)
509 & ARCMSR_IOP2DRV_MESSAGE_CMD_DONE) {
510 writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN
511 , reg->iop2drv_doorbell_reg);
517 } while (Retries++ < 20);/*max 20 sec*/
521 static void arcmsr_abort_hba_allcmd(struct AdapterControlBlock *acb)
523 struct MessageUnit_A __iomem *reg = acb->pmuA;
525 writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, ®->inbound_msgaddr0);
526 if (arcmsr_hba_wait_msgint_ready(acb))
528 "arcmsr%d: wait 'abort all outstanding command' timeout \n"
529 , acb->host->host_no);
532 static void arcmsr_abort_hbb_allcmd(struct AdapterControlBlock *acb)
534 struct MessageUnit_B *reg = acb->pmuB;
536 writel(ARCMSR_MESSAGE_ABORT_CMD, reg->drv2iop_doorbell_reg);
537 if (arcmsr_hbb_wait_msgint_ready(acb))
539 "arcmsr%d: wait 'abort all outstanding command' timeout \n"
540 , acb->host->host_no);
543 static void arcmsr_abort_allcmd(struct AdapterControlBlock *acb)
545 switch (acb->adapter_type) {
546 case ACB_ADAPTER_TYPE_A: {
547 arcmsr_abort_hba_allcmd(acb);
551 case ACB_ADAPTER_TYPE_B: {
552 arcmsr_abort_hbb_allcmd(acb);
557 static void arcmsr_pci_unmap_dma(struct CommandControlBlock *ccb)
559 struct scsi_cmnd *pcmd = ccb->pcmd;
561 scsi_dma_unmap(pcmd);
564 static void arcmsr_ccb_complete(struct CommandControlBlock *ccb, int stand_flag)
566 struct AdapterControlBlock *acb = ccb->acb;
567 struct scsi_cmnd *pcmd = ccb->pcmd;
569 arcmsr_pci_unmap_dma(ccb);
571 atomic_dec(&acb->ccboutstandingcount);
572 ccb->startdone = ARCMSR_CCB_DONE;
574 list_add_tail(&ccb->list, &acb->ccb_free_list);
575 pcmd->scsi_done(pcmd);
578 static void arcmsr_flush_hba_cache(struct AdapterControlBlock *acb)
580 struct MessageUnit_A __iomem *reg = acb->pmuA;
581 int retry_count = 30;
583 writel(ARCMSR_INBOUND_MESG0_FLUSH_CACHE, ®->inbound_msgaddr0);
585 if (!arcmsr_hba_wait_msgint_ready(acb))
589 printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
590 timeout, retry count down = %d \n", acb->host->host_no, retry_count);
592 } while (retry_count != 0);
595 static void arcmsr_flush_hbb_cache(struct AdapterControlBlock *acb)
597 struct MessageUnit_B *reg = acb->pmuB;
598 int retry_count = 30;
600 writel(ARCMSR_MESSAGE_FLUSH_CACHE, reg->drv2iop_doorbell_reg);
602 if (!arcmsr_hbb_wait_msgint_ready(acb))
606 printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
607 timeout,retry count down = %d \n", acb->host->host_no, retry_count);
609 } while (retry_count != 0);
612 static void arcmsr_flush_adapter_cache(struct AdapterControlBlock *acb)
614 switch (acb->adapter_type) {
616 case ACB_ADAPTER_TYPE_A: {
617 arcmsr_flush_hba_cache(acb);
621 case ACB_ADAPTER_TYPE_B: {
622 arcmsr_flush_hbb_cache(acb);
627 static void arcmsr_report_sense_info(struct CommandControlBlock *ccb)
630 struct scsi_cmnd *pcmd = ccb->pcmd;
631 struct SENSE_DATA *sensebuffer = (struct SENSE_DATA *)pcmd->sense_buffer;
633 pcmd->result = DID_OK << 16;
635 int sense_data_length =
636 sizeof(struct SENSE_DATA) < SCSI_SENSE_BUFFERSIZE
637 ? sizeof(struct SENSE_DATA) : SCSI_SENSE_BUFFERSIZE;
638 memset(sensebuffer, 0, SCSI_SENSE_BUFFERSIZE);
639 memcpy(sensebuffer, ccb->arcmsr_cdb.SenseData, sense_data_length);
640 sensebuffer->ErrorCode = SCSI_SENSE_CURRENT_ERRORS;
641 sensebuffer->Valid = 1;
645 static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb)
648 switch (acb->adapter_type) {
650 case ACB_ADAPTER_TYPE_A : {
651 struct MessageUnit_A __iomem *reg = acb->pmuA;
652 orig_mask = readl(®->outbound_intmask)|\
653 ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE;
654 writel(orig_mask|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE, \
655 ®->outbound_intmask);
659 case ACB_ADAPTER_TYPE_B : {
660 struct MessageUnit_B *reg = acb->pmuB;
661 orig_mask = readl(reg->iop2drv_doorbell_mask_reg) & \
662 (~ARCMSR_IOP2DRV_MESSAGE_CMD_DONE);
663 writel(0, reg->iop2drv_doorbell_mask_reg);
670 static void arcmsr_report_ccb_state(struct AdapterControlBlock *acb, \
671 struct CommandControlBlock *ccb, uint32_t flag_ccb)
675 id = ccb->pcmd->device->id;
676 lun = ccb->pcmd->device->lun;
677 if (!(flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR)) {
678 if (acb->devstate[id][lun] == ARECA_RAID_GONE)
679 acb->devstate[id][lun] = ARECA_RAID_GOOD;
680 ccb->pcmd->result = DID_OK << 16;
681 arcmsr_ccb_complete(ccb, 1);
683 switch (ccb->arcmsr_cdb.DeviceStatus) {
684 case ARCMSR_DEV_SELECT_TIMEOUT: {
685 acb->devstate[id][lun] = ARECA_RAID_GONE;
686 ccb->pcmd->result = DID_NO_CONNECT << 16;
687 arcmsr_ccb_complete(ccb, 1);
691 case ARCMSR_DEV_ABORTED:
693 case ARCMSR_DEV_INIT_FAIL: {
694 acb->devstate[id][lun] = ARECA_RAID_GONE;
695 ccb->pcmd->result = DID_BAD_TARGET << 16;
696 arcmsr_ccb_complete(ccb, 1);
700 case ARCMSR_DEV_CHECK_CONDITION: {
701 acb->devstate[id][lun] = ARECA_RAID_GOOD;
702 arcmsr_report_sense_info(ccb);
703 arcmsr_ccb_complete(ccb, 1);
709 "arcmsr%d: scsi id = %d lun = %d"
710 " isr get command error done, "
711 "but got unknown DeviceStatus = 0x%x \n"
715 , ccb->arcmsr_cdb.DeviceStatus);
716 acb->devstate[id][lun] = ARECA_RAID_GONE;
717 ccb->pcmd->result = DID_NO_CONNECT << 16;
718 arcmsr_ccb_complete(ccb, 1);
724 static void arcmsr_drain_donequeue(struct AdapterControlBlock *acb, uint32_t flag_ccb)
727 struct CommandControlBlock *ccb;
729 ccb = (struct CommandControlBlock *)(acb->vir2phy_offset + (flag_ccb << 5));
730 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
731 if (ccb->startdone == ARCMSR_CCB_ABORTED) {
732 struct scsi_cmnd *abortcmd = ccb->pcmd;
734 abortcmd->result |= DID_ABORT << 16;
735 arcmsr_ccb_complete(ccb, 1);
736 printk(KERN_NOTICE "arcmsr%d: ccb ='0x%p' \
737 isr got aborted command \n", acb->host->host_no, ccb);
740 printk(KERN_NOTICE "arcmsr%d: isr get an illegal ccb command \
742 "ccb = '0x%p' ccbacb = '0x%p' startdone = 0x%x"
743 " ccboutstandingcount = %d \n"
749 , atomic_read(&acb->ccboutstandingcount));
751 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
754 static void arcmsr_done4abort_postqueue(struct AdapterControlBlock *acb)
759 switch (acb->adapter_type) {
761 case ACB_ADAPTER_TYPE_A: {
762 struct MessageUnit_A __iomem *reg = acb->pmuA;
763 uint32_t outbound_intstatus;
764 outbound_intstatus = readl(®->outbound_intstatus) &
765 acb->outbound_int_enable;
766 /*clear and abort all outbound posted Q*/
767 writel(outbound_intstatus, ®->outbound_intstatus);/*clear interrupt*/
768 while (((flag_ccb = readl(®->outbound_queueport)) != 0xFFFFFFFF)
769 && (i++ < ARCMSR_MAX_OUTSTANDING_CMD)) {
770 arcmsr_drain_donequeue(acb, flag_ccb);
775 case ACB_ADAPTER_TYPE_B: {
776 struct MessageUnit_B *reg = acb->pmuB;
777 /*clear all outbound posted Q*/
778 for (i = 0; i < ARCMSR_MAX_HBB_POSTQUEUE; i++) {
779 if ((flag_ccb = readl(®->done_qbuffer[i])) != 0) {
780 writel(0, ®->done_qbuffer[i]);
781 arcmsr_drain_donequeue(acb, flag_ccb);
783 writel(0, ®->post_qbuffer[i]);
785 reg->doneq_index = 0;
786 reg->postq_index = 0;
791 static void arcmsr_remove(struct pci_dev *pdev)
793 struct Scsi_Host *host = pci_get_drvdata(pdev);
794 struct AdapterControlBlock *acb =
795 (struct AdapterControlBlock *) host->hostdata;
798 arcmsr_free_sysfs_attr(acb);
799 scsi_remove_host(host);
800 arcmsr_stop_adapter_bgrb(acb);
801 arcmsr_flush_adapter_cache(acb);
802 arcmsr_disable_outbound_ints(acb);
803 acb->acb_flags |= ACB_F_SCSISTOPADAPTER;
804 acb->acb_flags &= ~ACB_F_IOP_INITED;
806 for (poll_count = 0; poll_count < ARCMSR_MAX_OUTSTANDING_CMD; poll_count++) {
807 if (!atomic_read(&acb->ccboutstandingcount))
809 arcmsr_interrupt(acb);/* FIXME: need spinlock */
813 if (atomic_read(&acb->ccboutstandingcount)) {
816 arcmsr_abort_allcmd(acb);
817 arcmsr_done4abort_postqueue(acb);
818 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
819 struct CommandControlBlock *ccb = acb->pccb_pool[i];
820 if (ccb->startdone == ARCMSR_CCB_START) {
821 ccb->startdone = ARCMSR_CCB_ABORTED;
822 ccb->pcmd->result = DID_ABORT << 16;
823 arcmsr_ccb_complete(ccb, 1);
828 free_irq(pdev->irq, acb);
829 arcmsr_free_ccb_pool(acb);
830 pci_release_regions(pdev);
834 pci_disable_device(pdev);
835 pci_set_drvdata(pdev, NULL);
838 static void arcmsr_shutdown(struct pci_dev *pdev)
840 struct Scsi_Host *host = pci_get_drvdata(pdev);
841 struct AdapterControlBlock *acb =
842 (struct AdapterControlBlock *)host->hostdata;
844 arcmsr_stop_adapter_bgrb(acb);
845 arcmsr_flush_adapter_cache(acb);
848 static int arcmsr_module_init(void)
852 error = pci_register_driver(&arcmsr_pci_driver);
856 static void arcmsr_module_exit(void)
858 pci_unregister_driver(&arcmsr_pci_driver);
860 module_init(arcmsr_module_init);
861 module_exit(arcmsr_module_exit);
863 static void arcmsr_enable_outbound_ints(struct AdapterControlBlock *acb, \
868 switch (acb->adapter_type) {
870 case ACB_ADAPTER_TYPE_A : {
871 struct MessageUnit_A __iomem *reg = acb->pmuA;
872 mask = intmask_org & ~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE |
873 ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE);
874 writel(mask, ®->outbound_intmask);
875 acb->outbound_int_enable = ~(intmask_org & mask) & 0x000000ff;
879 case ACB_ADAPTER_TYPE_B : {
880 struct MessageUnit_B *reg = acb->pmuB;
881 mask = intmask_org | (ARCMSR_IOP2DRV_DATA_WRITE_OK | \
882 ARCMSR_IOP2DRV_DATA_READ_OK | ARCMSR_IOP2DRV_CDB_DONE);
883 writel(mask, reg->iop2drv_doorbell_mask_reg);
884 acb->outbound_int_enable = (intmask_org | mask) & 0x0000000f;
889 static void arcmsr_build_ccb(struct AdapterControlBlock *acb,
890 struct CommandControlBlock *ccb, struct scsi_cmnd *pcmd)
892 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
893 int8_t *psge = (int8_t *)&arcmsr_cdb->u;
894 __le32 address_lo, address_hi;
895 int arccdbsize = 0x30;
899 memset(arcmsr_cdb, 0, sizeof(struct ARCMSR_CDB));
901 arcmsr_cdb->TargetID = pcmd->device->id;
902 arcmsr_cdb->LUN = pcmd->device->lun;
903 arcmsr_cdb->Function = 1;
904 arcmsr_cdb->CdbLength = (uint8_t)pcmd->cmd_len;
905 arcmsr_cdb->Context = (unsigned long)arcmsr_cdb;
906 memcpy(arcmsr_cdb->Cdb, pcmd->cmnd, pcmd->cmd_len);
908 nseg = scsi_dma_map(pcmd);
913 int i, cdb_sgcount = 0;
914 struct scatterlist *sg;
916 /* map stor port SG list to our iop SG List. */
917 scsi_for_each_sg(pcmd, sg, nseg, i) {
918 /* Get the physical address of the current data pointer */
919 length = cpu_to_le32(sg_dma_len(sg));
920 address_lo = cpu_to_le32(dma_addr_lo32(sg_dma_address(sg)));
921 address_hi = cpu_to_le32(dma_addr_hi32(sg_dma_address(sg)));
922 if (address_hi == 0) {
923 struct SG32ENTRY *pdma_sg = (struct SG32ENTRY *)psge;
925 pdma_sg->address = address_lo;
926 pdma_sg->length = length;
927 psge += sizeof (struct SG32ENTRY);
928 arccdbsize += sizeof (struct SG32ENTRY);
930 struct SG64ENTRY *pdma_sg = (struct SG64ENTRY *)psge;
932 pdma_sg->addresshigh = address_hi;
933 pdma_sg->address = address_lo;
934 pdma_sg->length = length|cpu_to_le32(IS_SG64_ADDR);
935 psge += sizeof (struct SG64ENTRY);
936 arccdbsize += sizeof (struct SG64ENTRY);
940 arcmsr_cdb->sgcount = (uint8_t)cdb_sgcount;
941 arcmsr_cdb->DataLength = scsi_bufflen(pcmd);
942 if ( arccdbsize > 256)
943 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_SGL_BSIZE;
945 if (pcmd->sc_data_direction == DMA_TO_DEVICE ) {
946 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_WRITE;
947 ccb->ccb_flags |= CCB_FLAG_WRITE;
951 static void arcmsr_post_ccb(struct AdapterControlBlock *acb, struct CommandControlBlock *ccb)
953 uint32_t cdb_shifted_phyaddr = ccb->cdb_shifted_phyaddr;
954 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
955 atomic_inc(&acb->ccboutstandingcount);
956 ccb->startdone = ARCMSR_CCB_START;
958 switch (acb->adapter_type) {
959 case ACB_ADAPTER_TYPE_A: {
960 struct MessageUnit_A __iomem *reg = acb->pmuA;
962 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE)
963 writel(cdb_shifted_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,
964 ®->inbound_queueport);
966 writel(cdb_shifted_phyaddr, ®->inbound_queueport);
971 case ACB_ADAPTER_TYPE_B: {
972 struct MessageUnit_B *reg = acb->pmuB;
973 uint32_t ending_index, index = reg->postq_index;
975 ending_index = ((index + 1) % ARCMSR_MAX_HBB_POSTQUEUE);
976 writel(0, ®->post_qbuffer[ending_index]);
977 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE) {
978 writel(cdb_shifted_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,\
979 ®->post_qbuffer[index]);
982 writel(cdb_shifted_phyaddr, ®->post_qbuffer[index]);
985 index %= ARCMSR_MAX_HBB_POSTQUEUE;/*if last index number set it to 0 */
986 reg->postq_index = index;
987 writel(ARCMSR_DRV2IOP_CDB_POSTED, reg->drv2iop_doorbell_reg);
993 static void arcmsr_stop_hba_bgrb(struct AdapterControlBlock *acb)
995 struct MessageUnit_A __iomem *reg = acb->pmuA;
996 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
997 writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, ®->inbound_msgaddr0);
999 if (arcmsr_hba_wait_msgint_ready(acb)) {
1001 "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
1002 , acb->host->host_no);
1006 static void arcmsr_stop_hbb_bgrb(struct AdapterControlBlock *acb)
1008 struct MessageUnit_B *reg = acb->pmuB;
1009 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
1010 writel(ARCMSR_MESSAGE_STOP_BGRB, reg->drv2iop_doorbell_reg);
1012 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1014 "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
1015 , acb->host->host_no);
1019 static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb)
1021 switch (acb->adapter_type) {
1022 case ACB_ADAPTER_TYPE_A: {
1023 arcmsr_stop_hba_bgrb(acb);
1027 case ACB_ADAPTER_TYPE_B: {
1028 arcmsr_stop_hbb_bgrb(acb);
1034 static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb)
1036 switch (acb->adapter_type) {
1037 case ACB_ADAPTER_TYPE_A: {
1041 case ACB_ADAPTER_TYPE_B: {
1042 struct MessageUnit_B *reg = acb->pmuB;
1043 iounmap(reg->drv2iop_doorbell_reg - ARCMSR_DRV2IOP_DOORBELL);
1044 iounmap(reg->ioctl_wbuffer_reg - ARCMSR_IOCTL_WBUFFER);
1047 dma_free_coherent(&acb->pdev->dev,
1048 ARCMSR_MAX_FREECCB_NUM * sizeof (struct CommandControlBlock) + 0x20,
1050 acb->dma_coherent_handle);
1053 void arcmsr_iop_message_read(struct AdapterControlBlock *acb)
1055 switch (acb->adapter_type) {
1056 case ACB_ADAPTER_TYPE_A: {
1057 struct MessageUnit_A __iomem *reg = acb->pmuA;
1058 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, ®->inbound_doorbell);
1062 case ACB_ADAPTER_TYPE_B: {
1063 struct MessageUnit_B *reg = acb->pmuB;
1064 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell_reg);
1070 static void arcmsr_iop_message_wrote(struct AdapterControlBlock *acb)
1072 switch (acb->adapter_type) {
1073 case ACB_ADAPTER_TYPE_A: {
1074 struct MessageUnit_A __iomem *reg = acb->pmuA;
1076 ** push inbound doorbell tell iop, driver data write ok
1077 ** and wait reply on next hwinterrupt for next Qbuffer post
1079 writel(ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK, ®->inbound_doorbell);
1083 case ACB_ADAPTER_TYPE_B: {
1084 struct MessageUnit_B *reg = acb->pmuB;
1086 ** push inbound doorbell tell iop, driver data write ok
1087 ** and wait reply on next hwinterrupt for next Qbuffer post
1089 writel(ARCMSR_DRV2IOP_DATA_WRITE_OK, reg->drv2iop_doorbell_reg);
1095 struct QBUFFER __iomem *arcmsr_get_iop_rqbuffer(struct AdapterControlBlock *acb)
1097 struct QBUFFER __iomem *qbuffer = NULL;
1099 switch (acb->adapter_type) {
1101 case ACB_ADAPTER_TYPE_A: {
1102 struct MessageUnit_A __iomem *reg = acb->pmuA;
1103 qbuffer = (struct QBUFFER __iomem *)®->message_rbuffer;
1107 case ACB_ADAPTER_TYPE_B: {
1108 struct MessageUnit_B *reg = acb->pmuB;
1109 qbuffer = (struct QBUFFER __iomem *)reg->ioctl_rbuffer_reg;
1116 static struct QBUFFER __iomem *arcmsr_get_iop_wqbuffer(struct AdapterControlBlock *acb)
1118 struct QBUFFER __iomem *pqbuffer = NULL;
1120 switch (acb->adapter_type) {
1122 case ACB_ADAPTER_TYPE_A: {
1123 struct MessageUnit_A __iomem *reg = acb->pmuA;
1124 pqbuffer = (struct QBUFFER __iomem *) ®->message_wbuffer;
1128 case ACB_ADAPTER_TYPE_B: {
1129 struct MessageUnit_B *reg = acb->pmuB;
1130 pqbuffer = (struct QBUFFER __iomem *)reg->ioctl_wbuffer_reg;
1137 static void arcmsr_iop2drv_data_wrote_handle(struct AdapterControlBlock *acb)
1139 struct QBUFFER __iomem *prbuffer;
1140 struct QBUFFER *pQbuffer;
1141 uint8_t __iomem *iop_data;
1142 int32_t my_empty_len, iop_len, rqbuf_firstindex, rqbuf_lastindex;
1144 rqbuf_lastindex = acb->rqbuf_lastindex;
1145 rqbuf_firstindex = acb->rqbuf_firstindex;
1146 prbuffer = arcmsr_get_iop_rqbuffer(acb);
1147 iop_data = (uint8_t __iomem *)prbuffer->data;
1148 iop_len = prbuffer->data_len;
1149 my_empty_len = (rqbuf_firstindex - rqbuf_lastindex -1)&(ARCMSR_MAX_QBUFFER -1);
1151 if (my_empty_len >= iop_len)
1153 while (iop_len > 0) {
1154 pQbuffer = (struct QBUFFER *)&acb->rqbuffer[rqbuf_lastindex];
1155 memcpy(pQbuffer, iop_data,1);
1157 rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1161 acb->rqbuf_lastindex = rqbuf_lastindex;
1162 arcmsr_iop_message_read(acb);
1166 acb->acb_flags |= ACB_F_IOPDATA_OVERFLOW;
1170 static void arcmsr_iop2drv_data_read_handle(struct AdapterControlBlock *acb)
1172 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_READED;
1173 if (acb->wqbuf_firstindex != acb->wqbuf_lastindex) {
1175 struct QBUFFER __iomem *pwbuffer;
1176 uint8_t __iomem *iop_data;
1177 int32_t allxfer_len = 0;
1179 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
1180 pwbuffer = arcmsr_get_iop_wqbuffer(acb);
1181 iop_data = (uint8_t __iomem *)pwbuffer->data;
1183 while ((acb->wqbuf_firstindex != acb->wqbuf_lastindex) && \
1184 (allxfer_len < 124)) {
1185 pQbuffer = &acb->wqbuffer[acb->wqbuf_firstindex];
1186 memcpy(iop_data, pQbuffer, 1);
1187 acb->wqbuf_firstindex++;
1188 acb->wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1192 pwbuffer->data_len = allxfer_len;
1194 arcmsr_iop_message_wrote(acb);
1197 if (acb->wqbuf_firstindex == acb->wqbuf_lastindex) {
1198 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_CLEARED;
1202 static void arcmsr_hba_doorbell_isr(struct AdapterControlBlock *acb)
1204 uint32_t outbound_doorbell;
1205 struct MessageUnit_A __iomem *reg = acb->pmuA;
1207 outbound_doorbell = readl(®->outbound_doorbell);
1208 writel(outbound_doorbell, ®->outbound_doorbell);
1209 if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK) {
1210 arcmsr_iop2drv_data_wrote_handle(acb);
1213 if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_READ_OK) {
1214 arcmsr_iop2drv_data_read_handle(acb);
1218 static void arcmsr_hba_postqueue_isr(struct AdapterControlBlock *acb)
1221 struct MessageUnit_A __iomem *reg = acb->pmuA;
1223 while ((flag_ccb = readl(®->outbound_queueport)) != 0xFFFFFFFF) {
1224 arcmsr_drain_donequeue(acb, flag_ccb);
1228 static void arcmsr_hbb_postqueue_isr(struct AdapterControlBlock *acb)
1232 struct MessageUnit_B *reg = acb->pmuB;
1234 index = reg->doneq_index;
1236 while ((flag_ccb = readl(®->done_qbuffer[index])) != 0) {
1237 writel(0, ®->done_qbuffer[index]);
1238 arcmsr_drain_donequeue(acb, flag_ccb);
1240 index %= ARCMSR_MAX_HBB_POSTQUEUE;
1241 reg->doneq_index = index;
1245 static int arcmsr_handle_hba_isr(struct AdapterControlBlock *acb)
1247 uint32_t outbound_intstatus;
1248 struct MessageUnit_A __iomem *reg = acb->pmuA;
1250 outbound_intstatus = readl(®->outbound_intstatus) & \
1251 acb->outbound_int_enable;
1252 if (!(outbound_intstatus & ARCMSR_MU_OUTBOUND_HANDLE_INT)) {
1255 writel(outbound_intstatus, ®->outbound_intstatus);
1256 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_DOORBELL_INT) {
1257 arcmsr_hba_doorbell_isr(acb);
1259 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_POSTQUEUE_INT) {
1260 arcmsr_hba_postqueue_isr(acb);
1265 static int arcmsr_handle_hbb_isr(struct AdapterControlBlock *acb)
1267 uint32_t outbound_doorbell;
1268 struct MessageUnit_B *reg = acb->pmuB;
1270 outbound_doorbell = readl(reg->iop2drv_doorbell_reg) & \
1271 acb->outbound_int_enable;
1272 if (!outbound_doorbell)
1275 writel(~outbound_doorbell, reg->iop2drv_doorbell_reg);
1277 if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_WRITE_OK) {
1278 arcmsr_iop2drv_data_wrote_handle(acb);
1280 if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_READ_OK) {
1281 arcmsr_iop2drv_data_read_handle(acb);
1283 if (outbound_doorbell & ARCMSR_IOP2DRV_CDB_DONE) {
1284 arcmsr_hbb_postqueue_isr(acb);
1290 static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb)
1292 switch (acb->adapter_type) {
1293 case ACB_ADAPTER_TYPE_A: {
1294 if (arcmsr_handle_hba_isr(acb)) {
1300 case ACB_ADAPTER_TYPE_B: {
1301 if (arcmsr_handle_hbb_isr(acb)) {
1310 static void arcmsr_iop_parking(struct AdapterControlBlock *acb)
1313 /* stop adapter background rebuild */
1314 if (acb->acb_flags & ACB_F_MSG_START_BGRB) {
1315 uint32_t intmask_org;
1316 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
1317 intmask_org = arcmsr_disable_outbound_ints(acb);
1318 arcmsr_stop_adapter_bgrb(acb);
1319 arcmsr_flush_adapter_cache(acb);
1320 arcmsr_enable_outbound_ints(acb, intmask_org);
1325 void arcmsr_post_ioctldata2iop(struct AdapterControlBlock *acb)
1327 int32_t wqbuf_firstindex, wqbuf_lastindex;
1329 struct QBUFFER __iomem *pwbuffer;
1330 uint8_t __iomem *iop_data;
1331 int32_t allxfer_len = 0;
1333 pwbuffer = arcmsr_get_iop_wqbuffer(acb);
1334 iop_data = (uint8_t __iomem *)pwbuffer->data;
1335 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_READED) {
1336 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
1337 wqbuf_firstindex = acb->wqbuf_firstindex;
1338 wqbuf_lastindex = acb->wqbuf_lastindex;
1339 while ((wqbuf_firstindex != wqbuf_lastindex) && (allxfer_len < 124)) {
1340 pQbuffer = &acb->wqbuffer[wqbuf_firstindex];
1341 memcpy(iop_data, pQbuffer, 1);
1343 wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1347 acb->wqbuf_firstindex = wqbuf_firstindex;
1348 pwbuffer->data_len = allxfer_len;
1349 arcmsr_iop_message_wrote(acb);
1353 static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb, \
1354 struct scsi_cmnd *cmd)
1356 struct CMD_MESSAGE_FIELD *pcmdmessagefld;
1357 int retvalue = 0, transfer_len = 0;
1359 struct scatterlist *sg;
1360 uint32_t controlcode = (uint32_t ) cmd->cmnd[5] << 24 |
1361 (uint32_t ) cmd->cmnd[6] << 16 |
1362 (uint32_t ) cmd->cmnd[7] << 8 |
1363 (uint32_t ) cmd->cmnd[8];
1364 /* 4 bytes: Areca io control code */
1366 sg = scsi_sglist(cmd);
1367 buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
1368 if (scsi_sg_count(cmd) > 1) {
1369 retvalue = ARCMSR_MESSAGE_FAIL;
1372 transfer_len += sg->length;
1374 if (transfer_len > sizeof(struct CMD_MESSAGE_FIELD)) {
1375 retvalue = ARCMSR_MESSAGE_FAIL;
1378 pcmdmessagefld = (struct CMD_MESSAGE_FIELD *) buffer;
1379 switch(controlcode) {
1381 case ARCMSR_MESSAGE_READ_RQBUFFER: {
1382 unsigned long *ver_addr;
1383 dma_addr_t buf_handle;
1384 uint8_t *pQbuffer, *ptmpQbuffer;
1385 int32_t allxfer_len = 0;
1387 ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
1389 retvalue = ARCMSR_MESSAGE_FAIL;
1392 ptmpQbuffer = (uint8_t *) ver_addr;
1393 while ((acb->rqbuf_firstindex != acb->rqbuf_lastindex)
1394 && (allxfer_len < 1031)) {
1395 pQbuffer = &acb->rqbuffer[acb->rqbuf_firstindex];
1396 memcpy(ptmpQbuffer, pQbuffer, 1);
1397 acb->rqbuf_firstindex++;
1398 acb->rqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1402 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1404 struct QBUFFER __iomem *prbuffer;
1405 uint8_t __iomem *iop_data;
1408 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1409 prbuffer = arcmsr_get_iop_rqbuffer(acb);
1410 iop_data = prbuffer->data;
1411 iop_len = readl(&prbuffer->data_len);
1412 while (iop_len > 0) {
1413 acb->rqbuffer[acb->rqbuf_lastindex] = readb(iop_data);
1414 acb->rqbuf_lastindex++;
1415 acb->rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1419 arcmsr_iop_message_read(acb);
1421 memcpy(pcmdmessagefld->messagedatabuffer, (uint8_t *)ver_addr, allxfer_len);
1422 pcmdmessagefld->cmdmessage.Length = allxfer_len;
1423 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1424 pci_free_consistent(acb->pdev, 1032, ver_addr, buf_handle);
1428 case ARCMSR_MESSAGE_WRITE_WQBUFFER: {
1429 unsigned long *ver_addr;
1430 dma_addr_t buf_handle;
1431 int32_t my_empty_len, user_len, wqbuf_firstindex, wqbuf_lastindex;
1432 uint8_t *pQbuffer, *ptmpuserbuffer;
1434 ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
1436 retvalue = ARCMSR_MESSAGE_FAIL;
1439 ptmpuserbuffer = (uint8_t *)ver_addr;
1440 user_len = pcmdmessagefld->cmdmessage.Length;
1441 memcpy(ptmpuserbuffer, pcmdmessagefld->messagedatabuffer, user_len);
1442 wqbuf_lastindex = acb->wqbuf_lastindex;
1443 wqbuf_firstindex = acb->wqbuf_firstindex;
1444 if (wqbuf_lastindex != wqbuf_firstindex) {
1445 struct SENSE_DATA *sensebuffer =
1446 (struct SENSE_DATA *)cmd->sense_buffer;
1447 arcmsr_post_ioctldata2iop(acb);
1448 /* has error report sensedata */
1449 sensebuffer->ErrorCode = 0x70;
1450 sensebuffer->SenseKey = ILLEGAL_REQUEST;
1451 sensebuffer->AdditionalSenseLength = 0x0A;
1452 sensebuffer->AdditionalSenseCode = 0x20;
1453 sensebuffer->Valid = 1;
1454 retvalue = ARCMSR_MESSAGE_FAIL;
1456 my_empty_len = (wqbuf_firstindex-wqbuf_lastindex - 1)
1457 &(ARCMSR_MAX_QBUFFER - 1);
1458 if (my_empty_len >= user_len) {
1459 while (user_len > 0) {
1461 &acb->wqbuffer[acb->wqbuf_lastindex];
1462 memcpy(pQbuffer, ptmpuserbuffer, 1);
1463 acb->wqbuf_lastindex++;
1464 acb->wqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1468 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {
1470 ~ACB_F_MESSAGE_WQBUFFER_CLEARED;
1471 arcmsr_post_ioctldata2iop(acb);
1474 /* has error report sensedata */
1475 struct SENSE_DATA *sensebuffer =
1476 (struct SENSE_DATA *)cmd->sense_buffer;
1477 sensebuffer->ErrorCode = 0x70;
1478 sensebuffer->SenseKey = ILLEGAL_REQUEST;
1479 sensebuffer->AdditionalSenseLength = 0x0A;
1480 sensebuffer->AdditionalSenseCode = 0x20;
1481 sensebuffer->Valid = 1;
1482 retvalue = ARCMSR_MESSAGE_FAIL;
1485 pci_free_consistent(acb->pdev, 1032, ver_addr, buf_handle);
1489 case ARCMSR_MESSAGE_CLEAR_RQBUFFER: {
1490 uint8_t *pQbuffer = acb->rqbuffer;
1492 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1493 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1494 arcmsr_iop_message_read(acb);
1496 acb->acb_flags |= ACB_F_MESSAGE_RQBUFFER_CLEARED;
1497 acb->rqbuf_firstindex = 0;
1498 acb->rqbuf_lastindex = 0;
1499 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1500 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1504 case ARCMSR_MESSAGE_CLEAR_WQBUFFER: {
1505 uint8_t *pQbuffer = acb->wqbuffer;
1507 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1508 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1509 arcmsr_iop_message_read(acb);
1512 (ACB_F_MESSAGE_WQBUFFER_CLEARED |
1513 ACB_F_MESSAGE_WQBUFFER_READED);
1514 acb->wqbuf_firstindex = 0;
1515 acb->wqbuf_lastindex = 0;
1516 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1517 pcmdmessagefld->cmdmessage.ReturnCode =
1518 ARCMSR_MESSAGE_RETURNCODE_OK;
1522 case ARCMSR_MESSAGE_CLEAR_ALLQBUFFER: {
1525 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1526 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1527 arcmsr_iop_message_read(acb);
1530 (ACB_F_MESSAGE_WQBUFFER_CLEARED
1531 | ACB_F_MESSAGE_RQBUFFER_CLEARED
1532 | ACB_F_MESSAGE_WQBUFFER_READED);
1533 acb->rqbuf_firstindex = 0;
1534 acb->rqbuf_lastindex = 0;
1535 acb->wqbuf_firstindex = 0;
1536 acb->wqbuf_lastindex = 0;
1537 pQbuffer = acb->rqbuffer;
1538 memset(pQbuffer, 0, sizeof(struct QBUFFER));
1539 pQbuffer = acb->wqbuffer;
1540 memset(pQbuffer, 0, sizeof(struct QBUFFER));
1541 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1545 case ARCMSR_MESSAGE_RETURN_CODE_3F: {
1546 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_3F;
1550 case ARCMSR_MESSAGE_SAY_HELLO: {
1551 int8_t *hello_string = "Hello! I am ARCMSR";
1553 memcpy(pcmdmessagefld->messagedatabuffer, hello_string
1554 , (int16_t)strlen(hello_string));
1555 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1559 case ARCMSR_MESSAGE_SAY_GOODBYE:
1560 arcmsr_iop_parking(acb);
1563 case ARCMSR_MESSAGE_FLUSH_ADAPTER_CACHE:
1564 arcmsr_flush_adapter_cache(acb);
1568 retvalue = ARCMSR_MESSAGE_FAIL;
1571 sg = scsi_sglist(cmd);
1572 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
1576 static struct CommandControlBlock *arcmsr_get_freeccb(struct AdapterControlBlock *acb)
1578 struct list_head *head = &acb->ccb_free_list;
1579 struct CommandControlBlock *ccb = NULL;
1581 if (!list_empty(head)) {
1582 ccb = list_entry(head->next, struct CommandControlBlock, list);
1583 list_del(head->next);
1588 static void arcmsr_handle_virtual_command(struct AdapterControlBlock *acb,
1589 struct scsi_cmnd *cmd)
1591 switch (cmd->cmnd[0]) {
1593 unsigned char inqdata[36];
1595 struct scatterlist *sg;
1597 if (cmd->device->lun) {
1598 cmd->result = (DID_TIME_OUT << 16);
1599 cmd->scsi_done(cmd);
1602 inqdata[0] = TYPE_PROCESSOR;
1603 /* Periph Qualifier & Periph Dev Type */
1605 /* rem media bit & Dev Type Modifier */
1607 /* ISO, ECMA, & ANSI versions */
1609 /* length of additional data */
1610 strncpy(&inqdata[8], "Areca ", 8);
1611 /* Vendor Identification */
1612 strncpy(&inqdata[16], "RAID controller ", 16);
1613 /* Product Identification */
1614 strncpy(&inqdata[32], "R001", 4); /* Product Revision */
1616 sg = scsi_sglist(cmd);
1617 buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
1619 memcpy(buffer, inqdata, sizeof(inqdata));
1620 sg = scsi_sglist(cmd);
1621 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
1623 cmd->scsi_done(cmd);
1628 if (arcmsr_iop_message_xfer(acb, cmd))
1629 cmd->result = (DID_ERROR << 16);
1630 cmd->scsi_done(cmd);
1634 cmd->scsi_done(cmd);
1638 static int arcmsr_queue_command(struct scsi_cmnd *cmd,
1639 void (* done)(struct scsi_cmnd *))
1641 struct Scsi_Host *host = cmd->device->host;
1642 struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
1643 struct CommandControlBlock *ccb;
1644 int target = cmd->device->id;
1645 int lun = cmd->device->lun;
1647 cmd->scsi_done = done;
1648 cmd->host_scribble = NULL;
1650 if (acb->acb_flags & ACB_F_BUS_RESET) {
1651 printk(KERN_NOTICE "arcmsr%d: bus reset"
1652 " and return busy \n"
1653 , acb->host->host_no);
1654 return SCSI_MLQUEUE_HOST_BUSY;
1657 /* virtual device for iop message transfer */
1658 arcmsr_handle_virtual_command(acb, cmd);
1661 if (acb->devstate[target][lun] == ARECA_RAID_GONE) {
1664 block_cmd = cmd->cmnd[0] & 0x0f;
1665 if (block_cmd == 0x08 || block_cmd == 0x0a) {
1667 "arcmsr%d: block 'read/write'"
1668 "command with gone raid volume"
1669 " Cmd = %2x, TargetId = %d, Lun = %d \n"
1670 , acb->host->host_no
1673 cmd->result = (DID_NO_CONNECT << 16);
1674 cmd->scsi_done(cmd);
1678 if (atomic_read(&acb->ccboutstandingcount) >=
1679 ARCMSR_MAX_OUTSTANDING_CMD)
1680 return SCSI_MLQUEUE_HOST_BUSY;
1682 ccb = arcmsr_get_freeccb(acb);
1684 return SCSI_MLQUEUE_HOST_BUSY;
1686 arcmsr_build_ccb(acb, ccb, cmd);
1687 arcmsr_post_ccb(acb, ccb);
1691 static void arcmsr_get_hba_config(struct AdapterControlBlock *acb)
1693 struct MessageUnit_A __iomem *reg = acb->pmuA;
1694 char *acb_firm_model = acb->firm_model;
1695 char *acb_firm_version = acb->firm_version;
1696 char __iomem *iop_firm_model = (char __iomem *)(®->message_rwbuffer[15]);
1697 char __iomem *iop_firm_version = (char __iomem *)(®->message_rwbuffer[17]);
1700 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, ®->inbound_msgaddr0);
1701 if (arcmsr_hba_wait_msgint_ready(acb)) {
1702 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
1703 miscellaneous data' timeout \n", acb->host->host_no);
1708 *acb_firm_model = readb(iop_firm_model);
1716 *acb_firm_version = readb(iop_firm_version);
1722 printk(KERN_INFO "ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n"
1723 , acb->host->host_no
1724 , acb->firm_version);
1726 acb->firm_request_len = readl(®->message_rwbuffer[1]);
1727 acb->firm_numbers_queue = readl(®->message_rwbuffer[2]);
1728 acb->firm_sdram_size = readl(®->message_rwbuffer[3]);
1729 acb->firm_hd_channels = readl(®->message_rwbuffer[4]);
1732 static void arcmsr_get_hbb_config(struct AdapterControlBlock *acb)
1734 struct MessageUnit_B *reg = acb->pmuB;
1735 uint32_t __iomem *lrwbuffer = reg->msgcode_rwbuffer_reg;
1736 char *acb_firm_model = acb->firm_model;
1737 char *acb_firm_version = acb->firm_version;
1738 char __iomem *iop_firm_model = (char __iomem *)(&lrwbuffer[15]);
1739 /*firm_model,15,60-67*/
1740 char __iomem *iop_firm_version = (char __iomem *)(&lrwbuffer[17]);
1741 /*firm_version,17,68-83*/
1744 writel(ARCMSR_MESSAGE_GET_CONFIG, reg->drv2iop_doorbell_reg);
1745 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1746 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
1747 miscellaneous data' timeout \n", acb->host->host_no);
1753 *acb_firm_model = readb(iop_firm_model);
1762 *acb_firm_version = readb(iop_firm_version);
1768 printk(KERN_INFO "ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n",
1773 acb->firm_request_len = readl(lrwbuffer++);
1774 /*firm_request_len,1,04-07*/
1775 acb->firm_numbers_queue = readl(lrwbuffer++);
1776 /*firm_numbers_queue,2,08-11*/
1777 acb->firm_sdram_size = readl(lrwbuffer++);
1778 /*firm_sdram_size,3,12-15*/
1779 acb->firm_hd_channels = readl(lrwbuffer);
1780 /*firm_ide_channels,4,16-19*/
1783 static void arcmsr_get_firmware_spec(struct AdapterControlBlock *acb)
1785 switch (acb->adapter_type) {
1786 case ACB_ADAPTER_TYPE_A: {
1787 arcmsr_get_hba_config(acb);
1791 case ACB_ADAPTER_TYPE_B: {
1792 arcmsr_get_hbb_config(acb);
1798 static void arcmsr_polling_hba_ccbdone(struct AdapterControlBlock *acb,
1799 struct CommandControlBlock *poll_ccb)
1801 struct MessageUnit_A __iomem *reg = acb->pmuA;
1802 struct CommandControlBlock *ccb;
1803 uint32_t flag_ccb, outbound_intstatus, poll_ccb_done = 0, poll_count = 0;
1805 polling_hba_ccb_retry:
1807 outbound_intstatus = readl(®->outbound_intstatus) & acb->outbound_int_enable;
1808 writel(outbound_intstatus, ®->outbound_intstatus);/*clear interrupt*/
1810 if ((flag_ccb = readl(®->outbound_queueport)) == 0xFFFFFFFF) {
1815 if (poll_count > 100)
1817 goto polling_hba_ccb_retry;
1820 ccb = (struct CommandControlBlock *)(acb->vir2phy_offset + (flag_ccb << 5));
1821 poll_ccb_done = (ccb == poll_ccb) ? 1:0;
1822 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
1823 if ((ccb->startdone == ARCMSR_CCB_ABORTED) || (ccb == poll_ccb)) {
1824 printk(KERN_NOTICE "arcmsr%d: scsi id = %d lun = %d ccb = '0x%p'"
1825 " poll command abort successfully \n"
1826 , acb->host->host_no
1827 , ccb->pcmd->device->id
1828 , ccb->pcmd->device->lun
1830 ccb->pcmd->result = DID_ABORT << 16;
1831 arcmsr_ccb_complete(ccb, 1);
1835 printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
1836 " command done ccb = '0x%p'"
1837 "ccboutstandingcount = %d \n"
1838 , acb->host->host_no
1840 , atomic_read(&acb->ccboutstandingcount));
1843 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
1847 static void arcmsr_polling_hbb_ccbdone(struct AdapterControlBlock *acb, \
1848 struct CommandControlBlock *poll_ccb)
1850 struct MessageUnit_B *reg = acb->pmuB;
1851 struct CommandControlBlock *ccb;
1852 uint32_t flag_ccb, poll_ccb_done = 0, poll_count = 0;
1855 polling_hbb_ccb_retry:
1857 /* clear doorbell interrupt */
1858 writel(ARCMSR_DOORBELL_INT_CLEAR_PATTERN, reg->iop2drv_doorbell_reg);
1860 index = reg->doneq_index;
1861 if ((flag_ccb = readl(®->done_qbuffer[index])) == 0) {
1866 if (poll_count > 100)
1868 goto polling_hbb_ccb_retry;
1871 writel(0, ®->done_qbuffer[index]);
1873 /*if last index number set it to 0 */
1874 index %= ARCMSR_MAX_HBB_POSTQUEUE;
1875 reg->doneq_index = index;
1876 /* check ifcommand done with no error*/
1877 ccb = (struct CommandControlBlock *)\
1878 (acb->vir2phy_offset + (flag_ccb << 5));/*frame must be 32 bytes aligned*/
1879 poll_ccb_done = (ccb == poll_ccb) ? 1:0;
1880 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
1881 if (ccb->startdone == ARCMSR_CCB_ABORTED) {
1882 printk(KERN_NOTICE "arcmsr%d: \
1883 scsi id = %d lun = %d ccb = '0x%p' poll command abort successfully \n"
1885 ,ccb->pcmd->device->id
1886 ,ccb->pcmd->device->lun
1888 ccb->pcmd->result = DID_ABORT << 16;
1889 arcmsr_ccb_complete(ccb, 1);
1892 printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
1893 " command done ccb = '0x%p'"
1894 "ccboutstandingcount = %d \n"
1895 , acb->host->host_no
1897 , atomic_read(&acb->ccboutstandingcount));
1900 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
1901 } /*drain reply FIFO*/
1904 static void arcmsr_polling_ccbdone(struct AdapterControlBlock *acb, \
1905 struct CommandControlBlock *poll_ccb)
1907 switch (acb->adapter_type) {
1909 case ACB_ADAPTER_TYPE_A: {
1910 arcmsr_polling_hba_ccbdone(acb,poll_ccb);
1914 case ACB_ADAPTER_TYPE_B: {
1915 arcmsr_polling_hbb_ccbdone(acb,poll_ccb);
1920 static int arcmsr_iop_confirm(struct AdapterControlBlock *acb)
1922 uint32_t cdb_phyaddr, ccb_phyaddr_hi32;
1923 dma_addr_t dma_coherent_handle;
1925 ********************************************************************
1926 ** here we need to tell iop 331 our freeccb.HighPart
1927 ** if freeccb.HighPart is not zero
1928 ********************************************************************
1930 dma_coherent_handle = acb->dma_coherent_handle;
1931 cdb_phyaddr = (uint32_t)(dma_coherent_handle);
1932 ccb_phyaddr_hi32 = (uint32_t)((cdb_phyaddr >> 16) >> 16);
1934 ***********************************************************************
1935 ** if adapter type B, set window of "post command Q"
1936 ***********************************************************************
1938 switch (acb->adapter_type) {
1940 case ACB_ADAPTER_TYPE_A: {
1941 if (ccb_phyaddr_hi32 != 0) {
1942 struct MessageUnit_A __iomem *reg = acb->pmuA;
1943 uint32_t intmask_org;
1944 intmask_org = arcmsr_disable_outbound_ints(acb);
1945 writel(ARCMSR_SIGNATURE_SET_CONFIG, \
1946 ®->message_rwbuffer[0]);
1947 writel(ccb_phyaddr_hi32, ®->message_rwbuffer[1]);
1948 writel(ARCMSR_INBOUND_MESG0_SET_CONFIG, \
1949 ®->inbound_msgaddr0);
1950 if (arcmsr_hba_wait_msgint_ready(acb)) {
1951 printk(KERN_NOTICE "arcmsr%d: ""set ccb high \
1952 part physical address timeout\n",
1953 acb->host->host_no);
1956 arcmsr_enable_outbound_ints(acb, intmask_org);
1961 case ACB_ADAPTER_TYPE_B: {
1962 unsigned long post_queue_phyaddr;
1963 uint32_t __iomem *rwbuffer;
1965 struct MessageUnit_B *reg = acb->pmuB;
1966 uint32_t intmask_org;
1967 intmask_org = arcmsr_disable_outbound_ints(acb);
1968 reg->postq_index = 0;
1969 reg->doneq_index = 0;
1970 writel(ARCMSR_MESSAGE_SET_POST_WINDOW, reg->drv2iop_doorbell_reg);
1971 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1972 printk(KERN_NOTICE "arcmsr%d:can not set diver mode\n", \
1973 acb->host->host_no);
1976 post_queue_phyaddr = cdb_phyaddr + ARCMSR_MAX_FREECCB_NUM * \
1977 sizeof(struct CommandControlBlock) + offsetof(struct MessageUnit_B, post_qbuffer) ;
1978 rwbuffer = reg->msgcode_rwbuffer_reg;
1979 /* driver "set config" signature */
1980 writel(ARCMSR_SIGNATURE_SET_CONFIG, rwbuffer++);
1981 /* normal should be zero */
1982 writel(ccb_phyaddr_hi32, rwbuffer++);
1983 /* postQ size (256 + 8)*4 */
1984 writel(post_queue_phyaddr, rwbuffer++);
1985 /* doneQ size (256 + 8)*4 */
1986 writel(post_queue_phyaddr + 1056, rwbuffer++);
1987 /* ccb maxQ size must be --> [(256 + 8)*4]*/
1988 writel(1056, rwbuffer);
1990 writel(ARCMSR_MESSAGE_SET_CONFIG, reg->drv2iop_doorbell_reg);
1991 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1992 printk(KERN_NOTICE "arcmsr%d: 'set command Q window' \
1993 timeout \n",acb->host->host_no);
1997 writel(ARCMSR_MESSAGE_START_DRIVER_MODE, reg->drv2iop_doorbell_reg);
1998 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1999 printk(KERN_NOTICE "arcmsr%d: 'can not set diver mode \n"\
2000 ,acb->host->host_no);
2003 arcmsr_enable_outbound_ints(acb, intmask_org);
2010 static void arcmsr_wait_firmware_ready(struct AdapterControlBlock *acb)
2012 uint32_t firmware_state = 0;
2014 switch (acb->adapter_type) {
2016 case ACB_ADAPTER_TYPE_A: {
2017 struct MessageUnit_A __iomem *reg = acb->pmuA;
2019 firmware_state = readl(®->outbound_msgaddr1);
2020 } while ((firmware_state & ARCMSR_OUTBOUND_MESG1_FIRMWARE_OK) == 0);
2024 case ACB_ADAPTER_TYPE_B: {
2025 struct MessageUnit_B *reg = acb->pmuB;
2027 firmware_state = readl(reg->iop2drv_doorbell_reg);
2028 } while ((firmware_state & ARCMSR_MESSAGE_FIRMWARE_OK) == 0);
2034 static void arcmsr_start_hba_bgrb(struct AdapterControlBlock *acb)
2036 struct MessageUnit_A __iomem *reg = acb->pmuA;
2037 acb->acb_flags |= ACB_F_MSG_START_BGRB;
2038 writel(ARCMSR_INBOUND_MESG0_START_BGRB, ®->inbound_msgaddr0);
2039 if (arcmsr_hba_wait_msgint_ready(acb)) {
2040 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
2041 rebulid' timeout \n", acb->host->host_no);
2045 static void arcmsr_start_hbb_bgrb(struct AdapterControlBlock *acb)
2047 struct MessageUnit_B *reg = acb->pmuB;
2048 acb->acb_flags |= ACB_F_MSG_START_BGRB;
2049 writel(ARCMSR_MESSAGE_START_BGRB, reg->drv2iop_doorbell_reg);
2050 if (arcmsr_hbb_wait_msgint_ready(acb)) {
2051 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
2052 rebulid' timeout \n",acb->host->host_no);
2056 static void arcmsr_start_adapter_bgrb(struct AdapterControlBlock *acb)
2058 switch (acb->adapter_type) {
2059 case ACB_ADAPTER_TYPE_A:
2060 arcmsr_start_hba_bgrb(acb);
2062 case ACB_ADAPTER_TYPE_B:
2063 arcmsr_start_hbb_bgrb(acb);
2068 static void arcmsr_clear_doorbell_queue_buffer(struct AdapterControlBlock *acb)
2070 switch (acb->adapter_type) {
2071 case ACB_ADAPTER_TYPE_A: {
2072 struct MessageUnit_A __iomem *reg = acb->pmuA;
2073 uint32_t outbound_doorbell;
2074 /* empty doorbell Qbuffer if door bell ringed */
2075 outbound_doorbell = readl(®->outbound_doorbell);
2076 /*clear doorbell interrupt */
2077 writel(outbound_doorbell, ®->outbound_doorbell);
2078 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, ®->inbound_doorbell);
2082 case ACB_ADAPTER_TYPE_B: {
2083 struct MessageUnit_B *reg = acb->pmuB;
2084 /*clear interrupt and message state*/
2085 writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN, reg->iop2drv_doorbell_reg);
2086 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell_reg);
2087 /* let IOP know data has been read */
2093 static void arcmsr_iop_init(struct AdapterControlBlock *acb)
2095 uint32_t intmask_org;
2097 arcmsr_wait_firmware_ready(acb);
2098 arcmsr_iop_confirm(acb);
2099 /* disable all outbound interrupt */
2100 intmask_org = arcmsr_disable_outbound_ints(acb);
2101 arcmsr_get_firmware_spec(acb);
2102 /*start background rebuild*/
2103 arcmsr_start_adapter_bgrb(acb);
2104 /* empty doorbell Qbuffer if door bell ringed */
2105 arcmsr_clear_doorbell_queue_buffer(acb);
2106 /* enable outbound Post Queue,outbound doorbell Interrupt */
2107 arcmsr_enable_outbound_ints(acb, intmask_org);
2108 acb->acb_flags |= ACB_F_IOP_INITED;
2111 static void arcmsr_iop_reset(struct AdapterControlBlock *acb)
2113 struct CommandControlBlock *ccb;
2114 uint32_t intmask_org;
2117 if (atomic_read(&acb->ccboutstandingcount) != 0) {
2118 /* talk to iop 331 outstanding command aborted */
2119 arcmsr_abort_allcmd(acb);
2121 /* wait for 3 sec for all command aborted*/
2124 /* disable all outbound interrupt */
2125 intmask_org = arcmsr_disable_outbound_ints(acb);
2126 /* clear all outbound posted Q */
2127 arcmsr_done4abort_postqueue(acb);
2128 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2129 ccb = acb->pccb_pool[i];
2130 if (ccb->startdone == ARCMSR_CCB_START) {
2131 ccb->startdone = ARCMSR_CCB_ABORTED;
2132 arcmsr_ccb_complete(ccb, 1);
2135 /* enable all outbound interrupt */
2136 arcmsr_enable_outbound_ints(acb, intmask_org);
2140 static int arcmsr_bus_reset(struct scsi_cmnd *cmd)
2142 struct AdapterControlBlock *acb =
2143 (struct AdapterControlBlock *)cmd->device->host->hostdata;
2147 acb->acb_flags |= ACB_F_BUS_RESET;
2148 for (i = 0; i < 400; i++) {
2149 if (!atomic_read(&acb->ccboutstandingcount))
2151 arcmsr_interrupt(acb);/* FIXME: need spinlock */
2154 arcmsr_iop_reset(acb);
2155 acb->acb_flags &= ~ACB_F_BUS_RESET;
2159 static void arcmsr_abort_one_cmd(struct AdapterControlBlock *acb,
2160 struct CommandControlBlock *ccb)
2164 ccb->startdone = ARCMSR_CCB_ABORTED;
2167 ** Wait for 3 sec for all command done.
2171 intmask = arcmsr_disable_outbound_ints(acb);
2172 arcmsr_polling_ccbdone(acb, ccb);
2173 arcmsr_enable_outbound_ints(acb, intmask);
2176 static int arcmsr_abort(struct scsi_cmnd *cmd)
2178 struct AdapterControlBlock *acb =
2179 (struct AdapterControlBlock *)cmd->device->host->hostdata;
2183 "arcmsr%d: abort device command of scsi id = %d lun = %d \n",
2184 acb->host->host_no, cmd->device->id, cmd->device->lun);
2187 ************************************************
2188 ** the all interrupt service routine is locked
2189 ** we need to handle it as soon as possible and exit
2190 ************************************************
2192 if (!atomic_read(&acb->ccboutstandingcount))
2195 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2196 struct CommandControlBlock *ccb = acb->pccb_pool[i];
2197 if (ccb->startdone == ARCMSR_CCB_START && ccb->pcmd == cmd) {
2198 arcmsr_abort_one_cmd(acb, ccb);
2206 static const char *arcmsr_info(struct Scsi_Host *host)
2208 struct AdapterControlBlock *acb =
2209 (struct AdapterControlBlock *) host->hostdata;
2210 static char buf[256];
2214 switch (acb->pdev->device) {
2215 case PCI_DEVICE_ID_ARECA_1110:
2216 case PCI_DEVICE_ID_ARECA_1200:
2217 case PCI_DEVICE_ID_ARECA_1202:
2218 case PCI_DEVICE_ID_ARECA_1210:
2221 case PCI_DEVICE_ID_ARECA_1120:
2222 case PCI_DEVICE_ID_ARECA_1130:
2223 case PCI_DEVICE_ID_ARECA_1160:
2224 case PCI_DEVICE_ID_ARECA_1170:
2225 case PCI_DEVICE_ID_ARECA_1201:
2226 case PCI_DEVICE_ID_ARECA_1220:
2227 case PCI_DEVICE_ID_ARECA_1230:
2228 case PCI_DEVICE_ID_ARECA_1260:
2229 case PCI_DEVICE_ID_ARECA_1270:
2230 case PCI_DEVICE_ID_ARECA_1280:
2233 case PCI_DEVICE_ID_ARECA_1380:
2234 case PCI_DEVICE_ID_ARECA_1381:
2235 case PCI_DEVICE_ID_ARECA_1680:
2236 case PCI_DEVICE_ID_ARECA_1681:
2243 sprintf(buf, "Areca %s Host Adapter RAID Controller%s\n %s",
2244 type, raid6 ? "( RAID6 capable)" : "",
2245 ARCMSR_DRIVER_VERSION);
2248 #ifdef CONFIG_SCSI_ARCMSR_AER
2249 static pci_ers_result_t arcmsr_pci_slot_reset(struct pci_dev *pdev)
2251 struct Scsi_Host *host = pci_get_drvdata(pdev);
2252 struct AdapterControlBlock *acb =
2253 (struct AdapterControlBlock *) host->hostdata;
2254 uint32_t intmask_org;
2257 if (pci_enable_device(pdev)) {
2258 return PCI_ERS_RESULT_DISCONNECT;
2260 pci_set_master(pdev);
2261 intmask_org = arcmsr_disable_outbound_ints(acb);
2262 acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
2263 ACB_F_MESSAGE_RQBUFFER_CLEARED |
2264 ACB_F_MESSAGE_WQBUFFER_READED);
2265 acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
2266 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
2267 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
2268 acb->devstate[i][j] = ARECA_RAID_GONE;
2270 arcmsr_wait_firmware_ready(acb);
2271 arcmsr_iop_confirm(acb);
2272 /* disable all outbound interrupt */
2273 arcmsr_get_firmware_spec(acb);
2274 /*start background rebuild*/
2275 arcmsr_start_adapter_bgrb(acb);
2276 /* empty doorbell Qbuffer if door bell ringed */
2277 arcmsr_clear_doorbell_queue_buffer(acb);
2278 /* enable outbound Post Queue,outbound doorbell Interrupt */
2279 arcmsr_enable_outbound_ints(acb, intmask_org);
2280 acb->acb_flags |= ACB_F_IOP_INITED;
2282 pci_enable_pcie_error_reporting(pdev);
2283 return PCI_ERS_RESULT_RECOVERED;
2286 static void arcmsr_pci_ers_need_reset_forepart(struct pci_dev *pdev)
2288 struct Scsi_Host *host = pci_get_drvdata(pdev);
2289 struct AdapterControlBlock *acb = (struct AdapterControlBlock *)host->hostdata;
2290 struct CommandControlBlock *ccb;
2291 uint32_t intmask_org;
2294 if (atomic_read(&acb->ccboutstandingcount) != 0) {
2295 /* talk to iop 331 outstanding command aborted */
2296 arcmsr_abort_allcmd(acb);
2297 /* wait for 3 sec for all command aborted*/
2299 /* disable all outbound interrupt */
2300 intmask_org = arcmsr_disable_outbound_ints(acb);
2301 /* clear all outbound posted Q */
2302 arcmsr_done4abort_postqueue(acb);
2303 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2304 ccb = acb->pccb_pool[i];
2305 if (ccb->startdone == ARCMSR_CCB_START) {
2306 ccb->startdone = ARCMSR_CCB_ABORTED;
2307 arcmsr_ccb_complete(ccb, 1);
2310 /* enable all outbound interrupt */
2311 arcmsr_enable_outbound_ints(acb, intmask_org);
2313 pci_disable_device(pdev);
2316 static void arcmsr_pci_ers_disconnect_forepart(struct pci_dev *pdev)
2318 struct Scsi_Host *host = pci_get_drvdata(pdev);
2319 struct AdapterControlBlock *acb = \
2320 (struct AdapterControlBlock *)host->hostdata;
2322 arcmsr_stop_adapter_bgrb(acb);
2323 arcmsr_flush_adapter_cache(acb);
2326 static pci_ers_result_t arcmsr_pci_error_detected(struct pci_dev *pdev,
2327 pci_channel_state_t state)
2330 case pci_channel_io_frozen:
2331 arcmsr_pci_ers_need_reset_forepart(pdev);
2332 return PCI_ERS_RESULT_NEED_RESET;
2333 case pci_channel_io_perm_failure:
2334 arcmsr_pci_ers_disconnect_forepart(pdev);
2335 return PCI_ERS_RESULT_DISCONNECT;
2338 return PCI_ERS_RESULT_NEED_RESET;