[SCSI] arcmsr: simplify some double loops in sleeping functions
[pandora-kernel.git] / drivers / scsi / arcmsr / arcmsr_hba.c
1 /*
2 *******************************************************************************
3 **        O.S   : Linux
4 **   FILE NAME  : arcmsr_hba.c
5 **        BY    : Nick Cheng
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: support@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/slab.h>
60 #include <linux/pci.h>
61 #include <linux/aer.h>
62 #include <asm/dma.h>
63 #include <asm/io.h>
64 #include <asm/system.h>
65 #include <asm/uaccess.h>
66 #include <scsi/scsi_host.h>
67 #include <scsi/scsi.h>
68 #include <scsi/scsi_cmnd.h>
69 #include <scsi/scsi_tcq.h>
70 #include <scsi/scsi_device.h>
71 #include <scsi/scsi_transport.h>
72 #include <scsi/scsicam.h>
73 #include "arcmsr.h"
74 MODULE_AUTHOR("Nick Cheng <support@areca.com.tw>");
75 MODULE_DESCRIPTION("ARECA (ARC11xx/12xx/16xx/1880) SATA/SAS RAID Host Bus Adapter");
76 MODULE_LICENSE("Dual BSD/GPL");
77 MODULE_VERSION(ARCMSR_DRIVER_VERSION);
78
79 #define ARCMSR_SLEEPTIME        10
80 #define ARCMSR_RETRYCOUNT       12
81
82 wait_queue_head_t wait_q;
83 static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb,
84                                         struct scsi_cmnd *cmd);
85 static int arcmsr_iop_confirm(struct AdapterControlBlock *acb);
86 static int arcmsr_abort(struct scsi_cmnd *);
87 static int arcmsr_bus_reset(struct scsi_cmnd *);
88 static int arcmsr_bios_param(struct scsi_device *sdev,
89                 struct block_device *bdev, sector_t capacity, int *info);
90 static int arcmsr_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
91 static int arcmsr_probe(struct pci_dev *pdev,
92                                 const struct pci_device_id *id);
93 static void arcmsr_remove(struct pci_dev *pdev);
94 static void arcmsr_shutdown(struct pci_dev *pdev);
95 static void arcmsr_iop_init(struct AdapterControlBlock *acb);
96 static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb);
97 static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb);
98 static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb);
99 static void arcmsr_flush_hba_cache(struct AdapterControlBlock *acb);
100 static void arcmsr_flush_hbb_cache(struct AdapterControlBlock *acb);
101 static void arcmsr_request_device_map(unsigned long pacb);
102 static void arcmsr_request_hba_device_map(struct AdapterControlBlock *acb);
103 static void arcmsr_request_hbb_device_map(struct AdapterControlBlock *acb);
104 static void arcmsr_request_hbc_device_map(struct AdapterControlBlock *acb);
105 static void arcmsr_message_isr_bh_fn(struct work_struct *work);
106 static bool arcmsr_get_firmware_spec(struct AdapterControlBlock *acb);
107 static void arcmsr_start_adapter_bgrb(struct AdapterControlBlock *acb);
108 static void arcmsr_hbc_message_isr(struct AdapterControlBlock *pACB);
109 static void arcmsr_hardware_reset(struct AdapterControlBlock *acb);
110 static const char *arcmsr_info(struct Scsi_Host *);
111 static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb);
112 static int arcmsr_adjust_disk_queue_depth(struct scsi_device *sdev,
113                                           int queue_depth, int reason)
114 {
115         if (reason != SCSI_QDEPTH_DEFAULT)
116                 return -EOPNOTSUPP;
117
118         if (queue_depth > ARCMSR_MAX_CMD_PERLUN)
119                 queue_depth = ARCMSR_MAX_CMD_PERLUN;
120         scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
121         return queue_depth;
122 }
123
124 static struct scsi_host_template arcmsr_scsi_host_template = {
125         .module                 = THIS_MODULE,
126         .name                   = "ARCMSR ARECA SATA/SAS RAID Controller"
127                                 ARCMSR_DRIVER_VERSION,
128         .info                   = arcmsr_info,
129         .queuecommand           = arcmsr_queue_command,
130         .eh_abort_handler               = arcmsr_abort,
131         .eh_bus_reset_handler   = arcmsr_bus_reset,
132         .bios_param             = arcmsr_bios_param,
133         .change_queue_depth     = arcmsr_adjust_disk_queue_depth,
134         .can_queue              = ARCMSR_MAX_FREECCB_NUM,
135         .this_id                        = ARCMSR_SCSI_INITIATOR_ID,
136         .sg_tablesize                   = ARCMSR_DEFAULT_SG_ENTRIES, 
137         .max_sectors                    = ARCMSR_MAX_XFER_SECTORS_C, 
138         .cmd_per_lun            = ARCMSR_MAX_CMD_PERLUN,
139         .use_clustering         = ENABLE_CLUSTERING,
140         .shost_attrs            = arcmsr_host_attrs,
141 };
142 static struct pci_device_id arcmsr_device_id_table[] = {
143         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1110)},
144         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1120)},
145         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1130)},
146         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1160)},
147         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1170)},
148         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1200)},
149         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1201)},
150         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1202)},
151         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1210)},
152         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1220)},
153         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1230)},
154         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1260)},
155         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1270)},
156         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1280)},
157         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1380)},
158         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1381)},
159         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1680)},
160         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1681)},
161         {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1880)},
162         {0, 0}, /* Terminating entry */
163 };
164 MODULE_DEVICE_TABLE(pci, arcmsr_device_id_table);
165 static struct pci_driver arcmsr_pci_driver = {
166         .name                   = "arcmsr",
167         .id_table                       = arcmsr_device_id_table,
168         .probe                  = arcmsr_probe,
169         .remove                 = arcmsr_remove,
170         .shutdown               = arcmsr_shutdown,
171 };
172 /*
173 ****************************************************************************
174 ****************************************************************************
175 */
176
177 static void arcmsr_free_hbb_mu(struct AdapterControlBlock *acb)
178 {
179         switch (acb->adapter_type) {
180         case ACB_ADAPTER_TYPE_A:
181         case ACB_ADAPTER_TYPE_C:
182                 break;
183         case ACB_ADAPTER_TYPE_B:{
184                 dma_free_coherent(&acb->pdev->dev,
185                         sizeof(struct MessageUnit_B),
186                         acb->pmuB, acb->dma_coherent_handle_hbb_mu);
187         }
188         }
189 }
190
191 static bool arcmsr_remap_pciregion(struct AdapterControlBlock *acb)
192 {
193         struct pci_dev *pdev = acb->pdev;
194         switch (acb->adapter_type){
195         case ACB_ADAPTER_TYPE_A:{
196                 acb->pmuA = ioremap(pci_resource_start(pdev,0), pci_resource_len(pdev,0));
197                 if (!acb->pmuA) {
198                         printk(KERN_NOTICE "arcmsr%d: memory mapping region fail \n", acb->host->host_no);
199                         return false;
200                 }
201                 break;
202         }
203         case ACB_ADAPTER_TYPE_B:{
204                 void __iomem *mem_base0, *mem_base1;
205                 mem_base0 = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
206                 if (!mem_base0) {
207                         printk(KERN_NOTICE "arcmsr%d: memory mapping region fail \n", acb->host->host_no);
208                         return false;
209                 }
210                 mem_base1 = ioremap(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2));
211                 if (!mem_base1) {
212                         iounmap(mem_base0);
213                         printk(KERN_NOTICE "arcmsr%d: memory mapping region fail \n", acb->host->host_no);
214                         return false;
215                 }
216                 acb->mem_base0 = mem_base0;
217                 acb->mem_base1 = mem_base1;
218                 break;
219         }
220         case ACB_ADAPTER_TYPE_C:{
221                 acb->pmuC = ioremap_nocache(pci_resource_start(pdev, 1), pci_resource_len(pdev, 1));
222                 if (!acb->pmuC) {
223                         printk(KERN_NOTICE "arcmsr%d: memory mapping region fail \n", acb->host->host_no);
224                         return false;
225                 }
226                 if (readl(&acb->pmuC->outbound_doorbell) & ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE) {
227                         writel(ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE_DOORBELL_CLEAR, &acb->pmuC->outbound_doorbell_clear);/*clear interrupt*/
228                         return true;
229                 }
230                 break;
231         }
232         }
233         return true;
234 }
235
236 static void arcmsr_unmap_pciregion(struct AdapterControlBlock *acb)
237 {
238         switch (acb->adapter_type) {
239         case ACB_ADAPTER_TYPE_A:{
240                 iounmap(acb->pmuA);
241         }
242         break;
243         case ACB_ADAPTER_TYPE_B:{
244                 iounmap(acb->mem_base0);
245                 iounmap(acb->mem_base1);
246         }
247
248         break;
249         case ACB_ADAPTER_TYPE_C:{
250                 iounmap(acb->pmuC);
251         }
252         }
253 }
254
255 static irqreturn_t arcmsr_do_interrupt(int irq, void *dev_id)
256 {
257         irqreturn_t handle_state;
258         struct AdapterControlBlock *acb = dev_id;
259
260         handle_state = arcmsr_interrupt(acb);
261         return handle_state;
262 }
263
264 static int arcmsr_bios_param(struct scsi_device *sdev,
265                 struct block_device *bdev, sector_t capacity, int *geom)
266 {
267         int ret, heads, sectors, cylinders, total_capacity;
268         unsigned char *buffer;/* return copy of block device's partition table */
269
270         buffer = scsi_bios_ptable(bdev);
271         if (buffer) {
272                 ret = scsi_partsize(buffer, capacity, &geom[2], &geom[0], &geom[1]);
273                 kfree(buffer);
274                 if (ret != -1)
275                         return ret;
276         }
277         total_capacity = capacity;
278         heads = 64;
279         sectors = 32;
280         cylinders = total_capacity / (heads * sectors);
281         if (cylinders > 1024) {
282                 heads = 255;
283                 sectors = 63;
284                 cylinders = total_capacity / (heads * sectors);
285         }
286         geom[0] = heads;
287         geom[1] = sectors;
288         geom[2] = cylinders;
289         return 0;
290 }
291
292 static void arcmsr_define_adapter_type(struct AdapterControlBlock *acb)
293 {
294         struct pci_dev *pdev = acb->pdev;
295         u16 dev_id;
296         pci_read_config_word(pdev, PCI_DEVICE_ID, &dev_id);
297         acb->dev_id = dev_id;
298         switch (dev_id) {
299         case 0x1880: {
300                 acb->adapter_type = ACB_ADAPTER_TYPE_C;
301                 }
302                 break;
303         case 0x1201: {
304                 acb->adapter_type = ACB_ADAPTER_TYPE_B;
305                 }
306                 break;
307
308         default: acb->adapter_type = ACB_ADAPTER_TYPE_A;
309         }
310 }
311
312 static uint8_t arcmsr_hba_wait_msgint_ready(struct AdapterControlBlock *acb)
313 {
314         struct MessageUnit_A __iomem *reg = acb->pmuA;
315         int i;
316
317         for (i = 0; i < 2000; i++) {
318                 if (readl(&reg->outbound_intstatus) &
319                                 ARCMSR_MU_OUTBOUND_MESSAGE0_INT) {
320                         writel(ARCMSR_MU_OUTBOUND_MESSAGE0_INT,
321                                 &reg->outbound_intstatus);
322                         return true;
323                 }
324                 msleep(10);
325         } /* max 20 seconds */
326
327         return false;
328 }
329
330 static uint8_t arcmsr_hbb_wait_msgint_ready(struct AdapterControlBlock *acb)
331 {
332         struct MessageUnit_B *reg = acb->pmuB;
333         int i;
334
335         for (i = 0; i < 2000; i++) {
336                 if (readl(reg->iop2drv_doorbell)
337                         & ARCMSR_IOP2DRV_MESSAGE_CMD_DONE) {
338                         writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN,
339                                         reg->iop2drv_doorbell);
340                         writel(ARCMSR_DRV2IOP_END_OF_INTERRUPT,
341                                         reg->drv2iop_doorbell);
342                         return true;
343                 }
344                 msleep(10);
345         } /* max 20 seconds */
346
347         return false;
348 }
349
350 static uint8_t arcmsr_hbc_wait_msgint_ready(struct AdapterControlBlock *pACB)
351 {
352         struct MessageUnit_C *phbcmu = (struct MessageUnit_C *)pACB->pmuC;
353         int i;
354
355         for (i = 0; i < 2000; i++) {
356                 if (readl(&phbcmu->outbound_doorbell)
357                                 & ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE) {
358                         writel(ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE_DOORBELL_CLEAR,
359                                 &phbcmu->outbound_doorbell_clear); /*clear interrupt*/
360                         return true;
361                 }
362                 msleep(10);
363         } /* max 20 seconds */
364
365         return false;
366 }
367
368 static void arcmsr_flush_hba_cache(struct AdapterControlBlock *acb)
369 {
370         struct MessageUnit_A __iomem *reg = acb->pmuA;
371         int retry_count = 30;
372         writel(ARCMSR_INBOUND_MESG0_FLUSH_CACHE, &reg->inbound_msgaddr0);
373         do {
374                 if (arcmsr_hba_wait_msgint_ready(acb))
375                         break;
376                 else {
377                         retry_count--;
378                         printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
379                         timeout, retry count down = %d \n", acb->host->host_no, retry_count);
380                 }
381         } while (retry_count != 0);
382 }
383
384 static void arcmsr_flush_hbb_cache(struct AdapterControlBlock *acb)
385 {
386         struct MessageUnit_B *reg = acb->pmuB;
387         int retry_count = 30;
388         writel(ARCMSR_MESSAGE_FLUSH_CACHE, reg->drv2iop_doorbell);
389         do {
390                 if (arcmsr_hbb_wait_msgint_ready(acb))
391                         break;
392                 else {
393                         retry_count--;
394                         printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
395                         timeout,retry count down = %d \n", acb->host->host_no, retry_count);
396                 }
397         } while (retry_count != 0);
398 }
399
400 static void arcmsr_flush_hbc_cache(struct AdapterControlBlock *pACB)
401 {
402         struct MessageUnit_C *reg = (struct MessageUnit_C *)pACB->pmuC;
403         int retry_count = 30;/* enlarge wait flush adapter cache time: 10 minute */
404         writel(ARCMSR_INBOUND_MESG0_FLUSH_CACHE, &reg->inbound_msgaddr0);
405         writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &reg->inbound_doorbell);
406         do {
407                 if (arcmsr_hbc_wait_msgint_ready(pACB)) {
408                         break;
409                 } else {
410                         retry_count--;
411                         printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
412                         timeout,retry count down = %d \n", pACB->host->host_no, retry_count);
413                 }
414         } while (retry_count != 0);
415         return;
416 }
417 static void arcmsr_flush_adapter_cache(struct AdapterControlBlock *acb)
418 {
419         switch (acb->adapter_type) {
420
421         case ACB_ADAPTER_TYPE_A: {
422                 arcmsr_flush_hba_cache(acb);
423                 }
424                 break;
425
426         case ACB_ADAPTER_TYPE_B: {
427                 arcmsr_flush_hbb_cache(acb);
428                 }
429                 break;
430         case ACB_ADAPTER_TYPE_C: {
431                 arcmsr_flush_hbc_cache(acb);
432                 }
433         }
434 }
435
436 static int arcmsr_alloc_ccb_pool(struct AdapterControlBlock *acb)
437 {
438         struct pci_dev *pdev = acb->pdev;
439         void *dma_coherent;
440         dma_addr_t dma_coherent_handle;
441         struct CommandControlBlock *ccb_tmp;
442         int i = 0, j = 0;
443         dma_addr_t cdb_phyaddr;
444         unsigned long roundup_ccbsize = 0, offset;
445         unsigned long max_xfer_len;
446         unsigned long max_sg_entrys;
447         uint32_t  firm_config_version;
448         for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
449                 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
450                         acb->devstate[i][j] = ARECA_RAID_GONE;
451
452         max_xfer_len = ARCMSR_MAX_XFER_LEN;
453         max_sg_entrys = ARCMSR_DEFAULT_SG_ENTRIES;
454         firm_config_version = acb->firm_cfg_version;
455         if((firm_config_version & 0xFF) >= 3){
456                 max_xfer_len = (ARCMSR_CDB_SG_PAGE_LENGTH << ((firm_config_version >> 8) & 0xFF)) * 1024;/* max 4M byte */
457                 max_sg_entrys = (max_xfer_len/4096);    
458         }
459         acb->host->max_sectors = max_xfer_len/512;
460         acb->host->sg_tablesize = max_sg_entrys;
461         roundup_ccbsize = roundup(sizeof(struct CommandControlBlock) + (max_sg_entrys - 1) * sizeof(struct SG64ENTRY), 32);
462         acb->uncache_size = roundup_ccbsize * ARCMSR_MAX_FREECCB_NUM + 32;
463         dma_coherent = dma_alloc_coherent(&pdev->dev, acb->uncache_size, &dma_coherent_handle, GFP_KERNEL);
464         if(!dma_coherent){
465                 printk(KERN_NOTICE "arcmsr%d: dma_alloc_coherent got error \n", acb->host->host_no);
466                 return -ENOMEM;
467         }
468         acb->dma_coherent = dma_coherent;
469         acb->dma_coherent_handle = dma_coherent_handle;
470         memset(dma_coherent, 0, acb->uncache_size);
471         offset = roundup((unsigned long)dma_coherent, 32) - (unsigned long)dma_coherent;
472         dma_coherent_handle = dma_coherent_handle + offset;
473         dma_coherent = (struct CommandControlBlock *)dma_coherent + offset;
474         ccb_tmp = dma_coherent;
475         acb->vir2phy_offset = (unsigned long)dma_coherent - (unsigned long)dma_coherent_handle;
476         for(i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++){
477                 cdb_phyaddr = dma_coherent_handle + offsetof(struct CommandControlBlock, arcmsr_cdb);
478                 ccb_tmp->cdb_phyaddr_pattern = ((acb->adapter_type == ACB_ADAPTER_TYPE_C) ? cdb_phyaddr : (cdb_phyaddr >> 5));
479                 acb->pccb_pool[i] = ccb_tmp;
480                 ccb_tmp->acb = acb;
481                 INIT_LIST_HEAD(&ccb_tmp->list);
482                 list_add_tail(&ccb_tmp->list, &acb->ccb_free_list);
483                 ccb_tmp = (struct CommandControlBlock *)((unsigned long)ccb_tmp + roundup_ccbsize);
484                 dma_coherent_handle = dma_coherent_handle + roundup_ccbsize;
485         }
486         return 0;
487 }
488
489 static void arcmsr_message_isr_bh_fn(struct work_struct *work) 
490 {
491         struct AdapterControlBlock *acb = container_of(work,struct AdapterControlBlock, arcmsr_do_message_isr_bh);
492         switch (acb->adapter_type) {
493                 case ACB_ADAPTER_TYPE_A: {
494
495                         struct MessageUnit_A __iomem *reg  = acb->pmuA;
496                         char *acb_dev_map = (char *)acb->device_map;
497                         uint32_t __iomem *signature = (uint32_t __iomem*) (&reg->message_rwbuffer[0]);
498                         char __iomem *devicemap = (char __iomem*) (&reg->message_rwbuffer[21]);
499                         int target, lun;
500                         struct scsi_device *psdev;
501                         char diff;
502
503                         atomic_inc(&acb->rq_map_token);
504                         if (readl(signature) == ARCMSR_SIGNATURE_GET_CONFIG) {
505                                 for(target = 0; target < ARCMSR_MAX_TARGETID -1; target++) {
506                                         diff = (*acb_dev_map)^readb(devicemap);
507                                         if (diff != 0) {
508                                                 char temp;
509                                                 *acb_dev_map = readb(devicemap);
510                                                 temp =*acb_dev_map;
511                                                 for(lun = 0; lun < ARCMSR_MAX_TARGETLUN; lun++) {
512                                                         if((temp & 0x01)==1 && (diff & 0x01) == 1) {    
513                                                                 scsi_add_device(acb->host, 0, target, lun);
514                                                         }else if((temp & 0x01) == 0 && (diff & 0x01) == 1) {
515                                                                 psdev = scsi_device_lookup(acb->host, 0, target, lun);
516                                                                 if (psdev != NULL ) {
517                                                                         scsi_remove_device(psdev);
518                                                                         scsi_device_put(psdev);
519                                                                 }
520                                                         }
521                                                         temp >>= 1;
522                                                         diff >>= 1;
523                                                 }
524                                         }
525                                         devicemap++;
526                                         acb_dev_map++;
527                                 }
528                         }
529                         break;
530                 }
531
532                 case ACB_ADAPTER_TYPE_B: {
533                         struct MessageUnit_B *reg  = acb->pmuB;
534                         char *acb_dev_map = (char *)acb->device_map;
535                         uint32_t __iomem *signature = (uint32_t __iomem*)(&reg->message_rwbuffer[0]);
536                         char __iomem *devicemap = (char __iomem*)(&reg->message_rwbuffer[21]);
537                         int target, lun;
538                         struct scsi_device *psdev;
539                         char diff;
540
541                         atomic_inc(&acb->rq_map_token);
542                         if (readl(signature) == ARCMSR_SIGNATURE_GET_CONFIG) {
543                                 for(target = 0; target < ARCMSR_MAX_TARGETID -1; target++) {
544                                         diff = (*acb_dev_map)^readb(devicemap);
545                                         if (diff != 0) {
546                                                 char temp;
547                                                 *acb_dev_map = readb(devicemap);
548                                                 temp =*acb_dev_map;
549                                                 for(lun = 0; lun < ARCMSR_MAX_TARGETLUN; lun++) {
550                                                         if((temp & 0x01)==1 && (diff & 0x01) == 1) {    
551                                                                 scsi_add_device(acb->host, 0, target, lun);
552                                                         }else if((temp & 0x01) == 0 && (diff & 0x01) == 1) {
553                                                                 psdev = scsi_device_lookup(acb->host, 0, target, lun);
554                                                                 if (psdev != NULL ) {
555                                                                         scsi_remove_device(psdev);
556                                                                         scsi_device_put(psdev);
557                                                                 }
558                                                         }
559                                                         temp >>= 1;
560                                                         diff >>= 1;
561                                                 }
562                                         }
563                                         devicemap++;
564                                         acb_dev_map++;
565                                 }
566                         }
567                 }
568                 break;
569                 case ACB_ADAPTER_TYPE_C: {
570                         struct MessageUnit_C *reg  = acb->pmuC;
571                         char *acb_dev_map = (char *)acb->device_map;
572                         uint32_t __iomem *signature = (uint32_t __iomem *)(&reg->msgcode_rwbuffer[0]);
573                         char __iomem *devicemap = (char __iomem *)(&reg->msgcode_rwbuffer[21]);
574                         int target, lun;
575                         struct scsi_device *psdev;
576                         char diff;
577
578                         atomic_inc(&acb->rq_map_token);
579                         if (readl(signature) == ARCMSR_SIGNATURE_GET_CONFIG) {
580                                 for (target = 0; target < ARCMSR_MAX_TARGETID - 1; target++) {
581                                         diff = (*acb_dev_map)^readb(devicemap);
582                                         if (diff != 0) {
583                                                 char temp;
584                                                 *acb_dev_map = readb(devicemap);
585                                                 temp = *acb_dev_map;
586                                                 for (lun = 0; lun < ARCMSR_MAX_TARGETLUN; lun++) {
587                                                         if ((temp & 0x01) == 1 && (diff & 0x01) == 1) {
588                                                                 scsi_add_device(acb->host, 0, target, lun);
589                                                         } else if ((temp & 0x01) == 0 && (diff & 0x01) == 1) {
590                                                                 psdev = scsi_device_lookup(acb->host, 0, target, lun);
591                                                                 if (psdev != NULL) {
592                                                                         scsi_remove_device(psdev);
593                                                                         scsi_device_put(psdev);
594                                                                 }
595                                                         }
596                                                         temp >>= 1;
597                                                         diff >>= 1;
598                                                 }
599                                         }
600                                         devicemap++;
601                                         acb_dev_map++;
602                                 }
603                         }
604                 }
605         }
606 }
607
608 static int arcmsr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
609 {
610         struct Scsi_Host *host;
611         struct AdapterControlBlock *acb;
612         uint8_t bus,dev_fun;
613         int error;
614         error = pci_enable_device(pdev);
615         if(error){
616                 return -ENODEV;
617         }
618         host = scsi_host_alloc(&arcmsr_scsi_host_template, sizeof(struct AdapterControlBlock));
619         if(!host){
620                 goto pci_disable_dev;
621         }
622         error = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
623         if(error){
624                 error = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
625                 if(error){
626                         printk(KERN_WARNING
627                                "scsi%d: No suitable DMA mask available\n",
628                                host->host_no);
629                         goto scsi_host_release;
630                 }
631         }
632         init_waitqueue_head(&wait_q);
633         bus = pdev->bus->number;
634         dev_fun = pdev->devfn;
635         acb = (struct AdapterControlBlock *) host->hostdata;
636         memset(acb,0,sizeof(struct AdapterControlBlock));
637         acb->pdev = pdev;
638         acb->host = host;
639         host->max_lun = ARCMSR_MAX_TARGETLUN;
640         host->max_id = ARCMSR_MAX_TARGETID;             /*16:8*/
641         host->max_cmd_len = 16;                         /*this is issue of 64bit LBA ,over 2T byte*/
642         host->can_queue = ARCMSR_MAX_FREECCB_NUM;       /* max simultaneous cmds */             
643         host->cmd_per_lun = ARCMSR_MAX_CMD_PERLUN;          
644         host->this_id = ARCMSR_SCSI_INITIATOR_ID;
645         host->unique_id = (bus << 8) | dev_fun;
646         pci_set_drvdata(pdev, host);
647         pci_set_master(pdev);
648         error = pci_request_regions(pdev, "arcmsr");
649         if(error){
650                 goto scsi_host_release;
651         }
652         spin_lock_init(&acb->eh_lock);
653         spin_lock_init(&acb->ccblist_lock);
654         acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
655                         ACB_F_MESSAGE_RQBUFFER_CLEARED |
656                         ACB_F_MESSAGE_WQBUFFER_READED);
657         acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
658         INIT_LIST_HEAD(&acb->ccb_free_list);
659         arcmsr_define_adapter_type(acb);
660         error = arcmsr_remap_pciregion(acb);
661         if(!error){
662                 goto pci_release_regs;
663         }
664         error = arcmsr_get_firmware_spec(acb);
665         if(!error){
666                 goto unmap_pci_region;
667         }
668         error = arcmsr_alloc_ccb_pool(acb);
669         if(error){
670                 goto free_hbb_mu;
671         }
672         arcmsr_iop_init(acb);
673         error = scsi_add_host(host, &pdev->dev);
674         if(error){
675                 goto RAID_controller_stop;
676         }
677         error = request_irq(pdev->irq, arcmsr_do_interrupt, IRQF_SHARED, "arcmsr", acb);
678         if(error){
679                 goto scsi_host_remove;
680         }
681         host->irq = pdev->irq;
682         scsi_scan_host(host);
683         INIT_WORK(&acb->arcmsr_do_message_isr_bh, arcmsr_message_isr_bh_fn);
684         atomic_set(&acb->rq_map_token, 16);
685         atomic_set(&acb->ante_token_value, 16);
686         acb->fw_flag = FW_NORMAL;
687         init_timer(&acb->eternal_timer);
688         acb->eternal_timer.expires = jiffies + msecs_to_jiffies(6 * HZ);
689         acb->eternal_timer.data = (unsigned long) acb;
690         acb->eternal_timer.function = &arcmsr_request_device_map;
691         add_timer(&acb->eternal_timer);
692         if(arcmsr_alloc_sysfs_attr(acb))
693                 goto out_free_sysfs;
694         return 0;
695 out_free_sysfs:
696 scsi_host_remove:
697         scsi_remove_host(host);
698 RAID_controller_stop:
699         arcmsr_stop_adapter_bgrb(acb);
700         arcmsr_flush_adapter_cache(acb);
701         arcmsr_free_ccb_pool(acb);
702 free_hbb_mu:
703         arcmsr_free_hbb_mu(acb);
704 unmap_pci_region:
705         arcmsr_unmap_pciregion(acb);
706 pci_release_regs:
707         pci_release_regions(pdev);
708 scsi_host_release:
709         scsi_host_put(host);
710 pci_disable_dev:
711         pci_disable_device(pdev);
712         return -ENODEV;
713 }
714
715 static uint8_t arcmsr_abort_hba_allcmd(struct AdapterControlBlock *acb)
716 {
717         struct MessageUnit_A __iomem *reg = acb->pmuA;
718         writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, &reg->inbound_msgaddr0);
719         if (!arcmsr_hba_wait_msgint_ready(acb)) {
720                 printk(KERN_NOTICE
721                         "arcmsr%d: wait 'abort all outstanding command' timeout \n"
722                         , acb->host->host_no);
723                 return false;
724         }
725         return true;
726 }
727
728 static uint8_t arcmsr_abort_hbb_allcmd(struct AdapterControlBlock *acb)
729 {
730         struct MessageUnit_B *reg = acb->pmuB;
731
732         writel(ARCMSR_MESSAGE_ABORT_CMD, reg->drv2iop_doorbell);
733         if (!arcmsr_hbb_wait_msgint_ready(acb)) {
734                 printk(KERN_NOTICE
735                         "arcmsr%d: wait 'abort all outstanding command' timeout \n"
736                         , acb->host->host_no);
737                 return false;
738         }
739         return true;
740 }
741 static uint8_t arcmsr_abort_hbc_allcmd(struct AdapterControlBlock *pACB)
742 {
743         struct MessageUnit_C *reg = (struct MessageUnit_C *)pACB->pmuC;
744         writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, &reg->inbound_msgaddr0);
745         writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &reg->inbound_doorbell);
746         if (!arcmsr_hbc_wait_msgint_ready(pACB)) {
747                 printk(KERN_NOTICE
748                         "arcmsr%d: wait 'abort all outstanding command' timeout \n"
749                         , pACB->host->host_no);
750                 return false;
751         }
752         return true;
753 }
754 static uint8_t arcmsr_abort_allcmd(struct AdapterControlBlock *acb)
755 {
756         uint8_t rtnval = 0;
757         switch (acb->adapter_type) {
758         case ACB_ADAPTER_TYPE_A: {
759                 rtnval = arcmsr_abort_hba_allcmd(acb);
760                 }
761                 break;
762
763         case ACB_ADAPTER_TYPE_B: {
764                 rtnval = arcmsr_abort_hbb_allcmd(acb);
765                 }
766                 break;
767
768         case ACB_ADAPTER_TYPE_C: {
769                 rtnval = arcmsr_abort_hbc_allcmd(acb);
770                 }
771         }
772         return rtnval;
773 }
774
775 static bool arcmsr_hbb_enable_driver_mode(struct AdapterControlBlock *pacb)
776 {
777         struct MessageUnit_B *reg = pacb->pmuB;
778         writel(ARCMSR_MESSAGE_START_DRIVER_MODE, reg->drv2iop_doorbell);
779         if (!arcmsr_hbb_wait_msgint_ready(pacb)) {
780                 printk(KERN_ERR "arcmsr%d: can't set driver mode. \n", pacb->host->host_no);
781                 return false;
782         }
783         return true;
784 }
785
786 static void arcmsr_pci_unmap_dma(struct CommandControlBlock *ccb)
787 {
788         struct scsi_cmnd *pcmd = ccb->pcmd;
789
790         scsi_dma_unmap(pcmd);
791 }
792
793 static void arcmsr_ccb_complete(struct CommandControlBlock *ccb)
794 {
795         struct AdapterControlBlock *acb = ccb->acb;
796         struct scsi_cmnd *pcmd = ccb->pcmd;
797         unsigned long flags;
798         atomic_dec(&acb->ccboutstandingcount);
799         arcmsr_pci_unmap_dma(ccb);
800         ccb->startdone = ARCMSR_CCB_DONE;
801         spin_lock_irqsave(&acb->ccblist_lock, flags);
802         list_add_tail(&ccb->list, &acb->ccb_free_list);
803         spin_unlock_irqrestore(&acb->ccblist_lock, flags);
804         pcmd->scsi_done(pcmd);
805 }
806
807 static void arcmsr_report_sense_info(struct CommandControlBlock *ccb)
808 {
809
810         struct scsi_cmnd *pcmd = ccb->pcmd;
811         struct SENSE_DATA *sensebuffer = (struct SENSE_DATA *)pcmd->sense_buffer;
812         pcmd->result = DID_OK << 16;
813         if (sensebuffer) {
814                 int sense_data_length =
815                         sizeof(struct SENSE_DATA) < SCSI_SENSE_BUFFERSIZE
816                         ? sizeof(struct SENSE_DATA) : SCSI_SENSE_BUFFERSIZE;
817                 memset(sensebuffer, 0, SCSI_SENSE_BUFFERSIZE);
818                 memcpy(sensebuffer, ccb->arcmsr_cdb.SenseData, sense_data_length);
819                 sensebuffer->ErrorCode = SCSI_SENSE_CURRENT_ERRORS;
820                 sensebuffer->Valid = 1;
821         }
822 }
823
824 static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb)
825 {
826         u32 orig_mask = 0;
827         switch (acb->adapter_type) {    
828         case ACB_ADAPTER_TYPE_A : {
829                 struct MessageUnit_A __iomem *reg = acb->pmuA;
830                 orig_mask = readl(&reg->outbound_intmask);
831                 writel(orig_mask|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE, \
832                                                 &reg->outbound_intmask);
833                 }
834                 break;
835         case ACB_ADAPTER_TYPE_B : {
836                 struct MessageUnit_B *reg = acb->pmuB;
837                 orig_mask = readl(reg->iop2drv_doorbell_mask);
838                 writel(0, reg->iop2drv_doorbell_mask);
839                 }
840                 break;
841         case ACB_ADAPTER_TYPE_C:{
842                 struct MessageUnit_C *reg = (struct MessageUnit_C *)acb->pmuC;
843                 /* disable all outbound interrupt */
844                 orig_mask = readl(&reg->host_int_mask); /* disable outbound message0 int */
845                 writel(orig_mask|ARCMSR_HBCMU_ALL_INTMASKENABLE, &reg->host_int_mask);
846                 }
847                 break;
848         }
849         return orig_mask;
850 }
851
852 static void arcmsr_report_ccb_state(struct AdapterControlBlock *acb, 
853                         struct CommandControlBlock *ccb, bool error)
854 {
855         uint8_t id, lun;
856         id = ccb->pcmd->device->id;
857         lun = ccb->pcmd->device->lun;
858         if (!error) {
859                 if (acb->devstate[id][lun] == ARECA_RAID_GONE)
860                         acb->devstate[id][lun] = ARECA_RAID_GOOD;
861                 ccb->pcmd->result = DID_OK << 16;
862                 arcmsr_ccb_complete(ccb);
863         }else{
864                 switch (ccb->arcmsr_cdb.DeviceStatus) {
865                 case ARCMSR_DEV_SELECT_TIMEOUT: {
866                         acb->devstate[id][lun] = ARECA_RAID_GONE;
867                         ccb->pcmd->result = DID_NO_CONNECT << 16;
868                         arcmsr_ccb_complete(ccb);
869                         }
870                         break;
871
872                 case ARCMSR_DEV_ABORTED:
873
874                 case ARCMSR_DEV_INIT_FAIL: {
875                         acb->devstate[id][lun] = ARECA_RAID_GONE;
876                         ccb->pcmd->result = DID_BAD_TARGET << 16;
877                         arcmsr_ccb_complete(ccb);
878                         }
879                         break;
880
881                 case ARCMSR_DEV_CHECK_CONDITION: {
882                         acb->devstate[id][lun] = ARECA_RAID_GOOD;
883                         arcmsr_report_sense_info(ccb);
884                         arcmsr_ccb_complete(ccb);
885                         }
886                         break;
887
888                 default:
889                         printk(KERN_NOTICE
890                                 "arcmsr%d: scsi id = %d lun = %d isr get command error done, \
891                                 but got unknown DeviceStatus = 0x%x \n"
892                                 , acb->host->host_no
893                                 , id
894                                 , lun
895                                 , ccb->arcmsr_cdb.DeviceStatus);
896                                 acb->devstate[id][lun] = ARECA_RAID_GONE;
897                                 ccb->pcmd->result = DID_NO_CONNECT << 16;
898                                 arcmsr_ccb_complete(ccb);
899                         break;
900                 }
901         }
902 }
903
904 static void arcmsr_drain_donequeue(struct AdapterControlBlock *acb, struct CommandControlBlock *pCCB, bool error)
905 {
906         int id, lun;
907         if ((pCCB->acb != acb) || (pCCB->startdone != ARCMSR_CCB_START)) {
908                 if (pCCB->startdone == ARCMSR_CCB_ABORTED) {
909                         struct scsi_cmnd *abortcmd = pCCB->pcmd;
910                         if (abortcmd) {
911                                 id = abortcmd->device->id;
912                                 lun = abortcmd->device->lun;                            
913                                 abortcmd->result |= DID_ABORT << 16;
914                                 arcmsr_ccb_complete(pCCB);
915                                 printk(KERN_NOTICE "arcmsr%d: pCCB ='0x%p' isr got aborted command \n",
916                                 acb->host->host_no, pCCB);
917                         }
918                         return;
919                 }
920                 printk(KERN_NOTICE "arcmsr%d: isr get an illegal ccb command \
921                                 done acb = '0x%p'"
922                                 "ccb = '0x%p' ccbacb = '0x%p' startdone = 0x%x"
923                                 " ccboutstandingcount = %d \n"
924                                 , acb->host->host_no
925                                 , acb
926                                 , pCCB
927                                 , pCCB->acb
928                                 , pCCB->startdone
929                                 , atomic_read(&acb->ccboutstandingcount));
930                   return;
931         }
932         arcmsr_report_ccb_state(acb, pCCB, error);
933 }
934
935 static void arcmsr_done4abort_postqueue(struct AdapterControlBlock *acb)
936 {
937         int i = 0;
938         uint32_t flag_ccb;
939         struct ARCMSR_CDB *pARCMSR_CDB;
940         bool error;
941         struct CommandControlBlock *pCCB;
942         switch (acb->adapter_type) {
943
944         case ACB_ADAPTER_TYPE_A: {
945                 struct MessageUnit_A __iomem *reg = acb->pmuA;
946                 uint32_t outbound_intstatus;
947                 outbound_intstatus = readl(&reg->outbound_intstatus) &
948                                         acb->outbound_int_enable;
949                 /*clear and abort all outbound posted Q*/
950                 writel(outbound_intstatus, &reg->outbound_intstatus);/*clear interrupt*/
951                 while(((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF)
952                                 && (i++ < ARCMSR_MAX_OUTSTANDING_CMD)) {
953                         pARCMSR_CDB = (struct ARCMSR_CDB *)(acb->vir2phy_offset + (flag_ccb << 5));/*frame must be 32 bytes aligned*/
954                         pCCB = container_of(pARCMSR_CDB, struct CommandControlBlock, arcmsr_cdb);
955                         error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE0) ? true : false;
956                         arcmsr_drain_donequeue(acb, pCCB, error);
957                 }
958                 }
959                 break;
960
961         case ACB_ADAPTER_TYPE_B: {
962                 struct MessageUnit_B *reg = acb->pmuB;
963                 /*clear all outbound posted Q*/
964                 writel(ARCMSR_DOORBELL_INT_CLEAR_PATTERN, reg->iop2drv_doorbell); /* clear doorbell interrupt */
965                 for (i = 0; i < ARCMSR_MAX_HBB_POSTQUEUE; i++) {
966                         if ((flag_ccb = readl(&reg->done_qbuffer[i])) != 0) {
967                                 writel(0, &reg->done_qbuffer[i]);
968                                 pARCMSR_CDB = (struct ARCMSR_CDB *)(acb->vir2phy_offset+(flag_ccb << 5));/*frame must be 32 bytes aligned*/
969                                 pCCB = container_of(pARCMSR_CDB, struct CommandControlBlock, arcmsr_cdb);
970                                 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE0) ? true : false;
971                                 arcmsr_drain_donequeue(acb, pCCB, error);
972                         }
973                         reg->post_qbuffer[i] = 0;
974                 }
975                 reg->doneq_index = 0;
976                 reg->postq_index = 0;
977                 }
978                 break;
979         case ACB_ADAPTER_TYPE_C: {
980                 struct MessageUnit_C *reg = acb->pmuC;
981                 struct  ARCMSR_CDB *pARCMSR_CDB;
982                 uint32_t flag_ccb, ccb_cdb_phy;
983                 bool error;
984                 struct CommandControlBlock *pCCB;
985                 while ((readl(&reg->host_int_status) & ARCMSR_HBCMU_OUTBOUND_POSTQUEUE_ISR) && (i++ < ARCMSR_MAX_OUTSTANDING_CMD)) {
986                         /*need to do*/
987                         flag_ccb = readl(&reg->outbound_queueport_low);
988                         ccb_cdb_phy = (flag_ccb & 0xFFFFFFF0);
989                         pARCMSR_CDB = (struct  ARCMSR_CDB *)(acb->vir2phy_offset+ccb_cdb_phy);/*frame must be 32 bytes aligned*/
990                         pCCB = container_of(pARCMSR_CDB, struct CommandControlBlock, arcmsr_cdb);
991                         error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE1) ? true : false;
992                         arcmsr_drain_donequeue(acb, pCCB, error);
993                 }
994         }
995         }
996 }
997 static void arcmsr_remove(struct pci_dev *pdev)
998 {
999         struct Scsi_Host *host = pci_get_drvdata(pdev);
1000         struct AdapterControlBlock *acb =
1001                 (struct AdapterControlBlock *) host->hostdata;
1002         int poll_count = 0;
1003         arcmsr_free_sysfs_attr(acb);
1004         scsi_remove_host(host);
1005         flush_work_sync(&acb->arcmsr_do_message_isr_bh);
1006         del_timer_sync(&acb->eternal_timer);
1007         arcmsr_disable_outbound_ints(acb);
1008         arcmsr_stop_adapter_bgrb(acb);
1009         arcmsr_flush_adapter_cache(acb);        
1010         acb->acb_flags |= ACB_F_SCSISTOPADAPTER;
1011         acb->acb_flags &= ~ACB_F_IOP_INITED;
1012
1013         for (poll_count = 0; poll_count < ARCMSR_MAX_OUTSTANDING_CMD; poll_count++){
1014                 if (!atomic_read(&acb->ccboutstandingcount))
1015                         break;
1016                 arcmsr_interrupt(acb);/* FIXME: need spinlock */
1017                 msleep(25);
1018         }
1019
1020         if (atomic_read(&acb->ccboutstandingcount)) {
1021                 int i;
1022
1023                 arcmsr_abort_allcmd(acb);
1024                 arcmsr_done4abort_postqueue(acb);
1025                 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
1026                         struct CommandControlBlock *ccb = acb->pccb_pool[i];
1027                         if (ccb->startdone == ARCMSR_CCB_START) {
1028                                 ccb->startdone = ARCMSR_CCB_ABORTED;
1029                                 ccb->pcmd->result = DID_ABORT << 16;
1030                                 arcmsr_ccb_complete(ccb);
1031                         }
1032                 }
1033         }
1034         free_irq(pdev->irq, acb);
1035         arcmsr_free_ccb_pool(acb);
1036         arcmsr_free_hbb_mu(acb);
1037         arcmsr_unmap_pciregion(acb);
1038         pci_release_regions(pdev);
1039         scsi_host_put(host);
1040         pci_disable_device(pdev);
1041         pci_set_drvdata(pdev, NULL);
1042 }
1043
1044 static void arcmsr_shutdown(struct pci_dev *pdev)
1045 {
1046         struct Scsi_Host *host = pci_get_drvdata(pdev);
1047         struct AdapterControlBlock *acb =
1048                 (struct AdapterControlBlock *)host->hostdata;
1049         del_timer_sync(&acb->eternal_timer);
1050         arcmsr_disable_outbound_ints(acb);
1051         flush_work_sync(&acb->arcmsr_do_message_isr_bh);
1052         arcmsr_stop_adapter_bgrb(acb);
1053         arcmsr_flush_adapter_cache(acb);
1054 }
1055
1056 static int arcmsr_module_init(void)
1057 {
1058         int error = 0;
1059         error = pci_register_driver(&arcmsr_pci_driver);
1060         return error;
1061 }
1062
1063 static void arcmsr_module_exit(void)
1064 {
1065         pci_unregister_driver(&arcmsr_pci_driver);
1066 }
1067 module_init(arcmsr_module_init);
1068 module_exit(arcmsr_module_exit);
1069
1070 static void arcmsr_enable_outbound_ints(struct AdapterControlBlock *acb,
1071                                                 u32 intmask_org)
1072 {
1073         u32 mask;
1074         switch (acb->adapter_type) {
1075
1076         case ACB_ADAPTER_TYPE_A: {
1077                 struct MessageUnit_A __iomem *reg = acb->pmuA;
1078                 mask = intmask_org & ~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE |
1079                              ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE|
1080                              ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE);
1081                 writel(mask, &reg->outbound_intmask);
1082                 acb->outbound_int_enable = ~(intmask_org & mask) & 0x000000ff;
1083                 }
1084                 break;
1085
1086         case ACB_ADAPTER_TYPE_B: {
1087                 struct MessageUnit_B *reg = acb->pmuB;
1088                 mask = intmask_org | (ARCMSR_IOP2DRV_DATA_WRITE_OK |
1089                         ARCMSR_IOP2DRV_DATA_READ_OK |
1090                         ARCMSR_IOP2DRV_CDB_DONE |
1091                         ARCMSR_IOP2DRV_MESSAGE_CMD_DONE);
1092                 writel(mask, reg->iop2drv_doorbell_mask);
1093                 acb->outbound_int_enable = (intmask_org | mask) & 0x0000000f;
1094                 }
1095                 break;
1096         case ACB_ADAPTER_TYPE_C: {
1097                 struct MessageUnit_C *reg = acb->pmuC;
1098                 mask = ~(ARCMSR_HBCMU_UTILITY_A_ISR_MASK | ARCMSR_HBCMU_OUTBOUND_DOORBELL_ISR_MASK|ARCMSR_HBCMU_OUTBOUND_POSTQUEUE_ISR_MASK);
1099                 writel(intmask_org & mask, &reg->host_int_mask);
1100                 acb->outbound_int_enable = ~(intmask_org & mask) & 0x0000000f;
1101                 }
1102         }
1103 }
1104
1105 static int arcmsr_build_ccb(struct AdapterControlBlock *acb,
1106         struct CommandControlBlock *ccb, struct scsi_cmnd *pcmd)
1107 {
1108         struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
1109         int8_t *psge = (int8_t *)&arcmsr_cdb->u;
1110         __le32 address_lo, address_hi;
1111         int arccdbsize = 0x30;
1112         __le32 length = 0;
1113         int i;
1114         struct scatterlist *sg;
1115         int nseg;
1116         ccb->pcmd = pcmd;
1117         memset(arcmsr_cdb, 0, sizeof(struct ARCMSR_CDB));
1118         arcmsr_cdb->TargetID = pcmd->device->id;
1119         arcmsr_cdb->LUN = pcmd->device->lun;
1120         arcmsr_cdb->Function = 1;
1121         arcmsr_cdb->Context = 0;
1122         memcpy(arcmsr_cdb->Cdb, pcmd->cmnd, pcmd->cmd_len);
1123
1124         nseg = scsi_dma_map(pcmd);
1125         if (unlikely(nseg > acb->host->sg_tablesize || nseg < 0))
1126                 return FAILED;
1127         scsi_for_each_sg(pcmd, sg, nseg, i) {
1128                 /* Get the physical address of the current data pointer */
1129                 length = cpu_to_le32(sg_dma_len(sg));
1130                 address_lo = cpu_to_le32(dma_addr_lo32(sg_dma_address(sg)));
1131                 address_hi = cpu_to_le32(dma_addr_hi32(sg_dma_address(sg)));
1132                 if (address_hi == 0) {
1133                         struct SG32ENTRY *pdma_sg = (struct SG32ENTRY *)psge;
1134
1135                         pdma_sg->address = address_lo;
1136                         pdma_sg->length = length;
1137                         psge += sizeof (struct SG32ENTRY);
1138                         arccdbsize += sizeof (struct SG32ENTRY);
1139                 } else {
1140                         struct SG64ENTRY *pdma_sg = (struct SG64ENTRY *)psge;
1141
1142                         pdma_sg->addresshigh = address_hi;
1143                         pdma_sg->address = address_lo;
1144                         pdma_sg->length = length|cpu_to_le32(IS_SG64_ADDR);
1145                         psge += sizeof (struct SG64ENTRY);
1146                         arccdbsize += sizeof (struct SG64ENTRY);
1147                 }
1148         }
1149         arcmsr_cdb->sgcount = (uint8_t)nseg;
1150         arcmsr_cdb->DataLength = scsi_bufflen(pcmd);
1151         arcmsr_cdb->msgPages = arccdbsize/0x100 + (arccdbsize % 0x100 ? 1 : 0);
1152         if ( arccdbsize > 256)
1153                 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_SGL_BSIZE;
1154         if (pcmd->sc_data_direction == DMA_TO_DEVICE)
1155                 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_WRITE;
1156         ccb->arc_cdb_size = arccdbsize;
1157         return SUCCESS;
1158 }
1159
1160 static void arcmsr_post_ccb(struct AdapterControlBlock *acb, struct CommandControlBlock *ccb)
1161 {
1162         uint32_t cdb_phyaddr_pattern = ccb->cdb_phyaddr_pattern;
1163         struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
1164         atomic_inc(&acb->ccboutstandingcount);
1165         ccb->startdone = ARCMSR_CCB_START;
1166         switch (acb->adapter_type) {
1167         case ACB_ADAPTER_TYPE_A: {
1168                 struct MessageUnit_A __iomem *reg = acb->pmuA;
1169
1170                 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE)
1171                         writel(cdb_phyaddr_pattern | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,
1172                         &reg->inbound_queueport);
1173                 else {
1174                                 writel(cdb_phyaddr_pattern, &reg->inbound_queueport);
1175                 }
1176                 }
1177                 break;
1178
1179         case ACB_ADAPTER_TYPE_B: {
1180                 struct MessageUnit_B *reg = acb->pmuB;
1181                 uint32_t ending_index, index = reg->postq_index;
1182
1183                 ending_index = ((index + 1) % ARCMSR_MAX_HBB_POSTQUEUE);
1184                 writel(0, &reg->post_qbuffer[ending_index]);
1185                 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE) {
1186                         writel(cdb_phyaddr_pattern | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,\
1187                                                  &reg->post_qbuffer[index]);
1188                 } else {
1189                         writel(cdb_phyaddr_pattern, &reg->post_qbuffer[index]);
1190                 }
1191                 index++;
1192                 index %= ARCMSR_MAX_HBB_POSTQUEUE;/*if last index number set it to 0 */
1193                 reg->postq_index = index;
1194                 writel(ARCMSR_DRV2IOP_CDB_POSTED, reg->drv2iop_doorbell);
1195                 }
1196                 break;
1197         case ACB_ADAPTER_TYPE_C: {
1198                 struct MessageUnit_C *phbcmu = (struct MessageUnit_C *)acb->pmuC;
1199                 uint32_t ccb_post_stamp, arc_cdb_size;
1200
1201                 arc_cdb_size = (ccb->arc_cdb_size > 0x300) ? 0x300 : ccb->arc_cdb_size;
1202                 ccb_post_stamp = (cdb_phyaddr_pattern | ((arc_cdb_size - 1) >> 6) | 1);
1203                 if (acb->cdb_phyaddr_hi32) {
1204                         writel(acb->cdb_phyaddr_hi32, &phbcmu->inbound_queueport_high);
1205                         writel(ccb_post_stamp, &phbcmu->inbound_queueport_low);
1206                 } else {
1207                         writel(ccb_post_stamp, &phbcmu->inbound_queueport_low);
1208                 }
1209                 }
1210         }
1211 }
1212
1213 static void arcmsr_stop_hba_bgrb(struct AdapterControlBlock *acb)
1214 {
1215         struct MessageUnit_A __iomem *reg = acb->pmuA;
1216         acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
1217         writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, &reg->inbound_msgaddr0);
1218         if (!arcmsr_hba_wait_msgint_ready(acb)) {
1219                 printk(KERN_NOTICE
1220                         "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
1221                         , acb->host->host_no);
1222         }
1223 }
1224
1225 static void arcmsr_stop_hbb_bgrb(struct AdapterControlBlock *acb)
1226 {
1227         struct MessageUnit_B *reg = acb->pmuB;
1228         acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
1229         writel(ARCMSR_MESSAGE_STOP_BGRB, reg->drv2iop_doorbell);
1230
1231         if (!arcmsr_hbb_wait_msgint_ready(acb)) {
1232                 printk(KERN_NOTICE
1233                         "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
1234                         , acb->host->host_no);
1235         }
1236 }
1237
1238 static void arcmsr_stop_hbc_bgrb(struct AdapterControlBlock *pACB)
1239 {
1240         struct MessageUnit_C *reg = (struct MessageUnit_C *)pACB->pmuC;
1241         pACB->acb_flags &= ~ACB_F_MSG_START_BGRB;
1242         writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, &reg->inbound_msgaddr0);
1243         writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &reg->inbound_doorbell);
1244         if (!arcmsr_hbc_wait_msgint_ready(pACB)) {
1245                 printk(KERN_NOTICE
1246                         "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
1247                         , pACB->host->host_no);
1248         }
1249         return;
1250 }
1251 static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb)
1252 {
1253         switch (acb->adapter_type) {
1254         case ACB_ADAPTER_TYPE_A: {
1255                 arcmsr_stop_hba_bgrb(acb);
1256                 }
1257                 break;
1258
1259         case ACB_ADAPTER_TYPE_B: {
1260                 arcmsr_stop_hbb_bgrb(acb);
1261                 }
1262                 break;
1263         case ACB_ADAPTER_TYPE_C: {
1264                 arcmsr_stop_hbc_bgrb(acb);
1265                 }
1266         }
1267 }
1268
1269 static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb)
1270 {
1271         dma_free_coherent(&acb->pdev->dev, acb->uncache_size, acb->dma_coherent, acb->dma_coherent_handle);
1272 }
1273
1274 void arcmsr_iop_message_read(struct AdapterControlBlock *acb)
1275 {
1276         switch (acb->adapter_type) {
1277         case ACB_ADAPTER_TYPE_A: {
1278                 struct MessageUnit_A __iomem *reg = acb->pmuA;
1279                 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, &reg->inbound_doorbell);
1280                 }
1281                 break;
1282
1283         case ACB_ADAPTER_TYPE_B: {
1284                 struct MessageUnit_B *reg = acb->pmuB;
1285                 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell);
1286                 }
1287                 break;
1288         case ACB_ADAPTER_TYPE_C: {
1289                 struct MessageUnit_C __iomem *reg = acb->pmuC;
1290                 writel(ARCMSR_HBCMU_DRV2IOP_DATA_READ_OK, &reg->inbound_doorbell);
1291                 }
1292         }
1293 }
1294
1295 static void arcmsr_iop_message_wrote(struct AdapterControlBlock *acb)
1296 {
1297         switch (acb->adapter_type) {
1298         case ACB_ADAPTER_TYPE_A: {
1299                 struct MessageUnit_A __iomem *reg = acb->pmuA;
1300                 /*
1301                 ** push inbound doorbell tell iop, driver data write ok
1302                 ** and wait reply on next hwinterrupt for next Qbuffer post
1303                 */
1304                 writel(ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK, &reg->inbound_doorbell);
1305                 }
1306                 break;
1307
1308         case ACB_ADAPTER_TYPE_B: {
1309                 struct MessageUnit_B *reg = acb->pmuB;
1310                 /*
1311                 ** push inbound doorbell tell iop, driver data write ok
1312                 ** and wait reply on next hwinterrupt for next Qbuffer post
1313                 */
1314                 writel(ARCMSR_DRV2IOP_DATA_WRITE_OK, reg->drv2iop_doorbell);
1315                 }
1316                 break;
1317         case ACB_ADAPTER_TYPE_C: {
1318                 struct MessageUnit_C __iomem *reg = acb->pmuC;
1319                 /*
1320                 ** push inbound doorbell tell iop, driver data write ok
1321                 ** and wait reply on next hwinterrupt for next Qbuffer post
1322                 */
1323                 writel(ARCMSR_HBCMU_DRV2IOP_DATA_WRITE_OK, &reg->inbound_doorbell);
1324                 }
1325                 break;
1326         }
1327 }
1328
1329 struct QBUFFER __iomem *arcmsr_get_iop_rqbuffer(struct AdapterControlBlock *acb)
1330 {
1331         struct QBUFFER __iomem *qbuffer = NULL;
1332         switch (acb->adapter_type) {
1333
1334         case ACB_ADAPTER_TYPE_A: {
1335                 struct MessageUnit_A __iomem *reg = acb->pmuA;
1336                 qbuffer = (struct QBUFFER __iomem *)&reg->message_rbuffer;
1337                 }
1338                 break;
1339
1340         case ACB_ADAPTER_TYPE_B: {
1341                 struct MessageUnit_B *reg = acb->pmuB;
1342                 qbuffer = (struct QBUFFER __iomem *)reg->message_rbuffer;
1343                 }
1344                 break;
1345         case ACB_ADAPTER_TYPE_C: {
1346                 struct MessageUnit_C *phbcmu = (struct MessageUnit_C *)acb->pmuC;
1347                 qbuffer = (struct QBUFFER __iomem *)&phbcmu->message_rbuffer;
1348                 }
1349         }
1350         return qbuffer;
1351 }
1352
1353 static struct QBUFFER __iomem *arcmsr_get_iop_wqbuffer(struct AdapterControlBlock *acb)
1354 {
1355         struct QBUFFER __iomem *pqbuffer = NULL;
1356         switch (acb->adapter_type) {
1357
1358         case ACB_ADAPTER_TYPE_A: {
1359                 struct MessageUnit_A __iomem *reg = acb->pmuA;
1360                 pqbuffer = (struct QBUFFER __iomem *) &reg->message_wbuffer;
1361                 }
1362                 break;
1363
1364         case ACB_ADAPTER_TYPE_B: {
1365                 struct MessageUnit_B  *reg = acb->pmuB;
1366                 pqbuffer = (struct QBUFFER __iomem *)reg->message_wbuffer;
1367                 }
1368                 break;
1369         case ACB_ADAPTER_TYPE_C: {
1370                 struct MessageUnit_C *reg = (struct MessageUnit_C *)acb->pmuC;
1371                 pqbuffer = (struct QBUFFER __iomem *)&reg->message_wbuffer;
1372         }
1373
1374         }
1375         return pqbuffer;
1376 }
1377
1378 static void arcmsr_iop2drv_data_wrote_handle(struct AdapterControlBlock *acb)
1379 {
1380         struct QBUFFER __iomem *prbuffer;
1381         struct QBUFFER *pQbuffer;
1382         uint8_t __iomem *iop_data;
1383         int32_t my_empty_len, iop_len, rqbuf_firstindex, rqbuf_lastindex;
1384         rqbuf_lastindex = acb->rqbuf_lastindex;
1385         rqbuf_firstindex = acb->rqbuf_firstindex;
1386         prbuffer = arcmsr_get_iop_rqbuffer(acb);
1387         iop_data = (uint8_t __iomem *)prbuffer->data;
1388         iop_len = prbuffer->data_len;
1389         my_empty_len = (rqbuf_firstindex - rqbuf_lastindex - 1) & (ARCMSR_MAX_QBUFFER - 1);
1390
1391         if (my_empty_len >= iop_len)
1392         {
1393                 while (iop_len > 0) {
1394                         pQbuffer = (struct QBUFFER *)&acb->rqbuffer[rqbuf_lastindex];
1395                         memcpy(pQbuffer, iop_data, 1);
1396                         rqbuf_lastindex++;
1397                         rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1398                         iop_data++;
1399                         iop_len--;
1400                 }
1401                 acb->rqbuf_lastindex = rqbuf_lastindex;
1402                 arcmsr_iop_message_read(acb);
1403         }
1404
1405         else {
1406                 acb->acb_flags |= ACB_F_IOPDATA_OVERFLOW;
1407         }
1408 }
1409
1410 static void arcmsr_iop2drv_data_read_handle(struct AdapterControlBlock *acb)
1411 {
1412         acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_READED;
1413         if (acb->wqbuf_firstindex != acb->wqbuf_lastindex) {
1414                 uint8_t *pQbuffer;
1415                 struct QBUFFER __iomem *pwbuffer;
1416                 uint8_t __iomem *iop_data;
1417                 int32_t allxfer_len = 0;
1418
1419                 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
1420                 pwbuffer = arcmsr_get_iop_wqbuffer(acb);
1421                 iop_data = (uint8_t __iomem *)pwbuffer->data;
1422
1423                 while ((acb->wqbuf_firstindex != acb->wqbuf_lastindex) && \
1424                                                         (allxfer_len < 124)) {
1425                         pQbuffer = &acb->wqbuffer[acb->wqbuf_firstindex];
1426                         memcpy(iop_data, pQbuffer, 1);
1427                         acb->wqbuf_firstindex++;
1428                         acb->wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1429                         iop_data++;
1430                         allxfer_len++;
1431                 }
1432                 pwbuffer->data_len = allxfer_len;
1433
1434                 arcmsr_iop_message_wrote(acb);
1435         }
1436
1437         if (acb->wqbuf_firstindex == acb->wqbuf_lastindex) {
1438                 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_CLEARED;
1439         }
1440 }
1441
1442 static void arcmsr_hba_doorbell_isr(struct AdapterControlBlock *acb)
1443 {
1444         uint32_t outbound_doorbell;
1445         struct MessageUnit_A __iomem *reg = acb->pmuA;
1446         outbound_doorbell = readl(&reg->outbound_doorbell);
1447         writel(outbound_doorbell, &reg->outbound_doorbell);
1448         if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK) {
1449                 arcmsr_iop2drv_data_wrote_handle(acb);
1450         }
1451
1452         if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_READ_OK) {
1453                 arcmsr_iop2drv_data_read_handle(acb);
1454         }
1455 }
1456 static void arcmsr_hbc_doorbell_isr(struct AdapterControlBlock *pACB)
1457 {
1458         uint32_t outbound_doorbell;
1459         struct MessageUnit_C *reg = (struct MessageUnit_C *)pACB->pmuC;
1460         /*
1461         *******************************************************************
1462         **  Maybe here we need to check wrqbuffer_lock is lock or not
1463         **  DOORBELL: din! don!
1464         **  check if there are any mail need to pack from firmware
1465         *******************************************************************
1466         */
1467         outbound_doorbell = readl(&reg->outbound_doorbell);
1468         writel(outbound_doorbell, &reg->outbound_doorbell_clear);/*clear interrupt*/
1469         if (outbound_doorbell & ARCMSR_HBCMU_IOP2DRV_DATA_WRITE_OK) {
1470                 arcmsr_iop2drv_data_wrote_handle(pACB);
1471         }
1472         if (outbound_doorbell & ARCMSR_HBCMU_IOP2DRV_DATA_READ_OK) {
1473                 arcmsr_iop2drv_data_read_handle(pACB);
1474         }
1475         if (outbound_doorbell & ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE) {
1476                 arcmsr_hbc_message_isr(pACB);    /* messenger of "driver to iop commands" */
1477         }
1478         return;
1479 }
1480 static void arcmsr_hba_postqueue_isr(struct AdapterControlBlock *acb)
1481 {
1482         uint32_t flag_ccb;
1483         struct MessageUnit_A __iomem *reg = acb->pmuA;
1484         struct ARCMSR_CDB *pARCMSR_CDB;
1485         struct CommandControlBlock *pCCB;
1486         bool error;
1487         while ((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF) {
1488                 pARCMSR_CDB = (struct ARCMSR_CDB *)(acb->vir2phy_offset + (flag_ccb << 5));/*frame must be 32 bytes aligned*/
1489                 pCCB = container_of(pARCMSR_CDB, struct CommandControlBlock, arcmsr_cdb);
1490                 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE0) ? true : false;
1491                 arcmsr_drain_donequeue(acb, pCCB, error);
1492         }
1493 }
1494 static void arcmsr_hbb_postqueue_isr(struct AdapterControlBlock *acb)
1495 {
1496         uint32_t index;
1497         uint32_t flag_ccb;
1498         struct MessageUnit_B *reg = acb->pmuB;
1499         struct ARCMSR_CDB *pARCMSR_CDB;
1500         struct CommandControlBlock *pCCB;
1501         bool error;
1502         index = reg->doneq_index;
1503         while ((flag_ccb = readl(&reg->done_qbuffer[index])) != 0) {
1504                 writel(0, &reg->done_qbuffer[index]);
1505                 pARCMSR_CDB = (struct ARCMSR_CDB *)(acb->vir2phy_offset+(flag_ccb << 5));/*frame must be 32 bytes aligned*/
1506                 pCCB = container_of(pARCMSR_CDB, struct CommandControlBlock, arcmsr_cdb);
1507                 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE0) ? true : false;
1508                 arcmsr_drain_donequeue(acb, pCCB, error);
1509                 index++;
1510                 index %= ARCMSR_MAX_HBB_POSTQUEUE;
1511                 reg->doneq_index = index;
1512         }
1513 }
1514
1515 static void arcmsr_hbc_postqueue_isr(struct AdapterControlBlock *acb)
1516 {
1517         struct MessageUnit_C *phbcmu;
1518         struct ARCMSR_CDB *arcmsr_cdb;
1519         struct CommandControlBlock *ccb;
1520         uint32_t flag_ccb, ccb_cdb_phy, throttling = 0;
1521         int error;
1522
1523         phbcmu = (struct MessageUnit_C *)acb->pmuC;
1524         /* areca cdb command done */
1525         /* Use correct offset and size for syncing */
1526
1527         while (readl(&phbcmu->host_int_status) &
1528         ARCMSR_HBCMU_OUTBOUND_POSTQUEUE_ISR){
1529         /* check if command done with no error*/
1530         flag_ccb = readl(&phbcmu->outbound_queueport_low);
1531         ccb_cdb_phy = (flag_ccb & 0xFFFFFFF0);/*frame must be 32 bytes aligned*/
1532         arcmsr_cdb = (struct ARCMSR_CDB *)(acb->vir2phy_offset + ccb_cdb_phy);
1533         ccb = container_of(arcmsr_cdb, struct CommandControlBlock, arcmsr_cdb);
1534         error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE1) ? true : false;
1535         /* check if command done with no error */
1536         arcmsr_drain_donequeue(acb, ccb, error);
1537         if (throttling == ARCMSR_HBC_ISR_THROTTLING_LEVEL) {
1538                 writel(ARCMSR_HBCMU_DRV2IOP_POSTQUEUE_THROTTLING, &phbcmu->inbound_doorbell);
1539                 break;
1540         }
1541         throttling++;
1542         }
1543 }
1544 /*
1545 **********************************************************************************
1546 ** Handle a message interrupt
1547 **
1548 ** The only message interrupt we expect is in response to a query for the current adapter config.  
1549 ** We want this in order to compare the drivemap so that we can detect newly-attached drives.
1550 **********************************************************************************
1551 */
1552 static void arcmsr_hba_message_isr(struct AdapterControlBlock *acb)
1553 {
1554         struct MessageUnit_A *reg  = acb->pmuA;
1555         /*clear interrupt and message state*/
1556         writel(ARCMSR_MU_OUTBOUND_MESSAGE0_INT, &reg->outbound_intstatus);
1557         schedule_work(&acb->arcmsr_do_message_isr_bh);
1558 }
1559 static void arcmsr_hbb_message_isr(struct AdapterControlBlock *acb)
1560 {
1561         struct MessageUnit_B *reg  = acb->pmuB;
1562
1563         /*clear interrupt and message state*/
1564         writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN, reg->iop2drv_doorbell);
1565         schedule_work(&acb->arcmsr_do_message_isr_bh);
1566 }
1567 /*
1568 **********************************************************************************
1569 ** Handle a message interrupt
1570 **
1571 ** The only message interrupt we expect is in response to a query for the
1572 ** current adapter config.
1573 ** We want this in order to compare the drivemap so that we can detect newly-attached drives.
1574 **********************************************************************************
1575 */
1576 static void arcmsr_hbc_message_isr(struct AdapterControlBlock *acb)
1577 {
1578         struct MessageUnit_C *reg  = acb->pmuC;
1579         /*clear interrupt and message state*/
1580         writel(ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE_DOORBELL_CLEAR, &reg->outbound_doorbell_clear);
1581         schedule_work(&acb->arcmsr_do_message_isr_bh);
1582 }
1583
1584 static int arcmsr_handle_hba_isr(struct AdapterControlBlock *acb)
1585 {
1586         uint32_t outbound_intstatus;
1587         struct MessageUnit_A __iomem *reg = acb->pmuA;
1588         outbound_intstatus = readl(&reg->outbound_intstatus) &
1589                 acb->outbound_int_enable;
1590         if (!(outbound_intstatus & ARCMSR_MU_OUTBOUND_HANDLE_INT))      {
1591                 return 1;
1592         }
1593         writel(outbound_intstatus, &reg->outbound_intstatus);
1594         if (outbound_intstatus & ARCMSR_MU_OUTBOUND_DOORBELL_INT)       {
1595                 arcmsr_hba_doorbell_isr(acb);
1596         }
1597         if (outbound_intstatus & ARCMSR_MU_OUTBOUND_POSTQUEUE_INT) {
1598                 arcmsr_hba_postqueue_isr(acb);
1599         }
1600         if(outbound_intstatus & ARCMSR_MU_OUTBOUND_MESSAGE0_INT)        {
1601                 /* messenger of "driver to iop commands" */
1602                 arcmsr_hba_message_isr(acb);
1603         }
1604         return 0;
1605 }
1606
1607 static int arcmsr_handle_hbb_isr(struct AdapterControlBlock *acb)
1608 {
1609         uint32_t outbound_doorbell;
1610         struct MessageUnit_B *reg = acb->pmuB;
1611         outbound_doorbell = readl(reg->iop2drv_doorbell) &
1612                                 acb->outbound_int_enable;
1613         if (!outbound_doorbell)
1614                 return 1;
1615
1616         writel(~outbound_doorbell, reg->iop2drv_doorbell);
1617         /*in case the last action of doorbell interrupt clearance is cached,
1618         this action can push HW to write down the clear bit*/
1619         readl(reg->iop2drv_doorbell);
1620         writel(ARCMSR_DRV2IOP_END_OF_INTERRUPT, reg->drv2iop_doorbell);
1621         if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_WRITE_OK) {
1622                 arcmsr_iop2drv_data_wrote_handle(acb);
1623         }
1624         if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_READ_OK) {
1625                 arcmsr_iop2drv_data_read_handle(acb);
1626         }
1627         if (outbound_doorbell & ARCMSR_IOP2DRV_CDB_DONE) {
1628                 arcmsr_hbb_postqueue_isr(acb);
1629         }
1630         if(outbound_doorbell & ARCMSR_IOP2DRV_MESSAGE_CMD_DONE) {
1631                 /* messenger of "driver to iop commands" */
1632                 arcmsr_hbb_message_isr(acb);
1633         }
1634         return 0;
1635 }
1636
1637 static int arcmsr_handle_hbc_isr(struct AdapterControlBlock *pACB)
1638 {
1639         uint32_t host_interrupt_status;
1640         struct MessageUnit_C *phbcmu = (struct MessageUnit_C *)pACB->pmuC;
1641         /*
1642         *********************************************
1643         **   check outbound intstatus
1644         *********************************************
1645         */
1646         host_interrupt_status = readl(&phbcmu->host_int_status);
1647         if (!host_interrupt_status) {
1648                 /*it must be share irq*/
1649                 return 1;
1650         }
1651         /* MU ioctl transfer doorbell interrupts*/
1652         if (host_interrupt_status & ARCMSR_HBCMU_OUTBOUND_DOORBELL_ISR) {
1653                 arcmsr_hbc_doorbell_isr(pACB);   /* messenger of "ioctl message read write" */
1654         }
1655         /* MU post queue interrupts*/
1656         if (host_interrupt_status & ARCMSR_HBCMU_OUTBOUND_POSTQUEUE_ISR) {
1657                 arcmsr_hbc_postqueue_isr(pACB);  /* messenger of "scsi commands" */
1658         }
1659         return 0;
1660 }
1661 static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb)
1662 {
1663         switch (acb->adapter_type) {
1664         case ACB_ADAPTER_TYPE_A: {
1665                 if (arcmsr_handle_hba_isr(acb)) {
1666                         return IRQ_NONE;
1667                 }
1668                 }
1669                 break;
1670
1671         case ACB_ADAPTER_TYPE_B: {
1672                 if (arcmsr_handle_hbb_isr(acb)) {
1673                         return IRQ_NONE;
1674                 }
1675                 }
1676                 break;
1677          case ACB_ADAPTER_TYPE_C: {
1678                 if (arcmsr_handle_hbc_isr(acb)) {
1679                         return IRQ_NONE;
1680                 }
1681                 }
1682         }
1683         return IRQ_HANDLED;
1684 }
1685
1686 static void arcmsr_iop_parking(struct AdapterControlBlock *acb)
1687 {
1688         if (acb) {
1689                 /* stop adapter background rebuild */
1690                 if (acb->acb_flags & ACB_F_MSG_START_BGRB) {
1691                         uint32_t intmask_org;
1692                         acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
1693                         intmask_org = arcmsr_disable_outbound_ints(acb);
1694                         arcmsr_stop_adapter_bgrb(acb);
1695                         arcmsr_flush_adapter_cache(acb);
1696                         arcmsr_enable_outbound_ints(acb, intmask_org);
1697                 }
1698         }
1699 }
1700
1701 void arcmsr_post_ioctldata2iop(struct AdapterControlBlock *acb)
1702 {
1703         int32_t wqbuf_firstindex, wqbuf_lastindex;
1704         uint8_t *pQbuffer;
1705         struct QBUFFER __iomem *pwbuffer;
1706         uint8_t __iomem *iop_data;
1707         int32_t allxfer_len = 0;
1708         pwbuffer = arcmsr_get_iop_wqbuffer(acb);
1709         iop_data = (uint8_t __iomem *)pwbuffer->data;
1710         if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_READED) {
1711                 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
1712                 wqbuf_firstindex = acb->wqbuf_firstindex;
1713                 wqbuf_lastindex = acb->wqbuf_lastindex;
1714                 while ((wqbuf_firstindex != wqbuf_lastindex) && (allxfer_len < 124)) {
1715                         pQbuffer = &acb->wqbuffer[wqbuf_firstindex];
1716                         memcpy(iop_data, pQbuffer, 1);
1717                         wqbuf_firstindex++;
1718                         wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1719                         iop_data++;
1720                         allxfer_len++;
1721                 }
1722                 acb->wqbuf_firstindex = wqbuf_firstindex;
1723                 pwbuffer->data_len = allxfer_len;
1724                 arcmsr_iop_message_wrote(acb);
1725         }
1726 }
1727
1728 static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb,
1729                                         struct scsi_cmnd *cmd)
1730 {
1731         struct CMD_MESSAGE_FIELD *pcmdmessagefld;
1732         int retvalue = 0, transfer_len = 0;
1733         char *buffer;
1734         struct scatterlist *sg;
1735         uint32_t controlcode = (uint32_t ) cmd->cmnd[5] << 24 |
1736                                                 (uint32_t ) cmd->cmnd[6] << 16 |
1737                                                 (uint32_t ) cmd->cmnd[7] << 8  |
1738                                                 (uint32_t ) cmd->cmnd[8];
1739                                                 /* 4 bytes: Areca io control code */
1740         sg = scsi_sglist(cmd);
1741         buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
1742         if (scsi_sg_count(cmd) > 1) {
1743                 retvalue = ARCMSR_MESSAGE_FAIL;
1744                 goto message_out;
1745         }
1746         transfer_len += sg->length;
1747
1748         if (transfer_len > sizeof(struct CMD_MESSAGE_FIELD)) {
1749                 retvalue = ARCMSR_MESSAGE_FAIL;
1750                 goto message_out;
1751         }
1752         pcmdmessagefld = (struct CMD_MESSAGE_FIELD *) buffer;
1753         switch(controlcode) {
1754
1755         case ARCMSR_MESSAGE_READ_RQBUFFER: {
1756                 unsigned char *ver_addr;
1757                 uint8_t *pQbuffer, *ptmpQbuffer;
1758                 int32_t allxfer_len = 0;
1759
1760                 ver_addr = kmalloc(1032, GFP_ATOMIC);
1761                 if (!ver_addr) {
1762                         retvalue = ARCMSR_MESSAGE_FAIL;
1763                         goto message_out;
1764                 }
1765                                 
1766                 ptmpQbuffer = ver_addr;
1767                 while ((acb->rqbuf_firstindex != acb->rqbuf_lastindex)
1768                         && (allxfer_len < 1031)) {
1769                         pQbuffer = &acb->rqbuffer[acb->rqbuf_firstindex];
1770                         memcpy(ptmpQbuffer, pQbuffer, 1);
1771                         acb->rqbuf_firstindex++;
1772                         acb->rqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1773                         ptmpQbuffer++;
1774                         allxfer_len++;
1775                 }
1776                 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1777
1778                         struct QBUFFER __iomem *prbuffer;
1779                         uint8_t __iomem *iop_data;
1780                         int32_t iop_len;
1781
1782                         acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1783                         prbuffer = arcmsr_get_iop_rqbuffer(acb);
1784                         iop_data = prbuffer->data;
1785                         iop_len = readl(&prbuffer->data_len);
1786                         while (iop_len > 0) {
1787                                 acb->rqbuffer[acb->rqbuf_lastindex] = readb(iop_data);
1788                                 acb->rqbuf_lastindex++;
1789                                 acb->rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1790                                 iop_data++;
1791                                 iop_len--;
1792                         }
1793                         arcmsr_iop_message_read(acb);
1794                 }
1795                 memcpy(pcmdmessagefld->messagedatabuffer, ver_addr, allxfer_len);
1796                 pcmdmessagefld->cmdmessage.Length = allxfer_len;
1797                 if(acb->fw_flag == FW_DEADLOCK) {
1798                         pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1799                 }else{
1800                         pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1801                 }
1802                 kfree(ver_addr);
1803                 }
1804                 break;
1805
1806         case ARCMSR_MESSAGE_WRITE_WQBUFFER: {
1807                 unsigned char *ver_addr;
1808                 int32_t my_empty_len, user_len, wqbuf_firstindex, wqbuf_lastindex;
1809                 uint8_t *pQbuffer, *ptmpuserbuffer;
1810
1811                 ver_addr = kmalloc(1032, GFP_ATOMIC);
1812                 if (!ver_addr) {
1813                         retvalue = ARCMSR_MESSAGE_FAIL;
1814                         goto message_out;
1815                 }
1816                 if(acb->fw_flag == FW_DEADLOCK) {
1817                         pcmdmessagefld->cmdmessage.ReturnCode = 
1818                         ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1819                 }else{
1820                         pcmdmessagefld->cmdmessage.ReturnCode = 
1821                         ARCMSR_MESSAGE_RETURNCODE_OK;
1822                 }
1823                 ptmpuserbuffer = ver_addr;
1824                 user_len = pcmdmessagefld->cmdmessage.Length;
1825                 memcpy(ptmpuserbuffer, pcmdmessagefld->messagedatabuffer, user_len);
1826                 wqbuf_lastindex = acb->wqbuf_lastindex;
1827                 wqbuf_firstindex = acb->wqbuf_firstindex;
1828                 if (wqbuf_lastindex != wqbuf_firstindex) {
1829                         struct SENSE_DATA *sensebuffer =
1830                                 (struct SENSE_DATA *)cmd->sense_buffer;
1831                         arcmsr_post_ioctldata2iop(acb);
1832                         /* has error report sensedata */
1833                         sensebuffer->ErrorCode = 0x70;
1834                         sensebuffer->SenseKey = ILLEGAL_REQUEST;
1835                         sensebuffer->AdditionalSenseLength = 0x0A;
1836                         sensebuffer->AdditionalSenseCode = 0x20;
1837                         sensebuffer->Valid = 1;
1838                         retvalue = ARCMSR_MESSAGE_FAIL;
1839                 } else {
1840                         my_empty_len = (wqbuf_firstindex-wqbuf_lastindex - 1)
1841                                 &(ARCMSR_MAX_QBUFFER - 1);
1842                         if (my_empty_len >= user_len) {
1843                                 while (user_len > 0) {
1844                                         pQbuffer =
1845                                         &acb->wqbuffer[acb->wqbuf_lastindex];
1846                                         memcpy(pQbuffer, ptmpuserbuffer, 1);
1847                                         acb->wqbuf_lastindex++;
1848                                         acb->wqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1849                                         ptmpuserbuffer++;
1850                                         user_len--;
1851                                 }
1852                                 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {
1853                                         acb->acb_flags &=
1854                                                 ~ACB_F_MESSAGE_WQBUFFER_CLEARED;
1855                                         arcmsr_post_ioctldata2iop(acb);
1856                                 }
1857                         } else {
1858                                 /* has error report sensedata */
1859                                 struct SENSE_DATA *sensebuffer =
1860                                         (struct SENSE_DATA *)cmd->sense_buffer;
1861                                 sensebuffer->ErrorCode = 0x70;
1862                                 sensebuffer->SenseKey = ILLEGAL_REQUEST;
1863                                 sensebuffer->AdditionalSenseLength = 0x0A;
1864                                 sensebuffer->AdditionalSenseCode = 0x20;
1865                                 sensebuffer->Valid = 1;
1866                                 retvalue = ARCMSR_MESSAGE_FAIL;
1867                         }
1868                         }
1869                         kfree(ver_addr);
1870                 }
1871                 break;
1872
1873         case ARCMSR_MESSAGE_CLEAR_RQBUFFER: {
1874                 uint8_t *pQbuffer = acb->rqbuffer;
1875                 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1876                         acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1877                         arcmsr_iop_message_read(acb);
1878                 }
1879                 acb->acb_flags |= ACB_F_MESSAGE_RQBUFFER_CLEARED;
1880                 acb->rqbuf_firstindex = 0;
1881                 acb->rqbuf_lastindex = 0;
1882                 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1883                 if(acb->fw_flag == FW_DEADLOCK) {
1884                         pcmdmessagefld->cmdmessage.ReturnCode =
1885                         ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1886                 }else{
1887                         pcmdmessagefld->cmdmessage.ReturnCode =
1888                         ARCMSR_MESSAGE_RETURNCODE_OK;
1889                 }
1890                 }
1891                 break;
1892
1893         case ARCMSR_MESSAGE_CLEAR_WQBUFFER: {
1894                 uint8_t *pQbuffer = acb->wqbuffer;
1895                 if(acb->fw_flag == FW_DEADLOCK) {
1896                         pcmdmessagefld->cmdmessage.ReturnCode =
1897                         ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1898                 }else{
1899                         pcmdmessagefld->cmdmessage.ReturnCode =
1900                         ARCMSR_MESSAGE_RETURNCODE_OK;
1901                 }
1902
1903                 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1904                         acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1905                         arcmsr_iop_message_read(acb);
1906                 }
1907                 acb->acb_flags |=
1908                         (ACB_F_MESSAGE_WQBUFFER_CLEARED |
1909                                 ACB_F_MESSAGE_WQBUFFER_READED);
1910                 acb->wqbuf_firstindex = 0;
1911                 acb->wqbuf_lastindex = 0;
1912                 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1913                 }
1914                 break;
1915
1916         case ARCMSR_MESSAGE_CLEAR_ALLQBUFFER: {
1917                 uint8_t *pQbuffer;
1918
1919                 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1920                         acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1921                         arcmsr_iop_message_read(acb);
1922                 }
1923                 acb->acb_flags |=
1924                         (ACB_F_MESSAGE_WQBUFFER_CLEARED
1925                         | ACB_F_MESSAGE_RQBUFFER_CLEARED
1926                         | ACB_F_MESSAGE_WQBUFFER_READED);
1927                 acb->rqbuf_firstindex = 0;
1928                 acb->rqbuf_lastindex = 0;
1929                 acb->wqbuf_firstindex = 0;
1930                 acb->wqbuf_lastindex = 0;
1931                 pQbuffer = acb->rqbuffer;
1932                 memset(pQbuffer, 0, sizeof(struct QBUFFER));
1933                 pQbuffer = acb->wqbuffer;
1934                 memset(pQbuffer, 0, sizeof(struct QBUFFER));
1935                 if(acb->fw_flag == FW_DEADLOCK) {
1936                         pcmdmessagefld->cmdmessage.ReturnCode =
1937                         ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1938                 }else{
1939                         pcmdmessagefld->cmdmessage.ReturnCode =
1940                         ARCMSR_MESSAGE_RETURNCODE_OK;
1941                 }
1942                 }
1943                 break;
1944
1945         case ARCMSR_MESSAGE_RETURN_CODE_3F: {
1946                 if(acb->fw_flag == FW_DEADLOCK) {
1947                         pcmdmessagefld->cmdmessage.ReturnCode =
1948                         ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1949                 }else{
1950                         pcmdmessagefld->cmdmessage.ReturnCode =
1951                         ARCMSR_MESSAGE_RETURNCODE_3F;
1952                 }
1953                 break;
1954                 }
1955         case ARCMSR_MESSAGE_SAY_HELLO: {
1956                 int8_t *hello_string = "Hello! I am ARCMSR";
1957                 if(acb->fw_flag == FW_DEADLOCK) {
1958                         pcmdmessagefld->cmdmessage.ReturnCode =
1959                         ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1960                 }else{
1961                         pcmdmessagefld->cmdmessage.ReturnCode =
1962                         ARCMSR_MESSAGE_RETURNCODE_OK;
1963                 }
1964                 memcpy(pcmdmessagefld->messagedatabuffer, hello_string
1965                         , (int16_t)strlen(hello_string));
1966                 }
1967                 break;
1968
1969         case ARCMSR_MESSAGE_SAY_GOODBYE:
1970                 if(acb->fw_flag == FW_DEADLOCK) {
1971                         pcmdmessagefld->cmdmessage.ReturnCode =
1972                         ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1973                 }
1974                 arcmsr_iop_parking(acb);
1975                 break;
1976
1977         case ARCMSR_MESSAGE_FLUSH_ADAPTER_CACHE:
1978                 if(acb->fw_flag == FW_DEADLOCK) {
1979                         pcmdmessagefld->cmdmessage.ReturnCode =
1980                         ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1981                 }
1982                 arcmsr_flush_adapter_cache(acb);
1983                 break;
1984
1985         default:
1986                 retvalue = ARCMSR_MESSAGE_FAIL;
1987         }
1988         message_out:
1989         sg = scsi_sglist(cmd);
1990         kunmap_atomic(buffer - sg->offset, KM_IRQ0);
1991         return retvalue;
1992 }
1993
1994 static struct CommandControlBlock *arcmsr_get_freeccb(struct AdapterControlBlock *acb)
1995 {
1996         struct list_head *head = &acb->ccb_free_list;
1997         struct CommandControlBlock *ccb = NULL;
1998         unsigned long flags;
1999         spin_lock_irqsave(&acb->ccblist_lock, flags);
2000         if (!list_empty(head)) {
2001                 ccb = list_entry(head->next, struct CommandControlBlock, list);
2002                 list_del_init(&ccb->list);
2003         }else{
2004                 spin_unlock_irqrestore(&acb->ccblist_lock, flags);
2005                 return 0;
2006         }
2007         spin_unlock_irqrestore(&acb->ccblist_lock, flags);
2008         return ccb;
2009 }
2010
2011 static void arcmsr_handle_virtual_command(struct AdapterControlBlock *acb,
2012                 struct scsi_cmnd *cmd)
2013 {
2014         switch (cmd->cmnd[0]) {
2015         case INQUIRY: {
2016                 unsigned char inqdata[36];
2017                 char *buffer;
2018                 struct scatterlist *sg;
2019
2020                 if (cmd->device->lun) {
2021                         cmd->result = (DID_TIME_OUT << 16);
2022                         cmd->scsi_done(cmd);
2023                         return;
2024                 }
2025                 inqdata[0] = TYPE_PROCESSOR;
2026                 /* Periph Qualifier & Periph Dev Type */
2027                 inqdata[1] = 0;
2028                 /* rem media bit & Dev Type Modifier */
2029                 inqdata[2] = 0;
2030                 /* ISO, ECMA, & ANSI versions */
2031                 inqdata[4] = 31;
2032                 /* length of additional data */
2033                 strncpy(&inqdata[8], "Areca   ", 8);
2034                 /* Vendor Identification */
2035                 strncpy(&inqdata[16], "RAID controller ", 16);
2036                 /* Product Identification */
2037                 strncpy(&inqdata[32], "R001", 4); /* Product Revision */
2038
2039                 sg = scsi_sglist(cmd);
2040                 buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
2041
2042                 memcpy(buffer, inqdata, sizeof(inqdata));
2043                 sg = scsi_sglist(cmd);
2044                 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
2045
2046                 cmd->scsi_done(cmd);
2047         }
2048         break;
2049         case WRITE_BUFFER:
2050         case READ_BUFFER: {
2051                 if (arcmsr_iop_message_xfer(acb, cmd))
2052                         cmd->result = (DID_ERROR << 16);
2053                 cmd->scsi_done(cmd);
2054         }
2055         break;
2056         default:
2057                 cmd->scsi_done(cmd);
2058         }
2059 }
2060
2061 static int arcmsr_queue_command_lck(struct scsi_cmnd *cmd,
2062         void (* done)(struct scsi_cmnd *))
2063 {
2064         struct Scsi_Host *host = cmd->device->host;
2065         struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
2066         struct CommandControlBlock *ccb;
2067         int target = cmd->device->id;
2068         int lun = cmd->device->lun;
2069         uint8_t scsicmd = cmd->cmnd[0];
2070         cmd->scsi_done = done;
2071         cmd->host_scribble = NULL;
2072         cmd->result = 0;
2073         if ((scsicmd == SYNCHRONIZE_CACHE) ||(scsicmd == SEND_DIAGNOSTIC)){
2074                 if(acb->devstate[target][lun] == ARECA_RAID_GONE) {
2075                         cmd->result = (DID_NO_CONNECT << 16);
2076                 }
2077                 cmd->scsi_done(cmd);
2078                 return 0;
2079         }
2080         if (target == 16) {
2081                 /* virtual device for iop message transfer */
2082                 arcmsr_handle_virtual_command(acb, cmd);
2083                 return 0;
2084         }
2085         if (atomic_read(&acb->ccboutstandingcount) >=
2086                         ARCMSR_MAX_OUTSTANDING_CMD)
2087                 return SCSI_MLQUEUE_HOST_BUSY;
2088         ccb = arcmsr_get_freeccb(acb);
2089         if (!ccb)
2090                 return SCSI_MLQUEUE_HOST_BUSY;
2091         if (arcmsr_build_ccb( acb, ccb, cmd ) == FAILED) {
2092                 cmd->result = (DID_ERROR << 16) | (RESERVATION_CONFLICT << 1);
2093                 cmd->scsi_done(cmd);
2094                 return 0;
2095         }
2096         arcmsr_post_ccb(acb, ccb);
2097         return 0;
2098 }
2099
2100 static DEF_SCSI_QCMD(arcmsr_queue_command)
2101
2102 static bool arcmsr_get_hba_config(struct AdapterControlBlock *acb)
2103 {
2104         struct MessageUnit_A __iomem *reg = acb->pmuA;
2105         char *acb_firm_model = acb->firm_model;
2106         char *acb_firm_version = acb->firm_version;
2107         char *acb_device_map = acb->device_map;
2108         char __iomem *iop_firm_model = (char __iomem *)(&reg->message_rwbuffer[15]);
2109         char __iomem *iop_firm_version = (char __iomem *)(&reg->message_rwbuffer[17]);
2110         char __iomem *iop_device_map = (char __iomem *)(&reg->message_rwbuffer[21]);
2111         int count;
2112         writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
2113         if (!arcmsr_hba_wait_msgint_ready(acb)) {
2114                 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
2115                         miscellaneous data' timeout \n", acb->host->host_no);
2116                 return false;
2117         }
2118         count = 8;
2119         while (count){
2120                 *acb_firm_model = readb(iop_firm_model);
2121                 acb_firm_model++;
2122                 iop_firm_model++;
2123                 count--;
2124         }
2125
2126         count = 16;
2127         while (count){
2128                 *acb_firm_version = readb(iop_firm_version);
2129                 acb_firm_version++;
2130                 iop_firm_version++;
2131                 count--;
2132         }
2133
2134         count=16;
2135         while(count){
2136                 *acb_device_map = readb(iop_device_map);
2137                 acb_device_map++;
2138                 iop_device_map++;
2139                 count--;
2140         }
2141         printk(KERN_NOTICE "Areca RAID Controller%d: F/W %s & Model %s\n", 
2142                 acb->host->host_no,
2143                 acb->firm_version,
2144                 acb->firm_model);
2145         acb->signature = readl(&reg->message_rwbuffer[0]);
2146         acb->firm_request_len = readl(&reg->message_rwbuffer[1]);
2147         acb->firm_numbers_queue = readl(&reg->message_rwbuffer[2]);
2148         acb->firm_sdram_size = readl(&reg->message_rwbuffer[3]);
2149         acb->firm_hd_channels = readl(&reg->message_rwbuffer[4]);
2150         acb->firm_cfg_version = readl(&reg->message_rwbuffer[25]);  /*firm_cfg_version,25,100-103*/
2151         return true;
2152 }
2153 static bool arcmsr_get_hbb_config(struct AdapterControlBlock *acb)
2154 {
2155         struct MessageUnit_B *reg = acb->pmuB;
2156         struct pci_dev *pdev = acb->pdev;
2157         void *dma_coherent;
2158         dma_addr_t dma_coherent_handle;
2159         char *acb_firm_model = acb->firm_model;
2160         char *acb_firm_version = acb->firm_version;
2161         char *acb_device_map = acb->device_map;
2162         char __iomem *iop_firm_model;
2163         /*firm_model,15,60-67*/
2164         char __iomem *iop_firm_version;
2165         /*firm_version,17,68-83*/
2166         char __iomem *iop_device_map;
2167         /*firm_version,21,84-99*/
2168         int count;
2169         dma_coherent = dma_alloc_coherent(&pdev->dev, sizeof(struct MessageUnit_B), &dma_coherent_handle, GFP_KERNEL);
2170         if (!dma_coherent){
2171                 printk(KERN_NOTICE "arcmsr%d: dma_alloc_coherent got error for hbb mu\n", acb->host->host_no);
2172                 return false;
2173         }
2174         acb->dma_coherent_handle_hbb_mu = dma_coherent_handle;
2175         reg = (struct MessageUnit_B *)dma_coherent;
2176         acb->pmuB = reg;
2177         reg->drv2iop_doorbell= (uint32_t __iomem *)((unsigned long)acb->mem_base0 + ARCMSR_DRV2IOP_DOORBELL);
2178         reg->drv2iop_doorbell_mask = (uint32_t __iomem *)((unsigned long)acb->mem_base0 + ARCMSR_DRV2IOP_DOORBELL_MASK);
2179         reg->iop2drv_doorbell = (uint32_t __iomem *)((unsigned long)acb->mem_base0 + ARCMSR_IOP2DRV_DOORBELL);
2180         reg->iop2drv_doorbell_mask = (uint32_t __iomem *)((unsigned long)acb->mem_base0 + ARCMSR_IOP2DRV_DOORBELL_MASK);
2181         reg->message_wbuffer = (uint32_t __iomem *)((unsigned long)acb->mem_base1 + ARCMSR_MESSAGE_WBUFFER);
2182         reg->message_rbuffer =  (uint32_t __iomem *)((unsigned long)acb->mem_base1 + ARCMSR_MESSAGE_RBUFFER);
2183         reg->message_rwbuffer = (uint32_t __iomem *)((unsigned long)acb->mem_base1 + ARCMSR_MESSAGE_RWBUFFER);
2184         iop_firm_model = (char __iomem *)(&reg->message_rwbuffer[15]);  /*firm_model,15,60-67*/
2185         iop_firm_version = (char __iomem *)(&reg->message_rwbuffer[17]);        /*firm_version,17,68-83*/
2186         iop_device_map = (char __iomem *)(&reg->message_rwbuffer[21]);  /*firm_version,21,84-99*/
2187
2188         writel(ARCMSR_MESSAGE_GET_CONFIG, reg->drv2iop_doorbell);
2189         if (!arcmsr_hbb_wait_msgint_ready(acb)) {
2190                 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
2191                         miscellaneous data' timeout \n", acb->host->host_no);
2192                 return false;
2193         }
2194         count = 8;
2195         while (count){
2196                 *acb_firm_model = readb(iop_firm_model);
2197                 acb_firm_model++;
2198                 iop_firm_model++;
2199                 count--;
2200         }
2201         count = 16;
2202         while (count){
2203                 *acb_firm_version = readb(iop_firm_version);
2204                 acb_firm_version++;
2205                 iop_firm_version++;
2206                 count--;
2207         }
2208
2209         count = 16;
2210         while(count){
2211                 *acb_device_map = readb(iop_device_map);
2212                 acb_device_map++;
2213                 iop_device_map++;
2214                 count--;
2215         }
2216         
2217         printk(KERN_NOTICE "Areca RAID Controller%d: F/W %s & Model %s\n",
2218                 acb->host->host_no,
2219                 acb->firm_version,
2220                 acb->firm_model);
2221
2222         acb->signature = readl(&reg->message_rwbuffer[1]);
2223         /*firm_signature,1,00-03*/
2224         acb->firm_request_len = readl(&reg->message_rwbuffer[2]);
2225         /*firm_request_len,1,04-07*/
2226         acb->firm_numbers_queue = readl(&reg->message_rwbuffer[3]);
2227         /*firm_numbers_queue,2,08-11*/
2228         acb->firm_sdram_size = readl(&reg->message_rwbuffer[4]);
2229         /*firm_sdram_size,3,12-15*/
2230         acb->firm_hd_channels = readl(&reg->message_rwbuffer[5]);
2231         /*firm_ide_channels,4,16-19*/
2232         acb->firm_cfg_version = readl(&reg->message_rwbuffer[25]);  /*firm_cfg_version,25,100-103*/
2233         /*firm_ide_channels,4,16-19*/
2234         return true;
2235 }
2236
2237 static bool arcmsr_get_hbc_config(struct AdapterControlBlock *pACB)
2238 {
2239         uint32_t intmask_org, Index, firmware_state = 0;
2240         struct MessageUnit_C *reg = pACB->pmuC;
2241         char *acb_firm_model = pACB->firm_model;
2242         char *acb_firm_version = pACB->firm_version;
2243         char *iop_firm_model = (char *)(&reg->msgcode_rwbuffer[15]);    /*firm_model,15,60-67*/
2244         char *iop_firm_version = (char *)(&reg->msgcode_rwbuffer[17]);  /*firm_version,17,68-83*/
2245         int count;
2246         /* disable all outbound interrupt */
2247         intmask_org = readl(&reg->host_int_mask); /* disable outbound message0 int */
2248         writel(intmask_org|ARCMSR_HBCMU_ALL_INTMASKENABLE, &reg->host_int_mask);
2249         /* wait firmware ready */
2250         do {
2251                 firmware_state = readl(&reg->outbound_msgaddr1);
2252         } while ((firmware_state & ARCMSR_HBCMU_MESSAGE_FIRMWARE_OK) == 0);
2253         /* post "get config" instruction */
2254         writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
2255         writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &reg->inbound_doorbell);
2256         /* wait message ready */
2257         for (Index = 0; Index < 2000; Index++) {
2258                 if (readl(&reg->outbound_doorbell) & ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE) {
2259                         writel(ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE_DOORBELL_CLEAR, &reg->outbound_doorbell_clear);/*clear interrupt*/
2260                         break;
2261                 }
2262                 udelay(10);
2263         } /*max 1 seconds*/
2264         if (Index >= 2000) {
2265                 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
2266                         miscellaneous data' timeout \n", pACB->host->host_no);
2267                 return false;
2268         }
2269         count = 8;
2270         while (count) {
2271                 *acb_firm_model = readb(iop_firm_model);
2272                 acb_firm_model++;
2273                 iop_firm_model++;
2274                 count--;
2275         }
2276         count = 16;
2277         while (count) {
2278                 *acb_firm_version = readb(iop_firm_version);
2279                 acb_firm_version++;
2280                 iop_firm_version++;
2281                 count--;
2282         }
2283         printk(KERN_NOTICE "Areca RAID Controller%d: F/W %s & Model %s\n",
2284                 pACB->host->host_no,
2285                 pACB->firm_version,
2286                 pACB->firm_model);
2287         pACB->firm_request_len = readl(&reg->msgcode_rwbuffer[1]);   /*firm_request_len,1,04-07*/
2288         pACB->firm_numbers_queue = readl(&reg->msgcode_rwbuffer[2]); /*firm_numbers_queue,2,08-11*/
2289         pACB->firm_sdram_size = readl(&reg->msgcode_rwbuffer[3]);    /*firm_sdram_size,3,12-15*/
2290         pACB->firm_hd_channels = readl(&reg->msgcode_rwbuffer[4]);  /*firm_ide_channels,4,16-19*/
2291         pACB->firm_cfg_version = readl(&reg->msgcode_rwbuffer[25]);  /*firm_cfg_version,25,100-103*/
2292         /*all interrupt service will be enable at arcmsr_iop_init*/
2293         return true;
2294 }
2295 static bool arcmsr_get_firmware_spec(struct AdapterControlBlock *acb)
2296 {
2297         if (acb->adapter_type == ACB_ADAPTER_TYPE_A)
2298                 return arcmsr_get_hba_config(acb);
2299         else if (acb->adapter_type == ACB_ADAPTER_TYPE_B)
2300                 return arcmsr_get_hbb_config(acb);
2301         else
2302                 return arcmsr_get_hbc_config(acb);
2303 }
2304
2305 static int arcmsr_polling_hba_ccbdone(struct AdapterControlBlock *acb,
2306         struct CommandControlBlock *poll_ccb)
2307 {
2308         struct MessageUnit_A __iomem *reg = acb->pmuA;
2309         struct CommandControlBlock *ccb;
2310         struct ARCMSR_CDB *arcmsr_cdb;
2311         uint32_t flag_ccb, outbound_intstatus, poll_ccb_done = 0, poll_count = 0;
2312         int rtn;
2313         bool error;
2314         polling_hba_ccb_retry:
2315         poll_count++;
2316         outbound_intstatus = readl(&reg->outbound_intstatus) & acb->outbound_int_enable;
2317         writel(outbound_intstatus, &reg->outbound_intstatus);/*clear interrupt*/
2318         while (1) {
2319                 if ((flag_ccb = readl(&reg->outbound_queueport)) == 0xFFFFFFFF) {
2320                         if (poll_ccb_done){
2321                                 rtn = SUCCESS;
2322                                 break;
2323                         }else {
2324                                 msleep(25);
2325                                 if (poll_count > 100){
2326                                         rtn = FAILED;
2327                                         break;
2328                                 }
2329                                 goto polling_hba_ccb_retry;
2330                         }
2331                 }
2332                 arcmsr_cdb = (struct ARCMSR_CDB *)(acb->vir2phy_offset + (flag_ccb << 5));
2333                 ccb = container_of(arcmsr_cdb, struct CommandControlBlock, arcmsr_cdb);
2334                 poll_ccb_done = (ccb == poll_ccb) ? 1:0;
2335                 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
2336                         if ((ccb->startdone == ARCMSR_CCB_ABORTED) || (ccb == poll_ccb)) {
2337                                 printk(KERN_NOTICE "arcmsr%d: scsi id = %d lun = %d ccb = '0x%p'"
2338                                         " poll command abort successfully \n"
2339                                         , acb->host->host_no
2340                                         , ccb->pcmd->device->id
2341                                         , ccb->pcmd->device->lun
2342                                         , ccb);
2343                                 ccb->pcmd->result = DID_ABORT << 16;
2344                                 arcmsr_ccb_complete(ccb);
2345                                 continue;
2346                         }
2347                         printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
2348                                 " command done ccb = '0x%p'"
2349                                 "ccboutstandingcount = %d \n"
2350                                 , acb->host->host_no
2351                                 , ccb
2352                                 , atomic_read(&acb->ccboutstandingcount));
2353                         continue;
2354                 }
2355                 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE0) ? true : false;
2356                 arcmsr_report_ccb_state(acb, ccb, error);
2357         }
2358         return rtn;
2359 }
2360
2361 static int arcmsr_polling_hbb_ccbdone(struct AdapterControlBlock *acb,
2362                                         struct CommandControlBlock *poll_ccb)
2363 {
2364         struct MessageUnit_B *reg = acb->pmuB;
2365         struct ARCMSR_CDB *arcmsr_cdb;
2366         struct CommandControlBlock *ccb;
2367         uint32_t flag_ccb, poll_ccb_done = 0, poll_count = 0;
2368         int index, rtn;
2369         bool error;
2370         polling_hbb_ccb_retry:
2371
2372         poll_count++;
2373         /* clear doorbell interrupt */
2374         writel(ARCMSR_DOORBELL_INT_CLEAR_PATTERN, reg->iop2drv_doorbell);
2375         while(1){
2376                 index = reg->doneq_index;
2377                 if ((flag_ccb = readl(&reg->done_qbuffer[index])) == 0) {
2378                         if (poll_ccb_done){
2379                                 rtn = SUCCESS;
2380                                 break;
2381                         }else {
2382                                 msleep(25);
2383                                 if (poll_count > 100){
2384                                         rtn = FAILED;
2385                                         break;
2386                                 }
2387                                 goto polling_hbb_ccb_retry;
2388                         }
2389                 }
2390                 writel(0, &reg->done_qbuffer[index]);
2391                 index++;
2392                 /*if last index number set it to 0 */
2393                 index %= ARCMSR_MAX_HBB_POSTQUEUE;
2394                 reg->doneq_index = index;
2395                 /* check if command done with no error*/
2396                 arcmsr_cdb = (struct ARCMSR_CDB *)(acb->vir2phy_offset + (flag_ccb << 5));
2397                 ccb = container_of(arcmsr_cdb, struct CommandControlBlock, arcmsr_cdb);
2398                 poll_ccb_done = (ccb == poll_ccb) ? 1:0;
2399                 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
2400                         if ((ccb->startdone == ARCMSR_CCB_ABORTED) || (ccb == poll_ccb)) {
2401                                 printk(KERN_NOTICE "arcmsr%d: scsi id = %d lun = %d ccb = '0x%p'"
2402                                         " poll command abort successfully \n"
2403                                         ,acb->host->host_no
2404                                         ,ccb->pcmd->device->id
2405                                         ,ccb->pcmd->device->lun
2406                                         ,ccb);
2407                                 ccb->pcmd->result = DID_ABORT << 16;
2408                                 arcmsr_ccb_complete(ccb);
2409                                 continue;
2410                         }
2411                         printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
2412                                 " command done ccb = '0x%p'"
2413                                 "ccboutstandingcount = %d \n"
2414                                 , acb->host->host_no
2415                                 , ccb
2416                                 , atomic_read(&acb->ccboutstandingcount));
2417                         continue;
2418                 } 
2419                 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE0) ? true : false;
2420                 arcmsr_report_ccb_state(acb, ccb, error);
2421         }
2422         return rtn;
2423 }
2424
2425 static int arcmsr_polling_hbc_ccbdone(struct AdapterControlBlock *acb, struct CommandControlBlock *poll_ccb)
2426 {
2427         struct MessageUnit_C *reg = (struct MessageUnit_C *)acb->pmuC;
2428         uint32_t flag_ccb, ccb_cdb_phy;
2429         struct ARCMSR_CDB *arcmsr_cdb;
2430         bool error;
2431         struct CommandControlBlock *pCCB;
2432         uint32_t poll_ccb_done = 0, poll_count = 0;
2433         int rtn;
2434 polling_hbc_ccb_retry:
2435         poll_count++;
2436         while (1) {
2437                 if ((readl(&reg->host_int_status) & ARCMSR_HBCMU_OUTBOUND_POSTQUEUE_ISR) == 0) {
2438                         if (poll_ccb_done) {
2439                                 rtn = SUCCESS;
2440                                 break;
2441                         } else {
2442                                 msleep(25);
2443                                 if (poll_count > 100) {
2444                                         rtn = FAILED;
2445                                         break;
2446                                 }
2447                                 goto polling_hbc_ccb_retry;
2448                         }
2449                 }
2450                 flag_ccb = readl(&reg->outbound_queueport_low);
2451                 ccb_cdb_phy = (flag_ccb & 0xFFFFFFF0);
2452                 arcmsr_cdb = (struct ARCMSR_CDB *)(acb->vir2phy_offset + ccb_cdb_phy);/*frame must be 32 bytes aligned*/
2453                 pCCB = container_of(arcmsr_cdb, struct CommandControlBlock, arcmsr_cdb);
2454                 poll_ccb_done = (pCCB == poll_ccb) ? 1 : 0;
2455                 /* check ifcommand done with no error*/
2456                 if ((pCCB->acb != acb) || (pCCB->startdone != ARCMSR_CCB_START)) {
2457                         if (pCCB->startdone == ARCMSR_CCB_ABORTED) {
2458                                 printk(KERN_NOTICE "arcmsr%d: scsi id = %d lun = %d ccb = '0x%p'"
2459                                         " poll command abort successfully \n"
2460                                         , acb->host->host_no
2461                                         , pCCB->pcmd->device->id
2462                                         , pCCB->pcmd->device->lun
2463                                         , pCCB);
2464                                         pCCB->pcmd->result = DID_ABORT << 16;
2465                                         arcmsr_ccb_complete(pCCB);
2466                                 continue;
2467                         }
2468                         printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
2469                                 " command done ccb = '0x%p'"
2470                                 "ccboutstandingcount = %d \n"
2471                                 , acb->host->host_no
2472                                 , pCCB
2473                                 , atomic_read(&acb->ccboutstandingcount));
2474                         continue;
2475                 }
2476                 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE1) ? true : false;
2477                 arcmsr_report_ccb_state(acb, pCCB, error);
2478         }
2479         return rtn;
2480 }
2481 static int arcmsr_polling_ccbdone(struct AdapterControlBlock *acb,
2482                                         struct CommandControlBlock *poll_ccb)
2483 {
2484         int rtn = 0;
2485         switch (acb->adapter_type) {
2486
2487         case ACB_ADAPTER_TYPE_A: {
2488                 rtn = arcmsr_polling_hba_ccbdone(acb, poll_ccb);
2489                 }
2490                 break;
2491
2492         case ACB_ADAPTER_TYPE_B: {
2493                 rtn = arcmsr_polling_hbb_ccbdone(acb, poll_ccb);
2494                 }
2495                 break;
2496         case ACB_ADAPTER_TYPE_C: {
2497                 rtn = arcmsr_polling_hbc_ccbdone(acb, poll_ccb);
2498                 }
2499         }
2500         return rtn;
2501 }
2502
2503 static int arcmsr_iop_confirm(struct AdapterControlBlock *acb)
2504 {
2505         uint32_t cdb_phyaddr, cdb_phyaddr_hi32;
2506         dma_addr_t dma_coherent_handle;
2507         /*
2508         ********************************************************************
2509         ** here we need to tell iop 331 our freeccb.HighPart
2510         ** if freeccb.HighPart is not zero
2511         ********************************************************************
2512         */
2513         dma_coherent_handle = acb->dma_coherent_handle;
2514         cdb_phyaddr = (uint32_t)(dma_coherent_handle);
2515         cdb_phyaddr_hi32 = (uint32_t)((cdb_phyaddr >> 16) >> 16);
2516         acb->cdb_phyaddr_hi32 = cdb_phyaddr_hi32;
2517         /*
2518         ***********************************************************************
2519         **    if adapter type B, set window of "post command Q"
2520         ***********************************************************************
2521         */
2522         switch (acb->adapter_type) {
2523
2524         case ACB_ADAPTER_TYPE_A: {
2525                 if (cdb_phyaddr_hi32 != 0) {
2526                         struct MessageUnit_A __iomem *reg = acb->pmuA;
2527                         uint32_t intmask_org;
2528                         intmask_org = arcmsr_disable_outbound_ints(acb);
2529                         writel(ARCMSR_SIGNATURE_SET_CONFIG, \
2530                                                 &reg->message_rwbuffer[0]);
2531                         writel(cdb_phyaddr_hi32, &reg->message_rwbuffer[1]);
2532                         writel(ARCMSR_INBOUND_MESG0_SET_CONFIG, \
2533                                                         &reg->inbound_msgaddr0);
2534                         if (!arcmsr_hba_wait_msgint_ready(acb)) {
2535                                 printk(KERN_NOTICE "arcmsr%d: ""set ccb high \
2536                                 part physical address timeout\n",
2537                                 acb->host->host_no);
2538                                 return 1;
2539                         }
2540                         arcmsr_enable_outbound_ints(acb, intmask_org);
2541                 }
2542                 }
2543                 break;
2544
2545         case ACB_ADAPTER_TYPE_B: {
2546                 unsigned long post_queue_phyaddr;
2547                 uint32_t __iomem *rwbuffer;
2548
2549                 struct MessageUnit_B *reg = acb->pmuB;
2550                 uint32_t intmask_org;
2551                 intmask_org = arcmsr_disable_outbound_ints(acb);
2552                 reg->postq_index = 0;
2553                 reg->doneq_index = 0;
2554                 writel(ARCMSR_MESSAGE_SET_POST_WINDOW, reg->drv2iop_doorbell);
2555                 if (!arcmsr_hbb_wait_msgint_ready(acb)) {
2556                         printk(KERN_NOTICE "arcmsr%d:can not set diver mode\n", \
2557                                 acb->host->host_no);
2558                         return 1;
2559                 }
2560                 post_queue_phyaddr = acb->dma_coherent_handle_hbb_mu;
2561                 rwbuffer = reg->message_rwbuffer;
2562                 /* driver "set config" signature */
2563                 writel(ARCMSR_SIGNATURE_SET_CONFIG, rwbuffer++);
2564                 /* normal should be zero */
2565                 writel(cdb_phyaddr_hi32, rwbuffer++);
2566                 /* postQ size (256 + 8)*4        */
2567                 writel(post_queue_phyaddr, rwbuffer++);
2568                 /* doneQ size (256 + 8)*4        */
2569                 writel(post_queue_phyaddr + 1056, rwbuffer++);
2570                 /* ccb maxQ size must be --> [(256 + 8)*4]*/
2571                 writel(1056, rwbuffer);
2572
2573                 writel(ARCMSR_MESSAGE_SET_CONFIG, reg->drv2iop_doorbell);
2574                 if (!arcmsr_hbb_wait_msgint_ready(acb)) {
2575                         printk(KERN_NOTICE "arcmsr%d: 'set command Q window' \
2576                         timeout \n",acb->host->host_no);
2577                         return 1;
2578                 }
2579                 arcmsr_hbb_enable_driver_mode(acb);
2580                 arcmsr_enable_outbound_ints(acb, intmask_org);
2581                 }
2582                 break;
2583         case ACB_ADAPTER_TYPE_C: {
2584                 if (cdb_phyaddr_hi32 != 0) {
2585                         struct MessageUnit_C *reg = (struct MessageUnit_C *)acb->pmuC;
2586
2587                         printk(KERN_NOTICE "arcmsr%d: cdb_phyaddr_hi32=0x%x\n",
2588                                         acb->adapter_index, cdb_phyaddr_hi32);
2589                         writel(ARCMSR_SIGNATURE_SET_CONFIG, &reg->msgcode_rwbuffer[0]);
2590                         writel(cdb_phyaddr_hi32, &reg->msgcode_rwbuffer[1]);
2591                         writel(ARCMSR_INBOUND_MESG0_SET_CONFIG, &reg->inbound_msgaddr0);
2592                         writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &reg->inbound_doorbell);
2593                         if (!arcmsr_hbc_wait_msgint_ready(acb)) {
2594                                 printk(KERN_NOTICE "arcmsr%d: 'set command Q window' \
2595                                 timeout \n", acb->host->host_no);
2596                                 return 1;
2597                         }
2598                 }
2599                 }
2600         }
2601         return 0;
2602 }
2603
2604 static void arcmsr_wait_firmware_ready(struct AdapterControlBlock *acb)
2605 {
2606         uint32_t firmware_state = 0;
2607         switch (acb->adapter_type) {
2608
2609         case ACB_ADAPTER_TYPE_A: {
2610                 struct MessageUnit_A __iomem *reg = acb->pmuA;
2611                 do {
2612                         firmware_state = readl(&reg->outbound_msgaddr1);
2613                 } while ((firmware_state & ARCMSR_OUTBOUND_MESG1_FIRMWARE_OK) == 0);
2614                 }
2615                 break;
2616
2617         case ACB_ADAPTER_TYPE_B: {
2618                 struct MessageUnit_B *reg = acb->pmuB;
2619                 do {
2620                         firmware_state = readl(reg->iop2drv_doorbell);
2621                 } while ((firmware_state & ARCMSR_MESSAGE_FIRMWARE_OK) == 0);
2622                 writel(ARCMSR_DRV2IOP_END_OF_INTERRUPT, reg->drv2iop_doorbell);
2623                 }
2624                 break;
2625         case ACB_ADAPTER_TYPE_C: {
2626                 struct MessageUnit_C *reg = (struct MessageUnit_C *)acb->pmuC;
2627                 do {
2628                         firmware_state = readl(&reg->outbound_msgaddr1);
2629                 } while ((firmware_state & ARCMSR_HBCMU_MESSAGE_FIRMWARE_OK) == 0);
2630                 }
2631         }
2632 }
2633
2634 static void arcmsr_request_hba_device_map(struct AdapterControlBlock *acb)
2635 {
2636         struct MessageUnit_A __iomem *reg = acb->pmuA;
2637         if (unlikely(atomic_read(&acb->rq_map_token) == 0) || ((acb->acb_flags & ACB_F_BUS_RESET) != 0 ) || ((acb->acb_flags & ACB_F_ABORT) != 0 )){
2638                 mod_timer(&acb->eternal_timer, jiffies + msecs_to_jiffies(6 * HZ));
2639                 return;
2640         } else {
2641                 acb->fw_flag = FW_NORMAL;
2642                 if (atomic_read(&acb->ante_token_value) == atomic_read(&acb->rq_map_token)){
2643                         atomic_set(&acb->rq_map_token, 16);
2644                 }
2645                 atomic_set(&acb->ante_token_value, atomic_read(&acb->rq_map_token));
2646                 if (atomic_dec_and_test(&acb->rq_map_token)) {
2647                         mod_timer(&acb->eternal_timer, jiffies + msecs_to_jiffies(6 * HZ));
2648                         return;
2649                 }
2650                 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
2651                 mod_timer(&acb->eternal_timer, jiffies + msecs_to_jiffies(6 * HZ));
2652         }
2653         return;
2654 }
2655
2656 static void arcmsr_request_hbb_device_map(struct AdapterControlBlock *acb)
2657 {
2658         struct MessageUnit_B __iomem *reg = acb->pmuB;
2659         if (unlikely(atomic_read(&acb->rq_map_token) == 0) || ((acb->acb_flags & ACB_F_BUS_RESET) != 0 ) || ((acb->acb_flags & ACB_F_ABORT) != 0 )){
2660                 mod_timer(&acb->eternal_timer, jiffies + msecs_to_jiffies(6 * HZ));
2661                 return;
2662         } else {
2663                 acb->fw_flag = FW_NORMAL;
2664                 if (atomic_read(&acb->ante_token_value) == atomic_read(&acb->rq_map_token)) {
2665                         atomic_set(&acb->rq_map_token, 16);
2666                 }
2667                 atomic_set(&acb->ante_token_value, atomic_read(&acb->rq_map_token));
2668                 if (atomic_dec_and_test(&acb->rq_map_token)) {
2669                         mod_timer(&acb->eternal_timer, jiffies + msecs_to_jiffies(6 * HZ));
2670                         return;
2671                 }
2672                 writel(ARCMSR_MESSAGE_GET_CONFIG, reg->drv2iop_doorbell);
2673                 mod_timer(&acb->eternal_timer, jiffies + msecs_to_jiffies(6 * HZ));
2674         }
2675         return;
2676 }
2677
2678 static void arcmsr_request_hbc_device_map(struct AdapterControlBlock *acb)
2679 {
2680         struct MessageUnit_C __iomem *reg = acb->pmuC;
2681         if (unlikely(atomic_read(&acb->rq_map_token) == 0) || ((acb->acb_flags & ACB_F_BUS_RESET) != 0) || ((acb->acb_flags & ACB_F_ABORT) != 0)) {
2682                 mod_timer(&acb->eternal_timer, jiffies + msecs_to_jiffies(6 * HZ));
2683                 return;
2684         } else {
2685                 acb->fw_flag = FW_NORMAL;
2686                 if (atomic_read(&acb->ante_token_value) == atomic_read(&acb->rq_map_token)) {
2687                         atomic_set(&acb->rq_map_token, 16);
2688                 }
2689                 atomic_set(&acb->ante_token_value, atomic_read(&acb->rq_map_token));
2690                 if (atomic_dec_and_test(&acb->rq_map_token)) {
2691                         mod_timer(&acb->eternal_timer, jiffies + msecs_to_jiffies(6 * HZ));
2692                         return;
2693                 }
2694                 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
2695                 writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &reg->inbound_doorbell);
2696                 mod_timer(&acb->eternal_timer, jiffies + msecs_to_jiffies(6 * HZ));
2697         }
2698         return;
2699 }
2700
2701 static void arcmsr_request_device_map(unsigned long pacb)
2702 {
2703         struct AdapterControlBlock *acb = (struct AdapterControlBlock *)pacb;
2704         switch (acb->adapter_type) {
2705                 case ACB_ADAPTER_TYPE_A: {
2706                         arcmsr_request_hba_device_map(acb);
2707                 }
2708                 break;
2709                 case ACB_ADAPTER_TYPE_B: {
2710                         arcmsr_request_hbb_device_map(acb);
2711                 }
2712                 break;
2713                 case ACB_ADAPTER_TYPE_C: {
2714                         arcmsr_request_hbc_device_map(acb);
2715                 }
2716         }
2717 }
2718
2719 static void arcmsr_start_hba_bgrb(struct AdapterControlBlock *acb)
2720 {
2721         struct MessageUnit_A __iomem *reg = acb->pmuA;
2722         acb->acb_flags |= ACB_F_MSG_START_BGRB;
2723         writel(ARCMSR_INBOUND_MESG0_START_BGRB, &reg->inbound_msgaddr0);
2724         if (!arcmsr_hba_wait_msgint_ready(acb)) {
2725                 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
2726                                 rebulid' timeout \n", acb->host->host_no);
2727         }
2728 }
2729
2730 static void arcmsr_start_hbb_bgrb(struct AdapterControlBlock *acb)
2731 {
2732         struct MessageUnit_B *reg = acb->pmuB;
2733         acb->acb_flags |= ACB_F_MSG_START_BGRB;
2734         writel(ARCMSR_MESSAGE_START_BGRB, reg->drv2iop_doorbell);
2735         if (!arcmsr_hbb_wait_msgint_ready(acb)) {
2736                 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
2737                                 rebulid' timeout \n",acb->host->host_no);
2738         }
2739 }
2740
2741 static void arcmsr_start_hbc_bgrb(struct AdapterControlBlock *pACB)
2742 {
2743         struct MessageUnit_C *phbcmu = (struct MessageUnit_C *)pACB->pmuC;
2744         pACB->acb_flags |= ACB_F_MSG_START_BGRB;
2745         writel(ARCMSR_INBOUND_MESG0_START_BGRB, &phbcmu->inbound_msgaddr0);
2746         writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &phbcmu->inbound_doorbell);
2747         if (!arcmsr_hbc_wait_msgint_ready(pACB)) {
2748                 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
2749                                 rebulid' timeout \n", pACB->host->host_no);
2750         }
2751         return;
2752 }
2753 static void arcmsr_start_adapter_bgrb(struct AdapterControlBlock *acb)
2754 {
2755         switch (acb->adapter_type) {
2756         case ACB_ADAPTER_TYPE_A:
2757                 arcmsr_start_hba_bgrb(acb);
2758                 break;
2759         case ACB_ADAPTER_TYPE_B:
2760                 arcmsr_start_hbb_bgrb(acb);
2761                 break;
2762         case ACB_ADAPTER_TYPE_C:
2763                 arcmsr_start_hbc_bgrb(acb);
2764         }
2765 }
2766
2767 static void arcmsr_clear_doorbell_queue_buffer(struct AdapterControlBlock *acb)
2768 {
2769         switch (acb->adapter_type) {
2770         case ACB_ADAPTER_TYPE_A: {
2771                 struct MessageUnit_A __iomem *reg = acb->pmuA;
2772                 uint32_t outbound_doorbell;
2773                 /* empty doorbell Qbuffer if door bell ringed */
2774                 outbound_doorbell = readl(&reg->outbound_doorbell);
2775                 /*clear doorbell interrupt */
2776                 writel(outbound_doorbell, &reg->outbound_doorbell);
2777                 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, &reg->inbound_doorbell);
2778                 }
2779                 break;
2780
2781         case ACB_ADAPTER_TYPE_B: {
2782                 struct MessageUnit_B *reg = acb->pmuB;
2783                 /*clear interrupt and message state*/
2784                 writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN, reg->iop2drv_doorbell);
2785                 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell);
2786                 /* let IOP know data has been read */
2787                 }
2788                 break;
2789         case ACB_ADAPTER_TYPE_C: {
2790                 struct MessageUnit_C *reg = (struct MessageUnit_C *)acb->pmuC;
2791                 uint32_t outbound_doorbell;
2792                 /* empty doorbell Qbuffer if door bell ringed */
2793                 outbound_doorbell = readl(&reg->outbound_doorbell);
2794                 writel(outbound_doorbell, &reg->outbound_doorbell_clear);
2795                 writel(ARCMSR_HBCMU_DRV2IOP_DATA_READ_OK, &reg->inbound_doorbell);
2796                 }
2797         }
2798 }
2799
2800 static void arcmsr_enable_eoi_mode(struct AdapterControlBlock *acb)
2801 {
2802         switch (acb->adapter_type) {
2803         case ACB_ADAPTER_TYPE_A:
2804                 return;
2805         case ACB_ADAPTER_TYPE_B:
2806                 {
2807                         struct MessageUnit_B *reg = acb->pmuB;
2808                         writel(ARCMSR_MESSAGE_ACTIVE_EOI_MODE, reg->drv2iop_doorbell);
2809                         if (!arcmsr_hbb_wait_msgint_ready(acb)) {
2810                                 printk(KERN_NOTICE "ARCMSR IOP enables EOI_MODE TIMEOUT");
2811                                 return;
2812                         }
2813                 }
2814                 break;
2815         case ACB_ADAPTER_TYPE_C:
2816                 return;
2817         }
2818         return;
2819 }
2820
2821 static void arcmsr_hardware_reset(struct AdapterControlBlock *acb)
2822 {
2823         uint8_t value[64];
2824         int i, count = 0;
2825         struct MessageUnit_A __iomem *pmuA = acb->pmuA;
2826         struct MessageUnit_C __iomem *pmuC = acb->pmuC;
2827         u32 temp = 0;
2828         /* backup pci config data */
2829         printk(KERN_NOTICE "arcmsr%d: executing hw bus reset .....\n", acb->host->host_no);
2830         for (i = 0; i < 64; i++) {
2831                 pci_read_config_byte(acb->pdev, i, &value[i]);
2832         }
2833         /* hardware reset signal */
2834         if ((acb->dev_id == 0x1680)) {
2835                 writel(ARCMSR_ARC1680_BUS_RESET, &pmuA->reserved1[0]);
2836         } else if ((acb->dev_id == 0x1880)) {
2837                 do {
2838                         count++;
2839                         writel(0xF, &pmuC->write_sequence);
2840                         writel(0x4, &pmuC->write_sequence);
2841                         writel(0xB, &pmuC->write_sequence);
2842                         writel(0x2, &pmuC->write_sequence);
2843                         writel(0x7, &pmuC->write_sequence);
2844                         writel(0xD, &pmuC->write_sequence);
2845                 } while ((((temp = readl(&pmuC->host_diagnostic)) | ARCMSR_ARC1880_DiagWrite_ENABLE) == 0) && (count < 5));
2846                 writel(ARCMSR_ARC1880_RESET_ADAPTER, &pmuC->host_diagnostic);
2847         } else {
2848                 pci_write_config_byte(acb->pdev, 0x84, 0x20);
2849         }
2850         msleep(2000);
2851         /* write back pci config data */
2852         for (i = 0; i < 64; i++) {
2853                 pci_write_config_byte(acb->pdev, i, value[i]);
2854         }
2855         msleep(1000);
2856         return;
2857 }
2858 static void arcmsr_iop_init(struct AdapterControlBlock *acb)