blob: 901a3daeb36b8b13dc95c65a49b5ce3a48017872 [file] [log] [blame]
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001/*
HighPoint Linux Team00f59702007-12-13 16:14:26 -08002 * HighPoint RR3xxx/4xxx controller driver for Linux
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +08003 * Copyright (C) 2006-2009 HighPoint Technologies, Inc. All Rights Reserved.
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * Please report bugs/comments/suggestions to linux@highpoint-tech.com
15 *
16 * For more information, visit http://www.highpoint-tech.com
17 */
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080018#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/string.h>
21#include <linux/kernel.h>
22#include <linux/pci.h>
23#include <linux/interrupt.h>
24#include <linux/errno.h>
25#include <linux/delay.h>
26#include <linux/timer.h>
27#include <linux/spinlock.h>
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080028#include <asm/uaccess.h>
29#include <asm/io.h>
30#include <asm/div64.h>
31#include <scsi/scsi_cmnd.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/scsi_host.h>
36
37#include "hptiop.h"
38
39MODULE_AUTHOR("HighPoint Technologies, Inc.");
HighPoint Linux Team00f59702007-12-13 16:14:26 -080040MODULE_DESCRIPTION("HighPoint RocketRAID 3xxx/4xxx Controller Driver");
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080041
42static char driver_name[] = "hptiop";
HighPoint Linux Team00f59702007-12-13 16:14:26 -080043static const char driver_name_long[] = "RocketRAID 3xxx/4xxx Controller driver";
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +080044static const char driver_ver[] = "v1.6 (090910)";
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080045
HighPoint Linux Team00f59702007-12-13 16:14:26 -080046static int iop_send_sync_msg(struct hptiop_hba *hba, u32 msg, u32 millisec);
47static void hptiop_finish_scsi_req(struct hptiop_hba *hba, u32 tag,
48 struct hpt_iop_request_scsi_command *req);
49static void hptiop_host_request_callback_itl(struct hptiop_hba *hba, u32 tag);
50static void hptiop_iop_request_callback_itl(struct hptiop_hba *hba, u32 tag);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080051static void hptiop_message_callback(struct hptiop_hba *hba, u32 msg);
52
HighPoint Linux Team00f59702007-12-13 16:14:26 -080053static int iop_wait_ready_itl(struct hptiop_hba *hba, u32 millisec)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080054{
55 u32 req = 0;
56 int i;
57
58 for (i = 0; i < millisec; i++) {
HighPoint Linux Team00f59702007-12-13 16:14:26 -080059 req = readl(&hba->u.itl.iop->inbound_queue);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080060 if (req != IOPMU_QUEUE_EMPTY)
61 break;
62 msleep(1);
63 }
64
65 if (req != IOPMU_QUEUE_EMPTY) {
HighPoint Linux Team00f59702007-12-13 16:14:26 -080066 writel(req, &hba->u.itl.iop->outbound_queue);
67 readl(&hba->u.itl.iop->outbound_intstatus);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080068 return 0;
69 }
70
71 return -1;
72}
73
HighPoint Linux Team00f59702007-12-13 16:14:26 -080074static int iop_wait_ready_mv(struct hptiop_hba *hba, u32 millisec)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080075{
HighPoint Linux Team00f59702007-12-13 16:14:26 -080076 return iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080077}
78
HighPoint Linux Team00f59702007-12-13 16:14:26 -080079static void hptiop_request_callback_itl(struct hptiop_hba *hba, u32 tag)
80{
81 if (tag & IOPMU_QUEUE_ADDR_HOST_BIT)
82 hptiop_host_request_callback_itl(hba,
83 tag & ~IOPMU_QUEUE_ADDR_HOST_BIT);
84 else
85 hptiop_iop_request_callback_itl(hba, tag);
86}
87
88static void hptiop_drain_outbound_queue_itl(struct hptiop_hba *hba)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080089{
90 u32 req;
91
HighPoint Linux Team00f59702007-12-13 16:14:26 -080092 while ((req = readl(&hba->u.itl.iop->outbound_queue)) !=
93 IOPMU_QUEUE_EMPTY) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080094
95 if (req & IOPMU_QUEUE_MASK_HOST_BITS)
HighPoint Linux Team00f59702007-12-13 16:14:26 -080096 hptiop_request_callback_itl(hba, req);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +080097 else {
98 struct hpt_iop_request_header __iomem * p;
99
100 p = (struct hpt_iop_request_header __iomem *)
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800101 ((char __iomem *)hba->u.itl.iop + req);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800102
103 if (readl(&p->flags) & IOP_REQUEST_FLAG_SYNC_REQUEST) {
104 if (readl(&p->context))
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800105 hptiop_request_callback_itl(hba, req);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800106 else
107 writel(1, &p->context);
108 }
109 else
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800110 hptiop_request_callback_itl(hba, req);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800111 }
112 }
113}
114
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800115static int iop_intr_itl(struct hptiop_hba *hba)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800116{
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800117 struct hpt_iopmu_itl __iomem *iop = hba->u.itl.iop;
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +0800118 void __iomem *plx = hba->u.itl.plx;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800119 u32 status;
120 int ret = 0;
121
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +0800122 if (plx && readl(plx + 0x11C5C) & 0xf)
123 writel(1, plx + 0x11C60);
124
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800125 status = readl(&iop->outbound_intstatus);
126
127 if (status & IOPMU_OUTBOUND_INT_MSG0) {
128 u32 msg = readl(&iop->outbound_msgaddr0);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800129
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800130 dprintk("received outbound msg %x\n", msg);
131 writel(IOPMU_OUTBOUND_INT_MSG0, &iop->outbound_intstatus);
132 hptiop_message_callback(hba, msg);
133 ret = 1;
134 }
135
136 if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) {
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800137 hptiop_drain_outbound_queue_itl(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800138 ret = 1;
139 }
140
141 return ret;
142}
143
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800144static u64 mv_outbound_read(struct hpt_iopmu_mv __iomem *mu)
145{
146 u32 outbound_tail = readl(&mu->outbound_tail);
147 u32 outbound_head = readl(&mu->outbound_head);
148
149 if (outbound_tail != outbound_head) {
150 u64 p;
151
152 memcpy_fromio(&p, &mu->outbound_q[mu->outbound_tail], 8);
153 outbound_tail++;
154
155 if (outbound_tail == MVIOP_QUEUE_LEN)
156 outbound_tail = 0;
157 writel(outbound_tail, &mu->outbound_tail);
158 return p;
159 } else
160 return 0;
161}
162
163static void mv_inbound_write(u64 p, struct hptiop_hba *hba)
164{
165 u32 inbound_head = readl(&hba->u.mv.mu->inbound_head);
166 u32 head = inbound_head + 1;
167
168 if (head == MVIOP_QUEUE_LEN)
169 head = 0;
170
171 memcpy_toio(&hba->u.mv.mu->inbound_q[inbound_head], &p, 8);
172 writel(head, &hba->u.mv.mu->inbound_head);
173 writel(MVIOP_MU_INBOUND_INT_POSTQUEUE,
174 &hba->u.mv.regs->inbound_doorbell);
175}
176
177static void hptiop_request_callback_mv(struct hptiop_hba *hba, u64 tag)
178{
179 u32 req_type = (tag >> 5) & 0x7;
180 struct hpt_iop_request_scsi_command *req;
181
182 dprintk("hptiop_request_callback_mv: tag=%llx\n", tag);
183
184 BUG_ON((tag & MVIOP_MU_QUEUE_REQUEST_RETURN_CONTEXT) == 0);
185
186 switch (req_type) {
187 case IOP_REQUEST_TYPE_GET_CONFIG:
188 case IOP_REQUEST_TYPE_SET_CONFIG:
189 hba->msg_done = 1;
190 break;
191
192 case IOP_REQUEST_TYPE_SCSI_COMMAND:
193 req = hba->reqs[tag >> 8].req_virt;
194 if (likely(tag & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT))
195 req->header.result = cpu_to_le32(IOP_RESULT_SUCCESS);
196
197 hptiop_finish_scsi_req(hba, tag>>8, req);
198 break;
199
200 default:
201 break;
202 }
203}
204
205static int iop_intr_mv(struct hptiop_hba *hba)
206{
207 u32 status;
208 int ret = 0;
209
210 status = readl(&hba->u.mv.regs->outbound_doorbell);
211 writel(~status, &hba->u.mv.regs->outbound_doorbell);
212
213 if (status & MVIOP_MU_OUTBOUND_INT_MSG) {
214 u32 msg;
215 msg = readl(&hba->u.mv.mu->outbound_msg);
216 dprintk("received outbound msg %x\n", msg);
217 hptiop_message_callback(hba, msg);
218 ret = 1;
219 }
220
221 if (status & MVIOP_MU_OUTBOUND_INT_POSTQUEUE) {
222 u64 tag;
223
224 while ((tag = mv_outbound_read(hba->u.mv.mu)))
225 hptiop_request_callback_mv(hba, tag);
226 ret = 1;
227 }
228
229 return ret;
230}
231
232static int iop_send_sync_request_itl(struct hptiop_hba *hba,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800233 void __iomem *_req, u32 millisec)
234{
235 struct hpt_iop_request_header __iomem *req = _req;
236 u32 i;
237
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800238 writel(readl(&req->flags) | IOP_REQUEST_FLAG_SYNC_REQUEST, &req->flags);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800239 writel(0, &req->context);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800240 writel((unsigned long)req - (unsigned long)hba->u.itl.iop,
241 &hba->u.itl.iop->inbound_queue);
242 readl(&hba->u.itl.iop->outbound_intstatus);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800243
244 for (i = 0; i < millisec; i++) {
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800245 iop_intr_itl(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800246 if (readl(&req->context))
247 return 0;
248 msleep(1);
249 }
250
251 return -1;
252}
253
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800254static int iop_send_sync_request_mv(struct hptiop_hba *hba,
255 u32 size_bits, u32 millisec)
256{
257 struct hpt_iop_request_header *reqhdr = hba->u.mv.internal_req;
258 u32 i;
259
260 hba->msg_done = 0;
261 reqhdr->flags |= cpu_to_le32(IOP_REQUEST_FLAG_SYNC_REQUEST);
262 mv_inbound_write(hba->u.mv.internal_req_phy |
263 MVIOP_MU_QUEUE_ADDR_HOST_BIT | size_bits, hba);
264
265 for (i = 0; i < millisec; i++) {
266 iop_intr_mv(hba);
267 if (hba->msg_done)
268 return 0;
269 msleep(1);
270 }
271 return -1;
272}
273
274static void hptiop_post_msg_itl(struct hptiop_hba *hba, u32 msg)
275{
276 writel(msg, &hba->u.itl.iop->inbound_msgaddr0);
277 readl(&hba->u.itl.iop->outbound_intstatus);
278}
279
280static void hptiop_post_msg_mv(struct hptiop_hba *hba, u32 msg)
281{
282 writel(msg, &hba->u.mv.mu->inbound_msg);
283 writel(MVIOP_MU_INBOUND_INT_MSG, &hba->u.mv.regs->inbound_doorbell);
284 readl(&hba->u.mv.regs->inbound_doorbell);
285}
286
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800287static int iop_send_sync_msg(struct hptiop_hba *hba, u32 msg, u32 millisec)
288{
289 u32 i;
290
291 hba->msg_done = 0;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800292 hba->ops->post_msg(hba, msg);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800293
294 for (i = 0; i < millisec; i++) {
295 spin_lock_irq(hba->host->host_lock);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800296 hba->ops->iop_intr(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800297 spin_unlock_irq(hba->host->host_lock);
298 if (hba->msg_done)
299 break;
300 msleep(1);
301 }
302
303 return hba->msg_done? 0 : -1;
304}
305
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800306static int iop_get_config_itl(struct hptiop_hba *hba,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800307 struct hpt_iop_request_get_config *config)
308{
309 u32 req32;
310 struct hpt_iop_request_get_config __iomem *req;
311
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800312 req32 = readl(&hba->u.itl.iop->inbound_queue);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800313 if (req32 == IOPMU_QUEUE_EMPTY)
314 return -1;
315
316 req = (struct hpt_iop_request_get_config __iomem *)
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800317 ((unsigned long)hba->u.itl.iop + req32);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800318
319 writel(0, &req->header.flags);
320 writel(IOP_REQUEST_TYPE_GET_CONFIG, &req->header.type);
321 writel(sizeof(struct hpt_iop_request_get_config), &req->header.size);
322 writel(IOP_RESULT_PENDING, &req->header.result);
323
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800324 if (iop_send_sync_request_itl(hba, req, 20000)) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800325 dprintk("Get config send cmd failed\n");
326 return -1;
327 }
328
329 memcpy_fromio(config, req, sizeof(*config));
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800330 writel(req32, &hba->u.itl.iop->outbound_queue);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800331 return 0;
332}
333
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800334static int iop_get_config_mv(struct hptiop_hba *hba,
335 struct hpt_iop_request_get_config *config)
336{
337 struct hpt_iop_request_get_config *req = hba->u.mv.internal_req;
338
339 req->header.flags = cpu_to_le32(IOP_REQUEST_FLAG_OUTPUT_CONTEXT);
340 req->header.type = cpu_to_le32(IOP_REQUEST_TYPE_GET_CONFIG);
341 req->header.size =
342 cpu_to_le32(sizeof(struct hpt_iop_request_get_config));
343 req->header.result = cpu_to_le32(IOP_RESULT_PENDING);
James Bottomleyf6b196a2008-03-30 12:36:26 -0500344 req->header.context = cpu_to_le32(IOP_REQUEST_TYPE_GET_CONFIG<<5);
345 req->header.context_hi32 = 0;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800346
347 if (iop_send_sync_request_mv(hba, 0, 20000)) {
348 dprintk("Get config send cmd failed\n");
349 return -1;
350 }
351
352 memcpy(config, req, sizeof(struct hpt_iop_request_get_config));
353 return 0;
354}
355
356static int iop_set_config_itl(struct hptiop_hba *hba,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800357 struct hpt_iop_request_set_config *config)
358{
359 u32 req32;
360 struct hpt_iop_request_set_config __iomem *req;
361
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800362 req32 = readl(&hba->u.itl.iop->inbound_queue);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800363 if (req32 == IOPMU_QUEUE_EMPTY)
364 return -1;
365
366 req = (struct hpt_iop_request_set_config __iomem *)
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800367 ((unsigned long)hba->u.itl.iop + req32);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800368
369 memcpy_toio((u8 __iomem *)req + sizeof(struct hpt_iop_request_header),
370 (u8 *)config + sizeof(struct hpt_iop_request_header),
371 sizeof(struct hpt_iop_request_set_config) -
372 sizeof(struct hpt_iop_request_header));
373
374 writel(0, &req->header.flags);
375 writel(IOP_REQUEST_TYPE_SET_CONFIG, &req->header.type);
376 writel(sizeof(struct hpt_iop_request_set_config), &req->header.size);
377 writel(IOP_RESULT_PENDING, &req->header.result);
378
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800379 if (iop_send_sync_request_itl(hba, req, 20000)) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800380 dprintk("Set config send cmd failed\n");
381 return -1;
382 }
383
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800384 writel(req32, &hba->u.itl.iop->outbound_queue);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800385 return 0;
386}
387
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800388static int iop_set_config_mv(struct hptiop_hba *hba,
389 struct hpt_iop_request_set_config *config)
390{
391 struct hpt_iop_request_set_config *req = hba->u.mv.internal_req;
392
393 memcpy(req, config, sizeof(struct hpt_iop_request_set_config));
394 req->header.flags = cpu_to_le32(IOP_REQUEST_FLAG_OUTPUT_CONTEXT);
395 req->header.type = cpu_to_le32(IOP_REQUEST_TYPE_SET_CONFIG);
396 req->header.size =
397 cpu_to_le32(sizeof(struct hpt_iop_request_set_config));
398 req->header.result = cpu_to_le32(IOP_RESULT_PENDING);
James Bottomleyf6b196a2008-03-30 12:36:26 -0500399 req->header.context = cpu_to_le32(IOP_REQUEST_TYPE_SET_CONFIG<<5);
400 req->header.context_hi32 = 0;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800401
402 if (iop_send_sync_request_mv(hba, 0, 20000)) {
403 dprintk("Set config send cmd failed\n");
404 return -1;
405 }
406
407 return 0;
408}
409
410static void hptiop_enable_intr_itl(struct hptiop_hba *hba)
411{
412 writel(~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0),
413 &hba->u.itl.iop->outbound_intmask);
414}
415
416static void hptiop_enable_intr_mv(struct hptiop_hba *hba)
417{
418 writel(MVIOP_MU_OUTBOUND_INT_POSTQUEUE | MVIOP_MU_OUTBOUND_INT_MSG,
419 &hba->u.mv.regs->outbound_intmask);
420}
421
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800422static int hptiop_initialize_iop(struct hptiop_hba *hba)
423{
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800424 /* enable interrupts */
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800425 hba->ops->enable_intr(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800426
427 hba->initialized = 1;
428
429 /* start background tasks */
430 if (iop_send_sync_msg(hba,
431 IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
432 printk(KERN_ERR "scsi%d: fail to start background task\n",
433 hba->host->host_no);
434 return -1;
435 }
436 return 0;
437}
438
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800439static void __iomem *hptiop_map_pci_bar(struct hptiop_hba *hba, int index)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800440{
441 u32 mem_base_phy, length;
442 void __iomem *mem_base_virt;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800443
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800444 struct pci_dev *pcidev = hba->pcidev;
445
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800446
447 if (!(pci_resource_flags(pcidev, index) & IORESOURCE_MEM)) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800448 printk(KERN_ERR "scsi%d: pci resource invalid\n",
449 hba->host->host_no);
Harvey Harrison9bcf0912008-05-22 15:45:07 -0700450 return NULL;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800451 }
452
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800453 mem_base_phy = pci_resource_start(pcidev, index);
454 length = pci_resource_len(pcidev, index);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800455 mem_base_virt = ioremap(mem_base_phy, length);
456
457 if (!mem_base_virt) {
458 printk(KERN_ERR "scsi%d: Fail to ioremap memory space\n",
459 hba->host->host_no);
Harvey Harrison9bcf0912008-05-22 15:45:07 -0700460 return NULL;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800461 }
462 return mem_base_virt;
463}
464
465static int hptiop_map_pci_bar_itl(struct hptiop_hba *hba)
466{
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +0800467 struct pci_dev *pcidev = hba->pcidev;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800468 hba->u.itl.iop = hptiop_map_pci_bar(hba, 0);
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +0800469 if (hba->u.itl.iop == NULL)
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800470 return -1;
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +0800471 if ((pcidev->device & 0xff00) == 0x4400) {
472 hba->u.itl.plx = hba->u.itl.iop;
473 hba->u.itl.iop = hptiop_map_pci_bar(hba, 2);
474 if (hba->u.itl.iop == NULL) {
475 iounmap(hba->u.itl.plx);
476 return -1;
477 }
478 }
479 return 0;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800480}
481
482static void hptiop_unmap_pci_bar_itl(struct hptiop_hba *hba)
483{
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +0800484 if (hba->u.itl.plx)
485 iounmap(hba->u.itl.plx);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800486 iounmap(hba->u.itl.iop);
487}
488
489static int hptiop_map_pci_bar_mv(struct hptiop_hba *hba)
490{
491 hba->u.mv.regs = hptiop_map_pci_bar(hba, 0);
Harvey Harrison9bcf0912008-05-22 15:45:07 -0700492 if (hba->u.mv.regs == NULL)
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800493 return -1;
494
495 hba->u.mv.mu = hptiop_map_pci_bar(hba, 2);
Harvey Harrison9bcf0912008-05-22 15:45:07 -0700496 if (hba->u.mv.mu == NULL) {
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800497 iounmap(hba->u.mv.regs);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800498 return -1;
499 }
500
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800501 return 0;
502}
503
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800504static void hptiop_unmap_pci_bar_mv(struct hptiop_hba *hba)
505{
506 iounmap(hba->u.mv.regs);
507 iounmap(hba->u.mv.mu);
508}
509
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800510static void hptiop_message_callback(struct hptiop_hba *hba, u32 msg)
511{
512 dprintk("iop message 0x%x\n", msg);
513
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800514 if (msg == IOPMU_INBOUND_MSG0_NOP)
515 hba->msg_done = 1;
516
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800517 if (!hba->initialized)
518 return;
519
520 if (msg == IOPMU_INBOUND_MSG0_RESET) {
521 atomic_set(&hba->resetting, 0);
522 wake_up(&hba->reset_wq);
523 }
524 else if (msg <= IOPMU_INBOUND_MSG0_MAX)
525 hba->msg_done = 1;
526}
527
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800528static struct hptiop_request *get_req(struct hptiop_hba *hba)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800529{
530 struct hptiop_request *ret;
531
532 dprintk("get_req : req=%p\n", hba->req_list);
533
534 ret = hba->req_list;
535 if (ret)
536 hba->req_list = ret->next;
537
538 return ret;
539}
540
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800541static void free_req(struct hptiop_hba *hba, struct hptiop_request *req)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800542{
543 dprintk("free_req(%d, %p)\n", req->index, req);
544 req->next = hba->req_list;
545 hba->req_list = req;
546}
547
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800548static void hptiop_finish_scsi_req(struct hptiop_hba *hba, u32 tag,
549 struct hpt_iop_request_scsi_command *req)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800550{
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800551 struct scsi_cmnd *scp;
552
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800553 dprintk("hptiop_finish_scsi_req: req=%p, type=%d, "
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800554 "result=%d, context=0x%x tag=%d\n",
555 req, req->header.type, req->header.result,
556 req->header.context, tag);
557
558 BUG_ON(!req->header.result);
559 BUG_ON(req->header.type != cpu_to_le32(IOP_REQUEST_TYPE_SCSI_COMMAND));
560
561 scp = hba->reqs[tag].scp;
562
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900563 if (HPT_SCP(scp)->mapped)
564 scsi_dma_unmap(scp);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800565
566 switch (le32_to_cpu(req->header.result)) {
567 case IOP_RESULT_SUCCESS:
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800568 scsi_set_resid(scp,
569 scsi_bufflen(scp) - le32_to_cpu(req->dataxfer_length));
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800570 scp->result = (DID_OK<<16);
571 break;
572 case IOP_RESULT_BAD_TARGET:
573 scp->result = (DID_BAD_TARGET<<16);
574 break;
575 case IOP_RESULT_BUSY:
576 scp->result = (DID_BUS_BUSY<<16);
577 break;
578 case IOP_RESULT_RESET:
579 scp->result = (DID_RESET<<16);
580 break;
581 case IOP_RESULT_FAIL:
582 scp->result = (DID_ERROR<<16);
583 break;
584 case IOP_RESULT_INVALID_REQUEST:
585 scp->result = (DID_ABORT<<16);
586 break;
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800587 case IOP_RESULT_CHECK_CONDITION:
588 scsi_set_resid(scp,
589 scsi_bufflen(scp) - le32_to_cpu(req->dataxfer_length));
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800590 scp->result = SAM_STAT_CHECK_CONDITION;
FUJITA Tomonoric372f4a2008-01-27 10:22:26 +0900591 memcpy(scp->sense_buffer, &req->sg_list,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900592 min_t(size_t, SCSI_SENSE_BUFFERSIZE,
HighPoint Linux Team0fec02c2007-10-15 14:42:52 +0800593 le32_to_cpu(req->dataxfer_length)));
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800594 break;
595
596 default:
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -0500597 scp->result = DRIVER_INVALID << 24 | DID_ABORT << 16;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800598 break;
599 }
600
601 dprintk("scsi_done(%p)\n", scp);
602 scp->scsi_done(scp);
603 free_req(hba, &hba->reqs[tag]);
604}
605
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800606static void hptiop_host_request_callback_itl(struct hptiop_hba *hba, u32 _tag)
607{
608 struct hpt_iop_request_scsi_command *req;
609 u32 tag;
610
611 if (hba->iopintf_v2) {
612 tag = _tag & ~IOPMU_QUEUE_REQUEST_RESULT_BIT;
613 req = hba->reqs[tag].req_virt;
614 if (likely(_tag & IOPMU_QUEUE_REQUEST_RESULT_BIT))
615 req->header.result = cpu_to_le32(IOP_RESULT_SUCCESS);
616 } else {
617 tag = _tag;
618 req = hba->reqs[tag].req_virt;
619 }
620
621 hptiop_finish_scsi_req(hba, tag, req);
622}
623
624void hptiop_iop_request_callback_itl(struct hptiop_hba *hba, u32 tag)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800625{
626 struct hpt_iop_request_header __iomem *req;
627 struct hpt_iop_request_ioctl_command __iomem *p;
628 struct hpt_ioctl_k *arg;
629
630 req = (struct hpt_iop_request_header __iomem *)
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800631 ((unsigned long)hba->u.itl.iop + tag);
632 dprintk("hptiop_iop_request_callback_itl: req=%p, type=%d, "
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800633 "result=%d, context=0x%x tag=%d\n",
634 req, readl(&req->type), readl(&req->result),
635 readl(&req->context), tag);
636
637 BUG_ON(!readl(&req->result));
638 BUG_ON(readl(&req->type) != IOP_REQUEST_TYPE_IOCTL_COMMAND);
639
640 p = (struct hpt_iop_request_ioctl_command __iomem *)req;
641 arg = (struct hpt_ioctl_k *)(unsigned long)
642 (readl(&req->context) |
643 ((u64)readl(&req->context_hi32)<<32));
644
645 if (readl(&req->result) == IOP_RESULT_SUCCESS) {
646 arg->result = HPT_IOCTL_RESULT_OK;
647
648 if (arg->outbuf_size)
649 memcpy_fromio(arg->outbuf,
650 &p->buf[(readl(&p->inbuf_size) + 3)& ~3],
651 arg->outbuf_size);
652
653 if (arg->bytes_returned)
654 *arg->bytes_returned = arg->outbuf_size;
655 }
656 else
657 arg->result = HPT_IOCTL_RESULT_FAILED;
658
659 arg->done(arg);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800660 writel(tag, &hba->u.itl.iop->outbound_queue);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800661}
662
David Howells7d12e782006-10-05 14:55:46 +0100663static irqreturn_t hptiop_intr(int irq, void *dev_id)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800664{
665 struct hptiop_hba *hba = dev_id;
666 int handled;
667 unsigned long flags;
668
669 spin_lock_irqsave(hba->host->host_lock, flags);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800670 handled = hba->ops->iop_intr(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800671 spin_unlock_irqrestore(hba->host->host_lock, flags);
672
673 return handled;
674}
675
676static int hptiop_buildsgl(struct scsi_cmnd *scp, struct hpt_iopsg *psg)
677{
678 struct Scsi_Host *host = scp->device->host;
679 struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900680 struct scatterlist *sg;
681 int idx, nseg;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800682
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900683 nseg = scsi_dma_map(scp);
684 BUG_ON(nseg < 0);
685 if (!nseg)
686 return 0;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800687
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900688 HPT_SCP(scp)->sgcnt = nseg;
689 HPT_SCP(scp)->mapped = 1;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800690
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900691 BUG_ON(HPT_SCP(scp)->sgcnt > hba->max_sg_descriptors);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800692
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900693 scsi_for_each_sg(scp, sg, HPT_SCP(scp)->sgcnt, idx) {
694 psg[idx].pci_address = cpu_to_le64(sg_dma_address(sg));
695 psg[idx].size = cpu_to_le32(sg_dma_len(sg));
696 psg[idx].eot = (idx == HPT_SCP(scp)->sgcnt - 1) ?
697 cpu_to_le32(1) : 0;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800698 }
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900699 return HPT_SCP(scp)->sgcnt;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800700}
701
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800702static void hptiop_post_req_itl(struct hptiop_hba *hba,
703 struct hptiop_request *_req)
704{
705 struct hpt_iop_request_header *reqhdr = _req->req_virt;
706
707 reqhdr->context = cpu_to_le32(IOPMU_QUEUE_ADDR_HOST_BIT |
708 (u32)_req->index);
709 reqhdr->context_hi32 = 0;
710
711 if (hba->iopintf_v2) {
712 u32 size, size_bits;
713
714 size = le32_to_cpu(reqhdr->size);
715 if (size < 256)
716 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT;
717 else if (size < 512)
718 size_bits = IOPMU_QUEUE_ADDR_HOST_BIT;
719 else
720 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT |
721 IOPMU_QUEUE_ADDR_HOST_BIT;
722 writel(_req->req_shifted_phy | size_bits,
723 &hba->u.itl.iop->inbound_queue);
724 } else
725 writel(_req->req_shifted_phy | IOPMU_QUEUE_ADDR_HOST_BIT,
726 &hba->u.itl.iop->inbound_queue);
727}
728
729static void hptiop_post_req_mv(struct hptiop_hba *hba,
730 struct hptiop_request *_req)
731{
732 struct hpt_iop_request_header *reqhdr = _req->req_virt;
733 u32 size, size_bit;
734
735 reqhdr->context = cpu_to_le32(_req->index<<8 |
736 IOP_REQUEST_TYPE_SCSI_COMMAND<<5);
737 reqhdr->context_hi32 = 0;
738 size = le32_to_cpu(reqhdr->size);
739
740 if (size <= 256)
741 size_bit = 0;
742 else if (size <= 256*2)
743 size_bit = 1;
744 else if (size <= 256*3)
745 size_bit = 2;
746 else
747 size_bit = 3;
748
749 mv_inbound_write((_req->req_shifted_phy << 5) |
750 MVIOP_MU_QUEUE_ADDR_HOST_BIT | size_bit, hba);
751}
752
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800753static int hptiop_queuecommand(struct scsi_cmnd *scp,
754 void (*done)(struct scsi_cmnd *))
755{
756 struct Scsi_Host *host = scp->device->host;
757 struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
758 struct hpt_iop_request_scsi_command *req;
759 int sg_count = 0;
760 struct hptiop_request *_req;
761
762 BUG_ON(!done);
763 scp->scsi_done = done;
764
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800765 _req = get_req(hba);
766 if (_req == NULL) {
767 dprintk("hptiop_queuecmd : no free req\n");
HighPoint Linux Team4f2ddba2006-06-14 16:50:57 +0800768 return SCSI_MLQUEUE_HOST_BUSY;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800769 }
770
771 _req->scp = scp;
772
773 dprintk("hptiop_queuecmd(scp=%p) %d/%d/%d/%d cdb=(%x-%x-%x) "
774 "req_index=%d, req=%p\n",
775 scp,
776 host->host_no, scp->device->channel,
777 scp->device->id, scp->device->lun,
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300778 ((u32 *)scp->cmnd)[0],
779 ((u32 *)scp->cmnd)[1],
780 ((u32 *)scp->cmnd)[2],
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800781 _req->index, _req->req_virt);
782
783 scp->result = 0;
784
785 if (scp->device->channel || scp->device->lun ||
786 scp->device->id > hba->max_devices) {
787 scp->result = DID_BAD_TARGET << 16;
788 free_req(hba, _req);
789 goto cmd_done;
790 }
791
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +0800792 req = _req->req_virt;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800793
794 /* build S/G table */
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900795 sg_count = hptiop_buildsgl(scp, req->sg_list);
796 if (!sg_count)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800797 HPT_SCP(scp)->mapped = 0;
798
799 req->header.flags = cpu_to_le32(IOP_REQUEST_FLAG_OUTPUT_CONTEXT);
800 req->header.type = cpu_to_le32(IOP_REQUEST_TYPE_SCSI_COMMAND);
801 req->header.result = cpu_to_le32(IOP_RESULT_PENDING);
FUJITA Tomonorif9875492007-05-14 20:25:31 +0900802 req->dataxfer_length = cpu_to_le32(scsi_bufflen(scp));
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800803 req->channel = scp->device->channel;
804 req->target = scp->device->id;
805 req->lun = scp->device->lun;
806 req->header.size = cpu_to_le32(
807 sizeof(struct hpt_iop_request_scsi_command)
808 - sizeof(struct hpt_iopsg)
809 + sg_count * sizeof(struct hpt_iopsg));
810
811 memcpy(req->cdb, scp->cmnd, sizeof(req->cdb));
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800812 hba->ops->post_req(hba, _req);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800813 return 0;
814
815cmd_done:
816 dprintk("scsi_done(scp=%p)\n", scp);
817 scp->scsi_done(scp);
818 return 0;
819}
820
821static const char *hptiop_info(struct Scsi_Host *host)
822{
823 return driver_name_long;
824}
825
826static int hptiop_reset_hba(struct hptiop_hba *hba)
827{
828 if (atomic_xchg(&hba->resetting, 1) == 0) {
829 atomic_inc(&hba->reset_count);
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800830 hba->ops->post_msg(hba, IOPMU_INBOUND_MSG0_RESET);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800831 }
832
833 wait_event_timeout(hba->reset_wq,
834 atomic_read(&hba->resetting) == 0, 60 * HZ);
835
836 if (atomic_read(&hba->resetting)) {
837 /* IOP is in unkown state, abort reset */
838 printk(KERN_ERR "scsi%d: reset failed\n", hba->host->host_no);
839 return -1;
840 }
841
842 if (iop_send_sync_msg(hba,
843 IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
844 dprintk("scsi%d: fail to start background task\n",
845 hba->host->host_no);
846 }
847
848 return 0;
849}
850
851static int hptiop_reset(struct scsi_cmnd *scp)
852{
853 struct Scsi_Host * host = scp->device->host;
854 struct hptiop_hba * hba = (struct hptiop_hba *)host->hostdata;
855
856 printk(KERN_WARNING "hptiop_reset(%d/%d/%d) scp=%p\n",
857 scp->device->host->host_no, scp->device->channel,
858 scp->device->id, scp);
859
860 return hptiop_reset_hba(hba)? FAILED : SUCCESS;
861}
862
863static int hptiop_adjust_disk_queue_depth(struct scsi_device *sdev,
Mike Christiee881a172009-10-15 17:46:39 -0700864 int queue_depth, int reason)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800865{
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800866 struct hptiop_hba *hba = (struct hptiop_hba *)sdev->host->hostdata;
867
Mike Christiee881a172009-10-15 17:46:39 -0700868 if (reason != SCSI_QDEPTH_DEFAULT)
869 return -EOPNOTSUPP;
870
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800871 if (queue_depth > hba->max_requests)
872 queue_depth = hba->max_requests;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800873 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
874 return queue_depth;
875}
876
Tony Jonesee959b02008-02-22 00:13:36 +0100877static ssize_t hptiop_show_version(struct device *dev,
878 struct device_attribute *attr, char *buf)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800879{
880 return snprintf(buf, PAGE_SIZE, "%s\n", driver_ver);
881}
882
Tony Jonesee959b02008-02-22 00:13:36 +0100883static ssize_t hptiop_show_fw_version(struct device *dev,
884 struct device_attribute *attr, char *buf)
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800885{
Tony Jonesee959b02008-02-22 00:13:36 +0100886 struct Scsi_Host *host = class_to_shost(dev);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800887 struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
888
889 return snprintf(buf, PAGE_SIZE, "%d.%d.%d.%d\n",
890 hba->firmware_version >> 24,
891 (hba->firmware_version >> 16) & 0xff,
892 (hba->firmware_version >> 8) & 0xff,
893 hba->firmware_version & 0xff);
894}
895
Tony Jonesee959b02008-02-22 00:13:36 +0100896static struct device_attribute hptiop_attr_version = {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800897 .attr = {
898 .name = "driver-version",
899 .mode = S_IRUGO,
900 },
901 .show = hptiop_show_version,
902};
903
Tony Jonesee959b02008-02-22 00:13:36 +0100904static struct device_attribute hptiop_attr_fw_version = {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800905 .attr = {
906 .name = "firmware-version",
907 .mode = S_IRUGO,
908 },
909 .show = hptiop_show_fw_version,
910};
911
Tony Jonesee959b02008-02-22 00:13:36 +0100912static struct device_attribute *hptiop_attrs[] = {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800913 &hptiop_attr_version,
914 &hptiop_attr_fw_version,
915 NULL
916};
917
918static struct scsi_host_template driver_template = {
919 .module = THIS_MODULE,
920 .name = driver_name,
921 .queuecommand = hptiop_queuecommand,
922 .eh_device_reset_handler = hptiop_reset,
923 .eh_bus_reset_handler = hptiop_reset,
924 .info = hptiop_info,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800925 .emulated = 0,
926 .use_clustering = ENABLE_CLUSTERING,
927 .proc_name = driver_name,
928 .shost_attrs = hptiop_attrs,
929 .this_id = -1,
930 .change_queue_depth = hptiop_adjust_disk_queue_depth,
931};
932
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800933static int hptiop_internal_memalloc_mv(struct hptiop_hba *hba)
934{
935 hba->u.mv.internal_req = dma_alloc_coherent(&hba->pcidev->dev,
936 0x800, &hba->u.mv.internal_req_phy, GFP_KERNEL);
937 if (hba->u.mv.internal_req)
938 return 0;
939 else
940 return -1;
941}
942
943static int hptiop_internal_memfree_mv(struct hptiop_hba *hba)
944{
945 if (hba->u.mv.internal_req) {
946 dma_free_coherent(&hba->pcidev->dev, 0x800,
947 hba->u.mv.internal_req, hba->u.mv.internal_req_phy);
948 return 0;
949 } else
950 return -1;
951}
952
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800953static int __devinit hptiop_probe(struct pci_dev *pcidev,
954 const struct pci_device_id *id)
955{
956 struct Scsi_Host *host = NULL;
957 struct hptiop_hba *hba;
958 struct hpt_iop_request_get_config iop_config;
959 struct hpt_iop_request_set_config set_config;
960 dma_addr_t start_phy;
961 void *start_virt;
962 u32 offset, i, req_size;
963
964 dprintk("hptiop_probe(%p)\n", pcidev);
965
966 if (pci_enable_device(pcidev)) {
967 printk(KERN_ERR "hptiop: fail to enable pci device\n");
968 return -ENODEV;
969 }
970
971 printk(KERN_INFO "adapter at PCI %d:%d:%d, IRQ %d\n",
972 pcidev->bus->number, pcidev->devfn >> 3, pcidev->devfn & 7,
973 pcidev->irq);
974
975 pci_set_master(pcidev);
976
977 /* Enable 64bit DMA if possible */
Yang Hongyang6a355282009-04-06 19:01:13 -0700978 if (pci_set_dma_mask(pcidev, DMA_BIT_MASK(64))) {
Yang Hongyang284901a2009-04-06 19:01:15 -0700979 if (pci_set_dma_mask(pcidev, DMA_BIT_MASK(32))) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800980 printk(KERN_ERR "hptiop: fail to set dma_mask\n");
981 goto disable_pci_device;
982 }
983 }
984
985 if (pci_request_regions(pcidev, driver_name)) {
986 printk(KERN_ERR "hptiop: pci_request_regions failed\n");
987 goto disable_pci_device;
988 }
989
990 host = scsi_host_alloc(&driver_template, sizeof(struct hptiop_hba));
991 if (!host) {
992 printk(KERN_ERR "hptiop: fail to alloc scsi host\n");
993 goto free_pci_regions;
994 }
995
996 hba = (struct hptiop_hba *)host->hostdata;
997
HighPoint Linux Team00f59702007-12-13 16:14:26 -0800998 hba->ops = (struct hptiop_adapter_ops *)id->driver_data;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +0800999 hba->pcidev = pcidev;
1000 hba->host = host;
1001 hba->initialized = 0;
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +08001002 hba->iopintf_v2 = 0;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001003
1004 atomic_set(&hba->resetting, 0);
1005 atomic_set(&hba->reset_count, 0);
1006
1007 init_waitqueue_head(&hba->reset_wq);
1008 init_waitqueue_head(&hba->ioctl_wq);
1009
1010 host->max_lun = 1;
1011 host->max_channel = 0;
1012 host->io_port = 0;
1013 host->n_io_port = 0;
1014 host->irq = pcidev->irq;
1015
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001016 if (hba->ops->map_pci_bar(hba))
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001017 goto free_scsi_host;
1018
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001019 if (hba->ops->iop_wait_ready(hba, 20000)) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001020 printk(KERN_ERR "scsi%d: firmware not ready\n",
1021 hba->host->host_no);
1022 goto unmap_pci_bar;
1023 }
1024
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001025 if (hba->ops->internal_memalloc) {
1026 if (hba->ops->internal_memalloc(hba)) {
1027 printk(KERN_ERR "scsi%d: internal_memalloc failed\n",
1028 hba->host->host_no);
1029 goto unmap_pci_bar;
1030 }
1031 }
1032
1033 if (hba->ops->get_config(hba, &iop_config)) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001034 printk(KERN_ERR "scsi%d: get config failed\n",
1035 hba->host->host_no);
1036 goto unmap_pci_bar;
1037 }
1038
1039 hba->max_requests = min(le32_to_cpu(iop_config.max_requests),
1040 HPTIOP_MAX_REQUESTS);
1041 hba->max_devices = le32_to_cpu(iop_config.max_devices);
1042 hba->max_request_size = le32_to_cpu(iop_config.request_size);
1043 hba->max_sg_descriptors = le32_to_cpu(iop_config.max_sg_count);
1044 hba->firmware_version = le32_to_cpu(iop_config.firmware_version);
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +08001045 hba->interface_version = le32_to_cpu(iop_config.interface_version);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001046 hba->sdram_size = le32_to_cpu(iop_config.sdram_size);
1047
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +08001048 if (hba->firmware_version > 0x01020000 ||
1049 hba->interface_version > 0x01020000)
1050 hba->iopintf_v2 = 1;
1051
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001052 host->max_sectors = le32_to_cpu(iop_config.data_transfer_length) >> 9;
1053 host->max_id = le32_to_cpu(iop_config.max_devices);
1054 host->sg_tablesize = le32_to_cpu(iop_config.max_sg_count);
1055 host->can_queue = le32_to_cpu(iop_config.max_requests);
1056 host->cmd_per_lun = le32_to_cpu(iop_config.max_requests);
1057 host->max_cmd_len = 16;
1058
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +08001059 req_size = sizeof(struct hpt_iop_request_scsi_command)
1060 + sizeof(struct hpt_iopsg) * (hba->max_sg_descriptors - 1);
1061 if ((req_size & 0x1f) != 0)
1062 req_size = (req_size + 0x1f) & ~0x1f;
1063
1064 memset(&set_config, 0, sizeof(struct hpt_iop_request_set_config));
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001065 set_config.iop_id = cpu_to_le32(host->host_no);
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +08001066 set_config.vbus_id = cpu_to_le16(host->host_no);
1067 set_config.max_host_request_size = cpu_to_le16(req_size);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001068
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001069 if (hba->ops->set_config(hba, &set_config)) {
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001070 printk(KERN_ERR "scsi%d: set config failed\n",
1071 hba->host->host_no);
1072 goto unmap_pci_bar;
1073 }
1074
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001075 pci_set_drvdata(pcidev, host);
1076
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07001077 if (request_irq(pcidev->irq, hptiop_intr, IRQF_SHARED,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001078 driver_name, hba)) {
1079 printk(KERN_ERR "scsi%d: request irq %d failed\n",
1080 hba->host->host_no, pcidev->irq);
Christoph Hellwig3e740512006-07-30 19:13:36 +02001081 goto unmap_pci_bar;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001082 }
1083
1084 /* Allocate request mem */
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001085
1086 dprintk("req_size=%d, max_requests=%d\n", req_size, hba->max_requests);
1087
1088 hba->req_size = req_size;
1089 start_virt = dma_alloc_coherent(&pcidev->dev,
1090 hba->req_size*hba->max_requests + 0x20,
1091 &start_phy, GFP_KERNEL);
1092
1093 if (!start_virt) {
1094 printk(KERN_ERR "scsi%d: fail to alloc request mem\n",
1095 hba->host->host_no);
1096 goto free_request_irq;
1097 }
1098
1099 hba->dma_coherent = start_virt;
1100 hba->dma_coherent_handle = start_phy;
1101
1102 if ((start_phy & 0x1f) != 0)
1103 {
1104 offset = ((start_phy + 0x1f) & ~0x1f) - start_phy;
1105 start_phy += offset;
1106 start_virt += offset;
1107 }
1108
1109 hba->req_list = start_virt;
1110 for (i = 0; i < hba->max_requests; i++) {
1111 hba->reqs[i].next = NULL;
1112 hba->reqs[i].req_virt = start_virt;
1113 hba->reqs[i].req_shifted_phy = start_phy >> 5;
1114 hba->reqs[i].index = i;
1115 free_req(hba, &hba->reqs[i]);
1116 start_virt = (char *)start_virt + hba->req_size;
1117 start_phy = start_phy + hba->req_size;
1118 }
1119
1120 /* Enable Interrupt and start background task */
1121 if (hptiop_initialize_iop(hba))
1122 goto free_request_mem;
1123
Christoph Hellwig3e740512006-07-30 19:13:36 +02001124 if (scsi_add_host(host, &pcidev->dev)) {
1125 printk(KERN_ERR "scsi%d: scsi_add_host failed\n",
1126 hba->host->host_no);
1127 goto free_request_mem;
1128 }
1129
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001130
1131 scsi_scan_host(host);
1132
1133 dprintk("scsi%d: hptiop_probe successfully\n", hba->host->host_no);
1134 return 0;
1135
1136free_request_mem:
1137 dma_free_coherent(&hba->pcidev->dev,
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001138 hba->req_size * hba->max_requests + 0x20,
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001139 hba->dma_coherent, hba->dma_coherent_handle);
1140
1141free_request_irq:
1142 free_irq(hba->pcidev->irq, hba);
1143
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001144unmap_pci_bar:
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001145 if (hba->ops->internal_memfree)
1146 hba->ops->internal_memfree(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001147
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001148 hba->ops->unmap_pci_bar(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001149
1150free_scsi_host:
1151 scsi_host_put(host);
1152
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001153free_pci_regions:
1154 pci_release_regions(pcidev);
1155
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001156disable_pci_device:
1157 pci_disable_device(pcidev);
1158
1159 dprintk("scsi%d: hptiop_probe fail\n", host->host_no);
1160 return -ENODEV;
1161}
1162
1163static void hptiop_shutdown(struct pci_dev *pcidev)
1164{
1165 struct Scsi_Host *host = pci_get_drvdata(pcidev);
1166 struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001167
1168 dprintk("hptiop_shutdown(%p)\n", hba);
1169
1170 /* stop the iop */
1171 if (iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000))
1172 printk(KERN_ERR "scsi%d: shutdown the iop timeout\n",
1173 hba->host->host_no);
1174
1175 /* disable all outbound interrupts */
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001176 hba->ops->disable_intr(hba);
1177}
1178
1179static void hptiop_disable_intr_itl(struct hptiop_hba *hba)
1180{
1181 u32 int_mask;
1182
1183 int_mask = readl(&hba->u.itl.iop->outbound_intmask);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001184 writel(int_mask |
1185 IOPMU_OUTBOUND_INT_MSG0 | IOPMU_OUTBOUND_INT_POSTQUEUE,
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001186 &hba->u.itl.iop->outbound_intmask);
1187 readl(&hba->u.itl.iop->outbound_intmask);
1188}
1189
1190static void hptiop_disable_intr_mv(struct hptiop_hba *hba)
1191{
1192 writel(0, &hba->u.mv.regs->outbound_intmask);
1193 readl(&hba->u.mv.regs->outbound_intmask);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001194}
1195
1196static void hptiop_remove(struct pci_dev *pcidev)
1197{
1198 struct Scsi_Host *host = pci_get_drvdata(pcidev);
1199 struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
1200
1201 dprintk("scsi%d: hptiop_remove\n", hba->host->host_no);
1202
HighPoint Linux Team4f2ddba2006-06-14 16:50:57 +08001203 scsi_remove_host(host);
1204
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001205 hptiop_shutdown(pcidev);
1206
1207 free_irq(hba->pcidev->irq, hba);
1208
1209 dma_free_coherent(&hba->pcidev->dev,
1210 hba->req_size * hba->max_requests + 0x20,
1211 hba->dma_coherent,
1212 hba->dma_coherent_handle);
1213
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001214 if (hba->ops->internal_memfree)
1215 hba->ops->internal_memfree(hba);
1216
1217 hba->ops->unmap_pci_bar(hba);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001218
1219 pci_release_regions(hba->pcidev);
1220 pci_set_drvdata(hba->pcidev, NULL);
1221 pci_disable_device(hba->pcidev);
1222
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001223 scsi_host_put(host);
1224}
1225
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001226static struct hptiop_adapter_ops hptiop_itl_ops = {
1227 .iop_wait_ready = iop_wait_ready_itl,
Harvey Harrison9bcf0912008-05-22 15:45:07 -07001228 .internal_memalloc = NULL,
1229 .internal_memfree = NULL,
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001230 .map_pci_bar = hptiop_map_pci_bar_itl,
1231 .unmap_pci_bar = hptiop_unmap_pci_bar_itl,
1232 .enable_intr = hptiop_enable_intr_itl,
1233 .disable_intr = hptiop_disable_intr_itl,
1234 .get_config = iop_get_config_itl,
1235 .set_config = iop_set_config_itl,
1236 .iop_intr = iop_intr_itl,
1237 .post_msg = hptiop_post_msg_itl,
1238 .post_req = hptiop_post_req_itl,
1239};
1240
1241static struct hptiop_adapter_ops hptiop_mv_ops = {
1242 .iop_wait_ready = iop_wait_ready_mv,
1243 .internal_memalloc = hptiop_internal_memalloc_mv,
1244 .internal_memfree = hptiop_internal_memfree_mv,
1245 .map_pci_bar = hptiop_map_pci_bar_mv,
1246 .unmap_pci_bar = hptiop_unmap_pci_bar_mv,
1247 .enable_intr = hptiop_enable_intr_mv,
1248 .disable_intr = hptiop_disable_intr_mv,
1249 .get_config = iop_get_config_mv,
1250 .set_config = iop_set_config_mv,
1251 .iop_intr = iop_intr_mv,
1252 .post_msg = hptiop_post_msg_mv,
1253 .post_req = hptiop_post_req_mv,
1254};
1255
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001256static struct pci_device_id hptiop_id_table[] = {
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001257 { PCI_VDEVICE(TTI, 0x3220), (kernel_ulong_t)&hptiop_itl_ops },
1258 { PCI_VDEVICE(TTI, 0x3320), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +08001259 { PCI_VDEVICE(TTI, 0x3410), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001260 { PCI_VDEVICE(TTI, 0x3510), (kernel_ulong_t)&hptiop_itl_ops },
1261 { PCI_VDEVICE(TTI, 0x3511), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +08001262 { PCI_VDEVICE(TTI, 0x3520), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001263 { PCI_VDEVICE(TTI, 0x3521), (kernel_ulong_t)&hptiop_itl_ops },
1264 { PCI_VDEVICE(TTI, 0x3522), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Teamdd074282008-07-25 13:29:24 +08001265 { PCI_VDEVICE(TTI, 0x3530), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +08001266 { PCI_VDEVICE(TTI, 0x3540), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Teamdd074282008-07-25 13:29:24 +08001267 { PCI_VDEVICE(TTI, 0x3560), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Teamdd074282008-07-25 13:29:24 +08001268 { PCI_VDEVICE(TTI, 0x4210), (kernel_ulong_t)&hptiop_itl_ops },
1269 { PCI_VDEVICE(TTI, 0x4211), (kernel_ulong_t)&hptiop_itl_ops },
1270 { PCI_VDEVICE(TTI, 0x4310), (kernel_ulong_t)&hptiop_itl_ops },
1271 { PCI_VDEVICE(TTI, 0x4311), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team3bfc13c2009-09-11 17:21:27 +08001272 { PCI_VDEVICE(TTI, 0x4320), (kernel_ulong_t)&hptiop_itl_ops },
1273 { PCI_VDEVICE(TTI, 0x4321), (kernel_ulong_t)&hptiop_itl_ops },
1274 { PCI_VDEVICE(TTI, 0x4322), (kernel_ulong_t)&hptiop_itl_ops },
1275 { PCI_VDEVICE(TTI, 0x4400), (kernel_ulong_t)&hptiop_itl_ops },
HighPoint Linux Team00f59702007-12-13 16:14:26 -08001276 { PCI_VDEVICE(TTI, 0x3120), (kernel_ulong_t)&hptiop_mv_ops },
1277 { PCI_VDEVICE(TTI, 0x3122), (kernel_ulong_t)&hptiop_mv_ops },
1278 { PCI_VDEVICE(TTI, 0x3020), (kernel_ulong_t)&hptiop_mv_ops },
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001279 {},
1280};
1281
1282MODULE_DEVICE_TABLE(pci, hptiop_id_table);
1283
1284static struct pci_driver hptiop_pci_driver = {
1285 .name = driver_name,
1286 .id_table = hptiop_id_table,
1287 .probe = hptiop_probe,
1288 .remove = hptiop_remove,
1289 .shutdown = hptiop_shutdown,
1290};
1291
1292static int __init hptiop_module_init(void)
1293{
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001294 printk(KERN_INFO "%s %s\n", driver_name_long, driver_ver);
Christoph Hellwig3e740512006-07-30 19:13:36 +02001295 return pci_register_driver(&hptiop_pci_driver);
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001296}
1297
1298static void __exit hptiop_module_exit(void)
1299{
HighPoint Linux Teamede1e6f2006-05-16 14:38:09 +08001300 pci_unregister_driver(&hptiop_pci_driver);
1301}
1302
1303
1304module_init(hptiop_module_init);
1305module_exit(hptiop_module_exit);
1306
1307MODULE_LICENSE("GPL");
HighPoint Linux Teamdb9b6e82007-08-30 18:06:21 +08001308