sbp2.c 77 KB

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