comminit.c 16 KB

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