cciss.c 83 KB

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