cciss.c 97 KB

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