/*1* Functions to handle I2O memory2*3* Pulled from the inlines in i2o headers and uninlined4*5*6* This program is free software; you can redistribute it and/or modify it7* under the terms of the GNU General Public License as published by the8* Free Software Foundation; either version 2 of the License, or (at your9* option) any later version.10*/1112#include <linux/module.h>13#include <linux/i2o.h>14#include <linux/delay.h>15#include <linux/string.h>16#include <linux/slab.h>17#include "core.h"1819/* Protects our 32/64bit mask switching */20static DEFINE_MUTEX(mem_lock);2122/**23* i2o_sg_tablesize - Calculate the maximum number of elements in a SGL24* @c: I2O controller for which the calculation should be done25* @body_size: maximum body size used for message in 32-bit words.26*27* Return the maximum number of SG elements in a SG list.28*/29u16 i2o_sg_tablesize(struct i2o_controller *c, u16 body_size)30{31i2o_status_block *sb = c->status_block.virt;32u16 sg_count =33(sb->inbound_frame_size - sizeof(struct i2o_message) / 4) -34body_size;3536if (c->pae_support) {37/*38* for 64-bit a SG attribute element must be added and each39* SG element needs 12 bytes instead of 8.40*/41sg_count -= 2;42sg_count /= 3;43} else44sg_count /= 2;4546if (c->short_req && (sg_count > 8))47sg_count = 8;4849return sg_count;50}51EXPORT_SYMBOL_GPL(i2o_sg_tablesize);525354/**55* i2o_dma_map_single - Map pointer to controller and fill in I2O message.56* @c: I2O controller57* @ptr: pointer to the data which should be mapped58* @size: size of data in bytes59* @direction: DMA_TO_DEVICE / DMA_FROM_DEVICE60* @sg_ptr: pointer to the SG list inside the I2O message61*62* This function does all necessary DMA handling and also writes the I2O63* SGL elements into the I2O message. For details on DMA handling see also64* dma_map_single(). The pointer sg_ptr will only be set to the end of the65* SG list if the allocation was successful.66*67* Returns DMA address which must be checked for failures using68* dma_mapping_error().69*/70dma_addr_t i2o_dma_map_single(struct i2o_controller *c, void *ptr,71size_t size,72enum dma_data_direction direction,73u32 ** sg_ptr)74{75u32 sg_flags;76u32 *mptr = *sg_ptr;77dma_addr_t dma_addr;7879switch (direction) {80case DMA_TO_DEVICE:81sg_flags = 0xd4000000;82break;83case DMA_FROM_DEVICE:84sg_flags = 0xd0000000;85break;86default:87return 0;88}8990dma_addr = dma_map_single(&c->pdev->dev, ptr, size, direction);91if (!dma_mapping_error(&c->pdev->dev, dma_addr)) {92#ifdef CONFIG_I2O_EXT_ADAPTEC_DMA6493if ((sizeof(dma_addr_t) > 4) && c->pae_support) {94*mptr++ = cpu_to_le32(0x7C020002);95*mptr++ = cpu_to_le32(PAGE_SIZE);96}97#endif9899*mptr++ = cpu_to_le32(sg_flags | size);100*mptr++ = cpu_to_le32(i2o_dma_low(dma_addr));101#ifdef CONFIG_I2O_EXT_ADAPTEC_DMA64102if ((sizeof(dma_addr_t) > 4) && c->pae_support)103*mptr++ = cpu_to_le32(i2o_dma_high(dma_addr));104#endif105*sg_ptr = mptr;106}107return dma_addr;108}109EXPORT_SYMBOL_GPL(i2o_dma_map_single);110111/**112* i2o_dma_map_sg - Map a SG List to controller and fill in I2O message.113* @c: I2O controller114* @sg: SG list to be mapped115* @sg_count: number of elements in the SG list116* @direction: DMA_TO_DEVICE / DMA_FROM_DEVICE117* @sg_ptr: pointer to the SG list inside the I2O message118*119* This function does all necessary DMA handling and also writes the I2O120* SGL elements into the I2O message. For details on DMA handling see also121* dma_map_sg(). The pointer sg_ptr will only be set to the end of the SG122* list if the allocation was successful.123*124* Returns 0 on failure or 1 on success.125*/126int i2o_dma_map_sg(struct i2o_controller *c, struct scatterlist *sg,127int sg_count, enum dma_data_direction direction, u32 ** sg_ptr)128{129u32 sg_flags;130u32 *mptr = *sg_ptr;131132switch (direction) {133case DMA_TO_DEVICE:134sg_flags = 0x14000000;135break;136case DMA_FROM_DEVICE:137sg_flags = 0x10000000;138break;139default:140return 0;141}142143sg_count = dma_map_sg(&c->pdev->dev, sg, sg_count, direction);144if (!sg_count)145return 0;146147#ifdef CONFIG_I2O_EXT_ADAPTEC_DMA64148if ((sizeof(dma_addr_t) > 4) && c->pae_support) {149*mptr++ = cpu_to_le32(0x7C020002);150*mptr++ = cpu_to_le32(PAGE_SIZE);151}152#endif153154while (sg_count-- > 0) {155if (!sg_count)156sg_flags |= 0xC0000000;157*mptr++ = cpu_to_le32(sg_flags | sg_dma_len(sg));158*mptr++ = cpu_to_le32(i2o_dma_low(sg_dma_address(sg)));159#ifdef CONFIG_I2O_EXT_ADAPTEC_DMA64160if ((sizeof(dma_addr_t) > 4) && c->pae_support)161*mptr++ = cpu_to_le32(i2o_dma_high(sg_dma_address(sg)));162#endif163sg = sg_next(sg);164}165*sg_ptr = mptr;166167return 1;168}169EXPORT_SYMBOL_GPL(i2o_dma_map_sg);170171/**172* i2o_dma_alloc - Allocate DMA memory173* @dev: struct device pointer to the PCI device of the I2O controller174* @addr: i2o_dma struct which should get the DMA buffer175* @len: length of the new DMA memory176*177* Allocate a coherent DMA memory and write the pointers into addr.178*179* Returns 0 on success or -ENOMEM on failure.180*/181int i2o_dma_alloc(struct device *dev, struct i2o_dma *addr, size_t len)182{183struct pci_dev *pdev = to_pci_dev(dev);184int dma_64 = 0;185186mutex_lock(&mem_lock);187if ((sizeof(dma_addr_t) > 4) && (pdev->dma_mask == DMA_BIT_MASK(64))) {188dma_64 = 1;189if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {190mutex_unlock(&mem_lock);191return -ENOMEM;192}193}194195addr->virt = dma_alloc_coherent(dev, len, &addr->phys, GFP_KERNEL);196197if ((sizeof(dma_addr_t) > 4) && dma_64)198if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))199printk(KERN_WARNING "i2o: unable to set 64-bit DMA");200mutex_unlock(&mem_lock);201202if (!addr->virt)203return -ENOMEM;204205memset(addr->virt, 0, len);206addr->len = len;207208return 0;209}210EXPORT_SYMBOL_GPL(i2o_dma_alloc);211212213/**214* i2o_dma_free - Free DMA memory215* @dev: struct device pointer to the PCI device of the I2O controller216* @addr: i2o_dma struct which contains the DMA buffer217*218* Free a coherent DMA memory and set virtual address of addr to NULL.219*/220void i2o_dma_free(struct device *dev, struct i2o_dma *addr)221{222if (addr->virt) {223if (addr->phys)224dma_free_coherent(dev, addr->len, addr->virt,225addr->phys);226else227kfree(addr->virt);228addr->virt = NULL;229}230}231EXPORT_SYMBOL_GPL(i2o_dma_free);232233234/**235* i2o_dma_realloc - Realloc DMA memory236* @dev: struct device pointer to the PCI device of the I2O controller237* @addr: pointer to a i2o_dma struct DMA buffer238* @len: new length of memory239*240* If there was something allocated in the addr, free it first. If len > 0241* than try to allocate it and write the addresses back to the addr242* structure. If len == 0 set the virtual address to NULL.243*244* Returns the 0 on success or negative error code on failure.245*/246int i2o_dma_realloc(struct device *dev, struct i2o_dma *addr, size_t len)247{248i2o_dma_free(dev, addr);249250if (len)251return i2o_dma_alloc(dev, addr, len);252253return 0;254}255EXPORT_SYMBOL_GPL(i2o_dma_realloc);256257/*258* i2o_pool_alloc - Allocate an slab cache and mempool259* @mempool: pointer to struct i2o_pool to write data into.260* @name: name which is used to identify cache261* @size: size of each object262* @min_nr: minimum number of objects263*264* First allocates a slab cache with name and size. Then allocates a265* mempool which uses the slab cache for allocation and freeing.266*267* Returns 0 on success or negative error code on failure.268*/269int i2o_pool_alloc(struct i2o_pool *pool, const char *name,270size_t size, int min_nr)271{272pool->name = kmalloc(strlen(name) + 1, GFP_KERNEL);273if (!pool->name)274goto exit;275strcpy(pool->name, name);276277pool->slab =278kmem_cache_create(pool->name, size, 0, SLAB_HWCACHE_ALIGN, NULL);279if (!pool->slab)280goto free_name;281282pool->mempool = mempool_create_slab_pool(min_nr, pool->slab);283if (!pool->mempool)284goto free_slab;285286return 0;287288free_slab:289kmem_cache_destroy(pool->slab);290291free_name:292kfree(pool->name);293294exit:295return -ENOMEM;296}297EXPORT_SYMBOL_GPL(i2o_pool_alloc);298299/*300* i2o_pool_free - Free slab cache and mempool again301* @mempool: pointer to struct i2o_pool which should be freed302*303* Note that you have to return all objects to the mempool again before304* calling i2o_pool_free().305*/306void i2o_pool_free(struct i2o_pool *pool)307{308mempool_destroy(pool->mempool);309kmem_cache_destroy(pool->slab);310kfree(pool->name);311};312EXPORT_SYMBOL_GPL(i2o_pool_free);313314315