cciss.c 115 KB

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