commsup.c 52 KB

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