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 .use_sg_chaining = ENABLE_SG_CHAINING,
126 .shost_attrs = arcmsr_host_attrs,
128 #ifdef CONFIG_SCSI_ARCMSR_AER
129 static pci_ers_result_t arcmsr_pci_slot_reset(struct pci_dev *pdev);
130 static pci_ers_result_t arcmsr_pci_error_detected(struct pci_dev *pdev,
131 pci_channel_state_t state);
133 static struct pci_error_handlers arcmsr_pci_error_handlers = {
134 .error_detected = arcmsr_pci_error_detected,
135 .slot_reset = arcmsr_pci_slot_reset,
138 static struct pci_device_id arcmsr_device_id_table[] = {
139 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1110)},
140 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1120)},
141 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1130)},
142 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1160)},
143 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1170)},
144 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1200)},
145 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1201)},
146 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1202)},
147 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1210)},
148 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1220)},
149 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1230)},
150 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1260)},
151 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1270)},
152 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1280)},
153 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1380)},
154 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1381)},
155 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1680)},
156 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1681)},
157 {0, 0}, /* Terminating entry */
159 MODULE_DEVICE_TABLE(pci, arcmsr_device_id_table);
160 static struct pci_driver arcmsr_pci_driver = {
162 .id_table = arcmsr_device_id_table,
163 .probe = arcmsr_probe,
164 .remove = arcmsr_remove,
165 .shutdown = arcmsr_shutdown,
166 #ifdef CONFIG_SCSI_ARCMSR_AER
167 .err_handler = &arcmsr_pci_error_handlers,
171 static irqreturn_t arcmsr_do_interrupt(int irq, void *dev_id)
173 irqreturn_t handle_state;
174 struct AdapterControlBlock *acb = dev_id;
176 spin_lock(acb->host->host_lock);
177 handle_state = arcmsr_interrupt(acb);
178 spin_unlock(acb->host->host_lock);
183 static int arcmsr_bios_param(struct scsi_device *sdev,
184 struct block_device *bdev, sector_t capacity, int *geom)
186 int ret, heads, sectors, cylinders, total_capacity;
187 unsigned char *buffer;/* return copy of block device's partition table */
189 buffer = scsi_bios_ptable(bdev);
191 ret = scsi_partsize(buffer, capacity, &geom[2], &geom[0], &geom[1]);
196 total_capacity = capacity;
199 cylinders = total_capacity / (heads * sectors);
200 if (cylinders > 1024) {
203 cylinders = total_capacity / (heads * sectors);
211 static void arcmsr_define_adapter_type(struct AdapterControlBlock *acb)
213 struct pci_dev *pdev = acb->pdev;
215 pci_read_config_word(pdev, PCI_DEVICE_ID, &dev_id);
218 acb->adapter_type = ACB_ADAPTER_TYPE_B;
222 default : acb->adapter_type = ACB_ADAPTER_TYPE_A;
226 static int arcmsr_alloc_ccb_pool(struct AdapterControlBlock *acb)
229 switch (acb->adapter_type) {
231 case ACB_ADAPTER_TYPE_A: {
232 struct pci_dev *pdev = acb->pdev;
234 dma_addr_t dma_coherent_handle, dma_addr;
235 struct CommandControlBlock *ccb_tmp;
236 uint32_t intmask_org;
239 acb->pmuA = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
241 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);
252 acb->dma_coherent = dma_coherent;
253 acb->dma_coherent_handle = dma_coherent_handle;
255 if (((unsigned long)dma_coherent & 0x1F)) {
256 dma_coherent = dma_coherent +
257 (0x20 - ((unsigned long)dma_coherent & 0x1F));
258 dma_coherent_handle = dma_coherent_handle +
259 (0x20 - ((unsigned long)dma_coherent_handle & 0x1F));
262 dma_addr = dma_coherent_handle;
263 ccb_tmp = (struct CommandControlBlock *)dma_coherent;
264 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
265 ccb_tmp->cdb_shifted_phyaddr = dma_addr >> 5;
267 acb->pccb_pool[i] = ccb_tmp;
268 list_add_tail(&ccb_tmp->list, &acb->ccb_free_list);
269 dma_addr = dma_addr + sizeof(struct CommandControlBlock);
273 acb->vir2phy_offset = (unsigned long)ccb_tmp -(unsigned long)dma_addr;
274 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
275 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
276 acb->devstate[i][j] = ARECA_RAID_GONE;
279 ** here we need to tell iop 331 our ccb_tmp.HighPart
280 ** if ccb_tmp.HighPart is not zero
282 intmask_org = arcmsr_disable_outbound_ints(acb);
286 case ACB_ADAPTER_TYPE_B: {
288 struct pci_dev *pdev = acb->pdev;
289 struct MessageUnit_B *reg;
290 void __iomem *mem_base0, *mem_base1;
292 dma_addr_t dma_coherent_handle, dma_addr;
293 uint32_t intmask_org;
294 struct CommandControlBlock *ccb_tmp;
297 dma_coherent = dma_alloc_coherent(&pdev->dev,
298 ((ARCMSR_MAX_FREECCB_NUM *
299 sizeof(struct CommandControlBlock) + 0x20) +
300 sizeof(struct MessageUnit_B)),
301 &dma_coherent_handle, GFP_KERNEL);
305 acb->dma_coherent = dma_coherent;
306 acb->dma_coherent_handle = dma_coherent_handle;
308 if (((unsigned long)dma_coherent & 0x1F)) {
309 dma_coherent = dma_coherent +
310 (0x20 - ((unsigned long)dma_coherent & 0x1F));
311 dma_coherent_handle = dma_coherent_handle +
312 (0x20 - ((unsigned long)dma_coherent_handle & 0x1F));
315 reg = (struct MessageUnit_B *)(dma_coherent +
316 ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock));
318 dma_addr = dma_coherent_handle;
319 ccb_tmp = (struct CommandControlBlock *)dma_coherent;
320 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
321 ccb_tmp->cdb_shifted_phyaddr = dma_addr >> 5;
323 acb->pccb_pool[i] = ccb_tmp;
324 list_add_tail(&ccb_tmp->list, &acb->ccb_free_list);
325 dma_addr = dma_addr + sizeof(struct CommandControlBlock);
329 reg = (struct MessageUnit_B *)(dma_coherent +
330 ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock));
332 mem_base0 = ioremap(pci_resource_start(pdev, 0),
333 pci_resource_len(pdev, 0));
334 mem_base1 = ioremap(pci_resource_start(pdev, 2),
335 pci_resource_len(pdev, 2));
336 reg->drv2iop_doorbell_reg = mem_base0 + ARCMSR_DRV2IOP_DOORBELL;
337 reg->drv2iop_doorbell_mask_reg = mem_base0 +
338 ARCMSR_DRV2IOP_DOORBELL_MASK;
339 reg->iop2drv_doorbell_reg = mem_base0 + ARCMSR_IOP2DRV_DOORBELL;
340 reg->iop2drv_doorbell_mask_reg = mem_base0 +
341 ARCMSR_IOP2DRV_DOORBELL_MASK;
342 reg->ioctl_wbuffer_reg = mem_base1 + ARCMSR_IOCTL_WBUFFER;
343 reg->ioctl_rbuffer_reg = mem_base1 + ARCMSR_IOCTL_RBUFFER;
344 reg->msgcode_rwbuffer_reg = mem_base1 + ARCMSR_MSGCODE_RWBUFFER;
346 acb->vir2phy_offset = (unsigned long)ccb_tmp -(unsigned long)dma_addr;
347 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
348 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
349 acb->devstate[i][j] = ARECA_RAID_GOOD;
352 ** here we need to tell iop 331 our ccb_tmp.HighPart
353 ** if ccb_tmp.HighPart is not zero
355 intmask_org = arcmsr_disable_outbound_ints(acb);
362 static int arcmsr_probe(struct pci_dev *pdev,
363 const struct pci_device_id *id)
365 struct Scsi_Host *host;
366 struct AdapterControlBlock *acb;
367 uint8_t bus, dev_fun;
370 error = pci_enable_device(pdev);
373 pci_set_master(pdev);
375 host = scsi_host_alloc(&arcmsr_scsi_host_template,
376 sizeof(struct AdapterControlBlock));
379 goto out_disable_device;
381 acb = (struct AdapterControlBlock *)host->hostdata;
382 memset(acb, 0, sizeof (struct AdapterControlBlock));
384 error = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
386 error = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
389 "scsi%d: No suitable DMA mask available\n",
394 bus = pdev->bus->number;
395 dev_fun = pdev->devfn;
398 host->max_sectors = ARCMSR_MAX_XFER_SECTORS;
399 host->max_lun = ARCMSR_MAX_TARGETLUN;
400 host->max_id = ARCMSR_MAX_TARGETID;/*16:8*/
401 host->max_cmd_len = 16; /*this is issue of 64bit LBA, over 2T byte*/
402 host->sg_tablesize = ARCMSR_MAX_SG_ENTRIES;
403 host->can_queue = ARCMSR_MAX_FREECCB_NUM; /* max simultaneous cmds */
404 host->cmd_per_lun = ARCMSR_MAX_CMD_PERLUN;
405 host->this_id = ARCMSR_SCSI_INITIATOR_ID;
406 host->unique_id = (bus << 8) | dev_fun;
407 host->irq = pdev->irq;
408 error = pci_request_regions(pdev, "arcmsr");
412 arcmsr_define_adapter_type(acb);
414 acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
415 ACB_F_MESSAGE_RQBUFFER_CLEARED |
416 ACB_F_MESSAGE_WQBUFFER_READED);
417 acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
418 INIT_LIST_HEAD(&acb->ccb_free_list);
420 error = arcmsr_alloc_ccb_pool(acb);
422 goto out_release_regions;
424 error = request_irq(pdev->irq, arcmsr_do_interrupt,
425 IRQF_SHARED, "arcmsr", acb);
427 goto out_free_ccb_pool;
429 arcmsr_iop_init(acb);
430 pci_set_drvdata(pdev, host);
431 if (strncmp(acb->firm_version, "V1.42", 5) >= 0)
432 host->max_sectors= ARCMSR_MAX_XFER_SECTORS_B;
434 error = scsi_add_host(host, &pdev->dev);
438 error = arcmsr_alloc_sysfs_attr(acb);
442 scsi_scan_host(host);
443 #ifdef CONFIG_SCSI_ARCMSR_AER
444 pci_enable_pcie_error_reporting(pdev);
449 free_irq(pdev->irq, acb);
451 arcmsr_free_ccb_pool(acb);
454 pci_release_regions(pdev);
458 pci_disable_device(pdev);
463 static uint8_t arcmsr_hba_wait_msgint_ready(struct AdapterControlBlock *acb)
465 struct MessageUnit_A __iomem *reg = acb->pmuA;
467 uint8_t Retries = 0x00;
470 for (Index = 0; Index < 100; Index++) {
471 if (readl(®->outbound_intstatus) &
472 ARCMSR_MU_OUTBOUND_MESSAGE0_INT) {
473 writel(ARCMSR_MU_OUTBOUND_MESSAGE0_INT,
474 ®->outbound_intstatus);
480 } while (Retries++ < 20);/*max 20 sec*/
484 static uint8_t arcmsr_hbb_wait_msgint_ready(struct AdapterControlBlock *acb)
486 struct MessageUnit_B *reg = acb->pmuB;
488 uint8_t Retries = 0x00;
491 for (Index = 0; Index < 100; Index++) {
492 if (readl(reg->iop2drv_doorbell_reg)
493 & ARCMSR_IOP2DRV_MESSAGE_CMD_DONE) {
494 writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN
495 , reg->iop2drv_doorbell_reg);
501 } while (Retries++ < 20);/*max 20 sec*/
505 static void arcmsr_abort_hba_allcmd(struct AdapterControlBlock *acb)
507 struct MessageUnit_A __iomem *reg = acb->pmuA;
509 writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, ®->inbound_msgaddr0);
510 if (arcmsr_hba_wait_msgint_ready(acb))
512 "arcmsr%d: wait 'abort all outstanding command' timeout \n"
513 , acb->host->host_no);
516 static void arcmsr_abort_hbb_allcmd(struct AdapterControlBlock *acb)
518 struct MessageUnit_B *reg = acb->pmuB;
520 writel(ARCMSR_MESSAGE_ABORT_CMD, reg->drv2iop_doorbell_reg);
521 if (arcmsr_hbb_wait_msgint_ready(acb))
523 "arcmsr%d: wait 'abort all outstanding command' timeout \n"
524 , acb->host->host_no);
527 static void arcmsr_abort_allcmd(struct AdapterControlBlock *acb)
529 switch (acb->adapter_type) {
530 case ACB_ADAPTER_TYPE_A: {
531 arcmsr_abort_hba_allcmd(acb);
535 case ACB_ADAPTER_TYPE_B: {
536 arcmsr_abort_hbb_allcmd(acb);
541 static void arcmsr_pci_unmap_dma(struct CommandControlBlock *ccb)
543 struct scsi_cmnd *pcmd = ccb->pcmd;
545 scsi_dma_unmap(pcmd);
548 static void arcmsr_ccb_complete(struct CommandControlBlock *ccb, int stand_flag)
550 struct AdapterControlBlock *acb = ccb->acb;
551 struct scsi_cmnd *pcmd = ccb->pcmd;
553 arcmsr_pci_unmap_dma(ccb);
555 atomic_dec(&acb->ccboutstandingcount);
556 ccb->startdone = ARCMSR_CCB_DONE;
558 list_add_tail(&ccb->list, &acb->ccb_free_list);
559 pcmd->scsi_done(pcmd);
562 static void arcmsr_flush_hba_cache(struct AdapterControlBlock *acb)
564 struct MessageUnit_A __iomem *reg = acb->pmuA;
565 int retry_count = 30;
567 writel(ARCMSR_INBOUND_MESG0_FLUSH_CACHE, ®->inbound_msgaddr0);
569 if (!arcmsr_hba_wait_msgint_ready(acb))
573 printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
574 timeout, retry count down = %d \n", acb->host->host_no, retry_count);
576 } while (retry_count != 0);
579 static void arcmsr_flush_hbb_cache(struct AdapterControlBlock *acb)
581 struct MessageUnit_B *reg = acb->pmuB;
582 int retry_count = 30;
584 writel(ARCMSR_MESSAGE_FLUSH_CACHE, reg->drv2iop_doorbell_reg);
586 if (!arcmsr_hbb_wait_msgint_ready(acb))
590 printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
591 timeout,retry count down = %d \n", acb->host->host_no, retry_count);
593 } while (retry_count != 0);
596 static void arcmsr_flush_adapter_cache(struct AdapterControlBlock *acb)
598 switch (acb->adapter_type) {
600 case ACB_ADAPTER_TYPE_A: {
601 arcmsr_flush_hba_cache(acb);
605 case ACB_ADAPTER_TYPE_B: {
606 arcmsr_flush_hbb_cache(acb);
611 static void arcmsr_report_sense_info(struct CommandControlBlock *ccb)
614 struct scsi_cmnd *pcmd = ccb->pcmd;
615 struct SENSE_DATA *sensebuffer = (struct SENSE_DATA *)pcmd->sense_buffer;
617 pcmd->result = DID_OK << 16;
619 int sense_data_length =
620 sizeof(struct SENSE_DATA) < sizeof(pcmd->sense_buffer)
621 ? sizeof(struct SENSE_DATA) : sizeof(pcmd->sense_buffer);
622 memset(sensebuffer, 0, sizeof(pcmd->sense_buffer));
623 memcpy(sensebuffer, ccb->arcmsr_cdb.SenseData, sense_data_length);
624 sensebuffer->ErrorCode = SCSI_SENSE_CURRENT_ERRORS;
625 sensebuffer->Valid = 1;
629 static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb)
632 switch (acb->adapter_type) {
634 case ACB_ADAPTER_TYPE_A : {
635 struct MessageUnit_A __iomem *reg = acb->pmuA;
636 orig_mask = readl(®->outbound_intmask)|\
637 ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE;
638 writel(orig_mask|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE, \
639 ®->outbound_intmask);
643 case ACB_ADAPTER_TYPE_B : {
644 struct MessageUnit_B *reg = acb->pmuB;
645 orig_mask = readl(reg->iop2drv_doorbell_mask_reg) & \
646 (~ARCMSR_IOP2DRV_MESSAGE_CMD_DONE);
647 writel(0, reg->iop2drv_doorbell_mask_reg);
654 static void arcmsr_report_ccb_state(struct AdapterControlBlock *acb, \
655 struct CommandControlBlock *ccb, uint32_t flag_ccb)
659 id = ccb->pcmd->device->id;
660 lun = ccb->pcmd->device->lun;
661 if (!(flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR)) {
662 if (acb->devstate[id][lun] == ARECA_RAID_GONE)
663 acb->devstate[id][lun] = ARECA_RAID_GOOD;
664 ccb->pcmd->result = DID_OK << 16;
665 arcmsr_ccb_complete(ccb, 1);
667 switch (ccb->arcmsr_cdb.DeviceStatus) {
668 case ARCMSR_DEV_SELECT_TIMEOUT: {
669 acb->devstate[id][lun] = ARECA_RAID_GONE;
670 ccb->pcmd->result = DID_NO_CONNECT << 16;
671 arcmsr_ccb_complete(ccb, 1);
675 case ARCMSR_DEV_ABORTED:
677 case ARCMSR_DEV_INIT_FAIL: {
678 acb->devstate[id][lun] = ARECA_RAID_GONE;
679 ccb->pcmd->result = DID_BAD_TARGET << 16;
680 arcmsr_ccb_complete(ccb, 1);
684 case ARCMSR_DEV_CHECK_CONDITION: {
685 acb->devstate[id][lun] = ARECA_RAID_GOOD;
686 arcmsr_report_sense_info(ccb);
687 arcmsr_ccb_complete(ccb, 1);
693 "arcmsr%d: scsi id = %d lun = %d"
694 " isr get command error done, "
695 "but got unknown DeviceStatus = 0x%x \n"
699 , ccb->arcmsr_cdb.DeviceStatus);
700 acb->devstate[id][lun] = ARECA_RAID_GONE;
701 ccb->pcmd->result = DID_NO_CONNECT << 16;
702 arcmsr_ccb_complete(ccb, 1);
708 static void arcmsr_drain_donequeue(struct AdapterControlBlock *acb, uint32_t flag_ccb)
711 struct CommandControlBlock *ccb;
713 ccb = (struct CommandControlBlock *)(acb->vir2phy_offset + (flag_ccb << 5));
714 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
715 if (ccb->startdone == ARCMSR_CCB_ABORTED) {
716 struct scsi_cmnd *abortcmd = ccb->pcmd;
718 abortcmd->result |= DID_ABORT << 16;
719 arcmsr_ccb_complete(ccb, 1);
720 printk(KERN_NOTICE "arcmsr%d: ccb ='0x%p' \
721 isr got aborted command \n", acb->host->host_no, ccb);
724 printk(KERN_NOTICE "arcmsr%d: isr get an illegal ccb command \
726 "ccb = '0x%p' ccbacb = '0x%p' startdone = 0x%x"
727 " ccboutstandingcount = %d \n"
733 , atomic_read(&acb->ccboutstandingcount));
735 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
738 static void arcmsr_done4abort_postqueue(struct AdapterControlBlock *acb)
743 switch (acb->adapter_type) {
745 case ACB_ADAPTER_TYPE_A: {
746 struct MessageUnit_A __iomem *reg = acb->pmuA;
747 uint32_t outbound_intstatus;
748 outbound_intstatus = readl(®->outbound_intstatus) &
749 acb->outbound_int_enable;
750 /*clear and abort all outbound posted Q*/
751 writel(outbound_intstatus, ®->outbound_intstatus);/*clear interrupt*/
752 while (((flag_ccb = readl(®->outbound_queueport)) != 0xFFFFFFFF)
753 && (i++ < ARCMSR_MAX_OUTSTANDING_CMD)) {
754 arcmsr_drain_donequeue(acb, flag_ccb);
759 case ACB_ADAPTER_TYPE_B: {
760 struct MessageUnit_B *reg = acb->pmuB;
761 /*clear all outbound posted Q*/
762 for (i = 0; i < ARCMSR_MAX_HBB_POSTQUEUE; i++) {
763 if ((flag_ccb = readl(®->done_qbuffer[i])) != 0) {
764 writel(0, ®->done_qbuffer[i]);
765 arcmsr_drain_donequeue(acb, flag_ccb);
767 writel(0, ®->post_qbuffer[i]);
769 reg->doneq_index = 0;
770 reg->postq_index = 0;
775 static void arcmsr_remove(struct pci_dev *pdev)
777 struct Scsi_Host *host = pci_get_drvdata(pdev);
778 struct AdapterControlBlock *acb =
779 (struct AdapterControlBlock *) host->hostdata;
782 arcmsr_free_sysfs_attr(acb);
783 scsi_remove_host(host);
784 arcmsr_stop_adapter_bgrb(acb);
785 arcmsr_flush_adapter_cache(acb);
786 arcmsr_disable_outbound_ints(acb);
787 acb->acb_flags |= ACB_F_SCSISTOPADAPTER;
788 acb->acb_flags &= ~ACB_F_IOP_INITED;
790 for (poll_count = 0; poll_count < ARCMSR_MAX_OUTSTANDING_CMD; poll_count++) {
791 if (!atomic_read(&acb->ccboutstandingcount))
793 arcmsr_interrupt(acb);/* FIXME: need spinlock */
797 if (atomic_read(&acb->ccboutstandingcount)) {
800 arcmsr_abort_allcmd(acb);
801 arcmsr_done4abort_postqueue(acb);
802 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
803 struct CommandControlBlock *ccb = acb->pccb_pool[i];
804 if (ccb->startdone == ARCMSR_CCB_START) {
805 ccb->startdone = ARCMSR_CCB_ABORTED;
806 ccb->pcmd->result = DID_ABORT << 16;
807 arcmsr_ccb_complete(ccb, 1);
812 free_irq(pdev->irq, acb);
814 arcmsr_free_ccb_pool(acb);
815 pci_release_regions(pdev);
819 pci_disable_device(pdev);
820 pci_set_drvdata(pdev, NULL);
823 static void arcmsr_shutdown(struct pci_dev *pdev)
825 struct Scsi_Host *host = pci_get_drvdata(pdev);
826 struct AdapterControlBlock *acb =
827 (struct AdapterControlBlock *)host->hostdata;
829 arcmsr_stop_adapter_bgrb(acb);
830 arcmsr_flush_adapter_cache(acb);
833 static int arcmsr_module_init(void)
837 error = pci_register_driver(&arcmsr_pci_driver);
841 static void arcmsr_module_exit(void)
843 pci_unregister_driver(&arcmsr_pci_driver);
845 module_init(arcmsr_module_init);
846 module_exit(arcmsr_module_exit);
848 static void arcmsr_enable_outbound_ints(struct AdapterControlBlock *acb, \
853 switch (acb->adapter_type) {
855 case ACB_ADAPTER_TYPE_A : {
856 struct MessageUnit_A __iomem *reg = acb->pmuA;
857 mask = intmask_org & ~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE |
858 ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE);
859 writel(mask, ®->outbound_intmask);
860 acb->outbound_int_enable = ~(intmask_org & mask) & 0x000000ff;
864 case ACB_ADAPTER_TYPE_B : {
865 struct MessageUnit_B *reg = acb->pmuB;
866 mask = intmask_org | (ARCMSR_IOP2DRV_DATA_WRITE_OK | \
867 ARCMSR_IOP2DRV_DATA_READ_OK | ARCMSR_IOP2DRV_CDB_DONE);
868 writel(mask, reg->iop2drv_doorbell_mask_reg);
869 acb->outbound_int_enable = (intmask_org | mask) & 0x0000000f;
874 static void arcmsr_build_ccb(struct AdapterControlBlock *acb,
875 struct CommandControlBlock *ccb, struct scsi_cmnd *pcmd)
877 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
878 int8_t *psge = (int8_t *)&arcmsr_cdb->u;
879 __le32 address_lo, address_hi;
880 int arccdbsize = 0x30;
884 memset(arcmsr_cdb, 0, sizeof(struct ARCMSR_CDB));
886 arcmsr_cdb->TargetID = pcmd->device->id;
887 arcmsr_cdb->LUN = pcmd->device->lun;
888 arcmsr_cdb->Function = 1;
889 arcmsr_cdb->CdbLength = (uint8_t)pcmd->cmd_len;
890 arcmsr_cdb->Context = (unsigned long)arcmsr_cdb;
891 memcpy(arcmsr_cdb->Cdb, pcmd->cmnd, pcmd->cmd_len);
893 nseg = scsi_dma_map(pcmd);
898 int i, cdb_sgcount = 0;
899 struct scatterlist *sg;
901 /* map stor port SG list to our iop SG List. */
902 scsi_for_each_sg(pcmd, sg, nseg, i) {
903 /* Get the physical address of the current data pointer */
904 length = cpu_to_le32(sg_dma_len(sg));
905 address_lo = cpu_to_le32(dma_addr_lo32(sg_dma_address(sg)));
906 address_hi = cpu_to_le32(dma_addr_hi32(sg_dma_address(sg)));
907 if (address_hi == 0) {
908 struct SG32ENTRY *pdma_sg = (struct SG32ENTRY *)psge;
910 pdma_sg->address = address_lo;
911 pdma_sg->length = length;
912 psge += sizeof (struct SG32ENTRY);
913 arccdbsize += sizeof (struct SG32ENTRY);
915 struct SG64ENTRY *pdma_sg = (struct SG64ENTRY *)psge;
917 pdma_sg->addresshigh = address_hi;
918 pdma_sg->address = address_lo;
919 pdma_sg->length = length|IS_SG64_ADDR;
920 psge += sizeof (struct SG64ENTRY);
921 arccdbsize += sizeof (struct SG64ENTRY);
925 arcmsr_cdb->sgcount = (uint8_t)cdb_sgcount;
926 arcmsr_cdb->DataLength = scsi_bufflen(pcmd);
927 if ( arccdbsize > 256)
928 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_SGL_BSIZE;
930 if (pcmd->sc_data_direction == DMA_TO_DEVICE ) {
931 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_WRITE;
932 ccb->ccb_flags |= CCB_FLAG_WRITE;
936 static void arcmsr_post_ccb(struct AdapterControlBlock *acb, struct CommandControlBlock *ccb)
938 uint32_t cdb_shifted_phyaddr = ccb->cdb_shifted_phyaddr;
939 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
940 atomic_inc(&acb->ccboutstandingcount);
941 ccb->startdone = ARCMSR_CCB_START;
943 switch (acb->adapter_type) {
944 case ACB_ADAPTER_TYPE_A: {
945 struct MessageUnit_A __iomem *reg = acb->pmuA;
947 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE)
948 writel(cdb_shifted_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,
949 ®->inbound_queueport);
951 writel(cdb_shifted_phyaddr, ®->inbound_queueport);
956 case ACB_ADAPTER_TYPE_B: {
957 struct MessageUnit_B *reg = acb->pmuB;
958 uint32_t ending_index, index = reg->postq_index;
960 ending_index = ((index + 1) % ARCMSR_MAX_HBB_POSTQUEUE);
961 writel(0, ®->post_qbuffer[ending_index]);
962 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE) {
963 writel(cdb_shifted_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,\
964 ®->post_qbuffer[index]);
967 writel(cdb_shifted_phyaddr, ®->post_qbuffer[index]);
970 index %= ARCMSR_MAX_HBB_POSTQUEUE;/*if last index number set it to 0 */
971 reg->postq_index = index;
972 writel(ARCMSR_DRV2IOP_CDB_POSTED, reg->drv2iop_doorbell_reg);
978 static void arcmsr_stop_hba_bgrb(struct AdapterControlBlock *acb)
980 struct MessageUnit_A __iomem *reg = acb->pmuA;
981 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
982 writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, ®->inbound_msgaddr0);
984 if (arcmsr_hba_wait_msgint_ready(acb)) {
986 "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
987 , acb->host->host_no);
991 static void arcmsr_stop_hbb_bgrb(struct AdapterControlBlock *acb)
993 struct MessageUnit_B *reg = acb->pmuB;
994 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
995 writel(ARCMSR_MESSAGE_STOP_BGRB, reg->drv2iop_doorbell_reg);
997 if (arcmsr_hbb_wait_msgint_ready(acb)) {
999 "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
1000 , acb->host->host_no);
1004 static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb)
1006 switch (acb->adapter_type) {
1007 case ACB_ADAPTER_TYPE_A: {
1008 arcmsr_stop_hba_bgrb(acb);
1012 case ACB_ADAPTER_TYPE_B: {
1013 arcmsr_stop_hbb_bgrb(acb);
1019 static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb)
1021 dma_free_coherent(&acb->pdev->dev,
1022 ARCMSR_MAX_FREECCB_NUM * sizeof (struct CommandControlBlock) + 0x20,
1024 acb->dma_coherent_handle);
1027 void arcmsr_iop_message_read(struct AdapterControlBlock *acb)
1029 switch (acb->adapter_type) {
1030 case ACB_ADAPTER_TYPE_A: {
1031 struct MessageUnit_A __iomem *reg = acb->pmuA;
1032 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, ®->inbound_doorbell);
1036 case ACB_ADAPTER_TYPE_B: {
1037 struct MessageUnit_B *reg = acb->pmuB;
1038 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell_reg);
1044 static void arcmsr_iop_message_wrote(struct AdapterControlBlock *acb)
1046 switch (acb->adapter_type) {
1047 case ACB_ADAPTER_TYPE_A: {
1048 struct MessageUnit_A __iomem *reg = acb->pmuA;
1050 ** push inbound doorbell tell iop, driver data write ok
1051 ** and wait reply on next hwinterrupt for next Qbuffer post
1053 writel(ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK, ®->inbound_doorbell);
1057 case ACB_ADAPTER_TYPE_B: {
1058 struct MessageUnit_B *reg = acb->pmuB;
1060 ** push inbound doorbell tell iop, driver data write ok
1061 ** and wait reply on next hwinterrupt for next Qbuffer post
1063 writel(ARCMSR_DRV2IOP_DATA_WRITE_OK, reg->drv2iop_doorbell_reg);
1069 struct QBUFFER __iomem *arcmsr_get_iop_rqbuffer(struct AdapterControlBlock *acb)
1071 static struct QBUFFER __iomem *qbuffer;
1073 switch (acb->adapter_type) {
1075 case ACB_ADAPTER_TYPE_A: {
1076 struct MessageUnit_A __iomem *reg = acb->pmuA;
1077 qbuffer = (struct QBUFFER __iomem *)®->message_rbuffer;
1081 case ACB_ADAPTER_TYPE_B: {
1082 struct MessageUnit_B *reg = acb->pmuB;
1083 qbuffer = (struct QBUFFER __iomem *)reg->ioctl_rbuffer_reg;
1090 static struct QBUFFER __iomem *arcmsr_get_iop_wqbuffer(struct AdapterControlBlock *acb)
1092 static struct QBUFFER __iomem *pqbuffer;
1094 switch (acb->adapter_type) {
1096 case ACB_ADAPTER_TYPE_A: {
1097 struct MessageUnit_A __iomem *reg = acb->pmuA;
1098 pqbuffer = (struct QBUFFER __iomem *) ®->message_wbuffer;
1102 case ACB_ADAPTER_TYPE_B: {
1103 struct MessageUnit_B *reg = acb->pmuB;
1104 pqbuffer = (struct QBUFFER __iomem *)reg->ioctl_wbuffer_reg;
1111 static void arcmsr_iop2drv_data_wrote_handle(struct AdapterControlBlock *acb)
1113 struct QBUFFER __iomem *prbuffer;
1114 struct QBUFFER *pQbuffer;
1115 uint8_t __iomem *iop_data;
1116 int32_t my_empty_len, iop_len, rqbuf_firstindex, rqbuf_lastindex;
1118 rqbuf_lastindex = acb->rqbuf_lastindex;
1119 rqbuf_firstindex = acb->rqbuf_firstindex;
1120 prbuffer = arcmsr_get_iop_rqbuffer(acb);
1121 iop_data = (uint8_t __iomem *)prbuffer->data;
1122 iop_len = prbuffer->data_len;
1123 my_empty_len = (rqbuf_firstindex - rqbuf_lastindex -1)&(ARCMSR_MAX_QBUFFER -1);
1125 if (my_empty_len >= iop_len)
1127 while (iop_len > 0) {
1128 pQbuffer = (struct QBUFFER *)&acb->rqbuffer[rqbuf_lastindex];
1129 memcpy(pQbuffer, iop_data,1);
1131 rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1135 acb->rqbuf_lastindex = rqbuf_lastindex;
1136 arcmsr_iop_message_read(acb);
1140 acb->acb_flags |= ACB_F_IOPDATA_OVERFLOW;
1144 static void arcmsr_iop2drv_data_read_handle(struct AdapterControlBlock *acb)
1146 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_READED;
1147 if (acb->wqbuf_firstindex != acb->wqbuf_lastindex) {
1149 struct QBUFFER __iomem *pwbuffer;
1150 uint8_t __iomem *iop_data;
1151 int32_t allxfer_len = 0;
1153 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
1154 pwbuffer = arcmsr_get_iop_wqbuffer(acb);
1155 iop_data = (uint8_t __iomem *)pwbuffer->data;
1157 while ((acb->wqbuf_firstindex != acb->wqbuf_lastindex) && \
1158 (allxfer_len < 124)) {
1159 pQbuffer = &acb->wqbuffer[acb->wqbuf_firstindex];
1160 memcpy(iop_data, pQbuffer, 1);
1161 acb->wqbuf_firstindex++;
1162 acb->wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1166 pwbuffer->data_len = allxfer_len;
1168 arcmsr_iop_message_wrote(acb);
1171 if (acb->wqbuf_firstindex == acb->wqbuf_lastindex) {
1172 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_CLEARED;
1176 static void arcmsr_hba_doorbell_isr(struct AdapterControlBlock *acb)
1178 uint32_t outbound_doorbell;
1179 struct MessageUnit_A __iomem *reg = acb->pmuA;
1181 outbound_doorbell = readl(®->outbound_doorbell);
1182 writel(outbound_doorbell, ®->outbound_doorbell);
1183 if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK) {
1184 arcmsr_iop2drv_data_wrote_handle(acb);
1187 if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_READ_OK) {
1188 arcmsr_iop2drv_data_read_handle(acb);
1192 static void arcmsr_hba_postqueue_isr(struct AdapterControlBlock *acb)
1195 struct MessageUnit_A __iomem *reg = acb->pmuA;
1197 while ((flag_ccb = readl(®->outbound_queueport)) != 0xFFFFFFFF) {
1198 arcmsr_drain_donequeue(acb, flag_ccb);
1202 static void arcmsr_hbb_postqueue_isr(struct AdapterControlBlock *acb)
1206 struct MessageUnit_B *reg = acb->pmuB;
1208 index = reg->doneq_index;
1210 while ((flag_ccb = readl(®->done_qbuffer[index])) != 0) {
1211 writel(0, ®->done_qbuffer[index]);
1212 arcmsr_drain_donequeue(acb, flag_ccb);
1214 index %= ARCMSR_MAX_HBB_POSTQUEUE;
1215 reg->doneq_index = index;
1219 static int arcmsr_handle_hba_isr(struct AdapterControlBlock *acb)
1221 uint32_t outbound_intstatus;
1222 struct MessageUnit_A __iomem *reg = acb->pmuA;
1224 outbound_intstatus = readl(®->outbound_intstatus) & \
1225 acb->outbound_int_enable;
1226 if (!(outbound_intstatus & ARCMSR_MU_OUTBOUND_HANDLE_INT)) {
1229 writel(outbound_intstatus, ®->outbound_intstatus);
1230 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_DOORBELL_INT) {
1231 arcmsr_hba_doorbell_isr(acb);
1233 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_POSTQUEUE_INT) {
1234 arcmsr_hba_postqueue_isr(acb);
1239 static int arcmsr_handle_hbb_isr(struct AdapterControlBlock *acb)
1241 uint32_t outbound_doorbell;
1242 struct MessageUnit_B *reg = acb->pmuB;
1244 outbound_doorbell = readl(reg->iop2drv_doorbell_reg) & \
1245 acb->outbound_int_enable;
1246 if (!outbound_doorbell)
1249 writel(~outbound_doorbell, reg->iop2drv_doorbell_reg);
1251 if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_WRITE_OK) {
1252 arcmsr_iop2drv_data_wrote_handle(acb);
1254 if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_READ_OK) {
1255 arcmsr_iop2drv_data_read_handle(acb);
1257 if (outbound_doorbell & ARCMSR_IOP2DRV_CDB_DONE) {
1258 arcmsr_hbb_postqueue_isr(acb);
1264 static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb)
1266 switch (acb->adapter_type) {
1267 case ACB_ADAPTER_TYPE_A: {
1268 if (arcmsr_handle_hba_isr(acb)) {
1274 case ACB_ADAPTER_TYPE_B: {
1275 if (arcmsr_handle_hbb_isr(acb)) {
1284 static void arcmsr_iop_parking(struct AdapterControlBlock *acb)
1287 /* stop adapter background rebuild */
1288 if (acb->acb_flags & ACB_F_MSG_START_BGRB) {
1289 uint32_t intmask_org;
1290 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
1291 intmask_org = arcmsr_disable_outbound_ints(acb);
1292 arcmsr_stop_adapter_bgrb(acb);
1293 arcmsr_flush_adapter_cache(acb);
1294 arcmsr_enable_outbound_ints(acb, intmask_org);
1299 void arcmsr_post_ioctldata2iop(struct AdapterControlBlock *acb)
1301 int32_t wqbuf_firstindex, wqbuf_lastindex;
1303 struct QBUFFER __iomem *pwbuffer;
1304 uint8_t __iomem *iop_data;
1305 int32_t allxfer_len = 0;
1307 pwbuffer = arcmsr_get_iop_wqbuffer(acb);
1308 iop_data = (uint8_t __iomem *)pwbuffer->data;
1309 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_READED) {
1310 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
1311 wqbuf_firstindex = acb->wqbuf_firstindex;
1312 wqbuf_lastindex = acb->wqbuf_lastindex;
1313 while ((wqbuf_firstindex != wqbuf_lastindex) && (allxfer_len < 124)) {
1314 pQbuffer = &acb->wqbuffer[wqbuf_firstindex];
1315 memcpy(iop_data, pQbuffer, 1);
1317 wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1321 acb->wqbuf_firstindex = wqbuf_firstindex;
1322 pwbuffer->data_len = allxfer_len;
1323 arcmsr_iop_message_wrote(acb);
1327 static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb, \
1328 struct scsi_cmnd *cmd)
1330 struct CMD_MESSAGE_FIELD *pcmdmessagefld;
1331 int retvalue = 0, transfer_len = 0;
1333 struct scatterlist *sg;
1334 uint32_t controlcode = (uint32_t ) cmd->cmnd[5] << 24 |
1335 (uint32_t ) cmd->cmnd[6] << 16 |
1336 (uint32_t ) cmd->cmnd[7] << 8 |
1337 (uint32_t ) cmd->cmnd[8];
1338 /* 4 bytes: Areca io control code */
1340 sg = scsi_sglist(cmd);
1341 buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
1342 if (scsi_sg_count(cmd) > 1) {
1343 retvalue = ARCMSR_MESSAGE_FAIL;
1346 transfer_len += sg->length;
1348 if (transfer_len > sizeof(struct CMD_MESSAGE_FIELD)) {
1349 retvalue = ARCMSR_MESSAGE_FAIL;
1352 pcmdmessagefld = (struct CMD_MESSAGE_FIELD *) buffer;
1353 switch(controlcode) {
1355 case ARCMSR_MESSAGE_READ_RQBUFFER: {
1356 unsigned long *ver_addr;
1357 dma_addr_t buf_handle;
1358 uint8_t *pQbuffer, *ptmpQbuffer;
1359 int32_t allxfer_len = 0;
1361 ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
1363 retvalue = ARCMSR_MESSAGE_FAIL;
1366 ptmpQbuffer = (uint8_t *) ver_addr;
1367 while ((acb->rqbuf_firstindex != acb->rqbuf_lastindex)
1368 && (allxfer_len < 1031)) {
1369 pQbuffer = &acb->rqbuffer[acb->rqbuf_firstindex];
1370 memcpy(ptmpQbuffer, pQbuffer, 1);
1371 acb->rqbuf_firstindex++;
1372 acb->rqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1376 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1378 struct QBUFFER __iomem *prbuffer;
1379 uint8_t __iomem *iop_data;
1382 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1383 prbuffer = arcmsr_get_iop_rqbuffer(acb);
1384 iop_data = prbuffer->data;
1385 iop_len = readl(&prbuffer->data_len);
1386 while (iop_len > 0) {
1387 acb->rqbuffer[acb->rqbuf_lastindex] = readb(iop_data);
1388 acb->rqbuf_lastindex++;
1389 acb->rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1393 arcmsr_iop_message_read(acb);
1395 memcpy(pcmdmessagefld->messagedatabuffer, (uint8_t *)ver_addr, allxfer_len);
1396 pcmdmessagefld->cmdmessage.Length = allxfer_len;
1397 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1398 pci_free_consistent(acb->pdev, 1032, ver_addr, buf_handle);
1402 case ARCMSR_MESSAGE_WRITE_WQBUFFER: {
1403 unsigned long *ver_addr;
1404 dma_addr_t buf_handle;
1405 int32_t my_empty_len, user_len, wqbuf_firstindex, wqbuf_lastindex;
1406 uint8_t *pQbuffer, *ptmpuserbuffer;
1408 ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
1410 retvalue = ARCMSR_MESSAGE_FAIL;
1413 ptmpuserbuffer = (uint8_t *)ver_addr;
1414 user_len = pcmdmessagefld->cmdmessage.Length;
1415 memcpy(ptmpuserbuffer, pcmdmessagefld->messagedatabuffer, user_len);
1416 wqbuf_lastindex = acb->wqbuf_lastindex;
1417 wqbuf_firstindex = acb->wqbuf_firstindex;
1418 if (wqbuf_lastindex != wqbuf_firstindex) {
1419 struct SENSE_DATA *sensebuffer =
1420 (struct SENSE_DATA *)cmd->sense_buffer;
1421 arcmsr_post_ioctldata2iop(acb);
1422 /* has error report sensedata */
1423 sensebuffer->ErrorCode = 0x70;
1424 sensebuffer->SenseKey = ILLEGAL_REQUEST;
1425 sensebuffer->AdditionalSenseLength = 0x0A;
1426 sensebuffer->AdditionalSenseCode = 0x20;
1427 sensebuffer->Valid = 1;
1428 retvalue = ARCMSR_MESSAGE_FAIL;
1430 my_empty_len = (wqbuf_firstindex-wqbuf_lastindex - 1)
1431 &(ARCMSR_MAX_QBUFFER - 1);
1432 if (my_empty_len >= user_len) {
1433 while (user_len > 0) {
1435 &acb->wqbuffer[acb->wqbuf_lastindex];
1436 memcpy(pQbuffer, ptmpuserbuffer, 1);
1437 acb->wqbuf_lastindex++;
1438 acb->wqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1442 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {
1444 ~ACB_F_MESSAGE_WQBUFFER_CLEARED;
1445 arcmsr_post_ioctldata2iop(acb);
1448 /* has error report sensedata */
1449 struct SENSE_DATA *sensebuffer =
1450 (struct SENSE_DATA *)cmd->sense_buffer;
1451 sensebuffer->ErrorCode = 0x70;
1452 sensebuffer->SenseKey = ILLEGAL_REQUEST;
1453 sensebuffer->AdditionalSenseLength = 0x0A;
1454 sensebuffer->AdditionalSenseCode = 0x20;
1455 sensebuffer->Valid = 1;
1456 retvalue = ARCMSR_MESSAGE_FAIL;
1459 pci_free_consistent(acb->pdev, 1032, ver_addr, buf_handle);
1463 case ARCMSR_MESSAGE_CLEAR_RQBUFFER: {
1464 uint8_t *pQbuffer = acb->rqbuffer;
1466 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1467 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1468 arcmsr_iop_message_read(acb);
1470 acb->acb_flags |= ACB_F_MESSAGE_RQBUFFER_CLEARED;
1471 acb->rqbuf_firstindex = 0;
1472 acb->rqbuf_lastindex = 0;
1473 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1474 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1478 case ARCMSR_MESSAGE_CLEAR_WQBUFFER: {
1479 uint8_t *pQbuffer = acb->wqbuffer;
1481 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1482 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1483 arcmsr_iop_message_read(acb);
1486 (ACB_F_MESSAGE_WQBUFFER_CLEARED |
1487 ACB_F_MESSAGE_WQBUFFER_READED);
1488 acb->wqbuf_firstindex = 0;
1489 acb->wqbuf_lastindex = 0;
1490 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1491 pcmdmessagefld->cmdmessage.ReturnCode =
1492 ARCMSR_MESSAGE_RETURNCODE_OK;
1496 case ARCMSR_MESSAGE_CLEAR_ALLQBUFFER: {
1499 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1500 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1501 arcmsr_iop_message_read(acb);
1504 (ACB_F_MESSAGE_WQBUFFER_CLEARED
1505 | ACB_F_MESSAGE_RQBUFFER_CLEARED
1506 | ACB_F_MESSAGE_WQBUFFER_READED);
1507 acb->rqbuf_firstindex = 0;
1508 acb->rqbuf_lastindex = 0;
1509 acb->wqbuf_firstindex = 0;
1510 acb->wqbuf_lastindex = 0;
1511 pQbuffer = acb->rqbuffer;
1512 memset(pQbuffer, 0, sizeof(struct QBUFFER));
1513 pQbuffer = acb->wqbuffer;
1514 memset(pQbuffer, 0, sizeof(struct QBUFFER));
1515 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1519 case ARCMSR_MESSAGE_RETURN_CODE_3F: {
1520 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_3F;
1524 case ARCMSR_MESSAGE_SAY_HELLO: {
1525 int8_t *hello_string = "Hello! I am ARCMSR";
1527 memcpy(pcmdmessagefld->messagedatabuffer, hello_string
1528 , (int16_t)strlen(hello_string));
1529 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1533 case ARCMSR_MESSAGE_SAY_GOODBYE:
1534 arcmsr_iop_parking(acb);
1537 case ARCMSR_MESSAGE_FLUSH_ADAPTER_CACHE:
1538 arcmsr_flush_adapter_cache(acb);
1542 retvalue = ARCMSR_MESSAGE_FAIL;
1545 sg = scsi_sglist(cmd);
1546 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
1550 static struct CommandControlBlock *arcmsr_get_freeccb(struct AdapterControlBlock *acb)
1552 struct list_head *head = &acb->ccb_free_list;
1553 struct CommandControlBlock *ccb = NULL;
1555 if (!list_empty(head)) {
1556 ccb = list_entry(head->next, struct CommandControlBlock, list);
1557 list_del(head->next);
1562 static void arcmsr_handle_virtual_command(struct AdapterControlBlock *acb,
1563 struct scsi_cmnd *cmd)
1565 switch (cmd->cmnd[0]) {
1567 unsigned char inqdata[36];
1569 struct scatterlist *sg;
1571 if (cmd->device->lun) {
1572 cmd->result = (DID_TIME_OUT << 16);
1573 cmd->scsi_done(cmd);
1576 inqdata[0] = TYPE_PROCESSOR;
1577 /* Periph Qualifier & Periph Dev Type */
1579 /* rem media bit & Dev Type Modifier */
1581 /* ISO, ECMA, & ANSI versions */
1583 /* length of additional data */
1584 strncpy(&inqdata[8], "Areca ", 8);
1585 /* Vendor Identification */
1586 strncpy(&inqdata[16], "RAID controller ", 16);
1587 /* Product Identification */
1588 strncpy(&inqdata[32], "R001", 4); /* Product Revision */
1590 sg = scsi_sglist(cmd);
1591 buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
1593 memcpy(buffer, inqdata, sizeof(inqdata));
1594 sg = scsi_sglist(cmd);
1595 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
1597 cmd->scsi_done(cmd);
1602 if (arcmsr_iop_message_xfer(acb, cmd))
1603 cmd->result = (DID_ERROR << 16);
1604 cmd->scsi_done(cmd);
1608 cmd->scsi_done(cmd);
1612 static int arcmsr_queue_command(struct scsi_cmnd *cmd,
1613 void (* done)(struct scsi_cmnd *))
1615 struct Scsi_Host *host = cmd->device->host;
1616 struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
1617 struct CommandControlBlock *ccb;
1618 int target = cmd->device->id;
1619 int lun = cmd->device->lun;
1621 cmd->scsi_done = done;
1622 cmd->host_scribble = NULL;
1624 if (acb->acb_flags & ACB_F_BUS_RESET) {
1625 printk(KERN_NOTICE "arcmsr%d: bus reset"
1626 " and return busy \n"
1627 , acb->host->host_no);
1628 return SCSI_MLQUEUE_HOST_BUSY;
1631 /* virtual device for iop message transfer */
1632 arcmsr_handle_virtual_command(acb, cmd);
1635 if (acb->devstate[target][lun] == ARECA_RAID_GONE) {
1638 block_cmd = cmd->cmnd[0] & 0x0f;
1639 if (block_cmd == 0x08 || block_cmd == 0x0a) {
1641 "arcmsr%d: block 'read/write'"
1642 "command with gone raid volume"
1643 " Cmd = %2x, TargetId = %d, Lun = %d \n"
1644 , acb->host->host_no
1647 cmd->result = (DID_NO_CONNECT << 16);
1648 cmd->scsi_done(cmd);
1652 if (atomic_read(&acb->ccboutstandingcount) >=
1653 ARCMSR_MAX_OUTSTANDING_CMD)
1654 return SCSI_MLQUEUE_HOST_BUSY;
1656 ccb = arcmsr_get_freeccb(acb);
1658 return SCSI_MLQUEUE_HOST_BUSY;
1660 arcmsr_build_ccb(acb, ccb, cmd);
1661 arcmsr_post_ccb(acb, ccb);
1665 static void arcmsr_get_hba_config(struct AdapterControlBlock *acb)
1667 struct MessageUnit_A __iomem *reg = acb->pmuA;
1668 char *acb_firm_model = acb->firm_model;
1669 char *acb_firm_version = acb->firm_version;
1670 char __iomem *iop_firm_model = (char __iomem *)(®->message_rwbuffer[15]);
1671 char __iomem *iop_firm_version = (char __iomem *)(®->message_rwbuffer[17]);
1674 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, ®->inbound_msgaddr0);
1675 if (arcmsr_hba_wait_msgint_ready(acb)) {
1676 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
1677 miscellaneous data' timeout \n", acb->host->host_no);
1682 *acb_firm_model = readb(iop_firm_model);
1690 *acb_firm_version = readb(iop_firm_version);
1696 printk(KERN_INFO "ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n"
1697 , acb->host->host_no
1698 , acb->firm_version);
1700 acb->firm_request_len = readl(®->message_rwbuffer[1]);
1701 acb->firm_numbers_queue = readl(®->message_rwbuffer[2]);
1702 acb->firm_sdram_size = readl(®->message_rwbuffer[3]);
1703 acb->firm_hd_channels = readl(®->message_rwbuffer[4]);
1706 static void arcmsr_get_hbb_config(struct AdapterControlBlock *acb)
1708 struct MessageUnit_B *reg = acb->pmuB;
1709 uint32_t __iomem *lrwbuffer = reg->msgcode_rwbuffer_reg;
1710 char *acb_firm_model = acb->firm_model;
1711 char *acb_firm_version = acb->firm_version;
1712 char __iomem *iop_firm_model = (char __iomem *)(&lrwbuffer[15]);
1713 /*firm_model,15,60-67*/
1714 char __iomem *iop_firm_version = (char __iomem *)(&lrwbuffer[17]);
1715 /*firm_version,17,68-83*/
1718 writel(ARCMSR_MESSAGE_GET_CONFIG, reg->drv2iop_doorbell_reg);
1719 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1720 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
1721 miscellaneous data' timeout \n", acb->host->host_no);
1727 *acb_firm_model = readb(iop_firm_model);
1736 *acb_firm_version = readb(iop_firm_version);
1742 printk(KERN_INFO "ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n",
1747 acb->firm_request_len = readl(lrwbuffer++);
1748 /*firm_request_len,1,04-07*/
1749 acb->firm_numbers_queue = readl(lrwbuffer++);
1750 /*firm_numbers_queue,2,08-11*/
1751 acb->firm_sdram_size = readl(lrwbuffer++);
1752 /*firm_sdram_size,3,12-15*/
1753 acb->firm_hd_channels = readl(lrwbuffer);
1754 /*firm_ide_channels,4,16-19*/
1757 static void arcmsr_get_firmware_spec(struct AdapterControlBlock *acb)
1759 switch (acb->adapter_type) {
1760 case ACB_ADAPTER_TYPE_A: {
1761 arcmsr_get_hba_config(acb);
1765 case ACB_ADAPTER_TYPE_B: {
1766 arcmsr_get_hbb_config(acb);
1772 static void arcmsr_polling_hba_ccbdone(struct AdapterControlBlock *acb,
1773 struct CommandControlBlock *poll_ccb)
1775 struct MessageUnit_A __iomem *reg = acb->pmuA;
1776 struct CommandControlBlock *ccb;
1777 uint32_t flag_ccb, outbound_intstatus, poll_ccb_done = 0, poll_count = 0;
1779 polling_hba_ccb_retry:
1781 outbound_intstatus = readl(®->outbound_intstatus) & acb->outbound_int_enable;
1782 writel(outbound_intstatus, ®->outbound_intstatus);/*clear interrupt*/
1784 if ((flag_ccb = readl(®->outbound_queueport)) == 0xFFFFFFFF) {
1789 if (poll_count > 100)
1791 goto polling_hba_ccb_retry;
1794 ccb = (struct CommandControlBlock *)(acb->vir2phy_offset + (flag_ccb << 5));
1795 poll_ccb_done = (ccb == poll_ccb) ? 1:0;
1796 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
1797 if ((ccb->startdone == ARCMSR_CCB_ABORTED) || (ccb == poll_ccb)) {
1798 printk(KERN_NOTICE "arcmsr%d: scsi id = %d lun = %d ccb = '0x%p'"
1799 " poll command abort successfully \n"
1800 , acb->host->host_no
1801 , ccb->pcmd->device->id
1802 , ccb->pcmd->device->lun
1804 ccb->pcmd->result = DID_ABORT << 16;
1805 arcmsr_ccb_complete(ccb, 1);
1809 printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
1810 " command done ccb = '0x%p'"
1811 "ccboutstandingcount = %d \n"
1812 , acb->host->host_no
1814 , atomic_read(&acb->ccboutstandingcount));
1817 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
1821 static void arcmsr_polling_hbb_ccbdone(struct AdapterControlBlock *acb, \
1822 struct CommandControlBlock *poll_ccb)
1824 struct MessageUnit_B *reg = acb->pmuB;
1825 struct CommandControlBlock *ccb;
1826 uint32_t flag_ccb, poll_ccb_done = 0, poll_count = 0;
1829 polling_hbb_ccb_retry:
1831 /* clear doorbell interrupt */
1832 writel(ARCMSR_DOORBELL_INT_CLEAR_PATTERN, reg->iop2drv_doorbell_reg);
1834 index = reg->doneq_index;
1835 if ((flag_ccb = readl(®->done_qbuffer[index])) == 0) {
1840 if (poll_count > 100)
1842 goto polling_hbb_ccb_retry;
1845 writel(0, ®->done_qbuffer[index]);
1847 /*if last index number set it to 0 */
1848 index %= ARCMSR_MAX_HBB_POSTQUEUE;
1849 reg->doneq_index = index;
1850 /* check ifcommand done with no error*/
1851 ccb = (struct CommandControlBlock *)\
1852 (acb->vir2phy_offset + (flag_ccb << 5));/*frame must be 32 bytes aligned*/
1853 poll_ccb_done = (ccb == poll_ccb) ? 1:0;
1854 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
1855 if (ccb->startdone == ARCMSR_CCB_ABORTED) {
1856 printk(KERN_NOTICE "arcmsr%d: \
1857 scsi id = %d lun = %d ccb = '0x%p' poll command abort successfully \n"
1859 ,ccb->pcmd->device->id
1860 ,ccb->pcmd->device->lun
1862 ccb->pcmd->result = DID_ABORT << 16;
1863 arcmsr_ccb_complete(ccb, 1);
1866 printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
1867 " command done ccb = '0x%p'"
1868 "ccboutstandingcount = %d \n"
1869 , acb->host->host_no
1871 , atomic_read(&acb->ccboutstandingcount));
1874 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
1875 } /*drain reply FIFO*/
1878 static void arcmsr_polling_ccbdone(struct AdapterControlBlock *acb, \
1879 struct CommandControlBlock *poll_ccb)
1881 switch (acb->adapter_type) {
1883 case ACB_ADAPTER_TYPE_A: {
1884 arcmsr_polling_hba_ccbdone(acb,poll_ccb);
1888 case ACB_ADAPTER_TYPE_B: {
1889 arcmsr_polling_hbb_ccbdone(acb,poll_ccb);
1894 static int arcmsr_iop_confirm(struct AdapterControlBlock *acb)
1896 uint32_t cdb_phyaddr, ccb_phyaddr_hi32;
1897 dma_addr_t dma_coherent_handle;
1899 ********************************************************************
1900 ** here we need to tell iop 331 our freeccb.HighPart
1901 ** if freeccb.HighPart is not zero
1902 ********************************************************************
1904 dma_coherent_handle = acb->dma_coherent_handle;
1905 cdb_phyaddr = (uint32_t)(dma_coherent_handle);
1906 ccb_phyaddr_hi32 = (uint32_t)((cdb_phyaddr >> 16) >> 16);
1908 ***********************************************************************
1909 ** if adapter type B, set window of "post command Q"
1910 ***********************************************************************
1912 switch (acb->adapter_type) {
1914 case ACB_ADAPTER_TYPE_A: {
1915 if (ccb_phyaddr_hi32 != 0) {
1916 struct MessageUnit_A __iomem *reg = acb->pmuA;
1917 uint32_t intmask_org;
1918 intmask_org = arcmsr_disable_outbound_ints(acb);
1919 writel(ARCMSR_SIGNATURE_SET_CONFIG, \
1920 ®->message_rwbuffer[0]);
1921 writel(ccb_phyaddr_hi32, ®->message_rwbuffer[1]);
1922 writel(ARCMSR_INBOUND_MESG0_SET_CONFIG, \
1923 ®->inbound_msgaddr0);
1924 if (arcmsr_hba_wait_msgint_ready(acb)) {
1925 printk(KERN_NOTICE "arcmsr%d: ""set ccb high \
1926 part physical address timeout\n",
1927 acb->host->host_no);
1930 arcmsr_enable_outbound_ints(acb, intmask_org);
1935 case ACB_ADAPTER_TYPE_B: {
1936 unsigned long post_queue_phyaddr;
1937 uint32_t __iomem *rwbuffer;
1939 struct MessageUnit_B *reg = acb->pmuB;
1940 uint32_t intmask_org;
1941 intmask_org = arcmsr_disable_outbound_ints(acb);
1942 reg->postq_index = 0;
1943 reg->doneq_index = 0;
1944 writel(ARCMSR_MESSAGE_SET_POST_WINDOW, reg->drv2iop_doorbell_reg);
1945 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1946 printk(KERN_NOTICE "arcmsr%d:can not set diver mode\n", \
1947 acb->host->host_no);
1950 post_queue_phyaddr = cdb_phyaddr + ARCMSR_MAX_FREECCB_NUM * \
1951 sizeof(struct CommandControlBlock) + offsetof(struct MessageUnit_B, post_qbuffer) ;
1952 rwbuffer = reg->msgcode_rwbuffer_reg;
1953 /* driver "set config" signature */
1954 writel(ARCMSR_SIGNATURE_SET_CONFIG, rwbuffer++);
1955 /* normal should be zero */
1956 writel(ccb_phyaddr_hi32, rwbuffer++);
1957 /* postQ size (256 + 8)*4 */
1958 writel(post_queue_phyaddr, rwbuffer++);
1959 /* doneQ size (256 + 8)*4 */
1960 writel(post_queue_phyaddr + 1056, rwbuffer++);
1961 /* ccb maxQ size must be --> [(256 + 8)*4]*/
1962 writel(1056, rwbuffer);
1964 writel(ARCMSR_MESSAGE_SET_CONFIG, reg->drv2iop_doorbell_reg);
1965 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1966 printk(KERN_NOTICE "arcmsr%d: 'set command Q window' \
1967 timeout \n",acb->host->host_no);
1971 writel(ARCMSR_MESSAGE_START_DRIVER_MODE, reg->drv2iop_doorbell_reg);
1972 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1973 printk(KERN_NOTICE "arcmsr%d: 'can not set diver mode \n"\
1974 ,acb->host->host_no);
1977 arcmsr_enable_outbound_ints(acb, intmask_org);
1984 static void arcmsr_wait_firmware_ready(struct AdapterControlBlock *acb)
1986 uint32_t firmware_state = 0;
1988 switch (acb->adapter_type) {
1990 case ACB_ADAPTER_TYPE_A: {
1991 struct MessageUnit_A __iomem *reg = acb->pmuA;
1993 firmware_state = readl(®->outbound_msgaddr1);
1994 } while ((firmware_state & ARCMSR_OUTBOUND_MESG1_FIRMWARE_OK) == 0);
1998 case ACB_ADAPTER_TYPE_B: {
1999 struct MessageUnit_B *reg = acb->pmuB;
2001 firmware_state = readl(reg->iop2drv_doorbell_reg);
2002 } while ((firmware_state & ARCMSR_MESSAGE_FIRMWARE_OK) == 0);
2008 static void arcmsr_start_hba_bgrb(struct AdapterControlBlock *acb)
2010 struct MessageUnit_A __iomem *reg = acb->pmuA;
2011 acb->acb_flags |= ACB_F_MSG_START_BGRB;
2012 writel(ARCMSR_INBOUND_MESG0_START_BGRB, ®->inbound_msgaddr0);
2013 if (arcmsr_hba_wait_msgint_ready(acb)) {
2014 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
2015 rebulid' timeout \n", acb->host->host_no);
2019 static void arcmsr_start_hbb_bgrb(struct AdapterControlBlock *acb)
2021 struct MessageUnit_B *reg = acb->pmuB;
2022 acb->acb_flags |= ACB_F_MSG_START_BGRB;
2023 writel(ARCMSR_MESSAGE_START_BGRB, reg->drv2iop_doorbell_reg);
2024 if (arcmsr_hbb_wait_msgint_ready(acb)) {
2025 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
2026 rebulid' timeout \n",acb->host->host_no);
2030 static void arcmsr_start_adapter_bgrb(struct AdapterControlBlock *acb)
2032 switch (acb->adapter_type) {
2033 case ACB_ADAPTER_TYPE_A:
2034 arcmsr_start_hba_bgrb(acb);
2036 case ACB_ADAPTER_TYPE_B:
2037 arcmsr_start_hbb_bgrb(acb);
2042 static void arcmsr_clear_doorbell_queue_buffer(struct AdapterControlBlock *acb)
2044 switch (acb->adapter_type) {
2045 case ACB_ADAPTER_TYPE_A: {
2046 struct MessageUnit_A __iomem *reg = acb->pmuA;
2047 uint32_t outbound_doorbell;
2048 /* empty doorbell Qbuffer if door bell ringed */
2049 outbound_doorbell = readl(®->outbound_doorbell);
2050 /*clear doorbell interrupt */
2051 writel(outbound_doorbell, ®->outbound_doorbell);
2052 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, ®->inbound_doorbell);
2056 case ACB_ADAPTER_TYPE_B: {
2057 struct MessageUnit_B *reg = acb->pmuB;
2058 /*clear interrupt and message state*/
2059 writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN, reg->iop2drv_doorbell_reg);
2060 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell_reg);
2061 /* let IOP know data has been read */
2067 static void arcmsr_iop_init(struct AdapterControlBlock *acb)
2069 uint32_t intmask_org;
2071 arcmsr_wait_firmware_ready(acb);
2072 arcmsr_iop_confirm(acb);
2073 /* disable all outbound interrupt */
2074 intmask_org = arcmsr_disable_outbound_ints(acb);
2075 arcmsr_get_firmware_spec(acb);
2076 /*start background rebuild*/
2077 arcmsr_start_adapter_bgrb(acb);
2078 /* empty doorbell Qbuffer if door bell ringed */
2079 arcmsr_clear_doorbell_queue_buffer(acb);
2080 /* enable outbound Post Queue,outbound doorbell Interrupt */
2081 arcmsr_enable_outbound_ints(acb, intmask_org);
2082 acb->acb_flags |= ACB_F_IOP_INITED;
2085 static void arcmsr_iop_reset(struct AdapterControlBlock *acb)
2087 struct CommandControlBlock *ccb;
2088 uint32_t intmask_org;
2091 if (atomic_read(&acb->ccboutstandingcount) != 0) {
2092 /* talk to iop 331 outstanding command aborted */
2093 arcmsr_abort_allcmd(acb);
2095 /* wait for 3 sec for all command aborted*/
2098 /* disable all outbound interrupt */
2099 intmask_org = arcmsr_disable_outbound_ints(acb);
2100 /* clear all outbound posted Q */
2101 arcmsr_done4abort_postqueue(acb);
2102 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2103 ccb = acb->pccb_pool[i];
2104 if (ccb->startdone == ARCMSR_CCB_START) {
2105 ccb->startdone = ARCMSR_CCB_ABORTED;
2106 arcmsr_ccb_complete(ccb, 1);
2109 /* enable all outbound interrupt */
2110 arcmsr_enable_outbound_ints(acb, intmask_org);
2114 static int arcmsr_bus_reset(struct scsi_cmnd *cmd)
2116 struct AdapterControlBlock *acb =
2117 (struct AdapterControlBlock *)cmd->device->host->hostdata;
2121 acb->acb_flags |= ACB_F_BUS_RESET;
2122 for (i = 0; i < 400; i++) {
2123 if (!atomic_read(&acb->ccboutstandingcount))
2125 arcmsr_interrupt(acb);/* FIXME: need spinlock */
2128 arcmsr_iop_reset(acb);
2129 acb->acb_flags &= ~ACB_F_BUS_RESET;
2133 static void arcmsr_abort_one_cmd(struct AdapterControlBlock *acb,
2134 struct CommandControlBlock *ccb)
2138 ccb->startdone = ARCMSR_CCB_ABORTED;
2141 ** Wait for 3 sec for all command done.
2145 intmask = arcmsr_disable_outbound_ints(acb);
2146 arcmsr_polling_ccbdone(acb, ccb);
2147 arcmsr_enable_outbound_ints(acb, intmask);
2150 static int arcmsr_abort(struct scsi_cmnd *cmd)
2152 struct AdapterControlBlock *acb =
2153 (struct AdapterControlBlock *)cmd->device->host->hostdata;
2157 "arcmsr%d: abort device command of scsi id = %d lun = %d \n",
2158 acb->host->host_no, cmd->device->id, cmd->device->lun);
2161 ************************************************
2162 ** the all interrupt service routine is locked
2163 ** we need to handle it as soon as possible and exit
2164 ************************************************
2166 if (!atomic_read(&acb->ccboutstandingcount))
2169 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2170 struct CommandControlBlock *ccb = acb->pccb_pool[i];
2171 if (ccb->startdone == ARCMSR_CCB_START && ccb->pcmd == cmd) {
2172 arcmsr_abort_one_cmd(acb, ccb);
2180 static const char *arcmsr_info(struct Scsi_Host *host)
2182 struct AdapterControlBlock *acb =
2183 (struct AdapterControlBlock *) host->hostdata;
2184 static char buf[256];
2188 switch (acb->pdev->device) {
2189 case PCI_DEVICE_ID_ARECA_1110:
2190 case PCI_DEVICE_ID_ARECA_1200:
2191 case PCI_DEVICE_ID_ARECA_1202:
2192 case PCI_DEVICE_ID_ARECA_1210:
2195 case PCI_DEVICE_ID_ARECA_1120:
2196 case PCI_DEVICE_ID_ARECA_1130:
2197 case PCI_DEVICE_ID_ARECA_1160:
2198 case PCI_DEVICE_ID_ARECA_1170:
2199 case PCI_DEVICE_ID_ARECA_1201:
2200 case PCI_DEVICE_ID_ARECA_1220:
2201 case PCI_DEVICE_ID_ARECA_1230:
2202 case PCI_DEVICE_ID_ARECA_1260:
2203 case PCI_DEVICE_ID_ARECA_1270:
2204 case PCI_DEVICE_ID_ARECA_1280:
2207 case PCI_DEVICE_ID_ARECA_1380:
2208 case PCI_DEVICE_ID_ARECA_1381:
2209 case PCI_DEVICE_ID_ARECA_1680:
2210 case PCI_DEVICE_ID_ARECA_1681:
2217 sprintf(buf, "Areca %s Host Adapter RAID Controller%s\n %s",
2218 type, raid6 ? "( RAID6 capable)" : "",
2219 ARCMSR_DRIVER_VERSION);
2222 #ifdef CONFIG_SCSI_ARCMSR_AER
2223 static pci_ers_result_t arcmsr_pci_slot_reset(struct pci_dev *pdev)
2225 struct Scsi_Host *host = pci_get_drvdata(pdev);
2226 struct AdapterControlBlock *acb =
2227 (struct AdapterControlBlock *) host->hostdata;
2228 uint32_t intmask_org;
2231 if (pci_enable_device(pdev)) {
2232 return PCI_ERS_RESULT_DISCONNECT;
2234 pci_set_master(pdev);
2235 intmask_org = arcmsr_disable_outbound_ints(acb);
2236 acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
2237 ACB_F_MESSAGE_RQBUFFER_CLEARED |
2238 ACB_F_MESSAGE_WQBUFFER_READED);
2239 acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
2240 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
2241 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
2242 acb->devstate[i][j] = ARECA_RAID_GONE;
2244 arcmsr_wait_firmware_ready(acb);
2245 arcmsr_iop_confirm(acb);
2246 /* disable all outbound interrupt */
2247 arcmsr_get_firmware_spec(acb);
2248 /*start background rebuild*/
2249 arcmsr_start_adapter_bgrb(acb);
2250 /* empty doorbell Qbuffer if door bell ringed */
2251 arcmsr_clear_doorbell_queue_buffer(acb);
2252 /* enable outbound Post Queue,outbound doorbell Interrupt */
2253 arcmsr_enable_outbound_ints(acb, intmask_org);
2254 acb->acb_flags |= ACB_F_IOP_INITED;
2256 pci_enable_pcie_error_reporting(pdev);
2257 return PCI_ERS_RESULT_RECOVERED;
2260 static void arcmsr_pci_ers_need_reset_forepart(struct pci_dev *pdev)
2262 struct Scsi_Host *host = pci_get_drvdata(pdev);
2263 struct AdapterControlBlock *acb = (struct AdapterControlBlock *)host->hostdata;
2264 struct CommandControlBlock *ccb;
2265 uint32_t intmask_org;
2268 if (atomic_read(&acb->ccboutstandingcount) != 0) {
2269 /* talk to iop 331 outstanding command aborted */
2270 arcmsr_abort_allcmd(acb);
2271 /* wait for 3 sec for all command aborted*/
2273 /* disable all outbound interrupt */
2274 intmask_org = arcmsr_disable_outbound_ints(acb);
2275 /* clear all outbound posted Q */
2276 arcmsr_done4abort_postqueue(acb);
2277 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2278 ccb = acb->pccb_pool[i];
2279 if (ccb->startdone == ARCMSR_CCB_START) {
2280 ccb->startdone = ARCMSR_CCB_ABORTED;
2281 arcmsr_ccb_complete(ccb, 1);
2284 /* enable all outbound interrupt */
2285 arcmsr_enable_outbound_ints(acb, intmask_org);
2287 pci_disable_device(pdev);
2290 static void arcmsr_pci_ers_disconnect_forepart(struct pci_dev *pdev)
2292 struct Scsi_Host *host = pci_get_drvdata(pdev);
2293 struct AdapterControlBlock *acb = \
2294 (struct AdapterControlBlock *)host->hostdata;
2296 arcmsr_stop_adapter_bgrb(acb);
2297 arcmsr_flush_adapter_cache(acb);
2300 static pci_ers_result_t arcmsr_pci_error_detected(struct pci_dev *pdev,
2301 pci_channel_state_t state)
2304 case pci_channel_io_frozen:
2305 arcmsr_pci_ers_need_reset_forepart(pdev);
2306 return PCI_ERS_RESULT_NEED_RESET;
2307 case pci_channel_io_perm_failure:
2308 arcmsr_pci_ers_disconnect_forepart(pdev);
2309 return PCI_ERS_RESULT_DISCONNECT;
2312 return PCI_ERS_RESULT_NEED_RESET;