cciss.c 149 KB

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