blob: aaee028dd901bc98aae204438b595d36fbcbdbef [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,
FUJITA Tomonori9cb83c72007-10-16 11:24:32 +0200125 .use_sg_chaining = ENABLE_SG_CHAINING,
Erich Chen1c57e862006-07-12 08:59:32 -0700126 .shost_attrs = arcmsr_host_attrs,
127};
Nick Cheng1a4f5502007-09-13 17:26:40 +0800128#ifdef CONFIG_SCSI_ARCMSR_AER
Andrew Morton5975f642007-10-02 14:37:59 -0700129static pci_ers_result_t arcmsr_pci_slot_reset(struct pci_dev *pdev);
130static pci_ers_result_t arcmsr_pci_error_detected(struct pci_dev *pdev,
131 pci_channel_state_t state);
132
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800133static struct pci_error_handlers arcmsr_pci_error_handlers = {
134 .error_detected = arcmsr_pci_error_detected,
135 .slot_reset = arcmsr_pci_slot_reset,
136};
Nick Cheng1a4f5502007-09-13 17:26:40 +0800137#endif
Erich Chen1c57e862006-07-12 08:59:32 -0700138static struct pci_device_id arcmsr_device_id_table[] = {
139 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1110)},
140 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1120)},
141 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1130)},
142 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1160)},
143 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1170)},
Nick Cheng1a4f5502007-09-13 17:26:40 +0800144 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1200)},
145 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1201)},
146 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1202)},
Erich Chen1c57e862006-07-12 08:59:32 -0700147 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1210)},
148 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1220)},
149 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1230)},
150 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1260)},
151 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1270)},
152 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1280)},
153 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1380)},
154 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1381)},
155 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1680)},
156 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1681)},
157 {0, 0}, /* Terminating entry */
158};
159MODULE_DEVICE_TABLE(pci, arcmsr_device_id_table);
160static struct pci_driver arcmsr_pci_driver = {
161 .name = "arcmsr",
162 .id_table = arcmsr_device_id_table,
163 .probe = arcmsr_probe,
164 .remove = arcmsr_remove,
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800165 .shutdown = arcmsr_shutdown,
Nick Cheng1a4f5502007-09-13 17:26:40 +0800166 #ifdef CONFIG_SCSI_ARCMSR_AER
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800167 .err_handler = &arcmsr_pci_error_handlers,
Nick Cheng1a4f5502007-09-13 17:26:40 +0800168 #endif
Erich Chen1c57e862006-07-12 08:59:32 -0700169};
170
David Howells7d12e782006-10-05 14:55:46 +0100171static irqreturn_t arcmsr_do_interrupt(int irq, void *dev_id)
Erich Chen1c57e862006-07-12 08:59:32 -0700172{
173 irqreturn_t handle_state;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800174 struct AdapterControlBlock *acb = dev_id;
Erich Chen1c57e862006-07-12 08:59:32 -0700175
Nick Cheng1a4f5502007-09-13 17:26:40 +0800176 spin_lock(acb->host->host_lock);
Erich Chen1c57e862006-07-12 08:59:32 -0700177 handle_state = arcmsr_interrupt(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800178 spin_unlock(acb->host->host_lock);
179
Erich Chen1c57e862006-07-12 08:59:32 -0700180 return handle_state;
181}
182
183static int arcmsr_bios_param(struct scsi_device *sdev,
184 struct block_device *bdev, sector_t capacity, int *geom)
185{
186 int ret, heads, sectors, cylinders, total_capacity;
187 unsigned char *buffer;/* return copy of block device's partition table */
188
189 buffer = scsi_bios_ptable(bdev);
190 if (buffer) {
191 ret = scsi_partsize(buffer, capacity, &geom[2], &geom[0], &geom[1]);
192 kfree(buffer);
193 if (ret != -1)
194 return ret;
195 }
196 total_capacity = capacity;
197 heads = 64;
198 sectors = 32;
199 cylinders = total_capacity / (heads * sectors);
200 if (cylinders > 1024) {
201 heads = 255;
202 sectors = 63;
203 cylinders = total_capacity / (heads * sectors);
204 }
205 geom[0] = heads;
206 geom[1] = sectors;
207 geom[2] = cylinders;
208 return 0;
209}
210
Nick Cheng1a4f5502007-09-13 17:26:40 +0800211static void arcmsr_define_adapter_type(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -0700212{
213 struct pci_dev *pdev = acb->pdev;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800214 u16 dev_id;
215 pci_read_config_word(pdev, PCI_DEVICE_ID, &dev_id);
216 switch (dev_id) {
217 case 0x1201 : {
218 acb->adapter_type = ACB_ADAPTER_TYPE_B;
219 }
220 break;
Erich Chen1c57e862006-07-12 08:59:32 -0700221
Nick Cheng1a4f5502007-09-13 17:26:40 +0800222 default : acb->adapter_type = ACB_ADAPTER_TYPE_A;
223 }
224}
225
226static int arcmsr_alloc_ccb_pool(struct AdapterControlBlock *acb)
227{
228
229 switch (acb->adapter_type) {
230
231 case ACB_ADAPTER_TYPE_A: {
232 struct pci_dev *pdev = acb->pdev;
233 void *dma_coherent;
234 dma_addr_t dma_coherent_handle, dma_addr;
235 struct CommandControlBlock *ccb_tmp;
236 uint32_t intmask_org;
237 int i, j;
238
Al Viro80da1ad2007-10-29 05:08:28 +0000239 acb->pmuA = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
240 if (!acb->pmuA) {
Nick Cheng1a4f5502007-09-13 17:26:40 +0800241 printk(KERN_NOTICE "arcmsr%d: memory mapping region fail \n",
242 acb->host->host_no);
243 }
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);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800249 if (!dma_coherent)
250 return -ENOMEM;
Erich Chen1c57e862006-07-12 08:59:32 -0700251
Nick Cheng1a4f5502007-09-13 17:26:40 +0800252 acb->dma_coherent = dma_coherent;
253 acb->dma_coherent_handle = dma_coherent_handle;
Erich Chen1c57e862006-07-12 08:59:32 -0700254
Nick Cheng1a4f5502007-09-13 17:26:40 +0800255 if (((unsigned long)dma_coherent & 0x1F)) {
256 dma_coherent = dma_coherent +
257 (0x20 - ((unsigned long)dma_coherent & 0x1F));
258 dma_coherent_handle = dma_coherent_handle +
259 (0x20 - ((unsigned long)dma_coherent_handle & 0x1F));
260 }
261
262 dma_addr = dma_coherent_handle;
263 ccb_tmp = (struct CommandControlBlock *)dma_coherent;
264 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
265 ccb_tmp->cdb_shifted_phyaddr = dma_addr >> 5;
266 ccb_tmp->acb = acb;
267 acb->pccb_pool[i] = ccb_tmp;
268 list_add_tail(&ccb_tmp->list, &acb->ccb_free_list);
269 dma_addr = dma_addr + sizeof(struct CommandControlBlock);
270 ccb_tmp++;
271 }
272
273 acb->vir2phy_offset = (unsigned long)ccb_tmp -(unsigned long)dma_addr;
274 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
275 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
276 acb->devstate[i][j] = ARECA_RAID_GONE;
277
278 /*
279 ** here we need to tell iop 331 our ccb_tmp.HighPart
280 ** if ccb_tmp.HighPart is not zero
281 */
282 intmask_org = arcmsr_disable_outbound_ints(acb);
283 }
284 break;
285
286 case ACB_ADAPTER_TYPE_B: {
287
288 struct pci_dev *pdev = acb->pdev;
289 struct MessageUnit_B *reg;
Al Viro80da1ad2007-10-29 05:08:28 +0000290 void __iomem *mem_base0, *mem_base1;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800291 void *dma_coherent;
292 dma_addr_t dma_coherent_handle, dma_addr;
293 uint32_t intmask_org;
294 struct CommandControlBlock *ccb_tmp;
295 int i, j;
296
297 dma_coherent = dma_alloc_coherent(&pdev->dev,
298 ((ARCMSR_MAX_FREECCB_NUM *
299 sizeof(struct CommandControlBlock) + 0x20) +
300 sizeof(struct MessageUnit_B)),
301 &dma_coherent_handle, GFP_KERNEL);
302 if (!dma_coherent)
303 return -ENOMEM;
304
305 acb->dma_coherent = dma_coherent;
306 acb->dma_coherent_handle = dma_coherent_handle;
307
308 if (((unsigned long)dma_coherent & 0x1F)) {
309 dma_coherent = dma_coherent +
310 (0x20 - ((unsigned long)dma_coherent & 0x1F));
311 dma_coherent_handle = dma_coherent_handle +
312 (0x20 - ((unsigned long)dma_coherent_handle & 0x1F));
313 }
314
315 reg = (struct MessageUnit_B *)(dma_coherent +
316 ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock));
317
318 dma_addr = dma_coherent_handle;
319 ccb_tmp = (struct CommandControlBlock *)dma_coherent;
320 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
321 ccb_tmp->cdb_shifted_phyaddr = dma_addr >> 5;
322 ccb_tmp->acb = acb;
323 acb->pccb_pool[i] = ccb_tmp;
324 list_add_tail(&ccb_tmp->list, &acb->ccb_free_list);
325 dma_addr = dma_addr + sizeof(struct CommandControlBlock);
326 ccb_tmp++;
327 }
328
329 reg = (struct MessageUnit_B *)(dma_coherent +
330 ARCMSR_MAX_FREECCB_NUM * sizeof(struct CommandControlBlock));
Al Viro80da1ad2007-10-29 05:08:28 +0000331 acb->pmuB = reg;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800332 mem_base0 = ioremap(pci_resource_start(pdev, 0),
333 pci_resource_len(pdev, 0));
334 mem_base1 = ioremap(pci_resource_start(pdev, 2),
335 pci_resource_len(pdev, 2));
Al Viro80da1ad2007-10-29 05:08:28 +0000336 reg->drv2iop_doorbell_reg = mem_base0 + ARCMSR_DRV2IOP_DOORBELL;
337 reg->drv2iop_doorbell_mask_reg = mem_base0 +
338 ARCMSR_DRV2IOP_DOORBELL_MASK;
339 reg->iop2drv_doorbell_reg = mem_base0 + ARCMSR_IOP2DRV_DOORBELL;
340 reg->iop2drv_doorbell_mask_reg = mem_base0 +
341 ARCMSR_IOP2DRV_DOORBELL_MASK;
342 reg->ioctl_wbuffer_reg = mem_base1 + ARCMSR_IOCTL_WBUFFER;
343 reg->ioctl_rbuffer_reg = mem_base1 + ARCMSR_IOCTL_RBUFFER;
344 reg->msgcode_rwbuffer_reg = mem_base1 + ARCMSR_MSGCODE_RWBUFFER;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800345
346 acb->vir2phy_offset = (unsigned long)ccb_tmp -(unsigned long)dma_addr;
347 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
348 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
349 acb->devstate[i][j] = ARECA_RAID_GOOD;
350
351 /*
352 ** here we need to tell iop 331 our ccb_tmp.HighPart
353 ** if ccb_tmp.HighPart is not zero
354 */
355 intmask_org = arcmsr_disable_outbound_ints(acb);
356 }
357 break;
Erich Chen1c57e862006-07-12 08:59:32 -0700358 }
Erich Chen1c57e862006-07-12 08:59:32 -0700359 return 0;
360}
361
362static int arcmsr_probe(struct pci_dev *pdev,
363 const struct pci_device_id *id)
364{
365 struct Scsi_Host *host;
366 struct AdapterControlBlock *acb;
367 uint8_t bus, dev_fun;
368 int error;
369
370 error = pci_enable_device(pdev);
371 if (error)
372 goto out;
373 pci_set_master(pdev);
374
375 host = scsi_host_alloc(&arcmsr_scsi_host_template,
376 sizeof(struct AdapterControlBlock));
377 if (!host) {
378 error = -ENOMEM;
379 goto out_disable_device;
380 }
381 acb = (struct AdapterControlBlock *)host->hostdata;
382 memset(acb, 0, sizeof (struct AdapterControlBlock));
383
384 error = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
385 if (error) {
386 error = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
387 if (error) {
388 printk(KERN_WARNING
389 "scsi%d: No suitable DMA mask available\n",
390 host->host_no);
391 goto out_host_put;
392 }
393 }
394 bus = pdev->bus->number;
395 dev_fun = pdev->devfn;
396 acb->host = host;
397 acb->pdev = pdev;
398 host->max_sectors = ARCMSR_MAX_XFER_SECTORS;
399 host->max_lun = ARCMSR_MAX_TARGETLUN;
400 host->max_id = ARCMSR_MAX_TARGETID;/*16:8*/
401 host->max_cmd_len = 16; /*this is issue of 64bit LBA, over 2T byte*/
402 host->sg_tablesize = ARCMSR_MAX_SG_ENTRIES;
403 host->can_queue = ARCMSR_MAX_FREECCB_NUM; /* max simultaneous cmds */
404 host->cmd_per_lun = ARCMSR_MAX_CMD_PERLUN;
405 host->this_id = ARCMSR_SCSI_INITIATOR_ID;
406 host->unique_id = (bus << 8) | dev_fun;
407 host->irq = pdev->irq;
408 error = pci_request_regions(pdev, "arcmsr");
Nick Cheng1a4f5502007-09-13 17:26:40 +0800409 if (error) {
Erich Chen1c57e862006-07-12 08:59:32 -0700410 goto out_host_put;
Erich Chen1c57e862006-07-12 08:59:32 -0700411 }
Nick Cheng1a4f5502007-09-13 17:26:40 +0800412 arcmsr_define_adapter_type(acb);
413
Erich Chen1c57e862006-07-12 08:59:32 -0700414 acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
415 ACB_F_MESSAGE_RQBUFFER_CLEARED |
416 ACB_F_MESSAGE_WQBUFFER_READED);
417 acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
418 INIT_LIST_HEAD(&acb->ccb_free_list);
419
420 error = arcmsr_alloc_ccb_pool(acb);
421 if (error)
Nick Cheng1a4f5502007-09-13 17:26:40 +0800422 goto out_release_regions;
Erich Chen1c57e862006-07-12 08:59:32 -0700423
424 error = request_irq(pdev->irq, arcmsr_do_interrupt,
Jeff Garzik488a5c82007-07-27 13:01:15 -0400425 IRQF_SHARED, "arcmsr", acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700426 if (error)
427 goto out_free_ccb_pool;
428
429 arcmsr_iop_init(acb);
430 pci_set_drvdata(pdev, host);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800431 if (strncmp(acb->firm_version, "V1.42", 5) >= 0)
432 host->max_sectors= ARCMSR_MAX_XFER_SECTORS_B;
Erich Chen1c57e862006-07-12 08:59:32 -0700433
434 error = scsi_add_host(host, &pdev->dev);
435 if (error)
436 goto out_free_irq;
437
438 error = arcmsr_alloc_sysfs_attr(acb);
439 if (error)
440 goto out_free_sysfs;
441
442 scsi_scan_host(host);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800443 #ifdef CONFIG_SCSI_ARCMSR_AER
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800444 pci_enable_pcie_error_reporting(pdev);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800445 #endif
Erich Chen1c57e862006-07-12 08:59:32 -0700446 return 0;
447 out_free_sysfs:
448 out_free_irq:
449 free_irq(pdev->irq, acb);
450 out_free_ccb_pool:
451 arcmsr_free_ccb_pool(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700452 iounmap(acb->pmu);
453 out_release_regions:
454 pci_release_regions(pdev);
455 out_host_put:
456 scsi_host_put(host);
457 out_disable_device:
458 pci_disable_device(pdev);
459 out:
460 return error;
461}
462
Nick Cheng1a4f5502007-09-13 17:26:40 +0800463static uint8_t arcmsr_hba_wait_msgint_ready(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -0700464{
Al Viro80da1ad2007-10-29 05:08:28 +0000465 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800466 uint32_t Index;
467 uint8_t Retries = 0x00;
468
469 do {
470 for (Index = 0; Index < 100; Index++) {
471 if (readl(&reg->outbound_intstatus) &
472 ARCMSR_MU_OUTBOUND_MESSAGE0_INT) {
473 writel(ARCMSR_MU_OUTBOUND_MESSAGE0_INT,
474 &reg->outbound_intstatus);
475 return 0x00;
476 }
477 msleep(10);
478 }/*max 1 seconds*/
479
480 } while (Retries++ < 20);/*max 20 sec*/
481 return 0xff;
482}
483
484static uint8_t arcmsr_hbb_wait_msgint_ready(struct AdapterControlBlock *acb)
485{
Al Viro80da1ad2007-10-29 05:08:28 +0000486 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800487 uint32_t Index;
488 uint8_t Retries = 0x00;
489
490 do {
491 for (Index = 0; Index < 100; Index++) {
492 if (readl(reg->iop2drv_doorbell_reg)
493 & ARCMSR_IOP2DRV_MESSAGE_CMD_DONE) {
494 writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN
495 , reg->iop2drv_doorbell_reg);
496 return 0x00;
497 }
498 msleep(10);
499 }/*max 1 seconds*/
500
501 } while (Retries++ < 20);/*max 20 sec*/
502 return 0xff;
503}
504
505static void arcmsr_abort_hba_allcmd(struct AdapterControlBlock *acb)
506{
Al Viro80da1ad2007-10-29 05:08:28 +0000507 struct MessageUnit_A __iomem *reg = acb->pmuA;
Erich Chen1c57e862006-07-12 08:59:32 -0700508
509 writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, &reg->inbound_msgaddr0);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800510 if (arcmsr_hba_wait_msgint_ready(acb))
Erich Chen1c57e862006-07-12 08:59:32 -0700511 printk(KERN_NOTICE
512 "arcmsr%d: wait 'abort all outstanding command' timeout \n"
513 , acb->host->host_no);
514}
515
Nick Cheng1a4f5502007-09-13 17:26:40 +0800516static void arcmsr_abort_hbb_allcmd(struct AdapterControlBlock *acb)
517{
Al Viro80da1ad2007-10-29 05:08:28 +0000518 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800519
520 writel(ARCMSR_MESSAGE_ABORT_CMD, reg->drv2iop_doorbell_reg);
521 if (arcmsr_hbb_wait_msgint_ready(acb))
522 printk(KERN_NOTICE
523 "arcmsr%d: wait 'abort all outstanding command' timeout \n"
524 , acb->host->host_no);
525}
526
527static void arcmsr_abort_allcmd(struct AdapterControlBlock *acb)
528{
529 switch (acb->adapter_type) {
530 case ACB_ADAPTER_TYPE_A: {
531 arcmsr_abort_hba_allcmd(acb);
532 }
533 break;
534
535 case ACB_ADAPTER_TYPE_B: {
536 arcmsr_abort_hbb_allcmd(acb);
537 }
538 }
539}
540
Erich Chen1c57e862006-07-12 08:59:32 -0700541static void arcmsr_pci_unmap_dma(struct CommandControlBlock *ccb)
542{
Erich Chen1c57e862006-07-12 08:59:32 -0700543 struct scsi_cmnd *pcmd = ccb->pcmd;
544
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900545 scsi_dma_unmap(pcmd);
Erich Chen1c57e862006-07-12 08:59:32 -0700546}
547
548static void arcmsr_ccb_complete(struct CommandControlBlock *ccb, int stand_flag)
549{
550 struct AdapterControlBlock *acb = ccb->acb;
551 struct scsi_cmnd *pcmd = ccb->pcmd;
552
553 arcmsr_pci_unmap_dma(ccb);
554 if (stand_flag == 1)
555 atomic_dec(&acb->ccboutstandingcount);
556 ccb->startdone = ARCMSR_CCB_DONE;
557 ccb->ccb_flags = 0;
558 list_add_tail(&ccb->list, &acb->ccb_free_list);
559 pcmd->scsi_done(pcmd);
560}
561
Nick Cheng1a4f5502007-09-13 17:26:40 +0800562static void arcmsr_flush_hba_cache(struct AdapterControlBlock *acb)
563{
Al Viro80da1ad2007-10-29 05:08:28 +0000564 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800565 int retry_count = 30;
566
567 writel(ARCMSR_INBOUND_MESG0_FLUSH_CACHE, &reg->inbound_msgaddr0);
568 do {
569 if (!arcmsr_hba_wait_msgint_ready(acb))
570 break;
571 else {
572 retry_count--;
573 printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
574 timeout, retry count down = %d \n", acb->host->host_no, retry_count);
575 }
576 } while (retry_count != 0);
577}
578
579static void arcmsr_flush_hbb_cache(struct AdapterControlBlock *acb)
580{
Al Viro80da1ad2007-10-29 05:08:28 +0000581 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800582 int retry_count = 30;
583
584 writel(ARCMSR_MESSAGE_FLUSH_CACHE, reg->drv2iop_doorbell_reg);
585 do {
586 if (!arcmsr_hbb_wait_msgint_ready(acb))
587 break;
588 else {
589 retry_count--;
590 printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
591 timeout,retry count down = %d \n", acb->host->host_no, retry_count);
592 }
593 } while (retry_count != 0);
594}
595
596static void arcmsr_flush_adapter_cache(struct AdapterControlBlock *acb)
597{
598 switch (acb->adapter_type) {
599
600 case ACB_ADAPTER_TYPE_A: {
601 arcmsr_flush_hba_cache(acb);
602 }
603 break;
604
605 case ACB_ADAPTER_TYPE_B: {
606 arcmsr_flush_hbb_cache(acb);
607 }
608 }
609}
610
611static void arcmsr_report_sense_info(struct CommandControlBlock *ccb)
612{
613
614 struct scsi_cmnd *pcmd = ccb->pcmd;
615 struct SENSE_DATA *sensebuffer = (struct SENSE_DATA *)pcmd->sense_buffer;
616
617 pcmd->result = DID_OK << 16;
618 if (sensebuffer) {
619 int sense_data_length =
620 sizeof(struct SENSE_DATA) < sizeof(pcmd->sense_buffer)
621 ? sizeof(struct SENSE_DATA) : sizeof(pcmd->sense_buffer);
622 memset(sensebuffer, 0, sizeof(pcmd->sense_buffer));
623 memcpy(sensebuffer, ccb->arcmsr_cdb.SenseData, sense_data_length);
624 sensebuffer->ErrorCode = SCSI_SENSE_CURRENT_ERRORS;
625 sensebuffer->Valid = 1;
626 }
627}
628
629static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb)
630{
631 u32 orig_mask = 0;
632 switch (acb->adapter_type) {
633
634 case ACB_ADAPTER_TYPE_A : {
Al Viro80da1ad2007-10-29 05:08:28 +0000635 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800636 orig_mask = readl(&reg->outbound_intmask)|\
637 ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE;
638 writel(orig_mask|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE, \
639 &reg->outbound_intmask);
640 }
641 break;
642
643 case ACB_ADAPTER_TYPE_B : {
Al Viro80da1ad2007-10-29 05:08:28 +0000644 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800645 orig_mask = readl(reg->iop2drv_doorbell_mask_reg) & \
646 (~ARCMSR_IOP2DRV_MESSAGE_CMD_DONE);
647 writel(0, reg->iop2drv_doorbell_mask_reg);
648 }
649 break;
650 }
651 return orig_mask;
652}
653
654static void arcmsr_report_ccb_state(struct AdapterControlBlock *acb, \
655 struct CommandControlBlock *ccb, uint32_t flag_ccb)
656{
657
658 uint8_t id, lun;
659 id = ccb->pcmd->device->id;
660 lun = ccb->pcmd->device->lun;
661 if (!(flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR)) {
662 if (acb->devstate[id][lun] == ARECA_RAID_GONE)
663 acb->devstate[id][lun] = ARECA_RAID_GOOD;
664 ccb->pcmd->result = DID_OK << 16;
665 arcmsr_ccb_complete(ccb, 1);
666 } else {
667 switch (ccb->arcmsr_cdb.DeviceStatus) {
668 case ARCMSR_DEV_SELECT_TIMEOUT: {
669 acb->devstate[id][lun] = ARECA_RAID_GONE;
670 ccb->pcmd->result = DID_NO_CONNECT << 16;
671 arcmsr_ccb_complete(ccb, 1);
672 }
673 break;
674
675 case ARCMSR_DEV_ABORTED:
676
677 case ARCMSR_DEV_INIT_FAIL: {
678 acb->devstate[id][lun] = ARECA_RAID_GONE;
679 ccb->pcmd->result = DID_BAD_TARGET << 16;
680 arcmsr_ccb_complete(ccb, 1);
681 }
682 break;
683
684 case ARCMSR_DEV_CHECK_CONDITION: {
685 acb->devstate[id][lun] = ARECA_RAID_GOOD;
686 arcmsr_report_sense_info(ccb);
687 arcmsr_ccb_complete(ccb, 1);
688 }
689 break;
690
691 default:
692 printk(KERN_NOTICE
693 "arcmsr%d: scsi id = %d lun = %d"
694 " isr get command error done, "
695 "but got unknown DeviceStatus = 0x%x \n"
696 , acb->host->host_no
697 , id
698 , lun
699 , ccb->arcmsr_cdb.DeviceStatus);
700 acb->devstate[id][lun] = ARECA_RAID_GONE;
701 ccb->pcmd->result = DID_NO_CONNECT << 16;
702 arcmsr_ccb_complete(ccb, 1);
703 break;
704 }
705 }
706}
707
708static void arcmsr_drain_donequeue(struct AdapterControlBlock *acb, uint32_t flag_ccb)
709
710{
711 struct CommandControlBlock *ccb;
712
713 ccb = (struct CommandControlBlock *)(acb->vir2phy_offset + (flag_ccb << 5));
714 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
715 if (ccb->startdone == ARCMSR_CCB_ABORTED) {
716 struct scsi_cmnd *abortcmd = ccb->pcmd;
717 if (abortcmd) {
718 abortcmd->result |= DID_ABORT << 16;
719 arcmsr_ccb_complete(ccb, 1);
720 printk(KERN_NOTICE "arcmsr%d: ccb ='0x%p' \
721 isr got aborted command \n", acb->host->host_no, ccb);
722 }
723 }
724 printk(KERN_NOTICE "arcmsr%d: isr get an illegal ccb command \
725 done acb = '0x%p'"
726 "ccb = '0x%p' ccbacb = '0x%p' startdone = 0x%x"
727 " ccboutstandingcount = %d \n"
728 , acb->host->host_no
729 , acb
730 , ccb
731 , ccb->acb
732 , ccb->startdone
733 , atomic_read(&acb->ccboutstandingcount));
734 }
735 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
736}
737
738static void arcmsr_done4abort_postqueue(struct AdapterControlBlock *acb)
739{
740 int i = 0;
741 uint32_t flag_ccb;
742
743 switch (acb->adapter_type) {
744
745 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +0000746 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800747 uint32_t outbound_intstatus;
Al Viro80da1ad2007-10-29 05:08:28 +0000748 outbound_intstatus = readl(&reg->outbound_intstatus) &
Nick Cheng1a4f5502007-09-13 17:26:40 +0800749 acb->outbound_int_enable;
750 /*clear and abort all outbound posted Q*/
751 writel(outbound_intstatus, &reg->outbound_intstatus);/*clear interrupt*/
Al Viro80da1ad2007-10-29 05:08:28 +0000752 while (((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF)
Nick Cheng1a4f5502007-09-13 17:26:40 +0800753 && (i++ < ARCMSR_MAX_OUTSTANDING_CMD)) {
754 arcmsr_drain_donequeue(acb, flag_ccb);
755 }
756 }
757 break;
758
759 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +0000760 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800761 /*clear all outbound posted Q*/
762 for (i = 0; i < ARCMSR_MAX_HBB_POSTQUEUE; i++) {
763 if ((flag_ccb = readl(&reg->done_qbuffer[i])) != 0) {
764 writel(0, &reg->done_qbuffer[i]);
765 arcmsr_drain_donequeue(acb, flag_ccb);
766 }
767 writel(0, &reg->post_qbuffer[i]);
768 }
769 reg->doneq_index = 0;
770 reg->postq_index = 0;
771 }
772 break;
773 }
774}
Erich Chen1c57e862006-07-12 08:59:32 -0700775static void arcmsr_remove(struct pci_dev *pdev)
776{
777 struct Scsi_Host *host = pci_get_drvdata(pdev);
778 struct AdapterControlBlock *acb =
779 (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -0700780 int poll_count = 0;
781
782 arcmsr_free_sysfs_attr(acb);
783 scsi_remove_host(host);
784 arcmsr_stop_adapter_bgrb(acb);
785 arcmsr_flush_adapter_cache(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800786 arcmsr_disable_outbound_ints(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700787 acb->acb_flags |= ACB_F_SCSISTOPADAPTER;
788 acb->acb_flags &= ~ACB_F_IOP_INITED;
789
Nick Cheng1a4f5502007-09-13 17:26:40 +0800790 for (poll_count = 0; poll_count < ARCMSR_MAX_OUTSTANDING_CMD; poll_count++) {
Erich Chen1c57e862006-07-12 08:59:32 -0700791 if (!atomic_read(&acb->ccboutstandingcount))
792 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800793 arcmsr_interrupt(acb);/* FIXME: need spinlock */
Erich Chen1c57e862006-07-12 08:59:32 -0700794 msleep(25);
795 }
796
797 if (atomic_read(&acb->ccboutstandingcount)) {
798 int i;
799
800 arcmsr_abort_allcmd(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800801 arcmsr_done4abort_postqueue(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700802 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
803 struct CommandControlBlock *ccb = acb->pccb_pool[i];
804 if (ccb->startdone == ARCMSR_CCB_START) {
805 ccb->startdone = ARCMSR_CCB_ABORTED;
806 ccb->pcmd->result = DID_ABORT << 16;
807 arcmsr_ccb_complete(ccb, 1);
808 }
809 }
810 }
811
812 free_irq(pdev->irq, acb);
813 iounmap(acb->pmu);
814 arcmsr_free_ccb_pool(acb);
815 pci_release_regions(pdev);
816
817 scsi_host_put(host);
818
819 pci_disable_device(pdev);
820 pci_set_drvdata(pdev, NULL);
821}
822
823static void arcmsr_shutdown(struct pci_dev *pdev)
824{
825 struct Scsi_Host *host = pci_get_drvdata(pdev);
826 struct AdapterControlBlock *acb =
827 (struct AdapterControlBlock *)host->hostdata;
828
829 arcmsr_stop_adapter_bgrb(acb);
830 arcmsr_flush_adapter_cache(acb);
831}
832
833static int arcmsr_module_init(void)
834{
835 int error = 0;
836
837 error = pci_register_driver(&arcmsr_pci_driver);
838 return error;
839}
840
841static void arcmsr_module_exit(void)
842{
843 pci_unregister_driver(&arcmsr_pci_driver);
844}
845module_init(arcmsr_module_init);
846module_exit(arcmsr_module_exit);
847
Nick Cheng1a4f5502007-09-13 17:26:40 +0800848static void arcmsr_enable_outbound_ints(struct AdapterControlBlock *acb, \
849 u32 intmask_org)
Erich Chen1c57e862006-07-12 08:59:32 -0700850{
Erich Chen1c57e862006-07-12 08:59:32 -0700851 u32 mask;
852
Nick Cheng1a4f5502007-09-13 17:26:40 +0800853 switch (acb->adapter_type) {
854
855 case ACB_ADAPTER_TYPE_A : {
Al Viro80da1ad2007-10-29 05:08:28 +0000856 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800857 mask = intmask_org & ~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE |
Erich Chen1c57e862006-07-12 08:59:32 -0700858 ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800859 writel(mask, &reg->outbound_intmask);
860 acb->outbound_int_enable = ~(intmask_org & mask) & 0x000000ff;
861 }
862 break;
Erich Chen1c57e862006-07-12 08:59:32 -0700863
Nick Cheng1a4f5502007-09-13 17:26:40 +0800864 case ACB_ADAPTER_TYPE_B : {
Al Viro80da1ad2007-10-29 05:08:28 +0000865 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800866 mask = intmask_org | (ARCMSR_IOP2DRV_DATA_WRITE_OK | \
867 ARCMSR_IOP2DRV_DATA_READ_OK | ARCMSR_IOP2DRV_CDB_DONE);
868 writel(mask, reg->iop2drv_doorbell_mask_reg);
869 acb->outbound_int_enable = (intmask_org | mask) & 0x0000000f;
870 }
Erich Chen1c57e862006-07-12 08:59:32 -0700871 }
872}
873
Erich Chen1c57e862006-07-12 08:59:32 -0700874static void arcmsr_build_ccb(struct AdapterControlBlock *acb,
875 struct CommandControlBlock *ccb, struct scsi_cmnd *pcmd)
876{
877 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
878 int8_t *psge = (int8_t *)&arcmsr_cdb->u;
Al Viro80da1ad2007-10-29 05:08:28 +0000879 __le32 address_lo, address_hi;
Erich Chen1c57e862006-07-12 08:59:32 -0700880 int arccdbsize = 0x30;
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900881 int nseg;
Erich Chen1c57e862006-07-12 08:59:32 -0700882
883 ccb->pcmd = pcmd;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800884 memset(arcmsr_cdb, 0, sizeof(struct ARCMSR_CDB));
Erich Chen1c57e862006-07-12 08:59:32 -0700885 arcmsr_cdb->Bus = 0;
886 arcmsr_cdb->TargetID = pcmd->device->id;
887 arcmsr_cdb->LUN = pcmd->device->lun;
888 arcmsr_cdb->Function = 1;
889 arcmsr_cdb->CdbLength = (uint8_t)pcmd->cmd_len;
890 arcmsr_cdb->Context = (unsigned long)arcmsr_cdb;
891 memcpy(arcmsr_cdb->Cdb, pcmd->cmnd, pcmd->cmd_len);
Erich Chen1c57e862006-07-12 08:59:32 -0700892
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900893 nseg = scsi_dma_map(pcmd);
894 BUG_ON(nseg < 0);
895
896 if (nseg) {
Al Viro80da1ad2007-10-29 05:08:28 +0000897 __le32 length;
898 int i, cdb_sgcount = 0;
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900899 struct scatterlist *sg;
900
Erich Chen1c57e862006-07-12 08:59:32 -0700901 /* map stor port SG list to our iop SG List. */
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900902 scsi_for_each_sg(pcmd, sg, nseg, i) {
Erich Chen1c57e862006-07-12 08:59:32 -0700903 /* Get the physical address of the current data pointer */
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900904 length = cpu_to_le32(sg_dma_len(sg));
905 address_lo = cpu_to_le32(dma_addr_lo32(sg_dma_address(sg)));
906 address_hi = cpu_to_le32(dma_addr_hi32(sg_dma_address(sg)));
Erich Chen1c57e862006-07-12 08:59:32 -0700907 if (address_hi == 0) {
908 struct SG32ENTRY *pdma_sg = (struct SG32ENTRY *)psge;
909
910 pdma_sg->address = address_lo;
911 pdma_sg->length = length;
912 psge += sizeof (struct SG32ENTRY);
913 arccdbsize += sizeof (struct SG32ENTRY);
914 } else {
915 struct SG64ENTRY *pdma_sg = (struct SG64ENTRY *)psge;
916
917 pdma_sg->addresshigh = address_hi;
918 pdma_sg->address = address_lo;
919 pdma_sg->length = length|IS_SG64_ADDR;
920 psge += sizeof (struct SG64ENTRY);
921 arccdbsize += sizeof (struct SG64ENTRY);
922 }
Erich Chen1c57e862006-07-12 08:59:32 -0700923 cdb_sgcount++;
924 }
925 arcmsr_cdb->sgcount = (uint8_t)cdb_sgcount;
FUJITA Tomonorideff2622007-05-14 19:25:56 +0900926 arcmsr_cdb->DataLength = scsi_bufflen(pcmd);
Erich Chen1c57e862006-07-12 08:59:32 -0700927 if ( arccdbsize > 256)
928 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_SGL_BSIZE;
Erich Chen1c57e862006-07-12 08:59:32 -0700929 }
930 if (pcmd->sc_data_direction == DMA_TO_DEVICE ) {
931 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_WRITE;
932 ccb->ccb_flags |= CCB_FLAG_WRITE;
933 }
934}
935
936static void arcmsr_post_ccb(struct AdapterControlBlock *acb, struct CommandControlBlock *ccb)
937{
Erich Chen1c57e862006-07-12 08:59:32 -0700938 uint32_t cdb_shifted_phyaddr = ccb->cdb_shifted_phyaddr;
939 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
Erich Chen1c57e862006-07-12 08:59:32 -0700940 atomic_inc(&acb->ccboutstandingcount);
941 ccb->startdone = ARCMSR_CCB_START;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800942
943 switch (acb->adapter_type) {
944 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +0000945 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800946
947 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE)
948 writel(cdb_shifted_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,
Erich Chen1c57e862006-07-12 08:59:32 -0700949 &reg->inbound_queueport);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800950 else {
951 writel(cdb_shifted_phyaddr, &reg->inbound_queueport);
952 }
953 }
954 break;
955
956 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +0000957 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800958 uint32_t ending_index, index = reg->postq_index;
959
960 ending_index = ((index + 1) % ARCMSR_MAX_HBB_POSTQUEUE);
961 writel(0, &reg->post_qbuffer[ending_index]);
962 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE) {
963 writel(cdb_shifted_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,\
964 &reg->post_qbuffer[index]);
965 }
966 else {
967 writel(cdb_shifted_phyaddr, &reg->post_qbuffer[index]);
968 }
969 index++;
970 index %= ARCMSR_MAX_HBB_POSTQUEUE;/*if last index number set it to 0 */
971 reg->postq_index = index;
972 writel(ARCMSR_DRV2IOP_CDB_POSTED, reg->drv2iop_doorbell_reg);
973 }
974 break;
975 }
Erich Chen1c57e862006-07-12 08:59:32 -0700976}
977
Nick Cheng1a4f5502007-09-13 17:26:40 +0800978static void arcmsr_stop_hba_bgrb(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -0700979{
Al Viro80da1ad2007-10-29 05:08:28 +0000980 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800981 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
982 writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, &reg->inbound_msgaddr0);
Erich Chen1c57e862006-07-12 08:59:32 -0700983
Nick Cheng1a4f5502007-09-13 17:26:40 +0800984 if (arcmsr_hba_wait_msgint_ready(acb)) {
985 printk(KERN_NOTICE
986 "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
987 , acb->host->host_no);
988 }
989}
990
991static void arcmsr_stop_hbb_bgrb(struct AdapterControlBlock *acb)
992{
Al Viro80da1ad2007-10-29 05:08:28 +0000993 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800994 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
995 writel(ARCMSR_MESSAGE_STOP_BGRB, reg->drv2iop_doorbell_reg);
996
997 if (arcmsr_hbb_wait_msgint_ready(acb)) {
998 printk(KERN_NOTICE
999 "arcmsr%d: wait 'stop adapter background rebulid' timeout \n"
1000 , acb->host->host_no);
Erich Chen1c57e862006-07-12 08:59:32 -07001001 }
1002}
1003
1004static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb)
1005{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001006 switch (acb->adapter_type) {
1007 case ACB_ADAPTER_TYPE_A: {
1008 arcmsr_stop_hba_bgrb(acb);
1009 }
1010 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001011
Nick Cheng1a4f5502007-09-13 17:26:40 +08001012 case ACB_ADAPTER_TYPE_B: {
1013 arcmsr_stop_hbb_bgrb(acb);
1014 }
1015 break;
1016 }
Erich Chen1c57e862006-07-12 08:59:32 -07001017}
1018
1019static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb)
1020{
1021 dma_free_coherent(&acb->pdev->dev,
1022 ARCMSR_MAX_FREECCB_NUM * sizeof (struct CommandControlBlock) + 0x20,
1023 acb->dma_coherent,
1024 acb->dma_coherent_handle);
1025}
1026
Nick Cheng1a4f5502007-09-13 17:26:40 +08001027void arcmsr_iop_message_read(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07001028{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001029 switch (acb->adapter_type) {
1030 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001031 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001032 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, &reg->inbound_doorbell);
1033 }
1034 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001035
Nick Cheng1a4f5502007-09-13 17:26:40 +08001036 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001037 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001038 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell_reg);
1039 }
1040 break;
1041 }
1042}
1043
1044static void arcmsr_iop_message_wrote(struct AdapterControlBlock *acb)
1045{
1046 switch (acb->adapter_type) {
1047 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001048 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001049 /*
1050 ** push inbound doorbell tell iop, driver data write ok
1051 ** and wait reply on next hwinterrupt for next Qbuffer post
1052 */
1053 writel(ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK, &reg->inbound_doorbell);
1054 }
1055 break;
1056
1057 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001058 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001059 /*
1060 ** push inbound doorbell tell iop, driver data write ok
1061 ** and wait reply on next hwinterrupt for next Qbuffer post
1062 */
1063 writel(ARCMSR_DRV2IOP_DATA_WRITE_OK, reg->drv2iop_doorbell_reg);
1064 }
1065 break;
1066 }
1067}
1068
Al Viro80da1ad2007-10-29 05:08:28 +00001069struct QBUFFER __iomem *arcmsr_get_iop_rqbuffer(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08001070{
Al Viro80da1ad2007-10-29 05:08:28 +00001071 static struct QBUFFER __iomem *qbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001072
1073 switch (acb->adapter_type) {
1074
1075 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001076 struct MessageUnit_A __iomem *reg = acb->pmuA;
1077 qbuffer = (struct QBUFFER __iomem *)&reg->message_rbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001078 }
1079 break;
1080
1081 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001082 struct MessageUnit_B *reg = acb->pmuB;
1083 qbuffer = (struct QBUFFER __iomem *)reg->ioctl_rbuffer_reg;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001084 }
1085 break;
1086 }
1087 return qbuffer;
1088}
1089
Al Viro80da1ad2007-10-29 05:08:28 +00001090static struct QBUFFER __iomem *arcmsr_get_iop_wqbuffer(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08001091{
Al Viro80da1ad2007-10-29 05:08:28 +00001092 static struct QBUFFER __iomem *pqbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001093
1094 switch (acb->adapter_type) {
1095
1096 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001097 struct MessageUnit_A __iomem *reg = acb->pmuA;
1098 pqbuffer = (struct QBUFFER __iomem *) &reg->message_wbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001099 }
1100 break;
1101
1102 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001103 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001104 pqbuffer = (struct QBUFFER __iomem *)reg->ioctl_wbuffer_reg;
1105 }
1106 break;
1107 }
1108 return pqbuffer;
1109}
1110
1111static void arcmsr_iop2drv_data_wrote_handle(struct AdapterControlBlock *acb)
1112{
Al Viro80da1ad2007-10-29 05:08:28 +00001113 struct QBUFFER __iomem *prbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001114 struct QBUFFER *pQbuffer;
Al Viro80da1ad2007-10-29 05:08:28 +00001115 uint8_t __iomem *iop_data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001116 int32_t my_empty_len, iop_len, rqbuf_firstindex, rqbuf_lastindex;
1117
1118 rqbuf_lastindex = acb->rqbuf_lastindex;
1119 rqbuf_firstindex = acb->rqbuf_firstindex;
1120 prbuffer = arcmsr_get_iop_rqbuffer(acb);
Al Viro80da1ad2007-10-29 05:08:28 +00001121 iop_data = (uint8_t __iomem *)prbuffer->data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001122 iop_len = prbuffer->data_len;
1123 my_empty_len = (rqbuf_firstindex - rqbuf_lastindex -1)&(ARCMSR_MAX_QBUFFER -1);
1124
1125 if (my_empty_len >= iop_len)
1126 {
1127 while (iop_len > 0) {
1128 pQbuffer = (struct QBUFFER *)&acb->rqbuffer[rqbuf_lastindex];
1129 memcpy(pQbuffer, iop_data,1);
1130 rqbuf_lastindex++;
1131 rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1132 iop_data++;
1133 iop_len--;
1134 }
1135 acb->rqbuf_lastindex = rqbuf_lastindex;
1136 arcmsr_iop_message_read(acb);
1137 }
1138
1139 else {
1140 acb->acb_flags |= ACB_F_IOPDATA_OVERFLOW;
1141 }
1142}
1143
1144static void arcmsr_iop2drv_data_read_handle(struct AdapterControlBlock *acb)
1145{
1146 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_READED;
1147 if (acb->wqbuf_firstindex != acb->wqbuf_lastindex) {
1148 uint8_t *pQbuffer;
Al Viro80da1ad2007-10-29 05:08:28 +00001149 struct QBUFFER __iomem *pwbuffer;
1150 uint8_t __iomem *iop_data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001151 int32_t allxfer_len = 0;
1152
1153 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
1154 pwbuffer = arcmsr_get_iop_wqbuffer(acb);
1155 iop_data = (uint8_t __iomem *)pwbuffer->data;
1156
1157 while ((acb->wqbuf_firstindex != acb->wqbuf_lastindex) && \
1158 (allxfer_len < 124)) {
1159 pQbuffer = &acb->wqbuffer[acb->wqbuf_firstindex];
1160 memcpy(iop_data, pQbuffer, 1);
1161 acb->wqbuf_firstindex++;
1162 acb->wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1163 iop_data++;
1164 allxfer_len++;
1165 }
1166 pwbuffer->data_len = allxfer_len;
1167
1168 arcmsr_iop_message_wrote(acb);
1169 }
1170
1171 if (acb->wqbuf_firstindex == acb->wqbuf_lastindex) {
1172 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_CLEARED;
1173 }
1174}
1175
1176static void arcmsr_hba_doorbell_isr(struct AdapterControlBlock *acb)
1177{
1178 uint32_t outbound_doorbell;
Al Viro80da1ad2007-10-29 05:08:28 +00001179 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001180
1181 outbound_doorbell = readl(&reg->outbound_doorbell);
1182 writel(outbound_doorbell, &reg->outbound_doorbell);
1183 if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK) {
1184 arcmsr_iop2drv_data_wrote_handle(acb);
1185 }
1186
1187 if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_READ_OK) {
1188 arcmsr_iop2drv_data_read_handle(acb);
1189 }
1190}
1191
1192static void arcmsr_hba_postqueue_isr(struct AdapterControlBlock *acb)
1193{
1194 uint32_t flag_ccb;
Al Viro80da1ad2007-10-29 05:08:28 +00001195 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001196
1197 while ((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF) {
1198 arcmsr_drain_donequeue(acb, flag_ccb);
1199 }
1200}
1201
1202static void arcmsr_hbb_postqueue_isr(struct AdapterControlBlock *acb)
1203{
1204 uint32_t index;
1205 uint32_t flag_ccb;
Al Viro80da1ad2007-10-29 05:08:28 +00001206 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001207
1208 index = reg->doneq_index;
1209
1210 while ((flag_ccb = readl(&reg->done_qbuffer[index])) != 0) {
1211 writel(0, &reg->done_qbuffer[index]);
1212 arcmsr_drain_donequeue(acb, flag_ccb);
1213 index++;
1214 index %= ARCMSR_MAX_HBB_POSTQUEUE;
1215 reg->doneq_index = index;
1216 }
1217}
1218
1219static int arcmsr_handle_hba_isr(struct AdapterControlBlock *acb)
1220{
1221 uint32_t outbound_intstatus;
Al Viro80da1ad2007-10-29 05:08:28 +00001222 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001223
1224 outbound_intstatus = readl(&reg->outbound_intstatus) & \
1225 acb->outbound_int_enable;
1226 if (!(outbound_intstatus & ARCMSR_MU_OUTBOUND_HANDLE_INT)) {
1227 return 1;
1228 }
Erich Chen1c57e862006-07-12 08:59:32 -07001229 writel(outbound_intstatus, &reg->outbound_intstatus);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001230 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_DOORBELL_INT) {
1231 arcmsr_hba_doorbell_isr(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001232 }
1233 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_POSTQUEUE_INT) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001234 arcmsr_hba_postqueue_isr(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001235 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001236 return 0;
1237}
1238
1239static int arcmsr_handle_hbb_isr(struct AdapterControlBlock *acb)
1240{
1241 uint32_t outbound_doorbell;
Al Viro80da1ad2007-10-29 05:08:28 +00001242 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001243
1244 outbound_doorbell = readl(reg->iop2drv_doorbell_reg) & \
1245 acb->outbound_int_enable;
1246 if (!outbound_doorbell)
1247 return 1;
1248
1249 writel(~outbound_doorbell, reg->iop2drv_doorbell_reg);
1250
1251 if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_WRITE_OK) {
1252 arcmsr_iop2drv_data_wrote_handle(acb);
1253 }
1254 if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_READ_OK) {
1255 arcmsr_iop2drv_data_read_handle(acb);
1256 }
1257 if (outbound_doorbell & ARCMSR_IOP2DRV_CDB_DONE) {
1258 arcmsr_hbb_postqueue_isr(acb);
1259 }
1260
1261 return 0;
1262}
1263
1264static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb)
1265{
1266 switch (acb->adapter_type) {
1267 case ACB_ADAPTER_TYPE_A: {
1268 if (arcmsr_handle_hba_isr(acb)) {
1269 return IRQ_NONE;
1270 }
1271 }
1272 break;
1273
1274 case ACB_ADAPTER_TYPE_B: {
1275 if (arcmsr_handle_hbb_isr(acb)) {
1276 return IRQ_NONE;
1277 }
1278 }
1279 break;
1280 }
Erich Chen1c57e862006-07-12 08:59:32 -07001281 return IRQ_HANDLED;
1282}
1283
1284static void arcmsr_iop_parking(struct AdapterControlBlock *acb)
1285{
1286 if (acb) {
1287 /* stop adapter background rebuild */
1288 if (acb->acb_flags & ACB_F_MSG_START_BGRB) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001289 uint32_t intmask_org;
Erich Chen1c57e862006-07-12 08:59:32 -07001290 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001291 intmask_org = arcmsr_disable_outbound_ints(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001292 arcmsr_stop_adapter_bgrb(acb);
1293 arcmsr_flush_adapter_cache(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001294 arcmsr_enable_outbound_ints(acb, intmask_org);
Erich Chen1c57e862006-07-12 08:59:32 -07001295 }
1296 }
1297}
1298
Nick Cheng1a4f5502007-09-13 17:26:40 +08001299void arcmsr_post_ioctldata2iop(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07001300{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001301 int32_t wqbuf_firstindex, wqbuf_lastindex;
1302 uint8_t *pQbuffer;
Al Viro80da1ad2007-10-29 05:08:28 +00001303 struct QBUFFER __iomem *pwbuffer;
1304 uint8_t __iomem *iop_data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001305 int32_t allxfer_len = 0;
1306
1307 pwbuffer = arcmsr_get_iop_wqbuffer(acb);
1308 iop_data = (uint8_t __iomem *)pwbuffer->data;
1309 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_READED) {
1310 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
1311 wqbuf_firstindex = acb->wqbuf_firstindex;
1312 wqbuf_lastindex = acb->wqbuf_lastindex;
1313 while ((wqbuf_firstindex != wqbuf_lastindex) && (allxfer_len < 124)) {
1314 pQbuffer = &acb->wqbuffer[wqbuf_firstindex];
1315 memcpy(iop_data, pQbuffer, 1);
1316 wqbuf_firstindex++;
1317 wqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1318 iop_data++;
1319 allxfer_len++;
1320 }
1321 acb->wqbuf_firstindex = wqbuf_firstindex;
1322 pwbuffer->data_len = allxfer_len;
1323 arcmsr_iop_message_wrote(acb);
1324 }
1325}
1326
1327static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb, \
1328 struct scsi_cmnd *cmd)
1329{
Erich Chen1c57e862006-07-12 08:59:32 -07001330 struct CMD_MESSAGE_FIELD *pcmdmessagefld;
1331 int retvalue = 0, transfer_len = 0;
1332 char *buffer;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001333 struct scatterlist *sg;
Erich Chen1c57e862006-07-12 08:59:32 -07001334 uint32_t controlcode = (uint32_t ) cmd->cmnd[5] << 24 |
1335 (uint32_t ) cmd->cmnd[6] << 16 |
1336 (uint32_t ) cmd->cmnd[7] << 8 |
1337 (uint32_t ) cmd->cmnd[8];
Nick Cheng1a4f5502007-09-13 17:26:40 +08001338 /* 4 bytes: Areca io control code */
Erich Chen1c57e862006-07-12 08:59:32 -07001339
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001340 sg = scsi_sglist(cmd);
Jens Axboe45711f12007-10-22 21:19:53 +02001341 buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001342 if (scsi_sg_count(cmd) > 1) {
1343 retvalue = ARCMSR_MESSAGE_FAIL;
1344 goto message_out;
Erich Chen1c57e862006-07-12 08:59:32 -07001345 }
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001346 transfer_len += sg->length;
1347
Erich Chen1c57e862006-07-12 08:59:32 -07001348 if (transfer_len > sizeof(struct CMD_MESSAGE_FIELD)) {
1349 retvalue = ARCMSR_MESSAGE_FAIL;
1350 goto message_out;
1351 }
1352 pcmdmessagefld = (struct CMD_MESSAGE_FIELD *) buffer;
1353 switch(controlcode) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001354
Erich Chen1c57e862006-07-12 08:59:32 -07001355 case ARCMSR_MESSAGE_READ_RQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001356 unsigned long *ver_addr;
1357 dma_addr_t buf_handle;
1358 uint8_t *pQbuffer, *ptmpQbuffer;
1359 int32_t allxfer_len = 0;
Erich Chen1c57e862006-07-12 08:59:32 -07001360
Nick Cheng1a4f5502007-09-13 17:26:40 +08001361 ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
1362 if (!ver_addr) {
1363 retvalue = ARCMSR_MESSAGE_FAIL;
1364 goto message_out;
1365 }
1366 ptmpQbuffer = (uint8_t *) ver_addr;
1367 while ((acb->rqbuf_firstindex != acb->rqbuf_lastindex)
1368 && (allxfer_len < 1031)) {
1369 pQbuffer = &acb->rqbuffer[acb->rqbuf_firstindex];
1370 memcpy(ptmpQbuffer, pQbuffer, 1);
1371 acb->rqbuf_firstindex++;
1372 acb->rqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
1373 ptmpQbuffer++;
1374 allxfer_len++;
1375 }
1376 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
Erich Chen1c57e862006-07-12 08:59:32 -07001377
Al Viro80da1ad2007-10-29 05:08:28 +00001378 struct QBUFFER __iomem *prbuffer;
1379 uint8_t __iomem *iop_data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001380 int32_t iop_len;
1381
1382 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1383 prbuffer = arcmsr_get_iop_rqbuffer(acb);
Al Viro80da1ad2007-10-29 05:08:28 +00001384 iop_data = prbuffer->data;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001385 iop_len = readl(&prbuffer->data_len);
1386 while (iop_len > 0) {
1387 acb->rqbuffer[acb->rqbuf_lastindex] = readb(iop_data);
1388 acb->rqbuf_lastindex++;
1389 acb->rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1390 iop_data++;
1391 iop_len--;
Erich Chen1c57e862006-07-12 08:59:32 -07001392 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001393 arcmsr_iop_message_read(acb);
1394 }
1395 memcpy(pcmdmessagefld->messagedatabuffer, (uint8_t *)ver_addr, allxfer_len);
1396 pcmdmessagefld->cmdmessage.Length = allxfer_len;
1397 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
1398 pci_free_consistent(acb->pdev, 1032, ver_addr, buf_handle);
Erich Chen1c57e862006-07-12 08:59:32 -07001399 }
1400 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001401
Nick Cheng1a4f5502007-09-13 17:26:40 +08001402 case ARCMSR_MESSAGE_WRITE_WQBUFFER: {
1403 unsigned long *ver_addr;
1404 dma_addr_t buf_handle;
1405 int32_t my_empty_len, user_len, wqbuf_firstindex, wqbuf_lastindex;
1406 uint8_t *pQbuffer, *ptmpuserbuffer;
1407
1408 ver_addr = pci_alloc_consistent(acb->pdev, 1032, &buf_handle);
1409 if (!ver_addr) {
1410 retvalue = ARCMSR_MESSAGE_FAIL;
1411 goto message_out;
1412 }
1413 ptmpuserbuffer = (uint8_t *)ver_addr;
1414 user_len = pcmdmessagefld->cmdmessage.Length;
1415 memcpy(ptmpuserbuffer, pcmdmessagefld->messagedatabuffer, user_len);
1416 wqbuf_lastindex = acb->wqbuf_lastindex;
1417 wqbuf_firstindex = acb->wqbuf_firstindex;
1418 if (wqbuf_lastindex != wqbuf_firstindex) {
1419 struct SENSE_DATA *sensebuffer =
1420 (struct SENSE_DATA *)cmd->sense_buffer;
1421 arcmsr_post_ioctldata2iop(acb);
1422 /* has error report sensedata */
1423 sensebuffer->ErrorCode = 0x70;
1424 sensebuffer->SenseKey = ILLEGAL_REQUEST;
1425 sensebuffer->AdditionalSenseLength = 0x0A;
1426 sensebuffer->AdditionalSenseCode = 0x20;
1427 sensebuffer->Valid = 1;
1428 retvalue = ARCMSR_MESSAGE_FAIL;
1429 } else {
1430 my_empty_len = (wqbuf_firstindex-wqbuf_lastindex - 1)
1431 &(ARCMSR_MAX_QBUFFER - 1);
1432 if (my_empty_len >= user_len) {
1433 while (user_len > 0) {
1434 pQbuffer =
1435 &acb->wqbuffer[acb->wqbuf_lastindex];
1436 memcpy(pQbuffer, ptmpuserbuffer, 1);
1437 acb->wqbuf_lastindex++;
1438 acb->wqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
1439 ptmpuserbuffer++;
1440 user_len--;
1441 }
1442 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {
1443 acb->acb_flags &=
1444 ~ACB_F_MESSAGE_WQBUFFER_CLEARED;
1445 arcmsr_post_ioctldata2iop(acb);
1446 }
1447 } else {
1448 /* has error report sensedata */
Erich Chen1c57e862006-07-12 08:59:32 -07001449 struct SENSE_DATA *sensebuffer =
1450 (struct SENSE_DATA *)cmd->sense_buffer;
Erich Chen1c57e862006-07-12 08:59:32 -07001451 sensebuffer->ErrorCode = 0x70;
1452 sensebuffer->SenseKey = ILLEGAL_REQUEST;
1453 sensebuffer->AdditionalSenseLength = 0x0A;
1454 sensebuffer->AdditionalSenseCode = 0x20;
1455 sensebuffer->Valid = 1;
1456 retvalue = ARCMSR_MESSAGE_FAIL;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001457 }
Erich Chen1c57e862006-07-12 08:59:32 -07001458 }
1459 pci_free_consistent(acb->pdev, 1032, ver_addr, buf_handle);
1460 }
1461 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001462
Erich Chen1c57e862006-07-12 08:59:32 -07001463 case ARCMSR_MESSAGE_CLEAR_RQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001464 uint8_t *pQbuffer = acb->rqbuffer;
Erich Chen1c57e862006-07-12 08:59:32 -07001465
Nick Cheng1a4f5502007-09-13 17:26:40 +08001466 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1467 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1468 arcmsr_iop_message_read(acb);
1469 }
1470 acb->acb_flags |= ACB_F_MESSAGE_RQBUFFER_CLEARED;
1471 acb->rqbuf_firstindex = 0;
1472 acb->rqbuf_lastindex = 0;
1473 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1474 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07001475 }
1476 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001477
Erich Chen1c57e862006-07-12 08:59:32 -07001478 case ARCMSR_MESSAGE_CLEAR_WQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001479 uint8_t *pQbuffer = acb->wqbuffer;
Erich Chen1c57e862006-07-12 08:59:32 -07001480
Nick Cheng1a4f5502007-09-13 17:26:40 +08001481 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1482 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1483 arcmsr_iop_message_read(acb);
1484 }
1485 acb->acb_flags |=
1486 (ACB_F_MESSAGE_WQBUFFER_CLEARED |
1487 ACB_F_MESSAGE_WQBUFFER_READED);
1488 acb->wqbuf_firstindex = 0;
1489 acb->wqbuf_lastindex = 0;
1490 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1491 pcmdmessagefld->cmdmessage.ReturnCode =
1492 ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07001493 }
1494 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001495
Erich Chen1c57e862006-07-12 08:59:32 -07001496 case ARCMSR_MESSAGE_CLEAR_ALLQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001497 uint8_t *pQbuffer;
Erich Chen1c57e862006-07-12 08:59:32 -07001498
Nick Cheng1a4f5502007-09-13 17:26:40 +08001499 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
1500 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1501 arcmsr_iop_message_read(acb);
1502 }
1503 acb->acb_flags |=
1504 (ACB_F_MESSAGE_WQBUFFER_CLEARED
1505 | ACB_F_MESSAGE_RQBUFFER_CLEARED
1506 | ACB_F_MESSAGE_WQBUFFER_READED);
1507 acb->rqbuf_firstindex = 0;
1508 acb->rqbuf_lastindex = 0;
1509 acb->wqbuf_firstindex = 0;
1510 acb->wqbuf_lastindex = 0;
1511 pQbuffer = acb->rqbuffer;
1512 memset(pQbuffer, 0, sizeof(struct QBUFFER));
1513 pQbuffer = acb->wqbuffer;
1514 memset(pQbuffer, 0, sizeof(struct QBUFFER));
1515 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07001516 }
1517 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001518
Erich Chen1c57e862006-07-12 08:59:32 -07001519 case ARCMSR_MESSAGE_RETURN_CODE_3F: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001520 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_3F;
Erich Chen1c57e862006-07-12 08:59:32 -07001521 }
1522 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001523
Nick Cheng1a4f5502007-09-13 17:26:40 +08001524 case ARCMSR_MESSAGE_SAY_HELLO: {
1525 int8_t *hello_string = "Hello! I am ARCMSR";
1526
1527 memcpy(pcmdmessagefld->messagedatabuffer, hello_string
1528 , (int16_t)strlen(hello_string));
1529 pcmdmessagefld->cmdmessage.ReturnCode = ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07001530 }
1531 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001532
Erich Chen1c57e862006-07-12 08:59:32 -07001533 case ARCMSR_MESSAGE_SAY_GOODBYE:
1534 arcmsr_iop_parking(acb);
1535 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001536
Erich Chen1c57e862006-07-12 08:59:32 -07001537 case ARCMSR_MESSAGE_FLUSH_ADAPTER_CACHE:
1538 arcmsr_flush_adapter_cache(acb);
1539 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001540
Erich Chen1c57e862006-07-12 08:59:32 -07001541 default:
1542 retvalue = ARCMSR_MESSAGE_FAIL;
1543 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001544 message_out:
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001545 sg = scsi_sglist(cmd);
1546 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
Erich Chen1c57e862006-07-12 08:59:32 -07001547 return retvalue;
1548}
1549
1550static struct CommandControlBlock *arcmsr_get_freeccb(struct AdapterControlBlock *acb)
1551{
1552 struct list_head *head = &acb->ccb_free_list;
1553 struct CommandControlBlock *ccb = NULL;
1554
1555 if (!list_empty(head)) {
1556 ccb = list_entry(head->next, struct CommandControlBlock, list);
1557 list_del(head->next);
1558 }
1559 return ccb;
1560}
1561
1562static void arcmsr_handle_virtual_command(struct AdapterControlBlock *acb,
1563 struct scsi_cmnd *cmd)
1564{
1565 switch (cmd->cmnd[0]) {
1566 case INQUIRY: {
1567 unsigned char inqdata[36];
1568 char *buffer;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001569 struct scatterlist *sg;
Erich Chen1c57e862006-07-12 08:59:32 -07001570
1571 if (cmd->device->lun) {
1572 cmd->result = (DID_TIME_OUT << 16);
1573 cmd->scsi_done(cmd);
1574 return;
1575 }
1576 inqdata[0] = TYPE_PROCESSOR;
1577 /* Periph Qualifier & Periph Dev Type */
1578 inqdata[1] = 0;
1579 /* rem media bit & Dev Type Modifier */
1580 inqdata[2] = 0;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001581 /* ISO, ECMA, & ANSI versions */
Erich Chen1c57e862006-07-12 08:59:32 -07001582 inqdata[4] = 31;
1583 /* length of additional data */
1584 strncpy(&inqdata[8], "Areca ", 8);
1585 /* Vendor Identification */
1586 strncpy(&inqdata[16], "RAID controller ", 16);
1587 /* Product Identification */
1588 strncpy(&inqdata[32], "R001", 4); /* Product Revision */
Erich Chen1c57e862006-07-12 08:59:32 -07001589
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001590 sg = scsi_sglist(cmd);
Jens Axboe45711f12007-10-22 21:19:53 +02001591 buffer = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001592
Erich Chen1c57e862006-07-12 08:59:32 -07001593 memcpy(buffer, inqdata, sizeof(inqdata));
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001594 sg = scsi_sglist(cmd);
1595 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
Erich Chen1c57e862006-07-12 08:59:32 -07001596
Erich Chen1c57e862006-07-12 08:59:32 -07001597 cmd->scsi_done(cmd);
1598 }
1599 break;
1600 case WRITE_BUFFER:
1601 case READ_BUFFER: {
1602 if (arcmsr_iop_message_xfer(acb, cmd))
1603 cmd->result = (DID_ERROR << 16);
1604 cmd->scsi_done(cmd);
1605 }
1606 break;
1607 default:
1608 cmd->scsi_done(cmd);
1609 }
1610}
1611
1612static int arcmsr_queue_command(struct scsi_cmnd *cmd,
1613 void (* done)(struct scsi_cmnd *))
1614{
1615 struct Scsi_Host *host = cmd->device->host;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001616 struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -07001617 struct CommandControlBlock *ccb;
1618 int target = cmd->device->id;
1619 int lun = cmd->device->lun;
1620
1621 cmd->scsi_done = done;
1622 cmd->host_scribble = NULL;
1623 cmd->result = 0;
1624 if (acb->acb_flags & ACB_F_BUS_RESET) {
1625 printk(KERN_NOTICE "arcmsr%d: bus reset"
1626 " and return busy \n"
1627 , acb->host->host_no);
1628 return SCSI_MLQUEUE_HOST_BUSY;
1629 }
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001630 if (target == 16) {
Erich Chen1c57e862006-07-12 08:59:32 -07001631 /* virtual device for iop message transfer */
1632 arcmsr_handle_virtual_command(acb, cmd);
1633 return 0;
1634 }
1635 if (acb->devstate[target][lun] == ARECA_RAID_GONE) {
1636 uint8_t block_cmd;
1637
1638 block_cmd = cmd->cmnd[0] & 0x0f;
1639 if (block_cmd == 0x08 || block_cmd == 0x0a) {
1640 printk(KERN_NOTICE
1641 "arcmsr%d: block 'read/write'"
1642 "command with gone raid volume"
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001643 " Cmd = %2x, TargetId = %d, Lun = %d \n"
Erich Chen1c57e862006-07-12 08:59:32 -07001644 , acb->host->host_no
1645 , cmd->cmnd[0]
1646 , target, lun);
1647 cmd->result = (DID_NO_CONNECT << 16);
1648 cmd->scsi_done(cmd);
1649 return 0;
1650 }
1651 }
1652 if (atomic_read(&acb->ccboutstandingcount) >=
1653 ARCMSR_MAX_OUTSTANDING_CMD)
1654 return SCSI_MLQUEUE_HOST_BUSY;
1655
1656 ccb = arcmsr_get_freeccb(acb);
1657 if (!ccb)
1658 return SCSI_MLQUEUE_HOST_BUSY;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001659
Erich Chen1c57e862006-07-12 08:59:32 -07001660 arcmsr_build_ccb(acb, ccb, cmd);
1661 arcmsr_post_ccb(acb, ccb);
1662 return 0;
1663}
1664
Nick Cheng1a4f5502007-09-13 17:26:40 +08001665static void arcmsr_get_hba_config(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07001666{
Al Viro80da1ad2007-10-29 05:08:28 +00001667 struct MessageUnit_A __iomem *reg = acb->pmuA;
Erich Chen1c57e862006-07-12 08:59:32 -07001668 char *acb_firm_model = acb->firm_model;
1669 char *acb_firm_version = acb->firm_version;
Al Viro80da1ad2007-10-29 05:08:28 +00001670 char __iomem *iop_firm_model = (char __iomem *)(&reg->message_rwbuffer[15]);
1671 char __iomem *iop_firm_version = (char __iomem *)(&reg->message_rwbuffer[17]);
Erich Chen1c57e862006-07-12 08:59:32 -07001672 int count;
1673
1674 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001675 if (arcmsr_hba_wait_msgint_ready(acb)) {
1676 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
1677 miscellaneous data' timeout \n", acb->host->host_no);
1678 }
1679
Erich Chen1c57e862006-07-12 08:59:32 -07001680 count = 8;
1681 while (count) {
1682 *acb_firm_model = readb(iop_firm_model);
1683 acb_firm_model++;
1684 iop_firm_model++;
1685 count--;
1686 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001687
Erich Chen1c57e862006-07-12 08:59:32 -07001688 count = 16;
1689 while (count) {
1690 *acb_firm_version = readb(iop_firm_version);
1691 acb_firm_version++;
1692 iop_firm_version++;
1693 count--;
1694 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001695
1696 printk(KERN_INFO "ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n"
Erich Chen1c57e862006-07-12 08:59:32 -07001697 , acb->host->host_no
1698 , acb->firm_version);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001699
Erich Chen1c57e862006-07-12 08:59:32 -07001700 acb->firm_request_len = readl(&reg->message_rwbuffer[1]);
1701 acb->firm_numbers_queue = readl(&reg->message_rwbuffer[2]);
1702 acb->firm_sdram_size = readl(&reg->message_rwbuffer[3]);
1703 acb->firm_hd_channels = readl(&reg->message_rwbuffer[4]);
1704}
1705
Nick Cheng1a4f5502007-09-13 17:26:40 +08001706static void arcmsr_get_hbb_config(struct AdapterControlBlock *acb)
1707{
Al Viro80da1ad2007-10-29 05:08:28 +00001708 struct MessageUnit_B *reg = acb->pmuB;
1709 uint32_t __iomem *lrwbuffer = reg->msgcode_rwbuffer_reg;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001710 char *acb_firm_model = acb->firm_model;
1711 char *acb_firm_version = acb->firm_version;
Al Viro80da1ad2007-10-29 05:08:28 +00001712 char __iomem *iop_firm_model = (char __iomem *)(&lrwbuffer[15]);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001713 /*firm_model,15,60-67*/
Al Viro80da1ad2007-10-29 05:08:28 +00001714 char __iomem *iop_firm_version = (char __iomem *)(&lrwbuffer[17]);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001715 /*firm_version,17,68-83*/
1716 int count;
1717
1718 writel(ARCMSR_MESSAGE_GET_CONFIG, reg->drv2iop_doorbell_reg);
1719 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1720 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
1721 miscellaneous data' timeout \n", acb->host->host_no);
1722 }
1723
1724 count = 8;
1725 while (count)
1726 {
1727 *acb_firm_model = readb(iop_firm_model);
1728 acb_firm_model++;
1729 iop_firm_model++;
1730 count--;
1731 }
1732
1733 count = 16;
1734 while (count)
1735 {
1736 *acb_firm_version = readb(iop_firm_version);
1737 acb_firm_version++;
1738 iop_firm_version++;
1739 count--;
1740 }
1741
1742 printk(KERN_INFO "ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n",
1743 acb->host->host_no,
1744 acb->firm_version);
1745
1746 lrwbuffer++;
1747 acb->firm_request_len = readl(lrwbuffer++);
1748 /*firm_request_len,1,04-07*/
1749 acb->firm_numbers_queue = readl(lrwbuffer++);
1750 /*firm_numbers_queue,2,08-11*/
1751 acb->firm_sdram_size = readl(lrwbuffer++);
1752 /*firm_sdram_size,3,12-15*/
1753 acb->firm_hd_channels = readl(lrwbuffer);
1754 /*firm_ide_channels,4,16-19*/
1755}
1756
1757static void arcmsr_get_firmware_spec(struct AdapterControlBlock *acb)
1758{
1759 switch (acb->adapter_type) {
1760 case ACB_ADAPTER_TYPE_A: {
1761 arcmsr_get_hba_config(acb);
1762 }
1763 break;
1764
1765 case ACB_ADAPTER_TYPE_B: {
1766 arcmsr_get_hbb_config(acb);
1767 }
1768 break;
1769 }
1770}
1771
1772static void arcmsr_polling_hba_ccbdone(struct AdapterControlBlock *acb,
Erich Chen1c57e862006-07-12 08:59:32 -07001773 struct CommandControlBlock *poll_ccb)
1774{
Al Viro80da1ad2007-10-29 05:08:28 +00001775 struct MessageUnit_A __iomem *reg = acb->pmuA;
Erich Chen1c57e862006-07-12 08:59:32 -07001776 struct CommandControlBlock *ccb;
1777 uint32_t flag_ccb, outbound_intstatus, poll_ccb_done = 0, poll_count = 0;
Erich Chen1c57e862006-07-12 08:59:32 -07001778
Nick Cheng1a4f5502007-09-13 17:26:40 +08001779 polling_hba_ccb_retry:
Erich Chen1c57e862006-07-12 08:59:32 -07001780 poll_count++;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001781 outbound_intstatus = readl(&reg->outbound_intstatus) & acb->outbound_int_enable;
Erich Chen1c57e862006-07-12 08:59:32 -07001782 writel(outbound_intstatus, &reg->outbound_intstatus);/*clear interrupt*/
1783 while (1) {
1784 if ((flag_ccb = readl(&reg->outbound_queueport)) == 0xFFFFFFFF) {
1785 if (poll_ccb_done)
1786 break;
1787 else {
1788 msleep(25);
1789 if (poll_count > 100)
1790 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001791 goto polling_hba_ccb_retry;
Erich Chen1c57e862006-07-12 08:59:32 -07001792 }
1793 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001794 ccb = (struct CommandControlBlock *)(acb->vir2phy_offset + (flag_ccb << 5));
1795 poll_ccb_done = (ccb == poll_ccb) ? 1:0;
1796 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
1797 if ((ccb->startdone == ARCMSR_CCB_ABORTED) || (ccb == poll_ccb)) {
1798 printk(KERN_NOTICE "arcmsr%d: scsi id = %d lun = %d ccb = '0x%p'"
Erich Chen1c57e862006-07-12 08:59:32 -07001799 " poll command abort successfully \n"
1800 , acb->host->host_no
1801 , ccb->pcmd->device->id
1802 , ccb->pcmd->device->lun
1803 , ccb);
1804 ccb->pcmd->result = DID_ABORT << 16;
1805 arcmsr_ccb_complete(ccb, 1);
1806 poll_ccb_done = 1;
1807 continue;
1808 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001809 printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
1810 " command done ccb = '0x%p'"
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001811 "ccboutstandingcount = %d \n"
Erich Chen1c57e862006-07-12 08:59:32 -07001812 , acb->host->host_no
1813 , ccb
1814 , atomic_read(&acb->ccboutstandingcount));
1815 continue;
1816 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001817 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
1818 }
1819}
1820
1821static void arcmsr_polling_hbb_ccbdone(struct AdapterControlBlock *acb, \
1822 struct CommandControlBlock *poll_ccb)
1823{
Al Viro80da1ad2007-10-29 05:08:28 +00001824 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001825 struct CommandControlBlock *ccb;
1826 uint32_t flag_ccb, poll_ccb_done = 0, poll_count = 0;
1827 int index;
1828
1829 polling_hbb_ccb_retry:
1830 poll_count++;
1831 /* clear doorbell interrupt */
1832 writel(ARCMSR_DOORBELL_INT_CLEAR_PATTERN, reg->iop2drv_doorbell_reg);
1833 while (1) {
1834 index = reg->doneq_index;
1835 if ((flag_ccb = readl(&reg->done_qbuffer[index])) == 0) {
1836 if (poll_ccb_done)
1837 break;
1838 else {
1839 msleep(25);
1840 if (poll_count > 100)
1841 break;
1842 goto polling_hbb_ccb_retry;
Erich Chen1c57e862006-07-12 08:59:32 -07001843 }
Erich Chen1c57e862006-07-12 08:59:32 -07001844 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001845 writel(0, &reg->done_qbuffer[index]);
1846 index++;
1847 /*if last index number set it to 0 */
1848 index %= ARCMSR_MAX_HBB_POSTQUEUE;
1849 reg->doneq_index = index;
1850 /* check ifcommand done with no error*/
1851 ccb = (struct CommandControlBlock *)\
1852 (acb->vir2phy_offset + (flag_ccb << 5));/*frame must be 32 bytes aligned*/
1853 poll_ccb_done = (ccb == poll_ccb) ? 1:0;
1854 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
1855 if (ccb->startdone == ARCMSR_CCB_ABORTED) {
1856 printk(KERN_NOTICE "arcmsr%d: \
1857 scsi id = %d lun = %d ccb = '0x%p' poll command abort successfully \n"
1858 ,acb->host->host_no
1859 ,ccb->pcmd->device->id
1860 ,ccb->pcmd->device->lun
1861 ,ccb);
1862 ccb->pcmd->result = DID_ABORT << 16;
1863 arcmsr_ccb_complete(ccb, 1);
1864 continue;
1865 }
1866 printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
1867 " command done ccb = '0x%p'"
1868 "ccboutstandingcount = %d \n"
1869 , acb->host->host_no
1870 , ccb
1871 , atomic_read(&acb->ccboutstandingcount));
1872 continue;
1873 }
1874 arcmsr_report_ccb_state(acb, ccb, flag_ccb);
1875 } /*drain reply FIFO*/
1876}
1877
1878static void arcmsr_polling_ccbdone(struct AdapterControlBlock *acb, \
1879 struct CommandControlBlock *poll_ccb)
1880{
1881 switch (acb->adapter_type) {
1882
1883 case ACB_ADAPTER_TYPE_A: {
1884 arcmsr_polling_hba_ccbdone(acb,poll_ccb);
1885 }
1886 break;
1887
1888 case ACB_ADAPTER_TYPE_B: {
1889 arcmsr_polling_hbb_ccbdone(acb,poll_ccb);
Erich Chen1c57e862006-07-12 08:59:32 -07001890 }
1891 }
1892}
Nick Cheng1a4f5502007-09-13 17:26:40 +08001893
1894static int arcmsr_iop_confirm(struct AdapterControlBlock *acb)
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001895{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001896 uint32_t cdb_phyaddr, ccb_phyaddr_hi32;
1897 dma_addr_t dma_coherent_handle;
1898 /*
1899 ********************************************************************
1900 ** here we need to tell iop 331 our freeccb.HighPart
1901 ** if freeccb.HighPart is not zero
1902 ********************************************************************
1903 */
1904 dma_coherent_handle = acb->dma_coherent_handle;
1905 cdb_phyaddr = (uint32_t)(dma_coherent_handle);
1906 ccb_phyaddr_hi32 = (uint32_t)((cdb_phyaddr >> 16) >> 16);
1907 /*
1908 ***********************************************************************
1909 ** if adapter type B, set window of "post command Q"
1910 ***********************************************************************
1911 */
1912 switch (acb->adapter_type) {
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001913
Nick Cheng1a4f5502007-09-13 17:26:40 +08001914 case ACB_ADAPTER_TYPE_A: {
1915 if (ccb_phyaddr_hi32 != 0) {
Al Viro80da1ad2007-10-29 05:08:28 +00001916 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001917 uint32_t intmask_org;
1918 intmask_org = arcmsr_disable_outbound_ints(acb);
1919 writel(ARCMSR_SIGNATURE_SET_CONFIG, \
1920 &reg->message_rwbuffer[0]);
1921 writel(ccb_phyaddr_hi32, &reg->message_rwbuffer[1]);
1922 writel(ARCMSR_INBOUND_MESG0_SET_CONFIG, \
1923 &reg->inbound_msgaddr0);
1924 if (arcmsr_hba_wait_msgint_ready(acb)) {
1925 printk(KERN_NOTICE "arcmsr%d: ""set ccb high \
1926 part physical address timeout\n",
1927 acb->host->host_no);
1928 return 1;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001929 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001930 arcmsr_enable_outbound_ints(acb, intmask_org);
1931 }
1932 }
1933 break;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001934
Nick Cheng1a4f5502007-09-13 17:26:40 +08001935 case ACB_ADAPTER_TYPE_B: {
1936 unsigned long post_queue_phyaddr;
Al Viro80da1ad2007-10-29 05:08:28 +00001937 uint32_t __iomem *rwbuffer;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001938
Al Viro80da1ad2007-10-29 05:08:28 +00001939 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001940 uint32_t intmask_org;
1941 intmask_org = arcmsr_disable_outbound_ints(acb);
1942 reg->postq_index = 0;
1943 reg->doneq_index = 0;
1944 writel(ARCMSR_MESSAGE_SET_POST_WINDOW, reg->drv2iop_doorbell_reg);
1945 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1946 printk(KERN_NOTICE "arcmsr%d:can not set diver mode\n", \
1947 acb->host->host_no);
1948 return 1;
1949 }
1950 post_queue_phyaddr = cdb_phyaddr + ARCMSR_MAX_FREECCB_NUM * \
1951 sizeof(struct CommandControlBlock) + offsetof(struct MessageUnit_B, post_qbuffer) ;
1952 rwbuffer = reg->msgcode_rwbuffer_reg;
1953 /* driver "set config" signature */
1954 writel(ARCMSR_SIGNATURE_SET_CONFIG, rwbuffer++);
1955 /* normal should be zero */
1956 writel(ccb_phyaddr_hi32, rwbuffer++);
1957 /* postQ size (256 + 8)*4 */
1958 writel(post_queue_phyaddr, rwbuffer++);
1959 /* doneQ size (256 + 8)*4 */
1960 writel(post_queue_phyaddr + 1056, rwbuffer++);
1961 /* ccb maxQ size must be --> [(256 + 8)*4]*/
1962 writel(1056, rwbuffer);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001963
Nick Cheng1a4f5502007-09-13 17:26:40 +08001964 writel(ARCMSR_MESSAGE_SET_CONFIG, reg->drv2iop_doorbell_reg);
1965 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1966 printk(KERN_NOTICE "arcmsr%d: 'set command Q window' \
1967 timeout \n",acb->host->host_no);
1968 return 1;
1969 }
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001970
Nick Cheng1a4f5502007-09-13 17:26:40 +08001971 writel(ARCMSR_MESSAGE_START_DRIVER_MODE, reg->drv2iop_doorbell_reg);
1972 if (arcmsr_hbb_wait_msgint_ready(acb)) {
1973 printk(KERN_NOTICE "arcmsr%d: 'can not set diver mode \n"\
1974 ,acb->host->host_no);
1975 return 1;
1976 }
1977 arcmsr_enable_outbound_ints(acb, intmask_org);
1978 }
1979 break;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001980 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001981 return 0;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08001982}
1983
Nick Cheng1a4f5502007-09-13 17:26:40 +08001984static void arcmsr_wait_firmware_ready(struct AdapterControlBlock *acb)
1985{
1986 uint32_t firmware_state = 0;
1987
1988 switch (acb->adapter_type) {
1989
1990 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001991 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001992 do {
1993 firmware_state = readl(&reg->outbound_msgaddr1);
1994 } while ((firmware_state & ARCMSR_OUTBOUND_MESG1_FIRMWARE_OK) == 0);
1995 }
1996 break;
1997
1998 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001999 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002000 do {
2001 firmware_state = readl(reg->iop2drv_doorbell_reg);
2002 } while ((firmware_state & ARCMSR_MESSAGE_FIRMWARE_OK) == 0);
2003 }
2004 break;
2005 }
2006}
2007
2008static void arcmsr_start_hba_bgrb(struct AdapterControlBlock *acb)
2009{
Al Viro80da1ad2007-10-29 05:08:28 +00002010 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002011 acb->acb_flags |= ACB_F_MSG_START_BGRB;
2012 writel(ARCMSR_INBOUND_MESG0_START_BGRB, &reg->inbound_msgaddr0);
2013 if (arcmsr_hba_wait_msgint_ready(acb)) {
2014 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
2015 rebulid' timeout \n", acb->host->host_no);
2016 }
2017}
2018
2019static void arcmsr_start_hbb_bgrb(struct AdapterControlBlock *acb)
2020{
Al Viro80da1ad2007-10-29 05:08:28 +00002021 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002022 acb->acb_flags |= ACB_F_MSG_START_BGRB;
2023 writel(ARCMSR_MESSAGE_START_BGRB, reg->drv2iop_doorbell_reg);
2024 if (arcmsr_hbb_wait_msgint_ready(acb)) {
2025 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
2026 rebulid' timeout \n",acb->host->host_no);
2027 }
2028}
2029
2030static void arcmsr_start_adapter_bgrb(struct AdapterControlBlock *acb)
2031{
2032 switch (acb->adapter_type) {
2033 case ACB_ADAPTER_TYPE_A:
2034 arcmsr_start_hba_bgrb(acb);
2035 break;
2036 case ACB_ADAPTER_TYPE_B:
2037 arcmsr_start_hbb_bgrb(acb);
2038 break;
2039 }
2040}
2041
2042static void arcmsr_clear_doorbell_queue_buffer(struct AdapterControlBlock *acb)
2043{
2044 switch (acb->adapter_type) {
2045 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00002046 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002047 uint32_t outbound_doorbell;
2048 /* empty doorbell Qbuffer if door bell ringed */
2049 outbound_doorbell = readl(&reg->outbound_doorbell);
2050 /*clear doorbell interrupt */
2051 writel(outbound_doorbell, &reg->outbound_doorbell);
2052 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, &reg->inbound_doorbell);
2053 }
2054 break;
2055
2056 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00002057 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002058 /*clear interrupt and message state*/
2059 writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN, reg->iop2drv_doorbell_reg);
2060 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell_reg);
2061 /* let IOP know data has been read */
2062 }
2063 break;
2064 }
2065}
Erich Chen1c57e862006-07-12 08:59:32 -07002066
2067static void arcmsr_iop_init(struct AdapterControlBlock *acb)
2068{
Nick Cheng1a4f5502007-09-13 17:26:40 +08002069 uint32_t intmask_org;
Erich Chen1c57e862006-07-12 08:59:32 -07002070
Nick Cheng1a4f5502007-09-13 17:26:40 +08002071 arcmsr_wait_firmware_ready(acb);
2072 arcmsr_iop_confirm(acb);
2073 /* disable all outbound interrupt */
2074 intmask_org = arcmsr_disable_outbound_ints(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07002075 arcmsr_get_firmware_spec(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002076 /*start background rebuild*/
2077 arcmsr_start_adapter_bgrb(acb);
2078 /* empty doorbell Qbuffer if door bell ringed */
2079 arcmsr_clear_doorbell_queue_buffer(acb);
2080 /* enable outbound Post Queue,outbound doorbell Interrupt */
2081 arcmsr_enable_outbound_ints(acb, intmask_org);
Erich Chen1c57e862006-07-12 08:59:32 -07002082 acb->acb_flags |= ACB_F_IOP_INITED;
2083}
2084
2085static void arcmsr_iop_reset(struct AdapterControlBlock *acb)
2086{
Erich Chen1c57e862006-07-12 08:59:32 -07002087 struct CommandControlBlock *ccb;
2088 uint32_t intmask_org;
2089 int i = 0;
2090
2091 if (atomic_read(&acb->ccboutstandingcount) != 0) {
2092 /* talk to iop 331 outstanding command aborted */
2093 arcmsr_abort_allcmd(acb);
Jeff Garzik24430452007-07-27 13:00:48 -04002094
Erich Chen1c57e862006-07-12 08:59:32 -07002095 /* wait for 3 sec for all command aborted*/
Nick Cheng1a4f5502007-09-13 17:26:40 +08002096 ssleep(3);
Jeff Garzik24430452007-07-27 13:00:48 -04002097
Erich Chen1c57e862006-07-12 08:59:32 -07002098 /* disable all outbound interrupt */
2099 intmask_org = arcmsr_disable_outbound_ints(acb);
2100 /* clear all outbound posted Q */
Nick Cheng1a4f5502007-09-13 17:26:40 +08002101 arcmsr_done4abort_postqueue(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07002102 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2103 ccb = acb->pccb_pool[i];
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002104 if (ccb->startdone == ARCMSR_CCB_START) {
Erich Chen1c57e862006-07-12 08:59:32 -07002105 ccb->startdone = ARCMSR_CCB_ABORTED;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002106 arcmsr_ccb_complete(ccb, 1);
Erich Chen1c57e862006-07-12 08:59:32 -07002107 }
2108 }
2109 /* enable all outbound interrupt */
2110 arcmsr_enable_outbound_ints(acb, intmask_org);
2111 }
Erich Chen1c57e862006-07-12 08:59:32 -07002112}
2113
2114static int arcmsr_bus_reset(struct scsi_cmnd *cmd)
2115{
2116 struct AdapterControlBlock *acb =
2117 (struct AdapterControlBlock *)cmd->device->host->hostdata;
2118 int i;
2119
2120 acb->num_resets++;
2121 acb->acb_flags |= ACB_F_BUS_RESET;
2122 for (i = 0; i < 400; i++) {
2123 if (!atomic_read(&acb->ccboutstandingcount))
2124 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002125 arcmsr_interrupt(acb);/* FIXME: need spinlock */
Erich Chen1c57e862006-07-12 08:59:32 -07002126 msleep(25);
2127 }
2128 arcmsr_iop_reset(acb);
2129 acb->acb_flags &= ~ACB_F_BUS_RESET;
2130 return SUCCESS;
2131}
2132
2133static void arcmsr_abort_one_cmd(struct AdapterControlBlock *acb,
2134 struct CommandControlBlock *ccb)
2135{
2136 u32 intmask;
2137
2138 ccb->startdone = ARCMSR_CCB_ABORTED;
2139
2140 /*
2141 ** Wait for 3 sec for all command done.
2142 */
Nick Cheng1a4f5502007-09-13 17:26:40 +08002143 ssleep(3);
Erich Chen1c57e862006-07-12 08:59:32 -07002144
2145 intmask = arcmsr_disable_outbound_ints(acb);
2146 arcmsr_polling_ccbdone(acb, ccb);
2147 arcmsr_enable_outbound_ints(acb, intmask);
2148}
2149
2150static int arcmsr_abort(struct scsi_cmnd *cmd)
2151{
2152 struct AdapterControlBlock *acb =
2153 (struct AdapterControlBlock *)cmd->device->host->hostdata;
2154 int i = 0;
2155
2156 printk(KERN_NOTICE
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002157 "arcmsr%d: abort device command of scsi id = %d lun = %d \n",
Erich Chen1c57e862006-07-12 08:59:32 -07002158 acb->host->host_no, cmd->device->id, cmd->device->lun);
2159 acb->num_aborts++;
Erich Chen1c57e862006-07-12 08:59:32 -07002160 /*
2161 ************************************************
2162 ** the all interrupt service routine is locked
2163 ** we need to handle it as soon as possible and exit
2164 ************************************************
2165 */
2166 if (!atomic_read(&acb->ccboutstandingcount))
2167 return SUCCESS;
2168
2169 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2170 struct CommandControlBlock *ccb = acb->pccb_pool[i];
2171 if (ccb->startdone == ARCMSR_CCB_START && ccb->pcmd == cmd) {
2172 arcmsr_abort_one_cmd(acb, ccb);
2173 break;
2174 }
2175 }
2176
2177 return SUCCESS;
2178}
2179
2180static const char *arcmsr_info(struct Scsi_Host *host)
2181{
2182 struct AdapterControlBlock *acb =
2183 (struct AdapterControlBlock *) host->hostdata;
2184 static char buf[256];
2185 char *type;
2186 int raid6 = 1;
2187
2188 switch (acb->pdev->device) {
2189 case PCI_DEVICE_ID_ARECA_1110:
Nick Cheng1a4f5502007-09-13 17:26:40 +08002190 case PCI_DEVICE_ID_ARECA_1200:
2191 case PCI_DEVICE_ID_ARECA_1202:
Erich Chen1c57e862006-07-12 08:59:32 -07002192 case PCI_DEVICE_ID_ARECA_1210:
2193 raid6 = 0;
2194 /*FALLTHRU*/
2195 case PCI_DEVICE_ID_ARECA_1120:
2196 case PCI_DEVICE_ID_ARECA_1130:
2197 case PCI_DEVICE_ID_ARECA_1160:
2198 case PCI_DEVICE_ID_ARECA_1170:
Nick Cheng1a4f5502007-09-13 17:26:40 +08002199 case PCI_DEVICE_ID_ARECA_1201:
Erich Chen1c57e862006-07-12 08:59:32 -07002200 case PCI_DEVICE_ID_ARECA_1220:
2201 case PCI_DEVICE_ID_ARECA_1230:
2202 case PCI_DEVICE_ID_ARECA_1260:
2203 case PCI_DEVICE_ID_ARECA_1270:
2204 case PCI_DEVICE_ID_ARECA_1280:
2205 type = "SATA";
2206 break;
2207 case PCI_DEVICE_ID_ARECA_1380:
2208 case PCI_DEVICE_ID_ARECA_1381:
2209 case PCI_DEVICE_ID_ARECA_1680:
2210 case PCI_DEVICE_ID_ARECA_1681:
2211 type = "SAS";
2212 break;
2213 default:
2214 type = "X-TYPE";
2215 break;
2216 }
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002217 sprintf(buf, "Areca %s Host Adapter RAID Controller%s\n %s",
Erich Chen1c57e862006-07-12 08:59:32 -07002218 type, raid6 ? "( RAID6 capable)" : "",
2219 ARCMSR_DRIVER_VERSION);
2220 return buf;
2221}
Nick Cheng1a4f5502007-09-13 17:26:40 +08002222#ifdef CONFIG_SCSI_ARCMSR_AER
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002223static pci_ers_result_t arcmsr_pci_slot_reset(struct pci_dev *pdev)
2224{
Nick Cheng1a4f5502007-09-13 17:26:40 +08002225 struct Scsi_Host *host = pci_get_drvdata(pdev);
2226 struct AdapterControlBlock *acb =
2227 (struct AdapterControlBlock *) host->hostdata;
2228 uint32_t intmask_org;
2229 int i, j;
Erich Chen1c57e862006-07-12 08:59:32 -07002230
Nick Cheng1a4f5502007-09-13 17:26:40 +08002231 if (pci_enable_device(pdev)) {
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002232 return PCI_ERS_RESULT_DISCONNECT;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002233 }
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002234 pci_set_master(pdev);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002235 intmask_org = arcmsr_disable_outbound_ints(acb);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002236 acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
2237 ACB_F_MESSAGE_RQBUFFER_CLEARED |
2238 ACB_F_MESSAGE_WQBUFFER_READED);
2239 acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002240 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
2241 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
2242 acb->devstate[i][j] = ARECA_RAID_GONE;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002243
Nick Cheng1a4f5502007-09-13 17:26:40 +08002244 arcmsr_wait_firmware_ready(acb);
2245 arcmsr_iop_confirm(acb);
2246 /* disable all outbound interrupt */
2247 arcmsr_get_firmware_spec(acb);
2248 /*start background rebuild*/
2249 arcmsr_start_adapter_bgrb(acb);
2250 /* empty doorbell Qbuffer if door bell ringed */
2251 arcmsr_clear_doorbell_queue_buffer(acb);
2252 /* enable outbound Post Queue,outbound doorbell Interrupt */
2253 arcmsr_enable_outbound_ints(acb, intmask_org);
2254 acb->acb_flags |= ACB_F_IOP_INITED;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002255
Nick Cheng1a4f5502007-09-13 17:26:40 +08002256 pci_enable_pcie_error_reporting(pdev);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002257 return PCI_ERS_RESULT_RECOVERED;
2258}
2259
2260static void arcmsr_pci_ers_need_reset_forepart(struct pci_dev *pdev)
2261{
2262 struct Scsi_Host *host = pci_get_drvdata(pdev);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002263 struct AdapterControlBlock *acb = (struct AdapterControlBlock *)host->hostdata;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002264 struct CommandControlBlock *ccb;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002265 uint32_t intmask_org;
2266 int i = 0;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002267
Nick Cheng1a4f5502007-09-13 17:26:40 +08002268 if (atomic_read(&acb->ccboutstandingcount) != 0) {
2269 /* talk to iop 331 outstanding command aborted */
2270 arcmsr_abort_allcmd(acb);
2271 /* wait for 3 sec for all command aborted*/
2272 ssleep(3);
2273 /* disable all outbound interrupt */
2274 intmask_org = arcmsr_disable_outbound_ints(acb);
2275 /* clear all outbound posted Q */
2276 arcmsr_done4abort_postqueue(acb);
2277 for (i = 0; i < ARCMSR_MAX_FREECCB_NUM; i++) {
2278 ccb = acb->pccb_pool[i];
2279 if (ccb->startdone == ARCMSR_CCB_START) {
2280 ccb->startdone = ARCMSR_CCB_ABORTED;
2281 arcmsr_ccb_complete(ccb, 1);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002282 }
2283 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08002284 /* enable all outbound interrupt */
2285 arcmsr_enable_outbound_ints(acb, intmask_org);
2286 }
2287 pci_disable_device(pdev);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002288}
2289
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002290static void arcmsr_pci_ers_disconnect_forepart(struct pci_dev *pdev)
2291{
Nick Cheng1a4f5502007-09-13 17:26:40 +08002292 struct Scsi_Host *host = pci_get_drvdata(pdev);
2293 struct AdapterControlBlock *acb = \
2294 (struct AdapterControlBlock *)host->hostdata;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002295
Nick Cheng1a4f5502007-09-13 17:26:40 +08002296 arcmsr_stop_adapter_bgrb(acb);
2297 arcmsr_flush_adapter_cache(acb);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002298}
2299
2300static pci_ers_result_t arcmsr_pci_error_detected(struct pci_dev *pdev,
2301 pci_channel_state_t state)
2302{
2303 switch (state) {
2304 case pci_channel_io_frozen:
2305 arcmsr_pci_ers_need_reset_forepart(pdev);
2306 return PCI_ERS_RESULT_NEED_RESET;
2307 case pci_channel_io_perm_failure:
2308 arcmsr_pci_ers_disconnect_forepart(pdev);
2309 return PCI_ERS_RESULT_DISCONNECT;
2310 break;
2311 default:
2312 return PCI_ERS_RESULT_NEED_RESET;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002313 }
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08002314}
Nick Cheng1a4f5502007-09-13 17:26:40 +08002315#endif