sbp2.c 63 KB

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