megaraid_mbox.c 100 KB

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