sbp2.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713
  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 max_speed = IEEE1394_SPEED_MAX;
  94. module_param(max_speed, int, 0644);
  95. MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb, 1 = 200mb, 0 = 100mb)");
  96. /*
  97. * Set serialize_io to 1 if you'd like only one scsi command sent
  98. * down to us at a time (debugging). This might be necessary for very
  99. * badly behaved sbp2 devices.
  100. *
  101. * TODO: Make this configurable per device.
  102. */
  103. static int serialize_io = 1;
  104. module_param(serialize_io, int, 0444);
  105. MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers (default = 1, faster = 0)");
  106. /*
  107. * Bump up max_sectors if you'd like to support very large sized
  108. * transfers. Please note that some older sbp2 bridge chips are broken for
  109. * transfers greater or equal to 128KB. Default is a value of 255
  110. * sectors, or just under 128KB (at 512 byte sector size). I can note that
  111. * the Oxsemi sbp2 chipsets have no problems supporting very large
  112. * transfer sizes.
  113. */
  114. static int max_sectors = SBP2_MAX_SECTORS;
  115. module_param(max_sectors, int, 0444);
  116. MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = "
  117. __stringify(SBP2_MAX_SECTORS) ")");
  118. /*
  119. * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
  120. * do an exclusive login, as it's generally unsafe to have two hosts
  121. * talking to a single sbp2 device at the same time (filesystem coherency,
  122. * etc.). If you're running an sbp2 device that supports multiple logins,
  123. * and you're either running read-only filesystems or some sort of special
  124. * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
  125. * File System, or Lustre, then set exclusive_login to zero.
  126. *
  127. * So far only bridges from Oxford Semiconductor are known to support
  128. * concurrent logins. Depending on firmware, four or two concurrent logins
  129. * are possible on OXFW911 and newer Oxsemi bridges.
  130. */
  131. static int exclusive_login = 1;
  132. module_param(exclusive_login, int, 0644);
  133. MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)");
  134. /*
  135. * If any of the following workarounds is required for your device to work,
  136. * please submit the kernel messages logged by sbp2 to the linux1394-devel
  137. * mailing list.
  138. *
  139. * - 128kB max transfer
  140. * Limit transfer size. Necessary for some old bridges.
  141. *
  142. * - 36 byte inquiry
  143. * When scsi_mod probes the device, let the inquiry command look like that
  144. * from MS Windows.
  145. *
  146. * - skip mode page 8
  147. * Suppress sending of mode_sense for mode page 8 if the device pretends to
  148. * support the SCSI Primary Block commands instead of Reduced Block Commands.
  149. *
  150. * - fix capacity
  151. * Tell sd_mod to correct the last sector number reported by read_capacity.
  152. * Avoids access beyond actual disk limits on devices with an off-by-one bug.
  153. * Don't use this with devices which don't have this bug.
  154. *
  155. * - override internal blacklist
  156. * Instead of adding to the built-in blacklist, use only the workarounds
  157. * specified in the module load parameter.
  158. * Useful if a blacklist entry interfered with a non-broken device.
  159. */
  160. static int sbp2_default_workarounds;
  161. module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
  162. MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
  163. ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
  164. ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
  165. ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
  166. ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
  167. ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
  168. ", or a combination)");
  169. /*
  170. * Export information about protocols/devices supported by this driver.
  171. */
  172. static struct ieee1394_device_id sbp2_id_table[] = {
  173. {
  174. .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
  175. .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
  176. .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
  177. {}
  178. };
  179. MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
  180. /*
  181. * Debug levels, configured via kernel config, or enable here.
  182. */
  183. #define CONFIG_IEEE1394_SBP2_DEBUG 0
  184. /* #define CONFIG_IEEE1394_SBP2_DEBUG_ORBS */
  185. /* #define CONFIG_IEEE1394_SBP2_DEBUG_DMA */
  186. /* #define CONFIG_IEEE1394_SBP2_DEBUG 1 */
  187. /* #define CONFIG_IEEE1394_SBP2_DEBUG 2 */
  188. /* #define CONFIG_IEEE1394_SBP2_PACKET_DUMP */
  189. #ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS
  190. #define SBP2_ORB_DEBUG(fmt, args...) HPSB_ERR("sbp2(%s): "fmt, __FUNCTION__, ## args)
  191. static u32 global_outstanding_command_orbs = 0;
  192. #define outstanding_orb_incr global_outstanding_command_orbs++
  193. #define outstanding_orb_decr global_outstanding_command_orbs--
  194. #else
  195. #define SBP2_ORB_DEBUG(fmt, args...) do {} while (0)
  196. #define outstanding_orb_incr do {} while (0)
  197. #define outstanding_orb_decr do {} while (0)
  198. #endif
  199. #ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
  200. #define SBP2_DMA_ALLOC(fmt, args...) \
  201. HPSB_ERR("sbp2(%s)alloc(%d): "fmt, __FUNCTION__, \
  202. ++global_outstanding_dmas, ## args)
  203. #define SBP2_DMA_FREE(fmt, args...) \
  204. HPSB_ERR("sbp2(%s)free(%d): "fmt, __FUNCTION__, \
  205. --global_outstanding_dmas, ## args)
  206. static u32 global_outstanding_dmas = 0;
  207. #else
  208. #define SBP2_DMA_ALLOC(fmt, args...) do {} while (0)
  209. #define SBP2_DMA_FREE(fmt, args...) do {} while (0)
  210. #endif
  211. #if CONFIG_IEEE1394_SBP2_DEBUG >= 2
  212. #define SBP2_DEBUG(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  213. #define SBP2_INFO(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  214. #define SBP2_NOTICE(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  215. #define SBP2_WARN(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  216. #elif CONFIG_IEEE1394_SBP2_DEBUG == 1
  217. #define SBP2_DEBUG(fmt, args...) HPSB_DEBUG("sbp2: "fmt, ## args)
  218. #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
  219. #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
  220. #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
  221. #else
  222. #define SBP2_DEBUG(fmt, args...) do {} while (0)
  223. #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
  224. #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
  225. #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
  226. #endif
  227. #define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
  228. #define SBP2_DEBUG_ENTER() SBP2_DEBUG("%s", __FUNCTION__)
  229. /*
  230. * Globals
  231. */
  232. static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
  233. u32 status);
  234. static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
  235. u32 scsi_status, struct scsi_cmnd *SCpnt,
  236. void (*done)(struct scsi_cmnd *));
  237. static struct scsi_host_template scsi_driver_template;
  238. static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
  239. static void sbp2_host_reset(struct hpsb_host *host);
  240. static int sbp2_probe(struct device *dev);
  241. static int sbp2_remove(struct device *dev);
  242. static int sbp2_update(struct unit_directory *ud);
  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 struct hpsb_address_ops sbp2_physdma_ops = {
  252. .read = sbp2_handle_physdma_read,
  253. .write = sbp2_handle_physdma_write,
  254. };
  255. #endif
  256. static struct hpsb_protocol_driver sbp2_driver = {
  257. .name = "SBP2 Driver",
  258. .id_table = sbp2_id_table,
  259. .update = sbp2_update,
  260. .driver = {
  261. .name = SBP2_DEVICE_NAME,
  262. .bus = &ieee1394_bus_type,
  263. .probe = sbp2_probe,
  264. .remove = sbp2_remove,
  265. },
  266. };
  267. /*
  268. * List of devices with known bugs.
  269. *
  270. * The firmware_revision field, masked with 0xffff00, is the best indicator
  271. * for the type of bridge chip of a device. It yields a few false positives
  272. * but this did not break correctly behaving devices so far.
  273. */
  274. static const struct {
  275. u32 firmware_revision;
  276. u32 model_id;
  277. unsigned workarounds;
  278. } sbp2_workarounds_table[] = {
  279. /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
  280. .firmware_revision = 0x002800,
  281. .model_id = 0x001010,
  282. .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
  283. SBP2_WORKAROUND_MODE_SENSE_8,
  284. },
  285. /* Initio bridges, actually only needed for some older ones */ {
  286. .firmware_revision = 0x000200,
  287. .workarounds = SBP2_WORKAROUND_INQUIRY_36,
  288. },
  289. /* Symbios bridge */ {
  290. .firmware_revision = 0xa0b800,
  291. .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
  292. },
  293. /*
  294. * Note about the following Apple iPod blacklist entries:
  295. *
  296. * There are iPods (2nd gen, 3rd gen) with model_id==0. Since our
  297. * matching logic treats 0 as a wildcard, we cannot match this ID
  298. * without rewriting the matching routine. Fortunately these iPods
  299. * do not feature the read_capacity bug according to one report.
  300. * Read_capacity behaviour as well as model_id could change due to
  301. * Apple-supplied firmware updates though.
  302. */
  303. /* iPod 4th generation */ {
  304. .firmware_revision = 0x0a2700,
  305. .model_id = 0x000021,
  306. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  307. },
  308. /* iPod mini */ {
  309. .firmware_revision = 0x0a2700,
  310. .model_id = 0x000023,
  311. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  312. },
  313. /* iPod Photo */ {
  314. .firmware_revision = 0x0a2700,
  315. .model_id = 0x00007e,
  316. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  317. }
  318. };
  319. /**************************************
  320. * General utility functions
  321. **************************************/
  322. #ifndef __BIG_ENDIAN
  323. /*
  324. * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
  325. */
  326. static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
  327. {
  328. u32 *temp = buffer;
  329. for (length = (length >> 2); length--; )
  330. temp[length] = be32_to_cpu(temp[length]);
  331. return;
  332. }
  333. /*
  334. * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
  335. */
  336. static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
  337. {
  338. u32 *temp = buffer;
  339. for (length = (length >> 2); length--; )
  340. temp[length] = cpu_to_be32(temp[length]);
  341. return;
  342. }
  343. #else /* BIG_ENDIAN */
  344. /* Why waste the cpu cycles? */
  345. #define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
  346. #define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
  347. #endif
  348. #ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP
  349. /*
  350. * Debug packet dump routine. Length is in bytes.
  351. */
  352. static void sbp2util_packet_dump(void *buffer, int length, char *dump_name,
  353. u32 dump_phys_addr)
  354. {
  355. int i;
  356. unsigned char *dump = buffer;
  357. if (!dump || !length || !dump_name)
  358. return;
  359. if (dump_phys_addr)
  360. printk("[%s, 0x%x]", dump_name, dump_phys_addr);
  361. else
  362. printk("[%s]", dump_name);
  363. for (i = 0; i < length; i++) {
  364. if (i > 0x3f) {
  365. printk("\n ...");
  366. break;
  367. }
  368. if ((i & 0x3) == 0)
  369. printk(" ");
  370. if ((i & 0xf) == 0)
  371. printk("\n ");
  372. printk("%02x ", (int)dump[i]);
  373. }
  374. printk("\n");
  375. return;
  376. }
  377. #else
  378. #define sbp2util_packet_dump(w,x,y,z) do {} while (0)
  379. #endif
  380. static DECLARE_WAIT_QUEUE_HEAD(access_wq);
  381. /*
  382. * Waits for completion of an SBP-2 access request.
  383. * Returns nonzero if timed out or prematurely interrupted.
  384. */
  385. static int sbp2util_access_timeout(struct scsi_id_instance_data *scsi_id,
  386. int timeout)
  387. {
  388. long leftover = wait_event_interruptible_timeout(
  389. access_wq, scsi_id->access_complete, timeout);
  390. scsi_id->access_complete = 0;
  391. return leftover <= 0;
  392. }
  393. /* Frees an allocated packet */
  394. static void sbp2_free_packet(struct hpsb_packet *packet)
  395. {
  396. hpsb_free_tlabel(packet);
  397. hpsb_free_packet(packet);
  398. }
  399. /* This is much like hpsb_node_write(), except it ignores the response
  400. * subaction and returns immediately. Can be used from interrupts.
  401. */
  402. static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
  403. quadlet_t *buffer, size_t length)
  404. {
  405. struct hpsb_packet *packet;
  406. packet = hpsb_make_writepacket(ne->host, ne->nodeid,
  407. addr, buffer, length);
  408. if (!packet)
  409. return -ENOMEM;
  410. hpsb_set_packet_complete_task(packet,
  411. (void (*)(void *))sbp2_free_packet,
  412. packet);
  413. hpsb_node_fill_packet(ne, packet);
  414. if (hpsb_send_packet(packet) < 0) {
  415. sbp2_free_packet(packet);
  416. return -EIO;
  417. }
  418. return 0;
  419. }
  420. static void sbp2util_notify_fetch_agent(struct scsi_id_instance_data *scsi_id,
  421. u64 offset, quadlet_t *data, size_t len)
  422. {
  423. /*
  424. * There is a small window after a bus reset within which the node
  425. * entry's generation is current but the reconnect wasn't completed.
  426. */
  427. if (unlikely(atomic_read(&scsi_id->state) == SBP2LU_STATE_IN_RESET))
  428. return;
  429. if (hpsb_node_write(scsi_id->ne,
  430. scsi_id->sbp2_command_block_agent_addr + offset,
  431. data, len))
  432. SBP2_ERR("sbp2util_notify_fetch_agent failed.");
  433. /*
  434. * Now accept new SCSI commands, unless a bus reset happended during
  435. * hpsb_node_write.
  436. */
  437. if (likely(atomic_read(&scsi_id->state) != SBP2LU_STATE_IN_RESET))
  438. scsi_unblock_requests(scsi_id->scsi_host);
  439. }
  440. static void sbp2util_write_orb_pointer(struct work_struct *work)
  441. {
  442. struct scsi_id_instance_data *scsi_id =
  443. container_of(work, struct scsi_id_instance_data,
  444. protocol_work.work);
  445. quadlet_t data[2];
  446. data[0] = ORB_SET_NODE_ID(scsi_id->hi->host->node_id);
  447. data[1] = scsi_id->last_orb_dma;
  448. sbp2util_cpu_to_be32_buffer(data, 8);
  449. sbp2util_notify_fetch_agent(scsi_id, SBP2_ORB_POINTER_OFFSET, data, 8);
  450. }
  451. static void sbp2util_write_doorbell(struct work_struct *work)
  452. {
  453. struct scsi_id_instance_data *scsi_id =
  454. container_of(work, struct scsi_id_instance_data,
  455. protocol_work.work);
  456. sbp2util_notify_fetch_agent(scsi_id, SBP2_DOORBELL_OFFSET, NULL, 4);
  457. }
  458. /*
  459. * This function is called to create a pool of command orbs used for
  460. * command processing. It is called when a new sbp2 device is detected.
  461. */
  462. static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
  463. {
  464. struct sbp2scsi_host_info *hi = scsi_id->hi;
  465. int i;
  466. unsigned long flags, orbs;
  467. struct sbp2_command_info *command;
  468. orbs = serialize_io ? 2 : SBP2_MAX_CMDS;
  469. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  470. for (i = 0; i < orbs; i++) {
  471. command = kzalloc(sizeof(*command), GFP_ATOMIC);
  472. if (!command) {
  473. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock,
  474. flags);
  475. return -ENOMEM;
  476. }
  477. command->command_orb_dma =
  478. pci_map_single(hi->host->pdev, &command->command_orb,
  479. sizeof(struct sbp2_command_orb),
  480. PCI_DMA_TODEVICE);
  481. SBP2_DMA_ALLOC("single command orb DMA");
  482. command->sge_dma =
  483. pci_map_single(hi->host->pdev,
  484. &command->scatter_gather_element,
  485. sizeof(command->scatter_gather_element),
  486. PCI_DMA_BIDIRECTIONAL);
  487. SBP2_DMA_ALLOC("scatter_gather_element");
  488. INIT_LIST_HEAD(&command->list);
  489. list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
  490. }
  491. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  492. return 0;
  493. }
  494. /*
  495. * This function is called to delete a pool of command orbs.
  496. */
  497. static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
  498. {
  499. struct hpsb_host *host = scsi_id->hi->host;
  500. struct list_head *lh, *next;
  501. struct sbp2_command_info *command;
  502. unsigned long flags;
  503. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  504. if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
  505. list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) {
  506. command = list_entry(lh, struct sbp2_command_info, list);
  507. /* Release our generic DMA's */
  508. pci_unmap_single(host->pdev, command->command_orb_dma,
  509. sizeof(struct sbp2_command_orb),
  510. PCI_DMA_TODEVICE);
  511. SBP2_DMA_FREE("single command orb DMA");
  512. pci_unmap_single(host->pdev, command->sge_dma,
  513. sizeof(command->scatter_gather_element),
  514. PCI_DMA_BIDIRECTIONAL);
  515. SBP2_DMA_FREE("scatter_gather_element");
  516. kfree(command);
  517. }
  518. }
  519. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  520. return;
  521. }
  522. /*
  523. * This function finds the sbp2_command for a given outstanding command
  524. * orb.Only looks at the inuse list.
  525. */
  526. static struct sbp2_command_info *sbp2util_find_command_for_orb(
  527. struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
  528. {
  529. struct sbp2_command_info *command;
  530. unsigned long flags;
  531. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  532. if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
  533. list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) {
  534. if (command->command_orb_dma == orb) {
  535. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  536. return command;
  537. }
  538. }
  539. }
  540. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  541. SBP2_ORB_DEBUG("could not match command orb %x", (unsigned int)orb);
  542. return NULL;
  543. }
  544. /*
  545. * This function finds the sbp2_command for a given outstanding SCpnt.
  546. * Only looks at the inuse list.
  547. * Must be called with scsi_id->sbp2_command_orb_lock held.
  548. */
  549. static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
  550. struct scsi_id_instance_data *scsi_id, void *SCpnt)
  551. {
  552. struct sbp2_command_info *command;
  553. if (!list_empty(&scsi_id->sbp2_command_orb_inuse))
  554. list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list)
  555. if (command->Current_SCpnt == SCpnt)
  556. return command;
  557. return NULL;
  558. }
  559. /*
  560. * This function allocates a command orb used to send a scsi command.
  561. */
  562. static struct sbp2_command_info *sbp2util_allocate_command_orb(
  563. struct scsi_id_instance_data *scsi_id,
  564. struct scsi_cmnd *Current_SCpnt,
  565. void (*Current_done)(struct scsi_cmnd *))
  566. {
  567. struct list_head *lh;
  568. struct sbp2_command_info *command = NULL;
  569. unsigned long flags;
  570. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  571. if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
  572. lh = scsi_id->sbp2_command_orb_completed.next;
  573. list_del(lh);
  574. command = list_entry(lh, struct sbp2_command_info, list);
  575. command->Current_done = Current_done;
  576. command->Current_SCpnt = Current_SCpnt;
  577. list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
  578. } else {
  579. SBP2_ERR("%s: no orbs available", __FUNCTION__);
  580. }
  581. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  582. return command;
  583. }
  584. /* Free our DMA's */
  585. static void sbp2util_free_command_dma(struct sbp2_command_info *command)
  586. {
  587. struct scsi_id_instance_data *scsi_id =
  588. (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
  589. struct hpsb_host *host;
  590. if (!scsi_id) {
  591. SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
  592. return;
  593. }
  594. host = scsi_id->ud->ne->host;
  595. if (command->cmd_dma) {
  596. if (command->dma_type == CMD_DMA_SINGLE) {
  597. pci_unmap_single(host->pdev, command->cmd_dma,
  598. command->dma_size, command->dma_dir);
  599. SBP2_DMA_FREE("single bulk");
  600. } else if (command->dma_type == CMD_DMA_PAGE) {
  601. pci_unmap_page(host->pdev, command->cmd_dma,
  602. command->dma_size, command->dma_dir);
  603. SBP2_DMA_FREE("single page");
  604. } /* XXX: Check for CMD_DMA_NONE bug */
  605. command->dma_type = CMD_DMA_NONE;
  606. command->cmd_dma = 0;
  607. }
  608. if (command->sge_buffer) {
  609. pci_unmap_sg(host->pdev, command->sge_buffer,
  610. command->dma_size, command->dma_dir);
  611. SBP2_DMA_FREE("scatter list");
  612. command->sge_buffer = NULL;
  613. }
  614. }
  615. /*
  616. * This function moves a command to the completed orb list.
  617. * Must be called with scsi_id->sbp2_command_orb_lock held.
  618. */
  619. static void sbp2util_mark_command_completed(
  620. struct scsi_id_instance_data *scsi_id,
  621. struct sbp2_command_info *command)
  622. {
  623. list_del(&command->list);
  624. sbp2util_free_command_dma(command);
  625. list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
  626. }
  627. /*
  628. * Is scsi_id valid? Is the 1394 node still present?
  629. */
  630. static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id)
  631. {
  632. return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo;
  633. }
  634. /*********************************************
  635. * IEEE-1394 core driver stack related section
  636. *********************************************/
  637. static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud);
  638. static int sbp2_probe(struct device *dev)
  639. {
  640. struct unit_directory *ud;
  641. struct scsi_id_instance_data *scsi_id;
  642. SBP2_DEBUG_ENTER();
  643. ud = container_of(dev, struct unit_directory, device);
  644. /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
  645. * instead. */
  646. if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
  647. return -ENODEV;
  648. scsi_id = sbp2_alloc_device(ud);
  649. if (!scsi_id)
  650. return -ENOMEM;
  651. sbp2_parse_unit_directory(scsi_id, ud);
  652. return sbp2_start_device(scsi_id);
  653. }
  654. static int sbp2_remove(struct device *dev)
  655. {
  656. struct unit_directory *ud;
  657. struct scsi_id_instance_data *scsi_id;
  658. struct scsi_device *sdev;
  659. SBP2_DEBUG_ENTER();
  660. ud = container_of(dev, struct unit_directory, device);
  661. scsi_id = ud->device.driver_data;
  662. if (!scsi_id)
  663. return 0;
  664. if (scsi_id->scsi_host) {
  665. /* Get rid of enqueued commands if there is no chance to
  666. * send them. */
  667. if (!sbp2util_node_is_available(scsi_id))
  668. sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
  669. /* scsi_remove_device() will trigger shutdown functions of SCSI
  670. * highlevel drivers which would deadlock if blocked. */
  671. atomic_set(&scsi_id->state, SBP2LU_STATE_IN_SHUTDOWN);
  672. scsi_unblock_requests(scsi_id->scsi_host);
  673. }
  674. sdev = scsi_id->sdev;
  675. if (sdev) {
  676. scsi_id->sdev = NULL;
  677. scsi_remove_device(sdev);
  678. }
  679. sbp2_logout_device(scsi_id);
  680. sbp2_remove_device(scsi_id);
  681. return 0;
  682. }
  683. static int sbp2_update(struct unit_directory *ud)
  684. {
  685. struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
  686. SBP2_DEBUG_ENTER();
  687. if (sbp2_reconnect_device(scsi_id)) {
  688. /*
  689. * Ok, reconnect has failed. Perhaps we didn't
  690. * reconnect fast enough. Try doing a regular login, but
  691. * first do a logout just in case of any weirdness.
  692. */
  693. sbp2_logout_device(scsi_id);
  694. if (sbp2_login_device(scsi_id)) {
  695. /* Login failed too, just fail, and the backend
  696. * will call our sbp2_remove for us */
  697. SBP2_ERR("Failed to reconnect to sbp2 device!");
  698. return -EBUSY;
  699. }
  700. }
  701. /* Set max retries to something large on the device. */
  702. sbp2_set_busy_timeout(scsi_id);
  703. /* Do a SBP-2 fetch agent reset. */
  704. sbp2_agent_reset(scsi_id, 1);
  705. /* Get the max speed and packet size that we can use. */
  706. sbp2_max_speed_and_size(scsi_id);
  707. /* Complete any pending commands with busy (so they get
  708. * retried) and remove them from our queue
  709. */
  710. sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
  711. /* Accept new commands unless there was another bus reset in the
  712. * meantime. */
  713. if (hpsb_node_entry_valid(scsi_id->ne)) {
  714. atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
  715. scsi_unblock_requests(scsi_id->scsi_host);
  716. }
  717. return 0;
  718. }
  719. /* This functions is called by the sbp2_probe, for each new device. We now
  720. * allocate one scsi host for each scsi_id (unit directory). */
  721. static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
  722. {
  723. struct sbp2scsi_host_info *hi;
  724. struct Scsi_Host *scsi_host = NULL;
  725. struct scsi_id_instance_data *scsi_id = NULL;
  726. SBP2_DEBUG_ENTER();
  727. scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL);
  728. if (!scsi_id) {
  729. SBP2_ERR("failed to create scsi_id");
  730. goto failed_alloc;
  731. }
  732. scsi_id->ne = ud->ne;
  733. scsi_id->ud = ud;
  734. scsi_id->speed_code = IEEE1394_SPEED_100;
  735. scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
  736. scsi_id->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
  737. INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
  738. INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
  739. INIT_LIST_HEAD(&scsi_id->scsi_list);
  740. spin_lock_init(&scsi_id->sbp2_command_orb_lock);
  741. atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
  742. INIT_DELAYED_WORK(&scsi_id->protocol_work, NULL);
  743. ud->device.driver_data = scsi_id;
  744. hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
  745. if (!hi) {
  746. hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi));
  747. if (!hi) {
  748. SBP2_ERR("failed to allocate hostinfo");
  749. goto failed_alloc;
  750. }
  751. SBP2_DEBUG("sbp2_alloc_device: allocated hostinfo");
  752. hi->host = ud->ne->host;
  753. INIT_LIST_HEAD(&hi->scsi_ids);
  754. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  755. /* Handle data movement if physical dma is not
  756. * enabled or not supported on host controller */
  757. if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
  758. &sbp2_physdma_ops,
  759. 0x0ULL, 0xfffffffcULL)) {
  760. SBP2_ERR("failed to register lower 4GB address range");
  761. goto failed_alloc;
  762. }
  763. #endif
  764. }
  765. /* Prevent unloading of the 1394 host */
  766. if (!try_module_get(hi->host->driver->owner)) {
  767. SBP2_ERR("failed to get a reference on 1394 host driver");
  768. goto failed_alloc;
  769. }
  770. scsi_id->hi = hi;
  771. list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
  772. /* Register the status FIFO address range. We could use the same FIFO
  773. * for targets at different nodes. However we need different FIFOs per
  774. * target in order to support multi-unit devices.
  775. * The FIFO is located out of the local host controller's physical range
  776. * but, if possible, within the posted write area. Status writes will
  777. * then be performed as unified transactions. This slightly reduces
  778. * bandwidth usage, and some Prolific based devices seem to require it.
  779. */
  780. scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace(
  781. &sbp2_highlevel, ud->ne->host, &sbp2_ops,
  782. sizeof(struct sbp2_status_block), sizeof(quadlet_t),
  783. ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
  784. if (scsi_id->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
  785. SBP2_ERR("failed to allocate status FIFO address range");
  786. goto failed_alloc;
  787. }
  788. /* Register our host with the SCSI stack. */
  789. scsi_host = scsi_host_alloc(&scsi_driver_template,
  790. sizeof(unsigned long));
  791. if (!scsi_host) {
  792. SBP2_ERR("failed to register scsi host");
  793. goto failed_alloc;
  794. }
  795. scsi_host->hostdata[0] = (unsigned long)scsi_id;
  796. if (!scsi_add_host(scsi_host, &ud->device)) {
  797. scsi_id->scsi_host = scsi_host;
  798. return scsi_id;
  799. }
  800. SBP2_ERR("failed to add scsi host");
  801. scsi_host_put(scsi_host);
  802. failed_alloc:
  803. sbp2_remove_device(scsi_id);
  804. return NULL;
  805. }
  806. static void sbp2_host_reset(struct hpsb_host *host)
  807. {
  808. struct sbp2scsi_host_info *hi;
  809. struct scsi_id_instance_data *scsi_id;
  810. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  811. if (!hi)
  812. return;
  813. list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
  814. if (likely(atomic_read(&scsi_id->state) !=
  815. SBP2LU_STATE_IN_SHUTDOWN)) {
  816. atomic_set(&scsi_id->state, SBP2LU_STATE_IN_RESET);
  817. scsi_block_requests(scsi_id->scsi_host);
  818. }
  819. }
  820. /*
  821. * This function is where we first pull the node unique ids, and then
  822. * allocate memory and register a SBP-2 device.
  823. */
  824. static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
  825. {
  826. struct sbp2scsi_host_info *hi = scsi_id->hi;
  827. int error;
  828. SBP2_DEBUG_ENTER();
  829. /* Login FIFO DMA */
  830. scsi_id->login_response =
  831. pci_alloc_consistent(hi->host->pdev,
  832. sizeof(struct sbp2_login_response),
  833. &scsi_id->login_response_dma);
  834. if (!scsi_id->login_response)
  835. goto alloc_fail;
  836. SBP2_DMA_ALLOC("consistent DMA region for login FIFO");
  837. /* Query logins ORB DMA */
  838. scsi_id->query_logins_orb =
  839. pci_alloc_consistent(hi->host->pdev,
  840. sizeof(struct sbp2_query_logins_orb),
  841. &scsi_id->query_logins_orb_dma);
  842. if (!scsi_id->query_logins_orb)
  843. goto alloc_fail;
  844. SBP2_DMA_ALLOC("consistent DMA region for query logins ORB");
  845. /* Query logins response DMA */
  846. scsi_id->query_logins_response =
  847. pci_alloc_consistent(hi->host->pdev,
  848. sizeof(struct sbp2_query_logins_response),
  849. &scsi_id->query_logins_response_dma);
  850. if (!scsi_id->query_logins_response)
  851. goto alloc_fail;
  852. SBP2_DMA_ALLOC("consistent DMA region for query logins response");
  853. /* Reconnect ORB DMA */
  854. scsi_id->reconnect_orb =
  855. pci_alloc_consistent(hi->host->pdev,
  856. sizeof(struct sbp2_reconnect_orb),
  857. &scsi_id->reconnect_orb_dma);
  858. if (!scsi_id->reconnect_orb)
  859. goto alloc_fail;
  860. SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB");
  861. /* Logout ORB DMA */
  862. scsi_id->logout_orb =
  863. pci_alloc_consistent(hi->host->pdev,
  864. sizeof(struct sbp2_logout_orb),
  865. &scsi_id->logout_orb_dma);
  866. if (!scsi_id->logout_orb)
  867. goto alloc_fail;
  868. SBP2_DMA_ALLOC("consistent DMA region for logout ORB");
  869. /* Login ORB DMA */
  870. scsi_id->login_orb =
  871. pci_alloc_consistent(hi->host->pdev,
  872. sizeof(struct sbp2_login_orb),
  873. &scsi_id->login_orb_dma);
  874. if (!scsi_id->login_orb)
  875. goto alloc_fail;
  876. SBP2_DMA_ALLOC("consistent DMA region for login ORB");
  877. SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", scsi_id->ud->id);
  878. /*
  879. * Create our command orb pool
  880. */
  881. if (sbp2util_create_command_orb_pool(scsi_id)) {
  882. SBP2_ERR("sbp2util_create_command_orb_pool failed!");
  883. sbp2_remove_device(scsi_id);
  884. return -ENOMEM;
  885. }
  886. /* Schedule a timeout here. The reason is that we may be so close
  887. * to a bus reset, that the device is not available for logins.
  888. * This can happen when the bus reset is caused by the host
  889. * connected to the sbp2 device being removed. That host would
  890. * have a certain amount of time to relogin before the sbp2 device
  891. * allows someone else to login instead. One second makes sense. */
  892. if (msleep_interruptible(1000)) {
  893. sbp2_remove_device(scsi_id);
  894. return -EINTR;
  895. }
  896. /*
  897. * Login to the sbp-2 device
  898. */
  899. if (sbp2_login_device(scsi_id)) {
  900. /* Login failed, just remove the device. */
  901. sbp2_remove_device(scsi_id);
  902. return -EBUSY;
  903. }
  904. /*
  905. * Set max retries to something large on the device
  906. */
  907. sbp2_set_busy_timeout(scsi_id);
  908. /*
  909. * Do a SBP-2 fetch agent reset
  910. */
  911. sbp2_agent_reset(scsi_id, 1);
  912. /*
  913. * Get the max speed and packet size that we can use
  914. */
  915. sbp2_max_speed_and_size(scsi_id);
  916. /* Add this device to the scsi layer now */
  917. error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
  918. if (error) {
  919. SBP2_ERR("scsi_add_device failed");
  920. sbp2_logout_device(scsi_id);
  921. sbp2_remove_device(scsi_id);
  922. return error;
  923. }
  924. return 0;
  925. alloc_fail:
  926. SBP2_ERR("Could not allocate memory for scsi_id");
  927. sbp2_remove_device(scsi_id);
  928. return -ENOMEM;
  929. }
  930. /*
  931. * This function removes an sbp2 device from the sbp2scsi_host_info struct.
  932. */
  933. static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
  934. {
  935. struct sbp2scsi_host_info *hi;
  936. SBP2_DEBUG_ENTER();
  937. if (!scsi_id)
  938. return;
  939. hi = scsi_id->hi;
  940. /* This will remove our scsi device aswell */
  941. if (scsi_id->scsi_host) {
  942. scsi_remove_host(scsi_id->scsi_host);
  943. scsi_host_put(scsi_id->scsi_host);
  944. }
  945. flush_scheduled_work();
  946. sbp2util_remove_command_orb_pool(scsi_id);
  947. list_del(&scsi_id->scsi_list);
  948. if (scsi_id->login_response) {
  949. pci_free_consistent(hi->host->pdev,
  950. sizeof(struct sbp2_login_response),
  951. scsi_id->login_response,
  952. scsi_id->login_response_dma);
  953. SBP2_DMA_FREE("single login FIFO");
  954. }
  955. if (scsi_id->login_orb) {
  956. pci_free_consistent(hi->host->pdev,
  957. sizeof(struct sbp2_login_orb),
  958. scsi_id->login_orb,
  959. scsi_id->login_orb_dma);
  960. SBP2_DMA_FREE("single login ORB");
  961. }
  962. if (scsi_id->reconnect_orb) {
  963. pci_free_consistent(hi->host->pdev,
  964. sizeof(struct sbp2_reconnect_orb),
  965. scsi_id->reconnect_orb,
  966. scsi_id->reconnect_orb_dma);
  967. SBP2_DMA_FREE("single reconnect orb");
  968. }
  969. if (scsi_id->logout_orb) {
  970. pci_free_consistent(hi->host->pdev,
  971. sizeof(struct sbp2_logout_orb),
  972. scsi_id->logout_orb,
  973. scsi_id->logout_orb_dma);
  974. SBP2_DMA_FREE("single logout orb");
  975. }
  976. if (scsi_id->query_logins_orb) {
  977. pci_free_consistent(hi->host->pdev,
  978. sizeof(struct sbp2_query_logins_orb),
  979. scsi_id->query_logins_orb,
  980. scsi_id->query_logins_orb_dma);
  981. SBP2_DMA_FREE("single query logins orb");
  982. }
  983. if (scsi_id->query_logins_response) {
  984. pci_free_consistent(hi->host->pdev,
  985. sizeof(struct sbp2_query_logins_response),
  986. scsi_id->query_logins_response,
  987. scsi_id->query_logins_response_dma);
  988. SBP2_DMA_FREE("single query logins data");
  989. }
  990. if (scsi_id->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
  991. hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
  992. scsi_id->status_fifo_addr);
  993. scsi_id->ud->device.driver_data = NULL;
  994. if (hi)
  995. module_put(hi->host->driver->owner);
  996. SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->ud->id);
  997. kfree(scsi_id);
  998. }
  999. #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
  1000. /*
  1001. * This function deals with physical dma write requests (for adapters that do not support
  1002. * physical dma in hardware). Mostly just here for debugging...
  1003. */
  1004. static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
  1005. int destid, quadlet_t *data, u64 addr,
  1006. size_t length, u16 flags)
  1007. {
  1008. /*
  1009. * Manually put the data in the right place.
  1010. */
  1011. memcpy(bus_to_virt((u32) addr), data, length);
  1012. sbp2util_packet_dump(data, length, "sbp2 phys dma write by device",
  1013. (u32) addr);
  1014. return RCODE_COMPLETE;
  1015. }
  1016. /*
  1017. * This function deals with physical dma read requests (for adapters that do not support
  1018. * physical dma in hardware). Mostly just here for debugging...
  1019. */
  1020. static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
  1021. quadlet_t *data, u64 addr, size_t length,
  1022. u16 flags)
  1023. {
  1024. /*
  1025. * Grab data from memory and send a read response.
  1026. */
  1027. memcpy(data, bus_to_virt((u32) addr), length);
  1028. sbp2util_packet_dump(data, length, "sbp2 phys dma read by device",
  1029. (u32) addr);
  1030. return RCODE_COMPLETE;
  1031. }
  1032. #endif
  1033. /**************************************
  1034. * SBP-2 protocol related section
  1035. **************************************/
  1036. /*
  1037. * This function queries the device for the maximum concurrent logins it
  1038. * supports.
  1039. */
  1040. static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
  1041. {
  1042. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1043. quadlet_t data[2];
  1044. int max_logins;
  1045. int active_logins;
  1046. SBP2_DEBUG_ENTER();
  1047. scsi_id->query_logins_orb->reserved1 = 0x0;
  1048. scsi_id->query_logins_orb->reserved2 = 0x0;
  1049. scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
  1050. scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1051. scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
  1052. scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
  1053. scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
  1054. scsi_id->query_logins_orb->reserved_resp_length =
  1055. ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
  1056. scsi_id->query_logins_orb->status_fifo_hi =
  1057. ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
  1058. scsi_id->query_logins_orb->status_fifo_lo =
  1059. ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
  1060. sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
  1061. sbp2util_packet_dump(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb),
  1062. "sbp2 query logins orb", scsi_id->query_logins_orb_dma);
  1063. memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
  1064. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1065. data[1] = scsi_id->query_logins_orb_dma;
  1066. sbp2util_cpu_to_be32_buffer(data, 8);
  1067. hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
  1068. if (sbp2util_access_timeout(scsi_id, 2*HZ)) {
  1069. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  1070. return -EIO;
  1071. }
  1072. if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
  1073. SBP2_INFO("Error querying logins to SBP-2 device - timed out");
  1074. return -EIO;
  1075. }
  1076. if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
  1077. SBP2_INFO("Error querying logins to SBP-2 device - failed");
  1078. return -EIO;
  1079. }
  1080. sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
  1081. SBP2_DEBUG("length_max_logins = %x",
  1082. (unsigned int)scsi_id->query_logins_response->length_max_logins);
  1083. max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
  1084. SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
  1085. active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
  1086. SBP2_INFO("Number of active logins: %d", active_logins);
  1087. if (active_logins >= max_logins) {
  1088. return -EIO;
  1089. }
  1090. return 0;
  1091. }
  1092. /*
  1093. * This function is called in order to login to a particular SBP-2 device,
  1094. * after a bus reset.
  1095. */
  1096. static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
  1097. {
  1098. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1099. quadlet_t data[2];
  1100. SBP2_DEBUG_ENTER();
  1101. if (!scsi_id->login_orb) {
  1102. SBP2_DEBUG("%s: login_orb not alloc'd!", __FUNCTION__);
  1103. return -EIO;
  1104. }
  1105. if (!exclusive_login) {
  1106. if (sbp2_query_logins(scsi_id)) {
  1107. SBP2_INFO("Device does not support any more concurrent logins");
  1108. return -EIO;
  1109. }
  1110. }
  1111. /* Set-up login ORB, assume no password */
  1112. scsi_id->login_orb->password_hi = 0;
  1113. scsi_id->login_orb->password_lo = 0;
  1114. scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
  1115. scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1116. scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
  1117. scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0); /* One second reconnect time */
  1118. scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login); /* Exclusive access to device */
  1119. scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1); /* Notify us of login complete */
  1120. scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
  1121. scsi_id->login_orb->passwd_resp_lengths =
  1122. ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
  1123. scsi_id->login_orb->status_fifo_hi =
  1124. ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
  1125. scsi_id->login_orb->status_fifo_lo =
  1126. ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
  1127. sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
  1128. sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb),
  1129. "sbp2 login orb", scsi_id->login_orb_dma);
  1130. memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
  1131. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1132. data[1] = scsi_id->login_orb_dma;
  1133. sbp2util_cpu_to_be32_buffer(data, 8);
  1134. hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
  1135. /*
  1136. * Wait for login status (up to 20 seconds)...
  1137. */
  1138. if (sbp2util_access_timeout(scsi_id, 20*HZ)) {
  1139. SBP2_ERR("Error logging into SBP-2 device - timed out");
  1140. return -EIO;
  1141. }
  1142. /*
  1143. * Sanity. Make sure status returned matches login orb.
  1144. */
  1145. if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
  1146. SBP2_ERR("Error logging into SBP-2 device - timed out");
  1147. return -EIO;
  1148. }
  1149. if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
  1150. SBP2_ERR("Error logging into SBP-2 device - failed");
  1151. return -EIO;
  1152. }
  1153. /*
  1154. * Byte swap the login response, for use when reconnecting or
  1155. * logging out.
  1156. */
  1157. sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
  1158. /*
  1159. * Grab our command block agent address from the login response.
  1160. */
  1161. SBP2_DEBUG("command_block_agent_hi = %x",
  1162. (unsigned int)scsi_id->login_response->command_block_agent_hi);
  1163. SBP2_DEBUG("command_block_agent_lo = %x",
  1164. (unsigned int)scsi_id->login_response->command_block_agent_lo);
  1165. scsi_id->sbp2_command_block_agent_addr =
  1166. ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
  1167. scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
  1168. scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
  1169. SBP2_INFO("Logged into SBP-2 device");
  1170. return 0;
  1171. }
  1172. /*
  1173. * This function is called in order to logout from a particular SBP-2
  1174. * device, usually called during driver unload.
  1175. */
  1176. static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
  1177. {
  1178. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1179. quadlet_t data[2];
  1180. int error;
  1181. SBP2_DEBUG_ENTER();
  1182. /*
  1183. * Set-up logout ORB
  1184. */
  1185. scsi_id->logout_orb->reserved1 = 0x0;
  1186. scsi_id->logout_orb->reserved2 = 0x0;
  1187. scsi_id->logout_orb->reserved3 = 0x0;
  1188. scsi_id->logout_orb->reserved4 = 0x0;
  1189. scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
  1190. scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
  1191. /* Notify us when complete */
  1192. scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1193. scsi_id->logout_orb->reserved5 = 0x0;
  1194. scsi_id->logout_orb->status_fifo_hi =
  1195. ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
  1196. scsi_id->logout_orb->status_fifo_lo =
  1197. ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
  1198. /*
  1199. * Byte swap ORB if necessary
  1200. */
  1201. sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
  1202. sbp2util_packet_dump(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb),
  1203. "sbp2 logout orb", scsi_id->logout_orb_dma);
  1204. /*
  1205. * Ok, let's write to the target's management agent register
  1206. */
  1207. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1208. data[1] = scsi_id->logout_orb_dma;
  1209. sbp2util_cpu_to_be32_buffer(data, 8);
  1210. error = hpsb_node_write(scsi_id->ne,
  1211. scsi_id->sbp2_management_agent_addr, data, 8);
  1212. if (error)
  1213. return error;
  1214. /* Wait for device to logout...1 second. */
  1215. if (sbp2util_access_timeout(scsi_id, HZ))
  1216. return -EIO;
  1217. SBP2_INFO("Logged out of SBP-2 device");
  1218. return 0;
  1219. }
  1220. /*
  1221. * This function is called in order to reconnect to a particular SBP-2
  1222. * device, after a bus reset.
  1223. */
  1224. static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
  1225. {
  1226. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1227. quadlet_t data[2];
  1228. int error;
  1229. SBP2_DEBUG_ENTER();
  1230. /*
  1231. * Set-up reconnect ORB
  1232. */
  1233. scsi_id->reconnect_orb->reserved1 = 0x0;
  1234. scsi_id->reconnect_orb->reserved2 = 0x0;
  1235. scsi_id->reconnect_orb->reserved3 = 0x0;
  1236. scsi_id->reconnect_orb->reserved4 = 0x0;
  1237. scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
  1238. scsi_id->reconnect_orb->login_ID_misc |=
  1239. ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
  1240. /* Notify us when complete */
  1241. scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
  1242. scsi_id->reconnect_orb->reserved5 = 0x0;
  1243. scsi_id->reconnect_orb->status_fifo_hi =
  1244. ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
  1245. scsi_id->reconnect_orb->status_fifo_lo =
  1246. ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
  1247. /*
  1248. * Byte swap ORB if necessary
  1249. */
  1250. sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
  1251. sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb),
  1252. "sbp2 reconnect orb", scsi_id->reconnect_orb_dma);
  1253. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1254. data[1] = scsi_id->reconnect_orb_dma;
  1255. sbp2util_cpu_to_be32_buffer(data, 8);
  1256. error = hpsb_node_write(scsi_id->ne,
  1257. scsi_id->sbp2_management_agent_addr, data, 8);
  1258. if (error)
  1259. return error;
  1260. /*
  1261. * Wait for reconnect status (up to 1 second)...
  1262. */
  1263. if (sbp2util_access_timeout(scsi_id, HZ)) {
  1264. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1265. return -EIO;
  1266. }
  1267. /*
  1268. * Sanity. Make sure status returned matches reconnect orb.
  1269. */
  1270. if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
  1271. SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
  1272. return -EIO;
  1273. }
  1274. if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
  1275. SBP2_ERR("Error reconnecting to SBP-2 device - failed");
  1276. return -EIO;
  1277. }
  1278. HPSB_DEBUG("Reconnected to SBP-2 device");
  1279. return 0;
  1280. }
  1281. /*
  1282. * This function is called in order to set the busy timeout (number of
  1283. * retries to attempt) on the sbp2 device.
  1284. */
  1285. static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
  1286. {
  1287. quadlet_t data;
  1288. SBP2_DEBUG_ENTER();
  1289. data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
  1290. if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
  1291. SBP2_ERR("%s error", __FUNCTION__);
  1292. return 0;
  1293. }
  1294. /*
  1295. * This function is called to parse sbp2 device's config rom unit
  1296. * directory. Used to determine things like sbp2 management agent offset,
  1297. * and command set used (SCSI or RBC).
  1298. */
  1299. static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
  1300. struct unit_directory *ud)
  1301. {
  1302. struct csr1212_keyval *kv;
  1303. struct csr1212_dentry *dentry;
  1304. u64 management_agent_addr;
  1305. u32 command_set_spec_id, command_set, unit_characteristics,
  1306. firmware_revision;
  1307. unsigned workarounds;
  1308. int i;
  1309. SBP2_DEBUG_ENTER();
  1310. management_agent_addr = 0x0;
  1311. command_set_spec_id = 0x0;
  1312. command_set = 0x0;
  1313. unit_characteristics = 0x0;
  1314. firmware_revision = 0x0;
  1315. /* Handle different fields in the unit directory, based on keys */
  1316. csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
  1317. switch (kv->key.id) {
  1318. case CSR1212_KV_ID_DEPENDENT_INFO:
  1319. if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET) {
  1320. /* Save off the management agent address */
  1321. management_agent_addr =
  1322. CSR1212_REGISTER_SPACE_BASE +
  1323. (kv->value.csr_offset << 2);
  1324. SBP2_DEBUG("sbp2_management_agent_addr = %x",
  1325. (unsigned int)management_agent_addr);
  1326. } else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
  1327. scsi_id->sbp2_lun =
  1328. ORB_SET_LUN(kv->value.immediate);
  1329. }
  1330. break;
  1331. case SBP2_COMMAND_SET_SPEC_ID_KEY:
  1332. /* Command spec organization */
  1333. command_set_spec_id = kv->value.immediate;
  1334. SBP2_DEBUG("sbp2_command_set_spec_id = %x",
  1335. (unsigned int)command_set_spec_id);
  1336. break;
  1337. case SBP2_COMMAND_SET_KEY:
  1338. /* Command set used by sbp2 device */
  1339. command_set = kv->value.immediate;
  1340. SBP2_DEBUG("sbp2_command_set = %x",
  1341. (unsigned int)command_set);
  1342. break;
  1343. case SBP2_UNIT_CHARACTERISTICS_KEY:
  1344. /*
  1345. * Unit characterisitcs (orb related stuff
  1346. * that I'm not yet paying attention to)
  1347. */
  1348. unit_characteristics = kv->value.immediate;
  1349. SBP2_DEBUG("sbp2_unit_characteristics = %x",
  1350. (unsigned int)unit_characteristics);
  1351. break;
  1352. case SBP2_FIRMWARE_REVISION_KEY:
  1353. /* Firmware revision */
  1354. firmware_revision = kv->value.immediate;
  1355. SBP2_DEBUG("sbp2_firmware_revision = %x",
  1356. (unsigned int)firmware_revision);
  1357. break;
  1358. default:
  1359. break;
  1360. }
  1361. }
  1362. workarounds = sbp2_default_workarounds;
  1363. if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
  1364. for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
  1365. if (sbp2_workarounds_table[i].firmware_revision &&
  1366. sbp2_workarounds_table[i].firmware_revision !=
  1367. (firmware_revision & 0xffff00))
  1368. continue;
  1369. if (sbp2_workarounds_table[i].model_id &&
  1370. sbp2_workarounds_table[i].model_id != ud->model_id)
  1371. continue;
  1372. workarounds |= sbp2_workarounds_table[i].workarounds;
  1373. break;
  1374. }
  1375. if (workarounds)
  1376. SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
  1377. "(firmware_revision 0x%06x, vendor_id 0x%06x,"
  1378. " model_id 0x%06x)",
  1379. NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
  1380. workarounds, firmware_revision,
  1381. ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
  1382. ud->model_id);
  1383. /* We would need one SCSI host template for each target to adjust
  1384. * max_sectors on the fly, therefore warn only. */
  1385. if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
  1386. (max_sectors * 512) > (128 * 1024))
  1387. SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
  1388. "max transfer size. WARNING: Current max_sectors "
  1389. "setting is larger than 128KB (%d sectors)",
  1390. NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
  1391. max_sectors);
  1392. /* If this is a logical unit directory entry, process the parent
  1393. * to get the values. */
  1394. if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
  1395. struct unit_directory *parent_ud =
  1396. container_of(ud->device.parent, struct unit_directory, device);
  1397. sbp2_parse_unit_directory(scsi_id, parent_ud);
  1398. } else {
  1399. scsi_id->sbp2_management_agent_addr = management_agent_addr;
  1400. scsi_id->sbp2_command_set_spec_id = command_set_spec_id;
  1401. scsi_id->sbp2_command_set = command_set;
  1402. scsi_id->sbp2_unit_characteristics = unit_characteristics;
  1403. scsi_id->sbp2_firmware_revision = firmware_revision;
  1404. scsi_id->workarounds = workarounds;
  1405. if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
  1406. scsi_id->sbp2_lun = ORB_SET_LUN(ud->lun);
  1407. }
  1408. }
  1409. #define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
  1410. /*
  1411. * This function is called in order to determine the max speed and packet
  1412. * size we can use in our ORBs. Note, that we (the driver and host) only
  1413. * initiate the transaction. The SBP-2 device actually transfers the data
  1414. * (by reading from the DMA area we tell it). This means that the SBP-2
  1415. * device decides the actual maximum data it can transfer. We just tell it
  1416. * the speed that it needs to use, and the max_rec the host supports, and
  1417. * it takes care of the rest.
  1418. */
  1419. static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
  1420. {
  1421. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1422. u8 payload;
  1423. SBP2_DEBUG_ENTER();
  1424. scsi_id->speed_code =
  1425. hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)];
  1426. /* Bump down our speed if the user requested it */
  1427. if (scsi_id->speed_code > max_speed) {
  1428. scsi_id->speed_code = max_speed;
  1429. SBP2_ERR("Forcing SBP-2 max speed down to %s",
  1430. hpsb_speedto_str[scsi_id->speed_code]);
  1431. }
  1432. /* Payload size is the lesser of what our speed supports and what
  1433. * our host supports. */
  1434. payload = min(sbp2_speedto_max_payload[scsi_id->speed_code],
  1435. (u8) (hi->host->csr.max_rec - 1));
  1436. /* If physical DMA is off, work around limitation in ohci1394:
  1437. * packet size must not exceed PAGE_SIZE */
  1438. if (scsi_id->ne->host->low_addr_space < (1ULL << 32))
  1439. while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
  1440. payload)
  1441. payload--;
  1442. HPSB_DEBUG("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
  1443. NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
  1444. hpsb_speedto_str[scsi_id->speed_code],
  1445. SBP2_PAYLOAD_TO_BYTES(payload));
  1446. scsi_id->max_payload_size = payload;
  1447. return 0;
  1448. }
  1449. /*
  1450. * This function is called in order to perform a SBP-2 agent reset.
  1451. */
  1452. static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
  1453. {
  1454. quadlet_t data;
  1455. u64 addr;
  1456. int retval;
  1457. unsigned long flags;
  1458. SBP2_DEBUG_ENTER();
  1459. cancel_delayed_work(&scsi_id->protocol_work);
  1460. if (wait)
  1461. flush_scheduled_work();
  1462. data = ntohl(SBP2_AGENT_RESET_DATA);
  1463. addr = scsi_id->sbp2_command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
  1464. if (wait)
  1465. retval = hpsb_node_write(scsi_id->ne, addr, &data, 4);
  1466. else
  1467. retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4);
  1468. if (retval < 0) {
  1469. SBP2_ERR("hpsb_node_write failed.\n");
  1470. return -EIO;
  1471. }
  1472. /*
  1473. * Need to make sure orb pointer is written on next command
  1474. */
  1475. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  1476. scsi_id->last_orb = NULL;
  1477. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  1478. return 0;
  1479. }
  1480. static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
  1481. struct sbp2scsi_host_info *hi,
  1482. struct sbp2_command_info *command,
  1483. unsigned int scsi_use_sg,
  1484. struct scatterlist *sgpnt,
  1485. u32 orb_direction,
  1486. enum dma_data_direction dma_dir)
  1487. {
  1488. command->dma_dir = dma_dir;
  1489. orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1490. orb->misc |= ORB_SET_DIRECTION(orb_direction);
  1491. /* Special case if only one element (and less than 64KB in size) */
  1492. if ((scsi_use_sg == 1) &&
  1493. (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
  1494. SBP2_DEBUG("Only one s/g element");
  1495. command->dma_size = sgpnt[0].length;
  1496. command->dma_type = CMD_DMA_PAGE;
  1497. command->cmd_dma = pci_map_page(hi->host->pdev,
  1498. sgpnt[0].page,
  1499. sgpnt[0].offset,
  1500. command->dma_size,
  1501. command->dma_dir);
  1502. SBP2_DMA_ALLOC("single page scatter element");
  1503. orb->data_descriptor_lo = command->cmd_dma;
  1504. orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
  1505. } else {
  1506. struct sbp2_unrestricted_page_table *sg_element =
  1507. &command->scatter_gather_element[0];
  1508. u32 sg_count, sg_len;
  1509. dma_addr_t sg_addr;
  1510. int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
  1511. dma_dir);
  1512. SBP2_DMA_ALLOC("scatter list");
  1513. command->dma_size = scsi_use_sg;
  1514. command->sge_buffer = sgpnt;
  1515. /* use page tables (s/g) */
  1516. orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
  1517. orb->data_descriptor_lo = command->sge_dma;
  1518. /*
  1519. * Loop through and fill out our sbp-2 page tables
  1520. * (and split up anything too large)
  1521. */
  1522. for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
  1523. sg_len = sg_dma_len(sgpnt);
  1524. sg_addr = sg_dma_address(sgpnt);
  1525. while (sg_len) {
  1526. sg_element[sg_count].segment_base_lo = sg_addr;
  1527. if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
  1528. sg_element[sg_count].length_segment_base_hi =
  1529. PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
  1530. sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
  1531. sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
  1532. } else {
  1533. sg_element[sg_count].length_segment_base_hi =
  1534. PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
  1535. sg_len = 0;
  1536. }
  1537. sg_count++;
  1538. }
  1539. }
  1540. /* Number of page table (s/g) elements */
  1541. orb->misc |= ORB_SET_DATA_SIZE(sg_count);
  1542. sbp2util_packet_dump(sg_element,
  1543. (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
  1544. "sbp2 s/g list", command->sge_dma);
  1545. /* Byte swap page tables if necessary */
  1546. sbp2util_cpu_to_be32_buffer(sg_element,
  1547. (sizeof(struct sbp2_unrestricted_page_table)) *
  1548. sg_count);
  1549. }
  1550. }
  1551. static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
  1552. struct sbp2scsi_host_info *hi,
  1553. struct sbp2_command_info *command,
  1554. struct scatterlist *sgpnt,
  1555. u32 orb_direction,
  1556. unsigned int scsi_request_bufflen,
  1557. void *scsi_request_buffer,
  1558. enum dma_data_direction dma_dir)
  1559. {
  1560. command->dma_dir = dma_dir;
  1561. command->dma_size = scsi_request_bufflen;
  1562. command->dma_type = CMD_DMA_SINGLE;
  1563. command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
  1564. command->dma_size, command->dma_dir);
  1565. orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
  1566. orb->misc |= ORB_SET_DIRECTION(orb_direction);
  1567. SBP2_DMA_ALLOC("single bulk");
  1568. /*
  1569. * Handle case where we get a command w/o s/g enabled (but
  1570. * check for transfers larger than 64K)
  1571. */
  1572. if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
  1573. orb->data_descriptor_lo = command->cmd_dma;
  1574. orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
  1575. } else {
  1576. struct sbp2_unrestricted_page_table *sg_element =
  1577. &command->scatter_gather_element[0];
  1578. u32 sg_count, sg_len;
  1579. dma_addr_t sg_addr;
  1580. /*
  1581. * Need to turn this into page tables, since the
  1582. * buffer is too large.
  1583. */
  1584. orb->data_descriptor_lo = command->sge_dma;
  1585. /* Use page tables (s/g) */
  1586. orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
  1587. /*
  1588. * fill out our sbp-2 page tables (and split up
  1589. * the large buffer)
  1590. */
  1591. sg_count = 0;
  1592. sg_len = scsi_request_bufflen;
  1593. sg_addr = command->cmd_dma;
  1594. while (sg_len) {
  1595. sg_element[sg_count].segment_base_lo = sg_addr;
  1596. if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
  1597. sg_element[sg_count].length_segment_base_hi =
  1598. PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
  1599. sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
  1600. sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
  1601. } else {
  1602. sg_element[sg_count].length_segment_base_hi =
  1603. PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
  1604. sg_len = 0;
  1605. }
  1606. sg_count++;
  1607. }
  1608. /* Number of page table (s/g) elements */
  1609. orb->misc |= ORB_SET_DATA_SIZE(sg_count);
  1610. sbp2util_packet_dump(sg_element,
  1611. (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
  1612. "sbp2 s/g list", command->sge_dma);
  1613. /* Byte swap page tables if necessary */
  1614. sbp2util_cpu_to_be32_buffer(sg_element,
  1615. (sizeof(struct sbp2_unrestricted_page_table)) *
  1616. sg_count);
  1617. }
  1618. }
  1619. /*
  1620. * This function is called to create the actual command orb and s/g list
  1621. * out of the scsi command itself.
  1622. */
  1623. static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
  1624. struct sbp2_command_info *command,
  1625. unchar *scsi_cmd,
  1626. unsigned int scsi_use_sg,
  1627. unsigned int scsi_request_bufflen,
  1628. void *scsi_request_buffer,
  1629. enum dma_data_direction dma_dir)
  1630. {
  1631. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1632. struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
  1633. struct sbp2_command_orb *command_orb = &command->command_orb;
  1634. u32 orb_direction;
  1635. /*
  1636. * Set-up our command ORB..
  1637. *
  1638. * NOTE: We're doing unrestricted page tables (s/g), as this is
  1639. * best performance (at least with the devices I have). This means
  1640. * that data_size becomes the number of s/g elements, and
  1641. * page_size should be zero (for unrestricted).
  1642. */
  1643. command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
  1644. command_orb->next_ORB_lo = 0x0;
  1645. command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
  1646. command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
  1647. command_orb->misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */
  1648. if (dma_dir == DMA_NONE)
  1649. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1650. else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
  1651. orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
  1652. else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
  1653. orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
  1654. else {
  1655. SBP2_WARN("Falling back to DMA_NONE");
  1656. orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
  1657. }
  1658. /* Set-up our pagetable stuff */
  1659. if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
  1660. SBP2_DEBUG("No data transfer");
  1661. command_orb->data_descriptor_hi = 0x0;
  1662. command_orb->data_descriptor_lo = 0x0;
  1663. command_orb->misc |= ORB_SET_DIRECTION(1);
  1664. } else if (scsi_use_sg) {
  1665. SBP2_DEBUG("Use scatter/gather");
  1666. sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg,
  1667. sgpnt, orb_direction, dma_dir);
  1668. } else {
  1669. SBP2_DEBUG("No scatter/gather");
  1670. sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
  1671. orb_direction, scsi_request_bufflen,
  1672. scsi_request_buffer, dma_dir);
  1673. }
  1674. /* Byte swap command ORB if necessary */
  1675. sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
  1676. /* Put our scsi command in the command ORB */
  1677. memset(command_orb->cdb, 0, 12);
  1678. memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
  1679. }
  1680. /*
  1681. * This function is called in order to begin a regular SBP-2 command.
  1682. */
  1683. static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
  1684. struct sbp2_command_info *command)
  1685. {
  1686. struct sbp2scsi_host_info *hi = scsi_id->hi;
  1687. struct sbp2_command_orb *command_orb = &command->command_orb;
  1688. struct sbp2_command_orb *last_orb;
  1689. dma_addr_t last_orb_dma;
  1690. u64 addr = scsi_id->sbp2_command_block_agent_addr;
  1691. quadlet_t data[2];
  1692. size_t length;
  1693. unsigned long flags;
  1694. outstanding_orb_incr;
  1695. SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x",
  1696. command_orb, global_outstanding_command_orbs);
  1697. pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
  1698. sizeof(struct sbp2_command_orb),
  1699. PCI_DMA_TODEVICE);
  1700. pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
  1701. sizeof(command->scatter_gather_element),
  1702. PCI_DMA_BIDIRECTIONAL);
  1703. /*
  1704. * Check to see if there are any previous orbs to use
  1705. */
  1706. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  1707. last_orb = scsi_id->last_orb;
  1708. last_orb_dma = scsi_id->last_orb_dma;
  1709. if (!last_orb) {
  1710. /*
  1711. * last_orb == NULL means: We know that the target's fetch agent
  1712. * is not active right now.
  1713. */
  1714. addr += SBP2_ORB_POINTER_OFFSET;
  1715. data[0] = ORB_SET_NODE_ID(hi->host->node_id);
  1716. data[1] = command->command_orb_dma;
  1717. sbp2util_cpu_to_be32_buffer(data, 8);
  1718. length = 8;
  1719. } else {
  1720. /*
  1721. * last_orb != NULL means: We know that the target's fetch agent
  1722. * is (very probably) not dead or in reset state right now.
  1723. * We have an ORB already sent that we can append a new one to.
  1724. * The target's fetch agent may or may not have read this
  1725. * previous ORB yet.
  1726. */
  1727. pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma,
  1728. sizeof(struct sbp2_command_orb),
  1729. PCI_DMA_TODEVICE);
  1730. last_orb->next_ORB_lo = cpu_to_be32(command->command_orb_dma);
  1731. wmb();
  1732. /* Tells hardware that this pointer is valid */
  1733. last_orb->next_ORB_hi = 0;
  1734. pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma,
  1735. sizeof(struct sbp2_command_orb),
  1736. PCI_DMA_TODEVICE);
  1737. addr += SBP2_DOORBELL_OFFSET;
  1738. data[0] = 0;
  1739. length = 4;
  1740. }
  1741. scsi_id->last_orb = command_orb;
  1742. scsi_id->last_orb_dma = command->command_orb_dma;
  1743. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  1744. SBP2_ORB_DEBUG("write to %s register, command orb %p",
  1745. last_orb ? "DOORBELL" : "ORB_POINTER", command_orb);
  1746. if (sbp2util_node_write_no_wait(scsi_id->ne, addr, data, length)) {
  1747. /*
  1748. * sbp2util_node_write_no_wait failed. We certainly ran out
  1749. * of transaction labels, perhaps just because there were no
  1750. * context switches which gave khpsbpkt a chance to collect
  1751. * free tlabels. Try again in non-atomic context. If necessary,
  1752. * the workqueue job will sleep to guaranteedly get a tlabel.
  1753. * We do not accept new commands until the job is over.
  1754. */
  1755. scsi_block_requests(scsi_id->scsi_host);
  1756. PREPARE_DELAYED_WORK(&scsi_id->protocol_work,
  1757. last_orb ? sbp2util_write_doorbell:
  1758. sbp2util_write_orb_pointer);
  1759. schedule_delayed_work(&scsi_id->protocol_work, 0);
  1760. }
  1761. }
  1762. /*
  1763. * This function is called in order to begin a regular SBP-2 command.
  1764. */
  1765. static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
  1766. struct scsi_cmnd *SCpnt,
  1767. void (*done)(struct scsi_cmnd *))
  1768. {
  1769. unchar *cmd = (unchar *) SCpnt->cmnd;
  1770. unsigned int request_bufflen = SCpnt->request_bufflen;
  1771. struct sbp2_command_info *command;
  1772. SBP2_DEBUG_ENTER();
  1773. SBP2_DEBUG("SCSI transfer size = %x", request_bufflen);
  1774. SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
  1775. /*
  1776. * Allocate a command orb and s/g structure
  1777. */
  1778. command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
  1779. if (!command) {
  1780. return -EIO;
  1781. }
  1782. /*
  1783. * Now actually fill in the comamnd orb and sbp2 s/g list
  1784. */
  1785. sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
  1786. request_bufflen, SCpnt->request_buffer,
  1787. SCpnt->sc_data_direction);
  1788. sbp2util_packet_dump(&command->command_orb, sizeof(struct sbp2_command_orb),
  1789. "sbp2 command orb", command->command_orb_dma);
  1790. /*
  1791. * Link up the orb, and ring the doorbell if needed
  1792. */
  1793. sbp2_link_orb_command(scsi_id, command);
  1794. return 0;
  1795. }
  1796. /*
  1797. * Translates SBP-2 status into SCSI sense data for check conditions
  1798. */
  1799. static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
  1800. {
  1801. SBP2_DEBUG_ENTER();
  1802. /*
  1803. * Ok, it's pretty ugly... ;-)
  1804. */
  1805. sense_data[0] = 0x70;
  1806. sense_data[1] = 0x0;
  1807. sense_data[2] = sbp2_status[9];
  1808. sense_data[3] = sbp2_status[12];
  1809. sense_data[4] = sbp2_status[13];
  1810. sense_data[5] = sbp2_status[14];
  1811. sense_data[6] = sbp2_status[15];
  1812. sense_data[7] = 10;
  1813. sense_data[8] = sbp2_status[16];
  1814. sense_data[9] = sbp2_status[17];
  1815. sense_data[10] = sbp2_status[18];
  1816. sense_data[11] = sbp2_status[19];
  1817. sense_data[12] = sbp2_status[10];
  1818. sense_data[13] = sbp2_status[11];
  1819. sense_data[14] = sbp2_status[20];
  1820. sense_data[15] = sbp2_status[21];
  1821. return sbp2_status[8] & 0x3f; /* return scsi status */
  1822. }
  1823. /*
  1824. * This function deals with status writes from the SBP-2 device
  1825. */
  1826. static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
  1827. int destid, quadlet_t *data, u64 addr,
  1828. size_t length, u16 fl)
  1829. {
  1830. struct sbp2scsi_host_info *hi;
  1831. struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
  1832. struct scsi_cmnd *SCpnt = NULL;
  1833. struct sbp2_status_block *sb;
  1834. u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
  1835. struct sbp2_command_info *command;
  1836. unsigned long flags;
  1837. SBP2_DEBUG_ENTER();
  1838. sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr);
  1839. if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
  1840. SBP2_ERR("Wrong size of status block");
  1841. return RCODE_ADDRESS_ERROR;
  1842. }
  1843. if (unlikely(!host)) {
  1844. SBP2_ERR("host is NULL - this is bad!");
  1845. return RCODE_ADDRESS_ERROR;
  1846. }
  1847. hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
  1848. if (unlikely(!hi)) {
  1849. SBP2_ERR("host info is NULL - this is bad!");
  1850. return RCODE_ADDRESS_ERROR;
  1851. }
  1852. /*
  1853. * Find our scsi_id structure by looking at the status fifo address
  1854. * written to by the sbp2 device.
  1855. */
  1856. list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
  1857. if (scsi_id_tmp->ne->nodeid == nodeid &&
  1858. scsi_id_tmp->status_fifo_addr == addr) {
  1859. scsi_id = scsi_id_tmp;
  1860. break;
  1861. }
  1862. }
  1863. if (unlikely(!scsi_id)) {
  1864. SBP2_ERR("scsi_id is NULL - device is gone?");
  1865. return RCODE_ADDRESS_ERROR;
  1866. }
  1867. /*
  1868. * Put response into scsi_id status fifo buffer. The first two bytes
  1869. * come in big endian bit order. Often the target writes only a
  1870. * truncated status block, minimally the first two quadlets. The rest
  1871. * is implied to be zeros.
  1872. */
  1873. sb = &scsi_id->status_block;
  1874. memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
  1875. memcpy(sb, data, length);
  1876. sbp2util_be32_to_cpu_buffer(sb, 8);
  1877. /*
  1878. * Ignore unsolicited status. Handle command ORB status.
  1879. */
  1880. if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
  1881. command = NULL;
  1882. else
  1883. command = sbp2util_find_command_for_orb(scsi_id,
  1884. sb->ORB_offset_lo);
  1885. if (command) {
  1886. SBP2_DEBUG("Found status for command ORB");
  1887. pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
  1888. sizeof(struct sbp2_command_orb),
  1889. PCI_DMA_TODEVICE);
  1890. pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
  1891. sizeof(command->scatter_gather_element),
  1892. PCI_DMA_BIDIRECTIONAL);
  1893. SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb);
  1894. outstanding_orb_decr;
  1895. /*
  1896. * Matched status with command, now grab scsi command pointers
  1897. * and check status.
  1898. */
  1899. /*
  1900. * FIXME: If the src field in the status is 1, the ORB DMA must
  1901. * not be reused until status for a subsequent ORB is received.
  1902. */
  1903. SCpnt = command->Current_SCpnt;
  1904. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  1905. sbp2util_mark_command_completed(scsi_id, command);
  1906. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  1907. if (SCpnt) {
  1908. u32 h = sb->ORB_offset_hi_misc;
  1909. u32 r = STATUS_GET_RESP(h);
  1910. if (r != RESP_STATUS_REQUEST_COMPLETE) {
  1911. SBP2_WARN("resp 0x%x, sbp_status 0x%x",
  1912. r, STATUS_GET_SBP_STATUS(h));
  1913. scsi_status =
  1914. r == RESP_STATUS_TRANSPORT_FAILURE ?
  1915. SBP2_SCSI_STATUS_BUSY :
  1916. SBP2_SCSI_STATUS_COMMAND_TERMINATED;
  1917. }
  1918. /*
  1919. * See if the target stored any scsi status information.
  1920. */
  1921. if (STATUS_GET_LEN(h) > 1) {
  1922. SBP2_DEBUG("CHECK CONDITION");
  1923. scsi_status = sbp2_status_to_sense_data(
  1924. (unchar *)sb, SCpnt->sense_buffer);
  1925. }
  1926. /*
  1927. * Check to see if the dead bit is set. If so, we'll
  1928. * have to initiate a fetch agent reset.
  1929. */
  1930. if (STATUS_TEST_DEAD(h)) {
  1931. SBP2_DEBUG("Dead bit set - "
  1932. "initiating fetch agent reset");
  1933. sbp2_agent_reset(scsi_id, 0);
  1934. }
  1935. SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
  1936. }
  1937. /*
  1938. * Check here to see if there are no commands in-use. If there
  1939. * are none, we know that the fetch agent left the active state
  1940. * _and_ that we did not reactivate it yet. Therefore clear
  1941. * last_orb so that next time we write directly to the
  1942. * ORB_POINTER register. That way the fetch agent does not need
  1943. * to refetch the next_ORB.
  1944. */
  1945. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  1946. if (list_empty(&scsi_id->sbp2_command_orb_inuse))
  1947. scsi_id->last_orb = NULL;
  1948. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  1949. } else {
  1950. /*
  1951. * It's probably a login/logout/reconnect status.
  1952. */
  1953. if ((sb->ORB_offset_lo == scsi_id->reconnect_orb_dma) ||
  1954. (sb->ORB_offset_lo == scsi_id->login_orb_dma) ||
  1955. (sb->ORB_offset_lo == scsi_id->query_logins_orb_dma) ||
  1956. (sb->ORB_offset_lo == scsi_id->logout_orb_dma)) {
  1957. scsi_id->access_complete = 1;
  1958. wake_up_interruptible(&access_wq);
  1959. }
  1960. }
  1961. if (SCpnt) {
  1962. SBP2_DEBUG("Completing SCSI command");
  1963. sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
  1964. command->Current_done);
  1965. SBP2_ORB_DEBUG("command orb completed");
  1966. }
  1967. return RCODE_COMPLETE;
  1968. }
  1969. /**************************************
  1970. * SCSI interface related section
  1971. **************************************/
  1972. /*
  1973. * This routine is the main request entry routine for doing I/O. It is
  1974. * called from the scsi stack directly.
  1975. */
  1976. static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
  1977. void (*done)(struct scsi_cmnd *))
  1978. {
  1979. struct scsi_id_instance_data *scsi_id =
  1980. (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
  1981. struct sbp2scsi_host_info *hi;
  1982. int result = DID_NO_CONNECT << 16;
  1983. SBP2_DEBUG_ENTER();
  1984. #if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
  1985. scsi_print_command(SCpnt);
  1986. #endif
  1987. if (!sbp2util_node_is_available(scsi_id))
  1988. goto done;
  1989. hi = scsi_id->hi;
  1990. if (!hi) {
  1991. SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!");
  1992. goto done;
  1993. }
  1994. /*
  1995. * Until we handle multiple luns, just return selection time-out
  1996. * to any IO directed at non-zero LUNs
  1997. */
  1998. if (SCpnt->device->lun)
  1999. goto done;
  2000. /*
  2001. * Check for request sense command, and handle it here
  2002. * (autorequest sense)
  2003. */
  2004. if (SCpnt->cmnd[0] == REQUEST_SENSE) {
  2005. SBP2_DEBUG("REQUEST_SENSE");
  2006. memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
  2007. memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
  2008. sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
  2009. return 0;
  2010. }
  2011. /*
  2012. * Check to see if we are in the middle of a bus reset.
  2013. */
  2014. if (!hpsb_node_entry_valid(scsi_id->ne)) {
  2015. SBP2_ERR("Bus reset in progress - rejecting command");
  2016. result = DID_BUS_BUSY << 16;
  2017. goto done;
  2018. }
  2019. /*
  2020. * Bidirectional commands are not yet implemented,
  2021. * and unknown transfer direction not handled.
  2022. */
  2023. if (SCpnt->sc_data_direction == DMA_BIDIRECTIONAL) {
  2024. SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
  2025. result = DID_ERROR << 16;
  2026. goto done;
  2027. }
  2028. /*
  2029. * Try and send our SCSI command
  2030. */
  2031. if (sbp2_send_command(scsi_id, SCpnt, done)) {
  2032. SBP2_ERR("Error sending SCSI command");
  2033. sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
  2034. SCpnt, done);
  2035. }
  2036. return 0;
  2037. done:
  2038. SCpnt->result = result;
  2039. done(SCpnt);
  2040. return 0;
  2041. }
  2042. /*
  2043. * This function is called in order to complete all outstanding SBP-2
  2044. * commands (in case of resets, etc.).
  2045. */
  2046. static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
  2047. u32 status)
  2048. {
  2049. struct sbp2scsi_host_info *hi = scsi_id->hi;
  2050. struct list_head *lh;
  2051. struct sbp2_command_info *command;
  2052. unsigned long flags;
  2053. SBP2_DEBUG_ENTER();
  2054. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  2055. while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
  2056. SBP2_DEBUG("Found pending command to complete");
  2057. lh = scsi_id->sbp2_command_orb_inuse.next;
  2058. command = list_entry(lh, struct sbp2_command_info, list);
  2059. pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
  2060. sizeof(struct sbp2_command_orb),
  2061. PCI_DMA_TODEVICE);
  2062. pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
  2063. sizeof(command->scatter_gather_element),
  2064. PCI_DMA_BIDIRECTIONAL);
  2065. sbp2util_mark_command_completed(scsi_id, command);
  2066. if (command->Current_SCpnt) {
  2067. command->Current_SCpnt->result = status << 16;
  2068. command->Current_done(command->Current_SCpnt);
  2069. }
  2070. }
  2071. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  2072. return;
  2073. }
  2074. /*
  2075. * This function is called in order to complete a regular SBP-2 command.
  2076. *
  2077. * This can be called in interrupt context.
  2078. */
  2079. static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
  2080. u32 scsi_status, struct scsi_cmnd *SCpnt,
  2081. void (*done)(struct scsi_cmnd *))
  2082. {
  2083. SBP2_DEBUG_ENTER();
  2084. /*
  2085. * Sanity
  2086. */
  2087. if (!SCpnt) {
  2088. SBP2_ERR("SCpnt is NULL");
  2089. return;
  2090. }
  2091. /*
  2092. * If a bus reset is in progress and there was an error, don't
  2093. * complete the command, just let it get retried at the end of the
  2094. * bus reset.
  2095. */
  2096. if (!hpsb_node_entry_valid(scsi_id->ne)
  2097. && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
  2098. SBP2_ERR("Bus reset in progress - retry command later");
  2099. return;
  2100. }
  2101. /*
  2102. * Switch on scsi status
  2103. */
  2104. switch (scsi_status) {
  2105. case SBP2_SCSI_STATUS_GOOD:
  2106. SCpnt->result = DID_OK << 16;
  2107. break;
  2108. case SBP2_SCSI_STATUS_BUSY:
  2109. SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
  2110. SCpnt->result = DID_BUS_BUSY << 16;
  2111. break;
  2112. case SBP2_SCSI_STATUS_CHECK_CONDITION:
  2113. SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION");
  2114. SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
  2115. #if CONFIG_IEEE1394_SBP2_DEBUG >= 1
  2116. scsi_print_command(SCpnt);
  2117. scsi_print_sense(SBP2_DEVICE_NAME, SCpnt);
  2118. #endif
  2119. break;
  2120. case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
  2121. SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
  2122. SCpnt->result = DID_NO_CONNECT << 16;
  2123. scsi_print_command(SCpnt);
  2124. break;
  2125. case SBP2_SCSI_STATUS_CONDITION_MET:
  2126. case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
  2127. case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
  2128. SBP2_ERR("Bad SCSI status = %x", scsi_status);
  2129. SCpnt->result = DID_ERROR << 16;
  2130. scsi_print_command(SCpnt);
  2131. break;
  2132. default:
  2133. SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
  2134. SCpnt->result = DID_ERROR << 16;
  2135. }
  2136. /*
  2137. * If a bus reset is in progress and there was an error, complete
  2138. * the command as busy so that it will get retried.
  2139. */
  2140. if (!hpsb_node_entry_valid(scsi_id->ne)
  2141. && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
  2142. SBP2_ERR("Completing command with busy (bus reset)");
  2143. SCpnt->result = DID_BUS_BUSY << 16;
  2144. }
  2145. /*
  2146. * If a unit attention occurs, return busy status so it gets
  2147. * retried... it could have happened because of a 1394 bus reset
  2148. * or hot-plug...
  2149. * XXX DID_BUS_BUSY is actually a bad idea because it will defy
  2150. * the scsi layer's retry logic.
  2151. */
  2152. #if 0
  2153. if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) &&
  2154. (SCpnt->sense_buffer[2] == UNIT_ATTENTION)) {
  2155. SBP2_DEBUG("UNIT ATTENTION - return busy");
  2156. SCpnt->result = DID_BUS_BUSY << 16;
  2157. }
  2158. #endif
  2159. /*
  2160. * Tell scsi stack that we're done with this command
  2161. */
  2162. done(SCpnt);
  2163. }
  2164. static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
  2165. {
  2166. struct scsi_id_instance_data *scsi_id =
  2167. (struct scsi_id_instance_data *)sdev->host->hostdata[0];
  2168. scsi_id->sdev = sdev;
  2169. sdev->allow_restart = 1;
  2170. if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36)
  2171. sdev->inquiry_len = 36;
  2172. return 0;
  2173. }
  2174. static int sbp2scsi_slave_configure(struct scsi_device *sdev)
  2175. {
  2176. struct scsi_id_instance_data *scsi_id =
  2177. (struct scsi_id_instance_data *)sdev->host->hostdata[0];
  2178. blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
  2179. sdev->use_10_for_rw = 1;
  2180. if (sdev->type == TYPE_DISK &&
  2181. scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
  2182. sdev->skip_ms_page_8 = 1;
  2183. if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
  2184. sdev->fix_capacity = 1;
  2185. return 0;
  2186. }
  2187. static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
  2188. {
  2189. ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL;
  2190. return;
  2191. }
  2192. /*
  2193. * Called by scsi stack when something has really gone wrong. Usually
  2194. * called when a command has timed-out for some reason.
  2195. */
  2196. static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
  2197. {
  2198. struct scsi_id_instance_data *scsi_id =
  2199. (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
  2200. struct sbp2scsi_host_info *hi = scsi_id->hi;
  2201. struct sbp2_command_info *command;
  2202. unsigned long flags;
  2203. SBP2_ERR("aborting sbp2 command");
  2204. scsi_print_command(SCpnt);
  2205. if (sbp2util_node_is_available(scsi_id)) {
  2206. /*
  2207. * Right now, just return any matching command structures
  2208. * to the free pool.
  2209. */
  2210. spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
  2211. command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
  2212. if (command) {
  2213. SBP2_DEBUG("Found command to abort");
  2214. pci_dma_sync_single_for_cpu(hi->host->pdev,
  2215. command->command_orb_dma,
  2216. sizeof(struct sbp2_command_orb),
  2217. PCI_DMA_TODEVICE);
  2218. pci_dma_sync_single_for_cpu(hi->host->pdev,
  2219. command->sge_dma,
  2220. sizeof(command->scatter_gather_element),
  2221. PCI_DMA_BIDIRECTIONAL);
  2222. sbp2util_mark_command_completed(scsi_id, command);
  2223. if (command->Current_SCpnt) {
  2224. command->Current_SCpnt->result = DID_ABORT << 16;
  2225. command->Current_done(command->Current_SCpnt);
  2226. }
  2227. }
  2228. spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
  2229. /*
  2230. * Initiate a fetch agent reset.
  2231. */
  2232. sbp2_agent_reset(scsi_id, 1);
  2233. sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
  2234. }
  2235. return SUCCESS;
  2236. }
  2237. /*
  2238. * Called by scsi stack when something has really gone wrong.
  2239. */
  2240. static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
  2241. {
  2242. struct scsi_id_instance_data *scsi_id =
  2243. (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
  2244. SBP2_ERR("reset requested");
  2245. if (sbp2util_node_is_available(scsi_id)) {
  2246. SBP2_ERR("Generating sbp2 fetch agent reset");
  2247. sbp2_agent_reset(scsi_id, 1);
  2248. }
  2249. return SUCCESS;
  2250. }
  2251. static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
  2252. struct device_attribute *attr,
  2253. char *buf)
  2254. {
  2255. struct scsi_device *sdev;
  2256. struct scsi_id_instance_data *scsi_id;
  2257. int lun;
  2258. if (!(sdev = to_scsi_device(dev)))
  2259. return 0;
  2260. if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0]))
  2261. return 0;
  2262. lun = ORB_SET_LUN(scsi_id->sbp2_lun);
  2263. return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
  2264. scsi_id->ud->id, lun);
  2265. }
  2266. static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
  2267. static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
  2268. &dev_attr_ieee1394_id,
  2269. NULL
  2270. };
  2271. MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
  2272. MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
  2273. MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
  2274. MODULE_LICENSE("GPL");
  2275. /* SCSI host template */
  2276. static struct scsi_host_template scsi_driver_template = {
  2277. .module = THIS_MODULE,
  2278. .name = "SBP-2 IEEE-1394",
  2279. .proc_name = SBP2_DEVICE_NAME,
  2280. .queuecommand = sbp2scsi_queuecommand,
  2281. .eh_abort_handler = sbp2scsi_abort,
  2282. .eh_device_reset_handler = sbp2scsi_reset,
  2283. .slave_alloc = sbp2scsi_slave_alloc,
  2284. .slave_configure = sbp2scsi_slave_configure,
  2285. .slave_destroy = sbp2scsi_slave_destroy,
  2286. .this_id = -1,
  2287. .sg_tablesize = SG_ALL,
  2288. .use_clustering = ENABLE_CLUSTERING,
  2289. .cmd_per_lun = SBP2_MAX_CMDS,
  2290. .can_queue = SBP2_MAX_CMDS,
  2291. .emulated = 1,
  2292. .sdev_attrs = sbp2_sysfs_sdev_attrs,
  2293. };
  2294. static int sbp2_module_init(void)
  2295. {
  2296. int ret;
  2297. SBP2_DEBUG_ENTER();
  2298. /* Module load debug option to force one command at a time (serializing I/O) */
  2299. if (serialize_io) {
  2300. SBP2_INFO("Driver forced to serialize I/O (serialize_io=1)");
  2301. SBP2_INFO("Try serialize_io=0 for better performance");
  2302. scsi_driver_template.can_queue = 1;
  2303. scsi_driver_template.cmd_per_lun = 1;
  2304. }
  2305. if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
  2306. (max_sectors * 512) > (128 * 1024))
  2307. max_sectors = 128 * 1024 / 512;
  2308. scsi_driver_template.max_sectors = max_sectors;
  2309. /* Register our high level driver with 1394 stack */
  2310. hpsb_register_highlevel(&sbp2_highlevel);
  2311. ret = hpsb_register_protocol(&sbp2_driver);
  2312. if (ret) {
  2313. SBP2_ERR("Failed to register protocol");
  2314. hpsb_unregister_highlevel(&sbp2_highlevel);
  2315. return ret;
  2316. }
  2317. return 0;
  2318. }
  2319. static void __exit sbp2_module_exit(void)
  2320. {
  2321. SBP2_DEBUG_ENTER();
  2322. hpsb_unregister_protocol(&sbp2_driver);
  2323. hpsb_unregister_highlevel(&sbp2_highlevel);
  2324. }
  2325. module_init(sbp2_module_init);
  2326. module_exit(sbp2_module_exit);