6c005c4bc8165d5dd070fdd81f9d7ceab066f4a0
[dragonfly.git] / sys / dev / raid / hptiop / hptiop.c
1 /*
2  * HighPoint RR3xxx/4xxx RAID Driver for FreeBSD
3  * Copyright (C) 2007-2008 HighPoint Technologies, Inc. All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/hptiop/hptiop.c,v 1.8 2010/06/19 13:41:17 mav Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/cons.h>
32 #include <sys/time.h>
33 #include <sys/systm.h>
34
35 #include <sys/stat.h>
36 #include <sys/malloc.h>
37 #include <sys/conf.h>
38 #include <sys/libkern.h>
39 #include <sys/kernel.h>
40
41 #include <sys/kthread.h>
42 #include <sys/lock.h>
43 #include <sys/module.h>
44
45 #include <sys/eventhandler.h>
46 #include <sys/bus.h>
47 #include <sys/taskqueue.h>
48 #include <sys/ioccom.h>
49 #include <sys/device.h>
50 #include <sys/mplock2.h>
51
52 #include <machine/stdarg.h>
53 #include <sys/rman.h>
54
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57
58 #include <bus/pci/pcireg.h>
59 #include <bus/pci/pcivar.h>
60
61 #include <bus/cam/cam.h>
62 #include <bus/cam/cam_ccb.h>
63 #include <bus/cam/cam_sim.h>
64 #include <bus/cam/cam_xpt_periph.h>
65 #include <bus/cam/cam_xpt_sim.h>
66 #include <bus/cam/cam_debug.h>
67 #include <bus/cam/cam_periph.h>
68 #include <bus/cam/scsi/scsi_all.h>
69 #include <bus/cam/scsi/scsi_message.h>
70
71 #include <dev/raid/hptiop/hptiop.h>
72
73 static char driver_name[] = "hptiop";
74 static char driver_version[] = "v1.3 (010208)";
75
76 static devclass_t hptiop_devclass;
77
78 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
79                                 u_int32_t msg, u_int32_t millisec);
80 static void hptiop_request_callback_itl(struct hpt_iop_hba *hba,
81                                                         u_int32_t req);
82 static void hptiop_request_callback_mv(struct hpt_iop_hba *hba, u_int64_t req);
83 static void hptiop_os_message_callback(struct hpt_iop_hba *hba, u_int32_t msg);
84 static int  hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
85                                 struct hpt_iop_ioctl_param *pParams);
86 static int  hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
87                                 struct hpt_iop_ioctl_param *pParams);
88 static void hptiop_bus_scan_cb(struct cam_periph *periph, union ccb *ccb);
89 static int  hptiop_rescan_bus(struct hpt_iop_hba *hba);
90 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba);
91 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba);
92 static int hptiop_get_config_itl(struct hpt_iop_hba *hba,
93                                 struct hpt_iop_request_get_config *config);
94 static int hptiop_get_config_mv(struct hpt_iop_hba *hba,
95                                 struct hpt_iop_request_get_config *config);
96 static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
97                                 struct hpt_iop_request_set_config *config);
98 static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
99                                 struct hpt_iop_request_set_config *config);
100 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba);
101 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba);
102 static int  hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
103                         u_int32_t req32, struct hpt_iop_ioctl_param *pParams);
104 static int  hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
105                                 struct hpt_iop_request_ioctl_command *req,
106                                 struct hpt_iop_ioctl_param *pParams);
107 static void hptiop_post_req_itl(struct hpt_iop_hba *hba,
108                                 struct hpt_iop_srb *srb,
109                                 bus_dma_segment_t *segs, int nsegs);
110 static void hptiop_post_req_mv(struct hpt_iop_hba *hba,
111                                 struct hpt_iop_srb *srb,
112                                 bus_dma_segment_t *segs, int nsegs);
113 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg);
114 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg);
115 static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba);
116 static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba);
117 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba);
118 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba);
119 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb);
120 static int  hptiop_os_query_remove_device(struct hpt_iop_hba *hba, int tid);
121 static int  hptiop_probe(device_t dev);
122 static int  hptiop_attach(device_t dev);
123 static int  hptiop_detach(device_t dev);
124 static int  hptiop_shutdown(device_t dev);
125 static void hptiop_action(struct cam_sim *sim, union ccb *ccb);
126 static void hptiop_poll(struct cam_sim *sim);
127 static void hptiop_async(void *callback_arg, u_int32_t code,
128                                         struct cam_path *path, void *arg);
129 static void hptiop_pci_intr(void *arg);
130 static void hptiop_release_resource(struct hpt_iop_hba *hba);
131 static int  hptiop_reset_adapter(struct hpt_iop_hba *hba);
132
133 static d_open_t hptiop_open;
134 static d_close_t hptiop_close;
135 static d_ioctl_t hptiop_ioctl;
136
137 static struct dev_ops hptiop_ops = {
138         { driver_name, 0, 0 },
139         .d_open = hptiop_open,
140         .d_close = hptiop_close,
141         .d_ioctl = hptiop_ioctl,
142 };
143
144 #define hba_from_dev(dev) ((struct hpt_iop_hba *)(dev)->si_drv1)
145
146 #define BUS_SPACE_WRT4_ITL(offset, value) bus_space_write_4(hba->bar0t,\
147                 hba->bar0h, offsetof(struct hpt_iopmu_itl, offset), (value))
148 #define BUS_SPACE_RD4_ITL(offset) bus_space_read_4(hba->bar0t,\
149                 hba->bar0h, offsetof(struct hpt_iopmu_itl, offset))
150
151 #define BUS_SPACE_WRT4_MV0(offset, value) bus_space_write_4(hba->bar0t,\
152                 hba->bar0h, offsetof(struct hpt_iopmv_regs, offset), value)
153 #define BUS_SPACE_RD4_MV0(offset) bus_space_read_4(hba->bar0t,\
154                 hba->bar0h, offsetof(struct hpt_iopmv_regs, offset))
155 #define BUS_SPACE_WRT4_MV2(offset, value) bus_space_write_4(hba->bar2t,\
156                 hba->bar2h, offsetof(struct hpt_iopmu_mv, offset), value)
157 #define BUS_SPACE_RD4_MV2(offset) bus_space_read_4(hba->bar2t,\
158                 hba->bar2h, offsetof(struct hpt_iopmu_mv, offset))
159
160 static int hptiop_open(struct dev_open_args *ap)
161 {
162         cdev_t dev = ap->a_head.a_dev;
163         struct hpt_iop_hba *hba = hba_from_dev(dev);
164
165         if (hba==NULL)
166                 return ENXIO;
167         if (hba->flag & HPT_IOCTL_FLAG_OPEN)
168                 return EBUSY;
169         hba->flag |= HPT_IOCTL_FLAG_OPEN;
170         return 0;
171 }
172
173 static int hptiop_close(struct dev_close_args *ap)
174 {
175         cdev_t dev = ap->a_head.a_dev;
176         struct hpt_iop_hba *hba = hba_from_dev(dev);
177         hba->flag &= ~(u_int32_t)HPT_IOCTL_FLAG_OPEN;
178         return 0;
179 }
180
181 static int hptiop_ioctl(struct dev_ioctl_args *ap)
182 {
183         cdev_t dev = ap->a_head.a_dev;
184         u_long cmd = ap->a_cmd;
185         caddr_t data = ap->a_data;
186         int ret = EFAULT;
187         struct hpt_iop_hba *hba = hba_from_dev(dev);
188
189         get_mplock();
190
191         switch (cmd) {
192         case HPT_DO_IOCONTROL:
193                 ret = hba->ops->do_ioctl(hba,
194                                 (struct hpt_iop_ioctl_param *)data);
195                 break;
196         case HPT_SCAN_BUS:
197                 ret = hptiop_rescan_bus(hba);
198                 break;
199         }
200
201         rel_mplock();
202
203         return ret;
204 }
205
206 static u_int64_t hptiop_mv_outbound_read(struct hpt_iop_hba *hba)
207 {
208         u_int64_t p;
209         u_int32_t outbound_tail = BUS_SPACE_RD4_MV2(outbound_tail);
210         u_int32_t outbound_head = BUS_SPACE_RD4_MV2(outbound_head);
211
212         if (outbound_tail != outbound_head) {
213                 bus_space_read_region_4(hba->bar2t, hba->bar2h,
214                         offsetof(struct hpt_iopmu_mv,
215                                 outbound_q[outbound_tail]),
216                         (u_int32_t *)&p, 2);
217
218                 outbound_tail++;
219
220                 if (outbound_tail == MVIOP_QUEUE_LEN)
221                         outbound_tail = 0;
222
223                 BUS_SPACE_WRT4_MV2(outbound_tail, outbound_tail);
224                 return p;
225         } else
226                 return 0;
227 }
228
229 static void hptiop_mv_inbound_write(u_int64_t p, struct hpt_iop_hba *hba)
230 {
231         u_int32_t inbound_head = BUS_SPACE_RD4_MV2(inbound_head);
232         u_int32_t head = inbound_head + 1;
233
234         if (head == MVIOP_QUEUE_LEN)
235                 head = 0;
236
237         bus_space_write_region_4(hba->bar2t, hba->bar2h,
238                         offsetof(struct hpt_iopmu_mv, inbound_q[inbound_head]),
239                         (u_int32_t *)&p, 2);
240         BUS_SPACE_WRT4_MV2(inbound_head, head);
241         BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_POSTQUEUE);
242 }
243
244 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg)
245 {
246         BUS_SPACE_WRT4_ITL(inbound_msgaddr0, msg);
247         BUS_SPACE_RD4_ITL(outbound_intstatus);
248 }
249
250 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg)
251 {
252
253         BUS_SPACE_WRT4_MV2(inbound_msg, msg);
254         BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_MSG);
255
256         BUS_SPACE_RD4_MV0(outbound_intmask);
257 }
258
259 static int hptiop_wait_ready_itl(struct hpt_iop_hba * hba, u_int32_t millisec)
260 {
261         u_int32_t req=0;
262         int i;
263
264         for (i = 0; i < millisec; i++) {
265                 req = BUS_SPACE_RD4_ITL(inbound_queue);
266                 if (req != IOPMU_QUEUE_EMPTY)
267                         break;
268                 DELAY(1000);
269         }
270
271         if (req!=IOPMU_QUEUE_EMPTY) {
272                 BUS_SPACE_WRT4_ITL(outbound_queue, req);
273                 BUS_SPACE_RD4_ITL(outbound_intstatus);
274                 return 0;
275         }
276
277         return -1;
278 }
279
280 static int hptiop_wait_ready_mv(struct hpt_iop_hba * hba, u_int32_t millisec)
281 {
282         if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec))
283                 return -1;
284
285         return 0;
286 }
287
288 static void hptiop_request_callback_itl(struct hpt_iop_hba * hba,
289                                                         u_int32_t index)
290 {
291         struct hpt_iop_srb *srb;
292         struct hpt_iop_request_scsi_command *req=0;
293         union ccb *ccb;
294         u_int8_t *cdb;
295         u_int32_t result, temp, dxfer;
296         u_int64_t temp64;
297
298         if (index & IOPMU_QUEUE_MASK_HOST_BITS) { /*host req*/
299                 if (hba->firmware_version > 0x01020000 ||
300                         hba->interface_version > 0x01020000) {
301                         srb = hba->srb[index & ~(u_int32_t)
302                                 (IOPMU_QUEUE_ADDR_HOST_BIT
303                                 | IOPMU_QUEUE_REQUEST_RESULT_BIT)];
304                         req = (struct hpt_iop_request_scsi_command *)srb;
305                         if (index & IOPMU_QUEUE_REQUEST_RESULT_BIT)
306                                 result = IOP_RESULT_SUCCESS;
307                         else
308                                 result = req->header.result;
309                 } else {
310                         srb = hba->srb[index &
311                                 ~(u_int32_t)IOPMU_QUEUE_ADDR_HOST_BIT];
312                         req = (struct hpt_iop_request_scsi_command *)srb;
313                         result = req->header.result;
314                 }
315                 dxfer = req->dataxfer_length;
316                 goto srb_complete;
317         }
318
319         /*iop req*/
320         temp = bus_space_read_4(hba->bar0t, hba->bar0h, index +
321                 offsetof(struct hpt_iop_request_header, type));
322         result = bus_space_read_4(hba->bar0t, hba->bar0h, index +
323                 offsetof(struct hpt_iop_request_header, result));
324         switch(temp) {
325         case IOP_REQUEST_TYPE_IOCTL_COMMAND:
326         {
327                 temp64 = 0;
328                 bus_space_write_region_4(hba->bar0t, hba->bar0h, index +
329                         offsetof(struct hpt_iop_request_header, context),
330                         (u_int32_t *)&temp64, 2);
331                 wakeup((void *)((unsigned long)hba->u.itl.mu + index));
332                 break;
333         }
334
335         case IOP_REQUEST_TYPE_SCSI_COMMAND:
336                 bus_space_read_region_4(hba->bar0t, hba->bar0h, index +
337                         offsetof(struct hpt_iop_request_header, context),
338                         (u_int32_t *)&temp64, 2);
339                 srb = (struct hpt_iop_srb *)(unsigned long)temp64;
340                 dxfer = bus_space_read_4(hba->bar0t, hba->bar0h,
341                                 index + offsetof(struct hpt_iop_request_scsi_command,
342                                 dataxfer_length));
343 srb_complete:
344                 ccb = (union ccb *)srb->ccb;
345                 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
346                         cdb = ccb->csio.cdb_io.cdb_ptr;
347                 else
348                         cdb = ccb->csio.cdb_io.cdb_bytes;
349
350                 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
351                         ccb->ccb_h.status = CAM_REQ_CMP;
352                         goto scsi_done;
353                 }
354
355                 switch (result) {
356                 case IOP_RESULT_SUCCESS:
357                         switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
358                         case CAM_DIR_IN:
359                                 bus_dmamap_sync(hba->io_dmat,
360                                         srb->dma_map, BUS_DMASYNC_POSTREAD);
361                                 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
362                                 break;
363                         case CAM_DIR_OUT:
364                                 bus_dmamap_sync(hba->io_dmat,
365                                         srb->dma_map, BUS_DMASYNC_POSTWRITE);
366                                 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
367                                 break;
368                         }
369
370                         ccb->ccb_h.status = CAM_REQ_CMP;
371                         break;
372
373                 case IOP_RESULT_BAD_TARGET:
374                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
375                         break;
376                 case IOP_RESULT_BUSY:
377                         ccb->ccb_h.status = CAM_BUSY;
378                         break;
379                 case IOP_RESULT_INVALID_REQUEST:
380                         ccb->ccb_h.status = CAM_REQ_INVALID;
381                         break;
382                 case IOP_RESULT_FAIL:
383                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
384                         break;
385                 case IOP_RESULT_RESET:
386                         ccb->ccb_h.status = CAM_BUSY;
387                         break;
388                 case IOP_RESULT_CHECK_CONDITION:
389                         if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {/*iop*/
390                                 bus_space_read_region_1(hba->bar0t, hba->bar0h,
391                                         index + offsetof(struct hpt_iop_request_scsi_command,
392                                         sg_list), (u_int8_t *)&ccb->csio.sense_data,
393                                         MIN(dxfer, sizeof(ccb->csio.sense_data)));
394                         } else {
395                                 memcpy(&ccb->csio.sense_data, &req->sg_list,
396                                         MIN(dxfer, sizeof(ccb->csio.sense_data)));
397                         }
398                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
399                         ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
400                         ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
401                         break;
402                 default:
403                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
404                         break;
405                 }
406 scsi_done:
407                 if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS)
408                         BUS_SPACE_WRT4_ITL(outbound_queue, index);
409
410                 ccb->csio.resid = ccb->csio.dxfer_len - dxfer;
411
412                 hptiop_free_srb(hba, srb);
413                 xpt_done(ccb);
414                 break;
415         }
416 }
417
418 static void hptiop_drain_outbound_queue_itl(struct hpt_iop_hba *hba)
419 {
420         u_int32_t req, temp;
421
422         while ((req = BUS_SPACE_RD4_ITL(outbound_queue)) !=IOPMU_QUEUE_EMPTY) {
423                 if (req & IOPMU_QUEUE_MASK_HOST_BITS)
424                         hptiop_request_callback_itl(hba, req);
425                 else {
426                         struct hpt_iop_request_header *p;
427
428                         p = (struct hpt_iop_request_header *)
429                                 ((char *)hba->u.itl.mu + req);
430                         temp = bus_space_read_4(hba->bar0t,
431                                         hba->bar0h,req +
432                                         offsetof(struct hpt_iop_request_header,
433                                                 flags));
434                         if (temp & IOP_REQUEST_FLAG_SYNC_REQUEST) {
435                                 u_int64_t temp64;
436                                 bus_space_read_region_4(hba->bar0t,
437                                         hba->bar0h,req +
438                                         offsetof(struct hpt_iop_request_header,
439                                                 context),
440                                         (u_int32_t *)&temp64, 2);
441                                 if (temp64) {
442                                         hptiop_request_callback_itl(hba, req);
443                                 } else {
444                                         temp64 = 1;
445                                         bus_space_write_region_4(hba->bar0t,
446                                                 hba->bar0h,req +
447                                                 offsetof(struct hpt_iop_request_header,
448                                                         context),
449                                                 (u_int32_t *)&temp64, 2);
450                                 }
451                         } else
452                                 hptiop_request_callback_itl(hba, req);
453                 }
454         }
455 }
456
457 static int hptiop_intr_itl(struct hpt_iop_hba * hba)
458 {
459         u_int32_t status;
460         int ret = 0;
461
462         status = BUS_SPACE_RD4_ITL(outbound_intstatus);
463
464         if (status & IOPMU_OUTBOUND_INT_MSG0) {
465                 u_int32_t msg = BUS_SPACE_RD4_ITL(outbound_msgaddr0);
466                 KdPrint(("hptiop: received outbound msg %x\n", msg));
467                 BUS_SPACE_WRT4_ITL(outbound_intstatus, IOPMU_OUTBOUND_INT_MSG0);
468                 hptiop_os_message_callback(hba, msg);
469                 ret = 1;
470         }
471
472         if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) {
473                 hptiop_drain_outbound_queue_itl(hba);
474                 ret = 1;
475         }
476
477         return ret;
478 }
479
480 static void hptiop_request_callback_mv(struct hpt_iop_hba * hba,
481                                                         u_int64_t _tag)
482 {
483         u_int32_t context = (u_int32_t)_tag;
484
485         if (context & MVIOP_CMD_TYPE_SCSI) {
486                 struct hpt_iop_srb *srb;
487                 struct hpt_iop_request_scsi_command *req;
488                 union ccb *ccb;
489                 u_int8_t *cdb;
490
491                 srb = hba->srb[context >> MVIOP_REQUEST_NUMBER_START_BIT];
492                 req = (struct hpt_iop_request_scsi_command *)srb;
493                 ccb = (union ccb *)srb->ccb;
494                 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
495                         cdb = ccb->csio.cdb_io.cdb_ptr;
496                 else
497                         cdb = ccb->csio.cdb_io.cdb_bytes;
498
499                 if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
500                         ccb->ccb_h.status = CAM_REQ_CMP;
501                         goto scsi_done;
502                 }
503                 if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
504                         req->header.result = IOP_RESULT_SUCCESS;
505
506                 switch (req->header.result) {
507                 case IOP_RESULT_SUCCESS:
508                         switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
509                         case CAM_DIR_IN:
510                                 bus_dmamap_sync(hba->io_dmat,
511                                         srb->dma_map, BUS_DMASYNC_POSTREAD);
512                                 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
513                                 break;
514                         case CAM_DIR_OUT:
515                                 bus_dmamap_sync(hba->io_dmat,
516                                         srb->dma_map, BUS_DMASYNC_POSTWRITE);
517                                 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
518                                 break;
519                         }
520                         ccb->ccb_h.status = CAM_REQ_CMP;
521                         break;
522                 case IOP_RESULT_BAD_TARGET:
523                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
524                         break;
525                 case IOP_RESULT_BUSY:
526                         ccb->ccb_h.status = CAM_BUSY;
527                         break;
528                 case IOP_RESULT_INVALID_REQUEST:
529                         ccb->ccb_h.status = CAM_REQ_INVALID;
530                         break;
531                 case IOP_RESULT_FAIL:
532                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
533                         break;
534                 case IOP_RESULT_RESET:
535                         ccb->ccb_h.status = CAM_BUSY;
536                         break;
537                 case IOP_RESULT_CHECK_CONDITION:
538                         memcpy(&ccb->csio.sense_data, &req->sg_list,
539                                 MIN(req->dataxfer_length, sizeof(ccb->csio.sense_data)));
540                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
541                         ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
542                         ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
543                         break;
544                 default:
545                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
546                         break;
547                 }
548 scsi_done:
549                 ccb->csio.resid = ccb->csio.dxfer_len - req->dataxfer_length;
550
551                 hptiop_free_srb(hba, srb);
552                 xpt_done(ccb);
553         } else if (context & MVIOP_CMD_TYPE_IOCTL) {
554                 struct hpt_iop_request_ioctl_command *req = hba->ctlcfg_ptr;
555                 if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
556                         hba->config_done = 1;
557                 else
558                         hba->config_done = -1;
559                 wakeup(req);
560         } else if (context &
561                         (MVIOP_CMD_TYPE_SET_CONFIG |
562                                 MVIOP_CMD_TYPE_GET_CONFIG))
563                 hba->config_done = 1;
564         else {
565                 device_printf(hba->pcidev, "wrong callback type\n");
566         }
567 }
568
569 static void hptiop_drain_outbound_queue_mv(struct hpt_iop_hba * hba)
570 {
571         u_int64_t req;
572
573         while ((req = hptiop_mv_outbound_read(hba))) {
574                 if (req & MVIOP_MU_QUEUE_ADDR_HOST_BIT) {
575                         if (req & MVIOP_MU_QUEUE_REQUEST_RETURN_CONTEXT) {
576                                 hptiop_request_callback_mv(hba, req);
577                         }
578                 }
579         }
580 }
581
582 static int hptiop_intr_mv(struct hpt_iop_hba * hba)
583 {
584         u_int32_t status;
585         int ret = 0;
586
587         status = BUS_SPACE_RD4_MV0(outbound_doorbell);
588
589         if (status)
590                 BUS_SPACE_WRT4_MV0(outbound_doorbell, ~status);
591
592         if (status & MVIOP_MU_OUTBOUND_INT_MSG) {
593                 u_int32_t msg = BUS_SPACE_RD4_MV2(outbound_msg);
594                 KdPrint(("hptiop: received outbound msg %x\n", msg));
595                 hptiop_os_message_callback(hba, msg);
596                 ret = 1;
597         }
598
599         if (status & MVIOP_MU_OUTBOUND_INT_POSTQUEUE) {
600                 hptiop_drain_outbound_queue_mv(hba);
601                 ret = 1;
602         }
603
604         return ret;
605 }
606
607 static int hptiop_send_sync_request_itl(struct hpt_iop_hba * hba,
608                                         u_int32_t req32, u_int32_t millisec)
609 {
610         u_int32_t i;
611         u_int64_t temp64;
612
613         BUS_SPACE_WRT4_ITL(inbound_queue, req32);
614         BUS_SPACE_RD4_ITL(outbound_intstatus);
615
616         for (i = 0; i < millisec; i++) {
617                 hptiop_intr_itl(hba);
618                 bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
619                         offsetof(struct hpt_iop_request_header, context),
620                         (u_int32_t *)&temp64, 2);
621                 if (temp64)
622                         return 0;
623                 DELAY(1000);
624         }
625
626         return -1;
627 }
628
629 static int hptiop_send_sync_request_mv(struct hpt_iop_hba *hba,
630                                         void *req, u_int32_t millisec)
631 {
632         u_int32_t i;
633         u_int64_t phy_addr;
634         hba->config_done = 0;
635
636         phy_addr = hba->ctlcfgcmd_phy |
637                         (u_int64_t)MVIOP_MU_QUEUE_ADDR_HOST_BIT;
638         ((struct hpt_iop_request_get_config *)req)->header.flags |=
639                 IOP_REQUEST_FLAG_SYNC_REQUEST |
640                 IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
641         hptiop_mv_inbound_write(phy_addr, hba);
642         BUS_SPACE_RD4_MV0(outbound_intmask);
643
644         for (i = 0; i < millisec; i++) {
645                 hptiop_intr_mv(hba);
646                 if (hba->config_done)
647                         return 0;
648                 DELAY(1000);
649         }
650         return -1;
651 }
652
653 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
654                                         u_int32_t msg, u_int32_t millisec)
655 {
656         u_int32_t i;
657
658         hba->msg_done = 0;
659         hba->ops->post_msg(hba, msg);
660
661         for (i=0; i<millisec; i++) {
662                 hba->ops->iop_intr(hba);
663                 if (hba->msg_done)
664                         break;
665                 DELAY(1000);
666         }
667
668         return hba->msg_done? 0 : -1;
669 }
670
671 static int hptiop_get_config_itl(struct hpt_iop_hba * hba,
672                                 struct hpt_iop_request_get_config * config)
673 {
674         u_int32_t req32;
675
676         config->header.size = sizeof(struct hpt_iop_request_get_config);
677         config->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
678         config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
679         config->header.result = IOP_RESULT_PENDING;
680         config->header.context = 0;
681
682         req32 = BUS_SPACE_RD4_ITL(inbound_queue);
683         if (req32 == IOPMU_QUEUE_EMPTY)
684                 return -1;
685
686         bus_space_write_region_4(hba->bar0t, hba->bar0h,
687                         req32, (u_int32_t *)config,
688                         sizeof(struct hpt_iop_request_header) >> 2);
689
690         if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
691                 KdPrint(("hptiop: get config send cmd failed"));
692                 return -1;
693         }
694
695         bus_space_read_region_4(hba->bar0t, hba->bar0h,
696                         req32, (u_int32_t *)config,
697                         sizeof(struct hpt_iop_request_get_config) >> 2);
698
699         BUS_SPACE_WRT4_ITL(outbound_queue, req32);
700
701         return 0;
702 }
703
704 static int hptiop_get_config_mv(struct hpt_iop_hba * hba,
705                                 struct hpt_iop_request_get_config * config)
706 {
707         struct hpt_iop_request_get_config *req;
708
709         if (!(req = hba->ctlcfg_ptr))
710                 return -1;
711
712         req->header.flags = 0;
713         req->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
714         req->header.size = sizeof(struct hpt_iop_request_get_config);
715         req->header.result = IOP_RESULT_PENDING;
716         req->header.context = MVIOP_CMD_TYPE_GET_CONFIG;
717
718         if (hptiop_send_sync_request_mv(hba, req, 20000)) {
719                 KdPrint(("hptiop: get config send cmd failed"));
720                 return -1;
721         }
722
723         *config = *req;
724         return 0;
725 }
726
727 static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
728                                 struct hpt_iop_request_set_config *config)
729 {
730         u_int32_t req32;
731
732         req32 = BUS_SPACE_RD4_ITL(inbound_queue);
733
734         if (req32 == IOPMU_QUEUE_EMPTY)
735                 return -1;
736
737         config->header.size = sizeof(struct hpt_iop_request_set_config);
738         config->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
739         config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
740         config->header.result = IOP_RESULT_PENDING;
741         config->header.context = 0;
742
743         bus_space_write_region_4(hba->bar0t, hba->bar0h, req32,
744                 (u_int32_t *)config,
745                 sizeof(struct hpt_iop_request_set_config) >> 2);
746
747         if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
748                 KdPrint(("hptiop: set config send cmd failed"));
749                 return -1;
750         }
751
752         BUS_SPACE_WRT4_ITL(outbound_queue, req32);
753
754         return 0;
755 }
756
757 static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
758                                 struct hpt_iop_request_set_config *config)
759 {
760         struct hpt_iop_request_set_config *req;
761
762         if (!(req = hba->ctlcfg_ptr))
763                 return -1;
764
765         memcpy((u_int8_t *)req + sizeof(struct hpt_iop_request_header),
766                 (u_int8_t *)config + sizeof(struct hpt_iop_request_header),
767                 sizeof(struct hpt_iop_request_set_config) -
768                         sizeof(struct hpt_iop_request_header));
769
770         req->header.flags = 0;
771         req->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
772         req->header.size = sizeof(struct hpt_iop_request_set_config);
773         req->header.result = IOP_RESULT_PENDING;
774         req->header.context = MVIOP_CMD_TYPE_SET_CONFIG;
775
776         if (hptiop_send_sync_request_mv(hba, req, 20000)) {
777                 KdPrint(("hptiop: set config send cmd failed"));
778                 return -1;
779         }
780
781         return 0;
782 }
783
784 static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
785                                 u_int32_t req32,
786                                 struct hpt_iop_ioctl_param *pParams)
787 {
788         u_int64_t temp64;
789         struct hpt_iop_request_ioctl_command req;
790
791         if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
792                         (hba->max_request_size -
793                         offsetof(struct hpt_iop_request_ioctl_command, buf))) {
794                 device_printf(hba->pcidev, "request size beyond max value");
795                 return -1;
796         }
797
798         req.header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
799                 + pParams->nInBufferSize;
800         req.header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
801         req.header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
802         req.header.result = IOP_RESULT_PENDING;
803         req.header.context = req32 + (u_int64_t)(unsigned long)hba->u.itl.mu;
804         req.ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
805         req.inbuf_size = pParams->nInBufferSize;
806         req.outbuf_size = pParams->nOutBufferSize;
807         req.bytes_returned = 0;
808
809         bus_space_write_region_4(hba->bar0t, hba->bar0h, req32, (u_int32_t *)&req,
810                 offsetof(struct hpt_iop_request_ioctl_command, buf)>>2);
811
812         hptiop_lock_adapter(hba);
813
814         BUS_SPACE_WRT4_ITL(inbound_queue, req32);
815         BUS_SPACE_RD4_ITL(outbound_intstatus);
816
817         bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
818                 offsetof(struct hpt_iop_request_ioctl_command, header.context),
819                 (u_int32_t *)&temp64, 2);
820         while (temp64) {
821                 if (hptiop_sleep(hba, (void *)((unsigned long)hba->u.itl.mu + req32),
822                                 0, "hptctl", HPT_OSM_TIMEOUT)==0)
823                         break;
824                 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
825                 bus_space_read_region_4(hba->bar0t, hba->bar0h,req32 +
826                         offsetof(struct hpt_iop_request_ioctl_command,
827                                 header.context),
828                         (u_int32_t *)&temp64, 2);
829         }
830
831         hptiop_unlock_adapter(hba);
832         return 0;
833 }
834
835 static int hptiop_bus_space_copyin(struct hpt_iop_hba *hba, u_int32_t bus, void *user, int size)
836 {
837         unsigned char byte;
838         int i;
839
840         for (i=0; i<size; i++) {
841                 if (copyin((u_int8_t *)user + i, &byte, 1))
842                         return -1;
843                 bus_space_write_1(hba->bar0t, hba->bar0h, bus + i, byte);
844         }
845
846         return 0;
847 }
848
849 static int hptiop_bus_space_copyout(struct hpt_iop_hba *hba, u_int32_t bus, void *user, int size)
850 {
851         unsigned char byte;
852         int i;
853
854         for (i=0; i<size; i++) {
855                 byte = bus_space_read_1(hba->bar0t, hba->bar0h, bus + i);
856                 if (copyout(&byte, (u_int8_t *)user + i, 1))
857                         return -1;
858         }
859
860         return 0;
861 }
862
863 static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
864                                 struct hpt_iop_ioctl_param * pParams)
865 {
866         u_int32_t req32;
867         u_int32_t result;
868
869         if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
870                 (pParams->Magic != HPT_IOCTL_MAGIC32))
871                 return EFAULT;
872
873         req32 = BUS_SPACE_RD4_ITL(inbound_queue);
874         if (req32 == IOPMU_QUEUE_EMPTY)
875                 return EFAULT;
876
877         if (pParams->nInBufferSize)
878                 if (hptiop_bus_space_copyin(hba, req32 +
879                         offsetof(struct hpt_iop_request_ioctl_command, buf),
880                         (void *)pParams->lpInBuffer, pParams->nInBufferSize))
881                         goto invalid;
882
883         if (hptiop_post_ioctl_command_itl(hba, req32, pParams))
884                 goto invalid;
885
886         result = bus_space_read_4(hba->bar0t, hba->bar0h, req32 +
887                         offsetof(struct hpt_iop_request_ioctl_command,
888                                 header.result));
889
890         if (result == IOP_RESULT_SUCCESS) {
891                 if (pParams->nOutBufferSize)
892                         if (hptiop_bus_space_copyout(hba, req32 +
893                                 offsetof(struct hpt_iop_request_ioctl_command, buf) +
894                                         ((pParams->nInBufferSize + 3) & ~3),
895                                 (void *)pParams->lpOutBuffer, pParams->nOutBufferSize))
896                                 goto invalid;
897
898                 if (pParams->lpBytesReturned) {
899                         if (hptiop_bus_space_copyout(hba, req32 +
900                                 offsetof(struct hpt_iop_request_ioctl_command, bytes_returned),
901                                 (void *)pParams->lpBytesReturned, sizeof(unsigned  long)))
902                                 goto invalid;
903                 }
904
905                 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
906
907                 return 0;
908         } else{
909 invalid:
910                 BUS_SPACE_WRT4_ITL(outbound_queue, req32);
911
912                 return EFAULT;
913         }
914 }
915
916 static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
917                                 struct hpt_iop_request_ioctl_command *req,
918                                 struct hpt_iop_ioctl_param *pParams)
919 {
920         u_int64_t req_phy;
921         int size = 0;
922
923         if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
924                         (hba->max_request_size -
925                         offsetof(struct hpt_iop_request_ioctl_command, buf))) {
926                 device_printf(hba->pcidev, "request size beyond max value");
927                 return -1;
928         }
929
930         req->ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
931         req->inbuf_size = pParams->nInBufferSize;
932         req->outbuf_size = pParams->nOutBufferSize;
933         req->header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
934                                         + pParams->nInBufferSize;
935         req->header.context = (u_int64_t)MVIOP_CMD_TYPE_IOCTL;
936         req->header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
937         req->header.result = IOP_RESULT_PENDING;
938         req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
939         size = req->header.size >> 8;
940         size = size > 3 ? 3 : size;
941         req_phy = hba->ctlcfgcmd_phy | MVIOP_MU_QUEUE_ADDR_HOST_BIT | size;
942         hptiop_mv_inbound_write(req_phy, hba);
943
944         BUS_SPACE_RD4_MV0(outbound_intmask);
945
946         while (hba->config_done == 0) {
947                 if (hptiop_sleep(hba, req, 0,
948                         "hptctl", HPT_OSM_TIMEOUT)==0)
949                         continue;
950                 hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
951         }
952         return 0;
953 }
954
955 static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
956                                 struct hpt_iop_ioctl_param *pParams)
957 {
958         struct hpt_iop_request_ioctl_command *req;
959
960         if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
961                 (pParams->Magic != HPT_IOCTL_MAGIC32))
962                 return EFAULT;
963
964         req = (struct hpt_iop_request_ioctl_command *)(hba->ctlcfg_ptr);
965         hba->config_done = 0;
966         hptiop_lock_adapter(hba);
967         if (pParams->nInBufferSize)
968                 if (copyin((void *)pParams->lpInBuffer,
969                                 req->buf, pParams->nInBufferSize))
970                         goto invalid;
971         if (hptiop_post_ioctl_command_mv(hba, req, pParams))
972                 goto invalid;
973
974         if (hba->config_done == 1) {
975                 if (pParams->nOutBufferSize)
976                         if (copyout(req->buf +
977                                 ((pParams->nInBufferSize + 3) & ~3),
978                                 (void *)pParams->lpOutBuffer,
979                                 pParams->nOutBufferSize))
980                                 goto invalid;
981
982                 if (pParams->lpBytesReturned)
983                         if (copyout(&req->bytes_returned,
984                                 (void*)pParams->lpBytesReturned,
985                                 sizeof(u_int32_t)))
986                                 goto invalid;
987                 hptiop_unlock_adapter(hba);
988                 return 0;
989         } else{
990 invalid:
991                 hptiop_unlock_adapter(hba);
992                 return EFAULT;
993         }
994 }
995
996 static int  hptiop_rescan_bus(struct hpt_iop_hba * hba)
997 {
998         union ccb           *ccb;
999
1000         if ((ccb = xpt_alloc_ccb()) == NULL)
1001                 return(ENOMEM);
1002         if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(hba->sim),
1003                 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1004                 xpt_free_ccb(ccb);
1005                 return(EIO);
1006         }
1007
1008         xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/);
1009         ccb->ccb_h.func_code = XPT_SCAN_BUS;
1010         ccb->ccb_h.cbfcnp = hptiop_bus_scan_cb;
1011         ccb->crcn.flags = CAM_FLAG_NONE;
1012         xpt_action(ccb);
1013         return(0);
1014 }
1015
1016 static void hptiop_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
1017 {
1018         xpt_free_path(ccb->ccb_h.path);
1019         kfree(ccb, M_TEMP);
1020 }
1021
1022 static  bus_dmamap_callback_t   hptiop_map_srb;
1023 static  bus_dmamap_callback_t   hptiop_post_scsi_command;
1024 static  bus_dmamap_callback_t   hptiop_mv_map_ctlcfg;
1025
1026 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba)
1027 {
1028         hba->bar0_rid = 0x10;
1029         hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1030                         SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1031
1032         if (hba->bar0_res == NULL) {
1033                 device_printf(hba->pcidev,
1034                         "failed to get iop base adrress.\n");
1035                 return -1;
1036         }
1037         hba->bar0t = rman_get_bustag(hba->bar0_res);
1038         hba->bar0h = rman_get_bushandle(hba->bar0_res);
1039         hba->u.itl.mu = (struct hpt_iopmu_itl *)
1040                                 rman_get_virtual(hba->bar0_res);
1041
1042         if (!hba->u.itl.mu) {
1043                 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1044                                         hba->bar0_rid, hba->bar0_res);
1045                 device_printf(hba->pcidev, "alloc mem res failed\n");
1046                 return -1;
1047         }
1048
1049         return 0;
1050 }
1051
1052 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba)
1053 {
1054         hba->bar0_rid = 0x10;
1055         hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1056                         SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1057
1058         if (hba->bar0_res == NULL) {
1059                 device_printf(hba->pcidev, "failed to get iop bar0.\n");
1060                 return -1;
1061         }
1062         hba->bar0t = rman_get_bustag(hba->bar0_res);
1063         hba->bar0h = rman_get_bushandle(hba->bar0_res);
1064         hba->u.mv.regs = (struct hpt_iopmv_regs *)
1065                                 rman_get_virtual(hba->bar0_res);
1066
1067         if (!hba->u.mv.regs) {
1068                 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1069                                         hba->bar0_rid, hba->bar0_res);
1070                 device_printf(hba->pcidev, "alloc bar0 mem res failed\n");
1071                 return -1;
1072         }
1073
1074         hba->bar2_rid = 0x18;
1075         hba->bar2_res = bus_alloc_resource_any(hba->pcidev,
1076                         SYS_RES_MEMORY, &hba->bar2_rid, RF_ACTIVE);
1077
1078         if (hba->bar2_res == NULL) {
1079                 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1080                                         hba->bar0_rid, hba->bar0_res);
1081                 device_printf(hba->pcidev, "failed to get iop bar2.\n");
1082                 return -1;
1083         }
1084
1085         hba->bar2t = rman_get_bustag(hba->bar2_res);
1086         hba->bar2h = rman_get_bushandle(hba->bar2_res);
1087         hba->u.mv.mu = (struct hpt_iopmu_mv *)rman_get_virtual(hba->bar2_res);
1088
1089         if (!hba->u.mv.mu) {
1090                 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1091                                         hba->bar0_rid, hba->bar0_res);
1092                 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1093                                         hba->bar2_rid, hba->bar2_res);
1094                 device_printf(hba->pcidev, "alloc mem bar2 res failed\n");
1095                 return -1;
1096         }
1097
1098         return 0;
1099 }
1100
1101 static void hptiop_release_pci_res_itl(struct hpt_iop_hba *hba)
1102 {
1103         if (hba->bar0_res)
1104                 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1105                         hba->bar0_rid, hba->bar0_res);
1106 }
1107
1108 static void hptiop_release_pci_res_mv(struct hpt_iop_hba *hba)
1109 {
1110         if (hba->bar0_res)
1111                 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1112                         hba->bar0_rid, hba->bar0_res);
1113         if (hba->bar2_res)
1114                 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1115                         hba->bar2_rid, hba->bar2_res);
1116 }
1117
1118 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba)
1119 {
1120         if (bus_dma_tag_create(hba->parent_dmat,
1121                                 1,
1122                                 0,
1123                                 BUS_SPACE_MAXADDR_32BIT,
1124                                 BUS_SPACE_MAXADDR,
1125                                 NULL, NULL,
1126                                 0x800 - 0x8,
1127                                 1,
1128                                 BUS_SPACE_MAXSIZE_32BIT,
1129                                 BUS_DMA_ALLOCNOW,
1130                                 &hba->ctlcfg_dmat)) {
1131                 device_printf(hba->pcidev, "alloc ctlcfg_dmat failed\n");
1132                 return -1;
1133         }
1134
1135         if (bus_dmamem_alloc(hba->ctlcfg_dmat, (void **)&hba->ctlcfg_ptr,
1136                 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1137                 &hba->ctlcfg_dmamap) != 0) {
1138                         device_printf(hba->pcidev,
1139                                         "bus_dmamem_alloc failed!\n");
1140                         bus_dma_tag_destroy(hba->ctlcfg_dmat);
1141                         return -1;
1142         }
1143
1144         if (bus_dmamap_load(hba->ctlcfg_dmat,
1145                         hba->ctlcfg_dmamap, hba->ctlcfg_ptr,
1146                         MVIOP_IOCTLCFG_SIZE,
1147                         hptiop_mv_map_ctlcfg, hba, 0)) {
1148                 device_printf(hba->pcidev, "bus_dmamap_load failed!\n");
1149                 if (hba->ctlcfg_dmat)
1150                         bus_dmamem_free(hba->ctlcfg_dmat,
1151                                 hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1152                         bus_dma_tag_destroy(hba->ctlcfg_dmat);
1153                 return -1;
1154         }
1155
1156         return 0;
1157 }
1158
1159 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba)
1160 {
1161         if (hba->ctlcfg_dmat) {
1162                 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
1163                 bus_dmamem_free(hba->ctlcfg_dmat,
1164                                         hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1165                 bus_dma_tag_destroy(hba->ctlcfg_dmat);
1166         }
1167
1168         return 0;
1169 }
1170
1171 /*
1172  * CAM driver interface
1173  */
1174 static device_method_t driver_methods[] = {
1175         /* Device interface */
1176         DEVMETHOD(device_probe,     hptiop_probe),
1177         DEVMETHOD(device_attach,    hptiop_attach),
1178         DEVMETHOD(device_detach,    hptiop_detach),
1179         DEVMETHOD(device_shutdown,  hptiop_shutdown),
1180         { 0, 0 }
1181 };
1182
1183 static struct hptiop_adapter_ops hptiop_itl_ops = {
1184         .iop_wait_ready    = hptiop_wait_ready_itl,
1185         .internal_memalloc = 0,
1186         .internal_memfree  = 0,
1187         .alloc_pci_res     = hptiop_alloc_pci_res_itl,
1188         .release_pci_res   = hptiop_release_pci_res_itl,
1189         .enable_intr       = hptiop_enable_intr_itl,
1190         .disable_intr      = hptiop_disable_intr_itl,
1191         .get_config        = hptiop_get_config_itl,
1192         .set_config        = hptiop_set_config_itl,
1193         .iop_intr          = hptiop_intr_itl,
1194         .post_msg          = hptiop_post_msg_itl,
1195         .post_req          = hptiop_post_req_itl,
1196         .do_ioctl          = hptiop_do_ioctl_itl,
1197 };
1198
1199 static struct hptiop_adapter_ops hptiop_mv_ops = {
1200         .iop_wait_ready    = hptiop_wait_ready_mv,
1201         .internal_memalloc = hptiop_internal_memalloc_mv,
1202         .internal_memfree  = hptiop_internal_memfree_mv,
1203         .alloc_pci_res     = hptiop_alloc_pci_res_mv,
1204         .release_pci_res   = hptiop_release_pci_res_mv,
1205         .enable_intr       = hptiop_enable_intr_mv,
1206         .disable_intr      = hptiop_disable_intr_mv,
1207         .get_config        = hptiop_get_config_mv,
1208         .set_config        = hptiop_set_config_mv,
1209         .iop_intr          = hptiop_intr_mv,
1210         .post_msg          = hptiop_post_msg_mv,
1211         .post_req          = hptiop_post_req_mv,
1212         .do_ioctl          = hptiop_do_ioctl_mv,
1213 };
1214
1215 static driver_t hptiop_pci_driver = {
1216         driver_name,
1217         driver_methods,
1218         sizeof(struct hpt_iop_hba)
1219 };
1220
1221 DRIVER_MODULE(hptiop, pci, hptiop_pci_driver, hptiop_devclass, NULL, NULL);
1222
1223 static int hptiop_probe(device_t dev)
1224 {
1225         struct hpt_iop_hba *hba;
1226         u_int32_t id;
1227         static char buf[256];
1228         int sas = 0;
1229         struct hptiop_adapter_ops *ops;
1230
1231         if (pci_get_vendor(dev) != 0x1103)
1232                 return (ENXIO);
1233
1234         id = pci_get_device(dev);
1235
1236         switch (id) {
1237                 case 0x4320:
1238                         sas = 1;
1239                 case 0x3220:
1240                 case 0x3320:
1241                 case 0x3410:
1242                 case 0x3520:
1243                 case 0x3510:
1244                 case 0x3511:
1245                 case 0x3521:
1246                 case 0x3522:
1247                 case 0x3540:
1248                         ops = &hptiop_itl_ops;
1249                         break;
1250                 case 0x3120:
1251                 case 0x3122:
1252                 case 0x3020:
1253                         ops = &hptiop_mv_ops;
1254                         break;
1255                 default:
1256                         return (ENXIO);
1257         }
1258
1259         device_printf(dev, "adapter at PCI %d:%d:%d, IRQ %d\n",
1260                 pci_get_bus(dev), pci_get_slot(dev),
1261                 pci_get_function(dev), pci_get_irq(dev));
1262
1263         ksprintf(buf, "RocketRAID %x %s Controller",
1264                                 id, sas ? "SAS" : "SATA");
1265         device_set_desc_copy(dev, buf);
1266
1267         hba = (struct hpt_iop_hba *)device_get_softc(dev);
1268         bzero(hba, sizeof(struct hpt_iop_hba));
1269         hba->ops = ops;
1270
1271         KdPrint(("hba->ops=%p\n", hba->ops));
1272         return 0;
1273 }
1274
1275 static int hptiop_attach(device_t dev)
1276 {
1277         struct hpt_iop_hba *hba = (struct hpt_iop_hba *)device_get_softc(dev);
1278         struct hpt_iop_request_get_config  iop_config;
1279         struct hpt_iop_request_set_config  set_config;
1280         int rid = 0;
1281         struct cam_devq *devq;
1282         struct ccb_setasync ccb;
1283         u_int32_t unit = device_get_unit(dev);
1284
1285         device_printf(dev, "%d RocketRAID 3xxx/4xxx controller driver %s\n",
1286                         unit, driver_version);
1287
1288         KdPrint(("hptiop: attach(%d, %d/%d/%d) ops=%p\n", unit,
1289                 pci_get_bus(dev), pci_get_slot(dev),
1290                 pci_get_function(dev), hba->ops));
1291
1292         pci_enable_busmaster(dev);
1293         hba->pcidev = dev;
1294         hba->pciunit = unit;
1295
1296         if (hba->ops->alloc_pci_res(hba))
1297                 return ENXIO;
1298
1299         if (hba->ops->iop_wait_ready(hba, 2000)) {
1300                 device_printf(dev, "adapter is not ready\n");
1301                 goto release_pci_res;
1302         }
1303
1304         lockinit(&hba->lock, "hptioplock", 0, LK_CANRECURSE);
1305
1306         if (bus_dma_tag_create(NULL,/* parent */
1307                         1,  /* alignment */
1308                         0, /* boundary */
1309                         BUS_SPACE_MAXADDR,  /* lowaddr */
1310                         BUS_SPACE_MAXADDR,  /* highaddr */
1311                         NULL, NULL,         /* filter, filterarg */
1312                         BUS_SPACE_MAXSIZE_32BIT,    /* maxsize */
1313                         BUS_SPACE_UNRESTRICTED, /* nsegments */
1314                         BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
1315                         0,      /* flags */
1316                         &hba->parent_dmat   /* tag */))
1317         {
1318                 device_printf(dev, "alloc parent_dmat failed\n");
1319                 goto release_pci_res;
1320         }
1321
1322         if (hba->ops->internal_memalloc) {
1323                 if (hba->ops->internal_memalloc(hba)) {
1324                         device_printf(dev, "alloc srb_dmat failed\n");
1325                         goto destroy_parent_tag;
1326                 }
1327         }
1328
1329         if (hba->ops->get_config(hba, &iop_config)) {
1330                 device_printf(dev, "get iop config failed.\n");
1331                 goto get_config_failed;
1332         }
1333
1334         hba->firmware_version = iop_config.firmware_version;
1335         hba->interface_version = iop_config.interface_version;
1336         hba->max_requests = iop_config.max_requests;
1337         hba->max_devices = iop_config.max_devices;
1338         hba->max_request_size = iop_config.request_size;
1339         hba->max_sg_count = iop_config.max_sg_count;
1340
1341         if (bus_dma_tag_create(hba->parent_dmat,/* parent */
1342                         4,  /* alignment */
1343                         BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1344                         BUS_SPACE_MAXADDR,  /* lowaddr */
1345                         BUS_SPACE_MAXADDR,  /* highaddr */
1346                         NULL, NULL,         /* filter, filterarg */
1347                         PAGE_SIZE * (hba->max_sg_count-1),  /* maxsize */
1348                         hba->max_sg_count,  /* nsegments */
1349                         0x20000,    /* maxsegsize */
1350                         BUS_DMA_ALLOCNOW,       /* flags */
1351                         &hba->io_dmat   /* tag */))
1352         {
1353                 device_printf(dev, "alloc io_dmat failed\n");
1354                 goto get_config_failed;
1355         }
1356
1357         if (bus_dma_tag_create(hba->parent_dmat,/* parent */
1358                         1,  /* alignment */
1359                         0, /* boundary */
1360                         BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
1361                         BUS_SPACE_MAXADDR,  /* highaddr */
1362                         NULL, NULL,         /* filter, filterarg */
1363                         HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE + 0x20,
1364                         1,  /* nsegments */
1365                         BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
1366                         0,      /* flags */
1367                         &hba->srb_dmat  /* tag */))
1368         {
1369                 device_printf(dev, "alloc srb_dmat failed\n");
1370                 goto destroy_io_dmat;
1371         }
1372
1373         if (bus_dmamem_alloc(hba->srb_dmat, (void **)&hba->uncached_ptr,
1374                         BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1375                         &hba->srb_dmamap) != 0)
1376         {
1377                 device_printf(dev, "srb bus_dmamem_alloc failed!\n");
1378                 goto destroy_srb_dmat;
1379         }
1380
1381         if (bus_dmamap_load(hba->srb_dmat,
1382                         hba->srb_dmamap, hba->uncached_ptr,
1383                         (HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE) + 0x20,
1384                         hptiop_map_srb, hba, 0))
1385         {
1386                 device_printf(dev, "bus_dmamap_load failed!\n");
1387                 goto srb_dmamem_free;
1388         }
1389
1390         if ((devq = cam_simq_alloc(hba->max_requests - 1 )) == NULL) {
1391                 device_printf(dev, "cam_simq_alloc failed\n");
1392                 goto srb_dmamap_unload;
1393         }
1394
1395         hba->sim = cam_sim_alloc(hptiop_action, hptiop_poll, driver_name,
1396                         hba, unit, &sim_mplock, hba->max_requests - 1, 1, devq);
1397         if (!hba->sim) {
1398                 device_printf(dev, "cam_sim_alloc failed\n");
1399                 cam_simq_release(devq);
1400                 goto srb_dmamap_unload;
1401         }
1402         if (xpt_bus_register(hba->sim, 0) != CAM_SUCCESS)
1403         {
1404                 device_printf(dev, "xpt_bus_register failed\n");
1405                 goto free_cam_sim;
1406         }
1407
1408         if (xpt_create_path(&hba->path, /*periph */ NULL,
1409                         cam_sim_path(hba->sim), CAM_TARGET_WILDCARD,
1410                         CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1411                 device_printf(dev, "xpt_create_path failed\n");
1412                 goto deregister_xpt_bus;
1413         }
1414
1415         bzero(&set_config, sizeof(set_config));
1416         set_config.iop_id = unit;
1417         set_config.vbus_id = cam_sim_path(hba->sim);
1418         set_config.max_host_request_size = HPT_SRB_MAX_REQ_SIZE;
1419
1420         if (hba->ops->set_config(hba, &set_config)) {
1421                 device_printf(dev, "set iop config failed.\n");
1422                 goto free_hba_path;
1423         }
1424
1425         xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
1426         ccb.ccb_h.func_code = XPT_SASYNC_CB;
1427         ccb.event_enable = (AC_FOUND_DEVICE | AC_LOST_DEVICE);
1428         ccb.callback = hptiop_async;
1429         ccb.callback_arg = hba->sim;
1430         xpt_action((union ccb *)&ccb);
1431
1432         rid = 0;
1433         if ((hba->irq_res = bus_alloc_resource(hba->pcidev, SYS_RES_IRQ,
1434                         &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
1435                 device_printf(dev, "allocate irq failed!\n");
1436                 goto free_hba_path;
1437         }
1438
1439         if (bus_setup_intr(hba->pcidev, hba->irq_res, 0,
1440                                 hptiop_pci_intr, hba, &hba->irq_handle, NULL))
1441         {
1442                 device_printf(dev, "allocate intr function failed!\n");
1443                 goto free_irq_resource;
1444         }
1445
1446         if (hptiop_send_sync_msg(hba,
1447                         IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
1448                 device_printf(dev, "fail to start background task\n");
1449                 goto teartown_irq_resource;
1450         }
1451
1452         hba->ops->enable_intr(hba);
1453
1454         hba->ioctl_dev = make_dev(&hptiop_ops, unit,
1455                                 UID_ROOT, GID_WHEEL /*GID_OPERATOR*/,
1456                                 S_IRUSR | S_IWUSR, "%s%d", driver_name, unit);
1457
1458         hba->ioctl_dev->si_drv1 = hba;
1459
1460         hptiop_rescan_bus(hba);
1461
1462         return 0;
1463
1464
1465 teartown_irq_resource:
1466         bus_teardown_intr(dev, hba->irq_res, hba->irq_handle);
1467
1468 free_irq_resource:
1469         bus_release_resource(dev, SYS_RES_IRQ, 0, hba->irq_res);
1470
1471 free_hba_path:
1472         xpt_free_path(hba->path);
1473
1474 deregister_xpt_bus:
1475         xpt_bus_deregister(cam_sim_path(hba->sim));
1476
1477 free_cam_sim:
1478         cam_sim_free(hba->sim);
1479
1480 srb_dmamap_unload:
1481         if (hba->uncached_ptr)
1482                 bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
1483
1484 srb_dmamem_free:
1485         if (hba->uncached_ptr)
1486                 bus_dmamem_free(hba->srb_dmat,
1487                         hba->uncached_ptr, hba->srb_dmamap);
1488
1489 destroy_srb_dmat:
1490         if (hba->srb_dmat)
1491                 bus_dma_tag_destroy(hba->srb_dmat);
1492
1493 destroy_io_dmat:
1494         if (hba->io_dmat)
1495                 bus_dma_tag_destroy(hba->io_dmat);
1496
1497 get_config_failed:
1498         if (hba->ops->internal_memfree)
1499                 hba->ops->internal_memfree(hba);
1500
1501 destroy_parent_tag:
1502         if (hba->parent_dmat)
1503                 bus_dma_tag_destroy(hba->parent_dmat);
1504
1505 release_pci_res:
1506         if (hba->ops->release_pci_res)
1507                 hba->ops->release_pci_res(hba);
1508
1509         return ENXIO;
1510 }
1511
1512 static int hptiop_detach(device_t dev)
1513 {
1514         struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
1515         int i;
1516         int error = EBUSY;
1517
1518         hptiop_lock_adapter(hba);
1519         for (i = 0; i < hba->max_devices; i++)
1520                 if (hptiop_os_query_remove_device(hba, i)) {
1521                         device_printf(dev, "%d file system is busy. id=%d",
1522                                                 hba->pciunit, i);
1523                         goto out;
1524                 }
1525
1526         if ((error = hptiop_shutdown(dev)) != 0)
1527                 goto out;
1528         if (hptiop_send_sync_msg(hba,
1529                 IOPMU_INBOUND_MSG0_STOP_BACKGROUND_TASK, 60000))
1530                 goto out;
1531
1532         hptiop_release_resource(hba);
1533         error = 0;
1534 out:
1535         hptiop_unlock_adapter(hba);
1536         return error;
1537 }
1538
1539 static int hptiop_shutdown(device_t dev)
1540 {
1541         struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
1542
1543         int error = 0;
1544
1545         if (hba->flag & HPT_IOCTL_FLAG_OPEN) {
1546                 device_printf(dev, "%d device is busy", hba->pciunit);
1547                 return EBUSY;
1548         }
1549
1550         hba->ops->disable_intr(hba);
1551
1552         if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000))
1553                 error = EBUSY;
1554
1555         return error;
1556 }
1557
1558 static void hptiop_pci_intr(void *arg)
1559 {
1560         struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
1561         hptiop_lock_adapter(hba);
1562         hba->ops->iop_intr(hba);
1563         hptiop_unlock_adapter(hba);
1564 }
1565
1566 static void hptiop_poll(struct cam_sim *sim)
1567 {
1568         hptiop_pci_intr(cam_sim_softc(sim));
1569 }
1570
1571 static void hptiop_async(void * callback_arg, u_int32_t code,
1572                                         struct cam_path * path, void * arg)
1573 {
1574 }
1575
1576 static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba)
1577 {
1578         BUS_SPACE_WRT4_ITL(outbound_intmask,
1579                 ~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0));
1580 }
1581
1582 static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba)
1583 {
1584         u_int32_t int_mask;
1585
1586         int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
1587
1588         int_mask |= MVIOP_MU_OUTBOUND_INT_POSTQUEUE
1589                         | MVIOP_MU_OUTBOUND_INT_MSG;
1590         BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
1591 }
1592
1593 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba)
1594 {
1595         u_int32_t int_mask;
1596
1597         int_mask = BUS_SPACE_RD4_ITL(outbound_intmask);
1598
1599         int_mask |= IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0;
1600         BUS_SPACE_WRT4_ITL(outbound_intmask, int_mask);
1601         BUS_SPACE_RD4_ITL(outbound_intstatus);
1602 }
1603
1604 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba)
1605 {
1606         u_int32_t int_mask;
1607         int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
1608
1609         int_mask &= ~(MVIOP_MU_OUTBOUND_INT_MSG
1610                         | MVIOP_MU_OUTBOUND_INT_POSTQUEUE);
1611         BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
1612         BUS_SPACE_RD4_MV0(outbound_intmask);
1613 }
1614
1615 static int hptiop_reset_adapter(struct hpt_iop_hba * hba)
1616 {
1617         return hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
1618 }
1619
1620 static void *hptiop_get_srb(struct hpt_iop_hba * hba)
1621 {
1622         struct hpt_iop_srb * srb;
1623
1624         if (hba->srb_list) {
1625                 srb = hba->srb_list;
1626                 hba->srb_list = srb->next;
1627                 return srb;
1628         }
1629
1630         return NULL;
1631 }
1632
1633 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb)
1634 {
1635         srb->next = hba->srb_list;
1636         hba->srb_list = srb;
1637 }
1638
1639 static void hptiop_action(struct cam_sim *sim, union ccb *ccb)
1640 {
1641         struct hpt_iop_hba * hba = (struct hpt_iop_hba *)cam_sim_softc(sim);
1642         struct hpt_iop_srb * srb;
1643
1644         switch (ccb->ccb_h.func_code) {
1645
1646         case XPT_SCSI_IO:
1647                 hptiop_lock_adapter(hba);
1648                 if (ccb->ccb_h.target_lun != 0 ||
1649                         ccb->ccb_h.target_id >= hba->max_devices ||
1650                         (ccb->ccb_h.flags & CAM_CDB_PHYS))
1651                 {
1652                         ccb->ccb_h.status = CAM_TID_INVALID;
1653                         xpt_done(ccb);
1654                         goto scsi_done;
1655                 }
1656
1657                 if ((srb = hptiop_get_srb(hba)) == NULL) {
1658                         device_printf(hba->pcidev, "srb allocated failed");
1659                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1660                         xpt_done(ccb);
1661                         goto scsi_done;
1662                 }
1663
1664                 srb->ccb = ccb;
1665
1666                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1667                         hptiop_post_scsi_command(srb, NULL, 0, 0);
1668                 else if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
1669                         if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
1670                                 int error;
1671
1672                                 error = bus_dmamap_load(hba->io_dmat,
1673                                                 srb->dma_map,
1674                                                 ccb->csio.data_ptr,
1675                                                 ccb->csio.dxfer_len,
1676                                                 hptiop_post_scsi_command,
1677                                                 srb, 0);
1678
1679                                 if (error && error != EINPROGRESS) {
1680                                         device_printf(hba->pcidev,
1681                                                 "%d bus_dmamap_load error %d",
1682                                                 hba->pciunit, error);
1683                                         xpt_freeze_simq(hba->sim, 1);
1684                                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1685 invalid:
1686                                         hptiop_free_srb(hba, srb);
1687                                         xpt_done(ccb);
1688                                         goto scsi_done;
1689                                 }
1690                         }
1691                         else {
1692                                 device_printf(hba->pcidev,
1693                                         "CAM_DATA_PHYS not supported");
1694                                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1695                                 goto invalid;
1696                         }
1697                 }
1698                 else {
1699                         struct bus_dma_segment *segs;
1700
1701                         if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0 ||
1702                                 (ccb->ccb_h.flags & CAM_DATA_PHYS) != 0) {
1703                                 device_printf(hba->pcidev, "SCSI cmd failed");
1704                                 ccb->ccb_h.status=CAM_PROVIDE_FAIL;
1705                                 goto invalid;
1706                         }
1707
1708                         segs = (struct bus_dma_segment *)ccb->csio.data_ptr;
1709                         hptiop_post_scsi_command(srb, segs,
1710                                                 ccb->csio.sglist_cnt, 0);
1711                 }
1712
1713 scsi_done:
1714                 hptiop_unlock_adapter(hba);
1715                 return;
1716
1717         case XPT_RESET_BUS:
1718                 device_printf(hba->pcidev, "reset adapter");
1719                 hptiop_lock_adapter(hba);
1720                 hba->msg_done = 0;
1721                 hptiop_reset_adapter(hba);
1722                 hptiop_unlock_adapter(hba);
1723                 break;
1724
1725         case XPT_GET_TRAN_SETTINGS:
1726         case XPT_SET_TRAN_SETTINGS:
1727                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1728                 break;
1729
1730         case XPT_CALC_GEOMETRY:
1731                 ccb->ccg.heads = 255;
1732                 ccb->ccg.secs_per_track = 63;
1733                 ccb->ccg.cylinders = ccb->ccg.volume_size /
1734                                 (ccb->ccg.heads * ccb->ccg.secs_per_track);
1735                 ccb->ccb_h.status = CAM_REQ_CMP;
1736                 break;
1737
1738         case XPT_PATH_INQ:
1739         {
1740                 struct ccb_pathinq *cpi = &ccb->cpi;
1741
1742                 cpi->version_num = 1;
1743                 cpi->hba_inquiry = PI_SDTR_ABLE;
1744                 cpi->target_sprt = 0;
1745                 cpi->hba_misc = PIM_NOBUSRESET;
1746                 cpi->hba_eng_cnt = 0;
1747                 cpi->max_target = hba->max_devices;
1748                 cpi->max_lun = 0;
1749                 cpi->unit_number = cam_sim_unit(sim);
1750                 cpi->bus_id = cam_sim_bus(sim);
1751                 cpi->initiator_id = hba->max_devices;
1752                 cpi->base_transfer_speed = 3300;
1753
1754                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1755                 strncpy(cpi->hba_vid, "HPT   ", HBA_IDLEN);
1756                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1757                 cpi->transport = XPORT_SPI;
1758                 cpi->transport_version = 2;
1759                 cpi->protocol = PROTO_SCSI;
1760                 cpi->protocol_version = SCSI_REV_2;
1761                 cpi->ccb_h.status = CAM_REQ_CMP;
1762                 break;
1763         }
1764
1765         default:
1766                 ccb->ccb_h.status = CAM_REQ_INVALID;
1767                 break;
1768         }
1769
1770         xpt_done(ccb);
1771         return;
1772 }
1773
1774 static void hptiop_post_req_itl(struct hpt_iop_hba *hba,
1775                                 struct hpt_iop_srb *srb,
1776                                 bus_dma_segment_t *segs, int nsegs)
1777 {
1778         int idx;
1779         union ccb *ccb = srb->ccb;
1780         u_int8_t *cdb;
1781
1782         if (ccb->ccb_h.flags & CAM_CDB_POINTER)
1783                 cdb = ccb->csio.cdb_io.cdb_ptr;
1784         else
1785                 cdb = ccb->csio.cdb_io.cdb_bytes;
1786
1787         KdPrint(("ccb=%p %x-%x-%x\n",
1788                 ccb, *(u_int32_t *)cdb, *((u_int32_t *)cdb+1), *((u_int32_t *)cdb+2)));
1789
1790         if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {
1791                 u_int32_t iop_req32;
1792                 struct hpt_iop_request_scsi_command req;
1793
1794                 iop_req32 = BUS_SPACE_RD4_ITL(inbound_queue);
1795
1796                 if (iop_req32 == IOPMU_QUEUE_EMPTY) {
1797                         device_printf(hba->pcidev, "invaild req offset\n");
1798                         ccb->ccb_h.status = CAM_BUSY;
1799                         bus_dmamap_unload(hba->io_dmat, srb->dma_map);
1800                         hptiop_free_srb(hba, srb);
1801                         xpt_done(ccb);
1802                         return;
1803                 }
1804
1805                 if (ccb->csio.dxfer_len && nsegs > 0) {
1806                         struct hpt_iopsg *psg = req.sg_list;
1807                         for (idx = 0; idx < nsegs; idx++, psg++) {
1808                                 psg->pci_address = (u_int64_t)segs[idx].ds_addr;
1809                                 psg->size = segs[idx].ds_len;
1810                                 psg->eot = 0;
1811                         }
1812                         psg[-1].eot = 1;
1813                 }
1814
1815                 bcopy(cdb, req.cdb, ccb->csio.cdb_len);
1816
1817                 req.header.size = offsetof(struct hpt_iop_request_scsi_command, sg_list)
1818                                 + nsegs*sizeof(struct hpt_iopsg);
1819                 req.header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
1820                 req.header.flags = 0;
1821                 req.header.result = IOP_RESULT_PENDING;
1822                 req.header.context = (u_int64_t)(unsigned long)srb;
1823                 req.dataxfer_length = ccb->csio.dxfer_len;
1824                 req.channel =  0;
1825                 req.target =  ccb->ccb_h.target_id;
1826                 req.lun =  ccb->ccb_h.target_lun;
1827
1828                 bus_space_write_region_1(hba->bar0t, hba->bar0h, iop_req32,
1829                         (u_int8_t *)&req, req.header.size);
1830
1831                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1832                         bus_dmamap_sync(hba->io_dmat,
1833                                 srb->dma_map, BUS_DMASYNC_PREREAD);
1834                 }
1835                 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1836                         bus_dmamap_sync(hba->io_dmat,
1837                                 srb->dma_map, BUS_DMASYNC_PREWRITE);
1838
1839                 BUS_SPACE_WRT4_ITL(inbound_queue,iop_req32);
1840         } else {
1841                 struct hpt_iop_request_scsi_command *req;
1842
1843                 req = (struct hpt_iop_request_scsi_command *)srb;
1844                 if (ccb->csio.dxfer_len && nsegs > 0) {
1845                         struct hpt_iopsg *psg = req->sg_list;
1846                         for (idx = 0; idx < nsegs; idx++, psg++) {
1847                                 psg->pci_address =
1848                                         (u_int64_t)segs[idx].ds_addr;
1849                                 psg->size = segs[idx].ds_len;
1850                                 psg->eot = 0;
1851                         }
1852                         psg[-1].eot = 1;
1853                 }
1854
1855                 bcopy(cdb, req->cdb, ccb->csio.cdb_len);
1856
1857                 req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
1858                 req->header.result = IOP_RESULT_PENDING;
1859                 req->dataxfer_length = ccb->csio.dxfer_len;
1860                 req->channel =  0;
1861                 req->target =  ccb->ccb_h.target_id;
1862                 req->lun =  ccb->ccb_h.target_lun;
1863                 req->header.size = offsetof(struct hpt_iop_request_scsi_command, sg_list)
1864                         + nsegs*sizeof(struct hpt_iopsg);
1865                 req->header.context = (u_int64_t)srb->index |
1866                                                 IOPMU_QUEUE_ADDR_HOST_BIT;
1867                 req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
1868
1869                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1870                         bus_dmamap_sync(hba->io_dmat,
1871                                 srb->dma_map, BUS_DMASYNC_PREREAD);
1872                 }else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1873                         bus_dmamap_sync(hba->io_dmat,
1874                                 srb->dma_map, BUS_DMASYNC_PREWRITE);
1875                 }
1876
1877                 if (hba->firmware_version > 0x01020000
1878                         || hba->interface_version > 0x01020000) {
1879                         u_int32_t size_bits;
1880
1881                         if (req->header.size < 256)
1882                                 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT;
1883                         else if (req->header.size < 512)
1884                                 size_bits = IOPMU_QUEUE_ADDR_HOST_BIT;
1885                         else
1886                                 size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT
1887                                                 | IOPMU_QUEUE_ADDR_HOST_BIT;
1888
1889                         BUS_SPACE_WRT4_ITL(inbound_queue,
1890                                 (u_int32_t)srb->phy_addr | size_bits);
1891                 } else
1892                         BUS_SPACE_WRT4_ITL(inbound_queue, (u_int32_t)srb->phy_addr
1893                                 |IOPMU_QUEUE_ADDR_HOST_BIT);
1894         }
1895 }
1896
1897 static void hptiop_post_req_mv(struct hpt_iop_hba *hba,
1898                                 struct hpt_iop_srb *srb,
1899                                 bus_dma_segment_t *segs, int nsegs)
1900 {
1901         int idx, size;
1902         union ccb *ccb = srb->ccb;
1903         u_int8_t *cdb;
1904         struct hpt_iop_request_scsi_command *req;
1905         u_int64_t req_phy;
1906
1907         req = (struct hpt_iop_request_scsi_command *)srb;
1908         req_phy = srb->phy_addr;
1909
1910         if (ccb->csio.dxfer_len && nsegs > 0) {
1911                 struct hpt_iopsg *psg = req->sg_list;
1912                 for (idx = 0; idx < nsegs; idx++, psg++) {
1913                         psg->pci_address = (u_int64_t)segs[idx].ds_addr;
1914                         psg->size = segs[idx].ds_len;
1915                         psg->eot = 0;
1916                 }
1917                 psg[-1].eot = 1;
1918         }
1919         if (ccb->ccb_h.flags & CAM_CDB_POINTER)
1920                 cdb = ccb->csio.cdb_io.cdb_ptr;
1921         else
1922                 cdb = ccb->csio.cdb_io.cdb_bytes;
1923
1924         bcopy(cdb, req->cdb, ccb->csio.cdb_len);
1925         req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
1926         req->header.result = IOP_RESULT_PENDING;
1927         req->dataxfer_length = ccb->csio.dxfer_len;
1928         req->channel = 0;
1929         req->target =  ccb->ccb_h.target_id;
1930         req->lun =  ccb->ccb_h.target_lun;
1931         req->header.size = sizeof(struct hpt_iop_request_scsi_command)
1932                                 - sizeof(struct hpt_iopsg)
1933                                 + nsegs * sizeof(struct hpt_iopsg);
1934         if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1935                 bus_dmamap_sync(hba->io_dmat,
1936                         srb->dma_map, BUS_DMASYNC_PREREAD);
1937         }
1938         else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1939                 bus_dmamap_sync(hba->io_dmat,
1940                         srb->dma_map, BUS_DMASYNC_PREWRITE);
1941         req->header.context = (u_int64_t)srb->index
1942                                         << MVIOP_REQUEST_NUMBER_START_BIT
1943                                         | MVIOP_CMD_TYPE_SCSI;
1944         req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
1945         size = req->header.size >> 8;
1946         hptiop_mv_inbound_write(req_phy
1947                         | MVIOP_MU_QUEUE_ADDR_HOST_BIT
1948                         | (size > 3 ? 3 : size), hba);
1949 }
1950
1951 static void hptiop_post_scsi_command(void *arg, bus_dma_segment_t *segs,
1952                                         int nsegs, int error)
1953 {
1954         struct hpt_iop_srb *srb = (struct hpt_iop_srb *)arg;
1955         union ccb *ccb = srb->ccb;
1956         struct hpt_iop_hba *hba = srb->hba;
1957
1958         if (error || nsegs > hba->max_sg_count) {
1959                 KdPrint(("hptiop: func_code=%x tid=%x lun=%x nsegs=%d\n",
1960                         ccb->ccb_h.func_code,
1961                         ccb->ccb_h.target_id,
1962                         ccb->ccb_h.target_lun, nsegs));
1963                 ccb->ccb_h.status = CAM_BUSY;
1964                 bus_dmamap_unload(hba->io_dmat, srb->dma_map);
1965                 hptiop_free_srb(hba, srb);
1966                 xpt_done(ccb);
1967                 return;
1968         }
1969
1970         hba->ops->post_req(hba, srb, segs, nsegs);
1971 }
1972
1973 static void hptiop_mv_map_ctlcfg(void *arg, bus_dma_segment_t *segs,
1974                                 int nsegs, int error)
1975 {
1976         struct hpt_iop_hba *hba = (struct hpt_iop_hba *)arg;
1977         hba->ctlcfgcmd_phy = ((u_int64_t)segs->ds_addr + 0x1F)
1978                                 & ~(u_int64_t)0x1F;
1979         hba->ctlcfg_ptr = (u_int8_t *)(((unsigned long)hba->ctlcfg_ptr + 0x1F)
1980                                 & ~0x1F);
1981 }
1982
1983 static void hptiop_map_srb(void *arg, bus_dma_segment_t *segs,
1984                                 int nsegs, int error)
1985 {
1986         struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
1987         bus_addr_t phy_addr = (segs->ds_addr + 0x1F) & ~(bus_addr_t)0x1F;
1988         struct hpt_iop_srb *srb, *tmp_srb;
1989         int i;
1990
1991         if (error || nsegs == 0) {
1992                 device_printf(hba->pcidev, "hptiop_map_srb error");
1993                 return;
1994         }
1995
1996         /* map srb */
1997         srb = (struct hpt_iop_srb *)
1998                 (((unsigned long)hba->uncached_ptr + 0x1F)
1999                 & ~(unsigned long)0x1F);
2000
2001         for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
2002                 tmp_srb = (struct hpt_iop_srb *)
2003                                         ((char *)srb + i * HPT_SRB_MAX_SIZE);
2004                 if (((unsigned long)tmp_srb & 0x1F) == 0) {
2005                         if (bus_dmamap_create(hba->io_dmat,
2006                                                 0, &tmp_srb->dma_map)) {
2007                                 device_printf(hba->pcidev, "dmamap create failed");
2008                                 return;
2009                         }
2010
2011                         bzero(tmp_srb, sizeof(struct hpt_iop_srb));
2012                         tmp_srb->hba = hba;
2013                         tmp_srb->index = i;
2014                         if (hba->ctlcfg_ptr == 0) {/*itl iop*/
2015                                 tmp_srb->phy_addr = (u_int64_t)(u_int32_t)
2016                                                         (phy_addr >> 5);
2017                                 if (phy_addr & IOPMU_MAX_MEM_SUPPORT_MASK_32G)
2018                                         tmp_srb->srb_flag =
2019                                                 HPT_SRB_FLAG_HIGH_MEM_ACESS;
2020                         } else {
2021                                 tmp_srb->phy_addr = phy_addr;
2022                         }
2023
2024                         hptiop_free_srb(hba, tmp_srb);
2025                         hba->srb[i] = tmp_srb;
2026                         phy_addr += HPT_SRB_MAX_SIZE;
2027                 }
2028                 else {
2029                         device_printf(hba->pcidev, "invalid alignment");
2030                         return;
2031                 }
2032         }
2033 }
2034
2035 static void hptiop_os_message_callback(struct hpt_iop_hba * hba, u_int32_t msg)
2036 {
2037                 hba->msg_done = 1;
2038 }
2039
2040 static  int hptiop_os_query_remove_device(struct hpt_iop_hba * hba,
2041                                                 int target_id)
2042 {
2043         struct cam_periph       *periph = NULL;
2044         struct cam_path         *path;
2045         int                     status, retval = 0;
2046
2047         status = xpt_create_path(&path, NULL, hba->sim->path_id, target_id, 0);
2048
2049         if (status == CAM_REQ_CMP) {
2050                 if ((periph = cam_periph_find(path, "da")) != NULL) {
2051                         if (periph->refcount >= 1) {
2052                                 device_printf(hba->pcidev, "%d ,"
2053                                         "target_id=0x%x,"
2054                                         "refcount=%d",
2055                                     hba->pciunit, target_id, periph->refcount);
2056                                 retval = -1;
2057                         }
2058                 }
2059                 xpt_free_path(path);
2060         }
2061         return retval;
2062 }
2063
2064 static void hptiop_release_resource(struct hpt_iop_hba *hba)
2065 {
2066         int i;
2067         if (hba->path) {
2068                 struct ccb_setasync ccb;
2069
2070                 xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
2071                 ccb.ccb_h.func_code = XPT_SASYNC_CB;
2072                 ccb.event_enable = 0;
2073                 ccb.callback = hptiop_async;
2074                 ccb.callback_arg = hba->sim;
2075                 xpt_action((union ccb *)&ccb);
2076                 xpt_free_path(hba->path);
2077         }
2078
2079         if (hba->sim) {
2080                 xpt_bus_deregister(cam_sim_path(hba->sim));
2081                 cam_sim_free(hba->sim);
2082         }
2083
2084         if (hba->ctlcfg_dmat) {
2085                 bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
2086                 bus_dmamem_free(hba->ctlcfg_dmat,
2087                                         hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
2088                 bus_dma_tag_destroy(hba->ctlcfg_dmat);
2089         }
2090
2091         for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
2092                 struct hpt_iop_srb *srb = hba->srb[i];
2093                 if (srb->dma_map)
2094                         bus_dmamap_destroy(hba->io_dmat, srb->dma_map);
2095         }
2096
2097         if (hba->srb_dmat) {
2098                 bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
2099                 bus_dmamap_destroy(hba->srb_dmat, hba->srb_dmamap);
2100                 bus_dma_tag_destroy(hba->srb_dmat);
2101         }
2102
2103         if (hba->io_dmat)
2104                 bus_dma_tag_destroy(hba->io_dmat);
2105
2106         if (hba->parent_dmat)
2107                 bus_dma_tag_destroy(hba->parent_dmat);
2108
2109         if (hba->irq_handle)
2110                 bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
2111
2112         if (hba->irq_res)
2113                 bus_release_resource(hba->pcidev, SYS_RES_IRQ,
2114                                         0, hba->irq_res);
2115
2116         if (hba->bar0_res)
2117                 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
2118                                         hba->bar0_rid, hba->bar0_res);
2119         if (hba->bar2_res)
2120                 bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
2121                                         hba->bar2_rid, hba->bar2_res);
2122         if (hba->ioctl_dev)
2123                 destroy_dev(hba->ioctl_dev);
2124         dev_ops_remove_minor(&hptiop_ops, device_get_unit(hba->pcidev));
2125 }