cciss.c 148 KB

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