cciss.c 128 KB

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