cciss.c 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580
  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/smp_lock.h>
  29. #include <linux/delay.h>
  30. #include <linux/major.h>
  31. #include <linux/fs.h>
  32. #include <linux/bio.h>
  33. #include <linux/blkpg.h>
  34. #include <linux/timer.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/init.h>
  38. #include <linux/jiffies.h>
  39. #include <linux/hdreg.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/compat.h>
  42. #include <linux/mutex.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/io.h>
  45. #include <linux/dma-mapping.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/genhd.h>
  48. #include <linux/completion.h>
  49. #include <scsi/scsi.h>
  50. #include <scsi/sg.h>
  51. #include <scsi/scsi_ioctl.h>
  52. #include <linux/cdrom.h>
  53. #include <linux/scatterlist.h>
  54. #include <linux/kthread.h>
  55. #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
  56. #define DRIVER_NAME "HP CISS Driver (v 3.6.20)"
  57. #define DRIVER_VERSION CCISS_DRIVER_VERSION(3, 6, 20)
  58. /* Embedded module documentation macros - see modules.h */
  59. MODULE_AUTHOR("Hewlett-Packard Company");
  60. MODULE_DESCRIPTION("Driver for HP Smart Array Controllers");
  61. MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
  62. " SA6i P600 P800 P400 P400i E200 E200i E500 P700m"
  63. " Smart Array G2 Series SAS/SATA Controllers");
  64. MODULE_VERSION("3.6.20");
  65. MODULE_LICENSE("GPL");
  66. static int cciss_allow_hpsa;
  67. module_param(cciss_allow_hpsa, int, S_IRUGO|S_IWUSR);
  68. MODULE_PARM_DESC(cciss_allow_hpsa,
  69. "Prevent cciss driver from accessing hardware known to be "
  70. " supported by the hpsa driver");
  71. #include "cciss_cmd.h"
  72. #include "cciss.h"
  73. #include <linux/cciss_ioctl.h>
  74. /* define the PCI info for the cards we can control */
  75. static const struct pci_device_id cciss_pci_device_id[] = {
  76. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS, 0x0E11, 0x4070},
  77. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4080},
  78. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4082},
  79. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4083},
  80. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x4091},
  81. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409A},
  82. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409B},
  83. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409C},
  84. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409D},
  85. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA, 0x103C, 0x3225},
  86. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3223},
  87. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3234},
  88. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3235},
  89. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3211},
  90. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3212},
  91. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3213},
  92. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3214},
  93. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3215},
  94. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3237},
  95. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x323D},
  96. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
  97. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
  98. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
  99. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
  100. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
  101. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
  102. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
  103. {0,}
  104. };
  105. MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
  106. /* board_id = Subsystem Device ID & Vendor ID
  107. * product = Marketing Name for the board
  108. * access = Address of the struct of function pointers
  109. */
  110. static struct board_type products[] = {
  111. {0x40700E11, "Smart Array 5300", &SA5_access},
  112. {0x40800E11, "Smart Array 5i", &SA5B_access},
  113. {0x40820E11, "Smart Array 532", &SA5B_access},
  114. {0x40830E11, "Smart Array 5312", &SA5B_access},
  115. {0x409A0E11, "Smart Array 641", &SA5_access},
  116. {0x409B0E11, "Smart Array 642", &SA5_access},
  117. {0x409C0E11, "Smart Array 6400", &SA5_access},
  118. {0x409D0E11, "Smart Array 6400 EM", &SA5_access},
  119. {0x40910E11, "Smart Array 6i", &SA5_access},
  120. {0x3225103C, "Smart Array P600", &SA5_access},
  121. {0x3235103C, "Smart Array P400i", &SA5_access},
  122. {0x3211103C, "Smart Array E200i", &SA5_access},
  123. {0x3212103C, "Smart Array E200", &SA5_access},
  124. {0x3213103C, "Smart Array E200i", &SA5_access},
  125. {0x3214103C, "Smart Array E200i", &SA5_access},
  126. {0x3215103C, "Smart Array E200i", &SA5_access},
  127. {0x3237103C, "Smart Array E500", &SA5_access},
  128. /* controllers below this line are also supported by the hpsa driver. */
  129. #define HPSA_BOUNDARY 0x3223103C
  130. {0x3223103C, "Smart Array P800", &SA5_access},
  131. {0x3234103C, "Smart Array P400", &SA5_access},
  132. {0x323D103C, "Smart Array P700m", &SA5_access},
  133. {0x3241103C, "Smart Array P212", &SA5_access},
  134. {0x3243103C, "Smart Array P410", &SA5_access},
  135. {0x3245103C, "Smart Array P410i", &SA5_access},
  136. {0x3247103C, "Smart Array P411", &SA5_access},
  137. {0x3249103C, "Smart Array P812", &SA5_access},
  138. {0x324A103C, "Smart Array P712m", &SA5_access},
  139. {0x324B103C, "Smart Array P711m", &SA5_access},
  140. };
  141. /* How long to wait (in milliseconds) for board to go into simple mode */
  142. #define MAX_CONFIG_WAIT 30000
  143. #define MAX_IOCTL_CONFIG_WAIT 1000
  144. /*define how many times we will try a command because of bus resets */
  145. #define MAX_CMD_RETRIES 3
  146. #define MAX_CTLR 32
  147. /* Originally cciss driver only supports 8 major numbers */
  148. #define MAX_CTLR_ORIG 8
  149. static ctlr_info_t *hba[MAX_CTLR];
  150. static struct task_struct *cciss_scan_thread;
  151. static DEFINE_MUTEX(scan_mutex);
  152. static LIST_HEAD(scan_q);
  153. static void do_cciss_request(struct request_queue *q);
  154. static irqreturn_t do_cciss_intr(int irq, void *dev_id);
  155. static int cciss_open(struct block_device *bdev, fmode_t mode);
  156. static int cciss_release(struct gendisk *disk, fmode_t mode);
  157. static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
  158. unsigned int cmd, unsigned long arg);
  159. static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo);
  160. static int cciss_revalidate(struct gendisk *disk);
  161. static int rebuild_lun_table(ctlr_info_t *h, int first_time, int via_ioctl);
  162. static int deregister_disk(ctlr_info_t *h, int drv_index,
  163. int clear_all, int via_ioctl);
  164. static void cciss_read_capacity(int ctlr, int logvol,
  165. sector_t *total_size, unsigned int *block_size);
  166. static void cciss_read_capacity_16(int ctlr, int logvol,
  167. sector_t *total_size, unsigned int *block_size);
  168. static void cciss_geometry_inquiry(int ctlr, int logvol,
  169. sector_t total_size,
  170. unsigned int block_size, InquiryData_struct *inq_buff,
  171. drive_info_struct *drv);
  172. static void __devinit cciss_interrupt_mode(ctlr_info_t *, struct pci_dev *,
  173. __u32);
  174. static void start_io(ctlr_info_t *h);
  175. static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
  176. __u8 page_code, unsigned char scsi3addr[],
  177. int cmd_type);
  178. static int sendcmd_withirq_core(ctlr_info_t *h, CommandList_struct *c,
  179. int attempt_retry);
  180. static int process_sendcmd_error(ctlr_info_t *h, CommandList_struct *c);
  181. static void fail_all_cmds(unsigned long ctlr);
  182. static int add_to_scan_list(struct ctlr_info *h);
  183. static int scan_thread(void *data);
  184. static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c);
  185. static void cciss_hba_release(struct device *dev);
  186. static void cciss_device_release(struct device *dev);
  187. static void cciss_free_gendisk(ctlr_info_t *h, int drv_index);
  188. static void cciss_free_drive_info(ctlr_info_t *h, int drv_index);
  189. #ifdef CONFIG_PROC_FS
  190. static void cciss_procinit(int i);
  191. #else
  192. static void cciss_procinit(int i)
  193. {
  194. }
  195. #endif /* CONFIG_PROC_FS */
  196. #ifdef CONFIG_COMPAT
  197. static int cciss_compat_ioctl(struct block_device *, fmode_t,
  198. unsigned, unsigned long);
  199. #endif
  200. static const struct block_device_operations cciss_fops = {
  201. .owner = THIS_MODULE,
  202. .open = cciss_open,
  203. .release = cciss_release,
  204. .locked_ioctl = cciss_ioctl,
  205. .getgeo = cciss_getgeo,
  206. #ifdef CONFIG_COMPAT
  207. .compat_ioctl = cciss_compat_ioctl,
  208. #endif
  209. .revalidate_disk = cciss_revalidate,
  210. };
  211. /*
  212. * Enqueuing and dequeuing functions for cmdlists.
  213. */
  214. static inline void addQ(struct hlist_head *list, CommandList_struct *c)
  215. {
  216. hlist_add_head(&c->list, list);
  217. }
  218. static inline void removeQ(CommandList_struct *c)
  219. {
  220. /*
  221. * After kexec/dump some commands might still
  222. * be in flight, which the firmware will try
  223. * to complete. Resetting the firmware doesn't work
  224. * with old fw revisions, so we have to mark
  225. * them off as 'stale' to prevent the driver from
  226. * falling over.
  227. */
  228. if (WARN_ON(hlist_unhashed(&c->list))) {
  229. c->cmd_type = CMD_MSG_STALE;
  230. return;
  231. }
  232. hlist_del_init(&c->list);
  233. }
  234. #include "cciss_scsi.c" /* For SCSI tape support */
  235. static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
  236. "UNKNOWN"
  237. };
  238. #define RAID_UNKNOWN (sizeof(raid_label) / sizeof(raid_label[0])-1)
  239. #ifdef CONFIG_PROC_FS
  240. /*
  241. * Report information about this controller.
  242. */
  243. #define ENG_GIG 1000000000
  244. #define ENG_GIG_FACTOR (ENG_GIG/512)
  245. #define ENGAGE_SCSI "engage scsi"
  246. static struct proc_dir_entry *proc_cciss;
  247. static void cciss_seq_show_header(struct seq_file *seq)
  248. {
  249. ctlr_info_t *h = seq->private;
  250. seq_printf(seq, "%s: HP %s Controller\n"
  251. "Board ID: 0x%08lx\n"
  252. "Firmware Version: %c%c%c%c\n"
  253. "IRQ: %d\n"
  254. "Logical drives: %d\n"
  255. "Current Q depth: %d\n"
  256. "Current # commands on controller: %d\n"
  257. "Max Q depth since init: %d\n"
  258. "Max # commands on controller since init: %d\n"
  259. "Max SG entries since init: %d\n",
  260. h->devname,
  261. h->product_name,
  262. (unsigned long)h->board_id,
  263. h->firm_ver[0], h->firm_ver[1], h->firm_ver[2],
  264. h->firm_ver[3], (unsigned int)h->intr[SIMPLE_MODE_INT],
  265. h->num_luns,
  266. h->Qdepth, h->commands_outstanding,
  267. h->maxQsinceinit, h->max_outstanding, h->maxSG);
  268. #ifdef CONFIG_CISS_SCSI_TAPE
  269. cciss_seq_tape_report(seq, h->ctlr);
  270. #endif /* CONFIG_CISS_SCSI_TAPE */
  271. }
  272. static void *cciss_seq_start(struct seq_file *seq, loff_t *pos)
  273. {
  274. ctlr_info_t *h = seq->private;
  275. unsigned ctlr = h->ctlr;
  276. unsigned long flags;
  277. /* prevent displaying bogus info during configuration
  278. * or deconfiguration of a logical volume
  279. */
  280. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  281. if (h->busy_configuring) {
  282. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  283. return ERR_PTR(-EBUSY);
  284. }
  285. h->busy_configuring = 1;
  286. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  287. if (*pos == 0)
  288. cciss_seq_show_header(seq);
  289. return pos;
  290. }
  291. static int cciss_seq_show(struct seq_file *seq, void *v)
  292. {
  293. sector_t vol_sz, vol_sz_frac;
  294. ctlr_info_t *h = seq->private;
  295. unsigned ctlr = h->ctlr;
  296. loff_t *pos = v;
  297. drive_info_struct *drv = h->drv[*pos];
  298. if (*pos > h->highest_lun)
  299. return 0;
  300. if (drv->heads == 0)
  301. return 0;
  302. vol_sz = drv->nr_blocks;
  303. vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
  304. vol_sz_frac *= 100;
  305. sector_div(vol_sz_frac, ENG_GIG_FACTOR);
  306. if (drv->raid_level < 0 || drv->raid_level > RAID_UNKNOWN)
  307. drv->raid_level = RAID_UNKNOWN;
  308. seq_printf(seq, "cciss/c%dd%d:"
  309. "\t%4u.%02uGB\tRAID %s\n",
  310. ctlr, (int) *pos, (int)vol_sz, (int)vol_sz_frac,
  311. raid_label[drv->raid_level]);
  312. return 0;
  313. }
  314. static void *cciss_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  315. {
  316. ctlr_info_t *h = seq->private;
  317. if (*pos > h->highest_lun)
  318. return NULL;
  319. *pos += 1;
  320. return pos;
  321. }
  322. static void cciss_seq_stop(struct seq_file *seq, void *v)
  323. {
  324. ctlr_info_t *h = seq->private;
  325. /* Only reset h->busy_configuring if we succeeded in setting
  326. * it during cciss_seq_start. */
  327. if (v == ERR_PTR(-EBUSY))
  328. return;
  329. h->busy_configuring = 0;
  330. }
  331. static const struct seq_operations cciss_seq_ops = {
  332. .start = cciss_seq_start,
  333. .show = cciss_seq_show,
  334. .next = cciss_seq_next,
  335. .stop = cciss_seq_stop,
  336. };
  337. static int cciss_seq_open(struct inode *inode, struct file *file)
  338. {
  339. int ret = seq_open(file, &cciss_seq_ops);
  340. struct seq_file *seq = file->private_data;
  341. if (!ret)
  342. seq->private = PDE(inode)->data;
  343. return ret;
  344. }
  345. static ssize_t
  346. cciss_proc_write(struct file *file, const char __user *buf,
  347. size_t length, loff_t *ppos)
  348. {
  349. int err;
  350. char *buffer;
  351. #ifndef CONFIG_CISS_SCSI_TAPE
  352. return -EINVAL;
  353. #endif
  354. if (!buf || length > PAGE_SIZE - 1)
  355. return -EINVAL;
  356. buffer = (char *)__get_free_page(GFP_KERNEL);
  357. if (!buffer)
  358. return -ENOMEM;
  359. err = -EFAULT;
  360. if (copy_from_user(buffer, buf, length))
  361. goto out;
  362. buffer[length] = '\0';
  363. #ifdef CONFIG_CISS_SCSI_TAPE
  364. if (strncmp(ENGAGE_SCSI, buffer, sizeof ENGAGE_SCSI - 1) == 0) {
  365. struct seq_file *seq = file->private_data;
  366. ctlr_info_t *h = seq->private;
  367. int rc;
  368. rc = cciss_engage_scsi(h->ctlr);
  369. if (rc != 0)
  370. err = -rc;
  371. else
  372. err = length;
  373. } else
  374. #endif /* CONFIG_CISS_SCSI_TAPE */
  375. err = -EINVAL;
  376. /* might be nice to have "disengage" too, but it's not
  377. safely possible. (only 1 module use count, lock issues.) */
  378. out:
  379. free_page((unsigned long)buffer);
  380. return err;
  381. }
  382. static const struct file_operations cciss_proc_fops = {
  383. .owner = THIS_MODULE,
  384. .open = cciss_seq_open,
  385. .read = seq_read,
  386. .llseek = seq_lseek,
  387. .release = seq_release,
  388. .write = cciss_proc_write,
  389. };
  390. static void __devinit cciss_procinit(int i)
  391. {
  392. struct proc_dir_entry *pde;
  393. if (proc_cciss == NULL)
  394. proc_cciss = proc_mkdir("driver/cciss", NULL);
  395. if (!proc_cciss)
  396. return;
  397. pde = proc_create_data(hba[i]->devname, S_IWUSR | S_IRUSR | S_IRGRP |
  398. S_IROTH, proc_cciss,
  399. &cciss_proc_fops, hba[i]);
  400. }
  401. #endif /* CONFIG_PROC_FS */
  402. #define MAX_PRODUCT_NAME_LEN 19
  403. #define to_hba(n) container_of(n, struct ctlr_info, dev)
  404. #define to_drv(n) container_of(n, drive_info_struct, dev)
  405. static ssize_t host_store_rescan(struct device *dev,
  406. struct device_attribute *attr,
  407. const char *buf, size_t count)
  408. {
  409. struct ctlr_info *h = to_hba(dev);
  410. add_to_scan_list(h);
  411. wake_up_process(cciss_scan_thread);
  412. wait_for_completion_interruptible(&h->scan_wait);
  413. return count;
  414. }
  415. static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
  416. static ssize_t dev_show_unique_id(struct device *dev,
  417. struct device_attribute *attr,
  418. char *buf)
  419. {
  420. drive_info_struct *drv = to_drv(dev);
  421. struct ctlr_info *h = to_hba(drv->dev.parent);
  422. __u8 sn[16];
  423. unsigned long flags;
  424. int ret = 0;
  425. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  426. if (h->busy_configuring)
  427. ret = -EBUSY;
  428. else
  429. memcpy(sn, drv->serial_no, sizeof(sn));
  430. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  431. if (ret)
  432. return ret;
  433. else
  434. return snprintf(buf, 16 * 2 + 2,
  435. "%02X%02X%02X%02X%02X%02X%02X%02X"
  436. "%02X%02X%02X%02X%02X%02X%02X%02X\n",
  437. sn[0], sn[1], sn[2], sn[3],
  438. sn[4], sn[5], sn[6], sn[7],
  439. sn[8], sn[9], sn[10], sn[11],
  440. sn[12], sn[13], sn[14], sn[15]);
  441. }
  442. static DEVICE_ATTR(unique_id, S_IRUGO, dev_show_unique_id, NULL);
  443. static ssize_t dev_show_vendor(struct device *dev,
  444. struct device_attribute *attr,
  445. char *buf)
  446. {
  447. drive_info_struct *drv = to_drv(dev);
  448. struct ctlr_info *h = to_hba(drv->dev.parent);
  449. char vendor[VENDOR_LEN + 1];
  450. unsigned long flags;
  451. int ret = 0;
  452. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  453. if (h->busy_configuring)
  454. ret = -EBUSY;
  455. else
  456. memcpy(vendor, drv->vendor, VENDOR_LEN + 1);
  457. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  458. if (ret)
  459. return ret;
  460. else
  461. return snprintf(buf, sizeof(vendor) + 1, "%s\n", drv->vendor);
  462. }
  463. static DEVICE_ATTR(vendor, S_IRUGO, dev_show_vendor, NULL);
  464. static ssize_t dev_show_model(struct device *dev,
  465. struct device_attribute *attr,
  466. char *buf)
  467. {
  468. drive_info_struct *drv = to_drv(dev);
  469. struct ctlr_info *h = to_hba(drv->dev.parent);
  470. char model[MODEL_LEN + 1];
  471. unsigned long flags;
  472. int ret = 0;
  473. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  474. if (h->busy_configuring)
  475. ret = -EBUSY;
  476. else
  477. memcpy(model, drv->model, MODEL_LEN + 1);
  478. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  479. if (ret)
  480. return ret;
  481. else
  482. return snprintf(buf, sizeof(model) + 1, "%s\n", drv->model);
  483. }
  484. static DEVICE_ATTR(model, S_IRUGO, dev_show_model, NULL);
  485. static ssize_t dev_show_rev(struct device *dev,
  486. struct device_attribute *attr,
  487. char *buf)
  488. {
  489. drive_info_struct *drv = to_drv(dev);
  490. struct ctlr_info *h = to_hba(drv->dev.parent);
  491. char rev[REV_LEN + 1];
  492. unsigned long flags;
  493. int ret = 0;
  494. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  495. if (h->busy_configuring)
  496. ret = -EBUSY;
  497. else
  498. memcpy(rev, drv->rev, REV_LEN + 1);
  499. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  500. if (ret)
  501. return ret;
  502. else
  503. return snprintf(buf, sizeof(rev) + 1, "%s\n", drv->rev);
  504. }
  505. static DEVICE_ATTR(rev, S_IRUGO, dev_show_rev, NULL);
  506. static ssize_t cciss_show_lunid(struct device *dev,
  507. struct device_attribute *attr, char *buf)
  508. {
  509. drive_info_struct *drv = to_drv(dev);
  510. struct ctlr_info *h = to_hba(drv->dev.parent);
  511. unsigned long flags;
  512. unsigned char lunid[8];
  513. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  514. if (h->busy_configuring) {
  515. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  516. return -EBUSY;
  517. }
  518. if (!drv->heads) {
  519. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  520. return -ENOTTY;
  521. }
  522. memcpy(lunid, drv->LunID, sizeof(lunid));
  523. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  524. return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  525. lunid[0], lunid[1], lunid[2], lunid[3],
  526. lunid[4], lunid[5], lunid[6], lunid[7]);
  527. }
  528. static DEVICE_ATTR(lunid, S_IRUGO, cciss_show_lunid, NULL);
  529. static ssize_t cciss_show_raid_level(struct device *dev,
  530. struct device_attribute *attr, char *buf)
  531. {
  532. drive_info_struct *drv = to_drv(dev);
  533. struct ctlr_info *h = to_hba(drv->dev.parent);
  534. int raid;
  535. unsigned long flags;
  536. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  537. if (h->busy_configuring) {
  538. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  539. return -EBUSY;
  540. }
  541. raid = drv->raid_level;
  542. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  543. if (raid < 0 || raid > RAID_UNKNOWN)
  544. raid = RAID_UNKNOWN;
  545. return snprintf(buf, strlen(raid_label[raid]) + 7, "RAID %s\n",
  546. raid_label[raid]);
  547. }
  548. static DEVICE_ATTR(raid_level, S_IRUGO, cciss_show_raid_level, NULL);
  549. static ssize_t cciss_show_usage_count(struct device *dev,
  550. struct device_attribute *attr, char *buf)
  551. {
  552. drive_info_struct *drv = to_drv(dev);
  553. struct ctlr_info *h = to_hba(drv->dev.parent);
  554. unsigned long flags;
  555. int count;
  556. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  557. if (h->busy_configuring) {
  558. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  559. return -EBUSY;
  560. }
  561. count = drv->usage_count;
  562. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  563. return snprintf(buf, 20, "%d\n", count);
  564. }
  565. static DEVICE_ATTR(usage_count, S_IRUGO, cciss_show_usage_count, NULL);
  566. static struct attribute *cciss_host_attrs[] = {
  567. &dev_attr_rescan.attr,
  568. NULL
  569. };
  570. static struct attribute_group cciss_host_attr_group = {
  571. .attrs = cciss_host_attrs,
  572. };
  573. static const struct attribute_group *cciss_host_attr_groups[] = {
  574. &cciss_host_attr_group,
  575. NULL
  576. };
  577. static struct device_type cciss_host_type = {
  578. .name = "cciss_host",
  579. .groups = cciss_host_attr_groups,
  580. .release = cciss_hba_release,
  581. };
  582. static struct attribute *cciss_dev_attrs[] = {
  583. &dev_attr_unique_id.attr,
  584. &dev_attr_model.attr,
  585. &dev_attr_vendor.attr,
  586. &dev_attr_rev.attr,
  587. &dev_attr_lunid.attr,
  588. &dev_attr_raid_level.attr,
  589. &dev_attr_usage_count.attr,
  590. NULL
  591. };
  592. static struct attribute_group cciss_dev_attr_group = {
  593. .attrs = cciss_dev_attrs,
  594. };
  595. static const struct attribute_group *cciss_dev_attr_groups[] = {
  596. &cciss_dev_attr_group,
  597. NULL
  598. };
  599. static struct device_type cciss_dev_type = {
  600. .name = "cciss_device",
  601. .groups = cciss_dev_attr_groups,
  602. .release = cciss_device_release,
  603. };
  604. static struct bus_type cciss_bus_type = {
  605. .name = "cciss",
  606. };
  607. /*
  608. * cciss_hba_release is called when the reference count
  609. * of h->dev goes to zero.
  610. */
  611. static void cciss_hba_release(struct device *dev)
  612. {
  613. /*
  614. * nothing to do, but need this to avoid a warning
  615. * about not having a release handler from lib/kref.c.
  616. */
  617. }
  618. /*
  619. * Initialize sysfs entry for each controller. This sets up and registers
  620. * the 'cciss#' directory for each individual controller under
  621. * /sys/bus/pci/devices/<dev>/.
  622. */
  623. static int cciss_create_hba_sysfs_entry(struct ctlr_info *h)
  624. {
  625. device_initialize(&h->dev);
  626. h->dev.type = &cciss_host_type;
  627. h->dev.bus = &cciss_bus_type;
  628. dev_set_name(&h->dev, "%s", h->devname);
  629. h->dev.parent = &h->pdev->dev;
  630. return device_add(&h->dev);
  631. }
  632. /*
  633. * Remove sysfs entries for an hba.
  634. */
  635. static void cciss_destroy_hba_sysfs_entry(struct ctlr_info *h)
  636. {
  637. device_del(&h->dev);
  638. put_device(&h->dev); /* final put. */
  639. }
  640. /* cciss_device_release is called when the reference count
  641. * of h->drv[x]dev goes to zero.
  642. */
  643. static void cciss_device_release(struct device *dev)
  644. {
  645. drive_info_struct *drv = to_drv(dev);
  646. kfree(drv);
  647. }
  648. /*
  649. * Initialize sysfs for each logical drive. This sets up and registers
  650. * the 'c#d#' directory for each individual logical drive under
  651. * /sys/bus/pci/devices/<dev/ccis#/. We also create a link from
  652. * /sys/block/cciss!c#d# to this entry.
  653. */
  654. static long cciss_create_ld_sysfs_entry(struct ctlr_info *h,
  655. int drv_index)
  656. {
  657. struct device *dev;
  658. if (h->drv[drv_index]->device_initialized)
  659. return 0;
  660. dev = &h->drv[drv_index]->dev;
  661. device_initialize(dev);
  662. dev->type = &cciss_dev_type;
  663. dev->bus = &cciss_bus_type;
  664. dev_set_name(dev, "c%dd%d", h->ctlr, drv_index);
  665. dev->parent = &h->dev;
  666. h->drv[drv_index]->device_initialized = 1;
  667. return device_add(dev);
  668. }
  669. /*
  670. * Remove sysfs entries for a logical drive.
  671. */
  672. static void cciss_destroy_ld_sysfs_entry(struct ctlr_info *h, int drv_index,
  673. int ctlr_exiting)
  674. {
  675. struct device *dev = &h->drv[drv_index]->dev;
  676. /* special case for c*d0, we only destroy it on controller exit */
  677. if (drv_index == 0 && !ctlr_exiting)
  678. return;
  679. device_del(dev);
  680. put_device(dev); /* the "final" put. */
  681. h->drv[drv_index] = NULL;
  682. }
  683. /*
  684. * For operations that cannot sleep, a command block is allocated at init,
  685. * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
  686. * which ones are free or in use. For operations that can wait for kmalloc
  687. * to possible sleep, this routine can be called with get_from_pool set to 0.
  688. * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
  689. */
  690. static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool)
  691. {
  692. CommandList_struct *c;
  693. int i;
  694. u64bit temp64;
  695. dma_addr_t cmd_dma_handle, err_dma_handle;
  696. if (!get_from_pool) {
  697. c = (CommandList_struct *) pci_alloc_consistent(h->pdev,
  698. sizeof(CommandList_struct), &cmd_dma_handle);
  699. if (c == NULL)
  700. return NULL;
  701. memset(c, 0, sizeof(CommandList_struct));
  702. c->cmdindex = -1;
  703. c->err_info = (ErrorInfo_struct *)
  704. pci_alloc_consistent(h->pdev, sizeof(ErrorInfo_struct),
  705. &err_dma_handle);
  706. if (c->err_info == NULL) {
  707. pci_free_consistent(h->pdev,
  708. sizeof(CommandList_struct), c, cmd_dma_handle);
  709. return NULL;
  710. }
  711. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  712. } else { /* get it out of the controllers pool */
  713. do {
  714. i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
  715. if (i == h->nr_cmds)
  716. return NULL;
  717. } while (test_and_set_bit
  718. (i & (BITS_PER_LONG - 1),
  719. h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
  720. #ifdef CCISS_DEBUG
  721. printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
  722. #endif
  723. c = h->cmd_pool + i;
  724. memset(c, 0, sizeof(CommandList_struct));
  725. cmd_dma_handle = h->cmd_pool_dhandle
  726. + i * sizeof(CommandList_struct);
  727. c->err_info = h->errinfo_pool + i;
  728. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  729. err_dma_handle = h->errinfo_pool_dhandle
  730. + i * sizeof(ErrorInfo_struct);
  731. h->nr_allocs++;
  732. c->cmdindex = i;
  733. }
  734. INIT_HLIST_NODE(&c->list);
  735. c->busaddr = (__u32) cmd_dma_handle;
  736. temp64.val = (__u64) err_dma_handle;
  737. c->ErrDesc.Addr.lower = temp64.val32.lower;
  738. c->ErrDesc.Addr.upper = temp64.val32.upper;
  739. c->ErrDesc.Len = sizeof(ErrorInfo_struct);
  740. c->ctlr = h->ctlr;
  741. return c;
  742. }
  743. /*
  744. * Frees a command block that was previously allocated with cmd_alloc().
  745. */
  746. static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
  747. {
  748. int i;
  749. u64bit temp64;
  750. if (!got_from_pool) {
  751. temp64.val32.lower = c->ErrDesc.Addr.lower;
  752. temp64.val32.upper = c->ErrDesc.Addr.upper;
  753. pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
  754. c->err_info, (dma_addr_t) temp64.val);
  755. pci_free_consistent(h->pdev, sizeof(CommandList_struct),
  756. c, (dma_addr_t) c->busaddr);
  757. } else {
  758. i = c - h->cmd_pool;
  759. clear_bit(i & (BITS_PER_LONG - 1),
  760. h->cmd_pool_bits + (i / BITS_PER_LONG));
  761. h->nr_frees++;
  762. }
  763. }
  764. static inline ctlr_info_t *get_host(struct gendisk *disk)
  765. {
  766. return disk->queue->queuedata;
  767. }
  768. static inline drive_info_struct *get_drv(struct gendisk *disk)
  769. {
  770. return disk->private_data;
  771. }
  772. /*
  773. * Open. Make sure the device is really there.
  774. */
  775. static int cciss_open(struct block_device *bdev, fmode_t mode)
  776. {
  777. ctlr_info_t *host = get_host(bdev->bd_disk);
  778. drive_info_struct *drv = get_drv(bdev->bd_disk);
  779. #ifdef CCISS_DEBUG
  780. printk(KERN_DEBUG "cciss_open %s\n", bdev->bd_disk->disk_name);
  781. #endif /* CCISS_DEBUG */
  782. if (drv->busy_configuring)
  783. return -EBUSY;
  784. /*
  785. * Root is allowed to open raw volume zero even if it's not configured
  786. * so array config can still work. Root is also allowed to open any
  787. * volume that has a LUN ID, so it can issue IOCTL to reread the
  788. * disk information. I don't think I really like this
  789. * but I'm already using way to many device nodes to claim another one
  790. * for "raw controller".
  791. */
  792. if (drv->heads == 0) {
  793. if (MINOR(bdev->bd_dev) != 0) { /* not node 0? */
  794. /* if not node 0 make sure it is a partition = 0 */
  795. if (MINOR(bdev->bd_dev) & 0x0f) {
  796. return -ENXIO;
  797. /* if it is, make sure we have a LUN ID */
  798. } else if (memcmp(drv->LunID, CTLR_LUNID,
  799. sizeof(drv->LunID))) {
  800. return -ENXIO;
  801. }
  802. }
  803. if (!capable(CAP_SYS_ADMIN))
  804. return -EPERM;
  805. }
  806. drv->usage_count++;
  807. host->usage_count++;
  808. return 0;
  809. }
  810. /*
  811. * Close. Sync first.
  812. */
  813. static int cciss_release(struct gendisk *disk, fmode_t mode)
  814. {
  815. ctlr_info_t *host = get_host(disk);
  816. drive_info_struct *drv = get_drv(disk);
  817. #ifdef CCISS_DEBUG
  818. printk(KERN_DEBUG "cciss_release %s\n", disk->disk_name);
  819. #endif /* CCISS_DEBUG */
  820. drv->usage_count--;
  821. host->usage_count--;
  822. return 0;
  823. }
  824. #ifdef CONFIG_COMPAT
  825. static int do_ioctl(struct block_device *bdev, fmode_t mode,
  826. unsigned cmd, unsigned long arg)
  827. {
  828. int ret;
  829. lock_kernel();
  830. ret = cciss_ioctl(bdev, mode, cmd, arg);
  831. unlock_kernel();
  832. return ret;
  833. }
  834. static int cciss_ioctl32_passthru(struct block_device *bdev, fmode_t mode,
  835. unsigned cmd, unsigned long arg);
  836. static int cciss_ioctl32_big_passthru(struct block_device *bdev, fmode_t mode,
  837. unsigned cmd, unsigned long arg);
  838. static int cciss_compat_ioctl(struct block_device *bdev, fmode_t mode,
  839. unsigned cmd, unsigned long arg)
  840. {
  841. switch (cmd) {
  842. case CCISS_GETPCIINFO:
  843. case CCISS_GETINTINFO:
  844. case CCISS_SETINTINFO:
  845. case CCISS_GETNODENAME:
  846. case CCISS_SETNODENAME:
  847. case CCISS_GETHEARTBEAT:
  848. case CCISS_GETBUSTYPES:
  849. case CCISS_GETFIRMVER:
  850. case CCISS_GETDRIVVER:
  851. case CCISS_REVALIDVOLS:
  852. case CCISS_DEREGDISK:
  853. case CCISS_REGNEWDISK:
  854. case CCISS_REGNEWD:
  855. case CCISS_RESCANDISK:
  856. case CCISS_GETLUNINFO:
  857. return do_ioctl(bdev, mode, cmd, arg);
  858. case CCISS_PASSTHRU32:
  859. return cciss_ioctl32_passthru(bdev, mode, cmd, arg);
  860. case CCISS_BIG_PASSTHRU32:
  861. return cciss_ioctl32_big_passthru(bdev, mode, cmd, arg);
  862. default:
  863. return -ENOIOCTLCMD;
  864. }
  865. }
  866. static int cciss_ioctl32_passthru(struct block_device *bdev, fmode_t mode,
  867. unsigned cmd, unsigned long arg)
  868. {
  869. IOCTL32_Command_struct __user *arg32 =
  870. (IOCTL32_Command_struct __user *) arg;
  871. IOCTL_Command_struct arg64;
  872. IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  873. int err;
  874. u32 cp;
  875. err = 0;
  876. err |=
  877. copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  878. sizeof(arg64.LUN_info));
  879. err |=
  880. copy_from_user(&arg64.Request, &arg32->Request,
  881. sizeof(arg64.Request));
  882. err |=
  883. copy_from_user(&arg64.error_info, &arg32->error_info,
  884. sizeof(arg64.error_info));
  885. err |= get_user(arg64.buf_size, &arg32->buf_size);
  886. err |= get_user(cp, &arg32->buf);
  887. arg64.buf = compat_ptr(cp);
  888. err |= copy_to_user(p, &arg64, sizeof(arg64));
  889. if (err)
  890. return -EFAULT;
  891. err = do_ioctl(bdev, mode, CCISS_PASSTHRU, (unsigned long)p);
  892. if (err)
  893. return err;
  894. err |=
  895. copy_in_user(&arg32->error_info, &p->error_info,
  896. sizeof(arg32->error_info));
  897. if (err)
  898. return -EFAULT;
  899. return err;
  900. }
  901. static int cciss_ioctl32_big_passthru(struct block_device *bdev, fmode_t mode,
  902. unsigned cmd, unsigned long arg)
  903. {
  904. BIG_IOCTL32_Command_struct __user *arg32 =
  905. (BIG_IOCTL32_Command_struct __user *) arg;
  906. BIG_IOCTL_Command_struct arg64;
  907. BIG_IOCTL_Command_struct __user *p =
  908. compat_alloc_user_space(sizeof(arg64));
  909. int err;
  910. u32 cp;
  911. err = 0;
  912. err |=
  913. copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  914. sizeof(arg64.LUN_info));
  915. err |=
  916. copy_from_user(&arg64.Request, &arg32->Request,
  917. sizeof(arg64.Request));
  918. err |=
  919. copy_from_user(&arg64.error_info, &arg32->error_info,
  920. sizeof(arg64.error_info));
  921. err |= get_user(arg64.buf_size, &arg32->buf_size);
  922. err |= get_user(arg64.malloc_size, &arg32->malloc_size);
  923. err |= get_user(cp, &arg32->buf);
  924. arg64.buf = compat_ptr(cp);
  925. err |= copy_to_user(p, &arg64, sizeof(arg64));
  926. if (err)
  927. return -EFAULT;
  928. err = do_ioctl(bdev, mode, CCISS_BIG_PASSTHRU, (unsigned long)p);
  929. if (err)
  930. return err;
  931. err |=
  932. copy_in_user(&arg32->error_info, &p->error_info,
  933. sizeof(arg32->error_info));
  934. if (err)
  935. return -EFAULT;
  936. return err;
  937. }
  938. #endif
  939. static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  940. {
  941. drive_info_struct *drv = get_drv(bdev->bd_disk);
  942. if (!drv->cylinders)
  943. return -ENXIO;
  944. geo->heads = drv->heads;
  945. geo->sectors = drv->sectors;
  946. geo->cylinders = drv->cylinders;
  947. return 0;
  948. }
  949. static void check_ioctl_unit_attention(ctlr_info_t *host, CommandList_struct *c)
  950. {
  951. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  952. c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
  953. (void)check_for_unit_attention(host, c);
  954. }
  955. /*
  956. * ioctl
  957. */
  958. static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
  959. unsigned int cmd, unsigned long arg)
  960. {
  961. struct gendisk *disk = bdev->bd_disk;
  962. ctlr_info_t *host = get_host(disk);
  963. drive_info_struct *drv = get_drv(disk);
  964. int ctlr = host->ctlr;
  965. void __user *argp = (void __user *)arg;
  966. #ifdef CCISS_DEBUG
  967. printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
  968. #endif /* CCISS_DEBUG */
  969. switch (cmd) {
  970. case CCISS_GETPCIINFO:
  971. {
  972. cciss_pci_info_struct pciinfo;
  973. if (!arg)
  974. return -EINVAL;
  975. pciinfo.domain = pci_domain_nr(host->pdev->bus);
  976. pciinfo.bus = host->pdev->bus->number;
  977. pciinfo.dev_fn = host->pdev->devfn;
  978. pciinfo.board_id = host->board_id;
  979. if (copy_to_user
  980. (argp, &pciinfo, sizeof(cciss_pci_info_struct)))
  981. return -EFAULT;
  982. return 0;
  983. }
  984. case CCISS_GETINTINFO:
  985. {
  986. cciss_coalint_struct intinfo;
  987. if (!arg)
  988. return -EINVAL;
  989. intinfo.delay =
  990. readl(&host->cfgtable->HostWrite.CoalIntDelay);
  991. intinfo.count =
  992. readl(&host->cfgtable->HostWrite.CoalIntCount);
  993. if (copy_to_user
  994. (argp, &intinfo, sizeof(cciss_coalint_struct)))
  995. return -EFAULT;
  996. return 0;
  997. }
  998. case CCISS_SETINTINFO:
  999. {
  1000. cciss_coalint_struct intinfo;
  1001. unsigned long flags;
  1002. int i;
  1003. if (!arg)
  1004. return -EINVAL;
  1005. if (!capable(CAP_SYS_ADMIN))
  1006. return -EPERM;
  1007. if (copy_from_user
  1008. (&intinfo, argp, sizeof(cciss_coalint_struct)))
  1009. return -EFAULT;
  1010. if ((intinfo.delay == 0) && (intinfo.count == 0))
  1011. {
  1012. // printk("cciss_ioctl: delay and count cannot be 0\n");
  1013. return -EINVAL;
  1014. }
  1015. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  1016. /* Update the field, and then ring the doorbell */
  1017. writel(intinfo.delay,
  1018. &(host->cfgtable->HostWrite.CoalIntDelay));
  1019. writel(intinfo.count,
  1020. &(host->cfgtable->HostWrite.CoalIntCount));
  1021. writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
  1022. for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
  1023. if (!(readl(host->vaddr + SA5_DOORBELL)
  1024. & CFGTBL_ChangeReq))
  1025. break;
  1026. /* delay and try again */
  1027. udelay(1000);
  1028. }
  1029. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1030. if (i >= MAX_IOCTL_CONFIG_WAIT)
  1031. return -EAGAIN;
  1032. return 0;
  1033. }
  1034. case CCISS_GETNODENAME:
  1035. {
  1036. NodeName_type NodeName;
  1037. int i;
  1038. if (!arg)
  1039. return -EINVAL;
  1040. for (i = 0; i < 16; i++)
  1041. NodeName[i] =
  1042. readb(&host->cfgtable->ServerName[i]);
  1043. if (copy_to_user(argp, NodeName, sizeof(NodeName_type)))
  1044. return -EFAULT;
  1045. return 0;
  1046. }
  1047. case CCISS_SETNODENAME:
  1048. {
  1049. NodeName_type NodeName;
  1050. unsigned long flags;
  1051. int i;
  1052. if (!arg)
  1053. return -EINVAL;
  1054. if (!capable(CAP_SYS_ADMIN))
  1055. return -EPERM;
  1056. if (copy_from_user
  1057. (NodeName, argp, sizeof(NodeName_type)))
  1058. return -EFAULT;
  1059. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  1060. /* Update the field, and then ring the doorbell */
  1061. for (i = 0; i < 16; i++)
  1062. writeb(NodeName[i],
  1063. &host->cfgtable->ServerName[i]);
  1064. writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
  1065. for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
  1066. if (!(readl(host->vaddr + SA5_DOORBELL)
  1067. & CFGTBL_ChangeReq))
  1068. break;
  1069. /* delay and try again */
  1070. udelay(1000);
  1071. }
  1072. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1073. if (i >= MAX_IOCTL_CONFIG_WAIT)
  1074. return -EAGAIN;
  1075. return 0;
  1076. }
  1077. case CCISS_GETHEARTBEAT:
  1078. {
  1079. Heartbeat_type heartbeat;
  1080. if (!arg)
  1081. return -EINVAL;
  1082. heartbeat = readl(&host->cfgtable->HeartBeat);
  1083. if (copy_to_user
  1084. (argp, &heartbeat, sizeof(Heartbeat_type)))
  1085. return -EFAULT;
  1086. return 0;
  1087. }
  1088. case CCISS_GETBUSTYPES:
  1089. {
  1090. BusTypes_type BusTypes;
  1091. if (!arg)
  1092. return -EINVAL;
  1093. BusTypes = readl(&host->cfgtable->BusTypes);
  1094. if (copy_to_user
  1095. (argp, &BusTypes, sizeof(BusTypes_type)))
  1096. return -EFAULT;
  1097. return 0;
  1098. }
  1099. case CCISS_GETFIRMVER:
  1100. {
  1101. FirmwareVer_type firmware;
  1102. if (!arg)
  1103. return -EINVAL;
  1104. memcpy(firmware, host->firm_ver, 4);
  1105. if (copy_to_user
  1106. (argp, firmware, sizeof(FirmwareVer_type)))
  1107. return -EFAULT;
  1108. return 0;
  1109. }
  1110. case CCISS_GETDRIVVER:
  1111. {
  1112. DriverVer_type DriverVer = DRIVER_VERSION;
  1113. if (!arg)
  1114. return -EINVAL;
  1115. if (copy_to_user
  1116. (argp, &DriverVer, sizeof(DriverVer_type)))
  1117. return -EFAULT;
  1118. return 0;
  1119. }
  1120. case CCISS_DEREGDISK:
  1121. case CCISS_REGNEWD:
  1122. case CCISS_REVALIDVOLS:
  1123. return rebuild_lun_table(host, 0, 1);
  1124. case CCISS_GETLUNINFO:{
  1125. LogvolInfo_struct luninfo;
  1126. memcpy(&luninfo.LunID, drv->LunID,
  1127. sizeof(luninfo.LunID));
  1128. luninfo.num_opens = drv->usage_count;
  1129. luninfo.num_parts = 0;
  1130. if (copy_to_user(argp, &luninfo,
  1131. sizeof(LogvolInfo_struct)))
  1132. return -EFAULT;
  1133. return 0;
  1134. }
  1135. case CCISS_PASSTHRU:
  1136. {
  1137. IOCTL_Command_struct iocommand;
  1138. CommandList_struct *c;
  1139. char *buff = NULL;
  1140. u64bit temp64;
  1141. unsigned long flags;
  1142. DECLARE_COMPLETION_ONSTACK(wait);
  1143. if (!arg)
  1144. return -EINVAL;
  1145. if (!capable(CAP_SYS_RAWIO))
  1146. return -EPERM;
  1147. if (copy_from_user
  1148. (&iocommand, argp, sizeof(IOCTL_Command_struct)))
  1149. return -EFAULT;
  1150. if ((iocommand.buf_size < 1) &&
  1151. (iocommand.Request.Type.Direction != XFER_NONE)) {
  1152. return -EINVAL;
  1153. }
  1154. #if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
  1155. /* Check kmalloc limits */
  1156. if (iocommand.buf_size > 128000)
  1157. return -EINVAL;
  1158. #endif
  1159. if (iocommand.buf_size > 0) {
  1160. buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
  1161. if (buff == NULL)
  1162. return -EFAULT;
  1163. }
  1164. if (iocommand.Request.Type.Direction == XFER_WRITE) {
  1165. /* Copy the data into the buffer we created */
  1166. if (copy_from_user
  1167. (buff, iocommand.buf, iocommand.buf_size)) {
  1168. kfree(buff);
  1169. return -EFAULT;
  1170. }
  1171. } else {
  1172. memset(buff, 0, iocommand.buf_size);
  1173. }
  1174. if ((c = cmd_alloc(host, 0)) == NULL) {
  1175. kfree(buff);
  1176. return -ENOMEM;
  1177. }
  1178. // Fill in the command type
  1179. c->cmd_type = CMD_IOCTL_PEND;
  1180. // Fill in Command Header
  1181. c->Header.ReplyQueue = 0; // unused in simple mode
  1182. if (iocommand.buf_size > 0) // buffer to fill
  1183. {
  1184. c->Header.SGList = 1;
  1185. c->Header.SGTotal = 1;
  1186. } else // no buffers to fill
  1187. {
  1188. c->Header.SGList = 0;
  1189. c->Header.SGTotal = 0;
  1190. }
  1191. c->Header.LUN = iocommand.LUN_info;
  1192. c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
  1193. // Fill in Request block
  1194. c->Request = iocommand.Request;
  1195. // Fill in the scatter gather information
  1196. if (iocommand.buf_size > 0) {
  1197. temp64.val = pci_map_single(host->pdev, buff,
  1198. iocommand.buf_size,
  1199. PCI_DMA_BIDIRECTIONAL);
  1200. c->SG[0].Addr.lower = temp64.val32.lower;
  1201. c->SG[0].Addr.upper = temp64.val32.upper;
  1202. c->SG[0].Len = iocommand.buf_size;
  1203. c->SG[0].Ext = 0; // we are not chaining
  1204. }
  1205. c->waiting = &wait;
  1206. /* Put the request on the tail of the request queue */
  1207. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  1208. addQ(&host->reqQ, c);
  1209. host->Qdepth++;
  1210. start_io(host);
  1211. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1212. wait_for_completion(&wait);
  1213. /* unlock the buffers from DMA */
  1214. temp64.val32.lower = c->SG[0].Addr.lower;
  1215. temp64.val32.upper = c->SG[0].Addr.upper;
  1216. pci_unmap_single(host->pdev, (dma_addr_t) temp64.val,
  1217. iocommand.buf_size,
  1218. PCI_DMA_BIDIRECTIONAL);
  1219. check_ioctl_unit_attention(host, c);
  1220. /* Copy the error information out */
  1221. iocommand.error_info = *(c->err_info);
  1222. if (copy_to_user
  1223. (argp, &iocommand, sizeof(IOCTL_Command_struct))) {
  1224. kfree(buff);
  1225. cmd_free(host, c, 0);
  1226. return -EFAULT;
  1227. }
  1228. if (iocommand.Request.Type.Direction == XFER_READ) {
  1229. /* Copy the data out of the buffer we created */
  1230. if (copy_to_user
  1231. (iocommand.buf, buff, iocommand.buf_size)) {
  1232. kfree(buff);
  1233. cmd_free(host, c, 0);
  1234. return -EFAULT;
  1235. }
  1236. }
  1237. kfree(buff);
  1238. cmd_free(host, c, 0);
  1239. return 0;
  1240. }
  1241. case CCISS_BIG_PASSTHRU:{
  1242. BIG_IOCTL_Command_struct *ioc;
  1243. CommandList_struct *c;
  1244. unsigned char **buff = NULL;
  1245. int *buff_size = NULL;
  1246. u64bit temp64;
  1247. unsigned long flags;
  1248. BYTE sg_used = 0;
  1249. int status = 0;
  1250. int i;
  1251. DECLARE_COMPLETION_ONSTACK(wait);
  1252. __u32 left;
  1253. __u32 sz;
  1254. BYTE __user *data_ptr;
  1255. if (!arg)
  1256. return -EINVAL;
  1257. if (!capable(CAP_SYS_RAWIO))
  1258. return -EPERM;
  1259. ioc = (BIG_IOCTL_Command_struct *)
  1260. kmalloc(sizeof(*ioc), GFP_KERNEL);
  1261. if (!ioc) {
  1262. status = -ENOMEM;
  1263. goto cleanup1;
  1264. }
  1265. if (copy_from_user(ioc, argp, sizeof(*ioc))) {
  1266. status = -EFAULT;
  1267. goto cleanup1;
  1268. }
  1269. if ((ioc->buf_size < 1) &&
  1270. (ioc->Request.Type.Direction != XFER_NONE)) {
  1271. status = -EINVAL;
  1272. goto cleanup1;
  1273. }
  1274. /* Check kmalloc limits using all SGs */
  1275. if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
  1276. status = -EINVAL;
  1277. goto cleanup1;
  1278. }
  1279. if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
  1280. status = -EINVAL;
  1281. goto cleanup1;
  1282. }
  1283. buff =
  1284. kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
  1285. if (!buff) {
  1286. status = -ENOMEM;
  1287. goto cleanup1;
  1288. }
  1289. buff_size = kmalloc(MAXSGENTRIES * sizeof(int),
  1290. GFP_KERNEL);
  1291. if (!buff_size) {
  1292. status = -ENOMEM;
  1293. goto cleanup1;
  1294. }
  1295. left = ioc->buf_size;
  1296. data_ptr = ioc->buf;
  1297. while (left) {
  1298. sz = (left >
  1299. ioc->malloc_size) ? ioc->
  1300. malloc_size : left;
  1301. buff_size[sg_used] = sz;
  1302. buff[sg_used] = kmalloc(sz, GFP_KERNEL);
  1303. if (buff[sg_used] == NULL) {
  1304. status = -ENOMEM;
  1305. goto cleanup1;
  1306. }
  1307. if (ioc->Request.Type.Direction == XFER_WRITE) {
  1308. if (copy_from_user
  1309. (buff[sg_used], data_ptr, sz)) {
  1310. status = -EFAULT;
  1311. goto cleanup1;
  1312. }
  1313. } else {
  1314. memset(buff[sg_used], 0, sz);
  1315. }
  1316. left -= sz;
  1317. data_ptr += sz;
  1318. sg_used++;
  1319. }
  1320. if ((c = cmd_alloc(host, 0)) == NULL) {
  1321. status = -ENOMEM;
  1322. goto cleanup1;
  1323. }
  1324. c->cmd_type = CMD_IOCTL_PEND;
  1325. c->Header.ReplyQueue = 0;
  1326. if (ioc->buf_size > 0) {
  1327. c->Header.SGList = sg_used;
  1328. c->Header.SGTotal = sg_used;
  1329. } else {
  1330. c->Header.SGList = 0;
  1331. c->Header.SGTotal = 0;
  1332. }
  1333. c->Header.LUN = ioc->LUN_info;
  1334. c->Header.Tag.lower = c->busaddr;
  1335. c->Request = ioc->Request;
  1336. if (ioc->buf_size > 0) {
  1337. int i;
  1338. for (i = 0; i < sg_used; i++) {
  1339. temp64.val =
  1340. pci_map_single(host->pdev, buff[i],
  1341. buff_size[i],
  1342. PCI_DMA_BIDIRECTIONAL);
  1343. c->SG[i].Addr.lower =
  1344. temp64.val32.lower;
  1345. c->SG[i].Addr.upper =
  1346. temp64.val32.upper;
  1347. c->SG[i].Len = buff_size[i];
  1348. c->SG[i].Ext = 0; /* we are not chaining */
  1349. }
  1350. }
  1351. c->waiting = &wait;
  1352. /* Put the request on the tail of the request queue */
  1353. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  1354. addQ(&host->reqQ, c);
  1355. host->Qdepth++;
  1356. start_io(host);
  1357. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1358. wait_for_completion(&wait);
  1359. /* unlock the buffers from DMA */
  1360. for (i = 0; i < sg_used; i++) {
  1361. temp64.val32.lower = c->SG[i].Addr.lower;
  1362. temp64.val32.upper = c->SG[i].Addr.upper;
  1363. pci_unmap_single(host->pdev,
  1364. (dma_addr_t) temp64.val, buff_size[i],
  1365. PCI_DMA_BIDIRECTIONAL);
  1366. }
  1367. check_ioctl_unit_attention(host, c);
  1368. /* Copy the error information out */
  1369. ioc->error_info = *(c->err_info);
  1370. if (copy_to_user(argp, ioc, sizeof(*ioc))) {
  1371. cmd_free(host, c, 0);
  1372. status = -EFAULT;
  1373. goto cleanup1;
  1374. }
  1375. if (ioc->Request.Type.Direction == XFER_READ) {
  1376. /* Copy the data out of the buffer we created */
  1377. BYTE __user *ptr = ioc->buf;
  1378. for (i = 0; i < sg_used; i++) {
  1379. if (copy_to_user
  1380. (ptr, buff[i], buff_size[i])) {
  1381. cmd_free(host, c, 0);
  1382. status = -EFAULT;
  1383. goto cleanup1;
  1384. }
  1385. ptr += buff_size[i];
  1386. }
  1387. }
  1388. cmd_free(host, c, 0);
  1389. status = 0;
  1390. cleanup1:
  1391. if (buff) {
  1392. for (i = 0; i < sg_used; i++)
  1393. kfree(buff[i]);
  1394. kfree(buff);
  1395. }
  1396. kfree(buff_size);
  1397. kfree(ioc);
  1398. return status;
  1399. }
  1400. /* scsi_cmd_ioctl handles these, below, though some are not */
  1401. /* very meaningful for cciss. SG_IO is the main one people want. */
  1402. case SG_GET_VERSION_NUM:
  1403. case SG_SET_TIMEOUT:
  1404. case SG_GET_TIMEOUT:
  1405. case SG_GET_RESERVED_SIZE:
  1406. case SG_SET_RESERVED_SIZE:
  1407. case SG_EMULATED_HOST:
  1408. case SG_IO:
  1409. case SCSI_IOCTL_SEND_COMMAND:
  1410. return scsi_cmd_ioctl(disk->queue, disk, mode, cmd, argp);
  1411. /* scsi_cmd_ioctl would normally handle these, below, but */
  1412. /* they aren't a good fit for cciss, as CD-ROMs are */
  1413. /* not supported, and we don't have any bus/target/lun */
  1414. /* which we present to the kernel. */
  1415. case CDROM_SEND_PACKET:
  1416. case CDROMCLOSETRAY:
  1417. case CDROMEJECT:
  1418. case SCSI_IOCTL_GET_IDLUN:
  1419. case SCSI_IOCTL_GET_BUS_NUMBER:
  1420. default:
  1421. return -ENOTTY;
  1422. }
  1423. }
  1424. static void cciss_check_queues(ctlr_info_t *h)
  1425. {
  1426. int start_queue = h->next_to_run;
  1427. int i;
  1428. /* check to see if we have maxed out the number of commands that can
  1429. * be placed on the queue. If so then exit. We do this check here
  1430. * in case the interrupt we serviced was from an ioctl and did not
  1431. * free any new commands.
  1432. */
  1433. if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds)
  1434. return;
  1435. /* We have room on the queue for more commands. Now we need to queue
  1436. * them up. We will also keep track of the next queue to run so
  1437. * that every queue gets a chance to be started first.
  1438. */
  1439. for (i = 0; i < h->highest_lun + 1; i++) {
  1440. int curr_queue = (start_queue + i) % (h->highest_lun + 1);
  1441. /* make sure the disk has been added and the drive is real
  1442. * because this can be called from the middle of init_one.
  1443. */
  1444. if (!h->drv[curr_queue])
  1445. continue;
  1446. if (!(h->drv[curr_queue]->queue) ||
  1447. !(h->drv[curr_queue]->heads))
  1448. continue;
  1449. blk_start_queue(h->gendisk[curr_queue]->queue);
  1450. /* check to see if we have maxed out the number of commands
  1451. * that can be placed on the queue.
  1452. */
  1453. if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds) {
  1454. if (curr_queue == start_queue) {
  1455. h->next_to_run =
  1456. (start_queue + 1) % (h->highest_lun + 1);
  1457. break;
  1458. } else {
  1459. h->next_to_run = curr_queue;
  1460. break;
  1461. }
  1462. }
  1463. }
  1464. }
  1465. static void cciss_softirq_done(struct request *rq)
  1466. {
  1467. CommandList_struct *cmd = rq->completion_data;
  1468. ctlr_info_t *h = hba[cmd->ctlr];
  1469. SGDescriptor_struct *curr_sg = cmd->SG;
  1470. unsigned long flags;
  1471. u64bit temp64;
  1472. int i, ddir;
  1473. int sg_index = 0;
  1474. if (cmd->Request.Type.Direction == XFER_READ)
  1475. ddir = PCI_DMA_FROMDEVICE;
  1476. else
  1477. ddir = PCI_DMA_TODEVICE;
  1478. /* command did not need to be retried */
  1479. /* unmap the DMA mapping for all the scatter gather elements */
  1480. for (i = 0; i < cmd->Header.SGList; i++) {
  1481. if (curr_sg[sg_index].Ext == CCISS_SG_CHAIN) {
  1482. temp64.val32.lower = cmd->SG[i].Addr.lower;
  1483. temp64.val32.upper = cmd->SG[i].Addr.upper;
  1484. pci_dma_sync_single_for_cpu(h->pdev, temp64.val,
  1485. cmd->SG[i].Len, ddir);
  1486. pci_unmap_single(h->pdev, temp64.val,
  1487. cmd->SG[i].Len, ddir);
  1488. /* Point to the next block */
  1489. curr_sg = h->cmd_sg_list[cmd->cmdindex]->sgchain;
  1490. sg_index = 0;
  1491. }
  1492. temp64.val32.lower = curr_sg[sg_index].Addr.lower;
  1493. temp64.val32.upper = curr_sg[sg_index].Addr.upper;
  1494. pci_unmap_page(h->pdev, temp64.val, curr_sg[sg_index].Len,
  1495. ddir);
  1496. ++sg_index;
  1497. }
  1498. #ifdef CCISS_DEBUG
  1499. printk("Done with %p\n", rq);
  1500. #endif /* CCISS_DEBUG */
  1501. /* set the residual count for pc requests */
  1502. if (blk_pc_request(rq))
  1503. rq->resid_len = cmd->err_info->ResidualCnt;
  1504. blk_end_request_all(rq, (rq->errors == 0) ? 0 : -EIO);
  1505. spin_lock_irqsave(&h->lock, flags);
  1506. cmd_free(h, cmd, 1);
  1507. cciss_check_queues(h);
  1508. spin_unlock_irqrestore(&h->lock, flags);
  1509. }
  1510. static inline void log_unit_to_scsi3addr(ctlr_info_t *h,
  1511. unsigned char scsi3addr[], uint32_t log_unit)
  1512. {
  1513. memcpy(scsi3addr, h->drv[log_unit]->LunID,
  1514. sizeof(h->drv[log_unit]->LunID));
  1515. }
  1516. /* This function gets the SCSI vendor, model, and revision of a logical drive
  1517. * via the inquiry page 0. Model, vendor, and rev are set to empty strings if
  1518. * they cannot be read.
  1519. */
  1520. static void cciss_get_device_descr(int ctlr, int logvol,
  1521. char *vendor, char *model, char *rev)
  1522. {
  1523. int rc;
  1524. InquiryData_struct *inq_buf;
  1525. unsigned char scsi3addr[8];
  1526. *vendor = '\0';
  1527. *model = '\0';
  1528. *rev = '\0';
  1529. inq_buf = kzalloc(sizeof(InquiryData_struct), GFP_KERNEL);
  1530. if (!inq_buf)
  1531. return;
  1532. log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
  1533. rc = sendcmd_withirq(CISS_INQUIRY, ctlr, inq_buf, sizeof(*inq_buf), 0,
  1534. scsi3addr, TYPE_CMD);
  1535. if (rc == IO_OK) {
  1536. memcpy(vendor, &inq_buf->data_byte[8], VENDOR_LEN);
  1537. vendor[VENDOR_LEN] = '\0';
  1538. memcpy(model, &inq_buf->data_byte[16], MODEL_LEN);
  1539. model[MODEL_LEN] = '\0';
  1540. memcpy(rev, &inq_buf->data_byte[32], REV_LEN);
  1541. rev[REV_LEN] = '\0';
  1542. }
  1543. kfree(inq_buf);
  1544. return;
  1545. }
  1546. /* This function gets the serial number of a logical drive via
  1547. * inquiry page 0x83. Serial no. is 16 bytes. If the serial
  1548. * number cannot be had, for whatever reason, 16 bytes of 0xff
  1549. * are returned instead.
  1550. */
  1551. static void cciss_get_serial_no(int ctlr, int logvol,
  1552. unsigned char *serial_no, int buflen)
  1553. {
  1554. #define PAGE_83_INQ_BYTES 64
  1555. int rc;
  1556. unsigned char *buf;
  1557. unsigned char scsi3addr[8];
  1558. if (buflen > 16)
  1559. buflen = 16;
  1560. memset(serial_no, 0xff, buflen);
  1561. buf = kzalloc(PAGE_83_INQ_BYTES, GFP_KERNEL);
  1562. if (!buf)
  1563. return;
  1564. memset(serial_no, 0, buflen);
  1565. log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
  1566. rc = sendcmd_withirq(CISS_INQUIRY, ctlr, buf,
  1567. PAGE_83_INQ_BYTES, 0x83, scsi3addr, TYPE_CMD);
  1568. if (rc == IO_OK)
  1569. memcpy(serial_no, &buf[8], buflen);
  1570. kfree(buf);
  1571. return;
  1572. }
  1573. /*
  1574. * cciss_add_disk sets up the block device queue for a logical drive
  1575. */
  1576. static int cciss_add_disk(ctlr_info_t *h, struct gendisk *disk,
  1577. int drv_index)
  1578. {
  1579. disk->queue = blk_init_queue(do_cciss_request, &h->lock);
  1580. if (!disk->queue)
  1581. goto init_queue_failure;
  1582. sprintf(disk->disk_name, "cciss/c%dd%d", h->ctlr, drv_index);
  1583. disk->major = h->major;
  1584. disk->first_minor = drv_index << NWD_SHIFT;
  1585. disk->fops = &cciss_fops;
  1586. if (cciss_create_ld_sysfs_entry(h, drv_index))
  1587. goto cleanup_queue;
  1588. disk->private_data = h->drv[drv_index];
  1589. disk->driverfs_dev = &h->drv[drv_index]->dev;
  1590. /* Set up queue information */
  1591. blk_queue_bounce_limit(disk->queue, h->pdev->dma_mask);
  1592. /* This is a hardware imposed limit. */
  1593. blk_queue_max_hw_segments(disk->queue, h->maxsgentries);
  1594. /* This is a limit in the driver and could be eliminated. */
  1595. blk_queue_max_phys_segments(disk->queue, h->maxsgentries);
  1596. blk_queue_max_sectors(disk->queue, h->cciss_max_sectors);
  1597. blk_queue_softirq_done(disk->queue, cciss_softirq_done);
  1598. disk->queue->queuedata = h;
  1599. blk_queue_logical_block_size(disk->queue,
  1600. h->drv[drv_index]->block_size);
  1601. /* Make sure all queue data is written out before */
  1602. /* setting h->drv[drv_index]->queue, as setting this */
  1603. /* allows the interrupt handler to start the queue */
  1604. wmb();
  1605. h->drv[drv_index]->queue = disk->queue;
  1606. add_disk(disk);
  1607. return 0;
  1608. cleanup_queue:
  1609. blk_cleanup_queue(disk->queue);
  1610. disk->queue = NULL;
  1611. init_queue_failure:
  1612. return -1;
  1613. }
  1614. /* This function will check the usage_count of the drive to be updated/added.
  1615. * If the usage_count is zero and it is a heretofore unknown drive, or,
  1616. * the drive's capacity, geometry, or serial number has changed,
  1617. * then the drive information will be updated and the disk will be
  1618. * re-registered with the kernel. If these conditions don't hold,
  1619. * then it will be left alone for the next reboot. The exception to this
  1620. * is disk 0 which will always be left registered with the kernel since it
  1621. * is also the controller node. Any changes to disk 0 will show up on
  1622. * the next reboot.
  1623. */
  1624. static void cciss_update_drive_info(int ctlr, int drv_index, int first_time,
  1625. int via_ioctl)
  1626. {
  1627. ctlr_info_t *h = hba[ctlr];
  1628. struct gendisk *disk;
  1629. InquiryData_struct *inq_buff = NULL;
  1630. unsigned int block_size;
  1631. sector_t total_size;
  1632. unsigned long flags = 0;
  1633. int ret = 0;
  1634. drive_info_struct *drvinfo;
  1635. /* Get information about the disk and modify the driver structure */
  1636. inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
  1637. drvinfo = kzalloc(sizeof(*drvinfo), GFP_KERNEL);
  1638. if (inq_buff == NULL || drvinfo == NULL)
  1639. goto mem_msg;
  1640. /* testing to see if 16-byte CDBs are already being used */
  1641. if (h->cciss_read == CCISS_READ_16) {
  1642. cciss_read_capacity_16(h->ctlr, drv_index,
  1643. &total_size, &block_size);
  1644. } else {
  1645. cciss_read_capacity(ctlr, drv_index, &total_size, &block_size);
  1646. /* if read_capacity returns all F's this volume is >2TB */
  1647. /* in size so we switch to 16-byte CDB's for all */
  1648. /* read/write ops */
  1649. if (total_size == 0xFFFFFFFFULL) {
  1650. cciss_read_capacity_16(ctlr, drv_index,
  1651. &total_size, &block_size);
  1652. h->cciss_read = CCISS_READ_16;
  1653. h->cciss_write = CCISS_WRITE_16;
  1654. } else {
  1655. h->cciss_read = CCISS_READ_10;
  1656. h->cciss_write = CCISS_WRITE_10;
  1657. }
  1658. }
  1659. cciss_geometry_inquiry(ctlr, drv_index, total_size, block_size,
  1660. inq_buff, drvinfo);
  1661. drvinfo->block_size = block_size;
  1662. drvinfo->nr_blocks = total_size + 1;
  1663. cciss_get_device_descr(ctlr, drv_index, drvinfo->vendor,
  1664. drvinfo->model, drvinfo->rev);
  1665. cciss_get_serial_no(ctlr, drv_index, drvinfo->serial_no,
  1666. sizeof(drvinfo->serial_no));
  1667. /* Save the lunid in case we deregister the disk, below. */
  1668. memcpy(drvinfo->LunID, h->drv[drv_index]->LunID,
  1669. sizeof(drvinfo->LunID));
  1670. /* Is it the same disk we already know, and nothing's changed? */
  1671. if (h->drv[drv_index]->raid_level != -1 &&
  1672. ((memcmp(drvinfo->serial_no,
  1673. h->drv[drv_index]->serial_no, 16) == 0) &&
  1674. drvinfo->block_size == h->drv[drv_index]->block_size &&
  1675. drvinfo->nr_blocks == h->drv[drv_index]->nr_blocks &&
  1676. drvinfo->heads == h->drv[drv_index]->heads &&
  1677. drvinfo->sectors == h->drv[drv_index]->sectors &&
  1678. drvinfo->cylinders == h->drv[drv_index]->cylinders))
  1679. /* The disk is unchanged, nothing to update */
  1680. goto freeret;
  1681. /* If we get here it's not the same disk, or something's changed,
  1682. * so we need to * deregister it, and re-register it, if it's not
  1683. * in use.
  1684. * If the disk already exists then deregister it before proceeding
  1685. * (unless it's the first disk (for the controller node).
  1686. */
  1687. if (h->drv[drv_index]->raid_level != -1 && drv_index != 0) {
  1688. printk(KERN_WARNING "disk %d has changed.\n", drv_index);
  1689. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  1690. h->drv[drv_index]->busy_configuring = 1;
  1691. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1692. /* deregister_disk sets h->drv[drv_index]->queue = NULL
  1693. * which keeps the interrupt handler from starting
  1694. * the queue.
  1695. */
  1696. ret = deregister_disk(h, drv_index, 0, via_ioctl);
  1697. }
  1698. /* If the disk is in use return */
  1699. if (ret)
  1700. goto freeret;
  1701. /* Save the new information from cciss_geometry_inquiry
  1702. * and serial number inquiry. If the disk was deregistered
  1703. * above, then h->drv[drv_index] will be NULL.
  1704. */
  1705. if (h->drv[drv_index] == NULL) {
  1706. drvinfo->device_initialized = 0;
  1707. h->drv[drv_index] = drvinfo;
  1708. drvinfo = NULL; /* so it won't be freed below. */
  1709. } else {
  1710. /* special case for cxd0 */
  1711. h->drv[drv_index]->block_size = drvinfo->block_size;
  1712. h->drv[drv_index]->nr_blocks = drvinfo->nr_blocks;
  1713. h->drv[drv_index]->heads = drvinfo->heads;
  1714. h->drv[drv_index]->sectors = drvinfo->sectors;
  1715. h->drv[drv_index]->cylinders = drvinfo->cylinders;
  1716. h->drv[drv_index]->raid_level = drvinfo->raid_level;
  1717. memcpy(h->drv[drv_index]->serial_no, drvinfo->serial_no, 16);
  1718. memcpy(h->drv[drv_index]->vendor, drvinfo->vendor,
  1719. VENDOR_LEN + 1);
  1720. memcpy(h->drv[drv_index]->model, drvinfo->model, MODEL_LEN + 1);
  1721. memcpy(h->drv[drv_index]->rev, drvinfo->rev, REV_LEN + 1);
  1722. }
  1723. ++h->num_luns;
  1724. disk = h->gendisk[drv_index];
  1725. set_capacity(disk, h->drv[drv_index]->nr_blocks);
  1726. /* If it's not disk 0 (drv_index != 0)
  1727. * or if it was disk 0, but there was previously
  1728. * no actual corresponding configured logical drive
  1729. * (raid_leve == -1) then we want to update the
  1730. * logical drive's information.
  1731. */
  1732. if (drv_index || first_time) {
  1733. if (cciss_add_disk(h, disk, drv_index) != 0) {
  1734. cciss_free_gendisk(h, drv_index);
  1735. cciss_free_drive_info(h, drv_index);
  1736. printk(KERN_WARNING "cciss:%d could not update "
  1737. "disk %d\n", h->ctlr, drv_index);
  1738. --h->num_luns;
  1739. }
  1740. }
  1741. freeret:
  1742. kfree(inq_buff);
  1743. kfree(drvinfo);
  1744. return;
  1745. mem_msg:
  1746. printk(KERN_ERR "cciss: out of memory\n");
  1747. goto freeret;
  1748. }
  1749. /* This function will find the first index of the controllers drive array
  1750. * that has a null drv pointer and allocate the drive info struct and
  1751. * will return that index This is where new drives will be added.
  1752. * If the index to be returned is greater than the highest_lun index for
  1753. * the controller then highest_lun is set * to this new index.
  1754. * If there are no available indexes or if tha allocation fails, then -1
  1755. * is returned. * "controller_node" is used to know if this is a real
  1756. * logical drive, or just the controller node, which determines if this
  1757. * counts towards highest_lun.
  1758. */
  1759. static int cciss_alloc_drive_info(ctlr_info_t *h, int controller_node)
  1760. {
  1761. int i;
  1762. drive_info_struct *drv;
  1763. /* Search for an empty slot for our drive info */
  1764. for (i = 0; i < CISS_MAX_LUN; i++) {
  1765. /* if not cxd0 case, and it's occupied, skip it. */
  1766. if (h->drv[i] && i != 0)
  1767. continue;
  1768. /*
  1769. * If it's cxd0 case, and drv is alloc'ed already, and a
  1770. * disk is configured there, skip it.
  1771. */
  1772. if (i == 0 && h->drv[i] && h->drv[i]->raid_level != -1)
  1773. continue;
  1774. /*
  1775. * We've found an empty slot. Update highest_lun
  1776. * provided this isn't just the fake cxd0 controller node.
  1777. */
  1778. if (i > h->highest_lun && !controller_node)
  1779. h->highest_lun = i;
  1780. /* If adding a real disk at cxd0, and it's already alloc'ed */
  1781. if (i == 0 && h->drv[i] != NULL)
  1782. return i;
  1783. /*
  1784. * Found an empty slot, not already alloc'ed. Allocate it.
  1785. * Mark it with raid_level == -1, so we know it's new later on.
  1786. */
  1787. drv = kzalloc(sizeof(*drv), GFP_KERNEL);
  1788. if (!drv)
  1789. return -1;
  1790. drv->raid_level = -1; /* so we know it's new */
  1791. h->drv[i] = drv;
  1792. return i;
  1793. }
  1794. return -1;
  1795. }
  1796. static void cciss_free_drive_info(ctlr_info_t *h, int drv_index)
  1797. {
  1798. kfree(h->drv[drv_index]);
  1799. h->drv[drv_index] = NULL;
  1800. }
  1801. static void cciss_free_gendisk(ctlr_info_t *h, int drv_index)
  1802. {
  1803. put_disk(h->gendisk[drv_index]);
  1804. h->gendisk[drv_index] = NULL;
  1805. }
  1806. /* cciss_add_gendisk finds a free hba[]->drv structure
  1807. * and allocates a gendisk if needed, and sets the lunid
  1808. * in the drvinfo structure. It returns the index into
  1809. * the ->drv[] array, or -1 if none are free.
  1810. * is_controller_node indicates whether highest_lun should
  1811. * count this disk, or if it's only being added to provide
  1812. * a means to talk to the controller in case no logical
  1813. * drives have yet been configured.
  1814. */
  1815. static int cciss_add_gendisk(ctlr_info_t *h, unsigned char lunid[],
  1816. int controller_node)
  1817. {
  1818. int drv_index;
  1819. drv_index = cciss_alloc_drive_info(h, controller_node);
  1820. if (drv_index == -1)
  1821. return -1;
  1822. /*Check if the gendisk needs to be allocated */
  1823. if (!h->gendisk[drv_index]) {
  1824. h->gendisk[drv_index] =
  1825. alloc_disk(1 << NWD_SHIFT);
  1826. if (!h->gendisk[drv_index]) {
  1827. printk(KERN_ERR "cciss%d: could not "
  1828. "allocate a new disk %d\n",
  1829. h->ctlr, drv_index);
  1830. goto err_free_drive_info;
  1831. }
  1832. }
  1833. memcpy(h->drv[drv_index]->LunID, lunid,
  1834. sizeof(h->drv[drv_index]->LunID));
  1835. if (cciss_create_ld_sysfs_entry(h, drv_index))
  1836. goto err_free_disk;
  1837. /* Don't need to mark this busy because nobody */
  1838. /* else knows about this disk yet to contend */
  1839. /* for access to it. */
  1840. h->drv[drv_index]->busy_configuring = 0;
  1841. wmb();
  1842. return drv_index;
  1843. err_free_disk:
  1844. cciss_free_gendisk(h, drv_index);
  1845. err_free_drive_info:
  1846. cciss_free_drive_info(h, drv_index);
  1847. return -1;
  1848. }
  1849. /* This is for the special case of a controller which
  1850. * has no logical drives. In this case, we still need
  1851. * to register a disk so the controller can be accessed
  1852. * by the Array Config Utility.
  1853. */
  1854. static void cciss_add_controller_node(ctlr_info_t *h)
  1855. {
  1856. struct gendisk *disk;
  1857. int drv_index;
  1858. if (h->gendisk[0] != NULL) /* already did this? Then bail. */
  1859. return;
  1860. drv_index = cciss_add_gendisk(h, CTLR_LUNID, 1);
  1861. if (drv_index == -1)
  1862. goto error;
  1863. h->drv[drv_index]->block_size = 512;
  1864. h->drv[drv_index]->nr_blocks = 0;
  1865. h->drv[drv_index]->heads = 0;
  1866. h->drv[drv_index]->sectors = 0;
  1867. h->drv[drv_index]->cylinders = 0;
  1868. h->drv[drv_index]->raid_level = -1;
  1869. memset(h->drv[drv_index]->serial_no, 0, 16);
  1870. disk = h->gendisk[drv_index];
  1871. if (cciss_add_disk(h, disk, drv_index) == 0)
  1872. return;
  1873. cciss_free_gendisk(h, drv_index);
  1874. cciss_free_drive_info(h, drv_index);
  1875. error:
  1876. printk(KERN_WARNING "cciss%d: could not "
  1877. "add disk 0.\n", h->ctlr);
  1878. return;
  1879. }
  1880. /* This function will add and remove logical drives from the Logical
  1881. * drive array of the controller and maintain persistency of ordering
  1882. * so that mount points are preserved until the next reboot. This allows
  1883. * for the removal of logical drives in the middle of the drive array
  1884. * without a re-ordering of those drives.
  1885. * INPUT
  1886. * h = The controller to perform the operations on
  1887. */
  1888. static int rebuild_lun_table(ctlr_info_t *h, int first_time,
  1889. int via_ioctl)
  1890. {
  1891. int ctlr = h->ctlr;
  1892. int num_luns;
  1893. ReportLunData_struct *ld_buff = NULL;
  1894. int return_code;
  1895. int listlength = 0;
  1896. int i;
  1897. int drv_found;
  1898. int drv_index = 0;
  1899. unsigned char lunid[8] = CTLR_LUNID;
  1900. unsigned long flags;
  1901. if (!capable(CAP_SYS_RAWIO))
  1902. return -EPERM;
  1903. /* Set busy_configuring flag for this operation */
  1904. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  1905. if (h->busy_configuring) {
  1906. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1907. return -EBUSY;
  1908. }
  1909. h->busy_configuring = 1;
  1910. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1911. ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
  1912. if (ld_buff == NULL)
  1913. goto mem_msg;
  1914. return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
  1915. sizeof(ReportLunData_struct),
  1916. 0, CTLR_LUNID, TYPE_CMD);
  1917. if (return_code == IO_OK)
  1918. listlength = be32_to_cpu(*(__be32 *) ld_buff->LUNListLength);
  1919. else { /* reading number of logical volumes failed */
  1920. printk(KERN_WARNING "cciss: report logical volume"
  1921. " command failed\n");
  1922. listlength = 0;
  1923. goto freeret;
  1924. }
  1925. num_luns = listlength / 8; /* 8 bytes per entry */
  1926. if (num_luns > CISS_MAX_LUN) {
  1927. num_luns = CISS_MAX_LUN;
  1928. printk(KERN_WARNING "cciss: more luns configured"
  1929. " on controller than can be handled by"
  1930. " this driver.\n");
  1931. }
  1932. if (num_luns == 0)
  1933. cciss_add_controller_node(h);
  1934. /* Compare controller drive array to driver's drive array
  1935. * to see if any drives are missing on the controller due
  1936. * to action of Array Config Utility (user deletes drive)
  1937. * and deregister logical drives which have disappeared.
  1938. */
  1939. for (i = 0; i <= h->highest_lun; i++) {
  1940. int j;
  1941. drv_found = 0;
  1942. /* skip holes in the array from already deleted drives */
  1943. if (h->drv[i] == NULL)
  1944. continue;
  1945. for (j = 0; j < num_luns; j++) {
  1946. memcpy(lunid, &ld_buff->LUN[j][0], sizeof(lunid));
  1947. if (memcmp(h->drv[i]->LunID, lunid,
  1948. sizeof(lunid)) == 0) {
  1949. drv_found = 1;
  1950. break;
  1951. }
  1952. }
  1953. if (!drv_found) {
  1954. /* Deregister it from the OS, it's gone. */
  1955. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  1956. h->drv[i]->busy_configuring = 1;
  1957. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1958. return_code = deregister_disk(h, i, 1, via_ioctl);
  1959. if (h->drv[i] != NULL)
  1960. h->drv[i]->busy_configuring = 0;
  1961. }
  1962. }
  1963. /* Compare controller drive array to driver's drive array.
  1964. * Check for updates in the drive information and any new drives
  1965. * on the controller due to ACU adding logical drives, or changing
  1966. * a logical drive's size, etc. Reregister any new/changed drives
  1967. */
  1968. for (i = 0; i < num_luns; i++) {
  1969. int j;
  1970. drv_found = 0;
  1971. memcpy(lunid, &ld_buff->LUN[i][0], sizeof(lunid));
  1972. /* Find if the LUN is already in the drive array
  1973. * of the driver. If so then update its info
  1974. * if not in use. If it does not exist then find
  1975. * the first free index and add it.
  1976. */
  1977. for (j = 0; j <= h->highest_lun; j++) {
  1978. if (h->drv[j] != NULL &&
  1979. memcmp(h->drv[j]->LunID, lunid,
  1980. sizeof(h->drv[j]->LunID)) == 0) {
  1981. drv_index = j;
  1982. drv_found = 1;
  1983. break;
  1984. }
  1985. }
  1986. /* check if the drive was found already in the array */
  1987. if (!drv_found) {
  1988. drv_index = cciss_add_gendisk(h, lunid, 0);
  1989. if (drv_index == -1)
  1990. goto freeret;
  1991. }
  1992. cciss_update_drive_info(ctlr, drv_index, first_time,
  1993. via_ioctl);
  1994. } /* end for */
  1995. freeret:
  1996. kfree(ld_buff);
  1997. h->busy_configuring = 0;
  1998. /* We return -1 here to tell the ACU that we have registered/updated
  1999. * all of the drives that we can and to keep it from calling us
  2000. * additional times.
  2001. */
  2002. return -1;
  2003. mem_msg:
  2004. printk(KERN_ERR "cciss: out of memory\n");
  2005. h->busy_configuring = 0;
  2006. goto freeret;
  2007. }
  2008. static void cciss_clear_drive_info(drive_info_struct *drive_info)
  2009. {
  2010. /* zero out the disk size info */
  2011. drive_info->nr_blocks = 0;
  2012. drive_info->block_size = 0;
  2013. drive_info->heads = 0;
  2014. drive_info->sectors = 0;
  2015. drive_info->cylinders = 0;
  2016. drive_info->raid_level = -1;
  2017. memset(drive_info->serial_no, 0, sizeof(drive_info->serial_no));
  2018. memset(drive_info->model, 0, sizeof(drive_info->model));
  2019. memset(drive_info->rev, 0, sizeof(drive_info->rev));
  2020. memset(drive_info->vendor, 0, sizeof(drive_info->vendor));
  2021. /*
  2022. * don't clear the LUNID though, we need to remember which
  2023. * one this one is.
  2024. */
  2025. }
  2026. /* This function will deregister the disk and it's queue from the
  2027. * kernel. It must be called with the controller lock held and the
  2028. * drv structures busy_configuring flag set. It's parameters are:
  2029. *
  2030. * disk = This is the disk to be deregistered
  2031. * drv = This is the drive_info_struct associated with the disk to be
  2032. * deregistered. It contains information about the disk used
  2033. * by the driver.
  2034. * clear_all = This flag determines whether or not the disk information
  2035. * is going to be completely cleared out and the highest_lun
  2036. * reset. Sometimes we want to clear out information about
  2037. * the disk in preparation for re-adding it. In this case
  2038. * the highest_lun should be left unchanged and the LunID
  2039. * should not be cleared.
  2040. * via_ioctl
  2041. * This indicates whether we've reached this path via ioctl.
  2042. * This affects the maximum usage count allowed for c0d0 to be messed with.
  2043. * If this path is reached via ioctl(), then the max_usage_count will
  2044. * be 1, as the process calling ioctl() has got to have the device open.
  2045. * If we get here via sysfs, then the max usage count will be zero.
  2046. */
  2047. static int deregister_disk(ctlr_info_t *h, int drv_index,
  2048. int clear_all, int via_ioctl)
  2049. {
  2050. int i;
  2051. struct gendisk *disk;
  2052. drive_info_struct *drv;
  2053. int recalculate_highest_lun;
  2054. if (!capable(CAP_SYS_RAWIO))
  2055. return -EPERM;
  2056. drv = h->drv[drv_index];
  2057. disk = h->gendisk[drv_index];
  2058. /* make sure logical volume is NOT is use */
  2059. if (clear_all || (h->gendisk[0] == disk)) {
  2060. if (drv->usage_count > via_ioctl)
  2061. return -EBUSY;
  2062. } else if (drv->usage_count > 0)
  2063. return -EBUSY;
  2064. recalculate_highest_lun = (drv == h->drv[h->highest_lun]);
  2065. /* invalidate the devices and deregister the disk. If it is disk
  2066. * zero do not deregister it but just zero out it's values. This
  2067. * allows us to delete disk zero but keep the controller registered.
  2068. */
  2069. if (h->gendisk[0] != disk) {
  2070. struct request_queue *q = disk->queue;
  2071. if (disk->flags & GENHD_FL_UP) {
  2072. cciss_destroy_ld_sysfs_entry(h, drv_index, 0);
  2073. del_gendisk(disk);
  2074. }
  2075. if (q)
  2076. blk_cleanup_queue(q);
  2077. /* If clear_all is set then we are deleting the logical
  2078. * drive, not just refreshing its info. For drives
  2079. * other than disk 0 we will call put_disk. We do not
  2080. * do this for disk 0 as we need it to be able to
  2081. * configure the controller.
  2082. */
  2083. if (clear_all){
  2084. /* This isn't pretty, but we need to find the
  2085. * disk in our array and NULL our the pointer.
  2086. * This is so that we will call alloc_disk if
  2087. * this index is used again later.
  2088. */
  2089. for (i=0; i < CISS_MAX_LUN; i++){
  2090. if (h->gendisk[i] == disk) {
  2091. h->gendisk[i] = NULL;
  2092. break;
  2093. }
  2094. }
  2095. put_disk(disk);
  2096. }
  2097. } else {
  2098. set_capacity(disk, 0);
  2099. cciss_clear_drive_info(drv);
  2100. }
  2101. --h->num_luns;
  2102. /* if it was the last disk, find the new hightest lun */
  2103. if (clear_all && recalculate_highest_lun) {
  2104. int i, newhighest = -1;
  2105. for (i = 0; i <= h->highest_lun; i++) {
  2106. /* if the disk has size > 0, it is available */
  2107. if (h->drv[i] && h->drv[i]->heads)
  2108. newhighest = i;
  2109. }
  2110. h->highest_lun = newhighest;
  2111. }
  2112. return 0;
  2113. }
  2114. static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
  2115. size_t size, __u8 page_code, unsigned char *scsi3addr,
  2116. int cmd_type)
  2117. {
  2118. ctlr_info_t *h = hba[ctlr];
  2119. u64bit buff_dma_handle;
  2120. int status = IO_OK;
  2121. c->cmd_type = CMD_IOCTL_PEND;
  2122. c->Header.ReplyQueue = 0;
  2123. if (buff != NULL) {
  2124. c->Header.SGList = 1;
  2125. c->Header.SGTotal = 1;
  2126. } else {
  2127. c->Header.SGList = 0;
  2128. c->Header.SGTotal = 0;
  2129. }
  2130. c->Header.Tag.lower = c->busaddr;
  2131. memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
  2132. c->Request.Type.Type = cmd_type;
  2133. if (cmd_type == TYPE_CMD) {
  2134. switch (cmd) {
  2135. case CISS_INQUIRY:
  2136. /* are we trying to read a vital product page */
  2137. if (page_code != 0) {
  2138. c->Request.CDB[1] = 0x01;
  2139. c->Request.CDB[2] = page_code;
  2140. }
  2141. c->Request.CDBLen = 6;
  2142. c->Request.Type.Attribute = ATTR_SIMPLE;
  2143. c->Request.Type.Direction = XFER_READ;
  2144. c->Request.Timeout = 0;
  2145. c->Request.CDB[0] = CISS_INQUIRY;
  2146. c->Request.CDB[4] = size & 0xFF;
  2147. break;
  2148. case CISS_REPORT_LOG:
  2149. case CISS_REPORT_PHYS:
  2150. /* Talking to controller so It's a physical command
  2151. mode = 00 target = 0. Nothing to write.
  2152. */
  2153. c->Request.CDBLen = 12;
  2154. c->Request.Type.Attribute = ATTR_SIMPLE;
  2155. c->Request.Type.Direction = XFER_READ;
  2156. c->Request.Timeout = 0;
  2157. c->Request.CDB[0] = cmd;
  2158. c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
  2159. c->Request.CDB[7] = (size >> 16) & 0xFF;
  2160. c->Request.CDB[8] = (size >> 8) & 0xFF;
  2161. c->Request.CDB[9] = size & 0xFF;
  2162. break;
  2163. case CCISS_READ_CAPACITY:
  2164. c->Request.CDBLen = 10;
  2165. c->Request.Type.Attribute = ATTR_SIMPLE;
  2166. c->Request.Type.Direction = XFER_READ;
  2167. c->Request.Timeout = 0;
  2168. c->Request.CDB[0] = cmd;
  2169. break;
  2170. case CCISS_READ_CAPACITY_16:
  2171. c->Request.CDBLen = 16;
  2172. c->Request.Type.Attribute = ATTR_SIMPLE;
  2173. c->Request.Type.Direction = XFER_READ;
  2174. c->Request.Timeout = 0;
  2175. c->Request.CDB[0] = cmd;
  2176. c->Request.CDB[1] = 0x10;
  2177. c->Request.CDB[10] = (size >> 24) & 0xFF;
  2178. c->Request.CDB[11] = (size >> 16) & 0xFF;
  2179. c->Request.CDB[12] = (size >> 8) & 0xFF;
  2180. c->Request.CDB[13] = size & 0xFF;
  2181. c->Request.Timeout = 0;
  2182. c->Request.CDB[0] = cmd;
  2183. break;
  2184. case CCISS_CACHE_FLUSH:
  2185. c->Request.CDBLen = 12;
  2186. c->Request.Type.Attribute = ATTR_SIMPLE;
  2187. c->Request.Type.Direction = XFER_WRITE;
  2188. c->Request.Timeout = 0;
  2189. c->Request.CDB[0] = BMIC_WRITE;
  2190. c->Request.CDB[6] = BMIC_CACHE_FLUSH;
  2191. break;
  2192. case TEST_UNIT_READY:
  2193. c->Request.CDBLen = 6;
  2194. c->Request.Type.Attribute = ATTR_SIMPLE;
  2195. c->Request.Type.Direction = XFER_NONE;
  2196. c->Request.Timeout = 0;
  2197. break;
  2198. default:
  2199. printk(KERN_WARNING
  2200. "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
  2201. return IO_ERROR;
  2202. }
  2203. } else if (cmd_type == TYPE_MSG) {
  2204. switch (cmd) {
  2205. case 0: /* ABORT message */
  2206. c->Request.CDBLen = 12;
  2207. c->Request.Type.Attribute = ATTR_SIMPLE;
  2208. c->Request.Type.Direction = XFER_WRITE;
  2209. c->Request.Timeout = 0;
  2210. c->Request.CDB[0] = cmd; /* abort */
  2211. c->Request.CDB[1] = 0; /* abort a command */
  2212. /* buff contains the tag of the command to abort */
  2213. memcpy(&c->Request.CDB[4], buff, 8);
  2214. break;
  2215. case 1: /* RESET message */
  2216. c->Request.CDBLen = 16;
  2217. c->Request.Type.Attribute = ATTR_SIMPLE;
  2218. c->Request.Type.Direction = XFER_NONE;
  2219. c->Request.Timeout = 0;
  2220. memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
  2221. c->Request.CDB[0] = cmd; /* reset */
  2222. c->Request.CDB[1] = 0x03; /* reset a target */
  2223. break;
  2224. case 3: /* No-Op message */
  2225. c->Request.CDBLen = 1;
  2226. c->Request.Type.Attribute = ATTR_SIMPLE;
  2227. c->Request.Type.Direction = XFER_WRITE;
  2228. c->Request.Timeout = 0;
  2229. c->Request.CDB[0] = cmd;
  2230. break;
  2231. default:
  2232. printk(KERN_WARNING
  2233. "cciss%d: unknown message type %d\n", ctlr, cmd);
  2234. return IO_ERROR;
  2235. }
  2236. } else {
  2237. printk(KERN_WARNING
  2238. "cciss%d: unknown command type %d\n", ctlr, cmd_type);
  2239. return IO_ERROR;
  2240. }
  2241. /* Fill in the scatter gather information */
  2242. if (size > 0) {
  2243. buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
  2244. buff, size,
  2245. PCI_DMA_BIDIRECTIONAL);
  2246. c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
  2247. c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
  2248. c->SG[0].Len = size;
  2249. c->SG[0].Ext = 0; /* we are not chaining */
  2250. }
  2251. return status;
  2252. }
  2253. static int check_target_status(ctlr_info_t *h, CommandList_struct *c)
  2254. {
  2255. switch (c->err_info->ScsiStatus) {
  2256. case SAM_STAT_GOOD:
  2257. return IO_OK;
  2258. case SAM_STAT_CHECK_CONDITION:
  2259. switch (0xf & c->err_info->SenseInfo[2]) {
  2260. case 0: return IO_OK; /* no sense */
  2261. case 1: return IO_OK; /* recovered error */
  2262. default:
  2263. if (check_for_unit_attention(h, c))
  2264. return IO_NEEDS_RETRY;
  2265. printk(KERN_WARNING "cciss%d: cmd 0x%02x "
  2266. "check condition, sense key = 0x%02x\n",
  2267. h->ctlr, c->Request.CDB[0],
  2268. c->err_info->SenseInfo[2]);
  2269. }
  2270. break;
  2271. default:
  2272. printk(KERN_WARNING "cciss%d: cmd 0x%02x"
  2273. "scsi status = 0x%02x\n", h->ctlr,
  2274. c->Request.CDB[0], c->err_info->ScsiStatus);
  2275. break;
  2276. }
  2277. return IO_ERROR;
  2278. }
  2279. static int process_sendcmd_error(ctlr_info_t *h, CommandList_struct *c)
  2280. {
  2281. int return_status = IO_OK;
  2282. if (c->err_info->CommandStatus == CMD_SUCCESS)
  2283. return IO_OK;
  2284. switch (c->err_info->CommandStatus) {
  2285. case CMD_TARGET_STATUS:
  2286. return_status = check_target_status(h, c);
  2287. break;
  2288. case CMD_DATA_UNDERRUN:
  2289. case CMD_DATA_OVERRUN:
  2290. /* expected for inquiry and report lun commands */
  2291. break;
  2292. case CMD_INVALID:
  2293. printk(KERN_WARNING "cciss: cmd 0x%02x is "
  2294. "reported invalid\n", c->Request.CDB[0]);
  2295. return_status = IO_ERROR;
  2296. break;
  2297. case CMD_PROTOCOL_ERR:
  2298. printk(KERN_WARNING "cciss: cmd 0x%02x has "
  2299. "protocol error \n", c->Request.CDB[0]);
  2300. return_status = IO_ERROR;
  2301. break;
  2302. case CMD_HARDWARE_ERR:
  2303. printk(KERN_WARNING "cciss: cmd 0x%02x had "
  2304. " hardware error\n", c->Request.CDB[0]);
  2305. return_status = IO_ERROR;
  2306. break;
  2307. case CMD_CONNECTION_LOST:
  2308. printk(KERN_WARNING "cciss: cmd 0x%02x had "
  2309. "connection lost\n", c->Request.CDB[0]);
  2310. return_status = IO_ERROR;
  2311. break;
  2312. case CMD_ABORTED:
  2313. printk(KERN_WARNING "cciss: cmd 0x%02x was "
  2314. "aborted\n", c->Request.CDB[0]);
  2315. return_status = IO_ERROR;
  2316. break;
  2317. case CMD_ABORT_FAILED:
  2318. printk(KERN_WARNING "cciss: cmd 0x%02x reports "
  2319. "abort failed\n", c->Request.CDB[0]);
  2320. return_status = IO_ERROR;
  2321. break;
  2322. case CMD_UNSOLICITED_ABORT:
  2323. printk(KERN_WARNING
  2324. "cciss%d: unsolicited abort 0x%02x\n", h->ctlr,
  2325. c->Request.CDB[0]);
  2326. return_status = IO_NEEDS_RETRY;
  2327. break;
  2328. default:
  2329. printk(KERN_WARNING "cciss: cmd 0x%02x returned "
  2330. "unknown status %x\n", c->Request.CDB[0],
  2331. c->err_info->CommandStatus);
  2332. return_status = IO_ERROR;
  2333. }
  2334. return return_status;
  2335. }
  2336. static int sendcmd_withirq_core(ctlr_info_t *h, CommandList_struct *c,
  2337. int attempt_retry)
  2338. {
  2339. DECLARE_COMPLETION_ONSTACK(wait);
  2340. u64bit buff_dma_handle;
  2341. unsigned long flags;
  2342. int return_status = IO_OK;
  2343. resend_cmd2:
  2344. c->waiting = &wait;
  2345. /* Put the request on the tail of the queue and send it */
  2346. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  2347. addQ(&h->reqQ, c);
  2348. h->Qdepth++;
  2349. start_io(h);
  2350. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  2351. wait_for_completion(&wait);
  2352. if (c->err_info->CommandStatus == 0 || !attempt_retry)
  2353. goto command_done;
  2354. return_status = process_sendcmd_error(h, c);
  2355. if (return_status == IO_NEEDS_RETRY &&
  2356. c->retry_count < MAX_CMD_RETRIES) {
  2357. printk(KERN_WARNING "cciss%d: retrying 0x%02x\n", h->ctlr,
  2358. c->Request.CDB[0]);
  2359. c->retry_count++;
  2360. /* erase the old error information */
  2361. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  2362. return_status = IO_OK;
  2363. INIT_COMPLETION(wait);
  2364. goto resend_cmd2;
  2365. }
  2366. command_done:
  2367. /* unlock the buffers from DMA */
  2368. buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
  2369. buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
  2370. pci_unmap_single(h->pdev, (dma_addr_t) buff_dma_handle.val,
  2371. c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
  2372. return return_status;
  2373. }
  2374. static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
  2375. __u8 page_code, unsigned char scsi3addr[],
  2376. int cmd_type)
  2377. {
  2378. ctlr_info_t *h = hba[ctlr];
  2379. CommandList_struct *c;
  2380. int return_status;
  2381. c = cmd_alloc(h, 0);
  2382. if (!c)
  2383. return -ENOMEM;
  2384. return_status = fill_cmd(c, cmd, ctlr, buff, size, page_code,
  2385. scsi3addr, cmd_type);
  2386. if (return_status == IO_OK)
  2387. return_status = sendcmd_withirq_core(h, c, 1);
  2388. cmd_free(h, c, 0);
  2389. return return_status;
  2390. }
  2391. static void cciss_geometry_inquiry(int ctlr, int logvol,
  2392. sector_t total_size,
  2393. unsigned int block_size,
  2394. InquiryData_struct *inq_buff,
  2395. drive_info_struct *drv)
  2396. {
  2397. int return_code;
  2398. unsigned long t;
  2399. unsigned char scsi3addr[8];
  2400. memset(inq_buff, 0, sizeof(InquiryData_struct));
  2401. log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
  2402. return_code = sendcmd_withirq(CISS_INQUIRY, ctlr, inq_buff,
  2403. sizeof(*inq_buff), 0xC1, scsi3addr, TYPE_CMD);
  2404. if (return_code == IO_OK) {
  2405. if (inq_buff->data_byte[8] == 0xFF) {
  2406. printk(KERN_WARNING
  2407. "cciss: reading geometry failed, volume "
  2408. "does not support reading geometry\n");
  2409. drv->heads = 255;
  2410. drv->sectors = 32; // Sectors per track
  2411. drv->cylinders = total_size + 1;
  2412. drv->raid_level = RAID_UNKNOWN;
  2413. } else {
  2414. drv->heads = inq_buff->data_byte[6];
  2415. drv->sectors = inq_buff->data_byte[7];
  2416. drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
  2417. drv->cylinders += inq_buff->data_byte[5];
  2418. drv->raid_level = inq_buff->data_byte[8];
  2419. }
  2420. drv->block_size = block_size;
  2421. drv->nr_blocks = total_size + 1;
  2422. t = drv->heads * drv->sectors;
  2423. if (t > 1) {
  2424. sector_t real_size = total_size + 1;
  2425. unsigned long rem = sector_div(real_size, t);
  2426. if (rem)
  2427. real_size++;
  2428. drv->cylinders = real_size;
  2429. }
  2430. } else { /* Get geometry failed */
  2431. printk(KERN_WARNING "cciss: reading geometry failed\n");
  2432. }
  2433. }
  2434. static void
  2435. cciss_read_capacity(int ctlr, int logvol, sector_t *total_size,
  2436. unsigned int *block_size)
  2437. {
  2438. ReadCapdata_struct *buf;
  2439. int return_code;
  2440. unsigned char scsi3addr[8];
  2441. buf = kzalloc(sizeof(ReadCapdata_struct), GFP_KERNEL);
  2442. if (!buf) {
  2443. printk(KERN_WARNING "cciss: out of memory\n");
  2444. return;
  2445. }
  2446. log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
  2447. return_code = sendcmd_withirq(CCISS_READ_CAPACITY, ctlr, buf,
  2448. sizeof(ReadCapdata_struct), 0, scsi3addr, TYPE_CMD);
  2449. if (return_code == IO_OK) {
  2450. *total_size = be32_to_cpu(*(__be32 *) buf->total_size);
  2451. *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
  2452. } else { /* read capacity command failed */
  2453. printk(KERN_WARNING "cciss: read capacity failed\n");
  2454. *total_size = 0;
  2455. *block_size = BLOCK_SIZE;
  2456. }
  2457. kfree(buf);
  2458. }
  2459. static void cciss_read_capacity_16(int ctlr, int logvol,
  2460. sector_t *total_size, unsigned int *block_size)
  2461. {
  2462. ReadCapdata_struct_16 *buf;
  2463. int return_code;
  2464. unsigned char scsi3addr[8];
  2465. buf = kzalloc(sizeof(ReadCapdata_struct_16), GFP_KERNEL);
  2466. if (!buf) {
  2467. printk(KERN_WARNING "cciss: out of memory\n");
  2468. return;
  2469. }
  2470. log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
  2471. return_code = sendcmd_withirq(CCISS_READ_CAPACITY_16,
  2472. ctlr, buf, sizeof(ReadCapdata_struct_16),
  2473. 0, scsi3addr, TYPE_CMD);
  2474. if (return_code == IO_OK) {
  2475. *total_size = be64_to_cpu(*(__be64 *) buf->total_size);
  2476. *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
  2477. } else { /* read capacity command failed */
  2478. printk(KERN_WARNING "cciss: read capacity failed\n");
  2479. *total_size = 0;
  2480. *block_size = BLOCK_SIZE;
  2481. }
  2482. printk(KERN_INFO " blocks= %llu block_size= %d\n",
  2483. (unsigned long long)*total_size+1, *block_size);
  2484. kfree(buf);
  2485. }
  2486. static int cciss_revalidate(struct gendisk *disk)
  2487. {
  2488. ctlr_info_t *h = get_host(disk);
  2489. drive_info_struct *drv = get_drv(disk);
  2490. int logvol;
  2491. int FOUND = 0;
  2492. unsigned int block_size;
  2493. sector_t total_size;
  2494. InquiryData_struct *inq_buff = NULL;
  2495. for (logvol = 0; logvol < CISS_MAX_LUN; logvol++) {
  2496. if (memcmp(h->drv[logvol]->LunID, drv->LunID,
  2497. sizeof(drv->LunID)) == 0) {
  2498. FOUND = 1;
  2499. break;
  2500. }
  2501. }
  2502. if (!FOUND)
  2503. return 1;
  2504. inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
  2505. if (inq_buff == NULL) {
  2506. printk(KERN_WARNING "cciss: out of memory\n");
  2507. return 1;
  2508. }
  2509. if (h->cciss_read == CCISS_READ_10) {
  2510. cciss_read_capacity(h->ctlr, logvol,
  2511. &total_size, &block_size);
  2512. } else {
  2513. cciss_read_capacity_16(h->ctlr, logvol,
  2514. &total_size, &block_size);
  2515. }
  2516. cciss_geometry_inquiry(h->ctlr, logvol, total_size, block_size,
  2517. inq_buff, drv);
  2518. blk_queue_logical_block_size(drv->queue, drv->block_size);
  2519. set_capacity(disk, drv->nr_blocks);
  2520. kfree(inq_buff);
  2521. return 0;
  2522. }
  2523. /*
  2524. * Map (physical) PCI mem into (virtual) kernel space
  2525. */
  2526. static void __iomem *remap_pci_mem(ulong base, ulong size)
  2527. {
  2528. ulong page_base = ((ulong) base) & PAGE_MASK;
  2529. ulong page_offs = ((ulong) base) - page_base;
  2530. void __iomem *page_remapped = ioremap(page_base, page_offs + size);
  2531. return page_remapped ? (page_remapped + page_offs) : NULL;
  2532. }
  2533. /*
  2534. * Takes jobs of the Q and sends them to the hardware, then puts it on
  2535. * the Q to wait for completion.
  2536. */
  2537. static void start_io(ctlr_info_t *h)
  2538. {
  2539. CommandList_struct *c;
  2540. while (!hlist_empty(&h->reqQ)) {
  2541. c = hlist_entry(h->reqQ.first, CommandList_struct, list);
  2542. /* can't do anything if fifo is full */
  2543. if ((h->access.fifo_full(h))) {
  2544. printk(KERN_WARNING "cciss: fifo full\n");
  2545. break;
  2546. }
  2547. /* Get the first entry from the Request Q */
  2548. removeQ(c);
  2549. h->Qdepth--;
  2550. /* Tell the controller execute command */
  2551. h->access.submit_command(h, c);
  2552. /* Put job onto the completed Q */
  2553. addQ(&h->cmpQ, c);
  2554. }
  2555. }
  2556. /* Assumes that CCISS_LOCK(h->ctlr) is held. */
  2557. /* Zeros out the error record and then resends the command back */
  2558. /* to the controller */
  2559. static inline void resend_cciss_cmd(ctlr_info_t *h, CommandList_struct *c)
  2560. {
  2561. /* erase the old error information */
  2562. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  2563. /* add it to software queue and then send it to the controller */
  2564. addQ(&h->reqQ, c);
  2565. h->Qdepth++;
  2566. if (h->Qdepth > h->maxQsinceinit)
  2567. h->maxQsinceinit = h->Qdepth;
  2568. start_io(h);
  2569. }
  2570. static inline unsigned int make_status_bytes(unsigned int scsi_status_byte,
  2571. unsigned int msg_byte, unsigned int host_byte,
  2572. unsigned int driver_byte)
  2573. {
  2574. /* inverse of macros in scsi.h */
  2575. return (scsi_status_byte & 0xff) |
  2576. ((msg_byte & 0xff) << 8) |
  2577. ((host_byte & 0xff) << 16) |
  2578. ((driver_byte & 0xff) << 24);
  2579. }
  2580. static inline int evaluate_target_status(ctlr_info_t *h,
  2581. CommandList_struct *cmd, int *retry_cmd)
  2582. {
  2583. unsigned char sense_key;
  2584. unsigned char status_byte, msg_byte, host_byte, driver_byte;
  2585. int error_value;
  2586. *retry_cmd = 0;
  2587. /* If we get in here, it means we got "target status", that is, scsi status */
  2588. status_byte = cmd->err_info->ScsiStatus;
  2589. driver_byte = DRIVER_OK;
  2590. msg_byte = cmd->err_info->CommandStatus; /* correct? seems too device specific */
  2591. if (blk_pc_request(cmd->rq))
  2592. host_byte = DID_PASSTHROUGH;
  2593. else
  2594. host_byte = DID_OK;
  2595. error_value = make_status_bytes(status_byte, msg_byte,
  2596. host_byte, driver_byte);
  2597. if (cmd->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) {
  2598. if (!blk_pc_request(cmd->rq))
  2599. printk(KERN_WARNING "cciss: cmd %p "
  2600. "has SCSI Status 0x%x\n",
  2601. cmd, cmd->err_info->ScsiStatus);
  2602. return error_value;
  2603. }
  2604. /* check the sense key */
  2605. sense_key = 0xf & cmd->err_info->SenseInfo[2];
  2606. /* no status or recovered error */
  2607. if (((sense_key == 0x0) || (sense_key == 0x1)) && !blk_pc_request(cmd->rq))
  2608. error_value = 0;
  2609. if (check_for_unit_attention(h, cmd)) {
  2610. *retry_cmd = !blk_pc_request(cmd->rq);
  2611. return 0;
  2612. }
  2613. if (!blk_pc_request(cmd->rq)) { /* Not SG_IO or similar? */
  2614. if (error_value != 0)
  2615. printk(KERN_WARNING "cciss: cmd %p has CHECK CONDITION"
  2616. " sense key = 0x%x\n", cmd, sense_key);
  2617. return error_value;
  2618. }
  2619. /* SG_IO or similar, copy sense data back */
  2620. if (cmd->rq->sense) {
  2621. if (cmd->rq->sense_len > cmd->err_info->SenseLen)
  2622. cmd->rq->sense_len = cmd->err_info->SenseLen;
  2623. memcpy(cmd->rq->sense, cmd->err_info->SenseInfo,
  2624. cmd->rq->sense_len);
  2625. } else
  2626. cmd->rq->sense_len = 0;
  2627. return error_value;
  2628. }
  2629. /* checks the status of the job and calls complete buffers to mark all
  2630. * buffers for the completed job. Note that this function does not need
  2631. * to hold the hba/queue lock.
  2632. */
  2633. static inline void complete_command(ctlr_info_t *h, CommandList_struct *cmd,
  2634. int timeout)
  2635. {
  2636. int retry_cmd = 0;
  2637. struct request *rq = cmd->rq;
  2638. rq->errors = 0;
  2639. if (timeout)
  2640. rq->errors = make_status_bytes(0, 0, 0, DRIVER_TIMEOUT);
  2641. if (cmd->err_info->CommandStatus == 0) /* no error has occurred */
  2642. goto after_error_processing;
  2643. switch (cmd->err_info->CommandStatus) {
  2644. case CMD_TARGET_STATUS:
  2645. rq->errors = evaluate_target_status(h, cmd, &retry_cmd);
  2646. break;
  2647. case CMD_DATA_UNDERRUN:
  2648. if (blk_fs_request(cmd->rq)) {
  2649. printk(KERN_WARNING "cciss: cmd %p has"
  2650. " completed with data underrun "
  2651. "reported\n", cmd);
  2652. cmd->rq->resid_len = cmd->err_info->ResidualCnt;
  2653. }
  2654. break;
  2655. case CMD_DATA_OVERRUN:
  2656. if (blk_fs_request(cmd->rq))
  2657. printk(KERN_WARNING "cciss: cmd %p has"
  2658. " completed with data overrun "
  2659. "reported\n", cmd);
  2660. break;
  2661. case CMD_INVALID:
  2662. printk(KERN_WARNING "cciss: cmd %p is "
  2663. "reported invalid\n", cmd);
  2664. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2665. cmd->err_info->CommandStatus, DRIVER_OK,
  2666. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2667. break;
  2668. case CMD_PROTOCOL_ERR:
  2669. printk(KERN_WARNING "cciss: cmd %p has "
  2670. "protocol error \n", cmd);
  2671. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2672. cmd->err_info->CommandStatus, DRIVER_OK,
  2673. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2674. break;
  2675. case CMD_HARDWARE_ERR:
  2676. printk(KERN_WARNING "cciss: cmd %p had "
  2677. " hardware error\n", cmd);
  2678. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2679. cmd->err_info->CommandStatus, DRIVER_OK,
  2680. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2681. break;
  2682. case CMD_CONNECTION_LOST:
  2683. printk(KERN_WARNING "cciss: cmd %p had "
  2684. "connection lost\n", cmd);
  2685. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2686. cmd->err_info->CommandStatus, DRIVER_OK,
  2687. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2688. break;
  2689. case CMD_ABORTED:
  2690. printk(KERN_WARNING "cciss: cmd %p was "
  2691. "aborted\n", cmd);
  2692. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2693. cmd->err_info->CommandStatus, DRIVER_OK,
  2694. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT);
  2695. break;
  2696. case CMD_ABORT_FAILED:
  2697. printk(KERN_WARNING "cciss: cmd %p reports "
  2698. "abort failed\n", cmd);
  2699. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2700. cmd->err_info->CommandStatus, DRIVER_OK,
  2701. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2702. break;
  2703. case CMD_UNSOLICITED_ABORT:
  2704. printk(KERN_WARNING "cciss%d: unsolicited "
  2705. "abort %p\n", h->ctlr, cmd);
  2706. if (cmd->retry_count < MAX_CMD_RETRIES) {
  2707. retry_cmd = 1;
  2708. printk(KERN_WARNING
  2709. "cciss%d: retrying %p\n", h->ctlr, cmd);
  2710. cmd->retry_count++;
  2711. } else
  2712. printk(KERN_WARNING
  2713. "cciss%d: %p retried too "
  2714. "many times\n", h->ctlr, cmd);
  2715. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2716. cmd->err_info->CommandStatus, DRIVER_OK,
  2717. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT);
  2718. break;
  2719. case CMD_TIMEOUT:
  2720. printk(KERN_WARNING "cciss: cmd %p timedout\n", cmd);
  2721. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2722. cmd->err_info->CommandStatus, DRIVER_OK,
  2723. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2724. break;
  2725. default:
  2726. printk(KERN_WARNING "cciss: cmd %p returned "
  2727. "unknown status %x\n", cmd,
  2728. cmd->err_info->CommandStatus);
  2729. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2730. cmd->err_info->CommandStatus, DRIVER_OK,
  2731. blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
  2732. }
  2733. after_error_processing:
  2734. /* We need to return this command */
  2735. if (retry_cmd) {
  2736. resend_cciss_cmd(h, cmd);
  2737. return;
  2738. }
  2739. cmd->rq->completion_data = cmd;
  2740. blk_complete_request(cmd->rq);
  2741. }
  2742. /*
  2743. * Get a request and submit it to the controller.
  2744. */
  2745. static void do_cciss_request(struct request_queue *q)
  2746. {
  2747. ctlr_info_t *h = q->queuedata;
  2748. CommandList_struct *c;
  2749. sector_t start_blk;
  2750. int seg;
  2751. struct request *creq;
  2752. u64bit temp64;
  2753. struct scatterlist *tmp_sg;
  2754. SGDescriptor_struct *curr_sg;
  2755. drive_info_struct *drv;
  2756. int i, dir;
  2757. int nseg = 0;
  2758. int sg_index = 0;
  2759. int chained = 0;
  2760. /* We call start_io here in case there is a command waiting on the
  2761. * queue that has not been sent.
  2762. */
  2763. if (blk_queue_plugged(q))
  2764. goto startio;
  2765. queue:
  2766. creq = blk_peek_request(q);
  2767. if (!creq)
  2768. goto startio;
  2769. BUG_ON(creq->nr_phys_segments > h->maxsgentries);
  2770. if ((c = cmd_alloc(h, 1)) == NULL)
  2771. goto full;
  2772. blk_start_request(creq);
  2773. tmp_sg = h->scatter_list[c->cmdindex];
  2774. spin_unlock_irq(q->queue_lock);
  2775. c->cmd_type = CMD_RWREQ;
  2776. c->rq = creq;
  2777. /* fill in the request */
  2778. drv = creq->rq_disk->private_data;
  2779. c->Header.ReplyQueue = 0; // unused in simple mode
  2780. /* got command from pool, so use the command block index instead */
  2781. /* for direct lookups. */
  2782. /* The first 2 bits are reserved for controller error reporting. */
  2783. c->Header.Tag.lower = (c->cmdindex << 3);
  2784. c->Header.Tag.lower |= 0x04; /* flag for direct lookup. */
  2785. memcpy(&c->Header.LUN, drv->LunID, sizeof(drv->LunID));
  2786. c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
  2787. c->Request.Type.Type = TYPE_CMD; // It is a command.
  2788. c->Request.Type.Attribute = ATTR_SIMPLE;
  2789. c->Request.Type.Direction =
  2790. (rq_data_dir(creq) == READ) ? XFER_READ : XFER_WRITE;
  2791. c->Request.Timeout = 0; // Don't time out
  2792. c->Request.CDB[0] =
  2793. (rq_data_dir(creq) == READ) ? h->cciss_read : h->cciss_write;
  2794. start_blk = blk_rq_pos(creq);
  2795. #ifdef CCISS_DEBUG
  2796. printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n",
  2797. (int)blk_rq_pos(creq), (int)blk_rq_sectors(creq));
  2798. #endif /* CCISS_DEBUG */
  2799. sg_init_table(tmp_sg, h->maxsgentries);
  2800. seg = blk_rq_map_sg(q, creq, tmp_sg);
  2801. /* get the DMA records for the setup */
  2802. if (c->Request.Type.Direction == XFER_READ)
  2803. dir = PCI_DMA_FROMDEVICE;
  2804. else
  2805. dir = PCI_DMA_TODEVICE;
  2806. curr_sg = c->SG;
  2807. sg_index = 0;
  2808. chained = 0;
  2809. for (i = 0; i < seg; i++) {
  2810. if (((sg_index+1) == (h->max_cmd_sgentries)) &&
  2811. !chained && ((seg - i) > 1)) {
  2812. nseg = seg - i;
  2813. curr_sg[sg_index].Len = (nseg) *
  2814. sizeof(SGDescriptor_struct);
  2815. curr_sg[sg_index].Ext = CCISS_SG_CHAIN;
  2816. /* Point to next chain block. */
  2817. curr_sg = h->cmd_sg_list[c->cmdindex]->sgchain;
  2818. sg_index = 0;
  2819. chained = 1;
  2820. }
  2821. curr_sg[sg_index].Len = tmp_sg[i].length;
  2822. temp64.val = (__u64) pci_map_page(h->pdev, sg_page(&tmp_sg[i]),
  2823. tmp_sg[i].offset,
  2824. tmp_sg[i].length, dir);
  2825. curr_sg[sg_index].Addr.lower = temp64.val32.lower;
  2826. curr_sg[sg_index].Addr.upper = temp64.val32.upper;
  2827. curr_sg[sg_index].Ext = 0; /* we are not chaining */
  2828. ++sg_index;
  2829. }
  2830. if (chained) {
  2831. int len;
  2832. curr_sg = c->SG;
  2833. sg_index = h->max_cmd_sgentries - 1;
  2834. len = curr_sg[sg_index].Len;
  2835. /* Setup pointer to next chain block.
  2836. * Fill out last element in current chain
  2837. * block with address of next chain block.
  2838. */
  2839. temp64.val = pci_map_single(h->pdev,
  2840. h->cmd_sg_list[c->cmdindex]->sgchain,
  2841. len, dir);
  2842. h->cmd_sg_list[c->cmdindex]->sg_chain_dma = temp64.val;
  2843. curr_sg[sg_index].Addr.lower = temp64.val32.lower;
  2844. curr_sg[sg_index].Addr.upper = temp64.val32.upper;
  2845. pci_dma_sync_single_for_device(h->pdev,
  2846. h->cmd_sg_list[c->cmdindex]->sg_chain_dma,
  2847. len, dir);
  2848. }
  2849. /* track how many SG entries we are using */
  2850. if (seg > h->maxSG)
  2851. h->maxSG = seg;
  2852. #ifdef CCISS_DEBUG
  2853. printk(KERN_DEBUG "cciss: Submitting %ld sectors in %d segments "
  2854. "chained[%d]\n",
  2855. blk_rq_sectors(creq), seg, chained);
  2856. #endif /* CCISS_DEBUG */
  2857. c->Header.SGList = c->Header.SGTotal = seg + chained;
  2858. if (seg > h->max_cmd_sgentries)
  2859. c->Header.SGList = h->max_cmd_sgentries;
  2860. if (likely(blk_fs_request(creq))) {
  2861. if(h->cciss_read == CCISS_READ_10) {
  2862. c->Request.CDB[1] = 0;
  2863. c->Request.CDB[2] = (start_blk >> 24) & 0xff; //MSB
  2864. c->Request.CDB[3] = (start_blk >> 16) & 0xff;
  2865. c->Request.CDB[4] = (start_blk >> 8) & 0xff;
  2866. c->Request.CDB[5] = start_blk & 0xff;
  2867. c->Request.CDB[6] = 0; // (sect >> 24) & 0xff; MSB
  2868. c->Request.CDB[7] = (blk_rq_sectors(creq) >> 8) & 0xff;
  2869. c->Request.CDB[8] = blk_rq_sectors(creq) & 0xff;
  2870. c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
  2871. } else {
  2872. u32 upper32 = upper_32_bits(start_blk);
  2873. c->Request.CDBLen = 16;
  2874. c->Request.CDB[1]= 0;
  2875. c->Request.CDB[2]= (upper32 >> 24) & 0xff; //MSB
  2876. c->Request.CDB[3]= (upper32 >> 16) & 0xff;
  2877. c->Request.CDB[4]= (upper32 >> 8) & 0xff;
  2878. c->Request.CDB[5]= upper32 & 0xff;
  2879. c->Request.CDB[6]= (start_blk >> 24) & 0xff;
  2880. c->Request.CDB[7]= (start_blk >> 16) & 0xff;
  2881. c->Request.CDB[8]= (start_blk >> 8) & 0xff;
  2882. c->Request.CDB[9]= start_blk & 0xff;
  2883. c->Request.CDB[10]= (blk_rq_sectors(creq) >> 24) & 0xff;
  2884. c->Request.CDB[11]= (blk_rq_sectors(creq) >> 16) & 0xff;
  2885. c->Request.CDB[12]= (blk_rq_sectors(creq) >> 8) & 0xff;
  2886. c->Request.CDB[13]= blk_rq_sectors(creq) & 0xff;
  2887. c->Request.CDB[14] = c->Request.CDB[15] = 0;
  2888. }
  2889. } else if (blk_pc_request(creq)) {
  2890. c->Request.CDBLen = creq->cmd_len;
  2891. memcpy(c->Request.CDB, creq->cmd, BLK_MAX_CDB);
  2892. } else {
  2893. printk(KERN_WARNING "cciss%d: bad request type %d\n", h->ctlr, creq->cmd_type);
  2894. BUG();
  2895. }
  2896. spin_lock_irq(q->queue_lock);
  2897. addQ(&h->reqQ, c);
  2898. h->Qdepth++;
  2899. if (h->Qdepth > h->maxQsinceinit)
  2900. h->maxQsinceinit = h->Qdepth;
  2901. goto queue;
  2902. full:
  2903. blk_stop_queue(q);
  2904. startio:
  2905. /* We will already have the driver lock here so not need
  2906. * to lock it.
  2907. */
  2908. start_io(h);
  2909. }
  2910. static inline unsigned long get_next_completion(ctlr_info_t *h)
  2911. {
  2912. return h->access.command_completed(h);
  2913. }
  2914. static inline int interrupt_pending(ctlr_info_t *h)
  2915. {
  2916. return h->access.intr_pending(h);
  2917. }
  2918. static inline long interrupt_not_for_us(ctlr_info_t *h)
  2919. {
  2920. return (((h->access.intr_pending(h) == 0) ||
  2921. (h->interrupts_enabled == 0)));
  2922. }
  2923. static irqreturn_t do_cciss_intr(int irq, void *dev_id)
  2924. {
  2925. ctlr_info_t *h = dev_id;
  2926. CommandList_struct *c;
  2927. unsigned long flags;
  2928. __u32 a, a1, a2;
  2929. if (interrupt_not_for_us(h))
  2930. return IRQ_NONE;
  2931. /*
  2932. * If there are completed commands in the completion queue,
  2933. * we had better do something about it.
  2934. */
  2935. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  2936. while (interrupt_pending(h)) {
  2937. while ((a = get_next_completion(h)) != FIFO_EMPTY) {
  2938. a1 = a;
  2939. if ((a & 0x04)) {
  2940. a2 = (a >> 3);
  2941. if (a2 >= h->nr_cmds) {
  2942. printk(KERN_WARNING
  2943. "cciss: controller cciss%d failed, stopping.\n",
  2944. h->ctlr);
  2945. fail_all_cmds(h->ctlr);
  2946. return IRQ_HANDLED;
  2947. }
  2948. c = h->cmd_pool + a2;
  2949. a = c->busaddr;
  2950. } else {
  2951. struct hlist_node *tmp;
  2952. a &= ~3;
  2953. c = NULL;
  2954. hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
  2955. if (c->busaddr == a)
  2956. break;
  2957. }
  2958. }
  2959. /*
  2960. * If we've found the command, take it off the
  2961. * completion Q and free it
  2962. */
  2963. if (c && c->busaddr == a) {
  2964. removeQ(c);
  2965. if (c->cmd_type == CMD_RWREQ) {
  2966. complete_command(h, c, 0);
  2967. } else if (c->cmd_type == CMD_IOCTL_PEND) {
  2968. complete(c->waiting);
  2969. }
  2970. # ifdef CONFIG_CISS_SCSI_TAPE
  2971. else if (c->cmd_type == CMD_SCSI)
  2972. complete_scsi_command(c, 0, a1);
  2973. # endif
  2974. continue;
  2975. }
  2976. }
  2977. }
  2978. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  2979. return IRQ_HANDLED;
  2980. }
  2981. /**
  2982. * add_to_scan_list() - add controller to rescan queue
  2983. * @h: Pointer to the controller.
  2984. *
  2985. * Adds the controller to the rescan queue if not already on the queue.
  2986. *
  2987. * returns 1 if added to the queue, 0 if skipped (could be on the
  2988. * queue already, or the controller could be initializing or shutting
  2989. * down).
  2990. **/
  2991. static int add_to_scan_list(struct ctlr_info *h)
  2992. {
  2993. struct ctlr_info *test_h;
  2994. int found = 0;
  2995. int ret = 0;
  2996. if (h->busy_initializing)
  2997. return 0;
  2998. if (!mutex_trylock(&h->busy_shutting_down))
  2999. return 0;
  3000. mutex_lock(&scan_mutex);
  3001. list_for_each_entry(test_h, &scan_q, scan_list) {
  3002. if (test_h == h) {
  3003. found = 1;
  3004. break;
  3005. }
  3006. }
  3007. if (!found && !h->busy_scanning) {
  3008. INIT_COMPLETION(h->scan_wait);
  3009. list_add_tail(&h->scan_list, &scan_q);
  3010. ret = 1;
  3011. }
  3012. mutex_unlock(&scan_mutex);
  3013. mutex_unlock(&h->busy_shutting_down);
  3014. return ret;
  3015. }
  3016. /**
  3017. * remove_from_scan_list() - remove controller from rescan queue
  3018. * @h: Pointer to the controller.
  3019. *
  3020. * Removes the controller from the rescan queue if present. Blocks if
  3021. * the controller is currently conducting a rescan. The controller
  3022. * can be in one of three states:
  3023. * 1. Doesn't need a scan
  3024. * 2. On the scan list, but not scanning yet (we remove it)
  3025. * 3. Busy scanning (and not on the list). In this case we want to wait for
  3026. * the scan to complete to make sure the scanning thread for this
  3027. * controller is completely idle.
  3028. **/
  3029. static void remove_from_scan_list(struct ctlr_info *h)
  3030. {
  3031. struct ctlr_info *test_h, *tmp_h;
  3032. mutex_lock(&scan_mutex);
  3033. list_for_each_entry_safe(test_h, tmp_h, &scan_q, scan_list) {
  3034. if (test_h == h) { /* state 2. */
  3035. list_del(&h->scan_list);
  3036. complete_all(&h->scan_wait);
  3037. mutex_unlock(&scan_mutex);
  3038. return;
  3039. }
  3040. }
  3041. if (h->busy_scanning) { /* state 3. */
  3042. mutex_unlock(&scan_mutex);
  3043. wait_for_completion(&h->scan_wait);
  3044. } else { /* state 1, nothing to do. */
  3045. mutex_unlock(&scan_mutex);
  3046. }
  3047. }
  3048. /**
  3049. * scan_thread() - kernel thread used to rescan controllers
  3050. * @data: Ignored.
  3051. *
  3052. * A kernel thread used scan for drive topology changes on
  3053. * controllers. The thread processes only one controller at a time
  3054. * using a queue. Controllers are added to the queue using
  3055. * add_to_scan_list() and removed from the queue either after done
  3056. * processing or using remove_from_scan_list().
  3057. *
  3058. * returns 0.
  3059. **/
  3060. static int scan_thread(void *data)
  3061. {
  3062. struct ctlr_info *h;
  3063. while (1) {
  3064. set_current_state(TASK_INTERRUPTIBLE);
  3065. schedule();
  3066. if (kthread_should_stop())
  3067. break;
  3068. while (1) {
  3069. mutex_lock(&scan_mutex);
  3070. if (list_empty(&scan_q)) {
  3071. mutex_unlock(&scan_mutex);
  3072. break;
  3073. }
  3074. h = list_entry(scan_q.next,
  3075. struct ctlr_info,
  3076. scan_list);
  3077. list_del(&h->scan_list);
  3078. h->busy_scanning = 1;
  3079. mutex_unlock(&scan_mutex);
  3080. rebuild_lun_table(h, 0, 0);
  3081. complete_all(&h->scan_wait);
  3082. mutex_lock(&scan_mutex);
  3083. h->busy_scanning = 0;
  3084. mutex_unlock(&scan_mutex);
  3085. }
  3086. }
  3087. return 0;
  3088. }
  3089. static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c)
  3090. {
  3091. if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
  3092. return 0;
  3093. switch (c->err_info->SenseInfo[12]) {
  3094. case STATE_CHANGED:
  3095. printk(KERN_WARNING "cciss%d: a state change "
  3096. "detected, command retried\n", h->ctlr);
  3097. return 1;
  3098. break;
  3099. case LUN_FAILED:
  3100. printk(KERN_WARNING "cciss%d: LUN failure "
  3101. "detected, action required\n", h->ctlr);
  3102. return 1;
  3103. break;
  3104. case REPORT_LUNS_CHANGED:
  3105. printk(KERN_WARNING "cciss%d: report LUN data "
  3106. "changed\n", h->ctlr);
  3107. /*
  3108. * Here, we could call add_to_scan_list and wake up the scan thread,
  3109. * except that it's quite likely that we will get more than one
  3110. * REPORT_LUNS_CHANGED condition in quick succession, which means
  3111. * that those which occur after the first one will likely happen
  3112. * *during* the scan_thread's rescan. And the rescan code is not
  3113. * robust enough to restart in the middle, undoing what it has already
  3114. * done, and it's not clear that it's even possible to do this, since
  3115. * part of what it does is notify the block layer, which starts
  3116. * doing it's own i/o to read partition tables and so on, and the
  3117. * driver doesn't have visibility to know what might need undoing.
  3118. * In any event, if possible, it is horribly complicated to get right
  3119. * so we just don't do it for now.
  3120. *
  3121. * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012.
  3122. */
  3123. return 1;
  3124. break;
  3125. case POWER_OR_RESET:
  3126. printk(KERN_WARNING "cciss%d: a power on "
  3127. "or device reset detected\n", h->ctlr);
  3128. return 1;
  3129. break;
  3130. case UNIT_ATTENTION_CLEARED:
  3131. printk(KERN_WARNING "cciss%d: unit attention "
  3132. "cleared by another initiator\n", h->ctlr);
  3133. return 1;
  3134. break;
  3135. default:
  3136. printk(KERN_WARNING "cciss%d: unknown "
  3137. "unit attention detected\n", h->ctlr);
  3138. return 1;
  3139. }
  3140. }
  3141. /*
  3142. * We cannot read the structure directly, for portability we must use
  3143. * the io functions.
  3144. * This is for debug only.
  3145. */
  3146. #ifdef CCISS_DEBUG
  3147. static void print_cfg_table(CfgTable_struct *tb)
  3148. {
  3149. int i;
  3150. char temp_name[17];
  3151. printk("Controller Configuration information\n");
  3152. printk("------------------------------------\n");
  3153. for (i = 0; i < 4; i++)
  3154. temp_name[i] = readb(&(tb->Signature[i]));
  3155. temp_name[4] = '\0';
  3156. printk(" Signature = %s\n", temp_name);
  3157. printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
  3158. printk(" Transport methods supported = 0x%x\n",
  3159. readl(&(tb->TransportSupport)));
  3160. printk(" Transport methods active = 0x%x\n",
  3161. readl(&(tb->TransportActive)));
  3162. printk(" Requested transport Method = 0x%x\n",
  3163. readl(&(tb->HostWrite.TransportRequest)));
  3164. printk(" Coalesce Interrupt Delay = 0x%x\n",
  3165. readl(&(tb->HostWrite.CoalIntDelay)));
  3166. printk(" Coalesce Interrupt Count = 0x%x\n",
  3167. readl(&(tb->HostWrite.CoalIntCount)));
  3168. printk(" Max outstanding commands = 0x%d\n",
  3169. readl(&(tb->CmdsOutMax)));
  3170. printk(" Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
  3171. for (i = 0; i < 16; i++)
  3172. temp_name[i] = readb(&(tb->ServerName[i]));
  3173. temp_name[16] = '\0';
  3174. printk(" Server Name = %s\n", temp_name);
  3175. printk(" Heartbeat Counter = 0x%x\n\n\n", readl(&(tb->HeartBeat)));
  3176. }
  3177. #endif /* CCISS_DEBUG */
  3178. static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
  3179. {
  3180. int i, offset, mem_type, bar_type;
  3181. if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
  3182. return 0;
  3183. offset = 0;
  3184. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  3185. bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
  3186. if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
  3187. offset += 4;
  3188. else {
  3189. mem_type = pci_resource_flags(pdev, i) &
  3190. PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  3191. switch (mem_type) {
  3192. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  3193. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  3194. offset += 4; /* 32 bit */
  3195. break;
  3196. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  3197. offset += 8;
  3198. break;
  3199. default: /* reserved in PCI 2.2 */
  3200. printk(KERN_WARNING
  3201. "Base address is invalid\n");
  3202. return -1;
  3203. break;
  3204. }
  3205. }
  3206. if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
  3207. return i + 1;
  3208. }
  3209. return -1;
  3210. }
  3211. /* If MSI/MSI-X is supported by the kernel we will try to enable it on
  3212. * controllers that are capable. If not, we use IO-APIC mode.
  3213. */
  3214. static void __devinit cciss_interrupt_mode(ctlr_info_t *c,
  3215. struct pci_dev *pdev, __u32 board_id)
  3216. {
  3217. #ifdef CONFIG_PCI_MSI
  3218. int err;
  3219. struct msix_entry cciss_msix_entries[4] = { {0, 0}, {0, 1},
  3220. {0, 2}, {0, 3}
  3221. };
  3222. /* Some boards advertise MSI but don't really support it */
  3223. if ((board_id == 0x40700E11) ||
  3224. (board_id == 0x40800E11) ||
  3225. (board_id == 0x40820E11) || (board_id == 0x40830E11))
  3226. goto default_int_mode;
  3227. if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
  3228. err = pci_enable_msix(pdev, cciss_msix_entries, 4);
  3229. if (!err) {
  3230. c->intr[0] = cciss_msix_entries[0].vector;
  3231. c->intr[1] = cciss_msix_entries[1].vector;
  3232. c->intr[2] = cciss_msix_entries[2].vector;
  3233. c->intr[3] = cciss_msix_entries[3].vector;
  3234. c->msix_vector = 1;
  3235. return;
  3236. }
  3237. if (err > 0) {
  3238. printk(KERN_WARNING "cciss: only %d MSI-X vectors "
  3239. "available\n", err);
  3240. goto default_int_mode;
  3241. } else {
  3242. printk(KERN_WARNING "cciss: MSI-X init failed %d\n",
  3243. err);
  3244. goto default_int_mode;
  3245. }
  3246. }
  3247. if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
  3248. if (!pci_enable_msi(pdev)) {
  3249. c->msi_vector = 1;
  3250. } else {
  3251. printk(KERN_WARNING "cciss: MSI init failed\n");
  3252. }
  3253. }
  3254. default_int_mode:
  3255. #endif /* CONFIG_PCI_MSI */
  3256. /* if we get here we're going to use the default interrupt mode */
  3257. c->intr[SIMPLE_MODE_INT] = pdev->irq;
  3258. return;
  3259. }
  3260. static int __devinit cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
  3261. {
  3262. ushort subsystem_vendor_id, subsystem_device_id, command;
  3263. __u32 board_id, scratchpad = 0;
  3264. __u64 cfg_offset;
  3265. __u32 cfg_base_addr;
  3266. __u64 cfg_base_addr_index;
  3267. int i, prod_index, err;
  3268. subsystem_vendor_id = pdev->subsystem_vendor;
  3269. subsystem_device_id = pdev->subsystem_device;
  3270. board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
  3271. subsystem_vendor_id);
  3272. for (i = 0; i < ARRAY_SIZE(products); i++) {
  3273. /* Stand aside for hpsa driver on request */
  3274. if (cciss_allow_hpsa && products[i].board_id == HPSA_BOUNDARY)
  3275. return -ENODEV;
  3276. if (board_id == products[i].board_id)
  3277. break;
  3278. }
  3279. prod_index = i;
  3280. if (prod_index == ARRAY_SIZE(products)) {
  3281. dev_warn(&pdev->dev,
  3282. "unrecognized board ID: 0x%08lx, ignoring.\n",
  3283. (unsigned long) board_id);
  3284. return -ENODEV;
  3285. }
  3286. /* check to see if controller has been disabled */
  3287. /* BEFORE trying to enable it */
  3288. (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
  3289. if (!(command & 0x02)) {
  3290. printk(KERN_WARNING
  3291. "cciss: controller appears to be disabled\n");
  3292. return -ENODEV;
  3293. }
  3294. err = pci_enable_device(pdev);
  3295. if (err) {
  3296. printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
  3297. return err;
  3298. }
  3299. err = pci_request_regions(pdev, "cciss");
  3300. if (err) {
  3301. printk(KERN_ERR "cciss: Cannot obtain PCI resources, "
  3302. "aborting\n");
  3303. return err;
  3304. }
  3305. #ifdef CCISS_DEBUG
  3306. printk("command = %x\n", command);
  3307. printk("irq = %x\n", pdev->irq);
  3308. printk("board_id = %x\n", board_id);
  3309. #endif /* CCISS_DEBUG */
  3310. /* If the kernel supports MSI/MSI-X we will try to enable that functionality,
  3311. * else we use the IO-APIC interrupt assigned to us by system ROM.
  3312. */
  3313. cciss_interrupt_mode(c, pdev, board_id);
  3314. /* find the memory BAR */
  3315. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  3316. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM)
  3317. break;
  3318. }
  3319. if (i == DEVICE_COUNT_RESOURCE) {
  3320. printk(KERN_WARNING "cciss: No memory BAR found\n");
  3321. err = -ENODEV;
  3322. goto err_out_free_res;
  3323. }
  3324. c->paddr = pci_resource_start(pdev, i); /* addressing mode bits
  3325. * already removed
  3326. */
  3327. #ifdef CCISS_DEBUG
  3328. printk("address 0 = %lx\n", c->paddr);
  3329. #endif /* CCISS_DEBUG */
  3330. c->vaddr = remap_pci_mem(c->paddr, 0x250);
  3331. /* Wait for the board to become ready. (PCI hotplug needs this.)
  3332. * We poll for up to 120 secs, once per 100ms. */
  3333. for (i = 0; i < 1200; i++) {
  3334. scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
  3335. if (scratchpad == CCISS_FIRMWARE_READY)
  3336. break;
  3337. set_current_state(TASK_INTERRUPTIBLE);
  3338. schedule_timeout(msecs_to_jiffies(100)); /* wait 100ms */
  3339. }
  3340. if (scratchpad != CCISS_FIRMWARE_READY) {
  3341. printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
  3342. err = -ENODEV;
  3343. goto err_out_free_res;
  3344. }
  3345. /* get the address index number */
  3346. cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
  3347. cfg_base_addr &= (__u32) 0x0000ffff;
  3348. #ifdef CCISS_DEBUG
  3349. printk("cfg base address = %x\n", cfg_base_addr);
  3350. #endif /* CCISS_DEBUG */
  3351. cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
  3352. #ifdef CCISS_DEBUG
  3353. printk("cfg base address index = %llx\n",
  3354. (unsigned long long)cfg_base_addr_index);
  3355. #endif /* CCISS_DEBUG */
  3356. if (cfg_base_addr_index == -1) {
  3357. printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
  3358. err = -ENODEV;
  3359. goto err_out_free_res;
  3360. }
  3361. cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
  3362. #ifdef CCISS_DEBUG
  3363. printk("cfg offset = %llx\n", (unsigned long long)cfg_offset);
  3364. #endif /* CCISS_DEBUG */
  3365. c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
  3366. cfg_base_addr_index) +
  3367. cfg_offset, sizeof(CfgTable_struct));
  3368. c->board_id = board_id;
  3369. #ifdef CCISS_DEBUG
  3370. print_cfg_table(c->cfgtable);
  3371. #endif /* CCISS_DEBUG */
  3372. /* Some controllers support Zero Memory Raid (ZMR).
  3373. * When configured in ZMR mode the number of supported
  3374. * commands drops to 64. So instead of just setting an
  3375. * arbitrary value we make the driver a little smarter.
  3376. * We read the config table to tell us how many commands
  3377. * are supported on the controller then subtract 4 to
  3378. * leave a little room for ioctl calls.
  3379. */
  3380. c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
  3381. c->maxsgentries = readl(&(c->cfgtable->MaxSGElements));
  3382. /*
  3383. * Limit native command to 32 s/g elements to save dma'able memory.
  3384. * Howvever spec says if 0, use 31
  3385. */
  3386. c->max_cmd_sgentries = 31;
  3387. if (c->maxsgentries > 512) {
  3388. c->max_cmd_sgentries = 32;
  3389. c->chainsize = c->maxsgentries - c->max_cmd_sgentries + 1;
  3390. c->maxsgentries -= 1; /* account for chain pointer */
  3391. } else {
  3392. c->maxsgentries = 31; /* Default to traditional value */
  3393. c->chainsize = 0; /* traditional */
  3394. }
  3395. c->product_name = products[prod_index].product_name;
  3396. c->access = *(products[prod_index].access);
  3397. c->nr_cmds = c->max_commands - 4;
  3398. if ((readb(&c->cfgtable->Signature[0]) != 'C') ||
  3399. (readb(&c->cfgtable->Signature[1]) != 'I') ||
  3400. (readb(&c->cfgtable->Signature[2]) != 'S') ||
  3401. (readb(&c->cfgtable->Signature[3]) != 'S')) {
  3402. printk("Does not appear to be a valid CISS config table\n");
  3403. err = -ENODEV;
  3404. goto err_out_free_res;
  3405. }
  3406. #ifdef CONFIG_X86
  3407. {
  3408. /* Need to enable prefetch in the SCSI core for 6400 in x86 */
  3409. __u32 prefetch;
  3410. prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
  3411. prefetch |= 0x100;
  3412. writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
  3413. }
  3414. #endif
  3415. /* Disabling DMA prefetch and refetch for the P600.
  3416. * An ASIC bug may result in accesses to invalid memory addresses.
  3417. * We've disabled prefetch for some time now. Testing with XEN
  3418. * kernels revealed a bug in the refetch if dom0 resides on a P600.
  3419. */
  3420. if(board_id == 0x3225103C) {
  3421. __u32 dma_prefetch;
  3422. __u32 dma_refetch;
  3423. dma_prefetch = readl(c->vaddr + I2O_DMA1_CFG);
  3424. dma_prefetch |= 0x8000;
  3425. writel(dma_prefetch, c->vaddr + I2O_DMA1_CFG);
  3426. pci_read_config_dword(pdev, PCI_COMMAND_PARITY, &dma_refetch);
  3427. dma_refetch |= 0x1;
  3428. pci_write_config_dword(pdev, PCI_COMMAND_PARITY, dma_refetch);
  3429. }
  3430. #ifdef CCISS_DEBUG
  3431. printk("Trying to put board into Simple mode\n");
  3432. #endif /* CCISS_DEBUG */
  3433. c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
  3434. /* Update the field, and then ring the doorbell */
  3435. writel(CFGTBL_Trans_Simple, &(c->cfgtable->HostWrite.TransportRequest));
  3436. writel(CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
  3437. /* under certain very rare conditions, this can take awhile.
  3438. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  3439. * as we enter this code.) */
  3440. for (i = 0; i < MAX_CONFIG_WAIT; i++) {
  3441. if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
  3442. break;
  3443. /* delay and try again */
  3444. set_current_state(TASK_INTERRUPTIBLE);
  3445. schedule_timeout(msecs_to_jiffies(1));
  3446. }
  3447. #ifdef CCISS_DEBUG
  3448. printk(KERN_DEBUG "I counter got to %d %x\n", i,
  3449. readl(c->vaddr + SA5_DOORBELL));
  3450. #endif /* CCISS_DEBUG */
  3451. #ifdef CCISS_DEBUG
  3452. print_cfg_table(c->cfgtable);
  3453. #endif /* CCISS_DEBUG */
  3454. if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
  3455. printk(KERN_WARNING "cciss: unable to get board into"
  3456. " simple mode\n");
  3457. err = -ENODEV;
  3458. goto err_out_free_res;
  3459. }
  3460. return 0;
  3461. err_out_free_res:
  3462. /*
  3463. * Deliberately omit pci_disable_device(): it does something nasty to
  3464. * Smart Array controllers that pci_enable_device does not undo
  3465. */
  3466. pci_release_regions(pdev);
  3467. return err;
  3468. }
  3469. /* Function to find the first free pointer into our hba[] array
  3470. * Returns -1 if no free entries are left.
  3471. */
  3472. static int alloc_cciss_hba(void)
  3473. {
  3474. int i;
  3475. for (i = 0; i < MAX_CTLR; i++) {
  3476. if (!hba[i]) {
  3477. ctlr_info_t *p;
  3478. p = kzalloc(sizeof(ctlr_info_t), GFP_KERNEL);
  3479. if (!p)
  3480. goto Enomem;
  3481. hba[i] = p;
  3482. return i;
  3483. }
  3484. }
  3485. printk(KERN_WARNING "cciss: This driver supports a maximum"
  3486. " of %d controllers.\n", MAX_CTLR);
  3487. return -1;
  3488. Enomem:
  3489. printk(KERN_ERR "cciss: out of memory.\n");
  3490. return -1;
  3491. }
  3492. static void free_hba(int n)
  3493. {
  3494. ctlr_info_t *h = hba[n];
  3495. int i;
  3496. hba[n] = NULL;
  3497. for (i = 0; i < h->highest_lun + 1; i++)
  3498. if (h->gendisk[i] != NULL)
  3499. put_disk(h->gendisk[i]);
  3500. kfree(h);
  3501. }
  3502. /* Send a message CDB to the firmware. */
  3503. static __devinit int cciss_message(struct pci_dev *pdev, unsigned char opcode, unsigned char type)
  3504. {
  3505. typedef struct {
  3506. CommandListHeader_struct CommandHeader;
  3507. RequestBlock_struct Request;
  3508. ErrDescriptor_struct ErrorDescriptor;
  3509. } Command;
  3510. static const size_t cmd_sz = sizeof(Command) + sizeof(ErrorInfo_struct);
  3511. Command *cmd;
  3512. dma_addr_t paddr64;
  3513. uint32_t paddr32, tag;
  3514. void __iomem *vaddr;
  3515. int i, err;
  3516. vaddr = ioremap_nocache(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  3517. if (vaddr == NULL)
  3518. return -ENOMEM;
  3519. /* The Inbound Post Queue only accepts 32-bit physical addresses for the
  3520. CCISS commands, so they must be allocated from the lower 4GiB of
  3521. memory. */
  3522. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  3523. if (err) {
  3524. iounmap(vaddr);
  3525. return -ENOMEM;
  3526. }
  3527. cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
  3528. if (cmd == NULL) {
  3529. iounmap(vaddr);
  3530. return -ENOMEM;
  3531. }
  3532. /* This must fit, because of the 32-bit consistent DMA mask. Also,
  3533. although there's no guarantee, we assume that the address is at
  3534. least 4-byte aligned (most likely, it's page-aligned). */
  3535. paddr32 = paddr64;
  3536. cmd->CommandHeader.ReplyQueue = 0;
  3537. cmd->CommandHeader.SGList = 0;
  3538. cmd->CommandHeader.SGTotal = 0;
  3539. cmd->CommandHeader.Tag.lower = paddr32;
  3540. cmd->CommandHeader.Tag.upper = 0;
  3541. memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
  3542. cmd->Request.CDBLen = 16;
  3543. cmd->Request.Type.Type = TYPE_MSG;
  3544. cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
  3545. cmd->Request.Type.Direction = XFER_NONE;
  3546. cmd->Request.Timeout = 0; /* Don't time out */
  3547. cmd->Request.CDB[0] = opcode;
  3548. cmd->Request.CDB[1] = type;
  3549. memset(&cmd->Request.CDB[2], 0, 14); /* the rest of the CDB is reserved */
  3550. cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(Command);
  3551. cmd->ErrorDescriptor.Addr.upper = 0;
  3552. cmd->ErrorDescriptor.Len = sizeof(ErrorInfo_struct);
  3553. writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
  3554. for (i = 0; i < 10; i++) {
  3555. tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
  3556. if ((tag & ~3) == paddr32)
  3557. break;
  3558. schedule_timeout_uninterruptible(HZ);
  3559. }
  3560. iounmap(vaddr);
  3561. /* we leak the DMA buffer here ... no choice since the controller could
  3562. still complete the command. */
  3563. if (i == 10) {
  3564. printk(KERN_ERR "cciss: controller message %02x:%02x timed out\n",
  3565. opcode, type);
  3566. return -ETIMEDOUT;
  3567. }
  3568. pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
  3569. if (tag & 2) {
  3570. printk(KERN_ERR "cciss: controller message %02x:%02x failed\n",
  3571. opcode, type);
  3572. return -EIO;
  3573. }
  3574. printk(KERN_INFO "cciss: controller message %02x:%02x succeeded\n",
  3575. opcode, type);
  3576. return 0;
  3577. }
  3578. #define cciss_soft_reset_controller(p) cciss_message(p, 1, 0)
  3579. #define cciss_noop(p) cciss_message(p, 3, 0)
  3580. static __devinit int cciss_reset_msi(struct pci_dev *pdev)
  3581. {
  3582. /* the #defines are stolen from drivers/pci/msi.h. */
  3583. #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
  3584. #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
  3585. int pos;
  3586. u16 control = 0;
  3587. pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
  3588. if (pos) {
  3589. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  3590. if (control & PCI_MSI_FLAGS_ENABLE) {
  3591. printk(KERN_INFO "cciss: resetting MSI\n");
  3592. pci_write_config_word(pdev, msi_control_reg(pos), control & ~PCI_MSI_FLAGS_ENABLE);
  3593. }
  3594. }
  3595. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  3596. if (pos) {
  3597. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  3598. if (control & PCI_MSIX_FLAGS_ENABLE) {
  3599. printk(KERN_INFO "cciss: resetting MSI-X\n");
  3600. pci_write_config_word(pdev, msi_control_reg(pos), control & ~PCI_MSIX_FLAGS_ENABLE);
  3601. }
  3602. }
  3603. return 0;
  3604. }
  3605. /* This does a hard reset of the controller using PCI power management
  3606. * states. */
  3607. static __devinit int cciss_hard_reset_controller(struct pci_dev *pdev)
  3608. {
  3609. u16 pmcsr, saved_config_space[32];
  3610. int i, pos;
  3611. printk(KERN_INFO "cciss: using PCI PM to reset controller\n");
  3612. /* This is very nearly the same thing as
  3613. pci_save_state(pci_dev);
  3614. pci_set_power_state(pci_dev, PCI_D3hot);
  3615. pci_set_power_state(pci_dev, PCI_D0);
  3616. pci_restore_state(pci_dev);
  3617. but we can't use these nice canned kernel routines on
  3618. kexec, because they also check the MSI/MSI-X state in PCI
  3619. configuration space and do the wrong thing when it is
  3620. set/cleared. Also, the pci_save/restore_state functions
  3621. violate the ordering requirements for restoring the
  3622. configuration space from the CCISS document (see the
  3623. comment below). So we roll our own .... */
  3624. for (i = 0; i < 32; i++)
  3625. pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
  3626. pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
  3627. if (pos == 0) {
  3628. printk(KERN_ERR "cciss_reset_controller: PCI PM not supported\n");
  3629. return -ENODEV;
  3630. }
  3631. /* Quoting from the Open CISS Specification: "The Power
  3632. * Management Control/Status Register (CSR) controls the power
  3633. * state of the device. The normal operating state is D0,
  3634. * CSR=00h. The software off state is D3, CSR=03h. To reset
  3635. * the controller, place the interface device in D3 then to
  3636. * D0, this causes a secondary PCI reset which will reset the
  3637. * controller." */
  3638. /* enter the D3hot power management state */
  3639. pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
  3640. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  3641. pmcsr |= PCI_D3hot;
  3642. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  3643. schedule_timeout_uninterruptible(HZ >> 1);
  3644. /* enter the D0 power management state */
  3645. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  3646. pmcsr |= PCI_D0;
  3647. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  3648. schedule_timeout_uninterruptible(HZ >> 1);
  3649. /* Restore the PCI configuration space. The Open CISS
  3650. * Specification says, "Restore the PCI Configuration
  3651. * Registers, offsets 00h through 60h. It is important to
  3652. * restore the command register, 16-bits at offset 04h,
  3653. * last. Do not restore the configuration status register,
  3654. * 16-bits at offset 06h." Note that the offset is 2*i. */
  3655. for (i = 0; i < 32; i++) {
  3656. if (i == 2 || i == 3)
  3657. continue;
  3658. pci_write_config_word(pdev, 2*i, saved_config_space[i]);
  3659. }
  3660. wmb();
  3661. pci_write_config_word(pdev, 4, saved_config_space[2]);
  3662. return 0;
  3663. }
  3664. /*
  3665. * This is it. Find all the controllers and register them. I really hate
  3666. * stealing all these major device numbers.
  3667. * returns the number of block devices registered.
  3668. */
  3669. static int __devinit cciss_init_one(struct pci_dev *pdev,
  3670. const struct pci_device_id *ent)
  3671. {
  3672. int i;
  3673. int j = 0;
  3674. int k = 0;
  3675. int rc;
  3676. int dac, return_code;
  3677. InquiryData_struct *inq_buff;
  3678. if (reset_devices) {
  3679. /* Reset the controller with a PCI power-cycle */
  3680. if (cciss_hard_reset_controller(pdev) || cciss_reset_msi(pdev))
  3681. return -ENODEV;
  3682. /* Now try to get the controller to respond to a no-op. Some
  3683. devices (notably the HP Smart Array 5i Controller) need
  3684. up to 30 seconds to respond. */
  3685. for (i=0; i<30; i++) {
  3686. if (cciss_noop(pdev) == 0)
  3687. break;
  3688. schedule_timeout_uninterruptible(HZ);
  3689. }
  3690. if (i == 30) {
  3691. printk(KERN_ERR "cciss: controller seems dead\n");
  3692. return -EBUSY;
  3693. }
  3694. }
  3695. i = alloc_cciss_hba();
  3696. if (i < 0)
  3697. return -1;
  3698. hba[i]->busy_initializing = 1;
  3699. INIT_HLIST_HEAD(&hba[i]->cmpQ);
  3700. INIT_HLIST_HEAD(&hba[i]->reqQ);
  3701. mutex_init(&hba[i]->busy_shutting_down);
  3702. if (cciss_pci_init(hba[i], pdev) != 0)
  3703. goto clean_no_release_regions;
  3704. sprintf(hba[i]->devname, "cciss%d", i);
  3705. hba[i]->ctlr = i;
  3706. hba[i]->pdev = pdev;
  3707. init_completion(&hba[i]->scan_wait);
  3708. if (cciss_create_hba_sysfs_entry(hba[i]))
  3709. goto clean0;
  3710. /* configure PCI DMA stuff */
  3711. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  3712. dac = 1;
  3713. else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))
  3714. dac = 0;
  3715. else {
  3716. printk(KERN_ERR "cciss: no suitable DMA available\n");
  3717. goto clean1;
  3718. }
  3719. /*
  3720. * register with the major number, or get a dynamic major number
  3721. * by passing 0 as argument. This is done for greater than
  3722. * 8 controller support.
  3723. */
  3724. if (i < MAX_CTLR_ORIG)
  3725. hba[i]->major = COMPAQ_CISS_MAJOR + i;
  3726. rc = register_blkdev(hba[i]->major, hba[i]->devname);
  3727. if (rc == -EBUSY || rc == -EINVAL) {
  3728. printk(KERN_ERR
  3729. "cciss: Unable to get major number %d for %s "
  3730. "on hba %d\n", hba[i]->major, hba[i]->devname, i);
  3731. goto clean1;
  3732. } else {
  3733. if (i >= MAX_CTLR_ORIG)
  3734. hba[i]->major = rc;
  3735. }
  3736. /* make sure the board interrupts are off */
  3737. hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
  3738. if (request_irq(hba[i]->intr[SIMPLE_MODE_INT], do_cciss_intr,
  3739. IRQF_DISABLED | IRQF_SHARED, hba[i]->devname, hba[i])) {
  3740. printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
  3741. hba[i]->intr[SIMPLE_MODE_INT], hba[i]->devname);
  3742. goto clean2;
  3743. }
  3744. printk(KERN_INFO "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
  3745. hba[i]->devname, pdev->device, pci_name(pdev),
  3746. hba[i]->intr[SIMPLE_MODE_INT], dac ? "" : " not");
  3747. hba[i]->cmd_pool_bits =
  3748. kmalloc(DIV_ROUND_UP(hba[i]->nr_cmds, BITS_PER_LONG)
  3749. * sizeof(unsigned long), GFP_KERNEL);
  3750. hba[i]->cmd_pool = (CommandList_struct *)
  3751. pci_alloc_consistent(hba[i]->pdev,
  3752. hba[i]->nr_cmds * sizeof(CommandList_struct),
  3753. &(hba[i]->cmd_pool_dhandle));
  3754. hba[i]->errinfo_pool = (ErrorInfo_struct *)
  3755. pci_alloc_consistent(hba[i]->pdev,
  3756. hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
  3757. &(hba[i]->errinfo_pool_dhandle));
  3758. if ((hba[i]->cmd_pool_bits == NULL)
  3759. || (hba[i]->cmd_pool == NULL)
  3760. || (hba[i]->errinfo_pool == NULL)) {
  3761. printk(KERN_ERR "cciss: out of memory");
  3762. goto clean4;
  3763. }
  3764. /* Need space for temp scatter list */
  3765. hba[i]->scatter_list = kmalloc(hba[i]->max_commands *
  3766. sizeof(struct scatterlist *),
  3767. GFP_KERNEL);
  3768. for (k = 0; k < hba[i]->nr_cmds; k++) {
  3769. hba[i]->scatter_list[k] = kmalloc(sizeof(struct scatterlist) *
  3770. hba[i]->maxsgentries,
  3771. GFP_KERNEL);
  3772. if (hba[i]->scatter_list[k] == NULL) {
  3773. printk(KERN_ERR "cciss%d: could not allocate "
  3774. "s/g lists\n", i);
  3775. goto clean4;
  3776. }
  3777. }
  3778. hba[i]->cmd_sg_list = kmalloc(sizeof(struct Cmd_sg_list *) *
  3779. hba[i]->nr_cmds,
  3780. GFP_KERNEL);
  3781. if (!hba[i]->cmd_sg_list) {
  3782. printk(KERN_ERR "cciss%d: Cannot get memory for "
  3783. "s/g chaining.\n", i);
  3784. goto clean4;
  3785. }
  3786. /* Build up chain blocks for each command */
  3787. if (hba[i]->chainsize > 0) {
  3788. for (j = 0; j < hba[i]->nr_cmds; j++) {
  3789. hba[i]->cmd_sg_list[j] =
  3790. kmalloc(sizeof(struct Cmd_sg_list),
  3791. GFP_KERNEL);
  3792. if (!hba[i]->cmd_sg_list[j]) {
  3793. printk(KERN_ERR "cciss%d: Cannot get memory "
  3794. "for chain block.\n", i);
  3795. goto clean4;
  3796. }
  3797. /* Need a block of chainsized s/g elements. */
  3798. hba[i]->cmd_sg_list[j]->sgchain =
  3799. kmalloc((hba[i]->chainsize *
  3800. sizeof(SGDescriptor_struct)),
  3801. GFP_KERNEL);
  3802. if (!hba[i]->cmd_sg_list[j]->sgchain) {
  3803. printk(KERN_ERR "cciss%d: Cannot get memory "
  3804. "for s/g chains\n", i);
  3805. goto clean4;
  3806. }
  3807. }
  3808. }
  3809. spin_lock_init(&hba[i]->lock);
  3810. /* Initialize the pdev driver private data.
  3811. have it point to hba[i]. */
  3812. pci_set_drvdata(pdev, hba[i]);
  3813. /* command and error info recs zeroed out before
  3814. they are used */
  3815. memset(hba[i]->cmd_pool_bits, 0,
  3816. DIV_ROUND_UP(hba[i]->nr_cmds, BITS_PER_LONG)
  3817. * sizeof(unsigned long));
  3818. hba[i]->num_luns = 0;
  3819. hba[i]->highest_lun = -1;
  3820. for (j = 0; j < CISS_MAX_LUN; j++) {
  3821. hba[i]->drv[j] = NULL;
  3822. hba[i]->gendisk[j] = NULL;
  3823. }
  3824. cciss_scsi_setup(i);
  3825. /* Turn the interrupts on so we can service requests */
  3826. hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
  3827. /* Get the firmware version */
  3828. inq_buff = kzalloc(sizeof(InquiryData_struct), GFP_KERNEL);
  3829. if (inq_buff == NULL) {
  3830. printk(KERN_ERR "cciss: out of memory\n");
  3831. goto clean4;
  3832. }
  3833. return_code = sendcmd_withirq(CISS_INQUIRY, i, inq_buff,
  3834. sizeof(InquiryData_struct), 0, CTLR_LUNID, TYPE_CMD);
  3835. if (return_code == IO_OK) {
  3836. hba[i]->firm_ver[0] = inq_buff->data_byte[32];
  3837. hba[i]->firm_ver[1] = inq_buff->data_byte[33];
  3838. hba[i]->firm_ver[2] = inq_buff->data_byte[34];
  3839. hba[i]->firm_ver[3] = inq_buff->data_byte[35];
  3840. } else { /* send command failed */
  3841. printk(KERN_WARNING "cciss: unable to determine firmware"
  3842. " version of controller\n");
  3843. }
  3844. kfree(inq_buff);
  3845. cciss_procinit(i);
  3846. hba[i]->cciss_max_sectors = 8192;
  3847. rebuild_lun_table(hba[i], 1, 0);
  3848. hba[i]->busy_initializing = 0;
  3849. return 1;
  3850. clean4:
  3851. kfree(hba[i]->cmd_pool_bits);
  3852. /* Free up sg elements */
  3853. for (k = 0; k < hba[i]->nr_cmds; k++)
  3854. kfree(hba[i]->scatter_list[k]);
  3855. kfree(hba[i]->scatter_list);
  3856. for (j = 0; j < hba[i]->nr_cmds; j++) {
  3857. if (hba[i]->cmd_sg_list[j])
  3858. kfree(hba[i]->cmd_sg_list[j]->sgchain);
  3859. kfree(hba[i]->cmd_sg_list[j]);
  3860. }
  3861. if (hba[i]->cmd_pool)
  3862. pci_free_consistent(hba[i]->pdev,
  3863. hba[i]->nr_cmds * sizeof(CommandList_struct),
  3864. hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
  3865. if (hba[i]->errinfo_pool)
  3866. pci_free_consistent(hba[i]->pdev,
  3867. hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
  3868. hba[i]->errinfo_pool,
  3869. hba[i]->errinfo_pool_dhandle);
  3870. free_irq(hba[i]->intr[SIMPLE_MODE_INT], hba[i]);
  3871. clean2:
  3872. unregister_blkdev(hba[i]->major, hba[i]->devname);
  3873. clean1:
  3874. cciss_destroy_hba_sysfs_entry(hba[i]);
  3875. clean0:
  3876. pci_release_regions(pdev);
  3877. clean_no_release_regions:
  3878. hba[i]->busy_initializing = 0;
  3879. /*
  3880. * Deliberately omit pci_disable_device(): it does something nasty to
  3881. * Smart Array controllers that pci_enable_device does not undo
  3882. */
  3883. pci_set_drvdata(pdev, NULL);
  3884. free_hba(i);
  3885. return -1;
  3886. }
  3887. static void cciss_shutdown(struct pci_dev *pdev)
  3888. {
  3889. ctlr_info_t *h;
  3890. char *flush_buf;
  3891. int return_code;
  3892. h = pci_get_drvdata(pdev);
  3893. flush_buf = kzalloc(4, GFP_KERNEL);
  3894. if (!flush_buf) {
  3895. printk(KERN_WARNING
  3896. "cciss:%d cache not flushed, out of memory.\n",
  3897. h->ctlr);
  3898. return;
  3899. }
  3900. /* write all data in the battery backed cache to disk */
  3901. memset(flush_buf, 0, 4);
  3902. return_code = sendcmd_withirq(CCISS_CACHE_FLUSH, h->ctlr, flush_buf,
  3903. 4, 0, CTLR_LUNID, TYPE_CMD);
  3904. kfree(flush_buf);
  3905. if (return_code != IO_OK)
  3906. printk(KERN_WARNING "cciss%d: Error flushing cache\n",
  3907. h->ctlr);
  3908. h->access.set_intr_mask(h, CCISS_INTR_OFF);
  3909. free_irq(h->intr[2], h);
  3910. }
  3911. static void __devexit cciss_remove_one(struct pci_dev *pdev)
  3912. {
  3913. ctlr_info_t *tmp_ptr;
  3914. int i, j;
  3915. if (pci_get_drvdata(pdev) == NULL) {
  3916. printk(KERN_ERR "cciss: Unable to remove device \n");
  3917. return;
  3918. }
  3919. tmp_ptr = pci_get_drvdata(pdev);
  3920. i = tmp_ptr->ctlr;
  3921. if (hba[i] == NULL) {
  3922. printk(KERN_ERR "cciss: device appears to "
  3923. "already be removed \n");
  3924. return;
  3925. }
  3926. mutex_lock(&hba[i]->busy_shutting_down);
  3927. remove_from_scan_list(hba[i]);
  3928. remove_proc_entry(hba[i]->devname, proc_cciss);
  3929. unregister_blkdev(hba[i]->major, hba[i]->devname);
  3930. /* remove it from the disk list */
  3931. for (j = 0; j < CISS_MAX_LUN; j++) {
  3932. struct gendisk *disk = hba[i]->gendisk[j];
  3933. if (disk) {
  3934. struct request_queue *q = disk->queue;
  3935. if (disk->flags & GENHD_FL_UP) {
  3936. cciss_destroy_ld_sysfs_entry(hba[i], j, 1);
  3937. del_gendisk(disk);
  3938. }
  3939. if (q)
  3940. blk_cleanup_queue(q);
  3941. }
  3942. }
  3943. #ifdef CONFIG_CISS_SCSI_TAPE
  3944. cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
  3945. #endif
  3946. cciss_shutdown(pdev);
  3947. #ifdef CONFIG_PCI_MSI
  3948. if (hba[i]->msix_vector)
  3949. pci_disable_msix(hba[i]->pdev);
  3950. else if (hba[i]->msi_vector)
  3951. pci_disable_msi(hba[i]->pdev);
  3952. #endif /* CONFIG_PCI_MSI */
  3953. iounmap(hba[i]->vaddr);
  3954. pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(CommandList_struct),
  3955. hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
  3956. pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
  3957. hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
  3958. kfree(hba[i]->cmd_pool_bits);
  3959. /* Free up sg elements */
  3960. for (j = 0; j < hba[i]->nr_cmds; j++)
  3961. kfree(hba[i]->scatter_list[j]);
  3962. kfree(hba[i]->scatter_list);
  3963. for (j = 0; j < hba[i]->nr_cmds; j++) {
  3964. kfree(hba[i]->cmd_sg_list[j]->sgchain);
  3965. kfree(hba[i]->cmd_sg_list[j]);
  3966. }
  3967. /*
  3968. * Deliberately omit pci_disable_device(): it does something nasty to
  3969. * Smart Array controllers that pci_enable_device does not undo
  3970. */
  3971. pci_release_regions(pdev);
  3972. pci_set_drvdata(pdev, NULL);
  3973. cciss_destroy_hba_sysfs_entry(hba[i]);
  3974. mutex_unlock(&hba[i]->busy_shutting_down);
  3975. free_hba(i);
  3976. }
  3977. static struct pci_driver cciss_pci_driver = {
  3978. .name = "cciss",
  3979. .probe = cciss_init_one,
  3980. .remove = __devexit_p(cciss_remove_one),
  3981. .id_table = cciss_pci_device_id, /* id_table */
  3982. .shutdown = cciss_shutdown,
  3983. };
  3984. /*
  3985. * This is it. Register the PCI driver information for the cards we control
  3986. * the OS will call our registered routines when it finds one of our cards.
  3987. */
  3988. static int __init cciss_init(void)
  3989. {
  3990. int err;
  3991. /*
  3992. * The hardware requires that commands are aligned on a 64-bit
  3993. * boundary. Given that we use pci_alloc_consistent() to allocate an
  3994. * array of them, the size must be a multiple of 8 bytes.
  3995. */
  3996. BUILD_BUG_ON(sizeof(CommandList_struct) % 8);
  3997. printk(KERN_INFO DRIVER_NAME "\n");
  3998. err = bus_register(&cciss_bus_type);
  3999. if (err)
  4000. return err;
  4001. /* Start the scan thread */
  4002. cciss_scan_thread = kthread_run(scan_thread, NULL, "cciss_scan");
  4003. if (IS_ERR(cciss_scan_thread)) {
  4004. err = PTR_ERR(cciss_scan_thread);
  4005. goto err_bus_unregister;
  4006. }
  4007. /* Register for our PCI devices */
  4008. err = pci_register_driver(&cciss_pci_driver);
  4009. if (err)
  4010. goto err_thread_stop;
  4011. return err;
  4012. err_thread_stop:
  4013. kthread_stop(cciss_scan_thread);
  4014. err_bus_unregister:
  4015. bus_unregister(&cciss_bus_type);
  4016. return err;
  4017. }
  4018. static void __exit cciss_cleanup(void)
  4019. {
  4020. int i;
  4021. pci_unregister_driver(&cciss_pci_driver);
  4022. /* double check that all controller entrys have been removed */
  4023. for (i = 0; i < MAX_CTLR; i++) {
  4024. if (hba[i] != NULL) {
  4025. printk(KERN_WARNING "cciss: had to remove"
  4026. " controller %d\n", i);
  4027. cciss_remove_one(hba[i]->pdev);
  4028. }
  4029. }
  4030. kthread_stop(cciss_scan_thread);
  4031. remove_proc_entry("driver/cciss", NULL);
  4032. bus_unregister(&cciss_bus_type);
  4033. }
  4034. static void fail_all_cmds(unsigned long ctlr)
  4035. {
  4036. /* If we get here, the board is apparently dead. */
  4037. ctlr_info_t *h = hba[ctlr];
  4038. CommandList_struct *c;
  4039. unsigned long flags;
  4040. printk(KERN_WARNING "cciss%d: controller not responding.\n", h->ctlr);
  4041. h->alive = 0; /* the controller apparently died... */
  4042. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  4043. pci_disable_device(h->pdev); /* Make sure it is really dead. */
  4044. /* move everything off the request queue onto the completed queue */
  4045. while (!hlist_empty(&h->reqQ)) {
  4046. c = hlist_entry(h->reqQ.first, CommandList_struct, list);
  4047. removeQ(c);
  4048. h->Qdepth--;
  4049. addQ(&h->cmpQ, c);
  4050. }
  4051. /* Now, fail everything on the completed queue with a HW error */
  4052. while (!hlist_empty(&h->cmpQ)) {
  4053. c = hlist_entry(h->cmpQ.first, CommandList_struct, list);
  4054. removeQ(c);
  4055. if (c->cmd_type != CMD_MSG_STALE)
  4056. c->err_info->CommandStatus = CMD_HARDWARE_ERR;
  4057. if (c->cmd_type == CMD_RWREQ) {
  4058. complete_command(h, c, 0);
  4059. } else if (c->cmd_type == CMD_IOCTL_PEND)
  4060. complete(c->waiting);
  4061. #ifdef CONFIG_CISS_SCSI_TAPE
  4062. else if (c->cmd_type == CMD_SCSI)
  4063. complete_scsi_command(c, 0, 0);
  4064. #endif
  4065. }
  4066. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  4067. return;
  4068. }
  4069. module_init(cciss_init);
  4070. module_exit(cciss_cleanup);