cciss.c 95 KB

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