comminit.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Adaptec AAC series RAID controller driver
  3. * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
  4. *
  5. * based on the old aacraid driver that is..
  6. * Adaptec aacraid device driver for Linux.
  7. *
  8. * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2, or (at your option)
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; see the file COPYING. If not, write to
  22. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * Module Name:
  25. * comminit.c
  26. *
  27. * Abstract: This supports the initialization of the host adapter commuication interface.
  28. * This is a platform dependent module for the pci cyclone board.
  29. *
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/init.h>
  33. #include <linux/types.h>
  34. #include <linux/sched.h>
  35. #include <linux/pci.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/slab.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/completion.h>
  40. #include <linux/mm.h>
  41. #include <asm/semaphore.h>
  42. #include "aacraid.h"
  43. struct aac_common aac_config;
  44. static int aac_alloc_comm(struct aac_dev *dev, void **commaddr, unsigned long commsize, unsigned long commalign)
  45. {
  46. unsigned char *base;
  47. unsigned long size, align;
  48. unsigned long fibsize = 4096;
  49. unsigned long printfbufsiz = 256;
  50. struct aac_init *init;
  51. dma_addr_t phys;
  52. size = fibsize + sizeof(struct aac_init) + commsize + commalign + printfbufsiz;
  53. base = pci_alloc_consistent(dev->pdev, size, &phys);
  54. if(base == NULL)
  55. {
  56. printk(KERN_ERR "aacraid: unable to create mapping.\n");
  57. return 0;
  58. }
  59. dev->comm_addr = (void *)base;
  60. dev->comm_phys = phys;
  61. dev->comm_size = size;
  62. dev->init = (struct aac_init *)(base + fibsize);
  63. dev->init_pa = phys + fibsize;
  64. init = dev->init;
  65. init->InitStructRevision = cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION);
  66. init->MiniPortRevision = cpu_to_le32(Sa_MINIPORT_REVISION);
  67. init->fsrev = cpu_to_le32(dev->fsrev);
  68. /*
  69. * Adapter Fibs are the first thing allocated so that they
  70. * start page aligned
  71. */
  72. dev->aif_base_va = (struct hw_fib *)base;
  73. init->AdapterFibsVirtualAddress = 0;
  74. init->AdapterFibsPhysicalAddress = cpu_to_le32((u32)phys);
  75. init->AdapterFibsSize = cpu_to_le32(fibsize);
  76. init->AdapterFibAlign = cpu_to_le32(sizeof(struct hw_fib));
  77. /*
  78. * number of 4k pages of host physical memory. The aacraid fw needs
  79. * this number to be less than 4gb worth of pages. num_physpages is in
  80. * system page units. New firmware doesn't have any issues with the
  81. * mapping system, but older Firmware did, and had *troubles* dealing
  82. * with the math overloading past 32 bits, thus we must limit this
  83. * field.
  84. *
  85. * This assumes the memory is mapped zero->n, which isnt
  86. * always true on real computers. It also has some slight problems
  87. * with the GART on x86-64. I've btw never tried DMA from PCI space
  88. * on this platform but don't be suprised if its problematic.
  89. */
  90. #ifndef CONFIG_GART_IOMMU
  91. if ((num_physpages << (PAGE_SHIFT - 12)) <= AAC_MAX_HOSTPHYSMEMPAGES) {
  92. init->HostPhysMemPages =
  93. cpu_to_le32(num_physpages << (PAGE_SHIFT-12));
  94. } else
  95. #endif
  96. {
  97. init->HostPhysMemPages = cpu_to_le32(AAC_MAX_HOSTPHYSMEMPAGES);
  98. }
  99. /*
  100. * Increment the base address by the amount already used
  101. */
  102. base = base + fibsize + sizeof(struct aac_init);
  103. phys = (dma_addr_t)((ulong)phys + fibsize + sizeof(struct aac_init));
  104. /*
  105. * Align the beginning of Headers to commalign
  106. */
  107. align = (commalign - ((unsigned long)(base) & (commalign - 1)));
  108. base = base + align;
  109. phys = phys + align;
  110. /*
  111. * Fill in addresses of the Comm Area Headers and Queues
  112. */
  113. *commaddr = base;
  114. init->CommHeaderAddress = cpu_to_le32((u32)phys);
  115. /*
  116. * Increment the base address by the size of the CommArea
  117. */
  118. base = base + commsize;
  119. phys = phys + commsize;
  120. /*
  121. * Place the Printf buffer area after the Fast I/O comm area.
  122. */
  123. dev->printfbuf = (void *)base;
  124. init->printfbuf = cpu_to_le32(phys);
  125. init->printfbufsiz = cpu_to_le32(printfbufsiz);
  126. memset(base, 0, printfbufsiz);
  127. return 1;
  128. }
  129. static void aac_queue_init(struct aac_dev * dev, struct aac_queue * q, u32 *mem, int qsize)
  130. {
  131. q->numpending = 0;
  132. q->dev = dev;
  133. INIT_LIST_HEAD(&q->pendingq);
  134. init_waitqueue_head(&q->cmdready);
  135. INIT_LIST_HEAD(&q->cmdq);
  136. init_waitqueue_head(&q->qfull);
  137. spin_lock_init(&q->lockdata);
  138. q->lock = &q->lockdata;
  139. q->headers.producer = (__le32 *)mem;
  140. q->headers.consumer = (__le32 *)(mem+1);
  141. *(q->headers.producer) = cpu_to_le32(qsize);
  142. *(q->headers.consumer) = cpu_to_le32(qsize);
  143. q->entries = qsize;
  144. }
  145. /**
  146. * aac_send_shutdown - shutdown an adapter
  147. * @dev: Adapter to shutdown
  148. *
  149. * This routine will send a VM_CloseAll (shutdown) request to the adapter.
  150. */
  151. int aac_send_shutdown(struct aac_dev * dev)
  152. {
  153. struct fib * fibctx;
  154. struct aac_close *cmd;
  155. int status;
  156. fibctx = fib_alloc(dev);
  157. fib_init(fibctx);
  158. cmd = (struct aac_close *) fib_data(fibctx);
  159. cmd->command = cpu_to_le32(VM_CloseAll);
  160. cmd->cid = cpu_to_le32(0xffffffff);
  161. status = fib_send(ContainerCommand,
  162. fibctx,
  163. sizeof(struct aac_close),
  164. FsaNormal,
  165. 1, 1,
  166. NULL, NULL);
  167. if (status == 0)
  168. fib_complete(fibctx);
  169. fib_free(fibctx);
  170. return status;
  171. }
  172. /**
  173. * aac_comm_init - Initialise FSA data structures
  174. * @dev: Adapter to initialise
  175. *
  176. * Initializes the data structures that are required for the FSA commuication
  177. * interface to operate.
  178. * Returns
  179. * 1 - if we were able to init the commuication interface.
  180. * 0 - If there were errors initing. This is a fatal error.
  181. */
  182. static int aac_comm_init(struct aac_dev * dev)
  183. {
  184. unsigned long hdrsize = (sizeof(u32) * NUMBER_OF_COMM_QUEUES) * 2;
  185. unsigned long queuesize = sizeof(struct aac_entry) * TOTAL_QUEUE_ENTRIES;
  186. u32 *headers;
  187. struct aac_entry * queues;
  188. unsigned long size;
  189. struct aac_queue_block * comm = dev->queues;
  190. /*
  191. * Now allocate and initialize the zone structures used as our
  192. * pool of FIB context records. The size of the zone is based
  193. * on the system memory size. We also initialize the mutex used
  194. * to protect the zone.
  195. */
  196. spin_lock_init(&dev->fib_lock);
  197. /*
  198. * Allocate the physically contigous space for the commuication
  199. * queue headers.
  200. */
  201. size = hdrsize + queuesize;
  202. if (!aac_alloc_comm(dev, (void * *)&headers, size, QUEUE_ALIGNMENT))
  203. return -ENOMEM;
  204. queues = (struct aac_entry *)(((ulong)headers) + hdrsize);
  205. /* Adapter to Host normal priority Command queue */
  206. comm->queue[HostNormCmdQueue].base = queues;
  207. aac_queue_init(dev, &comm->queue[HostNormCmdQueue], headers, HOST_NORM_CMD_ENTRIES);
  208. queues += HOST_NORM_CMD_ENTRIES;
  209. headers += 2;
  210. /* Adapter to Host high priority command queue */
  211. comm->queue[HostHighCmdQueue].base = queues;
  212. aac_queue_init(dev, &comm->queue[HostHighCmdQueue], headers, HOST_HIGH_CMD_ENTRIES);
  213. queues += HOST_HIGH_CMD_ENTRIES;
  214. headers +=2;
  215. /* Host to adapter normal priority command queue */
  216. comm->queue[AdapNormCmdQueue].base = queues;
  217. aac_queue_init(dev, &comm->queue[AdapNormCmdQueue], headers, ADAP_NORM_CMD_ENTRIES);
  218. queues += ADAP_NORM_CMD_ENTRIES;
  219. headers += 2;
  220. /* host to adapter high priority command queue */
  221. comm->queue[AdapHighCmdQueue].base = queues;
  222. aac_queue_init(dev, &comm->queue[AdapHighCmdQueue], headers, ADAP_HIGH_CMD_ENTRIES);
  223. queues += ADAP_HIGH_CMD_ENTRIES;
  224. headers += 2;
  225. /* adapter to host normal priority response queue */
  226. comm->queue[HostNormRespQueue].base = queues;
  227. aac_queue_init(dev, &comm->queue[HostNormRespQueue], headers, HOST_NORM_RESP_ENTRIES);
  228. queues += HOST_NORM_RESP_ENTRIES;
  229. headers += 2;
  230. /* adapter to host high priority response queue */
  231. comm->queue[HostHighRespQueue].base = queues;
  232. aac_queue_init(dev, &comm->queue[HostHighRespQueue], headers, HOST_HIGH_RESP_ENTRIES);
  233. queues += HOST_HIGH_RESP_ENTRIES;
  234. headers += 2;
  235. /* host to adapter normal priority response queue */
  236. comm->queue[AdapNormRespQueue].base = queues;
  237. aac_queue_init(dev, &comm->queue[AdapNormRespQueue], headers, ADAP_NORM_RESP_ENTRIES);
  238. queues += ADAP_NORM_RESP_ENTRIES;
  239. headers += 2;
  240. /* host to adapter high priority response queue */
  241. comm->queue[AdapHighRespQueue].base = queues;
  242. aac_queue_init(dev, &comm->queue[AdapHighRespQueue], headers, ADAP_HIGH_RESP_ENTRIES);
  243. comm->queue[AdapNormCmdQueue].lock = comm->queue[HostNormRespQueue].lock;
  244. comm->queue[AdapHighCmdQueue].lock = comm->queue[HostHighRespQueue].lock;
  245. comm->queue[AdapNormRespQueue].lock = comm->queue[HostNormCmdQueue].lock;
  246. comm->queue[AdapHighRespQueue].lock = comm->queue[HostHighCmdQueue].lock;
  247. return 0;
  248. }
  249. struct aac_dev *aac_init_adapter(struct aac_dev *dev)
  250. {
  251. /*
  252. * Ok now init the communication subsystem
  253. */
  254. dev->queues = (struct aac_queue_block *) kmalloc(sizeof(struct aac_queue_block), GFP_KERNEL);
  255. if (dev->queues == NULL) {
  256. printk(KERN_ERR "Error could not allocate comm region.\n");
  257. return NULL;
  258. }
  259. memset(dev->queues, 0, sizeof(struct aac_queue_block));
  260. if (aac_comm_init(dev)<0){
  261. kfree(dev->queues);
  262. return NULL;
  263. }
  264. /*
  265. * Initialize the list of fibs
  266. */
  267. if(fib_setup(dev)<0){
  268. kfree(dev->queues);
  269. return NULL;
  270. }
  271. INIT_LIST_HEAD(&dev->fib_list);
  272. init_completion(&dev->aif_completion);
  273. return dev;
  274. }