scsi_dma.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:14k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  scsi_dma.c Copyright (C) 2000 Eric Youngdale
  3.  *
  4.  *  mid-level SCSI DMA bounce buffer allocator
  5.  *
  6.  */
  7. #define __NO_VERSION__
  8. #include <linux/config.h>
  9. #include <linux/module.h>
  10. #include <linux/blk.h>
  11. #include "scsi.h"
  12. #include "hosts.h"
  13. #include "constants.h"
  14. #ifdef CONFIG_KMOD
  15. #include <linux/kmod.h>
  16. #endif
  17. /*
  18.  * PAGE_SIZE must be a multiple of the sector size (512).  True
  19.  * for all reasonably recent architectures (even the VAX...).
  20.  */
  21. #define SECTOR_SIZE 512
  22. #define SECTORS_PER_PAGE (PAGE_SIZE/SECTOR_SIZE)
  23. #if SECTORS_PER_PAGE <= 8
  24. typedef unsigned char FreeSectorBitmap;
  25. #elif SECTORS_PER_PAGE <= 32
  26. typedef unsigned int FreeSectorBitmap;
  27. #else
  28. #error You lose.
  29. #endif
  30. /*
  31.  * Used for access to internal allocator used for DMA safe buffers.
  32.  */
  33. static spinlock_t allocator_request_lock = SPIN_LOCK_UNLOCKED;
  34. static FreeSectorBitmap *dma_malloc_freelist = NULL;
  35. static int need_isa_bounce_buffers;
  36. static unsigned int dma_sectors = 0;
  37. unsigned int scsi_dma_free_sectors = 0;
  38. unsigned int scsi_need_isa_buffer = 0;
  39. static unsigned char **dma_malloc_pages = NULL;
  40. /*
  41.  * Function:    scsi_malloc
  42.  *
  43.  * Purpose:     Allocate memory from the DMA-safe pool.
  44.  *
  45.  * Arguments:   len       - amount of memory we need.
  46.  *
  47.  * Lock status: No locks assumed to be held.  This function is SMP-safe.
  48.  *
  49.  * Returns:     Pointer to memory block.
  50.  *
  51.  * Notes:       Prior to the new queue code, this function was not SMP-safe.
  52.  *              This function can only allocate in units of sectors
  53.  *              (i.e. 512 bytes).
  54.  *
  55.  *              We cannot use the normal system allocator becuase we need
  56.  *              to be able to guarantee that we can process a complete disk
  57.  *              I/O request without touching the system allocator.  Think
  58.  *              about it - if the system were heavily swapping, and tried to
  59.  *              write out a block of memory to disk, and the SCSI code needed
  60.  *              to allocate more memory in order to be able to write the
  61.  *              data to disk, you would wedge the system.
  62.  */
  63. void *scsi_malloc(unsigned int len)
  64. {
  65. unsigned int nbits, mask;
  66. unsigned long flags;
  67. int i, j;
  68. if (len % SECTOR_SIZE != 0 || len > PAGE_SIZE)
  69. return NULL;
  70. nbits = len >> 9;
  71. mask = (1 << nbits) - 1;
  72. spin_lock_irqsave(&allocator_request_lock, flags);
  73. for (i = 0; i < dma_sectors / SECTORS_PER_PAGE; i++)
  74. for (j = 0; j <= SECTORS_PER_PAGE - nbits; j++) {
  75. if ((dma_malloc_freelist[i] & (mask << j)) == 0) {
  76. dma_malloc_freelist[i] |= (mask << j);
  77. scsi_dma_free_sectors -= nbits;
  78. #ifdef DEBUG
  79. SCSI_LOG_MLQUEUE(3, printk("SMalloc: %d %p [From:%p]n", len, dma_malloc_pages[i] + (j << 9)));
  80. printk("SMalloc: %d %p [From:%p]n", len, dma_malloc_pages[i] + (j << 9));
  81. #endif
  82. spin_unlock_irqrestore(&allocator_request_lock, flags);
  83. return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
  84. }
  85. }
  86. spin_unlock_irqrestore(&allocator_request_lock, flags);
  87. return NULL; /* Nope.  No more */
  88. }
  89. /*
  90.  * Function:    scsi_free
  91.  *
  92.  * Purpose:     Free memory into the DMA-safe pool.
  93.  *
  94.  * Arguments:   ptr       - data block we are freeing.
  95.  *              len       - size of block we are freeing.
  96.  *
  97.  * Lock status: No locks assumed to be held.  This function is SMP-safe.
  98.  *
  99.  * Returns:     Nothing
  100.  *
  101.  * Notes:       This function *must* only be used to free memory
  102.  *              allocated from scsi_malloc().
  103.  *
  104.  *              Prior to the new queue code, this function was not SMP-safe.
  105.  *              This function can only allocate in units of sectors
  106.  *              (i.e. 512 bytes).
  107.  */
  108. int scsi_free(void *obj, unsigned int len)
  109. {
  110. unsigned int page, sector, nbits, mask;
  111. unsigned long flags;
  112. #ifdef DEBUG
  113. unsigned long ret = 0;
  114. #ifdef __mips__
  115. __asm__ __volatile__("movet%0,$31":"=r"(ret));
  116. #else
  117. ret = __builtin_return_address(0);
  118. #endif
  119. printk("scsi_free %p %dn", obj, len);
  120. SCSI_LOG_MLQUEUE(3, printk("SFree: %p %dn", obj, len));
  121. #endif
  122. spin_lock_irqsave(&allocator_request_lock, flags);
  123. for (page = 0; page < dma_sectors / SECTORS_PER_PAGE; page++) {
  124. unsigned long page_addr = (unsigned long) dma_malloc_pages[page];
  125. if ((unsigned long) obj >= page_addr &&
  126.     (unsigned long) obj < page_addr + PAGE_SIZE) {
  127. sector = (((unsigned long) obj) - page_addr) >> 9;
  128. nbits = len >> 9;
  129. mask = (1 << nbits) - 1;
  130. if (sector + nbits > SECTORS_PER_PAGE)
  131. panic("scsi_free:Bad memory alignment");
  132. if ((dma_malloc_freelist[page] &
  133.      (mask << sector)) != (mask << sector)) {
  134. #ifdef DEBUG
  135. printk("scsi_free(obj=%p, len=%d) called from %08lxn",
  136.        obj, len, ret);
  137. #endif
  138. panic("scsi_free:Trying to free unused memory");
  139. }
  140. scsi_dma_free_sectors += nbits;
  141. dma_malloc_freelist[page] &= ~(mask << sector);
  142. spin_unlock_irqrestore(&allocator_request_lock, flags);
  143. return 0;
  144. }
  145. }
  146. panic("scsi_free:Bad offset");
  147. }
  148. /*
  149.  * Function:    scsi_resize_dma_pool
  150.  *
  151.  * Purpose:     Ensure that the DMA pool is sufficiently large to be
  152.  *              able to guarantee that we can always process I/O requests
  153.  *              without calling the system allocator.
  154.  *
  155.  * Arguments:   None.
  156.  *
  157.  * Lock status: No locks assumed to be held.  This function is SMP-safe.
  158.  *
  159.  * Returns:     Nothing
  160.  *
  161.  * Notes:       Prior to the new queue code, this function was not SMP-safe.
  162.  *              Go through the device list and recompute the most appropriate
  163.  *              size for the dma pool.  Then grab more memory (as required).
  164.  */
  165. void scsi_resize_dma_pool(void)
  166. {
  167. int i, k;
  168. unsigned long size;
  169. unsigned long flags;
  170. struct Scsi_Host *shpnt;
  171. struct Scsi_Host *host = NULL;
  172. Scsi_Device *SDpnt;
  173. FreeSectorBitmap *new_dma_malloc_freelist = NULL;
  174. unsigned int new_dma_sectors = 0;
  175. unsigned int new_need_isa_buffer = 0;
  176. unsigned char **new_dma_malloc_pages = NULL;
  177. int out_of_space = 0;
  178. spin_lock_irqsave(&allocator_request_lock, flags);
  179. if (!scsi_hostlist) {
  180. /*
  181.  * Free up the DMA pool.
  182.  */
  183. if (scsi_dma_free_sectors != dma_sectors)
  184. panic("SCSI DMA pool memory leak %d %dn", scsi_dma_free_sectors, dma_sectors);
  185. for (i = 0; i < dma_sectors / SECTORS_PER_PAGE; i++)
  186. free_pages((unsigned long) dma_malloc_pages[i], 0);
  187. if (dma_malloc_pages)
  188. kfree((char *) dma_malloc_pages);
  189. dma_malloc_pages = NULL;
  190. if (dma_malloc_freelist)
  191. kfree((char *) dma_malloc_freelist);
  192. dma_malloc_freelist = NULL;
  193. dma_sectors = 0;
  194. scsi_dma_free_sectors = 0;
  195. spin_unlock_irqrestore(&allocator_request_lock, flags);
  196. return;
  197. }
  198. /* Next, check to see if we need to extend the DMA buffer pool */
  199. new_dma_sectors = 2 * SECTORS_PER_PAGE; /* Base value we use */
  200. if (__pa(high_memory) - 1 > ISA_DMA_THRESHOLD)
  201. need_isa_bounce_buffers = 1;
  202. else
  203. need_isa_bounce_buffers = 0;
  204. if (scsi_devicelist)
  205. for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
  206. new_dma_sectors += SECTORS_PER_PAGE; /* Increment for each host */
  207. for (host = scsi_hostlist; host; host = host->next) {
  208. for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
  209. /*
  210.  * sd and sr drivers allocate scatterlists.
  211.  * sr drivers may allocate for each command 1x2048 or 2x1024 extra
  212.  * buffers for 2k sector size and 1k fs.
  213.  * sg driver allocates buffers < 4k.
  214.  * st driver does not need buffers from the dma pool.
  215.  * estimate 4k buffer/command for devices of unknown type (should panic).
  216.  */
  217. if (SDpnt->type == TYPE_WORM || SDpnt->type == TYPE_ROM ||
  218.     SDpnt->type == TYPE_DISK || SDpnt->type == TYPE_MOD) {
  219. int nents = host->sg_tablesize;
  220. #ifdef DMA_CHUNK_SIZE
  221. /* If the architecture does DMA sg merging, make sure
  222.    we count with at least 64 entries even for HBAs
  223.    which handle very few sg entries.  */
  224. if (nents < 64) nents = 64;
  225. #endif
  226. new_dma_sectors += ((nents *
  227. sizeof(struct scatterlist) + 511) >> 9) *
  228.  SDpnt->queue_depth;
  229. if (SDpnt->type == TYPE_WORM || SDpnt->type == TYPE_ROM)
  230. new_dma_sectors += (2048 >> 9) * SDpnt->queue_depth;
  231. } else if (SDpnt->type == TYPE_SCANNER ||
  232.    SDpnt->type == TYPE_PRINTER ||
  233.    SDpnt->type == TYPE_PROCESSOR ||
  234.    SDpnt->type == TYPE_COMM ||
  235.    SDpnt->type == TYPE_MEDIUM_CHANGER ||
  236.    SDpnt->type == TYPE_ENCLOSURE) {
  237. new_dma_sectors += (4096 >> 9) * SDpnt->queue_depth;
  238. } else {
  239. if (SDpnt->type != TYPE_TAPE) {
  240. printk("resize_dma_pool: unknown device type %dn", SDpnt->type);
  241. new_dma_sectors += (4096 >> 9) * SDpnt->queue_depth;
  242. }
  243. }
  244. if (host->unchecked_isa_dma &&
  245.     need_isa_bounce_buffers &&
  246.     SDpnt->type != TYPE_TAPE) {
  247. new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
  248.     SDpnt->queue_depth;
  249. new_need_isa_buffer++;
  250. }
  251. }
  252. }
  253. #ifdef DEBUG_INIT
  254. printk("resize_dma_pool: needed dma sectors = %dn", new_dma_sectors);
  255. #endif
  256. /* limit DMA memory to 32MB: */
  257. new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
  258. /*
  259.  * We never shrink the buffers - this leads to
  260.  * race conditions that I would rather not even think
  261.  * about right now.
  262.  */
  263. #if 0 /* Why do this? No gain and risks out_of_space */
  264. if (new_dma_sectors < dma_sectors)
  265. new_dma_sectors = dma_sectors;
  266. #endif
  267. if (new_dma_sectors <= dma_sectors) {
  268. spin_unlock_irqrestore(&allocator_request_lock, flags);
  269. return; /* best to quit while we are in front */
  270.         }
  271. for (k = 0; k < 20; ++k) { /* just in case */
  272. out_of_space = 0;
  273. size = (new_dma_sectors / SECTORS_PER_PAGE) *
  274.     sizeof(FreeSectorBitmap);
  275. new_dma_malloc_freelist = (FreeSectorBitmap *)
  276.     kmalloc(size, GFP_ATOMIC);
  277. if (new_dma_malloc_freelist) {
  278.                         memset(new_dma_malloc_freelist, 0, size);
  279. size = (new_dma_sectors / SECTORS_PER_PAGE) *
  280.     sizeof(*new_dma_malloc_pages);
  281. new_dma_malloc_pages = (unsigned char **)
  282.     kmalloc(size, GFP_ATOMIC);
  283. if (!new_dma_malloc_pages) {
  284. size = (new_dma_sectors / SECTORS_PER_PAGE) *
  285.     sizeof(FreeSectorBitmap);
  286. kfree((char *) new_dma_malloc_freelist);
  287. out_of_space = 1;
  288. } else {
  289.                                 memset(new_dma_malloc_pages, 0, size);
  290.                         }
  291. } else
  292. out_of_space = 1;
  293. if ((!out_of_space) && (new_dma_sectors > dma_sectors)) {
  294. for (i = dma_sectors / SECTORS_PER_PAGE;
  295.    i < new_dma_sectors / SECTORS_PER_PAGE; i++) {
  296. new_dma_malloc_pages[i] = (unsigned char *)
  297.     __get_free_pages(GFP_ATOMIC | GFP_DMA, 0);
  298. if (!new_dma_malloc_pages[i])
  299. break;
  300. }
  301. if (i != new_dma_sectors / SECTORS_PER_PAGE) { /* clean up */
  302. int k = i;
  303. out_of_space = 1;
  304. for (i = 0; i < k; ++i)
  305. free_pages((unsigned long) new_dma_malloc_pages[i], 0);
  306. }
  307. }
  308. if (out_of_space) { /* try scaling down new_dma_sectors request */
  309. printk("scsi::resize_dma_pool: WARNING, dma_sectors=%u, "
  310.        "wanted=%u, scalingn", dma_sectors, new_dma_sectors);
  311. if (new_dma_sectors < (8 * SECTORS_PER_PAGE))
  312. break; /* pretty well hopeless ... */
  313. new_dma_sectors = (new_dma_sectors * 3) / 4;
  314. new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
  315. if (new_dma_sectors <= dma_sectors)
  316. break; /* stick with what we have got */
  317. } else
  318. break; /* found space ... */
  319. } /* end of for loop */
  320. if (out_of_space) {
  321. spin_unlock_irqrestore(&allocator_request_lock, flags);
  322. scsi_need_isa_buffer = new_need_isa_buffer; /* some useful info */
  323. printk("      WARNING, not enough memory, pool not expandedn");
  324. return;
  325. }
  326. /* When we dick with the actual DMA list, we need to
  327.  * protect things
  328.  */
  329. if (dma_malloc_freelist) {
  330. size = (dma_sectors / SECTORS_PER_PAGE) * sizeof(FreeSectorBitmap);
  331. memcpy(new_dma_malloc_freelist, dma_malloc_freelist, size);
  332. kfree((char *) dma_malloc_freelist);
  333. }
  334. dma_malloc_freelist = new_dma_malloc_freelist;
  335. if (dma_malloc_pages) {
  336. size = (dma_sectors / SECTORS_PER_PAGE) * sizeof(*dma_malloc_pages);
  337. memcpy(new_dma_malloc_pages, dma_malloc_pages, size);
  338. kfree((char *) dma_malloc_pages);
  339. }
  340. scsi_dma_free_sectors += new_dma_sectors - dma_sectors;
  341. dma_malloc_pages = new_dma_malloc_pages;
  342. dma_sectors = new_dma_sectors;
  343. scsi_need_isa_buffer = new_need_isa_buffer;
  344. spin_unlock_irqrestore(&allocator_request_lock, flags);
  345. #ifdef DEBUG_INIT
  346. printk("resize_dma_pool: dma free sectors   = %dn", scsi_dma_free_sectors);
  347. printk("resize_dma_pool: dma sectors        = %dn", dma_sectors);
  348. printk("resize_dma_pool: need isa buffers   = %dn", scsi_need_isa_buffer);
  349. #endif
  350. }
  351. /*
  352.  * Function:    scsi_init_minimal_dma_pool
  353.  *
  354.  * Purpose:     Allocate a minimal (1-page) DMA pool.
  355.  *
  356.  * Arguments:   None.
  357.  *
  358.  * Lock status: No locks assumed to be held.  This function is SMP-safe.
  359.  *
  360.  * Returns:     Nothing
  361.  *
  362.  * Notes:       
  363.  */
  364. int scsi_init_minimal_dma_pool(void)
  365. {
  366. unsigned long size;
  367. unsigned long flags;
  368. int has_space = 0;
  369. spin_lock_irqsave(&allocator_request_lock, flags);
  370. dma_sectors = PAGE_SIZE / SECTOR_SIZE;
  371. scsi_dma_free_sectors = dma_sectors;
  372. /*
  373.  * Set up a minimal DMA buffer list - this will be used during scan_scsis
  374.  * in some cases.
  375.  */
  376. /* One bit per sector to indicate free/busy */
  377. size = (dma_sectors / SECTORS_PER_PAGE) * sizeof(FreeSectorBitmap);
  378. dma_malloc_freelist = (FreeSectorBitmap *)
  379.     kmalloc(size, GFP_ATOMIC);
  380. if (dma_malloc_freelist) {
  381.                 memset(dma_malloc_freelist, 0, size);
  382. /* One pointer per page for the page list */
  383. dma_malloc_pages = (unsigned char **) kmalloc(
  384.                         (dma_sectors / SECTORS_PER_PAGE) * sizeof(*dma_malloc_pages),
  385.      GFP_ATOMIC);
  386. if (dma_malloc_pages) {
  387.                         memset(dma_malloc_pages, 0, size);
  388. dma_malloc_pages[0] = (unsigned char *)
  389.     __get_free_pages(GFP_ATOMIC | GFP_DMA, 0);
  390. if (dma_malloc_pages[0])
  391. has_space = 1;
  392. }
  393. }
  394. if (!has_space) {
  395. if (dma_malloc_freelist) {
  396. kfree((char *) dma_malloc_freelist);
  397. if (dma_malloc_pages)
  398. kfree((char *) dma_malloc_pages);
  399. }
  400. spin_unlock_irqrestore(&allocator_request_lock, flags);
  401. printk("scsi::init_module: failed, out of memoryn");
  402. return 1;
  403. }
  404. spin_unlock_irqrestore(&allocator_request_lock, flags);
  405. return 0;
  406. }