cciss.c 148 KB

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