mpt2sas_base.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524
  1. /*
  2. * This is the Fusion MPT base driver providing common API layer interface
  3. * for access to MPT (Message Passing Technology) firmware.
  4. *
  5. * This code is based on drivers/scsi/mpt2sas/mpt2_base.c
  6. * Copyright (C) 2007-2008 LSI Corporation
  7. * (mailto:DL-MPTFusionLinux@lsi.com)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * NO WARRANTY
  20. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24. * solely responsible for determining the appropriateness of using and
  25. * distributing the Program and assumes all risks associated with its
  26. * exercise of rights under this Agreement, including but not limited to
  27. * the risks and costs of program errors, damage to or loss of data,
  28. * programs or equipment, and unavailability or interruption of operations.
  29. * DISCLAIMER OF LIABILITY
  30. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. * You should have received a copy of the GNU General Public License
  38. * along with this program; if not, write to the Free Software
  39. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  40. * USA.
  41. */
  42. #include <linux/version.h>
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/errno.h>
  46. #include <linux/init.h>
  47. #include <linux/slab.h>
  48. #include <linux/types.h>
  49. #include <linux/pci.h>
  50. #include <linux/kdev_t.h>
  51. #include <linux/blkdev.h>
  52. #include <linux/delay.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/dma-mapping.h>
  55. #include <linux/sort.h>
  56. #include <linux/io.h>
  57. #include "mpt2sas_base.h"
  58. static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
  59. #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
  60. #define MPT2SAS_MAX_REQUEST_QUEUE 500 /* maximum controller queue depth */
  61. static int max_queue_depth = -1;
  62. module_param(max_queue_depth, int, 0);
  63. MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
  64. static int max_sgl_entries = -1;
  65. module_param(max_sgl_entries, int, 0);
  66. MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
  67. static int msix_disable = -1;
  68. module_param(msix_disable, int, 0);
  69. MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
  70. /**
  71. * _base_fault_reset_work - workq handling ioc fault conditions
  72. * @work: input argument, used to derive ioc
  73. * Context: sleep.
  74. *
  75. * Return nothing.
  76. */
  77. static void
  78. _base_fault_reset_work(struct work_struct *work)
  79. {
  80. struct MPT2SAS_ADAPTER *ioc =
  81. container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
  82. unsigned long flags;
  83. u32 doorbell;
  84. int rc;
  85. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  86. if (ioc->shost_recovery)
  87. goto rearm_timer;
  88. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  89. doorbell = mpt2sas_base_get_iocstate(ioc, 0);
  90. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  91. rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  92. FORCE_BIG_HAMMER);
  93. printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
  94. __func__, (rc == 0) ? "success" : "failed");
  95. doorbell = mpt2sas_base_get_iocstate(ioc, 0);
  96. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  97. mpt2sas_base_fault_info(ioc, doorbell &
  98. MPI2_DOORBELL_DATA_MASK);
  99. }
  100. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  101. rearm_timer:
  102. if (ioc->fault_reset_work_q)
  103. queue_delayed_work(ioc->fault_reset_work_q,
  104. &ioc->fault_reset_work,
  105. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  106. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  107. }
  108. /**
  109. * mpt2sas_base_start_watchdog - start the fault_reset_work_q
  110. * @ioc: pointer to scsi command object
  111. * Context: sleep.
  112. *
  113. * Return nothing.
  114. */
  115. void
  116. mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
  117. {
  118. unsigned long flags;
  119. if (ioc->fault_reset_work_q)
  120. return;
  121. /* initialize fault polling */
  122. INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
  123. snprintf(ioc->fault_reset_work_q_name,
  124. sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
  125. ioc->fault_reset_work_q =
  126. create_singlethread_workqueue(ioc->fault_reset_work_q_name);
  127. if (!ioc->fault_reset_work_q) {
  128. printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
  129. ioc->name, __func__, __LINE__);
  130. return;
  131. }
  132. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  133. if (ioc->fault_reset_work_q)
  134. queue_delayed_work(ioc->fault_reset_work_q,
  135. &ioc->fault_reset_work,
  136. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  137. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  138. }
  139. /**
  140. * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
  141. * @ioc: pointer to scsi command object
  142. * Context: sleep.
  143. *
  144. * Return nothing.
  145. */
  146. void
  147. mpt2sas_base_stop_watchdog(struct MPT2SAS_ADAPTER *ioc)
  148. {
  149. unsigned long flags;
  150. struct workqueue_struct *wq;
  151. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  152. wq = ioc->fault_reset_work_q;
  153. ioc->fault_reset_work_q = NULL;
  154. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  155. if (wq) {
  156. if (!cancel_delayed_work(&ioc->fault_reset_work))
  157. flush_workqueue(wq);
  158. destroy_workqueue(wq);
  159. }
  160. }
  161. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  162. /**
  163. * _base_sas_ioc_info - verbose translation of the ioc status
  164. * @ioc: pointer to scsi command object
  165. * @mpi_reply: reply mf payload returned from firmware
  166. * @request_hdr: request mf
  167. *
  168. * Return nothing.
  169. */
  170. static void
  171. _base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
  172. MPI2RequestHeader_t *request_hdr)
  173. {
  174. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  175. MPI2_IOCSTATUS_MASK;
  176. char *desc = NULL;
  177. u16 frame_sz;
  178. char *func_str = NULL;
  179. /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
  180. if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  181. request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
  182. request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
  183. return;
  184. switch (ioc_status) {
  185. /****************************************************************************
  186. * Common IOCStatus values for all replies
  187. ****************************************************************************/
  188. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  189. desc = "invalid function";
  190. break;
  191. case MPI2_IOCSTATUS_BUSY:
  192. desc = "busy";
  193. break;
  194. case MPI2_IOCSTATUS_INVALID_SGL:
  195. desc = "invalid sgl";
  196. break;
  197. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  198. desc = "internal error";
  199. break;
  200. case MPI2_IOCSTATUS_INVALID_VPID:
  201. desc = "invalid vpid";
  202. break;
  203. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  204. desc = "insufficient resources";
  205. break;
  206. case MPI2_IOCSTATUS_INVALID_FIELD:
  207. desc = "invalid field";
  208. break;
  209. case MPI2_IOCSTATUS_INVALID_STATE:
  210. desc = "invalid state";
  211. break;
  212. case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
  213. desc = "op state not supported";
  214. break;
  215. /****************************************************************************
  216. * Config IOCStatus values
  217. ****************************************************************************/
  218. case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
  219. desc = "config invalid action";
  220. break;
  221. case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
  222. desc = "config invalid type";
  223. break;
  224. case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
  225. desc = "config invalid page";
  226. break;
  227. case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
  228. desc = "config invalid data";
  229. break;
  230. case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
  231. desc = "config no defaults";
  232. break;
  233. case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
  234. desc = "config cant commit";
  235. break;
  236. /****************************************************************************
  237. * SCSI IO Reply
  238. ****************************************************************************/
  239. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  240. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  241. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  242. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  243. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  244. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  245. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  246. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  247. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  248. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  249. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  250. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  251. break;
  252. /****************************************************************************
  253. * For use by SCSI Initiator and SCSI Target end-to-end data protection
  254. ****************************************************************************/
  255. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  256. desc = "eedp guard error";
  257. break;
  258. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  259. desc = "eedp ref tag error";
  260. break;
  261. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  262. desc = "eedp app tag error";
  263. break;
  264. /****************************************************************************
  265. * SCSI Target values
  266. ****************************************************************************/
  267. case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
  268. desc = "target invalid io index";
  269. break;
  270. case MPI2_IOCSTATUS_TARGET_ABORTED:
  271. desc = "target aborted";
  272. break;
  273. case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
  274. desc = "target no conn retryable";
  275. break;
  276. case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
  277. desc = "target no connection";
  278. break;
  279. case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
  280. desc = "target xfer count mismatch";
  281. break;
  282. case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
  283. desc = "target data offset error";
  284. break;
  285. case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
  286. desc = "target too much write data";
  287. break;
  288. case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
  289. desc = "target iu too short";
  290. break;
  291. case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
  292. desc = "target ack nak timeout";
  293. break;
  294. case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
  295. desc = "target nak received";
  296. break;
  297. /****************************************************************************
  298. * Serial Attached SCSI values
  299. ****************************************************************************/
  300. case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
  301. desc = "smp request failed";
  302. break;
  303. case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
  304. desc = "smp data overrun";
  305. break;
  306. /****************************************************************************
  307. * Diagnostic Buffer Post / Diagnostic Release values
  308. ****************************************************************************/
  309. case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
  310. desc = "diagnostic released";
  311. break;
  312. default:
  313. break;
  314. }
  315. if (!desc)
  316. return;
  317. switch (request_hdr->Function) {
  318. case MPI2_FUNCTION_CONFIG:
  319. frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
  320. func_str = "config_page";
  321. break;
  322. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  323. frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
  324. func_str = "task_mgmt";
  325. break;
  326. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  327. frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
  328. func_str = "sas_iounit_ctl";
  329. break;
  330. case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
  331. frame_sz = sizeof(Mpi2SepRequest_t);
  332. func_str = "enclosure";
  333. break;
  334. case MPI2_FUNCTION_IOC_INIT:
  335. frame_sz = sizeof(Mpi2IOCInitRequest_t);
  336. func_str = "ioc_init";
  337. break;
  338. case MPI2_FUNCTION_PORT_ENABLE:
  339. frame_sz = sizeof(Mpi2PortEnableRequest_t);
  340. func_str = "port_enable";
  341. break;
  342. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  343. frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
  344. func_str = "smp_passthru";
  345. break;
  346. default:
  347. frame_sz = 32;
  348. func_str = "unknown";
  349. break;
  350. }
  351. printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
  352. " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);
  353. _debug_dump_mf(request_hdr, frame_sz/4);
  354. }
  355. /**
  356. * _base_display_event_data - verbose translation of firmware asyn events
  357. * @ioc: pointer to scsi command object
  358. * @mpi_reply: reply mf payload returned from firmware
  359. *
  360. * Return nothing.
  361. */
  362. static void
  363. _base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
  364. Mpi2EventNotificationReply_t *mpi_reply)
  365. {
  366. char *desc = NULL;
  367. u16 event;
  368. if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
  369. return;
  370. event = le16_to_cpu(mpi_reply->Event);
  371. switch (event) {
  372. case MPI2_EVENT_LOG_DATA:
  373. desc = "Log Data";
  374. break;
  375. case MPI2_EVENT_STATE_CHANGE:
  376. desc = "Status Change";
  377. break;
  378. case MPI2_EVENT_HARD_RESET_RECEIVED:
  379. desc = "Hard Reset Received";
  380. break;
  381. case MPI2_EVENT_EVENT_CHANGE:
  382. desc = "Event Change";
  383. break;
  384. case MPI2_EVENT_TASK_SET_FULL:
  385. desc = "Task Set Full";
  386. break;
  387. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  388. desc = "Device Status Change";
  389. break;
  390. case MPI2_EVENT_IR_OPERATION_STATUS:
  391. desc = "IR Operation Status";
  392. break;
  393. case MPI2_EVENT_SAS_DISCOVERY:
  394. desc = "Discovery";
  395. break;
  396. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  397. desc = "SAS Broadcast Primitive";
  398. break;
  399. case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
  400. desc = "SAS Init Device Status Change";
  401. break;
  402. case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
  403. desc = "SAS Init Table Overflow";
  404. break;
  405. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  406. desc = "SAS Topology Change List";
  407. break;
  408. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  409. desc = "SAS Enclosure Device Status Change";
  410. break;
  411. case MPI2_EVENT_IR_VOLUME:
  412. desc = "IR Volume";
  413. break;
  414. case MPI2_EVENT_IR_PHYSICAL_DISK:
  415. desc = "IR Physical Disk";
  416. break;
  417. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  418. desc = "IR Configuration Change List";
  419. break;
  420. case MPI2_EVENT_LOG_ENTRY_ADDED:
  421. desc = "Log Entry Added";
  422. break;
  423. }
  424. if (!desc)
  425. return;
  426. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
  427. }
  428. #endif
  429. /**
  430. * _base_sas_log_info - verbose translation of firmware log info
  431. * @ioc: pointer to scsi command object
  432. * @log_info: log info
  433. *
  434. * Return nothing.
  435. */
  436. static void
  437. _base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
  438. {
  439. union loginfo_type {
  440. u32 loginfo;
  441. struct {
  442. u32 subcode:16;
  443. u32 code:8;
  444. u32 originator:4;
  445. u32 bus_type:4;
  446. } dw;
  447. };
  448. union loginfo_type sas_loginfo;
  449. char *originator_str = NULL;
  450. sas_loginfo.loginfo = log_info;
  451. if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
  452. return;
  453. /* each nexus loss loginfo */
  454. if (log_info == 0x31170000)
  455. return;
  456. /* eat the loginfos associated with task aborts */
  457. if (ioc->ignore_loginfos && (log_info == 30050000 || log_info ==
  458. 0x31140000 || log_info == 0x31130000))
  459. return;
  460. switch (sas_loginfo.dw.originator) {
  461. case 0:
  462. originator_str = "IOP";
  463. break;
  464. case 1:
  465. originator_str = "PL";
  466. break;
  467. case 2:
  468. originator_str = "IR";
  469. break;
  470. }
  471. printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
  472. "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
  473. originator_str, sas_loginfo.dw.code,
  474. sas_loginfo.dw.subcode);
  475. }
  476. /**
  477. * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
  478. * @ioc: pointer to scsi command object
  479. * @fault_code: fault code
  480. *
  481. * Return nothing.
  482. */
  483. void
  484. mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
  485. {
  486. printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
  487. ioc->name, fault_code);
  488. }
  489. /**
  490. * _base_display_reply_info -
  491. * @ioc: pointer to scsi command object
  492. * @smid: system request message index
  493. * @msix_index: MSIX table index supplied by the OS
  494. * @reply: reply message frame(lower 32bit addr)
  495. *
  496. * Return nothing.
  497. */
  498. static void
  499. _base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  500. u32 reply)
  501. {
  502. MPI2DefaultReply_t *mpi_reply;
  503. u16 ioc_status;
  504. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  505. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  506. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  507. if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
  508. (ioc->logging_level & MPT_DEBUG_REPLY)) {
  509. _base_sas_ioc_info(ioc , mpi_reply,
  510. mpt2sas_base_get_msg_frame(ioc, smid));
  511. }
  512. #endif
  513. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  514. _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
  515. }
  516. /**
  517. * mpt2sas_base_done - base internal command completion routine
  518. * @ioc: pointer to scsi command object
  519. * @smid: system request message index
  520. * @msix_index: MSIX table index supplied by the OS
  521. * @reply: reply message frame(lower 32bit addr)
  522. *
  523. * Return nothing.
  524. */
  525. void
  526. mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  527. u32 reply)
  528. {
  529. MPI2DefaultReply_t *mpi_reply;
  530. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  531. if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
  532. return;
  533. if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
  534. return;
  535. ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
  536. if (mpi_reply) {
  537. ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
  538. memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  539. }
  540. ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
  541. complete(&ioc->base_cmds.done);
  542. }
  543. /**
  544. * _base_async_event - main callback handler for firmware asyn events
  545. * @ioc: pointer to scsi command object
  546. * @msix_index: MSIX table index supplied by the OS
  547. * @reply: reply message frame(lower 32bit addr)
  548. *
  549. * Return nothing.
  550. */
  551. static void
  552. _base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
  553. {
  554. Mpi2EventNotificationReply_t *mpi_reply;
  555. Mpi2EventAckRequest_t *ack_request;
  556. u16 smid;
  557. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  558. if (!mpi_reply)
  559. return;
  560. if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
  561. return;
  562. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  563. _base_display_event_data(ioc, mpi_reply);
  564. #endif
  565. if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
  566. goto out;
  567. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  568. if (!smid) {
  569. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  570. ioc->name, __func__);
  571. goto out;
  572. }
  573. ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
  574. memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
  575. ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
  576. ack_request->Event = mpi_reply->Event;
  577. ack_request->EventContext = mpi_reply->EventContext;
  578. ack_request->VF_ID = 0; /* TODO */
  579. ack_request->VP_ID = 0;
  580. mpt2sas_base_put_smid_default(ioc, smid);
  581. out:
  582. /* scsih callback handler */
  583. mpt2sas_scsih_event_callback(ioc, msix_index, reply);
  584. /* ctl callback handler */
  585. mpt2sas_ctl_event_callback(ioc, msix_index, reply);
  586. }
  587. /**
  588. * _base_mask_interrupts - disable interrupts
  589. * @ioc: pointer to scsi command object
  590. *
  591. * Disabling ResetIRQ, Reply and Doorbell Interrupts
  592. *
  593. * Return nothing.
  594. */
  595. static void
  596. _base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
  597. {
  598. u32 him_register;
  599. ioc->mask_interrupts = 1;
  600. him_register = readl(&ioc->chip->HostInterruptMask);
  601. him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
  602. writel(him_register, &ioc->chip->HostInterruptMask);
  603. readl(&ioc->chip->HostInterruptMask);
  604. }
  605. /**
  606. * _base_unmask_interrupts - enable interrupts
  607. * @ioc: pointer to scsi command object
  608. *
  609. * Enabling only Reply Interrupts
  610. *
  611. * Return nothing.
  612. */
  613. static void
  614. _base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
  615. {
  616. u32 him_register;
  617. writel(0, &ioc->chip->HostInterruptStatus);
  618. him_register = readl(&ioc->chip->HostInterruptMask);
  619. him_register &= ~MPI2_HIM_RIM;
  620. writel(him_register, &ioc->chip->HostInterruptMask);
  621. ioc->mask_interrupts = 0;
  622. }
  623. union reply_descriptor {
  624. u64 word;
  625. struct {
  626. u32 low;
  627. u32 high;
  628. } u;
  629. };
  630. /**
  631. * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
  632. * @irq: irq number (not used)
  633. * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
  634. * @r: pt_regs pointer (not used)
  635. *
  636. * Return IRQ_HANDLE if processed, else IRQ_NONE.
  637. */
  638. static irqreturn_t
  639. _base_interrupt(int irq, void *bus_id)
  640. {
  641. union reply_descriptor rd;
  642. u32 completed_cmds;
  643. u8 request_desript_type;
  644. u16 smid;
  645. u8 cb_idx;
  646. u32 reply;
  647. u8 msix_index;
  648. struct MPT2SAS_ADAPTER *ioc = bus_id;
  649. Mpi2ReplyDescriptorsUnion_t *rpf;
  650. if (ioc->mask_interrupts)
  651. return IRQ_NONE;
  652. rpf = &ioc->reply_post_free[ioc->reply_post_host_index];
  653. request_desript_type = rpf->Default.ReplyFlags
  654. & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  655. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  656. return IRQ_NONE;
  657. completed_cmds = 0;
  658. do {
  659. rd.word = rpf->Words;
  660. if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
  661. goto out;
  662. reply = 0;
  663. cb_idx = 0xFF;
  664. smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
  665. msix_index = rpf->Default.MSIxIndex;
  666. if (request_desript_type ==
  667. MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
  668. reply = le32_to_cpu
  669. (rpf->AddressReply.ReplyFrameAddress);
  670. } else if (request_desript_type ==
  671. MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
  672. goto next;
  673. else if (request_desript_type ==
  674. MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
  675. goto next;
  676. if (smid)
  677. cb_idx = ioc->scsi_lookup[smid - 1].cb_idx;
  678. if (smid && cb_idx != 0xFF) {
  679. mpt_callbacks[cb_idx](ioc, smid, msix_index, reply);
  680. if (reply)
  681. _base_display_reply_info(ioc, smid, msix_index,
  682. reply);
  683. mpt2sas_base_free_smid(ioc, smid);
  684. }
  685. if (!smid)
  686. _base_async_event(ioc, msix_index, reply);
  687. /* reply free queue handling */
  688. if (reply) {
  689. ioc->reply_free_host_index =
  690. (ioc->reply_free_host_index ==
  691. (ioc->reply_free_queue_depth - 1)) ?
  692. 0 : ioc->reply_free_host_index + 1;
  693. ioc->reply_free[ioc->reply_free_host_index] =
  694. cpu_to_le32(reply);
  695. wmb();
  696. writel(ioc->reply_free_host_index,
  697. &ioc->chip->ReplyFreeHostIndex);
  698. }
  699. next:
  700. rpf->Words = ULLONG_MAX;
  701. ioc->reply_post_host_index = (ioc->reply_post_host_index ==
  702. (ioc->reply_post_queue_depth - 1)) ? 0 :
  703. ioc->reply_post_host_index + 1;
  704. request_desript_type =
  705. ioc->reply_post_free[ioc->reply_post_host_index].Default.
  706. ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  707. completed_cmds++;
  708. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  709. goto out;
  710. if (!ioc->reply_post_host_index)
  711. rpf = ioc->reply_post_free;
  712. else
  713. rpf++;
  714. } while (1);
  715. out:
  716. if (!completed_cmds)
  717. return IRQ_NONE;
  718. wmb();
  719. writel(ioc->reply_post_host_index, &ioc->chip->ReplyPostHostIndex);
  720. return IRQ_HANDLED;
  721. }
  722. /**
  723. * mpt2sas_base_release_callback_handler - clear interupt callback handler
  724. * @cb_idx: callback index
  725. *
  726. * Return nothing.
  727. */
  728. void
  729. mpt2sas_base_release_callback_handler(u8 cb_idx)
  730. {
  731. mpt_callbacks[cb_idx] = NULL;
  732. }
  733. /**
  734. * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
  735. * @cb_func: callback function
  736. *
  737. * Returns cb_func.
  738. */
  739. u8
  740. mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
  741. {
  742. u8 cb_idx;
  743. for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
  744. if (mpt_callbacks[cb_idx] == NULL)
  745. break;
  746. mpt_callbacks[cb_idx] = cb_func;
  747. return cb_idx;
  748. }
  749. /**
  750. * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
  751. *
  752. * Return nothing.
  753. */
  754. void
  755. mpt2sas_base_initialize_callback_handler(void)
  756. {
  757. u8 cb_idx;
  758. for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
  759. mpt2sas_base_release_callback_handler(cb_idx);
  760. }
  761. /**
  762. * mpt2sas_base_build_zero_len_sge - build zero length sg entry
  763. * @ioc: per adapter object
  764. * @paddr: virtual address for SGE
  765. *
  766. * Create a zero length scatter gather entry to insure the IOCs hardware has
  767. * something to use if the target device goes brain dead and tries
  768. * to send data even when none is asked for.
  769. *
  770. * Return nothing.
  771. */
  772. void
  773. mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
  774. {
  775. u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
  776. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
  777. MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
  778. MPI2_SGE_FLAGS_SHIFT);
  779. ioc->base_add_sg_single(paddr, flags_length, -1);
  780. }
  781. /**
  782. * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
  783. * @paddr: virtual address for SGE
  784. * @flags_length: SGE flags and data transfer length
  785. * @dma_addr: Physical address
  786. *
  787. * Return nothing.
  788. */
  789. static void
  790. _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  791. {
  792. Mpi2SGESimple32_t *sgel = paddr;
  793. flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
  794. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  795. sgel->FlagsLength = cpu_to_le32(flags_length);
  796. sgel->Address = cpu_to_le32(dma_addr);
  797. }
  798. /**
  799. * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
  800. * @paddr: virtual address for SGE
  801. * @flags_length: SGE flags and data transfer length
  802. * @dma_addr: Physical address
  803. *
  804. * Return nothing.
  805. */
  806. static void
  807. _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  808. {
  809. Mpi2SGESimple64_t *sgel = paddr;
  810. flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
  811. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  812. sgel->FlagsLength = cpu_to_le32(flags_length);
  813. sgel->Address = cpu_to_le64(dma_addr);
  814. }
  815. #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
  816. /**
  817. * _base_config_dma_addressing - set dma addressing
  818. * @ioc: per adapter object
  819. * @pdev: PCI device struct
  820. *
  821. * Returns 0 for success, non-zero for failure.
  822. */
  823. static int
  824. _base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
  825. {
  826. struct sysinfo s;
  827. char *desc = NULL;
  828. if (sizeof(dma_addr_t) > 4) {
  829. const uint64_t required_mask =
  830. dma_get_required_mask(&pdev->dev);
  831. if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
  832. DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
  833. DMA_BIT_MASK(64))) {
  834. ioc->base_add_sg_single = &_base_add_sg_single_64;
  835. ioc->sge_size = sizeof(Mpi2SGESimple64_t);
  836. desc = "64";
  837. goto out;
  838. }
  839. }
  840. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
  841. && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
  842. ioc->base_add_sg_single = &_base_add_sg_single_32;
  843. ioc->sge_size = sizeof(Mpi2SGESimple32_t);
  844. desc = "32";
  845. } else
  846. return -ENODEV;
  847. out:
  848. si_meminfo(&s);
  849. printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
  850. "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));
  851. return 0;
  852. }
  853. /**
  854. * _base_save_msix_table - backup msix vector table
  855. * @ioc: per adapter object
  856. *
  857. * This address an errata where diag reset clears out the table
  858. */
  859. static void
  860. _base_save_msix_table(struct MPT2SAS_ADAPTER *ioc)
  861. {
  862. int i;
  863. if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
  864. return;
  865. for (i = 0; i < ioc->msix_vector_count; i++)
  866. ioc->msix_table_backup[i] = ioc->msix_table[i];
  867. }
  868. /**
  869. * _base_restore_msix_table - this restores the msix vector table
  870. * @ioc: per adapter object
  871. *
  872. */
  873. static void
  874. _base_restore_msix_table(struct MPT2SAS_ADAPTER *ioc)
  875. {
  876. int i;
  877. if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
  878. return;
  879. for (i = 0; i < ioc->msix_vector_count; i++)
  880. ioc->msix_table[i] = ioc->msix_table_backup[i];
  881. }
  882. /**
  883. * _base_check_enable_msix - checks MSIX capabable.
  884. * @ioc: per adapter object
  885. *
  886. * Check to see if card is capable of MSIX, and set number
  887. * of avaliable msix vectors
  888. */
  889. static int
  890. _base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
  891. {
  892. int base;
  893. u16 message_control;
  894. u32 msix_table_offset;
  895. base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
  896. if (!base) {
  897. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
  898. "supported\n", ioc->name));
  899. return -EINVAL;
  900. }
  901. /* get msix vector count */
  902. pci_read_config_word(ioc->pdev, base + 2, &message_control);
  903. ioc->msix_vector_count = (message_control & 0x3FF) + 1;
  904. /* get msix table */
  905. pci_read_config_dword(ioc->pdev, base + 4, &msix_table_offset);
  906. msix_table_offset &= 0xFFFFFFF8;
  907. ioc->msix_table = (u32 *)((void *)ioc->chip + msix_table_offset);
  908. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
  909. "vector_count(%d), table_offset(0x%08x), table(%p)\n", ioc->name,
  910. ioc->msix_vector_count, msix_table_offset, ioc->msix_table));
  911. return 0;
  912. }
  913. /**
  914. * _base_disable_msix - disables msix
  915. * @ioc: per adapter object
  916. *
  917. */
  918. static void
  919. _base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
  920. {
  921. if (ioc->msix_enable) {
  922. pci_disable_msix(ioc->pdev);
  923. kfree(ioc->msix_table_backup);
  924. ioc->msix_table_backup = NULL;
  925. ioc->msix_enable = 0;
  926. }
  927. }
  928. /**
  929. * _base_enable_msix - enables msix, failback to io_apic
  930. * @ioc: per adapter object
  931. *
  932. */
  933. static int
  934. _base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
  935. {
  936. struct msix_entry entries;
  937. int r;
  938. u8 try_msix = 0;
  939. if (msix_disable == -1 || msix_disable == 0)
  940. try_msix = 1;
  941. if (!try_msix)
  942. goto try_ioapic;
  943. if (_base_check_enable_msix(ioc) != 0)
  944. goto try_ioapic;
  945. ioc->msix_table_backup = kcalloc(ioc->msix_vector_count,
  946. sizeof(u32), GFP_KERNEL);
  947. if (!ioc->msix_table_backup) {
  948. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
  949. "msix_table_backup failed!!!\n", ioc->name));
  950. goto try_ioapic;
  951. }
  952. memset(&entries, 0, sizeof(struct msix_entry));
  953. r = pci_enable_msix(ioc->pdev, &entries, 1);
  954. if (r) {
  955. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
  956. "failed (r=%d) !!!\n", ioc->name, r));
  957. goto try_ioapic;
  958. }
  959. r = request_irq(entries.vector, _base_interrupt, IRQF_SHARED,
  960. ioc->name, ioc);
  961. if (r) {
  962. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "unable to allocate "
  963. "interrupt %d !!!\n", ioc->name, entries.vector));
  964. pci_disable_msix(ioc->pdev);
  965. goto try_ioapic;
  966. }
  967. ioc->pci_irq = entries.vector;
  968. ioc->msix_enable = 1;
  969. return 0;
  970. /* failback to io_apic interrupt routing */
  971. try_ioapic:
  972. r = request_irq(ioc->pdev->irq, _base_interrupt, IRQF_SHARED,
  973. ioc->name, ioc);
  974. if (r) {
  975. printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
  976. ioc->name, ioc->pdev->irq);
  977. r = -EBUSY;
  978. goto out_fail;
  979. }
  980. ioc->pci_irq = ioc->pdev->irq;
  981. return 0;
  982. out_fail:
  983. return r;
  984. }
  985. /**
  986. * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
  987. * @ioc: per adapter object
  988. *
  989. * Returns 0 for success, non-zero for failure.
  990. */
  991. int
  992. mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
  993. {
  994. struct pci_dev *pdev = ioc->pdev;
  995. u32 memap_sz;
  996. u32 pio_sz;
  997. int i, r = 0;
  998. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n",
  999. ioc->name, __func__));
  1000. ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1001. if (pci_enable_device_mem(pdev)) {
  1002. printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
  1003. "failed\n", ioc->name);
  1004. return -ENODEV;
  1005. }
  1006. if (pci_request_selected_regions(pdev, ioc->bars,
  1007. MPT2SAS_DRIVER_NAME)) {
  1008. printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
  1009. "failed\n", ioc->name);
  1010. r = -ENODEV;
  1011. goto out_fail;
  1012. }
  1013. pci_set_master(pdev);
  1014. if (_base_config_dma_addressing(ioc, pdev) != 0) {
  1015. printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
  1016. ioc->name, pci_name(pdev));
  1017. r = -ENODEV;
  1018. goto out_fail;
  1019. }
  1020. for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
  1021. if (pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE_IO) {
  1022. if (pio_sz)
  1023. continue;
  1024. ioc->pio_chip = pci_resource_start(pdev, i);
  1025. pio_sz = pci_resource_len(pdev, i);
  1026. } else {
  1027. if (memap_sz)
  1028. continue;
  1029. ioc->chip_phys = pci_resource_start(pdev, i);
  1030. memap_sz = pci_resource_len(pdev, i);
  1031. ioc->chip = ioremap(ioc->chip_phys, memap_sz);
  1032. if (ioc->chip == NULL) {
  1033. printk(MPT2SAS_ERR_FMT "unable to map adapter "
  1034. "memory!\n", ioc->name);
  1035. r = -EINVAL;
  1036. goto out_fail;
  1037. }
  1038. }
  1039. }
  1040. _base_mask_interrupts(ioc);
  1041. r = _base_enable_msix(ioc);
  1042. if (r)
  1043. goto out_fail;
  1044. printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
  1045. ioc->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
  1046. "IO-APIC enabled"), ioc->pci_irq);
  1047. printk(MPT2SAS_INFO_FMT "iomem(0x%lx), mapped(0x%p), size(%d)\n",
  1048. ioc->name, ioc->chip_phys, ioc->chip, memap_sz);
  1049. printk(MPT2SAS_INFO_FMT "ioport(0x%lx), size(%d)\n",
  1050. ioc->name, ioc->pio_chip, pio_sz);
  1051. return 0;
  1052. out_fail:
  1053. if (ioc->chip_phys)
  1054. iounmap(ioc->chip);
  1055. ioc->chip_phys = 0;
  1056. ioc->pci_irq = -1;
  1057. pci_release_selected_regions(ioc->pdev, ioc->bars);
  1058. pci_disable_device(pdev);
  1059. return r;
  1060. }
  1061. /**
  1062. * mpt2sas_base_get_msg_frame_dma - obtain request mf pointer phys addr
  1063. * @ioc: per adapter object
  1064. * @smid: system request message index(smid zero is invalid)
  1065. *
  1066. * Returns phys pointer to message frame.
  1067. */
  1068. dma_addr_t
  1069. mpt2sas_base_get_msg_frame_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1070. {
  1071. return ioc->request_dma + (smid * ioc->request_sz);
  1072. }
  1073. /**
  1074. * mpt2sas_base_get_msg_frame - obtain request mf pointer
  1075. * @ioc: per adapter object
  1076. * @smid: system request message index(smid zero is invalid)
  1077. *
  1078. * Returns virt pointer to message frame.
  1079. */
  1080. void *
  1081. mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1082. {
  1083. return (void *)(ioc->request + (smid * ioc->request_sz));
  1084. }
  1085. /**
  1086. * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
  1087. * @ioc: per adapter object
  1088. * @smid: system request message index
  1089. *
  1090. * Returns virt pointer to sense buffer.
  1091. */
  1092. void *
  1093. mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1094. {
  1095. return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
  1096. }
  1097. /**
  1098. * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
  1099. * @ioc: per adapter object
  1100. * @smid: system request message index
  1101. *
  1102. * Returns phys pointer to sense buffer.
  1103. */
  1104. dma_addr_t
  1105. mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1106. {
  1107. return ioc->sense_dma + ((smid - 1) * SCSI_SENSE_BUFFERSIZE);
  1108. }
  1109. /**
  1110. * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
  1111. * @ioc: per adapter object
  1112. * @phys_addr: lower 32 physical addr of the reply
  1113. *
  1114. * Converts 32bit lower physical addr into a virt address.
  1115. */
  1116. void *
  1117. mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
  1118. {
  1119. if (!phys_addr)
  1120. return NULL;
  1121. return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
  1122. }
  1123. /**
  1124. * mpt2sas_base_get_smid - obtain a free smid
  1125. * @ioc: per adapter object
  1126. * @cb_idx: callback index
  1127. *
  1128. * Returns smid (zero is invalid)
  1129. */
  1130. u16
  1131. mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
  1132. {
  1133. unsigned long flags;
  1134. struct request_tracker *request;
  1135. u16 smid;
  1136. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1137. if (list_empty(&ioc->free_list)) {
  1138. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1139. printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
  1140. ioc->name, __func__);
  1141. return 0;
  1142. }
  1143. request = list_entry(ioc->free_list.next,
  1144. struct request_tracker, tracker_list);
  1145. request->cb_idx = cb_idx;
  1146. smid = request->smid;
  1147. list_del(&request->tracker_list);
  1148. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1149. return smid;
  1150. }
  1151. /**
  1152. * mpt2sas_base_free_smid - put smid back on free_list
  1153. * @ioc: per adapter object
  1154. * @smid: system request message index
  1155. *
  1156. * Return nothing.
  1157. */
  1158. void
  1159. mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1160. {
  1161. unsigned long flags;
  1162. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1163. ioc->scsi_lookup[smid - 1].cb_idx = 0xFF;
  1164. list_add_tail(&ioc->scsi_lookup[smid - 1].tracker_list,
  1165. &ioc->free_list);
  1166. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1167. /*
  1168. * See _wait_for_commands_to_complete() call with regards to this code.
  1169. */
  1170. if (ioc->shost_recovery && ioc->pending_io_count) {
  1171. if (ioc->pending_io_count == 1)
  1172. wake_up(&ioc->reset_wq);
  1173. ioc->pending_io_count--;
  1174. }
  1175. }
  1176. /**
  1177. * _base_writeq - 64 bit write to MMIO
  1178. * @ioc: per adapter object
  1179. * @b: data payload
  1180. * @addr: address in MMIO space
  1181. * @writeq_lock: spin lock
  1182. *
  1183. * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
  1184. * care of 32 bit environment where its not quarenteed to send the entire word
  1185. * in one transfer.
  1186. */
  1187. #ifndef writeq
  1188. static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
  1189. spinlock_t *writeq_lock)
  1190. {
  1191. unsigned long flags;
  1192. __u64 data_out = cpu_to_le64(b);
  1193. spin_lock_irqsave(writeq_lock, flags);
  1194. writel((u32)(data_out), addr);
  1195. writel((u32)(data_out >> 32), (addr + 4));
  1196. spin_unlock_irqrestore(writeq_lock, flags);
  1197. }
  1198. #else
  1199. static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
  1200. spinlock_t *writeq_lock)
  1201. {
  1202. writeq(cpu_to_le64(b), addr);
  1203. }
  1204. #endif
  1205. /**
  1206. * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
  1207. * @ioc: per adapter object
  1208. * @smid: system request message index
  1209. * @handle: device handle
  1210. *
  1211. * Return nothing.
  1212. */
  1213. void
  1214. mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
  1215. {
  1216. Mpi2RequestDescriptorUnion_t descriptor;
  1217. u64 *request = (u64 *)&descriptor;
  1218. descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
  1219. descriptor.SCSIIO.MSIxIndex = 0; /* TODO */
  1220. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  1221. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  1222. descriptor.SCSIIO.LMID = 0;
  1223. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1224. &ioc->scsi_lookup_lock);
  1225. }
  1226. /**
  1227. * mpt2sas_base_put_smid_hi_priority - send Task Managment request to firmware
  1228. * @ioc: per adapter object
  1229. * @smid: system request message index
  1230. *
  1231. * Return nothing.
  1232. */
  1233. void
  1234. mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1235. {
  1236. Mpi2RequestDescriptorUnion_t descriptor;
  1237. u64 *request = (u64 *)&descriptor;
  1238. descriptor.HighPriority.RequestFlags =
  1239. MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
  1240. descriptor.HighPriority.MSIxIndex = 0; /* TODO */
  1241. descriptor.HighPriority.SMID = cpu_to_le16(smid);
  1242. descriptor.HighPriority.LMID = 0;
  1243. descriptor.HighPriority.Reserved1 = 0;
  1244. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1245. &ioc->scsi_lookup_lock);
  1246. }
  1247. /**
  1248. * mpt2sas_base_put_smid_default - Default, primarily used for config pages
  1249. * @ioc: per adapter object
  1250. * @smid: system request message index
  1251. *
  1252. * Return nothing.
  1253. */
  1254. void
  1255. mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1256. {
  1257. Mpi2RequestDescriptorUnion_t descriptor;
  1258. u64 *request = (u64 *)&descriptor;
  1259. descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
  1260. descriptor.Default.MSIxIndex = 0; /* TODO */
  1261. descriptor.Default.SMID = cpu_to_le16(smid);
  1262. descriptor.Default.LMID = 0;
  1263. descriptor.Default.DescriptorTypeDependent = 0;
  1264. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1265. &ioc->scsi_lookup_lock);
  1266. }
  1267. /**
  1268. * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
  1269. * @ioc: per adapter object
  1270. * @smid: system request message index
  1271. * @io_index: value used to track the IO
  1272. *
  1273. * Return nothing.
  1274. */
  1275. void
  1276. mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
  1277. u16 io_index)
  1278. {
  1279. Mpi2RequestDescriptorUnion_t descriptor;
  1280. u64 *request = (u64 *)&descriptor;
  1281. descriptor.SCSITarget.RequestFlags =
  1282. MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
  1283. descriptor.SCSITarget.MSIxIndex = 0; /* TODO */
  1284. descriptor.SCSITarget.SMID = cpu_to_le16(smid);
  1285. descriptor.SCSITarget.LMID = 0;
  1286. descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
  1287. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1288. &ioc->scsi_lookup_lock);
  1289. }
  1290. /**
  1291. * _base_display_dell_branding - Disply branding string
  1292. * @ioc: per adapter object
  1293. *
  1294. * Return nothing.
  1295. */
  1296. static void
  1297. _base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
  1298. {
  1299. char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
  1300. if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
  1301. return;
  1302. memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
  1303. switch (ioc->pdev->subsystem_device) {
  1304. case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
  1305. strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
  1306. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1307. break;
  1308. case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
  1309. strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
  1310. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1311. break;
  1312. case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
  1313. strncpy(dell_branding,
  1314. MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
  1315. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1316. break;
  1317. case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
  1318. strncpy(dell_branding,
  1319. MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
  1320. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1321. break;
  1322. case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
  1323. strncpy(dell_branding,
  1324. MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
  1325. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1326. break;
  1327. case MPT2SAS_DELL_PERC_H200_SSDID:
  1328. strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
  1329. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1330. break;
  1331. case MPT2SAS_DELL_6GBPS_SAS_SSDID:
  1332. strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
  1333. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1334. break;
  1335. default:
  1336. sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
  1337. break;
  1338. }
  1339. printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
  1340. " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
  1341. ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
  1342. ioc->pdev->subsystem_device);
  1343. }
  1344. /**
  1345. * _base_display_ioc_capabilities - Disply IOC's capabilities.
  1346. * @ioc: per adapter object
  1347. *
  1348. * Return nothing.
  1349. */
  1350. static void
  1351. _base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
  1352. {
  1353. int i = 0;
  1354. char desc[16];
  1355. u8 revision;
  1356. u32 iounit_pg1_flags;
  1357. pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
  1358. strncpy(desc, ioc->manu_pg0.ChipName, 16);
  1359. printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
  1360. "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
  1361. ioc->name, desc,
  1362. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  1363. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  1364. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  1365. ioc->facts.FWVersion.Word & 0x000000FF,
  1366. revision,
  1367. (ioc->bios_pg3.BiosVersion & 0xFF000000) >> 24,
  1368. (ioc->bios_pg3.BiosVersion & 0x00FF0000) >> 16,
  1369. (ioc->bios_pg3.BiosVersion & 0x0000FF00) >> 8,
  1370. ioc->bios_pg3.BiosVersion & 0x000000FF);
  1371. _base_display_dell_branding(ioc);
  1372. printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
  1373. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
  1374. printk("Initiator");
  1375. i++;
  1376. }
  1377. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
  1378. printk("%sTarget", i ? "," : "");
  1379. i++;
  1380. }
  1381. i = 0;
  1382. printk("), ");
  1383. printk("Capabilities=(");
  1384. if (ioc->facts.IOCCapabilities &
  1385. MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
  1386. printk("Raid");
  1387. i++;
  1388. }
  1389. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
  1390. printk("%sTLR", i ? "," : "");
  1391. i++;
  1392. }
  1393. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
  1394. printk("%sMulticast", i ? "," : "");
  1395. i++;
  1396. }
  1397. if (ioc->facts.IOCCapabilities &
  1398. MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
  1399. printk("%sBIDI Target", i ? "," : "");
  1400. i++;
  1401. }
  1402. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
  1403. printk("%sEEDP", i ? "," : "");
  1404. i++;
  1405. }
  1406. if (ioc->facts.IOCCapabilities &
  1407. MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
  1408. printk("%sSnapshot Buffer", i ? "," : "");
  1409. i++;
  1410. }
  1411. if (ioc->facts.IOCCapabilities &
  1412. MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
  1413. printk("%sDiag Trace Buffer", i ? "," : "");
  1414. i++;
  1415. }
  1416. if (ioc->facts.IOCCapabilities &
  1417. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
  1418. printk("%sTask Set Full", i ? "," : "");
  1419. i++;
  1420. }
  1421. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  1422. if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
  1423. printk("%sNCQ", i ? "," : "");
  1424. i++;
  1425. }
  1426. printk(")\n");
  1427. }
  1428. /**
  1429. * _base_static_config_pages - static start of day config pages
  1430. * @ioc: per adapter object
  1431. *
  1432. * Return nothing.
  1433. */
  1434. static void
  1435. _base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
  1436. {
  1437. Mpi2ConfigReply_t mpi_reply;
  1438. u32 iounit_pg1_flags;
  1439. mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
  1440. if (ioc->ir_firmware)
  1441. mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
  1442. &ioc->manu_pg10);
  1443. mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
  1444. mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
  1445. mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
  1446. mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
  1447. mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  1448. _base_display_ioc_capabilities(ioc);
  1449. /*
  1450. * Enable task_set_full handling in iounit_pg1 when the
  1451. * facts capabilities indicate that its supported.
  1452. */
  1453. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  1454. if ((ioc->facts.IOCCapabilities &
  1455. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
  1456. iounit_pg1_flags &=
  1457. ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  1458. else
  1459. iounit_pg1_flags |=
  1460. MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  1461. ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
  1462. mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  1463. }
  1464. /**
  1465. * _base_release_memory_pools - release memory
  1466. * @ioc: per adapter object
  1467. *
  1468. * Free memory allocated from _base_allocate_memory_pools.
  1469. *
  1470. * Return nothing.
  1471. */
  1472. static void
  1473. _base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
  1474. {
  1475. dexitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
  1476. __func__));
  1477. if (ioc->request) {
  1478. pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
  1479. ioc->request, ioc->request_dma);
  1480. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
  1481. ": free\n", ioc->name, ioc->request));
  1482. ioc->request = NULL;
  1483. }
  1484. if (ioc->sense) {
  1485. pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
  1486. if (ioc->sense_dma_pool)
  1487. pci_pool_destroy(ioc->sense_dma_pool);
  1488. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
  1489. ": free\n", ioc->name, ioc->sense));
  1490. ioc->sense = NULL;
  1491. }
  1492. if (ioc->reply) {
  1493. pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
  1494. if (ioc->reply_dma_pool)
  1495. pci_pool_destroy(ioc->reply_dma_pool);
  1496. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
  1497. ": free\n", ioc->name, ioc->reply));
  1498. ioc->reply = NULL;
  1499. }
  1500. if (ioc->reply_free) {
  1501. pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
  1502. ioc->reply_free_dma);
  1503. if (ioc->reply_free_dma_pool)
  1504. pci_pool_destroy(ioc->reply_free_dma_pool);
  1505. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
  1506. "(0x%p): free\n", ioc->name, ioc->reply_free));
  1507. ioc->reply_free = NULL;
  1508. }
  1509. if (ioc->reply_post_free) {
  1510. pci_pool_free(ioc->reply_post_free_dma_pool,
  1511. ioc->reply_post_free, ioc->reply_post_free_dma);
  1512. if (ioc->reply_post_free_dma_pool)
  1513. pci_pool_destroy(ioc->reply_post_free_dma_pool);
  1514. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
  1515. "reply_post_free_pool(0x%p): free\n", ioc->name,
  1516. ioc->reply_post_free));
  1517. ioc->reply_post_free = NULL;
  1518. }
  1519. if (ioc->config_page) {
  1520. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
  1521. "config_page(0x%p): free\n", ioc->name,
  1522. ioc->config_page));
  1523. pci_free_consistent(ioc->pdev, ioc->config_page_sz,
  1524. ioc->config_page, ioc->config_page_dma);
  1525. }
  1526. kfree(ioc->scsi_lookup);
  1527. }
  1528. /**
  1529. * _base_allocate_memory_pools - allocate start of day memory pools
  1530. * @ioc: per adapter object
  1531. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  1532. *
  1533. * Returns 0 success, anything else error
  1534. */
  1535. static int
  1536. _base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  1537. {
  1538. Mpi2IOCFactsReply_t *facts;
  1539. u32 queue_size, queue_diff;
  1540. u16 max_sge_elements;
  1541. u16 num_of_reply_frames;
  1542. u16 chains_needed_per_io;
  1543. u32 sz, total_sz;
  1544. u16 i;
  1545. u32 retry_sz;
  1546. u16 max_request_credit;
  1547. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
  1548. __func__));
  1549. retry_sz = 0;
  1550. facts = &ioc->facts;
  1551. /* command line tunables for max sgl entries */
  1552. if (max_sgl_entries != -1) {
  1553. ioc->shost->sg_tablesize = (max_sgl_entries <
  1554. MPT2SAS_SG_DEPTH) ? max_sgl_entries :
  1555. MPT2SAS_SG_DEPTH;
  1556. } else {
  1557. ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
  1558. }
  1559. /* command line tunables for max controller queue depth */
  1560. if (max_queue_depth != -1) {
  1561. max_request_credit = (max_queue_depth < facts->RequestCredit)
  1562. ? max_queue_depth : facts->RequestCredit;
  1563. } else {
  1564. max_request_credit = (facts->RequestCredit >
  1565. MPT2SAS_MAX_REQUEST_QUEUE) ? MPT2SAS_MAX_REQUEST_QUEUE :
  1566. facts->RequestCredit;
  1567. }
  1568. ioc->request_depth = max_request_credit;
  1569. /* request frame size */
  1570. ioc->request_sz = facts->IOCRequestFrameSize * 4;
  1571. /* reply frame size */
  1572. ioc->reply_sz = facts->ReplyFrameSize * 4;
  1573. retry_allocation:
  1574. total_sz = 0;
  1575. /* calculate number of sg elements left over in the 1st frame */
  1576. max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
  1577. sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
  1578. ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
  1579. /* now do the same for a chain buffer */
  1580. max_sge_elements = ioc->request_sz - ioc->sge_size;
  1581. ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
  1582. ioc->chain_offset_value_for_main_message =
  1583. ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
  1584. (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
  1585. /*
  1586. * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
  1587. */
  1588. chains_needed_per_io = ((ioc->shost->sg_tablesize -
  1589. ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
  1590. + 1;
  1591. if (chains_needed_per_io > facts->MaxChainDepth) {
  1592. chains_needed_per_io = facts->MaxChainDepth;
  1593. ioc->shost->sg_tablesize = min_t(u16,
  1594. ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
  1595. * chains_needed_per_io), ioc->shost->sg_tablesize);
  1596. }
  1597. ioc->chains_needed_per_io = chains_needed_per_io;
  1598. /* reply free queue sizing - taking into account for events */
  1599. num_of_reply_frames = ioc->request_depth + 32;
  1600. /* number of replies frames can't be a multiple of 16 */
  1601. /* decrease number of reply frames by 1 */
  1602. if (!(num_of_reply_frames % 16))
  1603. num_of_reply_frames--;
  1604. /* calculate number of reply free queue entries
  1605. * (must be multiple of 16)
  1606. */
  1607. /* (we know reply_free_queue_depth is not a multiple of 16) */
  1608. queue_size = num_of_reply_frames;
  1609. queue_size += 16 - (queue_size % 16);
  1610. ioc->reply_free_queue_depth = queue_size;
  1611. /* reply descriptor post queue sizing */
  1612. /* this size should be the number of request frames + number of reply
  1613. * frames
  1614. */
  1615. queue_size = ioc->request_depth + num_of_reply_frames + 1;
  1616. /* round up to 16 byte boundary */
  1617. if (queue_size % 16)
  1618. queue_size += 16 - (queue_size % 16);
  1619. /* check against IOC maximum reply post queue depth */
  1620. if (queue_size > facts->MaxReplyDescriptorPostQueueDepth) {
  1621. queue_diff = queue_size -
  1622. facts->MaxReplyDescriptorPostQueueDepth;
  1623. /* round queue_diff up to multiple of 16 */
  1624. if (queue_diff % 16)
  1625. queue_diff += 16 - (queue_diff % 16);
  1626. /* adjust request_depth, reply_free_queue_depth,
  1627. * and queue_size
  1628. */
  1629. ioc->request_depth -= queue_diff;
  1630. ioc->reply_free_queue_depth -= queue_diff;
  1631. queue_size -= queue_diff;
  1632. }
  1633. ioc->reply_post_queue_depth = queue_size;
  1634. /* max scsi host queue depth */
  1635. ioc->shost->can_queue = ioc->request_depth - INTERNAL_CMDS_COUNT;
  1636. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host queue: depth"
  1637. "(%d)\n", ioc->name, ioc->shost->can_queue));
  1638. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
  1639. "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
  1640. "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
  1641. ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
  1642. ioc->chains_needed_per_io));
  1643. /* contiguous pool for request and chains, 16 byte align, one extra "
  1644. * "frame for smid=0
  1645. */
  1646. ioc->chain_depth = ioc->chains_needed_per_io * ioc->request_depth;
  1647. sz = ((ioc->request_depth + 1 + ioc->chain_depth) * ioc->request_sz);
  1648. ioc->request_dma_sz = sz;
  1649. ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
  1650. if (!ioc->request) {
  1651. printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
  1652. "failed: req_depth(%d), chains_per_io(%d), frame_sz(%d), "
  1653. "total(%d kB)\n", ioc->name, ioc->request_depth,
  1654. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  1655. if (ioc->request_depth < MPT2SAS_SAS_QUEUE_DEPTH)
  1656. goto out;
  1657. retry_sz += 64;
  1658. ioc->request_depth = max_request_credit - retry_sz;
  1659. goto retry_allocation;
  1660. }
  1661. if (retry_sz)
  1662. printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
  1663. "succeed: req_depth(%d), chains_per_io(%d), frame_sz(%d), "
  1664. "total(%d kb)\n", ioc->name, ioc->request_depth,
  1665. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  1666. ioc->chain = ioc->request + ((ioc->request_depth + 1) *
  1667. ioc->request_sz);
  1668. ioc->chain_dma = ioc->request_dma + ((ioc->request_depth + 1) *
  1669. ioc->request_sz);
  1670. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
  1671. "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
  1672. ioc->request, ioc->request_depth, ioc->request_sz,
  1673. ((ioc->request_depth + 1) * ioc->request_sz)/1024));
  1674. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool(0x%p): depth"
  1675. "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->chain,
  1676. ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
  1677. ioc->request_sz))/1024));
  1678. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
  1679. ioc->name, (unsigned long long) ioc->request_dma));
  1680. total_sz += sz;
  1681. ioc->scsi_lookup = kcalloc(ioc->request_depth,
  1682. sizeof(struct request_tracker), GFP_KERNEL);
  1683. if (!ioc->scsi_lookup) {
  1684. printk(MPT2SAS_ERR_FMT "scsi_lookup: kcalloc failed\n",
  1685. ioc->name);
  1686. goto out;
  1687. }
  1688. /* initialize some bits */
  1689. for (i = 0; i < ioc->request_depth; i++)
  1690. ioc->scsi_lookup[i].smid = i + 1;
  1691. /* sense buffers, 4 byte align */
  1692. sz = ioc->request_depth * SCSI_SENSE_BUFFERSIZE;
  1693. ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
  1694. 0);
  1695. if (!ioc->sense_dma_pool) {
  1696. printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
  1697. ioc->name);
  1698. goto out;
  1699. }
  1700. ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
  1701. &ioc->sense_dma);
  1702. if (!ioc->sense) {
  1703. printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
  1704. ioc->name);
  1705. goto out;
  1706. }
  1707. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
  1708. "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
  1709. "(%d kB)\n", ioc->name, ioc->sense, ioc->request_depth,
  1710. SCSI_SENSE_BUFFERSIZE, sz/1024));
  1711. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
  1712. ioc->name, (unsigned long long)ioc->sense_dma));
  1713. total_sz += sz;
  1714. /* reply pool, 4 byte align */
  1715. sz = ioc->reply_free_queue_depth * ioc->reply_sz;
  1716. ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
  1717. 0);
  1718. if (!ioc->reply_dma_pool) {
  1719. printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
  1720. ioc->name);
  1721. goto out;
  1722. }
  1723. ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
  1724. &ioc->reply_dma);
  1725. if (!ioc->reply) {
  1726. printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
  1727. ioc->name);
  1728. goto out;
  1729. }
  1730. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
  1731. "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
  1732. ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
  1733. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
  1734. ioc->name, (unsigned long long)ioc->reply_dma));
  1735. total_sz += sz;
  1736. /* reply free queue, 16 byte align */
  1737. sz = ioc->reply_free_queue_depth * 4;
  1738. ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
  1739. ioc->pdev, sz, 16, 0);
  1740. if (!ioc->reply_free_dma_pool) {
  1741. printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
  1742. "failed\n", ioc->name);
  1743. goto out;
  1744. }
  1745. ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
  1746. &ioc->reply_free_dma);
  1747. if (!ioc->reply_free) {
  1748. printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
  1749. "failed\n", ioc->name);
  1750. goto out;
  1751. }
  1752. memset(ioc->reply_free, 0, sz);
  1753. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
  1754. "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
  1755. ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
  1756. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
  1757. "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
  1758. total_sz += sz;
  1759. /* reply post queue, 16 byte align */
  1760. sz = ioc->reply_post_queue_depth * sizeof(Mpi2DefaultReplyDescriptor_t);
  1761. ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
  1762. ioc->pdev, sz, 16, 0);
  1763. if (!ioc->reply_post_free_dma_pool) {
  1764. printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
  1765. "failed\n", ioc->name);
  1766. goto out;
  1767. }
  1768. ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
  1769. GFP_KERNEL, &ioc->reply_post_free_dma);
  1770. if (!ioc->reply_post_free) {
  1771. printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
  1772. "failed\n", ioc->name);
  1773. goto out;
  1774. }
  1775. memset(ioc->reply_post_free, 0, sz);
  1776. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
  1777. "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
  1778. ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
  1779. sz/1024));
  1780. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
  1781. "(0x%llx)\n", ioc->name, (unsigned long long)
  1782. ioc->reply_post_free_dma));
  1783. total_sz += sz;
  1784. ioc->config_page_sz = 512;
  1785. ioc->config_page = pci_alloc_consistent(ioc->pdev,
  1786. ioc->config_page_sz, &ioc->config_page_dma);
  1787. if (!ioc->config_page) {
  1788. printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
  1789. "failed\n", ioc->name);
  1790. goto out;
  1791. }
  1792. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
  1793. "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
  1794. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
  1795. "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
  1796. total_sz += ioc->config_page_sz;
  1797. printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
  1798. ioc->name, total_sz/1024);
  1799. printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
  1800. "Max Controller Queue Depth(%d)\n",
  1801. ioc->name, ioc->shost->can_queue, facts->RequestCredit);
  1802. printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
  1803. ioc->name, ioc->shost->sg_tablesize);
  1804. return 0;
  1805. out:
  1806. _base_release_memory_pools(ioc);
  1807. return -ENOMEM;
  1808. }
  1809. /**
  1810. * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
  1811. * @ioc: Pointer to MPT_ADAPTER structure
  1812. * @cooked: Request raw or cooked IOC state
  1813. *
  1814. * Returns all IOC Doorbell register bits if cooked==0, else just the
  1815. * Doorbell bits in MPI_IOC_STATE_MASK.
  1816. */
  1817. u32
  1818. mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
  1819. {
  1820. u32 s, sc;
  1821. s = readl(&ioc->chip->Doorbell);
  1822. sc = s & MPI2_IOC_STATE_MASK;
  1823. return cooked ? sc : s;
  1824. }
  1825. /**
  1826. * _base_wait_on_iocstate - waiting on a particular ioc state
  1827. * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
  1828. * @timeout: timeout in second
  1829. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  1830. *
  1831. * Returns 0 for success, non-zero for failure.
  1832. */
  1833. static int
  1834. _base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
  1835. int sleep_flag)
  1836. {
  1837. u32 count, cntdn;
  1838. u32 current_state;
  1839. count = 0;
  1840. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  1841. do {
  1842. current_state = mpt2sas_base_get_iocstate(ioc, 1);
  1843. if (current_state == ioc_state)
  1844. return 0;
  1845. if (count && current_state == MPI2_IOC_STATE_FAULT)
  1846. break;
  1847. if (sleep_flag == CAN_SLEEP)
  1848. msleep(1);
  1849. else
  1850. udelay(500);
  1851. count++;
  1852. } while (--cntdn);
  1853. return current_state;
  1854. }
  1855. /**
  1856. * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
  1857. * a write to the doorbell)
  1858. * @ioc: per adapter object
  1859. * @timeout: timeout in second
  1860. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  1861. *
  1862. * Returns 0 for success, non-zero for failure.
  1863. *
  1864. * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
  1865. */
  1866. static int
  1867. _base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
  1868. int sleep_flag)
  1869. {
  1870. u32 cntdn, count;
  1871. u32 int_status;
  1872. count = 0;
  1873. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  1874. do {
  1875. int_status = readl(&ioc->chip->HostInterruptStatus);
  1876. if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  1877. dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  1878. "successfull count(%d), timeout(%d)\n", ioc->name,
  1879. __func__, count, timeout));
  1880. return 0;
  1881. }
  1882. if (sleep_flag == CAN_SLEEP)
  1883. msleep(1);
  1884. else
  1885. udelay(500);
  1886. count++;
  1887. } while (--cntdn);
  1888. printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
  1889. "int_status(%x)!\n", ioc->name, __func__, count, int_status);
  1890. return -EFAULT;
  1891. }
  1892. /**
  1893. * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
  1894. * @ioc: per adapter object
  1895. * @timeout: timeout in second
  1896. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  1897. *
  1898. * Returns 0 for success, non-zero for failure.
  1899. *
  1900. * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
  1901. * doorbell.
  1902. */
  1903. static int
  1904. _base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
  1905. int sleep_flag)
  1906. {
  1907. u32 cntdn, count;
  1908. u32 int_status;
  1909. u32 doorbell;
  1910. count = 0;
  1911. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  1912. do {
  1913. int_status = readl(&ioc->chip->HostInterruptStatus);
  1914. if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
  1915. dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  1916. "successfull count(%d), timeout(%d)\n", ioc->name,
  1917. __func__, count, timeout));
  1918. return 0;
  1919. } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  1920. doorbell = readl(&ioc->chip->Doorbell);
  1921. if ((doorbell & MPI2_IOC_STATE_MASK) ==
  1922. MPI2_IOC_STATE_FAULT) {
  1923. mpt2sas_base_fault_info(ioc , doorbell);
  1924. return -EFAULT;
  1925. }
  1926. } else if (int_status == 0xFFFFFFFF)
  1927. goto out;
  1928. if (sleep_flag == CAN_SLEEP)
  1929. msleep(1);
  1930. else
  1931. udelay(500);
  1932. count++;
  1933. } while (--cntdn);
  1934. out:
  1935. printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
  1936. "int_status(%x)!\n", ioc->name, __func__, count, int_status);
  1937. return -EFAULT;
  1938. }
  1939. /**
  1940. * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
  1941. * @ioc: per adapter object
  1942. * @timeout: timeout in second
  1943. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  1944. *
  1945. * Returns 0 for success, non-zero for failure.
  1946. *
  1947. */
  1948. static int
  1949. _base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
  1950. int sleep_flag)
  1951. {
  1952. u32 cntdn, count;
  1953. u32 doorbell_reg;
  1954. count = 0;
  1955. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  1956. do {
  1957. doorbell_reg = readl(&ioc->chip->Doorbell);
  1958. if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
  1959. dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  1960. "successfull count(%d), timeout(%d)\n", ioc->name,
  1961. __func__, count, timeout));
  1962. return 0;
  1963. }
  1964. if (sleep_flag == CAN_SLEEP)
  1965. msleep(1);
  1966. else
  1967. udelay(500);
  1968. count++;
  1969. } while (--cntdn);
  1970. printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
  1971. "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
  1972. return -EFAULT;
  1973. }
  1974. /**
  1975. * _base_send_ioc_reset - send doorbell reset
  1976. * @ioc: per adapter object
  1977. * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
  1978. * @timeout: timeout in second
  1979. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  1980. *
  1981. * Returns 0 for success, non-zero for failure.
  1982. */
  1983. static int
  1984. _base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
  1985. int sleep_flag)
  1986. {
  1987. u32 ioc_state;
  1988. int r = 0;
  1989. if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
  1990. printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
  1991. ioc->name, __func__);
  1992. return -EFAULT;
  1993. }
  1994. if (!(ioc->facts.IOCCapabilities &
  1995. MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
  1996. return -EFAULT;
  1997. printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
  1998. writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
  1999. &ioc->chip->Doorbell);
  2000. if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
  2001. r = -EFAULT;
  2002. goto out;
  2003. }
  2004. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
  2005. timeout, sleep_flag);
  2006. if (ioc_state) {
  2007. printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
  2008. " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
  2009. r = -EFAULT;
  2010. goto out;
  2011. }
  2012. out:
  2013. printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
  2014. ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
  2015. return r;
  2016. }
  2017. /**
  2018. * _base_handshake_req_reply_wait - send request thru doorbell interface
  2019. * @ioc: per adapter object
  2020. * @request_bytes: request length
  2021. * @request: pointer having request payload
  2022. * @reply_bytes: reply length
  2023. * @reply: pointer to reply payload
  2024. * @timeout: timeout in second
  2025. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2026. *
  2027. * Returns 0 for success, non-zero for failure.
  2028. */
  2029. static int
  2030. _base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
  2031. u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
  2032. {
  2033. MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
  2034. int i;
  2035. u8 failed;
  2036. u16 dummy;
  2037. u32 *mfp;
  2038. /* make sure doorbell is not in use */
  2039. if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
  2040. printk(MPT2SAS_ERR_FMT "doorbell is in use "
  2041. " (line=%d)\n", ioc->name, __LINE__);
  2042. return -EFAULT;
  2043. }
  2044. /* clear pending doorbell interrupts from previous state changes */
  2045. if (readl(&ioc->chip->HostInterruptStatus) &
  2046. MPI2_HIS_IOC2SYS_DB_STATUS)
  2047. writel(0, &ioc->chip->HostInterruptStatus);
  2048. /* send message to ioc */
  2049. writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
  2050. ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
  2051. &ioc->chip->Doorbell);
  2052. if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
  2053. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2054. "int failed (line=%d)\n", ioc->name, __LINE__);
  2055. return -EFAULT;
  2056. }
  2057. writel(0, &ioc->chip->HostInterruptStatus);
  2058. if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
  2059. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2060. "ack failed (line=%d)\n", ioc->name, __LINE__);
  2061. return -EFAULT;
  2062. }
  2063. /* send message 32-bits at a time */
  2064. for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
  2065. writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
  2066. if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
  2067. failed = 1;
  2068. }
  2069. if (failed) {
  2070. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2071. "sending request failed (line=%d)\n", ioc->name, __LINE__);
  2072. return -EFAULT;
  2073. }
  2074. /* now wait for the reply */
  2075. if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
  2076. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2077. "int failed (line=%d)\n", ioc->name, __LINE__);
  2078. return -EFAULT;
  2079. }
  2080. /* read the first two 16-bits, it gives the total length of the reply */
  2081. reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2082. & MPI2_DOORBELL_DATA_MASK);
  2083. writel(0, &ioc->chip->HostInterruptStatus);
  2084. if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
  2085. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2086. "int failed (line=%d)\n", ioc->name, __LINE__);
  2087. return -EFAULT;
  2088. }
  2089. reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2090. & MPI2_DOORBELL_DATA_MASK);
  2091. writel(0, &ioc->chip->HostInterruptStatus);
  2092. for (i = 2; i < default_reply->MsgLength * 2; i++) {
  2093. if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
  2094. printk(MPT2SAS_ERR_FMT "doorbell "
  2095. "handshake int failed (line=%d)\n", ioc->name,
  2096. __LINE__);
  2097. return -EFAULT;
  2098. }
  2099. if (i >= reply_bytes/2) /* overflow case */
  2100. dummy = readl(&ioc->chip->Doorbell);
  2101. else
  2102. reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2103. & MPI2_DOORBELL_DATA_MASK);
  2104. writel(0, &ioc->chip->HostInterruptStatus);
  2105. }
  2106. _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
  2107. if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
  2108. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
  2109. " (line=%d)\n", ioc->name, __LINE__));
  2110. }
  2111. writel(0, &ioc->chip->HostInterruptStatus);
  2112. if (ioc->logging_level & MPT_DEBUG_INIT) {
  2113. mfp = (u32 *)reply;
  2114. printk(KERN_DEBUG "\toffset:data\n");
  2115. for (i = 0; i < reply_bytes/4; i++)
  2116. printk(KERN_DEBUG "\t[0x%02x]:%08x\n", i*4,
  2117. le32_to_cpu(mfp[i]));
  2118. }
  2119. return 0;
  2120. }
  2121. /**
  2122. * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
  2123. * @ioc: per adapter object
  2124. * @mpi_reply: the reply payload from FW
  2125. * @mpi_request: the request payload sent to FW
  2126. *
  2127. * The SAS IO Unit Control Request message allows the host to perform low-level
  2128. * operations, such as resets on the PHYs of the IO Unit, also allows the host
  2129. * to obtain the IOC assigned device handles for a device if it has other
  2130. * identifying information about the device, in addition allows the host to
  2131. * remove IOC resources associated with the device.
  2132. *
  2133. * Returns 0 for success, non-zero for failure.
  2134. */
  2135. int
  2136. mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
  2137. Mpi2SasIoUnitControlReply_t *mpi_reply,
  2138. Mpi2SasIoUnitControlRequest_t *mpi_request)
  2139. {
  2140. u16 smid;
  2141. u32 ioc_state;
  2142. unsigned long timeleft;
  2143. u8 issue_reset;
  2144. int rc;
  2145. void *request;
  2146. u16 wait_state_count;
  2147. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
  2148. __func__));
  2149. mutex_lock(&ioc->base_cmds.mutex);
  2150. if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
  2151. printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
  2152. ioc->name, __func__);
  2153. rc = -EAGAIN;
  2154. goto out;
  2155. }
  2156. wait_state_count = 0;
  2157. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2158. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  2159. if (wait_state_count++ == 10) {
  2160. printk(MPT2SAS_ERR_FMT
  2161. "%s: failed due to ioc not operational\n",
  2162. ioc->name, __func__);
  2163. rc = -EFAULT;
  2164. goto out;
  2165. }
  2166. ssleep(1);
  2167. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2168. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  2169. "operational state(count=%d)\n", ioc->name,
  2170. __func__, wait_state_count);
  2171. }
  2172. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  2173. if (!smid) {
  2174. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2175. ioc->name, __func__);
  2176. rc = -EAGAIN;
  2177. goto out;
  2178. }
  2179. rc = 0;
  2180. ioc->base_cmds.status = MPT2_CMD_PENDING;
  2181. request = mpt2sas_base_get_msg_frame(ioc, smid);
  2182. ioc->base_cmds.smid = smid;
  2183. memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
  2184. if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  2185. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
  2186. ioc->ioc_link_reset_in_progress = 1;
  2187. mpt2sas_base_put_smid_default(ioc, smid);
  2188. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  2189. msecs_to_jiffies(10000));
  2190. if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  2191. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
  2192. ioc->ioc_link_reset_in_progress)
  2193. ioc->ioc_link_reset_in_progress = 0;
  2194. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  2195. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  2196. ioc->name, __func__);
  2197. _debug_dump_mf(mpi_request,
  2198. sizeof(Mpi2SasIoUnitControlRequest_t)/4);
  2199. if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
  2200. issue_reset = 1;
  2201. goto issue_host_reset;
  2202. }
  2203. if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
  2204. memcpy(mpi_reply, ioc->base_cmds.reply,
  2205. sizeof(Mpi2SasIoUnitControlReply_t));
  2206. else
  2207. memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  2208. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2209. goto out;
  2210. issue_host_reset:
  2211. if (issue_reset)
  2212. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  2213. FORCE_BIG_HAMMER);
  2214. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2215. rc = -EFAULT;
  2216. out:
  2217. mutex_unlock(&ioc->base_cmds.mutex);
  2218. return rc;
  2219. }
  2220. /**
  2221. * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
  2222. * @ioc: per adapter object
  2223. * @mpi_reply: the reply payload from FW
  2224. * @mpi_request: the request payload sent to FW
  2225. *
  2226. * The SCSI Enclosure Processor request message causes the IOC to
  2227. * communicate with SES devices to control LED status signals.
  2228. *
  2229. * Returns 0 for success, non-zero for failure.
  2230. */
  2231. int
  2232. mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
  2233. Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
  2234. {
  2235. u16 smid;
  2236. u32 ioc_state;
  2237. unsigned long timeleft;
  2238. u8 issue_reset;
  2239. int rc;
  2240. void *request;
  2241. u16 wait_state_count;
  2242. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
  2243. __func__));
  2244. mutex_lock(&ioc->base_cmds.mutex);
  2245. if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
  2246. printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
  2247. ioc->name, __func__);
  2248. rc = -EAGAIN;
  2249. goto out;
  2250. }
  2251. wait_state_count = 0;
  2252. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2253. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  2254. if (wait_state_count++ == 10) {
  2255. printk(MPT2SAS_ERR_FMT
  2256. "%s: failed due to ioc not operational\n",
  2257. ioc->name, __func__);
  2258. rc = -EFAULT;
  2259. goto out;
  2260. }
  2261. ssleep(1);
  2262. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2263. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  2264. "operational state(count=%d)\n", ioc->name,
  2265. __func__, wait_state_count);
  2266. }
  2267. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  2268. if (!smid) {
  2269. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2270. ioc->name, __func__);
  2271. rc = -EAGAIN;
  2272. goto out;
  2273. }
  2274. rc = 0;
  2275. ioc->base_cmds.status = MPT2_CMD_PENDING;
  2276. request = mpt2sas_base_get_msg_frame(ioc, smid);
  2277. ioc->base_cmds.smid = smid;
  2278. memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
  2279. mpt2sas_base_put_smid_default(ioc, smid);
  2280. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  2281. msecs_to_jiffies(10000));
  2282. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  2283. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  2284. ioc->name, __func__);
  2285. _debug_dump_mf(mpi_request,
  2286. sizeof(Mpi2SepRequest_t)/4);
  2287. if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
  2288. issue_reset = 1;
  2289. goto issue_host_reset;
  2290. }
  2291. if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
  2292. memcpy(mpi_reply, ioc->base_cmds.reply,
  2293. sizeof(Mpi2SepReply_t));
  2294. else
  2295. memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
  2296. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2297. goto out;
  2298. issue_host_reset:
  2299. if (issue_reset)
  2300. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  2301. FORCE_BIG_HAMMER);
  2302. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2303. rc = -EFAULT;
  2304. out:
  2305. mutex_unlock(&ioc->base_cmds.mutex);
  2306. return rc;
  2307. }
  2308. /**
  2309. * _base_get_port_facts - obtain port facts reply and save in ioc
  2310. * @ioc: per adapter object
  2311. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2312. *
  2313. * Returns 0 for success, non-zero for failure.
  2314. */
  2315. static int
  2316. _base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
  2317. {
  2318. Mpi2PortFactsRequest_t mpi_request;
  2319. Mpi2PortFactsReply_t mpi_reply, *pfacts;
  2320. int mpi_reply_sz, mpi_request_sz, r;
  2321. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
  2322. __func__));
  2323. mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
  2324. mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
  2325. memset(&mpi_request, 0, mpi_request_sz);
  2326. mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
  2327. mpi_request.PortNumber = port;
  2328. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  2329. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
  2330. if (r != 0) {
  2331. printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
  2332. ioc->name, __func__, r);
  2333. return r;
  2334. }
  2335. pfacts = &ioc->pfacts[port];
  2336. memset(pfacts, 0, sizeof(Mpi2PortFactsReply_t));
  2337. pfacts->PortNumber = mpi_reply.PortNumber;
  2338. pfacts->VP_ID = mpi_reply.VP_ID;
  2339. pfacts->VF_ID = mpi_reply.VF_ID;
  2340. pfacts->MaxPostedCmdBuffers =
  2341. le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
  2342. return 0;
  2343. }
  2344. /**
  2345. * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
  2346. * @ioc: per adapter object
  2347. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2348. *
  2349. * Returns 0 for success, non-zero for failure.
  2350. */
  2351. static int
  2352. _base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  2353. {
  2354. Mpi2IOCFactsRequest_t mpi_request;
  2355. Mpi2IOCFactsReply_t mpi_reply, *facts;
  2356. int mpi_reply_sz, mpi_request_sz, r;
  2357. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
  2358. __func__));
  2359. mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
  2360. mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
  2361. memset(&mpi_request, 0, mpi_request_sz);
  2362. mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
  2363. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  2364. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
  2365. if (r != 0) {
  2366. printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
  2367. ioc->name, __func__, r);
  2368. return r;
  2369. }
  2370. facts = &ioc->facts;
  2371. memset(facts, 0, sizeof(Mpi2IOCFactsReply_t));
  2372. facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
  2373. facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
  2374. facts->VP_ID = mpi_reply.VP_ID;
  2375. facts->VF_ID = mpi_reply.VF_ID;
  2376. facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
  2377. facts->MaxChainDepth = mpi_reply.MaxChainDepth;
  2378. facts->WhoInit = mpi_reply.WhoInit;
  2379. facts->NumberOfPorts = mpi_reply.NumberOfPorts;
  2380. facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
  2381. facts->MaxReplyDescriptorPostQueueDepth =
  2382. le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
  2383. facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
  2384. facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
  2385. if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
  2386. ioc->ir_firmware = 1;
  2387. facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
  2388. facts->IOCRequestFrameSize =
  2389. le16_to_cpu(mpi_reply.IOCRequestFrameSize);
  2390. facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
  2391. facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
  2392. ioc->shost->max_id = -1;
  2393. facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
  2394. facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
  2395. facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
  2396. facts->HighPriorityCredit =
  2397. le16_to_cpu(mpi_reply.HighPriorityCredit);
  2398. facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
  2399. facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
  2400. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
  2401. "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
  2402. facts->MaxChainDepth));
  2403. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
  2404. "reply frame size(%d)\n", ioc->name,
  2405. facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
  2406. return 0;
  2407. }
  2408. /**
  2409. * _base_send_ioc_init - send ioc_init to firmware
  2410. * @ioc: per adapter object
  2411. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2412. *
  2413. * Returns 0 for success, non-zero for failure.
  2414. */
  2415. static int
  2416. _base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  2417. {
  2418. Mpi2IOCInitRequest_t mpi_request;
  2419. Mpi2IOCInitReply_t mpi_reply;
  2420. int r;
  2421. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
  2422. __func__));
  2423. memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
  2424. mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
  2425. mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  2426. mpi_request.VF_ID = 0; /* TODO */
  2427. mpi_request.VP_ID = 0;
  2428. mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
  2429. mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
  2430. /* In MPI Revision I (0xA), the SystemReplyFrameSize(offset 0x18) was
  2431. * removed and made reserved. For those with older firmware will need
  2432. * this fix. It was decided that the Reply and Request frame sizes are
  2433. * the same.
  2434. */
  2435. if ((ioc->facts.HeaderVersion >> 8) < 0xA) {
  2436. mpi_request.Reserved7 = cpu_to_le16(ioc->reply_sz);
  2437. /* mpi_request.SystemReplyFrameSize =
  2438. * cpu_to_le16(ioc->reply_sz);
  2439. */
  2440. }
  2441. mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
  2442. mpi_request.ReplyDescriptorPostQueueDepth =
  2443. cpu_to_le16(ioc->reply_post_queue_depth);
  2444. mpi_request.ReplyFreeQueueDepth =
  2445. cpu_to_le16(ioc->reply_free_queue_depth);
  2446. #if BITS_PER_LONG > 32
  2447. mpi_request.SenseBufferAddressHigh =
  2448. cpu_to_le32(ioc->sense_dma >> 32);
  2449. mpi_request.SystemReplyAddressHigh =
  2450. cpu_to_le32(ioc->reply_dma >> 32);
  2451. mpi_request.SystemRequestFrameBaseAddress =
  2452. cpu_to_le64(ioc->request_dma);
  2453. mpi_request.ReplyFreeQueueAddress =
  2454. cpu_to_le64(ioc->reply_free_dma);
  2455. mpi_request.ReplyDescriptorPostQueueAddress =
  2456. cpu_to_le64(ioc->reply_post_free_dma);
  2457. #else
  2458. mpi_request.SystemRequestFrameBaseAddress =
  2459. cpu_to_le32(ioc->request_dma);
  2460. mpi_request.ReplyFreeQueueAddress =
  2461. cpu_to_le32(ioc->reply_free_dma);
  2462. mpi_request.ReplyDescriptorPostQueueAddress =
  2463. cpu_to_le32(ioc->reply_post_free_dma);
  2464. #endif
  2465. if (ioc->logging_level & MPT_DEBUG_INIT) {
  2466. u32 *mfp;
  2467. int i;
  2468. mfp = (u32 *)&mpi_request;
  2469. printk(KERN_DEBUG "\toffset:data\n");
  2470. for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
  2471. printk(KERN_DEBUG "\t[0x%02x]:%08x\n", i*4,
  2472. le32_to_cpu(mfp[i]));
  2473. }
  2474. r = _base_handshake_req_reply_wait(ioc,
  2475. sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
  2476. sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
  2477. sleep_flag);
  2478. if (r != 0) {
  2479. printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
  2480. ioc->name, __func__, r);
  2481. return r;
  2482. }
  2483. if (mpi_reply.IOCStatus != MPI2_IOCSTATUS_SUCCESS ||
  2484. mpi_reply.IOCLogInfo) {
  2485. printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
  2486. r = -EIO;
  2487. }
  2488. return 0;
  2489. }
  2490. /**
  2491. * _base_send_port_enable - send port_enable(discovery stuff) to firmware
  2492. * @ioc: per adapter object
  2493. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2494. *
  2495. * Returns 0 for success, non-zero for failure.
  2496. */
  2497. static int
  2498. _base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  2499. {
  2500. Mpi2PortEnableRequest_t *mpi_request;
  2501. u32 ioc_state;
  2502. unsigned long timeleft;
  2503. int r = 0;
  2504. u16 smid;
  2505. printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
  2506. if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
  2507. printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
  2508. ioc->name, __func__);
  2509. return -EAGAIN;
  2510. }
  2511. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  2512. if (!smid) {
  2513. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2514. ioc->name, __func__);
  2515. return -EAGAIN;
  2516. }
  2517. ioc->base_cmds.status = MPT2_CMD_PENDING;
  2518. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2519. ioc->base_cmds.smid = smid;
  2520. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  2521. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  2522. mpi_request->VF_ID = 0; /* TODO */
  2523. mpi_request->VP_ID = 0;
  2524. mpt2sas_base_put_smid_default(ioc, smid);
  2525. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  2526. 300*HZ);
  2527. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  2528. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  2529. ioc->name, __func__);
  2530. _debug_dump_mf(mpi_request,
  2531. sizeof(Mpi2PortEnableRequest_t)/4);
  2532. if (ioc->base_cmds.status & MPT2_CMD_RESET)
  2533. r = -EFAULT;
  2534. else
  2535. r = -ETIME;
  2536. goto out;
  2537. } else
  2538. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: complete\n",
  2539. ioc->name, __func__));
  2540. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_OPERATIONAL,
  2541. 60, sleep_flag);
  2542. if (ioc_state) {
  2543. printk(MPT2SAS_ERR_FMT "%s: failed going to operational state "
  2544. " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
  2545. r = -EFAULT;
  2546. }
  2547. out:
  2548. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2549. printk(MPT2SAS_INFO_FMT "port enable: %s\n",
  2550. ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
  2551. return r;
  2552. }
  2553. /**
  2554. * _base_unmask_events - turn on notification for this event
  2555. * @ioc: per adapter object
  2556. * @event: firmware event
  2557. *
  2558. * The mask is stored in ioc->event_masks.
  2559. */
  2560. static void
  2561. _base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
  2562. {
  2563. u32 desired_event;
  2564. if (event >= 128)
  2565. return;
  2566. desired_event = (1 << (event % 32));
  2567. if (event < 32)
  2568. ioc->event_masks[0] &= ~desired_event;
  2569. else if (event < 64)
  2570. ioc->event_masks[1] &= ~desired_event;
  2571. else if (event < 96)
  2572. ioc->event_masks[2] &= ~desired_event;
  2573. else if (event < 128)
  2574. ioc->event_masks[3] &= ~desired_event;
  2575. }
  2576. /**
  2577. * _base_event_notification - send event notification
  2578. * @ioc: per adapter object
  2579. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2580. *
  2581. * Returns 0 for success, non-zero for failure.
  2582. */
  2583. static int
  2584. _base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  2585. {
  2586. Mpi2EventNotificationRequest_t *mpi_request;
  2587. unsigned long timeleft;
  2588. u16 smid;
  2589. int r = 0;
  2590. int i;
  2591. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
  2592. __func__));
  2593. if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
  2594. printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
  2595. ioc->name, __func__);
  2596. return -EAGAIN;
  2597. }
  2598. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  2599. if (!smid) {
  2600. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2601. ioc->name, __func__);
  2602. return -EAGAIN;
  2603. }
  2604. ioc->base_cmds.status = MPT2_CMD_PENDING;
  2605. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2606. ioc->base_cmds.smid = smid;
  2607. memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
  2608. mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  2609. mpi_request->VF_ID = 0; /* TODO */
  2610. mpi_request->VP_ID = 0;
  2611. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  2612. mpi_request->EventMasks[i] =
  2613. le32_to_cpu(ioc->event_masks[i]);
  2614. mpt2sas_base_put_smid_default(ioc, smid);
  2615. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
  2616. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  2617. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  2618. ioc->name, __func__);
  2619. _debug_dump_mf(mpi_request,
  2620. sizeof(Mpi2EventNotificationRequest_t)/4);
  2621. if (ioc->base_cmds.status & MPT2_CMD_RESET)
  2622. r = -EFAULT;
  2623. else
  2624. r = -ETIME;
  2625. } else
  2626. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: complete\n",
  2627. ioc->name, __func__));
  2628. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2629. return r;
  2630. }
  2631. /**
  2632. * mpt2sas_base_validate_event_type - validating event types
  2633. * @ioc: per adapter object
  2634. * @event: firmware event
  2635. *
  2636. * This will turn on firmware event notification when application
  2637. * ask for that event. We don't mask events that are already enabled.
  2638. */
  2639. void
  2640. mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
  2641. {
  2642. int i, j;
  2643. u32 event_mask, desired_event;
  2644. u8 send_update_to_fw;
  2645. for (i = 0, send_update_to_fw = 0; i <
  2646. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
  2647. event_mask = ~event_type[i];
  2648. desired_event = 1;
  2649. for (j = 0; j < 32; j++) {
  2650. if (!(event_mask & desired_event) &&
  2651. (ioc->event_masks[i] & desired_event)) {
  2652. ioc->event_masks[i] &= ~desired_event;
  2653. send_update_to_fw = 1;
  2654. }
  2655. desired_event = (desired_event << 1);
  2656. }
  2657. }
  2658. if (!send_update_to_fw)
  2659. return;
  2660. mutex_lock(&ioc->base_cmds.mutex);
  2661. _base_event_notification(ioc, CAN_SLEEP);
  2662. mutex_unlock(&ioc->base_cmds.mutex);
  2663. }
  2664. /**
  2665. * _base_diag_reset - the "big hammer" start of day reset
  2666. * @ioc: per adapter object
  2667. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2668. *
  2669. * Returns 0 for success, non-zero for failure.
  2670. */
  2671. static int
  2672. _base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  2673. {
  2674. u32 host_diagnostic;
  2675. u32 ioc_state;
  2676. u32 count;
  2677. u32 hcb_size;
  2678. printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
  2679. _base_save_msix_table(ioc);
  2680. drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "clear interrupts\n",
  2681. ioc->name));
  2682. writel(0, &ioc->chip->HostInterruptStatus);
  2683. count = 0;
  2684. do {
  2685. /* Write magic sequence to WriteSequence register
  2686. * Loop until in diagnostic mode
  2687. */
  2688. drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "write magic "
  2689. "sequence\n", ioc->name));
  2690. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  2691. writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
  2692. writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
  2693. writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
  2694. writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
  2695. writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
  2696. writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
  2697. /* wait 100 msec */
  2698. if (sleep_flag == CAN_SLEEP)
  2699. msleep(100);
  2700. else
  2701. mdelay(100);
  2702. if (count++ > 20)
  2703. goto out;
  2704. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  2705. drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "wrote magic "
  2706. "sequence: count(%d), host_diagnostic(0x%08x)\n",
  2707. ioc->name, count, host_diagnostic));
  2708. } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
  2709. hcb_size = readl(&ioc->chip->HCBSize);
  2710. drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "diag reset: issued\n",
  2711. ioc->name));
  2712. writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
  2713. &ioc->chip->HostDiagnostic);
  2714. /* don't access any registers for 50 milliseconds */
  2715. msleep(50);
  2716. /* 300 second max wait */
  2717. for (count = 0; count < 3000000 ; count++) {
  2718. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  2719. if (host_diagnostic == 0xFFFFFFFF)
  2720. goto out;
  2721. if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
  2722. break;
  2723. /* wait 100 msec */
  2724. if (sleep_flag == CAN_SLEEP)
  2725. msleep(1);
  2726. else
  2727. mdelay(1);
  2728. }
  2729. if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
  2730. drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "restart the adapter "
  2731. "assuming the HCB Address points to good F/W\n",
  2732. ioc->name));
  2733. host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
  2734. host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
  2735. writel(host_diagnostic, &ioc->chip->HostDiagnostic);
  2736. drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  2737. "re-enable the HCDW\n", ioc->name));
  2738. writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
  2739. &ioc->chip->HCBSize);
  2740. }
  2741. drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "restart the adapter\n",
  2742. ioc->name));
  2743. writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
  2744. &ioc->chip->HostDiagnostic);
  2745. drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "disable writes to the "
  2746. "diagnostic register\n", ioc->name));
  2747. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  2748. drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "Wait for FW to go to the "
  2749. "READY state\n", ioc->name));
  2750. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
  2751. sleep_flag);
  2752. if (ioc_state) {
  2753. printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
  2754. " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
  2755. goto out;
  2756. }
  2757. _base_restore_msix_table(ioc);
  2758. printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
  2759. return 0;
  2760. out:
  2761. printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
  2762. return -EFAULT;
  2763. }
  2764. /**
  2765. * _base_make_ioc_ready - put controller in READY state
  2766. * @ioc: per adapter object
  2767. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2768. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  2769. *
  2770. * Returns 0 for success, non-zero for failure.
  2771. */
  2772. static int
  2773. _base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
  2774. enum reset_type type)
  2775. {
  2776. u32 ioc_state;
  2777. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
  2778. __func__));
  2779. ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
  2780. dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: ioc_state(0x%08x)\n",
  2781. ioc->name, __func__, ioc_state));
  2782. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
  2783. return 0;
  2784. if (ioc_state & MPI2_DOORBELL_USED) {
  2785. dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
  2786. "active!\n", ioc->name));
  2787. goto issue_diag_reset;
  2788. }
  2789. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  2790. mpt2sas_base_fault_info(ioc, ioc_state &
  2791. MPI2_DOORBELL_DATA_MASK);
  2792. goto issue_diag_reset;
  2793. }
  2794. if (type == FORCE_BIG_HAMMER)
  2795. goto issue_diag_reset;
  2796. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
  2797. if (!(_base_send_ioc_reset(ioc,
  2798. MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP)))
  2799. return 0;
  2800. issue_diag_reset:
  2801. return _base_diag_reset(ioc, CAN_SLEEP);
  2802. }
  2803. /**
  2804. * _base_make_ioc_operational - put controller in OPERATIONAL state
  2805. * @ioc: per adapter object
  2806. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2807. *
  2808. * Returns 0 for success, non-zero for failure.
  2809. */
  2810. static int
  2811. _base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  2812. {
  2813. int r, i;
  2814. unsigned long flags;
  2815. u32 reply_address;
  2816. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
  2817. __func__));
  2818. /* initialize the scsi lookup free list */
  2819. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2820. INIT_LIST_HEAD(&ioc->free_list);
  2821. for (i = 0; i < ioc->request_depth; i++) {
  2822. ioc->scsi_lookup[i].cb_idx = 0xFF;
  2823. list_add_tail(&ioc->scsi_lookup[i].tracker_list,
  2824. &ioc->free_list);
  2825. }
  2826. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2827. /* initialize Reply Free Queue */
  2828. for (i = 0, reply_address = (u32)ioc->reply_dma ;
  2829. i < ioc->reply_free_queue_depth ; i++, reply_address +=
  2830. ioc->reply_sz)
  2831. ioc->reply_free[i] = cpu_to_le32(reply_address);
  2832. /* initialize Reply Post Free Queue */
  2833. for (i = 0; i < ioc->reply_post_queue_depth; i++)
  2834. ioc->reply_post_free[i].Words = ULLONG_MAX;
  2835. r = _base_send_ioc_init(ioc, sleep_flag);
  2836. if (r)
  2837. return r;
  2838. /* initialize the index's */
  2839. ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
  2840. ioc->reply_post_host_index = 0;
  2841. writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
  2842. writel(0, &ioc->chip->ReplyPostHostIndex);
  2843. _base_unmask_interrupts(ioc);
  2844. r = _base_event_notification(ioc, sleep_flag);
  2845. if (r)
  2846. return r;
  2847. if (sleep_flag == CAN_SLEEP)
  2848. _base_static_config_pages(ioc);
  2849. r = _base_send_port_enable(ioc, sleep_flag);
  2850. if (r)
  2851. return r;
  2852. return r;
  2853. }
  2854. /**
  2855. * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
  2856. * @ioc: per adapter object
  2857. *
  2858. * Return nothing.
  2859. */
  2860. void
  2861. mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
  2862. {
  2863. struct pci_dev *pdev = ioc->pdev;
  2864. dexitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
  2865. __func__));
  2866. _base_mask_interrupts(ioc);
  2867. _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
  2868. if (ioc->pci_irq) {
  2869. synchronize_irq(pdev->irq);
  2870. free_irq(ioc->pci_irq, ioc);
  2871. }
  2872. _base_disable_msix(ioc);
  2873. if (ioc->chip_phys)
  2874. iounmap(ioc->chip);
  2875. ioc->pci_irq = -1;
  2876. ioc->chip_phys = 0;
  2877. pci_release_selected_regions(ioc->pdev, ioc->bars);
  2878. pci_disable_device(pdev);
  2879. return;
  2880. }
  2881. /**
  2882. * mpt2sas_base_attach - attach controller instance
  2883. * @ioc: per adapter object
  2884. *
  2885. * Returns 0 for success, non-zero for failure.
  2886. */
  2887. int
  2888. mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
  2889. {
  2890. int r, i;
  2891. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
  2892. __func__));
  2893. r = mpt2sas_base_map_resources(ioc);
  2894. if (r)
  2895. return r;
  2896. pci_set_drvdata(ioc->pdev, ioc->shost);
  2897. r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
  2898. if (r)
  2899. goto out_free_resources;
  2900. r = _base_get_ioc_facts(ioc, CAN_SLEEP);
  2901. if (r)
  2902. goto out_free_resources;
  2903. r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
  2904. if (r)
  2905. goto out_free_resources;
  2906. init_waitqueue_head(&ioc->reset_wq);
  2907. /* base internal command bits */
  2908. mutex_init(&ioc->base_cmds.mutex);
  2909. init_completion(&ioc->base_cmds.done);
  2910. ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  2911. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2912. /* transport internal command bits */
  2913. ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  2914. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  2915. mutex_init(&ioc->transport_cmds.mutex);
  2916. init_completion(&ioc->transport_cmds.done);
  2917. /* task management internal command bits */
  2918. ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  2919. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  2920. mutex_init(&ioc->tm_cmds.mutex);
  2921. /* config page internal command bits */
  2922. ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  2923. ioc->config_cmds.status = MPT2_CMD_NOT_USED;
  2924. mutex_init(&ioc->config_cmds.mutex);
  2925. /* ctl module internal command bits */
  2926. ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  2927. ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
  2928. mutex_init(&ioc->ctl_cmds.mutex);
  2929. init_completion(&ioc->ctl_cmds.done);
  2930. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  2931. ioc->event_masks[i] = -1;
  2932. /* here we enable the events we care about */
  2933. _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
  2934. _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
  2935. _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
  2936. _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  2937. _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
  2938. _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
  2939. _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
  2940. _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
  2941. _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
  2942. _base_unmask_events(ioc, MPI2_EVENT_TASK_SET_FULL);
  2943. _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
  2944. ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
  2945. sizeof(Mpi2PortFactsReply_t), GFP_KERNEL);
  2946. if (!ioc->pfacts)
  2947. goto out_free_resources;
  2948. for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
  2949. r = _base_get_port_facts(ioc, i, CAN_SLEEP);
  2950. if (r)
  2951. goto out_free_resources;
  2952. }
  2953. r = _base_make_ioc_operational(ioc, CAN_SLEEP);
  2954. if (r)
  2955. goto out_free_resources;
  2956. mpt2sas_base_start_watchdog(ioc);
  2957. return 0;
  2958. out_free_resources:
  2959. ioc->remove_host = 1;
  2960. mpt2sas_base_free_resources(ioc);
  2961. _base_release_memory_pools(ioc);
  2962. pci_set_drvdata(ioc->pdev, NULL);
  2963. kfree(ioc->tm_cmds.reply);
  2964. kfree(ioc->transport_cmds.reply);
  2965. kfree(ioc->config_cmds.reply);
  2966. kfree(ioc->base_cmds.reply);
  2967. kfree(ioc->ctl_cmds.reply);
  2968. kfree(ioc->pfacts);
  2969. ioc->ctl_cmds.reply = NULL;
  2970. ioc->base_cmds.reply = NULL;
  2971. ioc->tm_cmds.reply = NULL;
  2972. ioc->transport_cmds.reply = NULL;
  2973. ioc->config_cmds.reply = NULL;
  2974. ioc->pfacts = NULL;
  2975. return r;
  2976. }
  2977. /**
  2978. * mpt2sas_base_detach - remove controller instance
  2979. * @ioc: per adapter object
  2980. *
  2981. * Return nothing.
  2982. */
  2983. void
  2984. mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
  2985. {
  2986. dexitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
  2987. __func__));
  2988. mpt2sas_base_stop_watchdog(ioc);
  2989. mpt2sas_base_free_resources(ioc);
  2990. _base_release_memory_pools(ioc);
  2991. pci_set_drvdata(ioc->pdev, NULL);
  2992. kfree(ioc->pfacts);
  2993. kfree(ioc->ctl_cmds.reply);
  2994. kfree(ioc->base_cmds.reply);
  2995. kfree(ioc->tm_cmds.reply);
  2996. kfree(ioc->transport_cmds.reply);
  2997. kfree(ioc->config_cmds.reply);
  2998. }
  2999. /**
  3000. * _base_reset_handler - reset callback handler (for base)
  3001. * @ioc: per adapter object
  3002. * @reset_phase: phase
  3003. *
  3004. * The handler for doing any required cleanup or initialization.
  3005. *
  3006. * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
  3007. * MPT2_IOC_DONE_RESET
  3008. *
  3009. * Return nothing.
  3010. */
  3011. static void
  3012. _base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
  3013. {
  3014. switch (reset_phase) {
  3015. case MPT2_IOC_PRE_RESET:
  3016. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  3017. "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
  3018. break;
  3019. case MPT2_IOC_AFTER_RESET:
  3020. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  3021. "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
  3022. if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
  3023. ioc->transport_cmds.status |= MPT2_CMD_RESET;
  3024. mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
  3025. complete(&ioc->transport_cmds.done);
  3026. }
  3027. if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
  3028. ioc->base_cmds.status |= MPT2_CMD_RESET;
  3029. mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
  3030. complete(&ioc->base_cmds.done);
  3031. }
  3032. if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
  3033. ioc->config_cmds.status |= MPT2_CMD_RESET;
  3034. mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
  3035. ioc->config_cmds.smid = USHORT_MAX;
  3036. complete(&ioc->config_cmds.done);
  3037. }
  3038. break;
  3039. case MPT2_IOC_DONE_RESET:
  3040. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  3041. "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
  3042. break;
  3043. }
  3044. mpt2sas_scsih_reset_handler(ioc, reset_phase);
  3045. mpt2sas_ctl_reset_handler(ioc, reset_phase);
  3046. }
  3047. /**
  3048. * _wait_for_commands_to_complete - reset controller
  3049. * @ioc: Pointer to MPT_ADAPTER structure
  3050. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3051. *
  3052. * This function waiting(3s) for all pending commands to complete
  3053. * prior to putting controller in reset.
  3054. */
  3055. static void
  3056. _wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3057. {
  3058. u32 ioc_state;
  3059. unsigned long flags;
  3060. u16 i;
  3061. ioc->pending_io_count = 0;
  3062. if (sleep_flag != CAN_SLEEP)
  3063. return;
  3064. ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
  3065. if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
  3066. return;
  3067. /* pending command count */
  3068. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3069. for (i = 0; i < ioc->request_depth; i++)
  3070. if (ioc->scsi_lookup[i].cb_idx != 0xFF)
  3071. ioc->pending_io_count++;
  3072. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3073. if (!ioc->pending_io_count)
  3074. return;
  3075. /* wait for pending commands to complete */
  3076. wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 3 * HZ);
  3077. }
  3078. /**
  3079. * mpt2sas_base_hard_reset_handler - reset controller
  3080. * @ioc: Pointer to MPT_ADAPTER structure
  3081. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3082. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  3083. *
  3084. * Returns 0 for success, non-zero for failure.
  3085. */
  3086. int
  3087. mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
  3088. enum reset_type type)
  3089. {
  3090. int r;
  3091. unsigned long flags;
  3092. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
  3093. __func__));
  3094. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  3095. if (ioc->shost_recovery) {
  3096. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  3097. printk(MPT2SAS_ERR_FMT "%s: busy\n",
  3098. ioc->name, __func__);
  3099. return -EBUSY;
  3100. }
  3101. ioc->shost_recovery = 1;
  3102. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  3103. _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
  3104. _wait_for_commands_to_complete(ioc, sleep_flag);
  3105. _base_mask_interrupts(ioc);
  3106. r = _base_make_ioc_ready(ioc, sleep_flag, type);
  3107. if (r)
  3108. goto out;
  3109. _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
  3110. r = _base_make_ioc_operational(ioc, sleep_flag);
  3111. if (!r)
  3112. _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
  3113. out:
  3114. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: %s\n",
  3115. ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
  3116. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  3117. ioc->shost_recovery = 0;
  3118. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  3119. if (!r)
  3120. _base_reset_handler(ioc, MPT2_IOC_RUNNING);
  3121. return r;
  3122. }