megaraid.c 115 KB

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