hpsa.c 107 KB

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