Release 4.9 drivers/usb/host/ehci-mem.c
  
  
/*
 * Copyright (c) 2001 by David Brownell
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/* this file is part of ehci-hcd.c */
/*-------------------------------------------------------------------------*/
/*
 * There's basically three types of memory:
 *      - data used only by the HCD ... kmalloc is fine
 *      - async and periodic schedules, shared by HC and HCD ... these
 *        need to use dma_pool or dma_alloc_coherent
 *      - driver buffers, read/written by HC ... single shot DMA mapped
 *
 * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
 * No memory seen by this driver is pageable.
 */
/*-------------------------------------------------------------------------*/
/* Allocate the key transfer structures from the previously allocated pool */
static inline void ehci_qtd_init(struct ehci_hcd *ehci, struct ehci_qtd *qtd,
				  dma_addr_t dma)
{
	memset (qtd, 0, sizeof *qtd);
	qtd->qtd_dma = dma;
	qtd->hw_token = cpu_to_hc32(ehci, QTD_STS_HALT);
	qtd->hw_next = EHCI_LIST_END(ehci);
	qtd->hw_alt_next = EHCI_LIST_END(ehci);
	INIT_LIST_HEAD (&qtd->qtd_list);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| david brownell | david brownell | 60 | 81.08% | 3 | 60.00% | 
| stefan roese | stefan roese | 11 | 14.86% | 1 | 20.00% | 
| anatolij gustschin | anatolij gustschin | 3 | 4.05% | 1 | 20.00% | 
 | Total | 74 | 100.00% | 5 | 100.00% | 
static struct ehci_qtd *ehci_qtd_alloc (struct ehci_hcd *ehci, gfp_t flags)
{
	struct ehci_qtd		*qtd;
	dma_addr_t		dma;
	qtd = dma_pool_alloc (ehci->qtd_pool, flags, &dma);
	if (qtd != NULL) {
		ehci_qtd_init(ehci, qtd, dma);
	}
	return qtd;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus torvalds | linus torvalds | 50 | 86.21% | 1 | 14.29% | 
| david brownell | david brownell | 3 | 5.17% | 2 | 28.57% | 
| stefan roese | stefan roese | 2 | 3.45% | 1 | 14.29% | 
| alexey dobriyan | alexey dobriyan | 1 | 1.72% | 1 | 14.29% | 
| al viro | al viro | 1 | 1.72% | 1 | 14.29% | 
| deepak saxena | deepak saxena | 1 | 1.72% | 1 | 14.29% | 
 | Total | 58 | 100.00% | 7 | 100.00% | 
static inline void ehci_qtd_free (struct ehci_hcd *ehci, struct ehci_qtd *qtd)
{
	dma_pool_free (ehci->qtd_pool, qtd, qtd->qtd_dma);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus torvalds | linus torvalds | 29 | 96.67% | 1 | 50.00% | 
| deepak saxena | deepak saxena | 1 | 3.33% | 1 | 50.00% | 
 | Total | 30 | 100.00% | 2 | 100.00% | 
static void qh_destroy(struct ehci_hcd *ehci, struct ehci_qh *qh)
{
	/* clean qtds first, and know this is not linked */
	if (!list_empty (&qh->qtd_list) || qh->qh_next.ptr) {
		ehci_dbg (ehci, "unused qh not empty!\n");
		BUG ();
	}
	if (qh->dummy)
		ehci_qtd_free (ehci, qh->dummy);
	dma_pool_free(ehci->qh_pool, qh->hw, qh->qh_dma);
	kfree(qh);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| greg kroah-hartman | greg kroah-hartman | 67 | 82.72% | 1 | 25.00% | 
| alek du | alek du | 7 | 8.64% | 1 | 25.00% | 
| alan stern | alan stern | 5 | 6.17% | 1 | 25.00% | 
| david brownell | david brownell | 2 | 2.47% | 1 | 25.00% | 
 | Total | 81 | 100.00% | 4 | 100.00% | 
static struct ehci_qh *ehci_qh_alloc (struct ehci_hcd *ehci, gfp_t flags)
{
	struct ehci_qh		*qh;
	dma_addr_t		dma;
	qh = kzalloc(sizeof *qh, GFP_ATOMIC);
	if (!qh)
		goto done;
	qh->hw = (struct ehci_qh_hw *)
		dma_pool_alloc(ehci->qh_pool, flags, &dma);
	if (!qh->hw)
		goto fail;
	memset(qh->hw, 0, sizeof *qh->hw);
	qh->qh_dma = dma;
	// INIT_LIST_HEAD (&qh->qh_list);
	INIT_LIST_HEAD (&qh->qtd_list);
	INIT_LIST_HEAD(&qh->unlink_node);
	/* dummy td enables safe urb queuing */
	qh->dummy = ehci_qtd_alloc (ehci, flags);
	if (qh->dummy == NULL) {
		ehci_dbg (ehci, "no dummy td\n");
		goto fail1;
	}
done:
	return qh;
fail1:
	dma_pool_free(ehci->qh_pool, qh->hw, qh->qh_dma);
fail:
	kfree(qh);
	return NULL;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus torvalds | linus torvalds | 72 | 40.91% | 1 | 12.50% | 
| alek du | alek du | 50 | 28.41% | 1 | 12.50% | 
| david brownell | david brownell | 42 | 23.86% | 2 | 25.00% | 
| ming lei | ming lei | 8 | 4.55% | 1 | 12.50% | 
| deepak saxena | deepak saxena | 2 | 1.14% | 1 | 12.50% | 
| alexey dobriyan | alexey dobriyan | 1 | 0.57% | 1 | 12.50% | 
| al viro | al viro | 1 | 0.57% | 1 | 12.50% | 
 | Total | 176 | 100.00% | 8 | 100.00% | 
/*-------------------------------------------------------------------------*/
/* The queue heads and transfer descriptors are managed from pools tied
 * to each of the "per device" structures.
 * This is the initialisation and cleanup code.
 */
static void ehci_mem_cleanup (struct ehci_hcd *ehci)
{
	if (ehci->async)
		qh_destroy(ehci, ehci->async);
	ehci->async = NULL;
	if (ehci->dummy)
		qh_destroy(ehci, ehci->dummy);
	ehci->dummy = NULL;
	/* DMA consistent memory and pools */
	dma_pool_destroy(ehci->qtd_pool);
	ehci->qtd_pool = NULL;
	dma_pool_destroy(ehci->qh_pool);
	ehci->qh_pool = NULL;
	dma_pool_destroy(ehci->itd_pool);
	ehci->itd_pool = NULL;
	dma_pool_destroy(ehci->sitd_pool);
	ehci->sitd_pool = NULL;
	if (ehci->periodic)
		dma_free_coherent (ehci_to_hcd(ehci)->self.controller,
			ehci->periodic_size * sizeof (u32),
			ehci->periodic, ehci->periodic_dma);
	ehci->periodic = NULL;
	/* shadow periodic table */
	kfree(ehci->pshadow);
	ehci->pshadow = NULL;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus torvalds | linus torvalds | 101 | 62.73% | 1 | 14.29% | 
| andiry xu | andiry xu | 18 | 11.18% | 1 | 14.29% | 
| david brownell | david brownell | 17 | 10.56% | 1 | 14.29% | 
| alan stern | alan stern | 9 | 5.59% | 2 | 28.57% | 
| deepak saxena | deepak saxena | 9 | 5.59% | 1 | 14.29% | 
| mika kukkonen | mika kukkonen | 7 | 4.35% | 1 | 14.29% | 
 | Total | 161 | 100.00% | 7 | 100.00% | 
/* remember to add cleanup code (above) if you add anything here */
static int ehci_mem_init (struct ehci_hcd *ehci, gfp_t flags)
{
	int i;
	/* QTDs for control/bulk/intr transfers */
	ehci->qtd_pool = dma_pool_create ("ehci_qtd",
			ehci_to_hcd(ehci)->self.controller,
			sizeof (struct ehci_qtd),
			32 /* byte alignment (for hw parts) */,
			4096 /* can't cross 4K */);
	if (!ehci->qtd_pool) {
		goto fail;
	}
	/* QHs for control/bulk/intr transfers */
	ehci->qh_pool = dma_pool_create ("ehci_qh",
			ehci_to_hcd(ehci)->self.controller,
			sizeof(struct ehci_qh_hw),
			32 /* byte alignment (for hw parts) */,
			4096 /* can't cross 4K */);
	if (!ehci->qh_pool) {
		goto fail;
	}
	ehci->async = ehci_qh_alloc (ehci, flags);
	if (!ehci->async) {
		goto fail;
	}
	/* ITD for high speed ISO transfers */
	ehci->itd_pool = dma_pool_create ("ehci_itd",
			ehci_to_hcd(ehci)->self.controller,
			sizeof (struct ehci_itd),
			32 /* byte alignment (for hw parts) */,
			4096 /* can't cross 4K */);
	if (!ehci->itd_pool) {
		goto fail;
	}
	/* SITD for full/low speed split ISO transfers */
	ehci->sitd_pool = dma_pool_create ("ehci_sitd",
			ehci_to_hcd(ehci)->self.controller,
			sizeof (struct ehci_sitd),
			32 /* byte alignment (for hw parts) */,
			4096 /* can't cross 4K */);
	if (!ehci->sitd_pool) {
		goto fail;
	}
	/* Hardware periodic table */
	ehci->periodic = (__le32 *)
		dma_alloc_coherent (ehci_to_hcd(ehci)->self.controller,
			ehci->periodic_size * sizeof(__le32),
			&ehci->periodic_dma, flags);
	if (ehci->periodic == NULL) {
		goto fail;
	}
	if (ehci->use_dummy_qh) {
		struct ehci_qh_hw	*hw;
		ehci->dummy = ehci_qh_alloc(ehci, flags);
		if (!ehci->dummy)
			goto fail;
		hw = ehci->dummy->hw;
		hw->hw_next = EHCI_LIST_END(ehci);
		hw->hw_qtd_next = EHCI_LIST_END(ehci);
		hw->hw_alt_next = EHCI_LIST_END(ehci);
		ehci->dummy->hw = hw;
		for (i = 0; i < ehci->periodic_size; i++)
			ehci->periodic[i] = cpu_to_hc32(ehci,
					ehci->dummy->qh_dma);
	} else {
		for (i = 0; i < ehci->periodic_size; i++)
			ehci->periodic[i] = EHCI_LIST_END(ehci);
	}
	/* software shadow of hardware table */
	ehci->pshadow = kcalloc(ehci->periodic_size, sizeof(void *), flags);
	if (ehci->pshadow != NULL)
		return 0;
fail:
	ehci_dbg (ehci, "couldn't init memory\n");
	ehci_mem_cleanup (ehci);
	return -ENOMEM;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus torvalds | linus torvalds | 236 | 52.33% | 1 | 7.69% | 
| andiry xu | andiry xu | 108 | 23.95% | 1 | 7.69% | 
| david brownell | david brownell | 53 | 11.75% | 1 | 7.69% | 
| deepak saxena | deepak saxena | 21 | 4.66% | 1 | 7.69% | 
| alan stern | alan stern | 20 | 4.43% | 2 | 15.38% | 
| eric sesterhenn | eric sesterhenn | 3 | 0.67% | 1 | 7.69% | 
| al viro | al viro | 3 | 0.67% | 2 | 15.38% | 
| stefan roese | stefan roese | 3 | 0.67% | 1 | 7.69% | 
| alexey dobriyan | alexey dobriyan | 2 | 0.44% | 1 | 7.69% | 
| vladimir zapolskiy | vladimir zapolskiy | 1 | 0.22% | 1 | 7.69% | 
| alek du | alek du | 1 | 0.22% | 1 | 7.69% | 
 | Total | 451 | 100.00% | 13 | 100.00% | 
Overall Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus torvalds | linus torvalds | 494 | 47.50% | 1 | 4.00% | 
| david brownell | david brownell | 179 | 17.21% | 8 | 32.00% | 
| andiry xu | andiry xu | 126 | 12.12% | 1 | 4.00% | 
| greg kroah-hartman | greg kroah-hartman | 67 | 6.44% | 1 | 4.00% | 
| alek du | alek du | 58 | 5.58% | 1 | 4.00% | 
| deepak saxena | deepak saxena | 34 | 3.27% | 1 | 4.00% | 
| alan stern | alan stern | 34 | 3.27% | 3 | 12.00% | 
| stefan roese | stefan roese | 17 | 1.63% | 1 | 4.00% | 
| ming lei | ming lei | 8 | 0.77% | 1 | 4.00% | 
| mika kukkonen | mika kukkonen | 7 | 0.67% | 1 | 4.00% | 
| al viro | al viro | 5 | 0.48% | 2 | 8.00% | 
| alexey dobriyan | alexey dobriyan | 4 | 0.38% | 1 | 4.00% | 
| eric sesterhenn | eric sesterhenn | 3 | 0.29% | 1 | 4.00% | 
| anatolij gustschin | anatolij gustschin | 3 | 0.29% | 1 | 4.00% | 
| vladimir zapolskiy | vladimir zapolskiy | 1 | 0.10% | 1 | 4.00% | 
 | Total | 1040 | 100.00% | 25 | 100.00% |