blob: ffa54792bb330126615a49a873b79bc718f01b78 [file] [log] [blame]
Erich Chen1c57e862006-07-12 08:59:32 -07001/*
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
Nick Cheng1a4f5502007-09-13 17:26:40 +080012** E-mail: support@areca.com.tw
Erich Chen1c57e862006-07-12 08:59:32 -070013**
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>
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +080060#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Erich Chen1c57e862006-07-12 08:59:32 -070062#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
Nick Cheng36b83de2010-05-17 11:22:42 +080075#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
Nick Cheng1a4f5502007-09-13 17:26:40 +080083MODULE_AUTHOR("Erich Chen <support@areca.com.tw>");
Nick Cheng36b83de2010-05-17 11:22:42 +080084MODULE_DESCRIPTION("ARECA (ARC11xx/12xx/13xx/16xx) SATA/SAS RAID Host Bus Adapter");
Erich Chen1c57e862006-07-12 08:59:32 -070085MODULE_LICENSE("Dual BSD/GPL");
86MODULE_VERSION(ARCMSR_DRIVER_VERSION);
87
Nick Cheng1a4f5502007-09-13 17:26:40 +080088static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb,
89 struct scsi_cmnd *cmd);
90static int arcmsr_iop_confirm(struct AdapterControlBlock *acb);
Erich Chen1c57e862006-07-12 08:59:32 -070091static int arcmsr_abort(struct scsi_cmnd *);
92static int arcmsr_bus_reset(struct scsi_cmnd *);
93static int arcmsr_bios_param(struct scsi_device *sdev,
Nick Cheng1a4f5502007-09-13 17:26:40 +080094 struct block_device *bdev, sector_t capacity, int *info);
95static int arcmsr_queue_command(struct scsi_cmnd *cmd,
96 void (*done) (struct scsi_cmnd *));
Erich Chen1c57e862006-07-12 08:59:32 -070097static 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);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800103static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700104static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800105static void arcmsr_flush_hba_cache(struct AdapterControlBlock *acb);
106static void arcmsr_flush_hbb_cache(struct AdapterControlBlock *acb);
Nick Cheng36b83de2010-05-17 11:22:42 +0800107static 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
Erich Chen1c57e862006-07-12 08:59:32 -0700114static const char *arcmsr_info(struct Scsi_Host *);
115static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800116static int arcmsr_adjust_disk_queue_depth(struct scsi_device *sdev,
Mike Christiee881a172009-10-15 17:46:39 -0700117 int queue_depth, int reason)
Erich Chen1c57e862006-07-12 08:59:32 -0700118{
Mike Christiee881a172009-10-15 17:46:39 -0700119 if (reason != SCSI_QDEPTH_DEFAULT)
120 return -EOPNOTSUPP;
121
Erich Chen1c57e862006-07-12 08:59:32 -0700122 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,
Nick Cheng36b83de2010-05-17 11:22:42 +0800130 .name = "ARCMSR ARECA SATA/SAS RAID Host Bus Adapter"
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800131 ARCMSR_DRIVER_VERSION,
Erich Chen1c57e862006-07-12 08:59:32 -0700132 .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};
Erich Chen1c57e862006-07-12 08:59:32 -0700146static 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)},
Nick Cheng1a4f5502007-09-13 17:26:40 +0800152 {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)},
Erich Chen1c57e862006-07-12 08:59:32 -0700155 {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,
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800173 .shutdown = arcmsr_shutdown,
Erich Chen1c57e862006-07-12 08:59:32 -0700174};
175
David Howells7d12e782006-10-05 14:55:46 +0100176static irqreturn_t arcmsr_do_interrupt(int irq, void *dev_id)
Erich Chen1c57e862006-07-12 08:59:32 -0700177{
178 irqreturn_t handle_state;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800179 struct AdapterControlBlock *acb = dev_id;
Erich Chen1c57e862006-07-12 08:59:32 -0700180
Nick Cheng1a4f5502007-09-13 17:26:40 +0800181 spin_lock(acb->host->host_lock);
Erich Chen1c57e862006-07-12 08:59:32 -0700182 handle_state = arcmsr_interrupt(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800183 spin_unlock(acb->host->host_lock);
184
Erich Chen1c57e862006-07-12 08:59:32 -0700185 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
Nick Cheng1a4f5502007-09-13 17:26:40 +0800216static void arcmsr_define_adapter_type(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -0700217{
218 struct pci_dev *pdev = acb->pdev;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800219 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;
Erich Chen1c57e862006-07-12 08:59:32 -0700226
Nick Cheng1a4f5502007-09-13 17:26:40 +0800227 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) {
235
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;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800241 int i, j;
242
Nick Cheng36b83de2010-05-17 11:22:42 +0800243 acb->pmuA = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
Al Viro80da1ad2007-10-29 05:08:28 +0000244 if (!acb->pmuA) {
Nick Cheng1a4f5502007-09-13 17:26:40 +0800245 printk(KERN_NOTICE "arcmsr%d: memory mapping region fail \n",
246 acb->host->host_no);
Al Virodb3a91f2007-10-29 05:08:38 +0000247 return -ENOMEM;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800248 }
249
250 dma_coherent = dma_alloc_coherent(&pdev->dev,
Erich Chen1c57e862006-07-12 08:59:32 -0700251 ARCMSR_MAX_FREECCB_NUM *
252 sizeof (struct CommandControlBlock) + 0x20,
253 &dma_coherent_handle, GFP_KERNEL);
Al Virodb3a91f2007-10-29 05:08:38 +0000254
255 if (!dma_coherent) {
256 iounmap(acb->pmuA);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800257 return -ENOMEM;
Al Virodb3a91f2007-10-29 05:08:38 +0000258 }
Erich Chen1c57e862006-07-12 08:59:32 -0700259
Nick Cheng1a4f5502007-09-13 17:26:40 +0800260 acb->dma_coherent = dma_coherent;
261 acb->dma_coherent_handle = dma_coherent_handle;
Erich Chen1c57e862006-07-12 08:59:32 -0700262
Nick Cheng1a4f5502007-09-13 17:26:40 +0800263 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 }
269
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 }
280
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;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800285 }
286 break;
287
288 case ACB_ADAPTER_TYPE_B: {
289
290 struct pci_dev *pdev = acb->pdev;
291 struct MessageUnit_B *reg;
Al Viro80da1ad2007-10-29 05:08:28 +0000292 void __iomem *mem_base0, *mem_base1;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800293 void *dma_coherent;
294 dma_addr_t dma_coherent_handle, dma_addr;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800295 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
Nick Cheng1a4f5502007-09-13 17:26:40 +0800316 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));
Al Viro80da1ad2007-10-29 05:08:28 +0000329 acb->pmuB = reg;
Nick Cheng36b83de2010-05-17 11:22:42 +0800330 mem_base0 = ioremap(pci_resource_start(pdev, 0),
331 pci_resource_len(pdev, 0));
Al Virodb3a91f2007-10-29 05:08:38 +0000332 if (!mem_base0)
333 goto out;
334
Nick Cheng36b83de2010-05-17 11:22:42 +0800335 mem_base1 = ioremap(pci_resource_start(pdev, 2),
336 pci_resource_len(pdev, 2));
Al Virodb3a91f2007-10-29 05:08:38 +0000337 if (!mem_base1) {
338 iounmap(mem_base0);
339 goto out;
340 }
341
Al Viro80da1ad2007-10-29 05:08:28 +0000342 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;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800351
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;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800356 }
357 break;
Erich Chen1c57e862006-07-12 08:59:32 -0700358 }
Erich Chen1c57e862006-07-12 08:59:32 -0700359 return 0;
Al Virodb3a91f2007-10-29 05:08:38 +0000360
361out:
362 dma_free_coherent(&acb->pdev->dev,
Nick Cheng76d78302008-02-04 23:53:24 -0800363 (ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock) + 0x20 +
364 sizeof(struct MessageUnit_B)), acb->dma_coherent, acb->dma_coherent_handle);
Al Virodb3a91f2007-10-29 05:08:38 +0000365 return -ENOMEM;
Erich Chen1c57e862006-07-12 08:59:32 -0700366}
Nick Cheng36b83de2010-05-17 11:22:42 +0800367static 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}
Erich Chen1c57e862006-07-12 08:59:32 -0700449
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
Yang Hongyang6a355282009-04-06 19:01:13 -0700472 error = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Erich Chen1c57e862006-07-12 08:59:32 -0700473 if (error) {
Yang Hongyang284901a2009-04-06 19:01:15 -0700474 error = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Erich Chen1c57e862006-07-12 08:59:32 -0700475 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");
Nick Cheng1a4f5502007-09-13 17:26:40 +0800497 if (error) {
Erich Chen1c57e862006-07-12 08:59:32 -0700498 goto out_host_put;
Erich Chen1c57e862006-07-12 08:59:32 -0700499 }
Nick Cheng1a4f5502007-09-13 17:26:40 +0800500 arcmsr_define_adapter_type(acb);
501
Erich Chen1c57e862006-07-12 08:59:32 -0700502 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);
Nick Cheng36b83de2010-05-17 11:22:42 +0800507 INIT_WORK(&acb->arcmsr_do_message_isr_bh, arcmsr_message_isr_bh_fn);
Erich Chen1c57e862006-07-12 08:59:32 -0700508 error = arcmsr_alloc_ccb_pool(acb);
509 if (error)
Nick Cheng1a4f5502007-09-13 17:26:40 +0800510 goto out_release_regions;
Erich Chen1c57e862006-07-12 08:59:32 -0700511
Nick Cheng36b83de2010-05-17 11:22:42 +0800512 arcmsr_iop_init(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700513 error = request_irq(pdev->irq, arcmsr_do_interrupt,
Jeff Garzik488a5c82007-07-27 13:01:15 -0400514 IRQF_SHARED, "arcmsr", acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700515 if (error)
516 goto out_free_ccb_pool;
517
Erich Chen1c57e862006-07-12 08:59:32 -0700518 pci_set_drvdata(pdev, host);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800519 if (strncmp(acb->firm_version, "V1.42", 5) >= 0)
520 host->max_sectors= ARCMSR_MAX_XFER_SECTORS_B;
Erich Chen1c57e862006-07-12 08:59:32 -0700521
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);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800531 #ifdef CONFIG_SCSI_ARCMSR_AER
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800532 pci_enable_pcie_error_reporting(pdev);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800533 #endif
Nick Cheng36b83de2010-05-17 11:22:42 +0800534 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
Erich Chen1c57e862006-07-12 08:59:32 -0700542 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);
Erich Chen1c57e862006-07-12 08:59:32 -0700548 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
Nick Cheng1a4f5502007-09-13 17:26:40 +0800558static uint8_t arcmsr_hba_wait_msgint_ready(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -0700559{
Al Viro80da1ad2007-10-29 05:08:28 +0000560 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800561 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{
Al Viro80da1ad2007-10-29 05:08:28 +0000581 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800582 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);
Nick Cheng76d78302008-02-04 23:53:24 -0800591 writel(ARCMSR_DRV2IOP_END_OF_INTERRUPT, reg->drv2iop_doorbell_reg);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800592 return 0x00;
593 }
594 msleep(10);
595 }/*max 1 seconds*/
596
597 } while (Retries++ < 20);/*max 20 sec*/
598 return 0xff;
599}
600
Nick Cheng36b83de2010-05-17 11:22:42 +0800601static uint8_t arcmsr_abort_hba_allcmd(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +0800602{
Al Viro80da1ad2007-10-29 05:08:28 +0000603 struct MessageUnit_A __iomem *reg = acb->pmuA;
Erich Chen1c57e862006-07-12 08:59:32 -0700604
605 writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, &reg->inbound_msgaddr0);
Nick Cheng36b83de2010-05-17 11:22:42 +0800606 if (arcmsr_hba_wait_msgint_ready(acb)) {
Erich Chen1c57e862006-07-12 08:59:32 -0700607 printk(KERN_NOTICE
608 "arcmsr%d: wait 'abort all outstanding command' timeout \n"
609 , acb->host->host_no);
Nick Cheng36b83de2010-05-17 11:22:42 +0800610 return 0xff;
611 }
612 return 0x00;
Erich Chen1c57e862006-07-12 08:59:32 -0700613}
614
Nick Cheng36b83de2010-05-17 11:22:42 +0800615static uint8_t arcmsr_abort_hbb_allcmd(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +0800616{
Al Viro80da1ad2007-10-29 05:08:28 +0000617 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800618
619 writel(ARCMSR_MESSAGE_ABORT_CMD, reg->drv2iop_doorbell_reg);
Nick Cheng36b83de2010-05-17 11:22:42 +0800620 if (arcmsr_hbb_wait_msgint_ready(acb)) {
Nick Cheng1a4f5502007-09-13 17:26:40 +0800621 printk(KERN_NOTICE
622 "arcmsr%d: wait 'abort all outstanding command' timeout \n"
623 , acb->host->host_no);
Nick Cheng36b83de2010-05-17 11:22:42 +0800624 return 0xff;
625 }
626 return 0x00;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800627}
628
Nick Cheng36b83de2010-05-17 11:22:42 +0800629static uint8_t arcmsr_abort_allcmd(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +0800630{
Nick Cheng36b83de2010-05-17 11:22:42 +0800631 uint8_t rtnval = 0;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800632 switch (acb->adapter_type) {
633 case ACB_ADAPTER_TYPE_A: {
Nick Cheng36b83de2010-05-17 11:22:42 +0800634 rtnval = arcmsr_abort_hba_allcmd(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800635 }
636 break;
637
638 case ACB_ADAPTER_TYPE_B: {
Nick Cheng36b83de2010-05-17 11:22:42 +0800639 rtnval = arcmsr_abort_hbb_allcmd(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800640 }
641 }
Nick Cheng36b83de2010-05-17 11:22:42 +0800642 return rtnval;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800643}
644
Erich Chen1c57e862006-07-12 08:59:32 -0700645static void arcmsr_pci_unmap_dma(struct CommandControlBlock *ccb)
646{
Erich Chen1c57e862006-07-12 08:59:32 -0700647 struct scsi_cmnd *pcmd = ccb->pcmd;
648
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900649 scsi_dma_unmap(pcmd);
Erich Chen1c57e862006-07-12 08:59:32 -0700650}
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
Nick Cheng1a4f5502007-09-13 17:26:40 +0800666static void arcmsr_flush_hba_cache(struct AdapterControlBlock *acb)
667{
Al Viro80da1ad2007-10-29 05:08:28 +0000668 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800669 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{
Al Viro80da1ad2007-10-29 05:08:28 +0000685 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800686 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 =
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900724 sizeof(struct SENSE_DATA) < SCSI_SENSE_BUFFERSIZE
725 ? sizeof(struct SENSE_DATA) : SCSI_SENSE_BUFFERSIZE;
726 memset(sensebuffer, 0, SCSI_SENSE_BUFFERSIZE);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800727 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 : {
Al Viro80da1ad2007-10-29 05:08:28 +0000739 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng36b83de2010-05-17 11:22:42 +0800740 orig_mask = readl(&reg->outbound_intmask);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800741 writel(orig_mask|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE, \
742 &reg->outbound_intmask);
743 }
744 break;
745
746 case ACB_ADAPTER_TYPE_B : {
Al Viro80da1ad2007-10-29 05:08:28 +0000747 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng36b83de2010-05-17 11:22:42 +0800748 orig_mask = readl(reg->iop2drv_doorbell_mask_reg);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800749 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 }
Nick Cheng76d78302008-02-04 23:53:24 -0800837 else
Nick Cheng1a4f5502007-09-13 17:26:40 +0800838 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: {
Al Viro80da1ad2007-10-29 05:08:28 +0000849 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800850 uint32_t outbound_intstatus;
Al Viro80da1ad2007-10-29 05:08:28 +0000851 outbound_intstatus = readl(&reg->outbound_intstatus) &
Nick Cheng1a4f5502007-09-13 17:26:40 +0800852 acb->outbound_int_enable;
853 /*clear and abort all outbound posted Q*/
854 writel(outbound_intstatus, &reg->outbound_intstatus);/*clear interrupt*/
Al Viro80da1ad2007-10-29 05:08:28 +0000855 while (((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF)
Nick Cheng1a4f5502007-09-13 17:26:40 +0800856 && (i++ < ARCMSR_MAX_OUTSTANDING_CMD)) {
857 arcmsr_drain_donequeue(acb, flag_ccb);
858 }
859 }
860 break;
861
862 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +0000863 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800864 /*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}
Erich Chen1c57e862006-07-12 08:59:32 -0700878static 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;
Erich Chen1c57e862006-07-12 08:59:32 -0700883 int poll_count = 0;
Erich Chen1c57e862006-07-12 08:59:32 -0700884 arcmsr_free_sysfs_attr(acb);
885 scsi_remove_host(host);
Nick Cheng36b83de2010-05-17 11:22:42 +0800886 flush_scheduled_work();
887 del_timer_sync(&acb->eternal_timer);
888 arcmsr_disable_outbound_ints(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700889 arcmsr_stop_adapter_bgrb(acb);
890 arcmsr_flush_adapter_cache(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700891 acb->acb_flags |= ACB_F_SCSISTOPADAPTER;
892 acb->acb_flags &= ~ACB_F_IOP_INITED;
893
Nick Cheng1a4f5502007-09-13 17:26:40 +0800894 for (poll_count = 0; poll_count < ARCMSR_MAX_OUTSTANDING_CMD; poll_count++) {
Erich Chen1c57e862006-07-12 08:59:32 -0700895 if (!atomic_read(&acb->ccboutstandingcount))
896 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800897 arcmsr_interrupt(acb);/* FIXME: need spinlock */
Erich Chen1c57e862006-07-12 08:59:32 -0700898 msleep(25);
899 }
900
901 if (atomic_read(&acb->ccboutstandingcount)) {
902 int i;
903
904 arcmsr_abort_allcmd(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800905 arcmsr_done4abort_postqueue(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700906 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);
Erich Chen1c57e862006-07-12 08:59:32 -0700917 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;
Nick Cheng36b83de2010-05-17 11:22:42 +0800931 del_timer_sync(&acb->eternal_timer);
932 arcmsr_disable_outbound_ints(acb);
933 flush_scheduled_work();
Erich Chen1c57e862006-07-12 08:59:32 -0700934 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
Nick Cheng36b83de2010-05-17 11:22:42 +0800953static void arcmsr_enable_outbound_ints(struct AdapterControlBlock *acb,
Nick Cheng1a4f5502007-09-13 17:26:40 +0800954 u32 intmask_org)
Erich Chen1c57e862006-07-12 08:59:32 -0700955{
Erich Chen1c57e862006-07-12 08:59:32 -0700956 u32 mask;
957
Nick Cheng1a4f5502007-09-13 17:26:40 +0800958 switch (acb->adapter_type) {
959
960 case ACB_ADAPTER_TYPE_A : {
Al Viro80da1ad2007-10-29 05:08:28 +0000961 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800962 mask = intmask_org & ~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE |
Nick Cheng36b83de2010-05-17 11:22:42 +0800963 ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE|
964 ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800965 writel(mask, &reg->outbound_intmask);
966 acb->outbound_int_enable = ~(intmask_org & mask) & 0x000000ff;
967 }
968 break;
Erich Chen1c57e862006-07-12 08:59:32 -0700969
Nick Cheng1a4f5502007-09-13 17:26:40 +0800970 case ACB_ADAPTER_TYPE_B : {
Al Viro80da1ad2007-10-29 05:08:28 +0000971 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng36b83de2010-05-17 11:22:42 +0800972 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);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800976 writel(mask, reg->iop2drv_doorbell_mask_reg);
977 acb->outbound_int_enable = (intmask_org | mask) & 0x0000000f;
978 }
Erich Chen1c57e862006-07-12 08:59:32 -0700979 }
980}
981
Nick Cheng76d78302008-02-04 23:53:24 -0800982static int arcmsr_build_ccb(struct AdapterControlBlock *acb,
Erich Chen1c57e862006-07-12 08:59:32 -0700983 struct CommandControlBlock *ccb, struct scsi_cmnd *pcmd)
984{
985 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
986 int8_t *psge = (int8_t *)&arcmsr_cdb->u;
Al Viro80da1ad2007-10-29 05:08:28 +0000987 __le32 address_lo, address_hi;
Erich Chen1c57e862006-07-12 08:59:32 -0700988 int arccdbsize = 0x30;
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900989 int nseg;
Erich Chen1c57e862006-07-12 08:59:32 -0700990
991 ccb->pcmd = pcmd;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800992 memset(arcmsr_cdb, 0, sizeof(struct ARCMSR_CDB));
Erich Chen1c57e862006-07-12 08:59:32 -0700993 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);
Erich Chen1c57e862006-07-12 08:59:32 -07001000
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001001 nseg = scsi_dma_map(pcmd);
Nick Cheng76d78302008-02-04 23:53:24 -08001002 if (nseg > ARCMSR_MAX_SG_ENTRIES)
1003 return FAILED;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001004 BUG_ON(nseg < 0);
1005
1006 if (nseg) {
Al Viro80da1ad2007-10-29 05:08:28 +00001007 __le32 length;
1008 int i, cdb_sgcount = 0;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001009 struct scatterlist *sg;
1010
Erich Chen1c57e862006-07-12 08:59:32 -07001011 /* map stor port SG list to our iop SG List. */
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001012 scsi_for_each_sg(pcmd, sg, nseg, i) {
Erich Chen1c57e862006-07-12 08:59:32 -07001013 /* Get the physical address of the current data pointer */
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001014 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)));
Erich Chen1c57e862006-07-12 08:59:32 -07001017 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;
Al Viro6a7d26d2007-10-29 05:08:48 +00001029 pdma_sg->length = length|cpu_to_le32(IS_SG64_ADDR);
Erich Chen1c57e862006-07-12 08:59:32 -07001030 psge += sizeof (struct SG64ENTRY);
1031 arccdbsize += sizeof (struct SG64ENTRY);
1032 }
Erich Chen1c57e862006-07-12 08:59:32 -07001033 cdb_sgcount++;
1034 }
1035 arcmsr_cdb->sgcount = (uint8_t)cdb_sgcount;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001036 arcmsr_cdb->DataLength = scsi_bufflen(pcmd);
Erich Chen1c57e862006-07-12 08:59:32 -07001037 if ( arccdbsize > 256)
1038 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_SGL_BSIZE;
Erich Chen1c57e862006-07-12 08:59:32 -07001039 }
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 }
Nick Cheng76d78302008-02-04 23:53:24 -08001044 return SUCCESS;
Erich Chen1c57e862006-07-12 08:59:32 -07001045}
1046
1047static void arcmsr_post_ccb(struct AdapterControlBlock *acb, struct CommandControlBlock *ccb)
1048{
Erich Chen1c57e862006-07-12 08:59:32 -07001049 uint32_t cdb_shifted_phyaddr = ccb->cdb_shifted_phyaddr;
1050 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
Erich Chen1c57e862006-07-12 08:59:32 -07001051 atomic_inc(&acb->ccboutstandingcount);
1052 ccb->startdone = ARCMSR_CCB_START;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001053
1054 switch (acb->adapter_type) {
1055 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001056 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001057
1058 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE)
1059 writel(cdb_shifted_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,
Erich Chen1c57e862006-07-12 08:59:32 -07001060 &reg->inbound_queueport);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001061 else {
1062 writel(cdb_shifted_phyaddr, &reg->inbound_queueport);
1063 }
1064 }
1065 break;
1066
1067 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001068 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001069 uint32_t ending_index, index = reg->postq_index;
1070
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);
1084 }
1085 break;
1086 }
Erich Chen1c57e862006-07-12 08:59:32 -07001087}
1088
Nick Cheng1a4f5502007-09-13 17:26:40 +08001089static void arcmsr_stop_hba_bgrb(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07001090{
Al Viro80da1ad2007-10-29 05:08:28 +00001091 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001092 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
1093 writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, &reg->inbound_msgaddr0);
Erich Chen1c57e862006-07-12 08:59:32 -07001094
Nick Cheng1a4f5502007-09-13 17:26:40 +08001095 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{
Al Viro80da1ad2007-10-29 05:08:28 +00001104 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001105 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)) {
1109 printk(KERN_NOTICE
1110 "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
1111 , acb->host->host_no);
Erich Chen1c57e862006-07-12 08:59:32 -07001112 }
1113}
1114
1115static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb)
1116{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001117 switch (acb->adapter_type) {
1118 case ACB_ADAPTER_TYPE_A: {
1119 arcmsr_stop_hba_bgrb(acb);
1120 }
1121 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001122
Nick Cheng1a4f5502007-09-13 17:26:40 +08001123 case ACB_ADAPTER_TYPE_B: {
1124 arcmsr_stop_hbb_bgrb(acb);
1125 }
1126 break;
1127 }
Erich Chen1c57e862006-07-12 08:59:32 -07001128}
1129
1130static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb)
1131{
Al Virodb3a91f2007-10-29 05:08:38 +00001132 switch (acb->adapter_type) {
1133 case ACB_ADAPTER_TYPE_A: {
1134 iounmap(acb->pmuA);
Nick Cheng76d78302008-02-04 23:53:24 -08001135 dma_free_coherent(&acb->pdev->dev,
1136 ARCMSR_MAX_FREECCB_NUM * sizeof (struct CommandControlBlock) + 0x20,
1137 acb->dma_coherent,
1138 acb->dma_coherent_handle);
Al Virodb3a91f2007-10-29 05:08:38 +00001139 break;
1140 }
1141 case ACB_ADAPTER_TYPE_B: {
1142 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng36b83de2010-05-17 11:22:42 +08001143 iounmap((u8 *)reg->drv2iop_doorbell_reg - ARCMSR_DRV2IOP_DOORBELL);
1144 iounmap((u8 *)reg->ioctl_wbuffer_reg - ARCMSR_IOCTL_WBUFFER);
Nick Cheng76d78302008-02-04 23:53:24 -08001145 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);
Al Virodb3a91f2007-10-29 05:08:38 +00001148 }
1149 }
Nick Cheng76d78302008-02-04 23:53:24 -08001150
Erich Chen1c57e862006-07-12 08:59:32 -07001151}
1152
Nick Cheng1a4f5502007-09-13 17:26:40 +08001153void arcmsr_iop_message_read(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07001154{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001155 switch (acb->adapter_type) {
1156 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001157 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001158 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, &reg->inbound_doorbell);
1159 }
1160 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001161
Nick Cheng1a4f5502007-09-13 17:26:40 +08001162 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001163 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001164 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell_reg);
1165 }
1166 break;
1167 }
1168}
1169
1170static void arcmsr_iop_message_wrote(struct AdapterControlBlock *acb)
1171{
1172 switch (acb->adapter_type) {
1173 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001174 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001175 /*
1176 ** push inbound doorbell tell iop, driver data write ok
1177 ** and wait reply on next hwinterrupt for next Qbuffer post
1178 */
1179 writel(ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK, &reg->inbound_doorbell);
1180 }
1181 break;
1182
1183 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001184 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001185 /*
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
Al Viro80da1ad2007-10-29 05:08:28 +00001195struct QBUFFER __iomem *arcmsr_get_iop_rqbuffer(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08001196{
Al Viro0c7eb2e2007-10-29 05:08:58 +00001197 struct QBUFFER __iomem *qbuffer = NULL;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001198
1199 switch (acb->adapter_type) {
1200
1201 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001202 struct MessageUnit_A __iomem *reg = acb->pmuA;
1203 qbuffer = (struct QBUFFER __iomem *)&reg->message_rbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001204 }
1205 break;
1206
1207 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001208 struct MessageUnit_B *reg = acb->pmuB;
1209 qbuffer = (struct QBUFFER __iomem *)reg->ioctl_rbuffer_reg;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001210 }
1211 break;
1212 }
1213 return qbuffer;
1214}
1215
Al Viro80da1ad2007-10-29 05:08:28 +00001216static struct QBUFFER __iomem *arcmsr_get_iop_wqbuffer(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08001217{
Al Viro0c7eb2e2007-10-29 05:08:58 +00001218 struct QBUFFER __iomem *pqbuffer = NULL;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001219
1220 switch (acb->adapter_type) {
1221
1222 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001223 struct MessageUnit_A __iomem *reg = acb->pmuA;
1224 pqbuffer = (struct QBUFFER __iomem *) &reg->message_wbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001225 }
1226 break;
1227
1228 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001229 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001230 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{
Al Viro80da1ad2007-10-29 05:08:28 +00001239 struct QBUFFER __iomem *prbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001240 struct QBUFFER *pQbuffer;
Al Viro80da1ad2007-10-29 05:08:28 +00001241 uint8_t __iomem *iop_data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001242 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);
Al Viro80da1ad2007-10-29 05:08:28 +00001247 iop_data = (uint8_t __iomem *)prbuffer->data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001248 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;
Al Viro80da1ad2007-10-29 05:08:28 +00001275 struct QBUFFER __iomem *pwbuffer;
1276 uint8_t __iomem *iop_data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001277 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;
Al Viro80da1ad2007-10-29 05:08:28 +00001305 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001306
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;
Al Viro80da1ad2007-10-29 05:08:28 +00001321 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001322
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;
Al Viro80da1ad2007-10-29 05:08:28 +00001332 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001333
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}
Nick Cheng36b83de2010-05-17 11:22:42 +08001344/*
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;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001355
Nick Cheng36b83de2010-05-17 11:22:42 +08001356 /*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;
1363
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}
Nick Cheng1a4f5502007-09-13 17:26:40 +08001368static int arcmsr_handle_hba_isr(struct AdapterControlBlock *acb)
1369{
1370 uint32_t outbound_intstatus;
Al Viro80da1ad2007-10-29 05:08:28 +00001371 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001372
Nick Cheng36b83de2010-05-17 11:22:42 +08001373 outbound_intstatus = readl(&reg->outbound_intstatus) &
Nick Cheng1a4f5502007-09-13 17:26:40 +08001374 acb->outbound_int_enable;
1375 if (!(outbound_intstatus & ARCMSR_MU_OUTBOUND_HANDLE_INT)) {
1376 return 1;
1377 }
Erich Chen1c57e862006-07-12 08:59:32 -07001378 writel(outbound_intstatus, &reg->outbound_intstatus);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001379 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_DOORBELL_INT) {
1380 arcmsr_hba_doorbell_isr(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001381 }
1382 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_POSTQUEUE_INT) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001383 arcmsr_hba_postqueue_isr(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001384 }
Nick Cheng36b83de2010-05-17 11:22:42 +08001385 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_MESSAGE0_INT) {
1386 /* messenger of "driver to iop commands" */
1387 arcmsr_hba_message_isr(acb);
1388 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001389 return 0;
1390}
1391
1392static int arcmsr_handle_hbb_isr(struct AdapterControlBlock *acb)
1393{
1394 uint32_t outbound_doorbell;
Al Viro80da1ad2007-10-29 05:08:28 +00001395 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001396
Nick Cheng36b83de2010-05-17 11:22:42 +08001397 outbound_doorbell = readl(reg->iop2drv_doorbell_reg) &
Nick Cheng1a4f5502007-09-13 17:26:40 +08001398 acb->outbound_int_enable;
1399 if (!outbound_doorbell)
1400 return 1;
1401
1402 writel(~outbound_doorbell, reg->iop2drv_doorbell_reg);
Nick Cheng36b83de2010-05-17 11:22:42 +08001403 /*in case the last action of doorbell interrupt clearance is cached,
1404 this action can push HW to write down the clear bit*/
Nick Cheng76d78302008-02-04 23:53:24 -08001405 readl(reg->iop2drv_doorbell_reg);
1406 writel(ARCMSR_DRV2IOP_END_OF_INTERRUPT, reg->drv2iop_doorbell_reg);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001407 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 }
Nick Cheng36b83de2010-05-17 11:22:42 +08001416 if (outbound_doorbell & ARCMSR_IOP2DRV_MESSAGE_CMD_DONE) {
1417 /* messenger of "driver to iop commands" */
1418 arcmsr_hbb_message_isr(acb);
1419 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001420
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;
1440 }
Erich Chen1c57e862006-07-12 08:59:32 -07001441 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) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001449 uint32_t intmask_org;
Erich Chen1c57e862006-07-12 08:59:32 -07001450 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001451 intmask_org = arcmsr_disable_outbound_ints(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001452 arcmsr_stop_adapter_bgrb(acb);
1453 arcmsr_flush_adapter_cache(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001454 arcmsr_enable_outbound_ints(acb, intmask_org);
Erich Chen1c57e862006-07-12 08:59:32 -07001455 }
1456 }
1457}
1458
Nick Cheng1a4f5502007-09-13 17:26:40 +08001459void arcmsr_post_ioctldata2iop(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07001460{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001461 int32_t wqbuf_firstindex, wqbuf_lastindex;
1462 uint8_t *pQbuffer;
Al Viro80da1ad2007-10-29 05:08:28 +00001463 struct QBUFFER __iomem *pwbuffer;
1464 uint8_t __iomem *iop_data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001465 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++;
1480 }
1481 acb->wqbuf_firstindex = wqbuf_firstindex;
1482 pwbuffer->data_len = allxfer_len;
1483 arcmsr_iop_message_wrote(acb);
1484 }
1485}
1486
Nick Cheng36b83de2010-05-17 11:22:42 +08001487static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb,
Nick Cheng1a4f5502007-09-13 17:26:40 +08001488 struct scsi_cmnd *cmd)
1489{
Erich Chen1c57e862006-07-12 08:59:32 -07001490 struct CMD_MESSAGE_FIELD *pcmdmessagefld;
1491 int retvalue = 0, transfer_len = 0;
1492 char *buffer;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001493 struct scatterlist *sg;
Erich Chen1c57e862006-07-12 08:59:32 -07001494 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];
Nick Cheng1a4f5502007-09-13 17:26:40 +08001498 /* 4 bytes: Areca io control code */
Erich Chen1c57e862006-07-12 08:59:32 -07001499
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001500 sg = scsi_sglist(cmd);
Jens Axboe45711f12007-10-22 21:19:53 +02001501 buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001502 if (scsi_sg_count(cmd) > 1) {
1503 retvalue = ARCMSR_MESSAGE_FAIL;
1504 goto message_out;
Erich Chen1c57e862006-07-12 08:59:32 -07001505 }
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001506 transfer_len += sg->length;
1507
Erich Chen1c57e862006-07-12 08:59:32 -07001508 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) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001514
Erich Chen1c57e862006-07-12 08:59:32 -07001515 case ARCMSR_MESSAGE_READ_RQBUFFER: {
Daniel Drake69e562c2008-02-20 13:29:05 +00001516 unsigned char *ver_addr;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001517 uint8_t *pQbuffer, *ptmpQbuffer;
1518 int32_t allxfer_len = 0;
Erich Chen1c57e862006-07-12 08:59:32 -07001519
Daniel Drake69e562c2008-02-20 13:29:05 +00001520 ver_addr = kmalloc(1032, GFP_ATOMIC);
1521 if (!ver_addr) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001522 retvalue = ARCMSR_MESSAGE_FAIL;
1523 goto message_out;
1524 }
Nick Cheng36b83de2010-05-17 11:22:42 +08001525
1526 if (!acb->fw_state) {
1527 pcmdmessagefld->cmdmessage.ReturnCode =
1528 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1529 goto message_out;
1530 }
1531
Daniel Drake69e562c2008-02-20 13:29:05 +00001532 ptmpQbuffer = ver_addr;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001533 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) {
Erich Chen1c57e862006-07-12 08:59:32 -07001543
Al Viro80da1ad2007-10-29 05:08:28 +00001544 struct QBUFFER __iomem *prbuffer;
1545 uint8_t __iomem *iop_data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001546 int32_t iop_len;
1547
1548 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1549 prbuffer = arcmsr_get_iop_rqbuffer(acb);
Al Viro80da1ad2007-10-29 05:08:28 +00001550 iop_data = prbuffer->data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001551 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--;
Erich Chen1c57e862006-07-12 08:59:32 -07001558 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001559 arcmsr_iop_message_read(acb);
1560 }
Daniel Drake69e562c2008-02-20 13:29:05 +00001561 memcpy(pcmdmessagefld->messagedatabuffer, ver_addr, allxfer_len);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001562 pcmdmessagefld->cmdmessage.Length = allxfer_len;
1563 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
Daniel Drake69e562c2008-02-20 13:29:05 +00001564 kfree(ver_addr);
Erich Chen1c57e862006-07-12 08:59:32 -07001565 }
1566 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001567
Nick Cheng1a4f5502007-09-13 17:26:40 +08001568 case ARCMSR_MESSAGE_WRITE_WQBUFFER: {
Daniel Drake69e562c2008-02-20 13:29:05 +00001569 unsigned char *ver_addr;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001570 int32_t my_empty_len, user_len, wqbuf_firstindex, wqbuf_lastindex;
1571 uint8_t *pQbuffer, *ptmpuserbuffer;
1572
Daniel Drake69e562c2008-02-20 13:29:05 +00001573 ver_addr = kmalloc(1032, GFP_ATOMIC);
1574 if (!ver_addr) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001575 retvalue = ARCMSR_MESSAGE_FAIL;
1576 goto message_out;
1577 }
Nick Cheng36b83de2010-05-17 11:22:42 +08001578 if (!acb->fw_state) {
1579 pcmdmessagefld->cmdmessage.ReturnCode =
1580 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1581 goto message_out;
1582 }
1583
Daniel Drake69e562c2008-02-20 13:29:05 +00001584 ptmpuserbuffer = ver_addr;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001585 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 */
Erich Chen1c57e862006-07-12 08:59:32 -07001620 struct SENSE_DATA *sensebuffer =
1621 (struct SENSE_DATA *)cmd->sense_buffer;
Erich Chen1c57e862006-07-12 08:59:32 -07001622 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;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001628 }
Erich Chen1c57e862006-07-12 08:59:32 -07001629 }
Daniel Drake69e562c2008-02-20 13:29:05 +00001630 kfree(ver_addr);
Erich Chen1c57e862006-07-12 08:59:32 -07001631 }
1632 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001633
Erich Chen1c57e862006-07-12 08:59:32 -07001634 case ARCMSR_MESSAGE_CLEAR_RQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001635 uint8_t *pQbuffer = acb->rqbuffer;
Nick Cheng36b83de2010-05-17 11:22:42 +08001636 if (!acb->fw_state) {
1637 pcmdmessagefld->cmdmessage.ReturnCode =
1638 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1639 goto message_out;
1640 }
Erich Chen1c57e862006-07-12 08:59:32 -07001641
Nick Cheng1a4f5502007-09-13 17:26:40 +08001642 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;
Erich Chen1c57e862006-07-12 08:59:32 -07001651 }
1652 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001653
Erich Chen1c57e862006-07-12 08:59:32 -07001654 case ARCMSR_MESSAGE_CLEAR_WQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001655 uint8_t *pQbuffer = acb->wqbuffer;
Nick Cheng36b83de2010-05-17 11:22:42 +08001656 if (!acb->fw_state) {
1657 pcmdmessagefld->cmdmessage.ReturnCode =
1658 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1659 goto message_out;
1660 }
Erich Chen1c57e862006-07-12 08:59:32 -07001661
Nick Cheng1a4f5502007-09-13 17:26:40 +08001662 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;
Erich Chen1c57e862006-07-12 08:59:32 -07001674 }
1675 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001676
Erich Chen1c57e862006-07-12 08:59:32 -07001677 case ARCMSR_MESSAGE_CLEAR_ALLQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001678 uint8_t *pQbuffer;
Nick Cheng36b83de2010-05-17 11:22:42 +08001679 if (!acb->fw_state) {
1680 pcmdmessagefld->cmdmessage.ReturnCode =
1681 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1682 goto message_out;
1683 }
Erich Chen1c57e862006-07-12 08:59:32 -07001684
Nick Cheng1a4f5502007-09-13 17:26:40 +08001685 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;
Erich Chen1c57e862006-07-12 08:59:32 -07001702 }
1703 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001704
Erich Chen1c57e862006-07-12 08:59:32 -07001705 case ARCMSR_MESSAGE_RETURN_CODE_3F: {
Nick Cheng36b83de2010-05-17 11:22:42 +08001706 if (!acb->fw_state) {
1707 pcmdmessagefld->cmdmessage.ReturnCode =
1708 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1709 goto message_out;
1710 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001711 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_3F;
Erich Chen1c57e862006-07-12 08:59:32 -07001712 }
1713 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001714
Nick Cheng1a4f5502007-09-13 17:26:40 +08001715 case ARCMSR_MESSAGE_SAY_HELLO: {
1716 int8_t *hello_string = "Hello! I am ARCMSR";
Nick Cheng36b83de2010-05-17 11:22:42 +08001717 if (!acb->fw_state) {
1718 pcmdmessagefld->cmdmessage.ReturnCode =
1719 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1720 goto message_out;
1721 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001722 memcpy(pcmdmessagefld->messagedatabuffer, hello_string
1723 , (int16_t)strlen(hello_string));
1724 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07001725 }
1726 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001727
Erich Chen1c57e862006-07-12 08:59:32 -07001728 case ARCMSR_MESSAGE_SAY_GOODBYE:
Nick Cheng36b83de2010-05-17 11:22:42 +08001729 if (!acb->fw_state) {
1730 pcmdmessagefld->cmdmessage.ReturnCode =
1731 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1732 goto message_out;
1733 }
Erich Chen1c57e862006-07-12 08:59:32 -07001734 arcmsr_iop_parking(acb);
1735 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001736
Erich Chen1c57e862006-07-12 08:59:32 -07001737 case ARCMSR_MESSAGE_FLUSH_ADAPTER_CACHE:
Nick Cheng36b83de2010-05-17 11:22:42 +08001738 if (!acb->fw_state) {
1739 pcmdmessagefld->cmdmessage.ReturnCode =
1740 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
1741 goto message_out;
1742 }
Erich Chen1c57e862006-07-12 08:59:32 -07001743 arcmsr_flush_adapter_cache(acb);
1744 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001745
Erich Chen1c57e862006-07-12 08:59:32 -07001746 default:
1747 retvalue = ARCMSR_MESSAGE_FAIL;
1748 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001749 message_out:
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001750 sg = scsi_sglist(cmd);
1751 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
Erich Chen1c57e862006-07-12 08:59:32 -07001752 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;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001774 struct scatterlist *sg;
Erich Chen1c57e862006-07-12 08:59:32 -07001775
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;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001786 /* ISO, ECMA, & ANSI versions */
Erich Chen1c57e862006-07-12 08:59:32 -07001787 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 */
Erich Chen1c57e862006-07-12 08:59:32 -07001794
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001795 sg = scsi_sglist(cmd);
Jens Axboe45711f12007-10-22 21:19:53 +02001796 buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001797
Erich Chen1c57e862006-07-12 08:59:32 -07001798 memcpy(buffer, inqdata, sizeof(inqdata));
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001799 sg = scsi_sglist(cmd);
1800 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
Erich Chen1c57e862006-07-12 08:59:32 -07001801
Erich Chen1c57e862006-07-12 08:59:32 -07001802 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;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001821 struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -07001822 struct CommandControlBlock *ccb;
1823 int target = cmd->device->id;
1824 int lun = cmd->device->lun;
Nick Cheng36b83de2010-05-17 11:22:42 +08001825 uint8_t scsicmd = cmd->cmnd[0];
Erich Chen1c57e862006-07-12 08:59:32 -07001826 cmd->scsi_done = done;
1827 cmd->host_scribble = NULL;
1828 cmd->result = 0;
Nick Cheng36b83de2010-05-17 11:22:42 +08001829
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
Erich Chen1c57e862006-07-12 08:59:32 -07001838 if (acb->acb_flags & ACB_F_BUS_RESET) {
Nick Cheng36b83de2010-05-17 11:22:42 +08001839 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);
Erich Chen1c57e862006-07-12 08:59:32 -07001848 return SCSI_MLQUEUE_HOST_BUSY;
1849 }
Nick Cheng36b83de2010-05-17 11:22:42 +08001850
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
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001876 if (target == 16) {
Erich Chen1c57e862006-07-12 08:59:32 -07001877 /* 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"
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001889 " Cmd = %2x, TargetId = %d, Lun = %d \n"
Erich Chen1c57e862006-07-12 08:59:32 -07001890 , 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;
Nick Cheng76d78302008-02-04 23:53:24 -08001905 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 }
Erich Chen1c57e862006-07-12 08:59:32 -07001910 arcmsr_post_ccb(acb, ccb);
1911 return 0;
1912}
1913
Nick Cheng36b83de2010-05-17 11:22:42 +08001914static void *arcmsr_get_hba_config(struct AdapterControlBlock *acb, int mode)
Erich Chen1c57e862006-07-12 08:59:32 -07001915{
Al Viro80da1ad2007-10-29 05:08:28 +00001916 struct MessageUnit_A __iomem *reg = acb->pmuA;
Erich Chen1c57e862006-07-12 08:59:32 -07001917 char *acb_firm_model = acb->firm_model;
1918 char *acb_firm_version = acb->firm_version;
Nick Cheng36b83de2010-05-17 11:22:42 +08001919 char *acb_device_map = acb->device_map;
Al Viro80da1ad2007-10-29 05:08:28 +00001920 char __iomem *iop_firm_model = (char __iomem *)(&reg->message_rwbuffer[15]);
1921 char __iomem *iop_firm_version = (char __iomem *)(&reg->message_rwbuffer[17]);
Nick Cheng36b83de2010-05-17 11:22:42 +08001922 char __iomem *iop_device_map = (char __iomem *) (&reg->message_rwbuffer[21]);
Erich Chen1c57e862006-07-12 08:59:32 -07001923 int count;
1924
1925 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001926 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);
Nick Cheng36b83de2010-05-17 11:22:42 +08001929 return NULL;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001930 }
1931
Nick Cheng36b83de2010-05-17 11:22:42 +08001932 if (mode == 1) {
Erich Chen1c57e862006-07-12 08:59:32 -07001933 count = 8;
1934 while (count) {
1935 *acb_firm_model = readb(iop_firm_model);
1936 acb_firm_model++;
1937 iop_firm_model++;
1938 count--;
1939 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001940
Erich Chen1c57e862006-07-12 08:59:32 -07001941 count = 16;
1942 while (count) {
1943 *acb_firm_version = readb(iop_firm_version);
1944 acb_firm_version++;
1945 iop_firm_version++;
1946 count--;
1947 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001948
Nick Cheng36b83de2010-05-17 11:22:42 +08001949 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
Nick Cheng1a4f5502007-09-13 17:26:40 +08001957 printk(KERN_INFO "ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n"
Erich Chen1c57e862006-07-12 08:59:32 -07001958 , acb->host->host_no
1959 , acb->firm_version);
Nick Cheng36b83de2010-05-17 11:22:42 +08001960 acb->signature = readl(&reg->message_rwbuffer[0]);
Erich Chen1c57e862006-07-12 08:59:32 -07001961 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}
Nick Cheng36b83de2010-05-17 11:22:42 +08001966 return reg->message_rwbuffer;
1967}
1968static void __iomem *arcmsr_get_hbb_config(struct AdapterControlBlock *acb, int mode)
Nick Cheng1a4f5502007-09-13 17:26:40 +08001969{
Al Viro80da1ad2007-10-29 05:08:28 +00001970 struct MessageUnit_B *reg = acb->pmuB;
1971 uint32_t __iomem *lrwbuffer = reg->msgcode_rwbuffer_reg;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001972 char *acb_firm_model = acb->firm_model;
1973 char *acb_firm_version = acb->firm_version;
Nick Cheng36b83de2010-05-17 11:22:42 +08001974 char *acb_device_map = acb->device_map;
Al Viro80da1ad2007-10-29 05:08:28 +00001975 char __iomem *iop_firm_model = (char __iomem *)(&lrwbuffer[15]);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001976 /*firm_model,15,60-67*/
Al Viro80da1ad2007-10-29 05:08:28 +00001977 char __iomem *iop_firm_version = (char __iomem *)(&lrwbuffer[17]);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001978 /*firm_version,17,68-83*/
Nick Cheng36b83de2010-05-17 11:22:42 +08001979 char __iomem *iop_device_map = (char __iomem *) (&lrwbuffer[21]);
1980 /*firm_version,21,84-99*/
Nick Cheng1a4f5502007-09-13 17:26:40 +08001981 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);
Nick Cheng36b83de2010-05-17 11:22:42 +08001987 return NULL;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001988 }
1989
Nick Cheng36b83de2010-05-17 11:22:42 +08001990 if (mode == 1) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001991 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
Nick Cheng36b83de2010-05-17 11:22:42 +08002009 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
Nick Cheng1a4f5502007-09-13 17:26:40 +08002017 printk(KERN_INFO "ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n",
2018 acb->host->host_no,
2019 acb->firm_version);
2020
Nick Cheng36b83de2010-05-17 11:22:42 +08002021 acb->signature = readl(lrwbuffer++);
2022 /*firm_signature,1,00-03*/
Nick Cheng1a4f5502007-09-13 17:26:40 +08002023 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}
Nick Cheng36b83de2010-05-17 11:22:42 +08002032 return reg->msgcode_rwbuffer_reg;
2033}
2034static void *arcmsr_get_firmware_spec(struct AdapterControlBlock *acb, int mode)
Nick Cheng1a4f5502007-09-13 17:26:40 +08002035{
Nick Cheng36b83de2010-05-17 11:22:42 +08002036 void *rtnval = 0;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002037 switch (acb->adapter_type) {
2038 case ACB_ADAPTER_TYPE_A: {
Nick Cheng36b83de2010-05-17 11:22:42 +08002039 rtnval = arcmsr_get_hba_config(acb, mode);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002040 }
2041 break;
2042
2043 case ACB_ADAPTER_TYPE_B: {
Nick Cheng36b83de2010-05-17 11:22:42 +08002044 rtnval = arcmsr_get_hbb_config(acb, mode);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002045 }
2046 break;
2047 }
Nick Cheng36b83de2010-05-17 11:22:42 +08002048 return rtnval;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002049}
2050
2051static void arcmsr_polling_hba_ccbdone(struct AdapterControlBlock *acb,
Erich Chen1c57e862006-07-12 08:59:32 -07002052 struct CommandControlBlock *poll_ccb)
2053{
Al Viro80da1ad2007-10-29 05:08:28 +00002054 struct MessageUnit_A __iomem *reg = acb->pmuA;
Erich Chen1c57e862006-07-12 08:59:32 -07002055 struct CommandControlBlock *ccb;
2056 uint32_t flag_ccb, outbound_intstatus, poll_ccb_done = 0, poll_count = 0;
Erich Chen1c57e862006-07-12 08:59:32 -07002057
Nick Cheng1a4f5502007-09-13 17:26:40 +08002058 polling_hba_ccb_retry:
Erich Chen1c57e862006-07-12 08:59:32 -07002059 poll_count++;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002060 outbound_intstatus = readl(&reg->outbound_intstatus) & acb->outbound_int_enable;
Erich Chen1c57e862006-07-12 08:59:32 -07002061 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;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002070 goto polling_hba_ccb_retry;
Erich Chen1c57e862006-07-12 08:59:32 -07002071 }
2072 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08002073 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'"
Erich Chen1c57e862006-07-12 08:59:32 -07002078 " 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 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08002088 printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
2089 " command done ccb = '0x%p'"
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002090 "ccboutstandingcount = %d \n"
Erich Chen1c57e862006-07-12 08:59:32 -07002091 , acb->host->host_no
2092 , ccb
2093 , atomic_read(&acb->ccboutstandingcount));
2094 continue;
2095 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08002096 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
2097 }
2098}
2099
Nick Cheng76d78302008-02-04 23:53:24 -08002100static void arcmsr_polling_hbb_ccbdone(struct AdapterControlBlock *acb,
Nick Cheng1a4f5502007-09-13 17:26:40 +08002101 struct CommandControlBlock *poll_ccb)
2102{
Al Viro80da1ad2007-10-29 05:08:28 +00002103 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002104 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;
Erich Chen1c57e862006-07-12 08:59:32 -07002122 }
Erich Chen1c57e862006-07-12 08:59:32 -07002123 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08002124 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)) {
Nick Cheng76d78302008-02-04 23:53:24 -08002134 if ((ccb->startdone == ARCMSR_CCB_ABORTED) || (ccb == poll_ccb)) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08002135 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;
2142 arcmsr_ccb_complete(ccb, 1);
2143 continue;
2144 }
2145 printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
2146 " command done ccb = '0x%p'"
2147 "ccboutstandingcount = %d \n"
2148 , acb->host->host_no
2149 , ccb
2150 , atomic_read(&acb->ccboutstandingcount));
2151 continue;
2152 }
2153 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
2154 } /*drain reply FIFO*/
2155}
2156
Nick Cheng76d78302008-02-04 23:53:24 -08002157static void arcmsr_polling_ccbdone(struct AdapterControlBlock *acb,
Nick Cheng1a4f5502007-09-13 17:26:40 +08002158 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);
Erich Chen1c57e862006-07-12 08:59:32 -07002169 }
2170 }
2171}
Nick Cheng1a4f5502007-09-13 17:26:40 +08002172
2173static int arcmsr_iop_confirm(struct AdapterControlBlock *acb)
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002174{
Nick Cheng1a4f5502007-09-13 17:26:40 +08002175 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) {
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002192
Nick Cheng1a4f5502007-09-13 17:26:40 +08002193 case ACB_ADAPTER_TYPE_A: {
2194 if (ccb_phyaddr_hi32 != 0) {
Al Viro80da1ad2007-10-29 05:08:28 +00002195 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002196 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;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002208 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08002209 arcmsr_enable_outbound_ints(acb, intmask_org);
2210 }
2211 }
2212 break;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002213
Nick Cheng1a4f5502007-09-13 17:26:40 +08002214 case ACB_ADAPTER_TYPE_B: {
2215 unsigned long post_queue_phyaddr;
Al Viro80da1ad2007-10-29 05:08:28 +00002216 uint32_t __iomem *rwbuffer;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002217
Al Viro80da1ad2007-10-29 05:08:28 +00002218 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002219 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);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002242
Nick Cheng1a4f5502007-09-13 17:26:40 +08002243 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 }
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002249
Nick Cheng1a4f5502007-09-13 17:26:40 +08002250 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;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002259 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08002260 return 0;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002261}
2262
Nick Cheng1a4f5502007-09-13 17:26:40 +08002263static void arcmsr_wait_firmware_ready(struct AdapterControlBlock *acb)
2264{
2265 uint32_t firmware_state = 0;
2266
2267 switch (acb->adapter_type) {
2268
2269 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00002270 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002271 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: {
Al Viro80da1ad2007-10-29 05:08:28 +00002278 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002279 do {
2280 firmware_state = readl(reg->iop2drv_doorbell_reg);
2281 } while ((firmware_state & ARCMSR_MESSAGE_FIRMWARE_OK) == 0);
Nick Cheng76d78302008-02-04 23:53:24 -08002282 writel(ARCMSR_DRV2IOP_END_OF_INTERRUPT, reg->drv2iop_doorbell_reg);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002283 }
2284 break;
2285 }
2286}
2287
Nick Cheng36b83de2010-05-17 11:22:42 +08002288static 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
Nick Cheng1a4f5502007-09-13 17:26:40 +08002348static void arcmsr_start_hba_bgrb(struct AdapterControlBlock *acb)
2349{
Al Viro80da1ad2007-10-29 05:08:28 +00002350 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002351 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);
2356 }
2357}
2358
2359static void arcmsr_start_hbb_bgrb(struct AdapterControlBlock *acb)
2360{
Al Viro80da1ad2007-10-29 05:08:28 +00002361 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002362 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}
2369
2370static void arcmsr_start_adapter_bgrb(struct AdapterControlBlock *acb)
2371{
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}
2381
2382static void arcmsr_clear_doorbell_queue_buffer(struct AdapterControlBlock *acb)
2383{
2384 switch (acb->adapter_type) {
2385 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00002386 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002387 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;
2395
2396 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00002397 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002398 /*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;
2404 }
2405}
Erich Chen1c57e862006-07-12 08:59:32 -07002406
Nick Cheng76d78302008-02-04 23:53:24 -08002407static 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
Nick Cheng36b83de2010-05-17 11:22:42 +08002426static 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
Erich Chen1c57e862006-07-12 08:59:32 -07002480static void arcmsr_iop_init(struct AdapterControlBlock *acb)
2481{
Nick Cheng1a4f5502007-09-13 17:26:40 +08002482 uint32_t intmask_org;
Erich Chen1c57e862006-07-12 08:59:32 -07002483
Nick Cheng1a4f5502007-09-13 17:26:40 +08002484 /* disable all outbound interrupt */
2485 intmask_org = arcmsr_disable_outbound_ints(acb);
Nick Cheng76d78302008-02-04 23:53:24 -08002486 arcmsr_wait_firmware_ready(acb);
2487 arcmsr_iop_confirm(acb);
Nick Cheng36b83de2010-05-17 11:22:42 +08002488 arcmsr_get_firmware_spec(acb, 1);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002489 /*start background rebuild*/
2490 arcmsr_start_adapter_bgrb(acb);
2491 /* empty doorbell Qbuffer if door bell ringed */
2492 arcmsr_clear_doorbell_queue_buffer(acb);
Nick Cheng76d78302008-02-04 23:53:24 -08002493 arcmsr_enable_eoi_mode(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002494 /* enable outbound Post Queue,outbound doorbell Interrupt */
2495 arcmsr_enable_outbound_ints(acb, intmask_org);
Erich Chen1c57e862006-07-12 08:59:32 -07002496 acb->acb_flags |= ACB_F_IOP_INITED;
2497}
2498
Nick Cheng36b83de2010-05-17 11:22:42 +08002499static uint8_t arcmsr_iop_reset(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07002500{
Erich Chen1c57e862006-07-12 08:59:32 -07002501 struct CommandControlBlock *ccb;
2502 uint32_t intmask_org;
Nick Cheng36b83de2010-05-17 11:22:42 +08002503 uint8_t rtnval = 0x00;
Erich Chen1c57e862006-07-12 08:59:32 -07002504 int i = 0;
2505
2506 if (atomic_read(&acb->ccboutstandingcount) != 0) {
Erich Chen1c57e862006-07-12 08:59:32 -07002507 /* disable all outbound interrupt */
2508 intmask_org = arcmsr_disable_outbound_ints(acb);
Nick Cheng36b83de2010-05-17 11:22:42 +08002509 /* talk to iop 331 outstanding command aborted */
2510 rtnval = arcmsr_abort_allcmd(acb);
2511 /* wait for 3 sec for all command aborted*/
2512 ssleep(3);
Erich Chen1c57e862006-07-12 08:59:32 -07002513 /* clear all outbound posted Q */
Nick Cheng1a4f5502007-09-13 17:26:40 +08002514 arcmsr_done4abort_postqueue(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07002515 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2516 ccb = acb->pccb_pool[i];
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002517 if (ccb->startdone == ARCMSR_CCB_START) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08002518 arcmsr_ccb_complete(ccb, 1);
Erich Chen1c57e862006-07-12 08:59:32 -07002519 }
2520 }
Nick Cheng36b83de2010-05-17 11:22:42 +08002521 atomic_set(&acb->ccboutstandingcount, 0);
Erich Chen1c57e862006-07-12 08:59:32 -07002522 /* enable all outbound interrupt */
2523 arcmsr_enable_outbound_ints(acb, intmask_org);
Nick Cheng36b83de2010-05-17 11:22:42 +08002524 return rtnval;
Erich Chen1c57e862006-07-12 08:59:32 -07002525 }
Nick Cheng36b83de2010-05-17 11:22:42 +08002526 return rtnval;
Erich Chen1c57e862006-07-12 08:59:32 -07002527}
2528
2529static int arcmsr_bus_reset(struct scsi_cmnd *cmd)
2530{
2531 struct AdapterControlBlock *acb =
2532 (struct AdapterControlBlock *)cmd->device->host->hostdata;
Nick Cheng36b83de2010-05-17 11:22:42 +08002533 int retry = 0;
Erich Chen1c57e862006-07-12 08:59:32 -07002534
Nick Cheng36b83de2010-05-17 11:22:42 +08002535 if (acb->acb_flags & ACB_F_BUS_RESET)
2536 return SUCCESS;
2537
2538 printk(KERN_NOTICE "arcmsr%d: bus reset ..... \n", acb->adapter_index);
Erich Chen1c57e862006-07-12 08:59:32 -07002539 acb->acb_flags |= ACB_F_BUS_RESET;
Nick Cheng36b83de2010-05-17 11:22:42 +08002540 acb->num_resets++;
2541 while (atomic_read(&acb->ccboutstandingcount) != 0 && retry < 4) {
2542 arcmsr_interrupt(acb);
2543 retry++;
Erich Chen1c57e862006-07-12 08:59:32 -07002544 }
Nick Cheng36b83de2010-05-17 11:22:42 +08002545
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 }
2596 break;
2597 case ACB_ADAPTER_TYPE_B: {
2598 }
2599 }
2600 } else {
Erich Chen1c57e862006-07-12 08:59:32 -07002601 acb->acb_flags &= ~ACB_F_BUS_RESET;
Nick Cheng36b83de2010-05-17 11:22:42 +08002602 }
Erich Chen1c57e862006-07-12 08:59:32 -07002603 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 */
Nick Cheng1a4f5502007-09-13 17:26:40 +08002616 ssleep(3);
Erich Chen1c57e862006-07-12 08:59:32 -07002617
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
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002630 "arcmsr%d: abort device command of scsi id = %d lun = %d \n",
Erich Chen1c57e862006-07-12 08:59:32 -07002631 acb->host->host_no, cmd->device->id, cmd->device->lun);
2632 acb->num_aborts++;
Erich Chen1c57e862006-07-12 08:59:32 -07002633 /*
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:
Nick Cheng1a4f5502007-09-13 17:26:40 +08002663 case PCI_DEVICE_ID_ARECA_1200:
2664 case PCI_DEVICE_ID_ARECA_1202:
Erich Chen1c57e862006-07-12 08:59:32 -07002665 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:
Nick Cheng1a4f5502007-09-13 17:26:40 +08002672 case PCI_DEVICE_ID_ARECA_1201:
Erich Chen1c57e862006-07-12 08:59:32 -07002673 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 }
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002690 sprintf(buf, "Areca %s Host Adapter RAID Controller%s\n %s",
Erich Chen1c57e862006-07-12 08:59:32 -07002691 type, raid6 ? "( RAID6 capable)" : "",
2692 ARCMSR_DRIVER_VERSION);
2693 return buf;
2694}