megaraid_mbox.c 100 KB

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