cciss.c 101 KB

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