sbp2.c 76 KB

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