comminit.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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-2010 Adaptec, Inc.
  9. * 2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; see the file COPYING. If not, write to
  23. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. * Module Name:
  26. * comminit.c
  27. *
  28. * Abstract: This supports the initialization of the host adapter commuication interface.
  29. * This is a platform dependent module for the pci cyclone board.
  30. *
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/init.h>
  34. #include <linux/types.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 "aacraid.h"
  43. struct aac_common aac_config = {
  44. .irq_mod = 1
  45. };
  46. static int aac_alloc_comm(struct aac_dev *dev, void **commaddr, unsigned long commsize, unsigned long commalign)
  47. {
  48. unsigned char *base;
  49. unsigned long size, align;
  50. const unsigned long fibsize = 4096;
  51. const unsigned long printfbufsiz = 256;
  52. unsigned long host_rrq_size = 0;
  53. struct aac_init *init;
  54. dma_addr_t phys;
  55. unsigned long aac_max_hostphysmempages;
  56. if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1)
  57. host_rrq_size = (dev->scsi_host_ptr->can_queue
  58. + AAC_NUM_MGT_FIB) * sizeof(u32);
  59. size = fibsize + sizeof(struct aac_init) + commsize +
  60. commalign + printfbufsiz + host_rrq_size;
  61. base = pci_alloc_consistent(dev->pdev, size, &phys);
  62. if(base == NULL)
  63. {
  64. printk(KERN_ERR "aacraid: unable to create mapping.\n");
  65. return 0;
  66. }
  67. dev->comm_addr = (void *)base;
  68. dev->comm_phys = phys;
  69. dev->comm_size = size;
  70. if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) {
  71. dev->host_rrq = (u32 *)(base + fibsize);
  72. dev->host_rrq_pa = phys + fibsize;
  73. memset(dev->host_rrq, 0, host_rrq_size);
  74. }
  75. dev->init = (struct aac_init *)(base + fibsize + host_rrq_size);
  76. dev->init_pa = phys + fibsize + host_rrq_size;
  77. init = dev->init;
  78. init->InitStructRevision = cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION);
  79. if (dev->max_fib_size != sizeof(struct hw_fib))
  80. init->InitStructRevision = cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_4);
  81. init->MiniPortRevision = cpu_to_le32(Sa_MINIPORT_REVISION);
  82. init->fsrev = cpu_to_le32(dev->fsrev);
  83. /*
  84. * Adapter Fibs are the first thing allocated so that they
  85. * start page aligned
  86. */
  87. dev->aif_base_va = (struct hw_fib *)base;
  88. init->AdapterFibsVirtualAddress = 0;
  89. init->AdapterFibsPhysicalAddress = cpu_to_le32((u32)phys);
  90. init->AdapterFibsSize = cpu_to_le32(fibsize);
  91. init->AdapterFibAlign = cpu_to_le32(sizeof(struct hw_fib));
  92. /*
  93. * number of 4k pages of host physical memory. The aacraid fw needs
  94. * this number to be less than 4gb worth of pages. New firmware doesn't
  95. * have any issues with the mapping system, but older Firmware did, and
  96. * had *troubles* dealing with the math overloading past 32 bits, thus
  97. * we must limit this field.
  98. */
  99. aac_max_hostphysmempages = dma_get_required_mask(&dev->pdev->dev) >> 12;
  100. if (aac_max_hostphysmempages < AAC_MAX_HOSTPHYSMEMPAGES)
  101. init->HostPhysMemPages = cpu_to_le32(aac_max_hostphysmempages);
  102. else
  103. init->HostPhysMemPages = cpu_to_le32(AAC_MAX_HOSTPHYSMEMPAGES);
  104. init->InitFlags = 0;
  105. if (dev->comm_interface == AAC_COMM_MESSAGE) {
  106. init->InitFlags |= cpu_to_le32(INITFLAGS_NEW_COMM_SUPPORTED);
  107. dprintk((KERN_WARNING"aacraid: New Comm Interface enabled\n"));
  108. } else if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) {
  109. init->InitStructRevision = cpu_to_le32(ADAPTER_INIT_STRUCT_REVISION_6);
  110. init->InitFlags |= cpu_to_le32(INITFLAGS_NEW_COMM_TYPE1_SUPPORTED);
  111. dprintk((KERN_WARNING
  112. "aacraid: New Comm Interface type1 enabled\n"));
  113. }
  114. init->InitFlags |= cpu_to_le32(INITFLAGS_DRIVER_USES_UTC_TIME |
  115. INITFLAGS_DRIVER_SUPPORTS_PM);
  116. init->MaxIoCommands = cpu_to_le32(dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
  117. init->MaxIoSize = cpu_to_le32(dev->scsi_host_ptr->max_sectors << 9);
  118. init->MaxFibSize = cpu_to_le32(dev->max_fib_size);
  119. init->MaxNumAif = cpu_to_le32(dev->max_num_aif);
  120. init->HostRRQ_AddrHigh = (u32)((u64)dev->host_rrq_pa >> 32);
  121. init->HostRRQ_AddrLow = (u32)(dev->host_rrq_pa & 0xffffffff);
  122. /*
  123. * Increment the base address by the amount already used
  124. */
  125. base = base + fibsize + host_rrq_size + sizeof(struct aac_init);
  126. phys = (dma_addr_t)((ulong)phys + fibsize + host_rrq_size +
  127. sizeof(struct aac_init));
  128. /*
  129. * Align the beginning of Headers to commalign
  130. */
  131. align = (commalign - ((uintptr_t)(base) & (commalign - 1)));
  132. base = base + align;
  133. phys = phys + align;
  134. /*
  135. * Fill in addresses of the Comm Area Headers and Queues
  136. */
  137. *commaddr = base;
  138. init->CommHeaderAddress = cpu_to_le32((u32)phys);
  139. /*
  140. * Increment the base address by the size of the CommArea
  141. */
  142. base = base + commsize;
  143. phys = phys + commsize;
  144. /*
  145. * Place the Printf buffer area after the Fast I/O comm area.
  146. */
  147. dev->printfbuf = (void *)base;
  148. init->printfbuf = cpu_to_le32(phys);
  149. init->printfbufsiz = cpu_to_le32(printfbufsiz);
  150. memset(base, 0, printfbufsiz);
  151. return 1;
  152. }
  153. static void aac_queue_init(struct aac_dev * dev, struct aac_queue * q, u32 *mem, int qsize)
  154. {
  155. q->numpending = 0;
  156. q->dev = dev;
  157. init_waitqueue_head(&q->cmdready);
  158. INIT_LIST_HEAD(&q->cmdq);
  159. init_waitqueue_head(&q->qfull);
  160. spin_lock_init(&q->lockdata);
  161. q->lock = &q->lockdata;
  162. q->headers.producer = (__le32 *)mem;
  163. q->headers.consumer = (__le32 *)(mem+1);
  164. *(q->headers.producer) = cpu_to_le32(qsize);
  165. *(q->headers.consumer) = cpu_to_le32(qsize);
  166. q->entries = qsize;
  167. }
  168. /**
  169. * aac_send_shutdown - shutdown an adapter
  170. * @dev: Adapter to shutdown
  171. *
  172. * This routine will send a VM_CloseAll (shutdown) request to the adapter.
  173. */
  174. int aac_send_shutdown(struct aac_dev * dev)
  175. {
  176. struct fib * fibctx;
  177. struct aac_close *cmd;
  178. int status;
  179. fibctx = aac_fib_alloc(dev);
  180. if (!fibctx)
  181. return -ENOMEM;
  182. aac_fib_init(fibctx);
  183. cmd = (struct aac_close *) fib_data(fibctx);
  184. cmd->command = cpu_to_le32(VM_CloseAll);
  185. cmd->cid = cpu_to_le32(0xffffffff);
  186. status = aac_fib_send(ContainerCommand,
  187. fibctx,
  188. sizeof(struct aac_close),
  189. FsaNormal,
  190. -2 /* Timeout silently */, 1,
  191. NULL, NULL);
  192. if (status >= 0)
  193. aac_fib_complete(fibctx);
  194. /* FIB should be freed only after getting the response from the F/W */
  195. if (status != -ERESTARTSYS)
  196. aac_fib_free(fibctx);
  197. return status;
  198. }
  199. /**
  200. * aac_comm_init - Initialise FSA data structures
  201. * @dev: Adapter to initialise
  202. *
  203. * Initializes the data structures that are required for the FSA commuication
  204. * interface to operate.
  205. * Returns
  206. * 1 - if we were able to init the commuication interface.
  207. * 0 - If there were errors initing. This is a fatal error.
  208. */
  209. static int aac_comm_init(struct aac_dev * dev)
  210. {
  211. unsigned long hdrsize = (sizeof(u32) * NUMBER_OF_COMM_QUEUES) * 2;
  212. unsigned long queuesize = sizeof(struct aac_entry) * TOTAL_QUEUE_ENTRIES;
  213. u32 *headers;
  214. struct aac_entry * queues;
  215. unsigned long size;
  216. struct aac_queue_block * comm = dev->queues;
  217. /*
  218. * Now allocate and initialize the zone structures used as our
  219. * pool of FIB context records. The size of the zone is based
  220. * on the system memory size. We also initialize the mutex used
  221. * to protect the zone.
  222. */
  223. spin_lock_init(&dev->fib_lock);
  224. /*
  225. * Allocate the physically contiguous space for the commuication
  226. * queue headers.
  227. */
  228. size = hdrsize + queuesize;
  229. if (!aac_alloc_comm(dev, (void * *)&headers, size, QUEUE_ALIGNMENT))
  230. return -ENOMEM;
  231. queues = (struct aac_entry *)(((ulong)headers) + hdrsize);
  232. /* Adapter to Host normal priority Command queue */
  233. comm->queue[HostNormCmdQueue].base = queues;
  234. aac_queue_init(dev, &comm->queue[HostNormCmdQueue], headers, HOST_NORM_CMD_ENTRIES);
  235. queues += HOST_NORM_CMD_ENTRIES;
  236. headers += 2;
  237. /* Adapter to Host high priority command queue */
  238. comm->queue[HostHighCmdQueue].base = queues;
  239. aac_queue_init(dev, &comm->queue[HostHighCmdQueue], headers, HOST_HIGH_CMD_ENTRIES);
  240. queues += HOST_HIGH_CMD_ENTRIES;
  241. headers +=2;
  242. /* Host to adapter normal priority command queue */
  243. comm->queue[AdapNormCmdQueue].base = queues;
  244. aac_queue_init(dev, &comm->queue[AdapNormCmdQueue], headers, ADAP_NORM_CMD_ENTRIES);
  245. queues += ADAP_NORM_CMD_ENTRIES;
  246. headers += 2;
  247. /* host to adapter high priority command queue */
  248. comm->queue[AdapHighCmdQueue].base = queues;
  249. aac_queue_init(dev, &comm->queue[AdapHighCmdQueue], headers, ADAP_HIGH_CMD_ENTRIES);
  250. queues += ADAP_HIGH_CMD_ENTRIES;
  251. headers += 2;
  252. /* adapter to host normal priority response queue */
  253. comm->queue[HostNormRespQueue].base = queues;
  254. aac_queue_init(dev, &comm->queue[HostNormRespQueue], headers, HOST_NORM_RESP_ENTRIES);
  255. queues += HOST_NORM_RESP_ENTRIES;
  256. headers += 2;
  257. /* adapter to host high priority response queue */
  258. comm->queue[HostHighRespQueue].base = queues;
  259. aac_queue_init(dev, &comm->queue[HostHighRespQueue], headers, HOST_HIGH_RESP_ENTRIES);
  260. queues += HOST_HIGH_RESP_ENTRIES;
  261. headers += 2;
  262. /* host to adapter normal priority response queue */
  263. comm->queue[AdapNormRespQueue].base = queues;
  264. aac_queue_init(dev, &comm->queue[AdapNormRespQueue], headers, ADAP_NORM_RESP_ENTRIES);
  265. queues += ADAP_NORM_RESP_ENTRIES;
  266. headers += 2;
  267. /* host to adapter high priority response queue */
  268. comm->queue[AdapHighRespQueue].base = queues;
  269. aac_queue_init(dev, &comm->queue[AdapHighRespQueue], headers, ADAP_HIGH_RESP_ENTRIES);
  270. comm->queue[AdapNormCmdQueue].lock = comm->queue[HostNormRespQueue].lock;
  271. comm->queue[AdapHighCmdQueue].lock = comm->queue[HostHighRespQueue].lock;
  272. comm->queue[AdapNormRespQueue].lock = comm->queue[HostNormCmdQueue].lock;
  273. comm->queue[AdapHighRespQueue].lock = comm->queue[HostHighCmdQueue].lock;
  274. return 0;
  275. }
  276. struct aac_dev *aac_init_adapter(struct aac_dev *dev)
  277. {
  278. u32 status[5];
  279. struct Scsi_Host * host = dev->scsi_host_ptr;
  280. /*
  281. * Check the preferred comm settings, defaults from template.
  282. */
  283. dev->management_fib_count = 0;
  284. spin_lock_init(&dev->manage_lock);
  285. dev->max_fib_size = sizeof(struct hw_fib);
  286. dev->sg_tablesize = host->sg_tablesize = (dev->max_fib_size
  287. - sizeof(struct aac_fibhdr)
  288. - sizeof(struct aac_write) + sizeof(struct sgentry))
  289. / sizeof(struct sgentry);
  290. dev->comm_interface = AAC_COMM_PRODUCER;
  291. dev->raw_io_interface = dev->raw_io_64 = 0;
  292. if ((!aac_adapter_sync_cmd(dev, GET_ADAPTER_PROPERTIES,
  293. 0, 0, 0, 0, 0, 0, status+0, status+1, status+2, NULL, NULL)) &&
  294. (status[0] == 0x00000001)) {
  295. if (status[1] & le32_to_cpu(AAC_OPT_NEW_COMM_64))
  296. dev->raw_io_64 = 1;
  297. if (dev->a_ops.adapter_comm) {
  298. if (status[1] & le32_to_cpu(AAC_OPT_NEW_COMM_TYPE1)) {
  299. dev->comm_interface = AAC_COMM_MESSAGE_TYPE1;
  300. dev->raw_io_interface = 1;
  301. } else if (status[1] & le32_to_cpu(AAC_OPT_NEW_COMM)) {
  302. dev->comm_interface = AAC_COMM_MESSAGE;
  303. dev->raw_io_interface = 1;
  304. }
  305. }
  306. if ((dev->comm_interface == AAC_COMM_MESSAGE) &&
  307. (status[2] > dev->base_size)) {
  308. aac_adapter_ioremap(dev, 0);
  309. dev->base_size = status[2];
  310. if (aac_adapter_ioremap(dev, status[2])) {
  311. /* remap failed, go back ... */
  312. dev->comm_interface = AAC_COMM_PRODUCER;
  313. if (aac_adapter_ioremap(dev, AAC_MIN_FOOTPRINT_SIZE)) {
  314. printk(KERN_WARNING
  315. "aacraid: unable to map adapter.\n");
  316. return NULL;
  317. }
  318. }
  319. }
  320. }
  321. if ((!aac_adapter_sync_cmd(dev, GET_COMM_PREFERRED_SETTINGS,
  322. 0, 0, 0, 0, 0, 0,
  323. status+0, status+1, status+2, status+3, status+4))
  324. && (status[0] == 0x00000001)) {
  325. /*
  326. * status[1] >> 16 maximum command size in KB
  327. * status[1] & 0xFFFF maximum FIB size
  328. * status[2] >> 16 maximum SG elements to driver
  329. * status[2] & 0xFFFF maximum SG elements from driver
  330. * status[3] & 0xFFFF maximum number FIBs outstanding
  331. */
  332. host->max_sectors = (status[1] >> 16) << 1;
  333. /* Multiple of 32 for PMC */
  334. dev->max_fib_size = status[1] & 0xFFE0;
  335. host->sg_tablesize = status[2] >> 16;
  336. dev->sg_tablesize = status[2] & 0xFFFF;
  337. host->can_queue = (status[3] & 0xFFFF) - AAC_NUM_MGT_FIB;
  338. dev->max_num_aif = status[4] & 0xFFFF;
  339. /*
  340. * NOTE:
  341. * All these overrides are based on a fixed internal
  342. * knowledge and understanding of existing adapters,
  343. * acbsize should be set with caution.
  344. */
  345. if (acbsize == 512) {
  346. host->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
  347. dev->max_fib_size = 512;
  348. dev->sg_tablesize = host->sg_tablesize
  349. = (512 - sizeof(struct aac_fibhdr)
  350. - sizeof(struct aac_write) + sizeof(struct sgentry))
  351. / sizeof(struct sgentry);
  352. host->can_queue = AAC_NUM_IO_FIB;
  353. } else if (acbsize == 2048) {
  354. host->max_sectors = 512;
  355. dev->max_fib_size = 2048;
  356. host->sg_tablesize = 65;
  357. dev->sg_tablesize = 81;
  358. host->can_queue = 512 - AAC_NUM_MGT_FIB;
  359. } else if (acbsize == 4096) {
  360. host->max_sectors = 1024;
  361. dev->max_fib_size = 4096;
  362. host->sg_tablesize = 129;
  363. dev->sg_tablesize = 166;
  364. host->can_queue = 256 - AAC_NUM_MGT_FIB;
  365. } else if (acbsize == 8192) {
  366. host->max_sectors = 2048;
  367. dev->max_fib_size = 8192;
  368. host->sg_tablesize = 257;
  369. dev->sg_tablesize = 337;
  370. host->can_queue = 128 - AAC_NUM_MGT_FIB;
  371. } else if (acbsize > 0) {
  372. printk("Illegal acbsize=%d ignored\n", acbsize);
  373. }
  374. }
  375. {
  376. if (numacb > 0) {
  377. if (numacb < host->can_queue)
  378. host->can_queue = numacb;
  379. else
  380. printk("numacb=%d ignored\n", numacb);
  381. }
  382. }
  383. /*
  384. * Ok now init the communication subsystem
  385. */
  386. dev->queues = kzalloc(sizeof(struct aac_queue_block), GFP_KERNEL);
  387. if (dev->queues == NULL) {
  388. printk(KERN_ERR "Error could not allocate comm region.\n");
  389. return NULL;
  390. }
  391. if (aac_comm_init(dev)<0){
  392. kfree(dev->queues);
  393. return NULL;
  394. }
  395. /*
  396. * Initialize the list of fibs
  397. */
  398. if (aac_fib_setup(dev) < 0) {
  399. kfree(dev->queues);
  400. return NULL;
  401. }
  402. INIT_LIST_HEAD(&dev->fib_list);
  403. return dev;
  404. }