commsup.c 54 KB

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