commsup.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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-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. * commsup.c
  26. *
  27. * Abstract: Contain all routines that are required for FSA host/adapter
  28. * communication.
  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/completion.h>
  39. #include <linux/blkdev.h>
  40. #include <linux/delay.h>
  41. #include <linux/kthread.h>
  42. #include <linux/interrupt.h>
  43. #include <scsi/scsi.h>
  44. #include <scsi/scsi_host.h>
  45. #include <scsi/scsi_device.h>
  46. #include <scsi/scsi_cmnd.h>
  47. #include <asm/semaphore.h>
  48. #include "aacraid.h"
  49. /**
  50. * fib_map_alloc - allocate the fib objects
  51. * @dev: Adapter to allocate for
  52. *
  53. * Allocate and map the shared PCI space for the FIB blocks used to
  54. * talk to the Adaptec firmware.
  55. */
  56. static int fib_map_alloc(struct aac_dev *dev)
  57. {
  58. dprintk((KERN_INFO
  59. "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
  60. dev->pdev, dev->max_fib_size, dev->scsi_host_ptr->can_queue,
  61. AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
  62. if((dev->hw_fib_va = pci_alloc_consistent(dev->pdev, dev->max_fib_size
  63. * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB),
  64. &dev->hw_fib_pa))==NULL)
  65. return -ENOMEM;
  66. return 0;
  67. }
  68. /**
  69. * aac_fib_map_free - free the fib objects
  70. * @dev: Adapter to free
  71. *
  72. * Free the PCI mappings and the memory allocated for FIB blocks
  73. * on this adapter.
  74. */
  75. void aac_fib_map_free(struct aac_dev *dev)
  76. {
  77. pci_free_consistent(dev->pdev,
  78. dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB),
  79. dev->hw_fib_va, dev->hw_fib_pa);
  80. dev->hw_fib_va = NULL;
  81. dev->hw_fib_pa = 0;
  82. }
  83. /**
  84. * aac_fib_setup - setup the fibs
  85. * @dev: Adapter to set up
  86. *
  87. * Allocate the PCI space for the fibs, map it and then intialise the
  88. * fib area, the unmapped fib data and also the free list
  89. */
  90. int aac_fib_setup(struct aac_dev * dev)
  91. {
  92. struct fib *fibptr;
  93. struct hw_fib *hw_fib;
  94. dma_addr_t hw_fib_pa;
  95. int i;
  96. while (((i = fib_map_alloc(dev)) == -ENOMEM)
  97. && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
  98. dev->init->MaxIoCommands = cpu_to_le32((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) >> 1);
  99. dev->scsi_host_ptr->can_queue = le32_to_cpu(dev->init->MaxIoCommands) - AAC_NUM_MGT_FIB;
  100. }
  101. if (i<0)
  102. return -ENOMEM;
  103. hw_fib = dev->hw_fib_va;
  104. hw_fib_pa = dev->hw_fib_pa;
  105. memset(hw_fib, 0, dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
  106. /*
  107. * Initialise the fibs
  108. */
  109. for (i = 0, fibptr = &dev->fibs[i]; i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); i++, fibptr++)
  110. {
  111. fibptr->dev = dev;
  112. fibptr->hw_fib_va = hw_fib;
  113. fibptr->data = (void *) fibptr->hw_fib_va->data;
  114. fibptr->next = fibptr+1; /* Forward chain the fibs */
  115. init_MUTEX_LOCKED(&fibptr->event_wait);
  116. spin_lock_init(&fibptr->event_lock);
  117. hw_fib->header.XferState = cpu_to_le32(0xffffffff);
  118. hw_fib->header.SenderSize = cpu_to_le16(dev->max_fib_size);
  119. fibptr->hw_fib_pa = hw_fib_pa;
  120. hw_fib = (struct hw_fib *)((unsigned char *)hw_fib + dev->max_fib_size);
  121. hw_fib_pa = hw_fib_pa + dev->max_fib_size;
  122. }
  123. /*
  124. * Add the fib chain to the free list
  125. */
  126. dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
  127. /*
  128. * Enable this to debug out of queue space
  129. */
  130. dev->free_fib = &dev->fibs[0];
  131. return 0;
  132. }
  133. /**
  134. * aac_fib_alloc - allocate a fib
  135. * @dev: Adapter to allocate the fib for
  136. *
  137. * Allocate a fib from the adapter fib pool. If the pool is empty we
  138. * return NULL.
  139. */
  140. struct fib *aac_fib_alloc(struct aac_dev *dev)
  141. {
  142. struct fib * fibptr;
  143. unsigned long flags;
  144. spin_lock_irqsave(&dev->fib_lock, flags);
  145. fibptr = dev->free_fib;
  146. if(!fibptr){
  147. spin_unlock_irqrestore(&dev->fib_lock, flags);
  148. return fibptr;
  149. }
  150. dev->free_fib = fibptr->next;
  151. spin_unlock_irqrestore(&dev->fib_lock, flags);
  152. /*
  153. * Set the proper node type code and node byte size
  154. */
  155. fibptr->type = FSAFS_NTC_FIB_CONTEXT;
  156. fibptr->size = sizeof(struct fib);
  157. /*
  158. * Null out fields that depend on being zero at the start of
  159. * each I/O
  160. */
  161. fibptr->hw_fib_va->header.XferState = 0;
  162. fibptr->callback = NULL;
  163. fibptr->callback_data = NULL;
  164. return fibptr;
  165. }
  166. /**
  167. * aac_fib_free - free a fib
  168. * @fibptr: fib to free up
  169. *
  170. * Frees up a fib and places it on the appropriate queue
  171. */
  172. void aac_fib_free(struct fib *fibptr)
  173. {
  174. unsigned long flags;
  175. spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
  176. if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
  177. aac_config.fib_timeouts++;
  178. if (fibptr->hw_fib_va->header.XferState != 0) {
  179. printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
  180. (void*)fibptr,
  181. le32_to_cpu(fibptr->hw_fib_va->header.XferState));
  182. }
  183. fibptr->next = fibptr->dev->free_fib;
  184. fibptr->dev->free_fib = fibptr;
  185. spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
  186. }
  187. /**
  188. * aac_fib_init - initialise a fib
  189. * @fibptr: The fib to initialize
  190. *
  191. * Set up the generic fib fields ready for use
  192. */
  193. void aac_fib_init(struct fib *fibptr)
  194. {
  195. struct hw_fib *hw_fib = fibptr->hw_fib_va;
  196. hw_fib->header.StructType = FIB_MAGIC;
  197. hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
  198. hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
  199. hw_fib->header.SenderFibAddress = 0; /* Filled in later if needed */
  200. hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
  201. hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
  202. }
  203. /**
  204. * fib_deallocate - deallocate a fib
  205. * @fibptr: fib to deallocate
  206. *
  207. * Will deallocate and return to the free pool the FIB pointed to by the
  208. * caller.
  209. */
  210. static void fib_dealloc(struct fib * fibptr)
  211. {
  212. struct hw_fib *hw_fib = fibptr->hw_fib_va;
  213. BUG_ON(hw_fib->header.StructType != FIB_MAGIC);
  214. hw_fib->header.XferState = 0;
  215. }
  216. /*
  217. * Commuication primitives define and support the queuing method we use to
  218. * support host to adapter commuication. All queue accesses happen through
  219. * these routines and are the only routines which have a knowledge of the
  220. * how these queues are implemented.
  221. */
  222. /**
  223. * aac_get_entry - get a queue entry
  224. * @dev: Adapter
  225. * @qid: Queue Number
  226. * @entry: Entry return
  227. * @index: Index return
  228. * @nonotify: notification control
  229. *
  230. * With a priority the routine returns a queue entry if the queue has free entries. If the queue
  231. * is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
  232. * returned.
  233. */
  234. static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
  235. {
  236. struct aac_queue * q;
  237. unsigned long idx;
  238. /*
  239. * All of the queues wrap when they reach the end, so we check
  240. * to see if they have reached the end and if they have we just
  241. * set the index back to zero. This is a wrap. You could or off
  242. * the high bits in all updates but this is a bit faster I think.
  243. */
  244. q = &dev->queues->queue[qid];
  245. idx = *index = le32_to_cpu(*(q->headers.producer));
  246. /* Interrupt Moderation, only interrupt for first two entries */
  247. if (idx != le32_to_cpu(*(q->headers.consumer))) {
  248. if (--idx == 0) {
  249. if (qid == AdapNormCmdQueue)
  250. idx = ADAP_NORM_CMD_ENTRIES;
  251. else
  252. idx = ADAP_NORM_RESP_ENTRIES;
  253. }
  254. if (idx != le32_to_cpu(*(q->headers.consumer)))
  255. *nonotify = 1;
  256. }
  257. if (qid == AdapNormCmdQueue) {
  258. if (*index >= ADAP_NORM_CMD_ENTRIES)
  259. *index = 0; /* Wrap to front of the Producer Queue. */
  260. } else {
  261. if (*index >= ADAP_NORM_RESP_ENTRIES)
  262. *index = 0; /* Wrap to front of the Producer Queue. */
  263. }
  264. if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) { /* Queue is full */
  265. printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
  266. qid, q->numpending);
  267. return 0;
  268. } else {
  269. *entry = q->base + *index;
  270. return 1;
  271. }
  272. }
  273. /**
  274. * aac_queue_get - get the next free QE
  275. * @dev: Adapter
  276. * @index: Returned index
  277. * @priority: Priority of fib
  278. * @fib: Fib to associate with the queue entry
  279. * @wait: Wait if queue full
  280. * @fibptr: Driver fib object to go with fib
  281. * @nonotify: Don't notify the adapter
  282. *
  283. * Gets the next free QE off the requested priorty adapter command
  284. * queue and associates the Fib with the QE. The QE represented by
  285. * index is ready to insert on the queue when this routine returns
  286. * success.
  287. */
  288. int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify)
  289. {
  290. struct aac_entry * entry = NULL;
  291. int map = 0;
  292. if (qid == AdapNormCmdQueue) {
  293. /* if no entries wait for some if caller wants to */
  294. while (!aac_get_entry(dev, qid, &entry, index, nonotify))
  295. {
  296. printk(KERN_ERR "GetEntries failed\n");
  297. }
  298. /*
  299. * Setup queue entry with a command, status and fib mapped
  300. */
  301. entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
  302. map = 1;
  303. } else {
  304. while(!aac_get_entry(dev, qid, &entry, index, nonotify))
  305. {
  306. /* if no entries wait for some if caller wants to */
  307. }
  308. /*
  309. * Setup queue entry with command, status and fib mapped
  310. */
  311. entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
  312. entry->addr = hw_fib->header.SenderFibAddress;
  313. /* Restore adapters pointer to the FIB */
  314. hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */
  315. map = 0;
  316. }
  317. /*
  318. * If MapFib is true than we need to map the Fib and put pointers
  319. * in the queue entry.
  320. */
  321. if (map)
  322. entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
  323. return 0;
  324. }
  325. /*
  326. * Define the highest level of host to adapter communication routines.
  327. * These routines will support host to adapter FS commuication. These
  328. * routines have no knowledge of the commuication method used. This level
  329. * sends and receives FIBs. This level has no knowledge of how these FIBs
  330. * get passed back and forth.
  331. */
  332. /**
  333. * aac_fib_send - send a fib to the adapter
  334. * @command: Command to send
  335. * @fibptr: The fib
  336. * @size: Size of fib data area
  337. * @priority: Priority of Fib
  338. * @wait: Async/sync select
  339. * @reply: True if a reply is wanted
  340. * @callback: Called with reply
  341. * @callback_data: Passed to callback
  342. *
  343. * Sends the requested FIB to the adapter and optionally will wait for a
  344. * response FIB. If the caller does not wish to wait for a response than
  345. * an event to wait on must be supplied. This event will be set when a
  346. * response FIB is received from the adapter.
  347. */
  348. int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
  349. int priority, int wait, int reply, fib_callback callback,
  350. void *callback_data)
  351. {
  352. struct aac_dev * dev = fibptr->dev;
  353. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  354. unsigned long flags = 0;
  355. unsigned long qflags;
  356. if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
  357. return -EBUSY;
  358. /*
  359. * There are 5 cases with the wait and reponse requested flags.
  360. * The only invalid cases are if the caller requests to wait and
  361. * does not request a response and if the caller does not want a
  362. * response and the Fib is not allocated from pool. If a response
  363. * is not requesed the Fib will just be deallocaed by the DPC
  364. * routine when the response comes back from the adapter. No
  365. * further processing will be done besides deleting the Fib. We
  366. * will have a debug mode where the adapter can notify the host
  367. * it had a problem and the host can log that fact.
  368. */
  369. if (wait && !reply) {
  370. return -EINVAL;
  371. } else if (!wait && reply) {
  372. hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
  373. FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
  374. } else if (!wait && !reply) {
  375. hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
  376. FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
  377. } else if (wait && reply) {
  378. hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
  379. FIB_COUNTER_INCREMENT(aac_config.NormalSent);
  380. }
  381. /*
  382. * Map the fib into 32bits by using the fib number
  383. */
  384. hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
  385. hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
  386. /*
  387. * Set FIB state to indicate where it came from and if we want a
  388. * response from the adapter. Also load the command from the
  389. * caller.
  390. *
  391. * Map the hw fib pointer as a 32bit value
  392. */
  393. hw_fib->header.Command = cpu_to_le16(command);
  394. hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
  395. fibptr->hw_fib_va->header.Flags = 0; /* 0 the flags field - internal only*/
  396. /*
  397. * Set the size of the Fib we want to send to the adapter
  398. */
  399. hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
  400. if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
  401. return -EMSGSIZE;
  402. }
  403. /*
  404. * Get a queue entry connect the FIB to it and send an notify
  405. * the adapter a command is ready.
  406. */
  407. hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
  408. /*
  409. * Fill in the Callback and CallbackContext if we are not
  410. * going to wait.
  411. */
  412. if (!wait) {
  413. fibptr->callback = callback;
  414. fibptr->callback_data = callback_data;
  415. }
  416. fibptr->done = 0;
  417. fibptr->flags = 0;
  418. FIB_COUNTER_INCREMENT(aac_config.FibsSent);
  419. dprintk((KERN_DEBUG "Fib contents:.\n"));
  420. dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command)));
  421. dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
  422. dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState)));
  423. dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va));
  424. dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
  425. dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
  426. if (!dev->queues)
  427. return -EBUSY;
  428. if(wait)
  429. spin_lock_irqsave(&fibptr->event_lock, flags);
  430. aac_adapter_deliver(fibptr);
  431. /*
  432. * If the caller wanted us to wait for response wait now.
  433. */
  434. if (wait) {
  435. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  436. /* Only set for first known interruptable command */
  437. if (wait < 0) {
  438. /*
  439. * *VERY* Dangerous to time out a command, the
  440. * assumption is made that we have no hope of
  441. * functioning because an interrupt routing or other
  442. * hardware failure has occurred.
  443. */
  444. unsigned long count = 36000000L; /* 3 minutes */
  445. while (down_trylock(&fibptr->event_wait)) {
  446. int blink;
  447. if (--count == 0) {
  448. struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
  449. spin_lock_irqsave(q->lock, qflags);
  450. q->numpending--;
  451. spin_unlock_irqrestore(q->lock, qflags);
  452. if (wait == -1) {
  453. printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
  454. "Usually a result of a PCI interrupt routing problem;\n"
  455. "update mother board BIOS or consider utilizing one of\n"
  456. "the SAFE mode kernel options (acpi, apic etc)\n");
  457. }
  458. return -ETIMEDOUT;
  459. }
  460. if ((blink = aac_adapter_check_health(dev)) > 0) {
  461. if (wait == -1) {
  462. printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
  463. "Usually a result of a serious unrecoverable hardware problem\n",
  464. blink);
  465. }
  466. return -EFAULT;
  467. }
  468. udelay(5);
  469. }
  470. } else
  471. (void)down_interruptible(&fibptr->event_wait);
  472. spin_lock_irqsave(&fibptr->event_lock, flags);
  473. if (fibptr->done == 0) {
  474. fibptr->done = 2; /* Tell interrupt we aborted */
  475. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  476. return -EINTR;
  477. }
  478. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  479. BUG_ON(fibptr->done == 0);
  480. if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
  481. return -ETIMEDOUT;
  482. return 0;
  483. }
  484. /*
  485. * If the user does not want a response than return success otherwise
  486. * return pending
  487. */
  488. if (reply)
  489. return -EINPROGRESS;
  490. else
  491. return 0;
  492. }
  493. /**
  494. * aac_consumer_get - get the top of the queue
  495. * @dev: Adapter
  496. * @q: Queue
  497. * @entry: Return entry
  498. *
  499. * Will return a pointer to the entry on the top of the queue requested that
  500. * we are a consumer of, and return the address of the queue entry. It does
  501. * not change the state of the queue.
  502. */
  503. int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
  504. {
  505. u32 index;
  506. int status;
  507. if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
  508. status = 0;
  509. } else {
  510. /*
  511. * The consumer index must be wrapped if we have reached
  512. * the end of the queue, else we just use the entry
  513. * pointed to by the header index
  514. */
  515. if (le32_to_cpu(*q->headers.consumer) >= q->entries)
  516. index = 0;
  517. else
  518. index = le32_to_cpu(*q->headers.consumer);
  519. *entry = q->base + index;
  520. status = 1;
  521. }
  522. return(status);
  523. }
  524. /**
  525. * aac_consumer_free - free consumer entry
  526. * @dev: Adapter
  527. * @q: Queue
  528. * @qid: Queue ident
  529. *
  530. * Frees up the current top of the queue we are a consumer of. If the
  531. * queue was full notify the producer that the queue is no longer full.
  532. */
  533. void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
  534. {
  535. int wasfull = 0;
  536. u32 notify;
  537. if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
  538. wasfull = 1;
  539. if (le32_to_cpu(*q->headers.consumer) >= q->entries)
  540. *q->headers.consumer = cpu_to_le32(1);
  541. else
  542. *q->headers.consumer = cpu_to_le32(le32_to_cpu(*q->headers.consumer)+1);
  543. if (wasfull) {
  544. switch (qid) {
  545. case HostNormCmdQueue:
  546. notify = HostNormCmdNotFull;
  547. break;
  548. case HostNormRespQueue:
  549. notify = HostNormRespNotFull;
  550. break;
  551. default:
  552. BUG();
  553. return;
  554. }
  555. aac_adapter_notify(dev, notify);
  556. }
  557. }
  558. /**
  559. * aac_fib_adapter_complete - complete adapter issued fib
  560. * @fibptr: fib to complete
  561. * @size: size of fib
  562. *
  563. * Will do all necessary work to complete a FIB that was sent from
  564. * the adapter.
  565. */
  566. int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
  567. {
  568. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  569. struct aac_dev * dev = fibptr->dev;
  570. struct aac_queue * q;
  571. unsigned long nointr = 0;
  572. unsigned long qflags;
  573. if (hw_fib->header.XferState == 0) {
  574. if (dev->comm_interface == AAC_COMM_MESSAGE)
  575. kfree (hw_fib);
  576. return 0;
  577. }
  578. /*
  579. * If we plan to do anything check the structure type first.
  580. */
  581. if ( hw_fib->header.StructType != FIB_MAGIC ) {
  582. if (dev->comm_interface == AAC_COMM_MESSAGE)
  583. kfree (hw_fib);
  584. return -EINVAL;
  585. }
  586. /*
  587. * This block handles the case where the adapter had sent us a
  588. * command and we have finished processing the command. We
  589. * call completeFib when we are done processing the command
  590. * and want to send a response back to the adapter. This will
  591. * send the completed cdb to the adapter.
  592. */
  593. if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
  594. if (dev->comm_interface == AAC_COMM_MESSAGE) {
  595. kfree (hw_fib);
  596. } else {
  597. u32 index;
  598. hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
  599. if (size) {
  600. size += sizeof(struct aac_fibhdr);
  601. if (size > le16_to_cpu(hw_fib->header.SenderSize))
  602. return -EMSGSIZE;
  603. hw_fib->header.Size = cpu_to_le16(size);
  604. }
  605. q = &dev->queues->queue[AdapNormRespQueue];
  606. spin_lock_irqsave(q->lock, qflags);
  607. aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
  608. *(q->headers.producer) = cpu_to_le32(index + 1);
  609. spin_unlock_irqrestore(q->lock, qflags);
  610. if (!(nointr & (int)aac_config.irq_mod))
  611. aac_adapter_notify(dev, AdapNormRespQueue);
  612. }
  613. }
  614. else
  615. {
  616. printk(KERN_WARNING "aac_fib_adapter_complete: Unknown xferstate detected.\n");
  617. BUG();
  618. }
  619. return 0;
  620. }
  621. /**
  622. * aac_fib_complete - fib completion handler
  623. * @fib: FIB to complete
  624. *
  625. * Will do all necessary work to complete a FIB.
  626. */
  627. int aac_fib_complete(struct fib *fibptr)
  628. {
  629. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  630. /*
  631. * Check for a fib which has already been completed
  632. */
  633. if (hw_fib->header.XferState == 0)
  634. return 0;
  635. /*
  636. * If we plan to do anything check the structure type first.
  637. */
  638. if (hw_fib->header.StructType != FIB_MAGIC)
  639. return -EINVAL;
  640. /*
  641. * This block completes a cdb which orginated on the host and we
  642. * just need to deallocate the cdb or reinit it. At this point the
  643. * command is complete that we had sent to the adapter and this
  644. * cdb could be reused.
  645. */
  646. if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
  647. (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
  648. {
  649. fib_dealloc(fibptr);
  650. }
  651. else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
  652. {
  653. /*
  654. * This handles the case when the host has aborted the I/O
  655. * to the adapter because the adapter is not responding
  656. */
  657. fib_dealloc(fibptr);
  658. } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
  659. fib_dealloc(fibptr);
  660. } else {
  661. BUG();
  662. }
  663. return 0;
  664. }
  665. /**
  666. * aac_printf - handle printf from firmware
  667. * @dev: Adapter
  668. * @val: Message info
  669. *
  670. * Print a message passed to us by the controller firmware on the
  671. * Adaptec board
  672. */
  673. void aac_printf(struct aac_dev *dev, u32 val)
  674. {
  675. char *cp = dev->printfbuf;
  676. if (dev->printf_enabled)
  677. {
  678. int length = val & 0xffff;
  679. int level = (val >> 16) & 0xffff;
  680. /*
  681. * The size of the printfbuf is set in port.c
  682. * There is no variable or define for it
  683. */
  684. if (length > 255)
  685. length = 255;
  686. if (cp[length] != 0)
  687. cp[length] = 0;
  688. if (level == LOG_AAC_HIGH_ERROR)
  689. printk(KERN_WARNING "%s:%s", dev->name, cp);
  690. else
  691. printk(KERN_INFO "%s:%s", dev->name, cp);
  692. }
  693. memset(cp, 0, 256);
  694. }
  695. /**
  696. * aac_handle_aif - Handle a message from the firmware
  697. * @dev: Which adapter this fib is from
  698. * @fibptr: Pointer to fibptr from adapter
  699. *
  700. * This routine handles a driver notify fib from the adapter and
  701. * dispatches it to the appropriate routine for handling.
  702. */
  703. #define AIF_SNIFF_TIMEOUT (30*HZ)
  704. static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
  705. {
  706. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  707. struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
  708. u32 container;
  709. struct scsi_device *device;
  710. enum {
  711. NOTHING,
  712. DELETE,
  713. ADD,
  714. CHANGE
  715. } device_config_needed;
  716. /* Sniff for container changes */
  717. if (!dev || !dev->fsa_dev)
  718. return;
  719. container = (u32)-1;
  720. /*
  721. * We have set this up to try and minimize the number of
  722. * re-configures that take place. As a result of this when
  723. * certain AIF's come in we will set a flag waiting for another
  724. * type of AIF before setting the re-config flag.
  725. */
  726. switch (le32_to_cpu(aifcmd->command)) {
  727. case AifCmdDriverNotify:
  728. switch (le32_to_cpu(((u32 *)aifcmd->data)[0])) {
  729. /*
  730. * Morph or Expand complete
  731. */
  732. case AifDenMorphComplete:
  733. case AifDenVolumeExtendComplete:
  734. container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
  735. if (container >= dev->maximum_num_containers)
  736. break;
  737. /*
  738. * Find the scsi_device associated with the SCSI
  739. * address. Make sure we have the right array, and if
  740. * so set the flag to initiate a new re-config once we
  741. * see an AifEnConfigChange AIF come through.
  742. */
  743. if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
  744. device = scsi_device_lookup(dev->scsi_host_ptr,
  745. CONTAINER_TO_CHANNEL(container),
  746. CONTAINER_TO_ID(container),
  747. CONTAINER_TO_LUN(container));
  748. if (device) {
  749. dev->fsa_dev[container].config_needed = CHANGE;
  750. dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
  751. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  752. scsi_device_put(device);
  753. }
  754. }
  755. }
  756. /*
  757. * If we are waiting on something and this happens to be
  758. * that thing then set the re-configure flag.
  759. */
  760. if (container != (u32)-1) {
  761. if (container >= dev->maximum_num_containers)
  762. break;
  763. if ((dev->fsa_dev[container].config_waiting_on ==
  764. le32_to_cpu(*(u32 *)aifcmd->data)) &&
  765. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  766. dev->fsa_dev[container].config_waiting_on = 0;
  767. } else for (container = 0;
  768. container < dev->maximum_num_containers; ++container) {
  769. if ((dev->fsa_dev[container].config_waiting_on ==
  770. le32_to_cpu(*(u32 *)aifcmd->data)) &&
  771. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  772. dev->fsa_dev[container].config_waiting_on = 0;
  773. }
  774. break;
  775. case AifCmdEventNotify:
  776. switch (le32_to_cpu(((u32 *)aifcmd->data)[0])) {
  777. /*
  778. * Add an Array.
  779. */
  780. case AifEnAddContainer:
  781. container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
  782. if (container >= dev->maximum_num_containers)
  783. break;
  784. dev->fsa_dev[container].config_needed = ADD;
  785. dev->fsa_dev[container].config_waiting_on =
  786. AifEnConfigChange;
  787. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  788. break;
  789. /*
  790. * Delete an Array.
  791. */
  792. case AifEnDeleteContainer:
  793. container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
  794. if (container >= dev->maximum_num_containers)
  795. break;
  796. dev->fsa_dev[container].config_needed = DELETE;
  797. dev->fsa_dev[container].config_waiting_on =
  798. AifEnConfigChange;
  799. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  800. break;
  801. /*
  802. * Container change detected. If we currently are not
  803. * waiting on something else, setup to wait on a Config Change.
  804. */
  805. case AifEnContainerChange:
  806. container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
  807. if (container >= dev->maximum_num_containers)
  808. break;
  809. if (dev->fsa_dev[container].config_waiting_on &&
  810. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  811. break;
  812. dev->fsa_dev[container].config_needed = CHANGE;
  813. dev->fsa_dev[container].config_waiting_on =
  814. AifEnConfigChange;
  815. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  816. break;
  817. case AifEnConfigChange:
  818. break;
  819. }
  820. /*
  821. * If we are waiting on something and this happens to be
  822. * that thing then set the re-configure flag.
  823. */
  824. if (container != (u32)-1) {
  825. if (container >= dev->maximum_num_containers)
  826. break;
  827. if ((dev->fsa_dev[container].config_waiting_on ==
  828. le32_to_cpu(*(u32 *)aifcmd->data)) &&
  829. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  830. dev->fsa_dev[container].config_waiting_on = 0;
  831. } else for (container = 0;
  832. container < dev->maximum_num_containers; ++container) {
  833. if ((dev->fsa_dev[container].config_waiting_on ==
  834. le32_to_cpu(*(u32 *)aifcmd->data)) &&
  835. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  836. dev->fsa_dev[container].config_waiting_on = 0;
  837. }
  838. break;
  839. case AifCmdJobProgress:
  840. /*
  841. * These are job progress AIF's. When a Clear is being
  842. * done on a container it is initially created then hidden from
  843. * the OS. When the clear completes we don't get a config
  844. * change so we monitor the job status complete on a clear then
  845. * wait for a container change.
  846. */
  847. if ((((u32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero))
  848. && ((((u32 *)aifcmd->data)[6] == ((u32 *)aifcmd->data)[5])
  849. || (((u32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess)))) {
  850. for (container = 0;
  851. container < dev->maximum_num_containers;
  852. ++container) {
  853. /*
  854. * Stomp on all config sequencing for all
  855. * containers?
  856. */
  857. dev->fsa_dev[container].config_waiting_on =
  858. AifEnContainerChange;
  859. dev->fsa_dev[container].config_needed = ADD;
  860. dev->fsa_dev[container].config_waiting_stamp =
  861. jiffies;
  862. }
  863. }
  864. if ((((u32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero))
  865. && (((u32 *)aifcmd->data)[6] == 0)
  866. && (((u32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning))) {
  867. for (container = 0;
  868. container < dev->maximum_num_containers;
  869. ++container) {
  870. /*
  871. * Stomp on all config sequencing for all
  872. * containers?
  873. */
  874. dev->fsa_dev[container].config_waiting_on =
  875. AifEnContainerChange;
  876. dev->fsa_dev[container].config_needed = DELETE;
  877. dev->fsa_dev[container].config_waiting_stamp =
  878. jiffies;
  879. }
  880. }
  881. break;
  882. }
  883. device_config_needed = NOTHING;
  884. for (container = 0; container < dev->maximum_num_containers;
  885. ++container) {
  886. if ((dev->fsa_dev[container].config_waiting_on == 0) &&
  887. (dev->fsa_dev[container].config_needed != NOTHING) &&
  888. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
  889. device_config_needed =
  890. dev->fsa_dev[container].config_needed;
  891. dev->fsa_dev[container].config_needed = NOTHING;
  892. break;
  893. }
  894. }
  895. if (device_config_needed == NOTHING)
  896. return;
  897. /*
  898. * If we decided that a re-configuration needs to be done,
  899. * schedule it here on the way out the door, please close the door
  900. * behind you.
  901. */
  902. /*
  903. * Find the scsi_device associated with the SCSI address,
  904. * and mark it as changed, invalidating the cache. This deals
  905. * with changes to existing device IDs.
  906. */
  907. if (!dev || !dev->scsi_host_ptr)
  908. return;
  909. /*
  910. * force reload of disk info via aac_probe_container
  911. */
  912. if ((device_config_needed == CHANGE)
  913. && (dev->fsa_dev[container].valid == 1))
  914. dev->fsa_dev[container].valid = 2;
  915. if ((device_config_needed == CHANGE) ||
  916. (device_config_needed == ADD))
  917. aac_probe_container(dev, container);
  918. device = scsi_device_lookup(dev->scsi_host_ptr,
  919. CONTAINER_TO_CHANNEL(container),
  920. CONTAINER_TO_ID(container),
  921. CONTAINER_TO_LUN(container));
  922. if (device) {
  923. switch (device_config_needed) {
  924. case DELETE:
  925. case CHANGE:
  926. scsi_rescan_device(&device->sdev_gendev);
  927. default:
  928. break;
  929. }
  930. scsi_device_put(device);
  931. }
  932. if (device_config_needed == ADD) {
  933. scsi_add_device(dev->scsi_host_ptr,
  934. CONTAINER_TO_CHANNEL(container),
  935. CONTAINER_TO_ID(container),
  936. CONTAINER_TO_LUN(container));
  937. }
  938. }
  939. static int _aac_reset_adapter(struct aac_dev *aac, int forced)
  940. {
  941. int index, quirks;
  942. int retval;
  943. struct Scsi_Host *host;
  944. struct scsi_device *dev;
  945. struct scsi_cmnd *command;
  946. struct scsi_cmnd *command_list;
  947. int jafo = 0;
  948. /*
  949. * Assumptions:
  950. * - host is locked, unless called by the aacraid thread.
  951. * (a matter of convenience, due to legacy issues surrounding
  952. * eh_host_adapter_reset).
  953. * - in_reset is asserted, so no new i/o is getting to the
  954. * card.
  955. * - The card is dead, or will be very shortly ;-/ so no new
  956. * commands are completing in the interrupt service.
  957. */
  958. host = aac->scsi_host_ptr;
  959. scsi_block_requests(host);
  960. aac_adapter_disable_int(aac);
  961. if (aac->thread->pid != current->pid) {
  962. spin_unlock_irq(host->host_lock);
  963. kthread_stop(aac->thread);
  964. jafo = 1;
  965. }
  966. /*
  967. * If a positive health, means in a known DEAD PANIC
  968. * state and the adapter could be reset to `try again'.
  969. */
  970. retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
  971. if (retval)
  972. goto out;
  973. /*
  974. * Loop through the fibs, close the synchronous FIBS
  975. */
  976. for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
  977. struct fib *fib = &aac->fibs[index];
  978. if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
  979. (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
  980. unsigned long flagv;
  981. spin_lock_irqsave(&fib->event_lock, flagv);
  982. up(&fib->event_wait);
  983. spin_unlock_irqrestore(&fib->event_lock, flagv);
  984. schedule();
  985. retval = 0;
  986. }
  987. }
  988. /* Give some extra time for ioctls to complete. */
  989. if (retval == 0)
  990. ssleep(2);
  991. index = aac->cardtype;
  992. /*
  993. * Re-initialize the adapter, first free resources, then carefully
  994. * apply the initialization sequence to come back again. Only risk
  995. * is a change in Firmware dropping cache, it is assumed the caller
  996. * will ensure that i/o is queisced and the card is flushed in that
  997. * case.
  998. */
  999. aac_fib_map_free(aac);
  1000. pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
  1001. aac->comm_addr = NULL;
  1002. aac->comm_phys = 0;
  1003. kfree(aac->queues);
  1004. aac->queues = NULL;
  1005. free_irq(aac->pdev->irq, aac);
  1006. kfree(aac->fsa_dev);
  1007. aac->fsa_dev = NULL;
  1008. if (aac_get_driver_ident(index)->quirks & AAC_QUIRK_31BIT) {
  1009. if (((retval = pci_set_dma_mask(aac->pdev, DMA_31BIT_MASK))) ||
  1010. ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_31BIT_MASK))))
  1011. goto out;
  1012. } else {
  1013. if (((retval = pci_set_dma_mask(aac->pdev, DMA_32BIT_MASK))) ||
  1014. ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_32BIT_MASK))))
  1015. goto out;
  1016. }
  1017. if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
  1018. goto out;
  1019. if (aac_get_driver_ident(index)->quirks & AAC_QUIRK_31BIT)
  1020. if ((retval = pci_set_dma_mask(aac->pdev, DMA_32BIT_MASK)))
  1021. goto out;
  1022. if (jafo) {
  1023. aac->thread = kthread_run(aac_command_thread, aac, aac->name);
  1024. if (IS_ERR(aac->thread)) {
  1025. retval = PTR_ERR(aac->thread);
  1026. goto out;
  1027. }
  1028. }
  1029. (void)aac_get_adapter_info(aac);
  1030. quirks = aac_get_driver_ident(index)->quirks;
  1031. if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
  1032. host->sg_tablesize = 34;
  1033. host->max_sectors = (host->sg_tablesize * 8) + 112;
  1034. }
  1035. if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
  1036. host->sg_tablesize = 17;
  1037. host->max_sectors = (host->sg_tablesize * 8) + 112;
  1038. }
  1039. aac_get_config_status(aac, 1);
  1040. aac_get_containers(aac);
  1041. /*
  1042. * This is where the assumption that the Adapter is quiesced
  1043. * is important.
  1044. */
  1045. command_list = NULL;
  1046. __shost_for_each_device(dev, host) {
  1047. unsigned long flags;
  1048. spin_lock_irqsave(&dev->list_lock, flags);
  1049. list_for_each_entry(command, &dev->cmd_list, list)
  1050. if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
  1051. command->SCp.buffer = (struct scatterlist *)command_list;
  1052. command_list = command;
  1053. }
  1054. spin_unlock_irqrestore(&dev->list_lock, flags);
  1055. }
  1056. while ((command = command_list)) {
  1057. command_list = (struct scsi_cmnd *)command->SCp.buffer;
  1058. command->SCp.buffer = NULL;
  1059. command->result = DID_OK << 16
  1060. | COMMAND_COMPLETE << 8
  1061. | SAM_STAT_TASK_SET_FULL;
  1062. command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
  1063. command->scsi_done(command);
  1064. }
  1065. retval = 0;
  1066. out:
  1067. aac->in_reset = 0;
  1068. scsi_unblock_requests(host);
  1069. if (jafo) {
  1070. spin_lock_irq(host->host_lock);
  1071. }
  1072. return retval;
  1073. }
  1074. int aac_reset_adapter(struct aac_dev * aac, int forced)
  1075. {
  1076. unsigned long flagv = 0;
  1077. int retval;
  1078. struct Scsi_Host * host;
  1079. if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
  1080. return -EBUSY;
  1081. if (aac->in_reset) {
  1082. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1083. return -EBUSY;
  1084. }
  1085. aac->in_reset = 1;
  1086. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1087. /*
  1088. * Wait for all commands to complete to this specific
  1089. * target (block maximum 60 seconds). Although not necessary,
  1090. * it does make us a good storage citizen.
  1091. */
  1092. host = aac->scsi_host_ptr;
  1093. scsi_block_requests(host);
  1094. if (forced < 2) for (retval = 60; retval; --retval) {
  1095. struct scsi_device * dev;
  1096. struct scsi_cmnd * command;
  1097. int active = 0;
  1098. __shost_for_each_device(dev, host) {
  1099. spin_lock_irqsave(&dev->list_lock, flagv);
  1100. list_for_each_entry(command, &dev->cmd_list, list) {
  1101. if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
  1102. active++;
  1103. break;
  1104. }
  1105. }
  1106. spin_unlock_irqrestore(&dev->list_lock, flagv);
  1107. if (active)
  1108. break;
  1109. }
  1110. /*
  1111. * We can exit If all the commands are complete
  1112. */
  1113. if (active == 0)
  1114. break;
  1115. ssleep(1);
  1116. }
  1117. /* Quiesce build, flush cache, write through mode */
  1118. if (forced < 2)
  1119. aac_send_shutdown(aac);
  1120. spin_lock_irqsave(host->host_lock, flagv);
  1121. retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
  1122. spin_unlock_irqrestore(host->host_lock, flagv);
  1123. if ((forced < 2) && (retval == -ENODEV)) {
  1124. /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
  1125. struct fib * fibctx = aac_fib_alloc(aac);
  1126. if (fibctx) {
  1127. struct aac_pause *cmd;
  1128. int status;
  1129. aac_fib_init(fibctx);
  1130. cmd = (struct aac_pause *) fib_data(fibctx);
  1131. cmd->command = cpu_to_le32(VM_ContainerConfig);
  1132. cmd->type = cpu_to_le32(CT_PAUSE_IO);
  1133. cmd->timeout = cpu_to_le32(1);
  1134. cmd->min = cpu_to_le32(1);
  1135. cmd->noRescan = cpu_to_le32(1);
  1136. cmd->count = cpu_to_le32(0);
  1137. status = aac_fib_send(ContainerCommand,
  1138. fibctx,
  1139. sizeof(struct aac_pause),
  1140. FsaNormal,
  1141. -2 /* Timeout silently */, 1,
  1142. NULL, NULL);
  1143. if (status >= 0)
  1144. aac_fib_complete(fibctx);
  1145. aac_fib_free(fibctx);
  1146. }
  1147. }
  1148. return retval;
  1149. }
  1150. int aac_check_health(struct aac_dev * aac)
  1151. {
  1152. int BlinkLED;
  1153. unsigned long time_now, flagv = 0;
  1154. struct list_head * entry;
  1155. struct Scsi_Host * host;
  1156. /* Extending the scope of fib_lock slightly to protect aac->in_reset */
  1157. if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
  1158. return 0;
  1159. if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
  1160. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1161. return 0; /* OK */
  1162. }
  1163. aac->in_reset = 1;
  1164. /* Fake up an AIF:
  1165. * aac_aifcmd.command = AifCmdEventNotify = 1
  1166. * aac_aifcmd.seqnum = 0xFFFFFFFF
  1167. * aac_aifcmd.data[0] = AifEnExpEvent = 23
  1168. * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
  1169. * aac.aifcmd.data[2] = AifHighPriority = 3
  1170. * aac.aifcmd.data[3] = BlinkLED
  1171. */
  1172. time_now = jiffies/HZ;
  1173. entry = aac->fib_list.next;
  1174. /*
  1175. * For each Context that is on the
  1176. * fibctxList, make a copy of the
  1177. * fib, and then set the event to wake up the
  1178. * thread that is waiting for it.
  1179. */
  1180. while (entry != &aac->fib_list) {
  1181. /*
  1182. * Extract the fibctx
  1183. */
  1184. struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
  1185. struct hw_fib * hw_fib;
  1186. struct fib * fib;
  1187. /*
  1188. * Check if the queue is getting
  1189. * backlogged
  1190. */
  1191. if (fibctx->count > 20) {
  1192. /*
  1193. * It's *not* jiffies folks,
  1194. * but jiffies / HZ, so do not
  1195. * panic ...
  1196. */
  1197. u32 time_last = fibctx->jiffies;
  1198. /*
  1199. * Has it been > 2 minutes
  1200. * since the last read off
  1201. * the queue?
  1202. */
  1203. if ((time_now - time_last) > aif_timeout) {
  1204. entry = entry->next;
  1205. aac_close_fib_context(aac, fibctx);
  1206. continue;
  1207. }
  1208. }
  1209. /*
  1210. * Warning: no sleep allowed while
  1211. * holding spinlock
  1212. */
  1213. hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
  1214. fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
  1215. if (fib && hw_fib) {
  1216. struct aac_aifcmd * aif;
  1217. fib->hw_fib_va = hw_fib;
  1218. fib->dev = aac;
  1219. aac_fib_init(fib);
  1220. fib->type = FSAFS_NTC_FIB_CONTEXT;
  1221. fib->size = sizeof (struct fib);
  1222. fib->data = hw_fib->data;
  1223. aif = (struct aac_aifcmd *)hw_fib->data;
  1224. aif->command = cpu_to_le32(AifCmdEventNotify);
  1225. aif->seqnum = cpu_to_le32(0xFFFFFFFF);
  1226. aif->data[0] = AifEnExpEvent;
  1227. aif->data[1] = AifExeFirmwarePanic;
  1228. aif->data[2] = AifHighPriority;
  1229. aif->data[3] = BlinkLED;
  1230. /*
  1231. * Put the FIB onto the
  1232. * fibctx's fibs
  1233. */
  1234. list_add_tail(&fib->fiblink, &fibctx->fib_list);
  1235. fibctx->count++;
  1236. /*
  1237. * Set the event to wake up the
  1238. * thread that will waiting.
  1239. */
  1240. up(&fibctx->wait_sem);
  1241. } else {
  1242. printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
  1243. kfree(fib);
  1244. kfree(hw_fib);
  1245. }
  1246. entry = entry->next;
  1247. }
  1248. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1249. if (BlinkLED < 0) {
  1250. printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
  1251. goto out;
  1252. }
  1253. printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
  1254. if (!aac_check_reset || ((aac_check_reset != 1) &&
  1255. (aac->supplement_adapter_info.SupportedOptions2 &
  1256. le32_to_cpu(AAC_OPTION_IGNORE_RESET))))
  1257. goto out;
  1258. host = aac->scsi_host_ptr;
  1259. if (aac->thread->pid != current->pid)
  1260. spin_lock_irqsave(host->host_lock, flagv);
  1261. BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
  1262. if (aac->thread->pid != current->pid)
  1263. spin_unlock_irqrestore(host->host_lock, flagv);
  1264. return BlinkLED;
  1265. out:
  1266. aac->in_reset = 0;
  1267. return BlinkLED;
  1268. }
  1269. /**
  1270. * aac_command_thread - command processing thread
  1271. * @dev: Adapter to monitor
  1272. *
  1273. * Waits on the commandready event in it's queue. When the event gets set
  1274. * it will pull FIBs off it's queue. It will continue to pull FIBs off
  1275. * until the queue is empty. When the queue is empty it will wait for
  1276. * more FIBs.
  1277. */
  1278. int aac_command_thread(void *data)
  1279. {
  1280. struct aac_dev *dev = data;
  1281. struct hw_fib *hw_fib, *hw_newfib;
  1282. struct fib *fib, *newfib;
  1283. struct aac_fib_context *fibctx;
  1284. unsigned long flags;
  1285. DECLARE_WAITQUEUE(wait, current);
  1286. unsigned long next_jiffies = jiffies + HZ;
  1287. unsigned long next_check_jiffies = next_jiffies;
  1288. long difference = HZ;
  1289. /*
  1290. * We can only have one thread per adapter for AIF's.
  1291. */
  1292. if (dev->aif_thread)
  1293. return -EINVAL;
  1294. /*
  1295. * Let the DPC know it has a place to send the AIF's to.
  1296. */
  1297. dev->aif_thread = 1;
  1298. add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
  1299. set_current_state(TASK_INTERRUPTIBLE);
  1300. dprintk ((KERN_INFO "aac_command_thread start\n"));
  1301. while(1)
  1302. {
  1303. spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1304. while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
  1305. struct list_head *entry;
  1306. struct aac_aifcmd * aifcmd;
  1307. set_current_state(TASK_RUNNING);
  1308. entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
  1309. list_del(entry);
  1310. spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1311. fib = list_entry(entry, struct fib, fiblink);
  1312. /*
  1313. * We will process the FIB here or pass it to a
  1314. * worker thread that is TBD. We Really can't
  1315. * do anything at this point since we don't have
  1316. * anything defined for this thread to do.
  1317. */
  1318. hw_fib = fib->hw_fib_va;
  1319. memset(fib, 0, sizeof(struct fib));
  1320. fib->type = FSAFS_NTC_FIB_CONTEXT;
  1321. fib->size = sizeof( struct fib );
  1322. fib->hw_fib_va = hw_fib;
  1323. fib->data = hw_fib->data;
  1324. fib->dev = dev;
  1325. /*
  1326. * We only handle AifRequest fibs from the adapter.
  1327. */
  1328. aifcmd = (struct aac_aifcmd *) hw_fib->data;
  1329. if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
  1330. /* Handle Driver Notify Events */
  1331. aac_handle_aif(dev, fib);
  1332. *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
  1333. aac_fib_adapter_complete(fib, (u16)sizeof(u32));
  1334. } else {
  1335. struct list_head *entry;
  1336. /* The u32 here is important and intended. We are using
  1337. 32bit wrapping time to fit the adapter field */
  1338. u32 time_now, time_last;
  1339. unsigned long flagv;
  1340. unsigned num;
  1341. struct hw_fib ** hw_fib_pool, ** hw_fib_p;
  1342. struct fib ** fib_pool, ** fib_p;
  1343. /* Sniff events */
  1344. if ((aifcmd->command ==
  1345. cpu_to_le32(AifCmdEventNotify)) ||
  1346. (aifcmd->command ==
  1347. cpu_to_le32(AifCmdJobProgress))) {
  1348. aac_handle_aif(dev, fib);
  1349. }
  1350. time_now = jiffies/HZ;
  1351. /*
  1352. * Warning: no sleep allowed while
  1353. * holding spinlock. We take the estimate
  1354. * and pre-allocate a set of fibs outside the
  1355. * lock.
  1356. */
  1357. num = le32_to_cpu(dev->init->AdapterFibsSize)
  1358. / sizeof(struct hw_fib); /* some extra */
  1359. spin_lock_irqsave(&dev->fib_lock, flagv);
  1360. entry = dev->fib_list.next;
  1361. while (entry != &dev->fib_list) {
  1362. entry = entry->next;
  1363. ++num;
  1364. }
  1365. spin_unlock_irqrestore(&dev->fib_lock, flagv);
  1366. hw_fib_pool = NULL;
  1367. fib_pool = NULL;
  1368. if (num
  1369. && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
  1370. && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
  1371. hw_fib_p = hw_fib_pool;
  1372. fib_p = fib_pool;
  1373. while (hw_fib_p < &hw_fib_pool[num]) {
  1374. if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
  1375. --hw_fib_p;
  1376. break;
  1377. }
  1378. if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
  1379. kfree(*(--hw_fib_p));
  1380. break;
  1381. }
  1382. }
  1383. if ((num = hw_fib_p - hw_fib_pool) == 0) {
  1384. kfree(fib_pool);
  1385. fib_pool = NULL;
  1386. kfree(hw_fib_pool);
  1387. hw_fib_pool = NULL;
  1388. }
  1389. } else {
  1390. kfree(hw_fib_pool);
  1391. hw_fib_pool = NULL;
  1392. }
  1393. spin_lock_irqsave(&dev->fib_lock, flagv);
  1394. entry = dev->fib_list.next;
  1395. /*
  1396. * For each Context that is on the
  1397. * fibctxList, make a copy of the
  1398. * fib, and then set the event to wake up the
  1399. * thread that is waiting for it.
  1400. */
  1401. hw_fib_p = hw_fib_pool;
  1402. fib_p = fib_pool;
  1403. while (entry != &dev->fib_list) {
  1404. /*
  1405. * Extract the fibctx
  1406. */
  1407. fibctx = list_entry(entry, struct aac_fib_context, next);
  1408. /*
  1409. * Check if the queue is getting
  1410. * backlogged
  1411. */
  1412. if (fibctx->count > 20)
  1413. {
  1414. /*
  1415. * It's *not* jiffies folks,
  1416. * but jiffies / HZ so do not
  1417. * panic ...
  1418. */
  1419. time_last = fibctx->jiffies;
  1420. /*
  1421. * Has it been > 2 minutes
  1422. * since the last read off
  1423. * the queue?
  1424. */
  1425. if ((time_now - time_last) > aif_timeout) {
  1426. entry = entry->next;
  1427. aac_close_fib_context(dev, fibctx);
  1428. continue;
  1429. }
  1430. }
  1431. /*
  1432. * Warning: no sleep allowed while
  1433. * holding spinlock
  1434. */
  1435. if (hw_fib_p < &hw_fib_pool[num]) {
  1436. hw_newfib = *hw_fib_p;
  1437. *(hw_fib_p++) = NULL;
  1438. newfib = *fib_p;
  1439. *(fib_p++) = NULL;
  1440. /*
  1441. * Make the copy of the FIB
  1442. */
  1443. memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
  1444. memcpy(newfib, fib, sizeof(struct fib));
  1445. newfib->hw_fib_va = hw_newfib;
  1446. /*
  1447. * Put the FIB onto the
  1448. * fibctx's fibs
  1449. */
  1450. list_add_tail(&newfib->fiblink, &fibctx->fib_list);
  1451. fibctx->count++;
  1452. /*
  1453. * Set the event to wake up the
  1454. * thread that is waiting.
  1455. */
  1456. up(&fibctx->wait_sem);
  1457. } else {
  1458. printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
  1459. }
  1460. entry = entry->next;
  1461. }
  1462. /*
  1463. * Set the status of this FIB
  1464. */
  1465. *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
  1466. aac_fib_adapter_complete(fib, sizeof(u32));
  1467. spin_unlock_irqrestore(&dev->fib_lock, flagv);
  1468. /* Free up the remaining resources */
  1469. hw_fib_p = hw_fib_pool;
  1470. fib_p = fib_pool;
  1471. while (hw_fib_p < &hw_fib_pool[num]) {
  1472. kfree(*hw_fib_p);
  1473. kfree(*fib_p);
  1474. ++fib_p;
  1475. ++hw_fib_p;
  1476. }
  1477. kfree(hw_fib_pool);
  1478. kfree(fib_pool);
  1479. }
  1480. kfree(fib);
  1481. spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1482. }
  1483. /*
  1484. * There are no more AIF's
  1485. */
  1486. spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1487. /*
  1488. * Background activity
  1489. */
  1490. if ((time_before(next_check_jiffies,next_jiffies))
  1491. && ((difference = next_check_jiffies - jiffies) <= 0)) {
  1492. next_check_jiffies = next_jiffies;
  1493. if (aac_check_health(dev) == 0) {
  1494. difference = ((long)(unsigned)check_interval)
  1495. * HZ;
  1496. next_check_jiffies = jiffies + difference;
  1497. } else if (!dev->queues)
  1498. break;
  1499. }
  1500. if (!time_before(next_check_jiffies,next_jiffies)
  1501. && ((difference = next_jiffies - jiffies) <= 0)) {
  1502. struct timeval now;
  1503. int ret;
  1504. /* Don't even try to talk to adapter if its sick */
  1505. ret = aac_check_health(dev);
  1506. if (!ret && !dev->queues)
  1507. break;
  1508. next_check_jiffies = jiffies
  1509. + ((long)(unsigned)check_interval)
  1510. * HZ;
  1511. do_gettimeofday(&now);
  1512. /* Synchronize our watches */
  1513. if (((1000000 - (1000000 / HZ)) > now.tv_usec)
  1514. && (now.tv_usec > (1000000 / HZ)))
  1515. difference = (((1000000 - now.tv_usec) * HZ)
  1516. + 500000) / 1000000;
  1517. else if (ret == 0) {
  1518. struct fib *fibptr;
  1519. if ((fibptr = aac_fib_alloc(dev))) {
  1520. u32 * info;
  1521. aac_fib_init(fibptr);
  1522. info = (u32 *) fib_data(fibptr);
  1523. if (now.tv_usec > 500000)
  1524. ++now.tv_sec;
  1525. *info = cpu_to_le32(now.tv_sec);
  1526. (void)aac_fib_send(SendHostTime,
  1527. fibptr,
  1528. sizeof(*info),
  1529. FsaNormal,
  1530. 1, 1,
  1531. NULL,
  1532. NULL);
  1533. aac_fib_complete(fibptr);
  1534. aac_fib_free(fibptr);
  1535. }
  1536. difference = (long)(unsigned)update_interval*HZ;
  1537. } else {
  1538. /* retry shortly */
  1539. difference = 10 * HZ;
  1540. }
  1541. next_jiffies = jiffies + difference;
  1542. if (time_before(next_check_jiffies,next_jiffies))
  1543. difference = next_check_jiffies - jiffies;
  1544. }
  1545. if (difference <= 0)
  1546. difference = 1;
  1547. set_current_state(TASK_INTERRUPTIBLE);
  1548. schedule_timeout(difference);
  1549. if (kthread_should_stop())
  1550. break;
  1551. }
  1552. if (dev->queues)
  1553. remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
  1554. dev->aif_thread = 0;
  1555. return 0;
  1556. }