cciss.c 118 KB

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