cciss.c 90 KB

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