cciss.c 149 KB

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