1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*
  28  * hci1394_buf.c
  29  *   These routines handle IO mapped memory.  They include routines to alloc and
  30  *   free  IO mapped memory and a routine to get the adapters default dma
  31  *   attributes. These routines are meant to be called from the base context.
  32  *   They should not be called from an interrupt handler.
  33  */
  34 
  35 #include <sys/conf.h>
  36 #include <sys/ddi.h>
  37 #include <sys/modctl.h>
  38 #include <sys/stat.h>
  39 #include <sys/sunddi.h>
  40 #include <sys/kmem.h>
  41 
  42 #include <sys/1394/h1394.h>
  43 #include <sys/1394/adapters/hci1394.h>
  44 
  45 
  46 /*
  47  * hci1394_buffer_attr_get()
  48  *    returns (in dma_attr) the default DMA attributes for this adapter.
  49  */
  50 void
  51 hci1394_buf_attr_get(ddi_dma_attr_t *dma_attr)
  52 {
  53         dma_attr->dma_attr_version = DMA_ATTR_V0;
  54         dma_attr->dma_attr_addr_lo = (uint64_t)0x00000000;
  55         dma_attr->dma_attr_addr_hi = (uint64_t)0xFFFFFFFF;
  56         dma_attr->dma_attr_count_max = (uint64_t)0xFFFFFFFF;
  57         dma_attr->dma_attr_align = 64;
  58         dma_attr->dma_attr_burstsizes = 0x3FF;
  59         dma_attr->dma_attr_minxfer = 1;
  60         dma_attr->dma_attr_maxxfer = (uint64_t)0xFFFFFFFF;
  61         dma_attr->dma_attr_seg = (uint64_t)0xFFFFFFFF;
  62         dma_attr->dma_attr_sgllen = 0x7FFFFFFF;
  63         dma_attr->dma_attr_granular = 4;
  64         dma_attr->dma_attr_flags = 0;
  65 
  66 #if defined(__i386) || defined(__amd64)
  67         /* XXX - Not sure why x86 wants the dma_attr_seg to be 0x7FFF?? */
  68         dma_attr->dma_attr_seg = (uint64_t)0x7FFF;
  69 #endif
  70 }
  71 
  72 
  73 /*
  74  * hci1394_buf_alloc()
  75  *    Allocate an IO mapped buffer. drvinfo is passed in and contains generic
  76  *    driver info, like dip, instance, buf_attr, etc.  Parms is passed in and
  77  *    contains the input parameters for alloc, ow much memory to alloc, how many
  78  *    cookies can we handle, and alignment requirements. info is returned with
  79  *    all the info about the mapped buffer.  handle is returned. It should be
  80  *    used when calling hci1394_buf_free().
  81  */
  82 int
  83 hci1394_buf_alloc(hci1394_drvinfo_t *drvinfo, hci1394_buf_parms_t *parms,
  84     hci1394_buf_info_t *info, hci1394_buf_handle_t *handle)
  85 {
  86         ddi_dma_attr_t dma_attr;
  87         hci1394_buf_t *buf;
  88         int status;
  89 
  90 
  91         ASSERT(drvinfo != NULL);
  92         ASSERT(parms != NULL);
  93         ASSERT(info != NULL);
  94         ASSERT(handle != NULL);
  95         TNF_PROBE_0_DEBUG(hci1394_buf_alloc_enter, HCI1394_TNF_HAL_STACK, "");
  96 
  97         /* alloc the space to keep track of the buffer */
  98         buf = kmem_alloc(sizeof (hci1394_buf_t), KM_SLEEP);
  99 
 100         /* setup the return parameter */
 101         *handle = buf;
 102 
 103         /* save away pointer to general info */
 104         buf->bu_drvinfo = drvinfo;
 105 
 106         /* Get the default DMA attributes and override sgllen and alignment */
 107 
 108         hci1394_buf_attr_get(&dma_attr);
 109         dma_attr.dma_attr_sgllen = parms->bp_max_cookies;
 110         dma_attr.dma_attr_align = parms->bp_alignment;
 111 
 112         status = ddi_dma_alloc_handle(drvinfo->di_dip, &dma_attr,
 113             DDI_DMA_SLEEP, NULL, &buf->bu_dma_handle);
 114         if (status != DDI_SUCCESS) {
 115                 kmem_free(buf, sizeof (hci1394_buf_t));
 116                 TNF_PROBE_0(hci1394_buf_alloc_dah_fail, HCI1394_TNF_HAL_ERROR,
 117                     "");
 118                 TNF_PROBE_0_DEBUG(hci1394_buf_alloc_exit, HCI1394_TNF_HAL_STACK,
 119                     "");
 120                 return (DDI_FAILURE);
 121         }
 122 
 123         status = ddi_dma_mem_alloc(buf->bu_dma_handle, parms->bp_length,
 124             &drvinfo->di_buf_attr, DDI_DMA_STREAMING, DDI_DMA_SLEEP,
 125             NULL, &info->bi_kaddr, &info->bi_real_length, &buf->bu_handle);
 126         if (status != DDI_SUCCESS) {
 127                 ddi_dma_free_handle(&buf->bu_dma_handle);
 128                 kmem_free(buf, sizeof (hci1394_buf_t));
 129                 TNF_PROBE_0(hci1394_buf_alloc_dam_fail, HCI1394_TNF_HAL_ERROR,
 130                     "");
 131                 TNF_PROBE_0_DEBUG(hci1394_buf_alloc_exit, HCI1394_TNF_HAL_STACK,
 132                     "");
 133                 return (DDI_FAILURE);
 134         }
 135 
 136         status = ddi_dma_addr_bind_handle(buf->bu_dma_handle, NULL,
 137             info->bi_kaddr, info->bi_real_length, DDI_DMA_RDWR |
 138             DDI_DMA_STREAMING, DDI_DMA_SLEEP, NULL, &info->bi_cookie,
 139             &info->bi_cookie_count);
 140         if (status != DDI_SUCCESS) {
 141                 ddi_dma_mem_free(&buf->bu_handle);
 142                 ddi_dma_free_handle(&buf->bu_dma_handle);
 143                 kmem_free(buf, sizeof (hci1394_buf_t));
 144                 TNF_PROBE_0(hci1394_buf_alloc_dbh_fail, HCI1394_TNF_HAL_ERROR,
 145                     "");
 146                 TNF_PROBE_0_DEBUG(hci1394_buf_alloc_exit, HCI1394_TNF_HAL_STACK,
 147                     "");
 148                 return (DDI_FAILURE);
 149         }
 150 
 151         /* setup rest of buffer info returned to caller */
 152         info->bi_handle = buf->bu_handle;
 153         info->bi_dma_handle = buf->bu_dma_handle;
 154         info->bi_length = parms->bp_length;
 155 
 156         TNF_PROBE_0_DEBUG(hci1394_buf_alloc_exit, HCI1394_TNF_HAL_STACK, "");
 157 
 158         return (DDI_SUCCESS);
 159 }
 160 
 161 
 162 /*
 163  * hci1394_buf_free()
 164  *    Free IO mapped buffer. Notice that a pointer to the handle is used for
 165  *    the parameter.  free() will set your handle to NULL before returning.
 166  */
 167 void
 168 hci1394_buf_free(hci1394_buf_handle_t *handle)
 169 {
 170         hci1394_buf_t *buf;
 171 
 172         ASSERT(handle != NULL);
 173         TNF_PROBE_0_DEBUG(hci1394_buf_free_enter, HCI1394_TNF_HAL_STACK, "");
 174 
 175         buf = *handle;
 176         (void) ddi_dma_unbind_handle(buf->bu_dma_handle);
 177         ddi_dma_mem_free(&buf->bu_handle);
 178         ddi_dma_free_handle(&buf->bu_dma_handle);
 179 
 180         /* free the space to keep track of the buffer */
 181         kmem_free(buf, sizeof (hci1394_buf_t));
 182 
 183         /* set the handle to NULL to help catch bugs */
 184         *handle = NULL;
 185 
 186         TNF_PROBE_0_DEBUG(hci1394_buf_free_exit, HCI1394_TNF_HAL_STACK, "");
 187 }