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