cciss.c 98 KB

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