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