cciss.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423
  1. /*
  2. * Disk Array driver for HP SA 5xxx and 6xxx Controllers
  3. * Copyright 2000, 2006 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; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  13. * NON INFRINGEMENT. See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * Questions/Comments/Bugfixes to iss_storagedev@hp.com
  20. *
  21. */
  22. #include <linux/config.h> /* CONFIG_PROC_FS */
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/types.h>
  26. #include <linux/pci.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/delay.h>
  30. #include <linux/major.h>
  31. #include <linux/fs.h>
  32. #include <linux/bio.h>
  33. #include <linux/blkpg.h>
  34. #include <linux/timer.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/init.h>
  37. #include <linux/hdreg.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/compat.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/io.h>
  42. #include <linux/dma-mapping.h>
  43. #include <linux/blkdev.h>
  44. #include <linux/genhd.h>
  45. #include <linux/completion.h>
  46. #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
  47. #define DRIVER_NAME "HP CISS Driver (v 2.6.10)"
  48. #define DRIVER_VERSION CCISS_DRIVER_VERSION(2,6,10)
  49. /* Embedded module documentation macros - see modules.h */
  50. MODULE_AUTHOR("Hewlett-Packard Company");
  51. MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 2.6.10");
  52. MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
  53. " SA6i P600 P800 P400 P400i E200 E200i");
  54. MODULE_LICENSE("GPL");
  55. #include "cciss_cmd.h"
  56. #include "cciss.h"
  57. #include <linux/cciss_ioctl.h>
  58. /* define the PCI info for the cards we can control */
  59. static const struct pci_device_id cciss_pci_device_id[] = {
  60. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS,
  61. 0x0E11, 0x4070, 0, 0, 0},
  62. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
  63. 0x0E11, 0x4080, 0, 0, 0},
  64. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
  65. 0x0E11, 0x4082, 0, 0, 0},
  66. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
  67. 0x0E11, 0x4083, 0, 0, 0},
  68. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
  69. 0x0E11, 0x409A, 0, 0, 0},
  70. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
  71. 0x0E11, 0x409B, 0, 0, 0},
  72. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
  73. 0x0E11, 0x409C, 0, 0, 0},
  74. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
  75. 0x0E11, 0x409D, 0, 0, 0},
  76. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
  77. 0x0E11, 0x4091, 0, 0, 0},
  78. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA,
  79. 0x103C, 0x3225, 0, 0, 0},
  80. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC,
  81. 0x103c, 0x3223, 0, 0, 0},
  82. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC,
  83. 0x103c, 0x3234, 0, 0, 0},
  84. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC,
  85. 0x103c, 0x3235, 0, 0, 0},
  86. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
  87. 0x103c, 0x3211, 0, 0, 0},
  88. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
  89. 0x103c, 0x3212, 0, 0, 0},
  90. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
  91. 0x103c, 0x3213, 0, 0, 0},
  92. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
  93. 0x103c, 0x3214, 0, 0, 0},
  94. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
  95. 0x103c, 0x3215, 0, 0, 0},
  96. {0,}
  97. };
  98. MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
  99. #define NR_PRODUCTS ARRAY_SIZE(products)
  100. /* board_id = Subsystem Device ID & Vendor ID
  101. * product = Marketing Name for the board
  102. * access = Address of the struct of function pointers
  103. */
  104. static struct board_type products[] = {
  105. { 0x40700E11, "Smart Array 5300", &SA5_access },
  106. { 0x40800E11, "Smart Array 5i", &SA5B_access},
  107. { 0x40820E11, "Smart Array 532", &SA5B_access},
  108. { 0x40830E11, "Smart Array 5312", &SA5B_access},
  109. { 0x409A0E11, "Smart Array 641", &SA5_access},
  110. { 0x409B0E11, "Smart Array 642", &SA5_access},
  111. { 0x409C0E11, "Smart Array 6400", &SA5_access},
  112. { 0x409D0E11, "Smart Array 6400 EM", &SA5_access},
  113. { 0x40910E11, "Smart Array 6i", &SA5_access},
  114. { 0x3225103C, "Smart Array P600", &SA5_access},
  115. { 0x3223103C, "Smart Array P800", &SA5_access},
  116. { 0x3234103C, "Smart Array P400", &SA5_access},
  117. { 0x3235103C, "Smart Array P400i", &SA5_access},
  118. { 0x3211103C, "Smart Array E200i", &SA5_access},
  119. { 0x3212103C, "Smart Array E200", &SA5_access},
  120. { 0x3213103C, "Smart Array E200i", &SA5_access},
  121. { 0x3214103C, "Smart Array E200i", &SA5_access},
  122. { 0x3215103C, "Smart Array E200i", &SA5_access},
  123. };
  124. /* How long to wait (in millesconds) for board to go into simple mode */
  125. #define MAX_CONFIG_WAIT 30000
  126. #define MAX_IOCTL_CONFIG_WAIT 1000
  127. /*define how many times we will try a command because of bus resets */
  128. #define MAX_CMD_RETRIES 3
  129. #define READ_AHEAD 1024
  130. #define NR_CMDS 384 /* #commands that can be outstanding */
  131. #define MAX_CTLR 32
  132. /* Originally cciss driver only supports 8 major numbers */
  133. #define MAX_CTLR_ORIG 8
  134. static ctlr_info_t *hba[MAX_CTLR];
  135. static void do_cciss_request(request_queue_t *q);
  136. static irqreturn_t do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs);
  137. static int cciss_open(struct inode *inode, struct file *filep);
  138. static int cciss_release(struct inode *inode, struct file *filep);
  139. static int cciss_ioctl(struct inode *inode, struct file *filep,
  140. unsigned int cmd, unsigned long arg);
  141. static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo);
  142. static int revalidate_allvol(ctlr_info_t *host);
  143. static int cciss_revalidate(struct gendisk *disk);
  144. static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk);
  145. static int deregister_disk(struct gendisk *disk, drive_info_struct *drv, int clear_all);
  146. static void cciss_read_capacity(int ctlr, int logvol, ReadCapdata_struct *buf,
  147. int withirq, unsigned int *total_size, unsigned int *block_size);
  148. static void cciss_geometry_inquiry(int ctlr, int logvol,
  149. int withirq, unsigned int total_size,
  150. unsigned int block_size, InquiryData_struct *inq_buff,
  151. drive_info_struct *drv);
  152. static void cciss_getgeometry(int cntl_num);
  153. static void __devinit cciss_interrupt_mode(ctlr_info_t *, struct pci_dev *, __u32);
  154. static void start_io( ctlr_info_t *h);
  155. static int sendcmd( __u8 cmd, int ctlr, void *buff, size_t size,
  156. unsigned int use_unit_num, unsigned int log_unit, __u8 page_code,
  157. unsigned char *scsi3addr, int cmd_type);
  158. static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
  159. unsigned int use_unit_num, unsigned int log_unit, __u8 page_code,
  160. int cmd_type);
  161. static void fail_all_cmds(unsigned long ctlr);
  162. #ifdef CONFIG_PROC_FS
  163. static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
  164. int length, int *eof, void *data);
  165. static void cciss_procinit(int i);
  166. #else
  167. static void cciss_procinit(int i) {}
  168. #endif /* CONFIG_PROC_FS */
  169. #ifdef CONFIG_COMPAT
  170. static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg);
  171. #endif
  172. static struct block_device_operations cciss_fops = {
  173. .owner = THIS_MODULE,
  174. .open = cciss_open,
  175. .release = cciss_release,
  176. .ioctl = cciss_ioctl,
  177. .getgeo = cciss_getgeo,
  178. #ifdef CONFIG_COMPAT
  179. .compat_ioctl = cciss_compat_ioctl,
  180. #endif
  181. .revalidate_disk= cciss_revalidate,
  182. };
  183. /*
  184. * Enqueuing and dequeuing functions for cmdlists.
  185. */
  186. static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
  187. {
  188. if (*Qptr == NULL) {
  189. *Qptr = c;
  190. c->next = c->prev = c;
  191. } else {
  192. c->prev = (*Qptr)->prev;
  193. c->next = (*Qptr);
  194. (*Qptr)->prev->next = c;
  195. (*Qptr)->prev = c;
  196. }
  197. }
  198. static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
  199. CommandList_struct *c)
  200. {
  201. if (c && c->next != c) {
  202. if (*Qptr == c) *Qptr = c->next;
  203. c->prev->next = c->next;
  204. c->next->prev = c->prev;
  205. } else {
  206. *Qptr = NULL;
  207. }
  208. return c;
  209. }
  210. #include "cciss_scsi.c" /* For SCSI tape support */
  211. #ifdef CONFIG_PROC_FS
  212. /*
  213. * Report information about this controller.
  214. */
  215. #define ENG_GIG 1000000000
  216. #define ENG_GIG_FACTOR (ENG_GIG/512)
  217. #define RAID_UNKNOWN 6
  218. static const char *raid_label[] = {"0","4","1(1+0)","5","5+1","ADG",
  219. "UNKNOWN"};
  220. static struct proc_dir_entry *proc_cciss;
  221. static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
  222. int length, int *eof, void *data)
  223. {
  224. off_t pos = 0;
  225. off_t len = 0;
  226. int size, i, ctlr;
  227. ctlr_info_t *h = (ctlr_info_t*)data;
  228. drive_info_struct *drv;
  229. unsigned long flags;
  230. sector_t vol_sz, vol_sz_frac;
  231. ctlr = h->ctlr;
  232. /* prevent displaying bogus info during configuration
  233. * or deconfiguration of a logical volume
  234. */
  235. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  236. if (h->busy_configuring) {
  237. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  238. return -EBUSY;
  239. }
  240. h->busy_configuring = 1;
  241. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  242. size = sprintf(buffer, "%s: HP %s Controller\n"
  243. "Board ID: 0x%08lx\n"
  244. "Firmware Version: %c%c%c%c\n"
  245. "IRQ: %d\n"
  246. "Logical drives: %d\n"
  247. "Current Q depth: %d\n"
  248. "Current # commands on controller: %d\n"
  249. "Max Q depth since init: %d\n"
  250. "Max # commands on controller since init: %d\n"
  251. "Max SG entries since init: %d\n\n",
  252. h->devname,
  253. h->product_name,
  254. (unsigned long)h->board_id,
  255. h->firm_ver[0], h->firm_ver[1], h->firm_ver[2], h->firm_ver[3],
  256. (unsigned int)h->intr[SIMPLE_MODE_INT],
  257. h->num_luns,
  258. h->Qdepth, h->commands_outstanding,
  259. h->maxQsinceinit, h->max_outstanding, h->maxSG);
  260. pos += size; len += size;
  261. cciss_proc_tape_report(ctlr, buffer, &pos, &len);
  262. for(i=0; i<=h->highest_lun; i++) {
  263. drv = &h->drv[i];
  264. if (drv->heads == 0)
  265. continue;
  266. vol_sz = drv->nr_blocks;
  267. vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
  268. vol_sz_frac *= 100;
  269. sector_div(vol_sz_frac, ENG_GIG_FACTOR);
  270. if (drv->raid_level > 5)
  271. drv->raid_level = RAID_UNKNOWN;
  272. size = sprintf(buffer+len, "cciss/c%dd%d:"
  273. "\t%4u.%02uGB\tRAID %s\n",
  274. ctlr, i, (int)vol_sz, (int)vol_sz_frac,
  275. raid_label[drv->raid_level]);
  276. pos += size; len += size;
  277. }
  278. *eof = 1;
  279. *start = buffer+offset;
  280. len -= offset;
  281. if (len>length)
  282. len = length;
  283. h->busy_configuring = 0;
  284. return len;
  285. }
  286. static int
  287. cciss_proc_write(struct file *file, const char __user *buffer,
  288. unsigned long count, void *data)
  289. {
  290. unsigned char cmd[80];
  291. int len;
  292. #ifdef CONFIG_CISS_SCSI_TAPE
  293. ctlr_info_t *h = (ctlr_info_t *) data;
  294. int rc;
  295. #endif
  296. if (count > sizeof(cmd)-1) return -EINVAL;
  297. if (copy_from_user(cmd, buffer, count)) return -EFAULT;
  298. cmd[count] = '\0';
  299. len = strlen(cmd); // above 3 lines ensure safety
  300. if (len && cmd[len-1] == '\n')
  301. cmd[--len] = '\0';
  302. # ifdef CONFIG_CISS_SCSI_TAPE
  303. if (strcmp("engage scsi", cmd)==0) {
  304. rc = cciss_engage_scsi(h->ctlr);
  305. if (rc != 0) return -rc;
  306. return count;
  307. }
  308. /* might be nice to have "disengage" too, but it's not
  309. safely possible. (only 1 module use count, lock issues.) */
  310. # endif
  311. return -EINVAL;
  312. }
  313. /*
  314. * Get us a file in /proc/cciss that says something about each controller.
  315. * Create /proc/cciss if it doesn't exist yet.
  316. */
  317. static void __devinit cciss_procinit(int i)
  318. {
  319. struct proc_dir_entry *pde;
  320. if (proc_cciss == NULL) {
  321. proc_cciss = proc_mkdir("cciss", proc_root_driver);
  322. if (!proc_cciss)
  323. return;
  324. }
  325. pde = create_proc_read_entry(hba[i]->devname,
  326. S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
  327. proc_cciss, cciss_proc_get_info, hba[i]);
  328. pde->write_proc = cciss_proc_write;
  329. }
  330. #endif /* CONFIG_PROC_FS */
  331. /*
  332. * For operations that cannot sleep, a command block is allocated at init,
  333. * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
  334. * which ones are free or in use. For operations that can wait for kmalloc
  335. * to possible sleep, this routine can be called with get_from_pool set to 0.
  336. * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
  337. */
  338. static CommandList_struct * cmd_alloc(ctlr_info_t *h, int get_from_pool)
  339. {
  340. CommandList_struct *c;
  341. int i;
  342. u64bit temp64;
  343. dma_addr_t cmd_dma_handle, err_dma_handle;
  344. if (!get_from_pool)
  345. {
  346. c = (CommandList_struct *) pci_alloc_consistent(
  347. h->pdev, sizeof(CommandList_struct), &cmd_dma_handle);
  348. if(c==NULL)
  349. return NULL;
  350. memset(c, 0, sizeof(CommandList_struct));
  351. c->cmdindex = -1;
  352. c->err_info = (ErrorInfo_struct *)pci_alloc_consistent(
  353. h->pdev, sizeof(ErrorInfo_struct),
  354. &err_dma_handle);
  355. if (c->err_info == NULL)
  356. {
  357. pci_free_consistent(h->pdev,
  358. sizeof(CommandList_struct), c, cmd_dma_handle);
  359. return NULL;
  360. }
  361. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  362. } else /* get it out of the controllers pool */
  363. {
  364. do {
  365. i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS);
  366. if (i == NR_CMDS)
  367. return NULL;
  368. } while(test_and_set_bit(i & (BITS_PER_LONG - 1), h->cmd_pool_bits+(i/BITS_PER_LONG)) != 0);
  369. #ifdef CCISS_DEBUG
  370. printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
  371. #endif
  372. c = h->cmd_pool + i;
  373. memset(c, 0, sizeof(CommandList_struct));
  374. cmd_dma_handle = h->cmd_pool_dhandle
  375. + i*sizeof(CommandList_struct);
  376. c->err_info = h->errinfo_pool + i;
  377. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  378. err_dma_handle = h->errinfo_pool_dhandle
  379. + i*sizeof(ErrorInfo_struct);
  380. h->nr_allocs++;
  381. c->cmdindex = i;
  382. }
  383. c->busaddr = (__u32) cmd_dma_handle;
  384. temp64.val = (__u64) err_dma_handle;
  385. c->ErrDesc.Addr.lower = temp64.val32.lower;
  386. c->ErrDesc.Addr.upper = temp64.val32.upper;
  387. c->ErrDesc.Len = sizeof(ErrorInfo_struct);
  388. c->ctlr = h->ctlr;
  389. return c;
  390. }
  391. /*
  392. * Frees a command block that was previously allocated with cmd_alloc().
  393. */
  394. static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
  395. {
  396. int i;
  397. u64bit temp64;
  398. if( !got_from_pool)
  399. {
  400. temp64.val32.lower = c->ErrDesc.Addr.lower;
  401. temp64.val32.upper = c->ErrDesc.Addr.upper;
  402. pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
  403. c->err_info, (dma_addr_t) temp64.val);
  404. pci_free_consistent(h->pdev, sizeof(CommandList_struct),
  405. c, (dma_addr_t) c->busaddr);
  406. } else
  407. {
  408. i = c - h->cmd_pool;
  409. clear_bit(i&(BITS_PER_LONG-1), h->cmd_pool_bits+(i/BITS_PER_LONG));
  410. h->nr_frees++;
  411. }
  412. }
  413. static inline ctlr_info_t *get_host(struct gendisk *disk)
  414. {
  415. return disk->queue->queuedata;
  416. }
  417. static inline drive_info_struct *get_drv(struct gendisk *disk)
  418. {
  419. return disk->private_data;
  420. }
  421. /*
  422. * Open. Make sure the device is really there.
  423. */
  424. static int cciss_open(struct inode *inode, struct file *filep)
  425. {
  426. ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
  427. drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
  428. #ifdef CCISS_DEBUG
  429. printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
  430. #endif /* CCISS_DEBUG */
  431. if (host->busy_initializing || drv->busy_configuring)
  432. return -EBUSY;
  433. /*
  434. * Root is allowed to open raw volume zero even if it's not configured
  435. * so array config can still work. Root is also allowed to open any
  436. * volume that has a LUN ID, so it can issue IOCTL to reread the
  437. * disk information. I don't think I really like this
  438. * but I'm already using way to many device nodes to claim another one
  439. * for "raw controller".
  440. */
  441. if (drv->nr_blocks == 0) {
  442. if (iminor(inode) != 0) { /* not node 0? */
  443. /* if not node 0 make sure it is a partition = 0 */
  444. if (iminor(inode) & 0x0f) {
  445. return -ENXIO;
  446. /* if it is, make sure we have a LUN ID */
  447. } else if (drv->LunID == 0) {
  448. return -ENXIO;
  449. }
  450. }
  451. if (!capable(CAP_SYS_ADMIN))
  452. return -EPERM;
  453. }
  454. drv->usage_count++;
  455. host->usage_count++;
  456. return 0;
  457. }
  458. /*
  459. * Close. Sync first.
  460. */
  461. static int cciss_release(struct inode *inode, struct file *filep)
  462. {
  463. ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
  464. drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
  465. #ifdef CCISS_DEBUG
  466. printk(KERN_DEBUG "cciss_release %s\n", inode->i_bdev->bd_disk->disk_name);
  467. #endif /* CCISS_DEBUG */
  468. drv->usage_count--;
  469. host->usage_count--;
  470. return 0;
  471. }
  472. #ifdef CONFIG_COMPAT
  473. static int do_ioctl(struct file *f, unsigned cmd, unsigned long arg)
  474. {
  475. int ret;
  476. lock_kernel();
  477. ret = cciss_ioctl(f->f_dentry->d_inode, f, cmd, arg);
  478. unlock_kernel();
  479. return ret;
  480. }
  481. static int cciss_ioctl32_passthru(struct file *f, unsigned cmd, unsigned long arg);
  482. static int cciss_ioctl32_big_passthru(struct file *f, unsigned cmd, unsigned long arg);
  483. static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg)
  484. {
  485. switch (cmd) {
  486. case CCISS_GETPCIINFO:
  487. case CCISS_GETINTINFO:
  488. case CCISS_SETINTINFO:
  489. case CCISS_GETNODENAME:
  490. case CCISS_SETNODENAME:
  491. case CCISS_GETHEARTBEAT:
  492. case CCISS_GETBUSTYPES:
  493. case CCISS_GETFIRMVER:
  494. case CCISS_GETDRIVVER:
  495. case CCISS_REVALIDVOLS:
  496. case CCISS_DEREGDISK:
  497. case CCISS_REGNEWDISK:
  498. case CCISS_REGNEWD:
  499. case CCISS_RESCANDISK:
  500. case CCISS_GETLUNINFO:
  501. return do_ioctl(f, cmd, arg);
  502. case CCISS_PASSTHRU32:
  503. return cciss_ioctl32_passthru(f, cmd, arg);
  504. case CCISS_BIG_PASSTHRU32:
  505. return cciss_ioctl32_big_passthru(f, cmd, arg);
  506. default:
  507. return -ENOIOCTLCMD;
  508. }
  509. }
  510. static int cciss_ioctl32_passthru(struct file *f, unsigned cmd, unsigned long arg)
  511. {
  512. IOCTL32_Command_struct __user *arg32 =
  513. (IOCTL32_Command_struct __user *) arg;
  514. IOCTL_Command_struct arg64;
  515. IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  516. int err;
  517. u32 cp;
  518. err = 0;
  519. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
  520. err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
  521. err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
  522. err |= get_user(arg64.buf_size, &arg32->buf_size);
  523. err |= get_user(cp, &arg32->buf);
  524. arg64.buf = compat_ptr(cp);
  525. err |= copy_to_user(p, &arg64, sizeof(arg64));
  526. if (err)
  527. return -EFAULT;
  528. err = do_ioctl(f, CCISS_PASSTHRU, (unsigned long) p);
  529. if (err)
  530. return err;
  531. err |= copy_in_user(&arg32->error_info, &p->error_info, sizeof(arg32->error_info));
  532. if (err)
  533. return -EFAULT;
  534. return err;
  535. }
  536. static int cciss_ioctl32_big_passthru(struct file *file, unsigned cmd, unsigned long arg)
  537. {
  538. BIG_IOCTL32_Command_struct __user *arg32 =
  539. (BIG_IOCTL32_Command_struct __user *) arg;
  540. BIG_IOCTL_Command_struct arg64;
  541. BIG_IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  542. int err;
  543. u32 cp;
  544. err = 0;
  545. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
  546. err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
  547. err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
  548. err |= get_user(arg64.buf_size, &arg32->buf_size);
  549. err |= get_user(arg64.malloc_size, &arg32->malloc_size);
  550. err |= get_user(cp, &arg32->buf);
  551. arg64.buf = compat_ptr(cp);
  552. err |= copy_to_user(p, &arg64, sizeof(arg64));
  553. if (err)
  554. return -EFAULT;
  555. err = do_ioctl(file, CCISS_BIG_PASSTHRU, (unsigned long) p);
  556. if (err)
  557. return err;
  558. err |= copy_in_user(&arg32->error_info, &p->error_info, sizeof(arg32->error_info));
  559. if (err)
  560. return -EFAULT;
  561. return err;
  562. }
  563. #endif
  564. static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  565. {
  566. drive_info_struct *drv = get_drv(bdev->bd_disk);
  567. if (!drv->cylinders)
  568. return -ENXIO;
  569. geo->heads = drv->heads;
  570. geo->sectors = drv->sectors;
  571. geo->cylinders = drv->cylinders;
  572. return 0;
  573. }
  574. /*
  575. * ioctl
  576. */
  577. static int cciss_ioctl(struct inode *inode, struct file *filep,
  578. unsigned int cmd, unsigned long arg)
  579. {
  580. struct block_device *bdev = inode->i_bdev;
  581. struct gendisk *disk = bdev->bd_disk;
  582. ctlr_info_t *host = get_host(disk);
  583. drive_info_struct *drv = get_drv(disk);
  584. int ctlr = host->ctlr;
  585. void __user *argp = (void __user *)arg;
  586. #ifdef CCISS_DEBUG
  587. printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
  588. #endif /* CCISS_DEBUG */
  589. switch(cmd) {
  590. case CCISS_GETPCIINFO:
  591. {
  592. cciss_pci_info_struct pciinfo;
  593. if (!arg) return -EINVAL;
  594. pciinfo.domain = pci_domain_nr(host->pdev->bus);
  595. pciinfo.bus = host->pdev->bus->number;
  596. pciinfo.dev_fn = host->pdev->devfn;
  597. pciinfo.board_id = host->board_id;
  598. if (copy_to_user(argp, &pciinfo, sizeof( cciss_pci_info_struct )))
  599. return -EFAULT;
  600. return(0);
  601. }
  602. case CCISS_GETINTINFO:
  603. {
  604. cciss_coalint_struct intinfo;
  605. if (!arg) return -EINVAL;
  606. intinfo.delay = readl(&host->cfgtable->HostWrite.CoalIntDelay);
  607. intinfo.count = readl(&host->cfgtable->HostWrite.CoalIntCount);
  608. if (copy_to_user(argp, &intinfo, sizeof( cciss_coalint_struct )))
  609. return -EFAULT;
  610. return(0);
  611. }
  612. case CCISS_SETINTINFO:
  613. {
  614. cciss_coalint_struct intinfo;
  615. unsigned long flags;
  616. int i;
  617. if (!arg) return -EINVAL;
  618. if (!capable(CAP_SYS_ADMIN)) return -EPERM;
  619. if (copy_from_user(&intinfo, argp, sizeof( cciss_coalint_struct)))
  620. return -EFAULT;
  621. if ( (intinfo.delay == 0 ) && (intinfo.count == 0))
  622. {
  623. // printk("cciss_ioctl: delay and count cannot be 0\n");
  624. return( -EINVAL);
  625. }
  626. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  627. /* Update the field, and then ring the doorbell */
  628. writel( intinfo.delay,
  629. &(host->cfgtable->HostWrite.CoalIntDelay));
  630. writel( intinfo.count,
  631. &(host->cfgtable->HostWrite.CoalIntCount));
  632. writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
  633. for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
  634. if (!(readl(host->vaddr + SA5_DOORBELL)
  635. & CFGTBL_ChangeReq))
  636. break;
  637. /* delay and try again */
  638. udelay(1000);
  639. }
  640. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  641. if (i >= MAX_IOCTL_CONFIG_WAIT)
  642. return -EAGAIN;
  643. return(0);
  644. }
  645. case CCISS_GETNODENAME:
  646. {
  647. NodeName_type NodeName;
  648. int i;
  649. if (!arg) return -EINVAL;
  650. for(i=0;i<16;i++)
  651. NodeName[i] = readb(&host->cfgtable->ServerName[i]);
  652. if (copy_to_user(argp, NodeName, sizeof( NodeName_type)))
  653. return -EFAULT;
  654. return(0);
  655. }
  656. case CCISS_SETNODENAME:
  657. {
  658. NodeName_type NodeName;
  659. unsigned long flags;
  660. int i;
  661. if (!arg) return -EINVAL;
  662. if (!capable(CAP_SYS_ADMIN)) return -EPERM;
  663. if (copy_from_user(NodeName, argp, sizeof( NodeName_type)))
  664. return -EFAULT;
  665. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  666. /* Update the field, and then ring the doorbell */
  667. for(i=0;i<16;i++)
  668. writeb( NodeName[i], &host->cfgtable->ServerName[i]);
  669. writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
  670. for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
  671. if (!(readl(host->vaddr + SA5_DOORBELL)
  672. & CFGTBL_ChangeReq))
  673. break;
  674. /* delay and try again */
  675. udelay(1000);
  676. }
  677. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  678. if (i >= MAX_IOCTL_CONFIG_WAIT)
  679. return -EAGAIN;
  680. return(0);
  681. }
  682. case CCISS_GETHEARTBEAT:
  683. {
  684. Heartbeat_type heartbeat;
  685. if (!arg) return -EINVAL;
  686. heartbeat = readl(&host->cfgtable->HeartBeat);
  687. if (copy_to_user(argp, &heartbeat, sizeof( Heartbeat_type)))
  688. return -EFAULT;
  689. return(0);
  690. }
  691. case CCISS_GETBUSTYPES:
  692. {
  693. BusTypes_type BusTypes;
  694. if (!arg) return -EINVAL;
  695. BusTypes = readl(&host->cfgtable->BusTypes);
  696. if (copy_to_user(argp, &BusTypes, sizeof( BusTypes_type) ))
  697. return -EFAULT;
  698. return(0);
  699. }
  700. case CCISS_GETFIRMVER:
  701. {
  702. FirmwareVer_type firmware;
  703. if (!arg) return -EINVAL;
  704. memcpy(firmware, host->firm_ver, 4);
  705. if (copy_to_user(argp, firmware, sizeof( FirmwareVer_type)))
  706. return -EFAULT;
  707. return(0);
  708. }
  709. case CCISS_GETDRIVVER:
  710. {
  711. DriverVer_type DriverVer = DRIVER_VERSION;
  712. if (!arg) return -EINVAL;
  713. if (copy_to_user(argp, &DriverVer, sizeof( DriverVer_type) ))
  714. return -EFAULT;
  715. return(0);
  716. }
  717. case CCISS_REVALIDVOLS:
  718. if (bdev != bdev->bd_contains || drv != host->drv)
  719. return -ENXIO;
  720. return revalidate_allvol(host);
  721. case CCISS_GETLUNINFO: {
  722. LogvolInfo_struct luninfo;
  723. luninfo.LunID = drv->LunID;
  724. luninfo.num_opens = drv->usage_count;
  725. luninfo.num_parts = 0;
  726. if (copy_to_user(argp, &luninfo,
  727. sizeof(LogvolInfo_struct)))
  728. return -EFAULT;
  729. return(0);
  730. }
  731. case CCISS_DEREGDISK:
  732. return rebuild_lun_table(host, disk);
  733. case CCISS_REGNEWD:
  734. return rebuild_lun_table(host, NULL);
  735. case CCISS_PASSTHRU:
  736. {
  737. IOCTL_Command_struct iocommand;
  738. CommandList_struct *c;
  739. char *buff = NULL;
  740. u64bit temp64;
  741. unsigned long flags;
  742. DECLARE_COMPLETION(wait);
  743. if (!arg) return -EINVAL;
  744. if (!capable(CAP_SYS_RAWIO)) return -EPERM;
  745. if (copy_from_user(&iocommand, argp, sizeof( IOCTL_Command_struct) ))
  746. return -EFAULT;
  747. if((iocommand.buf_size < 1) &&
  748. (iocommand.Request.Type.Direction != XFER_NONE))
  749. {
  750. return -EINVAL;
  751. }
  752. #if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
  753. /* Check kmalloc limits */
  754. if(iocommand.buf_size > 128000)
  755. return -EINVAL;
  756. #endif
  757. if(iocommand.buf_size > 0)
  758. {
  759. buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
  760. if( buff == NULL)
  761. return -EFAULT;
  762. }
  763. if (iocommand.Request.Type.Direction == XFER_WRITE)
  764. {
  765. /* Copy the data into the buffer we created */
  766. if (copy_from_user(buff, iocommand.buf, iocommand.buf_size))
  767. {
  768. kfree(buff);
  769. return -EFAULT;
  770. }
  771. } else {
  772. memset(buff, 0, iocommand.buf_size);
  773. }
  774. if ((c = cmd_alloc(host , 0)) == NULL)
  775. {
  776. kfree(buff);
  777. return -ENOMEM;
  778. }
  779. // Fill in the command type
  780. c->cmd_type = CMD_IOCTL_PEND;
  781. // Fill in Command Header
  782. c->Header.ReplyQueue = 0; // unused in simple mode
  783. if( iocommand.buf_size > 0) // buffer to fill
  784. {
  785. c->Header.SGList = 1;
  786. c->Header.SGTotal= 1;
  787. } else // no buffers to fill
  788. {
  789. c->Header.SGList = 0;
  790. c->Header.SGTotal= 0;
  791. }
  792. c->Header.LUN = iocommand.LUN_info;
  793. c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
  794. // Fill in Request block
  795. c->Request = iocommand.Request;
  796. // Fill in the scatter gather information
  797. if (iocommand.buf_size > 0 )
  798. {
  799. temp64.val = pci_map_single( host->pdev, buff,
  800. iocommand.buf_size,
  801. PCI_DMA_BIDIRECTIONAL);
  802. c->SG[0].Addr.lower = temp64.val32.lower;
  803. c->SG[0].Addr.upper = temp64.val32.upper;
  804. c->SG[0].Len = iocommand.buf_size;
  805. c->SG[0].Ext = 0; // we are not chaining
  806. }
  807. c->waiting = &wait;
  808. /* Put the request on the tail of the request queue */
  809. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  810. addQ(&host->reqQ, c);
  811. host->Qdepth++;
  812. start_io(host);
  813. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  814. wait_for_completion(&wait);
  815. /* unlock the buffers from DMA */
  816. temp64.val32.lower = c->SG[0].Addr.lower;
  817. temp64.val32.upper = c->SG[0].Addr.upper;
  818. pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
  819. iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
  820. /* Copy the error information out */
  821. iocommand.error_info = *(c->err_info);
  822. if ( copy_to_user(argp, &iocommand, sizeof( IOCTL_Command_struct) ) )
  823. {
  824. kfree(buff);
  825. cmd_free(host, c, 0);
  826. return( -EFAULT);
  827. }
  828. if (iocommand.Request.Type.Direction == XFER_READ)
  829. {
  830. /* Copy the data out of the buffer we created */
  831. if (copy_to_user(iocommand.buf, buff, iocommand.buf_size))
  832. {
  833. kfree(buff);
  834. cmd_free(host, c, 0);
  835. return -EFAULT;
  836. }
  837. }
  838. kfree(buff);
  839. cmd_free(host, c, 0);
  840. return(0);
  841. }
  842. case CCISS_BIG_PASSTHRU: {
  843. BIG_IOCTL_Command_struct *ioc;
  844. CommandList_struct *c;
  845. unsigned char **buff = NULL;
  846. int *buff_size = NULL;
  847. u64bit temp64;
  848. unsigned long flags;
  849. BYTE sg_used = 0;
  850. int status = 0;
  851. int i;
  852. DECLARE_COMPLETION(wait);
  853. __u32 left;
  854. __u32 sz;
  855. BYTE __user *data_ptr;
  856. if (!arg)
  857. return -EINVAL;
  858. if (!capable(CAP_SYS_RAWIO))
  859. return -EPERM;
  860. ioc = (BIG_IOCTL_Command_struct *)
  861. kmalloc(sizeof(*ioc), GFP_KERNEL);
  862. if (!ioc) {
  863. status = -ENOMEM;
  864. goto cleanup1;
  865. }
  866. if (copy_from_user(ioc, argp, sizeof(*ioc))) {
  867. status = -EFAULT;
  868. goto cleanup1;
  869. }
  870. if ((ioc->buf_size < 1) &&
  871. (ioc->Request.Type.Direction != XFER_NONE)) {
  872. status = -EINVAL;
  873. goto cleanup1;
  874. }
  875. /* Check kmalloc limits using all SGs */
  876. if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
  877. status = -EINVAL;
  878. goto cleanup1;
  879. }
  880. if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
  881. status = -EINVAL;
  882. goto cleanup1;
  883. }
  884. buff = (unsigned char **) kmalloc(MAXSGENTRIES *
  885. sizeof(char *), GFP_KERNEL);
  886. if (!buff) {
  887. status = -ENOMEM;
  888. goto cleanup1;
  889. }
  890. memset(buff, 0, MAXSGENTRIES);
  891. buff_size = (int *) kmalloc(MAXSGENTRIES * sizeof(int),
  892. GFP_KERNEL);
  893. if (!buff_size) {
  894. status = -ENOMEM;
  895. goto cleanup1;
  896. }
  897. left = ioc->buf_size;
  898. data_ptr = ioc->buf;
  899. while (left) {
  900. sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
  901. buff_size[sg_used] = sz;
  902. buff[sg_used] = kmalloc(sz, GFP_KERNEL);
  903. if (buff[sg_used] == NULL) {
  904. status = -ENOMEM;
  905. goto cleanup1;
  906. }
  907. if (ioc->Request.Type.Direction == XFER_WRITE) {
  908. if (copy_from_user(buff[sg_used], data_ptr, sz)) {
  909. status = -ENOMEM;
  910. goto cleanup1;
  911. }
  912. } else {
  913. memset(buff[sg_used], 0, sz);
  914. }
  915. left -= sz;
  916. data_ptr += sz;
  917. sg_used++;
  918. }
  919. if ((c = cmd_alloc(host , 0)) == NULL) {
  920. status = -ENOMEM;
  921. goto cleanup1;
  922. }
  923. c->cmd_type = CMD_IOCTL_PEND;
  924. c->Header.ReplyQueue = 0;
  925. if( ioc->buf_size > 0) {
  926. c->Header.SGList = sg_used;
  927. c->Header.SGTotal= sg_used;
  928. } else {
  929. c->Header.SGList = 0;
  930. c->Header.SGTotal= 0;
  931. }
  932. c->Header.LUN = ioc->LUN_info;
  933. c->Header.Tag.lower = c->busaddr;
  934. c->Request = ioc->Request;
  935. if (ioc->buf_size > 0 ) {
  936. int i;
  937. for(i=0; i<sg_used; i++) {
  938. temp64.val = pci_map_single( host->pdev, buff[i],
  939. buff_size[i],
  940. PCI_DMA_BIDIRECTIONAL);
  941. c->SG[i].Addr.lower = temp64.val32.lower;
  942. c->SG[i].Addr.upper = temp64.val32.upper;
  943. c->SG[i].Len = buff_size[i];
  944. c->SG[i].Ext = 0; /* we are not chaining */
  945. }
  946. }
  947. c->waiting = &wait;
  948. /* Put the request on the tail of the request queue */
  949. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  950. addQ(&host->reqQ, c);
  951. host->Qdepth++;
  952. start_io(host);
  953. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  954. wait_for_completion(&wait);
  955. /* unlock the buffers from DMA */
  956. for(i=0; i<sg_used; i++) {
  957. temp64.val32.lower = c->SG[i].Addr.lower;
  958. temp64.val32.upper = c->SG[i].Addr.upper;
  959. pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
  960. buff_size[i], PCI_DMA_BIDIRECTIONAL);
  961. }
  962. /* Copy the error information out */
  963. ioc->error_info = *(c->err_info);
  964. if (copy_to_user(argp, ioc, sizeof(*ioc))) {
  965. cmd_free(host, c, 0);
  966. status = -EFAULT;
  967. goto cleanup1;
  968. }
  969. if (ioc->Request.Type.Direction == XFER_READ) {
  970. /* Copy the data out of the buffer we created */
  971. BYTE __user *ptr = ioc->buf;
  972. for(i=0; i< sg_used; i++) {
  973. if (copy_to_user(ptr, buff[i], buff_size[i])) {
  974. cmd_free(host, c, 0);
  975. status = -EFAULT;
  976. goto cleanup1;
  977. }
  978. ptr += buff_size[i];
  979. }
  980. }
  981. cmd_free(host, c, 0);
  982. status = 0;
  983. cleanup1:
  984. if (buff) {
  985. for(i=0; i<sg_used; i++)
  986. kfree(buff[i]);
  987. kfree(buff);
  988. }
  989. kfree(buff_size);
  990. kfree(ioc);
  991. return(status);
  992. }
  993. default:
  994. return -ENOTTY;
  995. }
  996. }
  997. /*
  998. * revalidate_allvol is for online array config utilities. After a
  999. * utility reconfigures the drives in the array, it can use this function
  1000. * (through an ioctl) to make the driver zap any previous disk structs for
  1001. * that controller and get new ones.
  1002. *
  1003. * Right now I'm using the getgeometry() function to do this, but this
  1004. * function should probably be finer grained and allow you to revalidate one
  1005. * particualar logical volume (instead of all of them on a particular
  1006. * controller).
  1007. */
  1008. static int revalidate_allvol(ctlr_info_t *host)
  1009. {
  1010. int ctlr = host->ctlr, i;
  1011. unsigned long flags;
  1012. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  1013. if (host->usage_count > 1) {
  1014. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1015. printk(KERN_WARNING "cciss: Device busy for volume"
  1016. " revalidation (usage=%d)\n", host->usage_count);
  1017. return -EBUSY;
  1018. }
  1019. host->usage_count++;
  1020. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1021. for(i=0; i< NWD; i++) {
  1022. struct gendisk *disk = host->gendisk[i];
  1023. if (disk) {
  1024. request_queue_t *q = disk->queue;
  1025. if (disk->flags & GENHD_FL_UP)
  1026. del_gendisk(disk);
  1027. if (q)
  1028. blk_cleanup_queue(q);
  1029. }
  1030. }
  1031. /*
  1032. * Set the partition and block size structures for all volumes
  1033. * on this controller to zero. We will reread all of this data
  1034. */
  1035. memset(host->drv, 0, sizeof(drive_info_struct)
  1036. * CISS_MAX_LUN);
  1037. /*
  1038. * Tell the array controller not to give us any interrupts while
  1039. * we check the new geometry. Then turn interrupts back on when
  1040. * we're done.
  1041. */
  1042. host->access.set_intr_mask(host, CCISS_INTR_OFF);
  1043. cciss_getgeometry(ctlr);
  1044. host->access.set_intr_mask(host, CCISS_INTR_ON);
  1045. /* Loop through each real device */
  1046. for (i = 0; i < NWD; i++) {
  1047. struct gendisk *disk = host->gendisk[i];
  1048. drive_info_struct *drv = &(host->drv[i]);
  1049. /* we must register the controller even if no disks exist */
  1050. /* this is for the online array utilities */
  1051. if (!drv->heads && i)
  1052. continue;
  1053. blk_queue_hardsect_size(drv->queue, drv->block_size);
  1054. set_capacity(disk, drv->nr_blocks);
  1055. add_disk(disk);
  1056. }
  1057. host->usage_count--;
  1058. return 0;
  1059. }
  1060. /* This function will check the usage_count of the drive to be updated/added.
  1061. * If the usage_count is zero then the drive information will be updated and
  1062. * the disk will be re-registered with the kernel. If not then it will be
  1063. * left alone for the next reboot. The exception to this is disk 0 which
  1064. * will always be left registered with the kernel since it is also the
  1065. * controller node. Any changes to disk 0 will show up on the next
  1066. * reboot.
  1067. */
  1068. static void cciss_update_drive_info(int ctlr, int drv_index)
  1069. {
  1070. ctlr_info_t *h = hba[ctlr];
  1071. struct gendisk *disk;
  1072. ReadCapdata_struct *size_buff = NULL;
  1073. InquiryData_struct *inq_buff = NULL;
  1074. unsigned int block_size;
  1075. unsigned int total_size;
  1076. unsigned long flags = 0;
  1077. int ret = 0;
  1078. /* if the disk already exists then deregister it before proceeding*/
  1079. if (h->drv[drv_index].raid_level != -1){
  1080. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  1081. h->drv[drv_index].busy_configuring = 1;
  1082. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1083. ret = deregister_disk(h->gendisk[drv_index],
  1084. &h->drv[drv_index], 0);
  1085. h->drv[drv_index].busy_configuring = 0;
  1086. }
  1087. /* If the disk is in use return */
  1088. if (ret)
  1089. return;
  1090. /* Get information about the disk and modify the driver sturcture */
  1091. size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
  1092. if (size_buff == NULL)
  1093. goto mem_msg;
  1094. inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
  1095. if (inq_buff == NULL)
  1096. goto mem_msg;
  1097. cciss_read_capacity(ctlr, drv_index, size_buff, 1,
  1098. &total_size, &block_size);
  1099. cciss_geometry_inquiry(ctlr, drv_index, 1, total_size, block_size,
  1100. inq_buff, &h->drv[drv_index]);
  1101. ++h->num_luns;
  1102. disk = h->gendisk[drv_index];
  1103. set_capacity(disk, h->drv[drv_index].nr_blocks);
  1104. /* if it's the controller it's already added */
  1105. if (drv_index){
  1106. disk->queue = blk_init_queue(do_cciss_request, &h->lock);
  1107. /* Set up queue information */
  1108. disk->queue->backing_dev_info.ra_pages = READ_AHEAD;
  1109. blk_queue_bounce_limit(disk->queue, hba[ctlr]->pdev->dma_mask);
  1110. /* This is a hardware imposed limit. */
  1111. blk_queue_max_hw_segments(disk->queue, MAXSGENTRIES);
  1112. /* This is a limit in the driver and could be eliminated. */
  1113. blk_queue_max_phys_segments(disk->queue, MAXSGENTRIES);
  1114. blk_queue_max_sectors(disk->queue, 512);
  1115. disk->queue->queuedata = hba[ctlr];
  1116. blk_queue_hardsect_size(disk->queue,
  1117. hba[ctlr]->drv[drv_index].block_size);
  1118. h->drv[drv_index].queue = disk->queue;
  1119. add_disk(disk);
  1120. }
  1121. freeret:
  1122. kfree(size_buff);
  1123. kfree(inq_buff);
  1124. return;
  1125. mem_msg:
  1126. printk(KERN_ERR "cciss: out of memory\n");
  1127. goto freeret;
  1128. }
  1129. /* This function will find the first index of the controllers drive array
  1130. * that has a -1 for the raid_level and will return that index. This is
  1131. * where new drives will be added. If the index to be returned is greater
  1132. * than the highest_lun index for the controller then highest_lun is set
  1133. * to this new index. If there are no available indexes then -1 is returned.
  1134. */
  1135. static int cciss_find_free_drive_index(int ctlr)
  1136. {
  1137. int i;
  1138. for (i=0; i < CISS_MAX_LUN; i++){
  1139. if (hba[ctlr]->drv[i].raid_level == -1){
  1140. if (i > hba[ctlr]->highest_lun)
  1141. hba[ctlr]->highest_lun = i;
  1142. return i;
  1143. }
  1144. }
  1145. return -1;
  1146. }
  1147. /* This function will add and remove logical drives from the Logical
  1148. * drive array of the controller and maintain persistancy of ordering
  1149. * so that mount points are preserved until the next reboot. This allows
  1150. * for the removal of logical drives in the middle of the drive array
  1151. * without a re-ordering of those drives.
  1152. * INPUT
  1153. * h = The controller to perform the operations on
  1154. * del_disk = The disk to remove if specified. If the value given
  1155. * is NULL then no disk is removed.
  1156. */
  1157. static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk)
  1158. {
  1159. int ctlr = h->ctlr;
  1160. int num_luns;
  1161. ReportLunData_struct *ld_buff = NULL;
  1162. drive_info_struct *drv = NULL;
  1163. int return_code;
  1164. int listlength = 0;
  1165. int i;
  1166. int drv_found;
  1167. int drv_index = 0;
  1168. __u32 lunid = 0;
  1169. unsigned long flags;
  1170. /* Set busy_configuring flag for this operation */
  1171. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  1172. if (h->num_luns >= CISS_MAX_LUN){
  1173. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1174. return -EINVAL;
  1175. }
  1176. if (h->busy_configuring){
  1177. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1178. return -EBUSY;
  1179. }
  1180. h->busy_configuring = 1;
  1181. /* if del_disk is NULL then we are being called to add a new disk
  1182. * and update the logical drive table. If it is not NULL then
  1183. * we will check if the disk is in use or not.
  1184. */
  1185. if (del_disk != NULL){
  1186. drv = get_drv(del_disk);
  1187. drv->busy_configuring = 1;
  1188. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1189. return_code = deregister_disk(del_disk, drv, 1);
  1190. drv->busy_configuring = 0;
  1191. h->busy_configuring = 0;
  1192. return return_code;
  1193. } else {
  1194. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1195. if (!capable(CAP_SYS_RAWIO))
  1196. return -EPERM;
  1197. ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
  1198. if (ld_buff == NULL)
  1199. goto mem_msg;
  1200. return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
  1201. sizeof(ReportLunData_struct), 0, 0, 0,
  1202. TYPE_CMD);
  1203. if (return_code == IO_OK){
  1204. listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
  1205. listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
  1206. listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
  1207. listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
  1208. } else{ /* reading number of logical volumes failed */
  1209. printk(KERN_WARNING "cciss: report logical volume"
  1210. " command failed\n");
  1211. listlength = 0;
  1212. goto freeret;
  1213. }
  1214. num_luns = listlength / 8; /* 8 bytes per entry */
  1215. if (num_luns > CISS_MAX_LUN){
  1216. num_luns = CISS_MAX_LUN;
  1217. printk(KERN_WARNING "cciss: more luns configured"
  1218. " on controller than can be handled by"
  1219. " this driver.\n");
  1220. }
  1221. /* Compare controller drive array to drivers drive array.
  1222. * Check for updates in the drive information and any new drives
  1223. * on the controller.
  1224. */
  1225. for (i=0; i < num_luns; i++){
  1226. int j;
  1227. drv_found = 0;
  1228. lunid = (0xff &
  1229. (unsigned int)(ld_buff->LUN[i][3])) << 24;
  1230. lunid |= (0xff &
  1231. (unsigned int)(ld_buff->LUN[i][2])) << 16;
  1232. lunid |= (0xff &
  1233. (unsigned int)(ld_buff->LUN[i][1])) << 8;
  1234. lunid |= 0xff &
  1235. (unsigned int)(ld_buff->LUN[i][0]);
  1236. /* Find if the LUN is already in the drive array
  1237. * of the controller. If so then update its info
  1238. * if not is use. If it does not exist then find
  1239. * the first free index and add it.
  1240. */
  1241. for (j=0; j <= h->highest_lun; j++){
  1242. if (h->drv[j].LunID == lunid){
  1243. drv_index = j;
  1244. drv_found = 1;
  1245. }
  1246. }
  1247. /* check if the drive was found already in the array */
  1248. if (!drv_found){
  1249. drv_index = cciss_find_free_drive_index(ctlr);
  1250. if (drv_index == -1)
  1251. goto freeret;
  1252. }
  1253. h->drv[drv_index].LunID = lunid;
  1254. cciss_update_drive_info(ctlr, drv_index);
  1255. } /* end for */
  1256. } /* end else */
  1257. freeret:
  1258. kfree(ld_buff);
  1259. h->busy_configuring = 0;
  1260. /* We return -1 here to tell the ACU that we have registered/updated
  1261. * all of the drives that we can and to keep it from calling us
  1262. * additional times.
  1263. */
  1264. return -1;
  1265. mem_msg:
  1266. printk(KERN_ERR "cciss: out of memory\n");
  1267. goto freeret;
  1268. }
  1269. /* This function will deregister the disk and it's queue from the
  1270. * kernel. It must be called with the controller lock held and the
  1271. * drv structures busy_configuring flag set. It's parameters are:
  1272. *
  1273. * disk = This is the disk to be deregistered
  1274. * drv = This is the drive_info_struct associated with the disk to be
  1275. * deregistered. It contains information about the disk used
  1276. * by the driver.
  1277. * clear_all = This flag determines whether or not the disk information
  1278. * is going to be completely cleared out and the highest_lun
  1279. * reset. Sometimes we want to clear out information about
  1280. * the disk in preperation for re-adding it. In this case
  1281. * the highest_lun should be left unchanged and the LunID
  1282. * should not be cleared.
  1283. */
  1284. static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
  1285. int clear_all)
  1286. {
  1287. ctlr_info_t *h = get_host(disk);
  1288. if (!capable(CAP_SYS_RAWIO))
  1289. return -EPERM;
  1290. /* make sure logical volume is NOT is use */
  1291. if(clear_all || (h->gendisk[0] == disk)) {
  1292. if (drv->usage_count > 1)
  1293. return -EBUSY;
  1294. }
  1295. else
  1296. if( drv->usage_count > 0 )
  1297. return -EBUSY;
  1298. /* invalidate the devices and deregister the disk. If it is disk
  1299. * zero do not deregister it but just zero out it's values. This
  1300. * allows us to delete disk zero but keep the controller registered.
  1301. */
  1302. if (h->gendisk[0] != disk){
  1303. if (disk) {
  1304. request_queue_t *q = disk->queue;
  1305. if (disk->flags & GENHD_FL_UP)
  1306. del_gendisk(disk);
  1307. if (q) {
  1308. blk_cleanup_queue(q);
  1309. drv->queue = NULL;
  1310. }
  1311. }
  1312. }
  1313. --h->num_luns;
  1314. /* zero out the disk size info */
  1315. drv->nr_blocks = 0;
  1316. drv->block_size = 0;
  1317. drv->heads = 0;
  1318. drv->sectors = 0;
  1319. drv->cylinders = 0;
  1320. drv->raid_level = -1; /* This can be used as a flag variable to
  1321. * indicate that this element of the drive
  1322. * array is free.
  1323. */
  1324. if (clear_all){
  1325. /* check to see if it was the last disk */
  1326. if (drv == h->drv + h->highest_lun) {
  1327. /* if so, find the new hightest lun */
  1328. int i, newhighest =-1;
  1329. for(i=0; i<h->highest_lun; i++) {
  1330. /* if the disk has size > 0, it is available */
  1331. if (h->drv[i].heads)
  1332. newhighest = i;
  1333. }
  1334. h->highest_lun = newhighest;
  1335. }
  1336. drv->LunID = 0;
  1337. }
  1338. return(0);
  1339. }
  1340. static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
  1341. size_t size,
  1342. unsigned int use_unit_num, /* 0: address the controller,
  1343. 1: address logical volume log_unit,
  1344. 2: periph device address is scsi3addr */
  1345. unsigned int log_unit, __u8 page_code, unsigned char *scsi3addr,
  1346. int cmd_type)
  1347. {
  1348. ctlr_info_t *h= hba[ctlr];
  1349. u64bit buff_dma_handle;
  1350. int status = IO_OK;
  1351. c->cmd_type = CMD_IOCTL_PEND;
  1352. c->Header.ReplyQueue = 0;
  1353. if( buff != NULL) {
  1354. c->Header.SGList = 1;
  1355. c->Header.SGTotal= 1;
  1356. } else {
  1357. c->Header.SGList = 0;
  1358. c->Header.SGTotal= 0;
  1359. }
  1360. c->Header.Tag.lower = c->busaddr;
  1361. c->Request.Type.Type = cmd_type;
  1362. if (cmd_type == TYPE_CMD) {
  1363. switch(cmd) {
  1364. case CISS_INQUIRY:
  1365. /* If the logical unit number is 0 then, this is going
  1366. to controller so It's a physical command
  1367. mode = 0 target = 0. So we have nothing to write.
  1368. otherwise, if use_unit_num == 1,
  1369. mode = 1(volume set addressing) target = LUNID
  1370. otherwise, if use_unit_num == 2,
  1371. mode = 0(periph dev addr) target = scsi3addr */
  1372. if (use_unit_num == 1) {
  1373. c->Header.LUN.LogDev.VolId=
  1374. h->drv[log_unit].LunID;
  1375. c->Header.LUN.LogDev.Mode = 1;
  1376. } else if (use_unit_num == 2) {
  1377. memcpy(c->Header.LUN.LunAddrBytes,scsi3addr,8);
  1378. c->Header.LUN.LogDev.Mode = 0;
  1379. }
  1380. /* are we trying to read a vital product page */
  1381. if(page_code != 0) {
  1382. c->Request.CDB[1] = 0x01;
  1383. c->Request.CDB[2] = page_code;
  1384. }
  1385. c->Request.CDBLen = 6;
  1386. c->Request.Type.Attribute = ATTR_SIMPLE;
  1387. c->Request.Type.Direction = XFER_READ;
  1388. c->Request.Timeout = 0;
  1389. c->Request.CDB[0] = CISS_INQUIRY;
  1390. c->Request.CDB[4] = size & 0xFF;
  1391. break;
  1392. case CISS_REPORT_LOG:
  1393. case CISS_REPORT_PHYS:
  1394. /* Talking to controller so It's a physical command
  1395. mode = 00 target = 0. Nothing to write.
  1396. */
  1397. c->Request.CDBLen = 12;
  1398. c->Request.Type.Attribute = ATTR_SIMPLE;
  1399. c->Request.Type.Direction = XFER_READ;
  1400. c->Request.Timeout = 0;
  1401. c->Request.CDB[0] = cmd;
  1402. c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
  1403. c->Request.CDB[7] = (size >> 16) & 0xFF;
  1404. c->Request.CDB[8] = (size >> 8) & 0xFF;
  1405. c->Request.CDB[9] = size & 0xFF;
  1406. break;
  1407. case CCISS_READ_CAPACITY:
  1408. c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
  1409. c->Header.LUN.LogDev.Mode = 1;
  1410. c->Request.CDBLen = 10;
  1411. c->Request.Type.Attribute = ATTR_SIMPLE;
  1412. c->Request.Type.Direction = XFER_READ;
  1413. c->Request.Timeout = 0;
  1414. c->Request.CDB[0] = cmd;
  1415. break;
  1416. case CCISS_CACHE_FLUSH:
  1417. c->Request.CDBLen = 12;
  1418. c->Request.Type.Attribute = ATTR_SIMPLE;
  1419. c->Request.Type.Direction = XFER_WRITE;
  1420. c->Request.Timeout = 0;
  1421. c->Request.CDB[0] = BMIC_WRITE;
  1422. c->Request.CDB[6] = BMIC_CACHE_FLUSH;
  1423. break;
  1424. default:
  1425. printk(KERN_WARNING
  1426. "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
  1427. return(IO_ERROR);
  1428. }
  1429. } else if (cmd_type == TYPE_MSG) {
  1430. switch (cmd) {
  1431. case 0: /* ABORT message */
  1432. c->Request.CDBLen = 12;
  1433. c->Request.Type.Attribute = ATTR_SIMPLE;
  1434. c->Request.Type.Direction = XFER_WRITE;
  1435. c->Request.Timeout = 0;
  1436. c->Request.CDB[0] = cmd; /* abort */
  1437. c->Request.CDB[1] = 0; /* abort a command */
  1438. /* buff contains the tag of the command to abort */
  1439. memcpy(&c->Request.CDB[4], buff, 8);
  1440. break;
  1441. case 1: /* RESET message */
  1442. c->Request.CDBLen = 12;
  1443. c->Request.Type.Attribute = ATTR_SIMPLE;
  1444. c->Request.Type.Direction = XFER_WRITE;
  1445. c->Request.Timeout = 0;
  1446. memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
  1447. c->Request.CDB[0] = cmd; /* reset */
  1448. c->Request.CDB[1] = 0x04; /* reset a LUN */
  1449. case 3: /* No-Op message */
  1450. c->Request.CDBLen = 1;
  1451. c->Request.Type.Attribute = ATTR_SIMPLE;
  1452. c->Request.Type.Direction = XFER_WRITE;
  1453. c->Request.Timeout = 0;
  1454. c->Request.CDB[0] = cmd;
  1455. break;
  1456. default:
  1457. printk(KERN_WARNING
  1458. "cciss%d: unknown message type %d\n",
  1459. ctlr, cmd);
  1460. return IO_ERROR;
  1461. }
  1462. } else {
  1463. printk(KERN_WARNING
  1464. "cciss%d: unknown command type %d\n", ctlr, cmd_type);
  1465. return IO_ERROR;
  1466. }
  1467. /* Fill in the scatter gather information */
  1468. if (size > 0) {
  1469. buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
  1470. buff, size, PCI_DMA_BIDIRECTIONAL);
  1471. c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
  1472. c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
  1473. c->SG[0].Len = size;
  1474. c->SG[0].Ext = 0; /* we are not chaining */
  1475. }
  1476. return status;
  1477. }
  1478. static int sendcmd_withirq(__u8 cmd,
  1479. int ctlr,
  1480. void *buff,
  1481. size_t size,
  1482. unsigned int use_unit_num,
  1483. unsigned int log_unit,
  1484. __u8 page_code,
  1485. int cmd_type)
  1486. {
  1487. ctlr_info_t *h = hba[ctlr];
  1488. CommandList_struct *c;
  1489. u64bit buff_dma_handle;
  1490. unsigned long flags;
  1491. int return_status;
  1492. DECLARE_COMPLETION(wait);
  1493. if ((c = cmd_alloc(h , 0)) == NULL)
  1494. return -ENOMEM;
  1495. return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
  1496. log_unit, page_code, NULL, cmd_type);
  1497. if (return_status != IO_OK) {
  1498. cmd_free(h, c, 0);
  1499. return return_status;
  1500. }
  1501. resend_cmd2:
  1502. c->waiting = &wait;
  1503. /* Put the request on the tail of the queue and send it */
  1504. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  1505. addQ(&h->reqQ, c);
  1506. h->Qdepth++;
  1507. start_io(h);
  1508. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1509. wait_for_completion(&wait);
  1510. if(c->err_info->CommandStatus != 0)
  1511. { /* an error has occurred */
  1512. switch(c->err_info->CommandStatus)
  1513. {
  1514. case CMD_TARGET_STATUS:
  1515. printk(KERN_WARNING "cciss: cmd %p has "
  1516. " completed with errors\n", c);
  1517. if( c->err_info->ScsiStatus)
  1518. {
  1519. printk(KERN_WARNING "cciss: cmd %p "
  1520. "has SCSI Status = %x\n",
  1521. c,
  1522. c->err_info->ScsiStatus);
  1523. }
  1524. break;
  1525. case CMD_DATA_UNDERRUN:
  1526. case CMD_DATA_OVERRUN:
  1527. /* expected for inquire and report lun commands */
  1528. break;
  1529. case CMD_INVALID:
  1530. printk(KERN_WARNING "cciss: Cmd %p is "
  1531. "reported invalid\n", c);
  1532. return_status = IO_ERROR;
  1533. break;
  1534. case CMD_PROTOCOL_ERR:
  1535. printk(KERN_WARNING "cciss: cmd %p has "
  1536. "protocol error \n", c);
  1537. return_status = IO_ERROR;
  1538. break;
  1539. case CMD_HARDWARE_ERR:
  1540. printk(KERN_WARNING "cciss: cmd %p had "
  1541. " hardware error\n", c);
  1542. return_status = IO_ERROR;
  1543. break;
  1544. case CMD_CONNECTION_LOST:
  1545. printk(KERN_WARNING "cciss: cmd %p had "
  1546. "connection lost\n", c);
  1547. return_status = IO_ERROR;
  1548. break;
  1549. case CMD_ABORTED:
  1550. printk(KERN_WARNING "cciss: cmd %p was "
  1551. "aborted\n", c);
  1552. return_status = IO_ERROR;
  1553. break;
  1554. case CMD_ABORT_FAILED:
  1555. printk(KERN_WARNING "cciss: cmd %p reports "
  1556. "abort failed\n", c);
  1557. return_status = IO_ERROR;
  1558. break;
  1559. case CMD_UNSOLICITED_ABORT:
  1560. printk(KERN_WARNING
  1561. "cciss%d: unsolicited abort %p\n",
  1562. ctlr, c);
  1563. if (c->retry_count < MAX_CMD_RETRIES) {
  1564. printk(KERN_WARNING
  1565. "cciss%d: retrying %p\n",
  1566. ctlr, c);
  1567. c->retry_count++;
  1568. /* erase the old error information */
  1569. memset(c->err_info, 0,
  1570. sizeof(ErrorInfo_struct));
  1571. return_status = IO_OK;
  1572. INIT_COMPLETION(wait);
  1573. goto resend_cmd2;
  1574. }
  1575. return_status = IO_ERROR;
  1576. break;
  1577. default:
  1578. printk(KERN_WARNING "cciss: cmd %p returned "
  1579. "unknown status %x\n", c,
  1580. c->err_info->CommandStatus);
  1581. return_status = IO_ERROR;
  1582. }
  1583. }
  1584. /* unlock the buffers from DMA */
  1585. buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
  1586. buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
  1587. pci_unmap_single( h->pdev, (dma_addr_t) buff_dma_handle.val,
  1588. c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
  1589. cmd_free(h, c, 0);
  1590. return(return_status);
  1591. }
  1592. static void cciss_geometry_inquiry(int ctlr, int logvol,
  1593. int withirq, unsigned int total_size,
  1594. unsigned int block_size, InquiryData_struct *inq_buff,
  1595. drive_info_struct *drv)
  1596. {
  1597. int return_code;
  1598. memset(inq_buff, 0, sizeof(InquiryData_struct));
  1599. if (withirq)
  1600. return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
  1601. inq_buff, sizeof(*inq_buff), 1, logvol ,0xC1, TYPE_CMD);
  1602. else
  1603. return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
  1604. sizeof(*inq_buff), 1, logvol ,0xC1, NULL, TYPE_CMD);
  1605. if (return_code == IO_OK) {
  1606. if(inq_buff->data_byte[8] == 0xFF) {
  1607. printk(KERN_WARNING
  1608. "cciss: reading geometry failed, volume "
  1609. "does not support reading geometry\n");
  1610. drv->block_size = block_size;
  1611. drv->nr_blocks = total_size;
  1612. drv->heads = 255;
  1613. drv->sectors = 32; // Sectors per track
  1614. drv->cylinders = total_size / 255 / 32;
  1615. } else {
  1616. unsigned int t;
  1617. drv->block_size = block_size;
  1618. drv->nr_blocks = total_size;
  1619. drv->heads = inq_buff->data_byte[6];
  1620. drv->sectors = inq_buff->data_byte[7];
  1621. drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
  1622. drv->cylinders += inq_buff->data_byte[5];
  1623. drv->raid_level = inq_buff->data_byte[8];
  1624. t = drv->heads * drv->sectors;
  1625. if (t > 1) {
  1626. drv->cylinders = total_size/t;
  1627. }
  1628. }
  1629. } else { /* Get geometry failed */
  1630. printk(KERN_WARNING "cciss: reading geometry failed\n");
  1631. }
  1632. printk(KERN_INFO " heads= %d, sectors= %d, cylinders= %d\n\n",
  1633. drv->heads, drv->sectors, drv->cylinders);
  1634. }
  1635. static void
  1636. cciss_read_capacity(int ctlr, int logvol, ReadCapdata_struct *buf,
  1637. int withirq, unsigned int *total_size, unsigned int *block_size)
  1638. {
  1639. int return_code;
  1640. memset(buf, 0, sizeof(*buf));
  1641. if (withirq)
  1642. return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
  1643. ctlr, buf, sizeof(*buf), 1, logvol, 0, TYPE_CMD);
  1644. else
  1645. return_code = sendcmd(CCISS_READ_CAPACITY,
  1646. ctlr, buf, sizeof(*buf), 1, logvol, 0, NULL, TYPE_CMD);
  1647. if (return_code == IO_OK) {
  1648. *total_size = be32_to_cpu(*((__be32 *) &buf->total_size[0]))+1;
  1649. *block_size = be32_to_cpu(*((__be32 *) &buf->block_size[0]));
  1650. } else { /* read capacity command failed */
  1651. printk(KERN_WARNING "cciss: read capacity failed\n");
  1652. *total_size = 0;
  1653. *block_size = BLOCK_SIZE;
  1654. }
  1655. printk(KERN_INFO " blocks= %u block_size= %d\n",
  1656. *total_size, *block_size);
  1657. return;
  1658. }
  1659. static int cciss_revalidate(struct gendisk *disk)
  1660. {
  1661. ctlr_info_t *h = get_host(disk);
  1662. drive_info_struct *drv = get_drv(disk);
  1663. int logvol;
  1664. int FOUND=0;
  1665. unsigned int block_size;
  1666. unsigned int total_size;
  1667. ReadCapdata_struct *size_buff = NULL;
  1668. InquiryData_struct *inq_buff = NULL;
  1669. for(logvol=0; logvol < CISS_MAX_LUN; logvol++)
  1670. {
  1671. if(h->drv[logvol].LunID == drv->LunID) {
  1672. FOUND=1;
  1673. break;
  1674. }
  1675. }
  1676. if (!FOUND) return 1;
  1677. size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
  1678. if (size_buff == NULL)
  1679. {
  1680. printk(KERN_WARNING "cciss: out of memory\n");
  1681. return 1;
  1682. }
  1683. inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
  1684. if (inq_buff == NULL)
  1685. {
  1686. printk(KERN_WARNING "cciss: out of memory\n");
  1687. kfree(size_buff);
  1688. return 1;
  1689. }
  1690. cciss_read_capacity(h->ctlr, logvol, size_buff, 1, &total_size, &block_size);
  1691. cciss_geometry_inquiry(h->ctlr, logvol, 1, total_size, block_size, inq_buff, drv);
  1692. blk_queue_hardsect_size(drv->queue, drv->block_size);
  1693. set_capacity(disk, drv->nr_blocks);
  1694. kfree(size_buff);
  1695. kfree(inq_buff);
  1696. return 0;
  1697. }
  1698. /*
  1699. * Wait polling for a command to complete.
  1700. * The memory mapped FIFO is polled for the completion.
  1701. * Used only at init time, interrupts from the HBA are disabled.
  1702. */
  1703. static unsigned long pollcomplete(int ctlr)
  1704. {
  1705. unsigned long done;
  1706. int i;
  1707. /* Wait (up to 20 seconds) for a command to complete */
  1708. for (i = 20 * HZ; i > 0; i--) {
  1709. done = hba[ctlr]->access.command_completed(hba[ctlr]);
  1710. if (done == FIFO_EMPTY)
  1711. schedule_timeout_uninterruptible(1);
  1712. else
  1713. return (done);
  1714. }
  1715. /* Invalid address to tell caller we ran out of time */
  1716. return 1;
  1717. }
  1718. static int add_sendcmd_reject(__u8 cmd, int ctlr, unsigned long complete)
  1719. {
  1720. /* We get in here if sendcmd() is polling for completions
  1721. and gets some command back that it wasn't expecting --
  1722. something other than that which it just sent down.
  1723. Ordinarily, that shouldn't happen, but it can happen when
  1724. the scsi tape stuff gets into error handling mode, and
  1725. starts using sendcmd() to try to abort commands and
  1726. reset tape drives. In that case, sendcmd may pick up
  1727. completions of commands that were sent to logical drives
  1728. through the block i/o system, or cciss ioctls completing, etc.
  1729. In that case, we need to save those completions for later
  1730. processing by the interrupt handler.
  1731. */
  1732. #ifdef CONFIG_CISS_SCSI_TAPE
  1733. struct sendcmd_reject_list *srl = &hba[ctlr]->scsi_rejects;
  1734. /* If it's not the scsi tape stuff doing error handling, (abort */
  1735. /* or reset) then we don't expect anything weird. */
  1736. if (cmd != CCISS_RESET_MSG && cmd != CCISS_ABORT_MSG) {
  1737. #endif
  1738. printk( KERN_WARNING "cciss cciss%d: SendCmd "
  1739. "Invalid command list address returned! (%lx)\n",
  1740. ctlr, complete);
  1741. /* not much we can do. */
  1742. #ifdef CONFIG_CISS_SCSI_TAPE
  1743. return 1;
  1744. }
  1745. /* We've sent down an abort or reset, but something else
  1746. has completed */
  1747. if (srl->ncompletions >= (NR_CMDS + 2)) {
  1748. /* Uh oh. No room to save it for later... */
  1749. printk(KERN_WARNING "cciss%d: Sendcmd: Invalid command addr, "
  1750. "reject list overflow, command lost!\n", ctlr);
  1751. return 1;
  1752. }
  1753. /* Save it for later */
  1754. srl->complete[srl->ncompletions] = complete;
  1755. srl->ncompletions++;
  1756. #endif
  1757. return 0;
  1758. }
  1759. /*
  1760. * Send a command to the controller, and wait for it to complete.
  1761. * Only used at init time.
  1762. */
  1763. static int sendcmd(
  1764. __u8 cmd,
  1765. int ctlr,
  1766. void *buff,
  1767. size_t size,
  1768. unsigned int use_unit_num, /* 0: address the controller,
  1769. 1: address logical volume log_unit,
  1770. 2: periph device address is scsi3addr */
  1771. unsigned int log_unit,
  1772. __u8 page_code,
  1773. unsigned char *scsi3addr,
  1774. int cmd_type)
  1775. {
  1776. CommandList_struct *c;
  1777. int i;
  1778. unsigned long complete;
  1779. ctlr_info_t *info_p= hba[ctlr];
  1780. u64bit buff_dma_handle;
  1781. int status, done = 0;
  1782. if ((c = cmd_alloc(info_p, 1)) == NULL) {
  1783. printk(KERN_WARNING "cciss: unable to get memory");
  1784. return(IO_ERROR);
  1785. }
  1786. status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
  1787. log_unit, page_code, scsi3addr, cmd_type);
  1788. if (status != IO_OK) {
  1789. cmd_free(info_p, c, 1);
  1790. return status;
  1791. }
  1792. resend_cmd1:
  1793. /*
  1794. * Disable interrupt
  1795. */
  1796. #ifdef CCISS_DEBUG
  1797. printk(KERN_DEBUG "cciss: turning intr off\n");
  1798. #endif /* CCISS_DEBUG */
  1799. info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
  1800. /* Make sure there is room in the command FIFO */
  1801. /* Actually it should be completely empty at this time */
  1802. /* unless we are in here doing error handling for the scsi */
  1803. /* tape side of the driver. */
  1804. for (i = 200000; i > 0; i--)
  1805. {
  1806. /* if fifo isn't full go */
  1807. if (!(info_p->access.fifo_full(info_p)))
  1808. {
  1809. break;
  1810. }
  1811. udelay(10);
  1812. printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
  1813. " waiting!\n", ctlr);
  1814. }
  1815. /*
  1816. * Send the cmd
  1817. */
  1818. info_p->access.submit_command(info_p, c);
  1819. done = 0;
  1820. do {
  1821. complete = pollcomplete(ctlr);
  1822. #ifdef CCISS_DEBUG
  1823. printk(KERN_DEBUG "cciss: command completed\n");
  1824. #endif /* CCISS_DEBUG */
  1825. if (complete == 1) {
  1826. printk( KERN_WARNING
  1827. "cciss cciss%d: SendCmd Timeout out, "
  1828. "No command list address returned!\n",
  1829. ctlr);
  1830. status = IO_ERROR;
  1831. done = 1;
  1832. break;
  1833. }
  1834. /* This will need to change for direct lookup completions */
  1835. if ( (complete & CISS_ERROR_BIT)
  1836. && (complete & ~CISS_ERROR_BIT) == c->busaddr)
  1837. {
  1838. /* if data overrun or underun on Report command
  1839. ignore it
  1840. */
  1841. if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
  1842. (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
  1843. (c->Request.CDB[0] == CISS_INQUIRY)) &&
  1844. ((c->err_info->CommandStatus ==
  1845. CMD_DATA_OVERRUN) ||
  1846. (c->err_info->CommandStatus ==
  1847. CMD_DATA_UNDERRUN)
  1848. ))
  1849. {
  1850. complete = c->busaddr;
  1851. } else {
  1852. if (c->err_info->CommandStatus ==
  1853. CMD_UNSOLICITED_ABORT) {
  1854. printk(KERN_WARNING "cciss%d: "
  1855. "unsolicited abort %p\n",
  1856. ctlr, c);
  1857. if (c->retry_count < MAX_CMD_RETRIES) {
  1858. printk(KERN_WARNING
  1859. "cciss%d: retrying %p\n",
  1860. ctlr, c);
  1861. c->retry_count++;
  1862. /* erase the old error */
  1863. /* information */
  1864. memset(c->err_info, 0,
  1865. sizeof(ErrorInfo_struct));
  1866. goto resend_cmd1;
  1867. } else {
  1868. printk(KERN_WARNING
  1869. "cciss%d: retried %p too "
  1870. "many times\n", ctlr, c);
  1871. status = IO_ERROR;
  1872. goto cleanup1;
  1873. }
  1874. } else if (c->err_info->CommandStatus == CMD_UNABORTABLE) {
  1875. printk(KERN_WARNING "cciss%d: command could not be aborted.\n", ctlr);
  1876. status = IO_ERROR;
  1877. goto cleanup1;
  1878. }
  1879. printk(KERN_WARNING "ciss ciss%d: sendcmd"
  1880. " Error %x \n", ctlr,
  1881. c->err_info->CommandStatus);
  1882. printk(KERN_WARNING "ciss ciss%d: sendcmd"
  1883. " offensive info\n"
  1884. " size %x\n num %x value %x\n", ctlr,
  1885. c->err_info->MoreErrInfo.Invalid_Cmd.offense_size,
  1886. c->err_info->MoreErrInfo.Invalid_Cmd.offense_num,
  1887. c->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
  1888. status = IO_ERROR;
  1889. goto cleanup1;
  1890. }
  1891. }
  1892. /* This will need changing for direct lookup completions */
  1893. if (complete != c->busaddr) {
  1894. if (add_sendcmd_reject(cmd, ctlr, complete) != 0) {
  1895. BUG(); /* we are pretty much hosed if we get here. */
  1896. }
  1897. continue;
  1898. } else
  1899. done = 1;
  1900. } while (!done);
  1901. cleanup1:
  1902. /* unlock the data buffer from DMA */
  1903. buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
  1904. buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
  1905. pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
  1906. c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
  1907. #ifdef CONFIG_CISS_SCSI_TAPE
  1908. /* if we saved some commands for later, process them now. */
  1909. if (info_p->scsi_rejects.ncompletions > 0)
  1910. do_cciss_intr(0, info_p, NULL);
  1911. #endif
  1912. cmd_free(info_p, c, 1);
  1913. return (status);
  1914. }
  1915. /*
  1916. * Map (physical) PCI mem into (virtual) kernel space
  1917. */
  1918. static void __iomem *remap_pci_mem(ulong base, ulong size)
  1919. {
  1920. ulong page_base = ((ulong) base) & PAGE_MASK;
  1921. ulong page_offs = ((ulong) base) - page_base;
  1922. void __iomem *page_remapped = ioremap(page_base, page_offs+size);
  1923. return page_remapped ? (page_remapped + page_offs) : NULL;
  1924. }
  1925. /*
  1926. * Takes jobs of the Q and sends them to the hardware, then puts it on
  1927. * the Q to wait for completion.
  1928. */
  1929. static void start_io( ctlr_info_t *h)
  1930. {
  1931. CommandList_struct *c;
  1932. while(( c = h->reqQ) != NULL )
  1933. {
  1934. /* can't do anything if fifo is full */
  1935. if ((h->access.fifo_full(h))) {
  1936. printk(KERN_WARNING "cciss: fifo full\n");
  1937. break;
  1938. }
  1939. /* Get the frist entry from the Request Q */
  1940. removeQ(&(h->reqQ), c);
  1941. h->Qdepth--;
  1942. /* Tell the controller execute command */
  1943. h->access.submit_command(h, c);
  1944. /* Put job onto the completed Q */
  1945. addQ (&(h->cmpQ), c);
  1946. }
  1947. }
  1948. static inline void complete_buffers(struct bio *bio, int status)
  1949. {
  1950. while (bio) {
  1951. struct bio *xbh = bio->bi_next;
  1952. int nr_sectors = bio_sectors(bio);
  1953. bio->bi_next = NULL;
  1954. blk_finished_io(len);
  1955. bio_endio(bio, nr_sectors << 9, status ? 0 : -EIO);
  1956. bio = xbh;
  1957. }
  1958. }
  1959. /* Assumes that CCISS_LOCK(h->ctlr) is held. */
  1960. /* Zeros out the error record and then resends the command back */
  1961. /* to the controller */
  1962. static inline void resend_cciss_cmd( ctlr_info_t *h, CommandList_struct *c)
  1963. {
  1964. /* erase the old error information */
  1965. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  1966. /* add it to software queue and then send it to the controller */
  1967. addQ(&(h->reqQ),c);
  1968. h->Qdepth++;
  1969. if(h->Qdepth > h->maxQsinceinit)
  1970. h->maxQsinceinit = h->Qdepth;
  1971. start_io(h);
  1972. }
  1973. static void cciss_softirq_done(struct request *rq)
  1974. {
  1975. CommandList_struct *cmd = rq->completion_data;
  1976. ctlr_info_t *h = hba[cmd->ctlr];
  1977. u64bit temp64;
  1978. int i, ddir;
  1979. if (cmd->Request.Type.Direction == XFER_READ)
  1980. ddir = PCI_DMA_FROMDEVICE;
  1981. else
  1982. ddir = PCI_DMA_TODEVICE;
  1983. /* command did not need to be retried */
  1984. /* unmap the DMA mapping for all the scatter gather elements */
  1985. for(i=0; i<cmd->Header.SGList; i++) {
  1986. temp64.val32.lower = cmd->SG[i].Addr.lower;
  1987. temp64.val32.upper = cmd->SG[i].Addr.upper;
  1988. pci_unmap_page(h->pdev, temp64.val, cmd->SG[i].Len, ddir);
  1989. }
  1990. complete_buffers(rq->bio, rq->errors);
  1991. #ifdef CCISS_DEBUG
  1992. printk("Done with %p\n", rq);
  1993. #endif /* CCISS_DEBUG */
  1994. spin_lock_irq(&h->lock);
  1995. end_that_request_last(rq, rq->errors);
  1996. cmd_free(h, cmd,1);
  1997. spin_unlock_irq(&h->lock);
  1998. }
  1999. /* checks the status of the job and calls complete buffers to mark all
  2000. * buffers for the completed job. Note that this function does not need
  2001. * to hold the hba/queue lock.
  2002. */
  2003. static inline void complete_command( ctlr_info_t *h, CommandList_struct *cmd,
  2004. int timeout)
  2005. {
  2006. int status = 1;
  2007. int retry_cmd = 0;
  2008. if (timeout)
  2009. status = 0;
  2010. if(cmd->err_info->CommandStatus != 0)
  2011. { /* an error has occurred */
  2012. switch(cmd->err_info->CommandStatus)
  2013. {
  2014. unsigned char sense_key;
  2015. case CMD_TARGET_STATUS:
  2016. status = 0;
  2017. if( cmd->err_info->ScsiStatus == 0x02)
  2018. {
  2019. printk(KERN_WARNING "cciss: cmd %p "
  2020. "has CHECK CONDITION "
  2021. " byte 2 = 0x%x\n", cmd,
  2022. cmd->err_info->SenseInfo[2]
  2023. );
  2024. /* check the sense key */
  2025. sense_key = 0xf &
  2026. cmd->err_info->SenseInfo[2];
  2027. /* no status or recovered error */
  2028. if((sense_key == 0x0) ||
  2029. (sense_key == 0x1))
  2030. {
  2031. status = 1;
  2032. }
  2033. } else
  2034. {
  2035. printk(KERN_WARNING "cciss: cmd %p "
  2036. "has SCSI Status 0x%x\n",
  2037. cmd, cmd->err_info->ScsiStatus);
  2038. }
  2039. break;
  2040. case CMD_DATA_UNDERRUN:
  2041. printk(KERN_WARNING "cciss: cmd %p has"
  2042. " completed with data underrun "
  2043. "reported\n", cmd);
  2044. break;
  2045. case CMD_DATA_OVERRUN:
  2046. printk(KERN_WARNING "cciss: cmd %p has"
  2047. " completed with data overrun "
  2048. "reported\n", cmd);
  2049. break;
  2050. case CMD_INVALID:
  2051. printk(KERN_WARNING "cciss: cmd %p is "
  2052. "reported invalid\n", cmd);
  2053. status = 0;
  2054. break;
  2055. case CMD_PROTOCOL_ERR:
  2056. printk(KERN_WARNING "cciss: cmd %p has "
  2057. "protocol error \n", cmd);
  2058. status = 0;
  2059. break;
  2060. case CMD_HARDWARE_ERR:
  2061. printk(KERN_WARNING "cciss: cmd %p had "
  2062. " hardware error\n", cmd);
  2063. status = 0;
  2064. break;
  2065. case CMD_CONNECTION_LOST:
  2066. printk(KERN_WARNING "cciss: cmd %p had "
  2067. "connection lost\n", cmd);
  2068. status=0;
  2069. break;
  2070. case CMD_ABORTED:
  2071. printk(KERN_WARNING "cciss: cmd %p was "
  2072. "aborted\n", cmd);
  2073. status=0;
  2074. break;
  2075. case CMD_ABORT_FAILED:
  2076. printk(KERN_WARNING "cciss: cmd %p reports "
  2077. "abort failed\n", cmd);
  2078. status=0;
  2079. break;
  2080. case CMD_UNSOLICITED_ABORT:
  2081. printk(KERN_WARNING "cciss%d: unsolicited "
  2082. "abort %p\n", h->ctlr, cmd);
  2083. if (cmd->retry_count < MAX_CMD_RETRIES) {
  2084. retry_cmd=1;
  2085. printk(KERN_WARNING
  2086. "cciss%d: retrying %p\n",
  2087. h->ctlr, cmd);
  2088. cmd->retry_count++;
  2089. } else
  2090. printk(KERN_WARNING
  2091. "cciss%d: %p retried too "
  2092. "many times\n", h->ctlr, cmd);
  2093. status=0;
  2094. break;
  2095. case CMD_TIMEOUT:
  2096. printk(KERN_WARNING "cciss: cmd %p timedout\n",
  2097. cmd);
  2098. status=0;
  2099. break;
  2100. default:
  2101. printk(KERN_WARNING "cciss: cmd %p returned "
  2102. "unknown status %x\n", cmd,
  2103. cmd->err_info->CommandStatus);
  2104. status=0;
  2105. }
  2106. }
  2107. /* We need to return this command */
  2108. if(retry_cmd) {
  2109. resend_cciss_cmd(h,cmd);
  2110. return;
  2111. }
  2112. cmd->rq->completion_data = cmd;
  2113. cmd->rq->errors = status;
  2114. blk_complete_request(cmd->rq);
  2115. }
  2116. /*
  2117. * Get a request and submit it to the controller.
  2118. */
  2119. static void do_cciss_request(request_queue_t *q)
  2120. {
  2121. ctlr_info_t *h= q->queuedata;
  2122. CommandList_struct *c;
  2123. int start_blk, seg;
  2124. struct request *creq;
  2125. u64bit temp64;
  2126. struct scatterlist tmp_sg[MAXSGENTRIES];
  2127. drive_info_struct *drv;
  2128. int i, dir;
  2129. /* We call start_io here in case there is a command waiting on the
  2130. * queue that has not been sent.
  2131. */
  2132. if (blk_queue_plugged(q))
  2133. goto startio;
  2134. queue:
  2135. creq = elv_next_request(q);
  2136. if (!creq)
  2137. goto startio;
  2138. if (creq->nr_phys_segments > MAXSGENTRIES)
  2139. BUG();
  2140. if (( c = cmd_alloc(h, 1)) == NULL)
  2141. goto full;
  2142. blkdev_dequeue_request(creq);
  2143. spin_unlock_irq(q->queue_lock);
  2144. c->cmd_type = CMD_RWREQ;
  2145. c->rq = creq;
  2146. /* fill in the request */
  2147. drv = creq->rq_disk->private_data;
  2148. c->Header.ReplyQueue = 0; // unused in simple mode
  2149. /* got command from pool, so use the command block index instead */
  2150. /* for direct lookups. */
  2151. /* The first 2 bits are reserved for controller error reporting. */
  2152. c->Header.Tag.lower = (c->cmdindex << 3);
  2153. c->Header.Tag.lower |= 0x04; /* flag for direct lookup. */
  2154. c->Header.LUN.LogDev.VolId= drv->LunID;
  2155. c->Header.LUN.LogDev.Mode = 1;
  2156. c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
  2157. c->Request.Type.Type = TYPE_CMD; // It is a command.
  2158. c->Request.Type.Attribute = ATTR_SIMPLE;
  2159. c->Request.Type.Direction =
  2160. (rq_data_dir(creq) == READ) ? XFER_READ: XFER_WRITE;
  2161. c->Request.Timeout = 0; // Don't time out
  2162. c->Request.CDB[0] = (rq_data_dir(creq) == READ) ? CCISS_READ : CCISS_WRITE;
  2163. start_blk = creq->sector;
  2164. #ifdef CCISS_DEBUG
  2165. printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n",(int) creq->sector,
  2166. (int) creq->nr_sectors);
  2167. #endif /* CCISS_DEBUG */
  2168. seg = blk_rq_map_sg(q, creq, tmp_sg);
  2169. /* get the DMA records for the setup */
  2170. if (c->Request.Type.Direction == XFER_READ)
  2171. dir = PCI_DMA_FROMDEVICE;
  2172. else
  2173. dir = PCI_DMA_TODEVICE;
  2174. for (i=0; i<seg; i++)
  2175. {
  2176. c->SG[i].Len = tmp_sg[i].length;
  2177. temp64.val = (__u64) pci_map_page(h->pdev, tmp_sg[i].page,
  2178. tmp_sg[i].offset, tmp_sg[i].length,
  2179. dir);
  2180. c->SG[i].Addr.lower = temp64.val32.lower;
  2181. c->SG[i].Addr.upper = temp64.val32.upper;
  2182. c->SG[i].Ext = 0; // we are not chaining
  2183. }
  2184. /* track how many SG entries we are using */
  2185. if( seg > h->maxSG)
  2186. h->maxSG = seg;
  2187. #ifdef CCISS_DEBUG
  2188. printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n", creq->nr_sectors, seg);
  2189. #endif /* CCISS_DEBUG */
  2190. c->Header.SGList = c->Header.SGTotal = seg;
  2191. c->Request.CDB[1]= 0;
  2192. c->Request.CDB[2]= (start_blk >> 24) & 0xff; //MSB
  2193. c->Request.CDB[3]= (start_blk >> 16) & 0xff;
  2194. c->Request.CDB[4]= (start_blk >> 8) & 0xff;
  2195. c->Request.CDB[5]= start_blk & 0xff;
  2196. c->Request.CDB[6]= 0; // (sect >> 24) & 0xff; MSB
  2197. c->Request.CDB[7]= (creq->nr_sectors >> 8) & 0xff;
  2198. c->Request.CDB[8]= creq->nr_sectors & 0xff;
  2199. c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
  2200. spin_lock_irq(q->queue_lock);
  2201. addQ(&(h->reqQ),c);
  2202. h->Qdepth++;
  2203. if(h->Qdepth > h->maxQsinceinit)
  2204. h->maxQsinceinit = h->Qdepth;
  2205. goto queue;
  2206. full:
  2207. blk_stop_queue(q);
  2208. startio:
  2209. /* We will already have the driver lock here so not need
  2210. * to lock it.
  2211. */
  2212. start_io(h);
  2213. }
  2214. static inline unsigned long get_next_completion(ctlr_info_t *h)
  2215. {
  2216. #ifdef CONFIG_CISS_SCSI_TAPE
  2217. /* Any rejects from sendcmd() lying around? Process them first */
  2218. if (h->scsi_rejects.ncompletions == 0)
  2219. return h->access.command_completed(h);
  2220. else {
  2221. struct sendcmd_reject_list *srl;
  2222. int n;
  2223. srl = &h->scsi_rejects;
  2224. n = --srl->ncompletions;
  2225. /* printk("cciss%d: processing saved reject\n", h->ctlr); */
  2226. printk("p");
  2227. return srl->complete[n];
  2228. }
  2229. #else
  2230. return h->access.command_completed(h);
  2231. #endif
  2232. }
  2233. static inline int interrupt_pending(ctlr_info_t *h)
  2234. {
  2235. #ifdef CONFIG_CISS_SCSI_TAPE
  2236. return ( h->access.intr_pending(h)
  2237. || (h->scsi_rejects.ncompletions > 0));
  2238. #else
  2239. return h->access.intr_pending(h);
  2240. #endif
  2241. }
  2242. static inline long interrupt_not_for_us(ctlr_info_t *h)
  2243. {
  2244. #ifdef CONFIG_CISS_SCSI_TAPE
  2245. return (((h->access.intr_pending(h) == 0) ||
  2246. (h->interrupts_enabled == 0))
  2247. && (h->scsi_rejects.ncompletions == 0));
  2248. #else
  2249. return (((h->access.intr_pending(h) == 0) ||
  2250. (h->interrupts_enabled == 0)));
  2251. #endif
  2252. }
  2253. static irqreturn_t do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs)
  2254. {
  2255. ctlr_info_t *h = dev_id;
  2256. CommandList_struct *c;
  2257. unsigned long flags;
  2258. __u32 a, a1, a2;
  2259. int j;
  2260. int start_queue = h->next_to_run;
  2261. if (interrupt_not_for_us(h))
  2262. return IRQ_NONE;
  2263. /*
  2264. * If there are completed commands in the completion queue,
  2265. * we had better do something about it.
  2266. */
  2267. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  2268. while (interrupt_pending(h)) {
  2269. while((a = get_next_completion(h)) != FIFO_EMPTY) {
  2270. a1 = a;
  2271. if ((a & 0x04)) {
  2272. a2 = (a >> 3);
  2273. if (a2 >= NR_CMDS) {
  2274. printk(KERN_WARNING "cciss: controller cciss%d failed, stopping.\n", h->ctlr);
  2275. fail_all_cmds(h->ctlr);
  2276. return IRQ_HANDLED;
  2277. }
  2278. c = h->cmd_pool + a2;
  2279. a = c->busaddr;
  2280. } else {
  2281. a &= ~3;
  2282. if ((c = h->cmpQ) == NULL) {
  2283. printk(KERN_WARNING "cciss: Completion of %08x ignored\n", a1);
  2284. continue;
  2285. }
  2286. while(c->busaddr != a) {
  2287. c = c->next;
  2288. if (c == h->cmpQ)
  2289. break;
  2290. }
  2291. }
  2292. /*
  2293. * If we've found the command, take it off the
  2294. * completion Q and free it
  2295. */
  2296. if (c->busaddr == a) {
  2297. removeQ(&h->cmpQ, c);
  2298. if (c->cmd_type == CMD_RWREQ) {
  2299. complete_command(h, c, 0);
  2300. } else if (c->cmd_type == CMD_IOCTL_PEND) {
  2301. complete(c->waiting);
  2302. }
  2303. # ifdef CONFIG_CISS_SCSI_TAPE
  2304. else if (c->cmd_type == CMD_SCSI)
  2305. complete_scsi_command(c, 0, a1);
  2306. # endif
  2307. continue;
  2308. }
  2309. }
  2310. }
  2311. /* check to see if we have maxed out the number of commands that can
  2312. * be placed on the queue. If so then exit. We do this check here
  2313. * in case the interrupt we serviced was from an ioctl and did not
  2314. * free any new commands.
  2315. */
  2316. if ((find_first_zero_bit(h->cmd_pool_bits, NR_CMDS)) == NR_CMDS)
  2317. goto cleanup;
  2318. /* We have room on the queue for more commands. Now we need to queue
  2319. * them up. We will also keep track of the next queue to run so
  2320. * that every queue gets a chance to be started first.
  2321. */
  2322. for (j=0; j < h->highest_lun + 1; j++){
  2323. int curr_queue = (start_queue + j) % (h->highest_lun + 1);
  2324. /* make sure the disk has been added and the drive is real
  2325. * because this can be called from the middle of init_one.
  2326. */
  2327. if(!(h->drv[curr_queue].queue) ||
  2328. !(h->drv[curr_queue].heads))
  2329. continue;
  2330. blk_start_queue(h->gendisk[curr_queue]->queue);
  2331. /* check to see if we have maxed out the number of commands
  2332. * that can be placed on the queue.
  2333. */
  2334. if ((find_first_zero_bit(h->cmd_pool_bits, NR_CMDS)) == NR_CMDS)
  2335. {
  2336. if (curr_queue == start_queue){
  2337. h->next_to_run = (start_queue + 1) % (h->highest_lun + 1);
  2338. goto cleanup;
  2339. } else {
  2340. h->next_to_run = curr_queue;
  2341. goto cleanup;
  2342. }
  2343. } else {
  2344. curr_queue = (curr_queue + 1) % (h->highest_lun + 1);
  2345. }
  2346. }
  2347. cleanup:
  2348. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  2349. return IRQ_HANDLED;
  2350. }
  2351. /*
  2352. * We cannot read the structure directly, for portablity we must use
  2353. * the io functions.
  2354. * This is for debug only.
  2355. */
  2356. #ifdef CCISS_DEBUG
  2357. static void print_cfg_table( CfgTable_struct *tb)
  2358. {
  2359. int i;
  2360. char temp_name[17];
  2361. printk("Controller Configuration information\n");
  2362. printk("------------------------------------\n");
  2363. for(i=0;i<4;i++)
  2364. temp_name[i] = readb(&(tb->Signature[i]));
  2365. temp_name[4]='\0';
  2366. printk(" Signature = %s\n", temp_name);
  2367. printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
  2368. printk(" Transport methods supported = 0x%x\n",
  2369. readl(&(tb-> TransportSupport)));
  2370. printk(" Transport methods active = 0x%x\n",
  2371. readl(&(tb->TransportActive)));
  2372. printk(" Requested transport Method = 0x%x\n",
  2373. readl(&(tb->HostWrite.TransportRequest)));
  2374. printk(" Coalese Interrupt Delay = 0x%x\n",
  2375. readl(&(tb->HostWrite.CoalIntDelay)));
  2376. printk(" Coalese Interrupt Count = 0x%x\n",
  2377. readl(&(tb->HostWrite.CoalIntCount)));
  2378. printk(" Max outstanding commands = 0x%d\n",
  2379. readl(&(tb->CmdsOutMax)));
  2380. printk(" Bus Types = 0x%x\n", readl(&(tb-> BusTypes)));
  2381. for(i=0;i<16;i++)
  2382. temp_name[i] = readb(&(tb->ServerName[i]));
  2383. temp_name[16] = '\0';
  2384. printk(" Server Name = %s\n", temp_name);
  2385. printk(" Heartbeat Counter = 0x%x\n\n\n",
  2386. readl(&(tb->HeartBeat)));
  2387. }
  2388. #endif /* CCISS_DEBUG */
  2389. static void release_io_mem(ctlr_info_t *c)
  2390. {
  2391. /* if IO mem was not protected do nothing */
  2392. if( c->io_mem_addr == 0)
  2393. return;
  2394. release_region(c->io_mem_addr, c->io_mem_length);
  2395. c->io_mem_addr = 0;
  2396. c->io_mem_length = 0;
  2397. }
  2398. static int find_PCI_BAR_index(struct pci_dev *pdev,
  2399. unsigned long pci_bar_addr)
  2400. {
  2401. int i, offset, mem_type, bar_type;
  2402. if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
  2403. return 0;
  2404. offset = 0;
  2405. for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
  2406. bar_type = pci_resource_flags(pdev, i) &
  2407. PCI_BASE_ADDRESS_SPACE;
  2408. if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
  2409. offset += 4;
  2410. else {
  2411. mem_type = pci_resource_flags(pdev, i) &
  2412. PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  2413. switch (mem_type) {
  2414. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  2415. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  2416. offset += 4; /* 32 bit */
  2417. break;
  2418. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  2419. offset += 8;
  2420. break;
  2421. default: /* reserved in PCI 2.2 */
  2422. printk(KERN_WARNING "Base address is invalid\n");
  2423. return -1;
  2424. break;
  2425. }
  2426. }
  2427. if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
  2428. return i+1;
  2429. }
  2430. return -1;
  2431. }
  2432. /* If MSI/MSI-X is supported by the kernel we will try to enable it on
  2433. * controllers that are capable. If not, we use IO-APIC mode.
  2434. */
  2435. static void __devinit cciss_interrupt_mode(ctlr_info_t *c, struct pci_dev *pdev, __u32 board_id)
  2436. {
  2437. #ifdef CONFIG_PCI_MSI
  2438. int err;
  2439. struct msix_entry cciss_msix_entries[4] = {{0,0}, {0,1},
  2440. {0,2}, {0,3}};
  2441. /* Some boards advertise MSI but don't really support it */
  2442. if ((board_id == 0x40700E11) ||
  2443. (board_id == 0x40800E11) ||
  2444. (board_id == 0x40820E11) ||
  2445. (board_id == 0x40830E11))
  2446. goto default_int_mode;
  2447. if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
  2448. err = pci_enable_msix(pdev, cciss_msix_entries, 4);
  2449. if (!err) {
  2450. c->intr[0] = cciss_msix_entries[0].vector;
  2451. c->intr[1] = cciss_msix_entries[1].vector;
  2452. c->intr[2] = cciss_msix_entries[2].vector;
  2453. c->intr[3] = cciss_msix_entries[3].vector;
  2454. c->msix_vector = 1;
  2455. return;
  2456. }
  2457. if (err > 0) {
  2458. printk(KERN_WARNING "cciss: only %d MSI-X vectors "
  2459. "available\n", err);
  2460. } else {
  2461. printk(KERN_WARNING "cciss: MSI-X init failed %d\n",
  2462. err);
  2463. }
  2464. }
  2465. if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
  2466. if (!pci_enable_msi(pdev)) {
  2467. c->intr[SIMPLE_MODE_INT] = pdev->irq;
  2468. c->msi_vector = 1;
  2469. return;
  2470. } else {
  2471. printk(KERN_WARNING "cciss: MSI init failed\n");
  2472. c->intr[SIMPLE_MODE_INT] = pdev->irq;
  2473. return;
  2474. }
  2475. }
  2476. #endif /* CONFIG_PCI_MSI */
  2477. /* if we get here we're going to use the default interrupt mode */
  2478. default_int_mode:
  2479. c->intr[SIMPLE_MODE_INT] = pdev->irq;
  2480. return;
  2481. }
  2482. static int cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
  2483. {
  2484. ushort subsystem_vendor_id, subsystem_device_id, command;
  2485. __u32 board_id, scratchpad = 0;
  2486. __u64 cfg_offset;
  2487. __u32 cfg_base_addr;
  2488. __u64 cfg_base_addr_index;
  2489. int i;
  2490. /* check to see if controller has been disabled */
  2491. /* BEFORE trying to enable it */
  2492. (void) pci_read_config_word(pdev, PCI_COMMAND,&command);
  2493. if(!(command & 0x02))
  2494. {
  2495. printk(KERN_WARNING "cciss: controller appears to be disabled\n");
  2496. return(-1);
  2497. }
  2498. if (pci_enable_device(pdev))
  2499. {
  2500. printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
  2501. return( -1);
  2502. }
  2503. subsystem_vendor_id = pdev->subsystem_vendor;
  2504. subsystem_device_id = pdev->subsystem_device;
  2505. board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
  2506. subsystem_vendor_id);
  2507. /* search for our IO range so we can protect it */
  2508. for(i=0; i<DEVICE_COUNT_RESOURCE; i++)
  2509. {
  2510. /* is this an IO range */
  2511. if( pci_resource_flags(pdev, i) & 0x01 ) {
  2512. c->io_mem_addr = pci_resource_start(pdev, i);
  2513. c->io_mem_length = pci_resource_end(pdev, i) -
  2514. pci_resource_start(pdev, i) +1;
  2515. #ifdef CCISS_DEBUG
  2516. printk("IO value found base_addr[%d] %lx %lx\n", i,
  2517. c->io_mem_addr, c->io_mem_length);
  2518. #endif /* CCISS_DEBUG */
  2519. /* register the IO range */
  2520. if(!request_region( c->io_mem_addr,
  2521. c->io_mem_length, "cciss"))
  2522. {
  2523. printk(KERN_WARNING "cciss I/O memory range already in use addr=%lx length=%ld\n",
  2524. c->io_mem_addr, c->io_mem_length);
  2525. c->io_mem_addr= 0;
  2526. c->io_mem_length = 0;
  2527. }
  2528. break;
  2529. }
  2530. }
  2531. #ifdef CCISS_DEBUG
  2532. printk("command = %x\n", command);
  2533. printk("irq = %x\n", pdev->irq);
  2534. printk("board_id = %x\n", board_id);
  2535. #endif /* CCISS_DEBUG */
  2536. /* If the kernel supports MSI/MSI-X we will try to enable that functionality,
  2537. * else we use the IO-APIC interrupt assigned to us by system ROM.
  2538. */
  2539. cciss_interrupt_mode(c, pdev, board_id);
  2540. /*
  2541. * Memory base addr is first addr , the second points to the config
  2542. * table
  2543. */
  2544. c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
  2545. #ifdef CCISS_DEBUG
  2546. printk("address 0 = %x\n", c->paddr);
  2547. #endif /* CCISS_DEBUG */
  2548. c->vaddr = remap_pci_mem(c->paddr, 200);
  2549. /* Wait for the board to become ready. (PCI hotplug needs this.)
  2550. * We poll for up to 120 secs, once per 100ms. */
  2551. for (i=0; i < 1200; i++) {
  2552. scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
  2553. if (scratchpad == CCISS_FIRMWARE_READY)
  2554. break;
  2555. set_current_state(TASK_INTERRUPTIBLE);
  2556. schedule_timeout(HZ / 10); /* wait 100ms */
  2557. }
  2558. if (scratchpad != CCISS_FIRMWARE_READY) {
  2559. printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
  2560. return -1;
  2561. }
  2562. /* get the address index number */
  2563. cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
  2564. cfg_base_addr &= (__u32) 0x0000ffff;
  2565. #ifdef CCISS_DEBUG
  2566. printk("cfg base address = %x\n", cfg_base_addr);
  2567. #endif /* CCISS_DEBUG */
  2568. cfg_base_addr_index =
  2569. find_PCI_BAR_index(pdev, cfg_base_addr);
  2570. #ifdef CCISS_DEBUG
  2571. printk("cfg base address index = %x\n", cfg_base_addr_index);
  2572. #endif /* CCISS_DEBUG */
  2573. if (cfg_base_addr_index == -1) {
  2574. printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
  2575. release_io_mem(c);
  2576. return -1;
  2577. }
  2578. cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
  2579. #ifdef CCISS_DEBUG
  2580. printk("cfg offset = %x\n", cfg_offset);
  2581. #endif /* CCISS_DEBUG */
  2582. c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
  2583. cfg_base_addr_index) + cfg_offset,
  2584. sizeof(CfgTable_struct));
  2585. c->board_id = board_id;
  2586. #ifdef CCISS_DEBUG
  2587. print_cfg_table(c->cfgtable);
  2588. #endif /* CCISS_DEBUG */
  2589. for(i=0; i<NR_PRODUCTS; i++) {
  2590. if (board_id == products[i].board_id) {
  2591. c->product_name = products[i].product_name;
  2592. c->access = *(products[i].access);
  2593. break;
  2594. }
  2595. }
  2596. if (i == NR_PRODUCTS) {
  2597. printk(KERN_WARNING "cciss: Sorry, I don't know how"
  2598. " to access the Smart Array controller %08lx\n",
  2599. (unsigned long)board_id);
  2600. return -1;
  2601. }
  2602. if ( (readb(&c->cfgtable->Signature[0]) != 'C') ||
  2603. (readb(&c->cfgtable->Signature[1]) != 'I') ||
  2604. (readb(&c->cfgtable->Signature[2]) != 'S') ||
  2605. (readb(&c->cfgtable->Signature[3]) != 'S') )
  2606. {
  2607. printk("Does not appear to be a valid CISS config table\n");
  2608. return -1;
  2609. }
  2610. #ifdef CONFIG_X86
  2611. {
  2612. /* Need to enable prefetch in the SCSI core for 6400 in x86 */
  2613. __u32 prefetch;
  2614. prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
  2615. prefetch |= 0x100;
  2616. writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
  2617. }
  2618. #endif
  2619. #ifdef CCISS_DEBUG
  2620. printk("Trying to put board into Simple mode\n");
  2621. #endif /* CCISS_DEBUG */
  2622. c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
  2623. /* Update the field, and then ring the doorbell */
  2624. writel( CFGTBL_Trans_Simple,
  2625. &(c->cfgtable->HostWrite.TransportRequest));
  2626. writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
  2627. /* under certain very rare conditions, this can take awhile.
  2628. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  2629. * as we enter this code.) */
  2630. for(i=0;i<MAX_CONFIG_WAIT;i++) {
  2631. if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
  2632. break;
  2633. /* delay and try again */
  2634. set_current_state(TASK_INTERRUPTIBLE);
  2635. schedule_timeout(10);
  2636. }
  2637. #ifdef CCISS_DEBUG
  2638. printk(KERN_DEBUG "I counter got to %d %x\n", i, readl(c->vaddr + SA5_DOORBELL));
  2639. #endif /* CCISS_DEBUG */
  2640. #ifdef CCISS_DEBUG
  2641. print_cfg_table(c->cfgtable);
  2642. #endif /* CCISS_DEBUG */
  2643. if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
  2644. {
  2645. printk(KERN_WARNING "cciss: unable to get board into"
  2646. " simple mode\n");
  2647. return -1;
  2648. }
  2649. return 0;
  2650. }
  2651. /*
  2652. * Gets information about the local volumes attached to the controller.
  2653. */
  2654. static void cciss_getgeometry(int cntl_num)
  2655. {
  2656. ReportLunData_struct *ld_buff;
  2657. ReadCapdata_struct *size_buff;
  2658. InquiryData_struct *inq_buff;
  2659. int return_code;
  2660. int i;
  2661. int listlength = 0;
  2662. __u32 lunid = 0;
  2663. int block_size;
  2664. int total_size;
  2665. ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
  2666. if (ld_buff == NULL)
  2667. {
  2668. printk(KERN_ERR "cciss: out of memory\n");
  2669. return;
  2670. }
  2671. memset(ld_buff, 0, sizeof(ReportLunData_struct));
  2672. size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
  2673. if (size_buff == NULL)
  2674. {
  2675. printk(KERN_ERR "cciss: out of memory\n");
  2676. kfree(ld_buff);
  2677. return;
  2678. }
  2679. inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
  2680. if (inq_buff == NULL)
  2681. {
  2682. printk(KERN_ERR "cciss: out of memory\n");
  2683. kfree(ld_buff);
  2684. kfree(size_buff);
  2685. return;
  2686. }
  2687. /* Get the firmware version */
  2688. return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
  2689. sizeof(InquiryData_struct), 0, 0 ,0, NULL, TYPE_CMD);
  2690. if (return_code == IO_OK)
  2691. {
  2692. hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
  2693. hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
  2694. hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
  2695. hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
  2696. } else /* send command failed */
  2697. {
  2698. printk(KERN_WARNING "cciss: unable to determine firmware"
  2699. " version of controller\n");
  2700. }
  2701. /* Get the number of logical volumes */
  2702. return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
  2703. sizeof(ReportLunData_struct), 0, 0, 0, NULL, TYPE_CMD);
  2704. if( return_code == IO_OK)
  2705. {
  2706. #ifdef CCISS_DEBUG
  2707. printk("LUN Data\n--------------------------\n");
  2708. #endif /* CCISS_DEBUG */
  2709. listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
  2710. listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
  2711. listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
  2712. listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
  2713. } else /* reading number of logical volumes failed */
  2714. {
  2715. printk(KERN_WARNING "cciss: report logical volume"
  2716. " command failed\n");
  2717. listlength = 0;
  2718. }
  2719. hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
  2720. if (hba[cntl_num]->num_luns > CISS_MAX_LUN)
  2721. {
  2722. printk(KERN_ERR "ciss: only %d number of logical volumes supported\n",
  2723. CISS_MAX_LUN);
  2724. hba[cntl_num]->num_luns = CISS_MAX_LUN;
  2725. }
  2726. #ifdef CCISS_DEBUG
  2727. printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
  2728. ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
  2729. ld_buff->LUNListLength[3], hba[cntl_num]->num_luns);
  2730. #endif /* CCISS_DEBUG */
  2731. hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns-1;
  2732. // for(i=0; i< hba[cntl_num]->num_luns; i++)
  2733. for(i=0; i < CISS_MAX_LUN; i++)
  2734. {
  2735. if (i < hba[cntl_num]->num_luns){
  2736. lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3]))
  2737. << 24;
  2738. lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2]))
  2739. << 16;
  2740. lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1]))
  2741. << 8;
  2742. lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
  2743. hba[cntl_num]->drv[i].LunID = lunid;
  2744. #ifdef CCISS_DEBUG
  2745. printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
  2746. ld_buff->LUN[i][0], ld_buff->LUN[i][1],
  2747. ld_buff->LUN[i][2], ld_buff->LUN[i][3],
  2748. hba[cntl_num]->drv[i].LunID);
  2749. #endif /* CCISS_DEBUG */
  2750. cciss_read_capacity(cntl_num, i, size_buff, 0,
  2751. &total_size, &block_size);
  2752. cciss_geometry_inquiry(cntl_num, i, 0, total_size,
  2753. block_size, inq_buff, &hba[cntl_num]->drv[i]);
  2754. } else {
  2755. /* initialize raid_level to indicate a free space */
  2756. hba[cntl_num]->drv[i].raid_level = -1;
  2757. }
  2758. }
  2759. kfree(ld_buff);
  2760. kfree(size_buff);
  2761. kfree(inq_buff);
  2762. }
  2763. /* Function to find the first free pointer into our hba[] array */
  2764. /* Returns -1 if no free entries are left. */
  2765. static int alloc_cciss_hba(void)
  2766. {
  2767. struct gendisk *disk[NWD];
  2768. int i, n;
  2769. for (n = 0; n < NWD; n++) {
  2770. disk[n] = alloc_disk(1 << NWD_SHIFT);
  2771. if (!disk[n])
  2772. goto out;
  2773. }
  2774. for(i=0; i< MAX_CTLR; i++) {
  2775. if (!hba[i]) {
  2776. ctlr_info_t *p;
  2777. p = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
  2778. if (!p)
  2779. goto Enomem;
  2780. memset(p, 0, sizeof(ctlr_info_t));
  2781. for (n = 0; n < NWD; n++)
  2782. p->gendisk[n] = disk[n];
  2783. hba[i] = p;
  2784. return i;
  2785. }
  2786. }
  2787. printk(KERN_WARNING "cciss: This driver supports a maximum"
  2788. " of %d controllers.\n", MAX_CTLR);
  2789. goto out;
  2790. Enomem:
  2791. printk(KERN_ERR "cciss: out of memory.\n");
  2792. out:
  2793. while (n--)
  2794. put_disk(disk[n]);
  2795. return -1;
  2796. }
  2797. static void free_hba(int i)
  2798. {
  2799. ctlr_info_t *p = hba[i];
  2800. int n;
  2801. hba[i] = NULL;
  2802. for (n = 0; n < NWD; n++)
  2803. put_disk(p->gendisk[n]);
  2804. kfree(p);
  2805. }
  2806. /*
  2807. * This is it. Find all the controllers and register them. I really hate
  2808. * stealing all these major device numbers.
  2809. * returns the number of block devices registered.
  2810. */
  2811. static int __devinit cciss_init_one(struct pci_dev *pdev,
  2812. const struct pci_device_id *ent)
  2813. {
  2814. request_queue_t *q;
  2815. int i;
  2816. int j;
  2817. int rc;
  2818. printk(KERN_DEBUG "cciss: Device 0x%x has been found at"
  2819. " bus %d dev %d func %d\n",
  2820. pdev->device, pdev->bus->number, PCI_SLOT(pdev->devfn),
  2821. PCI_FUNC(pdev->devfn));
  2822. i = alloc_cciss_hba();
  2823. if(i < 0)
  2824. return (-1);
  2825. hba[i]->busy_initializing = 1;
  2826. if (cciss_pci_init(hba[i], pdev) != 0)
  2827. goto clean1;
  2828. sprintf(hba[i]->devname, "cciss%d", i);
  2829. hba[i]->ctlr = i;
  2830. hba[i]->pdev = pdev;
  2831. /* configure PCI DMA stuff */
  2832. if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK))
  2833. printk("cciss: using DAC cycles\n");
  2834. else if (!pci_set_dma_mask(pdev, DMA_32BIT_MASK))
  2835. printk("cciss: not using DAC cycles\n");
  2836. else {
  2837. printk("cciss: no suitable DMA available\n");
  2838. goto clean1;
  2839. }
  2840. /*
  2841. * register with the major number, or get a dynamic major number
  2842. * by passing 0 as argument. This is done for greater than
  2843. * 8 controller support.
  2844. */
  2845. if (i < MAX_CTLR_ORIG)
  2846. hba[i]->major = COMPAQ_CISS_MAJOR + i;
  2847. rc = register_blkdev(hba[i]->major, hba[i]->devname);
  2848. if(rc == -EBUSY || rc == -EINVAL) {
  2849. printk(KERN_ERR
  2850. "cciss: Unable to get major number %d for %s "
  2851. "on hba %d\n", hba[i]->major, hba[i]->devname, i);
  2852. goto clean1;
  2853. }
  2854. else {
  2855. if (i >= MAX_CTLR_ORIG)
  2856. hba[i]->major = rc;
  2857. }
  2858. /* make sure the board interrupts are off */
  2859. hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
  2860. if( request_irq(hba[i]->intr[SIMPLE_MODE_INT], do_cciss_intr,
  2861. SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
  2862. hba[i]->devname, hba[i])) {
  2863. printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
  2864. hba[i]->intr[SIMPLE_MODE_INT], hba[i]->devname);
  2865. goto clean2;
  2866. }
  2867. hba[i]->cmd_pool_bits = kmalloc(((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long), GFP_KERNEL);
  2868. hba[i]->cmd_pool = (CommandList_struct *)pci_alloc_consistent(
  2869. hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
  2870. &(hba[i]->cmd_pool_dhandle));
  2871. hba[i]->errinfo_pool = (ErrorInfo_struct *)pci_alloc_consistent(
  2872. hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
  2873. &(hba[i]->errinfo_pool_dhandle));
  2874. if((hba[i]->cmd_pool_bits == NULL)
  2875. || (hba[i]->cmd_pool == NULL)
  2876. || (hba[i]->errinfo_pool == NULL)) {
  2877. printk( KERN_ERR "cciss: out of memory");
  2878. goto clean4;
  2879. }
  2880. #ifdef CONFIG_CISS_SCSI_TAPE
  2881. hba[i]->scsi_rejects.complete =
  2882. kmalloc(sizeof(hba[i]->scsi_rejects.complete[0]) *
  2883. (NR_CMDS + 5), GFP_KERNEL);
  2884. if (hba[i]->scsi_rejects.complete == NULL) {
  2885. printk( KERN_ERR "cciss: out of memory");
  2886. goto clean4;
  2887. }
  2888. #endif
  2889. spin_lock_init(&hba[i]->lock);
  2890. /* Initialize the pdev driver private data.
  2891. have it point to hba[i]. */
  2892. pci_set_drvdata(pdev, hba[i]);
  2893. /* command and error info recs zeroed out before
  2894. they are used */
  2895. memset(hba[i]->cmd_pool_bits, 0, ((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long));
  2896. #ifdef CCISS_DEBUG
  2897. printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n",i);
  2898. #endif /* CCISS_DEBUG */
  2899. cciss_getgeometry(i);
  2900. cciss_scsi_setup(i);
  2901. /* Turn the interrupts on so we can service requests */
  2902. hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
  2903. cciss_procinit(i);
  2904. hba[i]->busy_initializing = 0;
  2905. for(j=0; j < NWD; j++) { /* mfm */
  2906. drive_info_struct *drv = &(hba[i]->drv[j]);
  2907. struct gendisk *disk = hba[i]->gendisk[j];
  2908. q = blk_init_queue(do_cciss_request, &hba[i]->lock);
  2909. if (!q) {
  2910. printk(KERN_ERR
  2911. "cciss: unable to allocate queue for disk %d\n",
  2912. j);
  2913. break;
  2914. }
  2915. drv->queue = q;
  2916. q->backing_dev_info.ra_pages = READ_AHEAD;
  2917. blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
  2918. /* This is a hardware imposed limit. */
  2919. blk_queue_max_hw_segments(q, MAXSGENTRIES);
  2920. /* This is a limit in the driver and could be eliminated. */
  2921. blk_queue_max_phys_segments(q, MAXSGENTRIES);
  2922. blk_queue_max_sectors(q, 512);
  2923. blk_queue_softirq_done(q, cciss_softirq_done);
  2924. q->queuedata = hba[i];
  2925. sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
  2926. sprintf(disk->devfs_name, "cciss/host%d/target%d", i, j);
  2927. disk->major = hba[i]->major;
  2928. disk->first_minor = j << NWD_SHIFT;
  2929. disk->fops = &cciss_fops;
  2930. disk->queue = q;
  2931. disk->private_data = drv;
  2932. /* we must register the controller even if no disks exist */
  2933. /* this is for the online array utilities */
  2934. if(!drv->heads && j)
  2935. continue;
  2936. blk_queue_hardsect_size(q, drv->block_size);
  2937. set_capacity(disk, drv->nr_blocks);
  2938. add_disk(disk);
  2939. }
  2940. return(1);
  2941. clean4:
  2942. #ifdef CONFIG_CISS_SCSI_TAPE
  2943. if(hba[i]->scsi_rejects.complete)
  2944. kfree(hba[i]->scsi_rejects.complete);
  2945. #endif
  2946. kfree(hba[i]->cmd_pool_bits);
  2947. if(hba[i]->cmd_pool)
  2948. pci_free_consistent(hba[i]->pdev,
  2949. NR_CMDS * sizeof(CommandList_struct),
  2950. hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
  2951. if(hba[i]->errinfo_pool)
  2952. pci_free_consistent(hba[i]->pdev,
  2953. NR_CMDS * sizeof( ErrorInfo_struct),
  2954. hba[i]->errinfo_pool,
  2955. hba[i]->errinfo_pool_dhandle);
  2956. free_irq(hba[i]->intr[SIMPLE_MODE_INT], hba[i]);
  2957. clean2:
  2958. unregister_blkdev(hba[i]->major, hba[i]->devname);
  2959. clean1:
  2960. release_io_mem(hba[i]);
  2961. free_hba(i);
  2962. hba[i]->busy_initializing = 0;
  2963. return(-1);
  2964. }
  2965. static void __devexit cciss_remove_one (struct pci_dev *pdev)
  2966. {
  2967. ctlr_info_t *tmp_ptr;
  2968. int i, j;
  2969. char flush_buf[4];
  2970. int return_code;
  2971. if (pci_get_drvdata(pdev) == NULL)
  2972. {
  2973. printk( KERN_ERR "cciss: Unable to remove device \n");
  2974. return;
  2975. }
  2976. tmp_ptr = pci_get_drvdata(pdev);
  2977. i = tmp_ptr->ctlr;
  2978. if (hba[i] == NULL)
  2979. {
  2980. printk(KERN_ERR "cciss: device appears to "
  2981. "already be removed \n");
  2982. return;
  2983. }
  2984. /* Turn board interrupts off and send the flush cache command */
  2985. /* sendcmd will turn off interrupt, and send the flush...
  2986. * To write all data in the battery backed cache to disks */
  2987. memset(flush_buf, 0, 4);
  2988. return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
  2989. TYPE_CMD);
  2990. if(return_code != IO_OK)
  2991. {
  2992. printk(KERN_WARNING "Error Flushing cache on controller %d\n",
  2993. i);
  2994. }
  2995. free_irq(hba[i]->intr[2], hba[i]);
  2996. #ifdef CONFIG_PCI_MSI
  2997. if (hba[i]->msix_vector)
  2998. pci_disable_msix(hba[i]->pdev);
  2999. else if (hba[i]->msi_vector)
  3000. pci_disable_msi(hba[i]->pdev);
  3001. #endif /* CONFIG_PCI_MSI */
  3002. pci_set_drvdata(pdev, NULL);
  3003. iounmap(hba[i]->vaddr);
  3004. cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
  3005. unregister_blkdev(hba[i]->major, hba[i]->devname);
  3006. remove_proc_entry(hba[i]->devname, proc_cciss);
  3007. /* remove it from the disk list */
  3008. for (j = 0; j < NWD; j++) {
  3009. struct gendisk *disk = hba[i]->gendisk[j];
  3010. if (disk) {
  3011. request_queue_t *q = disk->queue;
  3012. if (disk->flags & GENHD_FL_UP)
  3013. del_gendisk(disk);
  3014. if (q)
  3015. blk_cleanup_queue(q);
  3016. }
  3017. }
  3018. pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
  3019. hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
  3020. pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
  3021. hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
  3022. kfree(hba[i]->cmd_pool_bits);
  3023. #ifdef CONFIG_CISS_SCSI_TAPE
  3024. kfree(hba[i]->scsi_rejects.complete);
  3025. #endif
  3026. release_io_mem(hba[i]);
  3027. free_hba(i);
  3028. }
  3029. static struct pci_driver cciss_pci_driver = {
  3030. .name = "cciss",
  3031. .probe = cciss_init_one,
  3032. .remove = __devexit_p(cciss_remove_one),
  3033. .id_table = cciss_pci_device_id, /* id_table */
  3034. };
  3035. /*
  3036. * This is it. Register the PCI driver information for the cards we control
  3037. * the OS will call our registered routines when it finds one of our cards.
  3038. */
  3039. static int __init cciss_init(void)
  3040. {
  3041. printk(KERN_INFO DRIVER_NAME "\n");
  3042. /* Register for our PCI devices */
  3043. return pci_register_driver(&cciss_pci_driver);
  3044. }
  3045. static void __exit cciss_cleanup(void)
  3046. {
  3047. int i;
  3048. pci_unregister_driver(&cciss_pci_driver);
  3049. /* double check that all controller entrys have been removed */
  3050. for (i=0; i< MAX_CTLR; i++)
  3051. {
  3052. if (hba[i] != NULL)
  3053. {
  3054. printk(KERN_WARNING "cciss: had to remove"
  3055. " controller %d\n", i);
  3056. cciss_remove_one(hba[i]->pdev);
  3057. }
  3058. }
  3059. remove_proc_entry("cciss", proc_root_driver);
  3060. }
  3061. static void fail_all_cmds(unsigned long ctlr)
  3062. {
  3063. /* If we get here, the board is apparently dead. */
  3064. ctlr_info_t *h = hba[ctlr];
  3065. CommandList_struct *c;
  3066. unsigned long flags;
  3067. printk(KERN_WARNING "cciss%d: controller not responding.\n", h->ctlr);
  3068. h->alive = 0; /* the controller apparently died... */
  3069. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  3070. pci_disable_device(h->pdev); /* Make sure it is really dead. */
  3071. /* move everything off the request queue onto the completed queue */
  3072. while( (c = h->reqQ) != NULL ) {
  3073. removeQ(&(h->reqQ), c);
  3074. h->Qdepth--;
  3075. addQ (&(h->cmpQ), c);
  3076. }
  3077. /* Now, fail everything on the completed queue with a HW error */
  3078. while( (c = h->cmpQ) != NULL ) {
  3079. removeQ(&h->cmpQ, c);
  3080. c->err_info->CommandStatus = CMD_HARDWARE_ERR;
  3081. if (c->cmd_type == CMD_RWREQ) {
  3082. complete_command(h, c, 0);
  3083. } else if (c->cmd_type == CMD_IOCTL_PEND)
  3084. complete(c->waiting);
  3085. #ifdef CONFIG_CISS_SCSI_TAPE
  3086. else if (c->cmd_type == CMD_SCSI)
  3087. complete_scsi_command(c, 0, 0);
  3088. #endif
  3089. }
  3090. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  3091. return;
  3092. }
  3093. module_init(cciss_init);
  3094. module_exit(cciss_cleanup);