megaraid.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122
  1. /*
  2. *
  3. * Linux MegaRAID device driver
  4. *
  5. * Copyright © 2002 LSI Logic Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
  13. * - fixes
  14. * - speed-ups (list handling fixes, issued_list, optimizations.)
  15. * - lots of cleanups.
  16. *
  17. * Copyright (c) 2003 Christoph Hellwig <hch@lst.de>
  18. * - new-style, hotplug-aware pci probing and scsi registration
  19. *
  20. * Version : v2.00.3 (Feb 19, 2003) - Atul Mukker <Atul.Mukker@lsil.com>
  21. *
  22. * Description: Linux device driver for LSI Logic MegaRAID controller
  23. *
  24. * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
  25. * 518, 520, 531, 532
  26. *
  27. * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
  28. * and others. Please send updates to the mailing list
  29. * linux-scsi@vger.kernel.org .
  30. *
  31. */
  32. #include <linux/mm.h>
  33. #include <linux/fs.h>
  34. #include <linux/blkdev.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/io.h>
  37. #include <linux/delay.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/reboot.h>
  40. #include <linux/module.h>
  41. #include <linux/list.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/pci.h>
  44. #include <linux/init.h>
  45. #include <scsi/scsicam.h>
  46. #include "scsi.h"
  47. #include <scsi/scsi_host.h>
  48. #include "megaraid.h"
  49. #define MEGARAID_MODULE_VERSION "2.00.3"
  50. MODULE_AUTHOR ("LSI Logic Corporation");
  51. MODULE_DESCRIPTION ("LSI Logic MegaRAID driver");
  52. MODULE_LICENSE ("GPL");
  53. MODULE_VERSION(MEGARAID_MODULE_VERSION);
  54. static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
  55. module_param(max_cmd_per_lun, uint, 0);
  56. MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
  57. static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
  58. module_param(max_sectors_per_io, ushort, 0);
  59. MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
  60. static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  61. module_param(max_mbox_busy_wait, ushort, 0);
  62. MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
  63. #define RDINDOOR(adapter) readl((adapter)->base + 0x20)
  64. #define RDOUTDOOR(adapter) readl((adapter)->base + 0x2C)
  65. #define WRINDOOR(adapter,value) writel(value, (adapter)->base + 0x20)
  66. #define WROUTDOOR(adapter,value) writel(value, (adapter)->base + 0x2C)
  67. /*
  68. * Global variables
  69. */
  70. static int hba_count;
  71. static adapter_t *hba_soft_state[MAX_CONTROLLERS];
  72. static struct proc_dir_entry *mega_proc_dir_entry;
  73. /* For controller re-ordering */
  74. static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
  75. /*
  76. * The File Operations structure for the serial/ioctl interface of the driver
  77. */
  78. static struct file_operations megadev_fops = {
  79. .owner = THIS_MODULE,
  80. .ioctl = megadev_ioctl,
  81. .open = megadev_open,
  82. };
  83. /*
  84. * Array to structures for storing the information about the controllers. This
  85. * information is sent to the user level applications, when they do an ioctl
  86. * for this information.
  87. */
  88. static struct mcontroller mcontroller[MAX_CONTROLLERS];
  89. /* The current driver version */
  90. static u32 driver_ver = 0x02000000;
  91. /* major number used by the device for character interface */
  92. static int major;
  93. #define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
  94. /*
  95. * Debug variable to print some diagnostic messages
  96. */
  97. static int trace_level;
  98. /**
  99. * mega_setup_mailbox()
  100. * @adapter - pointer to our soft state
  101. *
  102. * Allocates a 8 byte aligned memory for the handshake mailbox.
  103. */
  104. static int
  105. mega_setup_mailbox(adapter_t *adapter)
  106. {
  107. unsigned long align;
  108. adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
  109. sizeof(mbox64_t), &adapter->una_mbox64_dma);
  110. if( !adapter->una_mbox64 ) return -1;
  111. adapter->mbox = &adapter->una_mbox64->mbox;
  112. adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
  113. (~0UL ^ 0xFUL));
  114. adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
  115. align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
  116. adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
  117. /*
  118. * Register the mailbox if the controller is an io-mapped controller
  119. */
  120. if( adapter->flag & BOARD_IOMAP ) {
  121. outb_p(adapter->mbox_dma & 0xFF,
  122. adapter->host->io_port + MBOX_PORT0);
  123. outb_p((adapter->mbox_dma >> 8) & 0xFF,
  124. adapter->host->io_port + MBOX_PORT1);
  125. outb_p((adapter->mbox_dma >> 16) & 0xFF,
  126. adapter->host->io_port + MBOX_PORT2);
  127. outb_p((adapter->mbox_dma >> 24) & 0xFF,
  128. adapter->host->io_port + MBOX_PORT3);
  129. outb_p(ENABLE_MBOX_BYTE,
  130. adapter->host->io_port + ENABLE_MBOX_REGION);
  131. irq_ack(adapter);
  132. irq_enable(adapter);
  133. }
  134. return 0;
  135. }
  136. /*
  137. * mega_query_adapter()
  138. * @adapter - pointer to our soft state
  139. *
  140. * Issue the adapter inquiry commands to the controller and find out
  141. * information and parameter about the devices attached
  142. */
  143. static int
  144. mega_query_adapter(adapter_t *adapter)
  145. {
  146. dma_addr_t prod_info_dma_handle;
  147. mega_inquiry3 *inquiry3;
  148. u8 raw_mbox[sizeof(struct mbox_out)];
  149. mbox_t *mbox;
  150. int retval;
  151. /* Initialize adapter inquiry mailbox */
  152. mbox = (mbox_t *)raw_mbox;
  153. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  154. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  155. /*
  156. * Try to issue Inquiry3 command
  157. * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
  158. * update enquiry3 structure
  159. */
  160. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  161. inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
  162. raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
  163. raw_mbox[2] = NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
  164. raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
  165. /* Issue a blocking command to the card */
  166. if ((retval = issue_scb_block(adapter, raw_mbox))) {
  167. /* the adapter does not support 40ld */
  168. mraid_ext_inquiry *ext_inq;
  169. mraid_inquiry *inq;
  170. dma_addr_t dma_handle;
  171. ext_inq = pci_alloc_consistent(adapter->dev,
  172. sizeof(mraid_ext_inquiry), &dma_handle);
  173. if( ext_inq == NULL ) return -1;
  174. inq = &ext_inq->raid_inq;
  175. mbox->m_out.xferaddr = (u32)dma_handle;
  176. /*issue old 0x04 command to adapter */
  177. mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
  178. issue_scb_block(adapter, raw_mbox);
  179. /*
  180. * update Enquiry3 and ProductInfo structures with
  181. * mraid_inquiry structure
  182. */
  183. mega_8_to_40ld(inq, inquiry3,
  184. (mega_product_info *)&adapter->product_info);
  185. pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
  186. ext_inq, dma_handle);
  187. } else { /*adapter supports 40ld */
  188. adapter->flag |= BOARD_40LD;
  189. /*
  190. * get product_info, which is static information and will be
  191. * unchanged
  192. */
  193. prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
  194. &adapter->product_info,
  195. sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
  196. mbox->m_out.xferaddr = prod_info_dma_handle;
  197. raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
  198. raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
  199. if ((retval = issue_scb_block(adapter, raw_mbox)))
  200. printk(KERN_WARNING
  201. "megaraid: Product_info cmd failed with error: %d\n",
  202. retval);
  203. pci_unmap_single(adapter->dev, prod_info_dma_handle,
  204. sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
  205. }
  206. /*
  207. * kernel scans the channels from 0 to <= max_channel
  208. */
  209. adapter->host->max_channel =
  210. adapter->product_info.nchannels + NVIRT_CHAN -1;
  211. adapter->host->max_id = 16; /* max targets per channel */
  212. adapter->host->max_lun = 7; /* Upto 7 luns for non disk devices */
  213. adapter->host->cmd_per_lun = max_cmd_per_lun;
  214. adapter->numldrv = inquiry3->num_ldrv;
  215. adapter->max_cmds = adapter->product_info.max_commands;
  216. if(adapter->max_cmds > MAX_COMMANDS)
  217. adapter->max_cmds = MAX_COMMANDS;
  218. adapter->host->can_queue = adapter->max_cmds - 1;
  219. /*
  220. * Get the maximum number of scatter-gather elements supported by this
  221. * firmware
  222. */
  223. mega_get_max_sgl(adapter);
  224. adapter->host->sg_tablesize = adapter->sglen;
  225. /* use HP firmware and bios version encoding */
  226. if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
  227. sprintf (adapter->fw_version, "%c%d%d.%d%d",
  228. adapter->product_info.fw_version[2],
  229. adapter->product_info.fw_version[1] >> 8,
  230. adapter->product_info.fw_version[1] & 0x0f,
  231. adapter->product_info.fw_version[0] >> 8,
  232. adapter->product_info.fw_version[0] & 0x0f);
  233. sprintf (adapter->bios_version, "%c%d%d.%d%d",
  234. adapter->product_info.bios_version[2],
  235. adapter->product_info.bios_version[1] >> 8,
  236. adapter->product_info.bios_version[1] & 0x0f,
  237. adapter->product_info.bios_version[0] >> 8,
  238. adapter->product_info.bios_version[0] & 0x0f);
  239. } else {
  240. memcpy(adapter->fw_version,
  241. (char *)adapter->product_info.fw_version, 4);
  242. adapter->fw_version[4] = 0;
  243. memcpy(adapter->bios_version,
  244. (char *)adapter->product_info.bios_version, 4);
  245. adapter->bios_version[4] = 0;
  246. }
  247. printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
  248. adapter->fw_version, adapter->bios_version, adapter->numldrv);
  249. /*
  250. * Do we support extended (>10 bytes) cdbs
  251. */
  252. adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
  253. if (adapter->support_ext_cdb)
  254. printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
  255. return 0;
  256. }
  257. /**
  258. * mega_runpendq()
  259. * @adapter - pointer to our soft state
  260. *
  261. * Runs through the list of pending requests.
  262. */
  263. static inline void
  264. mega_runpendq(adapter_t *adapter)
  265. {
  266. if(!list_empty(&adapter->pending_list))
  267. __mega_runpendq(adapter);
  268. }
  269. /*
  270. * megaraid_queue()
  271. * @scmd - Issue this scsi command
  272. * @done - the callback hook into the scsi mid-layer
  273. *
  274. * The command queuing entry point for the mid-layer.
  275. */
  276. static int
  277. megaraid_queue(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
  278. {
  279. adapter_t *adapter;
  280. scb_t *scb;
  281. int busy=0;
  282. adapter = (adapter_t *)scmd->device->host->hostdata;
  283. scmd->scsi_done = done;
  284. /*
  285. * Allocate and build a SCB request
  286. * busy flag will be set if mega_build_cmd() command could not
  287. * allocate scb. We will return non-zero status in that case.
  288. * NOTE: scb can be null even though certain commands completed
  289. * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
  290. * return 0 in that case.
  291. */
  292. scb = mega_build_cmd(adapter, scmd, &busy);
  293. if(scb) {
  294. scb->state |= SCB_PENDQ;
  295. list_add_tail(&scb->list, &adapter->pending_list);
  296. /*
  297. * Check if the HBA is in quiescent state, e.g., during a
  298. * delete logical drive opertion. If it is, don't run
  299. * the pending_list.
  300. */
  301. if(atomic_read(&adapter->quiescent) == 0) {
  302. mega_runpendq(adapter);
  303. }
  304. return 0;
  305. }
  306. return busy;
  307. }
  308. /**
  309. * mega_allocate_scb()
  310. * @adapter - pointer to our soft state
  311. * @cmd - scsi command from the mid-layer
  312. *
  313. * Allocate a SCB structure. This is the central structure for controller
  314. * commands.
  315. */
  316. static inline scb_t *
  317. mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
  318. {
  319. struct list_head *head = &adapter->free_list;
  320. scb_t *scb;
  321. /* Unlink command from Free List */
  322. if( !list_empty(head) ) {
  323. scb = list_entry(head->next, scb_t, list);
  324. list_del_init(head->next);
  325. scb->state = SCB_ACTIVE;
  326. scb->cmd = cmd;
  327. scb->dma_type = MEGA_DMA_TYPE_NONE;
  328. return scb;
  329. }
  330. return NULL;
  331. }
  332. /**
  333. * mega_get_ldrv_num()
  334. * @adapter - pointer to our soft state
  335. * @cmd - scsi mid layer command
  336. * @channel - channel on the controller
  337. *
  338. * Calculate the logical drive number based on the information in scsi command
  339. * and the channel number.
  340. */
  341. static inline int
  342. mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
  343. {
  344. int tgt;
  345. int ldrv_num;
  346. tgt = cmd->device->id;
  347. if ( tgt > adapter->this_id )
  348. tgt--; /* we do not get inquires for initiator id */
  349. ldrv_num = (channel * 15) + tgt;
  350. /*
  351. * If we have a logical drive with boot enabled, project it first
  352. */
  353. if( adapter->boot_ldrv_enabled ) {
  354. if( ldrv_num == 0 ) {
  355. ldrv_num = adapter->boot_ldrv;
  356. }
  357. else {
  358. if( ldrv_num <= adapter->boot_ldrv ) {
  359. ldrv_num--;
  360. }
  361. }
  362. }
  363. /*
  364. * If "delete logical drive" feature is enabled on this controller.
  365. * Do only if at least one delete logical drive operation was done.
  366. *
  367. * Also, after logical drive deletion, instead of logical drive number,
  368. * the value returned should be 0x80+logical drive id.
  369. *
  370. * These is valid only for IO commands.
  371. */
  372. if (adapter->support_random_del && adapter->read_ldidmap )
  373. switch (cmd->cmnd[0]) {
  374. case READ_6: /* fall through */
  375. case WRITE_6: /* fall through */
  376. case READ_10: /* fall through */
  377. case WRITE_10:
  378. ldrv_num += 0x80;
  379. }
  380. return ldrv_num;
  381. }
  382. /**
  383. * mega_build_cmd()
  384. * @adapter - pointer to our soft state
  385. * @cmd - Prepare using this scsi command
  386. * @busy - busy flag if no resources
  387. *
  388. * Prepares a command and scatter gather list for the controller. This routine
  389. * also finds out if the commands is intended for a logical drive or a
  390. * physical device and prepares the controller command accordingly.
  391. *
  392. * We also re-order the logical drives and physical devices based on their
  393. * boot settings.
  394. */
  395. static scb_t *
  396. mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
  397. {
  398. mega_ext_passthru *epthru;
  399. mega_passthru *pthru;
  400. scb_t *scb;
  401. mbox_t *mbox;
  402. long seg;
  403. char islogical;
  404. int max_ldrv_num;
  405. int channel = 0;
  406. int target = 0;
  407. int ldrv_num = 0; /* logical drive number */
  408. /*
  409. * filter the internal and ioctl commands
  410. */
  411. if((cmd->cmnd[0] == MEGA_INTERNAL_CMD)) {
  412. return cmd->buffer;
  413. }
  414. /*
  415. * We know what channels our logical drives are on - mega_find_card()
  416. */
  417. islogical = adapter->logdrv_chan[cmd->device->channel];
  418. /*
  419. * The theory: If physical drive is chosen for boot, all the physical
  420. * devices are exported before the logical drives, otherwise physical
  421. * devices are pushed after logical drives, in which case - Kernel sees
  422. * the physical devices on virtual channel which is obviously converted
  423. * to actual channel on the HBA.
  424. */
  425. if( adapter->boot_pdrv_enabled ) {
  426. if( islogical ) {
  427. /* logical channel */
  428. channel = cmd->device->channel -
  429. adapter->product_info.nchannels;
  430. }
  431. else {
  432. /* this is physical channel */
  433. channel = cmd->device->channel;
  434. target = cmd->device->id;
  435. /*
  436. * boot from a physical disk, that disk needs to be
  437. * exposed first IF both the channels are SCSI, then
  438. * booting from the second channel is not allowed.
  439. */
  440. if( target == 0 ) {
  441. target = adapter->boot_pdrv_tgt;
  442. }
  443. else if( target == adapter->boot_pdrv_tgt ) {
  444. target = 0;
  445. }
  446. }
  447. }
  448. else {
  449. if( islogical ) {
  450. /* this is the logical channel */
  451. channel = cmd->device->channel;
  452. }
  453. else {
  454. /* physical channel */
  455. channel = cmd->device->channel - NVIRT_CHAN;
  456. target = cmd->device->id;
  457. }
  458. }
  459. if(islogical) {
  460. /* have just LUN 0 for each target on virtual channels */
  461. if (cmd->device->lun) {
  462. cmd->result = (DID_BAD_TARGET << 16);
  463. cmd->scsi_done(cmd);
  464. return NULL;
  465. }
  466. ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
  467. max_ldrv_num = (adapter->flag & BOARD_40LD) ?
  468. MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
  469. /*
  470. * max_ldrv_num increases by 0x80 if some logical drive was
  471. * deleted.
  472. */
  473. if(adapter->read_ldidmap)
  474. max_ldrv_num += 0x80;
  475. if(ldrv_num > max_ldrv_num ) {
  476. cmd->result = (DID_BAD_TARGET << 16);
  477. cmd->scsi_done(cmd);
  478. return NULL;
  479. }
  480. }
  481. else {
  482. if( cmd->device->lun > 7) {
  483. /*
  484. * Do not support lun >7 for physically accessed
  485. * devices
  486. */
  487. cmd->result = (DID_BAD_TARGET << 16);
  488. cmd->scsi_done(cmd);
  489. return NULL;
  490. }
  491. }
  492. /*
  493. *
  494. * Logical drive commands
  495. *
  496. */
  497. if(islogical) {
  498. switch (cmd->cmnd[0]) {
  499. case TEST_UNIT_READY:
  500. memset(cmd->request_buffer, 0, cmd->request_bufflen);
  501. #if MEGA_HAVE_CLUSTERING
  502. /*
  503. * Do we support clustering and is the support enabled
  504. * If no, return success always
  505. */
  506. if( !adapter->has_cluster ) {
  507. cmd->result = (DID_OK << 16);
  508. cmd->scsi_done(cmd);
  509. return NULL;
  510. }
  511. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  512. *busy = 1;
  513. return NULL;
  514. }
  515. scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
  516. scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
  517. scb->raw_mbox[3] = ldrv_num;
  518. scb->dma_direction = PCI_DMA_NONE;
  519. return scb;
  520. #else
  521. cmd->result = (DID_OK << 16);
  522. cmd->scsi_done(cmd);
  523. return NULL;
  524. #endif
  525. case MODE_SENSE:
  526. memset(cmd->request_buffer, 0, cmd->cmnd[4]);
  527. cmd->result = (DID_OK << 16);
  528. cmd->scsi_done(cmd);
  529. return NULL;
  530. case READ_CAPACITY:
  531. case INQUIRY:
  532. if(!(adapter->flag & (1L << cmd->device->channel))) {
  533. printk(KERN_NOTICE
  534. "scsi%d: scanning scsi channel %d ",
  535. adapter->host->host_no,
  536. cmd->device->channel);
  537. printk("for logical drives.\n");
  538. adapter->flag |= (1L << cmd->device->channel);
  539. }
  540. /* Allocate a SCB and initialize passthru */
  541. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  542. *busy = 1;
  543. return NULL;
  544. }
  545. pthru = scb->pthru;
  546. mbox = (mbox_t *)scb->raw_mbox;
  547. memset(mbox, 0, sizeof(scb->raw_mbox));
  548. memset(pthru, 0, sizeof(mega_passthru));
  549. pthru->timeout = 0;
  550. pthru->ars = 1;
  551. pthru->reqsenselen = 14;
  552. pthru->islogical = 1;
  553. pthru->logdrv = ldrv_num;
  554. pthru->cdblen = cmd->cmd_len;
  555. memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
  556. if( adapter->has_64bit_addr ) {
  557. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
  558. }
  559. else {
  560. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
  561. }
  562. scb->dma_direction = PCI_DMA_FROMDEVICE;
  563. pthru->numsgelements = mega_build_sglist(adapter, scb,
  564. &pthru->dataxferaddr, &pthru->dataxferlen);
  565. mbox->m_out.xferaddr = scb->pthru_dma_addr;
  566. return scb;
  567. case READ_6:
  568. case WRITE_6:
  569. case READ_10:
  570. case WRITE_10:
  571. case READ_12:
  572. case WRITE_12:
  573. /* Allocate a SCB and initialize mailbox */
  574. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  575. *busy = 1;
  576. return NULL;
  577. }
  578. mbox = (mbox_t *)scb->raw_mbox;
  579. memset(mbox, 0, sizeof(scb->raw_mbox));
  580. mbox->m_out.logdrv = ldrv_num;
  581. /*
  582. * A little hack: 2nd bit is zero for all scsi read
  583. * commands and is set for all scsi write commands
  584. */
  585. if( adapter->has_64bit_addr ) {
  586. mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
  587. MEGA_MBOXCMD_LWRITE64:
  588. MEGA_MBOXCMD_LREAD64 ;
  589. }
  590. else {
  591. mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
  592. MEGA_MBOXCMD_LWRITE:
  593. MEGA_MBOXCMD_LREAD ;
  594. }
  595. /*
  596. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  597. */
  598. if( cmd->cmd_len == 6 ) {
  599. mbox->m_out.numsectors = (u32) cmd->cmnd[4];
  600. mbox->m_out.lba =
  601. ((u32)cmd->cmnd[1] << 16) |
  602. ((u32)cmd->cmnd[2] << 8) |
  603. (u32)cmd->cmnd[3];
  604. mbox->m_out.lba &= 0x1FFFFF;
  605. #if MEGA_HAVE_STATS
  606. /*
  607. * Take modulo 0x80, since the logical drive
  608. * number increases by 0x80 when a logical
  609. * drive was deleted
  610. */
  611. if (*cmd->cmnd == READ_6) {
  612. adapter->nreads[ldrv_num%0x80]++;
  613. adapter->nreadblocks[ldrv_num%0x80] +=
  614. mbox->m_out.numsectors;
  615. } else {
  616. adapter->nwrites[ldrv_num%0x80]++;
  617. adapter->nwriteblocks[ldrv_num%0x80] +=
  618. mbox->m_out.numsectors;
  619. }
  620. #endif
  621. }
  622. /*
  623. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  624. */
  625. if( cmd->cmd_len == 10 ) {
  626. mbox->m_out.numsectors =
  627. (u32)cmd->cmnd[8] |
  628. ((u32)cmd->cmnd[7] << 8);
  629. mbox->m_out.lba =
  630. ((u32)cmd->cmnd[2] << 24) |
  631. ((u32)cmd->cmnd[3] << 16) |
  632. ((u32)cmd->cmnd[4] << 8) |
  633. (u32)cmd->cmnd[5];
  634. #if MEGA_HAVE_STATS
  635. if (*cmd->cmnd == READ_10) {
  636. adapter->nreads[ldrv_num%0x80]++;
  637. adapter->nreadblocks[ldrv_num%0x80] +=
  638. mbox->m_out.numsectors;
  639. } else {
  640. adapter->nwrites[ldrv_num%0x80]++;
  641. adapter->nwriteblocks[ldrv_num%0x80] +=
  642. mbox->m_out.numsectors;
  643. }
  644. #endif
  645. }
  646. /*
  647. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  648. */
  649. if( cmd->cmd_len == 12 ) {
  650. mbox->m_out.lba =
  651. ((u32)cmd->cmnd[2] << 24) |
  652. ((u32)cmd->cmnd[3] << 16) |
  653. ((u32)cmd->cmnd[4] << 8) |
  654. (u32)cmd->cmnd[5];
  655. mbox->m_out.numsectors =
  656. ((u32)cmd->cmnd[6] << 24) |
  657. ((u32)cmd->cmnd[7] << 16) |
  658. ((u32)cmd->cmnd[8] << 8) |
  659. (u32)cmd->cmnd[9];
  660. #if MEGA_HAVE_STATS
  661. if (*cmd->cmnd == READ_12) {
  662. adapter->nreads[ldrv_num%0x80]++;
  663. adapter->nreadblocks[ldrv_num%0x80] +=
  664. mbox->m_out.numsectors;
  665. } else {
  666. adapter->nwrites[ldrv_num%0x80]++;
  667. adapter->nwriteblocks[ldrv_num%0x80] +=
  668. mbox->m_out.numsectors;
  669. }
  670. #endif
  671. }
  672. /*
  673. * If it is a read command
  674. */
  675. if( (*cmd->cmnd & 0x0F) == 0x08 ) {
  676. scb->dma_direction = PCI_DMA_FROMDEVICE;
  677. }
  678. else {
  679. scb->dma_direction = PCI_DMA_TODEVICE;
  680. }
  681. /* Calculate Scatter-Gather info */
  682. mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
  683. (u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
  684. return scb;
  685. #if MEGA_HAVE_CLUSTERING
  686. case RESERVE: /* Fall through */
  687. case RELEASE:
  688. /*
  689. * Do we support clustering and is the support enabled
  690. */
  691. if( ! adapter->has_cluster ) {
  692. cmd->result = (DID_BAD_TARGET << 16);
  693. cmd->scsi_done(cmd);
  694. return NULL;
  695. }
  696. /* Allocate a SCB and initialize mailbox */
  697. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  698. *busy = 1;
  699. return NULL;
  700. }
  701. scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
  702. scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
  703. MEGA_RESERVE_LD : MEGA_RELEASE_LD;
  704. scb->raw_mbox[3] = ldrv_num;
  705. scb->dma_direction = PCI_DMA_NONE;
  706. return scb;
  707. #endif
  708. default:
  709. cmd->result = (DID_BAD_TARGET << 16);
  710. cmd->scsi_done(cmd);
  711. return NULL;
  712. }
  713. }
  714. /*
  715. * Passthru drive commands
  716. */
  717. else {
  718. /* Allocate a SCB and initialize passthru */
  719. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  720. *busy = 1;
  721. return NULL;
  722. }
  723. mbox = (mbox_t *)scb->raw_mbox;
  724. memset(mbox, 0, sizeof(scb->raw_mbox));
  725. if( adapter->support_ext_cdb ) {
  726. epthru = mega_prepare_extpassthru(adapter, scb, cmd,
  727. channel, target);
  728. mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
  729. mbox->m_out.xferaddr = scb->epthru_dma_addr;
  730. }
  731. else {
  732. pthru = mega_prepare_passthru(adapter, scb, cmd,
  733. channel, target);
  734. /* Initialize mailbox */
  735. if( adapter->has_64bit_addr ) {
  736. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
  737. }
  738. else {
  739. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
  740. }
  741. mbox->m_out.xferaddr = scb->pthru_dma_addr;
  742. }
  743. return scb;
  744. }
  745. return NULL;
  746. }
  747. /**
  748. * mega_prepare_passthru()
  749. * @adapter - pointer to our soft state
  750. * @scb - our scsi control block
  751. * @cmd - scsi command from the mid-layer
  752. * @channel - actual channel on the controller
  753. * @target - actual id on the controller.
  754. *
  755. * prepare a command for the scsi physical devices.
  756. */
  757. static mega_passthru *
  758. mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
  759. int channel, int target)
  760. {
  761. mega_passthru *pthru;
  762. pthru = scb->pthru;
  763. memset(pthru, 0, sizeof (mega_passthru));
  764. /* 0=6sec/1=60sec/2=10min/3=3hrs */
  765. pthru->timeout = 2;
  766. pthru->ars = 1;
  767. pthru->reqsenselen = 14;
  768. pthru->islogical = 0;
  769. pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
  770. pthru->target = (adapter->flag & BOARD_40LD) ?
  771. (channel << 4) | target : target;
  772. pthru->cdblen = cmd->cmd_len;
  773. pthru->logdrv = cmd->device->lun;
  774. memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
  775. /* Not sure about the direction */
  776. scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  777. /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
  778. switch (cmd->cmnd[0]) {
  779. case INQUIRY:
  780. case READ_CAPACITY:
  781. if(!(adapter->flag & (1L << cmd->device->channel))) {
  782. printk(KERN_NOTICE
  783. "scsi%d: scanning scsi channel %d [P%d] ",
  784. adapter->host->host_no,
  785. cmd->device->channel, channel);
  786. printk("for physical devices.\n");
  787. adapter->flag |= (1L << cmd->device->channel);
  788. }
  789. /* Fall through */
  790. default:
  791. pthru->numsgelements = mega_build_sglist(adapter, scb,
  792. &pthru->dataxferaddr, &pthru->dataxferlen);
  793. break;
  794. }
  795. return pthru;
  796. }
  797. /**
  798. * mega_prepare_extpassthru()
  799. * @adapter - pointer to our soft state
  800. * @scb - our scsi control block
  801. * @cmd - scsi command from the mid-layer
  802. * @channel - actual channel on the controller
  803. * @target - actual id on the controller.
  804. *
  805. * prepare a command for the scsi physical devices. This rountine prepares
  806. * commands for devices which can take extended CDBs (>10 bytes)
  807. */
  808. static mega_ext_passthru *
  809. mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
  810. int channel, int target)
  811. {
  812. mega_ext_passthru *epthru;
  813. epthru = scb->epthru;
  814. memset(epthru, 0, sizeof(mega_ext_passthru));
  815. /* 0=6sec/1=60sec/2=10min/3=3hrs */
  816. epthru->timeout = 2;
  817. epthru->ars = 1;
  818. epthru->reqsenselen = 14;
  819. epthru->islogical = 0;
  820. epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
  821. epthru->target = (adapter->flag & BOARD_40LD) ?
  822. (channel << 4) | target : target;
  823. epthru->cdblen = cmd->cmd_len;
  824. epthru->logdrv = cmd->device->lun;
  825. memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
  826. /* Not sure about the direction */
  827. scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  828. switch(cmd->cmnd[0]) {
  829. case INQUIRY:
  830. case READ_CAPACITY:
  831. if(!(adapter->flag & (1L << cmd->device->channel))) {
  832. printk(KERN_NOTICE
  833. "scsi%d: scanning scsi channel %d [P%d] ",
  834. adapter->host->host_no,
  835. cmd->device->channel, channel);
  836. printk("for physical devices.\n");
  837. adapter->flag |= (1L << cmd->device->channel);
  838. }
  839. /* Fall through */
  840. default:
  841. epthru->numsgelements = mega_build_sglist(adapter, scb,
  842. &epthru->dataxferaddr, &epthru->dataxferlen);
  843. break;
  844. }
  845. return epthru;
  846. }
  847. static void
  848. __mega_runpendq(adapter_t *adapter)
  849. {
  850. scb_t *scb;
  851. struct list_head *pos, *next;
  852. /* Issue any pending commands to the card */
  853. list_for_each_safe(pos, next, &adapter->pending_list) {
  854. scb = list_entry(pos, scb_t, list);
  855. if( !(scb->state & SCB_ISSUED) ) {
  856. if( issue_scb(adapter, scb) != 0 )
  857. return;
  858. }
  859. }
  860. return;
  861. }
  862. /**
  863. * issue_scb()
  864. * @adapter - pointer to our soft state
  865. * @scb - scsi control block
  866. *
  867. * Post a command to the card if the mailbox is available, otherwise return
  868. * busy. We also take the scb from the pending list if the mailbox is
  869. * available.
  870. */
  871. static int
  872. issue_scb(adapter_t *adapter, scb_t *scb)
  873. {
  874. volatile mbox64_t *mbox64 = adapter->mbox64;
  875. volatile mbox_t *mbox = adapter->mbox;
  876. unsigned int i = 0;
  877. if(unlikely(mbox->m_in.busy)) {
  878. do {
  879. udelay(1);
  880. i++;
  881. } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
  882. if(mbox->m_in.busy) return -1;
  883. }
  884. /* Copy mailbox data into host structure */
  885. memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
  886. sizeof(struct mbox_out));
  887. mbox->m_out.cmdid = scb->idx; /* Set cmdid */
  888. mbox->m_in.busy = 1; /* Set busy */
  889. /*
  890. * Increment the pending queue counter
  891. */
  892. atomic_inc(&adapter->pend_cmds);
  893. switch (mbox->m_out.cmd) {
  894. case MEGA_MBOXCMD_LREAD64:
  895. case MEGA_MBOXCMD_LWRITE64:
  896. case MEGA_MBOXCMD_PASSTHRU64:
  897. case MEGA_MBOXCMD_EXTPTHRU:
  898. mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
  899. mbox64->xfer_segment_hi = 0;
  900. mbox->m_out.xferaddr = 0xFFFFFFFF;
  901. break;
  902. default:
  903. mbox64->xfer_segment_lo = 0;
  904. mbox64->xfer_segment_hi = 0;
  905. }
  906. /*
  907. * post the command
  908. */
  909. scb->state |= SCB_ISSUED;
  910. if( likely(adapter->flag & BOARD_MEMMAP) ) {
  911. mbox->m_in.poll = 0;
  912. mbox->m_in.ack = 0;
  913. WRINDOOR(adapter, adapter->mbox_dma | 0x1);
  914. }
  915. else {
  916. irq_enable(adapter);
  917. issue_command(adapter);
  918. }
  919. return 0;
  920. }
  921. /*
  922. * Wait until the controller's mailbox is available
  923. */
  924. static inline int
  925. mega_busywait_mbox (adapter_t *adapter)
  926. {
  927. if (adapter->mbox->m_in.busy)
  928. return __mega_busywait_mbox(adapter);
  929. return 0;
  930. }
  931. /**
  932. * issue_scb_block()
  933. * @adapter - pointer to our soft state
  934. * @raw_mbox - the mailbox
  935. *
  936. * Issue a scb in synchronous and non-interrupt mode
  937. */
  938. static int
  939. issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
  940. {
  941. volatile mbox64_t *mbox64 = adapter->mbox64;
  942. volatile mbox_t *mbox = adapter->mbox;
  943. u8 byte;
  944. /* Wait until mailbox is free */
  945. if(mega_busywait_mbox (adapter))
  946. goto bug_blocked_mailbox;
  947. /* Copy mailbox data into host structure */
  948. memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
  949. mbox->m_out.cmdid = 0xFE;
  950. mbox->m_in.busy = 1;
  951. switch (raw_mbox[0]) {
  952. case MEGA_MBOXCMD_LREAD64:
  953. case MEGA_MBOXCMD_LWRITE64:
  954. case MEGA_MBOXCMD_PASSTHRU64:
  955. case MEGA_MBOXCMD_EXTPTHRU:
  956. mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
  957. mbox64->xfer_segment_hi = 0;
  958. mbox->m_out.xferaddr = 0xFFFFFFFF;
  959. break;
  960. default:
  961. mbox64->xfer_segment_lo = 0;
  962. mbox64->xfer_segment_hi = 0;
  963. }
  964. if( likely(adapter->flag & BOARD_MEMMAP) ) {
  965. mbox->m_in.poll = 0;
  966. mbox->m_in.ack = 0;
  967. mbox->m_in.numstatus = 0xFF;
  968. mbox->m_in.status = 0xFF;
  969. WRINDOOR(adapter, adapter->mbox_dma | 0x1);
  970. while((volatile u8)mbox->m_in.numstatus == 0xFF)
  971. cpu_relax();
  972. mbox->m_in.numstatus = 0xFF;
  973. while( (volatile u8)mbox->m_in.poll != 0x77 )
  974. cpu_relax();
  975. mbox->m_in.poll = 0;
  976. mbox->m_in.ack = 0x77;
  977. WRINDOOR(adapter, adapter->mbox_dma | 0x2);
  978. while(RDINDOOR(adapter) & 0x2)
  979. cpu_relax();
  980. }
  981. else {
  982. irq_disable(adapter);
  983. issue_command(adapter);
  984. while (!((byte = irq_state(adapter)) & INTR_VALID))
  985. cpu_relax();
  986. set_irq_state(adapter, byte);
  987. irq_enable(adapter);
  988. irq_ack(adapter);
  989. }
  990. return mbox->m_in.status;
  991. bug_blocked_mailbox:
  992. printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
  993. udelay (1000);
  994. return -1;
  995. }
  996. /**
  997. * megaraid_isr_iomapped()
  998. * @irq - irq
  999. * @devp - pointer to our soft state
  1000. * @regs - unused
  1001. *
  1002. * Interrupt service routine for io-mapped controllers.
  1003. * Find out if our device is interrupting. If yes, acknowledge the interrupt
  1004. * and service the completed commands.
  1005. */
  1006. static irqreturn_t
  1007. megaraid_isr_iomapped(int irq, void *devp, struct pt_regs *regs)
  1008. {
  1009. adapter_t *adapter = devp;
  1010. unsigned long flags;
  1011. u8 status;
  1012. u8 nstatus;
  1013. u8 completed[MAX_FIRMWARE_STATUS];
  1014. u8 byte;
  1015. int handled = 0;
  1016. /*
  1017. * loop till F/W has more commands for us to complete.
  1018. */
  1019. spin_lock_irqsave(&adapter->lock, flags);
  1020. do {
  1021. /* Check if a valid interrupt is pending */
  1022. byte = irq_state(adapter);
  1023. if( (byte & VALID_INTR_BYTE) == 0 ) {
  1024. /*
  1025. * No more pending commands
  1026. */
  1027. goto out_unlock;
  1028. }
  1029. set_irq_state(adapter, byte);
  1030. while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
  1031. == 0xFF)
  1032. cpu_relax();
  1033. adapter->mbox->m_in.numstatus = 0xFF;
  1034. status = adapter->mbox->m_in.status;
  1035. /*
  1036. * decrement the pending queue counter
  1037. */
  1038. atomic_sub(nstatus, &adapter->pend_cmds);
  1039. memcpy(completed, (void *)adapter->mbox->m_in.completed,
  1040. nstatus);
  1041. /* Acknowledge interrupt */
  1042. irq_ack(adapter);
  1043. mega_cmd_done(adapter, completed, nstatus, status);
  1044. mega_rundoneq(adapter);
  1045. handled = 1;
  1046. /* Loop through any pending requests */
  1047. if(atomic_read(&adapter->quiescent) == 0) {
  1048. mega_runpendq(adapter);
  1049. }
  1050. } while(1);
  1051. out_unlock:
  1052. spin_unlock_irqrestore(&adapter->lock, flags);
  1053. return IRQ_RETVAL(handled);
  1054. }
  1055. /**
  1056. * megaraid_isr_memmapped()
  1057. * @irq - irq
  1058. * @devp - pointer to our soft state
  1059. * @regs - unused
  1060. *
  1061. * Interrupt service routine for memory-mapped controllers.
  1062. * Find out if our device is interrupting. If yes, acknowledge the interrupt
  1063. * and service the completed commands.
  1064. */
  1065. static irqreturn_t
  1066. megaraid_isr_memmapped(int irq, void *devp, struct pt_regs *regs)
  1067. {
  1068. adapter_t *adapter = devp;
  1069. unsigned long flags;
  1070. u8 status;
  1071. u32 dword = 0;
  1072. u8 nstatus;
  1073. u8 completed[MAX_FIRMWARE_STATUS];
  1074. int handled = 0;
  1075. /*
  1076. * loop till F/W has more commands for us to complete.
  1077. */
  1078. spin_lock_irqsave(&adapter->lock, flags);
  1079. do {
  1080. /* Check if a valid interrupt is pending */
  1081. dword = RDOUTDOOR(adapter);
  1082. if(dword != 0x10001234) {
  1083. /*
  1084. * No more pending commands
  1085. */
  1086. goto out_unlock;
  1087. }
  1088. WROUTDOOR(adapter, 0x10001234);
  1089. while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
  1090. == 0xFF) {
  1091. cpu_relax();
  1092. }
  1093. adapter->mbox->m_in.numstatus = 0xFF;
  1094. status = adapter->mbox->m_in.status;
  1095. /*
  1096. * decrement the pending queue counter
  1097. */
  1098. atomic_sub(nstatus, &adapter->pend_cmds);
  1099. memcpy(completed, (void *)adapter->mbox->m_in.completed,
  1100. nstatus);
  1101. /* Acknowledge interrupt */
  1102. WRINDOOR(adapter, 0x2);
  1103. handled = 1;
  1104. while( RDINDOOR(adapter) & 0x02 ) cpu_relax();
  1105. mega_cmd_done(adapter, completed, nstatus, status);
  1106. mega_rundoneq(adapter);
  1107. /* Loop through any pending requests */
  1108. if(atomic_read(&adapter->quiescent) == 0) {
  1109. mega_runpendq(adapter);
  1110. }
  1111. } while(1);
  1112. out_unlock:
  1113. spin_unlock_irqrestore(&adapter->lock, flags);
  1114. return IRQ_RETVAL(handled);
  1115. }
  1116. /**
  1117. * mega_cmd_done()
  1118. * @adapter - pointer to our soft state
  1119. * @completed - array of ids of completed commands
  1120. * @nstatus - number of completed commands
  1121. * @status - status of the last command completed
  1122. *
  1123. * Complete the comamnds and call the scsi mid-layer callback hooks.
  1124. */
  1125. static void
  1126. mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
  1127. {
  1128. mega_ext_passthru *epthru = NULL;
  1129. struct scatterlist *sgl;
  1130. Scsi_Cmnd *cmd = NULL;
  1131. mega_passthru *pthru = NULL;
  1132. mbox_t *mbox = NULL;
  1133. u8 c;
  1134. scb_t *scb;
  1135. int islogical;
  1136. int cmdid;
  1137. int i;
  1138. /*
  1139. * for all the commands completed, call the mid-layer callback routine
  1140. * and free the scb.
  1141. */
  1142. for( i = 0; i < nstatus; i++ ) {
  1143. cmdid = completed[i];
  1144. if( cmdid == CMDID_INT_CMDS ) { /* internal command */
  1145. scb = &adapter->int_scb;
  1146. cmd = scb->cmd;
  1147. mbox = (mbox_t *)scb->raw_mbox;
  1148. /*
  1149. * Internal command interface do not fire the extended
  1150. * passthru or 64-bit passthru
  1151. */
  1152. pthru = scb->pthru;
  1153. }
  1154. else {
  1155. scb = &adapter->scb_list[cmdid];
  1156. /*
  1157. * Make sure f/w has completed a valid command
  1158. */
  1159. if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
  1160. printk(KERN_CRIT
  1161. "megaraid: invalid command ");
  1162. printk("Id %d, scb->state:%x, scsi cmd:%p\n",
  1163. cmdid, scb->state, scb->cmd);
  1164. continue;
  1165. }
  1166. /*
  1167. * Was a abort issued for this command
  1168. */
  1169. if( scb->state & SCB_ABORT ) {
  1170. printk(KERN_WARNING
  1171. "megaraid: aborted cmd %lx[%x] complete.\n",
  1172. scb->cmd->serial_number, scb->idx);
  1173. scb->cmd->result = (DID_ABORT << 16);
  1174. list_add_tail(SCSI_LIST(scb->cmd),
  1175. &adapter->completed_list);
  1176. mega_free_scb(adapter, scb);
  1177. continue;
  1178. }
  1179. /*
  1180. * Was a reset issued for this command
  1181. */
  1182. if( scb->state & SCB_RESET ) {
  1183. printk(KERN_WARNING
  1184. "megaraid: reset cmd %lx[%x] complete.\n",
  1185. scb->cmd->serial_number, scb->idx);
  1186. scb->cmd->result = (DID_RESET << 16);
  1187. list_add_tail(SCSI_LIST(scb->cmd),
  1188. &adapter->completed_list);
  1189. mega_free_scb (adapter, scb);
  1190. continue;
  1191. }
  1192. cmd = scb->cmd;
  1193. pthru = scb->pthru;
  1194. epthru = scb->epthru;
  1195. mbox = (mbox_t *)scb->raw_mbox;
  1196. #if MEGA_HAVE_STATS
  1197. {
  1198. int logdrv = mbox->m_out.logdrv;
  1199. islogical = adapter->logdrv_chan[cmd->channel];
  1200. /*
  1201. * Maintain an error counter for the logical drive.
  1202. * Some application like SNMP agent need such
  1203. * statistics
  1204. */
  1205. if( status && islogical && (cmd->cmnd[0] == READ_6 ||
  1206. cmd->cmnd[0] == READ_10 ||
  1207. cmd->cmnd[0] == READ_12)) {
  1208. /*
  1209. * Logical drive number increases by 0x80 when
  1210. * a logical drive is deleted
  1211. */
  1212. adapter->rd_errors[logdrv%0x80]++;
  1213. }
  1214. if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
  1215. cmd->cmnd[0] == WRITE_10 ||
  1216. cmd->cmnd[0] == WRITE_12)) {
  1217. /*
  1218. * Logical drive number increases by 0x80 when
  1219. * a logical drive is deleted
  1220. */
  1221. adapter->wr_errors[logdrv%0x80]++;
  1222. }
  1223. }
  1224. #endif
  1225. }
  1226. /*
  1227. * Do not return the presence of hard disk on the channel so,
  1228. * inquiry sent, and returned data==hard disk or removable
  1229. * hard disk and not logical, request should return failure! -
  1230. * PJ
  1231. */
  1232. islogical = adapter->logdrv_chan[cmd->device->channel];
  1233. if( cmd->cmnd[0] == INQUIRY && !islogical ) {
  1234. if( cmd->use_sg ) {
  1235. sgl = (struct scatterlist *)
  1236. cmd->request_buffer;
  1237. if( sgl->page ) {
  1238. c = *(unsigned char *)
  1239. page_address((&sgl[0])->page) +
  1240. (&sgl[0])->offset;
  1241. }
  1242. else {
  1243. printk(KERN_WARNING
  1244. "megaraid: invalid sg.\n");
  1245. c = 0;
  1246. }
  1247. }
  1248. else {
  1249. c = *(u8 *)cmd->request_buffer;
  1250. }
  1251. if(IS_RAID_CH(adapter, cmd->device->channel) &&
  1252. ((c & 0x1F ) == TYPE_DISK)) {
  1253. status = 0xF0;
  1254. }
  1255. }
  1256. /* clear result; otherwise, success returns corrupt value */
  1257. cmd->result = 0;
  1258. /* Convert MegaRAID status to Linux error code */
  1259. switch (status) {
  1260. case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
  1261. cmd->result |= (DID_OK << 16);
  1262. break;
  1263. case 0x02: /* ERROR_ABORTED, i.e.
  1264. SCSI_STATUS_CHECK_CONDITION */
  1265. /* set sense_buffer and result fields */
  1266. if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
  1267. mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
  1268. memcpy(cmd->sense_buffer, pthru->reqsensearea,
  1269. 14);
  1270. cmd->result = (DRIVER_SENSE << 24) |
  1271. (DID_OK << 16) |
  1272. (CHECK_CONDITION << 1);
  1273. }
  1274. else {
  1275. if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
  1276. memcpy(cmd->sense_buffer,
  1277. epthru->reqsensearea, 14);
  1278. cmd->result = (DRIVER_SENSE << 24) |
  1279. (DID_OK << 16) |
  1280. (CHECK_CONDITION << 1);
  1281. } else {
  1282. cmd->sense_buffer[0] = 0x70;
  1283. cmd->sense_buffer[2] = ABORTED_COMMAND;
  1284. cmd->result |= (CHECK_CONDITION << 1);
  1285. }
  1286. }
  1287. break;
  1288. case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
  1289. SCSI_STATUS_BUSY */
  1290. cmd->result |= (DID_BUS_BUSY << 16) | status;
  1291. break;
  1292. default:
  1293. #if MEGA_HAVE_CLUSTERING
  1294. /*
  1295. * If TEST_UNIT_READY fails, we know
  1296. * MEGA_RESERVATION_STATUS failed
  1297. */
  1298. if( cmd->cmnd[0] == TEST_UNIT_READY ) {
  1299. cmd->result |= (DID_ERROR << 16) |
  1300. (RESERVATION_CONFLICT << 1);
  1301. }
  1302. else
  1303. /*
  1304. * Error code returned is 1 if Reserve or Release
  1305. * failed or the input parameter is invalid
  1306. */
  1307. if( status == 1 &&
  1308. (cmd->cmnd[0] == RESERVE ||
  1309. cmd->cmnd[0] == RELEASE) ) {
  1310. cmd->result |= (DID_ERROR << 16) |
  1311. (RESERVATION_CONFLICT << 1);
  1312. }
  1313. else
  1314. #endif
  1315. cmd->result |= (DID_BAD_TARGET << 16)|status;
  1316. }
  1317. /*
  1318. * Only free SCBs for the commands coming down from the
  1319. * mid-layer, not for which were issued internally
  1320. *
  1321. * For internal command, restore the status returned by the
  1322. * firmware so that user can interpret it.
  1323. */
  1324. if( cmdid == CMDID_INT_CMDS ) { /* internal command */
  1325. cmd->result = status;
  1326. /*
  1327. * Remove the internal command from the pending list
  1328. */
  1329. list_del_init(&scb->list);
  1330. scb->state = SCB_FREE;
  1331. }
  1332. else {
  1333. mega_free_scb(adapter, scb);
  1334. }
  1335. /* Add Scsi_Command to end of completed queue */
  1336. list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
  1337. }
  1338. }
  1339. /*
  1340. * mega_runpendq()
  1341. *
  1342. * Run through the list of completed requests and finish it
  1343. */
  1344. static void
  1345. mega_rundoneq (adapter_t *adapter)
  1346. {
  1347. Scsi_Cmnd *cmd;
  1348. struct list_head *pos;
  1349. list_for_each(pos, &adapter->completed_list) {
  1350. Scsi_Pointer* spos = (Scsi_Pointer *)pos;
  1351. cmd = list_entry(spos, Scsi_Cmnd, SCp);
  1352. cmd->scsi_done(cmd);
  1353. }
  1354. INIT_LIST_HEAD(&adapter->completed_list);
  1355. }
  1356. /*
  1357. * Free a SCB structure
  1358. * Note: We assume the scsi commands associated with this scb is not free yet.
  1359. */
  1360. static void
  1361. mega_free_scb(adapter_t *adapter, scb_t *scb)
  1362. {
  1363. switch( scb->dma_type ) {
  1364. case MEGA_DMA_TYPE_NONE:
  1365. break;
  1366. case MEGA_BULK_DATA:
  1367. pci_unmap_page(adapter->dev, scb->dma_h_bulkdata,
  1368. scb->cmd->request_bufflen, scb->dma_direction);
  1369. break;
  1370. case MEGA_SGLIST:
  1371. pci_unmap_sg(adapter->dev, scb->cmd->request_buffer,
  1372. scb->cmd->use_sg, scb->dma_direction);
  1373. break;
  1374. default:
  1375. break;
  1376. }
  1377. /*
  1378. * Remove from the pending list
  1379. */
  1380. list_del_init(&scb->list);
  1381. /* Link the scb back into free list */
  1382. scb->state = SCB_FREE;
  1383. scb->cmd = NULL;
  1384. list_add(&scb->list, &adapter->free_list);
  1385. }
  1386. static int
  1387. __mega_busywait_mbox (adapter_t *adapter)
  1388. {
  1389. volatile mbox_t *mbox = adapter->mbox;
  1390. long counter;
  1391. for (counter = 0; counter < 10000; counter++) {
  1392. if (!mbox->m_in.busy)
  1393. return 0;
  1394. udelay(100); yield();
  1395. }
  1396. return -1; /* give up after 1 second */
  1397. }
  1398. /*
  1399. * Copies data to SGLIST
  1400. * Note: For 64 bit cards, we need a minimum of one SG element for read/write
  1401. */
  1402. static int
  1403. mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
  1404. {
  1405. struct scatterlist *sgl;
  1406. struct page *page;
  1407. unsigned long offset;
  1408. Scsi_Cmnd *cmd;
  1409. int sgcnt;
  1410. int idx;
  1411. cmd = scb->cmd;
  1412. /* Scatter-gather not used */
  1413. if( !cmd->use_sg ) {
  1414. page = virt_to_page(cmd->request_buffer);
  1415. offset = offset_in_page(cmd->request_buffer);
  1416. scb->dma_h_bulkdata = pci_map_page(adapter->dev,
  1417. page, offset,
  1418. cmd->request_bufflen,
  1419. scb->dma_direction);
  1420. scb->dma_type = MEGA_BULK_DATA;
  1421. /*
  1422. * We need to handle special 64-bit commands that need a
  1423. * minimum of 1 SG
  1424. */
  1425. if( adapter->has_64bit_addr ) {
  1426. scb->sgl64[0].address = scb->dma_h_bulkdata;
  1427. scb->sgl64[0].length = cmd->request_bufflen;
  1428. *buf = (u32)scb->sgl_dma_addr;
  1429. *len = (u32)cmd->request_bufflen;
  1430. return 1;
  1431. }
  1432. else {
  1433. *buf = (u32)scb->dma_h_bulkdata;
  1434. *len = (u32)cmd->request_bufflen;
  1435. }
  1436. return 0;
  1437. }
  1438. sgl = (struct scatterlist *)cmd->request_buffer;
  1439. /*
  1440. * Copy Scatter-Gather list info into controller structure.
  1441. *
  1442. * The number of sg elements returned must not exceed our limit
  1443. */
  1444. sgcnt = pci_map_sg(adapter->dev, sgl, cmd->use_sg,
  1445. scb->dma_direction);
  1446. scb->dma_type = MEGA_SGLIST;
  1447. if( sgcnt > adapter->sglen ) BUG();
  1448. for( idx = 0; idx < sgcnt; idx++, sgl++ ) {
  1449. if( adapter->has_64bit_addr ) {
  1450. scb->sgl64[idx].address = sg_dma_address(sgl);
  1451. scb->sgl64[idx].length = sg_dma_len(sgl);
  1452. }
  1453. else {
  1454. scb->sgl[idx].address = sg_dma_address(sgl);
  1455. scb->sgl[idx].length = sg_dma_len(sgl);
  1456. }
  1457. }
  1458. /* Reset pointer and length fields */
  1459. *buf = scb->sgl_dma_addr;
  1460. /*
  1461. * For passthru command, dataxferlen must be set, even for commands
  1462. * with a sg list
  1463. */
  1464. *len = (u32)cmd->request_bufflen;
  1465. /* Return count of SG requests */
  1466. return sgcnt;
  1467. }
  1468. /*
  1469. * mega_8_to_40ld()
  1470. *
  1471. * takes all info in AdapterInquiry structure and puts it into ProductInfo and
  1472. * Enquiry3 structures for later use
  1473. */
  1474. static void
  1475. mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
  1476. mega_product_info *product_info)
  1477. {
  1478. int i;
  1479. product_info->max_commands = inquiry->adapter_info.max_commands;
  1480. enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
  1481. product_info->nchannels = inquiry->adapter_info.nchannels;
  1482. for (i = 0; i < 4; i++) {
  1483. product_info->fw_version[i] =
  1484. inquiry->adapter_info.fw_version[i];
  1485. product_info->bios_version[i] =
  1486. inquiry->adapter_info.bios_version[i];
  1487. }
  1488. enquiry3->cache_flush_interval =
  1489. inquiry->adapter_info.cache_flush_interval;
  1490. product_info->dram_size = inquiry->adapter_info.dram_size;
  1491. enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
  1492. for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
  1493. enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
  1494. enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
  1495. enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
  1496. }
  1497. for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
  1498. enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
  1499. }
  1500. static inline void
  1501. mega_free_sgl(adapter_t *adapter)
  1502. {
  1503. scb_t *scb;
  1504. int i;
  1505. for(i = 0; i < adapter->max_cmds; i++) {
  1506. scb = &adapter->scb_list[i];
  1507. if( scb->sgl64 ) {
  1508. pci_free_consistent(adapter->dev,
  1509. sizeof(mega_sgl64) * adapter->sglen,
  1510. scb->sgl64,
  1511. scb->sgl_dma_addr);
  1512. scb->sgl64 = NULL;
  1513. }
  1514. if( scb->pthru ) {
  1515. pci_free_consistent(adapter->dev, sizeof(mega_passthru),
  1516. scb->pthru, scb->pthru_dma_addr);
  1517. scb->pthru = NULL;
  1518. }
  1519. if( scb->epthru ) {
  1520. pci_free_consistent(adapter->dev,
  1521. sizeof(mega_ext_passthru),
  1522. scb->epthru, scb->epthru_dma_addr);
  1523. scb->epthru = NULL;
  1524. }
  1525. }
  1526. }
  1527. /*
  1528. * Get information about the card/driver
  1529. */
  1530. const char *
  1531. megaraid_info(struct Scsi_Host *host)
  1532. {
  1533. static char buffer[512];
  1534. adapter_t *adapter;
  1535. adapter = (adapter_t *)host->hostdata;
  1536. sprintf (buffer,
  1537. "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
  1538. adapter->fw_version, adapter->product_info.max_commands,
  1539. adapter->host->max_id, adapter->host->max_channel,
  1540. adapter->host->max_lun);
  1541. return buffer;
  1542. }
  1543. /*
  1544. * Abort a previous SCSI request. Only commands on the pending list can be
  1545. * aborted. All the commands issued to the F/W must complete.
  1546. */
  1547. static int
  1548. megaraid_abort(Scsi_Cmnd *cmd)
  1549. {
  1550. adapter_t *adapter;
  1551. int rval;
  1552. adapter = (adapter_t *)cmd->device->host->hostdata;
  1553. rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
  1554. /*
  1555. * This is required here to complete any completed requests
  1556. * to be communicated over to the mid layer.
  1557. */
  1558. mega_rundoneq(adapter);
  1559. return rval;
  1560. }
  1561. static int
  1562. megaraid_reset(Scsi_Cmnd *cmd)
  1563. {
  1564. adapter_t *adapter;
  1565. megacmd_t mc;
  1566. int rval;
  1567. adapter = (adapter_t *)cmd->device->host->hostdata;
  1568. #if MEGA_HAVE_CLUSTERING
  1569. mc.cmd = MEGA_CLUSTER_CMD;
  1570. mc.opcode = MEGA_RESET_RESERVATIONS;
  1571. spin_unlock_irq(&adapter->lock);
  1572. if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) != 0 ) {
  1573. printk(KERN_WARNING
  1574. "megaraid: reservation reset failed.\n");
  1575. }
  1576. else {
  1577. printk(KERN_INFO "megaraid: reservation reset.\n");
  1578. }
  1579. spin_lock_irq(&adapter->lock);
  1580. #endif
  1581. rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
  1582. /*
  1583. * This is required here to complete any completed requests
  1584. * to be communicated over to the mid layer.
  1585. */
  1586. mega_rundoneq(adapter);
  1587. return rval;
  1588. }
  1589. /**
  1590. * megaraid_abort_and_reset()
  1591. * @adapter - megaraid soft state
  1592. * @cmd - scsi command to be aborted or reset
  1593. * @aor - abort or reset flag
  1594. *
  1595. * Try to locate the scsi command in the pending queue. If found and is not
  1596. * issued to the controller, abort/reset it. Otherwise return failure
  1597. */
  1598. static int
  1599. megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
  1600. {
  1601. struct list_head *pos, *next;
  1602. scb_t *scb;
  1603. printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
  1604. (aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
  1605. cmd->cmnd[0], cmd->device->channel,
  1606. cmd->device->id, cmd->device->lun);
  1607. if(list_empty(&adapter->pending_list))
  1608. return FALSE;
  1609. list_for_each_safe(pos, next, &adapter->pending_list) {
  1610. scb = list_entry(pos, scb_t, list);
  1611. if (scb->cmd == cmd) { /* Found command */
  1612. scb->state |= aor;
  1613. /*
  1614. * Check if this command has firmare owenership. If
  1615. * yes, we cannot reset this command. Whenever, f/w
  1616. * completes this command, we will return appropriate
  1617. * status from ISR.
  1618. */
  1619. if( scb->state & SCB_ISSUED ) {
  1620. printk(KERN_WARNING
  1621. "megaraid: %s-%lx[%x], fw owner.\n",
  1622. (aor==SCB_ABORT) ? "ABORTING":"RESET",
  1623. cmd->serial_number, scb->idx);
  1624. return FALSE;
  1625. }
  1626. else {
  1627. /*
  1628. * Not yet issued! Remove from the pending
  1629. * list
  1630. */
  1631. printk(KERN_WARNING
  1632. "megaraid: %s-%lx[%x], driver owner.\n",
  1633. (aor==SCB_ABORT) ? "ABORTING":"RESET",
  1634. cmd->serial_number, scb->idx);
  1635. mega_free_scb(adapter, scb);
  1636. if( aor == SCB_ABORT ) {
  1637. cmd->result = (DID_ABORT << 16);
  1638. }
  1639. else {
  1640. cmd->result = (DID_RESET << 16);
  1641. }
  1642. list_add_tail(SCSI_LIST(cmd),
  1643. &adapter->completed_list);
  1644. return TRUE;
  1645. }
  1646. }
  1647. }
  1648. return FALSE;
  1649. }
  1650. static inline int
  1651. make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
  1652. {
  1653. *pdev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
  1654. if( *pdev == NULL ) return -1;
  1655. memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
  1656. if( pci_set_dma_mask(*pdev, 0xffffffff) != 0 ) {
  1657. kfree(*pdev);
  1658. return -1;
  1659. }
  1660. return 0;
  1661. }
  1662. static inline void
  1663. free_local_pdev(struct pci_dev *pdev)
  1664. {
  1665. kfree(pdev);
  1666. }
  1667. /**
  1668. * mega_allocate_inquiry()
  1669. * @dma_handle - handle returned for dma address
  1670. * @pdev - handle to pci device
  1671. *
  1672. * allocates memory for inquiry structure
  1673. */
  1674. static inline void *
  1675. mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
  1676. {
  1677. return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
  1678. }
  1679. static inline void
  1680. mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
  1681. {
  1682. pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
  1683. }
  1684. #ifdef CONFIG_PROC_FS
  1685. /* Following code handles /proc fs */
  1686. #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
  1687. S_IRUSR | S_IFREG, \
  1688. controller_proc_dir_entry, \
  1689. func, adapter)
  1690. /**
  1691. * mega_create_proc_entry()
  1692. * @index - index in soft state array
  1693. * @parent - parent node for this /proc entry
  1694. *
  1695. * Creates /proc entries for our controllers.
  1696. */
  1697. static void
  1698. mega_create_proc_entry(int index, struct proc_dir_entry *parent)
  1699. {
  1700. struct proc_dir_entry *controller_proc_dir_entry = NULL;
  1701. u8 string[64] = { 0 };
  1702. adapter_t *adapter = hba_soft_state[index];
  1703. sprintf(string, "hba%d", adapter->host->host_no);
  1704. controller_proc_dir_entry =
  1705. adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
  1706. if(!controller_proc_dir_entry) {
  1707. printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
  1708. return;
  1709. }
  1710. adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
  1711. adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
  1712. adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
  1713. #if MEGA_HAVE_ENH_PROC
  1714. adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
  1715. adapter->proc_battery = CREATE_READ_PROC("battery-status",
  1716. proc_battery);
  1717. /*
  1718. * Display each physical drive on its channel
  1719. */
  1720. adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
  1721. proc_pdrv_ch0);
  1722. adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
  1723. proc_pdrv_ch1);
  1724. adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
  1725. proc_pdrv_ch2);
  1726. adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
  1727. proc_pdrv_ch3);
  1728. /*
  1729. * Display a set of up to 10 logical drive through each of following
  1730. * /proc entries
  1731. */
  1732. adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
  1733. proc_rdrv_10);
  1734. adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
  1735. proc_rdrv_20);
  1736. adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
  1737. proc_rdrv_30);
  1738. adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
  1739. proc_rdrv_40);
  1740. #endif
  1741. }
  1742. /**
  1743. * proc_read_config()
  1744. * @page - buffer to write the data in
  1745. * @start - where the actual data has been written in page
  1746. * @offset - same meaning as the read system call
  1747. * @count - same meaning as the read system call
  1748. * @eof - set if no more data needs to be returned
  1749. * @data - pointer to our soft state
  1750. *
  1751. * Display configuration information about the controller.
  1752. */
  1753. static int
  1754. proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
  1755. void *data)
  1756. {
  1757. adapter_t *adapter = (adapter_t *)data;
  1758. int len = 0;
  1759. len += sprintf(page+len, "%s", MEGARAID_VERSION);
  1760. if(adapter->product_info.product_name[0])
  1761. len += sprintf(page+len, "%s\n",
  1762. adapter->product_info.product_name);
  1763. len += sprintf(page+len, "Controller Type: ");
  1764. if( adapter->flag & BOARD_MEMMAP ) {
  1765. len += sprintf(page+len,
  1766. "438/466/467/471/493/518/520/531/532\n");
  1767. }
  1768. else {
  1769. len += sprintf(page+len,
  1770. "418/428/434\n");
  1771. }
  1772. if(adapter->flag & BOARD_40LD) {
  1773. len += sprintf(page+len,
  1774. "Controller Supports 40 Logical Drives\n");
  1775. }
  1776. if(adapter->flag & BOARD_64BIT) {
  1777. len += sprintf(page+len,
  1778. "Controller capable of 64-bit memory addressing\n");
  1779. }
  1780. if( adapter->has_64bit_addr ) {
  1781. len += sprintf(page+len,
  1782. "Controller using 64-bit memory addressing\n");
  1783. }
  1784. else {
  1785. len += sprintf(page+len,
  1786. "Controller is not using 64-bit memory addressing\n");
  1787. }
  1788. len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
  1789. adapter->host->irq);
  1790. len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
  1791. adapter->numldrv, adapter->product_info.nchannels);
  1792. len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
  1793. adapter->fw_version, adapter->bios_version,
  1794. adapter->product_info.dram_size);
  1795. len += sprintf(page+len,
  1796. "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
  1797. adapter->product_info.max_commands, adapter->max_cmds);
  1798. len += sprintf(page+len, "support_ext_cdb = %d\n",
  1799. adapter->support_ext_cdb);
  1800. len += sprintf(page+len, "support_random_del = %d\n",
  1801. adapter->support_random_del);
  1802. len += sprintf(page+len, "boot_ldrv_enabled = %d\n",
  1803. adapter->boot_ldrv_enabled);
  1804. len += sprintf(page+len, "boot_ldrv = %d\n",
  1805. adapter->boot_ldrv);
  1806. len += sprintf(page+len, "boot_pdrv_enabled = %d\n",
  1807. adapter->boot_pdrv_enabled);
  1808. len += sprintf(page+len, "boot_pdrv_ch = %d\n",
  1809. adapter->boot_pdrv_ch);
  1810. len += sprintf(page+len, "boot_pdrv_tgt = %d\n",
  1811. adapter->boot_pdrv_tgt);
  1812. len += sprintf(page+len, "quiescent = %d\n",
  1813. atomic_read(&adapter->quiescent));
  1814. len += sprintf(page+len, "has_cluster = %d\n",
  1815. adapter->has_cluster);
  1816. len += sprintf(page+len, "\nModule Parameters:\n");
  1817. len += sprintf(page+len, "max_cmd_per_lun = %d\n",
  1818. max_cmd_per_lun);
  1819. len += sprintf(page+len, "max_sectors_per_io = %d\n",
  1820. max_sectors_per_io);
  1821. *eof = 1;
  1822. return len;
  1823. }
  1824. /**
  1825. * proc_read_stat()
  1826. * @page - buffer to write the data in
  1827. * @start - where the actual data has been written in page
  1828. * @offset - same meaning as the read system call
  1829. * @count - same meaning as the read system call
  1830. * @eof - set if no more data needs to be returned
  1831. * @data - pointer to our soft state
  1832. *
  1833. * Diaplay statistical information about the I/O activity.
  1834. */
  1835. static int
  1836. proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
  1837. void *data)
  1838. {
  1839. adapter_t *adapter;
  1840. int len;
  1841. int i;
  1842. i = 0; /* avoid compilation warnings */
  1843. len = 0;
  1844. adapter = (adapter_t *)data;
  1845. len = sprintf(page, "Statistical Information for this controller\n");
  1846. len += sprintf(page+len, "pend_cmds = %d\n",
  1847. atomic_read(&adapter->pend_cmds));
  1848. #if MEGA_HAVE_STATS
  1849. for(i = 0; i < adapter->numldrv; i++) {
  1850. len += sprintf(page+len, "Logical Drive %d:\n", i);
  1851. len += sprintf(page+len,
  1852. "\tReads Issued = %lu, Writes Issued = %lu\n",
  1853. adapter->nreads[i], adapter->nwrites[i]);
  1854. len += sprintf(page+len,
  1855. "\tSectors Read = %lu, Sectors Written = %lu\n",
  1856. adapter->nreadblocks[i], adapter->nwriteblocks[i]);
  1857. len += sprintf(page+len,
  1858. "\tRead errors = %lu, Write errors = %lu\n\n",
  1859. adapter->rd_errors[i], adapter->wr_errors[i]);
  1860. }
  1861. #else
  1862. len += sprintf(page+len,
  1863. "IO and error counters not compiled in driver.\n");
  1864. #endif
  1865. *eof = 1;
  1866. return len;
  1867. }
  1868. /**
  1869. * proc_read_mbox()
  1870. * @page - buffer to write the data in
  1871. * @start - where the actual data has been written in page
  1872. * @offset - same meaning as the read system call
  1873. * @count - same meaning as the read system call
  1874. * @eof - set if no more data needs to be returned
  1875. * @data - pointer to our soft state
  1876. *
  1877. * Display mailbox information for the last command issued. This information
  1878. * is good for debugging.
  1879. */
  1880. static int
  1881. proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
  1882. void *data)
  1883. {
  1884. adapter_t *adapter = (adapter_t *)data;
  1885. volatile mbox_t *mbox = adapter->mbox;
  1886. int len = 0;
  1887. len = sprintf(page, "Contents of Mail Box Structure\n");
  1888. len += sprintf(page+len, " Fw Command = 0x%02x\n",
  1889. mbox->m_out.cmd);
  1890. len += sprintf(page+len, " Cmd Sequence = 0x%02x\n",
  1891. mbox->m_out.cmdid);
  1892. len += sprintf(page+len, " No of Sectors= %04d\n",
  1893. mbox->m_out.numsectors);
  1894. len += sprintf(page+len, " LBA = 0x%02x\n",
  1895. mbox->m_out.lba);
  1896. len += sprintf(page+len, " DTA = 0x%08x\n",
  1897. mbox->m_out.xferaddr);
  1898. len += sprintf(page+len, " Logical Drive= 0x%02x\n",
  1899. mbox->m_out.logdrv);
  1900. len += sprintf(page+len, " No of SG Elmt= 0x%02x\n",
  1901. mbox->m_out.numsgelements);
  1902. len += sprintf(page+len, " Busy = %01x\n",
  1903. mbox->m_in.busy);
  1904. len += sprintf(page+len, " Status = 0x%02x\n",
  1905. mbox->m_in.status);
  1906. *eof = 1;
  1907. return len;
  1908. }
  1909. /**
  1910. * proc_rebuild_rate()
  1911. * @page - buffer to write the data in
  1912. * @start - where the actual data has been written in page
  1913. * @offset - same meaning as the read system call
  1914. * @count - same meaning as the read system call
  1915. * @eof - set if no more data needs to be returned
  1916. * @data - pointer to our soft state
  1917. *
  1918. * Display current rebuild rate
  1919. */
  1920. static int
  1921. proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
  1922. void *data)
  1923. {
  1924. adapter_t *adapter = (adapter_t *)data;
  1925. dma_addr_t dma_handle;
  1926. caddr_t inquiry;
  1927. struct pci_dev *pdev;
  1928. int len = 0;
  1929. if( make_local_pdev(adapter, &pdev) != 0 ) {
  1930. *eof = 1;
  1931. return len;
  1932. }
  1933. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  1934. free_local_pdev(pdev);
  1935. *eof = 1;
  1936. return len;
  1937. }
  1938. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  1939. len = sprintf(page, "Adapter inquiry failed.\n");
  1940. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  1941. mega_free_inquiry(inquiry, dma_handle, pdev);
  1942. free_local_pdev(pdev);
  1943. *eof = 1;
  1944. return len;
  1945. }
  1946. if( adapter->flag & BOARD_40LD ) {
  1947. len = sprintf(page, "Rebuild Rate: [%d%%]\n",
  1948. ((mega_inquiry3 *)inquiry)->rebuild_rate);
  1949. }
  1950. else {
  1951. len = sprintf(page, "Rebuild Rate: [%d%%]\n",
  1952. ((mraid_ext_inquiry *)
  1953. inquiry)->raid_inq.adapter_info.rebuild_rate);
  1954. }
  1955. mega_free_inquiry(inquiry, dma_handle, pdev);
  1956. free_local_pdev(pdev);
  1957. *eof = 1;
  1958. return len;
  1959. }
  1960. /**
  1961. * proc_battery()
  1962. * @page - buffer to write the data in
  1963. * @start - where the actual data has been written in page
  1964. * @offset - same meaning as the read system call
  1965. * @count - same meaning as the read system call
  1966. * @eof - set if no more data needs to be returned
  1967. * @data - pointer to our soft state
  1968. *
  1969. * Display information about the battery module on the controller.
  1970. */
  1971. static int
  1972. proc_battery(char *page, char **start, off_t offset, int count, int *eof,
  1973. void *data)
  1974. {
  1975. adapter_t *adapter = (adapter_t *)data;
  1976. dma_addr_t dma_handle;
  1977. caddr_t inquiry;
  1978. struct pci_dev *pdev;
  1979. u8 battery_status = 0;
  1980. char str[256];
  1981. int len = 0;
  1982. if( make_local_pdev(adapter, &pdev) != 0 ) {
  1983. *eof = 1;
  1984. return len;
  1985. }
  1986. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  1987. free_local_pdev(pdev);
  1988. *eof = 1;
  1989. return len;
  1990. }
  1991. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  1992. len = sprintf(page, "Adapter inquiry failed.\n");
  1993. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  1994. mega_free_inquiry(inquiry, dma_handle, pdev);
  1995. free_local_pdev(pdev);
  1996. *eof = 1;
  1997. return len;
  1998. }
  1999. if( adapter->flag & BOARD_40LD ) {
  2000. battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
  2001. }
  2002. else {
  2003. battery_status = ((mraid_ext_inquiry *)inquiry)->
  2004. raid_inq.adapter_info.battery_status;
  2005. }
  2006. /*
  2007. * Decode the battery status
  2008. */
  2009. sprintf(str, "Battery Status:[%d]", battery_status);
  2010. if(battery_status == MEGA_BATT_CHARGE_DONE)
  2011. strcat(str, " Charge Done");
  2012. if(battery_status & MEGA_BATT_MODULE_MISSING)
  2013. strcat(str, " Module Missing");
  2014. if(battery_status & MEGA_BATT_LOW_VOLTAGE)
  2015. strcat(str, " Low Voltage");
  2016. if(battery_status & MEGA_BATT_TEMP_HIGH)
  2017. strcat(str, " Temperature High");
  2018. if(battery_status & MEGA_BATT_PACK_MISSING)
  2019. strcat(str, " Pack Missing");
  2020. if(battery_status & MEGA_BATT_CHARGE_INPROG)
  2021. strcat(str, " Charge In-progress");
  2022. if(battery_status & MEGA_BATT_CHARGE_FAIL)
  2023. strcat(str, " Charge Fail");
  2024. if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
  2025. strcat(str, " Cycles Exceeded");
  2026. len = sprintf(page, "%s\n", str);
  2027. mega_free_inquiry(inquiry, dma_handle, pdev);
  2028. free_local_pdev(pdev);
  2029. *eof = 1;
  2030. return len;
  2031. }
  2032. /**
  2033. * proc_pdrv_ch0()
  2034. * @page - buffer to write the data in
  2035. * @start - where the actual data has been written in page
  2036. * @offset - same meaning as the read system call
  2037. * @count - same meaning as the read system call
  2038. * @eof - set if no more data needs to be returned
  2039. * @data - pointer to our soft state
  2040. *
  2041. * Display information about the physical drives on physical channel 0.
  2042. */
  2043. static int
  2044. proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
  2045. void *data)
  2046. {
  2047. adapter_t *adapter = (adapter_t *)data;
  2048. *eof = 1;
  2049. return (proc_pdrv(adapter, page, 0));
  2050. }
  2051. /**
  2052. * proc_pdrv_ch1()
  2053. * @page - buffer to write the data in
  2054. * @start - where the actual data has been written in page
  2055. * @offset - same meaning as the read system call
  2056. * @count - same meaning as the read system call
  2057. * @eof - set if no more data needs to be returned
  2058. * @data - pointer to our soft state
  2059. *
  2060. * Display information about the physical drives on physical channel 1.
  2061. */
  2062. static int
  2063. proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
  2064. void *data)
  2065. {
  2066. adapter_t *adapter = (adapter_t *)data;
  2067. *eof = 1;
  2068. return (proc_pdrv(adapter, page, 1));
  2069. }
  2070. /**
  2071. * proc_pdrv_ch2()
  2072. * @page - buffer to write the data in
  2073. * @start - where the actual data has been written in page
  2074. * @offset - same meaning as the read system call
  2075. * @count - same meaning as the read system call
  2076. * @eof - set if no more data needs to be returned
  2077. * @data - pointer to our soft state
  2078. *
  2079. * Display information about the physical drives on physical channel 2.
  2080. */
  2081. static int
  2082. proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
  2083. void *data)
  2084. {
  2085. adapter_t *adapter = (adapter_t *)data;
  2086. *eof = 1;
  2087. return (proc_pdrv(adapter, page, 2));
  2088. }
  2089. /**
  2090. * proc_pdrv_ch3()
  2091. * @page - buffer to write the data in
  2092. * @start - where the actual data has been written in page
  2093. * @offset - same meaning as the read system call
  2094. * @count - same meaning as the read system call
  2095. * @eof - set if no more data needs to be returned
  2096. * @data - pointer to our soft state
  2097. *
  2098. * Display information about the physical drives on physical channel 3.
  2099. */
  2100. static int
  2101. proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
  2102. void *data)
  2103. {
  2104. adapter_t *adapter = (adapter_t *)data;
  2105. *eof = 1;
  2106. return (proc_pdrv(adapter, page, 3));
  2107. }
  2108. /**
  2109. * proc_pdrv()
  2110. * @page - buffer to write the data in
  2111. * @adapter - pointer to our soft state
  2112. *
  2113. * Display information about the physical drives.
  2114. */
  2115. static int
  2116. proc_pdrv(adapter_t *adapter, char *page, int channel)
  2117. {
  2118. dma_addr_t dma_handle;
  2119. char *scsi_inq;
  2120. dma_addr_t scsi_inq_dma_handle;
  2121. caddr_t inquiry;
  2122. struct pci_dev *pdev;
  2123. u8 *pdrv_state;
  2124. u8 state;
  2125. int tgt;
  2126. int max_channels;
  2127. int len = 0;
  2128. char str[80];
  2129. int i;
  2130. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2131. return len;
  2132. }
  2133. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2134. goto free_pdev;
  2135. }
  2136. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2137. len = sprintf(page, "Adapter inquiry failed.\n");
  2138. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2139. goto free_inquiry;
  2140. }
  2141. scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
  2142. if( scsi_inq == NULL ) {
  2143. len = sprintf(page, "memory not available for scsi inq.\n");
  2144. goto free_inquiry;
  2145. }
  2146. if( adapter->flag & BOARD_40LD ) {
  2147. pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
  2148. }
  2149. else {
  2150. pdrv_state = ((mraid_ext_inquiry *)inquiry)->
  2151. raid_inq.pdrv_info.pdrv_state;
  2152. }
  2153. max_channels = adapter->product_info.nchannels;
  2154. if( channel >= max_channels ) {
  2155. goto free_pci;
  2156. }
  2157. for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
  2158. i = channel*16 + tgt;
  2159. state = *(pdrv_state + i);
  2160. switch( state & 0x0F ) {
  2161. case PDRV_ONLINE:
  2162. sprintf(str,
  2163. "Channel:%2d Id:%2d State: Online",
  2164. channel, tgt);
  2165. break;
  2166. case PDRV_FAILED:
  2167. sprintf(str,
  2168. "Channel:%2d Id:%2d State: Failed",
  2169. channel, tgt);
  2170. break;
  2171. case PDRV_RBLD:
  2172. sprintf(str,
  2173. "Channel:%2d Id:%2d State: Rebuild",
  2174. channel, tgt);
  2175. break;
  2176. case PDRV_HOTSPARE:
  2177. sprintf(str,
  2178. "Channel:%2d Id:%2d State: Hot spare",
  2179. channel, tgt);
  2180. break;
  2181. default:
  2182. sprintf(str,
  2183. "Channel:%2d Id:%2d State: Un-configured",
  2184. channel, tgt);
  2185. break;
  2186. }
  2187. /*
  2188. * This interface displays inquiries for disk drives
  2189. * only. Inquries for logical drives and non-disk
  2190. * devices are available through /proc/scsi/scsi
  2191. */
  2192. memset(scsi_inq, 0, 256);
  2193. if( mega_internal_dev_inquiry(adapter, channel, tgt,
  2194. scsi_inq_dma_handle) ||
  2195. (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
  2196. continue;
  2197. }
  2198. /*
  2199. * Check for overflow. We print less than 240
  2200. * characters for inquiry
  2201. */
  2202. if( (len + 240) >= PAGE_SIZE ) break;
  2203. len += sprintf(page+len, "%s.\n", str);
  2204. len += mega_print_inquiry(page+len, scsi_inq);
  2205. }
  2206. free_pci:
  2207. pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
  2208. free_inquiry:
  2209. mega_free_inquiry(inquiry, dma_handle, pdev);
  2210. free_pdev:
  2211. free_local_pdev(pdev);
  2212. return len;
  2213. }
  2214. /*
  2215. * Display scsi inquiry
  2216. */
  2217. static int
  2218. mega_print_inquiry(char *page, char *scsi_inq)
  2219. {
  2220. int len = 0;
  2221. int i;
  2222. len = sprintf(page, " Vendor: ");
  2223. for( i = 8; i < 16; i++ ) {
  2224. len += sprintf(page+len, "%c", scsi_inq[i]);
  2225. }
  2226. len += sprintf(page+len, " Model: ");
  2227. for( i = 16; i < 32; i++ ) {
  2228. len += sprintf(page+len, "%c", scsi_inq[i]);
  2229. }
  2230. len += sprintf(page+len, " Rev: ");
  2231. for( i = 32; i < 36; i++ ) {
  2232. len += sprintf(page+len, "%c", scsi_inq[i]);
  2233. }
  2234. len += sprintf(page+len, "\n");
  2235. i = scsi_inq[0] & 0x1f;
  2236. len += sprintf(page+len, " Type: %s ",
  2237. i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
  2238. "Unknown ");
  2239. len += sprintf(page+len,
  2240. " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
  2241. if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
  2242. len += sprintf(page+len, " CCS\n");
  2243. else
  2244. len += sprintf(page+len, "\n");
  2245. return len;
  2246. }
  2247. /**
  2248. * proc_rdrv_10()
  2249. * @page - buffer to write the data in
  2250. * @start - where the actual data has been written in page
  2251. * @offset - same meaning as the read system call
  2252. * @count - same meaning as the read system call
  2253. * @eof - set if no more data needs to be returned
  2254. * @data - pointer to our soft state
  2255. *
  2256. * Display real time information about the logical drives 0 through 9.
  2257. */
  2258. static int
  2259. proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
  2260. void *data)
  2261. {
  2262. adapter_t *adapter = (adapter_t *)data;
  2263. *eof = 1;
  2264. return (proc_rdrv(adapter, page, 0, 9));
  2265. }
  2266. /**
  2267. * proc_rdrv_20()
  2268. * @page - buffer to write the data in
  2269. * @start - where the actual data has been written in page
  2270. * @offset - same meaning as the read system call
  2271. * @count - same meaning as the read system call
  2272. * @eof - set if no more data needs to be returned
  2273. * @data - pointer to our soft state
  2274. *
  2275. * Display real time information about the logical drives 0 through 9.
  2276. */
  2277. static int
  2278. proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
  2279. void *data)
  2280. {
  2281. adapter_t *adapter = (adapter_t *)data;
  2282. *eof = 1;
  2283. return (proc_rdrv(adapter, page, 10, 19));
  2284. }
  2285. /**
  2286. * proc_rdrv_30()
  2287. * @page - buffer to write the data in
  2288. * @start - where the actual data has been written in page
  2289. * @offset - same meaning as the read system call
  2290. * @count - same meaning as the read system call
  2291. * @eof - set if no more data needs to be returned
  2292. * @data - pointer to our soft state
  2293. *
  2294. * Display real time information about the logical drives 0 through 9.
  2295. */
  2296. static int
  2297. proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
  2298. void *data)
  2299. {
  2300. adapter_t *adapter = (adapter_t *)data;
  2301. *eof = 1;
  2302. return (proc_rdrv(adapter, page, 20, 29));
  2303. }
  2304. /**
  2305. * proc_rdrv_40()
  2306. * @page - buffer to write the data in
  2307. * @start - where the actual data has been written in page
  2308. * @offset - same meaning as the read system call
  2309. * @count - same meaning as the read system call
  2310. * @eof - set if no more data needs to be returned
  2311. * @data - pointer to our soft state
  2312. *
  2313. * Display real time information about the logical drives 0 through 9.
  2314. */
  2315. static int
  2316. proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
  2317. void *data)
  2318. {
  2319. adapter_t *adapter = (adapter_t *)data;
  2320. *eof = 1;
  2321. return (proc_rdrv(adapter, page, 30, 39));
  2322. }
  2323. /**
  2324. * proc_rdrv()
  2325. * @page - buffer to write the data in
  2326. * @adapter - pointer to our soft state
  2327. * @start - starting logical drive to display
  2328. * @end - ending logical drive to display
  2329. *
  2330. * We do not print the inquiry information since its already available through
  2331. * /proc/scsi/scsi interface
  2332. */
  2333. static int
  2334. proc_rdrv(adapter_t *adapter, char *page, int start, int end )
  2335. {
  2336. dma_addr_t dma_handle;
  2337. logdrv_param *lparam;
  2338. megacmd_t mc;
  2339. char *disk_array;
  2340. dma_addr_t disk_array_dma_handle;
  2341. caddr_t inquiry;
  2342. struct pci_dev *pdev;
  2343. u8 *rdrv_state;
  2344. int num_ldrv;
  2345. u32 array_sz;
  2346. int len = 0;
  2347. int i;
  2348. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2349. return len;
  2350. }
  2351. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2352. free_local_pdev(pdev);
  2353. return len;
  2354. }
  2355. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2356. len = sprintf(page, "Adapter inquiry failed.\n");
  2357. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2358. mega_free_inquiry(inquiry, dma_handle, pdev);
  2359. free_local_pdev(pdev);
  2360. return len;
  2361. }
  2362. memset(&mc, 0, sizeof(megacmd_t));
  2363. if( adapter->flag & BOARD_40LD ) {
  2364. array_sz = sizeof(disk_array_40ld);
  2365. rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
  2366. num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
  2367. }
  2368. else {
  2369. array_sz = sizeof(disk_array_8ld);
  2370. rdrv_state = ((mraid_ext_inquiry *)inquiry)->
  2371. raid_inq.logdrv_info.ldrv_state;
  2372. num_ldrv = ((mraid_ext_inquiry *)inquiry)->
  2373. raid_inq.logdrv_info.num_ldrv;
  2374. }
  2375. disk_array = pci_alloc_consistent(pdev, array_sz,
  2376. &disk_array_dma_handle);
  2377. if( disk_array == NULL ) {
  2378. len = sprintf(page, "memory not available.\n");
  2379. mega_free_inquiry(inquiry, dma_handle, pdev);
  2380. free_local_pdev(pdev);
  2381. return len;
  2382. }
  2383. mc.xferaddr = (u32)disk_array_dma_handle;
  2384. if( adapter->flag & BOARD_40LD ) {
  2385. mc.cmd = FC_NEW_CONFIG;
  2386. mc.opcode = OP_DCMD_READ_CONFIG;
  2387. if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) ) {
  2388. len = sprintf(page, "40LD read config failed.\n");
  2389. mega_free_inquiry(inquiry, dma_handle, pdev);
  2390. pci_free_consistent(pdev, array_sz, disk_array,
  2391. disk_array_dma_handle);
  2392. free_local_pdev(pdev);
  2393. return len;
  2394. }
  2395. }
  2396. else {
  2397. mc.cmd = NEW_READ_CONFIG_8LD;
  2398. if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) ) {
  2399. mc.cmd = READ_CONFIG_8LD;
  2400. if( mega_internal_command(adapter, LOCK_INT, &mc,
  2401. NULL) ){
  2402. len = sprintf(page,
  2403. "8LD read config failed.\n");
  2404. mega_free_inquiry(inquiry, dma_handle, pdev);
  2405. pci_free_consistent(pdev, array_sz,
  2406. disk_array,
  2407. disk_array_dma_handle);
  2408. free_local_pdev(pdev);
  2409. return len;
  2410. }
  2411. }
  2412. }
  2413. for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
  2414. if( adapter->flag & BOARD_40LD ) {
  2415. lparam =
  2416. &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
  2417. }
  2418. else {
  2419. lparam =
  2420. &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
  2421. }
  2422. /*
  2423. * Check for overflow. We print less than 240 characters for
  2424. * information about each logical drive.
  2425. */
  2426. if( (len + 240) >= PAGE_SIZE ) break;
  2427. len += sprintf(page+len, "Logical drive:%2d:, ", i);
  2428. switch( rdrv_state[i] & 0x0F ) {
  2429. case RDRV_OFFLINE:
  2430. len += sprintf(page+len, "state: offline");
  2431. break;
  2432. case RDRV_DEGRADED:
  2433. len += sprintf(page+len, "state: degraded");
  2434. break;
  2435. case RDRV_OPTIMAL:
  2436. len += sprintf(page+len, "state: optimal");
  2437. break;
  2438. case RDRV_DELETED:
  2439. len += sprintf(page+len, "state: deleted");
  2440. break;
  2441. default:
  2442. len += sprintf(page+len, "state: unknown");
  2443. break;
  2444. }
  2445. /*
  2446. * Check if check consistency or initialization is going on
  2447. * for this logical drive.
  2448. */
  2449. if( (rdrv_state[i] & 0xF0) == 0x20 ) {
  2450. len += sprintf(page+len,
  2451. ", check-consistency in progress");
  2452. }
  2453. else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
  2454. len += sprintf(page+len,
  2455. ", initialization in progress");
  2456. }
  2457. len += sprintf(page+len, "\n");
  2458. len += sprintf(page+len, "Span depth:%3d, ",
  2459. lparam->span_depth);
  2460. len += sprintf(page+len, "RAID level:%3d, ",
  2461. lparam->level);
  2462. len += sprintf(page+len, "Stripe size:%3d, ",
  2463. lparam->stripe_sz ? lparam->stripe_sz/2: 128);
  2464. len += sprintf(page+len, "Row size:%3d\n",
  2465. lparam->row_size);
  2466. len += sprintf(page+len, "Read Policy: ");
  2467. switch(lparam->read_ahead) {
  2468. case NO_READ_AHEAD:
  2469. len += sprintf(page+len, "No read ahead, ");
  2470. break;
  2471. case READ_AHEAD:
  2472. len += sprintf(page+len, "Read ahead, ");
  2473. break;
  2474. case ADAP_READ_AHEAD:
  2475. len += sprintf(page+len, "Adaptive, ");
  2476. break;
  2477. }
  2478. len += sprintf(page+len, "Write Policy: ");
  2479. switch(lparam->write_mode) {
  2480. case WRMODE_WRITE_THRU:
  2481. len += sprintf(page+len, "Write thru, ");
  2482. break;
  2483. case WRMODE_WRITE_BACK:
  2484. len += sprintf(page+len, "Write back, ");
  2485. break;
  2486. }
  2487. len += sprintf(page+len, "Cache Policy: ");
  2488. switch(lparam->direct_io) {
  2489. case CACHED_IO:
  2490. len += sprintf(page+len, "Cached IO\n\n");
  2491. break;
  2492. case DIRECT_IO:
  2493. len += sprintf(page+len, "Direct IO\n\n");
  2494. break;
  2495. }
  2496. }
  2497. mega_free_inquiry(inquiry, dma_handle, pdev);
  2498. pci_free_consistent(pdev, array_sz, disk_array,
  2499. disk_array_dma_handle);
  2500. free_local_pdev(pdev);
  2501. return len;
  2502. }
  2503. #endif
  2504. /**
  2505. * megaraid_biosparam()
  2506. *
  2507. * Return the disk geometry for a particular disk
  2508. */
  2509. static int
  2510. megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
  2511. sector_t capacity, int geom[])
  2512. {
  2513. adapter_t *adapter;
  2514. unsigned char *bh;
  2515. int heads;
  2516. int sectors;
  2517. int cylinders;
  2518. int rval;
  2519. /* Get pointer to host config structure */
  2520. adapter = (adapter_t *)sdev->host->hostdata;
  2521. if (IS_RAID_CH(adapter, sdev->channel)) {
  2522. /* Default heads (64) & sectors (32) */
  2523. heads = 64;
  2524. sectors = 32;
  2525. cylinders = (ulong)capacity / (heads * sectors);
  2526. /*
  2527. * Handle extended translation size for logical drives
  2528. * > 1Gb
  2529. */
  2530. if ((ulong)capacity >= 0x200000) {
  2531. heads = 255;
  2532. sectors = 63;
  2533. cylinders = (ulong)capacity / (heads * sectors);
  2534. }
  2535. /* return result */
  2536. geom[0] = heads;
  2537. geom[1] = sectors;
  2538. geom[2] = cylinders;
  2539. }
  2540. else {
  2541. bh = scsi_bios_ptable(bdev);
  2542. if( bh ) {
  2543. rval = scsi_partsize(bh, capacity,
  2544. &geom[2], &geom[0], &geom[1]);
  2545. kfree(bh);
  2546. if( rval != -1 )
  2547. return rval;
  2548. }
  2549. printk(KERN_INFO
  2550. "megaraid: invalid partition on this disk on channel %d\n",
  2551. sdev->channel);
  2552. /* Default heads (64) & sectors (32) */
  2553. heads = 64;
  2554. sectors = 32;
  2555. cylinders = (ulong)capacity / (heads * sectors);
  2556. /* Handle extended translation size for logical drives > 1Gb */
  2557. if ((ulong)capacity >= 0x200000) {
  2558. heads = 255;
  2559. sectors = 63;
  2560. cylinders = (ulong)capacity / (heads * sectors);
  2561. }
  2562. /* return result */
  2563. geom[0] = heads;
  2564. geom[1] = sectors;
  2565. geom[2] = cylinders;
  2566. }
  2567. return 0;
  2568. }
  2569. /**
  2570. * mega_init_scb()
  2571. * @adapter - pointer to our soft state
  2572. *
  2573. * Allocate memory for the various pointers in the scb structures:
  2574. * scatter-gather list pointer, passthru and extended passthru structure
  2575. * pointers.
  2576. */
  2577. static int
  2578. mega_init_scb(adapter_t *adapter)
  2579. {
  2580. scb_t *scb;
  2581. int i;
  2582. for( i = 0; i < adapter->max_cmds; i++ ) {
  2583. scb = &adapter->scb_list[i];
  2584. scb->sgl64 = NULL;
  2585. scb->sgl = NULL;
  2586. scb->pthru = NULL;
  2587. scb->epthru = NULL;
  2588. }
  2589. for( i = 0; i < adapter->max_cmds; i++ ) {
  2590. scb = &adapter->scb_list[i];
  2591. scb->idx = i;
  2592. scb->sgl64 = pci_alloc_consistent(adapter->dev,
  2593. sizeof(mega_sgl64) * adapter->sglen,
  2594. &scb->sgl_dma_addr);
  2595. scb->sgl = (mega_sglist *)scb->sgl64;
  2596. if( !scb->sgl ) {
  2597. printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
  2598. mega_free_sgl(adapter);
  2599. return -1;
  2600. }
  2601. scb->pthru = pci_alloc_consistent(adapter->dev,
  2602. sizeof(mega_passthru),
  2603. &scb->pthru_dma_addr);
  2604. if( !scb->pthru ) {
  2605. printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
  2606. mega_free_sgl(adapter);
  2607. return -1;
  2608. }
  2609. scb->epthru = pci_alloc_consistent(adapter->dev,
  2610. sizeof(mega_ext_passthru),
  2611. &scb->epthru_dma_addr);
  2612. if( !scb->epthru ) {
  2613. printk(KERN_WARNING
  2614. "Can't allocate extended passthru.\n");
  2615. mega_free_sgl(adapter);
  2616. return -1;
  2617. }
  2618. scb->dma_type = MEGA_DMA_TYPE_NONE;
  2619. /*
  2620. * Link to free list
  2621. * lock not required since we are loading the driver, so no
  2622. * commands possible right now.
  2623. */
  2624. scb->state = SCB_FREE;
  2625. scb->cmd = NULL;
  2626. list_add(&scb->list, &adapter->free_list);
  2627. }
  2628. return 0;
  2629. }
  2630. /**
  2631. * megadev_open()
  2632. * @inode - unused
  2633. * @filep - unused
  2634. *
  2635. * Routines for the character/ioctl interface to the driver. Find out if this
  2636. * is a valid open. If yes, increment the module use count so that it cannot
  2637. * be unloaded.
  2638. */
  2639. static int
  2640. megadev_open (struct inode *inode, struct file *filep)
  2641. {
  2642. /*
  2643. * Only allow superuser to access private ioctl interface
  2644. */
  2645. if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
  2646. return 0;
  2647. }
  2648. /**
  2649. * megadev_ioctl()
  2650. * @inode - Our device inode
  2651. * @filep - unused
  2652. * @cmd - ioctl command
  2653. * @arg - user buffer
  2654. *
  2655. * ioctl entry point for our private ioctl interface. We move the data in from
  2656. * the user space, prepare the command (if necessary, convert the old MIMD
  2657. * ioctl to new ioctl command), and issue a synchronous command to the
  2658. * controller.
  2659. */
  2660. static int
  2661. megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
  2662. unsigned long arg)
  2663. {
  2664. adapter_t *adapter;
  2665. nitioctl_t uioc;
  2666. int adapno;
  2667. int rval;
  2668. mega_passthru __user *upthru; /* user address for passthru */
  2669. mega_passthru *pthru; /* copy user passthru here */
  2670. dma_addr_t pthru_dma_hndl;
  2671. void *data = NULL; /* data to be transferred */
  2672. dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
  2673. megacmd_t mc;
  2674. megastat_t __user *ustats;
  2675. int num_ldrv;
  2676. u32 uxferaddr = 0;
  2677. struct pci_dev *pdev;
  2678. ustats = NULL; /* avoid compilation warnings */
  2679. num_ldrv = 0;
  2680. /*
  2681. * Make sure only USCSICMD are issued through this interface.
  2682. * MIMD application would still fire different command.
  2683. */
  2684. if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
  2685. return -EINVAL;
  2686. }
  2687. /*
  2688. * Check and convert a possible MIMD command to NIT command.
  2689. * mega_m_to_n() copies the data from the user space, so we do not
  2690. * have to do it here.
  2691. * NOTE: We will need some user address to copyout the data, therefore
  2692. * the inteface layer will also provide us with the required user
  2693. * addresses.
  2694. */
  2695. memset(&uioc, 0, sizeof(nitioctl_t));
  2696. if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
  2697. return rval;
  2698. switch( uioc.opcode ) {
  2699. case GET_DRIVER_VER:
  2700. if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
  2701. return (-EFAULT);
  2702. break;
  2703. case GET_N_ADAP:
  2704. if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
  2705. return (-EFAULT);
  2706. /*
  2707. * Shucks. MIMD interface returns a positive value for number
  2708. * of adapters. TODO: Change it to return 0 when there is no
  2709. * applicatio using mimd interface.
  2710. */
  2711. return hba_count;
  2712. case GET_ADAP_INFO:
  2713. /*
  2714. * Which adapter
  2715. */
  2716. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2717. return (-ENODEV);
  2718. if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
  2719. sizeof(struct mcontroller)) )
  2720. return (-EFAULT);
  2721. break;
  2722. #if MEGA_HAVE_STATS
  2723. case GET_STATS:
  2724. /*
  2725. * Which adapter
  2726. */
  2727. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2728. return (-ENODEV);
  2729. adapter = hba_soft_state[adapno];
  2730. ustats = uioc.uioc_uaddr;
  2731. if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
  2732. return (-EFAULT);
  2733. /*
  2734. * Check for the validity of the logical drive number
  2735. */
  2736. if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
  2737. if( copy_to_user(ustats->nreads, adapter->nreads,
  2738. num_ldrv*sizeof(u32)) )
  2739. return -EFAULT;
  2740. if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
  2741. num_ldrv*sizeof(u32)) )
  2742. return -EFAULT;
  2743. if( copy_to_user(ustats->nwrites, adapter->nwrites,
  2744. num_ldrv*sizeof(u32)) )
  2745. return -EFAULT;
  2746. if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
  2747. num_ldrv*sizeof(u32)) )
  2748. return -EFAULT;
  2749. if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
  2750. num_ldrv*sizeof(u32)) )
  2751. return -EFAULT;
  2752. if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
  2753. num_ldrv*sizeof(u32)) )
  2754. return -EFAULT;
  2755. return 0;
  2756. #endif
  2757. case MBOX_CMD:
  2758. /*
  2759. * Which adapter
  2760. */
  2761. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2762. return (-ENODEV);
  2763. adapter = hba_soft_state[adapno];
  2764. /*
  2765. * Deletion of logical drive is a special case. The adapter
  2766. * should be quiescent before this command is issued.
  2767. */
  2768. if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
  2769. uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
  2770. /*
  2771. * Do we support this feature
  2772. */
  2773. if( !adapter->support_random_del ) {
  2774. printk(KERN_WARNING "megaraid: logdrv ");
  2775. printk("delete on non-supporting F/W.\n");
  2776. return (-EINVAL);
  2777. }
  2778. rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
  2779. if( rval == 0 ) {
  2780. memset(&mc, 0, sizeof(megacmd_t));
  2781. mc.status = rval;
  2782. rval = mega_n_to_m((void __user *)arg, &mc);
  2783. }
  2784. return rval;
  2785. }
  2786. /*
  2787. * This interface only support the regular passthru commands.
  2788. * Reject extended passthru and 64-bit passthru
  2789. */
  2790. if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
  2791. uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
  2792. printk(KERN_WARNING "megaraid: rejected passthru.\n");
  2793. return (-EINVAL);
  2794. }
  2795. /*
  2796. * For all internal commands, the buffer must be allocated in
  2797. * <4GB address range
  2798. */
  2799. if( make_local_pdev(adapter, &pdev) != 0 )
  2800. return -EIO;
  2801. /* Is it a passthru command or a DCMD */
  2802. if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
  2803. /* Passthru commands */
  2804. pthru = pci_alloc_consistent(pdev,
  2805. sizeof(mega_passthru),
  2806. &pthru_dma_hndl);
  2807. if( pthru == NULL ) {
  2808. free_local_pdev(pdev);
  2809. return (-ENOMEM);
  2810. }
  2811. /*
  2812. * The user passthru structure
  2813. */
  2814. upthru = (mega_passthru __user *)MBOX(uioc)->xferaddr;
  2815. /*
  2816. * Copy in the user passthru here.
  2817. */
  2818. if( copy_from_user(pthru, upthru,
  2819. sizeof(mega_passthru)) ) {
  2820. pci_free_consistent(pdev,
  2821. sizeof(mega_passthru), pthru,
  2822. pthru_dma_hndl);
  2823. free_local_pdev(pdev);
  2824. return (-EFAULT);
  2825. }
  2826. /*
  2827. * Is there a data transfer
  2828. */
  2829. if( pthru->dataxferlen ) {
  2830. data = pci_alloc_consistent(pdev,
  2831. pthru->dataxferlen,
  2832. &data_dma_hndl);
  2833. if( data == NULL ) {
  2834. pci_free_consistent(pdev,
  2835. sizeof(mega_passthru),
  2836. pthru,
  2837. pthru_dma_hndl);
  2838. free_local_pdev(pdev);
  2839. return (-ENOMEM);
  2840. }
  2841. /*
  2842. * Save the user address and point the kernel
  2843. * address at just allocated memory
  2844. */
  2845. uxferaddr = pthru->dataxferaddr;
  2846. pthru->dataxferaddr = data_dma_hndl;
  2847. }
  2848. /*
  2849. * Is data coming down-stream
  2850. */
  2851. if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
  2852. /*
  2853. * Get the user data
  2854. */
  2855. if( copy_from_user(data, (char __user *)uxferaddr,
  2856. pthru->dataxferlen) ) {
  2857. rval = (-EFAULT);
  2858. goto freemem_and_return;
  2859. }
  2860. }
  2861. memset(&mc, 0, sizeof(megacmd_t));
  2862. mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  2863. mc.xferaddr = (u32)pthru_dma_hndl;
  2864. /*
  2865. * Issue the command
  2866. */
  2867. mega_internal_command(adapter, LOCK_INT, &mc, pthru);
  2868. rval = mega_n_to_m((void __user *)arg, &mc);
  2869. if( rval ) goto freemem_and_return;
  2870. /*
  2871. * Is data going up-stream
  2872. */
  2873. if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
  2874. if( copy_to_user((char __user *)uxferaddr, data,
  2875. pthru->dataxferlen) ) {
  2876. rval = (-EFAULT);
  2877. }
  2878. }
  2879. /*
  2880. * Send the request sense data also, irrespective of
  2881. * whether the user has asked for it or not.
  2882. */
  2883. copy_to_user(upthru->reqsensearea,
  2884. pthru->reqsensearea, 14);
  2885. freemem_and_return:
  2886. if( pthru->dataxferlen ) {
  2887. pci_free_consistent(pdev,
  2888. pthru->dataxferlen, data,
  2889. data_dma_hndl);
  2890. }
  2891. pci_free_consistent(pdev, sizeof(mega_passthru),
  2892. pthru, pthru_dma_hndl);
  2893. free_local_pdev(pdev);
  2894. return rval;
  2895. }
  2896. else {
  2897. /* DCMD commands */
  2898. /*
  2899. * Is there a data transfer
  2900. */
  2901. if( uioc.xferlen ) {
  2902. data = pci_alloc_consistent(pdev,
  2903. uioc.xferlen, &data_dma_hndl);
  2904. if( data == NULL ) {
  2905. free_local_pdev(pdev);
  2906. return (-ENOMEM);
  2907. }
  2908. uxferaddr = MBOX(uioc)->xferaddr;
  2909. }
  2910. /*
  2911. * Is data coming down-stream
  2912. */
  2913. if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
  2914. /*
  2915. * Get the user data
  2916. */
  2917. if( copy_from_user(data, (char __user *)uxferaddr,
  2918. uioc.xferlen) ) {
  2919. pci_free_consistent(pdev,
  2920. uioc.xferlen,
  2921. data, data_dma_hndl);
  2922. free_local_pdev(pdev);
  2923. return (-EFAULT);
  2924. }
  2925. }
  2926. memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
  2927. mc.xferaddr = (u32)data_dma_hndl;
  2928. /*
  2929. * Issue the command
  2930. */
  2931. mega_internal_command(adapter, LOCK_INT, &mc, NULL);
  2932. rval = mega_n_to_m((void __user *)arg, &mc);
  2933. if( rval ) {
  2934. if( uioc.xferlen ) {
  2935. pci_free_consistent(pdev,
  2936. uioc.xferlen, data,
  2937. data_dma_hndl);
  2938. }
  2939. free_local_pdev(pdev);
  2940. return rval;
  2941. }
  2942. /*
  2943. * Is data going up-stream
  2944. */
  2945. if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
  2946. if( copy_to_user((char __user *)uxferaddr, data,
  2947. uioc.xferlen) ) {
  2948. rval = (-EFAULT);
  2949. }
  2950. }
  2951. if( uioc.xferlen ) {
  2952. pci_free_consistent(pdev,
  2953. uioc.xferlen, data,
  2954. data_dma_hndl);
  2955. }
  2956. free_local_pdev(pdev);
  2957. return rval;
  2958. }
  2959. default:
  2960. return (-EINVAL);
  2961. }
  2962. return 0;
  2963. }
  2964. /**
  2965. * mega_m_to_n()
  2966. * @arg - user address
  2967. * @uioc - new ioctl structure
  2968. *
  2969. * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
  2970. * structure
  2971. *
  2972. * Converts the older mimd ioctl structure to newer NIT structure
  2973. */
  2974. static int
  2975. mega_m_to_n(void __user *arg, nitioctl_t *uioc)
  2976. {
  2977. struct uioctl_t uioc_mimd;
  2978. char signature[8] = {0};
  2979. u8 opcode;
  2980. u8 subopcode;
  2981. /*
  2982. * check is the application conforms to NIT. We do not have to do much
  2983. * in that case.
  2984. * We exploit the fact that the signature is stored in the very
  2985. * begining of the structure.
  2986. */
  2987. if( copy_from_user(signature, arg, 7) )
  2988. return (-EFAULT);
  2989. if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  2990. /*
  2991. * NOTE NOTE: The nit ioctl is still under flux because of
  2992. * change of mailbox definition, in HPE. No applications yet
  2993. * use this interface and let's not have applications use this
  2994. * interface till the new specifitions are in place.
  2995. */
  2996. return -EINVAL;
  2997. #if 0
  2998. if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
  2999. return (-EFAULT);
  3000. return 0;
  3001. #endif
  3002. }
  3003. /*
  3004. * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
  3005. *
  3006. * Get the user ioctl structure
  3007. */
  3008. if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
  3009. return (-EFAULT);
  3010. /*
  3011. * Get the opcode and subopcode for the commands
  3012. */
  3013. opcode = uioc_mimd.ui.fcs.opcode;
  3014. subopcode = uioc_mimd.ui.fcs.subopcode;
  3015. switch (opcode) {
  3016. case 0x82:
  3017. switch (subopcode) {
  3018. case MEGAIOC_QDRVRVER: /* Query driver version */
  3019. uioc->opcode = GET_DRIVER_VER;
  3020. uioc->uioc_uaddr = uioc_mimd.data;
  3021. break;
  3022. case MEGAIOC_QNADAP: /* Get # of adapters */
  3023. uioc->opcode = GET_N_ADAP;
  3024. uioc->uioc_uaddr = uioc_mimd.data;
  3025. break;
  3026. case MEGAIOC_QADAPINFO: /* Get adapter information */
  3027. uioc->opcode = GET_ADAP_INFO;
  3028. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3029. uioc->uioc_uaddr = uioc_mimd.data;
  3030. break;
  3031. default:
  3032. return(-EINVAL);
  3033. }
  3034. break;
  3035. case 0x81:
  3036. uioc->opcode = MBOX_CMD;
  3037. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3038. memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  3039. uioc->xferlen = uioc_mimd.ui.fcs.length;
  3040. if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  3041. if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  3042. break;
  3043. case 0x80:
  3044. uioc->opcode = MBOX_CMD;
  3045. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3046. memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  3047. /*
  3048. * Choose the xferlen bigger of input and output data
  3049. */
  3050. uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
  3051. uioc_mimd.outlen : uioc_mimd.inlen;
  3052. if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  3053. if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  3054. break;
  3055. default:
  3056. return (-EINVAL);
  3057. }
  3058. return 0;
  3059. }
  3060. /*
  3061. * mega_n_to_m()
  3062. * @arg - user address
  3063. * @mc - mailbox command
  3064. *
  3065. * Updates the status information to the application, depending on application
  3066. * conforms to older mimd ioctl interface or newer NIT ioctl interface
  3067. */
  3068. static int
  3069. mega_n_to_m(void __user *arg, megacmd_t *mc)
  3070. {
  3071. nitioctl_t __user *uiocp;
  3072. megacmd_t __user *umc;
  3073. mega_passthru __user *upthru;
  3074. struct uioctl_t __user *uioc_mimd;
  3075. char signature[8] = {0};
  3076. /*
  3077. * check is the application conforms to NIT.
  3078. */
  3079. if( copy_from_user(signature, arg, 7) )
  3080. return -EFAULT;
  3081. if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  3082. uiocp = arg;
  3083. if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
  3084. return (-EFAULT);
  3085. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3086. umc = MBOX_P(uiocp);
  3087. if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  3088. return -EFAULT;
  3089. if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
  3090. return (-EFAULT);
  3091. }
  3092. }
  3093. else {
  3094. uioc_mimd = arg;
  3095. if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
  3096. return (-EFAULT);
  3097. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3098. umc = (megacmd_t __user *)uioc_mimd->mbox;
  3099. if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  3100. return (-EFAULT);
  3101. if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
  3102. return (-EFAULT);
  3103. }
  3104. }
  3105. return 0;
  3106. }
  3107. /*
  3108. * MEGARAID 'FW' commands.
  3109. */
  3110. /**
  3111. * mega_is_bios_enabled()
  3112. * @adapter - pointer to our soft state
  3113. *
  3114. * issue command to find out if the BIOS is enabled for this controller
  3115. */
  3116. static int
  3117. mega_is_bios_enabled(adapter_t *adapter)
  3118. {
  3119. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3120. mbox_t *mbox;
  3121. int ret;
  3122. mbox = (mbox_t *)raw_mbox;
  3123. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3124. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3125. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3126. raw_mbox[0] = IS_BIOS_ENABLED;
  3127. raw_mbox[2] = GET_BIOS;
  3128. ret = issue_scb_block(adapter, raw_mbox);
  3129. return *(char *)adapter->mega_buffer;
  3130. }
  3131. /**
  3132. * mega_enum_raid_scsi()
  3133. * @adapter - pointer to our soft state
  3134. *
  3135. * Find out what channels are RAID/SCSI. This information is used to
  3136. * differentiate the virtual channels and physical channels and to support
  3137. * ROMB feature and non-disk devices.
  3138. */
  3139. static void
  3140. mega_enum_raid_scsi(adapter_t *adapter)
  3141. {
  3142. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3143. mbox_t *mbox;
  3144. int i;
  3145. mbox = (mbox_t *)raw_mbox;
  3146. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3147. /*
  3148. * issue command to find out what channels are raid/scsi
  3149. */
  3150. raw_mbox[0] = CHNL_CLASS;
  3151. raw_mbox[2] = GET_CHNL_CLASS;
  3152. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3153. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3154. /*
  3155. * Non-ROMB firmware fail this command, so all channels
  3156. * must be shown RAID
  3157. */
  3158. adapter->mega_ch_class = 0xFF;
  3159. if(!issue_scb_block(adapter, raw_mbox)) {
  3160. adapter->mega_ch_class = *((char *)adapter->mega_buffer);
  3161. }
  3162. for( i = 0; i < adapter->product_info.nchannels; i++ ) {
  3163. if( (adapter->mega_ch_class >> i) & 0x01 ) {
  3164. printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
  3165. i);
  3166. }
  3167. else {
  3168. printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
  3169. i);
  3170. }
  3171. }
  3172. return;
  3173. }
  3174. /**
  3175. * mega_get_boot_drv()
  3176. * @adapter - pointer to our soft state
  3177. *
  3178. * Find out which device is the boot device. Note, any logical drive or any
  3179. * phyical device (e.g., a CDROM) can be designated as a boot device.
  3180. */
  3181. static void
  3182. mega_get_boot_drv(adapter_t *adapter)
  3183. {
  3184. struct private_bios_data *prv_bios_data;
  3185. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3186. mbox_t *mbox;
  3187. u16 cksum = 0;
  3188. u8 *cksum_p;
  3189. u8 boot_pdrv;
  3190. int i;
  3191. mbox = (mbox_t *)raw_mbox;
  3192. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3193. raw_mbox[0] = BIOS_PVT_DATA;
  3194. raw_mbox[2] = GET_BIOS_PVT_DATA;
  3195. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3196. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3197. adapter->boot_ldrv_enabled = 0;
  3198. adapter->boot_ldrv = 0;
  3199. adapter->boot_pdrv_enabled = 0;
  3200. adapter->boot_pdrv_ch = 0;
  3201. adapter->boot_pdrv_tgt = 0;
  3202. if(issue_scb_block(adapter, raw_mbox) == 0) {
  3203. prv_bios_data =
  3204. (struct private_bios_data *)adapter->mega_buffer;
  3205. cksum = 0;
  3206. cksum_p = (char *)prv_bios_data;
  3207. for (i = 0; i < 14; i++ ) {
  3208. cksum += (u16)(*cksum_p++);
  3209. }
  3210. if (prv_bios_data->cksum == (u16)(0-cksum) ) {
  3211. /*
  3212. * If MSB is set, a physical drive is set as boot
  3213. * device
  3214. */
  3215. if( prv_bios_data->boot_drv & 0x80 ) {
  3216. adapter->boot_pdrv_enabled = 1;
  3217. boot_pdrv = prv_bios_data->boot_drv & 0x7F;
  3218. adapter->boot_pdrv_ch = boot_pdrv / 16;
  3219. adapter->boot_pdrv_tgt = boot_pdrv % 16;
  3220. }
  3221. else {
  3222. adapter->boot_ldrv_enabled = 1;
  3223. adapter->boot_ldrv = prv_bios_data->boot_drv;
  3224. }
  3225. }
  3226. }
  3227. }
  3228. /**
  3229. * mega_support_random_del()
  3230. * @adapter - pointer to our soft state
  3231. *
  3232. * Find out if this controller supports random deletion and addition of
  3233. * logical drives
  3234. */
  3235. static int
  3236. mega_support_random_del(adapter_t *adapter)
  3237. {
  3238. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3239. mbox_t *mbox;
  3240. int rval;
  3241. mbox = (mbox_t *)raw_mbox;
  3242. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3243. /*
  3244. * issue command
  3245. */
  3246. raw_mbox[0] = FC_DEL_LOGDRV;
  3247. raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  3248. rval = issue_scb_block(adapter, raw_mbox);
  3249. return !rval;
  3250. }
  3251. /**
  3252. * mega_support_ext_cdb()
  3253. * @adapter - pointer to our soft state
  3254. *
  3255. * Find out if this firmware support cdblen > 10
  3256. */
  3257. static int
  3258. mega_support_ext_cdb(adapter_t *adapter)
  3259. {
  3260. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3261. mbox_t *mbox;
  3262. int rval;
  3263. mbox = (mbox_t *)raw_mbox;
  3264. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3265. /*
  3266. * issue command to find out if controller supports extended CDBs.
  3267. */
  3268. raw_mbox[0] = 0xA4;
  3269. raw_mbox[2] = 0x16;
  3270. rval = issue_scb_block(adapter, raw_mbox);
  3271. return !rval;
  3272. }
  3273. /**
  3274. * mega_del_logdrv()
  3275. * @adapter - pointer to our soft state
  3276. * @logdrv - logical drive to be deleted
  3277. *
  3278. * Delete the specified logical drive. It is the responsibility of the user
  3279. * app to let the OS know about this operation.
  3280. */
  3281. static int
  3282. mega_del_logdrv(adapter_t *adapter, int logdrv)
  3283. {
  3284. unsigned long flags;
  3285. scb_t *scb;
  3286. int rval;
  3287. /*
  3288. * Stop sending commands to the controller, queue them internally.
  3289. * When deletion is complete, ISR will flush the queue.
  3290. */
  3291. atomic_set(&adapter->quiescent, 1);
  3292. /*
  3293. * Wait till all the issued commands are complete and there are no
  3294. * commands in the pending queue
  3295. */
  3296. while (atomic_read(&adapter->pend_cmds) > 0 ||
  3297. !list_empty(&adapter->pending_list))
  3298. msleep(1000); /* sleep for 1s */
  3299. rval = mega_do_del_logdrv(adapter, logdrv);
  3300. spin_lock_irqsave(&adapter->lock, flags);
  3301. /*
  3302. * If delete operation was successful, add 0x80 to the logical drive
  3303. * ids for commands in the pending queue.
  3304. */
  3305. if (adapter->read_ldidmap) {
  3306. struct list_head *pos;
  3307. list_for_each(pos, &adapter->pending_list) {
  3308. scb = list_entry(pos, scb_t, list);
  3309. if (scb->pthru->logdrv < 0x80 )
  3310. scb->pthru->logdrv += 0x80;
  3311. }
  3312. }
  3313. atomic_set(&adapter->quiescent, 0);
  3314. mega_runpendq(adapter);
  3315. spin_unlock_irqrestore(&adapter->lock, flags);
  3316. return rval;
  3317. }
  3318. static int
  3319. mega_do_del_logdrv(adapter_t *adapter, int logdrv)
  3320. {
  3321. megacmd_t mc;
  3322. int rval;
  3323. memset( &mc, 0, sizeof(megacmd_t));
  3324. mc.cmd = FC_DEL_LOGDRV;
  3325. mc.opcode = OP_DEL_LOGDRV;
  3326. mc.subopcode = logdrv;
  3327. rval = mega_internal_command(adapter, LOCK_INT, &mc, NULL);
  3328. /* log this event */
  3329. if(rval) {
  3330. printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
  3331. return rval;
  3332. }
  3333. /*
  3334. * After deleting first logical drive, the logical drives must be
  3335. * addressed by adding 0x80 to the logical drive id.
  3336. */
  3337. adapter->read_ldidmap = 1;
  3338. return rval;
  3339. }
  3340. /**
  3341. * mega_get_max_sgl()
  3342. * @adapter - pointer to our soft state
  3343. *
  3344. * Find out the maximum number of scatter-gather elements supported by this
  3345. * version of the firmware
  3346. */
  3347. static void
  3348. mega_get_max_sgl(adapter_t *adapter)
  3349. {
  3350. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3351. mbox_t *mbox;
  3352. mbox = (mbox_t *)raw_mbox;
  3353. memset(mbox, 0, sizeof(raw_mbox));
  3354. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3355. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3356. raw_mbox[0] = MAIN_MISC_OPCODE;
  3357. raw_mbox[2] = GET_MAX_SG_SUPPORT;
  3358. if( issue_scb_block(adapter, raw_mbox) ) {
  3359. /*
  3360. * f/w does not support this command. Choose the default value
  3361. */
  3362. adapter->sglen = MIN_SGLIST;
  3363. }
  3364. else {
  3365. adapter->sglen = *((char *)adapter->mega_buffer);
  3366. /*
  3367. * Make sure this is not more than the resources we are
  3368. * planning to allocate
  3369. */
  3370. if ( adapter->sglen > MAX_SGLIST )
  3371. adapter->sglen = MAX_SGLIST;
  3372. }
  3373. return;
  3374. }
  3375. /**
  3376. * mega_support_cluster()
  3377. * @adapter - pointer to our soft state
  3378. *
  3379. * Find out if this firmware support cluster calls.
  3380. */
  3381. static int
  3382. mega_support_cluster(adapter_t *adapter)
  3383. {
  3384. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3385. mbox_t *mbox;
  3386. mbox = (mbox_t *)raw_mbox;
  3387. memset(mbox, 0, sizeof(raw_mbox));
  3388. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3389. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3390. /*
  3391. * Try to get the initiator id. This command will succeed iff the
  3392. * clustering is available on this HBA.
  3393. */
  3394. raw_mbox[0] = MEGA_GET_TARGET_ID;
  3395. if( issue_scb_block(adapter, raw_mbox) == 0 ) {
  3396. /*
  3397. * Cluster support available. Get the initiator target id.
  3398. * Tell our id to mid-layer too.
  3399. */
  3400. adapter->this_id = *(u32 *)adapter->mega_buffer;
  3401. adapter->host->this_id = adapter->this_id;
  3402. return 1;
  3403. }
  3404. return 0;
  3405. }
  3406. /**
  3407. * mega_adapinq()
  3408. * @adapter - pointer to our soft state
  3409. * @dma_handle - DMA address of the buffer
  3410. *
  3411. * Issue internal comamnds while interrupts are available.
  3412. * We only issue direct mailbox commands from within the driver. ioctl()
  3413. * interface using these routines can issue passthru commands.
  3414. */
  3415. static int
  3416. mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
  3417. {
  3418. megacmd_t mc;
  3419. memset(&mc, 0, sizeof(megacmd_t));
  3420. if( adapter->flag & BOARD_40LD ) {
  3421. mc.cmd = FC_NEW_CONFIG;
  3422. mc.opcode = NC_SUBOP_ENQUIRY3;
  3423. mc.subopcode = ENQ3_GET_SOLICITED_FULL;
  3424. }
  3425. else {
  3426. mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
  3427. }
  3428. mc.xferaddr = (u32)dma_handle;
  3429. if ( mega_internal_command(adapter, LOCK_INT, &mc, NULL) != 0 ) {
  3430. return -1;
  3431. }
  3432. return 0;
  3433. }
  3434. /** mega_internal_dev_inquiry()
  3435. * @adapter - pointer to our soft state
  3436. * @ch - channel for this device
  3437. * @tgt - ID of this device
  3438. * @buf_dma_handle - DMA address of the buffer
  3439. *
  3440. * Issue the scsi inquiry for the specified device.
  3441. */
  3442. static int
  3443. mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
  3444. dma_addr_t buf_dma_handle)
  3445. {
  3446. mega_passthru *pthru;
  3447. dma_addr_t pthru_dma_handle;
  3448. megacmd_t mc;
  3449. int rval;
  3450. struct pci_dev *pdev;
  3451. /*
  3452. * For all internal commands, the buffer must be allocated in <4GB
  3453. * address range
  3454. */
  3455. if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
  3456. pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
  3457. &pthru_dma_handle);
  3458. if( pthru == NULL ) {
  3459. free_local_pdev(pdev);
  3460. return -1;
  3461. }
  3462. pthru->timeout = 2;
  3463. pthru->ars = 1;
  3464. pthru->reqsenselen = 14;
  3465. pthru->islogical = 0;
  3466. pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
  3467. pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
  3468. pthru->cdblen = 6;
  3469. pthru->cdb[0] = INQUIRY;
  3470. pthru->cdb[1] = 0;
  3471. pthru->cdb[2] = 0;
  3472. pthru->cdb[3] = 0;
  3473. pthru->cdb[4] = 255;
  3474. pthru->cdb[5] = 0;
  3475. pthru->dataxferaddr = (u32)buf_dma_handle;
  3476. pthru->dataxferlen = 256;
  3477. memset(&mc, 0, sizeof(megacmd_t));
  3478. mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  3479. mc.xferaddr = (u32)pthru_dma_handle;
  3480. rval = mega_internal_command(adapter, LOCK_INT, &mc, pthru);
  3481. pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
  3482. pthru_dma_handle);
  3483. free_local_pdev(pdev);
  3484. return rval;
  3485. }
  3486. /**
  3487. * mega_internal_command()
  3488. * @adapter - pointer to our soft state
  3489. * @ls - the scope of the exclusion lock.
  3490. * @mc - the mailbox command
  3491. * @pthru - Passthru structure for DCDB commands
  3492. *
  3493. * Issue the internal commands in interrupt mode.
  3494. * The last argument is the address of the passthru structure if the command
  3495. * to be fired is a passthru command
  3496. *
  3497. * lockscope specifies whether the caller has already acquired the lock. Of
  3498. * course, the caller must know which lock we are talking about.
  3499. *
  3500. * Note: parameter 'pthru' is null for non-passthru commands.
  3501. */
  3502. static int
  3503. mega_internal_command(adapter_t *adapter, lockscope_t ls, megacmd_t *mc,
  3504. mega_passthru *pthru )
  3505. {
  3506. Scsi_Cmnd *scmd;
  3507. struct scsi_device *sdev;
  3508. unsigned long flags = 0;
  3509. scb_t *scb;
  3510. int rval;
  3511. /*
  3512. * The internal commands share one command id and hence are
  3513. * serialized. This is so because we want to reserve maximum number of
  3514. * available command ids for the I/O commands.
  3515. */
  3516. down(&adapter->int_mtx);
  3517. scb = &adapter->int_scb;
  3518. memset(scb, 0, sizeof(scb_t));
  3519. scmd = &adapter->int_scmd;
  3520. memset(scmd, 0, sizeof(Scsi_Cmnd));
  3521. sdev = kmalloc(sizeof(struct scsi_device), GFP_KERNEL);
  3522. memset(sdev, 0, sizeof(struct scsi_device));
  3523. scmd->device = sdev;
  3524. scmd->device->host = adapter->host;
  3525. scmd->buffer = (void *)scb;
  3526. scmd->cmnd[0] = MEGA_INTERNAL_CMD;
  3527. scb->state |= SCB_ACTIVE;
  3528. scb->cmd = scmd;
  3529. memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
  3530. /*
  3531. * Is it a passthru command
  3532. */
  3533. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3534. scb->pthru = pthru;
  3535. }
  3536. scb->idx = CMDID_INT_CMDS;
  3537. scmd->state = 0;
  3538. /*
  3539. * Get the lock only if the caller has not acquired it already
  3540. */
  3541. if( ls == LOCK_INT ) spin_lock_irqsave(&adapter->lock, flags);
  3542. megaraid_queue(scmd, mega_internal_done);
  3543. if( ls == LOCK_INT ) spin_unlock_irqrestore(&adapter->lock, flags);
  3544. /*
  3545. * Wait till this command finishes. Do not use
  3546. * wait_event_interruptible(). It causes panic if CTRL-C is hit when
  3547. * dumping e.g., physical disk information through /proc interface.
  3548. */
  3549. #if 0
  3550. wait_event_interruptible(adapter->int_waitq, scmd->state);
  3551. #endif
  3552. wait_event(adapter->int_waitq, scmd->state);
  3553. rval = scmd->result;
  3554. mc->status = scmd->result;
  3555. kfree(sdev);
  3556. /*
  3557. * Print a debug message for all failed commands. Applications can use
  3558. * this information.
  3559. */
  3560. if( scmd->result && trace_level ) {
  3561. printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
  3562. mc->cmd, mc->opcode, mc->subopcode, scmd->result);
  3563. }
  3564. up(&adapter->int_mtx);
  3565. return rval;
  3566. }
  3567. /**
  3568. * mega_internal_done()
  3569. * @scmd - internal scsi command
  3570. *
  3571. * Callback routine for internal commands.
  3572. */
  3573. static void
  3574. mega_internal_done(Scsi_Cmnd *scmd)
  3575. {
  3576. adapter_t *adapter;
  3577. adapter = (adapter_t *)scmd->device->host->hostdata;
  3578. scmd->state = 1; /* thread waiting for its command to complete */
  3579. /*
  3580. * See comment in mega_internal_command() routine for
  3581. * wait_event_interruptible()
  3582. */
  3583. #if 0
  3584. wake_up_interruptible(&adapter->int_waitq);
  3585. #endif
  3586. wake_up(&adapter->int_waitq);
  3587. }
  3588. static struct scsi_host_template megaraid_template = {
  3589. .module = THIS_MODULE,
  3590. .name = "MegaRAID",
  3591. .proc_name = "megaraid",
  3592. .info = megaraid_info,
  3593. .queuecommand = megaraid_queue,
  3594. .bios_param = megaraid_biosparam,
  3595. .max_sectors = MAX_SECTORS_PER_IO,
  3596. .can_queue = MAX_COMMANDS,
  3597. .this_id = DEFAULT_INITIATOR_ID,
  3598. .sg_tablesize = MAX_SGLIST,
  3599. .cmd_per_lun = DEF_CMD_PER_LUN,
  3600. .use_clustering = ENABLE_CLUSTERING,
  3601. .eh_abort_handler = megaraid_abort,
  3602. .eh_device_reset_handler = megaraid_reset,
  3603. .eh_bus_reset_handler = megaraid_reset,
  3604. .eh_host_reset_handler = megaraid_reset,
  3605. };
  3606. static int __devinit
  3607. megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  3608. {
  3609. struct Scsi_Host *host;
  3610. adapter_t *adapter;
  3611. unsigned long mega_baseport, tbase, flag = 0;
  3612. u16 subsysid, subsysvid;
  3613. u8 pci_bus, pci_dev_func;
  3614. int irq, i, j;
  3615. int error = -ENODEV;
  3616. if (pci_enable_device(pdev))
  3617. goto out;
  3618. pci_set_master(pdev);
  3619. pci_bus = pdev->bus->number;
  3620. pci_dev_func = pdev->devfn;
  3621. /*
  3622. * The megaraid3 stuff reports the ID of the Intel part which is not
  3623. * remotely specific to the megaraid
  3624. */
  3625. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  3626. u16 magic;
  3627. /*
  3628. * Don't fall over the Compaq management cards using the same
  3629. * PCI identifier
  3630. */
  3631. if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
  3632. pdev->subsystem_device == 0xC000)
  3633. return -ENODEV;
  3634. /* Now check the magic signature byte */
  3635. pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
  3636. if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
  3637. return -ENODEV;
  3638. /* Ok it is probably a megaraid */
  3639. }
  3640. /*
  3641. * For these vendor and device ids, signature offsets are not
  3642. * valid and 64 bit is implicit
  3643. */
  3644. if (id->driver_data & BOARD_64BIT)
  3645. flag |= BOARD_64BIT;
  3646. else {
  3647. u32 magic64;
  3648. pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
  3649. if (magic64 == HBA_SIGNATURE_64BIT)
  3650. flag |= BOARD_64BIT;
  3651. }
  3652. subsysvid = pdev->subsystem_vendor;
  3653. subsysid = pdev->subsystem_device;
  3654. printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
  3655. id->vendor, id->device, pci_bus);
  3656. printk("slot %d:func %d\n",
  3657. PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
  3658. /* Read the base port and IRQ from PCI */
  3659. mega_baseport = pci_resource_start(pdev, 0);
  3660. irq = pdev->irq;
  3661. tbase = mega_baseport;
  3662. if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
  3663. flag |= BOARD_MEMMAP;
  3664. if (!request_mem_region(mega_baseport, 128, "megaraid")) {
  3665. printk(KERN_WARNING "megaraid: mem region busy!\n");
  3666. goto out_disable_device;
  3667. }
  3668. mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
  3669. if (!mega_baseport) {
  3670. printk(KERN_WARNING
  3671. "megaraid: could not map hba memory\n");
  3672. goto out_release_region;
  3673. }
  3674. } else {
  3675. flag |= BOARD_IOMAP;
  3676. mega_baseport += 0x10;
  3677. if (!request_region(mega_baseport, 16, "megaraid"))
  3678. goto out_disable_device;
  3679. }
  3680. /* Initialize SCSI Host structure */
  3681. host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
  3682. if (!host)
  3683. goto out_iounmap;
  3684. adapter = (adapter_t *)host->hostdata;
  3685. memset(adapter, 0, sizeof(adapter_t));
  3686. printk(KERN_NOTICE
  3687. "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
  3688. host->host_no, mega_baseport, irq);
  3689. adapter->base = mega_baseport;
  3690. INIT_LIST_HEAD(&adapter->free_list);
  3691. INIT_LIST_HEAD(&adapter->pending_list);
  3692. INIT_LIST_HEAD(&adapter->completed_list);
  3693. adapter->flag = flag;
  3694. spin_lock_init(&adapter->lock);
  3695. scsi_assign_lock(host, &adapter->lock);
  3696. host->cmd_per_lun = max_cmd_per_lun;
  3697. host->max_sectors = max_sectors_per_io;
  3698. adapter->dev = pdev;
  3699. adapter->host = host;
  3700. adapter->host->irq = irq;
  3701. if (flag & BOARD_MEMMAP)
  3702. adapter->host->base = tbase;
  3703. else {
  3704. adapter->host->io_port = tbase;
  3705. adapter->host->n_io_port = 16;
  3706. }
  3707. adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
  3708. /*
  3709. * Allocate buffer to issue internal commands.
  3710. */
  3711. adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
  3712. MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
  3713. if (!adapter->mega_buffer) {
  3714. printk(KERN_WARNING "megaraid: out of RAM.\n");
  3715. goto out_host_put;
  3716. }
  3717. adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
  3718. if (!adapter->scb_list) {
  3719. printk(KERN_WARNING "megaraid: out of RAM.\n");
  3720. goto out_free_cmd_buffer;
  3721. }
  3722. if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
  3723. megaraid_isr_memmapped : megaraid_isr_iomapped,
  3724. SA_SHIRQ, "megaraid", adapter)) {
  3725. printk(KERN_WARNING
  3726. "megaraid: Couldn't register IRQ %d!\n", irq);
  3727. goto out_free_scb_list;
  3728. }
  3729. if (mega_setup_mailbox(adapter))
  3730. goto out_free_irq;
  3731. if (mega_query_adapter(adapter))
  3732. goto out_free_mbox;
  3733. /*
  3734. * Have checks for some buggy f/w
  3735. */
  3736. if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
  3737. /*
  3738. * Which firmware
  3739. */
  3740. if (!strcmp(adapter->fw_version, "3.00") ||
  3741. !strcmp(adapter->fw_version, "3.01")) {
  3742. printk( KERN_WARNING
  3743. "megaraid: Your card is a Dell PERC "
  3744. "2/SC RAID controller with "
  3745. "firmware\nmegaraid: 3.00 or 3.01. "
  3746. "This driver is known to have "
  3747. "corruption issues\nmegaraid: with "
  3748. "those firmware versions on this "
  3749. "specific card. In order\nmegaraid: "
  3750. "to protect your data, please upgrade "
  3751. "your firmware to version\nmegaraid: "
  3752. "3.10 or later, available from the "
  3753. "Dell Technical Support web\n"
  3754. "megaraid: site at\nhttp://support."
  3755. "dell.com/us/en/filelib/download/"
  3756. "index.asp?fileid=2940\n"
  3757. );
  3758. }
  3759. }
  3760. /*
  3761. * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
  3762. * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
  3763. * support, since this firmware cannot handle 64 bit
  3764. * addressing
  3765. */
  3766. if ((subsysvid == HP_SUBSYS_VID) &&
  3767. ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
  3768. /*
  3769. * which firmware
  3770. */
  3771. if (!strcmp(adapter->fw_version, "H01.07") ||
  3772. !strcmp(adapter->fw_version, "H01.08") ||
  3773. !strcmp(adapter->fw_version, "H01.09") ) {
  3774. printk(KERN_WARNING
  3775. "megaraid: Firmware H.01.07, "
  3776. "H.01.08, and H.01.09 on 1M/2M "
  3777. "controllers\n"
  3778. "megaraid: do not support 64 bit "
  3779. "addressing.\nmegaraid: DISABLING "
  3780. "64 bit support.\n");
  3781. adapter->flag &= ~BOARD_64BIT;
  3782. }
  3783. }
  3784. if (mega_is_bios_enabled(adapter))
  3785. mega_hbas[hba_count].is_bios_enabled = 1;
  3786. mega_hbas[hba_count].hostdata_addr = adapter;
  3787. /*
  3788. * Find out which channel is raid and which is scsi. This is
  3789. * for ROMB support.
  3790. */
  3791. mega_enum_raid_scsi(adapter);
  3792. /*
  3793. * Find out if a logical drive is set as the boot drive. If
  3794. * there is one, will make that as the first logical drive.
  3795. * ROMB: Do we have to boot from a physical drive. Then all
  3796. * the physical drives would appear before the logical disks.
  3797. * Else, all the physical drives would be exported to the mid
  3798. * layer after logical drives.
  3799. */
  3800. mega_get_boot_drv(adapter);
  3801. if (adapter->boot_pdrv_enabled) {
  3802. j = adapter->product_info.nchannels;
  3803. for( i = 0; i < j; i++ )
  3804. adapter->logdrv_chan[i] = 0;
  3805. for( i = j; i < NVIRT_CHAN + j; i++ )
  3806. adapter->logdrv_chan[i] = 1;
  3807. } else {
  3808. for (i = 0; i < NVIRT_CHAN; i++)
  3809. adapter->logdrv_chan[i] = 1;
  3810. for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
  3811. adapter->logdrv_chan[i] = 0;
  3812. adapter->mega_ch_class <<= NVIRT_CHAN;
  3813. }
  3814. /*
  3815. * Do we support random deletion and addition of logical
  3816. * drives
  3817. */
  3818. adapter->read_ldidmap = 0; /* set it after first logdrv
  3819. delete cmd */
  3820. adapter->support_random_del = mega_support_random_del(adapter);
  3821. /* Initialize SCBs */
  3822. if (mega_init_scb(adapter))
  3823. goto out_free_mbox;
  3824. /*
  3825. * Reset the pending commands counter
  3826. */
  3827. atomic_set(&adapter->pend_cmds, 0);
  3828. /*
  3829. * Reset the adapter quiescent flag
  3830. */
  3831. atomic_set(&adapter->quiescent, 0);
  3832. hba_soft_state[hba_count] = adapter;
  3833. /*
  3834. * Fill in the structure which needs to be passed back to the
  3835. * application when it does an ioctl() for controller related
  3836. * information.
  3837. */
  3838. i = hba_count;
  3839. mcontroller[i].base = mega_baseport;
  3840. mcontroller[i].irq = irq;
  3841. mcontroller[i].numldrv = adapter->numldrv;
  3842. mcontroller[i].pcibus = pci_bus;
  3843. mcontroller[i].pcidev = id->device;
  3844. mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
  3845. mcontroller[i].pciid = -1;
  3846. mcontroller[i].pcivendor = id->vendor;
  3847. mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
  3848. mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
  3849. /* Set the Mode of addressing to 64 bit if we can */
  3850. if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
  3851. pci_set_dma_mask(pdev, 0xffffffffffffffffULL);
  3852. adapter->has_64bit_addr = 1;
  3853. } else {
  3854. pci_set_dma_mask(pdev, 0xffffffff);
  3855. adapter->has_64bit_addr = 0;
  3856. }
  3857. init_MUTEX(&adapter->int_mtx);
  3858. init_waitqueue_head(&adapter->int_waitq);
  3859. adapter->this_id = DEFAULT_INITIATOR_ID;
  3860. adapter->host->this_id = DEFAULT_INITIATOR_ID;
  3861. #if MEGA_HAVE_CLUSTERING
  3862. /*
  3863. * Is cluster support enabled on this controller
  3864. * Note: In a cluster the HBAs ( the initiators ) will have
  3865. * different target IDs and we cannot assume it to be 7. Call
  3866. * to mega_support_cluster() will get the target ids also if
  3867. * the cluster support is available
  3868. */
  3869. adapter->has_cluster = mega_support_cluster(adapter);
  3870. if (adapter->has_cluster) {
  3871. printk(KERN_NOTICE
  3872. "megaraid: Cluster driver, initiator id:%d\n",
  3873. adapter->this_id);
  3874. }
  3875. #endif
  3876. pci_set_drvdata(pdev, host);
  3877. mega_create_proc_entry(hba_count, mega_proc_dir_entry);
  3878. error = scsi_add_host(host, &pdev->dev);
  3879. if (error)
  3880. goto out_free_mbox;
  3881. scsi_scan_host(host);
  3882. hba_count++;
  3883. return 0;
  3884. out_free_mbox:
  3885. pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  3886. adapter->una_mbox64, adapter->una_mbox64_dma);
  3887. out_free_irq:
  3888. free_irq(adapter->host->irq, adapter);
  3889. out_free_scb_list:
  3890. kfree(adapter->scb_list);
  3891. out_free_cmd_buffer:
  3892. pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  3893. adapter->mega_buffer, adapter->buf_dma_handle);
  3894. out_host_put:
  3895. scsi_host_put(host);
  3896. out_iounmap:
  3897. if (flag & BOARD_MEMMAP)
  3898. iounmap((void *)mega_baseport);
  3899. out_release_region:
  3900. if (flag & BOARD_MEMMAP)
  3901. release_mem_region(tbase, 128);
  3902. else
  3903. release_region(mega_baseport, 16);
  3904. out_disable_device:
  3905. pci_disable_device(pdev);
  3906. out:
  3907. return error;
  3908. }
  3909. static void
  3910. __megaraid_shutdown(adapter_t *adapter)
  3911. {
  3912. u_char raw_mbox[sizeof(struct mbox_out)];
  3913. mbox_t *mbox = (mbox_t *)raw_mbox;
  3914. int i;
  3915. /* Flush adapter cache */
  3916. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3917. raw_mbox[0] = FLUSH_ADAPTER;
  3918. free_irq(adapter->host->irq, adapter);
  3919. /* Issue a blocking (interrupts disabled) command to the card */
  3920. issue_scb_block(adapter, raw_mbox);
  3921. /* Flush disks cache */
  3922. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3923. raw_mbox[0] = FLUSH_SYSTEM;
  3924. /* Issue a blocking (interrupts disabled) command to the card */
  3925. issue_scb_block(adapter, raw_mbox);
  3926. if (atomic_read(&adapter->pend_cmds) > 0)
  3927. printk(KERN_WARNING "megaraid: pending commands!!\n");
  3928. /*
  3929. * Have a delibrate delay to make sure all the caches are
  3930. * actually flushed.
  3931. */
  3932. for (i = 0; i <= 10; i++)
  3933. mdelay(1000);
  3934. }
  3935. static void
  3936. megaraid_remove_one(struct pci_dev *pdev)
  3937. {
  3938. struct Scsi_Host *host = pci_get_drvdata(pdev);
  3939. adapter_t *adapter = (adapter_t *)host->hostdata;
  3940. char buf[12] = { 0 };
  3941. scsi_remove_host(host);
  3942. __megaraid_shutdown(adapter);
  3943. /* Free our resources */
  3944. if (adapter->flag & BOARD_MEMMAP) {
  3945. iounmap((void *)adapter->base);
  3946. release_mem_region(adapter->host->base, 128);
  3947. } else
  3948. release_region(adapter->base, 16);
  3949. mega_free_sgl(adapter);
  3950. #ifdef CONFIG_PROC_FS
  3951. if (adapter->controller_proc_dir_entry) {
  3952. remove_proc_entry("stat", adapter->controller_proc_dir_entry);
  3953. remove_proc_entry("config",
  3954. adapter->controller_proc_dir_entry);
  3955. remove_proc_entry("mailbox",
  3956. adapter->controller_proc_dir_entry);
  3957. #if MEGA_HAVE_ENH_PROC
  3958. remove_proc_entry("rebuild-rate",
  3959. adapter->controller_proc_dir_entry);
  3960. remove_proc_entry("battery-status",
  3961. adapter->controller_proc_dir_entry);
  3962. remove_proc_entry("diskdrives-ch0",
  3963. adapter->controller_proc_dir_entry);
  3964. remove_proc_entry("diskdrives-ch1",
  3965. adapter->controller_proc_dir_entry);
  3966. remove_proc_entry("diskdrives-ch2",
  3967. adapter->controller_proc_dir_entry);
  3968. remove_proc_entry("diskdrives-ch3",
  3969. adapter->controller_proc_dir_entry);
  3970. remove_proc_entry("raiddrives-0-9",
  3971. adapter->controller_proc_dir_entry);
  3972. remove_proc_entry("raiddrives-10-19",
  3973. adapter->controller_proc_dir_entry);
  3974. remove_proc_entry("raiddrives-20-29",
  3975. adapter->controller_proc_dir_entry);
  3976. remove_proc_entry("raiddrives-30-39",
  3977. adapter->controller_proc_dir_entry);
  3978. #endif
  3979. sprintf(buf, "hba%d", adapter->host->host_no);
  3980. remove_proc_entry(buf, mega_proc_dir_entry);
  3981. }
  3982. #endif
  3983. pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  3984. adapter->mega_buffer, adapter->buf_dma_handle);
  3985. kfree(adapter->scb_list);
  3986. pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  3987. adapter->una_mbox64, adapter->una_mbox64_dma);
  3988. scsi_host_put(host);
  3989. pci_disable_device(pdev);
  3990. hba_count--;
  3991. }
  3992. static void
  3993. megaraid_shutdown(struct device *dev)
  3994. {
  3995. struct Scsi_Host *host = pci_get_drvdata(to_pci_dev(dev));
  3996. adapter_t *adapter = (adapter_t *)host->hostdata;
  3997. __megaraid_shutdown(adapter);
  3998. }
  3999. static struct pci_device_id megaraid_pci_tbl[] = {
  4000. {PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DISCOVERY,
  4001. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4002. {PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_PERC4_DI,
  4003. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BOARD_64BIT},
  4004. {PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_PERC4_QC_VERDE,
  4005. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BOARD_64BIT},
  4006. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
  4007. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4008. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
  4009. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4010. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID3,
  4011. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4012. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
  4013. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4014. {PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_AMI_MEGARAID3,
  4015. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4016. {0,}
  4017. };
  4018. MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
  4019. static struct pci_driver megaraid_pci_driver = {
  4020. .name = "megaraid",
  4021. .id_table = megaraid_pci_tbl,
  4022. .probe = megaraid_probe_one,
  4023. .remove = __devexit_p(megaraid_remove_one),
  4024. .driver = {
  4025. .shutdown = megaraid_shutdown,
  4026. },
  4027. };
  4028. static int __init megaraid_init(void)
  4029. {
  4030. int error;
  4031. if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
  4032. max_cmd_per_lun = MAX_CMD_PER_LUN;
  4033. if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
  4034. max_mbox_busy_wait = MBOX_BUSY_WAIT;
  4035. #ifdef CONFIG_PROC_FS
  4036. mega_proc_dir_entry = proc_mkdir("megaraid", &proc_root);
  4037. if (!mega_proc_dir_entry) {
  4038. printk(KERN_WARNING
  4039. "megaraid: failed to create megaraid root\n");
  4040. }
  4041. #endif
  4042. error = pci_module_init(&megaraid_pci_driver);
  4043. if (error) {
  4044. #ifdef CONFIG_PROC_FS
  4045. remove_proc_entry("megaraid", &proc_root);
  4046. #endif
  4047. return error;
  4048. }
  4049. /*
  4050. * Register the driver as a character device, for applications
  4051. * to access it for ioctls.
  4052. * First argument (major) to register_chrdev implies a dynamic
  4053. * major number allocation.
  4054. */
  4055. major = register_chrdev(0, "megadev", &megadev_fops);
  4056. if (!major) {
  4057. printk(KERN_WARNING
  4058. "megaraid: failed to register char device\n");
  4059. }
  4060. return 0;
  4061. }
  4062. static void __exit megaraid_exit(void)
  4063. {
  4064. /*
  4065. * Unregister the character device interface to the driver.
  4066. */
  4067. unregister_chrdev(major, "megadev");
  4068. pci_unregister_driver(&megaraid_pci_driver);
  4069. #ifdef CONFIG_PROC_FS
  4070. remove_proc_entry("megaraid", &proc_root);
  4071. #endif
  4072. }
  4073. module_init(megaraid_init);
  4074. module_exit(megaraid_exit);
  4075. /* vi: set ts=8 sw=8 tw=78: */