cciss.c 98 KB

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