megaraid_mbox.c 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132
  1. /*
  2. *
  3. * Linux MegaRAID device driver
  4. *
  5. * Copyright (c) 2003-2004 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. * FILE : megaraid_mbox.c
  13. * Version : v2.20.4.8 (Apr 11 2006)
  14. *
  15. * Authors:
  16. * Atul Mukker <Atul.Mukker@lsil.com>
  17. * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsil.com>
  18. * Manoj Jose <Manoj.Jose@lsil.com>
  19. * Seokmann Ju <Seokmann.Ju@lsil.com>
  20. *
  21. * List of supported controllers
  22. *
  23. * OEM Product Name VID DID SSVID SSID
  24. * --- ------------ --- --- ---- ----
  25. * Dell PERC3/QC 101E 1960 1028 0471
  26. * Dell PERC3/DC 101E 1960 1028 0493
  27. * Dell PERC3/SC 101E 1960 1028 0475
  28. * Dell PERC3/Di 1028 1960 1028 0123
  29. * Dell PERC4/SC 1000 1960 1028 0520
  30. * Dell PERC4/DC 1000 1960 1028 0518
  31. * Dell PERC4/QC 1000 0407 1028 0531
  32. * Dell PERC4/Di 1028 000F 1028 014A
  33. * Dell PERC 4e/Si 1028 0013 1028 016c
  34. * Dell PERC 4e/Di 1028 0013 1028 016d
  35. * Dell PERC 4e/Di 1028 0013 1028 016e
  36. * Dell PERC 4e/Di 1028 0013 1028 016f
  37. * Dell PERC 4e/Di 1028 0013 1028 0170
  38. * Dell PERC 4e/DC 1000 0408 1028 0002
  39. * Dell PERC 4e/SC 1000 0408 1028 0001
  40. *
  41. *
  42. * LSI MegaRAID SCSI 320-0 1000 1960 1000 A520
  43. * LSI MegaRAID SCSI 320-1 1000 1960 1000 0520
  44. * LSI MegaRAID SCSI 320-2 1000 1960 1000 0518
  45. * LSI MegaRAID SCSI 320-0X 1000 0407 1000 0530
  46. * LSI MegaRAID SCSI 320-2X 1000 0407 1000 0532
  47. * LSI MegaRAID SCSI 320-4X 1000 0407 1000 0531
  48. * LSI MegaRAID SCSI 320-1E 1000 0408 1000 0001
  49. * LSI MegaRAID SCSI 320-2E 1000 0408 1000 0002
  50. * LSI MegaRAID SATA 150-4 1000 1960 1000 4523
  51. * LSI MegaRAID SATA 150-6 1000 1960 1000 0523
  52. * LSI MegaRAID SATA 300-4X 1000 0409 1000 3004
  53. * LSI MegaRAID SATA 300-8X 1000 0409 1000 3008
  54. *
  55. * INTEL RAID Controller SRCU42X 1000 0407 8086 0532
  56. * INTEL RAID Controller SRCS16 1000 1960 8086 0523
  57. * INTEL RAID Controller SRCU42E 1000 0408 8086 0002
  58. * INTEL RAID Controller SRCZCRX 1000 0407 8086 0530
  59. * INTEL RAID Controller SRCS28X 1000 0409 8086 3008
  60. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3431
  61. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3499
  62. * INTEL RAID Controller SRCU51L 1000 1960 8086 0520
  63. *
  64. * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
  65. *
  66. * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
  67. *
  68. * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287
  69. *
  70. * For history of changes, see Documentation/ChangeLog.megaraid
  71. */
  72. #include "megaraid_mbox.h"
  73. static int megaraid_init(void);
  74. static void megaraid_exit(void);
  75. static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
  76. static void megaraid_detach_one(struct pci_dev *);
  77. static void megaraid_mbox_shutdown(struct pci_dev *);
  78. static int megaraid_io_attach(adapter_t *);
  79. static void megaraid_io_detach(adapter_t *);
  80. static int megaraid_init_mbox(adapter_t *);
  81. static void megaraid_fini_mbox(adapter_t *);
  82. static int megaraid_alloc_cmd_packets(adapter_t *);
  83. static void megaraid_free_cmd_packets(adapter_t *);
  84. static int megaraid_mbox_setup_dma_pools(adapter_t *);
  85. static void megaraid_mbox_teardown_dma_pools(adapter_t *);
  86. static int megaraid_sysfs_alloc_resources(adapter_t *);
  87. static void megaraid_sysfs_free_resources(adapter_t *);
  88. static int megaraid_abort_handler(struct scsi_cmnd *);
  89. static int megaraid_reset_handler(struct scsi_cmnd *);
  90. static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
  91. static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
  92. static int megaraid_busywait_mbox(mraid_device_t *);
  93. static int megaraid_mbox_product_info(adapter_t *);
  94. static int megaraid_mbox_extended_cdb(adapter_t *);
  95. static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
  96. static int megaraid_mbox_support_random_del(adapter_t *);
  97. static int megaraid_mbox_get_max_sg(adapter_t *);
  98. static void megaraid_mbox_enum_raid_scsi(adapter_t *);
  99. static void megaraid_mbox_flush_cache(adapter_t *);
  100. static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
  101. static void megaraid_mbox_setup_device_map(adapter_t *);
  102. static int megaraid_queue_command(struct scsi_cmnd *,
  103. void (*)(struct scsi_cmnd *));
  104. static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
  105. static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
  106. static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
  107. struct scsi_cmnd *);
  108. static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
  109. struct scsi_cmnd *);
  110. static irqreturn_t megaraid_isr(int, void *, struct pt_regs *);
  111. static void megaraid_mbox_dpc(unsigned long);
  112. static ssize_t megaraid_sysfs_show_app_hndl(struct class_device *, char *);
  113. static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
  114. static int megaraid_cmm_register(adapter_t *);
  115. static int megaraid_cmm_unregister(adapter_t *);
  116. static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
  117. static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
  118. static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
  119. static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
  120. static int wait_till_fw_empty(adapter_t *);
  121. MODULE_AUTHOR("sju@lsil.com");
  122. MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
  123. MODULE_LICENSE("GPL");
  124. MODULE_VERSION(MEGARAID_VERSION);
  125. /*
  126. * ### modules parameters for driver ###
  127. */
  128. /**
  129. * Set to enable driver to expose unconfigured disk to kernel
  130. */
  131. static int megaraid_expose_unconf_disks = 0;
  132. module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
  133. MODULE_PARM_DESC(unconf_disks,
  134. "Set to expose unconfigured disks to kernel (default=0)");
  135. /**
  136. * driver wait time if the adapter's mailbox is busy
  137. */
  138. static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  139. module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
  140. MODULE_PARM_DESC(busy_wait,
  141. "Max wait for mailbox in microseconds if busy (default=10)");
  142. /**
  143. * number of sectors per IO command
  144. */
  145. static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
  146. module_param_named(max_sectors, megaraid_max_sectors, int, 0);
  147. MODULE_PARM_DESC(max_sectors,
  148. "Maximum number of sectors per IO command (default=128)");
  149. /**
  150. * number of commands per logical unit
  151. */
  152. static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
  153. module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
  154. MODULE_PARM_DESC(cmd_per_lun,
  155. "Maximum number of commands per logical unit (default=64)");
  156. /**
  157. * Fast driver load option, skip scanning for physical devices during load.
  158. * This would result in non-disk devices being skipped during driver load
  159. * time. These can be later added though, using /proc/scsi/scsi
  160. */
  161. static unsigned int megaraid_fast_load = 0;
  162. module_param_named(fast_load, megaraid_fast_load, int, 0);
  163. MODULE_PARM_DESC(fast_load,
  164. "Faster loading of the driver, skips physical devices! (default=0)");
  165. /**
  166. * mraid_debug level - threshold for amount of information to be displayed by
  167. * the driver. This level can be changed through modules parameters, ioctl or
  168. * sysfs/proc interface. By default, print the announcement messages only.
  169. */
  170. int mraid_debug_level = CL_ANN;
  171. module_param_named(debug_level, mraid_debug_level, int, 0);
  172. MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
  173. /*
  174. * ### global data ###
  175. */
  176. static uint8_t megaraid_mbox_version[8] =
  177. { 0x02, 0x20, 0x04, 0x06, 3, 7, 20, 5 };
  178. /*
  179. * PCI table for all supported controllers.
  180. */
  181. static struct pci_device_id pci_id_table_g[] = {
  182. {
  183. PCI_VENDOR_ID_DELL,
  184. PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
  185. PCI_VENDOR_ID_DELL,
  186. PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
  187. },
  188. {
  189. PCI_VENDOR_ID_LSI_LOGIC,
  190. PCI_DEVICE_ID_PERC4_SC,
  191. PCI_VENDOR_ID_DELL,
  192. PCI_SUBSYS_ID_PERC4_SC,
  193. },
  194. {
  195. PCI_VENDOR_ID_LSI_LOGIC,
  196. PCI_DEVICE_ID_PERC4_DC,
  197. PCI_VENDOR_ID_DELL,
  198. PCI_SUBSYS_ID_PERC4_DC,
  199. },
  200. {
  201. PCI_VENDOR_ID_LSI_LOGIC,
  202. PCI_DEVICE_ID_VERDE,
  203. PCI_ANY_ID,
  204. PCI_ANY_ID,
  205. },
  206. {
  207. PCI_VENDOR_ID_DELL,
  208. PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
  209. PCI_VENDOR_ID_DELL,
  210. PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
  211. },
  212. {
  213. PCI_VENDOR_ID_DELL,
  214. PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
  215. PCI_VENDOR_ID_DELL,
  216. PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
  217. },
  218. {
  219. PCI_VENDOR_ID_DELL,
  220. PCI_DEVICE_ID_PERC4E_DI_KOBUK,
  221. PCI_VENDOR_ID_DELL,
  222. PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
  223. },
  224. {
  225. PCI_VENDOR_ID_DELL,
  226. PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
  227. PCI_VENDOR_ID_DELL,
  228. PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
  229. },
  230. {
  231. PCI_VENDOR_ID_DELL,
  232. PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
  233. PCI_VENDOR_ID_DELL,
  234. PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
  235. },
  236. {
  237. PCI_VENDOR_ID_DELL,
  238. PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
  239. PCI_VENDOR_ID_DELL,
  240. PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
  241. },
  242. {
  243. PCI_VENDOR_ID_LSI_LOGIC,
  244. PCI_DEVICE_ID_DOBSON,
  245. PCI_ANY_ID,
  246. PCI_ANY_ID,
  247. },
  248. {
  249. PCI_VENDOR_ID_AMI,
  250. PCI_DEVICE_ID_AMI_MEGARAID3,
  251. PCI_ANY_ID,
  252. PCI_ANY_ID,
  253. },
  254. {
  255. PCI_VENDOR_ID_LSI_LOGIC,
  256. PCI_DEVICE_ID_AMI_MEGARAID3,
  257. PCI_ANY_ID,
  258. PCI_ANY_ID,
  259. },
  260. {
  261. PCI_VENDOR_ID_LSI_LOGIC,
  262. PCI_DEVICE_ID_LINDSAY,
  263. PCI_ANY_ID,
  264. PCI_ANY_ID,
  265. },
  266. {0} /* Terminating entry */
  267. };
  268. MODULE_DEVICE_TABLE(pci, pci_id_table_g);
  269. static struct pci_driver megaraid_pci_driver_g = {
  270. .name = "megaraid",
  271. .id_table = pci_id_table_g,
  272. .probe = megaraid_probe_one,
  273. .remove = __devexit_p(megaraid_detach_one),
  274. .shutdown = megaraid_mbox_shutdown,
  275. };
  276. // definitions for the device attributes for exporting logical drive number
  277. // for a scsi address (Host, Channel, Id, Lun)
  278. CLASS_DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
  279. NULL);
  280. // Host template initializer for megaraid mbox sysfs device attributes
  281. static struct class_device_attribute *megaraid_shost_attrs[] = {
  282. &class_device_attr_megaraid_mbox_app_hndl,
  283. NULL,
  284. };
  285. DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
  286. // Host template initializer for megaraid mbox sysfs device attributes
  287. static struct device_attribute *megaraid_sdev_attrs[] = {
  288. &dev_attr_megaraid_mbox_ld,
  289. NULL,
  290. };
  291. /**
  292. * megaraid_change_queue_depth - Change the device's queue depth
  293. * @sdev: scsi device struct
  294. * @qdepth: depth to set
  295. *
  296. * Return value:
  297. * actual depth set
  298. **/
  299. static int megaraid_change_queue_depth(struct scsi_device *sdev, int qdepth)
  300. {
  301. if (qdepth > MBOX_MAX_SCSI_CMDS)
  302. qdepth = MBOX_MAX_SCSI_CMDS;
  303. scsi_adjust_queue_depth(sdev, 0, qdepth);
  304. return sdev->queue_depth;
  305. }
  306. /*
  307. * Scsi host template for megaraid unified driver
  308. */
  309. static struct scsi_host_template megaraid_template_g = {
  310. .module = THIS_MODULE,
  311. .name = "LSI Logic MegaRAID driver",
  312. .proc_name = "megaraid",
  313. .queuecommand = megaraid_queue_command,
  314. .eh_abort_handler = megaraid_abort_handler,
  315. .eh_device_reset_handler = megaraid_reset_handler,
  316. .eh_bus_reset_handler = megaraid_reset_handler,
  317. .eh_host_reset_handler = megaraid_reset_handler,
  318. .change_queue_depth = megaraid_change_queue_depth,
  319. .use_clustering = ENABLE_CLUSTERING,
  320. .sdev_attrs = megaraid_sdev_attrs,
  321. .shost_attrs = megaraid_shost_attrs,
  322. };
  323. /**
  324. * megaraid_init - module load hook
  325. *
  326. * We register ourselves as hotplug enabled module and let PCI subsystem
  327. * discover our adaters
  328. **/
  329. static int __init
  330. megaraid_init(void)
  331. {
  332. int rval;
  333. // Announce the driver version
  334. con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
  335. MEGARAID_EXT_VERSION));
  336. // check validity of module parameters
  337. if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
  338. con_log(CL_ANN, (KERN_WARNING
  339. "megaraid mailbox: max commands per lun reset to %d\n",
  340. MBOX_MAX_SCSI_CMDS));
  341. megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
  342. }
  343. // register as a PCI hot-plug driver module
  344. rval = pci_register_driver(&megaraid_pci_driver_g);
  345. if (rval < 0) {
  346. con_log(CL_ANN, (KERN_WARNING
  347. "megaraid: could not register hotplug support.\n"));
  348. }
  349. return rval;
  350. }
  351. /**
  352. * megaraid_exit - driver unload entry point
  353. *
  354. * We simply unwrap the megaraid_init routine here
  355. */
  356. static void __exit
  357. megaraid_exit(void)
  358. {
  359. con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
  360. // unregister as PCI hotplug driver
  361. pci_unregister_driver(&megaraid_pci_driver_g);
  362. return;
  363. }
  364. /**
  365. * megaraid_probe_one - PCI hotplug entry point
  366. * @param pdev : handle to this controller's PCI configuration space
  367. * @param id : pci device id of the class of controllers
  368. *
  369. * This routine should be called whenever a new adapter is detected by the
  370. * PCI hotplug susbsytem.
  371. **/
  372. static int __devinit
  373. megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  374. {
  375. adapter_t *adapter;
  376. // detected a new controller
  377. con_log(CL_ANN, (KERN_INFO
  378. "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  379. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  380. pdev->subsystem_device));
  381. con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
  382. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
  383. if (pci_enable_device(pdev)) {
  384. con_log(CL_ANN, (KERN_WARNING
  385. "megaraid: pci_enable_device failed\n"));
  386. return -ENODEV;
  387. }
  388. // Enable bus-mastering on this controller
  389. pci_set_master(pdev);
  390. // Allocate the per driver initialization structure
  391. adapter = kmalloc(sizeof(adapter_t), GFP_KERNEL);
  392. if (adapter == NULL) {
  393. con_log(CL_ANN, (KERN_WARNING
  394. "megaraid: out of memory, %s %d.\n", __FUNCTION__, __LINE__));
  395. goto out_probe_one;
  396. }
  397. memset(adapter, 0, sizeof(adapter_t));
  398. // set up PCI related soft state and other pre-known parameters
  399. adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
  400. adapter->irq = pdev->irq;
  401. adapter->pdev = pdev;
  402. atomic_set(&adapter->being_detached, 0);
  403. // Setup the default DMA mask. This would be changed later on
  404. // depending on hardware capabilities
  405. if (pci_set_dma_mask(adapter->pdev, DMA_32BIT_MASK) != 0) {
  406. con_log(CL_ANN, (KERN_WARNING
  407. "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
  408. goto out_free_adapter;
  409. }
  410. // Initialize the synchronization lock for kernel and LLD
  411. spin_lock_init(&adapter->lock);
  412. // Initialize the command queues: the list of free SCBs and the list
  413. // of pending SCBs.
  414. INIT_LIST_HEAD(&adapter->kscb_pool);
  415. spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
  416. INIT_LIST_HEAD(&adapter->pend_list);
  417. spin_lock_init(PENDING_LIST_LOCK(adapter));
  418. INIT_LIST_HEAD(&adapter->completed_list);
  419. spin_lock_init(COMPLETED_LIST_LOCK(adapter));
  420. // Start the mailbox based controller
  421. if (megaraid_init_mbox(adapter) != 0) {
  422. con_log(CL_ANN, (KERN_WARNING
  423. "megaraid: maibox adapter did not initialize\n"));
  424. goto out_free_adapter;
  425. }
  426. // Register with LSI Common Management Module
  427. if (megaraid_cmm_register(adapter) != 0) {
  428. con_log(CL_ANN, (KERN_WARNING
  429. "megaraid: could not register with management module\n"));
  430. goto out_fini_mbox;
  431. }
  432. // setup adapter handle in PCI soft state
  433. pci_set_drvdata(pdev, adapter);
  434. // attach with scsi mid-layer
  435. if (megaraid_io_attach(adapter) != 0) {
  436. con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
  437. goto out_cmm_unreg;
  438. }
  439. return 0;
  440. out_cmm_unreg:
  441. pci_set_drvdata(pdev, NULL);
  442. megaraid_cmm_unregister(adapter);
  443. out_fini_mbox:
  444. megaraid_fini_mbox(adapter);
  445. out_free_adapter:
  446. kfree(adapter);
  447. out_probe_one:
  448. pci_disable_device(pdev);
  449. return -ENODEV;
  450. }
  451. /**
  452. * megaraid_detach_one - release the framework resources and call LLD release
  453. * routine
  454. * @param pdev : handle for our PCI cofiguration space
  455. *
  456. * This routine is called during driver unload. We free all the allocated
  457. * resources and call the corresponding LLD so that it can also release all
  458. * its resources.
  459. *
  460. * This routine is also called from the PCI hotplug system
  461. **/
  462. static void
  463. megaraid_detach_one(struct pci_dev *pdev)
  464. {
  465. adapter_t *adapter;
  466. struct Scsi_Host *host;
  467. // Start a rollback on this adapter
  468. adapter = pci_get_drvdata(pdev);
  469. if (!adapter) {
  470. con_log(CL_ANN, (KERN_CRIT
  471. "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  472. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  473. pdev->subsystem_device));
  474. return;
  475. }
  476. else {
  477. con_log(CL_ANN, (KERN_NOTICE
  478. "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  479. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  480. pdev->subsystem_device));
  481. }
  482. host = adapter->host;
  483. // do not allow any more requests from the management module for this
  484. // adapter.
  485. // FIXME: How do we account for the request which might still be
  486. // pending with us?
  487. atomic_set(&adapter->being_detached, 1);
  488. // detach from the IO sub-system
  489. megaraid_io_detach(adapter);
  490. // reset the device state in the PCI structure. We check this
  491. // condition when we enter here. If the device state is NULL,
  492. // that would mean the device has already been removed
  493. pci_set_drvdata(pdev, NULL);
  494. // Unregister from common management module
  495. //
  496. // FIXME: this must return success or failure for conditions if there
  497. // is a command pending with LLD or not.
  498. megaraid_cmm_unregister(adapter);
  499. // finalize the mailbox based controller and release all resources
  500. megaraid_fini_mbox(adapter);
  501. kfree(adapter);
  502. scsi_host_put(host);
  503. pci_disable_device(pdev);
  504. return;
  505. }
  506. /**
  507. * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
  508. * @param device : generice driver model device
  509. *
  510. * Shutdown notification, perform flush cache
  511. */
  512. static void
  513. megaraid_mbox_shutdown(struct pci_dev *pdev)
  514. {
  515. adapter_t *adapter = pci_get_drvdata(pdev);
  516. static int counter;
  517. if (!adapter) {
  518. con_log(CL_ANN, (KERN_WARNING
  519. "megaraid: null device in shutdown\n"));
  520. return;
  521. }
  522. // flush caches now
  523. con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
  524. counter++));
  525. megaraid_mbox_flush_cache(adapter);
  526. con_log(CL_ANN, ("done\n"));
  527. }
  528. /**
  529. * megaraid_io_attach - attach a device with the IO subsystem
  530. * @param adapter : controller's soft state
  531. *
  532. * Attach this device with the IO subsystem
  533. **/
  534. static int
  535. megaraid_io_attach(adapter_t *adapter)
  536. {
  537. struct Scsi_Host *host;
  538. // Initialize SCSI Host structure
  539. host = scsi_host_alloc(&megaraid_template_g, 8);
  540. if (!host) {
  541. con_log(CL_ANN, (KERN_WARNING
  542. "megaraid mbox: scsi_register failed\n"));
  543. return -1;
  544. }
  545. SCSIHOST2ADAP(host) = (caddr_t)adapter;
  546. adapter->host = host;
  547. host->irq = adapter->irq;
  548. host->unique_id = adapter->unique_id;
  549. host->can_queue = adapter->max_cmds;
  550. host->this_id = adapter->init_id;
  551. host->sg_tablesize = adapter->sglen;
  552. host->max_sectors = adapter->max_sectors;
  553. host->cmd_per_lun = adapter->cmd_per_lun;
  554. host->max_channel = adapter->max_channel;
  555. host->max_id = adapter->max_target;
  556. host->max_lun = adapter->max_lun;
  557. // notify mid-layer about the new controller
  558. if (scsi_add_host(host, &adapter->pdev->dev)) {
  559. con_log(CL_ANN, (KERN_WARNING
  560. "megaraid mbox: scsi_add_host failed\n"));
  561. scsi_host_put(host);
  562. return -1;
  563. }
  564. scsi_scan_host(host);
  565. return 0;
  566. }
  567. /**
  568. * megaraid_io_detach - detach a device from the IO subsystem
  569. * @param adapter : controller's soft state
  570. *
  571. * Detach this device from the IO subsystem
  572. **/
  573. static void
  574. megaraid_io_detach(adapter_t *adapter)
  575. {
  576. struct Scsi_Host *host;
  577. con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
  578. host = adapter->host;
  579. scsi_remove_host(host);
  580. return;
  581. }
  582. /*
  583. * START: Mailbox Low Level Driver
  584. *
  585. * This is section specific to the single mailbox based controllers
  586. */
  587. /**
  588. * megaraid_init_mbox - initialize controller
  589. * @param adapter - our soft state
  590. *
  591. * . Allocate 16-byte aligned mailbox memory for firmware handshake
  592. * . Allocate controller's memory resources
  593. * . Find out all initialization data
  594. * . Allocate memory required for all the commands
  595. * . Use internal library of FW routines, build up complete soft state
  596. */
  597. static int __devinit
  598. megaraid_init_mbox(adapter_t *adapter)
  599. {
  600. struct pci_dev *pdev;
  601. mraid_device_t *raid_dev;
  602. int i;
  603. adapter->ito = MBOX_TIMEOUT;
  604. pdev = adapter->pdev;
  605. /*
  606. * Allocate and initialize the init data structure for mailbox
  607. * controllers
  608. */
  609. raid_dev = kmalloc(sizeof(mraid_device_t), GFP_KERNEL);
  610. if (raid_dev == NULL) return -1;
  611. memset(raid_dev, 0, sizeof(mraid_device_t));
  612. /*
  613. * Attach the adapter soft state to raid device soft state
  614. */
  615. adapter->raid_device = (caddr_t)raid_dev;
  616. raid_dev->fast_load = megaraid_fast_load;
  617. // our baseport
  618. raid_dev->baseport = pci_resource_start(pdev, 0);
  619. if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
  620. con_log(CL_ANN, (KERN_WARNING
  621. "megaraid: mem region busy\n"));
  622. goto out_free_raid_dev;
  623. }
  624. raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
  625. if (!raid_dev->baseaddr) {
  626. con_log(CL_ANN, (KERN_WARNING
  627. "megaraid: could not map hba memory\n") );
  628. goto out_release_regions;
  629. }
  630. //
  631. // Setup the rest of the soft state using the library of FW routines
  632. //
  633. // request IRQ and register the interrupt service routine
  634. if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
  635. adapter)) {
  636. con_log(CL_ANN, (KERN_WARNING
  637. "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
  638. goto out_iounmap;
  639. }
  640. // initialize the mutual exclusion lock for the mailbox
  641. spin_lock_init(&raid_dev->mailbox_lock);
  642. // allocate memory required for commands
  643. if (megaraid_alloc_cmd_packets(adapter) != 0) {
  644. goto out_free_irq;
  645. }
  646. // Product info
  647. if (megaraid_mbox_product_info(adapter) != 0) {
  648. goto out_alloc_cmds;
  649. }
  650. // Do we support extended CDBs
  651. adapter->max_cdb_sz = 10;
  652. if (megaraid_mbox_extended_cdb(adapter) == 0) {
  653. adapter->max_cdb_sz = 16;
  654. }
  655. /*
  656. * Do we support cluster environment, if we do, what is the initiator
  657. * id.
  658. * NOTE: In a non-cluster aware firmware environment, the LLD should
  659. * return 7 as initiator id.
  660. */
  661. adapter->ha = 0;
  662. adapter->init_id = -1;
  663. if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
  664. adapter->ha = 1;
  665. }
  666. /*
  667. * Prepare the device ids array to have the mapping between the kernel
  668. * device address and megaraid device address.
  669. * We export the physical devices on their actual addresses. The
  670. * logical drives are exported on a virtual SCSI channel
  671. */
  672. megaraid_mbox_setup_device_map(adapter);
  673. // If the firmware supports random deletion, update the device id map
  674. if (megaraid_mbox_support_random_del(adapter)) {
  675. // Change the logical drives numbers in device_ids array one
  676. // slot in device_ids is reserved for target id, that's why
  677. // "<=" below
  678. for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
  679. adapter->device_ids[adapter->max_channel][i] += 0x80;
  680. }
  681. adapter->device_ids[adapter->max_channel][adapter->init_id] =
  682. 0xFF;
  683. raid_dev->random_del_supported = 1;
  684. }
  685. /*
  686. * find out the maximum number of scatter-gather elements supported by
  687. * this firmware
  688. */
  689. adapter->sglen = megaraid_mbox_get_max_sg(adapter);
  690. // enumerate RAID and SCSI channels so that all devices on SCSI
  691. // channels can later be exported, including disk devices
  692. megaraid_mbox_enum_raid_scsi(adapter);
  693. /*
  694. * Other parameters required by upper layer
  695. *
  696. * maximum number of sectors per IO command
  697. */
  698. adapter->max_sectors = megaraid_max_sectors;
  699. /*
  700. * number of queued commands per LUN.
  701. */
  702. adapter->cmd_per_lun = megaraid_cmd_per_lun;
  703. /*
  704. * Allocate resources required to issue FW calls, when sysfs is
  705. * accessed
  706. */
  707. if (megaraid_sysfs_alloc_resources(adapter) != 0) {
  708. goto out_alloc_cmds;
  709. }
  710. // Set the DMA mask to 64-bit. All supported controllers as capable of
  711. // DMA in this range
  712. if (pci_set_dma_mask(adapter->pdev, DMA_64BIT_MASK) != 0) {
  713. con_log(CL_ANN, (KERN_WARNING
  714. "megaraid: could not set DMA mask for 64-bit.\n"));
  715. goto out_free_sysfs_res;
  716. }
  717. // setup tasklet for DPC
  718. tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
  719. (unsigned long)adapter);
  720. con_log(CL_DLEVEL1, (KERN_INFO
  721. "megaraid mbox hba successfully initialized\n"));
  722. return 0;
  723. out_free_sysfs_res:
  724. megaraid_sysfs_free_resources(adapter);
  725. out_alloc_cmds:
  726. megaraid_free_cmd_packets(adapter);
  727. out_free_irq:
  728. free_irq(adapter->irq, adapter);
  729. out_iounmap:
  730. iounmap(raid_dev->baseaddr);
  731. out_release_regions:
  732. pci_release_regions(pdev);
  733. out_free_raid_dev:
  734. kfree(raid_dev);
  735. return -1;
  736. }
  737. /**
  738. * megaraid_fini_mbox - undo controller initialization
  739. * @param adapter : our soft state
  740. */
  741. static void
  742. megaraid_fini_mbox(adapter_t *adapter)
  743. {
  744. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  745. // flush all caches
  746. megaraid_mbox_flush_cache(adapter);
  747. tasklet_kill(&adapter->dpc_h);
  748. megaraid_sysfs_free_resources(adapter);
  749. megaraid_free_cmd_packets(adapter);
  750. free_irq(adapter->irq, adapter);
  751. iounmap(raid_dev->baseaddr);
  752. pci_release_regions(adapter->pdev);
  753. kfree(raid_dev);
  754. return;
  755. }
  756. /**
  757. * megaraid_alloc_cmd_packets - allocate shared mailbox
  758. * @param adapter : soft state of the raid controller
  759. *
  760. * Allocate and align the shared mailbox. This maibox is used to issue
  761. * all the commands. For IO based controllers, the mailbox is also regsitered
  762. * with the FW. Allocate memory for all commands as well.
  763. * This is our big allocator
  764. */
  765. static int
  766. megaraid_alloc_cmd_packets(adapter_t *adapter)
  767. {
  768. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  769. struct pci_dev *pdev;
  770. unsigned long align;
  771. scb_t *scb;
  772. mbox_ccb_t *ccb;
  773. struct mraid_pci_blk *epthru_pci_blk;
  774. struct mraid_pci_blk *sg_pci_blk;
  775. struct mraid_pci_blk *mbox_pci_blk;
  776. int i;
  777. pdev = adapter->pdev;
  778. /*
  779. * Setup the mailbox
  780. * Allocate the common 16-byte aligned memory for the handshake
  781. * mailbox.
  782. */
  783. raid_dev->una_mbox64 = pci_alloc_consistent(adapter->pdev,
  784. sizeof(mbox64_t), &raid_dev->una_mbox64_dma);
  785. if (!raid_dev->una_mbox64) {
  786. con_log(CL_ANN, (KERN_WARNING
  787. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  788. __LINE__));
  789. return -1;
  790. }
  791. memset(raid_dev->una_mbox64, 0, sizeof(mbox64_t));
  792. /*
  793. * Align the mailbox at 16-byte boundary
  794. */
  795. raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
  796. raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
  797. (~0UL ^ 0xFUL));
  798. raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
  799. align = ((void *)raid_dev->mbox -
  800. ((void *)&raid_dev->una_mbox64->mbox32));
  801. raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
  802. align;
  803. // Allocate memory for commands issued internally
  804. adapter->ibuf = pci_alloc_consistent(pdev, MBOX_IBUF_SIZE,
  805. &adapter->ibuf_dma_h);
  806. if (!adapter->ibuf) {
  807. con_log(CL_ANN, (KERN_WARNING
  808. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  809. __LINE__));
  810. goto out_free_common_mbox;
  811. }
  812. memset(adapter->ibuf, 0, MBOX_IBUF_SIZE);
  813. // Allocate memory for our SCSI Command Blocks and their associated
  814. // memory
  815. /*
  816. * Allocate memory for the base list of scb. Later allocate memory for
  817. * CCBs and embedded components of each CCB and point the pointers in
  818. * scb to the allocated components
  819. * NOTE: The code to allocate SCB will be duplicated in all the LLD
  820. * since the calling routine does not yet know the number of available
  821. * commands.
  822. */
  823. adapter->kscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_SCSI_CMDS,
  824. GFP_KERNEL);
  825. if (adapter->kscb_list == NULL) {
  826. con_log(CL_ANN, (KERN_WARNING
  827. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  828. __LINE__));
  829. goto out_free_ibuf;
  830. }
  831. memset(adapter->kscb_list, 0, sizeof(scb_t) * MBOX_MAX_SCSI_CMDS);
  832. // memory allocation for our command packets
  833. if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
  834. con_log(CL_ANN, (KERN_WARNING
  835. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  836. __LINE__));
  837. goto out_free_scb_list;
  838. }
  839. // Adjust the scb pointers and link in the free pool
  840. epthru_pci_blk = raid_dev->epthru_pool;
  841. sg_pci_blk = raid_dev->sg_pool;
  842. mbox_pci_blk = raid_dev->mbox_pool;
  843. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  844. scb = adapter->kscb_list + i;
  845. ccb = raid_dev->ccb_list + i;
  846. ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
  847. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  848. ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
  849. ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
  850. // make sure the mailbox is aligned properly
  851. if (ccb->mbox_dma_h & 0x0F) {
  852. con_log(CL_ANN, (KERN_CRIT
  853. "megaraid mbox: not aligned on 16-bytes\n"));
  854. goto out_teardown_dma_pools;
  855. }
  856. ccb->epthru = (mraid_epassthru_t *)
  857. epthru_pci_blk[i].vaddr;
  858. ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
  859. ccb->pthru = (mraid_passthru_t *)ccb->epthru;
  860. ccb->pthru_dma_h = ccb->epthru_dma_h;
  861. ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
  862. ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
  863. ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
  864. scb->ccb = (caddr_t)ccb;
  865. scb->gp = 0;
  866. scb->sno = i; // command index
  867. scb->scp = NULL;
  868. scb->state = SCB_FREE;
  869. scb->dma_direction = PCI_DMA_NONE;
  870. scb->dma_type = MRAID_DMA_NONE;
  871. scb->dev_channel = -1;
  872. scb->dev_target = -1;
  873. // put scb in the free pool
  874. list_add_tail(&scb->list, &adapter->kscb_pool);
  875. }
  876. return 0;
  877. out_teardown_dma_pools:
  878. megaraid_mbox_teardown_dma_pools(adapter);
  879. out_free_scb_list:
  880. kfree(adapter->kscb_list);
  881. out_free_ibuf:
  882. pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
  883. adapter->ibuf_dma_h);
  884. out_free_common_mbox:
  885. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  886. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  887. return -1;
  888. }
  889. /**
  890. * megaraid_free_cmd_packets - free memory
  891. * @param adapter : soft state of the raid controller
  892. *
  893. * Release memory resources allocated for commands
  894. */
  895. static void
  896. megaraid_free_cmd_packets(adapter_t *adapter)
  897. {
  898. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  899. megaraid_mbox_teardown_dma_pools(adapter);
  900. kfree(adapter->kscb_list);
  901. pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
  902. (void *)adapter->ibuf, adapter->ibuf_dma_h);
  903. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  904. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  905. return;
  906. }
  907. /**
  908. * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
  909. * @param adapter : HBA soft state
  910. *
  911. * setup the dma pools for mailbox, passthru and extended passthru structures,
  912. * and scatter-gather lists
  913. */
  914. static int
  915. megaraid_mbox_setup_dma_pools(adapter_t *adapter)
  916. {
  917. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  918. struct mraid_pci_blk *epthru_pci_blk;
  919. struct mraid_pci_blk *sg_pci_blk;
  920. struct mraid_pci_blk *mbox_pci_blk;
  921. int i;
  922. // Allocate memory for 16-bytes aligned mailboxes
  923. raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
  924. adapter->pdev,
  925. sizeof(mbox64_t) + 16,
  926. 16, 0);
  927. if (raid_dev->mbox_pool_handle == NULL) {
  928. goto fail_setup_dma_pool;
  929. }
  930. mbox_pci_blk = raid_dev->mbox_pool;
  931. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  932. mbox_pci_blk[i].vaddr = pci_pool_alloc(
  933. raid_dev->mbox_pool_handle,
  934. GFP_KERNEL,
  935. &mbox_pci_blk[i].dma_addr);
  936. if (!mbox_pci_blk[i].vaddr) {
  937. goto fail_setup_dma_pool;
  938. }
  939. }
  940. /*
  941. * Allocate memory for each embedded passthru strucuture pointer
  942. * Request for a 128 bytes aligned structure for each passthru command
  943. * structure
  944. * Since passthru and extended passthru commands are exclusive, they
  945. * share common memory pool. Passthru structures piggyback on memory
  946. * allocted to extended passthru since passthru is smaller of the two
  947. */
  948. raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
  949. adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
  950. if (raid_dev->epthru_pool_handle == NULL) {
  951. goto fail_setup_dma_pool;
  952. }
  953. epthru_pci_blk = raid_dev->epthru_pool;
  954. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  955. epthru_pci_blk[i].vaddr = pci_pool_alloc(
  956. raid_dev->epthru_pool_handle,
  957. GFP_KERNEL,
  958. &epthru_pci_blk[i].dma_addr);
  959. if (!epthru_pci_blk[i].vaddr) {
  960. goto fail_setup_dma_pool;
  961. }
  962. }
  963. // Allocate memory for each scatter-gather list. Request for 512 bytes
  964. // alignment for each sg list
  965. raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
  966. adapter->pdev,
  967. sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
  968. 512, 0);
  969. if (raid_dev->sg_pool_handle == NULL) {
  970. goto fail_setup_dma_pool;
  971. }
  972. sg_pci_blk = raid_dev->sg_pool;
  973. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  974. sg_pci_blk[i].vaddr = pci_pool_alloc(
  975. raid_dev->sg_pool_handle,
  976. GFP_KERNEL,
  977. &sg_pci_blk[i].dma_addr);
  978. if (!sg_pci_blk[i].vaddr) {
  979. goto fail_setup_dma_pool;
  980. }
  981. }
  982. return 0;
  983. fail_setup_dma_pool:
  984. megaraid_mbox_teardown_dma_pools(adapter);
  985. return -1;
  986. }
  987. /**
  988. * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
  989. * @param adapter : HBA soft state
  990. *
  991. * teardown the dma pool for mailbox, passthru and extended passthru
  992. * structures, and scatter-gather lists
  993. */
  994. static void
  995. megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
  996. {
  997. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  998. struct mraid_pci_blk *epthru_pci_blk;
  999. struct mraid_pci_blk *sg_pci_blk;
  1000. struct mraid_pci_blk *mbox_pci_blk;
  1001. int i;
  1002. sg_pci_blk = raid_dev->sg_pool;
  1003. for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
  1004. pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
  1005. sg_pci_blk[i].dma_addr);
  1006. }
  1007. if (raid_dev->sg_pool_handle)
  1008. pci_pool_destroy(raid_dev->sg_pool_handle);
  1009. epthru_pci_blk = raid_dev->epthru_pool;
  1010. for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
  1011. pci_pool_free(raid_dev->epthru_pool_handle,
  1012. epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
  1013. }
  1014. if (raid_dev->epthru_pool_handle)
  1015. pci_pool_destroy(raid_dev->epthru_pool_handle);
  1016. mbox_pci_blk = raid_dev->mbox_pool;
  1017. for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
  1018. pci_pool_free(raid_dev->mbox_pool_handle,
  1019. mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
  1020. }
  1021. if (raid_dev->mbox_pool_handle)
  1022. pci_pool_destroy(raid_dev->mbox_pool_handle);
  1023. return;
  1024. }
  1025. /**
  1026. * megaraid_alloc_scb - detach and return a scb from the free list
  1027. * @adapter : controller's soft state
  1028. *
  1029. * return the scb from the head of the free list. NULL if there are none
  1030. * available
  1031. **/
  1032. static scb_t *
  1033. megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
  1034. {
  1035. struct list_head *head = &adapter->kscb_pool;
  1036. scb_t *scb = NULL;
  1037. unsigned long flags;
  1038. // detach scb from free pool
  1039. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1040. if (list_empty(head)) {
  1041. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1042. return NULL;
  1043. }
  1044. scb = list_entry(head->next, scb_t, list);
  1045. list_del_init(&scb->list);
  1046. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1047. scb->state = SCB_ACTIVE;
  1048. scb->scp = scp;
  1049. scb->dma_type = MRAID_DMA_NONE;
  1050. return scb;
  1051. }
  1052. /**
  1053. * megaraid_dealloc_scb - return the scb to the free pool
  1054. * @adapter : controller's soft state
  1055. * @scb : scb to be freed
  1056. *
  1057. * return the scb back to the free list of scbs. The caller must 'flush' the
  1058. * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
  1059. * NOTE NOTE: Make sure the scb is not on any list before calling this
  1060. * routine.
  1061. **/
  1062. static inline void
  1063. megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
  1064. {
  1065. unsigned long flags;
  1066. // put scb in the free pool
  1067. scb->state = SCB_FREE;
  1068. scb->scp = NULL;
  1069. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1070. list_add(&scb->list, &adapter->kscb_pool);
  1071. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1072. return;
  1073. }
  1074. /**
  1075. * megaraid_mbox_mksgl - make the scatter-gather list
  1076. * @adapter - controller's soft state
  1077. * @scb - scsi control block
  1078. *
  1079. * prepare the scatter-gather list
  1080. */
  1081. static int
  1082. megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
  1083. {
  1084. struct scatterlist *sgl;
  1085. mbox_ccb_t *ccb;
  1086. struct page *page;
  1087. unsigned long offset;
  1088. struct scsi_cmnd *scp;
  1089. int sgcnt;
  1090. int i;
  1091. scp = scb->scp;
  1092. ccb = (mbox_ccb_t *)scb->ccb;
  1093. // no mapping required if no data to be transferred
  1094. if (!scp->request_buffer || !scp->request_bufflen)
  1095. return 0;
  1096. if (!scp->use_sg) { /* scatter-gather list not used */
  1097. page = virt_to_page(scp->request_buffer);
  1098. offset = ((unsigned long)scp->request_buffer & ~PAGE_MASK);
  1099. ccb->buf_dma_h = pci_map_page(adapter->pdev, page, offset,
  1100. scp->request_bufflen,
  1101. scb->dma_direction);
  1102. scb->dma_type = MRAID_DMA_WBUF;
  1103. /*
  1104. * We need to handle special 64-bit commands that need a
  1105. * minimum of 1 SG
  1106. */
  1107. sgcnt = 1;
  1108. ccb->sgl64[0].address = ccb->buf_dma_h;
  1109. ccb->sgl64[0].length = scp->request_bufflen;
  1110. return sgcnt;
  1111. }
  1112. sgl = (struct scatterlist *)scp->request_buffer;
  1113. // The number of sg elements returned must not exceed our limit
  1114. sgcnt = pci_map_sg(adapter->pdev, sgl, scp->use_sg,
  1115. scb->dma_direction);
  1116. if (sgcnt > adapter->sglen) {
  1117. con_log(CL_ANN, (KERN_CRIT
  1118. "megaraid critical: too many sg elements:%d\n",
  1119. sgcnt));
  1120. BUG();
  1121. }
  1122. scb->dma_type = MRAID_DMA_WSG;
  1123. for (i = 0; i < sgcnt; i++, sgl++) {
  1124. ccb->sgl64[i].address = sg_dma_address(sgl);
  1125. ccb->sgl64[i].length = sg_dma_len(sgl);
  1126. }
  1127. // Return count of SG nodes
  1128. return sgcnt;
  1129. }
  1130. /**
  1131. * mbox_post_cmd - issue a mailbox command
  1132. * @adapter - controller's soft state
  1133. * @scb - command to be issued
  1134. *
  1135. * post the command to the controller if mailbox is availble.
  1136. */
  1137. static int
  1138. mbox_post_cmd(adapter_t *adapter, scb_t *scb)
  1139. {
  1140. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1141. mbox64_t *mbox64;
  1142. mbox_t *mbox;
  1143. mbox_ccb_t *ccb;
  1144. unsigned long flags;
  1145. unsigned int i = 0;
  1146. ccb = (mbox_ccb_t *)scb->ccb;
  1147. mbox = raid_dev->mbox;
  1148. mbox64 = raid_dev->mbox64;
  1149. /*
  1150. * Check for busy mailbox. If it is, return failure - the caller
  1151. * should retry later.
  1152. */
  1153. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1154. if (unlikely(mbox->busy)) {
  1155. do {
  1156. udelay(1);
  1157. i++;
  1158. rmb();
  1159. } while(mbox->busy && (i < max_mbox_busy_wait));
  1160. if (mbox->busy) {
  1161. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1162. return -1;
  1163. }
  1164. }
  1165. // Copy this command's mailbox data into "adapter's" mailbox
  1166. memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
  1167. mbox->cmdid = scb->sno;
  1168. adapter->outstanding_cmds++;
  1169. if (scb->dma_direction == PCI_DMA_TODEVICE) {
  1170. if (!scb->scp->use_sg) { // sg list not used
  1171. pci_dma_sync_single_for_device(adapter->pdev,
  1172. ccb->buf_dma_h,
  1173. scb->scp->request_bufflen,
  1174. PCI_DMA_TODEVICE);
  1175. }
  1176. else {
  1177. pci_dma_sync_sg_for_device(adapter->pdev,
  1178. scb->scp->request_buffer,
  1179. scb->scp->use_sg, PCI_DMA_TODEVICE);
  1180. }
  1181. }
  1182. mbox->busy = 1; // Set busy
  1183. mbox->poll = 0;
  1184. mbox->ack = 0;
  1185. wmb();
  1186. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  1187. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1188. return 0;
  1189. }
  1190. /**
  1191. * megaraid_queue_command - generic queue entry point for all LLDs
  1192. * @scp : pointer to the scsi command to be executed
  1193. * @done : callback routine to be called after the cmd has be completed
  1194. *
  1195. * Queue entry point for mailbox based controllers.
  1196. */
  1197. static int
  1198. megaraid_queue_command(struct scsi_cmnd *scp, void (* done)(struct scsi_cmnd *))
  1199. {
  1200. adapter_t *adapter;
  1201. scb_t *scb;
  1202. int if_busy;
  1203. adapter = SCP2ADAPTER(scp);
  1204. scp->scsi_done = done;
  1205. scp->result = 0;
  1206. /*
  1207. * Allocate and build a SCB request
  1208. * if_busy flag will be set if megaraid_mbox_build_cmd() command could
  1209. * not allocate scb. We will return non-zero status in that case.
  1210. * NOTE: scb can be null even though certain commands completed
  1211. * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
  1212. * return 0 in that case, and we would do the callback right away.
  1213. */
  1214. if_busy = 0;
  1215. scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
  1216. if (!scb) { // command already completed
  1217. done(scp);
  1218. return 0;
  1219. }
  1220. megaraid_mbox_runpendq(adapter, scb);
  1221. return if_busy;
  1222. }
  1223. /**
  1224. * megaraid_mbox_build_cmd - transform the mid-layer scsi command to megaraid
  1225. * firmware lingua
  1226. * @adapter - controller's soft state
  1227. * @scp - mid-layer scsi command pointer
  1228. * @busy - set if request could not be completed because of lack of
  1229. * resources
  1230. *
  1231. * convert the command issued by mid-layer to format understood by megaraid
  1232. * firmware. We also complete certain command without sending them to firmware
  1233. */
  1234. static scb_t *
  1235. megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
  1236. {
  1237. mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
  1238. int channel;
  1239. int target;
  1240. int islogical;
  1241. mbox_ccb_t *ccb;
  1242. mraid_passthru_t *pthru;
  1243. mbox64_t *mbox64;
  1244. mbox_t *mbox;
  1245. scb_t *scb;
  1246. char skip[] = "skipping";
  1247. char scan[] = "scanning";
  1248. char *ss;
  1249. /*
  1250. * Get the appropriate device map for the device this command is
  1251. * intended for
  1252. */
  1253. MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
  1254. /*
  1255. * Logical drive commands
  1256. */
  1257. if (islogical) {
  1258. switch (scp->cmnd[0]) {
  1259. case TEST_UNIT_READY:
  1260. /*
  1261. * Do we support clustering and is the support enabled
  1262. * If no, return success always
  1263. */
  1264. if (!adapter->ha) {
  1265. scp->result = (DID_OK << 16);
  1266. return NULL;
  1267. }
  1268. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1269. scp->result = (DID_ERROR << 16);
  1270. *busy = 1;
  1271. return NULL;
  1272. }
  1273. scb->dma_direction = scp->sc_data_direction;
  1274. scb->dev_channel = 0xFF;
  1275. scb->dev_target = target;
  1276. ccb = (mbox_ccb_t *)scb->ccb;
  1277. /*
  1278. * The command id will be provided by the command
  1279. * issuance routine
  1280. */
  1281. ccb->raw_mbox[0] = CLUSTER_CMD;
  1282. ccb->raw_mbox[2] = RESERVATION_STATUS;
  1283. ccb->raw_mbox[3] = target;
  1284. return scb;
  1285. case MODE_SENSE:
  1286. if (scp->use_sg) {
  1287. struct scatterlist *sgl;
  1288. caddr_t vaddr;
  1289. sgl = (struct scatterlist *)scp->request_buffer;
  1290. if (sgl->page) {
  1291. vaddr = (caddr_t)
  1292. (page_address((&sgl[0])->page)
  1293. + (&sgl[0])->offset);
  1294. memset(vaddr, 0, scp->cmnd[4]);
  1295. }
  1296. else {
  1297. con_log(CL_ANN, (KERN_WARNING
  1298. "megaraid mailbox: invalid sg:%d\n",
  1299. __LINE__));
  1300. }
  1301. }
  1302. else {
  1303. memset(scp->request_buffer, 0, scp->cmnd[4]);
  1304. }
  1305. scp->result = (DID_OK << 16);
  1306. return NULL;
  1307. case INQUIRY:
  1308. /*
  1309. * Display the channel scan for logical drives
  1310. * Do not display scan for a channel if already done.
  1311. */
  1312. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1313. con_log(CL_ANN, (KERN_INFO
  1314. "scsi[%d]: scanning scsi channel %d",
  1315. adapter->host->host_no,
  1316. SCP2CHANNEL(scp)));
  1317. con_log(CL_ANN, (
  1318. " [virtual] for logical drives\n"));
  1319. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1320. }
  1321. /* Fall through */
  1322. case READ_CAPACITY:
  1323. /*
  1324. * Do not allow LUN > 0 for logical drives and
  1325. * requests for more than 40 logical drives
  1326. */
  1327. if (SCP2LUN(scp)) {
  1328. scp->result = (DID_BAD_TARGET << 16);
  1329. return NULL;
  1330. }
  1331. if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
  1332. scp->result = (DID_BAD_TARGET << 16);
  1333. return NULL;
  1334. }
  1335. /* Allocate a SCB and initialize passthru */
  1336. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1337. scp->result = (DID_ERROR << 16);
  1338. *busy = 1;
  1339. return NULL;
  1340. }
  1341. ccb = (mbox_ccb_t *)scb->ccb;
  1342. scb->dev_channel = 0xFF;
  1343. scb->dev_target = target;
  1344. pthru = ccb->pthru;
  1345. mbox = ccb->mbox;
  1346. mbox64 = ccb->mbox64;
  1347. pthru->timeout = 0;
  1348. pthru->ars = 1;
  1349. pthru->reqsenselen = 14;
  1350. pthru->islogical = 1;
  1351. pthru->logdrv = target;
  1352. pthru->cdblen = scp->cmd_len;
  1353. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1354. mbox->cmd = MBOXCMD_PASSTHRU64;
  1355. scb->dma_direction = scp->sc_data_direction;
  1356. pthru->dataxferlen = scp->request_bufflen;
  1357. pthru->dataxferaddr = ccb->sgl_dma_h;
  1358. pthru->numsge = megaraid_mbox_mksgl(adapter,
  1359. scb);
  1360. mbox->xferaddr = 0xFFFFFFFF;
  1361. mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
  1362. mbox64->xferaddr_hi = 0;
  1363. return scb;
  1364. case READ_6:
  1365. case WRITE_6:
  1366. case READ_10:
  1367. case WRITE_10:
  1368. case READ_12:
  1369. case WRITE_12:
  1370. /*
  1371. * Allocate a SCB and initialize mailbox
  1372. */
  1373. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1374. scp->result = (DID_ERROR << 16);
  1375. *busy = 1;
  1376. return NULL;
  1377. }
  1378. ccb = (mbox_ccb_t *)scb->ccb;
  1379. scb->dev_channel = 0xFF;
  1380. scb->dev_target = target;
  1381. mbox = ccb->mbox;
  1382. mbox64 = ccb->mbox64;
  1383. mbox->logdrv = target;
  1384. /*
  1385. * A little HACK: 2nd bit is zero for all scsi read
  1386. * commands and is set for all scsi write commands
  1387. */
  1388. mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
  1389. MBOXCMD_LREAD64 ;
  1390. /*
  1391. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1392. */
  1393. if (scp->cmd_len == 6) {
  1394. mbox->numsectors = (uint32_t)scp->cmnd[4];
  1395. mbox->lba =
  1396. ((uint32_t)scp->cmnd[1] << 16) |
  1397. ((uint32_t)scp->cmnd[2] << 8) |
  1398. (uint32_t)scp->cmnd[3];
  1399. mbox->lba &= 0x1FFFFF;
  1400. }
  1401. /*
  1402. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1403. */
  1404. else if (scp->cmd_len == 10) {
  1405. mbox->numsectors =
  1406. (uint32_t)scp->cmnd[8] |
  1407. ((uint32_t)scp->cmnd[7] << 8);
  1408. mbox->lba =
  1409. ((uint32_t)scp->cmnd[2] << 24) |
  1410. ((uint32_t)scp->cmnd[3] << 16) |
  1411. ((uint32_t)scp->cmnd[4] << 8) |
  1412. (uint32_t)scp->cmnd[5];
  1413. }
  1414. /*
  1415. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1416. */
  1417. else if (scp->cmd_len == 12) {
  1418. mbox->lba =
  1419. ((uint32_t)scp->cmnd[2] << 24) |
  1420. ((uint32_t)scp->cmnd[3] << 16) |
  1421. ((uint32_t)scp->cmnd[4] << 8) |
  1422. (uint32_t)scp->cmnd[5];
  1423. mbox->numsectors =
  1424. ((uint32_t)scp->cmnd[6] << 24) |
  1425. ((uint32_t)scp->cmnd[7] << 16) |
  1426. ((uint32_t)scp->cmnd[8] << 8) |
  1427. (uint32_t)scp->cmnd[9];
  1428. }
  1429. else {
  1430. con_log(CL_ANN, (KERN_WARNING
  1431. "megaraid: unsupported CDB length\n"));
  1432. megaraid_dealloc_scb(adapter, scb);
  1433. scp->result = (DID_ERROR << 16);
  1434. return NULL;
  1435. }
  1436. scb->dma_direction = scp->sc_data_direction;
  1437. // Calculate Scatter-Gather info
  1438. mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
  1439. mbox->numsge = megaraid_mbox_mksgl(adapter,
  1440. scb);
  1441. mbox->xferaddr = 0xFFFFFFFF;
  1442. mbox64->xferaddr_hi = 0;
  1443. return scb;
  1444. case RESERVE:
  1445. case RELEASE:
  1446. /*
  1447. * Do we support clustering and is the support enabled
  1448. */
  1449. if (!adapter->ha) {
  1450. scp->result = (DID_BAD_TARGET << 16);
  1451. return NULL;
  1452. }
  1453. /*
  1454. * Allocate a SCB and initialize mailbox
  1455. */
  1456. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1457. scp->result = (DID_ERROR << 16);
  1458. *busy = 1;
  1459. return NULL;
  1460. }
  1461. ccb = (mbox_ccb_t *)scb->ccb;
  1462. scb->dev_channel = 0xFF;
  1463. scb->dev_target = target;
  1464. ccb->raw_mbox[0] = CLUSTER_CMD;
  1465. ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
  1466. RESERVE_LD : RELEASE_LD;
  1467. ccb->raw_mbox[3] = target;
  1468. scb->dma_direction = scp->sc_data_direction;
  1469. return scb;
  1470. default:
  1471. scp->result = (DID_BAD_TARGET << 16);
  1472. return NULL;
  1473. }
  1474. }
  1475. else { // Passthru device commands
  1476. // Do not allow access to target id > 15 or LUN > 7
  1477. if (target > 15 || SCP2LUN(scp) > 7) {
  1478. scp->result = (DID_BAD_TARGET << 16);
  1479. return NULL;
  1480. }
  1481. // if fast load option was set and scan for last device is
  1482. // over, reset the fast_load flag so that during a possible
  1483. // next scan, devices can be made available
  1484. if (rdev->fast_load && (target == 15) &&
  1485. (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
  1486. con_log(CL_ANN, (KERN_INFO
  1487. "megaraid[%d]: physical device scan re-enabled\n",
  1488. adapter->host->host_no));
  1489. rdev->fast_load = 0;
  1490. }
  1491. /*
  1492. * Display the channel scan for physical devices
  1493. */
  1494. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1495. ss = rdev->fast_load ? skip : scan;
  1496. con_log(CL_ANN, (KERN_INFO
  1497. "scsi[%d]: %s scsi channel %d [Phy %d]",
  1498. adapter->host->host_no, ss, SCP2CHANNEL(scp),
  1499. channel));
  1500. con_log(CL_ANN, (
  1501. " for non-raid devices\n"));
  1502. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1503. }
  1504. // disable channel sweep if fast load option given
  1505. if (rdev->fast_load) {
  1506. scp->result = (DID_BAD_TARGET << 16);
  1507. return NULL;
  1508. }
  1509. // Allocate a SCB and initialize passthru
  1510. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1511. scp->result = (DID_ERROR << 16);
  1512. *busy = 1;
  1513. return NULL;
  1514. }
  1515. ccb = (mbox_ccb_t *)scb->ccb;
  1516. scb->dev_channel = channel;
  1517. scb->dev_target = target;
  1518. scb->dma_direction = scp->sc_data_direction;
  1519. mbox = ccb->mbox;
  1520. mbox64 = ccb->mbox64;
  1521. // Does this firmware support extended CDBs
  1522. if (adapter->max_cdb_sz == 16) {
  1523. mbox->cmd = MBOXCMD_EXTPTHRU;
  1524. megaraid_mbox_prepare_epthru(adapter, scb, scp);
  1525. mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
  1526. mbox64->xferaddr_hi = 0;
  1527. mbox->xferaddr = 0xFFFFFFFF;
  1528. }
  1529. else {
  1530. mbox->cmd = MBOXCMD_PASSTHRU64;
  1531. megaraid_mbox_prepare_pthru(adapter, scb, scp);
  1532. mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
  1533. mbox64->xferaddr_hi = 0;
  1534. mbox->xferaddr = 0xFFFFFFFF;
  1535. }
  1536. return scb;
  1537. }
  1538. // NOT REACHED
  1539. }
  1540. /**
  1541. * megaraid_mbox_runpendq - execute commands queued in the pending queue
  1542. * @adapter : controller's soft state
  1543. * @scb : SCB to be queued in the pending list
  1544. *
  1545. * scan the pending list for commands which are not yet issued and try to
  1546. * post to the controller. The SCB can be a null pointer, which would indicate
  1547. * no SCB to be queue, just try to execute the ones in the pending list.
  1548. *
  1549. * NOTE: We do not actually traverse the pending list. The SCBs are plucked
  1550. * out from the head of the pending list. If it is successfully issued, the
  1551. * next SCB is at the head now.
  1552. */
  1553. static void
  1554. megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
  1555. {
  1556. scb_t *scb;
  1557. unsigned long flags;
  1558. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1559. if (scb_q) {
  1560. scb_q->state = SCB_PENDQ;
  1561. list_add_tail(&scb_q->list, &adapter->pend_list);
  1562. }
  1563. // if the adapter in not in quiescent mode, post the commands to FW
  1564. if (adapter->quiescent) {
  1565. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1566. return;
  1567. }
  1568. while (!list_empty(&adapter->pend_list)) {
  1569. assert_spin_locked(PENDING_LIST_LOCK(adapter));
  1570. scb = list_entry(adapter->pend_list.next, scb_t, list);
  1571. // remove the scb from the pending list and try to
  1572. // issue. If we are unable to issue it, put back in
  1573. // the pending list and return
  1574. list_del_init(&scb->list);
  1575. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1576. // if mailbox was busy, return SCB back to pending
  1577. // list. Make sure to add at the head, since that's
  1578. // where it would have been removed from
  1579. scb->state = SCB_ISSUED;
  1580. if (mbox_post_cmd(adapter, scb) != 0) {
  1581. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1582. scb->state = SCB_PENDQ;
  1583. list_add(&scb->list, &adapter->pend_list);
  1584. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  1585. flags);
  1586. return;
  1587. }
  1588. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1589. }
  1590. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1591. return;
  1592. }
  1593. /**
  1594. * megaraid_mbox_prepare_pthru - prepare a command for physical devices
  1595. * @adapter - pointer to controller's soft state
  1596. * @scb - scsi control block
  1597. * @scp - scsi command from the mid-layer
  1598. *
  1599. * prepare a command for the scsi physical devices
  1600. */
  1601. static void
  1602. megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
  1603. struct scsi_cmnd *scp)
  1604. {
  1605. mbox_ccb_t *ccb;
  1606. mraid_passthru_t *pthru;
  1607. uint8_t channel;
  1608. uint8_t target;
  1609. ccb = (mbox_ccb_t *)scb->ccb;
  1610. pthru = ccb->pthru;
  1611. channel = scb->dev_channel;
  1612. target = scb->dev_target;
  1613. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1614. pthru->timeout = 4;
  1615. pthru->ars = 1;
  1616. pthru->islogical = 0;
  1617. pthru->channel = 0;
  1618. pthru->target = (channel << 4) | target;
  1619. pthru->logdrv = SCP2LUN(scp);
  1620. pthru->reqsenselen = 14;
  1621. pthru->cdblen = scp->cmd_len;
  1622. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1623. if (scp->request_bufflen) {
  1624. pthru->dataxferlen = scp->request_bufflen;
  1625. pthru->dataxferaddr = ccb->sgl_dma_h;
  1626. pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1627. }
  1628. else {
  1629. pthru->dataxferaddr = 0;
  1630. pthru->dataxferlen = 0;
  1631. pthru->numsge = 0;
  1632. }
  1633. return;
  1634. }
  1635. /**
  1636. * megaraid_mbox_prepare_epthru - prepare a command for physical devices
  1637. * @adapter - pointer to controller's soft state
  1638. * @scb - scsi control block
  1639. * @scp - scsi command from the mid-layer
  1640. *
  1641. * prepare a command for the scsi physical devices. This rountine prepares
  1642. * commands for devices which can take extended CDBs (>10 bytes)
  1643. */
  1644. static void
  1645. megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
  1646. struct scsi_cmnd *scp)
  1647. {
  1648. mbox_ccb_t *ccb;
  1649. mraid_epassthru_t *epthru;
  1650. uint8_t channel;
  1651. uint8_t target;
  1652. ccb = (mbox_ccb_t *)scb->ccb;
  1653. epthru = ccb->epthru;
  1654. channel = scb->dev_channel;
  1655. target = scb->dev_target;
  1656. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1657. epthru->timeout = 4;
  1658. epthru->ars = 1;
  1659. epthru->islogical = 0;
  1660. epthru->channel = 0;
  1661. epthru->target = (channel << 4) | target;
  1662. epthru->logdrv = SCP2LUN(scp);
  1663. epthru->reqsenselen = 14;
  1664. epthru->cdblen = scp->cmd_len;
  1665. memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
  1666. if (scp->request_bufflen) {
  1667. epthru->dataxferlen = scp->request_bufflen;
  1668. epthru->dataxferaddr = ccb->sgl_dma_h;
  1669. epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1670. }
  1671. else {
  1672. epthru->dataxferaddr = 0;
  1673. epthru->dataxferlen = 0;
  1674. epthru->numsge = 0;
  1675. }
  1676. return;
  1677. }
  1678. /**
  1679. * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
  1680. * @adapter - controller's soft state
  1681. *
  1682. * Interrupt ackrowledgement sequence for memory mapped HBAs. Find out the
  1683. * completed command and put them on the completed list for later processing.
  1684. *
  1685. * Returns: 1 if the interrupt is valid, 0 otherwise
  1686. */
  1687. static int
  1688. megaraid_ack_sequence(adapter_t *adapter)
  1689. {
  1690. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1691. mbox_t *mbox;
  1692. scb_t *scb;
  1693. uint8_t nstatus;
  1694. uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
  1695. struct list_head clist;
  1696. int handled;
  1697. uint32_t dword;
  1698. unsigned long flags;
  1699. int i, j;
  1700. mbox = raid_dev->mbox;
  1701. // move the SCBs from the firmware completed array to our local list
  1702. INIT_LIST_HEAD(&clist);
  1703. // loop till F/W has more commands for us to complete
  1704. handled = 0;
  1705. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1706. do {
  1707. /*
  1708. * Check if a valid interrupt is pending. If found, force the
  1709. * interrupt line low.
  1710. */
  1711. dword = RDOUTDOOR(raid_dev);
  1712. if (dword != 0x10001234) break;
  1713. handled = 1;
  1714. WROUTDOOR(raid_dev, 0x10001234);
  1715. nstatus = 0;
  1716. // wait for valid numstatus to post
  1717. for (i = 0; i < 0xFFFFF; i++) {
  1718. if (mbox->numstatus != 0xFF) {
  1719. nstatus = mbox->numstatus;
  1720. break;
  1721. }
  1722. rmb();
  1723. }
  1724. mbox->numstatus = 0xFF;
  1725. adapter->outstanding_cmds -= nstatus;
  1726. for (i = 0; i < nstatus; i++) {
  1727. // wait for valid command index to post
  1728. for (j = 0; j < 0xFFFFF; j++) {
  1729. if (mbox->completed[i] != 0xFF) break;
  1730. rmb();
  1731. }
  1732. completed[i] = mbox->completed[i];
  1733. mbox->completed[i] = 0xFF;
  1734. if (completed[i] == 0xFF) {
  1735. con_log(CL_ANN, (KERN_CRIT
  1736. "megaraid: command posting timed out\n"));
  1737. BUG();
  1738. continue;
  1739. }
  1740. // Get SCB associated with this command id
  1741. if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
  1742. // a cmm command
  1743. scb = adapter->uscb_list + (completed[i] -
  1744. MBOX_MAX_SCSI_CMDS);
  1745. }
  1746. else {
  1747. // an os command
  1748. scb = adapter->kscb_list + completed[i];
  1749. }
  1750. scb->status = mbox->status;
  1751. list_add_tail(&scb->list, &clist);
  1752. }
  1753. // Acknowledge interrupt
  1754. WRINDOOR(raid_dev, 0x02);
  1755. } while(1);
  1756. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1757. // put the completed commands in the completed list. DPC would
  1758. // complete these commands later
  1759. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1760. list_splice(&clist, &adapter->completed_list);
  1761. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1762. // schedule the DPC if there is some work for it
  1763. if (handled)
  1764. tasklet_schedule(&adapter->dpc_h);
  1765. return handled;
  1766. }
  1767. /**
  1768. * megaraid_isr - isr for memory based mailbox based controllers
  1769. * @irq - irq
  1770. * @devp - pointer to our soft state
  1771. * @regs - unused
  1772. *
  1773. * Interrupt service routine for memory-mapped mailbox controllers.
  1774. */
  1775. static irqreturn_t
  1776. megaraid_isr(int irq, void *devp, struct pt_regs *regs)
  1777. {
  1778. adapter_t *adapter = devp;
  1779. int handled;
  1780. handled = megaraid_ack_sequence(adapter);
  1781. /* Loop through any pending requests */
  1782. if (!adapter->quiescent) {
  1783. megaraid_mbox_runpendq(adapter, NULL);
  1784. }
  1785. return IRQ_RETVAL(handled);
  1786. }
  1787. /**
  1788. * megaraid_mbox_sync_scb - sync kernel buffers
  1789. * @adapter : controller's soft state
  1790. * @scb : pointer to the resource packet
  1791. *
  1792. * DMA sync if required.
  1793. */
  1794. static void
  1795. megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
  1796. {
  1797. mbox_ccb_t *ccb;
  1798. ccb = (mbox_ccb_t *)scb->ccb;
  1799. switch (scb->dma_type) {
  1800. case MRAID_DMA_WBUF:
  1801. if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
  1802. pci_dma_sync_single_for_cpu(adapter->pdev,
  1803. ccb->buf_dma_h,
  1804. scb->scp->request_bufflen,
  1805. PCI_DMA_FROMDEVICE);
  1806. }
  1807. pci_unmap_page(adapter->pdev, ccb->buf_dma_h,
  1808. scb->scp->request_bufflen, scb->dma_direction);
  1809. break;
  1810. case MRAID_DMA_WSG:
  1811. if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
  1812. pci_dma_sync_sg_for_cpu(adapter->pdev,
  1813. scb->scp->request_buffer,
  1814. scb->scp->use_sg, PCI_DMA_FROMDEVICE);
  1815. }
  1816. pci_unmap_sg(adapter->pdev, scb->scp->request_buffer,
  1817. scb->scp->use_sg, scb->dma_direction);
  1818. break;
  1819. default:
  1820. break;
  1821. }
  1822. return;
  1823. }
  1824. /**
  1825. * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
  1826. * @devp : pointer to HBA soft state
  1827. *
  1828. * Pick up the commands from the completed list and send back to the owners.
  1829. * This is a reentrant function and does not assume any locks are held while
  1830. * it is being called.
  1831. */
  1832. static void
  1833. megaraid_mbox_dpc(unsigned long devp)
  1834. {
  1835. adapter_t *adapter = (adapter_t *)devp;
  1836. mraid_device_t *raid_dev;
  1837. struct list_head clist;
  1838. struct scatterlist *sgl;
  1839. scb_t *scb;
  1840. scb_t *tmp;
  1841. struct scsi_cmnd *scp;
  1842. mraid_passthru_t *pthru;
  1843. mraid_epassthru_t *epthru;
  1844. mbox_ccb_t *ccb;
  1845. int islogical;
  1846. int pdev_index;
  1847. int pdev_state;
  1848. mbox_t *mbox;
  1849. unsigned long flags;
  1850. uint8_t c;
  1851. int status;
  1852. uioc_t *kioc;
  1853. if (!adapter) return;
  1854. raid_dev = ADAP2RAIDDEV(adapter);
  1855. // move the SCBs from the completed list to our local list
  1856. INIT_LIST_HEAD(&clist);
  1857. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1858. list_splice_init(&adapter->completed_list, &clist);
  1859. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1860. list_for_each_entry_safe(scb, tmp, &clist, list) {
  1861. status = scb->status;
  1862. scp = scb->scp;
  1863. ccb = (mbox_ccb_t *)scb->ccb;
  1864. pthru = ccb->pthru;
  1865. epthru = ccb->epthru;
  1866. mbox = ccb->mbox;
  1867. // Make sure f/w has completed a valid command
  1868. if (scb->state != SCB_ISSUED) {
  1869. con_log(CL_ANN, (KERN_CRIT
  1870. "megaraid critical err: invalid command %d:%d:%p\n",
  1871. scb->sno, scb->state, scp));
  1872. BUG();
  1873. continue; // Must never happen!
  1874. }
  1875. // check for the management command and complete it right away
  1876. if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
  1877. scb->state = SCB_FREE;
  1878. scb->status = status;
  1879. // remove from local clist
  1880. list_del_init(&scb->list);
  1881. kioc = (uioc_t *)scb->gp;
  1882. kioc->status = 0;
  1883. megaraid_mbox_mm_done(adapter, scb);
  1884. continue;
  1885. }
  1886. // Was an abort issued for this command earlier
  1887. if (scb->state & SCB_ABORT) {
  1888. con_log(CL_ANN, (KERN_NOTICE
  1889. "megaraid: aborted cmd %lx[%x] completed\n",
  1890. scp->serial_number, scb->sno));
  1891. }
  1892. /*
  1893. * If the inquiry came of a disk drive which is not part of
  1894. * any RAID array, expose it to the kernel. For this to be
  1895. * enabled, user must set the "megaraid_expose_unconf_disks"
  1896. * flag to 1 by specifying it on module parameter list.
  1897. * This would enable data migration off drives from other
  1898. * configurations.
  1899. */
  1900. islogical = MRAID_IS_LOGICAL(adapter, scp);
  1901. if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
  1902. && IS_RAID_CH(raid_dev, scb->dev_channel)) {
  1903. if (scp->use_sg) {
  1904. sgl = (struct scatterlist *)
  1905. scp->request_buffer;
  1906. if (sgl->page) {
  1907. c = *(unsigned char *)
  1908. (page_address((&sgl[0])->page) +
  1909. (&sgl[0])->offset);
  1910. }
  1911. else {
  1912. con_log(CL_ANN, (KERN_WARNING
  1913. "megaraid mailbox: invalid sg:%d\n",
  1914. __LINE__));
  1915. c = 0;
  1916. }
  1917. }
  1918. else {
  1919. c = *(uint8_t *)scp->request_buffer;
  1920. }
  1921. if ((c & 0x1F ) == TYPE_DISK) {
  1922. pdev_index = (scb->dev_channel * 16) +
  1923. scb->dev_target;
  1924. pdev_state =
  1925. raid_dev->pdrv_state[pdev_index] & 0x0F;
  1926. if (pdev_state == PDRV_ONLINE ||
  1927. pdev_state == PDRV_FAILED ||
  1928. pdev_state == PDRV_RBLD ||
  1929. pdev_state == PDRV_HOTSPARE ||
  1930. megaraid_expose_unconf_disks == 0) {
  1931. status = 0xF0;
  1932. }
  1933. }
  1934. }
  1935. // Convert MegaRAID status to Linux error code
  1936. switch (status) {
  1937. case 0x00:
  1938. scp->result = (DID_OK << 16);
  1939. break;
  1940. case 0x02:
  1941. /* set sense_buffer and result fields */
  1942. if (mbox->cmd == MBOXCMD_PASSTHRU ||
  1943. mbox->cmd == MBOXCMD_PASSTHRU64) {
  1944. memcpy(scp->sense_buffer, pthru->reqsensearea,
  1945. 14);
  1946. scp->result = DRIVER_SENSE << 24 |
  1947. DID_OK << 16 | CHECK_CONDITION << 1;
  1948. }
  1949. else {
  1950. if (mbox->cmd == MBOXCMD_EXTPTHRU) {
  1951. memcpy(scp->sense_buffer,
  1952. epthru->reqsensearea, 14);
  1953. scp->result = DRIVER_SENSE << 24 |
  1954. DID_OK << 16 |
  1955. CHECK_CONDITION << 1;
  1956. } else {
  1957. scp->sense_buffer[0] = 0x70;
  1958. scp->sense_buffer[2] = ABORTED_COMMAND;
  1959. scp->result = CHECK_CONDITION << 1;
  1960. }
  1961. }
  1962. break;
  1963. case 0x08:
  1964. scp->result = DID_BUS_BUSY << 16 | status;
  1965. break;
  1966. default:
  1967. /*
  1968. * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
  1969. * failed
  1970. */
  1971. if (scp->cmnd[0] == TEST_UNIT_READY) {
  1972. scp->result = DID_ERROR << 16 |
  1973. RESERVATION_CONFLICT << 1;
  1974. }
  1975. else
  1976. /*
  1977. * Error code returned is 1 if Reserve or Release
  1978. * failed or the input parameter is invalid
  1979. */
  1980. if (status == 1 && (scp->cmnd[0] == RESERVE ||
  1981. scp->cmnd[0] == RELEASE)) {
  1982. scp->result = DID_ERROR << 16 |
  1983. RESERVATION_CONFLICT << 1;
  1984. }
  1985. else {
  1986. scp->result = DID_BAD_TARGET << 16 | status;
  1987. }
  1988. }
  1989. // print a debug message for all failed commands
  1990. if (status) {
  1991. megaraid_mbox_display_scb(adapter, scb);
  1992. }
  1993. // Free our internal resources and call the mid-layer callback
  1994. // routine
  1995. megaraid_mbox_sync_scb(adapter, scb);
  1996. // remove from local clist
  1997. list_del_init(&scb->list);
  1998. // put back in free list
  1999. megaraid_dealloc_scb(adapter, scb);
  2000. // send the scsi packet back to kernel
  2001. scp->scsi_done(scp);
  2002. }
  2003. return;
  2004. }
  2005. /**
  2006. * megaraid_abort_handler - abort the scsi command
  2007. * @scp : command to be aborted
  2008. *
  2009. * Abort a previous SCSI request. Only commands on the pending list can be
  2010. * aborted. All the commands issued to the F/W must complete.
  2011. **/
  2012. static int
  2013. megaraid_abort_handler(struct scsi_cmnd *scp)
  2014. {
  2015. adapter_t *adapter;
  2016. mraid_device_t *raid_dev;
  2017. scb_t *scb;
  2018. scb_t *tmp;
  2019. int found;
  2020. unsigned long flags;
  2021. int i;
  2022. adapter = SCP2ADAPTER(scp);
  2023. raid_dev = ADAP2RAIDDEV(adapter);
  2024. con_log(CL_ANN, (KERN_WARNING
  2025. "megaraid: aborting-%ld cmd=%x <c=%d t=%d l=%d>\n",
  2026. scp->serial_number, scp->cmnd[0], SCP2CHANNEL(scp),
  2027. SCP2TARGET(scp), SCP2LUN(scp)));
  2028. // If FW has stopped responding, simply return failure
  2029. if (raid_dev->hw_error) {
  2030. con_log(CL_ANN, (KERN_NOTICE
  2031. "megaraid: hw error, not aborting\n"));
  2032. return FAILED;
  2033. }
  2034. // There might a race here, where the command was completed by the
  2035. // firmware and now it is on the completed list. Before we could
  2036. // complete the command to the kernel in dpc, the abort came.
  2037. // Find out if this is the case to avoid the race.
  2038. scb = NULL;
  2039. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  2040. list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
  2041. if (scb->scp == scp) { // Found command
  2042. list_del_init(&scb->list); // from completed list
  2043. con_log(CL_ANN, (KERN_WARNING
  2044. "megaraid: %ld:%d[%d:%d], abort from completed list\n",
  2045. scp->serial_number, scb->sno,
  2046. scb->dev_channel, scb->dev_target));
  2047. scp->result = (DID_ABORT << 16);
  2048. scp->scsi_done(scp);
  2049. megaraid_dealloc_scb(adapter, scb);
  2050. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
  2051. flags);
  2052. return SUCCESS;
  2053. }
  2054. }
  2055. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  2056. // Find out if this command is still on the pending list. If it is and
  2057. // was never issued, abort and return success. If the command is owned
  2058. // by the firmware, we must wait for it to complete by the FW.
  2059. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2060. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2061. if (scb->scp == scp) { // Found command
  2062. list_del_init(&scb->list); // from pending list
  2063. ASSERT(!(scb->state & SCB_ISSUED));
  2064. con_log(CL_ANN, (KERN_WARNING
  2065. "megaraid abort: %ld[%d:%d], driver owner\n",
  2066. scp->serial_number, scb->dev_channel,
  2067. scb->dev_target));
  2068. scp->result = (DID_ABORT << 16);
  2069. scp->scsi_done(scp);
  2070. megaraid_dealloc_scb(adapter, scb);
  2071. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  2072. flags);
  2073. return SUCCESS;
  2074. }
  2075. }
  2076. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2077. // Check do we even own this command, in which case this would be
  2078. // owned by the firmware. The only way to locate the FW scb is to
  2079. // traverse through the list of all SCB, since driver does not
  2080. // maintain these SCBs on any list
  2081. found = 0;
  2082. spin_lock_irq(&adapter->lock);
  2083. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  2084. scb = adapter->kscb_list + i;
  2085. if (scb->scp == scp) {
  2086. found = 1;
  2087. if (!(scb->state & SCB_ISSUED)) {
  2088. con_log(CL_ANN, (KERN_WARNING
  2089. "megaraid abort: %ld%d[%d:%d], invalid state\n",
  2090. scp->serial_number, scb->sno, scb->dev_channel,
  2091. scb->dev_target));
  2092. BUG();
  2093. }
  2094. else {
  2095. con_log(CL_ANN, (KERN_WARNING
  2096. "megaraid abort: %ld:%d[%d:%d], fw owner\n",
  2097. scp->serial_number, scb->sno, scb->dev_channel,
  2098. scb->dev_target));
  2099. }
  2100. }
  2101. }
  2102. spin_unlock_irq(&adapter->lock);
  2103. if (!found) {
  2104. con_log(CL_ANN, (KERN_WARNING
  2105. "megaraid abort: scsi cmd:%ld, do now own\n",
  2106. scp->serial_number));
  2107. // FIXME: Should there be a callback for this command?
  2108. return SUCCESS;
  2109. }
  2110. // We cannot actually abort a command owned by firmware, return
  2111. // failure and wait for reset. In host reset handler, we will find out
  2112. // if the HBA is still live
  2113. return FAILED;
  2114. }
  2115. /**
  2116. * megaraid_reset_handler - device reset hadler for mailbox based driver
  2117. * @scp : reference command
  2118. *
  2119. * Reset handler for the mailbox based controller. First try to find out if
  2120. * the FW is still live, in which case the outstanding commands counter mut go
  2121. * down to 0. If that happens, also issue the reservation reset command to
  2122. * relinquish (possible) reservations on the logical drives connected to this
  2123. * host
  2124. **/
  2125. static int
  2126. megaraid_reset_handler(struct scsi_cmnd *scp)
  2127. {
  2128. adapter_t *adapter;
  2129. scb_t *scb;
  2130. scb_t *tmp;
  2131. mraid_device_t *raid_dev;
  2132. unsigned long flags;
  2133. uint8_t raw_mbox[sizeof(mbox_t)];
  2134. int rval;
  2135. int recovery_window;
  2136. int recovering;
  2137. int i;
  2138. uioc_t *kioc;
  2139. adapter = SCP2ADAPTER(scp);
  2140. raid_dev = ADAP2RAIDDEV(adapter);
  2141. // return failure if adapter is not responding
  2142. if (raid_dev->hw_error) {
  2143. con_log(CL_ANN, (KERN_NOTICE
  2144. "megaraid: hw error, cannot reset\n"));
  2145. return FAILED;
  2146. }
  2147. // Under exceptional conditions, FW can take up to 3 minutes to
  2148. // complete command processing. Wait for additional 2 minutes for the
  2149. // pending commands counter to go down to 0. If it doesn't, let the
  2150. // controller be marked offline
  2151. // Also, reset all the commands currently owned by the driver
  2152. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2153. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2154. list_del_init(&scb->list); // from pending list
  2155. if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
  2156. con_log(CL_ANN, (KERN_WARNING
  2157. "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
  2158. scb->sno, scb->dev_channel, scb->dev_target));
  2159. scb->status = -1;
  2160. kioc = (uioc_t *)scb->gp;
  2161. kioc->status = -EFAULT;
  2162. megaraid_mbox_mm_done(adapter, scb);
  2163. } else {
  2164. if (scb->scp == scp) { // Found command
  2165. con_log(CL_ANN, (KERN_WARNING
  2166. "megaraid: %ld:%d[%d:%d], reset from pending list\n",
  2167. scp->serial_number, scb->sno,
  2168. scb->dev_channel, scb->dev_target));
  2169. } else {
  2170. con_log(CL_ANN, (KERN_WARNING
  2171. "megaraid: IO packet with %d[%d:%d] being reset\n",
  2172. scb->sno, scb->dev_channel, scb->dev_target));
  2173. }
  2174. scb->scp->result = (DID_RESET << 16);
  2175. scb->scp->scsi_done(scb->scp);
  2176. megaraid_dealloc_scb(adapter, scb);
  2177. }
  2178. }
  2179. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2180. if (adapter->outstanding_cmds) {
  2181. con_log(CL_ANN, (KERN_NOTICE
  2182. "megaraid: %d outstanding commands. Max wait %d sec\n",
  2183. adapter->outstanding_cmds,
  2184. (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
  2185. }
  2186. recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
  2187. recovering = adapter->outstanding_cmds;
  2188. for (i = 0; i < recovery_window; i++) {
  2189. megaraid_ack_sequence(adapter);
  2190. // print a message once every 5 seconds only
  2191. if (!(i % 5)) {
  2192. con_log(CL_ANN, (
  2193. "megaraid mbox: Wait for %d commands to complete:%d\n",
  2194. adapter->outstanding_cmds,
  2195. (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
  2196. }
  2197. // bailout if no recovery happended in reset time
  2198. if (adapter->outstanding_cmds == 0) {
  2199. break;
  2200. }
  2201. msleep(1000);
  2202. }
  2203. spin_lock(&adapter->lock);
  2204. // If still outstanding commands, bail out
  2205. if (adapter->outstanding_cmds) {
  2206. con_log(CL_ANN, (KERN_WARNING
  2207. "megaraid mbox: critical hardware error!\n"));
  2208. raid_dev->hw_error = 1;
  2209. rval = FAILED;
  2210. goto out;
  2211. }
  2212. else {
  2213. con_log(CL_ANN, (KERN_NOTICE
  2214. "megaraid mbox: reset sequence completed sucessfully\n"));
  2215. }
  2216. // If the controller supports clustering, reset reservations
  2217. if (!adapter->ha) {
  2218. rval = SUCCESS;
  2219. goto out;
  2220. }
  2221. // clear reservations if any
  2222. raw_mbox[0] = CLUSTER_CMD;
  2223. raw_mbox[2] = RESET_RESERVATIONS;
  2224. rval = SUCCESS;
  2225. if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
  2226. con_log(CL_ANN,
  2227. (KERN_INFO "megaraid: reservation reset\n"));
  2228. }
  2229. else {
  2230. rval = FAILED;
  2231. con_log(CL_ANN, (KERN_WARNING
  2232. "megaraid: reservation reset failed\n"));
  2233. }
  2234. out:
  2235. spin_unlock_irq(&adapter->lock);
  2236. return rval;
  2237. }
  2238. /*
  2239. * START: internal commands library
  2240. *
  2241. * This section of the driver has the common routine used by the driver and
  2242. * also has all the FW routines
  2243. */
  2244. /**
  2245. * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
  2246. * @adapter - controller's soft state
  2247. * @raw_mbox - the mailbox
  2248. *
  2249. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2250. * controllers
  2251. */
  2252. static int
  2253. mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
  2254. {
  2255. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2256. mbox64_t *mbox64;
  2257. mbox_t *mbox;
  2258. uint8_t status;
  2259. int i;
  2260. mbox64 = raid_dev->mbox64;
  2261. mbox = raid_dev->mbox;
  2262. /*
  2263. * Wait until mailbox is free
  2264. */
  2265. if (megaraid_busywait_mbox(raid_dev) != 0)
  2266. goto blocked_mailbox;
  2267. /*
  2268. * Copy mailbox data into host structure
  2269. */
  2270. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
  2271. mbox->cmdid = 0xFE;
  2272. mbox->busy = 1;
  2273. mbox->poll = 0;
  2274. mbox->ack = 0;
  2275. mbox->numstatus = 0xFF;
  2276. mbox->status = 0xFF;
  2277. wmb();
  2278. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2279. // wait for maximum 1 second for status to post. If the status is not
  2280. // available within 1 second, assume FW is initializing and wait
  2281. // for an extended amount of time
  2282. if (mbox->numstatus == 0xFF) { // status not yet available
  2283. udelay(25);
  2284. for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
  2285. rmb();
  2286. msleep(1);
  2287. }
  2288. if (i == 1000) {
  2289. con_log(CL_ANN, (KERN_NOTICE
  2290. "megaraid mailbox: wait for FW to boot "));
  2291. for (i = 0; (mbox->numstatus == 0xFF) &&
  2292. (i < MBOX_RESET_WAIT); i++) {
  2293. rmb();
  2294. con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
  2295. MBOX_RESET_WAIT - i));
  2296. msleep(1000);
  2297. }
  2298. if (i == MBOX_RESET_WAIT) {
  2299. con_log(CL_ANN, (
  2300. "\nmegaraid mailbox: status not available\n"));
  2301. return -1;
  2302. }
  2303. con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
  2304. }
  2305. }
  2306. // wait for maximum 1 second for poll semaphore
  2307. if (mbox->poll != 0x77) {
  2308. udelay(25);
  2309. for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
  2310. rmb();
  2311. msleep(1);
  2312. }
  2313. if (i == 1000) {
  2314. con_log(CL_ANN, (KERN_WARNING
  2315. "megaraid mailbox: could not get poll semaphore\n"));
  2316. return -1;
  2317. }
  2318. }
  2319. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2320. wmb();
  2321. // wait for maximum 1 second for acknowledgement
  2322. if (RDINDOOR(raid_dev) & 0x2) {
  2323. udelay(25);
  2324. for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
  2325. rmb();
  2326. msleep(1);
  2327. }
  2328. if (i == 1000) {
  2329. con_log(CL_ANN, (KERN_WARNING
  2330. "megaraid mailbox: could not acknowledge\n"));
  2331. return -1;
  2332. }
  2333. }
  2334. mbox->poll = 0;
  2335. mbox->ack = 0x77;
  2336. status = mbox->status;
  2337. // invalidate the completed command id array. After command
  2338. // completion, firmware would write the valid id.
  2339. mbox->numstatus = 0xFF;
  2340. mbox->status = 0xFF;
  2341. for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
  2342. mbox->completed[i] = 0xFF;
  2343. }
  2344. return status;
  2345. blocked_mailbox:
  2346. con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
  2347. return -1;
  2348. }
  2349. /**
  2350. * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
  2351. * @adapter - controller's soft state
  2352. * @raw_mbox - the mailbox
  2353. *
  2354. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2355. * controllers. This is a faster version of the synchronous command and
  2356. * therefore can be called in interrupt-context as well
  2357. */
  2358. static int
  2359. mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
  2360. {
  2361. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2362. mbox_t *mbox;
  2363. long i;
  2364. mbox = raid_dev->mbox;
  2365. // return immediately if the mailbox is busy
  2366. if (mbox->busy) return -1;
  2367. // Copy mailbox data into host structure
  2368. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
  2369. mbox->cmdid = 0xFE;
  2370. mbox->busy = 1;
  2371. mbox->poll = 0;
  2372. mbox->ack = 0;
  2373. mbox->numstatus = 0xFF;
  2374. mbox->status = 0xFF;
  2375. wmb();
  2376. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2377. for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
  2378. if (mbox->numstatus != 0xFF) break;
  2379. rmb();
  2380. udelay(MBOX_SYNC_DELAY_200);
  2381. }
  2382. if (i == MBOX_SYNC_WAIT_CNT) {
  2383. // We may need to re-calibrate the counter
  2384. con_log(CL_ANN, (KERN_CRIT
  2385. "megaraid: fast sync command timed out\n"));
  2386. }
  2387. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2388. wmb();
  2389. return mbox->status;
  2390. }
  2391. /**
  2392. * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
  2393. * @raid_dev - RAID device (HBA) soft state
  2394. *
  2395. * wait until the controller's mailbox is available to accept more commands.
  2396. * wait for at most 1 second
  2397. */
  2398. static int
  2399. megaraid_busywait_mbox(mraid_device_t *raid_dev)
  2400. {
  2401. mbox_t *mbox = raid_dev->mbox;
  2402. int i = 0;
  2403. if (mbox->busy) {
  2404. udelay(25);
  2405. for (i = 0; mbox->busy && i < 1000; i++)
  2406. msleep(1);
  2407. }
  2408. if (i < 1000) return 0;
  2409. else return -1;
  2410. }
  2411. /**
  2412. * megaraid_mbox_product_info - some static information about the controller
  2413. * @adapter - our soft state
  2414. *
  2415. * issue commands to the controller to grab some parameters required by our
  2416. * caller.
  2417. */
  2418. static int
  2419. megaraid_mbox_product_info(adapter_t *adapter)
  2420. {
  2421. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2422. mbox_t *mbox;
  2423. uint8_t raw_mbox[sizeof(mbox_t)];
  2424. mraid_pinfo_t *pinfo;
  2425. dma_addr_t pinfo_dma_h;
  2426. mraid_inquiry3_t *mraid_inq3;
  2427. int i;
  2428. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2429. mbox = (mbox_t *)raw_mbox;
  2430. /*
  2431. * Issue an ENQUIRY3 command to find out certain adapter parameters,
  2432. * e.g., max channels, max commands etc.
  2433. */
  2434. pinfo = pci_alloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2435. &pinfo_dma_h);
  2436. if (pinfo == NULL) {
  2437. con_log(CL_ANN, (KERN_WARNING
  2438. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  2439. __LINE__));
  2440. return -1;
  2441. }
  2442. memset(pinfo, 0, sizeof(mraid_pinfo_t));
  2443. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2444. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2445. raw_mbox[0] = FC_NEW_CONFIG;
  2446. raw_mbox[2] = NC_SUBOP_ENQUIRY3;
  2447. raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
  2448. // Issue the command
  2449. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2450. con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
  2451. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2452. pinfo, pinfo_dma_h);
  2453. return -1;
  2454. }
  2455. /*
  2456. * Collect information about state of each physical drive
  2457. * attached to the controller. We will expose all the disks
  2458. * which are not part of RAID
  2459. */
  2460. mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
  2461. for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
  2462. raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
  2463. }
  2464. /*
  2465. * Get product info for information like number of channels,
  2466. * maximum commands supported.
  2467. */
  2468. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2469. mbox->xferaddr = (uint32_t)pinfo_dma_h;
  2470. raw_mbox[0] = FC_NEW_CONFIG;
  2471. raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
  2472. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2473. con_log(CL_ANN, (KERN_WARNING
  2474. "megaraid: product info failed\n"));
  2475. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2476. pinfo, pinfo_dma_h);
  2477. return -1;
  2478. }
  2479. /*
  2480. * Setup some parameters for host, as required by our caller
  2481. */
  2482. adapter->max_channel = pinfo->nchannels;
  2483. /*
  2484. * we will export all the logical drives on a single channel.
  2485. * Add 1 since inquires do not come for inititor ID
  2486. */
  2487. adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
  2488. adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
  2489. /*
  2490. * These are the maximum outstanding commands for the scsi-layer
  2491. */
  2492. adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
  2493. memset(adapter->fw_version, 0, VERSION_SIZE);
  2494. memset(adapter->bios_version, 0, VERSION_SIZE);
  2495. memcpy(adapter->fw_version, pinfo->fw_version, 4);
  2496. adapter->fw_version[4] = 0;
  2497. memcpy(adapter->bios_version, pinfo->bios_version, 4);
  2498. adapter->bios_version[4] = 0;
  2499. con_log(CL_ANN, (KERN_NOTICE
  2500. "megaraid: fw version:[%s] bios version:[%s]\n",
  2501. adapter->fw_version, adapter->bios_version));
  2502. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
  2503. pinfo_dma_h);
  2504. return 0;
  2505. }
  2506. /**
  2507. * megaraid_mbox_extended_cdb - check for support for extended CDBs
  2508. * @adapter - soft state for the controller
  2509. *
  2510. * this routine check whether the controller in question supports extended
  2511. * ( > 10 bytes ) CDBs
  2512. */
  2513. static int
  2514. megaraid_mbox_extended_cdb(adapter_t *adapter)
  2515. {
  2516. mbox_t *mbox;
  2517. uint8_t raw_mbox[sizeof(mbox_t)];
  2518. int rval;
  2519. mbox = (mbox_t *)raw_mbox;
  2520. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2521. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2522. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2523. raw_mbox[0] = MAIN_MISC_OPCODE;
  2524. raw_mbox[2] = SUPPORT_EXT_CDB;
  2525. /*
  2526. * Issue the command
  2527. */
  2528. rval = 0;
  2529. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2530. rval = -1;
  2531. }
  2532. return rval;
  2533. }
  2534. /**
  2535. * megaraid_mbox_support_ha - Do we support clustering
  2536. * @adapter - soft state for the controller
  2537. * @init_id - ID of the initiator
  2538. *
  2539. * Determine if the firmware supports clustering and the ID of the initiator.
  2540. */
  2541. static int
  2542. megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
  2543. {
  2544. mbox_t *mbox;
  2545. uint8_t raw_mbox[sizeof(mbox_t)];
  2546. int rval;
  2547. mbox = (mbox_t *)raw_mbox;
  2548. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2549. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2550. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2551. raw_mbox[0] = GET_TARGET_ID;
  2552. // Issue the command
  2553. *init_id = 7;
  2554. rval = -1;
  2555. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2556. *init_id = *(uint8_t *)adapter->ibuf;
  2557. con_log(CL_ANN, (KERN_INFO
  2558. "megaraid: cluster firmware, initiator ID: %d\n",
  2559. *init_id));
  2560. rval = 0;
  2561. }
  2562. return rval;
  2563. }
  2564. /**
  2565. * megaraid_mbox_support_random_del - Do we support random deletion
  2566. * @adapter - soft state for the controller
  2567. *
  2568. * Determine if the firmware supports random deletion
  2569. * Return: 1 is operation supported, 0 otherwise
  2570. */
  2571. static int
  2572. megaraid_mbox_support_random_del(adapter_t *adapter)
  2573. {
  2574. mbox_t *mbox;
  2575. uint8_t raw_mbox[sizeof(mbox_t)];
  2576. int rval;
  2577. mbox = (mbox_t *)raw_mbox;
  2578. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2579. raw_mbox[0] = FC_DEL_LOGDRV;
  2580. raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  2581. // Issue the command
  2582. rval = 0;
  2583. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2584. con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
  2585. rval = 1;
  2586. }
  2587. return rval;
  2588. }
  2589. /**
  2590. * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
  2591. * @adapter - soft state for the controller
  2592. *
  2593. * Find out the maximum number of scatter-gather elements supported by the
  2594. * firmware
  2595. */
  2596. static int
  2597. megaraid_mbox_get_max_sg(adapter_t *adapter)
  2598. {
  2599. mbox_t *mbox;
  2600. uint8_t raw_mbox[sizeof(mbox_t)];
  2601. int nsg;
  2602. mbox = (mbox_t *)raw_mbox;
  2603. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2604. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2605. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2606. raw_mbox[0] = MAIN_MISC_OPCODE;
  2607. raw_mbox[2] = GET_MAX_SG_SUPPORT;
  2608. // Issue the command
  2609. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2610. nsg = *(uint8_t *)adapter->ibuf;
  2611. }
  2612. else {
  2613. nsg = MBOX_DEFAULT_SG_SIZE;
  2614. }
  2615. if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
  2616. return nsg;
  2617. }
  2618. /**
  2619. * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
  2620. * @adapter - soft state for the controller
  2621. *
  2622. * Enumerate the RAID and SCSI channels for ROMB platoforms so that channels
  2623. * can be exported as regular SCSI channels
  2624. */
  2625. static void
  2626. megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
  2627. {
  2628. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2629. mbox_t *mbox;
  2630. uint8_t raw_mbox[sizeof(mbox_t)];
  2631. mbox = (mbox_t *)raw_mbox;
  2632. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2633. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2634. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2635. raw_mbox[0] = CHNL_CLASS;
  2636. raw_mbox[2] = GET_CHNL_CLASS;
  2637. // Issue the command. If the command fails, all channels are RAID
  2638. // channels
  2639. raid_dev->channel_class = 0xFF;
  2640. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2641. raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
  2642. }
  2643. return;
  2644. }
  2645. /**
  2646. * megaraid_mbox_flush_cache - flush adapter and disks cache
  2647. * @param adapter : soft state for the controller
  2648. *
  2649. * Flush adapter cache followed by disks cache
  2650. */
  2651. static void
  2652. megaraid_mbox_flush_cache(adapter_t *adapter)
  2653. {
  2654. mbox_t *mbox;
  2655. uint8_t raw_mbox[sizeof(mbox_t)];
  2656. mbox = (mbox_t *)raw_mbox;
  2657. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2658. raw_mbox[0] = FLUSH_ADAPTER;
  2659. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2660. con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
  2661. }
  2662. raw_mbox[0] = FLUSH_SYSTEM;
  2663. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2664. con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
  2665. }
  2666. return;
  2667. }
  2668. /**
  2669. * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
  2670. * @param adapter : controllers' soft state
  2671. * @param scb : SCB to be displayed
  2672. * @param level : debug level for console print
  2673. *
  2674. * Diplay information about the given SCB iff the current debug level is
  2675. * verbose
  2676. */
  2677. static void
  2678. megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
  2679. {
  2680. mbox_ccb_t *ccb;
  2681. struct scsi_cmnd *scp;
  2682. mbox_t *mbox;
  2683. int level;
  2684. int i;
  2685. ccb = (mbox_ccb_t *)scb->ccb;
  2686. scp = scb->scp;
  2687. mbox = ccb->mbox;
  2688. level = CL_DLEVEL3;
  2689. con_log(level, (KERN_NOTICE
  2690. "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
  2691. mbox->cmd, scb->sno));
  2692. con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
  2693. mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
  2694. mbox->numsge));
  2695. if (!scp) return;
  2696. con_log(level, (KERN_NOTICE "scsi cmnd: "));
  2697. for (i = 0; i < scp->cmd_len; i++) {
  2698. con_log(level, ("%#2.02x ", scp->cmnd[i]));
  2699. }
  2700. con_log(level, ("\n"));
  2701. return;
  2702. }
  2703. /**
  2704. * megaraid_mbox_setup_device_map - manage device ids
  2705. * @adapter : Driver's soft state
  2706. *
  2707. * Manange the device ids to have an appropraite mapping between the kernel
  2708. * scsi addresses and megaraid scsi and logical drive addresses. We export
  2709. * scsi devices on their actual addresses, whereas the logical drives are
  2710. * exported on a virtual scsi channel.
  2711. **/
  2712. static void
  2713. megaraid_mbox_setup_device_map(adapter_t *adapter)
  2714. {
  2715. uint8_t c;
  2716. uint8_t t;
  2717. /*
  2718. * First fill the values on the logical drive channel
  2719. */
  2720. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2721. adapter->device_ids[adapter->max_channel][t] =
  2722. (t < adapter->init_id) ? t : t - 1;
  2723. adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
  2724. /*
  2725. * Fill the values on the physical devices channels
  2726. */
  2727. for (c = 0; c < adapter->max_channel; c++)
  2728. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2729. adapter->device_ids[c][t] = (c << 8) | t;
  2730. }
  2731. /*
  2732. * END: internal commands library
  2733. */
  2734. /*
  2735. * START: Interface for the common management module
  2736. *
  2737. * This is the module, which interfaces with the common mangement module to
  2738. * provide support for ioctl and sysfs
  2739. */
  2740. /**
  2741. * megaraid_cmm_register - register with the mangement module
  2742. * @param adapter : HBA soft state
  2743. *
  2744. * Register with the management module, which allows applications to issue
  2745. * ioctl calls to the drivers. This interface is used by the management module
  2746. * to setup sysfs support as well.
  2747. */
  2748. static int
  2749. megaraid_cmm_register(adapter_t *adapter)
  2750. {
  2751. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2752. mraid_mmadp_t adp;
  2753. scb_t *scb;
  2754. mbox_ccb_t *ccb;
  2755. int rval;
  2756. int i;
  2757. // Allocate memory for the base list of scb for management module.
  2758. adapter->uscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_USER_CMDS,
  2759. GFP_KERNEL);
  2760. if (adapter->uscb_list == NULL) {
  2761. con_log(CL_ANN, (KERN_WARNING
  2762. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  2763. __LINE__));
  2764. return -1;
  2765. }
  2766. memset(adapter->uscb_list, 0, sizeof(scb_t) * MBOX_MAX_USER_CMDS);
  2767. // Initialize the synchronization parameters for resources for
  2768. // commands for management module
  2769. INIT_LIST_HEAD(&adapter->uscb_pool);
  2770. spin_lock_init(USER_FREE_LIST_LOCK(adapter));
  2771. // link all the packets. Note, CCB for commands, coming from the
  2772. // commom management module, mailbox physical address are already
  2773. // setup by it. We just need placeholder for that in our local command
  2774. // control blocks
  2775. for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
  2776. scb = adapter->uscb_list + i;
  2777. ccb = raid_dev->uccb_list + i;
  2778. scb->ccb = (caddr_t)ccb;
  2779. ccb->mbox64 = raid_dev->umbox64 + i;
  2780. ccb->mbox = &ccb->mbox64->mbox32;
  2781. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  2782. scb->gp = 0;
  2783. // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
  2784. // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
  2785. scb->sno = i + MBOX_MAX_SCSI_CMDS;
  2786. scb->scp = NULL;
  2787. scb->state = SCB_FREE;
  2788. scb->dma_direction = PCI_DMA_NONE;
  2789. scb->dma_type = MRAID_DMA_NONE;
  2790. scb->dev_channel = -1;
  2791. scb->dev_target = -1;
  2792. // put scb in the free pool
  2793. list_add_tail(&scb->list, &adapter->uscb_pool);
  2794. }
  2795. adp.unique_id = adapter->unique_id;
  2796. adp.drvr_type = DRVRTYPE_MBOX;
  2797. adp.drvr_data = (unsigned long)adapter;
  2798. adp.pdev = adapter->pdev;
  2799. adp.issue_uioc = megaraid_mbox_mm_handler;
  2800. adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
  2801. adp.max_kioc = MBOX_MAX_USER_CMDS;
  2802. if ((rval = mraid_mm_register_adp(&adp)) != 0) {
  2803. con_log(CL_ANN, (KERN_WARNING
  2804. "megaraid mbox: did not register with CMM\n"));
  2805. kfree(adapter->uscb_list);
  2806. }
  2807. return rval;
  2808. }
  2809. /**
  2810. * megaraid_cmm_unregister - un-register with the mangement module
  2811. * @param adapter : HBA soft state
  2812. *
  2813. * Un-register with the management module.
  2814. * FIXME: mgmt module must return failure for unregister if it has pending
  2815. * commands in LLD
  2816. */
  2817. static int
  2818. megaraid_cmm_unregister(adapter_t *adapter)
  2819. {
  2820. kfree(adapter->uscb_list);
  2821. mraid_mm_unregister_adp(adapter->unique_id);
  2822. return 0;
  2823. }
  2824. /**
  2825. * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
  2826. * @param drvr_data : LLD specific data
  2827. * @param kioc : CMM interface packet
  2828. * @param action : command action
  2829. *
  2830. * This routine is invoked whenever the Common Mangement Module (CMM) has a
  2831. * command for us. The 'action' parameter specifies if this is a new command
  2832. * or otherwise.
  2833. */
  2834. static int
  2835. megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
  2836. {
  2837. adapter_t *adapter;
  2838. if (action != IOCTL_ISSUE) {
  2839. con_log(CL_ANN, (KERN_WARNING
  2840. "megaraid: unsupported management action:%#2x\n",
  2841. action));
  2842. return (-ENOTSUPP);
  2843. }
  2844. adapter = (adapter_t *)drvr_data;
  2845. // make sure this adapter is not being detached right now.
  2846. if (atomic_read(&adapter->being_detached)) {
  2847. con_log(CL_ANN, (KERN_WARNING
  2848. "megaraid: reject management request, detaching\n"));
  2849. return (-ENODEV);
  2850. }
  2851. switch (kioc->opcode) {
  2852. case GET_ADAP_INFO:
  2853. kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
  2854. (unsigned long)kioc->buf_vaddr);
  2855. kioc->done(kioc);
  2856. return kioc->status;
  2857. case MBOX_CMD:
  2858. return megaraid_mbox_mm_command(adapter, kioc);
  2859. default:
  2860. kioc->status = (-EINVAL);
  2861. kioc->done(kioc);
  2862. return (-EINVAL);
  2863. }
  2864. return 0; // not reached
  2865. }
  2866. /**
  2867. * megaraid_mbox_mm_command - issues commands routed through CMM
  2868. * @param adapter : HBA soft state
  2869. * @param kioc : management command packet
  2870. *
  2871. * Issues commands, which are routed through the management module.
  2872. */
  2873. static int
  2874. megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
  2875. {
  2876. struct list_head *head = &adapter->uscb_pool;
  2877. mbox64_t *mbox64;
  2878. uint8_t *raw_mbox;
  2879. scb_t *scb;
  2880. mbox_ccb_t *ccb;
  2881. unsigned long flags;
  2882. // detach one scb from free pool
  2883. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  2884. if (list_empty(head)) { // should never happen because of CMM
  2885. con_log(CL_ANN, (KERN_WARNING
  2886. "megaraid mbox: bug in cmm handler, lost resources\n"));
  2887. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2888. return (-EINVAL);
  2889. }
  2890. scb = list_entry(head->next, scb_t, list);
  2891. list_del_init(&scb->list);
  2892. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2893. scb->state = SCB_ACTIVE;
  2894. scb->dma_type = MRAID_DMA_NONE;
  2895. scb->dma_direction = PCI_DMA_NONE;
  2896. ccb = (mbox_ccb_t *)scb->ccb;
  2897. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  2898. raw_mbox = (uint8_t *)&mbox64->mbox32;
  2899. memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
  2900. scb->gp = (unsigned long)kioc;
  2901. /*
  2902. * If it is a logdrv random delete operation, we have to wait till
  2903. * there are no outstanding cmds at the fw and then issue it directly
  2904. */
  2905. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  2906. if (wait_till_fw_empty(adapter)) {
  2907. con_log(CL_ANN, (KERN_NOTICE
  2908. "megaraid mbox: LD delete, timed out\n"));
  2909. kioc->status = -ETIME;
  2910. scb->status = -1;
  2911. megaraid_mbox_mm_done(adapter, scb);
  2912. return (-ETIME);
  2913. }
  2914. INIT_LIST_HEAD(&scb->list);
  2915. scb->state = SCB_ISSUED;
  2916. if (mbox_post_cmd(adapter, scb) != 0) {
  2917. con_log(CL_ANN, (KERN_NOTICE
  2918. "megaraid mbox: LD delete, mailbox busy\n"));
  2919. kioc->status = -EBUSY;
  2920. scb->status = -1;
  2921. megaraid_mbox_mm_done(adapter, scb);
  2922. return (-EBUSY);
  2923. }
  2924. return 0;
  2925. }
  2926. // put the command on the pending list and execute
  2927. megaraid_mbox_runpendq(adapter, scb);
  2928. return 0;
  2929. }
  2930. static int
  2931. wait_till_fw_empty(adapter_t *adapter)
  2932. {
  2933. unsigned long flags = 0;
  2934. int i;
  2935. /*
  2936. * Set the quiescent flag to stop issuing cmds to FW.
  2937. */
  2938. spin_lock_irqsave(&adapter->lock, flags);
  2939. adapter->quiescent++;
  2940. spin_unlock_irqrestore(&adapter->lock, flags);
  2941. /*
  2942. * Wait till there are no more cmds outstanding at FW. Try for at most
  2943. * 60 seconds
  2944. */
  2945. for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
  2946. con_log(CL_DLEVEL1, (KERN_INFO
  2947. "megaraid: FW has %d pending commands\n",
  2948. adapter->outstanding_cmds));
  2949. msleep(1000);
  2950. }
  2951. return adapter->outstanding_cmds;
  2952. }
  2953. /**
  2954. * megaraid_mbox_mm_done - callback for CMM commands
  2955. * @adapter : HBA soft state
  2956. * @scb : completed command
  2957. *
  2958. * Callback routine for internal commands originated from the management
  2959. * module.
  2960. */
  2961. static void
  2962. megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
  2963. {
  2964. uioc_t *kioc;
  2965. mbox64_t *mbox64;
  2966. uint8_t *raw_mbox;
  2967. unsigned long flags;
  2968. kioc = (uioc_t *)scb->gp;
  2969. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  2970. mbox64->mbox32.status = scb->status;
  2971. raw_mbox = (uint8_t *)&mbox64->mbox32;
  2972. // put scb in the free pool
  2973. scb->state = SCB_FREE;
  2974. scb->scp = NULL;
  2975. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  2976. list_add(&scb->list, &adapter->uscb_pool);
  2977. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2978. // if a delete logical drive operation succeeded, restart the
  2979. // controller
  2980. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  2981. adapter->quiescent--;
  2982. megaraid_mbox_runpendq(adapter, NULL);
  2983. }
  2984. kioc->done(kioc);
  2985. return;
  2986. }
  2987. /**
  2988. * gather_hbainfo - HBA characteristics for the applications
  2989. * @param adapter : HBA soft state
  2990. * @param hinfo : pointer to the caller's host info strucuture
  2991. */
  2992. static int
  2993. gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
  2994. {
  2995. uint8_t dmajor;
  2996. dmajor = megaraid_mbox_version[0];
  2997. hinfo->pci_vendor_id = adapter->pdev->vendor;
  2998. hinfo->pci_device_id = adapter->pdev->device;
  2999. hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
  3000. hinfo->subsys_device_id = adapter->pdev->subsystem_device;
  3001. hinfo->pci_bus = adapter->pdev->bus->number;
  3002. hinfo->pci_dev_fn = adapter->pdev->devfn;
  3003. hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
  3004. hinfo->irq = adapter->host->irq;
  3005. hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
  3006. hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
  3007. hinfo->host_no = adapter->host->host_no;
  3008. return 0;
  3009. }
  3010. /*
  3011. * END: Interface for the common management module
  3012. */
  3013. /**
  3014. * megaraid_sysfs_alloc_resources - allocate sysfs related resources
  3015. *
  3016. * Allocate packets required to issue FW calls whenever the sysfs attributes
  3017. * are read. These attributes would require up-to-date information from the
  3018. * FW. Also set up resources for mutual exclusion to share these resources and
  3019. * the wait queue.
  3020. *
  3021. * @param adapter : controller's soft state
  3022. *
  3023. * @return 0 on success
  3024. * @return -ERROR_CODE on failure
  3025. */
  3026. static int
  3027. megaraid_sysfs_alloc_resources(adapter_t *adapter)
  3028. {
  3029. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3030. int rval = 0;
  3031. raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
  3032. raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
  3033. raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
  3034. PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
  3035. if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
  3036. !raid_dev->sysfs_buffer) {
  3037. con_log(CL_ANN, (KERN_WARNING
  3038. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  3039. __LINE__));
  3040. rval = -ENOMEM;
  3041. megaraid_sysfs_free_resources(adapter);
  3042. }
  3043. sema_init(&raid_dev->sysfs_sem, 1);
  3044. init_waitqueue_head(&raid_dev->sysfs_wait_q);
  3045. return rval;
  3046. }
  3047. /**
  3048. * megaraid_sysfs_free_resources - free sysfs related resources
  3049. *
  3050. * Free packets allocated for sysfs FW commands
  3051. *
  3052. * @param adapter : controller's soft state
  3053. */
  3054. static void
  3055. megaraid_sysfs_free_resources(adapter_t *adapter)
  3056. {
  3057. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3058. kfree(raid_dev->sysfs_uioc);
  3059. kfree(raid_dev->sysfs_mbox64);
  3060. if (raid_dev->sysfs_buffer) {
  3061. pci_free_consistent(adapter->pdev, PAGE_SIZE,
  3062. raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
  3063. }
  3064. }
  3065. /**
  3066. * megaraid_sysfs_get_ldmap_done - callback for get ldmap
  3067. *
  3068. * Callback routine called in the ISR/tasklet context for get ldmap call
  3069. *
  3070. * @param uioc : completed packet
  3071. */
  3072. static void
  3073. megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
  3074. {
  3075. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3076. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3077. uioc->status = 0;
  3078. wake_up(&raid_dev->sysfs_wait_q);
  3079. }
  3080. /**
  3081. * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
  3082. *
  3083. * Timeout routine to recover and return to application, in case the adapter
  3084. * has stopped responding. A timeout of 60 seconds for this command seem like
  3085. * a good value
  3086. *
  3087. * @param uioc : timed out packet
  3088. */
  3089. static void
  3090. megaraid_sysfs_get_ldmap_timeout(unsigned long data)
  3091. {
  3092. uioc_t *uioc = (uioc_t *)data;
  3093. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3094. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3095. uioc->status = -ETIME;
  3096. wake_up(&raid_dev->sysfs_wait_q);
  3097. }
  3098. /**
  3099. * megaraid_sysfs_get_ldmap - get update logical drive map
  3100. *
  3101. * This routine will be called whenever user reads the logical drive
  3102. * attributes, go get the current logical drive mapping table from the
  3103. * firmware. We use the managment API's to issue commands to the controller.
  3104. *
  3105. * NOTE: The commands issuance functionality is not generalized and
  3106. * implemented in context of "get ld map" command only. If required, the
  3107. * command issuance logical can be trivially pulled out and implemented as a
  3108. * standalone libary. For now, this should suffice since there is no other
  3109. * user of this interface.
  3110. *
  3111. * @param adapter : controller's soft state
  3112. *
  3113. * @return 0 on success
  3114. * @return -1 on failure
  3115. */
  3116. static int
  3117. megaraid_sysfs_get_ldmap(adapter_t *adapter)
  3118. {
  3119. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3120. uioc_t *uioc;
  3121. mbox64_t *mbox64;
  3122. mbox_t *mbox;
  3123. char *raw_mbox;
  3124. struct timer_list sysfs_timer;
  3125. struct timer_list *timerp;
  3126. caddr_t ldmap;
  3127. int rval = 0;
  3128. /*
  3129. * Allow only one read at a time to go through the sysfs attributes
  3130. */
  3131. down(&raid_dev->sysfs_sem);
  3132. uioc = raid_dev->sysfs_uioc;
  3133. mbox64 = raid_dev->sysfs_mbox64;
  3134. ldmap = raid_dev->sysfs_buffer;
  3135. memset(uioc, 0, sizeof(uioc_t));
  3136. memset(mbox64, 0, sizeof(mbox64_t));
  3137. memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
  3138. mbox = &mbox64->mbox32;
  3139. raw_mbox = (char *)mbox;
  3140. uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
  3141. uioc->buf_vaddr = (caddr_t)adapter;
  3142. uioc->status = -ENODATA;
  3143. uioc->done = megaraid_sysfs_get_ldmap_done;
  3144. /*
  3145. * Prepare the mailbox packet to get the current logical drive mapping
  3146. * table
  3147. */
  3148. mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
  3149. raw_mbox[0] = FC_DEL_LOGDRV;
  3150. raw_mbox[2] = OP_GET_LDID_MAP;
  3151. /*
  3152. * Setup a timer to recover from a non-responding controller
  3153. */
  3154. timerp = &sysfs_timer;
  3155. init_timer(timerp);
  3156. timerp->function = megaraid_sysfs_get_ldmap_timeout;
  3157. timerp->data = (unsigned long)uioc;
  3158. timerp->expires = jiffies + 60 * HZ;
  3159. add_timer(timerp);
  3160. /*
  3161. * Send the command to the firmware
  3162. */
  3163. rval = megaraid_mbox_mm_command(adapter, uioc);
  3164. if (rval == 0) { // command successfully issued
  3165. wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
  3166. /*
  3167. * Check if the command timed out
  3168. */
  3169. if (uioc->status == -ETIME) {
  3170. con_log(CL_ANN, (KERN_NOTICE
  3171. "megaraid: sysfs get ld map timed out\n"));
  3172. rval = -ETIME;
  3173. }
  3174. else {
  3175. rval = mbox->status;
  3176. }
  3177. if (rval == 0) {
  3178. memcpy(raid_dev->curr_ldmap, ldmap,
  3179. sizeof(raid_dev->curr_ldmap));
  3180. }
  3181. else {
  3182. con_log(CL_ANN, (KERN_NOTICE
  3183. "megaraid: get ld map failed with %x\n", rval));
  3184. }
  3185. }
  3186. else {
  3187. con_log(CL_ANN, (KERN_NOTICE
  3188. "megaraid: could not issue ldmap command:%x\n", rval));
  3189. }
  3190. del_timer_sync(timerp);
  3191. up(&raid_dev->sysfs_sem);
  3192. return rval;
  3193. }
  3194. /**
  3195. * megaraid_sysfs_show_app_hndl - display application handle for this adapter
  3196. *
  3197. * Display the handle used by the applications while executing management
  3198. * tasks on the adapter. We invoke a management module API to get the adapter
  3199. * handle, since we do not interface with applications directly.
  3200. *
  3201. * @param cdev : class device object representation for the host
  3202. * @param buf : buffer to send data to
  3203. */
  3204. static ssize_t
  3205. megaraid_sysfs_show_app_hndl(struct class_device *cdev, char *buf)
  3206. {
  3207. struct Scsi_Host *shost = class_to_shost(cdev);
  3208. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
  3209. uint32_t app_hndl;
  3210. app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
  3211. return snprintf(buf, 8, "%u\n", app_hndl);
  3212. }
  3213. /**
  3214. * megaraid_sysfs_show_ldnum - display the logical drive number for this device
  3215. *
  3216. * Display the logical drive number for the device in question, if it a valid
  3217. * logical drive. For physical devices, "-1" is returned
  3218. * The logical drive number is displayed in following format
  3219. *
  3220. * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
  3221. * <int> <int> <int> <int>
  3222. *
  3223. * @param dev : device object representation for the scsi device
  3224. * @param buf : buffer to send data to
  3225. */
  3226. static ssize_t
  3227. megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
  3228. {
  3229. struct scsi_device *sdev = to_scsi_device(dev);
  3230. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
  3231. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3232. int scsi_id = -1;
  3233. int logical_drv = -1;
  3234. int ldid_map = -1;
  3235. uint32_t app_hndl = 0;
  3236. int mapped_sdev_id;
  3237. int rval;
  3238. int i;
  3239. if (raid_dev->random_del_supported &&
  3240. MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
  3241. rval = megaraid_sysfs_get_ldmap(adapter);
  3242. if (rval == 0) {
  3243. for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
  3244. mapped_sdev_id = sdev->id;
  3245. if (sdev->id > adapter->init_id) {
  3246. mapped_sdev_id -= 1;
  3247. }
  3248. if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
  3249. scsi_id = sdev->id;
  3250. logical_drv = i;
  3251. ldid_map = raid_dev->curr_ldmap[i];
  3252. app_hndl = mraid_mm_adapter_app_handle(
  3253. adapter->unique_id);
  3254. break;
  3255. }
  3256. }
  3257. }
  3258. else {
  3259. con_log(CL_ANN, (KERN_NOTICE
  3260. "megaraid: sysfs get ld map failed: %x\n",
  3261. rval));
  3262. }
  3263. }
  3264. return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
  3265. ldid_map, app_hndl);
  3266. }
  3267. /*
  3268. * END: Mailbox Low Level Driver
  3269. */
  3270. module_init(megaraid_init);
  3271. module_exit(megaraid_exit);
  3272. /* vim: set ts=8 sw=8 tw=78 ai si: */