sbp2.c 62 KB

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