sbp2.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  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 (usually storage devices) as regular
  32. * SCSI devices. E.g. mount /dev/sda1, fdisk, mkfs, etc..
  33. *
  34. * See http://www.t10.org/drafts.htm#sbp2 for the final draft of the SBP-2
  35. * specification and for where to purchase the official standard.
  36. *
  37. * TODO:
  38. * - look into possible improvements of the SCSI error handlers
  39. * - handle Unit_Characteristics.mgt_ORB_timeout and .ORB_size
  40. * - handle Logical_Unit_Number.ordered
  41. * - handle src == 1 in status blocks
  42. * - reimplement the DMA mapping in absence of physical DMA so that
  43. * bus_to_virt is no longer required
  44. * - debug the handling of absent physical DMA
  45. * - replace CONFIG_IEEE1394_SBP2_PHYS_DMA by automatic detection
  46. * (this is easy but depends on the previous two TODO items)
  47. * - make the parameter serialize_io configurable per device
  48. * - move all requests to fetch agent registers into non-atomic context,
  49. * replace all usages of sbp2util_node_write_no_wait by true transactions
  50. * - convert to generic DMA mapping API to eliminate dependency on PCI
  51. * Grep for inline FIXME comments below.
  52. */
  53. #include <linux/blkdev.h>
  54. #include <linux/compiler.h>
  55. #include <linux/delay.h>
  56. #include <linux/device.h>
  57. #include <linux/dma-mapping.h>
  58. #include <linux/gfp.h>
  59. #include <linux/init.h>
  60. #include <linux/kernel.h>
  61. #include <linux/list.h>
  62. #include <linux/module.h>
  63. #include <linux/moduleparam.h>
  64. #include <linux/pci.h>
  65. #include <linux/slab.h>
  66. #include <linux/spinlock.h>
  67. #include <linux/stat.h>
  68. #include <linux/string.h>
  69. #include <linux/stringify.h>
  70. #include <linux/types.h>
  71. #include <linux/wait.h>
  72. #include <asm/byteorder.h>
  73. #include <asm/errno.h>
  74. #include <asm/param.h>
  75. #include <asm/scatterlist.h>
  76. #include <asm/system.h>
  77. #include <asm/types.h>
  78. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  79. #include <asm/io.h> /* for bus_to_virt */
  80. #endif
  81. #include <scsi/scsi.h>
  82. #include <scsi/scsi_cmnd.h>
  83. #include <scsi/scsi_dbg.h>
  84. #include <scsi/scsi_device.h>
  85. #include <scsi/scsi_host.h>
  86. #include "csr1212.h"
  87. #include "highlevel.h"
  88. #include "hosts.h"
  89. #include "ieee1394.h"
  90. #include "ieee1394_core.h"
  91. #include "ieee1394_hotplug.h"
  92. #include "ieee1394_transactions.h"
  93. #include "ieee1394_types.h"
  94. #include "nodemgr.h"
  95. #include "sbp2.h"
  96. /*
  97. * Module load parameter definitions
  98. */
  99. /*
  100. * Change max_speed on module load if you have a bad IEEE-1394
  101. * controller that has trouble running 2KB packets at 400mb.
  102. *
  103. * NOTE: On certain OHCI parts I have seen short packets on async transmit
  104. * (probably due to PCI latency/throughput issues with the part). You can
  105. * bump down the speed if you are running into problems.
  106. */
  107. static int sbp2_max_speed = IEEE1394_SPEED_MAX;
  108. module_param_named(max_speed, sbp2_max_speed, int, 0644);
  109. MODULE_PARM_DESC(max_speed, "Force max speed "
  110. "(3 = 800Mb/s, 2 = 400Mb/s, 1 = 200Mb/s, 0 = 100Mb/s)");
  111. /*
  112. * Set serialize_io to 1 if you'd like only one scsi command sent
  113. * down to us at a time (debugging). This might be necessary for very
  114. * badly behaved sbp2 devices.
  115. */
  116. static int sbp2_serialize_io = 1;
  117. module_param_named(serialize_io, sbp2_serialize_io, int, 0444);
  118. MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers "
  119. "(default = 1, faster = 0)");
  120. /*
  121. * Bump up max_sectors if you'd like to support very large sized
  122. * transfers. Please note that some older sbp2 bridge chips are broken for
  123. * transfers greater or equal to 128KB. Default is a value of 255
  124. * sectors, or just under 128KB (at 512 byte sector size). I can note that
  125. * the Oxsemi sbp2 chipsets have no problems supporting very large
  126. * transfer sizes.
  127. */
  128. static int sbp2_max_sectors = SBP2_MAX_SECTORS;
  129. module_param_named(max_sectors, sbp2_max_sectors, int, 0444);
  130. MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported "
  131. "(default = " __stringify(SBP2_MAX_SECTORS) ")");
  132. /*
  133. * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
  134. * do an exclusive login, as it's generally unsafe to have two hosts
  135. * talking to a single sbp2 device at the same time (filesystem coherency,
  136. * etc.). If you're running an sbp2 device that supports multiple logins,
  137. * and you're either running read-only filesystems or some sort of special
  138. * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
  139. * File System, or Lustre, then set exclusive_login to zero.
  140. *
  141. * So far only bridges from Oxford Semiconductor are known to support
  142. * concurrent logins. Depending on firmware, four or two concurrent logins
  143. * are possible on OXFW911 and newer Oxsemi bridges.
  144. */
  145. static int sbp2_exclusive_login = 1;
  146. module_param_named(exclusive_login, sbp2_exclusive_login, int, 0644);
  147. MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
  148. "(default = 1)");
  149. /*
  150. * If any of the following workarounds is required for your device to work,
  151. * please submit the kernel messages logged by sbp2 to the linux1394-devel
  152. * mailing list.
  153. *
  154. * - 128kB max transfer
  155. * Limit transfer size. Necessary for some old bridges.
  156. *
  157. * - 36 byte inquiry
  158. * When scsi_mod probes the device, let the inquiry command look like that
  159. * from MS Windows.
  160. *
  161. * - skip mode page 8
  162. * Suppress sending of mode_sense for mode page 8 if the device pretends to
  163. * support the SCSI Primary Block commands instead of Reduced Block Commands.
  164. *
  165. * - fix capacity
  166. * Tell sd_mod to correct the last sector number reported by read_capacity.
  167. * Avoids access beyond actual disk limits on devices with an off-by-one bug.
  168. * Don't use this with devices which don't have this bug.
  169. *
  170. * - override internal blacklist
  171. * Instead of adding to the built-in blacklist, use only the workarounds
  172. * specified in the module load parameter.
  173. * Useful if a blacklist entry interfered with a non-broken device.
  174. */
  175. static int sbp2_default_workarounds;
  176. module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
  177. MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
  178. ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
  179. ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
  180. ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
  181. ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
  182. ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
  183. ", or a combination)");
  184. #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
  185. #define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  186. /*
  187. * Globals
  188. */
  189. static void sbp2scsi_complete_all_commands(struct sbp2_lu *, u32);
  190. static void sbp2scsi_complete_command(struct sbp2_lu *, u32, struct scsi_cmnd *,
  191. void (*)(struct scsi_cmnd *));
  192. static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *);
  193. static int sbp2_start_device(struct sbp2_lu *);
  194. static void sbp2_remove_device(struct sbp2_lu *);
  195. static int sbp2_login_device(struct sbp2_lu *);
  196. static int sbp2_reconnect_device(struct sbp2_lu *);
  197. static int sbp2_logout_device(struct sbp2_lu *);
  198. static void sbp2_host_reset(struct hpsb_host *);
  199. static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
  200. u64, size_t, u16);
  201. static int sbp2_agent_reset(struct sbp2_lu *, int);
  202. static void sbp2_parse_unit_directory(struct sbp2_lu *,
  203. struct unit_directory *);
  204. static int sbp2_set_busy_timeout(struct sbp2_lu *);
  205. static int sbp2_max_speed_and_size(struct sbp2_lu *);
  206. static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
  207. static struct hpsb_highlevel sbp2_highlevel = {
  208. .name = SBP2_DEVICE_NAME,
  209. .host_reset = sbp2_host_reset,
  210. };
  211. static struct hpsb_address_ops sbp2_ops = {
  212. .write = sbp2_handle_status_write
  213. };
  214. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  215. static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
  216. u64, size_t, u16);
  217. static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
  218. size_t, u16);
  219. static struct hpsb_address_ops sbp2_physdma_ops = {
  220. .read = sbp2_handle_physdma_read,
  221. .write = sbp2_handle_physdma_write,
  222. };
  223. #endif
  224. /*
  225. * Interface to driver core and IEEE 1394 core
  226. */
  227. static struct ieee1394_device_id sbp2_id_table[] = {
  228. {
  229. .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
  230. .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
  231. .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
  232. {}
  233. };
  234. MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
  235. static int sbp2_probe(struct device *);
  236. static int sbp2_remove(struct device *);
  237. static int sbp2_update(struct unit_directory *);
  238. static struct hpsb_protocol_driver sbp2_driver = {
  239. .name = "SBP2 Driver",
  240. .id_table = sbp2_id_table,
  241. .update = sbp2_update,
  242. .driver = {
  243. .name = SBP2_DEVICE_NAME,
  244. .bus = &ieee1394_bus_type,
  245. .probe = sbp2_probe,
  246. .remove = sbp2_remove,
  247. },
  248. };
  249. /*
  250. * Interface to SCSI core
  251. */
  252. static int sbp2scsi_queuecommand(struct scsi_cmnd *,
  253. void (*)(struct scsi_cmnd *));
  254. static int sbp2scsi_abort(struct scsi_cmnd *);
  255. static int sbp2scsi_reset(struct scsi_cmnd *);
  256. static int sbp2scsi_slave_alloc(struct scsi_device *);
  257. static int sbp2scsi_slave_configure(struct scsi_device *);
  258. static void sbp2scsi_slave_destroy(struct scsi_device *);
  259. static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
  260. struct device_attribute *, char *);
  261. static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
  262. static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
  263. &dev_attr_ieee1394_id,
  264. NULL
  265. };
  266. static struct scsi_host_template sbp2_shost_template = {
  267. .module = THIS_MODULE,
  268. .name = "SBP-2 IEEE-1394",
  269. .proc_name = SBP2_DEVICE_NAME,
  270. .queuecommand = sbp2scsi_queuecommand,
  271. .eh_abort_handler = sbp2scsi_abort,
  272. .eh_device_reset_handler = sbp2scsi_reset,
  273. .slave_alloc = sbp2scsi_slave_alloc,
  274. .slave_configure = sbp2scsi_slave_configure,
  275. .slave_destroy = sbp2scsi_slave_destroy,
  276. .this_id = -1,
  277. .sg_tablesize = SG_ALL,
  278. .use_clustering = ENABLE_CLUSTERING,
  279. .cmd_per_lun = SBP2_MAX_CMDS,
  280. .can_queue = SBP2_MAX_CMDS,
  281. .emulated = 1,
  282. .sdev_attrs = sbp2_sysfs_sdev_attrs,
  283. };
  284. /*
  285. * List of devices with known bugs.
  286. *
  287. * The firmware_revision field, masked with 0xffff00, is the best indicator
  288. * for the type of bridge chip of a device. It yields a few false positives
  289. * but this did not break correctly behaving devices so far.
  290. */
  291. static const struct {
  292. u32 firmware_revision;
  293. u32 model_id;
  294. unsigned workarounds;
  295. } sbp2_workarounds_table[] = {
  296. /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
  297. .firmware_revision = 0x002800,
  298. .model_id = 0x001010,
  299. .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
  300. SBP2_WORKAROUND_MODE_SENSE_8,
  301. },
  302. /* Initio bridges, actually only needed for some older ones */ {
  303. .firmware_revision = 0x000200,
  304. .workarounds = SBP2_WORKAROUND_INQUIRY_36,
  305. },
  306. /* Symbios bridge */ {
  307. .firmware_revision = 0xa0b800,
  308. .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
  309. },
  310. /*
  311. * Note about the following Apple iPod blacklist entries:
  312. *
  313. * There are iPods (2nd gen, 3rd gen) with model_id==0. Since our
  314. * matching logic treats 0 as a wildcard, we cannot match this ID
  315. * without rewriting the matching routine. Fortunately these iPods
  316. * do not feature the read_capacity bug according to one report.
  317. * Read_capacity behaviour as well as model_id could change due to
  318. * Apple-supplied firmware updates though.
  319. */
  320. /* iPod 4th generation */ {
  321. .firmware_revision = 0x0a2700,
  322. .model_id = 0x000021,
  323. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  324. },
  325. /* iPod mini */ {
  326. .firmware_revision = 0x0a2700,
  327. .model_id = 0x000023,
  328. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  329. },
  330. /* iPod Photo */ {
  331. .firmware_revision = 0x0a2700,
  332. .model_id = 0x00007e,
  333. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  334. }
  335. };
  336. /**************************************
  337. * General utility functions
  338. **************************************/
  339. #ifndef __BIG_ENDIAN
  340. /*
  341. * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
  342. */
  343. static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
  344. {
  345. u32 *temp = buffer;
  346. for (length = (length >> 2); length--; )
  347. temp[length] = be32_to_cpu(temp[length]);
  348. }
  349. /*
  350. * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
  351. */
  352. static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
  353. {
  354. u32 *temp = buffer;
  355. for (length = (length >> 2); length--; )
  356. temp[length] = cpu_to_be32(temp[length]);
  357. }
  358. #else /* BIG_ENDIAN */
  359. /* Why waste the cpu cycles? */
  360. #define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
  361. #define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
  362. #endif
  363. static DECLARE_WAIT_QUEUE_HEAD(sbp2_access_wq);
  364. /*
  365. * Waits for completion of an SBP-2 access request.
  366. * Returns nonzero if timed out or prematurely interrupted.
  367. */
  368. static int sbp2util_access_timeout(struct sbp2_lu *lu, int timeout)
  369. {
  370. long leftover;
  371. leftover = wait_event_interruptible_timeout(
  372. sbp2_access_wq, lu->access_complete, timeout);
  373. lu->access_complete = 0;
  374. return leftover <= 0;
  375. }
  376. static void sbp2_free_packet(void *packet)
  377. {
  378. hpsb_free_tlabel(packet);
  379. hpsb_free_packet(packet);
  380. }
  381. /*
  382. * This is much like hpsb_node_write(), except it ignores the response
  383. * subaction and returns immediately. Can be used from atomic context.
  384. */
  385. static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
  386. quadlet_t *buf, size_t len)
  387. {
  388. struct hpsb_packet *packet;
  389. packet = hpsb_make_writepacket(ne->host, ne->nodeid, addr, buf, len);
  390. if (!packet)
  391. return -ENOMEM;
  392. hpsb_set_packet_complete_task(packet, sbp2_free_packet, packet);
  393. hpsb_node_fill_packet(ne, packet);
  394. if (hpsb_send_packet(packet) < 0) {
  395. sbp2_free_packet(packet);
  396. return -EIO;
  397. }
  398. return 0;
  399. }
  400. static void sbp2util_notify_fetch_agent(struct sbp2_lu *lu, u64 offset,
  401. quadlet_t *data, size_t len)
  402. {
  403. /* There is a small window after a bus reset within which the node
  404. * entry's generation is current but the reconnect wasn't completed. */
  405. if (unlikely(atomic_read(&lu->state) == SBP2LU_STATE_IN_RESET))
  406. return;
  407. if (hpsb_node_write(lu->ne, lu->command_block_agent_addr + offset,
  408. data, len))
  409. SBP2_ERR("sbp2util_notify_fetch_agent failed.");
  410. /* Now accept new SCSI commands, unless a bus reset happended during
  411. * hpsb_node_write. */
  412. if (likely(atomic_read(&lu->state) != SBP2LU_STATE_IN_RESET))
  413. scsi_unblock_requests(lu->shost);
  414. }
  415. static void sbp2util_write_orb_pointer(struct work_struct *work)
  416. {
  417. quadlet_t data[2];
  418. data[0] = ORB_SET_NODE_ID((container_of(work, struct sbp2_lu, protocol_work))->hi->host->node_id);
  419. data[1] = (container_of(work, struct sbp2_lu, protocol_work))->last_orb_dma;
  420. sbp2util_cpu_to_be32_buffer(data, 8);
  421. sbp2util_notify_fetch_agent(container_of(work, struct sbp2_lu, protocol_work), SBP2_ORB_POINTER_OFFSET, data, 8);
  422. }
  423. static void sbp2util_write_doorbell(struct work_struct *work)
  424. {
  425. sbp2util_notify_fetch_agent(container_of(work, struct sbp2_lu, protocol_work), SBP2_DOORBELL_OFFSET, NULL, 4);
  426. }
  427. static int sbp2util_create_command_orb_pool(struct sbp2_lu *lu)
  428. {
  429. struct sbp2_fwhost_info *hi = lu->hi;
  430. int i;
  431. unsigned long flags, orbs;
  432. struct sbp2_command_info *cmd;
  433. orbs = sbp2_serialize_io ? 2 : SBP2_MAX_CMDS;
  434. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  435. for (i = 0; i < orbs; i++) {
  436. cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
  437. if (!cmd) {
  438. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  439. return -ENOMEM;
  440. }
  441. cmd->command_orb_dma = pci_map_single(hi->host->pdev,
  442. &cmd->command_orb,
  443. sizeof(struct sbp2_command_orb),
  444. PCI_DMA_TODEVICE);
  445. cmd->sge_dma = pci_map_single(hi->host->pdev,
  446. &cmd->scatter_gather_element,
  447. sizeof(cmd->scatter_gather_element),
  448. PCI_DMA_BIDIRECTIONAL);
  449. INIT_LIST_HEAD(&cmd->list);
  450. list_add_tail(&cmd->list, &lu->cmd_orb_completed);
  451. }
  452. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  453. return 0;
  454. }
  455. static void sbp2util_remove_command_orb_pool(struct sbp2_lu *lu)
  456. {
  457. struct hpsb_host *host = lu->hi->host;
  458. struct list_head *lh, *next;
  459. struct sbp2_command_info *cmd;
  460. unsigned long flags;
  461. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  462. if (!list_empty(&lu->cmd_orb_completed))
  463. list_for_each_safe(lh, next, &lu->cmd_orb_completed) {
  464. cmd = list_entry(lh, struct sbp2_command_info, list);
  465. pci_unmap_single(host->pdev, cmd->command_orb_dma,
  466. sizeof(struct sbp2_command_orb),
  467. PCI_DMA_TODEVICE);
  468. pci_unmap_single(host->pdev, cmd->sge_dma,
  469. sizeof(cmd->scatter_gather_element),
  470. PCI_DMA_BIDIRECTIONAL);
  471. kfree(cmd);
  472. }
  473. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  474. return;
  475. }
  476. /*
  477. * Finds the sbp2_command for a given outstanding command ORB.
  478. * Only looks at the in-use list.
  479. */
  480. static struct sbp2_command_info *sbp2util_find_command_for_orb(
  481. struct sbp2_lu *lu, dma_addr_t orb)
  482. {
  483. struct sbp2_command_info *cmd;
  484. unsigned long flags;
  485. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  486. if (!list_empty(&lu->cmd_orb_inuse))
  487. list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
  488. if (cmd->command_orb_dma == orb) {
  489. spin_unlock_irqrestore(
  490. &lu->cmd_orb_lock, flags);
  491. return cmd;
  492. }
  493. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  494. return NULL;
  495. }
  496. /*
  497. * Finds the sbp2_command for a given outstanding SCpnt.
  498. * Only looks at the in-use list.
  499. * Must be called with lu->cmd_orb_lock held.
  500. */
  501. static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
  502. struct sbp2_lu *lu, void *SCpnt)
  503. {
  504. struct sbp2_command_info *cmd;
  505. if (!list_empty(&lu->cmd_orb_inuse))
  506. list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
  507. if (cmd->Current_SCpnt == SCpnt)
  508. return cmd;
  509. return NULL;
  510. }
  511. static struct sbp2_command_info *sbp2util_allocate_command_orb(
  512. struct sbp2_lu *lu,
  513. struct scsi_cmnd *Current_SCpnt,
  514. void (*Current_done)(struct scsi_cmnd *))
  515. {
  516. struct list_head *lh;
  517. struct sbp2_command_info *cmd = NULL;
  518. unsigned long flags;
  519. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  520. if (!list_empty(&lu->cmd_orb_completed)) {
  521. lh = lu->cmd_orb_completed.next;
  522. list_del(lh);
  523. cmd = list_entry(lh, struct sbp2_command_info, list);
  524. cmd->Current_done = Current_done;
  525. cmd->Current_SCpnt = Current_SCpnt;
  526. list_add_tail(&cmd->list, &lu->cmd_orb_inuse);
  527. } else
  528. SBP2_ERR("%s: no orbs available", __FUNCTION__);
  529. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  530. return cmd;
  531. }
  532. static void sbp2util_free_command_dma(struct sbp2_command_info *cmd)
  533. {
  534. struct sbp2_lu *lu = (struct sbp2_lu *)
  535. cmd->Current_SCpnt->device->host->hostdata[0];
  536. struct hpsb_host *host;
  537. if (!lu) {
  538. SBP2_ERR("%s: lu == NULL", __FUNCTION__);
  539. return;
  540. }
  541. host = lu->ud->ne->host;
  542. if (cmd->cmd_dma) {
  543. if (cmd->dma_type == CMD_DMA_SINGLE)
  544. pci_unmap_single(host->pdev, cmd->cmd_dma,
  545. cmd->dma_size, cmd->dma_dir);
  546. else if (cmd->dma_type == CMD_DMA_PAGE)
  547. pci_unmap_page(host->pdev, cmd->cmd_dma,
  548. cmd->dma_size, cmd->dma_dir);
  549. /* XXX: Check for CMD_DMA_NONE bug */
  550. cmd->dma_type = CMD_DMA_NONE;
  551. cmd->cmd_dma = 0;
  552. }
  553. if (cmd->sge_buffer) {
  554. pci_unmap_sg(host->pdev, cmd->sge_buffer,
  555. cmd->dma_size, cmd->dma_dir);
  556. cmd->sge_buffer = NULL;
  557. }
  558. }
  559. /*
  560. * This function moves a command to the completed orb list.
  561. * Must be called with lu->cmd_orb_lock held.
  562. */
  563. static void sbp2util_mark_command_completed(
  564. struct sbp2_lu *lu,
  565. struct sbp2_command_info *cmd)
  566. {
  567. sbp2util_free_command_dma(cmd);
  568. list_move_tail(&cmd->list, &lu->cmd_orb_completed);
  569. }
  570. /*
  571. * Is lu valid? Is the 1394 node still present?
  572. */
  573. static inline int sbp2util_node_is_available(struct sbp2_lu *lu)
  574. {
  575. return lu && lu->ne && !lu->ne->in_limbo;
  576. }
  577. /*********************************************
  578. * IEEE-1394 core driver stack related section
  579. *********************************************/
  580. static int sbp2_probe(struct device *dev)
  581. {
  582. struct unit_directory *ud;
  583. struct sbp2_lu *lu;
  584. ud = container_of(dev, struct unit_directory, device);
  585. /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
  586. * instead. */
  587. if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
  588. return -ENODEV;
  589. lu = sbp2_alloc_device(ud);
  590. if (!lu)
  591. return -ENOMEM;
  592. sbp2_parse_unit_directory(lu, ud);
  593. return sbp2_start_device(lu);
  594. }
  595. static int sbp2_remove(struct device *dev)
  596. {
  597. struct unit_directory *ud;
  598. struct sbp2_lu *lu;
  599. struct scsi_device *sdev;
  600. ud = container_of(dev, struct unit_directory, device);
  601. lu = ud->device.driver_data;
  602. if (!lu)
  603. return 0;
  604. if (lu->shost) {
  605. /* Get rid of enqueued commands if there is no chance to
  606. * send them. */
  607. if (!sbp2util_node_is_available(lu))
  608. sbp2scsi_complete_all_commands(lu, DID_NO_CONNECT);
  609. /* scsi_remove_device() may trigger shutdown functions of SCSI
  610. * highlevel drivers which would deadlock if blocked. */
  611. atomic_set(&lu->state, SBP2LU_STATE_IN_SHUTDOWN);
  612. scsi_unblock_requests(lu->shost);
  613. }
  614. sdev = lu->sdev;
  615. if (sdev) {
  616. lu->sdev = NULL;
  617. scsi_remove_device(sdev);
  618. }
  619. sbp2_logout_device(lu);
  620. sbp2_remove_device(lu);
  621. return 0;
  622. }
  623. static int sbp2_update(struct unit_directory *ud)
  624. {
  625. struct sbp2_lu *lu = ud->device.driver_data;
  626. if (sbp2_reconnect_device(lu)) {
  627. /* Reconnect has failed. Perhaps we didn't reconnect fast
  628. * enough. Try a regular login, but first log out just in
  629. * case of any weirdness. */
  630. sbp2_logout_device(lu);
  631. if (sbp2_login_device(lu)) {
  632. /* Login failed too, just fail, and the backend
  633. * will call our sbp2_remove for us */
  634. SBP2_ERR("Failed to reconnect to sbp2 device!");
  635. return -EBUSY;
  636. }
  637. }
  638. sbp2_set_busy_timeout(lu);
  639. sbp2_agent_reset(lu, 1);
  640. sbp2_max_speed_and_size(lu);
  641. /* Complete any pending commands with busy (so they get retried)
  642. * and remove them from our queue. */
  643. sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
  644. /* Accept new commands unless there was another bus reset in the
  645. * meantime. */
  646. if (hpsb_node_entry_valid(lu->ne)) {
  647. atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
  648. scsi_unblock_requests(lu->shost);
  649. }
  650. return 0;
  651. }
  652. static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *ud)
  653. {
  654. struct sbp2_fwhost_info *hi;
  655. struct Scsi_Host *shost = NULL;
  656. struct sbp2_lu *lu = NULL;
  657. lu = kzalloc(sizeof(*lu), GFP_KERNEL);
  658. if (!lu) {
  659. SBP2_ERR("failed to create lu");
  660. goto failed_alloc;
  661. }
  662. lu->ne = ud->ne;
  663. lu->ud = ud;
  664. lu->speed_code = IEEE1394_SPEED_100;
  665. lu->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
  666. lu->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
  667. INIT_LIST_HEAD(&lu->cmd_orb_inuse);
  668. INIT_LIST_HEAD(&lu->cmd_orb_completed);
  669. INIT_LIST_HEAD(&lu->lu_list);
  670. spin_lock_init(&lu->cmd_orb_lock);
  671. atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
  672. INIT_WORK(&lu->protocol_work, NULL);
  673. ud->device.driver_data = lu;
  674. hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
  675. if (!hi) {
  676. hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host,
  677. sizeof(*hi));
  678. if (!hi) {
  679. SBP2_ERR("failed to allocate hostinfo");
  680. goto failed_alloc;
  681. }
  682. hi->host = ud->ne->host;
  683. INIT_LIST_HEAD(&hi->logical_units);
  684. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  685. /* Handle data movement if physical dma is not
  686. * enabled or not supported on host controller */
  687. if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
  688. &sbp2_physdma_ops,
  689. 0x0ULL, 0xfffffffcULL)) {
  690. SBP2_ERR("failed to register lower 4GB address range");
  691. goto failed_alloc;
  692. }
  693. #endif
  694. }
  695. /* Prevent unloading of the 1394 host */
  696. if (!try_module_get(hi->host->driver->owner)) {
  697. SBP2_ERR("failed to get a reference on 1394 host driver");
  698. goto failed_alloc;
  699. }
  700. lu->hi = hi;
  701. list_add_tail(&lu->lu_list, &hi->logical_units);
  702. /* Register the status FIFO address range. We could use the same FIFO
  703. * for targets at different nodes. However we need different FIFOs per
  704. * target in order to support multi-unit devices.
  705. * The FIFO is located out of the local host controller's physical range
  706. * but, if possible, within the posted write area. Status writes will
  707. * then be performed as unified transactions. This slightly reduces
  708. * bandwidth usage, and some Prolific based devices seem to require it.
  709. */
  710. lu->status_fifo_addr = hpsb_allocate_and_register_addrspace(
  711. &sbp2_highlevel, ud->ne->host, &sbp2_ops,
  712. sizeof(struct sbp2_status_block), sizeof(quadlet_t),
  713. ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
  714. if (lu->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
  715. SBP2_ERR("failed to allocate status FIFO address range");
  716. goto failed_alloc;
  717. }
  718. shost = scsi_host_alloc(&sbp2_shost_template, sizeof(unsigned long));
  719. if (!shost) {
  720. SBP2_ERR("failed to register scsi host");
  721. goto failed_alloc;
  722. }
  723. shost->hostdata[0] = (unsigned long)lu;
  724. if (!scsi_add_host(shost, &ud->device)) {
  725. lu->shost = shost;
  726. return lu;
  727. }
  728. SBP2_ERR("failed to add scsi host");
  729. scsi_host_put(shost);
  730. failed_alloc:
  731. sbp2_remove_device(lu);
  732. return NULL;
  733. }
  734. static void sbp2_host_reset(struct hpsb_host *host)
  735. {
  736. struct sbp2_fwhost_info *hi;
  737. struct sbp2_lu *lu;
  738. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  739. if (!hi)
  740. return;
  741. list_for_each_entry(lu, &hi->logical_units, lu_list)
  742. if (likely(atomic_read(&lu->state) !=
  743. SBP2LU_STATE_IN_SHUTDOWN)) {
  744. atomic_set(&lu->state, SBP2LU_STATE_IN_RESET);
  745. scsi_block_requests(lu->shost);
  746. }
  747. }
  748. static int sbp2_start_device(struct sbp2_lu *lu)
  749. {
  750. struct sbp2_fwhost_info *hi = lu->hi;
  751. int error;
  752. lu->login_response = pci_alloc_consistent(hi->host->pdev,
  753. sizeof(struct sbp2_login_response),
  754. &lu->login_response_dma);
  755. if (!lu->login_response)
  756. goto alloc_fail;
  757. lu->query_logins_orb = pci_alloc_consistent(hi->host->pdev,
  758. sizeof(struct sbp2_query_logins_orb),
  759. &lu->query_logins_orb_dma);
  760. if (!lu->query_logins_orb)
  761. goto alloc_fail;
  762. lu->query_logins_response = pci_alloc_consistent(hi->host->pdev,
  763. sizeof(struct sbp2_query_logins_response),
  764. &lu->query_logins_response_dma);
  765. if (!lu->query_logins_response)
  766. goto alloc_fail;
  767. lu->reconnect_orb = pci_alloc_consistent(hi->host->pdev,
  768. sizeof(struct sbp2_reconnect_orb),
  769. &lu->reconnect_orb_dma);
  770. if (!lu->reconnect_orb)
  771. goto alloc_fail;
  772. lu->logout_orb = pci_alloc_consistent(hi->host->pdev,
  773. sizeof(struct sbp2_logout_orb),
  774. &lu->logout_orb_dma);
  775. if (!lu->logout_orb)
  776. goto alloc_fail;
  777. lu->login_orb = pci_alloc_consistent(hi->host->pdev,
  778. sizeof(struct sbp2_login_orb),
  779. &lu->login_orb_dma);
  780. if (!lu->login_orb)
  781. goto alloc_fail;
  782. if (sbp2util_create_command_orb_pool(lu)) {
  783. SBP2_ERR("sbp2util_create_command_orb_pool failed!");
  784. sbp2_remove_device(lu);
  785. return -ENOMEM;
  786. }
  787. /* Wait a second before trying to log in. Previously logged in
  788. * initiators need a chance to reconnect. */
  789. if (msleep_interruptible(1000)) {
  790. sbp2_remove_device(lu);
  791. return -EINTR;
  792. }
  793. if (sbp2_login_device(lu)) {
  794. sbp2_remove_device(lu);
  795. return -EBUSY;
  796. }
  797. sbp2_set_busy_timeout(lu);
  798. sbp2_agent_reset(lu, 1);
  799. sbp2_max_speed_and_size(lu);
  800. error = scsi_add_device(lu->shost, 0, lu->ud->id, 0);
  801. if (error) {
  802. SBP2_ERR("scsi_add_device failed");
  803. sbp2_logout_device(lu);
  804. sbp2_remove_device(lu);
  805. return error;
  806. }
  807. return 0;
  808. alloc_fail:
  809. SBP2_ERR("Could not allocate memory for lu");
  810. sbp2_remove_device(lu);
  811. return -ENOMEM;
  812. }
  813. static void sbp2_remove_device(struct sbp2_lu *lu)
  814. {
  815. struct sbp2_fwhost_info *hi;
  816. if (!lu)
  817. return;
  818. hi = lu->hi;
  819. if (lu->shost) {
  820. scsi_remove_host(lu->shost);
  821. scsi_host_put(lu->shost);
  822. }
  823. flush_scheduled_work();
  824. sbp2util_remove_command_orb_pool(lu);
  825. list_del(&lu->lu_list);
  826. if (lu->login_response)
  827. pci_free_consistent(hi->host->pdev,
  828. sizeof(struct sbp2_login_response),
  829. lu->login_response,
  830. lu->login_response_dma);
  831. if (lu->login_orb)
  832. pci_free_consistent(hi->host->pdev,
  833. sizeof(struct sbp2_login_orb),
  834. lu->login_orb,
  835. lu->login_orb_dma);
  836. if (lu->reconnect_orb)
  837. pci_free_consistent(hi->host->pdev,
  838. sizeof(struct sbp2_reconnect_orb),
  839. lu->reconnect_orb,
  840. lu->reconnect_orb_dma);
  841. if (lu->logout_orb)
  842. pci_free_consistent(hi->host->pdev,
  843. sizeof(struct sbp2_logout_orb),
  844. lu->logout_orb,
  845. lu->logout_orb_dma);
  846. if (lu->query_logins_orb)
  847. pci_free_consistent(hi->host->pdev,
  848. sizeof(struct sbp2_query_logins_orb),
  849. lu->query_logins_orb,
  850. lu->query_logins_orb_dma);
  851. if (lu->query_logins_response)
  852. pci_free_consistent(hi->host->pdev,
  853. sizeof(struct sbp2_query_logins_response),
  854. lu->query_logins_response,
  855. lu->query_logins_response_dma);
  856. if (lu->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
  857. hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
  858. lu->status_fifo_addr);
  859. lu->ud->device.driver_data = NULL;
  860. if (hi)
  861. module_put(hi->host->driver->owner);
  862. kfree(lu);
  863. }
  864. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  865. /*
  866. * Deal with write requests on adapters which do not support physical DMA or
  867. * have it switched off.
  868. */
  869. static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
  870. int destid, quadlet_t *data, u64 addr,
  871. size_t length, u16 flags)
  872. {
  873. memcpy(bus_to_virt((u32) addr), data, length);
  874. return RCODE_COMPLETE;
  875. }
  876. /*
  877. * Deal with read requests on adapters which do not support physical DMA or
  878. * have it switched off.
  879. */
  880. static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
  881. quadlet_t *data, u64 addr, size_t length,
  882. u16 flags)
  883. {
  884. memcpy(data, bus_to_virt((u32) addr), length);
  885. return RCODE_COMPLETE;
  886. }
  887. #endif
  888. /**************************************
  889. * SBP-2 protocol related section
  890. **************************************/
  891. static int sbp2_query_logins(struct sbp2_lu *lu)
  892. {
  893. struct sbp2_fwhost_info *hi = lu->hi;
  894. quadlet_t data[2];
  895. int max_logins;
  896. int active_logins;
  897. lu->query_logins_orb->reserved1 = 0x0;
  898. lu->query_logins_orb->reserved2 = 0x0;
  899. lu->query_logins_orb->query_response_lo = lu->query_logins_response_dma;
  900. lu->query_logins_orb->query_response_hi =
  901. ORB_SET_NODE_ID(hi->host->node_id);
  902. lu->query_logins_orb->lun_misc =
  903. ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
  904. lu->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
  905. lu->query_logins_orb->lun_misc |= ORB_SET_LUN(lu->lun);
  906. lu->query_logins_orb->reserved_resp_length =
  907. ORB_SET_QUERY_LOGINS_RESP_LENGTH(
  908. sizeof(struct sbp2_query_logins_response));
  909. lu->query_logins_orb->status_fifo_hi =
  910. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  911. lu->query_logins_orb->status_fifo_lo =
  912. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  913. sbp2util_cpu_to_be32_buffer(lu->query_logins_orb,
  914. sizeof(struct sbp2_query_logins_orb));
  915. memset(lu->query_logins_response, 0,
  916. sizeof(struct sbp2_query_logins_response));
  917. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  918. data[1] = lu->query_logins_orb_dma;
  919. sbp2util_cpu_to_be32_buffer(data, 8);
  920. hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  921. if (sbp2util_access_timeout(lu, 2*HZ)) {
  922. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  923. return -EIO;
  924. }
  925. if (lu->status_block.ORB_offset_lo != lu->query_logins_orb_dma) {
  926. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  927. return -EIO;
  928. }
  929. if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
  930. SBP2_INFO("Error querying logins to SBP-2 device - failed");
  931. return -EIO;
  932. }
  933. sbp2util_cpu_to_be32_buffer(lu->query_logins_response,
  934. sizeof(struct sbp2_query_logins_response));
  935. max_logins = RESPONSE_GET_MAX_LOGINS(
  936. lu->query_logins_response->length_max_logins);
  937. SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
  938. active_logins = RESPONSE_GET_ACTIVE_LOGINS(
  939. lu->query_logins_response->length_max_logins);
  940. SBP2_INFO("Number of active logins: %d", active_logins);
  941. if (active_logins >= max_logins) {
  942. return -EIO;
  943. }
  944. return 0;
  945. }
  946. static int sbp2_login_device(struct sbp2_lu *lu)
  947. {
  948. struct sbp2_fwhost_info *hi = lu->hi;
  949. quadlet_t data[2];
  950. if (!lu->login_orb)
  951. return -EIO;
  952. if (!sbp2_exclusive_login && sbp2_query_logins(lu)) {
  953. SBP2_INFO("Device does not support any more concurrent logins");
  954. return -EIO;
  955. }
  956. /* assume no password */
  957. lu->login_orb->password_hi = 0;
  958. lu->login_orb->password_lo = 0;
  959. lu->login_orb->login_response_lo = lu->login_response_dma;
  960. lu->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
  961. lu->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
  962. /* one second reconnect time */
  963. lu->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
  964. lu->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
  965. lu->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
  966. lu->login_orb->lun_misc |= ORB_SET_LUN(lu->lun);
  967. lu->login_orb->passwd_resp_lengths =
  968. ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
  969. lu->login_orb->status_fifo_hi =
  970. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  971. lu->login_orb->status_fifo_lo =
  972. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  973. sbp2util_cpu_to_be32_buffer(lu->login_orb,
  974. sizeof(struct sbp2_login_orb));
  975. memset(lu->login_response, 0, sizeof(struct sbp2_login_response));
  976. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  977. data[1] = lu->login_orb_dma;
  978. sbp2util_cpu_to_be32_buffer(data, 8);
  979. hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  980. /* wait up to 20 seconds for login status */
  981. if (sbp2util_access_timeout(lu, 20*HZ)) {
  982. SBP2_ERR("Error logging into SBP-2 device - timed out");
  983. return -EIO;
  984. }
  985. /* make sure that the returned status matches the login ORB */
  986. if (lu->status_block.ORB_offset_lo != lu->login_orb_dma) {
  987. SBP2_ERR("Error logging into SBP-2 device - timed out");
  988. return -EIO;
  989. }
  990. if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
  991. SBP2_ERR("Error logging into SBP-2 device - failed");
  992. return -EIO;
  993. }
  994. sbp2util_cpu_to_be32_buffer(lu->login_response,
  995. sizeof(struct sbp2_login_response));
  996. lu->command_block_agent_addr =
  997. ((u64)lu->login_response->command_block_agent_hi) << 32;
  998. lu->command_block_agent_addr |=
  999. ((u64)lu->login_response->command_block_agent_lo);
  1000. lu->command_block_agent_addr &= 0x0000ffffffffffffULL;
  1001. SBP2_INFO("Logged into SBP-2 device");
  1002. return 0;
  1003. }
  1004. static int sbp2_logout_device(struct sbp2_lu *lu)
  1005. {
  1006. struct sbp2_fwhost_info *hi = lu->hi;
  1007. quadlet_t data[2];
  1008. int error;
  1009. lu->logout_orb->reserved1 = 0x0;
  1010. lu->logout_orb->reserved2 = 0x0;
  1011. lu->logout_orb->reserved3 = 0x0;
  1012. lu->logout_orb->reserved4 = 0x0;
  1013. lu->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
  1014. lu->logout_orb->login_ID_misc |=
  1015. ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
  1016. lu->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1017. lu->logout_orb->reserved5 = 0x0;
  1018. lu->logout_orb->status_fifo_hi =
  1019. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  1020. lu->logout_orb->status_fifo_lo =
  1021. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  1022. sbp2util_cpu_to_be32_buffer(lu->logout_orb,
  1023. sizeof(struct sbp2_logout_orb));
  1024. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1025. data[1] = lu->logout_orb_dma;
  1026. sbp2util_cpu_to_be32_buffer(data, 8);
  1027. error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  1028. if (error)
  1029. return error;
  1030. /* wait up to 1 second for the device to complete logout */
  1031. if (sbp2util_access_timeout(lu, HZ))
  1032. return -EIO;
  1033. SBP2_INFO("Logged out of SBP-2 device");
  1034. return 0;
  1035. }
  1036. static int sbp2_reconnect_device(struct sbp2_lu *lu)
  1037. {
  1038. struct sbp2_fwhost_info *hi = lu->hi;
  1039. quadlet_t data[2];
  1040. int error;
  1041. lu->reconnect_orb->reserved1 = 0x0;
  1042. lu->reconnect_orb->reserved2 = 0x0;
  1043. lu->reconnect_orb->reserved3 = 0x0;
  1044. lu->reconnect_orb->reserved4 = 0x0;
  1045. lu->reconnect_orb->login_ID_misc =
  1046. ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
  1047. lu->reconnect_orb->login_ID_misc |=
  1048. ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
  1049. lu->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1050. lu->reconnect_orb->reserved5 = 0x0;
  1051. lu->reconnect_orb->status_fifo_hi =
  1052. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  1053. lu->reconnect_orb->status_fifo_lo =
  1054. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  1055. sbp2util_cpu_to_be32_buffer(lu->reconnect_orb,
  1056. sizeof(struct sbp2_reconnect_orb));
  1057. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1058. data[1] = lu->reconnect_orb_dma;
  1059. sbp2util_cpu_to_be32_buffer(data, 8);
  1060. error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  1061. if (error)
  1062. return error;
  1063. /* wait up to 1 second for reconnect status */
  1064. if (sbp2util_access_timeout(lu, HZ)) {
  1065. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1066. return -EIO;
  1067. }
  1068. /* make sure that the returned status matches the reconnect ORB */
  1069. if (lu->status_block.ORB_offset_lo != lu->reconnect_orb_dma) {
  1070. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1071. return -EIO;
  1072. }
  1073. if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
  1074. SBP2_ERR("Error reconnecting to SBP-2 device - failed");
  1075. return -EIO;
  1076. }
  1077. SBP2_INFO("Reconnected to SBP-2 device");
  1078. return 0;
  1079. }
  1080. /*
  1081. * Set the target node's Single Phase Retry limit. Affects the target's retry
  1082. * behaviour if our node is too busy to accept requests.
  1083. */
  1084. static int sbp2_set_busy_timeout(struct sbp2_lu *lu)
  1085. {
  1086. quadlet_t data;
  1087. data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
  1088. if (hpsb_node_write(lu->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
  1089. SBP2_ERR("%s error", __FUNCTION__);
  1090. return 0;
  1091. }
  1092. static void sbp2_parse_unit_directory(struct sbp2_lu *lu,
  1093. struct unit_directory *ud)
  1094. {
  1095. struct csr1212_keyval *kv;
  1096. struct csr1212_dentry *dentry;
  1097. u64 management_agent_addr;
  1098. u32 unit_characteristics, firmware_revision;
  1099. unsigned workarounds;
  1100. int i;
  1101. management_agent_addr = 0;
  1102. unit_characteristics = 0;
  1103. firmware_revision = 0;
  1104. csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
  1105. switch (kv->key.id) {
  1106. case CSR1212_KV_ID_DEPENDENT_INFO:
  1107. if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
  1108. management_agent_addr =
  1109. CSR1212_REGISTER_SPACE_BASE +
  1110. (kv->value.csr_offset << 2);
  1111. else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
  1112. lu->lun = ORB_SET_LUN(kv->value.immediate);
  1113. break;
  1114. case SBP2_UNIT_CHARACTERISTICS_KEY:
  1115. /* FIXME: This is ignored so far.
  1116. * See SBP-2 clause 7.4.8. */
  1117. unit_characteristics = kv->value.immediate;
  1118. break;
  1119. case SBP2_FIRMWARE_REVISION_KEY:
  1120. firmware_revision = kv->value.immediate;
  1121. break;
  1122. default:
  1123. /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
  1124. * Its "ordered" bit has consequences for command ORB
  1125. * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
  1126. break;
  1127. }
  1128. }
  1129. workarounds = sbp2_default_workarounds;
  1130. if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
  1131. for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
  1132. if (sbp2_workarounds_table[i].firmware_revision &&
  1133. sbp2_workarounds_table[i].firmware_revision !=
  1134. (firmware_revision & 0xffff00))
  1135. continue;
  1136. if (sbp2_workarounds_table[i].model_id &&
  1137. sbp2_workarounds_table[i].model_id != ud->model_id)
  1138. continue;
  1139. workarounds |= sbp2_workarounds_table[i].workarounds;
  1140. break;
  1141. }
  1142. if (workarounds)
  1143. SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
  1144. "(firmware_revision 0x%06x, vendor_id 0x%06x,"
  1145. " model_id 0x%06x)",
  1146. NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
  1147. workarounds, firmware_revision,
  1148. ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
  1149. ud->model_id);
  1150. /* We would need one SCSI host template for each target to adjust
  1151. * max_sectors on the fly, therefore warn only. */
  1152. if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
  1153. (sbp2_max_sectors * 512) > (128 * 1024))
  1154. SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
  1155. "max transfer size. WARNING: Current max_sectors "
  1156. "setting is larger than 128KB (%d sectors)",
  1157. NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
  1158. sbp2_max_sectors);
  1159. /* If this is a logical unit directory entry, process the parent
  1160. * to get the values. */
  1161. if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
  1162. struct unit_directory *parent_ud = container_of(
  1163. ud->device.parent, struct unit_directory, device);
  1164. sbp2_parse_unit_directory(lu, parent_ud);
  1165. } else {
  1166. lu->management_agent_addr = management_agent_addr;
  1167. lu->workarounds = workarounds;
  1168. if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
  1169. lu->lun = ORB_SET_LUN(ud->lun);
  1170. }
  1171. }
  1172. #define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
  1173. /*
  1174. * This function is called in order to determine the max speed and packet
  1175. * size we can use in our ORBs. Note, that we (the driver and host) only
  1176. * initiate the transaction. The SBP-2 device actually transfers the data
  1177. * (by reading from the DMA area we tell it). This means that the SBP-2
  1178. * device decides the actual maximum data it can transfer. We just tell it
  1179. * the speed that it needs to use, and the max_rec the host supports, and
  1180. * it takes care of the rest.
  1181. */
  1182. static int sbp2_max_speed_and_size(struct sbp2_lu *lu)
  1183. {
  1184. struct sbp2_fwhost_info *hi = lu->hi;
  1185. u8 payload;
  1186. lu->speed_code = hi->host->speed[NODEID_TO_NODE(lu->ne->nodeid)];
  1187. if (lu->speed_code > sbp2_max_speed) {
  1188. lu->speed_code = sbp2_max_speed;
  1189. SBP2_INFO("Reducing speed to %s",
  1190. hpsb_speedto_str[sbp2_max_speed]);
  1191. }
  1192. /* Payload size is the lesser of what our speed supports and what
  1193. * our host supports. */
  1194. payload = min(sbp2_speedto_max_payload[lu->speed_code],
  1195. (u8) (hi->host->csr.max_rec - 1));
  1196. /* If physical DMA is off, work around limitation in ohci1394:
  1197. * packet size must not exceed PAGE_SIZE */
  1198. if (lu->ne->host->low_addr_space < (1ULL << 32))
  1199. while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
  1200. payload)
  1201. payload--;
  1202. SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
  1203. NODE_BUS_ARGS(hi->host, lu->ne->nodeid),
  1204. hpsb_speedto_str[lu->speed_code],
  1205. SBP2_PAYLOAD_TO_BYTES(payload));
  1206. lu->max_payload_size = payload;
  1207. return 0;
  1208. }
  1209. static int sbp2_agent_reset(struct sbp2_lu *lu, int wait)
  1210. {
  1211. quadlet_t data;
  1212. u64 addr;
  1213. int retval;
  1214. unsigned long flags;
  1215. /* cancel_delayed_work(&lu->protocol_work); */
  1216. if (wait)
  1217. flush_scheduled_work();
  1218. data = ntohl(SBP2_AGENT_RESET_DATA);
  1219. addr = lu->command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
  1220. if (wait)
  1221. retval = hpsb_node_write(lu->ne, addr, &data, 4);
  1222. else
  1223. retval = sbp2util_node_write_no_wait(lu->ne, addr, &data, 4);
  1224. if (retval < 0) {
  1225. SBP2_ERR("hpsb_node_write failed.\n");
  1226. return -EIO;
  1227. }
  1228. /* make sure that the ORB_POINTER is written on next command */
  1229. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1230. lu->last_orb = NULL;
  1231. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1232. return 0;
  1233. }
  1234. static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
  1235. struct sbp2_fwhost_info *hi,
  1236. struct sbp2_command_info *cmd,
  1237. unsigned int scsi_use_sg,
  1238. struct scatterlist *sgpnt,
  1239. u32 orb_direction,
  1240. enum dma_data_direction dma_dir)
  1241. {
  1242. cmd->dma_dir = dma_dir;
  1243. orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1244. orb->misc |= ORB_SET_DIRECTION(orb_direction);
  1245. /* special case if only one element (and less than 64KB in size) */
  1246. if ((scsi_use_sg == 1) &&
  1247. (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
  1248. cmd->dma_size = sgpnt[0].length;
  1249. cmd->dma_type = CMD_DMA_PAGE;
  1250. cmd->cmd_dma = pci_map_page(hi->host->pdev,
  1251. sgpnt[0].page, sgpnt[0].offset,
  1252. cmd->dma_size, cmd->dma_dir);
  1253. orb->data_descriptor_lo = cmd->cmd_dma;
  1254. orb->misc |= ORB_SET_DATA_SIZE(cmd->dma_size);
  1255. } else {
  1256. struct sbp2_unrestricted_page_table *sg_element =
  1257. &cmd->scatter_gather_element[0];
  1258. u32 sg_count, sg_len;
  1259. dma_addr_t sg_addr;
  1260. int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
  1261. dma_dir);
  1262. cmd->dma_size = scsi_use_sg;
  1263. cmd->sge_buffer = sgpnt;
  1264. /* use page tables (s/g) */
  1265. orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
  1266. orb->data_descriptor_lo = cmd->sge_dma;
  1267. /* loop through and fill out our SBP-2 page tables
  1268. * (and split up anything too large) */
  1269. for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
  1270. sg_len = sg_dma_len(sgpnt);
  1271. sg_addr = sg_dma_address(sgpnt);
  1272. while (sg_len) {
  1273. sg_element[sg_count].segment_base_lo = sg_addr;
  1274. if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
  1275. sg_element[sg_count].length_segment_base_hi =
  1276. PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
  1277. sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
  1278. sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
  1279. } else {
  1280. sg_element[sg_count].length_segment_base_hi =
  1281. PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
  1282. sg_len = 0;
  1283. }
  1284. sg_count++;
  1285. }
  1286. }
  1287. orb->misc |= ORB_SET_DATA_SIZE(sg_count);
  1288. sbp2util_cpu_to_be32_buffer(sg_element,
  1289. (sizeof(struct sbp2_unrestricted_page_table)) *
  1290. sg_count);
  1291. }
  1292. }
  1293. static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
  1294. struct sbp2_fwhost_info *hi,
  1295. struct sbp2_command_info *cmd,
  1296. struct scatterlist *sgpnt,
  1297. u32 orb_direction,
  1298. unsigned int scsi_request_bufflen,
  1299. void *scsi_request_buffer,
  1300. enum dma_data_direction dma_dir)
  1301. {
  1302. cmd->dma_dir = dma_dir;
  1303. cmd->dma_size = scsi_request_bufflen;
  1304. cmd->dma_type = CMD_DMA_SINGLE;
  1305. cmd->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
  1306. cmd->dma_size, cmd->dma_dir);
  1307. orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1308. orb->misc |= ORB_SET_DIRECTION(orb_direction);
  1309. /* handle case where we get a command w/o s/g enabled
  1310. * (but check for transfers larger than 64K) */
  1311. if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
  1312. orb->data_descriptor_lo = cmd->cmd_dma;
  1313. orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
  1314. } else {
  1315. /* The buffer is too large. Turn this into page tables. */
  1316. struct sbp2_unrestricted_page_table *sg_element =
  1317. &cmd->scatter_gather_element[0];
  1318. u32 sg_count, sg_len;
  1319. dma_addr_t sg_addr;
  1320. orb->data_descriptor_lo = cmd->sge_dma;
  1321. orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
  1322. /* fill out our SBP-2 page tables; split up the large buffer */
  1323. sg_count = 0;
  1324. sg_len = scsi_request_bufflen;
  1325. sg_addr = cmd->cmd_dma;
  1326. while (sg_len) {
  1327. sg_element[sg_count].segment_base_lo = sg_addr;
  1328. if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
  1329. sg_element[sg_count].length_segment_base_hi =
  1330. PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
  1331. sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
  1332. sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
  1333. } else {
  1334. sg_element[sg_count].length_segment_base_hi =
  1335. PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
  1336. sg_len = 0;
  1337. }
  1338. sg_count++;
  1339. }
  1340. orb->misc |= ORB_SET_DATA_SIZE(sg_count);
  1341. sbp2util_cpu_to_be32_buffer(sg_element,
  1342. (sizeof(struct sbp2_unrestricted_page_table)) *
  1343. sg_count);
  1344. }
  1345. }
  1346. static void sbp2_create_command_orb(struct sbp2_lu *lu,
  1347. struct sbp2_command_info *cmd,
  1348. unchar *scsi_cmd,
  1349. unsigned int scsi_use_sg,
  1350. unsigned int scsi_request_bufflen,
  1351. void *scsi_request_buffer,
  1352. enum dma_data_direction dma_dir)
  1353. {
  1354. struct sbp2_fwhost_info *hi = lu->hi;
  1355. struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
  1356. struct sbp2_command_orb *orb = &cmd->command_orb;
  1357. u32 orb_direction;
  1358. /*
  1359. * Set-up our command ORB.
  1360. *
  1361. * NOTE: We're doing unrestricted page tables (s/g), as this is
  1362. * best performance (at least with the devices I have). This means
  1363. * that data_size becomes the number of s/g elements, and
  1364. * page_size should be zero (for unrestricted).
  1365. */
  1366. orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
  1367. orb->next_ORB_lo = 0x0;
  1368. orb->misc = ORB_SET_MAX_PAYLOAD(lu->max_payload_size);
  1369. orb->misc |= ORB_SET_SPEED(lu->speed_code);
  1370. orb->misc |= ORB_SET_NOTIFY(1);
  1371. if (dma_dir == DMA_NONE)
  1372. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1373. else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
  1374. orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
  1375. else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
  1376. orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
  1377. else {
  1378. SBP2_INFO("Falling back to DMA_NONE");
  1379. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1380. }
  1381. /* set up our page table stuff */
  1382. if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
  1383. orb->data_descriptor_hi = 0x0;
  1384. orb->data_descriptor_lo = 0x0;
  1385. orb->misc |= ORB_SET_DIRECTION(1);
  1386. } else if (scsi_use_sg)
  1387. sbp2_prep_command_orb_sg(orb, hi, cmd, scsi_use_sg, sgpnt,
  1388. orb_direction, dma_dir);
  1389. else
  1390. sbp2_prep_command_orb_no_sg(orb, hi, cmd, sgpnt, orb_direction,
  1391. scsi_request_bufflen,
  1392. scsi_request_buffer, dma_dir);
  1393. sbp2util_cpu_to_be32_buffer(orb, sizeof(*orb));
  1394. memset(orb->cdb, 0, 12);
  1395. memcpy(orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
  1396. }
  1397. static void sbp2_link_orb_command(struct sbp2_lu *lu,
  1398. struct sbp2_command_info *cmd)
  1399. {
  1400. struct sbp2_fwhost_info *hi = lu->hi;
  1401. struct sbp2_command_orb *last_orb;
  1402. dma_addr_t last_orb_dma;
  1403. u64 addr = lu->command_block_agent_addr;
  1404. quadlet_t data[2];
  1405. size_t length;
  1406. unsigned long flags;
  1407. pci_dma_sync_single_for_device(hi->host->pdev, cmd->command_orb_dma,
  1408. sizeof(struct sbp2_command_orb),
  1409. PCI_DMA_TODEVICE);
  1410. pci_dma_sync_single_for_device(hi->host->pdev, cmd->sge_dma,
  1411. sizeof(cmd->scatter_gather_element),
  1412. PCI_DMA_BIDIRECTIONAL);
  1413. /* check to see if there are any previous orbs to use */
  1414. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1415. last_orb = lu->last_orb;
  1416. last_orb_dma = lu->last_orb_dma;
  1417. if (!last_orb) {
  1418. /*
  1419. * last_orb == NULL means: We know that the target's fetch agent
  1420. * is not active right now.
  1421. */
  1422. addr += SBP2_ORB_POINTER_OFFSET;
  1423. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1424. data[1] = cmd->command_orb_dma;
  1425. sbp2util_cpu_to_be32_buffer(data, 8);
  1426. length = 8;
  1427. } else {
  1428. /*
  1429. * last_orb != NULL means: We know that the target's fetch agent
  1430. * is (very probably) not dead or in reset state right now.
  1431. * We have an ORB already sent that we can append a new one to.
  1432. * The target's fetch agent may or may not have read this
  1433. * previous ORB yet.
  1434. */
  1435. pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma,
  1436. sizeof(struct sbp2_command_orb),
  1437. PCI_DMA_TODEVICE);
  1438. last_orb->next_ORB_lo = cpu_to_be32(cmd->command_orb_dma);
  1439. wmb();
  1440. /* Tells hardware that this pointer is valid */
  1441. last_orb->next_ORB_hi = 0;
  1442. pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma,
  1443. sizeof(struct sbp2_command_orb),
  1444. PCI_DMA_TODEVICE);
  1445. addr += SBP2_DOORBELL_OFFSET;
  1446. data[0] = 0;
  1447. length = 4;
  1448. }
  1449. lu->last_orb = &cmd->command_orb;
  1450. lu->last_orb_dma = cmd->command_orb_dma;
  1451. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1452. if (sbp2util_node_write_no_wait(lu->ne, addr, data, length)) {
  1453. /*
  1454. * sbp2util_node_write_no_wait failed. We certainly ran out
  1455. * of transaction labels, perhaps just because there were no
  1456. * context switches which gave khpsbpkt a chance to collect
  1457. * free tlabels. Try again in non-atomic context. If necessary,
  1458. * the workqueue job will sleep to guaranteedly get a tlabel.
  1459. * We do not accept new commands until the job is over.
  1460. */
  1461. scsi_block_requests(lu->shost);
  1462. PREPARE_WORK(&lu->protocol_work,
  1463. last_orb ? sbp2util_write_doorbell:
  1464. sbp2util_write_orb_pointer
  1465. /* */);
  1466. schedule_work(&lu->protocol_work);
  1467. }
  1468. }
  1469. static int sbp2_send_command(struct sbp2_lu *lu, struct scsi_cmnd *SCpnt,
  1470. void (*done)(struct scsi_cmnd *))
  1471. {
  1472. unchar *scsi_cmd = (unchar *)SCpnt->cmnd;
  1473. unsigned int request_bufflen = SCpnt->request_bufflen;
  1474. struct sbp2_command_info *cmd;
  1475. cmd = sbp2util_allocate_command_orb(lu, SCpnt, done);
  1476. if (!cmd)
  1477. return -EIO;
  1478. sbp2_create_command_orb(lu, cmd, scsi_cmd, SCpnt->use_sg,
  1479. request_bufflen, SCpnt->request_buffer,
  1480. SCpnt->sc_data_direction);
  1481. sbp2_link_orb_command(lu, cmd);
  1482. return 0;
  1483. }
  1484. /*
  1485. * Translates SBP-2 status into SCSI sense data for check conditions
  1486. */
  1487. static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status,
  1488. unchar *sense_data)
  1489. {
  1490. /* OK, it's pretty ugly... ;-) */
  1491. sense_data[0] = 0x70;
  1492. sense_data[1] = 0x0;
  1493. sense_data[2] = sbp2_status[9];
  1494. sense_data[3] = sbp2_status[12];
  1495. sense_data[4] = sbp2_status[13];
  1496. sense_data[5] = sbp2_status[14];
  1497. sense_data[6] = sbp2_status[15];
  1498. sense_data[7] = 10;
  1499. sense_data[8] = sbp2_status[16];
  1500. sense_data[9] = sbp2_status[17];
  1501. sense_data[10] = sbp2_status[18];
  1502. sense_data[11] = sbp2_status[19];
  1503. sense_data[12] = sbp2_status[10];
  1504. sense_data[13] = sbp2_status[11];
  1505. sense_data[14] = sbp2_status[20];
  1506. sense_data[15] = sbp2_status[21];
  1507. return sbp2_status[8] & 0x3f;
  1508. }
  1509. static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
  1510. int destid, quadlet_t *data, u64 addr,
  1511. size_t length, u16 fl)
  1512. {
  1513. struct sbp2_fwhost_info *hi;
  1514. struct sbp2_lu *lu = NULL, *lu_tmp;
  1515. struct scsi_cmnd *SCpnt = NULL;
  1516. struct sbp2_status_block *sb;
  1517. u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
  1518. struct sbp2_command_info *cmd;
  1519. unsigned long flags;
  1520. if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
  1521. SBP2_ERR("Wrong size of status block");
  1522. return RCODE_ADDRESS_ERROR;
  1523. }
  1524. if (unlikely(!host)) {
  1525. SBP2_ERR("host is NULL - this is bad!");
  1526. return RCODE_ADDRESS_ERROR;
  1527. }
  1528. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  1529. if (unlikely(!hi)) {
  1530. SBP2_ERR("host info is NULL - this is bad!");
  1531. return RCODE_ADDRESS_ERROR;
  1532. }
  1533. /* Find the unit which wrote the status. */
  1534. list_for_each_entry(lu_tmp, &hi->logical_units, lu_list) {
  1535. if (lu_tmp->ne->nodeid == nodeid &&
  1536. lu_tmp->status_fifo_addr == addr) {
  1537. lu = lu_tmp;
  1538. break;
  1539. }
  1540. }
  1541. if (unlikely(!lu)) {
  1542. SBP2_ERR("lu is NULL - device is gone?");
  1543. return RCODE_ADDRESS_ERROR;
  1544. }
  1545. /* Put response into lu status fifo buffer. The first two bytes
  1546. * come in big endian bit order. Often the target writes only a
  1547. * truncated status block, minimally the first two quadlets. The rest
  1548. * is implied to be zeros. */
  1549. sb = &lu->status_block;
  1550. memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
  1551. memcpy(sb, data, length);
  1552. sbp2util_be32_to_cpu_buffer(sb, 8);
  1553. /* Ignore unsolicited status. Handle command ORB status. */
  1554. if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
  1555. cmd = NULL;
  1556. else
  1557. cmd = sbp2util_find_command_for_orb(lu, sb->ORB_offset_lo);
  1558. if (cmd) {
  1559. pci_dma_sync_single_for_cpu(hi->host->pdev,
  1560. cmd->command_orb_dma,
  1561. sizeof(struct sbp2_command_orb),
  1562. PCI_DMA_TODEVICE);
  1563. pci_dma_sync_single_for_cpu(hi->host->pdev,
  1564. cmd->sge_dma,
  1565. sizeof(cmd->scatter_gather_element),
  1566. PCI_DMA_BIDIRECTIONAL);
  1567. /* Grab SCSI command pointers and check status. */
  1568. /*
  1569. * FIXME: If the src field in the status is 1, the ORB DMA must
  1570. * not be reused until status for a subsequent ORB is received.
  1571. */
  1572. SCpnt = cmd->Current_SCpnt;
  1573. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1574. sbp2util_mark_command_completed(lu, cmd);
  1575. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1576. if (SCpnt) {
  1577. u32 h = sb->ORB_offset_hi_misc;
  1578. u32 r = STATUS_GET_RESP(h);
  1579. if (r != RESP_STATUS_REQUEST_COMPLETE) {
  1580. SBP2_INFO("resp 0x%x, sbp_status 0x%x",
  1581. r, STATUS_GET_SBP_STATUS(h));
  1582. scsi_status =
  1583. r == RESP_STATUS_TRANSPORT_FAILURE ?
  1584. SBP2_SCSI_STATUS_BUSY :
  1585. SBP2_SCSI_STATUS_COMMAND_TERMINATED;
  1586. }
  1587. if (STATUS_GET_LEN(h) > 1)
  1588. scsi_status = sbp2_status_to_sense_data(
  1589. (unchar *)sb, SCpnt->sense_buffer);
  1590. if (STATUS_TEST_DEAD(h))
  1591. sbp2_agent_reset(lu, 0);
  1592. }
  1593. /* Check here to see if there are no commands in-use. If there
  1594. * are none, we know that the fetch agent left the active state
  1595. * _and_ that we did not reactivate it yet. Therefore clear
  1596. * last_orb so that next time we write directly to the
  1597. * ORB_POINTER register. That way the fetch agent does not need
  1598. * to refetch the next_ORB. */
  1599. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1600. if (list_empty(&lu->cmd_orb_inuse))
  1601. lu->last_orb = NULL;
  1602. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1603. } else {
  1604. /* It's probably status after a management request. */
  1605. if ((sb->ORB_offset_lo == lu->reconnect_orb_dma) ||
  1606. (sb->ORB_offset_lo == lu->login_orb_dma) ||
  1607. (sb->ORB_offset_lo == lu->query_logins_orb_dma) ||
  1608. (sb->ORB_offset_lo == lu->logout_orb_dma)) {
  1609. lu->access_complete = 1;
  1610. wake_up_interruptible(&sbp2_access_wq);
  1611. }
  1612. }
  1613. if (SCpnt)
  1614. sbp2scsi_complete_command(lu, scsi_status, SCpnt,
  1615. cmd->Current_done);
  1616. return RCODE_COMPLETE;
  1617. }
  1618. /**************************************
  1619. * SCSI interface related section
  1620. **************************************/
  1621. static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
  1622. void (*done)(struct scsi_cmnd *))
  1623. {
  1624. struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
  1625. struct sbp2_fwhost_info *hi;
  1626. int result = DID_NO_CONNECT << 16;
  1627. if (unlikely(!sbp2util_node_is_available(lu)))
  1628. goto done;
  1629. hi = lu->hi;
  1630. if (unlikely(!hi)) {
  1631. SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
  1632. goto done;
  1633. }
  1634. /* Multiple units are currently represented to the SCSI core as separate
  1635. * targets, not as one target with multiple LUs. Therefore return
  1636. * selection time-out to any IO directed at non-zero LUNs. */
  1637. if (unlikely(SCpnt->device->lun))
  1638. goto done;
  1639. /* handle the request sense command here (auto-request sense) */
  1640. if (SCpnt->cmnd[0] == REQUEST_SENSE) {
  1641. memcpy(SCpnt->request_buffer, SCpnt->sense_buffer,
  1642. SCpnt->request_bufflen);
  1643. memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
  1644. sbp2scsi_complete_command(lu, SBP2_SCSI_STATUS_GOOD, SCpnt,
  1645. done);
  1646. return 0;
  1647. }
  1648. if (unlikely(!hpsb_node_entry_valid(lu->ne))) {
  1649. SBP2_ERR("Bus reset in progress - rejecting command");
  1650. result = DID_BUS_BUSY << 16;
  1651. goto done;
  1652. }
  1653. /* Bidirectional commands are not yet implemented,
  1654. * and unknown transfer direction not handled. */
  1655. if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
  1656. SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
  1657. result = DID_ERROR << 16;
  1658. goto done;
  1659. }
  1660. if (sbp2_send_command(lu, SCpnt, done)) {
  1661. SBP2_ERR("Error sending SCSI command");
  1662. sbp2scsi_complete_command(lu,
  1663. SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
  1664. SCpnt, done);
  1665. }
  1666. return 0;
  1667. done:
  1668. SCpnt->result = result;
  1669. done(SCpnt);
  1670. return 0;
  1671. }
  1672. static void sbp2scsi_complete_all_commands(struct sbp2_lu *lu, u32 status)
  1673. {
  1674. struct sbp2_fwhost_info *hi = lu->hi;
  1675. struct list_head *lh;
  1676. struct sbp2_command_info *cmd;
  1677. unsigned long flags;
  1678. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1679. while (!list_empty(&lu->cmd_orb_inuse)) {
  1680. lh = lu->cmd_orb_inuse.next;
  1681. cmd = list_entry(lh, struct sbp2_command_info, list);
  1682. pci_dma_sync_single_for_cpu(hi->host->pdev,
  1683. cmd->command_orb_dma,
  1684. sizeof(struct sbp2_command_orb),
  1685. PCI_DMA_TODEVICE);
  1686. pci_dma_sync_single_for_cpu(hi->host->pdev, cmd->sge_dma,
  1687. sizeof(cmd->scatter_gather_element),
  1688. PCI_DMA_BIDIRECTIONAL);
  1689. sbp2util_mark_command_completed(lu, cmd);
  1690. if (cmd->Current_SCpnt) {
  1691. cmd->Current_SCpnt->result = status << 16;
  1692. cmd->Current_done(cmd->Current_SCpnt);
  1693. }
  1694. }
  1695. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1696. return;
  1697. }
  1698. /*
  1699. * Complete a regular SCSI command. Can be called in atomic context.
  1700. */
  1701. static void sbp2scsi_complete_command(struct sbp2_lu *lu, u32 scsi_status,
  1702. struct scsi_cmnd *SCpnt,
  1703. void (*done)(struct scsi_cmnd *))
  1704. {
  1705. if (!SCpnt) {
  1706. SBP2_ERR("SCpnt is NULL");
  1707. return;
  1708. }
  1709. switch (scsi_status) {
  1710. case SBP2_SCSI_STATUS_GOOD:
  1711. SCpnt->result = DID_OK << 16;
  1712. break;
  1713. case SBP2_SCSI_STATUS_BUSY:
  1714. SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
  1715. SCpnt->result = DID_BUS_BUSY << 16;
  1716. break;
  1717. case SBP2_SCSI_STATUS_CHECK_CONDITION:
  1718. SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
  1719. break;
  1720. case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
  1721. SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
  1722. SCpnt->result = DID_NO_CONNECT << 16;
  1723. scsi_print_command(SCpnt);
  1724. break;
  1725. case SBP2_SCSI_STATUS_CONDITION_MET:
  1726. case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
  1727. case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
  1728. SBP2_ERR("Bad SCSI status = %x", scsi_status);
  1729. SCpnt->result = DID_ERROR << 16;
  1730. scsi_print_command(SCpnt);
  1731. break;
  1732. default:
  1733. SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
  1734. SCpnt->result = DID_ERROR << 16;
  1735. }
  1736. /* If a bus reset is in progress and there was an error, complete
  1737. * the command as busy so that it will get retried. */
  1738. if (!hpsb_node_entry_valid(lu->ne)
  1739. && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
  1740. SBP2_ERR("Completing command with busy (bus reset)");
  1741. SCpnt->result = DID_BUS_BUSY << 16;
  1742. }
  1743. /* Tell the SCSI stack that we're done with this command. */
  1744. done(SCpnt);
  1745. }
  1746. static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
  1747. {
  1748. struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
  1749. lu->sdev = sdev;
  1750. sdev->allow_restart = 1;
  1751. if (lu->workarounds & SBP2_WORKAROUND_INQUIRY_36)
  1752. sdev->inquiry_len = 36;
  1753. return 0;
  1754. }
  1755. static int sbp2scsi_slave_configure(struct scsi_device *sdev)
  1756. {
  1757. struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
  1758. blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
  1759. sdev->use_10_for_rw = 1;
  1760. if (sdev->type == TYPE_DISK &&
  1761. lu->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
  1762. sdev->skip_ms_page_8 = 1;
  1763. if (lu->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
  1764. sdev->fix_capacity = 1;
  1765. return 0;
  1766. }
  1767. static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
  1768. {
  1769. ((struct sbp2_lu *)sdev->host->hostdata[0])->sdev = NULL;
  1770. return;
  1771. }
  1772. /*
  1773. * Called by scsi stack when something has really gone wrong.
  1774. * Usually called when a command has timed-out for some reason.
  1775. */
  1776. static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
  1777. {
  1778. struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
  1779. struct sbp2_fwhost_info *hi = lu->hi;
  1780. struct sbp2_command_info *cmd;
  1781. unsigned long flags;
  1782. SBP2_INFO("aborting sbp2 command");
  1783. scsi_print_command(SCpnt);
  1784. if (sbp2util_node_is_available(lu)) {
  1785. sbp2_agent_reset(lu, 1);
  1786. /* Return a matching command structure to the free pool. */
  1787. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1788. cmd = sbp2util_find_command_for_SCpnt(lu, SCpnt);
  1789. if (cmd) {
  1790. pci_dma_sync_single_for_cpu(hi->host->pdev,
  1791. cmd->command_orb_dma,
  1792. sizeof(struct sbp2_command_orb),
  1793. PCI_DMA_TODEVICE);
  1794. pci_dma_sync_single_for_cpu(hi->host->pdev,
  1795. cmd->sge_dma,
  1796. sizeof(cmd->scatter_gather_element),
  1797. PCI_DMA_BIDIRECTIONAL);
  1798. sbp2util_mark_command_completed(lu, cmd);
  1799. if (cmd->Current_SCpnt) {
  1800. cmd->Current_SCpnt->result = DID_ABORT << 16;
  1801. cmd->Current_done(cmd->Current_SCpnt);
  1802. }
  1803. }
  1804. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1805. sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
  1806. }
  1807. return SUCCESS;
  1808. }
  1809. /*
  1810. * Called by scsi stack when something has really gone wrong.
  1811. */
  1812. static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
  1813. {
  1814. struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
  1815. SBP2_INFO("reset requested");
  1816. if (sbp2util_node_is_available(lu)) {
  1817. SBP2_INFO("generating sbp2 fetch agent reset");
  1818. sbp2_agent_reset(lu, 1);
  1819. }
  1820. return SUCCESS;
  1821. }
  1822. static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
  1823. struct device_attribute *attr,
  1824. char *buf)
  1825. {
  1826. struct scsi_device *sdev;
  1827. struct sbp2_lu *lu;
  1828. if (!(sdev = to_scsi_device(dev)))
  1829. return 0;
  1830. if (!(lu = (struct sbp2_lu *)sdev->host->hostdata[0]))
  1831. return 0;
  1832. return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)lu->ne->guid,
  1833. lu->ud->id, ORB_SET_LUN(lu->lun));
  1834. }
  1835. MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
  1836. MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
  1837. MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
  1838. MODULE_LICENSE("GPL");
  1839. static int sbp2_module_init(void)
  1840. {
  1841. int ret;
  1842. if (sbp2_serialize_io) {
  1843. sbp2_shost_template.can_queue = 1;
  1844. sbp2_shost_template.cmd_per_lun = 1;
  1845. }
  1846. if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
  1847. (sbp2_max_sectors * 512) > (128 * 1024))
  1848. sbp2_max_sectors = 128 * 1024 / 512;
  1849. sbp2_shost_template.max_sectors = sbp2_max_sectors;
  1850. hpsb_register_highlevel(&sbp2_highlevel);
  1851. ret = hpsb_register_protocol(&sbp2_driver);
  1852. if (ret) {
  1853. SBP2_ERR("Failed to register protocol");
  1854. hpsb_unregister_highlevel(&sbp2_highlevel);
  1855. return ret;
  1856. }
  1857. return 0;
  1858. }
  1859. static void __exit sbp2_module_exit(void)
  1860. {
  1861. hpsb_unregister_protocol(&sbp2_driver);
  1862. hpsb_unregister_highlevel(&sbp2_highlevel);
  1863. }
  1864. module_init(sbp2_module_init);
  1865. module_exit(sbp2_module_exit);