xref: /openbmc/linux/drivers/scsi/arcmsr/arcmsr_hba.c (revision 643d1f7f)
1 /*
2 *******************************************************************************
3 **        O.S   : Linux
4 **   FILE NAME  : arcmsr_hba.c
5 **        BY    : Erich Chen
6 **   Description: SCSI RAID Device Driver for
7 **                ARECA RAID Host adapter
8 *******************************************************************************
9 ** Copyright (C) 2002 - 2005, Areca Technology Corporation All rights reserved
10 **
11 **     Web site: www.areca.com.tw
12 **       E-mail: 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/pci.h>
60 #include <linux/aer.h>
61 #include <asm/dma.h>
62 #include <asm/io.h>
63 #include <asm/system.h>
64 #include <asm/uaccess.h>
65 #include <scsi/scsi_host.h>
66 #include <scsi/scsi.h>
67 #include <scsi/scsi_cmnd.h>
68 #include <scsi/scsi_tcq.h>
69 #include <scsi/scsi_device.h>
70 #include <scsi/scsi_transport.h>
71 #include <scsi/scsicam.h>
72 #include "arcmsr.h"
73 
74 MODULE_AUTHOR("Erich Chen <support@areca.com.tw>");
75 MODULE_DESCRIPTION("ARECA (ARC11xx/12xx/13xx/16xx) SATA/SAS RAID HOST Adapter");
76 MODULE_LICENSE("Dual BSD/GPL");
77 MODULE_VERSION(ARCMSR_DRIVER_VERSION);
78 
79 static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb,
80 					struct scsi_cmnd *cmd);
81 static int arcmsr_iop_confirm(struct AdapterControlBlock *acb);
82 static int arcmsr_abort(struct scsi_cmnd *);
83 static int arcmsr_bus_reset(struct scsi_cmnd *);
84 static int arcmsr_bios_param(struct scsi_device *sdev,
85 		struct block_device *bdev, sector_t capacity, int *info);
86 static int arcmsr_queue_command(struct scsi_cmnd *cmd,
87 					void (*done) (struct scsi_cmnd *));
88 static int arcmsr_probe(struct pci_dev *pdev,
89 				const struct pci_device_id *id);
90 static void arcmsr_remove(struct pci_dev *pdev);
91 static void arcmsr_shutdown(struct pci_dev *pdev);
92 static void arcmsr_iop_init(struct AdapterControlBlock *acb);
93 static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb);
94 static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb);
95 static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb);
96 static void arcmsr_flush_hba_cache(struct AdapterControlBlock *acb);
97 static void arcmsr_flush_hbb_cache(struct AdapterControlBlock *acb);
98 static const char *arcmsr_info(struct Scsi_Host *);
99 static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb);
100 static int arcmsr_adjust_disk_queue_depth(struct scsi_device *sdev,
101 								int queue_depth)
102 {
103 	if (queue_depth > ARCMSR_MAX_CMD_PERLUN)
104 		queue_depth = ARCMSR_MAX_CMD_PERLUN;
105 	scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
106 	return queue_depth;
107 }
108 
109 static struct scsi_host_template arcmsr_scsi_host_template = {
110 	.module			= THIS_MODULE,
111 	.name			= "ARCMSR ARECA SATA/SAS RAID HOST Adapter"
112 							ARCMSR_DRIVER_VERSION,
113 	.info			= arcmsr_info,
114 	.queuecommand		= arcmsr_queue_command,
115 	.eh_abort_handler	= arcmsr_abort,
116 	.eh_bus_reset_handler	= arcmsr_bus_reset,
117 	.bios_param		= arcmsr_bios_param,
118 	.change_queue_depth	= arcmsr_adjust_disk_queue_depth,
119 	.can_queue		= ARCMSR_MAX_OUTSTANDING_CMD,
120 	.this_id		= ARCMSR_SCSI_INITIATOR_ID,
121 	.sg_tablesize		= ARCMSR_MAX_SG_ENTRIES,
122 	.max_sectors    	= ARCMSR_MAX_XFER_SECTORS,
123 	.cmd_per_lun		= ARCMSR_MAX_CMD_PERLUN,
124 	.use_clustering		= ENABLE_CLUSTERING,
125 	.shost_attrs		= arcmsr_host_attrs,
126 };
127 #ifdef CONFIG_SCSI_ARCMSR_AER
128 static pci_ers_result_t arcmsr_pci_slot_reset(struct pci_dev *pdev);
129 static pci_ers_result_t arcmsr_pci_error_detected(struct pci_dev *pdev,
130 						pci_channel_state_t state);
131 
132 static struct pci_error_handlers arcmsr_pci_error_handlers = {
133 	.error_detected		= arcmsr_pci_error_detected,
134 	.slot_reset		= arcmsr_pci_slot_reset,
135 };
136 #endif
137 static struct pci_device_id arcmsr_device_id_table[] = {
138 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1110)},
139 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1120)},
140 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1130)},
141 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1160)},
142 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1170)},
143 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1200)},
144 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1201)},
145 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1202)},
146 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1210)},
147 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1220)},
148 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1230)},
149 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1260)},
150 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1270)},
151 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1280)},
152 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1380)},
153 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1381)},
154 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1680)},
155 	{PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1681)},
156 	{0, 0}, /* Terminating entry */
157 };
158 MODULE_DEVICE_TABLE(pci, arcmsr_device_id_table);
159 static struct pci_driver arcmsr_pci_driver = {
160 	.name			= "arcmsr",
161 	.id_table		= arcmsr_device_id_table,
162 	.probe			= arcmsr_probe,
163 	.remove			= arcmsr_remove,
164 	.shutdown		= arcmsr_shutdown,
165 	#ifdef CONFIG_SCSI_ARCMSR_AER
166 	.err_handler		= &arcmsr_pci_error_handlers,
167 	#endif
168 };
169 
170 static irqreturn_t arcmsr_do_interrupt(int irq, void *dev_id)
171 {
172 	irqreturn_t handle_state;
173 	struct AdapterControlBlock *acb = dev_id;
174 
175 	spin_lock(acb->host->host_lock);
176 	handle_state = arcmsr_interrupt(acb);
177 	spin_unlock(acb->host->host_lock);
178 
179 	return handle_state;
180 }
181 
182 static int arcmsr_bios_param(struct scsi_device *sdev,
183 		struct block_device *bdev, sector_t capacity, int *geom)
184 {
185 	int ret, heads, sectors, cylinders, total_capacity;
186 	unsigned char *buffer;/* return copy of block device's partition table */
187 
188 	buffer = scsi_bios_ptable(bdev);
189 	if (buffer) {
190 		ret = scsi_partsize(buffer, capacity, &geom[2], &geom[0], &geom[1]);
191 		kfree(buffer);
192 		if (ret != -1)
193 			return ret;
194 	}
195 	total_capacity = capacity;
196 	heads = 64;
197 	sectors = 32;
198 	cylinders = total_capacity / (heads * sectors);
199 	if (cylinders > 1024) {
200 		heads = 255;
201 		sectors = 63;
202 		cylinders = total_capacity / (heads * sectors);
203 	}
204 	geom[0] = heads;
205 	geom[1] = sectors;
206 	geom[2] = cylinders;
207 	return 0;
208 }
209 
210 static void arcmsr_define_adapter_type(struct AdapterControlBlock *acb)
211 {
212 	struct pci_dev *pdev = acb->pdev;
213 	u16 dev_id;
214 	pci_read_config_word(pdev, PCI_DEVICE_ID, &dev_id);
215 	switch (dev_id) {
216 	case 0x1201 : {
217 		acb->adapter_type = ACB_ADAPTER_TYPE_B;
218 		}
219 		break;
220 
221 	default : acb->adapter_type = ACB_ADAPTER_TYPE_A;
222 	}
223 }
224 
225 static int arcmsr_alloc_ccb_pool(struct AdapterControlBlock *acb)
226 {
227 
228 	switch (acb->adapter_type) {
229 
230 	case ACB_ADAPTER_TYPE_A: {
231 		struct pci_dev *pdev = acb->pdev;
232 		void *dma_coherent;
233 		dma_addr_t dma_coherent_handle, dma_addr;
234 		struct CommandControlBlock *ccb_tmp;
235 		uint32_t intmask_org;
236 		int i, j;
237 
238 		acb->pmuA = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
239 		if (!acb->pmuA) {
240 			printk(KERN_NOTICE "arcmsr%d: memory mapping region fail \n",
241 							acb->host->host_no);
242 			return -ENOMEM;
243 		}
244 
245 		dma_coherent = dma_alloc_coherent(&pdev->dev,
246 			ARCMSR_MAX_FREECCB_NUM *
247 			sizeof (struct CommandControlBlock) + 0x20,
248 			&dma_coherent_handle, GFP_KERNEL);
249 
250 		if (!dma_coherent) {
251 			iounmap(acb->pmuA);
252 			return -ENOMEM;
253 		}
254 
255 		acb->dma_coherent = dma_coherent;
256 		acb->dma_coherent_handle = dma_coherent_handle;
257 
258 		if (((unsigned long)dma_coherent & 0x1F)) {
259 			dma_coherent = dma_coherent +
260 				(0x20 - ((unsigned long)dma_coherent & 0x1F));
261 			dma_coherent_handle = dma_coherent_handle +
262 				(0x20 - ((unsigned long)dma_coherent_handle & 0x1F));
263 		}
264 
265 		dma_addr = dma_coherent_handle;
266 		ccb_tmp = (struct CommandControlBlock *)dma_coherent;
267 		for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
268 			ccb_tmp->cdb_shifted_phyaddr = dma_addr >> 5;
269 			ccb_tmp->acb = acb;
270 			acb->pccb_pool[i] = ccb_tmp;
271 			list_add_tail(&ccb_tmp->list, &acb->ccb_free_list);
272 			dma_addr = dma_addr + sizeof(struct CommandControlBlock);
273 			ccb_tmp++;
274 		}
275 
276 		acb->vir2phy_offset = (unsigned long)ccb_tmp -(unsigned long)dma_addr;
277 		for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
278 			for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
279 				acb->devstate[i][j] = ARECA_RAID_GONE;
280 
281 		/*
282 		** here we need to tell iop 331 our ccb_tmp.HighPart
283 		** if ccb_tmp.HighPart is not zero
284 		*/
285 		intmask_org = arcmsr_disable_outbound_ints(acb);
286 		}
287 		break;
288 
289 	case ACB_ADAPTER_TYPE_B: {
290 
291 		struct pci_dev *pdev = acb->pdev;
292 		struct MessageUnit_B *reg;
293 		void __iomem *mem_base0, *mem_base1;
294 		void *dma_coherent;
295 		dma_addr_t dma_coherent_handle, dma_addr;
296 		uint32_t intmask_org;
297 		struct CommandControlBlock *ccb_tmp;
298 		int i, j;
299 
300 		dma_coherent = dma_alloc_coherent(&pdev->dev,
301 			((ARCMSR_MAX_FREECCB_NUM *
302 			sizeof(struct CommandControlBlock) + 0x20) +
303 			sizeof(struct MessageUnit_B)),
304 			&dma_coherent_handle, GFP_KERNEL);
305 		if (!dma_coherent)
306 			return -ENOMEM;
307 
308 		acb->dma_coherent = dma_coherent;
309 		acb->dma_coherent_handle = dma_coherent_handle;
310 
311 		if (((unsigned long)dma_coherent & 0x1F)) {
312 			dma_coherent = dma_coherent +
313 				(0x20 - ((unsigned long)dma_coherent & 0x1F));
314 			dma_coherent_handle = dma_coherent_handle +
315 				(0x20 - ((unsigned long)dma_coherent_handle & 0x1F));
316 		}
317 
318 		reg = (struct MessageUnit_B *)(dma_coherent +
319 		ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock));
320 
321 		dma_addr = dma_coherent_handle;
322 		ccb_tmp = (struct CommandControlBlock *)dma_coherent;
323 		for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
324 			ccb_tmp->cdb_shifted_phyaddr = dma_addr >> 5;
325 			ccb_tmp->acb = acb;
326 			acb->pccb_pool[i] = ccb_tmp;
327 			list_add_tail(&ccb_tmp->list, &acb->ccb_free_list);
328 			dma_addr = dma_addr + sizeof(struct CommandControlBlock);
329 			ccb_tmp++;
330 		}
331 
332 		reg = (struct MessageUnit_B *)(dma_coherent +
333 		ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock));
334 		acb->pmuB = reg;
335 		mem_base0 = ioremap(pci_resource_start(pdev, 0),
336 					pci_resource_len(pdev, 0));
337 		if (!mem_base0)
338 			goto out;
339 
340 		mem_base1 = ioremap(pci_resource_start(pdev, 2),
341 					pci_resource_len(pdev, 2));
342 		if (!mem_base1) {
343 			iounmap(mem_base0);
344 			goto out;
345 		}
346 
347 		reg->drv2iop_doorbell_reg = mem_base0 + ARCMSR_DRV2IOP_DOORBELL;
348 		reg->drv2iop_doorbell_mask_reg = mem_base0 +
349 						ARCMSR_DRV2IOP_DOORBELL_MASK;
350 		reg->iop2drv_doorbell_reg = mem_base0 + ARCMSR_IOP2DRV_DOORBELL;
351 		reg->iop2drv_doorbell_mask_reg = mem_base0 +
352 						ARCMSR_IOP2DRV_DOORBELL_MASK;
353 		reg->ioctl_wbuffer_reg = mem_base1 + ARCMSR_IOCTL_WBUFFER;
354 		reg->ioctl_rbuffer_reg = mem_base1 + ARCMSR_IOCTL_RBUFFER;
355 		reg->msgcode_rwbuffer_reg = mem_base1 + ARCMSR_MSGCODE_RWBUFFER;
356 
357 		acb->vir2phy_offset = (unsigned long)ccb_tmp -(unsigned long)dma_addr;
358 		for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
359 			for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
360 				acb->devstate[i][j] = ARECA_RAID_GOOD;
361 
362 		/*
363 		** here we need to tell iop 331 our ccb_tmp.HighPart
364 		** if ccb_tmp.HighPart is not zero
365 		*/
366 		intmask_org = arcmsr_disable_outbound_ints(acb);
367 		}
368 		break;
369 	}
370 	return 0;
371 
372 out:
373 	dma_free_coherent(&acb->pdev->dev,
374 		ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock) + 0x20,
375 		acb->dma_coherent, acb->dma_coherent_handle);
376 	return -ENOMEM;
377 }
378 
379 static int arcmsr_probe(struct pci_dev *pdev,
380 	const struct pci_device_id *id)
381 {
382 	struct Scsi_Host *host;
383 	struct AdapterControlBlock *acb;
384 	uint8_t bus, dev_fun;
385 	int error;
386 
387 	error = pci_enable_device(pdev);
388 	if (error)
389 		goto out;
390 	pci_set_master(pdev);
391 
392 	host = scsi_host_alloc(&arcmsr_scsi_host_template,
393 			sizeof(struct AdapterControlBlock));
394 	if (!host) {
395 		error = -ENOMEM;
396 		goto out_disable_device;
397 	}
398 	acb = (struct AdapterControlBlock *)host->hostdata;
399 	memset(acb, 0, sizeof (struct AdapterControlBlock));
400 
401 	error = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
402 	if (error) {
403 		error = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
404 		if (error) {
405 			printk(KERN_WARNING
406 			       "scsi%d: No suitable DMA mask available\n",
407 			       host->host_no);
408 			goto out_host_put;
409 		}
410 	}
411 	bus = pdev->bus->number;
412 	dev_fun = pdev->devfn;
413 	acb->host = host;
414 	acb->pdev = pdev;
415 	host->max_sectors = ARCMSR_MAX_XFER_SECTORS;
416 	host->max_lun = ARCMSR_MAX_TARGETLUN;
417 	host->max_id = ARCMSR_MAX_TARGETID;/*16:8*/
418 	host->max_cmd_len = 16;    /*this is issue of 64bit LBA, over 2T byte*/
419 	host->sg_tablesize = ARCMSR_MAX_SG_ENTRIES;
420 	host->can_queue = ARCMSR_MAX_FREECCB_NUM; /* max simultaneous cmds */
421 	host->cmd_per_lun = ARCMSR_MAX_CMD_PERLUN;
422 	host->this_id = ARCMSR_SCSI_INITIATOR_ID;
423 	host->unique_id = (bus << 8) | dev_fun;
424 	host->irq = pdev->irq;
425 	error = pci_request_regions(pdev, "arcmsr");
426 	if (error) {
427 		goto out_host_put;
428 	}
429 	arcmsr_define_adapter_type(acb);
430 
431 	acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
432 			   ACB_F_MESSAGE_RQBUFFER_CLEARED |
433 			   ACB_F_MESSAGE_WQBUFFER_READED);
434 	acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
435 	INIT_LIST_HEAD(&acb->ccb_free_list);
436 
437 	error = arcmsr_alloc_ccb_pool(acb);
438 	if (error)
439 		goto out_release_regions;
440 
441 	error = request_irq(pdev->irq, arcmsr_do_interrupt,
442 			    IRQF_SHARED, "arcmsr", acb);
443 	if (error)
444 		goto out_free_ccb_pool;
445 
446 	arcmsr_iop_init(acb);
447 	pci_set_drvdata(pdev, host);
448 	if (strncmp(acb->firm_version, "V1.42", 5) >= 0)
449 		host->max_sectors= ARCMSR_MAX_XFER_SECTORS_B;
450 
451 	error = scsi_add_host(host, &pdev->dev);
452 	if (error)
453 		goto out_free_irq;
454 
455 	error = arcmsr_alloc_sysfs_attr(acb);
456 	if (error)
457 		goto out_free_sysfs;
458 
459 	scsi_scan_host(host);
460 	#ifdef CONFIG_SCSI_ARCMSR_AER
461 	pci_enable_pcie_error_reporting(pdev);
462 	#endif
463 	return 0;
464  out_free_sysfs:
465  out_free_irq:
466 	free_irq(pdev->irq, acb);
467  out_free_ccb_pool:
468 	arcmsr_free_ccb_pool(acb);
469  out_release_regions:
470 	pci_release_regions(pdev);
471  out_host_put:
472 	scsi_host_put(host);
473  out_disable_device:
474 	pci_disable_device(pdev);
475  out:
476 	return error;
477 }
478 
479 static uint8_t arcmsr_hba_wait_msgint_ready(struct AdapterControlBlock *acb)
480 {
481 	struct MessageUnit_A __iomem *reg = acb->pmuA;
482 	uint32_t Index;
483 	uint8_t Retries = 0x00;
484 
485 	do {
486 		for (Index = 0; Index < 100; Index++) {
487 			if (readl(&reg->outbound_intstatus) &
488 					ARCMSR_MU_OUTBOUND_MESSAGE0_INT) {
489 				writel(ARCMSR_MU_OUTBOUND_MESSAGE0_INT,
490 					&reg->outbound_intstatus);
491 				return 0x00;
492 			}
493 			msleep(10);
494 		}/*max 1 seconds*/
495 
496 	} while (Retries++ < 20);/*max 20 sec*/
497 	return 0xff;
498 }
499 
500 static uint8_t arcmsr_hbb_wait_msgint_ready(struct AdapterControlBlock *acb)
501 {
502 	struct MessageUnit_B *reg = acb->pmuB;
503 	uint32_t Index;
504 	uint8_t Retries = 0x00;
505 
506 	do {
507 		for (Index = 0; Index < 100; Index++) {
508 			if (readl(reg->iop2drv_doorbell_reg)
509 				& ARCMSR_IOP2DRV_MESSAGE_CMD_DONE) {
510 				writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN
511 					, reg->iop2drv_doorbell_reg);
512 				return 0x00;
513 			}
514 			msleep(10);
515 		}/*max 1 seconds*/
516 
517 	} while (Retries++ < 20);/*max 20 sec*/
518 	return 0xff;
519 }
520 
521 static void arcmsr_abort_hba_allcmd(struct AdapterControlBlock *acb)
522 {
523 	struct MessageUnit_A __iomem *reg = acb->pmuA;
524 
525 	writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, &reg->inbound_msgaddr0);
526 	if (arcmsr_hba_wait_msgint_ready(acb))
527 		printk(KERN_NOTICE
528 			"arcmsr%d: wait 'abort all outstanding command' timeout \n"
529 			, acb->host->host_no);
530 }
531 
532 static void arcmsr_abort_hbb_allcmd(struct AdapterControlBlock *acb)
533 {
534 	struct MessageUnit_B *reg = acb->pmuB;
535 
536 	writel(ARCMSR_MESSAGE_ABORT_CMD, reg->drv2iop_doorbell_reg);
537 	if (arcmsr_hbb_wait_msgint_ready(acb))
538 		printk(KERN_NOTICE
539 			"arcmsr%d: wait 'abort all outstanding command' timeout \n"
540 			, acb->host->host_no);
541 }
542 
543 static void arcmsr_abort_allcmd(struct AdapterControlBlock *acb)
544 {
545 	switch (acb->adapter_type) {
546 	case ACB_ADAPTER_TYPE_A: {
547 		arcmsr_abort_hba_allcmd(acb);
548 		}
549 		break;
550 
551 	case ACB_ADAPTER_TYPE_B: {
552 		arcmsr_abort_hbb_allcmd(acb);
553 		}
554 	}
555 }
556 
557 static void arcmsr_pci_unmap_dma(struct CommandControlBlock *ccb)
558 {
559 	struct scsi_cmnd *pcmd = ccb->pcmd;
560 
561 	scsi_dma_unmap(pcmd);
562 }
563 
564 static void arcmsr_ccb_complete(struct CommandControlBlock *ccb, int stand_flag)
565 {
566 	struct AdapterControlBlock *acb = ccb->acb;
567 	struct scsi_cmnd *pcmd = ccb->pcmd;
568 
569 	arcmsr_pci_unmap_dma(ccb);
570 	if (stand_flag == 1)
571 		atomic_dec(&acb->ccboutstandingcount);
572 	ccb->startdone = ARCMSR_CCB_DONE;
573 	ccb->ccb_flags = 0;
574 	list_add_tail(&ccb->list, &acb->ccb_free_list);
575 	pcmd->scsi_done(pcmd);
576 }
577 
578 static void arcmsr_flush_hba_cache(struct AdapterControlBlock *acb)
579 {
580 	struct MessageUnit_A __iomem *reg = acb->pmuA;
581 	int retry_count = 30;
582 
583 	writel(ARCMSR_INBOUND_MESG0_FLUSH_CACHE, &reg->inbound_msgaddr0);
584 	do {
585 		if (!arcmsr_hba_wait_msgint_ready(acb))
586 			break;
587 		else {
588 			retry_count--;
589 			printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
590 			timeout, retry count down = %d \n", acb->host->host_no, retry_count);
591 		}
592 	} while (retry_count != 0);
593 }
594 
595 static void arcmsr_flush_hbb_cache(struct AdapterControlBlock *acb)
596 {
597 	struct MessageUnit_B *reg = acb->pmuB;
598 	int retry_count = 30;
599 
600 	writel(ARCMSR_MESSAGE_FLUSH_CACHE, reg->drv2iop_doorbell_reg);
601 	do {
602 		if (!arcmsr_hbb_wait_msgint_ready(acb))
603 			break;
604 		else {
605 			retry_count--;
606 			printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
607 			timeout,retry count down = %d \n", acb->host->host_no, retry_count);
608 		}
609 	} while (retry_count != 0);
610 }
611 
612 static void arcmsr_flush_adapter_cache(struct AdapterControlBlock *acb)
613 {
614 	switch (acb->adapter_type) {
615 
616 	case ACB_ADAPTER_TYPE_A: {
617 		arcmsr_flush_hba_cache(acb);
618 		}
619 		break;
620 
621 	case ACB_ADAPTER_TYPE_B: {
622 		arcmsr_flush_hbb_cache(acb);
623 		}
624 	}
625 }
626 
627 static void arcmsr_report_sense_info(struct CommandControlBlock *ccb)
628 {
629 
630 	struct scsi_cmnd *pcmd = ccb->pcmd;
631 	struct SENSE_DATA *sensebuffer = (struct SENSE_DATA *)pcmd->sense_buffer;
632 
633 	pcmd->result = DID_OK << 16;
634 	if (sensebuffer) {
635 		int sense_data_length =
636 			sizeof(struct SENSE_DATA) < SCSI_SENSE_BUFFERSIZE
637 			? sizeof(struct SENSE_DATA) : SCSI_SENSE_BUFFERSIZE;
638 		memset(sensebuffer, 0, SCSI_SENSE_BUFFERSIZE);
639 		memcpy(sensebuffer, ccb->arcmsr_cdb.SenseData, sense_data_length);
640 		sensebuffer->ErrorCode = SCSI_SENSE_CURRENT_ERRORS;
641 		sensebuffer->Valid = 1;
642 	}
643 }
644 
645 static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb)
646 {
647 	u32 orig_mask = 0;
648 	switch (acb->adapter_type) {
649 
650 	case ACB_ADAPTER_TYPE_A : {
651 		struct MessageUnit_A __iomem *reg = acb->pmuA;
652 		orig_mask = readl(&reg->outbound_intmask)|\
653 				ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE;
654 		writel(orig_mask|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE, \
655 						&reg->outbound_intmask);
656 		}
657 		break;
658 
659 	case ACB_ADAPTER_TYPE_B : {
660 		struct MessageUnit_B *reg = acb->pmuB;
661 		orig_mask = readl(reg->iop2drv_doorbell_mask_reg) & \
662 					(~ARCMSR_IOP2DRV_MESSAGE_CMD_DONE);
663 		writel(0, reg->iop2drv_doorbell_mask_reg);
664 		}
665 		break;
666 	}
667 	return orig_mask;
668 }
669 
670 static void arcmsr_report_ccb_state(struct AdapterControlBlock *acb, \
671 			struct CommandControlBlock *ccb, uint32_t flag_ccb)
672 {
673 
674 	uint8_t id, lun;
675 	id = ccb->pcmd->device->id;
676 	lun = ccb->pcmd->device->lun;
677 	if (!(flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR)) {
678 		if (acb->devstate[id][lun] == ARECA_RAID_GONE)
679 			acb->devstate[id][lun] = ARECA_RAID_GOOD;
680 			ccb->pcmd->result = DID_OK << 16;
681 			arcmsr_ccb_complete(ccb, 1);
682 	} else {
683 		switch (ccb->arcmsr_cdb.DeviceStatus) {
684 		case ARCMSR_DEV_SELECT_TIMEOUT: {
685 			acb->devstate[id][lun] = ARECA_RAID_GONE;
686 			ccb->pcmd->result = DID_NO_CONNECT << 16;
687 			arcmsr_ccb_complete(ccb, 1);
688 			}
689 			break;
690 
691 		case ARCMSR_DEV_ABORTED:
692 
693 		case ARCMSR_DEV_INIT_FAIL: {
694 			acb->devstate[id][lun] = ARECA_RAID_GONE;
695 			ccb->pcmd->result = DID_BAD_TARGET << 16;
696 			arcmsr_ccb_complete(ccb, 1);
697 			}
698 			break;
699 
700 		case ARCMSR_DEV_CHECK_CONDITION: {
701 			acb->devstate[id][lun] = ARECA_RAID_GOOD;
702 			arcmsr_report_sense_info(ccb);
703 			arcmsr_ccb_complete(ccb, 1);
704 			}
705 			break;
706 
707 		default:
708 				printk(KERN_NOTICE
709 					"arcmsr%d: scsi id = %d lun = %d"
710 					" isr get command error done, "
711 					"but got unknown DeviceStatus = 0x%x \n"
712 					, acb->host->host_no
713 					, id
714 					, lun
715 					, ccb->arcmsr_cdb.DeviceStatus);
716 					acb->devstate[id][lun] = ARECA_RAID_GONE;
717 					ccb->pcmd->result = DID_NO_CONNECT << 16;
718 					arcmsr_ccb_complete(ccb, 1);
719 			break;
720 		}
721 	}
722 }
723 
724 static void arcmsr_drain_donequeue(struct AdapterControlBlock *acb, uint32_t flag_ccb)
725 
726 {
727 	struct CommandControlBlock *ccb;
728 
729 	ccb = (struct CommandControlBlock *)(acb->vir2phy_offset + (flag_ccb << 5));
730 	if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
731 		if (ccb->startdone == ARCMSR_CCB_ABORTED) {
732 			struct scsi_cmnd *abortcmd = ccb->pcmd;
733 			if (abortcmd) {
734 				abortcmd->result |= DID_ABORT << 16;
735 				arcmsr_ccb_complete(ccb, 1);
736 				printk(KERN_NOTICE "arcmsr%d: ccb ='0x%p' \
737 				isr got aborted command \n", acb->host->host_no, ccb);
738 			}
739 		}
740 		printk(KERN_NOTICE "arcmsr%d: isr get an illegal ccb command \
741 				done acb = '0x%p'"
742 				"ccb = '0x%p' ccbacb = '0x%p' startdone = 0x%x"
743 				" ccboutstandingcount = %d \n"
744 				, acb->host->host_no
745 				, acb
746 				, ccb
747 				, ccb->acb
748 				, ccb->startdone
749 				, atomic_read(&acb->ccboutstandingcount));
750 		}
751 	arcmsr_report_ccb_state(acb, ccb, flag_ccb);
752 }
753 
754 static void arcmsr_done4abort_postqueue(struct AdapterControlBlock *acb)
755 {
756 	int i = 0;
757 	uint32_t flag_ccb;
758 
759 	switch (acb->adapter_type) {
760 
761 	case ACB_ADAPTER_TYPE_A: {
762 		struct MessageUnit_A __iomem *reg = acb->pmuA;
763 		uint32_t outbound_intstatus;
764 		outbound_intstatus = readl(&reg->outbound_intstatus) &
765 					acb->outbound_int_enable;
766 		/*clear and abort all outbound posted Q*/
767 		writel(outbound_intstatus, &reg->outbound_intstatus);/*clear interrupt*/
768 		while (((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF)
769 				&& (i++ < ARCMSR_MAX_OUTSTANDING_CMD)) {
770 			arcmsr_drain_donequeue(acb, flag_ccb);
771 		}
772 		}
773 		break;
774 
775 	case ACB_ADAPTER_TYPE_B: {
776 		struct MessageUnit_B *reg = acb->pmuB;
777 		/*clear all outbound posted Q*/
778 		for (i = 0; i < ARCMSR_MAX_HBB_POSTQUEUE; i++) {
779 			if ((flag_ccb = readl(&reg->done_qbuffer[i])) != 0) {
780 				writel(0, &reg->done_qbuffer[i]);
781 				arcmsr_drain_donequeue(acb, flag_ccb);
782 			}
783 			writel(0, &reg->post_qbuffer[i]);
784 		}
785 		reg->doneq_index = 0;
786 		reg->postq_index = 0;
787 		}
788 		break;
789 	}
790 }
791 static void arcmsr_remove(struct pci_dev *pdev)
792 {
793 	struct Scsi_Host *host = pci_get_drvdata(pdev);
794 	struct AdapterControlBlock *acb =
795 		(struct AdapterControlBlock *) host->hostdata;
796 	int poll_count = 0;
797 
798 	arcmsr_free_sysfs_attr(acb);
799 	scsi_remove_host(host);
800 	arcmsr_stop_adapter_bgrb(acb);
801 	arcmsr_flush_adapter_cache(acb);
802 	arcmsr_disable_outbound_ints(acb);
803 	acb->acb_flags |= ACB_F_SCSISTOPADAPTER;
804 	acb->acb_flags &= ~ACB_F_IOP_INITED;
805 
806 	for (poll_count = 0; poll_count < ARCMSR_MAX_OUTSTANDING_CMD; poll_count++) {
807 		if (!atomic_read(&acb->ccboutstandingcount))
808 			break;
809 		arcmsr_interrupt(acb);/* FIXME: need spinlock */
810 		msleep(25);
811 	}
812 
813 	if (atomic_read(&acb->ccboutstandingcount)) {
814 		int i;
815 
816 		arcmsr_abort_allcmd(acb);
817 		arcmsr_done4abort_postqueue(acb);
818 		for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
819 			struct CommandControlBlock *ccb = acb->pccb_pool[i];
820 			if (ccb->startdone == ARCMSR_CCB_START) {
821 				ccb->startdone = ARCMSR_CCB_ABORTED;
822 				ccb->pcmd->result = DID_ABORT << 16;
823 				arcmsr_ccb_complete(ccb, 1);
824 			}
825 		}
826 	}
827 
828 	free_irq(pdev->irq, acb);
829 	arcmsr_free_ccb_pool(acb);
830 	pci_release_regions(pdev);
831 
832 	scsi_host_put(host);
833 
834 	pci_disable_device(pdev);
835 	pci_set_drvdata(pdev, NULL);
836 }
837 
838 static void arcmsr_shutdown(struct pci_dev *pdev)
839 {
840 	struct Scsi_Host *host = pci_get_drvdata(pdev);
841 	struct AdapterControlBlock *acb =
842 		(struct AdapterControlBlock *)host->hostdata;
843 
844 	arcmsr_stop_adapter_bgrb(acb);
845 	arcmsr_flush_adapter_cache(acb);
846 }
847 
848 static int arcmsr_module_init(void)
849 {
850 	int error = 0;
851 
852 	error = pci_register_driver(&arcmsr_pci_driver);
853 	return error;
854 }
855 
856 static void arcmsr_module_exit(void)
857 {
858 	pci_unregister_driver(&arcmsr_pci_driver);
859 }
860 module_init(arcmsr_module_init);
861 module_exit(arcmsr_module_exit);
862 
863 static void arcmsr_enable_outbound_ints(struct AdapterControlBlock *acb, \
864 						u32 intmask_org)
865 {
866 	u32 mask;
867 
868 	switch (acb->adapter_type) {
869 
870 	case ACB_ADAPTER_TYPE_A : {
871 		struct MessageUnit_A __iomem *reg = acb->pmuA;
872 		mask = intmask_org & ~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE |
873 			     ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE);
874 		writel(mask, &reg->outbound_intmask);
875 		acb->outbound_int_enable = ~(intmask_org & mask) & 0x000000ff;
876 		}
877 		break;
878 
879 	case ACB_ADAPTER_TYPE_B : {
880 		struct MessageUnit_B *reg = acb->pmuB;
881 		mask = intmask_org | (ARCMSR_IOP2DRV_DATA_WRITE_OK | \
882 			ARCMSR_IOP2DRV_DATA_READ_OK | ARCMSR_IOP2DRV_CDB_DONE);
883 		writel(mask, reg->iop2drv_doorbell_mask_reg);
884 		acb->outbound_int_enable = (intmask_org | mask) & 0x0000000f;
885 		}
886 	}
887 }
888 
889 static void arcmsr_build_ccb(struct AdapterControlBlock *acb,
890 	struct CommandControlBlock *ccb, struct scsi_cmnd *pcmd)
891 {
892 	struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
893 	int8_t *psge = (int8_t *)&arcmsr_cdb->u;
894 	__le32 address_lo, address_hi;
895 	int arccdbsize = 0x30;
896 	int nseg;
897 
898 	ccb->pcmd = pcmd;
899 	memset(arcmsr_cdb, 0, sizeof(struct ARCMSR_CDB));
900 	arcmsr_cdb->Bus = 0;
901 	arcmsr_cdb->TargetID = pcmd->device->id;
902 	arcmsr_cdb->LUN = pcmd->device->lun;
903 	arcmsr_cdb->Function = 1;
904 	arcmsr_cdb->CdbLength = (uint8_t)pcmd->cmd_len;
905 	arcmsr_cdb->Context = (unsigned long)arcmsr_cdb;
906 	memcpy(arcmsr_cdb->Cdb, pcmd->cmnd, pcmd->cmd_len);
907 
908 	nseg = scsi_dma_map(pcmd);
909 	BUG_ON(nseg < 0);
910 
911 	if (nseg) {
912 		__le32 length;
913 		int i, cdb_sgcount = 0;
914 		struct scatterlist *sg;
915 
916 		/* map stor port SG list to our iop SG List. */
917 		scsi_for_each_sg(pcmd, sg, nseg, i) {
918 			/* Get the physical address of the current data pointer */
919 			length = cpu_to_le32(sg_dma_len(sg));
920 			address_lo = cpu_to_le32(dma_addr_lo32(sg_dma_address(sg)));
921 			address_hi = cpu_to_le32(dma_addr_hi32(sg_dma_address(sg)));
922 			if (address_hi == 0) {
923 				struct SG32ENTRY *pdma_sg = (struct SG32ENTRY *)psge;
924 
925 				pdma_sg->address = address_lo;
926 				pdma_sg->length = length;
927 				psge += sizeof (struct SG32ENTRY);
928 				arccdbsize += sizeof (struct SG32ENTRY);
929 			} else {
930 				struct SG64ENTRY *pdma_sg = (struct SG64ENTRY *)psge;
931 
932 				pdma_sg->addresshigh = address_hi;
933 				pdma_sg->address = address_lo;
934 				pdma_sg->length = length|cpu_to_le32(IS_SG64_ADDR);
935 				psge += sizeof (struct SG64ENTRY);
936 				arccdbsize += sizeof (struct SG64ENTRY);
937 			}
938 			cdb_sgcount++;
939 		}
940 		arcmsr_cdb->sgcount = (uint8_t)cdb_sgcount;
941 		arcmsr_cdb->DataLength = scsi_bufflen(pcmd);
942 		if ( arccdbsize > 256)
943 			arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_SGL_BSIZE;
944 	}
945 	if (pcmd->sc_data_direction == DMA_TO_DEVICE ) {
946 		arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_WRITE;
947 		ccb->ccb_flags |= CCB_FLAG_WRITE;
948 	}
949 }
950 
951 static void arcmsr_post_ccb(struct AdapterControlBlock *acb, struct CommandControlBlock *ccb)
952 {
953 	uint32_t cdb_shifted_phyaddr = ccb->cdb_shifted_phyaddr;
954 	struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
955 	atomic_inc(&acb->ccboutstandingcount);
956 	ccb->startdone = ARCMSR_CCB_START;
957 
958 	switch (acb->adapter_type) {
959 	case ACB_ADAPTER_TYPE_A: {
960 		struct MessageUnit_A __iomem *reg = acb->pmuA;
961 
962 		if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE)
963 			writel(cdb_shifted_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,
964 			&reg->inbound_queueport);
965 		else {
966 				writel(cdb_shifted_phyaddr, &reg->inbound_queueport);
967 		}
968 		}
969 		break;
970 
971 	case ACB_ADAPTER_TYPE_B: {
972 		struct MessageUnit_B *reg = acb->pmuB;
973 		uint32_t ending_index, index = reg->postq_index;
974 
975 		ending_index = ((index + 1) % ARCMSR_MAX_HBB_POSTQUEUE);
976 		writel(0, &reg->post_qbuffer[ending_index]);
977 		if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE) {
978 			writel(cdb_shifted_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,\
979 						 &reg->post_qbuffer[index]);
980 		}
981 		else {
982 			writel(cdb_shifted_phyaddr, &reg->post_qbuffer[index]);
983 		}
984 		index++;
985 		index %= ARCMSR_MAX_HBB_POSTQUEUE;/*if last index number set it to 0 */
986 		reg->postq_index = index;
987 		writel(ARCMSR_DRV2IOP_CDB_POSTED, reg->drv2iop_doorbell_reg);
988 		}
989 		break;
990 	}
991 }
992 
993 static void arcmsr_stop_hba_bgrb(struct AdapterControlBlock *acb)
994 {
995 	struct MessageUnit_A __iomem *reg = acb->pmuA;
996 	acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
997 	writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, &reg->inbound_msgaddr0);
998 
999 	if (arcmsr_hba_wait_msgint_ready(acb)) {
1000 		printk(KERN_NOTICE
1001 			"arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
1002 			, acb->host->host_no);
1003 	}
1004 }
1005 
1006 static void arcmsr_stop_hbb_bgrb(struct AdapterControlBlock *acb)
1007 {
1008 	struct MessageUnit_B *reg = acb->pmuB;
1009 	acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
1010 	writel(ARCMSR_MESSAGE_STOP_BGRB, reg->drv2iop_doorbell_reg);
1011 
1012 	if (arcmsr_hbb_wait_msgint_ready(acb)) {
1013 		printk(KERN_NOTICE
1014 			"arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
1015 			, acb->host->host_no);
1016 	}
1017 }
1018 
1019 static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb)
1020 {
1021 	switch (acb->adapter_type) {
1022 	case ACB_ADAPTER_TYPE_A: {
1023 		arcmsr_stop_hba_bgrb(acb);
1024 		}
1025 		break;
1026 
1027 	case ACB_ADAPTER_TYPE_B: {
1028 		arcmsr_stop_hbb_bgrb(acb);
1029 		}
1030 		break;
1031 	}
1032 }
1033 
1034 static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb)
1035 {
1036 	switch (acb->adapter_type) {
1037 	case ACB_ADAPTER_TYPE_A: {
1038 		iounmap(acb->pmuA);
1039 		break;
1040 	}
1041 	case ACB_ADAPTER_TYPE_B: {
1042 		struct MessageUnit_B *reg = acb->pmuB;
1043 		iounmap(reg->drv2iop_doorbell_reg - ARCMSR_DRV2IOP_DOORBELL);
1044 		iounmap(reg->ioctl_wbuffer_reg - ARCMSR_IOCTL_WBUFFER);
1045 	}
1046 	}
1047 	dma_free_coherent(&acb->pdev->dev,
1048 		ARCMSR_MAX_FREECCB_NUM * sizeof (struct CommandControlBlock) + 0x20,
1049 		acb->dma_coherent,
1050 		acb->dma_coherent_handle);
1051 }
1052 
1053 void arcmsr_iop_message_read(struct AdapterControlBlock *acb)
1054 {
1055 	switch (acb->adapter_type) {
1056 	case ACB_ADAPTER_TYPE_A: {
1057 		struct MessageUnit_A __iomem *reg = acb->pmuA;
1058 		writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, &reg->inbound_doorbell);
1059 		}
1060 		break;
1061 
1062 	case ACB_ADAPTER_TYPE_B: {
1063 		struct MessageUnit_B *reg = acb->pmuB;
1064 		writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell_reg);
1065 		}
1066 		break;
1067 	}
1068 }
1069 
1070 static void arcmsr_iop_message_wrote(struct AdapterControlBlock *acb)
1071 {
1072 	switch (acb->adapter_type) {
1073 	case ACB_ADAPTER_TYPE_A: {
1074 		struct MessageUnit_A __iomem *reg = acb->pmuA;
1075 		/*
1076 		** push inbound doorbell tell iop, driver data write ok
1077 		** and wait reply on next hwinterrupt for next Qbuffer post
1078 		*/
1079 		writel(ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK, &reg->inbound_doorbell);
1080 		}
1081 		break;
1082 
1083 	case ACB_ADAPTER_TYPE_B: {
1084 		struct MessageUnit_B *reg = acb->pmuB;
1085 		/*
1086 		** push inbound doorbell tell iop, driver data write ok
1087 		** and wait reply on next hwinterrupt for next Qbuffer post
1088 		*/
1089 		writel(ARCMSR_DRV2IOP_DATA_WRITE_OK, reg->drv2iop_doorbell_reg);
1090 		}
1091 		break;
1092 	}
1093 }
1094 
1095 struct QBUFFER __iomem *arcmsr_get_iop_rqbuffer(struct AdapterControlBlock *acb)
1096 {
1097 	struct QBUFFER __iomem *qbuffer = NULL;
1098 
1099 	switch (acb->adapter_type) {
1100 
1101 	case ACB_ADAPTER_TYPE_A: {
1102 		struct MessageUnit_A __iomem *reg = acb->pmuA;
1103 		qbuffer = (struct QBUFFER __iomem *)&reg->message_rbuffer;
1104 		}
1105 		break;
1106 
1107 	case ACB_ADAPTER_TYPE_B: {
1108 		struct MessageUnit_B *reg = acb->pmuB;
1109 		qbuffer = (struct QBUFFER __iomem *)reg->ioctl_rbuffer_reg;
1110 		}
1111 		break;
1112 	}
1113 	return qbuffer;
1114 }
1115 
1116 static struct QBUFFER __iomem *arcmsr_get_iop_wqbuffer(struct AdapterControlBlock *acb)
1117 {
1118 	struct QBUFFER __iomem *pqbuffer = NULL;
1119 
1120 	switch (acb->adapter_type) {
1121 
1122 	case ACB_ADAPTER_TYPE_A: {
1123 		struct MessageUnit_A __iomem *reg = acb->pmuA;
1124 		pqbuffer = (struct QBUFFER __iomem *) &reg->message_wbuffer;
1125 		}
1126 		break;
1127 
1128 	case ACB_ADAPTER_TYPE_B: {
1129 		struct MessageUnit_B  *reg = acb->pmuB;
1130 		pqbuffer = (struct QBUFFER __iomem *)reg->ioctl_wbuffer_reg;
1131 		}
1132 		break;
1133 	}
1134 	return pqbuffer;
1135 }
1136 
1137 static void arcmsr_iop2drv_data_wrote_handle(struct AdapterControlBlock *acb)
1138 {
1139 	struct QBUFFER __iomem *prbuffer;
1140 	struct QBUFFER *pQbuffer;
1141 	uint8_t __iomem *iop_data;
1142 	int32_t my_empty_len, iop_len, rqbuf_firstindex, rqbuf_lastindex;
1143 
1144 	rqbuf_lastindex = acb->rqbuf_lastindex;
1145 	rqbuf_firstindex = acb->rqbuf_firstindex;
1146 	prbuffer = arcmsr_get_iop_rqbuffer(acb);
1147 	iop_data = (uint8_t __iomem *)prbuffer->data;
1148 	iop_len = prbuffer->data_len;
1149 	my_empty_len = (rqbuf_firstindex - rqbuf_lastindex -1)&(ARCMSR_MAX_QBUFFER -1);
1150 
1151 	if (my_empty_len >= iop_len)
1152 	{
1153 		while (iop_len > 0) {
1154 			pQbuffer = (struct QBUFFER *)&acb->rqbuffer[rqbuf_lastindex];
1155 			memcpy(pQbuffer, iop_data,1);
1156 			rqbuf_lastindex++;
1157 			rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1158 			iop_data++;
1159 			iop_len--;
1160 		}
1161 		acb->rqbuf_lastindex = rqbuf_lastindex;
1162 		arcmsr_iop_message_read(acb);
1163 	}
1164 
1165 	else {
1166 		acb->acb_flags |= ACB_F_IOPDATA_OVERFLOW;
1167 	}
1168 }
1169 
1170 static void arcmsr_iop2drv_data_read_handle(struct AdapterControlBlock *acb)
1171 {
1172 	acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_READED;
1173 	if (acb->wqbuf_firstindex != acb->wqbuf_lastindex) {
1174 		uint8_t *pQbuffer;
1175 		struct QBUFFER __iomem *pwbuffer;
1176 		uint8_t __iomem *iop_data;
1177 		int32_t allxfer_len = 0;
1178 
1179 		acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
1180 		pwbuffer = arcmsr_get_iop_wqbuffer(acb);
1181 		iop_data = (uint8_t __iomem *)pwbuffer->data;
1182 
1183 		while ((acb->wqbuf_firstindex != acb->wqbuf_lastindex) && \
1184 							(allxfer_len < 124)) {
1185 			pQbuffer = &acb->wqbuffer[acb->wqbuf_firstindex];
1186 			memcpy(iop_data, pQbuffer, 1);
1187 			acb->wqbuf_firstindex++;
1188 			acb->wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1189 			iop_data++;
1190 			allxfer_len++;
1191 		}
1192 		pwbuffer->data_len = allxfer_len;
1193 
1194 		arcmsr_iop_message_wrote(acb);
1195 	}
1196 
1197 	if (acb->wqbuf_firstindex == acb->wqbuf_lastindex) {
1198 		acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_CLEARED;
1199 	}
1200 }
1201 
1202 static void arcmsr_hba_doorbell_isr(struct AdapterControlBlock *acb)
1203 {
1204 	uint32_t outbound_doorbell;
1205 	struct MessageUnit_A __iomem *reg = acb->pmuA;
1206 
1207 	outbound_doorbell = readl(&reg->outbound_doorbell);
1208 	writel(outbound_doorbell, &reg->outbound_doorbell);
1209 	if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK) {
1210 		arcmsr_iop2drv_data_wrote_handle(acb);
1211 	}
1212 
1213 	if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_READ_OK) 	{
1214 		arcmsr_iop2drv_data_read_handle(acb);
1215 	}
1216 }
1217 
1218 static void arcmsr_hba_postqueue_isr(struct AdapterControlBlock *acb)
1219 {
1220 	uint32_t flag_ccb;
1221 	struct MessageUnit_A __iomem *reg = acb->pmuA;
1222 
1223 	while ((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF) {
1224 		arcmsr_drain_donequeue(acb, flag_ccb);
1225 	}
1226 }
1227 
1228 static void arcmsr_hbb_postqueue_isr(struct AdapterControlBlock *acb)
1229 {
1230 	uint32_t index;
1231 	uint32_t flag_ccb;
1232 	struct MessageUnit_B *reg = acb->pmuB;
1233 
1234 	index = reg->doneq_index;
1235 
1236 	while ((flag_ccb = readl(&reg->done_qbuffer[index])) != 0) {
1237 		writel(0, &reg->done_qbuffer[index]);
1238 		arcmsr_drain_donequeue(acb, flag_ccb);
1239 		index++;
1240 		index %= ARCMSR_MAX_HBB_POSTQUEUE;
1241 		reg->doneq_index = index;
1242 	}
1243 }
1244 
1245 static int arcmsr_handle_hba_isr(struct AdapterControlBlock *acb)
1246 {
1247 	uint32_t outbound_intstatus;
1248 	struct MessageUnit_A __iomem *reg = acb->pmuA;
1249 
1250 	outbound_intstatus = readl(&reg->outbound_intstatus) & \
1251 							acb->outbound_int_enable;
1252 	if (!(outbound_intstatus & ARCMSR_MU_OUTBOUND_HANDLE_INT))	{
1253 		return 1;
1254 	}
1255 	writel(outbound_intstatus, &reg->outbound_intstatus);
1256 	if (outbound_intstatus & ARCMSR_MU_OUTBOUND_DOORBELL_INT)	{
1257 		arcmsr_hba_doorbell_isr(acb);
1258 	}
1259 	if (outbound_intstatus & ARCMSR_MU_OUTBOUND_POSTQUEUE_INT) {
1260 		arcmsr_hba_postqueue_isr(acb);
1261 	}
1262 	return 0;
1263 }
1264 
1265 static int arcmsr_handle_hbb_isr(struct AdapterControlBlock *acb)
1266 {
1267 	uint32_t outbound_doorbell;
1268 	struct MessageUnit_B *reg = acb->pmuB;
1269 
1270 	outbound_doorbell = readl(reg->iop2drv_doorbell_reg) & \
1271 							acb->outbound_int_enable;
1272 	if (!outbound_doorbell)
1273 		return 1;
1274 
1275 	writel(~outbound_doorbell, reg->iop2drv_doorbell_reg);
1276 
1277 	if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_WRITE_OK) 	{
1278 		arcmsr_iop2drv_data_wrote_handle(acb);
1279 	}
1280 	if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_READ_OK) {
1281 		arcmsr_iop2drv_data_read_handle(acb);
1282 	}
1283 	if (outbound_doorbell & ARCMSR_IOP2DRV_CDB_DONE) {
1284 		arcmsr_hbb_postqueue_isr(acb);
1285 	}
1286 
1287 	return 0;
1288 }
1289 
1290 static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb)
1291 {
1292 	switch (acb->adapter_type) {
1293 	case ACB_ADAPTER_TYPE_A: {
1294 		if (arcmsr_handle_hba_isr(acb)) {
1295 			return IRQ_NONE;
1296 		}
1297 		}
1298 		break;
1299 
1300 	case ACB_ADAPTER_TYPE_B: {
1301 		if (arcmsr_handle_hbb_isr(acb)) {
1302 			return IRQ_NONE;
1303 		}
1304 		}
1305 		break;
1306 	}
1307 	return IRQ_HANDLED;
1308 }
1309 
1310 static void arcmsr_iop_parking(struct AdapterControlBlock *acb)
1311 {
1312 	if (acb) {
1313 		/* stop adapter background rebuild */
1314 		if (acb->acb_flags & ACB_F_MSG_START_BGRB) {
1315 			uint32_t intmask_org;
1316 			acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
1317 			intmask_org = arcmsr_disable_outbound_ints(acb);
1318 			arcmsr_stop_adapter_bgrb(acb);
1319 			arcmsr_flush_adapter_cache(acb);
1320 			arcmsr_enable_outbound_ints(acb, intmask_org);
1321 		}
1322 	}
1323 }
1324 
1325 void arcmsr_post_ioctldata2iop(struct AdapterControlBlock *acb)
1326 {
1327 	int32_t wqbuf_firstindex, wqbuf_lastindex;
1328 	uint8_t *pQbuffer;
1329 	struct QBUFFER __iomem *pwbuffer;
1330 	uint8_t __iomem *iop_data;
1331 	int32_t allxfer_len = 0;
1332 
1333 	pwbuffer = arcmsr_get_iop_wqbuffer(acb);
1334 	iop_data = (uint8_t __iomem *)pwbuffer->data;
1335 	if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_READED) {
1336 		acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
1337 		wqbuf_firstindex = acb->wqbuf_firstindex;
1338 		wqbuf_lastindex = acb->wqbuf_lastindex;
1339 		while ((wqbuf_firstindex != wqbuf_lastindex) && (allxfer_len < 124)) {
1340 			pQbuffer = &acb->wqbuffer[wqbuf_firstindex];
1341 			memcpy(iop_data, pQbuffer, 1);
1342 			wqbuf_firstindex++;
1343 			wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1344 			iop_data++;
1345 			allxfer_len++;
1346 		}
1347 		acb->wqbuf_firstindex = wqbuf_firstindex;
1348 		pwbuffer->data_len = allxfer_len;
1349 		arcmsr_iop_message_wrote(acb);
1350 	}
1351 }
1352 
1353 static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb, \
1354 					struct scsi_cmnd *cmd)
1355 {
1356 	struct CMD_MESSAGE_FIELD *pcmdmessagefld;
1357 	int retvalue = 0, transfer_len = 0;
1358 	char *buffer;
1359 	struct scatterlist *sg;
1360 	uint32_t controlcode = (uint32_t ) cmd->cmnd[5] << 24 |
1361 						(uint32_t ) cmd->cmnd[6] << 16 |
1362 						(uint32_t ) cmd->cmnd[7] << 8  |
1363 						(uint32_t ) cmd->cmnd[8];
1364 						/* 4 bytes: Areca io control code */
1365 
1366 	sg = scsi_sglist(cmd);
1367 	buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
1368 	if (scsi_sg_count(cmd) > 1) {
1369 		retvalue = ARCMSR_MESSAGE_FAIL;
1370 		goto message_out;
1371 	}
1372 	transfer_len += sg->length;
1373 
1374 	if (transfer_len > sizeof(struct CMD_MESSAGE_FIELD)) {
1375 		retvalue = ARCMSR_MESSAGE_FAIL;
1376 		goto message_out;
1377 	}
1378 	pcmdmessagefld = (struct CMD_MESSAGE_FIELD *) buffer;
1379 	switch(controlcode) {
1380 
1381 	case ARCMSR_MESSAGE_READ_RQBUFFER: {
1382 		unsigned long *ver_addr;
1383 		dma_addr_t buf_handle;
1384 		uint8_t *pQbuffer, *ptmpQbuffer;
1385 		int32_t allxfer_len = 0;
1386 
1387 		ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
1388 		if (!ver_addr) {
1389 			retvalue = ARCMSR_MESSAGE_FAIL;
1390 			goto message_out;
1391 		}
1392 		ptmpQbuffer = (uint8_t *) ver_addr;
1393 		while ((acb->rqbuf_firstindex != acb->rqbuf_lastindex)
1394 			&& (allxfer_len < 1031)) {
1395 			pQbuffer = &acb->rqbuffer[acb->rqbuf_firstindex];
1396 			memcpy(ptmpQbuffer, pQbuffer, 1);
1397 			acb->rqbuf_firstindex++;
1398 			acb->rqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1399 			ptmpQbuffer++;
1400 			allxfer_len++;
1401 		}
1402 		if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1403 
1404 			struct QBUFFER __iomem *prbuffer;
1405 			uint8_t __iomem *iop_data;
1406 			int32_t iop_len;
1407 
1408 			acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1409 			prbuffer = arcmsr_get_iop_rqbuffer(acb);
1410 			iop_data = prbuffer->data;
1411 			iop_len = readl(&prbuffer->data_len);
1412 			while (iop_len > 0) {
1413 				acb->rqbuffer[acb->rqbuf_lastindex] = readb(iop_data);
1414 				acb->rqbuf_lastindex++;
1415 				acb->rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1416 				iop_data++;
1417 				iop_len--;
1418 			}
1419 			arcmsr_iop_message_read(acb);
1420 		}
1421 		memcpy(pcmdmessagefld->messagedatabuffer, (uint8_t *)ver_addr, allxfer_len);
1422 		pcmdmessagefld->cmdmessage.Length = allxfer_len;
1423 		pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1424 		pci_free_consistent(acb->pdev, 1032, ver_addr, buf_handle);
1425 		}
1426 		break;
1427 
1428 	case ARCMSR_MESSAGE_WRITE_WQBUFFER: {
1429 		unsigned long *ver_addr;
1430 		dma_addr_t buf_handle;
1431 		int32_t my_empty_len, user_len, wqbuf_firstindex, wqbuf_lastindex;
1432 		uint8_t *pQbuffer, *ptmpuserbuffer;
1433 
1434 		ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
1435 		if (!ver_addr) {
1436 			retvalue = ARCMSR_MESSAGE_FAIL;
1437 			goto message_out;
1438 		}
1439 		ptmpuserbuffer = (uint8_t *)ver_addr;
1440 		user_len = pcmdmessagefld->cmdmessage.Length;
1441 		memcpy(ptmpuserbuffer, pcmdmessagefld->messagedatabuffer, user_len);
1442 		wqbuf_lastindex = acb->wqbuf_lastindex;
1443 		wqbuf_firstindex = acb->wqbuf_firstindex;
1444 		if (wqbuf_lastindex != wqbuf_firstindex) {
1445 			struct SENSE_DATA *sensebuffer =
1446 				(struct SENSE_DATA *)cmd->sense_buffer;
1447 			arcmsr_post_ioctldata2iop(acb);
1448 			/* has error report sensedata */
1449 			sensebuffer->ErrorCode = 0x70;
1450 			sensebuffer->SenseKey = ILLEGAL_REQUEST;
1451 			sensebuffer->AdditionalSenseLength = 0x0A;
1452 			sensebuffer->AdditionalSenseCode = 0x20;
1453 			sensebuffer->Valid = 1;
1454 			retvalue = ARCMSR_MESSAGE_FAIL;
1455 		} else {
1456 			my_empty_len = (wqbuf_firstindex-wqbuf_lastindex - 1)
1457 				&(ARCMSR_MAX_QBUFFER - 1);
1458 			if (my_empty_len >= user_len) {
1459 				while (user_len > 0) {
1460 					pQbuffer =
1461 					&acb->wqbuffer[acb->wqbuf_lastindex];
1462 					memcpy(pQbuffer, ptmpuserbuffer, 1);
1463 					acb->wqbuf_lastindex++;
1464 					acb->wqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1465 					ptmpuserbuffer++;
1466 					user_len--;
1467 				}
1468 				if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {
1469 					acb->acb_flags &=
1470 						~ACB_F_MESSAGE_WQBUFFER_CLEARED;
1471 					arcmsr_post_ioctldata2iop(acb);
1472 				}
1473 			} else {
1474 				/* has error report sensedata */
1475 				struct SENSE_DATA *sensebuffer =
1476 					(struct SENSE_DATA *)cmd->sense_buffer;
1477 				sensebuffer->ErrorCode = 0x70;
1478 				sensebuffer->SenseKey = ILLEGAL_REQUEST;
1479 				sensebuffer->AdditionalSenseLength = 0x0A;
1480 				sensebuffer->AdditionalSenseCode = 0x20;
1481 				sensebuffer->Valid = 1;
1482 				retvalue = ARCMSR_MESSAGE_FAIL;
1483 			}
1484 			}
1485 			pci_free_consistent(acb->pdev, 1032, ver_addr, buf_handle);
1486 		}
1487 		break;
1488 
1489 	case ARCMSR_MESSAGE_CLEAR_RQBUFFER: {
1490 		uint8_t *pQbuffer = acb->rqbuffer;
1491 
1492 		if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1493 			acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1494 			arcmsr_iop_message_read(acb);
1495 		}
1496 		acb->acb_flags |= ACB_F_MESSAGE_RQBUFFER_CLEARED;
1497 		acb->rqbuf_firstindex = 0;
1498 		acb->rqbuf_lastindex = 0;
1499 		memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1500 		pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1501 		}
1502 		break;
1503 
1504 	case ARCMSR_MESSAGE_CLEAR_WQBUFFER: {
1505 		uint8_t *pQbuffer = acb->wqbuffer;
1506 
1507 		if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1508 			acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1509 			arcmsr_iop_message_read(acb);
1510 		}
1511 		acb->acb_flags |=
1512 			(ACB_F_MESSAGE_WQBUFFER_CLEARED |
1513 				ACB_F_MESSAGE_WQBUFFER_READED);
1514 		acb->wqbuf_firstindex = 0;
1515 		acb->wqbuf_lastindex = 0;
1516 		memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1517 		pcmdmessagefld->cmdmessage.ReturnCode =
1518 			ARCMSR_MESSAGE_RETURNCODE_OK;
1519 		}
1520 		break;
1521 
1522 	case ARCMSR_MESSAGE_CLEAR_ALLQBUFFER: {
1523 		uint8_t *pQbuffer;
1524 
1525 		if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1526 			acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1527 			arcmsr_iop_message_read(acb);
1528 		}
1529 		acb->acb_flags |=
1530 			(ACB_F_MESSAGE_WQBUFFER_CLEARED
1531 			| ACB_F_MESSAGE_RQBUFFER_CLEARED
1532 			| ACB_F_MESSAGE_WQBUFFER_READED);
1533 		acb->rqbuf_firstindex = 0;
1534 		acb->rqbuf_lastindex = 0;
1535 		acb->wqbuf_firstindex = 0;
1536 		acb->wqbuf_lastindex = 0;
1537 		pQbuffer = acb->rqbuffer;
1538 		memset(pQbuffer, 0, sizeof(struct QBUFFER));
1539 		pQbuffer = acb->wqbuffer;
1540 		memset(pQbuffer, 0, sizeof(struct QBUFFER));
1541 		pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1542 		}
1543 		break;
1544 
1545 	case ARCMSR_MESSAGE_RETURN_CODE_3F: {
1546 		pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_3F;
1547 		}
1548 		break;
1549 
1550 	case ARCMSR_MESSAGE_SAY_HELLO: {
1551 		int8_t *hello_string = "Hello! I am ARCMSR";
1552 
1553 		memcpy(pcmdmessagefld->messagedatabuffer, hello_string
1554 			, (int16_t)strlen(hello_string));
1555 		pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1556 		}
1557 		break;
1558 
1559 	case ARCMSR_MESSAGE_SAY_GOODBYE:
1560 		arcmsr_iop_parking(acb);
1561 		break;
1562 
1563 	case ARCMSR_MESSAGE_FLUSH_ADAPTER_CACHE:
1564 		arcmsr_flush_adapter_cache(acb);
1565 		break;
1566 
1567 	default:
1568 		retvalue = ARCMSR_MESSAGE_FAIL;
1569 	}
1570 	message_out:
1571 	sg = scsi_sglist(cmd);
1572 	kunmap_atomic(buffer - sg->offset, KM_IRQ0);
1573 	return retvalue;
1574 }
1575 
1576 static struct CommandControlBlock *arcmsr_get_freeccb(struct AdapterControlBlock *acb)
1577 {
1578 	struct list_head *head = &acb->ccb_free_list;
1579 	struct CommandControlBlock *ccb = NULL;
1580 
1581 	if (!list_empty(head)) {
1582 		ccb = list_entry(head->next, struct CommandControlBlock, list);
1583 		list_del(head->next);
1584 	}
1585 	return ccb;
1586 }
1587 
1588 static void arcmsr_handle_virtual_command(struct AdapterControlBlock *acb,
1589 		struct scsi_cmnd *cmd)
1590 {
1591 	switch (cmd->cmnd[0]) {
1592 	case INQUIRY: {
1593 		unsigned char inqdata[36];
1594 		char *buffer;
1595 		struct scatterlist *sg;
1596 
1597 		if (cmd->device->lun) {
1598 			cmd->result = (DID_TIME_OUT << 16);
1599 			cmd->scsi_done(cmd);
1600 			return;
1601 		}
1602 		inqdata[0] = TYPE_PROCESSOR;
1603 		/* Periph Qualifier & Periph Dev Type */
1604 		inqdata[1] = 0;
1605 		/* rem media bit & Dev Type Modifier */
1606 		inqdata[2] = 0;
1607 		/* ISO, ECMA, & ANSI versions */
1608 		inqdata[4] = 31;
1609 		/* length of additional data */
1610 		strncpy(&inqdata[8], "Areca   ", 8);
1611 		/* Vendor Identification */
1612 		strncpy(&inqdata[16], "RAID controller ", 16);
1613 		/* Product Identification */
1614 		strncpy(&inqdata[32], "R001", 4); /* Product Revision */
1615 
1616 		sg = scsi_sglist(cmd);
1617 		buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
1618 
1619 		memcpy(buffer, inqdata, sizeof(inqdata));
1620 		sg = scsi_sglist(cmd);
1621 		kunmap_atomic(buffer - sg->offset, KM_IRQ0);
1622 
1623 		cmd->scsi_done(cmd);
1624 	}
1625 	break;
1626 	case WRITE_BUFFER:
1627 	case READ_BUFFER: {
1628 		if (arcmsr_iop_message_xfer(acb, cmd))
1629 			cmd->result = (DID_ERROR << 16);
1630 		cmd->scsi_done(cmd);
1631 	}
1632 	break;
1633 	default:
1634 		cmd->scsi_done(cmd);
1635 	}
1636 }
1637 
1638 static int arcmsr_queue_command(struct scsi_cmnd *cmd,
1639 	void (* done)(struct scsi_cmnd *))
1640 {
1641 	struct Scsi_Host *host = cmd->device->host;
1642 	struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
1643 	struct CommandControlBlock *ccb;
1644 	int target = cmd->device->id;
1645 	int lun = cmd->device->lun;
1646 
1647 	cmd->scsi_done = done;
1648 	cmd->host_scribble = NULL;
1649 	cmd->result = 0;
1650 	if (acb->acb_flags & ACB_F_BUS_RESET) {
1651 		printk(KERN_NOTICE "arcmsr%d: bus reset"
1652 			" and return busy \n"
1653 			, acb->host->host_no);
1654 		return SCSI_MLQUEUE_HOST_BUSY;
1655 	}
1656 	if (target == 16) {
1657 		/* virtual device for iop message transfer */
1658 		arcmsr_handle_virtual_command(acb, cmd);
1659 		return 0;
1660 	}
1661 	if (acb->devstate[target][lun] == ARECA_RAID_GONE) {
1662 		uint8_t block_cmd;
1663 
1664 		block_cmd = cmd->cmnd[0] & 0x0f;
1665 		if (block_cmd == 0x08 || block_cmd == 0x0a) {
1666 			printk(KERN_NOTICE
1667 				"arcmsr%d: block 'read/write'"
1668 				"command with gone raid volume"
1669 				" Cmd = %2x, TargetId = %d, Lun = %d \n"
1670 				, acb->host->host_no
1671 				, cmd->cmnd[0]
1672 				, target, lun);
1673 			cmd->result = (DID_NO_CONNECT << 16);
1674 			cmd->scsi_done(cmd);
1675 			return 0;
1676 		}
1677 	}
1678 	if (atomic_read(&acb->ccboutstandingcount) >=
1679 			ARCMSR_MAX_OUTSTANDING_CMD)
1680 		return SCSI_MLQUEUE_HOST_BUSY;
1681 
1682 	ccb = arcmsr_get_freeccb(acb);
1683 	if (!ccb)
1684 		return SCSI_MLQUEUE_HOST_BUSY;
1685 
1686 	arcmsr_build_ccb(acb, ccb, cmd);
1687 	arcmsr_post_ccb(acb, ccb);
1688 	return 0;
1689 }
1690 
1691 static void arcmsr_get_hba_config(struct AdapterControlBlock *acb)
1692 {
1693 	struct MessageUnit_A __iomem *reg = acb->pmuA;
1694 	char *acb_firm_model = acb->firm_model;
1695 	char *acb_firm_version = acb->firm_version;
1696 	char __iomem *iop_firm_model = (char __iomem *)(&reg->message_rwbuffer[15]);
1697 	char __iomem *iop_firm_version = (char __iomem *)(&reg->message_rwbuffer[17]);
1698 	int count;
1699 
1700 	writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
1701 	if (arcmsr_hba_wait_msgint_ready(acb)) {
1702 		printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
1703 			miscellaneous data' timeout \n", acb->host->host_no);
1704 	}
1705 
1706 	count = 8;
1707 	while (count) {
1708 		*acb_firm_model = readb(iop_firm_model);
1709 		acb_firm_model++;
1710 		iop_firm_model++;
1711 		count--;
1712 	}
1713 
1714 	count = 16;
1715 	while (count) {
1716 		*acb_firm_version = readb(iop_firm_version);
1717 		acb_firm_version++;
1718 		iop_firm_version++;
1719 		count--;
1720 	}
1721 
1722 	printk(KERN_INFO 	"ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n"
1723 		, acb->host->host_no
1724 		, acb->firm_version);
1725 
1726 	acb->firm_request_len = readl(&reg->message_rwbuffer[1]);
1727 	acb->firm_numbers_queue = readl(&reg->message_rwbuffer[2]);
1728 	acb->firm_sdram_size = readl(&reg->message_rwbuffer[3]);
1729 	acb->firm_hd_channels = readl(&reg->message_rwbuffer[4]);
1730 }
1731 
1732 static void arcmsr_get_hbb_config(struct AdapterControlBlock *acb)
1733 {
1734 	struct MessageUnit_B *reg = acb->pmuB;
1735 	uint32_t __iomem *lrwbuffer = reg->msgcode_rwbuffer_reg;
1736 	char *acb_firm_model = acb->firm_model;
1737 	char *acb_firm_version = acb->firm_version;
1738 	char __iomem *iop_firm_model = (char __iomem *)(&lrwbuffer[15]);
1739 	/*firm_model,15,60-67*/
1740 	char __iomem *iop_firm_version = (char __iomem *)(&lrwbuffer[17]);
1741 	/*firm_version,17,68-83*/
1742 	int count;
1743 
1744 	writel(ARCMSR_MESSAGE_GET_CONFIG, reg->drv2iop_doorbell_reg);
1745 	if (arcmsr_hbb_wait_msgint_ready(acb)) {
1746 		printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
1747 			miscellaneous data' timeout \n", acb->host->host_no);
1748 	}
1749 
1750 	count = 8;
1751 	while (count)
1752 	{
1753 		*acb_firm_model = readb(iop_firm_model);
1754 		acb_firm_model++;
1755 		iop_firm_model++;
1756 		count--;
1757 	}
1758 
1759 	count = 16;
1760 	while (count)
1761 	{
1762 		*acb_firm_version = readb(iop_firm_version);
1763 		acb_firm_version++;
1764 		iop_firm_version++;
1765 		count--;
1766 	}
1767 
1768 	printk(KERN_INFO "ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n",
1769 			acb->host->host_no,
1770 			acb->firm_version);
1771 
1772 	lrwbuffer++;
1773 	acb->firm_request_len = readl(lrwbuffer++);
1774 	/*firm_request_len,1,04-07*/
1775 	acb->firm_numbers_queue = readl(lrwbuffer++);
1776 	/*firm_numbers_queue,2,08-11*/
1777 	acb->firm_sdram_size = readl(lrwbuffer++);
1778 	/*firm_sdram_size,3,12-15*/
1779 	acb->firm_hd_channels = readl(lrwbuffer);
1780 	/*firm_ide_channels,4,16-19*/
1781 }
1782 
1783 static void arcmsr_get_firmware_spec(struct AdapterControlBlock *acb)
1784 {
1785 	switch (acb->adapter_type) {
1786 	case ACB_ADAPTER_TYPE_A: {
1787 		arcmsr_get_hba_config(acb);
1788 		}
1789 		break;
1790 
1791 	case ACB_ADAPTER_TYPE_B: {
1792 		arcmsr_get_hbb_config(acb);
1793 		}
1794 		break;
1795 	}
1796 }
1797 
1798 static void arcmsr_polling_hba_ccbdone(struct AdapterControlBlock *acb,
1799 	struct CommandControlBlock *poll_ccb)
1800 {
1801 	struct MessageUnit_A __iomem *reg = acb->pmuA;
1802 	struct CommandControlBlock *ccb;
1803 	uint32_t flag_ccb, outbound_intstatus, poll_ccb_done = 0, poll_count = 0;
1804 
1805 	polling_hba_ccb_retry:
1806 	poll_count++;
1807 	outbound_intstatus = readl(&reg->outbound_intstatus) & acb->outbound_int_enable;
1808 	writel(outbound_intstatus, &reg->outbound_intstatus);/*clear interrupt*/
1809 	while (1) {
1810 		if ((flag_ccb = readl(&reg->outbound_queueport)) == 0xFFFFFFFF) {
1811 			if (poll_ccb_done)
1812 				break;
1813 			else {
1814 				msleep(25);
1815 				if (poll_count > 100)
1816 					break;
1817 				goto polling_hba_ccb_retry;
1818 			}
1819 		}
1820 		ccb = (struct CommandControlBlock *)(acb->vir2phy_offset + (flag_ccb << 5));
1821 		poll_ccb_done = (ccb == poll_ccb) ? 1:0;
1822 		if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
1823 			if ((ccb->startdone == ARCMSR_CCB_ABORTED) || (ccb == poll_ccb)) {
1824 				printk(KERN_NOTICE "arcmsr%d: scsi id = %d lun = %d ccb = '0x%p'"
1825 					" poll command abort successfully \n"
1826 					, acb->host->host_no
1827 					, ccb->pcmd->device->id
1828 					, ccb->pcmd->device->lun
1829 					, ccb);
1830 				ccb->pcmd->result = DID_ABORT << 16;
1831 				arcmsr_ccb_complete(ccb, 1);
1832 				poll_ccb_done = 1;
1833 				continue;
1834 			}
1835 			printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
1836 				" command done ccb = '0x%p'"
1837 				"ccboutstandingcount = %d \n"
1838 				, acb->host->host_no
1839 				, ccb
1840 				, atomic_read(&acb->ccboutstandingcount));
1841 			continue;
1842 		}
1843 		arcmsr_report_ccb_state(acb, ccb, flag_ccb);
1844 	}
1845 }
1846 
1847 static void arcmsr_polling_hbb_ccbdone(struct AdapterControlBlock *acb, \
1848 					struct CommandControlBlock *poll_ccb)
1849 {
1850 		struct MessageUnit_B *reg = acb->pmuB;
1851 		struct CommandControlBlock *ccb;
1852 		uint32_t flag_ccb, poll_ccb_done = 0, poll_count = 0;
1853 		int index;
1854 
1855 	polling_hbb_ccb_retry:
1856 		poll_count++;
1857 		/* clear doorbell interrupt */
1858 		writel(ARCMSR_DOORBELL_INT_CLEAR_PATTERN, reg->iop2drv_doorbell_reg);
1859 		while (1) {
1860 			index = reg->doneq_index;
1861 			if ((flag_ccb = readl(&reg->done_qbuffer[index])) == 0) {
1862 				if (poll_ccb_done)
1863 					break;
1864 				else {
1865 					msleep(25);
1866 					if (poll_count > 100)
1867 						break;
1868 					goto polling_hbb_ccb_retry;
1869 				}
1870 			}
1871 			writel(0, &reg->done_qbuffer[index]);
1872 			index++;
1873 			/*if last index number set it to 0 */
1874 			index %= ARCMSR_MAX_HBB_POSTQUEUE;
1875 			reg->doneq_index = index;
1876 			/* check ifcommand done with no error*/
1877 			ccb = (struct CommandControlBlock *)\
1878       (acb->vir2phy_offset + (flag_ccb << 5));/*frame must be 32 bytes aligned*/
1879 			poll_ccb_done = (ccb == poll_ccb) ? 1:0;
1880 			if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
1881 				if (ccb->startdone == ARCMSR_CCB_ABORTED) {
1882 					printk(KERN_NOTICE "arcmsr%d: \
1883 		scsi id = %d lun = %d ccb = '0x%p' poll command abort successfully \n"
1884 						,acb->host->host_no
1885 						,ccb->pcmd->device->id
1886 						,ccb->pcmd->device->lun
1887 						,ccb);
1888 					ccb->pcmd->result = DID_ABORT << 16;
1889 					arcmsr_ccb_complete(ccb, 1);
1890 					continue;
1891 				}
1892 				printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
1893 					" command done ccb = '0x%p'"
1894 					"ccboutstandingcount = %d \n"
1895 					, acb->host->host_no
1896 					, ccb
1897 					, atomic_read(&acb->ccboutstandingcount));
1898 				continue;
1899 			}
1900 			arcmsr_report_ccb_state(acb, ccb, flag_ccb);
1901 		}	/*drain reply FIFO*/
1902 }
1903 
1904 static void arcmsr_polling_ccbdone(struct AdapterControlBlock *acb, \
1905 					struct CommandControlBlock *poll_ccb)
1906 {
1907 	switch (acb->adapter_type) {
1908 
1909 	case ACB_ADAPTER_TYPE_A: {
1910 		arcmsr_polling_hba_ccbdone(acb,poll_ccb);
1911 		}
1912 		break;
1913 
1914 	case ACB_ADAPTER_TYPE_B: {
1915 		arcmsr_polling_hbb_ccbdone(acb,poll_ccb);
1916 		}
1917 	}
1918 }
1919 
1920 static int arcmsr_iop_confirm(struct AdapterControlBlock *acb)
1921 {
1922 	uint32_t cdb_phyaddr, ccb_phyaddr_hi32;
1923 	dma_addr_t dma_coherent_handle;
1924 	/*
1925 	********************************************************************
1926 	** here we need to tell iop 331 our freeccb.HighPart
1927 	** if freeccb.HighPart is not zero
1928 	********************************************************************
1929 	*/
1930 	dma_coherent_handle = acb->dma_coherent_handle;
1931 	cdb_phyaddr = (uint32_t)(dma_coherent_handle);
1932 	ccb_phyaddr_hi32 = (uint32_t)((cdb_phyaddr >> 16) >> 16);
1933 	/*
1934 	***********************************************************************
1935 	**    if adapter type B, set window of "post command Q"
1936 	***********************************************************************
1937 	*/
1938 	switch (acb->adapter_type) {
1939 
1940 	case ACB_ADAPTER_TYPE_A: {
1941 		if (ccb_phyaddr_hi32 != 0) {
1942 			struct MessageUnit_A __iomem *reg = acb->pmuA;
1943 			uint32_t intmask_org;
1944 			intmask_org = arcmsr_disable_outbound_ints(acb);
1945 			writel(ARCMSR_SIGNATURE_SET_CONFIG, \
1946 						&reg->message_rwbuffer[0]);
1947 			writel(ccb_phyaddr_hi32, &reg->message_rwbuffer[1]);
1948 			writel(ARCMSR_INBOUND_MESG0_SET_CONFIG, \
1949 							&reg->inbound_msgaddr0);
1950 			if (arcmsr_hba_wait_msgint_ready(acb)) {
1951 				printk(KERN_NOTICE "arcmsr%d: ""set ccb high \
1952 				part physical address timeout\n",
1953 				acb->host->host_no);
1954 				return 1;
1955 			}
1956 			arcmsr_enable_outbound_ints(acb, intmask_org);
1957 		}
1958 		}
1959 		break;
1960 
1961 	case ACB_ADAPTER_TYPE_B: {
1962 		unsigned long post_queue_phyaddr;
1963 		uint32_t __iomem *rwbuffer;
1964 
1965 		struct MessageUnit_B *reg = acb->pmuB;
1966 		uint32_t intmask_org;
1967 		intmask_org = arcmsr_disable_outbound_ints(acb);
1968 		reg->postq_index = 0;
1969 		reg->doneq_index = 0;
1970 		writel(ARCMSR_MESSAGE_SET_POST_WINDOW, reg->drv2iop_doorbell_reg);
1971 		if (arcmsr_hbb_wait_msgint_ready(acb)) {
1972 			printk(KERN_NOTICE "arcmsr%d:can not set diver mode\n", \
1973 				acb->host->host_no);
1974 			return 1;
1975 		}
1976 		post_queue_phyaddr = cdb_phyaddr + ARCMSR_MAX_FREECCB_NUM * \
1977 		sizeof(struct CommandControlBlock) + offsetof(struct MessageUnit_B, post_qbuffer) ;
1978 		rwbuffer = reg->msgcode_rwbuffer_reg;
1979 		/* driver "set config" signature */
1980 		writel(ARCMSR_SIGNATURE_SET_CONFIG, rwbuffer++);
1981 		/* normal should be zero */
1982 		writel(ccb_phyaddr_hi32, rwbuffer++);
1983 		/* postQ size (256 + 8)*4	 */
1984 		writel(post_queue_phyaddr, rwbuffer++);
1985 		/* doneQ size (256 + 8)*4	 */
1986 		writel(post_queue_phyaddr + 1056, rwbuffer++);
1987 		/* ccb maxQ size must be --> [(256 + 8)*4]*/
1988 		writel(1056, rwbuffer);
1989 
1990 		writel(ARCMSR_MESSAGE_SET_CONFIG, reg->drv2iop_doorbell_reg);
1991 		if (arcmsr_hbb_wait_msgint_ready(acb)) {
1992 			printk(KERN_NOTICE "arcmsr%d: 'set command Q window' \
1993 			timeout \n",acb->host->host_no);
1994 			return 1;
1995 		}
1996 
1997 		writel(ARCMSR_MESSAGE_START_DRIVER_MODE, reg->drv2iop_doorbell_reg);
1998 		if (arcmsr_hbb_wait_msgint_ready(acb)) {
1999 			printk(KERN_NOTICE "arcmsr%d: 'can not set diver mode \n"\
2000 			,acb->host->host_no);
2001 			return 1;
2002 		}
2003 		arcmsr_enable_outbound_ints(acb, intmask_org);
2004 		}
2005 		break;
2006 	}
2007 	return 0;
2008 }
2009 
2010 static void arcmsr_wait_firmware_ready(struct AdapterControlBlock *acb)
2011 {
2012 	uint32_t firmware_state = 0;
2013 
2014 	switch (acb->adapter_type) {
2015 
2016 	case ACB_ADAPTER_TYPE_A: {
2017 		struct MessageUnit_A __iomem *reg = acb->pmuA;
2018 		do {
2019 			firmware_state = readl(&reg->outbound_msgaddr1);
2020 		} while ((firmware_state & ARCMSR_OUTBOUND_MESG1_FIRMWARE_OK) == 0);
2021 		}
2022 		break;
2023 
2024 	case ACB_ADAPTER_TYPE_B: {
2025 		struct MessageUnit_B *reg = acb->pmuB;
2026 		do {
2027 			firmware_state = readl(reg->iop2drv_doorbell_reg);
2028 		} while ((firmware_state & ARCMSR_MESSAGE_FIRMWARE_OK) == 0);
2029 		}
2030 		break;
2031 	}
2032 }
2033 
2034 static void arcmsr_start_hba_bgrb(struct AdapterControlBlock *acb)
2035 {
2036 	struct MessageUnit_A __iomem *reg = acb->pmuA;
2037 	acb->acb_flags |= ACB_F_MSG_START_BGRB;
2038 	writel(ARCMSR_INBOUND_MESG0_START_BGRB, &reg->inbound_msgaddr0);
2039 	if (arcmsr_hba_wait_msgint_ready(acb)) {
2040 		printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
2041 				rebulid' timeout \n", acb->host->host_no);
2042 	}
2043 }
2044 
2045 static void arcmsr_start_hbb_bgrb(struct AdapterControlBlock *acb)
2046 {
2047 	struct MessageUnit_B *reg = acb->pmuB;
2048 	acb->acb_flags |= ACB_F_MSG_START_BGRB;
2049 	writel(ARCMSR_MESSAGE_START_BGRB, reg->drv2iop_doorbell_reg);
2050 	if (arcmsr_hbb_wait_msgint_ready(acb)) {
2051 		printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
2052 				rebulid' timeout \n",acb->host->host_no);
2053 	}
2054 }
2055 
2056 static void arcmsr_start_adapter_bgrb(struct AdapterControlBlock *acb)
2057 {
2058 	switch (acb->adapter_type) {
2059 	case ACB_ADAPTER_TYPE_A:
2060 		arcmsr_start_hba_bgrb(acb);
2061 		break;
2062 	case ACB_ADAPTER_TYPE_B:
2063 		arcmsr_start_hbb_bgrb(acb);
2064 		break;
2065 	}
2066 }
2067 
2068 static void arcmsr_clear_doorbell_queue_buffer(struct AdapterControlBlock *acb)
2069 {
2070 	switch (acb->adapter_type) {
2071 	case ACB_ADAPTER_TYPE_A: {
2072 		struct MessageUnit_A __iomem *reg = acb->pmuA;
2073 		uint32_t outbound_doorbell;
2074 		/* empty doorbell Qbuffer if door bell ringed */
2075 		outbound_doorbell = readl(&reg->outbound_doorbell);
2076 		/*clear doorbell interrupt */
2077 		writel(outbound_doorbell, &reg->outbound_doorbell);
2078 		writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, &reg->inbound_doorbell);
2079 		}
2080 		break;
2081 
2082 	case ACB_ADAPTER_TYPE_B: {
2083 		struct MessageUnit_B *reg = acb->pmuB;
2084 		/*clear interrupt and message state*/
2085 		writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN, reg->iop2drv_doorbell_reg);
2086 		writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell_reg);
2087 		/* let IOP know data has been read */
2088 		}
2089 		break;
2090 	}
2091 }
2092 
2093 static void arcmsr_iop_init(struct AdapterControlBlock *acb)
2094 {
2095 	uint32_t intmask_org;
2096 
2097 	arcmsr_wait_firmware_ready(acb);
2098 	arcmsr_iop_confirm(acb);
2099        /* disable all outbound interrupt */
2100        intmask_org = arcmsr_disable_outbound_ints(acb);
2101 	arcmsr_get_firmware_spec(acb);
2102 	/*start background rebuild*/
2103 	arcmsr_start_adapter_bgrb(acb);
2104 	/* empty doorbell Qbuffer if door bell ringed */
2105 	arcmsr_clear_doorbell_queue_buffer(acb);
2106 	/* enable outbound Post Queue,outbound doorbell Interrupt */
2107 	arcmsr_enable_outbound_ints(acb, intmask_org);
2108 	acb->acb_flags |= ACB_F_IOP_INITED;
2109 }
2110 
2111 static void arcmsr_iop_reset(struct AdapterControlBlock *acb)
2112 {
2113 	struct CommandControlBlock *ccb;
2114 	uint32_t intmask_org;
2115 	int i = 0;
2116 
2117 	if (atomic_read(&acb->ccboutstandingcount) != 0) {
2118 		/* talk to iop 331 outstanding command aborted */
2119 		arcmsr_abort_allcmd(acb);
2120 
2121 		/* wait for 3 sec for all command aborted*/
2122 		ssleep(3);
2123 
2124 		/* disable all outbound interrupt */
2125 		intmask_org = arcmsr_disable_outbound_ints(acb);
2126 		/* clear all outbound posted Q */
2127 		arcmsr_done4abort_postqueue(acb);
2128 		for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2129 			ccb = acb->pccb_pool[i];
2130 			if (ccb->startdone == ARCMSR_CCB_START) {
2131 				ccb->startdone = ARCMSR_CCB_ABORTED;
2132 				arcmsr_ccb_complete(ccb, 1);
2133 			}
2134 		}
2135 		/* enable all outbound interrupt */
2136 		arcmsr_enable_outbound_ints(acb, intmask_org);
2137 	}
2138 }
2139 
2140 static int arcmsr_bus_reset(struct scsi_cmnd *cmd)
2141 {
2142 	struct AdapterControlBlock *acb =
2143 		(struct AdapterControlBlock *)cmd->device->host->hostdata;
2144 	int i;
2145 
2146 	acb->num_resets++;
2147 	acb->acb_flags |= ACB_F_BUS_RESET;
2148 	for (i = 0; i < 400; i++) {
2149 		if (!atomic_read(&acb->ccboutstandingcount))
2150 			break;
2151 		arcmsr_interrupt(acb);/* FIXME: need spinlock */
2152 		msleep(25);
2153 	}
2154 	arcmsr_iop_reset(acb);
2155 	acb->acb_flags &= ~ACB_F_BUS_RESET;
2156 	return SUCCESS;
2157 }
2158 
2159 static void arcmsr_abort_one_cmd(struct AdapterControlBlock *acb,
2160 		struct CommandControlBlock *ccb)
2161 {
2162 	u32 intmask;
2163 
2164 	ccb->startdone = ARCMSR_CCB_ABORTED;
2165 
2166 	/*
2167 	** Wait for 3 sec for all command done.
2168 	*/
2169 	ssleep(3);
2170 
2171 	intmask = arcmsr_disable_outbound_ints(acb);
2172 	arcmsr_polling_ccbdone(acb, ccb);
2173 	arcmsr_enable_outbound_ints(acb, intmask);
2174 }
2175 
2176 static int arcmsr_abort(struct scsi_cmnd *cmd)
2177 {
2178 	struct AdapterControlBlock *acb =
2179 		(struct AdapterControlBlock *)cmd->device->host->hostdata;
2180 	int i = 0;
2181 
2182 	printk(KERN_NOTICE
2183 		"arcmsr%d: abort device command of scsi id = %d lun = %d \n",
2184 		acb->host->host_no, cmd->device->id, cmd->device->lun);
2185 	acb->num_aborts++;
2186 	/*
2187 	************************************************
2188 	** the all interrupt service routine is locked
2189 	** we need to handle it as soon as possible and exit
2190 	************************************************
2191 	*/
2192 	if (!atomic_read(&acb->ccboutstandingcount))
2193 		return SUCCESS;
2194 
2195 	for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2196 		struct CommandControlBlock *ccb = acb->pccb_pool[i];
2197 		if (ccb->startdone == ARCMSR_CCB_START && ccb->pcmd == cmd) {
2198 			arcmsr_abort_one_cmd(acb, ccb);
2199 			break;
2200 		}
2201 	}
2202 
2203 	return SUCCESS;
2204 }
2205 
2206 static const char *arcmsr_info(struct Scsi_Host *host)
2207 {
2208 	struct AdapterControlBlock *acb =
2209 		(struct AdapterControlBlock *) host->hostdata;
2210 	static char buf[256];
2211 	char *type;
2212 	int raid6 = 1;
2213 
2214 	switch (acb->pdev->device) {
2215 	case PCI_DEVICE_ID_ARECA_1110:
2216 	case PCI_DEVICE_ID_ARECA_1200:
2217 	case PCI_DEVICE_ID_ARECA_1202:
2218 	case PCI_DEVICE_ID_ARECA_1210:
2219 		raid6 = 0;
2220 		/*FALLTHRU*/
2221 	case PCI_DEVICE_ID_ARECA_1120:
2222 	case PCI_DEVICE_ID_ARECA_1130:
2223 	case PCI_DEVICE_ID_ARECA_1160:
2224 	case PCI_DEVICE_ID_ARECA_1170:
2225 	case PCI_DEVICE_ID_ARECA_1201:
2226 	case PCI_DEVICE_ID_ARECA_1220:
2227 	case PCI_DEVICE_ID_ARECA_1230:
2228 	case PCI_DEVICE_ID_ARECA_1260:
2229 	case PCI_DEVICE_ID_ARECA_1270:
2230 	case PCI_DEVICE_ID_ARECA_1280:
2231 		type = "SATA";
2232 		break;
2233 	case PCI_DEVICE_ID_ARECA_1380:
2234 	case PCI_DEVICE_ID_ARECA_1381:
2235 	case PCI_DEVICE_ID_ARECA_1680:
2236 	case PCI_DEVICE_ID_ARECA_1681:
2237 		type = "SAS";
2238 		break;
2239 	default:
2240 		type = "X-TYPE";
2241 		break;
2242 	}
2243 	sprintf(buf, "Areca %s Host Adapter RAID Controller%s\n %s",
2244 			type, raid6 ? "( RAID6 capable)" : "",
2245 			ARCMSR_DRIVER_VERSION);
2246 	return buf;
2247 }
2248 #ifdef CONFIG_SCSI_ARCMSR_AER
2249 static pci_ers_result_t arcmsr_pci_slot_reset(struct pci_dev *pdev)
2250 {
2251 	struct Scsi_Host *host = pci_get_drvdata(pdev);
2252 	struct AdapterControlBlock *acb =
2253 		(struct AdapterControlBlock *) host->hostdata;
2254 	uint32_t intmask_org;
2255 	int i, j;
2256 
2257 	if (pci_enable_device(pdev)) {
2258 		return PCI_ERS_RESULT_DISCONNECT;
2259 	}
2260 	pci_set_master(pdev);
2261 	intmask_org = arcmsr_disable_outbound_ints(acb);
2262 	acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
2263 			   ACB_F_MESSAGE_RQBUFFER_CLEARED |
2264 			   ACB_F_MESSAGE_WQBUFFER_READED);
2265 	acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
2266 	for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
2267 		for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
2268 			acb->devstate[i][j] = ARECA_RAID_GONE;
2269 
2270 	arcmsr_wait_firmware_ready(acb);
2271 	arcmsr_iop_confirm(acb);
2272        /* disable all outbound interrupt */
2273 	arcmsr_get_firmware_spec(acb);
2274 	/*start background rebuild*/
2275 	arcmsr_start_adapter_bgrb(acb);
2276 	/* empty doorbell Qbuffer if door bell ringed */
2277 	arcmsr_clear_doorbell_queue_buffer(acb);
2278 	/* enable outbound Post Queue,outbound doorbell Interrupt */
2279 	arcmsr_enable_outbound_ints(acb, intmask_org);
2280 	acb->acb_flags |= ACB_F_IOP_INITED;
2281 
2282 	pci_enable_pcie_error_reporting(pdev);
2283 	return PCI_ERS_RESULT_RECOVERED;
2284 }
2285 
2286 static void arcmsr_pci_ers_need_reset_forepart(struct pci_dev *pdev)
2287 {
2288 	struct Scsi_Host *host = pci_get_drvdata(pdev);
2289 	struct AdapterControlBlock *acb = (struct AdapterControlBlock *)host->hostdata;
2290 	struct CommandControlBlock *ccb;
2291 	uint32_t intmask_org;
2292 	int i = 0;
2293 
2294 	if (atomic_read(&acb->ccboutstandingcount) != 0) {
2295 		/* talk to iop 331 outstanding command aborted */
2296 		arcmsr_abort_allcmd(acb);
2297 		/* wait for 3 sec for all command aborted*/
2298 		ssleep(3);
2299 		/* disable all outbound interrupt */
2300 		intmask_org = arcmsr_disable_outbound_ints(acb);
2301 		/* clear all outbound posted Q */
2302 		arcmsr_done4abort_postqueue(acb);
2303 		for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2304 			ccb = acb->pccb_pool[i];
2305 			if (ccb->startdone == ARCMSR_CCB_START) {
2306 				ccb->startdone = ARCMSR_CCB_ABORTED;
2307 				arcmsr_ccb_complete(ccb, 1);
2308 			}
2309 		}
2310 		/* enable all outbound interrupt */
2311 		arcmsr_enable_outbound_ints(acb, intmask_org);
2312 	}
2313 	pci_disable_device(pdev);
2314 }
2315 
2316 static void arcmsr_pci_ers_disconnect_forepart(struct pci_dev *pdev)
2317 {
2318 			struct Scsi_Host *host = pci_get_drvdata(pdev);
2319 			struct AdapterControlBlock *acb	= \
2320 				(struct AdapterControlBlock *)host->hostdata;
2321 
2322 			arcmsr_stop_adapter_bgrb(acb);
2323 			arcmsr_flush_adapter_cache(acb);
2324 }
2325 
2326 static pci_ers_result_t arcmsr_pci_error_detected(struct pci_dev *pdev,
2327 						pci_channel_state_t state)
2328 {
2329 	switch (state) {
2330 	case pci_channel_io_frozen:
2331 			arcmsr_pci_ers_need_reset_forepart(pdev);
2332 			return PCI_ERS_RESULT_NEED_RESET;
2333 	case pci_channel_io_perm_failure:
2334 			arcmsr_pci_ers_disconnect_forepart(pdev);
2335 			return PCI_ERS_RESULT_DISCONNECT;
2336 			break;
2337 	default:
2338 			return PCI_ERS_RESULT_NEED_RESET;
2339 	  }
2340 }
2341 #endif
2342