blob: a48632b05ade49e754fa65a34bdcb801d687375e [file] [log] [blame]
Erich Chen1c57e862006-07-12 08:59:32 -07001/*
2*******************************************************************************
3** O.S : Linux
4** FILE NAME : arcmsr_hba.c
Ching Huangaaa64f62014-08-19 15:22:45 +08005** BY : Nick Cheng, C.L. Huang
6** Description: SCSI RAID Device Driver for Areca RAID Controller
Erich Chen1c57e862006-07-12 08:59:32 -07007*******************************************************************************
Ching Huangaaa64f62014-08-19 15:22:45 +08008** Copyright (C) 2002 - 2014, Areca Technology Corporation All rights reserved
Erich Chen1c57e862006-07-12 08:59:32 -07009**
10** Web site: www.areca.com.tw
Nick Cheng1a4f5502007-09-13 17:26:40 +080011** E-mail: support@areca.com.tw
Erich Chen1c57e862006-07-12 08:59:32 -070012**
13** This program is free software; you can redistribute it and/or modify
14** it under the terms of the GNU General Public License version 2 as
15** published by the Free Software Foundation.
16** This program is distributed in the hope that it will be useful,
17** but WITHOUT ANY WARRANTY; without even the implied warranty of
18** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19** GNU General Public License for more details.
20*******************************************************************************
21** Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions
23** are met:
24** 1. Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** 2. Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in the
28** documentation and/or other materials provided with the distribution.
29** 3. The name of the author may not be used to endorse or promote products
30** derived from this software without specific prior written permission.
31**
32** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
33** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
36** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,BUT
37** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38** DATA, OR PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY
39** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40** (INCLUDING NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF
41** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42*******************************************************************************
43** For history of changes, see Documentation/scsi/ChangeLog.arcmsr
Mauro Carvalho Chehabdade67f2020-03-02 09:16:15 +010044** Firmware Specification, see Documentation/scsi/arcmsr_spec.rst
Erich Chen1c57e862006-07-12 08:59:32 -070045*******************************************************************************
46*/
47#include <linux/module.h>
48#include <linux/reboot.h>
49#include <linux/spinlock.h>
50#include <linux/pci_ids.h>
51#include <linux/interrupt.h>
52#include <linux/moduleparam.h>
53#include <linux/errno.h>
54#include <linux/types.h>
55#include <linux/delay.h>
56#include <linux/dma-mapping.h>
57#include <linux/timer.h>
David Millera7c89622010-08-16 21:20:07 -070058#include <linux/slab.h>
Erich Chen1c57e862006-07-12 08:59:32 -070059#include <linux/pci.h>
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +080060#include <linux/aer.h>
Ching Huang2e9feb42014-09-24 17:33:34 +080061#include <linux/circ_buf.h>
Erich Chen1c57e862006-07-12 08:59:32 -070062#include <asm/dma.h>
63#include <asm/io.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080064#include <linux/uaccess.h>
Erich Chen1c57e862006-07-12 08:59:32 -070065#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"
Ching Huangaaa64f62014-08-19 15:22:45 +080073MODULE_AUTHOR("Nick Cheng, C.L. Huang <support@areca.com.tw>");
74MODULE_DESCRIPTION("Areca ARC11xx/12xx/16xx/188x SAS/SATA RAID Controller Driver");
Erich Chen1c57e862006-07-12 08:59:32 -070075MODULE_LICENSE("Dual BSD/GPL");
76MODULE_VERSION(ARCMSR_DRIVER_VERSION);
Tomas Henzl8b7eb862011-04-29 16:28:24 +020077
Ching Huang07640402017-12-05 10:26:38 +080078static int msix_enable = 1;
79module_param(msix_enable, int, S_IRUGO);
80MODULE_PARM_DESC(msix_enable, "Enable MSI-X interrupt(0 ~ 1), msix_enable=1(enable), =0(disable)");
81
Ching Huanga18686e2017-12-05 10:24:01 +080082static int msi_enable = 1;
83module_param(msi_enable, int, S_IRUGO);
84MODULE_PARM_DESC(msi_enable, "Enable MSI interrupt(0 ~ 1), msi_enable=1(enable), =0(disable)");
85
Ching Huangdd6206e2017-12-05 09:47:44 +080086static int host_can_queue = ARCMSR_DEFAULT_OUTSTANDING_CMD;
87module_param(host_can_queue, int, S_IRUGO);
88MODULE_PARM_DESC(host_can_queue, " adapter queue depth(32 ~ 1024), default is 128");
89
Ching Huangabf33d82017-12-05 09:55:02 +080090static int cmd_per_lun = ARCMSR_DEFAULT_CMD_PERLUN;
91module_param(cmd_per_lun, int, S_IRUGO);
92MODULE_PARM_DESC(cmd_per_lun, " device queue depth(1 ~ 128), default is 32");
93
Ching Huang7ec72612018-12-19 16:53:44 +080094static int dma_mask_64 = 0;
95module_param(dma_mask_64, int, S_IRUGO);
96MODULE_PARM_DESC(dma_mask_64, " set DMA mask to 64 bits(0 ~ 1), dma_mask_64=1(64 bits), =0(32 bits)");
97
Ching Huangb416c092017-12-05 09:59:52 +080098static int set_date_time = 0;
99module_param(set_date_time, int, S_IRUGO);
100MODULE_PARM_DESC(set_date_time, " send date, time to iop(0 ~ 1), set_date_time=1(enable), default(=0) is disable");
101
Tomas Henzl8b7eb862011-04-29 16:28:24 +0200102#define ARCMSR_SLEEPTIME 10
103#define ARCMSR_RETRYCOUNT 12
104
Ching Huangc10b1d52014-08-19 15:20:31 +0800105static wait_queue_head_t wait_q;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800106static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb,
107 struct scsi_cmnd *cmd);
108static int arcmsr_iop_confirm(struct AdapterControlBlock *acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700109static int arcmsr_abort(struct scsi_cmnd *);
110static int arcmsr_bus_reset(struct scsi_cmnd *);
111static int arcmsr_bios_param(struct scsi_device *sdev,
Nick Cheng1a4f5502007-09-13 17:26:40 +0800112 struct block_device *bdev, sector_t capacity, int *info);
Jeff Garzikf2812332010-11-16 02:10:29 -0500113static int arcmsr_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Erich Chen1c57e862006-07-12 08:59:32 -0700114static int arcmsr_probe(struct pci_dev *pdev,
115 const struct pci_device_id *id);
Ching Huang61cda872014-08-19 14:26:09 +0800116static int arcmsr_suspend(struct pci_dev *pdev, pm_message_t state);
117static int arcmsr_resume(struct pci_dev *pdev);
Erich Chen1c57e862006-07-12 08:59:32 -0700118static void arcmsr_remove(struct pci_dev *pdev);
119static void arcmsr_shutdown(struct pci_dev *pdev);
120static void arcmsr_iop_init(struct AdapterControlBlock *acb);
121static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800122static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb);
Ching Huang61cda872014-08-19 14:26:09 +0800123static void arcmsr_enable_outbound_ints(struct AdapterControlBlock *acb,
124 u32 intmask_org);
Erich Chen1c57e862006-07-12 08:59:32 -0700125static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb);
Ching Huang626fa322014-08-19 15:10:12 +0800126static void arcmsr_hbaA_flush_cache(struct AdapterControlBlock *acb);
127static void arcmsr_hbaB_flush_cache(struct AdapterControlBlock *acb);
Kees Cooke99e88a2017-10-16 14:43:17 -0700128static void arcmsr_request_device_map(struct timer_list *t);
Nick Cheng36b83de2010-05-17 11:22:42 +0800129static void arcmsr_message_isr_bh_fn(struct work_struct *work);
Nick Chengae52e7f2010-06-18 15:39:12 +0800130static bool arcmsr_get_firmware_spec(struct AdapterControlBlock *acb);
Nick Cheng36b83de2010-05-17 11:22:42 +0800131static void arcmsr_start_adapter_bgrb(struct AdapterControlBlock *acb);
Ching Huang626fa322014-08-19 15:10:12 +0800132static void arcmsr_hbaC_message_isr(struct AdapterControlBlock *pACB);
Ching Huang5b374792014-08-19 15:25:22 +0800133static void arcmsr_hbaD_message_isr(struct AdapterControlBlock *acb);
Ching Huang23509022017-12-05 09:35:34 +0800134static void arcmsr_hbaE_message_isr(struct AdapterControlBlock *acb);
135static void arcmsr_hbaE_postqueue_isr(struct AdapterControlBlock *acb);
Nick Chengcdd3cb12010-07-13 20:03:04 +0800136static void arcmsr_hardware_reset(struct AdapterControlBlock *acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700137static const char *arcmsr_info(struct Scsi_Host *);
138static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb);
Ching Huangb4eb6ae2014-08-19 15:28:36 +0800139static void arcmsr_free_irq(struct pci_dev *, struct AdapterControlBlock *);
Ching Huang7e315ff2015-11-25 19:49:33 +0800140static void arcmsr_wait_firmware_ready(struct AdapterControlBlock *acb);
Ching Huangb416c092017-12-05 09:59:52 +0800141static void arcmsr_set_iop_datetime(struct timer_list *);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100142static int arcmsr_adjust_disk_queue_depth(struct scsi_device *sdev, int queue_depth)
Erich Chen1c57e862006-07-12 08:59:32 -0700143{
144 if (queue_depth > ARCMSR_MAX_CMD_PERLUN)
145 queue_depth = ARCMSR_MAX_CMD_PERLUN;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100146 return scsi_change_queue_depth(sdev, queue_depth);
Erich Chen1c57e862006-07-12 08:59:32 -0700147}
148
149static struct scsi_host_template arcmsr_scsi_host_template = {
150 .module = THIS_MODULE,
Ching Huangaaa64f62014-08-19 15:22:45 +0800151 .name = "Areca SAS/SATA RAID driver",
Erich Chen1c57e862006-07-12 08:59:32 -0700152 .info = arcmsr_info,
153 .queuecommand = arcmsr_queue_command,
Ching Huanga3de4b52017-12-05 10:16:13 +0800154 .eh_abort_handler = arcmsr_abort,
Erich Chen1c57e862006-07-12 08:59:32 -0700155 .eh_bus_reset_handler = arcmsr_bus_reset,
156 .bios_param = arcmsr_bios_param,
157 .change_queue_depth = arcmsr_adjust_disk_queue_depth,
Ching Huangdd6206e2017-12-05 09:47:44 +0800158 .can_queue = ARCMSR_DEFAULT_OUTSTANDING_CMD,
Ching Huanga3de4b52017-12-05 10:16:13 +0800159 .this_id = ARCMSR_SCSI_INITIATOR_ID,
160 .sg_tablesize = ARCMSR_DEFAULT_SG_ENTRIES,
161 .max_sectors = ARCMSR_MAX_XFER_SECTORS_C,
Ching Huangabf33d82017-12-05 09:55:02 +0800162 .cmd_per_lun = ARCMSR_DEFAULT_CMD_PERLUN,
Erich Chen1c57e862006-07-12 08:59:32 -0700163 .shost_attrs = arcmsr_host_attrs,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400164 .no_write_same = 1,
Erich Chen1c57e862006-07-12 08:59:32 -0700165};
Ching Huang8b7c9942014-08-19 14:55:57 +0800166
Erich Chen1c57e862006-07-12 08:59:32 -0700167static struct pci_device_id arcmsr_device_id_table[] = {
Ching Huang8b7c9942014-08-19 14:55:57 +0800168 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1110),
169 .driver_data = ACB_ADAPTER_TYPE_A},
170 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1120),
171 .driver_data = ACB_ADAPTER_TYPE_A},
172 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1130),
173 .driver_data = ACB_ADAPTER_TYPE_A},
174 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1160),
175 .driver_data = ACB_ADAPTER_TYPE_A},
176 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1170),
177 .driver_data = ACB_ADAPTER_TYPE_A},
178 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1200),
179 .driver_data = ACB_ADAPTER_TYPE_B},
180 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1201),
181 .driver_data = ACB_ADAPTER_TYPE_B},
182 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1202),
183 .driver_data = ACB_ADAPTER_TYPE_B},
Ching Huang7e315ff2015-11-25 19:49:33 +0800184 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1203),
185 .driver_data = ACB_ADAPTER_TYPE_B},
Ching Huang8b7c9942014-08-19 14:55:57 +0800186 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1210),
187 .driver_data = ACB_ADAPTER_TYPE_A},
Ching Huang5b374792014-08-19 15:25:22 +0800188 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1214),
189 .driver_data = ACB_ADAPTER_TYPE_D},
Ching Huang8b7c9942014-08-19 14:55:57 +0800190 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1220),
191 .driver_data = ACB_ADAPTER_TYPE_A},
192 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1230),
193 .driver_data = ACB_ADAPTER_TYPE_A},
194 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1260),
195 .driver_data = ACB_ADAPTER_TYPE_A},
196 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1270),
197 .driver_data = ACB_ADAPTER_TYPE_A},
198 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1280),
199 .driver_data = ACB_ADAPTER_TYPE_A},
200 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1380),
201 .driver_data = ACB_ADAPTER_TYPE_A},
202 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1381),
203 .driver_data = ACB_ADAPTER_TYPE_A},
204 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1680),
205 .driver_data = ACB_ADAPTER_TYPE_A},
206 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1681),
207 .driver_data = ACB_ADAPTER_TYPE_A},
208 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1880),
209 .driver_data = ACB_ADAPTER_TYPE_C},
Ching Huang23509022017-12-05 09:35:34 +0800210 {PCI_DEVICE(PCI_VENDOR_ID_ARECA, PCI_DEVICE_ID_ARECA_1884),
211 .driver_data = ACB_ADAPTER_TYPE_E},
Erich Chen1c57e862006-07-12 08:59:32 -0700212 {0, 0}, /* Terminating entry */
213};
214MODULE_DEVICE_TABLE(pci, arcmsr_device_id_table);
Ching Huang8b7c9942014-08-19 14:55:57 +0800215
Erich Chen1c57e862006-07-12 08:59:32 -0700216static struct pci_driver arcmsr_pci_driver = {
217 .name = "arcmsr",
Ching Huanga3de4b52017-12-05 10:16:13 +0800218 .id_table = arcmsr_device_id_table,
Erich Chen1c57e862006-07-12 08:59:32 -0700219 .probe = arcmsr_probe,
220 .remove = arcmsr_remove,
Ching Huang61cda872014-08-19 14:26:09 +0800221 .suspend = arcmsr_suspend,
222 .resume = arcmsr_resume,
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +0800223 .shutdown = arcmsr_shutdown,
Erich Chen1c57e862006-07-12 08:59:32 -0700224};
Nick Chengcdd3cb12010-07-13 20:03:04 +0800225/*
226****************************************************************************
227****************************************************************************
228*/
Nick Chengcdd3cb12010-07-13 20:03:04 +0800229
Ching Huang609d0852018-12-19 16:27:20 +0800230static void arcmsr_free_io_queue(struct AdapterControlBlock *acb)
Nick Chengae52e7f2010-06-18 15:39:12 +0800231{
232 switch (acb->adapter_type) {
Ching Huang5b374792014-08-19 15:25:22 +0800233 case ACB_ADAPTER_TYPE_B:
Ching Huang23509022017-12-05 09:35:34 +0800234 case ACB_ADAPTER_TYPE_D:
235 case ACB_ADAPTER_TYPE_E: {
Ching Huang381d66d2018-12-19 16:24:03 +0800236 dma_free_coherent(&acb->pdev->dev, acb->ioqueue_size,
Ching Huang6e38adf2014-08-19 15:14:14 +0800237 acb->dma_coherent2, acb->dma_coherent_handle2);
238 break;
Nick Chengae52e7f2010-06-18 15:39:12 +0800239 }
240 }
241}
242
243static bool arcmsr_remap_pciregion(struct AdapterControlBlock *acb)
244{
245 struct pci_dev *pdev = acb->pdev;
Nick Chengcdd3cb12010-07-13 20:03:04 +0800246 switch (acb->adapter_type){
Nick Chengae52e7f2010-06-18 15:39:12 +0800247 case ACB_ADAPTER_TYPE_A:{
Nick Chengcdd3cb12010-07-13 20:03:04 +0800248 acb->pmuA = ioremap(pci_resource_start(pdev,0), pci_resource_len(pdev,0));
Nick Chengae52e7f2010-06-18 15:39:12 +0800249 if (!acb->pmuA) {
250 printk(KERN_NOTICE "arcmsr%d: memory mapping region fail \n", acb->host->host_no);
251 return false;
252 }
253 break;
254 }
255 case ACB_ADAPTER_TYPE_B:{
256 void __iomem *mem_base0, *mem_base1;
257 mem_base0 = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
258 if (!mem_base0) {
259 printk(KERN_NOTICE "arcmsr%d: memory mapping region fail \n", acb->host->host_no);
260 return false;
261 }
262 mem_base1 = ioremap(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2));
263 if (!mem_base1) {
264 iounmap(mem_base0);
265 printk(KERN_NOTICE "arcmsr%d: memory mapping region fail \n", acb->host->host_no);
266 return false;
267 }
268 acb->mem_base0 = mem_base0;
269 acb->mem_base1 = mem_base1;
Nick Chengcdd3cb12010-07-13 20:03:04 +0800270 break;
271 }
272 case ACB_ADAPTER_TYPE_C:{
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100273 acb->pmuC = ioremap(pci_resource_start(pdev, 1), pci_resource_len(pdev, 1));
Nick Chengcdd3cb12010-07-13 20:03:04 +0800274 if (!acb->pmuC) {
275 printk(KERN_NOTICE "arcmsr%d: memory mapping region fail \n", acb->host->host_no);
276 return false;
277 }
278 if (readl(&acb->pmuC->outbound_doorbell) & ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE) {
279 writel(ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE_DOORBELL_CLEAR, &acb->pmuC->outbound_doorbell_clear);/*clear interrupt*/
280 return true;
281 }
282 break;
Nick Chengae52e7f2010-06-18 15:39:12 +0800283 }
Ching Huang5b374792014-08-19 15:25:22 +0800284 case ACB_ADAPTER_TYPE_D: {
285 void __iomem *mem_base0;
286 unsigned long addr, range, flags;
287
288 addr = (unsigned long)pci_resource_start(pdev, 0);
289 range = pci_resource_len(pdev, 0);
290 flags = pci_resource_flags(pdev, 0);
Dan Williams92b19ff2015-08-10 23:07:06 -0400291 mem_base0 = ioremap(addr, range);
Ching Huang5b374792014-08-19 15:25:22 +0800292 if (!mem_base0) {
293 pr_notice("arcmsr%d: memory mapping region fail\n",
294 acb->host->host_no);
295 return false;
296 }
297 acb->mem_base0 = mem_base0;
298 break;
299 }
Ching Huang23509022017-12-05 09:35:34 +0800300 case ACB_ADAPTER_TYPE_E: {
301 acb->pmuE = ioremap(pci_resource_start(pdev, 1),
302 pci_resource_len(pdev, 1));
303 if (!acb->pmuE) {
304 pr_notice("arcmsr%d: memory mapping region fail \n",
305 acb->host->host_no);
306 return false;
307 }
308 writel(0, &acb->pmuE->host_int_status); /*clear interrupt*/
309 writel(ARCMSR_HBEMU_DOORBELL_SYNC, &acb->pmuE->iobound_doorbell); /* synchronize doorbell to 0 */
310 acb->in_doorbell = 0;
311 acb->out_doorbell = 0;
312 break;
313 }
Nick Chengae52e7f2010-06-18 15:39:12 +0800314 }
315 return true;
316}
317
318static void arcmsr_unmap_pciregion(struct AdapterControlBlock *acb)
319{
320 switch (acb->adapter_type) {
Nick Chengcdd3cb12010-07-13 20:03:04 +0800321 case ACB_ADAPTER_TYPE_A:{
322 iounmap(acb->pmuA);
323 }
324 break;
325 case ACB_ADAPTER_TYPE_B:{
326 iounmap(acb->mem_base0);
327 iounmap(acb->mem_base1);
328 }
329
330 break;
331 case ACB_ADAPTER_TYPE_C:{
332 iounmap(acb->pmuC);
333 }
Ching Huang5b374792014-08-19 15:25:22 +0800334 break;
335 case ACB_ADAPTER_TYPE_D:
336 iounmap(acb->mem_base0);
337 break;
Ching Huang23509022017-12-05 09:35:34 +0800338 case ACB_ADAPTER_TYPE_E:
339 iounmap(acb->pmuE);
340 break;
Nick Chengae52e7f2010-06-18 15:39:12 +0800341 }
342}
343
David Howells7d12e782006-10-05 14:55:46 +0100344static irqreturn_t arcmsr_do_interrupt(int irq, void *dev_id)
Erich Chen1c57e862006-07-12 08:59:32 -0700345{
346 irqreturn_t handle_state;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800347 struct AdapterControlBlock *acb = dev_id;
Erich Chen1c57e862006-07-12 08:59:32 -0700348
Erich Chen1c57e862006-07-12 08:59:32 -0700349 handle_state = arcmsr_interrupt(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700350 return handle_state;
351}
352
353static int arcmsr_bios_param(struct scsi_device *sdev,
354 struct block_device *bdev, sector_t capacity, int *geom)
355{
356 int ret, heads, sectors, cylinders, total_capacity;
357 unsigned char *buffer;/* return copy of block device's partition table */
358
359 buffer = scsi_bios_ptable(bdev);
360 if (buffer) {
361 ret = scsi_partsize(buffer, capacity, &geom[2], &geom[0], &geom[1]);
362 kfree(buffer);
363 if (ret != -1)
364 return ret;
365 }
366 total_capacity = capacity;
367 heads = 64;
368 sectors = 32;
369 cylinders = total_capacity / (heads * sectors);
370 if (cylinders > 1024) {
371 heads = 255;
372 sectors = 63;
373 cylinders = total_capacity / (heads * sectors);
374 }
375 geom[0] = heads;
376 geom[1] = sectors;
377 geom[2] = cylinders;
378 return 0;
379}
380
Ching Huang626fa322014-08-19 15:10:12 +0800381static uint8_t arcmsr_hbaA_wait_msgint_ready(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +0800382{
Nick Chengae52e7f2010-06-18 15:39:12 +0800383 struct MessageUnit_A __iomem *reg = acb->pmuA;
Tomas Henzl8b7eb862011-04-29 16:28:24 +0200384 int i;
Nick Chengae52e7f2010-06-18 15:39:12 +0800385
Tomas Henzl8b7eb862011-04-29 16:28:24 +0200386 for (i = 0; i < 2000; i++) {
387 if (readl(&reg->outbound_intstatus) &
388 ARCMSR_MU_OUTBOUND_MESSAGE0_INT) {
389 writel(ARCMSR_MU_OUTBOUND_MESSAGE0_INT,
390 &reg->outbound_intstatus);
391 return true;
392 }
393 msleep(10);
394 } /* max 20 seconds */
395
Nick Chengcdd3cb12010-07-13 20:03:04 +0800396 return false;
Nick Chengae52e7f2010-06-18 15:39:12 +0800397}
398
Ching Huang626fa322014-08-19 15:10:12 +0800399static uint8_t arcmsr_hbaB_wait_msgint_ready(struct AdapterControlBlock *acb)
Nick Chengae52e7f2010-06-18 15:39:12 +0800400{
401 struct MessageUnit_B *reg = acb->pmuB;
Tomas Henzl8b7eb862011-04-29 16:28:24 +0200402 int i;
Nick Chengae52e7f2010-06-18 15:39:12 +0800403
Tomas Henzl8b7eb862011-04-29 16:28:24 +0200404 for (i = 0; i < 2000; i++) {
405 if (readl(reg->iop2drv_doorbell)
406 & ARCMSR_IOP2DRV_MESSAGE_CMD_DONE) {
407 writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN,
408 reg->iop2drv_doorbell);
409 writel(ARCMSR_DRV2IOP_END_OF_INTERRUPT,
410 reg->drv2iop_doorbell);
411 return true;
412 }
413 msleep(10);
414 } /* max 20 seconds */
415
Nick Chengcdd3cb12010-07-13 20:03:04 +0800416 return false;
Nick Chengae52e7f2010-06-18 15:39:12 +0800417}
418
Ching Huang626fa322014-08-19 15:10:12 +0800419static uint8_t arcmsr_hbaC_wait_msgint_ready(struct AdapterControlBlock *pACB)
Nick Chengcdd3cb12010-07-13 20:03:04 +0800420{
Ching Huangc10b1d52014-08-19 15:20:31 +0800421 struct MessageUnit_C __iomem *phbcmu = pACB->pmuC;
Tomas Henzl8b7eb862011-04-29 16:28:24 +0200422 int i;
423
424 for (i = 0; i < 2000; i++) {
425 if (readl(&phbcmu->outbound_doorbell)
426 & ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE) {
427 writel(ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE_DOORBELL_CLEAR,
428 &phbcmu->outbound_doorbell_clear); /*clear interrupt*/
429 return true;
430 }
431 msleep(10);
432 } /* max 20 seconds */
433
Nick Chengcdd3cb12010-07-13 20:03:04 +0800434 return false;
435}
Tomas Henzl8b7eb862011-04-29 16:28:24 +0200436
Ching Huang5b374792014-08-19 15:25:22 +0800437static bool arcmsr_hbaD_wait_msgint_ready(struct AdapterControlBlock *pACB)
438{
439 struct MessageUnit_D *reg = pACB->pmuD;
440 int i;
441
442 for (i = 0; i < 2000; i++) {
443 if (readl(reg->outbound_doorbell)
444 & ARCMSR_ARC1214_IOP2DRV_MESSAGE_CMD_DONE) {
445 writel(ARCMSR_ARC1214_IOP2DRV_MESSAGE_CMD_DONE,
446 reg->outbound_doorbell);
447 return true;
448 }
449 msleep(10);
450 } /* max 20 seconds */
451 return false;
452}
453
Ching Huang23509022017-12-05 09:35:34 +0800454static bool arcmsr_hbaE_wait_msgint_ready(struct AdapterControlBlock *pACB)
455{
456 int i;
457 uint32_t read_doorbell;
458 struct MessageUnit_E __iomem *phbcmu = pACB->pmuE;
459
460 for (i = 0; i < 2000; i++) {
461 read_doorbell = readl(&phbcmu->iobound_doorbell);
462 if ((read_doorbell ^ pACB->in_doorbell) & ARCMSR_HBEMU_IOP2DRV_MESSAGE_CMD_DONE) {
463 writel(0, &phbcmu->host_int_status); /*clear interrupt*/
464 pACB->in_doorbell = read_doorbell;
465 return true;
466 }
467 msleep(10);
468 } /* max 20 seconds */
469 return false;
470}
471
Ching Huang626fa322014-08-19 15:10:12 +0800472static void arcmsr_hbaA_flush_cache(struct AdapterControlBlock *acb)
Nick Chengae52e7f2010-06-18 15:39:12 +0800473{
474 struct MessageUnit_A __iomem *reg = acb->pmuA;
475 int retry_count = 30;
Nick Chengae52e7f2010-06-18 15:39:12 +0800476 writel(ARCMSR_INBOUND_MESG0_FLUSH_CACHE, &reg->inbound_msgaddr0);
477 do {
Ching Huang626fa322014-08-19 15:10:12 +0800478 if (arcmsr_hbaA_wait_msgint_ready(acb))
Nick Chengae52e7f2010-06-18 15:39:12 +0800479 break;
480 else {
481 retry_count--;
482 printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
483 timeout, retry count down = %d \n", acb->host->host_no, retry_count);
484 }
485 } while (retry_count != 0);
486}
487
Ching Huang626fa322014-08-19 15:10:12 +0800488static void arcmsr_hbaB_flush_cache(struct AdapterControlBlock *acb)
Nick Chengae52e7f2010-06-18 15:39:12 +0800489{
490 struct MessageUnit_B *reg = acb->pmuB;
491 int retry_count = 30;
Nick Chengae52e7f2010-06-18 15:39:12 +0800492 writel(ARCMSR_MESSAGE_FLUSH_CACHE, reg->drv2iop_doorbell);
493 do {
Ching Huang626fa322014-08-19 15:10:12 +0800494 if (arcmsr_hbaB_wait_msgint_ready(acb))
Nick Chengae52e7f2010-06-18 15:39:12 +0800495 break;
496 else {
497 retry_count--;
498 printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
499 timeout,retry count down = %d \n", acb->host->host_no, retry_count);
500 }
501 } while (retry_count != 0);
502}
503
Ching Huang626fa322014-08-19 15:10:12 +0800504static void arcmsr_hbaC_flush_cache(struct AdapterControlBlock *pACB)
Nick Chengcdd3cb12010-07-13 20:03:04 +0800505{
Ching Huangc10b1d52014-08-19 15:20:31 +0800506 struct MessageUnit_C __iomem *reg = pACB->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +0800507 int retry_count = 30;/* enlarge wait flush adapter cache time: 10 minute */
508 writel(ARCMSR_INBOUND_MESG0_FLUSH_CACHE, &reg->inbound_msgaddr0);
509 writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &reg->inbound_doorbell);
510 do {
Ching Huang626fa322014-08-19 15:10:12 +0800511 if (arcmsr_hbaC_wait_msgint_ready(pACB)) {
Nick Chengcdd3cb12010-07-13 20:03:04 +0800512 break;
513 } else {
514 retry_count--;
515 printk(KERN_NOTICE "arcmsr%d: wait 'flush adapter cache' \
516 timeout,retry count down = %d \n", pACB->host->host_no, retry_count);
517 }
518 } while (retry_count != 0);
519 return;
520}
Ching Huang5b374792014-08-19 15:25:22 +0800521
522static void arcmsr_hbaD_flush_cache(struct AdapterControlBlock *pACB)
523{
524 int retry_count = 15;
525 struct MessageUnit_D *reg = pACB->pmuD;
526
527 writel(ARCMSR_INBOUND_MESG0_FLUSH_CACHE, reg->inbound_msgaddr0);
528 do {
529 if (arcmsr_hbaD_wait_msgint_ready(pACB))
530 break;
531
532 retry_count--;
533 pr_notice("arcmsr%d: wait 'flush adapter "
534 "cache' timeout, retry count down = %d\n",
535 pACB->host->host_no, retry_count);
536 } while (retry_count != 0);
537}
538
Ching Huang23509022017-12-05 09:35:34 +0800539static void arcmsr_hbaE_flush_cache(struct AdapterControlBlock *pACB)
540{
541 int retry_count = 30;
542 struct MessageUnit_E __iomem *reg = pACB->pmuE;
543
544 writel(ARCMSR_INBOUND_MESG0_FLUSH_CACHE, &reg->inbound_msgaddr0);
545 pACB->out_doorbell ^= ARCMSR_HBEMU_DRV2IOP_MESSAGE_CMD_DONE;
546 writel(pACB->out_doorbell, &reg->iobound_doorbell);
547 do {
548 if (arcmsr_hbaE_wait_msgint_ready(pACB))
549 break;
550 retry_count--;
551 pr_notice("arcmsr%d: wait 'flush adapter "
552 "cache' timeout, retry count down = %d\n",
553 pACB->host->host_no, retry_count);
554 } while (retry_count != 0);
555}
556
Nick Chengae52e7f2010-06-18 15:39:12 +0800557static void arcmsr_flush_adapter_cache(struct AdapterControlBlock *acb)
558{
Nick Cheng1a4f5502007-09-13 17:26:40 +0800559 switch (acb->adapter_type) {
560
561 case ACB_ADAPTER_TYPE_A: {
Ching Huang626fa322014-08-19 15:10:12 +0800562 arcmsr_hbaA_flush_cache(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +0800563 }
564 break;
565
566 case ACB_ADAPTER_TYPE_B: {
Ching Huang626fa322014-08-19 15:10:12 +0800567 arcmsr_hbaB_flush_cache(acb);
Nick Chengae52e7f2010-06-18 15:39:12 +0800568 }
Nick Chengcdd3cb12010-07-13 20:03:04 +0800569 break;
570 case ACB_ADAPTER_TYPE_C: {
Ching Huang626fa322014-08-19 15:10:12 +0800571 arcmsr_hbaC_flush_cache(acb);
Nick Chengcdd3cb12010-07-13 20:03:04 +0800572 }
Ching Huang5b374792014-08-19 15:25:22 +0800573 break;
574 case ACB_ADAPTER_TYPE_D:
575 arcmsr_hbaD_flush_cache(acb);
576 break;
Ching Huang23509022017-12-05 09:35:34 +0800577 case ACB_ADAPTER_TYPE_E:
578 arcmsr_hbaE_flush_cache(acb);
579 break;
Nick Chengae52e7f2010-06-18 15:39:12 +0800580 }
581}
Nick Cheng1a4f5502007-09-13 17:26:40 +0800582
Ching Huang222f1182018-12-19 16:31:00 +0800583static void arcmsr_hbaB_assign_regAddr(struct AdapterControlBlock *acb)
584{
585 struct MessageUnit_B *reg = acb->pmuB;
586
587 if (acb->pdev->device == PCI_DEVICE_ID_ARECA_1203) {
588 reg->drv2iop_doorbell = MEM_BASE0(ARCMSR_DRV2IOP_DOORBELL_1203);
589 reg->drv2iop_doorbell_mask = MEM_BASE0(ARCMSR_DRV2IOP_DOORBELL_MASK_1203);
590 reg->iop2drv_doorbell = MEM_BASE0(ARCMSR_IOP2DRV_DOORBELL_1203);
591 reg->iop2drv_doorbell_mask = MEM_BASE0(ARCMSR_IOP2DRV_DOORBELL_MASK_1203);
592 } else {
593 reg->drv2iop_doorbell= MEM_BASE0(ARCMSR_DRV2IOP_DOORBELL);
594 reg->drv2iop_doorbell_mask = MEM_BASE0(ARCMSR_DRV2IOP_DOORBELL_MASK);
595 reg->iop2drv_doorbell = MEM_BASE0(ARCMSR_IOP2DRV_DOORBELL);
596 reg->iop2drv_doorbell_mask = MEM_BASE0(ARCMSR_IOP2DRV_DOORBELL_MASK);
597 }
598 reg->message_wbuffer = MEM_BASE1(ARCMSR_MESSAGE_WBUFFER);
599 reg->message_rbuffer = MEM_BASE1(ARCMSR_MESSAGE_RBUFFER);
600 reg->message_rwbuffer = MEM_BASE1(ARCMSR_MESSAGE_RWBUFFER);
601}
602
603static void arcmsr_hbaD_assign_regAddr(struct AdapterControlBlock *acb)
604{
605 struct MessageUnit_D *reg = acb->pmuD;
606
607 reg->chip_id = MEM_BASE0(ARCMSR_ARC1214_CHIP_ID);
608 reg->cpu_mem_config = MEM_BASE0(ARCMSR_ARC1214_CPU_MEMORY_CONFIGURATION);
609 reg->i2o_host_interrupt_mask = MEM_BASE0(ARCMSR_ARC1214_I2_HOST_INTERRUPT_MASK);
610 reg->sample_at_reset = MEM_BASE0(ARCMSR_ARC1214_SAMPLE_RESET);
611 reg->reset_request = MEM_BASE0(ARCMSR_ARC1214_RESET_REQUEST);
612 reg->host_int_status = MEM_BASE0(ARCMSR_ARC1214_MAIN_INTERRUPT_STATUS);
613 reg->pcief0_int_enable = MEM_BASE0(ARCMSR_ARC1214_PCIE_F0_INTERRUPT_ENABLE);
614 reg->inbound_msgaddr0 = MEM_BASE0(ARCMSR_ARC1214_INBOUND_MESSAGE0);
615 reg->inbound_msgaddr1 = MEM_BASE0(ARCMSR_ARC1214_INBOUND_MESSAGE1);
616 reg->outbound_msgaddr0 = MEM_BASE0(ARCMSR_ARC1214_OUTBOUND_MESSAGE0);
617 reg->outbound_msgaddr1 = MEM_BASE0(ARCMSR_ARC1214_OUTBOUND_MESSAGE1);
618 reg->inbound_doorbell = MEM_BASE0(ARCMSR_ARC1214_INBOUND_DOORBELL);
619 reg->outbound_doorbell = MEM_BASE0(ARCMSR_ARC1214_OUTBOUND_DOORBELL);
620 reg->outbound_doorbell_enable = MEM_BASE0(ARCMSR_ARC1214_OUTBOUND_DOORBELL_ENABLE);
621 reg->inboundlist_base_low = MEM_BASE0(ARCMSR_ARC1214_INBOUND_LIST_BASE_LOW);
622 reg->inboundlist_base_high = MEM_BASE0(ARCMSR_ARC1214_INBOUND_LIST_BASE_HIGH);
623 reg->inboundlist_write_pointer = MEM_BASE0(ARCMSR_ARC1214_INBOUND_LIST_WRITE_POINTER);
624 reg->outboundlist_base_low = MEM_BASE0(ARCMSR_ARC1214_OUTBOUND_LIST_BASE_LOW);
625 reg->outboundlist_base_high = MEM_BASE0(ARCMSR_ARC1214_OUTBOUND_LIST_BASE_HIGH);
626 reg->outboundlist_copy_pointer = MEM_BASE0(ARCMSR_ARC1214_OUTBOUND_LIST_COPY_POINTER);
627 reg->outboundlist_read_pointer = MEM_BASE0(ARCMSR_ARC1214_OUTBOUND_LIST_READ_POINTER);
628 reg->outboundlist_interrupt_cause = MEM_BASE0(ARCMSR_ARC1214_OUTBOUND_INTERRUPT_CAUSE);
629 reg->outboundlist_interrupt_enable = MEM_BASE0(ARCMSR_ARC1214_OUTBOUND_INTERRUPT_ENABLE);
630 reg->message_wbuffer = MEM_BASE0(ARCMSR_ARC1214_MESSAGE_WBUFFER);
631 reg->message_rbuffer = MEM_BASE0(ARCMSR_ARC1214_MESSAGE_RBUFFER);
632 reg->msgcode_rwbuffer = MEM_BASE0(ARCMSR_ARC1214_MESSAGE_RWBUFFER);
633}
634
Ching Huang02040672015-11-26 19:41:15 +0800635static bool arcmsr_alloc_io_queue(struct AdapterControlBlock *acb)
636{
637 bool rtn = true;
638 void *dma_coherent;
639 dma_addr_t dma_coherent_handle;
640 struct pci_dev *pdev = acb->pdev;
641
642 switch (acb->adapter_type) {
643 case ACB_ADAPTER_TYPE_B: {
Ching Huang381d66d2018-12-19 16:24:03 +0800644 acb->ioqueue_size = roundup(sizeof(struct MessageUnit_B), 32);
Ching Huang3e3153b2019-01-18 10:53:41 +0800645 dma_coherent = dma_alloc_coherent(&pdev->dev, acb->ioqueue_size,
Ching Huang02040672015-11-26 19:41:15 +0800646 &dma_coherent_handle, GFP_KERNEL);
647 if (!dma_coherent) {
648 pr_notice("arcmsr%d: DMA allocation failed\n", acb->host->host_no);
649 return false;
650 }
651 acb->dma_coherent_handle2 = dma_coherent_handle;
652 acb->dma_coherent2 = dma_coherent;
Ching Huang222f1182018-12-19 16:31:00 +0800653 acb->pmuB = (struct MessageUnit_B *)dma_coherent;
654 arcmsr_hbaB_assign_regAddr(acb);
Ching Huang02040672015-11-26 19:41:15 +0800655 }
656 break;
657 case ACB_ADAPTER_TYPE_D: {
Ching Huang381d66d2018-12-19 16:24:03 +0800658 acb->ioqueue_size = roundup(sizeof(struct MessageUnit_D), 32);
Ching Huang3e3153b2019-01-18 10:53:41 +0800659 dma_coherent = dma_alloc_coherent(&pdev->dev, acb->ioqueue_size,
Ching Huang02040672015-11-26 19:41:15 +0800660 &dma_coherent_handle, GFP_KERNEL);
661 if (!dma_coherent) {
662 pr_notice("arcmsr%d: DMA allocation failed\n", acb->host->host_no);
663 return false;
664 }
665 acb->dma_coherent_handle2 = dma_coherent_handle;
666 acb->dma_coherent2 = dma_coherent;
Ching Huang222f1182018-12-19 16:31:00 +0800667 acb->pmuD = (struct MessageUnit_D *)dma_coherent;
668 arcmsr_hbaD_assign_regAddr(acb);
Ching Huang02040672015-11-26 19:41:15 +0800669 }
670 break;
Ching Huang23509022017-12-05 09:35:34 +0800671 case ACB_ADAPTER_TYPE_E: {
672 uint32_t completeQ_size;
673 completeQ_size = sizeof(struct deliver_completeQ) * ARCMSR_MAX_HBE_DONEQUEUE + 128;
Ching Huang381d66d2018-12-19 16:24:03 +0800674 acb->ioqueue_size = roundup(completeQ_size, 32);
Ching Huang3e3153b2019-01-18 10:53:41 +0800675 dma_coherent = dma_alloc_coherent(&pdev->dev, acb->ioqueue_size,
Ching Huang23509022017-12-05 09:35:34 +0800676 &dma_coherent_handle, GFP_KERNEL);
677 if (!dma_coherent){
678 pr_notice("arcmsr%d: DMA allocation failed\n", acb->host->host_no);
679 return false;
680 }
681 acb->dma_coherent_handle2 = dma_coherent_handle;
682 acb->dma_coherent2 = dma_coherent;
683 acb->pCompletionQ = dma_coherent;
Ching Huang381d66d2018-12-19 16:24:03 +0800684 acb->completionQ_entry = acb->ioqueue_size / sizeof(struct deliver_completeQ);
Ching Huang23509022017-12-05 09:35:34 +0800685 acb->doneq_index = 0;
686 }
687 break;
Ching Huang02040672015-11-26 19:41:15 +0800688 default:
689 break;
690 }
691 return rtn;
692}
693
Nick Chengae52e7f2010-06-18 15:39:12 +0800694static int arcmsr_alloc_ccb_pool(struct AdapterControlBlock *acb)
695{
Nick Chengcdd3cb12010-07-13 20:03:04 +0800696 struct pci_dev *pdev = acb->pdev;
697 void *dma_coherent;
698 dma_addr_t dma_coherent_handle;
699 struct CommandControlBlock *ccb_tmp;
700 int i = 0, j = 0;
Ching Huang7860a482018-12-19 16:34:58 +0800701 unsigned long cdb_phyaddr, next_ccb_phy;
Tomas Henzl87f76152011-04-29 16:28:30 +0200702 unsigned long roundup_ccbsize;
Nick Chengcdd3cb12010-07-13 20:03:04 +0800703 unsigned long max_xfer_len;
704 unsigned long max_sg_entrys;
Ching Huang7860a482018-12-19 16:34:58 +0800705 uint32_t firm_config_version, curr_phy_upper32;
Tomas Henzl87f76152011-04-29 16:28:30 +0200706
Nick Chengcdd3cb12010-07-13 20:03:04 +0800707 for (i = 0; i < ARCMSR_MAX_TARGETID; i++)
708 for (j = 0; j < ARCMSR_MAX_TARGETLUN; j++)
709 acb->devstate[i][j] = ARECA_RAID_GONE;
Nick Chengae52e7f2010-06-18 15:39:12 +0800710
Nick Chengcdd3cb12010-07-13 20:03:04 +0800711 max_xfer_len = ARCMSR_MAX_XFER_LEN;
712 max_sg_entrys = ARCMSR_DEFAULT_SG_ENTRIES;
713 firm_config_version = acb->firm_cfg_version;
714 if((firm_config_version & 0xFF) >= 3){
715 max_xfer_len = (ARCMSR_CDB_SG_PAGE_LENGTH << ((firm_config_version >> 8) & 0xFF)) * 1024;/* max 4M byte */
Tomas Henzl87f76152011-04-29 16:28:30 +0200716 max_sg_entrys = (max_xfer_len/4096);
Nick Chengcdd3cb12010-07-13 20:03:04 +0800717 }
718 acb->host->max_sectors = max_xfer_len/512;
719 acb->host->sg_tablesize = max_sg_entrys;
720 roundup_ccbsize = roundup(sizeof(struct CommandControlBlock) + (max_sg_entrys - 1) * sizeof(struct SG64ENTRY), 32);
Ching Huangd076e4a2017-12-05 09:44:23 +0800721 acb->uncache_size = roundup_ccbsize * acb->maxFreeCCB;
Ching Huang222f1182018-12-19 16:31:00 +0800722 acb->uncache_size += acb->ioqueue_size;
Nick Chengcdd3cb12010-07-13 20:03:04 +0800723 dma_coherent = dma_alloc_coherent(&pdev->dev, acb->uncache_size, &dma_coherent_handle, GFP_KERNEL);
724 if(!dma_coherent){
Tomas Henzl87f76152011-04-29 16:28:30 +0200725 printk(KERN_NOTICE "arcmsr%d: dma_alloc_coherent got error\n", acb->host->host_no);
Nick Chengcdd3cb12010-07-13 20:03:04 +0800726 return -ENOMEM;
727 }
728 acb->dma_coherent = dma_coherent;
729 acb->dma_coherent_handle = dma_coherent_handle;
730 memset(dma_coherent, 0, acb->uncache_size);
Ching Huang23509022017-12-05 09:35:34 +0800731 acb->ccbsize = roundup_ccbsize;
Nick Chengcdd3cb12010-07-13 20:03:04 +0800732 ccb_tmp = dma_coherent;
Ching Huang7860a482018-12-19 16:34:58 +0800733 curr_phy_upper32 = upper_32_bits(dma_coherent_handle);
Nick Chengcdd3cb12010-07-13 20:03:04 +0800734 acb->vir2phy_offset = (unsigned long)dma_coherent - (unsigned long)dma_coherent_handle;
Ching Huangd076e4a2017-12-05 09:44:23 +0800735 for(i = 0; i < acb->maxFreeCCB; i++){
Ching Huang7860a482018-12-19 16:34:58 +0800736 cdb_phyaddr = (unsigned long)dma_coherent_handle + offsetof(struct CommandControlBlock, arcmsr_cdb);
Ching Huang5b374792014-08-19 15:25:22 +0800737 switch (acb->adapter_type) {
738 case ACB_ADAPTER_TYPE_A:
739 case ACB_ADAPTER_TYPE_B:
740 ccb_tmp->cdb_phyaddr = cdb_phyaddr >> 5;
741 break;
742 case ACB_ADAPTER_TYPE_C:
743 case ACB_ADAPTER_TYPE_D:
Ching Huang23509022017-12-05 09:35:34 +0800744 case ACB_ADAPTER_TYPE_E:
Ching Huang5b374792014-08-19 15:25:22 +0800745 ccb_tmp->cdb_phyaddr = cdb_phyaddr;
746 break;
747 }
Nick Chengcdd3cb12010-07-13 20:03:04 +0800748 acb->pccb_pool[i] = ccb_tmp;
749 ccb_tmp->acb = acb;
Ching Huang23509022017-12-05 09:35:34 +0800750 ccb_tmp->smid = (u32)i << 16;
Nick Chengcdd3cb12010-07-13 20:03:04 +0800751 INIT_LIST_HEAD(&ccb_tmp->list);
Ching Huang7860a482018-12-19 16:34:58 +0800752 next_ccb_phy = dma_coherent_handle + roundup_ccbsize;
753 if (upper_32_bits(next_ccb_phy) != curr_phy_upper32) {
754 acb->maxFreeCCB = i;
755 acb->host->can_queue = i;
756 break;
757 }
758 else
759 list_add_tail(&ccb_tmp->list, &acb->ccb_free_list);
Nick Chengcdd3cb12010-07-13 20:03:04 +0800760 ccb_tmp = (struct CommandControlBlock *)((unsigned long)ccb_tmp + roundup_ccbsize);
Ching Huang7860a482018-12-19 16:34:58 +0800761 dma_coherent_handle = next_ccb_phy;
Erich Chen1c57e862006-07-12 08:59:32 -0700762 }
Ching Huang222f1182018-12-19 16:31:00 +0800763 acb->dma_coherent_handle2 = dma_coherent_handle;
764 acb->dma_coherent2 = ccb_tmp;
765 switch (acb->adapter_type) {
766 case ACB_ADAPTER_TYPE_B:
767 acb->pmuB = (struct MessageUnit_B *)acb->dma_coherent2;
768 arcmsr_hbaB_assign_regAddr(acb);
769 break;
770 case ACB_ADAPTER_TYPE_D:
771 acb->pmuD = (struct MessageUnit_D *)acb->dma_coherent2;
772 arcmsr_hbaD_assign_regAddr(acb);
773 break;
774 case ACB_ADAPTER_TYPE_E:
775 acb->pCompletionQ = acb->dma_coherent2;
776 acb->completionQ_entry = acb->ioqueue_size / sizeof(struct deliver_completeQ);
777 acb->doneq_index = 0;
778 break;
779 }
Erich Chen1c57e862006-07-12 08:59:32 -0700780 return 0;
781}
Nick Cheng36b83de2010-05-17 11:22:42 +0800782
Nick Chengcdd3cb12010-07-13 20:03:04 +0800783static void arcmsr_message_isr_bh_fn(struct work_struct *work)
784{
Ching Huang12aad942014-08-19 14:59:00 +0800785 struct AdapterControlBlock *acb = container_of(work,
786 struct AdapterControlBlock, arcmsr_do_message_isr_bh);
787 char *acb_dev_map = (char *)acb->device_map;
788 uint32_t __iomem *signature = NULL;
789 char __iomem *devicemap = NULL;
790 int target, lun;
791 struct scsi_device *psdev;
792 char diff, temp;
793
Ching Huang5dd8b3e2017-12-05 09:57:23 +0800794 acb->acb_flags &= ~ACB_F_MSG_GET_CONFIG;
Nick Cheng36b83de2010-05-17 11:22:42 +0800795 switch (acb->adapter_type) {
Ching Huang12aad942014-08-19 14:59:00 +0800796 case ACB_ADAPTER_TYPE_A: {
797 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng36b83de2010-05-17 11:22:42 +0800798
Ching Huang12aad942014-08-19 14:59:00 +0800799 signature = (uint32_t __iomem *)(&reg->message_rwbuffer[0]);
800 devicemap = (char __iomem *)(&reg->message_rwbuffer[21]);
Nick Chengcdd3cb12010-07-13 20:03:04 +0800801 break;
Ching Huang12aad942014-08-19 14:59:00 +0800802 }
803 case ACB_ADAPTER_TYPE_B: {
804 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng36b83de2010-05-17 11:22:42 +0800805
Ching Huang12aad942014-08-19 14:59:00 +0800806 signature = (uint32_t __iomem *)(&reg->message_rwbuffer[0]);
807 devicemap = (char __iomem *)(&reg->message_rwbuffer[21]);
808 break;
809 }
810 case ACB_ADAPTER_TYPE_C: {
811 struct MessageUnit_C __iomem *reg = acb->pmuC;
812
813 signature = (uint32_t __iomem *)(&reg->msgcode_rwbuffer[0]);
814 devicemap = (char __iomem *)(&reg->msgcode_rwbuffer[21]);
815 break;
816 }
Ching Huang5b374792014-08-19 15:25:22 +0800817 case ACB_ADAPTER_TYPE_D: {
818 struct MessageUnit_D *reg = acb->pmuD;
819
820 signature = (uint32_t __iomem *)(&reg->msgcode_rwbuffer[0]);
821 devicemap = (char __iomem *)(&reg->msgcode_rwbuffer[21]);
822 break;
823 }
Ching Huang23509022017-12-05 09:35:34 +0800824 case ACB_ADAPTER_TYPE_E: {
825 struct MessageUnit_E __iomem *reg = acb->pmuE;
826
827 signature = (uint32_t __iomem *)(&reg->msgcode_rwbuffer[0]);
828 devicemap = (char __iomem *)(&reg->msgcode_rwbuffer[21]);
829 break;
830 }
Ching Huang12aad942014-08-19 14:59:00 +0800831 }
832 atomic_inc(&acb->rq_map_token);
833 if (readl(signature) != ARCMSR_SIGNATURE_GET_CONFIG)
834 return;
835 for (target = 0; target < ARCMSR_MAX_TARGETID - 1;
836 target++) {
837 temp = readb(devicemap);
838 diff = (*acb_dev_map) ^ temp;
839 if (diff != 0) {
840 *acb_dev_map = temp;
841 for (lun = 0; lun < ARCMSR_MAX_TARGETLUN;
842 lun++) {
843 if ((diff & 0x01) == 1 &&
844 (temp & 0x01) == 1) {
845 scsi_add_device(acb->host,
846 0, target, lun);
847 } else if ((diff & 0x01) == 1
848 && (temp & 0x01) == 0) {
849 psdev = scsi_device_lookup(acb->host,
850 0, target, lun);
851 if (psdev != NULL) {
852 scsi_remove_device(psdev);
853 scsi_device_put(psdev);
Nick Cheng36b83de2010-05-17 11:22:42 +0800854 }
Nick Cheng36b83de2010-05-17 11:22:42 +0800855 }
Ching Huang12aad942014-08-19 14:59:00 +0800856 temp >>= 1;
857 diff >>= 1;
Nick Cheng36b83de2010-05-17 11:22:42 +0800858 }
859 }
Ching Huang12aad942014-08-19 14:59:00 +0800860 devicemap++;
861 acb_dev_map++;
Nick Cheng36b83de2010-05-17 11:22:42 +0800862 }
863}
Erich Chen1c57e862006-07-12 08:59:32 -0700864
Ching Huang1d1166e2014-08-19 14:23:31 +0800865static int
866arcmsr_request_irq(struct pci_dev *pdev, struct AdapterControlBlock *acb)
867{
Christoph Hellwig68130c92016-09-11 15:31:23 +0200868 unsigned long flags;
869 int nvec, i;
Ching Huang1d1166e2014-08-19 14:23:31 +0800870
Ching Huang07640402017-12-05 10:26:38 +0800871 if (msix_enable == 0)
872 goto msi_int0;
Christoph Hellwig68130c92016-09-11 15:31:23 +0200873 nvec = pci_alloc_irq_vectors(pdev, 1, ARCMST_NUM_MSIX_VECTORS,
874 PCI_IRQ_MSIX);
875 if (nvec > 0) {
876 pr_info("arcmsr%d: msi-x enabled\n", acb->host->host_no);
877 flags = 0;
878 } else {
Ching Huang07640402017-12-05 10:26:38 +0800879msi_int0:
Ching Huanga18686e2017-12-05 10:24:01 +0800880 if (msi_enable == 1) {
881 nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
882 if (nvec == 1) {
883 dev_info(&pdev->dev, "msi enabled\n");
884 goto msi_int1;
885 }
886 }
887 nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_LEGACY);
Christoph Hellwig68130c92016-09-11 15:31:23 +0200888 if (nvec < 1)
889 return FAILED;
Ching Huanga18686e2017-12-05 10:24:01 +0800890msi_int1:
Christoph Hellwig68130c92016-09-11 15:31:23 +0200891 flags = IRQF_SHARED;
892 }
893
894 acb->vector_count = nvec;
895 for (i = 0; i < nvec; i++) {
896 if (request_irq(pci_irq_vector(pdev, i), arcmsr_do_interrupt,
897 flags, "arcmsr", acb)) {
Ching Huang1d1166e2014-08-19 14:23:31 +0800898 pr_warn("arcmsr%d: request_irq =%d failed!\n",
Christoph Hellwig68130c92016-09-11 15:31:23 +0200899 acb->host->host_no, pci_irq_vector(pdev, i));
900 goto out_free_irq;
Ching Huang1d1166e2014-08-19 14:23:31 +0800901 }
Ching Huang1d1166e2014-08-19 14:23:31 +0800902 }
Christoph Hellwig68130c92016-09-11 15:31:23 +0200903
Ching Huang1d1166e2014-08-19 14:23:31 +0800904 return SUCCESS;
Christoph Hellwig68130c92016-09-11 15:31:23 +0200905out_free_irq:
906 while (--i >= 0)
907 free_irq(pci_irq_vector(pdev, i), acb);
908 pci_free_irq_vectors(pdev);
909 return FAILED;
Ching Huang1d1166e2014-08-19 14:23:31 +0800910}
911
Ching Huangea331f32017-12-05 10:11:23 +0800912static void arcmsr_init_get_devmap_timer(struct AdapterControlBlock *pacb)
913{
914 INIT_WORK(&pacb->arcmsr_do_message_isr_bh, arcmsr_message_isr_bh_fn);
915 atomic_set(&pacb->rq_map_token, 16);
916 atomic_set(&pacb->ante_token_value, 16);
917 pacb->fw_flag = FW_NORMAL;
918 timer_setup(&pacb->eternal_timer, arcmsr_request_device_map, 0);
919 pacb->eternal_timer.expires = jiffies + msecs_to_jiffies(6 * HZ);
920 add_timer(&pacb->eternal_timer);
921}
922
Ching Huangb416c092017-12-05 09:59:52 +0800923static void arcmsr_init_set_datetime_timer(struct AdapterControlBlock *pacb)
924{
925 timer_setup(&pacb->refresh_timer, arcmsr_set_iop_datetime, 0);
926 pacb->refresh_timer.expires = jiffies + msecs_to_jiffies(60 * 1000);
927 add_timer(&pacb->refresh_timer);
928}
929
Ching Huang1d120c62018-12-19 16:56:34 +0800930static int arcmsr_set_dma_mask(struct AdapterControlBlock *acb)
931{
932 struct pci_dev *pcidev = acb->pdev;
933
934 if (IS_DMA64) {
935 if (((acb->adapter_type == ACB_ADAPTER_TYPE_A) && !dma_mask_64) ||
936 dma_set_mask(&pcidev->dev, DMA_BIT_MASK(64)))
937 goto dma32;
938 if (dma_set_coherent_mask(&pcidev->dev, DMA_BIT_MASK(64)) ||
939 dma_set_mask_and_coherent(&pcidev->dev, DMA_BIT_MASK(64))) {
940 printk("arcmsr: set DMA 64 mask failed\n");
941 return -ENXIO;
942 }
943 } else {
944dma32:
945 if (dma_set_mask(&pcidev->dev, DMA_BIT_MASK(32)) ||
946 dma_set_coherent_mask(&pcidev->dev, DMA_BIT_MASK(32)) ||
947 dma_set_mask_and_coherent(&pcidev->dev, DMA_BIT_MASK(32))) {
948 printk("arcmsr: set DMA 32-bit mask failed\n");
949 return -ENXIO;
950 }
951 }
952 return 0;
953}
954
Nick Chengae52e7f2010-06-18 15:39:12 +0800955static int arcmsr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Erich Chen1c57e862006-07-12 08:59:32 -0700956{
957 struct Scsi_Host *host;
958 struct AdapterControlBlock *acb;
Nick Chengcdd3cb12010-07-13 20:03:04 +0800959 uint8_t bus,dev_fun;
Erich Chen1c57e862006-07-12 08:59:32 -0700960 int error;
Erich Chen1c57e862006-07-12 08:59:32 -0700961 error = pci_enable_device(pdev);
Nick Chengcdd3cb12010-07-13 20:03:04 +0800962 if(error){
Nick Chengae52e7f2010-06-18 15:39:12 +0800963 return -ENODEV;
Erich Chen1c57e862006-07-12 08:59:32 -0700964 }
Nick Chengae52e7f2010-06-18 15:39:12 +0800965 host = scsi_host_alloc(&arcmsr_scsi_host_template, sizeof(struct AdapterControlBlock));
Nick Chengcdd3cb12010-07-13 20:03:04 +0800966 if(!host){
967 goto pci_disable_dev;
Nick Chengae52e7f2010-06-18 15:39:12 +0800968 }
Nick Chengae52e7f2010-06-18 15:39:12 +0800969 init_waitqueue_head(&wait_q);
Erich Chen1c57e862006-07-12 08:59:32 -0700970 bus = pdev->bus->number;
971 dev_fun = pdev->devfn;
Nick Chengae52e7f2010-06-18 15:39:12 +0800972 acb = (struct AdapterControlBlock *) host->hostdata;
Nick Chengcdd3cb12010-07-13 20:03:04 +0800973 memset(acb,0,sizeof(struct AdapterControlBlock));
Erich Chen1c57e862006-07-12 08:59:32 -0700974 acb->pdev = pdev;
Ching Huang1d120c62018-12-19 16:56:34 +0800975 acb->adapter_type = id->driver_data;
976 if (arcmsr_set_dma_mask(acb))
977 goto scsi_host_release;
Nick Chengae52e7f2010-06-18 15:39:12 +0800978 acb->host = host;
Erich Chen1c57e862006-07-12 08:59:32 -0700979 host->max_lun = ARCMSR_MAX_TARGETLUN;
Nick Chengcdd3cb12010-07-13 20:03:04 +0800980 host->max_id = ARCMSR_MAX_TARGETID; /*16:8*/
981 host->max_cmd_len = 16; /*this is issue of 64bit LBA ,over 2T byte*/
Ching Huangdd6206e2017-12-05 09:47:44 +0800982 if ((host_can_queue < ARCMSR_MIN_OUTSTANDING_CMD) || (host_can_queue > ARCMSR_MAX_OUTSTANDING_CMD))
983 host_can_queue = ARCMSR_DEFAULT_OUTSTANDING_CMD;
984 host->can_queue = host_can_queue; /* max simultaneous cmds */
Ching Huangabf33d82017-12-05 09:55:02 +0800985 if ((cmd_per_lun < ARCMSR_MIN_CMD_PERLUN) || (cmd_per_lun > ARCMSR_MAX_CMD_PERLUN))
986 cmd_per_lun = ARCMSR_DEFAULT_CMD_PERLUN;
987 host->cmd_per_lun = cmd_per_lun;
Erich Chen1c57e862006-07-12 08:59:32 -0700988 host->this_id = ARCMSR_SCSI_INITIATOR_ID;
989 host->unique_id = (bus << 8) | dev_fun;
Nick Chengae52e7f2010-06-18 15:39:12 +0800990 pci_set_drvdata(pdev, host);
991 pci_set_master(pdev);
Erich Chen1c57e862006-07-12 08:59:32 -0700992 error = pci_request_regions(pdev, "arcmsr");
Nick Chengcdd3cb12010-07-13 20:03:04 +0800993 if(error){
Nick Chengae52e7f2010-06-18 15:39:12 +0800994 goto scsi_host_release;
Erich Chen1c57e862006-07-12 08:59:32 -0700995 }
Nick Chengae52e7f2010-06-18 15:39:12 +0800996 spin_lock_init(&acb->eh_lock);
997 spin_lock_init(&acb->ccblist_lock);
Ching Huang5b374792014-08-19 15:25:22 +0800998 spin_lock_init(&acb->postq_lock);
999 spin_lock_init(&acb->doneq_lock);
Ching Huangbb263c42014-08-19 15:17:45 +08001000 spin_lock_init(&acb->rqbuffer_lock);
1001 spin_lock_init(&acb->wqbuffer_lock);
Erich Chen1c57e862006-07-12 08:59:32 -07001002 acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
Nick Chengcdd3cb12010-07-13 20:03:04 +08001003 ACB_F_MESSAGE_RQBUFFER_CLEARED |
1004 ACB_F_MESSAGE_WQBUFFER_READED);
Erich Chen1c57e862006-07-12 08:59:32 -07001005 acb->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
1006 INIT_LIST_HEAD(&acb->ccb_free_list);
Nick Chengae52e7f2010-06-18 15:39:12 +08001007 error = arcmsr_remap_pciregion(acb);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001008 if(!error){
Nick Chengae52e7f2010-06-18 15:39:12 +08001009 goto pci_release_regs;
1010 }
Ching Huang02040672015-11-26 19:41:15 +08001011 error = arcmsr_alloc_io_queue(acb);
1012 if (!error)
1013 goto unmap_pci_region;
Nick Chengae52e7f2010-06-18 15:39:12 +08001014 error = arcmsr_get_firmware_spec(acb);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001015 if(!error){
Ching Huang02040672015-11-26 19:41:15 +08001016 goto free_hbb_mu;
Nick Chengae52e7f2010-06-18 15:39:12 +08001017 }
Ching Huang222f1182018-12-19 16:31:00 +08001018 arcmsr_free_io_queue(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001019 error = arcmsr_alloc_ccb_pool(acb);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001020 if(error){
Ching Huang222f1182018-12-19 16:31:00 +08001021 goto unmap_pci_region;
Nick Chengae52e7f2010-06-18 15:39:12 +08001022 }
Erich Chen1c57e862006-07-12 08:59:32 -07001023 error = scsi_add_host(host, &pdev->dev);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001024 if(error){
Ching Huangb4eb6ae2014-08-19 15:28:36 +08001025 goto free_ccb_pool;
Nick Chengae52e7f2010-06-18 15:39:12 +08001026 }
Ching Huang1d1166e2014-08-19 14:23:31 +08001027 if (arcmsr_request_irq(pdev, acb) == FAILED)
Nick Chengae52e7f2010-06-18 15:39:12 +08001028 goto scsi_host_remove;
Ching Huang1d1166e2014-08-19 14:23:31 +08001029 arcmsr_iop_init(acb);
Ching Huangea331f32017-12-05 10:11:23 +08001030 arcmsr_init_get_devmap_timer(acb);
Ching Huangb416c092017-12-05 09:59:52 +08001031 if (set_date_time)
1032 arcmsr_init_set_datetime_timer(acb);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001033 if(arcmsr_alloc_sysfs_attr(acb))
Nick Chengae52e7f2010-06-18 15:39:12 +08001034 goto out_free_sysfs;
Ching Huangb4eb6ae2014-08-19 15:28:36 +08001035 scsi_scan_host(host);
Erich Chen1c57e862006-07-12 08:59:32 -07001036 return 0;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001037out_free_sysfs:
Ching Huangb416c092017-12-05 09:59:52 +08001038 if (set_date_time)
1039 del_timer_sync(&acb->refresh_timer);
Ching Huangb4eb6ae2014-08-19 15:28:36 +08001040 del_timer_sync(&acb->eternal_timer);
1041 flush_work(&acb->arcmsr_do_message_isr_bh);
Nick Chengae52e7f2010-06-18 15:39:12 +08001042 arcmsr_stop_adapter_bgrb(acb);
1043 arcmsr_flush_adapter_cache(acb);
Ching Huangb4eb6ae2014-08-19 15:28:36 +08001044 arcmsr_free_irq(pdev, acb);
1045scsi_host_remove:
1046 scsi_remove_host(host);
1047free_ccb_pool:
Erich Chen1c57e862006-07-12 08:59:32 -07001048 arcmsr_free_ccb_pool(acb);
Ching Huang222f1182018-12-19 16:31:00 +08001049 goto unmap_pci_region;
Nick Chengae52e7f2010-06-18 15:39:12 +08001050free_hbb_mu:
Ching Huang609d0852018-12-19 16:27:20 +08001051 arcmsr_free_io_queue(acb);
Nick Chengae52e7f2010-06-18 15:39:12 +08001052unmap_pci_region:
1053 arcmsr_unmap_pciregion(acb);
1054pci_release_regs:
Erich Chen1c57e862006-07-12 08:59:32 -07001055 pci_release_regions(pdev);
Nick Chengae52e7f2010-06-18 15:39:12 +08001056scsi_host_release:
Erich Chen1c57e862006-07-12 08:59:32 -07001057 scsi_host_put(host);
Nick Chengae52e7f2010-06-18 15:39:12 +08001058pci_disable_dev:
Erich Chen1c57e862006-07-12 08:59:32 -07001059 pci_disable_device(pdev);
Nick Chengae52e7f2010-06-18 15:39:12 +08001060 return -ENODEV;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001061}
1062
Ching Huang1d1166e2014-08-19 14:23:31 +08001063static void arcmsr_free_irq(struct pci_dev *pdev,
1064 struct AdapterControlBlock *acb)
1065{
1066 int i;
1067
Christoph Hellwig68130c92016-09-11 15:31:23 +02001068 for (i = 0; i < acb->vector_count; i++)
1069 free_irq(pci_irq_vector(pdev, i), acb);
1070 pci_free_irq_vectors(pdev);
Ching Huang1d1166e2014-08-19 14:23:31 +08001071}
1072
Ching Huang61cda872014-08-19 14:26:09 +08001073static int arcmsr_suspend(struct pci_dev *pdev, pm_message_t state)
1074{
1075 uint32_t intmask_org;
1076 struct Scsi_Host *host = pci_get_drvdata(pdev);
1077 struct AdapterControlBlock *acb =
1078 (struct AdapterControlBlock *)host->hostdata;
1079
1080 intmask_org = arcmsr_disable_outbound_ints(acb);
1081 arcmsr_free_irq(pdev, acb);
1082 del_timer_sync(&acb->eternal_timer);
Ching Huangb416c092017-12-05 09:59:52 +08001083 if (set_date_time)
1084 del_timer_sync(&acb->refresh_timer);
Ching Huang61cda872014-08-19 14:26:09 +08001085 flush_work(&acb->arcmsr_do_message_isr_bh);
1086 arcmsr_stop_adapter_bgrb(acb);
1087 arcmsr_flush_adapter_cache(acb);
1088 pci_set_drvdata(pdev, host);
1089 pci_save_state(pdev);
1090 pci_disable_device(pdev);
1091 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1092 return 0;
1093}
1094
1095static int arcmsr_resume(struct pci_dev *pdev)
1096{
Ching Huang61cda872014-08-19 14:26:09 +08001097 struct Scsi_Host *host = pci_get_drvdata(pdev);
1098 struct AdapterControlBlock *acb =
1099 (struct AdapterControlBlock *)host->hostdata;
1100
1101 pci_set_power_state(pdev, PCI_D0);
1102 pci_enable_wake(pdev, PCI_D0, 0);
1103 pci_restore_state(pdev);
1104 if (pci_enable_device(pdev)) {
1105 pr_warn("%s: pci_enable_device error\n", __func__);
1106 return -ENODEV;
1107 }
Ching Huang1d120c62018-12-19 16:56:34 +08001108 if (arcmsr_set_dma_mask(acb))
1109 goto controller_unregister;
Ching Huang61cda872014-08-19 14:26:09 +08001110 pci_set_master(pdev);
1111 if (arcmsr_request_irq(pdev, acb) == FAILED)
1112 goto controller_stop;
Ching Huangafdda872018-12-19 16:59:48 +08001113 switch (acb->adapter_type) {
1114 case ACB_ADAPTER_TYPE_B: {
1115 struct MessageUnit_B *reg = acb->pmuB;
Ching Huang317d0e02019-01-18 10:58:51 +08001116 uint32_t i;
1117 for (i = 0; i < ARCMSR_MAX_HBB_POSTQUEUE; i++) {
1118 reg->post_qbuffer[i] = 0;
1119 reg->done_qbuffer[i] = 0;
1120 }
Ching Huangafdda872018-12-19 16:59:48 +08001121 reg->postq_index = 0;
1122 reg->doneq_index = 0;
1123 break;
1124 }
1125 case ACB_ADAPTER_TYPE_E:
Ching Huang97fe2222018-07-09 18:24:53 +08001126 writel(0, &acb->pmuE->host_int_status);
1127 writel(ARCMSR_HBEMU_DOORBELL_SYNC, &acb->pmuE->iobound_doorbell);
1128 acb->in_doorbell = 0;
1129 acb->out_doorbell = 0;
1130 acb->doneq_index = 0;
Ching Huangafdda872018-12-19 16:59:48 +08001131 break;
Ching Huang97fe2222018-07-09 18:24:53 +08001132 }
Ching Huang61cda872014-08-19 14:26:09 +08001133 arcmsr_iop_init(acb);
Ching Huangea331f32017-12-05 10:11:23 +08001134 arcmsr_init_get_devmap_timer(acb);
Ching Huangb416c092017-12-05 09:59:52 +08001135 if (set_date_time)
1136 arcmsr_init_set_datetime_timer(acb);
Ching Huang61cda872014-08-19 14:26:09 +08001137 return 0;
1138controller_stop:
1139 arcmsr_stop_adapter_bgrb(acb);
1140 arcmsr_flush_adapter_cache(acb);
1141controller_unregister:
1142 scsi_remove_host(host);
1143 arcmsr_free_ccb_pool(acb);
1144 arcmsr_unmap_pciregion(acb);
1145 pci_release_regions(pdev);
1146 scsi_host_put(host);
1147 pci_disable_device(pdev);
1148 return -ENODEV;
1149}
1150
Ching Huang626fa322014-08-19 15:10:12 +08001151static uint8_t arcmsr_hbaA_abort_allcmd(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08001152{
Al Viro80da1ad2007-10-29 05:08:28 +00001153 struct MessageUnit_A __iomem *reg = acb->pmuA;
Erich Chen1c57e862006-07-12 08:59:32 -07001154 writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, &reg->inbound_msgaddr0);
Ching Huang626fa322014-08-19 15:10:12 +08001155 if (!arcmsr_hbaA_wait_msgint_ready(acb)) {
Erich Chen1c57e862006-07-12 08:59:32 -07001156 printk(KERN_NOTICE
Ching Huang626fa322014-08-19 15:10:12 +08001157 "arcmsr%d: wait 'abort all outstanding command' timeout\n"
Erich Chen1c57e862006-07-12 08:59:32 -07001158 , acb->host->host_no);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001159 return false;
Nick Cheng36b83de2010-05-17 11:22:42 +08001160 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08001161 return true;
Erich Chen1c57e862006-07-12 08:59:32 -07001162}
1163
Ching Huang626fa322014-08-19 15:10:12 +08001164static uint8_t arcmsr_hbaB_abort_allcmd(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08001165{
Al Viro80da1ad2007-10-29 05:08:28 +00001166 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001167
Nick Chengae52e7f2010-06-18 15:39:12 +08001168 writel(ARCMSR_MESSAGE_ABORT_CMD, reg->drv2iop_doorbell);
Ching Huang626fa322014-08-19 15:10:12 +08001169 if (!arcmsr_hbaB_wait_msgint_ready(acb)) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001170 printk(KERN_NOTICE
Ching Huang626fa322014-08-19 15:10:12 +08001171 "arcmsr%d: wait 'abort all outstanding command' timeout\n"
Nick Cheng1a4f5502007-09-13 17:26:40 +08001172 , acb->host->host_no);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001173 return false;
Nick Cheng36b83de2010-05-17 11:22:42 +08001174 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08001175 return true;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001176}
Ching Huang626fa322014-08-19 15:10:12 +08001177static uint8_t arcmsr_hbaC_abort_allcmd(struct AdapterControlBlock *pACB)
Nick Chengcdd3cb12010-07-13 20:03:04 +08001178{
Ching Huangc10b1d52014-08-19 15:20:31 +08001179 struct MessageUnit_C __iomem *reg = pACB->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001180 writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, &reg->inbound_msgaddr0);
1181 writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &reg->inbound_doorbell);
Ching Huang626fa322014-08-19 15:10:12 +08001182 if (!arcmsr_hbaC_wait_msgint_ready(pACB)) {
Nick Chengcdd3cb12010-07-13 20:03:04 +08001183 printk(KERN_NOTICE
Ching Huang626fa322014-08-19 15:10:12 +08001184 "arcmsr%d: wait 'abort all outstanding command' timeout\n"
Nick Chengcdd3cb12010-07-13 20:03:04 +08001185 , pACB->host->host_no);
1186 return false;
1187 }
1188 return true;
1189}
Ching Huang5b374792014-08-19 15:25:22 +08001190
1191static uint8_t arcmsr_hbaD_abort_allcmd(struct AdapterControlBlock *pACB)
1192{
1193 struct MessageUnit_D *reg = pACB->pmuD;
1194
1195 writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, reg->inbound_msgaddr0);
1196 if (!arcmsr_hbaD_wait_msgint_ready(pACB)) {
1197 pr_notice("arcmsr%d: wait 'abort all outstanding "
1198 "command' timeout\n", pACB->host->host_no);
1199 return false;
1200 }
1201 return true;
1202}
1203
Ching Huang23509022017-12-05 09:35:34 +08001204static uint8_t arcmsr_hbaE_abort_allcmd(struct AdapterControlBlock *pACB)
1205{
1206 struct MessageUnit_E __iomem *reg = pACB->pmuE;
1207
1208 writel(ARCMSR_INBOUND_MESG0_ABORT_CMD, &reg->inbound_msgaddr0);
1209 pACB->out_doorbell ^= ARCMSR_HBEMU_DRV2IOP_MESSAGE_CMD_DONE;
1210 writel(pACB->out_doorbell, &reg->iobound_doorbell);
1211 if (!arcmsr_hbaE_wait_msgint_ready(pACB)) {
1212 pr_notice("arcmsr%d: wait 'abort all outstanding "
1213 "command' timeout\n", pACB->host->host_no);
1214 return false;
1215 }
1216 return true;
1217}
1218
Nick Cheng36b83de2010-05-17 11:22:42 +08001219static uint8_t arcmsr_abort_allcmd(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08001220{
Nick Cheng36b83de2010-05-17 11:22:42 +08001221 uint8_t rtnval = 0;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001222 switch (acb->adapter_type) {
1223 case ACB_ADAPTER_TYPE_A: {
Ching Huang626fa322014-08-19 15:10:12 +08001224 rtnval = arcmsr_hbaA_abort_allcmd(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001225 }
1226 break;
1227
1228 case ACB_ADAPTER_TYPE_B: {
Ching Huang626fa322014-08-19 15:10:12 +08001229 rtnval = arcmsr_hbaB_abort_allcmd(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001230 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08001231 break;
1232
1233 case ACB_ADAPTER_TYPE_C: {
Ching Huang626fa322014-08-19 15:10:12 +08001234 rtnval = arcmsr_hbaC_abort_allcmd(acb);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001235 }
Ching Huang5b374792014-08-19 15:25:22 +08001236 break;
1237
1238 case ACB_ADAPTER_TYPE_D:
1239 rtnval = arcmsr_hbaD_abort_allcmd(acb);
1240 break;
Ching Huang23509022017-12-05 09:35:34 +08001241 case ACB_ADAPTER_TYPE_E:
1242 rtnval = arcmsr_hbaE_abort_allcmd(acb);
1243 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001244 }
Nick Cheng36b83de2010-05-17 11:22:42 +08001245 return rtnval;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001246}
1247
Erich Chen1c57e862006-07-12 08:59:32 -07001248static void arcmsr_pci_unmap_dma(struct CommandControlBlock *ccb)
1249{
Erich Chen1c57e862006-07-12 08:59:32 -07001250 struct scsi_cmnd *pcmd = ccb->pcmd;
1251
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001252 scsi_dma_unmap(pcmd);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001253}
Erich Chen1c57e862006-07-12 08:59:32 -07001254
Nick Chengae52e7f2010-06-18 15:39:12 +08001255static void arcmsr_ccb_complete(struct CommandControlBlock *ccb)
Erich Chen1c57e862006-07-12 08:59:32 -07001256{
1257 struct AdapterControlBlock *acb = ccb->acb;
1258 struct scsi_cmnd *pcmd = ccb->pcmd;
Nick Chengae52e7f2010-06-18 15:39:12 +08001259 unsigned long flags;
Nick Chengae52e7f2010-06-18 15:39:12 +08001260 atomic_dec(&acb->ccboutstandingcount);
Erich Chen1c57e862006-07-12 08:59:32 -07001261 arcmsr_pci_unmap_dma(ccb);
Erich Chen1c57e862006-07-12 08:59:32 -07001262 ccb->startdone = ARCMSR_CCB_DONE;
Nick Chengae52e7f2010-06-18 15:39:12 +08001263 spin_lock_irqsave(&acb->ccblist_lock, flags);
Erich Chen1c57e862006-07-12 08:59:32 -07001264 list_add_tail(&ccb->list, &acb->ccb_free_list);
Nick Chengae52e7f2010-06-18 15:39:12 +08001265 spin_unlock_irqrestore(&acb->ccblist_lock, flags);
Erich Chen1c57e862006-07-12 08:59:32 -07001266 pcmd->scsi_done(pcmd);
1267}
1268
Nick Cheng1a4f5502007-09-13 17:26:40 +08001269static void arcmsr_report_sense_info(struct CommandControlBlock *ccb)
1270{
1271
1272 struct scsi_cmnd *pcmd = ccb->pcmd;
1273 struct SENSE_DATA *sensebuffer = (struct SENSE_DATA *)pcmd->sense_buffer;
Ching Huangfa576b42017-12-05 11:28:37 +08001274 pcmd->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001275 if (sensebuffer) {
1276 int sense_data_length =
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09001277 sizeof(struct SENSE_DATA) < SCSI_SENSE_BUFFERSIZE
1278 ? sizeof(struct SENSE_DATA) : SCSI_SENSE_BUFFERSIZE;
1279 memset(sensebuffer, 0, SCSI_SENSE_BUFFERSIZE);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001280 memcpy(sensebuffer, ccb->arcmsr_cdb.SenseData, sense_data_length);
1281 sensebuffer->ErrorCode = SCSI_SENSE_CURRENT_ERRORS;
1282 sensebuffer->Valid = 1;
Ching Huangfa576b42017-12-05 11:28:37 +08001283 pcmd->result |= (DRIVER_SENSE << 24);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001284 }
1285}
1286
1287static u32 arcmsr_disable_outbound_ints(struct AdapterControlBlock *acb)
1288{
1289 u32 orig_mask = 0;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001290 switch (acb->adapter_type) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001291 case ACB_ADAPTER_TYPE_A : {
Al Viro80da1ad2007-10-29 05:08:28 +00001292 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng36b83de2010-05-17 11:22:42 +08001293 orig_mask = readl(&reg->outbound_intmask);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001294 writel(orig_mask|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE, \
1295 &reg->outbound_intmask);
1296 }
1297 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001298 case ACB_ADAPTER_TYPE_B : {
Al Viro80da1ad2007-10-29 05:08:28 +00001299 struct MessageUnit_B *reg = acb->pmuB;
Nick Chengae52e7f2010-06-18 15:39:12 +08001300 orig_mask = readl(reg->iop2drv_doorbell_mask);
1301 writel(0, reg->iop2drv_doorbell_mask);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001302 }
1303 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001304 case ACB_ADAPTER_TYPE_C:{
Ching Huangc10b1d52014-08-19 15:20:31 +08001305 struct MessageUnit_C __iomem *reg = acb->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001306 /* disable all outbound interrupt */
1307 orig_mask = readl(&reg->host_int_mask); /* disable outbound message0 int */
1308 writel(orig_mask|ARCMSR_HBCMU_ALL_INTMASKENABLE, &reg->host_int_mask);
1309 }
1310 break;
Ching Huang5b374792014-08-19 15:25:22 +08001311 case ACB_ADAPTER_TYPE_D: {
1312 struct MessageUnit_D *reg = acb->pmuD;
1313 /* disable all outbound interrupt */
1314 writel(ARCMSR_ARC1214_ALL_INT_DISABLE, reg->pcief0_int_enable);
1315 }
1316 break;
Ching Huang23509022017-12-05 09:35:34 +08001317 case ACB_ADAPTER_TYPE_E: {
1318 struct MessageUnit_E __iomem *reg = acb->pmuE;
1319 orig_mask = readl(&reg->host_int_mask);
1320 writel(orig_mask | ARCMSR_HBEMU_OUTBOUND_DOORBELL_ISR | ARCMSR_HBEMU_OUTBOUND_POSTQUEUE_ISR, &reg->host_int_mask);
1321 readl(&reg->host_int_mask); /* Dummy readl to force pci flush */
1322 }
1323 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001324 }
1325 return orig_mask;
1326}
1327
Nick Chengcdd3cb12010-07-13 20:03:04 +08001328static void arcmsr_report_ccb_state(struct AdapterControlBlock *acb,
1329 struct CommandControlBlock *ccb, bool error)
Nick Cheng1a4f5502007-09-13 17:26:40 +08001330{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001331 uint8_t id, lun;
1332 id = ccb->pcmd->device->id;
1333 lun = ccb->pcmd->device->lun;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001334 if (!error) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001335 if (acb->devstate[id][lun] == ARECA_RAID_GONE)
1336 acb->devstate[id][lun] = ARECA_RAID_GOOD;
Julia Lawall7968f192010-08-05 22:19:36 +02001337 ccb->pcmd->result = DID_OK << 16;
1338 arcmsr_ccb_complete(ccb);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001339 }else{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001340 switch (ccb->arcmsr_cdb.DeviceStatus) {
1341 case ARCMSR_DEV_SELECT_TIMEOUT: {
1342 acb->devstate[id][lun] = ARECA_RAID_GONE;
1343 ccb->pcmd->result = DID_NO_CONNECT << 16;
Nick Chengae52e7f2010-06-18 15:39:12 +08001344 arcmsr_ccb_complete(ccb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001345 }
1346 break;
1347
1348 case ARCMSR_DEV_ABORTED:
1349
1350 case ARCMSR_DEV_INIT_FAIL: {
1351 acb->devstate[id][lun] = ARECA_RAID_GONE;
1352 ccb->pcmd->result = DID_BAD_TARGET << 16;
Nick Chengae52e7f2010-06-18 15:39:12 +08001353 arcmsr_ccb_complete(ccb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001354 }
1355 break;
1356
1357 case ARCMSR_DEV_CHECK_CONDITION: {
1358 acb->devstate[id][lun] = ARECA_RAID_GOOD;
1359 arcmsr_report_sense_info(ccb);
Nick Chengae52e7f2010-06-18 15:39:12 +08001360 arcmsr_ccb_complete(ccb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001361 }
1362 break;
1363
1364 default:
Nick Chengcdd3cb12010-07-13 20:03:04 +08001365 printk(KERN_NOTICE
1366 "arcmsr%d: scsi id = %d lun = %d isr get command error done, \
1367 but got unknown DeviceStatus = 0x%x \n"
1368 , acb->host->host_no
1369 , id
1370 , lun
1371 , ccb->arcmsr_cdb.DeviceStatus);
1372 acb->devstate[id][lun] = ARECA_RAID_GONE;
1373 ccb->pcmd->result = DID_NO_CONNECT << 16;
1374 arcmsr_ccb_complete(ccb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001375 break;
1376 }
1377 }
1378}
1379
Nick Chengcdd3cb12010-07-13 20:03:04 +08001380static void arcmsr_drain_donequeue(struct AdapterControlBlock *acb, struct CommandControlBlock *pCCB, bool error)
Nick Cheng1a4f5502007-09-13 17:26:40 +08001381{
Nick Chengcdd3cb12010-07-13 20:03:04 +08001382 if ((pCCB->acb != acb) || (pCCB->startdone != ARCMSR_CCB_START)) {
1383 if (pCCB->startdone == ARCMSR_CCB_ABORTED) {
1384 struct scsi_cmnd *abortcmd = pCCB->pcmd;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001385 if (abortcmd) {
1386 abortcmd->result |= DID_ABORT << 16;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001387 arcmsr_ccb_complete(pCCB);
1388 printk(KERN_NOTICE "arcmsr%d: pCCB ='0x%p' isr got aborted command \n",
1389 acb->host->host_no, pCCB);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001390 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08001391 return;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001392 }
1393 printk(KERN_NOTICE "arcmsr%d: isr get an illegal ccb command \
1394 done acb = '0x%p'"
1395 "ccb = '0x%p' ccbacb = '0x%p' startdone = 0x%x"
1396 " ccboutstandingcount = %d \n"
1397 , acb->host->host_no
1398 , acb
Nick Chengcdd3cb12010-07-13 20:03:04 +08001399 , pCCB
1400 , pCCB->acb
1401 , pCCB->startdone
Nick Cheng1a4f5502007-09-13 17:26:40 +08001402 , atomic_read(&acb->ccboutstandingcount));
Colin Ian King9b44ffa2019-11-14 18:00:07 +00001403 return;
NickCheng97b99122011-01-06 17:32:41 +08001404 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08001405 arcmsr_report_ccb_state(acb, pCCB, error);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001406}
1407
1408static void arcmsr_done4abort_postqueue(struct AdapterControlBlock *acb)
1409{
1410 int i = 0;
Ching Huang9e386a52018-12-19 16:43:15 +08001411 uint32_t flag_ccb;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001412 struct ARCMSR_CDB *pARCMSR_CDB;
1413 bool error;
1414 struct CommandControlBlock *pCCB;
Ching Huang9e386a52018-12-19 16:43:15 +08001415 unsigned long ccb_cdb_phy, cdb_phy_hipart;
1416
Nick Cheng1a4f5502007-09-13 17:26:40 +08001417 switch (acb->adapter_type) {
1418
1419 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001420 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001421 uint32_t outbound_intstatus;
Al Viro80da1ad2007-10-29 05:08:28 +00001422 outbound_intstatus = readl(&reg->outbound_intstatus) &
Nick Cheng1a4f5502007-09-13 17:26:40 +08001423 acb->outbound_int_enable;
1424 /*clear and abort all outbound posted Q*/
1425 writel(outbound_intstatus, &reg->outbound_intstatus);/*clear interrupt*/
Nick Chengcdd3cb12010-07-13 20:03:04 +08001426 while(((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF)
Ching Huange4587f42017-12-05 09:51:27 +08001427 && (i++ < acb->maxOutstanding)) {
Ching Huang9e386a52018-12-19 16:43:15 +08001428 ccb_cdb_phy = (flag_ccb << 5) & 0xffffffff;
1429 if (acb->cdb_phyadd_hipart)
1430 ccb_cdb_phy = ccb_cdb_phy | acb->cdb_phyadd_hipart;
1431 pARCMSR_CDB = (struct ARCMSR_CDB *)(acb->vir2phy_offset + ccb_cdb_phy);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001432 pCCB = container_of(pARCMSR_CDB, struct CommandControlBlock, arcmsr_cdb);
1433 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE0) ? true : false;
1434 arcmsr_drain_donequeue(acb, pCCB, error);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001435 }
1436 }
1437 break;
1438
1439 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001440 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001441 /*clear all outbound posted Q*/
NickCheng97b99122011-01-06 17:32:41 +08001442 writel(ARCMSR_DOORBELL_INT_CLEAR_PATTERN, reg->iop2drv_doorbell); /* clear doorbell interrupt */
Nick Cheng1a4f5502007-09-13 17:26:40 +08001443 for (i = 0; i < ARCMSR_MAX_HBB_POSTQUEUE; i++) {
Ching Huangc10b1d52014-08-19 15:20:31 +08001444 flag_ccb = reg->done_qbuffer[i];
1445 if (flag_ccb != 0) {
1446 reg->done_qbuffer[i] = 0;
Ching Huange66764f2018-12-19 16:45:46 +08001447 ccb_cdb_phy = (flag_ccb << 5) & 0xffffffff;
1448 if (acb->cdb_phyadd_hipart)
1449 ccb_cdb_phy = ccb_cdb_phy | acb->cdb_phyadd_hipart;
1450 pARCMSR_CDB = (struct ARCMSR_CDB *)(acb->vir2phy_offset + ccb_cdb_phy);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001451 pCCB = container_of(pARCMSR_CDB, struct CommandControlBlock, arcmsr_cdb);
1452 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE0) ? true : false;
1453 arcmsr_drain_donequeue(acb, pCCB, error);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001454 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08001455 reg->post_qbuffer[i] = 0;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001456 }
1457 reg->doneq_index = 0;
1458 reg->postq_index = 0;
1459 }
1460 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001461 case ACB_ADAPTER_TYPE_C: {
Ching Huangc10b1d52014-08-19 15:20:31 +08001462 struct MessageUnit_C __iomem *reg = acb->pmuC;
Ching Huange4587f42017-12-05 09:51:27 +08001463 while ((readl(&reg->host_int_status) & ARCMSR_HBCMU_OUTBOUND_POSTQUEUE_ISR) && (i++ < acb->maxOutstanding)) {
Nick Chengcdd3cb12010-07-13 20:03:04 +08001464 /*need to do*/
1465 flag_ccb = readl(&reg->outbound_queueport_low);
1466 ccb_cdb_phy = (flag_ccb & 0xFFFFFFF0);
Ching Huangc71ec552018-12-19 16:48:16 +08001467 if (acb->cdb_phyadd_hipart)
1468 ccb_cdb_phy = ccb_cdb_phy | acb->cdb_phyadd_hipart;
1469 pARCMSR_CDB = (struct ARCMSR_CDB *)(acb->vir2phy_offset + ccb_cdb_phy);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001470 pCCB = container_of(pARCMSR_CDB, struct CommandControlBlock, arcmsr_cdb);
1471 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE1) ? true : false;
1472 arcmsr_drain_donequeue(acb, pCCB, error);
1473 }
Ching Huang5b374792014-08-19 15:25:22 +08001474 }
1475 break;
1476 case ACB_ADAPTER_TYPE_D: {
1477 struct MessageUnit_D *pmu = acb->pmuD;
Ching Huang3b8155d2014-09-15 19:05:33 +08001478 uint32_t outbound_write_pointer;
1479 uint32_t doneq_index, index_stripped, addressLow, residual, toggle;
1480 unsigned long flags;
Ching Huang5b374792014-08-19 15:25:22 +08001481
Ching Huang5b374792014-08-19 15:25:22 +08001482 residual = atomic_read(&acb->ccboutstandingcount);
1483 for (i = 0; i < residual; i++) {
Ching Huang3b8155d2014-09-15 19:05:33 +08001484 spin_lock_irqsave(&acb->doneq_lock, flags);
1485 outbound_write_pointer =
1486 pmu->done_qbuffer[0].addressLow + 1;
1487 doneq_index = pmu->doneq_index;
1488 if ((doneq_index & 0xFFF) !=
Ching Huang5b374792014-08-19 15:25:22 +08001489 (outbound_write_pointer & 0xFFF)) {
Ching Huang3b8155d2014-09-15 19:05:33 +08001490 toggle = doneq_index & 0x4000;
1491 index_stripped = (doneq_index & 0xFFF) + 1;
1492 index_stripped %= ARCMSR_MAX_ARC1214_DONEQUEUE;
1493 pmu->doneq_index = index_stripped ? (index_stripped | toggle) :
1494 ((toggle ^ 0x4000) + 1);
Ching Huang5b374792014-08-19 15:25:22 +08001495 doneq_index = pmu->doneq_index;
Ching Huang3b8155d2014-09-15 19:05:33 +08001496 spin_unlock_irqrestore(&acb->doneq_lock, flags);
Ching Huanga36ade42018-12-19 16:51:14 +08001497 cdb_phy_hipart = pmu->done_qbuffer[doneq_index &
1498 0xFFF].addressHigh;
Ching Huang5b374792014-08-19 15:25:22 +08001499 addressLow = pmu->done_qbuffer[doneq_index &
1500 0xFFF].addressLow;
1501 ccb_cdb_phy = (addressLow & 0xFFFFFFF0);
Ching Huanga36ade42018-12-19 16:51:14 +08001502 if (acb->cdb_phyadd_hipart)
1503 ccb_cdb_phy = ccb_cdb_phy | acb->cdb_phyadd_hipart;
Ching Huang5b374792014-08-19 15:25:22 +08001504 pARCMSR_CDB = (struct ARCMSR_CDB *)
1505 (acb->vir2phy_offset + ccb_cdb_phy);
1506 pCCB = container_of(pARCMSR_CDB,
1507 struct CommandControlBlock, arcmsr_cdb);
1508 error = (addressLow &
1509 ARCMSR_CCBREPLY_FLAG_ERROR_MODE1) ?
1510 true : false;
1511 arcmsr_drain_donequeue(acb, pCCB, error);
1512 writel(doneq_index,
1513 pmu->outboundlist_read_pointer);
Ching Huang3b8155d2014-09-15 19:05:33 +08001514 } else {
1515 spin_unlock_irqrestore(&acb->doneq_lock, flags);
1516 mdelay(10);
Ching Huang5b374792014-08-19 15:25:22 +08001517 }
Ching Huang5b374792014-08-19 15:25:22 +08001518 }
1519 pmu->postq_index = 0;
1520 pmu->doneq_index = 0x40FF;
1521 }
1522 break;
Ching Huang23509022017-12-05 09:35:34 +08001523 case ACB_ADAPTER_TYPE_E:
1524 arcmsr_hbaE_postqueue_isr(acb);
1525 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001526 }
1527}
Ching Huang1d1166e2014-08-19 14:23:31 +08001528
Ching Huangc4c1adb2018-03-15 14:33:36 +08001529static void arcmsr_remove_scsi_devices(struct AdapterControlBlock *acb)
1530{
1531 char *acb_dev_map = (char *)acb->device_map;
1532 int target, lun, i;
1533 struct scsi_device *psdev;
1534 struct CommandControlBlock *ccb;
1535 char temp;
1536
1537 for (i = 0; i < acb->maxFreeCCB; i++) {
1538 ccb = acb->pccb_pool[i];
1539 if (ccb->startdone == ARCMSR_CCB_START) {
1540 ccb->pcmd->result = DID_NO_CONNECT << 16;
1541 arcmsr_pci_unmap_dma(ccb);
1542 ccb->pcmd->scsi_done(ccb->pcmd);
1543 }
1544 }
1545 for (target = 0; target < ARCMSR_MAX_TARGETID; target++) {
1546 temp = *acb_dev_map;
1547 if (temp) {
1548 for (lun = 0; lun < ARCMSR_MAX_TARGETLUN; lun++) {
1549 if (temp & 1) {
1550 psdev = scsi_device_lookup(acb->host,
1551 0, target, lun);
1552 if (psdev != NULL) {
1553 scsi_remove_device(psdev);
1554 scsi_device_put(psdev);
1555 }
1556 }
1557 temp >>= 1;
1558 }
1559 *acb_dev_map = 0;
1560 }
1561 acb_dev_map++;
1562 }
1563}
1564
1565static void arcmsr_free_pcidev(struct AdapterControlBlock *acb)
1566{
1567 struct pci_dev *pdev;
1568 struct Scsi_Host *host;
1569
1570 host = acb->host;
1571 arcmsr_free_sysfs_attr(acb);
1572 scsi_remove_host(host);
1573 flush_work(&acb->arcmsr_do_message_isr_bh);
1574 del_timer_sync(&acb->eternal_timer);
1575 if (set_date_time)
1576 del_timer_sync(&acb->refresh_timer);
1577 pdev = acb->pdev;
1578 arcmsr_free_irq(pdev, acb);
1579 arcmsr_free_ccb_pool(acb);
Ching Huangc4c1adb2018-03-15 14:33:36 +08001580 arcmsr_unmap_pciregion(acb);
1581 pci_release_regions(pdev);
1582 scsi_host_put(host);
1583 pci_disable_device(pdev);
1584}
1585
Erich Chen1c57e862006-07-12 08:59:32 -07001586static void arcmsr_remove(struct pci_dev *pdev)
1587{
1588 struct Scsi_Host *host = pci_get_drvdata(pdev);
1589 struct AdapterControlBlock *acb =
1590 (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -07001591 int poll_count = 0;
Ching Huangc4c1adb2018-03-15 14:33:36 +08001592 uint16_t dev_id;
1593
1594 pci_read_config_word(pdev, PCI_DEVICE_ID, &dev_id);
1595 if (dev_id == 0xffff) {
1596 acb->acb_flags &= ~ACB_F_IOP_INITED;
1597 acb->acb_flags |= ACB_F_ADAPTER_REMOVED;
1598 arcmsr_remove_scsi_devices(acb);
1599 arcmsr_free_pcidev(acb);
1600 return;
1601 }
Erich Chen1c57e862006-07-12 08:59:32 -07001602 arcmsr_free_sysfs_attr(acb);
1603 scsi_remove_host(host);
Tejun Heo43829732012-08-20 14:51:24 -07001604 flush_work(&acb->arcmsr_do_message_isr_bh);
Nick Cheng36b83de2010-05-17 11:22:42 +08001605 del_timer_sync(&acb->eternal_timer);
Ching Huangb416c092017-12-05 09:59:52 +08001606 if (set_date_time)
1607 del_timer_sync(&acb->refresh_timer);
Nick Cheng36b83de2010-05-17 11:22:42 +08001608 arcmsr_disable_outbound_ints(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001609 arcmsr_stop_adapter_bgrb(acb);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001610 arcmsr_flush_adapter_cache(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001611 acb->acb_flags |= ACB_F_SCSISTOPADAPTER;
1612 acb->acb_flags &= ~ACB_F_IOP_INITED;
1613
Ching Huange4587f42017-12-05 09:51:27 +08001614 for (poll_count = 0; poll_count < acb->maxOutstanding; poll_count++){
Erich Chen1c57e862006-07-12 08:59:32 -07001615 if (!atomic_read(&acb->ccboutstandingcount))
1616 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001617 arcmsr_interrupt(acb);/* FIXME: need spinlock */
Erich Chen1c57e862006-07-12 08:59:32 -07001618 msleep(25);
1619 }
1620
1621 if (atomic_read(&acb->ccboutstandingcount)) {
1622 int i;
1623
1624 arcmsr_abort_allcmd(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001625 arcmsr_done4abort_postqueue(acb);
Ching Huangd076e4a2017-12-05 09:44:23 +08001626 for (i = 0; i < acb->maxFreeCCB; i++) {
Erich Chen1c57e862006-07-12 08:59:32 -07001627 struct CommandControlBlock *ccb = acb->pccb_pool[i];
1628 if (ccb->startdone == ARCMSR_CCB_START) {
1629 ccb->startdone = ARCMSR_CCB_ABORTED;
1630 ccb->pcmd->result = DID_ABORT << 16;
Nick Chengae52e7f2010-06-18 15:39:12 +08001631 arcmsr_ccb_complete(ccb);
Erich Chen1c57e862006-07-12 08:59:32 -07001632 }
1633 }
1634 }
Ching Huang1d1166e2014-08-19 14:23:31 +08001635 arcmsr_free_irq(pdev, acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001636 arcmsr_free_ccb_pool(acb);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001637 arcmsr_unmap_pciregion(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07001638 pci_release_regions(pdev);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001639 scsi_host_put(host);
Erich Chen1c57e862006-07-12 08:59:32 -07001640 pci_disable_device(pdev);
Erich Chen1c57e862006-07-12 08:59:32 -07001641}
1642
1643static void arcmsr_shutdown(struct pci_dev *pdev)
1644{
1645 struct Scsi_Host *host = pci_get_drvdata(pdev);
1646 struct AdapterControlBlock *acb =
1647 (struct AdapterControlBlock *)host->hostdata;
Ching Huangc4c1adb2018-03-15 14:33:36 +08001648 if (acb->acb_flags & ACB_F_ADAPTER_REMOVED)
1649 return;
Nick Cheng36b83de2010-05-17 11:22:42 +08001650 del_timer_sync(&acb->eternal_timer);
Ching Huangb416c092017-12-05 09:59:52 +08001651 if (set_date_time)
1652 del_timer_sync(&acb->refresh_timer);
Nick Cheng36b83de2010-05-17 11:22:42 +08001653 arcmsr_disable_outbound_ints(acb);
Ching Huang1d1166e2014-08-19 14:23:31 +08001654 arcmsr_free_irq(pdev, acb);
Tejun Heo43829732012-08-20 14:51:24 -07001655 flush_work(&acb->arcmsr_do_message_isr_bh);
Erich Chen1c57e862006-07-12 08:59:32 -07001656 arcmsr_stop_adapter_bgrb(acb);
1657 arcmsr_flush_adapter_cache(acb);
1658}
1659
1660static int arcmsr_module_init(void)
1661{
1662 int error = 0;
Erich Chen1c57e862006-07-12 08:59:32 -07001663 error = pci_register_driver(&arcmsr_pci_driver);
1664 return error;
1665}
1666
1667static void arcmsr_module_exit(void)
1668{
1669 pci_unregister_driver(&arcmsr_pci_driver);
1670}
1671module_init(arcmsr_module_init);
1672module_exit(arcmsr_module_exit);
1673
Nick Cheng36b83de2010-05-17 11:22:42 +08001674static void arcmsr_enable_outbound_ints(struct AdapterControlBlock *acb,
Nick Cheng1a4f5502007-09-13 17:26:40 +08001675 u32 intmask_org)
Erich Chen1c57e862006-07-12 08:59:32 -07001676{
Erich Chen1c57e862006-07-12 08:59:32 -07001677 u32 mask;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001678 switch (acb->adapter_type) {
1679
Nick Chengcdd3cb12010-07-13 20:03:04 +08001680 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001681 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001682 mask = intmask_org & ~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE |
Nick Cheng36b83de2010-05-17 11:22:42 +08001683 ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE|
1684 ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001685 writel(mask, &reg->outbound_intmask);
1686 acb->outbound_int_enable = ~(intmask_org & mask) & 0x000000ff;
1687 }
1688 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001689
Nick Chengcdd3cb12010-07-13 20:03:04 +08001690 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001691 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng36b83de2010-05-17 11:22:42 +08001692 mask = intmask_org | (ARCMSR_IOP2DRV_DATA_WRITE_OK |
1693 ARCMSR_IOP2DRV_DATA_READ_OK |
1694 ARCMSR_IOP2DRV_CDB_DONE |
1695 ARCMSR_IOP2DRV_MESSAGE_CMD_DONE);
Nick Chengae52e7f2010-06-18 15:39:12 +08001696 writel(mask, reg->iop2drv_doorbell_mask);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001697 acb->outbound_int_enable = (intmask_org | mask) & 0x0000000f;
1698 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08001699 break;
1700 case ACB_ADAPTER_TYPE_C: {
Ching Huangc10b1d52014-08-19 15:20:31 +08001701 struct MessageUnit_C __iomem *reg = acb->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001702 mask = ~(ARCMSR_HBCMU_UTILITY_A_ISR_MASK | ARCMSR_HBCMU_OUTBOUND_DOORBELL_ISR_MASK|ARCMSR_HBCMU_OUTBOUND_POSTQUEUE_ISR_MASK);
1703 writel(intmask_org & mask, &reg->host_int_mask);
1704 acb->outbound_int_enable = ~(intmask_org & mask) & 0x0000000f;
1705 }
Ching Huang5b374792014-08-19 15:25:22 +08001706 break;
1707 case ACB_ADAPTER_TYPE_D: {
1708 struct MessageUnit_D *reg = acb->pmuD;
1709
1710 mask = ARCMSR_ARC1214_ALL_INT_ENABLE;
1711 writel(intmask_org | mask, reg->pcief0_int_enable);
1712 break;
1713 }
Ching Huang23509022017-12-05 09:35:34 +08001714 case ACB_ADAPTER_TYPE_E: {
1715 struct MessageUnit_E __iomem *reg = acb->pmuE;
1716
1717 mask = ~(ARCMSR_HBEMU_OUTBOUND_DOORBELL_ISR | ARCMSR_HBEMU_OUTBOUND_POSTQUEUE_ISR);
1718 writel(intmask_org & mask, &reg->host_int_mask);
1719 break;
1720 }
Erich Chen1c57e862006-07-12 08:59:32 -07001721 }
1722}
1723
Nick Cheng76d78302008-02-04 23:53:24 -08001724static int arcmsr_build_ccb(struct AdapterControlBlock *acb,
Erich Chen1c57e862006-07-12 08:59:32 -07001725 struct CommandControlBlock *ccb, struct scsi_cmnd *pcmd)
1726{
1727 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
1728 int8_t *psge = (int8_t *)&arcmsr_cdb->u;
Al Viro80da1ad2007-10-29 05:08:28 +00001729 __le32 address_lo, address_hi;
Erich Chen1c57e862006-07-12 08:59:32 -07001730 int arccdbsize = 0x30;
Nick Chengae52e7f2010-06-18 15:39:12 +08001731 __le32 length = 0;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001732 int i;
Nick Chengae52e7f2010-06-18 15:39:12 +08001733 struct scatterlist *sg;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001734 int nseg;
Erich Chen1c57e862006-07-12 08:59:32 -07001735 ccb->pcmd = pcmd;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001736 memset(arcmsr_cdb, 0, sizeof(struct ARCMSR_CDB));
Erich Chen1c57e862006-07-12 08:59:32 -07001737 arcmsr_cdb->TargetID = pcmd->device->id;
1738 arcmsr_cdb->LUN = pcmd->device->lun;
1739 arcmsr_cdb->Function = 1;
Ching Huang626fa322014-08-19 15:10:12 +08001740 arcmsr_cdb->msgContext = 0;
Erich Chen1c57e862006-07-12 08:59:32 -07001741 memcpy(arcmsr_cdb->Cdb, pcmd->cmnd, pcmd->cmd_len);
Erich Chen1c57e862006-07-12 08:59:32 -07001742
FUJITA Tomonorideff2622007-05-14 19:25:56 +09001743 nseg = scsi_dma_map(pcmd);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001744 if (unlikely(nseg > acb->host->sg_tablesize || nseg < 0))
Nick Cheng76d78302008-02-04 23:53:24 -08001745 return FAILED;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001746 scsi_for_each_sg(pcmd, sg, nseg, i) {
1747 /* Get the physical address of the current data pointer */
1748 length = cpu_to_le32(sg_dma_len(sg));
1749 address_lo = cpu_to_le32(dma_addr_lo32(sg_dma_address(sg)));
1750 address_hi = cpu_to_le32(dma_addr_hi32(sg_dma_address(sg)));
1751 if (address_hi == 0) {
1752 struct SG32ENTRY *pdma_sg = (struct SG32ENTRY *)psge;
Erich Chen1c57e862006-07-12 08:59:32 -07001753
Nick Chengcdd3cb12010-07-13 20:03:04 +08001754 pdma_sg->address = address_lo;
1755 pdma_sg->length = length;
1756 psge += sizeof (struct SG32ENTRY);
1757 arccdbsize += sizeof (struct SG32ENTRY);
1758 } else {
1759 struct SG64ENTRY *pdma_sg = (struct SG64ENTRY *)psge;
Erich Chen1c57e862006-07-12 08:59:32 -07001760
Nick Chengcdd3cb12010-07-13 20:03:04 +08001761 pdma_sg->addresshigh = address_hi;
1762 pdma_sg->address = address_lo;
1763 pdma_sg->length = length|cpu_to_le32(IS_SG64_ADDR);
1764 psge += sizeof (struct SG64ENTRY);
1765 arccdbsize += sizeof (struct SG64ENTRY);
Erich Chen1c57e862006-07-12 08:59:32 -07001766 }
Erich Chen1c57e862006-07-12 08:59:32 -07001767 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08001768 arcmsr_cdb->sgcount = (uint8_t)nseg;
1769 arcmsr_cdb->DataLength = scsi_bufflen(pcmd);
1770 arcmsr_cdb->msgPages = arccdbsize/0x100 + (arccdbsize % 0x100 ? 1 : 0);
1771 if ( arccdbsize > 256)
1772 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_SGL_BSIZE;
roel kluinc32e0612011-01-01 19:40:23 +01001773 if (pcmd->sc_data_direction == DMA_TO_DEVICE)
Nick Chengcdd3cb12010-07-13 20:03:04 +08001774 arcmsr_cdb->Flags |= ARCMSR_CDB_FLAG_WRITE;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001775 ccb->arc_cdb_size = arccdbsize;
Nick Cheng76d78302008-02-04 23:53:24 -08001776 return SUCCESS;
Erich Chen1c57e862006-07-12 08:59:32 -07001777}
1778
1779static void arcmsr_post_ccb(struct AdapterControlBlock *acb, struct CommandControlBlock *ccb)
1780{
Ching Huang626fa322014-08-19 15:10:12 +08001781 uint32_t cdb_phyaddr = ccb->cdb_phyaddr;
Erich Chen1c57e862006-07-12 08:59:32 -07001782 struct ARCMSR_CDB *arcmsr_cdb = (struct ARCMSR_CDB *)&ccb->arcmsr_cdb;
Erich Chen1c57e862006-07-12 08:59:32 -07001783 atomic_inc(&acb->ccboutstandingcount);
1784 ccb->startdone = ARCMSR_CCB_START;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001785 switch (acb->adapter_type) {
1786 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001787 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001788
1789 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE)
Ching Huang626fa322014-08-19 15:10:12 +08001790 writel(cdb_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE,
Erich Chen1c57e862006-07-12 08:59:32 -07001791 &reg->inbound_queueport);
Ching Huang626fa322014-08-19 15:10:12 +08001792 else
1793 writel(cdb_phyaddr, &reg->inbound_queueport);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001794 break;
Ching Huang626fa322014-08-19 15:10:12 +08001795 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001796
1797 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001798 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001799 uint32_t ending_index, index = reg->postq_index;
1800
1801 ending_index = ((index + 1) % ARCMSR_MAX_HBB_POSTQUEUE);
Ching Huangc10b1d52014-08-19 15:20:31 +08001802 reg->post_qbuffer[ending_index] = 0;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001803 if (arcmsr_cdb->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE) {
Ching Huangc10b1d52014-08-19 15:20:31 +08001804 reg->post_qbuffer[index] =
1805 cdb_phyaddr | ARCMSR_CCBPOST_FLAG_SGL_BSIZE;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001806 } else {
Ching Huangc10b1d52014-08-19 15:20:31 +08001807 reg->post_qbuffer[index] = cdb_phyaddr;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001808 }
1809 index++;
1810 index %= ARCMSR_MAX_HBB_POSTQUEUE;/*if last index number set it to 0 */
1811 reg->postq_index = index;
Nick Chengae52e7f2010-06-18 15:39:12 +08001812 writel(ARCMSR_DRV2IOP_CDB_POSTED, reg->drv2iop_doorbell);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001813 }
1814 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001815 case ACB_ADAPTER_TYPE_C: {
Ching Huangc10b1d52014-08-19 15:20:31 +08001816 struct MessageUnit_C __iomem *phbcmu = acb->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001817 uint32_t ccb_post_stamp, arc_cdb_size;
1818
1819 arc_cdb_size = (ccb->arc_cdb_size > 0x300) ? 0x300 : ccb->arc_cdb_size;
Ching Huang626fa322014-08-19 15:10:12 +08001820 ccb_post_stamp = (cdb_phyaddr | ((arc_cdb_size - 1) >> 6) | 1);
Ching Huangc71ec552018-12-19 16:48:16 +08001821 writel(upper_32_bits(ccb->cdb_phyaddr), &phbcmu->inbound_queueport_high);
1822 writel(ccb_post_stamp, &phbcmu->inbound_queueport_low);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001823 }
Ching Huang5b374792014-08-19 15:25:22 +08001824 break;
1825 case ACB_ADAPTER_TYPE_D: {
1826 struct MessageUnit_D *pmu = acb->pmuD;
1827 u16 index_stripped;
Ching Huang3b8155d2014-09-15 19:05:33 +08001828 u16 postq_index, toggle;
Ching Huang5b374792014-08-19 15:25:22 +08001829 unsigned long flags;
1830 struct InBound_SRB *pinbound_srb;
1831
1832 spin_lock_irqsave(&acb->postq_lock, flags);
1833 postq_index = pmu->postq_index;
1834 pinbound_srb = (struct InBound_SRB *)&(pmu->post_qbuffer[postq_index & 0xFF]);
Ching Huanga36ade42018-12-19 16:51:14 +08001835 pinbound_srb->addressHigh = upper_32_bits(ccb->cdb_phyaddr);
1836 pinbound_srb->addressLow = cdb_phyaddr;
Ching Huang5b374792014-08-19 15:25:22 +08001837 pinbound_srb->length = ccb->arc_cdb_size >> 2;
1838 arcmsr_cdb->msgContext = dma_addr_lo32(cdb_phyaddr);
Ching Huang3b8155d2014-09-15 19:05:33 +08001839 toggle = postq_index & 0x4000;
1840 index_stripped = postq_index + 1;
1841 index_stripped &= (ARCMSR_MAX_ARC1214_POSTQUEUE - 1);
1842 pmu->postq_index = index_stripped ? (index_stripped | toggle) :
1843 (toggle ^ 0x4000);
Ching Huang5b374792014-08-19 15:25:22 +08001844 writel(postq_index, pmu->inboundlist_write_pointer);
1845 spin_unlock_irqrestore(&acb->postq_lock, flags);
1846 break;
1847 }
Ching Huang23509022017-12-05 09:35:34 +08001848 case ACB_ADAPTER_TYPE_E: {
1849 struct MessageUnit_E __iomem *pmu = acb->pmuE;
1850 u32 ccb_post_stamp, arc_cdb_size;
1851
1852 arc_cdb_size = (ccb->arc_cdb_size > 0x300) ? 0x300 : ccb->arc_cdb_size;
1853 ccb_post_stamp = (ccb->smid | ((arc_cdb_size - 1) >> 6));
1854 writel(0, &pmu->inbound_queueport_high);
1855 writel(ccb_post_stamp, &pmu->inbound_queueport_low);
1856 break;
1857 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08001858 }
Erich Chen1c57e862006-07-12 08:59:32 -07001859}
1860
Ching Huang626fa322014-08-19 15:10:12 +08001861static void arcmsr_hbaA_stop_bgrb(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07001862{
Al Viro80da1ad2007-10-29 05:08:28 +00001863 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001864 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
1865 writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, &reg->inbound_msgaddr0);
Ching Huang626fa322014-08-19 15:10:12 +08001866 if (!arcmsr_hbaA_wait_msgint_ready(acb)) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001867 printk(KERN_NOTICE
Geert Uytterhoeven948dff7a2018-09-03 19:35:29 +02001868 "arcmsr%d: wait 'stop adapter background rebuild' timeout\n"
Nick Cheng1a4f5502007-09-13 17:26:40 +08001869 , acb->host->host_no);
1870 }
1871}
1872
Ching Huang626fa322014-08-19 15:10:12 +08001873static void arcmsr_hbaB_stop_bgrb(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08001874{
Al Viro80da1ad2007-10-29 05:08:28 +00001875 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001876 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
Nick Chengae52e7f2010-06-18 15:39:12 +08001877 writel(ARCMSR_MESSAGE_STOP_BGRB, reg->drv2iop_doorbell);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001878
Ching Huang626fa322014-08-19 15:10:12 +08001879 if (!arcmsr_hbaB_wait_msgint_ready(acb)) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08001880 printk(KERN_NOTICE
Geert Uytterhoeven948dff7a2018-09-03 19:35:29 +02001881 "arcmsr%d: wait 'stop adapter background rebuild' timeout\n"
Nick Cheng1a4f5502007-09-13 17:26:40 +08001882 , acb->host->host_no);
Erich Chen1c57e862006-07-12 08:59:32 -07001883 }
1884}
1885
Ching Huang626fa322014-08-19 15:10:12 +08001886static void arcmsr_hbaC_stop_bgrb(struct AdapterControlBlock *pACB)
Nick Chengcdd3cb12010-07-13 20:03:04 +08001887{
Ching Huangc10b1d52014-08-19 15:20:31 +08001888 struct MessageUnit_C __iomem *reg = pACB->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001889 pACB->acb_flags &= ~ACB_F_MSG_START_BGRB;
1890 writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, &reg->inbound_msgaddr0);
1891 writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &reg->inbound_doorbell);
Ching Huang626fa322014-08-19 15:10:12 +08001892 if (!arcmsr_hbaC_wait_msgint_ready(pACB)) {
Nick Chengcdd3cb12010-07-13 20:03:04 +08001893 printk(KERN_NOTICE
Geert Uytterhoeven948dff7a2018-09-03 19:35:29 +02001894 "arcmsr%d: wait 'stop adapter background rebuild' timeout\n"
Nick Chengcdd3cb12010-07-13 20:03:04 +08001895 , pACB->host->host_no);
1896 }
1897 return;
1898}
Ching Huang5b374792014-08-19 15:25:22 +08001899
1900static void arcmsr_hbaD_stop_bgrb(struct AdapterControlBlock *pACB)
1901{
1902 struct MessageUnit_D *reg = pACB->pmuD;
1903
1904 pACB->acb_flags &= ~ACB_F_MSG_START_BGRB;
1905 writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, reg->inbound_msgaddr0);
1906 if (!arcmsr_hbaD_wait_msgint_ready(pACB))
Geert Uytterhoeven948dff7a2018-09-03 19:35:29 +02001907 pr_notice("arcmsr%d: wait 'stop adapter background rebuild' "
Ching Huang5b374792014-08-19 15:25:22 +08001908 "timeout\n", pACB->host->host_no);
1909}
1910
Ching Huang23509022017-12-05 09:35:34 +08001911static void arcmsr_hbaE_stop_bgrb(struct AdapterControlBlock *pACB)
1912{
1913 struct MessageUnit_E __iomem *reg = pACB->pmuE;
1914
1915 pACB->acb_flags &= ~ACB_F_MSG_START_BGRB;
1916 writel(ARCMSR_INBOUND_MESG0_STOP_BGRB, &reg->inbound_msgaddr0);
1917 pACB->out_doorbell ^= ARCMSR_HBEMU_DRV2IOP_MESSAGE_CMD_DONE;
1918 writel(pACB->out_doorbell, &reg->iobound_doorbell);
1919 if (!arcmsr_hbaE_wait_msgint_ready(pACB)) {
Geert Uytterhoeven948dff7a2018-09-03 19:35:29 +02001920 pr_notice("arcmsr%d: wait 'stop adapter background rebuild' "
Ching Huang23509022017-12-05 09:35:34 +08001921 "timeout\n", pACB->host->host_no);
1922 }
1923}
1924
Erich Chen1c57e862006-07-12 08:59:32 -07001925static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb)
1926{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001927 switch (acb->adapter_type) {
1928 case ACB_ADAPTER_TYPE_A: {
Ching Huang626fa322014-08-19 15:10:12 +08001929 arcmsr_hbaA_stop_bgrb(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001930 }
1931 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001932
Nick Cheng1a4f5502007-09-13 17:26:40 +08001933 case ACB_ADAPTER_TYPE_B: {
Ching Huang626fa322014-08-19 15:10:12 +08001934 arcmsr_hbaB_stop_bgrb(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001935 }
1936 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001937 case ACB_ADAPTER_TYPE_C: {
Ching Huang626fa322014-08-19 15:10:12 +08001938 arcmsr_hbaC_stop_bgrb(acb);
Nick Chengcdd3cb12010-07-13 20:03:04 +08001939 }
Ching Huang5b374792014-08-19 15:25:22 +08001940 break;
1941 case ACB_ADAPTER_TYPE_D:
1942 arcmsr_hbaD_stop_bgrb(acb);
1943 break;
Ching Huang23509022017-12-05 09:35:34 +08001944 case ACB_ADAPTER_TYPE_E:
1945 arcmsr_hbaE_stop_bgrb(acb);
1946 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001947 }
Erich Chen1c57e862006-07-12 08:59:32 -07001948}
1949
1950static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb)
1951{
Nick Chengcdd3cb12010-07-13 20:03:04 +08001952 dma_free_coherent(&acb->pdev->dev, acb->uncache_size, acb->dma_coherent, acb->dma_coherent_handle);
Erich Chen1c57e862006-07-12 08:59:32 -07001953}
1954
Ching Huangc10b1d52014-08-19 15:20:31 +08001955static void arcmsr_iop_message_read(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07001956{
Nick Cheng1a4f5502007-09-13 17:26:40 +08001957 switch (acb->adapter_type) {
1958 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001959 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001960 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, &reg->inbound_doorbell);
1961 }
1962 break;
Erich Chen1c57e862006-07-12 08:59:32 -07001963
Nick Cheng1a4f5502007-09-13 17:26:40 +08001964 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00001965 struct MessageUnit_B *reg = acb->pmuB;
Nick Chengae52e7f2010-06-18 15:39:12 +08001966 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell);
Nick Cheng1a4f5502007-09-13 17:26:40 +08001967 }
1968 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08001969 case ACB_ADAPTER_TYPE_C: {
1970 struct MessageUnit_C __iomem *reg = acb->pmuC;
Ching Huang5b374792014-08-19 15:25:22 +08001971
Nick Chengcdd3cb12010-07-13 20:03:04 +08001972 writel(ARCMSR_HBCMU_DRV2IOP_DATA_READ_OK, &reg->inbound_doorbell);
1973 }
Ching Huang5b374792014-08-19 15:25:22 +08001974 break;
1975 case ACB_ADAPTER_TYPE_D: {
1976 struct MessageUnit_D *reg = acb->pmuD;
1977 writel(ARCMSR_ARC1214_DRV2IOP_DATA_OUT_READ,
1978 reg->inbound_doorbell);
1979 }
1980 break;
Ching Huang23509022017-12-05 09:35:34 +08001981 case ACB_ADAPTER_TYPE_E: {
1982 struct MessageUnit_E __iomem *reg = acb->pmuE;
1983 acb->out_doorbell ^= ARCMSR_HBEMU_DRV2IOP_DATA_READ_OK;
1984 writel(acb->out_doorbell, &reg->iobound_doorbell);
1985 }
1986 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001987 }
1988}
1989
1990static void arcmsr_iop_message_wrote(struct AdapterControlBlock *acb)
1991{
1992 switch (acb->adapter_type) {
1993 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00001994 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08001995 /*
1996 ** push inbound doorbell tell iop, driver data write ok
1997 ** and wait reply on next hwinterrupt for next Qbuffer post
1998 */
1999 writel(ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK, &reg->inbound_doorbell);
2000 }
2001 break;
2002
2003 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00002004 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002005 /*
2006 ** push inbound doorbell tell iop, driver data write ok
2007 ** and wait reply on next hwinterrupt for next Qbuffer post
2008 */
Nick Chengae52e7f2010-06-18 15:39:12 +08002009 writel(ARCMSR_DRV2IOP_DATA_WRITE_OK, reg->drv2iop_doorbell);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002010 }
2011 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002012 case ACB_ADAPTER_TYPE_C: {
2013 struct MessageUnit_C __iomem *reg = acb->pmuC;
2014 /*
2015 ** push inbound doorbell tell iop, driver data write ok
2016 ** and wait reply on next hwinterrupt for next Qbuffer post
2017 */
2018 writel(ARCMSR_HBCMU_DRV2IOP_DATA_WRITE_OK, &reg->inbound_doorbell);
2019 }
2020 break;
Ching Huang5b374792014-08-19 15:25:22 +08002021 case ACB_ADAPTER_TYPE_D: {
2022 struct MessageUnit_D *reg = acb->pmuD;
2023 writel(ARCMSR_ARC1214_DRV2IOP_DATA_IN_READY,
2024 reg->inbound_doorbell);
2025 }
2026 break;
Ching Huang23509022017-12-05 09:35:34 +08002027 case ACB_ADAPTER_TYPE_E: {
2028 struct MessageUnit_E __iomem *reg = acb->pmuE;
2029 acb->out_doorbell ^= ARCMSR_HBEMU_DRV2IOP_DATA_WRITE_OK;
2030 writel(acb->out_doorbell, &reg->iobound_doorbell);
2031 }
2032 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002033 }
2034}
2035
Al Viro80da1ad2007-10-29 05:08:28 +00002036struct QBUFFER __iomem *arcmsr_get_iop_rqbuffer(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08002037{
Al Viro0c7eb2e2007-10-29 05:08:58 +00002038 struct QBUFFER __iomem *qbuffer = NULL;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002039 switch (acb->adapter_type) {
2040
2041 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00002042 struct MessageUnit_A __iomem *reg = acb->pmuA;
2043 qbuffer = (struct QBUFFER __iomem *)&reg->message_rbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002044 }
2045 break;
2046
2047 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00002048 struct MessageUnit_B *reg = acb->pmuB;
Nick Chengae52e7f2010-06-18 15:39:12 +08002049 qbuffer = (struct QBUFFER __iomem *)reg->message_rbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002050 }
2051 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002052 case ACB_ADAPTER_TYPE_C: {
Ching Huangc10b1d52014-08-19 15:20:31 +08002053 struct MessageUnit_C __iomem *phbcmu = acb->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002054 qbuffer = (struct QBUFFER __iomem *)&phbcmu->message_rbuffer;
2055 }
Ching Huang5b374792014-08-19 15:25:22 +08002056 break;
2057 case ACB_ADAPTER_TYPE_D: {
2058 struct MessageUnit_D *reg = acb->pmuD;
2059 qbuffer = (struct QBUFFER __iomem *)reg->message_rbuffer;
2060 }
2061 break;
Ching Huang23509022017-12-05 09:35:34 +08002062 case ACB_ADAPTER_TYPE_E: {
2063 struct MessageUnit_E __iomem *reg = acb->pmuE;
2064 qbuffer = (struct QBUFFER __iomem *)&reg->message_rbuffer;
2065 }
2066 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002067 }
2068 return qbuffer;
2069}
2070
Al Viro80da1ad2007-10-29 05:08:28 +00002071static struct QBUFFER __iomem *arcmsr_get_iop_wqbuffer(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08002072{
Al Viro0c7eb2e2007-10-29 05:08:58 +00002073 struct QBUFFER __iomem *pqbuffer = NULL;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002074 switch (acb->adapter_type) {
2075
2076 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00002077 struct MessageUnit_A __iomem *reg = acb->pmuA;
2078 pqbuffer = (struct QBUFFER __iomem *) &reg->message_wbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002079 }
2080 break;
2081
2082 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00002083 struct MessageUnit_B *reg = acb->pmuB;
Nick Chengae52e7f2010-06-18 15:39:12 +08002084 pqbuffer = (struct QBUFFER __iomem *)reg->message_wbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002085 }
2086 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002087 case ACB_ADAPTER_TYPE_C: {
Ching Huangc10b1d52014-08-19 15:20:31 +08002088 struct MessageUnit_C __iomem *reg = acb->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002089 pqbuffer = (struct QBUFFER __iomem *)&reg->message_wbuffer;
Ching Huang5b374792014-08-19 15:25:22 +08002090 }
2091 break;
2092 case ACB_ADAPTER_TYPE_D: {
2093 struct MessageUnit_D *reg = acb->pmuD;
2094 pqbuffer = (struct QBUFFER __iomem *)reg->message_wbuffer;
2095 }
2096 break;
Ching Huang23509022017-12-05 09:35:34 +08002097 case ACB_ADAPTER_TYPE_E: {
2098 struct MessageUnit_E __iomem *reg = acb->pmuE;
2099 pqbuffer = (struct QBUFFER __iomem *)&reg->message_wbuffer;
2100 }
2101 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002102 }
2103 return pqbuffer;
2104}
2105
Ching Huangbb263c42014-08-19 15:17:45 +08002106static uint32_t
2107arcmsr_Read_iop_rqbuffer_in_DWORD(struct AdapterControlBlock *acb,
2108 struct QBUFFER __iomem *prbuffer)
Nick Cheng1a4f5502007-09-13 17:26:40 +08002109{
Ching Huangbb263c42014-08-19 15:17:45 +08002110 uint8_t *pQbuffer;
2111 uint8_t *buf1 = NULL;
2112 uint32_t __iomem *iop_data;
2113 uint32_t iop_len, data_len, *buf2 = NULL;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002114
Ching Huangbb263c42014-08-19 15:17:45 +08002115 iop_data = (uint32_t __iomem *)prbuffer->data;
2116 iop_len = readl(&prbuffer->data_len);
2117 if (iop_len > 0) {
2118 buf1 = kmalloc(128, GFP_ATOMIC);
2119 buf2 = (uint32_t *)buf1;
2120 if (buf1 == NULL)
2121 return 0;
2122 data_len = iop_len;
2123 while (data_len >= 4) {
2124 *buf2++ = readl(iop_data);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002125 iop_data++;
Ching Huangbb263c42014-08-19 15:17:45 +08002126 data_len -= 4;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002127 }
Ching Huangbb263c42014-08-19 15:17:45 +08002128 if (data_len)
2129 *buf2 = readl(iop_data);
2130 buf2 = (uint32_t *)buf1;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002131 }
Ching Huangbb263c42014-08-19 15:17:45 +08002132 while (iop_len > 0) {
Ching Huang2e9feb42014-09-24 17:33:34 +08002133 pQbuffer = &acb->rqbuffer[acb->rqbuf_putIndex];
Ching Huangbb263c42014-08-19 15:17:45 +08002134 *pQbuffer = *buf1;
Ching Huang2e9feb42014-09-24 17:33:34 +08002135 acb->rqbuf_putIndex++;
Ching Huangbb263c42014-08-19 15:17:45 +08002136 /* if last, index number set it to 0 */
Ching Huang2e9feb42014-09-24 17:33:34 +08002137 acb->rqbuf_putIndex %= ARCMSR_MAX_QBUFFER;
Ching Huangbb263c42014-08-19 15:17:45 +08002138 buf1++;
2139 iop_len--;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002140 }
Ching Huang2e9feb42014-09-24 17:33:34 +08002141 kfree(buf2);
Ching Huangbb263c42014-08-19 15:17:45 +08002142 /* let IOP know data has been read */
2143 arcmsr_iop_message_read(acb);
2144 return 1;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002145}
2146
Ching Huangbb263c42014-08-19 15:17:45 +08002147uint32_t
2148arcmsr_Read_iop_rqbuffer_data(struct AdapterControlBlock *acb,
2149 struct QBUFFER __iomem *prbuffer) {
2150
2151 uint8_t *pQbuffer;
2152 uint8_t __iomem *iop_data;
2153 uint32_t iop_len;
2154
Ching Huang52b4dab2017-12-05 09:28:04 +08002155 if (acb->adapter_type > ACB_ADAPTER_TYPE_B)
Ching Huangbb263c42014-08-19 15:17:45 +08002156 return arcmsr_Read_iop_rqbuffer_in_DWORD(acb, prbuffer);
2157 iop_data = (uint8_t __iomem *)prbuffer->data;
2158 iop_len = readl(&prbuffer->data_len);
2159 while (iop_len > 0) {
Ching Huang2e9feb42014-09-24 17:33:34 +08002160 pQbuffer = &acb->rqbuffer[acb->rqbuf_putIndex];
Ching Huangbb263c42014-08-19 15:17:45 +08002161 *pQbuffer = readb(iop_data);
Ching Huang2e9feb42014-09-24 17:33:34 +08002162 acb->rqbuf_putIndex++;
2163 acb->rqbuf_putIndex %= ARCMSR_MAX_QBUFFER;
Ching Huangbb263c42014-08-19 15:17:45 +08002164 iop_data++;
2165 iop_len--;
2166 }
2167 arcmsr_iop_message_read(acb);
2168 return 1;
2169}
2170
2171static void arcmsr_iop2drv_data_wrote_handle(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08002172{
Ching Huangbb263c42014-08-19 15:17:45 +08002173 unsigned long flags;
2174 struct QBUFFER __iomem *prbuffer;
2175 int32_t buf_empty_len;
2176
2177 spin_lock_irqsave(&acb->rqbuffer_lock, flags);
2178 prbuffer = arcmsr_get_iop_rqbuffer(acb);
Ching Huang2e9feb42014-09-24 17:33:34 +08002179 buf_empty_len = (acb->rqbuf_putIndex - acb->rqbuf_getIndex - 1) &
Ching Huangbb263c42014-08-19 15:17:45 +08002180 (ARCMSR_MAX_QBUFFER - 1);
2181 if (buf_empty_len >= readl(&prbuffer->data_len)) {
2182 if (arcmsr_Read_iop_rqbuffer_data(acb, prbuffer) == 0)
2183 acb->acb_flags |= ACB_F_IOPDATA_OVERFLOW;
2184 } else
2185 acb->acb_flags |= ACB_F_IOPDATA_OVERFLOW;
2186 spin_unlock_irqrestore(&acb->rqbuffer_lock, flags);
2187}
2188
2189static void arcmsr_write_ioctldata2iop_in_DWORD(struct AdapterControlBlock *acb)
2190{
2191 uint8_t *pQbuffer;
2192 struct QBUFFER __iomem *pwbuffer;
2193 uint8_t *buf1 = NULL;
2194 uint32_t __iomem *iop_data;
2195 uint32_t allxfer_len = 0, data_len, *buf2 = NULL, data;
2196
2197 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_READED) {
2198 buf1 = kmalloc(128, GFP_ATOMIC);
2199 buf2 = (uint32_t *)buf1;
2200 if (buf1 == NULL)
2201 return;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002202
2203 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
2204 pwbuffer = arcmsr_get_iop_wqbuffer(acb);
Ching Huangbb263c42014-08-19 15:17:45 +08002205 iop_data = (uint32_t __iomem *)pwbuffer->data;
Ching Huang2e9feb42014-09-24 17:33:34 +08002206 while ((acb->wqbuf_getIndex != acb->wqbuf_putIndex)
Ching Huangbb263c42014-08-19 15:17:45 +08002207 && (allxfer_len < 124)) {
Ching Huang2e9feb42014-09-24 17:33:34 +08002208 pQbuffer = &acb->wqbuffer[acb->wqbuf_getIndex];
Ching Huangbb263c42014-08-19 15:17:45 +08002209 *buf1 = *pQbuffer;
Ching Huang2e9feb42014-09-24 17:33:34 +08002210 acb->wqbuf_getIndex++;
2211 acb->wqbuf_getIndex %= ARCMSR_MAX_QBUFFER;
Ching Huangbb263c42014-08-19 15:17:45 +08002212 buf1++;
2213 allxfer_len++;
2214 }
2215 data_len = allxfer_len;
2216 buf1 = (uint8_t *)buf2;
2217 while (data_len >= 4) {
2218 data = *buf2++;
2219 writel(data, iop_data);
2220 iop_data++;
2221 data_len -= 4;
2222 }
2223 if (data_len) {
2224 data = *buf2;
2225 writel(data, iop_data);
2226 }
2227 writel(allxfer_len, &pwbuffer->data_len);
2228 kfree(buf1);
2229 arcmsr_iop_message_wrote(acb);
2230 }
2231}
2232
2233void
2234arcmsr_write_ioctldata2iop(struct AdapterControlBlock *acb)
2235{
2236 uint8_t *pQbuffer;
2237 struct QBUFFER __iomem *pwbuffer;
2238 uint8_t __iomem *iop_data;
2239 int32_t allxfer_len = 0;
2240
Ching Huang52b4dab2017-12-05 09:28:04 +08002241 if (acb->adapter_type > ACB_ADAPTER_TYPE_B) {
Ching Huangbb263c42014-08-19 15:17:45 +08002242 arcmsr_write_ioctldata2iop_in_DWORD(acb);
2243 return;
2244 }
2245 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_READED) {
2246 acb->acb_flags &= (~ACB_F_MESSAGE_WQBUFFER_READED);
2247 pwbuffer = arcmsr_get_iop_wqbuffer(acb);
2248 iop_data = (uint8_t __iomem *)pwbuffer->data;
Ching Huang2e9feb42014-09-24 17:33:34 +08002249 while ((acb->wqbuf_getIndex != acb->wqbuf_putIndex)
Ching Huangbb263c42014-08-19 15:17:45 +08002250 && (allxfer_len < 124)) {
Ching Huang2e9feb42014-09-24 17:33:34 +08002251 pQbuffer = &acb->wqbuffer[acb->wqbuf_getIndex];
Ching Huangbb263c42014-08-19 15:17:45 +08002252 writeb(*pQbuffer, iop_data);
Ching Huang2e9feb42014-09-24 17:33:34 +08002253 acb->wqbuf_getIndex++;
2254 acb->wqbuf_getIndex %= ARCMSR_MAX_QBUFFER;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002255 iop_data++;
2256 allxfer_len++;
2257 }
Ching Huangbb263c42014-08-19 15:17:45 +08002258 writel(allxfer_len, &pwbuffer->data_len);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002259 arcmsr_iop_message_wrote(acb);
2260 }
Ching Huangbb263c42014-08-19 15:17:45 +08002261}
Nick Cheng1a4f5502007-09-13 17:26:40 +08002262
Ching Huangbb263c42014-08-19 15:17:45 +08002263static void arcmsr_iop2drv_data_read_handle(struct AdapterControlBlock *acb)
2264{
2265 unsigned long flags;
2266
2267 spin_lock_irqsave(&acb->wqbuffer_lock, flags);
2268 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_READED;
Ching Huang2e9feb42014-09-24 17:33:34 +08002269 if (acb->wqbuf_getIndex != acb->wqbuf_putIndex)
Ching Huangbb263c42014-08-19 15:17:45 +08002270 arcmsr_write_ioctldata2iop(acb);
Ching Huang2e9feb42014-09-24 17:33:34 +08002271 if (acb->wqbuf_getIndex == acb->wqbuf_putIndex)
Nick Cheng1a4f5502007-09-13 17:26:40 +08002272 acb->acb_flags |= ACB_F_MESSAGE_WQBUFFER_CLEARED;
Ching Huangbb263c42014-08-19 15:17:45 +08002273 spin_unlock_irqrestore(&acb->wqbuffer_lock, flags);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002274}
2275
Ching Huang626fa322014-08-19 15:10:12 +08002276static void arcmsr_hbaA_doorbell_isr(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08002277{
2278 uint32_t outbound_doorbell;
Al Viro80da1ad2007-10-29 05:08:28 +00002279 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002280 outbound_doorbell = readl(&reg->outbound_doorbell);
Ching Huang6b393722014-08-19 14:18:24 +08002281 do {
2282 writel(outbound_doorbell, &reg->outbound_doorbell);
2283 if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK)
2284 arcmsr_iop2drv_data_wrote_handle(acb);
2285 if (outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_READ_OK)
2286 arcmsr_iop2drv_data_read_handle(acb);
2287 outbound_doorbell = readl(&reg->outbound_doorbell);
2288 } while (outbound_doorbell & (ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK
2289 | ARCMSR_OUTBOUND_IOP331_DATA_READ_OK));
Nick Cheng1a4f5502007-09-13 17:26:40 +08002290}
Ching Huang626fa322014-08-19 15:10:12 +08002291static void arcmsr_hbaC_doorbell_isr(struct AdapterControlBlock *pACB)
Nick Chengcdd3cb12010-07-13 20:03:04 +08002292{
2293 uint32_t outbound_doorbell;
Ching Huangc10b1d52014-08-19 15:20:31 +08002294 struct MessageUnit_C __iomem *reg = pACB->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002295 /*
2296 *******************************************************************
2297 ** Maybe here we need to check wrqbuffer_lock is lock or not
2298 ** DOORBELL: din! don!
2299 ** check if there are any mail need to pack from firmware
2300 *******************************************************************
2301 */
2302 outbound_doorbell = readl(&reg->outbound_doorbell);
Ching Huang6b393722014-08-19 14:18:24 +08002303 do {
2304 writel(outbound_doorbell, &reg->outbound_doorbell_clear);
2305 readl(&reg->outbound_doorbell_clear);
2306 if (outbound_doorbell & ARCMSR_HBCMU_IOP2DRV_DATA_WRITE_OK)
2307 arcmsr_iop2drv_data_wrote_handle(pACB);
2308 if (outbound_doorbell & ARCMSR_HBCMU_IOP2DRV_DATA_READ_OK)
2309 arcmsr_iop2drv_data_read_handle(pACB);
2310 if (outbound_doorbell & ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE)
Ching Huang626fa322014-08-19 15:10:12 +08002311 arcmsr_hbaC_message_isr(pACB);
Ching Huang6b393722014-08-19 14:18:24 +08002312 outbound_doorbell = readl(&reg->outbound_doorbell);
2313 } while (outbound_doorbell & (ARCMSR_HBCMU_IOP2DRV_DATA_WRITE_OK
2314 | ARCMSR_HBCMU_IOP2DRV_DATA_READ_OK
2315 | ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE));
Nick Chengcdd3cb12010-07-13 20:03:04 +08002316}
Ching Huang5b374792014-08-19 15:25:22 +08002317
2318static void arcmsr_hbaD_doorbell_isr(struct AdapterControlBlock *pACB)
2319{
2320 uint32_t outbound_doorbell;
2321 struct MessageUnit_D *pmu = pACB->pmuD;
2322
2323 outbound_doorbell = readl(pmu->outbound_doorbell);
2324 do {
2325 writel(outbound_doorbell, pmu->outbound_doorbell);
2326 if (outbound_doorbell & ARCMSR_ARC1214_IOP2DRV_MESSAGE_CMD_DONE)
2327 arcmsr_hbaD_message_isr(pACB);
2328 if (outbound_doorbell & ARCMSR_ARC1214_IOP2DRV_DATA_WRITE_OK)
2329 arcmsr_iop2drv_data_wrote_handle(pACB);
2330 if (outbound_doorbell & ARCMSR_ARC1214_IOP2DRV_DATA_READ_OK)
2331 arcmsr_iop2drv_data_read_handle(pACB);
2332 outbound_doorbell = readl(pmu->outbound_doorbell);
2333 } while (outbound_doorbell & (ARCMSR_ARC1214_IOP2DRV_DATA_WRITE_OK
2334 | ARCMSR_ARC1214_IOP2DRV_DATA_READ_OK
2335 | ARCMSR_ARC1214_IOP2DRV_MESSAGE_CMD_DONE));
2336}
2337
Ching Huang23509022017-12-05 09:35:34 +08002338static void arcmsr_hbaE_doorbell_isr(struct AdapterControlBlock *pACB)
2339{
2340 uint32_t outbound_doorbell, in_doorbell, tmp;
2341 struct MessageUnit_E __iomem *reg = pACB->pmuE;
2342
2343 in_doorbell = readl(&reg->iobound_doorbell);
2344 outbound_doorbell = in_doorbell ^ pACB->in_doorbell;
2345 do {
2346 writel(0, &reg->host_int_status); /* clear interrupt */
2347 if (outbound_doorbell & ARCMSR_HBEMU_IOP2DRV_DATA_WRITE_OK) {
2348 arcmsr_iop2drv_data_wrote_handle(pACB);
2349 }
2350 if (outbound_doorbell & ARCMSR_HBEMU_IOP2DRV_DATA_READ_OK) {
2351 arcmsr_iop2drv_data_read_handle(pACB);
2352 }
2353 if (outbound_doorbell & ARCMSR_HBEMU_IOP2DRV_MESSAGE_CMD_DONE) {
2354 arcmsr_hbaE_message_isr(pACB);
2355 }
2356 tmp = in_doorbell;
2357 in_doorbell = readl(&reg->iobound_doorbell);
2358 outbound_doorbell = tmp ^ in_doorbell;
2359 } while (outbound_doorbell & (ARCMSR_HBEMU_IOP2DRV_DATA_WRITE_OK
2360 | ARCMSR_HBEMU_IOP2DRV_DATA_READ_OK
2361 | ARCMSR_HBEMU_IOP2DRV_MESSAGE_CMD_DONE));
2362 pACB->in_doorbell = in_doorbell;
2363}
2364
Ching Huang626fa322014-08-19 15:10:12 +08002365static void arcmsr_hbaA_postqueue_isr(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08002366{
2367 uint32_t flag_ccb;
Al Viro80da1ad2007-10-29 05:08:28 +00002368 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002369 struct ARCMSR_CDB *pARCMSR_CDB;
2370 struct CommandControlBlock *pCCB;
2371 bool error;
Ching Huang9e386a52018-12-19 16:43:15 +08002372 unsigned long cdb_phy_addr;
2373
Nick Cheng1a4f5502007-09-13 17:26:40 +08002374 while ((flag_ccb = readl(&reg->outbound_queueport)) != 0xFFFFFFFF) {
Ching Huang9e386a52018-12-19 16:43:15 +08002375 cdb_phy_addr = (flag_ccb << 5) & 0xffffffff;
2376 if (acb->cdb_phyadd_hipart)
2377 cdb_phy_addr = cdb_phy_addr | acb->cdb_phyadd_hipart;
2378 pARCMSR_CDB = (struct ARCMSR_CDB *)(acb->vir2phy_offset + cdb_phy_addr);
Nick Chengcdd3cb12010-07-13 20:03:04 +08002379 pCCB = container_of(pARCMSR_CDB, struct CommandControlBlock, arcmsr_cdb);
2380 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE0) ? true : false;
2381 arcmsr_drain_donequeue(acb, pCCB, error);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002382 }
2383}
Ching Huang626fa322014-08-19 15:10:12 +08002384static void arcmsr_hbaB_postqueue_isr(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08002385{
2386 uint32_t index;
2387 uint32_t flag_ccb;
Al Viro80da1ad2007-10-29 05:08:28 +00002388 struct MessageUnit_B *reg = acb->pmuB;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002389 struct ARCMSR_CDB *pARCMSR_CDB;
2390 struct CommandControlBlock *pCCB;
2391 bool error;
Ching Huange66764f2018-12-19 16:45:46 +08002392 unsigned long cdb_phy_addr;
2393
Nick Cheng1a4f5502007-09-13 17:26:40 +08002394 index = reg->doneq_index;
Ching Huangc10b1d52014-08-19 15:20:31 +08002395 while ((flag_ccb = reg->done_qbuffer[index]) != 0) {
Ching Huange66764f2018-12-19 16:45:46 +08002396 cdb_phy_addr = (flag_ccb << 5) & 0xffffffff;
2397 if (acb->cdb_phyadd_hipart)
2398 cdb_phy_addr = cdb_phy_addr | acb->cdb_phyadd_hipart;
2399 pARCMSR_CDB = (struct ARCMSR_CDB *)(acb->vir2phy_offset + cdb_phy_addr);
Nick Chengcdd3cb12010-07-13 20:03:04 +08002400 pCCB = container_of(pARCMSR_CDB, struct CommandControlBlock, arcmsr_cdb);
2401 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE0) ? true : false;
2402 arcmsr_drain_donequeue(acb, pCCB, error);
Ching Huange66764f2018-12-19 16:45:46 +08002403 reg->done_qbuffer[index] = 0;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002404 index++;
2405 index %= ARCMSR_MAX_HBB_POSTQUEUE;
2406 reg->doneq_index = index;
2407 }
2408}
Nick Chengcdd3cb12010-07-13 20:03:04 +08002409
Ching Huang626fa322014-08-19 15:10:12 +08002410static void arcmsr_hbaC_postqueue_isr(struct AdapterControlBlock *acb)
Nick Chengcdd3cb12010-07-13 20:03:04 +08002411{
Ching Huangc10b1d52014-08-19 15:20:31 +08002412 struct MessageUnit_C __iomem *phbcmu;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002413 struct ARCMSR_CDB *arcmsr_cdb;
2414 struct CommandControlBlock *ccb;
Ching Huangc71ec552018-12-19 16:48:16 +08002415 uint32_t flag_ccb, throttling = 0;
2416 unsigned long ccb_cdb_phy;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002417 int error;
2418
Ching Huangc10b1d52014-08-19 15:20:31 +08002419 phbcmu = acb->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002420 /* areca cdb command done */
2421 /* Use correct offset and size for syncing */
2422
Ching Huang6b393722014-08-19 14:18:24 +08002423 while ((flag_ccb = readl(&phbcmu->outbound_queueport_low)) !=
2424 0xFFFFFFFF) {
2425 ccb_cdb_phy = (flag_ccb & 0xFFFFFFF0);
Ching Huangc71ec552018-12-19 16:48:16 +08002426 if (acb->cdb_phyadd_hipart)
2427 ccb_cdb_phy = ccb_cdb_phy | acb->cdb_phyadd_hipart;
Ching Huang6b393722014-08-19 14:18:24 +08002428 arcmsr_cdb = (struct ARCMSR_CDB *)(acb->vir2phy_offset
2429 + ccb_cdb_phy);
2430 ccb = container_of(arcmsr_cdb, struct CommandControlBlock,
2431 arcmsr_cdb);
2432 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE1)
2433 ? true : false;
2434 /* check if command done with no error */
2435 arcmsr_drain_donequeue(acb, ccb, error);
2436 throttling++;
2437 if (throttling == ARCMSR_HBC_ISR_THROTTLING_LEVEL) {
2438 writel(ARCMSR_HBCMU_DRV2IOP_POSTQUEUE_THROTTLING,
2439 &phbcmu->inbound_doorbell);
2440 throttling = 0;
2441 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08002442 }
2443}
Ching Huang5b374792014-08-19 15:25:22 +08002444
2445static void arcmsr_hbaD_postqueue_isr(struct AdapterControlBlock *acb)
2446{
Ching Huang3b8155d2014-09-15 19:05:33 +08002447 u32 outbound_write_pointer, doneq_index, index_stripped, toggle;
Ching Huanga36ade42018-12-19 16:51:14 +08002448 uint32_t addressLow;
Ching Huang5b374792014-08-19 15:25:22 +08002449 int error;
2450 struct MessageUnit_D *pmu;
2451 struct ARCMSR_CDB *arcmsr_cdb;
2452 struct CommandControlBlock *ccb;
Ching Huanga36ade42018-12-19 16:51:14 +08002453 unsigned long flags, ccb_cdb_phy, cdb_phy_hipart;
Ching Huang5b374792014-08-19 15:25:22 +08002454
2455 spin_lock_irqsave(&acb->doneq_lock, flags);
2456 pmu = acb->pmuD;
2457 outbound_write_pointer = pmu->done_qbuffer[0].addressLow + 1;
2458 doneq_index = pmu->doneq_index;
2459 if ((doneq_index & 0xFFF) != (outbound_write_pointer & 0xFFF)) {
2460 do {
Ching Huang3b8155d2014-09-15 19:05:33 +08002461 toggle = doneq_index & 0x4000;
2462 index_stripped = (doneq_index & 0xFFF) + 1;
2463 index_stripped %= ARCMSR_MAX_ARC1214_DONEQUEUE;
2464 pmu->doneq_index = index_stripped ? (index_stripped | toggle) :
2465 ((toggle ^ 0x4000) + 1);
Ching Huang5b374792014-08-19 15:25:22 +08002466 doneq_index = pmu->doneq_index;
Ching Huanga36ade42018-12-19 16:51:14 +08002467 cdb_phy_hipart = pmu->done_qbuffer[doneq_index &
2468 0xFFF].addressHigh;
Ching Huang5b374792014-08-19 15:25:22 +08002469 addressLow = pmu->done_qbuffer[doneq_index &
2470 0xFFF].addressLow;
2471 ccb_cdb_phy = (addressLow & 0xFFFFFFF0);
Ching Huanga36ade42018-12-19 16:51:14 +08002472 if (acb->cdb_phyadd_hipart)
2473 ccb_cdb_phy = ccb_cdb_phy | acb->cdb_phyadd_hipart;
Ching Huang5b374792014-08-19 15:25:22 +08002474 arcmsr_cdb = (struct ARCMSR_CDB *)(acb->vir2phy_offset
2475 + ccb_cdb_phy);
2476 ccb = container_of(arcmsr_cdb,
2477 struct CommandControlBlock, arcmsr_cdb);
2478 error = (addressLow & ARCMSR_CCBREPLY_FLAG_ERROR_MODE1)
2479 ? true : false;
2480 arcmsr_drain_donequeue(acb, ccb, error);
2481 writel(doneq_index, pmu->outboundlist_read_pointer);
2482 } while ((doneq_index & 0xFFF) !=
2483 (outbound_write_pointer & 0xFFF));
2484 }
2485 writel(ARCMSR_ARC1214_OUTBOUND_LIST_INTERRUPT_CLEAR,
2486 pmu->outboundlist_interrupt_cause);
2487 readl(pmu->outboundlist_interrupt_cause);
2488 spin_unlock_irqrestore(&acb->doneq_lock, flags);
2489}
2490
Ching Huang23509022017-12-05 09:35:34 +08002491static void arcmsr_hbaE_postqueue_isr(struct AdapterControlBlock *acb)
2492{
2493 uint32_t doneq_index;
2494 uint16_t cmdSMID;
2495 int error;
2496 struct MessageUnit_E __iomem *pmu;
2497 struct CommandControlBlock *ccb;
2498 unsigned long flags;
2499
2500 spin_lock_irqsave(&acb->doneq_lock, flags);
2501 doneq_index = acb->doneq_index;
2502 pmu = acb->pmuE;
2503 while ((readl(&pmu->reply_post_producer_index) & 0xFFFF) != doneq_index) {
2504 cmdSMID = acb->pCompletionQ[doneq_index].cmdSMID;
2505 ccb = acb->pccb_pool[cmdSMID];
2506 error = (acb->pCompletionQ[doneq_index].cmdFlag
2507 & ARCMSR_CCBREPLY_FLAG_ERROR_MODE1) ? true : false;
2508 arcmsr_drain_donequeue(acb, ccb, error);
2509 doneq_index++;
2510 if (doneq_index >= acb->completionQ_entry)
2511 doneq_index = 0;
2512 }
2513 acb->doneq_index = doneq_index;
2514 writel(doneq_index, &pmu->reply_post_consumer_index);
2515 spin_unlock_irqrestore(&acb->doneq_lock, flags);
2516}
2517
Nick Cheng36b83de2010-05-17 11:22:42 +08002518/*
2519**********************************************************************************
2520** Handle a message interrupt
2521**
Nick Chengcdd3cb12010-07-13 20:03:04 +08002522** The only message interrupt we expect is in response to a query for the current adapter config.
Nick Cheng36b83de2010-05-17 11:22:42 +08002523** We want this in order to compare the drivemap so that we can detect newly-attached drives.
2524**********************************************************************************
2525*/
Ching Huang626fa322014-08-19 15:10:12 +08002526static void arcmsr_hbaA_message_isr(struct AdapterControlBlock *acb)
Nick Cheng36b83de2010-05-17 11:22:42 +08002527{
Ching Huangc10b1d52014-08-19 15:20:31 +08002528 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng36b83de2010-05-17 11:22:42 +08002529 /*clear interrupt and message state*/
2530 writel(ARCMSR_MU_OUTBOUND_MESSAGE0_INT, &reg->outbound_intstatus);
Ching Huang5dd8b3e2017-12-05 09:57:23 +08002531 if (acb->acb_flags & ACB_F_MSG_GET_CONFIG)
2532 schedule_work(&acb->arcmsr_do_message_isr_bh);
Nick Cheng36b83de2010-05-17 11:22:42 +08002533}
Ching Huang626fa322014-08-19 15:10:12 +08002534static void arcmsr_hbaB_message_isr(struct AdapterControlBlock *acb)
Nick Cheng36b83de2010-05-17 11:22:42 +08002535{
2536 struct MessageUnit_B *reg = acb->pmuB;
2537
2538 /*clear interrupt and message state*/
Nick Chengae52e7f2010-06-18 15:39:12 +08002539 writel(ARCMSR_MESSAGE_INT_CLEAR_PATTERN, reg->iop2drv_doorbell);
Ching Huang5dd8b3e2017-12-05 09:57:23 +08002540 if (acb->acb_flags & ACB_F_MSG_GET_CONFIG)
2541 schedule_work(&acb->arcmsr_do_message_isr_bh);
Nick Cheng36b83de2010-05-17 11:22:42 +08002542}
Nick Chengcdd3cb12010-07-13 20:03:04 +08002543/*
2544**********************************************************************************
2545** Handle a message interrupt
2546**
2547** The only message interrupt we expect is in response to a query for the
2548** current adapter config.
2549** We want this in order to compare the drivemap so that we can detect newly-attached drives.
2550**********************************************************************************
2551*/
Ching Huang626fa322014-08-19 15:10:12 +08002552static void arcmsr_hbaC_message_isr(struct AdapterControlBlock *acb)
Nick Chengcdd3cb12010-07-13 20:03:04 +08002553{
Ching Huangc10b1d52014-08-19 15:20:31 +08002554 struct MessageUnit_C __iomem *reg = acb->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002555 /*clear interrupt and message state*/
2556 writel(ARCMSR_HBCMU_IOP2DRV_MESSAGE_CMD_DONE_DOORBELL_CLEAR, &reg->outbound_doorbell_clear);
Ching Huang5dd8b3e2017-12-05 09:57:23 +08002557 if (acb->acb_flags & ACB_F_MSG_GET_CONFIG)
2558 schedule_work(&acb->arcmsr_do_message_isr_bh);
Nick Chengcdd3cb12010-07-13 20:03:04 +08002559}
2560
Ching Huang5b374792014-08-19 15:25:22 +08002561static void arcmsr_hbaD_message_isr(struct AdapterControlBlock *acb)
2562{
2563 struct MessageUnit_D *reg = acb->pmuD;
2564
2565 writel(ARCMSR_ARC1214_IOP2DRV_MESSAGE_CMD_DONE, reg->outbound_doorbell);
2566 readl(reg->outbound_doorbell);
Ching Huang5dd8b3e2017-12-05 09:57:23 +08002567 if (acb->acb_flags & ACB_F_MSG_GET_CONFIG)
2568 schedule_work(&acb->arcmsr_do_message_isr_bh);
Ching Huang5b374792014-08-19 15:25:22 +08002569}
2570
Ching Huang23509022017-12-05 09:35:34 +08002571static void arcmsr_hbaE_message_isr(struct AdapterControlBlock *acb)
2572{
2573 struct MessageUnit_E __iomem *reg = acb->pmuE;
2574
2575 writel(0, &reg->host_int_status);
Ching Huang5dd8b3e2017-12-05 09:57:23 +08002576 if (acb->acb_flags & ACB_F_MSG_GET_CONFIG)
2577 schedule_work(&acb->arcmsr_do_message_isr_bh);
Ching Huang23509022017-12-05 09:35:34 +08002578}
2579
Ching Huang626fa322014-08-19 15:10:12 +08002580static int arcmsr_hbaA_handle_isr(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08002581{
2582 uint32_t outbound_intstatus;
Al Viro80da1ad2007-10-29 05:08:28 +00002583 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng36b83de2010-05-17 11:22:42 +08002584 outbound_intstatus = readl(&reg->outbound_intstatus) &
Nick Chengcdd3cb12010-07-13 20:03:04 +08002585 acb->outbound_int_enable;
Ching Huang6b393722014-08-19 14:18:24 +08002586 if (!(outbound_intstatus & ARCMSR_MU_OUTBOUND_HANDLE_INT))
2587 return IRQ_NONE;
2588 do {
2589 writel(outbound_intstatus, &reg->outbound_intstatus);
2590 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_DOORBELL_INT)
Ching Huang626fa322014-08-19 15:10:12 +08002591 arcmsr_hbaA_doorbell_isr(acb);
Ching Huang6b393722014-08-19 14:18:24 +08002592 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_POSTQUEUE_INT)
Ching Huang626fa322014-08-19 15:10:12 +08002593 arcmsr_hbaA_postqueue_isr(acb);
Ching Huang6b393722014-08-19 14:18:24 +08002594 if (outbound_intstatus & ARCMSR_MU_OUTBOUND_MESSAGE0_INT)
Ching Huang626fa322014-08-19 15:10:12 +08002595 arcmsr_hbaA_message_isr(acb);
Ching Huang6b393722014-08-19 14:18:24 +08002596 outbound_intstatus = readl(&reg->outbound_intstatus) &
2597 acb->outbound_int_enable;
2598 } while (outbound_intstatus & (ARCMSR_MU_OUTBOUND_DOORBELL_INT
2599 | ARCMSR_MU_OUTBOUND_POSTQUEUE_INT
2600 | ARCMSR_MU_OUTBOUND_MESSAGE0_INT));
2601 return IRQ_HANDLED;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002602}
2603
Ching Huang626fa322014-08-19 15:10:12 +08002604static int arcmsr_hbaB_handle_isr(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08002605{
2606 uint32_t outbound_doorbell;
Al Viro80da1ad2007-10-29 05:08:28 +00002607 struct MessageUnit_B *reg = acb->pmuB;
Nick Chengae52e7f2010-06-18 15:39:12 +08002608 outbound_doorbell = readl(reg->iop2drv_doorbell) &
Nick Chengcdd3cb12010-07-13 20:03:04 +08002609 acb->outbound_int_enable;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002610 if (!outbound_doorbell)
Ching Huang6b393722014-08-19 14:18:24 +08002611 return IRQ_NONE;
2612 do {
2613 writel(~outbound_doorbell, reg->iop2drv_doorbell);
2614 writel(ARCMSR_DRV2IOP_END_OF_INTERRUPT, reg->drv2iop_doorbell);
2615 if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_WRITE_OK)
2616 arcmsr_iop2drv_data_wrote_handle(acb);
2617 if (outbound_doorbell & ARCMSR_IOP2DRV_DATA_READ_OK)
2618 arcmsr_iop2drv_data_read_handle(acb);
2619 if (outbound_doorbell & ARCMSR_IOP2DRV_CDB_DONE)
Ching Huang626fa322014-08-19 15:10:12 +08002620 arcmsr_hbaB_postqueue_isr(acb);
Ching Huang6b393722014-08-19 14:18:24 +08002621 if (outbound_doorbell & ARCMSR_IOP2DRV_MESSAGE_CMD_DONE)
Ching Huang626fa322014-08-19 15:10:12 +08002622 arcmsr_hbaB_message_isr(acb);
Ching Huang6b393722014-08-19 14:18:24 +08002623 outbound_doorbell = readl(reg->iop2drv_doorbell) &
2624 acb->outbound_int_enable;
2625 } while (outbound_doorbell & (ARCMSR_IOP2DRV_DATA_WRITE_OK
2626 | ARCMSR_IOP2DRV_DATA_READ_OK
2627 | ARCMSR_IOP2DRV_CDB_DONE
2628 | ARCMSR_IOP2DRV_MESSAGE_CMD_DONE));
2629 return IRQ_HANDLED;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002630}
2631
Ching Huang626fa322014-08-19 15:10:12 +08002632static int arcmsr_hbaC_handle_isr(struct AdapterControlBlock *pACB)
Nick Chengcdd3cb12010-07-13 20:03:04 +08002633{
2634 uint32_t host_interrupt_status;
Ching Huangc10b1d52014-08-19 15:20:31 +08002635 struct MessageUnit_C __iomem *phbcmu = pACB->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002636 /*
2637 *********************************************
2638 ** check outbound intstatus
2639 *********************************************
2640 */
Ching Huang6b393722014-08-19 14:18:24 +08002641 host_interrupt_status = readl(&phbcmu->host_int_status) &
2642 (ARCMSR_HBCMU_OUTBOUND_POSTQUEUE_ISR |
2643 ARCMSR_HBCMU_OUTBOUND_DOORBELL_ISR);
2644 if (!host_interrupt_status)
2645 return IRQ_NONE;
2646 do {
2647 if (host_interrupt_status & ARCMSR_HBCMU_OUTBOUND_DOORBELL_ISR)
Ching Huang626fa322014-08-19 15:10:12 +08002648 arcmsr_hbaC_doorbell_isr(pACB);
Ching Huang6b393722014-08-19 14:18:24 +08002649 /* MU post queue interrupts*/
2650 if (host_interrupt_status & ARCMSR_HBCMU_OUTBOUND_POSTQUEUE_ISR)
Ching Huang626fa322014-08-19 15:10:12 +08002651 arcmsr_hbaC_postqueue_isr(pACB);
Ching Huang6b393722014-08-19 14:18:24 +08002652 host_interrupt_status = readl(&phbcmu->host_int_status);
2653 } while (host_interrupt_status & (ARCMSR_HBCMU_OUTBOUND_POSTQUEUE_ISR |
2654 ARCMSR_HBCMU_OUTBOUND_DOORBELL_ISR));
2655 return IRQ_HANDLED;
Nick Chengcdd3cb12010-07-13 20:03:04 +08002656}
Ching Huang5b374792014-08-19 15:25:22 +08002657
2658static irqreturn_t arcmsr_hbaD_handle_isr(struct AdapterControlBlock *pACB)
2659{
2660 u32 host_interrupt_status;
2661 struct MessageUnit_D *pmu = pACB->pmuD;
2662
2663 host_interrupt_status = readl(pmu->host_int_status) &
2664 (ARCMSR_ARC1214_OUTBOUND_POSTQUEUE_ISR |
2665 ARCMSR_ARC1214_OUTBOUND_DOORBELL_ISR);
2666 if (!host_interrupt_status)
2667 return IRQ_NONE;
2668 do {
2669 /* MU post queue interrupts*/
2670 if (host_interrupt_status &
2671 ARCMSR_ARC1214_OUTBOUND_POSTQUEUE_ISR)
2672 arcmsr_hbaD_postqueue_isr(pACB);
2673 if (host_interrupt_status &
2674 ARCMSR_ARC1214_OUTBOUND_DOORBELL_ISR)
2675 arcmsr_hbaD_doorbell_isr(pACB);
2676 host_interrupt_status = readl(pmu->host_int_status);
2677 } while (host_interrupt_status &
2678 (ARCMSR_ARC1214_OUTBOUND_POSTQUEUE_ISR |
2679 ARCMSR_ARC1214_OUTBOUND_DOORBELL_ISR));
2680 return IRQ_HANDLED;
2681}
2682
Ching Huang23509022017-12-05 09:35:34 +08002683static irqreturn_t arcmsr_hbaE_handle_isr(struct AdapterControlBlock *pACB)
2684{
2685 uint32_t host_interrupt_status;
2686 struct MessageUnit_E __iomem *pmu = pACB->pmuE;
2687
2688 host_interrupt_status = readl(&pmu->host_int_status) &
2689 (ARCMSR_HBEMU_OUTBOUND_POSTQUEUE_ISR |
2690 ARCMSR_HBEMU_OUTBOUND_DOORBELL_ISR);
2691 if (!host_interrupt_status)
2692 return IRQ_NONE;
2693 do {
2694 /* MU ioctl transfer doorbell interrupts*/
2695 if (host_interrupt_status & ARCMSR_HBEMU_OUTBOUND_DOORBELL_ISR) {
2696 arcmsr_hbaE_doorbell_isr(pACB);
2697 }
2698 /* MU post queue interrupts*/
2699 if (host_interrupt_status & ARCMSR_HBEMU_OUTBOUND_POSTQUEUE_ISR) {
2700 arcmsr_hbaE_postqueue_isr(pACB);
2701 }
2702 host_interrupt_status = readl(&pmu->host_int_status);
2703 } while (host_interrupt_status & (ARCMSR_HBEMU_OUTBOUND_POSTQUEUE_ISR |
2704 ARCMSR_HBEMU_OUTBOUND_DOORBELL_ISR));
2705 return IRQ_HANDLED;
2706}
2707
Nick Cheng1a4f5502007-09-13 17:26:40 +08002708static irqreturn_t arcmsr_interrupt(struct AdapterControlBlock *acb)
2709{
2710 switch (acb->adapter_type) {
Ching Huang6b393722014-08-19 14:18:24 +08002711 case ACB_ADAPTER_TYPE_A:
Ching Huang626fa322014-08-19 15:10:12 +08002712 return arcmsr_hbaA_handle_isr(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002713 break;
Ching Huang6b393722014-08-19 14:18:24 +08002714 case ACB_ADAPTER_TYPE_B:
Ching Huang626fa322014-08-19 15:10:12 +08002715 return arcmsr_hbaB_handle_isr(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002716 break;
Ching Huang6b393722014-08-19 14:18:24 +08002717 case ACB_ADAPTER_TYPE_C:
Ching Huang626fa322014-08-19 15:10:12 +08002718 return arcmsr_hbaC_handle_isr(acb);
Ching Huang5b374792014-08-19 15:25:22 +08002719 case ACB_ADAPTER_TYPE_D:
2720 return arcmsr_hbaD_handle_isr(acb);
Ching Huang23509022017-12-05 09:35:34 +08002721 case ACB_ADAPTER_TYPE_E:
2722 return arcmsr_hbaE_handle_isr(acb);
Ching Huang6b393722014-08-19 14:18:24 +08002723 default:
2724 return IRQ_NONE;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002725 }
Erich Chen1c57e862006-07-12 08:59:32 -07002726}
2727
2728static void arcmsr_iop_parking(struct AdapterControlBlock *acb)
2729{
2730 if (acb) {
2731 /* stop adapter background rebuild */
2732 if (acb->acb_flags & ACB_F_MSG_START_BGRB) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08002733 uint32_t intmask_org;
Erich Chen1c57e862006-07-12 08:59:32 -07002734 acb->acb_flags &= ~ACB_F_MSG_START_BGRB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002735 intmask_org = arcmsr_disable_outbound_ints(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07002736 arcmsr_stop_adapter_bgrb(acb);
2737 arcmsr_flush_adapter_cache(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002738 arcmsr_enable_outbound_ints(acb, intmask_org);
Erich Chen1c57e862006-07-12 08:59:32 -07002739 }
2740 }
2741}
2742
Ching Huangbb263c42014-08-19 15:17:45 +08002743
2744void arcmsr_clear_iop2drv_rqueue_buffer(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07002745{
Ching Huangbb263c42014-08-19 15:17:45 +08002746 uint32_t i;
2747
2748 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
2749 for (i = 0; i < 15; i++) {
2750 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
2751 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
Ching Huang2e9feb42014-09-24 17:33:34 +08002752 acb->rqbuf_getIndex = 0;
2753 acb->rqbuf_putIndex = 0;
Ching Huangbb263c42014-08-19 15:17:45 +08002754 arcmsr_iop_message_read(acb);
2755 mdelay(30);
Ching Huang2e9feb42014-09-24 17:33:34 +08002756 } else if (acb->rqbuf_getIndex !=
2757 acb->rqbuf_putIndex) {
2758 acb->rqbuf_getIndex = 0;
2759 acb->rqbuf_putIndex = 0;
Ching Huangbb263c42014-08-19 15:17:45 +08002760 mdelay(30);
2761 } else
2762 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002763 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08002764 }
2765}
2766
Nick Cheng36b83de2010-05-17 11:22:42 +08002767static int arcmsr_iop_message_xfer(struct AdapterControlBlock *acb,
Ching Huangbb263c42014-08-19 15:17:45 +08002768 struct scsi_cmnd *cmd)
Nick Cheng1a4f5502007-09-13 17:26:40 +08002769{
Erich Chen1c57e862006-07-12 08:59:32 -07002770 char *buffer;
Ching Huangbb263c42014-08-19 15:17:45 +08002771 unsigned short use_sg;
2772 int retvalue = 0, transfer_len = 0;
2773 unsigned long flags;
2774 struct CMD_MESSAGE_FIELD *pcmdmessagefld;
2775 uint32_t controlcode = (uint32_t)cmd->cmnd[5] << 24 |
2776 (uint32_t)cmd->cmnd[6] << 16 |
2777 (uint32_t)cmd->cmnd[7] << 8 |
2778 (uint32_t)cmd->cmnd[8];
FUJITA Tomonorideff2622007-05-14 19:25:56 +09002779 struct scatterlist *sg;
Ching Huangbb263c42014-08-19 15:17:45 +08002780
2781 use_sg = scsi_sg_count(cmd);
FUJITA Tomonorideff2622007-05-14 19:25:56 +09002782 sg = scsi_sglist(cmd);
Cong Wang77dfce02011-11-25 23:14:23 +08002783 buffer = kmap_atomic(sg_page(sg)) + sg->offset;
Ching Huangbb263c42014-08-19 15:17:45 +08002784 if (use_sg > 1) {
FUJITA Tomonorideff2622007-05-14 19:25:56 +09002785 retvalue = ARCMSR_MESSAGE_FAIL;
2786 goto message_out;
Erich Chen1c57e862006-07-12 08:59:32 -07002787 }
FUJITA Tomonorideff2622007-05-14 19:25:56 +09002788 transfer_len += sg->length;
Erich Chen1c57e862006-07-12 08:59:32 -07002789 if (transfer_len > sizeof(struct CMD_MESSAGE_FIELD)) {
2790 retvalue = ARCMSR_MESSAGE_FAIL;
Ching Huangbb263c42014-08-19 15:17:45 +08002791 pr_info("%s: ARCMSR_MESSAGE_FAIL!\n", __func__);
Erich Chen1c57e862006-07-12 08:59:32 -07002792 goto message_out;
2793 }
Ching Huangbb263c42014-08-19 15:17:45 +08002794 pcmdmessagefld = (struct CMD_MESSAGE_FIELD *)buffer;
2795 switch (controlcode) {
Erich Chen1c57e862006-07-12 08:59:32 -07002796 case ARCMSR_MESSAGE_READ_RQBUFFER: {
Daniel Drake69e562c2008-02-20 13:29:05 +00002797 unsigned char *ver_addr;
Ching Huang2e9feb42014-09-24 17:33:34 +08002798 uint8_t *ptmpQbuffer;
Ching Huangbb263c42014-08-19 15:17:45 +08002799 uint32_t allxfer_len = 0;
Ching Huang2e9feb42014-09-24 17:33:34 +08002800 ver_addr = kmalloc(ARCMSR_API_DATA_BUFLEN, GFP_ATOMIC);
Daniel Drake69e562c2008-02-20 13:29:05 +00002801 if (!ver_addr) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08002802 retvalue = ARCMSR_MESSAGE_FAIL;
Ching Huangbb263c42014-08-19 15:17:45 +08002803 pr_info("%s: memory not enough!\n", __func__);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002804 goto message_out;
2805 }
Daniel Drake69e562c2008-02-20 13:29:05 +00002806 ptmpQbuffer = ver_addr;
Ching Huangbb263c42014-08-19 15:17:45 +08002807 spin_lock_irqsave(&acb->rqbuffer_lock, flags);
Ching Huang2e9feb42014-09-24 17:33:34 +08002808 if (acb->rqbuf_getIndex != acb->rqbuf_putIndex) {
2809 unsigned int tail = acb->rqbuf_getIndex;
2810 unsigned int head = acb->rqbuf_putIndex;
2811 unsigned int cnt_to_end = CIRC_CNT_TO_END(head, tail, ARCMSR_MAX_QBUFFER);
2812
2813 allxfer_len = CIRC_CNT(head, tail, ARCMSR_MAX_QBUFFER);
2814 if (allxfer_len > ARCMSR_API_DATA_BUFLEN)
2815 allxfer_len = ARCMSR_API_DATA_BUFLEN;
2816
2817 if (allxfer_len <= cnt_to_end)
2818 memcpy(ptmpQbuffer, acb->rqbuffer + tail, allxfer_len);
2819 else {
2820 memcpy(ptmpQbuffer, acb->rqbuffer + tail, cnt_to_end);
2821 memcpy(ptmpQbuffer + cnt_to_end, acb->rqbuffer, allxfer_len - cnt_to_end);
Ching Huangbb263c42014-08-19 15:17:45 +08002822 }
Ching Huang2e9feb42014-09-24 17:33:34 +08002823 acb->rqbuf_getIndex = (acb->rqbuf_getIndex + allxfer_len) % ARCMSR_MAX_QBUFFER;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002824 }
Ching Huangbb263c42014-08-19 15:17:45 +08002825 memcpy(pcmdmessagefld->messagedatabuffer, ver_addr,
2826 allxfer_len);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002827 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
Al Viro80da1ad2007-10-29 05:08:28 +00002828 struct QBUFFER __iomem *prbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002829 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
2830 prbuffer = arcmsr_get_iop_rqbuffer(acb);
Ching Huangbb263c42014-08-19 15:17:45 +08002831 if (arcmsr_Read_iop_rqbuffer_data(acb, prbuffer) == 0)
2832 acb->acb_flags |= ACB_F_IOPDATA_OVERFLOW;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002833 }
Ching Huangbb263c42014-08-19 15:17:45 +08002834 spin_unlock_irqrestore(&acb->rqbuffer_lock, flags);
Daniel Drake69e562c2008-02-20 13:29:05 +00002835 kfree(ver_addr);
Ching Huangbb263c42014-08-19 15:17:45 +08002836 pcmdmessagefld->cmdmessage.Length = allxfer_len;
2837 if (acb->fw_flag == FW_DEADLOCK)
2838 pcmdmessagefld->cmdmessage.ReturnCode =
2839 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
2840 else
2841 pcmdmessagefld->cmdmessage.ReturnCode =
2842 ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07002843 break;
Ching Huangbb263c42014-08-19 15:17:45 +08002844 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08002845 case ARCMSR_MESSAGE_WRITE_WQBUFFER: {
Daniel Drake69e562c2008-02-20 13:29:05 +00002846 unsigned char *ver_addr;
Dan Carpenter7bc2b552016-09-15 16:44:56 +03002847 uint32_t user_len;
2848 int32_t cnt2end;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002849 uint8_t *pQbuffer, *ptmpuserbuffer;
Borislav Petkov4bd173c2016-09-23 13:22:26 +02002850
2851 user_len = pcmdmessagefld->cmdmessage.Length;
2852 if (user_len > ARCMSR_API_DATA_BUFLEN) {
2853 retvalue = ARCMSR_MESSAGE_FAIL;
2854 goto message_out;
2855 }
2856
Ching Huang2e9feb42014-09-24 17:33:34 +08002857 ver_addr = kmalloc(ARCMSR_API_DATA_BUFLEN, GFP_ATOMIC);
Daniel Drake69e562c2008-02-20 13:29:05 +00002858 if (!ver_addr) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08002859 retvalue = ARCMSR_MESSAGE_FAIL;
2860 goto message_out;
2861 }
Daniel Drake69e562c2008-02-20 13:29:05 +00002862 ptmpuserbuffer = ver_addr;
Borislav Petkov4bd173c2016-09-23 13:22:26 +02002863
Ching Huangbb263c42014-08-19 15:17:45 +08002864 memcpy(ptmpuserbuffer,
2865 pcmdmessagefld->messagedatabuffer, user_len);
2866 spin_lock_irqsave(&acb->wqbuffer_lock, flags);
Ching Huang2e9feb42014-09-24 17:33:34 +08002867 if (acb->wqbuf_putIndex != acb->wqbuf_getIndex) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08002868 struct SENSE_DATA *sensebuffer =
2869 (struct SENSE_DATA *)cmd->sense_buffer;
Ching Huangbb263c42014-08-19 15:17:45 +08002870 arcmsr_write_ioctldata2iop(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002871 /* has error report sensedata */
Ching Huangbb263c42014-08-19 15:17:45 +08002872 sensebuffer->ErrorCode = SCSI_SENSE_CURRENT_ERRORS;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002873 sensebuffer->SenseKey = ILLEGAL_REQUEST;
2874 sensebuffer->AdditionalSenseLength = 0x0A;
2875 sensebuffer->AdditionalSenseCode = 0x20;
2876 sensebuffer->Valid = 1;
2877 retvalue = ARCMSR_MESSAGE_FAIL;
2878 } else {
Ching Huang2e9feb42014-09-24 17:33:34 +08002879 pQbuffer = &acb->wqbuffer[acb->wqbuf_putIndex];
2880 cnt2end = ARCMSR_MAX_QBUFFER - acb->wqbuf_putIndex;
2881 if (user_len > cnt2end) {
2882 memcpy(pQbuffer, ptmpuserbuffer, cnt2end);
2883 ptmpuserbuffer += cnt2end;
2884 user_len -= cnt2end;
2885 acb->wqbuf_putIndex = 0;
2886 pQbuffer = acb->wqbuffer;
2887 }
2888 memcpy(pQbuffer, ptmpuserbuffer, user_len);
2889 acb->wqbuf_putIndex += user_len;
2890 acb->wqbuf_putIndex %= ARCMSR_MAX_QBUFFER;
2891 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {
2892 acb->acb_flags &=
Nick Cheng1a4f5502007-09-13 17:26:40 +08002893 ~ACB_F_MESSAGE_WQBUFFER_CLEARED;
Ching Huang2e9feb42014-09-24 17:33:34 +08002894 arcmsr_write_ioctldata2iop(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002895 }
Erich Chen1c57e862006-07-12 08:59:32 -07002896 }
Ching Huangbb263c42014-08-19 15:17:45 +08002897 spin_unlock_irqrestore(&acb->wqbuffer_lock, flags);
2898 kfree(ver_addr);
2899 if (acb->fw_flag == FW_DEADLOCK)
2900 pcmdmessagefld->cmdmessage.ReturnCode =
2901 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
2902 else
2903 pcmdmessagefld->cmdmessage.ReturnCode =
2904 ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07002905 break;
Ching Huangbb263c42014-08-19 15:17:45 +08002906 }
Erich Chen1c57e862006-07-12 08:59:32 -07002907 case ARCMSR_MESSAGE_CLEAR_RQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08002908 uint8_t *pQbuffer = acb->rqbuffer;
Ching Huangbb263c42014-08-19 15:17:45 +08002909
2910 arcmsr_clear_iop2drv_rqueue_buffer(acb);
2911 spin_lock_irqsave(&acb->rqbuffer_lock, flags);
Nick Cheng1a4f5502007-09-13 17:26:40 +08002912 acb->acb_flags |= ACB_F_MESSAGE_RQBUFFER_CLEARED;
Ching Huang2e9feb42014-09-24 17:33:34 +08002913 acb->rqbuf_getIndex = 0;
2914 acb->rqbuf_putIndex = 0;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002915 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
Ching Huangbb263c42014-08-19 15:17:45 +08002916 spin_unlock_irqrestore(&acb->rqbuffer_lock, flags);
2917 if (acb->fw_flag == FW_DEADLOCK)
Nick Chengae52e7f2010-06-18 15:39:12 +08002918 pcmdmessagefld->cmdmessage.ReturnCode =
Ching Huangbb263c42014-08-19 15:17:45 +08002919 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
2920 else
Nick Chengae52e7f2010-06-18 15:39:12 +08002921 pcmdmessagefld->cmdmessage.ReturnCode =
Ching Huangbb263c42014-08-19 15:17:45 +08002922 ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07002923 break;
Ching Huangbb263c42014-08-19 15:17:45 +08002924 }
Erich Chen1c57e862006-07-12 08:59:32 -07002925 case ARCMSR_MESSAGE_CLEAR_WQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08002926 uint8_t *pQbuffer = acb->wqbuffer;
Ching Huangbb263c42014-08-19 15:17:45 +08002927 spin_lock_irqsave(&acb->wqbuffer_lock, flags);
2928 acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
2929 ACB_F_MESSAGE_WQBUFFER_READED);
Ching Huang2e9feb42014-09-24 17:33:34 +08002930 acb->wqbuf_getIndex = 0;
2931 acb->wqbuf_putIndex = 0;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002932 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
Ching Huangbb263c42014-08-19 15:17:45 +08002933 spin_unlock_irqrestore(&acb->wqbuffer_lock, flags);
2934 if (acb->fw_flag == FW_DEADLOCK)
2935 pcmdmessagefld->cmdmessage.ReturnCode =
2936 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
2937 else
2938 pcmdmessagefld->cmdmessage.ReturnCode =
2939 ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07002940 break;
Ching Huangbb263c42014-08-19 15:17:45 +08002941 }
Erich Chen1c57e862006-07-12 08:59:32 -07002942 case ARCMSR_MESSAGE_CLEAR_ALLQBUFFER: {
Nick Cheng1a4f5502007-09-13 17:26:40 +08002943 uint8_t *pQbuffer;
Ching Huangbb263c42014-08-19 15:17:45 +08002944 arcmsr_clear_iop2drv_rqueue_buffer(acb);
2945 spin_lock_irqsave(&acb->rqbuffer_lock, flags);
2946 acb->acb_flags |= ACB_F_MESSAGE_RQBUFFER_CLEARED;
Ching Huang2e9feb42014-09-24 17:33:34 +08002947 acb->rqbuf_getIndex = 0;
2948 acb->rqbuf_putIndex = 0;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002949 pQbuffer = acb->rqbuffer;
2950 memset(pQbuffer, 0, sizeof(struct QBUFFER));
Ching Huangbb263c42014-08-19 15:17:45 +08002951 spin_unlock_irqrestore(&acb->rqbuffer_lock, flags);
2952 spin_lock_irqsave(&acb->wqbuffer_lock, flags);
2953 acb->acb_flags |= (ACB_F_MESSAGE_WQBUFFER_CLEARED |
2954 ACB_F_MESSAGE_WQBUFFER_READED);
Ching Huang2e9feb42014-09-24 17:33:34 +08002955 acb->wqbuf_getIndex = 0;
2956 acb->wqbuf_putIndex = 0;
Nick Cheng1a4f5502007-09-13 17:26:40 +08002957 pQbuffer = acb->wqbuffer;
2958 memset(pQbuffer, 0, sizeof(struct QBUFFER));
Ching Huangbb263c42014-08-19 15:17:45 +08002959 spin_unlock_irqrestore(&acb->wqbuffer_lock, flags);
2960 if (acb->fw_flag == FW_DEADLOCK)
Nick Chengae52e7f2010-06-18 15:39:12 +08002961 pcmdmessagefld->cmdmessage.ReturnCode =
Ching Huangbb263c42014-08-19 15:17:45 +08002962 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
2963 else
Nick Chengae52e7f2010-06-18 15:39:12 +08002964 pcmdmessagefld->cmdmessage.ReturnCode =
Ching Huangbb263c42014-08-19 15:17:45 +08002965 ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07002966 break;
Ching Huangbb263c42014-08-19 15:17:45 +08002967 }
Erich Chen1c57e862006-07-12 08:59:32 -07002968 case ARCMSR_MESSAGE_RETURN_CODE_3F: {
Ching Huangbb263c42014-08-19 15:17:45 +08002969 if (acb->fw_flag == FW_DEADLOCK)
Nick Cheng36b83de2010-05-17 11:22:42 +08002970 pcmdmessagefld->cmdmessage.ReturnCode =
Ching Huangbb263c42014-08-19 15:17:45 +08002971 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
2972 else
Nick Chengae52e7f2010-06-18 15:39:12 +08002973 pcmdmessagefld->cmdmessage.ReturnCode =
Ching Huangbb263c42014-08-19 15:17:45 +08002974 ARCMSR_MESSAGE_RETURNCODE_3F;
Erich Chen1c57e862006-07-12 08:59:32 -07002975 break;
Ching Huangbb263c42014-08-19 15:17:45 +08002976 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08002977 case ARCMSR_MESSAGE_SAY_HELLO: {
2978 int8_t *hello_string = "Hello! I am ARCMSR";
Ching Huangbb263c42014-08-19 15:17:45 +08002979 if (acb->fw_flag == FW_DEADLOCK)
Nick Cheng36b83de2010-05-17 11:22:42 +08002980 pcmdmessagefld->cmdmessage.ReturnCode =
Ching Huangbb263c42014-08-19 15:17:45 +08002981 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
2982 else
Nick Chengae52e7f2010-06-18 15:39:12 +08002983 pcmdmessagefld->cmdmessage.ReturnCode =
Ching Huangbb263c42014-08-19 15:17:45 +08002984 ARCMSR_MESSAGE_RETURNCODE_OK;
2985 memcpy(pcmdmessagefld->messagedatabuffer,
2986 hello_string, (int16_t)strlen(hello_string));
Erich Chen1c57e862006-07-12 08:59:32 -07002987 break;
Ching Huangbb263c42014-08-19 15:17:45 +08002988 }
2989 case ARCMSR_MESSAGE_SAY_GOODBYE: {
2990 if (acb->fw_flag == FW_DEADLOCK)
Nick Cheng36b83de2010-05-17 11:22:42 +08002991 pcmdmessagefld->cmdmessage.ReturnCode =
Ching Huangbb263c42014-08-19 15:17:45 +08002992 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
2993 else
2994 pcmdmessagefld->cmdmessage.ReturnCode =
2995 ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07002996 arcmsr_iop_parking(acb);
2997 break;
Ching Huangbb263c42014-08-19 15:17:45 +08002998 }
2999 case ARCMSR_MESSAGE_FLUSH_ADAPTER_CACHE: {
3000 if (acb->fw_flag == FW_DEADLOCK)
Nick Cheng36b83de2010-05-17 11:22:42 +08003001 pcmdmessagefld->cmdmessage.ReturnCode =
Ching Huangbb263c42014-08-19 15:17:45 +08003002 ARCMSR_MESSAGE_RETURNCODE_BUS_HANG_ON;
3003 else
3004 pcmdmessagefld->cmdmessage.ReturnCode =
3005 ARCMSR_MESSAGE_RETURNCODE_OK;
Erich Chen1c57e862006-07-12 08:59:32 -07003006 arcmsr_flush_adapter_cache(acb);
3007 break;
Ching Huangbb263c42014-08-19 15:17:45 +08003008 }
Erich Chen1c57e862006-07-12 08:59:32 -07003009 default:
3010 retvalue = ARCMSR_MESSAGE_FAIL;
Ching Huangbb263c42014-08-19 15:17:45 +08003011 pr_info("%s: unknown controlcode!\n", __func__);
Erich Chen1c57e862006-07-12 08:59:32 -07003012 }
Ching Huangbb263c42014-08-19 15:17:45 +08003013message_out:
3014 if (use_sg) {
3015 struct scatterlist *sg = scsi_sglist(cmd);
3016 kunmap_atomic(buffer - sg->offset);
3017 }
Erich Chen1c57e862006-07-12 08:59:32 -07003018 return retvalue;
3019}
3020
3021static struct CommandControlBlock *arcmsr_get_freeccb(struct AdapterControlBlock *acb)
3022{
3023 struct list_head *head = &acb->ccb_free_list;
3024 struct CommandControlBlock *ccb = NULL;
Nick Chengae52e7f2010-06-18 15:39:12 +08003025 unsigned long flags;
3026 spin_lock_irqsave(&acb->ccblist_lock, flags);
Erich Chen1c57e862006-07-12 08:59:32 -07003027 if (!list_empty(head)) {
3028 ccb = list_entry(head->next, struct CommandControlBlock, list);
Nick Chengae52e7f2010-06-18 15:39:12 +08003029 list_del_init(&ccb->list);
Nick Chengcdd3cb12010-07-13 20:03:04 +08003030 }else{
Nick Chengae52e7f2010-06-18 15:39:12 +08003031 spin_unlock_irqrestore(&acb->ccblist_lock, flags);
Ching Huangc10b1d52014-08-19 15:20:31 +08003032 return NULL;
Erich Chen1c57e862006-07-12 08:59:32 -07003033 }
Nick Chengae52e7f2010-06-18 15:39:12 +08003034 spin_unlock_irqrestore(&acb->ccblist_lock, flags);
Erich Chen1c57e862006-07-12 08:59:32 -07003035 return ccb;
3036}
3037
3038static void arcmsr_handle_virtual_command(struct AdapterControlBlock *acb,
3039 struct scsi_cmnd *cmd)
3040{
3041 switch (cmd->cmnd[0]) {
3042 case INQUIRY: {
3043 unsigned char inqdata[36];
3044 char *buffer;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09003045 struct scatterlist *sg;
Erich Chen1c57e862006-07-12 08:59:32 -07003046
3047 if (cmd->device->lun) {
3048 cmd->result = (DID_TIME_OUT << 16);
3049 cmd->scsi_done(cmd);
3050 return;
3051 }
3052 inqdata[0] = TYPE_PROCESSOR;
3053 /* Periph Qualifier & Periph Dev Type */
3054 inqdata[1] = 0;
3055 /* rem media bit & Dev Type Modifier */
3056 inqdata[2] = 0;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08003057 /* ISO, ECMA, & ANSI versions */
Erich Chen1c57e862006-07-12 08:59:32 -07003058 inqdata[4] = 31;
3059 /* length of additional data */
3060 strncpy(&inqdata[8], "Areca ", 8);
3061 /* Vendor Identification */
3062 strncpy(&inqdata[16], "RAID controller ", 16);
3063 /* Product Identification */
3064 strncpy(&inqdata[32], "R001", 4); /* Product Revision */
Erich Chen1c57e862006-07-12 08:59:32 -07003065
FUJITA Tomonorideff2622007-05-14 19:25:56 +09003066 sg = scsi_sglist(cmd);
Cong Wang77dfce02011-11-25 23:14:23 +08003067 buffer = kmap_atomic(sg_page(sg)) + sg->offset;
FUJITA Tomonorideff2622007-05-14 19:25:56 +09003068
Erich Chen1c57e862006-07-12 08:59:32 -07003069 memcpy(buffer, inqdata, sizeof(inqdata));
FUJITA Tomonorideff2622007-05-14 19:25:56 +09003070 sg = scsi_sglist(cmd);
Cong Wang77dfce02011-11-25 23:14:23 +08003071 kunmap_atomic(buffer - sg->offset);
Erich Chen1c57e862006-07-12 08:59:32 -07003072
Erich Chen1c57e862006-07-12 08:59:32 -07003073 cmd->scsi_done(cmd);
3074 }
3075 break;
3076 case WRITE_BUFFER:
3077 case READ_BUFFER: {
3078 if (arcmsr_iop_message_xfer(acb, cmd))
3079 cmd->result = (DID_ERROR << 16);
3080 cmd->scsi_done(cmd);
3081 }
3082 break;
3083 default:
3084 cmd->scsi_done(cmd);
3085 }
3086}
3087
Jeff Garzikf2812332010-11-16 02:10:29 -05003088static int arcmsr_queue_command_lck(struct scsi_cmnd *cmd,
Erich Chen1c57e862006-07-12 08:59:32 -07003089 void (* done)(struct scsi_cmnd *))
3090{
3091 struct Scsi_Host *host = cmd->device->host;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003092 struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -07003093 struct CommandControlBlock *ccb;
3094 int target = cmd->device->id;
Ching Huangc4c1adb2018-03-15 14:33:36 +08003095
3096 if (acb->acb_flags & ACB_F_ADAPTER_REMOVED) {
3097 cmd->result = (DID_NO_CONNECT << 16);
3098 cmd->scsi_done(cmd);
3099 return 0;
3100 }
Erich Chen1c57e862006-07-12 08:59:32 -07003101 cmd->scsi_done = done;
3102 cmd->host_scribble = NULL;
3103 cmd->result = 0;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08003104 if (target == 16) {
Erich Chen1c57e862006-07-12 08:59:32 -07003105 /* virtual device for iop message transfer */
3106 arcmsr_handle_virtual_command(acb, cmd);
3107 return 0;
3108 }
Erich Chen1c57e862006-07-12 08:59:32 -07003109 ccb = arcmsr_get_freeccb(acb);
3110 if (!ccb)
3111 return SCSI_MLQUEUE_HOST_BUSY;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003112 if (arcmsr_build_ccb( acb, ccb, cmd ) == FAILED) {
Nick Cheng76d78302008-02-04 23:53:24 -08003113 cmd->result = (DID_ERROR << 16) | (RESERVATION_CONFLICT << 1);
3114 cmd->scsi_done(cmd);
3115 return 0;
3116 }
Erich Chen1c57e862006-07-12 08:59:32 -07003117 arcmsr_post_ccb(acb, ccb);
3118 return 0;
3119}
3120
Jeff Garzikf2812332010-11-16 02:10:29 -05003121static DEF_SCSI_QCMD(arcmsr_queue_command)
3122
Ching Huang1e9c8102017-12-12 18:53:33 +08003123static void arcmsr_get_adapter_config(struct AdapterControlBlock *pACB, uint32_t *rwbuffer)
3124{
3125 int count;
3126 uint32_t *acb_firm_model = (uint32_t *)pACB->firm_model;
3127 uint32_t *acb_firm_version = (uint32_t *)pACB->firm_version;
3128 uint32_t *acb_device_map = (uint32_t *)pACB->device_map;
3129 uint32_t *firm_model = &rwbuffer[15];
3130 uint32_t *firm_version = &rwbuffer[17];
3131 uint32_t *device_map = &rwbuffer[21];
3132
3133 count = 2;
3134 while (count) {
3135 *acb_firm_model = readl(firm_model);
3136 acb_firm_model++;
3137 firm_model++;
3138 count--;
3139 }
3140 count = 4;
3141 while (count) {
3142 *acb_firm_version = readl(firm_version);
3143 acb_firm_version++;
3144 firm_version++;
3145 count--;
3146 }
3147 count = 4;
3148 while (count) {
3149 *acb_device_map = readl(device_map);
3150 acb_device_map++;
3151 device_map++;
3152 count--;
3153 }
3154 pACB->signature = readl(&rwbuffer[0]);
3155 pACB->firm_request_len = readl(&rwbuffer[1]);
3156 pACB->firm_numbers_queue = readl(&rwbuffer[2]);
3157 pACB->firm_sdram_size = readl(&rwbuffer[3]);
3158 pACB->firm_hd_channels = readl(&rwbuffer[4]);
3159 pACB->firm_cfg_version = readl(&rwbuffer[25]);
3160 pr_notice("Areca RAID Controller%d: Model %s, F/W %s\n",
3161 pACB->host->host_no,
3162 pACB->firm_model,
3163 pACB->firm_version);
3164}
3165
Ching Huang626fa322014-08-19 15:10:12 +08003166static bool arcmsr_hbaA_get_config(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07003167{
Al Viro80da1ad2007-10-29 05:08:28 +00003168 struct MessageUnit_A __iomem *reg = acb->pmuA;
Ching Huang1e9c8102017-12-12 18:53:33 +08003169
Ching Huangb6b30842017-12-12 18:48:24 +08003170 arcmsr_wait_firmware_ready(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07003171 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
Ching Huang626fa322014-08-19 15:10:12 +08003172 if (!arcmsr_hbaA_wait_msgint_ready(acb)) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08003173 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
3174 miscellaneous data' timeout \n", acb->host->host_no);
Nick Chengae52e7f2010-06-18 15:39:12 +08003175 return false;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003176 }
Ching Huang1e9c8102017-12-12 18:53:33 +08003177 arcmsr_get_adapter_config(acb, reg->message_rwbuffer);
Nick Chengae52e7f2010-06-18 15:39:12 +08003178 return true;
Erich Chen1c57e862006-07-12 08:59:32 -07003179}
Ching Huang626fa322014-08-19 15:10:12 +08003180static bool arcmsr_hbaB_get_config(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08003181{
Ching Huang02040672015-11-26 19:41:15 +08003182 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003183
Ching Huang7e315ff2015-11-25 19:49:33 +08003184 arcmsr_wait_firmware_ready(acb);
3185 writel(ARCMSR_MESSAGE_START_DRIVER_MODE, reg->drv2iop_doorbell);
3186 if (!arcmsr_hbaB_wait_msgint_ready(acb)) {
3187 printk(KERN_ERR "arcmsr%d: can't set driver mode.\n", acb->host->host_no);
Ching Huang02040672015-11-26 19:41:15 +08003188 return false;
Ching Huang7e315ff2015-11-25 19:49:33 +08003189 }
Nick Chengae52e7f2010-06-18 15:39:12 +08003190 writel(ARCMSR_MESSAGE_GET_CONFIG, reg->drv2iop_doorbell);
Ching Huang626fa322014-08-19 15:10:12 +08003191 if (!arcmsr_hbaB_wait_msgint_ready(acb)) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08003192 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
3193 miscellaneous data' timeout \n", acb->host->host_no);
Ching Huang02040672015-11-26 19:41:15 +08003194 return false;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003195 }
Ching Huang1e9c8102017-12-12 18:53:33 +08003196 arcmsr_get_adapter_config(acb, reg->message_rwbuffer);
Nick Chengcdd3cb12010-07-13 20:03:04 +08003197 return true;
3198}
3199
Ching Huang626fa322014-08-19 15:10:12 +08003200static bool arcmsr_hbaC_get_config(struct AdapterControlBlock *pACB)
Nick Chengcdd3cb12010-07-13 20:03:04 +08003201{
Ching Huangdf9f0ee2017-12-12 18:42:29 +08003202 uint32_t intmask_org;
Ching Huangc10b1d52014-08-19 15:20:31 +08003203 struct MessageUnit_C __iomem *reg = pACB->pmuC;
Ching Huang1e9c8102017-12-12 18:53:33 +08003204
Nick Chengcdd3cb12010-07-13 20:03:04 +08003205 /* disable all outbound interrupt */
3206 intmask_org = readl(&reg->host_int_mask); /* disable outbound message0 int */
3207 writel(intmask_org|ARCMSR_HBCMU_ALL_INTMASKENABLE, &reg->host_int_mask);
3208 /* wait firmware ready */
Ching Huangdf9f0ee2017-12-12 18:42:29 +08003209 arcmsr_wait_firmware_ready(pACB);
Nick Chengcdd3cb12010-07-13 20:03:04 +08003210 /* post "get config" instruction */
3211 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
3212 writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &reg->inbound_doorbell);
3213 /* wait message ready */
Ching Huangdf9f0ee2017-12-12 18:42:29 +08003214 if (!arcmsr_hbaC_wait_msgint_ready(pACB)) {
Nick Chengcdd3cb12010-07-13 20:03:04 +08003215 printk(KERN_NOTICE "arcmsr%d: wait 'get adapter firmware \
3216 miscellaneous data' timeout \n", pACB->host->host_no);
3217 return false;
3218 }
Ching Huang1e9c8102017-12-12 18:53:33 +08003219 arcmsr_get_adapter_config(pACB, reg->msgcode_rwbuffer);
Nick Chengae52e7f2010-06-18 15:39:12 +08003220 return true;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003221}
Ching Huang5b374792014-08-19 15:25:22 +08003222
3223static bool arcmsr_hbaD_get_config(struct AdapterControlBlock *acb)
3224{
Ching Huang02040672015-11-26 19:41:15 +08003225 struct MessageUnit_D *reg = acb->pmuD;
Ching Huang5b374792014-08-19 15:25:22 +08003226
Ching Huang5b374792014-08-19 15:25:22 +08003227 if (readl(acb->pmuD->outbound_doorbell) &
3228 ARCMSR_ARC1214_IOP2DRV_MESSAGE_CMD_DONE) {
3229 writel(ARCMSR_ARC1214_IOP2DRV_MESSAGE_CMD_DONE,
3230 acb->pmuD->outbound_doorbell);/*clear interrupt*/
3231 }
Ching Huangb6b30842017-12-12 18:48:24 +08003232 arcmsr_wait_firmware_ready(acb);
Ching Huang5b374792014-08-19 15:25:22 +08003233 /* post "get config" instruction */
3234 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, reg->inbound_msgaddr0);
3235 /* wait message ready */
3236 if (!arcmsr_hbaD_wait_msgint_ready(acb)) {
3237 pr_notice("arcmsr%d: wait get adapter firmware "
3238 "miscellaneous data timeout\n", acb->host->host_no);
Ching Huang5b374792014-08-19 15:25:22 +08003239 return false;
3240 }
Ching Huang1e9c8102017-12-12 18:53:33 +08003241 arcmsr_get_adapter_config(acb, reg->msgcode_rwbuffer);
Ching Huang5b374792014-08-19 15:25:22 +08003242 return true;
3243}
3244
Ching Huang23509022017-12-05 09:35:34 +08003245static bool arcmsr_hbaE_get_config(struct AdapterControlBlock *pACB)
3246{
Ching Huang23509022017-12-05 09:35:34 +08003247 struct MessageUnit_E __iomem *reg = pACB->pmuE;
Ching Huang22c4ae52017-12-12 18:50:51 +08003248 uint32_t intmask_org;
Ching Huang23509022017-12-05 09:35:34 +08003249
3250 /* disable all outbound interrupt */
3251 intmask_org = readl(&reg->host_int_mask); /* disable outbound message0 int */
3252 writel(intmask_org | ARCMSR_HBEMU_ALL_INTMASKENABLE, &reg->host_int_mask);
3253 /* wait firmware ready */
Ching Huang22c4ae52017-12-12 18:50:51 +08003254 arcmsr_wait_firmware_ready(pACB);
Ching Huang23509022017-12-05 09:35:34 +08003255 mdelay(20);
3256 /* post "get config" instruction */
3257 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
3258
3259 pACB->out_doorbell ^= ARCMSR_HBEMU_DRV2IOP_MESSAGE_CMD_DONE;
3260 writel(pACB->out_doorbell, &reg->iobound_doorbell);
3261 /* wait message ready */
Ching Huang22c4ae52017-12-12 18:50:51 +08003262 if (!arcmsr_hbaE_wait_msgint_ready(pACB)) {
Ching Huang23509022017-12-05 09:35:34 +08003263 pr_notice("arcmsr%d: wait get adapter firmware "
3264 "miscellaneous data timeout\n", pACB->host->host_no);
3265 return false;
3266 }
Ching Huang1e9c8102017-12-12 18:53:33 +08003267 arcmsr_get_adapter_config(pACB, reg->msgcode_rwbuffer);
Ching Huang23509022017-12-05 09:35:34 +08003268 return true;
3269}
3270
Nick Chengae52e7f2010-06-18 15:39:12 +08003271static bool arcmsr_get_firmware_spec(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08003272{
Ching Huang3df824a2014-08-19 14:29:41 +08003273 bool rtn = false;
3274
3275 switch (acb->adapter_type) {
3276 case ACB_ADAPTER_TYPE_A:
Ching Huang626fa322014-08-19 15:10:12 +08003277 rtn = arcmsr_hbaA_get_config(acb);
Ching Huang3df824a2014-08-19 14:29:41 +08003278 break;
3279 case ACB_ADAPTER_TYPE_B:
Ching Huang626fa322014-08-19 15:10:12 +08003280 rtn = arcmsr_hbaB_get_config(acb);
Ching Huang3df824a2014-08-19 14:29:41 +08003281 break;
3282 case ACB_ADAPTER_TYPE_C:
Ching Huang626fa322014-08-19 15:10:12 +08003283 rtn = arcmsr_hbaC_get_config(acb);
Ching Huang3df824a2014-08-19 14:29:41 +08003284 break;
Ching Huang5b374792014-08-19 15:25:22 +08003285 case ACB_ADAPTER_TYPE_D:
3286 rtn = arcmsr_hbaD_get_config(acb);
3287 break;
Ching Huang23509022017-12-05 09:35:34 +08003288 case ACB_ADAPTER_TYPE_E:
3289 rtn = arcmsr_hbaE_get_config(acb);
3290 break;
Ching Huang3df824a2014-08-19 14:29:41 +08003291 default:
3292 break;
3293 }
Ching Huangdd6206e2017-12-05 09:47:44 +08003294 acb->maxOutstanding = acb->firm_numbers_queue - 1;
3295 if (acb->host->can_queue >= acb->firm_numbers_queue)
3296 acb->host->can_queue = acb->maxOutstanding;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003297 else
Ching Huangdd6206e2017-12-05 09:47:44 +08003298 acb->maxOutstanding = acb->host->can_queue;
Ching Huangd076e4a2017-12-05 09:44:23 +08003299 acb->maxFreeCCB = acb->host->can_queue;
3300 if (acb->maxFreeCCB < ARCMSR_MAX_FREECCB_NUM)
3301 acb->maxFreeCCB += 64;
Ching Huang3df824a2014-08-19 14:29:41 +08003302 return rtn;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003303}
3304
Ching Huang626fa322014-08-19 15:10:12 +08003305static int arcmsr_hbaA_polling_ccbdone(struct AdapterControlBlock *acb,
Erich Chen1c57e862006-07-12 08:59:32 -07003306 struct CommandControlBlock *poll_ccb)
3307{
Al Viro80da1ad2007-10-29 05:08:28 +00003308 struct MessageUnit_A __iomem *reg = acb->pmuA;
Erich Chen1c57e862006-07-12 08:59:32 -07003309 struct CommandControlBlock *ccb;
Nick Chengae52e7f2010-06-18 15:39:12 +08003310 struct ARCMSR_CDB *arcmsr_cdb;
Erich Chen1c57e862006-07-12 08:59:32 -07003311 uint32_t flag_ccb, outbound_intstatus, poll_ccb_done = 0, poll_count = 0;
Nick Chengae52e7f2010-06-18 15:39:12 +08003312 int rtn;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003313 bool error;
Ching Huang9e386a52018-12-19 16:43:15 +08003314 unsigned long ccb_cdb_phy;
3315
3316polling_hba_ccb_retry:
Erich Chen1c57e862006-07-12 08:59:32 -07003317 poll_count++;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003318 outbound_intstatus = readl(&reg->outbound_intstatus) & acb->outbound_int_enable;
Erich Chen1c57e862006-07-12 08:59:32 -07003319 writel(outbound_intstatus, &reg->outbound_intstatus);/*clear interrupt*/
3320 while (1) {
3321 if ((flag_ccb = readl(&reg->outbound_queueport)) == 0xFFFFFFFF) {
Nick Chengcdd3cb12010-07-13 20:03:04 +08003322 if (poll_ccb_done){
Nick Chengae52e7f2010-06-18 15:39:12 +08003323 rtn = SUCCESS;
Erich Chen1c57e862006-07-12 08:59:32 -07003324 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003325 }else {
3326 msleep(25);
3327 if (poll_count > 100){
Nick Chengae52e7f2010-06-18 15:39:12 +08003328 rtn = FAILED;
Erich Chen1c57e862006-07-12 08:59:32 -07003329 break;
Nick Chengae52e7f2010-06-18 15:39:12 +08003330 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08003331 goto polling_hba_ccb_retry;
Erich Chen1c57e862006-07-12 08:59:32 -07003332 }
3333 }
Ching Huang9e386a52018-12-19 16:43:15 +08003334 ccb_cdb_phy = (flag_ccb << 5) & 0xffffffff;
3335 if (acb->cdb_phyadd_hipart)
3336 ccb_cdb_phy = ccb_cdb_phy | acb->cdb_phyadd_hipart;
3337 arcmsr_cdb = (struct ARCMSR_CDB *)(acb->vir2phy_offset + ccb_cdb_phy);
Nick Chengae52e7f2010-06-18 15:39:12 +08003338 ccb = container_of(arcmsr_cdb, struct CommandControlBlock, arcmsr_cdb);
Ching Huangcab5aec2014-08-19 14:47:16 +08003339 poll_ccb_done |= (ccb == poll_ccb) ? 1 : 0;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003340 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
3341 if ((ccb->startdone == ARCMSR_CCB_ABORTED) || (ccb == poll_ccb)) {
3342 printk(KERN_NOTICE "arcmsr%d: scsi id = %d lun = %d ccb = '0x%p'"
Erich Chen1c57e862006-07-12 08:59:32 -07003343 " poll command abort successfully \n"
3344 , acb->host->host_no
3345 , ccb->pcmd->device->id
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003346 , (u32)ccb->pcmd->device->lun
Erich Chen1c57e862006-07-12 08:59:32 -07003347 , ccb);
3348 ccb->pcmd->result = DID_ABORT << 16;
Nick Chengae52e7f2010-06-18 15:39:12 +08003349 arcmsr_ccb_complete(ccb);
Erich Chen1c57e862006-07-12 08:59:32 -07003350 continue;
3351 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08003352 printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
3353 " command done ccb = '0x%p'"
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08003354 "ccboutstandingcount = %d \n"
Erich Chen1c57e862006-07-12 08:59:32 -07003355 , acb->host->host_no
3356 , ccb
3357 , atomic_read(&acb->ccboutstandingcount));
3358 continue;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003359 }
3360 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE0) ? true : false;
3361 arcmsr_report_ccb_state(acb, ccb, error);
Nick Cheng1a4f5502007-09-13 17:26:40 +08003362 }
Nick Chengae52e7f2010-06-18 15:39:12 +08003363 return rtn;
3364}
Nick Cheng1a4f5502007-09-13 17:26:40 +08003365
Ching Huang626fa322014-08-19 15:10:12 +08003366static int arcmsr_hbaB_polling_ccbdone(struct AdapterControlBlock *acb,
Nick Cheng1a4f5502007-09-13 17:26:40 +08003367 struct CommandControlBlock *poll_ccb)
3368{
Nick Chengcdd3cb12010-07-13 20:03:04 +08003369 struct MessageUnit_B *reg = acb->pmuB;
Nick Chengae52e7f2010-06-18 15:39:12 +08003370 struct ARCMSR_CDB *arcmsr_cdb;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003371 struct CommandControlBlock *ccb;
3372 uint32_t flag_ccb, poll_ccb_done = 0, poll_count = 0;
Nick Chengae52e7f2010-06-18 15:39:12 +08003373 int index, rtn;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003374 bool error;
Ching Huange66764f2018-12-19 16:45:46 +08003375 unsigned long ccb_cdb_phy;
NickCheng97b99122011-01-06 17:32:41 +08003376
Ching Huange66764f2018-12-19 16:45:46 +08003377polling_hbb_ccb_retry:
Nick Chengcdd3cb12010-07-13 20:03:04 +08003378 poll_count++;
3379 /* clear doorbell interrupt */
Nick Chengae52e7f2010-06-18 15:39:12 +08003380 writel(ARCMSR_DOORBELL_INT_CLEAR_PATTERN, reg->iop2drv_doorbell);
Nick Chengcdd3cb12010-07-13 20:03:04 +08003381 while(1){
3382 index = reg->doneq_index;
Ching Huangc10b1d52014-08-19 15:20:31 +08003383 flag_ccb = reg->done_qbuffer[index];
3384 if (flag_ccb == 0) {
Nick Chengcdd3cb12010-07-13 20:03:04 +08003385 if (poll_ccb_done){
Nick Chengae52e7f2010-06-18 15:39:12 +08003386 rtn = SUCCESS;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003387 break;
3388 }else {
3389 msleep(25);
3390 if (poll_count > 100){
Nick Chengae52e7f2010-06-18 15:39:12 +08003391 rtn = FAILED;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003392 break;
Nick Chengae52e7f2010-06-18 15:39:12 +08003393 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08003394 goto polling_hbb_ccb_retry;
Erich Chen1c57e862006-07-12 08:59:32 -07003395 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08003396 }
Ching Huangc10b1d52014-08-19 15:20:31 +08003397 reg->done_qbuffer[index] = 0;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003398 index++;
3399 /*if last index number set it to 0 */
3400 index %= ARCMSR_MAX_HBB_POSTQUEUE;
3401 reg->doneq_index = index;
3402 /* check if command done with no error*/
Ching Huange66764f2018-12-19 16:45:46 +08003403 ccb_cdb_phy = (flag_ccb << 5) & 0xffffffff;
3404 if (acb->cdb_phyadd_hipart)
3405 ccb_cdb_phy = ccb_cdb_phy | acb->cdb_phyadd_hipart;
3406 arcmsr_cdb = (struct ARCMSR_CDB *)(acb->vir2phy_offset + ccb_cdb_phy);
Nick Chengae52e7f2010-06-18 15:39:12 +08003407 ccb = container_of(arcmsr_cdb, struct CommandControlBlock, arcmsr_cdb);
Ching Huangcab5aec2014-08-19 14:47:16 +08003408 poll_ccb_done |= (ccb == poll_ccb) ? 1 : 0;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003409 if ((ccb->acb != acb) || (ccb->startdone != ARCMSR_CCB_START)) {
3410 if ((ccb->startdone == ARCMSR_CCB_ABORTED) || (ccb == poll_ccb)) {
Nick Chengae52e7f2010-06-18 15:39:12 +08003411 printk(KERN_NOTICE "arcmsr%d: scsi id = %d lun = %d ccb = '0x%p'"
3412 " poll command abort successfully \n"
Nick Chengcdd3cb12010-07-13 20:03:04 +08003413 ,acb->host->host_no
3414 ,ccb->pcmd->device->id
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003415 ,(u32)ccb->pcmd->device->lun
Nick Chengcdd3cb12010-07-13 20:03:04 +08003416 ,ccb);
3417 ccb->pcmd->result = DID_ABORT << 16;
Nick Chengae52e7f2010-06-18 15:39:12 +08003418 arcmsr_ccb_complete(ccb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08003419 continue;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003420 }
3421 printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
3422 " command done ccb = '0x%p'"
3423 "ccboutstandingcount = %d \n"
3424 , acb->host->host_no
3425 , ccb
3426 , atomic_read(&acb->ccboutstandingcount));
3427 continue;
3428 }
3429 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE0) ? true : false;
3430 arcmsr_report_ccb_state(acb, ccb, error);
3431 }
Nick Chengae52e7f2010-06-18 15:39:12 +08003432 return rtn;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003433}
3434
Ching Huang626fa322014-08-19 15:10:12 +08003435static int arcmsr_hbaC_polling_ccbdone(struct AdapterControlBlock *acb,
3436 struct CommandControlBlock *poll_ccb)
Nick Chengcdd3cb12010-07-13 20:03:04 +08003437{
Ching Huangc10b1d52014-08-19 15:20:31 +08003438 struct MessageUnit_C __iomem *reg = acb->pmuC;
Ching Huangc71ec552018-12-19 16:48:16 +08003439 uint32_t flag_ccb;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003440 struct ARCMSR_CDB *arcmsr_cdb;
3441 bool error;
3442 struct CommandControlBlock *pCCB;
3443 uint32_t poll_ccb_done = 0, poll_count = 0;
3444 int rtn;
Ching Huangc71ec552018-12-19 16:48:16 +08003445 unsigned long ccb_cdb_phy;
3446
Nick Chengcdd3cb12010-07-13 20:03:04 +08003447polling_hbc_ccb_retry:
3448 poll_count++;
3449 while (1) {
3450 if ((readl(&reg->host_int_status) & ARCMSR_HBCMU_OUTBOUND_POSTQUEUE_ISR) == 0) {
3451 if (poll_ccb_done) {
3452 rtn = SUCCESS;
3453 break;
3454 } else {
3455 msleep(25);
3456 if (poll_count > 100) {
3457 rtn = FAILED;
3458 break;
3459 }
3460 goto polling_hbc_ccb_retry;
3461 }
3462 }
3463 flag_ccb = readl(&reg->outbound_queueport_low);
3464 ccb_cdb_phy = (flag_ccb & 0xFFFFFFF0);
Ching Huangc71ec552018-12-19 16:48:16 +08003465 if (acb->cdb_phyadd_hipart)
3466 ccb_cdb_phy = ccb_cdb_phy | acb->cdb_phyadd_hipart;
3467 arcmsr_cdb = (struct ARCMSR_CDB *)(acb->vir2phy_offset + ccb_cdb_phy);
Nick Chengcdd3cb12010-07-13 20:03:04 +08003468 pCCB = container_of(arcmsr_cdb, struct CommandControlBlock, arcmsr_cdb);
Ching Huangcab5aec2014-08-19 14:47:16 +08003469 poll_ccb_done |= (pCCB == poll_ccb) ? 1 : 0;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003470 /* check ifcommand done with no error*/
3471 if ((pCCB->acb != acb) || (pCCB->startdone != ARCMSR_CCB_START)) {
3472 if (pCCB->startdone == ARCMSR_CCB_ABORTED) {
3473 printk(KERN_NOTICE "arcmsr%d: scsi id = %d lun = %d ccb = '0x%p'"
3474 " poll command abort successfully \n"
3475 , acb->host->host_no
3476 , pCCB->pcmd->device->id
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003477 , (u32)pCCB->pcmd->device->lun
Nick Chengcdd3cb12010-07-13 20:03:04 +08003478 , pCCB);
Colin Ian King9b44ffa2019-11-14 18:00:07 +00003479 pCCB->pcmd->result = DID_ABORT << 16;
3480 arcmsr_ccb_complete(pCCB);
Nick Chengcdd3cb12010-07-13 20:03:04 +08003481 continue;
3482 }
3483 printk(KERN_NOTICE "arcmsr%d: polling get an illegal ccb"
3484 " command done ccb = '0x%p'"
3485 "ccboutstandingcount = %d \n"
3486 , acb->host->host_no
3487 , pCCB
3488 , atomic_read(&acb->ccboutstandingcount));
3489 continue;
3490 }
3491 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE1) ? true : false;
3492 arcmsr_report_ccb_state(acb, pCCB, error);
3493 }
3494 return rtn;
3495}
Ching Huang5b374792014-08-19 15:25:22 +08003496
3497static int arcmsr_hbaD_polling_ccbdone(struct AdapterControlBlock *acb,
3498 struct CommandControlBlock *poll_ccb)
3499{
3500 bool error;
Ching Huanga36ade42018-12-19 16:51:14 +08003501 uint32_t poll_ccb_done = 0, poll_count = 0, flag_ccb;
Ching Huang3b8155d2014-09-15 19:05:33 +08003502 int rtn, doneq_index, index_stripped, outbound_write_pointer, toggle;
Ching Huanga36ade42018-12-19 16:51:14 +08003503 unsigned long flags, ccb_cdb_phy, cdb_phy_hipart;
Ching Huang5b374792014-08-19 15:25:22 +08003504 struct ARCMSR_CDB *arcmsr_cdb;
3505 struct CommandControlBlock *pCCB;
3506 struct MessageUnit_D *pmu = acb->pmuD;
3507
3508polling_hbaD_ccb_retry:
3509 poll_count++;
3510 while (1) {
Ching Huang3b8155d2014-09-15 19:05:33 +08003511 spin_lock_irqsave(&acb->doneq_lock, flags);
Ching Huang5b374792014-08-19 15:25:22 +08003512 outbound_write_pointer = pmu->done_qbuffer[0].addressLow + 1;
3513 doneq_index = pmu->doneq_index;
3514 if ((outbound_write_pointer & 0xFFF) == (doneq_index & 0xFFF)) {
Ching Huang3b8155d2014-09-15 19:05:33 +08003515 spin_unlock_irqrestore(&acb->doneq_lock, flags);
Ching Huang5b374792014-08-19 15:25:22 +08003516 if (poll_ccb_done) {
3517 rtn = SUCCESS;
3518 break;
3519 } else {
3520 msleep(25);
3521 if (poll_count > 40) {
3522 rtn = FAILED;
3523 break;
3524 }
3525 goto polling_hbaD_ccb_retry;
3526 }
3527 }
Ching Huang3b8155d2014-09-15 19:05:33 +08003528 toggle = doneq_index & 0x4000;
3529 index_stripped = (doneq_index & 0xFFF) + 1;
3530 index_stripped %= ARCMSR_MAX_ARC1214_DONEQUEUE;
3531 pmu->doneq_index = index_stripped ? (index_stripped | toggle) :
3532 ((toggle ^ 0x4000) + 1);
Ching Huang5b374792014-08-19 15:25:22 +08003533 doneq_index = pmu->doneq_index;
Ching Huang3b8155d2014-09-15 19:05:33 +08003534 spin_unlock_irqrestore(&acb->doneq_lock, flags);
Ching Huanga36ade42018-12-19 16:51:14 +08003535 cdb_phy_hipart = pmu->done_qbuffer[doneq_index &
3536 0xFFF].addressHigh;
Ching Huang5b374792014-08-19 15:25:22 +08003537 flag_ccb = pmu->done_qbuffer[doneq_index & 0xFFF].addressLow;
3538 ccb_cdb_phy = (flag_ccb & 0xFFFFFFF0);
Ching Huanga36ade42018-12-19 16:51:14 +08003539 if (acb->cdb_phyadd_hipart)
3540 ccb_cdb_phy = ccb_cdb_phy | acb->cdb_phyadd_hipart;
Ching Huang5b374792014-08-19 15:25:22 +08003541 arcmsr_cdb = (struct ARCMSR_CDB *)(acb->vir2phy_offset +
3542 ccb_cdb_phy);
3543 pCCB = container_of(arcmsr_cdb, struct CommandControlBlock,
3544 arcmsr_cdb);
3545 poll_ccb_done |= (pCCB == poll_ccb) ? 1 : 0;
3546 if ((pCCB->acb != acb) ||
3547 (pCCB->startdone != ARCMSR_CCB_START)) {
3548 if (pCCB->startdone == ARCMSR_CCB_ABORTED) {
3549 pr_notice("arcmsr%d: scsi id = %d "
3550 "lun = %d ccb = '0x%p' poll command "
3551 "abort successfully\n"
3552 , acb->host->host_no
3553 , pCCB->pcmd->device->id
3554 , (u32)pCCB->pcmd->device->lun
3555 , pCCB);
3556 pCCB->pcmd->result = DID_ABORT << 16;
3557 arcmsr_ccb_complete(pCCB);
3558 continue;
3559 }
3560 pr_notice("arcmsr%d: polling an illegal "
3561 "ccb command done ccb = '0x%p' "
3562 "ccboutstandingcount = %d\n"
3563 , acb->host->host_no
3564 , pCCB
3565 , atomic_read(&acb->ccboutstandingcount));
3566 continue;
3567 }
3568 error = (flag_ccb & ARCMSR_CCBREPLY_FLAG_ERROR_MODE1)
3569 ? true : false;
3570 arcmsr_report_ccb_state(acb, pCCB, error);
3571 }
3572 return rtn;
3573}
3574
Ching Huang23509022017-12-05 09:35:34 +08003575static int arcmsr_hbaE_polling_ccbdone(struct AdapterControlBlock *acb,
3576 struct CommandControlBlock *poll_ccb)
3577{
3578 bool error;
3579 uint32_t poll_ccb_done = 0, poll_count = 0, doneq_index;
3580 uint16_t cmdSMID;
3581 unsigned long flags;
3582 int rtn;
3583 struct CommandControlBlock *pCCB;
3584 struct MessageUnit_E __iomem *reg = acb->pmuE;
3585
3586 polling_hbaC_ccb_retry:
3587 poll_count++;
3588 while (1) {
3589 spin_lock_irqsave(&acb->doneq_lock, flags);
3590 doneq_index = acb->doneq_index;
3591 if ((readl(&reg->reply_post_producer_index) & 0xFFFF) ==
3592 doneq_index) {
3593 spin_unlock_irqrestore(&acb->doneq_lock, flags);
3594 if (poll_ccb_done) {
3595 rtn = SUCCESS;
3596 break;
3597 } else {
3598 msleep(25);
3599 if (poll_count > 40) {
3600 rtn = FAILED;
3601 break;
3602 }
3603 goto polling_hbaC_ccb_retry;
3604 }
3605 }
3606 cmdSMID = acb->pCompletionQ[doneq_index].cmdSMID;
3607 doneq_index++;
3608 if (doneq_index >= acb->completionQ_entry)
3609 doneq_index = 0;
3610 acb->doneq_index = doneq_index;
3611 spin_unlock_irqrestore(&acb->doneq_lock, flags);
3612 pCCB = acb->pccb_pool[cmdSMID];
3613 poll_ccb_done |= (pCCB == poll_ccb) ? 1 : 0;
3614 /* check if command done with no error*/
3615 if ((pCCB->acb != acb) || (pCCB->startdone != ARCMSR_CCB_START)) {
3616 if (pCCB->startdone == ARCMSR_CCB_ABORTED) {
3617 pr_notice("arcmsr%d: scsi id = %d "
3618 "lun = %d ccb = '0x%p' poll command "
3619 "abort successfully\n"
3620 , acb->host->host_no
3621 , pCCB->pcmd->device->id
3622 , (u32)pCCB->pcmd->device->lun
3623 , pCCB);
3624 pCCB->pcmd->result = DID_ABORT << 16;
3625 arcmsr_ccb_complete(pCCB);
3626 continue;
3627 }
3628 pr_notice("arcmsr%d: polling an illegal "
3629 "ccb command done ccb = '0x%p' "
3630 "ccboutstandingcount = %d\n"
3631 , acb->host->host_no
3632 , pCCB
3633 , atomic_read(&acb->ccboutstandingcount));
3634 continue;
3635 }
3636 error = (acb->pCompletionQ[doneq_index].cmdFlag &
3637 ARCMSR_CCBREPLY_FLAG_ERROR_MODE1) ? true : false;
3638 arcmsr_report_ccb_state(acb, pCCB, error);
3639 }
3640 writel(doneq_index, &reg->reply_post_consumer_index);
3641 return rtn;
3642}
3643
Nick Chengae52e7f2010-06-18 15:39:12 +08003644static int arcmsr_polling_ccbdone(struct AdapterControlBlock *acb,
Nick Cheng1a4f5502007-09-13 17:26:40 +08003645 struct CommandControlBlock *poll_ccb)
3646{
Nick Chengae52e7f2010-06-18 15:39:12 +08003647 int rtn = 0;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003648 switch (acb->adapter_type) {
3649
3650 case ACB_ADAPTER_TYPE_A: {
Ching Huang626fa322014-08-19 15:10:12 +08003651 rtn = arcmsr_hbaA_polling_ccbdone(acb, poll_ccb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08003652 }
3653 break;
3654
3655 case ACB_ADAPTER_TYPE_B: {
Ching Huang626fa322014-08-19 15:10:12 +08003656 rtn = arcmsr_hbaB_polling_ccbdone(acb, poll_ccb);
Erich Chen1c57e862006-07-12 08:59:32 -07003657 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08003658 break;
3659 case ACB_ADAPTER_TYPE_C: {
Ching Huang626fa322014-08-19 15:10:12 +08003660 rtn = arcmsr_hbaC_polling_ccbdone(acb, poll_ccb);
Nick Chengcdd3cb12010-07-13 20:03:04 +08003661 }
Ching Huang5b374792014-08-19 15:25:22 +08003662 break;
3663 case ACB_ADAPTER_TYPE_D:
3664 rtn = arcmsr_hbaD_polling_ccbdone(acb, poll_ccb);
3665 break;
Ching Huang23509022017-12-05 09:35:34 +08003666 case ACB_ADAPTER_TYPE_E:
3667 rtn = arcmsr_hbaE_polling_ccbdone(acb, poll_ccb);
3668 break;
Erich Chen1c57e862006-07-12 08:59:32 -07003669 }
Nick Chengae52e7f2010-06-18 15:39:12 +08003670 return rtn;
Erich Chen1c57e862006-07-12 08:59:32 -07003671}
Nick Cheng1a4f5502007-09-13 17:26:40 +08003672
Ching Huangb416c092017-12-05 09:59:52 +08003673static void arcmsr_set_iop_datetime(struct timer_list *t)
3674{
3675 struct AdapterControlBlock *pacb = from_timer(pacb, t, refresh_timer);
Arnd Bergmann45596c72018-01-22 00:12:26 +01003676 unsigned int next_time;
3677 struct tm tm;
3678
Ching Huangb416c092017-12-05 09:59:52 +08003679 union {
3680 struct {
3681 uint16_t signature;
3682 uint8_t year;
3683 uint8_t month;
3684 uint8_t date;
3685 uint8_t hour;
3686 uint8_t minute;
3687 uint8_t second;
3688 } a;
3689 struct {
3690 uint32_t msg_time[2];
3691 } b;
3692 } datetime;
3693
Arnd Bergmann45596c72018-01-22 00:12:26 +01003694 time64_to_tm(ktime_get_real_seconds(), -sys_tz.tz_minuteswest * 60, &tm);
Ching Huangb416c092017-12-05 09:59:52 +08003695
3696 datetime.a.signature = 0x55AA;
Arnd Bergmann45596c72018-01-22 00:12:26 +01003697 datetime.a.year = tm.tm_year - 100; /* base 2000 instead of 1900 */
3698 datetime.a.month = tm.tm_mon;
3699 datetime.a.date = tm.tm_mday;
3700 datetime.a.hour = tm.tm_hour;
3701 datetime.a.minute = tm.tm_min;
3702 datetime.a.second = tm.tm_sec;
Ching Huangb416c092017-12-05 09:59:52 +08003703
3704 switch (pacb->adapter_type) {
3705 case ACB_ADAPTER_TYPE_A: {
3706 struct MessageUnit_A __iomem *reg = pacb->pmuA;
3707 writel(datetime.b.msg_time[0], &reg->message_rwbuffer[0]);
3708 writel(datetime.b.msg_time[1], &reg->message_rwbuffer[1]);
3709 writel(ARCMSR_INBOUND_MESG0_SYNC_TIMER, &reg->inbound_msgaddr0);
3710 break;
3711 }
3712 case ACB_ADAPTER_TYPE_B: {
3713 uint32_t __iomem *rwbuffer;
3714 struct MessageUnit_B *reg = pacb->pmuB;
3715 rwbuffer = reg->message_rwbuffer;
3716 writel(datetime.b.msg_time[0], rwbuffer++);
3717 writel(datetime.b.msg_time[1], rwbuffer++);
3718 writel(ARCMSR_MESSAGE_SYNC_TIMER, reg->drv2iop_doorbell);
3719 break;
3720 }
3721 case ACB_ADAPTER_TYPE_C: {
3722 struct MessageUnit_C __iomem *reg = pacb->pmuC;
3723 writel(datetime.b.msg_time[0], &reg->msgcode_rwbuffer[0]);
3724 writel(datetime.b.msg_time[1], &reg->msgcode_rwbuffer[1]);
3725 writel(ARCMSR_INBOUND_MESG0_SYNC_TIMER, &reg->inbound_msgaddr0);
3726 writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &reg->inbound_doorbell);
3727 break;
3728 }
3729 case ACB_ADAPTER_TYPE_D: {
3730 uint32_t __iomem *rwbuffer;
3731 struct MessageUnit_D *reg = pacb->pmuD;
3732 rwbuffer = reg->msgcode_rwbuffer;
3733 writel(datetime.b.msg_time[0], rwbuffer++);
3734 writel(datetime.b.msg_time[1], rwbuffer++);
3735 writel(ARCMSR_INBOUND_MESG0_SYNC_TIMER, reg->inbound_msgaddr0);
3736 break;
3737 }
3738 case ACB_ADAPTER_TYPE_E: {
3739 struct MessageUnit_E __iomem *reg = pacb->pmuE;
3740 writel(datetime.b.msg_time[0], &reg->msgcode_rwbuffer[0]);
3741 writel(datetime.b.msg_time[1], &reg->msgcode_rwbuffer[1]);
3742 writel(ARCMSR_INBOUND_MESG0_SYNC_TIMER, &reg->inbound_msgaddr0);
3743 pacb->out_doorbell ^= ARCMSR_HBEMU_DRV2IOP_MESSAGE_CMD_DONE;
3744 writel(pacb->out_doorbell, &reg->iobound_doorbell);
3745 break;
3746 }
3747 }
3748 if (sys_tz.tz_minuteswest)
3749 next_time = ARCMSR_HOURS;
3750 else
3751 next_time = ARCMSR_MINUTES;
3752 mod_timer(&pacb->refresh_timer, jiffies + msecs_to_jiffies(next_time));
3753}
3754
Nick Cheng1a4f5502007-09-13 17:26:40 +08003755static int arcmsr_iop_confirm(struct AdapterControlBlock *acb)
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08003756{
Nick Chengae52e7f2010-06-18 15:39:12 +08003757 uint32_t cdb_phyaddr, cdb_phyaddr_hi32;
Ching Huang6e38adf2014-08-19 15:14:14 +08003758 dma_addr_t dma_coherent_handle;
Dan Carpentere2c70422014-02-11 19:06:33 +03003759
Nick Cheng1a4f5502007-09-13 17:26:40 +08003760 /*
3761 ********************************************************************
3762 ** here we need to tell iop 331 our freeccb.HighPart
3763 ** if freeccb.HighPart is not zero
3764 ********************************************************************
3765 */
Ching Huang6e38adf2014-08-19 15:14:14 +08003766 switch (acb->adapter_type) {
3767 case ACB_ADAPTER_TYPE_B:
Ching Huang5b374792014-08-19 15:25:22 +08003768 case ACB_ADAPTER_TYPE_D:
Ching Huang6e38adf2014-08-19 15:14:14 +08003769 dma_coherent_handle = acb->dma_coherent_handle2;
3770 break;
Ching Huang23509022017-12-05 09:35:34 +08003771 case ACB_ADAPTER_TYPE_E:
3772 dma_coherent_handle = acb->dma_coherent_handle +
3773 offsetof(struct CommandControlBlock, arcmsr_cdb);
3774 break;
Ching Huang6e38adf2014-08-19 15:14:14 +08003775 default:
3776 dma_coherent_handle = acb->dma_coherent_handle;
3777 break;
3778 }
3779 cdb_phyaddr = lower_32_bits(dma_coherent_handle);
3780 cdb_phyaddr_hi32 = upper_32_bits(dma_coherent_handle);
Nick Chengcdd3cb12010-07-13 20:03:04 +08003781 acb->cdb_phyaddr_hi32 = cdb_phyaddr_hi32;
Ching Huang7860a482018-12-19 16:34:58 +08003782 acb->cdb_phyadd_hipart = ((uint64_t)cdb_phyaddr_hi32) << 32;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003783 /*
3784 ***********************************************************************
3785 ** if adapter type B, set window of "post command Q"
3786 ***********************************************************************
3787 */
3788 switch (acb->adapter_type) {
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08003789
Nick Cheng1a4f5502007-09-13 17:26:40 +08003790 case ACB_ADAPTER_TYPE_A: {
Nick Chengae52e7f2010-06-18 15:39:12 +08003791 if (cdb_phyaddr_hi32 != 0) {
Al Viro80da1ad2007-10-29 05:08:28 +00003792 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003793 writel(ARCMSR_SIGNATURE_SET_CONFIG, \
3794 &reg->message_rwbuffer[0]);
Nick Chengae52e7f2010-06-18 15:39:12 +08003795 writel(cdb_phyaddr_hi32, &reg->message_rwbuffer[1]);
Nick Cheng1a4f5502007-09-13 17:26:40 +08003796 writel(ARCMSR_INBOUND_MESG0_SET_CONFIG, \
3797 &reg->inbound_msgaddr0);
Ching Huang626fa322014-08-19 15:10:12 +08003798 if (!arcmsr_hbaA_wait_msgint_ready(acb)) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08003799 printk(KERN_NOTICE "arcmsr%d: ""set ccb high \
3800 part physical address timeout\n",
3801 acb->host->host_no);
3802 return 1;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08003803 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08003804 }
3805 }
3806 break;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08003807
Nick Cheng1a4f5502007-09-13 17:26:40 +08003808 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00003809 uint32_t __iomem *rwbuffer;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08003810
Al Viro80da1ad2007-10-29 05:08:28 +00003811 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003812 reg->postq_index = 0;
3813 reg->doneq_index = 0;
Nick Chengae52e7f2010-06-18 15:39:12 +08003814 writel(ARCMSR_MESSAGE_SET_POST_WINDOW, reg->drv2iop_doorbell);
Ching Huang626fa322014-08-19 15:10:12 +08003815 if (!arcmsr_hbaB_wait_msgint_ready(acb)) {
Nik Nyby47268a42015-06-30 17:25:27 -04003816 printk(KERN_NOTICE "arcmsr%d: cannot set driver mode\n", \
Nick Cheng1a4f5502007-09-13 17:26:40 +08003817 acb->host->host_no);
3818 return 1;
3819 }
Nick Chengae52e7f2010-06-18 15:39:12 +08003820 rwbuffer = reg->message_rwbuffer;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003821 /* driver "set config" signature */
3822 writel(ARCMSR_SIGNATURE_SET_CONFIG, rwbuffer++);
3823 /* normal should be zero */
Nick Chengae52e7f2010-06-18 15:39:12 +08003824 writel(cdb_phyaddr_hi32, rwbuffer++);
Nick Cheng1a4f5502007-09-13 17:26:40 +08003825 /* postQ size (256 + 8)*4 */
Ching Huang6e38adf2014-08-19 15:14:14 +08003826 writel(cdb_phyaddr, rwbuffer++);
Nick Cheng1a4f5502007-09-13 17:26:40 +08003827 /* doneQ size (256 + 8)*4 */
Ching Huang6e38adf2014-08-19 15:14:14 +08003828 writel(cdb_phyaddr + 1056, rwbuffer++);
Nick Cheng1a4f5502007-09-13 17:26:40 +08003829 /* ccb maxQ size must be --> [(256 + 8)*4]*/
3830 writel(1056, rwbuffer);
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08003831
Nick Chengae52e7f2010-06-18 15:39:12 +08003832 writel(ARCMSR_MESSAGE_SET_CONFIG, reg->drv2iop_doorbell);
Ching Huang626fa322014-08-19 15:10:12 +08003833 if (!arcmsr_hbaB_wait_msgint_ready(acb)) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08003834 printk(KERN_NOTICE "arcmsr%d: 'set command Q window' \
3835 timeout \n",acb->host->host_no);
3836 return 1;
3837 }
Ching Huanga5849722014-08-19 15:01:28 +08003838 writel(ARCMSR_MESSAGE_START_DRIVER_MODE, reg->drv2iop_doorbell);
Ching Huang626fa322014-08-19 15:10:12 +08003839 if (!arcmsr_hbaB_wait_msgint_ready(acb)) {
Ching Huanga5849722014-08-19 15:01:28 +08003840 pr_err("arcmsr%d: can't set driver mode.\n",
3841 acb->host->host_no);
3842 return 1;
3843 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08003844 }
3845 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003846 case ACB_ADAPTER_TYPE_C: {
Ching Huangc10b1d52014-08-19 15:20:31 +08003847 struct MessageUnit_C __iomem *reg = acb->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003848
Tomas Henzl8b7eb862011-04-29 16:28:24 +02003849 printk(KERN_NOTICE "arcmsr%d: cdb_phyaddr_hi32=0x%x\n",
3850 acb->adapter_index, cdb_phyaddr_hi32);
Nick Chengcdd3cb12010-07-13 20:03:04 +08003851 writel(ARCMSR_SIGNATURE_SET_CONFIG, &reg->msgcode_rwbuffer[0]);
3852 writel(cdb_phyaddr_hi32, &reg->msgcode_rwbuffer[1]);
3853 writel(ARCMSR_INBOUND_MESG0_SET_CONFIG, &reg->inbound_msgaddr0);
3854 writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &reg->inbound_doorbell);
Ching Huang626fa322014-08-19 15:10:12 +08003855 if (!arcmsr_hbaC_wait_msgint_ready(acb)) {
Nick Chengcdd3cb12010-07-13 20:03:04 +08003856 printk(KERN_NOTICE "arcmsr%d: 'set command Q window' \
3857 timeout \n", acb->host->host_no);
3858 return 1;
3859 }
3860 }
Ching Huang5b374792014-08-19 15:25:22 +08003861 break;
3862 case ACB_ADAPTER_TYPE_D: {
3863 uint32_t __iomem *rwbuffer;
3864 struct MessageUnit_D *reg = acb->pmuD;
3865 reg->postq_index = 0;
3866 reg->doneq_index = 0;
3867 rwbuffer = reg->msgcode_rwbuffer;
3868 writel(ARCMSR_SIGNATURE_SET_CONFIG, rwbuffer++);
3869 writel(cdb_phyaddr_hi32, rwbuffer++);
3870 writel(cdb_phyaddr, rwbuffer++);
3871 writel(cdb_phyaddr + (ARCMSR_MAX_ARC1214_POSTQUEUE *
3872 sizeof(struct InBound_SRB)), rwbuffer++);
3873 writel(0x100, rwbuffer);
3874 writel(ARCMSR_INBOUND_MESG0_SET_CONFIG, reg->inbound_msgaddr0);
3875 if (!arcmsr_hbaD_wait_msgint_ready(acb)) {
3876 pr_notice("arcmsr%d: 'set command Q window' timeout\n",
3877 acb->host->host_no);
3878 return 1;
3879 }
3880 }
3881 break;
Ching Huang23509022017-12-05 09:35:34 +08003882 case ACB_ADAPTER_TYPE_E: {
3883 struct MessageUnit_E __iomem *reg = acb->pmuE;
3884 writel(ARCMSR_SIGNATURE_SET_CONFIG, &reg->msgcode_rwbuffer[0]);
3885 writel(ARCMSR_SIGNATURE_1884, &reg->msgcode_rwbuffer[1]);
3886 writel(cdb_phyaddr, &reg->msgcode_rwbuffer[2]);
3887 writel(cdb_phyaddr_hi32, &reg->msgcode_rwbuffer[3]);
3888 writel(acb->ccbsize, &reg->msgcode_rwbuffer[4]);
3889 dma_coherent_handle = acb->dma_coherent_handle2;
3890 cdb_phyaddr = (uint32_t)(dma_coherent_handle & 0xffffffff);
3891 cdb_phyaddr_hi32 = (uint32_t)((dma_coherent_handle >> 16) >> 16);
3892 writel(cdb_phyaddr, &reg->msgcode_rwbuffer[5]);
3893 writel(cdb_phyaddr_hi32, &reg->msgcode_rwbuffer[6]);
Ching Huang381d66d2018-12-19 16:24:03 +08003894 writel(acb->ioqueue_size, &reg->msgcode_rwbuffer[7]);
Ching Huang23509022017-12-05 09:35:34 +08003895 writel(ARCMSR_INBOUND_MESG0_SET_CONFIG, &reg->inbound_msgaddr0);
3896 acb->out_doorbell ^= ARCMSR_HBEMU_DRV2IOP_MESSAGE_CMD_DONE;
3897 writel(acb->out_doorbell, &reg->iobound_doorbell);
3898 if (!arcmsr_hbaE_wait_msgint_ready(acb)) {
3899 pr_notice("arcmsr%d: 'set command Q window' timeout \n",
3900 acb->host->host_no);
3901 return 1;
3902 }
3903 }
3904 break;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08003905 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08003906 return 0;
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08003907}
3908
Nick Cheng1a4f5502007-09-13 17:26:40 +08003909static void arcmsr_wait_firmware_ready(struct AdapterControlBlock *acb)
3910{
3911 uint32_t firmware_state = 0;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003912 switch (acb->adapter_type) {
3913
3914 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00003915 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003916 do {
Ching Huangc2c62eb2018-03-15 14:37:40 +08003917 if (!(acb->acb_flags & ACB_F_IOP_INITED))
3918 msleep(20);
Nick Cheng1a4f5502007-09-13 17:26:40 +08003919 firmware_state = readl(&reg->outbound_msgaddr1);
3920 } while ((firmware_state & ARCMSR_OUTBOUND_MESG1_FIRMWARE_OK) == 0);
3921 }
3922 break;
3923
3924 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00003925 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003926 do {
Ching Huangc2c62eb2018-03-15 14:37:40 +08003927 if (!(acb->acb_flags & ACB_F_IOP_INITED))
3928 msleep(20);
Nick Chengae52e7f2010-06-18 15:39:12 +08003929 firmware_state = readl(reg->iop2drv_doorbell);
Nick Cheng1a4f5502007-09-13 17:26:40 +08003930 } while ((firmware_state & ARCMSR_MESSAGE_FIRMWARE_OK) == 0);
Nick Chengae52e7f2010-06-18 15:39:12 +08003931 writel(ARCMSR_DRV2IOP_END_OF_INTERRUPT, reg->drv2iop_doorbell);
Nick Cheng1a4f5502007-09-13 17:26:40 +08003932 }
3933 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003934 case ACB_ADAPTER_TYPE_C: {
Ching Huangc10b1d52014-08-19 15:20:31 +08003935 struct MessageUnit_C __iomem *reg = acb->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08003936 do {
Ching Huangc2c62eb2018-03-15 14:37:40 +08003937 if (!(acb->acb_flags & ACB_F_IOP_INITED))
3938 msleep(20);
Nick Chengcdd3cb12010-07-13 20:03:04 +08003939 firmware_state = readl(&reg->outbound_msgaddr1);
3940 } while ((firmware_state & ARCMSR_HBCMU_MESSAGE_FIRMWARE_OK) == 0);
3941 }
Ching Huang5b374792014-08-19 15:25:22 +08003942 break;
3943 case ACB_ADAPTER_TYPE_D: {
3944 struct MessageUnit_D *reg = acb->pmuD;
3945 do {
Ching Huangc2c62eb2018-03-15 14:37:40 +08003946 if (!(acb->acb_flags & ACB_F_IOP_INITED))
3947 msleep(20);
Ching Huang5b374792014-08-19 15:25:22 +08003948 firmware_state = readl(reg->outbound_msgaddr1);
3949 } while ((firmware_state &
3950 ARCMSR_ARC1214_MESSAGE_FIRMWARE_OK) == 0);
3951 }
3952 break;
Ching Huang23509022017-12-05 09:35:34 +08003953 case ACB_ADAPTER_TYPE_E: {
3954 struct MessageUnit_E __iomem *reg = acb->pmuE;
3955 do {
Ching Huangc2c62eb2018-03-15 14:37:40 +08003956 if (!(acb->acb_flags & ACB_F_IOP_INITED))
3957 msleep(20);
Ching Huang23509022017-12-05 09:35:34 +08003958 firmware_state = readl(&reg->outbound_msgaddr1);
3959 } while ((firmware_state & ARCMSR_HBEMU_MESSAGE_FIRMWARE_OK) == 0);
3960 }
3961 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08003962 }
3963}
3964
Kees Cooke99e88a2017-10-16 14:43:17 -07003965static void arcmsr_request_device_map(struct timer_list *t)
Nick Cheng36b83de2010-05-17 11:22:42 +08003966{
Kees Cooke99e88a2017-10-16 14:43:17 -07003967 struct AdapterControlBlock *acb = from_timer(acb, t, eternal_timer);
Ching Huang6ae9abe2017-12-13 16:33:36 +08003968 if (unlikely(atomic_read(&acb->rq_map_token) == 0) ||
3969 (acb->acb_flags & ACB_F_BUS_RESET) ||
3970 (acb->acb_flags & ACB_F_ABORT)) {
3971 mod_timer(&acb->eternal_timer,
3972 jiffies + msecs_to_jiffies(6 * HZ));
3973 } else {
3974 acb->fw_flag = FW_NORMAL;
3975 if (atomic_read(&acb->ante_token_value) ==
3976 atomic_read(&acb->rq_map_token)) {
3977 atomic_set(&acb->rq_map_token, 16);
3978 }
3979 atomic_set(&acb->ante_token_value,
3980 atomic_read(&acb->rq_map_token));
3981 if (atomic_dec_and_test(&acb->rq_map_token)) {
3982 mod_timer(&acb->eternal_timer, jiffies +
3983 msecs_to_jiffies(6 * HZ));
3984 return;
3985 }
3986 switch (acb->adapter_type) {
Nick Cheng36b83de2010-05-17 11:22:42 +08003987 case ACB_ADAPTER_TYPE_A: {
Ching Huang6ae9abe2017-12-13 16:33:36 +08003988 struct MessageUnit_A __iomem *reg = acb->pmuA;
3989 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
3990 break;
3991 }
Nick Cheng36b83de2010-05-17 11:22:42 +08003992 case ACB_ADAPTER_TYPE_B: {
Ching Huang6ae9abe2017-12-13 16:33:36 +08003993 struct MessageUnit_B *reg = acb->pmuB;
3994 writel(ARCMSR_MESSAGE_GET_CONFIG, reg->drv2iop_doorbell);
3995 break;
3996 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08003997 case ACB_ADAPTER_TYPE_C: {
Ching Huang6ae9abe2017-12-13 16:33:36 +08003998 struct MessageUnit_C __iomem *reg = acb->pmuC;
3999 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
4000 writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &reg->inbound_doorbell);
4001 break;
4002 }
4003 case ACB_ADAPTER_TYPE_D: {
4004 struct MessageUnit_D *reg = acb->pmuD;
4005 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, reg->inbound_msgaddr0);
4006 break;
4007 }
4008 case ACB_ADAPTER_TYPE_E: {
4009 struct MessageUnit_E __iomem *reg = acb->pmuE;
4010 writel(ARCMSR_INBOUND_MESG0_GET_CONFIG, &reg->inbound_msgaddr0);
4011 acb->out_doorbell ^= ARCMSR_HBEMU_DRV2IOP_MESSAGE_CMD_DONE;
4012 writel(acb->out_doorbell, &reg->iobound_doorbell);
4013 break;
4014 }
4015 default:
4016 return;
Nick Chengcdd3cb12010-07-13 20:03:04 +08004017 }
Ching Huang6ae9abe2017-12-13 16:33:36 +08004018 acb->acb_flags |= ACB_F_MSG_GET_CONFIG;
4019 mod_timer(&acb->eternal_timer, jiffies + msecs_to_jiffies(6 * HZ));
Nick Cheng36b83de2010-05-17 11:22:42 +08004020 }
4021}
4022
Ching Huang626fa322014-08-19 15:10:12 +08004023static void arcmsr_hbaA_start_bgrb(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08004024{
Al Viro80da1ad2007-10-29 05:08:28 +00004025 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08004026 acb->acb_flags |= ACB_F_MSG_START_BGRB;
4027 writel(ARCMSR_INBOUND_MESG0_START_BGRB, &reg->inbound_msgaddr0);
Ching Huang626fa322014-08-19 15:10:12 +08004028 if (!arcmsr_hbaA_wait_msgint_ready(acb)) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08004029 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
Geert Uytterhoeven948dff7a2018-09-03 19:35:29 +02004030 rebuild' timeout \n", acb->host->host_no);
Nick Cheng1a4f5502007-09-13 17:26:40 +08004031 }
4032}
4033
Ching Huang626fa322014-08-19 15:10:12 +08004034static void arcmsr_hbaB_start_bgrb(struct AdapterControlBlock *acb)
Nick Cheng1a4f5502007-09-13 17:26:40 +08004035{
Al Viro80da1ad2007-10-29 05:08:28 +00004036 struct MessageUnit_B *reg = acb->pmuB;
Nick Cheng1a4f5502007-09-13 17:26:40 +08004037 acb->acb_flags |= ACB_F_MSG_START_BGRB;
Nick Chengae52e7f2010-06-18 15:39:12 +08004038 writel(ARCMSR_MESSAGE_START_BGRB, reg->drv2iop_doorbell);
Ching Huang626fa322014-08-19 15:10:12 +08004039 if (!arcmsr_hbaB_wait_msgint_ready(acb)) {
Nick Cheng1a4f5502007-09-13 17:26:40 +08004040 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
Geert Uytterhoeven948dff7a2018-09-03 19:35:29 +02004041 rebuild' timeout \n",acb->host->host_no);
Nick Cheng1a4f5502007-09-13 17:26:40 +08004042 }
4043}
4044
Ching Huang626fa322014-08-19 15:10:12 +08004045static void arcmsr_hbaC_start_bgrb(struct AdapterControlBlock *pACB)
Nick Chengcdd3cb12010-07-13 20:03:04 +08004046{
Ching Huangc10b1d52014-08-19 15:20:31 +08004047 struct MessageUnit_C __iomem *phbcmu = pACB->pmuC;
Nick Chengcdd3cb12010-07-13 20:03:04 +08004048 pACB->acb_flags |= ACB_F_MSG_START_BGRB;
4049 writel(ARCMSR_INBOUND_MESG0_START_BGRB, &phbcmu->inbound_msgaddr0);
4050 writel(ARCMSR_HBCMU_DRV2IOP_MESSAGE_CMD_DONE, &phbcmu->inbound_doorbell);
Ching Huang626fa322014-08-19 15:10:12 +08004051 if (!arcmsr_hbaC_wait_msgint_ready(pACB)) {
Nick Chengcdd3cb12010-07-13 20:03:04 +08004052 printk(KERN_NOTICE "arcmsr%d: wait 'start adapter background \
Geert Uytterhoeven948dff7a2018-09-03 19:35:29 +02004053 rebuild' timeout \n", pACB->host->host_no);
Nick Chengcdd3cb12010-07-13 20:03:04 +08004054 }
4055 return;
4056}
Ching Huang5b374792014-08-19 15:25:22 +08004057
4058static void arcmsr_hbaD_start_bgrb(struct AdapterControlBlock *pACB)
4059{
4060 struct MessageUnit_D *pmu = pACB->pmuD;
4061
4062 pACB->acb_flags |= ACB_F_MSG_START_BGRB;
4063 writel(ARCMSR_INBOUND_MESG0_START_BGRB, pmu->inbound_msgaddr0);
4064 if (!arcmsr_hbaD_wait_msgint_ready(pACB)) {
4065 pr_notice("arcmsr%d: wait 'start adapter "
Geert Uytterhoeven948dff7a2018-09-03 19:35:29 +02004066 "background rebuild' timeout\n", pACB->host->host_no);
Ching Huang5b374792014-08-19 15:25:22 +08004067 }
4068}
4069
Ching Huang23509022017-12-05 09:35:34 +08004070static void arcmsr_hbaE_start_bgrb(struct AdapterControlBlock *pACB)
4071{
4072 struct MessageUnit_E __iomem *pmu = pACB->pmuE;
4073
4074 pACB->acb_flags |= ACB_F_MSG_START_BGRB;
4075 writel(ARCMSR_INBOUND_MESG0_START_BGRB, &pmu->inbound_msgaddr0);
4076 pACB->out_doorbell ^= ARCMSR_HBEMU_DRV2IOP_MESSAGE_CMD_DONE;
4077 writel(pACB->out_doorbell, &pmu->iobound_doorbell);
4078 if (!arcmsr_hbaE_wait_msgint_ready(pACB)) {
4079 pr_notice("arcmsr%d: wait 'start adapter "
Geert Uytterhoeven948dff7a2018-09-03 19:35:29 +02004080 "background rebuild' timeout \n", pACB->host->host_no);
Ching Huang23509022017-12-05 09:35:34 +08004081 }
4082}
4083
Nick Cheng1a4f5502007-09-13 17:26:40 +08004084static void arcmsr_start_adapter_bgrb(struct AdapterControlBlock *acb)
4085{
4086 switch (acb->adapter_type) {
4087 case ACB_ADAPTER_TYPE_A:
Ching Huang626fa322014-08-19 15:10:12 +08004088 arcmsr_hbaA_start_bgrb(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08004089 break;
4090 case ACB_ADAPTER_TYPE_B:
Ching Huang626fa322014-08-19 15:10:12 +08004091 arcmsr_hbaB_start_bgrb(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08004092 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08004093 case ACB_ADAPTER_TYPE_C:
Ching Huang626fa322014-08-19 15:10:12 +08004094 arcmsr_hbaC_start_bgrb(acb);
Ching Huang5b374792014-08-19 15:25:22 +08004095 break;
4096 case ACB_ADAPTER_TYPE_D:
4097 arcmsr_hbaD_start_bgrb(acb);
4098 break;
Ching Huang23509022017-12-05 09:35:34 +08004099 case ACB_ADAPTER_TYPE_E:
4100 arcmsr_hbaE_start_bgrb(acb);
4101 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08004102 }
4103}
4104
4105static void arcmsr_clear_doorbell_queue_buffer(struct AdapterControlBlock *acb)
4106{
4107 switch (acb->adapter_type) {
4108 case ACB_ADAPTER_TYPE_A: {
Al Viro80da1ad2007-10-29 05:08:28 +00004109 struct MessageUnit_A __iomem *reg = acb->pmuA;
Nick Cheng1a4f5502007-09-13 17:26:40 +08004110 uint32_t outbound_doorbell;
4111 /* empty doorbell Qbuffer if door bell ringed */
4112 outbound_doorbell = readl(&reg->outbound_doorbell);
4113 /*clear doorbell interrupt */
4114 writel(outbound_doorbell, &reg->outbound_doorbell);
4115 writel(ARCMSR_INBOUND_DRIVER_DATA_READ_OK, &reg->inbound_doorbell);
4116 }
4117 break;
4118
4119 case ACB_ADAPTER_TYPE_B: {
Al Viro80da1ad2007-10-29 05:08:28 +00004120 struct MessageUnit_B *reg = acb->pmuB;
Ching Huang2124c5b2017-12-05 10:02:16 +08004121 uint32_t outbound_doorbell, i;
4122 writel(ARCMSR_DOORBELL_INT_CLEAR_PATTERN, reg->iop2drv_doorbell);
Nick Chengae52e7f2010-06-18 15:39:12 +08004123 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell);
Nick Cheng1a4f5502007-09-13 17:26:40 +08004124 /* let IOP know data has been read */
Ching Huang2124c5b2017-12-05 10:02:16 +08004125 for(i=0; i < 200; i++) {
4126 msleep(20);
4127 outbound_doorbell = readl(reg->iop2drv_doorbell);
4128 if( outbound_doorbell & ARCMSR_IOP2DRV_DATA_WRITE_OK) {
4129 writel(ARCMSR_DOORBELL_INT_CLEAR_PATTERN, reg->iop2drv_doorbell);
4130 writel(ARCMSR_DRV2IOP_DATA_READ_OK, reg->drv2iop_doorbell);
4131 } else
4132 break;
4133 }
Nick Cheng1a4f5502007-09-13 17:26:40 +08004134 }
4135 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08004136 case ACB_ADAPTER_TYPE_C: {
Ching Huangc10b1d52014-08-19 15:20:31 +08004137 struct MessageUnit_C __iomem *reg = acb->pmuC;
Ching Huang5eb6bfa2014-08-19 15:07:35 +08004138 uint32_t outbound_doorbell, i;
Nick Chengcdd3cb12010-07-13 20:03:04 +08004139 /* empty doorbell Qbuffer if door bell ringed */
4140 outbound_doorbell = readl(&reg->outbound_doorbell);
4141 writel(outbound_doorbell, &reg->outbound_doorbell_clear);
4142 writel(ARCMSR_HBCMU_DRV2IOP_DATA_READ_OK, &reg->inbound_doorbell);
Ching Huang5eb6bfa2014-08-19 15:07:35 +08004143 for (i = 0; i < 200; i++) {
4144 msleep(20);
4145 outbound_doorbell = readl(&reg->outbound_doorbell);
4146 if (outbound_doorbell &
4147 ARCMSR_HBCMU_IOP2DRV_DATA_WRITE_OK) {
4148 writel(outbound_doorbell,
4149 &reg->outbound_doorbell_clear);
4150 writel(ARCMSR_HBCMU_DRV2IOP_DATA_READ_OK,
4151 &reg->inbound_doorbell);
4152 } else
4153 break;
4154 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08004155 }
Ching Huang5b374792014-08-19 15:25:22 +08004156 break;
4157 case ACB_ADAPTER_TYPE_D: {
4158 struct MessageUnit_D *reg = acb->pmuD;
4159 uint32_t outbound_doorbell, i;
4160 /* empty doorbell Qbuffer if door bell ringed */
4161 outbound_doorbell = readl(reg->outbound_doorbell);
4162 writel(outbound_doorbell, reg->outbound_doorbell);
4163 writel(ARCMSR_ARC1214_DRV2IOP_DATA_OUT_READ,
4164 reg->inbound_doorbell);
4165 for (i = 0; i < 200; i++) {
4166 msleep(20);
4167 outbound_doorbell = readl(reg->outbound_doorbell);
4168 if (outbound_doorbell &
4169 ARCMSR_ARC1214_IOP2DRV_DATA_WRITE_OK) {
4170 writel(outbound_doorbell,
4171 reg->outbound_doorbell);
4172 writel(ARCMSR_ARC1214_DRV2IOP_DATA_OUT_READ,
4173 reg->inbound_doorbell);
4174 } else
4175 break;
4176 }
4177 }
4178 break;
Ching Huang23509022017-12-05 09:35:34 +08004179 case ACB_ADAPTER_TYPE_E: {
4180 struct MessageUnit_E __iomem *reg = acb->pmuE;
4181 uint32_t i, tmp;
4182
4183 acb->in_doorbell = readl(&reg->iobound_doorbell);
4184 writel(0, &reg->host_int_status); /*clear interrupt*/
4185 acb->out_doorbell ^= ARCMSR_HBEMU_DRV2IOP_DATA_READ_OK;
4186 writel(acb->out_doorbell, &reg->iobound_doorbell);
4187 for(i=0; i < 200; i++) {
4188 msleep(20);
4189 tmp = acb->in_doorbell;
4190 acb->in_doorbell = readl(&reg->iobound_doorbell);
4191 if((tmp ^ acb->in_doorbell) & ARCMSR_HBEMU_IOP2DRV_DATA_WRITE_OK) {
4192 writel(0, &reg->host_int_status); /*clear interrupt*/
4193 acb->out_doorbell ^= ARCMSR_HBEMU_DRV2IOP_DATA_READ_OK;
4194 writel(acb->out_doorbell, &reg->iobound_doorbell);
4195 } else
4196 break;
4197 }
4198 }
4199 break;
Nick Cheng1a4f5502007-09-13 17:26:40 +08004200 }
4201}
Erich Chen1c57e862006-07-12 08:59:32 -07004202
Nick Cheng76d78302008-02-04 23:53:24 -08004203static void arcmsr_enable_eoi_mode(struct AdapterControlBlock *acb)
4204{
4205 switch (acb->adapter_type) {
4206 case ACB_ADAPTER_TYPE_A:
4207 return;
4208 case ACB_ADAPTER_TYPE_B:
4209 {
4210 struct MessageUnit_B *reg = acb->pmuB;
Nick Chengae52e7f2010-06-18 15:39:12 +08004211 writel(ARCMSR_MESSAGE_ACTIVE_EOI_MODE, reg->drv2iop_doorbell);
Ching Huang626fa322014-08-19 15:10:12 +08004212 if (!arcmsr_hbaB_wait_msgint_ready(acb)) {
Nick Cheng76d78302008-02-04 23:53:24 -08004213 printk(KERN_NOTICE "ARCMSR IOP enables EOI_MODE TIMEOUT");
4214 return;
4215 }
4216 }
4217 break;
Nick Chengcdd3cb12010-07-13 20:03:04 +08004218 case ACB_ADAPTER_TYPE_C:
4219 return;
Nick Cheng76d78302008-02-04 23:53:24 -08004220 }
4221 return;
4222}
4223
Nick Cheng36b83de2010-05-17 11:22:42 +08004224static void arcmsr_hardware_reset(struct AdapterControlBlock *acb)
4225{
4226 uint8_t value[64];
Nick Chengcdd3cb12010-07-13 20:03:04 +08004227 int i, count = 0;
4228 struct MessageUnit_A __iomem *pmuA = acb->pmuA;
4229 struct MessageUnit_C __iomem *pmuC = acb->pmuC;
Ching Huang5b374792014-08-19 15:25:22 +08004230 struct MessageUnit_D *pmuD = acb->pmuD;
Dan Carpenter6ad819b2012-06-09 12:10:19 +03004231
Nick Cheng36b83de2010-05-17 11:22:42 +08004232 /* backup pci config data */
Nick Chengcdd3cb12010-07-13 20:03:04 +08004233 printk(KERN_NOTICE "arcmsr%d: executing hw bus reset .....\n", acb->host->host_no);
Nick Cheng36b83de2010-05-17 11:22:42 +08004234 for (i = 0; i < 64; i++) {
4235 pci_read_config_byte(acb->pdev, i, &value[i]);
4236 }
4237 /* hardware reset signal */
Colin Ian Kingca2ade22018-10-01 00:03:07 +01004238 if (acb->dev_id == 0x1680) {
Nick Chengcdd3cb12010-07-13 20:03:04 +08004239 writel(ARCMSR_ARC1680_BUS_RESET, &pmuA->reserved1[0]);
Colin Ian Kingca2ade22018-10-01 00:03:07 +01004240 } else if (acb->dev_id == 0x1880) {
Nick Chengcdd3cb12010-07-13 20:03:04 +08004241 do {
4242 count++;
4243 writel(0xF, &pmuC->write_sequence);
4244 writel(0x4, &pmuC->write_sequence);
4245 writel(0xB, &pmuC->write_sequence);
4246 writel(0x2, &pmuC->write_sequence);
4247 writel(0x7, &pmuC->write_sequence);
4248 writel(0xD, &pmuC->write_sequence);
Dan Carpenter6ad819b2012-06-09 12:10:19 +03004249 } while (((readl(&pmuC->host_diagnostic) & ARCMSR_ARC1880_DiagWrite_ENABLE) == 0) && (count < 5));
Nick Chengcdd3cb12010-07-13 20:03:04 +08004250 writel(ARCMSR_ARC1880_RESET_ADAPTER, &pmuC->host_diagnostic);
Ching Huang23509022017-12-05 09:35:34 +08004251 } else if (acb->dev_id == 0x1884) {
4252 struct MessageUnit_E __iomem *pmuE = acb->pmuE;
4253 do {
4254 count++;
4255 writel(0x4, &pmuE->write_sequence_3xxx);
4256 writel(0xB, &pmuE->write_sequence_3xxx);
4257 writel(0x2, &pmuE->write_sequence_3xxx);
4258 writel(0x7, &pmuE->write_sequence_3xxx);
4259 writel(0xD, &pmuE->write_sequence_3xxx);
4260 mdelay(10);
4261 } while (((readl(&pmuE->host_diagnostic_3xxx) &
4262 ARCMSR_ARC1884_DiagWrite_ENABLE) == 0) && (count < 5));
4263 writel(ARCMSR_ARC188X_RESET_ADAPTER, &pmuE->host_diagnostic_3xxx);
Colin Ian Kingca2ade22018-10-01 00:03:07 +01004264 } else if (acb->dev_id == 0x1214) {
Ching Huang5b374792014-08-19 15:25:22 +08004265 writel(0x20, pmuD->reset_request);
Nick Chengae52e7f2010-06-18 15:39:12 +08004266 } else {
Nick Chengcdd3cb12010-07-13 20:03:04 +08004267 pci_write_config_byte(acb->pdev, 0x84, 0x20);
Nick Chengae52e7f2010-06-18 15:39:12 +08004268 }
Nick Chengcdd3cb12010-07-13 20:03:04 +08004269 msleep(2000);
Nick Cheng36b83de2010-05-17 11:22:42 +08004270 /* write back pci config data */
4271 for (i = 0; i < 64; i++) {
4272 pci_write_config_byte(acb->pdev, i, value[i]);
4273 }
4274 msleep(1000);
4275 return;
4276}
Ching Huang72a7f312017-12-05 09:31:59 +08004277
4278static bool arcmsr_reset_in_progress(struct AdapterControlBlock *acb)
4279{
4280 bool rtn = true;
4281
4282 switch(acb->adapter_type) {
4283 case ACB_ADAPTER_TYPE_A:{
4284 struct MessageUnit_A __iomem *reg = acb->pmuA;
4285 rtn = ((readl(&reg->outbound_msgaddr1) &
4286 ARCMSR_OUTBOUND_MESG1_FIRMWARE_OK) == 0) ? true : false;
4287 }
4288 break;
4289 case ACB_ADAPTER_TYPE_B:{
4290 struct MessageUnit_B *reg = acb->pmuB;
4291 rtn = ((readl(reg->iop2drv_doorbell) &
4292 ARCMSR_MESSAGE_FIRMWARE_OK) == 0) ? true : false;
4293 }
4294 break;
4295 case ACB_ADAPTER_TYPE_C:{
4296 struct MessageUnit_C __iomem *reg = acb->pmuC;
4297 rtn = (readl(&reg->host_diagnostic) & 0x04) ? true : false;
4298 }
4299 break;
4300 case ACB_ADAPTER_TYPE_D:{
4301 struct MessageUnit_D *reg = acb->pmuD;
4302 rtn = ((readl(reg->sample_at_reset) & 0x80) == 0) ?
4303 true : false;
4304 }
4305 break;
Ching Huang23509022017-12-05 09:35:34 +08004306 case ACB_ADAPTER_TYPE_E:{
4307 struct MessageUnit_E __iomem *reg = acb->pmuE;
4308 rtn = (readl(&reg->host_diagnostic_3xxx) &
4309 ARCMSR_ARC188X_RESET_ADAPTER) ? true : false;
4310 }
4311 break;
Ching Huang72a7f312017-12-05 09:31:59 +08004312 }
4313 return rtn;
4314}
4315
Erich Chen1c57e862006-07-12 08:59:32 -07004316static void arcmsr_iop_init(struct AdapterControlBlock *acb)
4317{
Nick Cheng1a4f5502007-09-13 17:26:40 +08004318 uint32_t intmask_org;
Nick Chengcdd3cb12010-07-13 20:03:04 +08004319 /* disable all outbound interrupt */
4320 intmask_org = arcmsr_disable_outbound_ints(acb);
Nick Cheng76d78302008-02-04 23:53:24 -08004321 arcmsr_wait_firmware_ready(acb);
4322 arcmsr_iop_confirm(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08004323 /*start background rebuild*/
4324 arcmsr_start_adapter_bgrb(acb);
4325 /* empty doorbell Qbuffer if door bell ringed */
4326 arcmsr_clear_doorbell_queue_buffer(acb);
Nick Cheng76d78302008-02-04 23:53:24 -08004327 arcmsr_enable_eoi_mode(acb);
Nick Cheng1a4f5502007-09-13 17:26:40 +08004328 /* enable outbound Post Queue,outbound doorbell Interrupt */
4329 arcmsr_enable_outbound_ints(acb, intmask_org);
Erich Chen1c57e862006-07-12 08:59:32 -07004330 acb->acb_flags |= ACB_F_IOP_INITED;
4331}
4332
Nick Cheng36b83de2010-05-17 11:22:42 +08004333static uint8_t arcmsr_iop_reset(struct AdapterControlBlock *acb)
Erich Chen1c57e862006-07-12 08:59:32 -07004334{
Erich Chen1c57e862006-07-12 08:59:32 -07004335 struct CommandControlBlock *ccb;
4336 uint32_t intmask_org;
Nick Cheng36b83de2010-05-17 11:22:42 +08004337 uint8_t rtnval = 0x00;
Erich Chen1c57e862006-07-12 08:59:32 -07004338 int i = 0;
NickCheng97b99122011-01-06 17:32:41 +08004339 unsigned long flags;
4340
Erich Chen1c57e862006-07-12 08:59:32 -07004341 if (atomic_read(&acb->ccboutstandingcount) != 0) {
Erich Chen1c57e862006-07-12 08:59:32 -07004342 /* disable all outbound interrupt */
4343 intmask_org = arcmsr_disable_outbound_ints(acb);
Nick Cheng36b83de2010-05-17 11:22:42 +08004344 /* talk to iop 331 outstanding command aborted */
4345 rtnval = arcmsr_abort_allcmd(acb);
Erich Chen1c57e862006-07-12 08:59:32 -07004346 /* clear all outbound posted Q */
Nick Cheng1a4f5502007-09-13 17:26:40 +08004347 arcmsr_done4abort_postqueue(acb);
Ching Huangd076e4a2017-12-05 09:44:23 +08004348 for (i = 0; i < acb->maxFreeCCB; i++) {
Erich Chen1c57e862006-07-12 08:59:32 -07004349 ccb = acb->pccb_pool[i];
nickcheng(鄭守謙a1f6e022007-06-15 11:43:32 +08004350 if (ccb->startdone == ARCMSR_CCB_START) {
NickCheng97b99122011-01-06 17:32:41 +08004351 scsi_dma_unmap(ccb->pcmd);
4352 ccb->startdone = ARCMSR_CCB_DONE;
4353 ccb->ccb_flags = 0;
4354 spin_lock_irqsave(&acb->ccblist_lock, flags);
4355 list_add_tail(&ccb->list, &acb->ccb_free_list);
4356 spin_unlock_irqrestore(&acb->ccblist_lock, flags);
Erich Chen1c57e862006-07-12 08:59:32 -07004357 }
4358 }
Nick Cheng36b83de2010-05-17 11:22:42 +08004359 atomic_set(&acb->ccboutstandingcount, 0);
Erich Chen1c57e862006-07-12 08:59:32 -07004360 /* enable all outbound interrupt */
4361 arcmsr_enable_outbound_ints(acb, intmask_org);
Nick Cheng36b83de2010-05-17 11:22:42 +08004362 return rtnval;
Erich Chen1c57e862006-07-12 08:59:32 -07004363 }
Nick Cheng36b83de2010-05-17 11:22:42 +08004364 return rtnval;
Erich Chen1c57e862006-07-12 08:59:32 -07004365}
4366
4367static int arcmsr_bus_reset(struct scsi_cmnd *cmd)
4368{
NickCheng97b99122011-01-06 17:32:41 +08004369 struct AdapterControlBlock *acb;
Nick Chengae52e7f2010-06-18 15:39:12 +08004370 int retry_count = 0;
4371 int rtn = FAILED;
Nick Chengae52e7f2010-06-18 15:39:12 +08004372 acb = (struct AdapterControlBlock *) cmd->device->host->hostdata;
Ching Huangc4c1adb2018-03-15 14:33:36 +08004373 if (acb->acb_flags & ACB_F_ADAPTER_REMOVED)
4374 return SUCCESS;
Ching Huang72a7f312017-12-05 09:31:59 +08004375 pr_notice("arcmsr: executing bus reset eh.....num_resets = %d,"
4376 " num_aborts = %d \n", acb->num_resets, acb->num_aborts);
Nick Cheng36b83de2010-05-17 11:22:42 +08004377 acb->num_resets++;
Nick Cheng36b83de2010-05-17 11:22:42 +08004378
Ching Huang72a7f312017-12-05 09:31:59 +08004379 if (acb->acb_flags & ACB_F_BUS_RESET) {
4380 long timeout;
Ching Huang852c3f32017-12-05 10:18:47 +08004381 pr_notice("arcmsr: there is a bus reset eh proceeding...\n");
Ching Huang72a7f312017-12-05 09:31:59 +08004382 timeout = wait_event_timeout(wait_q, (acb->acb_flags
4383 & ACB_F_BUS_RESET) == 0, 220 * HZ);
4384 if (timeout)
4385 return SUCCESS;
4386 }
4387 acb->acb_flags |= ACB_F_BUS_RESET;
4388 if (!arcmsr_iop_reset(acb)) {
4389 arcmsr_hardware_reset(acb);
4390 acb->acb_flags &= ~ACB_F_IOP_INITED;
4391wait_reset_done:
4392 ssleep(ARCMSR_SLEEPTIME);
4393 if (arcmsr_reset_in_progress(acb)) {
4394 if (retry_count > ARCMSR_RETRYCOUNT) {
4395 acb->fw_flag = FW_DEADLOCK;
4396 pr_notice("arcmsr%d: waiting for hw bus reset"
4397 " return, RETRY TERMINATED!!\n",
4398 acb->host->host_no);
4399 return FAILED;
Nick Chengae52e7f2010-06-18 15:39:12 +08004400 }
Ching Huang72a7f312017-12-05 09:31:59 +08004401 retry_count++;
4402 goto wait_reset_done;
Nick Chengcdd3cb12010-07-13 20:03:04 +08004403 }
Ching Huang72a7f312017-12-05 09:31:59 +08004404 arcmsr_iop_init(acb);
4405 atomic_set(&acb->rq_map_token, 16);
4406 atomic_set(&acb->ante_token_value, 16);
4407 acb->fw_flag = FW_NORMAL;
4408 mod_timer(&acb->eternal_timer, jiffies +
4409 msecs_to_jiffies(6 * HZ));
4410 acb->acb_flags &= ~ACB_F_BUS_RESET;
4411 rtn = SUCCESS;
4412 pr_notice("arcmsr: scsi bus reset eh returns with success\n");
4413 } else {
4414 acb->acb_flags &= ~ACB_F_BUS_RESET;
4415 atomic_set(&acb->rq_map_token, 16);
4416 atomic_set(&acb->ante_token_value, 16);
4417 acb->fw_flag = FW_NORMAL;
4418 mod_timer(&acb->eternal_timer, jiffies +
4419 msecs_to_jiffies(6 * HZ));
4420 rtn = SUCCESS;
Nick Chengae52e7f2010-06-18 15:39:12 +08004421 }
4422 return rtn;
Erich Chen1c57e862006-07-12 08:59:32 -07004423}
4424
Nick Chengae52e7f2010-06-18 15:39:12 +08004425static int arcmsr_abort_one_cmd(struct AdapterControlBlock *acb,
Erich Chen1c57e862006-07-12 08:59:32 -07004426 struct CommandControlBlock *ccb)
4427{
Nick Chengae52e7f2010-06-18 15:39:12 +08004428 int rtn;
Nick Chengae52e7f2010-06-18 15:39:12 +08004429 rtn = arcmsr_polling_ccbdone(acb, ccb);
Nick Chengae52e7f2010-06-18 15:39:12 +08004430 return rtn;
Erich Chen1c57e862006-07-12 08:59:32 -07004431}
4432
4433static int arcmsr_abort(struct scsi_cmnd *cmd)
4434{
4435 struct AdapterControlBlock *acb =
4436 (struct AdapterControlBlock *)cmd->device->host->hostdata;
4437 int i = 0;
Nick Chengae52e7f2010-06-18 15:39:12 +08004438 int rtn = FAILED;
Ching Huangcab5aec2014-08-19 14:47:16 +08004439 uint32_t intmask_org;
4440
Ching Huangc4c1adb2018-03-15 14:33:36 +08004441 if (acb->acb_flags & ACB_F_ADAPTER_REMOVED)
4442 return SUCCESS;
Erich Chen1c57e862006-07-12 08:59:32 -07004443 printk(KERN_NOTICE
Ching Huangcab5aec2014-08-19 14:47:16 +08004444 "arcmsr%d: abort device command of scsi id = %d lun = %d\n",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004445 acb->host->host_no, cmd->device->id, (u32)cmd->device->lun);
Nick Chengae52e7f2010-06-18 15:39:12 +08004446 acb->acb_flags |= ACB_F_ABORT;
Erich Chen1c57e862006-07-12 08:59:32 -07004447 acb->num_aborts++;
Erich Chen1c57e862006-07-12 08:59:32 -07004448 /*
4449 ************************************************
4450 ** the all interrupt service routine is locked
4451 ** we need to handle it as soon as possible and exit
4452 ************************************************
4453 */
Ching Huangcab5aec2014-08-19 14:47:16 +08004454 if (!atomic_read(&acb->ccboutstandingcount)) {
4455 acb->acb_flags &= ~ACB_F_ABORT;
Nick Chengae52e7f2010-06-18 15:39:12 +08004456 return rtn;
Ching Huangcab5aec2014-08-19 14:47:16 +08004457 }
Erich Chen1c57e862006-07-12 08:59:32 -07004458
Ching Huangcab5aec2014-08-19 14:47:16 +08004459 intmask_org = arcmsr_disable_outbound_ints(acb);
Ching Huangd076e4a2017-12-05 09:44:23 +08004460 for (i = 0; i < acb->maxFreeCCB; i++) {
Erich Chen1c57e862006-07-12 08:59:32 -07004461 struct CommandControlBlock *ccb = acb->pccb_pool[i];
4462 if (ccb->startdone == ARCMSR_CCB_START && ccb->pcmd == cmd) {
Nick Chengae52e7f2010-06-18 15:39:12 +08004463 ccb->startdone = ARCMSR_CCB_ABORTED;
4464 rtn = arcmsr_abort_one_cmd(acb, ccb);
Erich Chen1c57e862006-07-12 08:59:32 -07004465 break;
4466 }
4467 }
Nick Chengae52e7f2010-06-18 15:39:12 +08004468 acb->acb_flags &= ~ACB_F_ABORT;
Ching Huangcab5aec2014-08-19 14:47:16 +08004469 arcmsr_enable_outbound_ints(acb, intmask_org);
Nick Chengae52e7f2010-06-18 15:39:12 +08004470 return rtn;
Erich Chen1c57e862006-07-12 08:59:32 -07004471}
4472
4473static const char *arcmsr_info(struct Scsi_Host *host)
4474{
4475 struct AdapterControlBlock *acb =
4476 (struct AdapterControlBlock *) host->hostdata;
4477 static char buf[256];
4478 char *type;
4479 int raid6 = 1;
Erich Chen1c57e862006-07-12 08:59:32 -07004480 switch (acb->pdev->device) {
4481 case PCI_DEVICE_ID_ARECA_1110:
Nick Cheng1a4f5502007-09-13 17:26:40 +08004482 case PCI_DEVICE_ID_ARECA_1200:
4483 case PCI_DEVICE_ID_ARECA_1202:
Erich Chen1c57e862006-07-12 08:59:32 -07004484 case PCI_DEVICE_ID_ARECA_1210:
4485 raid6 = 0;
4486 /*FALLTHRU*/
4487 case PCI_DEVICE_ID_ARECA_1120:
4488 case PCI_DEVICE_ID_ARECA_1130:
4489 case PCI_DEVICE_ID_ARECA_1160:
4490 case PCI_DEVICE_ID_ARECA_1170:
Nick Cheng1a4f5502007-09-13 17:26:40 +08004491 case PCI_DEVICE_ID_ARECA_1201:
Ching Huang7e315ff2015-11-25 19:49:33 +08004492 case PCI_DEVICE_ID_ARECA_1203:
Erich Chen1c57e862006-07-12 08:59:32 -07004493 case PCI_DEVICE_ID_ARECA_1220:
4494 case PCI_DEVICE_ID_ARECA_1230:
4495 case PCI_DEVICE_ID_ARECA_1260:
4496 case PCI_DEVICE_ID_ARECA_1270:
4497 case PCI_DEVICE_ID_ARECA_1280:
4498 type = "SATA";
4499 break;
Ching Huang5b374792014-08-19 15:25:22 +08004500 case PCI_DEVICE_ID_ARECA_1214:
Erich Chen1c57e862006-07-12 08:59:32 -07004501 case PCI_DEVICE_ID_ARECA_1380:
4502 case PCI_DEVICE_ID_ARECA_1381:
4503 case PCI_DEVICE_ID_ARECA_1680:
4504 case PCI_DEVICE_ID_ARECA_1681:
Nick Chengcdd3cb12010-07-13 20:03:04 +08004505 case PCI_DEVICE_ID_ARECA_1880:
Ching Huang23509022017-12-05 09:35:34 +08004506 case PCI_DEVICE_ID_ARECA_1884:
Ching Huangaaa64f62014-08-19 15:22:45 +08004507 type = "SAS/SATA";
Erich Chen1c57e862006-07-12 08:59:32 -07004508 break;
4509 default:
Ching Huangaaa64f62014-08-19 15:22:45 +08004510 type = "unknown";
4511 raid6 = 0;
Erich Chen1c57e862006-07-12 08:59:32 -07004512 break;
4513 }
Ching Huangaaa64f62014-08-19 15:22:45 +08004514 sprintf(buf, "Areca %s RAID Controller %s\narcmsr version %s\n",
4515 type, raid6 ? "(RAID6 capable)" : "", ARCMSR_DRIVER_VERSION);
Erich Chen1c57e862006-07-12 08:59:32 -07004516 return buf;
4517}