cciss.c 83 KB

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