blob: f4a202e8df267329e44b15cbe48ad546911de118 [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>
Erich Chen1c57e862006-07-12 08:59:32 -070061#include <asm/dma.h>
62#include <asm/io.h>
63#include <asm/system.h>
64#include <asm/uaccess.h>
65#include <scsi/scsi_host.h>
66#include <scsi/scsi.h>
67#include <scsi/scsi_cmnd.h>
68#include <scsi/scsi_tcq.h>
69#include <scsi/scsi_device.h>
70#include <scsi/scsi_transport.h>
71#include <scsi/scsicam.h>
72#include "arcmsr.h"
73
Nick Cheng1a4f5502007-09-13 17:26:40 +080074MODULE_AUTHOR("Erich Chen <support@areca.com.tw>");
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +080075MODULE_DESCRIPTION("ARECA (ARC11xx/12xx/13xx/16xx) SATA/SAS RAID HOST Adapter");
Erich Chen1c57e862006-07-12 08:59:32 -070076MODULE_LICENSE("Dual BSD/GPL");
77MODULE_VERSION(ARCMSR_DRIVER_VERSION);
78
Nick Cheng1a4f5502007-09-13 17:26:40 +080079static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb,
80 struct scsi_cmnd *cmd);
81static int arcmsr_iop_confirm(struct AdapterControlBlock *acb);
Erich Chen1c57e862006-07-12 08:59:32 -070082static int arcmsr_abort(struct scsi_cmnd *);
83static int arcmsr_bus_reset(struct scsi_cmnd *);
84static int arcmsr_bios_param(struct scsi_device *sdev,
Nick Cheng1a4f5502007-09-13 17:26:40 +080085 struct block_device *bdev, sector_t capacity, int *info);
86static int arcmsr_queue_command(struct scsi_cmnd *cmd,
87 void (*done) (struct scsi_cmnd *));
Erich Chen1c57e862006-07-12 08:59:32 -070088static int arcmsr_probe(struct pci_dev *pdev,
89 const struct pci_device_id *id);
90static void arcmsr_remove(struct pci_dev *pdev);
91static void arcmsr_shutdown(struct pci_dev *pdev);
92static void arcmsr_iop_init(struct AdapterControlBlock *acb);
93static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +080094static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb);
Erich Chen1c57e862006-07-12 08:59:32 -070095static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +080096static void arcmsr_flush_hba_cache(struct AdapterControlBlock *acb);
97static void arcmsr_flush_hbb_cache(struct AdapterControlBlock *acb);
Erich Chen1c57e862006-07-12 08:59:32 -070098static const char *arcmsr_info(struct Scsi_Host *);
99static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800100static int arcmsr_adjust_disk_queue_depth(struct scsi_device *sdev,
101 int queue_depth)
Erich Chen1c57e862006-07-12 08:59:32 -0700102{
103 if (queue_depth > ARCMSR_MAX_CMD_PERLUN)
104 queue_depth = ARCMSR_MAX_CMD_PERLUN;
105 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
106 return queue_depth;
107}
108
109static struct scsi_host_template arcmsr_scsi_host_template = {
110 .module = THIS_MODULE,
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800111 .name = "ARCMSR ARECA SATA/SAS RAID HOST Adapter"
112 ARCMSR_DRIVER_VERSION,
Erich Chen1c57e862006-07-12 08:59:32 -0700113 .info = arcmsr_info,
114 .queuecommand = arcmsr_queue_command,
115 .eh_abort_handler = arcmsr_abort,
116 .eh_bus_reset_handler = arcmsr_bus_reset,
117 .bios_param = arcmsr_bios_param,
118 .change_queue_depth = arcmsr_adjust_disk_queue_depth,
119 .can_queue = ARCMSR_MAX_OUTSTANDING_CMD,
120 .this_id = ARCMSR_SCSI_INITIATOR_ID,
121 .sg_tablesize = ARCMSR_MAX_SG_ENTRIES,
122 .max_sectors = ARCMSR_MAX_XFER_SECTORS,
123 .cmd_per_lun = ARCMSR_MAX_CMD_PERLUN,
124 .use_clustering = ENABLE_CLUSTERING,
125 .shost_attrs = arcmsr_host_attrs,
126};
Nick Cheng1a4f5502007-09-13 17:26:40 +0800127#ifdef CONFIG_SCSI_ARCMSR_AER
Andrew Morton5975f642007-10-02 14:37:59 -0700128static pci_ers_result_t arcmsr_pci_slot_reset(struct pci_dev *pdev);
129static pci_ers_result_t arcmsr_pci_error_detected(struct pci_dev *pdev,
130 pci_channel_state_t state);
131
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800132static struct pci_error_handlers arcmsr_pci_error_handlers = {
133 .error_detected = arcmsr_pci_error_detected,
134 .slot_reset = arcmsr_pci_slot_reset,
135};
Nick Cheng1a4f5502007-09-13 17:26:40 +0800136#endif
Erich Chen1c57e862006-07-12 08:59:32 -0700137static struct pci_device_id arcmsr_device_id_table[] = {
138 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1110)},
139 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1120)},
140 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1130)},
141 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1160)},
142 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1170)},
Nick Cheng1a4f5502007-09-13 17:26:40 +0800143 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1200)},
144 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1201)},
145 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1202)},
Erich Chen1c57e862006-07-12 08:59:32 -0700146 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1210)},
147 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1220)},
148 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1230)},
149 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1260)},
150 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1270)},
151 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1280)},
152 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1380)},
153 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1381)},
154 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1680)},
155 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1681)},
156 {0, 0}, /* Terminating entry */
157};
158MODULE_DEVICE_TABLE(pci, arcmsr_device_id_table);
159static struct pci_driver arcmsr_pci_driver = {
160 .name = "arcmsr",
161 .id_table = arcmsr_device_id_table,
162 .probe = arcmsr_probe,
163 .remove = arcmsr_remove,
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800164 .shutdown = arcmsr_shutdown,
Nick Cheng1a4f5502007-09-13 17:26:40 +0800165 #ifdef CONFIG_SCSI_ARCMSR_AER
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800166 .err_handler = &arcmsr_pci_error_handlers,
Nick Cheng1a4f5502007-09-13 17:26:40 +0800167 #endif
Erich Chen1c57e862006-07-12 08:59:32 -0700168};
169
David Howells7d12e782006-10-05 14:55:46 +0100170static irqreturn_t arcmsr_do_interrupt(int irq, void *dev_id)
Erich Chen1c57e862006-07-12 08:59:32 -0700171{
172 irqreturn_t handle_state;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800173 struct AdapterControlBlock *acb = dev_id;
Erich Chen1c57e862006-07-12 08:59:32 -0700174
Nick Cheng1a4f5502007-09-13 17:26:40 +0800175 spin_lock(acb->host->host_lock);
Erich Chen1c57e862006-07-12 08:59:32 -0700176 handle_state = arcmsr_interrupt(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800177 spin_unlock(acb->host->host_lock);
178
Erich Chen1c57e862006-07-12 08:59:32 -0700179 return handle_state;
180}
181
182static int arcmsr_bios_param(struct scsi_device *sdev,
183 struct block_device *bdev, sector_t capacity, int *geom)
184{
185 int ret, heads, sectors, cylinders, total_capacity;
186 unsigned char *buffer;/* return copy of block device's partition table */
187
188 buffer = scsi_bios_ptable(bdev);
189 if (buffer) {
190 ret = scsi_partsize(buffer, capacity, &geom[2], &geom[0], &geom[1]);
191 kfree(buffer);
192 if (ret != -1)
193 return ret;
194 }
195 total_capacity = capacity;
196 heads = 64;
197 sectors = 32;
198 cylinders = total_capacity / (heads * sectors);
199 if (cylinders > 1024) {
200 heads = 255;
201 sectors = 63;
202 cylinders = total_capacity / (heads * sectors);
203 }
204 geom[0] = heads;
205 geom[1] = sectors;
206 geom[2] = cylinders;
207 return 0;
208}
209
Nick Cheng1a4f5502007-09-13 17:26:40 +0800210static void arcmsr_define_adapter_type(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -0700211{
212 struct pci_dev *pdev = acb->pdev;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800213 u16 dev_id;
214 pci_read_config_word(pdev, PCI_DEVICE_ID, &dev_id);
215 switch (dev_id) {
216 case 0x1201 : {
217 acb->adapter_type = ACB_ADAPTER_TYPE_B;
218 }
219 break;
Erich Chen1c57e862006-07-12 08:59:32 -0700220
Nick Cheng1a4f5502007-09-13 17:26:40 +0800221 default : acb->adapter_type = ACB_ADAPTER_TYPE_A;
222 }
223}
224
225static int arcmsr_alloc_ccb_pool(struct AdapterControlBlock *acb)
226{
227
228 switch (acb->adapter_type) {
229
230 case ACB_ADAPTER_TYPE_A: {
231 struct pci_dev *pdev = acb->pdev;
232 void *dma_coherent;
233 dma_addr_t dma_coherent_handle, dma_addr;
234 struct CommandControlBlock *ccb_tmp;
235 uint32_t intmask_org;
236 int i, j;
237
Al Viro80da1ad2007-10-29 05:08:28 +0000238 acb->pmuA = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
239 if (!acb->pmuA) {
Nick Cheng1a4f5502007-09-13 17:26:40 +0800240 printk(KERN_NOTICE "arcmsr%d: memory mapping region fail \n",
241 acb->host->host_no);
Al Virodb3a91f2007-10-29 05:08:38 +0000242 return -ENOMEM;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800243 }
244
245 dma_coherent = dma_alloc_coherent(&pdev->dev,
Erich Chen1c57e862006-07-12 08:59:32 -0700246 ARCMSR_MAX_FREECCB_NUM *
247 sizeof (struct CommandControlBlock) + 0x20,
248 &dma_coherent_handle, GFP_KERNEL);
Al Virodb3a91f2007-10-29 05:08:38 +0000249
250 if (!dma_coherent) {
251 iounmap(acb->pmuA);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800252 return -ENOMEM;
Al Virodb3a91f2007-10-29 05:08:38 +0000253 }
Erich Chen1c57e862006-07-12 08:59:32 -0700254
Nick Cheng1a4f5502007-09-13 17:26:40 +0800255 acb->dma_coherent = dma_coherent;
256 acb->dma_coherent_handle = dma_coherent_handle;
Erich Chen1c57e862006-07-12 08:59:32 -0700257
Nick Cheng1a4f5502007-09-13 17:26:40 +0800258 if (((unsigned long)dma_coherent & 0x1F)) {
259 dma_coherent = dma_coherent +
260 (0x20 - ((unsigned long)dma_coherent & 0x1F));
261 dma_coherent_handle = dma_coherent_handle +
262 (0x20 - ((unsigned long)dma_coherent_handle & 0x1F));
263 }
264
265 dma_addr = dma_coherent_handle;
266 ccb_tmp = (struct CommandControlBlock *)dma_coherent;
267 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
268 ccb_tmp->cdb_shifted_phyaddr = dma_addr >> 5;
269 ccb_tmp->acb = acb;
270 acb->pccb_pool[i] = ccb_tmp;
271 list_add_tail(&ccb_tmp->list, &acb->ccb_free_list);
272 dma_addr = dma_addr + sizeof(struct CommandControlBlock);
273 ccb_tmp++;
274 }
275
276 acb->vir2phy_offset = (unsigned long)ccb_tmp -(unsigned long)dma_addr;
277 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
278 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
279 acb->devstate[i][j] = ARECA_RAID_GONE;
280
281 /*
282 ** here we need to tell iop 331 our ccb_tmp.HighPart
283 ** if ccb_tmp.HighPart is not zero
284 */
285 intmask_org = arcmsr_disable_outbound_ints(acb);
286 }
287 break;
288
289 case ACB_ADAPTER_TYPE_B: {
290
291 struct pci_dev *pdev = acb->pdev;
292 struct MessageUnit_B *reg;
Al Viro80da1ad2007-10-29 05:08:28 +0000293 void __iomem *mem_base0, *mem_base1;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800294 void *dma_coherent;
295 dma_addr_t dma_coherent_handle, dma_addr;
296 uint32_t intmask_org;
297 struct CommandControlBlock *ccb_tmp;
298 int i, j;
299
300 dma_coherent = dma_alloc_coherent(&pdev->dev,
301 ((ARCMSR_MAX_FREECCB_NUM *
302 sizeof(struct CommandControlBlock) + 0x20) +
303 sizeof(struct MessageUnit_B)),
304 &dma_coherent_handle, GFP_KERNEL);
305 if (!dma_coherent)
306 return -ENOMEM;
307
308 acb->dma_coherent = dma_coherent;
309 acb->dma_coherent_handle = dma_coherent_handle;
310
311 if (((unsigned long)dma_coherent & 0x1F)) {
312 dma_coherent = dma_coherent +
313 (0x20 - ((unsigned long)dma_coherent & 0x1F));
314 dma_coherent_handle = dma_coherent_handle +
315 (0x20 - ((unsigned long)dma_coherent_handle & 0x1F));
316 }
317
318 reg = (struct MessageUnit_B *)(dma_coherent +
319 ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock));
320
321 dma_addr = dma_coherent_handle;
322 ccb_tmp = (struct CommandControlBlock *)dma_coherent;
323 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
324 ccb_tmp->cdb_shifted_phyaddr = dma_addr >> 5;
325 ccb_tmp->acb = acb;
326 acb->pccb_pool[i] = ccb_tmp;
327 list_add_tail(&ccb_tmp->list, &acb->ccb_free_list);
328 dma_addr = dma_addr + sizeof(struct CommandControlBlock);
329 ccb_tmp++;
330 }
331
332 reg = (struct MessageUnit_B *)(dma_coherent +
333 ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock));
Al Viro80da1ad2007-10-29 05:08:28 +0000334 acb->pmuB = reg;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800335 mem_base0 = ioremap(pci_resource_start(pdev, 0),
336 pci_resource_len(pdev, 0));
Al Virodb3a91f2007-10-29 05:08:38 +0000337 if (!mem_base0)
338 goto out;
339
Nick Cheng1a4f5502007-09-13 17:26:40 +0800340 mem_base1 = ioremap(pci_resource_start(pdev, 2),
341 pci_resource_len(pdev, 2));
Al Virodb3a91f2007-10-29 05:08:38 +0000342 if (!mem_base1) {
343 iounmap(mem_base0);
344 goto out;
345 }
346
Al Viro80da1ad2007-10-29 05:08:28 +0000347 reg->drv2iop_doorbell_reg = mem_base0 + ARCMSR_DRV2IOP_DOORBELL;
348 reg->drv2iop_doorbell_mask_reg = mem_base0 +
349 ARCMSR_DRV2IOP_DOORBELL_MASK;
350 reg->iop2drv_doorbell_reg = mem_base0 + ARCMSR_IOP2DRV_DOORBELL;
351 reg->iop2drv_doorbell_mask_reg = mem_base0 +
352 ARCMSR_IOP2DRV_DOORBELL_MASK;
353 reg->ioctl_wbuffer_reg = mem_base1 + ARCMSR_IOCTL_WBUFFER;
354 reg->ioctl_rbuffer_reg = mem_base1 + ARCMSR_IOCTL_RBUFFER;
355 reg->msgcode_rwbuffer_reg = mem_base1 + ARCMSR_MSGCODE_RWBUFFER;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800356
357 acb->vir2phy_offset = (unsigned long)ccb_tmp -(unsigned long)dma_addr;
358 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
359 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
360 acb->devstate[i][j] = ARECA_RAID_GOOD;
361
362 /*
363 ** here we need to tell iop 331 our ccb_tmp.HighPart
364 ** if ccb_tmp.HighPart is not zero
365 */
366 intmask_org = arcmsr_disable_outbound_ints(acb);
367 }
368 break;
Erich Chen1c57e862006-07-12 08:59:32 -0700369 }
Erich Chen1c57e862006-07-12 08:59:32 -0700370 return 0;
Al Virodb3a91f2007-10-29 05:08:38 +0000371
372out:
373 dma_free_coherent(&acb->pdev->dev,
374 ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock) + 0x20,
375 acb->dma_coherent, acb->dma_coherent_handle);
376 return -ENOMEM;
Erich Chen1c57e862006-07-12 08:59:32 -0700377}
378
379static int arcmsr_probe(struct pci_dev *pdev,
380 const struct pci_device_id *id)
381{
382 struct Scsi_Host *host;
383 struct AdapterControlBlock *acb;
384 uint8_t bus, dev_fun;
385 int error;
386
387 error = pci_enable_device(pdev);
388 if (error)
389 goto out;
390 pci_set_master(pdev);
391
392 host = scsi_host_alloc(&arcmsr_scsi_host_template,
393 sizeof(struct AdapterControlBlock));
394 if (!host) {
395 error = -ENOMEM;
396 goto out_disable_device;
397 }
398 acb = (struct AdapterControlBlock *)host->hostdata;
399 memset(acb, 0, sizeof (struct AdapterControlBlock));
400
401 error = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
402 if (error) {
403 error = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
404 if (error) {
405 printk(KERN_WARNING
406 "scsi%d: No suitable DMA mask available\n",
407 host->host_no);
408 goto out_host_put;
409 }
410 }
411 bus = pdev->bus->number;
412 dev_fun = pdev->devfn;
413 acb->host = host;
414 acb->pdev = pdev;
415 host->max_sectors = ARCMSR_MAX_XFER_SECTORS;
416 host->max_lun = ARCMSR_MAX_TARGETLUN;
417 host->max_id = ARCMSR_MAX_TARGETID;/*16:8*/
418 host->max_cmd_len = 16; /*this is issue of 64bit LBA, over 2T byte*/
419 host->sg_tablesize = ARCMSR_MAX_SG_ENTRIES;
420 host->can_queue = ARCMSR_MAX_FREECCB_NUM; /* max simultaneous cmds */
421 host->cmd_per_lun = ARCMSR_MAX_CMD_PERLUN;
422 host->this_id = ARCMSR_SCSI_INITIATOR_ID;
423 host->unique_id = (bus << 8) | dev_fun;
424 host->irq = pdev->irq;
425 error = pci_request_regions(pdev, "arcmsr");
Nick Cheng1a4f5502007-09-13 17:26:40 +0800426 if (error) {
Erich Chen1c57e862006-07-12 08:59:32 -0700427 goto out_host_put;
Erich Chen1c57e862006-07-12 08:59:32 -0700428 }
Nick Cheng1a4f5502007-09-13 17:26:40 +0800429 arcmsr_define_adapter_type(acb);
430
Erich Chen1c57e862006-07-12 08:59:32 -0700431 acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
432 ACB_F_MESSAGE_RQBUFFER_CLEARED |
433 ACB_F_MESSAGE_WQBUFFER_READED);
434 acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
435 INIT_LIST_HEAD(&acb->ccb_free_list);
436
437 error = arcmsr_alloc_ccb_pool(acb);
438 if (error)
Nick Cheng1a4f5502007-09-13 17:26:40 +0800439 goto out_release_regions;
Erich Chen1c57e862006-07-12 08:59:32 -0700440
441 error = request_irq(pdev->irq, arcmsr_do_interrupt,
Jeff Garzik488a5c82007-07-27 13:01:15 -0400442 IRQF_SHARED, "arcmsr", acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700443 if (error)
444 goto out_free_ccb_pool;
445
446 arcmsr_iop_init(acb);
447 pci_set_drvdata(pdev, host);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800448 if (strncmp(acb->firm_version, "V1.42", 5) >= 0)
449 host->max_sectors= ARCMSR_MAX_XFER_SECTORS_B;
Erich Chen1c57e862006-07-12 08:59:32 -0700450
451 error = scsi_add_host(host, &pdev->dev);
452 if (error)
453 goto out_free_irq;
454
455 error = arcmsr_alloc_sysfs_attr(acb);
456 if (error)
457 goto out_free_sysfs;
458
459 scsi_scan_host(host);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800460 #ifdef CONFIG_SCSI_ARCMSR_AER
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800461 pci_enable_pcie_error_reporting(pdev);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800462 #endif
Erich Chen1c57e862006-07-12 08:59:32 -0700463 return 0;
464 out_free_sysfs:
465 out_free_irq:
466 free_irq(pdev->irq, acb);
467 out_free_ccb_pool:
468 arcmsr_free_ccb_pool(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700469 out_release_regions:
470 pci_release_regions(pdev);
471 out_host_put:
472 scsi_host_put(host);
473 out_disable_device:
474 pci_disable_device(pdev);
475 out:
476 return error;
477}
478
Nick Cheng1a4f5502007-09-13 17:26:40 +0800479static uint8_t arcmsr_hba_wait_msgint_ready(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -0700480{
Al Viro80da1ad2007-10-29 05:08:28 +0000481 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800482 uint32_t Index;
483 uint8_t Retries = 0x00;
484
485 do {
486 for (Index = 0; Index < 100; Index++) {
487 if (readl(&reg->outbound_intstatus) &
488 ARCMSR_MU_OUTBOUND_MESSAGE0_INT) {
489 writel(ARCMSR_MU_OUTBOUND_MESSAGE0_INT,
490 &reg->outbound_intstatus);
491 return 0x00;
492 }
493 msleep(10);
494 }/*max 1 seconds*/
495
496 } while (Retries++ < 20);/*max 20 sec*/
497 return 0xff;
498}
499
500static uint8_t arcmsr_hbb_wait_msgint_ready(struct AdapterControlBlock *acb)
501{
Al Viro80da1ad2007-10-29 05:08:28 +0000502 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800503 uint32_t Index;
504 uint8_t Retries = 0x00;
505
506 do {
507 for (Index = 0; Index < 100; Index++) {
508 if (readl(reg->iop2drv_doorbell_reg)
509 & ARCMSR_IOP2DRV_MESSAGE_CMD_DONE) {
510 writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN
511 , reg->iop2drv_doorbell_reg);
512 return 0x00;
513 }
514 msleep(10);
515 }/*max 1 seconds*/
516
517 } while (Retries++ < 20);/*max 20 sec*/
518 return 0xff;
519}
520
521static void arcmsr_abort_hba_allcmd(struct AdapterControlBlock *acb)
522{
Al Viro80da1ad2007-10-29 05:08:28 +0000523 struct MessageUnit_A __iomem *reg = acb->pmuA;
Erich Chen1c57e862006-07-12 08:59:32 -0700524
525 writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, &reg->inbound_msgaddr0);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800526 if (arcmsr_hba_wait_msgint_ready(acb))
Erich Chen1c57e862006-07-12 08:59:32 -0700527 printk(KERN_NOTICE
528 "arcmsr%d: wait 'abort all outstanding command' timeout \n"
529 , acb->host->host_no);
530}
531
Nick Cheng1a4f5502007-09-13 17:26:40 +0800532static void arcmsr_abort_hbb_allcmd(struct AdapterControlBlock *acb)
533{
Al Viro80da1ad2007-10-29 05:08:28 +0000534 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800535
536 writel(ARCMSR_MESSAGE_ABORT_CMD, reg->drv2iop_doorbell_reg);
537 if (arcmsr_hbb_wait_msgint_ready(acb))
538 printk(KERN_NOTICE
539 "arcmsr%d: wait 'abort all outstanding command' timeout \n"
540 , acb->host->host_no);
541}
542
543static void arcmsr_abort_allcmd(struct AdapterControlBlock *acb)
544{
545 switch (acb->adapter_type) {
546 case ACB_ADAPTER_TYPE_A: {
547 arcmsr_abort_hba_allcmd(acb);
548 }
549 break;
550
551 case ACB_ADAPTER_TYPE_B: {
552 arcmsr_abort_hbb_allcmd(acb);
553 }
554 }
555}
556
Erich Chen1c57e862006-07-12 08:59:32 -0700557static void arcmsr_pci_unmap_dma(struct CommandControlBlock *ccb)
558{
Erich Chen1c57e862006-07-12 08:59:32 -0700559 struct scsi_cmnd *pcmd = ccb->pcmd;
560
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900561 scsi_dma_unmap(pcmd);
Erich Chen1c57e862006-07-12 08:59:32 -0700562}
563
564static void arcmsr_ccb_complete(struct CommandControlBlock *ccb, int stand_flag)
565{
566 struct AdapterControlBlock *acb = ccb->acb;
567 struct scsi_cmnd *pcmd = ccb->pcmd;
568
569 arcmsr_pci_unmap_dma(ccb);
570 if (stand_flag == 1)
571 atomic_dec(&acb->ccboutstandingcount);
572 ccb->startdone = ARCMSR_CCB_DONE;
573 ccb->ccb_flags = 0;
574 list_add_tail(&ccb->list, &acb->ccb_free_list);
575 pcmd->scsi_done(pcmd);
576}
577
Nick Cheng1a4f5502007-09-13 17:26:40 +0800578static void arcmsr_flush_hba_cache(struct AdapterControlBlock *acb)
579{
Al Viro80da1ad2007-10-29 05:08:28 +0000580 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800581 int retry_count = 30;
582
583 writel(ARCMSR_INBOUND_MESG0_FLUSH_CACHE, &reg->inbound_msgaddr0);
584 do {
585 if (!arcmsr_hba_wait_msgint_ready(acb))
586 break;
587 else {
588 retry_count--;
589 printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
590 timeout, retry count down = %d \n", acb->host->host_no, retry_count);
591 }
592 } while (retry_count != 0);
593}
594
595static void arcmsr_flush_hbb_cache(struct AdapterControlBlock *acb)
596{
Al Viro80da1ad2007-10-29 05:08:28 +0000597 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800598 int retry_count = 30;
599
600 writel(ARCMSR_MESSAGE_FLUSH_CACHE, reg->drv2iop_doorbell_reg);
601 do {
602 if (!arcmsr_hbb_wait_msgint_ready(acb))
603 break;
604 else {
605 retry_count--;
606 printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
607 timeout,retry count down = %d \n", acb->host->host_no, retry_count);
608 }
609 } while (retry_count != 0);
610}
611
612static void arcmsr_flush_adapter_cache(struct AdapterControlBlock *acb)
613{
614 switch (acb->adapter_type) {
615
616 case ACB_ADAPTER_TYPE_A: {
617 arcmsr_flush_hba_cache(acb);
618 }
619 break;
620
621 case ACB_ADAPTER_TYPE_B: {
622 arcmsr_flush_hbb_cache(acb);
623 }
624 }
625}
626
627static void arcmsr_report_sense_info(struct CommandControlBlock *ccb)
628{
629
630 struct scsi_cmnd *pcmd = ccb->pcmd;
631 struct SENSE_DATA *sensebuffer = (struct SENSE_DATA *)pcmd->sense_buffer;
632
633 pcmd->result = DID_OK << 16;
634 if (sensebuffer) {
635 int sense_data_length =
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900636 sizeof(struct SENSE_DATA) < SCSI_SENSE_BUFFERSIZE
637 ? sizeof(struct SENSE_DATA) : SCSI_SENSE_BUFFERSIZE;
638 memset(sensebuffer, 0, SCSI_SENSE_BUFFERSIZE);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800639 memcpy(sensebuffer, ccb->arcmsr_cdb.SenseData, sense_data_length);
640 sensebuffer->ErrorCode = SCSI_SENSE_CURRENT_ERRORS;
641 sensebuffer->Valid = 1;
642 }
643}
644
645static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb)
646{
647 u32 orig_mask = 0;
648 switch (acb->adapter_type) {
649
650 case ACB_ADAPTER_TYPE_A : {
Al Viro80da1ad2007-10-29 05:08:28 +0000651 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800652 orig_mask = readl(&reg->outbound_intmask)|\
653 ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE;
654 writel(orig_mask|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE, \
655 &reg->outbound_intmask);
656 }
657 break;
658
659 case ACB_ADAPTER_TYPE_B : {
Al Viro80da1ad2007-10-29 05:08:28 +0000660 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800661 orig_mask = readl(reg->iop2drv_doorbell_mask_reg) & \
662 (~ARCMSR_IOP2DRV_MESSAGE_CMD_DONE);
663 writel(0, reg->iop2drv_doorbell_mask_reg);
664 }
665 break;
666 }
667 return orig_mask;
668}
669
670static void arcmsr_report_ccb_state(struct AdapterControlBlock *acb, \
671 struct CommandControlBlock *ccb, uint32_t flag_ccb)
672{
673
674 uint8_t id, lun;
675 id = ccb->pcmd->device->id;
676 lun = ccb->pcmd->device->lun;
677 if (!(flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR)) {
678 if (acb->devstate[id][lun] == ARECA_RAID_GONE)
679 acb->devstate[id][lun] = ARECA_RAID_GOOD;
680 ccb->pcmd->result = DID_OK << 16;
681 arcmsr_ccb_complete(ccb, 1);
682 } else {
683 switch (ccb->arcmsr_cdb.DeviceStatus) {
684 case ARCMSR_DEV_SELECT_TIMEOUT: {
685 acb->devstate[id][lun] = ARECA_RAID_GONE;
686 ccb->pcmd->result = DID_NO_CONNECT << 16;
687 arcmsr_ccb_complete(ccb, 1);
688 }
689 break;
690
691 case ARCMSR_DEV_ABORTED:
692
693 case ARCMSR_DEV_INIT_FAIL: {
694 acb->devstate[id][lun] = ARECA_RAID_GONE;
695 ccb->pcmd->result = DID_BAD_TARGET << 16;
696 arcmsr_ccb_complete(ccb, 1);
697 }
698 break;
699
700 case ARCMSR_DEV_CHECK_CONDITION: {
701 acb->devstate[id][lun] = ARECA_RAID_GOOD;
702 arcmsr_report_sense_info(ccb);
703 arcmsr_ccb_complete(ccb, 1);
704 }
705 break;
706
707 default:
708 printk(KERN_NOTICE
709 "arcmsr%d: scsi id = %d lun = %d"
710 " isr get command error done, "
711 "but got unknown DeviceStatus = 0x%x \n"
712 , acb->host->host_no
713 , id
714 , lun
715 , ccb->arcmsr_cdb.DeviceStatus);
716 acb->devstate[id][lun] = ARECA_RAID_GONE;
717 ccb->pcmd->result = DID_NO_CONNECT << 16;
718 arcmsr_ccb_complete(ccb, 1);
719 break;
720 }
721 }
722}
723
724static void arcmsr_drain_donequeue(struct AdapterControlBlock *acb, uint32_t flag_ccb)
725
726{
727 struct CommandControlBlock *ccb;
728
729 ccb = (struct CommandControlBlock *)(acb->vir2phy_offset + (flag_ccb << 5));
730 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
731 if (ccb->startdone == ARCMSR_CCB_ABORTED) {
732 struct scsi_cmnd *abortcmd = ccb->pcmd;
733 if (abortcmd) {
734 abortcmd->result |= DID_ABORT << 16;
735 arcmsr_ccb_complete(ccb, 1);
736 printk(KERN_NOTICE "arcmsr%d: ccb ='0x%p' \
737 isr got aborted command \n", acb->host->host_no, ccb);
738 }
739 }
740 printk(KERN_NOTICE "arcmsr%d: isr get an illegal ccb command \
741 done acb = '0x%p'"
742 "ccb = '0x%p' ccbacb = '0x%p' startdone = 0x%x"
743 " ccboutstandingcount = %d \n"
744 , acb->host->host_no
745 , acb
746 , ccb
747 , ccb->acb
748 , ccb->startdone
749 , atomic_read(&acb->ccboutstandingcount));
750 }
751 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
752}
753
754static void arcmsr_done4abort_postqueue(struct AdapterControlBlock *acb)
755{
756 int i = 0;
757 uint32_t flag_ccb;
758
759 switch (acb->adapter_type) {
760
761 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +0000762 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800763 uint32_t outbound_intstatus;
Al Viro80da1ad2007-10-29 05:08:28 +0000764 outbound_intstatus = readl(&reg->outbound_intstatus) &
Nick Cheng1a4f5502007-09-13 17:26:40 +0800765 acb->outbound_int_enable;
766 /*clear and abort all outbound posted Q*/
767 writel(outbound_intstatus, &reg->outbound_intstatus);/*clear interrupt*/
Al Viro80da1ad2007-10-29 05:08:28 +0000768 while (((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF)
Nick Cheng1a4f5502007-09-13 17:26:40 +0800769 && (i++ < ARCMSR_MAX_OUTSTANDING_CMD)) {
770 arcmsr_drain_donequeue(acb, flag_ccb);
771 }
772 }
773 break;
774
775 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +0000776 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800777 /*clear all outbound posted Q*/
778 for (i = 0; i < ARCMSR_MAX_HBB_POSTQUEUE; i++) {
779 if ((flag_ccb = readl(&reg->done_qbuffer[i])) != 0) {
780 writel(0, &reg->done_qbuffer[i]);
781 arcmsr_drain_donequeue(acb, flag_ccb);
782 }
783 writel(0, &reg->post_qbuffer[i]);
784 }
785 reg->doneq_index = 0;
786 reg->postq_index = 0;
787 }
788 break;
789 }
790}
Erich Chen1c57e862006-07-12 08:59:32 -0700791static void arcmsr_remove(struct pci_dev *pdev)
792{
793 struct Scsi_Host *host = pci_get_drvdata(pdev);
794 struct AdapterControlBlock *acb =
795 (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -0700796 int poll_count = 0;
797
798 arcmsr_free_sysfs_attr(acb);
799 scsi_remove_host(host);
800 arcmsr_stop_adapter_bgrb(acb);
801 arcmsr_flush_adapter_cache(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800802 arcmsr_disable_outbound_ints(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700803 acb->acb_flags |= ACB_F_SCSISTOPADAPTER;
804 acb->acb_flags &= ~ACB_F_IOP_INITED;
805
Nick Cheng1a4f5502007-09-13 17:26:40 +0800806 for (poll_count = 0; poll_count < ARCMSR_MAX_OUTSTANDING_CMD; poll_count++) {
Erich Chen1c57e862006-07-12 08:59:32 -0700807 if (!atomic_read(&acb->ccboutstandingcount))
808 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800809 arcmsr_interrupt(acb);/* FIXME: need spinlock */
Erich Chen1c57e862006-07-12 08:59:32 -0700810 msleep(25);
811 }
812
813 if (atomic_read(&acb->ccboutstandingcount)) {
814 int i;
815
816 arcmsr_abort_allcmd(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800817 arcmsr_done4abort_postqueue(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700818 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
819 struct CommandControlBlock *ccb = acb->pccb_pool[i];
820 if (ccb->startdone == ARCMSR_CCB_START) {
821 ccb->startdone = ARCMSR_CCB_ABORTED;
822 ccb->pcmd->result = DID_ABORT << 16;
823 arcmsr_ccb_complete(ccb, 1);
824 }
825 }
826 }
827
828 free_irq(pdev->irq, acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700829 arcmsr_free_ccb_pool(acb);
830 pci_release_regions(pdev);
831
832 scsi_host_put(host);
833
834 pci_disable_device(pdev);
835 pci_set_drvdata(pdev, NULL);
836}
837
838static void arcmsr_shutdown(struct pci_dev *pdev)
839{
840 struct Scsi_Host *host = pci_get_drvdata(pdev);
841 struct AdapterControlBlock *acb =
842 (struct AdapterControlBlock *)host->hostdata;
843
844 arcmsr_stop_adapter_bgrb(acb);
845 arcmsr_flush_adapter_cache(acb);
846}
847
848static int arcmsr_module_init(void)
849{
850 int error = 0;
851
852 error = pci_register_driver(&arcmsr_pci_driver);
853 return error;
854}
855
856static void arcmsr_module_exit(void)
857{
858 pci_unregister_driver(&arcmsr_pci_driver);
859}
860module_init(arcmsr_module_init);
861module_exit(arcmsr_module_exit);
862
Nick Cheng1a4f5502007-09-13 17:26:40 +0800863static void arcmsr_enable_outbound_ints(struct AdapterControlBlock *acb, \
864 u32 intmask_org)
Erich Chen1c57e862006-07-12 08:59:32 -0700865{
Erich Chen1c57e862006-07-12 08:59:32 -0700866 u32 mask;
867
Nick Cheng1a4f5502007-09-13 17:26:40 +0800868 switch (acb->adapter_type) {
869
870 case ACB_ADAPTER_TYPE_A : {
Al Viro80da1ad2007-10-29 05:08:28 +0000871 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800872 mask = intmask_org & ~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE |
Erich Chen1c57e862006-07-12 08:59:32 -0700873 ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800874 writel(mask, &reg->outbound_intmask);
875 acb->outbound_int_enable = ~(intmask_org & mask) & 0x000000ff;
876 }
877 break;
Erich Chen1c57e862006-07-12 08:59:32 -0700878
Nick Cheng1a4f5502007-09-13 17:26:40 +0800879 case ACB_ADAPTER_TYPE_B : {
Al Viro80da1ad2007-10-29 05:08:28 +0000880 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800881 mask = intmask_org | (ARCMSR_IOP2DRV_DATA_WRITE_OK | \
882 ARCMSR_IOP2DRV_DATA_READ_OK | ARCMSR_IOP2DRV_CDB_DONE);
883 writel(mask, reg->iop2drv_doorbell_mask_reg);
884 acb->outbound_int_enable = (intmask_org | mask) & 0x0000000f;
885 }
Erich Chen1c57e862006-07-12 08:59:32 -0700886 }
887}
888
Erich Chen1c57e862006-07-12 08:59:32 -0700889static void arcmsr_build_ccb(struct AdapterControlBlock *acb,
890 struct CommandControlBlock *ccb, struct scsi_cmnd *pcmd)
891{
892 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
893 int8_t *psge = (int8_t *)&arcmsr_cdb->u;
Al Viro80da1ad2007-10-29 05:08:28 +0000894 __le32 address_lo, address_hi;
Erich Chen1c57e862006-07-12 08:59:32 -0700895 int arccdbsize = 0x30;
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900896 int nseg;
Erich Chen1c57e862006-07-12 08:59:32 -0700897
898 ccb->pcmd = pcmd;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800899 memset(arcmsr_cdb, 0, sizeof(struct ARCMSR_CDB));
Erich Chen1c57e862006-07-12 08:59:32 -0700900 arcmsr_cdb->Bus = 0;
901 arcmsr_cdb->TargetID = pcmd->device->id;
902 arcmsr_cdb->LUN = pcmd->device->lun;
903 arcmsr_cdb->Function = 1;
904 arcmsr_cdb->CdbLength = (uint8_t)pcmd->cmd_len;
905 arcmsr_cdb->Context = (unsigned long)arcmsr_cdb;
906 memcpy(arcmsr_cdb->Cdb, pcmd->cmnd, pcmd->cmd_len);
Erich Chen1c57e862006-07-12 08:59:32 -0700907
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900908 nseg = scsi_dma_map(pcmd);
909 BUG_ON(nseg < 0);
910
911 if (nseg) {
Al Viro80da1ad2007-10-29 05:08:28 +0000912 __le32 length;
913 int i, cdb_sgcount = 0;
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900914 struct scatterlist *sg;
915
Erich Chen1c57e862006-07-12 08:59:32 -0700916 /* map stor port SG list to our iop SG List. */
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900917 scsi_for_each_sg(pcmd, sg, nseg, i) {
Erich Chen1c57e862006-07-12 08:59:32 -0700918 /* Get the physical address of the current data pointer */
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900919 length = cpu_to_le32(sg_dma_len(sg));
920 address_lo = cpu_to_le32(dma_addr_lo32(sg_dma_address(sg)));
921 address_hi = cpu_to_le32(dma_addr_hi32(sg_dma_address(sg)));
Erich Chen1c57e862006-07-12 08:59:32 -0700922 if (address_hi == 0) {
923 struct SG32ENTRY *pdma_sg = (struct SG32ENTRY *)psge;
924
925 pdma_sg->address = address_lo;
926 pdma_sg->length = length;
927 psge += sizeof (struct SG32ENTRY);
928 arccdbsize += sizeof (struct SG32ENTRY);
929 } else {
930 struct SG64ENTRY *pdma_sg = (struct SG64ENTRY *)psge;
931
932 pdma_sg->addresshigh = address_hi;
933 pdma_sg->address = address_lo;
Al Viro6a7d26d2007-10-29 05:08:48 +0000934 pdma_sg->length = length|cpu_to_le32(IS_SG64_ADDR);
Erich Chen1c57e862006-07-12 08:59:32 -0700935 psge += sizeof (struct SG64ENTRY);
936 arccdbsize += sizeof (struct SG64ENTRY);
937 }
Erich Chen1c57e862006-07-12 08:59:32 -0700938 cdb_sgcount++;
939 }
940 arcmsr_cdb->sgcount = (uint8_t)cdb_sgcount;
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900941 arcmsr_cdb->DataLength = scsi_bufflen(pcmd);
Erich Chen1c57e862006-07-12 08:59:32 -0700942 if ( arccdbsize > 256)
943 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_SGL_BSIZE;
Erich Chen1c57e862006-07-12 08:59:32 -0700944 }
945 if (pcmd->sc_data_direction == DMA_TO_DEVICE ) {
946 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_WRITE;
947 ccb->ccb_flags |= CCB_FLAG_WRITE;
948 }
949}
950
951static void arcmsr_post_ccb(struct AdapterControlBlock *acb, struct CommandControlBlock *ccb)
952{
Erich Chen1c57e862006-07-12 08:59:32 -0700953 uint32_t cdb_shifted_phyaddr = ccb->cdb_shifted_phyaddr;
954 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
Erich Chen1c57e862006-07-12 08:59:32 -0700955 atomic_inc(&acb->ccboutstandingcount);
956 ccb->startdone = ARCMSR_CCB_START;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800957
958 switch (acb->adapter_type) {
959 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +0000960 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800961
962 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE)
963 writel(cdb_shifted_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,
Erich Chen1c57e862006-07-12 08:59:32 -0700964 &reg->inbound_queueport);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800965 else {
966 writel(cdb_shifted_phyaddr, &reg->inbound_queueport);
967 }
968 }
969 break;
970
971 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +0000972 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800973 uint32_t ending_index, index = reg->postq_index;
974
975 ending_index = ((index + 1) % ARCMSR_MAX_HBB_POSTQUEUE);
976 writel(0, &reg->post_qbuffer[ending_index]);
977 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE) {
978 writel(cdb_shifted_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,\
979 &reg->post_qbuffer[index]);
980 }
981 else {
982 writel(cdb_shifted_phyaddr, &reg->post_qbuffer[index]);
983 }
984 index++;
985 index %= ARCMSR_MAX_HBB_POSTQUEUE;/*if last index number set it to 0 */
986 reg->postq_index = index;
987 writel(ARCMSR_DRV2IOP_CDB_POSTED, reg->drv2iop_doorbell_reg);
988 }
989 break;
990 }
Erich Chen1c57e862006-07-12 08:59:32 -0700991}
992
Nick Cheng1a4f5502007-09-13 17:26:40 +0800993static void arcmsr_stop_hba_bgrb(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -0700994{
Al Viro80da1ad2007-10-29 05:08:28 +0000995 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800996 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
997 writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, &reg->inbound_msgaddr0);
Erich Chen1c57e862006-07-12 08:59:32 -0700998
Nick Cheng1a4f5502007-09-13 17:26:40 +0800999 if (arcmsr_hba_wait_msgint_ready(acb)) {
1000 printk(KERN_NOTICE
1001 "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
1002 , acb->host->host_no);
1003 }
1004}
1005
1006static void arcmsr_stop_hbb_bgrb(struct AdapterControlBlock *acb)
1007{
Al Viro80da1ad2007-10-29 05:08:28 +00001008 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001009 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
1010 writel(ARCMSR_MESSAGE_STOP_BGRB, reg->drv2iop_doorbell_reg);
1011
1012 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1013 printk(KERN_NOTICE
1014 "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
1015 , acb->host->host_no);
Erich Chen1c57e862006-07-12 08:59:32 -07001016 }
1017}
1018
1019static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb)
1020{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001021 switch (acb->adapter_type) {
1022 case ACB_ADAPTER_TYPE_A: {
1023 arcmsr_stop_hba_bgrb(acb);
1024 }
1025 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001026
Nick Cheng1a4f5502007-09-13 17:26:40 +08001027 case ACB_ADAPTER_TYPE_B: {
1028 arcmsr_stop_hbb_bgrb(acb);
1029 }
1030 break;
1031 }
Erich Chen1c57e862006-07-12 08:59:32 -07001032}
1033
1034static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb)
1035{
Al Virodb3a91f2007-10-29 05:08:38 +00001036 switch (acb->adapter_type) {
1037 case ACB_ADAPTER_TYPE_A: {
1038 iounmap(acb->pmuA);
1039 break;
1040 }
1041 case ACB_ADAPTER_TYPE_B: {
1042 struct MessageUnit_B *reg = acb->pmuB;
1043 iounmap(reg->drv2iop_doorbell_reg - ARCMSR_DRV2IOP_DOORBELL);
1044 iounmap(reg->ioctl_wbuffer_reg - ARCMSR_IOCTL_WBUFFER);
1045 }
1046 }
Erich Chen1c57e862006-07-12 08:59:32 -07001047 dma_free_coherent(&acb->pdev->dev,
1048 ARCMSR_MAX_FREECCB_NUM * sizeof (struct CommandControlBlock) + 0x20,
1049 acb->dma_coherent,
1050 acb->dma_coherent_handle);
1051}
1052
Nick Cheng1a4f5502007-09-13 17:26:40 +08001053void arcmsr_iop_message_read(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07001054{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001055 switch (acb->adapter_type) {
1056 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001057 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001058 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, &reg->inbound_doorbell);
1059 }
1060 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001061
Nick Cheng1a4f5502007-09-13 17:26:40 +08001062 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001063 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001064 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell_reg);
1065 }
1066 break;
1067 }
1068}
1069
1070static void arcmsr_iop_message_wrote(struct AdapterControlBlock *acb)
1071{
1072 switch (acb->adapter_type) {
1073 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001074 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001075 /*
1076 ** push inbound doorbell tell iop, driver data write ok
1077 ** and wait reply on next hwinterrupt for next Qbuffer post
1078 */
1079 writel(ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK, &reg->inbound_doorbell);
1080 }
1081 break;
1082
1083 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001084 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001085 /*
1086 ** push inbound doorbell tell iop, driver data write ok
1087 ** and wait reply on next hwinterrupt for next Qbuffer post
1088 */
1089 writel(ARCMSR_DRV2IOP_DATA_WRITE_OK, reg->drv2iop_doorbell_reg);
1090 }
1091 break;
1092 }
1093}
1094
Al Viro80da1ad2007-10-29 05:08:28 +00001095struct QBUFFER __iomem *arcmsr_get_iop_rqbuffer(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08001096{
Al Viro0c7eb2e2007-10-29 05:08:58 +00001097 struct QBUFFER __iomem *qbuffer = NULL;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001098
1099 switch (acb->adapter_type) {
1100
1101 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001102 struct MessageUnit_A __iomem *reg = acb->pmuA;
1103 qbuffer = (struct QBUFFER __iomem *)&reg->message_rbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001104 }
1105 break;
1106
1107 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001108 struct MessageUnit_B *reg = acb->pmuB;
1109 qbuffer = (struct QBUFFER __iomem *)reg->ioctl_rbuffer_reg;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001110 }
1111 break;
1112 }
1113 return qbuffer;
1114}
1115
Al Viro80da1ad2007-10-29 05:08:28 +00001116static struct QBUFFER __iomem *arcmsr_get_iop_wqbuffer(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08001117{
Al Viro0c7eb2e2007-10-29 05:08:58 +00001118 struct QBUFFER __iomem *pqbuffer = NULL;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001119
1120 switch (acb->adapter_type) {
1121
1122 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001123 struct MessageUnit_A __iomem *reg = acb->pmuA;
1124 pqbuffer = (struct QBUFFER __iomem *) &reg->message_wbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001125 }
1126 break;
1127
1128 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001129 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001130 pqbuffer = (struct QBUFFER __iomem *)reg->ioctl_wbuffer_reg;
1131 }
1132 break;
1133 }
1134 return pqbuffer;
1135}
1136
1137static void arcmsr_iop2drv_data_wrote_handle(struct AdapterControlBlock *acb)
1138{
Al Viro80da1ad2007-10-29 05:08:28 +00001139 struct QBUFFER __iomem *prbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001140 struct QBUFFER *pQbuffer;
Al Viro80da1ad2007-10-29 05:08:28 +00001141 uint8_t __iomem *iop_data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001142 int32_t my_empty_len, iop_len, rqbuf_firstindex, rqbuf_lastindex;
1143
1144 rqbuf_lastindex = acb->rqbuf_lastindex;
1145 rqbuf_firstindex = acb->rqbuf_firstindex;
1146 prbuffer = arcmsr_get_iop_rqbuffer(acb);
Al Viro80da1ad2007-10-29 05:08:28 +00001147 iop_data = (uint8_t __iomem *)prbuffer->data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001148 iop_len = prbuffer->data_len;
1149 my_empty_len = (rqbuf_firstindex - rqbuf_lastindex -1)&(ARCMSR_MAX_QBUFFER -1);
1150
1151 if (my_empty_len >= iop_len)
1152 {
1153 while (iop_len > 0) {
1154 pQbuffer = (struct QBUFFER *)&acb->rqbuffer[rqbuf_lastindex];
1155 memcpy(pQbuffer, iop_data,1);
1156 rqbuf_lastindex++;
1157 rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1158 iop_data++;
1159 iop_len--;
1160 }
1161 acb->rqbuf_lastindex = rqbuf_lastindex;
1162 arcmsr_iop_message_read(acb);
1163 }
1164
1165 else {
1166 acb->acb_flags |= ACB_F_IOPDATA_OVERFLOW;
1167 }
1168}
1169
1170static void arcmsr_iop2drv_data_read_handle(struct AdapterControlBlock *acb)
1171{
1172 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_READED;
1173 if (acb->wqbuf_firstindex != acb->wqbuf_lastindex) {
1174 uint8_t *pQbuffer;
Al Viro80da1ad2007-10-29 05:08:28 +00001175 struct QBUFFER __iomem *pwbuffer;
1176 uint8_t __iomem *iop_data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001177 int32_t allxfer_len = 0;
1178
1179 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
1180 pwbuffer = arcmsr_get_iop_wqbuffer(acb);
1181 iop_data = (uint8_t __iomem *)pwbuffer->data;
1182
1183 while ((acb->wqbuf_firstindex != acb->wqbuf_lastindex) && \
1184 (allxfer_len < 124)) {
1185 pQbuffer = &acb->wqbuffer[acb->wqbuf_firstindex];
1186 memcpy(iop_data, pQbuffer, 1);
1187 acb->wqbuf_firstindex++;
1188 acb->wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1189 iop_data++;
1190 allxfer_len++;
1191 }
1192 pwbuffer->data_len = allxfer_len;
1193
1194 arcmsr_iop_message_wrote(acb);
1195 }
1196
1197 if (acb->wqbuf_firstindex == acb->wqbuf_lastindex) {
1198 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_CLEARED;
1199 }
1200}
1201
1202static void arcmsr_hba_doorbell_isr(struct AdapterControlBlock *acb)
1203{
1204 uint32_t outbound_doorbell;
Al Viro80da1ad2007-10-29 05:08:28 +00001205 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001206
1207 outbound_doorbell = readl(&reg->outbound_doorbell);
1208 writel(outbound_doorbell, &reg->outbound_doorbell);
1209 if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK) {
1210 arcmsr_iop2drv_data_wrote_handle(acb);
1211 }
1212
1213 if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_READ_OK) {
1214 arcmsr_iop2drv_data_read_handle(acb);
1215 }
1216}
1217
1218static void arcmsr_hba_postqueue_isr(struct AdapterControlBlock *acb)
1219{
1220 uint32_t flag_ccb;
Al Viro80da1ad2007-10-29 05:08:28 +00001221 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001222
1223 while ((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF) {
1224 arcmsr_drain_donequeue(acb, flag_ccb);
1225 }
1226}
1227
1228static void arcmsr_hbb_postqueue_isr(struct AdapterControlBlock *acb)
1229{
1230 uint32_t index;
1231 uint32_t flag_ccb;
Al Viro80da1ad2007-10-29 05:08:28 +00001232 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001233
1234 index = reg->doneq_index;
1235
1236 while ((flag_ccb = readl(&reg->done_qbuffer[index])) != 0) {
1237 writel(0, &reg->done_qbuffer[index]);
1238 arcmsr_drain_donequeue(acb, flag_ccb);
1239 index++;
1240 index %= ARCMSR_MAX_HBB_POSTQUEUE;
1241 reg->doneq_index = index;
1242 }
1243}
1244
1245static int arcmsr_handle_hba_isr(struct AdapterControlBlock *acb)
1246{
1247 uint32_t outbound_intstatus;
Al Viro80da1ad2007-10-29 05:08:28 +00001248 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001249
1250 outbound_intstatus = readl(&reg->outbound_intstatus) & \
1251 acb->outbound_int_enable;
1252 if (!(outbound_intstatus & ARCMSR_MU_OUTBOUND_HANDLE_INT)) {
1253 return 1;
1254 }
Erich Chen1c57e862006-07-12 08:59:32 -07001255 writel(outbound_intstatus, &reg->outbound_intstatus);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001256 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_DOORBELL_INT) {
1257 arcmsr_hba_doorbell_isr(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001258 }
1259 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_POSTQUEUE_INT) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001260 arcmsr_hba_postqueue_isr(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001261 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001262 return 0;
1263}
1264
1265static int arcmsr_handle_hbb_isr(struct AdapterControlBlock *acb)
1266{
1267 uint32_t outbound_doorbell;
Al Viro80da1ad2007-10-29 05:08:28 +00001268 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001269
1270 outbound_doorbell = readl(reg->iop2drv_doorbell_reg) & \
1271 acb->outbound_int_enable;
1272 if (!outbound_doorbell)
1273 return 1;
1274
1275 writel(~outbound_doorbell, reg->iop2drv_doorbell_reg);
1276
1277 if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_WRITE_OK) {
1278 arcmsr_iop2drv_data_wrote_handle(acb);
1279 }
1280 if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_READ_OK) {
1281 arcmsr_iop2drv_data_read_handle(acb);
1282 }
1283 if (outbound_doorbell & ARCMSR_IOP2DRV_CDB_DONE) {
1284 arcmsr_hbb_postqueue_isr(acb);
1285 }
1286
1287 return 0;
1288}
1289
1290static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb)
1291{
1292 switch (acb->adapter_type) {
1293 case ACB_ADAPTER_TYPE_A: {
1294 if (arcmsr_handle_hba_isr(acb)) {
1295 return IRQ_NONE;
1296 }
1297 }
1298 break;
1299
1300 case ACB_ADAPTER_TYPE_B: {
1301 if (arcmsr_handle_hbb_isr(acb)) {
1302 return IRQ_NONE;
1303 }
1304 }
1305 break;
1306 }
Erich Chen1c57e862006-07-12 08:59:32 -07001307 return IRQ_HANDLED;
1308}
1309
1310static void arcmsr_iop_parking(struct AdapterControlBlock *acb)
1311{
1312 if (acb) {
1313 /* stop adapter background rebuild */
1314 if (acb->acb_flags & ACB_F_MSG_START_BGRB) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001315 uint32_t intmask_org;
Erich Chen1c57e862006-07-12 08:59:32 -07001316 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001317 intmask_org = arcmsr_disable_outbound_ints(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001318 arcmsr_stop_adapter_bgrb(acb);
1319 arcmsr_flush_adapter_cache(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001320 arcmsr_enable_outbound_ints(acb, intmask_org);
Erich Chen1c57e862006-07-12 08:59:32 -07001321 }
1322 }
1323}
1324
Nick Cheng1a4f5502007-09-13 17:26:40 +08001325void arcmsr_post_ioctldata2iop(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07001326{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001327 int32_t wqbuf_firstindex, wqbuf_lastindex;
1328 uint8_t *pQbuffer;
Al Viro80da1ad2007-10-29 05:08:28 +00001329 struct QBUFFER __iomem *pwbuffer;
1330 uint8_t __iomem *iop_data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001331 int32_t allxfer_len = 0;
1332
1333 pwbuffer = arcmsr_get_iop_wqbuffer(acb);
1334 iop_data = (uint8_t __iomem *)pwbuffer->data;
1335 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_READED) {
1336 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
1337 wqbuf_firstindex = acb->wqbuf_firstindex;
1338 wqbuf_lastindex = acb->wqbuf_lastindex;
1339 while ((wqbuf_firstindex != wqbuf_lastindex) && (allxfer_len < 124)) {
1340 pQbuffer = &acb->wqbuffer[wqbuf_firstindex];
1341 memcpy(iop_data, pQbuffer, 1);
1342 wqbuf_firstindex++;
1343 wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1344 iop_data++;
1345 allxfer_len++;
1346 }
1347 acb->wqbuf_firstindex = wqbuf_firstindex;
1348 pwbuffer->data_len = allxfer_len;
1349 arcmsr_iop_message_wrote(acb);
1350 }
1351}
1352
1353static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb, \
1354 struct scsi_cmnd *cmd)
1355{
Erich Chen1c57e862006-07-12 08:59:32 -07001356 struct CMD_MESSAGE_FIELD *pcmdmessagefld;
1357 int retvalue = 0, transfer_len = 0;
1358 char *buffer;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001359 struct scatterlist *sg;
Erich Chen1c57e862006-07-12 08:59:32 -07001360 uint32_t controlcode = (uint32_t ) cmd->cmnd[5] << 24 |
1361 (uint32_t ) cmd->cmnd[6] << 16 |
1362 (uint32_t ) cmd->cmnd[7] << 8 |
1363 (uint32_t ) cmd->cmnd[8];
Nick Cheng1a4f5502007-09-13 17:26:40 +08001364 /* 4 bytes: Areca io control code */
Erich Chen1c57e862006-07-12 08:59:32 -07001365
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001366 sg = scsi_sglist(cmd);
Jens Axboe45711f12007-10-22 21:19:53 +02001367 buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001368 if (scsi_sg_count(cmd) > 1) {
1369 retvalue = ARCMSR_MESSAGE_FAIL;
1370 goto message_out;
Erich Chen1c57e862006-07-12 08:59:32 -07001371 }
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001372 transfer_len += sg->length;
1373
Erich Chen1c57e862006-07-12 08:59:32 -07001374 if (transfer_len > sizeof(struct CMD_MESSAGE_FIELD)) {
1375 retvalue = ARCMSR_MESSAGE_FAIL;
1376 goto message_out;
1377 }
1378 pcmdmessagefld = (struct CMD_MESSAGE_FIELD *) buffer;
1379 switch(controlcode) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001380
Erich Chen1c57e862006-07-12 08:59:32 -07001381 case ARCMSR_MESSAGE_READ_RQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001382 unsigned long *ver_addr;
1383 dma_addr_t buf_handle;
1384 uint8_t *pQbuffer, *ptmpQbuffer;
1385 int32_t allxfer_len = 0;
Erich Chen1c57e862006-07-12 08:59:32 -07001386
Nick Cheng1a4f5502007-09-13 17:26:40 +08001387 ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
1388 if (!ver_addr) {
1389 retvalue = ARCMSR_MESSAGE_FAIL;
1390 goto message_out;
1391 }
1392 ptmpQbuffer = (uint8_t *) ver_addr;
1393 while ((acb->rqbuf_firstindex != acb->rqbuf_lastindex)
1394 && (allxfer_len < 1031)) {
1395 pQbuffer = &acb->rqbuffer[acb->rqbuf_firstindex];
1396 memcpy(ptmpQbuffer, pQbuffer, 1);
1397 acb->rqbuf_firstindex++;
1398 acb->rqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1399 ptmpQbuffer++;
1400 allxfer_len++;
1401 }
1402 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
Erich Chen1c57e862006-07-12 08:59:32 -07001403
Al Viro80da1ad2007-10-29 05:08:28 +00001404 struct QBUFFER __iomem *prbuffer;
1405 uint8_t __iomem *iop_data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001406 int32_t iop_len;
1407
1408 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1409 prbuffer = arcmsr_get_iop_rqbuffer(acb);
Al Viro80da1ad2007-10-29 05:08:28 +00001410 iop_data = prbuffer->data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001411 iop_len = readl(&prbuffer->data_len);
1412 while (iop_len > 0) {
1413 acb->rqbuffer[acb->rqbuf_lastindex] = readb(iop_data);
1414 acb->rqbuf_lastindex++;
1415 acb->rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1416 iop_data++;
1417 iop_len--;
Erich Chen1c57e862006-07-12 08:59:32 -07001418 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001419 arcmsr_iop_message_read(acb);
1420 }
1421 memcpy(pcmdmessagefld->messagedatabuffer, (uint8_t *)ver_addr, allxfer_len);
1422 pcmdmessagefld->cmdmessage.Length = allxfer_len;
1423 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1424 pci_free_consistent(acb->pdev, 1032, ver_addr, buf_handle);
Erich Chen1c57e862006-07-12 08:59:32 -07001425 }
1426 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001427
Nick Cheng1a4f5502007-09-13 17:26:40 +08001428 case ARCMSR_MESSAGE_WRITE_WQBUFFER: {
1429 unsigned long *ver_addr;
1430 dma_addr_t buf_handle;
1431 int32_t my_empty_len, user_len, wqbuf_firstindex, wqbuf_lastindex;
1432 uint8_t *pQbuffer, *ptmpuserbuffer;
1433
1434 ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
1435 if (!ver_addr) {
1436 retvalue = ARCMSR_MESSAGE_FAIL;
1437 goto message_out;
1438 }
1439 ptmpuserbuffer = (uint8_t *)ver_addr;
1440 user_len = pcmdmessagefld->cmdmessage.Length;
1441 memcpy(ptmpuserbuffer, pcmdmessagefld->messagedatabuffer, user_len);
1442 wqbuf_lastindex = acb->wqbuf_lastindex;
1443 wqbuf_firstindex = acb->wqbuf_firstindex;
1444 if (wqbuf_lastindex != wqbuf_firstindex) {
1445 struct SENSE_DATA *sensebuffer =
1446 (struct SENSE_DATA *)cmd->sense_buffer;
1447 arcmsr_post_ioctldata2iop(acb);
1448 /* has error report sensedata */
1449 sensebuffer->ErrorCode = 0x70;
1450 sensebuffer->SenseKey = ILLEGAL_REQUEST;
1451 sensebuffer->AdditionalSenseLength = 0x0A;
1452 sensebuffer->AdditionalSenseCode = 0x20;
1453 sensebuffer->Valid = 1;
1454 retvalue = ARCMSR_MESSAGE_FAIL;
1455 } else {
1456 my_empty_len = (wqbuf_firstindex-wqbuf_lastindex - 1)
1457 &(ARCMSR_MAX_QBUFFER - 1);
1458 if (my_empty_len >= user_len) {
1459 while (user_len > 0) {
1460 pQbuffer =
1461 &acb->wqbuffer[acb->wqbuf_lastindex];
1462 memcpy(pQbuffer, ptmpuserbuffer, 1);
1463 acb->wqbuf_lastindex++;
1464 acb->wqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1465 ptmpuserbuffer++;
1466 user_len--;
1467 }
1468 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {
1469 acb->acb_flags &=
1470 ~ACB_F_MESSAGE_WQBUFFER_CLEARED;
1471 arcmsr_post_ioctldata2iop(acb);
1472 }
1473 } else {
1474 /* has error report sensedata */
Erich Chen1c57e862006-07-12 08:59:32 -07001475 struct SENSE_DATA *sensebuffer =
1476 (struct SENSE_DATA *)cmd->sense_buffer;
Erich Chen1c57e862006-07-12 08:59:32 -07001477 sensebuffer->ErrorCode = 0x70;
1478 sensebuffer->SenseKey = ILLEGAL_REQUEST;
1479 sensebuffer->AdditionalSenseLength = 0x0A;
1480 sensebuffer->AdditionalSenseCode = 0x20;
1481 sensebuffer->Valid = 1;
1482 retvalue = ARCMSR_MESSAGE_FAIL;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001483 }
Erich Chen1c57e862006-07-12 08:59:32 -07001484 }
1485 pci_free_consistent(acb->pdev, 1032, ver_addr, buf_handle);
1486 }
1487 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001488
Erich Chen1c57e862006-07-12 08:59:32 -07001489 case ARCMSR_MESSAGE_CLEAR_RQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001490 uint8_t *pQbuffer = acb->rqbuffer;
Erich Chen1c57e862006-07-12 08:59:32 -07001491
Nick Cheng1a4f5502007-09-13 17:26:40 +08001492 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1493 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1494 arcmsr_iop_message_read(acb);
1495 }
1496 acb->acb_flags |= ACB_F_MESSAGE_RQBUFFER_CLEARED;
1497 acb->rqbuf_firstindex = 0;
1498 acb->rqbuf_lastindex = 0;
1499 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1500 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07001501 }
1502 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001503
Erich Chen1c57e862006-07-12 08:59:32 -07001504 case ARCMSR_MESSAGE_CLEAR_WQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001505 uint8_t *pQbuffer = acb->wqbuffer;
Erich Chen1c57e862006-07-12 08:59:32 -07001506
Nick Cheng1a4f5502007-09-13 17:26:40 +08001507 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1508 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1509 arcmsr_iop_message_read(acb);
1510 }
1511 acb->acb_flags |=
1512 (ACB_F_MESSAGE_WQBUFFER_CLEARED |
1513 ACB_F_MESSAGE_WQBUFFER_READED);
1514 acb->wqbuf_firstindex = 0;
1515 acb->wqbuf_lastindex = 0;
1516 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1517 pcmdmessagefld->cmdmessage.ReturnCode =
1518 ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07001519 }
1520 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001521
Erich Chen1c57e862006-07-12 08:59:32 -07001522 case ARCMSR_MESSAGE_CLEAR_ALLQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001523 uint8_t *pQbuffer;
Erich Chen1c57e862006-07-12 08:59:32 -07001524
Nick Cheng1a4f5502007-09-13 17:26:40 +08001525 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1526 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1527 arcmsr_iop_message_read(acb);
1528 }
1529 acb->acb_flags |=
1530 (ACB_F_MESSAGE_WQBUFFER_CLEARED
1531 | ACB_F_MESSAGE_RQBUFFER_CLEARED
1532 | ACB_F_MESSAGE_WQBUFFER_READED);
1533 acb->rqbuf_firstindex = 0;
1534 acb->rqbuf_lastindex = 0;
1535 acb->wqbuf_firstindex = 0;
1536 acb->wqbuf_lastindex = 0;
1537 pQbuffer = acb->rqbuffer;
1538 memset(pQbuffer, 0, sizeof(struct QBUFFER));
1539 pQbuffer = acb->wqbuffer;
1540 memset(pQbuffer, 0, sizeof(struct QBUFFER));
1541 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07001542 }
1543 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001544
Erich Chen1c57e862006-07-12 08:59:32 -07001545 case ARCMSR_MESSAGE_RETURN_CODE_3F: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001546 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_3F;
Erich Chen1c57e862006-07-12 08:59:32 -07001547 }
1548 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001549
Nick Cheng1a4f5502007-09-13 17:26:40 +08001550 case ARCMSR_MESSAGE_SAY_HELLO: {
1551 int8_t *hello_string = "Hello! I am ARCMSR";
1552
1553 memcpy(pcmdmessagefld->messagedatabuffer, hello_string
1554 , (int16_t)strlen(hello_string));
1555 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07001556 }
1557 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001558
Erich Chen1c57e862006-07-12 08:59:32 -07001559 case ARCMSR_MESSAGE_SAY_GOODBYE:
1560 arcmsr_iop_parking(acb);
1561 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001562
Erich Chen1c57e862006-07-12 08:59:32 -07001563 case ARCMSR_MESSAGE_FLUSH_ADAPTER_CACHE:
1564 arcmsr_flush_adapter_cache(acb);
1565 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001566
Erich Chen1c57e862006-07-12 08:59:32 -07001567 default:
1568 retvalue = ARCMSR_MESSAGE_FAIL;
1569 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001570 message_out:
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001571 sg = scsi_sglist(cmd);
1572 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
Erich Chen1c57e862006-07-12 08:59:32 -07001573 return retvalue;
1574}
1575
1576static struct CommandControlBlock *arcmsr_get_freeccb(struct AdapterControlBlock *acb)
1577{
1578 struct list_head *head = &acb->ccb_free_list;
1579 struct CommandControlBlock *ccb = NULL;
1580
1581 if (!list_empty(head)) {
1582 ccb = list_entry(head->next, struct CommandControlBlock, list);
1583 list_del(head->next);
1584 }
1585 return ccb;
1586}
1587
1588static void arcmsr_handle_virtual_command(struct AdapterControlBlock *acb,
1589 struct scsi_cmnd *cmd)
1590{
1591 switch (cmd->cmnd[0]) {
1592 case INQUIRY: {
1593 unsigned char inqdata[36];
1594 char *buffer;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001595 struct scatterlist *sg;
Erich Chen1c57e862006-07-12 08:59:32 -07001596
1597 if (cmd->device->lun) {
1598 cmd->result = (DID_TIME_OUT << 16);
1599 cmd->scsi_done(cmd);
1600 return;
1601 }
1602 inqdata[0] = TYPE_PROCESSOR;
1603 /* Periph Qualifier & Periph Dev Type */
1604 inqdata[1] = 0;
1605 /* rem media bit & Dev Type Modifier */
1606 inqdata[2] = 0;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001607 /* ISO, ECMA, & ANSI versions */
Erich Chen1c57e862006-07-12 08:59:32 -07001608 inqdata[4] = 31;
1609 /* length of additional data */
1610 strncpy(&inqdata[8], "Areca ", 8);
1611 /* Vendor Identification */
1612 strncpy(&inqdata[16], "RAID controller ", 16);
1613 /* Product Identification */
1614 strncpy(&inqdata[32], "R001", 4); /* Product Revision */
Erich Chen1c57e862006-07-12 08:59:32 -07001615
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001616 sg = scsi_sglist(cmd);
Jens Axboe45711f12007-10-22 21:19:53 +02001617 buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001618
Erich Chen1c57e862006-07-12 08:59:32 -07001619 memcpy(buffer, inqdata, sizeof(inqdata));
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001620 sg = scsi_sglist(cmd);
1621 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
Erich Chen1c57e862006-07-12 08:59:32 -07001622
Erich Chen1c57e862006-07-12 08:59:32 -07001623 cmd->scsi_done(cmd);
1624 }
1625 break;
1626 case WRITE_BUFFER:
1627 case READ_BUFFER: {
1628 if (arcmsr_iop_message_xfer(acb, cmd))
1629 cmd->result = (DID_ERROR << 16);
1630 cmd->scsi_done(cmd);
1631 }
1632 break;
1633 default:
1634 cmd->scsi_done(cmd);
1635 }
1636}
1637
1638static int arcmsr_queue_command(struct scsi_cmnd *cmd,
1639 void (* done)(struct scsi_cmnd *))
1640{
1641 struct Scsi_Host *host = cmd->device->host;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001642 struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -07001643 struct CommandControlBlock *ccb;
1644 int target = cmd->device->id;
1645 int lun = cmd->device->lun;
1646
1647 cmd->scsi_done = done;
1648 cmd->host_scribble = NULL;
1649 cmd->result = 0;
1650 if (acb->acb_flags & ACB_F_BUS_RESET) {
1651 printk(KERN_NOTICE "arcmsr%d: bus reset"
1652 " and return busy \n"
1653 , acb->host->host_no);
1654 return SCSI_MLQUEUE_HOST_BUSY;
1655 }
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001656 if (target == 16) {
Erich Chen1c57e862006-07-12 08:59:32 -07001657 /* virtual device for iop message transfer */
1658 arcmsr_handle_virtual_command(acb, cmd);
1659 return 0;
1660 }
1661 if (acb->devstate[target][lun] == ARECA_RAID_GONE) {
1662 uint8_t block_cmd;
1663
1664 block_cmd = cmd->cmnd[0] & 0x0f;
1665 if (block_cmd == 0x08 || block_cmd == 0x0a) {
1666 printk(KERN_NOTICE
1667 "arcmsr%d: block 'read/write'"
1668 "command with gone raid volume"
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001669 " Cmd = %2x, TargetId = %d, Lun = %d \n"
Erich Chen1c57e862006-07-12 08:59:32 -07001670 , acb->host->host_no
1671 , cmd->cmnd[0]
1672 , target, lun);
1673 cmd->result = (DID_NO_CONNECT << 16);
1674 cmd->scsi_done(cmd);
1675 return 0;
1676 }
1677 }
1678 if (atomic_read(&acb->ccboutstandingcount) >=
1679 ARCMSR_MAX_OUTSTANDING_CMD)
1680 return SCSI_MLQUEUE_HOST_BUSY;
1681
1682 ccb = arcmsr_get_freeccb(acb);
1683 if (!ccb)
1684 return SCSI_MLQUEUE_HOST_BUSY;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001685
Erich Chen1c57e862006-07-12 08:59:32 -07001686 arcmsr_build_ccb(acb, ccb, cmd);
1687 arcmsr_post_ccb(acb, ccb);
1688 return 0;
1689}
1690
Nick Cheng1a4f5502007-09-13 17:26:40 +08001691static void arcmsr_get_hba_config(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07001692{
Al Viro80da1ad2007-10-29 05:08:28 +00001693 struct MessageUnit_A __iomem *reg = acb->pmuA;
Erich Chen1c57e862006-07-12 08:59:32 -07001694 char *acb_firm_model = acb->firm_model;
1695 char *acb_firm_version = acb->firm_version;
Al Viro80da1ad2007-10-29 05:08:28 +00001696 char __iomem *iop_firm_model = (char __iomem *)(&reg->message_rwbuffer[15]);
1697 char __iomem *iop_firm_version = (char __iomem *)(&reg->message_rwbuffer[17]);
Erich Chen1c57e862006-07-12 08:59:32 -07001698 int count;
1699
1700 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001701 if (arcmsr_hba_wait_msgint_ready(acb)) {
1702 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
1703 miscellaneous data' timeout \n", acb->host->host_no);
1704 }
1705
Erich Chen1c57e862006-07-12 08:59:32 -07001706 count = 8;
1707 while (count) {
1708 *acb_firm_model = readb(iop_firm_model);
1709 acb_firm_model++;
1710 iop_firm_model++;
1711 count--;
1712 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001713
Erich Chen1c57e862006-07-12 08:59:32 -07001714 count = 16;
1715 while (count) {
1716 *acb_firm_version = readb(iop_firm_version);
1717 acb_firm_version++;
1718 iop_firm_version++;
1719 count--;
1720 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001721
1722 printk(KERN_INFO "ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n"
Erich Chen1c57e862006-07-12 08:59:32 -07001723 , acb->host->host_no
1724 , acb->firm_version);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001725
Erich Chen1c57e862006-07-12 08:59:32 -07001726 acb->firm_request_len = readl(&reg->message_rwbuffer[1]);
1727 acb->firm_numbers_queue = readl(&reg->message_rwbuffer[2]);
1728 acb->firm_sdram_size = readl(&reg->message_rwbuffer[3]);
1729 acb->firm_hd_channels = readl(&reg->message_rwbuffer[4]);
1730}
1731
Nick Cheng1a4f5502007-09-13 17:26:40 +08001732static void arcmsr_get_hbb_config(struct AdapterControlBlock *acb)
1733{
Al Viro80da1ad2007-10-29 05:08:28 +00001734 struct MessageUnit_B *reg = acb->pmuB;
1735 uint32_t __iomem *lrwbuffer = reg->msgcode_rwbuffer_reg;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001736 char *acb_firm_model = acb->firm_model;
1737 char *acb_firm_version = acb->firm_version;
Al Viro80da1ad2007-10-29 05:08:28 +00001738 char __iomem *iop_firm_model = (char __iomem *)(&lrwbuffer[15]);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001739 /*firm_model,15,60-67*/
Al Viro80da1ad2007-10-29 05:08:28 +00001740 char __iomem *iop_firm_version = (char __iomem *)(&lrwbuffer[17]);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001741 /*firm_version,17,68-83*/
1742 int count;
1743
1744 writel(ARCMSR_MESSAGE_GET_CONFIG, reg->drv2iop_doorbell_reg);
1745 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1746 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
1747 miscellaneous data' timeout \n", acb->host->host_no);
1748 }
1749
1750 count = 8;
1751 while (count)
1752 {
1753 *acb_firm_model = readb(iop_firm_model);
1754 acb_firm_model++;
1755 iop_firm_model++;
1756 count--;
1757 }
1758
1759 count = 16;
1760 while (count)
1761 {
1762 *acb_firm_version = readb(iop_firm_version);
1763 acb_firm_version++;
1764 iop_firm_version++;
1765 count--;
1766 }
1767
1768 printk(KERN_INFO "ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n",
1769 acb->host->host_no,
1770 acb->firm_version);
1771
1772 lrwbuffer++;
1773 acb->firm_request_len = readl(lrwbuffer++);
1774 /*firm_request_len,1,04-07*/
1775 acb->firm_numbers_queue = readl(lrwbuffer++);
1776 /*firm_numbers_queue,2,08-11*/
1777 acb->firm_sdram_size = readl(lrwbuffer++);
1778 /*firm_sdram_size,3,12-15*/
1779 acb->firm_hd_channels = readl(lrwbuffer);
1780 /*firm_ide_channels,4,16-19*/
1781}
1782
1783static void arcmsr_get_firmware_spec(struct AdapterControlBlock *acb)
1784{
1785 switch (acb->adapter_type) {
1786 case ACB_ADAPTER_TYPE_A: {
1787 arcmsr_get_hba_config(acb);
1788 }
1789 break;
1790
1791 case ACB_ADAPTER_TYPE_B: {
1792 arcmsr_get_hbb_config(acb);
1793 }
1794 break;
1795 }
1796}
1797
1798static void arcmsr_polling_hba_ccbdone(struct AdapterControlBlock *acb,
Erich Chen1c57e862006-07-12 08:59:32 -07001799 struct CommandControlBlock *poll_ccb)
1800{
Al Viro80da1ad2007-10-29 05:08:28 +00001801 struct MessageUnit_A __iomem *reg = acb->pmuA;
Erich Chen1c57e862006-07-12 08:59:32 -07001802 struct CommandControlBlock *ccb;
1803 uint32_t flag_ccb, outbound_intstatus, poll_ccb_done = 0, poll_count = 0;
Erich Chen1c57e862006-07-12 08:59:32 -07001804
Nick Cheng1a4f5502007-09-13 17:26:40 +08001805 polling_hba_ccb_retry:
Erich Chen1c57e862006-07-12 08:59:32 -07001806 poll_count++;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001807 outbound_intstatus = readl(&reg->outbound_intstatus) & acb->outbound_int_enable;
Erich Chen1c57e862006-07-12 08:59:32 -07001808 writel(outbound_intstatus, &reg->outbound_intstatus);/*clear interrupt*/
1809 while (1) {
1810 if ((flag_ccb = readl(&reg->outbound_queueport)) == 0xFFFFFFFF) {
1811 if (poll_ccb_done)
1812 break;
1813 else {
1814 msleep(25);
1815 if (poll_count > 100)
1816 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001817 goto polling_hba_ccb_retry;
Erich Chen1c57e862006-07-12 08:59:32 -07001818 }
1819 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001820 ccb = (struct CommandControlBlock *)(acb->vir2phy_offset + (flag_ccb << 5));
1821 poll_ccb_done = (ccb == poll_ccb) ? 1:0;
1822 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
1823 if ((ccb->startdone == ARCMSR_CCB_ABORTED) || (ccb == poll_ccb)) {
1824 printk(KERN_NOTICE "arcmsr%d: scsi id = %d lun = %d ccb = '0x%p'"
Erich Chen1c57e862006-07-12 08:59:32 -07001825 " poll command abort successfully \n"
1826 , acb->host->host_no
1827 , ccb->pcmd->device->id
1828 , ccb->pcmd->device->lun
1829 , ccb);
1830 ccb->pcmd->result = DID_ABORT << 16;
1831 arcmsr_ccb_complete(ccb, 1);
1832 poll_ccb_done = 1;
1833 continue;
1834 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001835 printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
1836 " command done ccb = '0x%p'"
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001837 "ccboutstandingcount = %d \n"
Erich Chen1c57e862006-07-12 08:59:32 -07001838 , acb->host->host_no
1839 , ccb
1840 , atomic_read(&acb->ccboutstandingcount));
1841 continue;
1842 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001843 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
1844 }
1845}
1846
1847static void arcmsr_polling_hbb_ccbdone(struct AdapterControlBlock *acb, \
1848 struct CommandControlBlock *poll_ccb)
1849{
Al Viro80da1ad2007-10-29 05:08:28 +00001850 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001851 struct CommandControlBlock *ccb;
1852 uint32_t flag_ccb, poll_ccb_done = 0, poll_count = 0;
1853 int index;
1854
1855 polling_hbb_ccb_retry:
1856 poll_count++;
1857 /* clear doorbell interrupt */
1858 writel(ARCMSR_DOORBELL_INT_CLEAR_PATTERN, reg->iop2drv_doorbell_reg);
1859 while (1) {
1860 index = reg->doneq_index;
1861 if ((flag_ccb = readl(&reg->done_qbuffer[index])) == 0) {
1862 if (poll_ccb_done)
1863 break;
1864 else {
1865 msleep(25);
1866 if (poll_count > 100)
1867 break;
1868 goto polling_hbb_ccb_retry;
Erich Chen1c57e862006-07-12 08:59:32 -07001869 }
Erich Chen1c57e862006-07-12 08:59:32 -07001870 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001871 writel(0, &reg->done_qbuffer[index]);
1872 index++;
1873 /*if last index number set it to 0 */
1874 index %= ARCMSR_MAX_HBB_POSTQUEUE;
1875 reg->doneq_index = index;
1876 /* check ifcommand done with no error*/
1877 ccb = (struct CommandControlBlock *)\
1878 (acb->vir2phy_offset + (flag_ccb << 5));/*frame must be 32 bytes aligned*/
1879 poll_ccb_done = (ccb == poll_ccb) ? 1:0;
1880 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
1881 if (ccb->startdone == ARCMSR_CCB_ABORTED) {
1882 printk(KERN_NOTICE "arcmsr%d: \
1883 scsi id = %d lun = %d ccb = '0x%p' poll command abort successfully \n"
1884 ,acb->host->host_no
1885 ,ccb->pcmd->device->id
1886 ,ccb->pcmd->device->lun
1887 ,ccb);
1888 ccb->pcmd->result = DID_ABORT << 16;
1889 arcmsr_ccb_complete(ccb, 1);
1890 continue;
1891 }
1892 printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
1893 " command done ccb = '0x%p'"
1894 "ccboutstandingcount = %d \n"
1895 , acb->host->host_no
1896 , ccb
1897 , atomic_read(&acb->ccboutstandingcount));
1898 continue;
1899 }
1900 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
1901 } /*drain reply FIFO*/
1902}
1903
1904static void arcmsr_polling_ccbdone(struct AdapterControlBlock *acb, \
1905 struct CommandControlBlock *poll_ccb)
1906{
1907 switch (acb->adapter_type) {
1908
1909 case ACB_ADAPTER_TYPE_A: {
1910 arcmsr_polling_hba_ccbdone(acb,poll_ccb);
1911 }
1912 break;
1913
1914 case ACB_ADAPTER_TYPE_B: {
1915 arcmsr_polling_hbb_ccbdone(acb,poll_ccb);
Erich Chen1c57e862006-07-12 08:59:32 -07001916 }
1917 }
1918}
Nick Cheng1a4f5502007-09-13 17:26:40 +08001919
1920static int arcmsr_iop_confirm(struct AdapterControlBlock *acb)
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001921{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001922 uint32_t cdb_phyaddr, ccb_phyaddr_hi32;
1923 dma_addr_t dma_coherent_handle;
1924 /*
1925 ********************************************************************
1926 ** here we need to tell iop 331 our freeccb.HighPart
1927 ** if freeccb.HighPart is not zero
1928 ********************************************************************
1929 */
1930 dma_coherent_handle = acb->dma_coherent_handle;
1931 cdb_phyaddr = (uint32_t)(dma_coherent_handle);
1932 ccb_phyaddr_hi32 = (uint32_t)((cdb_phyaddr >> 16) >> 16);
1933 /*
1934 ***********************************************************************
1935 ** if adapter type B, set window of "post command Q"
1936 ***********************************************************************
1937 */
1938 switch (acb->adapter_type) {
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001939
Nick Cheng1a4f5502007-09-13 17:26:40 +08001940 case ACB_ADAPTER_TYPE_A: {
1941 if (ccb_phyaddr_hi32 != 0) {
Al Viro80da1ad2007-10-29 05:08:28 +00001942 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001943 uint32_t intmask_org;
1944 intmask_org = arcmsr_disable_outbound_ints(acb);
1945 writel(ARCMSR_SIGNATURE_SET_CONFIG, \
1946 &reg->message_rwbuffer[0]);
1947 writel(ccb_phyaddr_hi32, &reg->message_rwbuffer[1]);
1948 writel(ARCMSR_INBOUND_MESG0_SET_CONFIG, \
1949 &reg->inbound_msgaddr0);
1950 if (arcmsr_hba_wait_msgint_ready(acb)) {
1951 printk(KERN_NOTICE "arcmsr%d: ""set ccb high \
1952 part physical address timeout\n",
1953 acb->host->host_no);
1954 return 1;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001955 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001956 arcmsr_enable_outbound_ints(acb, intmask_org);
1957 }
1958 }
1959 break;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001960
Nick Cheng1a4f5502007-09-13 17:26:40 +08001961 case ACB_ADAPTER_TYPE_B: {
1962 unsigned long post_queue_phyaddr;
Al Viro80da1ad2007-10-29 05:08:28 +00001963 uint32_t __iomem *rwbuffer;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001964
Al Viro80da1ad2007-10-29 05:08:28 +00001965 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001966 uint32_t intmask_org;
1967 intmask_org = arcmsr_disable_outbound_ints(acb);
1968 reg->postq_index = 0;
1969 reg->doneq_index = 0;
1970 writel(ARCMSR_MESSAGE_SET_POST_WINDOW, reg->drv2iop_doorbell_reg);
1971 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1972 printk(KERN_NOTICE "arcmsr%d:can not set diver mode\n", \
1973 acb->host->host_no);
1974 return 1;
1975 }
1976 post_queue_phyaddr = cdb_phyaddr + ARCMSR_MAX_FREECCB_NUM * \
1977 sizeof(struct CommandControlBlock) + offsetof(struct MessageUnit_B, post_qbuffer) ;
1978 rwbuffer = reg->msgcode_rwbuffer_reg;
1979 /* driver "set config" signature */
1980 writel(ARCMSR_SIGNATURE_SET_CONFIG, rwbuffer++);
1981 /* normal should be zero */
1982 writel(ccb_phyaddr_hi32, rwbuffer++);
1983 /* postQ size (256 + 8)*4 */
1984 writel(post_queue_phyaddr, rwbuffer++);
1985 /* doneQ size (256 + 8)*4 */
1986 writel(post_queue_phyaddr + 1056, rwbuffer++);
1987 /* ccb maxQ size must be --> [(256 + 8)*4]*/
1988 writel(1056, rwbuffer);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001989
Nick Cheng1a4f5502007-09-13 17:26:40 +08001990 writel(ARCMSR_MESSAGE_SET_CONFIG, reg->drv2iop_doorbell_reg);
1991 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1992 printk(KERN_NOTICE "arcmsr%d: 'set command Q window' \
1993 timeout \n",acb->host->host_no);
1994 return 1;
1995 }
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001996
Nick Cheng1a4f5502007-09-13 17:26:40 +08001997 writel(ARCMSR_MESSAGE_START_DRIVER_MODE, reg->drv2iop_doorbell_reg);
1998 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1999 printk(KERN_NOTICE "arcmsr%d: 'can not set diver mode \n"\
2000 ,acb->host->host_no);
2001 return 1;
2002 }
2003 arcmsr_enable_outbound_ints(acb, intmask_org);
2004 }
2005 break;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002006 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08002007 return 0;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002008}
2009
Nick Cheng1a4f5502007-09-13 17:26:40 +08002010static void arcmsr_wait_firmware_ready(struct AdapterControlBlock *acb)
2011{
2012 uint32_t firmware_state = 0;
2013
2014 switch (acb->adapter_type) {
2015
2016 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00002017 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002018 do {
2019 firmware_state = readl(&reg->outbound_msgaddr1);
2020 } while ((firmware_state & ARCMSR_OUTBOUND_MESG1_FIRMWARE_OK) == 0);
2021 }
2022 break;
2023
2024 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00002025 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002026 do {
2027 firmware_state = readl(reg->iop2drv_doorbell_reg);
2028 } while ((firmware_state & ARCMSR_MESSAGE_FIRMWARE_OK) == 0);
2029 }
2030 break;
2031 }
2032}
2033
2034static void arcmsr_start_hba_bgrb(struct AdapterControlBlock *acb)
2035{
Al Viro80da1ad2007-10-29 05:08:28 +00002036 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002037 acb->acb_flags |= ACB_F_MSG_START_BGRB;
2038 writel(ARCMSR_INBOUND_MESG0_START_BGRB, &reg->inbound_msgaddr0);
2039 if (arcmsr_hba_wait_msgint_ready(acb)) {
2040 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
2041 rebulid' timeout \n", acb->host->host_no);
2042 }
2043}
2044
2045static void arcmsr_start_hbb_bgrb(struct AdapterControlBlock *acb)
2046{
Al Viro80da1ad2007-10-29 05:08:28 +00002047 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002048 acb->acb_flags |= ACB_F_MSG_START_BGRB;
2049 writel(ARCMSR_MESSAGE_START_BGRB, reg->drv2iop_doorbell_reg);
2050 if (arcmsr_hbb_wait_msgint_ready(acb)) {
2051 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
2052 rebulid' timeout \n",acb->host->host_no);
2053 }
2054}
2055
2056static void arcmsr_start_adapter_bgrb(struct AdapterControlBlock *acb)
2057{
2058 switch (acb->adapter_type) {
2059 case ACB_ADAPTER_TYPE_A:
2060 arcmsr_start_hba_bgrb(acb);
2061 break;
2062 case ACB_ADAPTER_TYPE_B:
2063 arcmsr_start_hbb_bgrb(acb);
2064 break;
2065 }
2066}
2067
2068static void arcmsr_clear_doorbell_queue_buffer(struct AdapterControlBlock *acb)
2069{
2070 switch (acb->adapter_type) {
2071 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00002072 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002073 uint32_t outbound_doorbell;
2074 /* empty doorbell Qbuffer if door bell ringed */
2075 outbound_doorbell = readl(&reg->outbound_doorbell);
2076 /*clear doorbell interrupt */
2077 writel(outbound_doorbell, &reg->outbound_doorbell);
2078 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, &reg->inbound_doorbell);
2079 }
2080 break;
2081
2082 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00002083 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002084 /*clear interrupt and message state*/
2085 writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN, reg->iop2drv_doorbell_reg);
2086 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell_reg);
2087 /* let IOP know data has been read */
2088 }
2089 break;
2090 }
2091}
Erich Chen1c57e862006-07-12 08:59:32 -07002092
2093static void arcmsr_iop_init(struct AdapterControlBlock *acb)
2094{
Nick Cheng1a4f5502007-09-13 17:26:40 +08002095 uint32_t intmask_org;
Erich Chen1c57e862006-07-12 08:59:32 -07002096
Nick Cheng1a4f5502007-09-13 17:26:40 +08002097 arcmsr_wait_firmware_ready(acb);
2098 arcmsr_iop_confirm(acb);
2099 /* disable all outbound interrupt */
2100 intmask_org = arcmsr_disable_outbound_ints(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07002101 arcmsr_get_firmware_spec(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002102 /*start background rebuild*/
2103 arcmsr_start_adapter_bgrb(acb);
2104 /* empty doorbell Qbuffer if door bell ringed */
2105 arcmsr_clear_doorbell_queue_buffer(acb);
2106 /* enable outbound Post Queue,outbound doorbell Interrupt */
2107 arcmsr_enable_outbound_ints(acb, intmask_org);
Erich Chen1c57e862006-07-12 08:59:32 -07002108 acb->acb_flags |= ACB_F_IOP_INITED;
2109}
2110
2111static void arcmsr_iop_reset(struct AdapterControlBlock *acb)
2112{
Erich Chen1c57e862006-07-12 08:59:32 -07002113 struct CommandControlBlock *ccb;
2114 uint32_t intmask_org;
2115 int i = 0;
2116
2117 if (atomic_read(&acb->ccboutstandingcount) != 0) {
2118 /* talk to iop 331 outstanding command aborted */
2119 arcmsr_abort_allcmd(acb);
Jeff Garzik24430452007-07-27 13:00:48 -04002120
Erich Chen1c57e862006-07-12 08:59:32 -07002121 /* wait for 3 sec for all command aborted*/
Nick Cheng1a4f5502007-09-13 17:26:40 +08002122 ssleep(3);
Jeff Garzik24430452007-07-27 13:00:48 -04002123
Erich Chen1c57e862006-07-12 08:59:32 -07002124 /* disable all outbound interrupt */
2125 intmask_org = arcmsr_disable_outbound_ints(acb);
2126 /* clear all outbound posted Q */
Nick Cheng1a4f5502007-09-13 17:26:40 +08002127 arcmsr_done4abort_postqueue(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07002128 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2129 ccb = acb->pccb_pool[i];
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002130 if (ccb->startdone == ARCMSR_CCB_START) {
Erich Chen1c57e862006-07-12 08:59:32 -07002131 ccb->startdone = ARCMSR_CCB_ABORTED;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002132 arcmsr_ccb_complete(ccb, 1);
Erich Chen1c57e862006-07-12 08:59:32 -07002133 }
2134 }
2135 /* enable all outbound interrupt */
2136 arcmsr_enable_outbound_ints(acb, intmask_org);
2137 }
Erich Chen1c57e862006-07-12 08:59:32 -07002138}
2139
2140static int arcmsr_bus_reset(struct scsi_cmnd *cmd)
2141{
2142 struct AdapterControlBlock *acb =
2143 (struct AdapterControlBlock *)cmd->device->host->hostdata;
2144 int i;
2145
2146 acb->num_resets++;
2147 acb->acb_flags |= ACB_F_BUS_RESET;
2148 for (i = 0; i < 400; i++) {
2149 if (!atomic_read(&acb->ccboutstandingcount))
2150 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002151 arcmsr_interrupt(acb);/* FIXME: need spinlock */
Erich Chen1c57e862006-07-12 08:59:32 -07002152 msleep(25);
2153 }
2154 arcmsr_iop_reset(acb);
2155 acb->acb_flags &= ~ACB_F_BUS_RESET;
2156 return SUCCESS;
2157}
2158
2159static void arcmsr_abort_one_cmd(struct AdapterControlBlock *acb,
2160 struct CommandControlBlock *ccb)
2161{
2162 u32 intmask;
2163
2164 ccb->startdone = ARCMSR_CCB_ABORTED;
2165
2166 /*
2167 ** Wait for 3 sec for all command done.
2168 */
Nick Cheng1a4f5502007-09-13 17:26:40 +08002169 ssleep(3);
Erich Chen1c57e862006-07-12 08:59:32 -07002170
2171 intmask = arcmsr_disable_outbound_ints(acb);
2172 arcmsr_polling_ccbdone(acb, ccb);
2173 arcmsr_enable_outbound_ints(acb, intmask);
2174}
2175
2176static int arcmsr_abort(struct scsi_cmnd *cmd)
2177{
2178 struct AdapterControlBlock *acb =
2179 (struct AdapterControlBlock *)cmd->device->host->hostdata;
2180 int i = 0;
2181
2182 printk(KERN_NOTICE
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002183 "arcmsr%d: abort device command of scsi id = %d lun = %d \n",
Erich Chen1c57e862006-07-12 08:59:32 -07002184 acb->host->host_no, cmd->device->id, cmd->device->lun);
2185 acb->num_aborts++;
Erich Chen1c57e862006-07-12 08:59:32 -07002186 /*
2187 ************************************************
2188 ** the all interrupt service routine is locked
2189 ** we need to handle it as soon as possible and exit
2190 ************************************************
2191 */
2192 if (!atomic_read(&acb->ccboutstandingcount))
2193 return SUCCESS;
2194
2195 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2196 struct CommandControlBlock *ccb = acb->pccb_pool[i];
2197 if (ccb->startdone == ARCMSR_CCB_START && ccb->pcmd == cmd) {
2198 arcmsr_abort_one_cmd(acb, ccb);
2199 break;
2200 }
2201 }
2202
2203 return SUCCESS;
2204}
2205
2206static const char *arcmsr_info(struct Scsi_Host *host)
2207{
2208 struct AdapterControlBlock *acb =
2209 (struct AdapterControlBlock *) host->hostdata;
2210 static char buf[256];
2211 char *type;
2212 int raid6 = 1;
2213
2214 switch (acb->pdev->device) {
2215 case PCI_DEVICE_ID_ARECA_1110:
Nick Cheng1a4f5502007-09-13 17:26:40 +08002216 case PCI_DEVICE_ID_ARECA_1200:
2217 case PCI_DEVICE_ID_ARECA_1202:
Erich Chen1c57e862006-07-12 08:59:32 -07002218 case PCI_DEVICE_ID_ARECA_1210:
2219 raid6 = 0;
2220 /*FALLTHRU*/
2221 case PCI_DEVICE_ID_ARECA_1120:
2222 case PCI_DEVICE_ID_ARECA_1130:
2223 case PCI_DEVICE_ID_ARECA_1160:
2224 case PCI_DEVICE_ID_ARECA_1170:
Nick Cheng1a4f5502007-09-13 17:26:40 +08002225 case PCI_DEVICE_ID_ARECA_1201:
Erich Chen1c57e862006-07-12 08:59:32 -07002226 case PCI_DEVICE_ID_ARECA_1220:
2227 case PCI_DEVICE_ID_ARECA_1230:
2228 case PCI_DEVICE_ID_ARECA_1260:
2229 case PCI_DEVICE_ID_ARECA_1270:
2230 case PCI_DEVICE_ID_ARECA_1280:
2231 type = "SATA";
2232 break;
2233 case PCI_DEVICE_ID_ARECA_1380:
2234 case PCI_DEVICE_ID_ARECA_1381:
2235 case PCI_DEVICE_ID_ARECA_1680:
2236 case PCI_DEVICE_ID_ARECA_1681:
2237 type = "SAS";
2238 break;
2239 default:
2240 type = "X-TYPE";
2241 break;
2242 }
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002243 sprintf(buf, "Areca %s Host Adapter RAID Controller%s\n %s",
Erich Chen1c57e862006-07-12 08:59:32 -07002244 type, raid6 ? "( RAID6 capable)" : "",
2245 ARCMSR_DRIVER_VERSION);
2246 return buf;
2247}
Nick Cheng1a4f5502007-09-13 17:26:40 +08002248#ifdef CONFIG_SCSI_ARCMSR_AER
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002249static pci_ers_result_t arcmsr_pci_slot_reset(struct pci_dev *pdev)
2250{
Nick Cheng1a4f5502007-09-13 17:26:40 +08002251 struct Scsi_Host *host = pci_get_drvdata(pdev);
2252 struct AdapterControlBlock *acb =
2253 (struct AdapterControlBlock *) host->hostdata;
2254 uint32_t intmask_org;
2255 int i, j;
Erich Chen1c57e862006-07-12 08:59:32 -07002256
Nick Cheng1a4f5502007-09-13 17:26:40 +08002257 if (pci_enable_device(pdev)) {
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002258 return PCI_ERS_RESULT_DISCONNECT;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002259 }
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002260 pci_set_master(pdev);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002261 intmask_org = arcmsr_disable_outbound_ints(acb);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002262 acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
2263 ACB_F_MESSAGE_RQBUFFER_CLEARED |
2264 ACB_F_MESSAGE_WQBUFFER_READED);
2265 acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002266 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
2267 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
2268 acb->devstate[i][j] = ARECA_RAID_GONE;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002269
Nick Cheng1a4f5502007-09-13 17:26:40 +08002270 arcmsr_wait_firmware_ready(acb);
2271 arcmsr_iop_confirm(acb);
2272 /* disable all outbound interrupt */
2273 arcmsr_get_firmware_spec(acb);
2274 /*start background rebuild*/
2275 arcmsr_start_adapter_bgrb(acb);
2276 /* empty doorbell Qbuffer if door bell ringed */
2277 arcmsr_clear_doorbell_queue_buffer(acb);
2278 /* enable outbound Post Queue,outbound doorbell Interrupt */
2279 arcmsr_enable_outbound_ints(acb, intmask_org);
2280 acb->acb_flags |= ACB_F_IOP_INITED;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002281
Nick Cheng1a4f5502007-09-13 17:26:40 +08002282 pci_enable_pcie_error_reporting(pdev);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002283 return PCI_ERS_RESULT_RECOVERED;
2284}
2285
2286static void arcmsr_pci_ers_need_reset_forepart(struct pci_dev *pdev)
2287{
2288 struct Scsi_Host *host = pci_get_drvdata(pdev);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002289 struct AdapterControlBlock *acb = (struct AdapterControlBlock *)host->hostdata;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002290 struct CommandControlBlock *ccb;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002291 uint32_t intmask_org;
2292 int i = 0;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002293
Nick Cheng1a4f5502007-09-13 17:26:40 +08002294 if (atomic_read(&acb->ccboutstandingcount) != 0) {
2295 /* talk to iop 331 outstanding command aborted */
2296 arcmsr_abort_allcmd(acb);
2297 /* wait for 3 sec for all command aborted*/
2298 ssleep(3);
2299 /* disable all outbound interrupt */
2300 intmask_org = arcmsr_disable_outbound_ints(acb);
2301 /* clear all outbound posted Q */
2302 arcmsr_done4abort_postqueue(acb);
2303 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2304 ccb = acb->pccb_pool[i];
2305 if (ccb->startdone == ARCMSR_CCB_START) {
2306 ccb->startdone = ARCMSR_CCB_ABORTED;
2307 arcmsr_ccb_complete(ccb, 1);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002308 }
2309 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08002310 /* enable all outbound interrupt */
2311 arcmsr_enable_outbound_ints(acb, intmask_org);
2312 }
2313 pci_disable_device(pdev);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002314}
2315
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002316static void arcmsr_pci_ers_disconnect_forepart(struct pci_dev *pdev)
2317{
Nick Cheng1a4f5502007-09-13 17:26:40 +08002318 struct Scsi_Host *host = pci_get_drvdata(pdev);
2319 struct AdapterControlBlock *acb = \
2320 (struct AdapterControlBlock *)host->hostdata;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002321
Nick Cheng1a4f5502007-09-13 17:26:40 +08002322 arcmsr_stop_adapter_bgrb(acb);
2323 arcmsr_flush_adapter_cache(acb);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002324}
2325
2326static pci_ers_result_t arcmsr_pci_error_detected(struct pci_dev *pdev,
2327 pci_channel_state_t state)
2328{
2329 switch (state) {
2330 case pci_channel_io_frozen:
2331 arcmsr_pci_ers_need_reset_forepart(pdev);
2332 return PCI_ERS_RESULT_NEED_RESET;
2333 case pci_channel_io_perm_failure:
2334 arcmsr_pci_ers_disconnect_forepart(pdev);
2335 return PCI_ERS_RESULT_DISCONNECT;
2336 break;
2337 default:
2338 return PCI_ERS_RESULT_NEED_RESET;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002339 }
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002340}
Nick Cheng1a4f5502007-09-13 17:26:40 +08002341#endif