Pull throttle into release branch
[linux-2.6] / drivers / scsi / arcmsr / arcmsr_hba.c
1 /*
2 *******************************************************************************
3 **        O.S   : Linux
4 **   FILE NAME  : arcmsr_hba.c
5 **        BY    : Erich Chen
6 **   Description: SCSI RAID Device Driver for
7 **                ARECA RAID Host adapter
8 *******************************************************************************
9 ** Copyright (C) 2002 - 2005, Areca Technology Corporation All rights reserved
10 **
11 **     Web site: www.areca.com.tw
12 **       E-mail: erich@areca.com.tw
13 **
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
24 ** are met:
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.
32 **
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 *******************************************************************************
47 */
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>
61 #include <asm/dma.h>
62 #include <asm/io.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>
72 #include "arcmsr.h"
73
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);
78
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)
101 {
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);
105         return queue_depth;
106 }
107
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,
112         .info                   = arcmsr_info,
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,
125 };
126 static struct pci_error_handlers arcmsr_pci_error_handlers = {
127         .error_detected         = arcmsr_pci_error_detected,
128         .slot_reset             = arcmsr_pci_slot_reset,
129 };
130
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 */
148 };
149 MODULE_DEVICE_TABLE(pci, arcmsr_device_id_table);
150 static struct pci_driver arcmsr_pci_driver = {
151         .name                   = "arcmsr",
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,
157 };
158
159 static irqreturn_t arcmsr_do_interrupt(int irq, void *dev_id)
160 {
161         irqreturn_t handle_state;
162         struct AdapterControlBlock *acb;
163         unsigned long flags;
164
165         acb = (struct AdapterControlBlock *)dev_id;
166
167         spin_lock_irqsave(acb->host->host_lock, flags);
168         handle_state = arcmsr_interrupt(acb);
169         spin_unlock_irqrestore(acb->host->host_lock, flags);
170         return handle_state;
171 }
172
173 static int arcmsr_bios_param(struct scsi_device *sdev,
174                 struct block_device *bdev, sector_t capacity, int *geom)
175 {
176         int ret, heads, sectors, cylinders, total_capacity;
177         unsigned char *buffer;/* return copy of block device's partition table */
178
179         buffer = scsi_bios_ptable(bdev);
180         if (buffer) {
181                 ret = scsi_partsize(buffer, capacity, &geom[2], &geom[0], &geom[1]);
182                 kfree(buffer);
183                 if (ret != -1)
184                         return ret;
185         }
186         total_capacity = capacity;
187         heads = 64;
188         sectors = 32;
189         cylinders = total_capacity / (heads * sectors);
190         if (cylinders > 1024) {
191                 heads = 255;
192                 sectors = 63;
193                 cylinders = total_capacity / (heads * sectors);
194         }
195         geom[0] = heads;
196         geom[1] = sectors;
197         geom[2] = cylinders;
198         return 0;
199 }
200
201 static int arcmsr_alloc_ccb_pool(struct AdapterControlBlock *acb)
202 {
203         struct pci_dev *pdev = acb->pdev;
204         struct MessageUnit __iomem *reg = acb->pmu;
205         u32 ccb_phyaddr_hi32;
206         void *dma_coherent;
207         dma_addr_t dma_coherent_handle, dma_addr;
208         struct CommandControlBlock *ccb_tmp;
209         int i, j;
210
211         dma_coherent = dma_alloc_coherent(&pdev->dev,
212                         ARCMSR_MAX_FREECCB_NUM *
213                         sizeof (struct CommandControlBlock) + 0x20,
214                         &dma_coherent_handle, GFP_KERNEL);
215         if (!dma_coherent)
216                 return -ENOMEM;
217
218         acb->dma_coherent = dma_coherent;
219         acb->dma_coherent_handle = dma_coherent_handle;
220
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));
226         }
227
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;
232                 ccb_tmp->acb = acb;
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);
236                 ccb_tmp++;
237         }
238
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;
244
245         /*
246         ** here we need to tell iop 331 our ccb_tmp.HighPart
247         ** if ccb_tmp.HighPart is not zero
248         */
249         ccb_phyaddr_hi32 = (uint32_t) ((dma_coherent_handle >> 16) >> 16);
250         if (ccb_phyaddr_hi32 != 0) {
251                 writel(ARCMSR_SIGNATURE_SET_CONFIG, &reg->message_rwbuffer[0]);
252                 writel(ccb_phyaddr_hi32, &reg->message_rwbuffer[1]);
253                 writel(ARCMSR_INBOUND_MESG0_SET_CONFIG, &reg->inbound_msgaddr0);
254                 if (arcmsr_wait_msgint_ready(acb))
255                         printk(KERN_NOTICE "arcmsr%d: "
256                                "'set ccb high part physical address' timeout\n",
257                                 acb->host->host_no);
258         }
259
260         writel(readl(&reg->outbound_intmask) |
261                         ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE,
262                &reg->outbound_intmask);
263         return 0;
264 }
265
266 static int arcmsr_probe(struct pci_dev *pdev,
267         const struct pci_device_id *id)
268 {
269         struct Scsi_Host *host;
270         struct AdapterControlBlock *acb;
271         uint8_t bus, dev_fun;
272         int error;
273
274         error = pci_enable_device(pdev);
275         if (error)
276                 goto out;
277         pci_set_master(pdev);
278
279         host = scsi_host_alloc(&arcmsr_scsi_host_template,
280                         sizeof(struct AdapterControlBlock));
281         if (!host) {
282                 error = -ENOMEM;
283                 goto out_disable_device;
284         }
285         acb = (struct AdapterControlBlock *)host->hostdata;
286         memset(acb, 0, sizeof (struct AdapterControlBlock));
287
288         error = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
289         if (error) {
290                 error = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
291                 if (error) {
292                         printk(KERN_WARNING
293                                "scsi%d: No suitable DMA mask available\n",
294                                host->host_no);
295                         goto out_host_put;
296                 }
297         }
298         bus = pdev->bus->number;
299         dev_fun = pdev->devfn;
300         acb->host = host;
301         acb->pdev = pdev;
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");
313         if (error)
314                 goto out_host_put;
315
316         acb->pmu = ioremap(pci_resource_start(pdev, 0),
317                            pci_resource_len(pdev, 0));
318         if (!acb->pmu) {
319                 printk(KERN_NOTICE "arcmsr%d: memory"
320                         " mapping region fail \n", acb->host->host_no);
321                 goto out_release_regions;
322         }
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);
328
329         error = arcmsr_alloc_ccb_pool(acb);
330         if (error)
331                 goto out_iounmap;
332
333         error = request_irq(pdev->irq, arcmsr_do_interrupt,
334                         IRQF_DISABLED | IRQF_SHARED, "arcmsr", acb);
335         if (error)
336                 goto out_free_ccb_pool;
337
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;
342
343         error = scsi_add_host(host, &pdev->dev);
344         if (error)
345                 goto out_free_irq;
346
347         error = arcmsr_alloc_sysfs_attr(acb);
348         if (error)
349                 goto out_free_sysfs;
350
351         scsi_scan_host(host);
352         pci_enable_pcie_error_reporting(pdev);
353         return 0;
354  out_free_sysfs:
355  out_free_irq:
356         free_irq(pdev->irq, acb);
357  out_free_ccb_pool:
358         arcmsr_free_ccb_pool(acb);
359  out_iounmap:
360         iounmap(acb->pmu);
361  out_release_regions:
362         pci_release_regions(pdev);
363  out_host_put:
364         scsi_host_put(host);
365  out_disable_device:
366         pci_disable_device(pdev);
367  out:
368         return error;
369 }
370
371 static void arcmsr_abort_allcmd(struct AdapterControlBlock *acb)
372 {
373         struct MessageUnit __iomem *reg = acb->pmu;
374
375         writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, &reg->inbound_msgaddr0);
376         if (arcmsr_wait_msgint_ready(acb))
377                 printk(KERN_NOTICE
378                         "arcmsr%d: wait 'abort all outstanding command' timeout \n"
379                         , acb->host->host_no);
380 }
381
382 static void arcmsr_pci_unmap_dma(struct CommandControlBlock *ccb)
383 {
384         struct scsi_cmnd *pcmd = ccb->pcmd;
385
386         scsi_dma_unmap(pcmd);
387 }
388
389 static void arcmsr_ccb_complete(struct CommandControlBlock *ccb, int stand_flag)
390 {
391         struct AdapterControlBlock *acb = ccb->acb;
392         struct scsi_cmnd *pcmd = ccb->pcmd;
393
394         arcmsr_pci_unmap_dma(ccb);
395         if (stand_flag == 1)
396                 atomic_dec(&acb->ccboutstandingcount);
397         ccb->startdone = ARCMSR_CCB_DONE;
398         ccb->ccb_flags = 0;
399         list_add_tail(&ccb->list, &acb->ccb_free_list);
400         pcmd->scsi_done(pcmd);
401 }
402
403 static void arcmsr_remove(struct pci_dev *pdev)
404 {
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;
409         int poll_count = 0;
410
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(&reg->outbound_intmask) |
416                 ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE,
417                 &reg->outbound_intmask);
418         acb->acb_flags |= ACB_F_SCSISTOPADAPTER;
419         acb->acb_flags &= ~ACB_F_IOP_INITED;
420
421         for (poll_count = 0; poll_count < 256; poll_count++) {
422                 if (!atomic_read(&acb->ccboutstandingcount))
423                         break;
424                 arcmsr_interrupt(acb);
425                 msleep(25);
426         }
427
428         if (atomic_read(&acb->ccboutstandingcount)) {
429                 int i;
430
431                 arcmsr_abort_allcmd(acb);
432                 for (i = 0; i < ARCMSR_MAX_OUTSTANDING_CMD; i++)
433                         readl(&reg->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);
440                         }
441                 }
442         }
443
444         free_irq(pdev->irq, acb);
445         iounmap(acb->pmu);
446         arcmsr_free_ccb_pool(acb);
447         pci_release_regions(pdev);
448
449         scsi_host_put(host);
450
451         pci_disable_device(pdev);
452         pci_set_drvdata(pdev, NULL);
453 }
454
455 static void arcmsr_shutdown(struct pci_dev *pdev)
456 {
457         struct Scsi_Host *host = pci_get_drvdata(pdev);
458         struct AdapterControlBlock *acb =
459                 (struct AdapterControlBlock *)host->hostdata;
460
461         arcmsr_stop_adapter_bgrb(acb);
462         arcmsr_flush_adapter_cache(acb);
463 }
464
465 static int arcmsr_module_init(void)
466 {
467         int error = 0;
468
469         error = pci_register_driver(&arcmsr_pci_driver);
470         return error;
471 }
472
473 static void arcmsr_module_exit(void)
474 {
475         pci_unregister_driver(&arcmsr_pci_driver);
476 }
477 module_init(arcmsr_module_init);
478 module_exit(arcmsr_module_exit);
479
480 static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb)
481 {
482         struct MessageUnit __iomem *reg = acb->pmu;
483         u32 orig_mask = readl(&reg->outbound_intmask);
484
485         writel(orig_mask | ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE,
486                         &reg->outbound_intmask);
487         return orig_mask;
488 }
489
490 static void arcmsr_enable_outbound_ints(struct AdapterControlBlock *acb,
491                 u32 orig_mask)
492 {
493         struct MessageUnit __iomem *reg = acb->pmu;
494         u32 mask;
495
496         mask = orig_mask & ~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE |
497                              ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE);
498         writel(mask, &reg->outbound_intmask);
499 }
500
501 static void arcmsr_flush_adapter_cache(struct AdapterControlBlock *acb)
502 {
503         struct MessageUnit __iomem *reg = acb->pmu;
504
505         writel(ARCMSR_INBOUND_MESG0_FLUSH_CACHE, &reg->inbound_msgaddr0);
506         if (arcmsr_wait_msgint_ready(acb))
507                 printk(KERN_NOTICE
508                         "arcmsr%d: wait 'flush adapter cache' timeout \n"
509                         , acb->host->host_no);
510 }
511
512 static void arcmsr_report_sense_info(struct CommandControlBlock *ccb)
513 {
514         struct scsi_cmnd *pcmd = ccb->pcmd;
515         struct SENSE_DATA *sensebuffer = (struct SENSE_DATA *)pcmd->sense_buffer;
516
517         pcmd->result = DID_OK << 16;
518         if (sensebuffer) {
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;
526         }
527 }
528
529 static uint8_t arcmsr_wait_msgint_ready(struct AdapterControlBlock *acb)
530 {
531         struct MessageUnit __iomem *reg = acb->pmu;
532         uint32_t Index;
533         uint8_t Retries = 0x00;
534
535         do {
536                 for (Index = 0; Index < 100; Index++) {
537                         if (readl(&reg->outbound_intstatus)
538                                 & ARCMSR_MU_OUTBOUND_MESSAGE0_INT) {
539                                 writel(ARCMSR_MU_OUTBOUND_MESSAGE0_INT
540                                         , &reg->outbound_intstatus);
541                                 return 0x00;
542                         }
543                         msleep_interruptible(10);
544                 }/*max 1 seconds*/
545         } while (Retries++ < 20);/*max 20 sec*/
546         return 0xff;
547 }
548
549 static void arcmsr_build_ccb(struct AdapterControlBlock *acb,
550         struct CommandControlBlock *ccb, struct scsi_cmnd *pcmd)
551 {
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;
556         int nseg;
557
558         ccb->pcmd = pcmd;
559         memset(arcmsr_cdb, 0, sizeof (struct ARCMSR_CDB));
560         arcmsr_cdb->Bus = 0;
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);
567
568         nseg = scsi_dma_map(pcmd);
569         BUG_ON(nseg < 0);
570
571         if (nseg) {
572                 int length, i, cdb_sgcount = 0;
573                 struct scatterlist *sg;
574
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;
583
584                                 pdma_sg->address = address_lo;
585                                 pdma_sg->length = length;
586                                 psge += sizeof (struct SG32ENTRY);
587                                 arccdbsize += sizeof (struct SG32ENTRY);
588                         } else {
589                                 struct SG64ENTRY *pdma_sg = (struct SG64ENTRY *)psge;
590
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);
596                         }
597                         cdb_sgcount++;
598                 }
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;
603         }
604         if (pcmd->sc_data_direction == DMA_TO_DEVICE ) {
605                 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_WRITE;
606                 ccb->ccb_flags |= CCB_FLAG_WRITE;
607         }
608 }
609
610 static void arcmsr_post_ccb(struct AdapterControlBlock *acb, struct CommandControlBlock *ccb)
611 {
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;
615
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                         &reg->inbound_queueport);
621         else
622                 writel(cdb_shifted_phyaddr, &reg->inbound_queueport);
623 }
624
625 void arcmsr_post_Qbuffer(struct AdapterControlBlock *acb)
626 {
627         struct MessageUnit __iomem *reg = acb->pmu;
628         struct QBUFFER __iomem *pwbuffer = (struct QBUFFER __iomem *) &reg->message_wbuffer;
629         uint8_t __iomem *iop_data = (uint8_t __iomem *) pwbuffer->data;
630         int32_t allxfer_len = 0;
631
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;
639                         iop_data++;
640                         allxfer_len++;
641                 }
642                 writel(allxfer_len, &pwbuffer->data_len);
643                 writel(ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK
644                         , &reg->inbound_doorbell);
645         }
646 }
647
648 static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb)
649 {
650         struct MessageUnit __iomem *reg = acb->pmu;
651
652         acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
653         writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, &reg->inbound_msgaddr0);
654         if (arcmsr_wait_msgint_ready(acb))
655                 printk(KERN_NOTICE
656                         "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
657                         , acb->host->host_no);
658 }
659
660 static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb)
661 {
662         dma_free_coherent(&acb->pdev->dev,
663                 ARCMSR_MAX_FREECCB_NUM * sizeof (struct CommandControlBlock) + 0x20,
664                 acb->dma_coherent,
665                 acb->dma_coherent_handle);
666 }
667
668 static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb)
669 {
670         struct MessageUnit __iomem *reg = acb->pmu;
671         struct CommandControlBlock *ccb;
672         uint32_t flag_ccb, outbound_intstatus, outbound_doorbell;
673
674         outbound_intstatus = readl(&reg->outbound_intstatus)
675                 & acb->outbound_int_enable;
676         writel(outbound_intstatus, &reg->outbound_intstatus);
677         if (outbound_intstatus & ARCMSR_MU_OUTBOUND_DOORBELL_INT) {
678                 outbound_doorbell = readl(&reg->outbound_doorbell);
679                 writel(outbound_doorbell, &reg->outbound_doorbell);
680                 if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK) {
681                         struct QBUFFER __iomem * prbuffer =
682                                 (struct QBUFFER __iomem *) &reg->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;
685
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;
696                                         iop_data++;
697                                         iop_len--;
698                                 }
699                                 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK,
700                                         &reg->inbound_doorbell);
701                         } else
702                                 acb->acb_flags |= ACB_F_IOPDATA_OVERFLOW;
703                 }
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 *) &reg->message_wbuffer;
709                                 uint8_t __iomem * iop_data = (uint8_t __iomem *) pwbuffer->data;
710                                 int32_t allxfer_len = 0;
711
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;
718                                         iop_data++;
719                                         allxfer_len++;
720                                 }
721                                 writel(allxfer_len, &pwbuffer->data_len);
722                                 writel(ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK,
723                                         &reg->inbound_doorbell);
724                         }
725                         if (acb->wqbuf_firstindex == acb->wqbuf_lastindex)
726                                 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_CLEARED;
727                 }
728         }
729         if (outbound_intstatus & ARCMSR_MU_OUTBOUND_POSTQUEUE_INT) {
730                 int id, lun;
731                 /*
732                 ****************************************************************
733                 **            areca cdb command done
734                 ****************************************************************
735                 */
736                 while (1) {
737                         if ((flag_ccb = readl(&reg->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 +
741                                 (flag_ccb << 5));
742                         if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
743                                 if (ccb->startdone == ARCMSR_CCB_ABORTED) {
744                                         struct scsi_cmnd *abortcmd = ccb->pcmd;
745                                         if (abortcmd) {
746                                         abortcmd->result |= DID_ABORT >> 16;
747                                         arcmsr_ccb_complete(ccb, 1);
748                                         printk(KERN_NOTICE
749                                                 "arcmsr%d: ccb ='0x%p' isr got aborted command \n"
750                                                 , acb->host->host_no, ccb);
751                                         }
752                                         continue;
753                                 }
754                                 printk(KERN_NOTICE
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"
758                                         , acb->host->host_no
759                                         , acb
760                                         , ccb
761                                         , ccb->acb
762                                         , ccb->startdone
763                                         , atomic_read(&acb->ccboutstandingcount));
764                                 continue;
765                         }
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);
773                         } else {
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);
779                                         }
780                                         break;
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);
786                                         }
787                                         break;
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);
792                                         }
793                                         break;
794                                 default:
795                                         printk(KERN_NOTICE
796                                                 "arcmsr%d: scsi id = %d lun = %d"
797                                                 " isr get command error done, "
798                                                 "but got unknown DeviceStatus = 0x%x \n"
799                                                 , acb->host->host_no
800                                                 , id
801                                                 , lun
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);
806                                         break;
807                                 }
808                         }
809                 }/*drain reply FIFO*/
810         }
811         if (!(outbound_intstatus & ARCMSR_MU_OUTBOUND_HANDLE_INT))
812                 return IRQ_NONE;
813         return IRQ_HANDLED;
814 }
815
816 static void arcmsr_iop_parking(struct AdapterControlBlock *acb)
817 {
818         if (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);
824                 }
825         }
826 }
827
828 static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb, struct scsi_cmnd *cmd)
829 {
830         struct MessageUnit __iomem *reg = acb->pmu;
831         struct CMD_MESSAGE_FIELD *pcmdmessagefld;
832         int retvalue = 0, transfer_len = 0;
833         char *buffer;
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 */
840
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;
845                 goto message_out;
846         }
847         transfer_len += sg->length;
848
849         if (transfer_len > sizeof(struct CMD_MESSAGE_FIELD)) {
850                 retvalue = ARCMSR_MESSAGE_FAIL;
851                 goto message_out;
852         }
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;
860
861                         ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
862                         if (!ver_addr) {
863                                 retvalue = ARCMSR_MESSAGE_FAIL;
864                                 goto message_out;
865                         }
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;
873                                 ptmpQbuffer++;
874                                 allxfer_len++;
875                         }
876                         if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
877                                 struct QBUFFER __iomem * prbuffer = (struct QBUFFER __iomem *)
878                                                         &reg->message_rbuffer;
879                                 uint8_t __iomem * iop_data = (uint8_t __iomem *)prbuffer->data;
880                                 int32_t iop_len;
881
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;
888                                         iop_data++;
889                                         iop_len--;
890                                 }
891                                 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK,
892                                                 &reg->inbound_doorbell);
893                         }
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);
899                 }
900                 break;
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;
906
907                         ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
908                         if (!ver_addr) {
909                                 retvalue = ARCMSR_MESSAGE_FAIL;
910                                 goto message_out;
911                         }
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;
928                         } else {
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) {
933                                                 pQbuffer =
934                                                 &acb->wqbuffer[acb->wqbuf_lastindex];
935                                                 memcpy(pQbuffer, ptmpuserbuffer, 1);
936                                                 acb->wqbuf_lastindex++;
937                                                 acb->wqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
938                                                 ptmpuserbuffer++;
939                                                 user_len--;
940                                         }
941                                         if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {
942                                                 acb->acb_flags &=
943                                                         ~ACB_F_MESSAGE_WQBUFFER_CLEARED;
944                                                 arcmsr_post_Qbuffer(acb);
945                                         }
946                                 } else {
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;
956                                 }
957                         }
958                         pci_free_consistent(acb->pdev, 1032, ver_addr, buf_handle);
959                 }
960                 break;
961         case ARCMSR_MESSAGE_CLEAR_RQBUFFER: {
962                         uint8_t *pQbuffer = acb->rqbuffer;
963
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                                         &reg->inbound_doorbell);
968                         }
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;
975                 }
976                 break;
977         case ARCMSR_MESSAGE_CLEAR_WQBUFFER: {
978                         uint8_t *pQbuffer = acb->wqbuffer;
979
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                                                 , &reg->inbound_doorbell);
984                         }
985                         acb->acb_flags |=
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;
993                 }
994                 break;
995         case ARCMSR_MESSAGE_CLEAR_ALLQBUFFER: {
996                         uint8_t *pQbuffer;
997
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                                                 , &reg->inbound_doorbell);
1002                         }
1003                         acb->acb_flags |=
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;
1016                 }
1017                 break;
1018         case ARCMSR_MESSAGE_RETURN_CODE_3F: {
1019                         pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_3F;
1020                 }
1021                 break;
1022         case ARCMSR_MESSAGE_SAY_HELLO: {
1023                         int8_t * hello_string = "Hello! I am ARCMSR";
1024
1025                         memcpy(pcmdmessagefld->messagedatabuffer, hello_string
1026                                 , (int16_t)strlen(hello_string));
1027                         pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1028                 }
1029                 break;
1030         case ARCMSR_MESSAGE_SAY_GOODBYE:
1031                 arcmsr_iop_parking(acb);
1032                 break;
1033         case ARCMSR_MESSAGE_FLUSH_ADAPTER_CACHE:
1034                 arcmsr_flush_adapter_cache(acb);
1035                 break;
1036         default:
1037                 retvalue = ARCMSR_MESSAGE_FAIL;
1038         }
1039  message_out:
1040         sg = scsi_sglist(cmd);
1041         kunmap_atomic(buffer - sg->offset, KM_IRQ0);
1042
1043         return retvalue;
1044 }
1045
1046 static struct CommandControlBlock *arcmsr_get_freeccb(struct AdapterControlBlock *acb)
1047 {
1048         struct list_head *head = &acb->ccb_free_list;
1049         struct CommandControlBlock *ccb = NULL;
1050
1051         if (!list_empty(head)) {
1052                 ccb = list_entry(head->next, struct CommandControlBlock, list);
1053                 list_del(head->next);
1054         }
1055         return ccb;
1056 }
1057
1058 static void arcmsr_handle_virtual_command(struct AdapterControlBlock *acb,
1059                 struct scsi_cmnd *cmd)
1060 {
1061         switch (cmd->cmnd[0]) {
1062         case INQUIRY: {
1063                 unsigned char inqdata[36];
1064                 char *buffer;
1065                 struct scatterlist *sg;
1066
1067                 if (cmd->device->lun) {
1068                         cmd->result = (DID_TIME_OUT << 16);
1069                         cmd->scsi_done(cmd);
1070                         return;
1071                 }
1072                 inqdata[0] = TYPE_PROCESSOR;
1073                 /* Periph Qualifier & Periph Dev Type */
1074                 inqdata[1] = 0;
1075                 /* rem media bit & Dev Type Modifier */
1076                 inqdata[2] = 0;
1077                 /* ISO, ECMA, & ANSI versions */
1078                 inqdata[4] = 31;
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 */
1085
1086                 sg = scsi_sglist(cmd);
1087                 buffer = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
1088
1089                 memcpy(buffer, inqdata, sizeof(inqdata));
1090                 sg = scsi_sglist(cmd);
1091                 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
1092
1093                 cmd->scsi_done(cmd);
1094         }
1095         break;
1096         case WRITE_BUFFER:
1097         case READ_BUFFER: {
1098                 if (arcmsr_iop_message_xfer(acb, cmd))
1099                         cmd->result = (DID_ERROR << 16);
1100                 cmd->scsi_done(cmd);
1101         }
1102         break;
1103         default:
1104                 cmd->scsi_done(cmd);
1105         }
1106 }
1107
1108 static int arcmsr_queue_command(struct scsi_cmnd *cmd,
1109         void (* done)(struct scsi_cmnd *))
1110 {
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;
1117
1118         cmd->scsi_done = done;
1119         cmd->host_scribble = NULL;
1120         cmd->result = 0;
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;
1126         }
1127         if (target == 16) {
1128                 /* virtual device for iop message transfer */
1129                 arcmsr_handle_virtual_command(acb, cmd);
1130                 return 0;
1131         }
1132         if (acb->devstate[target][lun] == ARECA_RAID_GONE) {
1133                 uint8_t block_cmd;
1134
1135                 block_cmd = cmd->cmnd[0] & 0x0f;
1136                 if (block_cmd == 0x08 || block_cmd == 0x0a) {
1137                         printk(KERN_NOTICE
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
1142                                 , cmd->cmnd[0]
1143                                 , target, lun);
1144                         cmd->result = (DID_NO_CONNECT << 16);
1145                         cmd->scsi_done(cmd);
1146                         return 0;
1147                 }
1148         }
1149         if (atomic_read(&acb->ccboutstandingcount) >=
1150                         ARCMSR_MAX_OUTSTANDING_CMD)
1151                 return SCSI_MLQUEUE_HOST_BUSY;
1152
1153         ccb = arcmsr_get_freeccb(acb);
1154         if (!ccb)
1155                 return SCSI_MLQUEUE_HOST_BUSY;
1156         arcmsr_build_ccb(acb, ccb, cmd);
1157         arcmsr_post_ccb(acb, ccb);
1158         return 0;
1159 }
1160
1161 static void arcmsr_get_firmware_spec(struct AdapterControlBlock *acb)
1162 {
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 *) &reg->message_rwbuffer[15];
1167         char __iomem *iop_firm_version = (char __iomem *) &reg->message_rwbuffer[17];
1168         int count;
1169
1170         writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
1171         if (arcmsr_wait_msgint_ready(acb))
1172                 printk(KERN_NOTICE
1173                         "arcmsr%d: wait "
1174                         "'get adapter firmware miscellaneous data' timeout \n"
1175                         , acb->host->host_no);
1176         count = 8;
1177         while (count) {
1178                 *acb_firm_model = readb(iop_firm_model);
1179                 acb_firm_model++;
1180                 iop_firm_model++;
1181                 count--;
1182         }
1183         count = 16;
1184         while (count) {
1185                 *acb_firm_version = readb(iop_firm_version);
1186                 acb_firm_version++;
1187                 iop_firm_version++;
1188                 count--;
1189         }
1190         printk(KERN_INFO
1191                 "ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n"
1192                 , acb->host->host_no
1193                 , acb->firm_version);
1194         acb->firm_request_len = readl(&reg->message_rwbuffer[1]);
1195         acb->firm_numbers_queue = readl(&reg->message_rwbuffer[2]);
1196         acb->firm_sdram_size = readl(&reg->message_rwbuffer[3]);
1197         acb->firm_hd_channels = readl(&reg->message_rwbuffer[4]);
1198 }
1199
1200 static void arcmsr_polling_ccbdone(struct AdapterControlBlock *acb,
1201         struct CommandControlBlock *poll_ccb)
1202 {
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;
1206         int id, lun;
1207
1208  polling_ccb_retry:
1209         poll_count++;
1210         outbound_intstatus = readl(&reg->outbound_intstatus)
1211                                         & acb->outbound_int_enable;
1212         writel(outbound_intstatus, &reg->outbound_intstatus);/*clear interrupt*/
1213         while (1) {
1214                 if ((flag_ccb = readl(&reg->outbound_queueport)) == 0xFFFFFFFF) {
1215                         if (poll_ccb_done)
1216                                 break;
1217                         else {
1218                                 msleep(25);
1219                                 if (poll_count > 100)
1220                                         break;
1221                                 goto polling_ccb_retry;
1222                         }
1223                 }
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)) {
1230                                 printk(KERN_NOTICE
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
1236                                         , ccb);
1237                                 ccb->pcmd->result = DID_ABORT << 16;
1238                                 arcmsr_ccb_complete(ccb, 1);
1239                                 poll_ccb_done = 1;
1240                                 continue;
1241                         }
1242                         printk(KERN_NOTICE
1243                                 "arcmsr%d: polling get an illegal ccb"
1244                                 " command done ccb ='0x%p'"
1245                                 "ccboutstandingcount = %d \n"
1246                                 , acb->host->host_no
1247                                 , ccb
1248                                 , atomic_read(&acb->ccboutstandingcount));
1249                         continue;
1250                 }
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);
1258                 } else {
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);
1264                                 }
1265                                 break;
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);
1271                                 }
1272                                 break;
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);
1277                                 }
1278                                 break;
1279                         default:
1280                                 printk(KERN_NOTICE
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
1285                                         , id
1286                                         , lun
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);
1291                                 break;
1292                         }
1293                 }
1294         }
1295 }
1296 static void arcmsr_done4_abort_postqueue(struct AdapterControlBlock *acb)
1297 {
1298         int i = 0, found = 0;
1299         int id, lun;
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*/
1304
1305         while (((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF) &&
1306 (i++ < 256)){
1307                 ccb = (struct CommandControlBlock *)(acb->vir2phy_offset +
1308 (flag_ccb << 5));
1309         if (ccb){
1310                 if ((ccb->acb != acb)||(ccb->startdone != \
1311 ARCMSR_CCB_START)){
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));
1316                                 continue;
1317                         }
1318
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);
1326                         }
1327                         else {
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);
1333                                 }
1334                                 break;
1335
1336                                 case ARCMSR_DEV_ABORTED:
1337
1338                                 case ARCMSR_DEV_INIT_FAIL: {
1339                                                 acb->devstate[id][lun] =
1340                                                         ARECA_RAID_GONE;
1341                                                 ccb->pcmd->result =
1342                                                         DID_BAD_TARGET << 16;
1343                                 arcmsr_ccb_complete(ccb, 1);
1344                                 }
1345                                 break;
1346
1347                                 case ARCMSR_DEV_CHECK_CONDITION: {
1348                                                 acb->devstate[id][lun] =
1349                                                         ARECA_RAID_GOOD;
1350                                                 arcmsr_report_sense_info(ccb);
1351                                                 arcmsr_ccb_complete(ccb, 1);
1352                                 }
1353                                 break;
1354
1355                                 default:
1356                                                 printk(KERN_NOTICE
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] =
1365                                                                 ARECA_RAID_GONE;
1366                                                 ccb->pcmd->result =
1367                                                         DID_BAD_TARGET << 16;
1368                                                 arcmsr_ccb_complete(ccb, 1);
1369                                 break;
1370                                }
1371         }
1372                        found = 1;
1373                }
1374         }
1375         if (found){
1376                 outbound_intstatus = readl(&reg->outbound_intstatus) & \
1377                         acb->outbound_int_enable;
1378                 writel(outbound_intstatus, &reg->outbound_intstatus);
1379                 /*clear interrupt*/
1380         }
1381         return;
1382 }
1383
1384
1385 static void arcmsr_iop_init(struct AdapterControlBlock *acb)
1386 {
1387         struct MessageUnit __iomem *reg = acb->pmu;
1388         uint32_t intmask_org, mask, outbound_doorbell, firmware_state = 0;
1389
1390         do {
1391                 firmware_state = readl(&reg->outbound_msgaddr1);
1392         } while (!(firmware_state & ARCMSR_OUTBOUND_MESG1_FIRMWARE_OK));
1393         intmask_org = readl(&reg->outbound_intmask)
1394                         | ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE;
1395         arcmsr_get_firmware_spec(acb);
1396
1397         acb->acb_flags |= ACB_F_MSG_START_BGRB;
1398         writel(ARCMSR_INBOUND_MESG0_START_BGRB, &reg->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);
1403         }
1404
1405         outbound_doorbell = readl(&reg->outbound_doorbell);
1406         writel(outbound_doorbell, &reg->outbound_doorbell);
1407         writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, &reg->inbound_doorbell);
1408         mask = ~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE
1409                         | ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE);
1410         writel(intmask_org & mask, &reg->outbound_intmask);
1411         acb->outbound_int_enable = ~(intmask_org & mask) & 0x000000ff;
1412         acb->acb_flags |= ACB_F_IOP_INITED;
1413 }
1414
1415 static void arcmsr_iop_reset(struct AdapterControlBlock *acb)
1416 {
1417         struct CommandControlBlock *ccb;
1418         uint32_t intmask_org;
1419         int i = 0;
1420
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;
1434                         }
1435                 }
1436                 /* enable all outbound interrupt */
1437                 arcmsr_enable_outbound_ints(acb, intmask_org);
1438         }
1439
1440 }
1441
1442 static int arcmsr_bus_reset(struct scsi_cmnd *cmd)
1443 {
1444         struct AdapterControlBlock *acb =
1445                 (struct AdapterControlBlock *)cmd->device->host->hostdata;
1446         int i;
1447
1448         acb->num_resets++;
1449         acb->acb_flags |= ACB_F_BUS_RESET;
1450         for (i = 0; i < 400; i++) {
1451                 if (!atomic_read(&acb->ccboutstandingcount))
1452                         break;
1453                 arcmsr_interrupt(acb);
1454                 msleep(25);
1455         }
1456         arcmsr_iop_reset(acb);
1457         acb->acb_flags &= ~ACB_F_BUS_RESET;
1458         return SUCCESS;
1459 }
1460
1461 static void arcmsr_abort_one_cmd(struct AdapterControlBlock *acb,
1462                 struct CommandControlBlock *ccb)
1463 {
1464         u32 intmask;
1465
1466         ccb->startdone = ARCMSR_CCB_ABORTED;
1467
1468         /*
1469         ** Wait for 3 sec for all command done.
1470         */
1471         msleep_interruptible(3000);
1472
1473         intmask = arcmsr_disable_outbound_ints(acb);
1474         arcmsr_polling_ccbdone(acb, ccb);
1475         arcmsr_enable_outbound_ints(acb, intmask);
1476 }
1477
1478 static int arcmsr_abort(struct scsi_cmnd *cmd)
1479 {
1480         struct AdapterControlBlock *acb =
1481                 (struct AdapterControlBlock *)cmd->device->host->hostdata;
1482         int i = 0;
1483
1484         printk(KERN_NOTICE
1485                 "arcmsr%d: abort device command of scsi id = %d lun = %d \n",
1486                 acb->host->host_no, cmd->device->id, cmd->device->lun);
1487         acb->num_aborts++;
1488         /*
1489         ************************************************
1490         ** the all interrupt service routine is locked
1491         ** we need to handle it as soon as possible and exit
1492         ************************************************
1493         */
1494         if (!atomic_read(&acb->ccboutstandingcount))
1495                 return SUCCESS;
1496
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);
1501                         break;
1502                 }
1503         }
1504
1505         return SUCCESS;
1506 }
1507
1508 static const char *arcmsr_info(struct Scsi_Host *host)
1509 {
1510         struct AdapterControlBlock *acb =
1511                 (struct AdapterControlBlock *) host->hostdata;
1512         static char buf[256];
1513         char *type;
1514         int raid6 = 1;
1515
1516         switch (acb->pdev->device) {
1517         case PCI_DEVICE_ID_ARECA_1110:
1518         case PCI_DEVICE_ID_ARECA_1210:
1519                 raid6 = 0;
1520                 /*FALLTHRU*/
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:
1530                 type = "SATA";
1531                 break;
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:
1536                 type = "SAS";
1537                 break;
1538         default:
1539                 type = "X-TYPE";
1540                 break;
1541         }
1542         sprintf(buf, "Areca %s Host Adapter RAID Controller%s\n %s",
1543                         type, raid6 ? "( RAID6 capable)" : "",
1544                         ARCMSR_DRIVER_VERSION);
1545         return buf;
1546 }
1547
1548 static pci_ers_result_t arcmsr_pci_slot_reset(struct pci_dev *pdev)
1549 {
1550         struct Scsi_Host *host;
1551         struct AdapterControlBlock *acb;
1552         uint8_t bus, dev_fun;
1553         int error;
1554
1555         error = pci_enable_device(pdev);
1556         if (error)
1557                 return PCI_ERS_RESULT_DISCONNECT;
1558         pci_set_master(pdev);
1559
1560         host = scsi_host_alloc(&arcmsr_scsi_host_template, sizeof \
1561 (struct AdapterControlBlock));
1562         if (!host)
1563                 return PCI_ERS_RESULT_DISCONNECT;
1564         acb = (struct AdapterControlBlock *)host->hostdata;
1565         memset(acb, 0, sizeof (struct AdapterControlBlock));
1566
1567         error = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
1568         if (error) {
1569                 error = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1570                 if (error) {
1571                         printk(KERN_WARNING
1572                                "scsi%d: No suitable DMA mask available\n",
1573                                host->host_no);
1574                         return PCI_ERS_RESULT_DISCONNECT;
1575                 }
1576         }
1577         bus = pdev->bus->number;
1578         dev_fun = pdev->devfn;
1579         acb = (struct AdapterControlBlock *) host->hostdata;
1580         memset(acb, 0, sizeof(struct AdapterControlBlock));
1581         acb->pdev = pdev;
1582         acb->host = host;
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");
1594         if (error)
1595                 return PCI_ERS_RESULT_DISCONNECT;
1596
1597         acb->pmu = ioremap(pci_resource_start(pdev, 0),
1598                            pci_resource_len(pdev, 0));
1599         if (!acb->pmu) {
1600                 printk(KERN_NOTICE "arcmsr%d: memory"
1601                         " mapping region fail \n", acb->host->host_no);
1602                 return PCI_ERS_RESULT_DISCONNECT;
1603         }
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);
1609
1610         error = arcmsr_alloc_ccb_pool(acb);
1611         if (error)
1612                 return PCI_ERS_RESULT_DISCONNECT;
1613
1614         error = request_irq(pdev->irq, arcmsr_do_interrupt,
1615                         IRQF_DISABLED | IRQF_SHARED, "arcmsr", acb);
1616         if (error)
1617                 return PCI_ERS_RESULT_DISCONNECT;
1618
1619         arcmsr_iop_init(acb);
1620         if (strncmp(acb->firm_version, "V1.42", 5) >= 0)
1621               host->max_sectors = ARCMSR_MAX_XFER_SECTORS_B;
1622
1623         pci_set_drvdata(pdev, host);
1624
1625         error = scsi_add_host(host, &pdev->dev);
1626         if (error)
1627                 return PCI_ERS_RESULT_DISCONNECT;
1628
1629         error = arcmsr_alloc_sysfs_attr(acb);
1630         if (error)
1631                 return PCI_ERS_RESULT_DISCONNECT;
1632
1633         scsi_scan_host(host);
1634         return PCI_ERS_RESULT_RECOVERED;
1635 }
1636
1637 static void arcmsr_pci_ers_need_reset_forepart(struct pci_dev *pdev)
1638 {
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;
1645         int id, lun;
1646         uint32_t flag_ccb, outbound_intstatus;
1647
1648         while (((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF) &&
1649                                                                 (i++ < 256)){
1650                         ccb = (struct CommandControlBlock *)(acb->vir2phy_offset
1651                                                          + (flag_ccb << 5));
1652                         if (ccb){
1653                                 if ((ccb->acb != acb)||(ccb->startdone !=
1654                                                         ARCMSR_CCB_START)){
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));
1660                                         continue;
1661                                 }
1662
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] ==
1667                                                                 ARECA_RAID_GONE)
1668                                                 acb->devstate[id][lun] =
1669                                                                 ARECA_RAID_GOOD;
1670                                         ccb->pcmd->result = DID_OK << 16;
1671                                         arcmsr_ccb_complete(ccb, 1);
1672                                 }
1673                                 else {
1674                                         switch(ccb->arcmsr_cdb.DeviceStatus) {
1675                                         case ARCMSR_DEV_SELECT_TIMEOUT: {
1676                                                         acb->devstate[id][lun] =
1677                                                         ARECA_RAID_GONE;
1678                                                         ccb->pcmd->result =
1679                                                         DID_NO_CONNECT << 16;
1680                                                         arcmsr_ccb_complete(ccb, 1);
1681                                         }
1682                                         break;
1683
1684                                         case ARCMSR_DEV_ABORTED:
1685
1686                                         case ARCMSR_DEV_INIT_FAIL: {
1687                                                         acb->devstate[id][lun] =
1688                                                          ARECA_RAID_GONE;
1689                                                         ccb->pcmd->result =
1690                                                         DID_BAD_TARGET << 16;
1691                                                         arcmsr_ccb_complete(ccb, 1);
1692                                         }
1693                                         break;
1694
1695                                         case ARCMSR_DEV_CHECK_CONDITION: {
1696                                                         acb->devstate[id][lun] =
1697                                                          ARECA_RAID_GOOD;
1698                                                         arcmsr_report_sense_info(ccb);
1699                                                         arcmsr_ccb_complete(ccb, 1);
1700                                         }
1701                                         break;
1702
1703                                         default:
1704                                                         printk(KERN_NOTICE
1705                                                                 "arcmsr%d: scsi \
1706                                                                 id = %d lun = %d"
1707                                                                 " polling and \
1708                                                                 getting command \
1709                                                                 error done"
1710                                                                 "but got unknown \
1711                                                         DeviceStatus = 0x%x \n"
1712                                                         , acb->host->host_no,
1713                                                                 id, lun,
1714                                                 ccb->arcmsr_cdb.DeviceStatus);
1715                                                         acb->devstate[id][lun] =
1716                                                                 ARECA_RAID_GONE;
1717                                                         ccb->pcmd->result =
1718                                                         DID_BAD_TARGET << 16;
1719                                                         arcmsr_ccb_complete(ccb, 1);
1720                                         break;
1721                                         }
1722                                 }
1723                                 found = 1;
1724                         }
1725                 }
1726         if (found){
1727                 outbound_intstatus = readl(&reg->outbound_intstatus) &
1728                                                         acb->outbound_int_enable;
1729                 writel(outbound_intstatus, &reg->outbound_intstatus);
1730                 /*clear interrupt*/
1731                     }
1732         return;
1733 }
1734
1735
1736 static void arcmsr_pci_ers_disconnect_forepart(struct pci_dev *pdev)
1737 {
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;
1744         int id, lun;
1745         uint32_t flag_ccb, outbound_intstatus;
1746
1747         while (((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF) &&
1748                                                                 (i++ < 256)){
1749                         ccb = (struct CommandControlBlock *)(acb->vir2phy_offset +
1750                                                         (flag_ccb << 5));
1751                         if (ccb){
1752                                 if ((ccb->acb != acb)||(ccb->startdone !=
1753                                                         ARCMSR_CCB_START)){
1754                                         printk(KERN_NOTICE
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));
1760                                         continue;
1761                         }
1762
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);
1770                         }
1771                         else {
1772                                 switch(ccb->arcmsr_cdb.DeviceStatus) {
1773                                 case ARCMSR_DEV_SELECT_TIMEOUT: {
1774                                                 acb->devstate[id][lun] =
1775                                                                 ARECA_RAID_GONE;
1776                                                 ccb->pcmd->result =
1777                                                         DID_NO_CONNECT << 16;
1778                                                 arcmsr_ccb_complete(ccb, 1);
1779                                 }
1780                                 break;
1781
1782                                 case ARCMSR_DEV_ABORTED:
1783
1784                                 case ARCMSR_DEV_INIT_FAIL: {
1785                                                 acb->devstate[id][lun] =
1786                                                                 ARECA_RAID_GONE;
1787                                                 ccb->pcmd->result =
1788                                                         DID_BAD_TARGET << 16;
1789                                                 arcmsr_ccb_complete(ccb, 1);
1790                                 }
1791                                 break;
1792
1793                                 case ARCMSR_DEV_CHECK_CONDITION: {
1794                                                 acb->devstate[id][lun] =
1795                                                                 ARECA_RAID_GOOD;
1796                                                 arcmsr_report_sense_info(ccb);
1797                                                 arcmsr_ccb_complete(ccb, 1);
1798                                 }
1799                                 break;
1800
1801                                 default:
1802                                                 printk(KERN_NOTICE "arcmsr%d: \
1803                                                         scsi id = %d lun = %d"
1804                                                                 " polling and \
1805                                                 getting command error done"
1806                                                                 "but got unknown \
1807                                                  DeviceStatus = 0x%x \n"
1808                                                                 , acb->host->host_no,
1809                                         id, lun, ccb->arcmsr_cdb.DeviceStatus);
1810                                                         acb->devstate[id][lun] =
1811                                                                 ARECA_RAID_GONE;
1812                                                         ccb->pcmd->result =
1813                                                         DID_BAD_TARGET << 16;
1814                                                         arcmsr_ccb_complete(ccb, 1);
1815                                 break;
1816                                 }
1817                         }
1818                         found = 1;
1819                 }
1820         }
1821         if (found){
1822                 outbound_intstatus = readl(&reg->outbound_intstatus) &
1823                                                 acb->outbound_int_enable;
1824                 writel(outbound_intstatus, &reg->outbound_intstatus);
1825                 /*clear interrupt*/
1826         }
1827         return;
1828 }
1829
1830 static pci_ers_result_t arcmsr_pci_error_detected(struct pci_dev *pdev,
1831                                                 pci_channel_state_t state)
1832 {
1833         switch (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;
1840                         break;
1841         default:
1842                         return PCI_ERS_RESULT_NEED_RESET;
1843         }
1844 }