sbp2.c 63 KB

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