cciss.c 99 KB

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