sbp2.c 63 KB

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