cciss.c 149 KB

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