cciss.c 105 KB

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