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