sbp2.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709
  1. /*
  2. * sbp2.c - SBP-2 protocol driver for IEEE-1394
  3. *
  4. * Copyright (C) 2000 James Goodwin, Filanet Corporation (www.filanet.com)
  5. * jamesg@filanet.com (JSG)
  6. *
  7. * Copyright (C) 2003 Ben Collins <bcollins@debian.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. /*
  24. * Brief Description:
  25. *
  26. * This driver implements the Serial Bus Protocol 2 (SBP-2) over IEEE-1394
  27. * under Linux. The SBP-2 driver is implemented as an IEEE-1394 high-level
  28. * driver. It also registers as a SCSI lower-level driver in order to accept
  29. * SCSI commands for transport using SBP-2.
  30. *
  31. * You may access any attached SBP-2 storage devices as if they were SCSI
  32. * devices (e.g. mount /dev/sda1, fdisk, mkfs, etc.).
  33. *
  34. * Current Issues:
  35. *
  36. * - Error Handling: SCSI aborts and bus reset requests are handled somewhat
  37. * but the code needs additional debugging.
  38. */
  39. #include <linux/blkdev.h>
  40. #include <linux/compiler.h>
  41. #include <linux/delay.h>
  42. #include <linux/device.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/gfp.h>
  45. #include <linux/init.h>
  46. #include <linux/kernel.h>
  47. #include <linux/list.h>
  48. #include <linux/module.h>
  49. #include <linux/moduleparam.h>
  50. #include <linux/pci.h>
  51. #include <linux/slab.h>
  52. #include <linux/spinlock.h>
  53. #include <linux/stat.h>
  54. #include <linux/string.h>
  55. #include <linux/stringify.h>
  56. #include <linux/types.h>
  57. #include <linux/wait.h>
  58. #include <asm/byteorder.h>
  59. #include <asm/errno.h>
  60. #include <asm/param.h>
  61. #include <asm/scatterlist.h>
  62. #include <asm/system.h>
  63. #include <asm/types.h>
  64. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  65. #include <asm/io.h> /* for bus_to_virt */
  66. #endif
  67. #include <scsi/scsi.h>
  68. #include <scsi/scsi_cmnd.h>
  69. #include <scsi/scsi_dbg.h>
  70. #include <scsi/scsi_device.h>
  71. #include <scsi/scsi_host.h>
  72. #include "csr1212.h"
  73. #include "highlevel.h"
  74. #include "hosts.h"
  75. #include "ieee1394.h"
  76. #include "ieee1394_core.h"
  77. #include "ieee1394_hotplug.h"
  78. #include "ieee1394_transactions.h"
  79. #include "ieee1394_types.h"
  80. #include "nodemgr.h"
  81. #include "sbp2.h"
  82. /*
  83. * Module load parameter definitions
  84. */
  85. /*
  86. * Change max_speed on module load if you have a bad IEEE-1394
  87. * controller that has trouble running 2KB packets at 400mb.
  88. *
  89. * NOTE: On certain OHCI parts I have seen short packets on async transmit
  90. * (probably due to PCI latency/throughput issues with the part). You can
  91. * bump down the speed if you are running into problems.
  92. */
  93. static int max_speed = IEEE1394_SPEED_MAX;
  94. module_param(max_speed, int, 0644);
  95. MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb, 1 = 200mb, 0 = 100mb)");
  96. /*
  97. * Set serialize_io to 1 if you'd like only one scsi command sent
  98. * down to us at a time (debugging). This might be necessary for very
  99. * badly behaved sbp2 devices.
  100. *
  101. * TODO: Make this configurable per device.
  102. */
  103. static int serialize_io = 1;
  104. module_param(serialize_io, int, 0444);
  105. MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers (default = 1, faster = 0)");
  106. /*
  107. * Bump up max_sectors if you'd like to support very large sized
  108. * transfers. Please note that some older sbp2 bridge chips are broken for
  109. * transfers greater or equal to 128KB. Default is a value of 255
  110. * sectors, or just under 128KB (at 512 byte sector size). I can note that
  111. * the Oxsemi sbp2 chipsets have no problems supporting very large
  112. * transfer sizes.
  113. */
  114. static int max_sectors = SBP2_MAX_SECTORS;
  115. module_param(max_sectors, int, 0444);
  116. MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = "
  117. __stringify(SBP2_MAX_SECTORS) ")");
  118. /*
  119. * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
  120. * do an exclusive login, as it's generally unsafe to have two hosts
  121. * talking to a single sbp2 device at the same time (filesystem coherency,
  122. * etc.). If you're running an sbp2 device that supports multiple logins,
  123. * and you're either running read-only filesystems or some sort of special
  124. * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
  125. * File System, or Lustre, then set exclusive_login to zero.
  126. *
  127. * So far only bridges from Oxford Semiconductor are known to support
  128. * concurrent logins. Depending on firmware, four or two concurrent logins
  129. * are possible on OXFW911 and newer Oxsemi bridges.
  130. */
  131. static int exclusive_login = 1;
  132. module_param(exclusive_login, int, 0644);
  133. MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)");
  134. /*
  135. * If any of the following workarounds is required for your device to work,
  136. * please submit the kernel messages logged by sbp2 to the linux1394-devel
  137. * mailing list.
  138. *
  139. * - 128kB max transfer
  140. * Limit transfer size. Necessary for some old bridges.
  141. *
  142. * - 36 byte inquiry
  143. * When scsi_mod probes the device, let the inquiry command look like that
  144. * from MS Windows.
  145. *
  146. * - skip mode page 8
  147. * Suppress sending of mode_sense for mode page 8 if the device pretends to
  148. * support the SCSI Primary Block commands instead of Reduced Block Commands.
  149. *
  150. * - fix capacity
  151. * Tell sd_mod to correct the last sector number reported by read_capacity.
  152. * Avoids access beyond actual disk limits on devices with an off-by-one bug.
  153. * Don't use this with devices which don't have this bug.
  154. *
  155. * - override internal blacklist
  156. * Instead of adding to the built-in blacklist, use only the workarounds
  157. * specified in the module load parameter.
  158. * Useful if a blacklist entry interfered with a non-broken device.
  159. */
  160. static int sbp2_default_workarounds;
  161. module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
  162. MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
  163. ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
  164. ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
  165. ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
  166. ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
  167. ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
  168. ", or a combination)");
  169. /*
  170. * Export information about protocols/devices supported by this driver.
  171. */
  172. static struct ieee1394_device_id sbp2_id_table[] = {
  173. {
  174. .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
  175. .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
  176. .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
  177. {}
  178. };
  179. MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
  180. /*
  181. * Debug levels, configured via kernel config, or enable here.
  182. */
  183. #define CONFIG_IEEE1394_SBP2_DEBUG 0
  184. /* #define CONFIG_IEEE1394_SBP2_DEBUG_ORBS */
  185. /* #define CONFIG_IEEE1394_SBP2_DEBUG_DMA */
  186. /* #define CONFIG_IEEE1394_SBP2_DEBUG 1 */
  187. /* #define CONFIG_IEEE1394_SBP2_DEBUG 2 */
  188. /* #define CONFIG_IEEE1394_SBP2_PACKET_DUMP */
  189. #ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS
  190. #define SBP2_ORB_DEBUG(fmt, args...) HPSB_ERR("sbp2(%s): "fmt, __FUNCTION__, ## args)
  191. static u32 global_outstanding_command_orbs = 0;
  192. #define outstanding_orb_incr global_outstanding_command_orbs++
  193. #define outstanding_orb_decr global_outstanding_command_orbs--
  194. #else
  195. #define SBP2_ORB_DEBUG(fmt, args...) do {} while (0)
  196. #define outstanding_orb_incr do {} while (0)
  197. #define outstanding_orb_decr do {} while (0)
  198. #endif
  199. #ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
  200. #define SBP2_DMA_ALLOC(fmt, args...) \
  201. HPSB_ERR("sbp2(%s)alloc(%d): "fmt, __FUNCTION__, \
  202. ++global_outstanding_dmas, ## args)
  203. #define SBP2_DMA_FREE(fmt, args...) \
  204. HPSB_ERR("sbp2(%s)free(%d): "fmt, __FUNCTION__, \
  205. --global_outstanding_dmas, ## args)
  206. static u32 global_outstanding_dmas = 0;
  207. #else
  208. #define SBP2_DMA_ALLOC(fmt, args...) do {} while (0)
  209. #define SBP2_DMA_FREE(fmt, args...) do {} while (0)
  210. #endif
  211. #if CONFIG_IEEE1394_SBP2_DEBUG >= 2
  212. #define SBP2_DEBUG(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  213. #define SBP2_INFO(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  214. #define SBP2_NOTICE(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  215. #define SBP2_WARN(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  216. #elif CONFIG_IEEE1394_SBP2_DEBUG == 1
  217. #define SBP2_DEBUG(fmt, args...) HPSB_DEBUG("sbp2: "fmt, ## args)
  218. #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
  219. #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
  220. #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
  221. #else
  222. #define SBP2_DEBUG(fmt, args...) do {} while (0)
  223. #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
  224. #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
  225. #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
  226. #endif
  227. #define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  228. #define SBP2_DEBUG_ENTER() SBP2_DEBUG("%s", __FUNCTION__)
  229. /*
  230. * Globals
  231. */
  232. static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
  233. u32 status);
  234. static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
  235. u32 scsi_status, struct scsi_cmnd *SCpnt,
  236. void (*done)(struct scsi_cmnd *));
  237. static struct scsi_host_template scsi_driver_template;
  238. static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
  239. static void sbp2_host_reset(struct hpsb_host *host);
  240. static int sbp2_probe(struct device *dev);
  241. static int sbp2_remove(struct device *dev);
  242. static int sbp2_update(struct unit_directory *ud);
  243. static struct hpsb_highlevel sbp2_highlevel = {
  244. .name = SBP2_DEVICE_NAME,
  245. .host_reset = sbp2_host_reset,
  246. };
  247. static struct hpsb_address_ops sbp2_ops = {
  248. .write = sbp2_handle_status_write
  249. };
  250. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  251. static struct hpsb_address_ops sbp2_physdma_ops = {
  252. .read = sbp2_handle_physdma_read,
  253. .write = sbp2_handle_physdma_write,
  254. };
  255. #endif
  256. static struct hpsb_protocol_driver sbp2_driver = {
  257. .name = "SBP2 Driver",
  258. .id_table = sbp2_id_table,
  259. .update = sbp2_update,
  260. .driver = {
  261. .name = SBP2_DEVICE_NAME,
  262. .bus = &ieee1394_bus_type,
  263. .probe = sbp2_probe,
  264. .remove = sbp2_remove,
  265. },
  266. };
  267. /*
  268. * List of devices with known bugs.
  269. *
  270. * The firmware_revision field, masked with 0xffff00, is the best indicator
  271. * for the type of bridge chip of a device. It yields a few false positives
  272. * but this did not break correctly behaving devices so far.
  273. */
  274. static const struct {
  275. u32 firmware_revision;
  276. u32 model_id;
  277. unsigned workarounds;
  278. } sbp2_workarounds_table[] = {
  279. /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
  280. .firmware_revision = 0x002800,
  281. .model_id = 0x001010,
  282. .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
  283. SBP2_WORKAROUND_MODE_SENSE_8,
  284. },
  285. /* Initio bridges, actually only needed for some older ones */ {
  286. .firmware_revision = 0x000200,
  287. .workarounds = SBP2_WORKAROUND_INQUIRY_36,
  288. },
  289. /* Symbios bridge */ {
  290. .firmware_revision = 0xa0b800,
  291. .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
  292. },
  293. /*
  294. * Note about the following Apple iPod blacklist entries:
  295. *
  296. * There are iPods (2nd gen, 3rd gen) with model_id==0. Since our
  297. * matching logic treats 0 as a wildcard, we cannot match this ID
  298. * without rewriting the matching routine. Fortunately these iPods
  299. * do not feature the read_capacity bug according to one report.
  300. * Read_capacity behaviour as well as model_id could change due to
  301. * Apple-supplied firmware updates though.
  302. */
  303. /* iPod 4th generation */ {
  304. .firmware_revision = 0x0a2700,
  305. .model_id = 0x000021,
  306. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  307. },
  308. /* iPod mini */ {
  309. .firmware_revision = 0x0a2700,
  310. .model_id = 0x000023,
  311. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  312. },
  313. /* iPod Photo */ {
  314. .firmware_revision = 0x0a2700,
  315. .model_id = 0x00007e,
  316. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  317. }
  318. };
  319. /**************************************
  320. * General utility functions
  321. **************************************/
  322. #ifndef __BIG_ENDIAN
  323. /*
  324. * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
  325. */
  326. static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
  327. {
  328. u32 *temp = buffer;
  329. for (length = (length >> 2); length--; )
  330. temp[length] = be32_to_cpu(temp[length]);
  331. return;
  332. }
  333. /*
  334. * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
  335. */
  336. static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
  337. {
  338. u32 *temp = buffer;
  339. for (length = (length >> 2); length--; )
  340. temp[length] = cpu_to_be32(temp[length]);
  341. return;
  342. }
  343. #else /* BIG_ENDIAN */
  344. /* Why waste the cpu cycles? */
  345. #define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
  346. #define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
  347. #endif
  348. #ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP
  349. /*
  350. * Debug packet dump routine. Length is in bytes.
  351. */
  352. static void sbp2util_packet_dump(void *buffer, int length, char *dump_name,
  353. u32 dump_phys_addr)
  354. {
  355. int i;
  356. unsigned char *dump = buffer;
  357. if (!dump || !length || !dump_name)
  358. return;
  359. if (dump_phys_addr)
  360. printk("[%s, 0x%x]", dump_name, dump_phys_addr);
  361. else
  362. printk("[%s]", dump_name);
  363. for (i = 0; i < length; i++) {
  364. if (i > 0x3f) {
  365. printk("\n ...");
  366. break;
  367. }
  368. if ((i & 0x3) == 0)
  369. printk(" ");
  370. if ((i & 0xf) == 0)
  371. printk("\n ");
  372. printk("%02x ", (int)dump[i]);
  373. }
  374. printk("\n");
  375. return;
  376. }
  377. #else
  378. #define sbp2util_packet_dump(w,x,y,z) do {} while (0)
  379. #endif
  380. static DECLARE_WAIT_QUEUE_HEAD(access_wq);
  381. /*
  382. * Waits for completion of an SBP-2 access request.
  383. * Returns nonzero if timed out or prematurely interrupted.
  384. */
  385. static int sbp2util_access_timeout(struct scsi_id_instance_data *scsi_id,
  386. int timeout)
  387. {
  388. long leftover = wait_event_interruptible_timeout(
  389. access_wq, scsi_id->access_complete, timeout);
  390. scsi_id->access_complete = 0;
  391. return leftover <= 0;
  392. }
  393. /* Frees an allocated packet */
  394. static void sbp2_free_packet(struct hpsb_packet *packet)
  395. {
  396. hpsb_free_tlabel(packet);
  397. hpsb_free_packet(packet);
  398. }
  399. /* This is much like hpsb_node_write(), except it ignores the response
  400. * subaction and returns immediately. Can be used from interrupts.
  401. */
  402. static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
  403. quadlet_t *buffer, size_t length)
  404. {
  405. struct hpsb_packet *packet;
  406. packet = hpsb_make_writepacket(ne->host, ne->nodeid,
  407. addr, buffer, length);
  408. if (!packet)
  409. return -ENOMEM;
  410. hpsb_set_packet_complete_task(packet,
  411. (void (*)(void *))sbp2_free_packet,
  412. packet);
  413. hpsb_node_fill_packet(ne, packet);
  414. if (hpsb_send_packet(packet) < 0) {
  415. sbp2_free_packet(packet);
  416. return -EIO;
  417. }
  418. return 0;
  419. }
  420. static void sbp2util_notify_fetch_agent(struct scsi_id_instance_data *scsi_id,
  421. u64 offset, quadlet_t *data, size_t len)
  422. {
  423. /*
  424. * There is a small window after a bus reset within which the node
  425. * entry's generation is current but the reconnect wasn't completed.
  426. */
  427. if (unlikely(atomic_read(&scsi_id->state) == SBP2LU_STATE_IN_RESET))
  428. return;
  429. if (hpsb_node_write(scsi_id->ne,
  430. scsi_id->sbp2_command_block_agent_addr + offset,
  431. data, len))
  432. SBP2_ERR("sbp2util_notify_fetch_agent failed.");
  433. /*
  434. * Now accept new SCSI commands, unless a bus reset happended during
  435. * hpsb_node_write.
  436. */
  437. if (likely(atomic_read(&scsi_id->state) != SBP2LU_STATE_IN_RESET))
  438. scsi_unblock_requests(scsi_id->scsi_host);
  439. }
  440. static void sbp2util_write_orb_pointer(void *p)
  441. {
  442. quadlet_t data[2];
  443. data[0] = ORB_SET_NODE_ID(
  444. ((struct scsi_id_instance_data *)p)->hi->host->node_id);
  445. data[1] = ((struct scsi_id_instance_data *)p)->last_orb_dma;
  446. sbp2util_cpu_to_be32_buffer(data, 8);
  447. sbp2util_notify_fetch_agent(p, SBP2_ORB_POINTER_OFFSET, data, 8);
  448. }
  449. static void sbp2util_write_doorbell(void *p)
  450. {
  451. sbp2util_notify_fetch_agent(p, SBP2_DOORBELL_OFFSET, NULL, 4);
  452. }
  453. /*
  454. * This function is called to create a pool of command orbs used for
  455. * command processing. It is called when a new sbp2 device is detected.
  456. */
  457. static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
  458. {
  459. struct sbp2scsi_host_info *hi = scsi_id->hi;
  460. int i;
  461. unsigned long flags, orbs;
  462. struct sbp2_command_info *command;
  463. orbs = serialize_io ? 2 : SBP2_MAX_CMDS;
  464. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  465. for (i = 0; i < orbs; i++) {
  466. command = kzalloc(sizeof(*command), GFP_ATOMIC);
  467. if (!command) {
  468. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock,
  469. flags);
  470. return -ENOMEM;
  471. }
  472. command->command_orb_dma =
  473. pci_map_single(hi->host->pdev, &command->command_orb,
  474. sizeof(struct sbp2_command_orb),
  475. PCI_DMA_TODEVICE);
  476. SBP2_DMA_ALLOC("single command orb DMA");
  477. command->sge_dma =
  478. pci_map_single(hi->host->pdev,
  479. &command->scatter_gather_element,
  480. sizeof(command->scatter_gather_element),
  481. PCI_DMA_BIDIRECTIONAL);
  482. SBP2_DMA_ALLOC("scatter_gather_element");
  483. INIT_LIST_HEAD(&command->list);
  484. list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
  485. }
  486. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  487. return 0;
  488. }
  489. /*
  490. * This function is called to delete a pool of command orbs.
  491. */
  492. static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
  493. {
  494. struct hpsb_host *host = scsi_id->hi->host;
  495. struct list_head *lh, *next;
  496. struct sbp2_command_info *command;
  497. unsigned long flags;
  498. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  499. if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
  500. list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) {
  501. command = list_entry(lh, struct sbp2_command_info, list);
  502. /* Release our generic DMA's */
  503. pci_unmap_single(host->pdev, command->command_orb_dma,
  504. sizeof(struct sbp2_command_orb),
  505. PCI_DMA_TODEVICE);
  506. SBP2_DMA_FREE("single command orb DMA");
  507. pci_unmap_single(host->pdev, command->sge_dma,
  508. sizeof(command->scatter_gather_element),
  509. PCI_DMA_BIDIRECTIONAL);
  510. SBP2_DMA_FREE("scatter_gather_element");
  511. kfree(command);
  512. }
  513. }
  514. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  515. return;
  516. }
  517. /*
  518. * This function finds the sbp2_command for a given outstanding command
  519. * orb.Only looks at the inuse list.
  520. */
  521. static struct sbp2_command_info *sbp2util_find_command_for_orb(
  522. struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
  523. {
  524. struct sbp2_command_info *command;
  525. unsigned long flags;
  526. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  527. if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
  528. list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) {
  529. if (command->command_orb_dma == orb) {
  530. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  531. return command;
  532. }
  533. }
  534. }
  535. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  536. SBP2_ORB_DEBUG("could not match command orb %x", (unsigned int)orb);
  537. return NULL;
  538. }
  539. /*
  540. * This function finds the sbp2_command for a given outstanding SCpnt.
  541. * Only looks at the inuse list.
  542. * Must be called with scsi_id->sbp2_command_orb_lock held.
  543. */
  544. static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
  545. struct scsi_id_instance_data *scsi_id, void *SCpnt)
  546. {
  547. struct sbp2_command_info *command;
  548. if (!list_empty(&scsi_id->sbp2_command_orb_inuse))
  549. list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list)
  550. if (command->Current_SCpnt == SCpnt)
  551. return command;
  552. return NULL;
  553. }
  554. /*
  555. * This function allocates a command orb used to send a scsi command.
  556. */
  557. static struct sbp2_command_info *sbp2util_allocate_command_orb(
  558. struct scsi_id_instance_data *scsi_id,
  559. struct scsi_cmnd *Current_SCpnt,
  560. void (*Current_done)(struct scsi_cmnd *))
  561. {
  562. struct list_head *lh;
  563. struct sbp2_command_info *command = NULL;
  564. unsigned long flags;
  565. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  566. if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
  567. lh = scsi_id->sbp2_command_orb_completed.next;
  568. list_del(lh);
  569. command = list_entry(lh, struct sbp2_command_info, list);
  570. command->Current_done = Current_done;
  571. command->Current_SCpnt = Current_SCpnt;
  572. list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
  573. } else {
  574. SBP2_ERR("%s: no orbs available", __FUNCTION__);
  575. }
  576. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  577. return command;
  578. }
  579. /* Free our DMA's */
  580. static void sbp2util_free_command_dma(struct sbp2_command_info *command)
  581. {
  582. struct scsi_id_instance_data *scsi_id =
  583. (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
  584. struct hpsb_host *host;
  585. if (!scsi_id) {
  586. SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
  587. return;
  588. }
  589. host = scsi_id->ud->ne->host;
  590. if (command->cmd_dma) {
  591. if (command->dma_type == CMD_DMA_SINGLE) {
  592. pci_unmap_single(host->pdev, command->cmd_dma,
  593. command->dma_size, command->dma_dir);
  594. SBP2_DMA_FREE("single bulk");
  595. } else if (command->dma_type == CMD_DMA_PAGE) {
  596. pci_unmap_page(host->pdev, command->cmd_dma,
  597. command->dma_size, command->dma_dir);
  598. SBP2_DMA_FREE("single page");
  599. } /* XXX: Check for CMD_DMA_NONE bug */
  600. command->dma_type = CMD_DMA_NONE;
  601. command->cmd_dma = 0;
  602. }
  603. if (command->sge_buffer) {
  604. pci_unmap_sg(host->pdev, command->sge_buffer,
  605. command->dma_size, command->dma_dir);
  606. SBP2_DMA_FREE("scatter list");
  607. command->sge_buffer = NULL;
  608. }
  609. }
  610. /*
  611. * This function moves a command to the completed orb list.
  612. * Must be called with scsi_id->sbp2_command_orb_lock held.
  613. */
  614. static void sbp2util_mark_command_completed(
  615. struct scsi_id_instance_data *scsi_id,
  616. struct sbp2_command_info *command)
  617. {
  618. list_del(&command->list);
  619. sbp2util_free_command_dma(command);
  620. list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
  621. }
  622. /*
  623. * Is scsi_id valid? Is the 1394 node still present?
  624. */
  625. static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id)
  626. {
  627. return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo;
  628. }
  629. /*********************************************
  630. * IEEE-1394 core driver stack related section
  631. *********************************************/
  632. static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud);
  633. static int sbp2_probe(struct device *dev)
  634. {
  635. struct unit_directory *ud;
  636. struct scsi_id_instance_data *scsi_id;
  637. SBP2_DEBUG_ENTER();
  638. ud = container_of(dev, struct unit_directory, device);
  639. /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
  640. * instead. */
  641. if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
  642. return -ENODEV;
  643. scsi_id = sbp2_alloc_device(ud);
  644. if (!scsi_id)
  645. return -ENOMEM;
  646. sbp2_parse_unit_directory(scsi_id, ud);
  647. return sbp2_start_device(scsi_id);
  648. }
  649. static int sbp2_remove(struct device *dev)
  650. {
  651. struct unit_directory *ud;
  652. struct scsi_id_instance_data *scsi_id;
  653. struct scsi_device *sdev;
  654. SBP2_DEBUG_ENTER();
  655. ud = container_of(dev, struct unit_directory, device);
  656. scsi_id = ud->device.driver_data;
  657. if (!scsi_id)
  658. return 0;
  659. if (scsi_id->scsi_host) {
  660. /* Get rid of enqueued commands if there is no chance to
  661. * send them. */
  662. if (!sbp2util_node_is_available(scsi_id))
  663. sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
  664. /* scsi_remove_device() will trigger shutdown functions of SCSI
  665. * highlevel drivers which would deadlock if blocked. */
  666. atomic_set(&scsi_id->state, SBP2LU_STATE_IN_SHUTDOWN);
  667. scsi_unblock_requests(scsi_id->scsi_host);
  668. }
  669. sdev = scsi_id->sdev;
  670. if (sdev) {
  671. scsi_id->sdev = NULL;
  672. scsi_remove_device(sdev);
  673. }
  674. sbp2_logout_device(scsi_id);
  675. sbp2_remove_device(scsi_id);
  676. return 0;
  677. }
  678. static int sbp2_update(struct unit_directory *ud)
  679. {
  680. struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
  681. SBP2_DEBUG_ENTER();
  682. if (sbp2_reconnect_device(scsi_id)) {
  683. /*
  684. * Ok, reconnect has failed. Perhaps we didn't
  685. * reconnect fast enough. Try doing a regular login, but
  686. * first do a logout just in case of any weirdness.
  687. */
  688. sbp2_logout_device(scsi_id);
  689. if (sbp2_login_device(scsi_id)) {
  690. /* Login failed too, just fail, and the backend
  691. * will call our sbp2_remove for us */
  692. SBP2_ERR("Failed to reconnect to sbp2 device!");
  693. return -EBUSY;
  694. }
  695. }
  696. /* Set max retries to something large on the device. */
  697. sbp2_set_busy_timeout(scsi_id);
  698. /* Do a SBP-2 fetch agent reset. */
  699. sbp2_agent_reset(scsi_id, 1);
  700. /* Get the max speed and packet size that we can use. */
  701. sbp2_max_speed_and_size(scsi_id);
  702. /* Complete any pending commands with busy (so they get
  703. * retried) and remove them from our queue
  704. */
  705. sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
  706. /* Accept new commands unless there was another bus reset in the
  707. * meantime. */
  708. if (hpsb_node_entry_valid(scsi_id->ne)) {
  709. atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
  710. scsi_unblock_requests(scsi_id->scsi_host);
  711. }
  712. return 0;
  713. }
  714. /* This functions is called by the sbp2_probe, for each new device. We now
  715. * allocate one scsi host for each scsi_id (unit directory). */
  716. static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
  717. {
  718. struct sbp2scsi_host_info *hi;
  719. struct Scsi_Host *scsi_host = NULL;
  720. struct scsi_id_instance_data *scsi_id = NULL;
  721. SBP2_DEBUG_ENTER();
  722. scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL);
  723. if (!scsi_id) {
  724. SBP2_ERR("failed to create scsi_id");
  725. goto failed_alloc;
  726. }
  727. scsi_id->ne = ud->ne;
  728. scsi_id->ud = ud;
  729. scsi_id->speed_code = IEEE1394_SPEED_100;
  730. scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
  731. scsi_id->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
  732. INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
  733. INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
  734. INIT_LIST_HEAD(&scsi_id->scsi_list);
  735. spin_lock_init(&scsi_id->sbp2_command_orb_lock);
  736. atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
  737. INIT_WORK(&scsi_id->protocol_work, NULL, NULL);
  738. ud->device.driver_data = scsi_id;
  739. hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
  740. if (!hi) {
  741. hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi));
  742. if (!hi) {
  743. SBP2_ERR("failed to allocate hostinfo");
  744. goto failed_alloc;
  745. }
  746. SBP2_DEBUG("sbp2_alloc_device: allocated hostinfo");
  747. hi->host = ud->ne->host;
  748. INIT_LIST_HEAD(&hi->scsi_ids);
  749. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  750. /* Handle data movement if physical dma is not
  751. * enabled or not supported on host controller */
  752. if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
  753. &sbp2_physdma_ops,
  754. 0x0ULL, 0xfffffffcULL)) {
  755. SBP2_ERR("failed to register lower 4GB address range");
  756. goto failed_alloc;
  757. }
  758. #endif
  759. }
  760. /* Prevent unloading of the 1394 host */
  761. if (!try_module_get(hi->host->driver->owner)) {
  762. SBP2_ERR("failed to get a reference on 1394 host driver");
  763. goto failed_alloc;
  764. }
  765. scsi_id->hi = hi;
  766. list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
  767. /* Register the status FIFO address range. We could use the same FIFO
  768. * for targets at different nodes. However we need different FIFOs per
  769. * target in order to support multi-unit devices.
  770. * The FIFO is located out of the local host controller's physical range
  771. * but, if possible, within the posted write area. Status writes will
  772. * then be performed as unified transactions. This slightly reduces
  773. * bandwidth usage, and some Prolific based devices seem to require it.
  774. */
  775. scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace(
  776. &sbp2_highlevel, ud->ne->host, &sbp2_ops,
  777. sizeof(struct sbp2_status_block), sizeof(quadlet_t),
  778. ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
  779. if (scsi_id->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
  780. SBP2_ERR("failed to allocate status FIFO address range");
  781. goto failed_alloc;
  782. }
  783. /* Register our host with the SCSI stack. */
  784. scsi_host = scsi_host_alloc(&scsi_driver_template,
  785. sizeof(unsigned long));
  786. if (!scsi_host) {
  787. SBP2_ERR("failed to register scsi host");
  788. goto failed_alloc;
  789. }
  790. scsi_host->hostdata[0] = (unsigned long)scsi_id;
  791. if (!scsi_add_host(scsi_host, &ud->device)) {
  792. scsi_id->scsi_host = scsi_host;
  793. return scsi_id;
  794. }
  795. SBP2_ERR("failed to add scsi host");
  796. scsi_host_put(scsi_host);
  797. failed_alloc:
  798. sbp2_remove_device(scsi_id);
  799. return NULL;
  800. }
  801. static void sbp2_host_reset(struct hpsb_host *host)
  802. {
  803. struct sbp2scsi_host_info *hi;
  804. struct scsi_id_instance_data *scsi_id;
  805. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  806. if (!hi)
  807. return;
  808. list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
  809. if (likely(atomic_read(&scsi_id->state) !=
  810. SBP2LU_STATE_IN_SHUTDOWN)) {
  811. atomic_set(&scsi_id->state, SBP2LU_STATE_IN_RESET);
  812. scsi_block_requests(scsi_id->scsi_host);
  813. }
  814. }
  815. /*
  816. * This function is where we first pull the node unique ids, and then
  817. * allocate memory and register a SBP-2 device.
  818. */
  819. static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
  820. {
  821. struct sbp2scsi_host_info *hi = scsi_id->hi;
  822. int error;
  823. SBP2_DEBUG_ENTER();
  824. /* Login FIFO DMA */
  825. scsi_id->login_response =
  826. pci_alloc_consistent(hi->host->pdev,
  827. sizeof(struct sbp2_login_response),
  828. &scsi_id->login_response_dma);
  829. if (!scsi_id->login_response)
  830. goto alloc_fail;
  831. SBP2_DMA_ALLOC("consistent DMA region for login FIFO");
  832. /* Query logins ORB DMA */
  833. scsi_id->query_logins_orb =
  834. pci_alloc_consistent(hi->host->pdev,
  835. sizeof(struct sbp2_query_logins_orb),
  836. &scsi_id->query_logins_orb_dma);
  837. if (!scsi_id->query_logins_orb)
  838. goto alloc_fail;
  839. SBP2_DMA_ALLOC("consistent DMA region for query logins ORB");
  840. /* Query logins response DMA */
  841. scsi_id->query_logins_response =
  842. pci_alloc_consistent(hi->host->pdev,
  843. sizeof(struct sbp2_query_logins_response),
  844. &scsi_id->query_logins_response_dma);
  845. if (!scsi_id->query_logins_response)
  846. goto alloc_fail;
  847. SBP2_DMA_ALLOC("consistent DMA region for query logins response");
  848. /* Reconnect ORB DMA */
  849. scsi_id->reconnect_orb =
  850. pci_alloc_consistent(hi->host->pdev,
  851. sizeof(struct sbp2_reconnect_orb),
  852. &scsi_id->reconnect_orb_dma);
  853. if (!scsi_id->reconnect_orb)
  854. goto alloc_fail;
  855. SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB");
  856. /* Logout ORB DMA */
  857. scsi_id->logout_orb =
  858. pci_alloc_consistent(hi->host->pdev,
  859. sizeof(struct sbp2_logout_orb),
  860. &scsi_id->logout_orb_dma);
  861. if (!scsi_id->logout_orb)
  862. goto alloc_fail;
  863. SBP2_DMA_ALLOC("consistent DMA region for logout ORB");
  864. /* Login ORB DMA */
  865. scsi_id->login_orb =
  866. pci_alloc_consistent(hi->host->pdev,
  867. sizeof(struct sbp2_login_orb),
  868. &scsi_id->login_orb_dma);
  869. if (!scsi_id->login_orb)
  870. goto alloc_fail;
  871. SBP2_DMA_ALLOC("consistent DMA region for login ORB");
  872. SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", scsi_id->ud->id);
  873. /*
  874. * Create our command orb pool
  875. */
  876. if (sbp2util_create_command_orb_pool(scsi_id)) {
  877. SBP2_ERR("sbp2util_create_command_orb_pool failed!");
  878. sbp2_remove_device(scsi_id);
  879. return -ENOMEM;
  880. }
  881. /* Schedule a timeout here. The reason is that we may be so close
  882. * to a bus reset, that the device is not available for logins.
  883. * This can happen when the bus reset is caused by the host
  884. * connected to the sbp2 device being removed. That host would
  885. * have a certain amount of time to relogin before the sbp2 device
  886. * allows someone else to login instead. One second makes sense. */
  887. if (msleep_interruptible(1000)) {
  888. sbp2_remove_device(scsi_id);
  889. return -EINTR;
  890. }
  891. /*
  892. * Login to the sbp-2 device
  893. */
  894. if (sbp2_login_device(scsi_id)) {
  895. /* Login failed, just remove the device. */
  896. sbp2_remove_device(scsi_id);
  897. return -EBUSY;
  898. }
  899. /*
  900. * Set max retries to something large on the device
  901. */
  902. sbp2_set_busy_timeout(scsi_id);
  903. /*
  904. * Do a SBP-2 fetch agent reset
  905. */
  906. sbp2_agent_reset(scsi_id, 1);
  907. /*
  908. * Get the max speed and packet size that we can use
  909. */
  910. sbp2_max_speed_and_size(scsi_id);
  911. /* Add this device to the scsi layer now */
  912. error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
  913. if (error) {
  914. SBP2_ERR("scsi_add_device failed");
  915. sbp2_logout_device(scsi_id);
  916. sbp2_remove_device(scsi_id);
  917. return error;
  918. }
  919. return 0;
  920. alloc_fail:
  921. SBP2_ERR("Could not allocate memory for scsi_id");
  922. sbp2_remove_device(scsi_id);
  923. return -ENOMEM;
  924. }
  925. /*
  926. * This function removes an sbp2 device from the sbp2scsi_host_info struct.
  927. */
  928. static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
  929. {
  930. struct sbp2scsi_host_info *hi;
  931. SBP2_DEBUG_ENTER();
  932. if (!scsi_id)
  933. return;
  934. hi = scsi_id->hi;
  935. /* This will remove our scsi device aswell */
  936. if (scsi_id->scsi_host) {
  937. scsi_remove_host(scsi_id->scsi_host);
  938. scsi_host_put(scsi_id->scsi_host);
  939. }
  940. flush_scheduled_work();
  941. sbp2util_remove_command_orb_pool(scsi_id);
  942. list_del(&scsi_id->scsi_list);
  943. if (scsi_id->login_response) {
  944. pci_free_consistent(hi->host->pdev,
  945. sizeof(struct sbp2_login_response),
  946. scsi_id->login_response,
  947. scsi_id->login_response_dma);
  948. SBP2_DMA_FREE("single login FIFO");
  949. }
  950. if (scsi_id->login_orb) {
  951. pci_free_consistent(hi->host->pdev,
  952. sizeof(struct sbp2_login_orb),
  953. scsi_id->login_orb,
  954. scsi_id->login_orb_dma);
  955. SBP2_DMA_FREE("single login ORB");
  956. }
  957. if (scsi_id->reconnect_orb) {
  958. pci_free_consistent(hi->host->pdev,
  959. sizeof(struct sbp2_reconnect_orb),
  960. scsi_id->reconnect_orb,
  961. scsi_id->reconnect_orb_dma);
  962. SBP2_DMA_FREE("single reconnect orb");
  963. }
  964. if (scsi_id->logout_orb) {
  965. pci_free_consistent(hi->host->pdev,
  966. sizeof(struct sbp2_logout_orb),
  967. scsi_id->logout_orb,
  968. scsi_id->logout_orb_dma);
  969. SBP2_DMA_FREE("single logout orb");
  970. }
  971. if (scsi_id->query_logins_orb) {
  972. pci_free_consistent(hi->host->pdev,
  973. sizeof(struct sbp2_query_logins_orb),
  974. scsi_id->query_logins_orb,
  975. scsi_id->query_logins_orb_dma);
  976. SBP2_DMA_FREE("single query logins orb");
  977. }
  978. if (scsi_id->query_logins_response) {
  979. pci_free_consistent(hi->host->pdev,
  980. sizeof(struct sbp2_query_logins_response),
  981. scsi_id->query_logins_response,
  982. scsi_id->query_logins_response_dma);
  983. SBP2_DMA_FREE("single query logins data");
  984. }
  985. if (scsi_id->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
  986. hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
  987. scsi_id->status_fifo_addr);
  988. scsi_id->ud->device.driver_data = NULL;
  989. if (hi)
  990. module_put(hi->host->driver->owner);
  991. SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->ud->id);
  992. kfree(scsi_id);
  993. }
  994. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  995. /*
  996. * This function deals with physical dma write requests (for adapters that do not support
  997. * physical dma in hardware). Mostly just here for debugging...
  998. */
  999. static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
  1000. int destid, quadlet_t *data, u64 addr,
  1001. size_t length, u16 flags)
  1002. {
  1003. /*
  1004. * Manually put the data in the right place.
  1005. */
  1006. memcpy(bus_to_virt((u32) addr), data, length);
  1007. sbp2util_packet_dump(data, length, "sbp2 phys dma write by device",
  1008. (u32) addr);
  1009. return RCODE_COMPLETE;
  1010. }
  1011. /*
  1012. * This function deals with physical dma read requests (for adapters that do not support
  1013. * physical dma in hardware). Mostly just here for debugging...
  1014. */
  1015. static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
  1016. quadlet_t *data, u64 addr, size_t length,
  1017. u16 flags)
  1018. {
  1019. /*
  1020. * Grab data from memory and send a read response.
  1021. */
  1022. memcpy(data, bus_to_virt((u32) addr), length);
  1023. sbp2util_packet_dump(data, length, "sbp2 phys dma read by device",
  1024. (u32) addr);
  1025. return RCODE_COMPLETE;
  1026. }
  1027. #endif
  1028. /**************************************
  1029. * SBP-2 protocol related section
  1030. **************************************/
  1031. /*
  1032. * This function queries the device for the maximum concurrent logins it
  1033. * supports.
  1034. */
  1035. static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
  1036. {
  1037. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1038. quadlet_t data[2];
  1039. int max_logins;
  1040. int active_logins;
  1041. SBP2_DEBUG_ENTER();
  1042. scsi_id->query_logins_orb->reserved1 = 0x0;
  1043. scsi_id->query_logins_orb->reserved2 = 0x0;
  1044. scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
  1045. scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1046. scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
  1047. scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
  1048. scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
  1049. scsi_id->query_logins_orb->reserved_resp_length =
  1050. ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
  1051. scsi_id->query_logins_orb->status_fifo_hi =
  1052. ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
  1053. scsi_id->query_logins_orb->status_fifo_lo =
  1054. ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
  1055. sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
  1056. sbp2util_packet_dump(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb),
  1057. "sbp2 query logins orb", scsi_id->query_logins_orb_dma);
  1058. memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
  1059. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1060. data[1] = scsi_id->query_logins_orb_dma;
  1061. sbp2util_cpu_to_be32_buffer(data, 8);
  1062. hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
  1063. if (sbp2util_access_timeout(scsi_id, 2*HZ)) {
  1064. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  1065. return -EIO;
  1066. }
  1067. if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
  1068. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  1069. return -EIO;
  1070. }
  1071. if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
  1072. SBP2_INFO("Error querying logins to SBP-2 device - failed");
  1073. return -EIO;
  1074. }
  1075. sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
  1076. SBP2_DEBUG("length_max_logins = %x",
  1077. (unsigned int)scsi_id->query_logins_response->length_max_logins);
  1078. max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
  1079. SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
  1080. active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
  1081. SBP2_INFO("Number of active logins: %d", active_logins);
  1082. if (active_logins >= max_logins) {
  1083. return -EIO;
  1084. }
  1085. return 0;
  1086. }
  1087. /*
  1088. * This function is called in order to login to a particular SBP-2 device,
  1089. * after a bus reset.
  1090. */
  1091. static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
  1092. {
  1093. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1094. quadlet_t data[2];
  1095. SBP2_DEBUG_ENTER();
  1096. if (!scsi_id->login_orb) {
  1097. SBP2_DEBUG("%s: login_orb not alloc'd!", __FUNCTION__);
  1098. return -EIO;
  1099. }
  1100. if (!exclusive_login) {
  1101. if (sbp2_query_logins(scsi_id)) {
  1102. SBP2_INFO("Device does not support any more concurrent logins");
  1103. return -EIO;
  1104. }
  1105. }
  1106. /* Set-up login ORB, assume no password */
  1107. scsi_id->login_orb->password_hi = 0;
  1108. scsi_id->login_orb->password_lo = 0;
  1109. scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
  1110. scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1111. scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
  1112. scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0); /* One second reconnect time */
  1113. scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login); /* Exclusive access to device */
  1114. scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1); /* Notify us of login complete */
  1115. scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
  1116. scsi_id->login_orb->passwd_resp_lengths =
  1117. ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
  1118. scsi_id->login_orb->status_fifo_hi =
  1119. ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
  1120. scsi_id->login_orb->status_fifo_lo =
  1121. ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
  1122. sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
  1123. sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb),
  1124. "sbp2 login orb", scsi_id->login_orb_dma);
  1125. memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
  1126. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1127. data[1] = scsi_id->login_orb_dma;
  1128. sbp2util_cpu_to_be32_buffer(data, 8);
  1129. hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
  1130. /*
  1131. * Wait for login status (up to 20 seconds)...
  1132. */
  1133. if (sbp2util_access_timeout(scsi_id, 20*HZ)) {
  1134. SBP2_ERR("Error logging into SBP-2 device - timed out");
  1135. return -EIO;
  1136. }
  1137. /*
  1138. * Sanity. Make sure status returned matches login orb.
  1139. */
  1140. if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
  1141. SBP2_ERR("Error logging into SBP-2 device - timed out");
  1142. return -EIO;
  1143. }
  1144. if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
  1145. SBP2_ERR("Error logging into SBP-2 device - failed");
  1146. return -EIO;
  1147. }
  1148. /*
  1149. * Byte swap the login response, for use when reconnecting or
  1150. * logging out.
  1151. */
  1152. sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
  1153. /*
  1154. * Grab our command block agent address from the login response.
  1155. */
  1156. SBP2_DEBUG("command_block_agent_hi = %x",
  1157. (unsigned int)scsi_id->login_response->command_block_agent_hi);
  1158. SBP2_DEBUG("command_block_agent_lo = %x",
  1159. (unsigned int)scsi_id->login_response->command_block_agent_lo);
  1160. scsi_id->sbp2_command_block_agent_addr =
  1161. ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
  1162. scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
  1163. scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
  1164. SBP2_INFO("Logged into SBP-2 device");
  1165. return 0;
  1166. }
  1167. /*
  1168. * This function is called in order to logout from a particular SBP-2
  1169. * device, usually called during driver unload.
  1170. */
  1171. static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
  1172. {
  1173. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1174. quadlet_t data[2];
  1175. int error;
  1176. SBP2_DEBUG_ENTER();
  1177. /*
  1178. * Set-up logout ORB
  1179. */
  1180. scsi_id->logout_orb->reserved1 = 0x0;
  1181. scsi_id->logout_orb->reserved2 = 0x0;
  1182. scsi_id->logout_orb->reserved3 = 0x0;
  1183. scsi_id->logout_orb->reserved4 = 0x0;
  1184. scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
  1185. scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
  1186. /* Notify us when complete */
  1187. scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1188. scsi_id->logout_orb->reserved5 = 0x0;
  1189. scsi_id->logout_orb->status_fifo_hi =
  1190. ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
  1191. scsi_id->logout_orb->status_fifo_lo =
  1192. ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
  1193. /*
  1194. * Byte swap ORB if necessary
  1195. */
  1196. sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
  1197. sbp2util_packet_dump(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb),
  1198. "sbp2 logout orb", scsi_id->logout_orb_dma);
  1199. /*
  1200. * Ok, let's write to the target's management agent register
  1201. */
  1202. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1203. data[1] = scsi_id->logout_orb_dma;
  1204. sbp2util_cpu_to_be32_buffer(data, 8);
  1205. error = hpsb_node_write(scsi_id->ne,
  1206. scsi_id->sbp2_management_agent_addr, data, 8);
  1207. if (error)
  1208. return error;
  1209. /* Wait for device to logout...1 second. */
  1210. if (sbp2util_access_timeout(scsi_id, HZ))
  1211. return -EIO;
  1212. SBP2_INFO("Logged out of SBP-2 device");
  1213. return 0;
  1214. }
  1215. /*
  1216. * This function is called in order to reconnect to a particular SBP-2
  1217. * device, after a bus reset.
  1218. */
  1219. static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
  1220. {
  1221. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1222. quadlet_t data[2];
  1223. int error;
  1224. SBP2_DEBUG_ENTER();
  1225. /*
  1226. * Set-up reconnect ORB
  1227. */
  1228. scsi_id->reconnect_orb->reserved1 = 0x0;
  1229. scsi_id->reconnect_orb->reserved2 = 0x0;
  1230. scsi_id->reconnect_orb->reserved3 = 0x0;
  1231. scsi_id->reconnect_orb->reserved4 = 0x0;
  1232. scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
  1233. scsi_id->reconnect_orb->login_ID_misc |=
  1234. ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
  1235. /* Notify us when complete */
  1236. scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1237. scsi_id->reconnect_orb->reserved5 = 0x0;
  1238. scsi_id->reconnect_orb->status_fifo_hi =
  1239. ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
  1240. scsi_id->reconnect_orb->status_fifo_lo =
  1241. ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
  1242. /*
  1243. * Byte swap ORB if necessary
  1244. */
  1245. sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
  1246. sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb),
  1247. "sbp2 reconnect orb", scsi_id->reconnect_orb_dma);
  1248. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1249. data[1] = scsi_id->reconnect_orb_dma;
  1250. sbp2util_cpu_to_be32_buffer(data, 8);
  1251. error = hpsb_node_write(scsi_id->ne,
  1252. scsi_id->sbp2_management_agent_addr, data, 8);
  1253. if (error)
  1254. return error;
  1255. /*
  1256. * Wait for reconnect status (up to 1 second)...
  1257. */
  1258. if (sbp2util_access_timeout(scsi_id, HZ)) {
  1259. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1260. return -EIO;
  1261. }
  1262. /*
  1263. * Sanity. Make sure status returned matches reconnect orb.
  1264. */
  1265. if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
  1266. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1267. return -EIO;
  1268. }
  1269. if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
  1270. SBP2_ERR("Error reconnecting to SBP-2 device - failed");
  1271. return -EIO;
  1272. }
  1273. HPSB_DEBUG("Reconnected to SBP-2 device");
  1274. return 0;
  1275. }
  1276. /*
  1277. * This function is called in order to set the busy timeout (number of
  1278. * retries to attempt) on the sbp2 device.
  1279. */
  1280. static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
  1281. {
  1282. quadlet_t data;
  1283. SBP2_DEBUG_ENTER();
  1284. data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
  1285. if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
  1286. SBP2_ERR("%s error", __FUNCTION__);
  1287. return 0;
  1288. }
  1289. /*
  1290. * This function is called to parse sbp2 device's config rom unit
  1291. * directory. Used to determine things like sbp2 management agent offset,
  1292. * and command set used (SCSI or RBC).
  1293. */
  1294. static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
  1295. struct unit_directory *ud)
  1296. {
  1297. struct csr1212_keyval *kv;
  1298. struct csr1212_dentry *dentry;
  1299. u64 management_agent_addr;
  1300. u32 command_set_spec_id, command_set, unit_characteristics,
  1301. firmware_revision;
  1302. unsigned workarounds;
  1303. int i;
  1304. SBP2_DEBUG_ENTER();
  1305. management_agent_addr = 0x0;
  1306. command_set_spec_id = 0x0;
  1307. command_set = 0x0;
  1308. unit_characteristics = 0x0;
  1309. firmware_revision = 0x0;
  1310. /* Handle different fields in the unit directory, based on keys */
  1311. csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
  1312. switch (kv->key.id) {
  1313. case CSR1212_KV_ID_DEPENDENT_INFO:
  1314. if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET) {
  1315. /* Save off the management agent address */
  1316. management_agent_addr =
  1317. CSR1212_REGISTER_SPACE_BASE +
  1318. (kv->value.csr_offset << 2);
  1319. SBP2_DEBUG("sbp2_management_agent_addr = %x",
  1320. (unsigned int)management_agent_addr);
  1321. } else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
  1322. scsi_id->sbp2_lun =
  1323. ORB_SET_LUN(kv->value.immediate);
  1324. }
  1325. break;
  1326. case SBP2_COMMAND_SET_SPEC_ID_KEY:
  1327. /* Command spec organization */
  1328. command_set_spec_id = kv->value.immediate;
  1329. SBP2_DEBUG("sbp2_command_set_spec_id = %x",
  1330. (unsigned int)command_set_spec_id);
  1331. break;
  1332. case SBP2_COMMAND_SET_KEY:
  1333. /* Command set used by sbp2 device */
  1334. command_set = kv->value.immediate;
  1335. SBP2_DEBUG("sbp2_command_set = %x",
  1336. (unsigned int)command_set);
  1337. break;
  1338. case SBP2_UNIT_CHARACTERISTICS_KEY:
  1339. /*
  1340. * Unit characterisitcs (orb related stuff
  1341. * that I'm not yet paying attention to)
  1342. */
  1343. unit_characteristics = kv->value.immediate;
  1344. SBP2_DEBUG("sbp2_unit_characteristics = %x",
  1345. (unsigned int)unit_characteristics);
  1346. break;
  1347. case SBP2_FIRMWARE_REVISION_KEY:
  1348. /* Firmware revision */
  1349. firmware_revision = kv->value.immediate;
  1350. SBP2_DEBUG("sbp2_firmware_revision = %x",
  1351. (unsigned int)firmware_revision);
  1352. break;
  1353. default:
  1354. break;
  1355. }
  1356. }
  1357. workarounds = sbp2_default_workarounds;
  1358. if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
  1359. for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
  1360. if (sbp2_workarounds_table[i].firmware_revision &&
  1361. sbp2_workarounds_table[i].firmware_revision !=
  1362. (firmware_revision & 0xffff00))
  1363. continue;
  1364. if (sbp2_workarounds_table[i].model_id &&
  1365. sbp2_workarounds_table[i].model_id != ud->model_id)
  1366. continue;
  1367. workarounds |= sbp2_workarounds_table[i].workarounds;
  1368. break;
  1369. }
  1370. if (workarounds)
  1371. SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
  1372. "(firmware_revision 0x%06x, vendor_id 0x%06x,"
  1373. " model_id 0x%06x)",
  1374. NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
  1375. workarounds, firmware_revision,
  1376. ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
  1377. ud->model_id);
  1378. /* We would need one SCSI host template for each target to adjust
  1379. * max_sectors on the fly, therefore warn only. */
  1380. if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
  1381. (max_sectors * 512) > (128 * 1024))
  1382. SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
  1383. "max transfer size. WARNING: Current max_sectors "
  1384. "setting is larger than 128KB (%d sectors)",
  1385. NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
  1386. max_sectors);
  1387. /* If this is a logical unit directory entry, process the parent
  1388. * to get the values. */
  1389. if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
  1390. struct unit_directory *parent_ud =
  1391. container_of(ud->device.parent, struct unit_directory, device);
  1392. sbp2_parse_unit_directory(scsi_id, parent_ud);
  1393. } else {
  1394. scsi_id->sbp2_management_agent_addr = management_agent_addr;
  1395. scsi_id->sbp2_command_set_spec_id = command_set_spec_id;
  1396. scsi_id->sbp2_command_set = command_set;
  1397. scsi_id->sbp2_unit_characteristics = unit_characteristics;
  1398. scsi_id->sbp2_firmware_revision = firmware_revision;
  1399. scsi_id->workarounds = workarounds;
  1400. if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
  1401. scsi_id->sbp2_lun = ORB_SET_LUN(ud->lun);
  1402. }
  1403. }
  1404. #define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
  1405. /*
  1406. * This function is called in order to determine the max speed and packet
  1407. * size we can use in our ORBs. Note, that we (the driver and host) only
  1408. * initiate the transaction. The SBP-2 device actually transfers the data
  1409. * (by reading from the DMA area we tell it). This means that the SBP-2
  1410. * device decides the actual maximum data it can transfer. We just tell it
  1411. * the speed that it needs to use, and the max_rec the host supports, and
  1412. * it takes care of the rest.
  1413. */
  1414. static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
  1415. {
  1416. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1417. u8 payload;
  1418. SBP2_DEBUG_ENTER();
  1419. scsi_id->speed_code =
  1420. hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)];
  1421. /* Bump down our speed if the user requested it */
  1422. if (scsi_id->speed_code > max_speed) {
  1423. scsi_id->speed_code = max_speed;
  1424. SBP2_ERR("Forcing SBP-2 max speed down to %s",
  1425. hpsb_speedto_str[scsi_id->speed_code]);
  1426. }
  1427. /* Payload size is the lesser of what our speed supports and what
  1428. * our host supports. */
  1429. payload = min(sbp2_speedto_max_payload[scsi_id->speed_code],
  1430. (u8) (hi->host->csr.max_rec - 1));
  1431. /* If physical DMA is off, work around limitation in ohci1394:
  1432. * packet size must not exceed PAGE_SIZE */
  1433. if (scsi_id->ne->host->low_addr_space < (1ULL << 32))
  1434. while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
  1435. payload)
  1436. payload--;
  1437. HPSB_DEBUG("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
  1438. NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
  1439. hpsb_speedto_str[scsi_id->speed_code],
  1440. SBP2_PAYLOAD_TO_BYTES(payload));
  1441. scsi_id->max_payload_size = payload;
  1442. return 0;
  1443. }
  1444. /*
  1445. * This function is called in order to perform a SBP-2 agent reset.
  1446. */
  1447. static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
  1448. {
  1449. quadlet_t data;
  1450. u64 addr;
  1451. int retval;
  1452. unsigned long flags;
  1453. SBP2_DEBUG_ENTER();
  1454. cancel_delayed_work(&scsi_id->protocol_work);
  1455. if (wait)
  1456. flush_scheduled_work();
  1457. data = ntohl(SBP2_AGENT_RESET_DATA);
  1458. addr = scsi_id->sbp2_command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
  1459. if (wait)
  1460. retval = hpsb_node_write(scsi_id->ne, addr, &data, 4);
  1461. else
  1462. retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4);
  1463. if (retval < 0) {
  1464. SBP2_ERR("hpsb_node_write failed.\n");
  1465. return -EIO;
  1466. }
  1467. /*
  1468. * Need to make sure orb pointer is written on next command
  1469. */
  1470. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  1471. scsi_id->last_orb = NULL;
  1472. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  1473. return 0;
  1474. }
  1475. static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
  1476. struct sbp2scsi_host_info *hi,
  1477. struct sbp2_command_info *command,
  1478. unsigned int scsi_use_sg,
  1479. struct scatterlist *sgpnt,
  1480. u32 orb_direction,
  1481. enum dma_data_direction dma_dir)
  1482. {
  1483. command->dma_dir = dma_dir;
  1484. orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1485. orb->misc |= ORB_SET_DIRECTION(orb_direction);
  1486. /* Special case if only one element (and less than 64KB in size) */
  1487. if ((scsi_use_sg == 1) &&
  1488. (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
  1489. SBP2_DEBUG("Only one s/g element");
  1490. command->dma_size = sgpnt[0].length;
  1491. command->dma_type = CMD_DMA_PAGE;
  1492. command->cmd_dma = pci_map_page(hi->host->pdev,
  1493. sgpnt[0].page,
  1494. sgpnt[0].offset,
  1495. command->dma_size,
  1496. command->dma_dir);
  1497. SBP2_DMA_ALLOC("single page scatter element");
  1498. orb->data_descriptor_lo = command->cmd_dma;
  1499. orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
  1500. } else {
  1501. struct sbp2_unrestricted_page_table *sg_element =
  1502. &command->scatter_gather_element[0];
  1503. u32 sg_count, sg_len;
  1504. dma_addr_t sg_addr;
  1505. int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
  1506. dma_dir);
  1507. SBP2_DMA_ALLOC("scatter list");
  1508. command->dma_size = scsi_use_sg;
  1509. command->sge_buffer = sgpnt;
  1510. /* use page tables (s/g) */
  1511. orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
  1512. orb->data_descriptor_lo = command->sge_dma;
  1513. /*
  1514. * Loop through and fill out our sbp-2 page tables
  1515. * (and split up anything too large)
  1516. */
  1517. for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
  1518. sg_len = sg_dma_len(sgpnt);
  1519. sg_addr = sg_dma_address(sgpnt);
  1520. while (sg_len) {
  1521. sg_element[sg_count].segment_base_lo = sg_addr;
  1522. if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
  1523. sg_element[sg_count].length_segment_base_hi =
  1524. PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
  1525. sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
  1526. sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
  1527. } else {
  1528. sg_element[sg_count].length_segment_base_hi =
  1529. PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
  1530. sg_len = 0;
  1531. }
  1532. sg_count++;
  1533. }
  1534. }
  1535. /* Number of page table (s/g) elements */
  1536. orb->misc |= ORB_SET_DATA_SIZE(sg_count);
  1537. sbp2util_packet_dump(sg_element,
  1538. (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
  1539. "sbp2 s/g list", command->sge_dma);
  1540. /* Byte swap page tables if necessary */
  1541. sbp2util_cpu_to_be32_buffer(sg_element,
  1542. (sizeof(struct sbp2_unrestricted_page_table)) *
  1543. sg_count);
  1544. }
  1545. }
  1546. static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
  1547. struct sbp2scsi_host_info *hi,
  1548. struct sbp2_command_info *command,
  1549. struct scatterlist *sgpnt,
  1550. u32 orb_direction,
  1551. unsigned int scsi_request_bufflen,
  1552. void *scsi_request_buffer,
  1553. enum dma_data_direction dma_dir)
  1554. {
  1555. command->dma_dir = dma_dir;
  1556. command->dma_size = scsi_request_bufflen;
  1557. command->dma_type = CMD_DMA_SINGLE;
  1558. command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
  1559. command->dma_size, command->dma_dir);
  1560. orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1561. orb->misc |= ORB_SET_DIRECTION(orb_direction);
  1562. SBP2_DMA_ALLOC("single bulk");
  1563. /*
  1564. * Handle case where we get a command w/o s/g enabled (but
  1565. * check for transfers larger than 64K)
  1566. */
  1567. if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
  1568. orb->data_descriptor_lo = command->cmd_dma;
  1569. orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
  1570. } else {
  1571. struct sbp2_unrestricted_page_table *sg_element =
  1572. &command->scatter_gather_element[0];
  1573. u32 sg_count, sg_len;
  1574. dma_addr_t sg_addr;
  1575. /*
  1576. * Need to turn this into page tables, since the
  1577. * buffer is too large.
  1578. */
  1579. orb->data_descriptor_lo = command->sge_dma;
  1580. /* Use page tables (s/g) */
  1581. orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
  1582. /*
  1583. * fill out our sbp-2 page tables (and split up
  1584. * the large buffer)
  1585. */
  1586. sg_count = 0;
  1587. sg_len = scsi_request_bufflen;
  1588. sg_addr = command->cmd_dma;
  1589. while (sg_len) {
  1590. sg_element[sg_count].segment_base_lo = sg_addr;
  1591. if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
  1592. sg_element[sg_count].length_segment_base_hi =
  1593. PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
  1594. sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
  1595. sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
  1596. } else {
  1597. sg_element[sg_count].length_segment_base_hi =
  1598. PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
  1599. sg_len = 0;
  1600. }
  1601. sg_count++;
  1602. }
  1603. /* Number of page table (s/g) elements */
  1604. orb->misc |= ORB_SET_DATA_SIZE(sg_count);
  1605. sbp2util_packet_dump(sg_element,
  1606. (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
  1607. "sbp2 s/g list", command->sge_dma);
  1608. /* Byte swap page tables if necessary */
  1609. sbp2util_cpu_to_be32_buffer(sg_element,
  1610. (sizeof(struct sbp2_unrestricted_page_table)) *
  1611. sg_count);
  1612. }
  1613. }
  1614. /*
  1615. * This function is called to create the actual command orb and s/g list
  1616. * out of the scsi command itself.
  1617. */
  1618. static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
  1619. struct sbp2_command_info *command,
  1620. unchar *scsi_cmd,
  1621. unsigned int scsi_use_sg,
  1622. unsigned int scsi_request_bufflen,
  1623. void *scsi_request_buffer,
  1624. enum dma_data_direction dma_dir)
  1625. {
  1626. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1627. struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
  1628. struct sbp2_command_orb *command_orb = &command->command_orb;
  1629. u32 orb_direction;
  1630. /*
  1631. * Set-up our command ORB..
  1632. *
  1633. * NOTE: We're doing unrestricted page tables (s/g), as this is
  1634. * best performance (at least with the devices I have). This means
  1635. * that data_size becomes the number of s/g elements, and
  1636. * page_size should be zero (for unrestricted).
  1637. */
  1638. command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
  1639. command_orb->next_ORB_lo = 0x0;
  1640. command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
  1641. command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
  1642. command_orb->misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */
  1643. if (dma_dir == DMA_NONE)
  1644. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1645. else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
  1646. orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
  1647. else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
  1648. orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
  1649. else {
  1650. SBP2_WARN("Falling back to DMA_NONE");
  1651. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1652. }
  1653. /* Set-up our pagetable stuff */
  1654. if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
  1655. SBP2_DEBUG("No data transfer");
  1656. command_orb->data_descriptor_hi = 0x0;
  1657. command_orb->data_descriptor_lo = 0x0;
  1658. command_orb->misc |= ORB_SET_DIRECTION(1);
  1659. } else if (scsi_use_sg) {
  1660. SBP2_DEBUG("Use scatter/gather");
  1661. sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg,
  1662. sgpnt, orb_direction, dma_dir);
  1663. } else {
  1664. SBP2_DEBUG("No scatter/gather");
  1665. sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
  1666. orb_direction, scsi_request_bufflen,
  1667. scsi_request_buffer, dma_dir);
  1668. }
  1669. /* Byte swap command ORB if necessary */
  1670. sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
  1671. /* Put our scsi command in the command ORB */
  1672. memset(command_orb->cdb, 0, 12);
  1673. memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
  1674. }
  1675. /*
  1676. * This function is called in order to begin a regular SBP-2 command.
  1677. */
  1678. static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
  1679. struct sbp2_command_info *command)
  1680. {
  1681. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1682. struct sbp2_command_orb *command_orb = &command->command_orb;
  1683. struct sbp2_command_orb *last_orb;
  1684. dma_addr_t last_orb_dma;
  1685. u64 addr = scsi_id->sbp2_command_block_agent_addr;
  1686. quadlet_t data[2];
  1687. size_t length;
  1688. unsigned long flags;
  1689. outstanding_orb_incr;
  1690. SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x",
  1691. command_orb, global_outstanding_command_orbs);
  1692. pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
  1693. sizeof(struct sbp2_command_orb),
  1694. PCI_DMA_TODEVICE);
  1695. pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
  1696. sizeof(command->scatter_gather_element),
  1697. PCI_DMA_BIDIRECTIONAL);
  1698. /*
  1699. * Check to see if there are any previous orbs to use
  1700. */
  1701. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  1702. last_orb = scsi_id->last_orb;
  1703. last_orb_dma = scsi_id->last_orb_dma;
  1704. if (!last_orb) {
  1705. /*
  1706. * last_orb == NULL means: We know that the target's fetch agent
  1707. * is not active right now.
  1708. */
  1709. addr += SBP2_ORB_POINTER_OFFSET;
  1710. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1711. data[1] = command->command_orb_dma;
  1712. sbp2util_cpu_to_be32_buffer(data, 8);
  1713. length = 8;
  1714. } else {
  1715. /*
  1716. * last_orb != NULL means: We know that the target's fetch agent
  1717. * is (very probably) not dead or in reset state right now.
  1718. * We have an ORB already sent that we can append a new one to.
  1719. * The target's fetch agent may or may not have read this
  1720. * previous ORB yet.
  1721. */
  1722. pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma,
  1723. sizeof(struct sbp2_command_orb),
  1724. PCI_DMA_TODEVICE);
  1725. last_orb->next_ORB_lo = cpu_to_be32(command->command_orb_dma);
  1726. wmb();
  1727. /* Tells hardware that this pointer is valid */
  1728. last_orb->next_ORB_hi = 0;
  1729. pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma,
  1730. sizeof(struct sbp2_command_orb),
  1731. PCI_DMA_TODEVICE);
  1732. addr += SBP2_DOORBELL_OFFSET;
  1733. data[0] = 0;
  1734. length = 4;
  1735. }
  1736. scsi_id->last_orb = command_orb;
  1737. scsi_id->last_orb_dma = command->command_orb_dma;
  1738. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  1739. SBP2_ORB_DEBUG("write to %s register, command orb %p",
  1740. last_orb ? "DOORBELL" : "ORB_POINTER", command_orb);
  1741. if (sbp2util_node_write_no_wait(scsi_id->ne, addr, data, length)) {
  1742. /*
  1743. * sbp2util_node_write_no_wait failed. We certainly ran out
  1744. * of transaction labels, perhaps just because there were no
  1745. * context switches which gave khpsbpkt a chance to collect
  1746. * free tlabels. Try again in non-atomic context. If necessary,
  1747. * the workqueue job will sleep to guaranteedly get a tlabel.
  1748. * We do not accept new commands until the job is over.
  1749. */
  1750. scsi_block_requests(scsi_id->scsi_host);
  1751. PREPARE_WORK(&scsi_id->protocol_work,
  1752. last_orb ? sbp2util_write_doorbell:
  1753. sbp2util_write_orb_pointer,
  1754. scsi_id);
  1755. schedule_work(&scsi_id->protocol_work);
  1756. }
  1757. }
  1758. /*
  1759. * This function is called in order to begin a regular SBP-2 command.
  1760. */
  1761. static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
  1762. struct scsi_cmnd *SCpnt,
  1763. void (*done)(struct scsi_cmnd *))
  1764. {
  1765. unchar *cmd = (unchar *) SCpnt->cmnd;
  1766. unsigned int request_bufflen = SCpnt->request_bufflen;
  1767. struct sbp2_command_info *command;
  1768. SBP2_DEBUG_ENTER();
  1769. SBP2_DEBUG("SCSI transfer size = %x", request_bufflen);
  1770. SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
  1771. /*
  1772. * Allocate a command orb and s/g structure
  1773. */
  1774. command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
  1775. if (!command) {
  1776. return -EIO;
  1777. }
  1778. /*
  1779. * Now actually fill in the comamnd orb and sbp2 s/g list
  1780. */
  1781. sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
  1782. request_bufflen, SCpnt->request_buffer,
  1783. SCpnt->sc_data_direction);
  1784. sbp2util_packet_dump(&command->command_orb, sizeof(struct sbp2_command_orb),
  1785. "sbp2 command orb", command->command_orb_dma);
  1786. /*
  1787. * Link up the orb, and ring the doorbell if needed
  1788. */
  1789. sbp2_link_orb_command(scsi_id, command);
  1790. return 0;
  1791. }
  1792. /*
  1793. * Translates SBP-2 status into SCSI sense data for check conditions
  1794. */
  1795. static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
  1796. {
  1797. SBP2_DEBUG_ENTER();
  1798. /*
  1799. * Ok, it's pretty ugly... ;-)
  1800. */
  1801. sense_data[0] = 0x70;
  1802. sense_data[1] = 0x0;
  1803. sense_data[2] = sbp2_status[9];
  1804. sense_data[3] = sbp2_status[12];
  1805. sense_data[4] = sbp2_status[13];
  1806. sense_data[5] = sbp2_status[14];
  1807. sense_data[6] = sbp2_status[15];
  1808. sense_data[7] = 10;
  1809. sense_data[8] = sbp2_status[16];
  1810. sense_data[9] = sbp2_status[17];
  1811. sense_data[10] = sbp2_status[18];
  1812. sense_data[11] = sbp2_status[19];
  1813. sense_data[12] = sbp2_status[10];
  1814. sense_data[13] = sbp2_status[11];
  1815. sense_data[14] = sbp2_status[20];
  1816. sense_data[15] = sbp2_status[21];
  1817. return sbp2_status[8] & 0x3f; /* return scsi status */
  1818. }
  1819. /*
  1820. * This function deals with status writes from the SBP-2 device
  1821. */
  1822. static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
  1823. int destid, quadlet_t *data, u64 addr,
  1824. size_t length, u16 fl)
  1825. {
  1826. struct sbp2scsi_host_info *hi;
  1827. struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
  1828. struct scsi_cmnd *SCpnt = NULL;
  1829. struct sbp2_status_block *sb;
  1830. u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
  1831. struct sbp2_command_info *command;
  1832. unsigned long flags;
  1833. SBP2_DEBUG_ENTER();
  1834. sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr);
  1835. if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
  1836. SBP2_ERR("Wrong size of status block");
  1837. return RCODE_ADDRESS_ERROR;
  1838. }
  1839. if (unlikely(!host)) {
  1840. SBP2_ERR("host is NULL - this is bad!");
  1841. return RCODE_ADDRESS_ERROR;
  1842. }
  1843. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  1844. if (unlikely(!hi)) {
  1845. SBP2_ERR("host info is NULL - this is bad!");
  1846. return RCODE_ADDRESS_ERROR;
  1847. }
  1848. /*
  1849. * Find our scsi_id structure by looking at the status fifo address
  1850. * written to by the sbp2 device.
  1851. */
  1852. list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
  1853. if (scsi_id_tmp->ne->nodeid == nodeid &&
  1854. scsi_id_tmp->status_fifo_addr == addr) {
  1855. scsi_id = scsi_id_tmp;
  1856. break;
  1857. }
  1858. }
  1859. if (unlikely(!scsi_id)) {
  1860. SBP2_ERR("scsi_id is NULL - device is gone?");
  1861. return RCODE_ADDRESS_ERROR;
  1862. }
  1863. /*
  1864. * Put response into scsi_id status fifo buffer. The first two bytes
  1865. * come in big endian bit order. Often the target writes only a
  1866. * truncated status block, minimally the first two quadlets. The rest
  1867. * is implied to be zeros.
  1868. */
  1869. sb = &scsi_id->status_block;
  1870. memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
  1871. memcpy(sb, data, length);
  1872. sbp2util_be32_to_cpu_buffer(sb, 8);
  1873. /*
  1874. * Ignore unsolicited status. Handle command ORB status.
  1875. */
  1876. if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
  1877. command = NULL;
  1878. else
  1879. command = sbp2util_find_command_for_orb(scsi_id,
  1880. sb->ORB_offset_lo);
  1881. if (command) {
  1882. SBP2_DEBUG("Found status for command ORB");
  1883. pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
  1884. sizeof(struct sbp2_command_orb),
  1885. PCI_DMA_TODEVICE);
  1886. pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
  1887. sizeof(command->scatter_gather_element),
  1888. PCI_DMA_BIDIRECTIONAL);
  1889. SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb);
  1890. outstanding_orb_decr;
  1891. /*
  1892. * Matched status with command, now grab scsi command pointers
  1893. * and check status.
  1894. */
  1895. /*
  1896. * FIXME: If the src field in the status is 1, the ORB DMA must
  1897. * not be reused until status for a subsequent ORB is received.
  1898. */
  1899. SCpnt = command->Current_SCpnt;
  1900. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  1901. sbp2util_mark_command_completed(scsi_id, command);
  1902. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  1903. if (SCpnt) {
  1904. u32 h = sb->ORB_offset_hi_misc;
  1905. u32 r = STATUS_GET_RESP(h);
  1906. if (r != RESP_STATUS_REQUEST_COMPLETE) {
  1907. SBP2_WARN("resp 0x%x, sbp_status 0x%x",
  1908. r, STATUS_GET_SBP_STATUS(h));
  1909. scsi_status =
  1910. r == RESP_STATUS_TRANSPORT_FAILURE ?
  1911. SBP2_SCSI_STATUS_BUSY :
  1912. SBP2_SCSI_STATUS_COMMAND_TERMINATED;
  1913. }
  1914. /*
  1915. * See if the target stored any scsi status information.
  1916. */
  1917. if (STATUS_GET_LEN(h) > 1) {
  1918. SBP2_DEBUG("CHECK CONDITION");
  1919. scsi_status = sbp2_status_to_sense_data(
  1920. (unchar *)sb, SCpnt->sense_buffer);
  1921. }
  1922. /*
  1923. * Check to see if the dead bit is set. If so, we'll
  1924. * have to initiate a fetch agent reset.
  1925. */
  1926. if (STATUS_TEST_DEAD(h)) {
  1927. SBP2_DEBUG("Dead bit set - "
  1928. "initiating fetch agent reset");
  1929. sbp2_agent_reset(scsi_id, 0);
  1930. }
  1931. SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
  1932. }
  1933. /*
  1934. * Check here to see if there are no commands in-use. If there
  1935. * are none, we know that the fetch agent left the active state
  1936. * _and_ that we did not reactivate it yet. Therefore clear
  1937. * last_orb so that next time we write directly to the
  1938. * ORB_POINTER register. That way the fetch agent does not need
  1939. * to refetch the next_ORB.
  1940. */
  1941. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  1942. if (list_empty(&scsi_id->sbp2_command_orb_inuse))
  1943. scsi_id->last_orb = NULL;
  1944. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  1945. } else {
  1946. /*
  1947. * It's probably a login/logout/reconnect status.
  1948. */
  1949. if ((sb->ORB_offset_lo == scsi_id->reconnect_orb_dma) ||
  1950. (sb->ORB_offset_lo == scsi_id->login_orb_dma) ||
  1951. (sb->ORB_offset_lo == scsi_id->query_logins_orb_dma) ||
  1952. (sb->ORB_offset_lo == scsi_id->logout_orb_dma)) {
  1953. scsi_id->access_complete = 1;
  1954. wake_up_interruptible(&access_wq);
  1955. }
  1956. }
  1957. if (SCpnt) {
  1958. SBP2_DEBUG("Completing SCSI command");
  1959. sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
  1960. command->Current_done);
  1961. SBP2_ORB_DEBUG("command orb completed");
  1962. }
  1963. return RCODE_COMPLETE;
  1964. }
  1965. /**************************************
  1966. * SCSI interface related section
  1967. **************************************/
  1968. /*
  1969. * This routine is the main request entry routine for doing I/O. It is
  1970. * called from the scsi stack directly.
  1971. */
  1972. static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
  1973. void (*done)(struct scsi_cmnd *))
  1974. {
  1975. struct scsi_id_instance_data *scsi_id =
  1976. (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
  1977. struct sbp2scsi_host_info *hi;
  1978. int result = DID_NO_CONNECT << 16;
  1979. SBP2_DEBUG_ENTER();
  1980. #if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
  1981. scsi_print_command(SCpnt);
  1982. #endif
  1983. if (!sbp2util_node_is_available(scsi_id))
  1984. goto done;
  1985. hi = scsi_id->hi;
  1986. if (!hi) {
  1987. SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!");
  1988. goto done;
  1989. }
  1990. /*
  1991. * Until we handle multiple luns, just return selection time-out
  1992. * to any IO directed at non-zero LUNs
  1993. */
  1994. if (SCpnt->device->lun)
  1995. goto done;
  1996. /*
  1997. * Check for request sense command, and handle it here
  1998. * (autorequest sense)
  1999. */
  2000. if (SCpnt->cmnd[0] == REQUEST_SENSE) {
  2001. SBP2_DEBUG("REQUEST_SENSE");
  2002. memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
  2003. memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
  2004. sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
  2005. return 0;
  2006. }
  2007. /*
  2008. * Check to see if we are in the middle of a bus reset.
  2009. */
  2010. if (!hpsb_node_entry_valid(scsi_id->ne)) {
  2011. SBP2_ERR("Bus reset in progress - rejecting command");
  2012. result = DID_BUS_BUSY << 16;
  2013. goto done;
  2014. }
  2015. /*
  2016. * Bidirectional commands are not yet implemented,
  2017. * and unknown transfer direction not handled.
  2018. */
  2019. if (SCpnt->sc_data_direction == DMA_BIDIRECTIONAL) {
  2020. SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
  2021. result = DID_ERROR << 16;
  2022. goto done;
  2023. }
  2024. /*
  2025. * Try and send our SCSI command
  2026. */
  2027. if (sbp2_send_command(scsi_id, SCpnt, done)) {
  2028. SBP2_ERR("Error sending SCSI command");
  2029. sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
  2030. SCpnt, done);
  2031. }
  2032. return 0;
  2033. done:
  2034. SCpnt->result = result;
  2035. done(SCpnt);
  2036. return 0;
  2037. }
  2038. /*
  2039. * This function is called in order to complete all outstanding SBP-2
  2040. * commands (in case of resets, etc.).
  2041. */
  2042. static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
  2043. u32 status)
  2044. {
  2045. struct sbp2scsi_host_info *hi = scsi_id->hi;
  2046. struct list_head *lh;
  2047. struct sbp2_command_info *command;
  2048. unsigned long flags;
  2049. SBP2_DEBUG_ENTER();
  2050. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  2051. while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
  2052. SBP2_DEBUG("Found pending command to complete");
  2053. lh = scsi_id->sbp2_command_orb_inuse.next;
  2054. command = list_entry(lh, struct sbp2_command_info, list);
  2055. pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
  2056. sizeof(struct sbp2_command_orb),
  2057. PCI_DMA_TODEVICE);
  2058. pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
  2059. sizeof(command->scatter_gather_element),
  2060. PCI_DMA_BIDIRECTIONAL);
  2061. sbp2util_mark_command_completed(scsi_id, command);
  2062. if (command->Current_SCpnt) {
  2063. command->Current_SCpnt->result = status << 16;
  2064. command->Current_done(command->Current_SCpnt);
  2065. }
  2066. }
  2067. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  2068. return;
  2069. }
  2070. /*
  2071. * This function is called in order to complete a regular SBP-2 command.
  2072. *
  2073. * This can be called in interrupt context.
  2074. */
  2075. static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
  2076. u32 scsi_status, struct scsi_cmnd *SCpnt,
  2077. void (*done)(struct scsi_cmnd *))
  2078. {
  2079. SBP2_DEBUG_ENTER();
  2080. /*
  2081. * Sanity
  2082. */
  2083. if (!SCpnt) {
  2084. SBP2_ERR("SCpnt is NULL");
  2085. return;
  2086. }
  2087. /*
  2088. * If a bus reset is in progress and there was an error, don't
  2089. * complete the command, just let it get retried at the end of the
  2090. * bus reset.
  2091. */
  2092. if (!hpsb_node_entry_valid(scsi_id->ne)
  2093. && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
  2094. SBP2_ERR("Bus reset in progress - retry command later");
  2095. return;
  2096. }
  2097. /*
  2098. * Switch on scsi status
  2099. */
  2100. switch (scsi_status) {
  2101. case SBP2_SCSI_STATUS_GOOD:
  2102. SCpnt->result = DID_OK << 16;
  2103. break;
  2104. case SBP2_SCSI_STATUS_BUSY:
  2105. SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
  2106. SCpnt->result = DID_BUS_BUSY << 16;
  2107. break;
  2108. case SBP2_SCSI_STATUS_CHECK_CONDITION:
  2109. SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION");
  2110. SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
  2111. #if CONFIG_IEEE1394_SBP2_DEBUG >= 1
  2112. scsi_print_command(SCpnt);
  2113. scsi_print_sense(SBP2_DEVICE_NAME, SCpnt);
  2114. #endif
  2115. break;
  2116. case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
  2117. SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
  2118. SCpnt->result = DID_NO_CONNECT << 16;
  2119. scsi_print_command(SCpnt);
  2120. break;
  2121. case SBP2_SCSI_STATUS_CONDITION_MET:
  2122. case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
  2123. case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
  2124. SBP2_ERR("Bad SCSI status = %x", scsi_status);
  2125. SCpnt->result = DID_ERROR << 16;
  2126. scsi_print_command(SCpnt);
  2127. break;
  2128. default:
  2129. SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
  2130. SCpnt->result = DID_ERROR << 16;
  2131. }
  2132. /*
  2133. * If a bus reset is in progress and there was an error, complete
  2134. * the command as busy so that it will get retried.
  2135. */
  2136. if (!hpsb_node_entry_valid(scsi_id->ne)
  2137. && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
  2138. SBP2_ERR("Completing command with busy (bus reset)");
  2139. SCpnt->result = DID_BUS_BUSY << 16;
  2140. }
  2141. /*
  2142. * If a unit attention occurs, return busy status so it gets
  2143. * retried... it could have happened because of a 1394 bus reset
  2144. * or hot-plug...
  2145. * XXX DID_BUS_BUSY is actually a bad idea because it will defy
  2146. * the scsi layer's retry logic.
  2147. */
  2148. #if 0
  2149. if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) &&
  2150. (SCpnt->sense_buffer[2] == UNIT_ATTENTION)) {
  2151. SBP2_DEBUG("UNIT ATTENTION - return busy");
  2152. SCpnt->result = DID_BUS_BUSY << 16;
  2153. }
  2154. #endif
  2155. /*
  2156. * Tell scsi stack that we're done with this command
  2157. */
  2158. done(SCpnt);
  2159. }
  2160. static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
  2161. {
  2162. struct scsi_id_instance_data *scsi_id =
  2163. (struct scsi_id_instance_data *)sdev->host->hostdata[0];
  2164. scsi_id->sdev = sdev;
  2165. sdev->allow_restart = 1;
  2166. if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36)
  2167. sdev->inquiry_len = 36;
  2168. return 0;
  2169. }
  2170. static int sbp2scsi_slave_configure(struct scsi_device *sdev)
  2171. {
  2172. struct scsi_id_instance_data *scsi_id =
  2173. (struct scsi_id_instance_data *)sdev->host->hostdata[0];
  2174. blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
  2175. sdev->use_10_for_rw = 1;
  2176. if (sdev->type == TYPE_DISK &&
  2177. scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
  2178. sdev->skip_ms_page_8 = 1;
  2179. if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
  2180. sdev->fix_capacity = 1;
  2181. return 0;
  2182. }
  2183. static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
  2184. {
  2185. ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL;
  2186. return;
  2187. }
  2188. /*
  2189. * Called by scsi stack when something has really gone wrong. Usually
  2190. * called when a command has timed-out for some reason.
  2191. */
  2192. static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
  2193. {
  2194. struct scsi_id_instance_data *scsi_id =
  2195. (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
  2196. struct sbp2scsi_host_info *hi = scsi_id->hi;
  2197. struct sbp2_command_info *command;
  2198. unsigned long flags;
  2199. SBP2_ERR("aborting sbp2 command");
  2200. scsi_print_command(SCpnt);
  2201. if (sbp2util_node_is_available(scsi_id)) {
  2202. /*
  2203. * Right now, just return any matching command structures
  2204. * to the free pool.
  2205. */
  2206. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  2207. command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
  2208. if (command) {
  2209. SBP2_DEBUG("Found command to abort");
  2210. pci_dma_sync_single_for_cpu(hi->host->pdev,
  2211. command->command_orb_dma,
  2212. sizeof(struct sbp2_command_orb),
  2213. PCI_DMA_TODEVICE);
  2214. pci_dma_sync_single_for_cpu(hi->host->pdev,
  2215. command->sge_dma,
  2216. sizeof(command->scatter_gather_element),
  2217. PCI_DMA_BIDIRECTIONAL);
  2218. sbp2util_mark_command_completed(scsi_id, command);
  2219. if (command->Current_SCpnt) {
  2220. command->Current_SCpnt->result = DID_ABORT << 16;
  2221. command->Current_done(command->Current_SCpnt);
  2222. }
  2223. }
  2224. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  2225. /*
  2226. * Initiate a fetch agent reset.
  2227. */
  2228. sbp2_agent_reset(scsi_id, 1);
  2229. sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
  2230. }
  2231. return SUCCESS;
  2232. }
  2233. /*
  2234. * Called by scsi stack when something has really gone wrong.
  2235. */
  2236. static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
  2237. {
  2238. struct scsi_id_instance_data *scsi_id =
  2239. (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
  2240. SBP2_ERR("reset requested");
  2241. if (sbp2util_node_is_available(scsi_id)) {
  2242. SBP2_ERR("Generating sbp2 fetch agent reset");
  2243. sbp2_agent_reset(scsi_id, 1);
  2244. }
  2245. return SUCCESS;
  2246. }
  2247. static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
  2248. struct device_attribute *attr,
  2249. char *buf)
  2250. {
  2251. struct scsi_device *sdev;
  2252. struct scsi_id_instance_data *scsi_id;
  2253. int lun;
  2254. if (!(sdev = to_scsi_device(dev)))
  2255. return 0;
  2256. if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0]))
  2257. return 0;
  2258. lun = ORB_SET_LUN(scsi_id->sbp2_lun);
  2259. return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
  2260. scsi_id->ud->id, lun);
  2261. }
  2262. static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
  2263. static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
  2264. &dev_attr_ieee1394_id,
  2265. NULL
  2266. };
  2267. MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
  2268. MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
  2269. MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
  2270. MODULE_LICENSE("GPL");
  2271. /* SCSI host template */
  2272. static struct scsi_host_template scsi_driver_template = {
  2273. .module = THIS_MODULE,
  2274. .name = "SBP-2 IEEE-1394",
  2275. .proc_name = SBP2_DEVICE_NAME,
  2276. .queuecommand = sbp2scsi_queuecommand,
  2277. .eh_abort_handler = sbp2scsi_abort,
  2278. .eh_device_reset_handler = sbp2scsi_reset,
  2279. .slave_alloc = sbp2scsi_slave_alloc,
  2280. .slave_configure = sbp2scsi_slave_configure,
  2281. .slave_destroy = sbp2scsi_slave_destroy,
  2282. .this_id = -1,
  2283. .sg_tablesize = SG_ALL,
  2284. .use_clustering = ENABLE_CLUSTERING,
  2285. .cmd_per_lun = SBP2_MAX_CMDS,
  2286. .can_queue = SBP2_MAX_CMDS,
  2287. .emulated = 1,
  2288. .sdev_attrs = sbp2_sysfs_sdev_attrs,
  2289. };
  2290. static int sbp2_module_init(void)
  2291. {
  2292. int ret;
  2293. SBP2_DEBUG_ENTER();
  2294. /* Module load debug option to force one command at a time (serializing I/O) */
  2295. if (serialize_io) {
  2296. SBP2_INFO("Driver forced to serialize I/O (serialize_io=1)");
  2297. SBP2_INFO("Try serialize_io=0 for better performance");
  2298. scsi_driver_template.can_queue = 1;
  2299. scsi_driver_template.cmd_per_lun = 1;
  2300. }
  2301. if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
  2302. (max_sectors * 512) > (128 * 1024))
  2303. max_sectors = 128 * 1024 / 512;
  2304. scsi_driver_template.max_sectors = max_sectors;
  2305. /* Register our high level driver with 1394 stack */
  2306. hpsb_register_highlevel(&sbp2_highlevel);
  2307. ret = hpsb_register_protocol(&sbp2_driver);
  2308. if (ret) {
  2309. SBP2_ERR("Failed to register protocol");
  2310. hpsb_unregister_highlevel(&sbp2_highlevel);
  2311. return ret;
  2312. }
  2313. return 0;
  2314. }
  2315. static void __exit sbp2_module_exit(void)
  2316. {
  2317. SBP2_DEBUG_ENTER();
  2318. hpsb_unregister_protocol(&sbp2_driver);
  2319. hpsb_unregister_highlevel(&sbp2_highlevel);
  2320. }
  2321. module_init(sbp2_module_init);
  2322. module_exit(sbp2_module_exit);