sbp2.c 63 KB

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