cciss.c 105 KB

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