commsup.c 55 KB

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