hpsa.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116
  1. /*
  2. * Disk Array driver for HP Smart Array SAS controllers
  3. * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  12. * NON INFRINGEMENT. See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * Questions/Comments/Bugfixes to iss_storagedev@hp.com
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/types.h>
  24. #include <linux/pci.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/delay.h>
  28. #include <linux/fs.h>
  29. #include <linux/timer.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/init.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/compat.h>
  34. #include <linux/blktrace_api.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/io.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/completion.h>
  39. #include <linux/moduleparam.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_cmnd.h>
  42. #include <scsi/scsi_device.h>
  43. #include <scsi/scsi_host.h>
  44. #include <scsi/scsi_tcq.h>
  45. #include <linux/cciss_ioctl.h>
  46. #include <linux/string.h>
  47. #include <linux/bitmap.h>
  48. #include <asm/atomic.h>
  49. #include <linux/kthread.h>
  50. #include "hpsa_cmd.h"
  51. #include "hpsa.h"
  52. /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
  53. #define HPSA_DRIVER_VERSION "2.0.2-1"
  54. #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
  55. /* How long to wait (in milliseconds) for board to go into simple mode */
  56. #define MAX_CONFIG_WAIT 30000
  57. #define MAX_IOCTL_CONFIG_WAIT 1000
  58. /*define how many times we will try a command because of bus resets */
  59. #define MAX_CMD_RETRIES 3
  60. /* Embedded module documentation macros - see modules.h */
  61. MODULE_AUTHOR("Hewlett-Packard Company");
  62. MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
  63. HPSA_DRIVER_VERSION);
  64. MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
  65. MODULE_VERSION(HPSA_DRIVER_VERSION);
  66. MODULE_LICENSE("GPL");
  67. static int hpsa_allow_any;
  68. module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
  69. MODULE_PARM_DESC(hpsa_allow_any,
  70. "Allow hpsa driver to access unknown HP Smart Array hardware");
  71. /* define the PCI info for the cards we can control */
  72. static const struct pci_device_id hpsa_pci_device_id[] = {
  73. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
  74. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
  75. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
  76. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
  77. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
  78. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324a},
  79. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324b},
  80. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
  81. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3250},
  82. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3251},
  83. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3252},
  84. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3253},
  85. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3254},
  86. {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  87. PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
  88. {0,}
  89. };
  90. MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
  91. /* board_id = Subsystem Device ID & Vendor ID
  92. * product = Marketing Name for the board
  93. * access = Address of the struct of function pointers
  94. */
  95. static struct board_type products[] = {
  96. {0x3241103C, "Smart Array P212", &SA5_access},
  97. {0x3243103C, "Smart Array P410", &SA5_access},
  98. {0x3245103C, "Smart Array P410i", &SA5_access},
  99. {0x3247103C, "Smart Array P411", &SA5_access},
  100. {0x3249103C, "Smart Array P812", &SA5_access},
  101. {0x324a103C, "Smart Array P712m", &SA5_access},
  102. {0x324b103C, "Smart Array P711m", &SA5_access},
  103. {0x3250103C, "Smart Array", &SA5_access},
  104. {0x3250113C, "Smart Array", &SA5_access},
  105. {0x3250123C, "Smart Array", &SA5_access},
  106. {0x3250133C, "Smart Array", &SA5_access},
  107. {0x3250143C, "Smart Array", &SA5_access},
  108. {0xFFFF103C, "Unknown Smart Array", &SA5_access},
  109. };
  110. static int number_of_controllers;
  111. static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
  112. static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
  113. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
  114. static void start_io(struct ctlr_info *h);
  115. #ifdef CONFIG_COMPAT
  116. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
  117. #endif
  118. static void cmd_free(struct ctlr_info *h, struct CommandList *c);
  119. static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
  120. static struct CommandList *cmd_alloc(struct ctlr_info *h);
  121. static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
  122. static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  123. void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
  124. int cmd_type);
  125. static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
  126. static void hpsa_scan_start(struct Scsi_Host *);
  127. static int hpsa_scan_finished(struct Scsi_Host *sh,
  128. unsigned long elapsed_time);
  129. static int hpsa_change_queue_depth(struct scsi_device *sdev,
  130. int qdepth, int reason);
  131. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
  132. static int hpsa_slave_alloc(struct scsi_device *sdev);
  133. static void hpsa_slave_destroy(struct scsi_device *sdev);
  134. static ssize_t raid_level_show(struct device *dev,
  135. struct device_attribute *attr, char *buf);
  136. static ssize_t lunid_show(struct device *dev,
  137. struct device_attribute *attr, char *buf);
  138. static ssize_t unique_id_show(struct device *dev,
  139. struct device_attribute *attr, char *buf);
  140. static ssize_t host_show_firmware_revision(struct device *dev,
  141. struct device_attribute *attr, char *buf);
  142. static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
  143. static ssize_t host_store_rescan(struct device *dev,
  144. struct device_attribute *attr, const char *buf, size_t count);
  145. static int check_for_unit_attention(struct ctlr_info *h,
  146. struct CommandList *c);
  147. static void check_ioctl_unit_attention(struct ctlr_info *h,
  148. struct CommandList *c);
  149. /* performant mode helper functions */
  150. static void calc_bucket_map(int *bucket, int num_buckets,
  151. int nsgs, int *bucket_map);
  152. static __devinit void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
  153. static inline u32 next_command(struct ctlr_info *h);
  154. static int __devinit hpsa_find_cfg_addrs(struct pci_dev *pdev,
  155. void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index,
  156. u64 *cfg_offset);
  157. static int __devinit hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
  158. unsigned long *memory_bar);
  159. static int __devinit hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
  160. static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
  161. static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
  162. static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
  163. static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
  164. static DEVICE_ATTR(firmware_revision, S_IRUGO,
  165. host_show_firmware_revision, NULL);
  166. static struct device_attribute *hpsa_sdev_attrs[] = {
  167. &dev_attr_raid_level,
  168. &dev_attr_lunid,
  169. &dev_attr_unique_id,
  170. NULL,
  171. };
  172. static struct device_attribute *hpsa_shost_attrs[] = {
  173. &dev_attr_rescan,
  174. &dev_attr_firmware_revision,
  175. NULL,
  176. };
  177. static struct scsi_host_template hpsa_driver_template = {
  178. .module = THIS_MODULE,
  179. .name = "hpsa",
  180. .proc_name = "hpsa",
  181. .queuecommand = hpsa_scsi_queue_command,
  182. .scan_start = hpsa_scan_start,
  183. .scan_finished = hpsa_scan_finished,
  184. .change_queue_depth = hpsa_change_queue_depth,
  185. .this_id = -1,
  186. .use_clustering = ENABLE_CLUSTERING,
  187. .eh_device_reset_handler = hpsa_eh_device_reset_handler,
  188. .ioctl = hpsa_ioctl,
  189. .slave_alloc = hpsa_slave_alloc,
  190. .slave_destroy = hpsa_slave_destroy,
  191. #ifdef CONFIG_COMPAT
  192. .compat_ioctl = hpsa_compat_ioctl,
  193. #endif
  194. .sdev_attrs = hpsa_sdev_attrs,
  195. .shost_attrs = hpsa_shost_attrs,
  196. };
  197. static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
  198. {
  199. unsigned long *priv = shost_priv(sdev->host);
  200. return (struct ctlr_info *) *priv;
  201. }
  202. static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
  203. {
  204. unsigned long *priv = shost_priv(sh);
  205. return (struct ctlr_info *) *priv;
  206. }
  207. static int check_for_unit_attention(struct ctlr_info *h,
  208. struct CommandList *c)
  209. {
  210. if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
  211. return 0;
  212. switch (c->err_info->SenseInfo[12]) {
  213. case STATE_CHANGED:
  214. dev_warn(&h->pdev->dev, "hpsa%d: a state change "
  215. "detected, command retried\n", h->ctlr);
  216. break;
  217. case LUN_FAILED:
  218. dev_warn(&h->pdev->dev, "hpsa%d: LUN failure "
  219. "detected, action required\n", h->ctlr);
  220. break;
  221. case REPORT_LUNS_CHANGED:
  222. dev_warn(&h->pdev->dev, "hpsa%d: report LUN data "
  223. "changed, action required\n", h->ctlr);
  224. /*
  225. * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012.
  226. */
  227. break;
  228. case POWER_OR_RESET:
  229. dev_warn(&h->pdev->dev, "hpsa%d: a power on "
  230. "or device reset detected\n", h->ctlr);
  231. break;
  232. case UNIT_ATTENTION_CLEARED:
  233. dev_warn(&h->pdev->dev, "hpsa%d: unit attention "
  234. "cleared by another initiator\n", h->ctlr);
  235. break;
  236. default:
  237. dev_warn(&h->pdev->dev, "hpsa%d: unknown "
  238. "unit attention detected\n", h->ctlr);
  239. break;
  240. }
  241. return 1;
  242. }
  243. static ssize_t host_store_rescan(struct device *dev,
  244. struct device_attribute *attr,
  245. const char *buf, size_t count)
  246. {
  247. struct ctlr_info *h;
  248. struct Scsi_Host *shost = class_to_shost(dev);
  249. h = shost_to_hba(shost);
  250. hpsa_scan_start(h->scsi_host);
  251. return count;
  252. }
  253. static ssize_t host_show_firmware_revision(struct device *dev,
  254. struct device_attribute *attr, char *buf)
  255. {
  256. struct ctlr_info *h;
  257. struct Scsi_Host *shost = class_to_shost(dev);
  258. unsigned char *fwrev;
  259. h = shost_to_hba(shost);
  260. if (!h->hba_inquiry_data)
  261. return 0;
  262. fwrev = &h->hba_inquiry_data[32];
  263. return snprintf(buf, 20, "%c%c%c%c\n",
  264. fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
  265. }
  266. /* Enqueuing and dequeuing functions for cmdlists. */
  267. static inline void addQ(struct hlist_head *list, struct CommandList *c)
  268. {
  269. hlist_add_head(&c->list, list);
  270. }
  271. static inline u32 next_command(struct ctlr_info *h)
  272. {
  273. u32 a;
  274. if (unlikely(h->transMethod != CFGTBL_Trans_Performant))
  275. return h->access.command_completed(h);
  276. if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) {
  277. a = *(h->reply_pool_head); /* Next cmd in ring buffer */
  278. (h->reply_pool_head)++;
  279. h->commands_outstanding--;
  280. } else {
  281. a = FIFO_EMPTY;
  282. }
  283. /* Check for wraparound */
  284. if (h->reply_pool_head == (h->reply_pool + h->max_commands)) {
  285. h->reply_pool_head = h->reply_pool;
  286. h->reply_pool_wraparound ^= 1;
  287. }
  288. return a;
  289. }
  290. /* set_performant_mode: Modify the tag for cciss performant
  291. * set bit 0 for pull model, bits 3-1 for block fetch
  292. * register number
  293. */
  294. static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
  295. {
  296. if (likely(h->transMethod == CFGTBL_Trans_Performant))
  297. c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
  298. }
  299. static void enqueue_cmd_and_start_io(struct ctlr_info *h,
  300. struct CommandList *c)
  301. {
  302. unsigned long flags;
  303. set_performant_mode(h, c);
  304. spin_lock_irqsave(&h->lock, flags);
  305. addQ(&h->reqQ, c);
  306. h->Qdepth++;
  307. start_io(h);
  308. spin_unlock_irqrestore(&h->lock, flags);
  309. }
  310. static inline void removeQ(struct CommandList *c)
  311. {
  312. if (WARN_ON(hlist_unhashed(&c->list)))
  313. return;
  314. hlist_del_init(&c->list);
  315. }
  316. static inline int is_hba_lunid(unsigned char scsi3addr[])
  317. {
  318. return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
  319. }
  320. static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
  321. {
  322. return (scsi3addr[3] & 0xC0) == 0x40;
  323. }
  324. static inline int is_scsi_rev_5(struct ctlr_info *h)
  325. {
  326. if (!h->hba_inquiry_data)
  327. return 0;
  328. if ((h->hba_inquiry_data[2] & 0x07) == 5)
  329. return 1;
  330. return 0;
  331. }
  332. static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
  333. "UNKNOWN"
  334. };
  335. #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
  336. static ssize_t raid_level_show(struct device *dev,
  337. struct device_attribute *attr, char *buf)
  338. {
  339. ssize_t l = 0;
  340. unsigned char rlevel;
  341. struct ctlr_info *h;
  342. struct scsi_device *sdev;
  343. struct hpsa_scsi_dev_t *hdev;
  344. unsigned long flags;
  345. sdev = to_scsi_device(dev);
  346. h = sdev_to_hba(sdev);
  347. spin_lock_irqsave(&h->lock, flags);
  348. hdev = sdev->hostdata;
  349. if (!hdev) {
  350. spin_unlock_irqrestore(&h->lock, flags);
  351. return -ENODEV;
  352. }
  353. /* Is this even a logical drive? */
  354. if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
  355. spin_unlock_irqrestore(&h->lock, flags);
  356. l = snprintf(buf, PAGE_SIZE, "N/A\n");
  357. return l;
  358. }
  359. rlevel = hdev->raid_level;
  360. spin_unlock_irqrestore(&h->lock, flags);
  361. if (rlevel > RAID_UNKNOWN)
  362. rlevel = RAID_UNKNOWN;
  363. l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
  364. return l;
  365. }
  366. static ssize_t lunid_show(struct device *dev,
  367. struct device_attribute *attr, char *buf)
  368. {
  369. struct ctlr_info *h;
  370. struct scsi_device *sdev;
  371. struct hpsa_scsi_dev_t *hdev;
  372. unsigned long flags;
  373. unsigned char lunid[8];
  374. sdev = to_scsi_device(dev);
  375. h = sdev_to_hba(sdev);
  376. spin_lock_irqsave(&h->lock, flags);
  377. hdev = sdev->hostdata;
  378. if (!hdev) {
  379. spin_unlock_irqrestore(&h->lock, flags);
  380. return -ENODEV;
  381. }
  382. memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
  383. spin_unlock_irqrestore(&h->lock, flags);
  384. return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  385. lunid[0], lunid[1], lunid[2], lunid[3],
  386. lunid[4], lunid[5], lunid[6], lunid[7]);
  387. }
  388. static ssize_t unique_id_show(struct device *dev,
  389. struct device_attribute *attr, char *buf)
  390. {
  391. struct ctlr_info *h;
  392. struct scsi_device *sdev;
  393. struct hpsa_scsi_dev_t *hdev;
  394. unsigned long flags;
  395. unsigned char sn[16];
  396. sdev = to_scsi_device(dev);
  397. h = sdev_to_hba(sdev);
  398. spin_lock_irqsave(&h->lock, flags);
  399. hdev = sdev->hostdata;
  400. if (!hdev) {
  401. spin_unlock_irqrestore(&h->lock, flags);
  402. return -ENODEV;
  403. }
  404. memcpy(sn, hdev->device_id, sizeof(sn));
  405. spin_unlock_irqrestore(&h->lock, flags);
  406. return snprintf(buf, 16 * 2 + 2,
  407. "%02X%02X%02X%02X%02X%02X%02X%02X"
  408. "%02X%02X%02X%02X%02X%02X%02X%02X\n",
  409. sn[0], sn[1], sn[2], sn[3],
  410. sn[4], sn[5], sn[6], sn[7],
  411. sn[8], sn[9], sn[10], sn[11],
  412. sn[12], sn[13], sn[14], sn[15]);
  413. }
  414. static int hpsa_find_target_lun(struct ctlr_info *h,
  415. unsigned char scsi3addr[], int bus, int *target, int *lun)
  416. {
  417. /* finds an unused bus, target, lun for a new physical device
  418. * assumes h->devlock is held
  419. */
  420. int i, found = 0;
  421. DECLARE_BITMAP(lun_taken, HPSA_MAX_SCSI_DEVS_PER_HBA);
  422. memset(&lun_taken[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA >> 3);
  423. for (i = 0; i < h->ndevices; i++) {
  424. if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
  425. set_bit(h->dev[i]->target, lun_taken);
  426. }
  427. for (i = 0; i < HPSA_MAX_SCSI_DEVS_PER_HBA; i++) {
  428. if (!test_bit(i, lun_taken)) {
  429. /* *bus = 1; */
  430. *target = i;
  431. *lun = 0;
  432. found = 1;
  433. break;
  434. }
  435. }
  436. return !found;
  437. }
  438. /* Add an entry into h->dev[] array. */
  439. static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
  440. struct hpsa_scsi_dev_t *device,
  441. struct hpsa_scsi_dev_t *added[], int *nadded)
  442. {
  443. /* assumes h->devlock is held */
  444. int n = h->ndevices;
  445. int i;
  446. unsigned char addr1[8], addr2[8];
  447. struct hpsa_scsi_dev_t *sd;
  448. if (n >= HPSA_MAX_SCSI_DEVS_PER_HBA) {
  449. dev_err(&h->pdev->dev, "too many devices, some will be "
  450. "inaccessible.\n");
  451. return -1;
  452. }
  453. /* physical devices do not have lun or target assigned until now. */
  454. if (device->lun != -1)
  455. /* Logical device, lun is already assigned. */
  456. goto lun_assigned;
  457. /* If this device a non-zero lun of a multi-lun device
  458. * byte 4 of the 8-byte LUN addr will contain the logical
  459. * unit no, zero otherise.
  460. */
  461. if (device->scsi3addr[4] == 0) {
  462. /* This is not a non-zero lun of a multi-lun device */
  463. if (hpsa_find_target_lun(h, device->scsi3addr,
  464. device->bus, &device->target, &device->lun) != 0)
  465. return -1;
  466. goto lun_assigned;
  467. }
  468. /* This is a non-zero lun of a multi-lun device.
  469. * Search through our list and find the device which
  470. * has the same 8 byte LUN address, excepting byte 4.
  471. * Assign the same bus and target for this new LUN.
  472. * Use the logical unit number from the firmware.
  473. */
  474. memcpy(addr1, device->scsi3addr, 8);
  475. addr1[4] = 0;
  476. for (i = 0; i < n; i++) {
  477. sd = h->dev[i];
  478. memcpy(addr2, sd->scsi3addr, 8);
  479. addr2[4] = 0;
  480. /* differ only in byte 4? */
  481. if (memcmp(addr1, addr2, 8) == 0) {
  482. device->bus = sd->bus;
  483. device->target = sd->target;
  484. device->lun = device->scsi3addr[4];
  485. break;
  486. }
  487. }
  488. if (device->lun == -1) {
  489. dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
  490. " suspect firmware bug or unsupported hardware "
  491. "configuration.\n");
  492. return -1;
  493. }
  494. lun_assigned:
  495. h->dev[n] = device;
  496. h->ndevices++;
  497. added[*nadded] = device;
  498. (*nadded)++;
  499. /* initially, (before registering with scsi layer) we don't
  500. * know our hostno and we don't want to print anything first
  501. * time anyway (the scsi layer's inquiries will show that info)
  502. */
  503. /* if (hostno != -1) */
  504. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
  505. scsi_device_type(device->devtype), hostno,
  506. device->bus, device->target, device->lun);
  507. return 0;
  508. }
  509. /* Replace an entry from h->dev[] array. */
  510. static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
  511. int entry, struct hpsa_scsi_dev_t *new_entry,
  512. struct hpsa_scsi_dev_t *added[], int *nadded,
  513. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  514. {
  515. /* assumes h->devlock is held */
  516. BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
  517. removed[*nremoved] = h->dev[entry];
  518. (*nremoved)++;
  519. h->dev[entry] = new_entry;
  520. added[*nadded] = new_entry;
  521. (*nadded)++;
  522. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
  523. scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
  524. new_entry->target, new_entry->lun);
  525. }
  526. /* Remove an entry from h->dev[] array. */
  527. static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
  528. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  529. {
  530. /* assumes h->devlock is held */
  531. int i;
  532. struct hpsa_scsi_dev_t *sd;
  533. BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
  534. sd = h->dev[entry];
  535. removed[*nremoved] = h->dev[entry];
  536. (*nremoved)++;
  537. for (i = entry; i < h->ndevices-1; i++)
  538. h->dev[i] = h->dev[i+1];
  539. h->ndevices--;
  540. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
  541. scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
  542. sd->lun);
  543. }
  544. #define SCSI3ADDR_EQ(a, b) ( \
  545. (a)[7] == (b)[7] && \
  546. (a)[6] == (b)[6] && \
  547. (a)[5] == (b)[5] && \
  548. (a)[4] == (b)[4] && \
  549. (a)[3] == (b)[3] && \
  550. (a)[2] == (b)[2] && \
  551. (a)[1] == (b)[1] && \
  552. (a)[0] == (b)[0])
  553. static void fixup_botched_add(struct ctlr_info *h,
  554. struct hpsa_scsi_dev_t *added)
  555. {
  556. /* called when scsi_add_device fails in order to re-adjust
  557. * h->dev[] to match the mid layer's view.
  558. */
  559. unsigned long flags;
  560. int i, j;
  561. spin_lock_irqsave(&h->lock, flags);
  562. for (i = 0; i < h->ndevices; i++) {
  563. if (h->dev[i] == added) {
  564. for (j = i; j < h->ndevices-1; j++)
  565. h->dev[j] = h->dev[j+1];
  566. h->ndevices--;
  567. break;
  568. }
  569. }
  570. spin_unlock_irqrestore(&h->lock, flags);
  571. kfree(added);
  572. }
  573. static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
  574. struct hpsa_scsi_dev_t *dev2)
  575. {
  576. if ((is_logical_dev_addr_mode(dev1->scsi3addr) ||
  577. (dev1->lun != -1 && dev2->lun != -1)) &&
  578. dev1->devtype != 0x0C)
  579. return (memcmp(dev1, dev2, sizeof(*dev1)) == 0);
  580. /* we compare everything except lun and target as these
  581. * are not yet assigned. Compare parts likely
  582. * to differ first
  583. */
  584. if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
  585. sizeof(dev1->scsi3addr)) != 0)
  586. return 0;
  587. if (memcmp(dev1->device_id, dev2->device_id,
  588. sizeof(dev1->device_id)) != 0)
  589. return 0;
  590. if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
  591. return 0;
  592. if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
  593. return 0;
  594. if (memcmp(dev1->revision, dev2->revision, sizeof(dev1->revision)) != 0)
  595. return 0;
  596. if (dev1->devtype != dev2->devtype)
  597. return 0;
  598. if (dev1->raid_level != dev2->raid_level)
  599. return 0;
  600. if (dev1->bus != dev2->bus)
  601. return 0;
  602. return 1;
  603. }
  604. /* Find needle in haystack. If exact match found, return DEVICE_SAME,
  605. * and return needle location in *index. If scsi3addr matches, but not
  606. * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
  607. * location in *index. If needle not found, return DEVICE_NOT_FOUND.
  608. */
  609. static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
  610. struct hpsa_scsi_dev_t *haystack[], int haystack_size,
  611. int *index)
  612. {
  613. int i;
  614. #define DEVICE_NOT_FOUND 0
  615. #define DEVICE_CHANGED 1
  616. #define DEVICE_SAME 2
  617. for (i = 0; i < haystack_size; i++) {
  618. if (haystack[i] == NULL) /* previously removed. */
  619. continue;
  620. if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
  621. *index = i;
  622. if (device_is_the_same(needle, haystack[i]))
  623. return DEVICE_SAME;
  624. else
  625. return DEVICE_CHANGED;
  626. }
  627. }
  628. *index = -1;
  629. return DEVICE_NOT_FOUND;
  630. }
  631. static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
  632. struct hpsa_scsi_dev_t *sd[], int nsds)
  633. {
  634. /* sd contains scsi3 addresses and devtypes, and inquiry
  635. * data. This function takes what's in sd to be the current
  636. * reality and updates h->dev[] to reflect that reality.
  637. */
  638. int i, entry, device_change, changes = 0;
  639. struct hpsa_scsi_dev_t *csd;
  640. unsigned long flags;
  641. struct hpsa_scsi_dev_t **added, **removed;
  642. int nadded, nremoved;
  643. struct Scsi_Host *sh = NULL;
  644. added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  645. GFP_KERNEL);
  646. removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  647. GFP_KERNEL);
  648. if (!added || !removed) {
  649. dev_warn(&h->pdev->dev, "out of memory in "
  650. "adjust_hpsa_scsi_table\n");
  651. goto free_and_out;
  652. }
  653. spin_lock_irqsave(&h->devlock, flags);
  654. /* find any devices in h->dev[] that are not in
  655. * sd[] and remove them from h->dev[], and for any
  656. * devices which have changed, remove the old device
  657. * info and add the new device info.
  658. */
  659. i = 0;
  660. nremoved = 0;
  661. nadded = 0;
  662. while (i < h->ndevices) {
  663. csd = h->dev[i];
  664. device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
  665. if (device_change == DEVICE_NOT_FOUND) {
  666. changes++;
  667. hpsa_scsi_remove_entry(h, hostno, i,
  668. removed, &nremoved);
  669. continue; /* remove ^^^, hence i not incremented */
  670. } else if (device_change == DEVICE_CHANGED) {
  671. changes++;
  672. hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
  673. added, &nadded, removed, &nremoved);
  674. /* Set it to NULL to prevent it from being freed
  675. * at the bottom of hpsa_update_scsi_devices()
  676. */
  677. sd[entry] = NULL;
  678. }
  679. i++;
  680. }
  681. /* Now, make sure every device listed in sd[] is also
  682. * listed in h->dev[], adding them if they aren't found
  683. */
  684. for (i = 0; i < nsds; i++) {
  685. if (!sd[i]) /* if already added above. */
  686. continue;
  687. device_change = hpsa_scsi_find_entry(sd[i], h->dev,
  688. h->ndevices, &entry);
  689. if (device_change == DEVICE_NOT_FOUND) {
  690. changes++;
  691. if (hpsa_scsi_add_entry(h, hostno, sd[i],
  692. added, &nadded) != 0)
  693. break;
  694. sd[i] = NULL; /* prevent from being freed later. */
  695. } else if (device_change == DEVICE_CHANGED) {
  696. /* should never happen... */
  697. changes++;
  698. dev_warn(&h->pdev->dev,
  699. "device unexpectedly changed.\n");
  700. /* but if it does happen, we just ignore that device */
  701. }
  702. }
  703. spin_unlock_irqrestore(&h->devlock, flags);
  704. /* Don't notify scsi mid layer of any changes the first time through
  705. * (or if there are no changes) scsi_scan_host will do it later the
  706. * first time through.
  707. */
  708. if (hostno == -1 || !changes)
  709. goto free_and_out;
  710. sh = h->scsi_host;
  711. /* Notify scsi mid layer of any removed devices */
  712. for (i = 0; i < nremoved; i++) {
  713. struct scsi_device *sdev =
  714. scsi_device_lookup(sh, removed[i]->bus,
  715. removed[i]->target, removed[i]->lun);
  716. if (sdev != NULL) {
  717. scsi_remove_device(sdev);
  718. scsi_device_put(sdev);
  719. } else {
  720. /* We don't expect to get here.
  721. * future cmds to this device will get selection
  722. * timeout as if the device was gone.
  723. */
  724. dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
  725. " for removal.", hostno, removed[i]->bus,
  726. removed[i]->target, removed[i]->lun);
  727. }
  728. kfree(removed[i]);
  729. removed[i] = NULL;
  730. }
  731. /* Notify scsi mid layer of any added devices */
  732. for (i = 0; i < nadded; i++) {
  733. if (scsi_add_device(sh, added[i]->bus,
  734. added[i]->target, added[i]->lun) == 0)
  735. continue;
  736. dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
  737. "device not added.\n", hostno, added[i]->bus,
  738. added[i]->target, added[i]->lun);
  739. /* now we have to remove it from h->dev,
  740. * since it didn't get added to scsi mid layer
  741. */
  742. fixup_botched_add(h, added[i]);
  743. }
  744. free_and_out:
  745. kfree(added);
  746. kfree(removed);
  747. }
  748. /*
  749. * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
  750. * Assume's h->devlock is held.
  751. */
  752. static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
  753. int bus, int target, int lun)
  754. {
  755. int i;
  756. struct hpsa_scsi_dev_t *sd;
  757. for (i = 0; i < h->ndevices; i++) {
  758. sd = h->dev[i];
  759. if (sd->bus == bus && sd->target == target && sd->lun == lun)
  760. return sd;
  761. }
  762. return NULL;
  763. }
  764. /* link sdev->hostdata to our per-device structure. */
  765. static int hpsa_slave_alloc(struct scsi_device *sdev)
  766. {
  767. struct hpsa_scsi_dev_t *sd;
  768. unsigned long flags;
  769. struct ctlr_info *h;
  770. h = sdev_to_hba(sdev);
  771. spin_lock_irqsave(&h->devlock, flags);
  772. sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
  773. sdev_id(sdev), sdev->lun);
  774. if (sd != NULL)
  775. sdev->hostdata = sd;
  776. spin_unlock_irqrestore(&h->devlock, flags);
  777. return 0;
  778. }
  779. static void hpsa_slave_destroy(struct scsi_device *sdev)
  780. {
  781. /* nothing to do. */
  782. }
  783. static void hpsa_scsi_setup(struct ctlr_info *h)
  784. {
  785. h->ndevices = 0;
  786. h->scsi_host = NULL;
  787. spin_lock_init(&h->devlock);
  788. }
  789. static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
  790. {
  791. int i;
  792. if (!h->cmd_sg_list)
  793. return;
  794. for (i = 0; i < h->nr_cmds; i++) {
  795. kfree(h->cmd_sg_list[i]);
  796. h->cmd_sg_list[i] = NULL;
  797. }
  798. kfree(h->cmd_sg_list);
  799. h->cmd_sg_list = NULL;
  800. }
  801. static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
  802. {
  803. int i;
  804. if (h->chainsize <= 0)
  805. return 0;
  806. h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
  807. GFP_KERNEL);
  808. if (!h->cmd_sg_list)
  809. return -ENOMEM;
  810. for (i = 0; i < h->nr_cmds; i++) {
  811. h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
  812. h->chainsize, GFP_KERNEL);
  813. if (!h->cmd_sg_list[i])
  814. goto clean;
  815. }
  816. return 0;
  817. clean:
  818. hpsa_free_sg_chain_blocks(h);
  819. return -ENOMEM;
  820. }
  821. static void hpsa_map_sg_chain_block(struct ctlr_info *h,
  822. struct CommandList *c)
  823. {
  824. struct SGDescriptor *chain_sg, *chain_block;
  825. u64 temp64;
  826. chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
  827. chain_block = h->cmd_sg_list[c->cmdindex];
  828. chain_sg->Ext = HPSA_SG_CHAIN;
  829. chain_sg->Len = sizeof(*chain_sg) *
  830. (c->Header.SGTotal - h->max_cmd_sg_entries);
  831. temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
  832. PCI_DMA_TODEVICE);
  833. chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
  834. chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
  835. }
  836. static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
  837. struct CommandList *c)
  838. {
  839. struct SGDescriptor *chain_sg;
  840. union u64bit temp64;
  841. if (c->Header.SGTotal <= h->max_cmd_sg_entries)
  842. return;
  843. chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
  844. temp64.val32.lower = chain_sg->Addr.lower;
  845. temp64.val32.upper = chain_sg->Addr.upper;
  846. pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
  847. }
  848. static void complete_scsi_command(struct CommandList *cp,
  849. int timeout, u32 tag)
  850. {
  851. struct scsi_cmnd *cmd;
  852. struct ctlr_info *h;
  853. struct ErrorInfo *ei;
  854. unsigned char sense_key;
  855. unsigned char asc; /* additional sense code */
  856. unsigned char ascq; /* additional sense code qualifier */
  857. ei = cp->err_info;
  858. cmd = (struct scsi_cmnd *) cp->scsi_cmd;
  859. h = cp->h;
  860. scsi_dma_unmap(cmd); /* undo the DMA mappings */
  861. if (cp->Header.SGTotal > h->max_cmd_sg_entries)
  862. hpsa_unmap_sg_chain_block(h, cp);
  863. cmd->result = (DID_OK << 16); /* host byte */
  864. cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
  865. cmd->result |= ei->ScsiStatus;
  866. /* copy the sense data whether we need to or not. */
  867. memcpy(cmd->sense_buffer, ei->SenseInfo,
  868. ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
  869. SCSI_SENSE_BUFFERSIZE :
  870. ei->SenseLen);
  871. scsi_set_resid(cmd, ei->ResidualCnt);
  872. if (ei->CommandStatus == 0) {
  873. cmd->scsi_done(cmd);
  874. cmd_free(h, cp);
  875. return;
  876. }
  877. /* an error has occurred */
  878. switch (ei->CommandStatus) {
  879. case CMD_TARGET_STATUS:
  880. if (ei->ScsiStatus) {
  881. /* Get sense key */
  882. sense_key = 0xf & ei->SenseInfo[2];
  883. /* Get additional sense code */
  884. asc = ei->SenseInfo[12];
  885. /* Get addition sense code qualifier */
  886. ascq = ei->SenseInfo[13];
  887. }
  888. if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
  889. if (check_for_unit_attention(h, cp)) {
  890. cmd->result = DID_SOFT_ERROR << 16;
  891. break;
  892. }
  893. if (sense_key == ILLEGAL_REQUEST) {
  894. /*
  895. * SCSI REPORT_LUNS is commonly unsupported on
  896. * Smart Array. Suppress noisy complaint.
  897. */
  898. if (cp->Request.CDB[0] == REPORT_LUNS)
  899. break;
  900. /* If ASC/ASCQ indicate Logical Unit
  901. * Not Supported condition,
  902. */
  903. if ((asc == 0x25) && (ascq == 0x0)) {
  904. dev_warn(&h->pdev->dev, "cp %p "
  905. "has check condition\n", cp);
  906. break;
  907. }
  908. }
  909. if (sense_key == NOT_READY) {
  910. /* If Sense is Not Ready, Logical Unit
  911. * Not ready, Manual Intervention
  912. * required
  913. */
  914. if ((asc == 0x04) && (ascq == 0x03)) {
  915. dev_warn(&h->pdev->dev, "cp %p "
  916. "has check condition: unit "
  917. "not ready, manual "
  918. "intervention required\n", cp);
  919. break;
  920. }
  921. }
  922. if (sense_key == ABORTED_COMMAND) {
  923. /* Aborted command is retryable */
  924. dev_warn(&h->pdev->dev, "cp %p "
  925. "has check condition: aborted command: "
  926. "ASC: 0x%x, ASCQ: 0x%x\n",
  927. cp, asc, ascq);
  928. cmd->result = DID_SOFT_ERROR << 16;
  929. break;
  930. }
  931. /* Must be some other type of check condition */
  932. dev_warn(&h->pdev->dev, "cp %p has check condition: "
  933. "unknown type: "
  934. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  935. "Returning result: 0x%x, "
  936. "cmd=[%02x %02x %02x %02x %02x "
  937. "%02x %02x %02x %02x %02x %02x "
  938. "%02x %02x %02x %02x %02x]\n",
  939. cp, sense_key, asc, ascq,
  940. cmd->result,
  941. cmd->cmnd[0], cmd->cmnd[1],
  942. cmd->cmnd[2], cmd->cmnd[3],
  943. cmd->cmnd[4], cmd->cmnd[5],
  944. cmd->cmnd[6], cmd->cmnd[7],
  945. cmd->cmnd[8], cmd->cmnd[9],
  946. cmd->cmnd[10], cmd->cmnd[11],
  947. cmd->cmnd[12], cmd->cmnd[13],
  948. cmd->cmnd[14], cmd->cmnd[15]);
  949. break;
  950. }
  951. /* Problem was not a check condition
  952. * Pass it up to the upper layers...
  953. */
  954. if (ei->ScsiStatus) {
  955. dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
  956. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  957. "Returning result: 0x%x\n",
  958. cp, ei->ScsiStatus,
  959. sense_key, asc, ascq,
  960. cmd->result);
  961. } else { /* scsi status is zero??? How??? */
  962. dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
  963. "Returning no connection.\n", cp),
  964. /* Ordinarily, this case should never happen,
  965. * but there is a bug in some released firmware
  966. * revisions that allows it to happen if, for
  967. * example, a 4100 backplane loses power and
  968. * the tape drive is in it. We assume that
  969. * it's a fatal error of some kind because we
  970. * can't show that it wasn't. We will make it
  971. * look like selection timeout since that is
  972. * the most common reason for this to occur,
  973. * and it's severe enough.
  974. */
  975. cmd->result = DID_NO_CONNECT << 16;
  976. }
  977. break;
  978. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  979. break;
  980. case CMD_DATA_OVERRUN:
  981. dev_warn(&h->pdev->dev, "cp %p has"
  982. " completed with data overrun "
  983. "reported\n", cp);
  984. break;
  985. case CMD_INVALID: {
  986. /* print_bytes(cp, sizeof(*cp), 1, 0);
  987. print_cmd(cp); */
  988. /* We get CMD_INVALID if you address a non-existent device
  989. * instead of a selection timeout (no response). You will
  990. * see this if you yank out a drive, then try to access it.
  991. * This is kind of a shame because it means that any other
  992. * CMD_INVALID (e.g. driver bug) will get interpreted as a
  993. * missing target. */
  994. cmd->result = DID_NO_CONNECT << 16;
  995. }
  996. break;
  997. case CMD_PROTOCOL_ERR:
  998. dev_warn(&h->pdev->dev, "cp %p has "
  999. "protocol error \n", cp);
  1000. break;
  1001. case CMD_HARDWARE_ERR:
  1002. cmd->result = DID_ERROR << 16;
  1003. dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
  1004. break;
  1005. case CMD_CONNECTION_LOST:
  1006. cmd->result = DID_ERROR << 16;
  1007. dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
  1008. break;
  1009. case CMD_ABORTED:
  1010. cmd->result = DID_ABORT << 16;
  1011. dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
  1012. cp, ei->ScsiStatus);
  1013. break;
  1014. case CMD_ABORT_FAILED:
  1015. cmd->result = DID_ERROR << 16;
  1016. dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
  1017. break;
  1018. case CMD_UNSOLICITED_ABORT:
  1019. cmd->result = DID_RESET << 16;
  1020. dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited "
  1021. "abort\n", cp);
  1022. break;
  1023. case CMD_TIMEOUT:
  1024. cmd->result = DID_TIME_OUT << 16;
  1025. dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
  1026. break;
  1027. default:
  1028. cmd->result = DID_ERROR << 16;
  1029. dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
  1030. cp, ei->CommandStatus);
  1031. }
  1032. cmd->scsi_done(cmd);
  1033. cmd_free(h, cp);
  1034. }
  1035. static int hpsa_scsi_detect(struct ctlr_info *h)
  1036. {
  1037. struct Scsi_Host *sh;
  1038. int error;
  1039. sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
  1040. if (sh == NULL)
  1041. goto fail;
  1042. sh->io_port = 0;
  1043. sh->n_io_port = 0;
  1044. sh->this_id = -1;
  1045. sh->max_channel = 3;
  1046. sh->max_cmd_len = MAX_COMMAND_SIZE;
  1047. sh->max_lun = HPSA_MAX_LUN;
  1048. sh->max_id = HPSA_MAX_LUN;
  1049. sh->can_queue = h->nr_cmds;
  1050. sh->cmd_per_lun = h->nr_cmds;
  1051. sh->sg_tablesize = h->maxsgentries;
  1052. h->scsi_host = sh;
  1053. sh->hostdata[0] = (unsigned long) h;
  1054. sh->irq = h->intr[PERF_MODE_INT];
  1055. sh->unique_id = sh->irq;
  1056. error = scsi_add_host(sh, &h->pdev->dev);
  1057. if (error)
  1058. goto fail_host_put;
  1059. scsi_scan_host(sh);
  1060. return 0;
  1061. fail_host_put:
  1062. dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host"
  1063. " failed for controller %d\n", h->ctlr);
  1064. scsi_host_put(sh);
  1065. return error;
  1066. fail:
  1067. dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc"
  1068. " failed for controller %d\n", h->ctlr);
  1069. return -ENOMEM;
  1070. }
  1071. static void hpsa_pci_unmap(struct pci_dev *pdev,
  1072. struct CommandList *c, int sg_used, int data_direction)
  1073. {
  1074. int i;
  1075. union u64bit addr64;
  1076. for (i = 0; i < sg_used; i++) {
  1077. addr64.val32.lower = c->SG[i].Addr.lower;
  1078. addr64.val32.upper = c->SG[i].Addr.upper;
  1079. pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
  1080. data_direction);
  1081. }
  1082. }
  1083. static void hpsa_map_one(struct pci_dev *pdev,
  1084. struct CommandList *cp,
  1085. unsigned char *buf,
  1086. size_t buflen,
  1087. int data_direction)
  1088. {
  1089. u64 addr64;
  1090. if (buflen == 0 || data_direction == PCI_DMA_NONE) {
  1091. cp->Header.SGList = 0;
  1092. cp->Header.SGTotal = 0;
  1093. return;
  1094. }
  1095. addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
  1096. cp->SG[0].Addr.lower =
  1097. (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
  1098. cp->SG[0].Addr.upper =
  1099. (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
  1100. cp->SG[0].Len = buflen;
  1101. cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
  1102. cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
  1103. }
  1104. static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
  1105. struct CommandList *c)
  1106. {
  1107. DECLARE_COMPLETION_ONSTACK(wait);
  1108. c->waiting = &wait;
  1109. enqueue_cmd_and_start_io(h, c);
  1110. wait_for_completion(&wait);
  1111. }
  1112. static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
  1113. struct CommandList *c, int data_direction)
  1114. {
  1115. int retry_count = 0;
  1116. do {
  1117. memset(c->err_info, 0, sizeof(c->err_info));
  1118. hpsa_scsi_do_simple_cmd_core(h, c);
  1119. retry_count++;
  1120. } while (check_for_unit_attention(h, c) && retry_count <= 3);
  1121. hpsa_pci_unmap(h->pdev, c, 1, data_direction);
  1122. }
  1123. static void hpsa_scsi_interpret_error(struct CommandList *cp)
  1124. {
  1125. struct ErrorInfo *ei;
  1126. struct device *d = &cp->h->pdev->dev;
  1127. ei = cp->err_info;
  1128. switch (ei->CommandStatus) {
  1129. case CMD_TARGET_STATUS:
  1130. dev_warn(d, "cmd %p has completed with errors\n", cp);
  1131. dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
  1132. ei->ScsiStatus);
  1133. if (ei->ScsiStatus == 0)
  1134. dev_warn(d, "SCSI status is abnormally zero. "
  1135. "(probably indicates selection timeout "
  1136. "reported incorrectly due to a known "
  1137. "firmware bug, circa July, 2001.)\n");
  1138. break;
  1139. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  1140. dev_info(d, "UNDERRUN\n");
  1141. break;
  1142. case CMD_DATA_OVERRUN:
  1143. dev_warn(d, "cp %p has completed with data overrun\n", cp);
  1144. break;
  1145. case CMD_INVALID: {
  1146. /* controller unfortunately reports SCSI passthru's
  1147. * to non-existent targets as invalid commands.
  1148. */
  1149. dev_warn(d, "cp %p is reported invalid (probably means "
  1150. "target device no longer present)\n", cp);
  1151. /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
  1152. print_cmd(cp); */
  1153. }
  1154. break;
  1155. case CMD_PROTOCOL_ERR:
  1156. dev_warn(d, "cp %p has protocol error \n", cp);
  1157. break;
  1158. case CMD_HARDWARE_ERR:
  1159. /* cmd->result = DID_ERROR << 16; */
  1160. dev_warn(d, "cp %p had hardware error\n", cp);
  1161. break;
  1162. case CMD_CONNECTION_LOST:
  1163. dev_warn(d, "cp %p had connection lost\n", cp);
  1164. break;
  1165. case CMD_ABORTED:
  1166. dev_warn(d, "cp %p was aborted\n", cp);
  1167. break;
  1168. case CMD_ABORT_FAILED:
  1169. dev_warn(d, "cp %p reports abort failed\n", cp);
  1170. break;
  1171. case CMD_UNSOLICITED_ABORT:
  1172. dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
  1173. break;
  1174. case CMD_TIMEOUT:
  1175. dev_warn(d, "cp %p timed out\n", cp);
  1176. break;
  1177. default:
  1178. dev_warn(d, "cp %p returned unknown status %x\n", cp,
  1179. ei->CommandStatus);
  1180. }
  1181. }
  1182. static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
  1183. unsigned char page, unsigned char *buf,
  1184. unsigned char bufsize)
  1185. {
  1186. int rc = IO_OK;
  1187. struct CommandList *c;
  1188. struct ErrorInfo *ei;
  1189. c = cmd_special_alloc(h);
  1190. if (c == NULL) { /* trouble... */
  1191. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1192. return -ENOMEM;
  1193. }
  1194. fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD);
  1195. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  1196. ei = c->err_info;
  1197. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  1198. hpsa_scsi_interpret_error(c);
  1199. rc = -1;
  1200. }
  1201. cmd_special_free(h, c);
  1202. return rc;
  1203. }
  1204. static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
  1205. {
  1206. int rc = IO_OK;
  1207. struct CommandList *c;
  1208. struct ErrorInfo *ei;
  1209. c = cmd_special_alloc(h);
  1210. if (c == NULL) { /* trouble... */
  1211. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1212. return -ENOMEM;
  1213. }
  1214. fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG);
  1215. hpsa_scsi_do_simple_cmd_core(h, c);
  1216. /* no unmap needed here because no data xfer. */
  1217. ei = c->err_info;
  1218. if (ei->CommandStatus != 0) {
  1219. hpsa_scsi_interpret_error(c);
  1220. rc = -1;
  1221. }
  1222. cmd_special_free(h, c);
  1223. return rc;
  1224. }
  1225. static void hpsa_get_raid_level(struct ctlr_info *h,
  1226. unsigned char *scsi3addr, unsigned char *raid_level)
  1227. {
  1228. int rc;
  1229. unsigned char *buf;
  1230. *raid_level = RAID_UNKNOWN;
  1231. buf = kzalloc(64, GFP_KERNEL);
  1232. if (!buf)
  1233. return;
  1234. rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
  1235. if (rc == 0)
  1236. *raid_level = buf[8];
  1237. if (*raid_level > RAID_UNKNOWN)
  1238. *raid_level = RAID_UNKNOWN;
  1239. kfree(buf);
  1240. return;
  1241. }
  1242. /* Get the device id from inquiry page 0x83 */
  1243. static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
  1244. unsigned char *device_id, int buflen)
  1245. {
  1246. int rc;
  1247. unsigned char *buf;
  1248. if (buflen > 16)
  1249. buflen = 16;
  1250. buf = kzalloc(64, GFP_KERNEL);
  1251. if (!buf)
  1252. return -1;
  1253. rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
  1254. if (rc == 0)
  1255. memcpy(device_id, &buf[8], buflen);
  1256. kfree(buf);
  1257. return rc != 0;
  1258. }
  1259. static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
  1260. struct ReportLUNdata *buf, int bufsize,
  1261. int extended_response)
  1262. {
  1263. int rc = IO_OK;
  1264. struct CommandList *c;
  1265. unsigned char scsi3addr[8];
  1266. struct ErrorInfo *ei;
  1267. c = cmd_special_alloc(h);
  1268. if (c == NULL) { /* trouble... */
  1269. dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1270. return -1;
  1271. }
  1272. /* address the controller */
  1273. memset(scsi3addr, 0, sizeof(scsi3addr));
  1274. fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
  1275. buf, bufsize, 0, scsi3addr, TYPE_CMD);
  1276. if (extended_response)
  1277. c->Request.CDB[1] = extended_response;
  1278. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  1279. ei = c->err_info;
  1280. if (ei->CommandStatus != 0 &&
  1281. ei->CommandStatus != CMD_DATA_UNDERRUN) {
  1282. hpsa_scsi_interpret_error(c);
  1283. rc = -1;
  1284. }
  1285. cmd_special_free(h, c);
  1286. return rc;
  1287. }
  1288. static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
  1289. struct ReportLUNdata *buf,
  1290. int bufsize, int extended_response)
  1291. {
  1292. return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
  1293. }
  1294. static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
  1295. struct ReportLUNdata *buf, int bufsize)
  1296. {
  1297. return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
  1298. }
  1299. static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
  1300. int bus, int target, int lun)
  1301. {
  1302. device->bus = bus;
  1303. device->target = target;
  1304. device->lun = lun;
  1305. }
  1306. static int hpsa_update_device_info(struct ctlr_info *h,
  1307. unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device)
  1308. {
  1309. #define OBDR_TAPE_INQ_SIZE 49
  1310. unsigned char *inq_buff;
  1311. inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
  1312. if (!inq_buff)
  1313. goto bail_out;
  1314. /* Do an inquiry to the device to see what it is. */
  1315. if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
  1316. (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
  1317. /* Inquiry failed (msg printed already) */
  1318. dev_err(&h->pdev->dev,
  1319. "hpsa_update_device_info: inquiry failed\n");
  1320. goto bail_out;
  1321. }
  1322. this_device->devtype = (inq_buff[0] & 0x1f);
  1323. memcpy(this_device->scsi3addr, scsi3addr, 8);
  1324. memcpy(this_device->vendor, &inq_buff[8],
  1325. sizeof(this_device->vendor));
  1326. memcpy(this_device->model, &inq_buff[16],
  1327. sizeof(this_device->model));
  1328. memcpy(this_device->revision, &inq_buff[32],
  1329. sizeof(this_device->revision));
  1330. memset(this_device->device_id, 0,
  1331. sizeof(this_device->device_id));
  1332. hpsa_get_device_id(h, scsi3addr, this_device->device_id,
  1333. sizeof(this_device->device_id));
  1334. if (this_device->devtype == TYPE_DISK &&
  1335. is_logical_dev_addr_mode(scsi3addr))
  1336. hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
  1337. else
  1338. this_device->raid_level = RAID_UNKNOWN;
  1339. kfree(inq_buff);
  1340. return 0;
  1341. bail_out:
  1342. kfree(inq_buff);
  1343. return 1;
  1344. }
  1345. static unsigned char *msa2xxx_model[] = {
  1346. "MSA2012",
  1347. "MSA2024",
  1348. "MSA2312",
  1349. "MSA2324",
  1350. NULL,
  1351. };
  1352. static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
  1353. {
  1354. int i;
  1355. for (i = 0; msa2xxx_model[i]; i++)
  1356. if (strncmp(device->model, msa2xxx_model[i],
  1357. strlen(msa2xxx_model[i])) == 0)
  1358. return 1;
  1359. return 0;
  1360. }
  1361. /* Helper function to assign bus, target, lun mapping of devices.
  1362. * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical
  1363. * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
  1364. * Logical drive target and lun are assigned at this time, but
  1365. * physical device lun and target assignment are deferred (assigned
  1366. * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
  1367. */
  1368. static void figure_bus_target_lun(struct ctlr_info *h,
  1369. u8 *lunaddrbytes, int *bus, int *target, int *lun,
  1370. struct hpsa_scsi_dev_t *device)
  1371. {
  1372. u32 lunid;
  1373. if (is_logical_dev_addr_mode(lunaddrbytes)) {
  1374. /* logical device */
  1375. if (unlikely(is_scsi_rev_5(h))) {
  1376. /* p1210m, logical drives lun assignments
  1377. * match SCSI REPORT LUNS data.
  1378. */
  1379. lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
  1380. *bus = 0;
  1381. *target = 0;
  1382. *lun = (lunid & 0x3fff) + 1;
  1383. } else {
  1384. /* not p1210m... */
  1385. lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
  1386. if (is_msa2xxx(h, device)) {
  1387. /* msa2xxx way, put logicals on bus 1
  1388. * and match target/lun numbers box
  1389. * reports.
  1390. */
  1391. *bus = 1;
  1392. *target = (lunid >> 16) & 0x3fff;
  1393. *lun = lunid & 0x00ff;
  1394. } else {
  1395. /* Traditional smart array way. */
  1396. *bus = 0;
  1397. *lun = 0;
  1398. *target = lunid & 0x3fff;
  1399. }
  1400. }
  1401. } else {
  1402. /* physical device */
  1403. if (is_hba_lunid(lunaddrbytes))
  1404. if (unlikely(is_scsi_rev_5(h))) {
  1405. *bus = 0; /* put p1210m ctlr at 0,0,0 */
  1406. *target = 0;
  1407. *lun = 0;
  1408. return;
  1409. } else
  1410. *bus = 3; /* traditional smartarray */
  1411. else
  1412. *bus = 2; /* physical disk */
  1413. *target = -1;
  1414. *lun = -1; /* we will fill these in later. */
  1415. }
  1416. }
  1417. /*
  1418. * If there is no lun 0 on a target, linux won't find any devices.
  1419. * For the MSA2xxx boxes, we have to manually detect the enclosure
  1420. * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
  1421. * it for some reason. *tmpdevice is the target we're adding,
  1422. * this_device is a pointer into the current element of currentsd[]
  1423. * that we're building up in update_scsi_devices(), below.
  1424. * lunzerobits is a bitmap that tracks which targets already have a
  1425. * lun 0 assigned.
  1426. * Returns 1 if an enclosure was added, 0 if not.
  1427. */
  1428. static int add_msa2xxx_enclosure_device(struct ctlr_info *h,
  1429. struct hpsa_scsi_dev_t *tmpdevice,
  1430. struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
  1431. int bus, int target, int lun, unsigned long lunzerobits[],
  1432. int *nmsa2xxx_enclosures)
  1433. {
  1434. unsigned char scsi3addr[8];
  1435. if (test_bit(target, lunzerobits))
  1436. return 0; /* There is already a lun 0 on this target. */
  1437. if (!is_logical_dev_addr_mode(lunaddrbytes))
  1438. return 0; /* It's the logical targets that may lack lun 0. */
  1439. if (!is_msa2xxx(h, tmpdevice))
  1440. return 0; /* It's only the MSA2xxx that have this problem. */
  1441. if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */
  1442. return 0;
  1443. if (is_hba_lunid(scsi3addr))
  1444. return 0; /* Don't add the RAID controller here. */
  1445. if (is_scsi_rev_5(h))
  1446. return 0; /* p1210m doesn't need to do this. */
  1447. #define MAX_MSA2XXX_ENCLOSURES 32
  1448. if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) {
  1449. dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX "
  1450. "enclosures exceeded. Check your hardware "
  1451. "configuration.");
  1452. return 0;
  1453. }
  1454. memset(scsi3addr, 0, 8);
  1455. scsi3addr[3] = target;
  1456. if (hpsa_update_device_info(h, scsi3addr, this_device))
  1457. return 0;
  1458. (*nmsa2xxx_enclosures)++;
  1459. hpsa_set_bus_target_lun(this_device, bus, target, 0);
  1460. set_bit(target, lunzerobits);
  1461. return 1;
  1462. }
  1463. /*
  1464. * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
  1465. * logdev. The number of luns in physdev and logdev are returned in
  1466. * *nphysicals and *nlogicals, respectively.
  1467. * Returns 0 on success, -1 otherwise.
  1468. */
  1469. static int hpsa_gather_lun_info(struct ctlr_info *h,
  1470. int reportlunsize,
  1471. struct ReportLUNdata *physdev, u32 *nphysicals,
  1472. struct ReportLUNdata *logdev, u32 *nlogicals)
  1473. {
  1474. if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) {
  1475. dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
  1476. return -1;
  1477. }
  1478. *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8;
  1479. if (*nphysicals > HPSA_MAX_PHYS_LUN) {
  1480. dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
  1481. " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  1482. *nphysicals - HPSA_MAX_PHYS_LUN);
  1483. *nphysicals = HPSA_MAX_PHYS_LUN;
  1484. }
  1485. if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
  1486. dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
  1487. return -1;
  1488. }
  1489. *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
  1490. /* Reject Logicals in excess of our max capability. */
  1491. if (*nlogicals > HPSA_MAX_LUN) {
  1492. dev_warn(&h->pdev->dev,
  1493. "maximum logical LUNs (%d) exceeded. "
  1494. "%d LUNs ignored.\n", HPSA_MAX_LUN,
  1495. *nlogicals - HPSA_MAX_LUN);
  1496. *nlogicals = HPSA_MAX_LUN;
  1497. }
  1498. if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
  1499. dev_warn(&h->pdev->dev,
  1500. "maximum logical + physical LUNs (%d) exceeded. "
  1501. "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  1502. *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
  1503. *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
  1504. }
  1505. return 0;
  1506. }
  1507. u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
  1508. int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list,
  1509. struct ReportLUNdata *logdev_list)
  1510. {
  1511. /* Helper function, figure out where the LUN ID info is coming from
  1512. * given index i, lists of physical and logical devices, where in
  1513. * the list the raid controller is supposed to appear (first or last)
  1514. */
  1515. int logicals_start = nphysicals + (raid_ctlr_position == 0);
  1516. int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
  1517. if (i == raid_ctlr_position)
  1518. return RAID_CTLR_LUNID;
  1519. if (i < logicals_start)
  1520. return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
  1521. if (i < last_device)
  1522. return &logdev_list->LUN[i - nphysicals -
  1523. (raid_ctlr_position == 0)][0];
  1524. BUG();
  1525. return NULL;
  1526. }
  1527. static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
  1528. {
  1529. /* the idea here is we could get notified
  1530. * that some devices have changed, so we do a report
  1531. * physical luns and report logical luns cmd, and adjust
  1532. * our list of devices accordingly.
  1533. *
  1534. * The scsi3addr's of devices won't change so long as the
  1535. * adapter is not reset. That means we can rescan and
  1536. * tell which devices we already know about, vs. new
  1537. * devices, vs. disappearing devices.
  1538. */
  1539. struct ReportLUNdata *physdev_list = NULL;
  1540. struct ReportLUNdata *logdev_list = NULL;
  1541. unsigned char *inq_buff = NULL;
  1542. u32 nphysicals = 0;
  1543. u32 nlogicals = 0;
  1544. u32 ndev_allocated = 0;
  1545. struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
  1546. int ncurrent = 0;
  1547. int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8;
  1548. int i, nmsa2xxx_enclosures, ndevs_to_allocate;
  1549. int bus, target, lun;
  1550. int raid_ctlr_position;
  1551. DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR);
  1552. currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  1553. GFP_KERNEL);
  1554. physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
  1555. logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
  1556. inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
  1557. tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
  1558. if (!currentsd || !physdev_list || !logdev_list ||
  1559. !inq_buff || !tmpdevice) {
  1560. dev_err(&h->pdev->dev, "out of memory\n");
  1561. goto out;
  1562. }
  1563. memset(lunzerobits, 0, sizeof(lunzerobits));
  1564. if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals,
  1565. logdev_list, &nlogicals))
  1566. goto out;
  1567. /* We might see up to 32 MSA2xxx enclosures, actually 8 of them
  1568. * but each of them 4 times through different paths. The plus 1
  1569. * is for the RAID controller.
  1570. */
  1571. ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1;
  1572. /* Allocate the per device structures */
  1573. for (i = 0; i < ndevs_to_allocate; i++) {
  1574. currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
  1575. if (!currentsd[i]) {
  1576. dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
  1577. __FILE__, __LINE__);
  1578. goto out;
  1579. }
  1580. ndev_allocated++;
  1581. }
  1582. if (unlikely(is_scsi_rev_5(h)))
  1583. raid_ctlr_position = 0;
  1584. else
  1585. raid_ctlr_position = nphysicals + nlogicals;
  1586. /* adjust our table of devices */
  1587. nmsa2xxx_enclosures = 0;
  1588. for (i = 0; i < nphysicals + nlogicals + 1; i++) {
  1589. u8 *lunaddrbytes;
  1590. /* Figure out where the LUN ID info is coming from */
  1591. lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
  1592. i, nphysicals, nlogicals, physdev_list, logdev_list);
  1593. /* skip masked physical devices. */
  1594. if (lunaddrbytes[3] & 0xC0 &&
  1595. i < nphysicals + (raid_ctlr_position == 0))
  1596. continue;
  1597. /* Get device type, vendor, model, device id */
  1598. if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice))
  1599. continue; /* skip it if we can't talk to it. */
  1600. figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun,
  1601. tmpdevice);
  1602. this_device = currentsd[ncurrent];
  1603. /*
  1604. * For the msa2xxx boxes, we have to insert a LUN 0 which
  1605. * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
  1606. * is nonetheless an enclosure device there. We have to
  1607. * present that otherwise linux won't find anything if
  1608. * there is no lun 0.
  1609. */
  1610. if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device,
  1611. lunaddrbytes, bus, target, lun, lunzerobits,
  1612. &nmsa2xxx_enclosures)) {
  1613. ncurrent++;
  1614. this_device = currentsd[ncurrent];
  1615. }
  1616. *this_device = *tmpdevice;
  1617. hpsa_set_bus_target_lun(this_device, bus, target, lun);
  1618. switch (this_device->devtype) {
  1619. case TYPE_ROM: {
  1620. /* We don't *really* support actual CD-ROM devices,
  1621. * just "One Button Disaster Recovery" tape drive
  1622. * which temporarily pretends to be a CD-ROM drive.
  1623. * So we check that the device is really an OBDR tape
  1624. * device by checking for "$DR-10" in bytes 43-48 of
  1625. * the inquiry data.
  1626. */
  1627. char obdr_sig[7];
  1628. #define OBDR_TAPE_SIG "$DR-10"
  1629. strncpy(obdr_sig, &inq_buff[43], 6);
  1630. obdr_sig[6] = '\0';
  1631. if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
  1632. /* Not OBDR device, ignore it. */
  1633. break;
  1634. }
  1635. ncurrent++;
  1636. break;
  1637. case TYPE_DISK:
  1638. if (i < nphysicals)
  1639. break;
  1640. ncurrent++;
  1641. break;
  1642. case TYPE_TAPE:
  1643. case TYPE_MEDIUM_CHANGER:
  1644. ncurrent++;
  1645. break;
  1646. case TYPE_RAID:
  1647. /* Only present the Smartarray HBA as a RAID controller.
  1648. * If it's a RAID controller other than the HBA itself
  1649. * (an external RAID controller, MSA500 or similar)
  1650. * don't present it.
  1651. */
  1652. if (!is_hba_lunid(lunaddrbytes))
  1653. break;
  1654. ncurrent++;
  1655. break;
  1656. default:
  1657. break;
  1658. }
  1659. if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA)
  1660. break;
  1661. }
  1662. adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
  1663. out:
  1664. kfree(tmpdevice);
  1665. for (i = 0; i < ndev_allocated; i++)
  1666. kfree(currentsd[i]);
  1667. kfree(currentsd);
  1668. kfree(inq_buff);
  1669. kfree(physdev_list);
  1670. kfree(logdev_list);
  1671. }
  1672. /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
  1673. * dma mapping and fills in the scatter gather entries of the
  1674. * hpsa command, cp.
  1675. */
  1676. static int hpsa_scatter_gather(struct ctlr_info *h,
  1677. struct CommandList *cp,
  1678. struct scsi_cmnd *cmd)
  1679. {
  1680. unsigned int len;
  1681. struct scatterlist *sg;
  1682. u64 addr64;
  1683. int use_sg, i, sg_index, chained;
  1684. struct SGDescriptor *curr_sg;
  1685. BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
  1686. use_sg = scsi_dma_map(cmd);
  1687. if (use_sg < 0)
  1688. return use_sg;
  1689. if (!use_sg)
  1690. goto sglist_finished;
  1691. curr_sg = cp->SG;
  1692. chained = 0;
  1693. sg_index = 0;
  1694. scsi_for_each_sg(cmd, sg, use_sg, i) {
  1695. if (i == h->max_cmd_sg_entries - 1 &&
  1696. use_sg > h->max_cmd_sg_entries) {
  1697. chained = 1;
  1698. curr_sg = h->cmd_sg_list[cp->cmdindex];
  1699. sg_index = 0;
  1700. }
  1701. addr64 = (u64) sg_dma_address(sg);
  1702. len = sg_dma_len(sg);
  1703. curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
  1704. curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
  1705. curr_sg->Len = len;
  1706. curr_sg->Ext = 0; /* we are not chaining */
  1707. curr_sg++;
  1708. }
  1709. if (use_sg + chained > h->maxSG)
  1710. h->maxSG = use_sg + chained;
  1711. if (chained) {
  1712. cp->Header.SGList = h->max_cmd_sg_entries;
  1713. cp->Header.SGTotal = (u16) (use_sg + 1);
  1714. hpsa_map_sg_chain_block(h, cp);
  1715. return 0;
  1716. }
  1717. sglist_finished:
  1718. cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
  1719. cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
  1720. return 0;
  1721. }
  1722. static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
  1723. void (*done)(struct scsi_cmnd *))
  1724. {
  1725. struct ctlr_info *h;
  1726. struct hpsa_scsi_dev_t *dev;
  1727. unsigned char scsi3addr[8];
  1728. struct CommandList *c;
  1729. unsigned long flags;
  1730. /* Get the ptr to our adapter structure out of cmd->host. */
  1731. h = sdev_to_hba(cmd->device);
  1732. dev = cmd->device->hostdata;
  1733. if (!dev) {
  1734. cmd->result = DID_NO_CONNECT << 16;
  1735. done(cmd);
  1736. return 0;
  1737. }
  1738. memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
  1739. /* Need a lock as this is being allocated from the pool */
  1740. spin_lock_irqsave(&h->lock, flags);
  1741. c = cmd_alloc(h);
  1742. spin_unlock_irqrestore(&h->lock, flags);
  1743. if (c == NULL) { /* trouble... */
  1744. dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
  1745. return SCSI_MLQUEUE_HOST_BUSY;
  1746. }
  1747. /* Fill in the command list header */
  1748. cmd->scsi_done = done; /* save this for use by completion code */
  1749. /* save c in case we have to abort it */
  1750. cmd->host_scribble = (unsigned char *) c;
  1751. c->cmd_type = CMD_SCSI;
  1752. c->scsi_cmd = cmd;
  1753. c->Header.ReplyQueue = 0; /* unused in simple mode */
  1754. memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
  1755. c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
  1756. c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
  1757. /* Fill in the request block... */
  1758. c->Request.Timeout = 0;
  1759. memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
  1760. BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
  1761. c->Request.CDBLen = cmd->cmd_len;
  1762. memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
  1763. c->Request.Type.Type = TYPE_CMD;
  1764. c->Request.Type.Attribute = ATTR_SIMPLE;
  1765. switch (cmd->sc_data_direction) {
  1766. case DMA_TO_DEVICE:
  1767. c->Request.Type.Direction = XFER_WRITE;
  1768. break;
  1769. case DMA_FROM_DEVICE:
  1770. c->Request.Type.Direction = XFER_READ;
  1771. break;
  1772. case DMA_NONE:
  1773. c->Request.Type.Direction = XFER_NONE;
  1774. break;
  1775. case DMA_BIDIRECTIONAL:
  1776. /* This can happen if a buggy application does a scsi passthru
  1777. * and sets both inlen and outlen to non-zero. ( see
  1778. * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
  1779. */
  1780. c->Request.Type.Direction = XFER_RSVD;
  1781. /* This is technically wrong, and hpsa controllers should
  1782. * reject it with CMD_INVALID, which is the most correct
  1783. * response, but non-fibre backends appear to let it
  1784. * slide by, and give the same results as if this field
  1785. * were set correctly. Either way is acceptable for
  1786. * our purposes here.
  1787. */
  1788. break;
  1789. default:
  1790. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  1791. cmd->sc_data_direction);
  1792. BUG();
  1793. break;
  1794. }
  1795. if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
  1796. cmd_free(h, c);
  1797. return SCSI_MLQUEUE_HOST_BUSY;
  1798. }
  1799. enqueue_cmd_and_start_io(h, c);
  1800. /* the cmd'll come back via intr handler in complete_scsi_command() */
  1801. return 0;
  1802. }
  1803. static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
  1804. static void hpsa_scan_start(struct Scsi_Host *sh)
  1805. {
  1806. struct ctlr_info *h = shost_to_hba(sh);
  1807. unsigned long flags;
  1808. /* wait until any scan already in progress is finished. */
  1809. while (1) {
  1810. spin_lock_irqsave(&h->scan_lock, flags);
  1811. if (h->scan_finished)
  1812. break;
  1813. spin_unlock_irqrestore(&h->scan_lock, flags);
  1814. wait_event(h->scan_wait_queue, h->scan_finished);
  1815. /* Note: We don't need to worry about a race between this
  1816. * thread and driver unload because the midlayer will
  1817. * have incremented the reference count, so unload won't
  1818. * happen if we're in here.
  1819. */
  1820. }
  1821. h->scan_finished = 0; /* mark scan as in progress */
  1822. spin_unlock_irqrestore(&h->scan_lock, flags);
  1823. hpsa_update_scsi_devices(h, h->scsi_host->host_no);
  1824. spin_lock_irqsave(&h->scan_lock, flags);
  1825. h->scan_finished = 1; /* mark scan as finished. */
  1826. wake_up_all(&h->scan_wait_queue);
  1827. spin_unlock_irqrestore(&h->scan_lock, flags);
  1828. }
  1829. static int hpsa_scan_finished(struct Scsi_Host *sh,
  1830. unsigned long elapsed_time)
  1831. {
  1832. struct ctlr_info *h = shost_to_hba(sh);
  1833. unsigned long flags;
  1834. int finished;
  1835. spin_lock_irqsave(&h->scan_lock, flags);
  1836. finished = h->scan_finished;
  1837. spin_unlock_irqrestore(&h->scan_lock, flags);
  1838. return finished;
  1839. }
  1840. static int hpsa_change_queue_depth(struct scsi_device *sdev,
  1841. int qdepth, int reason)
  1842. {
  1843. struct ctlr_info *h = sdev_to_hba(sdev);
  1844. if (reason != SCSI_QDEPTH_DEFAULT)
  1845. return -ENOTSUPP;
  1846. if (qdepth < 1)
  1847. qdepth = 1;
  1848. else
  1849. if (qdepth > h->nr_cmds)
  1850. qdepth = h->nr_cmds;
  1851. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1852. return sdev->queue_depth;
  1853. }
  1854. static void hpsa_unregister_scsi(struct ctlr_info *h)
  1855. {
  1856. /* we are being forcibly unloaded, and may not refuse. */
  1857. scsi_remove_host(h->scsi_host);
  1858. scsi_host_put(h->scsi_host);
  1859. h->scsi_host = NULL;
  1860. }
  1861. static int hpsa_register_scsi(struct ctlr_info *h)
  1862. {
  1863. int rc;
  1864. rc = hpsa_scsi_detect(h);
  1865. if (rc != 0)
  1866. dev_err(&h->pdev->dev, "hpsa_register_scsi: failed"
  1867. " hpsa_scsi_detect(), rc is %d\n", rc);
  1868. return rc;
  1869. }
  1870. static int wait_for_device_to_become_ready(struct ctlr_info *h,
  1871. unsigned char lunaddr[])
  1872. {
  1873. int rc = 0;
  1874. int count = 0;
  1875. int waittime = 1; /* seconds */
  1876. struct CommandList *c;
  1877. c = cmd_special_alloc(h);
  1878. if (!c) {
  1879. dev_warn(&h->pdev->dev, "out of memory in "
  1880. "wait_for_device_to_become_ready.\n");
  1881. return IO_ERROR;
  1882. }
  1883. /* Send test unit ready until device ready, or give up. */
  1884. while (count < HPSA_TUR_RETRY_LIMIT) {
  1885. /* Wait for a bit. do this first, because if we send
  1886. * the TUR right away, the reset will just abort it.
  1887. */
  1888. msleep(1000 * waittime);
  1889. count++;
  1890. /* Increase wait time with each try, up to a point. */
  1891. if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
  1892. waittime = waittime * 2;
  1893. /* Send the Test Unit Ready */
  1894. fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD);
  1895. hpsa_scsi_do_simple_cmd_core(h, c);
  1896. /* no unmap needed here because no data xfer. */
  1897. if (c->err_info->CommandStatus == CMD_SUCCESS)
  1898. break;
  1899. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  1900. c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
  1901. (c->err_info->SenseInfo[2] == NO_SENSE ||
  1902. c->err_info->SenseInfo[2] == UNIT_ATTENTION))
  1903. break;
  1904. dev_warn(&h->pdev->dev, "waiting %d secs "
  1905. "for device to become ready.\n", waittime);
  1906. rc = 1; /* device not ready. */
  1907. }
  1908. if (rc)
  1909. dev_warn(&h->pdev->dev, "giving up on device.\n");
  1910. else
  1911. dev_warn(&h->pdev->dev, "device is ready.\n");
  1912. cmd_special_free(h, c);
  1913. return rc;
  1914. }
  1915. /* Need at least one of these error handlers to keep ../scsi/hosts.c from
  1916. * complaining. Doing a host- or bus-reset can't do anything good here.
  1917. */
  1918. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
  1919. {
  1920. int rc;
  1921. struct ctlr_info *h;
  1922. struct hpsa_scsi_dev_t *dev;
  1923. /* find the controller to which the command to be aborted was sent */
  1924. h = sdev_to_hba(scsicmd->device);
  1925. if (h == NULL) /* paranoia */
  1926. return FAILED;
  1927. dev = scsicmd->device->hostdata;
  1928. if (!dev) {
  1929. dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
  1930. "device lookup failed.\n");
  1931. return FAILED;
  1932. }
  1933. dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
  1934. h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
  1935. /* send a reset to the SCSI LUN which the command was sent to */
  1936. rc = hpsa_send_reset(h, dev->scsi3addr);
  1937. if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
  1938. return SUCCESS;
  1939. dev_warn(&h->pdev->dev, "resetting device failed.\n");
  1940. return FAILED;
  1941. }
  1942. /*
  1943. * For operations that cannot sleep, a command block is allocated at init,
  1944. * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
  1945. * which ones are free or in use. Lock must be held when calling this.
  1946. * cmd_free() is the complement.
  1947. */
  1948. static struct CommandList *cmd_alloc(struct ctlr_info *h)
  1949. {
  1950. struct CommandList *c;
  1951. int i;
  1952. union u64bit temp64;
  1953. dma_addr_t cmd_dma_handle, err_dma_handle;
  1954. do {
  1955. i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
  1956. if (i == h->nr_cmds)
  1957. return NULL;
  1958. } while (test_and_set_bit
  1959. (i & (BITS_PER_LONG - 1),
  1960. h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
  1961. c = h->cmd_pool + i;
  1962. memset(c, 0, sizeof(*c));
  1963. cmd_dma_handle = h->cmd_pool_dhandle
  1964. + i * sizeof(*c);
  1965. c->err_info = h->errinfo_pool + i;
  1966. memset(c->err_info, 0, sizeof(*c->err_info));
  1967. err_dma_handle = h->errinfo_pool_dhandle
  1968. + i * sizeof(*c->err_info);
  1969. h->nr_allocs++;
  1970. c->cmdindex = i;
  1971. INIT_HLIST_NODE(&c->list);
  1972. c->busaddr = (u32) cmd_dma_handle;
  1973. temp64.val = (u64) err_dma_handle;
  1974. c->ErrDesc.Addr.lower = temp64.val32.lower;
  1975. c->ErrDesc.Addr.upper = temp64.val32.upper;
  1976. c->ErrDesc.Len = sizeof(*c->err_info);
  1977. c->h = h;
  1978. return c;
  1979. }
  1980. /* For operations that can wait for kmalloc to possibly sleep,
  1981. * this routine can be called. Lock need not be held to call
  1982. * cmd_special_alloc. cmd_special_free() is the complement.
  1983. */
  1984. static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
  1985. {
  1986. struct CommandList *c;
  1987. union u64bit temp64;
  1988. dma_addr_t cmd_dma_handle, err_dma_handle;
  1989. c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
  1990. if (c == NULL)
  1991. return NULL;
  1992. memset(c, 0, sizeof(*c));
  1993. c->cmdindex = -1;
  1994. c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
  1995. &err_dma_handle);
  1996. if (c->err_info == NULL) {
  1997. pci_free_consistent(h->pdev,
  1998. sizeof(*c), c, cmd_dma_handle);
  1999. return NULL;
  2000. }
  2001. memset(c->err_info, 0, sizeof(*c->err_info));
  2002. INIT_HLIST_NODE(&c->list);
  2003. c->busaddr = (u32) cmd_dma_handle;
  2004. temp64.val = (u64) err_dma_handle;
  2005. c->ErrDesc.Addr.lower = temp64.val32.lower;
  2006. c->ErrDesc.Addr.upper = temp64.val32.upper;
  2007. c->ErrDesc.Len = sizeof(*c->err_info);
  2008. c->h = h;
  2009. return c;
  2010. }
  2011. static void cmd_free(struct ctlr_info *h, struct CommandList *c)
  2012. {
  2013. int i;
  2014. i = c - h->cmd_pool;
  2015. clear_bit(i & (BITS_PER_LONG - 1),
  2016. h->cmd_pool_bits + (i / BITS_PER_LONG));
  2017. h->nr_frees++;
  2018. }
  2019. static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
  2020. {
  2021. union u64bit temp64;
  2022. temp64.val32.lower = c->ErrDesc.Addr.lower;
  2023. temp64.val32.upper = c->ErrDesc.Addr.upper;
  2024. pci_free_consistent(h->pdev, sizeof(*c->err_info),
  2025. c->err_info, (dma_addr_t) temp64.val);
  2026. pci_free_consistent(h->pdev, sizeof(*c),
  2027. c, (dma_addr_t) c->busaddr);
  2028. }
  2029. #ifdef CONFIG_COMPAT
  2030. static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
  2031. {
  2032. IOCTL32_Command_struct __user *arg32 =
  2033. (IOCTL32_Command_struct __user *) arg;
  2034. IOCTL_Command_struct arg64;
  2035. IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  2036. int err;
  2037. u32 cp;
  2038. err = 0;
  2039. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  2040. sizeof(arg64.LUN_info));
  2041. err |= copy_from_user(&arg64.Request, &arg32->Request,
  2042. sizeof(arg64.Request));
  2043. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  2044. sizeof(arg64.error_info));
  2045. err |= get_user(arg64.buf_size, &arg32->buf_size);
  2046. err |= get_user(cp, &arg32->buf);
  2047. arg64.buf = compat_ptr(cp);
  2048. err |= copy_to_user(p, &arg64, sizeof(arg64));
  2049. if (err)
  2050. return -EFAULT;
  2051. err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
  2052. if (err)
  2053. return err;
  2054. err |= copy_in_user(&arg32->error_info, &p->error_info,
  2055. sizeof(arg32->error_info));
  2056. if (err)
  2057. return -EFAULT;
  2058. return err;
  2059. }
  2060. static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
  2061. int cmd, void *arg)
  2062. {
  2063. BIG_IOCTL32_Command_struct __user *arg32 =
  2064. (BIG_IOCTL32_Command_struct __user *) arg;
  2065. BIG_IOCTL_Command_struct arg64;
  2066. BIG_IOCTL_Command_struct __user *p =
  2067. compat_alloc_user_space(sizeof(arg64));
  2068. int err;
  2069. u32 cp;
  2070. err = 0;
  2071. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  2072. sizeof(arg64.LUN_info));
  2073. err |= copy_from_user(&arg64.Request, &arg32->Request,
  2074. sizeof(arg64.Request));
  2075. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  2076. sizeof(arg64.error_info));
  2077. err |= get_user(arg64.buf_size, &arg32->buf_size);
  2078. err |= get_user(arg64.malloc_size, &arg32->malloc_size);
  2079. err |= get_user(cp, &arg32->buf);
  2080. arg64.buf = compat_ptr(cp);
  2081. err |= copy_to_user(p, &arg64, sizeof(arg64));
  2082. if (err)
  2083. return -EFAULT;
  2084. err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
  2085. if (err)
  2086. return err;
  2087. err |= copy_in_user(&arg32->error_info, &p->error_info,
  2088. sizeof(arg32->error_info));
  2089. if (err)
  2090. return -EFAULT;
  2091. return err;
  2092. }
  2093. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
  2094. {
  2095. switch (cmd) {
  2096. case CCISS_GETPCIINFO:
  2097. case CCISS_GETINTINFO:
  2098. case CCISS_SETINTINFO:
  2099. case CCISS_GETNODENAME:
  2100. case CCISS_SETNODENAME:
  2101. case CCISS_GETHEARTBEAT:
  2102. case CCISS_GETBUSTYPES:
  2103. case CCISS_GETFIRMVER:
  2104. case CCISS_GETDRIVVER:
  2105. case CCISS_REVALIDVOLS:
  2106. case CCISS_DEREGDISK:
  2107. case CCISS_REGNEWDISK:
  2108. case CCISS_REGNEWD:
  2109. case CCISS_RESCANDISK:
  2110. case CCISS_GETLUNINFO:
  2111. return hpsa_ioctl(dev, cmd, arg);
  2112. case CCISS_PASSTHRU32:
  2113. return hpsa_ioctl32_passthru(dev, cmd, arg);
  2114. case CCISS_BIG_PASSTHRU32:
  2115. return hpsa_ioctl32_big_passthru(dev, cmd, arg);
  2116. default:
  2117. return -ENOIOCTLCMD;
  2118. }
  2119. }
  2120. #endif
  2121. static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
  2122. {
  2123. struct hpsa_pci_info pciinfo;
  2124. if (!argp)
  2125. return -EINVAL;
  2126. pciinfo.domain = pci_domain_nr(h->pdev->bus);
  2127. pciinfo.bus = h->pdev->bus->number;
  2128. pciinfo.dev_fn = h->pdev->devfn;
  2129. pciinfo.board_id = h->board_id;
  2130. if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
  2131. return -EFAULT;
  2132. return 0;
  2133. }
  2134. static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
  2135. {
  2136. DriverVer_type DriverVer;
  2137. unsigned char vmaj, vmin, vsubmin;
  2138. int rc;
  2139. rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
  2140. &vmaj, &vmin, &vsubmin);
  2141. if (rc != 3) {
  2142. dev_info(&h->pdev->dev, "driver version string '%s' "
  2143. "unrecognized.", HPSA_DRIVER_VERSION);
  2144. vmaj = 0;
  2145. vmin = 0;
  2146. vsubmin = 0;
  2147. }
  2148. DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
  2149. if (!argp)
  2150. return -EINVAL;
  2151. if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
  2152. return -EFAULT;
  2153. return 0;
  2154. }
  2155. static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  2156. {
  2157. IOCTL_Command_struct iocommand;
  2158. struct CommandList *c;
  2159. char *buff = NULL;
  2160. union u64bit temp64;
  2161. if (!argp)
  2162. return -EINVAL;
  2163. if (!capable(CAP_SYS_RAWIO))
  2164. return -EPERM;
  2165. if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
  2166. return -EFAULT;
  2167. if ((iocommand.buf_size < 1) &&
  2168. (iocommand.Request.Type.Direction != XFER_NONE)) {
  2169. return -EINVAL;
  2170. }
  2171. if (iocommand.buf_size > 0) {
  2172. buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
  2173. if (buff == NULL)
  2174. return -EFAULT;
  2175. }
  2176. if (iocommand.Request.Type.Direction == XFER_WRITE) {
  2177. /* Copy the data into the buffer we created */
  2178. if (copy_from_user(buff, iocommand.buf, iocommand.buf_size)) {
  2179. kfree(buff);
  2180. return -EFAULT;
  2181. }
  2182. } else
  2183. memset(buff, 0, iocommand.buf_size);
  2184. c = cmd_special_alloc(h);
  2185. if (c == NULL) {
  2186. kfree(buff);
  2187. return -ENOMEM;
  2188. }
  2189. /* Fill in the command type */
  2190. c->cmd_type = CMD_IOCTL_PEND;
  2191. /* Fill in Command Header */
  2192. c->Header.ReplyQueue = 0; /* unused in simple mode */
  2193. if (iocommand.buf_size > 0) { /* buffer to fill */
  2194. c->Header.SGList = 1;
  2195. c->Header.SGTotal = 1;
  2196. } else { /* no buffers to fill */
  2197. c->Header.SGList = 0;
  2198. c->Header.SGTotal = 0;
  2199. }
  2200. memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
  2201. /* use the kernel address the cmd block for tag */
  2202. c->Header.Tag.lower = c->busaddr;
  2203. /* Fill in Request block */
  2204. memcpy(&c->Request, &iocommand.Request,
  2205. sizeof(c->Request));
  2206. /* Fill in the scatter gather information */
  2207. if (iocommand.buf_size > 0) {
  2208. temp64.val = pci_map_single(h->pdev, buff,
  2209. iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
  2210. c->SG[0].Addr.lower = temp64.val32.lower;
  2211. c->SG[0].Addr.upper = temp64.val32.upper;
  2212. c->SG[0].Len = iocommand.buf_size;
  2213. c->SG[0].Ext = 0; /* we are not chaining*/
  2214. }
  2215. hpsa_scsi_do_simple_cmd_core(h, c);
  2216. hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
  2217. check_ioctl_unit_attention(h, c);
  2218. /* Copy the error information out */
  2219. memcpy(&iocommand.error_info, c->err_info,
  2220. sizeof(iocommand.error_info));
  2221. if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
  2222. kfree(buff);
  2223. cmd_special_free(h, c);
  2224. return -EFAULT;
  2225. }
  2226. if (iocommand.Request.Type.Direction == XFER_READ) {
  2227. /* Copy the data out of the buffer we created */
  2228. if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
  2229. kfree(buff);
  2230. cmd_special_free(h, c);
  2231. return -EFAULT;
  2232. }
  2233. }
  2234. kfree(buff);
  2235. cmd_special_free(h, c);
  2236. return 0;
  2237. }
  2238. static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  2239. {
  2240. BIG_IOCTL_Command_struct *ioc;
  2241. struct CommandList *c;
  2242. unsigned char **buff = NULL;
  2243. int *buff_size = NULL;
  2244. union u64bit temp64;
  2245. BYTE sg_used = 0;
  2246. int status = 0;
  2247. int i;
  2248. u32 left;
  2249. u32 sz;
  2250. BYTE __user *data_ptr;
  2251. if (!argp)
  2252. return -EINVAL;
  2253. if (!capable(CAP_SYS_RAWIO))
  2254. return -EPERM;
  2255. ioc = (BIG_IOCTL_Command_struct *)
  2256. kmalloc(sizeof(*ioc), GFP_KERNEL);
  2257. if (!ioc) {
  2258. status = -ENOMEM;
  2259. goto cleanup1;
  2260. }
  2261. if (copy_from_user(ioc, argp, sizeof(*ioc))) {
  2262. status = -EFAULT;
  2263. goto cleanup1;
  2264. }
  2265. if ((ioc->buf_size < 1) &&
  2266. (ioc->Request.Type.Direction != XFER_NONE)) {
  2267. status = -EINVAL;
  2268. goto cleanup1;
  2269. }
  2270. /* Check kmalloc limits using all SGs */
  2271. if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
  2272. status = -EINVAL;
  2273. goto cleanup1;
  2274. }
  2275. if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
  2276. status = -EINVAL;
  2277. goto cleanup1;
  2278. }
  2279. buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
  2280. if (!buff) {
  2281. status = -ENOMEM;
  2282. goto cleanup1;
  2283. }
  2284. buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL);
  2285. if (!buff_size) {
  2286. status = -ENOMEM;
  2287. goto cleanup1;
  2288. }
  2289. left = ioc->buf_size;
  2290. data_ptr = ioc->buf;
  2291. while (left) {
  2292. sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
  2293. buff_size[sg_used] = sz;
  2294. buff[sg_used] = kmalloc(sz, GFP_KERNEL);
  2295. if (buff[sg_used] == NULL) {
  2296. status = -ENOMEM;
  2297. goto cleanup1;
  2298. }
  2299. if (ioc->Request.Type.Direction == XFER_WRITE) {
  2300. if (copy_from_user(buff[sg_used], data_ptr, sz)) {
  2301. status = -ENOMEM;
  2302. goto cleanup1;
  2303. }
  2304. } else
  2305. memset(buff[sg_used], 0, sz);
  2306. left -= sz;
  2307. data_ptr += sz;
  2308. sg_used++;
  2309. }
  2310. c = cmd_special_alloc(h);
  2311. if (c == NULL) {
  2312. status = -ENOMEM;
  2313. goto cleanup1;
  2314. }
  2315. c->cmd_type = CMD_IOCTL_PEND;
  2316. c->Header.ReplyQueue = 0;
  2317. if (ioc->buf_size > 0) {
  2318. c->Header.SGList = sg_used;
  2319. c->Header.SGTotal = sg_used;
  2320. } else {
  2321. c->Header.SGList = 0;
  2322. c->Header.SGTotal = 0;
  2323. }
  2324. memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
  2325. c->Header.Tag.lower = c->busaddr;
  2326. memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
  2327. if (ioc->buf_size > 0) {
  2328. int i;
  2329. for (i = 0; i < sg_used; i++) {
  2330. temp64.val = pci_map_single(h->pdev, buff[i],
  2331. buff_size[i], PCI_DMA_BIDIRECTIONAL);
  2332. c->SG[i].Addr.lower = temp64.val32.lower;
  2333. c->SG[i].Addr.upper = temp64.val32.upper;
  2334. c->SG[i].Len = buff_size[i];
  2335. /* we are not chaining */
  2336. c->SG[i].Ext = 0;
  2337. }
  2338. }
  2339. hpsa_scsi_do_simple_cmd_core(h, c);
  2340. hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
  2341. check_ioctl_unit_attention(h, c);
  2342. /* Copy the error information out */
  2343. memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
  2344. if (copy_to_user(argp, ioc, sizeof(*ioc))) {
  2345. cmd_special_free(h, c);
  2346. status = -EFAULT;
  2347. goto cleanup1;
  2348. }
  2349. if (ioc->Request.Type.Direction == XFER_READ) {
  2350. /* Copy the data out of the buffer we created */
  2351. BYTE __user *ptr = ioc->buf;
  2352. for (i = 0; i < sg_used; i++) {
  2353. if (copy_to_user(ptr, buff[i], buff_size[i])) {
  2354. cmd_special_free(h, c);
  2355. status = -EFAULT;
  2356. goto cleanup1;
  2357. }
  2358. ptr += buff_size[i];
  2359. }
  2360. }
  2361. cmd_special_free(h, c);
  2362. status = 0;
  2363. cleanup1:
  2364. if (buff) {
  2365. for (i = 0; i < sg_used; i++)
  2366. kfree(buff[i]);
  2367. kfree(buff);
  2368. }
  2369. kfree(buff_size);
  2370. kfree(ioc);
  2371. return status;
  2372. }
  2373. static void check_ioctl_unit_attention(struct ctlr_info *h,
  2374. struct CommandList *c)
  2375. {
  2376. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  2377. c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
  2378. (void) check_for_unit_attention(h, c);
  2379. }
  2380. /*
  2381. * ioctl
  2382. */
  2383. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
  2384. {
  2385. struct ctlr_info *h;
  2386. void __user *argp = (void __user *)arg;
  2387. h = sdev_to_hba(dev);
  2388. switch (cmd) {
  2389. case CCISS_DEREGDISK:
  2390. case CCISS_REGNEWDISK:
  2391. case CCISS_REGNEWD:
  2392. hpsa_scan_start(h->scsi_host);
  2393. return 0;
  2394. case CCISS_GETPCIINFO:
  2395. return hpsa_getpciinfo_ioctl(h, argp);
  2396. case CCISS_GETDRIVVER:
  2397. return hpsa_getdrivver_ioctl(h, argp);
  2398. case CCISS_PASSTHRU:
  2399. return hpsa_passthru_ioctl(h, argp);
  2400. case CCISS_BIG_PASSTHRU:
  2401. return hpsa_big_passthru_ioctl(h, argp);
  2402. default:
  2403. return -ENOTTY;
  2404. }
  2405. }
  2406. static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  2407. void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
  2408. int cmd_type)
  2409. {
  2410. int pci_dir = XFER_NONE;
  2411. c->cmd_type = CMD_IOCTL_PEND;
  2412. c->Header.ReplyQueue = 0;
  2413. if (buff != NULL && size > 0) {
  2414. c->Header.SGList = 1;
  2415. c->Header.SGTotal = 1;
  2416. } else {
  2417. c->Header.SGList = 0;
  2418. c->Header.SGTotal = 0;
  2419. }
  2420. c->Header.Tag.lower = c->busaddr;
  2421. memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
  2422. c->Request.Type.Type = cmd_type;
  2423. if (cmd_type == TYPE_CMD) {
  2424. switch (cmd) {
  2425. case HPSA_INQUIRY:
  2426. /* are we trying to read a vital product page */
  2427. if (page_code != 0) {
  2428. c->Request.CDB[1] = 0x01;
  2429. c->Request.CDB[2] = page_code;
  2430. }
  2431. c->Request.CDBLen = 6;
  2432. c->Request.Type.Attribute = ATTR_SIMPLE;
  2433. c->Request.Type.Direction = XFER_READ;
  2434. c->Request.Timeout = 0;
  2435. c->Request.CDB[0] = HPSA_INQUIRY;
  2436. c->Request.CDB[4] = size & 0xFF;
  2437. break;
  2438. case HPSA_REPORT_LOG:
  2439. case HPSA_REPORT_PHYS:
  2440. /* Talking to controller so It's a physical command
  2441. mode = 00 target = 0. Nothing to write.
  2442. */
  2443. c->Request.CDBLen = 12;
  2444. c->Request.Type.Attribute = ATTR_SIMPLE;
  2445. c->Request.Type.Direction = XFER_READ;
  2446. c->Request.Timeout = 0;
  2447. c->Request.CDB[0] = cmd;
  2448. c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
  2449. c->Request.CDB[7] = (size >> 16) & 0xFF;
  2450. c->Request.CDB[8] = (size >> 8) & 0xFF;
  2451. c->Request.CDB[9] = size & 0xFF;
  2452. break;
  2453. case HPSA_CACHE_FLUSH:
  2454. c->Request.CDBLen = 12;
  2455. c->Request.Type.Attribute = ATTR_SIMPLE;
  2456. c->Request.Type.Direction = XFER_WRITE;
  2457. c->Request.Timeout = 0;
  2458. c->Request.CDB[0] = BMIC_WRITE;
  2459. c->Request.CDB[6] = BMIC_CACHE_FLUSH;
  2460. break;
  2461. case TEST_UNIT_READY:
  2462. c->Request.CDBLen = 6;
  2463. c->Request.Type.Attribute = ATTR_SIMPLE;
  2464. c->Request.Type.Direction = XFER_NONE;
  2465. c->Request.Timeout = 0;
  2466. break;
  2467. default:
  2468. dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
  2469. BUG();
  2470. return;
  2471. }
  2472. } else if (cmd_type == TYPE_MSG) {
  2473. switch (cmd) {
  2474. case HPSA_DEVICE_RESET_MSG:
  2475. c->Request.CDBLen = 16;
  2476. c->Request.Type.Type = 1; /* It is a MSG not a CMD */
  2477. c->Request.Type.Attribute = ATTR_SIMPLE;
  2478. c->Request.Type.Direction = XFER_NONE;
  2479. c->Request.Timeout = 0; /* Don't time out */
  2480. c->Request.CDB[0] = 0x01; /* RESET_MSG is 0x01 */
  2481. c->Request.CDB[1] = 0x03; /* Reset target above */
  2482. /* If bytes 4-7 are zero, it means reset the */
  2483. /* LunID device */
  2484. c->Request.CDB[4] = 0x00;
  2485. c->Request.CDB[5] = 0x00;
  2486. c->Request.CDB[6] = 0x00;
  2487. c->Request.CDB[7] = 0x00;
  2488. break;
  2489. default:
  2490. dev_warn(&h->pdev->dev, "unknown message type %d\n",
  2491. cmd);
  2492. BUG();
  2493. }
  2494. } else {
  2495. dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
  2496. BUG();
  2497. }
  2498. switch (c->Request.Type.Direction) {
  2499. case XFER_READ:
  2500. pci_dir = PCI_DMA_FROMDEVICE;
  2501. break;
  2502. case XFER_WRITE:
  2503. pci_dir = PCI_DMA_TODEVICE;
  2504. break;
  2505. case XFER_NONE:
  2506. pci_dir = PCI_DMA_NONE;
  2507. break;
  2508. default:
  2509. pci_dir = PCI_DMA_BIDIRECTIONAL;
  2510. }
  2511. hpsa_map_one(h->pdev, c, buff, size, pci_dir);
  2512. return;
  2513. }
  2514. /*
  2515. * Map (physical) PCI mem into (virtual) kernel space
  2516. */
  2517. static void __iomem *remap_pci_mem(ulong base, ulong size)
  2518. {
  2519. ulong page_base = ((ulong) base) & PAGE_MASK;
  2520. ulong page_offs = ((ulong) base) - page_base;
  2521. void __iomem *page_remapped = ioremap(page_base, page_offs + size);
  2522. return page_remapped ? (page_remapped + page_offs) : NULL;
  2523. }
  2524. /* Takes cmds off the submission queue and sends them to the hardware,
  2525. * then puts them on the queue of cmds waiting for completion.
  2526. */
  2527. static void start_io(struct ctlr_info *h)
  2528. {
  2529. struct CommandList *c;
  2530. while (!hlist_empty(&h->reqQ)) {
  2531. c = hlist_entry(h->reqQ.first, struct CommandList, list);
  2532. /* can't do anything if fifo is full */
  2533. if ((h->access.fifo_full(h))) {
  2534. dev_warn(&h->pdev->dev, "fifo full\n");
  2535. break;
  2536. }
  2537. /* Get the first entry from the Request Q */
  2538. removeQ(c);
  2539. h->Qdepth--;
  2540. /* Tell the controller execute command */
  2541. h->access.submit_command(h, c);
  2542. /* Put job onto the completed Q */
  2543. addQ(&h->cmpQ, c);
  2544. }
  2545. }
  2546. static inline unsigned long get_next_completion(struct ctlr_info *h)
  2547. {
  2548. return h->access.command_completed(h);
  2549. }
  2550. static inline bool interrupt_pending(struct ctlr_info *h)
  2551. {
  2552. return h->access.intr_pending(h);
  2553. }
  2554. static inline long interrupt_not_for_us(struct ctlr_info *h)
  2555. {
  2556. return (h->access.intr_pending(h) == 0) ||
  2557. (h->interrupts_enabled == 0);
  2558. }
  2559. static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
  2560. u32 raw_tag)
  2561. {
  2562. if (unlikely(tag_index >= h->nr_cmds)) {
  2563. dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
  2564. return 1;
  2565. }
  2566. return 0;
  2567. }
  2568. static inline void finish_cmd(struct CommandList *c, u32 raw_tag)
  2569. {
  2570. removeQ(c);
  2571. if (likely(c->cmd_type == CMD_SCSI))
  2572. complete_scsi_command(c, 0, raw_tag);
  2573. else if (c->cmd_type == CMD_IOCTL_PEND)
  2574. complete(c->waiting);
  2575. }
  2576. static inline u32 hpsa_tag_contains_index(u32 tag)
  2577. {
  2578. #define DIRECT_LOOKUP_BIT 0x10
  2579. return tag & DIRECT_LOOKUP_BIT;
  2580. }
  2581. static inline u32 hpsa_tag_to_index(u32 tag)
  2582. {
  2583. #define DIRECT_LOOKUP_SHIFT 5
  2584. return tag >> DIRECT_LOOKUP_SHIFT;
  2585. }
  2586. static inline u32 hpsa_tag_discard_error_bits(u32 tag)
  2587. {
  2588. #define HPSA_ERROR_BITS 0x03
  2589. return tag & ~HPSA_ERROR_BITS;
  2590. }
  2591. /* process completion of an indexed ("direct lookup") command */
  2592. static inline u32 process_indexed_cmd(struct ctlr_info *h,
  2593. u32 raw_tag)
  2594. {
  2595. u32 tag_index;
  2596. struct CommandList *c;
  2597. tag_index = hpsa_tag_to_index(raw_tag);
  2598. if (bad_tag(h, tag_index, raw_tag))
  2599. return next_command(h);
  2600. c = h->cmd_pool + tag_index;
  2601. finish_cmd(c, raw_tag);
  2602. return next_command(h);
  2603. }
  2604. /* process completion of a non-indexed command */
  2605. static inline u32 process_nonindexed_cmd(struct ctlr_info *h,
  2606. u32 raw_tag)
  2607. {
  2608. u32 tag;
  2609. struct CommandList *c = NULL;
  2610. struct hlist_node *tmp;
  2611. tag = hpsa_tag_discard_error_bits(raw_tag);
  2612. hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
  2613. if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
  2614. finish_cmd(c, raw_tag);
  2615. return next_command(h);
  2616. }
  2617. }
  2618. bad_tag(h, h->nr_cmds + 1, raw_tag);
  2619. return next_command(h);
  2620. }
  2621. static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id)
  2622. {
  2623. struct ctlr_info *h = dev_id;
  2624. unsigned long flags;
  2625. u32 raw_tag;
  2626. if (interrupt_not_for_us(h))
  2627. return IRQ_NONE;
  2628. spin_lock_irqsave(&h->lock, flags);
  2629. while (interrupt_pending(h)) {
  2630. raw_tag = get_next_completion(h);
  2631. while (raw_tag != FIFO_EMPTY) {
  2632. if (hpsa_tag_contains_index(raw_tag))
  2633. raw_tag = process_indexed_cmd(h, raw_tag);
  2634. else
  2635. raw_tag = process_nonindexed_cmd(h, raw_tag);
  2636. }
  2637. }
  2638. spin_unlock_irqrestore(&h->lock, flags);
  2639. return IRQ_HANDLED;
  2640. }
  2641. static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id)
  2642. {
  2643. struct ctlr_info *h = dev_id;
  2644. unsigned long flags;
  2645. u32 raw_tag;
  2646. spin_lock_irqsave(&h->lock, flags);
  2647. raw_tag = get_next_completion(h);
  2648. while (raw_tag != FIFO_EMPTY) {
  2649. if (hpsa_tag_contains_index(raw_tag))
  2650. raw_tag = process_indexed_cmd(h, raw_tag);
  2651. else
  2652. raw_tag = process_nonindexed_cmd(h, raw_tag);
  2653. }
  2654. spin_unlock_irqrestore(&h->lock, flags);
  2655. return IRQ_HANDLED;
  2656. }
  2657. /* Send a message CDB to the firmware. */
  2658. static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
  2659. unsigned char type)
  2660. {
  2661. struct Command {
  2662. struct CommandListHeader CommandHeader;
  2663. struct RequestBlock Request;
  2664. struct ErrDescriptor ErrorDescriptor;
  2665. };
  2666. struct Command *cmd;
  2667. static const size_t cmd_sz = sizeof(*cmd) +
  2668. sizeof(cmd->ErrorDescriptor);
  2669. dma_addr_t paddr64;
  2670. uint32_t paddr32, tag;
  2671. void __iomem *vaddr;
  2672. int i, err;
  2673. vaddr = pci_ioremap_bar(pdev, 0);
  2674. if (vaddr == NULL)
  2675. return -ENOMEM;
  2676. /* The Inbound Post Queue only accepts 32-bit physical addresses for the
  2677. * CCISS commands, so they must be allocated from the lower 4GiB of
  2678. * memory.
  2679. */
  2680. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  2681. if (err) {
  2682. iounmap(vaddr);
  2683. return -ENOMEM;
  2684. }
  2685. cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
  2686. if (cmd == NULL) {
  2687. iounmap(vaddr);
  2688. return -ENOMEM;
  2689. }
  2690. /* This must fit, because of the 32-bit consistent DMA mask. Also,
  2691. * although there's no guarantee, we assume that the address is at
  2692. * least 4-byte aligned (most likely, it's page-aligned).
  2693. */
  2694. paddr32 = paddr64;
  2695. cmd->CommandHeader.ReplyQueue = 0;
  2696. cmd->CommandHeader.SGList = 0;
  2697. cmd->CommandHeader.SGTotal = 0;
  2698. cmd->CommandHeader.Tag.lower = paddr32;
  2699. cmd->CommandHeader.Tag.upper = 0;
  2700. memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
  2701. cmd->Request.CDBLen = 16;
  2702. cmd->Request.Type.Type = TYPE_MSG;
  2703. cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
  2704. cmd->Request.Type.Direction = XFER_NONE;
  2705. cmd->Request.Timeout = 0; /* Don't time out */
  2706. cmd->Request.CDB[0] = opcode;
  2707. cmd->Request.CDB[1] = type;
  2708. memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
  2709. cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
  2710. cmd->ErrorDescriptor.Addr.upper = 0;
  2711. cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
  2712. writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
  2713. for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
  2714. tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
  2715. if (hpsa_tag_discard_error_bits(tag) == paddr32)
  2716. break;
  2717. msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
  2718. }
  2719. iounmap(vaddr);
  2720. /* we leak the DMA buffer here ... no choice since the controller could
  2721. * still complete the command.
  2722. */
  2723. if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
  2724. dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
  2725. opcode, type);
  2726. return -ETIMEDOUT;
  2727. }
  2728. pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
  2729. if (tag & HPSA_ERROR_BIT) {
  2730. dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
  2731. opcode, type);
  2732. return -EIO;
  2733. }
  2734. dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
  2735. opcode, type);
  2736. return 0;
  2737. }
  2738. #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0)
  2739. #define hpsa_noop(p) hpsa_message(p, 3, 0)
  2740. static __devinit int hpsa_reset_msi(struct pci_dev *pdev)
  2741. {
  2742. /* the #defines are stolen from drivers/pci/msi.h. */
  2743. #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
  2744. #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
  2745. int pos;
  2746. u16 control = 0;
  2747. pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
  2748. if (pos) {
  2749. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  2750. if (control & PCI_MSI_FLAGS_ENABLE) {
  2751. dev_info(&pdev->dev, "resetting MSI\n");
  2752. pci_write_config_word(pdev, msi_control_reg(pos),
  2753. control & ~PCI_MSI_FLAGS_ENABLE);
  2754. }
  2755. }
  2756. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  2757. if (pos) {
  2758. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  2759. if (control & PCI_MSIX_FLAGS_ENABLE) {
  2760. dev_info(&pdev->dev, "resetting MSI-X\n");
  2761. pci_write_config_word(pdev, msi_control_reg(pos),
  2762. control & ~PCI_MSIX_FLAGS_ENABLE);
  2763. }
  2764. }
  2765. return 0;
  2766. }
  2767. static int hpsa_controller_hard_reset(struct pci_dev *pdev,
  2768. void * __iomem vaddr, bool use_doorbell)
  2769. {
  2770. u16 pmcsr;
  2771. int pos;
  2772. if (use_doorbell) {
  2773. /* For everything after the P600, the PCI power state method
  2774. * of resetting the controller doesn't work, so we have this
  2775. * other way using the doorbell register.
  2776. */
  2777. dev_info(&pdev->dev, "using doorbell to reset controller\n");
  2778. writel(DOORBELL_CTLR_RESET, vaddr + SA5_DOORBELL);
  2779. msleep(1000);
  2780. } else { /* Try to do it the PCI power state way */
  2781. /* Quoting from the Open CISS Specification: "The Power
  2782. * Management Control/Status Register (CSR) controls the power
  2783. * state of the device. The normal operating state is D0,
  2784. * CSR=00h. The software off state is D3, CSR=03h. To reset
  2785. * the controller, place the interface device in D3 then to D0,
  2786. * this causes a secondary PCI reset which will reset the
  2787. * controller." */
  2788. pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
  2789. if (pos == 0) {
  2790. dev_err(&pdev->dev,
  2791. "hpsa_reset_controller: "
  2792. "PCI PM not supported\n");
  2793. return -ENODEV;
  2794. }
  2795. dev_info(&pdev->dev, "using PCI PM to reset controller\n");
  2796. /* enter the D3hot power management state */
  2797. pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
  2798. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  2799. pmcsr |= PCI_D3hot;
  2800. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  2801. msleep(500);
  2802. /* enter the D0 power management state */
  2803. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  2804. pmcsr |= PCI_D0;
  2805. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  2806. msleep(500);
  2807. }
  2808. return 0;
  2809. }
  2810. /* This does a hard reset of the controller using PCI power management
  2811. * states or the using the doorbell register.
  2812. */
  2813. static __devinit int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
  2814. {
  2815. u16 saved_config_space[32];
  2816. u64 cfg_offset;
  2817. u32 cfg_base_addr;
  2818. u64 cfg_base_addr_index;
  2819. void __iomem *vaddr;
  2820. unsigned long paddr;
  2821. u32 misc_fw_support, active_transport;
  2822. int rc, i;
  2823. struct CfgTable __iomem *cfgtable;
  2824. bool use_doorbell;
  2825. u32 board_id;
  2826. /* For controllers as old as the P600, this is very nearly
  2827. * the same thing as
  2828. *
  2829. * pci_save_state(pci_dev);
  2830. * pci_set_power_state(pci_dev, PCI_D3hot);
  2831. * pci_set_power_state(pci_dev, PCI_D0);
  2832. * pci_restore_state(pci_dev);
  2833. *
  2834. * but we can't use these nice canned kernel routines on
  2835. * kexec, because they also check the MSI/MSI-X state in PCI
  2836. * configuration space and do the wrong thing when it is
  2837. * set/cleared. Also, the pci_save/restore_state functions
  2838. * violate the ordering requirements for restoring the
  2839. * configuration space from the CCISS document (see the
  2840. * comment below). So we roll our own ....
  2841. *
  2842. * For controllers newer than the P600, the pci power state
  2843. * method of resetting doesn't work so we have another way
  2844. * using the doorbell register.
  2845. */
  2846. /* Exclude 640x boards. These are two pci devices in one slot
  2847. * which share a battery backed cache module. One controls the
  2848. * cache, the other accesses the cache through the one that controls
  2849. * it. If we reset the one controlling the cache, the other will
  2850. * likely not be happy. Just forbid resetting this conjoined mess.
  2851. * The 640x isn't really supported by hpsa anyway.
  2852. */
  2853. hpsa_lookup_board_id(pdev, &board_id);
  2854. if (board_id == 0x409C0E11 || board_id == 0x409D0E11)
  2855. return -ENOTSUPP;
  2856. for (i = 0; i < 32; i++)
  2857. pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
  2858. /* find the first memory BAR, so we can find the cfg table */
  2859. rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
  2860. if (rc)
  2861. return rc;
  2862. vaddr = remap_pci_mem(paddr, 0x250);
  2863. if (!vaddr)
  2864. return -ENOMEM;
  2865. /* find cfgtable in order to check if reset via doorbell is supported */
  2866. rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
  2867. &cfg_base_addr_index, &cfg_offset);
  2868. if (rc)
  2869. goto unmap_vaddr;
  2870. cfgtable = remap_pci_mem(pci_resource_start(pdev,
  2871. cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
  2872. if (!cfgtable) {
  2873. rc = -ENOMEM;
  2874. goto unmap_vaddr;
  2875. }
  2876. /* If reset via doorbell register is supported, use that. */
  2877. misc_fw_support = readl(&cfgtable->misc_fw_support);
  2878. use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
  2879. /* The doorbell reset seems to cause lockups on some Smart
  2880. * Arrays (e.g. P410, P410i, maybe others). Until this is
  2881. * fixed or at least isolated, avoid the doorbell reset.
  2882. */
  2883. use_doorbell = 0;
  2884. rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
  2885. if (rc)
  2886. goto unmap_cfgtable;
  2887. /* Restore the PCI configuration space. The Open CISS
  2888. * Specification says, "Restore the PCI Configuration
  2889. * Registers, offsets 00h through 60h. It is important to
  2890. * restore the command register, 16-bits at offset 04h,
  2891. * last. Do not restore the configuration status register,
  2892. * 16-bits at offset 06h." Note that the offset is 2*i.
  2893. */
  2894. for (i = 0; i < 32; i++) {
  2895. if (i == 2 || i == 3)
  2896. continue;
  2897. pci_write_config_word(pdev, 2*i, saved_config_space[i]);
  2898. }
  2899. wmb();
  2900. pci_write_config_word(pdev, 4, saved_config_space[2]);
  2901. /* Some devices (notably the HP Smart Array 5i Controller)
  2902. need a little pause here */
  2903. msleep(HPSA_POST_RESET_PAUSE_MSECS);
  2904. /* Controller should be in simple mode at this point. If it's not,
  2905. * It means we're on one of those controllers which doesn't support
  2906. * the doorbell reset method and on which the PCI power management reset
  2907. * method doesn't work (P800, for example.)
  2908. * In those cases, pretend the reset worked and hope for the best.
  2909. */
  2910. active_transport = readl(&cfgtable->TransportActive);
  2911. if (active_transport & PERFORMANT_MODE) {
  2912. dev_warn(&pdev->dev, "Unable to successfully reset controller,"
  2913. " proceeding anyway.\n");
  2914. rc = -ENOTSUPP;
  2915. }
  2916. unmap_cfgtable:
  2917. iounmap(cfgtable);
  2918. unmap_vaddr:
  2919. iounmap(vaddr);
  2920. return rc;
  2921. }
  2922. /*
  2923. * We cannot read the structure directly, for portability we must use
  2924. * the io functions.
  2925. * This is for debug only.
  2926. */
  2927. static void print_cfg_table(struct device *dev, struct CfgTable *tb)
  2928. {
  2929. #ifdef HPSA_DEBUG
  2930. int i;
  2931. char temp_name[17];
  2932. dev_info(dev, "Controller Configuration information\n");
  2933. dev_info(dev, "------------------------------------\n");
  2934. for (i = 0; i < 4; i++)
  2935. temp_name[i] = readb(&(tb->Signature[i]));
  2936. temp_name[4] = '\0';
  2937. dev_info(dev, " Signature = %s\n", temp_name);
  2938. dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
  2939. dev_info(dev, " Transport methods supported = 0x%x\n",
  2940. readl(&(tb->TransportSupport)));
  2941. dev_info(dev, " Transport methods active = 0x%x\n",
  2942. readl(&(tb->TransportActive)));
  2943. dev_info(dev, " Requested transport Method = 0x%x\n",
  2944. readl(&(tb->HostWrite.TransportRequest)));
  2945. dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
  2946. readl(&(tb->HostWrite.CoalIntDelay)));
  2947. dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
  2948. readl(&(tb->HostWrite.CoalIntCount)));
  2949. dev_info(dev, " Max outstanding commands = 0x%d\n",
  2950. readl(&(tb->CmdsOutMax)));
  2951. dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
  2952. for (i = 0; i < 16; i++)
  2953. temp_name[i] = readb(&(tb->ServerName[i]));
  2954. temp_name[16] = '\0';
  2955. dev_info(dev, " Server Name = %s\n", temp_name);
  2956. dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
  2957. readl(&(tb->HeartBeat)));
  2958. #endif /* HPSA_DEBUG */
  2959. }
  2960. static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
  2961. {
  2962. int i, offset, mem_type, bar_type;
  2963. if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
  2964. return 0;
  2965. offset = 0;
  2966. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  2967. bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
  2968. if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
  2969. offset += 4;
  2970. else {
  2971. mem_type = pci_resource_flags(pdev, i) &
  2972. PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  2973. switch (mem_type) {
  2974. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  2975. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  2976. offset += 4; /* 32 bit */
  2977. break;
  2978. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  2979. offset += 8;
  2980. break;
  2981. default: /* reserved in PCI 2.2 */
  2982. dev_warn(&pdev->dev,
  2983. "base address is invalid\n");
  2984. return -1;
  2985. break;
  2986. }
  2987. }
  2988. if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
  2989. return i + 1;
  2990. }
  2991. return -1;
  2992. }
  2993. /* If MSI/MSI-X is supported by the kernel we will try to enable it on
  2994. * controllers that are capable. If not, we use IO-APIC mode.
  2995. */
  2996. static void __devinit hpsa_interrupt_mode(struct ctlr_info *h)
  2997. {
  2998. #ifdef CONFIG_PCI_MSI
  2999. int err;
  3000. struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1},
  3001. {0, 2}, {0, 3}
  3002. };
  3003. /* Some boards advertise MSI but don't really support it */
  3004. if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
  3005. (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
  3006. goto default_int_mode;
  3007. if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
  3008. dev_info(&h->pdev->dev, "MSIX\n");
  3009. err = pci_enable_msix(h->pdev, hpsa_msix_entries, 4);
  3010. if (!err) {
  3011. h->intr[0] = hpsa_msix_entries[0].vector;
  3012. h->intr[1] = hpsa_msix_entries[1].vector;
  3013. h->intr[2] = hpsa_msix_entries[2].vector;
  3014. h->intr[3] = hpsa_msix_entries[3].vector;
  3015. h->msix_vector = 1;
  3016. return;
  3017. }
  3018. if (err > 0) {
  3019. dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
  3020. "available\n", err);
  3021. goto default_int_mode;
  3022. } else {
  3023. dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
  3024. err);
  3025. goto default_int_mode;
  3026. }
  3027. }
  3028. if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
  3029. dev_info(&h->pdev->dev, "MSI\n");
  3030. if (!pci_enable_msi(h->pdev))
  3031. h->msi_vector = 1;
  3032. else
  3033. dev_warn(&h->pdev->dev, "MSI init failed\n");
  3034. }
  3035. default_int_mode:
  3036. #endif /* CONFIG_PCI_MSI */
  3037. /* if we get here we're going to use the default interrupt mode */
  3038. h->intr[PERF_MODE_INT] = h->pdev->irq;
  3039. }
  3040. static int __devinit hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
  3041. {
  3042. int i;
  3043. u32 subsystem_vendor_id, subsystem_device_id;
  3044. subsystem_vendor_id = pdev->subsystem_vendor;
  3045. subsystem_device_id = pdev->subsystem_device;
  3046. *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
  3047. subsystem_vendor_id;
  3048. for (i = 0; i < ARRAY_SIZE(products); i++)
  3049. if (*board_id == products[i].board_id)
  3050. return i;
  3051. if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
  3052. subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
  3053. !hpsa_allow_any) {
  3054. dev_warn(&pdev->dev, "unrecognized board ID: "
  3055. "0x%08x, ignoring.\n", *board_id);
  3056. return -ENODEV;
  3057. }
  3058. return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
  3059. }
  3060. static inline bool hpsa_board_disabled(struct pci_dev *pdev)
  3061. {
  3062. u16 command;
  3063. (void) pci_read_config_word(pdev, PCI_COMMAND, &command);
  3064. return ((command & PCI_COMMAND_MEMORY) == 0);
  3065. }
  3066. static int __devinit hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
  3067. unsigned long *memory_bar)
  3068. {
  3069. int i;
  3070. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  3071. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  3072. /* addressing mode bits already removed */
  3073. *memory_bar = pci_resource_start(pdev, i);
  3074. dev_dbg(&pdev->dev, "memory BAR = %lx\n",
  3075. *memory_bar);
  3076. return 0;
  3077. }
  3078. dev_warn(&pdev->dev, "no memory BAR found\n");
  3079. return -ENODEV;
  3080. }
  3081. static int __devinit hpsa_wait_for_board_ready(struct ctlr_info *h)
  3082. {
  3083. int i;
  3084. u32 scratchpad;
  3085. for (i = 0; i < HPSA_BOARD_READY_ITERATIONS; i++) {
  3086. scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
  3087. if (scratchpad == HPSA_FIRMWARE_READY)
  3088. return 0;
  3089. msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
  3090. }
  3091. dev_warn(&h->pdev->dev, "board not ready, timed out.\n");
  3092. return -ENODEV;
  3093. }
  3094. static int __devinit hpsa_find_cfg_addrs(struct pci_dev *pdev,
  3095. void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index,
  3096. u64 *cfg_offset)
  3097. {
  3098. *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
  3099. *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
  3100. *cfg_base_addr &= (u32) 0x0000ffff;
  3101. *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
  3102. if (*cfg_base_addr_index == -1) {
  3103. dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
  3104. return -ENODEV;
  3105. }
  3106. return 0;
  3107. }
  3108. static int __devinit hpsa_find_cfgtables(struct ctlr_info *h)
  3109. {
  3110. u64 cfg_offset;
  3111. u32 cfg_base_addr;
  3112. u64 cfg_base_addr_index;
  3113. u32 trans_offset;
  3114. int rc;
  3115. rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
  3116. &cfg_base_addr_index, &cfg_offset);
  3117. if (rc)
  3118. return rc;
  3119. h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
  3120. cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
  3121. if (!h->cfgtable)
  3122. return -ENOMEM;
  3123. /* Find performant mode table. */
  3124. trans_offset = readl(&h->cfgtable->TransMethodOffset);
  3125. h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
  3126. cfg_base_addr_index)+cfg_offset+trans_offset,
  3127. sizeof(*h->transtable));
  3128. if (!h->transtable)
  3129. return -ENOMEM;
  3130. return 0;
  3131. }
  3132. static void __devinit hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
  3133. {
  3134. h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
  3135. if (h->max_commands < 16) {
  3136. dev_warn(&h->pdev->dev, "Controller reports "
  3137. "max supported commands of %d, an obvious lie. "
  3138. "Using 16. Ensure that firmware is up to date.\n",
  3139. h->max_commands);
  3140. h->max_commands = 16;
  3141. }
  3142. }
  3143. /* Interrogate the hardware for some limits:
  3144. * max commands, max SG elements without chaining, and with chaining,
  3145. * SG chain block size, etc.
  3146. */
  3147. static void __devinit hpsa_find_board_params(struct ctlr_info *h)
  3148. {
  3149. hpsa_get_max_perf_mode_cmds(h);
  3150. h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
  3151. h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
  3152. /*
  3153. * Limit in-command s/g elements to 32 save dma'able memory.
  3154. * Howvever spec says if 0, use 31
  3155. */
  3156. h->max_cmd_sg_entries = 31;
  3157. if (h->maxsgentries > 512) {
  3158. h->max_cmd_sg_entries = 32;
  3159. h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
  3160. h->maxsgentries--; /* save one for chain pointer */
  3161. } else {
  3162. h->maxsgentries = 31; /* default to traditional values */
  3163. h->chainsize = 0;
  3164. }
  3165. }
  3166. static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
  3167. {
  3168. if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
  3169. (readb(&h->cfgtable->Signature[1]) != 'I') ||
  3170. (readb(&h->cfgtable->Signature[2]) != 'S') ||
  3171. (readb(&h->cfgtable->Signature[3]) != 'S')) {
  3172. dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
  3173. return false;
  3174. }
  3175. return true;
  3176. }
  3177. /* Need to enable prefetch in the SCSI core for 6400 in x86 */
  3178. static inline void hpsa_enable_scsi_prefetch(struct ctlr_info *h)
  3179. {
  3180. #ifdef CONFIG_X86
  3181. u32 prefetch;
  3182. prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
  3183. prefetch |= 0x100;
  3184. writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
  3185. #endif
  3186. }
  3187. /* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
  3188. * in a prefetch beyond physical memory.
  3189. */
  3190. static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
  3191. {
  3192. u32 dma_prefetch;
  3193. if (h->board_id != 0x3225103C)
  3194. return;
  3195. dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
  3196. dma_prefetch |= 0x8000;
  3197. writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
  3198. }
  3199. static void __devinit hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
  3200. {
  3201. int i;
  3202. /* under certain very rare conditions, this can take awhile.
  3203. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  3204. * as we enter this code.)
  3205. */
  3206. for (i = 0; i < MAX_CONFIG_WAIT; i++) {
  3207. if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
  3208. break;
  3209. /* delay and try again */
  3210. msleep(10);
  3211. }
  3212. }
  3213. static int __devinit hpsa_enter_simple_mode(struct ctlr_info *h)
  3214. {
  3215. u32 trans_support;
  3216. trans_support = readl(&(h->cfgtable->TransportSupport));
  3217. if (!(trans_support & SIMPLE_MODE))
  3218. return -ENOTSUPP;
  3219. h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
  3220. /* Update the field, and then ring the doorbell */
  3221. writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
  3222. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  3223. hpsa_wait_for_mode_change_ack(h);
  3224. print_cfg_table(&h->pdev->dev, h->cfgtable);
  3225. if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
  3226. dev_warn(&h->pdev->dev,
  3227. "unable to get board into simple mode\n");
  3228. return -ENODEV;
  3229. }
  3230. return 0;
  3231. }
  3232. static int __devinit hpsa_pci_init(struct ctlr_info *h)
  3233. {
  3234. int prod_index, err;
  3235. prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
  3236. if (prod_index < 0)
  3237. return -ENODEV;
  3238. h->product_name = products[prod_index].product_name;
  3239. h->access = *(products[prod_index].access);
  3240. if (hpsa_board_disabled(h->pdev)) {
  3241. dev_warn(&h->pdev->dev, "controller appears to be disabled\n");
  3242. return -ENODEV;
  3243. }
  3244. err = pci_enable_device(h->pdev);
  3245. if (err) {
  3246. dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
  3247. return err;
  3248. }
  3249. err = pci_request_regions(h->pdev, "hpsa");
  3250. if (err) {
  3251. dev_err(&h->pdev->dev,
  3252. "cannot obtain PCI resources, aborting\n");
  3253. return err;
  3254. }
  3255. hpsa_interrupt_mode(h);
  3256. err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
  3257. if (err)
  3258. goto err_out_free_res;
  3259. h->vaddr = remap_pci_mem(h->paddr, 0x250);
  3260. if (!h->vaddr) {
  3261. err = -ENOMEM;
  3262. goto err_out_free_res;
  3263. }
  3264. err = hpsa_wait_for_board_ready(h);
  3265. if (err)
  3266. goto err_out_free_res;
  3267. err = hpsa_find_cfgtables(h);
  3268. if (err)
  3269. goto err_out_free_res;
  3270. hpsa_find_board_params(h);
  3271. if (!hpsa_CISS_signature_present(h)) {
  3272. err = -ENODEV;
  3273. goto err_out_free_res;
  3274. }
  3275. hpsa_enable_scsi_prefetch(h);
  3276. hpsa_p600_dma_prefetch_quirk(h);
  3277. err = hpsa_enter_simple_mode(h);
  3278. if (err)
  3279. goto err_out_free_res;
  3280. return 0;
  3281. err_out_free_res:
  3282. if (h->transtable)
  3283. iounmap(h->transtable);
  3284. if (h->cfgtable)
  3285. iounmap(h->cfgtable);
  3286. if (h->vaddr)
  3287. iounmap(h->vaddr);
  3288. /*
  3289. * Deliberately omit pci_disable_device(): it does something nasty to
  3290. * Smart Array controllers that pci_enable_device does not undo
  3291. */
  3292. pci_release_regions(h->pdev);
  3293. return err;
  3294. }
  3295. static void __devinit hpsa_hba_inquiry(struct ctlr_info *h)
  3296. {
  3297. int rc;
  3298. #define HBA_INQUIRY_BYTE_COUNT 64
  3299. h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
  3300. if (!h->hba_inquiry_data)
  3301. return;
  3302. rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
  3303. h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
  3304. if (rc != 0) {
  3305. kfree(h->hba_inquiry_data);
  3306. h->hba_inquiry_data = NULL;
  3307. }
  3308. }
  3309. static __devinit int hpsa_init_reset_devices(struct pci_dev *pdev)
  3310. {
  3311. int rc, i;
  3312. if (!reset_devices)
  3313. return 0;
  3314. /* Reset the controller with a PCI power-cycle or via doorbell */
  3315. rc = hpsa_kdump_hard_reset_controller(pdev);
  3316. /* -ENOTSUPP here means we cannot reset the controller
  3317. * but it's already (and still) up and running in
  3318. * "performant mode". Or, it might be 640x, which can't reset
  3319. * due to concerns about shared bbwc between 6402/6404 pair.
  3320. */
  3321. if (rc == -ENOTSUPP)
  3322. return 0; /* just try to do the kdump anyhow. */
  3323. if (rc)
  3324. return -ENODEV;
  3325. if (hpsa_reset_msi(pdev))
  3326. return -ENODEV;
  3327. /* Now try to get the controller to respond to a no-op */
  3328. for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
  3329. if (hpsa_noop(pdev) == 0)
  3330. break;
  3331. else
  3332. dev_warn(&pdev->dev, "no-op failed%s\n",
  3333. (i < 11 ? "; re-trying" : ""));
  3334. }
  3335. return 0;
  3336. }
  3337. static int __devinit hpsa_init_one(struct pci_dev *pdev,
  3338. const struct pci_device_id *ent)
  3339. {
  3340. int dac, rc;
  3341. struct ctlr_info *h;
  3342. if (number_of_controllers == 0)
  3343. printk(KERN_INFO DRIVER_NAME "\n");
  3344. rc = hpsa_init_reset_devices(pdev);
  3345. if (rc)
  3346. return rc;
  3347. /* Command structures must be aligned on a 32-byte boundary because
  3348. * the 5 lower bits of the address are used by the hardware. and by
  3349. * the driver. See comments in hpsa.h for more info.
  3350. */
  3351. #define COMMANDLIST_ALIGNMENT 32
  3352. BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
  3353. h = kzalloc(sizeof(*h), GFP_KERNEL);
  3354. if (!h)
  3355. return -ENOMEM;
  3356. h->pdev = pdev;
  3357. h->busy_initializing = 1;
  3358. INIT_HLIST_HEAD(&h->cmpQ);
  3359. INIT_HLIST_HEAD(&h->reqQ);
  3360. rc = hpsa_pci_init(h);
  3361. if (rc != 0)
  3362. goto clean1;
  3363. sprintf(h->devname, "hpsa%d", number_of_controllers);
  3364. h->ctlr = number_of_controllers;
  3365. number_of_controllers++;
  3366. /* configure PCI DMA stuff */
  3367. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  3368. if (rc == 0) {
  3369. dac = 1;
  3370. } else {
  3371. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3372. if (rc == 0) {
  3373. dac = 0;
  3374. } else {
  3375. dev_err(&pdev->dev, "no suitable DMA available\n");
  3376. goto clean1;
  3377. }
  3378. }
  3379. /* make sure the board interrupts are off */
  3380. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  3381. if (h->msix_vector || h->msi_vector)
  3382. rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr_msi,
  3383. IRQF_DISABLED, h->devname, h);
  3384. else
  3385. rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr_intx,
  3386. IRQF_DISABLED, h->devname, h);
  3387. if (rc) {
  3388. dev_err(&pdev->dev, "unable to get irq %d for %s\n",
  3389. h->intr[PERF_MODE_INT], h->devname);
  3390. goto clean2;
  3391. }
  3392. dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
  3393. h->devname, pdev->device,
  3394. h->intr[PERF_MODE_INT], dac ? "" : " not");
  3395. h->cmd_pool_bits =
  3396. kmalloc(((h->nr_cmds + BITS_PER_LONG -
  3397. 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
  3398. h->cmd_pool = pci_alloc_consistent(h->pdev,
  3399. h->nr_cmds * sizeof(*h->cmd_pool),
  3400. &(h->cmd_pool_dhandle));
  3401. h->errinfo_pool = pci_alloc_consistent(h->pdev,
  3402. h->nr_cmds * sizeof(*h->errinfo_pool),
  3403. &(h->errinfo_pool_dhandle));
  3404. if ((h->cmd_pool_bits == NULL)
  3405. || (h->cmd_pool == NULL)
  3406. || (h->errinfo_pool == NULL)) {
  3407. dev_err(&pdev->dev, "out of memory");
  3408. rc = -ENOMEM;
  3409. goto clean4;
  3410. }
  3411. if (hpsa_allocate_sg_chain_blocks(h))
  3412. goto clean4;
  3413. spin_lock_init(&h->lock);
  3414. spin_lock_init(&h->scan_lock);
  3415. init_waitqueue_head(&h->scan_wait_queue);
  3416. h->scan_finished = 1; /* no scan currently in progress */
  3417. pci_set_drvdata(pdev, h);
  3418. memset(h->cmd_pool_bits, 0,
  3419. ((h->nr_cmds + BITS_PER_LONG -
  3420. 1) / BITS_PER_LONG) * sizeof(unsigned long));
  3421. hpsa_scsi_setup(h);
  3422. /* Turn the interrupts on so we can service requests */
  3423. h->access.set_intr_mask(h, HPSA_INTR_ON);
  3424. hpsa_put_ctlr_into_performant_mode(h);
  3425. hpsa_hba_inquiry(h);
  3426. hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
  3427. h->busy_initializing = 0;
  3428. return 1;
  3429. clean4:
  3430. hpsa_free_sg_chain_blocks(h);
  3431. kfree(h->cmd_pool_bits);
  3432. if (h->cmd_pool)
  3433. pci_free_consistent(h->pdev,
  3434. h->nr_cmds * sizeof(struct CommandList),
  3435. h->cmd_pool, h->cmd_pool_dhandle);
  3436. if (h->errinfo_pool)
  3437. pci_free_consistent(h->pdev,
  3438. h->nr_cmds * sizeof(struct ErrorInfo),
  3439. h->errinfo_pool,
  3440. h->errinfo_pool_dhandle);
  3441. free_irq(h->intr[PERF_MODE_INT], h);
  3442. clean2:
  3443. clean1:
  3444. h->busy_initializing = 0;
  3445. kfree(h);
  3446. return rc;
  3447. }
  3448. static void hpsa_flush_cache(struct ctlr_info *h)
  3449. {
  3450. char *flush_buf;
  3451. struct CommandList *c;
  3452. flush_buf = kzalloc(4, GFP_KERNEL);
  3453. if (!flush_buf)
  3454. return;
  3455. c = cmd_special_alloc(h);
  3456. if (!c) {
  3457. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  3458. goto out_of_memory;
  3459. }
  3460. fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
  3461. RAID_CTLR_LUNID, TYPE_CMD);
  3462. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
  3463. if (c->err_info->CommandStatus != 0)
  3464. dev_warn(&h->pdev->dev,
  3465. "error flushing cache on controller\n");
  3466. cmd_special_free(h, c);
  3467. out_of_memory:
  3468. kfree(flush_buf);
  3469. }
  3470. static void hpsa_shutdown(struct pci_dev *pdev)
  3471. {
  3472. struct ctlr_info *h;
  3473. h = pci_get_drvdata(pdev);
  3474. /* Turn board interrupts off and send the flush cache command
  3475. * sendcmd will turn off interrupt, and send the flush...
  3476. * To write all data in the battery backed cache to disks
  3477. */
  3478. hpsa_flush_cache(h);
  3479. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  3480. free_irq(h->intr[PERF_MODE_INT], h);
  3481. #ifdef CONFIG_PCI_MSI
  3482. if (h->msix_vector)
  3483. pci_disable_msix(h->pdev);
  3484. else if (h->msi_vector)
  3485. pci_disable_msi(h->pdev);
  3486. #endif /* CONFIG_PCI_MSI */
  3487. }
  3488. static void __devexit hpsa_remove_one(struct pci_dev *pdev)
  3489. {
  3490. struct ctlr_info *h;
  3491. if (pci_get_drvdata(pdev) == NULL) {
  3492. dev_err(&pdev->dev, "unable to remove device \n");
  3493. return;
  3494. }
  3495. h = pci_get_drvdata(pdev);
  3496. hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
  3497. hpsa_shutdown(pdev);
  3498. iounmap(h->vaddr);
  3499. iounmap(h->transtable);
  3500. iounmap(h->cfgtable);
  3501. hpsa_free_sg_chain_blocks(h);
  3502. pci_free_consistent(h->pdev,
  3503. h->nr_cmds * sizeof(struct CommandList),
  3504. h->cmd_pool, h->cmd_pool_dhandle);
  3505. pci_free_consistent(h->pdev,
  3506. h->nr_cmds * sizeof(struct ErrorInfo),
  3507. h->errinfo_pool, h->errinfo_pool_dhandle);
  3508. pci_free_consistent(h->pdev, h->reply_pool_size,
  3509. h->reply_pool, h->reply_pool_dhandle);
  3510. kfree(h->cmd_pool_bits);
  3511. kfree(h->blockFetchTable);
  3512. kfree(h->hba_inquiry_data);
  3513. /*
  3514. * Deliberately omit pci_disable_device(): it does something nasty to
  3515. * Smart Array controllers that pci_enable_device does not undo
  3516. */
  3517. pci_release_regions(pdev);
  3518. pci_set_drvdata(pdev, NULL);
  3519. kfree(h);
  3520. }
  3521. static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
  3522. __attribute__((unused)) pm_message_t state)
  3523. {
  3524. return -ENOSYS;
  3525. }
  3526. static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
  3527. {
  3528. return -ENOSYS;
  3529. }
  3530. static struct pci_driver hpsa_pci_driver = {
  3531. .name = "hpsa",
  3532. .probe = hpsa_init_one,
  3533. .remove = __devexit_p(hpsa_remove_one),
  3534. .id_table = hpsa_pci_device_id, /* id_table */
  3535. .shutdown = hpsa_shutdown,
  3536. .suspend = hpsa_suspend,
  3537. .resume = hpsa_resume,
  3538. };
  3539. /* Fill in bucket_map[], given nsgs (the max number of
  3540. * scatter gather elements supported) and bucket[],
  3541. * which is an array of 8 integers. The bucket[] array
  3542. * contains 8 different DMA transfer sizes (in 16
  3543. * byte increments) which the controller uses to fetch
  3544. * commands. This function fills in bucket_map[], which
  3545. * maps a given number of scatter gather elements to one of
  3546. * the 8 DMA transfer sizes. The point of it is to allow the
  3547. * controller to only do as much DMA as needed to fetch the
  3548. * command, with the DMA transfer size encoded in the lower
  3549. * bits of the command address.
  3550. */
  3551. static void calc_bucket_map(int bucket[], int num_buckets,
  3552. int nsgs, int *bucket_map)
  3553. {
  3554. int i, j, b, size;
  3555. /* even a command with 0 SGs requires 4 blocks */
  3556. #define MINIMUM_TRANSFER_BLOCKS 4
  3557. #define NUM_BUCKETS 8
  3558. /* Note, bucket_map must have nsgs+1 entries. */
  3559. for (i = 0; i <= nsgs; i++) {
  3560. /* Compute size of a command with i SG entries */
  3561. size = i + MINIMUM_TRANSFER_BLOCKS;
  3562. b = num_buckets; /* Assume the biggest bucket */
  3563. /* Find the bucket that is just big enough */
  3564. for (j = 0; j < 8; j++) {
  3565. if (bucket[j] >= size) {
  3566. b = j;
  3567. break;
  3568. }
  3569. }
  3570. /* for a command with i SG entries, use bucket b. */
  3571. bucket_map[i] = b;
  3572. }
  3573. }
  3574. static __devinit void hpsa_enter_performant_mode(struct ctlr_info *h)
  3575. {
  3576. int i;
  3577. unsigned long register_value;
  3578. /* This is a bit complicated. There are 8 registers on
  3579. * the controller which we write to to tell it 8 different
  3580. * sizes of commands which there may be. It's a way of
  3581. * reducing the DMA done to fetch each command. Encoded into
  3582. * each command's tag are 3 bits which communicate to the controller
  3583. * which of the eight sizes that command fits within. The size of
  3584. * each command depends on how many scatter gather entries there are.
  3585. * Each SG entry requires 16 bytes. The eight registers are programmed
  3586. * with the number of 16-byte blocks a command of that size requires.
  3587. * The smallest command possible requires 5 such 16 byte blocks.
  3588. * the largest command possible requires MAXSGENTRIES + 4 16-byte
  3589. * blocks. Note, this only extends to the SG entries contained
  3590. * within the command block, and does not extend to chained blocks
  3591. * of SG elements. bft[] contains the eight values we write to
  3592. * the registers. They are not evenly distributed, but have more
  3593. * sizes for small commands, and fewer sizes for larger commands.
  3594. */
  3595. int bft[8] = {5, 6, 8, 10, 12, 20, 28, MAXSGENTRIES + 4};
  3596. BUILD_BUG_ON(28 > MAXSGENTRIES + 4);
  3597. /* 5 = 1 s/g entry or 4k
  3598. * 6 = 2 s/g entry or 8k
  3599. * 8 = 4 s/g entry or 16k
  3600. * 10 = 6 s/g entry or 24k
  3601. */
  3602. h->reply_pool_wraparound = 1; /* spec: init to 1 */
  3603. /* Controller spec: zero out this buffer. */
  3604. memset(h->reply_pool, 0, h->reply_pool_size);
  3605. h->reply_pool_head = h->reply_pool;
  3606. bft[7] = h->max_sg_entries + 4;
  3607. calc_bucket_map(bft, ARRAY_SIZE(bft), 32, h->blockFetchTable);
  3608. for (i = 0; i < 8; i++)
  3609. writel(bft[i], &h->transtable->BlockFetch[i]);
  3610. /* size of controller ring buffer */
  3611. writel(h->max_commands, &h->transtable->RepQSize);
  3612. writel(1, &h->transtable->RepQCount);
  3613. writel(0, &h->transtable->RepQCtrAddrLow32);
  3614. writel(0, &h->transtable->RepQCtrAddrHigh32);
  3615. writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32);
  3616. writel(0, &h->transtable->RepQAddr0High32);
  3617. writel(CFGTBL_Trans_Performant,
  3618. &(h->cfgtable->HostWrite.TransportRequest));
  3619. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  3620. hpsa_wait_for_mode_change_ack(h);
  3621. register_value = readl(&(h->cfgtable->TransportActive));
  3622. if (!(register_value & CFGTBL_Trans_Performant)) {
  3623. dev_warn(&h->pdev->dev, "unable to get board into"
  3624. " performant mode\n");
  3625. return;
  3626. }
  3627. }
  3628. static __devinit void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
  3629. {
  3630. u32 trans_support;
  3631. trans_support = readl(&(h->cfgtable->TransportSupport));
  3632. if (!(trans_support & PERFORMANT_MODE))
  3633. return;
  3634. hpsa_get_max_perf_mode_cmds(h);
  3635. h->max_sg_entries = 32;
  3636. /* Performant mode ring buffer and supporting data structures */
  3637. h->reply_pool_size = h->max_commands * sizeof(u64);
  3638. h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
  3639. &(h->reply_pool_dhandle));
  3640. /* Need a block fetch table for performant mode */
  3641. h->blockFetchTable = kmalloc(((h->max_sg_entries+1) *
  3642. sizeof(u32)), GFP_KERNEL);
  3643. if ((h->reply_pool == NULL)
  3644. || (h->blockFetchTable == NULL))
  3645. goto clean_up;
  3646. hpsa_enter_performant_mode(h);
  3647. /* Change the access methods to the performant access methods */
  3648. h->access = SA5_performant_access;
  3649. h->transMethod = CFGTBL_Trans_Performant;
  3650. return;
  3651. clean_up:
  3652. if (h->reply_pool)
  3653. pci_free_consistent(h->pdev, h->reply_pool_size,
  3654. h->reply_pool, h->reply_pool_dhandle);
  3655. kfree(h->blockFetchTable);
  3656. }
  3657. /*
  3658. * This is it. Register the PCI driver information for the cards we control
  3659. * the OS will call our registered routines when it finds one of our cards.
  3660. */
  3661. static int __init hpsa_init(void)
  3662. {
  3663. return pci_register_driver(&hpsa_pci_driver);
  3664. }
  3665. static void __exit hpsa_cleanup(void)
  3666. {
  3667. pci_unregister_driver(&hpsa_pci_driver);
  3668. }
  3669. module_init(hpsa_init);
  3670. module_exit(hpsa_cleanup);