commsup.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Adaptec AAC series RAID controller driver
  4. * (c) Copyright 2001 Red Hat Inc.
  5. *
  6. * based on the old aacraid driver that is..
  7. * Adaptec aacraid device driver for Linux.
  8. *
  9. * Copyright (c) 2000-2010 Adaptec, Inc.
  10. * 2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
  11. * 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
  12. *
  13. * Module Name:
  14. * commsup.c
  15. *
  16. * Abstract: Contain all routines that are required for FSA host/adapter
  17. * communication.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/crash_dump.h>
  22. #include <linux/types.h>
  23. #include <linux/sched.h>
  24. #include <linux/pci.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/slab.h>
  27. #include <linux/completion.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/delay.h>
  30. #include <linux/kthread.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/bcd.h>
  33. #include <scsi/scsi.h>
  34. #include <scsi/scsi_host.h>
  35. #include <scsi/scsi_device.h>
  36. #include <scsi/scsi_cmnd.h>
  37. #include "aacraid.h"
  38. /**
  39. * fib_map_alloc - allocate the fib objects
  40. * @dev: Adapter to allocate for
  41. *
  42. * Allocate and map the shared PCI space for the FIB blocks used to
  43. * talk to the Adaptec firmware.
  44. */
  45. static int fib_map_alloc(struct aac_dev *dev)
  46. {
  47. if (dev->max_fib_size > AAC_MAX_NATIVE_SIZE)
  48. dev->max_cmd_size = AAC_MAX_NATIVE_SIZE;
  49. else
  50. dev->max_cmd_size = dev->max_fib_size;
  51. if (dev->max_fib_size < AAC_MAX_NATIVE_SIZE) {
  52. dev->max_cmd_size = AAC_MAX_NATIVE_SIZE;
  53. } else {
  54. dev->max_cmd_size = dev->max_fib_size;
  55. }
  56. dprintk((KERN_INFO
  57. "allocate hardware fibs dma_alloc_coherent(%p, %d * (%d + %d), %p)\n",
  58. &dev->pdev->dev, dev->max_cmd_size, dev->scsi_host_ptr->can_queue,
  59. AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
  60. dev->hw_fib_va = dma_alloc_coherent(&dev->pdev->dev,
  61. (dev->max_cmd_size + sizeof(struct aac_fib_xporthdr))
  62. * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) + (ALIGN32 - 1),
  63. &dev->hw_fib_pa, GFP_KERNEL);
  64. if (dev->hw_fib_va == NULL)
  65. return -ENOMEM;
  66. return 0;
  67. }
  68. /**
  69. * aac_fib_map_free - free the fib objects
  70. * @dev: Adapter to free
  71. *
  72. * Free the PCI mappings and the memory allocated for FIB blocks
  73. * on this adapter.
  74. */
  75. void aac_fib_map_free(struct aac_dev *dev)
  76. {
  77. size_t alloc_size;
  78. size_t fib_size;
  79. int num_fibs;
  80. if(!dev->hw_fib_va || !dev->max_cmd_size)
  81. return;
  82. num_fibs = dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB;
  83. fib_size = dev->max_fib_size + sizeof(struct aac_fib_xporthdr);
  84. alloc_size = fib_size * num_fibs + ALIGN32 - 1;
  85. dma_free_coherent(&dev->pdev->dev, alloc_size, dev->hw_fib_va,
  86. dev->hw_fib_pa);
  87. dev->hw_fib_va = NULL;
  88. dev->hw_fib_pa = 0;
  89. }
  90. void aac_fib_vector_assign(struct aac_dev *dev)
  91. {
  92. u32 i = 0;
  93. u32 vector = 1;
  94. struct fib *fibptr = NULL;
  95. for (i = 0, fibptr = &dev->fibs[i];
  96. i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
  97. i++, fibptr++) {
  98. if ((dev->max_msix == 1) ||
  99. (i > ((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1)
  100. - dev->vector_cap))) {
  101. fibptr->vector_no = 0;
  102. } else {
  103. fibptr->vector_no = vector;
  104. vector++;
  105. if (vector == dev->max_msix)
  106. vector = 1;
  107. }
  108. }
  109. }
  110. /**
  111. * aac_fib_setup - setup the fibs
  112. * @dev: Adapter to set up
  113. *
  114. * Allocate the PCI space for the fibs, map it and then initialise the
  115. * fib area, the unmapped fib data and also the free list
  116. */
  117. int aac_fib_setup(struct aac_dev * dev)
  118. {
  119. struct fib *fibptr;
  120. struct hw_fib *hw_fib;
  121. dma_addr_t hw_fib_pa;
  122. int i;
  123. u32 max_cmds;
  124. while (((i = fib_map_alloc(dev)) == -ENOMEM)
  125. && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
  126. max_cmds = (dev->scsi_host_ptr->can_queue+AAC_NUM_MGT_FIB) >> 1;
  127. dev->scsi_host_ptr->can_queue = max_cmds - AAC_NUM_MGT_FIB;
  128. if (dev->comm_interface != AAC_COMM_MESSAGE_TYPE3)
  129. dev->init->r7.max_io_commands = cpu_to_le32(max_cmds);
  130. }
  131. if (i<0)
  132. return -ENOMEM;
  133. memset(dev->hw_fib_va, 0,
  134. (dev->max_cmd_size + sizeof(struct aac_fib_xporthdr)) *
  135. (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
  136. /* 32 byte alignment for PMC */
  137. hw_fib_pa = (dev->hw_fib_pa + (ALIGN32 - 1)) & ~(ALIGN32 - 1);
  138. hw_fib = (struct hw_fib *)((unsigned char *)dev->hw_fib_va +
  139. (hw_fib_pa - dev->hw_fib_pa));
  140. /* add Xport header */
  141. hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
  142. sizeof(struct aac_fib_xporthdr));
  143. hw_fib_pa += sizeof(struct aac_fib_xporthdr);
  144. /*
  145. * Initialise the fibs
  146. */
  147. for (i = 0, fibptr = &dev->fibs[i];
  148. i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
  149. i++, fibptr++)
  150. {
  151. fibptr->flags = 0;
  152. fibptr->size = sizeof(struct fib);
  153. fibptr->dev = dev;
  154. fibptr->hw_fib_va = hw_fib;
  155. fibptr->data = (void *) fibptr->hw_fib_va->data;
  156. fibptr->next = fibptr+1; /* Forward chain the fibs */
  157. init_completion(&fibptr->event_wait);
  158. spin_lock_init(&fibptr->event_lock);
  159. hw_fib->header.XferState = cpu_to_le32(0xffffffff);
  160. hw_fib->header.SenderSize =
  161. cpu_to_le16(dev->max_fib_size); /* ?? max_cmd_size */
  162. fibptr->hw_fib_pa = hw_fib_pa;
  163. fibptr->hw_sgl_pa = hw_fib_pa +
  164. offsetof(struct aac_hba_cmd_req, sge[2]);
  165. /*
  166. * one element is for the ptr to the separate sg list,
  167. * second element for 32 byte alignment
  168. */
  169. fibptr->hw_error_pa = hw_fib_pa +
  170. offsetof(struct aac_native_hba, resp.resp_bytes[0]);
  171. hw_fib = (struct hw_fib *)((unsigned char *)hw_fib +
  172. dev->max_cmd_size + sizeof(struct aac_fib_xporthdr));
  173. hw_fib_pa = hw_fib_pa +
  174. dev->max_cmd_size + sizeof(struct aac_fib_xporthdr);
  175. }
  176. /*
  177. *Assign vector numbers to fibs
  178. */
  179. aac_fib_vector_assign(dev);
  180. /*
  181. * Add the fib chain to the free list
  182. */
  183. dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
  184. /*
  185. * Set 8 fibs aside for management tools
  186. */
  187. dev->free_fib = &dev->fibs[dev->scsi_host_ptr->can_queue];
  188. return 0;
  189. }
  190. /**
  191. * aac_fib_alloc_tag-allocate a fib using tags
  192. * @dev: Adapter to allocate the fib for
  193. * @scmd: SCSI command
  194. *
  195. * Allocate a fib from the adapter fib pool using tags
  196. * from the blk layer.
  197. */
  198. struct fib *aac_fib_alloc_tag(struct aac_dev *dev, struct scsi_cmnd *scmd)
  199. {
  200. struct fib *fibptr;
  201. fibptr = &dev->fibs[scsi_cmd_to_rq(scmd)->tag];
  202. /*
  203. * Null out fields that depend on being zero at the start of
  204. * each I/O
  205. */
  206. fibptr->hw_fib_va->header.XferState = 0;
  207. fibptr->type = FSAFS_NTC_FIB_CONTEXT;
  208. fibptr->callback_data = NULL;
  209. fibptr->callback = NULL;
  210. fibptr->flags = 0;
  211. return fibptr;
  212. }
  213. /**
  214. * aac_fib_alloc - allocate a fib
  215. * @dev: Adapter to allocate the fib for
  216. *
  217. * Allocate a fib from the adapter fib pool. If the pool is empty we
  218. * return NULL.
  219. */
  220. struct fib *aac_fib_alloc(struct aac_dev *dev)
  221. {
  222. struct fib * fibptr;
  223. unsigned long flags;
  224. spin_lock_irqsave(&dev->fib_lock, flags);
  225. fibptr = dev->free_fib;
  226. if(!fibptr){
  227. spin_unlock_irqrestore(&dev->fib_lock, flags);
  228. return fibptr;
  229. }
  230. dev->free_fib = fibptr->next;
  231. spin_unlock_irqrestore(&dev->fib_lock, flags);
  232. /*
  233. * Set the proper node type code and node byte size
  234. */
  235. fibptr->type = FSAFS_NTC_FIB_CONTEXT;
  236. fibptr->size = sizeof(struct fib);
  237. /*
  238. * Null out fields that depend on being zero at the start of
  239. * each I/O
  240. */
  241. fibptr->hw_fib_va->header.XferState = 0;
  242. fibptr->flags = 0;
  243. fibptr->callback = NULL;
  244. fibptr->callback_data = NULL;
  245. return fibptr;
  246. }
  247. /**
  248. * aac_fib_free - free a fib
  249. * @fibptr: fib to free up
  250. *
  251. * Frees up a fib and places it on the appropriate queue
  252. */
  253. void aac_fib_free(struct fib *fibptr)
  254. {
  255. unsigned long flags;
  256. if (fibptr->done == 2)
  257. return;
  258. spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
  259. if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
  260. aac_config.fib_timeouts++;
  261. if (!(fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) &&
  262. fibptr->hw_fib_va->header.XferState != 0) {
  263. printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
  264. (void*)fibptr,
  265. le32_to_cpu(fibptr->hw_fib_va->header.XferState));
  266. }
  267. fibptr->next = fibptr->dev->free_fib;
  268. fibptr->dev->free_fib = fibptr;
  269. spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
  270. }
  271. /**
  272. * aac_fib_init - initialise a fib
  273. * @fibptr: The fib to initialize
  274. *
  275. * Set up the generic fib fields ready for use
  276. */
  277. void aac_fib_init(struct fib *fibptr)
  278. {
  279. struct hw_fib *hw_fib = fibptr->hw_fib_va;
  280. memset(&hw_fib->header, 0, sizeof(struct aac_fibhdr));
  281. hw_fib->header.StructType = FIB_MAGIC;
  282. hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
  283. hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
  284. hw_fib->header.u.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
  285. hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
  286. }
  287. /**
  288. * fib_dealloc - deallocate a fib
  289. * @fibptr: fib to deallocate
  290. *
  291. * Will deallocate and return to the free pool the FIB pointed to by the
  292. * caller.
  293. */
  294. static void fib_dealloc(struct fib * fibptr)
  295. {
  296. struct hw_fib *hw_fib = fibptr->hw_fib_va;
  297. hw_fib->header.XferState = 0;
  298. }
  299. /*
  300. * Commuication primitives define and support the queuing method we use to
  301. * support host to adapter commuication. All queue accesses happen through
  302. * these routines and are the only routines which have a knowledge of the
  303. * how these queues are implemented.
  304. */
  305. /**
  306. * aac_get_entry - get a queue entry
  307. * @dev: Adapter
  308. * @qid: Queue Number
  309. * @entry: Entry return
  310. * @index: Index return
  311. * @nonotify: notification control
  312. *
  313. * With a priority the routine returns a queue entry if the queue has free entries. If the queue
  314. * is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
  315. * returned.
  316. */
  317. static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
  318. {
  319. struct aac_queue * q;
  320. unsigned long idx;
  321. /*
  322. * All of the queues wrap when they reach the end, so we check
  323. * to see if they have reached the end and if they have we just
  324. * set the index back to zero. This is a wrap. You could or off
  325. * the high bits in all updates but this is a bit faster I think.
  326. */
  327. q = &dev->queues->queue[qid];
  328. idx = *index = le32_to_cpu(*(q->headers.producer));
  329. /* Interrupt Moderation, only interrupt for first two entries */
  330. if (idx != le32_to_cpu(*(q->headers.consumer))) {
  331. if (--idx == 0) {
  332. if (qid == AdapNormCmdQueue)
  333. idx = ADAP_NORM_CMD_ENTRIES;
  334. else
  335. idx = ADAP_NORM_RESP_ENTRIES;
  336. }
  337. if (idx != le32_to_cpu(*(q->headers.consumer)))
  338. *nonotify = 1;
  339. }
  340. if (qid == AdapNormCmdQueue) {
  341. if (*index >= ADAP_NORM_CMD_ENTRIES)
  342. *index = 0; /* Wrap to front of the Producer Queue. */
  343. } else {
  344. if (*index >= ADAP_NORM_RESP_ENTRIES)
  345. *index = 0; /* Wrap to front of the Producer Queue. */
  346. }
  347. /* Queue is full */
  348. if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
  349. printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
  350. qid, atomic_read(&q->numpending));
  351. return 0;
  352. } else {
  353. *entry = q->base + *index;
  354. return 1;
  355. }
  356. }
  357. /**
  358. * aac_queue_get - get the next free QE
  359. * @dev: Adapter
  360. * @index: Returned index
  361. * @qid: Queue number
  362. * @hw_fib: Fib to associate with the queue entry
  363. * @wait: Wait if queue full
  364. * @fibptr: Driver fib object to go with fib
  365. * @nonotify: Don't notify the adapter
  366. *
  367. * Gets the next free QE off the requested priorty adapter command
  368. * queue and associates the Fib with the QE. The QE represented by
  369. * index is ready to insert on the queue when this routine returns
  370. * success.
  371. */
  372. 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)
  373. {
  374. struct aac_entry * entry = NULL;
  375. int map = 0;
  376. if (qid == AdapNormCmdQueue) {
  377. /* if no entries wait for some if caller wants to */
  378. while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
  379. printk(KERN_ERR "GetEntries failed\n");
  380. }
  381. /*
  382. * Setup queue entry with a command, status and fib mapped
  383. */
  384. entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
  385. map = 1;
  386. } else {
  387. while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
  388. /* if no entries wait for some if caller wants to */
  389. }
  390. /*
  391. * Setup queue entry with command, status and fib mapped
  392. */
  393. entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
  394. entry->addr = hw_fib->header.SenderFibAddress;
  395. /* Restore adapters pointer to the FIB */
  396. hw_fib->header.u.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */
  397. map = 0;
  398. }
  399. /*
  400. * If MapFib is true than we need to map the Fib and put pointers
  401. * in the queue entry.
  402. */
  403. if (map)
  404. entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
  405. return 0;
  406. }
  407. /*
  408. * Define the highest level of host to adapter communication routines.
  409. * These routines will support host to adapter FS commuication. These
  410. * routines have no knowledge of the commuication method used. This level
  411. * sends and receives FIBs. This level has no knowledge of how these FIBs
  412. * get passed back and forth.
  413. */
  414. /**
  415. * aac_fib_send - send a fib to the adapter
  416. * @command: Command to send
  417. * @fibptr: The fib
  418. * @size: Size of fib data area
  419. * @priority: Priority of Fib
  420. * @wait: Async/sync select
  421. * @reply: True if a reply is wanted
  422. * @callback: Called with reply
  423. * @callback_data: Passed to callback
  424. *
  425. * Sends the requested FIB to the adapter and optionally will wait for a
  426. * response FIB. If the caller does not wish to wait for a response than
  427. * an event to wait on must be supplied. This event will be set when a
  428. * response FIB is received from the adapter.
  429. */
  430. int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
  431. int priority, int wait, int reply, fib_callback callback,
  432. void *callback_data)
  433. {
  434. struct aac_dev * dev = fibptr->dev;
  435. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  436. unsigned long flags = 0;
  437. unsigned long mflags = 0;
  438. unsigned long sflags = 0;
  439. if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
  440. return -EBUSY;
  441. if (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed))
  442. return -EINVAL;
  443. /*
  444. * There are 5 cases with the wait and response requested flags.
  445. * The only invalid cases are if the caller requests to wait and
  446. * does not request a response and if the caller does not want a
  447. * response and the Fib is not allocated from pool. If a response
  448. * is not requested the Fib will just be deallocaed by the DPC
  449. * routine when the response comes back from the adapter. No
  450. * further processing will be done besides deleting the Fib. We
  451. * will have a debug mode where the adapter can notify the host
  452. * it had a problem and the host can log that fact.
  453. */
  454. fibptr->flags = 0;
  455. if (wait && !reply) {
  456. return -EINVAL;
  457. } else if (!wait && reply) {
  458. hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
  459. FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
  460. } else if (!wait && !reply) {
  461. hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
  462. FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
  463. } else if (wait && reply) {
  464. hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
  465. FIB_COUNTER_INCREMENT(aac_config.NormalSent);
  466. }
  467. /*
  468. * Map the fib into 32bits by using the fib number
  469. */
  470. hw_fib->header.SenderFibAddress =
  471. cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
  472. /* use the same shifted value for handle to be compatible
  473. * with the new native hba command handle
  474. */
  475. hw_fib->header.Handle =
  476. cpu_to_le32((((u32)(fibptr - dev->fibs)) << 2) + 1);
  477. /*
  478. * Set FIB state to indicate where it came from and if we want a
  479. * response from the adapter. Also load the command from the
  480. * caller.
  481. *
  482. * Map the hw fib pointer as a 32bit value
  483. */
  484. hw_fib->header.Command = cpu_to_le16(command);
  485. hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
  486. /*
  487. * Set the size of the Fib we want to send to the adapter
  488. */
  489. hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
  490. if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
  491. return -EMSGSIZE;
  492. }
  493. /*
  494. * Get a queue entry connect the FIB to it and send an notify
  495. * the adapter a command is ready.
  496. */
  497. hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
  498. /*
  499. * Fill in the Callback and CallbackContext if we are not
  500. * going to wait.
  501. */
  502. if (!wait) {
  503. fibptr->callback = callback;
  504. fibptr->callback_data = callback_data;
  505. fibptr->flags = FIB_CONTEXT_FLAG;
  506. }
  507. fibptr->done = 0;
  508. FIB_COUNTER_INCREMENT(aac_config.FibsSent);
  509. dprintk((KERN_DEBUG "Fib contents:.\n"));
  510. dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command)));
  511. dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
  512. dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState)));
  513. dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va));
  514. dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
  515. dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
  516. if (!dev->queues)
  517. return -EBUSY;
  518. if (wait) {
  519. spin_lock_irqsave(&dev->manage_lock, mflags);
  520. if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
  521. printk(KERN_INFO "No management Fibs Available:%d\n",
  522. dev->management_fib_count);
  523. spin_unlock_irqrestore(&dev->manage_lock, mflags);
  524. return -EBUSY;
  525. }
  526. dev->management_fib_count++;
  527. spin_unlock_irqrestore(&dev->manage_lock, mflags);
  528. spin_lock_irqsave(&fibptr->event_lock, flags);
  529. }
  530. if (dev->sync_mode) {
  531. if (wait)
  532. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  533. spin_lock_irqsave(&dev->sync_lock, sflags);
  534. if (dev->sync_fib) {
  535. list_add_tail(&fibptr->fiblink, &dev->sync_fib_list);
  536. spin_unlock_irqrestore(&dev->sync_lock, sflags);
  537. } else {
  538. dev->sync_fib = fibptr;
  539. spin_unlock_irqrestore(&dev->sync_lock, sflags);
  540. aac_adapter_sync_cmd(dev, SEND_SYNCHRONOUS_FIB,
  541. (u32)fibptr->hw_fib_pa, 0, 0, 0, 0, 0,
  542. NULL, NULL, NULL, NULL, NULL);
  543. }
  544. if (wait) {
  545. fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
  546. if (wait_for_completion_interruptible(&fibptr->event_wait)) {
  547. fibptr->flags &= ~FIB_CONTEXT_FLAG_WAIT;
  548. return -EFAULT;
  549. }
  550. return 0;
  551. }
  552. return -EINPROGRESS;
  553. }
  554. if (aac_adapter_deliver(fibptr) != 0) {
  555. printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
  556. if (wait) {
  557. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  558. spin_lock_irqsave(&dev->manage_lock, mflags);
  559. dev->management_fib_count--;
  560. spin_unlock_irqrestore(&dev->manage_lock, mflags);
  561. }
  562. return -EBUSY;
  563. }
  564. /*
  565. * If the caller wanted us to wait for response wait now.
  566. */
  567. if (wait) {
  568. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  569. /* Only set for first known interruptable command */
  570. if (wait < 0) {
  571. /*
  572. * *VERY* Dangerous to time out a command, the
  573. * assumption is made that we have no hope of
  574. * functioning because an interrupt routing or other
  575. * hardware failure has occurred.
  576. */
  577. unsigned long timeout = jiffies + (180 * HZ); /* 3 minutes */
  578. while (!try_wait_for_completion(&fibptr->event_wait)) {
  579. int blink;
  580. if (time_is_before_eq_jiffies(timeout)) {
  581. struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
  582. atomic_dec(&q->numpending);
  583. if (wait == -1) {
  584. printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
  585. "Usually a result of a PCI interrupt routing problem;\n"
  586. "update mother board BIOS or consider utilizing one of\n"
  587. "the SAFE mode kernel options (acpi, apic etc)\n");
  588. }
  589. return -ETIMEDOUT;
  590. }
  591. if (unlikely(aac_pci_offline(dev)))
  592. return -EFAULT;
  593. if ((blink = aac_adapter_check_health(dev)) > 0) {
  594. if (wait == -1) {
  595. printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
  596. "Usually a result of a serious unrecoverable hardware problem\n",
  597. blink);
  598. }
  599. return -EFAULT;
  600. }
  601. /*
  602. * Allow other processes / CPUS to use core
  603. */
  604. schedule();
  605. }
  606. } else if (wait_for_completion_interruptible(&fibptr->event_wait)) {
  607. /* Do nothing ... satisfy
  608. * wait_for_completion_interruptible must_check */
  609. }
  610. spin_lock_irqsave(&fibptr->event_lock, flags);
  611. if (fibptr->done == 0) {
  612. fibptr->done = 2; /* Tell interrupt we aborted */
  613. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  614. return -ERESTARTSYS;
  615. }
  616. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  617. BUG_ON(fibptr->done == 0);
  618. if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
  619. return -ETIMEDOUT;
  620. return 0;
  621. }
  622. /*
  623. * If the user does not want a response than return success otherwise
  624. * return pending
  625. */
  626. if (reply)
  627. return -EINPROGRESS;
  628. else
  629. return 0;
  630. }
  631. int aac_hba_send(u8 command, struct fib *fibptr, fib_callback callback,
  632. void *callback_data)
  633. {
  634. struct aac_dev *dev = fibptr->dev;
  635. int wait;
  636. unsigned long flags = 0;
  637. unsigned long mflags = 0;
  638. struct aac_hba_cmd_req *hbacmd = (struct aac_hba_cmd_req *)
  639. fibptr->hw_fib_va;
  640. fibptr->flags = (FIB_CONTEXT_FLAG | FIB_CONTEXT_FLAG_NATIVE_HBA);
  641. if (callback) {
  642. wait = 0;
  643. fibptr->callback = callback;
  644. fibptr->callback_data = callback_data;
  645. } else
  646. wait = 1;
  647. hbacmd->iu_type = command;
  648. if (command == HBA_IU_TYPE_SCSI_CMD_REQ) {
  649. /* bit1 of request_id must be 0 */
  650. hbacmd->request_id =
  651. cpu_to_le32((((u32)(fibptr - dev->fibs)) << 2) + 1);
  652. fibptr->flags |= FIB_CONTEXT_FLAG_SCSI_CMD;
  653. } else
  654. return -EINVAL;
  655. if (wait) {
  656. spin_lock_irqsave(&dev->manage_lock, mflags);
  657. if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
  658. spin_unlock_irqrestore(&dev->manage_lock, mflags);
  659. return -EBUSY;
  660. }
  661. dev->management_fib_count++;
  662. spin_unlock_irqrestore(&dev->manage_lock, mflags);
  663. spin_lock_irqsave(&fibptr->event_lock, flags);
  664. }
  665. if (aac_adapter_deliver(fibptr) != 0) {
  666. if (wait) {
  667. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  668. spin_lock_irqsave(&dev->manage_lock, mflags);
  669. dev->management_fib_count--;
  670. spin_unlock_irqrestore(&dev->manage_lock, mflags);
  671. }
  672. return -EBUSY;
  673. }
  674. FIB_COUNTER_INCREMENT(aac_config.NativeSent);
  675. if (wait) {
  676. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  677. if (unlikely(aac_pci_offline(dev)))
  678. return -EFAULT;
  679. fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
  680. if (wait_for_completion_interruptible(&fibptr->event_wait))
  681. fibptr->done = 2;
  682. fibptr->flags &= ~(FIB_CONTEXT_FLAG_WAIT);
  683. spin_lock_irqsave(&fibptr->event_lock, flags);
  684. if ((fibptr->done == 0) || (fibptr->done == 2)) {
  685. fibptr->done = 2; /* Tell interrupt we aborted */
  686. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  687. return -ERESTARTSYS;
  688. }
  689. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  690. WARN_ON(fibptr->done == 0);
  691. if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
  692. return -ETIMEDOUT;
  693. return 0;
  694. }
  695. return -EINPROGRESS;
  696. }
  697. /**
  698. * aac_consumer_get - get the top of the queue
  699. * @dev: Adapter
  700. * @q: Queue
  701. * @entry: Return entry
  702. *
  703. * Will return a pointer to the entry on the top of the queue requested that
  704. * we are a consumer of, and return the address of the queue entry. It does
  705. * not change the state of the queue.
  706. */
  707. int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
  708. {
  709. u32 index;
  710. int status;
  711. if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
  712. status = 0;
  713. } else {
  714. /*
  715. * The consumer index must be wrapped if we have reached
  716. * the end of the queue, else we just use the entry
  717. * pointed to by the header index
  718. */
  719. if (le32_to_cpu(*q->headers.consumer) >= q->entries)
  720. index = 0;
  721. else
  722. index = le32_to_cpu(*q->headers.consumer);
  723. *entry = q->base + index;
  724. status = 1;
  725. }
  726. return(status);
  727. }
  728. /**
  729. * aac_consumer_free - free consumer entry
  730. * @dev: Adapter
  731. * @q: Queue
  732. * @qid: Queue ident
  733. *
  734. * Frees up the current top of the queue we are a consumer of. If the
  735. * queue was full notify the producer that the queue is no longer full.
  736. */
  737. void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
  738. {
  739. int wasfull = 0;
  740. u32 notify;
  741. if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
  742. wasfull = 1;
  743. if (le32_to_cpu(*q->headers.consumer) >= q->entries)
  744. *q->headers.consumer = cpu_to_le32(1);
  745. else
  746. le32_add_cpu(q->headers.consumer, 1);
  747. if (wasfull) {
  748. switch (qid) {
  749. case HostNormCmdQueue:
  750. notify = HostNormCmdNotFull;
  751. break;
  752. case HostNormRespQueue:
  753. notify = HostNormRespNotFull;
  754. break;
  755. default:
  756. BUG();
  757. return;
  758. }
  759. aac_adapter_notify(dev, notify);
  760. }
  761. }
  762. /**
  763. * aac_fib_adapter_complete - complete adapter issued fib
  764. * @fibptr: fib to complete
  765. * @size: size of fib
  766. *
  767. * Will do all necessary work to complete a FIB that was sent from
  768. * the adapter.
  769. */
  770. int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
  771. {
  772. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  773. struct aac_dev * dev = fibptr->dev;
  774. struct aac_queue * q;
  775. unsigned long nointr = 0;
  776. unsigned long qflags;
  777. if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1 ||
  778. dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
  779. dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) {
  780. kfree(hw_fib);
  781. return 0;
  782. }
  783. if (hw_fib->header.XferState == 0) {
  784. if (dev->comm_interface == AAC_COMM_MESSAGE)
  785. kfree(hw_fib);
  786. return 0;
  787. }
  788. /*
  789. * If we plan to do anything check the structure type first.
  790. */
  791. if (hw_fib->header.StructType != FIB_MAGIC &&
  792. hw_fib->header.StructType != FIB_MAGIC2 &&
  793. hw_fib->header.StructType != FIB_MAGIC2_64) {
  794. if (dev->comm_interface == AAC_COMM_MESSAGE)
  795. kfree(hw_fib);
  796. return -EINVAL;
  797. }
  798. /*
  799. * This block handles the case where the adapter had sent us a
  800. * command and we have finished processing the command. We
  801. * call completeFib when we are done processing the command
  802. * and want to send a response back to the adapter. This will
  803. * send the completed cdb to the adapter.
  804. */
  805. if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
  806. if (dev->comm_interface == AAC_COMM_MESSAGE) {
  807. kfree (hw_fib);
  808. } else {
  809. u32 index;
  810. hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
  811. if (size) {
  812. size += sizeof(struct aac_fibhdr);
  813. if (size > le16_to_cpu(hw_fib->header.SenderSize))
  814. return -EMSGSIZE;
  815. hw_fib->header.Size = cpu_to_le16(size);
  816. }
  817. q = &dev->queues->queue[AdapNormRespQueue];
  818. spin_lock_irqsave(q->lock, qflags);
  819. aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
  820. *(q->headers.producer) = cpu_to_le32(index + 1);
  821. spin_unlock_irqrestore(q->lock, qflags);
  822. if (!(nointr & (int)aac_config.irq_mod))
  823. aac_adapter_notify(dev, AdapNormRespQueue);
  824. }
  825. } else {
  826. printk(KERN_WARNING "aac_fib_adapter_complete: "
  827. "Unknown xferstate detected.\n");
  828. BUG();
  829. }
  830. return 0;
  831. }
  832. /**
  833. * aac_fib_complete - fib completion handler
  834. * @fibptr: FIB to complete
  835. *
  836. * Will do all necessary work to complete a FIB.
  837. */
  838. int aac_fib_complete(struct fib *fibptr)
  839. {
  840. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  841. if (fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) {
  842. fib_dealloc(fibptr);
  843. return 0;
  844. }
  845. /*
  846. * Check for a fib which has already been completed or with a
  847. * status wait timeout
  848. */
  849. if (hw_fib->header.XferState == 0 || fibptr->done == 2)
  850. return 0;
  851. /*
  852. * If we plan to do anything check the structure type first.
  853. */
  854. if (hw_fib->header.StructType != FIB_MAGIC &&
  855. hw_fib->header.StructType != FIB_MAGIC2 &&
  856. hw_fib->header.StructType != FIB_MAGIC2_64)
  857. return -EINVAL;
  858. /*
  859. * This block completes a cdb which orginated on the host and we
  860. * just need to deallocate the cdb or reinit it. At this point the
  861. * command is complete that we had sent to the adapter and this
  862. * cdb could be reused.
  863. */
  864. if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
  865. (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
  866. {
  867. fib_dealloc(fibptr);
  868. }
  869. else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
  870. {
  871. /*
  872. * This handles the case when the host has aborted the I/O
  873. * to the adapter because the adapter is not responding
  874. */
  875. fib_dealloc(fibptr);
  876. } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
  877. fib_dealloc(fibptr);
  878. } else {
  879. BUG();
  880. }
  881. return 0;
  882. }
  883. /**
  884. * aac_printf - handle printf from firmware
  885. * @dev: Adapter
  886. * @val: Message info
  887. *
  888. * Print a message passed to us by the controller firmware on the
  889. * Adaptec board
  890. */
  891. void aac_printf(struct aac_dev *dev, u32 val)
  892. {
  893. char *cp = dev->printfbuf;
  894. if (dev->printf_enabled)
  895. {
  896. int length = val & 0xffff;
  897. int level = (val >> 16) & 0xffff;
  898. /*
  899. * The size of the printfbuf is set in port.c
  900. * There is no variable or define for it
  901. */
  902. if (length > 255)
  903. length = 255;
  904. if (cp[length] != 0)
  905. cp[length] = 0;
  906. if (level == LOG_AAC_HIGH_ERROR)
  907. printk(KERN_WARNING "%s:%s", dev->name, cp);
  908. else
  909. printk(KERN_INFO "%s:%s", dev->name, cp);
  910. }
  911. memset(cp, 0, 256);
  912. }
  913. static inline int aac_aif_data(struct aac_aifcmd *aifcmd, uint32_t index)
  914. {
  915. return le32_to_cpu(((__le32 *)aifcmd->data)[index]);
  916. }
  917. static void aac_handle_aif_bu(struct aac_dev *dev, struct aac_aifcmd *aifcmd)
  918. {
  919. switch (aac_aif_data(aifcmd, 1)) {
  920. case AifBuCacheDataLoss:
  921. if (aac_aif_data(aifcmd, 2))
  922. dev_info(&dev->pdev->dev, "Backup unit had cache data loss - [%d]\n",
  923. aac_aif_data(aifcmd, 2));
  924. else
  925. dev_info(&dev->pdev->dev, "Backup Unit had cache data loss\n");
  926. break;
  927. case AifBuCacheDataRecover:
  928. if (aac_aif_data(aifcmd, 2))
  929. dev_info(&dev->pdev->dev, "DDR cache data recovered successfully - [%d]\n",
  930. aac_aif_data(aifcmd, 2));
  931. else
  932. dev_info(&dev->pdev->dev, "DDR cache data recovered successfully\n");
  933. break;
  934. }
  935. }
  936. #define AIF_SNIFF_TIMEOUT (500*HZ)
  937. /**
  938. * aac_handle_aif - Handle a message from the firmware
  939. * @dev: Which adapter this fib is from
  940. * @fibptr: Pointer to fibptr from adapter
  941. *
  942. * This routine handles a driver notify fib from the adapter and
  943. * dispatches it to the appropriate routine for handling.
  944. */
  945. static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
  946. {
  947. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  948. struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
  949. u32 channel, id, lun, container;
  950. struct scsi_device *device;
  951. enum {
  952. NOTHING,
  953. DELETE,
  954. ADD,
  955. CHANGE
  956. } device_config_needed = NOTHING;
  957. /* Sniff for container changes */
  958. if (!dev || !dev->fsa_dev)
  959. return;
  960. container = channel = id = lun = (u32)-1;
  961. /*
  962. * We have set this up to try and minimize the number of
  963. * re-configures that take place. As a result of this when
  964. * certain AIF's come in we will set a flag waiting for another
  965. * type of AIF before setting the re-config flag.
  966. */
  967. switch (le32_to_cpu(aifcmd->command)) {
  968. case AifCmdDriverNotify:
  969. switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
  970. case AifRawDeviceRemove:
  971. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  972. if ((container >> 28)) {
  973. container = (u32)-1;
  974. break;
  975. }
  976. channel = (container >> 24) & 0xF;
  977. if (channel >= dev->maximum_num_channels) {
  978. container = (u32)-1;
  979. break;
  980. }
  981. id = container & 0xFFFF;
  982. if (id >= dev->maximum_num_physicals) {
  983. container = (u32)-1;
  984. break;
  985. }
  986. lun = (container >> 16) & 0xFF;
  987. container = (u32)-1;
  988. channel = aac_phys_to_logical(channel);
  989. device_config_needed = DELETE;
  990. break;
  991. /*
  992. * Morph or Expand complete
  993. */
  994. case AifDenMorphComplete:
  995. case AifDenVolumeExtendComplete:
  996. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  997. if (container >= dev->maximum_num_containers)
  998. break;
  999. /*
  1000. * Find the scsi_device associated with the SCSI
  1001. * address. Make sure we have the right array, and if
  1002. * so set the flag to initiate a new re-config once we
  1003. * see an AifEnConfigChange AIF come through.
  1004. */
  1005. if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
  1006. device = scsi_device_lookup(dev->scsi_host_ptr,
  1007. CONTAINER_TO_CHANNEL(container),
  1008. CONTAINER_TO_ID(container),
  1009. CONTAINER_TO_LUN(container));
  1010. if (device) {
  1011. dev->fsa_dev[container].config_needed = CHANGE;
  1012. dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
  1013. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  1014. scsi_device_put(device);
  1015. }
  1016. }
  1017. }
  1018. /*
  1019. * If we are waiting on something and this happens to be
  1020. * that thing then set the re-configure flag.
  1021. */
  1022. if (container != (u32)-1) {
  1023. if (container >= dev->maximum_num_containers)
  1024. break;
  1025. if ((dev->fsa_dev[container].config_waiting_on ==
  1026. le32_to_cpu(*(__le32 *)aifcmd->data)) &&
  1027. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  1028. dev->fsa_dev[container].config_waiting_on = 0;
  1029. } else for (container = 0;
  1030. container < dev->maximum_num_containers; ++container) {
  1031. if ((dev->fsa_dev[container].config_waiting_on ==
  1032. le32_to_cpu(*(__le32 *)aifcmd->data)) &&
  1033. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  1034. dev->fsa_dev[container].config_waiting_on = 0;
  1035. }
  1036. break;
  1037. case AifCmdEventNotify:
  1038. switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
  1039. case AifEnBatteryEvent:
  1040. dev->cache_protected =
  1041. (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
  1042. break;
  1043. /*
  1044. * Add an Array.
  1045. */
  1046. case AifEnAddContainer:
  1047. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  1048. if (container >= dev->maximum_num_containers)
  1049. break;
  1050. dev->fsa_dev[container].config_needed = ADD;
  1051. dev->fsa_dev[container].config_waiting_on =
  1052. AifEnConfigChange;
  1053. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  1054. break;
  1055. /*
  1056. * Delete an Array.
  1057. */
  1058. case AifEnDeleteContainer:
  1059. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  1060. if (container >= dev->maximum_num_containers)
  1061. break;
  1062. dev->fsa_dev[container].config_needed = DELETE;
  1063. dev->fsa_dev[container].config_waiting_on =
  1064. AifEnConfigChange;
  1065. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  1066. break;
  1067. /*
  1068. * Container change detected. If we currently are not
  1069. * waiting on something else, setup to wait on a Config Change.
  1070. */
  1071. case AifEnContainerChange:
  1072. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  1073. if (container >= dev->maximum_num_containers)
  1074. break;
  1075. if (dev->fsa_dev[container].config_waiting_on &&
  1076. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  1077. break;
  1078. dev->fsa_dev[container].config_needed = CHANGE;
  1079. dev->fsa_dev[container].config_waiting_on =
  1080. AifEnConfigChange;
  1081. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  1082. break;
  1083. case AifEnConfigChange:
  1084. break;
  1085. case AifEnAddJBOD:
  1086. case AifEnDeleteJBOD:
  1087. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  1088. if ((container >> 28)) {
  1089. container = (u32)-1;
  1090. break;
  1091. }
  1092. channel = (container >> 24) & 0xF;
  1093. if (channel >= dev->maximum_num_channels) {
  1094. container = (u32)-1;
  1095. break;
  1096. }
  1097. id = container & 0xFFFF;
  1098. if (id >= dev->maximum_num_physicals) {
  1099. container = (u32)-1;
  1100. break;
  1101. }
  1102. lun = (container >> 16) & 0xFF;
  1103. container = (u32)-1;
  1104. channel = aac_phys_to_logical(channel);
  1105. device_config_needed =
  1106. (((__le32 *)aifcmd->data)[0] ==
  1107. cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
  1108. if (device_config_needed == ADD) {
  1109. device = scsi_device_lookup(dev->scsi_host_ptr,
  1110. channel,
  1111. id,
  1112. lun);
  1113. if (device) {
  1114. scsi_remove_device(device);
  1115. scsi_device_put(device);
  1116. }
  1117. }
  1118. break;
  1119. case AifEnEnclosureManagement:
  1120. /*
  1121. * If in JBOD mode, automatic exposure of new
  1122. * physical target to be suppressed until configured.
  1123. */
  1124. if (dev->jbod)
  1125. break;
  1126. switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
  1127. case EM_DRIVE_INSERTION:
  1128. case EM_DRIVE_REMOVAL:
  1129. case EM_SES_DRIVE_INSERTION:
  1130. case EM_SES_DRIVE_REMOVAL:
  1131. container = le32_to_cpu(
  1132. ((__le32 *)aifcmd->data)[2]);
  1133. if ((container >> 28)) {
  1134. container = (u32)-1;
  1135. break;
  1136. }
  1137. channel = (container >> 24) & 0xF;
  1138. if (channel >= dev->maximum_num_channels) {
  1139. container = (u32)-1;
  1140. break;
  1141. }
  1142. id = container & 0xFFFF;
  1143. lun = (container >> 16) & 0xFF;
  1144. container = (u32)-1;
  1145. if (id >= dev->maximum_num_physicals) {
  1146. /* legacy dev_t ? */
  1147. if ((0x2000 <= id) || lun || channel ||
  1148. ((channel = (id >> 7) & 0x3F) >=
  1149. dev->maximum_num_channels))
  1150. break;
  1151. lun = (id >> 4) & 7;
  1152. id &= 0xF;
  1153. }
  1154. channel = aac_phys_to_logical(channel);
  1155. device_config_needed =
  1156. ((((__le32 *)aifcmd->data)[3]
  1157. == cpu_to_le32(EM_DRIVE_INSERTION)) ||
  1158. (((__le32 *)aifcmd->data)[3]
  1159. == cpu_to_le32(EM_SES_DRIVE_INSERTION))) ?
  1160. ADD : DELETE;
  1161. break;
  1162. }
  1163. break;
  1164. case AifBuManagerEvent:
  1165. aac_handle_aif_bu(dev, aifcmd);
  1166. break;
  1167. }
  1168. /*
  1169. * If we are waiting on something and this happens to be
  1170. * that thing then set the re-configure flag.
  1171. */
  1172. if (container != (u32)-1) {
  1173. if (container >= dev->maximum_num_containers)
  1174. break;
  1175. if ((dev->fsa_dev[container].config_waiting_on ==
  1176. le32_to_cpu(*(__le32 *)aifcmd->data)) &&
  1177. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  1178. dev->fsa_dev[container].config_waiting_on = 0;
  1179. } else for (container = 0;
  1180. container < dev->maximum_num_containers; ++container) {
  1181. if ((dev->fsa_dev[container].config_waiting_on ==
  1182. le32_to_cpu(*(__le32 *)aifcmd->data)) &&
  1183. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  1184. dev->fsa_dev[container].config_waiting_on = 0;
  1185. }
  1186. break;
  1187. case AifCmdJobProgress:
  1188. /*
  1189. * These are job progress AIF's. When a Clear is being
  1190. * done on a container it is initially created then hidden from
  1191. * the OS. When the clear completes we don't get a config
  1192. * change so we monitor the job status complete on a clear then
  1193. * wait for a container change.
  1194. */
  1195. if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
  1196. (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
  1197. ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
  1198. for (container = 0;
  1199. container < dev->maximum_num_containers;
  1200. ++container) {
  1201. /*
  1202. * Stomp on all config sequencing for all
  1203. * containers?
  1204. */
  1205. dev->fsa_dev[container].config_waiting_on =
  1206. AifEnContainerChange;
  1207. dev->fsa_dev[container].config_needed = ADD;
  1208. dev->fsa_dev[container].config_waiting_stamp =
  1209. jiffies;
  1210. }
  1211. }
  1212. if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
  1213. ((__le32 *)aifcmd->data)[6] == 0 &&
  1214. ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
  1215. for (container = 0;
  1216. container < dev->maximum_num_containers;
  1217. ++container) {
  1218. /*
  1219. * Stomp on all config sequencing for all
  1220. * containers?
  1221. */
  1222. dev->fsa_dev[container].config_waiting_on =
  1223. AifEnContainerChange;
  1224. dev->fsa_dev[container].config_needed = DELETE;
  1225. dev->fsa_dev[container].config_waiting_stamp =
  1226. jiffies;
  1227. }
  1228. }
  1229. break;
  1230. }
  1231. container = 0;
  1232. retry_next:
  1233. if (device_config_needed == NOTHING) {
  1234. for (; container < dev->maximum_num_containers; ++container) {
  1235. if ((dev->fsa_dev[container].config_waiting_on == 0) &&
  1236. (dev->fsa_dev[container].config_needed != NOTHING) &&
  1237. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
  1238. device_config_needed =
  1239. dev->fsa_dev[container].config_needed;
  1240. dev->fsa_dev[container].config_needed = NOTHING;
  1241. channel = CONTAINER_TO_CHANNEL(container);
  1242. id = CONTAINER_TO_ID(container);
  1243. lun = CONTAINER_TO_LUN(container);
  1244. break;
  1245. }
  1246. }
  1247. }
  1248. if (device_config_needed == NOTHING)
  1249. return;
  1250. /*
  1251. * If we decided that a re-configuration needs to be done,
  1252. * schedule it here on the way out the door, please close the door
  1253. * behind you.
  1254. */
  1255. /*
  1256. * Find the scsi_device associated with the SCSI address,
  1257. * and mark it as changed, invalidating the cache. This deals
  1258. * with changes to existing device IDs.
  1259. */
  1260. if (!dev || !dev->scsi_host_ptr)
  1261. return;
  1262. /*
  1263. * force reload of disk info via aac_probe_container
  1264. */
  1265. if ((channel == CONTAINER_CHANNEL) &&
  1266. (device_config_needed != NOTHING)) {
  1267. if (dev->fsa_dev[container].valid == 1)
  1268. dev->fsa_dev[container].valid = 2;
  1269. aac_probe_container(dev, container);
  1270. }
  1271. device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
  1272. if (device) {
  1273. switch (device_config_needed) {
  1274. case DELETE:
  1275. #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
  1276. scsi_remove_device(device);
  1277. #else
  1278. if (scsi_device_online(device)) {
  1279. scsi_device_set_state(device, SDEV_OFFLINE);
  1280. sdev_printk(KERN_INFO, device,
  1281. "Device offlined - %s\n",
  1282. (channel == CONTAINER_CHANNEL) ?
  1283. "array deleted" :
  1284. "enclosure services event");
  1285. }
  1286. #endif
  1287. break;
  1288. case ADD:
  1289. if (!scsi_device_online(device)) {
  1290. sdev_printk(KERN_INFO, device,
  1291. "Device online - %s\n",
  1292. (channel == CONTAINER_CHANNEL) ?
  1293. "array created" :
  1294. "enclosure services event");
  1295. scsi_device_set_state(device, SDEV_RUNNING);
  1296. }
  1297. fallthrough;
  1298. case CHANGE:
  1299. if ((channel == CONTAINER_CHANNEL)
  1300. && (!dev->fsa_dev[container].valid)) {
  1301. #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
  1302. scsi_remove_device(device);
  1303. #else
  1304. if (!scsi_device_online(device))
  1305. break;
  1306. scsi_device_set_state(device, SDEV_OFFLINE);
  1307. sdev_printk(KERN_INFO, device,
  1308. "Device offlined - %s\n",
  1309. "array failed");
  1310. #endif
  1311. break;
  1312. }
  1313. scsi_rescan_device(device);
  1314. break;
  1315. default:
  1316. break;
  1317. }
  1318. scsi_device_put(device);
  1319. device_config_needed = NOTHING;
  1320. }
  1321. if (device_config_needed == ADD)
  1322. scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
  1323. if (channel == CONTAINER_CHANNEL) {
  1324. container++;
  1325. device_config_needed = NOTHING;
  1326. goto retry_next;
  1327. }
  1328. }
  1329. static void aac_schedule_bus_scan(struct aac_dev *aac)
  1330. {
  1331. if (aac->sa_firmware)
  1332. aac_schedule_safw_scan_worker(aac);
  1333. else
  1334. aac_schedule_src_reinit_aif_worker(aac);
  1335. }
  1336. static int _aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type)
  1337. {
  1338. int index, quirks;
  1339. int retval;
  1340. struct Scsi_Host *host = aac->scsi_host_ptr;
  1341. int jafo = 0;
  1342. int bled;
  1343. u64 dmamask;
  1344. int num_of_fibs = 0;
  1345. /*
  1346. * Assumptions:
  1347. * - host is locked, unless called by the aacraid thread.
  1348. * (a matter of convenience, due to legacy issues surrounding
  1349. * eh_host_adapter_reset).
  1350. * - in_reset is asserted, so no new i/o is getting to the
  1351. * card.
  1352. * - The card is dead, or will be very shortly ;-/ so no new
  1353. * commands are completing in the interrupt service.
  1354. */
  1355. aac_adapter_disable_int(aac);
  1356. if (aac->thread && aac->thread->pid != current->pid) {
  1357. spin_unlock_irq(host->host_lock);
  1358. kthread_stop(aac->thread);
  1359. aac->thread = NULL;
  1360. jafo = 1;
  1361. }
  1362. /*
  1363. * If a positive health, means in a known DEAD PANIC
  1364. * state and the adapter could be reset to `try again'.
  1365. */
  1366. bled = forced ? 0 : aac_adapter_check_health(aac);
  1367. retval = aac_adapter_restart(aac, bled, reset_type);
  1368. if (retval)
  1369. goto out;
  1370. /*
  1371. * Loop through the fibs, close the synchronous FIBS
  1372. */
  1373. retval = 1;
  1374. num_of_fibs = aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB;
  1375. for (index = 0; index < num_of_fibs; index++) {
  1376. struct fib *fib = &aac->fibs[index];
  1377. __le32 XferState = fib->hw_fib_va->header.XferState;
  1378. bool is_response_expected = false;
  1379. if (!(XferState & cpu_to_le32(NoResponseExpected | Async)) &&
  1380. (XferState & cpu_to_le32(ResponseExpected)))
  1381. is_response_expected = true;
  1382. if (is_response_expected
  1383. || fib->flags & FIB_CONTEXT_FLAG_WAIT) {
  1384. unsigned long flagv;
  1385. spin_lock_irqsave(&fib->event_lock, flagv);
  1386. complete(&fib->event_wait);
  1387. spin_unlock_irqrestore(&fib->event_lock, flagv);
  1388. schedule();
  1389. retval = 0;
  1390. }
  1391. }
  1392. /* Give some extra time for ioctls to complete. */
  1393. if (retval == 0)
  1394. ssleep(2);
  1395. index = aac->cardtype;
  1396. /*
  1397. * Re-initialize the adapter, first free resources, then carefully
  1398. * apply the initialization sequence to come back again. Only risk
  1399. * is a change in Firmware dropping cache, it is assumed the caller
  1400. * will ensure that i/o is queisced and the card is flushed in that
  1401. * case.
  1402. */
  1403. aac_free_irq(aac);
  1404. aac_fib_map_free(aac);
  1405. dma_free_coherent(&aac->pdev->dev, aac->comm_size, aac->comm_addr,
  1406. aac->comm_phys);
  1407. aac_adapter_ioremap(aac, 0);
  1408. aac->comm_addr = NULL;
  1409. aac->comm_phys = 0;
  1410. kfree(aac->queues);
  1411. aac->queues = NULL;
  1412. kfree(aac->fsa_dev);
  1413. aac->fsa_dev = NULL;
  1414. dmamask = DMA_BIT_MASK(32);
  1415. quirks = aac_get_driver_ident(index)->quirks;
  1416. if (quirks & AAC_QUIRK_31BIT)
  1417. retval = dma_set_mask(&aac->pdev->dev, dmamask);
  1418. else if (!(quirks & AAC_QUIRK_SRC))
  1419. retval = dma_set_mask(&aac->pdev->dev, dmamask);
  1420. else
  1421. retval = dma_set_coherent_mask(&aac->pdev->dev, dmamask);
  1422. if (quirks & AAC_QUIRK_31BIT && !retval) {
  1423. dmamask = DMA_BIT_MASK(31);
  1424. retval = dma_set_coherent_mask(&aac->pdev->dev, dmamask);
  1425. }
  1426. if (retval)
  1427. goto out;
  1428. if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
  1429. goto out;
  1430. if (jafo) {
  1431. aac->thread = kthread_run(aac_command_thread, aac, "%s",
  1432. aac->name);
  1433. if (IS_ERR(aac->thread)) {
  1434. retval = PTR_ERR(aac->thread);
  1435. aac->thread = NULL;
  1436. goto out;
  1437. }
  1438. }
  1439. (void)aac_get_adapter_info(aac);
  1440. if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
  1441. host->sg_tablesize = 34;
  1442. host->max_sectors = (host->sg_tablesize * 8) + 112;
  1443. }
  1444. if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
  1445. host->sg_tablesize = 17;
  1446. host->max_sectors = (host->sg_tablesize * 8) + 112;
  1447. }
  1448. aac_get_config_status(aac, 1);
  1449. aac_get_containers(aac);
  1450. /*
  1451. * This is where the assumption that the Adapter is quiesced
  1452. * is important.
  1453. */
  1454. scsi_host_complete_all_commands(host, DID_RESET);
  1455. retval = 0;
  1456. out:
  1457. aac->in_reset = 0;
  1458. /*
  1459. * Issue bus rescan to catch any configuration that might have
  1460. * occurred
  1461. */
  1462. if (!retval && !is_kdump_kernel()) {
  1463. dev_info(&aac->pdev->dev, "Scheduling bus rescan\n");
  1464. aac_schedule_bus_scan(aac);
  1465. }
  1466. if (jafo) {
  1467. spin_lock_irq(host->host_lock);
  1468. }
  1469. return retval;
  1470. }
  1471. int aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type)
  1472. {
  1473. unsigned long flagv = 0;
  1474. int retval, unblock_retval;
  1475. struct Scsi_Host *host = aac->scsi_host_ptr;
  1476. int bled;
  1477. if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
  1478. return -EBUSY;
  1479. if (aac->in_reset) {
  1480. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1481. return -EBUSY;
  1482. }
  1483. aac->in_reset = 1;
  1484. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1485. /*
  1486. * Wait for all commands to complete to this specific
  1487. * target (block maximum 60 seconds). Although not necessary,
  1488. * it does make us a good storage citizen.
  1489. */
  1490. scsi_host_block(host);
  1491. /* Quiesce build, flush cache, write through mode */
  1492. if (forced < 2)
  1493. aac_send_shutdown(aac);
  1494. spin_lock_irqsave(host->host_lock, flagv);
  1495. bled = forced ? forced :
  1496. (aac_check_reset != 0 && aac_check_reset != 1);
  1497. retval = _aac_reset_adapter(aac, bled, reset_type);
  1498. spin_unlock_irqrestore(host->host_lock, flagv);
  1499. unblock_retval = scsi_host_unblock(host, SDEV_RUNNING);
  1500. if (!retval)
  1501. retval = unblock_retval;
  1502. if ((forced < 2) && (retval == -ENODEV)) {
  1503. /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
  1504. struct fib * fibctx = aac_fib_alloc(aac);
  1505. if (fibctx) {
  1506. struct aac_pause *cmd;
  1507. int status;
  1508. aac_fib_init(fibctx);
  1509. cmd = (struct aac_pause *) fib_data(fibctx);
  1510. cmd->command = cpu_to_le32(VM_ContainerConfig);
  1511. cmd->type = cpu_to_le32(CT_PAUSE_IO);
  1512. cmd->timeout = cpu_to_le32(1);
  1513. cmd->min = cpu_to_le32(1);
  1514. cmd->noRescan = cpu_to_le32(1);
  1515. cmd->count = cpu_to_le32(0);
  1516. status = aac_fib_send(ContainerCommand,
  1517. fibctx,
  1518. sizeof(struct aac_pause),
  1519. FsaNormal,
  1520. -2 /* Timeout silently */, 1,
  1521. NULL, NULL);
  1522. if (status >= 0)
  1523. aac_fib_complete(fibctx);
  1524. /* FIB should be freed only after getting
  1525. * the response from the F/W */
  1526. if (status != -ERESTARTSYS)
  1527. aac_fib_free(fibctx);
  1528. }
  1529. }
  1530. return retval;
  1531. }
  1532. int aac_check_health(struct aac_dev * aac)
  1533. {
  1534. int BlinkLED;
  1535. unsigned long time_now, flagv = 0;
  1536. struct list_head * entry;
  1537. /* Extending the scope of fib_lock slightly to protect aac->in_reset */
  1538. if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
  1539. return 0;
  1540. if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
  1541. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1542. return 0; /* OK */
  1543. }
  1544. aac->in_reset = 1;
  1545. /* Fake up an AIF:
  1546. * aac_aifcmd.command = AifCmdEventNotify = 1
  1547. * aac_aifcmd.seqnum = 0xFFFFFFFF
  1548. * aac_aifcmd.data[0] = AifEnExpEvent = 23
  1549. * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
  1550. * aac.aifcmd.data[2] = AifHighPriority = 3
  1551. * aac.aifcmd.data[3] = BlinkLED
  1552. */
  1553. time_now = jiffies/HZ;
  1554. entry = aac->fib_list.next;
  1555. /*
  1556. * For each Context that is on the
  1557. * fibctxList, make a copy of the
  1558. * fib, and then set the event to wake up the
  1559. * thread that is waiting for it.
  1560. */
  1561. while (entry != &aac->fib_list) {
  1562. /*
  1563. * Extract the fibctx
  1564. */
  1565. struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
  1566. struct hw_fib * hw_fib;
  1567. struct fib * fib;
  1568. /*
  1569. * Check if the queue is getting
  1570. * backlogged
  1571. */
  1572. if (fibctx->count > 20) {
  1573. /*
  1574. * It's *not* jiffies folks,
  1575. * but jiffies / HZ, so do not
  1576. * panic ...
  1577. */
  1578. u32 time_last = fibctx->jiffies;
  1579. /*
  1580. * Has it been > 2 minutes
  1581. * since the last read off
  1582. * the queue?
  1583. */
  1584. if ((time_now - time_last) > aif_timeout) {
  1585. entry = entry->next;
  1586. aac_close_fib_context(aac, fibctx);
  1587. continue;
  1588. }
  1589. }
  1590. /*
  1591. * Warning: no sleep allowed while
  1592. * holding spinlock
  1593. */
  1594. hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
  1595. fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
  1596. if (fib && hw_fib) {
  1597. struct aac_aifcmd * aif;
  1598. fib->hw_fib_va = hw_fib;
  1599. fib->dev = aac;
  1600. aac_fib_init(fib);
  1601. fib->type = FSAFS_NTC_FIB_CONTEXT;
  1602. fib->size = sizeof (struct fib);
  1603. fib->data = hw_fib->data;
  1604. aif = (struct aac_aifcmd *)hw_fib->data;
  1605. aif->command = cpu_to_le32(AifCmdEventNotify);
  1606. aif->seqnum = cpu_to_le32(0xFFFFFFFF);
  1607. ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
  1608. ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
  1609. ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
  1610. ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
  1611. /*
  1612. * Put the FIB onto the
  1613. * fibctx's fibs
  1614. */
  1615. list_add_tail(&fib->fiblink, &fibctx->fib_list);
  1616. fibctx->count++;
  1617. /*
  1618. * Set the event to wake up the
  1619. * thread that will waiting.
  1620. */
  1621. complete(&fibctx->completion);
  1622. } else {
  1623. printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
  1624. kfree(fib);
  1625. kfree(hw_fib);
  1626. }
  1627. entry = entry->next;
  1628. }
  1629. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1630. if (BlinkLED < 0) {
  1631. printk(KERN_ERR "%s: Host adapter is dead (or got a PCI error) %d\n",
  1632. aac->name, BlinkLED);
  1633. goto out;
  1634. }
  1635. printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
  1636. out:
  1637. aac->in_reset = 0;
  1638. return BlinkLED;
  1639. }
  1640. static inline int is_safw_raid_volume(struct aac_dev *aac, int bus, int target)
  1641. {
  1642. return bus == CONTAINER_CHANNEL && target < aac->maximum_num_containers;
  1643. }
  1644. static struct scsi_device *aac_lookup_safw_scsi_device(struct aac_dev *dev,
  1645. int bus,
  1646. int target)
  1647. {
  1648. if (bus != CONTAINER_CHANNEL)
  1649. bus = aac_phys_to_logical(bus);
  1650. return scsi_device_lookup(dev->scsi_host_ptr, bus, target, 0);
  1651. }
  1652. static int aac_add_safw_device(struct aac_dev *dev, int bus, int target)
  1653. {
  1654. if (bus != CONTAINER_CHANNEL)
  1655. bus = aac_phys_to_logical(bus);
  1656. return scsi_add_device(dev->scsi_host_ptr, bus, target, 0);
  1657. }
  1658. static void aac_put_safw_scsi_device(struct scsi_device *sdev)
  1659. {
  1660. if (sdev)
  1661. scsi_device_put(sdev);
  1662. }
  1663. static void aac_remove_safw_device(struct aac_dev *dev, int bus, int target)
  1664. {
  1665. struct scsi_device *sdev;
  1666. sdev = aac_lookup_safw_scsi_device(dev, bus, target);
  1667. scsi_remove_device(sdev);
  1668. aac_put_safw_scsi_device(sdev);
  1669. }
  1670. static inline int aac_is_safw_scan_count_equal(struct aac_dev *dev,
  1671. int bus, int target)
  1672. {
  1673. return dev->hba_map[bus][target].scan_counter == dev->scan_counter;
  1674. }
  1675. static int aac_is_safw_target_valid(struct aac_dev *dev, int bus, int target)
  1676. {
  1677. if (is_safw_raid_volume(dev, bus, target))
  1678. return dev->fsa_dev[target].valid;
  1679. else
  1680. return aac_is_safw_scan_count_equal(dev, bus, target);
  1681. }
  1682. static int aac_is_safw_device_exposed(struct aac_dev *dev, int bus, int target)
  1683. {
  1684. int is_exposed = 0;
  1685. struct scsi_device *sdev;
  1686. sdev = aac_lookup_safw_scsi_device(dev, bus, target);
  1687. if (sdev)
  1688. is_exposed = 1;
  1689. aac_put_safw_scsi_device(sdev);
  1690. return is_exposed;
  1691. }
  1692. static int aac_update_safw_host_devices(struct aac_dev *dev)
  1693. {
  1694. int i;
  1695. int bus;
  1696. int target;
  1697. int is_exposed = 0;
  1698. int rcode = 0;
  1699. rcode = aac_setup_safw_adapter(dev);
  1700. if (unlikely(rcode < 0)) {
  1701. goto out;
  1702. }
  1703. for (i = 0; i < AAC_BUS_TARGET_LOOP; i++) {
  1704. bus = get_bus_number(i);
  1705. target = get_target_number(i);
  1706. is_exposed = aac_is_safw_device_exposed(dev, bus, target);
  1707. if (aac_is_safw_target_valid(dev, bus, target) && !is_exposed)
  1708. aac_add_safw_device(dev, bus, target);
  1709. else if (!aac_is_safw_target_valid(dev, bus, target) &&
  1710. is_exposed)
  1711. aac_remove_safw_device(dev, bus, target);
  1712. }
  1713. out:
  1714. return rcode;
  1715. }
  1716. static int aac_scan_safw_host(struct aac_dev *dev)
  1717. {
  1718. int rcode = 0;
  1719. rcode = aac_update_safw_host_devices(dev);
  1720. if (rcode)
  1721. aac_schedule_safw_scan_worker(dev);
  1722. return rcode;
  1723. }
  1724. int aac_scan_host(struct aac_dev *dev)
  1725. {
  1726. int rcode = 0;
  1727. mutex_lock(&dev->scan_mutex);
  1728. if (dev->sa_firmware)
  1729. rcode = aac_scan_safw_host(dev);
  1730. else
  1731. scsi_scan_host(dev->scsi_host_ptr);
  1732. mutex_unlock(&dev->scan_mutex);
  1733. return rcode;
  1734. }
  1735. void aac_src_reinit_aif_worker(struct work_struct *work)
  1736. {
  1737. struct aac_dev *dev = container_of(to_delayed_work(work),
  1738. struct aac_dev, src_reinit_aif_worker);
  1739. wait_event(dev->scsi_host_ptr->host_wait,
  1740. !scsi_host_in_recovery(dev->scsi_host_ptr));
  1741. aac_reinit_aif(dev, dev->cardtype);
  1742. }
  1743. /**
  1744. * aac_handle_sa_aif - Handle a message from the firmware
  1745. * @dev: Which adapter this fib is from
  1746. * @fibptr: Pointer to fibptr from adapter
  1747. *
  1748. * This routine handles a driver notify fib from the adapter and
  1749. * dispatches it to the appropriate routine for handling.
  1750. */
  1751. static void aac_handle_sa_aif(struct aac_dev *dev, struct fib *fibptr)
  1752. {
  1753. int i;
  1754. u32 events = 0;
  1755. if (fibptr->hbacmd_size & SA_AIF_HOTPLUG)
  1756. events = SA_AIF_HOTPLUG;
  1757. else if (fibptr->hbacmd_size & SA_AIF_HARDWARE)
  1758. events = SA_AIF_HARDWARE;
  1759. else if (fibptr->hbacmd_size & SA_AIF_PDEV_CHANGE)
  1760. events = SA_AIF_PDEV_CHANGE;
  1761. else if (fibptr->hbacmd_size & SA_AIF_LDEV_CHANGE)
  1762. events = SA_AIF_LDEV_CHANGE;
  1763. else if (fibptr->hbacmd_size & SA_AIF_BPSTAT_CHANGE)
  1764. events = SA_AIF_BPSTAT_CHANGE;
  1765. else if (fibptr->hbacmd_size & SA_AIF_BPCFG_CHANGE)
  1766. events = SA_AIF_BPCFG_CHANGE;
  1767. switch (events) {
  1768. case SA_AIF_HOTPLUG:
  1769. case SA_AIF_HARDWARE:
  1770. case SA_AIF_PDEV_CHANGE:
  1771. case SA_AIF_LDEV_CHANGE:
  1772. case SA_AIF_BPCFG_CHANGE:
  1773. aac_scan_host(dev);
  1774. break;
  1775. case SA_AIF_BPSTAT_CHANGE:
  1776. /* currently do nothing */
  1777. break;
  1778. }
  1779. for (i = 1; i <= 10; ++i) {
  1780. events = src_readl(dev, MUnit.IDR);
  1781. if (events & (1<<23)) {
  1782. pr_warn(" AIF not cleared by firmware - %d/%d)\n",
  1783. i, 10);
  1784. ssleep(1);
  1785. }
  1786. }
  1787. }
  1788. static int get_fib_count(struct aac_dev *dev)
  1789. {
  1790. unsigned int num = 0;
  1791. struct list_head *entry;
  1792. unsigned long flagv;
  1793. /*
  1794. * Warning: no sleep allowed while
  1795. * holding spinlock. We take the estimate
  1796. * and pre-allocate a set of fibs outside the
  1797. * lock.
  1798. */
  1799. num = le32_to_cpu(dev->init->r7.adapter_fibs_size)
  1800. / sizeof(struct hw_fib); /* some extra */
  1801. spin_lock_irqsave(&dev->fib_lock, flagv);
  1802. entry = dev->fib_list.next;
  1803. while (entry != &dev->fib_list) {
  1804. entry = entry->next;
  1805. ++num;
  1806. }
  1807. spin_unlock_irqrestore(&dev->fib_lock, flagv);
  1808. return num;
  1809. }
  1810. static int fillup_pools(struct aac_dev *dev, struct hw_fib **hw_fib_pool,
  1811. struct fib **fib_pool,
  1812. unsigned int num)
  1813. {
  1814. struct hw_fib **hw_fib_p;
  1815. struct fib **fib_p;
  1816. hw_fib_p = hw_fib_pool;
  1817. fib_p = fib_pool;
  1818. while (hw_fib_p < &hw_fib_pool[num]) {
  1819. *(hw_fib_p) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL);
  1820. if (!(*(hw_fib_p++))) {
  1821. --hw_fib_p;
  1822. break;
  1823. }
  1824. *(fib_p) = kmalloc(sizeof(struct fib), GFP_KERNEL);
  1825. if (!(*(fib_p++))) {
  1826. kfree(*(--hw_fib_p));
  1827. break;
  1828. }
  1829. }
  1830. /*
  1831. * Get the actual number of allocated fibs
  1832. */
  1833. num = hw_fib_p - hw_fib_pool;
  1834. return num;
  1835. }
  1836. static void wakeup_fibctx_threads(struct aac_dev *dev,
  1837. struct hw_fib **hw_fib_pool,
  1838. struct fib **fib_pool,
  1839. struct fib *fib,
  1840. struct hw_fib *hw_fib,
  1841. unsigned int num)
  1842. {
  1843. unsigned long flagv;
  1844. struct list_head *entry;
  1845. struct hw_fib **hw_fib_p;
  1846. struct fib **fib_p;
  1847. u32 time_now, time_last;
  1848. struct hw_fib *hw_newfib;
  1849. struct fib *newfib;
  1850. struct aac_fib_context *fibctx;
  1851. time_now = jiffies/HZ;
  1852. spin_lock_irqsave(&dev->fib_lock, flagv);
  1853. entry = dev->fib_list.next;
  1854. /*
  1855. * For each Context that is on the
  1856. * fibctxList, make a copy of the
  1857. * fib, and then set the event to wake up the
  1858. * thread that is waiting for it.
  1859. */
  1860. hw_fib_p = hw_fib_pool;
  1861. fib_p = fib_pool;
  1862. while (entry != &dev->fib_list) {
  1863. /*
  1864. * Extract the fibctx
  1865. */
  1866. fibctx = list_entry(entry, struct aac_fib_context,
  1867. next);
  1868. /*
  1869. * Check if the queue is getting
  1870. * backlogged
  1871. */
  1872. if (fibctx->count > 20) {
  1873. /*
  1874. * It's *not* jiffies folks,
  1875. * but jiffies / HZ so do not
  1876. * panic ...
  1877. */
  1878. time_last = fibctx->jiffies;
  1879. /*
  1880. * Has it been > 2 minutes
  1881. * since the last read off
  1882. * the queue?
  1883. */
  1884. if ((time_now - time_last) > aif_timeout) {
  1885. entry = entry->next;
  1886. aac_close_fib_context(dev, fibctx);
  1887. continue;
  1888. }
  1889. }
  1890. /*
  1891. * Warning: no sleep allowed while
  1892. * holding spinlock
  1893. */
  1894. if (hw_fib_p >= &hw_fib_pool[num]) {
  1895. pr_warn("aifd: didn't allocate NewFib\n");
  1896. entry = entry->next;
  1897. continue;
  1898. }
  1899. hw_newfib = *hw_fib_p;
  1900. *(hw_fib_p++) = NULL;
  1901. newfib = *fib_p;
  1902. *(fib_p++) = NULL;
  1903. /*
  1904. * Make the copy of the FIB
  1905. */
  1906. memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
  1907. memcpy(newfib, fib, sizeof(struct fib));
  1908. newfib->hw_fib_va = hw_newfib;
  1909. /*
  1910. * Put the FIB onto the
  1911. * fibctx's fibs
  1912. */
  1913. list_add_tail(&newfib->fiblink, &fibctx->fib_list);
  1914. fibctx->count++;
  1915. /*
  1916. * Set the event to wake up the
  1917. * thread that is waiting.
  1918. */
  1919. complete(&fibctx->completion);
  1920. entry = entry->next;
  1921. }
  1922. /*
  1923. * Set the status of this FIB
  1924. */
  1925. *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
  1926. aac_fib_adapter_complete(fib, sizeof(u32));
  1927. spin_unlock_irqrestore(&dev->fib_lock, flagv);
  1928. }
  1929. static void aac_process_events(struct aac_dev *dev)
  1930. {
  1931. struct hw_fib *hw_fib;
  1932. struct fib *fib;
  1933. unsigned long flags;
  1934. spinlock_t *t_lock;
  1935. t_lock = dev->queues->queue[HostNormCmdQueue].lock;
  1936. spin_lock_irqsave(t_lock, flags);
  1937. while (!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
  1938. struct list_head *entry;
  1939. struct aac_aifcmd *aifcmd;
  1940. unsigned int num;
  1941. struct hw_fib **hw_fib_pool, **hw_fib_p;
  1942. struct fib **fib_pool, **fib_p;
  1943. set_current_state(TASK_RUNNING);
  1944. entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
  1945. list_del(entry);
  1946. t_lock = dev->queues->queue[HostNormCmdQueue].lock;
  1947. spin_unlock_irqrestore(t_lock, flags);
  1948. fib = list_entry(entry, struct fib, fiblink);
  1949. hw_fib = fib->hw_fib_va;
  1950. if (dev->sa_firmware) {
  1951. /* Thor AIF */
  1952. aac_handle_sa_aif(dev, fib);
  1953. aac_fib_adapter_complete(fib, (u16)sizeof(u32));
  1954. goto free_fib;
  1955. }
  1956. /*
  1957. * We will process the FIB here or pass it to a
  1958. * worker thread that is TBD. We Really can't
  1959. * do anything at this point since we don't have
  1960. * anything defined for this thread to do.
  1961. */
  1962. memset(fib, 0, sizeof(struct fib));
  1963. fib->type = FSAFS_NTC_FIB_CONTEXT;
  1964. fib->size = sizeof(struct fib);
  1965. fib->hw_fib_va = hw_fib;
  1966. fib->data = hw_fib->data;
  1967. fib->dev = dev;
  1968. /*
  1969. * We only handle AifRequest fibs from the adapter.
  1970. */
  1971. aifcmd = (struct aac_aifcmd *) hw_fib->data;
  1972. if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
  1973. /* Handle Driver Notify Events */
  1974. aac_handle_aif(dev, fib);
  1975. *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
  1976. aac_fib_adapter_complete(fib, (u16)sizeof(u32));
  1977. goto free_fib;
  1978. }
  1979. /*
  1980. * The u32 here is important and intended. We are using
  1981. * 32bit wrapping time to fit the adapter field
  1982. */
  1983. /* Sniff events */
  1984. if (aifcmd->command == cpu_to_le32(AifCmdEventNotify)
  1985. || aifcmd->command == cpu_to_le32(AifCmdJobProgress)) {
  1986. aac_handle_aif(dev, fib);
  1987. }
  1988. /*
  1989. * get number of fibs to process
  1990. */
  1991. num = get_fib_count(dev);
  1992. if (!num)
  1993. goto free_fib;
  1994. hw_fib_pool = kmalloc_array(num, sizeof(struct hw_fib *),
  1995. GFP_KERNEL);
  1996. if (!hw_fib_pool)
  1997. goto free_fib;
  1998. fib_pool = kmalloc_array(num, sizeof(struct fib *), GFP_KERNEL);
  1999. if (!fib_pool)
  2000. goto free_hw_fib_pool;
  2001. /*
  2002. * Fill up fib pointer pools with actual fibs
  2003. * and hw_fibs
  2004. */
  2005. num = fillup_pools(dev, hw_fib_pool, fib_pool, num);
  2006. if (!num)
  2007. goto free_mem;
  2008. /*
  2009. * wakeup the thread that is waiting for
  2010. * the response from fw (ioctl)
  2011. */
  2012. wakeup_fibctx_threads(dev, hw_fib_pool, fib_pool,
  2013. fib, hw_fib, num);
  2014. free_mem:
  2015. /* Free up the remaining resources */
  2016. hw_fib_p = hw_fib_pool;
  2017. fib_p = fib_pool;
  2018. while (hw_fib_p < &hw_fib_pool[num]) {
  2019. kfree(*hw_fib_p);
  2020. kfree(*fib_p);
  2021. ++fib_p;
  2022. ++hw_fib_p;
  2023. }
  2024. kfree(fib_pool);
  2025. free_hw_fib_pool:
  2026. kfree(hw_fib_pool);
  2027. free_fib:
  2028. kfree(fib);
  2029. t_lock = dev->queues->queue[HostNormCmdQueue].lock;
  2030. spin_lock_irqsave(t_lock, flags);
  2031. }
  2032. /*
  2033. * There are no more AIF's
  2034. */
  2035. t_lock = dev->queues->queue[HostNormCmdQueue].lock;
  2036. spin_unlock_irqrestore(t_lock, flags);
  2037. }
  2038. static int aac_send_wellness_command(struct aac_dev *dev, char *wellness_str,
  2039. u32 datasize)
  2040. {
  2041. struct aac_srb *srbcmd;
  2042. struct sgmap64 *sg64;
  2043. dma_addr_t addr;
  2044. char *dma_buf;
  2045. struct fib *fibptr;
  2046. int ret = -ENOMEM;
  2047. u32 vbus, vid;
  2048. fibptr = aac_fib_alloc(dev);
  2049. if (!fibptr)
  2050. goto out;
  2051. dma_buf = dma_alloc_coherent(&dev->pdev->dev, datasize, &addr,
  2052. GFP_KERNEL);
  2053. if (!dma_buf)
  2054. goto fib_free_out;
  2055. aac_fib_init(fibptr);
  2056. vbus = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_bus);
  2057. vid = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_target);
  2058. srbcmd = (struct aac_srb *)fib_data(fibptr);
  2059. srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
  2060. srbcmd->channel = cpu_to_le32(vbus);
  2061. srbcmd->id = cpu_to_le32(vid);
  2062. srbcmd->lun = 0;
  2063. srbcmd->flags = cpu_to_le32(SRB_DataOut);
  2064. srbcmd->timeout = cpu_to_le32(10);
  2065. srbcmd->retry_limit = 0;
  2066. srbcmd->cdb_size = cpu_to_le32(12);
  2067. srbcmd->count = cpu_to_le32(datasize);
  2068. memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
  2069. srbcmd->cdb[0] = BMIC_OUT;
  2070. srbcmd->cdb[6] = WRITE_HOST_WELLNESS;
  2071. memcpy(dma_buf, (char *)wellness_str, datasize);
  2072. sg64 = (struct sgmap64 *)&srbcmd->sg;
  2073. sg64->count = cpu_to_le32(1);
  2074. sg64->sg[0].addr[1] = cpu_to_le32((u32)(((addr) >> 16) >> 16));
  2075. sg64->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
  2076. sg64->sg[0].count = cpu_to_le32(datasize);
  2077. ret = aac_fib_send(ScsiPortCommand64, fibptr,
  2078. sizeof(struct aac_srb) + sizeof(struct sgentry),
  2079. FsaNormal, 1, 1, NULL, NULL);
  2080. dma_free_coherent(&dev->pdev->dev, datasize, dma_buf, addr);
  2081. /*
  2082. * Do not set XferState to zero unless
  2083. * receives a response from F/W
  2084. */
  2085. if (ret >= 0)
  2086. aac_fib_complete(fibptr);
  2087. /*
  2088. * FIB should be freed only after
  2089. * getting the response from the F/W
  2090. */
  2091. if (ret != -ERESTARTSYS)
  2092. goto fib_free_out;
  2093. out:
  2094. return ret;
  2095. fib_free_out:
  2096. aac_fib_free(fibptr);
  2097. goto out;
  2098. }
  2099. static int aac_send_safw_hostttime(struct aac_dev *dev, struct timespec64 *now)
  2100. {
  2101. struct tm cur_tm;
  2102. char wellness_str[] = "<HW>TD\010\0\0\0\0\0\0\0\0\0DW\0\0ZZ";
  2103. u32 datasize = sizeof(wellness_str);
  2104. time64_t local_time;
  2105. int ret = -ENODEV;
  2106. if (!dev->sa_firmware)
  2107. goto out;
  2108. local_time = (now->tv_sec - (sys_tz.tz_minuteswest * 60));
  2109. time64_to_tm(local_time, 0, &cur_tm);
  2110. cur_tm.tm_mon += 1;
  2111. cur_tm.tm_year += 1900;
  2112. wellness_str[8] = bin2bcd(cur_tm.tm_hour);
  2113. wellness_str[9] = bin2bcd(cur_tm.tm_min);
  2114. wellness_str[10] = bin2bcd(cur_tm.tm_sec);
  2115. wellness_str[12] = bin2bcd(cur_tm.tm_mon);
  2116. wellness_str[13] = bin2bcd(cur_tm.tm_mday);
  2117. wellness_str[14] = bin2bcd(cur_tm.tm_year / 100);
  2118. wellness_str[15] = bin2bcd(cur_tm.tm_year % 100);
  2119. ret = aac_send_wellness_command(dev, wellness_str, datasize);
  2120. out:
  2121. return ret;
  2122. }
  2123. static int aac_send_hosttime(struct aac_dev *dev, struct timespec64 *now)
  2124. {
  2125. int ret = -ENOMEM;
  2126. struct fib *fibptr;
  2127. __le32 *info;
  2128. fibptr = aac_fib_alloc(dev);
  2129. if (!fibptr)
  2130. goto out;
  2131. aac_fib_init(fibptr);
  2132. info = (__le32 *)fib_data(fibptr);
  2133. *info = cpu_to_le32(now->tv_sec); /* overflow in y2106 */
  2134. ret = aac_fib_send(SendHostTime, fibptr, sizeof(*info), FsaNormal,
  2135. 1, 1, NULL, NULL);
  2136. /*
  2137. * Do not set XferState to zero unless
  2138. * receives a response from F/W
  2139. */
  2140. if (ret >= 0)
  2141. aac_fib_complete(fibptr);
  2142. /*
  2143. * FIB should be freed only after
  2144. * getting the response from the F/W
  2145. */
  2146. if (ret != -ERESTARTSYS)
  2147. aac_fib_free(fibptr);
  2148. out:
  2149. return ret;
  2150. }
  2151. /**
  2152. * aac_command_thread - command processing thread
  2153. * @data: Adapter to monitor
  2154. *
  2155. * Waits on the commandready event in it's queue. When the event gets set
  2156. * it will pull FIBs off it's queue. It will continue to pull FIBs off
  2157. * until the queue is empty. When the queue is empty it will wait for
  2158. * more FIBs.
  2159. */
  2160. int aac_command_thread(void *data)
  2161. {
  2162. struct aac_dev *dev = data;
  2163. DECLARE_WAITQUEUE(wait, current);
  2164. unsigned long next_jiffies = jiffies + HZ;
  2165. unsigned long next_check_jiffies = next_jiffies;
  2166. long difference = HZ;
  2167. /*
  2168. * We can only have one thread per adapter for AIF's.
  2169. */
  2170. if (dev->aif_thread)
  2171. return -EINVAL;
  2172. /*
  2173. * Let the DPC know it has a place to send the AIF's to.
  2174. */
  2175. dev->aif_thread = 1;
  2176. add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
  2177. set_current_state(TASK_INTERRUPTIBLE);
  2178. dprintk ((KERN_INFO "aac_command_thread start\n"));
  2179. while (1) {
  2180. aac_process_events(dev);
  2181. /*
  2182. * Background activity
  2183. */
  2184. if ((time_before(next_check_jiffies,next_jiffies))
  2185. && ((difference = next_check_jiffies - jiffies) <= 0)) {
  2186. next_check_jiffies = next_jiffies;
  2187. if (aac_adapter_check_health(dev) == 0) {
  2188. difference = ((long)(unsigned)check_interval)
  2189. * HZ;
  2190. next_check_jiffies = jiffies + difference;
  2191. } else if (!dev->queues)
  2192. break;
  2193. }
  2194. if (!time_before(next_check_jiffies,next_jiffies)
  2195. && ((difference = next_jiffies - jiffies) <= 0)) {
  2196. struct timespec64 now;
  2197. int ret;
  2198. /* Don't even try to talk to adapter if its sick */
  2199. ret = aac_adapter_check_health(dev);
  2200. if (ret || !dev->queues)
  2201. break;
  2202. next_check_jiffies = jiffies
  2203. + ((long)(unsigned)check_interval)
  2204. * HZ;
  2205. ktime_get_real_ts64(&now);
  2206. /* Synchronize our watches */
  2207. if (((NSEC_PER_SEC - (NSEC_PER_SEC / HZ)) > now.tv_nsec)
  2208. && (now.tv_nsec > (NSEC_PER_SEC / HZ)))
  2209. difference = HZ + HZ / 2 -
  2210. now.tv_nsec / (NSEC_PER_SEC / HZ);
  2211. else {
  2212. if (now.tv_nsec > NSEC_PER_SEC / 2)
  2213. ++now.tv_sec;
  2214. if (dev->sa_firmware)
  2215. ret =
  2216. aac_send_safw_hostttime(dev, &now);
  2217. else
  2218. ret = aac_send_hosttime(dev, &now);
  2219. difference = (long)(unsigned)update_interval*HZ;
  2220. }
  2221. next_jiffies = jiffies + difference;
  2222. if (time_before(next_check_jiffies,next_jiffies))
  2223. difference = next_check_jiffies - jiffies;
  2224. }
  2225. if (difference <= 0)
  2226. difference = 1;
  2227. set_current_state(TASK_INTERRUPTIBLE);
  2228. if (kthread_should_stop())
  2229. break;
  2230. /*
  2231. * we probably want usleep_range() here instead of the
  2232. * jiffies computation
  2233. */
  2234. schedule_timeout(difference);
  2235. if (kthread_should_stop())
  2236. break;
  2237. }
  2238. if (dev->queues)
  2239. remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
  2240. dev->aif_thread = 0;
  2241. return 0;
  2242. }
  2243. int aac_acquire_irq(struct aac_dev *dev)
  2244. {
  2245. int i;
  2246. int j;
  2247. int ret = 0;
  2248. if (!dev->sync_mode && dev->msi_enabled && dev->max_msix > 1) {
  2249. for (i = 0; i < dev->max_msix; i++) {
  2250. dev->aac_msix[i].vector_no = i;
  2251. dev->aac_msix[i].dev = dev;
  2252. if (request_irq(pci_irq_vector(dev->pdev, i),
  2253. dev->a_ops.adapter_intr,
  2254. 0, "aacraid", &(dev->aac_msix[i]))) {
  2255. printk(KERN_ERR "%s%d: Failed to register IRQ for vector %d.\n",
  2256. dev->name, dev->id, i);
  2257. for (j = 0 ; j < i ; j++)
  2258. free_irq(pci_irq_vector(dev->pdev, j),
  2259. &(dev->aac_msix[j]));
  2260. pci_disable_msix(dev->pdev);
  2261. ret = -1;
  2262. }
  2263. }
  2264. } else {
  2265. dev->aac_msix[0].vector_no = 0;
  2266. dev->aac_msix[0].dev = dev;
  2267. if (request_irq(dev->pdev->irq, dev->a_ops.adapter_intr,
  2268. IRQF_SHARED, "aacraid",
  2269. &(dev->aac_msix[0])) < 0) {
  2270. if (dev->msi)
  2271. pci_disable_msi(dev->pdev);
  2272. printk(KERN_ERR "%s%d: Interrupt unavailable.\n",
  2273. dev->name, dev->id);
  2274. ret = -1;
  2275. }
  2276. }
  2277. return ret;
  2278. }
  2279. void aac_free_irq(struct aac_dev *dev)
  2280. {
  2281. int i;
  2282. if (aac_is_src(dev)) {
  2283. if (dev->max_msix > 1) {
  2284. for (i = 0; i < dev->max_msix; i++)
  2285. free_irq(pci_irq_vector(dev->pdev, i),
  2286. &(dev->aac_msix[i]));
  2287. } else {
  2288. free_irq(dev->pdev->irq, &(dev->aac_msix[0]));
  2289. }
  2290. } else {
  2291. free_irq(dev->pdev->irq, dev);
  2292. }
  2293. if (dev->msi)
  2294. pci_disable_msi(dev->pdev);
  2295. else if (dev->max_msix > 1)
  2296. pci_disable_msix(dev->pdev);
  2297. }