comminit.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 <scsi/scsi_host.h>
  42. #include <asm/semaphore.h>
  43. #include "aacraid.h"
  44. struct aac_common aac_config;
  45. static int aac_alloc_comm(struct aac_dev *dev, void **commaddr, unsigned long commsize, unsigned long commalign)
  46. {
  47. unsigned char *base;
  48. unsigned long size, align;
  49. const unsigned long fibsize = 4096;
  50. const unsigned long printfbufsiz = 256;
  51. struct aac_init *init;
  52. dma_addr_t phys;
  53. size = fibsize + sizeof(struct aac_init) + commsize + commalign + printfbufsiz;
  54. base = pci_alloc_consistent(dev->pdev, size, &phys);
  55. if(base == NULL)
  56. {
  57. printk(KERN_ERR "aacraid: unable to create mapping.\n");
  58. return 0;
  59. }
  60. dev->comm_addr = (void *)base;
  61. dev->comm_phys = phys;
  62. dev->comm_size = size;
  63. dev->init = (struct aac_init *)(base + fibsize);
  64. dev->init_pa = phys + fibsize;
  65. init = dev->init;
  66. init->InitStructRevision = cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION);
  67. if (dev->max_fib_size != sizeof(struct hw_fib))
  68. init->InitStructRevision = cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_4);
  69. init->MiniPortRevision = cpu_to_le32(Sa_MINIPORT_REVISION);
  70. init->fsrev = cpu_to_le32(dev->fsrev);
  71. /*
  72. * Adapter Fibs are the first thing allocated so that they
  73. * start page aligned
  74. */
  75. dev->aif_base_va = (struct hw_fib *)base;
  76. init->AdapterFibsVirtualAddress = 0;
  77. init->AdapterFibsPhysicalAddress = cpu_to_le32((u32)phys);
  78. init->AdapterFibsSize = cpu_to_le32(fibsize);
  79. init->AdapterFibAlign = cpu_to_le32(sizeof(struct hw_fib));
  80. /*
  81. * number of 4k pages of host physical memory. The aacraid fw needs
  82. * this number to be less than 4gb worth of pages. num_physpages is in
  83. * system page units. New firmware doesn't have any issues with the
  84. * mapping system, but older Firmware did, and had *troubles* dealing
  85. * with the math overloading past 32 bits, thus we must limit this
  86. * field.
  87. *
  88. * This assumes the memory is mapped zero->n, which isnt
  89. * always true on real computers. It also has some slight problems
  90. * with the GART on x86-64. I've btw never tried DMA from PCI space
  91. * on this platform but don't be suprised if its problematic.
  92. */
  93. #ifndef CONFIG_GART_IOMMU
  94. if ((num_physpages << (PAGE_SHIFT - 12)) <= AAC_MAX_HOSTPHYSMEMPAGES) {
  95. init->HostPhysMemPages =
  96. cpu_to_le32(num_physpages << (PAGE_SHIFT-12));
  97. } else
  98. #endif
  99. {
  100. init->HostPhysMemPages = cpu_to_le32(AAC_MAX_HOSTPHYSMEMPAGES);
  101. }
  102. init->InitFlags = 0;
  103. init->MaxIoCommands = cpu_to_le32(dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
  104. init->MaxIoSize = cpu_to_le32(dev->scsi_host_ptr->max_sectors << 9);
  105. init->MaxFibSize = cpu_to_le32(dev->max_fib_size);
  106. /*
  107. * Increment the base address by the amount already used
  108. */
  109. base = base + fibsize + sizeof(struct aac_init);
  110. phys = (dma_addr_t)((ulong)phys + fibsize + sizeof(struct aac_init));
  111. /*
  112. * Align the beginning of Headers to commalign
  113. */
  114. align = (commalign - ((unsigned long)(base) & (commalign - 1)));
  115. base = base + align;
  116. phys = phys + align;
  117. /*
  118. * Fill in addresses of the Comm Area Headers and Queues
  119. */
  120. *commaddr = base;
  121. init->CommHeaderAddress = cpu_to_le32((u32)phys);
  122. /*
  123. * Increment the base address by the size of the CommArea
  124. */
  125. base = base + commsize;
  126. phys = phys + commsize;
  127. /*
  128. * Place the Printf buffer area after the Fast I/O comm area.
  129. */
  130. dev->printfbuf = (void *)base;
  131. init->printfbuf = cpu_to_le32(phys);
  132. init->printfbufsiz = cpu_to_le32(printfbufsiz);
  133. memset(base, 0, printfbufsiz);
  134. return 1;
  135. }
  136. static void aac_queue_init(struct aac_dev * dev, struct aac_queue * q, u32 *mem, int qsize)
  137. {
  138. q->numpending = 0;
  139. q->dev = dev;
  140. INIT_LIST_HEAD(&q->pendingq);
  141. init_waitqueue_head(&q->cmdready);
  142. INIT_LIST_HEAD(&q->cmdq);
  143. init_waitqueue_head(&q->qfull);
  144. spin_lock_init(&q->lockdata);
  145. q->lock = &q->lockdata;
  146. q->headers.producer = (__le32 *)mem;
  147. q->headers.consumer = (__le32 *)(mem+1);
  148. *(q->headers.producer) = cpu_to_le32(qsize);
  149. *(q->headers.consumer) = cpu_to_le32(qsize);
  150. q->entries = qsize;
  151. }
  152. /**
  153. * aac_send_shutdown - shutdown an adapter
  154. * @dev: Adapter to shutdown
  155. *
  156. * This routine will send a VM_CloseAll (shutdown) request to the adapter.
  157. */
  158. int aac_send_shutdown(struct aac_dev * dev)
  159. {
  160. struct fib * fibctx;
  161. struct aac_close *cmd;
  162. int status;
  163. fibctx = fib_alloc(dev);
  164. if (!fibctx)
  165. return -ENOMEM;
  166. fib_init(fibctx);
  167. cmd = (struct aac_close *) fib_data(fibctx);
  168. cmd->command = cpu_to_le32(VM_CloseAll);
  169. cmd->cid = cpu_to_le32(0xffffffff);
  170. status = fib_send(ContainerCommand,
  171. fibctx,
  172. sizeof(struct aac_close),
  173. FsaNormal,
  174. 1, 1,
  175. NULL, NULL);
  176. if (status == 0)
  177. fib_complete(fibctx);
  178. fib_free(fibctx);
  179. return status;
  180. }
  181. /**
  182. * aac_comm_init - Initialise FSA data structures
  183. * @dev: Adapter to initialise
  184. *
  185. * Initializes the data structures that are required for the FSA commuication
  186. * interface to operate.
  187. * Returns
  188. * 1 - if we were able to init the commuication interface.
  189. * 0 - If there were errors initing. This is a fatal error.
  190. */
  191. static int aac_comm_init(struct aac_dev * dev)
  192. {
  193. unsigned long hdrsize = (sizeof(u32) * NUMBER_OF_COMM_QUEUES) * 2;
  194. unsigned long queuesize = sizeof(struct aac_entry) * TOTAL_QUEUE_ENTRIES;
  195. u32 *headers;
  196. struct aac_entry * queues;
  197. unsigned long size;
  198. struct aac_queue_block * comm = dev->queues;
  199. /*
  200. * Now allocate and initialize the zone structures used as our
  201. * pool of FIB context records. The size of the zone is based
  202. * on the system memory size. We also initialize the mutex used
  203. * to protect the zone.
  204. */
  205. spin_lock_init(&dev->fib_lock);
  206. /*
  207. * Allocate the physically contigous space for the commuication
  208. * queue headers.
  209. */
  210. size = hdrsize + queuesize;
  211. if (!aac_alloc_comm(dev, (void * *)&headers, size, QUEUE_ALIGNMENT))
  212. return -ENOMEM;
  213. queues = (struct aac_entry *)(((ulong)headers) + hdrsize);
  214. /* Adapter to Host normal priority Command queue */
  215. comm->queue[HostNormCmdQueue].base = queues;
  216. aac_queue_init(dev, &comm->queue[HostNormCmdQueue], headers, HOST_NORM_CMD_ENTRIES);
  217. queues += HOST_NORM_CMD_ENTRIES;
  218. headers += 2;
  219. /* Adapter to Host high priority command queue */
  220. comm->queue[HostHighCmdQueue].base = queues;
  221. aac_queue_init(dev, &comm->queue[HostHighCmdQueue], headers, HOST_HIGH_CMD_ENTRIES);
  222. queues += HOST_HIGH_CMD_ENTRIES;
  223. headers +=2;
  224. /* Host to adapter normal priority command queue */
  225. comm->queue[AdapNormCmdQueue].base = queues;
  226. aac_queue_init(dev, &comm->queue[AdapNormCmdQueue], headers, ADAP_NORM_CMD_ENTRIES);
  227. queues += ADAP_NORM_CMD_ENTRIES;
  228. headers += 2;
  229. /* host to adapter high priority command queue */
  230. comm->queue[AdapHighCmdQueue].base = queues;
  231. aac_queue_init(dev, &comm->queue[AdapHighCmdQueue], headers, ADAP_HIGH_CMD_ENTRIES);
  232. queues += ADAP_HIGH_CMD_ENTRIES;
  233. headers += 2;
  234. /* adapter to host normal priority response queue */
  235. comm->queue[HostNormRespQueue].base = queues;
  236. aac_queue_init(dev, &comm->queue[HostNormRespQueue], headers, HOST_NORM_RESP_ENTRIES);
  237. queues += HOST_NORM_RESP_ENTRIES;
  238. headers += 2;
  239. /* adapter to host high priority response queue */
  240. comm->queue[HostHighRespQueue].base = queues;
  241. aac_queue_init(dev, &comm->queue[HostHighRespQueue], headers, HOST_HIGH_RESP_ENTRIES);
  242. queues += HOST_HIGH_RESP_ENTRIES;
  243. headers += 2;
  244. /* host to adapter normal priority response queue */
  245. comm->queue[AdapNormRespQueue].base = queues;
  246. aac_queue_init(dev, &comm->queue[AdapNormRespQueue], headers, ADAP_NORM_RESP_ENTRIES);
  247. queues += ADAP_NORM_RESP_ENTRIES;
  248. headers += 2;
  249. /* host to adapter high priority response queue */
  250. comm->queue[AdapHighRespQueue].base = queues;
  251. aac_queue_init(dev, &comm->queue[AdapHighRespQueue], headers, ADAP_HIGH_RESP_ENTRIES);
  252. comm->queue[AdapNormCmdQueue].lock = comm->queue[HostNormRespQueue].lock;
  253. comm->queue[AdapHighCmdQueue].lock = comm->queue[HostHighRespQueue].lock;
  254. comm->queue[AdapNormRespQueue].lock = comm->queue[HostNormCmdQueue].lock;
  255. comm->queue[AdapHighRespQueue].lock = comm->queue[HostHighCmdQueue].lock;
  256. return 0;
  257. }
  258. struct aac_dev *aac_init_adapter(struct aac_dev *dev)
  259. {
  260. u32 status[5];
  261. struct Scsi_Host * host = dev->scsi_host_ptr;
  262. /*
  263. * Check the preferred comm settings, defaults from template.
  264. */
  265. dev->max_fib_size = sizeof(struct hw_fib);
  266. dev->sg_tablesize = host->sg_tablesize = (dev->max_fib_size
  267. - sizeof(struct aac_fibhdr)
  268. - sizeof(struct aac_write) + sizeof(struct sgmap))
  269. / sizeof(struct sgmap);
  270. if ((!aac_adapter_sync_cmd(dev, GET_COMM_PREFERRED_SETTINGS,
  271. 0, 0, 0, 0, 0, 0,
  272. status+0, status+1, status+2, status+3, status+4))
  273. && (status[0] == 0x00000001)) {
  274. /*
  275. * status[1] >> 16 maximum command size in KB
  276. * status[1] & 0xFFFF maximum FIB size
  277. * status[2] >> 16 maximum SG elements to driver
  278. * status[2] & 0xFFFF maximum SG elements from driver
  279. * status[3] & 0xFFFF maximum number FIBs outstanding
  280. */
  281. host->max_sectors = (status[1] >> 16) << 1;
  282. dev->max_fib_size = status[1] & 0xFFFF;
  283. host->sg_tablesize = status[2] >> 16;
  284. dev->sg_tablesize = status[2] & 0xFFFF;
  285. host->can_queue = (status[3] & 0xFFFF) - AAC_NUM_MGT_FIB;
  286. /*
  287. * NOTE:
  288. * All these overrides are based on a fixed internal
  289. * knowledge and understanding of existing adapters,
  290. * acbsize should be set with caution.
  291. */
  292. if (acbsize == 512) {
  293. host->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
  294. dev->max_fib_size = 512;
  295. dev->sg_tablesize = host->sg_tablesize
  296. = (512 - sizeof(struct aac_fibhdr)
  297. - sizeof(struct aac_write) + sizeof(struct sgmap))
  298. / sizeof(struct sgmap);
  299. host->can_queue = AAC_NUM_IO_FIB;
  300. } else if (acbsize == 2048) {
  301. host->max_sectors = 512;
  302. dev->max_fib_size = 2048;
  303. host->sg_tablesize = 65;
  304. dev->sg_tablesize = 81;
  305. host->can_queue = 512 - AAC_NUM_MGT_FIB;
  306. } else if (acbsize == 4096) {
  307. host->max_sectors = 1024;
  308. dev->max_fib_size = 4096;
  309. host->sg_tablesize = 129;
  310. dev->sg_tablesize = 166;
  311. host->can_queue = 256 - AAC_NUM_MGT_FIB;
  312. } else if (acbsize == 8192) {
  313. host->max_sectors = 2048;
  314. dev->max_fib_size = 8192;
  315. host->sg_tablesize = 257;
  316. dev->sg_tablesize = 337;
  317. host->can_queue = 128 - AAC_NUM_MGT_FIB;
  318. } else if (acbsize > 0) {
  319. printk("Illegal acbsize=%d ignored\n", acbsize);
  320. }
  321. }
  322. {
  323. if (numacb > 0) {
  324. if (numacb < host->can_queue)
  325. host->can_queue = numacb;
  326. else
  327. printk("numacb=%d ignored\n", numacb);
  328. }
  329. }
  330. /*
  331. * Ok now init the communication subsystem
  332. */
  333. dev->queues = (struct aac_queue_block *) kmalloc(sizeof(struct aac_queue_block), GFP_KERNEL);
  334. if (dev->queues == NULL) {
  335. printk(KERN_ERR "Error could not allocate comm region.\n");
  336. return NULL;
  337. }
  338. memset(dev->queues, 0, sizeof(struct aac_queue_block));
  339. if (aac_comm_init(dev)<0){
  340. kfree(dev->queues);
  341. return NULL;
  342. }
  343. /*
  344. * Initialize the list of fibs
  345. */
  346. if(fib_setup(dev)<0){
  347. kfree(dev->queues);
  348. return NULL;
  349. }
  350. INIT_LIST_HEAD(&dev->fib_list);
  351. init_completion(&dev->aif_completion);
  352. return dev;
  353. }