hpsa.c 105 KB

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