comminit.c 13 KB

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