1 /*
2 * Copyright (c) HighPoint Technologies, Inc.
3 * 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: releng/7.3/sys/dev/hptrr/hptrr_osm_bsd.c 199836 2009-11-26 15:18:05Z mav $
27 */
28 #include <dev/hptrr/hptrr_config.h>
29 /* $Id: osm_bsd.c,v 1.27 2007/11/22 07:35:49 gmm Exp $
30 *
31 * HighPoint RAID Driver for FreeBSD
32 * Copyright (C) 2005 HighPoint Technologies, Inc. All Rights Reserved.
33 */
34 #include <dev/hptrr/os_bsd.h>
35 #include <dev/hptrr/hptintf.h>
36
37 static int attach_generic = 1;
38 TUNABLE_INT("hw.hptrr.attach_generic", &attach_generic);
39
40 static int hpt_probe(device_t dev)
41 {
42 PCI_ID pci_id;
43 HIM *him;
44 int i;
45 PHBA hba;
46
47 /* Some of supported chips are used not only by HPT. */
48 if (pci_get_vendor(dev) != 0x1103 && !attach_generic)
49 return (ENXIO);
50 for (him = him_list; him; him = him->next) {
51 for (i=0; him->get_supported_device_id(i, &pci_id); i++) {
52 if ((pci_get_vendor(dev) == pci_id.vid) &&
53 (pci_get_device(dev) == pci_id.did)){
54 KdPrint(("hpt_probe: adapter at PCI %d:%d:%d, IRQ %d",
55 pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev), pci_get_irq(dev)
56 ));
57 device_set_desc(dev, him->name);
58 hba = (PHBA)device_get_softc(dev);
59 memset(hba, 0, sizeof(HBA));
60 hba->ext_type = EXT_TYPE_HBA;
61 hba->ldm_adapter.him = him;
62 return 0;
63 }
64 }
65 }
66
67 return (ENXIO);
68 }
69
70 static int hpt_attach(device_t dev)
71 {
72 PHBA hba = (PHBA)device_get_softc(dev);
73 HIM *him = hba->ldm_adapter.him;
74 PCI_ID pci_id;
75 HPT_UINT size;
76 PVBUS vbus;
77 PVBUS_EXT vbus_ext;
78
79 KdPrint(("hpt_attach(%d/%d/%d)", pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)));
80
81 #if __FreeBSD_version >=440000
82 pci_enable_busmaster(dev);
83 #endif
84
85 pci_id.vid = pci_get_vendor(dev);
86 pci_id.did = pci_get_device(dev);
87 pci_id.rev = pci_get_revid(dev);
88
89 size = him->get_adapter_size(&pci_id);
90 hba->ldm_adapter.him_handle = malloc(size, M_DEVBUF, M_WAITOK);
91 if (!hba->ldm_adapter.him_handle)
92 return ENXIO;
93
94 hba->pcidev = dev;
95 hba->pciaddr.tree = 0;
96 hba->pciaddr.bus = pci_get_bus(dev);
97 hba->pciaddr.device = pci_get_slot(dev);
98 hba->pciaddr.function = pci_get_function(dev);
99
100 if (!him->create_adapter(&pci_id, hba->pciaddr, hba->ldm_adapter.him_handle, hba)) {
101 free(hba->ldm_adapter.him_handle, M_DEVBUF);
102 return -1;
103 }
104
105 os_printk("adapter at PCI %d:%d:%d, IRQ %d",
106 hba->pciaddr.bus, hba->pciaddr.device, hba->pciaddr.function, pci_get_irq(dev));
107
108 if (!ldm_register_adapter(&hba->ldm_adapter)) {
109 size = ldm_get_vbus_size();
110 vbus_ext = malloc(sizeof(VBUS_EXT) + size, M_DEVBUF, M_WAITOK);
111 if (!vbus_ext) {
112 free(hba->ldm_adapter.him_handle, M_DEVBUF);
113 return -1;
114 }
115 memset(vbus_ext, 0, sizeof(VBUS_EXT));
116 vbus_ext->ext_type = EXT_TYPE_VBUS;
117 ldm_create_vbus((PVBUS)vbus_ext->vbus, vbus_ext);
118 ldm_register_adapter(&hba->ldm_adapter);
119 }
120
121 ldm_for_each_vbus(vbus, vbus_ext) {
122 if (hba->ldm_adapter.vbus==vbus) {
123 hba->vbus_ext = vbus_ext;
124 hba->next = vbus_ext->hba_list;
125 vbus_ext->hba_list = hba;
126 break;
127 }
128 }
129 return 0;
130 }
131
132 /*
133 * Maybe we'd better to use the bus_dmamem_alloc to alloc DMA memory,
134 * but there are some problems currently (alignment, etc).
135 */
136 static __inline void *__get_free_pages(int order)
137 {
138 /* don't use low memory - other devices may get starved */
139 return contigmalloc(PAGE_SIZE<<order,
140 M_DEVBUF, M_WAITOK, BUS_SPACE_MAXADDR_24BIT, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
141 }
142
143 static __inline void free_pages(void *p, int order)
144 {
145 contigfree(p, PAGE_SIZE<<order, M_DEVBUF);
146 }
147
148 static int hpt_alloc_mem(PVBUS_EXT vbus_ext)
149 {
150 PHBA hba;
151 struct freelist *f;
152 HPT_UINT i;
153 void **p;
154
155 for (hba = vbus_ext->hba_list; hba; hba = hba->next)
156 hba->ldm_adapter.him->get_meminfo(hba->ldm_adapter.him_handle);
157
158 ldm_get_mem_info((PVBUS)vbus_ext->vbus, 0);
159
160 for (f=vbus_ext->freelist_head; f; f=f->next) {
161 KdPrint(("%s: %d*%d=%d bytes",
162 f->tag, f->count, f->size, f->count*f->size));
163 for (i=0; i<f->count; i++) {
164 p = (void **)malloc(f->size, M_DEVBUF, M_WAITOK);
165 if (!p) return (ENXIO);
166 *p = f->head;
167 f->head = p;
168 }
169 }
170
171 for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
172 int order, size, j;
173
174 HPT_ASSERT((f->size & (f->alignment-1))==0);
175
176 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1) ;
177
178 KdPrint(("%s: %d*%d=%d bytes, order %d",
179 f->tag, f->count, f->size, f->count*f->size, order));
180 HPT_ASSERT(f->alignment<=PAGE_SIZE);
181
182 for (i=0; i<f->count;) {
183 p = (void **)__get_free_pages(order);
184 if (!p) return -1;
185 for (j = size/f->size; j && i<f->count; i++,j--) {
186 *p = f->head;
187 *(BUS_ADDRESS *)(p+1) = (BUS_ADDRESS)vtophys(p);
188 f->head = p;
189 p = (void **)((unsigned long)p + f->size);
190 }
191 }
192 }
193
194 HPT_ASSERT(PAGE_SIZE==DMAPOOL_PAGE_SIZE);
195
196 for (i=0; i<os_max_cache_pages; i++) {
197 p = (void **)__get_free_pages(0);
198 if (!p) return -1;
199 HPT_ASSERT(((HPT_UPTR)p & (DMAPOOL_PAGE_SIZE-1))==0);
200 dmapool_put_page((PVBUS)vbus_ext->vbus, p, (BUS_ADDRESS)vtophys(p));
201 }
202
203 return 0;
204 }
205
206 static void hpt_free_mem(PVBUS_EXT vbus_ext)
207 {
208 struct freelist *f;
209 void *p;
210 int i;
211 BUS_ADDRESS bus;
212
213 for (f=vbus_ext->freelist_head; f; f=f->next) {
214 #if DBG
215 if (f->count!=f->reserved_count) {
216 KdPrint(("memory leak for freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
217 }
218 #endif
219 while ((p=freelist_get(f)))
220 free(p, M_DEVBUF);
221 }
222
223 for (i=0; i<os_max_cache_pages; i++) {
224 p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus);
225 HPT_ASSERT(p);
226 free_pages(p, 0);
227 }
228
229 for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
230 int order, size;
231 #if DBG
232 if (f->count!=f->reserved_count) {
233 KdPrint(("memory leak for dma freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
234 }
235 #endif
236 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1) ;
237
238 while ((p=freelist_get_dma(f, &bus))) {
239 if (order)
240 free_pages(p, order);
241 else {
242 /* can't free immediately since other blocks in this page may still be in the list */
243 if (((HPT_UPTR)p & (PAGE_SIZE-1))==0)
244 dmapool_put_page((PVBUS)vbus_ext->vbus, p, bus);
245 }
246 }
247 }
248
249 while ((p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus)))
250 free_pages(p, 0);
251 }
252
253 static int hpt_init_vbus(PVBUS_EXT vbus_ext)
254 {
255 PHBA hba;
256
257 for (hba = vbus_ext->hba_list; hba; hba = hba->next)
258 if (!hba->ldm_adapter.him->initialize(hba->ldm_adapter.him_handle)) {
259 KdPrint(("fail to initialize %p", hba));
260 return -1;
261 }
262
263 ldm_initialize_vbus((PVBUS)vbus_ext->vbus, &vbus_ext->hba_list->ldm_adapter);
264 return 0;
265 }
266
267 static void hpt_flush_done(PCOMMAND pCmd)
268 {
269 PVDEV vd = pCmd->target;
270
271 if (mIsArray(vd->type) && vd->u.array.transform && vd!=vd->u.array.transform->target) {
272 vd = vd->u.array.transform->target;
273 HPT_ASSERT(vd);
274 pCmd->target = vd;
275 pCmd->Result = RETURN_PENDING;
276 vdev_queue_cmd(pCmd);
277 return;
278 }
279
280 *(int *)pCmd->priv = 1;
281 wakeup(pCmd);
282 }
283
284 /*
285 * flush a vdev (without retry).
286 */
287 static int hpt_flush_vdev(PVBUS_EXT vbus_ext, PVDEV vd)
288 {
289 PCOMMAND pCmd;
290 int result = 0, done;
291 HPT_UINT count;
292
293 KdPrint(("flusing dev %p", vd));
294
295 hpt_lock_vbus(vbus_ext);
296
297 if (mIsArray(vd->type) && vd->u.array.transform)
298 count = MAX(vd->u.array.transform->source->cmds_per_request,
299 vd->u.array.transform->target->cmds_per_request);
300 else
301 count = vd->cmds_per_request;
302
303 pCmd = ldm_alloc_cmds(vd->vbus, count);
304
305 if (!pCmd) {
306 hpt_unlock_vbus(vbus_ext);
307 return -1;
308 }
309
310 pCmd->type = CMD_TYPE_FLUSH;
311 pCmd->flags.hard_flush = 1;
312 pCmd->target = vd;
313 pCmd->done = hpt_flush_done;
314 done = 0;
315 pCmd->priv = &done;
316
317 ldm_queue_cmd(pCmd);
318
319 if (!done) {
320 while (hpt_sleep(vbus_ext, pCmd, PPAUSE, "hptfls", HPT_OSM_TIMEOUT)) {
321 ldm_reset_vbus(vd->vbus);
322 }
323 }
324
325 KdPrint(("flush result %d", pCmd->Result));
326
327 if (pCmd->Result!=RETURN_SUCCESS)
328 result = -1;
329
330 ldm_free_cmds(pCmd);
331
332 hpt_unlock_vbus(vbus_ext);
333
334 return result;
335 }
336
337 static void hpt_stop_tasks(PVBUS_EXT vbus_ext);
338 static void hpt_shutdown_vbus(PVBUS_EXT vbus_ext, int howto)
339 {
340 PVBUS vbus = (PVBUS)vbus_ext->vbus;
341 PHBA hba;
342 int i;
343
344 KdPrint(("hpt_shutdown_vbus"));
345
346 /* stop all ctl tasks and disable the worker taskqueue */
347 hpt_stop_tasks(vbus_ext);
348 vbus_ext->worker.ta_context = 0;
349
350 /* flush devices */
351 for (i=0; i<osm_max_targets; i++) {
352 PVDEV vd = ldm_find_target(vbus, i);
353 if (vd) {
354 /* retry once */
355 if (hpt_flush_vdev(vbus_ext, vd))
356 hpt_flush_vdev(vbus_ext, vd);
357 }
358 }
359
360 hpt_lock_vbus(vbus_ext);
361 ldm_shutdown(vbus);
362 hpt_unlock_vbus(vbus_ext);
363
364 ldm_release_vbus(vbus);
365
366 for (hba=vbus_ext->hba_list; hba; hba=hba->next)
367 bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
368
369 hpt_free_mem(vbus_ext);
370
371 while ((hba=vbus_ext->hba_list)) {
372 vbus_ext->hba_list = hba->next;
373 free(hba->ldm_adapter.him_handle, M_DEVBUF);
374 }
375
376 free(vbus_ext, M_DEVBUF);
377 KdPrint(("hpt_shutdown_vbus done"));
378 }
379
380 static void __hpt_do_tasks(PVBUS_EXT vbus_ext)
381 {
382 OSM_TASK *tasks;
383
384 tasks = vbus_ext->tasks;
385 vbus_ext->tasks = 0;
386
387 while (tasks) {
388 OSM_TASK *t = tasks;
389 tasks = t->next;
390 t->next = 0;
391 t->func(vbus_ext->vbus, t->data);
392 }
393 }
394
395 static void hpt_do_tasks(PVBUS_EXT vbus_ext, int pending)
396 {
397 if(vbus_ext){
398 hpt_lock_vbus(vbus_ext);
399 __hpt_do_tasks(vbus_ext);
400 hpt_unlock_vbus(vbus_ext);
401 }
402 }
403
404 static void hpt_action(struct cam_sim *sim, union ccb *ccb);
405 static void hpt_poll(struct cam_sim *sim);
406 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg);
407 static void hpt_pci_intr(void *arg);
408
409 static __inline POS_CMDEXT cmdext_get(PVBUS_EXT vbus_ext)
410 {
411 POS_CMDEXT p = vbus_ext->cmdext_list;
412 if (p)
413 vbus_ext->cmdext_list = p->next;
414 return p;
415 }
416
417 static __inline void cmdext_put(POS_CMDEXT p)
418 {
419 p->next = p->vbus_ext->cmdext_list;
420 p->vbus_ext->cmdext_list = p;
421 }
422
423 static void hpt_timeout(void *arg)
424 {
425 PCOMMAND pCmd = (PCOMMAND)arg;
426 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
427
428 KdPrint(("pCmd %p timeout", pCmd));
429
430 ldm_reset_vbus((PVBUS)ext->vbus_ext->vbus);
431 }
432
433 static void os_cmddone(PCOMMAND pCmd)
434 {
435 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
436 union ccb *ccb = ext->ccb;
437
438 KdPrint(("os_cmddone(%p, %d)", pCmd, pCmd->Result));
439
440 untimeout(hpt_timeout, pCmd, ccb->ccb_h.timeout_ch);
441
442 switch(pCmd->Result) {
443 case RETURN_SUCCESS:
444 ccb->ccb_h.status = CAM_REQ_CMP;
445 break;
446 case RETURN_BAD_DEVICE:
447 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
448 break;
449 case RETURN_DEVICE_BUSY:
450 ccb->ccb_h.status = CAM_BUSY;
451 break;
452 case RETURN_INVALID_REQUEST:
453 ccb->ccb_h.status = CAM_REQ_INVALID;
454 break;
455 case RETURN_SELECTION_TIMEOUT:
456 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
457 break;
458 case RETURN_RETRY:
459 ccb->ccb_h.status = CAM_BUSY;
460 break;
461 default:
462 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
463 break;
464 }
465
466 if (pCmd->flags.data_in) {
467 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTREAD);
468 }
469 else if (pCmd->flags.data_out) {
470 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTWRITE);
471 }
472
473 bus_dmamap_unload(ext->vbus_ext->io_dmat, ext->dma_map);
474
475 cmdext_put(ext);
476 ldm_free_cmds(pCmd);
477 xpt_done(ccb);
478 }
479
480 static int os_buildsgl(PCOMMAND pCmd, PSG pSg, int logical)
481 {
482 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
483 union ccb *ccb = ext->ccb;
484 bus_dma_segment_t *sgList = (bus_dma_segment_t *)ccb->csio.data_ptr;
485 int idx;
486
487 if(logical) {
488 if (ccb->ccb_h.flags & CAM_DATA_PHYS)
489 panic("physical address unsupported");
490
491 if (ccb->ccb_h.flags & CAM_SCATTER_VALID) {
492 if (ccb->ccb_h.flags & CAM_SG_LIST_PHYS)
493 panic("physical address unsupported");
494
495 for (idx = 0; idx < ccb->csio.sglist_cnt; idx++) {
496 os_set_sgptr(&pSg[idx], (HPT_U8 *)(HPT_UPTR)sgList[idx].ds_addr);
497 pSg[idx].size = sgList[idx].ds_len;
498 pSg[idx].eot = (idx==ccb->csio.sglist_cnt-1)? 1 : 0;
499 }
500 }
501 else {
502 os_set_sgptr(pSg, (HPT_U8 *)ccb->csio.data_ptr);
503 pSg->size = ccb->csio.dxfer_len;
504 pSg->eot = 1;
505 }
506 return TRUE;
507 }
508
509 /* since we have provided physical sg, nobody will ask us to build physical sg */
510 HPT_ASSERT(0);
511 return FALSE;
512 }
513
514 static void hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
515 {
516 PCOMMAND pCmd = (PCOMMAND)arg;
517 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
518 PSG psg = pCmd->psg;
519 int idx;
520
521 HPT_ASSERT(pCmd->flags.physical_sg);
522
523 if (error || nsegs == 0)
524 panic("busdma error");
525
526 HPT_ASSERT(nsegs<=os_max_sg_descriptors);
527
528 for (idx = 0; idx < nsegs; idx++, psg++) {
529 psg->addr.bus = segs[idx].ds_addr;
530 psg->size = segs[idx].ds_len;
531 psg->eot = 0;
532 }
533 psg[-1].eot = 1;
534
535 if (pCmd->flags.data_in) {
536 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_PREREAD);
537 }
538 else if (pCmd->flags.data_out) {
539 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_PREWRITE);
540 }
541
542 ext->ccb->ccb_h.timeout_ch = timeout(hpt_timeout, pCmd, HPT_OSM_TIMEOUT);
543 ldm_queue_cmd(pCmd);
544 }
545
546 static void hpt_scsi_io(PVBUS_EXT vbus_ext, union ccb *ccb)
547 {
548 PVBUS vbus = (PVBUS)vbus_ext->vbus;
549 PVDEV vd;
550 PCOMMAND pCmd;
551 POS_CMDEXT ext;
552 HPT_U8 *cdb;
553
554 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
555 cdb = ccb->csio.cdb_io.cdb_ptr;
556 else
557 cdb = ccb->csio.cdb_io.cdb_bytes;
558
559 KdPrint(("hpt_scsi_io: ccb %x id %d lun %d cdb %x-%x-%x",
560 ccb,
561 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
562 *(HPT_U32 *)&cdb[0], *(HPT_U32 *)&cdb[4], *(HPT_U32 *)&cdb[8]
563 ));
564
565 /* ccb->ccb_h.path_id is not our bus id - don't check it */
566 if (ccb->ccb_h.target_lun != 0 ||
567 ccb->ccb_h.target_id >= osm_max_targets ||
568 (ccb->ccb_h.flags & CAM_CDB_PHYS))
569 {
570 ccb->ccb_h.status = CAM_TID_INVALID;
571 xpt_done(ccb);
572 return;
573 }
574
575 vd = ldm_find_target(vbus, ccb->ccb_h.target_id);
576
577 if (!vd) {
578 ccb->ccb_h.status = CAM_TID_INVALID;
579 xpt_done(ccb);
580 return;
581 }
582
583 switch (cdb[0]) {
584 case TEST_UNIT_READY:
585 case START_STOP_UNIT:
586 case SYNCHRONIZE_CACHE:
587 ccb->ccb_h.status = CAM_REQ_CMP;
588 break;
589
590 case INQUIRY:
591 {
592 PINQUIRYDATA inquiryData;
593 memset(ccb->csio.data_ptr, 0, ccb->csio.dxfer_len);
594 inquiryData = (PINQUIRYDATA)ccb->csio.data_ptr;
595
596 inquiryData->AdditionalLength = 31;
597 inquiryData->CommandQueue = 1;
598 memcpy(&inquiryData->VendorId, "HPT ", 8);
599 memcpy(&inquiryData->ProductId, "DISK 0_0 ", 16);
600
601 if (vd->target_id / 10) {
602 inquiryData->ProductId[7] = (vd->target_id % 100) / 10 + '';
603 inquiryData->ProductId[8] = (vd->target_id % 100) % 10 + '';
604 }
605 else
606 inquiryData->ProductId[7] = (vd->target_id % 100) % 10 + '';
607
608 memcpy(&inquiryData->ProductRevisionLevel, "4.00", 4);
609
610 ccb->ccb_h.status = CAM_REQ_CMP;
611 }
612 break;
613
614 case READ_CAPACITY:
615 {
616 HPT_U8 *rbuf = ccb->csio.data_ptr;
617 HPT_U32 cap;
618
619 if (vd->capacity>0xfffffffful)
620 cap = 0xfffffffful;
621 else
622 cap = vd->capacity - 1;
623
624 rbuf[0] = (HPT_U8)(cap>>24);
625 rbuf[1] = (HPT_U8)(cap>>16);
626 rbuf[2] = (HPT_U8)(cap>>8);
627 rbuf[3] = (HPT_U8)cap;
628 rbuf[4] = 0;
629 rbuf[5] = 0;
630 rbuf[6] = 2;
631 rbuf[7] = 0;
632
633 ccb->ccb_h.status = CAM_REQ_CMP;
634 break;
635 }
636
637 case SERVICE_ACTION_IN:
638 {
639 HPT_U8 *rbuf = ccb->csio.data_ptr;
640 HPT_U64 cap = vd->capacity - 1;
641
642 rbuf[0] = (HPT_U8)(cap>>56);
643 rbuf[1] = (HPT_U8)(cap>>48);
644 rbuf[2] = (HPT_U8)(cap>>40);
645 rbuf[3] = (HPT_U8)(cap>>32);
646 rbuf[4] = (HPT_U8)(cap>>24);
647 rbuf[5] = (HPT_U8)(cap>>16);
648 rbuf[6] = (HPT_U8)(cap>>8);
649 rbuf[7] = (HPT_U8)cap;
650 rbuf[8] = 0;
651 rbuf[9] = 0;
652 rbuf[10] = 2;
653 rbuf[11] = 0;
654
655 ccb->ccb_h.status = CAM_REQ_CMP;
656 break;
657 }
658
659 case READ_6:
660 case READ_10:
661 case READ_16:
662 case WRITE_6:
663 case WRITE_10:
664 case WRITE_16:
665 case 0x13:
666 case 0x2f:
667 {
668 pCmd = ldm_alloc_cmds(vbus, vd->cmds_per_request);
669 if(!pCmd){
670 KdPrint(("Failed to allocate command!"));
671 ccb->ccb_h.status = CAM_BUSY;
672 break;
673 }
674
675 switch (cdb[0]) {
676 case READ_6:
677 case WRITE_6:
678 case 0x13:
679 pCmd->uCmd.Ide.Lba = ((HPT_U32)cdb[1] << 16) | ((HPT_U32)cdb[2] << 8) | (HPT_U32)cdb[3];
680 pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[4];
681 break;
682 case READ_16:
683 case WRITE_16:
684 {
685 HPT_U64 block =
686 ((HPT_U64)cdb[2]<<56) |
687 ((HPT_U64)cdb[3]<<48) |
688 ((HPT_U64)cdb[4]<<40) |
689 ((HPT_U64)cdb[5]<<32) |
690 ((HPT_U64)cdb[6]<<24) |
691 ((HPT_U64)cdb[7]<<16) |
692 ((HPT_U64)cdb[8]<<8) |
693 ((HPT_U64)cdb[9]);
694 pCmd->uCmd.Ide.Lba = block;
695 pCmd->uCmd.Ide.nSectors = (HPT_U16)cdb[13] | ((HPT_U16)cdb[12]<<8);
696 break;
697 }
698
699 default:
700 pCmd->uCmd.Ide.Lba = (HPT_U32)cdb[5] | ((HPT_U32)cdb[4] << 8) | ((HPT_U32)cdb[3] << 16) | ((HPT_U32)cdb[2] << 24);
701 pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[8] | ((HPT_U16)cdb[7]<<8);
702 break;
703 }
704
705 switch (cdb[0]) {
706 case READ_6:
707 case READ_10:
708 case READ_16:
709 pCmd->flags.data_in = 1;
710 break;
711 case WRITE_6:
712 case WRITE_10:
713 case WRITE_16:
714 pCmd->flags.data_out = 1;
715 break;
716 }
717 pCmd->priv = ext = cmdext_get(vbus_ext);
718 HPT_ASSERT(ext);
719 ext->ccb = ccb;
720 pCmd->target = vd;
721 pCmd->done = os_cmddone;
722 pCmd->buildsgl = os_buildsgl;
723
724 pCmd->psg = ext->psg;
725
726 if (ccb->ccb_h.flags & CAM_SCATTER_VALID) {
727 int idx;
728 bus_dma_segment_t *sgList = (bus_dma_segment_t *)ccb->csio.data_ptr;
729
730 if (ccb->ccb_h.flags & CAM_SG_LIST_PHYS)
731 pCmd->flags.physical_sg = 1;
732
733 for (idx = 0; idx < ccb->csio.sglist_cnt; idx++) {
734 pCmd->psg[idx].addr.bus = sgList[idx].ds_addr;
735 pCmd->psg[idx].size = sgList[idx].ds_len;
736 pCmd->psg[idx].eot = (idx==ccb->csio.sglist_cnt-1)? 1 : 0;
737 }
738
739 ccb->ccb_h.timeout_ch = timeout(hpt_timeout, pCmd, HPT_OSM_TIMEOUT);
740 ldm_queue_cmd(pCmd);
741 }
742 else {
743 int error;
744 pCmd->flags.physical_sg = 1;
745 error = bus_dmamap_load(vbus_ext->io_dmat,
746 ext->dma_map,
747 ccb->csio.data_ptr, ccb->csio.dxfer_len,
748 hpt_io_dmamap_callback, pCmd,
749 BUS_DMA_WAITOK
750 );
751 KdPrint(("bus_dmamap_load return %d", error));
752 if (error && error!=EINPROGRESS) {
753 os_printk("bus_dmamap_load error %d", error);
754 cmdext_put(ext);
755 ldm_free_cmds(pCmd);
756 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
757 xpt_done(ccb);
758 }
759 }
760 return;
761 }
762
763 default:
764 ccb->ccb_h.status = CAM_REQ_INVALID;
765 break;
766 }
767
768 xpt_done(ccb);
769 return;
770 }
771
772 static void hpt_action(struct cam_sim *sim, union ccb *ccb)
773 {
774 PVBUS_EXT vbus_ext = (PVBUS_EXT)cam_sim_softc(sim);
775
776 KdPrint(("hpt_action(fn=%d, id=%d)", ccb->ccb_h.func_code, ccb->ccb_h.target_id));
777
778 switch (ccb->ccb_h.func_code) {
779
780 case XPT_SCSI_IO:
781 hpt_lock_vbus(vbus_ext);
782 hpt_scsi_io(vbus_ext, ccb);
783 hpt_unlock_vbus(vbus_ext);
784 return;
785
786 case XPT_RESET_BUS:
787 hpt_lock_vbus(vbus_ext);
788 ldm_reset_vbus((PVBUS)vbus_ext->vbus);
789 hpt_unlock_vbus(vbus_ext);
790 break;
791
792 case XPT_GET_TRAN_SETTINGS:
793 case XPT_SET_TRAN_SETTINGS:
794 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
795 break;
796
797 case XPT_CALC_GEOMETRY:
798 ccb->ccg.heads = 255;
799 ccb->ccg.secs_per_track = 63;
800 ccb->ccg.cylinders = ccb->ccg.volume_size / (ccb->ccg.heads * ccb->ccg.secs_per_track);
801 ccb->ccb_h.status = CAM_REQ_CMP;
802 break;
803
804 case XPT_PATH_INQ:
805 {
806 struct ccb_pathinq *cpi = &ccb->cpi;
807
808 cpi->version_num = 1;
809 cpi->hba_inquiry = PI_SDTR_ABLE;
810 cpi->target_sprt = 0;
811 cpi->hba_misc = PIM_NOBUSRESET;
812 cpi->hba_eng_cnt = 0;
813 cpi->max_target = osm_max_targets;
814 cpi->max_lun = 0;
815 cpi->unit_number = cam_sim_unit(sim);
816 cpi->bus_id = cam_sim_bus(sim);
817 cpi->initiator_id = osm_max_targets;
818 cpi->base_transfer_speed = 3300;
819
820 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
821 strncpy(cpi->hba_vid, "HPT ", HBA_IDLEN);
822 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
823 cpi->ccb_h.status = CAM_REQ_CMP;
824 break;
825 }
826
827 default:
828 ccb->ccb_h.status = CAM_REQ_INVALID;
829 break;
830 }
831
832 xpt_done(ccb);
833 return;
834 }
835
836 static void hpt_pci_intr(void *arg)
837 {
838 PVBUS_EXT vbus_ext = (PVBUS_EXT)arg;
839 hpt_lock_vbus(vbus_ext);
840 ldm_intr((PVBUS)vbus_ext->vbus);
841 hpt_unlock_vbus(vbus_ext);
842 }
843
844 static void hpt_poll(struct cam_sim *sim)
845 {
846 hpt_pci_intr(cam_sim_softc(sim));
847 }
848
849 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg)
850 {
851 KdPrint(("hpt_async"));
852 }
853
854 static int hpt_shutdown(device_t dev)
855 {
856 KdPrint(("hpt_shutdown(dev=%p)", dev));
857 return 0;
858 }
859
860 static int hpt_detach(device_t dev)
861 {
862 /* we don't allow the driver to be unloaded. */
863 return EBUSY;
864 }
865
866 static void hpt_ioctl_done(struct _IOCTL_ARG *arg)
867 {
868 arg->ioctl_cmnd = 0;
869 wakeup(arg);
870 }
871
872 static void __hpt_do_ioctl(PVBUS_EXT vbus_ext, IOCTL_ARG *ioctl_args)
873 {
874 ioctl_args->result = -1;
875 ioctl_args->done = hpt_ioctl_done;
876 ioctl_args->ioctl_cmnd = (void *)1;
877
878 hpt_lock_vbus(vbus_ext);
879 ldm_ioctl((PVBUS)vbus_ext->vbus, ioctl_args);
880
881 while (ioctl_args->ioctl_cmnd) {
882 if (hpt_sleep(vbus_ext, ioctl_args, PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
883 break;
884 ldm_reset_vbus((PVBUS)vbus_ext->vbus);
885 __hpt_do_tasks(vbus_ext);
886 }
887
888 /* KdPrint(("ioctl %x result %d", ioctl_args->dwIoControlCode, ioctl_args->result)); */
889
890 hpt_unlock_vbus(vbus_ext);
891 }
892
893 static void hpt_do_ioctl(IOCTL_ARG *ioctl_args)
894 {
895 PVBUS vbus;
896 PVBUS_EXT vbus_ext;
897
898 ldm_for_each_vbus(vbus, vbus_ext) {
899 __hpt_do_ioctl(vbus_ext, ioctl_args);
900 if (ioctl_args->result!=HPT_IOCTL_RESULT_WRONG_VBUS)
901 return;
902 }
903 }
904
905 #define HPT_DO_IOCTL(code, inbuf, insize, outbuf, outsize) ({\
906 IOCTL_ARG arg;\
907 arg.dwIoControlCode = code;\
908 arg.lpInBuffer = inbuf;\
909 arg.lpOutBuffer = outbuf;\
910 arg.nInBufferSize = insize;\
911 arg.nOutBufferSize = outsize;\
912 arg.lpBytesReturned = 0;\
913 hpt_do_ioctl(&arg);\
914 arg.result;\
915 })
916
917 #define DEVICEID_VALID(id) ((id) && ((HPT_U32)(id)!=0xffffffff))
918
919 static int hpt_get_logical_devices(DEVICEID * pIds, int nMaxCount)
920 {
921 int i;
922 HPT_U32 count = nMaxCount-1;
923
924 if (HPT_DO_IOCTL(HPT_IOCTL_GET_LOGICAL_DEVICES,
925 &count, sizeof(HPT_U32), pIds, sizeof(DEVICEID)*nMaxCount))
926 return -1;
927
928 nMaxCount = (int)pIds[0];
929 for (i=0; i<nMaxCount; i++) pIds[i] = pIds[i+1];
930 return nMaxCount;
931 }
932
933 static int hpt_get_device_info_v3(DEVICEID id, PLOGICAL_DEVICE_INFO_V3 pInfo)
934 {
935 return HPT_DO_IOCTL(HPT_IOCTL_GET_DEVICE_INFO_V3,
936 &id, sizeof(DEVICEID), pInfo, sizeof(LOGICAL_DEVICE_INFO_V3));
937 }
938
939 /* not belong to this file logically, but we want to use ioctl interface */
940 static int __hpt_stop_tasks(PVBUS_EXT vbus_ext, DEVICEID id)
941 {
942 LOGICAL_DEVICE_INFO_V3 devinfo;
943 int i, result;
944 DEVICEID param[2] = { id, 0 };
945
946 if (hpt_get_device_info_v3(id, &devinfo))
947 return -1;
948
949 if (devinfo.Type!=LDT_ARRAY)
950 return -1;
951
952 if (devinfo.u.array.Flags & ARRAY_FLAG_REBUILDING)
953 param[1] = AS_REBUILD_ABORT;
954 else if (devinfo.u.array.Flags & ARRAY_FLAG_VERIFYING)
955 param[1] = AS_VERIFY_ABORT;
956 else if (devinfo.u.array.Flags & ARRAY_FLAG_INITIALIZING)
957 param[1] = AS_INITIALIZE_ABORT;
958 else if (devinfo.u.array.Flags & ARRAY_FLAG_TRANSFORMING)
959 param[1] = AS_TRANSFORM_ABORT;
960 else
961 return -1;
962
963 KdPrint(("SET_ARRAY_STATE(%x, %d)", param[0], param[1]));
964 result = HPT_DO_IOCTL(HPT_IOCTL_SET_ARRAY_STATE,
965 param, sizeof(param), 0, 0);
966
967 for (i=0; i<devinfo.u.array.nDisk; i++)
968 if (DEVICEID_VALID(devinfo.u.array.Members[i]))
969 __hpt_stop_tasks(vbus_ext, devinfo.u.array.Members[i]);
970
971 return result;
972 }
973
974 static void hpt_stop_tasks(PVBUS_EXT vbus_ext)
975 {
976 DEVICEID ids[32];
977 int i, count;
978
979 count = hpt_get_logical_devices((DEVICEID *)&ids, sizeof(ids)/sizeof(ids[0]));
980
981 for (i=0; i<count; i++)
982 __hpt_stop_tasks(vbus_ext, ids[i]);
983 }
984
985 static d_open_t hpt_open;
986 static d_close_t hpt_close;
987 static d_ioctl_t hpt_ioctl;
988 static void hpt_bus_scan_cb(struct cam_periph *periph, union ccb *ccb);
989 static int hpt_rescan_bus(void);
990
991 static struct cdevsw hpt_cdevsw = {
992 .d_open = hpt_open,
993 .d_close = hpt_close,
994 .d_ioctl = hpt_ioctl,
995 .d_name = driver_name,
996 #if __FreeBSD_version>=503000
997 .d_version = D_VERSION,
998 #endif
999 #if (__FreeBSD_version>=503000 && __FreeBSD_version<600034)
1000 .d_flags = D_NEEDGIANT,
1001 #endif
1002 #if __FreeBSD_version<600034
1003 #if __FreeBSD_version>501000
1004 .d_maj = MAJOR_AUTO,
1005 #else
1006 .d_maj = HPT_DEV_MAJOR,
1007 #endif
1008 #endif
1009 };
1010
1011 static struct intr_config_hook hpt_ich;
1012
1013 /*
1014 * hpt_final_init will be called after all hpt_attach.
1015 */
1016 static void hpt_final_init(void *dummy)
1017 {
1018 int i;
1019 PVBUS_EXT vbus_ext;
1020 PVBUS vbus;
1021 PHBA hba;
1022
1023 /* Clear the config hook */
1024 config_intrhook_disestablish(&hpt_ich);
1025
1026 /* allocate memory */
1027 i = 0;
1028 ldm_for_each_vbus(vbus, vbus_ext) {
1029 if (hpt_alloc_mem(vbus_ext)) {
1030 os_printk("out of memory");
1031 return;
1032 }
1033 i++;
1034 }
1035
1036 if (!i) {
1037 if (bootverbose)
1038 os_printk("no controller detected.");
1039 return;
1040 }
1041
1042 /* initializing hardware */
1043 ldm_for_each_vbus(vbus, vbus_ext) {
1044 /* make timer available here */
1045 callout_handle_init(&vbus_ext->timer);
1046 if (hpt_init_vbus(vbus_ext)) {
1047 os_printk("fail to initialize hardware");
1048 break; /* FIXME */
1049 }
1050 }
1051
1052 /* register CAM interface */
1053 ldm_for_each_vbus(vbus, vbus_ext) {
1054 struct cam_devq *devq;
1055 struct ccb_setasync ccb;
1056
1057 #if (__FreeBSD_version >= 500000)
1058 mtx_init(&vbus_ext->lock, "hptsleeplock", NULL, MTX_DEF);
1059 #endif
1060 if (bus_dma_tag_create(NULL,/* parent */
1061 4, /* alignment */
1062 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1063 BUS_SPACE_MAXADDR, /* lowaddr */
1064 BUS_SPACE_MAXADDR, /* highaddr */
1065 NULL, NULL, /* filter, filterarg */
1066 PAGE_SIZE * (os_max_sg_descriptors-1), /* maxsize */
1067 os_max_sg_descriptors, /* nsegments */
1068 0x10000, /* maxsegsize */
1069 BUS_DMA_WAITOK, /* flags */
1070 #if __FreeBSD_version>502000
1071 busdma_lock_mutex, /* lockfunc */
1072 &vbus_ext->lock, /* lockfuncarg */
1073 #endif
1074 &vbus_ext->io_dmat /* tag */))
1075 {
1076 return ;
1077 }
1078
1079 for (i=0; i<os_max_queue_comm; i++) {
1080 POS_CMDEXT ext = (POS_CMDEXT)malloc(sizeof(OS_CMDEXT), M_DEVBUF, M_WAITOK);
1081 if (!ext) {
1082 os_printk("Can't alloc cmdext(%d)", i);
1083 return ;
1084 }
1085 ext->vbus_ext = vbus_ext;
1086 ext->next = vbus_ext->cmdext_list;
1087 vbus_ext->cmdext_list = ext;
1088
1089 if (bus_dmamap_create(vbus_ext->io_dmat, 0, &ext->dma_map)) {
1090 os_printk("Can't create dma map(%d)", i);
1091 return ;
1092 }
1093 }
1094
1095 if ((devq = cam_simq_alloc(os_max_queue_comm)) == NULL) {
1096 os_printk("cam_simq_alloc failed");
1097 return ;
1098 }
1099
1100 #if __FreeBSD_version > 700025
1101 vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name,
1102 vbus_ext, 0, &Giant, os_max_queue_comm, /*tagged*/8, devq);
1103 #else
1104 vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name,
1105 vbus_ext, 0, os_max_queue_comm, /*tagged*/8, devq);
1106 #endif
1107
1108 if (!vbus_ext->sim) {
1109 os_printk("cam_sim_alloc failed");
1110 cam_simq_free(devq);
1111 return ;
1112 }
1113
1114 #if __FreeBSD_version > 700044
1115 if (xpt_bus_register(vbus_ext->sim, NULL, 0) != CAM_SUCCESS) {
1116 #else
1117 if (xpt_bus_register(vbus_ext->sim, 0) != CAM_SUCCESS) {
1118 #endif
1119 os_printk("xpt_bus_register failed");
1120 cam_sim_free(vbus_ext->sim, /*free devq*/ TRUE);
1121 vbus_ext->sim = NULL;
1122 return ;
1123 }
1124
1125 if (xpt_create_path(&vbus_ext->path, /*periph */ NULL,
1126 cam_sim_path(vbus_ext->sim), CAM_TARGET_WILDCARD,
1127 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1128 {
1129 os_printk("xpt_create_path failed");
1130 xpt_bus_deregister(cam_sim_path(vbus_ext->sim));
1131 cam_sim_free(vbus_ext->sim, /*free_devq*/TRUE);
1132 vbus_ext->sim = NULL;
1133 return ;
1134 }
1135
1136 xpt_setup_ccb(&ccb.ccb_h, vbus_ext->path, /*priority*/5);
1137 ccb.ccb_h.func_code = XPT_SASYNC_CB;
1138 ccb.event_enable = AC_LOST_DEVICE;
1139 ccb.callback = hpt_async;
1140 ccb.callback_arg = vbus_ext;
1141 xpt_action((union ccb *)&ccb);
1142
1143 for (hba = vbus_ext->hba_list; hba; hba = hba->next) {
1144 int rid = 0;
1145 if ((hba->irq_res = bus_alloc_resource(hba->pcidev,
1146 SYS_RES_IRQ, &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL)
1147 {
1148 os_printk("can't allocate interrupt");
1149 return ;
1150 }
1151
1152 if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM,
1153 #if __FreeBSD_version > 700025
1154 NULL, hpt_pci_intr, vbus_ext, &hba->irq_handle))
1155 #else
1156 hpt_pci_intr, vbus_ext, &hba->irq_handle))
1157 #endif
1158 {
1159 os_printk("can't set up interrupt");
1160 return ;
1161 }
1162 hba->ldm_adapter.him->intr_control(hba->ldm_adapter.him_handle, HPT_TRUE);
1163 }
1164
1165 vbus_ext->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final,
1166 hpt_shutdown_vbus, vbus_ext, SHUTDOWN_PRI_DEFAULT);
1167 if (!vbus_ext->shutdown_eh)
1168 os_printk("Shutdown event registration failed");
1169 }
1170
1171 ldm_for_each_vbus(vbus, vbus_ext) {
1172 TASK_INIT(&vbus_ext->worker, 0, (task_fn_t *)hpt_do_tasks, vbus_ext);
1173 if (vbus_ext->tasks)
1174 TASK_ENQUEUE(&vbus_ext->worker);
1175 }
1176
1177 make_dev(&hpt_cdevsw, DRIVER_MINOR, UID_ROOT, GID_OPERATOR,
1178 S_IRUSR | S_IWUSR, driver_name);
1179 }
1180
1181 #if defined(KLD_MODULE) && (__FreeBSD_version >= 503000)
1182
1183 typedef struct driverlink *driverlink_t;
1184 struct driverlink {
1185 kobj_class_t driver;
1186 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
1187 };
1188
1189 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
1190
1191 struct devclass {
1192 TAILQ_ENTRY(devclass) link;
1193 devclass_t parent; /* parent in devclass hierarchy */
1194 driver_list_t drivers; /* bus devclasses store drivers for bus */
1195 char *name;
1196 device_t *devices; /* array of devices indexed by unit */
1197 int maxunit; /* size of devices array */
1198 };
1199
1200 static void override_kernel_driver(void)
1201 {
1202 driverlink_t dl, dlfirst;
1203 driver_t *tmpdriver;
1204 devclass_t dc = devclass_find("pci");
1205
1206 if (dc){
1207 dlfirst = TAILQ_FIRST(&dc->drivers);
1208 for (dl = dlfirst; dl; dl = TAILQ_NEXT(dl, link)) {
1209 if(strcmp(dl->driver->name, driver_name) == 0) {
1210 tmpdriver=dl->driver;
1211 dl->driver=dlfirst->driver;
1212 dlfirst->driver=tmpdriver;
1213 break;
1214 }
1215 }
1216 }
1217 }
1218
1219 #else
1220 #define override_kernel_driver()
1221 #endif
1222
1223 static void hpt_init(void *dummy)
1224 {
1225 if (bootverbose)
1226 os_printk("%s %s", driver_name_long, driver_ver);
1227
1228 override_kernel_driver();
1229 init_config();
1230
1231 hpt_ich.ich_func = hpt_final_init;
1232 hpt_ich.ich_arg = NULL;
1233 if (config_intrhook_establish(&hpt_ich) != 0) {
1234 printf("%s: cannot establish configuration hook\n",
1235 driver_name_long);
1236 }
1237
1238 }
1239 SYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL);
1240
1241 /*
1242 * CAM driver interface
1243 */
1244 static device_method_t driver_methods[] = {
1245 /* Device interface */
1246 DEVMETHOD(device_probe, hpt_probe),
1247 DEVMETHOD(device_attach, hpt_attach),
1248 DEVMETHOD(device_detach, hpt_detach),
1249 DEVMETHOD(device_shutdown, hpt_shutdown),
1250 { 0, 0 }
1251 };
1252
1253 static driver_t hpt_pci_driver = {
1254 driver_name,
1255 driver_methods,
1256 sizeof(HBA)
1257 };
1258
1259 static devclass_t hpt_devclass;
1260
1261 #ifndef TARGETNAME
1262 #error "no TARGETNAME found"
1263 #endif
1264
1265 /* use this to make TARGETNAME be expanded */
1266 #define __DRIVER_MODULE(p1, p2, p3, p4, p5, p6) DRIVER_MODULE(p1, p2, p3, p4, p5, p6)
1267 #define __MODULE_VERSION(p1, p2) MODULE_VERSION(p1, p2)
1268 #define __MODULE_DEPEND(p1, p2, p3, p4, p5) MODULE_DEPEND(p1, p2, p3, p4, p5)
1269 __DRIVER_MODULE(TARGETNAME, pci, hpt_pci_driver, hpt_devclass, 0, 0);
1270 __MODULE_VERSION(TARGETNAME, 1);
1271 __MODULE_DEPEND(TARGETNAME, cam, 1, 1, 1);
1272
1273 #if __FreeBSD_version>503000
1274 typedef struct cdev * ioctl_dev_t;
1275 #else
1276 typedef dev_t ioctl_dev_t;
1277 #endif
1278
1279 #if __FreeBSD_version >= 500000
1280 typedef struct thread * ioctl_thread_t;
1281 #else
1282 typedef struct proc * ioctl_thread_t;
1283 #endif
1284
1285 static int hpt_open(ioctl_dev_t dev, int flags, int devtype, ioctl_thread_t td)
1286 {
1287 return 0;
1288 }
1289
1290 static int hpt_close(ioctl_dev_t dev, int flags, int devtype, ioctl_thread_t td)
1291 {
1292 return 0;
1293 }
1294
1295 static int hpt_ioctl(ioctl_dev_t dev, u_long cmd, caddr_t data, int fflag, ioctl_thread_t td)
1296 {
1297 PHPT_IOCTL_PARAM piop=(PHPT_IOCTL_PARAM)data;
1298 IOCTL_ARG ioctl_args;
1299 HPT_U32 bytesReturned;
1300
1301 switch (cmd){
1302 case HPT_DO_IOCONTROL:
1303 {
1304 if (piop->Magic == HPT_IOCTL_MAGIC || piop->Magic == HPT_IOCTL_MAGIC32) {
1305 KdPrint(("ioctl=%x in=%p len=%d out=%p len=%d\n",
1306 piop->dwIoControlCode,
1307 piop->lpInBuffer,
1308 piop->nInBufferSize,
1309 piop->lpOutBuffer,
1310 piop->nOutBufferSize));
1311
1312 memset(&ioctl_args, 0, sizeof(ioctl_args));
1313
1314 ioctl_args.dwIoControlCode = piop->dwIoControlCode;
1315 ioctl_args.nInBufferSize = piop->nInBufferSize;
1316 ioctl_args.nOutBufferSize = piop->nOutBufferSize;
1317 ioctl_args.lpBytesReturned = &bytesReturned;
1318
1319 if (ioctl_args.nInBufferSize) {
1320 ioctl_args.lpInBuffer = malloc(ioctl_args.nInBufferSize, M_DEVBUF, M_WAITOK);
1321 if (!ioctl_args.lpInBuffer)
1322 goto invalid;
1323 if (copyin((void*)piop->lpInBuffer,
1324 ioctl_args.lpInBuffer, piop->nInBufferSize))
1325 goto invalid;
1326 }
1327
1328 if (ioctl_args.nOutBufferSize) {
1329 ioctl_args.lpOutBuffer = malloc(ioctl_args.nOutBufferSize, M_DEVBUF, M_WAITOK);
1330 if (!ioctl_args.lpOutBuffer)
1331 goto invalid;
1332 }
1333
1334 #if (__FreeBSD_version >= 500000)
1335 mtx_lock(&Giant);
1336 #endif
1337
1338 hpt_do_ioctl(&ioctl_args);
1339
1340 #if (__FreeBSD_version >= 500000)
1341 mtx_unlock(&Giant);
1342 #endif
1343
1344 if (ioctl_args.result==HPT_IOCTL_RESULT_OK) {
1345 if (piop->nOutBufferSize) {
1346 if (copyout(ioctl_args.lpOutBuffer,
1347 (void*)piop->lpOutBuffer, piop->nOutBufferSize))
1348 goto invalid;
1349 }
1350 if (piop->lpBytesReturned) {
1351 if (copyout(&bytesReturned,
1352 (void*)piop->lpBytesReturned, sizeof(HPT_U32)))
1353 goto invalid;
1354 }
1355 if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1356 if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1357 return 0;
1358 }
1359 invalid:
1360 if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1361 if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1362 return EFAULT;
1363 }
1364 return EFAULT;
1365 }
1366
1367 case HPT_SCAN_BUS:
1368 {
1369 return hpt_rescan_bus();
1370 }
1371 default:
1372 KdPrint(("invalid command!"));
1373 return EFAULT;
1374 }
1375
1376 }
1377
1378 static int hpt_rescan_bus(void)
1379 {
1380 struct cam_path *path;
1381 union ccb *ccb;
1382 PVBUS vbus;
1383 PVBUS_EXT vbus_ext;
1384
1385 #if (__FreeBSD_version >= 500000)
1386 mtx_lock(&Giant);
1387 #endif
1388
1389 ldm_for_each_vbus(vbus, vbus_ext) {
1390 if (xpt_create_path(&path, xpt_periph, cam_sim_path(vbus_ext->sim),
1391 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1392 return(EIO);
1393 if ((ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK)) == NULL)
1394 return(ENOMEM);
1395 bzero(ccb, sizeof(union ccb));
1396 xpt_setup_ccb(&ccb->ccb_h, path, 5);
1397 ccb->ccb_h.func_code = XPT_SCAN_BUS;
1398 ccb->ccb_h.cbfcnp = hpt_bus_scan_cb;
1399 ccb->crcn.flags = CAM_FLAG_NONE;
1400 xpt_action(ccb);
1401 }
1402
1403 #if (__FreeBSD_version >= 500000)
1404 mtx_unlock(&Giant);
1405 #endif
1406
1407 return(0);
1408 }
1409
1410 static void hpt_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
1411 {
1412 if (ccb->ccb_h.status != CAM_REQ_CMP)
1413 KdPrint(("cam_scan_callback: failure status = %x",ccb->ccb_h.status));
1414 else
1415 KdPrint(("Scan bus successfully!"));
1416
1417 xpt_free_path(ccb->ccb_h.path);
1418 free(ccb, M_TEMP);
1419 return;
1420 }
Cache object: fdf79b95096caddb7e20ababbdb8f636
|