cciss.c 138 KB

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