sbp2.c 63 KB

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