sbp2.c 63 KB

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