megaraid_mbox.c 99 KB

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