cciss.c 88 KB

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