hpsa.c 97 KB

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