sbp2.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  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. struct hpsb_host *host)
  463. {
  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. #endif
  689. }
  690. /* Prevent unloading of the 1394 host */
  691. if (!try_module_get(hi->host->driver->owner)) {
  692. SBP2_ERR("failed to get a reference on 1394 host driver");
  693. goto failed_alloc;
  694. }
  695. lu->hi = hi;
  696. list_add_tail(&lu->lu_list, &hi->logical_units);
  697. /* Register the status FIFO address range. We could use the same FIFO
  698. * for targets at different nodes. However we need different FIFOs per
  699. * target in order to support multi-unit devices.
  700. * The FIFO is located out of the local host controller's physical range
  701. * but, if possible, within the posted write area. Status writes will
  702. * then be performed as unified transactions. This slightly reduces
  703. * bandwidth usage, and some Prolific based devices seem to require it.
  704. */
  705. lu->status_fifo_addr = hpsb_allocate_and_register_addrspace(
  706. &sbp2_highlevel, ud->ne->host, &sbp2_ops,
  707. sizeof(struct sbp2_status_block), sizeof(quadlet_t),
  708. ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
  709. if (lu->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
  710. SBP2_ERR("failed to allocate status FIFO address range");
  711. goto failed_alloc;
  712. }
  713. shost = scsi_host_alloc(&sbp2_shost_template, sizeof(unsigned long));
  714. if (!shost) {
  715. SBP2_ERR("failed to register scsi host");
  716. goto failed_alloc;
  717. }
  718. shost->hostdata[0] = (unsigned long)lu;
  719. if (!scsi_add_host(shost, &ud->device)) {
  720. lu->shost = shost;
  721. return lu;
  722. }
  723. SBP2_ERR("failed to add scsi host");
  724. scsi_host_put(shost);
  725. failed_alloc:
  726. sbp2_remove_device(lu);
  727. return NULL;
  728. }
  729. static void sbp2_host_reset(struct hpsb_host *host)
  730. {
  731. struct sbp2_fwhost_info *hi;
  732. struct sbp2_lu *lu;
  733. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  734. if (!hi)
  735. return;
  736. list_for_each_entry(lu, &hi->logical_units, lu_list)
  737. if (likely(atomic_read(&lu->state) !=
  738. SBP2LU_STATE_IN_SHUTDOWN)) {
  739. atomic_set(&lu->state, SBP2LU_STATE_IN_RESET);
  740. scsi_block_requests(lu->shost);
  741. }
  742. }
  743. static int sbp2_start_device(struct sbp2_lu *lu)
  744. {
  745. struct sbp2_fwhost_info *hi = lu->hi;
  746. int error;
  747. lu->login_response = dma_alloc_coherent(hi->host->device.parent,
  748. sizeof(struct sbp2_login_response),
  749. &lu->login_response_dma, GFP_KERNEL);
  750. if (!lu->login_response)
  751. goto alloc_fail;
  752. lu->query_logins_orb = dma_alloc_coherent(hi->host->device.parent,
  753. sizeof(struct sbp2_query_logins_orb),
  754. &lu->query_logins_orb_dma, GFP_KERNEL);
  755. if (!lu->query_logins_orb)
  756. goto alloc_fail;
  757. lu->query_logins_response = dma_alloc_coherent(hi->host->device.parent,
  758. sizeof(struct sbp2_query_logins_response),
  759. &lu->query_logins_response_dma, GFP_KERNEL);
  760. if (!lu->query_logins_response)
  761. goto alloc_fail;
  762. lu->reconnect_orb = dma_alloc_coherent(hi->host->device.parent,
  763. sizeof(struct sbp2_reconnect_orb),
  764. &lu->reconnect_orb_dma, GFP_KERNEL);
  765. if (!lu->reconnect_orb)
  766. goto alloc_fail;
  767. lu->logout_orb = dma_alloc_coherent(hi->host->device.parent,
  768. sizeof(struct sbp2_logout_orb),
  769. &lu->logout_orb_dma, GFP_KERNEL);
  770. if (!lu->logout_orb)
  771. goto alloc_fail;
  772. lu->login_orb = dma_alloc_coherent(hi->host->device.parent,
  773. sizeof(struct sbp2_login_orb),
  774. &lu->login_orb_dma, GFP_KERNEL);
  775. if (!lu->login_orb)
  776. goto alloc_fail;
  777. if (sbp2util_create_command_orb_pool(lu))
  778. goto alloc_fail;
  779. /* Wait a second before trying to log in. Previously logged in
  780. * initiators need a chance to reconnect. */
  781. if (msleep_interruptible(1000)) {
  782. sbp2_remove_device(lu);
  783. return -EINTR;
  784. }
  785. if (sbp2_login_device(lu)) {
  786. sbp2_remove_device(lu);
  787. return -EBUSY;
  788. }
  789. sbp2_set_busy_timeout(lu);
  790. sbp2_agent_reset(lu, 1);
  791. sbp2_max_speed_and_size(lu);
  792. error = scsi_add_device(lu->shost, 0, lu->ud->id, 0);
  793. if (error) {
  794. SBP2_ERR("scsi_add_device failed");
  795. sbp2_logout_device(lu);
  796. sbp2_remove_device(lu);
  797. return error;
  798. }
  799. return 0;
  800. alloc_fail:
  801. SBP2_ERR("Could not allocate memory for lu");
  802. sbp2_remove_device(lu);
  803. return -ENOMEM;
  804. }
  805. static void sbp2_remove_device(struct sbp2_lu *lu)
  806. {
  807. struct sbp2_fwhost_info *hi;
  808. if (!lu)
  809. return;
  810. hi = lu->hi;
  811. if (!hi)
  812. goto no_hi;
  813. if (lu->shost) {
  814. scsi_remove_host(lu->shost);
  815. scsi_host_put(lu->shost);
  816. }
  817. flush_scheduled_work();
  818. sbp2util_remove_command_orb_pool(lu, hi->host);
  819. list_del(&lu->lu_list);
  820. if (lu->login_response)
  821. dma_free_coherent(hi->host->device.parent,
  822. sizeof(struct sbp2_login_response),
  823. lu->login_response,
  824. lu->login_response_dma);
  825. if (lu->login_orb)
  826. dma_free_coherent(hi->host->device.parent,
  827. sizeof(struct sbp2_login_orb),
  828. lu->login_orb,
  829. lu->login_orb_dma);
  830. if (lu->reconnect_orb)
  831. dma_free_coherent(hi->host->device.parent,
  832. sizeof(struct sbp2_reconnect_orb),
  833. lu->reconnect_orb,
  834. lu->reconnect_orb_dma);
  835. if (lu->logout_orb)
  836. dma_free_coherent(hi->host->device.parent,
  837. sizeof(struct sbp2_logout_orb),
  838. lu->logout_orb,
  839. lu->logout_orb_dma);
  840. if (lu->query_logins_orb)
  841. dma_free_coherent(hi->host->device.parent,
  842. sizeof(struct sbp2_query_logins_orb),
  843. lu->query_logins_orb,
  844. lu->query_logins_orb_dma);
  845. if (lu->query_logins_response)
  846. dma_free_coherent(hi->host->device.parent,
  847. sizeof(struct sbp2_query_logins_response),
  848. lu->query_logins_response,
  849. lu->query_logins_response_dma);
  850. if (lu->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
  851. hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
  852. lu->status_fifo_addr);
  853. lu->ud->device.driver_data = NULL;
  854. module_put(hi->host->driver->owner);
  855. no_hi:
  856. kfree(lu);
  857. }
  858. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  859. /*
  860. * Deal with write requests on adapters which do not support physical DMA or
  861. * have it switched off.
  862. */
  863. static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
  864. int destid, quadlet_t *data, u64 addr,
  865. size_t length, u16 flags)
  866. {
  867. memcpy(bus_to_virt((u32) addr), data, length);
  868. return RCODE_COMPLETE;
  869. }
  870. /*
  871. * Deal with read requests on adapters which do not support physical DMA or
  872. * have it switched off.
  873. */
  874. static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
  875. quadlet_t *data, u64 addr, size_t length,
  876. u16 flags)
  877. {
  878. memcpy(data, bus_to_virt((u32) addr), length);
  879. return RCODE_COMPLETE;
  880. }
  881. #endif
  882. /**************************************
  883. * SBP-2 protocol related section
  884. **************************************/
  885. static int sbp2_query_logins(struct sbp2_lu *lu)
  886. {
  887. struct sbp2_fwhost_info *hi = lu->hi;
  888. quadlet_t data[2];
  889. int max_logins;
  890. int active_logins;
  891. lu->query_logins_orb->reserved1 = 0x0;
  892. lu->query_logins_orb->reserved2 = 0x0;
  893. lu->query_logins_orb->query_response_lo = lu->query_logins_response_dma;
  894. lu->query_logins_orb->query_response_hi =
  895. ORB_SET_NODE_ID(hi->host->node_id);
  896. lu->query_logins_orb->lun_misc =
  897. ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
  898. lu->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
  899. lu->query_logins_orb->lun_misc |= ORB_SET_LUN(lu->lun);
  900. lu->query_logins_orb->reserved_resp_length =
  901. ORB_SET_QUERY_LOGINS_RESP_LENGTH(
  902. sizeof(struct sbp2_query_logins_response));
  903. lu->query_logins_orb->status_fifo_hi =
  904. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  905. lu->query_logins_orb->status_fifo_lo =
  906. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  907. sbp2util_cpu_to_be32_buffer(lu->query_logins_orb,
  908. sizeof(struct sbp2_query_logins_orb));
  909. memset(lu->query_logins_response, 0,
  910. sizeof(struct sbp2_query_logins_response));
  911. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  912. data[1] = lu->query_logins_orb_dma;
  913. sbp2util_cpu_to_be32_buffer(data, 8);
  914. hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  915. if (sbp2util_access_timeout(lu, 2*HZ)) {
  916. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  917. return -EIO;
  918. }
  919. if (lu->status_block.ORB_offset_lo != lu->query_logins_orb_dma) {
  920. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  921. return -EIO;
  922. }
  923. if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
  924. SBP2_INFO("Error querying logins to SBP-2 device - failed");
  925. return -EIO;
  926. }
  927. sbp2util_cpu_to_be32_buffer(lu->query_logins_response,
  928. sizeof(struct sbp2_query_logins_response));
  929. max_logins = RESPONSE_GET_MAX_LOGINS(
  930. lu->query_logins_response->length_max_logins);
  931. SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
  932. active_logins = RESPONSE_GET_ACTIVE_LOGINS(
  933. lu->query_logins_response->length_max_logins);
  934. SBP2_INFO("Number of active logins: %d", active_logins);
  935. if (active_logins >= max_logins) {
  936. return -EIO;
  937. }
  938. return 0;
  939. }
  940. static int sbp2_login_device(struct sbp2_lu *lu)
  941. {
  942. struct sbp2_fwhost_info *hi = lu->hi;
  943. quadlet_t data[2];
  944. if (!lu->login_orb)
  945. return -EIO;
  946. if (!sbp2_exclusive_login && sbp2_query_logins(lu)) {
  947. SBP2_INFO("Device does not support any more concurrent logins");
  948. return -EIO;
  949. }
  950. /* assume no password */
  951. lu->login_orb->password_hi = 0;
  952. lu->login_orb->password_lo = 0;
  953. lu->login_orb->login_response_lo = lu->login_response_dma;
  954. lu->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
  955. lu->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
  956. /* one second reconnect time */
  957. lu->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
  958. lu->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
  959. lu->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
  960. lu->login_orb->lun_misc |= ORB_SET_LUN(lu->lun);
  961. lu->login_orb->passwd_resp_lengths =
  962. ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
  963. lu->login_orb->status_fifo_hi =
  964. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  965. lu->login_orb->status_fifo_lo =
  966. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  967. sbp2util_cpu_to_be32_buffer(lu->login_orb,
  968. sizeof(struct sbp2_login_orb));
  969. memset(lu->login_response, 0, sizeof(struct sbp2_login_response));
  970. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  971. data[1] = lu->login_orb_dma;
  972. sbp2util_cpu_to_be32_buffer(data, 8);
  973. hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  974. /* wait up to 20 seconds for login status */
  975. if (sbp2util_access_timeout(lu, 20*HZ)) {
  976. SBP2_ERR("Error logging into SBP-2 device - timed out");
  977. return -EIO;
  978. }
  979. /* make sure that the returned status matches the login ORB */
  980. if (lu->status_block.ORB_offset_lo != lu->login_orb_dma) {
  981. SBP2_ERR("Error logging into SBP-2 device - timed out");
  982. return -EIO;
  983. }
  984. if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
  985. SBP2_ERR("Error logging into SBP-2 device - failed");
  986. return -EIO;
  987. }
  988. sbp2util_cpu_to_be32_buffer(lu->login_response,
  989. sizeof(struct sbp2_login_response));
  990. lu->command_block_agent_addr =
  991. ((u64)lu->login_response->command_block_agent_hi) << 32;
  992. lu->command_block_agent_addr |=
  993. ((u64)lu->login_response->command_block_agent_lo);
  994. lu->command_block_agent_addr &= 0x0000ffffffffffffULL;
  995. SBP2_INFO("Logged into SBP-2 device");
  996. return 0;
  997. }
  998. static int sbp2_logout_device(struct sbp2_lu *lu)
  999. {
  1000. struct sbp2_fwhost_info *hi = lu->hi;
  1001. quadlet_t data[2];
  1002. int error;
  1003. lu->logout_orb->reserved1 = 0x0;
  1004. lu->logout_orb->reserved2 = 0x0;
  1005. lu->logout_orb->reserved3 = 0x0;
  1006. lu->logout_orb->reserved4 = 0x0;
  1007. lu->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
  1008. lu->logout_orb->login_ID_misc |=
  1009. ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
  1010. lu->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1011. lu->logout_orb->reserved5 = 0x0;
  1012. lu->logout_orb->status_fifo_hi =
  1013. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  1014. lu->logout_orb->status_fifo_lo =
  1015. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  1016. sbp2util_cpu_to_be32_buffer(lu->logout_orb,
  1017. sizeof(struct sbp2_logout_orb));
  1018. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1019. data[1] = lu->logout_orb_dma;
  1020. sbp2util_cpu_to_be32_buffer(data, 8);
  1021. error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  1022. if (error)
  1023. return error;
  1024. /* wait up to 1 second for the device to complete logout */
  1025. if (sbp2util_access_timeout(lu, HZ))
  1026. return -EIO;
  1027. SBP2_INFO("Logged out of SBP-2 device");
  1028. return 0;
  1029. }
  1030. static int sbp2_reconnect_device(struct sbp2_lu *lu)
  1031. {
  1032. struct sbp2_fwhost_info *hi = lu->hi;
  1033. quadlet_t data[2];
  1034. int error;
  1035. lu->reconnect_orb->reserved1 = 0x0;
  1036. lu->reconnect_orb->reserved2 = 0x0;
  1037. lu->reconnect_orb->reserved3 = 0x0;
  1038. lu->reconnect_orb->reserved4 = 0x0;
  1039. lu->reconnect_orb->login_ID_misc =
  1040. ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
  1041. lu->reconnect_orb->login_ID_misc |=
  1042. ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
  1043. lu->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1044. lu->reconnect_orb->reserved5 = 0x0;
  1045. lu->reconnect_orb->status_fifo_hi =
  1046. ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
  1047. lu->reconnect_orb->status_fifo_lo =
  1048. ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
  1049. sbp2util_cpu_to_be32_buffer(lu->reconnect_orb,
  1050. sizeof(struct sbp2_reconnect_orb));
  1051. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1052. data[1] = lu->reconnect_orb_dma;
  1053. sbp2util_cpu_to_be32_buffer(data, 8);
  1054. error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
  1055. if (error)
  1056. return error;
  1057. /* wait up to 1 second for reconnect status */
  1058. if (sbp2util_access_timeout(lu, HZ)) {
  1059. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1060. return -EIO;
  1061. }
  1062. /* make sure that the returned status matches the reconnect ORB */
  1063. if (lu->status_block.ORB_offset_lo != lu->reconnect_orb_dma) {
  1064. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1065. return -EIO;
  1066. }
  1067. if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
  1068. SBP2_ERR("Error reconnecting to SBP-2 device - failed");
  1069. return -EIO;
  1070. }
  1071. SBP2_INFO("Reconnected to SBP-2 device");
  1072. return 0;
  1073. }
  1074. /*
  1075. * Set the target node's Single Phase Retry limit. Affects the target's retry
  1076. * behaviour if our node is too busy to accept requests.
  1077. */
  1078. static int sbp2_set_busy_timeout(struct sbp2_lu *lu)
  1079. {
  1080. quadlet_t data;
  1081. data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
  1082. if (hpsb_node_write(lu->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
  1083. SBP2_ERR("%s error", __FUNCTION__);
  1084. return 0;
  1085. }
  1086. static void sbp2_parse_unit_directory(struct sbp2_lu *lu,
  1087. struct unit_directory *ud)
  1088. {
  1089. struct csr1212_keyval *kv;
  1090. struct csr1212_dentry *dentry;
  1091. u64 management_agent_addr;
  1092. u32 unit_characteristics, firmware_revision;
  1093. unsigned workarounds;
  1094. int i;
  1095. management_agent_addr = 0;
  1096. unit_characteristics = 0;
  1097. firmware_revision = 0;
  1098. csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
  1099. switch (kv->key.id) {
  1100. case CSR1212_KV_ID_DEPENDENT_INFO:
  1101. if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
  1102. management_agent_addr =
  1103. CSR1212_REGISTER_SPACE_BASE +
  1104. (kv->value.csr_offset << 2);
  1105. else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
  1106. lu->lun = ORB_SET_LUN(kv->value.immediate);
  1107. break;
  1108. case SBP2_UNIT_CHARACTERISTICS_KEY:
  1109. /* FIXME: This is ignored so far.
  1110. * See SBP-2 clause 7.4.8. */
  1111. unit_characteristics = kv->value.immediate;
  1112. break;
  1113. case SBP2_FIRMWARE_REVISION_KEY:
  1114. firmware_revision = kv->value.immediate;
  1115. break;
  1116. default:
  1117. /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
  1118. * Its "ordered" bit has consequences for command ORB
  1119. * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
  1120. break;
  1121. }
  1122. }
  1123. workarounds = sbp2_default_workarounds;
  1124. if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
  1125. for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
  1126. if (sbp2_workarounds_table[i].firmware_revision !=
  1127. SBP2_ROM_VALUE_WILDCARD &&
  1128. sbp2_workarounds_table[i].firmware_revision !=
  1129. (firmware_revision & 0xffff00))
  1130. continue;
  1131. if (sbp2_workarounds_table[i].model_id !=
  1132. SBP2_ROM_VALUE_WILDCARD &&
  1133. sbp2_workarounds_table[i].model_id != ud->model_id)
  1134. continue;
  1135. workarounds |= sbp2_workarounds_table[i].workarounds;
  1136. break;
  1137. }
  1138. if (workarounds)
  1139. SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
  1140. "(firmware_revision 0x%06x, vendor_id 0x%06x,"
  1141. " model_id 0x%06x)",
  1142. NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
  1143. workarounds, firmware_revision,
  1144. ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
  1145. ud->model_id);
  1146. /* We would need one SCSI host template for each target to adjust
  1147. * max_sectors on the fly, therefore warn only. */
  1148. if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
  1149. (sbp2_max_sectors * 512) > (128 * 1024))
  1150. SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
  1151. "max transfer size. WARNING: Current max_sectors "
  1152. "setting is larger than 128KB (%d sectors)",
  1153. NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
  1154. sbp2_max_sectors);
  1155. /* If this is a logical unit directory entry, process the parent
  1156. * to get the values. */
  1157. if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
  1158. struct unit_directory *parent_ud = container_of(
  1159. ud->device.parent, struct unit_directory, device);
  1160. sbp2_parse_unit_directory(lu, parent_ud);
  1161. } else {
  1162. lu->management_agent_addr = management_agent_addr;
  1163. lu->workarounds = workarounds;
  1164. if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
  1165. lu->lun = ORB_SET_LUN(ud->lun);
  1166. }
  1167. }
  1168. #define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
  1169. /*
  1170. * This function is called in order to determine the max speed and packet
  1171. * size we can use in our ORBs. Note, that we (the driver and host) only
  1172. * initiate the transaction. The SBP-2 device actually transfers the data
  1173. * (by reading from the DMA area we tell it). This means that the SBP-2
  1174. * device decides the actual maximum data it can transfer. We just tell it
  1175. * the speed that it needs to use, and the max_rec the host supports, and
  1176. * it takes care of the rest.
  1177. */
  1178. static int sbp2_max_speed_and_size(struct sbp2_lu *lu)
  1179. {
  1180. struct sbp2_fwhost_info *hi = lu->hi;
  1181. u8 payload;
  1182. lu->speed_code = hi->host->speed[NODEID_TO_NODE(lu->ne->nodeid)];
  1183. if (lu->speed_code > sbp2_max_speed) {
  1184. lu->speed_code = sbp2_max_speed;
  1185. SBP2_INFO("Reducing speed to %s",
  1186. hpsb_speedto_str[sbp2_max_speed]);
  1187. }
  1188. /* Payload size is the lesser of what our speed supports and what
  1189. * our host supports. */
  1190. payload = min(sbp2_speedto_max_payload[lu->speed_code],
  1191. (u8) (hi->host->csr.max_rec - 1));
  1192. /* If physical DMA is off, work around limitation in ohci1394:
  1193. * packet size must not exceed PAGE_SIZE */
  1194. if (lu->ne->host->low_addr_space < (1ULL << 32))
  1195. while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
  1196. payload)
  1197. payload--;
  1198. SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
  1199. NODE_BUS_ARGS(hi->host, lu->ne->nodeid),
  1200. hpsb_speedto_str[lu->speed_code],
  1201. SBP2_PAYLOAD_TO_BYTES(payload));
  1202. lu->max_payload_size = payload;
  1203. return 0;
  1204. }
  1205. static int sbp2_agent_reset(struct sbp2_lu *lu, int wait)
  1206. {
  1207. quadlet_t data;
  1208. u64 addr;
  1209. int retval;
  1210. unsigned long flags;
  1211. /* flush lu->protocol_work */
  1212. if (wait)
  1213. flush_scheduled_work();
  1214. data = ntohl(SBP2_AGENT_RESET_DATA);
  1215. addr = lu->command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
  1216. if (wait)
  1217. retval = hpsb_node_write(lu->ne, addr, &data, 4);
  1218. else
  1219. retval = sbp2util_node_write_no_wait(lu->ne, addr, &data, 4);
  1220. if (retval < 0) {
  1221. SBP2_ERR("hpsb_node_write failed.\n");
  1222. return -EIO;
  1223. }
  1224. /* make sure that the ORB_POINTER is written on next command */
  1225. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1226. lu->last_orb = NULL;
  1227. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1228. return 0;
  1229. }
  1230. static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
  1231. struct sbp2_fwhost_info *hi,
  1232. struct sbp2_command_info *cmd,
  1233. unsigned int scsi_use_sg,
  1234. struct scatterlist *sgpnt,
  1235. u32 orb_direction,
  1236. enum dma_data_direction dma_dir)
  1237. {
  1238. cmd->dma_dir = dma_dir;
  1239. orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1240. orb->misc |= ORB_SET_DIRECTION(orb_direction);
  1241. /* special case if only one element (and less than 64KB in size) */
  1242. if ((scsi_use_sg == 1) &&
  1243. (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
  1244. cmd->dma_size = sgpnt[0].length;
  1245. cmd->dma_type = CMD_DMA_PAGE;
  1246. cmd->cmd_dma = dma_map_page(hi->host->device.parent,
  1247. sgpnt[0].page, sgpnt[0].offset,
  1248. cmd->dma_size, cmd->dma_dir);
  1249. orb->data_descriptor_lo = cmd->cmd_dma;
  1250. orb->misc |= ORB_SET_DATA_SIZE(cmd->dma_size);
  1251. } else {
  1252. struct sbp2_unrestricted_page_table *sg_element =
  1253. &cmd->scatter_gather_element[0];
  1254. u32 sg_count, sg_len;
  1255. dma_addr_t sg_addr;
  1256. int i, count = dma_map_sg(hi->host->device.parent, sgpnt,
  1257. scsi_use_sg, dma_dir);
  1258. cmd->dma_size = scsi_use_sg;
  1259. cmd->sge_buffer = sgpnt;
  1260. /* use page tables (s/g) */
  1261. orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
  1262. orb->data_descriptor_lo = cmd->sge_dma;
  1263. /* loop through and fill out our SBP-2 page tables
  1264. * (and split up anything too large) */
  1265. for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
  1266. sg_len = sg_dma_len(sgpnt);
  1267. sg_addr = sg_dma_address(sgpnt);
  1268. while (sg_len) {
  1269. sg_element[sg_count].segment_base_lo = sg_addr;
  1270. if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
  1271. sg_element[sg_count].length_segment_base_hi =
  1272. PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
  1273. sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
  1274. sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
  1275. } else {
  1276. sg_element[sg_count].length_segment_base_hi =
  1277. PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
  1278. sg_len = 0;
  1279. }
  1280. sg_count++;
  1281. }
  1282. }
  1283. orb->misc |= ORB_SET_DATA_SIZE(sg_count);
  1284. sbp2util_cpu_to_be32_buffer(sg_element,
  1285. (sizeof(struct sbp2_unrestricted_page_table)) *
  1286. sg_count);
  1287. }
  1288. }
  1289. static void sbp2_create_command_orb(struct sbp2_lu *lu,
  1290. struct sbp2_command_info *cmd,
  1291. unchar *scsi_cmd,
  1292. unsigned int scsi_use_sg,
  1293. unsigned int scsi_request_bufflen,
  1294. void *scsi_request_buffer,
  1295. enum dma_data_direction dma_dir)
  1296. {
  1297. struct sbp2_fwhost_info *hi = lu->hi;
  1298. struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
  1299. struct sbp2_command_orb *orb = &cmd->command_orb;
  1300. u32 orb_direction;
  1301. /*
  1302. * Set-up our command ORB.
  1303. *
  1304. * NOTE: We're doing unrestricted page tables (s/g), as this is
  1305. * best performance (at least with the devices I have). This means
  1306. * that data_size becomes the number of s/g elements, and
  1307. * page_size should be zero (for unrestricted).
  1308. */
  1309. orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
  1310. orb->next_ORB_lo = 0x0;
  1311. orb->misc = ORB_SET_MAX_PAYLOAD(lu->max_payload_size);
  1312. orb->misc |= ORB_SET_SPEED(lu->speed_code);
  1313. orb->misc |= ORB_SET_NOTIFY(1);
  1314. if (dma_dir == DMA_NONE)
  1315. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1316. else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
  1317. orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
  1318. else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
  1319. orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
  1320. else {
  1321. SBP2_INFO("Falling back to DMA_NONE");
  1322. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1323. }
  1324. /* set up our page table stuff */
  1325. if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
  1326. orb->data_descriptor_hi = 0x0;
  1327. orb->data_descriptor_lo = 0x0;
  1328. orb->misc |= ORB_SET_DIRECTION(1);
  1329. } else
  1330. sbp2_prep_command_orb_sg(orb, hi, cmd, scsi_use_sg, sgpnt,
  1331. orb_direction, dma_dir);
  1332. sbp2util_cpu_to_be32_buffer(orb, sizeof(*orb));
  1333. memset(orb->cdb, 0, 12);
  1334. memcpy(orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
  1335. }
  1336. static void sbp2_link_orb_command(struct sbp2_lu *lu,
  1337. struct sbp2_command_info *cmd)
  1338. {
  1339. struct sbp2_fwhost_info *hi = lu->hi;
  1340. struct sbp2_command_orb *last_orb;
  1341. dma_addr_t last_orb_dma;
  1342. u64 addr = lu->command_block_agent_addr;
  1343. quadlet_t data[2];
  1344. size_t length;
  1345. unsigned long flags;
  1346. dma_sync_single_for_device(hi->host->device.parent,
  1347. cmd->command_orb_dma,
  1348. sizeof(struct sbp2_command_orb),
  1349. DMA_TO_DEVICE);
  1350. dma_sync_single_for_device(hi->host->device.parent, cmd->sge_dma,
  1351. sizeof(cmd->scatter_gather_element),
  1352. DMA_TO_DEVICE);
  1353. /* check to see if there are any previous orbs to use */
  1354. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1355. last_orb = lu->last_orb;
  1356. last_orb_dma = lu->last_orb_dma;
  1357. if (!last_orb) {
  1358. /*
  1359. * last_orb == NULL means: We know that the target's fetch agent
  1360. * is not active right now.
  1361. */
  1362. addr += SBP2_ORB_POINTER_OFFSET;
  1363. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1364. data[1] = cmd->command_orb_dma;
  1365. sbp2util_cpu_to_be32_buffer(data, 8);
  1366. length = 8;
  1367. } else {
  1368. /*
  1369. * last_orb != NULL means: We know that the target's fetch agent
  1370. * is (very probably) not dead or in reset state right now.
  1371. * We have an ORB already sent that we can append a new one to.
  1372. * The target's fetch agent may or may not have read this
  1373. * previous ORB yet.
  1374. */
  1375. dma_sync_single_for_cpu(hi->host->device.parent, last_orb_dma,
  1376. sizeof(struct sbp2_command_orb),
  1377. DMA_TO_DEVICE);
  1378. last_orb->next_ORB_lo = cpu_to_be32(cmd->command_orb_dma);
  1379. wmb();
  1380. /* Tells hardware that this pointer is valid */
  1381. last_orb->next_ORB_hi = 0;
  1382. dma_sync_single_for_device(hi->host->device.parent,
  1383. last_orb_dma,
  1384. sizeof(struct sbp2_command_orb),
  1385. DMA_TO_DEVICE);
  1386. addr += SBP2_DOORBELL_OFFSET;
  1387. data[0] = 0;
  1388. length = 4;
  1389. }
  1390. lu->last_orb = &cmd->command_orb;
  1391. lu->last_orb_dma = cmd->command_orb_dma;
  1392. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1393. if (sbp2util_node_write_no_wait(lu->ne, addr, data, length)) {
  1394. /*
  1395. * sbp2util_node_write_no_wait failed. We certainly ran out
  1396. * of transaction labels, perhaps just because there were no
  1397. * context switches which gave khpsbpkt a chance to collect
  1398. * free tlabels. Try again in non-atomic context. If necessary,
  1399. * the workqueue job will sleep to guaranteedly get a tlabel.
  1400. * We do not accept new commands until the job is over.
  1401. */
  1402. scsi_block_requests(lu->shost);
  1403. PREPARE_WORK(&lu->protocol_work,
  1404. last_orb ? sbp2util_write_doorbell:
  1405. sbp2util_write_orb_pointer);
  1406. schedule_work(&lu->protocol_work);
  1407. }
  1408. }
  1409. static int sbp2_send_command(struct sbp2_lu *lu, struct scsi_cmnd *SCpnt,
  1410. void (*done)(struct scsi_cmnd *))
  1411. {
  1412. unchar *scsi_cmd = (unchar *)SCpnt->cmnd;
  1413. unsigned int request_bufflen = scsi_bufflen(SCpnt);
  1414. struct sbp2_command_info *cmd;
  1415. cmd = sbp2util_allocate_command_orb(lu, SCpnt, done);
  1416. if (!cmd)
  1417. return -EIO;
  1418. sbp2_create_command_orb(lu, cmd, scsi_cmd, scsi_sg_count(SCpnt),
  1419. request_bufflen, scsi_sglist(SCpnt),
  1420. SCpnt->sc_data_direction);
  1421. sbp2_link_orb_command(lu, cmd);
  1422. return 0;
  1423. }
  1424. /*
  1425. * Translates SBP-2 status into SCSI sense data for check conditions
  1426. */
  1427. static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status,
  1428. unchar *sense_data)
  1429. {
  1430. /* OK, it's pretty ugly... ;-) */
  1431. sense_data[0] = 0x70;
  1432. sense_data[1] = 0x0;
  1433. sense_data[2] = sbp2_status[9];
  1434. sense_data[3] = sbp2_status[12];
  1435. sense_data[4] = sbp2_status[13];
  1436. sense_data[5] = sbp2_status[14];
  1437. sense_data[6] = sbp2_status[15];
  1438. sense_data[7] = 10;
  1439. sense_data[8] = sbp2_status[16];
  1440. sense_data[9] = sbp2_status[17];
  1441. sense_data[10] = sbp2_status[18];
  1442. sense_data[11] = sbp2_status[19];
  1443. sense_data[12] = sbp2_status[10];
  1444. sense_data[13] = sbp2_status[11];
  1445. sense_data[14] = sbp2_status[20];
  1446. sense_data[15] = sbp2_status[21];
  1447. return sbp2_status[8] & 0x3f;
  1448. }
  1449. static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
  1450. int destid, quadlet_t *data, u64 addr,
  1451. size_t length, u16 fl)
  1452. {
  1453. struct sbp2_fwhost_info *hi;
  1454. struct sbp2_lu *lu = NULL, *lu_tmp;
  1455. struct scsi_cmnd *SCpnt = NULL;
  1456. struct sbp2_status_block *sb;
  1457. u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
  1458. struct sbp2_command_info *cmd;
  1459. unsigned long flags;
  1460. if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
  1461. SBP2_ERR("Wrong size of status block");
  1462. return RCODE_ADDRESS_ERROR;
  1463. }
  1464. if (unlikely(!host)) {
  1465. SBP2_ERR("host is NULL - this is bad!");
  1466. return RCODE_ADDRESS_ERROR;
  1467. }
  1468. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  1469. if (unlikely(!hi)) {
  1470. SBP2_ERR("host info is NULL - this is bad!");
  1471. return RCODE_ADDRESS_ERROR;
  1472. }
  1473. /* Find the unit which wrote the status. */
  1474. list_for_each_entry(lu_tmp, &hi->logical_units, lu_list) {
  1475. if (lu_tmp->ne->nodeid == nodeid &&
  1476. lu_tmp->status_fifo_addr == addr) {
  1477. lu = lu_tmp;
  1478. break;
  1479. }
  1480. }
  1481. if (unlikely(!lu)) {
  1482. SBP2_ERR("lu is NULL - device is gone?");
  1483. return RCODE_ADDRESS_ERROR;
  1484. }
  1485. /* Put response into lu status fifo buffer. The first two bytes
  1486. * come in big endian bit order. Often the target writes only a
  1487. * truncated status block, minimally the first two quadlets. The rest
  1488. * is implied to be zeros. */
  1489. sb = &lu->status_block;
  1490. memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
  1491. memcpy(sb, data, length);
  1492. sbp2util_be32_to_cpu_buffer(sb, 8);
  1493. /* Ignore unsolicited status. Handle command ORB status. */
  1494. if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
  1495. cmd = NULL;
  1496. else
  1497. cmd = sbp2util_find_command_for_orb(lu, sb->ORB_offset_lo);
  1498. if (cmd) {
  1499. dma_sync_single_for_cpu(hi->host->device.parent,
  1500. cmd->command_orb_dma,
  1501. sizeof(struct sbp2_command_orb),
  1502. DMA_TO_DEVICE);
  1503. dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
  1504. sizeof(cmd->scatter_gather_element),
  1505. DMA_TO_DEVICE);
  1506. /* Grab SCSI command pointers and check status. */
  1507. /*
  1508. * FIXME: If the src field in the status is 1, the ORB DMA must
  1509. * not be reused until status for a subsequent ORB is received.
  1510. */
  1511. SCpnt = cmd->Current_SCpnt;
  1512. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1513. sbp2util_mark_command_completed(lu, cmd);
  1514. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1515. if (SCpnt) {
  1516. u32 h = sb->ORB_offset_hi_misc;
  1517. u32 r = STATUS_GET_RESP(h);
  1518. if (r != RESP_STATUS_REQUEST_COMPLETE) {
  1519. SBP2_INFO("resp 0x%x, sbp_status 0x%x",
  1520. r, STATUS_GET_SBP_STATUS(h));
  1521. scsi_status =
  1522. r == RESP_STATUS_TRANSPORT_FAILURE ?
  1523. SBP2_SCSI_STATUS_BUSY :
  1524. SBP2_SCSI_STATUS_COMMAND_TERMINATED;
  1525. }
  1526. if (STATUS_GET_LEN(h) > 1)
  1527. scsi_status = sbp2_status_to_sense_data(
  1528. (unchar *)sb, SCpnt->sense_buffer);
  1529. if (STATUS_TEST_DEAD(h))
  1530. sbp2_agent_reset(lu, 0);
  1531. }
  1532. /* Check here to see if there are no commands in-use. If there
  1533. * are none, we know that the fetch agent left the active state
  1534. * _and_ that we did not reactivate it yet. Therefore clear
  1535. * last_orb so that next time we write directly to the
  1536. * ORB_POINTER register. That way the fetch agent does not need
  1537. * to refetch the next_ORB. */
  1538. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1539. if (list_empty(&lu->cmd_orb_inuse))
  1540. lu->last_orb = NULL;
  1541. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1542. } else {
  1543. /* It's probably status after a management request. */
  1544. if ((sb->ORB_offset_lo == lu->reconnect_orb_dma) ||
  1545. (sb->ORB_offset_lo == lu->login_orb_dma) ||
  1546. (sb->ORB_offset_lo == lu->query_logins_orb_dma) ||
  1547. (sb->ORB_offset_lo == lu->logout_orb_dma)) {
  1548. lu->access_complete = 1;
  1549. wake_up_interruptible(&sbp2_access_wq);
  1550. }
  1551. }
  1552. if (SCpnt)
  1553. sbp2scsi_complete_command(lu, scsi_status, SCpnt,
  1554. cmd->Current_done);
  1555. return RCODE_COMPLETE;
  1556. }
  1557. /**************************************
  1558. * SCSI interface related section
  1559. **************************************/
  1560. static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
  1561. void (*done)(struct scsi_cmnd *))
  1562. {
  1563. struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
  1564. struct sbp2_fwhost_info *hi;
  1565. int result = DID_NO_CONNECT << 16;
  1566. if (unlikely(!sbp2util_node_is_available(lu)))
  1567. goto done;
  1568. hi = lu->hi;
  1569. if (unlikely(!hi)) {
  1570. SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
  1571. goto done;
  1572. }
  1573. /* Multiple units are currently represented to the SCSI core as separate
  1574. * targets, not as one target with multiple LUs. Therefore return
  1575. * selection time-out to any IO directed at non-zero LUNs. */
  1576. if (unlikely(SCpnt->device->lun))
  1577. goto done;
  1578. if (unlikely(!hpsb_node_entry_valid(lu->ne))) {
  1579. SBP2_ERR("Bus reset in progress - rejecting command");
  1580. result = DID_BUS_BUSY << 16;
  1581. goto done;
  1582. }
  1583. /* Bidirectional commands are not yet implemented,
  1584. * and unknown transfer direction not handled. */
  1585. if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
  1586. SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
  1587. result = DID_ERROR << 16;
  1588. goto done;
  1589. }
  1590. if (sbp2_send_command(lu, SCpnt, done)) {
  1591. SBP2_ERR("Error sending SCSI command");
  1592. sbp2scsi_complete_command(lu,
  1593. SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
  1594. SCpnt, done);
  1595. }
  1596. return 0;
  1597. done:
  1598. SCpnt->result = result;
  1599. done(SCpnt);
  1600. return 0;
  1601. }
  1602. static void sbp2scsi_complete_all_commands(struct sbp2_lu *lu, u32 status)
  1603. {
  1604. struct sbp2_fwhost_info *hi = lu->hi;
  1605. struct list_head *lh;
  1606. struct sbp2_command_info *cmd;
  1607. unsigned long flags;
  1608. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1609. while (!list_empty(&lu->cmd_orb_inuse)) {
  1610. lh = lu->cmd_orb_inuse.next;
  1611. cmd = list_entry(lh, struct sbp2_command_info, list);
  1612. dma_sync_single_for_cpu(hi->host->device.parent,
  1613. cmd->command_orb_dma,
  1614. sizeof(struct sbp2_command_orb),
  1615. DMA_TO_DEVICE);
  1616. dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
  1617. sizeof(cmd->scatter_gather_element),
  1618. DMA_TO_DEVICE);
  1619. sbp2util_mark_command_completed(lu, cmd);
  1620. if (cmd->Current_SCpnt) {
  1621. cmd->Current_SCpnt->result = status << 16;
  1622. cmd->Current_done(cmd->Current_SCpnt);
  1623. }
  1624. }
  1625. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1626. return;
  1627. }
  1628. /*
  1629. * Complete a regular SCSI command. Can be called in atomic context.
  1630. */
  1631. static void sbp2scsi_complete_command(struct sbp2_lu *lu, u32 scsi_status,
  1632. struct scsi_cmnd *SCpnt,
  1633. void (*done)(struct scsi_cmnd *))
  1634. {
  1635. if (!SCpnt) {
  1636. SBP2_ERR("SCpnt is NULL");
  1637. return;
  1638. }
  1639. switch (scsi_status) {
  1640. case SBP2_SCSI_STATUS_GOOD:
  1641. SCpnt->result = DID_OK << 16;
  1642. break;
  1643. case SBP2_SCSI_STATUS_BUSY:
  1644. SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
  1645. SCpnt->result = DID_BUS_BUSY << 16;
  1646. break;
  1647. case SBP2_SCSI_STATUS_CHECK_CONDITION:
  1648. SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
  1649. break;
  1650. case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
  1651. SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
  1652. SCpnt->result = DID_NO_CONNECT << 16;
  1653. scsi_print_command(SCpnt);
  1654. break;
  1655. case SBP2_SCSI_STATUS_CONDITION_MET:
  1656. case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
  1657. case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
  1658. SBP2_ERR("Bad SCSI status = %x", scsi_status);
  1659. SCpnt->result = DID_ERROR << 16;
  1660. scsi_print_command(SCpnt);
  1661. break;
  1662. default:
  1663. SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
  1664. SCpnt->result = DID_ERROR << 16;
  1665. }
  1666. /* If a bus reset is in progress and there was an error, complete
  1667. * the command as busy so that it will get retried. */
  1668. if (!hpsb_node_entry_valid(lu->ne)
  1669. && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
  1670. SBP2_ERR("Completing command with busy (bus reset)");
  1671. SCpnt->result = DID_BUS_BUSY << 16;
  1672. }
  1673. /* Tell the SCSI stack that we're done with this command. */
  1674. done(SCpnt);
  1675. }
  1676. static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
  1677. {
  1678. struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
  1679. lu->sdev = sdev;
  1680. sdev->allow_restart = 1;
  1681. if (lu->workarounds & SBP2_WORKAROUND_INQUIRY_36)
  1682. sdev->inquiry_len = 36;
  1683. return 0;
  1684. }
  1685. static int sbp2scsi_slave_configure(struct scsi_device *sdev)
  1686. {
  1687. struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
  1688. sdev->use_10_for_rw = 1;
  1689. if (sdev->type == TYPE_ROM)
  1690. sdev->use_10_for_ms = 1;
  1691. if (sdev->type == TYPE_DISK &&
  1692. lu->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
  1693. sdev->skip_ms_page_8 = 1;
  1694. if (lu->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
  1695. sdev->fix_capacity = 1;
  1696. return 0;
  1697. }
  1698. static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
  1699. {
  1700. ((struct sbp2_lu *)sdev->host->hostdata[0])->sdev = NULL;
  1701. return;
  1702. }
  1703. /*
  1704. * Called by scsi stack when something has really gone wrong.
  1705. * Usually called when a command has timed-out for some reason.
  1706. */
  1707. static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
  1708. {
  1709. struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
  1710. struct sbp2_fwhost_info *hi = lu->hi;
  1711. struct sbp2_command_info *cmd;
  1712. unsigned long flags;
  1713. SBP2_INFO("aborting sbp2 command");
  1714. scsi_print_command(SCpnt);
  1715. if (sbp2util_node_is_available(lu)) {
  1716. sbp2_agent_reset(lu, 1);
  1717. /* Return a matching command structure to the free pool. */
  1718. spin_lock_irqsave(&lu->cmd_orb_lock, flags);
  1719. cmd = sbp2util_find_command_for_SCpnt(lu, SCpnt);
  1720. if (cmd) {
  1721. dma_sync_single_for_cpu(hi->host->device.parent,
  1722. cmd->command_orb_dma,
  1723. sizeof(struct sbp2_command_orb),
  1724. DMA_TO_DEVICE);
  1725. dma_sync_single_for_cpu(hi->host->device.parent,
  1726. cmd->sge_dma,
  1727. sizeof(cmd->scatter_gather_element),
  1728. DMA_TO_DEVICE);
  1729. sbp2util_mark_command_completed(lu, cmd);
  1730. if (cmd->Current_SCpnt) {
  1731. cmd->Current_SCpnt->result = DID_ABORT << 16;
  1732. cmd->Current_done(cmd->Current_SCpnt);
  1733. }
  1734. }
  1735. spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
  1736. sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
  1737. }
  1738. return SUCCESS;
  1739. }
  1740. /*
  1741. * Called by scsi stack when something has really gone wrong.
  1742. */
  1743. static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
  1744. {
  1745. struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
  1746. SBP2_INFO("reset requested");
  1747. if (sbp2util_node_is_available(lu)) {
  1748. SBP2_INFO("generating sbp2 fetch agent reset");
  1749. sbp2_agent_reset(lu, 1);
  1750. }
  1751. return SUCCESS;
  1752. }
  1753. static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
  1754. struct device_attribute *attr,
  1755. char *buf)
  1756. {
  1757. struct scsi_device *sdev;
  1758. struct sbp2_lu *lu;
  1759. if (!(sdev = to_scsi_device(dev)))
  1760. return 0;
  1761. if (!(lu = (struct sbp2_lu *)sdev->host->hostdata[0]))
  1762. return 0;
  1763. if (sbp2_long_sysfs_ieee1394_id)
  1764. return sprintf(buf, "%016Lx:%06x:%04x\n",
  1765. (unsigned long long)lu->ne->guid,
  1766. lu->ud->directory_id, ORB_SET_LUN(lu->lun));
  1767. else
  1768. return sprintf(buf, "%016Lx:%d:%d\n",
  1769. (unsigned long long)lu->ne->guid,
  1770. lu->ud->id, ORB_SET_LUN(lu->lun));
  1771. }
  1772. MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
  1773. MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
  1774. MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
  1775. MODULE_LICENSE("GPL");
  1776. static int sbp2_module_init(void)
  1777. {
  1778. int ret;
  1779. if (sbp2_serialize_io) {
  1780. sbp2_shost_template.can_queue = 1;
  1781. sbp2_shost_template.cmd_per_lun = 1;
  1782. }
  1783. if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
  1784. (sbp2_max_sectors * 512) > (128 * 1024))
  1785. sbp2_max_sectors = 128 * 1024 / 512;
  1786. sbp2_shost_template.max_sectors = sbp2_max_sectors;
  1787. hpsb_register_highlevel(&sbp2_highlevel);
  1788. ret = hpsb_register_protocol(&sbp2_driver);
  1789. if (ret) {
  1790. SBP2_ERR("Failed to register protocol");
  1791. hpsb_unregister_highlevel(&sbp2_highlevel);
  1792. return ret;
  1793. }
  1794. return 0;
  1795. }
  1796. static void __exit sbp2_module_exit(void)
  1797. {
  1798. hpsb_unregister_protocol(&sbp2_driver);
  1799. hpsb_unregister_highlevel(&sbp2_highlevel);
  1800. }
  1801. module_init(sbp2_module_init);
  1802. module_exit(sbp2_module_exit);