commsup.c 50 KB

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