blob: d04245e379d759c47b9afe255a614440abab794a [file] [log] [blame]
Thomas Gleixner8e8e69d2019-05-29 07:17:59 -07001// SPDX-License-Identifier: GPL-2.0-only
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08002/*
HighPoint Linux Team00f59702007-12-13 16:14:26 -08003 * HighPoint RR3xxx/4xxx controller driver for Linux
linuxa93429c2015-07-31 11:25:55 +08004 * Copyright (C) 2006-2015 HighPoint Technologies, Inc. All Rights Reserved.
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08005 *
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08006 * Please report bugs/comments/suggestions to linux@highpoint-tech.com
7 *
8 * For more information, visit http://www.highpoint-tech.com
9 */
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080010#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/string.h>
13#include <linux/kernel.h>
14#include <linux/pci.h>
15#include <linux/interrupt.h>
16#include <linux/errno.h>
17#include <linux/delay.h>
18#include <linux/timer.h>
19#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h>
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080022#include <asm/io.h>
23#include <asm/div64.h>
24#include <scsi/scsi_cmnd.h>
25#include <scsi/scsi_device.h>
26#include <scsi/scsi.h>
27#include <scsi/scsi_tcq.h>
28#include <scsi/scsi_host.h>
29
30#include "hptiop.h"
31
32MODULE_AUTHOR("HighPoint Technologies, Inc.");
HighPoint Linux Team00f59702007-12-13 16:14:26 -080033MODULE_DESCRIPTION("HighPoint RocketRAID 3xxx/4xxx Controller Driver");
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080034
35static char driver_name[] = "hptiop";
HighPoint Linux Team00f59702007-12-13 16:14:26 -080036static const char driver_name_long[] = "RocketRAID 3xxx/4xxx Controller driver";
linuxa93429c2015-07-31 11:25:55 +080037static const char driver_ver[] = "v1.10.0";
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080038
HighPoint Linux Team00f59702007-12-13 16:14:26 -080039static int iop_send_sync_msg(struct hptiop_hba *hba, u32 msg, u32 millisec);
40static void hptiop_finish_scsi_req(struct hptiop_hba *hba, u32 tag,
41 struct hpt_iop_request_scsi_command *req);
42static void hptiop_host_request_callback_itl(struct hptiop_hba *hba, u32 tag);
43static void hptiop_iop_request_callback_itl(struct hptiop_hba *hba, u32 tag);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080044static void hptiop_message_callback(struct hptiop_hba *hba, u32 msg);
45
HighPoint Linux Team00f59702007-12-13 16:14:26 -080046static int iop_wait_ready_itl(struct hptiop_hba *hba, u32 millisec)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080047{
48 u32 req = 0;
49 int i;
50
51 for (i = 0; i < millisec; i++) {
HighPoint Linux Team00f59702007-12-13 16:14:26 -080052 req = readl(&hba->u.itl.iop->inbound_queue);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080053 if (req != IOPMU_QUEUE_EMPTY)
54 break;
55 msleep(1);
56 }
57
58 if (req != IOPMU_QUEUE_EMPTY) {
HighPoint Linux Team00f59702007-12-13 16:14:26 -080059 writel(req, &hba->u.itl.iop->outbound_queue);
60 readl(&hba->u.itl.iop->outbound_intstatus);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080061 return 0;
62 }
63
64 return -1;
65}
66
HighPoint Linux Team00f59702007-12-13 16:14:26 -080067static int iop_wait_ready_mv(struct hptiop_hba *hba, u32 millisec)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080068{
HighPoint Linux Team00f59702007-12-13 16:14:26 -080069 return iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080070}
71
HighPoint Linux Team286aa032012-10-25 08:41:52 +080072static int iop_wait_ready_mvfrey(struct hptiop_hba *hba, u32 millisec)
73{
74 return iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec);
75}
76
HighPoint Linux Team00f59702007-12-13 16:14:26 -080077static void hptiop_request_callback_itl(struct hptiop_hba *hba, u32 tag)
78{
79 if (tag & IOPMU_QUEUE_ADDR_HOST_BIT)
80 hptiop_host_request_callback_itl(hba,
81 tag & ~IOPMU_QUEUE_ADDR_HOST_BIT);
82 else
83 hptiop_iop_request_callback_itl(hba, tag);
84}
85
86static void hptiop_drain_outbound_queue_itl(struct hptiop_hba *hba)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080087{
88 u32 req;
89
HighPoint Linux Team00f59702007-12-13 16:14:26 -080090 while ((req = readl(&hba->u.itl.iop->outbound_queue)) !=
91 IOPMU_QUEUE_EMPTY) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080092
93 if (req & IOPMU_QUEUE_MASK_HOST_BITS)
HighPoint Linux Team00f59702007-12-13 16:14:26 -080094 hptiop_request_callback_itl(hba, req);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080095 else {
96 struct hpt_iop_request_header __iomem * p;
97
98 p = (struct hpt_iop_request_header __iomem *)
HighPoint Linux Team00f59702007-12-13 16:14:26 -080099 ((char __iomem *)hba->u.itl.iop + req);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800100
101 if (readl(&p->flags) & IOP_REQUEST_FLAG_SYNC_REQUEST) {
102 if (readl(&p->context))
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800103 hptiop_request_callback_itl(hba, req);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800104 else
105 writel(1, &p->context);
106 }
107 else
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800108 hptiop_request_callback_itl(hba, req);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800109 }
110 }
111}
112
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800113static int iop_intr_itl(struct hptiop_hba *hba)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800114{
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800115 struct hpt_iopmu_itl __iomem *iop = hba->u.itl.iop;
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +0800116 void __iomem *plx = hba->u.itl.plx;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800117 u32 status;
118 int ret = 0;
119
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +0800120 if (plx && readl(plx + 0x11C5C) & 0xf)
121 writel(1, plx + 0x11C60);
122
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800123 status = readl(&iop->outbound_intstatus);
124
125 if (status & IOPMU_OUTBOUND_INT_MSG0) {
126 u32 msg = readl(&iop->outbound_msgaddr0);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800127
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800128 dprintk("received outbound msg %x\n", msg);
129 writel(IOPMU_OUTBOUND_INT_MSG0, &iop->outbound_intstatus);
130 hptiop_message_callback(hba, msg);
131 ret = 1;
132 }
133
134 if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) {
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800135 hptiop_drain_outbound_queue_itl(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800136 ret = 1;
137 }
138
139 return ret;
140}
141
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800142static u64 mv_outbound_read(struct hpt_iopmu_mv __iomem *mu)
143{
144 u32 outbound_tail = readl(&mu->outbound_tail);
145 u32 outbound_head = readl(&mu->outbound_head);
146
147 if (outbound_tail != outbound_head) {
148 u64 p;
149
150 memcpy_fromio(&p, &mu->outbound_q[mu->outbound_tail], 8);
151 outbound_tail++;
152
153 if (outbound_tail == MVIOP_QUEUE_LEN)
154 outbound_tail = 0;
155 writel(outbound_tail, &mu->outbound_tail);
156 return p;
157 } else
158 return 0;
159}
160
161static void mv_inbound_write(u64 p, struct hptiop_hba *hba)
162{
163 u32 inbound_head = readl(&hba->u.mv.mu->inbound_head);
164 u32 head = inbound_head + 1;
165
166 if (head == MVIOP_QUEUE_LEN)
167 head = 0;
168
169 memcpy_toio(&hba->u.mv.mu->inbound_q[inbound_head], &p, 8);
170 writel(head, &hba->u.mv.mu->inbound_head);
171 writel(MVIOP_MU_INBOUND_INT_POSTQUEUE,
172 &hba->u.mv.regs->inbound_doorbell);
173}
174
175static void hptiop_request_callback_mv(struct hptiop_hba *hba, u64 tag)
176{
177 u32 req_type = (tag >> 5) & 0x7;
178 struct hpt_iop_request_scsi_command *req;
179
180 dprintk("hptiop_request_callback_mv: tag=%llx\n", tag);
181
182 BUG_ON((tag & MVIOP_MU_QUEUE_REQUEST_RETURN_CONTEXT) == 0);
183
184 switch (req_type) {
185 case IOP_REQUEST_TYPE_GET_CONFIG:
186 case IOP_REQUEST_TYPE_SET_CONFIG:
187 hba->msg_done = 1;
188 break;
189
190 case IOP_REQUEST_TYPE_SCSI_COMMAND:
191 req = hba->reqs[tag >> 8].req_virt;
192 if (likely(tag & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT))
193 req->header.result = cpu_to_le32(IOP_RESULT_SUCCESS);
194
195 hptiop_finish_scsi_req(hba, tag>>8, req);
196 break;
197
198 default:
199 break;
200 }
201}
202
203static int iop_intr_mv(struct hptiop_hba *hba)
204{
205 u32 status;
206 int ret = 0;
207
208 status = readl(&hba->u.mv.regs->outbound_doorbell);
209 writel(~status, &hba->u.mv.regs->outbound_doorbell);
210
211 if (status & MVIOP_MU_OUTBOUND_INT_MSG) {
212 u32 msg;
213 msg = readl(&hba->u.mv.mu->outbound_msg);
214 dprintk("received outbound msg %x\n", msg);
215 hptiop_message_callback(hba, msg);
216 ret = 1;
217 }
218
219 if (status & MVIOP_MU_OUTBOUND_INT_POSTQUEUE) {
220 u64 tag;
221
222 while ((tag = mv_outbound_read(hba->u.mv.mu)))
223 hptiop_request_callback_mv(hba, tag);
224 ret = 1;
225 }
226
227 return ret;
228}
229
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800230static void hptiop_request_callback_mvfrey(struct hptiop_hba *hba, u32 _tag)
231{
232 u32 req_type = _tag & 0xf;
233 struct hpt_iop_request_scsi_command *req;
234
235 switch (req_type) {
236 case IOP_REQUEST_TYPE_GET_CONFIG:
237 case IOP_REQUEST_TYPE_SET_CONFIG:
238 hba->msg_done = 1;
239 break;
240
241 case IOP_REQUEST_TYPE_SCSI_COMMAND:
242 req = hba->reqs[(_tag >> 4) & 0xff].req_virt;
243 if (likely(_tag & IOPMU_QUEUE_REQUEST_RESULT_BIT))
244 req->header.result = IOP_RESULT_SUCCESS;
245 hptiop_finish_scsi_req(hba, (_tag >> 4) & 0xff, req);
246 break;
247
248 default:
249 break;
250 }
251}
252
253static int iop_intr_mvfrey(struct hptiop_hba *hba)
254{
255 u32 _tag, status, cptr, cur_rptr;
256 int ret = 0;
257
258 if (hba->initialized)
259 writel(0, &(hba->u.mvfrey.mu->pcie_f0_int_enable));
260
261 status = readl(&(hba->u.mvfrey.mu->f0_doorbell));
262 if (status) {
263 writel(status, &(hba->u.mvfrey.mu->f0_doorbell));
264 if (status & CPU_TO_F0_DRBL_MSG_BIT) {
265 u32 msg = readl(&(hba->u.mvfrey.mu->cpu_to_f0_msg_a));
266 dprintk("received outbound msg %x\n", msg);
267 hptiop_message_callback(hba, msg);
268 }
269 ret = 1;
270 }
271
272 status = readl(&(hba->u.mvfrey.mu->isr_cause));
273 if (status) {
274 writel(status, &(hba->u.mvfrey.mu->isr_cause));
275 do {
276 cptr = *hba->u.mvfrey.outlist_cptr & 0xff;
277 cur_rptr = hba->u.mvfrey.outlist_rptr;
278 while (cur_rptr != cptr) {
279 cur_rptr++;
280 if (cur_rptr == hba->u.mvfrey.list_count)
281 cur_rptr = 0;
282
283 _tag = hba->u.mvfrey.outlist[cur_rptr].val;
284 BUG_ON(!(_tag & IOPMU_QUEUE_MASK_HOST_BITS));
285 hptiop_request_callback_mvfrey(hba, _tag);
286 ret = 1;
287 }
288 hba->u.mvfrey.outlist_rptr = cur_rptr;
289 } while (cptr != (*hba->u.mvfrey.outlist_cptr & 0xff));
290 }
291
292 if (hba->initialized)
293 writel(0x1010, &(hba->u.mvfrey.mu->pcie_f0_int_enable));
294
295 return ret;
296}
297
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800298static int iop_send_sync_request_itl(struct hptiop_hba *hba,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800299 void __iomem *_req, u32 millisec)
300{
301 struct hpt_iop_request_header __iomem *req = _req;
302 u32 i;
303
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800304 writel(readl(&req->flags) | IOP_REQUEST_FLAG_SYNC_REQUEST, &req->flags);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800305 writel(0, &req->context);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800306 writel((unsigned long)req - (unsigned long)hba->u.itl.iop,
307 &hba->u.itl.iop->inbound_queue);
308 readl(&hba->u.itl.iop->outbound_intstatus);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800309
310 for (i = 0; i < millisec; i++) {
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800311 iop_intr_itl(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800312 if (readl(&req->context))
313 return 0;
314 msleep(1);
315 }
316
317 return -1;
318}
319
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800320static int iop_send_sync_request_mv(struct hptiop_hba *hba,
321 u32 size_bits, u32 millisec)
322{
323 struct hpt_iop_request_header *reqhdr = hba->u.mv.internal_req;
324 u32 i;
325
326 hba->msg_done = 0;
327 reqhdr->flags |= cpu_to_le32(IOP_REQUEST_FLAG_SYNC_REQUEST);
328 mv_inbound_write(hba->u.mv.internal_req_phy |
329 MVIOP_MU_QUEUE_ADDR_HOST_BIT | size_bits, hba);
330
331 for (i = 0; i < millisec; i++) {
332 iop_intr_mv(hba);
333 if (hba->msg_done)
334 return 0;
335 msleep(1);
336 }
337 return -1;
338}
339
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800340static int iop_send_sync_request_mvfrey(struct hptiop_hba *hba,
341 u32 size_bits, u32 millisec)
342{
343 struct hpt_iop_request_header *reqhdr =
344 hba->u.mvfrey.internal_req.req_virt;
345 u32 i;
346
347 hba->msg_done = 0;
348 reqhdr->flags |= cpu_to_le32(IOP_REQUEST_FLAG_SYNC_REQUEST);
349 hba->ops->post_req(hba, &(hba->u.mvfrey.internal_req));
350
351 for (i = 0; i < millisec; i++) {
352 iop_intr_mvfrey(hba);
353 if (hba->msg_done)
354 break;
355 msleep(1);
356 }
357 return hba->msg_done ? 0 : -1;
358}
359
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800360static void hptiop_post_msg_itl(struct hptiop_hba *hba, u32 msg)
361{
362 writel(msg, &hba->u.itl.iop->inbound_msgaddr0);
363 readl(&hba->u.itl.iop->outbound_intstatus);
364}
365
366static void hptiop_post_msg_mv(struct hptiop_hba *hba, u32 msg)
367{
368 writel(msg, &hba->u.mv.mu->inbound_msg);
369 writel(MVIOP_MU_INBOUND_INT_MSG, &hba->u.mv.regs->inbound_doorbell);
370 readl(&hba->u.mv.regs->inbound_doorbell);
371}
372
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800373static void hptiop_post_msg_mvfrey(struct hptiop_hba *hba, u32 msg)
374{
375 writel(msg, &(hba->u.mvfrey.mu->f0_to_cpu_msg_a));
376 readl(&(hba->u.mvfrey.mu->f0_to_cpu_msg_a));
377}
378
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800379static int iop_send_sync_msg(struct hptiop_hba *hba, u32 msg, u32 millisec)
380{
381 u32 i;
382
383 hba->msg_done = 0;
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800384 hba->ops->disable_intr(hba);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800385 hba->ops->post_msg(hba, msg);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800386
387 for (i = 0; i < millisec; i++) {
388 spin_lock_irq(hba->host->host_lock);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800389 hba->ops->iop_intr(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800390 spin_unlock_irq(hba->host->host_lock);
391 if (hba->msg_done)
392 break;
393 msleep(1);
394 }
395
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800396 hba->ops->enable_intr(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800397 return hba->msg_done? 0 : -1;
398}
399
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800400static int iop_get_config_itl(struct hptiop_hba *hba,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800401 struct hpt_iop_request_get_config *config)
402{
403 u32 req32;
404 struct hpt_iop_request_get_config __iomem *req;
405
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800406 req32 = readl(&hba->u.itl.iop->inbound_queue);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800407 if (req32 == IOPMU_QUEUE_EMPTY)
408 return -1;
409
410 req = (struct hpt_iop_request_get_config __iomem *)
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800411 ((unsigned long)hba->u.itl.iop + req32);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800412
413 writel(0, &req->header.flags);
414 writel(IOP_REQUEST_TYPE_GET_CONFIG, &req->header.type);
415 writel(sizeof(struct hpt_iop_request_get_config), &req->header.size);
416 writel(IOP_RESULT_PENDING, &req->header.result);
417
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800418 if (iop_send_sync_request_itl(hba, req, 20000)) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800419 dprintk("Get config send cmd failed\n");
420 return -1;
421 }
422
423 memcpy_fromio(config, req, sizeof(*config));
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800424 writel(req32, &hba->u.itl.iop->outbound_queue);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800425 return 0;
426}
427
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800428static int iop_get_config_mv(struct hptiop_hba *hba,
429 struct hpt_iop_request_get_config *config)
430{
431 struct hpt_iop_request_get_config *req = hba->u.mv.internal_req;
432
433 req->header.flags = cpu_to_le32(IOP_REQUEST_FLAG_OUTPUT_CONTEXT);
434 req->header.type = cpu_to_le32(IOP_REQUEST_TYPE_GET_CONFIG);
435 req->header.size =
436 cpu_to_le32(sizeof(struct hpt_iop_request_get_config));
437 req->header.result = cpu_to_le32(IOP_RESULT_PENDING);
James Bottomleyf6b196a2008-03-30 12:36:26 -0500438 req->header.context = cpu_to_le32(IOP_REQUEST_TYPE_GET_CONFIG<<5);
439 req->header.context_hi32 = 0;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800440
441 if (iop_send_sync_request_mv(hba, 0, 20000)) {
442 dprintk("Get config send cmd failed\n");
443 return -1;
444 }
445
446 memcpy(config, req, sizeof(struct hpt_iop_request_get_config));
447 return 0;
448}
449
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800450static int iop_get_config_mvfrey(struct hptiop_hba *hba,
451 struct hpt_iop_request_get_config *config)
452{
453 struct hpt_iop_request_get_config *info = hba->u.mvfrey.config;
454
455 if (info->header.size != sizeof(struct hpt_iop_request_get_config) ||
456 info->header.type != IOP_REQUEST_TYPE_GET_CONFIG)
457 return -1;
458
459 config->interface_version = info->interface_version;
460 config->firmware_version = info->firmware_version;
461 config->max_requests = info->max_requests;
462 config->request_size = info->request_size;
463 config->max_sg_count = info->max_sg_count;
464 config->data_transfer_length = info->data_transfer_length;
465 config->alignment_mask = info->alignment_mask;
466 config->max_devices = info->max_devices;
467 config->sdram_size = info->sdram_size;
468
469 return 0;
470}
471
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800472static int iop_set_config_itl(struct hptiop_hba *hba,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800473 struct hpt_iop_request_set_config *config)
474{
475 u32 req32;
476 struct hpt_iop_request_set_config __iomem *req;
477
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800478 req32 = readl(&hba->u.itl.iop->inbound_queue);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800479 if (req32 == IOPMU_QUEUE_EMPTY)
480 return -1;
481
482 req = (struct hpt_iop_request_set_config __iomem *)
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800483 ((unsigned long)hba->u.itl.iop + req32);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800484
485 memcpy_toio((u8 __iomem *)req + sizeof(struct hpt_iop_request_header),
486 (u8 *)config + sizeof(struct hpt_iop_request_header),
487 sizeof(struct hpt_iop_request_set_config) -
488 sizeof(struct hpt_iop_request_header));
489
490 writel(0, &req->header.flags);
491 writel(IOP_REQUEST_TYPE_SET_CONFIG, &req->header.type);
492 writel(sizeof(struct hpt_iop_request_set_config), &req->header.size);
493 writel(IOP_RESULT_PENDING, &req->header.result);
494
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800495 if (iop_send_sync_request_itl(hba, req, 20000)) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800496 dprintk("Set config send cmd failed\n");
497 return -1;
498 }
499
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800500 writel(req32, &hba->u.itl.iop->outbound_queue);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800501 return 0;
502}
503
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800504static int iop_set_config_mv(struct hptiop_hba *hba,
505 struct hpt_iop_request_set_config *config)
506{
507 struct hpt_iop_request_set_config *req = hba->u.mv.internal_req;
508
509 memcpy(req, config, sizeof(struct hpt_iop_request_set_config));
510 req->header.flags = cpu_to_le32(IOP_REQUEST_FLAG_OUTPUT_CONTEXT);
511 req->header.type = cpu_to_le32(IOP_REQUEST_TYPE_SET_CONFIG);
512 req->header.size =
513 cpu_to_le32(sizeof(struct hpt_iop_request_set_config));
514 req->header.result = cpu_to_le32(IOP_RESULT_PENDING);
James Bottomleyf6b196a2008-03-30 12:36:26 -0500515 req->header.context = cpu_to_le32(IOP_REQUEST_TYPE_SET_CONFIG<<5);
516 req->header.context_hi32 = 0;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800517
518 if (iop_send_sync_request_mv(hba, 0, 20000)) {
519 dprintk("Set config send cmd failed\n");
520 return -1;
521 }
522
523 return 0;
524}
525
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800526static int iop_set_config_mvfrey(struct hptiop_hba *hba,
527 struct hpt_iop_request_set_config *config)
528{
529 struct hpt_iop_request_set_config *req =
530 hba->u.mvfrey.internal_req.req_virt;
531
532 memcpy(req, config, sizeof(struct hpt_iop_request_set_config));
533 req->header.flags = cpu_to_le32(IOP_REQUEST_FLAG_OUTPUT_CONTEXT);
534 req->header.type = cpu_to_le32(IOP_REQUEST_TYPE_SET_CONFIG);
535 req->header.size =
536 cpu_to_le32(sizeof(struct hpt_iop_request_set_config));
537 req->header.result = cpu_to_le32(IOP_RESULT_PENDING);
538 req->header.context = cpu_to_le32(IOP_REQUEST_TYPE_SET_CONFIG<<5);
539 req->header.context_hi32 = 0;
540
541 if (iop_send_sync_request_mvfrey(hba, 0, 20000)) {
542 dprintk("Set config send cmd failed\n");
543 return -1;
544 }
545
546 return 0;
547}
548
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800549static void hptiop_enable_intr_itl(struct hptiop_hba *hba)
550{
551 writel(~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0),
552 &hba->u.itl.iop->outbound_intmask);
553}
554
555static void hptiop_enable_intr_mv(struct hptiop_hba *hba)
556{
557 writel(MVIOP_MU_OUTBOUND_INT_POSTQUEUE | MVIOP_MU_OUTBOUND_INT_MSG,
558 &hba->u.mv.regs->outbound_intmask);
559}
560
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800561static void hptiop_enable_intr_mvfrey(struct hptiop_hba *hba)
562{
563 writel(CPU_TO_F0_DRBL_MSG_BIT, &(hba->u.mvfrey.mu->f0_doorbell_enable));
564 writel(0x1, &(hba->u.mvfrey.mu->isr_enable));
565 writel(0x1010, &(hba->u.mvfrey.mu->pcie_f0_int_enable));
566}
567
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800568static int hptiop_initialize_iop(struct hptiop_hba *hba)
569{
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800570 /* enable interrupts */
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800571 hba->ops->enable_intr(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800572
573 hba->initialized = 1;
574
575 /* start background tasks */
576 if (iop_send_sync_msg(hba,
577 IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
578 printk(KERN_ERR "scsi%d: fail to start background task\n",
579 hba->host->host_no);
580 return -1;
581 }
582 return 0;
583}
584
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800585static void __iomem *hptiop_map_pci_bar(struct hptiop_hba *hba, int index)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800586{
587 u32 mem_base_phy, length;
588 void __iomem *mem_base_virt;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800589
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800590 struct pci_dev *pcidev = hba->pcidev;
591
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800592
593 if (!(pci_resource_flags(pcidev, index) & IORESOURCE_MEM)) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800594 printk(KERN_ERR "scsi%d: pci resource invalid\n",
595 hba->host->host_no);
Harvey Harrison9bcf0912008-05-22 15:45:07 -0700596 return NULL;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800597 }
598
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800599 mem_base_phy = pci_resource_start(pcidev, index);
600 length = pci_resource_len(pcidev, index);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800601 mem_base_virt = ioremap(mem_base_phy, length);
602
603 if (!mem_base_virt) {
604 printk(KERN_ERR "scsi%d: Fail to ioremap memory space\n",
605 hba->host->host_no);
Harvey Harrison9bcf0912008-05-22 15:45:07 -0700606 return NULL;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800607 }
608 return mem_base_virt;
609}
610
611static int hptiop_map_pci_bar_itl(struct hptiop_hba *hba)
612{
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +0800613 struct pci_dev *pcidev = hba->pcidev;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800614 hba->u.itl.iop = hptiop_map_pci_bar(hba, 0);
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +0800615 if (hba->u.itl.iop == NULL)
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800616 return -1;
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +0800617 if ((pcidev->device & 0xff00) == 0x4400) {
618 hba->u.itl.plx = hba->u.itl.iop;
619 hba->u.itl.iop = hptiop_map_pci_bar(hba, 2);
620 if (hba->u.itl.iop == NULL) {
621 iounmap(hba->u.itl.plx);
622 return -1;
623 }
624 }
625 return 0;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800626}
627
628static void hptiop_unmap_pci_bar_itl(struct hptiop_hba *hba)
629{
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +0800630 if (hba->u.itl.plx)
631 iounmap(hba->u.itl.plx);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800632 iounmap(hba->u.itl.iop);
633}
634
635static int hptiop_map_pci_bar_mv(struct hptiop_hba *hba)
636{
637 hba->u.mv.regs = hptiop_map_pci_bar(hba, 0);
Harvey Harrison9bcf0912008-05-22 15:45:07 -0700638 if (hba->u.mv.regs == NULL)
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800639 return -1;
640
641 hba->u.mv.mu = hptiop_map_pci_bar(hba, 2);
Harvey Harrison9bcf0912008-05-22 15:45:07 -0700642 if (hba->u.mv.mu == NULL) {
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800643 iounmap(hba->u.mv.regs);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800644 return -1;
645 }
646
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800647 return 0;
648}
649
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800650static int hptiop_map_pci_bar_mvfrey(struct hptiop_hba *hba)
651{
652 hba->u.mvfrey.config = hptiop_map_pci_bar(hba, 0);
653 if (hba->u.mvfrey.config == NULL)
654 return -1;
655
656 hba->u.mvfrey.mu = hptiop_map_pci_bar(hba, 2);
657 if (hba->u.mvfrey.mu == NULL) {
658 iounmap(hba->u.mvfrey.config);
659 return -1;
660 }
661
662 return 0;
663}
664
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800665static void hptiop_unmap_pci_bar_mv(struct hptiop_hba *hba)
666{
667 iounmap(hba->u.mv.regs);
668 iounmap(hba->u.mv.mu);
669}
670
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800671static void hptiop_unmap_pci_bar_mvfrey(struct hptiop_hba *hba)
672{
673 iounmap(hba->u.mvfrey.config);
674 iounmap(hba->u.mvfrey.mu);
675}
676
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800677static void hptiop_message_callback(struct hptiop_hba *hba, u32 msg)
678{
679 dprintk("iop message 0x%x\n", msg);
680
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800681 if (msg == IOPMU_INBOUND_MSG0_NOP ||
682 msg == IOPMU_INBOUND_MSG0_RESET_COMM)
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800683 hba->msg_done = 1;
684
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800685 if (!hba->initialized)
686 return;
687
688 if (msg == IOPMU_INBOUND_MSG0_RESET) {
689 atomic_set(&hba->resetting, 0);
690 wake_up(&hba->reset_wq);
691 }
692 else if (msg <= IOPMU_INBOUND_MSG0_MAX)
693 hba->msg_done = 1;
694}
695
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800696static struct hptiop_request *get_req(struct hptiop_hba *hba)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800697{
698 struct hptiop_request *ret;
699
700 dprintk("get_req : req=%p\n", hba->req_list);
701
702 ret = hba->req_list;
703 if (ret)
704 hba->req_list = ret->next;
705
706 return ret;
707}
708
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800709static void free_req(struct hptiop_hba *hba, struct hptiop_request *req)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800710{
711 dprintk("free_req(%d, %p)\n", req->index, req);
712 req->next = hba->req_list;
713 hba->req_list = req;
714}
715
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800716static void hptiop_finish_scsi_req(struct hptiop_hba *hba, u32 tag,
717 struct hpt_iop_request_scsi_command *req)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800718{
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800719 struct scsi_cmnd *scp;
720
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800721 dprintk("hptiop_finish_scsi_req: req=%p, type=%d, "
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800722 "result=%d, context=0x%x tag=%d\n",
723 req, req->header.type, req->header.result,
724 req->header.context, tag);
725
726 BUG_ON(!req->header.result);
727 BUG_ON(req->header.type != cpu_to_le32(IOP_REQUEST_TYPE_SCSI_COMMAND));
728
729 scp = hba->reqs[tag].scp;
730
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900731 if (HPT_SCP(scp)->mapped)
732 scsi_dma_unmap(scp);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800733
734 switch (le32_to_cpu(req->header.result)) {
735 case IOP_RESULT_SUCCESS:
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800736 scsi_set_resid(scp,
737 scsi_bufflen(scp) - le32_to_cpu(req->dataxfer_length));
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800738 scp->result = (DID_OK<<16);
739 break;
740 case IOP_RESULT_BAD_TARGET:
741 scp->result = (DID_BAD_TARGET<<16);
742 break;
743 case IOP_RESULT_BUSY:
744 scp->result = (DID_BUS_BUSY<<16);
745 break;
746 case IOP_RESULT_RESET:
747 scp->result = (DID_RESET<<16);
748 break;
749 case IOP_RESULT_FAIL:
750 scp->result = (DID_ERROR<<16);
751 break;
752 case IOP_RESULT_INVALID_REQUEST:
753 scp->result = (DID_ABORT<<16);
754 break;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800755 case IOP_RESULT_CHECK_CONDITION:
756 scsi_set_resid(scp,
757 scsi_bufflen(scp) - le32_to_cpu(req->dataxfer_length));
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800758 scp->result = SAM_STAT_CHECK_CONDITION;
linuxa93429c2015-07-31 11:25:55 +0800759 memcpy(scp->sense_buffer, &req->sg_list, SCSI_SENSE_BUFFERSIZE);
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800760 goto skip_resid;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800761
762 default:
Hannes Reinecke16576ad2021-04-27 10:30:16 +0200763 scp->result = DID_ABORT << 16;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800764 break;
765 }
766
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800767 scsi_set_resid(scp,
768 scsi_bufflen(scp) - le32_to_cpu(req->dataxfer_length));
769
770skip_resid:
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800771 dprintk("scsi_done(%p)\n", scp);
Bart Van Assche574015a2021-10-07 13:28:32 -0700772 scsi_done(scp);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800773 free_req(hba, &hba->reqs[tag]);
774}
775
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800776static void hptiop_host_request_callback_itl(struct hptiop_hba *hba, u32 _tag)
777{
778 struct hpt_iop_request_scsi_command *req;
779 u32 tag;
780
781 if (hba->iopintf_v2) {
782 tag = _tag & ~IOPMU_QUEUE_REQUEST_RESULT_BIT;
783 req = hba->reqs[tag].req_virt;
784 if (likely(_tag & IOPMU_QUEUE_REQUEST_RESULT_BIT))
785 req->header.result = cpu_to_le32(IOP_RESULT_SUCCESS);
786 } else {
787 tag = _tag;
788 req = hba->reqs[tag].req_virt;
789 }
790
791 hptiop_finish_scsi_req(hba, tag, req);
792}
793
Colin Ian King06d9eb42017-06-22 16:52:19 +0100794static void hptiop_iop_request_callback_itl(struct hptiop_hba *hba, u32 tag)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800795{
796 struct hpt_iop_request_header __iomem *req;
797 struct hpt_iop_request_ioctl_command __iomem *p;
798 struct hpt_ioctl_k *arg;
799
800 req = (struct hpt_iop_request_header __iomem *)
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800801 ((unsigned long)hba->u.itl.iop + tag);
802 dprintk("hptiop_iop_request_callback_itl: req=%p, type=%d, "
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800803 "result=%d, context=0x%x tag=%d\n",
804 req, readl(&req->type), readl(&req->result),
805 readl(&req->context), tag);
806
807 BUG_ON(!readl(&req->result));
808 BUG_ON(readl(&req->type) != IOP_REQUEST_TYPE_IOCTL_COMMAND);
809
810 p = (struct hpt_iop_request_ioctl_command __iomem *)req;
811 arg = (struct hpt_ioctl_k *)(unsigned long)
812 (readl(&req->context) |
813 ((u64)readl(&req->context_hi32)<<32));
814
815 if (readl(&req->result) == IOP_RESULT_SUCCESS) {
816 arg->result = HPT_IOCTL_RESULT_OK;
817
818 if (arg->outbuf_size)
819 memcpy_fromio(arg->outbuf,
820 &p->buf[(readl(&p->inbuf_size) + 3)& ~3],
821 arg->outbuf_size);
822
823 if (arg->bytes_returned)
824 *arg->bytes_returned = arg->outbuf_size;
825 }
826 else
827 arg->result = HPT_IOCTL_RESULT_FAILED;
828
829 arg->done(arg);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800830 writel(tag, &hba->u.itl.iop->outbound_queue);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800831}
832
David Howells7d12e782006-10-05 14:55:46 +0100833static irqreturn_t hptiop_intr(int irq, void *dev_id)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800834{
835 struct hptiop_hba *hba = dev_id;
836 int handled;
837 unsigned long flags;
838
839 spin_lock_irqsave(hba->host->host_lock, flags);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800840 handled = hba->ops->iop_intr(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800841 spin_unlock_irqrestore(hba->host->host_lock, flags);
842
843 return handled;
844}
845
846static int hptiop_buildsgl(struct scsi_cmnd *scp, struct hpt_iopsg *psg)
847{
848 struct Scsi_Host *host = scp->device->host;
849 struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900850 struct scatterlist *sg;
851 int idx, nseg;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800852
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900853 nseg = scsi_dma_map(scp);
854 BUG_ON(nseg < 0);
855 if (!nseg)
856 return 0;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800857
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900858 HPT_SCP(scp)->sgcnt = nseg;
859 HPT_SCP(scp)->mapped = 1;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800860
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900861 BUG_ON(HPT_SCP(scp)->sgcnt > hba->max_sg_descriptors);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800862
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900863 scsi_for_each_sg(scp, sg, HPT_SCP(scp)->sgcnt, idx) {
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800864 psg[idx].pci_address = cpu_to_le64(sg_dma_address(sg)) |
865 hba->ops->host_phy_flag;
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900866 psg[idx].size = cpu_to_le32(sg_dma_len(sg));
867 psg[idx].eot = (idx == HPT_SCP(scp)->sgcnt - 1) ?
868 cpu_to_le32(1) : 0;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800869 }
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900870 return HPT_SCP(scp)->sgcnt;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800871}
872
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800873static void hptiop_post_req_itl(struct hptiop_hba *hba,
874 struct hptiop_request *_req)
875{
876 struct hpt_iop_request_header *reqhdr = _req->req_virt;
877
878 reqhdr->context = cpu_to_le32(IOPMU_QUEUE_ADDR_HOST_BIT |
879 (u32)_req->index);
880 reqhdr->context_hi32 = 0;
881
882 if (hba->iopintf_v2) {
883 u32 size, size_bits;
884
885 size = le32_to_cpu(reqhdr->size);
886 if (size < 256)
887 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT;
888 else if (size < 512)
889 size_bits = IOPMU_QUEUE_ADDR_HOST_BIT;
890 else
891 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT |
892 IOPMU_QUEUE_ADDR_HOST_BIT;
893 writel(_req->req_shifted_phy | size_bits,
894 &hba->u.itl.iop->inbound_queue);
895 } else
896 writel(_req->req_shifted_phy | IOPMU_QUEUE_ADDR_HOST_BIT,
897 &hba->u.itl.iop->inbound_queue);
898}
899
900static void hptiop_post_req_mv(struct hptiop_hba *hba,
901 struct hptiop_request *_req)
902{
903 struct hpt_iop_request_header *reqhdr = _req->req_virt;
904 u32 size, size_bit;
905
906 reqhdr->context = cpu_to_le32(_req->index<<8 |
907 IOP_REQUEST_TYPE_SCSI_COMMAND<<5);
908 reqhdr->context_hi32 = 0;
909 size = le32_to_cpu(reqhdr->size);
910
911 if (size <= 256)
912 size_bit = 0;
913 else if (size <= 256*2)
914 size_bit = 1;
915 else if (size <= 256*3)
916 size_bit = 2;
917 else
918 size_bit = 3;
919
920 mv_inbound_write((_req->req_shifted_phy << 5) |
921 MVIOP_MU_QUEUE_ADDR_HOST_BIT | size_bit, hba);
922}
923
HighPoint Linux Team286aa032012-10-25 08:41:52 +0800924static void hptiop_post_req_mvfrey(struct hptiop_hba *hba,
925 struct hptiop_request *_req)
926{
927 struct hpt_iop_request_header *reqhdr = _req->req_virt;
928 u32 index;
929
930 reqhdr->flags |= cpu_to_le32(IOP_REQUEST_FLAG_OUTPUT_CONTEXT |
931 IOP_REQUEST_FLAG_ADDR_BITS |
932 ((_req->req_shifted_phy >> 11) & 0xffff0000));
933 reqhdr->context = cpu_to_le32(IOPMU_QUEUE_ADDR_HOST_BIT |
934 (_req->index << 4) | reqhdr->type);
935 reqhdr->context_hi32 = cpu_to_le32((_req->req_shifted_phy << 5) &
936 0xffffffff);
937
938 hba->u.mvfrey.inlist_wptr++;
939 index = hba->u.mvfrey.inlist_wptr & 0x3fff;
940
941 if (index == hba->u.mvfrey.list_count) {
942 index = 0;
943 hba->u.mvfrey.inlist_wptr &= ~0x3fff;
944 hba->u.mvfrey.inlist_wptr ^= CL_POINTER_TOGGLE;
945 }
946
947 hba->u.mvfrey.inlist[index].addr =
948 (dma_addr_t)_req->req_shifted_phy << 5;
949 hba->u.mvfrey.inlist[index].intrfc_len = (reqhdr->size + 3) / 4;
950 writel(hba->u.mvfrey.inlist_wptr,
951 &(hba->u.mvfrey.mu->inbound_write_ptr));
952 readl(&(hba->u.mvfrey.mu->inbound_write_ptr));
953}
954
955static int hptiop_reset_comm_itl(struct hptiop_hba *hba)
956{
957 return 0;
958}
959
960static int hptiop_reset_comm_mv(struct hptiop_hba *hba)
961{
962 return 0;
963}
964
965static int hptiop_reset_comm_mvfrey(struct hptiop_hba *hba)
966{
967 u32 list_count = hba->u.mvfrey.list_count;
968
969 if (iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET_COMM, 3000))
970 return -1;
971
972 /* wait 100ms for MCU ready */
973 msleep(100);
974
975 writel(cpu_to_le32(hba->u.mvfrey.inlist_phy & 0xffffffff),
976 &(hba->u.mvfrey.mu->inbound_base));
977 writel(cpu_to_le32((hba->u.mvfrey.inlist_phy >> 16) >> 16),
978 &(hba->u.mvfrey.mu->inbound_base_high));
979
980 writel(cpu_to_le32(hba->u.mvfrey.outlist_phy & 0xffffffff),
981 &(hba->u.mvfrey.mu->outbound_base));
982 writel(cpu_to_le32((hba->u.mvfrey.outlist_phy >> 16) >> 16),
983 &(hba->u.mvfrey.mu->outbound_base_high));
984
985 writel(cpu_to_le32(hba->u.mvfrey.outlist_cptr_phy & 0xffffffff),
986 &(hba->u.mvfrey.mu->outbound_shadow_base));
987 writel(cpu_to_le32((hba->u.mvfrey.outlist_cptr_phy >> 16) >> 16),
988 &(hba->u.mvfrey.mu->outbound_shadow_base_high));
989
990 hba->u.mvfrey.inlist_wptr = (list_count - 1) | CL_POINTER_TOGGLE;
991 *hba->u.mvfrey.outlist_cptr = (list_count - 1) | CL_POINTER_TOGGLE;
992 hba->u.mvfrey.outlist_rptr = list_count - 1;
993 return 0;
994}
995
Bart Van Asscheaf049df2021-10-07 13:46:14 -0700996static int hptiop_queuecommand_lck(struct scsi_cmnd *scp)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800997{
998 struct Scsi_Host *host = scp->device->host;
999 struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
1000 struct hpt_iop_request_scsi_command *req;
1001 int sg_count = 0;
1002 struct hptiop_request *_req;
1003
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001004 _req = get_req(hba);
1005 if (_req == NULL) {
1006 dprintk("hptiop_queuecmd : no free req\n");
HighPoint Linux Team4f2ddba2006-06-14 16:50:57 +08001007 return SCSI_MLQUEUE_HOST_BUSY;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001008 }
1009
1010 _req->scp = scp;
1011
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001012 dprintk("hptiop_queuecmd(scp=%p) %d/%d/%d/%llu cdb=(%08x-%08x-%08x-%08x) "
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001013 "req_index=%d, req=%p\n",
1014 scp,
1015 host->host_no, scp->device->channel,
1016 scp->device->id, scp->device->lun,
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001017 cpu_to_be32(((u32 *)scp->cmnd)[0]),
1018 cpu_to_be32(((u32 *)scp->cmnd)[1]),
1019 cpu_to_be32(((u32 *)scp->cmnd)[2]),
1020 cpu_to_be32(((u32 *)scp->cmnd)[3]),
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001021 _req->index, _req->req_virt);
1022
1023 scp->result = 0;
1024
linuxa93429c2015-07-31 11:25:55 +08001025 if (scp->device->channel ||
1026 (scp->device->id > hba->max_devices) ||
1027 ((scp->device->id == (hba->max_devices-1)) && scp->device->lun)) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001028 scp->result = DID_BAD_TARGET << 16;
1029 free_req(hba, _req);
1030 goto cmd_done;
1031 }
1032
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +08001033 req = _req->req_virt;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001034
1035 /* build S/G table */
FUJITA Tomonorif9875492007-05-14 20:25:31 +09001036 sg_count = hptiop_buildsgl(scp, req->sg_list);
1037 if (!sg_count)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001038 HPT_SCP(scp)->mapped = 0;
1039
1040 req->header.flags = cpu_to_le32(IOP_REQUEST_FLAG_OUTPUT_CONTEXT);
1041 req->header.type = cpu_to_le32(IOP_REQUEST_TYPE_SCSI_COMMAND);
1042 req->header.result = cpu_to_le32(IOP_RESULT_PENDING);
FUJITA Tomonorif9875492007-05-14 20:25:31 +09001043 req->dataxfer_length = cpu_to_le32(scsi_bufflen(scp));
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001044 req->channel = scp->device->channel;
1045 req->target = scp->device->id;
1046 req->lun = scp->device->lun;
1047 req->header.size = cpu_to_le32(
1048 sizeof(struct hpt_iop_request_scsi_command)
1049 - sizeof(struct hpt_iopsg)
1050 + sg_count * sizeof(struct hpt_iopsg));
1051
1052 memcpy(req->cdb, scp->cmnd, sizeof(req->cdb));
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001053 hba->ops->post_req(hba, _req);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001054 return 0;
1055
1056cmd_done:
1057 dprintk("scsi_done(scp=%p)\n", scp);
Bart Van Assche574015a2021-10-07 13:28:32 -07001058 scsi_done(scp);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001059 return 0;
1060}
1061
Jeff Garzikf2812332010-11-16 02:10:29 -05001062static DEF_SCSI_QCMD(hptiop_queuecommand)
1063
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001064static const char *hptiop_info(struct Scsi_Host *host)
1065{
1066 return driver_name_long;
1067}
1068
1069static int hptiop_reset_hba(struct hptiop_hba *hba)
1070{
1071 if (atomic_xchg(&hba->resetting, 1) == 0) {
1072 atomic_inc(&hba->reset_count);
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001073 hba->ops->post_msg(hba, IOPMU_INBOUND_MSG0_RESET);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001074 }
1075
1076 wait_event_timeout(hba->reset_wq,
1077 atomic_read(&hba->resetting) == 0, 60 * HZ);
1078
1079 if (atomic_read(&hba->resetting)) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001080 /* IOP is in unknown state, abort reset */
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001081 printk(KERN_ERR "scsi%d: reset failed\n", hba->host->host_no);
1082 return -1;
1083 }
1084
1085 if (iop_send_sync_msg(hba,
1086 IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
1087 dprintk("scsi%d: fail to start background task\n",
1088 hba->host->host_no);
1089 }
1090
1091 return 0;
1092}
1093
1094static int hptiop_reset(struct scsi_cmnd *scp)
1095{
Hannes Reineckeaceb2942017-08-25 13:57:04 +02001096 struct hptiop_hba * hba = (struct hptiop_hba *)scp->device->host->hostdata;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001097
Hannes Reineckeaceb2942017-08-25 13:57:04 +02001098 printk(KERN_WARNING "hptiop_reset(%d/%d/%d)\n",
1099 scp->device->host->host_no, -1, -1);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001100
1101 return hptiop_reset_hba(hba)? FAILED : SUCCESS;
1102}
1103
1104static int hptiop_adjust_disk_queue_depth(struct scsi_device *sdev,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001105 int queue_depth)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001106{
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001107 struct hptiop_hba *hba = (struct hptiop_hba *)sdev->host->hostdata;
1108
1109 if (queue_depth > hba->max_requests)
1110 queue_depth = hba->max_requests;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001111 return scsi_change_queue_depth(sdev, queue_depth);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001112}
1113
Tony Jonesee959b02008-02-22 00:13:36 +01001114static ssize_t hptiop_show_version(struct device *dev,
1115 struct device_attribute *attr, char *buf)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001116{
1117 return snprintf(buf, PAGE_SIZE, "%s\n", driver_ver);
1118}
1119
Tony Jonesee959b02008-02-22 00:13:36 +01001120static ssize_t hptiop_show_fw_version(struct device *dev,
1121 struct device_attribute *attr, char *buf)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001122{
Tony Jonesee959b02008-02-22 00:13:36 +01001123 struct Scsi_Host *host = class_to_shost(dev);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001124 struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
1125
1126 return snprintf(buf, PAGE_SIZE, "%d.%d.%d.%d\n",
1127 hba->firmware_version >> 24,
1128 (hba->firmware_version >> 16) & 0xff,
1129 (hba->firmware_version >> 8) & 0xff,
1130 hba->firmware_version & 0xff);
1131}
1132
Tony Jonesee959b02008-02-22 00:13:36 +01001133static struct device_attribute hptiop_attr_version = {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001134 .attr = {
1135 .name = "driver-version",
1136 .mode = S_IRUGO,
1137 },
1138 .show = hptiop_show_version,
1139};
1140
Tony Jonesee959b02008-02-22 00:13:36 +01001141static struct device_attribute hptiop_attr_fw_version = {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001142 .attr = {
1143 .name = "firmware-version",
1144 .mode = S_IRUGO,
1145 },
1146 .show = hptiop_show_fw_version,
1147};
1148
Bart Van Asschee8fbc282021-10-12 16:35:34 -07001149static struct attribute *hptiop_host_attrs[] = {
1150 &hptiop_attr_version.attr,
1151 &hptiop_attr_fw_version.attr,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001152 NULL
1153};
1154
Bart Van Asschee8fbc282021-10-12 16:35:34 -07001155ATTRIBUTE_GROUPS(hptiop_host);
1156
linuxa93429c2015-07-31 11:25:55 +08001157static int hptiop_slave_config(struct scsi_device *sdev)
1158{
1159 if (sdev->type == TYPE_TAPE)
1160 blk_queue_max_hw_sectors(sdev->request_queue, 8192);
1161
1162 return 0;
1163}
1164
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001165static struct scsi_host_template driver_template = {
1166 .module = THIS_MODULE,
1167 .name = driver_name,
1168 .queuecommand = hptiop_queuecommand,
Hannes Reineckeaceb2942017-08-25 13:57:04 +02001169 .eh_host_reset_handler = hptiop_reset,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001170 .info = hptiop_info,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001171 .emulated = 0,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001172 .proc_name = driver_name,
Bart Van Asschee8fbc282021-10-12 16:35:34 -07001173 .shost_groups = hptiop_host_groups,
linuxa93429c2015-07-31 11:25:55 +08001174 .slave_configure = hptiop_slave_config,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001175 .this_id = -1,
1176 .change_queue_depth = hptiop_adjust_disk_queue_depth,
1177};
1178
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001179static int hptiop_internal_memalloc_itl(struct hptiop_hba *hba)
1180{
1181 return 0;
1182}
1183
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001184static int hptiop_internal_memalloc_mv(struct hptiop_hba *hba)
1185{
1186 hba->u.mv.internal_req = dma_alloc_coherent(&hba->pcidev->dev,
1187 0x800, &hba->u.mv.internal_req_phy, GFP_KERNEL);
1188 if (hba->u.mv.internal_req)
1189 return 0;
1190 else
1191 return -1;
1192}
1193
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001194static int hptiop_internal_memalloc_mvfrey(struct hptiop_hba *hba)
1195{
1196 u32 list_count = readl(&hba->u.mvfrey.mu->inbound_conf_ctl);
1197 char *p;
1198 dma_addr_t phy;
1199
1200 BUG_ON(hba->max_request_size == 0);
1201
1202 if (list_count == 0) {
1203 BUG_ON(1);
1204 return -1;
1205 }
1206
1207 list_count >>= 16;
1208
1209 hba->u.mvfrey.list_count = list_count;
1210 hba->u.mvfrey.internal_mem_size = 0x800 +
1211 list_count * sizeof(struct mvfrey_inlist_entry) +
1212 list_count * sizeof(struct mvfrey_outlist_entry) +
1213 sizeof(int);
1214
1215 p = dma_alloc_coherent(&hba->pcidev->dev,
1216 hba->u.mvfrey.internal_mem_size, &phy, GFP_KERNEL);
1217 if (!p)
1218 return -1;
1219
1220 hba->u.mvfrey.internal_req.req_virt = p;
1221 hba->u.mvfrey.internal_req.req_shifted_phy = phy >> 5;
1222 hba->u.mvfrey.internal_req.scp = NULL;
1223 hba->u.mvfrey.internal_req.next = NULL;
1224
1225 p += 0x800;
1226 phy += 0x800;
1227
1228 hba->u.mvfrey.inlist = (struct mvfrey_inlist_entry *)p;
1229 hba->u.mvfrey.inlist_phy = phy;
1230
1231 p += list_count * sizeof(struct mvfrey_inlist_entry);
1232 phy += list_count * sizeof(struct mvfrey_inlist_entry);
1233
1234 hba->u.mvfrey.outlist = (struct mvfrey_outlist_entry *)p;
1235 hba->u.mvfrey.outlist_phy = phy;
1236
1237 p += list_count * sizeof(struct mvfrey_outlist_entry);
1238 phy += list_count * sizeof(struct mvfrey_outlist_entry);
1239
1240 hba->u.mvfrey.outlist_cptr = (__le32 *)p;
1241 hba->u.mvfrey.outlist_cptr_phy = phy;
1242
1243 return 0;
1244}
1245
1246static int hptiop_internal_memfree_itl(struct hptiop_hba *hba)
1247{
1248 return 0;
1249}
1250
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001251static int hptiop_internal_memfree_mv(struct hptiop_hba *hba)
1252{
1253 if (hba->u.mv.internal_req) {
1254 dma_free_coherent(&hba->pcidev->dev, 0x800,
1255 hba->u.mv.internal_req, hba->u.mv.internal_req_phy);
1256 return 0;
1257 } else
1258 return -1;
1259}
1260
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001261static int hptiop_internal_memfree_mvfrey(struct hptiop_hba *hba)
1262{
1263 if (hba->u.mvfrey.internal_req.req_virt) {
1264 dma_free_coherent(&hba->pcidev->dev,
1265 hba->u.mvfrey.internal_mem_size,
1266 hba->u.mvfrey.internal_req.req_virt,
1267 (dma_addr_t)
1268 hba->u.mvfrey.internal_req.req_shifted_phy << 5);
1269 return 0;
1270 } else
1271 return -1;
1272}
1273
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001274static int hptiop_probe(struct pci_dev *pcidev, const struct pci_device_id *id)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001275{
1276 struct Scsi_Host *host = NULL;
1277 struct hptiop_hba *hba;
HighPoint Linux Team23f0bb42012-06-14 08:47:07 +01001278 struct hptiop_adapter_ops *iop_ops;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001279 struct hpt_iop_request_get_config iop_config;
1280 struct hpt_iop_request_set_config set_config;
1281 dma_addr_t start_phy;
1282 void *start_virt;
1283 u32 offset, i, req_size;
Hannes Reinecke3e344b62019-02-18 08:34:26 +01001284 int rc;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001285
1286 dprintk("hptiop_probe(%p)\n", pcidev);
1287
1288 if (pci_enable_device(pcidev)) {
1289 printk(KERN_ERR "hptiop: fail to enable pci device\n");
1290 return -ENODEV;
1291 }
1292
1293 printk(KERN_INFO "adapter at PCI %d:%d:%d, IRQ %d\n",
1294 pcidev->bus->number, pcidev->devfn >> 3, pcidev->devfn & 7,
1295 pcidev->irq);
1296
1297 pci_set_master(pcidev);
1298
1299 /* Enable 64bit DMA if possible */
HighPoint Linux Team23f0bb42012-06-14 08:47:07 +01001300 iop_ops = (struct hptiop_adapter_ops *)id->driver_data;
Hannes Reinecke3e344b62019-02-18 08:34:26 +01001301 rc = dma_set_mask(&pcidev->dev,
1302 DMA_BIT_MASK(iop_ops->hw_dma_bit_mask));
1303 if (rc)
1304 rc = dma_set_mask(&pcidev->dev, DMA_BIT_MASK(32));
1305
1306 if (rc) {
Christoph Hellwig453cd372018-10-18 15:10:18 +02001307 printk(KERN_ERR "hptiop: fail to set dma_mask\n");
1308 goto disable_pci_device;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001309 }
1310
1311 if (pci_request_regions(pcidev, driver_name)) {
1312 printk(KERN_ERR "hptiop: pci_request_regions failed\n");
1313 goto disable_pci_device;
1314 }
1315
1316 host = scsi_host_alloc(&driver_template, sizeof(struct hptiop_hba));
1317 if (!host) {
1318 printk(KERN_ERR "hptiop: fail to alloc scsi host\n");
1319 goto free_pci_regions;
1320 }
1321
1322 hba = (struct hptiop_hba *)host->hostdata;
linuxa93429c2015-07-31 11:25:55 +08001323 memset(hba, 0, sizeof(struct hptiop_hba));
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001324
HighPoint Linux Team23f0bb42012-06-14 08:47:07 +01001325 hba->ops = iop_ops;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001326 hba->pcidev = pcidev;
1327 hba->host = host;
1328 hba->initialized = 0;
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +08001329 hba->iopintf_v2 = 0;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001330
1331 atomic_set(&hba->resetting, 0);
1332 atomic_set(&hba->reset_count, 0);
1333
1334 init_waitqueue_head(&hba->reset_wq);
1335 init_waitqueue_head(&hba->ioctl_wq);
1336
linuxa93429c2015-07-31 11:25:55 +08001337 host->max_lun = 128;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001338 host->max_channel = 0;
1339 host->io_port = 0;
1340 host->n_io_port = 0;
1341 host->irq = pcidev->irq;
1342
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001343 if (hba->ops->map_pci_bar(hba))
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001344 goto free_scsi_host;
1345
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001346 if (hba->ops->iop_wait_ready(hba, 20000)) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001347 printk(KERN_ERR "scsi%d: firmware not ready\n",
1348 hba->host->host_no);
1349 goto unmap_pci_bar;
1350 }
1351
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001352 if (hba->ops->family == MV_BASED_IOP) {
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001353 if (hba->ops->internal_memalloc(hba)) {
1354 printk(KERN_ERR "scsi%d: internal_memalloc failed\n",
1355 hba->host->host_no);
1356 goto unmap_pci_bar;
1357 }
1358 }
1359
1360 if (hba->ops->get_config(hba, &iop_config)) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001361 printk(KERN_ERR "scsi%d: get config failed\n",
1362 hba->host->host_no);
1363 goto unmap_pci_bar;
1364 }
1365
1366 hba->max_requests = min(le32_to_cpu(iop_config.max_requests),
1367 HPTIOP_MAX_REQUESTS);
1368 hba->max_devices = le32_to_cpu(iop_config.max_devices);
1369 hba->max_request_size = le32_to_cpu(iop_config.request_size);
1370 hba->max_sg_descriptors = le32_to_cpu(iop_config.max_sg_count);
1371 hba->firmware_version = le32_to_cpu(iop_config.firmware_version);
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +08001372 hba->interface_version = le32_to_cpu(iop_config.interface_version);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001373 hba->sdram_size = le32_to_cpu(iop_config.sdram_size);
1374
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001375 if (hba->ops->family == MVFREY_BASED_IOP) {
1376 if (hba->ops->internal_memalloc(hba)) {
1377 printk(KERN_ERR "scsi%d: internal_memalloc failed\n",
1378 hba->host->host_no);
1379 goto unmap_pci_bar;
1380 }
1381 if (hba->ops->reset_comm(hba)) {
1382 printk(KERN_ERR "scsi%d: reset comm failed\n",
1383 hba->host->host_no);
1384 goto unmap_pci_bar;
1385 }
1386 }
1387
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +08001388 if (hba->firmware_version > 0x01020000 ||
1389 hba->interface_version > 0x01020000)
1390 hba->iopintf_v2 = 1;
1391
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001392 host->max_sectors = le32_to_cpu(iop_config.data_transfer_length) >> 9;
1393 host->max_id = le32_to_cpu(iop_config.max_devices);
1394 host->sg_tablesize = le32_to_cpu(iop_config.max_sg_count);
1395 host->can_queue = le32_to_cpu(iop_config.max_requests);
1396 host->cmd_per_lun = le32_to_cpu(iop_config.max_requests);
1397 host->max_cmd_len = 16;
1398
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +08001399 req_size = sizeof(struct hpt_iop_request_scsi_command)
1400 + sizeof(struct hpt_iopsg) * (hba->max_sg_descriptors - 1);
1401 if ((req_size & 0x1f) != 0)
1402 req_size = (req_size + 0x1f) & ~0x1f;
1403
1404 memset(&set_config, 0, sizeof(struct hpt_iop_request_set_config));
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001405 set_config.iop_id = cpu_to_le32(host->host_no);
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +08001406 set_config.vbus_id = cpu_to_le16(host->host_no);
1407 set_config.max_host_request_size = cpu_to_le16(req_size);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001408
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001409 if (hba->ops->set_config(hba, &set_config)) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001410 printk(KERN_ERR "scsi%d: set config failed\n",
1411 hba->host->host_no);
1412 goto unmap_pci_bar;
1413 }
1414
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001415 pci_set_drvdata(pcidev, host);
1416
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07001417 if (request_irq(pcidev->irq, hptiop_intr, IRQF_SHARED,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001418 driver_name, hba)) {
1419 printk(KERN_ERR "scsi%d: request irq %d failed\n",
1420 hba->host->host_no, pcidev->irq);
Christoph Hellwig3e740512006-07-30 19:13:36 +02001421 goto unmap_pci_bar;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001422 }
1423
1424 /* Allocate request mem */
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001425
1426 dprintk("req_size=%d, max_requests=%d\n", req_size, hba->max_requests);
1427
1428 hba->req_size = req_size;
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001429 hba->req_list = NULL;
linuxa93429c2015-07-31 11:25:55 +08001430
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001431 for (i = 0; i < hba->max_requests; i++) {
linuxa93429c2015-07-31 11:25:55 +08001432 start_virt = dma_alloc_coherent(&pcidev->dev,
1433 hba->req_size + 0x20,
1434 &start_phy, GFP_KERNEL);
1435
1436 if (!start_virt) {
1437 printk(KERN_ERR "scsi%d: fail to alloc request mem\n",
1438 hba->host->host_no);
1439 goto free_request_mem;
1440 }
1441
1442 hba->dma_coherent[i] = start_virt;
1443 hba->dma_coherent_handle[i] = start_phy;
1444
1445 if ((start_phy & 0x1f) != 0) {
1446 offset = ((start_phy + 0x1f) & ~0x1f) - start_phy;
1447 start_phy += offset;
1448 start_virt += offset;
1449 }
1450
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001451 hba->reqs[i].next = NULL;
1452 hba->reqs[i].req_virt = start_virt;
1453 hba->reqs[i].req_shifted_phy = start_phy >> 5;
1454 hba->reqs[i].index = i;
1455 free_req(hba, &hba->reqs[i]);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001456 }
1457
1458 /* Enable Interrupt and start background task */
1459 if (hptiop_initialize_iop(hba))
1460 goto free_request_mem;
1461
Christoph Hellwig3e740512006-07-30 19:13:36 +02001462 if (scsi_add_host(host, &pcidev->dev)) {
1463 printk(KERN_ERR "scsi%d: scsi_add_host failed\n",
1464 hba->host->host_no);
1465 goto free_request_mem;
1466 }
1467
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001468 scsi_scan_host(host);
1469
1470 dprintk("scsi%d: hptiop_probe successfully\n", hba->host->host_no);
1471 return 0;
1472
1473free_request_mem:
linuxa93429c2015-07-31 11:25:55 +08001474 for (i = 0; i < hba->max_requests; i++) {
1475 if (hba->dma_coherent[i] && hba->dma_coherent_handle[i])
1476 dma_free_coherent(&hba->pcidev->dev,
1477 hba->req_size + 0x20,
1478 hba->dma_coherent[i],
1479 hba->dma_coherent_handle[i]);
1480 else
1481 break;
1482 }
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001483
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001484 free_irq(hba->pcidev->irq, hba);
1485
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001486unmap_pci_bar:
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001487 hba->ops->internal_memfree(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001488
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001489 hba->ops->unmap_pci_bar(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001490
1491free_scsi_host:
1492 scsi_host_put(host);
1493
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001494free_pci_regions:
1495 pci_release_regions(pcidev);
1496
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001497disable_pci_device:
1498 pci_disable_device(pcidev);
1499
Julia Lawall1db90ea2010-05-27 14:33:47 +02001500 dprintk("scsi%d: hptiop_probe fail\n", host ? host->host_no : 0);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001501 return -ENODEV;
1502}
1503
1504static void hptiop_shutdown(struct pci_dev *pcidev)
1505{
1506 struct Scsi_Host *host = pci_get_drvdata(pcidev);
1507 struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001508
1509 dprintk("hptiop_shutdown(%p)\n", hba);
1510
1511 /* stop the iop */
1512 if (iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000))
1513 printk(KERN_ERR "scsi%d: shutdown the iop timeout\n",
1514 hba->host->host_no);
1515
1516 /* disable all outbound interrupts */
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001517 hba->ops->disable_intr(hba);
1518}
1519
1520static void hptiop_disable_intr_itl(struct hptiop_hba *hba)
1521{
1522 u32 int_mask;
1523
1524 int_mask = readl(&hba->u.itl.iop->outbound_intmask);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001525 writel(int_mask |
1526 IOPMU_OUTBOUND_INT_MSG0 | IOPMU_OUTBOUND_INT_POSTQUEUE,
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001527 &hba->u.itl.iop->outbound_intmask);
1528 readl(&hba->u.itl.iop->outbound_intmask);
1529}
1530
1531static void hptiop_disable_intr_mv(struct hptiop_hba *hba)
1532{
1533 writel(0, &hba->u.mv.regs->outbound_intmask);
1534 readl(&hba->u.mv.regs->outbound_intmask);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001535}
1536
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001537static void hptiop_disable_intr_mvfrey(struct hptiop_hba *hba)
1538{
1539 writel(0, &(hba->u.mvfrey.mu->f0_doorbell_enable));
1540 readl(&(hba->u.mvfrey.mu->f0_doorbell_enable));
1541 writel(0, &(hba->u.mvfrey.mu->isr_enable));
1542 readl(&(hba->u.mvfrey.mu->isr_enable));
1543 writel(0, &(hba->u.mvfrey.mu->pcie_f0_int_enable));
1544 readl(&(hba->u.mvfrey.mu->pcie_f0_int_enable));
1545}
1546
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001547static void hptiop_remove(struct pci_dev *pcidev)
1548{
1549 struct Scsi_Host *host = pci_get_drvdata(pcidev);
1550 struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
linuxa93429c2015-07-31 11:25:55 +08001551 u32 i;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001552
1553 dprintk("scsi%d: hptiop_remove\n", hba->host->host_no);
1554
HighPoint Linux Team4f2ddba2006-06-14 16:50:57 +08001555 scsi_remove_host(host);
1556
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001557 hptiop_shutdown(pcidev);
1558
1559 free_irq(hba->pcidev->irq, hba);
1560
linuxa93429c2015-07-31 11:25:55 +08001561 for (i = 0; i < hba->max_requests; i++) {
1562 if (hba->dma_coherent[i] && hba->dma_coherent_handle[i])
1563 dma_free_coherent(&hba->pcidev->dev,
1564 hba->req_size + 0x20,
1565 hba->dma_coherent[i],
1566 hba->dma_coherent_handle[i]);
1567 else
1568 break;
1569 }
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001570
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001571 hba->ops->internal_memfree(hba);
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001572
1573 hba->ops->unmap_pci_bar(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001574
1575 pci_release_regions(hba->pcidev);
1576 pci_set_drvdata(hba->pcidev, NULL);
1577 pci_disable_device(hba->pcidev);
1578
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001579 scsi_host_put(host);
1580}
1581
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001582static struct hptiop_adapter_ops hptiop_itl_ops = {
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001583 .family = INTEL_BASED_IOP,
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001584 .iop_wait_ready = iop_wait_ready_itl,
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001585 .internal_memalloc = hptiop_internal_memalloc_itl,
1586 .internal_memfree = hptiop_internal_memfree_itl,
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001587 .map_pci_bar = hptiop_map_pci_bar_itl,
1588 .unmap_pci_bar = hptiop_unmap_pci_bar_itl,
1589 .enable_intr = hptiop_enable_intr_itl,
1590 .disable_intr = hptiop_disable_intr_itl,
1591 .get_config = iop_get_config_itl,
1592 .set_config = iop_set_config_itl,
1593 .iop_intr = iop_intr_itl,
1594 .post_msg = hptiop_post_msg_itl,
1595 .post_req = hptiop_post_req_itl,
HighPoint Linux Team23f0bb42012-06-14 08:47:07 +01001596 .hw_dma_bit_mask = 64,
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001597 .reset_comm = hptiop_reset_comm_itl,
1598 .host_phy_flag = cpu_to_le64(0),
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001599};
1600
1601static struct hptiop_adapter_ops hptiop_mv_ops = {
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001602 .family = MV_BASED_IOP,
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001603 .iop_wait_ready = iop_wait_ready_mv,
1604 .internal_memalloc = hptiop_internal_memalloc_mv,
1605 .internal_memfree = hptiop_internal_memfree_mv,
1606 .map_pci_bar = hptiop_map_pci_bar_mv,
1607 .unmap_pci_bar = hptiop_unmap_pci_bar_mv,
1608 .enable_intr = hptiop_enable_intr_mv,
1609 .disable_intr = hptiop_disable_intr_mv,
1610 .get_config = iop_get_config_mv,
1611 .set_config = iop_set_config_mv,
1612 .iop_intr = iop_intr_mv,
1613 .post_msg = hptiop_post_msg_mv,
1614 .post_req = hptiop_post_req_mv,
HighPoint Linux Team23f0bb42012-06-14 08:47:07 +01001615 .hw_dma_bit_mask = 33,
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001616 .reset_comm = hptiop_reset_comm_mv,
1617 .host_phy_flag = cpu_to_le64(0),
1618};
1619
1620static struct hptiop_adapter_ops hptiop_mvfrey_ops = {
1621 .family = MVFREY_BASED_IOP,
1622 .iop_wait_ready = iop_wait_ready_mvfrey,
1623 .internal_memalloc = hptiop_internal_memalloc_mvfrey,
1624 .internal_memfree = hptiop_internal_memfree_mvfrey,
1625 .map_pci_bar = hptiop_map_pci_bar_mvfrey,
1626 .unmap_pci_bar = hptiop_unmap_pci_bar_mvfrey,
1627 .enable_intr = hptiop_enable_intr_mvfrey,
1628 .disable_intr = hptiop_disable_intr_mvfrey,
1629 .get_config = iop_get_config_mvfrey,
1630 .set_config = iop_set_config_mvfrey,
1631 .iop_intr = iop_intr_mvfrey,
1632 .post_msg = hptiop_post_msg_mvfrey,
1633 .post_req = hptiop_post_req_mvfrey,
1634 .hw_dma_bit_mask = 64,
1635 .reset_comm = hptiop_reset_comm_mvfrey,
1636 .host_phy_flag = cpu_to_le64(1),
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001637};
1638
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001639static struct pci_device_id hptiop_id_table[] = {
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001640 { PCI_VDEVICE(TTI, 0x3220), (kernel_ulong_t)&hptiop_itl_ops },
1641 { PCI_VDEVICE(TTI, 0x3320), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +08001642 { PCI_VDEVICE(TTI, 0x3410), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001643 { PCI_VDEVICE(TTI, 0x3510), (kernel_ulong_t)&hptiop_itl_ops },
1644 { PCI_VDEVICE(TTI, 0x3511), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +08001645 { PCI_VDEVICE(TTI, 0x3520), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001646 { PCI_VDEVICE(TTI, 0x3521), (kernel_ulong_t)&hptiop_itl_ops },
1647 { PCI_VDEVICE(TTI, 0x3522), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Teamdd074282008-07-25 13:29:24 +08001648 { PCI_VDEVICE(TTI, 0x3530), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +08001649 { PCI_VDEVICE(TTI, 0x3540), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Teamdd074282008-07-25 13:29:24 +08001650 { PCI_VDEVICE(TTI, 0x3560), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Teamdd074282008-07-25 13:29:24 +08001651 { PCI_VDEVICE(TTI, 0x4210), (kernel_ulong_t)&hptiop_itl_ops },
1652 { PCI_VDEVICE(TTI, 0x4211), (kernel_ulong_t)&hptiop_itl_ops },
1653 { PCI_VDEVICE(TTI, 0x4310), (kernel_ulong_t)&hptiop_itl_ops },
1654 { PCI_VDEVICE(TTI, 0x4311), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +08001655 { PCI_VDEVICE(TTI, 0x4320), (kernel_ulong_t)&hptiop_itl_ops },
1656 { PCI_VDEVICE(TTI, 0x4321), (kernel_ulong_t)&hptiop_itl_ops },
1657 { PCI_VDEVICE(TTI, 0x4322), (kernel_ulong_t)&hptiop_itl_ops },
1658 { PCI_VDEVICE(TTI, 0x4400), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001659 { PCI_VDEVICE(TTI, 0x3120), (kernel_ulong_t)&hptiop_mv_ops },
1660 { PCI_VDEVICE(TTI, 0x3122), (kernel_ulong_t)&hptiop_mv_ops },
1661 { PCI_VDEVICE(TTI, 0x3020), (kernel_ulong_t)&hptiop_mv_ops },
HighPoint Linux Team286aa032012-10-25 08:41:52 +08001662 { PCI_VDEVICE(TTI, 0x4520), (kernel_ulong_t)&hptiop_mvfrey_ops },
1663 { PCI_VDEVICE(TTI, 0x4522), (kernel_ulong_t)&hptiop_mvfrey_ops },
linuxa93429c2015-07-31 11:25:55 +08001664 { PCI_VDEVICE(TTI, 0x3610), (kernel_ulong_t)&hptiop_mvfrey_ops },
1665 { PCI_VDEVICE(TTI, 0x3611), (kernel_ulong_t)&hptiop_mvfrey_ops },
1666 { PCI_VDEVICE(TTI, 0x3620), (kernel_ulong_t)&hptiop_mvfrey_ops },
1667 { PCI_VDEVICE(TTI, 0x3622), (kernel_ulong_t)&hptiop_mvfrey_ops },
1668 { PCI_VDEVICE(TTI, 0x3640), (kernel_ulong_t)&hptiop_mvfrey_ops },
1669 { PCI_VDEVICE(TTI, 0x3660), (kernel_ulong_t)&hptiop_mvfrey_ops },
1670 { PCI_VDEVICE(TTI, 0x3680), (kernel_ulong_t)&hptiop_mvfrey_ops },
1671 { PCI_VDEVICE(TTI, 0x3690), (kernel_ulong_t)&hptiop_mvfrey_ops },
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001672 {},
1673};
1674
1675MODULE_DEVICE_TABLE(pci, hptiop_id_table);
1676
1677static struct pci_driver hptiop_pci_driver = {
1678 .name = driver_name,
1679 .id_table = hptiop_id_table,
1680 .probe = hptiop_probe,
1681 .remove = hptiop_remove,
1682 .shutdown = hptiop_shutdown,
1683};
1684
1685static int __init hptiop_module_init(void)
1686{
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001687 printk(KERN_INFO "%s %s\n", driver_name_long, driver_ver);
Christoph Hellwig3e740512006-07-30 19:13:36 +02001688 return pci_register_driver(&hptiop_pci_driver);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001689}
1690
1691static void __exit hptiop_module_exit(void)
1692{
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001693 pci_unregister_driver(&hptiop_pci_driver);
1694}
1695
1696
1697module_init(hptiop_module_init);
1698module_exit(hptiop_module_exit);
1699
1700MODULE_LICENSE("GPL");
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +08001701