sbp2.c 67 KB

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