xref: /openbmc/linux/drivers/scsi/hptiop.c (revision c21b37f6)
1 /*
2  * HighPoint RR3xxx controller driver for Linux
3  * Copyright (C) 2006 HighPoint Technologies, Inc. All Rights Reserved.
4  *
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  */
18 #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>
28 #include <linux/hdreg.h>
29 #include <asm/uaccess.h>
30 #include <asm/io.h>
31 #include <asm/div64.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi.h>
35 #include <scsi/scsi_tcq.h>
36 #include <scsi/scsi_host.h>
37 
38 #include "hptiop.h"
39 
40 MODULE_AUTHOR("HighPoint Technologies, Inc.");
41 MODULE_DESCRIPTION("HighPoint RocketRAID 3xxx SATA Controller Driver");
42 
43 static char driver_name[] = "hptiop";
44 static const char driver_name_long[] = "RocketRAID 3xxx SATA Controller driver";
45 static const char driver_ver[] = "v1.0 (060426)";
46 
47 static void hptiop_host_request_callback(struct hptiop_hba *hba, u32 tag);
48 static void hptiop_iop_request_callback(struct hptiop_hba *hba, u32 tag);
49 static void hptiop_message_callback(struct hptiop_hba *hba, u32 msg);
50 
51 static inline void hptiop_pci_posting_flush(struct hpt_iopmu __iomem *iop)
52 {
53 	readl(&iop->outbound_intstatus);
54 }
55 
56 static int iop_wait_ready(struct hpt_iopmu __iomem *iop, u32 millisec)
57 {
58 	u32 req = 0;
59 	int i;
60 
61 	for (i = 0; i < millisec; i++) {
62 		req = readl(&iop->inbound_queue);
63 		if (req != IOPMU_QUEUE_EMPTY)
64 			break;
65 		msleep(1);
66 	}
67 
68 	if (req != IOPMU_QUEUE_EMPTY) {
69 		writel(req, &iop->outbound_queue);
70 		hptiop_pci_posting_flush(iop);
71 		return 0;
72 	}
73 
74 	return -1;
75 }
76 
77 static void hptiop_request_callback(struct hptiop_hba *hba, u32 tag)
78 {
79 	if ((tag & IOPMU_QUEUE_MASK_HOST_BITS) == IOPMU_QUEUE_ADDR_HOST_BIT)
80 		return hptiop_host_request_callback(hba,
81 				tag & ~IOPMU_QUEUE_ADDR_HOST_BIT);
82 	else
83 		return hptiop_iop_request_callback(hba, tag);
84 }
85 
86 static inline void hptiop_drain_outbound_queue(struct hptiop_hba *hba)
87 {
88 	u32 req;
89 
90 	while ((req = readl(&hba->iop->outbound_queue)) != IOPMU_QUEUE_EMPTY) {
91 
92 		if (req & IOPMU_QUEUE_MASK_HOST_BITS)
93 			hptiop_request_callback(hba, req);
94 		else {
95 			struct hpt_iop_request_header __iomem * p;
96 
97 			p = (struct hpt_iop_request_header __iomem *)
98 				((char __iomem *)hba->iop + req);
99 
100 			if (readl(&p->flags) & IOP_REQUEST_FLAG_SYNC_REQUEST) {
101 				if (readl(&p->context))
102 					hptiop_request_callback(hba, req);
103 				else
104 					writel(1, &p->context);
105 			}
106 			else
107 				hptiop_request_callback(hba, req);
108 		}
109 	}
110 }
111 
112 static int __iop_intr(struct hptiop_hba *hba)
113 {
114 	struct hpt_iopmu __iomem *iop = hba->iop;
115 	u32 status;
116 	int ret = 0;
117 
118 	status = readl(&iop->outbound_intstatus);
119 
120 	if (status & IOPMU_OUTBOUND_INT_MSG0) {
121 		u32 msg = readl(&iop->outbound_msgaddr0);
122 		dprintk("received outbound msg %x\n", msg);
123 		writel(IOPMU_OUTBOUND_INT_MSG0, &iop->outbound_intstatus);
124 		hptiop_message_callback(hba, msg);
125 		ret = 1;
126 	}
127 
128 	if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) {
129 		hptiop_drain_outbound_queue(hba);
130 		ret = 1;
131 	}
132 
133 	return ret;
134 }
135 
136 static int iop_send_sync_request(struct hptiop_hba *hba,
137 					void __iomem *_req, u32 millisec)
138 {
139 	struct hpt_iop_request_header __iomem *req = _req;
140 	u32 i;
141 
142 	writel(readl(&req->flags) | IOP_REQUEST_FLAG_SYNC_REQUEST,
143 			&req->flags);
144 
145 	writel(0, &req->context);
146 
147 	writel((unsigned long)req - (unsigned long)hba->iop,
148 			&hba->iop->inbound_queue);
149 
150 	hptiop_pci_posting_flush(hba->iop);
151 
152 	for (i = 0; i < millisec; i++) {
153 		__iop_intr(hba);
154 		if (readl(&req->context))
155 			return 0;
156 		msleep(1);
157 	}
158 
159 	return -1;
160 }
161 
162 static int iop_send_sync_msg(struct hptiop_hba *hba, u32 msg, u32 millisec)
163 {
164 	u32 i;
165 
166 	hba->msg_done = 0;
167 
168 	writel(msg, &hba->iop->inbound_msgaddr0);
169 
170 	hptiop_pci_posting_flush(hba->iop);
171 
172 	for (i = 0; i < millisec; i++) {
173 		spin_lock_irq(hba->host->host_lock);
174 		__iop_intr(hba);
175 		spin_unlock_irq(hba->host->host_lock);
176 		if (hba->msg_done)
177 			break;
178 		msleep(1);
179 	}
180 
181 	return hba->msg_done? 0 : -1;
182 }
183 
184 static int iop_get_config(struct hptiop_hba *hba,
185 				struct hpt_iop_request_get_config *config)
186 {
187 	u32 req32;
188 	struct hpt_iop_request_get_config __iomem *req;
189 
190 	req32 = readl(&hba->iop->inbound_queue);
191 	if (req32 == IOPMU_QUEUE_EMPTY)
192 		return -1;
193 
194 	req = (struct hpt_iop_request_get_config __iomem *)
195 			((unsigned long)hba->iop + req32);
196 
197 	writel(0, &req->header.flags);
198 	writel(IOP_REQUEST_TYPE_GET_CONFIG, &req->header.type);
199 	writel(sizeof(struct hpt_iop_request_get_config), &req->header.size);
200 	writel(IOP_RESULT_PENDING, &req->header.result);
201 
202 	if (iop_send_sync_request(hba, req, 20000)) {
203 		dprintk("Get config send cmd failed\n");
204 		return -1;
205 	}
206 
207 	memcpy_fromio(config, req, sizeof(*config));
208 	writel(req32, &hba->iop->outbound_queue);
209 	return 0;
210 }
211 
212 static int iop_set_config(struct hptiop_hba *hba,
213 				struct hpt_iop_request_set_config *config)
214 {
215 	u32 req32;
216 	struct hpt_iop_request_set_config __iomem *req;
217 
218 	req32 = readl(&hba->iop->inbound_queue);
219 	if (req32 == IOPMU_QUEUE_EMPTY)
220 		return -1;
221 
222 	req = (struct hpt_iop_request_set_config __iomem *)
223 			((unsigned long)hba->iop + req32);
224 
225 	memcpy_toio((u8 __iomem *)req + sizeof(struct hpt_iop_request_header),
226 		(u8 *)config + sizeof(struct hpt_iop_request_header),
227 		sizeof(struct hpt_iop_request_set_config) -
228 			sizeof(struct hpt_iop_request_header));
229 
230 	writel(0, &req->header.flags);
231 	writel(IOP_REQUEST_TYPE_SET_CONFIG, &req->header.type);
232 	writel(sizeof(struct hpt_iop_request_set_config), &req->header.size);
233 	writel(IOP_RESULT_PENDING, &req->header.result);
234 
235 	if (iop_send_sync_request(hba, req, 20000)) {
236 		dprintk("Set config send cmd failed\n");
237 		return -1;
238 	}
239 
240 	writel(req32, &hba->iop->outbound_queue);
241 	return 0;
242 }
243 
244 static int hptiop_initialize_iop(struct hptiop_hba *hba)
245 {
246 	struct hpt_iopmu __iomem *iop = hba->iop;
247 
248 	/* enable interrupts */
249 	writel(~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0),
250 			&iop->outbound_intmask);
251 
252 	hba->initialized = 1;
253 
254 	/* start background tasks */
255 	if (iop_send_sync_msg(hba,
256 			IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
257 		printk(KERN_ERR "scsi%d: fail to start background task\n",
258 			hba->host->host_no);
259 		return -1;
260 	}
261 	return 0;
262 }
263 
264 static int hptiop_map_pci_bar(struct hptiop_hba *hba)
265 {
266 	u32 mem_base_phy, length;
267 	void __iomem *mem_base_virt;
268 	struct pci_dev *pcidev = hba->pcidev;
269 
270 	if (!(pci_resource_flags(pcidev, 0) & IORESOURCE_MEM)) {
271 		printk(KERN_ERR "scsi%d: pci resource invalid\n",
272 				hba->host->host_no);
273 		return -1;
274 	}
275 
276 	mem_base_phy = pci_resource_start(pcidev, 0);
277 	length = pci_resource_len(pcidev, 0);
278 	mem_base_virt = ioremap(mem_base_phy, length);
279 
280 	if (!mem_base_virt) {
281 		printk(KERN_ERR "scsi%d: Fail to ioremap memory space\n",
282 				hba->host->host_no);
283 		return -1;
284 	}
285 
286 	hba->iop = mem_base_virt;
287 	dprintk("hptiop_map_pci_bar: iop=%p\n", hba->iop);
288 	return 0;
289 }
290 
291 static void hptiop_message_callback(struct hptiop_hba *hba, u32 msg)
292 {
293 	dprintk("iop message 0x%x\n", msg);
294 
295 	if (!hba->initialized)
296 		return;
297 
298 	if (msg == IOPMU_INBOUND_MSG0_RESET) {
299 		atomic_set(&hba->resetting, 0);
300 		wake_up(&hba->reset_wq);
301 	}
302 	else if (msg <= IOPMU_INBOUND_MSG0_MAX)
303 		hba->msg_done = 1;
304 }
305 
306 static inline struct hptiop_request *get_req(struct hptiop_hba *hba)
307 {
308 	struct hptiop_request *ret;
309 
310 	dprintk("get_req : req=%p\n", hba->req_list);
311 
312 	ret = hba->req_list;
313 	if (ret)
314 		hba->req_list = ret->next;
315 
316 	return ret;
317 }
318 
319 static inline void free_req(struct hptiop_hba *hba, struct hptiop_request *req)
320 {
321 	dprintk("free_req(%d, %p)\n", req->index, req);
322 	req->next = hba->req_list;
323 	hba->req_list = req;
324 }
325 
326 static void hptiop_host_request_callback(struct hptiop_hba *hba, u32 tag)
327 {
328 	struct hpt_iop_request_scsi_command *req;
329 	struct scsi_cmnd *scp;
330 
331 	req = (struct hpt_iop_request_scsi_command *)hba->reqs[tag].req_virt;
332 	dprintk("hptiop_host_request_callback: req=%p, type=%d, "
333 			"result=%d, context=0x%x tag=%d\n",
334 			req, req->header.type, req->header.result,
335 			req->header.context, tag);
336 
337 	BUG_ON(!req->header.result);
338 	BUG_ON(req->header.type != cpu_to_le32(IOP_REQUEST_TYPE_SCSI_COMMAND));
339 
340 	scp = hba->reqs[tag].scp;
341 
342 	if (HPT_SCP(scp)->mapped)
343 		scsi_dma_unmap(scp);
344 
345 	switch (le32_to_cpu(req->header.result)) {
346 	case IOP_RESULT_SUCCESS:
347 		scp->result = (DID_OK<<16);
348 		break;
349 	case IOP_RESULT_BAD_TARGET:
350 		scp->result = (DID_BAD_TARGET<<16);
351 		break;
352 	case IOP_RESULT_BUSY:
353 		scp->result = (DID_BUS_BUSY<<16);
354 		break;
355 	case IOP_RESULT_RESET:
356 		scp->result = (DID_RESET<<16);
357 		break;
358 	case IOP_RESULT_FAIL:
359 		scp->result = (DID_ERROR<<16);
360 		break;
361 	case IOP_RESULT_INVALID_REQUEST:
362 		scp->result = (DID_ABORT<<16);
363 		break;
364 	case IOP_RESULT_MODE_SENSE_CHECK_CONDITION:
365 		scp->result = SAM_STAT_CHECK_CONDITION;
366 		memset(&scp->sense_buffer,
367 				0, sizeof(scp->sense_buffer));
368 		memcpy(&scp->sense_buffer,
369 			&req->sg_list, le32_to_cpu(req->dataxfer_length));
370 		break;
371 
372 	default:
373 		scp->result = ((DRIVER_INVALID|SUGGEST_ABORT)<<24) |
374 					(DID_ABORT<<16);
375 		break;
376 	}
377 
378 	dprintk("scsi_done(%p)\n", scp);
379 	scp->scsi_done(scp);
380 	free_req(hba, &hba->reqs[tag]);
381 }
382 
383 void hptiop_iop_request_callback(struct hptiop_hba *hba, u32 tag)
384 {
385 	struct hpt_iop_request_header __iomem *req;
386 	struct hpt_iop_request_ioctl_command __iomem *p;
387 	struct hpt_ioctl_k *arg;
388 
389 	req = (struct hpt_iop_request_header __iomem *)
390 			((unsigned long)hba->iop + tag);
391 	dprintk("hptiop_iop_request_callback: req=%p, type=%d, "
392 			"result=%d, context=0x%x tag=%d\n",
393 			req, readl(&req->type), readl(&req->result),
394 			readl(&req->context), tag);
395 
396 	BUG_ON(!readl(&req->result));
397 	BUG_ON(readl(&req->type) != IOP_REQUEST_TYPE_IOCTL_COMMAND);
398 
399 	p = (struct hpt_iop_request_ioctl_command __iomem *)req;
400 	arg = (struct hpt_ioctl_k *)(unsigned long)
401 		(readl(&req->context) |
402 			((u64)readl(&req->context_hi32)<<32));
403 
404 	if (readl(&req->result) == IOP_RESULT_SUCCESS) {
405 		arg->result = HPT_IOCTL_RESULT_OK;
406 
407 		if (arg->outbuf_size)
408 			memcpy_fromio(arg->outbuf,
409 				&p->buf[(readl(&p->inbuf_size) + 3)& ~3],
410 				arg->outbuf_size);
411 
412 		if (arg->bytes_returned)
413 			*arg->bytes_returned = arg->outbuf_size;
414 	}
415 	else
416 		arg->result = HPT_IOCTL_RESULT_FAILED;
417 
418 	arg->done(arg);
419 	writel(tag, &hba->iop->outbound_queue);
420 }
421 
422 static irqreturn_t hptiop_intr(int irq, void *dev_id)
423 {
424 	struct hptiop_hba  *hba = dev_id;
425 	int  handled;
426 	unsigned long flags;
427 
428 	spin_lock_irqsave(hba->host->host_lock, flags);
429 	handled = __iop_intr(hba);
430 	spin_unlock_irqrestore(hba->host->host_lock, flags);
431 
432 	return handled;
433 }
434 
435 static int hptiop_buildsgl(struct scsi_cmnd *scp, struct hpt_iopsg *psg)
436 {
437 	struct Scsi_Host *host = scp->device->host;
438 	struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
439 	struct scatterlist *sg;
440 	int idx, nseg;
441 
442 	nseg = scsi_dma_map(scp);
443 	BUG_ON(nseg < 0);
444 	if (!nseg)
445 		return 0;
446 
447 	HPT_SCP(scp)->sgcnt = nseg;
448 	HPT_SCP(scp)->mapped = 1;
449 
450 	BUG_ON(HPT_SCP(scp)->sgcnt > hba->max_sg_descriptors);
451 
452 	scsi_for_each_sg(scp, sg, HPT_SCP(scp)->sgcnt, idx) {
453 		psg[idx].pci_address = cpu_to_le64(sg_dma_address(sg));
454 		psg[idx].size = cpu_to_le32(sg_dma_len(sg));
455 		psg[idx].eot = (idx == HPT_SCP(scp)->sgcnt - 1) ?
456 			cpu_to_le32(1) : 0;
457 	}
458 	return HPT_SCP(scp)->sgcnt;
459 }
460 
461 static int hptiop_queuecommand(struct scsi_cmnd *scp,
462 				void (*done)(struct scsi_cmnd *))
463 {
464 	struct Scsi_Host *host = scp->device->host;
465 	struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
466 	struct hpt_iop_request_scsi_command *req;
467 	int sg_count = 0;
468 	struct hptiop_request *_req;
469 
470 	BUG_ON(!done);
471 	scp->scsi_done = done;
472 
473 	_req = get_req(hba);
474 	if (_req == NULL) {
475 		dprintk("hptiop_queuecmd : no free req\n");
476 		return SCSI_MLQUEUE_HOST_BUSY;
477 	}
478 
479 	_req->scp = scp;
480 
481 	dprintk("hptiop_queuecmd(scp=%p) %d/%d/%d/%d cdb=(%x-%x-%x) "
482 			"req_index=%d, req=%p\n",
483 			scp,
484 			host->host_no, scp->device->channel,
485 			scp->device->id, scp->device->lun,
486 			*((u32 *)&scp->cmnd),
487 			*((u32 *)&scp->cmnd + 1),
488 			*((u32 *)&scp->cmnd + 2),
489 			_req->index, _req->req_virt);
490 
491 	scp->result = 0;
492 
493 	if (scp->device->channel || scp->device->lun ||
494 			scp->device->id > hba->max_devices) {
495 		scp->result = DID_BAD_TARGET << 16;
496 		free_req(hba, _req);
497 		goto cmd_done;
498 	}
499 
500 	req = (struct hpt_iop_request_scsi_command *)_req->req_virt;
501 
502 	/* build S/G table */
503 	sg_count = hptiop_buildsgl(scp, req->sg_list);
504 	if (!sg_count)
505 		HPT_SCP(scp)->mapped = 0;
506 
507 	req->header.flags = cpu_to_le32(IOP_REQUEST_FLAG_OUTPUT_CONTEXT);
508 	req->header.type = cpu_to_le32(IOP_REQUEST_TYPE_SCSI_COMMAND);
509 	req->header.result = cpu_to_le32(IOP_RESULT_PENDING);
510 	req->header.context = cpu_to_le32(IOPMU_QUEUE_ADDR_HOST_BIT |
511 							(u32)_req->index);
512 	req->header.context_hi32 = 0;
513 	req->dataxfer_length = cpu_to_le32(scsi_bufflen(scp));
514 	req->channel = scp->device->channel;
515 	req->target = scp->device->id;
516 	req->lun = scp->device->lun;
517 	req->header.size = cpu_to_le32(
518 				sizeof(struct hpt_iop_request_scsi_command)
519 				 - sizeof(struct hpt_iopsg)
520 				 + sg_count * sizeof(struct hpt_iopsg));
521 
522 	memcpy(req->cdb, scp->cmnd, sizeof(req->cdb));
523 
524 	writel(IOPMU_QUEUE_ADDR_HOST_BIT | _req->req_shifted_phy,
525 			&hba->iop->inbound_queue);
526 
527 	return 0;
528 
529 cmd_done:
530 	dprintk("scsi_done(scp=%p)\n", scp);
531 	scp->scsi_done(scp);
532 	return 0;
533 }
534 
535 static const char *hptiop_info(struct Scsi_Host *host)
536 {
537 	return driver_name_long;
538 }
539 
540 static int hptiop_reset_hba(struct hptiop_hba *hba)
541 {
542 	if (atomic_xchg(&hba->resetting, 1) == 0) {
543 		atomic_inc(&hba->reset_count);
544 		writel(IOPMU_INBOUND_MSG0_RESET,
545 				&hba->iop->inbound_msgaddr0);
546 		hptiop_pci_posting_flush(hba->iop);
547 	}
548 
549 	wait_event_timeout(hba->reset_wq,
550 			atomic_read(&hba->resetting) == 0, 60 * HZ);
551 
552 	if (atomic_read(&hba->resetting)) {
553 		/* IOP is in unkown state, abort reset */
554 		printk(KERN_ERR "scsi%d: reset failed\n", hba->host->host_no);
555 		return -1;
556 	}
557 
558 	if (iop_send_sync_msg(hba,
559 		IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
560 		dprintk("scsi%d: fail to start background task\n",
561 				hba->host->host_no);
562 	}
563 
564 	return 0;
565 }
566 
567 static int hptiop_reset(struct scsi_cmnd *scp)
568 {
569 	struct Scsi_Host * host = scp->device->host;
570 	struct hptiop_hba * hba = (struct hptiop_hba *)host->hostdata;
571 
572 	printk(KERN_WARNING "hptiop_reset(%d/%d/%d) scp=%p\n",
573 			scp->device->host->host_no, scp->device->channel,
574 			scp->device->id, scp);
575 
576 	return hptiop_reset_hba(hba)? FAILED : SUCCESS;
577 }
578 
579 static int hptiop_adjust_disk_queue_depth(struct scsi_device *sdev,
580 						int queue_depth)
581 {
582 	if(queue_depth > 256)
583 		queue_depth = 256;
584 	scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
585 	return queue_depth;
586 }
587 
588 static ssize_t hptiop_show_version(struct class_device *class_dev, char *buf)
589 {
590 	return snprintf(buf, PAGE_SIZE, "%s\n", driver_ver);
591 }
592 
593 static ssize_t hptiop_show_fw_version(struct class_device *class_dev, char *buf)
594 {
595 	struct Scsi_Host *host = class_to_shost(class_dev);
596 	struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
597 
598 	return snprintf(buf, PAGE_SIZE, "%d.%d.%d.%d\n",
599 				hba->firmware_version >> 24,
600 				(hba->firmware_version >> 16) & 0xff,
601 				(hba->firmware_version >> 8) & 0xff,
602 				hba->firmware_version & 0xff);
603 }
604 
605 static struct class_device_attribute hptiop_attr_version = {
606 	.attr = {
607 		.name = "driver-version",
608 		.mode = S_IRUGO,
609 	},
610 	.show = hptiop_show_version,
611 };
612 
613 static struct class_device_attribute hptiop_attr_fw_version = {
614 	.attr = {
615 		.name = "firmware-version",
616 		.mode = S_IRUGO,
617 	},
618 	.show = hptiop_show_fw_version,
619 };
620 
621 static struct class_device_attribute *hptiop_attrs[] = {
622 	&hptiop_attr_version,
623 	&hptiop_attr_fw_version,
624 	NULL
625 };
626 
627 static struct scsi_host_template driver_template = {
628 	.module                     = THIS_MODULE,
629 	.name                       = driver_name,
630 	.queuecommand               = hptiop_queuecommand,
631 	.eh_device_reset_handler    = hptiop_reset,
632 	.eh_bus_reset_handler       = hptiop_reset,
633 	.info                       = hptiop_info,
634 	.unchecked_isa_dma          = 0,
635 	.emulated                   = 0,
636 	.use_clustering             = ENABLE_CLUSTERING,
637 	.proc_name                  = driver_name,
638 	.shost_attrs                = hptiop_attrs,
639 	.this_id                    = -1,
640 	.change_queue_depth         = hptiop_adjust_disk_queue_depth,
641 };
642 
643 static int __devinit hptiop_probe(struct pci_dev *pcidev,
644 					const struct pci_device_id *id)
645 {
646 	struct Scsi_Host *host = NULL;
647 	struct hptiop_hba *hba;
648 	struct hpt_iop_request_get_config iop_config;
649 	struct hpt_iop_request_set_config set_config;
650 	dma_addr_t start_phy;
651 	void *start_virt;
652 	u32 offset, i, req_size;
653 
654 	dprintk("hptiop_probe(%p)\n", pcidev);
655 
656 	if (pci_enable_device(pcidev)) {
657 		printk(KERN_ERR "hptiop: fail to enable pci device\n");
658 		return -ENODEV;
659 	}
660 
661 	printk(KERN_INFO "adapter at PCI %d:%d:%d, IRQ %d\n",
662 		pcidev->bus->number, pcidev->devfn >> 3, pcidev->devfn & 7,
663 		pcidev->irq);
664 
665 	pci_set_master(pcidev);
666 
667 	/* Enable 64bit DMA if possible */
668 	if (pci_set_dma_mask(pcidev, DMA_64BIT_MASK)) {
669 		if (pci_set_dma_mask(pcidev, DMA_32BIT_MASK)) {
670 			printk(KERN_ERR "hptiop: fail to set dma_mask\n");
671 			goto disable_pci_device;
672 		}
673 	}
674 
675 	if (pci_request_regions(pcidev, driver_name)) {
676 		printk(KERN_ERR "hptiop: pci_request_regions failed\n");
677 		goto disable_pci_device;
678 	}
679 
680 	host = scsi_host_alloc(&driver_template, sizeof(struct hptiop_hba));
681 	if (!host) {
682 		printk(KERN_ERR "hptiop: fail to alloc scsi host\n");
683 		goto free_pci_regions;
684 	}
685 
686 	hba = (struct hptiop_hba *)host->hostdata;
687 
688 	hba->pcidev = pcidev;
689 	hba->host = host;
690 	hba->initialized = 0;
691 
692 	atomic_set(&hba->resetting, 0);
693 	atomic_set(&hba->reset_count, 0);
694 
695 	init_waitqueue_head(&hba->reset_wq);
696 	init_waitqueue_head(&hba->ioctl_wq);
697 
698 	host->max_lun = 1;
699 	host->max_channel = 0;
700 	host->io_port = 0;
701 	host->n_io_port = 0;
702 	host->irq = pcidev->irq;
703 
704 	if (hptiop_map_pci_bar(hba))
705 		goto free_scsi_host;
706 
707 	if (iop_wait_ready(hba->iop, 20000)) {
708 		printk(KERN_ERR "scsi%d: firmware not ready\n",
709 				hba->host->host_no);
710 		goto unmap_pci_bar;
711 	}
712 
713 	if (iop_get_config(hba, &iop_config)) {
714 		printk(KERN_ERR "scsi%d: get config failed\n",
715 				hba->host->host_no);
716 		goto unmap_pci_bar;
717 	}
718 
719 	hba->max_requests = min(le32_to_cpu(iop_config.max_requests),
720 				HPTIOP_MAX_REQUESTS);
721 	hba->max_devices = le32_to_cpu(iop_config.max_devices);
722 	hba->max_request_size = le32_to_cpu(iop_config.request_size);
723 	hba->max_sg_descriptors = le32_to_cpu(iop_config.max_sg_count);
724 	hba->firmware_version = le32_to_cpu(iop_config.firmware_version);
725 	hba->sdram_size = le32_to_cpu(iop_config.sdram_size);
726 
727 	host->max_sectors = le32_to_cpu(iop_config.data_transfer_length) >> 9;
728 	host->max_id = le32_to_cpu(iop_config.max_devices);
729 	host->sg_tablesize = le32_to_cpu(iop_config.max_sg_count);
730 	host->can_queue = le32_to_cpu(iop_config.max_requests);
731 	host->cmd_per_lun = le32_to_cpu(iop_config.max_requests);
732 	host->max_cmd_len = 16;
733 
734 	set_config.vbus_id = cpu_to_le32(host->host_no);
735 	set_config.iop_id = cpu_to_le32(host->host_no);
736 
737 	if (iop_set_config(hba, &set_config)) {
738 		printk(KERN_ERR "scsi%d: set config failed\n",
739 				hba->host->host_no);
740 		goto unmap_pci_bar;
741 	}
742 
743 	pci_set_drvdata(pcidev, host);
744 
745 	if (request_irq(pcidev->irq, hptiop_intr, IRQF_SHARED,
746 					driver_name, hba)) {
747 		printk(KERN_ERR "scsi%d: request irq %d failed\n",
748 					hba->host->host_no, pcidev->irq);
749 		goto unmap_pci_bar;
750 	}
751 
752 	/* Allocate request mem */
753 	req_size = sizeof(struct hpt_iop_request_scsi_command)
754 		+ sizeof(struct hpt_iopsg) * (hba->max_sg_descriptors - 1);
755 	if ((req_size& 0x1f) != 0)
756 		req_size = (req_size + 0x1f) & ~0x1f;
757 
758 	dprintk("req_size=%d, max_requests=%d\n", req_size, hba->max_requests);
759 
760 	hba->req_size = req_size;
761 	start_virt = dma_alloc_coherent(&pcidev->dev,
762 				hba->req_size*hba->max_requests + 0x20,
763 				&start_phy, GFP_KERNEL);
764 
765 	if (!start_virt) {
766 		printk(KERN_ERR "scsi%d: fail to alloc request mem\n",
767 					hba->host->host_no);
768 		goto free_request_irq;
769 	}
770 
771 	hba->dma_coherent = start_virt;
772 	hba->dma_coherent_handle = start_phy;
773 
774 	if ((start_phy & 0x1f) != 0)
775 	{
776 		offset = ((start_phy + 0x1f) & ~0x1f) - start_phy;
777 		start_phy += offset;
778 		start_virt += offset;
779 	}
780 
781 	hba->req_list = start_virt;
782 	for (i = 0; i < hba->max_requests; i++) {
783 		hba->reqs[i].next = NULL;
784 		hba->reqs[i].req_virt = start_virt;
785 		hba->reqs[i].req_shifted_phy = start_phy >> 5;
786 		hba->reqs[i].index = i;
787 		free_req(hba, &hba->reqs[i]);
788 		start_virt = (char *)start_virt + hba->req_size;
789 		start_phy = start_phy + hba->req_size;
790 	}
791 
792 	/* Enable Interrupt and start background task */
793 	if (hptiop_initialize_iop(hba))
794 		goto free_request_mem;
795 
796 	if (scsi_add_host(host, &pcidev->dev)) {
797 		printk(KERN_ERR "scsi%d: scsi_add_host failed\n",
798 					hba->host->host_no);
799 		goto free_request_mem;
800 	}
801 
802 
803 	scsi_scan_host(host);
804 
805 	dprintk("scsi%d: hptiop_probe successfully\n", hba->host->host_no);
806 	return 0;
807 
808 free_request_mem:
809 	dma_free_coherent(&hba->pcidev->dev,
810 			hba->req_size*hba->max_requests + 0x20,
811 			hba->dma_coherent, hba->dma_coherent_handle);
812 
813 free_request_irq:
814 	free_irq(hba->pcidev->irq, hba);
815 
816 unmap_pci_bar:
817 	iounmap(hba->iop);
818 
819 free_pci_regions:
820 	pci_release_regions(pcidev) ;
821 
822 free_scsi_host:
823 	scsi_host_put(host);
824 
825 disable_pci_device:
826 	pci_disable_device(pcidev);
827 
828 	dprintk("scsi%d: hptiop_probe fail\n", host->host_no);
829 	return -ENODEV;
830 }
831 
832 static void hptiop_shutdown(struct pci_dev *pcidev)
833 {
834 	struct Scsi_Host *host = pci_get_drvdata(pcidev);
835 	struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
836 	struct hpt_iopmu __iomem *iop = hba->iop;
837 	u32    int_mask;
838 
839 	dprintk("hptiop_shutdown(%p)\n", hba);
840 
841 	/* stop the iop */
842 	if (iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000))
843 		printk(KERN_ERR "scsi%d: shutdown the iop timeout\n",
844 					hba->host->host_no);
845 
846 	/* disable all outbound interrupts */
847 	int_mask = readl(&iop->outbound_intmask);
848 	writel(int_mask |
849 		IOPMU_OUTBOUND_INT_MSG0 | IOPMU_OUTBOUND_INT_POSTQUEUE,
850 		&iop->outbound_intmask);
851 	hptiop_pci_posting_flush(iop);
852 }
853 
854 static void hptiop_remove(struct pci_dev *pcidev)
855 {
856 	struct Scsi_Host *host = pci_get_drvdata(pcidev);
857 	struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
858 
859 	dprintk("scsi%d: hptiop_remove\n", hba->host->host_no);
860 
861 	scsi_remove_host(host);
862 
863 	hptiop_shutdown(pcidev);
864 
865 	free_irq(hba->pcidev->irq, hba);
866 
867 	dma_free_coherent(&hba->pcidev->dev,
868 			hba->req_size * hba->max_requests + 0x20,
869 			hba->dma_coherent,
870 			hba->dma_coherent_handle);
871 
872 	iounmap(hba->iop);
873 
874 	pci_release_regions(hba->pcidev);
875 	pci_set_drvdata(hba->pcidev, NULL);
876 	pci_disable_device(hba->pcidev);
877 
878 	scsi_host_put(host);
879 }
880 
881 static struct pci_device_id hptiop_id_table[] = {
882 	{ PCI_DEVICE(0x1103, 0x3220) },
883 	{ PCI_DEVICE(0x1103, 0x3320) },
884 	{},
885 };
886 
887 MODULE_DEVICE_TABLE(pci, hptiop_id_table);
888 
889 static struct pci_driver hptiop_pci_driver = {
890 	.name       = driver_name,
891 	.id_table   = hptiop_id_table,
892 	.probe      = hptiop_probe,
893 	.remove     = hptiop_remove,
894 	.shutdown   = hptiop_shutdown,
895 };
896 
897 static int __init hptiop_module_init(void)
898 {
899 	printk(KERN_INFO "%s %s\n", driver_name_long, driver_ver);
900 	return pci_register_driver(&hptiop_pci_driver);
901 }
902 
903 static void __exit hptiop_module_exit(void)
904 {
905 	pci_unregister_driver(&hptiop_pci_driver);
906 }
907 
908 
909 module_init(hptiop_module_init);
910 module_exit(hptiop_module_exit);
911 
912 MODULE_LICENSE("GPL");
913