megaraid_mbox.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146
  1. /*
  2. *
  3. * Linux MegaRAID device driver
  4. *
  5. * Copyright (c) 2003-2004 LSI Logic Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * FILE : megaraid_mbox.c
  13. * Version : v2.20.5.1 (Nov 16 2006)
  14. *
  15. * Authors:
  16. * Atul Mukker <Atul.Mukker@lsi.com>
  17. * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsi.com>
  18. * Manoj Jose <Manoj.Jose@lsi.com>
  19. * Seokmann Ju
  20. *
  21. * List of supported controllers
  22. *
  23. * OEM Product Name VID DID SSVID SSID
  24. * --- ------------ --- --- ---- ----
  25. * Dell PERC3/QC 101E 1960 1028 0471
  26. * Dell PERC3/DC 101E 1960 1028 0493
  27. * Dell PERC3/SC 101E 1960 1028 0475
  28. * Dell PERC3/Di 1028 1960 1028 0123
  29. * Dell PERC4/SC 1000 1960 1028 0520
  30. * Dell PERC4/DC 1000 1960 1028 0518
  31. * Dell PERC4/QC 1000 0407 1028 0531
  32. * Dell PERC4/Di 1028 000F 1028 014A
  33. * Dell PERC 4e/Si 1028 0013 1028 016c
  34. * Dell PERC 4e/Di 1028 0013 1028 016d
  35. * Dell PERC 4e/Di 1028 0013 1028 016e
  36. * Dell PERC 4e/Di 1028 0013 1028 016f
  37. * Dell PERC 4e/Di 1028 0013 1028 0170
  38. * Dell PERC 4e/DC 1000 0408 1028 0002
  39. * Dell PERC 4e/SC 1000 0408 1028 0001
  40. *
  41. *
  42. * LSI MegaRAID SCSI 320-0 1000 1960 1000 A520
  43. * LSI MegaRAID SCSI 320-1 1000 1960 1000 0520
  44. * LSI MegaRAID SCSI 320-2 1000 1960 1000 0518
  45. * LSI MegaRAID SCSI 320-0X 1000 0407 1000 0530
  46. * LSI MegaRAID SCSI 320-2X 1000 0407 1000 0532
  47. * LSI MegaRAID SCSI 320-4X 1000 0407 1000 0531
  48. * LSI MegaRAID SCSI 320-1E 1000 0408 1000 0001
  49. * LSI MegaRAID SCSI 320-2E 1000 0408 1000 0002
  50. * LSI MegaRAID SATA 150-4 1000 1960 1000 4523
  51. * LSI MegaRAID SATA 150-6 1000 1960 1000 0523
  52. * LSI MegaRAID SATA 300-4X 1000 0409 1000 3004
  53. * LSI MegaRAID SATA 300-8X 1000 0409 1000 3008
  54. *
  55. * INTEL RAID Controller SRCU42X 1000 0407 8086 0532
  56. * INTEL RAID Controller SRCS16 1000 1960 8086 0523
  57. * INTEL RAID Controller SRCU42E 1000 0408 8086 0002
  58. * INTEL RAID Controller SRCZCRX 1000 0407 8086 0530
  59. * INTEL RAID Controller SRCS28X 1000 0409 8086 3008
  60. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3431
  61. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3499
  62. * INTEL RAID Controller SRCU51L 1000 1960 8086 0520
  63. *
  64. * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
  65. *
  66. * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
  67. *
  68. * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287
  69. *
  70. * For history of changes, see Documentation/scsi/ChangeLog.megaraid
  71. */
  72. #include <linux/slab.h>
  73. #include <linux/module.h>
  74. #include "megaraid_mbox.h"
  75. static int megaraid_init(void);
  76. static void megaraid_exit(void);
  77. static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
  78. static void megaraid_detach_one(struct pci_dev *);
  79. static void megaraid_mbox_shutdown(struct pci_dev *);
  80. static int megaraid_io_attach(adapter_t *);
  81. static void megaraid_io_detach(adapter_t *);
  82. static int megaraid_init_mbox(adapter_t *);
  83. static void megaraid_fini_mbox(adapter_t *);
  84. static int megaraid_alloc_cmd_packets(adapter_t *);
  85. static void megaraid_free_cmd_packets(adapter_t *);
  86. static int megaraid_mbox_setup_dma_pools(adapter_t *);
  87. static void megaraid_mbox_teardown_dma_pools(adapter_t *);
  88. static int megaraid_sysfs_alloc_resources(adapter_t *);
  89. static void megaraid_sysfs_free_resources(adapter_t *);
  90. static int megaraid_abort_handler(struct scsi_cmnd *);
  91. static int megaraid_reset_handler(struct scsi_cmnd *);
  92. static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
  93. static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
  94. static int megaraid_busywait_mbox(mraid_device_t *);
  95. static int megaraid_mbox_product_info(adapter_t *);
  96. static int megaraid_mbox_extended_cdb(adapter_t *);
  97. static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
  98. static int megaraid_mbox_support_random_del(adapter_t *);
  99. static int megaraid_mbox_get_max_sg(adapter_t *);
  100. static void megaraid_mbox_enum_raid_scsi(adapter_t *);
  101. static void megaraid_mbox_flush_cache(adapter_t *);
  102. static int megaraid_mbox_fire_sync_cmd(adapter_t *);
  103. static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
  104. static void megaraid_mbox_setup_device_map(adapter_t *);
  105. static int megaraid_queue_command(struct Scsi_Host *, struct scsi_cmnd *);
  106. static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
  107. static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
  108. static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
  109. struct scsi_cmnd *);
  110. static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
  111. struct scsi_cmnd *);
  112. static irqreturn_t megaraid_isr(int, void *);
  113. static void megaraid_mbox_dpc(unsigned long);
  114. static ssize_t megaraid_sysfs_show_app_hndl(struct device *, struct device_attribute *attr, char *);
  115. static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
  116. static int megaraid_cmm_register(adapter_t *);
  117. static int megaraid_cmm_unregister(adapter_t *);
  118. static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
  119. static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
  120. static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
  121. static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
  122. static int wait_till_fw_empty(adapter_t *);
  123. MODULE_AUTHOR("megaraidlinux@lsi.com");
  124. MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
  125. MODULE_LICENSE("GPL");
  126. MODULE_VERSION(MEGARAID_VERSION);
  127. /*
  128. * ### modules parameters for driver ###
  129. */
  130. /*
  131. * Set to enable driver to expose unconfigured disk to kernel
  132. */
  133. static int megaraid_expose_unconf_disks = 0;
  134. module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
  135. MODULE_PARM_DESC(unconf_disks,
  136. "Set to expose unconfigured disks to kernel (default=0)");
  137. /*
  138. * driver wait time if the adapter's mailbox is busy
  139. */
  140. static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  141. module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
  142. MODULE_PARM_DESC(busy_wait,
  143. "Max wait for mailbox in microseconds if busy (default=10)");
  144. /*
  145. * number of sectors per IO command
  146. */
  147. static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
  148. module_param_named(max_sectors, megaraid_max_sectors, int, 0);
  149. MODULE_PARM_DESC(max_sectors,
  150. "Maximum number of sectors per IO command (default=128)");
  151. /*
  152. * number of commands per logical unit
  153. */
  154. static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
  155. module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
  156. MODULE_PARM_DESC(cmd_per_lun,
  157. "Maximum number of commands per logical unit (default=64)");
  158. /*
  159. * Fast driver load option, skip scanning for physical devices during load.
  160. * This would result in non-disk devices being skipped during driver load
  161. * time. These can be later added though, using /proc/scsi/scsi
  162. */
  163. static unsigned int megaraid_fast_load = 0;
  164. module_param_named(fast_load, megaraid_fast_load, int, 0);
  165. MODULE_PARM_DESC(fast_load,
  166. "Faster loading of the driver, skips physical devices! (default=0)");
  167. /*
  168. * mraid_debug level - threshold for amount of information to be displayed by
  169. * the driver. This level can be changed through modules parameters, ioctl or
  170. * sysfs/proc interface. By default, print the announcement messages only.
  171. */
  172. int mraid_debug_level = CL_ANN;
  173. module_param_named(debug_level, mraid_debug_level, int, 0);
  174. MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
  175. /*
  176. * ### global data ###
  177. */
  178. static uint8_t megaraid_mbox_version[8] =
  179. { 0x02, 0x20, 0x04, 0x06, 3, 7, 20, 5 };
  180. /*
  181. * PCI table for all supported controllers.
  182. */
  183. static struct pci_device_id pci_id_table_g[] = {
  184. {
  185. PCI_VENDOR_ID_DELL,
  186. PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
  187. PCI_VENDOR_ID_DELL,
  188. PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
  189. },
  190. {
  191. PCI_VENDOR_ID_LSI_LOGIC,
  192. PCI_DEVICE_ID_PERC4_SC,
  193. PCI_VENDOR_ID_DELL,
  194. PCI_SUBSYS_ID_PERC4_SC,
  195. },
  196. {
  197. PCI_VENDOR_ID_LSI_LOGIC,
  198. PCI_DEVICE_ID_PERC4_DC,
  199. PCI_VENDOR_ID_DELL,
  200. PCI_SUBSYS_ID_PERC4_DC,
  201. },
  202. {
  203. PCI_VENDOR_ID_LSI_LOGIC,
  204. PCI_DEVICE_ID_VERDE,
  205. PCI_ANY_ID,
  206. PCI_ANY_ID,
  207. },
  208. {
  209. PCI_VENDOR_ID_DELL,
  210. PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
  211. PCI_VENDOR_ID_DELL,
  212. PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
  213. },
  214. {
  215. PCI_VENDOR_ID_DELL,
  216. PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
  217. PCI_VENDOR_ID_DELL,
  218. PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
  219. },
  220. {
  221. PCI_VENDOR_ID_DELL,
  222. PCI_DEVICE_ID_PERC4E_DI_KOBUK,
  223. PCI_VENDOR_ID_DELL,
  224. PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
  225. },
  226. {
  227. PCI_VENDOR_ID_DELL,
  228. PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
  229. PCI_VENDOR_ID_DELL,
  230. PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
  231. },
  232. {
  233. PCI_VENDOR_ID_DELL,
  234. PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
  235. PCI_VENDOR_ID_DELL,
  236. PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
  237. },
  238. {
  239. PCI_VENDOR_ID_DELL,
  240. PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
  241. PCI_VENDOR_ID_DELL,
  242. PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
  243. },
  244. {
  245. PCI_VENDOR_ID_LSI_LOGIC,
  246. PCI_DEVICE_ID_DOBSON,
  247. PCI_ANY_ID,
  248. PCI_ANY_ID,
  249. },
  250. {
  251. PCI_VENDOR_ID_AMI,
  252. PCI_DEVICE_ID_AMI_MEGARAID3,
  253. PCI_ANY_ID,
  254. PCI_ANY_ID,
  255. },
  256. {
  257. PCI_VENDOR_ID_LSI_LOGIC,
  258. PCI_DEVICE_ID_AMI_MEGARAID3,
  259. PCI_ANY_ID,
  260. PCI_ANY_ID,
  261. },
  262. {
  263. PCI_VENDOR_ID_LSI_LOGIC,
  264. PCI_DEVICE_ID_LINDSAY,
  265. PCI_ANY_ID,
  266. PCI_ANY_ID,
  267. },
  268. {0} /* Terminating entry */
  269. };
  270. MODULE_DEVICE_TABLE(pci, pci_id_table_g);
  271. static struct pci_driver megaraid_pci_driver = {
  272. .name = "megaraid",
  273. .id_table = pci_id_table_g,
  274. .probe = megaraid_probe_one,
  275. .remove = megaraid_detach_one,
  276. .shutdown = megaraid_mbox_shutdown,
  277. };
  278. // definitions for the device attributes for exporting logical drive number
  279. // for a scsi address (Host, Channel, Id, Lun)
  280. DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
  281. NULL);
  282. // Host template initializer for megaraid mbox sysfs device attributes
  283. static struct device_attribute *megaraid_shost_attrs[] = {
  284. &dev_attr_megaraid_mbox_app_hndl,
  285. NULL,
  286. };
  287. DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
  288. // Host template initializer for megaraid mbox sysfs device attributes
  289. static struct device_attribute *megaraid_sdev_attrs[] = {
  290. &dev_attr_megaraid_mbox_ld,
  291. NULL,
  292. };
  293. /*
  294. * Scsi host template for megaraid unified driver
  295. */
  296. static struct scsi_host_template megaraid_template_g = {
  297. .module = THIS_MODULE,
  298. .name = "LSI Logic MegaRAID driver",
  299. .proc_name = "megaraid",
  300. .queuecommand = megaraid_queue_command,
  301. .eh_abort_handler = megaraid_abort_handler,
  302. .eh_device_reset_handler = megaraid_reset_handler,
  303. .eh_bus_reset_handler = megaraid_reset_handler,
  304. .eh_host_reset_handler = megaraid_reset_handler,
  305. .change_queue_depth = scsi_change_queue_depth,
  306. .use_clustering = ENABLE_CLUSTERING,
  307. .no_write_same = 1,
  308. .sdev_attrs = megaraid_sdev_attrs,
  309. .shost_attrs = megaraid_shost_attrs,
  310. };
  311. /**
  312. * megaraid_init - module load hook
  313. *
  314. * We register ourselves as hotplug enabled module and let PCI subsystem
  315. * discover our adapters.
  316. */
  317. static int __init
  318. megaraid_init(void)
  319. {
  320. int rval;
  321. // Announce the driver version
  322. con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
  323. MEGARAID_EXT_VERSION));
  324. // check validity of module parameters
  325. if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
  326. con_log(CL_ANN, (KERN_WARNING
  327. "megaraid mailbox: max commands per lun reset to %d\n",
  328. MBOX_MAX_SCSI_CMDS));
  329. megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
  330. }
  331. // register as a PCI hot-plug driver module
  332. rval = pci_register_driver(&megaraid_pci_driver);
  333. if (rval < 0) {
  334. con_log(CL_ANN, (KERN_WARNING
  335. "megaraid: could not register hotplug support.\n"));
  336. }
  337. return rval;
  338. }
  339. /**
  340. * megaraid_exit - driver unload entry point
  341. *
  342. * We simply unwrap the megaraid_init routine here.
  343. */
  344. static void __exit
  345. megaraid_exit(void)
  346. {
  347. con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
  348. // unregister as PCI hotplug driver
  349. pci_unregister_driver(&megaraid_pci_driver);
  350. return;
  351. }
  352. /**
  353. * megaraid_probe_one - PCI hotplug entry point
  354. * @pdev : handle to this controller's PCI configuration space
  355. * @id : pci device id of the class of controllers
  356. *
  357. * This routine should be called whenever a new adapter is detected by the
  358. * PCI hotplug susbsystem.
  359. */
  360. static int
  361. megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  362. {
  363. adapter_t *adapter;
  364. // detected a new controller
  365. con_log(CL_ANN, (KERN_INFO
  366. "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  367. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  368. pdev->subsystem_device));
  369. con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
  370. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
  371. if (pci_enable_device(pdev)) {
  372. con_log(CL_ANN, (KERN_WARNING
  373. "megaraid: pci_enable_device failed\n"));
  374. return -ENODEV;
  375. }
  376. // Enable bus-mastering on this controller
  377. pci_set_master(pdev);
  378. // Allocate the per driver initialization structure
  379. adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
  380. if (adapter == NULL) {
  381. con_log(CL_ANN, (KERN_WARNING
  382. "megaraid: out of memory, %s %d.\n", __func__, __LINE__));
  383. goto out_probe_one;
  384. }
  385. // set up PCI related soft state and other pre-known parameters
  386. adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
  387. adapter->irq = pdev->irq;
  388. adapter->pdev = pdev;
  389. atomic_set(&adapter->being_detached, 0);
  390. // Setup the default DMA mask. This would be changed later on
  391. // depending on hardware capabilities
  392. if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(32)) != 0) {
  393. con_log(CL_ANN, (KERN_WARNING
  394. "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
  395. goto out_free_adapter;
  396. }
  397. // Initialize the synchronization lock for kernel and LLD
  398. spin_lock_init(&adapter->lock);
  399. // Initialize the command queues: the list of free SCBs and the list
  400. // of pending SCBs.
  401. INIT_LIST_HEAD(&adapter->kscb_pool);
  402. spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
  403. INIT_LIST_HEAD(&adapter->pend_list);
  404. spin_lock_init(PENDING_LIST_LOCK(adapter));
  405. INIT_LIST_HEAD(&adapter->completed_list);
  406. spin_lock_init(COMPLETED_LIST_LOCK(adapter));
  407. // Start the mailbox based controller
  408. if (megaraid_init_mbox(adapter) != 0) {
  409. con_log(CL_ANN, (KERN_WARNING
  410. "megaraid: maibox adapter did not initialize\n"));
  411. goto out_free_adapter;
  412. }
  413. // Register with LSI Common Management Module
  414. if (megaraid_cmm_register(adapter) != 0) {
  415. con_log(CL_ANN, (KERN_WARNING
  416. "megaraid: could not register with management module\n"));
  417. goto out_fini_mbox;
  418. }
  419. // setup adapter handle in PCI soft state
  420. pci_set_drvdata(pdev, adapter);
  421. // attach with scsi mid-layer
  422. if (megaraid_io_attach(adapter) != 0) {
  423. con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
  424. goto out_cmm_unreg;
  425. }
  426. return 0;
  427. out_cmm_unreg:
  428. megaraid_cmm_unregister(adapter);
  429. out_fini_mbox:
  430. megaraid_fini_mbox(adapter);
  431. out_free_adapter:
  432. kfree(adapter);
  433. out_probe_one:
  434. pci_disable_device(pdev);
  435. return -ENODEV;
  436. }
  437. /**
  438. * megaraid_detach_one - release framework resources and call LLD release routine
  439. * @pdev : handle for our PCI configuration space
  440. *
  441. * This routine is called during driver unload. We free all the allocated
  442. * resources and call the corresponding LLD so that it can also release all
  443. * its resources.
  444. *
  445. * This routine is also called from the PCI hotplug system.
  446. */
  447. static void
  448. megaraid_detach_one(struct pci_dev *pdev)
  449. {
  450. adapter_t *adapter;
  451. struct Scsi_Host *host;
  452. // Start a rollback on this adapter
  453. adapter = pci_get_drvdata(pdev);
  454. if (!adapter) {
  455. con_log(CL_ANN, (KERN_CRIT
  456. "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  457. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  458. pdev->subsystem_device));
  459. return;
  460. }
  461. else {
  462. con_log(CL_ANN, (KERN_NOTICE
  463. "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  464. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  465. pdev->subsystem_device));
  466. }
  467. host = adapter->host;
  468. // do not allow any more requests from the management module for this
  469. // adapter.
  470. // FIXME: How do we account for the request which might still be
  471. // pending with us?
  472. atomic_set(&adapter->being_detached, 1);
  473. // detach from the IO sub-system
  474. megaraid_io_detach(adapter);
  475. // Unregister from common management module
  476. //
  477. // FIXME: this must return success or failure for conditions if there
  478. // is a command pending with LLD or not.
  479. megaraid_cmm_unregister(adapter);
  480. // finalize the mailbox based controller and release all resources
  481. megaraid_fini_mbox(adapter);
  482. kfree(adapter);
  483. scsi_host_put(host);
  484. pci_disable_device(pdev);
  485. return;
  486. }
  487. /**
  488. * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
  489. * @pdev : generic driver model device
  490. *
  491. * Shutdown notification, perform flush cache.
  492. */
  493. static void
  494. megaraid_mbox_shutdown(struct pci_dev *pdev)
  495. {
  496. adapter_t *adapter = pci_get_drvdata(pdev);
  497. static int counter;
  498. if (!adapter) {
  499. con_log(CL_ANN, (KERN_WARNING
  500. "megaraid: null device in shutdown\n"));
  501. return;
  502. }
  503. // flush caches now
  504. con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
  505. counter++));
  506. megaraid_mbox_flush_cache(adapter);
  507. con_log(CL_ANN, ("done\n"));
  508. }
  509. /**
  510. * megaraid_io_attach - attach a device with the IO subsystem
  511. * @adapter : controller's soft state
  512. *
  513. * Attach this device with the IO subsystem.
  514. */
  515. static int
  516. megaraid_io_attach(adapter_t *adapter)
  517. {
  518. struct Scsi_Host *host;
  519. // Initialize SCSI Host structure
  520. host = scsi_host_alloc(&megaraid_template_g, 8);
  521. if (!host) {
  522. con_log(CL_ANN, (KERN_WARNING
  523. "megaraid mbox: scsi_register failed\n"));
  524. return -1;
  525. }
  526. SCSIHOST2ADAP(host) = (caddr_t)adapter;
  527. adapter->host = host;
  528. host->irq = adapter->irq;
  529. host->unique_id = adapter->unique_id;
  530. host->can_queue = adapter->max_cmds;
  531. host->this_id = adapter->init_id;
  532. host->sg_tablesize = adapter->sglen;
  533. host->max_sectors = adapter->max_sectors;
  534. host->cmd_per_lun = adapter->cmd_per_lun;
  535. host->max_channel = adapter->max_channel;
  536. host->max_id = adapter->max_target;
  537. host->max_lun = adapter->max_lun;
  538. // notify mid-layer about the new controller
  539. if (scsi_add_host(host, &adapter->pdev->dev)) {
  540. con_log(CL_ANN, (KERN_WARNING
  541. "megaraid mbox: scsi_add_host failed\n"));
  542. scsi_host_put(host);
  543. return -1;
  544. }
  545. scsi_scan_host(host);
  546. return 0;
  547. }
  548. /**
  549. * megaraid_io_detach - detach a device from the IO subsystem
  550. * @adapter : controller's soft state
  551. *
  552. * Detach this device from the IO subsystem.
  553. */
  554. static void
  555. megaraid_io_detach(adapter_t *adapter)
  556. {
  557. struct Scsi_Host *host;
  558. con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
  559. host = adapter->host;
  560. scsi_remove_host(host);
  561. return;
  562. }
  563. /*
  564. * START: Mailbox Low Level Driver
  565. *
  566. * This is section specific to the single mailbox based controllers
  567. */
  568. /**
  569. * megaraid_init_mbox - initialize controller
  570. * @adapter : our soft state
  571. *
  572. * - Allocate 16-byte aligned mailbox memory for firmware handshake
  573. * - Allocate controller's memory resources
  574. * - Find out all initialization data
  575. * - Allocate memory required for all the commands
  576. * - Use internal library of FW routines, build up complete soft state
  577. */
  578. static int
  579. megaraid_init_mbox(adapter_t *adapter)
  580. {
  581. struct pci_dev *pdev;
  582. mraid_device_t *raid_dev;
  583. int i;
  584. uint32_t magic64;
  585. adapter->ito = MBOX_TIMEOUT;
  586. pdev = adapter->pdev;
  587. /*
  588. * Allocate and initialize the init data structure for mailbox
  589. * controllers
  590. */
  591. raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
  592. if (raid_dev == NULL) return -1;
  593. /*
  594. * Attach the adapter soft state to raid device soft state
  595. */
  596. adapter->raid_device = (caddr_t)raid_dev;
  597. raid_dev->fast_load = megaraid_fast_load;
  598. // our baseport
  599. raid_dev->baseport = pci_resource_start(pdev, 0);
  600. if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
  601. con_log(CL_ANN, (KERN_WARNING
  602. "megaraid: mem region busy\n"));
  603. goto out_free_raid_dev;
  604. }
  605. raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
  606. if (!raid_dev->baseaddr) {
  607. con_log(CL_ANN, (KERN_WARNING
  608. "megaraid: could not map hba memory\n") );
  609. goto out_release_regions;
  610. }
  611. /* initialize the mutual exclusion lock for the mailbox */
  612. spin_lock_init(&raid_dev->mailbox_lock);
  613. /* allocate memory required for commands */
  614. if (megaraid_alloc_cmd_packets(adapter) != 0)
  615. goto out_iounmap;
  616. /*
  617. * Issue SYNC cmd to flush the pending cmds in the adapter
  618. * and initialize its internal state
  619. */
  620. if (megaraid_mbox_fire_sync_cmd(adapter))
  621. con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
  622. /*
  623. * Setup the rest of the soft state using the library of
  624. * FW routines
  625. */
  626. /* request IRQ and register the interrupt service routine */
  627. if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
  628. adapter)) {
  629. con_log(CL_ANN, (KERN_WARNING
  630. "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
  631. goto out_alloc_cmds;
  632. }
  633. // Product info
  634. if (megaraid_mbox_product_info(adapter) != 0)
  635. goto out_free_irq;
  636. // Do we support extended CDBs
  637. adapter->max_cdb_sz = 10;
  638. if (megaraid_mbox_extended_cdb(adapter) == 0) {
  639. adapter->max_cdb_sz = 16;
  640. }
  641. /*
  642. * Do we support cluster environment, if we do, what is the initiator
  643. * id.
  644. * NOTE: In a non-cluster aware firmware environment, the LLD should
  645. * return 7 as initiator id.
  646. */
  647. adapter->ha = 0;
  648. adapter->init_id = -1;
  649. if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
  650. adapter->ha = 1;
  651. }
  652. /*
  653. * Prepare the device ids array to have the mapping between the kernel
  654. * device address and megaraid device address.
  655. * We export the physical devices on their actual addresses. The
  656. * logical drives are exported on a virtual SCSI channel
  657. */
  658. megaraid_mbox_setup_device_map(adapter);
  659. // If the firmware supports random deletion, update the device id map
  660. if (megaraid_mbox_support_random_del(adapter)) {
  661. // Change the logical drives numbers in device_ids array one
  662. // slot in device_ids is reserved for target id, that's why
  663. // "<=" below
  664. for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
  665. adapter->device_ids[adapter->max_channel][i] += 0x80;
  666. }
  667. adapter->device_ids[adapter->max_channel][adapter->init_id] =
  668. 0xFF;
  669. raid_dev->random_del_supported = 1;
  670. }
  671. /*
  672. * find out the maximum number of scatter-gather elements supported by
  673. * this firmware
  674. */
  675. adapter->sglen = megaraid_mbox_get_max_sg(adapter);
  676. // enumerate RAID and SCSI channels so that all devices on SCSI
  677. // channels can later be exported, including disk devices
  678. megaraid_mbox_enum_raid_scsi(adapter);
  679. /*
  680. * Other parameters required by upper layer
  681. *
  682. * maximum number of sectors per IO command
  683. */
  684. adapter->max_sectors = megaraid_max_sectors;
  685. /*
  686. * number of queued commands per LUN.
  687. */
  688. adapter->cmd_per_lun = megaraid_cmd_per_lun;
  689. /*
  690. * Allocate resources required to issue FW calls, when sysfs is
  691. * accessed
  692. */
  693. if (megaraid_sysfs_alloc_resources(adapter) != 0)
  694. goto out_free_irq;
  695. // Set the DMA mask to 64-bit. All supported controllers as capable of
  696. // DMA in this range
  697. pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
  698. if (((magic64 == HBA_SIGNATURE_64_BIT) &&
  699. ((adapter->pdev->subsystem_device !=
  700. PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
  701. (adapter->pdev->subsystem_device !=
  702. PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
  703. (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
  704. adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
  705. (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
  706. adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
  707. (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
  708. adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
  709. (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
  710. adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
  711. (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
  712. adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
  713. if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(64))) {
  714. con_log(CL_ANN, (KERN_WARNING
  715. "megaraid: DMA mask for 64-bit failed\n"));
  716. if (pci_set_dma_mask (adapter->pdev, DMA_BIT_MASK(32))) {
  717. con_log(CL_ANN, (KERN_WARNING
  718. "megaraid: 32-bit DMA mask failed\n"));
  719. goto out_free_sysfs_res;
  720. }
  721. }
  722. }
  723. // setup tasklet for DPC
  724. tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
  725. (unsigned long)adapter);
  726. con_log(CL_DLEVEL1, (KERN_INFO
  727. "megaraid mbox hba successfully initialized\n"));
  728. return 0;
  729. out_free_sysfs_res:
  730. megaraid_sysfs_free_resources(adapter);
  731. out_free_irq:
  732. free_irq(adapter->irq, adapter);
  733. out_alloc_cmds:
  734. megaraid_free_cmd_packets(adapter);
  735. out_iounmap:
  736. iounmap(raid_dev->baseaddr);
  737. out_release_regions:
  738. pci_release_regions(pdev);
  739. out_free_raid_dev:
  740. kfree(raid_dev);
  741. return -1;
  742. }
  743. /**
  744. * megaraid_fini_mbox - undo controller initialization
  745. * @adapter : our soft state
  746. */
  747. static void
  748. megaraid_fini_mbox(adapter_t *adapter)
  749. {
  750. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  751. // flush all caches
  752. megaraid_mbox_flush_cache(adapter);
  753. tasklet_kill(&adapter->dpc_h);
  754. megaraid_sysfs_free_resources(adapter);
  755. megaraid_free_cmd_packets(adapter);
  756. free_irq(adapter->irq, adapter);
  757. iounmap(raid_dev->baseaddr);
  758. pci_release_regions(adapter->pdev);
  759. kfree(raid_dev);
  760. return;
  761. }
  762. /**
  763. * megaraid_alloc_cmd_packets - allocate shared mailbox
  764. * @adapter : soft state of the raid controller
  765. *
  766. * Allocate and align the shared mailbox. This maibox is used to issue
  767. * all the commands. For IO based controllers, the mailbox is also registered
  768. * with the FW. Allocate memory for all commands as well.
  769. * This is our big allocator.
  770. */
  771. static int
  772. megaraid_alloc_cmd_packets(adapter_t *adapter)
  773. {
  774. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  775. struct pci_dev *pdev;
  776. unsigned long align;
  777. scb_t *scb;
  778. mbox_ccb_t *ccb;
  779. struct mraid_pci_blk *epthru_pci_blk;
  780. struct mraid_pci_blk *sg_pci_blk;
  781. struct mraid_pci_blk *mbox_pci_blk;
  782. int i;
  783. pdev = adapter->pdev;
  784. /*
  785. * Setup the mailbox
  786. * Allocate the common 16-byte aligned memory for the handshake
  787. * mailbox.
  788. */
  789. raid_dev->una_mbox64 = pci_zalloc_consistent(adapter->pdev,
  790. sizeof(mbox64_t),
  791. &raid_dev->una_mbox64_dma);
  792. if (!raid_dev->una_mbox64) {
  793. con_log(CL_ANN, (KERN_WARNING
  794. "megaraid: out of memory, %s %d\n", __func__,
  795. __LINE__));
  796. return -1;
  797. }
  798. /*
  799. * Align the mailbox at 16-byte boundary
  800. */
  801. raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
  802. raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
  803. (~0UL ^ 0xFUL));
  804. raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
  805. align = ((void *)raid_dev->mbox -
  806. ((void *)&raid_dev->una_mbox64->mbox32));
  807. raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
  808. align;
  809. // Allocate memory for commands issued internally
  810. adapter->ibuf = pci_zalloc_consistent(pdev, MBOX_IBUF_SIZE,
  811. &adapter->ibuf_dma_h);
  812. if (!adapter->ibuf) {
  813. con_log(CL_ANN, (KERN_WARNING
  814. "megaraid: out of memory, %s %d\n", __func__,
  815. __LINE__));
  816. goto out_free_common_mbox;
  817. }
  818. // Allocate memory for our SCSI Command Blocks and their associated
  819. // memory
  820. /*
  821. * Allocate memory for the base list of scb. Later allocate memory for
  822. * CCBs and embedded components of each CCB and point the pointers in
  823. * scb to the allocated components
  824. * NOTE: The code to allocate SCB will be duplicated in all the LLD
  825. * since the calling routine does not yet know the number of available
  826. * commands.
  827. */
  828. adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL);
  829. if (adapter->kscb_list == NULL) {
  830. con_log(CL_ANN, (KERN_WARNING
  831. "megaraid: out of memory, %s %d\n", __func__,
  832. __LINE__));
  833. goto out_free_ibuf;
  834. }
  835. // memory allocation for our command packets
  836. if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
  837. con_log(CL_ANN, (KERN_WARNING
  838. "megaraid: out of memory, %s %d\n", __func__,
  839. __LINE__));
  840. goto out_free_scb_list;
  841. }
  842. // Adjust the scb pointers and link in the free pool
  843. epthru_pci_blk = raid_dev->epthru_pool;
  844. sg_pci_blk = raid_dev->sg_pool;
  845. mbox_pci_blk = raid_dev->mbox_pool;
  846. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  847. scb = adapter->kscb_list + i;
  848. ccb = raid_dev->ccb_list + i;
  849. ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
  850. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  851. ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
  852. ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
  853. // make sure the mailbox is aligned properly
  854. if (ccb->mbox_dma_h & 0x0F) {
  855. con_log(CL_ANN, (KERN_CRIT
  856. "megaraid mbox: not aligned on 16-bytes\n"));
  857. goto out_teardown_dma_pools;
  858. }
  859. ccb->epthru = (mraid_epassthru_t *)
  860. epthru_pci_blk[i].vaddr;
  861. ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
  862. ccb->pthru = (mraid_passthru_t *)ccb->epthru;
  863. ccb->pthru_dma_h = ccb->epthru_dma_h;
  864. ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
  865. ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
  866. ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
  867. scb->ccb = (caddr_t)ccb;
  868. scb->gp = 0;
  869. scb->sno = i; // command index
  870. scb->scp = NULL;
  871. scb->state = SCB_FREE;
  872. scb->dma_direction = PCI_DMA_NONE;
  873. scb->dma_type = MRAID_DMA_NONE;
  874. scb->dev_channel = -1;
  875. scb->dev_target = -1;
  876. // put scb in the free pool
  877. list_add_tail(&scb->list, &adapter->kscb_pool);
  878. }
  879. return 0;
  880. out_teardown_dma_pools:
  881. megaraid_mbox_teardown_dma_pools(adapter);
  882. out_free_scb_list:
  883. kfree(adapter->kscb_list);
  884. out_free_ibuf:
  885. pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
  886. adapter->ibuf_dma_h);
  887. out_free_common_mbox:
  888. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  889. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  890. return -1;
  891. }
  892. /**
  893. * megaraid_free_cmd_packets - free memory
  894. * @adapter : soft state of the raid controller
  895. *
  896. * Release memory resources allocated for commands.
  897. */
  898. static void
  899. megaraid_free_cmd_packets(adapter_t *adapter)
  900. {
  901. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  902. megaraid_mbox_teardown_dma_pools(adapter);
  903. kfree(adapter->kscb_list);
  904. pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
  905. (void *)adapter->ibuf, adapter->ibuf_dma_h);
  906. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  907. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  908. return;
  909. }
  910. /**
  911. * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
  912. * @adapter : HBA soft state
  913. *
  914. * Setup the dma pools for mailbox, passthru and extended passthru structures,
  915. * and scatter-gather lists.
  916. */
  917. static int
  918. megaraid_mbox_setup_dma_pools(adapter_t *adapter)
  919. {
  920. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  921. struct mraid_pci_blk *epthru_pci_blk;
  922. struct mraid_pci_blk *sg_pci_blk;
  923. struct mraid_pci_blk *mbox_pci_blk;
  924. int i;
  925. // Allocate memory for 16-bytes aligned mailboxes
  926. raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
  927. adapter->pdev,
  928. sizeof(mbox64_t) + 16,
  929. 16, 0);
  930. if (raid_dev->mbox_pool_handle == NULL) {
  931. goto fail_setup_dma_pool;
  932. }
  933. mbox_pci_blk = raid_dev->mbox_pool;
  934. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  935. mbox_pci_blk[i].vaddr = pci_pool_alloc(
  936. raid_dev->mbox_pool_handle,
  937. GFP_KERNEL,
  938. &mbox_pci_blk[i].dma_addr);
  939. if (!mbox_pci_blk[i].vaddr) {
  940. goto fail_setup_dma_pool;
  941. }
  942. }
  943. /*
  944. * Allocate memory for each embedded passthru strucuture pointer
  945. * Request for a 128 bytes aligned structure for each passthru command
  946. * structure
  947. * Since passthru and extended passthru commands are exclusive, they
  948. * share common memory pool. Passthru structures piggyback on memory
  949. * allocted to extended passthru since passthru is smaller of the two
  950. */
  951. raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
  952. adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
  953. if (raid_dev->epthru_pool_handle == NULL) {
  954. goto fail_setup_dma_pool;
  955. }
  956. epthru_pci_blk = raid_dev->epthru_pool;
  957. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  958. epthru_pci_blk[i].vaddr = pci_pool_alloc(
  959. raid_dev->epthru_pool_handle,
  960. GFP_KERNEL,
  961. &epthru_pci_blk[i].dma_addr);
  962. if (!epthru_pci_blk[i].vaddr) {
  963. goto fail_setup_dma_pool;
  964. }
  965. }
  966. // Allocate memory for each scatter-gather list. Request for 512 bytes
  967. // alignment for each sg list
  968. raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
  969. adapter->pdev,
  970. sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
  971. 512, 0);
  972. if (raid_dev->sg_pool_handle == NULL) {
  973. goto fail_setup_dma_pool;
  974. }
  975. sg_pci_blk = raid_dev->sg_pool;
  976. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  977. sg_pci_blk[i].vaddr = pci_pool_alloc(
  978. raid_dev->sg_pool_handle,
  979. GFP_KERNEL,
  980. &sg_pci_blk[i].dma_addr);
  981. if (!sg_pci_blk[i].vaddr) {
  982. goto fail_setup_dma_pool;
  983. }
  984. }
  985. return 0;
  986. fail_setup_dma_pool:
  987. megaraid_mbox_teardown_dma_pools(adapter);
  988. return -1;
  989. }
  990. /**
  991. * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
  992. * @adapter : HBA soft state
  993. *
  994. * Teardown the dma pool for mailbox, passthru and extended passthru
  995. * structures, and scatter-gather lists.
  996. */
  997. static void
  998. megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
  999. {
  1000. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1001. struct mraid_pci_blk *epthru_pci_blk;
  1002. struct mraid_pci_blk *sg_pci_blk;
  1003. struct mraid_pci_blk *mbox_pci_blk;
  1004. int i;
  1005. sg_pci_blk = raid_dev->sg_pool;
  1006. for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
  1007. pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
  1008. sg_pci_blk[i].dma_addr);
  1009. }
  1010. if (raid_dev->sg_pool_handle)
  1011. pci_pool_destroy(raid_dev->sg_pool_handle);
  1012. epthru_pci_blk = raid_dev->epthru_pool;
  1013. for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
  1014. pci_pool_free(raid_dev->epthru_pool_handle,
  1015. epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
  1016. }
  1017. if (raid_dev->epthru_pool_handle)
  1018. pci_pool_destroy(raid_dev->epthru_pool_handle);
  1019. mbox_pci_blk = raid_dev->mbox_pool;
  1020. for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
  1021. pci_pool_free(raid_dev->mbox_pool_handle,
  1022. mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
  1023. }
  1024. if (raid_dev->mbox_pool_handle)
  1025. pci_pool_destroy(raid_dev->mbox_pool_handle);
  1026. return;
  1027. }
  1028. /**
  1029. * megaraid_alloc_scb - detach and return a scb from the free list
  1030. * @adapter : controller's soft state
  1031. * @scp : pointer to the scsi command to be executed
  1032. *
  1033. * Return the scb from the head of the free list. %NULL if there are none
  1034. * available.
  1035. */
  1036. static scb_t *
  1037. megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
  1038. {
  1039. struct list_head *head = &adapter->kscb_pool;
  1040. scb_t *scb = NULL;
  1041. unsigned long flags;
  1042. // detach scb from free pool
  1043. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1044. if (list_empty(head)) {
  1045. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1046. return NULL;
  1047. }
  1048. scb = list_entry(head->next, scb_t, list);
  1049. list_del_init(&scb->list);
  1050. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1051. scb->state = SCB_ACTIVE;
  1052. scb->scp = scp;
  1053. scb->dma_type = MRAID_DMA_NONE;
  1054. return scb;
  1055. }
  1056. /**
  1057. * megaraid_dealloc_scb - return the scb to the free pool
  1058. * @adapter : controller's soft state
  1059. * @scb : scb to be freed
  1060. *
  1061. * Return the scb back to the free list of scbs. The caller must 'flush' the
  1062. * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
  1063. * NOTE NOTE: Make sure the scb is not on any list before calling this
  1064. * routine.
  1065. */
  1066. static inline void
  1067. megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
  1068. {
  1069. unsigned long flags;
  1070. // put scb in the free pool
  1071. scb->state = SCB_FREE;
  1072. scb->scp = NULL;
  1073. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1074. list_add(&scb->list, &adapter->kscb_pool);
  1075. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1076. return;
  1077. }
  1078. /**
  1079. * megaraid_mbox_mksgl - make the scatter-gather list
  1080. * @adapter : controller's soft state
  1081. * @scb : scsi control block
  1082. *
  1083. * Prepare the scatter-gather list.
  1084. */
  1085. static int
  1086. megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
  1087. {
  1088. struct scatterlist *sgl;
  1089. mbox_ccb_t *ccb;
  1090. struct scsi_cmnd *scp;
  1091. int sgcnt;
  1092. int i;
  1093. scp = scb->scp;
  1094. ccb = (mbox_ccb_t *)scb->ccb;
  1095. sgcnt = scsi_dma_map(scp);
  1096. BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen);
  1097. // no mapping required if no data to be transferred
  1098. if (!sgcnt)
  1099. return 0;
  1100. scb->dma_type = MRAID_DMA_WSG;
  1101. scsi_for_each_sg(scp, sgl, sgcnt, i) {
  1102. ccb->sgl64[i].address = sg_dma_address(sgl);
  1103. ccb->sgl64[i].length = sg_dma_len(sgl);
  1104. }
  1105. // Return count of SG nodes
  1106. return sgcnt;
  1107. }
  1108. /**
  1109. * mbox_post_cmd - issue a mailbox command
  1110. * @adapter : controller's soft state
  1111. * @scb : command to be issued
  1112. *
  1113. * Post the command to the controller if mailbox is available.
  1114. */
  1115. static int
  1116. mbox_post_cmd(adapter_t *adapter, scb_t *scb)
  1117. {
  1118. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1119. mbox64_t *mbox64;
  1120. mbox_t *mbox;
  1121. mbox_ccb_t *ccb;
  1122. unsigned long flags;
  1123. unsigned int i = 0;
  1124. ccb = (mbox_ccb_t *)scb->ccb;
  1125. mbox = raid_dev->mbox;
  1126. mbox64 = raid_dev->mbox64;
  1127. /*
  1128. * Check for busy mailbox. If it is, return failure - the caller
  1129. * should retry later.
  1130. */
  1131. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1132. if (unlikely(mbox->busy)) {
  1133. do {
  1134. udelay(1);
  1135. i++;
  1136. rmb();
  1137. } while(mbox->busy && (i < max_mbox_busy_wait));
  1138. if (mbox->busy) {
  1139. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1140. return -1;
  1141. }
  1142. }
  1143. // Copy this command's mailbox data into "adapter's" mailbox
  1144. memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
  1145. mbox->cmdid = scb->sno;
  1146. adapter->outstanding_cmds++;
  1147. if (scb->dma_direction == PCI_DMA_TODEVICE)
  1148. pci_dma_sync_sg_for_device(adapter->pdev,
  1149. scsi_sglist(scb->scp),
  1150. scsi_sg_count(scb->scp),
  1151. PCI_DMA_TODEVICE);
  1152. mbox->busy = 1; // Set busy
  1153. mbox->poll = 0;
  1154. mbox->ack = 0;
  1155. wmb();
  1156. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  1157. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1158. return 0;
  1159. }
  1160. /**
  1161. * megaraid_queue_command - generic queue entry point for all LLDs
  1162. * @scp : pointer to the scsi command to be executed
  1163. * @done : callback routine to be called after the cmd has be completed
  1164. *
  1165. * Queue entry point for mailbox based controllers.
  1166. */
  1167. static int
  1168. megaraid_queue_command_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
  1169. {
  1170. adapter_t *adapter;
  1171. scb_t *scb;
  1172. int if_busy;
  1173. adapter = SCP2ADAPTER(scp);
  1174. scp->scsi_done = done;
  1175. scp->result = 0;
  1176. /*
  1177. * Allocate and build a SCB request
  1178. * if_busy flag will be set if megaraid_mbox_build_cmd() command could
  1179. * not allocate scb. We will return non-zero status in that case.
  1180. * NOTE: scb can be null even though certain commands completed
  1181. * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
  1182. * return 0 in that case, and we would do the callback right away.
  1183. */
  1184. if_busy = 0;
  1185. scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
  1186. if (!scb) { // command already completed
  1187. done(scp);
  1188. return 0;
  1189. }
  1190. megaraid_mbox_runpendq(adapter, scb);
  1191. return if_busy;
  1192. }
  1193. static DEF_SCSI_QCMD(megaraid_queue_command)
  1194. /**
  1195. * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
  1196. * @adapter : controller's soft state
  1197. * @scp : mid-layer scsi command pointer
  1198. * @busy : set if request could not be completed because of lack of
  1199. * resources
  1200. *
  1201. * Transform the mid-layer scsi command to megaraid firmware lingua.
  1202. * Convert the command issued by mid-layer to format understood by megaraid
  1203. * firmware. We also complete certain commands without sending them to firmware.
  1204. */
  1205. static scb_t *
  1206. megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
  1207. {
  1208. mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
  1209. int channel;
  1210. int target;
  1211. int islogical;
  1212. mbox_ccb_t *ccb;
  1213. mraid_passthru_t *pthru;
  1214. mbox64_t *mbox64;
  1215. mbox_t *mbox;
  1216. scb_t *scb;
  1217. char skip[] = "skipping";
  1218. char scan[] = "scanning";
  1219. char *ss;
  1220. /*
  1221. * Get the appropriate device map for the device this command is
  1222. * intended for
  1223. */
  1224. MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
  1225. /*
  1226. * Logical drive commands
  1227. */
  1228. if (islogical) {
  1229. switch (scp->cmnd[0]) {
  1230. case TEST_UNIT_READY:
  1231. /*
  1232. * Do we support clustering and is the support enabled
  1233. * If no, return success always
  1234. */
  1235. if (!adapter->ha) {
  1236. scp->result = (DID_OK << 16);
  1237. return NULL;
  1238. }
  1239. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1240. scp->result = (DID_ERROR << 16);
  1241. *busy = 1;
  1242. return NULL;
  1243. }
  1244. scb->dma_direction = scp->sc_data_direction;
  1245. scb->dev_channel = 0xFF;
  1246. scb->dev_target = target;
  1247. ccb = (mbox_ccb_t *)scb->ccb;
  1248. /*
  1249. * The command id will be provided by the command
  1250. * issuance routine
  1251. */
  1252. ccb->raw_mbox[0] = CLUSTER_CMD;
  1253. ccb->raw_mbox[2] = RESERVATION_STATUS;
  1254. ccb->raw_mbox[3] = target;
  1255. return scb;
  1256. case MODE_SENSE:
  1257. {
  1258. struct scatterlist *sgl;
  1259. caddr_t vaddr;
  1260. sgl = scsi_sglist(scp);
  1261. if (sg_page(sgl)) {
  1262. vaddr = (caddr_t) sg_virt(&sgl[0]);
  1263. memset(vaddr, 0, scp->cmnd[4]);
  1264. }
  1265. else {
  1266. con_log(CL_ANN, (KERN_WARNING
  1267. "megaraid mailbox: invalid sg:%d\n",
  1268. __LINE__));
  1269. }
  1270. }
  1271. scp->result = (DID_OK << 16);
  1272. return NULL;
  1273. case INQUIRY:
  1274. /*
  1275. * Display the channel scan for logical drives
  1276. * Do not display scan for a channel if already done.
  1277. */
  1278. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1279. con_log(CL_ANN, (KERN_INFO
  1280. "scsi[%d]: scanning scsi channel %d",
  1281. adapter->host->host_no,
  1282. SCP2CHANNEL(scp)));
  1283. con_log(CL_ANN, (
  1284. " [virtual] for logical drives\n"));
  1285. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1286. }
  1287. if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
  1288. scp->sense_buffer[0] = 0x70;
  1289. scp->sense_buffer[2] = ILLEGAL_REQUEST;
  1290. scp->sense_buffer[12] = MEGA_INVALID_FIELD_IN_CDB;
  1291. scp->result = CHECK_CONDITION << 1;
  1292. return NULL;
  1293. }
  1294. /* Fall through */
  1295. case READ_CAPACITY:
  1296. /*
  1297. * Do not allow LUN > 0 for logical drives and
  1298. * requests for more than 40 logical drives
  1299. */
  1300. if (SCP2LUN(scp)) {
  1301. scp->result = (DID_BAD_TARGET << 16);
  1302. return NULL;
  1303. }
  1304. if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
  1305. scp->result = (DID_BAD_TARGET << 16);
  1306. return NULL;
  1307. }
  1308. /* Allocate a SCB and initialize passthru */
  1309. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1310. scp->result = (DID_ERROR << 16);
  1311. *busy = 1;
  1312. return NULL;
  1313. }
  1314. ccb = (mbox_ccb_t *)scb->ccb;
  1315. scb->dev_channel = 0xFF;
  1316. scb->dev_target = target;
  1317. pthru = ccb->pthru;
  1318. mbox = ccb->mbox;
  1319. mbox64 = ccb->mbox64;
  1320. pthru->timeout = 0;
  1321. pthru->ars = 1;
  1322. pthru->reqsenselen = 14;
  1323. pthru->islogical = 1;
  1324. pthru->logdrv = target;
  1325. pthru->cdblen = scp->cmd_len;
  1326. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1327. mbox->cmd = MBOXCMD_PASSTHRU64;
  1328. scb->dma_direction = scp->sc_data_direction;
  1329. pthru->dataxferlen = scsi_bufflen(scp);
  1330. pthru->dataxferaddr = ccb->sgl_dma_h;
  1331. pthru->numsge = megaraid_mbox_mksgl(adapter,
  1332. scb);
  1333. mbox->xferaddr = 0xFFFFFFFF;
  1334. mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
  1335. mbox64->xferaddr_hi = 0;
  1336. return scb;
  1337. case READ_6:
  1338. case WRITE_6:
  1339. case READ_10:
  1340. case WRITE_10:
  1341. case READ_12:
  1342. case WRITE_12:
  1343. /*
  1344. * Allocate a SCB and initialize mailbox
  1345. */
  1346. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1347. scp->result = (DID_ERROR << 16);
  1348. *busy = 1;
  1349. return NULL;
  1350. }
  1351. ccb = (mbox_ccb_t *)scb->ccb;
  1352. scb->dev_channel = 0xFF;
  1353. scb->dev_target = target;
  1354. mbox = ccb->mbox;
  1355. mbox64 = ccb->mbox64;
  1356. mbox->logdrv = target;
  1357. /*
  1358. * A little HACK: 2nd bit is zero for all scsi read
  1359. * commands and is set for all scsi write commands
  1360. */
  1361. mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
  1362. MBOXCMD_LREAD64 ;
  1363. /*
  1364. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1365. */
  1366. if (scp->cmd_len == 6) {
  1367. mbox->numsectors = (uint32_t)scp->cmnd[4];
  1368. mbox->lba =
  1369. ((uint32_t)scp->cmnd[1] << 16) |
  1370. ((uint32_t)scp->cmnd[2] << 8) |
  1371. (uint32_t)scp->cmnd[3];
  1372. mbox->lba &= 0x1FFFFF;
  1373. }
  1374. /*
  1375. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1376. */
  1377. else if (scp->cmd_len == 10) {
  1378. mbox->numsectors =
  1379. (uint32_t)scp->cmnd[8] |
  1380. ((uint32_t)scp->cmnd[7] << 8);
  1381. mbox->lba =
  1382. ((uint32_t)scp->cmnd[2] << 24) |
  1383. ((uint32_t)scp->cmnd[3] << 16) |
  1384. ((uint32_t)scp->cmnd[4] << 8) |
  1385. (uint32_t)scp->cmnd[5];
  1386. }
  1387. /*
  1388. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1389. */
  1390. else if (scp->cmd_len == 12) {
  1391. mbox->lba =
  1392. ((uint32_t)scp->cmnd[2] << 24) |
  1393. ((uint32_t)scp->cmnd[3] << 16) |
  1394. ((uint32_t)scp->cmnd[4] << 8) |
  1395. (uint32_t)scp->cmnd[5];
  1396. mbox->numsectors =
  1397. ((uint32_t)scp->cmnd[6] << 24) |
  1398. ((uint32_t)scp->cmnd[7] << 16) |
  1399. ((uint32_t)scp->cmnd[8] << 8) |
  1400. (uint32_t)scp->cmnd[9];
  1401. }
  1402. else {
  1403. con_log(CL_ANN, (KERN_WARNING
  1404. "megaraid: unsupported CDB length\n"));
  1405. megaraid_dealloc_scb(adapter, scb);
  1406. scp->result = (DID_ERROR << 16);
  1407. return NULL;
  1408. }
  1409. scb->dma_direction = scp->sc_data_direction;
  1410. // Calculate Scatter-Gather info
  1411. mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
  1412. mbox->numsge = megaraid_mbox_mksgl(adapter,
  1413. scb);
  1414. mbox->xferaddr = 0xFFFFFFFF;
  1415. mbox64->xferaddr_hi = 0;
  1416. return scb;
  1417. case RESERVE:
  1418. case RELEASE:
  1419. /*
  1420. * Do we support clustering and is the support enabled
  1421. */
  1422. if (!adapter->ha) {
  1423. scp->result = (DID_BAD_TARGET << 16);
  1424. return NULL;
  1425. }
  1426. /*
  1427. * Allocate a SCB and initialize mailbox
  1428. */
  1429. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1430. scp->result = (DID_ERROR << 16);
  1431. *busy = 1;
  1432. return NULL;
  1433. }
  1434. ccb = (mbox_ccb_t *)scb->ccb;
  1435. scb->dev_channel = 0xFF;
  1436. scb->dev_target = target;
  1437. ccb->raw_mbox[0] = CLUSTER_CMD;
  1438. ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
  1439. RESERVE_LD : RELEASE_LD;
  1440. ccb->raw_mbox[3] = target;
  1441. scb->dma_direction = scp->sc_data_direction;
  1442. return scb;
  1443. default:
  1444. scp->result = (DID_BAD_TARGET << 16);
  1445. return NULL;
  1446. }
  1447. }
  1448. else { // Passthru device commands
  1449. // Do not allow access to target id > 15 or LUN > 7
  1450. if (target > 15 || SCP2LUN(scp) > 7) {
  1451. scp->result = (DID_BAD_TARGET << 16);
  1452. return NULL;
  1453. }
  1454. // if fast load option was set and scan for last device is
  1455. // over, reset the fast_load flag so that during a possible
  1456. // next scan, devices can be made available
  1457. if (rdev->fast_load && (target == 15) &&
  1458. (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
  1459. con_log(CL_ANN, (KERN_INFO
  1460. "megaraid[%d]: physical device scan re-enabled\n",
  1461. adapter->host->host_no));
  1462. rdev->fast_load = 0;
  1463. }
  1464. /*
  1465. * Display the channel scan for physical devices
  1466. */
  1467. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1468. ss = rdev->fast_load ? skip : scan;
  1469. con_log(CL_ANN, (KERN_INFO
  1470. "scsi[%d]: %s scsi channel %d [Phy %d]",
  1471. adapter->host->host_no, ss, SCP2CHANNEL(scp),
  1472. channel));
  1473. con_log(CL_ANN, (
  1474. " for non-raid devices\n"));
  1475. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1476. }
  1477. // disable channel sweep if fast load option given
  1478. if (rdev->fast_load) {
  1479. scp->result = (DID_BAD_TARGET << 16);
  1480. return NULL;
  1481. }
  1482. // Allocate a SCB and initialize passthru
  1483. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1484. scp->result = (DID_ERROR << 16);
  1485. *busy = 1;
  1486. return NULL;
  1487. }
  1488. ccb = (mbox_ccb_t *)scb->ccb;
  1489. scb->dev_channel = channel;
  1490. scb->dev_target = target;
  1491. scb->dma_direction = scp->sc_data_direction;
  1492. mbox = ccb->mbox;
  1493. mbox64 = ccb->mbox64;
  1494. // Does this firmware support extended CDBs
  1495. if (adapter->max_cdb_sz == 16) {
  1496. mbox->cmd = MBOXCMD_EXTPTHRU;
  1497. megaraid_mbox_prepare_epthru(adapter, scb, scp);
  1498. mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
  1499. mbox64->xferaddr_hi = 0;
  1500. mbox->xferaddr = 0xFFFFFFFF;
  1501. }
  1502. else {
  1503. mbox->cmd = MBOXCMD_PASSTHRU64;
  1504. megaraid_mbox_prepare_pthru(adapter, scb, scp);
  1505. mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
  1506. mbox64->xferaddr_hi = 0;
  1507. mbox->xferaddr = 0xFFFFFFFF;
  1508. }
  1509. return scb;
  1510. }
  1511. // NOT REACHED
  1512. }
  1513. /**
  1514. * megaraid_mbox_runpendq - execute commands queued in the pending queue
  1515. * @adapter : controller's soft state
  1516. * @scb_q : SCB to be queued in the pending list
  1517. *
  1518. * Scan the pending list for commands which are not yet issued and try to
  1519. * post to the controller. The SCB can be a null pointer, which would indicate
  1520. * no SCB to be queue, just try to execute the ones in the pending list.
  1521. *
  1522. * NOTE: We do not actually traverse the pending list. The SCBs are plucked
  1523. * out from the head of the pending list. If it is successfully issued, the
  1524. * next SCB is at the head now.
  1525. */
  1526. static void
  1527. megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
  1528. {
  1529. scb_t *scb;
  1530. unsigned long flags;
  1531. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1532. if (scb_q) {
  1533. scb_q->state = SCB_PENDQ;
  1534. list_add_tail(&scb_q->list, &adapter->pend_list);
  1535. }
  1536. // if the adapter in not in quiescent mode, post the commands to FW
  1537. if (adapter->quiescent) {
  1538. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1539. return;
  1540. }
  1541. while (!list_empty(&adapter->pend_list)) {
  1542. assert_spin_locked(PENDING_LIST_LOCK(adapter));
  1543. scb = list_entry(adapter->pend_list.next, scb_t, list);
  1544. // remove the scb from the pending list and try to
  1545. // issue. If we are unable to issue it, put back in
  1546. // the pending list and return
  1547. list_del_init(&scb->list);
  1548. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1549. // if mailbox was busy, return SCB back to pending
  1550. // list. Make sure to add at the head, since that's
  1551. // where it would have been removed from
  1552. scb->state = SCB_ISSUED;
  1553. if (mbox_post_cmd(adapter, scb) != 0) {
  1554. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1555. scb->state = SCB_PENDQ;
  1556. list_add(&scb->list, &adapter->pend_list);
  1557. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  1558. flags);
  1559. return;
  1560. }
  1561. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1562. }
  1563. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1564. return;
  1565. }
  1566. /**
  1567. * megaraid_mbox_prepare_pthru - prepare a command for physical devices
  1568. * @adapter : pointer to controller's soft state
  1569. * @scb : scsi control block
  1570. * @scp : scsi command from the mid-layer
  1571. *
  1572. * Prepare a command for the scsi physical devices.
  1573. */
  1574. static void
  1575. megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
  1576. struct scsi_cmnd *scp)
  1577. {
  1578. mbox_ccb_t *ccb;
  1579. mraid_passthru_t *pthru;
  1580. uint8_t channel;
  1581. uint8_t target;
  1582. ccb = (mbox_ccb_t *)scb->ccb;
  1583. pthru = ccb->pthru;
  1584. channel = scb->dev_channel;
  1585. target = scb->dev_target;
  1586. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1587. pthru->timeout = 4;
  1588. pthru->ars = 1;
  1589. pthru->islogical = 0;
  1590. pthru->channel = 0;
  1591. pthru->target = (channel << 4) | target;
  1592. pthru->logdrv = SCP2LUN(scp);
  1593. pthru->reqsenselen = 14;
  1594. pthru->cdblen = scp->cmd_len;
  1595. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1596. if (scsi_bufflen(scp)) {
  1597. pthru->dataxferlen = scsi_bufflen(scp);
  1598. pthru->dataxferaddr = ccb->sgl_dma_h;
  1599. pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1600. }
  1601. else {
  1602. pthru->dataxferaddr = 0;
  1603. pthru->dataxferlen = 0;
  1604. pthru->numsge = 0;
  1605. }
  1606. return;
  1607. }
  1608. /**
  1609. * megaraid_mbox_prepare_epthru - prepare a command for physical devices
  1610. * @adapter : pointer to controller's soft state
  1611. * @scb : scsi control block
  1612. * @scp : scsi command from the mid-layer
  1613. *
  1614. * Prepare a command for the scsi physical devices. This routine prepares
  1615. * commands for devices which can take extended CDBs (>10 bytes).
  1616. */
  1617. static void
  1618. megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
  1619. struct scsi_cmnd *scp)
  1620. {
  1621. mbox_ccb_t *ccb;
  1622. mraid_epassthru_t *epthru;
  1623. uint8_t channel;
  1624. uint8_t target;
  1625. ccb = (mbox_ccb_t *)scb->ccb;
  1626. epthru = ccb->epthru;
  1627. channel = scb->dev_channel;
  1628. target = scb->dev_target;
  1629. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1630. epthru->timeout = 4;
  1631. epthru->ars = 1;
  1632. epthru->islogical = 0;
  1633. epthru->channel = 0;
  1634. epthru->target = (channel << 4) | target;
  1635. epthru->logdrv = SCP2LUN(scp);
  1636. epthru->reqsenselen = 14;
  1637. epthru->cdblen = scp->cmd_len;
  1638. memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
  1639. if (scsi_bufflen(scp)) {
  1640. epthru->dataxferlen = scsi_bufflen(scp);
  1641. epthru->dataxferaddr = ccb->sgl_dma_h;
  1642. epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1643. }
  1644. else {
  1645. epthru->dataxferaddr = 0;
  1646. epthru->dataxferlen = 0;
  1647. epthru->numsge = 0;
  1648. }
  1649. return;
  1650. }
  1651. /**
  1652. * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
  1653. * @adapter : controller's soft state
  1654. *
  1655. * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
  1656. * completed command and put them on the completed list for later processing.
  1657. *
  1658. * Returns: 1 if the interrupt is valid, 0 otherwise
  1659. */
  1660. static int
  1661. megaraid_ack_sequence(adapter_t *adapter)
  1662. {
  1663. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1664. mbox_t *mbox;
  1665. scb_t *scb;
  1666. uint8_t nstatus;
  1667. uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
  1668. struct list_head clist;
  1669. int handled;
  1670. uint32_t dword;
  1671. unsigned long flags;
  1672. int i, j;
  1673. mbox = raid_dev->mbox;
  1674. // move the SCBs from the firmware completed array to our local list
  1675. INIT_LIST_HEAD(&clist);
  1676. // loop till F/W has more commands for us to complete
  1677. handled = 0;
  1678. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1679. do {
  1680. /*
  1681. * Check if a valid interrupt is pending. If found, force the
  1682. * interrupt line low.
  1683. */
  1684. dword = RDOUTDOOR(raid_dev);
  1685. if (dword != 0x10001234) break;
  1686. handled = 1;
  1687. WROUTDOOR(raid_dev, 0x10001234);
  1688. nstatus = 0;
  1689. // wait for valid numstatus to post
  1690. for (i = 0; i < 0xFFFFF; i++) {
  1691. if (mbox->numstatus != 0xFF) {
  1692. nstatus = mbox->numstatus;
  1693. break;
  1694. }
  1695. rmb();
  1696. }
  1697. mbox->numstatus = 0xFF;
  1698. adapter->outstanding_cmds -= nstatus;
  1699. for (i = 0; i < nstatus; i++) {
  1700. // wait for valid command index to post
  1701. for (j = 0; j < 0xFFFFF; j++) {
  1702. if (mbox->completed[i] != 0xFF) break;
  1703. rmb();
  1704. }
  1705. completed[i] = mbox->completed[i];
  1706. mbox->completed[i] = 0xFF;
  1707. if (completed[i] == 0xFF) {
  1708. con_log(CL_ANN, (KERN_CRIT
  1709. "megaraid: command posting timed out\n"));
  1710. BUG();
  1711. continue;
  1712. }
  1713. // Get SCB associated with this command id
  1714. if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
  1715. // a cmm command
  1716. scb = adapter->uscb_list + (completed[i] -
  1717. MBOX_MAX_SCSI_CMDS);
  1718. }
  1719. else {
  1720. // an os command
  1721. scb = adapter->kscb_list + completed[i];
  1722. }
  1723. scb->status = mbox->status;
  1724. list_add_tail(&scb->list, &clist);
  1725. }
  1726. // Acknowledge interrupt
  1727. WRINDOOR(raid_dev, 0x02);
  1728. } while(1);
  1729. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1730. // put the completed commands in the completed list. DPC would
  1731. // complete these commands later
  1732. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1733. list_splice(&clist, &adapter->completed_list);
  1734. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1735. // schedule the DPC if there is some work for it
  1736. if (handled)
  1737. tasklet_schedule(&adapter->dpc_h);
  1738. return handled;
  1739. }
  1740. /**
  1741. * megaraid_isr - isr for memory based mailbox based controllers
  1742. * @irq : irq
  1743. * @devp : pointer to our soft state
  1744. *
  1745. * Interrupt service routine for memory-mapped mailbox controllers.
  1746. */
  1747. static irqreturn_t
  1748. megaraid_isr(int irq, void *devp)
  1749. {
  1750. adapter_t *adapter = devp;
  1751. int handled;
  1752. handled = megaraid_ack_sequence(adapter);
  1753. /* Loop through any pending requests */
  1754. if (!adapter->quiescent) {
  1755. megaraid_mbox_runpendq(adapter, NULL);
  1756. }
  1757. return IRQ_RETVAL(handled);
  1758. }
  1759. /**
  1760. * megaraid_mbox_sync_scb - sync kernel buffers
  1761. * @adapter : controller's soft state
  1762. * @scb : pointer to the resource packet
  1763. *
  1764. * DMA sync if required.
  1765. */
  1766. static void
  1767. megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
  1768. {
  1769. mbox_ccb_t *ccb;
  1770. ccb = (mbox_ccb_t *)scb->ccb;
  1771. if (scb->dma_direction == PCI_DMA_FROMDEVICE)
  1772. pci_dma_sync_sg_for_cpu(adapter->pdev,
  1773. scsi_sglist(scb->scp),
  1774. scsi_sg_count(scb->scp),
  1775. PCI_DMA_FROMDEVICE);
  1776. scsi_dma_unmap(scb->scp);
  1777. return;
  1778. }
  1779. /**
  1780. * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
  1781. * @devp : pointer to HBA soft state
  1782. *
  1783. * Pick up the commands from the completed list and send back to the owners.
  1784. * This is a reentrant function and does not assume any locks are held while
  1785. * it is being called.
  1786. */
  1787. static void
  1788. megaraid_mbox_dpc(unsigned long devp)
  1789. {
  1790. adapter_t *adapter = (adapter_t *)devp;
  1791. mraid_device_t *raid_dev;
  1792. struct list_head clist;
  1793. struct scatterlist *sgl;
  1794. scb_t *scb;
  1795. scb_t *tmp;
  1796. struct scsi_cmnd *scp;
  1797. mraid_passthru_t *pthru;
  1798. mraid_epassthru_t *epthru;
  1799. mbox_ccb_t *ccb;
  1800. int islogical;
  1801. int pdev_index;
  1802. int pdev_state;
  1803. mbox_t *mbox;
  1804. unsigned long flags;
  1805. uint8_t c;
  1806. int status;
  1807. uioc_t *kioc;
  1808. if (!adapter) return;
  1809. raid_dev = ADAP2RAIDDEV(adapter);
  1810. // move the SCBs from the completed list to our local list
  1811. INIT_LIST_HEAD(&clist);
  1812. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1813. list_splice_init(&adapter->completed_list, &clist);
  1814. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1815. list_for_each_entry_safe(scb, tmp, &clist, list) {
  1816. status = scb->status;
  1817. scp = scb->scp;
  1818. ccb = (mbox_ccb_t *)scb->ccb;
  1819. pthru = ccb->pthru;
  1820. epthru = ccb->epthru;
  1821. mbox = ccb->mbox;
  1822. // Make sure f/w has completed a valid command
  1823. if (scb->state != SCB_ISSUED) {
  1824. con_log(CL_ANN, (KERN_CRIT
  1825. "megaraid critical err: invalid command %d:%d:%p\n",
  1826. scb->sno, scb->state, scp));
  1827. BUG();
  1828. continue; // Must never happen!
  1829. }
  1830. // check for the management command and complete it right away
  1831. if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
  1832. scb->state = SCB_FREE;
  1833. scb->status = status;
  1834. // remove from local clist
  1835. list_del_init(&scb->list);
  1836. kioc = (uioc_t *)scb->gp;
  1837. kioc->status = 0;
  1838. megaraid_mbox_mm_done(adapter, scb);
  1839. continue;
  1840. }
  1841. // Was an abort issued for this command earlier
  1842. if (scb->state & SCB_ABORT) {
  1843. con_log(CL_ANN, (KERN_NOTICE
  1844. "megaraid: aborted cmd [%x] completed\n",
  1845. scb->sno));
  1846. }
  1847. /*
  1848. * If the inquiry came of a disk drive which is not part of
  1849. * any RAID array, expose it to the kernel. For this to be
  1850. * enabled, user must set the "megaraid_expose_unconf_disks"
  1851. * flag to 1 by specifying it on module parameter list.
  1852. * This would enable data migration off drives from other
  1853. * configurations.
  1854. */
  1855. islogical = MRAID_IS_LOGICAL(adapter, scp);
  1856. if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
  1857. && IS_RAID_CH(raid_dev, scb->dev_channel)) {
  1858. sgl = scsi_sglist(scp);
  1859. if (sg_page(sgl)) {
  1860. c = *(unsigned char *) sg_virt(&sgl[0]);
  1861. } else {
  1862. con_log(CL_ANN, (KERN_WARNING
  1863. "megaraid mailbox: invalid sg:%d\n",
  1864. __LINE__));
  1865. c = 0;
  1866. }
  1867. if ((c & 0x1F ) == TYPE_DISK) {
  1868. pdev_index = (scb->dev_channel * 16) +
  1869. scb->dev_target;
  1870. pdev_state =
  1871. raid_dev->pdrv_state[pdev_index] & 0x0F;
  1872. if (pdev_state == PDRV_ONLINE ||
  1873. pdev_state == PDRV_FAILED ||
  1874. pdev_state == PDRV_RBLD ||
  1875. pdev_state == PDRV_HOTSPARE ||
  1876. megaraid_expose_unconf_disks == 0) {
  1877. status = 0xF0;
  1878. }
  1879. }
  1880. }
  1881. // Convert MegaRAID status to Linux error code
  1882. switch (status) {
  1883. case 0x00:
  1884. scp->result = (DID_OK << 16);
  1885. break;
  1886. case 0x02:
  1887. /* set sense_buffer and result fields */
  1888. if (mbox->cmd == MBOXCMD_PASSTHRU ||
  1889. mbox->cmd == MBOXCMD_PASSTHRU64) {
  1890. memcpy(scp->sense_buffer, pthru->reqsensearea,
  1891. 14);
  1892. scp->result = DRIVER_SENSE << 24 |
  1893. DID_OK << 16 | CHECK_CONDITION << 1;
  1894. }
  1895. else {
  1896. if (mbox->cmd == MBOXCMD_EXTPTHRU) {
  1897. memcpy(scp->sense_buffer,
  1898. epthru->reqsensearea, 14);
  1899. scp->result = DRIVER_SENSE << 24 |
  1900. DID_OK << 16 |
  1901. CHECK_CONDITION << 1;
  1902. } else {
  1903. scp->sense_buffer[0] = 0x70;
  1904. scp->sense_buffer[2] = ABORTED_COMMAND;
  1905. scp->result = CHECK_CONDITION << 1;
  1906. }
  1907. }
  1908. break;
  1909. case 0x08:
  1910. scp->result = DID_BUS_BUSY << 16 | status;
  1911. break;
  1912. default:
  1913. /*
  1914. * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
  1915. * failed
  1916. */
  1917. if (scp->cmnd[0] == TEST_UNIT_READY) {
  1918. scp->result = DID_ERROR << 16 |
  1919. RESERVATION_CONFLICT << 1;
  1920. }
  1921. else
  1922. /*
  1923. * Error code returned is 1 if Reserve or Release
  1924. * failed or the input parameter is invalid
  1925. */
  1926. if (status == 1 && (scp->cmnd[0] == RESERVE ||
  1927. scp->cmnd[0] == RELEASE)) {
  1928. scp->result = DID_ERROR << 16 |
  1929. RESERVATION_CONFLICT << 1;
  1930. }
  1931. else {
  1932. scp->result = DID_BAD_TARGET << 16 | status;
  1933. }
  1934. }
  1935. // print a debug message for all failed commands
  1936. if (status) {
  1937. megaraid_mbox_display_scb(adapter, scb);
  1938. }
  1939. // Free our internal resources and call the mid-layer callback
  1940. // routine
  1941. megaraid_mbox_sync_scb(adapter, scb);
  1942. // remove from local clist
  1943. list_del_init(&scb->list);
  1944. // put back in free list
  1945. megaraid_dealloc_scb(adapter, scb);
  1946. // send the scsi packet back to kernel
  1947. scp->scsi_done(scp);
  1948. }
  1949. return;
  1950. }
  1951. /**
  1952. * megaraid_abort_handler - abort the scsi command
  1953. * @scp : command to be aborted
  1954. *
  1955. * Abort a previous SCSI request. Only commands on the pending list can be
  1956. * aborted. All the commands issued to the F/W must complete.
  1957. **/
  1958. static int
  1959. megaraid_abort_handler(struct scsi_cmnd *scp)
  1960. {
  1961. adapter_t *adapter;
  1962. mraid_device_t *raid_dev;
  1963. scb_t *scb;
  1964. scb_t *tmp;
  1965. int found;
  1966. unsigned long flags;
  1967. int i;
  1968. adapter = SCP2ADAPTER(scp);
  1969. raid_dev = ADAP2RAIDDEV(adapter);
  1970. con_log(CL_ANN, (KERN_WARNING
  1971. "megaraid: aborting cmd=%x <c=%d t=%d l=%d>\n",
  1972. scp->cmnd[0], SCP2CHANNEL(scp),
  1973. SCP2TARGET(scp), SCP2LUN(scp)));
  1974. // If FW has stopped responding, simply return failure
  1975. if (raid_dev->hw_error) {
  1976. con_log(CL_ANN, (KERN_NOTICE
  1977. "megaraid: hw error, not aborting\n"));
  1978. return FAILED;
  1979. }
  1980. // There might a race here, where the command was completed by the
  1981. // firmware and now it is on the completed list. Before we could
  1982. // complete the command to the kernel in dpc, the abort came.
  1983. // Find out if this is the case to avoid the race.
  1984. scb = NULL;
  1985. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1986. list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
  1987. if (scb->scp == scp) { // Found command
  1988. list_del_init(&scb->list); // from completed list
  1989. con_log(CL_ANN, (KERN_WARNING
  1990. "megaraid: %d[%d:%d], abort from completed list\n",
  1991. scb->sno, scb->dev_channel, scb->dev_target));
  1992. scp->result = (DID_ABORT << 16);
  1993. scp->scsi_done(scp);
  1994. megaraid_dealloc_scb(adapter, scb);
  1995. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
  1996. flags);
  1997. return SUCCESS;
  1998. }
  1999. }
  2000. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  2001. // Find out if this command is still on the pending list. If it is and
  2002. // was never issued, abort and return success. If the command is owned
  2003. // by the firmware, we must wait for it to complete by the FW.
  2004. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2005. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2006. if (scb->scp == scp) { // Found command
  2007. list_del_init(&scb->list); // from pending list
  2008. ASSERT(!(scb->state & SCB_ISSUED));
  2009. con_log(CL_ANN, (KERN_WARNING
  2010. "megaraid abort: [%d:%d], driver owner\n",
  2011. scb->dev_channel, scb->dev_target));
  2012. scp->result = (DID_ABORT << 16);
  2013. scp->scsi_done(scp);
  2014. megaraid_dealloc_scb(adapter, scb);
  2015. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  2016. flags);
  2017. return SUCCESS;
  2018. }
  2019. }
  2020. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2021. // Check do we even own this command, in which case this would be
  2022. // owned by the firmware. The only way to locate the FW scb is to
  2023. // traverse through the list of all SCB, since driver does not
  2024. // maintain these SCBs on any list
  2025. found = 0;
  2026. spin_lock_irq(&adapter->lock);
  2027. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  2028. scb = adapter->kscb_list + i;
  2029. if (scb->scp == scp) {
  2030. found = 1;
  2031. if (!(scb->state & SCB_ISSUED)) {
  2032. con_log(CL_ANN, (KERN_WARNING
  2033. "megaraid abort: %d[%d:%d], invalid state\n",
  2034. scb->sno, scb->dev_channel, scb->dev_target));
  2035. BUG();
  2036. }
  2037. else {
  2038. con_log(CL_ANN, (KERN_WARNING
  2039. "megaraid abort: %d[%d:%d], fw owner\n",
  2040. scb->sno, scb->dev_channel, scb->dev_target));
  2041. }
  2042. }
  2043. }
  2044. spin_unlock_irq(&adapter->lock);
  2045. if (!found) {
  2046. con_log(CL_ANN, (KERN_WARNING "megaraid abort: do now own\n"));
  2047. // FIXME: Should there be a callback for this command?
  2048. return SUCCESS;
  2049. }
  2050. // We cannot actually abort a command owned by firmware, return
  2051. // failure and wait for reset. In host reset handler, we will find out
  2052. // if the HBA is still live
  2053. return FAILED;
  2054. }
  2055. /**
  2056. * megaraid_reset_handler - device reset handler for mailbox based driver
  2057. * @scp : reference command
  2058. *
  2059. * Reset handler for the mailbox based controller. First try to find out if
  2060. * the FW is still live, in which case the outstanding commands counter mut go
  2061. * down to 0. If that happens, also issue the reservation reset command to
  2062. * relinquish (possible) reservations on the logical drives connected to this
  2063. * host.
  2064. **/
  2065. static int
  2066. megaraid_reset_handler(struct scsi_cmnd *scp)
  2067. {
  2068. adapter_t *adapter;
  2069. scb_t *scb;
  2070. scb_t *tmp;
  2071. mraid_device_t *raid_dev;
  2072. unsigned long flags;
  2073. uint8_t raw_mbox[sizeof(mbox_t)];
  2074. int rval;
  2075. int recovery_window;
  2076. int recovering;
  2077. int i;
  2078. uioc_t *kioc;
  2079. adapter = SCP2ADAPTER(scp);
  2080. raid_dev = ADAP2RAIDDEV(adapter);
  2081. // return failure if adapter is not responding
  2082. if (raid_dev->hw_error) {
  2083. con_log(CL_ANN, (KERN_NOTICE
  2084. "megaraid: hw error, cannot reset\n"));
  2085. return FAILED;
  2086. }
  2087. // Under exceptional conditions, FW can take up to 3 minutes to
  2088. // complete command processing. Wait for additional 2 minutes for the
  2089. // pending commands counter to go down to 0. If it doesn't, let the
  2090. // controller be marked offline
  2091. // Also, reset all the commands currently owned by the driver
  2092. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2093. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2094. list_del_init(&scb->list); // from pending list
  2095. if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
  2096. con_log(CL_ANN, (KERN_WARNING
  2097. "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
  2098. scb->sno, scb->dev_channel, scb->dev_target));
  2099. scb->status = -1;
  2100. kioc = (uioc_t *)scb->gp;
  2101. kioc->status = -EFAULT;
  2102. megaraid_mbox_mm_done(adapter, scb);
  2103. } else {
  2104. if (scb->scp == scp) { // Found command
  2105. con_log(CL_ANN, (KERN_WARNING
  2106. "megaraid: %d[%d:%d], reset from pending list\n",
  2107. scb->sno, scb->dev_channel, scb->dev_target));
  2108. } else {
  2109. con_log(CL_ANN, (KERN_WARNING
  2110. "megaraid: IO packet with %d[%d:%d] being reset\n",
  2111. scb->sno, scb->dev_channel, scb->dev_target));
  2112. }
  2113. scb->scp->result = (DID_RESET << 16);
  2114. scb->scp->scsi_done(scb->scp);
  2115. megaraid_dealloc_scb(adapter, scb);
  2116. }
  2117. }
  2118. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2119. if (adapter->outstanding_cmds) {
  2120. con_log(CL_ANN, (KERN_NOTICE
  2121. "megaraid: %d outstanding commands. Max wait %d sec\n",
  2122. adapter->outstanding_cmds,
  2123. (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
  2124. }
  2125. recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
  2126. recovering = adapter->outstanding_cmds;
  2127. for (i = 0; i < recovery_window; i++) {
  2128. megaraid_ack_sequence(adapter);
  2129. // print a message once every 5 seconds only
  2130. if (!(i % 5)) {
  2131. con_log(CL_ANN, (
  2132. "megaraid mbox: Wait for %d commands to complete:%d\n",
  2133. adapter->outstanding_cmds,
  2134. (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
  2135. }
  2136. // bailout if no recovery happened in reset time
  2137. if (adapter->outstanding_cmds == 0) {
  2138. break;
  2139. }
  2140. msleep(1000);
  2141. }
  2142. spin_lock(&adapter->lock);
  2143. // If still outstanding commands, bail out
  2144. if (adapter->outstanding_cmds) {
  2145. con_log(CL_ANN, (KERN_WARNING
  2146. "megaraid mbox: critical hardware error!\n"));
  2147. raid_dev->hw_error = 1;
  2148. rval = FAILED;
  2149. goto out;
  2150. }
  2151. else {
  2152. con_log(CL_ANN, (KERN_NOTICE
  2153. "megaraid mbox: reset sequence completed successfully\n"));
  2154. }
  2155. // If the controller supports clustering, reset reservations
  2156. if (!adapter->ha) {
  2157. rval = SUCCESS;
  2158. goto out;
  2159. }
  2160. // clear reservations if any
  2161. raw_mbox[0] = CLUSTER_CMD;
  2162. raw_mbox[2] = RESET_RESERVATIONS;
  2163. rval = SUCCESS;
  2164. if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
  2165. con_log(CL_ANN,
  2166. (KERN_INFO "megaraid: reservation reset\n"));
  2167. }
  2168. else {
  2169. rval = FAILED;
  2170. con_log(CL_ANN, (KERN_WARNING
  2171. "megaraid: reservation reset failed\n"));
  2172. }
  2173. out:
  2174. spin_unlock(&adapter->lock);
  2175. return rval;
  2176. }
  2177. /*
  2178. * START: internal commands library
  2179. *
  2180. * This section of the driver has the common routine used by the driver and
  2181. * also has all the FW routines
  2182. */
  2183. /**
  2184. * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
  2185. * @adapter : controller's soft state
  2186. * @raw_mbox : the mailbox
  2187. *
  2188. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2189. * controllers.
  2190. */
  2191. static int
  2192. mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
  2193. {
  2194. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2195. mbox64_t *mbox64;
  2196. mbox_t *mbox;
  2197. uint8_t status;
  2198. int i;
  2199. mbox64 = raid_dev->mbox64;
  2200. mbox = raid_dev->mbox;
  2201. /*
  2202. * Wait until mailbox is free
  2203. */
  2204. if (megaraid_busywait_mbox(raid_dev) != 0)
  2205. goto blocked_mailbox;
  2206. /*
  2207. * Copy mailbox data into host structure
  2208. */
  2209. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
  2210. mbox->cmdid = 0xFE;
  2211. mbox->busy = 1;
  2212. mbox->poll = 0;
  2213. mbox->ack = 0;
  2214. mbox->numstatus = 0xFF;
  2215. mbox->status = 0xFF;
  2216. wmb();
  2217. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2218. // wait for maximum 1 second for status to post. If the status is not
  2219. // available within 1 second, assume FW is initializing and wait
  2220. // for an extended amount of time
  2221. if (mbox->numstatus == 0xFF) { // status not yet available
  2222. udelay(25);
  2223. for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
  2224. rmb();
  2225. msleep(1);
  2226. }
  2227. if (i == 1000) {
  2228. con_log(CL_ANN, (KERN_NOTICE
  2229. "megaraid mailbox: wait for FW to boot "));
  2230. for (i = 0; (mbox->numstatus == 0xFF) &&
  2231. (i < MBOX_RESET_WAIT); i++) {
  2232. rmb();
  2233. con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
  2234. MBOX_RESET_WAIT - i));
  2235. msleep(1000);
  2236. }
  2237. if (i == MBOX_RESET_WAIT) {
  2238. con_log(CL_ANN, (
  2239. "\nmegaraid mailbox: status not available\n"));
  2240. return -1;
  2241. }
  2242. con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
  2243. }
  2244. }
  2245. // wait for maximum 1 second for poll semaphore
  2246. if (mbox->poll != 0x77) {
  2247. udelay(25);
  2248. for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
  2249. rmb();
  2250. msleep(1);
  2251. }
  2252. if (i == 1000) {
  2253. con_log(CL_ANN, (KERN_WARNING
  2254. "megaraid mailbox: could not get poll semaphore\n"));
  2255. return -1;
  2256. }
  2257. }
  2258. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2259. wmb();
  2260. // wait for maximum 1 second for acknowledgement
  2261. if (RDINDOOR(raid_dev) & 0x2) {
  2262. udelay(25);
  2263. for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
  2264. rmb();
  2265. msleep(1);
  2266. }
  2267. if (i == 1000) {
  2268. con_log(CL_ANN, (KERN_WARNING
  2269. "megaraid mailbox: could not acknowledge\n"));
  2270. return -1;
  2271. }
  2272. }
  2273. mbox->poll = 0;
  2274. mbox->ack = 0x77;
  2275. status = mbox->status;
  2276. // invalidate the completed command id array. After command
  2277. // completion, firmware would write the valid id.
  2278. mbox->numstatus = 0xFF;
  2279. mbox->status = 0xFF;
  2280. for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
  2281. mbox->completed[i] = 0xFF;
  2282. }
  2283. return status;
  2284. blocked_mailbox:
  2285. con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
  2286. return -1;
  2287. }
  2288. /**
  2289. * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
  2290. * @adapter : controller's soft state
  2291. * @raw_mbox : the mailbox
  2292. *
  2293. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2294. * controllers. This is a faster version of the synchronous command and
  2295. * therefore can be called in interrupt-context as well.
  2296. */
  2297. static int
  2298. mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
  2299. {
  2300. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2301. mbox_t *mbox;
  2302. long i;
  2303. mbox = raid_dev->mbox;
  2304. // return immediately if the mailbox is busy
  2305. if (mbox->busy) return -1;
  2306. // Copy mailbox data into host structure
  2307. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
  2308. mbox->cmdid = 0xFE;
  2309. mbox->busy = 1;
  2310. mbox->poll = 0;
  2311. mbox->ack = 0;
  2312. mbox->numstatus = 0xFF;
  2313. mbox->status = 0xFF;
  2314. wmb();
  2315. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2316. for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
  2317. if (mbox->numstatus != 0xFF) break;
  2318. rmb();
  2319. udelay(MBOX_SYNC_DELAY_200);
  2320. }
  2321. if (i == MBOX_SYNC_WAIT_CNT) {
  2322. // We may need to re-calibrate the counter
  2323. con_log(CL_ANN, (KERN_CRIT
  2324. "megaraid: fast sync command timed out\n"));
  2325. }
  2326. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2327. wmb();
  2328. return mbox->status;
  2329. }
  2330. /**
  2331. * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
  2332. * @raid_dev : RAID device (HBA) soft state
  2333. *
  2334. * Wait until the controller's mailbox is available to accept more commands.
  2335. * Wait for at most 1 second.
  2336. */
  2337. static int
  2338. megaraid_busywait_mbox(mraid_device_t *raid_dev)
  2339. {
  2340. mbox_t *mbox = raid_dev->mbox;
  2341. int i = 0;
  2342. if (mbox->busy) {
  2343. udelay(25);
  2344. for (i = 0; mbox->busy && i < 1000; i++)
  2345. msleep(1);
  2346. }
  2347. if (i < 1000) return 0;
  2348. else return -1;
  2349. }
  2350. /**
  2351. * megaraid_mbox_product_info - some static information about the controller
  2352. * @adapter : our soft state
  2353. *
  2354. * Issue commands to the controller to grab some parameters required by our
  2355. * caller.
  2356. */
  2357. static int
  2358. megaraid_mbox_product_info(adapter_t *adapter)
  2359. {
  2360. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2361. mbox_t *mbox;
  2362. uint8_t raw_mbox[sizeof(mbox_t)];
  2363. mraid_pinfo_t *pinfo;
  2364. dma_addr_t pinfo_dma_h;
  2365. mraid_inquiry3_t *mraid_inq3;
  2366. int i;
  2367. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2368. mbox = (mbox_t *)raw_mbox;
  2369. /*
  2370. * Issue an ENQUIRY3 command to find out certain adapter parameters,
  2371. * e.g., max channels, max commands etc.
  2372. */
  2373. pinfo = pci_zalloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2374. &pinfo_dma_h);
  2375. if (pinfo == NULL) {
  2376. con_log(CL_ANN, (KERN_WARNING
  2377. "megaraid: out of memory, %s %d\n", __func__,
  2378. __LINE__));
  2379. return -1;
  2380. }
  2381. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2382. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2383. raw_mbox[0] = FC_NEW_CONFIG;
  2384. raw_mbox[2] = NC_SUBOP_ENQUIRY3;
  2385. raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
  2386. // Issue the command
  2387. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2388. con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
  2389. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2390. pinfo, pinfo_dma_h);
  2391. return -1;
  2392. }
  2393. /*
  2394. * Collect information about state of each physical drive
  2395. * attached to the controller. We will expose all the disks
  2396. * which are not part of RAID
  2397. */
  2398. mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
  2399. for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
  2400. raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
  2401. }
  2402. /*
  2403. * Get product info for information like number of channels,
  2404. * maximum commands supported.
  2405. */
  2406. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2407. mbox->xferaddr = (uint32_t)pinfo_dma_h;
  2408. raw_mbox[0] = FC_NEW_CONFIG;
  2409. raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
  2410. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2411. con_log(CL_ANN, (KERN_WARNING
  2412. "megaraid: product info failed\n"));
  2413. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2414. pinfo, pinfo_dma_h);
  2415. return -1;
  2416. }
  2417. /*
  2418. * Setup some parameters for host, as required by our caller
  2419. */
  2420. adapter->max_channel = pinfo->nchannels;
  2421. /*
  2422. * we will export all the logical drives on a single channel.
  2423. * Add 1 since inquires do not come for inititor ID
  2424. */
  2425. adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
  2426. adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
  2427. /*
  2428. * These are the maximum outstanding commands for the scsi-layer
  2429. */
  2430. adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
  2431. memset(adapter->fw_version, 0, VERSION_SIZE);
  2432. memset(adapter->bios_version, 0, VERSION_SIZE);
  2433. memcpy(adapter->fw_version, pinfo->fw_version, 4);
  2434. adapter->fw_version[4] = 0;
  2435. memcpy(adapter->bios_version, pinfo->bios_version, 4);
  2436. adapter->bios_version[4] = 0;
  2437. con_log(CL_ANN, (KERN_NOTICE
  2438. "megaraid: fw version:[%s] bios version:[%s]\n",
  2439. adapter->fw_version, adapter->bios_version));
  2440. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
  2441. pinfo_dma_h);
  2442. return 0;
  2443. }
  2444. /**
  2445. * megaraid_mbox_extended_cdb - check for support for extended CDBs
  2446. * @adapter : soft state for the controller
  2447. *
  2448. * This routine check whether the controller in question supports extended
  2449. * ( > 10 bytes ) CDBs.
  2450. */
  2451. static int
  2452. megaraid_mbox_extended_cdb(adapter_t *adapter)
  2453. {
  2454. mbox_t *mbox;
  2455. uint8_t raw_mbox[sizeof(mbox_t)];
  2456. int rval;
  2457. mbox = (mbox_t *)raw_mbox;
  2458. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2459. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2460. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2461. raw_mbox[0] = MAIN_MISC_OPCODE;
  2462. raw_mbox[2] = SUPPORT_EXT_CDB;
  2463. /*
  2464. * Issue the command
  2465. */
  2466. rval = 0;
  2467. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2468. rval = -1;
  2469. }
  2470. return rval;
  2471. }
  2472. /**
  2473. * megaraid_mbox_support_ha - Do we support clustering
  2474. * @adapter : soft state for the controller
  2475. * @init_id : ID of the initiator
  2476. *
  2477. * Determine if the firmware supports clustering and the ID of the initiator.
  2478. */
  2479. static int
  2480. megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
  2481. {
  2482. mbox_t *mbox;
  2483. uint8_t raw_mbox[sizeof(mbox_t)];
  2484. int rval;
  2485. mbox = (mbox_t *)raw_mbox;
  2486. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2487. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2488. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2489. raw_mbox[0] = GET_TARGET_ID;
  2490. // Issue the command
  2491. *init_id = 7;
  2492. rval = -1;
  2493. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2494. *init_id = *(uint8_t *)adapter->ibuf;
  2495. con_log(CL_ANN, (KERN_INFO
  2496. "megaraid: cluster firmware, initiator ID: %d\n",
  2497. *init_id));
  2498. rval = 0;
  2499. }
  2500. return rval;
  2501. }
  2502. /**
  2503. * megaraid_mbox_support_random_del - Do we support random deletion
  2504. * @adapter : soft state for the controller
  2505. *
  2506. * Determine if the firmware supports random deletion.
  2507. * Return: 1 is operation supported, 0 otherwise
  2508. */
  2509. static int
  2510. megaraid_mbox_support_random_del(adapter_t *adapter)
  2511. {
  2512. mbox_t *mbox;
  2513. uint8_t raw_mbox[sizeof(mbox_t)];
  2514. int rval;
  2515. /*
  2516. * Newer firmware on Dell CERC expect a different
  2517. * random deletion handling, so disable it.
  2518. */
  2519. if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI &&
  2520. adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 &&
  2521. adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
  2522. adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH &&
  2523. (adapter->fw_version[0] > '6' ||
  2524. (adapter->fw_version[0] == '6' &&
  2525. adapter->fw_version[2] > '6') ||
  2526. (adapter->fw_version[0] == '6'
  2527. && adapter->fw_version[2] == '6'
  2528. && adapter->fw_version[3] > '1'))) {
  2529. con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n"));
  2530. return 0;
  2531. }
  2532. mbox = (mbox_t *)raw_mbox;
  2533. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2534. raw_mbox[0] = FC_DEL_LOGDRV;
  2535. raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  2536. // Issue the command
  2537. rval = 0;
  2538. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2539. con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
  2540. rval = 1;
  2541. }
  2542. return rval;
  2543. }
  2544. /**
  2545. * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
  2546. * @adapter : soft state for the controller
  2547. *
  2548. * Find out the maximum number of scatter-gather elements supported by the
  2549. * firmware.
  2550. */
  2551. static int
  2552. megaraid_mbox_get_max_sg(adapter_t *adapter)
  2553. {
  2554. mbox_t *mbox;
  2555. uint8_t raw_mbox[sizeof(mbox_t)];
  2556. int nsg;
  2557. mbox = (mbox_t *)raw_mbox;
  2558. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2559. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2560. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2561. raw_mbox[0] = MAIN_MISC_OPCODE;
  2562. raw_mbox[2] = GET_MAX_SG_SUPPORT;
  2563. // Issue the command
  2564. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2565. nsg = *(uint8_t *)adapter->ibuf;
  2566. }
  2567. else {
  2568. nsg = MBOX_DEFAULT_SG_SIZE;
  2569. }
  2570. if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
  2571. return nsg;
  2572. }
  2573. /**
  2574. * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
  2575. * @adapter : soft state for the controller
  2576. *
  2577. * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
  2578. * can be exported as regular SCSI channels.
  2579. */
  2580. static void
  2581. megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
  2582. {
  2583. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2584. mbox_t *mbox;
  2585. uint8_t raw_mbox[sizeof(mbox_t)];
  2586. mbox = (mbox_t *)raw_mbox;
  2587. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2588. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2589. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2590. raw_mbox[0] = CHNL_CLASS;
  2591. raw_mbox[2] = GET_CHNL_CLASS;
  2592. // Issue the command. If the command fails, all channels are RAID
  2593. // channels
  2594. raid_dev->channel_class = 0xFF;
  2595. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2596. raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
  2597. }
  2598. return;
  2599. }
  2600. /**
  2601. * megaraid_mbox_flush_cache - flush adapter and disks cache
  2602. * @adapter : soft state for the controller
  2603. *
  2604. * Flush adapter cache followed by disks cache.
  2605. */
  2606. static void
  2607. megaraid_mbox_flush_cache(adapter_t *adapter)
  2608. {
  2609. mbox_t *mbox;
  2610. uint8_t raw_mbox[sizeof(mbox_t)];
  2611. mbox = (mbox_t *)raw_mbox;
  2612. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2613. raw_mbox[0] = FLUSH_ADAPTER;
  2614. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2615. con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
  2616. }
  2617. raw_mbox[0] = FLUSH_SYSTEM;
  2618. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2619. con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
  2620. }
  2621. return;
  2622. }
  2623. /**
  2624. * megaraid_mbox_fire_sync_cmd - fire the sync cmd
  2625. * @adapter : soft state for the controller
  2626. *
  2627. * Clears the pending cmds in FW and reinits its RAID structs.
  2628. */
  2629. static int
  2630. megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
  2631. {
  2632. mbox_t *mbox;
  2633. uint8_t raw_mbox[sizeof(mbox_t)];
  2634. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2635. mbox64_t *mbox64;
  2636. int status = 0;
  2637. int i;
  2638. uint32_t dword;
  2639. mbox = (mbox_t *)raw_mbox;
  2640. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2641. raw_mbox[0] = 0xFF;
  2642. mbox64 = raid_dev->mbox64;
  2643. mbox = raid_dev->mbox;
  2644. /* Wait until mailbox is free */
  2645. if (megaraid_busywait_mbox(raid_dev) != 0) {
  2646. status = 1;
  2647. goto blocked_mailbox;
  2648. }
  2649. /* Copy mailbox data into host structure */
  2650. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
  2651. mbox->cmdid = 0xFE;
  2652. mbox->busy = 1;
  2653. mbox->poll = 0;
  2654. mbox->ack = 0;
  2655. mbox->numstatus = 0;
  2656. mbox->status = 0;
  2657. wmb();
  2658. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2659. /* Wait for maximum 1 min for status to post.
  2660. * If the Firmware SUPPORTS the ABOVE COMMAND,
  2661. * mbox->cmd will be set to 0
  2662. * else
  2663. * the firmware will reject the command with
  2664. * mbox->numstatus set to 1
  2665. */
  2666. i = 0;
  2667. status = 0;
  2668. while (!mbox->numstatus && mbox->cmd == 0xFF) {
  2669. rmb();
  2670. msleep(1);
  2671. i++;
  2672. if (i > 1000 * 60) {
  2673. status = 1;
  2674. break;
  2675. }
  2676. }
  2677. if (mbox->numstatus == 1)
  2678. status = 1; /*cmd not supported*/
  2679. /* Check for interrupt line */
  2680. dword = RDOUTDOOR(raid_dev);
  2681. WROUTDOOR(raid_dev, dword);
  2682. WRINDOOR(raid_dev,2);
  2683. return status;
  2684. blocked_mailbox:
  2685. con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
  2686. return status;
  2687. }
  2688. /**
  2689. * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
  2690. * @adapter : controller's soft state
  2691. * @scb : SCB to be displayed
  2692. * @level : debug level for console print
  2693. *
  2694. * Diplay information about the given SCB iff the current debug level is
  2695. * verbose.
  2696. */
  2697. static void
  2698. megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
  2699. {
  2700. mbox_ccb_t *ccb;
  2701. struct scsi_cmnd *scp;
  2702. mbox_t *mbox;
  2703. int level;
  2704. int i;
  2705. ccb = (mbox_ccb_t *)scb->ccb;
  2706. scp = scb->scp;
  2707. mbox = ccb->mbox;
  2708. level = CL_DLEVEL3;
  2709. con_log(level, (KERN_NOTICE
  2710. "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
  2711. mbox->cmd, scb->sno));
  2712. con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
  2713. mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
  2714. mbox->numsge));
  2715. if (!scp) return;
  2716. con_log(level, (KERN_NOTICE "scsi cmnd: "));
  2717. for (i = 0; i < scp->cmd_len; i++) {
  2718. con_log(level, ("%#2.02x ", scp->cmnd[i]));
  2719. }
  2720. con_log(level, ("\n"));
  2721. return;
  2722. }
  2723. /**
  2724. * megaraid_mbox_setup_device_map - manage device ids
  2725. * @adapter : Driver's soft state
  2726. *
  2727. * Manage the device ids to have an appropriate mapping between the kernel
  2728. * scsi addresses and megaraid scsi and logical drive addresses. We export
  2729. * scsi devices on their actual addresses, whereas the logical drives are
  2730. * exported on a virtual scsi channel.
  2731. */
  2732. static void
  2733. megaraid_mbox_setup_device_map(adapter_t *adapter)
  2734. {
  2735. uint8_t c;
  2736. uint8_t t;
  2737. /*
  2738. * First fill the values on the logical drive channel
  2739. */
  2740. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2741. adapter->device_ids[adapter->max_channel][t] =
  2742. (t < adapter->init_id) ? t : t - 1;
  2743. adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
  2744. /*
  2745. * Fill the values on the physical devices channels
  2746. */
  2747. for (c = 0; c < adapter->max_channel; c++)
  2748. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2749. adapter->device_ids[c][t] = (c << 8) | t;
  2750. }
  2751. /*
  2752. * END: internal commands library
  2753. */
  2754. /*
  2755. * START: Interface for the common management module
  2756. *
  2757. * This is the module, which interfaces with the common management module to
  2758. * provide support for ioctl and sysfs
  2759. */
  2760. /**
  2761. * megaraid_cmm_register - register with the management module
  2762. * @adapter : HBA soft state
  2763. *
  2764. * Register with the management module, which allows applications to issue
  2765. * ioctl calls to the drivers. This interface is used by the management module
  2766. * to setup sysfs support as well.
  2767. */
  2768. static int
  2769. megaraid_cmm_register(adapter_t *adapter)
  2770. {
  2771. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2772. mraid_mmadp_t adp;
  2773. scb_t *scb;
  2774. mbox_ccb_t *ccb;
  2775. int rval;
  2776. int i;
  2777. // Allocate memory for the base list of scb for management module.
  2778. adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
  2779. if (adapter->uscb_list == NULL) {
  2780. con_log(CL_ANN, (KERN_WARNING
  2781. "megaraid: out of memory, %s %d\n", __func__,
  2782. __LINE__));
  2783. return -1;
  2784. }
  2785. // Initialize the synchronization parameters for resources for
  2786. // commands for management module
  2787. INIT_LIST_HEAD(&adapter->uscb_pool);
  2788. spin_lock_init(USER_FREE_LIST_LOCK(adapter));
  2789. // link all the packets. Note, CCB for commands, coming from the
  2790. // commom management module, mailbox physical address are already
  2791. // setup by it. We just need placeholder for that in our local command
  2792. // control blocks
  2793. for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
  2794. scb = adapter->uscb_list + i;
  2795. ccb = raid_dev->uccb_list + i;
  2796. scb->ccb = (caddr_t)ccb;
  2797. ccb->mbox64 = raid_dev->umbox64 + i;
  2798. ccb->mbox = &ccb->mbox64->mbox32;
  2799. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  2800. scb->gp = 0;
  2801. // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
  2802. // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
  2803. scb->sno = i + MBOX_MAX_SCSI_CMDS;
  2804. scb->scp = NULL;
  2805. scb->state = SCB_FREE;
  2806. scb->dma_direction = PCI_DMA_NONE;
  2807. scb->dma_type = MRAID_DMA_NONE;
  2808. scb->dev_channel = -1;
  2809. scb->dev_target = -1;
  2810. // put scb in the free pool
  2811. list_add_tail(&scb->list, &adapter->uscb_pool);
  2812. }
  2813. adp.unique_id = adapter->unique_id;
  2814. adp.drvr_type = DRVRTYPE_MBOX;
  2815. adp.drvr_data = (unsigned long)adapter;
  2816. adp.pdev = adapter->pdev;
  2817. adp.issue_uioc = megaraid_mbox_mm_handler;
  2818. adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
  2819. adp.max_kioc = MBOX_MAX_USER_CMDS;
  2820. if ((rval = mraid_mm_register_adp(&adp)) != 0) {
  2821. con_log(CL_ANN, (KERN_WARNING
  2822. "megaraid mbox: did not register with CMM\n"));
  2823. kfree(adapter->uscb_list);
  2824. }
  2825. return rval;
  2826. }
  2827. /**
  2828. * megaraid_cmm_unregister - un-register with the management module
  2829. * @adapter : HBA soft state
  2830. *
  2831. * Un-register with the management module.
  2832. * FIXME: mgmt module must return failure for unregister if it has pending
  2833. * commands in LLD.
  2834. */
  2835. static int
  2836. megaraid_cmm_unregister(adapter_t *adapter)
  2837. {
  2838. kfree(adapter->uscb_list);
  2839. mraid_mm_unregister_adp(adapter->unique_id);
  2840. return 0;
  2841. }
  2842. /**
  2843. * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
  2844. * @drvr_data : LLD specific data
  2845. * @kioc : CMM interface packet
  2846. * @action : command action
  2847. *
  2848. * This routine is invoked whenever the Common Management Module (CMM) has a
  2849. * command for us. The 'action' parameter specifies if this is a new command
  2850. * or otherwise.
  2851. */
  2852. static int
  2853. megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
  2854. {
  2855. adapter_t *adapter;
  2856. if (action != IOCTL_ISSUE) {
  2857. con_log(CL_ANN, (KERN_WARNING
  2858. "megaraid: unsupported management action:%#2x\n",
  2859. action));
  2860. return (-ENOTSUPP);
  2861. }
  2862. adapter = (adapter_t *)drvr_data;
  2863. // make sure this adapter is not being detached right now.
  2864. if (atomic_read(&adapter->being_detached)) {
  2865. con_log(CL_ANN, (KERN_WARNING
  2866. "megaraid: reject management request, detaching\n"));
  2867. return (-ENODEV);
  2868. }
  2869. switch (kioc->opcode) {
  2870. case GET_ADAP_INFO:
  2871. kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
  2872. (unsigned long)kioc->buf_vaddr);
  2873. kioc->done(kioc);
  2874. return kioc->status;
  2875. case MBOX_CMD:
  2876. return megaraid_mbox_mm_command(adapter, kioc);
  2877. default:
  2878. kioc->status = (-EINVAL);
  2879. kioc->done(kioc);
  2880. return (-EINVAL);
  2881. }
  2882. return 0; // not reached
  2883. }
  2884. /**
  2885. * megaraid_mbox_mm_command - issues commands routed through CMM
  2886. * @adapter : HBA soft state
  2887. * @kioc : management command packet
  2888. *
  2889. * Issues commands, which are routed through the management module.
  2890. */
  2891. static int
  2892. megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
  2893. {
  2894. struct list_head *head = &adapter->uscb_pool;
  2895. mbox64_t *mbox64;
  2896. uint8_t *raw_mbox;
  2897. scb_t *scb;
  2898. mbox_ccb_t *ccb;
  2899. unsigned long flags;
  2900. // detach one scb from free pool
  2901. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  2902. if (list_empty(head)) { // should never happen because of CMM
  2903. con_log(CL_ANN, (KERN_WARNING
  2904. "megaraid mbox: bug in cmm handler, lost resources\n"));
  2905. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2906. return (-EINVAL);
  2907. }
  2908. scb = list_entry(head->next, scb_t, list);
  2909. list_del_init(&scb->list);
  2910. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2911. scb->state = SCB_ACTIVE;
  2912. scb->dma_type = MRAID_DMA_NONE;
  2913. scb->dma_direction = PCI_DMA_NONE;
  2914. ccb = (mbox_ccb_t *)scb->ccb;
  2915. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  2916. raw_mbox = (uint8_t *)&mbox64->mbox32;
  2917. memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
  2918. scb->gp = (unsigned long)kioc;
  2919. /*
  2920. * If it is a logdrv random delete operation, we have to wait till
  2921. * there are no outstanding cmds at the fw and then issue it directly
  2922. */
  2923. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  2924. if (wait_till_fw_empty(adapter)) {
  2925. con_log(CL_ANN, (KERN_NOTICE
  2926. "megaraid mbox: LD delete, timed out\n"));
  2927. kioc->status = -ETIME;
  2928. scb->status = -1;
  2929. megaraid_mbox_mm_done(adapter, scb);
  2930. return (-ETIME);
  2931. }
  2932. INIT_LIST_HEAD(&scb->list);
  2933. scb->state = SCB_ISSUED;
  2934. if (mbox_post_cmd(adapter, scb) != 0) {
  2935. con_log(CL_ANN, (KERN_NOTICE
  2936. "megaraid mbox: LD delete, mailbox busy\n"));
  2937. kioc->status = -EBUSY;
  2938. scb->status = -1;
  2939. megaraid_mbox_mm_done(adapter, scb);
  2940. return (-EBUSY);
  2941. }
  2942. return 0;
  2943. }
  2944. // put the command on the pending list and execute
  2945. megaraid_mbox_runpendq(adapter, scb);
  2946. return 0;
  2947. }
  2948. static int
  2949. wait_till_fw_empty(adapter_t *adapter)
  2950. {
  2951. unsigned long flags = 0;
  2952. int i;
  2953. /*
  2954. * Set the quiescent flag to stop issuing cmds to FW.
  2955. */
  2956. spin_lock_irqsave(&adapter->lock, flags);
  2957. adapter->quiescent++;
  2958. spin_unlock_irqrestore(&adapter->lock, flags);
  2959. /*
  2960. * Wait till there are no more cmds outstanding at FW. Try for at most
  2961. * 60 seconds
  2962. */
  2963. for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
  2964. con_log(CL_DLEVEL1, (KERN_INFO
  2965. "megaraid: FW has %d pending commands\n",
  2966. adapter->outstanding_cmds));
  2967. msleep(1000);
  2968. }
  2969. return adapter->outstanding_cmds;
  2970. }
  2971. /**
  2972. * megaraid_mbox_mm_done - callback for CMM commands
  2973. * @adapter : HBA soft state
  2974. * @scb : completed command
  2975. *
  2976. * Callback routine for internal commands originated from the management
  2977. * module.
  2978. */
  2979. static void
  2980. megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
  2981. {
  2982. uioc_t *kioc;
  2983. mbox64_t *mbox64;
  2984. uint8_t *raw_mbox;
  2985. unsigned long flags;
  2986. kioc = (uioc_t *)scb->gp;
  2987. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  2988. mbox64->mbox32.status = scb->status;
  2989. raw_mbox = (uint8_t *)&mbox64->mbox32;
  2990. // put scb in the free pool
  2991. scb->state = SCB_FREE;
  2992. scb->scp = NULL;
  2993. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  2994. list_add(&scb->list, &adapter->uscb_pool);
  2995. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2996. // if a delete logical drive operation succeeded, restart the
  2997. // controller
  2998. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  2999. adapter->quiescent--;
  3000. megaraid_mbox_runpendq(adapter, NULL);
  3001. }
  3002. kioc->done(kioc);
  3003. return;
  3004. }
  3005. /**
  3006. * gather_hbainfo - HBA characteristics for the applications
  3007. * @adapter : HBA soft state
  3008. * @hinfo : pointer to the caller's host info strucuture
  3009. */
  3010. static int
  3011. gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
  3012. {
  3013. uint8_t dmajor;
  3014. dmajor = megaraid_mbox_version[0];
  3015. hinfo->pci_vendor_id = adapter->pdev->vendor;
  3016. hinfo->pci_device_id = adapter->pdev->device;
  3017. hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
  3018. hinfo->subsys_device_id = adapter->pdev->subsystem_device;
  3019. hinfo->pci_bus = adapter->pdev->bus->number;
  3020. hinfo->pci_dev_fn = adapter->pdev->devfn;
  3021. hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
  3022. hinfo->irq = adapter->host->irq;
  3023. hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
  3024. hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
  3025. hinfo->host_no = adapter->host->host_no;
  3026. return 0;
  3027. }
  3028. /*
  3029. * END: Interface for the common management module
  3030. */
  3031. /**
  3032. * megaraid_sysfs_alloc_resources - allocate sysfs related resources
  3033. * @adapter : controller's soft state
  3034. *
  3035. * Allocate packets required to issue FW calls whenever the sysfs attributes
  3036. * are read. These attributes would require up-to-date information from the
  3037. * FW. Also set up resources for mutual exclusion to share these resources and
  3038. * the wait queue.
  3039. *
  3040. * Return 0 on success.
  3041. * Return -ERROR_CODE on failure.
  3042. */
  3043. static int
  3044. megaraid_sysfs_alloc_resources(adapter_t *adapter)
  3045. {
  3046. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3047. int rval = 0;
  3048. raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
  3049. raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
  3050. raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
  3051. PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
  3052. if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
  3053. !raid_dev->sysfs_buffer) {
  3054. con_log(CL_ANN, (KERN_WARNING
  3055. "megaraid: out of memory, %s %d\n", __func__,
  3056. __LINE__));
  3057. rval = -ENOMEM;
  3058. megaraid_sysfs_free_resources(adapter);
  3059. }
  3060. mutex_init(&raid_dev->sysfs_mtx);
  3061. init_waitqueue_head(&raid_dev->sysfs_wait_q);
  3062. return rval;
  3063. }
  3064. /**
  3065. * megaraid_sysfs_free_resources - free sysfs related resources
  3066. * @adapter : controller's soft state
  3067. *
  3068. * Free packets allocated for sysfs FW commands
  3069. */
  3070. static void
  3071. megaraid_sysfs_free_resources(adapter_t *adapter)
  3072. {
  3073. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3074. kfree(raid_dev->sysfs_uioc);
  3075. kfree(raid_dev->sysfs_mbox64);
  3076. if (raid_dev->sysfs_buffer) {
  3077. pci_free_consistent(adapter->pdev, PAGE_SIZE,
  3078. raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
  3079. }
  3080. }
  3081. /**
  3082. * megaraid_sysfs_get_ldmap_done - callback for get ldmap
  3083. * @uioc : completed packet
  3084. *
  3085. * Callback routine called in the ISR/tasklet context for get ldmap call
  3086. */
  3087. static void
  3088. megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
  3089. {
  3090. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3091. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3092. uioc->status = 0;
  3093. wake_up(&raid_dev->sysfs_wait_q);
  3094. }
  3095. /**
  3096. * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
  3097. * @data : timed out packet
  3098. *
  3099. * Timeout routine to recover and return to application, in case the adapter
  3100. * has stopped responding. A timeout of 60 seconds for this command seems like
  3101. * a good value.
  3102. */
  3103. static void
  3104. megaraid_sysfs_get_ldmap_timeout(unsigned long data)
  3105. {
  3106. uioc_t *uioc = (uioc_t *)data;
  3107. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3108. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3109. uioc->status = -ETIME;
  3110. wake_up(&raid_dev->sysfs_wait_q);
  3111. }
  3112. /**
  3113. * megaraid_sysfs_get_ldmap - get update logical drive map
  3114. * @adapter : controller's soft state
  3115. *
  3116. * This routine will be called whenever user reads the logical drive
  3117. * attributes, go get the current logical drive mapping table from the
  3118. * firmware. We use the management API's to issue commands to the controller.
  3119. *
  3120. * NOTE: The commands issuance functionality is not generalized and
  3121. * implemented in context of "get ld map" command only. If required, the
  3122. * command issuance logical can be trivially pulled out and implemented as a
  3123. * standalone library. For now, this should suffice since there is no other
  3124. * user of this interface.
  3125. *
  3126. * Return 0 on success.
  3127. * Return -1 on failure.
  3128. */
  3129. static int
  3130. megaraid_sysfs_get_ldmap(adapter_t *adapter)
  3131. {
  3132. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3133. uioc_t *uioc;
  3134. mbox64_t *mbox64;
  3135. mbox_t *mbox;
  3136. char *raw_mbox;
  3137. struct timer_list sysfs_timer;
  3138. struct timer_list *timerp;
  3139. caddr_t ldmap;
  3140. int rval = 0;
  3141. /*
  3142. * Allow only one read at a time to go through the sysfs attributes
  3143. */
  3144. mutex_lock(&raid_dev->sysfs_mtx);
  3145. uioc = raid_dev->sysfs_uioc;
  3146. mbox64 = raid_dev->sysfs_mbox64;
  3147. ldmap = raid_dev->sysfs_buffer;
  3148. memset(uioc, 0, sizeof(uioc_t));
  3149. memset(mbox64, 0, sizeof(mbox64_t));
  3150. memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
  3151. mbox = &mbox64->mbox32;
  3152. raw_mbox = (char *)mbox;
  3153. uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
  3154. uioc->buf_vaddr = (caddr_t)adapter;
  3155. uioc->status = -ENODATA;
  3156. uioc->done = megaraid_sysfs_get_ldmap_done;
  3157. /*
  3158. * Prepare the mailbox packet to get the current logical drive mapping
  3159. * table
  3160. */
  3161. mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
  3162. raw_mbox[0] = FC_DEL_LOGDRV;
  3163. raw_mbox[2] = OP_GET_LDID_MAP;
  3164. /*
  3165. * Setup a timer to recover from a non-responding controller
  3166. */
  3167. timerp = &sysfs_timer;
  3168. init_timer(timerp);
  3169. timerp->function = megaraid_sysfs_get_ldmap_timeout;
  3170. timerp->data = (unsigned long)uioc;
  3171. timerp->expires = jiffies + 60 * HZ;
  3172. add_timer(timerp);
  3173. /*
  3174. * Send the command to the firmware
  3175. */
  3176. rval = megaraid_mbox_mm_command(adapter, uioc);
  3177. if (rval == 0) { // command successfully issued
  3178. wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
  3179. /*
  3180. * Check if the command timed out
  3181. */
  3182. if (uioc->status == -ETIME) {
  3183. con_log(CL_ANN, (KERN_NOTICE
  3184. "megaraid: sysfs get ld map timed out\n"));
  3185. rval = -ETIME;
  3186. }
  3187. else {
  3188. rval = mbox->status;
  3189. }
  3190. if (rval == 0) {
  3191. memcpy(raid_dev->curr_ldmap, ldmap,
  3192. sizeof(raid_dev->curr_ldmap));
  3193. }
  3194. else {
  3195. con_log(CL_ANN, (KERN_NOTICE
  3196. "megaraid: get ld map failed with %x\n", rval));
  3197. }
  3198. }
  3199. else {
  3200. con_log(CL_ANN, (KERN_NOTICE
  3201. "megaraid: could not issue ldmap command:%x\n", rval));
  3202. }
  3203. del_timer_sync(timerp);
  3204. mutex_unlock(&raid_dev->sysfs_mtx);
  3205. return rval;
  3206. }
  3207. /**
  3208. * megaraid_sysfs_show_app_hndl - display application handle for this adapter
  3209. * @cdev : class device object representation for the host
  3210. * @buf : buffer to send data to
  3211. *
  3212. * Display the handle used by the applications while executing management
  3213. * tasks on the adapter. We invoke a management module API to get the adapter
  3214. * handle, since we do not interface with applications directly.
  3215. */
  3216. static ssize_t
  3217. megaraid_sysfs_show_app_hndl(struct device *dev, struct device_attribute *attr,
  3218. char *buf)
  3219. {
  3220. struct Scsi_Host *shost = class_to_shost(dev);
  3221. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
  3222. uint32_t app_hndl;
  3223. app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
  3224. return snprintf(buf, 8, "%u\n", app_hndl);
  3225. }
  3226. /**
  3227. * megaraid_sysfs_show_ldnum - display the logical drive number for this device
  3228. * @dev : device object representation for the scsi device
  3229. * @attr : device attribute to show
  3230. * @buf : buffer to send data to
  3231. *
  3232. * Display the logical drive number for the device in question, if it a valid
  3233. * logical drive. For physical devices, "-1" is returned.
  3234. *
  3235. * The logical drive number is displayed in following format:
  3236. *
  3237. * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
  3238. *
  3239. * <int> <int> <int> <int>
  3240. */
  3241. static ssize_t
  3242. megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
  3243. {
  3244. struct scsi_device *sdev = to_scsi_device(dev);
  3245. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
  3246. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3247. int scsi_id = -1;
  3248. int logical_drv = -1;
  3249. int ldid_map = -1;
  3250. uint32_t app_hndl = 0;
  3251. int mapped_sdev_id;
  3252. int rval;
  3253. int i;
  3254. if (raid_dev->random_del_supported &&
  3255. MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
  3256. rval = megaraid_sysfs_get_ldmap(adapter);
  3257. if (rval == 0) {
  3258. for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
  3259. mapped_sdev_id = sdev->id;
  3260. if (sdev->id > adapter->init_id) {
  3261. mapped_sdev_id -= 1;
  3262. }
  3263. if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
  3264. scsi_id = sdev->id;
  3265. logical_drv = i;
  3266. ldid_map = raid_dev->curr_ldmap[i];
  3267. app_hndl = mraid_mm_adapter_app_handle(
  3268. adapter->unique_id);
  3269. break;
  3270. }
  3271. }
  3272. }
  3273. else {
  3274. con_log(CL_ANN, (KERN_NOTICE
  3275. "megaraid: sysfs get ld map failed: %x\n",
  3276. rval));
  3277. }
  3278. }
  3279. return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
  3280. ldid_map, app_hndl);
  3281. }
  3282. /*
  3283. * END: Mailbox Low Level Driver
  3284. */
  3285. module_init(megaraid_init);
  3286. module_exit(megaraid_exit);
  3287. /* vim: set ts=8 sw=8 tw=78 ai si: */