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: erich@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 <erich@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, struct scsi_cmnd *cmd);
80 static int arcmsr_abort(struct scsi_cmnd *);
81 static int arcmsr_bus_reset(struct scsi_cmnd *);
82 static int arcmsr_bios_param(struct scsi_device *sdev,
83 struct block_device *bdev, sector_t capacity, int *info);
84 static int arcmsr_queue_command(struct scsi_cmnd * cmd,
85 void (*done) (struct scsi_cmnd *));
86 static int arcmsr_probe(struct pci_dev *pdev,
87 const struct pci_device_id *id);
88 static void arcmsr_remove(struct pci_dev *pdev);
89 static void arcmsr_shutdown(struct pci_dev *pdev);
90 static void arcmsr_iop_init(struct AdapterControlBlock *acb);
91 static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb);
92 static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb);
93 static void arcmsr_flush_adapter_cache(struct AdapterControlBlock *acb);
94 static uint8_t arcmsr_wait_msgint_ready(struct AdapterControlBlock *acb);
95 static const char *arcmsr_info(struct Scsi_Host *);
96 static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb);
97 static pci_ers_result_t arcmsr_pci_error_detected(struct pci_dev *pdev,
98 pci_channel_state_t state);
99 static pci_ers_result_t arcmsr_pci_slot_reset(struct pci_dev *pdev);
100 static int arcmsr_adjust_disk_queue_depth(struct scsi_device *sdev, int queue_depth)
102 if (queue_depth > ARCMSR_MAX_CMD_PERLUN)
103 queue_depth = ARCMSR_MAX_CMD_PERLUN;
104 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
108 static struct scsi_host_template arcmsr_scsi_host_template = {
109 .module = THIS_MODULE,
110 .name = "ARCMSR ARECA SATA/SAS RAID HOST Adapter"
111 ARCMSR_DRIVER_VERSION,
113 .queuecommand = arcmsr_queue_command,
114 .eh_abort_handler = arcmsr_abort,
115 .eh_bus_reset_handler = arcmsr_bus_reset,
116 .bios_param = arcmsr_bios_param,
117 .change_queue_depth = arcmsr_adjust_disk_queue_depth,
118 .can_queue = ARCMSR_MAX_OUTSTANDING_CMD,
119 .this_id = ARCMSR_SCSI_INITIATOR_ID,
120 .sg_tablesize = ARCMSR_MAX_SG_ENTRIES,
121 .max_sectors = ARCMSR_MAX_XFER_SECTORS,
122 .cmd_per_lun = ARCMSR_MAX_CMD_PERLUN,
123 .use_clustering = ENABLE_CLUSTERING,
124 .shost_attrs = arcmsr_host_attrs,
126 static struct pci_error_handlers arcmsr_pci_error_handlers = {
127 .error_detected = arcmsr_pci_error_detected,
128 .slot_reset = arcmsr_pci_slot_reset,
131 static struct pci_device_id arcmsr_device_id_table[] = {
132 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1110)},
133 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1120)},
134 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1130)},
135 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1160)},
136 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1170)},
137 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1210)},
138 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1220)},
139 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1230)},
140 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1260)},
141 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1270)},
142 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1280)},
143 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1380)},
144 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1381)},
145 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1680)},
146 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1681)},
147 {0, 0}, /* Terminating entry */
149 MODULE_DEVICE_TABLE(pci, arcmsr_device_id_table);
150 static struct pci_driver arcmsr_pci_driver = {
152 .id_table = arcmsr_device_id_table,
153 .probe = arcmsr_probe,
154 .remove = arcmsr_remove,
155 .shutdown = arcmsr_shutdown,
156 .err_handler = &arcmsr_pci_error_handlers,
159 static irqreturn_t arcmsr_do_interrupt(int irq, void *dev_id)
161 irqreturn_t handle_state;
162 struct AdapterControlBlock *acb;
165 acb = (struct AdapterControlBlock *)dev_id;
167 spin_lock_irqsave(acb->host->host_lock, flags);
168 handle_state = arcmsr_interrupt(acb);
169 spin_unlock_irqrestore(acb->host->host_lock, flags);
173 static int arcmsr_bios_param(struct scsi_device *sdev,
174 struct block_device *bdev, sector_t capacity, int *geom)
176 int ret, heads, sectors, cylinders, total_capacity;
177 unsigned char *buffer;/* return copy of block device's partition table */
179 buffer = scsi_bios_ptable(bdev);
181 ret = scsi_partsize(buffer, capacity, &geom[2], &geom[0], &geom[1]);
186 total_capacity = capacity;
189 cylinders = total_capacity / (heads * sectors);
190 if (cylinders > 1024) {
193 cylinders = total_capacity / (heads * sectors);
201 static int arcmsr_alloc_ccb_pool(struct AdapterControlBlock *acb)
203 struct pci_dev *pdev = acb->pdev;
204 struct MessageUnit __iomem *reg = acb->pmu;
205 u32 ccb_phyaddr_hi32;
207 dma_addr_t dma_coherent_handle, dma_addr;
208 struct CommandControlBlock *ccb_tmp;
211 dma_coherent = dma_alloc_coherent(&pdev->dev,
212 ARCMSR_MAX_FREECCB_NUM *
213 sizeof (struct CommandControlBlock) + 0x20,
214 &dma_coherent_handle, GFP_KERNEL);
218 acb->dma_coherent = dma_coherent;
219 acb->dma_coherent_handle = dma_coherent_handle;
221 if (((unsigned long)dma_coherent & 0x1F)) {
222 dma_coherent = dma_coherent +
223 (0x20 - ((unsigned long)dma_coherent & 0x1F));
224 dma_coherent_handle = dma_coherent_handle +
225 (0x20 - ((unsigned long)dma_coherent_handle & 0x1F));
228 dma_addr = dma_coherent_handle;
229 ccb_tmp = (struct CommandControlBlock *)dma_coherent;
230 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
231 ccb_tmp->cdb_shifted_phyaddr = dma_addr >> 5;
233 acb->pccb_pool[i] = ccb_tmp;
234 list_add_tail(&ccb_tmp->list, &acb->ccb_free_list);
235 dma_addr = dma_addr + sizeof (struct CommandControlBlock);
239 acb->vir2phy_offset = (unsigned long)ccb_tmp -
240 (unsigned long)dma_addr;
241 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
242 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
243 acb->devstate[i][j] = ARECA_RAID_GOOD;
246 ** here we need to tell iop 331 our ccb_tmp.HighPart
247 ** if ccb_tmp.HighPart is not zero
249 ccb_phyaddr_hi32 = (uint32_t) ((dma_coherent_handle >> 16) >> 16);
250 if (ccb_phyaddr_hi32 != 0) {
251 writel(ARCMSR_SIGNATURE_SET_CONFIG, ®->message_rwbuffer[0]);
252 writel(ccb_phyaddr_hi32, ®->message_rwbuffer[1]);
253 writel(ARCMSR_INBOUND_MESG0_SET_CONFIG, ®->inbound_msgaddr0);
254 if (arcmsr_wait_msgint_ready(acb))
255 printk(KERN_NOTICE "arcmsr%d: "
256 "'set ccb high part physical address' timeout\n",
260 writel(readl(®->outbound_intmask) |
261 ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE,
262 ®->outbound_intmask);
266 static int arcmsr_probe(struct pci_dev *pdev,
267 const struct pci_device_id *id)
269 struct Scsi_Host *host;
270 struct AdapterControlBlock *acb;
271 uint8_t bus, dev_fun;
274 error = pci_enable_device(pdev);
277 pci_set_master(pdev);
279 host = scsi_host_alloc(&arcmsr_scsi_host_template,
280 sizeof(struct AdapterControlBlock));
283 goto out_disable_device;
285 acb = (struct AdapterControlBlock *)host->hostdata;
286 memset(acb, 0, sizeof (struct AdapterControlBlock));
288 error = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
290 error = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
293 "scsi%d: No suitable DMA mask available\n",
298 bus = pdev->bus->number;
299 dev_fun = pdev->devfn;
302 host->max_sectors = ARCMSR_MAX_XFER_SECTORS;
303 host->max_lun = ARCMSR_MAX_TARGETLUN;
304 host->max_id = ARCMSR_MAX_TARGETID;/*16:8*/
305 host->max_cmd_len = 16; /*this is issue of 64bit LBA, over 2T byte*/
306 host->sg_tablesize = ARCMSR_MAX_SG_ENTRIES;
307 host->can_queue = ARCMSR_MAX_FREECCB_NUM; /* max simultaneous cmds */
308 host->cmd_per_lun = ARCMSR_MAX_CMD_PERLUN;
309 host->this_id = ARCMSR_SCSI_INITIATOR_ID;
310 host->unique_id = (bus << 8) | dev_fun;
311 host->irq = pdev->irq;
312 error = pci_request_regions(pdev, "arcmsr");
316 acb->pmu = ioremap(pci_resource_start(pdev, 0),
317 pci_resource_len(pdev, 0));
319 printk(KERN_NOTICE "arcmsr%d: memory"
320 " mapping region fail \n", acb->host->host_no);
321 goto out_release_regions;
323 acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
324 ACB_F_MESSAGE_RQBUFFER_CLEARED |
325 ACB_F_MESSAGE_WQBUFFER_READED);
326 acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
327 INIT_LIST_HEAD(&acb->ccb_free_list);
329 error = arcmsr_alloc_ccb_pool(acb);
333 error = request_irq(pdev->irq, arcmsr_do_interrupt,
334 IRQF_DISABLED | IRQF_SHARED, "arcmsr", acb);
336 goto out_free_ccb_pool;
338 arcmsr_iop_init(acb);
339 pci_set_drvdata(pdev, host);
340 if (strncmp(acb->firm_version, "V1.42", 5) >= 0)
341 host->max_sectors= ARCMSR_MAX_XFER_SECTORS_B;
343 error = scsi_add_host(host, &pdev->dev);
347 error = arcmsr_alloc_sysfs_attr(acb);
351 scsi_scan_host(host);
352 pci_enable_pcie_error_reporting(pdev);
356 free_irq(pdev->irq, acb);
358 arcmsr_free_ccb_pool(acb);
362 pci_release_regions(pdev);
366 pci_disable_device(pdev);
371 static void arcmsr_abort_allcmd(struct AdapterControlBlock *acb)
373 struct MessageUnit __iomem *reg = acb->pmu;
375 writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, ®->inbound_msgaddr0);
376 if (arcmsr_wait_msgint_ready(acb))
378 "arcmsr%d: wait 'abort all outstanding command' timeout \n"
379 , acb->host->host_no);
382 static void arcmsr_pci_unmap_dma(struct CommandControlBlock *ccb)
384 struct scsi_cmnd *pcmd = ccb->pcmd;
386 scsi_dma_unmap(pcmd);
389 static void arcmsr_ccb_complete(struct CommandControlBlock *ccb, int stand_flag)
391 struct AdapterControlBlock *acb = ccb->acb;
392 struct scsi_cmnd *pcmd = ccb->pcmd;
394 arcmsr_pci_unmap_dma(ccb);
396 atomic_dec(&acb->ccboutstandingcount);
397 ccb->startdone = ARCMSR_CCB_DONE;
399 list_add_tail(&ccb->list, &acb->ccb_free_list);
400 pcmd->scsi_done(pcmd);
403 static void arcmsr_remove(struct pci_dev *pdev)
405 struct Scsi_Host *host = pci_get_drvdata(pdev);
406 struct AdapterControlBlock *acb =
407 (struct AdapterControlBlock *) host->hostdata;
408 struct MessageUnit __iomem *reg = acb->pmu;
411 arcmsr_free_sysfs_attr(acb);
412 scsi_remove_host(host);
413 arcmsr_stop_adapter_bgrb(acb);
414 arcmsr_flush_adapter_cache(acb);
415 writel(readl(®->outbound_intmask) |
416 ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE,
417 ®->outbound_intmask);
418 acb->acb_flags |= ACB_F_SCSISTOPADAPTER;
419 acb->acb_flags &= ~ACB_F_IOP_INITED;
421 for (poll_count = 0; poll_count < 256; poll_count++) {
422 if (!atomic_read(&acb->ccboutstandingcount))
424 arcmsr_interrupt(acb);
428 if (atomic_read(&acb->ccboutstandingcount)) {
431 arcmsr_abort_allcmd(acb);
432 for (i = 0; i < ARCMSR_MAX_OUTSTANDING_CMD; i++)
433 readl(®->outbound_queueport);
434 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
435 struct CommandControlBlock *ccb = acb->pccb_pool[i];
436 if (ccb->startdone == ARCMSR_CCB_START) {
437 ccb->startdone = ARCMSR_CCB_ABORTED;
438 ccb->pcmd->result = DID_ABORT << 16;
439 arcmsr_ccb_complete(ccb, 1);
444 free_irq(pdev->irq, acb);
446 arcmsr_free_ccb_pool(acb);
447 pci_release_regions(pdev);
451 pci_disable_device(pdev);
452 pci_set_drvdata(pdev, NULL);
455 static void arcmsr_shutdown(struct pci_dev *pdev)
457 struct Scsi_Host *host = pci_get_drvdata(pdev);
458 struct AdapterControlBlock *acb =
459 (struct AdapterControlBlock *)host->hostdata;
461 arcmsr_stop_adapter_bgrb(acb);
462 arcmsr_flush_adapter_cache(acb);
465 static int arcmsr_module_init(void)
469 error = pci_register_driver(&arcmsr_pci_driver);
473 static void arcmsr_module_exit(void)
475 pci_unregister_driver(&arcmsr_pci_driver);
477 module_init(arcmsr_module_init);
478 module_exit(arcmsr_module_exit);
480 static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb)
482 struct MessageUnit __iomem *reg = acb->pmu;
483 u32 orig_mask = readl(®->outbound_intmask);
485 writel(orig_mask | ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE,
486 ®->outbound_intmask);
490 static void arcmsr_enable_outbound_ints(struct AdapterControlBlock *acb,
493 struct MessageUnit __iomem *reg = acb->pmu;
496 mask = orig_mask & ~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE |
497 ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE);
498 writel(mask, ®->outbound_intmask);
501 static void arcmsr_flush_adapter_cache(struct AdapterControlBlock *acb)
503 struct MessageUnit __iomem *reg = acb->pmu;
505 writel(ARCMSR_INBOUND_MESG0_FLUSH_CACHE, ®->inbound_msgaddr0);
506 if (arcmsr_wait_msgint_ready(acb))
508 "arcmsr%d: wait 'flush adapter cache' timeout \n"
509 , acb->host->host_no);
512 static void arcmsr_report_sense_info(struct CommandControlBlock *ccb)
514 struct scsi_cmnd *pcmd = ccb->pcmd;
515 struct SENSE_DATA *sensebuffer = (struct SENSE_DATA *)pcmd->sense_buffer;
517 pcmd->result = DID_OK << 16;
519 int sense_data_length =
520 sizeof (struct SENSE_DATA) < sizeof (pcmd->sense_buffer)
521 ? sizeof (struct SENSE_DATA) : sizeof (pcmd->sense_buffer);
522 memset(sensebuffer, 0, sizeof (pcmd->sense_buffer));
523 memcpy(sensebuffer, ccb->arcmsr_cdb.SenseData, sense_data_length);
524 sensebuffer->ErrorCode = SCSI_SENSE_CURRENT_ERRORS;
525 sensebuffer->Valid = 1;
529 static uint8_t arcmsr_wait_msgint_ready(struct AdapterControlBlock *acb)
531 struct MessageUnit __iomem *reg = acb->pmu;
533 uint8_t Retries = 0x00;
536 for (Index = 0; Index < 100; Index++) {
537 if (readl(®->outbound_intstatus)
538 & ARCMSR_MU_OUTBOUND_MESSAGE0_INT) {
539 writel(ARCMSR_MU_OUTBOUND_MESSAGE0_INT
540 , ®->outbound_intstatus);
543 msleep_interruptible(10);
545 } while (Retries++ < 20);/*max 20 sec*/
549 static void arcmsr_build_ccb(struct AdapterControlBlock *acb,
550 struct CommandControlBlock *ccb, struct scsi_cmnd *pcmd)
552 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
553 int8_t *psge = (int8_t *)&arcmsr_cdb->u;
554 uint32_t address_lo, address_hi;
555 int arccdbsize = 0x30;
559 memset(arcmsr_cdb, 0, sizeof (struct ARCMSR_CDB));
561 arcmsr_cdb->TargetID = pcmd->device->id;
562 arcmsr_cdb->LUN = pcmd->device->lun;
563 arcmsr_cdb->Function = 1;
564 arcmsr_cdb->CdbLength = (uint8_t)pcmd->cmd_len;
565 arcmsr_cdb->Context = (unsigned long)arcmsr_cdb;
566 memcpy(arcmsr_cdb->Cdb, pcmd->cmnd, pcmd->cmd_len);
568 nseg = scsi_dma_map(pcmd);
572 int length, i, cdb_sgcount = 0;
573 struct scatterlist *sg;
575 /* map stor port SG list to our iop SG List. */
576 scsi_for_each_sg(pcmd, sg, nseg, i) {
577 /* Get the physical address of the current data pointer */
578 length = cpu_to_le32(sg_dma_len(sg));
579 address_lo = cpu_to_le32(dma_addr_lo32(sg_dma_address(sg)));
580 address_hi = cpu_to_le32(dma_addr_hi32(sg_dma_address(sg)));
581 if (address_hi == 0) {
582 struct SG32ENTRY *pdma_sg = (struct SG32ENTRY *)psge;
584 pdma_sg->address = address_lo;
585 pdma_sg->length = length;
586 psge += sizeof (struct SG32ENTRY);
587 arccdbsize += sizeof (struct SG32ENTRY);
589 struct SG64ENTRY *pdma_sg = (struct SG64ENTRY *)psge;
591 pdma_sg->addresshigh = address_hi;
592 pdma_sg->address = address_lo;
593 pdma_sg->length = length|IS_SG64_ADDR;
594 psge += sizeof (struct SG64ENTRY);
595 arccdbsize += sizeof (struct SG64ENTRY);
599 arcmsr_cdb->sgcount = (uint8_t)cdb_sgcount;
600 arcmsr_cdb->DataLength = scsi_bufflen(pcmd);
601 if ( arccdbsize > 256)
602 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_SGL_BSIZE;
604 if (pcmd->sc_data_direction == DMA_TO_DEVICE ) {
605 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_WRITE;
606 ccb->ccb_flags |= CCB_FLAG_WRITE;
610 static void arcmsr_post_ccb(struct AdapterControlBlock *acb, struct CommandControlBlock *ccb)
612 struct MessageUnit __iomem *reg = acb->pmu;
613 uint32_t cdb_shifted_phyaddr = ccb->cdb_shifted_phyaddr;
614 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
616 atomic_inc(&acb->ccboutstandingcount);
617 ccb->startdone = ARCMSR_CCB_START;
618 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE)
619 writel(cdb_shifted_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,
620 ®->inbound_queueport);
622 writel(cdb_shifted_phyaddr, ®->inbound_queueport);
625 void arcmsr_post_Qbuffer(struct AdapterControlBlock *acb)
627 struct MessageUnit __iomem *reg = acb->pmu;
628 struct QBUFFER __iomem *pwbuffer = (struct QBUFFER __iomem *) ®->message_wbuffer;
629 uint8_t __iomem *iop_data = (uint8_t __iomem *) pwbuffer->data;
630 int32_t allxfer_len = 0;
632 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_READED) {
633 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
634 while ((acb->wqbuf_firstindex != acb->wqbuf_lastindex)
635 && (allxfer_len < 124)) {
636 writeb(acb->wqbuffer[acb->wqbuf_firstindex], iop_data);
637 acb->wqbuf_firstindex++;
638 acb->wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
642 writel(allxfer_len, &pwbuffer->data_len);
643 writel(ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK
644 , ®->inbound_doorbell);
648 static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb)
650 struct MessageUnit __iomem *reg = acb->pmu;
652 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
653 writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, ®->inbound_msgaddr0);
654 if (arcmsr_wait_msgint_ready(acb))
656 "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
657 , acb->host->host_no);
660 static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb)
662 dma_free_coherent(&acb->pdev->dev,
663 ARCMSR_MAX_FREECCB_NUM * sizeof (struct CommandControlBlock) + 0x20,
665 acb->dma_coherent_handle);
668 static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb)
670 struct MessageUnit __iomem *reg = acb->pmu;
671 struct CommandControlBlock *ccb;
672 uint32_t flag_ccb, outbound_intstatus, outbound_doorbell;
674 outbound_intstatus = readl(®->outbound_intstatus)
675 & acb->outbound_int_enable;
676 writel(outbound_intstatus, ®->outbound_intstatus);
677 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_DOORBELL_INT) {
678 outbound_doorbell = readl(®->outbound_doorbell);
679 writel(outbound_doorbell, ®->outbound_doorbell);
680 if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK) {
681 struct QBUFFER __iomem * prbuffer =
682 (struct QBUFFER __iomem *) ®->message_rbuffer;
683 uint8_t __iomem * iop_data = (uint8_t __iomem *)prbuffer->data;
684 int32_t my_empty_len, iop_len, rqbuf_firstindex, rqbuf_lastindex;
686 rqbuf_lastindex = acb->rqbuf_lastindex;
687 rqbuf_firstindex = acb->rqbuf_firstindex;
688 iop_len = readl(&prbuffer->data_len);
689 my_empty_len = (rqbuf_firstindex - rqbuf_lastindex - 1)
690 &(ARCMSR_MAX_QBUFFER - 1);
691 if (my_empty_len >= iop_len) {
692 while (iop_len > 0) {
693 acb->rqbuffer[acb->rqbuf_lastindex] = readb(iop_data);
694 acb->rqbuf_lastindex++;
695 acb->rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
699 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK,
700 ®->inbound_doorbell);
702 acb->acb_flags |= ACB_F_IOPDATA_OVERFLOW;
704 if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_READ_OK) {
705 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_READED;
706 if (acb->wqbuf_firstindex != acb->wqbuf_lastindex) {
707 struct QBUFFER __iomem * pwbuffer =
708 (struct QBUFFER __iomem *) ®->message_wbuffer;
709 uint8_t __iomem * iop_data = (uint8_t __iomem *) pwbuffer->data;
710 int32_t allxfer_len = 0;
712 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
713 while ((acb->wqbuf_firstindex != acb->wqbuf_lastindex)
714 && (allxfer_len < 124)) {
715 writeb(acb->wqbuffer[acb->wqbuf_firstindex], iop_data);
716 acb->wqbuf_firstindex++;
717 acb->wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
721 writel(allxfer_len, &pwbuffer->data_len);
722 writel(ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK,
723 ®->inbound_doorbell);
725 if (acb->wqbuf_firstindex == acb->wqbuf_lastindex)
726 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_CLEARED;
729 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_POSTQUEUE_INT) {
732 ****************************************************************
733 ** areca cdb command done
734 ****************************************************************
737 if ((flag_ccb = readl(®->outbound_queueport)) == 0xFFFFFFFF)
738 break;/*chip FIFO no ccb for completion already*/
739 /* check if command done with no error*/
740 ccb = (struct CommandControlBlock *)(acb->vir2phy_offset +
742 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
743 if (ccb->startdone == ARCMSR_CCB_ABORTED) {
744 struct scsi_cmnd *abortcmd = ccb->pcmd;
746 abortcmd->result |= DID_ABORT >> 16;
747 arcmsr_ccb_complete(ccb, 1);
749 "arcmsr%d: ccb ='0x%p' isr got aborted command \n"
750 , acb->host->host_no, ccb);
755 "arcmsr%d: isr get an illegal ccb command done acb = '0x%p'"
756 "ccb = '0x%p' ccbacb = '0x%p' startdone = 0x%x"
757 " ccboutstandingcount = %d \n"
763 , atomic_read(&acb->ccboutstandingcount));
766 id = ccb->pcmd->device->id;
767 lun = ccb->pcmd->device->lun;
768 if (!(flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR)) {
769 if (acb->devstate[id][lun] == ARECA_RAID_GONE)
770 acb->devstate[id][lun] = ARECA_RAID_GOOD;
771 ccb->pcmd->result = DID_OK << 16;
772 arcmsr_ccb_complete(ccb, 1);
774 switch(ccb->arcmsr_cdb.DeviceStatus) {
775 case ARCMSR_DEV_SELECT_TIMEOUT: {
776 acb->devstate[id][lun] = ARECA_RAID_GONE;
777 ccb->pcmd->result = DID_NO_CONNECT << 16;
778 arcmsr_ccb_complete(ccb, 1);
781 case ARCMSR_DEV_ABORTED:
782 case ARCMSR_DEV_INIT_FAIL: {
783 acb->devstate[id][lun] = ARECA_RAID_GONE;
784 ccb->pcmd->result = DID_BAD_TARGET << 16;
785 arcmsr_ccb_complete(ccb, 1);
788 case ARCMSR_DEV_CHECK_CONDITION: {
789 acb->devstate[id][lun] = ARECA_RAID_GOOD;
790 arcmsr_report_sense_info(ccb);
791 arcmsr_ccb_complete(ccb, 1);
796 "arcmsr%d: scsi id = %d lun = %d"
797 " isr get command error done, "
798 "but got unknown DeviceStatus = 0x%x \n"
802 , ccb->arcmsr_cdb.DeviceStatus);
803 acb->devstate[id][lun] = ARECA_RAID_GONE;
804 ccb->pcmd->result = DID_NO_CONNECT << 16;
805 arcmsr_ccb_complete(ccb, 1);
809 }/*drain reply FIFO*/
811 if (!(outbound_intstatus & ARCMSR_MU_OUTBOUND_HANDLE_INT))
816 static void arcmsr_iop_parking(struct AdapterControlBlock *acb)
819 /* stop adapter background rebuild */
820 if (acb->acb_flags & ACB_F_MSG_START_BGRB) {
821 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
822 arcmsr_stop_adapter_bgrb(acb);
823 arcmsr_flush_adapter_cache(acb);
828 static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb, struct scsi_cmnd *cmd)
830 struct MessageUnit __iomem *reg = acb->pmu;
831 struct CMD_MESSAGE_FIELD *pcmdmessagefld;
832 int retvalue = 0, transfer_len = 0;
834 struct scatterlist *sg;
835 uint32_t controlcode = (uint32_t ) cmd->cmnd[5] << 24 |
836 (uint32_t ) cmd->cmnd[6] << 16 |
837 (uint32_t ) cmd->cmnd[7] << 8 |
838 (uint32_t ) cmd->cmnd[8];
839 /* 4 bytes: Areca io control code */
841 sg = scsi_sglist(cmd);
842 buffer = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
843 if (scsi_sg_count(cmd) > 1) {
844 retvalue = ARCMSR_MESSAGE_FAIL;
847 transfer_len += sg->length;
849 if (transfer_len > sizeof(struct CMD_MESSAGE_FIELD)) {
850 retvalue = ARCMSR_MESSAGE_FAIL;
853 pcmdmessagefld = (struct CMD_MESSAGE_FIELD *) buffer;
854 switch(controlcode) {
855 case ARCMSR_MESSAGE_READ_RQBUFFER: {
856 unsigned long *ver_addr;
857 dma_addr_t buf_handle;
858 uint8_t *pQbuffer, *ptmpQbuffer;
859 int32_t allxfer_len = 0;
861 ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
863 retvalue = ARCMSR_MESSAGE_FAIL;
866 ptmpQbuffer = (uint8_t *) ver_addr;
867 while ((acb->rqbuf_firstindex != acb->rqbuf_lastindex)
868 && (allxfer_len < 1031)) {
869 pQbuffer = &acb->rqbuffer[acb->rqbuf_firstindex];
870 memcpy(ptmpQbuffer, pQbuffer, 1);
871 acb->rqbuf_firstindex++;
872 acb->rqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
876 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
877 struct QBUFFER __iomem * prbuffer = (struct QBUFFER __iomem *)
878 ®->message_rbuffer;
879 uint8_t __iomem * iop_data = (uint8_t __iomem *)prbuffer->data;
882 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
883 iop_len = readl(&prbuffer->data_len);
884 while (iop_len > 0) {
885 acb->rqbuffer[acb->rqbuf_lastindex] = readb(iop_data);
886 acb->rqbuf_lastindex++;
887 acb->rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
891 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK,
892 ®->inbound_doorbell);
894 memcpy(pcmdmessagefld->messagedatabuffer,
895 (uint8_t *)ver_addr, allxfer_len);
896 pcmdmessagefld->cmdmessage.Length = allxfer_len;
897 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
898 pci_free_consistent(acb->pdev, 1032, ver_addr, buf_handle);
901 case ARCMSR_MESSAGE_WRITE_WQBUFFER: {
902 unsigned long *ver_addr;
903 dma_addr_t buf_handle;
904 int32_t my_empty_len, user_len, wqbuf_firstindex, wqbuf_lastindex;
905 uint8_t *pQbuffer, *ptmpuserbuffer;
907 ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
909 retvalue = ARCMSR_MESSAGE_FAIL;
912 ptmpuserbuffer = (uint8_t *)ver_addr;
913 user_len = pcmdmessagefld->cmdmessage.Length;
914 memcpy(ptmpuserbuffer, pcmdmessagefld->messagedatabuffer, user_len);
915 wqbuf_lastindex = acb->wqbuf_lastindex;
916 wqbuf_firstindex = acb->wqbuf_firstindex;
917 if (wqbuf_lastindex != wqbuf_firstindex) {
918 struct SENSE_DATA *sensebuffer =
919 (struct SENSE_DATA *)cmd->sense_buffer;
920 arcmsr_post_Qbuffer(acb);
921 /* has error report sensedata */
922 sensebuffer->ErrorCode = 0x70;
923 sensebuffer->SenseKey = ILLEGAL_REQUEST;
924 sensebuffer->AdditionalSenseLength = 0x0A;
925 sensebuffer->AdditionalSenseCode = 0x20;
926 sensebuffer->Valid = 1;
927 retvalue = ARCMSR_MESSAGE_FAIL;
929 my_empty_len = (wqbuf_firstindex-wqbuf_lastindex - 1)
930 &(ARCMSR_MAX_QBUFFER - 1);
931 if (my_empty_len >= user_len) {
932 while (user_len > 0) {
934 &acb->wqbuffer[acb->wqbuf_lastindex];
935 memcpy(pQbuffer, ptmpuserbuffer, 1);
936 acb->wqbuf_lastindex++;
937 acb->wqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
941 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {
943 ~ACB_F_MESSAGE_WQBUFFER_CLEARED;
944 arcmsr_post_Qbuffer(acb);
947 /* has error report sensedata */
948 struct SENSE_DATA *sensebuffer =
949 (struct SENSE_DATA *)cmd->sense_buffer;
950 sensebuffer->ErrorCode = 0x70;
951 sensebuffer->SenseKey = ILLEGAL_REQUEST;
952 sensebuffer->AdditionalSenseLength = 0x0A;
953 sensebuffer->AdditionalSenseCode = 0x20;
954 sensebuffer->Valid = 1;
955 retvalue = ARCMSR_MESSAGE_FAIL;
958 pci_free_consistent(acb->pdev, 1032, ver_addr, buf_handle);
961 case ARCMSR_MESSAGE_CLEAR_RQBUFFER: {
962 uint8_t *pQbuffer = acb->rqbuffer;
964 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
965 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
966 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK,
967 ®->inbound_doorbell);
969 acb->acb_flags |= ACB_F_MESSAGE_RQBUFFER_CLEARED;
970 acb->rqbuf_firstindex = 0;
971 acb->rqbuf_lastindex = 0;
972 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
973 pcmdmessagefld->cmdmessage.ReturnCode =
974 ARCMSR_MESSAGE_RETURNCODE_OK;
977 case ARCMSR_MESSAGE_CLEAR_WQBUFFER: {
978 uint8_t *pQbuffer = acb->wqbuffer;
980 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
981 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
982 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK
983 , ®->inbound_doorbell);
986 (ACB_F_MESSAGE_WQBUFFER_CLEARED |
987 ACB_F_MESSAGE_WQBUFFER_READED);
988 acb->wqbuf_firstindex = 0;
989 acb->wqbuf_lastindex = 0;
990 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
991 pcmdmessagefld->cmdmessage.ReturnCode =
992 ARCMSR_MESSAGE_RETURNCODE_OK;
995 case ARCMSR_MESSAGE_CLEAR_ALLQBUFFER: {
998 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
999 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1000 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK
1001 , ®->inbound_doorbell);
1004 (ACB_F_MESSAGE_WQBUFFER_CLEARED
1005 | ACB_F_MESSAGE_RQBUFFER_CLEARED
1006 | ACB_F_MESSAGE_WQBUFFER_READED);
1007 acb->rqbuf_firstindex = 0;
1008 acb->rqbuf_lastindex = 0;
1009 acb->wqbuf_firstindex = 0;
1010 acb->wqbuf_lastindex = 0;
1011 pQbuffer = acb->rqbuffer;
1012 memset(pQbuffer, 0, sizeof (struct QBUFFER));
1013 pQbuffer = acb->wqbuffer;
1014 memset(pQbuffer, 0, sizeof (struct QBUFFER));
1015 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1018 case ARCMSR_MESSAGE_RETURN_CODE_3F: {
1019 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_3F;
1022 case ARCMSR_MESSAGE_SAY_HELLO: {
1023 int8_t * hello_string = "Hello! I am ARCMSR";
1025 memcpy(pcmdmessagefld->messagedatabuffer, hello_string
1026 , (int16_t)strlen(hello_string));
1027 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1030 case ARCMSR_MESSAGE_SAY_GOODBYE:
1031 arcmsr_iop_parking(acb);
1033 case ARCMSR_MESSAGE_FLUSH_ADAPTER_CACHE:
1034 arcmsr_flush_adapter_cache(acb);
1037 retvalue = ARCMSR_MESSAGE_FAIL;
1040 sg = scsi_sglist(cmd);
1041 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
1046 static struct CommandControlBlock *arcmsr_get_freeccb(struct AdapterControlBlock *acb)
1048 struct list_head *head = &acb->ccb_free_list;
1049 struct CommandControlBlock *ccb = NULL;
1051 if (!list_empty(head)) {
1052 ccb = list_entry(head->next, struct CommandControlBlock, list);
1053 list_del(head->next);
1058 static void arcmsr_handle_virtual_command(struct AdapterControlBlock *acb,
1059 struct scsi_cmnd *cmd)
1061 switch (cmd->cmnd[0]) {
1063 unsigned char inqdata[36];
1065 struct scatterlist *sg;
1067 if (cmd->device->lun) {
1068 cmd->result = (DID_TIME_OUT << 16);
1069 cmd->scsi_done(cmd);
1072 inqdata[0] = TYPE_PROCESSOR;
1073 /* Periph Qualifier & Periph Dev Type */
1075 /* rem media bit & Dev Type Modifier */
1077 /* ISO, ECMA, & ANSI versions */
1079 /* length of additional data */
1080 strncpy(&inqdata[8], "Areca ", 8);
1081 /* Vendor Identification */
1082 strncpy(&inqdata[16], "RAID controller ", 16);
1083 /* Product Identification */
1084 strncpy(&inqdata[32], "R001", 4); /* Product Revision */
1086 sg = scsi_sglist(cmd);
1087 buffer = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
1089 memcpy(buffer, inqdata, sizeof(inqdata));
1090 sg = scsi_sglist(cmd);
1091 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
1093 cmd->scsi_done(cmd);
1098 if (arcmsr_iop_message_xfer(acb, cmd))
1099 cmd->result = (DID_ERROR << 16);
1100 cmd->scsi_done(cmd);
1104 cmd->scsi_done(cmd);
1108 static int arcmsr_queue_command(struct scsi_cmnd *cmd,
1109 void (* done)(struct scsi_cmnd *))
1111 struct Scsi_Host *host = cmd->device->host;
1112 struct AdapterControlBlock *acb =
1113 (struct AdapterControlBlock *) host->hostdata;
1114 struct CommandControlBlock *ccb;
1115 int target = cmd->device->id;
1116 int lun = cmd->device->lun;
1118 cmd->scsi_done = done;
1119 cmd->host_scribble = NULL;
1121 if (acb->acb_flags & ACB_F_BUS_RESET) {
1122 printk(KERN_NOTICE "arcmsr%d: bus reset"
1123 " and return busy \n"
1124 , acb->host->host_no);
1125 return SCSI_MLQUEUE_HOST_BUSY;
1128 /* virtual device for iop message transfer */
1129 arcmsr_handle_virtual_command(acb, cmd);
1132 if (acb->devstate[target][lun] == ARECA_RAID_GONE) {
1135 block_cmd = cmd->cmnd[0] & 0x0f;
1136 if (block_cmd == 0x08 || block_cmd == 0x0a) {
1138 "arcmsr%d: block 'read/write'"
1139 "command with gone raid volume"
1140 " Cmd = %2x, TargetId = %d, Lun = %d \n"
1141 , acb->host->host_no
1144 cmd->result = (DID_NO_CONNECT << 16);
1145 cmd->scsi_done(cmd);
1149 if (atomic_read(&acb->ccboutstandingcount) >=
1150 ARCMSR_MAX_OUTSTANDING_CMD)
1151 return SCSI_MLQUEUE_HOST_BUSY;
1153 ccb = arcmsr_get_freeccb(acb);
1155 return SCSI_MLQUEUE_HOST_BUSY;
1156 arcmsr_build_ccb(acb, ccb, cmd);
1157 arcmsr_post_ccb(acb, ccb);
1161 static void arcmsr_get_firmware_spec(struct AdapterControlBlock *acb)
1163 struct MessageUnit __iomem *reg = acb->pmu;
1164 char *acb_firm_model = acb->firm_model;
1165 char *acb_firm_version = acb->firm_version;
1166 char __iomem *iop_firm_model = (char __iomem *) ®->message_rwbuffer[15];
1167 char __iomem *iop_firm_version = (char __iomem *) ®->message_rwbuffer[17];
1170 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, ®->inbound_msgaddr0);
1171 if (arcmsr_wait_msgint_ready(acb))
1174 "'get adapter firmware miscellaneous data' timeout \n"
1175 , acb->host->host_no);
1178 *acb_firm_model = readb(iop_firm_model);
1185 *acb_firm_version = readb(iop_firm_version);
1191 "ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n"
1192 , acb->host->host_no
1193 , acb->firm_version);
1194 acb->firm_request_len = readl(®->message_rwbuffer[1]);
1195 acb->firm_numbers_queue = readl(®->message_rwbuffer[2]);
1196 acb->firm_sdram_size = readl(®->message_rwbuffer[3]);
1197 acb->firm_hd_channels = readl(®->message_rwbuffer[4]);
1200 static void arcmsr_polling_ccbdone(struct AdapterControlBlock *acb,
1201 struct CommandControlBlock *poll_ccb)
1203 struct MessageUnit __iomem *reg = acb->pmu;
1204 struct CommandControlBlock *ccb;
1205 uint32_t flag_ccb, outbound_intstatus, poll_ccb_done = 0, poll_count = 0;
1210 outbound_intstatus = readl(®->outbound_intstatus)
1211 & acb->outbound_int_enable;
1212 writel(outbound_intstatus, ®->outbound_intstatus);/*clear interrupt*/
1214 if ((flag_ccb = readl(®->outbound_queueport)) == 0xFFFFFFFF) {
1219 if (poll_count > 100)
1221 goto polling_ccb_retry;
1224 ccb = (struct CommandControlBlock *)
1225 (acb->vir2phy_offset + (flag_ccb << 5));
1226 if ((ccb->acb != acb) ||
1227 (ccb->startdone != ARCMSR_CCB_START)) {
1228 if ((ccb->startdone == ARCMSR_CCB_ABORTED) ||
1229 (ccb == poll_ccb)) {
1231 "arcmsr%d: scsi id = %d lun = %d ccb = '0x%p'"
1232 " poll command abort successfully \n"
1233 , acb->host->host_no
1234 , ccb->pcmd->device->id
1235 , ccb->pcmd->device->lun
1237 ccb->pcmd->result = DID_ABORT << 16;
1238 arcmsr_ccb_complete(ccb, 1);
1243 "arcmsr%d: polling get an illegal ccb"
1244 " command done ccb ='0x%p'"
1245 "ccboutstandingcount = %d \n"
1246 , acb->host->host_no
1248 , atomic_read(&acb->ccboutstandingcount));
1251 id = ccb->pcmd->device->id;
1252 lun = ccb->pcmd->device->lun;
1253 if (!(flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR)) {
1254 if (acb->devstate[id][lun] == ARECA_RAID_GONE)
1255 acb->devstate[id][lun] = ARECA_RAID_GOOD;
1256 ccb->pcmd->result = DID_OK << 16;
1257 arcmsr_ccb_complete(ccb, 1);
1259 switch(ccb->arcmsr_cdb.DeviceStatus) {
1260 case ARCMSR_DEV_SELECT_TIMEOUT: {
1261 acb->devstate[id][lun] = ARECA_RAID_GONE;
1262 ccb->pcmd->result = DID_NO_CONNECT << 16;
1263 arcmsr_ccb_complete(ccb, 1);
1266 case ARCMSR_DEV_ABORTED:
1267 case ARCMSR_DEV_INIT_FAIL: {
1268 acb->devstate[id][lun] = ARECA_RAID_GONE;
1269 ccb->pcmd->result = DID_BAD_TARGET << 16;
1270 arcmsr_ccb_complete(ccb, 1);
1273 case ARCMSR_DEV_CHECK_CONDITION: {
1274 acb->devstate[id][lun] = ARECA_RAID_GOOD;
1275 arcmsr_report_sense_info(ccb);
1276 arcmsr_ccb_complete(ccb, 1);
1281 "arcmsr%d: scsi id = %d lun = %d"
1282 " polling and getting command error done"
1283 "but got unknown DeviceStatus = 0x%x \n"
1284 , acb->host->host_no
1287 , ccb->arcmsr_cdb.DeviceStatus);
1288 acb->devstate[id][lun] = ARECA_RAID_GONE;
1289 ccb->pcmd->result = DID_BAD_TARGET << 16;
1290 arcmsr_ccb_complete(ccb, 1);
1296 static void arcmsr_done4_abort_postqueue(struct AdapterControlBlock *acb)
1298 int i = 0, found = 0;
1300 uint32_t flag_ccb, outbound_intstatus;
1301 struct MessageUnit __iomem *reg = acb->pmu;
1302 struct CommandControlBlock *ccb;
1303 /*clear and abort all outbound posted Q*/
1305 while (((flag_ccb = readl(®->outbound_queueport)) != 0xFFFFFFFF) &&
1307 ccb = (struct CommandControlBlock *)(acb->vir2phy_offset +
1310 if ((ccb->acb != acb)||(ccb->startdone != \
1312 printk(KERN_NOTICE "arcmsr%d: polling get \
1313 an illegal ccb" "command done ccb = '0x%p'""ccboutstandingcount = %d \n",
1314 acb->host->host_no, ccb,
1315 atomic_read(&acb->ccboutstandingcount));
1319 id = ccb->pcmd->device->id;
1320 lun = ccb->pcmd->device->lun;
1321 if (!(flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR)){
1322 if (acb->devstate[id][lun] == ARECA_RAID_GONE)
1323 acb->devstate[id][lun] = ARECA_RAID_GOOD;
1324 ccb->pcmd->result = DID_OK << 16;
1325 arcmsr_ccb_complete(ccb, 1);
1328 switch(ccb->arcmsr_cdb.DeviceStatus) {
1329 case ARCMSR_DEV_SELECT_TIMEOUT: {
1330 acb->devstate[id][lun] = ARECA_RAID_GONE;
1331 ccb->pcmd->result = DID_NO_CONNECT << 16;
1332 arcmsr_ccb_complete(ccb, 1);
1336 case ARCMSR_DEV_ABORTED:
1338 case ARCMSR_DEV_INIT_FAIL: {
1339 acb->devstate[id][lun] =
1342 DID_BAD_TARGET << 16;
1343 arcmsr_ccb_complete(ccb, 1);
1347 case ARCMSR_DEV_CHECK_CONDITION: {
1348 acb->devstate[id][lun] =
1350 arcmsr_report_sense_info(ccb);
1351 arcmsr_ccb_complete(ccb, 1);
1357 "arcmsr%d: scsi id = %d \
1358 lun = %d""polling and \
1359 getting command error \
1360 done""but got unknown \
1361 DeviceStatus = 0x%x \n",
1362 acb->host->host_no, id,
1363 lun, ccb->arcmsr_cdb.DeviceStatus);
1364 acb->devstate[id][lun] =
1367 DID_BAD_TARGET << 16;
1368 arcmsr_ccb_complete(ccb, 1);
1376 outbound_intstatus = readl(®->outbound_intstatus) & \
1377 acb->outbound_int_enable;
1378 writel(outbound_intstatus, ®->outbound_intstatus);
1385 static void arcmsr_iop_init(struct AdapterControlBlock *acb)
1387 struct MessageUnit __iomem *reg = acb->pmu;
1388 uint32_t intmask_org, mask, outbound_doorbell, firmware_state = 0;
1391 firmware_state = readl(®->outbound_msgaddr1);
1392 } while (!(firmware_state & ARCMSR_OUTBOUND_MESG1_FIRMWARE_OK));
1393 intmask_org = readl(®->outbound_intmask)
1394 | ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE;
1395 arcmsr_get_firmware_spec(acb);
1397 acb->acb_flags |= ACB_F_MSG_START_BGRB;
1398 writel(ARCMSR_INBOUND_MESG0_START_BGRB, ®->inbound_msgaddr0);
1399 if (arcmsr_wait_msgint_ready(acb)) {
1400 printk(KERN_NOTICE "arcmsr%d: "
1401 "wait 'start adapter background rebulid' timeout\n",
1402 acb->host->host_no);
1405 outbound_doorbell = readl(®->outbound_doorbell);
1406 writel(outbound_doorbell, ®->outbound_doorbell);
1407 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, ®->inbound_doorbell);
1408 mask = ~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE
1409 | ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE);
1410 writel(intmask_org & mask, ®->outbound_intmask);
1411 acb->outbound_int_enable = ~(intmask_org & mask) & 0x000000ff;
1412 acb->acb_flags |= ACB_F_IOP_INITED;
1415 static void arcmsr_iop_reset(struct AdapterControlBlock *acb)
1417 struct CommandControlBlock *ccb;
1418 uint32_t intmask_org;
1421 if (atomic_read(&acb->ccboutstandingcount) != 0) {
1422 /* talk to iop 331 outstanding command aborted */
1423 arcmsr_abort_allcmd(acb);
1424 /* wait for 3 sec for all command aborted*/
1425 msleep_interruptible(3000);
1426 /* disable all outbound interrupt */
1427 intmask_org = arcmsr_disable_outbound_ints(acb);
1428 /* clear all outbound posted Q */
1429 arcmsr_done4_abort_postqueue(acb);
1430 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
1431 ccb = acb->pccb_pool[i];
1432 if (ccb->startdone == ARCMSR_CCB_START) {
1433 ccb->startdone = ARCMSR_CCB_ABORTED;
1436 /* enable all outbound interrupt */
1437 arcmsr_enable_outbound_ints(acb, intmask_org);
1442 static int arcmsr_bus_reset(struct scsi_cmnd *cmd)
1444 struct AdapterControlBlock *acb =
1445 (struct AdapterControlBlock *)cmd->device->host->hostdata;
1449 acb->acb_flags |= ACB_F_BUS_RESET;
1450 for (i = 0; i < 400; i++) {
1451 if (!atomic_read(&acb->ccboutstandingcount))
1453 arcmsr_interrupt(acb);
1456 arcmsr_iop_reset(acb);
1457 acb->acb_flags &= ~ACB_F_BUS_RESET;
1461 static void arcmsr_abort_one_cmd(struct AdapterControlBlock *acb,
1462 struct CommandControlBlock *ccb)
1466 ccb->startdone = ARCMSR_CCB_ABORTED;
1469 ** Wait for 3 sec for all command done.
1471 msleep_interruptible(3000);
1473 intmask = arcmsr_disable_outbound_ints(acb);
1474 arcmsr_polling_ccbdone(acb, ccb);
1475 arcmsr_enable_outbound_ints(acb, intmask);
1478 static int arcmsr_abort(struct scsi_cmnd *cmd)
1480 struct AdapterControlBlock *acb =
1481 (struct AdapterControlBlock *)cmd->device->host->hostdata;
1485 "arcmsr%d: abort device command of scsi id = %d lun = %d \n",
1486 acb->host->host_no, cmd->device->id, cmd->device->lun);
1489 ************************************************
1490 ** the all interrupt service routine is locked
1491 ** we need to handle it as soon as possible and exit
1492 ************************************************
1494 if (!atomic_read(&acb->ccboutstandingcount))
1497 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
1498 struct CommandControlBlock *ccb = acb->pccb_pool[i];
1499 if (ccb->startdone == ARCMSR_CCB_START && ccb->pcmd == cmd) {
1500 arcmsr_abort_one_cmd(acb, ccb);
1508 static const char *arcmsr_info(struct Scsi_Host *host)
1510 struct AdapterControlBlock *acb =
1511 (struct AdapterControlBlock *) host->hostdata;
1512 static char buf[256];
1516 switch (acb->pdev->device) {
1517 case PCI_DEVICE_ID_ARECA_1110:
1518 case PCI_DEVICE_ID_ARECA_1210:
1521 case PCI_DEVICE_ID_ARECA_1120:
1522 case PCI_DEVICE_ID_ARECA_1130:
1523 case PCI_DEVICE_ID_ARECA_1160:
1524 case PCI_DEVICE_ID_ARECA_1170:
1525 case PCI_DEVICE_ID_ARECA_1220:
1526 case PCI_DEVICE_ID_ARECA_1230:
1527 case PCI_DEVICE_ID_ARECA_1260:
1528 case PCI_DEVICE_ID_ARECA_1270:
1529 case PCI_DEVICE_ID_ARECA_1280:
1532 case PCI_DEVICE_ID_ARECA_1380:
1533 case PCI_DEVICE_ID_ARECA_1381:
1534 case PCI_DEVICE_ID_ARECA_1680:
1535 case PCI_DEVICE_ID_ARECA_1681:
1542 sprintf(buf, "Areca %s Host Adapter RAID Controller%s\n %s",
1543 type, raid6 ? "( RAID6 capable)" : "",
1544 ARCMSR_DRIVER_VERSION);
1548 static pci_ers_result_t arcmsr_pci_slot_reset(struct pci_dev *pdev)
1550 struct Scsi_Host *host;
1551 struct AdapterControlBlock *acb;
1552 uint8_t bus, dev_fun;
1555 error = pci_enable_device(pdev);
1557 return PCI_ERS_RESULT_DISCONNECT;
1558 pci_set_master(pdev);
1560 host = scsi_host_alloc(&arcmsr_scsi_host_template, sizeof \
1561 (struct AdapterControlBlock));
1563 return PCI_ERS_RESULT_DISCONNECT;
1564 acb = (struct AdapterControlBlock *)host->hostdata;
1565 memset(acb, 0, sizeof (struct AdapterControlBlock));
1567 error = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
1569 error = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1572 "scsi%d: No suitable DMA mask available\n",
1574 return PCI_ERS_RESULT_DISCONNECT;
1577 bus = pdev->bus->number;
1578 dev_fun = pdev->devfn;
1579 acb = (struct AdapterControlBlock *) host->hostdata;
1580 memset(acb, 0, sizeof(struct AdapterControlBlock));
1583 host->max_sectors = ARCMSR_MAX_XFER_SECTORS;
1584 host->max_lun = ARCMSR_MAX_TARGETLUN;
1585 host->max_id = ARCMSR_MAX_TARGETID;/*16:8*/
1586 host->max_cmd_len = 16; /*this is issue of 64bit LBA, over 2T byte*/
1587 host->sg_tablesize = ARCMSR_MAX_SG_ENTRIES;
1588 host->can_queue = ARCMSR_MAX_FREECCB_NUM; /* max simultaneous cmds */
1589 host->cmd_per_lun = ARCMSR_MAX_CMD_PERLUN;
1590 host->this_id = ARCMSR_SCSI_INITIATOR_ID;
1591 host->unique_id = (bus << 8) | dev_fun;
1592 host->irq = pdev->irq;
1593 error = pci_request_regions(pdev, "arcmsr");
1595 return PCI_ERS_RESULT_DISCONNECT;
1597 acb->pmu = ioremap(pci_resource_start(pdev, 0),
1598 pci_resource_len(pdev, 0));
1600 printk(KERN_NOTICE "arcmsr%d: memory"
1601 " mapping region fail \n", acb->host->host_no);
1602 return PCI_ERS_RESULT_DISCONNECT;
1604 acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
1605 ACB_F_MESSAGE_RQBUFFER_CLEARED |
1606 ACB_F_MESSAGE_WQBUFFER_READED);
1607 acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
1608 INIT_LIST_HEAD(&acb->ccb_free_list);
1610 error = arcmsr_alloc_ccb_pool(acb);
1612 return PCI_ERS_RESULT_DISCONNECT;
1614 error = request_irq(pdev->irq, arcmsr_do_interrupt,
1615 IRQF_DISABLED | IRQF_SHARED, "arcmsr", acb);
1617 return PCI_ERS_RESULT_DISCONNECT;
1619 arcmsr_iop_init(acb);
1620 if (strncmp(acb->firm_version, "V1.42", 5) >= 0)
1621 host->max_sectors = ARCMSR_MAX_XFER_SECTORS_B;
1623 pci_set_drvdata(pdev, host);
1625 error = scsi_add_host(host, &pdev->dev);
1627 return PCI_ERS_RESULT_DISCONNECT;
1629 error = arcmsr_alloc_sysfs_attr(acb);
1631 return PCI_ERS_RESULT_DISCONNECT;
1633 scsi_scan_host(host);
1634 return PCI_ERS_RESULT_RECOVERED;
1637 static void arcmsr_pci_ers_need_reset_forepart(struct pci_dev *pdev)
1639 struct Scsi_Host *host = pci_get_drvdata(pdev);
1640 struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
1641 struct MessageUnit __iomem *reg = acb->pmu;
1642 struct CommandControlBlock *ccb;
1643 /*clear and abort all outbound posted Q*/
1644 int i = 0, found = 0;
1646 uint32_t flag_ccb, outbound_intstatus;
1648 while (((flag_ccb = readl(®->outbound_queueport)) != 0xFFFFFFFF) &&
1650 ccb = (struct CommandControlBlock *)(acb->vir2phy_offset
1653 if ((ccb->acb != acb)||(ccb->startdone !=
1655 printk(KERN_NOTICE "arcmsr%d: polling \
1656 get an illegal ccb"" command done ccb = '0x%p'"
1657 "ccboutstandingcount = %d \n",
1658 acb->host->host_no, ccb,
1659 atomic_read(&acb->ccboutstandingcount));
1663 id = ccb->pcmd->device->id;
1664 lun = ccb->pcmd->device->lun;
1665 if (!(flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR)) {
1666 if (acb->devstate[id][lun] ==
1668 acb->devstate[id][lun] =
1670 ccb->pcmd->result = DID_OK << 16;
1671 arcmsr_ccb_complete(ccb, 1);
1674 switch(ccb->arcmsr_cdb.DeviceStatus) {
1675 case ARCMSR_DEV_SELECT_TIMEOUT: {
1676 acb->devstate[id][lun] =
1679 DID_NO_CONNECT << 16;
1680 arcmsr_ccb_complete(ccb, 1);
1684 case ARCMSR_DEV_ABORTED:
1686 case ARCMSR_DEV_INIT_FAIL: {
1687 acb->devstate[id][lun] =
1690 DID_BAD_TARGET << 16;
1691 arcmsr_ccb_complete(ccb, 1);
1695 case ARCMSR_DEV_CHECK_CONDITION: {
1696 acb->devstate[id][lun] =
1698 arcmsr_report_sense_info(ccb);
1699 arcmsr_ccb_complete(ccb, 1);
1711 DeviceStatus = 0x%x \n"
1712 , acb->host->host_no,
1714 ccb->arcmsr_cdb.DeviceStatus);
1715 acb->devstate[id][lun] =
1718 DID_BAD_TARGET << 16;
1719 arcmsr_ccb_complete(ccb, 1);
1727 outbound_intstatus = readl(®->outbound_intstatus) &
1728 acb->outbound_int_enable;
1729 writel(outbound_intstatus, ®->outbound_intstatus);
1736 static void arcmsr_pci_ers_disconnect_forepart(struct pci_dev *pdev)
1738 struct Scsi_Host *host = pci_get_drvdata(pdev);
1739 struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
1740 struct MessageUnit __iomem *reg = acb->pmu;
1741 struct CommandControlBlock *ccb;
1742 /*clear and abort all outbound posted Q*/
1743 int i = 0, found = 0;
1745 uint32_t flag_ccb, outbound_intstatus;
1747 while (((flag_ccb = readl(®->outbound_queueport)) != 0xFFFFFFFF) &&
1749 ccb = (struct CommandControlBlock *)(acb->vir2phy_offset +
1752 if ((ccb->acb != acb)||(ccb->startdone !=
1755 "arcmsr%d: polling get an illegal ccb"
1756 " command done ccb = '0x%p'"
1757 "ccboutstandingcount = %d \n",
1758 acb->host->host_no, ccb,
1759 atomic_read(&acb->ccboutstandingcount));
1763 id = ccb->pcmd->device->id;
1764 lun = ccb->pcmd->device->lun;
1765 if (!(flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR)) {
1766 if (acb->devstate[id][lun] == ARECA_RAID_GONE)
1767 acb->devstate[id][lun] = ARECA_RAID_GOOD;
1768 ccb->pcmd->result = DID_OK << 16;
1769 arcmsr_ccb_complete(ccb, 1);
1772 switch(ccb->arcmsr_cdb.DeviceStatus) {
1773 case ARCMSR_DEV_SELECT_TIMEOUT: {
1774 acb->devstate[id][lun] =
1777 DID_NO_CONNECT << 16;
1778 arcmsr_ccb_complete(ccb, 1);
1782 case ARCMSR_DEV_ABORTED:
1784 case ARCMSR_DEV_INIT_FAIL: {
1785 acb->devstate[id][lun] =
1788 DID_BAD_TARGET << 16;
1789 arcmsr_ccb_complete(ccb, 1);
1793 case ARCMSR_DEV_CHECK_CONDITION: {
1794 acb->devstate[id][lun] =
1796 arcmsr_report_sense_info(ccb);
1797 arcmsr_ccb_complete(ccb, 1);
1802 printk(KERN_NOTICE "arcmsr%d: \
1803 scsi id = %d lun = %d"
1805 getting command error done"
1807 DeviceStatus = 0x%x \n"
1808 , acb->host->host_no,
1809 id, lun, ccb->arcmsr_cdb.DeviceStatus);
1810 acb->devstate[id][lun] =
1813 DID_BAD_TARGET << 16;
1814 arcmsr_ccb_complete(ccb, 1);
1822 outbound_intstatus = readl(®->outbound_intstatus) &
1823 acb->outbound_int_enable;
1824 writel(outbound_intstatus, ®->outbound_intstatus);
1830 static pci_ers_result_t arcmsr_pci_error_detected(struct pci_dev *pdev,
1831 pci_channel_state_t state)
1834 case pci_channel_io_frozen:
1835 arcmsr_pci_ers_need_reset_forepart(pdev);
1836 return PCI_ERS_RESULT_NEED_RESET;
1837 case pci_channel_io_perm_failure:
1838 arcmsr_pci_ers_disconnect_forepart(pdev);
1839 return PCI_ERS_RESULT_DISCONNECT;
1842 return PCI_ERS_RESULT_NEED_RESET;