nsp32.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430
  1. /*
  2. * NinjaSCSI-32Bi Cardbus, NinjaSCSI-32UDE PCI/CardBus SCSI driver
  3. * Copyright (C) 2001, 2002, 2003
  4. * YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>
  5. * GOTO Masanori <gotom@debian.or.jp>, <gotom@debian.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. *
  18. * Revision History:
  19. * 1.0: Initial Release.
  20. * 1.1: Add /proc SDTR status.
  21. * Remove obsolete error handler nsp32_reset.
  22. * Some clean up.
  23. * 1.2: PowerPC (big endian) support.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/string.h>
  29. #include <linux/timer.h>
  30. #include <linux/ioport.h>
  31. #include <linux/major.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/pci.h>
  35. #include <linux/delay.h>
  36. #include <linux/ctype.h>
  37. #include <linux/dma-mapping.h>
  38. #include <asm/dma.h>
  39. #include <asm/io.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_cmnd.h>
  42. #include <scsi/scsi_device.h>
  43. #include <scsi/scsi_host.h>
  44. #include <scsi/scsi_ioctl.h>
  45. #include "nsp32.h"
  46. /***********************************************************************
  47. * Module parameters
  48. */
  49. static int trans_mode = 0; /* default: BIOS */
  50. module_param (trans_mode, int, 0);
  51. MODULE_PARM_DESC(trans_mode, "transfer mode (0: BIOS(default) 1: Async 2: Ultra20M");
  52. #define ASYNC_MODE 1
  53. #define ULTRA20M_MODE 2
  54. static bool auto_param = 0; /* default: ON */
  55. module_param (auto_param, bool, 0);
  56. MODULE_PARM_DESC(auto_param, "AutoParameter mode (0: ON(default) 1: OFF)");
  57. static bool disc_priv = 1; /* default: OFF */
  58. module_param (disc_priv, bool, 0);
  59. MODULE_PARM_DESC(disc_priv, "disconnection privilege mode (0: ON 1: OFF(default))");
  60. MODULE_AUTHOR("YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>, GOTO Masanori <gotom@debian.or.jp>");
  61. MODULE_DESCRIPTION("Workbit NinjaSCSI-32Bi/UDE CardBus/PCI SCSI host bus adapter module");
  62. MODULE_LICENSE("GPL");
  63. static const char *nsp32_release_version = "1.2";
  64. /****************************************************************************
  65. * Supported hardware
  66. */
  67. static struct pci_device_id nsp32_pci_table[] = {
  68. {
  69. .vendor = PCI_VENDOR_ID_IODATA,
  70. .device = PCI_DEVICE_ID_NINJASCSI_32BI_CBSC_II,
  71. .subvendor = PCI_ANY_ID,
  72. .subdevice = PCI_ANY_ID,
  73. .driver_data = MODEL_IODATA,
  74. },
  75. {
  76. .vendor = PCI_VENDOR_ID_WORKBIT,
  77. .device = PCI_DEVICE_ID_NINJASCSI_32BI_KME,
  78. .subvendor = PCI_ANY_ID,
  79. .subdevice = PCI_ANY_ID,
  80. .driver_data = MODEL_KME,
  81. },
  82. {
  83. .vendor = PCI_VENDOR_ID_WORKBIT,
  84. .device = PCI_DEVICE_ID_NINJASCSI_32BI_WBT,
  85. .subvendor = PCI_ANY_ID,
  86. .subdevice = PCI_ANY_ID,
  87. .driver_data = MODEL_WORKBIT,
  88. },
  89. {
  90. .vendor = PCI_VENDOR_ID_WORKBIT,
  91. .device = PCI_DEVICE_ID_WORKBIT_STANDARD,
  92. .subvendor = PCI_ANY_ID,
  93. .subdevice = PCI_ANY_ID,
  94. .driver_data = MODEL_PCI_WORKBIT,
  95. },
  96. {
  97. .vendor = PCI_VENDOR_ID_WORKBIT,
  98. .device = PCI_DEVICE_ID_NINJASCSI_32BI_LOGITEC,
  99. .subvendor = PCI_ANY_ID,
  100. .subdevice = PCI_ANY_ID,
  101. .driver_data = MODEL_LOGITEC,
  102. },
  103. {
  104. .vendor = PCI_VENDOR_ID_WORKBIT,
  105. .device = PCI_DEVICE_ID_NINJASCSI_32BIB_LOGITEC,
  106. .subvendor = PCI_ANY_ID,
  107. .subdevice = PCI_ANY_ID,
  108. .driver_data = MODEL_PCI_LOGITEC,
  109. },
  110. {
  111. .vendor = PCI_VENDOR_ID_WORKBIT,
  112. .device = PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO,
  113. .subvendor = PCI_ANY_ID,
  114. .subdevice = PCI_ANY_ID,
  115. .driver_data = MODEL_PCI_MELCO,
  116. },
  117. {
  118. .vendor = PCI_VENDOR_ID_WORKBIT,
  119. .device = PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO_II,
  120. .subvendor = PCI_ANY_ID,
  121. .subdevice = PCI_ANY_ID,
  122. .driver_data = MODEL_PCI_MELCO,
  123. },
  124. {0,0,},
  125. };
  126. MODULE_DEVICE_TABLE(pci, nsp32_pci_table);
  127. static nsp32_hw_data nsp32_data_base; /* probe <-> detect glue */
  128. /*
  129. * Period/AckWidth speed conversion table
  130. *
  131. * Note: This period/ackwidth speed table must be in descending order.
  132. */
  133. static nsp32_sync_table nsp32_sync_table_40M[] = {
  134. /* {PNo, AW, SP, EP, SREQ smpl} Speed(MB/s) Period AckWidth */
  135. {0x1, 0, 0x0c, 0x0c, SMPL_40M}, /* 20.0 : 50ns, 25ns */
  136. {0x2, 0, 0x0d, 0x18, SMPL_40M}, /* 13.3 : 75ns, 25ns */
  137. {0x3, 1, 0x19, 0x19, SMPL_40M}, /* 10.0 : 100ns, 50ns */
  138. {0x4, 1, 0x1a, 0x1f, SMPL_20M}, /* 8.0 : 125ns, 50ns */
  139. {0x5, 2, 0x20, 0x25, SMPL_20M}, /* 6.7 : 150ns, 75ns */
  140. {0x6, 2, 0x26, 0x31, SMPL_20M}, /* 5.7 : 175ns, 75ns */
  141. {0x7, 3, 0x32, 0x32, SMPL_20M}, /* 5.0 : 200ns, 100ns */
  142. {0x8, 3, 0x33, 0x38, SMPL_10M}, /* 4.4 : 225ns, 100ns */
  143. {0x9, 3, 0x39, 0x3e, SMPL_10M}, /* 4.0 : 250ns, 100ns */
  144. };
  145. static nsp32_sync_table nsp32_sync_table_20M[] = {
  146. {0x1, 0, 0x19, 0x19, SMPL_40M}, /* 10.0 : 100ns, 50ns */
  147. {0x2, 0, 0x1a, 0x25, SMPL_20M}, /* 6.7 : 150ns, 50ns */
  148. {0x3, 1, 0x26, 0x32, SMPL_20M}, /* 5.0 : 200ns, 100ns */
  149. {0x4, 1, 0x33, 0x3e, SMPL_10M}, /* 4.0 : 250ns, 100ns */
  150. {0x5, 2, 0x3f, 0x4b, SMPL_10M}, /* 3.3 : 300ns, 150ns */
  151. {0x6, 2, 0x4c, 0x57, SMPL_10M}, /* 2.8 : 350ns, 150ns */
  152. {0x7, 3, 0x58, 0x64, SMPL_10M}, /* 2.5 : 400ns, 200ns */
  153. {0x8, 3, 0x65, 0x70, SMPL_10M}, /* 2.2 : 450ns, 200ns */
  154. {0x9, 3, 0x71, 0x7d, SMPL_10M}, /* 2.0 : 500ns, 200ns */
  155. };
  156. static nsp32_sync_table nsp32_sync_table_pci[] = {
  157. {0x1, 0, 0x0c, 0x0f, SMPL_40M}, /* 16.6 : 60ns, 30ns */
  158. {0x2, 0, 0x10, 0x16, SMPL_40M}, /* 11.1 : 90ns, 30ns */
  159. {0x3, 1, 0x17, 0x1e, SMPL_20M}, /* 8.3 : 120ns, 60ns */
  160. {0x4, 1, 0x1f, 0x25, SMPL_20M}, /* 6.7 : 150ns, 60ns */
  161. {0x5, 2, 0x26, 0x2d, SMPL_20M}, /* 5.6 : 180ns, 90ns */
  162. {0x6, 2, 0x2e, 0x34, SMPL_10M}, /* 4.8 : 210ns, 90ns */
  163. {0x7, 3, 0x35, 0x3c, SMPL_10M}, /* 4.2 : 240ns, 120ns */
  164. {0x8, 3, 0x3d, 0x43, SMPL_10M}, /* 3.7 : 270ns, 120ns */
  165. {0x9, 3, 0x44, 0x4b, SMPL_10M}, /* 3.3 : 300ns, 120ns */
  166. };
  167. /*
  168. * function declaration
  169. */
  170. /* module entry point */
  171. static int nsp32_probe (struct pci_dev *, const struct pci_device_id *);
  172. static void nsp32_remove(struct pci_dev *);
  173. static int __init init_nsp32 (void);
  174. static void __exit exit_nsp32 (void);
  175. /* struct struct scsi_host_template */
  176. static int nsp32_show_info (struct seq_file *, struct Scsi_Host *);
  177. static int nsp32_detect (struct pci_dev *pdev);
  178. static int nsp32_queuecommand(struct Scsi_Host *, struct scsi_cmnd *);
  179. static const char *nsp32_info (struct Scsi_Host *);
  180. static int nsp32_release (struct Scsi_Host *);
  181. /* SCSI error handler */
  182. static int nsp32_eh_abort (struct scsi_cmnd *);
  183. static int nsp32_eh_bus_reset (struct scsi_cmnd *);
  184. static int nsp32_eh_host_reset(struct scsi_cmnd *);
  185. /* generate SCSI message */
  186. static void nsp32_build_identify(struct scsi_cmnd *);
  187. static void nsp32_build_nop (struct scsi_cmnd *);
  188. static void nsp32_build_reject (struct scsi_cmnd *);
  189. static void nsp32_build_sdtr (struct scsi_cmnd *, unsigned char, unsigned char);
  190. /* SCSI message handler */
  191. static int nsp32_busfree_occur(struct scsi_cmnd *, unsigned short);
  192. static void nsp32_msgout_occur (struct scsi_cmnd *);
  193. static void nsp32_msgin_occur (struct scsi_cmnd *, unsigned long, unsigned short);
  194. static int nsp32_setup_sg_table (struct scsi_cmnd *);
  195. static int nsp32_selection_autopara(struct scsi_cmnd *);
  196. static int nsp32_selection_autoscsi(struct scsi_cmnd *);
  197. static void nsp32_scsi_done (struct scsi_cmnd *);
  198. static int nsp32_arbitration (struct scsi_cmnd *, unsigned int);
  199. static int nsp32_reselection (struct scsi_cmnd *, unsigned char);
  200. static void nsp32_adjust_busfree (struct scsi_cmnd *, unsigned int);
  201. static void nsp32_restart_autoscsi (struct scsi_cmnd *, unsigned short);
  202. /* SCSI SDTR */
  203. static void nsp32_analyze_sdtr (struct scsi_cmnd *);
  204. static int nsp32_search_period_entry(nsp32_hw_data *, nsp32_target *, unsigned char);
  205. static void nsp32_set_async (nsp32_hw_data *, nsp32_target *);
  206. static void nsp32_set_max_sync (nsp32_hw_data *, nsp32_target *, unsigned char *, unsigned char *);
  207. static void nsp32_set_sync_entry (nsp32_hw_data *, nsp32_target *, int, unsigned char);
  208. /* SCSI bus status handler */
  209. static void nsp32_wait_req (nsp32_hw_data *, int);
  210. static void nsp32_wait_sack (nsp32_hw_data *, int);
  211. static void nsp32_sack_assert (nsp32_hw_data *);
  212. static void nsp32_sack_negate (nsp32_hw_data *);
  213. static void nsp32_do_bus_reset(nsp32_hw_data *);
  214. /* hardware interrupt handler */
  215. static irqreturn_t do_nsp32_isr(int, void *);
  216. /* initialize hardware */
  217. static int nsp32hw_init(nsp32_hw_data *);
  218. /* EEPROM handler */
  219. static int nsp32_getprom_param (nsp32_hw_data *);
  220. static int nsp32_getprom_at24 (nsp32_hw_data *);
  221. static int nsp32_getprom_c16 (nsp32_hw_data *);
  222. static void nsp32_prom_start (nsp32_hw_data *);
  223. static void nsp32_prom_stop (nsp32_hw_data *);
  224. static int nsp32_prom_read (nsp32_hw_data *, int);
  225. static int nsp32_prom_read_bit (nsp32_hw_data *);
  226. static void nsp32_prom_write_bit(nsp32_hw_data *, int);
  227. static void nsp32_prom_set (nsp32_hw_data *, int, int);
  228. static int nsp32_prom_get (nsp32_hw_data *, int);
  229. /* debug/warning/info message */
  230. static void nsp32_message (const char *, int, char *, char *, ...);
  231. #ifdef NSP32_DEBUG
  232. static void nsp32_dmessage(const char *, int, int, char *, ...);
  233. #endif
  234. /*
  235. * max_sectors is currently limited up to 128.
  236. */
  237. static struct scsi_host_template nsp32_template = {
  238. .proc_name = "nsp32",
  239. .name = "Workbit NinjaSCSI-32Bi/UDE",
  240. .show_info = nsp32_show_info,
  241. .info = nsp32_info,
  242. .queuecommand = nsp32_queuecommand,
  243. .can_queue = 1,
  244. .sg_tablesize = NSP32_SG_SIZE,
  245. .max_sectors = 128,
  246. .this_id = NSP32_HOST_SCSIID,
  247. .use_clustering = DISABLE_CLUSTERING,
  248. .eh_abort_handler = nsp32_eh_abort,
  249. .eh_bus_reset_handler = nsp32_eh_bus_reset,
  250. .eh_host_reset_handler = nsp32_eh_host_reset,
  251. /* .highmem_io = 1, */
  252. };
  253. #include "nsp32_io.h"
  254. /***********************************************************************
  255. * debug, error print
  256. */
  257. #ifndef NSP32_DEBUG
  258. # define NSP32_DEBUG_MASK 0x000000
  259. # define nsp32_msg(type, args...) nsp32_message ("", 0, (type), args)
  260. # define nsp32_dbg(mask, args...) /* */
  261. #else
  262. # define NSP32_DEBUG_MASK 0xffffff
  263. # define nsp32_msg(type, args...) \
  264. nsp32_message (__func__, __LINE__, (type), args)
  265. # define nsp32_dbg(mask, args...) \
  266. nsp32_dmessage(__func__, __LINE__, (mask), args)
  267. #endif
  268. #define NSP32_DEBUG_QUEUECOMMAND BIT(0)
  269. #define NSP32_DEBUG_REGISTER BIT(1)
  270. #define NSP32_DEBUG_AUTOSCSI BIT(2)
  271. #define NSP32_DEBUG_INTR BIT(3)
  272. #define NSP32_DEBUG_SGLIST BIT(4)
  273. #define NSP32_DEBUG_BUSFREE BIT(5)
  274. #define NSP32_DEBUG_CDB_CONTENTS BIT(6)
  275. #define NSP32_DEBUG_RESELECTION BIT(7)
  276. #define NSP32_DEBUG_MSGINOCCUR BIT(8)
  277. #define NSP32_DEBUG_EEPROM BIT(9)
  278. #define NSP32_DEBUG_MSGOUTOCCUR BIT(10)
  279. #define NSP32_DEBUG_BUSRESET BIT(11)
  280. #define NSP32_DEBUG_RESTART BIT(12)
  281. #define NSP32_DEBUG_SYNC BIT(13)
  282. #define NSP32_DEBUG_WAIT BIT(14)
  283. #define NSP32_DEBUG_TARGETFLAG BIT(15)
  284. #define NSP32_DEBUG_PROC BIT(16)
  285. #define NSP32_DEBUG_INIT BIT(17)
  286. #define NSP32_SPECIAL_PRINT_REGISTER BIT(20)
  287. #define NSP32_DEBUG_BUF_LEN 100
  288. static void nsp32_message(const char *func, int line, char *type, char *fmt, ...)
  289. {
  290. va_list args;
  291. char buf[NSP32_DEBUG_BUF_LEN];
  292. va_start(args, fmt);
  293. vsnprintf(buf, sizeof(buf), fmt, args);
  294. va_end(args);
  295. #ifndef NSP32_DEBUG
  296. printk("%snsp32: %s\n", type, buf);
  297. #else
  298. printk("%snsp32: %s (%d): %s\n", type, func, line, buf);
  299. #endif
  300. }
  301. #ifdef NSP32_DEBUG
  302. static void nsp32_dmessage(const char *func, int line, int mask, char *fmt, ...)
  303. {
  304. va_list args;
  305. char buf[NSP32_DEBUG_BUF_LEN];
  306. va_start(args, fmt);
  307. vsnprintf(buf, sizeof(buf), fmt, args);
  308. va_end(args);
  309. if (mask & NSP32_DEBUG_MASK) {
  310. printk("nsp32-debug: 0x%x %s (%d): %s\n", mask, func, line, buf);
  311. }
  312. }
  313. #endif
  314. #ifdef NSP32_DEBUG
  315. # include "nsp32_debug.c"
  316. #else
  317. # define show_command(arg) /* */
  318. # define show_busphase(arg) /* */
  319. # define show_autophase(arg) /* */
  320. #endif
  321. /*
  322. * IDENTIFY Message
  323. */
  324. static void nsp32_build_identify(struct scsi_cmnd *SCpnt)
  325. {
  326. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  327. int pos = data->msgout_len;
  328. int mode = FALSE;
  329. /* XXX: Auto DiscPriv detection is progressing... */
  330. if (disc_priv == 0) {
  331. /* mode = TRUE; */
  332. }
  333. data->msgoutbuf[pos] = IDENTIFY(mode, SCpnt->device->lun); pos++;
  334. data->msgout_len = pos;
  335. }
  336. /*
  337. * SDTR Message Routine
  338. */
  339. static void nsp32_build_sdtr(struct scsi_cmnd *SCpnt,
  340. unsigned char period,
  341. unsigned char offset)
  342. {
  343. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  344. int pos = data->msgout_len;
  345. data->msgoutbuf[pos] = EXTENDED_MESSAGE; pos++;
  346. data->msgoutbuf[pos] = EXTENDED_SDTR_LEN; pos++;
  347. data->msgoutbuf[pos] = EXTENDED_SDTR; pos++;
  348. data->msgoutbuf[pos] = period; pos++;
  349. data->msgoutbuf[pos] = offset; pos++;
  350. data->msgout_len = pos;
  351. }
  352. /*
  353. * No Operation Message
  354. */
  355. static void nsp32_build_nop(struct scsi_cmnd *SCpnt)
  356. {
  357. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  358. int pos = data->msgout_len;
  359. if (pos != 0) {
  360. nsp32_msg(KERN_WARNING,
  361. "Some messages are already contained!");
  362. return;
  363. }
  364. data->msgoutbuf[pos] = NOP; pos++;
  365. data->msgout_len = pos;
  366. }
  367. /*
  368. * Reject Message
  369. */
  370. static void nsp32_build_reject(struct scsi_cmnd *SCpnt)
  371. {
  372. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  373. int pos = data->msgout_len;
  374. data->msgoutbuf[pos] = MESSAGE_REJECT; pos++;
  375. data->msgout_len = pos;
  376. }
  377. /*
  378. * timer
  379. */
  380. #if 0
  381. static void nsp32_start_timer(struct scsi_cmnd *SCpnt, int time)
  382. {
  383. unsigned int base = SCpnt->host->io_port;
  384. nsp32_dbg(NSP32_DEBUG_INTR, "timer=%d", time);
  385. if (time & (~TIMER_CNT_MASK)) {
  386. nsp32_dbg(NSP32_DEBUG_INTR, "timer set overflow");
  387. }
  388. nsp32_write2(base, TIMER_SET, time & TIMER_CNT_MASK);
  389. }
  390. #endif
  391. /*
  392. * set SCSI command and other parameter to asic, and start selection phase
  393. */
  394. static int nsp32_selection_autopara(struct scsi_cmnd *SCpnt)
  395. {
  396. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  397. unsigned int base = SCpnt->device->host->io_port;
  398. unsigned int host_id = SCpnt->device->host->this_id;
  399. unsigned char target = scmd_id(SCpnt);
  400. nsp32_autoparam *param = data->autoparam;
  401. unsigned char phase;
  402. int i, ret;
  403. unsigned int msgout;
  404. u16_le s;
  405. nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "in");
  406. /*
  407. * check bus free
  408. */
  409. phase = nsp32_read1(base, SCSI_BUS_MONITOR);
  410. if (phase != BUSMON_BUS_FREE) {
  411. nsp32_msg(KERN_WARNING, "bus busy");
  412. show_busphase(phase & BUSMON_PHASE_MASK);
  413. SCpnt->result = DID_BUS_BUSY << 16;
  414. return FALSE;
  415. }
  416. /*
  417. * message out
  418. *
  419. * Note: If the range of msgout_len is 1 - 3, fill scsi_msgout.
  420. * over 3 messages needs another routine.
  421. */
  422. if (data->msgout_len == 0) {
  423. nsp32_msg(KERN_ERR, "SCSI MsgOut without any message!");
  424. SCpnt->result = DID_ERROR << 16;
  425. return FALSE;
  426. } else if (data->msgout_len > 0 && data->msgout_len <= 3) {
  427. msgout = 0;
  428. for (i = 0; i < data->msgout_len; i++) {
  429. /*
  430. * the sending order of the message is:
  431. * MCNT 3: MSG#0 -> MSG#1 -> MSG#2
  432. * MCNT 2: MSG#1 -> MSG#2
  433. * MCNT 1: MSG#2
  434. */
  435. msgout >>= 8;
  436. msgout |= ((unsigned int)(data->msgoutbuf[i]) << 24);
  437. }
  438. msgout |= MV_VALID; /* MV valid */
  439. msgout |= (unsigned int)data->msgout_len; /* len */
  440. } else {
  441. /* data->msgout_len > 3 */
  442. msgout = 0;
  443. }
  444. // nsp_dbg(NSP32_DEBUG_AUTOSCSI, "sel time out=0x%x\n", nsp32_read2(base, SEL_TIME_OUT));
  445. // nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
  446. /*
  447. * setup asic parameter
  448. */
  449. memset(param, 0, sizeof(nsp32_autoparam));
  450. /* cdb */
  451. for (i = 0; i < SCpnt->cmd_len; i++) {
  452. param->cdb[4 * i] = SCpnt->cmnd[i];
  453. }
  454. /* outgoing messages */
  455. param->msgout = cpu_to_le32(msgout);
  456. /* syncreg, ackwidth, target id, SREQ sampling rate */
  457. param->syncreg = data->cur_target->syncreg;
  458. param->ackwidth = data->cur_target->ackwidth;
  459. param->target_id = BIT(host_id) | BIT(target);
  460. param->sample_reg = data->cur_target->sample_reg;
  461. // nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "sample rate=0x%x\n", data->cur_target->sample_reg);
  462. /* command control */
  463. param->command_control = cpu_to_le16(CLEAR_CDB_FIFO_POINTER |
  464. AUTOSCSI_START |
  465. AUTO_MSGIN_00_OR_04 |
  466. AUTO_MSGIN_02 |
  467. AUTO_ATN );
  468. /* transfer control */
  469. s = 0;
  470. switch (data->trans_method) {
  471. case NSP32_TRANSFER_BUSMASTER:
  472. s |= BM_START;
  473. break;
  474. case NSP32_TRANSFER_MMIO:
  475. s |= CB_MMIO_MODE;
  476. break;
  477. case NSP32_TRANSFER_PIO:
  478. s |= CB_IO_MODE;
  479. break;
  480. default:
  481. nsp32_msg(KERN_ERR, "unknown trans_method");
  482. break;
  483. }
  484. /*
  485. * OR-ed BLIEND_MODE, FIFO intr is decreased, instead of PCI bus waits.
  486. * For bus master transfer, it's taken off.
  487. */
  488. s |= (TRANSFER_GO | ALL_COUNTER_CLR);
  489. param->transfer_control = cpu_to_le16(s);
  490. /* sg table addr */
  491. param->sgt_pointer = cpu_to_le32(data->cur_lunt->sglun_paddr);
  492. /*
  493. * transfer parameter to ASIC
  494. */
  495. nsp32_write4(base, SGT_ADR, data->auto_paddr);
  496. nsp32_write2(base, COMMAND_CONTROL, CLEAR_CDB_FIFO_POINTER |
  497. AUTO_PARAMETER );
  498. /*
  499. * Check arbitration
  500. */
  501. ret = nsp32_arbitration(SCpnt, base);
  502. return ret;
  503. }
  504. /*
  505. * Selection with AUTO SCSI (without AUTO PARAMETER)
  506. */
  507. static int nsp32_selection_autoscsi(struct scsi_cmnd *SCpnt)
  508. {
  509. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  510. unsigned int base = SCpnt->device->host->io_port;
  511. unsigned int host_id = SCpnt->device->host->this_id;
  512. unsigned char target = scmd_id(SCpnt);
  513. unsigned char phase;
  514. int status;
  515. unsigned short command = 0;
  516. unsigned int msgout = 0;
  517. unsigned short execph;
  518. int i;
  519. nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "in");
  520. /*
  521. * IRQ disable
  522. */
  523. nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
  524. /*
  525. * check bus line
  526. */
  527. phase = nsp32_read1(base, SCSI_BUS_MONITOR);
  528. if(((phase & BUSMON_BSY) == 1) || (phase & BUSMON_SEL) == 1) {
  529. nsp32_msg(KERN_WARNING, "bus busy");
  530. SCpnt->result = DID_BUS_BUSY << 16;
  531. status = 1;
  532. goto out;
  533. }
  534. /*
  535. * clear execph
  536. */
  537. execph = nsp32_read2(base, SCSI_EXECUTE_PHASE);
  538. /*
  539. * clear FIFO counter to set CDBs
  540. */
  541. nsp32_write2(base, COMMAND_CONTROL, CLEAR_CDB_FIFO_POINTER);
  542. /*
  543. * set CDB0 - CDB15
  544. */
  545. for (i = 0; i < SCpnt->cmd_len; i++) {
  546. nsp32_write1(base, COMMAND_DATA, SCpnt->cmnd[i]);
  547. }
  548. nsp32_dbg(NSP32_DEBUG_CDB_CONTENTS, "CDB[0]=[0x%x]", SCpnt->cmnd[0]);
  549. /*
  550. * set SCSIOUT LATCH(initiator)/TARGET(target) (OR-ed) ID
  551. */
  552. nsp32_write1(base, SCSI_OUT_LATCH_TARGET_ID, BIT(host_id) | BIT(target));
  553. /*
  554. * set SCSI MSGOUT REG
  555. *
  556. * Note: If the range of msgout_len is 1 - 3, fill scsi_msgout.
  557. * over 3 messages needs another routine.
  558. */
  559. if (data->msgout_len == 0) {
  560. nsp32_msg(KERN_ERR, "SCSI MsgOut without any message!");
  561. SCpnt->result = DID_ERROR << 16;
  562. status = 1;
  563. goto out;
  564. } else if (data->msgout_len > 0 && data->msgout_len <= 3) {
  565. msgout = 0;
  566. for (i = 0; i < data->msgout_len; i++) {
  567. /*
  568. * the sending order of the message is:
  569. * MCNT 3: MSG#0 -> MSG#1 -> MSG#2
  570. * MCNT 2: MSG#1 -> MSG#2
  571. * MCNT 1: MSG#2
  572. */
  573. msgout >>= 8;
  574. msgout |= ((unsigned int)(data->msgoutbuf[i]) << 24);
  575. }
  576. msgout |= MV_VALID; /* MV valid */
  577. msgout |= (unsigned int)data->msgout_len; /* len */
  578. nsp32_write4(base, SCSI_MSG_OUT, msgout);
  579. } else {
  580. /* data->msgout_len > 3 */
  581. nsp32_write4(base, SCSI_MSG_OUT, 0);
  582. }
  583. /*
  584. * set selection timeout(= 250ms)
  585. */
  586. nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
  587. /*
  588. * set SREQ hazard killer sampling rate
  589. *
  590. * TODO: sample_rate (BASE+0F) is 0 when internal clock = 40MHz.
  591. * check other internal clock!
  592. */
  593. nsp32_write1(base, SREQ_SMPL_RATE, data->cur_target->sample_reg);
  594. /*
  595. * clear Arbit
  596. */
  597. nsp32_write1(base, SET_ARBIT, ARBIT_CLEAR);
  598. /*
  599. * set SYNCREG
  600. * Don't set BM_START_ADR before setting this register.
  601. */
  602. nsp32_write1(base, SYNC_REG, data->cur_target->syncreg);
  603. /*
  604. * set ACKWIDTH
  605. */
  606. nsp32_write1(base, ACK_WIDTH, data->cur_target->ackwidth);
  607. nsp32_dbg(NSP32_DEBUG_AUTOSCSI,
  608. "syncreg=0x%x, ackwidth=0x%x, sgtpaddr=0x%x, id=0x%x",
  609. nsp32_read1(base, SYNC_REG), nsp32_read1(base, ACK_WIDTH),
  610. nsp32_read4(base, SGT_ADR), nsp32_read1(base, SCSI_OUT_LATCH_TARGET_ID));
  611. nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "msgout_len=%d, msgout=0x%x",
  612. data->msgout_len, msgout);
  613. /*
  614. * set SGT ADDR (physical address)
  615. */
  616. nsp32_write4(base, SGT_ADR, data->cur_lunt->sglun_paddr);
  617. /*
  618. * set TRANSFER CONTROL REG
  619. */
  620. command = 0;
  621. command |= (TRANSFER_GO | ALL_COUNTER_CLR);
  622. if (data->trans_method & NSP32_TRANSFER_BUSMASTER) {
  623. if (scsi_bufflen(SCpnt) > 0) {
  624. command |= BM_START;
  625. }
  626. } else if (data->trans_method & NSP32_TRANSFER_MMIO) {
  627. command |= CB_MMIO_MODE;
  628. } else if (data->trans_method & NSP32_TRANSFER_PIO) {
  629. command |= CB_IO_MODE;
  630. }
  631. nsp32_write2(base, TRANSFER_CONTROL, command);
  632. /*
  633. * start AUTO SCSI, kick off arbitration
  634. */
  635. command = (CLEAR_CDB_FIFO_POINTER |
  636. AUTOSCSI_START |
  637. AUTO_MSGIN_00_OR_04 |
  638. AUTO_MSGIN_02 |
  639. AUTO_ATN );
  640. nsp32_write2(base, COMMAND_CONTROL, command);
  641. /*
  642. * Check arbitration
  643. */
  644. status = nsp32_arbitration(SCpnt, base);
  645. out:
  646. /*
  647. * IRQ enable
  648. */
  649. nsp32_write2(base, IRQ_CONTROL, 0);
  650. return status;
  651. }
  652. /*
  653. * Arbitration Status Check
  654. *
  655. * Note: Arbitration counter is waited during ARBIT_GO is not lifting.
  656. * Using udelay(1) consumes CPU time and system time, but
  657. * arbitration delay time is defined minimal 2.4us in SCSI
  658. * specification, thus udelay works as coarse grained wait timer.
  659. */
  660. static int nsp32_arbitration(struct scsi_cmnd *SCpnt, unsigned int base)
  661. {
  662. unsigned char arbit;
  663. int status = TRUE;
  664. int time = 0;
  665. do {
  666. arbit = nsp32_read1(base, ARBIT_STATUS);
  667. time++;
  668. } while ((arbit & (ARBIT_WIN | ARBIT_FAIL)) == 0 &&
  669. (time <= ARBIT_TIMEOUT_TIME));
  670. nsp32_dbg(NSP32_DEBUG_AUTOSCSI,
  671. "arbit: 0x%x, delay time: %d", arbit, time);
  672. if (arbit & ARBIT_WIN) {
  673. /* Arbitration succeeded */
  674. SCpnt->result = DID_OK << 16;
  675. nsp32_index_write1(base, EXT_PORT, LED_ON); /* PCI LED on */
  676. } else if (arbit & ARBIT_FAIL) {
  677. /* Arbitration failed */
  678. SCpnt->result = DID_BUS_BUSY << 16;
  679. status = FALSE;
  680. } else {
  681. /*
  682. * unknown error or ARBIT_GO timeout,
  683. * something lock up! guess no connection.
  684. */
  685. nsp32_dbg(NSP32_DEBUG_AUTOSCSI, "arbit timeout");
  686. SCpnt->result = DID_NO_CONNECT << 16;
  687. status = FALSE;
  688. }
  689. /*
  690. * clear Arbit
  691. */
  692. nsp32_write1(base, SET_ARBIT, ARBIT_CLEAR);
  693. return status;
  694. }
  695. /*
  696. * reselection
  697. *
  698. * Note: This reselection routine is called from msgin_occur,
  699. * reselection target id&lun must be already set.
  700. * SCSI-2 says IDENTIFY implies RESTORE_POINTER operation.
  701. */
  702. static int nsp32_reselection(struct scsi_cmnd *SCpnt, unsigned char newlun)
  703. {
  704. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  705. unsigned int host_id = SCpnt->device->host->this_id;
  706. unsigned int base = SCpnt->device->host->io_port;
  707. unsigned char tmpid, newid;
  708. nsp32_dbg(NSP32_DEBUG_RESELECTION, "enter");
  709. /*
  710. * calculate reselected SCSI ID
  711. */
  712. tmpid = nsp32_read1(base, RESELECT_ID);
  713. tmpid &= (~BIT(host_id));
  714. newid = 0;
  715. while (tmpid) {
  716. if (tmpid & 1) {
  717. break;
  718. }
  719. tmpid >>= 1;
  720. newid++;
  721. }
  722. /*
  723. * If reselected New ID:LUN is not existed
  724. * or current nexus is not existed, unexpected
  725. * reselection is occurred. Send reject message.
  726. */
  727. if (newid >= ARRAY_SIZE(data->lunt) || newlun >= ARRAY_SIZE(data->lunt[0])) {
  728. nsp32_msg(KERN_WARNING, "unknown id/lun");
  729. return FALSE;
  730. } else if(data->lunt[newid][newlun].SCpnt == NULL) {
  731. nsp32_msg(KERN_WARNING, "no SCSI command is processing");
  732. return FALSE;
  733. }
  734. data->cur_id = newid;
  735. data->cur_lun = newlun;
  736. data->cur_target = &(data->target[newid]);
  737. data->cur_lunt = &(data->lunt[newid][newlun]);
  738. /* reset SACK/SavedACK counter (or ALL clear?) */
  739. nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
  740. return TRUE;
  741. }
  742. /*
  743. * nsp32_setup_sg_table - build scatter gather list for transfer data
  744. * with bus master.
  745. *
  746. * Note: NinjaSCSI-32Bi/UDE bus master can not transfer over 64KB at a time.
  747. */
  748. static int nsp32_setup_sg_table(struct scsi_cmnd *SCpnt)
  749. {
  750. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  751. struct scatterlist *sg;
  752. nsp32_sgtable *sgt = data->cur_lunt->sglun->sgt;
  753. int num, i;
  754. u32_le l;
  755. if (sgt == NULL) {
  756. nsp32_dbg(NSP32_DEBUG_SGLIST, "SGT == null");
  757. return FALSE;
  758. }
  759. num = scsi_dma_map(SCpnt);
  760. if (!num)
  761. return TRUE;
  762. else if (num < 0)
  763. return FALSE;
  764. else {
  765. scsi_for_each_sg(SCpnt, sg, num, i) {
  766. /*
  767. * Build nsp32_sglist, substitute sg dma addresses.
  768. */
  769. sgt[i].addr = cpu_to_le32(sg_dma_address(sg));
  770. sgt[i].len = cpu_to_le32(sg_dma_len(sg));
  771. if (le32_to_cpu(sgt[i].len) > 0x10000) {
  772. nsp32_msg(KERN_ERR,
  773. "can't transfer over 64KB at a time, size=0x%lx", le32_to_cpu(sgt[i].len));
  774. return FALSE;
  775. }
  776. nsp32_dbg(NSP32_DEBUG_SGLIST,
  777. "num 0x%x : addr 0x%lx len 0x%lx",
  778. i,
  779. le32_to_cpu(sgt[i].addr),
  780. le32_to_cpu(sgt[i].len ));
  781. }
  782. /* set end mark */
  783. l = le32_to_cpu(sgt[num-1].len);
  784. sgt[num-1].len = cpu_to_le32(l | SGTEND);
  785. }
  786. return TRUE;
  787. }
  788. static int nsp32_queuecommand_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  789. {
  790. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  791. nsp32_target *target;
  792. nsp32_lunt *cur_lunt;
  793. int ret;
  794. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
  795. "enter. target: 0x%x LUN: 0x%llx cmnd: 0x%x cmndlen: 0x%x "
  796. "use_sg: 0x%x reqbuf: 0x%lx reqlen: 0x%x",
  797. SCpnt->device->id, SCpnt->device->lun, SCpnt->cmnd[0], SCpnt->cmd_len,
  798. scsi_sg_count(SCpnt), scsi_sglist(SCpnt), scsi_bufflen(SCpnt));
  799. if (data->CurrentSC != NULL) {
  800. nsp32_msg(KERN_ERR, "Currentsc != NULL. Cancel this command request");
  801. data->CurrentSC = NULL;
  802. SCpnt->result = DID_NO_CONNECT << 16;
  803. done(SCpnt);
  804. return 0;
  805. }
  806. /* check target ID is not same as this initiator ID */
  807. if (scmd_id(SCpnt) == SCpnt->device->host->this_id) {
  808. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND, "target==host???");
  809. SCpnt->result = DID_BAD_TARGET << 16;
  810. done(SCpnt);
  811. return 0;
  812. }
  813. /* check target LUN is allowable value */
  814. if (SCpnt->device->lun >= MAX_LUN) {
  815. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND, "no more lun");
  816. SCpnt->result = DID_BAD_TARGET << 16;
  817. done(SCpnt);
  818. return 0;
  819. }
  820. show_command(SCpnt);
  821. SCpnt->scsi_done = done;
  822. data->CurrentSC = SCpnt;
  823. SCpnt->SCp.Status = CHECK_CONDITION;
  824. SCpnt->SCp.Message = 0;
  825. scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
  826. SCpnt->SCp.ptr = (char *)scsi_sglist(SCpnt);
  827. SCpnt->SCp.this_residual = scsi_bufflen(SCpnt);
  828. SCpnt->SCp.buffer = NULL;
  829. SCpnt->SCp.buffers_residual = 0;
  830. /* initialize data */
  831. data->msgout_len = 0;
  832. data->msgin_len = 0;
  833. cur_lunt = &(data->lunt[SCpnt->device->id][SCpnt->device->lun]);
  834. cur_lunt->SCpnt = SCpnt;
  835. cur_lunt->save_datp = 0;
  836. cur_lunt->msgin03 = FALSE;
  837. data->cur_lunt = cur_lunt;
  838. data->cur_id = SCpnt->device->id;
  839. data->cur_lun = SCpnt->device->lun;
  840. ret = nsp32_setup_sg_table(SCpnt);
  841. if (ret == FALSE) {
  842. nsp32_msg(KERN_ERR, "SGT fail");
  843. SCpnt->result = DID_ERROR << 16;
  844. nsp32_scsi_done(SCpnt);
  845. return 0;
  846. }
  847. /* Build IDENTIFY */
  848. nsp32_build_identify(SCpnt);
  849. /*
  850. * If target is the first time to transfer after the reset
  851. * (target don't have SDTR_DONE and SDTR_INITIATOR), sync
  852. * message SDTR is needed to do synchronous transfer.
  853. */
  854. target = &data->target[scmd_id(SCpnt)];
  855. data->cur_target = target;
  856. if (!(target->sync_flag & (SDTR_DONE | SDTR_INITIATOR | SDTR_TARGET))) {
  857. unsigned char period, offset;
  858. if (trans_mode != ASYNC_MODE) {
  859. nsp32_set_max_sync(data, target, &period, &offset);
  860. nsp32_build_sdtr(SCpnt, period, offset);
  861. target->sync_flag |= SDTR_INITIATOR;
  862. } else {
  863. nsp32_set_async(data, target);
  864. target->sync_flag |= SDTR_DONE;
  865. }
  866. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
  867. "SDTR: entry: %d start_period: 0x%x offset: 0x%x\n",
  868. target->limit_entry, period, offset);
  869. } else if (target->sync_flag & SDTR_INITIATOR) {
  870. /*
  871. * It was negotiating SDTR with target, sending from the
  872. * initiator, but there are no chance to remove this flag.
  873. * Set async because we don't get proper negotiation.
  874. */
  875. nsp32_set_async(data, target);
  876. target->sync_flag &= ~SDTR_INITIATOR;
  877. target->sync_flag |= SDTR_DONE;
  878. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
  879. "SDTR_INITIATOR: fall back to async");
  880. } else if (target->sync_flag & SDTR_TARGET) {
  881. /*
  882. * It was negotiating SDTR with target, sending from target,
  883. * but there are no chance to remove this flag. Set async
  884. * because we don't get proper negotiation.
  885. */
  886. nsp32_set_async(data, target);
  887. target->sync_flag &= ~SDTR_TARGET;
  888. target->sync_flag |= SDTR_DONE;
  889. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND,
  890. "Unknown SDTR from target is reached, fall back to async.");
  891. }
  892. nsp32_dbg(NSP32_DEBUG_TARGETFLAG,
  893. "target: %d sync_flag: 0x%x syncreg: 0x%x ackwidth: 0x%x",
  894. SCpnt->device->id, target->sync_flag, target->syncreg,
  895. target->ackwidth);
  896. /* Selection */
  897. if (auto_param == 0) {
  898. ret = nsp32_selection_autopara(SCpnt);
  899. } else {
  900. ret = nsp32_selection_autoscsi(SCpnt);
  901. }
  902. if (ret != TRUE) {
  903. nsp32_dbg(NSP32_DEBUG_QUEUECOMMAND, "selection fail");
  904. nsp32_scsi_done(SCpnt);
  905. }
  906. return 0;
  907. }
  908. static DEF_SCSI_QCMD(nsp32_queuecommand)
  909. /* initialize asic */
  910. static int nsp32hw_init(nsp32_hw_data *data)
  911. {
  912. unsigned int base = data->BaseAddress;
  913. unsigned short irq_stat;
  914. unsigned long lc_reg;
  915. unsigned char power;
  916. lc_reg = nsp32_index_read4(base, CFG_LATE_CACHE);
  917. if ((lc_reg & 0xff00) == 0) {
  918. lc_reg |= (0x20 << 8);
  919. nsp32_index_write2(base, CFG_LATE_CACHE, lc_reg & 0xffff);
  920. }
  921. nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
  922. nsp32_write2(base, TRANSFER_CONTROL, 0);
  923. nsp32_write4(base, BM_CNT, 0);
  924. nsp32_write2(base, SCSI_EXECUTE_PHASE, 0);
  925. do {
  926. irq_stat = nsp32_read2(base, IRQ_STATUS);
  927. nsp32_dbg(NSP32_DEBUG_INIT, "irq_stat 0x%x", irq_stat);
  928. } while (irq_stat & IRQSTATUS_ANY_IRQ);
  929. /*
  930. * Fill FIFO_FULL_SHLD, FIFO_EMPTY_SHLD. Below parameter is
  931. * designated by specification.
  932. */
  933. if ((data->trans_method & NSP32_TRANSFER_PIO) ||
  934. (data->trans_method & NSP32_TRANSFER_MMIO)) {
  935. nsp32_index_write1(base, FIFO_FULL_SHLD_COUNT, 0x40);
  936. nsp32_index_write1(base, FIFO_EMPTY_SHLD_COUNT, 0x40);
  937. } else if (data->trans_method & NSP32_TRANSFER_BUSMASTER) {
  938. nsp32_index_write1(base, FIFO_FULL_SHLD_COUNT, 0x10);
  939. nsp32_index_write1(base, FIFO_EMPTY_SHLD_COUNT, 0x60);
  940. } else {
  941. nsp32_dbg(NSP32_DEBUG_INIT, "unknown transfer mode");
  942. }
  943. nsp32_dbg(NSP32_DEBUG_INIT, "full 0x%x emp 0x%x",
  944. nsp32_index_read1(base, FIFO_FULL_SHLD_COUNT),
  945. nsp32_index_read1(base, FIFO_EMPTY_SHLD_COUNT));
  946. nsp32_index_write1(base, CLOCK_DIV, data->clock);
  947. nsp32_index_write1(base, BM_CYCLE, MEMRD_CMD1 | SGT_AUTO_PARA_MEMED_CMD);
  948. nsp32_write1(base, PARITY_CONTROL, 0); /* parity check is disable */
  949. /*
  950. * initialize MISC_WRRD register
  951. *
  952. * Note: Designated parameters is obeyed as following:
  953. * MISC_SCSI_DIRECTION_DETECTOR_SELECT: It must be set.
  954. * MISC_MASTER_TERMINATION_SELECT: It must be set.
  955. * MISC_BMREQ_NEGATE_TIMING_SEL: It should be set.
  956. * MISC_AUTOSEL_TIMING_SEL: It should be set.
  957. * MISC_BMSTOP_CHANGE2_NONDATA_PHASE: It should be set.
  958. * MISC_DELAYED_BMSTART: It's selected for safety.
  959. *
  960. * Note: If MISC_BMSTOP_CHANGE2_NONDATA_PHASE is set, then
  961. * we have to set TRANSFERCONTROL_BM_START as 0 and set
  962. * appropriate value before restarting bus master transfer.
  963. */
  964. nsp32_index_write2(base, MISC_WR,
  965. (SCSI_DIRECTION_DETECTOR_SELECT |
  966. DELAYED_BMSTART |
  967. MASTER_TERMINATION_SELECT |
  968. BMREQ_NEGATE_TIMING_SEL |
  969. AUTOSEL_TIMING_SEL |
  970. BMSTOP_CHANGE2_NONDATA_PHASE));
  971. nsp32_index_write1(base, TERM_PWR_CONTROL, 0);
  972. power = nsp32_index_read1(base, TERM_PWR_CONTROL);
  973. if (!(power & SENSE)) {
  974. nsp32_msg(KERN_INFO, "term power on");
  975. nsp32_index_write1(base, TERM_PWR_CONTROL, BPWR);
  976. }
  977. nsp32_write2(base, TIMER_SET, TIMER_STOP);
  978. nsp32_write2(base, TIMER_SET, TIMER_STOP); /* Required 2 times */
  979. nsp32_write1(base, SYNC_REG, 0);
  980. nsp32_write1(base, ACK_WIDTH, 0);
  981. nsp32_write2(base, SEL_TIME_OUT, SEL_TIMEOUT_TIME);
  982. /*
  983. * enable to select designated IRQ (except for
  984. * IRQSELECT_SERR, IRQSELECT_PERR, IRQSELECT_BMCNTERR)
  985. */
  986. nsp32_index_write2(base, IRQ_SELECT, IRQSELECT_TIMER_IRQ |
  987. IRQSELECT_SCSIRESET_IRQ |
  988. IRQSELECT_FIFO_SHLD_IRQ |
  989. IRQSELECT_RESELECT_IRQ |
  990. IRQSELECT_PHASE_CHANGE_IRQ |
  991. IRQSELECT_AUTO_SCSI_SEQ_IRQ |
  992. // IRQSELECT_BMCNTERR_IRQ |
  993. IRQSELECT_TARGET_ABORT_IRQ |
  994. IRQSELECT_MASTER_ABORT_IRQ );
  995. nsp32_write2(base, IRQ_CONTROL, 0);
  996. /* PCI LED off */
  997. nsp32_index_write1(base, EXT_PORT_DDR, LED_OFF);
  998. nsp32_index_write1(base, EXT_PORT, LED_OFF);
  999. return TRUE;
  1000. }
  1001. /* interrupt routine */
  1002. static irqreturn_t do_nsp32_isr(int irq, void *dev_id)
  1003. {
  1004. nsp32_hw_data *data = dev_id;
  1005. unsigned int base = data->BaseAddress;
  1006. struct scsi_cmnd *SCpnt = data->CurrentSC;
  1007. unsigned short auto_stat, irq_stat, trans_stat;
  1008. unsigned char busmon, busphase;
  1009. unsigned long flags;
  1010. int ret;
  1011. int handled = 0;
  1012. struct Scsi_Host *host = data->Host;
  1013. spin_lock_irqsave(host->host_lock, flags);
  1014. /*
  1015. * IRQ check, then enable IRQ mask
  1016. */
  1017. irq_stat = nsp32_read2(base, IRQ_STATUS);
  1018. nsp32_dbg(NSP32_DEBUG_INTR,
  1019. "enter IRQ: %d, IRQstatus: 0x%x", irq, irq_stat);
  1020. /* is this interrupt comes from Ninja asic? */
  1021. if ((irq_stat & IRQSTATUS_ANY_IRQ) == 0) {
  1022. nsp32_dbg(NSP32_DEBUG_INTR, "shared interrupt: irq other 0x%x", irq_stat);
  1023. goto out2;
  1024. }
  1025. handled = 1;
  1026. nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
  1027. busmon = nsp32_read1(base, SCSI_BUS_MONITOR);
  1028. busphase = busmon & BUSMON_PHASE_MASK;
  1029. trans_stat = nsp32_read2(base, TRANSFER_STATUS);
  1030. if ((irq_stat == 0xffff) && (trans_stat == 0xffff)) {
  1031. nsp32_msg(KERN_INFO, "card disconnect");
  1032. if (data->CurrentSC != NULL) {
  1033. nsp32_msg(KERN_INFO, "clean up current SCSI command");
  1034. SCpnt->result = DID_BAD_TARGET << 16;
  1035. nsp32_scsi_done(SCpnt);
  1036. }
  1037. goto out;
  1038. }
  1039. /* Timer IRQ */
  1040. if (irq_stat & IRQSTATUS_TIMER_IRQ) {
  1041. nsp32_dbg(NSP32_DEBUG_INTR, "timer stop");
  1042. nsp32_write2(base, TIMER_SET, TIMER_STOP);
  1043. goto out;
  1044. }
  1045. /* SCSI reset */
  1046. if (irq_stat & IRQSTATUS_SCSIRESET_IRQ) {
  1047. nsp32_msg(KERN_INFO, "detected someone do bus reset");
  1048. nsp32_do_bus_reset(data);
  1049. if (SCpnt != NULL) {
  1050. SCpnt->result = DID_RESET << 16;
  1051. nsp32_scsi_done(SCpnt);
  1052. }
  1053. goto out;
  1054. }
  1055. if (SCpnt == NULL) {
  1056. nsp32_msg(KERN_WARNING, "SCpnt==NULL this can't be happened");
  1057. nsp32_msg(KERN_WARNING, "irq_stat=0x%x trans_stat=0x%x", irq_stat, trans_stat);
  1058. goto out;
  1059. }
  1060. /*
  1061. * AutoSCSI Interrupt.
  1062. * Note: This interrupt is occurred when AutoSCSI is finished. Then
  1063. * check SCSIEXECUTEPHASE, and do appropriate action. Each phases are
  1064. * recorded when AutoSCSI sequencer has been processed.
  1065. */
  1066. if(irq_stat & IRQSTATUS_AUTOSCSI_IRQ) {
  1067. /* getting SCSI executed phase */
  1068. auto_stat = nsp32_read2(base, SCSI_EXECUTE_PHASE);
  1069. nsp32_write2(base, SCSI_EXECUTE_PHASE, 0);
  1070. /* Selection Timeout, go busfree phase. */
  1071. if (auto_stat & SELECTION_TIMEOUT) {
  1072. nsp32_dbg(NSP32_DEBUG_INTR,
  1073. "selection timeout occurred");
  1074. SCpnt->result = DID_TIME_OUT << 16;
  1075. nsp32_scsi_done(SCpnt);
  1076. goto out;
  1077. }
  1078. if (auto_stat & MSGOUT_PHASE) {
  1079. /*
  1080. * MsgOut phase was processed.
  1081. * If MSG_IN_OCCUER is not set, then MsgOut phase is
  1082. * completed. Thus, msgout_len must reset. Otherwise,
  1083. * nothing to do here. If MSG_OUT_OCCUER is occurred,
  1084. * then we will encounter the condition and check.
  1085. */
  1086. if (!(auto_stat & MSG_IN_OCCUER) &&
  1087. (data->msgout_len <= 3)) {
  1088. /*
  1089. * !MSG_IN_OCCUER && msgout_len <=3
  1090. * ---> AutoSCSI with MSGOUTreg is processed.
  1091. */
  1092. data->msgout_len = 0;
  1093. };
  1094. nsp32_dbg(NSP32_DEBUG_INTR, "MsgOut phase processed");
  1095. }
  1096. if ((auto_stat & DATA_IN_PHASE) &&
  1097. (scsi_get_resid(SCpnt) > 0) &&
  1098. ((nsp32_read2(base, FIFO_REST_CNT) & FIFO_REST_MASK) != 0)) {
  1099. printk( "auto+fifo\n");
  1100. //nsp32_pio_read(SCpnt);
  1101. }
  1102. if (auto_stat & (DATA_IN_PHASE | DATA_OUT_PHASE)) {
  1103. /* DATA_IN_PHASE/DATA_OUT_PHASE was processed. */
  1104. nsp32_dbg(NSP32_DEBUG_INTR,
  1105. "Data in/out phase processed");
  1106. /* read BMCNT, SGT pointer addr */
  1107. nsp32_dbg(NSP32_DEBUG_INTR, "BMCNT=0x%lx",
  1108. nsp32_read4(base, BM_CNT));
  1109. nsp32_dbg(NSP32_DEBUG_INTR, "addr=0x%lx",
  1110. nsp32_read4(base, SGT_ADR));
  1111. nsp32_dbg(NSP32_DEBUG_INTR, "SACK=0x%lx",
  1112. nsp32_read4(base, SACK_CNT));
  1113. nsp32_dbg(NSP32_DEBUG_INTR, "SSACK=0x%lx",
  1114. nsp32_read4(base, SAVED_SACK_CNT));
  1115. scsi_set_resid(SCpnt, 0); /* all data transferred! */
  1116. }
  1117. /*
  1118. * MsgIn Occur
  1119. */
  1120. if (auto_stat & MSG_IN_OCCUER) {
  1121. nsp32_msgin_occur(SCpnt, irq_stat, auto_stat);
  1122. }
  1123. /*
  1124. * MsgOut Occur
  1125. */
  1126. if (auto_stat & MSG_OUT_OCCUER) {
  1127. nsp32_msgout_occur(SCpnt);
  1128. }
  1129. /*
  1130. * Bus Free Occur
  1131. */
  1132. if (auto_stat & BUS_FREE_OCCUER) {
  1133. ret = nsp32_busfree_occur(SCpnt, auto_stat);
  1134. if (ret == TRUE) {
  1135. goto out;
  1136. }
  1137. }
  1138. if (auto_stat & STATUS_PHASE) {
  1139. /*
  1140. * Read CSB and substitute CSB for SCpnt->result
  1141. * to save status phase stutas byte.
  1142. * scsi error handler checks host_byte (DID_*:
  1143. * low level driver to indicate status), then checks
  1144. * status_byte (SCSI status byte).
  1145. */
  1146. SCpnt->result = (int)nsp32_read1(base, SCSI_CSB_IN);
  1147. }
  1148. if (auto_stat & ILLEGAL_PHASE) {
  1149. /* Illegal phase is detected. SACK is not back. */
  1150. nsp32_msg(KERN_WARNING,
  1151. "AUTO SCSI ILLEGAL PHASE OCCUR!!!!");
  1152. /* TODO: currently we don't have any action... bus reset? */
  1153. /*
  1154. * To send back SACK, assert, wait, and negate.
  1155. */
  1156. nsp32_sack_assert(data);
  1157. nsp32_wait_req(data, NEGATE);
  1158. nsp32_sack_negate(data);
  1159. }
  1160. if (auto_stat & COMMAND_PHASE) {
  1161. /* nothing to do */
  1162. nsp32_dbg(NSP32_DEBUG_INTR, "Command phase processed");
  1163. }
  1164. if (auto_stat & AUTOSCSI_BUSY) {
  1165. /* AutoSCSI is running */
  1166. }
  1167. show_autophase(auto_stat);
  1168. }
  1169. /* FIFO_SHLD_IRQ */
  1170. if (irq_stat & IRQSTATUS_FIFO_SHLD_IRQ) {
  1171. nsp32_dbg(NSP32_DEBUG_INTR, "FIFO IRQ");
  1172. switch(busphase) {
  1173. case BUSPHASE_DATA_OUT:
  1174. nsp32_dbg(NSP32_DEBUG_INTR, "fifo/write");
  1175. //nsp32_pio_write(SCpnt);
  1176. break;
  1177. case BUSPHASE_DATA_IN:
  1178. nsp32_dbg(NSP32_DEBUG_INTR, "fifo/read");
  1179. //nsp32_pio_read(SCpnt);
  1180. break;
  1181. case BUSPHASE_STATUS:
  1182. nsp32_dbg(NSP32_DEBUG_INTR, "fifo/status");
  1183. SCpnt->SCp.Status = nsp32_read1(base, SCSI_CSB_IN);
  1184. break;
  1185. default:
  1186. nsp32_dbg(NSP32_DEBUG_INTR, "fifo/other phase");
  1187. nsp32_dbg(NSP32_DEBUG_INTR, "irq_stat=0x%x trans_stat=0x%x", irq_stat, trans_stat);
  1188. show_busphase(busphase);
  1189. break;
  1190. }
  1191. goto out;
  1192. }
  1193. /* Phase Change IRQ */
  1194. if (irq_stat & IRQSTATUS_PHASE_CHANGE_IRQ) {
  1195. nsp32_dbg(NSP32_DEBUG_INTR, "phase change IRQ");
  1196. switch(busphase) {
  1197. case BUSPHASE_MESSAGE_IN:
  1198. nsp32_dbg(NSP32_DEBUG_INTR, "phase chg/msg in");
  1199. nsp32_msgin_occur(SCpnt, irq_stat, 0);
  1200. break;
  1201. default:
  1202. nsp32_msg(KERN_WARNING, "phase chg/other phase?");
  1203. nsp32_msg(KERN_WARNING, "irq_stat=0x%x trans_stat=0x%x\n",
  1204. irq_stat, trans_stat);
  1205. show_busphase(busphase);
  1206. break;
  1207. }
  1208. goto out;
  1209. }
  1210. /* PCI_IRQ */
  1211. if (irq_stat & IRQSTATUS_PCI_IRQ) {
  1212. nsp32_dbg(NSP32_DEBUG_INTR, "PCI IRQ occurred");
  1213. /* Do nothing */
  1214. }
  1215. /* BMCNTERR_IRQ */
  1216. if (irq_stat & IRQSTATUS_BMCNTERR_IRQ) {
  1217. nsp32_msg(KERN_ERR, "Received unexpected BMCNTERR IRQ! ");
  1218. /*
  1219. * TODO: To be implemented improving bus master
  1220. * transfer reliability when BMCNTERR is occurred in
  1221. * AutoSCSI phase described in specification.
  1222. */
  1223. }
  1224. #if 0
  1225. nsp32_dbg(NSP32_DEBUG_INTR,
  1226. "irq_stat=0x%x trans_stat=0x%x", irq_stat, trans_stat);
  1227. show_busphase(busphase);
  1228. #endif
  1229. out:
  1230. /* disable IRQ mask */
  1231. nsp32_write2(base, IRQ_CONTROL, 0);
  1232. out2:
  1233. spin_unlock_irqrestore(host->host_lock, flags);
  1234. nsp32_dbg(NSP32_DEBUG_INTR, "exit");
  1235. return IRQ_RETVAL(handled);
  1236. }
  1237. static int nsp32_show_info(struct seq_file *m, struct Scsi_Host *host)
  1238. {
  1239. unsigned long flags;
  1240. nsp32_hw_data *data;
  1241. int hostno;
  1242. unsigned int base;
  1243. unsigned char mode_reg;
  1244. int id, speed;
  1245. long model;
  1246. hostno = host->host_no;
  1247. data = (nsp32_hw_data *)host->hostdata;
  1248. base = host->io_port;
  1249. seq_puts(m, "NinjaSCSI-32 status\n\n");
  1250. seq_printf(m, "Driver version: %s, $Revision: 1.33 $\n", nsp32_release_version);
  1251. seq_printf(m, "SCSI host No.: %d\n", hostno);
  1252. seq_printf(m, "IRQ: %d\n", host->irq);
  1253. seq_printf(m, "IO: 0x%lx-0x%lx\n", host->io_port, host->io_port + host->n_io_port - 1);
  1254. seq_printf(m, "MMIO(virtual address): 0x%lx-0x%lx\n", host->base, host->base + data->MmioLength - 1);
  1255. seq_printf(m, "sg_tablesize: %d\n", host->sg_tablesize);
  1256. seq_printf(m, "Chip revision: 0x%x\n", (nsp32_read2(base, INDEX_REG) >> 8) & 0xff);
  1257. mode_reg = nsp32_index_read1(base, CHIP_MODE);
  1258. model = data->pci_devid->driver_data;
  1259. #ifdef CONFIG_PM
  1260. seq_printf(m, "Power Management: %s\n", (mode_reg & OPTF) ? "yes" : "no");
  1261. #endif
  1262. seq_printf(m, "OEM: %ld, %s\n", (mode_reg & (OEM0|OEM1)), nsp32_model[model]);
  1263. spin_lock_irqsave(&(data->Lock), flags);
  1264. seq_printf(m, "CurrentSC: 0x%p\n\n", data->CurrentSC);
  1265. spin_unlock_irqrestore(&(data->Lock), flags);
  1266. seq_puts(m, "SDTR status\n");
  1267. for (id = 0; id < ARRAY_SIZE(data->target); id++) {
  1268. seq_printf(m, "id %d: ", id);
  1269. if (id == host->this_id) {
  1270. seq_puts(m, "----- NinjaSCSI-32 host adapter\n");
  1271. continue;
  1272. }
  1273. if (data->target[id].sync_flag == SDTR_DONE) {
  1274. if (data->target[id].period == 0 &&
  1275. data->target[id].offset == ASYNC_OFFSET ) {
  1276. seq_puts(m, "async");
  1277. } else {
  1278. seq_puts(m, " sync");
  1279. }
  1280. } else {
  1281. seq_puts(m, " none");
  1282. }
  1283. if (data->target[id].period != 0) {
  1284. speed = 1000000 / (data->target[id].period * 4);
  1285. seq_printf(m, " transfer %d.%dMB/s, offset %d",
  1286. speed / 1000,
  1287. speed % 1000,
  1288. data->target[id].offset
  1289. );
  1290. }
  1291. seq_putc(m, '\n');
  1292. }
  1293. return 0;
  1294. }
  1295. /*
  1296. * Reset parameters and call scsi_done for data->cur_lunt.
  1297. * Be careful setting SCpnt->result = DID_* before calling this function.
  1298. */
  1299. static void nsp32_scsi_done(struct scsi_cmnd *SCpnt)
  1300. {
  1301. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1302. unsigned int base = SCpnt->device->host->io_port;
  1303. scsi_dma_unmap(SCpnt);
  1304. /*
  1305. * clear TRANSFERCONTROL_BM_START
  1306. */
  1307. nsp32_write2(base, TRANSFER_CONTROL, 0);
  1308. nsp32_write4(base, BM_CNT, 0);
  1309. /*
  1310. * call scsi_done
  1311. */
  1312. (*SCpnt->scsi_done)(SCpnt);
  1313. /*
  1314. * reset parameters
  1315. */
  1316. data->cur_lunt->SCpnt = NULL;
  1317. data->cur_lunt = NULL;
  1318. data->cur_target = NULL;
  1319. data->CurrentSC = NULL;
  1320. }
  1321. /*
  1322. * Bus Free Occur
  1323. *
  1324. * Current Phase is BUSFREE. AutoSCSI is automatically execute BUSFREE phase
  1325. * with ACK reply when below condition is matched:
  1326. * MsgIn 00: Command Complete.
  1327. * MsgIn 02: Save Data Pointer.
  1328. * MsgIn 04: Diconnect.
  1329. * In other case, unexpected BUSFREE is detected.
  1330. */
  1331. static int nsp32_busfree_occur(struct scsi_cmnd *SCpnt, unsigned short execph)
  1332. {
  1333. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1334. unsigned int base = SCpnt->device->host->io_port;
  1335. nsp32_dbg(NSP32_DEBUG_BUSFREE, "enter execph=0x%x", execph);
  1336. show_autophase(execph);
  1337. nsp32_write4(base, BM_CNT, 0);
  1338. nsp32_write2(base, TRANSFER_CONTROL, 0);
  1339. /*
  1340. * MsgIn 02: Save Data Pointer
  1341. *
  1342. * VALID:
  1343. * Save Data Pointer is received. Adjust pointer.
  1344. *
  1345. * NO-VALID:
  1346. * SCSI-3 says if Save Data Pointer is not received, then we restart
  1347. * processing and we can't adjust any SCSI data pointer in next data
  1348. * phase.
  1349. */
  1350. if (execph & MSGIN_02_VALID) {
  1351. nsp32_dbg(NSP32_DEBUG_BUSFREE, "MsgIn02_Valid");
  1352. /*
  1353. * Check sack_cnt/saved_sack_cnt, then adjust sg table if
  1354. * needed.
  1355. */
  1356. if (!(execph & MSGIN_00_VALID) &&
  1357. ((execph & DATA_IN_PHASE) || (execph & DATA_OUT_PHASE))) {
  1358. unsigned int sacklen, s_sacklen;
  1359. /*
  1360. * Read SACK count and SAVEDSACK count, then compare.
  1361. */
  1362. sacklen = nsp32_read4(base, SACK_CNT );
  1363. s_sacklen = nsp32_read4(base, SAVED_SACK_CNT);
  1364. /*
  1365. * If SAVEDSACKCNT == 0, it means SavedDataPointer is
  1366. * come after data transferring.
  1367. */
  1368. if (s_sacklen > 0) {
  1369. /*
  1370. * Comparing between sack and savedsack to
  1371. * check the condition of AutoMsgIn03.
  1372. *
  1373. * If they are same, set msgin03 == TRUE,
  1374. * COMMANDCONTROL_AUTO_MSGIN_03 is enabled at
  1375. * reselection. On the other hand, if they
  1376. * aren't same, set msgin03 == FALSE, and
  1377. * COMMANDCONTROL_AUTO_MSGIN_03 is disabled at
  1378. * reselection.
  1379. */
  1380. if (sacklen != s_sacklen) {
  1381. data->cur_lunt->msgin03 = FALSE;
  1382. } else {
  1383. data->cur_lunt->msgin03 = TRUE;
  1384. }
  1385. nsp32_adjust_busfree(SCpnt, s_sacklen);
  1386. }
  1387. }
  1388. /* This value has not substitude with valid value yet... */
  1389. //data->cur_lunt->save_datp = data->cur_datp;
  1390. } else {
  1391. /*
  1392. * no processing.
  1393. */
  1394. }
  1395. if (execph & MSGIN_03_VALID) {
  1396. /* MsgIn03 was valid to be processed. No need processing. */
  1397. }
  1398. /*
  1399. * target SDTR check
  1400. */
  1401. if (data->cur_target->sync_flag & SDTR_INITIATOR) {
  1402. /*
  1403. * SDTR negotiation pulled by the initiator has not
  1404. * finished yet. Fall back to ASYNC mode.
  1405. */
  1406. nsp32_set_async(data, data->cur_target);
  1407. data->cur_target->sync_flag &= ~SDTR_INITIATOR;
  1408. data->cur_target->sync_flag |= SDTR_DONE;
  1409. } else if (data->cur_target->sync_flag & SDTR_TARGET) {
  1410. /*
  1411. * SDTR negotiation pulled by the target has been
  1412. * negotiating.
  1413. */
  1414. if (execph & (MSGIN_00_VALID | MSGIN_04_VALID)) {
  1415. /*
  1416. * If valid message is received, then
  1417. * negotiation is succeeded.
  1418. */
  1419. } else {
  1420. /*
  1421. * On the contrary, if unexpected bus free is
  1422. * occurred, then negotiation is failed. Fall
  1423. * back to ASYNC mode.
  1424. */
  1425. nsp32_set_async(data, data->cur_target);
  1426. }
  1427. data->cur_target->sync_flag &= ~SDTR_TARGET;
  1428. data->cur_target->sync_flag |= SDTR_DONE;
  1429. }
  1430. /*
  1431. * It is always ensured by SCSI standard that initiator
  1432. * switches into Bus Free Phase after
  1433. * receiving message 00 (Command Complete), 04 (Disconnect).
  1434. * It's the reason that processing here is valid.
  1435. */
  1436. if (execph & MSGIN_00_VALID) {
  1437. /* MsgIn 00: Command Complete */
  1438. nsp32_dbg(NSP32_DEBUG_BUSFREE, "command complete");
  1439. SCpnt->SCp.Status = nsp32_read1(base, SCSI_CSB_IN);
  1440. SCpnt->SCp.Message = 0;
  1441. nsp32_dbg(NSP32_DEBUG_BUSFREE,
  1442. "normal end stat=0x%x resid=0x%x\n",
  1443. SCpnt->SCp.Status, scsi_get_resid(SCpnt));
  1444. SCpnt->result = (DID_OK << 16) |
  1445. (SCpnt->SCp.Message << 8) |
  1446. (SCpnt->SCp.Status << 0);
  1447. nsp32_scsi_done(SCpnt);
  1448. /* All operation is done */
  1449. return TRUE;
  1450. } else if (execph & MSGIN_04_VALID) {
  1451. /* MsgIn 04: Disconnect */
  1452. SCpnt->SCp.Status = nsp32_read1(base, SCSI_CSB_IN);
  1453. SCpnt->SCp.Message = 4;
  1454. nsp32_dbg(NSP32_DEBUG_BUSFREE, "disconnect");
  1455. return TRUE;
  1456. } else {
  1457. /* Unexpected bus free */
  1458. nsp32_msg(KERN_WARNING, "unexpected bus free occurred");
  1459. /* DID_ERROR? */
  1460. //SCpnt->result = (DID_OK << 16) | (SCpnt->SCp.Message << 8) | (SCpnt->SCp.Status << 0);
  1461. SCpnt->result = DID_ERROR << 16;
  1462. nsp32_scsi_done(SCpnt);
  1463. return TRUE;
  1464. }
  1465. return FALSE;
  1466. }
  1467. /*
  1468. * nsp32_adjust_busfree - adjusting SG table
  1469. *
  1470. * Note: This driver adjust the SG table using SCSI ACK
  1471. * counter instead of BMCNT counter!
  1472. */
  1473. static void nsp32_adjust_busfree(struct scsi_cmnd *SCpnt, unsigned int s_sacklen)
  1474. {
  1475. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1476. int old_entry = data->cur_entry;
  1477. int new_entry;
  1478. int sg_num = data->cur_lunt->sg_num;
  1479. nsp32_sgtable *sgt = data->cur_lunt->sglun->sgt;
  1480. unsigned int restlen, sentlen;
  1481. u32_le len, addr;
  1482. nsp32_dbg(NSP32_DEBUG_SGLIST, "old resid=0x%x", scsi_get_resid(SCpnt));
  1483. /* adjust saved SACK count with 4 byte start address boundary */
  1484. s_sacklen -= le32_to_cpu(sgt[old_entry].addr) & 3;
  1485. /*
  1486. * calculate new_entry from sack count and each sgt[].len
  1487. * calculate the byte which is intent to send
  1488. */
  1489. sentlen = 0;
  1490. for (new_entry = old_entry; new_entry < sg_num; new_entry++) {
  1491. sentlen += (le32_to_cpu(sgt[new_entry].len) & ~SGTEND);
  1492. if (sentlen > s_sacklen) {
  1493. break;
  1494. }
  1495. }
  1496. /* all sgt is processed */
  1497. if (new_entry == sg_num) {
  1498. goto last;
  1499. }
  1500. if (sentlen == s_sacklen) {
  1501. /* XXX: confirm it's ok or not */
  1502. /* In this case, it's ok because we are at
  1503. the head element of the sg. restlen is correctly calculated. */
  1504. }
  1505. /* calculate the rest length for transferring */
  1506. restlen = sentlen - s_sacklen;
  1507. /* update adjusting current SG table entry */
  1508. len = le32_to_cpu(sgt[new_entry].len);
  1509. addr = le32_to_cpu(sgt[new_entry].addr);
  1510. addr += (len - restlen);
  1511. sgt[new_entry].addr = cpu_to_le32(addr);
  1512. sgt[new_entry].len = cpu_to_le32(restlen);
  1513. /* set cur_entry with new_entry */
  1514. data->cur_entry = new_entry;
  1515. return;
  1516. last:
  1517. if (scsi_get_resid(SCpnt) < sentlen) {
  1518. nsp32_msg(KERN_ERR, "resid underflow");
  1519. }
  1520. scsi_set_resid(SCpnt, scsi_get_resid(SCpnt) - sentlen);
  1521. nsp32_dbg(NSP32_DEBUG_SGLIST, "new resid=0x%x", scsi_get_resid(SCpnt));
  1522. /* update hostdata and lun */
  1523. return;
  1524. }
  1525. /*
  1526. * It's called MsgOut phase occur.
  1527. * NinjaSCSI-32Bi/UDE automatically processes up to 3 messages in
  1528. * message out phase. It, however, has more than 3 messages,
  1529. * HBA creates the interrupt and we have to process by hand.
  1530. */
  1531. static void nsp32_msgout_occur(struct scsi_cmnd *SCpnt)
  1532. {
  1533. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1534. unsigned int base = SCpnt->device->host->io_port;
  1535. //unsigned short command;
  1536. long new_sgtp;
  1537. int i;
  1538. nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR,
  1539. "enter: msgout_len: 0x%x", data->msgout_len);
  1540. /*
  1541. * If MsgOut phase is occurred without having any
  1542. * message, then No_Operation is sent (SCSI-2).
  1543. */
  1544. if (data->msgout_len == 0) {
  1545. nsp32_build_nop(SCpnt);
  1546. }
  1547. /*
  1548. * Set SGTP ADDR current entry for restarting AUTOSCSI,
  1549. * because SGTP is incremented next point.
  1550. * There is few statement in the specification...
  1551. */
  1552. new_sgtp = data->cur_lunt->sglun_paddr +
  1553. (data->cur_lunt->cur_entry * sizeof(nsp32_sgtable));
  1554. /*
  1555. * send messages
  1556. */
  1557. for (i = 0; i < data->msgout_len; i++) {
  1558. nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR,
  1559. "%d : 0x%x", i, data->msgoutbuf[i]);
  1560. /*
  1561. * Check REQ is asserted.
  1562. */
  1563. nsp32_wait_req(data, ASSERT);
  1564. if (i == (data->msgout_len - 1)) {
  1565. /*
  1566. * If the last message, set the AutoSCSI restart
  1567. * before send back the ack message. AutoSCSI
  1568. * restart automatically negate ATN signal.
  1569. */
  1570. //command = (AUTO_MSGIN_00_OR_04 | AUTO_MSGIN_02);
  1571. //nsp32_restart_autoscsi(SCpnt, command);
  1572. nsp32_write2(base, COMMAND_CONTROL,
  1573. (CLEAR_CDB_FIFO_POINTER |
  1574. AUTO_COMMAND_PHASE |
  1575. AUTOSCSI_RESTART |
  1576. AUTO_MSGIN_00_OR_04 |
  1577. AUTO_MSGIN_02 ));
  1578. }
  1579. /*
  1580. * Write data with SACK, then wait sack is
  1581. * automatically negated.
  1582. */
  1583. nsp32_write1(base, SCSI_DATA_WITH_ACK, data->msgoutbuf[i]);
  1584. nsp32_wait_sack(data, NEGATE);
  1585. nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR, "bus: 0x%x\n",
  1586. nsp32_read1(base, SCSI_BUS_MONITOR));
  1587. };
  1588. data->msgout_len = 0;
  1589. nsp32_dbg(NSP32_DEBUG_MSGOUTOCCUR, "exit");
  1590. }
  1591. /*
  1592. * Restart AutoSCSI
  1593. *
  1594. * Note: Restarting AutoSCSI needs set:
  1595. * SYNC_REG, ACK_WIDTH, SGT_ADR, TRANSFER_CONTROL
  1596. */
  1597. static void nsp32_restart_autoscsi(struct scsi_cmnd *SCpnt, unsigned short command)
  1598. {
  1599. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1600. unsigned int base = data->BaseAddress;
  1601. unsigned short transfer = 0;
  1602. nsp32_dbg(NSP32_DEBUG_RESTART, "enter");
  1603. if (data->cur_target == NULL || data->cur_lunt == NULL) {
  1604. nsp32_msg(KERN_ERR, "Target or Lun is invalid");
  1605. }
  1606. /*
  1607. * set SYNC_REG
  1608. * Don't set BM_START_ADR before setting this register.
  1609. */
  1610. nsp32_write1(base, SYNC_REG, data->cur_target->syncreg);
  1611. /*
  1612. * set ACKWIDTH
  1613. */
  1614. nsp32_write1(base, ACK_WIDTH, data->cur_target->ackwidth);
  1615. /*
  1616. * set SREQ hazard killer sampling rate
  1617. */
  1618. nsp32_write1(base, SREQ_SMPL_RATE, data->cur_target->sample_reg);
  1619. /*
  1620. * set SGT ADDR (physical address)
  1621. */
  1622. nsp32_write4(base, SGT_ADR, data->cur_lunt->sglun_paddr);
  1623. /*
  1624. * set TRANSFER CONTROL REG
  1625. */
  1626. transfer = 0;
  1627. transfer |= (TRANSFER_GO | ALL_COUNTER_CLR);
  1628. if (data->trans_method & NSP32_TRANSFER_BUSMASTER) {
  1629. if (scsi_bufflen(SCpnt) > 0) {
  1630. transfer |= BM_START;
  1631. }
  1632. } else if (data->trans_method & NSP32_TRANSFER_MMIO) {
  1633. transfer |= CB_MMIO_MODE;
  1634. } else if (data->trans_method & NSP32_TRANSFER_PIO) {
  1635. transfer |= CB_IO_MODE;
  1636. }
  1637. nsp32_write2(base, TRANSFER_CONTROL, transfer);
  1638. /*
  1639. * restart AutoSCSI
  1640. *
  1641. * TODO: COMMANDCONTROL_AUTO_COMMAND_PHASE is needed ?
  1642. */
  1643. command |= (CLEAR_CDB_FIFO_POINTER |
  1644. AUTO_COMMAND_PHASE |
  1645. AUTOSCSI_RESTART );
  1646. nsp32_write2(base, COMMAND_CONTROL, command);
  1647. nsp32_dbg(NSP32_DEBUG_RESTART, "exit");
  1648. }
  1649. /*
  1650. * cannot run automatically message in occur
  1651. */
  1652. static void nsp32_msgin_occur(struct scsi_cmnd *SCpnt,
  1653. unsigned long irq_status,
  1654. unsigned short execph)
  1655. {
  1656. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1657. unsigned int base = SCpnt->device->host->io_port;
  1658. unsigned char msg;
  1659. unsigned char msgtype;
  1660. unsigned char newlun;
  1661. unsigned short command = 0;
  1662. int msgclear = TRUE;
  1663. long new_sgtp;
  1664. int ret;
  1665. /*
  1666. * read first message
  1667. * Use SCSIDATA_W_ACK instead of SCSIDATAIN, because the procedure
  1668. * of Message-In have to be processed before sending back SCSI ACK.
  1669. */
  1670. msg = nsp32_read1(base, SCSI_DATA_IN);
  1671. data->msginbuf[(unsigned char)data->msgin_len] = msg;
  1672. msgtype = data->msginbuf[0];
  1673. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR,
  1674. "enter: msglen: 0x%x msgin: 0x%x msgtype: 0x%x",
  1675. data->msgin_len, msg, msgtype);
  1676. /*
  1677. * TODO: We need checking whether bus phase is message in?
  1678. */
  1679. /*
  1680. * assert SCSI ACK
  1681. */
  1682. nsp32_sack_assert(data);
  1683. /*
  1684. * processing IDENTIFY
  1685. */
  1686. if (msgtype & 0x80) {
  1687. if (!(irq_status & IRQSTATUS_RESELECT_OCCUER)) {
  1688. /* Invalid (non reselect) phase */
  1689. goto reject;
  1690. }
  1691. newlun = msgtype & 0x1f; /* TODO: SPI-3 compliant? */
  1692. ret = nsp32_reselection(SCpnt, newlun);
  1693. if (ret == TRUE) {
  1694. goto restart;
  1695. } else {
  1696. goto reject;
  1697. }
  1698. }
  1699. /*
  1700. * processing messages except for IDENTIFY
  1701. *
  1702. * TODO: Messages are all SCSI-2 terminology. SCSI-3 compliance is TODO.
  1703. */
  1704. switch (msgtype) {
  1705. /*
  1706. * 1-byte message
  1707. */
  1708. case COMMAND_COMPLETE:
  1709. case DISCONNECT:
  1710. /*
  1711. * These messages should not be occurred.
  1712. * They should be processed on AutoSCSI sequencer.
  1713. */
  1714. nsp32_msg(KERN_WARNING,
  1715. "unexpected message of AutoSCSI MsgIn: 0x%x", msg);
  1716. break;
  1717. case RESTORE_POINTERS:
  1718. /*
  1719. * AutoMsgIn03 is disabled, and HBA gets this message.
  1720. */
  1721. if ((execph & DATA_IN_PHASE) || (execph & DATA_OUT_PHASE)) {
  1722. unsigned int s_sacklen;
  1723. s_sacklen = nsp32_read4(base, SAVED_SACK_CNT);
  1724. if ((execph & MSGIN_02_VALID) && (s_sacklen > 0)) {
  1725. nsp32_adjust_busfree(SCpnt, s_sacklen);
  1726. } else {
  1727. /* No need to rewrite SGT */
  1728. }
  1729. }
  1730. data->cur_lunt->msgin03 = FALSE;
  1731. /* Update with the new value */
  1732. /* reset SACK/SavedACK counter (or ALL clear?) */
  1733. nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
  1734. /*
  1735. * set new sg pointer
  1736. */
  1737. new_sgtp = data->cur_lunt->sglun_paddr +
  1738. (data->cur_lunt->cur_entry * sizeof(nsp32_sgtable));
  1739. nsp32_write4(base, SGT_ADR, new_sgtp);
  1740. break;
  1741. case SAVE_POINTERS:
  1742. /*
  1743. * These messages should not be occurred.
  1744. * They should be processed on AutoSCSI sequencer.
  1745. */
  1746. nsp32_msg (KERN_WARNING,
  1747. "unexpected message of AutoSCSI MsgIn: SAVE_POINTERS");
  1748. break;
  1749. case MESSAGE_REJECT:
  1750. /* If previous message_out is sending SDTR, and get
  1751. message_reject from target, SDTR negotiation is failed */
  1752. if (data->cur_target->sync_flag &
  1753. (SDTR_INITIATOR | SDTR_TARGET)) {
  1754. /*
  1755. * Current target is negotiating SDTR, but it's
  1756. * failed. Fall back to async transfer mode, and set
  1757. * SDTR_DONE.
  1758. */
  1759. nsp32_set_async(data, data->cur_target);
  1760. data->cur_target->sync_flag &= ~SDTR_INITIATOR;
  1761. data->cur_target->sync_flag |= SDTR_DONE;
  1762. }
  1763. break;
  1764. case LINKED_CMD_COMPLETE:
  1765. case LINKED_FLG_CMD_COMPLETE:
  1766. /* queue tag is not supported currently */
  1767. nsp32_msg (KERN_WARNING,
  1768. "unsupported message: 0x%x", msgtype);
  1769. break;
  1770. case INITIATE_RECOVERY:
  1771. /* staring ECA (Extended Contingent Allegiance) state. */
  1772. /* This message is declined in SPI2 or later. */
  1773. goto reject;
  1774. /*
  1775. * 2-byte message
  1776. */
  1777. case SIMPLE_QUEUE_TAG:
  1778. case 0x23:
  1779. /*
  1780. * 0x23: Ignore_Wide_Residue is not declared in scsi.h.
  1781. * No support is needed.
  1782. */
  1783. if (data->msgin_len >= 1) {
  1784. goto reject;
  1785. }
  1786. /* current position is 1-byte of 2 byte */
  1787. msgclear = FALSE;
  1788. break;
  1789. /*
  1790. * extended message
  1791. */
  1792. case EXTENDED_MESSAGE:
  1793. if (data->msgin_len < 1) {
  1794. /*
  1795. * Current position does not reach 2-byte
  1796. * (2-byte is extended message length).
  1797. */
  1798. msgclear = FALSE;
  1799. break;
  1800. }
  1801. if ((data->msginbuf[1] + 1) > data->msgin_len) {
  1802. /*
  1803. * Current extended message has msginbuf[1] + 2
  1804. * (msgin_len starts counting from 0, so buf[1] + 1).
  1805. * If current message position is not finished,
  1806. * continue receiving message.
  1807. */
  1808. msgclear = FALSE;
  1809. break;
  1810. }
  1811. /*
  1812. * Reach here means regular length of each type of
  1813. * extended messages.
  1814. */
  1815. switch (data->msginbuf[2]) {
  1816. case EXTENDED_MODIFY_DATA_POINTER:
  1817. /* TODO */
  1818. goto reject; /* not implemented yet */
  1819. break;
  1820. case EXTENDED_SDTR:
  1821. /*
  1822. * Exchange this message between initiator and target.
  1823. */
  1824. if (data->msgin_len != EXTENDED_SDTR_LEN + 1) {
  1825. /*
  1826. * received inappropriate message.
  1827. */
  1828. goto reject;
  1829. break;
  1830. }
  1831. nsp32_analyze_sdtr(SCpnt);
  1832. break;
  1833. case EXTENDED_EXTENDED_IDENTIFY:
  1834. /* SCSI-I only, not supported. */
  1835. goto reject; /* not implemented yet */
  1836. break;
  1837. case EXTENDED_WDTR:
  1838. goto reject; /* not implemented yet */
  1839. break;
  1840. default:
  1841. goto reject;
  1842. }
  1843. break;
  1844. default:
  1845. goto reject;
  1846. }
  1847. restart:
  1848. if (msgclear == TRUE) {
  1849. data->msgin_len = 0;
  1850. /*
  1851. * If restarting AutoSCSI, but there are some message to out
  1852. * (msgout_len > 0), set AutoATN, and set SCSIMSGOUT as 0
  1853. * (MV_VALID = 0). When commandcontrol is written with
  1854. * AutoSCSI restart, at the same time MsgOutOccur should be
  1855. * happened (however, such situation is really possible...?).
  1856. */
  1857. if (data->msgout_len > 0) {
  1858. nsp32_write4(base, SCSI_MSG_OUT, 0);
  1859. command |= AUTO_ATN;
  1860. }
  1861. /*
  1862. * restart AutoSCSI
  1863. * If it's failed, COMMANDCONTROL_AUTO_COMMAND_PHASE is needed.
  1864. */
  1865. command |= (AUTO_MSGIN_00_OR_04 | AUTO_MSGIN_02);
  1866. /*
  1867. * If current msgin03 is TRUE, then flag on.
  1868. */
  1869. if (data->cur_lunt->msgin03 == TRUE) {
  1870. command |= AUTO_MSGIN_03;
  1871. }
  1872. data->cur_lunt->msgin03 = FALSE;
  1873. } else {
  1874. data->msgin_len++;
  1875. }
  1876. /*
  1877. * restart AutoSCSI
  1878. */
  1879. nsp32_restart_autoscsi(SCpnt, command);
  1880. /*
  1881. * wait SCSI REQ negate for REQ-ACK handshake
  1882. */
  1883. nsp32_wait_req(data, NEGATE);
  1884. /*
  1885. * negate SCSI ACK
  1886. */
  1887. nsp32_sack_negate(data);
  1888. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit");
  1889. return;
  1890. reject:
  1891. nsp32_msg(KERN_WARNING,
  1892. "invalid or unsupported MessageIn, rejected. "
  1893. "current msg: 0x%x (len: 0x%x), processing msg: 0x%x",
  1894. msg, data->msgin_len, msgtype);
  1895. nsp32_build_reject(SCpnt);
  1896. data->msgin_len = 0;
  1897. goto restart;
  1898. }
  1899. /*
  1900. *
  1901. */
  1902. static void nsp32_analyze_sdtr(struct scsi_cmnd *SCpnt)
  1903. {
  1904. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  1905. nsp32_target *target = data->cur_target;
  1906. nsp32_sync_table *synct;
  1907. unsigned char get_period = data->msginbuf[3];
  1908. unsigned char get_offset = data->msginbuf[4];
  1909. int entry;
  1910. int syncnum;
  1911. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "enter");
  1912. synct = data->synct;
  1913. syncnum = data->syncnum;
  1914. /*
  1915. * If this inititor sent the SDTR message, then target responds SDTR,
  1916. * initiator SYNCREG, ACKWIDTH from SDTR parameter.
  1917. * Messages are not appropriate, then send back reject message.
  1918. * If initiator did not send the SDTR, but target sends SDTR,
  1919. * initiator calculator the appropriate parameter and send back SDTR.
  1920. */
  1921. if (target->sync_flag & SDTR_INITIATOR) {
  1922. /*
  1923. * Initiator sent SDTR, the target responds and
  1924. * send back negotiation SDTR.
  1925. */
  1926. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "target responds SDTR");
  1927. target->sync_flag &= ~SDTR_INITIATOR;
  1928. target->sync_flag |= SDTR_DONE;
  1929. /*
  1930. * offset:
  1931. */
  1932. if (get_offset > SYNC_OFFSET) {
  1933. /*
  1934. * Negotiation is failed, the target send back
  1935. * unexpected offset value.
  1936. */
  1937. goto reject;
  1938. }
  1939. if (get_offset == ASYNC_OFFSET) {
  1940. /*
  1941. * Negotiation is succeeded, the target want
  1942. * to fall back into asynchronous transfer mode.
  1943. */
  1944. goto async;
  1945. }
  1946. /*
  1947. * period:
  1948. * Check whether sync period is too short. If too short,
  1949. * fall back to async mode. If it's ok, then investigate
  1950. * the received sync period. If sync period is acceptable
  1951. * between sync table start_period and end_period, then
  1952. * set this I_T nexus as sent offset and period.
  1953. * If it's not acceptable, send back reject and fall back
  1954. * to async mode.
  1955. */
  1956. if (get_period < data->synct[0].period_num) {
  1957. /*
  1958. * Negotiation is failed, the target send back
  1959. * unexpected period value.
  1960. */
  1961. goto reject;
  1962. }
  1963. entry = nsp32_search_period_entry(data, target, get_period);
  1964. if (entry < 0) {
  1965. /*
  1966. * Target want to use long period which is not
  1967. * acceptable NinjaSCSI-32Bi/UDE.
  1968. */
  1969. goto reject;
  1970. }
  1971. /*
  1972. * Set new sync table and offset in this I_T nexus.
  1973. */
  1974. nsp32_set_sync_entry(data, target, entry, get_offset);
  1975. } else {
  1976. /* Target send SDTR to initiator. */
  1977. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "target send SDTR");
  1978. target->sync_flag |= SDTR_INITIATOR;
  1979. /* offset: */
  1980. if (get_offset > SYNC_OFFSET) {
  1981. /* send back as SYNC_OFFSET */
  1982. get_offset = SYNC_OFFSET;
  1983. }
  1984. /* period: */
  1985. if (get_period < data->synct[0].period_num) {
  1986. get_period = data->synct[0].period_num;
  1987. }
  1988. entry = nsp32_search_period_entry(data, target, get_period);
  1989. if (get_offset == ASYNC_OFFSET || entry < 0) {
  1990. nsp32_set_async(data, target);
  1991. nsp32_build_sdtr(SCpnt, 0, ASYNC_OFFSET);
  1992. } else {
  1993. nsp32_set_sync_entry(data, target, entry, get_offset);
  1994. nsp32_build_sdtr(SCpnt, get_period, get_offset);
  1995. }
  1996. }
  1997. target->period = get_period;
  1998. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit");
  1999. return;
  2000. reject:
  2001. /*
  2002. * If the current message is unacceptable, send back to the target
  2003. * with reject message.
  2004. */
  2005. nsp32_build_reject(SCpnt);
  2006. async:
  2007. nsp32_set_async(data, target); /* set as ASYNC transfer mode */
  2008. target->period = 0;
  2009. nsp32_dbg(NSP32_DEBUG_MSGINOCCUR, "exit: set async");
  2010. return;
  2011. }
  2012. /*
  2013. * Search config entry number matched in sync_table from given
  2014. * target and speed period value. If failed to search, return negative value.
  2015. */
  2016. static int nsp32_search_period_entry(nsp32_hw_data *data,
  2017. nsp32_target *target,
  2018. unsigned char period)
  2019. {
  2020. int i;
  2021. if (target->limit_entry >= data->syncnum) {
  2022. nsp32_msg(KERN_ERR, "limit_entry exceeds syncnum!");
  2023. target->limit_entry = 0;
  2024. }
  2025. for (i = target->limit_entry; i < data->syncnum; i++) {
  2026. if (period >= data->synct[i].start_period &&
  2027. period <= data->synct[i].end_period) {
  2028. break;
  2029. }
  2030. }
  2031. /*
  2032. * Check given period value is over the sync_table value.
  2033. * If so, return max value.
  2034. */
  2035. if (i == data->syncnum) {
  2036. i = -1;
  2037. }
  2038. return i;
  2039. }
  2040. /*
  2041. * target <-> initiator use ASYNC transfer
  2042. */
  2043. static void nsp32_set_async(nsp32_hw_data *data, nsp32_target *target)
  2044. {
  2045. unsigned char period = data->synct[target->limit_entry].period_num;
  2046. target->offset = ASYNC_OFFSET;
  2047. target->period = 0;
  2048. target->syncreg = TO_SYNCREG(period, ASYNC_OFFSET);
  2049. target->ackwidth = 0;
  2050. target->sample_reg = 0;
  2051. nsp32_dbg(NSP32_DEBUG_SYNC, "set async");
  2052. }
  2053. /*
  2054. * target <-> initiator use maximum SYNC transfer
  2055. */
  2056. static void nsp32_set_max_sync(nsp32_hw_data *data,
  2057. nsp32_target *target,
  2058. unsigned char *period,
  2059. unsigned char *offset)
  2060. {
  2061. unsigned char period_num, ackwidth;
  2062. period_num = data->synct[target->limit_entry].period_num;
  2063. *period = data->synct[target->limit_entry].start_period;
  2064. ackwidth = data->synct[target->limit_entry].ackwidth;
  2065. *offset = SYNC_OFFSET;
  2066. target->syncreg = TO_SYNCREG(period_num, *offset);
  2067. target->ackwidth = ackwidth;
  2068. target->offset = *offset;
  2069. target->sample_reg = 0; /* disable SREQ sampling */
  2070. }
  2071. /*
  2072. * target <-> initiator use entry number speed
  2073. */
  2074. static void nsp32_set_sync_entry(nsp32_hw_data *data,
  2075. nsp32_target *target,
  2076. int entry,
  2077. unsigned char offset)
  2078. {
  2079. unsigned char period, ackwidth, sample_rate;
  2080. period = data->synct[entry].period_num;
  2081. ackwidth = data->synct[entry].ackwidth;
  2082. offset = offset;
  2083. sample_rate = data->synct[entry].sample_rate;
  2084. target->syncreg = TO_SYNCREG(period, offset);
  2085. target->ackwidth = ackwidth;
  2086. target->offset = offset;
  2087. target->sample_reg = sample_rate | SAMPLING_ENABLE;
  2088. nsp32_dbg(NSP32_DEBUG_SYNC, "set sync");
  2089. }
  2090. /*
  2091. * It waits until SCSI REQ becomes assertion or negation state.
  2092. *
  2093. * Note: If nsp32_msgin_occur is called, we asserts SCSI ACK. Then
  2094. * connected target responds SCSI REQ negation. We have to wait
  2095. * SCSI REQ becomes negation in order to negate SCSI ACK signal for
  2096. * REQ-ACK handshake.
  2097. */
  2098. static void nsp32_wait_req(nsp32_hw_data *data, int state)
  2099. {
  2100. unsigned int base = data->BaseAddress;
  2101. int wait_time = 0;
  2102. unsigned char bus, req_bit;
  2103. if (!((state == ASSERT) || (state == NEGATE))) {
  2104. nsp32_msg(KERN_ERR, "unknown state designation");
  2105. }
  2106. /* REQ is BIT(5) */
  2107. req_bit = (state == ASSERT ? BUSMON_REQ : 0);
  2108. do {
  2109. bus = nsp32_read1(base, SCSI_BUS_MONITOR);
  2110. if ((bus & BUSMON_REQ) == req_bit) {
  2111. nsp32_dbg(NSP32_DEBUG_WAIT,
  2112. "wait_time: %d", wait_time);
  2113. return;
  2114. }
  2115. udelay(1);
  2116. wait_time++;
  2117. } while (wait_time < REQSACK_TIMEOUT_TIME);
  2118. nsp32_msg(KERN_WARNING, "wait REQ timeout, req_bit: 0x%x", req_bit);
  2119. }
  2120. /*
  2121. * It waits until SCSI SACK becomes assertion or negation state.
  2122. */
  2123. static void nsp32_wait_sack(nsp32_hw_data *data, int state)
  2124. {
  2125. unsigned int base = data->BaseAddress;
  2126. int wait_time = 0;
  2127. unsigned char bus, ack_bit;
  2128. if (!((state == ASSERT) || (state == NEGATE))) {
  2129. nsp32_msg(KERN_ERR, "unknown state designation");
  2130. }
  2131. /* ACK is BIT(4) */
  2132. ack_bit = (state == ASSERT ? BUSMON_ACK : 0);
  2133. do {
  2134. bus = nsp32_read1(base, SCSI_BUS_MONITOR);
  2135. if ((bus & BUSMON_ACK) == ack_bit) {
  2136. nsp32_dbg(NSP32_DEBUG_WAIT,
  2137. "wait_time: %d", wait_time);
  2138. return;
  2139. }
  2140. udelay(1);
  2141. wait_time++;
  2142. } while (wait_time < REQSACK_TIMEOUT_TIME);
  2143. nsp32_msg(KERN_WARNING, "wait SACK timeout, ack_bit: 0x%x", ack_bit);
  2144. }
  2145. /*
  2146. * assert SCSI ACK
  2147. *
  2148. * Note: SCSI ACK assertion needs with ACKENB=1, AUTODIRECTION=1.
  2149. */
  2150. static void nsp32_sack_assert(nsp32_hw_data *data)
  2151. {
  2152. unsigned int base = data->BaseAddress;
  2153. unsigned char busctrl;
  2154. busctrl = nsp32_read1(base, SCSI_BUS_CONTROL);
  2155. busctrl |= (BUSCTL_ACK | AUTODIRECTION | ACKENB);
  2156. nsp32_write1(base, SCSI_BUS_CONTROL, busctrl);
  2157. }
  2158. /*
  2159. * negate SCSI ACK
  2160. */
  2161. static void nsp32_sack_negate(nsp32_hw_data *data)
  2162. {
  2163. unsigned int base = data->BaseAddress;
  2164. unsigned char busctrl;
  2165. busctrl = nsp32_read1(base, SCSI_BUS_CONTROL);
  2166. busctrl &= ~BUSCTL_ACK;
  2167. nsp32_write1(base, SCSI_BUS_CONTROL, busctrl);
  2168. }
  2169. /*
  2170. * Note: n_io_port is defined as 0x7f because I/O register port is
  2171. * assigned as:
  2172. * 0x800-0x8ff: memory mapped I/O port
  2173. * 0x900-0xbff: (map same 0x800-0x8ff I/O port image repeatedly)
  2174. * 0xc00-0xfff: CardBus status registers
  2175. */
  2176. static int nsp32_detect(struct pci_dev *pdev)
  2177. {
  2178. struct Scsi_Host *host; /* registered host structure */
  2179. struct resource *res;
  2180. nsp32_hw_data *data;
  2181. int ret;
  2182. int i, j;
  2183. nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
  2184. /*
  2185. * register this HBA as SCSI device
  2186. */
  2187. host = scsi_host_alloc(&nsp32_template, sizeof(nsp32_hw_data));
  2188. if (host == NULL) {
  2189. nsp32_msg (KERN_ERR, "failed to scsi register");
  2190. goto err;
  2191. }
  2192. /*
  2193. * set nsp32_hw_data
  2194. */
  2195. data = (nsp32_hw_data *)host->hostdata;
  2196. memcpy(data, &nsp32_data_base, sizeof(nsp32_hw_data));
  2197. host->irq = data->IrqNumber;
  2198. host->io_port = data->BaseAddress;
  2199. host->unique_id = data->BaseAddress;
  2200. host->n_io_port = data->NumAddress;
  2201. host->base = (unsigned long)data->MmioAddress;
  2202. data->Host = host;
  2203. spin_lock_init(&(data->Lock));
  2204. data->cur_lunt = NULL;
  2205. data->cur_target = NULL;
  2206. /*
  2207. * Bus master transfer mode is supported currently.
  2208. */
  2209. data->trans_method = NSP32_TRANSFER_BUSMASTER;
  2210. /*
  2211. * Set clock div, CLOCK_4 (HBA has own external clock, and
  2212. * dividing * 100ns/4).
  2213. * Currently CLOCK_4 has only tested, not for CLOCK_2/PCICLK yet.
  2214. */
  2215. data->clock = CLOCK_4;
  2216. /*
  2217. * Select appropriate nsp32_sync_table and set I_CLOCKDIV.
  2218. */
  2219. switch (data->clock) {
  2220. case CLOCK_4:
  2221. /* If data->clock is CLOCK_4, then select 40M sync table. */
  2222. data->synct = nsp32_sync_table_40M;
  2223. data->syncnum = ARRAY_SIZE(nsp32_sync_table_40M);
  2224. break;
  2225. case CLOCK_2:
  2226. /* If data->clock is CLOCK_2, then select 20M sync table. */
  2227. data->synct = nsp32_sync_table_20M;
  2228. data->syncnum = ARRAY_SIZE(nsp32_sync_table_20M);
  2229. break;
  2230. case PCICLK:
  2231. /* If data->clock is PCICLK, then select pci sync table. */
  2232. data->synct = nsp32_sync_table_pci;
  2233. data->syncnum = ARRAY_SIZE(nsp32_sync_table_pci);
  2234. break;
  2235. default:
  2236. nsp32_msg(KERN_WARNING,
  2237. "Invalid clock div is selected, set CLOCK_4.");
  2238. /* Use default value CLOCK_4 */
  2239. data->clock = CLOCK_4;
  2240. data->synct = nsp32_sync_table_40M;
  2241. data->syncnum = ARRAY_SIZE(nsp32_sync_table_40M);
  2242. }
  2243. /*
  2244. * setup nsp32_lunt
  2245. */
  2246. /*
  2247. * setup DMA
  2248. */
  2249. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
  2250. nsp32_msg (KERN_ERR, "failed to set PCI DMA mask");
  2251. goto scsi_unregister;
  2252. }
  2253. /*
  2254. * allocate autoparam DMA resource.
  2255. */
  2256. data->autoparam = pci_alloc_consistent(pdev, sizeof(nsp32_autoparam), &(data->auto_paddr));
  2257. if (data->autoparam == NULL) {
  2258. nsp32_msg(KERN_ERR, "failed to allocate DMA memory");
  2259. goto scsi_unregister;
  2260. }
  2261. /*
  2262. * allocate scatter-gather DMA resource.
  2263. */
  2264. data->sg_list = pci_alloc_consistent(pdev, NSP32_SG_TABLE_SIZE,
  2265. &(data->sg_paddr));
  2266. if (data->sg_list == NULL) {
  2267. nsp32_msg(KERN_ERR, "failed to allocate DMA memory");
  2268. goto free_autoparam;
  2269. }
  2270. for (i = 0; i < ARRAY_SIZE(data->lunt); i++) {
  2271. for (j = 0; j < ARRAY_SIZE(data->lunt[0]); j++) {
  2272. int offset = i * ARRAY_SIZE(data->lunt[0]) + j;
  2273. nsp32_lunt tmp = {
  2274. .SCpnt = NULL,
  2275. .save_datp = 0,
  2276. .msgin03 = FALSE,
  2277. .sg_num = 0,
  2278. .cur_entry = 0,
  2279. .sglun = &(data->sg_list[offset]),
  2280. .sglun_paddr = data->sg_paddr + (offset * sizeof(nsp32_sglun)),
  2281. };
  2282. data->lunt[i][j] = tmp;
  2283. }
  2284. }
  2285. /*
  2286. * setup target
  2287. */
  2288. for (i = 0; i < ARRAY_SIZE(data->target); i++) {
  2289. nsp32_target *target = &(data->target[i]);
  2290. target->limit_entry = 0;
  2291. target->sync_flag = 0;
  2292. nsp32_set_async(data, target);
  2293. }
  2294. /*
  2295. * EEPROM check
  2296. */
  2297. ret = nsp32_getprom_param(data);
  2298. if (ret == FALSE) {
  2299. data->resettime = 3; /* default 3 */
  2300. }
  2301. /*
  2302. * setup HBA
  2303. */
  2304. nsp32hw_init(data);
  2305. snprintf(data->info_str, sizeof(data->info_str),
  2306. "NinjaSCSI-32Bi/UDE: irq %d, io 0x%lx+0x%x",
  2307. host->irq, host->io_port, host->n_io_port);
  2308. /*
  2309. * SCSI bus reset
  2310. *
  2311. * Note: It's important to reset SCSI bus in initialization phase.
  2312. * NinjaSCSI-32Bi/UDE HBA EEPROM seems to exchange SDTR when
  2313. * system is coming up, so SCSI devices connected to HBA is set as
  2314. * un-asynchronous mode. It brings the merit that this HBA is
  2315. * ready to start synchronous transfer without any preparation,
  2316. * but we are difficult to control transfer speed. In addition,
  2317. * it prevents device transfer speed from effecting EEPROM start-up
  2318. * SDTR. NinjaSCSI-32Bi/UDE has the feature if EEPROM is set as
  2319. * Auto Mode, then FAST-10M is selected when SCSI devices are
  2320. * connected same or more than 4 devices. It should be avoided
  2321. * depending on this specification. Thus, resetting the SCSI bus
  2322. * restores all connected SCSI devices to asynchronous mode, then
  2323. * this driver set SDTR safely later, and we can control all SCSI
  2324. * device transfer mode.
  2325. */
  2326. nsp32_do_bus_reset(data);
  2327. ret = request_irq(host->irq, do_nsp32_isr, IRQF_SHARED, "nsp32", data);
  2328. if (ret < 0) {
  2329. nsp32_msg(KERN_ERR, "Unable to allocate IRQ for NinjaSCSI32 "
  2330. "SCSI PCI controller. Interrupt: %d", host->irq);
  2331. goto free_sg_list;
  2332. }
  2333. /*
  2334. * PCI IO register
  2335. */
  2336. res = request_region(host->io_port, host->n_io_port, "nsp32");
  2337. if (res == NULL) {
  2338. nsp32_msg(KERN_ERR,
  2339. "I/O region 0x%lx+0x%lx is already used",
  2340. data->BaseAddress, data->NumAddress);
  2341. goto free_irq;
  2342. }
  2343. ret = scsi_add_host(host, &pdev->dev);
  2344. if (ret) {
  2345. nsp32_msg(KERN_ERR, "failed to add scsi host");
  2346. goto free_region;
  2347. }
  2348. scsi_scan_host(host);
  2349. pci_set_drvdata(pdev, host);
  2350. return 0;
  2351. free_region:
  2352. release_region(host->io_port, host->n_io_port);
  2353. free_irq:
  2354. free_irq(host->irq, data);
  2355. free_sg_list:
  2356. pci_free_consistent(pdev, NSP32_SG_TABLE_SIZE,
  2357. data->sg_list, data->sg_paddr);
  2358. free_autoparam:
  2359. pci_free_consistent(pdev, sizeof(nsp32_autoparam),
  2360. data->autoparam, data->auto_paddr);
  2361. scsi_unregister:
  2362. scsi_host_put(host);
  2363. err:
  2364. return 1;
  2365. }
  2366. static int nsp32_release(struct Scsi_Host *host)
  2367. {
  2368. nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
  2369. if (data->autoparam) {
  2370. pci_free_consistent(data->Pci, sizeof(nsp32_autoparam),
  2371. data->autoparam, data->auto_paddr);
  2372. }
  2373. if (data->sg_list) {
  2374. pci_free_consistent(data->Pci, NSP32_SG_TABLE_SIZE,
  2375. data->sg_list, data->sg_paddr);
  2376. }
  2377. if (host->irq) {
  2378. free_irq(host->irq, data);
  2379. }
  2380. if (host->io_port && host->n_io_port) {
  2381. release_region(host->io_port, host->n_io_port);
  2382. }
  2383. if (data->MmioAddress) {
  2384. iounmap(data->MmioAddress);
  2385. }
  2386. return 0;
  2387. }
  2388. static const char *nsp32_info(struct Scsi_Host *shpnt)
  2389. {
  2390. nsp32_hw_data *data = (nsp32_hw_data *)shpnt->hostdata;
  2391. return data->info_str;
  2392. }
  2393. /****************************************************************************
  2394. * error handler
  2395. */
  2396. static int nsp32_eh_abort(struct scsi_cmnd *SCpnt)
  2397. {
  2398. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  2399. unsigned int base = SCpnt->device->host->io_port;
  2400. nsp32_msg(KERN_WARNING, "abort");
  2401. if (data->cur_lunt->SCpnt == NULL) {
  2402. nsp32_dbg(NSP32_DEBUG_BUSRESET, "abort failed");
  2403. return FAILED;
  2404. }
  2405. if (data->cur_target->sync_flag & (SDTR_INITIATOR | SDTR_TARGET)) {
  2406. /* reset SDTR negotiation */
  2407. data->cur_target->sync_flag = 0;
  2408. nsp32_set_async(data, data->cur_target);
  2409. }
  2410. nsp32_write2(base, TRANSFER_CONTROL, 0);
  2411. nsp32_write2(base, BM_CNT, 0);
  2412. SCpnt->result = DID_ABORT << 16;
  2413. nsp32_scsi_done(SCpnt);
  2414. nsp32_dbg(NSP32_DEBUG_BUSRESET, "abort success");
  2415. return SUCCESS;
  2416. }
  2417. static int nsp32_eh_bus_reset(struct scsi_cmnd *SCpnt)
  2418. {
  2419. nsp32_hw_data *data = (nsp32_hw_data *)SCpnt->device->host->hostdata;
  2420. unsigned int base = SCpnt->device->host->io_port;
  2421. spin_lock_irq(SCpnt->device->host->host_lock);
  2422. nsp32_msg(KERN_INFO, "Bus Reset");
  2423. nsp32_dbg(NSP32_DEBUG_BUSRESET, "SCpnt=0x%x", SCpnt);
  2424. nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
  2425. nsp32_do_bus_reset(data);
  2426. nsp32_write2(base, IRQ_CONTROL, 0);
  2427. spin_unlock_irq(SCpnt->device->host->host_lock);
  2428. return SUCCESS; /* SCSI bus reset is succeeded at any time. */
  2429. }
  2430. static void nsp32_do_bus_reset(nsp32_hw_data *data)
  2431. {
  2432. unsigned int base = data->BaseAddress;
  2433. unsigned short intrdat;
  2434. int i;
  2435. nsp32_dbg(NSP32_DEBUG_BUSRESET, "in");
  2436. /*
  2437. * stop all transfer
  2438. * clear TRANSFERCONTROL_BM_START
  2439. * clear counter
  2440. */
  2441. nsp32_write2(base, TRANSFER_CONTROL, 0);
  2442. nsp32_write4(base, BM_CNT, 0);
  2443. nsp32_write4(base, CLR_COUNTER, CLRCOUNTER_ALLMASK);
  2444. /*
  2445. * fall back to asynchronous transfer mode
  2446. * initialize SDTR negotiation flag
  2447. */
  2448. for (i = 0; i < ARRAY_SIZE(data->target); i++) {
  2449. nsp32_target *target = &data->target[i];
  2450. target->sync_flag = 0;
  2451. nsp32_set_async(data, target);
  2452. }
  2453. /*
  2454. * reset SCSI bus
  2455. */
  2456. nsp32_write1(base, SCSI_BUS_CONTROL, BUSCTL_RST);
  2457. mdelay(RESET_HOLD_TIME / 1000);
  2458. nsp32_write1(base, SCSI_BUS_CONTROL, 0);
  2459. for(i = 0; i < 5; i++) {
  2460. intrdat = nsp32_read2(base, IRQ_STATUS); /* dummy read */
  2461. nsp32_dbg(NSP32_DEBUG_BUSRESET, "irq:1: 0x%x", intrdat);
  2462. }
  2463. data->CurrentSC = NULL;
  2464. }
  2465. static int nsp32_eh_host_reset(struct scsi_cmnd *SCpnt)
  2466. {
  2467. struct Scsi_Host *host = SCpnt->device->host;
  2468. unsigned int base = SCpnt->device->host->io_port;
  2469. nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
  2470. nsp32_msg(KERN_INFO, "Host Reset");
  2471. nsp32_dbg(NSP32_DEBUG_BUSRESET, "SCpnt=0x%x", SCpnt);
  2472. spin_lock_irq(SCpnt->device->host->host_lock);
  2473. nsp32hw_init(data);
  2474. nsp32_write2(base, IRQ_CONTROL, IRQ_CONTROL_ALL_IRQ_MASK);
  2475. nsp32_do_bus_reset(data);
  2476. nsp32_write2(base, IRQ_CONTROL, 0);
  2477. spin_unlock_irq(SCpnt->device->host->host_lock);
  2478. return SUCCESS; /* Host reset is succeeded at any time. */
  2479. }
  2480. /**************************************************************************
  2481. * EEPROM handler
  2482. */
  2483. /*
  2484. * getting EEPROM parameter
  2485. */
  2486. static int nsp32_getprom_param(nsp32_hw_data *data)
  2487. {
  2488. int vendor = data->pci_devid->vendor;
  2489. int device = data->pci_devid->device;
  2490. int ret, val, i;
  2491. /*
  2492. * EEPROM checking.
  2493. */
  2494. ret = nsp32_prom_read(data, 0x7e);
  2495. if (ret != 0x55) {
  2496. nsp32_msg(KERN_INFO, "No EEPROM detected: 0x%x", ret);
  2497. return FALSE;
  2498. }
  2499. ret = nsp32_prom_read(data, 0x7f);
  2500. if (ret != 0xaa) {
  2501. nsp32_msg(KERN_INFO, "Invalid number: 0x%x", ret);
  2502. return FALSE;
  2503. }
  2504. /*
  2505. * check EEPROM type
  2506. */
  2507. if (vendor == PCI_VENDOR_ID_WORKBIT &&
  2508. device == PCI_DEVICE_ID_WORKBIT_STANDARD) {
  2509. ret = nsp32_getprom_c16(data);
  2510. } else if (vendor == PCI_VENDOR_ID_WORKBIT &&
  2511. device == PCI_DEVICE_ID_NINJASCSI_32BIB_LOGITEC) {
  2512. ret = nsp32_getprom_at24(data);
  2513. } else if (vendor == PCI_VENDOR_ID_WORKBIT &&
  2514. device == PCI_DEVICE_ID_NINJASCSI_32UDE_MELCO ) {
  2515. ret = nsp32_getprom_at24(data);
  2516. } else {
  2517. nsp32_msg(KERN_WARNING, "Unknown EEPROM");
  2518. ret = FALSE;
  2519. }
  2520. /* for debug : SPROM data full checking */
  2521. for (i = 0; i <= 0x1f; i++) {
  2522. val = nsp32_prom_read(data, i);
  2523. nsp32_dbg(NSP32_DEBUG_EEPROM,
  2524. "rom address 0x%x : 0x%x", i, val);
  2525. }
  2526. return ret;
  2527. }
  2528. /*
  2529. * AT24C01A (Logitec: LHA-600S), AT24C02 (Melco Buffalo: IFC-USLP) data map:
  2530. *
  2531. * ROMADDR
  2532. * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
  2533. * Value 0x0: ASYNC, 0x0c: Ultra-20M, 0x19: Fast-10M
  2534. * 0x07 : HBA Synchronous Transfer Period
  2535. * Value 0: AutoSync, 1: Manual Setting
  2536. * 0x08 - 0x0f : Not Used? (0x0)
  2537. * 0x10 : Bus Termination
  2538. * Value 0: Auto[ON], 1: ON, 2: OFF
  2539. * 0x11 : Not Used? (0)
  2540. * 0x12 : Bus Reset Delay Time (0x03)
  2541. * 0x13 : Bootable CD Support
  2542. * Value 0: Disable, 1: Enable
  2543. * 0x14 : Device Scan
  2544. * Bit 7 6 5 4 3 2 1 0
  2545. * | <----------------->
  2546. * | SCSI ID: Value 0: Skip, 1: YES
  2547. * |-> Value 0: ALL scan, Value 1: Manual
  2548. * 0x15 - 0x1b : Not Used? (0)
  2549. * 0x1c : Constant? (0x01) (clock div?)
  2550. * 0x1d - 0x7c : Not Used (0xff)
  2551. * 0x7d : Not Used? (0xff)
  2552. * 0x7e : Constant (0x55), Validity signature
  2553. * 0x7f : Constant (0xaa), Validity signature
  2554. */
  2555. static int nsp32_getprom_at24(nsp32_hw_data *data)
  2556. {
  2557. int ret, i;
  2558. int auto_sync;
  2559. nsp32_target *target;
  2560. int entry;
  2561. /*
  2562. * Reset time which is designated by EEPROM.
  2563. *
  2564. * TODO: Not used yet.
  2565. */
  2566. data->resettime = nsp32_prom_read(data, 0x12);
  2567. /*
  2568. * HBA Synchronous Transfer Period
  2569. *
  2570. * Note: auto_sync = 0: auto, 1: manual. Ninja SCSI HBA spec says
  2571. * that if auto_sync is 0 (auto), and connected SCSI devices are
  2572. * same or lower than 3, then transfer speed is set as ULTRA-20M.
  2573. * On the contrary if connected SCSI devices are same or higher
  2574. * than 4, then transfer speed is set as FAST-10M.
  2575. *
  2576. * I break this rule. The number of connected SCSI devices are
  2577. * only ignored. If auto_sync is 0 (auto), then transfer speed is
  2578. * forced as ULTRA-20M.
  2579. */
  2580. ret = nsp32_prom_read(data, 0x07);
  2581. switch (ret) {
  2582. case 0:
  2583. auto_sync = TRUE;
  2584. break;
  2585. case 1:
  2586. auto_sync = FALSE;
  2587. break;
  2588. default:
  2589. nsp32_msg(KERN_WARNING,
  2590. "Unsupported Auto Sync mode. Fall back to manual mode.");
  2591. auto_sync = TRUE;
  2592. }
  2593. if (trans_mode == ULTRA20M_MODE) {
  2594. auto_sync = TRUE;
  2595. }
  2596. /*
  2597. * each device Synchronous Transfer Period
  2598. */
  2599. for (i = 0; i < NSP32_HOST_SCSIID; i++) {
  2600. target = &data->target[i];
  2601. if (auto_sync == TRUE) {
  2602. target->limit_entry = 0; /* set as ULTRA20M */
  2603. } else {
  2604. ret = nsp32_prom_read(data, i);
  2605. entry = nsp32_search_period_entry(data, target, ret);
  2606. if (entry < 0) {
  2607. /* search failed... set maximum speed */
  2608. entry = 0;
  2609. }
  2610. target->limit_entry = entry;
  2611. }
  2612. }
  2613. return TRUE;
  2614. }
  2615. /*
  2616. * C16 110 (I-O Data: SC-NBD) data map:
  2617. *
  2618. * ROMADDR
  2619. * 0x00 - 0x06 : Device Synchronous Transfer Period (SCSI ID 0 - 6)
  2620. * Value 0x0: 20MB/S, 0x1: 10MB/S, 0x2: 5MB/S, 0x3: ASYNC
  2621. * 0x07 : 0 (HBA Synchronous Transfer Period: Auto Sync)
  2622. * 0x08 - 0x0f : Not Used? (0x0)
  2623. * 0x10 : Transfer Mode
  2624. * Value 0: PIO, 1: Busmater
  2625. * 0x11 : Bus Reset Delay Time (0x00-0x20)
  2626. * 0x12 : Bus Termination
  2627. * Value 0: Disable, 1: Enable
  2628. * 0x13 - 0x19 : Disconnection
  2629. * Value 0: Disable, 1: Enable
  2630. * 0x1a - 0x7c : Not Used? (0)
  2631. * 0x7d : Not Used? (0xf8)
  2632. * 0x7e : Constant (0x55), Validity signature
  2633. * 0x7f : Constant (0xaa), Validity signature
  2634. */
  2635. static int nsp32_getprom_c16(nsp32_hw_data *data)
  2636. {
  2637. int ret, i;
  2638. nsp32_target *target;
  2639. int entry, val;
  2640. /*
  2641. * Reset time which is designated by EEPROM.
  2642. *
  2643. * TODO: Not used yet.
  2644. */
  2645. data->resettime = nsp32_prom_read(data, 0x11);
  2646. /*
  2647. * each device Synchronous Transfer Period
  2648. */
  2649. for (i = 0; i < NSP32_HOST_SCSIID; i++) {
  2650. target = &data->target[i];
  2651. ret = nsp32_prom_read(data, i);
  2652. switch (ret) {
  2653. case 0: /* 20MB/s */
  2654. val = 0x0c;
  2655. break;
  2656. case 1: /* 10MB/s */
  2657. val = 0x19;
  2658. break;
  2659. case 2: /* 5MB/s */
  2660. val = 0x32;
  2661. break;
  2662. case 3: /* ASYNC */
  2663. val = 0x00;
  2664. break;
  2665. default: /* default 20MB/s */
  2666. val = 0x0c;
  2667. break;
  2668. }
  2669. entry = nsp32_search_period_entry(data, target, val);
  2670. if (entry < 0 || trans_mode == ULTRA20M_MODE) {
  2671. /* search failed... set maximum speed */
  2672. entry = 0;
  2673. }
  2674. target->limit_entry = entry;
  2675. }
  2676. return TRUE;
  2677. }
  2678. /*
  2679. * Atmel AT24C01A (drived in 5V) serial EEPROM routines
  2680. */
  2681. static int nsp32_prom_read(nsp32_hw_data *data, int romaddr)
  2682. {
  2683. int i, val;
  2684. /* start condition */
  2685. nsp32_prom_start(data);
  2686. /* device address */
  2687. nsp32_prom_write_bit(data, 1); /* 1 */
  2688. nsp32_prom_write_bit(data, 0); /* 0 */
  2689. nsp32_prom_write_bit(data, 1); /* 1 */
  2690. nsp32_prom_write_bit(data, 0); /* 0 */
  2691. nsp32_prom_write_bit(data, 0); /* A2: 0 (GND) */
  2692. nsp32_prom_write_bit(data, 0); /* A1: 0 (GND) */
  2693. nsp32_prom_write_bit(data, 0); /* A0: 0 (GND) */
  2694. /* R/W: W for dummy write */
  2695. nsp32_prom_write_bit(data, 0);
  2696. /* ack */
  2697. nsp32_prom_write_bit(data, 0);
  2698. /* word address */
  2699. for (i = 7; i >= 0; i--) {
  2700. nsp32_prom_write_bit(data, ((romaddr >> i) & 1));
  2701. }
  2702. /* ack */
  2703. nsp32_prom_write_bit(data, 0);
  2704. /* start condition */
  2705. nsp32_prom_start(data);
  2706. /* device address */
  2707. nsp32_prom_write_bit(data, 1); /* 1 */
  2708. nsp32_prom_write_bit(data, 0); /* 0 */
  2709. nsp32_prom_write_bit(data, 1); /* 1 */
  2710. nsp32_prom_write_bit(data, 0); /* 0 */
  2711. nsp32_prom_write_bit(data, 0); /* A2: 0 (GND) */
  2712. nsp32_prom_write_bit(data, 0); /* A1: 0 (GND) */
  2713. nsp32_prom_write_bit(data, 0); /* A0: 0 (GND) */
  2714. /* R/W: R */
  2715. nsp32_prom_write_bit(data, 1);
  2716. /* ack */
  2717. nsp32_prom_write_bit(data, 0);
  2718. /* data... */
  2719. val = 0;
  2720. for (i = 7; i >= 0; i--) {
  2721. val += (nsp32_prom_read_bit(data) << i);
  2722. }
  2723. /* no ack */
  2724. nsp32_prom_write_bit(data, 1);
  2725. /* stop condition */
  2726. nsp32_prom_stop(data);
  2727. return val;
  2728. }
  2729. static void nsp32_prom_set(nsp32_hw_data *data, int bit, int val)
  2730. {
  2731. int base = data->BaseAddress;
  2732. int tmp;
  2733. tmp = nsp32_index_read1(base, SERIAL_ROM_CTL);
  2734. if (val == 0) {
  2735. tmp &= ~bit;
  2736. } else {
  2737. tmp |= bit;
  2738. }
  2739. nsp32_index_write1(base, SERIAL_ROM_CTL, tmp);
  2740. udelay(10);
  2741. }
  2742. static int nsp32_prom_get(nsp32_hw_data *data, int bit)
  2743. {
  2744. int base = data->BaseAddress;
  2745. int tmp, ret;
  2746. if (bit != SDA) {
  2747. nsp32_msg(KERN_ERR, "return value is not appropriate");
  2748. return 0;
  2749. }
  2750. tmp = nsp32_index_read1(base, SERIAL_ROM_CTL) & bit;
  2751. if (tmp == 0) {
  2752. ret = 0;
  2753. } else {
  2754. ret = 1;
  2755. }
  2756. udelay(10);
  2757. return ret;
  2758. }
  2759. static void nsp32_prom_start (nsp32_hw_data *data)
  2760. {
  2761. /* start condition */
  2762. nsp32_prom_set(data, SCL, 1);
  2763. nsp32_prom_set(data, SDA, 1);
  2764. nsp32_prom_set(data, ENA, 1); /* output mode */
  2765. nsp32_prom_set(data, SDA, 0); /* keeping SCL=1 and transiting
  2766. * SDA 1->0 is start condition */
  2767. nsp32_prom_set(data, SCL, 0);
  2768. }
  2769. static void nsp32_prom_stop (nsp32_hw_data *data)
  2770. {
  2771. /* stop condition */
  2772. nsp32_prom_set(data, SCL, 1);
  2773. nsp32_prom_set(data, SDA, 0);
  2774. nsp32_prom_set(data, ENA, 1); /* output mode */
  2775. nsp32_prom_set(data, SDA, 1);
  2776. nsp32_prom_set(data, SCL, 0);
  2777. }
  2778. static void nsp32_prom_write_bit(nsp32_hw_data *data, int val)
  2779. {
  2780. /* write */
  2781. nsp32_prom_set(data, SDA, val);
  2782. nsp32_prom_set(data, SCL, 1 );
  2783. nsp32_prom_set(data, SCL, 0 );
  2784. }
  2785. static int nsp32_prom_read_bit(nsp32_hw_data *data)
  2786. {
  2787. int val;
  2788. /* read */
  2789. nsp32_prom_set(data, ENA, 0); /* input mode */
  2790. nsp32_prom_set(data, SCL, 1);
  2791. val = nsp32_prom_get(data, SDA);
  2792. nsp32_prom_set(data, SCL, 0);
  2793. nsp32_prom_set(data, ENA, 1); /* output mode */
  2794. return val;
  2795. }
  2796. /**************************************************************************
  2797. * Power Management
  2798. */
  2799. #ifdef CONFIG_PM
  2800. /* Device suspended */
  2801. static int nsp32_suspend(struct pci_dev *pdev, pm_message_t state)
  2802. {
  2803. struct Scsi_Host *host = pci_get_drvdata(pdev);
  2804. nsp32_msg(KERN_INFO, "pci-suspend: pdev=0x%p, state=%ld, slot=%s, host=0x%p", pdev, state, pci_name(pdev), host);
  2805. pci_save_state (pdev);
  2806. pci_disable_device (pdev);
  2807. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2808. return 0;
  2809. }
  2810. /* Device woken up */
  2811. static int nsp32_resume(struct pci_dev *pdev)
  2812. {
  2813. struct Scsi_Host *host = pci_get_drvdata(pdev);
  2814. nsp32_hw_data *data = (nsp32_hw_data *)host->hostdata;
  2815. unsigned short reg;
  2816. nsp32_msg(KERN_INFO, "pci-resume: pdev=0x%p, slot=%s, host=0x%p", pdev, pci_name(pdev), host);
  2817. pci_set_power_state(pdev, PCI_D0);
  2818. pci_enable_wake (pdev, PCI_D0, 0);
  2819. pci_restore_state (pdev);
  2820. reg = nsp32_read2(data->BaseAddress, INDEX_REG);
  2821. nsp32_msg(KERN_INFO, "io=0x%x reg=0x%x", data->BaseAddress, reg);
  2822. if (reg == 0xffff) {
  2823. nsp32_msg(KERN_INFO, "missing device. abort resume.");
  2824. return 0;
  2825. }
  2826. nsp32hw_init (data);
  2827. nsp32_do_bus_reset(data);
  2828. nsp32_msg(KERN_INFO, "resume success");
  2829. return 0;
  2830. }
  2831. #endif
  2832. /************************************************************************
  2833. * PCI/Cardbus probe/remove routine
  2834. */
  2835. static int nsp32_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2836. {
  2837. int ret;
  2838. nsp32_hw_data *data = &nsp32_data_base;
  2839. nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
  2840. ret = pci_enable_device(pdev);
  2841. if (ret) {
  2842. nsp32_msg(KERN_ERR, "failed to enable pci device");
  2843. return ret;
  2844. }
  2845. data->Pci = pdev;
  2846. data->pci_devid = id;
  2847. data->IrqNumber = pdev->irq;
  2848. data->BaseAddress = pci_resource_start(pdev, 0);
  2849. data->NumAddress = pci_resource_len (pdev, 0);
  2850. data->MmioAddress = pci_ioremap_bar(pdev, 1);
  2851. data->MmioLength = pci_resource_len (pdev, 1);
  2852. pci_set_master(pdev);
  2853. ret = nsp32_detect(pdev);
  2854. nsp32_msg(KERN_INFO, "irq: %i mmio: %p+0x%lx slot: %s model: %s",
  2855. pdev->irq,
  2856. data->MmioAddress, data->MmioLength,
  2857. pci_name(pdev),
  2858. nsp32_model[id->driver_data]);
  2859. nsp32_dbg(NSP32_DEBUG_REGISTER, "exit %d", ret);
  2860. return ret;
  2861. }
  2862. static void nsp32_remove(struct pci_dev *pdev)
  2863. {
  2864. struct Scsi_Host *host = pci_get_drvdata(pdev);
  2865. nsp32_dbg(NSP32_DEBUG_REGISTER, "enter");
  2866. scsi_remove_host(host);
  2867. nsp32_release(host);
  2868. scsi_host_put(host);
  2869. }
  2870. static struct pci_driver nsp32_driver = {
  2871. .name = "nsp32",
  2872. .id_table = nsp32_pci_table,
  2873. .probe = nsp32_probe,
  2874. .remove = nsp32_remove,
  2875. #ifdef CONFIG_PM
  2876. .suspend = nsp32_suspend,
  2877. .resume = nsp32_resume,
  2878. #endif
  2879. };
  2880. /*********************************************************************
  2881. * Moule entry point
  2882. */
  2883. static int __init init_nsp32(void) {
  2884. nsp32_msg(KERN_INFO, "loading...");
  2885. return pci_register_driver(&nsp32_driver);
  2886. }
  2887. static void __exit exit_nsp32(void) {
  2888. nsp32_msg(KERN_INFO, "unloading...");
  2889. pci_unregister_driver(&nsp32_driver);
  2890. }
  2891. module_init(init_nsp32);
  2892. module_exit(exit_nsp32);
  2893. /* end */