comminit.c 13 KB

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